Changed D3D9 to D3D11 api connection

Changed mesh to D3D11
Changed material to D3D11 (WIP)
This commit is contained in:
2026-11-23 12:31:31 +01:00
parent abb8da9cff
commit 204c55e915
14 changed files with 288 additions and 182 deletions
+74 -35
View File
@@ -5,61 +5,100 @@ INT Direct3D::init(HWND hWnd, UINT width, UINT height, BOOL isFullscreen)
{
HRESULT hr = {};
// 1. create connection to Direct3D API
IDirect3D9* pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if (pD3D == nullptr) return 200;
// 1. define presentation parameters
DXGI_SWAP_CHAIN_DESC swapChainDesc = {};
swapChainDesc.OutputWindow = hWnd; // target window
swapChainDesc.Windowed = !isFullscreen; // Window mode yes or no
swapChainDesc.BufferCount = 1;
swapChainDesc.BufferDesc.Width = width;
swapChainDesc.BufferDesc.Height = height;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; // what happens to the front buffer after swap
swapChainDesc.SampleDesc.Count = 1; // have to be at least 1
// 2. check compatibility
D3DCAPS9 caps = {};
UINT adapter = 0; // graphic card index - 0 means primary adapter
D3DDEVTYPE devType = D3DDEVTYPE_HAL; // hardware or software renderer, here hardware
hr = pD3D->GetDeviceCaps(adapter, devType, &caps);
// 2. create Direct3D device, device context & swap chain
D3D_FEATURE_LEVEL supportedFeatureLevels[] =
{
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0
};
D3D_FEATURE_LEVEL chosenFeatureLevel = {};
hr = D3D11CreateDeviceAndSwapChain(
nullptr, // want to use a specific adapter, here not => default adapter
D3D_DRIVER_TYPE_HARDWARE, // hardware or software rendering
nullptr, // reference to the software rendering module
0, // optional flags
supportedFeatureLevels, std::size(supportedFeatureLevels), // supported D3D api versions
D3D11_SDK_VERSION,
&swapChainDesc, &_pDXGISwapChain, &_pD3DDevice, &chosenFeatureLevel, &_pD3DDeviceContext
);
CheckFailed(hr, 200);
// 3. create render target view
ID3D11Texture2D* pBackBuffer = nullptr;
hr = _pDXGISwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
CheckFailed(hr, 210);
DWORD vertexProcessing = D3DCREATE_SOFTWARE_VERTEXPROCESSING; // fallback if hardware processing is not available
if (caps.VertexProcessingCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
vertexProcessing = D3DCREATE_HARDWARE_VERTEXPROCESSING; // hardware processing is available
hr = _pD3DDevice->CreateRenderTargetView(pBackBuffer, nullptr, &_pRenderTargetView);
CheckFailed(hr, 215);
// 3. define presentation parameters
D3DPRESENT_PARAMETERS pp = {};
pp.hDeviceWindow = hWnd; // target window
pp.Windowed = !isFullscreen; // Window mode yes or no
pp.BackBufferCount = 1;
pp.BackBufferWidth = width;
pp.BackBufferHeight = height;
pp.BackBufferFormat = D3DFMT_A8R8G8B8;
pp.SwapEffect = D3DSWAPEFFECT_DISCARD; // what happens to the front buffer after swap
safeRelease(pBackBuffer);
// 4. create Direct3D device
hr = pD3D->CreateDevice(adapter, devType, hWnd, vertexProcessing, &pp, &_pD3DDevice);
CheckFailed(hr, 220);
// 4. create rasterizer state
// 5. tidy up
safeRelease(pD3D);
//d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
//d3d.getDevice()->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
//d3d.getDevice()->SetRenderState(D3DRS_LIGHTING, TRUE);
//d3d.getDevice()->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL);
//d3d.getDevice()->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL);
//d3d.getDevice()->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_MATERIAL);
//d3d.getDevice()->SetRenderState(D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_MATERIAL);
//d3d.getDevice()->SetRenderState(D3DRS_SPECULARENABLE, TRUE);
// 5. create depth stencil view & state
// 6. create view port
D3D11_VIEWPORT viewPort = {}; // describe the area where the back buffer should be rendered in the window, you can have more than one, e.g. for split screen
viewPort.Width = width;
viewPort.Height = height;
viewPort.MinDepth = 0.0f;
viewPort.MaxDepth = 1.0f;
// 7. setup render pipeline
_pD3DDeviceContext->RSSetViewports(1, &viewPort);
_pD3DDeviceContext->OMSetRenderTargets(1, &_pRenderTargetView, nullptr);
return 0;
}
void Direct3D::beginScene()
{
_pD3DDevice->BeginScene();
FLOAT backgroundColor[] = { 0.0f, 0.0f, 0.0f, 1.0f };
// clear buffers
_pD3DDevice->Clear(
0, nullptr, // only clear specific buffer regions, here clear all
D3DCLEAR_TARGET, // which buffers to clear
D3DCOLOR_XRGB(100, 100, 100), 1.0f, 0xffffffff // clear values
);
_pD3DDeviceContext->ClearRenderTargetView(_pRenderTargetView, backgroundColor);
}
void Direct3D::endScene()
{
_pD3DDevice->EndScene();
_pD3DDevice->Present(nullptr, nullptr, nullptr, nullptr);
_pDXGISwapChain->Present(
0, // vsync interval, 0 -> deactivated, 1,2,3 or 4 -> activated for x frames
0 // optional flags
);
}
void Direct3D::deInit()
{
safeRelease(_pDepthStencilState);
safeRelease(_pDepthStencilView);
safeRelease(_pRasterizerState);
safeRelease(_pRenderTargetView);
safeRelease(_pDXGISwapChain);
safeRelease(_pD3DDeviceContext);
safeRelease(_pD3DDevice);
}