Reputation: 385
I'm trying to write some DirectX code that takes a compiled shader and uses reflection to automatically generate input layouts for vertex shaders. I have a simple vertex shader which is designed to take advantage of instancing, this is the input it accepts:
struct VertexIn
{
// Per-vertex data
float3 pos : POSITION;
float4 color : COLOR;
// Per-instance data
matrix worldMatrix : WORLDMATRIX;
};
And my layout, if I were to write it by hand, would be:
// Per-vertex data
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
// Per-instance data.
{ "WORLDMATRIX", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
{ "WORLDMATRIX", 1, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
{ "WORLDMATRIX", 2, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
{ "WORLDMATRIX", 3, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_INSTANCE_DATA, 1 }
When I reflect the compiled shader I get the correct number of input parameters, but when I try to read the input descriptions through D3D11_SIGNATURE_PARAMTER_DESC
, I can't find the details that I need to fill the D3D11_INPUT_ELEMENT_DESC
members InputSlot
, InputSlotClass
and InstanceDataStepRate
? Am I looking in the wrong description for this information or is getting instancing details not possible through reflection?
If this isn't possible then I'm guessing the only other alternative I have is to pass the expected vertex input layout and check all parameters?
Upvotes: 3
Views: 1449
Reputation: 10039
It is not possible to know what the values for InputSlot
, InputSlotClass
and InstanceDataStepRate
should be, given only a vertex shader. It is a question of data binding, vs. data processing. The vertex shader processes data, but the input layout defines how the data is fed to the shader.
For example, looking at your shader input definition, you give no indication (other than the comment) that worldMatrix
is an per-instance member. In fact, it doesn't have to be - you could easily make an input layout and vertex buffer binding which made it not per-instance data, using the same vertex shader code.
As an alternative, you could create a semantic naming convention within the shader code to dictate what the values of the input layout become instanced, and their instancing advance rate. For example:
matrix worldMatrix : WORLDMATRIX_INSTANCED_1_1;
By parsing the semantic, you can get that it is instanced, the input slot and step rate. However, such a scheme might be more trouble than it's worth. At some point, you will need to know what input data you have, and how it will be bound to a shader you wrote, thus dictating the input layout.
Upvotes: 3