How demanding is SDL_SetRenderTarget performance-wise?

I have created a function that renders a texture:

void renderTexture(WHStruct logicalSize, SDL_Texture* sTexture, SDL_Texture* dTexture, areaStruct sRect, areaStruct dRect, int transparencyPercentage) {
	SDL_SetRenderTarget(renderer, dTexture);

	SDL_RenderSetLogicalSize(renderer, logicalSize.w, logicalSize.h);

	SDL_Rect sourceRect = { sRect.x, sRect.y, sRect.w, sRect.h };
	SDL_Rect destinationRect = { dRect.x, dRect.y, dRect.w, dRect.h };

	SDL_SetTextureBlendMode(sTexture, SDL_BLENDMODE_BLEND);
	SDL_SetTextureAlphaMod(sTexture, (255 * transparencyPercentage) / 100);

	if (sRect.w > -1 && dRect.w > -1) {
		SDL_RenderCopy(renderer, sTexture, &sourceRect, &destinationRect);
	else if (sRect.w == -1 && dRect.w > -1) {
		SDL_RenderCopy(renderer, sTexture, NULL, &destinationRect);
	else if (sRect.w > -1 && dRect.w == -1) {
		SDL_RenderCopy(renderer, sTexture, &sourceRect, NULL);

This allows me to render a texture in one call rather than calling SDL_SetRenderTarget, SDL_RenderSetLogicalSize, SDL_SetTextureBlendMode, etc. each time. Which makes development faster.

However, I was concerned about SDL_SetRenderTarget (and the other SDL functions like SDL_RenderSetLogicalSize, SDL_SetTextureBlendMode, etc.) in terms of performance.

Is calling SDL_SetRenderTarget each time one texture is rendered slower than calling it once outside a loop and just calling SDL_RenderCopy inside a loop to render multiple textures?

You should be fine calling SDL_SetRenderTarget a lot[1] as long as you are calling it on the same texture target every time.

SetTextureBlendMode is also fine [2] as it only sets a parameter.

SDL_RenderSetLogicalSize does a bit more work. I am not sure if its enough to cause a performance hit, but if you can avoid it i certainly would recommend you to.

[1] SDL/SDL_render.c at 84c44e01d3bcf693e80c624bc82e03e49d40144c · libsdl-org/SDL · GitHub
[2] SDL/SDL_render.c at 84c44e01d3bcf693e80c624bc82e03e49d40144c · libsdl-org/SDL · GitHub

1 Like

I see. Well, I’ve been using my function and calling these each time on different textures and I see no impact on performance. But it might impact once I start processing a lot of textures as I develop the game more. So it might be wise for me to refactor now lol. Thank you!