Files
gpd-925-en/GraphicsProgramming/D3D11/Direct3D.cpp
T
david d2a487a4f6 Added: matrix buffer
Added: world, view & projection matrices
Added: delta time calculation
Added: quad mesh
2026-10-12 18:47:53 +02:00

100 lines
3.3 KiB
C++

#include "Direct3D.h"
#include "Utilities.h"
#include <xutility>
INT Direct3D::init(HWND hWnd, UINT width, UINT height, BOOL isFullscreen)
{
HRESULT hr = {};
// 1. define swap chain description
DXGI_SWAP_CHAIN_DESC swapChainDesc = {};
swapChainDesc.OutputWindow = hWnd;
swapChainDesc.Windowed = !isFullscreen;
swapChainDesc.BufferCount = 1; // how many back buffers we want to use, at least 1
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how we want to render to the back buffer
swapChainDesc.BufferDesc.Width = width;
swapChainDesc.BufferDesc.Height = height;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // pixel format, here 32 bits, 4 channel, each 8 bit, unsigned normalized floats
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; // what happen to the front buffer after swap
swapChainDesc.SampleDesc.Count = 1; // activate MSAA with a value higher than 1 (also set Quality if activated), has to be at least 1
// 2. create Direct3D device, device context & swap chain
D3D_FEATURE_LEVEL desiredFeaturesLevels[] = {
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0
};
D3D_FEATURE_LEVEL chosenFeatureLevel = {};
hr = D3D11CreateDeviceAndSwapChain(
nullptr, // graphic card we want to use, here default one
D3D_DRIVER_TYPE_HARDWARE, // should we use the graphic card or a software renderer
nullptr, // handle to software render module (only for D3D_DRIVER_TYPE_SOFTWARE)
0, // optional flags
desiredFeaturesLevels, std::size(desiredFeaturesLevels), // supported Direct3D versions
D3D11_SDK_VERSION, // constant value for identifying the SDK version at runtime
&swapChainDesc,
&_pDXGISwapChain,
&_pD3DDevice,
&chosenFeatureLevel, // Direct3D version supported by the current hardware
&_pD3DDeviceContext
);
CheckFailed(hr, 200);
// 3. create render target
ID3D11Texture2D* pBackBuffer = nullptr;
// get the back buffer and save it in a texture object
hr = _pDXGISwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
CheckFailed(hr, 210);
// connect texture to render target view
hr = _pD3DDevice->CreateRenderTargetView(pBackBuffer, nullptr, &_pD3DRenderTargetView);
CheckFailed(hr, 220);
safeRelease(pBackBuffer);
// 4. create depth stencil buffer
// 5. create rasterizer state
// 6. describe viewport
D3D11_VIEWPORT viewport = {};
viewport.Width = static_cast<FLOAT>(width);
viewport.Height = static_cast<FLOAT>(height);
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;
// 7. setup render pipeline (device context)
_pD3DDeviceContext->RSSetViewports(1, &viewport);
_pD3DDeviceContext->OMSetRenderTargets(1, &_pD3DRenderTargetView, nullptr); // TODO: create depth stencil view and set it for output merger stage
return 0;
}
void Direct3D::beginScene()
{
// TODO: implement a custom color via parameters
// TODO: (optional) implement a random color every frame
FLOAT color[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
// 8. clear the buffers
_pD3DDeviceContext->ClearRenderTargetView(_pD3DRenderTargetView, color);
}
void Direct3D::endScene()
{
// 9. present the buffer
_pDXGISwapChain->Present(
0, // sync interval, 0 -> no sync, 1/2/3/4 -> amount frames to be synced, works only in fullscreen
0 // optional flags
);
}
void Direct3D::deInit()
{
safeRelease(_pD3DRenderTargetView);
safeRelease(_pDXGISwapChain);
safeRelease(_pD3DDeviceContext);
safeRelease(_pD3DDevice);
}