【问题标题】:Error creating vertex buffer创建顶点缓冲区时出错
【发布时间】:2014-03-13 09:19:42
【问题描述】:

创建顶点缓冲区时出错 这是我的代码:

bool ColorShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
{



HRESULT result;
    ID3D10Blob* errorMessage;
    ID3D10Blob* vertexShaderBuffer;
    ID3D10Blob* pixelShaderBuffer;
    D3D11_INPUT_ELEMENT_DESC polygonLayout[2];
    unsigned int numElements;
    D3D11_BUFFER_DESC matrixBufferDesc;


// Initialize the pointers this function will use to null.
errorMessage = 0;
vertexShaderBuffer = 0;
pixelShaderBuffer = 0;

// Compile the vertex shader code.
result = D3DX11CompileFromFile(vsFilename, NULL, NULL, "ColorVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, NULL, 
                               &vertexShaderBuffer, &errorMessage, NULL);
if(FAILED(result))
{
    // If the shader failed to compile it should have writen something to the error message.
    if(errorMessage)
    {
        OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
    }
    // If there was  nothing in the error message then it simply could not find the shader file itself.
    else
    {
        MessageBox(hwnd, vsFilename, L"Missing Shader File", MB_OK);
    }

    return false;
}

// Compile the pixel shader code.
result = D3DX11CompileFromFile(psFilename, NULL, NULL, "ColorPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, NULL, 
                               &pixelShaderBuffer, &errorMessage, NULL);
if(FAILED(result))
{
    // If the shader failed to compile it should have writen something to the error message.
    if(errorMessage)
    {
        OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
    }
    // If there was nothing in the error message then it simply could not find the file itself.
    else
    {
        MessageBox(hwnd, psFilename, L"Missing Shader File", MB_OK);
    }

    return false;
}

// Create the vertex shader from the buffer.
result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL,
    &m_vertexShader);
if(FAILED(result))
{
    return false;
}

// Create the pixel shader from the buffer.
result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL,
    &m_pixelShader);
if(FAILED(result))
{
    return false;
}

// Create the vertex input layout description.
// 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 = "COLOR";
polygonLayout[1].SemanticIndex = 0;
polygonLayout[1].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
polygonLayout[1].InputSlot = 0;
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
polygonLayout[1].InstanceDataStepRate = 0;

// Get a count of the elements in the layout.
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);

// Create the vertex input layout.
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), 
                                   vertexShaderBuffer->GetBufferSize(), &m_layout);
if(FAILED(result))
{
    return false;
}

// Release the vertex shader buffer and pixel shader buffer since they are no longer needed.
vertexShaderBuffer->Release();
vertexShaderBuffer = 0;

pixelShaderBuffer->Release();
pixelShaderBuffer = 0;

// Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
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;

// Create the constant buffer pointer so we can access the vertex shader constant buffer from within this class.
result = device->CreateBuffer(&matrixBufferDesc, NULL, &m_matrixBuffer);
if(FAILED(result))
{
    return false;
}

return true;
}

顺便说一句,我从教程http://www.rastertek.com/dx11tut04.html 开始。你可以在那里看到漏洞代码。 我听说问题可能是,我的显卡不支持 DirectX 11。如果是这个问题,我可以使用功能级别 11 或类似的东西进行软件顶点处理,这样就可以了。

【问题讨论】:

  • 好的,我认为是硬件问题。所以问题仍然存在,我可以在笔记本上做这些东西吗?哪些硬件不支持 DirectX 11?
  • 至少你应该告诉我们你遇到了什么错误。

标签: c++ directx directx-11 vertex-buffer


【解决方案1】:

如果您的卡不支持 Direct3D 11 功能,该程序将在设备初始化而不是着色器创建时失败,因此如果您使用 D3D_FEATURE_LEVEL_11_0 初始化设备并且它成功,这意味着您的问题出在着色器的某个地方代码。这很可能是因为 IntelliSense 不适用于 HLSL。

幸运的是,您的框架带有一些错误报告功能,因此您只需查看 shader_errors.txt 文件即可了解问题所在。

如果不是上述情况(即您已经更改了 Direct3D 功能级别),那么您有多种选择:

  1. 更改着色器模型以匹配 D3DX11CompileFromFile() 方法中的 Direct3D 功能级别,例如如果您使用D3D_FEATURE_LEVEL_10_0,请将您的着色器模型设置为vs_4_0ps_4_0

  2. 如果您使用的是 Windows 8(在不支持 DX11 的机器上不太可能),那么您可以在设备初始化中使用 D3D_DRIVER_TYPE_WARP,这是一个快速的软件光栅化程序,但在旧版本的 Windows 上它不支持 11_0 功能级别。

  3. 作为最后的手段,您可以使用D3D_DRIVER_TYPE_REFERENCE,它支持所有 Direct3D 功能,但速度极慢,甚至无法使用。

最终,最好有某种形式的后备选项,以防您运行的硬件不支持您想要的功能级别。例如:

D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0;

HRESULT result = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, D3D11_CREATE_DEVICE_DEBUG, &featureLevel, 1, D3D11_SDK_VERSION, &swapChainDesc, &m_swapChain, &m_device, NULL, &m_deviceContext);
if (FAILED(result))
{
    featureLevel = D3D_FEATURE_LEVEL_10_0;
    result = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, D3D11_CREATE_DEVICE_DEBUG, &featureLevel, 1, D3D11_SDK_VERSION, &swapChainDesc, &m_swapChain, &m_device, NULL, &m_deviceContext);
    if (FAILED(result))
    {
        // OK, now quit, we need at least DirectX 10 compatible hardware
        return false;
    }
}

// In shader class
if (m_device->GetFeatureLevel() == D3D_FEATURE_LEVEL_11_0)
{
    // use 5.0 shader model
}
else
{
    // use 4.0 shader model
}

【讨论】:

  • 非常感谢您的回答,对于不清楚的问题,我很抱歉。我现在再解释一下。问题是我可以做些什么来让 Directx11 在不支持它的硬件上工作。我在另一台计算机上运行我的程序,但那台计算机支持 Directx11,所以它在那里工作。这个程序一直工作到着色器因为我将功能级别更改为directx 9。现在我将检查它是否适用于directX10。并且...是的,谢谢。
  • 我记得您可以启用硬件抽象层来模拟软件缺少的任何硬件以进行测试。从我的笔记中可以看出,您将 DirectX 设置为使用“参考设备”类型,并且在创建设备时在第二个参数中设置。我知道 DirectX 9 是这样,至少如果你还有文件等等,请查看它,因为它已经有一段时间了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多