Can't bind SDL_Texture to opengl to use in fragment shader

Hi everyone,

My workflow is basically the following:

I tried to use the following code to bind my SDL_Texture in opengl:

	if (SDL_GL_BindTexture(myTexture, NULL, NULL))
		throw SDL_GetError();
	glUniform1i(glGetUniformLocation(programID, "texture1"), 0); 

However, SDL_GL_BindTexture always returns -1, and the SDL error is “Operation not supported”.

I can’t use SDL_LockTexture to create a texture using raw pixel data with glTexImage2D, and then bind this new texture, because my SDL_Texture is created with the SDL_TEXTUREACCESS_TARGET flag, and thus I can’t set the SDL_TEXTUREACCESS_STREAMING flag (else SDL_SetRenderTarget complains that the texture hasn’t been initialized as a target).

I tried several hacks to retrieve the GLuint of the SDL_Texture, however it appears to be always 0.

Thus, how could I bind my SDL_Texture to send it to my fragment shader?

The only solution I found was to recode my entire rendering process to blit everything to a huge SDL_Surface, then convert it to an opengl texture.

Can you show the code you use to create the renderer and the texture? SDL_GL_BindTexture works for me (returns zero) for the current render target, tested in Windows/OpenGL:

  myTexture = SDL_GetRenderTarget(renderer);
  if (SDL_GL_BindTexture(myTexture, NULL, NULL))
		throw SDL_GetError();
_renderer = std::unique_ptr<SDL_Renderer, sdl_deleter>(
                SDL_CreateRenderer(_window.get(), -1, SDL_RENDERER_ACCELERATED),

With sdl_deleter being the following struct:

struct sdl_deleter
        void operator()(SDL_Window *p) const { SDL_DestroyWindow(p); }
        void operator()(SDL_Renderer *p) const { SDL_DestroyRenderer(p); }
        void operator()(SDL_GLContext *p) const { SDL_GL_DeleteContext(p); }
        void operator()(SDL_Surface *p) const { SDL_FreeSurface(p); }
        void operator()(SDL_Texture *p) const { SDL_DestroyTexture(p); }
        void operator()(TTF_Font *p) const { TTF_CloseFont(p); }

The window is created the following way:

_window = std::unique_ptr<SDL_Window, sdl_deleter>(
                                                RESX, RESY, SDL_WINDOW_OPENGL),

The only significant difference from my code is that I include SDL_RENDERER_PRESENTVSYNC in the renderer flags, but it seems unlikely that it is the cause of the different behavior. You didn’t list the code which creates your target texture; I wonder if the issue might be the texture pixel format, I use SDL_PIXELFORMAT_ARGB8888 for OpenGL.

Experienced same problem. If I call SDL_SetHint(SDL_HINT_RENDER_DRIVER,"opengles2"); before SDL_Init then “half” works (RGB channels swapped).