• Advertisement
Sign in to follow this  

Setting constants to shaders

This topic is 4836 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

Hi, I'm trying to find out, how to set structures and arrays to shader? For example: I have in shader: struct DIRECTIONAL_LIGHT { float3 Direction; float4 Diffuse; float4 Specular; }; DIRECTIONAL_LIGHT DirLight; Is it correct to set it like this: struct DirectionalLight { D3DXVECTOR3 Direction; D3DXVECTOR4 Diffuse; D3DXVECTOR4 Specular; }; DirectionalLight DirLight; ConstantTable->SetValue(Device, LightDirHandle, &DirLight, sizeof(DirLight) ); ??? What about setting second member of array? (Example: DIRECTIONAL_LIGHT DirLight[4]; How to set DirLight[2] ???) How is ID3DXConstantTable interface exatly working? What is in the constant table? How is constant handle search done? Thanks for answer Filousov

Share this post


Link to post
Share on other sites
Advertisement
To access an array of constants, you can use "arrayName[index]" (ie DirLight[2]). So, you can just do this:
// Initialization
// Get the handles of each of the array members
D3DXHANDLE LightDirHandle0 = ConstantTable->GetConstantByName( NULL, "DirLight[0]" );
D3DXHANDLE LightDirHandle1 = ConstantTable->GetConstantByName( NULL, "DirLight[1]" );
/* And so on...*/

// Run-time
// Set each of the values using the handles
ConstantTable->SetValue(Device, LightDirHandle0, &DirLight, sizeof(DirLight));

Share this post


Link to post
Share on other sites
Hi,
still the structure is not set to shader:

I have structure in shader:
struct DIRECTIONAL_LIGHT
{
float3 Direction;
float4 Diffuse;
float4 Specular;
};

DIRECTIONAL_LIGHT DirLight;

But by calling:
ConstantTable->SetValue(Device, DirLightHandle, &DirLight, sizeof(DirLight) );

member DirLight is not set!!!
Why?
Must I set each member of DIRECTIONAL_LIGHT separately?

I'm suffering very much, that Microsoft is not able to properly document such things.

Thanks for answer

Share this post


Link to post
Share on other sites
I tried it and the shader gets not the same data:

Data set:
Direction (-1, -1, 0)
Diffuse (1, 1, 1, 1)
Specular (1, 1, 1, 1)

And in pixel shader I have:
Direction(-1, -1, 0)
Diffuse (-1, -1, 0, 1) ????
Specular (1, 1, 1, 1)

Share this post


Link to post
Share on other sites
It's interesting, that changing order of struct members makes setting using SetFloatArray correct.

So good order is:
struct DIRECTIONAL_LIGHT
{
float4 Diffuse;
float4 Specular;
float3 Direction;
};

I tried both orders with same constant setting method using SetFloatArray. Only good order worked.

Trying to set structure using SetValue at once result in no error, but NOTHING IS SET!!!!



Share this post


Link to post
Share on other sites
perhaps the struct is organized according to size, somehow. First the float4 then float 3.

Share this post


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

  • Advertisement