Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ createInputLayout返回false_C++_Directx 11 - Fatal编程技术网

C++ createInputLayout返回false

C++ createInputLayout返回false,c++,directx-11,C++,Directx 11,最近我一直在关注rasertek的DirectX编程,在我的代码中,createinputlayout()函数不起作用。我想我得到的代码与源代码相同,但是createinputlayout不断返回false textureShaderClass.cpp #include "textureshaderclass.h" TextureShaderClass::TextureShaderClass() : m_vertexShader(0), m_pixelShader(0), m_layout(0

最近我一直在关注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”
TextureShaderClass::TextureShaderClass():m_vertexShader(0)、m_pixelShader(0)、m_布局(0)、m_matrixBuffer(0)、m_采样状态(0)
{  }
TextureShareClass::TextureShareClass(常量TextureShareClass&)
{  }
TextureShaderClass::~TextureShaderClass()
{  }
bool TextureShaderClass::init(ID3D11Device*device,HWND-HWND)
{
布尔结果;
结果=初始化标头(设备,hwnd,L“./引擎/纹理.vs”,L“./引擎/纹理.ps”);
如果(!结果)
{
返回false;
}
返回true;
}
void TextureShaderClass::shutdown()
{
关闭下遮板();
}
bool TextureShaderClass::render(ID3D11DeviceContext*deviceContext,int indexCount,D3DXMATRIX WordMatrix,D3DXMATRIX viewMatrix,D3DXMATRIX projectionMatrix,ID3D11ShaderResourceView*纹理)
{
if(!(设置ShaderParameters(deviceContext、WordMatrix、viewMatrix、projectionMatrix、texture)))
{
返回false;
}
renderShader(deviceContext、indexCount);
返回true;
}
bool TextureShaderClass::initializeShader(ID3D11Device*device,HWND-HWND,WCHAR*vsFilename,WCHAR*psFilename)
{
ID3D10Blob*errorMessage=0,
*vertexShaderBuffer=0,
*pixelShaderBuffer=0;
D3D11输入元素描述多边形布局[2];
无符号整数;
D3D11_BUFFER_DESC matrixBufferDesc;
D3D11_取样器_DESC取样器DESC;
如果(失败(D3DX11CompileFromFileW(vsFilename,0,0,“纹理顶点着色器”,“vs_5_0”,“D3D10_着色器”\u启用严格性,0,0和vertexShaderBuffer,&errorMessage,0)))
{
如果(错误消息)
outputShaderErrorMessage(errorMessage、hwnd、vsFilename);
其他的
MessageBoxW(hwnd,vsFilename,L“缺少着色器文件”,MB_OK);
返回false;
}
如果(失败(D3DX11CompileFromFileW(psFilename,0,0,“纹理像素着色器”,“ps_5_0”,D3D10_着色器_启用_严格性,0,0,&pixelShaderBuffer,&errorMessage,0)))
{
如果(错误消息)
outputShaderErrorMessage(errorMessage、hwnd、psFilename);
其他的
MessageBoxW(hwnd,psFilename,L“缺少着色器文件”,MB_OK);
返回false;
}
如果(失败(设备->创建vertexShader(vertexShaderBuffer->GetBufferPointer(),vertexShaderBuffer->GetBufferSize(),0,&m_vertexShader)))
{
返回false;
}
如果(失败(设备->创建pixelShader(pixelShaderBuffer->GetBufferPointer(),pixelShaderBuffer->GetBufferSize(),0,&m_pixelShader)))
{
返回false;
}
polygonLayout[0]。SemanticName=“位置”;
polygonLayout[0]。SemanticIndex=0;
polygonLayout[0]。格式=DXGI_格式_r32g32b32a2a32_浮点;
polygonLayout[0]。InputSlot=0;
polygonLayout[0]。AlignedByteOffset=0;
polygonLayout[0]。InputSlotClass=D3D11\u每个顶点的输入数据;
polygonLayout[0]。InstanceDataStepRate=0;
polygonLayout[1]。SemanticName=“TEXCOORD”;
polygonLayout[1]。SemanticIndex=0;
polygonLayout[1]。格式-DXGI_Format_R32G32_FLOAT;
polygonLayout[1]。InputSlot=0;
polygonLayout[1]。AlignedByteOffset=D3D11\u APPEND\u ALIGNED\u ELEMENT;
polygonLayout[1]。InputSlotClass=D3D11\u每个顶点的输入数据;
polygonLayout[1]。InstanceDataStepRate=0;
numElements=sizeof(polygonLayout)/sizeof(polygonLayout[0]);
如果(失败(设备->CreateInputLayout(polygonLayout、NumeElements、vertexShaderBuffer->GetBufferPointer()、vertexShaderBuffer->GetBufferSize(),&m_布局)))
{
返回false;
}
m_vertexShader->Release();
m_vertexShader=0;
m_pixelShader->Release();
m_pixelShader=0;
matrixBufferDesc.Usage=D3D11\u Usage\u DYNAMIC;
matrixBufferDesc.ByteWidth=sizeof(MatrixBufferType);
matrixBufferDesc.BindFlags=D3D11\u绑定\u常量\u缓冲区;
matrixBufferDesc.CPUAccessFlags=D3D11\u CPU\u访问\u写入;
matrixBufferDesc.MiscFlags=0;
matrixBufferDesc.StructureByteStride=0;
if(失败(设备->创建缓冲区(&matrixBufferDesc,0,&m_matrixBuffer)))
{
返回false;
}
samplerDesc.Filter=D3D11_Filter_MIN_MAG_MIP_LINEAR;
sampledesc.AddressU=D3D11\u纹理\u地址\u包裹;
sampledesc.AddressV=D3D11\u纹理\u地址\u包裹;
samplerDesc.AddressW=D3D11\u纹理\u地址\u包裹;
sampledesc.MipLODBias=0.0f;
samplerDesc.Max各向异性=1;
samplerDesc.ComparisonFunc=D3D11\u比较\u始终;
SamplerDescr.BorderColor[0]=0;
SamplerDescr.BorderColor[1]=0;
SamplerDescr.BorderColor[2]=0;
SamplerDescr.BorderColor[3]=0;
samplerDesc.MinLOD=0;
samplerDesc.MaxLOD=D3D11_FLOAT32_MAX;
if(失败(设备->CreateSamplerState(&SampleDescr,&m_sampleState)))
{
返回false;
}
返回true;
}
void TextureShaderClass::shutdownShader()
{
if(m_vertexShader)
{
m_vertexShader->Release();
m_vertexShader=0;
}
if(m_像素着色器)
{
m_pixelShader->Release();
m_pixelShader=0;
}
if(m_布局)
{
m_布局->发布();
m_布局=0;
}
if(m_matrixBuffer)
{
m_matrixBuffer->Release();
m_matrixBuffer=0;
}
if(m_样本状态)
{
m_sampleState->Release();
m_sampleState=0;
}
}
void TextureShaderClass::outputShaderErrorMessage(ID3D10Blob*errorMessage、HWND HWND、WCHAR*fileLocation)
{
char*compileErrors=static_cast(errorMessage->GetBufferPointer());;
unsigned long bufferSize=errorMessage->GetBufferSize();
标准:流式流量计;
fout.open(“shader errora.txt”);
for(无符号长i=0;i#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
polygonLayout[1].Format - DXGI_FORMAT_R32G32_FLOAT;
polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;