Question about mixing SDL_Renderer and OpenGL Context [Solved]

For starters, hello, I’m new here. I’ve been experimenting with SDL2 for a few months and I absolutely love it and I am incredibly grateful to everyone who helps make it happen.

This is a question about retroactively adding an OpenGL context (really about adding Dear ImGui) to a project that is already deeply committed to using SDL_Renderer.

Firstly, I’m aware that SDL 2.0.18 (and therefore https://github.com/ocornut/imgui/pull/3926) is on the horizon, which would completely solve my issue but I wanted to ask my question anyway, at least to improve my understanding of the problem (I’m curious of the technical details) or, if possible, to find a reasonable workaround to hold me over until 2.0.18.

I have code like the following:

bool Graphics::beginFrame() const {
	if (imguiInitialized) {
		ImGui_ImplSDL2_NewFrame();
		ImGui_ImplOpenGL3_NewFrame();
		ImGui::NewFrame();
	}

	setDrawColor(Color::BLACK);
	if (SDL_RenderClear(renderer.get()) < 0) {
		SDL_Log("Graphics::beginFrame - SDL_RenderClear failed: %s\n", SDL_GetError());
		return false;
	}
	return true;
}

void Graphics::endFrame() const {
	if (imguiInitialized) {
		ImGui::Render();
		ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
		// SDL_GL_SwapWindow(window.get());
	}

	SDL_RenderPresent(renderer.get());
}

If I uncomment SDL_GL_SwapWindow then I get the flickering described here: Problem with SDL_RenderPresent() + SDL_GL_SwapWindow()
which makes perfect sense.

The strange thing is, if I leave SDL_GL_SwapWindow commented out, then on Linux I get the perfect outcome. All ImGui windows are drawn on top of everything that was drawn with SDL_Renderer.
But on Windows and Mac, if I leave SDL_GL_SwapWindow commented out, then only things drawn with SDL_Renderer will be visible and all ImGui windows are invisible (but of course they are still fully functional with the mouse and keyboard).

Why might the OpenGL context and SDL Renderer play so nicely on Linux but not on Windows or Mac?
And is there possibly a hacky workaround I can use to get it to work just the same on Windows and Mac for the time being?

Thanks for reading.

1 Like

hello,

I’m not 100% sure, but maybe this is the fact that on Mac and Window, the SDL Renderer isn’t using an OpenGL backend, but the Metal back-end on Mac and D3D11 back-end on Window.

You can check on Mac, by forcing the OpenGl back-end for SDL_Renderer using
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
before creating the window.

Btw: about the flickering Problem with SDL_RenderPresent() + SDL_GL_SwapWindow():
SDL_RenderPresent ends up being a swap window.
So if you call the two functions. you present two frames successively.
(1 correct + 1 blank, or 1 imgui + 1 sdl, depending on where you place the calls).

1 Like

Wow, that absolutely fixed the issue (for Windows)! Thanks for the tip.
I naively assumed that by including SDL_WINDOW_OPENGL in my SDL_WindowFlags when calling SDL_CreateWindow that it would also force the SDL_Renderer to use OpenGL as well.

However, by adding SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl") on Mac, that is causing ImGui initialization to fail with:

ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to compile vertex shader! With GLSL: #version 150

ERROR: 0:1: '' :  version '150' is not supported

ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to compile fragment shader! With GLSL: #version 150

ERROR: 0:1: '' :  version '150' is not supported

ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to link shader program! With GLSL #version 150

ERROR: One or more attached shaders not successfully compiled

Obviously, GLSL version 150 works just fine on Mac when I do not include SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl").
I know we’re getting a bit more off topic from the original issue, but any ideas why this might be happening or how to fix it?

still not sure, but maybe try the imgui opengl2 example

1 Like

SDL_Renderer creates an OpenGL 2 (on Mac at least) so you wont be able to use OpenGL 3 with it unless there is a way I dont know of to make SDL_Renderer create a 3 context or use an already created context.

1 Like

Yep, switching to imgui_impl_opengl2 fixed my issue on Mac. Many thanks to both of you.