#include "Direct3D.h" #include "Utilities.h" #include 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 ID3D11Texture2D* pDepthStencilBuffer = nullptr; D3D11_TEXTURE2D_DESC depthStencilBufferDesc = {}; depthStencilBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL; depthStencilBufferDesc.Width = width; depthStencilBufferDesc.Height = height; depthStencilBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; depthStencilBufferDesc.ArraySize = 1; depthStencilBufferDesc.Usage = D3D11_USAGE_DEFAULT; depthStencilBufferDesc.SampleDesc.Count = 1; hr = _pD3DDevice->CreateTexture2D(&depthStencilBufferDesc, nullptr, &pDepthStencilBuffer); CheckFailed(hr, 225); hr = _pD3DDevice->CreateDepthStencilView(pDepthStencilBuffer, nullptr, &_pD3DDepthStencilView); CheckFailed(hr, 230); safeRelease(pDepthStencilBuffer); D3D11_DEPTH_STENCIL_DESC depthStencilDesc = {}; depthStencilDesc.DepthEnable = true; depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL; depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; hr = _pD3DDevice->CreateDepthStencilState(&depthStencilDesc, &_pD3DDepthStencilState); CheckFailed(hr, 240); // 5. create rasterizer state D3D11_RASTERIZER_DESC rasterizerDesc = {}; rasterizerDesc.FillMode = D3D11_FILL_SOLID; rasterizerDesc.CullMode = D3D11_CULL_BACK; hr = _pD3DDevice->CreateRasterizerState(&rasterizerDesc, &_pD3DRasterizerState); CheckFailed(hr, 250); // 6. describe viewport D3D11_VIEWPORT viewport = {}; viewport.Width = static_cast(width); viewport.Height = static_cast(height); viewport.MinDepth = 0.0f; viewport.MaxDepth = 1.0f; // 7. setup render pipeline (device context) _pD3DDeviceContext->RSSetViewports(1, &viewport); _pD3DDeviceContext->OMSetRenderTargets(1, &_pD3DRenderTargetView, _pD3DDepthStencilView); _pD3DDeviceContext->OMSetDepthStencilState(_pD3DDepthStencilState, 0xFFFFFFFF); _pD3DDeviceContext->RSSetState(_pD3DRasterizerState); 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); _pD3DDeviceContext->ClearDepthStencilView(_pD3DDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0xFFFFFFFF); } 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); }