Reputation: 1035
Is there any way to set HLSL (for example) vertex shader constant from c++ code in opengl-style?
I mean - no d3dx, no constant-table. Shader is already compiled with fxc.exe and set. Have only LPDIRECT3DVERTEXSHADER9 and LPDIRECT3DDEVICE9. I planned to use SetVertexShaderConstant
*, but found no way to translate constant name (for example "u_mvpMatrix") to "Register number that will contain the first constant value.".
Upvotes: 2
Views: 3381
Reputation: 76
With the deprecation of D3DX I feel many people could find this topic in search of a solution to the same problem, as did I.
There is indeed a way to translate constant name to register number, by parsing the actual compiled shader code.
I've found a topic discussing exactly this and positing working code that implements this, and it works flawlessly. I don't want to take credit, but link the original author, so apologies for "just" posting a link to the topic on GameDev.net:
http://www.gamedev.net/topic/648016-replacement-for-id3dxconstanttable/#entry5095598
This solution is exactly what the OP was asking for, not a "workaround".
Upvotes: 0
Reputation: 891
You can set constants by name using the ID3DXConstantTable API. For example if you have a float4
constant called cameraPos
in you shader you can set it from C/C++ like so:
float val[4] = {0,1,0,1};
D3DXHANDLE camposHandle = consttab->GetConstantByName(NULL, "cameraPos");
consttab->SetFloatArray(d3ddev, camposHandle, val, 4);
In this example, d3ddev
is the device object and consttab is the ID3DXConstantTable
object. You can get this object when you compile the shader code using D3DXCompileShaderFromFile or one of its variants.
Upvotes: 0
Reputation: 665
You need to bind your constants to registers in your shader file. Eg.
float4 sample_constant : register(c12);
This binds the sample_constant to register 12. You can then use SetVertexShaderConstant() to set the constant using 12 for the register.
Upvotes: 1