Coder Social home page Coder Social logo

Comments (5)

ocornut avatar ocornut commented on May 30, 2024

Hello,

I think, for people like me who creates opengl es context under windows, a define can be introduced, which prevents unnecessary opengl warnings.

This seems like a XY Problem. We should focus on fixing the initial problem instead:

Under windows I don't define any of these, because otherwise it starts asking for headers and functions to link them.

That's the point of those defines. You should clarify how you are using GL ES 2 under Windows in your main code (what does your other code looks like in terms of includes and init code, which library are you using, how are you compiling/linking) and we should strive to make any valid ES 2 setup work with our backend.

from imgui.

afraidofdark avatar afraidofdark commented on May 30, 2024

Here is the minimal code that will create the problem.
link to my glad file gles2.h

#include "SDL.h"
#include <ImGui/imgui.h>
#include <ImGui/backends/imgui_impl_opengl3.h>
#include <ImGui/backends/imgui_impl_sdl2.h>

#define GLAD_GLES2_IMPLEMENTATION
#include "glad/gles2.h"

#include <iostream>
#include <functional>
#include <string>

bool g_running = true;
SDL_Window* g_window = nullptr;
SDL_GLContext g_context = nullptr;

void GLDebugMessageCallback(GLenum source,
	GLenum type,
	GLuint id,
	GLenum severity,
	GLsizei length,
	const GLchar* msg,
	const void* data)
{
	std::cout << msg << std::endl;
}

int main(int argc, char* argv[])
{
	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_GAMECONTROLLER) < 0)
	{
		g_running = false;
	}
	else
	{
		SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
		SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
		SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
		SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
		SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
		SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
		SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
		SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);

		g_window =
			SDL_CreateWindow("ToolKit",
				SDL_WINDOWPOS_UNDEFINED,
				SDL_WINDOWPOS_UNDEFINED,
				1280,
				800,
				SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);

		if (g_window == nullptr)
		{
			g_running = false;
		}
		else
		{
			g_context = SDL_GL_CreateContext(g_window);

			if (g_context == nullptr)
			{
				g_running = false;
			}
			else
			{
				SDL_GL_MakeCurrent(g_window, g_context);
				SDL_GL_SetSwapInterval(0);
			}
		}

		IMGUI_CHECKVERSION();
		ImGui::CreateContext();

		ImGuiIO& io = ImGui::GetIO();
		io.ConfigFlags |= ImGuiConfigFlags_DockingEnable | ImGuiConfigFlags_ViewportsEnable;
		io.ConfigWindowsMoveFromTitleBarOnly = true;

		ImGui_ImplSDL2_InitForOpenGL(g_window, g_context);
		ImGui_ImplOpenGL3_Init("#version 300 es");

		// Load gl and error reporters
		gladLoadGLES2((GLADloadfunc)SDL_GL_GetProcAddress);

		if (glDebugMessageCallback != NULL)
		{
			glEnable(GL_DEBUG_OUTPUT);
			glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);

			glDebugMessageCallback(&GLDebugMessageCallback, nullptr);

			glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_NOTIFICATION, 0, nullptr, GL_FALSE);
			glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_LOW, 0, nullptr, GL_FALSE);
			glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_MEDIUM, 0, nullptr, GL_TRUE);
			glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_HIGH, 0, nullptr, GL_TRUE);
		}

		while (true)
		{
			SDL_Event sdlEvent;
			while (SDL_PollEvent(&sdlEvent))
			{
				ImGui_ImplSDL2_ProcessEvent(&sdlEvent);
			}

			ImGui_ImplOpenGL3_NewFrame();
			ImGui_ImplSDL2_NewFrame();
			ImGui::NewFrame();

			ImGui::ShowDemoWindow();

			ImGui::Render();
			ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
			ImGui::EndFrame();
			ImGui::UpdatePlatformWindows();
			ImGui::RenderPlatformWindowsDefault();

			SDL_GL_MakeCurrent(g_window, g_context);
			SDL_GL_SwapWindow(g_window);

			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		}

		return 0;
	}
}

from imgui.

ocornut avatar ocornut commented on May 30, 2024

Thank you. The GLAD stuff has no effects really, so i believe the only meaningful setup line here is using SDL_GL_CONTEXT_PROFILE_ES. Must investigate SDL code to see what it means and how we could somehow detect it on our end.

from imgui.

afraidofdark avatar afraidofdark commented on May 30, 2024

I tried to detect the opengl version, macros does not consider the case of loading opengl es 3.0 on destkop, so settings in the ImGui_ImplOpenGL3_Data are not generated correctly.

Here is the diff that still fixes the problem with properly assigning the opengl version.

IMHO adding one more macro which indicates DESKTOP_OPENGL_ES3 is much better, because the codebase already have suitable checks for GLES3.0 every where.

diff --git a/backends/imgui_impl_opengl3.cpp b/backends/imgui_impl_opengl3.cpp
index 0be98b63..46cf80d9 100644
--- a/backends/imgui_impl_opengl3.cpp
+++ b/backends/imgui_impl_opengl3.cpp
@@ -319,6 +319,14 @@ bool    ImGui_ImplOpenGL3_Init(const char* glsl_version)
 
 #if defined(IMGUI_IMPL_OPENGL_ES3)
     bd->GlProfileIsES3 = true;
+#else
+
+    const GLubyte* version = glGetString(GL_VERSION);
+    if (strstr((const char*) version, "OpenGL ES"))
+    {
+        bd->GlProfileIsES3 = true;
+    }
+
 #endif
 
     bd->UseBufferSubData = false;
@@ -424,7 +432,10 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid
         glDisable(GL_PRIMITIVE_RESTART);
 #endif
 #ifdef IMGUI_IMPL_OPENGL_HAS_POLYGON_MODE
-    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
+    if (!bd->GlProfileIsES3)
+    {
+        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
+    }
 #endif
 
     // Support for GL 4.5 rarely used glClipControl(GL_UPPER_LEFT)
@@ -513,7 +524,12 @@ void    ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
     GLuint last_vertex_array_object; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, (GLint*)&last_vertex_array_object);
 #endif
 #ifdef IMGUI_IMPL_OPENGL_HAS_POLYGON_MODE
-    GLint last_polygon_mode[2]; glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode);
+    GLint last_polygon_mode[2];
+    if (!bd->GlProfileIsES3)
+    {
+        glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode);
+    }
+    
 #endif
     GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport);
     GLint last_scissor_box[4]; glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box);
@@ -653,14 +669,17 @@ void    ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
 
 #ifdef IMGUI_IMPL_OPENGL_HAS_POLYGON_MODE
     // Desktop OpenGL 3.0 and OpenGL 3.1 had separate polygon draw modes for front-facing and back-facing faces of polygons
-    if (bd->GlVersion <= 310 || bd->GlProfileIsCompat)
+    if (!bd->GlProfileIsES3)
     {
-        glPolygonMode(GL_FRONT, (GLenum)last_polygon_mode[0]);
-        glPolygonMode(GL_BACK, (GLenum)last_polygon_mode[1]);
-    }
-    else
-    {
-        glPolygonMode(GL_FRONT_AND_BACK, (GLenum)last_polygon_mode[0]);
+        if (bd->GlVersion <= 310 || bd->GlProfileIsCompat)
+        {
+            glPolygonMode(GL_FRONT, (GLenum) last_polygon_mode[0]);
+            glPolygonMode(GL_BACK, (GLenum) last_polygon_mode[1]);
+        }
+        else
+        {
+            glPolygonMode(GL_FRONT_AND_BACK, (GLenum) last_polygon_mode[0]);
+        }
     }
 #endif // IMGUI_IMPL_OPENGL_HAS_POLYGON_MODE

from imgui.

ocornut avatar ocornut commented on May 30, 2024

Thank you, this is super useful as until now I had no idea how to run an ES context on desktop (I've requested that help a few times before).

Committed a backend-side fix as 9ec299e

I have reduced your repro to the following blocks changes from existing example:

Include:

#define GLAD_GLES2_IMPLEMENTATION
#include "glad/gles2.h"

Init:

// Decide GL+GLSL versions
const char* glsl_version = "#version 300 es";
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);

Init:

// Setup Glad
gladLoadGLES2((GLADloadfunc)SDL_GL_GetProcAddress);

// Setup debug message callback
if (glDebugMessageCallback != NULL)
{
    glEnable(GL_DEBUG_OUTPUT);
    glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
    glDebugMessageCallback([](GLenum, GLenum, GLuint, GLenum, GLsizei, const GLchar* msg, const void*) { printf("%s\n", msg); }, nullptr);
    glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_NOTIFICATION, 0, nullptr, GL_FALSE);
    glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_LOW, 0, nullptr, GL_FALSE);
    glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_MEDIUM, 0, nullptr, GL_TRUE);
    glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_HIGH, 0, nullptr, GL_TRUE);
}

IMHO adding one more macro which indicates DESKTOP_OPENGL_ES3 is much better, because the codebase already have suitable checks for GLES3.0 every where.

My problem is that a DESKTOP_OPENGL_ES3 like macro would imply we know what headers to pull, I'm not sure there is a standard for Desktop ES? We currently locally use our own loader which happens to work with SDL setup.
In fact, being able to do it without a compile-time define is a benefit.

If there is a better way to detect an ES context I'd always be happy to hear it.

Thanks for your help!

from imgui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.