Nameless Engine
Loading...
Searching...
No Matches
DirectXRenderer.h
1#pragma once
2
3// Standard.
4#include <optional>
5#include <variant>
6#include <vector>
7#include <mutex>
8#include <atomic>
9#include <memory>
10
11// Custom.
12#include "render/Renderer.h"
13#include "misc/Error.h"
14#include "render/directx/resource/DirectXResource.h"
15
16// External.
17#include "directx/d3dx12.h"
18
19// OS.
20#include <wrl.h>
21#include <dxgi1_4.h>
22
23namespace ne {
24 using namespace Microsoft::WRL;
25
26 class GameManager;
27 class DirectXResourceManager;
28 class Material;
29 class CameraProperties;
30 class DirectXPso;
31 struct QueuedForExecutionComputeShaders;
32 class ComputeShaderInterface;
33 struct DirectXFrameResource;
34
36 class DirectXRenderer : public Renderer {
37 public:
38 DirectXRenderer() = delete;
39 DirectXRenderer(const DirectXRenderer&) = delete;
40 DirectXRenderer& operator=(const DirectXRenderer&) = delete;
41
42 virtual ~DirectXRenderer() override;
43
55 static std::variant<std::unique_ptr<Renderer>, std::pair<Error, std::string>>
56 create(GameManager* pGameManager, const std::vector<std::string>& vBlacklistedGpuNames);
57
63 static constexpr DXGI_FORMAT getBackBufferFormat() { return backBufferFormat; }
64
70 static constexpr DXGI_FORMAT getDepthStencilBufferFormat() { return depthStencilBufferFormat; }
71
77 static constexpr DXGI_FORMAT getDepthBufferFormatNoMultisampling() {
79 }
80
86 static constexpr DXGI_FORMAT getShadowMapFormat() { return shadowMapFormat; }
87
94 static constexpr DXGI_FORMAT getShadowMappingPointLightColorTargetFormat() {
96 }
97
106 virtual std::vector<std::string> getSupportedGpuNames() const override;
107
113 virtual std::variant<std::set<std::pair<unsigned int, unsigned int>>, Error>
114 getSupportedRenderResolutions() const override;
115
124 virtual std::variant<std::set<std::pair<unsigned int, unsigned int>>, Error>
125 getSupportedRefreshRates() const override;
126
132 virtual std::string getCurrentlyUsedGpuName() const override;
133
137 virtual void waitForGpuToFinishWorkUpToThisPoint() override;
138
144 virtual RendererType getType() const override;
145
154 virtual std::string getUsedApiVersion() const override;
155
161 ID3D12Device* getD3dDevice() const;
162
168 ID3D12GraphicsCommandList* getD3dCommandList();
169
175 ID3D12CommandQueue* getD3dCommandQueue();
176
182 IDXGIAdapter3* getVideoAdapter() const;
183
189 UINT getMsaaQualityLevel() const;
190
204
210 virtual std::pair<unsigned int, unsigned int> getRenderTargetSize() const override;
211
212 protected:
221
230 virtual void drawShadowMappingPass(
231 FrameResource* pCurrentFrameResource,
232 size_t iCurrentFrameResourceIndex,
233 GraphicsPipelineRegistry* pGraphicsPipelines) override;
234
242 virtual void drawMeshesDepthPrepass(
243 FrameResource* pCurrentFrameResource,
244 size_t iCurrentFrameResourceIndex,
245 const std::vector<Renderer::MeshesInFrustum::PipelineInFrustumInfo>& vOpaquePipelines) override;
246
257 FrameResource* pCurrentFrameResource,
258 size_t iCurrentFrameResourceIndex,
259 ComputeExecutionStage stage) override;
260
269 virtual void drawMeshesMainPass(
270 FrameResource* pCurrentFrameResource,
271 size_t iCurrentFrameResourceIndex,
272 const std::vector<Renderer::MeshesInFrustum::PipelineInFrustumInfo>& vOpaquePipelines,
273 const std::vector<Renderer::MeshesInFrustum::PipelineInFrustumInfo>& vTransparentPipelines)
274 override;
275
282 virtual void
283 present(FrameResource* pCurrentFrameResource, size_t iCurrentFrameResourceIndex) override;
284
297 DirectXFrameResource* pCurrentFrameResource,
298 size_t iCurrentFrameResourceIndex,
299 const std::vector<Renderer::MeshesInFrustum::PipelineInFrustumInfo>& pipelinesOfSpecificType,
300 D3D12_GPU_DESCRIPTOR_HANDLE directionalShadowMapsHandle,
301 D3D12_GPU_DESCRIPTOR_HANDLE spotShadowMapsHandle,
302 D3D12_GPU_DESCRIPTOR_HANDLE pointShadowMapsGpuHandle,
303 const bool bIsDrawingTransparentMeshes);
304
311 [[nodiscard]] virtual std::optional<Error> onRenderSettingsChangedDerived() override;
312
322 virtual void waitForGpuToFinishUsingFrameResource(FrameResource* pFrameResource) override;
323
332 virtual std::variant<std::optional<AntialiasingQuality>, Error>
333 getMaxSupportedAntialiasingQuality() const override;
334
343 virtual bool isInitialized() const override;
344
345 private:
358 [[nodiscard]] std::optional<Error> initialize(const std::vector<std::string>& vBlacklistedGpuNames);
359
360#if defined(DEBUG)
370 [[nodiscard]] std::optional<Error> enableDebugLayer();
371#endif
372
382 [[nodiscard]] std::optional<Error> createDepthStencilBuffer();
383
393 [[nodiscard]] std::optional<Error>
394 pickVideoAdapter(const std::vector<std::string>& vBlacklistedGpuNames);
395
403 static size_t rateGpuSuitability(DXGI_ADAPTER_DESC1 adapterDesc);
404
410 [[nodiscard]] std::optional<Error> setOutputAdapter();
411
417 [[nodiscard]] std::optional<Error> createCommandQueue();
418
426 [[nodiscard]] std::optional<Error> createCommandList();
427
433 [[nodiscard]] std::optional<Error> createSwapChain();
434
443 [[nodiscard]] std::optional<Error>
444 initializeDirectX(const std::vector<std::string>& vBlacklistedGpuNames);
445
457 virtual void prepareForDrawingNextFrame(
458 CameraProperties* pCameraProperties, FrameResource* pCurrentFrameResource) override;
459
465 void resetCommandListForGraphics(DirectXFrameResource* pCurrentFrameResource);
466
472 void executeGraphicsCommandList(ID3D12GraphicsCommandList* pCommandListToExecute);
473
479 [[nodiscard]] std::optional<Error> updateMsaaQualityLevelCount();
480
486 void waitForFenceValue(UINT64 iFenceToWaitFor);
487
495 ID3D12CommandAllocator* pCommandAllocator,
496 std::unordered_map<Pipeline*, std::unordered_set<ComputeShaderInterface*>>&
497 computePipelinesToSubmit);
498
505 std::variant<std::vector<DXGI_MODE_DESC>, Error> getSupportedDisplayModes() const;
506
513
515 ComPtr<IDXGIFactory4> pFactory;
516
518 ComPtr<ID3D12Device> pDevice;
519
521 ComPtr<IDXGIAdapter3> pVideoAdapter;
522
524 ComPtr<IDXGIOutput> pOutputAdapter;
525
527 ComPtr<IDXGISwapChain3> pSwapChain;
528
530 ComPtr<ID3D12CommandQueue> pCommandQueue;
531
533 ComPtr<ID3D12GraphicsCommandList> pCommandList;
534
536 ComPtr<ID3D12GraphicsCommandList> pComputeCommandList;
537
539 ComPtr<ID3D12Fence> pFence;
540
541#if defined(DEBUG)
543 ComPtr<ID3D12InfoQueue1> pInfoQueue;
544
546 ComPtr<ID3D12Debug> pDebugController;
547#endif
548
550 std::pair<std::recursive_mutex, UINT64> mtxCurrentFenceValue;
551
553 std::vector<std::unique_ptr<DirectXResource>> vSwapChainBuffers;
554
556 std::unique_ptr<DirectXResource> pDepthStencilBuffer;
557
566 std::unique_ptr<DirectXResource> pDepthBufferNoMultisampling;
567
569 float backBufferFillColor[4] = {0.0F, 0.0F, 0.0F, 1.0F};
570
572 std::unique_ptr<DirectXResource> pMsaaRenderBuffer;
573
575 std::vector<std::string> vSupportedGpuNames;
576
578 std::pair<unsigned int, unsigned int> renderTargetSize = {0, 0};
579
582
584 D3D12_VIEWPORT screenViewport;
585
587 D3D12_RECT scissorRect;
588
590 std::string sUsedVideoAdapter;
591
594
597
603
606
608 static constexpr DXGI_FORMAT backBufferFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
609
611 static constexpr DXGI_FORMAT depthStencilBufferFormat = DXGI_FORMAT_D32_FLOAT;
612
614 static constexpr DXGI_FORMAT shadowMapFormat = DXGI_FORMAT_D32_FLOAT;
615
619 static constexpr DXGI_FORMAT shadowMappingPointLightColorTargetFormat = DXGI_FORMAT_R32_FLOAT;
620
630 static constexpr DXGI_FORMAT depthBufferNoMultisamplingFormat = DXGI_FORMAT_R32_FLOAT;
631
633 static constexpr DXGI_MODE_SCALING usedScaling = DXGI_MODE_SCALING_UNSPECIFIED;
634
636 static constexpr DXGI_MODE_SCANLINE_ORDER usedScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_PROGRESSIVE;
637
639 static constexpr D3D_FEATURE_LEVEL rendererD3dFeatureLevel =
640 D3D_FEATURE_LEVEL::D3D_FEATURE_LEVEL_12_1;
641 };
642} // namespace ne
Definition: CameraProperties.h:22
Definition: DirectXRenderer.h:36
ComPtr< ID3D12CommandQueue > pCommandQueue
Definition: DirectXRenderer.h:530
virtual RendererType getType() const override
Definition: DirectXRenderer.cpp:2359
virtual std::variant< std::optional< AntialiasingQuality >, Error > getMaxSupportedAntialiasingQuality() const override
Definition: DirectXRenderer.cpp:2275
virtual std::pair< unsigned int, unsigned int > getRenderTargetSize() const override
Definition: DirectXRenderer.cpp:2101
virtual std::vector< std::string > getSupportedGpuNames() const override
Definition: DirectXRenderer.cpp:481
ComPtr< ID3D12Device > pDevice
Definition: DirectXRenderer.h:518
std::pair< std::recursive_mutex, UINT64 > mtxCurrentFenceValue
Definition: DirectXRenderer.h:550
std::optional< Error > updateMsaaQualityLevelCount()
Definition: DirectXRenderer.cpp:1566
static constexpr D3D_FEATURE_LEVEL rendererD3dFeatureLevel
Definition: DirectXRenderer.h:639
ComPtr< ID3D12Fence > pFence
Definition: DirectXRenderer.h:539
static constexpr DXGI_FORMAT getBackBufferFormat()
Definition: DirectXRenderer.h:63
static constexpr DXGI_FORMAT shadowMappingPointLightColorTargetFormat
Definition: DirectXRenderer.h:619
static constexpr DXGI_FORMAT backBufferFormat
Definition: DirectXRenderer.h:608
void drawMeshesMainPassSpecificPipelines(DirectXFrameResource *pCurrentFrameResource, size_t iCurrentFrameResourceIndex, const std::vector< Renderer::MeshesInFrustum::PipelineInFrustumInfo > &pipelinesOfSpecificType, D3D12_GPU_DESCRIPTOR_HANDLE directionalShadowMapsHandle, D3D12_GPU_DESCRIPTOR_HANDLE spotShadowMapsHandle, D3D12_GPU_DESCRIPTOR_HANDLE pointShadowMapsGpuHandle, const bool bIsDrawingTransparentMeshes)
Definition: DirectXRenderer.cpp:1353
virtual std::variant< std::set< std::pair< unsigned int, unsigned int > >, Error > getSupportedRenderResolutions() const override
Definition: DirectXRenderer.cpp:484
ID3D12Device * getD3dDevice() const
Definition: DirectXRenderer.cpp:2368
ID3D12CommandQueue * getD3dCommandQueue()
Definition: DirectXRenderer.cpp:2372
std::optional< Error > pickVideoAdapter(const std::vector< std::string > &vBlacklistedGpuNames)
Definition: DirectXRenderer.cpp:291
UINT iMsaaQualityLevelsCount
Definition: DirectXRenderer.h:581
DirectXResource * getCurrentBackBufferResource()
Definition: DirectXRenderer.cpp:2412
static std::variant< std::unique_ptr< Renderer >, std::pair< Error, std::string > > create(GameManager *pGameManager, const std::vector< std::string > &vBlacklistedGpuNames)
Definition: DirectXRenderer.cpp:162
static constexpr DXGI_FORMAT getShadowMapFormat()
Definition: DirectXRenderer.h:86
static constexpr DXGI_FORMAT shadowMapFormat
Definition: DirectXRenderer.h:614
std::optional< Error > createCommandList()
Definition: DirectXRenderer.cpp:1754
ComPtr< IDXGIFactory4 > pFactory
Definition: DirectXRenderer.h:515
virtual void drawShadowMappingPass(FrameResource *pCurrentFrameResource, size_t iCurrentFrameResourceIndex, GraphicsPipelineRegistry *pGraphicsPipelines) override
Definition: DirectXRenderer.cpp:777
static constexpr DXGI_FORMAT getShadowMappingPointLightColorTargetFormat()
Definition: DirectXRenderer.h:94
virtual void waitForGpuToFinishUsingFrameResource(FrameResource *pFrameResource) override
Definition: DirectXRenderer.cpp:2266
virtual GpuResource * getDepthTextureNoMultisampling() override
Definition: DirectXRenderer.cpp:2088
static constexpr DXGI_MODE_SCANLINE_ORDER usedScanlineOrdering
Definition: DirectXRenderer.h:636
static constexpr DXGI_MODE_SCALING usedScaling
Definition: DirectXRenderer.h:633
std::optional< Error > initializeDirectX(const std::vector< std::string > &vBlacklistedGpuNames)
Definition: DirectXRenderer.cpp:1867
virtual std::variant< std::set< std::pair< unsigned int, unsigned int > >, Error > getSupportedRefreshRates() const override
Definition: DirectXRenderer.cpp:503
void resetCommandListForGraphics(DirectXFrameResource *pCurrentFrameResource)
Definition: DirectXRenderer.cpp:545
std::variant< std::vector< DXGI_MODE_DESC >, Error > getSupportedDisplayModes() const
Definition: DirectXRenderer.cpp:2374
virtual std::string getCurrentlyUsedGpuName() const override
Definition: DirectXRenderer.cpp:522
virtual void present(FrameResource *pCurrentFrameResource, size_t iCurrentFrameResourceIndex) override
Definition: DirectXRenderer.cpp:1529
void dispatchComputeShadersOnGraphicsQueue(ID3D12CommandAllocator *pCommandAllocator, std::unordered_map< Pipeline *, std::unordered_set< ComputeShaderInterface * > > &computePipelinesToSubmit)
Definition: DirectXRenderer.cpp:1647
static constexpr DXGI_FORMAT depthStencilBufferFormat
Definition: DirectXRenderer.h:611
std::optional< Error > setOutputAdapter()
Definition: DirectXRenderer.cpp:1716
std::string sUsedVideoAdapter
Definition: DirectXRenderer.h:590
ComPtr< ID3D12GraphicsCommandList > pCommandList
Definition: DirectXRenderer.h:533
static constexpr DXGI_FORMAT getDepthBufferFormatNoMultisampling()
Definition: DirectXRenderer.h:77
D3D12_VIEWPORT screenViewport
Definition: DirectXRenderer.h:584
std::optional< Error > createCommandQueue()
Definition: DirectXRenderer.cpp:1736
virtual std::optional< Error > onRenderSettingsChangedDerived() override
Definition: DirectXRenderer.cpp:2105
bool bIsDirectXInitialized
Definition: DirectXRenderer.h:605
static size_t rateGpuSuitability(DXGI_ADAPTER_DESC1 adapterDesc)
Definition: DirectXRenderer.cpp:1697
static constexpr DXGI_FORMAT depthBufferNoMultisamplingFormat
Definition: DirectXRenderer.h:630
virtual void waitForGpuToFinishWorkUpToThisPoint() override
Definition: DirectXRenderer.cpp:2325
std::vector< std::string > vSupportedGpuNames
Definition: DirectXRenderer.h:575
std::optional< Error > createSwapChain()
Definition: DirectXRenderer.cpp:1807
ComPtr< ID3D12GraphicsCommandList > pComputeCommandList
Definition: DirectXRenderer.h:536
IDXGIAdapter3 * getVideoAdapter() const
Definition: DirectXRenderer.cpp:2084
virtual void drawMeshesDepthPrepass(FrameResource *pCurrentFrameResource, size_t iCurrentFrameResourceIndex, const std::vector< Renderer::MeshesInFrustum::PipelineInFrustumInfo > &vOpaquePipelines) override
Definition: DirectXRenderer.cpp:574
void executeGraphicsCommandList(ID3D12GraphicsCommandList *pCommandListToExecute)
Definition: DirectXRenderer.cpp:2309
virtual bool isInitialized() const override
Definition: DirectXRenderer.cpp:2307
std::unique_ptr< DirectXResource > pMsaaRenderBuffer
Definition: DirectXRenderer.h:572
std::optional< Error > initialize(const std::vector< std::string > &vBlacklistedGpuNames)
Definition: DirectXRenderer.cpp:121
UINT iPresentFlags
Definition: DirectXRenderer.h:596
virtual void prepareForDrawingNextFrame(CameraProperties *pCameraProperties, FrameResource *pCurrentFrameResource) override
Definition: DirectXRenderer.cpp:524
std::unique_ptr< DirectXResource > pDepthStencilBuffer
Definition: DirectXRenderer.h:556
virtual void executeComputeShadersOnGraphicsQueue(FrameResource *pCurrentFrameResource, size_t iCurrentFrameResourceIndex, ComputeExecutionStage stage) override
Definition: DirectXRenderer.cpp:1192
static constexpr DXGI_FORMAT getDepthStencilBufferFormat()
Definition: DirectXRenderer.h:70
ComPtr< IDXGISwapChain3 > pSwapChain
Definition: DirectXRenderer.h:527
virtual std::string getUsedApiVersion() const override
Definition: DirectXRenderer.cpp:2361
void waitForFenceValue(UINT64 iFenceToWaitFor)
Definition: DirectXRenderer.cpp:1625
ComPtr< IDXGIOutput > pOutputAdapter
Definition: DirectXRenderer.h:524
float backBufferFillColor[4]
Definition: DirectXRenderer.h:569
std::pair< unsigned int, unsigned int > renderTargetSize
Definition: DirectXRenderer.h:578
ComPtr< IDXGIAdapter3 > pVideoAdapter
Definition: DirectXRenderer.h:521
bool bIsUsingMsaaRenderTarget
Definition: DirectXRenderer.h:602
UINT getMsaaQualityLevel() const
Definition: DirectXRenderer.cpp:2086
std::vector< std::unique_ptr< DirectXResource > > vSwapChainBuffers
Definition: DirectXRenderer.h:553
virtual void drawMeshesMainPass(FrameResource *pCurrentFrameResource, size_t iCurrentFrameResourceIndex, const std::vector< Renderer::MeshesInFrustum::PipelineInFrustumInfo > &vOpaquePipelines, const std::vector< Renderer::MeshesInFrustum::PipelineInFrustumInfo > &vTransparentPipelines) override
Definition: DirectXRenderer.cpp:1212
std::unique_ptr< DirectXResource > pDepthBufferNoMultisampling
Definition: DirectXRenderer.h:566
UINT iPresentSyncInterval
Definition: DirectXRenderer.h:593
std::optional< Error > createDepthStencilBuffer()
Definition: DirectXRenderer.cpp:202
ID3D12GraphicsCommandList * getD3dCommandList()
Definition: DirectXRenderer.cpp:2370
D3D12_RECT scissorRect
Definition: DirectXRenderer.h:587
Definition: DirectXResource.h:32
Definition: Error.h:27
Definition: GameManager.h:34
Definition: GpuResource.h:16
Definition: Pipeline.h:20
Definition: Renderer.h:43
GameManager * pGameManager
Definition: Renderer.h:870
Definition: DirectXFrameResource.h:30
Definition: FrameResource.h:15
Definition: PipelineRegistry.hpp:31