RavEngine
Loading...
Searching...
No Matches
D3D12CommandBuffer.hpp
1#pragma once
2#include <RGL/Types.hpp>
3#include <RGL/CommandBuffer.hpp>
4#include "RGLD3D12.hpp"
5#include "D3D12TrackedResource.hpp"
6#include <directx/d3d12.h>
7#include <memory>
8#include <unordered_map>
9
10namespace RGL {
12 const struct D3D12TrackedResource* texture = nullptr;
13 covered_mips_t coveredMips;
14 covered_layers_t coveredLayers;
15 D3D12TextureLastUseKey(decltype(texture) texture, decltype(coveredMips) coveredMips, decltype(coveredLayers) coveredLayers) : texture(texture), coveredMips(coveredMips), coveredLayers(coveredLayers) {}
16 bool operator==(const D3D12TextureLastUseKey& other) const {
17 return texture == other.texture && coveredMips == other.coveredMips && coveredLayers == other.coveredLayers;
18 }
19 };
20}
21
22namespace std {
23 template<>
24 struct hash<RGL::D3D12TextureLastUseKey> {
25 size_t operator()(const RGL::D3D12TextureLastUseKey& other) const {
26 return uintptr_t(other.texture) ^ other.coveredMips ^ other.coveredLayers;
27 }
28 };
29}
30
31namespace RGL {
32 struct CommandQueueD3D12;
33 struct TextureD3D12;
34 struct BufferD3D12;
35
37 Microsoft::WRL::ComPtr<ID3D12GraphicsCommandList2> commandList;
38 const std::shared_ptr<CommandQueueD3D12> owningQueue;
39
40 std::shared_ptr<struct RenderPassD3D12> currentRenderPass;
41 std::shared_ptr<struct RenderPipelineD3D12> currentRenderPipeline;
42 std::shared_ptr<struct ComputePipelineD3D12> currentComputePipeline;
43
44 //TODO: also store if the last use was a write or a read
45 //if a read, then we need to insert a standard barrier even if it's already in the right state
47 D3D12_RESOURCE_STATES state;
48 bool written = false;
49 };
50 std::unordered_map<const struct D3D12TrackedResource*, ResourceLastUse> activeBuffers;
51
52 std::unordered_map<D3D12TextureLastUseKey, ResourceLastUse> activeTextures;
53
54 bool ended = false;
55
56 CommandBufferD3D12(decltype(owningQueue));
57
58 // clear the command buffer, to encode new commands
59 void Reset() final;
60
61 // signal that new commands are arriving
62 void Begin() final;
63
64 // signal that all commands ahve been encoded
65 void End() final;
66
67 void BeginRendering(RGLRenderPassPtr) final;
68 void EndRendering() final;
69
70 void BindRenderPipeline(RGLRenderPipelinePtr) final;
71 void BeginCompute(RGLComputePipelinePtr) final;
72 void EndCompute() final;
73 void DispatchCompute(uint32_t threadsX, uint32_t threadsY, uint32_t threadsZ, uint32_t threadsPerThreadgroupX=1, uint32_t threadsPerThreadgroupY=1, uint32_t threadsPerThreadgroupZ=1) final;
74
75 void BindBuffer(RGLBufferPtr buffer, uint32_t bindingOffset, uint32_t offsetIntoBuffer = 0) final;
76 void BindComputeBuffer(RGLBufferPtr buffer, uint32_t binding, uint32_t offsetIntoBuffer = 0) final;
77 void SetVertexBuffer(RGLBufferPtr buffer, const VertexBufferBinding& bindingInfo = {}) final;
78
79 void SetVertexBytes(const untyped_span data, uint32_t offset) final;
80 void SetFragmentBytes(const untyped_span data, uint32_t offset) final;
81 void SetComputeBytes(const untyped_span data, uint32_t offset) final;
82
83 void SetIndexBuffer(RGLBufferPtr buffer) final;
84
85 void SetVertexSampler(RGLSamplerPtr sampler, uint32_t index) final;
86 void SetFragmentSampler(RGLSamplerPtr sampler, uint32_t index) final;
87 void SetComputeSampler(RGLSamplerPtr sampler, uint32_t index) final;
88
89 void SetVertexTexture(const TextureView& texture, uint32_t index) final;
90 void SetFragmentTexture(const TextureView& texture, uint32_t index) final;
91 void SetComputeTexture(const TextureView& texture, uint32_t index) final;
92
93 void Draw(uint32_t nVertices, const DrawInstancedConfig & = {}) final;
94 void DrawIndexed(uint32_t nIndices, const DrawIndexedInstancedConfig & = {}) final;
95
96 void BindBindlessBufferDescriptorSet(uint32_t set_idx) final;
97
98 void SetViewport(const Viewport&) final;
99 void SetScissor(const Rect&) final;
100
101 void CopyTextureToBuffer(TextureView& sourceTexture, const Rect& sourceRect, size_t offset, RGLBufferPtr desetBuffer) final;
102
103 void CopyBufferToTexture(RGLBufferPtr source, uint32_t size, const TextureDestConfig& dest) final;
104
105 void CopyBufferToBuffer(BufferCopyConfig from, BufferCopyConfig to, uint32_t size) final;
106
107 void CopyTextureToTexture(const TextureCopyConfig& from, const TextureCopyConfig& to) final;
108
109 // submit onto the queue that created this command buffer
110 void Commit(const CommitConfig&) final;
111
112 void ExecuteIndirectIndexed(const IndirectConfig&) final;
113 void ExecuteIndirect(const IndirectConfig&) final;
114 void DispatchIndirect(const DispatchIndirectConfig&) final;
115
116 void BeginRenderDebugMarker(const std::string& label) final;
117 void BeginComputeDebugMarker(const std::string& label) final;
118
119 void EndRenderDebugMarker() final;
120 void EndComputeDebugMarker() final;
121
122 void UseResource(const TextureView& tx) final;
123 void UseResource(const RGLBufferPtr buffer) final;
124
125 void BlockUntilCompleted() final;
126
127 virtual ~CommandBufferD3D12();
128 private:
129 void SyncIfNeeded(const BufferD3D12* buffer, D3D12_RESOURCE_STATES needed, bool written = false);
130 void SyncIfNeeded(TextureView texture, D3D12_RESOURCE_STATES needed, bool written);
131 D3D12_RESOURCE_STATES GetBufferCurrentResourceState(const struct D3D12TrackedResource*);
132 HANDLE internalFenceEvent;
133 Microsoft::WRL::ComPtr<ID3D12Fence> internalFence;
134 };
135}
Definition Span.hpp:11
Definition CommandBuffer.hpp:18
Definition CommandBuffer.hpp:11
Definition D3D12CommandBuffer.hpp:46
Definition D3D12CommandBuffer.hpp:36
Definition D3D12CommandBuffer.hpp:11
Definition D3D12TrackedResource.hpp:6
Definition CommandBuffer.hpp:99
Definition Texture.hpp:43
Definition CommandBuffer.hpp:86