Nui Engine
A game engine framework
Loading...
Searching...
No Matches
ConstantBuffer.h
1#pragma once
2#include "Graphics/Common.h"
3
4namespace Nui::Graphics
5{
10 constexpr U32 CalculateCBSize(U32 size)
11 {
12 return ((size + 15) / 16) * 16;
13 }
14
19 template <typename T>
21 {
22 public:
24 : GPUWritable(false)
25 {
26 ZeroMemory(&Data, sizeof(T));
27 }
28
35 static ConstantBuffer<T> Create(ID3D11Device1* device, bool gpuWritable = false)
36 {
38
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;
43 desc.MiscFlags = 0;
44 desc.ByteWidth = static_cast<U32>(sizeof(T) + (16 - (sizeof(T) % 16)));
45
46 if (gpuWritable)
47 {
48 desc.Usage = D3D11_USAGE_DEFAULT;
49 desc.CPUAccessFlags = 0;
50 }
51 GPUWritable = gpuWritable;
52
53 DXCall(device->CreateBuffer(&desc, nullptr, cb.Buffer.ReleaseAndGetAddressOf()));
54
55 return cb;
56 }
57
62 void ApplyChanges(ID3D11DeviceContext1* ctx)
63 {
64 if (!Buffer)
65 throw std::runtime_error("Trying to apply changes on a constant buffer that was not created");
66
67 if (GPUWritable)
68 {
69 ctx->UpdateSubresource(Buffer.Get(), 0, nullptr, &Data, 0, 0);
70 }
71 else
72 {
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);
77 }
78 }
79
85 void SetVS(ID3D11DeviceContext1* ctx, U32 slot)
86 {
87 if (!Buffer)
88 throw std::runtime_error("Constant buffer was nullptr when trying to set for VS");
89
90 ID3D11Buffer* bufferArray[1];
91 bufferArray[0] = Buffer.Get();
92
93 ctx->VSSetConstantBuffers(slot, 1, bufferArray);
94 }
95
101 void SetPS(ID3D11DeviceContext1* ctx, U32 slot)
102 {
103 if (!Buffer)
104 throw std::runtime_error("Constant buffer was nullptr when trying to set for PS");
105
106 ID3D11Buffer* bufferArray[1];
107 bufferArray[0] = Buffer.Get();
108
109 ctx->PSSetConstantBuffers(slot, 1, bufferArray);
110 }
111
117 void SetCS(ID3D11DeviceContext1* ctx, U32 slot)
118 {
119 if (!Buffer)
120 throw std::runtime_error("Constant buffer was nullptr when trying to set for CS");
121
122 ID3D11Buffer* bufferArray[1];
123 bufferArray[0] = Buffer.Get();
124
125 ctx->CSSetConstantBuffers(slot, 1, bufferArray);
126 }
127
133 void SetGS(ID3D11DeviceContext1* ctx, U32 slot)
134 {
135 if (!Buffer)
136 throw std::runtime_error("Constant buffer was nullptr when trying to set for GS");
137
138 ID3D11Buffer* bufferArray[1];
139 bufferArray[0] = Buffer.Get();
140
141 ctx->GSSetConstantBuffers(slot, 1, bufferArray);
142 }
143
149 void SetHS(ID3D11DeviceContext1* ctx, U32 slot)
150 {
151 if (!Buffer)
152 throw std::runtime_error("Constant buffer was nullptr when trying to set for HS");
153
154 ID3D11Buffer* bufferArray[1];
155 bufferArray[0] = Buffer.Get();
156
157 ctx->HSSetConstantBuffers(slot, 1, bufferArray);
158 }
159
165 void SetDS(ID3D11DeviceContext1* ctx, U32 slot)
166 {
167 if (!Buffer)
168 throw std::runtime_error("Constant buffer was nullptr when trying to set for DS");
169
170 ID3D11Buffer* bufferArray[1];
171 bufferArray[0] = Buffer.Get();
172
173 ctx->DSSetConstantBuffers(slot, 1, bufferArray);
174 }
175
176 public:
177 T Data;
178 ComPtr<ID3D11Buffer> Buffer;
179 bool GPUWritable;
180 };
181}
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