Skip to content

Commit beecfa3

Browse files
author
Floyd Huizinga
committed
proper resource cleanup, finish up code for "3D Rendering", remove old project
1 parent ece810d commit beecfa3

File tree

9 files changed

+109
-97
lines changed

9 files changed

+109
-97
lines changed

src/Cpp/1-getting-started/1-3-3-SettingUp3DRendering/Setting3DApplication.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ Setting3DApplication::~Setting3DApplication()
4242
_deviceContext->Flush();
4343
_textureSrv.Reset();
4444
_triangleVertices.Reset();
45+
_perFrameConstantBuffer.Reset();
46+
_perObjectConstantBuffer.Reset();
47+
_linearSamplerState.Reset();
48+
_rasterState.Reset();
49+
_shaderCollection.Destroy();
4550
DestroySwapchainResources();
4651
_swapChain.Reset();
4752
_dxgiFactory.Reset();

src/Cpp/1-getting-started/1-3-3-SettingUp3DRendering/ShaderCollection.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,10 @@ void ShaderCollection::Set(ID3D11DeviceContext* context)
188188
context->VSSetShader(_vertexShader.Get(), nullptr, 0);
189189
context->PSSetShader(_pixelShader.Get(), nullptr, 0);
190190
}
191+
192+
void ShaderCollection::Destroy()
193+
{
194+
_vertexShader.Reset();
195+
_pixelShader.Reset();
196+
_inputLayout.Reset();
197+
}

src/Cpp/1-getting-started/1-3-3-SettingUp3DRendering/ShaderCollection.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@ struct ShaderCollectionDescriptor
1717
class ShaderCollection
1818
{
1919
public:
20+
2021
static ShaderCollection CreateShaderCollection(
2122
const ShaderCollectionDescriptor& settings, ID3D11Device* device);
23+
2224
static size_t GetLayoutByteSize(VertexType vertexType);
25+
2326
void Set(ID3D11DeviceContext* context);
27+
void Destroy();
28+
2429
private:
30+
2531
static [[nodiscard]] WRL::ComPtr<ID3D11VertexShader> CreateVertexShader(
2632
ID3D11Device* device,
2733
const std::wstring& filePath,

src/Cpp/1-getting-started/1-3-4-3DRendering/3DRenderingApplication.cpp

Lines changed: 72 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -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

134191
void 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

289345
void 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-
}

src/Cpp/1-getting-started/1-3-4-3DRendering/3DRenderingApplication.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ class Rendering3DApplication final : public Application
3636
void Render() override;
3737

3838
private:
39-
bool CreateDepthStencilStates();
39+
40+
void CreateRasterState();
41+
void CreateDepthStencilView();
42+
void CreateDepthState();
4043
void CreateConstantBuffers();
4144
bool CreateSwapchainResources();
4245
void DestroySwapchainResources();
@@ -46,7 +49,9 @@ class Rendering3DApplication final : public Application
4649
WRL::ComPtr<IDXGIFactory2> _dxgiFactory = nullptr;
4750
WRL::ComPtr<IDXGISwapChain1> _swapChain = nullptr;
4851
WRL::ComPtr<ID3D11RenderTargetView> _renderTarget = nullptr;
52+
WRL::ComPtr<ID3D11DepthStencilView> _depthTarget = nullptr;
4953
WRL::ComPtr<ID3D11RasterizerState> _rasterState = nullptr;
54+
WRL::ComPtr<ID3D11DepthStencilState> _depthState = nullptr;
5055
WRL::ComPtr<ID3D11Buffer> _cubeVertices = nullptr;
5156
WRL::ComPtr<ID3D11Buffer> _cubeIndices = nullptr;
5257
WRL::ComPtr<ID3D11Debug> _debug = nullptr;

src/Cpp/1-getting-started/1-3-4-3DRendering/ShaderCollection.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,10 @@ void ShaderCollection::Set(ID3D11DeviceContext* context)
188188
context->VSSetShader(_vertexShader.Get(), nullptr, 0);
189189
context->PSSetShader(_pixelShader.Get(), nullptr, 0);
190190
}
191+
192+
void ShaderCollection::Destroy()
193+
{
194+
_vertexShader.Reset();
195+
_pixelShader.Reset();
196+
_inputLayout.Reset();
197+
}

src/Cpp/1-getting-started/1-3-4-3DRendering/ShaderCollection.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ struct ShaderCollectionDescriptor
1717
class ShaderCollection
1818
{
1919
public:
20+
2021
static ShaderCollection CreateShaderCollection(
2122
const ShaderCollectionDescriptor& settings, ID3D11Device* device);
2223
static size_t GetLayoutByteSize(VertexType vertexType);
24+
2325
void Set(ID3D11DeviceContext* context);
26+
void Destroy();
27+
2428
private:
29+
2530
static [[nodiscard]] WRL::ComPtr<ID3D11VertexShader> CreateVertexShader(
2631
ID3D11Device* device,
2732
const std::wstring& filePath,

src/Cpp/1-getting-started/1-3-5-Models/1-3-5-Models.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
2222
<ConfigurationType>Application</ConfigurationType>
2323
<UseDebugLibraries>true</UseDebugLibraries>
24-
<PlatformToolset>v142</PlatformToolset>
2524
<CharacterSet>Unicode</CharacterSet>
25+
<PlatformToolset>v143</PlatformToolset>
2626
</PropertyGroup>
2727
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
2828
<ConfigurationType>Application</ConfigurationType>

src/LearnD3D11.sln

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "1-3-7-DearImGui", "Cpp\1-ge
7676
{A3B1AE74-436A-44B7-AD17-A0D19168773C} = {A3B1AE74-436A-44B7-AD17-A0D19168773C}
7777
EndProjectSection
7878
EndProject
79-
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "1-3-4-DepthBuffer", "Cpp\1-getting-started\1-3-4-DepthBuffer\1-3-4-DepthBuffer.vcxproj", "{B2D92817-E897-4797-9864-C1470F217F1E}"
80-
ProjectSection(ProjectDependencies) = postProject
81-
{A3B1AE74-436A-44B7-AD17-A0D19168773C} = {A3B1AE74-436A-44B7-AD17-A0D19168773C}
82-
EndProjectSection
83-
EndProject
8479
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "1-3-1-RasterizerState", "Cpp\1-getting-started\1-3-1-RasterizerState\1-3-1-RasterizerState.vcxproj", "{1B35D552-7FE9-43AD-B0BC-7375D96034A5}"
8580
ProjectSection(ProjectDependencies) = postProject
8681
{A3B1AE74-436A-44B7-AD17-A0D19168773C} = {A3B1AE74-436A-44B7-AD17-A0D19168773C}
@@ -154,10 +149,6 @@ Global
154149
{0989B526-EC66-4C1C-8121-4B9AC53E2C83}.Debug|x64.Build.0 = Debug|x64
155150
{0989B526-EC66-4C1C-8121-4B9AC53E2C83}.Release|x64.ActiveCfg = Release|x64
156151
{0989B526-EC66-4C1C-8121-4B9AC53E2C83}.Release|x64.Build.0 = Release|x64
157-
{B2D92817-E897-4797-9864-C1470F217F1E}.Debug|x64.ActiveCfg = Debug|x64
158-
{B2D92817-E897-4797-9864-C1470F217F1E}.Debug|x64.Build.0 = Debug|x64
159-
{B2D92817-E897-4797-9864-C1470F217F1E}.Release|x64.ActiveCfg = Release|x64
160-
{B2D92817-E897-4797-9864-C1470F217F1E}.Release|x64.Build.0 = Release|x64
161152
{1B35D552-7FE9-43AD-B0BC-7375D96034A5}.Debug|x64.ActiveCfg = Debug|x64
162153
{1B35D552-7FE9-43AD-B0BC-7375D96034A5}.Debug|x64.Build.0 = Debug|x64
163154
{1B35D552-7FE9-43AD-B0BC-7375D96034A5}.Release|x64.ActiveCfg = Release|x64
@@ -199,7 +190,6 @@ Global
199190
{A1F990E5-58A1-4345-AF2B-599F785519A7} = {69E36221-7B54-4891-AB39-B9F2C5025815}
200191
{1B3547F4-344F-41FC-8AB0-312AF6E01A96} = {125F76A8-AE74-44AE-B471-BE7C09EB5EB4}
201192
{0989B526-EC66-4C1C-8121-4B9AC53E2C83} = {125F76A8-AE74-44AE-B471-BE7C09EB5EB4}
202-
{B2D92817-E897-4797-9864-C1470F217F1E} = {125F76A8-AE74-44AE-B471-BE7C09EB5EB4}
203193
{1B35D552-7FE9-43AD-B0BC-7375D96034A5} = {125F76A8-AE74-44AE-B471-BE7C09EB5EB4}
204194
{B88367A4-CB5C-4FB1-9FF9-ADDA3D9D0AB2} = {69E36221-7B54-4891-AB39-B9F2C5025815}
205195
{FDA61AEE-067D-4C41-B526-A4F6BC85C44E} = {0AC134D5-97CD-4724-A717-1C0C8EC76F05}

0 commit comments

Comments
 (0)