Guest User

Input layout creation using reflection in DirectX 11

a guest
Aug 12th, 2015
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.48 KB | None | 0 0
  1.     // Reflect the parameters from the shader.
  2.     // Inspired by: http://members.gamedev.net/JasonZ/Heiroglyph/D3D11ShaderReflection.pdf
  3.     Microsoft::WRL::ComPtr<ID3D11ShaderReflection> pReflector;
  4.     hr = D3DReflect( m_pShaderBlob->GetBufferPointer(), m_pShaderBlob->GetBufferSize(), IID_ID3D11ShaderReflection, &pReflector );
  5.  
  6.     if ( FAILED( hr ) )
  7.     {
  8.         ReportError( "Failed to reflect shader." );
  9.         return false;
  10.     }
  11.  
  12.     // Query input parameters and build the input layout
  13.     D3D11_SHADER_DESC shaderDescription;
  14.     hr = pReflector->GetDesc( &shaderDescription );
  15.  
  16.     if ( FAILED( hr ) )
  17.     {
  18.         ReportError( "Failed to get shader description from shader reflector." );
  19.         return false;
  20.     }
  21.  
  22.     m_InputSemantics.clear();
  23.  
  24.     UINT numInputParameters = shaderDescription.InputParameters;
  25.     std::vector<D3D11_INPUT_ELEMENT_DESC> inputElements;
  26.     for ( UINT i = 0; i < numInputParameters; ++i )
  27.     {
  28.         D3D11_INPUT_ELEMENT_DESC inputElement;
  29.         D3D11_SIGNATURE_PARAMETER_DESC parameterSignature;
  30.  
  31.         pReflector->GetInputParameterDesc( i, &parameterSignature );
  32.  
  33.         inputElement.SemanticName = parameterSignature.SemanticName;
  34.         inputElement.SemanticIndex = parameterSignature.SemanticIndex;
  35.         inputElement.InputSlot = i; // TODO: If using interleaved arrays, then the input slot should be 0.  If using packed arrays, the input slot will vary.
  36.         inputElement.AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
  37.         inputElement.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; // TODO: Figure out how to deal with per-instance data? .. Don't. Just use structured buffers to store per-instance data and use the SV_InstanceID as an index in the structured buffer.
  38.         inputElement.InstanceDataStepRate = 0;
  39.         inputElement.Format = GetDXGIFormat( parameterSignature );
  40.  
  41.         assert( inputElement.Format != DXGI_FORMAT_UNKNOWN );
  42.  
  43.         inputElements.push_back( inputElement );
  44.  
  45.         m_InputSemantics.insert( SemanticMap::value_type( BufferBinding( inputElement.SemanticName, inputElement.SemanticIndex ), i ) );
  46.     }
  47.  
  48.     if ( inputElements.size() > 0 )
  49.     {
  50.         hr = m_pDevice->CreateInputLayout( inputElements.data(), (UINT)inputElements.size(), m_pShaderBlob->GetBufferPointer(), m_pShaderBlob->GetBufferSize(), &m_pInputLayout );
  51.         if ( FAILED( hr ) )
  52.         {
  53.             ReportError( "Failed to create input layout." );
  54.             return false;
  55.         }
  56.     }
Advertisement
Add Comment
Please, Sign In to add comment