The Dirtchamber
A mixed reality testing environment for real-time global illumination algorithms
d3d_tools.h
Go to the documentation of this file.
1 /*
2  * Dune D3D library - Tobias Alexander Franke 2011
3  * For copyright and license see LICENSE
4  * http://www.tobias-franke.eu
5  */
6 
9 #ifndef DUNE_D3D_TOOLS
10 #define DUNE_D3D_TOOLS
11 
12 #include <D3D11.h>
13 #include <DirectXMath.h>
14 
15 namespace dune
16 {
24  {
25  protected:
26  ID3D11Query* frequency_;
27  ID3D11Query* start_;
28  ID3D11Query* stop_;
29 
30  ID3D11DeviceContext* context_;
31 
32  public:
33  profile_query();
34  virtual ~profile_query() {}
35 
37  void create(ID3D11Device* device);
38 
40  void destroy();
41 
43  void begin(ID3D11DeviceContext* context);
44 
46  void end();
47 
49  float result();
50  };
51 
52  template<typename T>
53  void set_debug_name(T* obj, const CHAR* name)
54  {
55  if (obj)
56  obj->SetPrivateData(WKPDID_D3DDebugObjectName, lstrlenA(name), name);
57  }
58 
60  template<typename T>
61  void safe_release(T& obj)
62  {
63  if (obj)
64  {
65  obj->Release();
66  obj = nullptr;
67  }
68  }
69 
85  template<typename T>
86  void exchange(T** oldv, T* newv)
87  {
88  if (!oldv)
89  return;
90 
91  if (*oldv == newv)
92  return;
93 
94  if (newv)
95  {
96  safe_release(*oldv);
97  newv->AddRef();
98  *oldv = newv;
99  }
100  }
101 
109  void set_viewport(ID3D11DeviceContext* context, size_t w, size_t h);
110 
111  /* \brief Create a new XMFLOAT3 containing only maximum values of a and b. */
112  DirectX::XMFLOAT3 max(const DirectX::XMFLOAT3& a, const DirectX::XMFLOAT3& b);
113 
114  /* \brief Create a new XMFLOAT3 containing only minimum values of a and b. */
115  DirectX::XMFLOAT3 min(const DirectX::XMFLOAT3& a, const DirectX::XMFLOAT3& b);
116 
117  /* \brief Returns true of a and b contain equal elements. */
118  bool equal(const DirectX::XMFLOAT4& a, const DirectX::XMFLOAT4& b);
119 
128  void clear_rtvs(ID3D11DeviceContext* context,
129  ID3D11RenderTargetView** rtvs,
130  size_t num_rtvs,
131  FLOAT* clear_color);
132 
134  bool is_srgb(ID3D11ShaderResourceView* rtv);
135 
137  bool is_srgb(DXGI_FORMAT f);
138 
139  void assert_hr_detail(const HRESULT& hr, const char* file, DWORD line, const char* msg);
140 
148  #define assert_hr(x) assert_hr_detail(x, __FILE__, __LINE__, #x)
149 }
150 
151 #endif
void create(ID3D11Device *device)
Create a profile_query object.
Definition: d3d_tools.cpp:40
void destroy()
Destroy a profile_query object and free its resources.
Definition: d3d_tools.cpp:53
void begin(ID3D11DeviceContext *context)
Start a GPU time query.
Definition: d3d_tools.cpp:61
float result()
Stop measuring time and return the result in miliseconds.
Definition: d3d_tools.cpp:75
void clear_rtvs(ID3D11DeviceContext *context, ID3D11RenderTargetView **rtvs, size_t num_rtvs, FLOAT *clear_color)
Clear a list of render targets with a color.
Definition: d3d_tools.cpp:154
void set_viewport(ID3D11DeviceContext *context, size_t w, size_t h)
Resize the current viewport.
Definition: d3d_tools.cpp:112
void end()
Stop measuring time.
Definition: d3d_tools.cpp:69
A GPU profiler.
Definition: d3d_tools.h:23
void safe_release(T &obj)
Safely release an object of type T, setting it to nullptr afterwards.
Definition: d3d_tools.h:61
void exchange(T **oldv, T *newv)
Exchange an object of type T with a new one.
Definition: d3d_tools.h:86