@@ -35,6 +35,12 @@ Rendering3DApplication::~Rendering3DApplication()
3535 _textureSrv.Reset ();
3636 _cubeIndices.Reset ();
3737 _cubeVertices.Reset ();
38+ _perFrameConstantBuffer.Reset ();
39+ _perObjectConstantBuffer.Reset ();
40+ _rasterState.Reset ();
41+ _depthState.Reset ();
42+ _depthTarget.Reset ();
43+ _shaderCollection.Destroy ();
3844 DestroySwapchainResources ();
3945 _swapChain.Reset ();
4046 _dxgiFactory.Reset ();
@@ -125,11 +131,62 @@ bool Rendering3DApplication::Initialize()
125131 }
126132
127133 CreateSwapchainResources ();
128-
134+ CreateRasterState ();
135+ CreateDepthStencilView ();
136+ CreateDepthState ();
129137 CreateConstantBuffers ();
130138 return true ;
131139}
132140
141+ void Rendering3DApplication::CreateRasterState ()
142+ {
143+ D3D11_RASTERIZER_DESC rasterDesc{};
144+ rasterDesc.CullMode = D3D11_CULL_NONE;
145+ rasterDesc.FillMode = D3D11_FILL_SOLID;
146+
147+ _device->CreateRasterizerState (&rasterDesc, &_rasterState);
148+ }
149+
150+ void Rendering3DApplication::CreateDepthStencilView ()
151+ {
152+ D3D11_TEXTURE2D_DESC texDesc{};
153+ texDesc.Height = GetWindowHeight ();
154+ texDesc.Width = GetWindowWidth ();
155+ texDesc.ArraySize = 1 ;
156+ texDesc.SampleDesc .Count = 1 ;
157+ texDesc.MipLevels = 1 ;
158+ texDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
159+ texDesc.Format = DXGI_FORMAT_R32_TYPELESS;
160+
161+ ID3D11Texture2D* texture = nullptr ;
162+ if (FAILED (_device->CreateTexture2D (&texDesc, nullptr , &texture)))
163+ {
164+ std::cout << " DXGI: Failed to create texture for DepthStencilView\n " ;
165+ return ;
166+ }
167+
168+ D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc{};
169+ dsvDesc.Format = DXGI_FORMAT_D32_FLOAT;
170+ dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
171+ if (FAILED (_device->CreateDepthStencilView (texture, &dsvDesc, &_depthTarget)))
172+ {
173+ std::cout << " DXGI: Failed to create DepthStencilView\n " ;
174+ texture->Release ();
175+ return ;
176+ }
177+
178+ texture->Release ();
179+ }
180+
181+ void Rendering3DApplication::CreateDepthState ()
182+ {
183+ D3D11_DEPTH_STENCIL_DESC depthDesc{};
184+ depthDesc.DepthEnable = TRUE ;
185+ depthDesc.DepthFunc = D3D11_COMPARISON_FUNC::D3D11_COMPARISON_LESS;
186+ depthDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
187+
188+ _device->CreateDepthStencilState (&depthDesc, &_depthState);
189+ }
133190
134191void Rendering3DApplication::CreateConstantBuffers ()
135192{
@@ -193,7 +250,6 @@ bool Rendering3DApplication::Load()
193250 // Back
194251 4 , 6 , 7 ,
195252 7 , 5 , 4
196-
197253 };
198254
199255 D3D11_BUFFER_DESC bufferInfo = {};
@@ -226,12 +282,6 @@ bool Rendering3DApplication::Load()
226282 return false ;
227283 }
228284
229- D3D11_RASTERIZER_DESC rasterDesc{};
230- rasterDesc.CullMode = D3D11_CULL_NONE;
231- rasterDesc.FillMode = D3D11_FILL_SOLID;
232-
233- _device->CreateRasterizerState (&rasterDesc, &_rasterState);
234-
235285 return true ;
236286}
237287
@@ -268,10 +318,13 @@ void Rendering3DApplication::OnResize(
268318 const int32_t height)
269319{
270320 Application::OnResize (width, height);
321+
322+ ID3D11RenderTargetView* nullRTV = nullptr ;
323+ _deviceContext->OMSetRenderTargets (1 , &nullRTV, nullptr );
271324 _deviceContext->Flush ();
272325
273326 DestroySwapchainResources ();
274-
327+
275328 if (FAILED (_swapChain->ResizeBuffers (
276329 0 ,
277330 width,
@@ -284,6 +337,9 @@ void Rendering3DApplication::OnResize(
284337 }
285338
286339 CreateSwapchainResources ();
340+
341+ _depthTarget.Reset ();
342+ CreateDepthStencilView ();
287343}
288344
289345void Rendering3DApplication::Update ()
@@ -342,13 +398,15 @@ void Rendering3DApplication::Render()
342398 float clearColor[] = { 0 .1f , 0 .1f , 0 .1f , 1 .0f };
343399 constexpr uint32_t vertexOffset = 0 ;
344400
345- ID3D11RenderTargetView* nullTarget = nullptr ;
401+ ID3D11RenderTargetView* nullRTV = nullptr ;
402+
403+ // set to nullptr so we can clear properly
404+ _deviceContext->OMSetRenderTargets (1 , &nullRTV, nullptr );
346405
347- // set to 0 so we can clear properly
348- _deviceContext->OMSetRenderTargets (1 , &nullTarget, nullptr );
349406 _deviceContext->ClearRenderTargetView (_renderTarget.Get (), clearColor);
407+ _deviceContext->ClearDepthStencilView (_depthTarget.Get (), D3D11_CLEAR_FLAG::D3D11_CLEAR_DEPTH, 1 .0f , 0 );
350408
351- _deviceContext->OMSetRenderTargets (1 , _renderTarget.GetAddressOf (), nullptr );
409+ _deviceContext->OMSetRenderTargets (1 , _renderTarget.GetAddressOf (), _depthTarget. Get () );
352410
353411 _deviceContext->IASetPrimitiveTopology (D3D11_PRIMITIVE_TOPOLOGY::D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
354412
@@ -379,6 +437,7 @@ void Rendering3DApplication::Render()
379437
380438 _deviceContext->RSSetViewports (1 , &viewport);
381439 _deviceContext->RSSetState (_rasterState.Get ());
440+ _deviceContext->OMSetDepthStencilState (_depthState.Get (), 0 );
382441
383442 ID3D11Buffer* constantBuffers[2 ] =
384443 {
@@ -391,75 +450,3 @@ void Rendering3DApplication::Render()
391450 _deviceContext->DrawIndexed (36 , 0 , 0 );
392451 _swapChain->Present (1 , 0 );
393452}
394-
395- bool Rendering3DApplication::CreateDepthStencilStates ()
396- {
397- // D3D11_DEPTH_STENCIL_DESC depthStencilDescriptor = {};
398- // depthStencilDescriptor.DepthEnable = false;
399- // depthStencilDescriptor.DepthWriteMask = D3D11_DEPTH_WRITE_MASK::D3D11_DEPTH_WRITE_MASK_ALL;
400- // depthStencilDescriptor.DepthFunc = D3D11_COMPARISON_FUNC::D3D11_COMPARISON_LESS;
401- // depthStencilDescriptor.StencilEnable = false;
402- // if (FAILED(_device->CreateDepthStencilState(&depthStencilDescriptor, &_depthDisabledDepthStencilState)))
403- // {
404- // std::cout << "D3D11: Failed to create disabled depth stencil state\n";
405- // return false;
406- // }
407- //
408- // depthStencilDescriptor.DepthEnable = true;
409- // if (FAILED(_device->CreateDepthStencilState(&depthStencilDescriptor, &_depthEnabledLessDepthStencilState)))
410- // {
411- // std::cout << "D3D11: Failed to create enabled depth stencil state\n";
412- // return false;
413- // }
414- //
415- // depthStencilDescriptor.DepthFunc = D3D11_COMPARISON_FUNC::D3D11_COMPARISON_LESS_EQUAL;
416- // if (FAILED(_device->CreateDepthStencilState(&depthStencilDescriptor, &_depthEnabledLessEqualDepthStencilState)))
417- // {
418- // std::cout << "D3D11: Failed to create enabled depth stencil state\n";
419- // return false;
420- // }
421- //
422- // depthStencilDescriptor.DepthFunc = D3D11_COMPARISON_FUNC::D3D11_COMPARISON_ALWAYS;
423- // if (FAILED(_device->CreateDepthStencilState(&depthStencilDescriptor, &_depthEnabledAlwaysDepthStencilState)))
424- // {
425- // std::cout << "D3D11: Failed to create enabled depth stencil state\n";
426- // return false;
427- // }
428- //
429- // depthStencilDescriptor.DepthFunc = D3D11_COMPARISON_FUNC::D3D11_COMPARISON_NEVER;
430- // if (FAILED(_device->CreateDepthStencilState(&depthStencilDescriptor, &_depthEnabledNeverDepthStencilState)))
431- // {
432- // std::cout << "D3D11: Failed to create enabled depth stencil state\n";
433- // return false;
434- // }
435- //
436- // depthStencilDescriptor.DepthFunc = D3D11_COMPARISON_FUNC::D3D11_COMPARISON_GREATER;
437- // if (FAILED(_device->CreateDepthStencilState(&depthStencilDescriptor, &_depthEnabledGreaterDepthStencilState)))
438- // {
439- // std::cout << "D3D11: Failed to create enabled depth stencil state\n";
440- // return false;
441- // }
442- //
443- // depthStencilDescriptor.DepthFunc = D3D11_COMPARISON_FUNC::D3D11_COMPARISON_GREATER_EQUAL;
444- // if (FAILED(_device->CreateDepthStencilState(&depthStencilDescriptor, &_depthEnabledGreaterEqualDepthStencilState)))
445- // {
446- // std::cout << "D3D11: Failed to create enabled depth stencil state\n";
447- // return false;
448- // }
449- //
450- // depthStencilDescriptor.DepthFunc = D3D11_COMPARISON_FUNC::D3D11_COMPARISON_EQUAL;
451- // if (FAILED(_device->CreateDepthStencilState(&depthStencilDescriptor, &_depthEnabledEqualDepthStencilState)))
452- // {
453- // std::cout << "D3D11: Failed to create enabled depth stencil state\n";
454- // return false;
455- // }
456- //
457- // depthStencilDescriptor.DepthFunc = D3D11_COMPARISON_FUNC::D3D11_COMPARISON_NOT_EQUAL;
458- // if (FAILED(_device->CreateDepthStencilState(&depthStencilDescriptor, &_depthEnabledNotEqualDepthStencilState)))
459- // {
460- // std::cout << "D3D11: Failed to create enabled depth stencil state\n";
461- // return false;
462- // }
463-
464- return true ;
465- }
0 commit comments