Util_Direct3D.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /************************************************************************************
  2. Filename : Util_Direct3D.cpp
  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. #include "Util_Direct3D.h"
  20. #include "Kernel/OVR_Log.h"
  21. namespace OVR { namespace D3DUtil {
  22. bool VerifyHRESULT(const char* file, int line, HRESULT hr)
  23. {
  24. if (FAILED(hr))
  25. {
  26. LogError("D3D function returned fail HRESULT at %s on line %d : %s",
  27. file, line, D3DUtil::GetWindowsErrorString(hr).ToCStr());
  28. OVR_ASSERT(false);
  29. return false;
  30. }
  31. return true;
  32. }
  33. String GetWindowsErrorString(HRESULT hr)
  34. {
  35. wchar_t errorTextAddr[256] = {};
  36. DWORD slen = FormatMessageW(
  37. // use system message tables to retrieve error text
  38. FORMAT_MESSAGE_FROM_SYSTEM
  39. // allocate buffer on local heap for error text
  40. | FORMAT_MESSAGE_ALLOCATE_BUFFER
  41. // Important! will fail otherwise, since we're not
  42. // (and CANNOT) pass insertion parameters
  43. | FORMAT_MESSAGE_IGNORE_INSERTS,
  44. nullptr, // unused with FORMAT_MESSAGE_FROM_SYSTEM
  45. hr,
  46. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  47. errorTextAddr, // output
  48. 256, // minimum size for output buffer
  49. nullptr); // arguments - see note
  50. char errorText[256];
  51. OVR::UTF8Util::Strlcpy(errorTextAddr, OVR_ARRAY_COUNT(errorTextAddr), errorText, 256);
  52. char formatStr[512];
  53. OVR_snprintf(formatStr, sizeof(formatStr), "[Code=%x = %d]", hr, hr);
  54. String retStr = formatStr;
  55. if (slen > 0 && errorText)
  56. {
  57. retStr += " ";
  58. retStr += errorText;
  59. // release memory allocated by FormatMessage()
  60. LocalFree(errorText);
  61. }
  62. return retStr;
  63. }
  64. void LogD3DCompileError(HRESULT hr, ID3DBlob* blob)
  65. {
  66. if (FAILED(hr))
  67. {
  68. char* errStr = (char*)blob->GetBufferPointer();
  69. SIZE_T len = blob->GetBufferSize();
  70. if (errStr && len > 0)
  71. {
  72. LogError("Error compiling shader: %s", errStr);
  73. }
  74. }
  75. }
  76. }} // namespace OVR::D3DUtil