The Dirtchamber
A mixed reality testing environment for real-time global illumination algorithms
cbuffer.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_CONSTANT_BUFFER
10 #define DUNE_CONSTANT_BUFFER
11 
12 #include "shader_resource.h"
13 #include "d3d_tools.h"
14 
15 namespace dune
16 {
26  template<typename T>
27  class cbuffer : public shader_resource
28  {
29  protected:
30  ID3D11Buffer* cb_;
31  ID3D11DeviceContext* context_;
32  D3D11_MAPPED_SUBRESOURCE mapped_res_;
33  T local_;
34 
35  T* map(ID3D11DeviceContext* context)
36  {
37  context_ = context;
38  context_->Map(cb_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_res_);
39  return reinterpret_cast<T*>(mapped_res_.pData);
40  }
41 
42  void unmap()
43  {
44  if (mapped_res_.pData)
45  {
46  context_->Unmap(cb_, 0);
47  ZeroMemory(&mapped_res_, sizeof(D3D11_MAPPED_SUBRESOURCE));
48  }
49  }
50 
51  public:
52  cbuffer() :
53  cb_(nullptr),
54  context_(nullptr)
55  {
56  ZeroMemory(&mapped_res_, sizeof(D3D11_MAPPED_SUBRESOURCE));
57  ZeroMemory(&local_, sizeof(T));
58  }
59 
60  virtual ~cbuffer() {}
61 
62  T& data()
63  {
64  return local_;
65  }
66 
68  const T& data() const
69  {
70  return local_;
71  }
72 
73  void create(ID3D11Device* device)
74  {
75  D3D11_BUFFER_DESC cbd;
76  cbd.Usage = D3D11_USAGE_DYNAMIC;
77  cbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
78  cbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
79  cbd.MiscFlags = 0;
80  cbd.StructureByteStride = 0;
81 
82  cbd.ByteWidth = sizeof(T);
83  assert_hr(device->CreateBuffer(&cbd, nullptr, &cb_));
84  }
85 
86  void to_vs(ID3D11DeviceContext* context, UINT start_slot)
87  {
88  update(context);
89  context_->VSSetConstantBuffers(start_slot, 1, &cb_);
90  }
91 
92  void to_gs(ID3D11DeviceContext* context, UINT start_slot)
93  {
94  update(context);
95  context_->GSSetConstantBuffers(start_slot, 1, &cb_);
96  }
97 
98  void to_ps(ID3D11DeviceContext* context, UINT start_slot)
99  {
100  update(context);
101  context_->PSSetConstantBuffers(start_slot, 1, &cb_);
102  }
103 
104  void to_cs(ID3D11DeviceContext* context, UINT start_slot)
105  {
106  update(context);
107  context_->CSSetConstantBuffers(start_slot, 1, &cb_);
108  }
109 
111  void update(ID3D11DeviceContext* context)
112  {
113  T* mapped = map(context);
114  {
115  *mapped = local_;
116  }
117  unmap();
118  }
119 
120  void destroy()
121  {
122  if (cb_ != nullptr)
123  {
124  unmap();
125  cb_->Release();
126  }
127 
128  cb_ = nullptr;
129  context_ = nullptr;
130  ZeroMemory(&mapped_res_, sizeof(D3D11_MAPPED_SUBRESOURCE));
131  ZeroMemory(&local_, sizeof(T));
132  }
133  };
134 }
135 
136 #endif
void update(ID3D11DeviceContext *context)
Copy over the local buffer into the mapped resource.
Definition: cbuffer.h:111
const T & data() const
Returns a reference to the local copy of the constant buffer.
Definition: cbuffer.h:68
A shader resource wrapper.
Definition: shader_resource.h:24
void to_ps(ID3D11DeviceContext *context, UINT start_slot)
Upload the shader resource to register slot of a pixel shader.
Definition: cbuffer.h:98
void to_cs(ID3D11DeviceContext *context, UINT start_slot)
Upload the shader resource to register slot of a compute shader.
Definition: cbuffer.h:104
void to_gs(ID3D11DeviceContext *context, UINT start_slot)
Upload the shader resource to register slot of a geometry shader.
Definition: cbuffer.h:92
A wrapper for constant buffers.
Definition: cbuffer.h:27
#define assert_hr(x)
An assert for HRESULTs.
Definition: d3d_tools.h:148
void destroy()
Destroy the shader_resource and free all memory.
Definition: cbuffer.h:120
void to_vs(ID3D11DeviceContext *context, UINT start_slot)
Upload the shader resource to register slot of a vertex shader.
Definition: cbuffer.h:86