Util_Direct3D.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /************************************************************************************
  2. Filename : Util_Direct3D.h
  3. Content : Shared code for Direct3D
  4. Created : Oct 14, 2014
  5. Authors : Chris Taylor
  6. Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
  7. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  8. you may not use the Oculus VR Rift SDK except in compliance with the License,
  9. which is provided at the time of installation or download, or which
  10. otherwise accompanies this software in either electronic or hard copy form.
  11. You may obtain a copy of the License at
  12. http://www.oculusvr.com/licenses/LICENSE-3.2
  13. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  14. distributed under the License is distributed on an "AS IS" BASIS,
  15. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. See the License for the specific language governing permissions and
  17. limitations under the License.
  18. ************************************************************************************/
  19. #ifndef OVR_Util_Direct3D_h
  20. #define OVR_Util_Direct3D_h
  21. // Include Windows correctly first before implicitly including it below
  22. #include "Kernel/OVR_Win32_IncludeWindows.h"
  23. #include "Kernel/OVR_String.h"
  24. // Direct3D 11
  25. #include <D3D11Shader.h>
  26. #include <d3dcompiler.h>
  27. #if _MSC_VER >= 1800
  28. // Visual Studio 2013+ support newer D3D/DXGI headers.
  29. #define OVR_D3D11_VER 2
  30. #include <d3d11_2.h>
  31. #define OVR_DXGI_VER 3
  32. #include <dxgi1_3.h> // Used in place of 1.2 for IDXGIFactory2 debug version (when available)
  33. #elif _MSC_VER >= 1700
  34. // Visual Studio 2012+ only supports older D3D/DXGI headers.
  35. #define OVR_D3D11_VER 1
  36. #include <d3d11_1.h>
  37. #else
  38. // Visual Studio 2010+ only supports original D3D/DXGI headers.
  39. #define OVR_D3D11_VER 1
  40. #include <d3d11.h>
  41. #endif
  42. namespace OVR { namespace D3DUtil {
  43. String GetWindowsErrorString(HRESULT hr);
  44. //-----------------------------------------------------------------------------
  45. // Helper macros for verifying HRESULT values from Direct3D API calls
  46. //
  47. // These will assert on failure in debug mode, and in release or debug mode
  48. // these functions will report the file and line where the error occurred,
  49. // and what the error code was to the log at Error-level.
  50. // Assert on HRESULT failure
  51. bool VerifyHRESULT(const char* file, int line, HRESULT hr);
  52. #define OVR_D3D_CHECK(hr) OVR::D3DUtil::VerifyHRESULT(__FILE__, __LINE__, hr)
  53. // Internal implementation of the OVR_D3D_CHECK_RET family of functions.
  54. #define OVR_D3D_CHECK_RET_IMPL(hr, returnExpression) \
  55. { \
  56. if (!OVR_D3D_CHECK(hr)) \
  57. { \
  58. returnExpression \
  59. } \
  60. }
  61. // Returns provided value on failure.
  62. // Example usage:
  63. // size_t Func() {
  64. // . . .
  65. // HRESULT hr = Device->QueryInterface(__uuidof(IDXGIDevice), (void **)&pDXGIDevice.GetRawRef());
  66. // OVR_D3D_CHECK_RET_VAL(hr, 0);
  67. // . . .
  68. // }
  69. #define OVR_D3D_CHECK_RET_VAL(hr, failureValue) OVR_D3D_CHECK_RET_IMPL(hr, return failureValue;)
  70. // Returns void on failure
  71. // Example usage:
  72. // void Func() {
  73. // . . .
  74. // HRESULT hr = Device->QueryInterface(__uuidof(IDXGIDevice), (void **)&pDXGIDevice.GetRawRef());
  75. // OVR_D3D_CHECK_RET(hr);
  76. // . . .
  77. // }
  78. #define OVR_D3D_CHECK_RET(hr) OVR_D3D_CHECK_RET_IMPL(hr, return;)
  79. // Returns false on failure
  80. // Example usage:
  81. // bool Func() {
  82. // . . .
  83. // HRESULT hr = Device->QueryInterface(__uuidof(IDXGIDevice), (void **)&pDXGIDevice.GetRawRef());
  84. // OVR_D3D_CHECK_RET_FALSE(hr);
  85. // . . .
  86. // }
  87. #define OVR_D3D_CHECK_RET_FALSE(hr) OVR_D3D_CHECK_RET_IMPL(hr, return false;)
  88. // Returns nullptr on failure
  89. // Example usage:
  90. // void* Func() {
  91. // . . .
  92. // HRESULT hr = Device->QueryInterface(__uuidof(IDXGIDevice), (void **)&pDXGIDevice.GetRawRef());
  93. // OVR_D3D_CHECK_RET_NULL(hr);
  94. // . . .
  95. // }
  96. #define OVR_D3D_CHECK_RET_NULL(hr) OVR_D3D_CHECK_RET_IMPL(hr, return nullptr;)
  97. // If the result is a failure, it will write the exact compile error to the error log
  98. void LogD3DCompileError(HRESULT hr, ID3DBlob* errorBlob);
  99. #if defined(OVR_BUILD_DEBUG)
  100. // Enable this to track down double-tagging object warnings from D3D.
  101. #define OVR_D3D_TRACK_DOUBLE_TAGGING
  102. #if defined(OVR_D3D_TRACK_DOUBLE_TAGGING)
  103. #define OVR_D3D_CHECK_REUSE(child) \
  104. char tagReuseBuffer[1024]; \
  105. UINT reuseSize = (UINT)sizeof(tagReuseBuffer); \
  106. OVR_ASSERT(FAILED(child->GetPrivateData(WKPDID_D3DDebugObjectName, &reuseSize, tagReuseBuffer)));
  107. #else
  108. #define OVR_D3D_CHECK_REUSE(child) (void(0))
  109. #endif
  110. #define OVR_D3D_TAG_OBJECT(child) \
  111. if (child) { \
  112. OVR_D3D_CHECK_REUSE(child); \
  113. const char* tagName = OVR_STRINGIZE(child) " " __FILE__ "(" OVR_STRINGIZE(__LINE__) ")"; \
  114. UINT tagDataSize = (UINT)strlen(tagName); \
  115. HRESULT tagHR = child->SetPrivateData(WKPDID_D3DDebugObjectName, tagDataSize, tagName); \
  116. OVR_D3D_CHECK(tagHR); \
  117. }
  118. #else // !Debug:
  119. #define OVR_D3D_TAG_OBJECT(child) (void(0))
  120. #endif // !Debug
  121. }} // namespace OVR::D3DUtil
  122. #endif // OVR_Util_Direct3D_h