c++ - 输入布局,访问冲突,错误处理无法正常运行

标签 c++ debugging error-handling directx-11

我正在学习DirectX 11,我想深入了解DirectX调试,因为在第199行(创建inputLayout)时遇到读取访问错误的访问冲突
我试图显示一个显示DirectX错误的错误框,因为我在某处读到,让该框显示有关错误的信息是一种很好的编程习惯
有任何想法吗?
此外,与输入布局的帮助将不胜感激

ID3DBlob *VS, *PS;

#if defined(DEBUG) | defined(_DEBUG)
#ifndef HR
#define HR(x) \
{ \
HRESULT hr = (x); \
if(FAILED(hr)) \
{ \
DXTrace(__FILE__, (DWORD)__LINE__, hr, L#x, true); \
} \
}
#endif
#else
#ifndef HR
#define HR(x) (x)
#endif
#endif

D3DX11CompileFromFile(L"shaders.fx", 0, 0, "VS", "vs_5_0", 0, 0, 0, &VS, 0, 0);
D3DX11CompileFromFile(L"shaders.fx", 0, 0, "PS", "ps_5_0", 0, 0, 0, &PS, 0, 0);

device->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &vShader);
device->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pShader);

VS->Release();
PS->Release();
context->VSSetShader(vShader, 0, 0);
context->PSSetShader(pShader, 0, 0);


// define the input layout
D3D11_INPUT_ELEMENT_DESC layout[] =
  {
    { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
};


UINT numElements = ARRAYSIZE(layout);

//below gives me access violation error and says that &inputLayout is NULL 
HR(device->CreateInputLayout(layout, numElements, VS->GetBufferPointer(), VS->GetBufferSize(), &inputLayout));

最佳答案

根据上面的代码创建布局之前,您要释放VS blob。创建输入布局时,您需要原始的Vertex Shader二进制blob,以便Direct3D可以验证它们是否匹配。

一个简单的解决方法是在调用VS->Release();之后将CreateInputLayout移至。

一个更好的答案是删除所有对Release的显式使用,而是依赖于像Microsoft::WRL::ComPtr这样的智能指针。

#include <wrl/client.h>

using Microsoft::WRL::ComPtr;

...

ComPtr<ID3DBlob> VS, PS;

...

D3DX11CompileFromFile(L"shaders.fx", 0, 0, "VS", "vs_5_0", 0, 0, 0, &VS, 0, 0);
D3DX11CompileFromFile(L"shaders.fx", 0, 0, "PS", "ps_5_0", 0, 0, 0, &PS, 0, 0);

device->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &vShader);
device->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pShader);

context->VSSetShader(vShader, 0, 0);
context->PSSetShader(pShader, 0, 0);

...

//below gives me access violation error and says that &inputLayout is NULL 
HR(device->CreateInputLayout(layout, numElements, VS->GetBufferPointer(), VS->GetBufferSize()));

每当VSPS超出范围时,它们都会清除自身。

关于c++ - 输入布局,访问冲突,错误处理无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33700381/

相关文章:

java - 在 Eclipse IDE 中调试 micronaut 应用程序

python - 将 Python 错误写入 txt 文件

python - django.template.exceptions.TemplateSyntaxError : Could not parse some characters: |{{b.count}}||rangef

c++ - 表示盘中时间的最佳方式是什么 HH :MM:SS in c++11

c++ - 从 PSD 文件构建层复合

c++ - Typedef 和函数声明不能​​一起工作

c++ - std vector 中的 push_back() 崩溃

java - 为什么我的 Eclipse Helios 调试在单步执行 Path.register(WatchService, Kind<?>...) 时会出现?

javascript - 多个图像 slider 加载图像错误

python - 使用python脚本将文件内容复制到另一个文件