[SDL2.0.10] Textures disappearing & renderer causing error when resizing window with a Render Target texture

Hello. As the title says, I’m trying to render to a target texture, and then render that texture to the window. Everything works fine, except if I resize the window.
The second after I resize the window, all of the textures disappear (including the renderer?), and the screen is just black.
Additionally, SDL sets this error message: CreateTexture(D3DPOOL_DEFAULT): INVALIDCALL

The weird thing is, it works perfectly if the SDL renderer is initiated with SDL_RENDERER_SOFTWARE.
I don’t know much about openGL, so I’m guessing it’s my fault and I’m doing something wrong. Any help or explanations would be appreciated.

I’ve tested this on 2 computers, both Windows 10.

Here’s a video of it working as expected (when set to SDL_RENDERER_SOFTWARE): https://giant.gfycat.com/NeglectedIckyGharial.webm

And here’s a video of it breaking (when set to SDL_RENDERER_ACCELERATED): https://giant.gfycat.com/PoliteUnfinishedIndianringneckparakeet.webm

Here’s the image I’m using:
hamster|nullxnull

And here’s the source code:

Source Code
// Mostly from https://gist.github.com/Twinklebear/8265888
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <SDL.h>

#define WIN_WIDTH 640
#define WIN_HEIGHT 480

int main(int argc, char* argv[]) {
	SDL_Init(SDL_INIT_EVERYTHING);

	SDL_Window* win = SDL_CreateWindow("Rendering to a texture!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIN_WIDTH, WIN_HEIGHT, SDL_WINDOW_RESIZABLE);
	// Error only happens when flag is SDL_RENDERER_ACCELERATED, works fine with SDL_RENDERER_SOFTWARE
	SDL_Renderer* renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_TARGETTEXTURE | SDL_RENDERER_ACCELERATED);

	SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);

	//Put your own bmp image here
	SDL_Surface* bmpSurf = SDL_LoadBMP("hamster.bmp");
	SDL_Texture* bmpTex = SDL_CreateTextureFromSurface(renderer, bmpSurf);
	SDL_FreeSurface(bmpSurf);

	//Make a target texture to render too
	SDL_Texture* texTarget = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, WIN_WIDTH, WIN_HEIGHT);

	//Now render to the texture
	SDL_SetRenderTarget(renderer, texTarget);
	SDL_RenderClear(renderer);
	SDL_RenderCopy(renderer, bmpTex, NULL, NULL);
	//Detach the texture
	SDL_SetRenderTarget(renderer, NULL);

	SDL_DestroyTexture(bmpTex);

	if (SDL_RenderTargetSupported(renderer) == SDL_FALSE) {
		return EXIT_FAILURE;
	}

	bool quit = false;
	SDL_Event e;
	while (!quit) {
		while (SDL_PollEvent(&e)) {
			if (e.type == SDL_QUIT) {
				quit = true;
			}
		}

		SDL_RenderClear(renderer);

		//Now render the texture target to our screen, but upside down
		SDL_RenderCopyEx(renderer, texTarget, NULL, NULL, 0, NULL, SDL_FLIP_VERTICAL);


		/*
		*
		*
		* THIS IS WHERE SOMETHING GOES WRONG
		*
		*
		*/
		SDL_RenderPresent(renderer);

		if (strlen(SDL_GetError()) != 0) {
			printf("%s\n", SDL_GetError());
			SDL_SetError('\0');
		}
		/*
		*
		*
		*
		*
		*/


		SDL_Delay(20);
	}

	SDL_DestroyTexture(texTarget);
	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(win);
	SDL_Quit();
	return EXIT_SUCCESS;
}

Thanks!

From the error message I assume it’s not using OpenGL but Direct3D, which is the default on Windows. It might be interesting to see if OpenGL behaves differently, which you can do by setting this hint before creating the renderer:

SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");

Aha, I could’ve guessed that one…

Setting the SDL hint did the trick! No errors, textures all redraw properly, works perfectly, thanks!

One more thing, for anyone having this problem - just setting the hint seemed to make SDL remake the window after the renderer was created (meaning the window would pop up for a quick second, and then the real one would appear). This was easily fixed by passing the SDL_WINDOW_OPENGL flag when creating the window.

So, is this an SDL bug? Direct3D bug?

Thanks again.