2017-10-17 58 views
0

some part of code image,another part of code image,我是初学者到DirectX 12(或游戏编程),并从微软文档学习。在使用功能ThrowIfFailed()我得到一个错误从vs2015编辑哪个头文件包含DirectX 12中的ThrowIfFiled()

该声明没有存储类或类型说明符智能感知。

任何人都可以帮忙。

+0

这不是一个头的问题。您只需在任何函数之外编写代码 – Garf365

+0

请发布您的代码以帮助我们了解发生了什么 – Garf365

+0

https://msdn.microsoft.com/en-us/library/windows/desktop/dn859356(v=vs.85)。 aspx它写在LoadPipeline()函数 –

回答

1

当你是新来的DirectX编程,我强烈建议开始与DirectX 11,而比DirectX 12. DirectX 12假设你已经是一位专业的DirectX 11开发人员,并且是一个相当无情的API。如果您计划成为图形开发人员,这绝对是值得学习的,但从DX 12开始,通过DX 11开展工作是一项巨大的任务。见的DirectX工具包教程DX11和/或DX12

对于现代的DirectX示例代码,并在VS的DirectX模板,微软采用的是标准的辅助函数ThrowIfFailed。它不是操作系统或系统标题的一部分;

#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值失败:它只是在本地项目的预编译头文件(pch.h)定义。如果忽略特定DirectX 11或DirectX 12 API的返回值是安全的,则它将返回void。您通常使用ThrowIfFailed进行“快速失败”场景(即,如果该功能失败,则您的程序无法恢复)。

注意建议使用C++ Exception Handling(又名/EHsc),这是VS模板中的默认编译器设置。在x64和ARM平台上,这非常高效地实现,无需任何额外的代码开销。传统的x86需要编译器创建的一些额外的epilog/prologue代码。本地代码中的大多数异常处理“FUD”基于使用较早的异步结构异常处理(又名/EHa)的经验,这严重妨碍了代码优化器。

查看this wiki page了解更多的细节和使用信息。您还应该阅读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); 
     } 
    } 
} 
+0

先生,你推荐任何DirectX11或github的书就够了吗? –

+0

@ShivamKushwaha你可以在互联网上找到很多教程 – Asesh

0

这个错误是因为你的一些代码在任何函数之外。

你的错误就在这里:

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