Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Multipass shaders ?  (Read 5083 times)

0 Members and 1 Guest are viewing this topic.

dydya-stepa

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Multipass shaders ?
« on: November 26, 2011, 10:15:57 am »
does anybody know how to do multipass shaders in opengl ?

i need to make a gaussian blur which is a vertical and horizontal passes according to description. I don't know much about shaders. Not sure if i need 2 passes in this case.

So help and advices are appreciated.

sample code or a nice reference would be perfect.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Multipass shaders ?
« Reply #1 on: November 26, 2011, 10:42:21 am »
"Multi-pass rendering" basically consists of rendering the same thing several times in a loop, using the output (texture) of the previous pass as the input of the current one.

So for your gaussian blur shader, you would do the following:
Code: [Select]
// vertical pass
bind(texture);
bind(vertical_shader);
draw(to output1);

// horizontal pass
bind(output1);
bind(horizontal_shader);
draw(to screen);


Note that you can also fake a gaussian blur with a single pass (see the "Shader" SFML example). The result is not as good, but is saves GPU instructions.

For more details, it seems that Google has a huge amount of links for "gaussian blur shader" ;)
Laurent Gomila - SFML developer

dydya-stepa

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Multipass shaders ?
« Reply #2 on: November 26, 2011, 11:02:24 am »
this is a pseudocode i suppose?

i wonder what is most efficient to use in sfml? should i use RenderImage or anything else?

because in total it requires even more passes if this is a post effect i need to first render my full scene into an output1. then take it and apply 1st shader. and then apply 2nd shader drawing into window.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Multipass shaders ?
« Reply #3 on: November 26, 2011, 11:14:16 am »
Quote
this is a pseudocode i suppose?

Of course :D

Quote
i wonder what is most efficient to use in sfml? should i use RenderImage or anything else?

The most efficient is to render directly to the texture that will be used for the next pass, so it is sf::RenderTexture (previously named sf::RenderImage).

Quote
because in total it requires even more passes if this is a post effect i need to first render my full scene into an output1. then take it and apply 1st shader. and then apply 2nd shader drawing into window.

That's true, a post-effect is always a multi-pass effect because it requires to render the scene first, in order to get the first input texture to work on.
(however this first pass doesn't render the same stuff as the other passes, so it's a little bit different ;))
Laurent Gomila - SFML developer

dydya-stepa

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Multipass shaders ?
« Reply #4 on: November 26, 2011, 01:10:08 pm »
so i need 2 render textures ?

1) render scene into texture1
2) render texture1 into texture2 with 1st shader
3) render texture2 into screen

?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Multipass shaders ?
« Reply #5 on: November 26, 2011, 01:40:34 pm »
Yes.
Laurent Gomila - SFML developer

dydya-stepa

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Multipass shaders ?
« Reply #6 on: December 21, 2011, 05:05:38 pm »
is it possible to avoid having too many texture copying ? Is it possible to just have 1 RenderTexture and apply 10 posteffects on it - without copying it into another RenderTexture?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Multipass shaders ?
« Reply #7 on: December 21, 2011, 05:45:14 pm »
What do you mean? There's no copy there, you just use a different render target for every pass.
Laurent Gomila - SFML developer

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Multipass shaders ?
« Reply #8 on: December 21, 2011, 06:27:54 pm »
You need at a minimum of 2 textures at all times. A source and a destination. If you try to force through somehow(I don't think OpenGL even permits you to bind the texture rendering to a variable) you will get invalid result as you will be writing over data that you are sampling from. Gaussian blur will be a perfect example of this. You have to sample around the current pixel, if you have the source and destination being the same texture, that means you will be sampling from already blurred pixels. See the problem?

Baseline, what you want to do is both not allowed by OpenGL or DirectX and if it were even allowed you would get invalid result.

So that's why you have a source texture that you sample pixels from and then render to a new destination texture. Then you can alternate between these textures so for the second pass, the destination becomes the source and the previous source becomes the new destination. If you are so inclined on pre-mature optimization and want to minimize the amount of memory used(no matter what, you won't get it to get faster here, only thing you can do is optimize the memory usage) you will still have to have 2 texture at least to render to.

Like Donald Knuth said: "premature optimization is the root of all evil"
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

 

anything