HLSL, simple shader - help

Started by
4 comments, last by Magos 18 years, 10 months ago
Recently started with shaders. I'm having a problem with a vertex shader. Assume this simple vertex shader, the box I'm rendering (2x2x2 centered around origo) is rendered stretched across the entire screen (as it should since it's untransformed coordinates, right?)

struct VS_IN
{
  float4 Position : POSITION;
};

struct VS_OUT
{
  float4 Position : POSITION;
};

VS_OUT main(VS_IN In)
{
  VS_OUT Out;

  Out.Position = In.Position;

  return Out;
}
Now when I try to apply a matrix multiplication to put the vertices in their correct positions the box won't show up at all. The code below should give the same effect as if the shader wouldn't be applied at all, right? Without the shader the box renders fine, but with the shader the box won't show at all. What could be wrong?

struct VS_IN
{
  float4 Position : POSITION;
};

struct VS_OUT
{
  float4 Position : POSITION;
};

float4x4 Matrix : WORLDVIEWPROJECTION;

VS_OUT main(VS_IN In)
{
  VS_OUT Out;

  Out.Position = mul(In.Position, Matrix);

  return Out;
}
----------------------------------------MagosX.com
Advertisement
Clutching at straws a bit here, but have you tried having a float3 in your VS_IN structure?

I've seen a lot of code with Out.Position = mul(float4(In.Position,1), Matrix); or similiar... that is, putting the W=1.0f component in at the shader level.

Just a guess [smile]
hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Didn't seem to work. I have a strong suspicion that the matrix only contains 0's thus making everything appear at 0,0,0 thus not visible.
When typing this:

float4x4 Matrix : WORLDVIEWPROJECTION;


It correctly refers to the World * View * Projection matrix, right? Have I missed something? Do I have to somehow manually send it to the device?
----------------------------------------MagosX.com
yes, you must. The optional semantic on global variables are useless... for now.

set a D3DXHANDLE to the return of ID3DXEffect::GetParameterByName() with the first parameter NULL. Then call ID3DXEffect::SetMatrix with that handle as the first parameter, and your matrix as the second.
When using HLSL, you have to make sure that your application is setting all the constants the shader needs to use. In your case, your applicaiton must set the matrix that the shader will use. To do this, you call ID3DXEffect::SetMatrix if you're using the effect interface. If you're using HLSL without the effect interface, then you need to pass variables to the shader through the constant table. You can set the matrix by calling ID3DXConstantTable::SetMatrix.

Calling IDirect3DDevice::SetTransform will NOT associate your matrix with the shader. This function is only for the fixed function pipeline and has nothing to do with shaders at all.

The optional semantics on global variables are not useless. The semantics are there so that an engine which adheres to the DX standard about shader semantics can automatically set the constants a shader needs. Underneath, the engine will still call ID3DXEffect::Set*. The key thing is that the application/engine needs to be written to take advantage of the optional semantics; the D3D runtime won't do it for you.

neneboricua
The constant table was the problem, it works now, thanks!
----------------------------------------MagosX.com

This topic is closed to new replies.

Advertisement