-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstantBuffer.h
More file actions
66 lines (57 loc) · 1.47 KB
/
ConstantBuffer.h
File metadata and controls
66 lines (57 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#pragma once
#include "Bindable.h"
#include "NouException.h"
class ConstantBuffer : public Bindable
{
public:
ConstantBuffer(GraphicsD11& gfx, UINT size, void* data)
{
OutputDebugString(("ConstantBuffer size: " + std::to_string(size) + "\n").c_str());
D3D11_BUFFER_DESC bd = {};
bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
bd.Usage = D3D11_USAGE_DYNAMIC;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
bd.MiscFlags = 0u;
bd.ByteWidth = size;
bd.StructureByteStride = 0u;
D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = data;
HRESULT res = GetDevice(gfx)->CreateBuffer(&bd, &sd, &pConstBuffer);
CHECK_HR_EXCEPT();
}
void Update(GraphicsD11& gfx, UINT size, void* data)
{
HRESULT res;
D3D11_MAPPED_SUBRESOURCE sr;
res = GetContext(gfx)->Map(
pConstBuffer.Get(),
0u,
D3D11_MAP_WRITE_DISCARD,
0u,
&sr
);
CHECK_HR_EXCEPT();
memcpy(sr.pData, data, size);
GetContext(gfx)->Unmap(pConstBuffer.Get(), 0u);
}
protected:
Microsoft::WRL::ComPtr<ID3D11Buffer> pConstBuffer;
};
class VertexConstantBuffer : public ConstantBuffer
{
public:
using ConstantBuffer::ConstantBuffer;
void Bind(GraphicsD11& gfx) noexcept override
{
this->GetContext(gfx)->VSSetConstantBuffers(0u, 1u, pConstBuffer.GetAddressOf());
}
};
class FragmentConstantbuffer : public ConstantBuffer
{
public:
using ConstantBuffer::ConstantBuffer;
void Bind(GraphicsD11& gfx) noexcept override
{
this->GetContext(gfx)->PSSetConstantBuffers(0u, 1u, pConstBuffer.GetAddressOf());
}
};