@@ -5,6 +5,8 @@ ScreenQuad::ScreenQuad() {
55 this ->m_core = Core::GetInstance ();
66 this ->m_renderer = nullptr ;
77 this ->m_shader = nullptr ;
8+ this ->m_sceneMgr = SceneManager::GetInstance ();
9+ this ->m_nSqCBuffIndex = -1 ;
810}
911
1012void ScreenQuad::Init () {
@@ -83,26 +85,31 @@ void ScreenQuad::D3D12Init(D3D12* renderer) {
8385 CD3DX12_DESCRIPTOR_RANGE normalRange;
8486 CD3DX12_DESCRIPTOR_RANGE positionRange;
8587 CD3DX12_DESCRIPTOR_RANGE ORMRange;
88+ CD3DX12_DESCRIPTOR_RANGE cbuffRange;
8689
8790 albedoRange.Init (D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1 , 0 );
8891 normalRange.Init (D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1 , 1 );
8992 positionRange.Init (D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1 , 2 );
9093 ORMRange.Init (D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1 , 3 );
94+ cbuffRange.Init (D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1 , 0 );
9195
9296 CD3DX12_ROOT_PARAMETER albedoParam;
9397 CD3DX12_ROOT_PARAMETER normalParam;
9498 CD3DX12_ROOT_PARAMETER positionParam;
9599 CD3DX12_ROOT_PARAMETER ORMParam;
100+ CD3DX12_ROOT_PARAMETER cbuffParam;
96101 albedoParam.InitAsDescriptorTable (1 , &albedoRange, D3D12_SHADER_VISIBILITY_PIXEL);
97102 normalParam.InitAsDescriptorTable (1 , &normalRange, D3D12_SHADER_VISIBILITY_PIXEL);
98103 positionParam.InitAsDescriptorTable (1 , &positionRange, D3D12_SHADER_VISIBILITY_PIXEL);
99104 ORMParam.InitAsDescriptorTable (1 , &ORMRange, D3D12_SHADER_VISIBILITY_PIXEL);
105+ cbuffParam.InitAsDescriptorTable (1 , &cbuffRange, D3D12_SHADER_VISIBILITY_PIXEL);
100106
101107 D3D12_ROOT_PARAMETER rootParams[] = {
102108 albedoParam,
103109 normalParam,
104110 positionParam,
105- ORMParam
111+ ORMParam,
112+ cbuffParam
106113 };
107114
108115 D3D12_ROOT_SIGNATURE_DESC rootDesc = { };
@@ -157,6 +164,7 @@ void ScreenQuad::D3D12Init(D3D12* renderer) {
157164 plDesc.SampleMask = UINT32_MAX;
158165
159166 ThrowIfFailed (this ->m_dev ->CreateGraphicsPipelineState (&plDesc, IID_PPV_ARGS (this ->m_plState .GetAddressOf ())));
167+ this ->InitConstantBuffer ();
160168}
161169
162170void ScreenQuad::Render () {
@@ -165,11 +173,54 @@ void ScreenQuad::Render() {
165173 }
166174}
167175
176+ void ScreenQuad::InitConstantBuffer () {
177+ if (D3D12* renderer = dynamic_cast <D3D12*>(this ->m_renderer )) {
178+ Camera* currentCamera = this ->m_sceneMgr ->GetCurrentScene ()->GetCurrentCamera ();
179+
180+ this ->m_sqCBuffData .cameraPosition = XMFLOAT3 (
181+ currentCamera->transform .location .x ,
182+ currentCamera->transform .location .y ,
183+ currentCamera->transform .location .z
184+ );
185+
186+ UINT nConstantBufferSize = (sizeof (this ->m_sqCBuffData ) + 255 ) & ~255 ;
187+ renderer->CreateBuffer (&this ->m_sqCBuffData , nConstantBufferSize, m_sqCBuffer);
188+
189+ renderer->m_cbvSrvHeap ->Allocate (1 );
190+ this ->m_nSqCBuffIndex = renderer->m_cbvSrvHeap ->GetLastDescriptorIndex ();
191+ Descriptor sqBuffDesc = renderer->m_cbvSrvHeap ->GetDescriptor (this ->m_nSqCBuffIndex );
192+
193+ D3D12_CONSTANT_BUFFER_VIEW_DESC cbvDesc = { };
194+ cbvDesc.BufferLocation = this ->m_sqCBuffer ->GetGPUVirtualAddress ();
195+ cbvDesc.SizeInBytes = nConstantBufferSize;
196+ this ->m_dev ->CreateConstantBufferView (&cbvDesc, sqBuffDesc.cpuHandle );
197+ }
198+ }
199+
200+ void ScreenQuad::UpdateConstantBuffer () {
201+ Camera* currentCamera = this ->m_sceneMgr ->GetCurrentScene ()->GetCurrentCamera ();
202+
203+ this ->m_sqCBuffData .cameraPosition = XMFLOAT3 (
204+ currentCamera->transform .location .x ,
205+ currentCamera->transform .location .y ,
206+ currentCamera->transform .location .z
207+ );
208+
209+ UINT nConstantBufferSize = (sizeof (this ->m_sqCBuffData ) + 255 ) & ~255 ;
210+
211+ PVOID pData;
212+ ThrowIfFailed (this ->m_sqCBuffer ->Map (0 , nullptr , &pData));
213+ memcpy (pData, &this ->m_sqCBuffData , nConstantBufferSize);
214+ this ->m_sqCBuffer ->Unmap (0 , nullptr );
215+ }
216+
168217void ScreenQuad::D3D12Render (D3D12* renderer) {
218+ this ->UpdateConstantBuffer ();
169219 Descriptor albedoDesc = renderer->m_cbvSrvHeap ->GetDescriptor (0 );
170220 Descriptor normalDesc = renderer->m_cbvSrvHeap ->GetDescriptor (1 );
171221 Descriptor positionDesc = renderer->m_cbvSrvHeap ->GetDescriptor (2 );
172222 Descriptor materialDesc = renderer->m_cbvSrvHeap ->GetDescriptor (3 );
223+ Descriptor sqBuffDesc = renderer->m_cbvSrvHeap ->GetDescriptor (this ->m_nSqCBuffIndex );
173224 this ->m_list ->OMSetRenderTargets (1 , &this ->m_rtvDescriptor .cpuHandle , FALSE , nullptr );
174225 this ->m_list ->SetPipelineState (this ->m_plState .Get ());
175226 this ->m_list ->ClearRenderTargetView (this ->m_rtvDescriptor .cpuHandle , RGBA{ 0 .f , 0 .f , 0 .f , 1 .f }, 0 , nullptr );
@@ -183,6 +234,8 @@ void ScreenQuad::D3D12Render(D3D12* renderer) {
183234 this ->m_list ->SetGraphicsRootDescriptorTable (1 , normalDesc.gpuHandle );
184235 this ->m_list ->SetGraphicsRootDescriptorTable (2 , positionDesc.gpuHandle );
185236 this ->m_list ->SetGraphicsRootDescriptorTable (3 , materialDesc.gpuHandle );
237+ this ->m_list ->SetGraphicsRootDescriptorTable (3 , materialDesc.gpuHandle );
238+ this ->m_list ->SetGraphicsRootDescriptorTable (4 , sqBuffDesc.gpuHandle );
186239
187240 this ->m_list ->DrawIndexedInstanced (this ->m_indices .size (), 1 , 0 , 0 , 0 );
188241}
0 commit comments