SDL icon indicating copy to clipboard operation
SDL copied to clipboard

SDL3, macOS: SDL_HINT_RENDER_DRIVER fallback does not work

Open andreasgrabher opened this issue 1 year ago • 9 comments

I tried setting SDL_HINT_RENDER_DRIVER to "direct3d" on macOS just to see if it will fall back to the default renderer. Unfortunately this seems to not work as described in the documentation:

If the application doesn't pick a specific renderer to use, this variable specifies the name of the preferred renderer. If the preferred renderer can't be initialized, the normal default renderer is used.

I get this error when calling SDL_CreateRenderer():

Couldn't find matching render driver!

andreasgrabher avatar Oct 05 '24 15:10 andreasgrabher

Oh, the documentation is wrong here. I'll fix that. It's intentional that it returns NULL if the preferred renderer isn't available, so that the application can try several, in order of preference.

slouken avatar Oct 05 '24 16:10 slouken

I am not sure this makes sense. If I want to explicitly create a metal renderer I just call

SDL_CreateRenderer(sdlRenderer, "metal");

Why should someone call

SDL_SetHint(SDL_HINT_RENDER_DRIVER, "metal");
SDL_CreateRenderer(sdlRenderer, NULL);

instead? I think if the hint works this way it is pretty much useless.

andreasgrabher avatar Oct 06 '24 14:10 andreasgrabher

Because you can do this:

renderer = SDL_CreateRenderer("gpu");
if (!renderer) {
    renderer = SDL_CreateRenderer("opengles2");
}
if (!renderer) {
    renderer = "SDL_CreateRenderer("direct3d");
}

slouken avatar Oct 06 '24 15:10 slouken

Exactly, so why would I need that hint?

andreasgrabher avatar Oct 06 '24 15:10 andreasgrabher

Oh, you're right, I was conflating the two. Mmmm, I will reopen this for further thought.

slouken avatar Oct 06 '24 15:10 slouken

Isn't a hint useful for a user to try another renderer backend (through an environment variable), and the SDL_CreateRenderer a way to force a backend by the developer?

madebr avatar Oct 06 '24 15:10 madebr

I think this hint would make sense if someone has one single preferred renderer but does not want things to fail if that one is not available.

I think if someone wants to make his own order of preference with multiple renderers this would be appropriate:

static const char* renderers[] = {
	"direct3d",
	"direct3d11",
	"direct3d12",
	"metal",
	"opengl",
	"opengles2",
	"opengles",
	"vulkan",
	"software"
};

SDL_Renderer* CreateRenderer(SDL_Window* sdlWindow)
{
	int i;
	SDL_Renderer* sdlRenderer;
	for (i = 0; i < SDL_arraysize(renderers); i++) {
		fprintf(stderr, "Trying to create %s renderer\n", renderers[i]);
		sdlRenderer = SDL_CreateRenderer(sdlWindow, renderers[i]);
		if (sdlRenderer) {
			fprintf(stderr, "Successfully created %s renderer\n", SDL_GetRendererName(sdlRenderer));
			return sdlRenderer;
		}
	}
	return NULL;
}

andreasgrabher avatar Oct 06 '24 15:10 andreasgrabher

Yep, that's what we do for video drivers, etc. I'll take a look.

slouken avatar Oct 06 '24 15:10 slouken

(Just making a note that what we need to do here is make sure SDL_HINT_RENDER_DRIVER takes a comma-separated list, to match what audio and video subsystems already do.)

icculus avatar Dec 04 '24 18:12 icculus

I know that 3.2.0 should be released really soon, but I think this shoud be fixed before release. People might use this hint to set a specific renderer instead of using SDL_CreateRenderer() for that. Changing the hint to be treated as prefered renderer could then break someone's code.

andreasgrabher avatar Jan 21 '25 06:01 andreasgrabher

I snuck this in for 3.2.0.

The documentation in the original comment was updated at some point to reflect reality, but this now accepts a comma-separated list (both in SDL_CreateRenderer's name parameter and SDL_HINT_RENDER_DRIVER), matching what the video/audio/whatever subsystems already do.

icculus avatar Jan 21 '25 16:01 icculus

Fantastic! Thank you very much!

andreasgrabher avatar Jan 21 '25 17:01 andreasgrabher