LiteFX 0.3.1.2022
Computer Graphics Engine
dx12_api.hpp
1#pragma once
2
3#if !defined (LITEFX_DIRECTX12_API)
4# if defined(LiteFX_Backends_DirectX12_EXPORTS) && (defined _WIN32 || defined WINCE)
5# define LITEFX_DIRECTX12_API __declspec(dllexport)
6# elif (defined(LiteFX_Backends_DirectX12_EXPORTS) || defined(__APPLE__)) && defined __GNUC__ && __GNUC__ >= 4
7# define LITEFX_DIRECTX12_API __attribute__ ((visibility ("default")))
8# elif !defined(LiteFX_Backends_DirectX12_EXPORTS) && (defined _WIN32 || defined WINCE)
9# define LITEFX_DIRECTX12_API __declspec(dllimport)
10# endif
11#endif
12
13#ifndef LITEFX_DIRECTX12_API
14# define LITEFX_DIRECTX12_API
15#endif
16
17#if (!defined _WIN32 && !defined WINCE)
18# pragma message ("DirectX12: The DirectX backend requires Windows to be built on.")
19#endif
20
21#include <directx/d3d12.h>
22#include <directx/dxcore.h>
23#include <directx/d3dx12.h>
24#include <dxguids/dxguids.h>
25#include <dxgi1_6.h>
26#include <dxcapi.h>
27#include <comdef.h>
28
29#include <wrl.h>
30using namespace Microsoft::WRL;
31
32#include <litefx/config.h>
33#include <litefx/rendering.hpp>
34
36 using namespace LiteFX::Math;
37 using namespace LiteFX::Rendering;
38
39 constexpr char DIRECTX12_LOG[] = "Backend::DirectX12";
40
41 // Forward declarations.
53 class DirectX12Barrier;
63 class DirectX12Queue;
65 class DirectX12Device;
66 class DirectX12Backend;
67
68 // Interface declarations.
69 class IDirectX12Buffer;
72 class IDirectX12Image;
74
75#if defined(BUILD_DEFINE_BUILDERS)
76 // Builder declarations.
77 class DirectX12VertexBufferLayoutBuilder;
78 class DirectX12DescriptorSetLayoutBuilder;
79 class DirectX12PushConstantsLayoutBuilder;
80 class DirectX12PipelineLayoutBuilder;
81 class DirectX12ShaderProgramBuilder;
82 class DirectX12InputAssemblerBuilder;
83 class DirectX12RasterizerBuilder;
84 class DirectX12RenderPipelineBuilder;
85 class DirectX12ComputePipelineBuilder;
86 class DirectX12RenderPassBuilder;
87#endif // defined(BUILD_DEFINE_BUILDERS)
88
93 template <class THandle>
95 // 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.
96
100 namespace DX12
101 {
105 Format LITEFX_DIRECTX12_API getFormat(const DXGI_FORMAT& format);
106
110 DXGI_FORMAT LITEFX_DIRECTX12_API getFormat(const Format& format);
111
115 DXGI_FORMAT LITEFX_DIRECTX12_API getFormat(const BufferFormat& format);
116
120 bool LITEFX_DIRECTX12_API isSRGB(const Format& format);
121
125 D3D12_RESOURCE_DIMENSION LITEFX_DIRECTX12_API getImageType(const ImageDimensions& dimensions);
126
130 PolygonMode LITEFX_DIRECTX12_API getPolygonMode(const D3D12_FILL_MODE& mode);
131
135 D3D12_FILL_MODE LITEFX_DIRECTX12_API getPolygonMode(const PolygonMode& mode);
136
140 CullMode LITEFX_DIRECTX12_API getCullMode(const D3D12_CULL_MODE& mode);
141
145 D3D12_CULL_MODE LITEFX_DIRECTX12_API getCullMode(const CullMode& mode);
146
150 PrimitiveTopology LITEFX_DIRECTX12_API getPrimitiveTopology(const D3D12_PRIMITIVE_TOPOLOGY& topology);
151
155 D3D12_PRIMITIVE_TOPOLOGY LITEFX_DIRECTX12_API getPrimitiveTopology(const PrimitiveTopology& topology);
156
160 D3D12_PRIMITIVE_TOPOLOGY_TYPE LITEFX_DIRECTX12_API getPrimitiveTopologyType(const PrimitiveTopology& topology);
161
165 LPCTSTR LITEFX_DIRECTX12_API getSemanticName(const AttributeSemantic& semantic);
166
172 String LITEFX_DIRECTX12_API getVendorName(const UInt32& vendorId);
173
177 D3D12_COMPARISON_FUNC LITEFX_DIRECTX12_API getCompareOp(const CompareOperation& compareOp);
178
182 D3D12_STENCIL_OP LITEFX_DIRECTX12_API getStencilOp(const StencilOperation& stencilOp);
183
187 D3D12_BLEND LITEFX_DIRECTX12_API getBlendFactor(const BlendFactor& blendFactor);
188
192 D3D12_BLEND_OP LITEFX_DIRECTX12_API getBlendOperation(const BlendOperation& blendOperation);
193
197 D3D12_RESOURCE_STATES LITEFX_DIRECTX12_API getResourceState(const ResourceState& resourceState);
198 }
199
203 class LITEFX_DIRECTX12_API DirectX12GraphicsAdapter : public IGraphicsAdapter, public ComResource<IDXGIAdapter4> {
204 LITEFX_IMPLEMENTATION(DirectX12GraphicsAdapterImpl);
205
206 public:
211 explicit DirectX12GraphicsAdapter(ComPtr<IDXGIAdapter4> adapter);
214 virtual ~DirectX12GraphicsAdapter() noexcept;
215
216 public:
218 virtual String name() const noexcept override;
219
221 virtual UInt64 uniqueId() const noexcept override;
222
224 virtual UInt32 vendorId() const noexcept override;
225
227 virtual UInt32 deviceId() const noexcept override;
228
230 virtual GraphicsAdapterType type() const noexcept override;
231
236 virtual UInt32 driverVersion() const noexcept override;
237
242 virtual UInt32 apiVersion() const noexcept override;
243
245 virtual UInt64 dedicatedMemory() const noexcept override;
246 };
247
251 class LITEFX_DIRECTX12_API DirectX12Surface : public ISurface, public Resource<HWND> {
252 public:
257 explicit DirectX12Surface(const HWND& hwnd) noexcept;
260 virtual ~DirectX12Surface() noexcept;
261 };
262
263 DEFINE_EXCEPTION(DX12PlatformException, std::runtime_error);
264
265 template <typename TException, typename ...TArgs>
266 inline void raiseIfFailed(HRESULT hr, StringView message, TArgs&&... args) {
267 if (SUCCEEDED(hr)) [[likely]]
268 return;
269
270 _com_error error(hr);
271
272 if (message.empty())
273 throw TException(DX12PlatformException("{1} (HRESULT 0x{0:08X})", static_cast<unsigned>(hr), error.ErrorMessage()));
274 else
275 throw TException(DX12PlatformException("{1} (HRESULT 0x{0:08X})", static_cast<unsigned>(hr), error.ErrorMessage()), fmt::format(fmt::runtime(message), std::forward<TArgs>(args)...));
276 }
277}
Implements the DirectX 12 RenderBackend.
Definition: dx12.hpp:1592
Implements a DirectX 12 resource barrier.
Definition: dx12.hpp:154
Records commands for a DirectX12CommandQueue
Definition: dx12.hpp:789
Implements a DirectX 12 ComputePipeline.
Definition: dx12.hpp:929
Implements a DirectX 12 IDescriptorLayout
Definition: dx12.hpp:386
Implements a DirectX 12 DescriptorSet.
Definition: dx12.hpp:317
Implements a DirectX 12 DescriptorSetLayout.
Definition: dx12.hpp:438
Implements a DirectX 12 graphics device.
Definition: dx12.hpp:1399
Implements a DirectX 12 frame buffer.
Definition: dx12.hpp:970
Implements a DirectX12 IGraphicsAdapter.
Definition: dx12_api.hpp:203
DirectX12GraphicsAdapter(const DirectX12GraphicsAdapter &)=delete
DirectX12GraphicsAdapter(DirectX12GraphicsAdapter &&)=delete
A graphics factory that produces objects for a DirectX12Device.
Definition: dx12.hpp:1339
Implements a DirectX 12 index buffer layout.
Definition: dx12.hpp:55
Implements the DirectX 12 input assembler state.
Definition: dx12.hpp:703
Implements a IInputAttachmentMapping.
Definition: dx12.hpp:1166
Implements a DirectX 12 PipelineLayout.
Definition: dx12.hpp:655
Defines the base class for DirectX 12 pipeline state objects.
Definition: dx12.hpp:772
Implements the DirectX 12 PushConstantsLayout.
Definition: dx12.hpp:610
Implements the DirectX 12 IPushConstantsRange.
Definition: dx12.hpp:550
Implements a DirectX 12 command queue.
Definition: dx12.hpp:1276
Implements a DirectX 12 IRasterizer.
Definition: dx12.hpp:743
Implements a DirectX 12 render pass.
Definition: dx12.hpp:1065
Implements a DirectX 12 RenderPipeline.
Definition: dx12.hpp:859
Implements a DirectX 12 IShaderModule.
Definition: dx12.hpp:220
Implements a DirectX 12 ShaderProgram.
Definition: dx12.hpp:263
Implements a DirectX12 ISurface.
Definition: dx12_api.hpp:251
DirectX12Surface(const DirectX12Surface &)=delete
DirectX12Surface(DirectX12Surface &&)=delete
Implements a DirectX 12 swap chain.
Definition: dx12.hpp:1220
Implements a DirectX 12 vertex buffer layout.
Definition: dx12.hpp:18
Represents the base interface for a DirectX 12 buffer implementation.
Definition: dx12.hpp:92
Represents a DirectX 12 sampled image or the base interface for a texture.
Definition: dx12.hpp:130
Represents a DirectX 12 index buffer.
Definition: dx12.hpp:115
Represents a DirectX 12 sampler.
Definition: dx12.hpp:142
Represents a DirectX 12 vertex buffer.
Definition: dx12.hpp:102
Represents a physical graphics adapter.
Definition: rendering_api.hpp:1754
Represents a surface to render to.
Definition: rendering_api.hpp:1815
Implements the IResource interface.
Definition: containers.hpp:371
Definition: math.hpp:30
uint32_t UInt32
Definition: math.hpp:37
uint64_t UInt64
Definition: math.hpp:39
PolygonMode LITEFX_DIRECTX12_API getPolygonMode(const D3D12_FILL_MODE &mode)
Definition: convert.cpp:381
LPCTSTR LITEFX_DIRECTX12_API getSemanticName(const AttributeSemantic &semantic)
Definition: convert.cpp:492
Format LITEFX_DIRECTX12_API getFormat(const DXGI_FORMAT &format)
Definition: convert.cpp:5
CullMode LITEFX_DIRECTX12_API getCullMode(const D3D12_CULL_MODE &mode)
Definition: convert.cpp:407
D3D12_STENCIL_OP LITEFX_DIRECTX12_API getStencilOp(const StencilOperation &stencilOp)
Definition: convert.cpp:556
PrimitiveTopology LITEFX_DIRECTX12_API getPrimitiveTopology(const D3D12_PRIMITIVE_TOPOLOGY &topology)
Definition: convert.cpp:437
bool LITEFX_DIRECTX12_API isSRGB(const Format &format)
Definition: convert.cpp:348
D3D12_PRIMITIVE_TOPOLOGY_TYPE LITEFX_DIRECTX12_API getPrimitiveTopologyType(const PrimitiveTopology &topology)
Definition: convert.cpp:475
D3D12_COMPARISON_FUNC LITEFX_DIRECTX12_API getCompareOp(const CompareOperation &compareOp)
Definition: convert.cpp:541
D3D12_RESOURCE_STATES LITEFX_DIRECTX12_API getResourceState(const ResourceState &resourceState)
Definition: convert.cpp:609
D3D12_BLEND LITEFX_DIRECTX12_API getBlendFactor(const BlendFactor &blendFactor)
Definition: convert.cpp:571
String LITEFX_DIRECTX12_API getVendorName(const UInt32 &vendorId)
Definition: convert.cpp:521
D3D12_BLEND_OP LITEFX_DIRECTX12_API getBlendOperation(const BlendOperation &blendOperation)
Definition: convert.cpp:597
D3D12_RESOURCE_DIMENSION LITEFX_DIRECTX12_API getImageType(const ImageDimensions &dimensions)
Definition: convert.cpp:365
Definition: dx12.hpp:8
void raiseIfFailed(HRESULT hr, StringView message, TArgs &&... args)
Definition: dx12_api.hpp:266
DEFINE_EXCEPTION(DX12PlatformException, std::runtime_error)
constexpr char DIRECTX12_LOG[]
Definition: dx12_api.hpp:39
Definition: dx12.hpp:8
std::string String
Definition: string.hpp:19
std::string_view StringView
Definition: string.hpp:21
auto format(LiteFX::Platform t, FormatContext &ctx) const
Definition: app_formatters.hpp:8