Skip to content

Commit b478822

Browse files
author
Themperror
authored
Merge pull request #135 from spec-chum/main
Fix text to match code on github
2 parents 97d7796 + 22c6bdf commit b478822

File tree

1 file changed

+46
-40
lines changed

1 file changed

+46
-40
lines changed

docs/1-introduction/1-1-getting-started/1-1-2-hello-d3d11.md

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,44 @@ for an application with a window through GLFW. The implementation for `Main.cpp`
77

88
If you are looking at the source code for this chapter, you will also notice that `Application.cpp`
99
and `Application.hpp` do not exist anymore, as we have moved both of these files into a separate
10-
`Framework` project to ease development between chapters. This `Framework` project will include
10+
`Framework` project that creates a static library to ease development between chapters. This `Framework` project will include
1111
code that is shared between all chapters, so it might include a lot of other files which are not
12-
used or are not relevant within some chapters. Please note that the code for already existing files
12+
used or are not relevant within some chapters.
13+
14+
The `Framework` project can be found [here](https://github.com/GraphicsProgramming/learnd3d11/tree/main/src/Cpp/Framework).
15+
16+
Please note that the code for already existing files
1317
is also subject to change to accommodate newer chapters and their needs.
1418

1519
However, let's start by breaking down the relevant bits and pieces by showing you how the new
1620
class, which derives from `Application` will look like.
1721

18-
## HelloD3D11.hpp
22+
## HelloD3D11Application.hpp
1923

2024
```cpp
21-
#include <d3d11_2.h>
22-
#include <wrl.h>
25+
#pragma once
2326

24-
#include "Application.hpp"
27+
#include <d3d11.h>
28+
#include <dxgi1_3.h>
29+
#include <wrl.h>
2530

26-
#include <string_view>
31+
#include <Application.hpp>
2732

2833
class HelloD3D11Application final : public Application
2934
{
3035
template <typename T>
3136
using ComPtr = Microsoft::WRL::ComPtr<T>;
37+
3238
public:
33-
HelloD3D11Application(const std::string_view title);
39+
HelloD3D11Application(const std::string& title);
3440
~HelloD3D11Application() override;
3541

3642
protected:
3743
bool Initialize() override;
3844
bool Load() override;
3945
void OnResize(
40-
const int32_t width,
41-
const int32_t height) override;
46+
int32_t width,
47+
int32_t height) override;
4248
void Update() override;
4349
void Render() override;
4450

@@ -54,19 +60,19 @@ private:
5460
};
5561
```
5662

57-
## HelloD3D11.cpp
63+
## HelloD3D11Application.cpp
5864

5965
And the implementation side
6066

6167
```cpp
62-
#include "HelloD3D11.hpp"
68+
#include "HelloD3D11Application.hpp"
6369

6470
#include <GLFW/glfw3.h>
6571
#define GLFW_EXPOSE_NATIVE_WIN32
6672
#include <GLFW/glfw3native.h>
6773

68-
#include <d3dcompiler.h>
6974
#include <DirectXMath.h>
75+
#include <d3dcompiler.h>
7076

7177
#include <iostream>
7278

@@ -87,6 +93,7 @@ HelloD3D11Application::~HelloD3D11Application()
8793

8894
bool HelloD3D11Application::Initialize()
8995
{
96+
return true;
9097
}
9198

9299
bool HelloD3D11Application::Load()
@@ -96,6 +103,7 @@ bool HelloD3D11Application::Load()
96103

97104
bool HelloD3D11Application::CreateSwapchainResources()
98105
{
106+
return true;
99107
}
100108

101109
void HelloD3D11Application::DestroySwapchainResources()
@@ -117,6 +125,14 @@ void HelloD3D11Application::Render()
117125
}
118126
```
119127
128+
HelloD3D11Application.hpp
129+
```cpp
130+
#include <d3d11.h>
131+
#include <dxgi1_3.h>
132+
#include <wrl.h>
133+
```
134+
135+
HelloD3D11Application.cpp
120136
```cpp
121137
#include <GLFW/glfw3.h>
122138
#define GLFW_EXPOSE_NATIVE_WIN32
@@ -129,7 +145,7 @@ void HelloD3D11Application::Render()
129145
We need to include the following headers, here's what each of these headers includes:
130146

131147
- `d3d11.h`: The core of D3D11, it contains all the ID3D11XXX types and most of the enums we will be using with D3D11
132-
- `dxgi.h`: The core of DXGI, it contains all the IDXGIXXX types and additional enums that are required for DXGI structures
148+
- `dxgi1_3.h`: The core of DXGI, it contains all the IDXGIXXX types and additional enums that are required for DXGI structures
133149
- `d3dcompiler.h`: Contains all the functions necessary to compiler our HLSL shaders into bytecode that will be fed into the GPU
134150
- `DirectXMath.h`: DirectX's own math library, it contains all the types and math functions we will be using throughout the series
135151
- `wrl.h`: Is used for `Microsoft::WRL::ComPtr<T>`, to manage COM resources automatically.
@@ -142,14 +158,7 @@ We need to include the following headers, here's what each of these headers incl
142158
#pragma comment(lib, "dxguid.lib")
143159
```
144160

145-
Of course just including the headers isn't enough, we must also link against D3D11 & friends to be able to actually use the stuff declared in the headers, put these `#pragma comment(lib, "PATH_TO_LIB")` in `HelloD3D11.cpp` right below the includes to link these libraries.
146-
147-
```cpp
148-
template <typename T>
149-
using ComPtr = Microsoft::WRL::ComPtr<T>;
150-
```
151-
152-
Since the namespace name for ComPtr<T> is quite long, we are making a type alias like this.
161+
Of course just including the headers isn't enough, we must also link against D3D11 & friends to be able to actually use the stuff declared in the headers, put these `#pragma comment(lib, "PATH_TO_LIB")` in `HelloD3D11Application.cpp` right below the includes to link these libraries.
153162

154163
```cpp
155164
ComPtr<IDXGIFactory2> _dxgiFactory = nullptr;
@@ -181,9 +190,7 @@ if (!Application::Initialize())
181190
return false;
182191
}
183192

184-
if (FAILED(CreateDXGIFactory2(
185-
0,
186-
IID_PPV_ARGS(&_dxgiFactory))))
193+
if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&_dxgiFactory))))
187194
{
188195
std::cout << "DXGI: Unable to create DXGIFactory\n";
189196
return false;
@@ -196,10 +203,9 @@ The first part calls the parent class, where `GLFW` is initialized and setup.
196203
```cpp
197204
#define IID_PPV_ARGS(ppType) __uuidof(**(ppType)), IID_PPV_ARGS_Helper(ppType)
198205
```
199-
Which means that typing `IID_PPV_ARGS(&_dxgiFactory)` it is expanded by the compiler into `__uuidof(**(&_dxgiFactory)), IID_PPV_ARGS_Helper(_dxgiFactory)`. This functionally means that for functions that have a parameter setup as `REFIID` and functionally after a `[out] void**` parameter, this macro will expand the `IID_PPV_ARGS(ppType)` expression into these parameters for ease of use — this can be seen with the used `CreateDXGIFactory2` method where the second last and last parameter are a `REFIID` and `void**`:
206+
Which means that typing `IID_PPV_ARGS(&_dxgiFactory)` it is expanded by the compiler into `__uuidof(**(&_dxgiFactory)), IID_PPV_ARGS_Helper(_dxgiFactory)`. This functionally means that for functions that have a parameter setup as `REFIID` and functionally after a `[out] void**` parameter, this macro will expand the `IID_PPV_ARGS(ppType)` expression into these parameters for ease of use — this can be seen with the used `CreateDXGIFactory1` method where the parameters are a `REFIID` and `void**`:
200207
```cpp
201-
HRESULT CreateDXGIFactory2(
202-
UINT Flags,
208+
HRESULT CreateDXGIFactory1(
203209
REFIID riid,
204210
[out] void **ppFactory
205211
);
@@ -220,27 +226,26 @@ What the parts of the `IID_PPV_ARGS(ppType)` macro are:
220226

221227
https://docs.microsoft.com/en-us/windows/win32/direct3ddxgi/d3d10-graphics-programming-guide-dxgi
222228

223-
[`CreateDXGIFactory2`](https://docs.microsoft.com/en-us/windows/win32/api/dxgi1_3/nf-dxgi1_3-createdxgifactory2) is the entry point to create a factory for us, a `IDXGIFactory2` to be precise.
229+
[`CreateDXGIFactory1`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-createdxgifactory1) is the entry point to create a factory for us, a `IDXGIFactory1` to be precise.
224230
There are various implementations of it, depending on what version you aim for, you get additional functionality.
225231

226-
DXGI 1.0 up to 1.6 More information can be found [here](https://docs.microsoft.com/en-us/windows/win32/api/_direct3ddxgi/) We will stick with `IDXGIFactory2` for now.
232+
DXGI 1.0 up to 1.6 More information can be found [here](https://docs.microsoft.com/en-us/windows/win32/api/_direct3ddxgi/) We will stick with `IDXGIFactory1` for now.
227233

228234
```cpp
229235
constexpr D3D_FEATURE_LEVEL deviceFeatureLevel = D3D_FEATURE_LEVEL::D3D_FEATURE_LEVEL_11_0;
230-
constexpr UINT deviceFlags = D3D11_CREATE_DEVICE_FLAG::D3D11_CREATE_DEVICE_BGRA_SUPPORT;
231236
if (FAILED(D3D11CreateDevice(
232237
nullptr,
233238
D3D_DRIVER_TYPE::D3D_DRIVER_TYPE_HARDWARE,
234239
nullptr,
235-
deviceFlags,
240+
0,
236241
&deviceFeatureLevel,
237242
1,
238243
D3D11_SDK_VERSION,
239244
&_device,
240245
nullptr,
241246
&_deviceContext)))
242247
{
243-
std::cout << "D3D11: Failed to create Device and Device Context\n";
248+
std::cout << "D3D11: Failed to create device and device Context\n";
244249
return false;
245250
}
246251
```
@@ -262,6 +267,7 @@ swapChainDescriptor.SampleDesc.Quality = 0;
262267
swapChainDescriptor.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
263268
swapChainDescriptor.BufferCount = 2;
264269
swapChainDescriptor.SwapEffect = DXGI_SWAP_EFFECT::DXGI_SWAP_EFFECT_FLIP_DISCARD;
270+
swapChainDescriptor.Scaling = DXGI_SCALING::DXGI_SCALING_STRETCH;
265271
swapChainDescriptor.Flags = {};
266272

267273
DXGI_SWAP_CHAIN_FULLSCREEN_DESC swapChainFullscreenDescriptor = {};
@@ -275,7 +281,7 @@ if (FAILED(_dxgiFactory->CreateSwapChainForHwnd(
275281
nullptr,
276282
&_swapChain)))
277283
{
278-
std::cout << "DXGI: Failed to create SwapChain\n";
284+
std::cout << "DXGI: Failed to create swapchain\n";
279285
return false;
280286
}
281287
```
@@ -284,9 +290,9 @@ After we successfully create device and device context, the next step is to crea
284290
285291
The majority of values should make some sense without explanation, like width and height, and whether we want it to support a windowed window or not.
286292
287-
`BufferUsage` tells the swapchain's buffers their usage, something we render to, and can present. The format here is in BGRA order, like the device creation flag we specified earlier, if you remember.
293+
`BufferUsage` tells the swapchain's buffers their usage, something we render to, and can present.
288294
289-
`Scaling` tells DXGI to scale the buffer's contents to fit the presentation's target size.
295+
`Scaling` tells DXGI how to scale the buffer's contents to fit the presentation's target size.
290296
291297
`BufferCount` is 2, because we want double buffering. Double buffering is an age-old technique to avoid presenting an image that is being used by the GPU, instead we work on the "back buffer", while the GPU is happy presenting the "front buffer", then, as soon as we are done with the back buffer, we swap front and back, and begin working on the former front buffer present that one and render to the other one again in the meantime. That process is supposed to reduce flicker or tearing.
292298
@@ -379,8 +385,8 @@ void HelloD3D11Application::Render()
379385
D3D11_VIEWPORT viewport = {};
380386
viewport.TopLeftX = 0;
381387
viewport.TopLeftY = 0;
382-
viewport.Width = GetWindowWidth();
383-
viewport.Height = GetWindowHeight();
388+
viewport.Width = static_cast<float>(GetWindowWidth());
389+
viewport.Height = static_cast<float>(GetWindowHeight());
384390
viewport.MinDepth = 0.0f;
385391
viewport.MaxDepth = 1.0f;
386392
@@ -468,8 +474,8 @@ void Application::HandleResize(
468474
const int32_t width,
469475
const int32_t height)
470476
{
471-
Application& application = *static_cast<Application*>(glfwGetWindowUserPointer(window));
472-
application.OnResize(width, height);
477+
Application* application = static_cast<Application*>(glfwGetWindowUserPointer(window));
478+
application->OnResize(width, height);
473479
}
474480
475481
GLFWwindow* Application::GetWindow() const

0 commit comments

Comments
 (0)