Skip to content

Commit 172458c

Browse files
committed
Get rid of abstractions
1 parent b84c663 commit 172458c

File tree

8 files changed

+102
-223
lines changed

8 files changed

+102
-223
lines changed

src/Cpp/1-getting-started/1-3-1-RasterizerState/1-3-1-RasterizerState.vcxproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,12 @@
105105
</PostBuildEvent>
106106
</ItemDefinitionGroup>
107107
<ItemGroup>
108-
<ClCompile Include="DeviceContext.cpp" />
109108
<ClCompile Include="Main.cpp" />
110109
<ClCompile Include="RasterizerStateApplication.cpp" />
111110
<ClCompile Include="ShaderCollection.cpp" />
112111
</ItemGroup>
113112
<ItemGroup>
114113
<ClInclude Include="Definitions.hpp" />
115-
<ClInclude Include="DeviceContext.hpp" />
116114
<ClInclude Include="RasterizerStateApplication.hpp" />
117115
<ClInclude Include="ShaderCollection.hpp" />
118116
<ClInclude Include="VertexType.hpp" />

src/Cpp/1-getting-started/1-3-1-RasterizerState/1-3-1-RasterizerState.vcxproj.filters

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
<ClCompile Include="RasterizerStateApplication.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-1-RasterizerState/DeviceContext.cpp

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

src/Cpp/1-getting-started/1-3-1-RasterizerState/DeviceContext.hpp

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

src/Cpp/1-getting-started/1-3-1-RasterizerState/RasterizerStateApplication.cpp

Lines changed: 95 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "RasterizerStateApplication.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
@@ -40,12 +38,10 @@ RasterizerStateApplication::~RasterizerStateApplication()
4038
_solidFrameCullFrontRasterizerState.Reset();
4139
_solidFrameCullNoneRasterizerState.Reset();
4240

43-
_pipeline.reset();
44-
_pipelineFactory.reset();
41+
_shaderCollection.Destroy();
4542
DestroySwapchainResources();
4643
_swapChain.Reset();
4744
_dxgiFactory.Reset();
48-
_deviceContext.reset();
4945
#if !defined(NDEBUG)
5046
_debug->ReportLiveDeviceObjects(D3D11_RLDO_FLAGS::D3D11_RLDO_DETAIL);
5147
_debug.Reset();
@@ -102,7 +98,7 @@ bool RasterizerStateApplication::Initialize()
10298
_device->SetPrivateData(WKPDID_D3DDebugObjectName, sizeof(deviceName), deviceName);
10399
SetDebugName(deviceContext.Get(), "CTX_Main");
104100

105-
_deviceContext = std::make_unique<DeviceContext>(_device, std::move(deviceContext));
101+
_deviceContext = deviceContext;
106102

107103
DXGI_SWAP_CHAIN_DESC1 swapChainDescriptor = {};
108104
swapChainDescriptor.Width = GetWindowWidth();
@@ -133,8 +129,17 @@ bool RasterizerStateApplication::Initialize()
133129

134130
CreateSwapchainResources();
135131

136-
_pipelineFactory = std::make_unique<PipelineFactory>(_device);
132+
return true;
133+
}
134+
135+
bool RasterizerStateApplication::Load()
136+
{
137+
ShaderCollectionDescriptor shaderDescriptor = {};
138+
shaderDescriptor.VertexShaderFilePath = L"Assets/Shaders/Main.vs.hlsl";
139+
shaderDescriptor.PixelShaderFilePath = L"Assets/Shaders/Main.ps.hlsl";
140+
shaderDescriptor.VertexType = VertexType::PositionColor;
137141

142+
_shaderCollection = ShaderCollection::CreateShaderCollection(shaderDescriptor, _device.Get());
138143

139144
D3D11_BUFFER_DESC triangleBufferDesc{};
140145
triangleBufferDesc.BindFlags = D3D11_BIND_FLAG::D3D11_BIND_VERTEX_BUFFER;
@@ -144,11 +149,10 @@ bool RasterizerStateApplication::Initialize()
144149
float yPos = 1.0f;
145150
for (int i = 0; i < _triangleVertices.size(); i++)
146151
{
147-
VertexPositionColor vertices[] =
148-
{
149-
VertexPositionColor{Position{xPos + 0.5f, yPos - 0.25f, 1.0f}, Color{ 1.0f, 0.0f, 0.0f} },
150-
VertexPositionColor{Position{xPos + 0.33f, yPos - 0.5f, 1.0f}, Color{ 0.0f, 1.0f, 0.0f} },
151-
VertexPositionColor{Position{xPos + 0.66f, yPos - 0.5f, 1.0f}, Color{ 0.0f, 0.0f, 1.0f} },
152+
VertexPositionColor vertices[] = {
153+
VertexPositionColor{Position{ xPos + 0.5f, yPos - 0.25f, 1.0f }, Color{ 1.0f, 0.0f, 0.0f }},
154+
VertexPositionColor{Position{ xPos + 0.33f, yPos - 0.5f, 1.0f }, Color{ 0.0f, 1.0f, 0.0f }},
155+
VertexPositionColor{Position{ xPos + 0.66f, yPos - 0.5f, 1.0f }, Color{ 0.0f, 0.0f, 1.0f }},
152156
};
153157

154158
D3D11_SUBRESOURCE_DATA sd{};
@@ -164,28 +168,6 @@ bool RasterizerStateApplication::Initialize()
164168
}
165169
}
166170

167-
168-
return true;
169-
}
170-
171-
bool RasterizerStateApplication::Load()
172-
{
173-
PipelineDescriptor pipelineDescriptor = {};
174-
pipelineDescriptor.VertexFilePath = L"Assets/Shaders/Main.vs.hlsl";
175-
pipelineDescriptor.PixelFilePath = L"Assets/Shaders/Main.ps.hlsl";
176-
pipelineDescriptor.VertexType = VertexType::PositionColor;
177-
if (!_pipelineFactory->CreatePipeline(pipelineDescriptor, _pipeline))
178-
{
179-
std::cout << "PipelineFactory: Failed to create pipeline\n";
180-
return false;
181-
}
182-
183-
_pipeline->SetViewport(
184-
0.0f,
185-
0.0f,
186-
static_cast<float>(GetWindowWidth()),
187-
static_cast<float>(GetWindowHeight()));
188-
189171
if (!CreateRasterizerStates())
190172
{
191173
return false;
@@ -253,40 +235,85 @@ void RasterizerStateApplication::Update()
253235
void RasterizerStateApplication::Render()
254236
{
255237
float clearColor[] = { 0.1f, 0.1f, 0.1f, 1.0f };
238+
ID3D11RenderTargetView* nullRTV = nullptr;
239+
constexpr uint32_t vertexOffset = 0;
256240

257-
_deviceContext->Clear(
258-
_renderTarget.Get(),
259-
clearColor);
260-
261-
_pipeline->SetRasterizerState(_solidFrameCullNoneRasterizerState.Get());
262-
_deviceContext->SetPipeline(_pipeline.get());
263-
_deviceContext->SetVertexBuffer(_triangleVertices[0].Get(), 0);
264-
_deviceContext->Draw();
265-
266-
_pipeline->SetRasterizerState(_solidFrameCullFrontRasterizerState.Get());
267-
_deviceContext->SetPipeline(_pipeline.get());
268-
_deviceContext->SetVertexBuffer(_triangleVertices[1].Get(), 0);
269-
_deviceContext->Draw();
270-
271-
_pipeline->SetRasterizerState(_solidFrameCullBackRasterizerState.Get());
272-
_deviceContext->SetPipeline(_pipeline.get());
273-
_deviceContext->SetVertexBuffer(_triangleVertices[2].Get(), 0);
274-
_deviceContext->Draw();
275-
276-
_pipeline->SetRasterizerState(_wireFrameCullNoneRasterizerState.Get());
277-
_deviceContext->SetPipeline(_pipeline.get());
278-
_deviceContext->SetVertexBuffer(_triangleVertices[3].Get(), 0);
279-
_deviceContext->Draw();
280-
281-
_pipeline->SetRasterizerState(_wireFrameCullFrontRasterizerState.Get());
282-
_deviceContext->SetPipeline(_pipeline.get());
283-
_deviceContext->SetVertexBuffer(_triangleVertices[4].Get(), 0);
284-
_deviceContext->Draw();
285-
286-
_pipeline->SetRasterizerState(_wireFrameCullBackRasterizerState.Get());
287-
_deviceContext->SetPipeline(_pipeline.get());
288-
_deviceContext->SetVertexBuffer(_triangleVertices[5].Get(), 0);
289-
_deviceContext->Draw();
241+
D3D11_VIEWPORT viewport = {
242+
0.0f,
243+
0.0f,
244+
static_cast<float>(GetWindowWidth()),
245+
static_cast<float>(GetWindowHeight()),
246+
0.0f,
247+
1.0f
248+
};
249+
250+
_deviceContext->RSSetViewports(1, &viewport);
251+
_deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY::D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
252+
253+
_shaderCollection.ApplyToContext(_deviceContext.Get());
254+
255+
256+
_deviceContext->OMSetRenderTargets(1, &nullRTV, nullptr);
257+
258+
_deviceContext->ClearRenderTargetView(_renderTarget.Get(), clearColor);
259+
_deviceContext->OMSetRenderTargets(1, _renderTarget.GetAddressOf(), nullptr);
260+
261+
UINT stride = _shaderCollection.GetLayoutByteSize(VertexType::PositionColor);
262+
263+
_deviceContext->IASetVertexBuffers(
264+
0,
265+
1,
266+
_triangleVertices[0].GetAddressOf(),
267+
&stride,
268+
&vertexOffset);
269+
270+
_deviceContext->RSSetState(_solidFrameCullNoneRasterizerState.Get());
271+
_deviceContext->Draw(3, 0);
272+
273+
_deviceContext->RSSetState(_solidFrameCullFrontRasterizerState.Get());
274+
_deviceContext->IASetVertexBuffers(
275+
0,
276+
1,
277+
_triangleVertices[1].GetAddressOf(),
278+
&stride,
279+
&vertexOffset);
280+
_deviceContext->Draw(3, 0);
281+
282+
_deviceContext->RSSetState(_solidFrameCullBackRasterizerState.Get());
283+
_deviceContext->IASetVertexBuffers(
284+
0,
285+
1,
286+
_triangleVertices[2].GetAddressOf(),
287+
&stride,
288+
&vertexOffset);
289+
_deviceContext->Draw(3, 0);
290+
291+
_deviceContext->RSSetState(_wireFrameCullNoneRasterizerState.Get());
292+
_deviceContext->IASetVertexBuffers(
293+
0,
294+
1,
295+
_triangleVertices[3].GetAddressOf(),
296+
&stride,
297+
&vertexOffset);
298+
_deviceContext->Draw(3, 0);
299+
300+
_deviceContext->RSSetState(_wireFrameCullFrontRasterizerState.Get());
301+
_deviceContext->IASetVertexBuffers(
302+
0,
303+
1,
304+
_triangleVertices[4].GetAddressOf(),
305+
&stride,
306+
&vertexOffset);
307+
_deviceContext->Draw(3, 0);
308+
309+
_deviceContext->RSSetState(_wireFrameCullBackRasterizerState.Get());
310+
_deviceContext->IASetVertexBuffers(
311+
0,
312+
1,
313+
_triangleVertices[5].GetAddressOf(),
314+
&stride,
315+
&vertexOffset);
316+
_deviceContext->Draw(3, 0);
290317

291318
_swapChain->Present(1, 0);
292319
}

0 commit comments

Comments
 (0)