Skip to content

Commit 74d540a

Browse files
committed
ConstantBuffers implemented
1 parent fbf6198 commit 74d540a

File tree

5 files changed

+85
-33
lines changed

5 files changed

+85
-33
lines changed

src/Shader/GBufferPass.hlsl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
cbuffer WVP : register(b0)
2+
{
3+
matrix World;
4+
matrix View;
5+
matrix Projection;
6+
};
7+
18
struct VertexOutput
29
{
310
float4 position : SV_Position;
@@ -12,8 +19,10 @@ VertexOutput VertexMain(float4 position : POSITION0, float4 normal : NORMAL0, fl
1219
{
1320
VertexOutput output;
1421

15-
output.position = position;
16-
output.normal = normal;
22+
output.position = mul(position, World);
23+
output.position = mul(output.position, View);
24+
output.position = mul(output.position, Projection);
25+
output.normal = mul(normal, World);
1726
output.uv = uv;
1827

1928
return output;

src/private/Core/GameObject/Component/Mesh.cpp

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Mesh::Mesh(std::string name, Transform& parentTransform) : Component::Component(
1414
this->m_bMeshLoaded = false;
1515
this->m_nTotalVertices = 0;
1616
this->m_nSamplerIndex = -1; // I put -1 cuz 0 can actually be occupied.
17+
this->m_nWvpIndex = -1;
1718
this->m_nTotalVertices = 0;
1819
this->m_shader = nullptr;
1920

@@ -27,8 +28,8 @@ Mesh::Mesh(std::string name, Transform& parentTransform) : Component::Component(
2728
this->m_wvp.World *= XMMatrixTranspose(XMMatrixRotationY(XMConvertToRadians(this->m_transform.rotation.y)));
2829
this->m_wvp.World *= XMMatrixTranspose(XMMatrixRotationZ(XMConvertToRadians(this->m_transform.rotation.z)));
2930

30-
this->m_wvp.View = XMMatrixTranspose(XMMatrixIdentity());
31-
this->m_wvp.Projection = XMMatrixTranspose(XMMatrixPerspectiveLH(XMConvertToRadians(90.f), static_cast<float>(nWidth) / static_cast<float>(nHeight), 0.01f, 3000.f));
31+
this->m_wvp.View = XMMatrixTranspose(XMMatrixIdentity() * XMMatrixTranslation(0.f, 0.f, 2.f));
32+
this->m_wvp.Projection = XMMatrixTranspose(XMMatrixPerspectiveFovLH(XMConvertToRadians(90.f), static_cast<float>(nWidth) / static_cast<float>(nHeight), 0.01f, 3000.f));
3233
}
3334

3435
void Mesh::Init() {
@@ -80,17 +81,58 @@ void Mesh::UploadVertices() {
8081
}
8182
}
8283

84+
void Mesh::InitConstantBuffer() {
85+
D3D12* d3d12 = dynamic_cast<D3D12*>(this->m_renderer);
86+
87+
UINT nWVPSize = (sizeof(this->m_wvp) + 255) & ~255;
88+
89+
d3d12->CreateBuffer(&this->m_wvp, nWVPSize, this->m_wvpRes);
90+
this->m_wvpRes->SetName(L"Mesh WVP Constant buffer");
91+
92+
D3D12_CONSTANT_BUFFER_VIEW_DESC cbvDesc = { };
93+
cbvDesc.BufferLocation = this->m_wvpRes->GetGPUVirtualAddress();
94+
cbvDesc.SizeInBytes = nWVPSize;
95+
96+
this->m_cbv_srvHeap->Allocate(1);
97+
this->m_nWvpIndex = this->m_cbv_srvHeap->GetLastDescriptorIndex();
98+
Descriptor wvpDesc = this->m_cbv_srvHeap->GetDescriptor(this->m_nWvpIndex);
99+
100+
this->m_dev->CreateConstantBufferView(&cbvDesc, wvpDesc.cpuHandle);
101+
}
102+
103+
void Mesh::UpdateConstantBuffer() {
104+
UINT nWVPSize = (sizeof(this->m_wvp) + 255) & ~255;
105+
this->m_wvp.World = XMMatrixTranspose(XMMatrixIdentity() * XMMatrixTranslation(this->m_transform.location.x, this->m_transform.location.y, this->m_transform.location.z));
106+
this->m_wvp.World *= XMMatrixTranspose(XMMatrixRotationX(XMConvertToRadians(this->m_transform.rotation.x)));
107+
this->m_wvp.World *= XMMatrixTranspose(XMMatrixRotationY(XMConvertToRadians(this->m_transform.rotation.y)));
108+
this->m_wvp.World *= XMMatrixTranspose(XMMatrixRotationZ(XMConvertToRadians(this->m_transform.rotation.z)));
109+
110+
this->m_wvp.View = XMMatrixTranspose(XMMatrixIdentity() * XMMatrixTranslation(0.f, 0.f, 2.f));
111+
//this->m_wvp.Projection = XMMatrixTranspose(XMMatrixPerspectiveLH(XMConvertToRadians(90.f), static_cast<float>(nWidth) / static_cast<float>(nHeight), 0.01f, 3000.f));
112+
113+
PVOID pData;
114+
ThrowIfFailed(this->m_wvpRes->Map(0, nullptr, &pData));
115+
memcpy(pData, &this->m_wvp, nWVPSize);
116+
this->m_wvpRes->Unmap(0, nullptr);
117+
118+
return;
119+
}
120+
83121
void Mesh::Update() {
84122
Component::Update();
85123

86124
}
87125

88126
void Mesh::Render() {
127+
this->UpdateConstantBuffer();
89128
this->m_list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
90129
this->m_list->SetPipelineState(this->m_plState.Get());
91130
this->m_list->SetGraphicsRootSignature(this->m_rootSig.Get());
92131

132+
Descriptor wvpDesc = this->m_cbv_srvHeap->GetDescriptor(this->m_nWvpIndex);
133+
93134
this->m_list->SetGraphicsRootDescriptorTable(0, this->m_samplerDescriptor.gpuHandle);
135+
this->m_list->SetGraphicsRootDescriptorTable(2, wvpDesc.gpuHandle);
94136

95137
int i = 0;
96138
for (D3D12_VERTEX_BUFFER_VIEW vbv : this->m_VBVs) {
@@ -114,17 +156,22 @@ void Mesh::InitPipeline() {
114156

115157
CD3DX12_DESCRIPTOR_RANGE albedoRange;
116158
CD3DX12_DESCRIPTOR_RANGE samplerRange;
159+
CD3DX12_DESCRIPTOR_RANGE wvpRange;
117160
albedoRange.Init(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, 0);
118161
samplerRange.Init(D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER, 1, 0);
162+
wvpRange.Init(D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1, 0);
119163

120164
CD3DX12_ROOT_PARAMETER albedoParam;
121165
CD3DX12_ROOT_PARAMETER samplerParam;
166+
CD3DX12_ROOT_PARAMETER wvpParam;
122167
albedoParam.InitAsDescriptorTable(1, &albedoRange, D3D12_SHADER_VISIBILITY_PIXEL);
123168
samplerParam.InitAsDescriptorTable(1, &samplerRange, D3D12_SHADER_VISIBILITY_PIXEL);
124-
169+
wvpParam.InitAsDescriptorTable(1, &wvpRange, D3D12_SHADER_VISIBILITY_VERTEX);
170+
125171
D3D12_ROOT_PARAMETER rootParams[] = {
126172
samplerParam,
127-
albedoParam
173+
albedoParam,
174+
wvpParam
128175
};
129176

130177
D3D12_ROOT_SIGNATURE_DESC rootDesc = { };
@@ -169,7 +216,7 @@ void Mesh::InitPipeline() {
169216
plDesc.DSVFormat = DXGI_FORMAT_D24_UNORM_S8_UINT;
170217
plDesc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
171218
plDesc.RasterizerState.CullMode = D3D12_CULL_MODE_BACK;
172-
plDesc.RasterizerState.FrontCounterClockwise = TRUE;
219+
plDesc.RasterizerState.FrontCounterClockwise = FALSE;
173220
plDesc.RTVFormats[0] = DXGI_FORMAT_B8G8R8A8_UNORM;
174221
plDesc.RTVFormats[1] = DXGI_FORMAT_B8G8R8A8_UNORM;
175222
plDesc.RTVFormats[2] = DXGI_FORMAT_B8G8R8A8_UNORM;
@@ -194,6 +241,7 @@ void Mesh::D3D12Init(D3D12* renderer) {
194241
this->UploadVertices();
195242
this->InitPipeline();
196243
this->InitSampler(renderer);
244+
this->InitConstantBuffer();
197245
}
198246

199247
void Mesh::InitSampler(D3D12* renderer) {

src/private/Core/GameObject/GameObject.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ GameObject::GameObject(std::string name) {
66
}
77

88
void GameObject::Init() {
9+
//this->transform.Translate(0.f, 0.f, 0.f);
910
this->m_mesh = new Mesh("StaticMeshComponent", this->transform);
1011
this->m_components.push_back(this->m_mesh);
1112
this->m_mesh->LoadModel("f16.fbx");

src/private/Core/Renderer/ScreenQuad.cpp

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -125,39 +125,29 @@ void ScreenQuad::D3D12Init(D3D12* renderer) {
125125
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, NULL}
126126
};
127127

128+
D3D12_INPUT_LAYOUT_DESC layout = { };
129+
layout.pInputElementDescs = elements;
130+
layout.NumElements = _countof(elements);
131+
128132
D3D12_GRAPHICS_PIPELINE_STATE_DESC plDesc = { };
133+
plDesc.Flags = D3D12_PIPELINE_STATE_FLAG_NONE;
134+
plDesc.DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC(D3D12_DEFAULT);
135+
plDesc.DepthStencilState.DepthEnable = TRUE;
136+
plDesc.DepthStencilState.StencilEnable = FALSE;
137+
plDesc.InputLayout = layout;
129138
plDesc.VS.pShaderBytecode = vShader;
130139
plDesc.VS.BytecodeLength = nVertexLength;
131140
plDesc.PS.pShaderBytecode = pShader;
132141
plDesc.PS.BytecodeLength = nPixelLength;
133142
plDesc.pRootSignature = this->m_rootSig.Get();
134-
plDesc.DepthStencilState.DepthEnable = FALSE;
135-
plDesc.DepthStencilState.StencilEnable = FALSE;
136-
plDesc.InputLayout.NumElements = _countof(elements);
137-
plDesc.InputLayout.pInputElementDescs = elements;
138-
plDesc.Flags = D3D12_PIPELINE_STATE_FLAG_NONE;
143+
plDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
144+
plDesc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
145+
plDesc.RasterizerState.CullMode = D3D12_CULL_MODE_BACK;
146+
plDesc.RTVFormats[0] = DXGI_FORMAT_B8G8R8A8_UNORM;
147+
plDesc.NumRenderTargets = 1;
139148
plDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
140-
plDesc.NodeMask = 0;
141149
plDesc.SampleDesc.Count = 8;
142150
plDesc.SampleMask = UINT32_MAX;
143-
plDesc.NumRenderTargets = 1;
144-
plDesc.RTVFormats[0] = DXGI_FORMAT_B8G8R8A8_UNORM;
145-
plDesc.RasterizerState.FillMode = D3D12_FILL_MODE_SOLID;
146-
plDesc.RasterizerState.CullMode = D3D12_CULL_MODE_BACK;
147-
plDesc.RasterizerState.ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF;
148-
plDesc.RasterizerState.FrontCounterClockwise = FALSE;
149-
plDesc.RasterizerState.DepthClipEnable = TRUE;
150-
151-
const D3D12_RENDER_TARGET_BLEND_DESC rtbDesc = {
152-
FALSE, FALSE,
153-
D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD,
154-
D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD,
155-
D3D12_LOGIC_OP_NOOP,
156-
D3D12_COLOR_WRITE_ENABLE_ALL,
157-
};
158-
159-
for (UINT i = 0; i < D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
160-
plDesc.BlendState.RenderTarget[i] = rtbDesc;
161151

162152
ThrowIfFailed(this->m_dev->CreateGraphicsPipelineState(&plDesc, IID_PPV_ARGS(this->m_plState.GetAddressOf())));
163153
}

src/public/Core/GameObject/Component/Mesh.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,16 @@ class Mesh : public Component {
6060

6161
UINT m_nTotalVertices;
6262

63+
ComPtr<ID3D12Resource> m_wvpRes;
64+
WVP m_wvp;
65+
UINT m_nWvpIndex;
66+
void InitConstantBuffer();
67+
void UpdateConstantBuffer();
68+
6369
void UploadVertices();
6470
void InitPipeline();
6571
void InitSampler(D3D12* renderer);
6672

67-
WVP m_wvp;
68-
6973
Transform m_transform;
7074
public:
7175
Mesh(std::string name, Transform& parentTransform);

0 commit comments

Comments
 (0)