2017-11-26 9 views
1

DX11を使用してゲームエンジンを開発しています。私の問題は、vertexShaderBuffernullptrだったので、私は読み取りアクセス違反を受けているということです。DirectX11 C++シェーダバッファがポリゴンレイアウトの説明でヌルになる

bool TerrainShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, LPCSTR vsFileName, LPCSTR psFileName) 
{ 
    HRESULT result; 
    ID3D10Blob* errorMessage = nullptr; 
    ID3D10Blob* vertexShaderBuffer = nullptr; 
    ID3D10Blob* pixelShaderBuffer = nullptr; 
    D3D11_INPUT_ELEMENT_DESC polygonLayout[3]; 
    unsigned int numElements; 
    D3D11_SAMPLER_DESC samplerDesc; 
    D3D11_BUFFER_DESC matrixBufferDesc; 
    D3D11_BUFFER_DESC lightBufferDesc; 


    result = D3DX11CompileFromFile(vsFileName, NULL, NULL, "TerrainVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, NULL, 
     &vertexShaderBuffer, &errorMessage, NULL); 
    if (FAILED(result)) 
    { 
     if (errorMessage) 
     { 
      OutputShaderErrorMessage(errorMessage, hwnd, vsFileName); 
     } 
     else 
     { 
      MessageBox(hwnd, vsFileName, "Missing Shader File", MB_OK); 
     } 

     return false; 
    } 

    result = D3DX11CompileFromFile(psFileName, NULL, NULL, "TerrainPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, NULL, 
     &pixelShaderBuffer, &errorMessage, NULL); 
    if (FAILED(result)) 
    { 
     if (errorMessage) 
     { 
      OutputShaderErrorMessage(errorMessage, hwnd, psFileName); 
     } 
     else 
     { 
      MessageBox(hwnd, psFileName, "Missing Shader File", MB_OK); 
     } 

     return false; 
    } 

    result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, 
     &m_vertexShader); 
    if (FAILED(result)) 
    { 
     return false; 
    } 

    result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, 
     &m_pixelShader); 
    if (FAILED(result)) 
    { 
     return false; 
    } 

    // This setup needs to match the VertexType stucture in the ModelClass and in the shader. 
    polygonLayout[0].SemanticName = "POSITION"; 
    polygonLayout[0].SemanticIndex = 0; 
    polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT; 
    polygonLayout[0].InputSlot = 0; 
    polygonLayout[0].AlignedByteOffset = 0; 
    polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; 
    polygonLayout[0].InstanceDataStepRate = 0; 

    polygonLayout[1].SemanticName = "TEXCOORD"; 
    polygonLayout[1].SemanticIndex = 0; 
    polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT; 
    polygonLayout[1].InputSlot = 0; 
    polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; 
    polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; 
    polygonLayout[1].InstanceDataStepRate = 0; 

    polygonLayout[2].SemanticName = "NORMAL"; 
    polygonLayout[2].SemanticIndex = 0; 
    polygonLayout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT; 
    polygonLayout[2].InputSlot = 0; 
    polygonLayout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; 
    polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; 
    polygonLayout[2].InstanceDataStepRate = 0; 

    polygonLayout[3].SemanticName = "COLOR"; 
    polygonLayout[3].SemanticIndex = 0; 
    polygonLayout[3].Format = DXGI_FORMAT_R32G32B32_FLOAT; 
    polygonLayout[3].InputSlot = 0; 
    polygonLayout[3].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; 
    polygonLayout[3].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; 
    polygonLayout[3].InstanceDataStepRate = 0; 

    numElements = sizeof(polygonLayout)/sizeof(polygonLayout[0]); 

    result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), 
     vertexShaderBuffer->GetBufferSize(), &m_layout); 

    if (FAILED(result)) 
    { 
     return false; 
    } 

    vertexShaderBuffer->Release(); 
    vertexShaderBuffer = nullptr; 

    pixelShaderBuffer->Release(); 
    pixelShaderBuffer = nullptr; 
//Continues 

コードはpolygonLayout[3].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;に到達すると、バッファが正常に設定されています。しかし、その行(polygonLayout[3].InstanceDataStepRate = 0;)後の値は、例外をスロー

result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), 
      vertexShaderBuffer->GetBufferSize(), &m_layout); 

ラインで、無appearant理由nullを行きます。

オンラインで検索しましたが、結果はありませんでした。どんな助けでも大歓迎です。前もって感謝します。

答えて

3

polygonLayoutアレイには3つの項目しか含まれていないため、polygonLayout[3]を入力するとバッファオーバーランが発生し、未定義の動作になります(スタックに格納されている他の変数が破壊される可能性があります)。 1)4つのアイテムを含むようにすることは良い考えです。

::std::array<D3D11_INPUT_ELEMENT_DESC, 4> polygonLayout; 
+0

それだ:2)(デバッグ)インデックスチェックして配列のラッパーを使用!ありがとう!それは私の愚かな間違いでした。 – aPlutonicCoder