Shader Reflection
Shader的信息可以通过轮询ID3D11ShaderReflection接口获取,该接口也可以获取用于轮询Constant Buffer,Variable,Type信息的接口。通过D3DReflect()函数可以从shader获取其ID3D11ShaderReflection接口
以下为从shader中获取ID3D11ShaderReflection的方法:
- ID3DBlob* pCompiledShader;
-
- // call D3DCompile Compile Shader
- ...
-
- Microsoft::WRL::ComPtr<ID3D11ShaderReflection> pReflector;
-
- D3DReflect(pCompiledShader->GetBufferPointer(),pCompiledShader->GetBufferSize(),IID_ID3D11ShaderReflection,reinterpret_cast<void**>(pReflector.GetAddressOf()));
我们可以通过该接口获取D3D11_SHADER_DESC,该结构体用于描述shader
其成员ConstantBuffes表示shader constant buffer数目,InputParameters,OutputParameters,BoundResources分别描述了输入签名数,输出签名数,shader绑定资源数。
- D3D11_SHADRE_DESC ShaderDesc;
- pReflector->GetDesc(&ShaderDesc);
Input Signature
我们可以通过ID3D11ShaderReflection调用GetInputParameterDesc()获取shader的输入参数描述
原型如下:
- HRESULT GetInputParameterDesc(
- [in] int ParameterIndex,[out] D3D11_SIGNATURE_PARAMETER_DESC *pDesc
- );
因此我们可以获取shader所有的输入描述
- // 用于保存输入参数描述
- std::vector<D3D11_SIGNATURE_PARAMETER_DESC> InputSignatures;
- for(int i = 0; i < ShaderDesc.InputParameters; i++)
- {
- D3D11_SIGNATURE_PARAMETER_DESC InputDesc;
- pReflector->GetInputParameterDesc(i,&InputDesc);
- InputSignatures.push_back(InputDesc);
- }
Output Signature
与InputSignature类似,我们可以通过GetOutputParameterDesc()获取shader的输出参数描述。
函数原型如下:
- HRESULT GetOutputParameterDesc(
- [in] UINT ParameterIndex,[out] D3D11_SIGNATURE_PARAMETER_DESC *pDesc
- );
- // 保存shader所有的输出描述
- std::vector<D3D11_SIGNATURE_PARAMETER_DESC> OutputSignatures;
- for(int i = 0; i < ShaderDesc.OutputParameters; i++)
- {
- D3D11_SIGNATURE_PARAMETER_DESC OutputDesc;
- pReflector->GetOutputParameterDesc(i,&OutputDesc);
- OutputSignatures.push_back(OutputDesc);
- }
Constant Buffer
ID3D11ShaderReflection接口还可以获取ID3D11ShaderReflectionConstantBuffer接口,该接口包含shader的constant buffer的信息,该接口可以通过调用GetConstantBufferByName()或者GetConstantBufferByIndex()获取
我们可以通过如下方式获取shader中constant buffer的信息
- // ConstantBuffers保存所有的Constant Buffer描述
- for(int i = 0; i < ShaderDesc.ConstantBuffers; i++)
- {
- ID3D11ShaderReflectionConstantBuffer* pCBReflecion = nullptr;
- pCBReflection = pReflector->GetConstantBufferByIndex(i);
-
- D3D11_SHADER_BUFFER_DESC ShaderBuferDesc;
- pCBReflection->GetDesc(&ShaderBufferDesc);
-
- if(ShaderBufferDesc.Type == D3D_CT_CBUFFER || ShaderBufferDesc.Type == D3D_CT_TBUFFER)
- {
- ConstantBuffers.push_back(ShaderBufferDesc);
-
- // 查询constant buffer接口信息
- ...
- }
- }
ID3D11ShaderReflectionConstantBuffer接口也提供一个GetDesc() 方法,该方法返回一个包含constant buffer信息的结构体,结构体为D3D11_SHADER_BUFFE_DESC,声明如下:
- typedef struct D3D11_SHADER_BUFFER_DESC
- {
- LPCSTR Name;
- D3D_CBUFFER_TYPE Type;
- UINT Variables;
- UINT Size;
- UINT uFlags;
-
- }D3D11_SHADER_BUFFER_DESC;
我们感兴趣的部分主要是Size和Variables,Size表示Constant Buffer的总大小,可以用于初始化ID3D11ConstantBuffer,Variables表示Constant Buffer中常量数,Variables结合GetVariableByIndex可以枚举constant buffer中的所有常量
Variable
ID3D11ShaderReflectionVariable接口包含了Constant Buffer中Constant的信息,该接口可以由ID3D11ShaderReflectionConstantBuffer接口调用GetVariableByName()或者GetVariableByIndex()获取,ID3D11ShaderReflection接口调用GetVariableByName()或者GetVariableByIndex()可以获取shader中的全局变量,全局变量不仅包含constants,而且还有resource object , samplers , interface instance。ID3D11ShaderReflectionVariable接口的GetDesc()方法返回一个D3D11_SHADER_VARIABLE_DESC结构体。该结构体包含变量的总说明。
- typedef struct D3D11_SHADER_VARIABLE_DESC
- {
- LPCSTR Name;
- UINT StartOffset;
- UINT Size;
- UINT uFlags;
- LPVOID DefaultValue;
- UINT StartTexture;
- UINT TextureSize;
- UINT StartSampler;
- UINT SamplerSize;
- }D3D11_SHADER_VARIABLE_DESC;
Name为HLSL中变量的名字,StartOffset,Size包含了Constant Buffer开始出的字节偏移和该值的字节大小。
Type
ID3D11ShaderReflectionType接口包含了变量的类型信息,可以由变量的ID3D11ShaderReflectionVariable接口调用GetType()得到。调用该接口的GetDesc方法将会返回一个结构体D3D11_SHADER_TYPE_DESC
- typedef struct D3D11_SHADER_TYPE_DESC
- {
- D3D_SHADER_VARIABLE_CLASS Class;
- D3D_SHADER_VARIABLE_TYPE Type;
- UINT Rows;
- UINT Columns;
- UINT Elements;
- UINT Members;
- UINT Offset;
- LPCSTR Name;
- }D3D11_SHADER_TYPE_DESC;
该结构体的Class成员表明了该变量是个标量,向量,矩阵,资源对象,结构体,类或者接口指针。Type成员指明了标量,向量,矩阵的原始类型,如float,int , double等等。若该变量为资源对象,则Type指明的是资源类型,如Texture2D,Buffer等等。Rows,Columns表明了矩阵的行和列数,其他的数值类型为1。Elements表明了数组类型的元素数。Members表明Structure或者class类型的成员数。Offset表示相对于父结构的偏移。
- // 查询constant buffer 接口信息
-
- // Variables保存Constant Buffer中所有变量信息
- // VariableTypes保存Constant Buffer中所有变量的类型信息
- for(int j = 0; j < ShaderBufferDesc.Variable; j++)
- {
- ID3D11ShaderReflectionVariable* pShaderReflectionVariable = GetVariableByIndex(i);
-
- D3D11_SHADER_VARIABLE_DESC VariableDesc;
- pShaderReflectionVariable->GetDesc(VariableDesc);
-
- Variables.push_back(VariableDesc);
-
- // ------------Type--------------
- ID3D11ShaderReflectionType* pShaderReflectionType = pShaderReflectionVariable->GetType();
-
- D3D11_SHADER_TYPE_DESC ShaderTypeDesc;
- pShaderReflectionType->GetDesc(&ShaderTypeDesc);
-
- VariableTypes.push_back(ShaderTypeDesc);
- }
Resource Bind
ID3D11ShaderReflection接口通过调用GetResourceBindingDesc方法将会返回一个D3D11_SHADER_INPUT_BIND_DESC结构体,该结构体描述了资源绑定信息:
- typedef struct D3D11_SHADER_INPUT_BIND_DESC
- {
- LPCSTR Name; // Name of the resource
- D3D_SHADER_INPUT_TYPE Type; // Type of resource (e.g. texture,cbuffer,etc.)
- UINT BindPoint; // Starting bind point
- UINT BindCount; // Number of contiguous bind points (for arrays)
-
- UINT uFlags; // Input binding flags
- D3D_RESOURCE_RETURN_TYPE ReturnType; // Return type (if texture)
- D3D_SRV_DIMENSION Dimension; // Dimension (if texture)
- UINT NumSamples; // Number of samples (0 if not MS texture)
- }D3D11_SHADER_INPUT_BIND_DESC;
- // ResourceBinds:
- // std::vector<D3D11_SHADER_INPUT_BIND_DESC>保存绑定资源信息
- for(int i = 0; i < ShaderDesc.BoundResources; i++)
- {
- D3D11_SHADRE_INPUT_BIND_DESC ResourceBindDesc;
- pReflector->GetResourceBinding(i,&ResourceBindDesc);
-
- ResourceBinds.push_back(ResourceBindDesc);
- }