Sign in to follow this  
ciel123

Multiple vertex/pixel shaders

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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")
Edited by Matias Goldberg

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this