Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

So far, we've only used pixel shaders because we never needed to do any custom processing at the vertex shader level. However, the 3.0 model shader forces you to define a vertex shader. And that's where I get lost.

That's because our pixel shaders have this signature:

sampler2D Texture0;

struct PixelShaderInput{
        float2 TexCoord : TEXCOORD0;
        float4 Color : COLOR;
};
struct PixelShaderOutput {
    float4 color : COLOR0;
};
PixelShaderOutput PixelShaderFirstPass( PixelShaderInput input ) {
        //Magic happens here
        //We use the tex2D function to sample Texture0
}

From what I've read about vertex shaders, the vertex shader output is the pixel shader input. So I added this:

struct VertexShaderInput{
    float4 Position : POSITION;
}

PixelShaderInput MyVertexShader( VertexShaderInput input  ){
     //At a loss here...
}

I don't know how to map the vertex shader input to the pixel shader input. All examples I found on the web just have the vertex shader return a float4, which is what their pixel shader expects.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

A vertex shader can map the TEXCOORD0 semantic input. So I just receive it and return it unchanged for the pixel shader to use. Same for COLOR. But the vertex shader is required to process the POSITION input (I got an error saying so when I compiled the effect). So I ended up with this (currently working, and the speedup was small but noticeable):

I also noticed my pixel shaders were not using their COLOR input, so I removed it (they obtained the pixel color by sampling the texture at the given coordinates).

sampler2D Texture0;
float4x4 g_matProjection;
float4x4 g_matView;
float4x4 g_matWorld;

struct VertexShaderInput{
    float4 Position : POSITION;
    float2 TexCoord : TEXCOORD0;
};
struct VertexShaderOutput {
    float4 Position : POSITION;
    float2 TexCoord : TEXCOORD0;
};
VertexShaderOutput BasicVertexShader( VertexShaderInput input ) {
    VertexShaderOutput output;
    output.Position = mul( input.Position, g_matWorld );
    output.Position = mul( output.Position, g_matView );
    output.Position = mul( output.Position, g_matProjection );
    output.TexCoord = input.TexCoord; //Just pass it along
    return output;
}

struct PixelShaderOutput {
    float4 color : COLOR0;
};
PixelShaderOutput PixelShaderFirstPass( VertexShaderOutput input ) {
        //Magic happens here
        //We use the tex2D function to sample Texture0
}

Another thing I learnt is that when you implement your own vertex shader, D3D calls that set the transform matrices are ignored. So, for example, I had to replace m_pd3dDevice->SetTransform( D3DTS_WORLD, pNewVal ) with an effect SetMatrix call that updates the g_matWorld effect variable. Same for view and projection matrices.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.