The Dirtchamber
A mixed reality testing environment for real-time global illumination algorithms
light_propagation_volume.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_LIGHT_PROPAGATION_VOLUME
10 #define DUNE_LIGHT_PROPAGATION_VOLUME
11 
12 #include <D3D11.h>
13 
14 #include "render_target.h"
15 #include "shader_resource.h"
16 #include "cbuffer.h"
17 #include "d3d_tools.h"
18 
19 namespace dune
20 {
21  class gbuffer;
22  struct d3d_mesh;
23 
33  {
34  public:
35  float time_inject_;
36  float time_propagate_;
37  float time_normalize_;
38 
39  struct param
40  {
41  float vpl_scale;
42  float lpv_flux_amplifier;
43  UINT num_vpls;
44  BOOL debug_gi;
45  };
46 
47  protected:
48  ID3D11BlendState* bs_inject_;
49  ID3D11BlendState* bs_propagate_;
50 
51  sampler_state ss_vplfilter_;
52 
53  ID3D11VertexShader* vs_inject_;
54  ID3D11GeometryShader* gs_inject_;
55  ID3D11PixelShader* ps_inject_;
56 
57  ID3D11VertexShader* vs_propagate_;
58  ID3D11GeometryShader* gs_propagate_;
59  ID3D11PixelShader* ps_propagate_;
60 
61  ID3D11PixelShader* ps_normalize_;
62 
63  INT propagate_start_slot_;
64  INT inject_rsm_start_slot_;
65 
66  ID3D11Buffer* lpv_volume_;
67  ID3D11InputLayout* input_layout_;
68  UINT volume_size_;
69 
70  render_target lpv_r_[2];
71  render_target lpv_g_[2];
72  render_target lpv_b_[2];
73 
74  render_target lpv_accum_r_;
75  render_target lpv_accum_g_;
76  render_target lpv_accum_b_;
77 
78  render_target lpv_inject_counter_;
79 
80  size_t iterations_rendered_;
81 
82  unsigned int curr_;
83  unsigned int next_;
84 
85  DirectX::XMFLOAT4X4 world_to_lpv_;
86 
87  struct cbs_debug
88  {
89  DirectX::XMFLOAT3 lpv_pos;
90  float pad;
91  };
92 
93  cbuffer<cbs_debug> cb_debug_;
94 
95  struct cbs_parameters
96  {
97  DirectX::XMFLOAT4X4 world_to_lpv;
98  UINT lpv_size;
99  DirectX::XMFLOAT3 pad;
100  };
101 
102  cbuffer<cbs_parameters> cb_parameters_;
103 
104  struct cbs_propagation
105  {
106  UINT iteration;
107  DirectX::XMFLOAT3 pad;
108  };
109 
110  cbuffer<cbs_propagation> cb_propagation_;
111 
113  cb_gi_parameters cb_gi_parameters_;
114 
115  dune::profile_query profiler_;
116 
117  protected:
118  void swap_buffers();
119 
120  void normalize(ID3D11DeviceContext* context);
121  void propagate(ID3D11DeviceContext* context);
122  void propagate(ID3D11DeviceContext* context, size_t num_iterations);
123 
124  public:
126  virtual ~light_propagation_volume() {}
127 
128  virtual void create(ID3D11Device* device, UINT volume_size);
129  virtual void destroy();
130 
132 
133  cb_gi_parameters& parameters() { return cb_gi_parameters_; }
134  const cb_gi_parameters& parameters() const { return cb_gi_parameters_; }
136 
151  void set_inject_shader(ID3D11VertexShader* vs, ID3D11GeometryShader* gs, ID3D11PixelShader* ps, ID3D11PixelShader* ps_normalize, UINT inject_rsm_start_slot);
152 
171  void set_propagate_shader(ID3D11Device* device, ID3D11VertexShader* vs, ID3D11GeometryShader* gs, ID3D11PixelShader* ps, ID3DBlob* input_binary, UINT propagate_start_slot);
172 
183  virtual void inject(ID3D11DeviceContext* context, gbuffer& rsm, bool clear);
184 
186  void render(ID3D11DeviceContext* context);
187 
189 
190  size_t num_propagations() const { return iterations_rendered_; }
191  void set_num_propagations(size_t n) { iterations_rendered_ = n; }
193 
195  void visualize(ID3D11DeviceContext* context, d3d_mesh* node, UINT debug_info_slot);
196 
198 
199  void set_model_matrix(ID3D11DeviceContext* context, const DirectX::XMFLOAT4X4& model, const DirectX::XMFLOAT3& lpv_min, const DirectX::XMFLOAT3& lpv_max, UINT lpv_parameters_slot);
200  const DirectX::XMFLOAT4X4& world_to_lpv() const { return world_to_lpv_; }
202 
203  virtual void to_ps(ID3D11DeviceContext* context, UINT slot);
204  };
205 
223  {
224  protected:
225  ID3D11BlendState* bs_negative_inject_;
226  ID3D11VertexShader* vs_direct_inject_;
227 
228  bool negative_;
229 
230  struct cbs_delta_injection
231  {
232  FLOAT scale;
233  BOOL is_direct;
234  FLOAT pad1;
235  FLOAT pad2;
236  };
237 
238  cbuffer<cbs_delta_injection> cb_delta_injection_;
239 
240  public:
242  virtual ~delta_light_propagation_volume() {}
243 
244  virtual void create(ID3D11Device* device, UINT volume_size);
245  virtual void destroy();
246 
248  void set_direct_inject_shader(ID3D11VertexShader* ps);
249 
261  void inject(ID3D11DeviceContext* context, gbuffer& rsm, bool clear, bool is_direct, float scale);
262 
264  void render_indirect(ID3D11DeviceContext* context);
265 
267 
268  void propagate_direct(ID3D11DeviceContext* context, size_t num_iterations);
269  void render_direct(ID3D11DeviceContext* context, size_t num_iterations, UINT lpv_out_start_slot);
271  };
272 }
273 
274 #endif
void render_direct(ID3D11DeviceContext *context, size_t num_iterations, UINT lpv_out_start_slot)
Render/propagate direct injects.
Definition: light_propagation_volume.cpp:698
void propagate_direct(ID3D11DeviceContext *context, size_t num_iterations)
Render/propagate direct injects.
Definition: light_propagation_volume.cpp:610
cb_gi_parameters & parameters()
Return local cbuffer parameters.
Definition: light_propagation_volume.h:133
void inject(ID3D11DeviceContext *context, gbuffer &rsm, bool clear, bool is_direct, float scale)
Overwrites the default injection to include direct injects.
Definition: light_propagation_volume.cpp:534
A Delta Light Propagation Volume (DLPV).
Definition: light_propagation_volume.h:222
void set_propagate_shader(ID3D11Device *device, ID3D11VertexShader *vs, ID3D11GeometryShader *gs, ID3D11PixelShader *ps, ID3DBlob *input_binary, UINT propagate_start_slot)
Set the complete propagation shader.
Definition: light_propagation_volume.cpp:478
const cb_gi_parameters & parameters() const
Return local cbuffer parameters.
Definition: light_propagation_volume.h:134
A Light Propagation Volume.
Definition: light_propagation_volume.h:32
A shader resource wrapper.
Definition: shader_resource.h:24
virtual void destroy()
Destroy the shader_resource and free all memory.
Definition: light_propagation_volume.cpp:203
void render_indirect(ID3D11DeviceContext *context)
Render/propagate indirect injects.
Definition: light_propagation_volume.cpp:713
virtual void to_ps(ID3D11DeviceContext *context, UINT slot)
Upload the shader resource to register slot of a pixel shader.
Definition: light_propagation_volume.cpp:398
void set_inject_shader(ID3D11VertexShader *vs, ID3D11GeometryShader *gs, ID3D11PixelShader *ps, ID3D11PixelShader *ps_normalize, UINT inject_rsm_start_slot)
Set the complete injection shader.
Definition: light_propagation_volume.cpp:468
void visualize(ID3D11DeviceContext *context, d3d_mesh *node, UINT debug_info_slot)
A rendering function to visualize the LPV with colored cubes for each voxel.
Definition: light_propagation_volume.cpp:499
A wrapper class for a sampler state.
Definition: shader_resource.h:49
size_t num_propagations() const
Get/set the number of propagation steps for the LPV.
Definition: light_propagation_volume.h:190
const DirectX::XMFLOAT4X4 & world_to_lpv() const
Set/get the world -> LPV matrix, which transforms world coordinates to LPV volume coordinates...
Definition: light_propagation_volume.h:200
virtual void destroy()
Destroy the shader_resource and free all memory.
Definition: light_propagation_volume.cpp:688
void set_model_matrix(ID3D11DeviceContext *context, const DirectX::XMFLOAT4X4 &model, const DirectX::XMFLOAT3 &lpv_min, const DirectX::XMFLOAT3 &lpv_max, UINT lpv_parameters_slot)
Set/get the world -> LPV matrix, which transforms world coordinates to LPV volume coordinates...
Definition: light_propagation_volume.cpp:176
A GPU profiler.
Definition: d3d_tools.h:23
A render target wrapper.
Definition: render_target.h:30
void render(ID3D11DeviceContext *context)
Run the normalization and propagation of the LPV.
Definition: light_propagation_volume.cpp:455
A geometry buffer (collection of render_target objects).
Definition: gbuffer.h:30
void set_direct_inject_shader(ID3D11VertexShader *ps)
Additionally to the regular indirect injection shader, this will set the direct injection pixel shade...
Definition: light_propagation_volume.cpp:683
Main D3D mesh interface class.
Definition: mesh.h:129
void set_num_propagations(size_t n)
Get/set the number of propagation steps for the LPV.
Definition: light_propagation_volume.h:191
virtual void inject(ID3D11DeviceContext *context, gbuffer &rsm, bool clear)
Inject VPLs from an RSM into the LPV.
Definition: light_propagation_volume.cpp:254