Nui Engine
A game engine framework
Loading...
Searching...
No Matches
Common.h
1#pragma once
2#include <Core/Common/NuiWin.h>
3#include <Core/Common/Types.h>
4#include <Core/Utils/Exceptions.h>
5#include <Core/Math/Math.h>
6#include <wrl/client.h>
7
8#include <d3d11sdklayers.h>
9#include <wincodec.h>
10#include <dxgi1_6.h>
11#include <d3d11_4.h>
12#include <dxgidebug.h>
13#include <d3dcompiler.h>
14#include <d3d11shader.h>
15#include <DirectXColors.h>
16
17
18// Library links
19#pragma comment(lib, "d3d11.lib")
20#pragma comment(lib, "d3d9.lib")
21#pragma comment(lib, "dxgi.lib")
22#pragma comment(lib, "dxguid.lib")
23#pragma comment(lib, "D3DCompiler.lib")
24
25
26namespace Nui::Graphics
27{
28 template <typename T>
29 using ComPtr = Microsoft::WRL::ComPtr<T>;
30
31 // Helper class for COM exceptions
32 class com_exception : public std::exception
33 {
34 public:
35 com_exception(HRESULT hr, StringView expression, StringView file, U32 line)
36 : m_result(hr)
37 , m_file(file)
38 , m_line(line)
39 {}
40
41 const char* what() const noexcept override
42 {
43 static char s_str[512] = {};
44 sprintf_s(s_str, "DXCall caught an exception\nMessage: %s\nExpression: %s\nFile: %s(%u)",
45 GetWin32ErrorString(m_result).c_str(), m_expression.c_str(), m_file.c_str(), m_line);
46
47 return s_str;
48 }
49
50 private:
51 HRESULT m_result;
52 String m_file;
53 String m_expression;
54 U32 m_line;
55 };
56
57 inline void ThrowIfFailed(HRESULT hr, StringView expression, StringView file, U32 line)
58 {
59 if (FAILED(hr))
60 {
61 throw com_exception(hr, expression, file, line);
62 }
63 }
64
65 template <class T>
66 void SafeReleaseCOM(T** ptr)
67 {
68 if (*ptr)
69 {
70 (*ptr)->Release();
71 *ptr = nullptr;
72 }
73 }
74
75 template <typename T>
76 HRESULT SetDebugName(T deviceChild, const String& debugName)
77 {
78#if defined(NUI_DEBUG)
79 return deviceChild->SetPrivateData(WKPDID_D3DDebugObjectName, (U32)debugName.size(), debugName.data());
80#else
81 return S_OK;
82#endif // defined(NUI_DEBUG)
83
84 }
85
86#define DXCall(expr) ThrowIfFailed(expr, #expr, __FILE__, __LINE__)
87// Align to float4 (16 bytes)
88#define Float4Align __declspec(align(16))
89}
Definition Common.h:33