HLSL Shader Question : Vertex Shader fails, Pixel Shader Succeeds

Started by
6 comments, last by L. Spiro 12 years, 7 months ago
Here is my sooper awesomer shader:



// globals, set via constant table
uniform extern float4x4 MatWorld;
sampler2D TexSampler;

// Vertex shader input structure
// using a 2d engine so Position is float3
struct VS_INPUT
{
float4 Position : POSITION;
float2 Texture : TEXCOORD0;
};

// Vertex shader output structure
// Position converted to float4 for DX
struct VS_OUTPUT
{
float4 Position : POSITION;
float2 Texture : TEXCOORD0;
};

// **** VERTEX SHADER ****
VS_OUTPUT vs_main( in VS_INPUT In )
{
VS_OUTPUT Out = (VS_OUTPUT)0;

Out.Position = In.Position;
Out.Texture = In.Texture; //copy original texcoords

return Out;
}

// **** PIXEL SHADER ****
float4 ps_main( float2 Tex : TEXCOORD0 ) : COLOR0
{
float4 Color;

Color = tex2D( TexSampler, Tex.xy);
return Color;
}

technique fx_technique
{
pass P0
{
VertexShader = compile vs_2_0 vs_main();
PixelShader = compile ps_2_0 ps_main();

FillMode = Solid;
}
}



It takes any textured quad I give it and renders NOTHING! w00t!

BUT! If I change the technique to:




technique fx_technique
{
pass P0
{
VertexShader = null;
PixelShader = compile ps_2_0 ps_main();

FillMode = Solid;
}
}


It renders my textures. I've debugged the Shader with Pix and it says the the VertexShader is doing what it's supposed to, but it doesn't draw anything and I think for some reason that when vs_main() is called ps_main() isn't.

Any clues are greatly appreciated.
Advertisement
You are missing position in your pixel shader. Your vertex shader outputs position and texcoords, but the pixel shader only takes a texcoord. Add Position and it should compile
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.
I have found it a good practice to use the vertex output structure as input to the pixel shader.

[color=#1C2837][font=CourierNew, monospace][size=2][color=#000000] float4 ps_main[color=#666600]([color=#000000] [/font][font=CourierNew, monospace][size=2]VS_OUTPUT[/font][font=CourierNew, monospace][size=2][color=#000000] [color="#660066"]V[color=#000000] [color=#666600])[color=#000000] [color=#666600]:[color=#000000] COLOR0[/font]
[font=CourierNew, monospace][size=2][color=#000000]
[/font]
[color=#000000]Cheers!
same thing...

Here it is now:



// globals, set via constant table
uniform extern float4x4 MatWorld;
sampler2D TexSampler;

struct VS_VERTEX
{
float4 Position : POSITION;
float2 Texture : TEXCOORD0;
};

// **** VERTEX SHADER ****
VS_VERTEX vs_main( VS_VERTEX vsIn )
{
VS_VERTEX Out = (VS_VERTEX)0;

//float4x4 mat = mul(MatProj, MatView);
//mat = mul(MatWorld, mat);

Out.Position = mul(vsIn.Position, MatWorld);
Out.Texture = vsIn.Texture;

return Out;
}

// **** PIXEL SHADER ****
float4 ps_main( VS_VERTEX vsIn ) : COLOR0
{
float4 Color;

Color = tex2D( TexSampler, vsIn.Texture.xy);
return Color;
}

technique fx_technique
{
pass P0
{
VertexShader = compile vs_2_0 vs_main();
PixelShader = compile ps_2_0 ps_main();
FillMode = Solid;
}
}




If I set VertexShader = null; in the technique it renders, but no transform.

If I debug the Vertex Shader in Pix, it gives me the correct values, but nothing is drawn.
I have a better idea!


I think DX moves the vertices into 3d space differently when you translate in C++ using d3d_device->SetTransform() (which works) then if you translate using a shader.

I'll go back and make sure the ViewPort and Camera are set for 3d.

Thanks for your help.
If the names of your variables are not misleading, you are transforming the vertex by the world matrix.
You must translate it by the world-view-projection matrix.


L. Spiro

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

Multiply by world, then by view, then by projection.
But to be clear, These matrices can easily be combined into one, so that you do not waste cycles by performing 3 matrix multiplies.
It is best to compose a single matrix that represents all three of these transforms, and call it the WorldViewProjection matrix.


L. Spiro

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

This topic is closed to new replies.

Advertisement