Multi-Pass Shader - how?

Started by
7 comments, last by CrazyCdn 16 years, 10 months ago
Hi! I never used shader before. But before I continue writing my game engine I'd like to get some informations about using shaders. Well single-pass shaders are easy to implent. But how does multi-pass shader work? As far as I know, I'll have to create a third buffer beyond back- and front-buffer for that pass? Is that correct. Could someone maybe even post a short pseudo-language code and give me some functions that could be useful? Thanks :)
Advertisement
What shader language? HLSL, GLSL, Cg? This makes a difference in some ways.

If you're using OpenGL and want to make another render target look into FBOs. Not sure what the D3D alternative is called.

But we need more info I think before we can give you real answers.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

sorry.

I think I'll use GLSL. It seems to be the best for me. :) Do you need some more informations?
If you need to render to a offscreen buffer, sure use a FBO.

If you will be using GLSL, you might want to put

gl_Position = ftransform();

in each VS to make sure output vertices are always identical, else you might get some visual artifacts. Other languages don't have that feature.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
multipass?
u need to define the question better

multipass is normally just redrawing the model (with blending enabled) + with a diferent material
shaders have very little to do with it
Quote:Original post by zedz
multipass?
u need to define the question better

multipass is normally just redrawing the model (with blending enabled) + with a diferent material
shaders have very little to do with it

You're a little off here - advanced shaders use multiple passes a lot. A prominent example are shaders that are too big (i.e instruction count exceeds hardware capabilities) and thus need to be split up into multiple passes.
There are other uses for multipass shaders, too. Certain post-processing shaders may require multiple passes as well as HDR and bloom effects.

Cheers,
Pat
Quote:Original post by Aldoric
Well single-pass shaders are easy to implent. But how does multi-pass shader work?
That's tricky. Historically, multipassing were being used to accumulate light contributions.
As such, the passes were combined simply by blending them one over the other with additive blend. thus obtaining (col * light0) + (col * light1) + ... = col * (light0 + light1 + ...).

Unluckly, for shaders it's no more so easy. A shader may output a procedural alpha mask which needs to be fetched back for the second pass. How exactly this is arranged to be "given" to the next pass is no more trivial. Simple blend tricks may cut it anyway but in general I would say you need more advanced machinery.
Quote:Original post by Aldoric
As far as I know, I'll have to create a third buffer beyond back- and front-buffer for that pass? Is that correct.
Yes and no. You don't strictly need this but having another render target for ping-pong rendering does have several advantages - mainly because the blend operations do become programmable - I am confident most (if not all) apps out there use ping-pong rendering to some degree.
Quote:Original post by Aldoric
Could someone maybe even post a short pseudo-language code and give me some functions that could be useful?
I don't know what you mean by "functions" but for language CgFX and MS'FX are good candidates. Collada also features an internal SLANG but I'm not confident with it.
I suggest CgFX because it does include a bit of integration with FX composer, which may come useful as a design help.

Previously "Krohm"

Quote:You're a little off here - advanced shaders use multiple passes a lot. A prominent example are shaders that are too big (i.e instruction count exceeds hardware capabilities) and thus need to be split up into multiple passes.
There are other uses for multipass shaders, too. Certain post-processing shaders may require multiple passes as well as HDR and bloom effects.
that still doesnt really have anything to do with shaders, i think u mean taking the resulting input from a calculation + inputing it into another calculation. that is nothing shader specific to it, also splitting shaders up(?) same deal, but this has nothing to do with shaders.
one of us is misunderstanding what the OP wants (it could be me)
like i said he need's to define the question better

Ok, I'll explain a bit how we handle multi-pass so you can have a basic idea.

Firstly we have a material class, this is what is applied to meshes. It can have multiple textures, normal map, diffuse map, etc. attached to it. It also attaches shaders to it (any number you want). A material is saved in a file for us with references to all the above info. It also saves how many passes a specific shader gets and the orders. So you can say material 1 uses shaders 3-5 (just random ID's I made up) but that shader 5 runs first for two loops then shader 3 once, shader 4 once then shader 3 one more time.

Then the renderer makes all the magic happen! Well this is how we handle shadesr and multiple passes, others likely handle it much differently. Hopefully this helps.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

This topic is closed to new replies.

Advertisement