summaryrefslogtreecommitdiffhomepage
path: root/ovr_sdk_win_23.0.0/LibOVRKernel/Src/Util/Util_Direct3D.cpp
blob: f2baecfc79d71fdd0a2903f11310c5433b3b37e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/************************************************************************************

Filename    :   Util_Direct3D.cpp
Content     :   Shared code for Direct3D
Created     :   Oct 14, 2014
Authors     :   Chris Taylor

Copyright   :   Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.

Licensed under the Oculus Master SDK License Version 1.0 (the "License");
you may not use the Oculus VR Rift SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.

You may obtain a copy of the License at

https://developer.oculus.com/licenses/oculusmastersdk-1.0

Unless required by applicable law or agreed to in writing, the Oculus VR SDK
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

************************************************************************************/

#include "Util_Direct3D.h"
#include "Logging/Logging_Library.h"

static ovrlog::Channel Logger("Util_Direct3D");

namespace OVR {
namespace D3DUtil {

bool VerifyHRESULT(const char* file, int line, HRESULT hr) {
  if (FAILED(hr)) {
    Logger.LogErrorF(
        "D3D function returned fail HRESULT at %s on line %d : %s",
        file,
        line,
        D3DUtil::GetWindowsErrorString(hr).ToCStr());
    OVR_ASSERT(false);
    return false;
  }

  return true;
}

String GetWindowsErrorString(HRESULT hr) {
  wchar_t* errorText = nullptr;

  DWORD slen = FormatMessageW(
      // use system message tables to retrieve error text
      FORMAT_MESSAGE_FROM_SYSTEM
          // allocate buffer on local heap for error text
          | FORMAT_MESSAGE_ALLOCATE_BUFFER
          // Important! will fail otherwise, since we're not
          // (and CANNOT) pass insertion parameters
          | FORMAT_MESSAGE_IGNORE_INSERTS,
      nullptr, // unused with FORMAT_MESSAGE_FROM_SYSTEM
      hr,
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
      (LPWSTR)&errorText, // output, allocated via LocalAlloc (free with LocalFree)
      256, // minimum size for output buffer
      nullptr); // arguments - see note

  char formatStr[512];
  snprintf(formatStr, sizeof(formatStr), "[Code=%lx = %ld]", hr, hr);

  String retStr = formatStr;

  if (slen > 0 && errorText) {
    retStr += " ";
    retStr += errorText;

    // release memory allocated by FormatMessage()
    LocalFree(errorText);
  }

  return retStr;
}

void LogD3DCompileError(HRESULT hr, ID3DBlob* blob) {
  if (FAILED(hr)) {
    char* errStr = (char*)blob->GetBufferPointer();
    SIZE_T len = blob->GetBufferSize();

    if (errStr && len > 0) {
      Logger.LogErrorF("Error compiling shader: %s", errStr);
    }
  }
}
} // namespace D3DUtil
} // namespace OVR