2#include "Graphics/Common.h"
10 constexpr U32 CalculateCBSize(U32 size)
12 return ((size + 15) / 16) * 16;
26 ZeroMemory(&Data,
sizeof(T));
39 D3D11_BUFFER_DESC desc;
40 desc.Usage = D3D11_USAGE_DYNAMIC;
41 desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
42 desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
44 desc.ByteWidth =
static_cast<U32
>(
sizeof(T) + (16 - (
sizeof(T) % 16)));
48 desc.Usage = D3D11_USAGE_DEFAULT;
49 desc.CPUAccessFlags = 0;
51 GPUWritable = gpuWritable;
53 DXCall(device->CreateBuffer(&desc,
nullptr, cb.Buffer.ReleaseAndGetAddressOf()));
65 throw std::runtime_error(
"Trying to apply changes on a constant buffer that was not created");
69 ctx->UpdateSubresource(Buffer.Get(), 0,
nullptr, &Data, 0, 0);
73 D3D11_MAPPED_SUBRESOURCE mappedResource{};
74 DXCall(ctx->Map(Buffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource));
75 CopyMemory(mappedResource.pData, &Data,
sizeof(T));
76 ctx->Unmap(Buffer, 0);
85 void SetVS(ID3D11DeviceContext1* ctx, U32 slot)
88 throw std::runtime_error(
"Constant buffer was nullptr when trying to set for VS");
90 ID3D11Buffer* bufferArray[1];
91 bufferArray[0] = Buffer.Get();
93 ctx->VSSetConstantBuffers(slot, 1, bufferArray);
101 void SetPS(ID3D11DeviceContext1* ctx, U32 slot)
104 throw std::runtime_error(
"Constant buffer was nullptr when trying to set for PS");
106 ID3D11Buffer* bufferArray[1];
107 bufferArray[0] = Buffer.Get();
109 ctx->PSSetConstantBuffers(slot, 1, bufferArray);
117 void SetCS(ID3D11DeviceContext1* ctx, U32 slot)
120 throw std::runtime_error(
"Constant buffer was nullptr when trying to set for CS");
122 ID3D11Buffer* bufferArray[1];
123 bufferArray[0] = Buffer.Get();
125 ctx->CSSetConstantBuffers(slot, 1, bufferArray);
133 void SetGS(ID3D11DeviceContext1* ctx, U32 slot)
136 throw std::runtime_error(
"Constant buffer was nullptr when trying to set for GS");
138 ID3D11Buffer* bufferArray[1];
139 bufferArray[0] = Buffer.Get();
141 ctx->GSSetConstantBuffers(slot, 1, bufferArray);
149 void SetHS(ID3D11DeviceContext1* ctx, U32 slot)
152 throw std::runtime_error(
"Constant buffer was nullptr when trying to set for HS");
154 ID3D11Buffer* bufferArray[1];
155 bufferArray[0] = Buffer.Get();
157 ctx->HSSetConstantBuffers(slot, 1, bufferArray);
165 void SetDS(ID3D11DeviceContext1* ctx, U32 slot)
168 throw std::runtime_error(
"Constant buffer was nullptr when trying to set for DS");
170 ID3D11Buffer* bufferArray[1];
171 bufferArray[0] = Buffer.Get();
173 ctx->DSSetConstantBuffers(slot, 1, bufferArray);
178 ComPtr<ID3D11Buffer> Buffer;
Represents a constant buffer.
Definition ConstantBuffer.h:21
void SetDS(ID3D11DeviceContext1 *ctx, U32 slot)
Sets the constant buffer for the domain shader.
Definition ConstantBuffer.h:165
void SetHS(ID3D11DeviceContext1 *ctx, U32 slot)
Sets the constant buffer for the hull shader.
Definition ConstantBuffer.h:149
static ConstantBuffer< T > Create(ID3D11Device1 *device, bool gpuWritable=false)
Creates a constant buffer.
Definition ConstantBuffer.h:35
void SetGS(ID3D11DeviceContext1 *ctx, U32 slot)
Sets the constant buffer for the geometry shader.
Definition ConstantBuffer.h:133
void SetCS(ID3D11DeviceContext1 *ctx, U32 slot)
Sets the constant buffer for the compute shader.
Definition ConstantBuffer.h:117
void SetVS(ID3D11DeviceContext1 *ctx, U32 slot)
Sets the constant buffer for the vertex shader.
Definition ConstantBuffer.h:85
void SetPS(ID3D11DeviceContext1 *ctx, U32 slot)
Sets the constant buffer for the pixel shader.
Definition ConstantBuffer.h:101
void ApplyChanges(ID3D11DeviceContext1 *ctx)
Applies the changes made to the constant buffer.
Definition ConstantBuffer.h:62