Advertisement
Guest User

Input layout creation using reflection in DirectX 11

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