Understanding HLSL

Started by
7 comments, last by M4573R 14 years, 7 months ago
So I was given a basic framework of an .fx file. In it, I have the vertex shader output structure, and the vertex shader function. (I left the other stuff out, but it works perfectly as-is).

struct VS_OUTPUT
{
    float4 pos : POSITION;
    float2 tex0  : TEXCOORD0;
    float3 normal : TEXCOORD1;
};

VS_OUTPUT SimpleVertex( VS_INPUT IN )
{
  VS_OUTPUT OUT;
  
  OUT.pos = mul( float4(IN.position, 1) , worldViewProj);
  OUT.normal = mul( float4(IN.normal, 0) , world );
  OUT.tex0 = IN.tex0;
	
  return OUT;
}

I'm very new to programming shaders, but I need to start learning them to make my next game. Lets say I wanted the vertex shader to output something else, perhaps the position of the vertex in view space. I tried this: float4 view : TEXCOORD2; and OUT.view = mul( float4(IN.normal, 0) , worldView ); ( I loaded the matrix in correctly from my application ) When I run the program, I get an error saying that OUT is being used without being initialized. Same thing happens if I add VS_OUTPUT OUT = (VS_OUTPUT)0; Why does it think it's not initialized when I add something to it?
Advertisement
Hello,

Before I can answer you question, if I understood it right, you updated the shader code to the following?

struct VS_OUTPUT{    float4 pos : POSITION;    float2 tex0  : TEXCOORD0;    float3 normal : TEXCOORD1;    float4 view : TEXCOORD2;};VS_OUTPUT SimpleVertex( VS_INPUT IN ){  VS_OUTPUT OUT;    OUT.pos = mul( float4(IN.position, 1) , worldViewProj);  OUT.normal = mul( float4(IN.normal, 0) , world );  OUT.tex0 = IN.tex0;  OUT.view = mul( float4(IN.normal, 0), worldView);	  return OUT;}


Is this correct?
Hi,

Changing the line
VS_OUTPUT OUT;

to
VS_OUTPUT OUT = (VS_OUTPUT) 0;

may solve your problem.

hth.
Rohat.
There's no "hard", and "the impossible" takes just a little time.
Quote:Original post by programci_84
Hi,

Changing the line
VS_OUTPUT OUT;

to
VS_OUTPUT OUT = (VS_OUTPUT) 0;

may solve your problem.

hth.
Rohat.


I believe he already tried that, atleast that is what I understood from one his last phrases:

Quote:Original post by M4573R
Same thing happens if I add VS_OUTPUT OUT = (VS_OUTPUT)0;
Yes, I already tried that, and it didn't work. And in the line I added to the vertex shader, it's suppose to use the vertex's position, not the normal. But it any case it shouldn't matter for the error it's giving me. And yes, that's what I changed it to Xeile.

Thanks for helping btw, I'm having a hard time finding good tutorials on HLSL.
Could you please post the entire .fx file you're trying to compile, or at least something that reproduces the compile error?
Here it is:

texture texture0;float4x4 worldViewProj : WorldViewProjection;float4x4 world : World;float4x4 worldView : WorldView;float3 lightDir = float3(0,0,-1);static const int MaxMatrices = 80;float4x3   WorldMatrixArray[MaxMatrices] : WORLDMATRIXARRAY;sampler Sampler{    Texture   = texture0;    MipFilter = LINEAR;    MinFilter = LINEAR;    MagFilter = LINEAR;    AddressU = CLAMP;    AddressV = CLAMP;    AddressW = CLAMP;            };struct VS_INPUT{	float3 position	: POSITION;	float3 normal : NORMAL;	float2 tex0 : TEXCOORD0;};struct VS_INPUT_SKIN{    float4 position	: POSITION;    float3 normal : NORMAL;		float2 tex0 : TEXCOORD0;    float4 BlendWeights : BLENDWEIGHT;    float4 BlendIndices : BLENDINDICES;};struct VS_OUTPUT{    float4 pos : POSITION;		float2 tex0  : TEXCOORD0;    float3 normal : TEXCOORD1;    float4 view : TEXCOORD2;};VS_OUTPUT SimpleVertex( VS_INPUT IN ){  VS_OUTPUT OUT = (VS_OUTPUT)0;  	OUT.pos = mul( float4(IN.position, 1) , worldViewProj);	OUT.normal = mul( float4(IN.normal, 0) , world );	OUT.tex0 = IN.tex0;	OUT.view = mul( float4(IN.position, 1) , worldView );		return OUT;}float4 SimplePixel( VS_OUTPUT IN ) : COLOR{   float NL = saturate(dot(IN.normal, lightDir.xyz));   float4 texColor = tex2D( Sampler , IN.tex0 );   return NL * texColor;}VS_OUTPUT SkinVertex( VS_INPUT_SKIN IN , uniform int NumBones ){    VS_OUTPUT OUT;        float3 Pos = float3(0,0,0);    float3 Normal = float3(0,0,0);	       float BlendWeightsArray[4] = (float[4])IN.BlendWeights;    int   IndexArray[4]        = (int[4])IN.BlendIndices;	    	for (int iBone = 0; iBone < NumBones ; iBone++)    {               Pos += mul( IN.position , WorldMatrixArray[IndexArray[iBone]]) * BlendWeightsArray[iBone];        Normal += mul(IN.normal , WorldMatrixArray[IndexArray[iBone]]) * BlendWeightsArray[iBone];    }        	Normal = normalize(Normal);	OUT.pos = mul( float4( Pos , 1.0f) , worldViewProj);		OUT.normal = mul( float4(Normal, 0) , world );		OUT.tex0 = IN.tex0;	return OUT;}technique Technique0{    pass Pass0    {        VertexShader = compile vs_2_0 SimpleVertex();        PixelShader  = compile ps_2_0 SimplePixel();        ZEnable = true;        AlphaBlendEnable = false;    }}technique TechniqueSkin{    pass Pass0    {        VertexShader = compile vs_2_0 SkinVertex( 4 );        PixelShader  = compile ps_2_0 SimplePixel();        ZEnable = true;        AlphaBlendEnable = false;    }}
In your SkinVertex method you're not initializing OUT.view.
I feel dumb now. I didn't think to look at the other shader because I was thinking it had its own out structure for some reason, but it only has its own in structure. Thank you!

This topic is closed to new replies.

Advertisement