Using GLSL for fullscreen FX

Greetings everyone,

I have read the conversation at
http://forums.libsdl.org/viewtopic.php?t=6946&sid=e1086f0489c98133fc10fe202ca08214
which suggests that mixing OpenGL shaders and SDL is a bad idea, and
yet I feel like it would be such a waste to redo the rendering code
from scratch, so I attempted an experiment. Now, I know very little
(nothing) about shaders, but managed to load and compile one
successfully. Now I want a uniform sampler in the fragment shader and
am hitting a roadblock.

My goal is simply to read the pixels from an offscreen render target,
modify them, and send to the main renderer. Using SDL I created an
offscreen surface which works perfectly. If I just render to it and
then switch the render target back to the main one, it copies over
fine. However, when trying to access a sampler from the fragment
shader, I get nothing but black.

I found the following on the opengl wiki, and wonder if it applies to
sampling and copying FROM the same texture as well: “Normally, you
should not sample a texture and render to that same texture at the
same time. This would give you undefined behavior. It might work on
some GPUs and with some driver version but not others.”

Clearly I’m in over my head, but before I continue, I would like to
confirm whether what I want to do is possible without hacking base
SDL. I’m wondering if anyone could spot a newbie mistake in the
following code. The commented out openGL code was an attempt to skip
the SDL render API, but it left me with an oddly coloured image.

RENDER LOOP BEGIN:

SDL_SetRenderTarget(RENDERER, OFFSCREEN);

SDL_SetRenderDrawColor(RENDERER, 128, 128, 128, 255);

SDL_RenderClear(RENDERER);

// Render World Here

WIN->render(Point(0, 0));

SDL_SetRenderTarget(RENDERER, 0);

glUseProgram(SHADER_PROGRAMS[0]); // If I comment this out, all

copying works fine.

glActiveTexture(GL_TEXTURE0);

SDL_GL_BindTexture(OFFSCREEN, &w, &h);

/* float w, h;

glEnable(GL_TEXTURE_2D);

SDL_GL_BindTexture(OFFSCREEN, &w, &h);

glBegin(GL_QUADS);

glTexCoord2f(0, 0);     glVertex2f(0, 0);

glTexCoord2f(0, 480);   glVertex2f(0, 480);

glTexCoord2f(640, 480); glVertex2f(640, 480);

glTexCoord2f(640, 0);   glVertex2f(640, 0);

glEnd();

glDisable(GL_TEXTURE_2D);

SDL_GL_UnbindTexture(OFFSCREEN); */

SDL_RenderCopy(RENDERER, OFFSCREEN, NULL, NULL);

SDL_RenderPresent(RENDERER);

SDL_GL_UnbindTexture(OFFSCREEN);

Many thanks,
Jeremy Jurksztowicz

Hi, Just as a follow-up I’ve tried to learn what I can from the testshader.c source, but it seems like it’s been disabled for a while now, and initial attempts to hack it to work are failing. Is the code therein still a valid approach to using shaders, and can it be modified to use SDL_Textures instead of pure GL textures?

did you look at the sample code here (http://wiki.libsdl.org/moin.fcg/SDL_GL_BindTexture)?

brada wrote:

did you look at the sample code here (http://wiki.libsdl.org/moin.fcg/SDL_GL_BindTexture)?

Hi, I did check that out, thanks. I can get openGL rendering and even shaders to work with the SDL_Renderer interface in the general, but am still having trouble binding textures to samplers in GLSL shaders. I naively updated the testshader.c test in SDL to use SDL2 api (http://forums.libsdl.org/viewtopic.php?t=9199), and it renders, but the second shader included with the test (the one that samples a texture) renders flat black. I assume that at one point the shader worked as expected, and so am trying to figure out how to get it working again, with SDL2.

In the general, does anyone on the list have a GLSL shader installed in their pipeline? Does anyone have sample code to demonstrate a way to use shaders with SDL_Textures? I imagine that this would be a commonly desired thing, and a canonical example would help a lot of developers out.

I don’t know if it helps, but I just updated testshader.c for SDL 2.0, and
it works great here.On Thu, Jun 27, 2013 at 8:08 AM, eclectocrat wrote:

**

brada wrote:

did you look at the sample code herehttp://wiki.libsdl.org/moin.fcg/SDL_GL_BindTexture
?

Hi, I did check that out, thanks. I can get openGL rendering and even
shaders to work with the SDL_Renderer interface in the general, but am
still having trouble binding textures to samplers in GLSL shaders. I
naively updated the testshader.c test in SDL to use SDL2 api (
http://forums.libsdl.org/viewtopic.php?t=9199), and it renders, but the
second shader included with the test (the one that samples a texture)
renders flat black. I assume that at one point the shader worked as
expected, and so am trying to figure out how to get it working again, with
SDL2.

In the general, does anyone on the list have a GLSL shader installed in
their pipeline? Does anyone have sample code to demonstrate a way to use
shaders with SDL_Textures? I imagine that this would be a commonly desired
thing, and a canonical example would help a lot of developers out.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thanks, Sam, I’ll take a look and see if it works on my system too.On Thu, Jun 27, 2013 at 3:07 PM, Sam Lantinga wrote:

I don’t know if it helps, but I just updated testshader.c for SDL 2.0, and
it works great here.

On Thu, Jun 27, 2013 at 8:08 AM, eclectocrat <@Jeremy_Jurksztowicz>wrote:

**

brada wrote:

did you look at the sample code herehttp://wiki.libsdl.org/moin.fcg/SDL_GL_BindTexture
?

Hi, I did check that out, thanks. I can get openGL rendering and even
shaders to work with the SDL_Renderer interface in the general, but am
still having trouble binding textures to samplers in GLSL shaders. I
naively updated the testshader.c test in SDL to use SDL2 api (
http://forums.libsdl.org/viewtopic.php?t=9199), and it renders, but the
second shader included with the test (the one that samples a texture)
renders flat black. I assume that at one point the shader worked as
expected, and so am trying to figure out how to get it working again, with
SDL2.

In the general, does anyone on the list have a GLSL shader installed in
their pipeline? Does anyone have sample code to demonstrate a way to use
shaders with SDL_Textures? I imagine that this would be a commonly desired
thing, and a canonical example would help a lot of developers out.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Hey, The updated testshader.c works as expected! I had to change:

#ifdef HAVE_OPENGL

to

#if 1

In order for it to compile (Xcode templates running under Xcode 4.), and I
had to change ‘Icon.bmp’ to a full pathname, but it works great on my OSX
10.8 machine. Thanks a ton, I now have a working example of GLSL shaders in
an SDL program! I’ll be sure to add an entry to the WIKI once I figure out
all the nitty gritty.

Thanks again,

Jeremy J.On Thu, Jun 27, 2013 at 3:09 PM, Jeremy Jurksztowicz <@Jeremy_Jurksztowicz wrote:

Thanks, Sam, I’ll take a look and see if it works on my system too.

On Thu, Jun 27, 2013 at 3:07 PM, Sam Lantinga wrote:

I don’t know if it helps, but I just updated testshader.c for SDL 2.0,
and it works great here.

On Thu, Jun 27, 2013 at 8:08 AM, eclectocrat <@Jeremy_Jurksztowicz>wrote:

**

brada wrote:

did you look at the sample code herehttp://wiki.libsdl.org/moin.fcg/SDL_GL_BindTexture
?

Hi, I did check that out, thanks. I can get openGL rendering and even
shaders to work with the SDL_Renderer interface in the general, but am
still having trouble binding textures to samplers in GLSL shaders. I
naively updated the testshader.c test in SDL to use SDL2 api (
http://forums.libsdl.org/viewtopic.php?t=9199), and it renders, but the
second shader included with the test (the one that samples a texture)
renders flat black. I assume that at one point the shader worked as
expected, and so am trying to figure out how to get it working again, with
SDL2.

In the general, does anyone on the list have a GLSL shader installed in
their pipeline? Does anyone have sample code to demonstrate a way to use
shaders with SDL_Textures? I imagine that this would be a commonly desired
thing, and a canonical example would help a lot of developers out.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

You’re welcome! I’m glad you got it working! :)On Thu, Jun 27, 2013 at 12:30 PM, Jeremy Jurksztowicz < jurksztowicz at gmail.com> wrote:

Hey, The updated testshader.c works as expected! I had to change:

#ifdef HAVE_OPENGL

to

#if 1

In order for it to compile (Xcode templates running under Xcode 4.), and I
had to change ‘Icon.bmp’ to a full pathname, but it works great on my OSX
10.8 machine. Thanks a ton, I now have a working example of GLSL shaders in
an SDL program! I’ll be sure to add an entry to the WIKI once I figure out
all the nitty gritty.

Thanks again,

Jeremy J.

On Thu, Jun 27, 2013 at 3:09 PM, Jeremy Jurksztowicz < jurksztowicz at gmail.com> wrote:

Thanks, Sam, I’ll take a look and see if it works on my system too.

On Thu, Jun 27, 2013 at 3:07 PM, Sam Lantinga <@slouken> wrote:

I don’t know if it helps, but I just updated testshader.c for SDL 2.0,
and it works great here.

On Thu, Jun 27, 2013 at 8:08 AM, eclectocrat wrote:

**

brada wrote:

did you look at the sample code herehttp://wiki.libsdl.org/moin.fcg/SDL_GL_BindTexture
?

Hi, I did check that out, thanks. I can get openGL rendering and even
shaders to work with the SDL_Renderer interface in the general, but am
still having trouble binding textures to samplers in GLSL shaders. I
naively updated the testshader.c test in SDL to use SDL2 api (
http://forums.libsdl.org/viewtopic.php?t=9199), and it renders, but
the second shader included with the test (the one that samples a texture)
renders flat black. I assume that at one point the shader worked as
expected, and so am trying to figure out how to get it working again, with
SDL2.

In the general, does anyone on the list have a GLSL shader installed in
their pipeline? Does anyone have sample code to demonstrate a way to use
shaders with SDL_Textures? I imagine that this would be a commonly desired
thing, and a canonical example would help a lot of developers out.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org