DirectX 12中哪个头文件包含ThrowIfFailed() [英] which header file contains ThrowIfFailed() in DirectX 12

查看:362
本文介绍了DirectX 12中哪个头文件包含ThrowIfFailed()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

部分代码图像,

some part of code image, another part of code image, I am beginner to DirectX 12 (or Game Programming) and studying from Microsoft documentation. While using function ThrowIfFailed() I get an error from intellisense of vs2015 editor

此声明没有存储类或类型说明符.

This declaration has no storage class or type specifier.

任何人都可以帮忙.

推荐答案

由于您是DirectX编程的新手,因此我强烈建议从DirectX 11而不是DirectX 12开始. DirectX 12假定您已经是DirectX 11的专业开发人员,并且是一个非常令人无法忍受的API.如果您打算成为图形开发人员,那绝对值得学习,但是从DX 12到DX 11的开始是一项艰巨的任务.有关 DX11 的信息,请参见 DirectX Tool Kit 教程./or DX12

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.它不是操作系统或系统头文件的一部分;它只是在本地项目的预编译头文件(pch.h)中定义的:

For modern DirectX sample code and in the VS DirectX templates, Microsoft uses a standard helper function ThrowIfFailed. It's not part of the OS or system headers; it's just defined in the local project's Precompiled Header File (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的返回值,它将返回void.通常,您将ThrowIfFailed用于快速失败"的情况(即,如果函数失败,则程序将无法恢复).

For COM programming, you must check at runtime all HRESULT values for failure. If it's safe to ignore the return value of a particular DirectX 11 or DirectX 12 API, it will return void instead. You generally use ThrowIfFailed for 'fast fail' scenarios (i.e. your program can't recover if the function fails).

请注意,建议使用 C ++异常处理(又名/EHsc),这是VS模板中的默认编译器设置.在x64和ARM平台上,此实现非常有效,而没有任何其他代码开销.旧版x86需要编译器创建的一些其他Epilog/序言代码.围绕本机代码中的异常处理的大多数"FUD"都是基于使用较旧的异步结构化异常处理(又称为/EHa)的经验,这严重地妨碍了代码优化器.

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.

有关更多详细信息和使用信息,请参见此Wiki页面.您还应该阅读 ComPtr 上的页面.

See this wiki page for a bit more detail and usage information. You should also read the page on ComPtr.

GitHub 上的Direct3D游戏VS模板版本中,我使用了ThrowIfFailed的略微增强版本,您也可以使用:

In my version of the Direct3D Game VS Templates on GitHub, I use a slightly enhanced version of ThrowIfFailed which you could also use:

#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);
        }
    }
}

这篇关于DirectX 12中哪个头文件包含ThrowIfFailed()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆