DirectX异常处理 [英] DirectX Exception handling

查看:279
本文介绍了DirectX异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注DirectX的rastertek教程,同时还在David Abrahams( http://www.boost.org/community/exception_safety.html )。



为什么(根据任何人的知识)异常处理设置了它在rastertek教程中的方式,它提供了什么级别的保护?例如:

  mhresult = D3D11CreateDeviceAndSwapChain(NULL,D3D_DRIVER_TYPE_HARDWARE, NULL,0,& mfeatureLevel,1,
D3D11_SDK_VERSION,& mswapChainDesc,& mswapChain,& mdevice,NULL,& mdeviceContext);

if(FAILED(mhresult))
{
return false;
}

如果在调用CreateDevice时发生未处理的异常,在我们检查mhresult的结果之前?



这是否会影响我们是否调用一个具有HResult返回值或布尔值的方法?

  result = mEstablishHW(); 
if(!result)
{
return false;有没有另一种方法可以提供强大的异常安全性而不会对性能产生影响?

解决方案

DirectX是一个基于COM 的API。并且不允许异常跨越COM边界。所以没有DirectX函数存在,曾经抛出。相反,他们使用C样式的返回代码(称为 HRESULT )来指示错误。 p>

为了有效地使用DirectX,显然你应该至少学习一些COM的基础知识。但是这里有一些提示,让您的初始代码更安全,并简化调试:




  • 始终总是检查返回代码。这并不意味着你每次都应该写 if / else 。写一个将检查 HRESULT 的宏,在不 S_OK 的情况下断开调试器,并告诉您一个文件,行和功能发生的地方。您也可以将 HRESULT 转换为可读字符串并输出(在控制台中)。

  • 这是一个好主意,如果它们是有效的(即不是 NULL ): mswapChain mdevice

  • 使用DirectX调试图层: D3D11_CREATE_DEVICE_DEBUG

  • 使用图形诊断工具:自2012年以来内置于Visual Studio Nvidia Nsight 也很不错

  • 最后,阅读文档! MSDN不完美,但 DirectX文档是很好写的。



无论如何,DirectX不是开始学习异常安全的API。也许标准库(如试图为你的类写适当的 swap 函数)或Boost(像文件系统)将更好地工作。


I'm following the rastertek tutorial on DirectX while also studying Exception-Safety in Generic Components by David Abrahams (http://www.boost.org/community/exception_safety.html).

Why (to the best of anyone's knowledge) is the exception handling set up the way it is in the rastertek tutorial and what level of protection does it offer?

For instance:

mhresult = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, &mfeatureLevel, 1,
    D3D11_SDK_VERSION, &mswapChainDesc, &mswapChain, &mdevice, NULL, &mdeviceContext);

if (FAILED(mhresult))
{
    return false;
}

if an unhandled exception occurs in the call to CreateDevice won't the program crash before we check the result of mhresult?

Does it make a difference whether we're calling a method with an HResult return value or just a boolean?

result = mEstablishHW();
if (!result)
{
    return false;
}

Is there an alternative approach that would provide strong exception safety without a performance impact?

解决方案

DirectX is a COM-based API. And exceptions are not allowed to cross COM boundary. So no DirectX function exists that ever throw. Instead, they use C-style return codes, which are called HRESULT, to indicate errors.

To effectively work with DirectX, obviously you should learn at least some basics of COM. But here are tips to start with, to make your initial code safer and to simplify debugging:

  • Always always always check return code. It doesn't mean you should write if/else each time. Write a macro that will check HRESULT, break a debugger in case of "not S_OK", and tell you a file, line and function where it happened. You could also convert HRESULT to readable string and output it (in console).
  • It is a good idea, to check output objects if they are valid (i.e. not "NULL"): mswapChain, mdevice, mdeviceContext in your example.
  • Use DirectX debug layer: D3D11_CREATE_DEVICE_DEBUG
  • Use Graphics diagnostics tools: one is built into Visual Studio since 2012, Nvidia Nsight is very good too
  • Finally, read the docs! MSDN isn't perfect, but DirectX docs are pretty well-written.

Anyway, DirectX is not the API to start to learn exception safety. Maybe Standard library (like trying to write proper swap function for your classes) or Boost (like filesystem) will work better for that purpose.

这篇关于DirectX异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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