Jump to content
  • Advertisement
Sign in to follow this  
AndyGeers

HLSL constants problem

This topic is 4749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm just starting to experiment with the use of vertex shaders for the first time, and am having some problems moving over from the assembly language version to the High Level shader language. I'm trying to get my World View matrix passed in, and it's not working right. In the shader file I have these global declarations:
float4x4 mProjection;
float4x3 mWorldView;
Then in the main game code I do this:
D3DXHANDLE handle;
if (handle = m_pShaderConstants->GetConstantByName(NULL, "mWorldView"))
{
    // Set worldview matrix
    m_pShaderConstants->SetMatrix(m_pd3dDevice, handle, (D3DXMATRIX*)&matWorldView);
}
if (handle = m_pShaderConstants->GetConstantByName(NULL, "mProjection"))
{
    // Set projection matrix
    m_pShaderConstants->SetMatrix(m_pd3dDevice, handle, (D3DXMATRIX*)&matProj);
}
The weird thing is that it works for 'mProjection' but not 'mWorldView'. I tried taking out the 'if' clauses (in case 0 was a valid handle after all) but the call corresponding to matWorldView returned a FAILED hresult. I've tried all sorts of combinations: changing the order of the function calls, changing the order of the declarations in the shader, renaming them, making both a 4x4 matrix. None of it seems to work - any ideas? Edit: I get the 'm_pShaderConstants' pointer during the call to D3DXCompileShaderFromFile. Edit2: I'm using shaders version 1.1 [Edited by - AndyGeers on May 23, 2005 11:12:00 AM]

Share this post


Link to post
Share on other sites
Advertisement
Does your shader reference mWorldView? If you use FXC to compile your shader from the command line, does it show mWorldView in the constant table?

xyzzy

Share this post


Link to post
Share on other sites
It's supposed to reference mWorldView, but it's not showing up in the parameter list in the source file outputted by FXC. Here's the full shader code:


struct VS_INPUT
{
float4 vPosition : POSITION;
float3 vNormal : NORMAL;
};

struct VS_OUTPUT
{
float4 vPosition : POSITION;
float3 vDiffuse : COLOR0;
};

// Declare the required matrices with the appropriate semantics
// so that the viewer can supply the necessary matrix information.
const float4x4 mProjection;
const float4x3 mWorldView;

// Helper function to transform position/normal into view space
void TransformVertex(float4 vPos, float3 vNormal,
float3 vTransformedPosition, float3 vTransformedNormal)
{
// Transform the position into view space
vTransformedPosition = mul(vPos, mWorldView);

// Transform the normal into view space
// (just use the upper 3x3 of WorldView)
vTransformedNormal = mul(vNormal, (float3x3)mWorldView);
}

//----------------------------------------------------------------------------
// Shader body
//----------------------------------------------------------------------------
VS_OUTPUT main(const VS_INPUT v)
{
VS_OUTPUT Output;
float3 vTransformedPosition = {0,0,0};
float3 vTransformedNormal = {0,0,0};

// Transform the position and normal into world space.
TransformVertex(v.vPosition, v.vNormal,
vTransformedPosition, vTransformedNormal);

// Transform position into screen space from view space.
Output.vPosition = mul(float4(vTransformedPosition, 1.0), mProjection);

// Make it black
Output.vDiffuse = float4(0, 0, 0, 1);

return Output;
}

Share this post


Link to post
Share on other sites
Hmm, well removing the function call and pulling the code inside the main function fixes the problem. Does that mean that the example given in the SDK doesn't work then, since it's more or less exactly the same code?

Share this post


Link to post
Share on other sites
Not sure if this helps, ive never created another function inside of HLSL except for a fragment of a shader which outputs something. BUT when you call TransformVertex(...) could it not be using the same vTransformedPosition, or vTransformedNormal? Try taking out the function and simply write all of it inside of the actualy shader(cutting out the function totally.) What happens is the compiler is "Optimizing" it and taking out mWorldView totally. Although it looks like your're using it, your're really not, because it is being optimized and removed.

EDIT: sum1 already made this point, i need to start reading through posts. But in normal C++ you pass it as a pointer or a reference usually(ive never done it in hlsl, so not sure if there is a difference.)

Share this post


Link to post
Share on other sites
The problem is that your TransformVertex function isn't actually returning anything.. so HLSL correctly optimizes it away. If you wanted it to actually return vTransformedPosition and vTransformedNormal, you would need to tag those as 'out' parameters, like this:

void TransformVertex(float4 vPos, float3 vNormal,
out float3 vTransformedPosition, out float3 vTransformedNormal)

xyzzy

Share this post


Link to post
Share on other sites
Thanks for that - I'll try adding 'out' in there.

the question remains though - is the example Glow.fx in the SDK broken then? Cos I just copied that function from there!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!