Compare commits
3
Commits
9dc5cc8a08
...
3f679574a6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3f679574a6 | ||
|
|
4fea2e0df5 | ||
|
|
1d95a970fe |
@@ -1,32 +1,97 @@
|
||||
#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 ouput merger stage
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Direct3D::beginScene()
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -16,15 +16,19 @@ INT Window::init(HINSTANCE hInstance, UINT width, UINT height)
|
||||
if (!RegisterClass(&wc)) return 100;
|
||||
|
||||
// 3. adjust window size (optional)
|
||||
DWORD style = WS_OVERLAPPEDWINDOW; // window with title bar & some more decorations
|
||||
//DWORD style = WS_POPUP; // window without any decorations
|
||||
|
||||
RECT rect = { 100, 100, 100 + width, 100 + height }; // left-top & bottom-right corners
|
||||
AdjustWindowRect(&rect, style, FALSE);
|
||||
|
||||
// 4. create window instance
|
||||
_hWnd = CreateWindow(
|
||||
wc.lpszClassName, // class name
|
||||
wc.lpszClassName, // window title
|
||||
WS_OVERLAPPEDWINDOW, // visual style
|
||||
100, 100, // window position (left-top corner)
|
||||
width, height, // window size
|
||||
style, // visual style
|
||||
rect.left, rect.top, // window position (left-top corner)
|
||||
rect.right - rect.left, rect.bottom - rect.top, // window size
|
||||
nullptr, // optional handle to a parent window
|
||||
nullptr, // optional handle to a menu instance
|
||||
hInstance,
|
||||
|
||||
@@ -16,8 +16,8 @@ int WINAPI WinMain(
|
||||
)
|
||||
{
|
||||
// some constant values
|
||||
const UINT width = 640;
|
||||
const UINT height = 480;
|
||||
const UINT width = 1024;
|
||||
const UINT height = 768;
|
||||
const BOOL isFullscreen = FALSE;
|
||||
|
||||
UINT error = 0; // error code for identifying errors
|
||||
|
||||
Reference in New Issue
Block a user