Skip to content

Commit b475ef6

Browse files
author
Floyd Huizinga
committed
Fix up 1-3-2 and later with changes to ShaderCollection and refactor into nonabstraction
1 parent b84c663 commit b475ef6

13 files changed

+74
-166
lines changed

src/Cpp/1-getting-started/1-3-2-Texturing/1-3-2-Texturing.vcxproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,12 @@ xcopy /Y $(ProjectDir)Assets\Textures\*.* $(OutDir)Assets\Textures\</Command>
116116
</PostBuildEvent>
117117
</ItemDefinitionGroup>
118118
<ItemGroup>
119-
<ClCompile Include="DeviceContext.cpp" />
120119
<ClCompile Include="Main.cpp" />
121120
<ClCompile Include="ShaderCollection.cpp" />
122121
<ClCompile Include="TexturingApplication.cpp" />
123122
</ItemGroup>
124123
<ItemGroup>
125124
<ClInclude Include="Definitions.hpp" />
126-
<ClInclude Include="DeviceContext.hpp" />
127125
<ClInclude Include="ShaderCollection.hpp" />
128126
<ClInclude Include="TexturingApplication.hpp" />
129127
<ClInclude Include="VertexType.hpp" />

src/Cpp/1-getting-started/1-3-2-Texturing/1-3-2-Texturing.vcxproj.filters

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
<ClCompile Include="TexturingApplication.cpp">
2222
<Filter>Source Files</Filter>
2323
</ClCompile>
24-
<ClCompile Include="DeviceContext.cpp">
25-
<Filter>Source Files</Filter>
26-
</ClCompile>
2724
<ClCompile Include="ShaderCollection.cpp">
2825
<Filter>Source Files</Filter>
2926
</ClCompile>
@@ -38,9 +35,6 @@
3835
<ClInclude Include="VertexType.hpp">
3936
<Filter>Header Files</Filter>
4037
</ClInclude>
41-
<ClInclude Include="DeviceContext.hpp">
42-
<Filter>Header Files</Filter>
43-
</ClInclude>
4438
<ClInclude Include="ShaderCollection.hpp">
4539
<Filter>Header Files</Filter>
4640
</ClInclude>

src/Cpp/1-getting-started/1-3-2-Texturing/DeviceContext.cpp

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/Cpp/1-getting-started/1-3-2-Texturing/DeviceContext.hpp

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/Cpp/1-getting-started/1-3-2-Texturing/TexturingApplication.cpp

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "TexturingApplication.hpp"
2-
#include "DeviceContext.hpp"
3-
#include "Pipeline.hpp"
4-
#include "PipelineFactory.hpp"
2+
#include "ShaderCollection.hpp"
53

64
#include <GLFW/glfw3.h>
75
#define GLFW_EXPOSE_NATIVE_WIN32
@@ -44,12 +42,13 @@ TexturingApplication::~TexturingApplication()
4442
_deviceContext->Flush();
4543
_textureSrv.Reset();
4644
_triangleVertices.Reset();
47-
_pipeline.reset();
48-
_pipelineFactory.reset();
45+
_linearSamplerState.Reset();
46+
_rasterState.Reset();
47+
_shaderCollection.Destroy();
4948
DestroySwapchainResources();
5049
_swapChain.Reset();
5150
_dxgiFactory.Reset();
52-
_deviceContext.reset();
51+
_deviceContext.Reset();
5352
#if !defined(NDEBUG)
5453
_debug->ReportLiveDeviceObjects(D3D11_RLDO_FLAGS::D3D11_RLDO_DETAIL);
5554
_debug.Reset();
@@ -101,12 +100,12 @@ bool TexturingApplication::Initialize()
101100
}
102101
#endif
103102

103+
_deviceContext = deviceContext;
104+
104105
constexpr char deviceName[] = "DEV_Main";
105106
_device->SetPrivateData(WKPDID_D3DDebugObjectName, sizeof(deviceName), deviceName);
106107
SetDebugName(deviceContext.Get(), "CTX_Main");
107108

108-
_deviceContext = std::make_unique<DeviceContext>(std::move(deviceContext));
109-
110109
DXGI_SWAP_CHAIN_DESC1 swapChainDescriptor = {};
111110
swapChainDescriptor.Width = GetWindowWidth();
112111
swapChainDescriptor.Height = GetWindowHeight();
@@ -136,8 +135,6 @@ bool TexturingApplication::Initialize()
136135

137136
CreateSwapchainResources();
138137

139-
_pipelineFactory = std::make_unique<PipelineFactory>(_device);
140-
141138
FreeImage_Initialise();
142139
return true;
143140
}
@@ -296,21 +293,11 @@ WRL::ComPtr<ID3D11ShaderResourceView> CreateTextureView(ID3D11Device* device, co
296293

297294
bool TexturingApplication::Load()
298295
{
299-
PipelineDescriptor pipelineDescriptor = {};
300-
pipelineDescriptor.VertexFilePath = L"Assets/Shaders/Main.vs.hlsl";
301-
pipelineDescriptor.PixelFilePath = L"Assets/Shaders/Main.ps.hlsl";
302-
pipelineDescriptor.VertexType = VertexType::PositionColorUv;
303-
if (!_pipelineFactory->CreatePipeline(pipelineDescriptor, _pipeline))
304-
{
305-
std::cout << "PipelineFactory: Failed to create pipeline\n";
306-
return false;
307-
}
308-
309-
_pipeline->SetViewport(
310-
0.0f,
311-
0.0f,
312-
static_cast<float>(GetWindowWidth()),
313-
static_cast<float>(GetWindowHeight()));
296+
ShaderCollectionDescriptor shaderCollectionDescriptor = {};
297+
shaderCollectionDescriptor.VertexShaderFilePath = L"Assets/Shaders/Main.vs.hlsl";
298+
shaderCollectionDescriptor.PixelShaderFilePath = L"Assets/Shaders/Main.ps.hlsl";
299+
shaderCollectionDescriptor.VertexType = VertexType::PositionColorUv;
300+
_shaderCollection = ShaderCollection::CreateShaderCollection(shaderCollectionDescriptor, _device.Get());
314301

315302
constexpr VertexPositionColorUv vertices[] = {
316303
{ Position{ 0.0f, 0.5f, 0.0f }, Color{ 0.25f, 0.39f, 0.19f }, Uv{ 0.5f, 0.0f }},
@@ -344,8 +331,6 @@ bool TexturingApplication::Load()
344331
_textureSrv = _fallbackTextureSrv;
345332
}
346333

347-
_pipeline->BindTexture(0, _textureSrv.Get());
348-
349334
D3D11_SAMPLER_DESC linearSamplerStateDescriptor = {};
350335
linearSamplerStateDescriptor.Filter = D3D11_FILTER::D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT;
351336
linearSamplerStateDescriptor.AddressU = D3D11_TEXTURE_ADDRESS_MODE::D3D11_TEXTURE_ADDRESS_WRAP;
@@ -357,7 +342,6 @@ bool TexturingApplication::Load()
357342
return false;
358343
}
359344

360-
_pipeline->BindSampler(0, _linearSamplerState.Get());
361345

362346
return true;
363347
}
@@ -423,9 +407,43 @@ void TexturingApplication::Render()
423407
float clearColor[] = { 0.1f, 0.1f, 0.1f, 1.0f };
424408
constexpr uint32_t vertexOffset = 0;
425409

426-
_deviceContext->Clear(_renderTarget.Get(), clearColor);
427-
_deviceContext->SetPipeline(_pipeline.get());
428-
_deviceContext->SetVertexBuffer(_triangleVertices.Get(), vertexOffset);
429-
_deviceContext->Draw();
410+
ID3D11RenderTargetView* nullTarget = nullptr;
411+
412+
//set to 0 so we can clear properly
413+
_deviceContext->OMSetRenderTargets(1, &nullTarget, nullptr);
414+
_deviceContext->ClearRenderTargetView(_renderTarget.Get(), clearColor);
415+
416+
_deviceContext->OMSetRenderTargets(1, _renderTarget.GetAddressOf(), nullptr);
417+
418+
_deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY::D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
419+
420+
D3D11_BUFFER_DESC description = {};
421+
_triangleVertices->GetDesc(&description);
422+
UINT stride = sizeof(VertexPositionColorUv);
423+
_deviceContext->IASetVertexBuffers(
424+
0,
425+
1,
426+
_triangleVertices.GetAddressOf(),
427+
&stride,
428+
&vertexOffset);
429+
430+
_shaderCollection.ApplyToContext(_deviceContext.Get());
431+
432+
D3D11_VIEWPORT viewport = {
433+
0.0f,
434+
0.0f,
435+
static_cast<float>(GetWindowWidth()),
436+
static_cast<float>(GetWindowHeight()),
437+
0.0f,
438+
1.0f
439+
};
440+
441+
_deviceContext->RSSetViewports(1, &viewport);
442+
_deviceContext->RSSetState(_rasterState.Get());
443+
444+
_deviceContext->PSSetShaderResources(0, 1, _textureSrv.GetAddressOf());
445+
_deviceContext->PSSetSamplers(0, 1, _linearSamplerState.GetAddressOf());
446+
447+
_deviceContext->Draw(3, 0);
430448
_swapChain->Present(1, 0);
431449
}

src/Cpp/1-getting-started/1-3-2-Texturing/TexturingApplication.hpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
#include <d3d11_2.h>
77

88
#include <memory>
9-
10-
class DeviceContext;
11-
class Pipeline;
12-
class PipelineFactory;
9+
#include "ShaderCollection.hpp"
1310

1411
class TexturingApplication final : public Application
1512
{
@@ -30,18 +27,19 @@ class TexturingApplication final : public Application
3027
bool CreateSwapchainResources();
3128
void DestroySwapchainResources();
3229

33-
std::unique_ptr<DeviceContext> _deviceContext = nullptr;
34-
std::unique_ptr<Pipeline> _pipeline = nullptr;
35-
std::unique_ptr<PipelineFactory> _pipelineFactory = nullptr;
3630

3731
WRL::ComPtr<ID3D11Device> _device = nullptr;
32+
WRL::ComPtr<ID3D11DeviceContext> _deviceContext = nullptr;
3833
WRL::ComPtr<IDXGIFactory2> _dxgiFactory = nullptr;
3934
WRL::ComPtr<IDXGISwapChain1> _swapChain = nullptr;
4035
WRL::ComPtr<ID3D11RenderTargetView> _renderTarget = nullptr;
36+
WRL::ComPtr<ID3D11RasterizerState> _rasterState = nullptr;
4137
WRL::ComPtr<ID3D11Buffer> _triangleVertices = nullptr;
4238
WRL::ComPtr<ID3D11Debug> _debug = nullptr;
4339

4440
WRL::ComPtr<ID3D11SamplerState> _linearSamplerState = nullptr;
4541
WRL::ComPtr<ID3D11ShaderResourceView> _textureSrv = nullptr;
4642
WRL::ComPtr<ID3D11ShaderResourceView> _fallbackTextureSrv = nullptr;
43+
44+
ShaderCollection _shaderCollection;
4745
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ void Setting3DApplication::Render()
505505
&stride,
506506
&vertexOffset);
507507

508-
_shaderCollection.Set(_deviceContext.Get());
508+
_shaderCollection.ApplyToContext(_deviceContext.Get());
509509

510510
D3D11_VIEWPORT viewport = {
511511
0.0f,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class ShaderCollection
5252
WRL::ComPtr<ID3D11VertexShader> _vertexShader = nullptr;
5353
WRL::ComPtr<ID3D11PixelShader> _pixelShader = nullptr;
5454
WRL::ComPtr<ID3D11InputLayout> _inputLayout = nullptr;
55-
D3D11_PRIMITIVE_TOPOLOGY _primitiveTopology = {};*
55+
D3D11_PRIMITIVE_TOPOLOGY _primitiveTopology = {};
5656
uint32_t _vertexSize = 0;
5757
static std::unordered_map<VertexType, std::vector<D3D11_INPUT_ELEMENT_DESC>> _layoutMap;
5858
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ void Rendering3DApplication::Render()
429429
0
430430
);
431431

432-
_shaderCollection.Set(_deviceContext.Get());
432+
_shaderCollection.ApplyToContext(_deviceContext.Get());
433433

434434
D3D11_VIEWPORT viewport = {
435435
0.0f,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,14 @@ xcopy /Y $(ProjectDir)Assets\Textures\*.* $(OutDir)Assets\Textures\</Command>
117117
<ItemGroup>
118118
<ClCompile Include="Main.cpp" />
119119
<ClCompile Include="LoadingMeshesApplication.cpp" />
120+
<ClCompile Include="ShaderCollection.cpp" />
120121
<ClCompile Include="TextureFactory.cpp" />
121122
</ItemGroup>
122123
<ItemGroup>
123124
<ClInclude Include="Definitions.hpp" />
124125
<ClInclude Include="LoadingMeshesApplication.hpp" />
125126
<ClInclude Include="ResourceDescriptor.hpp" />
127+
<ClInclude Include="ShaderCollection.hpp" />
126128
<ClInclude Include="TextureFactory.hpp" />
127129
<ClInclude Include="VertexType.hpp" />
128130
</ItemGroup>

0 commit comments

Comments
 (0)