diff options
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
| -rw-r--r-- | runtime/shims/d3d12_com.cpp | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index d9b1a009c..6858f073c 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp | |||
| @@ -368,6 +368,7 @@ public: | |||
| 368 | if (count > D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT) return check(false); | 368 | if (count > D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT) return check(false); |
| 369 | render_targets_.fill({}); | 369 | render_targets_.fill({}); |
| 370 | render_target_count_ = count; | 370 | render_target_count_ = count; |
| 371 | draw_views_stale_ = true; | ||
| 371 | for (UINT i = 0; i < count && targets; ++i) { | 372 | for (UINT i = 0; i < count && targets; ++i) { |
| 372 | const SIZE_T handle = single_range ? targets[0].ptr + SIZE_T(i) * descriptor_size : targets[i].ptr; | 373 | const SIZE_T handle = single_range ? targets[0].ptr + SIZE_T(i) * descriptor_size : targets[i].ptr; |
| 373 | if (handle) render_targets_[i] = *reinterpret_cast<const leonida::TargetView*>(handle); | 374 | if (handle) render_targets_[i] = *reinterpret_cast<const leonida::TargetView*>(handle); |
| @@ -989,6 +990,10 @@ private: | |||
| 989 | leonida::Rect scissor_{}; | 990 | leonida::Rect scissor_{}; |
| 990 | std::array<leonida::TargetView, D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT> render_targets_{}; | 991 | std::array<leonida::TargetView, D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT> render_targets_{}; |
| 991 | UINT render_target_count_ = 0; | 992 | UINT render_target_count_ = 0; |
| 993 | // One block shared by every draw that keeps the same targets and buffers, rebuilt when either changes, so a | ||
| 994 | // run of draws does not allocate per draw. | ||
| 995 | std::shared_ptr<const leonida::DrawViews> draw_views_; | ||
| 996 | bool draw_views_stale_ = true; | ||
| 992 | leonida::TargetView depth_{}; | 997 | leonida::TargetView depth_{}; |
| 993 | std::array<float, 4> blend_factor_{1, 1, 1, 1}; | 998 | std::array<float, 4> blend_factor_{1, 1, 1, 1}; |
| 994 | 999 | ||
| @@ -1053,12 +1058,14 @@ private: | |||
| 1053 | scissor_ = {}; | 1058 | scissor_ = {}; |
| 1054 | render_targets_.fill({}); | 1059 | render_targets_.fill({}); |
| 1055 | render_target_count_ = 0; | 1060 | render_target_count_ = 0; |
| 1061 | draw_views_stale_ = true; | ||
| 1056 | depth_ = {}; | 1062 | depth_ = {}; |
| 1057 | blend_factor_ = {1, 1, 1, 1}; | 1063 | blend_factor_ = {1, 1, 1, 1}; |
| 1058 | set_pipeline(state); | 1064 | set_pipeline(state); |
| 1059 | } | 1065 | } |
| 1060 | 1066 | ||
| 1061 | void set_vertex_buffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) { | 1067 | void set_vertex_buffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) { |
| 1068 | if (count) draw_views_stale_ = true; | ||
| 1062 | for (UINT i = 0; i < count; ++i) | 1069 | for (UINT i = 0; i < count; ++i) |
| 1063 | vertex_buffers_[start + i] = views ? leonida::VertexBufferView{views[i].BufferLocation, views[i].SizeInBytes, | 1070 | vertex_buffers_[start + i] = views ? leonida::VertexBufferView{views[i].BufferLocation, views[i].SizeInBytes, |
| 1064 | views[i].StrideInBytes} | 1071 | views[i].StrideInBytes} |
| @@ -1075,16 +1082,28 @@ private: | |||
| 1075 | check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z})); | 1082 | check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z})); |
| 1076 | } | 1083 | } |
| 1077 | 1084 | ||
| 1085 | // The views block a draw records. Rebuilt only when the render targets or the bound vertex buffers change. | ||
| 1086 | const std::shared_ptr<const leonida::DrawViews>& draw_views() { | ||
| 1087 | if (draw_views_stale_ || !draw_views_) { | ||
| 1088 | auto views = std::make_shared<leonida::DrawViews>(); | ||
| 1089 | views->render_targets = render_targets_; | ||
| 1090 | views->vertex_buffers = vertex_buffers_; | ||
| 1091 | views->render_target_count = render_target_count_; | ||
| 1092 | draw_views_ = std::move(views); | ||
| 1093 | draw_views_stale_ = false; | ||
| 1094 | } | ||
| 1095 | return draw_views_; | ||
| 1096 | } | ||
| 1097 | |||
| 1078 | // A draw needs a graphics pipeline, a graphics root signature and a topology. | 1098 | // A draw needs a graphics pipeline, a graphics root signature and a topology. |
| 1079 | void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { | 1099 | void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { |
| 1080 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); | 1100 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); |
| 1081 | // No viewport (never set, or unbound with RSSetViewports(0)) or an empty one: D3D12 rasterizes nothing, | 1101 | // No viewport (never set, or unbound with RSSetViewports(0)) or an empty one: D3D12 rasterizes nothing, |
| 1082 | // and Vulkan forbids a 0x0 viewport, so the draw is not recorded. | 1102 | // and Vulkan forbids a 0x0 viewport, so the draw is not recorded. |
| 1083 | if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; | 1103 | if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; |
| 1084 | leonida::Draw draw{graphics_pipeline_, graphics_.data, render_targets_, depth_, | 1104 | leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, index_, topology_, viewport_, |
| 1085 | vertex_buffers_, index_, topology_, viewport_, scissor_, | 1105 | scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, |
| 1086 | blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, first, | 1106 | first, base_vertex, first_instance}; |
| 1087 | base_vertex, first_instance, render_target_count_}; | ||
| 1088 | check(list_.draw(std::move(draw))); | 1107 | check(list_.draw(std::move(draw))); |
| 1089 | } | 1108 | } |
| 1090 | }; | 1109 | }; |
| @@ -1542,8 +1561,11 @@ HRESULT create_command_queue(leonida::VulkanContext& context, VkQueue queue, std | |||
| 1542 | } | 1561 | } |
| 1543 | 1562 | ||
| 1544 | HRESULT create_command_allocator(D3D12_COMMAND_LIST_TYPE type, REFIID riid, void** out, IUnknown* device) { | 1563 | HRESULT create_command_allocator(D3D12_COMMAND_LIST_TYPE type, REFIID riid, void** out, IUnknown* device) { |
| 1545 | if (!out) return E_POINTER; | 1564 | // The host runtime's order (d3d12_vulkan_device_test, compare_invalid_arguments "command allocator:"): a type |
| 1546 | *out = nullptr; | 1565 | // past VIDEO_ENCODE is E_INVALIDARG, then a null output is E_UNEXPECTED. |
| 1566 | if (out) *out = nullptr; | ||
| 1567 | if (UINT(type) > UINT(D3D12_COMMAND_LIST_TYPE_VIDEO_ENCODE)) return E_INVALIDARG; | ||
| 1568 | if (!out) return E_UNEXPECTED; | ||
| 1547 | auto* object = new CommandAllocator(type); | 1569 | auto* object = new CommandAllocator(type); |
| 1548 | object->attach_device(device); | 1570 | object->attach_device(device); |
| 1549 | return hand_out(object, riid, out); | 1571 | return hand_out(object, riid, out); |
| @@ -1551,12 +1573,13 @@ HRESULT create_command_allocator(D3D12_COMMAND_LIST_TYPE type, REFIID riid, void | |||
| 1551 | 1573 | ||
| 1552 | HRESULT create_command_list(D3D12_COMMAND_LIST_TYPE type, ID3D12CommandAllocator* allocator, REFIID riid, | 1574 | HRESULT create_command_list(D3D12_COMMAND_LIST_TYPE type, ID3D12CommandAllocator* allocator, REFIID riid, |
| 1553 | void** out, IUnknown* device, ID3D12PipelineState* initial_state) { | 1575 | void** out, IUnknown* device, ID3D12PipelineState* initial_state) { |
| 1554 | if (!out) return E_POINTER; | 1576 | if (out) *out = nullptr; |
| 1555 | *out = nullptr; | ||
| 1556 | CommandAllocator* command_allocator = CommandAllocator::from(allocator); | 1577 | CommandAllocator* command_allocator = CommandAllocator::from(allocator); |
| 1557 | if (!command_allocator || command_allocator->type() != type) return E_INVALIDARG; | 1578 | if (!command_allocator || command_allocator->type() != type) return E_INVALIDARG; |
| 1558 | // A list is created open, so the allocator must be free: E_INVALIDARG as on the PC runtime (D3D-51). | 1579 | // A list is created open, so the allocator must be free: E_INVALIDARG as on the PC runtime (D3D-51). |
| 1559 | if (command_allocator->recording()) return E_INVALIDARG; | 1580 | if (command_allocator->recording()) return E_INVALIDARG; |
| 1581 | // A null output only after the arguments, as the host runtime answers ("command list:" cases). | ||
| 1582 | if (!out) return E_UNEXPECTED; | ||
| 1560 | if (type != D3D12_COMMAND_LIST_TYPE_DIRECT && type != D3D12_COMMAND_LIST_TYPE_BUNDLE && | 1583 | if (type != D3D12_COMMAND_LIST_TYPE_DIRECT && type != D3D12_COMMAND_LIST_TYPE_BUNDLE && |
| 1561 | type != D3D12_COMMAND_LIST_TYPE_COMPUTE && type != D3D12_COMMAND_LIST_TYPE_COPY) | 1584 | type != D3D12_COMMAND_LIST_TYPE_COMPUTE && type != D3D12_COMMAND_LIST_TYPE_COPY) |
| 1562 | stub("create_command_list(non-direct type)"); | 1585 | stub("create_command_list(non-direct type)"); |