Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 184 additions & 0 deletions src/d3d12/d3d12_command_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ MTLD3D12CommandAllocatorImpl::Initialize() {
encoder_last = nullptr;
encoder_count_ = 0;

icb_.clear();

return S_OK;
}

Expand Down Expand Up @@ -134,6 +136,188 @@ MTLD3D12CommandAllocatorImpl::Reset() {
return Initialize();
};

IndirectComputeCommandData *
MTLD3D12CommandAllocatorImpl::EncodeIndirectComputeCommand(MTLD3D12CommandSignature *pCmdSig, MTLD3D12ComputePipelineState *pPSO, size_t MaxCount) {
WMTIndirectCommandBufferInfo info;
info.inherit_buffers = !pCmdSig->UpdateRootArguments;
info.inherit_pso = 1;
info.inherit_cull_mode = 0;
info.inherit_fill_mode = 0;
info.inherit_front_facing = 0;
info.inherit_depth_bias = 0;
info.inherit_depth_clip_mode = 0;
info.inherit_depth_stencil_state = 0;
info.support_color_attachment_mapping = 0;
info.support_dynamic_attribute_stride = 0;
info.support_ray_tracing = 0;
info.type = WMTIndirectCommandTypeConcurrentDispatch;
info.max_vertex_buffer_binding = 0;
info.max_fragment_buffer_binding = 0;
info.max_object_buffer_binding = 0;
info.max_mesh_buffer_binding = 0;
info.max_kernel_buffer_binding = 31;
info.max_kernel_threadgroup_memory_binding = 0;
info.max_object_threadgroup_memory_binding = 0;
info.gpu_resource_id = 0;

auto icb = device_->GetMTLDevice().newIndirectCommandBuffer(info, MaxCount, WMTResourceStorageModeShared);

auto [Ptr, Offset] = AllocateGPUHeap(sizeof(IndirectComputeCommandData), 16);

auto data = reinterpret_cast<IndirectComputeCommandData *>(Ptr);

data->cmd_buf = info.gpu_resource_id;
data->max_count = MaxCount;
data->tgsize_x = pPSO->threadgroup_size.width;
data->tgsize_y = pPSO->threadgroup_size.height;
data->tgsize_z = pPSO->threadgroup_size.depth;

{
// populated outside
data->max_count_buffer = 0;
data->argument_buffer = 0;
data->rootsig_qwords = 0;
data->rootsig_qwords_stride = 0;
data->static_samplers = 0;
}

{
/**
* TODO: move these out?
*/

auto &cmd_use_icb = EncodeComputeCommand<wmtcmd_compute_useresource>();
cmd_use_icb.type = WMTComputeCommandUseResource;
cmd_use_icb.usage = WMTResourceUsageRead | WMTResourceUsageWrite;
cmd_use_icb.resource = icb;

auto &cmd_setpso_res = EncodeComputeCommand<wmtcmd_compute_setpso>();
cmd_setpso_res.type = WMTComputeCommandSetPSO;
cmd_setpso_res.pso = pCmdSig->compute_resolver;
cmd_setpso_res.threadgroup_size = {1, 1, 1};

auto &cmd_argbuf_res = EncodeComputeCommand<wmtcmd_compute_setbuffer>();
cmd_argbuf_res.type = WMTComputeCommandSetBuffer;
cmd_argbuf_res.buffer = gpu_heap_buffer_;
cmd_argbuf_res.offset = Offset;
cmd_argbuf_res.index = 30;

auto &cmd_dispatch_res = EncodeComputeCommand<wmtcmd_compute_dispatch>();
cmd_dispatch_res.type = WMTComputeCommandDispatch;
cmd_dispatch_res.size = {1, 1, 1};

auto &cmd_setpso = EncodeComputeCommand<wmtcmd_compute_setpso>();
cmd_setpso.type = WMTComputeCommandSetPSO;
cmd_setpso.pso = pPSO->pso;
cmd_setpso.threadgroup_size = pPSO->threadgroup_size; // not really used
}

auto &cmd = EncodeComputeCommand<wmtcmd_compute_executecommands>();
cmd.type = WMTComputeCommandExecuteCommandsInBuffer;
cmd.indirect_command_buffer = icb;
cmd.location = 0;
cmd.length = MaxCount;

icb_.push_back(std::move(icb));

return data;
}

IndirectRenderCommandData *
MTLD3D12CommandAllocatorImpl::EncodeIndirectRenderCommand(
MTLD3D12CommandSignature *pCmdSig, MTLD3D12GraphicsPipelineState *pPSO, size_t MaxCount
) {
WMTIndirectCommandBufferInfo info;
info.inherit_buffers = !(pCmdSig->UpdateVertexBuffers || pCmdSig->UpdateIndexBuffer || pCmdSig->UpdateRootArguments);
info.inherit_pso = 1;
info.inherit_cull_mode = 1;
info.inherit_fill_mode = 1;
info.inherit_front_facing = 1;
info.inherit_depth_bias = 1;
info.inherit_depth_clip_mode = 1;
info.inherit_depth_stencil_state = 1;
info.support_color_attachment_mapping = 0;
info.support_dynamic_attribute_stride = 0;
info.support_ray_tracing = 0;
info.type = pCmdSig->CommandType == D3D12_INDIRECT_ARGUMENT_TYPE_DRAW_INDEXED ? WMTIndirectCommandTypeDrawIndexed
: WMTIndirectCommandTypeDraw;
info.max_vertex_buffer_binding = 31;
info.max_fragment_buffer_binding = 31;
info.max_object_buffer_binding = 0;
info.max_mesh_buffer_binding = 0;
info.max_kernel_buffer_binding = 0;
info.max_kernel_threadgroup_memory_binding = 0;
info.max_object_threadgroup_memory_binding = 0;
info.gpu_resource_id = 0;

auto icb = device_->GetMTLDevice().newIndirectCommandBuffer(info, MaxCount, WMTResourceStorageModePrivate);

auto [Ptr, Offset] = AllocateGPUHeap(sizeof(IndirectRenderCommandData), 16);

auto data = reinterpret_cast<IndirectRenderCommandData *>(Ptr);

data->cmd_buf = info.gpu_resource_id;
data->max_count = MaxCount;

{
// populated outside
data->max_count_buffer = 0;
data->argument_buffer = 0;
data->rootsig_qwords = 0;
data->rootsig_qwords_stride = 0;
data->static_samplers = 0;
data->vertex_buffer = 0;
data->vertex_argbuf_stride = 0;
data->primitive_type = 0;
data->index_buffer = 0;
data->index_buffer_format = {};
}

{
/**
* TODO: move these out?
*/

auto &cmd_use_icb = EncodeRenderCommand<wmtcmd_render_useresource>();
cmd_use_icb.type = WMTRenderCommandUseResource;
cmd_use_icb.stages = WMTRenderStageVertex;
cmd_use_icb.usage = WMTResourceUsageRead | WMTResourceUsageWrite;
cmd_use_icb.resource = icb;

auto &cmd_setpso_res = EncodeRenderCommand<wmtcmd_render_setpso>();
cmd_setpso_res.type = WMTRenderCommandSetPSO;
cmd_setpso_res.pso = pCmdSig->render_resolver;

auto &cmd_argbuf_res = EncodeRenderCommand<wmtcmd_render_setbuffer>();
cmd_argbuf_res.type = WMTRenderCommandSetVertexBuffer;
cmd_argbuf_res.buffer = gpu_heap_buffer_;
cmd_argbuf_res.offset = Offset;
cmd_argbuf_res.index = 30;

auto &cmd_draw_res = EncodeRenderCommand<wmtcmd_render_draw>();
cmd_draw_res.type = WMTRenderCommandDraw;
cmd_draw_res.primitive_type = WMTPrimitiveTypePoint;
cmd_draw_res.vertex_start = 0;
cmd_draw_res.vertex_count = 1;
cmd_draw_res.base_instance = 0;
cmd_draw_res.instance_count = 1;

auto &cmd_setpso = EncodeRenderCommand<wmtcmd_render_setpso>();
cmd_setpso.type = WMTRenderCommandSetPSO;
cmd_setpso.pso = pPSO->pso;
}

auto &cmd = EncodeRenderCommand<wmtcmd_render_executecommands>();
cmd.type = WMTRenderCommandExecuteCommandsInBuffer;
cmd.indirect_command_buffer = icb;
cmd.location = 0;
cmd.length = MaxCount;

icb_.push_back(std::move(icb));

return data;
}

template <>
WMT::Reference<WMT::ComputePipelineState>
SimpleCommandContext<MTLD3D12CommandAllocatorImpl>::getComputePipeline(std::string name) {
Expand Down
33 changes: 33 additions & 0 deletions src/d3d12/d3d12_command_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,34 @@ ptr_add(const void *const p, const std::uintptr_t &amount) noexcept {
return reinterpret_cast<void *>(reinterpret_cast<std::uintptr_t>(p) + amount);
}

struct IndirectComputeCommandData {
uint64_t cmd_buf;
uint64_t max_count;
uint64_t max_count_buffer;
uint64_t argument_buffer;
uint64_t static_samplers;
uint64_t rootsig_qwords;
uint32_t rootsig_qwords_stride;
uint32_t tgsize_x;
uint32_t tgsize_y;
uint32_t tgsize_z;
};

struct IndirectRenderCommandData {
uint64_t cmd_buf;
uint64_t max_count;
uint64_t max_count_buffer;
uint64_t argument_buffer;
uint64_t static_samplers;
uint64_t rootsig_qwords;
uint32_t rootsig_qwords_stride;
uint32_t primitive_type;
uint64_t vertex_buffer;
uint64_t index_buffer;
DXGI_FORMAT index_buffer_format;
uint32_t vertex_argbuf_stride;
};

class MTLD3D12CommandAllocatorImpl : public MTLD3D12Pageable<MTLD3D12CommandAllocator> {
friend class MTLD3D12GraphicsCommandListImpl;
friend struct SimpleCommandContext<MTLD3D12CommandAllocatorImpl>;
Expand All @@ -58,6 +86,7 @@ class MTLD3D12CommandAllocatorImpl : public MTLD3D12Pageable<MTLD3D12CommandAllo

small_vector<EncoderData, 64> encoder_lists_;

small_vector<WMT::Reference<WMT::IndirectCommandBuffer>, 4> icb_;

ClearUAV<MTLD3D12CommandAllocatorImpl> clear_uav_;

Expand Down Expand Up @@ -186,6 +215,10 @@ class MTLD3D12CommandAllocatorImpl : public MTLD3D12Pageable<MTLD3D12CommandAllo
assert(gpu_heap_offset_ < kGPUHeapSize);
return {ptr_add(gpu_heap_, aligned), aligned};
}

IndirectComputeCommandData *EncodeIndirectComputeCommand(MTLD3D12CommandSignature *pCmdSig, MTLD3D12ComputePipelineState *pPSO, size_t MaxCount);

IndirectRenderCommandData *EncodeIndirectRenderCommand(MTLD3D12CommandSignature *pCmdSig, MTLD3D12GraphicsPipelineState *pPSO, size_t MaxCount);
};

} // namespace dxmt
Loading
Loading