创建顶点缓冲区错误
Error creating vertex buffer
创建vertexbuffer时出现错误下面是我的代码:
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之类的,这样就可以了。
如果你的卡不支持Direct3D 11功能,程序会在设备初始化失败,而不是着色器创建,所以如果你用D3D_FEATURE_LEVEL_11_0
初始化你的设备,它成功了,这意味着你的问题在于着色器代码的某处。这是很有可能的,因为智能感知不能与HLSL一起工作。
幸运的是,你的框架有一些错误报告功能,所以你可以简单地查看shader_errors.txt文件,看看你的问题在哪里。
如果上述情况并非如此(即您已经更改了Direct3D功能级别),那么您有几个选项:
-
改变你的着色器模型,以匹配Direct3D特征水平在
D3DX11CompileFromFile()
方法,例如,如果你使用D3D_FEATURE_LEVEL_10_0
设置你的着色器模型为vs_4_0
和ps_4_0
。 -
如果你使用的是Windows 8(在不支持DX11的机器上不太可能),那么你可以在设备初始化中使用
D3D_DRIVER_TYPE_WARP
,这是一个快速的软件光栅化器,但在旧版本的Windows上它不支持11_0功能级别。 -
作为最后的手段,你可以使用
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
}
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- 在 leetcode 上提交解决方案时出现堆栈缓冲区溢出错误
- 使用 strcat 获取缓冲区溢出错误
- 将缓冲区复制到剪贴板 [换行错误]
- openCL-创建子缓冲区返回错误代码13
- 与Qt交叉编译到Raspberry Pi 3B+通讯录(协议缓冲区)-错误符号查找错误
- 应该如何读取堆缓冲区溢出错误消息?
- 为什么我遇到缓冲区错误?
- 错误:使用未声明的标识符"缓冲区"C++
- 导致堆缓冲区错误的动态分配数组的析构函数
- OpenGL 顶点缓冲区类重定义和模板方法错误
- 带有模板的循环缓冲区在Keil MDK5上是模棱两可的错误?
- 协议缓冲区错误版本
- 我正在尝试使用回溯来解决 N queen 问题,但在编译时它会给出运行时错误(动态堆栈缓冲区溢出)
- 可能的 ComputeCPP SYCL 错误读取嵌套缓冲区
- 为什么 LeetCode 给出错误:地址清理器:堆缓冲区溢出
- 为什么错误 C6386 缓冲区溢出与 strsafe.h 字符串 Cch 函数
- C++缓冲区太小错误
- coreValidation-drawState-Invalidimagelayout错误上的命令缓冲区错误
- Cython缓冲区协议示例错误