How do multiple pass shaders work in HLSL?
I'm new to shaders and HLSL, having done enough with BasicEffect class. I understand how the pipeline works, especially for shaders with only 1 pass. However, incase of 2-pass or N-pass shaders, i don't quite understand how the results of the 2(or N) shaders outputs get combined.
Can you please explain the how the combination takes? And if 开发者_开发知识库possible, an example where you'd prefer using a multi-pass shader rather than multiple single-pass shaders?
Multi-pass shaders simply ADD the results to the previous pass(es). They can be used to support multiple lights, particularly when the GPU's shader model does not have enough instructions to support the required number of lights in a single pass.
Unless you really need to, I wouldn't recommend using more than one pass as it does complicate things such as alpha blending and fog. You'll need to set up your blend states differently in the first pass to subsequent passes.
[EDIT] AS per Melchior Blausand's comment, it is more correct to say that the output each pass is combined with the current value according to the current blend operation and blend modes, where the current value is the result of combining all the previous passes. It is common for multi-pass shaders to use the ADD blend mode to combine multiple lights. Also note that the alpha channel can be combined with different blend modes to the colour channels.
Well it is all up to you because you must call BeginPass method before rendering the mesh itself and loop through all passes. This gives you the ability to change blend mode and even the mesh is not required to be the same.
So in general, the result will be same as if you render (different) meshes with one-pass shader.
精彩评论