createInputLayout返回false

时间:2015-02-17 04:54:35

标签: c++ directx-11

最近我一直在关注rasertek的DirectX编程,在我的代码中,createinputlayout()函数不起作用。我认为我的源代码具有相同的代码,但createinputlayout不断返回false

textureShaderClass.cpp

#include "textureshaderclass.h"

TextureShaderClass::TextureShaderClass() : m_vertexShader(0), m_pixelShader(0), m_layout(0), m_matrixBuffer(0), m_sampleState(0)
{  }

TextureShaderClass::TextureShaderClass(const TextureShaderClass&)
{  }

TextureShaderClass::~TextureShaderClass()
{  }

bool TextureShaderClass::init(ID3D11Device* device, HWND hwnd)
{
    bool result;

    result = initializeShader(device, hwnd, L"../Engine/texture.vs", L"../Engine/texture.ps");
    if (!result)
    {
        return false;
    }

    return true;
}

void TextureShaderClass::shutdown()
{
    shutdownShader();
}

bool TextureShaderClass::render(ID3D11DeviceContext* deviceContext, int indexCount, D3DXMATRIX wordlMatrix, D3DXMATRIX viewMatrix, D3DXMATRIX projectionMatrix, ID3D11ShaderResourceView* texture)
{
    if (!(setShaderParameters(deviceContext, wordlMatrix, viewMatrix, projectionMatrix, texture)))
    {
        return false;
    }

    renderShader(deviceContext, indexCount);

    return true;
}

bool TextureShaderClass::initializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
{
    ID3D10Blob* errorMessage        = 0,
                *vertexShaderBuffer = 0,
                *pixelShaderBuffer  = 0;
    D3D11_INPUT_ELEMENT_DESC polygonLayout[2];
    unsigned int numElements;
    D3D11_BUFFER_DESC matrixBufferDesc;
    D3D11_SAMPLER_DESC samplerDesc;

    if (FAILED(D3DX11CompileFromFileW(vsFilename, 0, 0, "Texture Vertex Shader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, 0, &vertexShaderBuffer, &errorMessage, 0)))
    {
        if (errorMessage)
            outputShaderErrorMessage(errorMessage, hwnd, vsFilename);
        else
            MessageBoxW(hwnd, vsFilename, L"Missing Shader File", MB_OK);
        return false;
    }

    if (FAILED(D3DX11CompileFromFileW(psFilename, 0, 0, "Texture Pixel Shader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, 0, &pixelShaderBuffer, &errorMessage, 0)))
    {
        if (errorMessage)
            outputShaderErrorMessage(errorMessage, hwnd, psFilename);
        else
            MessageBoxW(hwnd, psFilename, L"Missing Shader File", MB_OK);
        return false;
    }

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

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

    polygonLayout[0].SemanticName = "POSITION";
    polygonLayout[0].SemanticIndex = 0;
    polygonLayout[0].Format = DXGI_FORMAT_R32G32B32A32_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;

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

    if (FAILED(device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), &m_layout)))
    {
        return false;
    }

    m_vertexShader->Release();
    m_vertexShader = 0;

    m_pixelShader->Release();
    m_pixelShader = 0;

    matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
    matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
    matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
    matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
    matrixBufferDesc.MiscFlags = 0;
    matrixBufferDesc.StructureByteStride = 0;

    if (FAILED(device->CreateBuffer(&matrixBufferDesc, 0, &m_matrixBuffer)))
    {
        return false;
    }

    samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
    samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
    samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
    samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
    samplerDesc.MipLODBias = 0.0f;
    samplerDesc.MaxAnisotropy = 1;
    samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
    samplerDesc.BorderColor[0] = 0;
    samplerDesc.BorderColor[1] = 0;
    samplerDesc.BorderColor[2] = 0;
    samplerDesc.BorderColor[3] = 0;
    samplerDesc.MinLOD = 0;
    samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;

    if (FAILED(device->CreateSamplerState(&samplerDesc, &m_sampleState)))
    {
        return false;
    }

    return true;
}

void TextureShaderClass::shutdownShader()
{
    if (m_vertexShader)
    {
        m_vertexShader->Release();
        m_vertexShader = 0;
    }

    if (m_pixelShader)
    {
        m_pixelShader->Release();
        m_pixelShader = 0;
    }

    if (m_layout)
    {
        m_layout->Release();
        m_layout = 0;
    }

    if (m_matrixBuffer)
    {
        m_matrixBuffer->Release();
        m_matrixBuffer = 0;
    }

    if (m_sampleState)
    {
        m_sampleState->Release();
        m_sampleState = 0;
    }
}

void TextureShaderClass::outputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* fileLocation)
{
    char* compileErrors = static_cast<char*>(errorMessage->GetBufferPointer());;
    unsigned long bufferSize = errorMessage->GetBufferSize();
    std::ofstream fout;
    fout.open("shader-errora.txt");

    for (unsigned long i = 0; i < bufferSize; ++i)
    {
        fout << compileErrors[i];
    }

    fout.close();

    errorMessage->Release();
    errorMessage = 0;

    MessageBoxW(hwnd, L"Error compiling shader check shader-errora.txt for more information.", fileLocation, MB_OK);
}

bool TextureShaderClass::setShaderParameters(ID3D11DeviceContext* deviceContext, D3DXMATRIX worldMatrix, D3DXMATRIX viewMatrix, D3DXMATRIX projectionMatrix, ID3D11ShaderResourceView* texture)
{
    D3D11_MAPPED_SUBRESOURCE mappedResource;
    MatrixBufferType* dataPtr;
    unsigned int bufferNumber;

    D3DXMatrixTranspose(&worldMatrix, &worldMatrix);
    D3DXMatrixTranspose(&viewMatrix,  &viewMatrix);
    D3DXMatrixTranspose(&projectionMatrix, &projectionMatrix);

    if (FAILED(deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource)))
    {
        return false;
    }

    dataPtr = static_cast<MatrixBufferType*>(mappedResource.pData);

    dataPtr->world      = worldMatrix;
    dataPtr->view       = viewMatrix;
    dataPtr->projection = projectionMatrix;

    deviceContext->Unmap(m_matrixBuffer, 0);

    bufferNumber = 0;

    deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);

    deviceContext->PSSetShaderResources(0, 1, &texture);

    return true;
}   

void TextureShaderClass::renderShader(ID3D11DeviceContext* deviceContext, int indexCount)
{
    deviceContext->IASetInputLayout(m_layout);

    deviceContext->VSSetShader(m_vertexShader, 0, 0);
    deviceContext->PSSetShader(m_pixelShader, 0, 0);

    deviceContext->PSSetSamplers(0,1, &m_sampleState);

    deviceContext->DrawIndexed(indexCount, 0, 0);
}

textureShaderClass.h

#ifndef TEXTURESHADERCLASS_H
#define TEXTURESHADERCLASS_H

#include <d3d11.h>
#include <d3dx10math.h>
#include <D3DX11async.h>
#include <fstream>

class TextureShaderClass
{
private:
    struct MatrixBufferType
    {
        D3DXMATRIX world;
        D3DXMATRIX view;
        D3DXMATRIX projection;
    };

public:
    TextureShaderClass();
    TextureShaderClass(const TextureShaderClass&);
    ~TextureShaderClass();

    bool init(ID3D11Device*, HWND);
    void shutdown();
    bool render(ID3D11DeviceContext*, int, D3DXMATRIX, D3DXMATRIX, D3DXMATRIX, ID3D11ShaderResourceView*);

private:
    bool initializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*);
    void shutdownShader();
    void outputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);

    bool setShaderParameters(ID3D11DeviceContext*, D3DXMATRIX, D3DXMATRIX, D3DXMATRIX, ID3D11ShaderResourceView*);
    void renderShader(ID3D11DeviceContext*, int);

private:
    ID3D11VertexShader* m_vertexShader;
    ID3D11PixelShader*  m_pixelShader;
    ID3D11InputLayout*  m_layout;
    ID3D11Buffer*       m_matrixBuffer;
    ID3D11SamplerState* m_sampleState;
};

#endif

1 个答案:

答案 0 :(得分:3)

if ($_SERVER['REQUEST_METHOD'] == 'POST')

您正在使用另一个DXGI_FORMAT枚举减去DXGI_FORMAT类型的垃圾填充格式变量,从而产生一个整数值,该值不会随后存储在任何位置。这里:

polygonLayout[1].Format - DXGI_FORMAT_R32G32_FLOAT;

:)