9 #ifndef DUNE_CONSTANT_BUFFER
10 #define DUNE_CONSTANT_BUFFER
31 ID3D11DeviceContext* context_;
32 D3D11_MAPPED_SUBRESOURCE mapped_res_;
35 T* map(ID3D11DeviceContext* context)
38 context_->Map(cb_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_res_);
39 return reinterpret_cast<T*
>(mapped_res_.pData);
44 if (mapped_res_.pData)
46 context_->Unmap(cb_, 0);
47 ZeroMemory(&mapped_res_,
sizeof(D3D11_MAPPED_SUBRESOURCE));
56 ZeroMemory(&mapped_res_,
sizeof(D3D11_MAPPED_SUBRESOURCE));
57 ZeroMemory(&local_,
sizeof(T));
73 void create(ID3D11Device* device)
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;
80 cbd.StructureByteStride = 0;
82 cbd.ByteWidth =
sizeof(T);
83 assert_hr(device->CreateBuffer(&cbd,
nullptr, &cb_));
86 void to_vs(ID3D11DeviceContext* context, UINT start_slot)
89 context_->VSSetConstantBuffers(start_slot, 1, &cb_);
92 void to_gs(ID3D11DeviceContext* context, UINT start_slot)
95 context_->GSSetConstantBuffers(start_slot, 1, &cb_);
98 void to_ps(ID3D11DeviceContext* context, UINT start_slot)
101 context_->PSSetConstantBuffers(start_slot, 1, &cb_);
104 void to_cs(ID3D11DeviceContext* context, UINT start_slot)
107 context_->CSSetConstantBuffers(start_slot, 1, &cb_);
111 void update(ID3D11DeviceContext* context)
113 T* mapped = map(context);
130 ZeroMemory(&mapped_res_,
sizeof(D3D11_MAPPED_SUBRESOURCE));
131 ZeroMemory(&local_,
sizeof(T));
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
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