The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Using GLSL for fullscreen FX
eclectocrat


Joined: 26 Mar 2011
Posts: 72
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
_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
eclectocrat


Joined: 26 Mar 2011
Posts: 72
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?
brada


Joined: 16 Aug 2012
Posts: 38
did you look at the sample code here?
eclectocrat


Joined: 26 Mar 2011
Posts: 72
brada wrote:
did you look at the sample code here?


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.
Using GLSL for fullscreen FX
Sam Lantinga


Joined: 10 Sep 2009
Posts: 1765
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:
Quote:



brada wrote:

did you look at the sample code here?




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

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Using GLSL for fullscreen FX
eclectocrat


Joined: 26 Mar 2011
Posts: 72
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:
Quote:
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:


Quote:



brada wrote:

did you look at the sample code here?




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

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org





_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Using GLSL for fullscreen FX
eclectocrat


Joined: 26 Mar 2011
Posts: 72
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 wrote:
Quote:
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:
Quote:
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:


Quote:



brada wrote:

did you look at the sample code here?




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

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org





_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org






Using GLSL for fullscreen FX
Sam Lantinga


Joined: 10 Sep 2009
Posts: 1765
You're welcome!  I'm glad you got it working! Smile


On Thu, Jun 27, 2013 at 12:30 PM, Jeremy Jurksztowicz wrote:
Quote:
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 wrote:
Quote:
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:
Quote:
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:


Quote:



brada wrote:

did you look at the sample code here?




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

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org





_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org












_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org