The Dirtchamber
A mixed reality testing environment for real-time global illumination algorithms
shader_tools.h
Go to the documentation of this file.
1 /*
2  * Dune D3D library - Tobias Alexander Franke 2012
3  * For copyright and license see LICENSE
4  * http://www.tobias-franke.eu
5  */
6 
9 #ifndef DUNE_SHADER_TOOLS
10 #define DUNE_SHADER_TOOLS
11 
12 #include <map>
13 #include <string>
14 
15 #include <D3D11.h>
16 
17 #include "exception.h"
18 
19 namespace dune
20 {
21  namespace detail
22  {
23  void compile_shader(ID3D11Device* device, LPCTSTR filename, LPCSTR shadermodel, LPCSTR mainfunc, UINT shader_flags, D3D_SHADER_MACRO* defines, ID3DBlob** binary);
24  void compile_shader(ID3D11Device* device, ID3DBlob* binary, ID3D11GeometryShader** gs);
25  void compile_shader(ID3D11Device* device, ID3DBlob* binary, ID3D11PixelShader** ps);
26  void compile_shader(ID3D11Device* device, ID3DBlob* binary, ID3D11VertexShader** vs);
27  }
28 
49  template<typename T>
50  void compile_shader(ID3D11Device* device, LPCTSTR filename, LPCSTR shadermodel, LPCSTR mainfunc, UINT shader_flags, D3D_SHADER_MACRO* defines, T** shader, ID3DBlob** binary = nullptr)
51  {
52  ID3DBlob* blob = nullptr;
53 
54  try
55  {
56  detail::compile_shader(device, filename, shadermodel, mainfunc, shader_flags, defines, &blob);
57  }
58  catch (exception& e)
59  {
60  tcerr << e.msg() << std::endl;
61  safe_release(blob);
62  return;
63  }
64 
65  safe_release(*shader);
66 
67  try
68  {
69  detail::compile_shader(device, blob, shader);
70  }
71  catch (exception& e)
72  {
73  safe_release(blob);
74  throw e;
75  }
76 
77  if (binary)
78  *binary = blob;
79  else
80  safe_release(blob);
81  }
82 }
83 
84 #endif
Exception class.
Definition: exception.h:25
virtual const tstring & msg() const
Return exception message as tstring.
Definition: exception.h:42
void safe_release(T &obj)
Safely release an object of type T, setting it to nullptr afterwards.
Definition: d3d_tools.h:61
void compile_shader(ID3D11Device *device, LPCTSTR filename, LPCSTR shadermodel, LPCSTR mainfunc, UINT shader_flags, D3D_SHADER_MACRO *defines, T **shader, ID3DBlob **binary=nullptr)
(Re-)Compile a shader.
Definition: shader_tools.h:50