Blowbox 2017
A 3D Game Engine by Riko Ophorst using DirectX 12
command_context.h
1 #pragma once
2 
3 #include "util/string.h"
4 
5 #include "renderer/buffers/gpu_resource.h"
6 #include "renderer/buffers/gpu_buffer.h"
7 #include "renderer/buffers/structured_buffer.h"
8 #include "renderer/buffers/byte_address_buffer.h"
9 #include "renderer/buffers/depth_buffer.h"
10 #include "renderer/buffers/color_buffer.h"
11 #include "renderer/buffers/upload_buffer.h"
12 
13 #define COMMAND_CONTEXT_BARRIER_BUFFER_SIZE 16
14 
15 namespace blowbox
16 {
20  struct Param32Bit
21  {
23  Param32Bit(FLOAT f_) : f(f_) {}
24 
26  Param32Bit(UINT u_) : u(u_) {}
27 
29  Param32Bit(INT i_) : i(i_) {}
30 
35  void operator=(FLOAT f_) { f = f_; }
36 
41  void operator=(UINT u_) { u = u_; }
42 
47  void operator=(INT i_) { i = i_; }
48 
49  union
50  {
51  FLOAT f;
52  UINT u;
53  INT i;
54  };
55  };
56 
57  class GraphicsContext;
58  class ComputeContext;
59 
63  struct NonCopyable
64  {
65  NonCopyable() = default;
66  NonCopyable(const NonCopyable&) = delete;
67  NonCopyable& operator=(const NonCopyable&) = delete;
68  };
69 
78  {
79  friend class CommandContextManager;
80  private:
85  CommandContext(D3D12_COMMAND_LIST_TYPE type);
86 
91  void Reset();
92 
93  public:
95  ~CommandContext();
96 
103  static CommandContext& Begin(const WString& name = L"");
104 
106  GraphicsContext& GetGraphicsContext();
107 
109  ComputeContext& GetComputeContext();
110 
112  void Initialize();
113 
120  static void InitializeTexture(GpuResource& dest_resource, UINT num_subresources, D3D12_SUBRESOURCE_DATA subresource_data[]);
121 
130  static void InitializeBuffer(GpuResource& dest_resource, const void* data, UINT num_bytes, bool use_offset = false, UINT offset = 0);
131 
137  void CopyBuffer(GpuResource& dest_resource, UploadBuffer& source_resource);
138 
144  void CopyBuffer(GpuResource& dest_resource, GpuResource& source_resource);
145 
154  void CopyBufferRegion(GpuResource& dest_resource, UINT dest_offset, GpuResource& source_resource, UINT source_offset, UINT num_bytes);
155 
163  void CopySubresource(GpuResource& dest_resource, UINT dest_subresource_index, GpuResource& source_resource, UINT source_subresource_index);
164 
171  void CopyCounter(GpuResource& dest_resource, UINT dest_offset, StructuredBuffer& source_resource);
172 
177  uint64_t Flush(bool wait_for_completion = false);
178 
183  uint64_t Finish(bool wait_for_completion = false);
184 
191  void TransitionResource(GpuResource& resource, const D3D12_RESOURCE_STATES& new_state, bool flush_immediate = false);
192 
199  void BeginResourceTransition(GpuResource& resource, const D3D12_RESOURCE_STATES& new_state, bool flush_immediate = false);
200 
206  void InsertUAVBarrier(GpuResource& resource, bool flush_immediate = false);
207 
214  void InsertAliasBuffer(GpuResource& before, GpuResource& after, bool flush_immediate = false);
215 
217  void FlushResourceBarriers();
218 
224  void InsertTimeStamp(ID3D12QueryHeap* query_heap, UINT query_idx);
225 
232  void ResolveTimeStamps(ID3D12Resource* read_back_heap, ID3D12QueryHeap* query_heap, UINT num_queries);
233 
239  void SetDescriptorHeap(D3D12_DESCRIPTOR_HEAP_TYPE type, ID3D12DescriptorHeap* heap);
240 
247  void SetDescriptorHeaps(UINT heap_count, D3D12_DESCRIPTOR_HEAP_TYPE heap_types[], ID3D12DescriptorHeap* heaps[]);
248 
255  void SetPredication(ID3D12Resource* buffer, UINT64 buffer_offset, D3D12_PREDICATION_OP op);
256 
258  ID3D12GraphicsCommandList* Get();
259 
260  protected:
262  void BindDescriptorHeaps();
263 
268  void SetName(const WString& name) { list_->SetName(name.c_str()); name_ = name; }
269 
270  protected:
272  D3D12_COMMAND_LIST_TYPE type_;
273  ID3D12GraphicsCommandList* list_;
274  ID3D12CommandAllocator* allocator_;
275 
277  D3D12_RESOURCE_BARRIER resource_barrier_buffer_[COMMAND_CONTEXT_BARRIER_BUFFER_SIZE];
278 
279  ID3D12DescriptorHeap* descriptor_heaps_[4];
280 
281  ID3D12RootSignature* graphics_root_signature_;
282  ID3D12PipelineState* graphics_pipeline_state_;
283  ID3D12RootSignature* compute_root_signature_;
284  ID3D12PipelineState* compute_pipeline_state_;
285  };
286 }
void operator=(INT i_)
The assignment operator for ints.
Definition: command_context.h:47
Wrapper around D3D12 command lists.
Definition: command_context.h:77
void operator=(FLOAT f_)
The assignment operator for floats.
Definition: command_context.h:35
void operator=(UINT u_)
The assignment operator for uints.
Definition: command_context.h:41
UINT num_flushable_barriers_
The number of resource barriers that are queued up.
Definition: command_context.h:276
INT i
The int value.
Definition: command_context.h:53
ID3D12RootSignature * compute_root_signature_
The compute root signature that is currently bound.
Definition: command_context.h:283
Provides clean access to all subsystems.
Definition: get.h:44
void SetName(const WString &name)
Sets the name of the CommandContext.
Definition: command_context.h:268
ID3D12PipelineState * graphics_pipeline_state_
The GraphicsPSO that is currently bound.
Definition: command_context.h:282
Wraps ID3D12Resource objects.
Definition: gpu_resource.h:19
ID3D12CommandAllocator * allocator_
The allocator used to allocate the commands.
Definition: command_context.h:274
ID3D12PipelineState * compute_pipeline_state_
The ComputePSO that is currently bound.
Definition: command_context.h:284
FLOAT f
The floating point value.
Definition: command_context.h:51
Param32Bit(UINT u_)
Constructs a uint-based Param32Bit.
Definition: command_context.h:26
eastl::wstring WString
Typedef for wrapping the EASTL wstring.
Definition: string.h:20
Param32Bit(INT i_)
Constructs a int-based Param32Bit.
Definition: command_context.h:29
ID3D12RootSignature * graphics_root_signature_
The graphics root signature that is currently bound.
Definition: command_context.h:281
Param32Bit(FLOAT f_)
Constructs a float-based Param32Bit.
Definition: command_context.h:23
Wraps functionality for submitting graphics related command lists.
Definition: graphics_context.h:16
ID3D12GraphicsCommandList * list_
The actual underlying command list.
Definition: command_context.h:273
The main Blowbox namespace.
Definition: image.cc:8
Wrapper for StructuredBuffer types.
Definition: structured_buffer.h:18
Manages the pool of available CommandContexts.
Definition: command_context_manager.h:23
Wraps upload buffer types, which allow you to upload data to the GPU.
Definition: upload_buffer.h:19
Represents a 32 bit parameter, which can either be a float, uint or int.
Definition: command_context.h:20
Utility class - if you inherit from this, the class becomes non-copyable.
Definition: command_context.h:63
WString name_
The name of the CommandContext.
Definition: command_context.h:271
D3D12_COMMAND_LIST_TYPE type_
The type of CommandContext this is.
Definition: command_context.h:272
UINT u
The uint value.
Definition: command_context.h:52