?/TD>
Microsoft DirectX 9.0

Getting and Setting Effect Values


Getting and setting effect values can be accomplished with any of the type specific methods in ID3DXBaseEffect, or just two generic methods: ID3DXBaseEffect::GetValue and ID3DXBaseEffect::SetValue. This includes any arbitrary parameter or annotation, simple types, structs, arrays, strings, shaders, and textures. These two methods can be used in place of nearly all Getxxx and Setxxx calls in ID3DXBaseEffect.

In all of these examples, handles could be used in place of the strings shown here.

Getting Arbitrary Values

Given the following effect file parameters:

float fValue;
float4 vecLight;
float4x4 mWorld;
float3x2 m3x2matrix;
string xFile = "bigship1.x";
texture tex1;
vertexshader vsMyShader;
vertexshader vsArray[4] = {
    /* initialize 4 vertex shaders here ... */
};

struct {
    float fArray[4];
    float fSingle;
} myStruct, myStructArray[2];

// and a pointer to an effect interface
LPD3DXEFFECT pEffect;
// which needs to be initialized before using it.

Get a single floating point value.

FLOAT f;
pEffect->GetValue("fValue", (void*)&f, D3DX_DEFAULT);

Get a string.

LPCSTR szString;
pEffect->GetValue("xFile", (void*)&szString, sizeof(LPCSTR));

Get a vertex shader. Shaders are handled in exactly the same way as textures. They must be released when no longer needed.

LPDIRECT3DVERTEXSHADER9 pVShader;
pEffect->GetValue("vsMyShader", (void*)&pVShader, sizeof(LPDIRECT3DVERTEXSHADER9));
SAFE_RELEASE(pVShader);

Get a vector:

D3DXVECTOR4 vecLight;
pEffect->GetValue("vecLight", (void*)(FLOAT*)vecLight, sizeof(D3DXVECTOR4));

Get a matrix:

D3DXMATRIX matWorld;
pEffect->GetValue("mWorld", (void*)(FLOAT*)matWorld, sizeof(D3DXMATRIX));

Get a non-4x4 matrix. Matrices are stored without padding, so a 3x2 matrix is six floats.

FLOAT m3x2matrix[3 * 2];
pEffect->GetValue("m3x2matrix", (void*)m3x2matrix, desc.Bytes);

If you have a D3DXPARAMETER_DESC "desc" that has been filled in by GetParameterDesc, you can also pass in desc.Bytes.

Get a string. You are getting just a pointer to a const string. The size is always sizeof(LPCSTR), and you should copy the string if you intend to use it later because it is not guaranteed to be around after performing more operations with the effect.

LPCSTR szString;
pEffect->GetValue("xFile", (void*)&szString, sizeof(LPCSTR));

Get a texture. It sets your pointer to the texture parameter, and increments the ref count. This means that you are responsible for calling Release on that pointer when you're done with it.

LPDIRECT3DBASETEXTURE9 pTexture;
pEffect->GetValue("tex1", (void*)&pTexture, sizeof(LPDIRECT3DBASETEXTURE9));
SAFE_RELEASE(pTexture);

Get an array of pointers to vertex shaders:

LPDIRECT3DVERTEXSHADER9 pVSArray[4];
pEffect->GetValue("vsArray", (void*)pVSArray, sizeof(LPDIRECT3DVERTEXSHADER9) * 4);
// don't forget to release each object in the array

Get myStruct. Structs are always numeric types and flattened without padding. The struct has 5 floats, so initialize an array to hold it:

FLOAT pMyStruct[4 + 1];
pEffect->GetValue("myStruct", (void*)pMyStruct, sizeof(pMyStruct));

Get an array of structs:

FLOAT pMyStructArray[ (4+1) * 2 ];
pEffect->GetValue("myStructArray, (void*)pMyStructArray, sizeof(pMyStructArray));

Setting Arbitrary Values

Set a single floating point value.

FLOAT f = 1.0f;
pEffect->SetValue("fValue", (void*)&f, D3DX_DEFAULT);

Set a string.

LPCSTR szString = "some string";
pEffect->SetValue("xFile", (void*)&szString, sizeof(LPCSTR));

Set a vertex shader. Shaders are handled in exactly the same way as textures. They must be released when no longer needed.

LPDIRECT3DVERTEXSHADER9 pVShader; // needs initialization
pEffect->SetValue("vsMyShader", (void*)&pVShader, sizeof(LPDIRECT3DVERTEXSHADER9));
SAFE_RELEASE(pVShader);

Set a vector:

D3DXVECTOR4 vecLight; // needs initialization
pEffect->SetValue("vecLight", (void*)(FLOAT*)vecLight, sizeof(D3DXVECTOR4));

Set a matrix:

D3DXMATRIX matWorld; // needs initialization
pEffect->SetValue("mWorld", (void*)(FLOAT*)matWorld, sizeof(D3DXMATRIX));

Set a non-4x4 matrix. Matrices are stored without padding, so a 3x2 matrix is six floats.

FLOAT m3x2matrix[3 * 2]; // needs initialization
pEffect->SetValue("m3x2matrix", (void*)m3x2matrix, desc.Bytes);

If you have a D3DXPARAMETER_DESC "desc" that has been filled in by GetParameterDesc, you can also pass in desc.Bytes.

Set a string. You are getting just a pointer to a const string. The size is always sizeof(LPCSTR), and you should copy the string if you intend to use it later because it is not guaranteed to be around after performing more operations with the effect.

LPCSTR szString; // needs initialization
pEffect->SetValue("xFile", (void*)&szString, sizeof(LPCSTR));

Set a texture. It sets your pointer to the texture parameter, and increments the ref count. This means that you are responsible for calling Release on that pointer when you're done with it.

LPDIRECT3DBASETEXTURE9 pTexture; // needs initialization
pEffect->SetValue("tex1", (void*)&pTexture, sizeof(LPDIRECT3DBASETEXTURE9));
SAFE_RELEASE(pTexture);

Set an array of pointers to vertex shaders as follows:

LPDIRECT3DVERTEXSHADER9 pVSArray[4]; // needs initialization
pEffect->SetValue("vsArray", (void*)pVSArray, sizeof(LPDIRECT3DVERTEXSHADER9) * 4);
// Don't forget to release each object in the array.

Set myStruct. Structs are always numeric types and flattened without padding. The struct has five floats, so initialize an array to hold it.

FLOAT pMyStruct[4 + 1]; // needs initialization
pEffect->SetValue("myStruct", (void*)pMyStruct, sizeof(pMyStruct));

Set an array of structs.

FLOAT pMyStructArray[ (4+1) * 2 ]; // needs initialization
pEffect->SetValue("myStructArray, (void*)pMyStructArray, sizeof(pMyStructArray));


© 2002 Microsoft Corporation. All rights reserved.