HLSL Code:

struct Light
    float3 pos;
    float3 dir;
    int type;
} m_aLights[3];

How can I get/set an effectvariable to m_aLights with Direct3D10?



2 Answers 2


say you have a global effect variable in your effect file like:

float myVariable; 

you have to create a pointer to this in your code like this:

declare it:

ID3D10EffectMatrixVariable* pointerToVariable; //declaration

point to the effect:

pointerToVariable = pEffect->GetVariableByName("myVariable")->AsScalar(); //in your initialisation

Set it:

myVariable += 0.5f; //so whatever to the variable in a draw call for example
pointerToVariable->SetFloat(&myVariable); //and send it through

There are also methods which are called appropriate things such as AsIntVector, AsFloatVector, AsMatrix, SetMatrix, SetIntVector etc for the appropriate types of variables.

  • \$\begingroup\$ Yes, I know that but these can not set an array of structs, which was my question... \$\endgroup\$
    – xcrypt
    Dec 9, 2011 at 16:49

This is simple.

First skip when you initialize your effect variable AsScalar() or whatever because this is your own structure, and DirectX does not know your stuff.

Then, the most important thing is when you refresh effect variables every frame.

Do: NameOfYourVariable->SetRawValue(void*) (so address of your struct)

P.s: if it is already a pointer, pass it without "&", offset (if padding problem, add offset or shift will be shifting everything in your structure by passing in the shader), sizeof(yourStruct)).

Note: First, padding can be caused by virtual function thanks to inheritance, so struct should be okay, it is not a class which inherits. Second, in your struct, be sure to place bigger variable first, then place other in a decreasing size order. Make sense? Should be.

Therefore, weird things can appear if not power of two with DirectX so if you start with a float4 as color and place couple of vector3 after, add a float between each vector3 as padding and don't worry, you're not going to add these float but they will set your variable's values in the shader to the right BYTE.

e.g. Vector3
     INT64    (smaller than vector3 = sizeof(FFLOAT) * 3).
     FFLOAT   (32 bits < 64).
     CCHAR    (8 bits < 32).

That's it.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .