Jump to content
  • Advertisement
Sign in to follow this  
ryt

Shader passes

This topic is 2153 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What are shader passes and how to use them ? I know that some effects cant be done using a single pass but they are made of two passes or more.

 

Lets say that we want to create a shader with two passes using vertex and pixel shader. For the first pass we get data from IA (Input Assembler) and from there is passed to VS (Vertex Shader). In VS the vertices are transformed and prepared for screen space. The next stage prior to PS (Pixel Shader) is RS (Rasterizer Stage). There the data is rasterized and interpolated. Once that is completed the data is sent to PS where it is textured and various colors are applied. After a few modifications like alpha blending and depth buffer stage the data is ready to be presented to the screen.

But what now happens with a second pass ? Is the data that we get for second pass in screen space or do we get it again from IA ?

Share this post


Link to post
Share on other sites
Advertisement

Is the data that we get for second pass in screen space

Shaders don’t have passes. They execute once and are done until invoked again.

Objects have passes. If you want to do multi-pass rendering, you need to draw the object once for each pass, using a different shader for each pass. That means resubmitting the geometry a second time and going through the whole pipeline again, just with a different shader and frame-buffer blending options.


L. Spiro

Share this post


Link to post
Share on other sites

 

Is the data that we get for second pass in screen space

Shaders don’t have passes. They execute once and are done until invoked again.

Objects have passes. If you want to do multi-pass rendering, you need to draw the object once for each pass, using a different shader for each pass. That means resubmitting the geometry a second time and going through the whole pipeline again, just with a different shader and frame-buffer blending options.


L. Spiro

 

 

But why would we then go trough the first pass anyway since when we enter the second time the data would be overwritten ? Lets say we draw object in first pass in yellow and in second pass we apply the blue. The result on the screen would be a blue object ?

 

Also if we go trough the VS again in the second pass, isn't it the same computation since we already transformed the object in first pass ?

Edited by ryt

Share this post


Link to post
Share on other sites

But why would we then go trough the first pass anyway since when we enter the second time the data would be overwritten ?

I said the frame-buffer has different blending options on subsequent passes.
If you set the blending mode to additive, then it would be yellow added to blue.
If you set the blending mode to modulate, then it would be yellow multiplied by blue.

Also if we go trough the VS again in the second pass, isn't it the same computation since we already transformed the object in first pass ?

Yes.
Which is why you squeeze as much into a single pass as you possibly can.
The main reason you need to apply multiple passes is when you need to deal with renders that require more texture units than are available. If your object needs 23 textures to be rendered properly and your hardware or API only supports 8 textures bound at a time then you are going to need 3 passes to render the object.

Previous renders are stored in the frame-buffer so as I said you have to take care as to how each pass blends its new results with the previous via blend states.


L. Spiro

Share this post


Link to post
Share on other sites

Let me give you a little example when multiple pass effects can help.

Imagine that you need to draw a planet :(random link http://img842.imageshack.us/img842/9114/20100827screen001.jpg )

we've got 2 components here:

1 - the planet itself with the texture that represnts the surface of the planet

2 - the atmosphere( the thin blue arc around the planet)

 

What we can do here is to draw the shape of our planet two times ( pass A and pass B)

A - we draw the sphere apply the lighting and the texture

B - we draw the sphere a little bit bigger (we could use addition scaling in our vertex shader). Based on the vertex positions of the planet mesh, and the sun position, we compute the color of the sphere(based on the athmosphere) and a trasperancy, because the atmosphere is actually transperant.

Edited by imoogiBG

Share this post


Link to post
Share on other sites

Ok I see, it is meant to be used with blend states.

Edited by ryt

Share this post


Link to post
Share on other sites

Ok I see, it is meant to be used with blend states.

 

In some cases there are performance reasons to do multiple passes, for example a z prepass which renders the scene geometry with a really simple pixel shader (or no pixel shader at all) for the sole purpose of filling out the depth buffer, then a second pass over the scene geometry is performed with depth compare set to EQUAL and a complicated pixel shader (like a shader that performs lots of expensive lighting calculations or something). This ensures each pixel is only drawn once with the expensive pixel shader. This isn't always a performance win, but it is sometimes (scenes with lots of overdraw and expensive shading, for example, can benefit from this).

Share this post


Link to post
Share on other sites

A classic example of 2 passes is interpolation, such as blurring using convolution kernel.

The highest speed blurring takes place when you do horizontal and vertical blurring separately, and the math works out, so that's what people do.

You can do this with any convolution kernel you want, such as uniform, raised, gaussian, sharpen etc., and the results will be the same as if you did it in one pass.

 

Note that there may be a point in time in the near future when the cost of doing a pass (the setup) is more costly than simply doing it all in one go.

Of course you also have to consider the size of the data you are doing the operation on, though it is usually fullscreen texture operations.

 

It's not common to do multiple passes anymore, because you should be writing to several textures anyways.

My personal favorite being (r, g, b, linear depth) + (nx, ny, nz, absolute_height)

So, I no longer need a depth texture. How convenient, because depth textures are typically very slow compared to a depth buffer. smile.png

Edited by Kaptein

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!