Little Clarification on Shaders

Started by
2 comments, last by Namethatnobodyelsetook 13 years, 10 months ago
Cannot find an explicit answer to this online nor in my book.

In order to make a shader that fully replicates the fixed-function pipeline I would need a declaration such as this:
VS_OUTPUT Main( float4 vPosition : POSITION0,	float4 vPosition2 : POSITION1,	float3 vNormal : NORMAL0,	float3 vNormal2 : NORMAL1,	float3 vVertColor : COLOR,	float2 tTex0 : TEXCOORD0,	float2 tTex1 : TEXCOORD1,	float2 tTex2 : TEXCOORD2,	float2 tTex3 : TEXCOORD3,	float2 tTex4 : TEXCOORD4,	float2 tTex5 : TEXCOORD5,	float2 tTex6 : TEXCOORD6,	float2 tTex7 : TEXCOORD7 )


Most of my models only have one texture, and no vertex colors.
Do I have to rewrite the whole vertex shader for every combination of parameters, or will unused parameters be NULL and only error if accessed?


Thank you,
Yogurt Emperor

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Advertisement
I'm not sure if the inputs are guaranteed to be 0.

You wouldn't make a single shader or a single input to replicate the fixed pipeline, nor would you attempt to recreate each possible version of the shader.

Also, each of those texcoords can be float1, float2, float3, or float4, even in the fixed pipe. There is a second vertex color too, and they are float4s.

Using just 1 texcoord, it can be float1-4, and come from any of the 8 texcoord inputs, or be generated as either positon, normal, reflection, spheremap. It may or may not have a texture coord transform, and it might use projected lookup. So just handling a single texcoord, and nothing else gives 4*(8+4)*2*2, or 192 possible permutations.

Using 2 texcoords, you'll have 192 possible permuations of each, or 36,864 combinations.

For 8 texcoords, you'll have 1.84675732 * pow(10,18) combinations. Even if you could store each shader in a byte, you'd run out of RAM, and we've haven't touched fixed pipe skinning, material options, lights, and the texture stage blending options.

What I'm saying is DON'T MAKE A SHADER THAT DOES EVERYTHING. MAKE THE SHADERS THAT YOU NEED. Typically a full game will use a dozen shaders, maybe two dozen, each with a few techniques in each (normal, boned, batched, etc.)
Thank you for your reply.

I know there more to a fixed-function replica than what I posted and I never intended to make a do-all shader.
This is just an example to illustrate the question I am asking, and the only answer I need (however now I need a second answer).

I only need to know what happens with parameters that are not actually part of the active vertex declaration when the shader is invoked. 99.999% of the time I would not be supplying anything for the second position and normal, so am I required to make 2 shaders, one for second position/normal and one without, or can I just make one that skips usage of the second position/normal based on a boolean flag? DISCLAIMER: This question does not reflect my true intentions—the answer to this hypothetical question will give me all the information I need to proceed with my true plans.

Second question:
If I used float4 for the POSITION (as shown), can I no longer send 3-float vertices into the stream, or will the 4th component automatically be set to 1.0 (or just be garbage; whatever follows the point in the stream)?
Obviously no good could come of this in any case (if the shader is designed to work with a 4-point vertex then you must send it one) but again this is just a hypothetical question for the sake of knowledge/planning.


Thank you,
Yogurt Emperor

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

I can answer that last part.
1 Float will become float,0,0,1 as a float4 in the shader
2 floats will become float1,float2,0,1 as a float4 in the shader.
3 floats will become float1,float2,float3,1 as a float4 in the shader.

This applies to ALL shader inputs, not just position. I'm not sure if this changed in shader model 4 or 5, as I've yet to try them.

Booleans in shaders are tricky. The card may actually branch, or it may calculate both sets of code, and then discard one. As long as the final answer is correct, it can do whatever it wants. The problem comes in the code path you didn't intend to run. Perhaps you'll cause floating point exceptions with you uninitialized values, slowing down your shader (that is, slowing it down even more than the fact that it's running both sets of code).

You can probably get away with it in a vertex shader, however I'd advise against it in the pixel shader.

This topic is closed to new replies.

Advertisement