Multiple vertex/pixel shaders

Started by
3 comments, last by Matias Goldberg 7 years, 4 months ago

This is a pretty basic question, but the HLSL documentation and guides out there are pretty scarce and disappointing...

I've learned that vertex and pixel shaders are executed by the graphics pipeline during the vertex and pixel shader stages, respectively. But I've seen projects that use multiple vertex/pixel shader functions, like:

// Vertex shader

float4 DoSomeTransformationVS(float4 v : POSITION) : SV_POSITION

{

...

}

// Also vertex shader

float4 DoAnotherTransformationVS(float4 v : POSITION) : SV_POSITION

{

...

}

What happens then? Are both functions executed during VS stage? In fact, how does the compiler know which functions are vertex shaders and which are pixel shaders?

Advertisement

If I got it correctly, these are different vertex shader program. You can have multiple vertex shader available in your program, but you can only bind one vs in each pass. For the above code, I guess there are multiple passes each frame (or the project allow you to switch vs during runtime), and these two vs is for different passes

When you compile a shader, you specify the name of a function in the source code that acts as the "main" function (aka the "entry point").

Your example is just a convenience for the original programmer, where they've written two vertex programs in the same file. That's a common thing to do when there's some code sharing.

Thanks guys and sorry for the silly question.

As Hodgman says, you specify the entry point function while compiling a shader, and while compiling you specify whether this is a vertex or a pixel shader function.

Also please note that in other APIs it's different:

  1. In HLSL you specify whether it's a vertex/pixel shader while compiling (i.e. from C++ or command line if using fxc)
  2. In GLSL you can't specify the entry point's name (it must be always called main) meaning you can't mix pixel and vertex shaders in the same file. Whether you're compiling a vertex or pixel shader is specified while compiling (i.e. from C++)
  3. In Metal, you use the keywords "vertex" and "fragment" to specify whether the function is a vertex or pixel shader entry point. Also, you cannot name a function as "main" (which directly conflicts with GLSL's requirement of having the entry point named "main")

This topic is closed to new replies.

Advertisement