LiteFX 0.4.1.2025
Computer Graphics Engine
Loading...
Searching...
No Matches
dx12_api.hpp
1#pragma once
2
3#include <litefx/config.h>
4
5#if !defined (LITEFX_DIRECTX12_API)
6# if defined(LiteFX_Backends_DirectX12_EXPORTS) && (defined _WIN32 || defined WINCE)
7# define LITEFX_DIRECTX12_API __declspec(dllexport)
8# elif (defined(LiteFX_Backends_DirectX12_EXPORTS) || defined(__APPLE__)) && defined __GNUC__ && __GNUC__ >= 4
9# define LITEFX_DIRECTX12_API __attribute__ ((visibility ("default")))
10# elif !defined(LiteFX_Backends_DirectX12_EXPORTS) && (defined _WIN32 || defined WINCE)
11# define LITEFX_DIRECTX12_API __declspec(dllimport)
12# endif
13#endif
14
15#ifndef LITEFX_DIRECTX12_API
16# define LITEFX_DIRECTX12_API
17#endif
18
19#if (!defined _WIN32 && !defined WINCE)
20# pragma message ("DirectX12: The DirectX backend requires Windows to be built on.")
21#endif
22
23#ifdef LITEFX_DEFINE_GLOBAL_EXPORTS
24#include <d3d12agility.hpp>
25#endif // LITEFX_DEFINE_GLOBAL_EXPORTS
26
27#include <directx/d3d12.h>
28#include <directx/dxcore.h>
29#include <directx/d3dx12.h>
30#include <dxguids/dxguids.h>
31#include <dxgi1_6.h>
32#include <dxcapi.h>
33#include <comdef.h>
34
35#include <wrl.h>
36using namespace Microsoft::WRL;
37
38#include <litefx/rendering.hpp>
39#include "dx12_formatters.hpp"
40
42 using namespace LiteFX::Math;
43 using namespace LiteFX::Rendering;
44
45 constexpr StringView DIRECTX12_LOG = "Backend::DirectX12"sv;
46
47 // Forward declarations.
59 class DirectX12Barrier;
69 class DirectX12Queue;
71 class DirectX12Device;
72 class DirectX12Backend;
73
74 // Interface declarations.
75 class IDirectX12Buffer;
78 class IDirectX12Image;
83
84#if defined(LITEFX_BUILD_DEFINE_BUILDERS)
85 // Builder declarations.
86 class DirectX12VertexBufferLayoutBuilder;
87 class DirectX12DescriptorSetLayoutBuilder;
88 class DirectX12PushConstantsLayoutBuilder;
89 class DirectX12PipelineLayoutBuilder;
90 class DirectX12ShaderProgramBuilder;
91 class DirectX12InputAssemblerBuilder;
92 class DirectX12RasterizerBuilder;
93 class DirectX12RenderPipelineBuilder;
94 class DirectX12ComputePipelineBuilder;
95 class DirectX12RayTracingPipelineBuilder;
96 class DirectX12RenderPassBuilder;
97 class DirectX12BarrierBuilder;
98#endif // defined(LITEFX_BUILD_DEFINE_BUILDERS)
99
104 template <class THandle>
106 // TODO: We could overwrite the handle() methods here and return `.AsWeak` from the ComPtr and create an overload to get a reference-counted handle, if required.
107
111 namespace DX12
112 {
116 Format LITEFX_DIRECTX12_API getFormat(const DXGI_FORMAT& format);
117
121 DXGI_FORMAT LITEFX_DIRECTX12_API getFormat(Format format);
122
126 DXGI_FORMAT LITEFX_DIRECTX12_API getFormat(BufferFormat format);
127
131 bool LITEFX_DIRECTX12_API isSRGB(Format format);
132
136 D3D12_RESOURCE_DIMENSION LITEFX_DIRECTX12_API getImageType(ImageDimensions dimensions);
137
141 PolygonMode LITEFX_DIRECTX12_API getPolygonMode(const D3D12_FILL_MODE& mode);
142
146 D3D12_FILL_MODE LITEFX_DIRECTX12_API getPolygonMode(PolygonMode mode);
147
151 CullMode LITEFX_DIRECTX12_API getCullMode(const D3D12_CULL_MODE& mode);
152
156 D3D12_CULL_MODE LITEFX_DIRECTX12_API getCullMode(CullMode mode);
157
161 PrimitiveTopology LITEFX_DIRECTX12_API getPrimitiveTopology(const D3D12_PRIMITIVE_TOPOLOGY& topology);
162
166 D3D12_PRIMITIVE_TOPOLOGY LITEFX_DIRECTX12_API getPrimitiveTopology(PrimitiveTopology topology);
167
171 D3D12_PRIMITIVE_TOPOLOGY_TYPE LITEFX_DIRECTX12_API getPrimitiveTopologyType(PrimitiveTopology topology);
172
176 LPCTSTR LITEFX_DIRECTX12_API getSemanticName(AttributeSemantic semantic);
177
183 String LITEFX_DIRECTX12_API getVendorName(UInt32 vendorId);
184
188 D3D12_COMPARISON_FUNC LITEFX_DIRECTX12_API getCompareOp(CompareOperation compareOp);
189
193 D3D12_STENCIL_OP LITEFX_DIRECTX12_API getStencilOp(StencilOperation stencilOp);
194
198 D3D12_BLEND LITEFX_DIRECTX12_API getBlendFactor(BlendFactor blendFactor);
199
203 D3D12_BLEND_OP LITEFX_DIRECTX12_API getBlendOperation(BlendOperation blendOperation);
204
208 D3D12_BARRIER_SYNC LITEFX_DIRECTX12_API getPipelineStage(PipelineStage pipelineStage);
209
213 D3D12_BARRIER_ACCESS LITEFX_DIRECTX12_API getResourceAccess(ResourceAccess resourceAccess);
214
218 D3D12_BARRIER_LAYOUT LITEFX_DIRECTX12_API getImageLayout(ImageLayout imageLayout);
219 }
220
224 class LITEFX_DIRECTX12_API DirectX12GraphicsAdapter final : public IGraphicsAdapter, public ComResource<IDXGIAdapter4> {
225 LITEFX_IMPLEMENTATION(DirectX12GraphicsAdapterImpl);
227
228 private:
233 explicit DirectX12GraphicsAdapter(ComPtr<IDXGIAdapter4> adapter);
234
237
240
242 DirectX12GraphicsAdapter& operator=(DirectX12GraphicsAdapter&&) noexcept = delete;
243
245 DirectX12GraphicsAdapter& operator=(const DirectX12GraphicsAdapter&) = delete;
246
247 public:
249 ~DirectX12GraphicsAdapter() noexcept override;
250
251 public:
257 static inline auto create(ComPtr<IDXGIAdapter4> adapter) {
258 return SharedObject::create<DirectX12GraphicsAdapter>(std::move(adapter));
259 }
260
261 public:
263 String name() const override;
264
266 UInt64 uniqueId() const noexcept override;
267
269 UInt32 vendorId() const noexcept override;
270
272 UInt32 deviceId() const noexcept override;
273
275 GraphicsAdapterType type() const noexcept override;
276
278 UInt64 driverVersion() const noexcept override;
279
281 UInt32 apiVersion() const noexcept override;
282
284 UInt64 dedicatedMemory() const noexcept override;
285 };
286
290 class LITEFX_DIRECTX12_API DirectX12Surface final : public ISurface, public Resource<HWND> {
291 public:
296 explicit DirectX12Surface(const HWND& hwnd) noexcept;
297
300
302 DirectX12Surface(DirectX12Surface&&) noexcept = default;
303
305 DirectX12Surface& operator=(const DirectX12Surface&) = delete;
306
308 DirectX12Surface& operator=(DirectX12Surface&&) noexcept = default;
309
311 ~DirectX12Surface() noexcept override;
312 };
313
317 class LITEFX_DIRECTX12_API DX12PlatformException : public RuntimeException {
318 private:
319 HRESULT m_code;
320 _com_error m_error;
321
322 public:
327 explicit DX12PlatformException(HRESULT result) :
328 RuntimeException("{1} (HRESULT 0x{0:08X})", static_cast<unsigned>(result), _com_error(result).ErrorMessage()), m_code(result), m_error(result) { }
329
335 explicit DX12PlatformException(HRESULT result, StringView message) :
336 RuntimeException("{2} {1} (HRESULT 0x{0:08X})", static_cast<unsigned>(result), _com_error(result).ErrorMessage(), message), m_code(result), m_error(result) { }
337
344 template <typename ...TArgs>
345 explicit DX12PlatformException(HRESULT result, std::format_string<TArgs...> format, TArgs&&... args) :
346 DX12PlatformException(result, std::format(format, std::forward<TArgs>(args)...)) { }
347
350
353
355 DX12PlatformException& operator=(DX12PlatformException&&) noexcept = default;
356
358 DX12PlatformException& operator=(const DX12PlatformException&) = default;
359
361 ~DX12PlatformException() noexcept override = default;
362
363 public:
368 const _com_error& error() const noexcept {
369 return m_error;
370 }
371
376 HRESULT code() const noexcept {
377 return m_code;
378 }
379 };
380
387 template <typename ...TArgs>
388 static inline void raiseIfFailed(HRESULT hr, std::format_string<TArgs...> message, TArgs&&... args) {
389 if (SUCCEEDED(hr)) [[likely]]
390 return;
391 else
392 throw DX12PlatformException(hr, message, std::forward<TArgs>(args)...);
393 }
394}
An exception that is thrown, if a requested D3D12 operation could not be executed.
Definition dx12_api.hpp:317
DX12PlatformException(HRESULT result, StringView message)
Initializes a new exception.
Definition dx12_api.hpp:335
DX12PlatformException(HRESULT result)
Initializes a new exception.
Definition dx12_api.hpp:327
HRESULT code() const noexcept
Returns the error code.
Definition dx12_api.hpp:376
DX12PlatformException(DX12PlatformException &&) noexcept=default
DX12PlatformException(HRESULT result, std::format_string< TArgs... > format, TArgs &&... args)
Initializes a new exception.
Definition dx12_api.hpp:345
Implements the DirectX 12 RenderBackend.
Definition dx12.hpp:2820
Implements a DirectX 12 resource barrier.
Definition dx12.hpp:477
Implements a DirectX 12 bottom-level acceleration structure (BLAS).
Definition dx12.hpp:295
Records commands for a DirectX12Queue
Definition dx12.hpp:1433
Implements a DirectX 12 ComputePipeline.
Definition dx12.hpp:1866
Implements a DirectX 12 IDescriptorLayout
Definition dx12.hpp:790
Implements a DirectX 12 DescriptorSet.
Definition dx12.hpp:705
Implements a DirectX 12 DescriptorSetLayout.
Definition dx12.hpp:866
Implements a DirectX 12 graphics device.
Definition dx12.hpp:2566
Implements a DirectX 12 frame buffer.
Definition dx12.hpp:2009
Implements a DirectX12 IGraphicsAdapter.
Definition dx12_api.hpp:224
A graphics factory that produces objects for a DirectX12Device.
Definition dx12.hpp:2471
Implements a DirectX 12 index buffer layout.
Definition dx12.hpp:112
Implements the DirectX 12 input assembler state.
Definition dx12.hpp:1250
Implements a DirectX 12 PipelineLayout.
Definition dx12.hpp:1146
Defines the base class for DirectX 12 pipeline state objects.
Definition dx12.hpp:1409
Implements the DirectX 12 PushConstantsLayout.
Definition dx12.hpp:1089
Implements the DirectX 12 IPushConstantsRange.
Definition dx12.hpp:1031
Implements a DirectX 12 command queue.
Definition dx12.hpp:1680
Implements a DirectX 12 IRasterizer.
Definition dx12.hpp:1334
Implements a DirectX 12 RayTracingPipeline.
Definition dx12.hpp:1927
Implements a DirectX 12 render pass.
Definition dx12.hpp:2148
Implements a DirectX 12 RenderPipeline.
Definition dx12.hpp:1789
Implements a DirectX 12 IShaderModule.
Definition dx12.hpp:556
Implements a DirectX 12 ShaderProgram.
Definition dx12.hpp:616
Implements a DirectX12 ISurface.
Definition dx12_api.hpp:290
DirectX12Surface(const DirectX12Surface &)=delete
DirectX12Surface(DirectX12Surface &&) noexcept=default
Implements a DirectX 12 swap chain.
Definition dx12.hpp:2356
Implements a DirectX 12 top-level acceleration structure (TLAS).
Definition dx12.hpp:389
Implements a DirectX 12 vertex buffer layout.
Definition dx12.hpp:21
Represents the base interface for a DirectX 12 acceleration structure implementation.
Definition dx12.hpp:279
Represents the base interface for a DirectX 12 buffer implementation.
Definition dx12.hpp:183
Represents a DirectX 12 sampled image or the base interface for a texture.
Definition dx12.hpp:242
Represents a DirectX 12 index buffer.
Definition dx12.hpp:220
Represents a DirectX 12 sampler.
Definition dx12.hpp:261
Represents a DirectX 12 vertex buffer.
Definition dx12.hpp:200
Represents a physical graphics adapter.
Definition rendering_api.hpp:2648
Represents a surface to render to.
Definition rendering_api.hpp:2720
Implements the IResource interface.
Definition containers.hpp:823
An exception that is thrown, if a requested operation could not be executed.
Definition exceptions.hpp:235
static auto create(TArgs &&... args) -> SharedPtr< T >
Generic factory method used to create instances of the shared object.
Definition containers.hpp:1114
Definition math.hpp:30
uint64_t UInt64
A type for an unsigned 64 bit integer.
Definition math.hpp:66
uint32_t UInt32
A type for an unsigned 32 bit integer.
Definition math.hpp:56
PolygonMode LITEFX_DIRECTX12_API getPolygonMode(const D3D12_FILL_MODE &mode)
Definition convert.cpp:381
Format LITEFX_DIRECTX12_API getFormat(const DXGI_FORMAT &format)
Definition convert.cpp:5
D3D12_BLEND_OP LITEFX_DIRECTX12_API getBlendOperation(BlendOperation blendOperation)
Definition convert.cpp:599
D3D12_BARRIER_SYNC LITEFX_DIRECTX12_API getPipelineStage(PipelineStage pipelineStage)
Definition convert.cpp:611
CullMode LITEFX_DIRECTX12_API getCullMode(const D3D12_CULL_MODE &mode)
Definition convert.cpp:407
D3D12_PRIMITIVE_TOPOLOGY_TYPE LITEFX_DIRECTX12_API getPrimitiveTopologyType(PrimitiveTopology topology)
Definition convert.cpp:475
PrimitiveTopology LITEFX_DIRECTX12_API getPrimitiveTopology(const D3D12_PRIMITIVE_TOPOLOGY &topology)
Definition convert.cpp:437
D3D12_BARRIER_LAYOUT LITEFX_DIRECTX12_API getImageLayout(ImageLayout imageLayout)
Definition convert.cpp:721
D3D12_BLEND LITEFX_DIRECTX12_API getBlendFactor(BlendFactor blendFactor)
Definition convert.cpp:573
D3D12_RESOURCE_DIMENSION LITEFX_DIRECTX12_API getImageType(ImageDimensions dimensions)
Definition convert.cpp:365
LPCTSTR LITEFX_DIRECTX12_API getSemanticName(AttributeSemantic semantic)
Definition convert.cpp:492
D3D12_STENCIL_OP LITEFX_DIRECTX12_API getStencilOp(StencilOperation stencilOp)
Definition convert.cpp:558
String LITEFX_DIRECTX12_API getVendorName(UInt32 vendorId)
Definition convert.cpp:521
D3D12_COMPARISON_FUNC LITEFX_DIRECTX12_API getCompareOp(CompareOperation compareOp)
Definition convert.cpp:543
D3D12_BARRIER_ACCESS LITEFX_DIRECTX12_API getResourceAccess(ResourceAccess resourceAccess)
Definition convert.cpp:663
bool LITEFX_DIRECTX12_API isSRGB(Format format)
Definition convert.cpp:348
Definition dx12.hpp:11
constexpr StringView DIRECTX12_LOG
Definition dx12_api.hpp:45
Definition dx12.hpp:11
ImageLayout
Specifies the layout of an IImage resource.
Definition rendering_api.hpp:1686
BlendFactor
Specifies a blend factor.
Definition rendering_api.hpp:1307
CullMode
Describes which faces are culled by the Rasterizer stage.
Definition rendering_api.hpp:991
GraphicsAdapterType
Defines different types of graphics adapters.
Definition rendering_api.hpp:75
CompareOperation
Describes the operation used to compare depth or stencil values during depth/stencil tests.
Definition rendering_api.hpp:1215
BlendOperation
Specifies a blend operation.
Definition rendering_api.hpp:1359
PrimitiveTopology
Describes the topology of a mesh primitive.
Definition rendering_api.hpp:765
PolygonMode
Describes the draw mode for polygons.
Definition rendering_api.hpp:970
AttributeSemantic
Describes the semantic of a buffer attribute.
Definition rendering_api.hpp:371
ImageDimensions
Describes the dimensions of a image resource, i.e. the dimensions that are required to access a texel...
Definition rendering_api.hpp:1086
StencilOperation
An operation that is applied to the stencil buffer.
Definition rendering_api.hpp:1261
PipelineStage
Defines pipeline stages as points where synchronization may occur.
Definition rendering_api.hpp:1373
BufferFormat
Describes a buffer attribute format.
Definition rendering_api.hpp:339
ResourceAccess
Defines how a IBuffer or IImage resource is accessed.
Definition rendering_api.hpp:1537
Format
Describes a texel format.
Definition rendering_api.hpp:182
std::string String
Definition string.hpp:24
std::string_view StringView
Definition string.hpp:26
An allocator used to allocate the shared object.
Definition containers.hpp:1098