哪个头文件包含 DirectX 12 中的 ThrowIfFailed()
which header file contains ThrowIfFailed() in DirectX 12
some part of code image, another part of code image, 我是 DirectX 12(或游戏编程)的初学者,正在学习 Microsoft 文档。使用函数 ThrowIfFailed()
时,我从 vs2015 编辑器
的智能感知中收到错误
This declaration has no storage class or type specifier.
谁能帮忙。
这个错误是因为你的一些代码在任何函数之外。
你的错误就在这里:
void D3D12HelloTriangle::LoadPipeline() {
#if defined(_DEBUG) { //<= this brack is simply ignore because on a pre-processor line
ComPtr<ID3D12Debug> debugController;
if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController)))) {
debugController->EnableDebugLayer();
}
} // So, this one closes method LoadPipeline
#endif
// From here, you are out of any function
ComPtr<IDXGIFactory4> factory;
ThrowIfFailed(CreateDXGIFactory1(IID_PPV_ARGS(&factory)));
所以更正它:
void D3D12HelloTriangle::LoadPipeline() {
#if defined(_DEBUG)
{ //<= just put this bracket on it's own line
ComPtr<ID3D12Debug> debugController;
if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController)))) {
debugController->EnableDebugLayer();
}
} // So, this one close method LoadPipeline
#endif
// From here, you are out of any function
ComPtr<IDXGIFactory4> factory;
ThrowIfFailed(CreateDXGIFactory1(IID_PPV_ARGS(&factory)));
As you are new to DirectX programming, I strongly recommend starting with DirectX 11 rather than DirectX 12. DirectX 12 assumes you are already an expert DirectX 11 developer, and is a quite unforgiving API. It's absolutely worth learning if you plan to be a graphics developer, but starting with DX 12 over DX 11 is a huge undertaking. See the DirectX Tool Kit tutorials for DX11 and/or DX12
对于现代 DirectX 示例代码和 VS DirectX 模板,Microsoft 使用标准辅助函数 ThrowIfFailed
。它不是 OS 或系统 headers 的一部分;它只是在本地项目的预编译 Header 文件中定义 (pch.h
):
#include <exception>
namespace DX
{
inline void ThrowIfFailed(HRESULT hr)
{
if (FAILED(hr))
{
// Set a breakpoint on this line to catch DirectX API errors
throw std::exception();
}
}
}
对于 COM 编程,您必须在运行时检查所有 HRESULT
值是否失败。如果可以安全地忽略特定 DirectX 11 或 DirectX 12 API 的 return 值,它将改为 return void
。您通常将 ThrowIfFailed
用于 'fast fail' 场景(即,如果函数失败,您的程序将无法恢复)。
Note the recommendation is to use C++ Exception Handling (a.k.a. /EHsc
) which is the default compiler setting in the VS templates. On the x64 and ARM platforms, this is implemented very efficiently without any additional code overhead. Legacy x86 requires some additional epilog/prologue code that the compiler creates. Most of the "FUD" around exception handling in native code is based on the experience of using the older Asynchronous Structured Exception Handling (a.k.a. /EHa
) which severely hampers the code optimizer.
参见 this wiki page for a bit more detail and usage information. You should also read the page on ComPtr。
在我 GitHub 上的 Direct3D 游戏 VS 模板版本中,我使用了稍微增强的 ThrowIfFailed
版本,您也可以使用它:
#include <exception>
namespace DX
{
// Helper class for COM exceptions
class com_exception : public std::exception
{
public:
com_exception(HRESULT hr) : result(hr) {}
virtual const char* what() const override
{
static char s_str[64] = {};
sprintf_s(s_str, "Failure with HRESULT of %08X",
static_cast<unsigned int>(result));
return s_str;
}
private:
HRESULT result;
};
// Helper utility converts D3D API failures into exceptions.
inline void ThrowIfFailed(HRESULT hr)
{
if (FAILED(hr))
{
throw com_exception(hr);
}
}
}
some part of code image, another part of code image, 我是 DirectX 12(或游戏编程)的初学者,正在学习 Microsoft 文档。使用函数 ThrowIfFailed()
时,我从 vs2015 编辑器
This declaration has no storage class or type specifier.
谁能帮忙。
这个错误是因为你的一些代码在任何函数之外。
你的错误就在这里:
void D3D12HelloTriangle::LoadPipeline() {
#if defined(_DEBUG) { //<= this brack is simply ignore because on a pre-processor line
ComPtr<ID3D12Debug> debugController;
if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController)))) {
debugController->EnableDebugLayer();
}
} // So, this one closes method LoadPipeline
#endif
// From here, you are out of any function
ComPtr<IDXGIFactory4> factory;
ThrowIfFailed(CreateDXGIFactory1(IID_PPV_ARGS(&factory)));
所以更正它:
void D3D12HelloTriangle::LoadPipeline() {
#if defined(_DEBUG)
{ //<= just put this bracket on it's own line
ComPtr<ID3D12Debug> debugController;
if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController)))) {
debugController->EnableDebugLayer();
}
} // So, this one close method LoadPipeline
#endif
// From here, you are out of any function
ComPtr<IDXGIFactory4> factory;
ThrowIfFailed(CreateDXGIFactory1(IID_PPV_ARGS(&factory)));
As you are new to DirectX programming, I strongly recommend starting with DirectX 11 rather than DirectX 12. DirectX 12 assumes you are already an expert DirectX 11 developer, and is a quite unforgiving API. It's absolutely worth learning if you plan to be a graphics developer, but starting with DX 12 over DX 11 is a huge undertaking. See the DirectX Tool Kit tutorials for DX11 and/or DX12
对于现代 DirectX 示例代码和 VS DirectX 模板,Microsoft 使用标准辅助函数 ThrowIfFailed
。它不是 OS 或系统 headers 的一部分;它只是在本地项目的预编译 Header 文件中定义 (pch.h
):
#include <exception>
namespace DX
{
inline void ThrowIfFailed(HRESULT hr)
{
if (FAILED(hr))
{
// Set a breakpoint on this line to catch DirectX API errors
throw std::exception();
}
}
}
对于 COM 编程,您必须在运行时检查所有 HRESULT
值是否失败。如果可以安全地忽略特定 DirectX 11 或 DirectX 12 API 的 return 值,它将改为 return void
。您通常将 ThrowIfFailed
用于 'fast fail' 场景(即,如果函数失败,您的程序将无法恢复)。
Note the recommendation is to use C++ Exception Handling (a.k.a.
/EHsc
) which is the default compiler setting in the VS templates. On the x64 and ARM platforms, this is implemented very efficiently without any additional code overhead. Legacy x86 requires some additional epilog/prologue code that the compiler creates. Most of the "FUD" around exception handling in native code is based on the experience of using the older Asynchronous Structured Exception Handling (a.k.a./EHa
) which severely hampers the code optimizer.
参见 this wiki page for a bit more detail and usage information. You should also read the page on ComPtr。
在我 GitHub 上的 Direct3D 游戏 VS 模板版本中,我使用了稍微增强的 ThrowIfFailed
版本,您也可以使用它:
#include <exception>
namespace DX
{
// Helper class for COM exceptions
class com_exception : public std::exception
{
public:
com_exception(HRESULT hr) : result(hr) {}
virtual const char* what() const override
{
static char s_str[64] = {};
sprintf_s(s_str, "Failure with HRESULT of %08X",
static_cast<unsigned int>(result));
return s_str;
}
private:
HRESULT result;
};
// Helper utility converts D3D API failures into exceptions.
inline void ThrowIfFailed(HRESULT hr)
{
if (FAILED(hr))
{
throw com_exception(hr);
}
}
}