diff options
| author | aslan2142 <jan.trencansky@protonmail.com> | 2026-11-24 21:31:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-24 21:31:21 +0100 |
| commit | ccd9c5f369bee45a810bcb0a4405f8c8a670c896 (patch) | |
| tree | 28a66dd04d5e69e5f95cec273dbf6a11e93c3abf /runtime/src | |
| parent | e0f171958f5a2e57741142545a98c3c0c645e530 (diff) | |
| parent | 7d50f3b749ab8c2902bef94ef8cc76885c22712a (diff) | |
Merge pull request #510 from jujumoneylol/pr/d3d12-multi-viewport
d3d12/vulkan: implement multi-viewport and multi-scissor rendering (D3D-MULTI-VIEWPORT)
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/command_list.h | 10 | ||||
| -rw-r--r-- | runtime/src/vulkan_backend.h | 3 | ||||
| -rw-r--r-- | runtime/src/vulkan_replay.h | 67 |
3 files changed, 69 insertions, 11 deletions
diff --git a/runtime/src/command_list.h b/runtime/src/command_list.h index 7c5c09e0a..a7578b8ff 100644 --- a/runtime/src/command_list.h +++ b/runtime/src/command_list.h | |||
| @@ -414,6 +414,15 @@ struct DrawViews { | |||
| 414 | std::uint32_t render_target_count = 8; | 414 | std::uint32_t render_target_count = 8; |
| 415 | }; | 415 | }; |
| 416 | 416 | ||
| 417 | // Shared across draws that keep the same multi-viewport / multi-scissor state (null for the common 1-viewport case). | ||
| 418 | struct DrawViewports { | ||
| 419 | static constexpr std::uint32_t max_count = 16; | ||
| 420 | std::array<Viewport, max_count> viewports{}; | ||
| 421 | std::uint32_t viewport_count = 0; | ||
| 422 | std::array<Rect, max_count> scissors{}; | ||
| 423 | std::uint32_t scissor_count = 0; | ||
| 424 | }; | ||
| 425 | |||
| 417 | // DrawInstanced / DrawIndexedInstanced / DispatchMesh, with everything they use captured when they are recorded: | 426 | // DrawInstanced / DrawIndexedInstanced / DispatchMesh, with everything they use captured when they are recorded: |
| 418 | // D3D12 reads render-target and depth descriptors at OMSetRenderTargets, and every other piece of state is the | 427 | // D3D12 reads render-target and depth descriptors at OMSetRenderTargets, and every other piece of state is the |
| 419 | // list's state at the draw. resource 0 marks an unbound render-target slot or no depth buffer. A mesh draw | 428 | // list's state at the draw. resource 0 marks an unbound render-target slot or no depth buffer. A mesh draw |
| @@ -440,6 +449,7 @@ struct Draw { | |||
| 440 | std::int32_t base_vertex = 0; | 449 | std::int32_t base_vertex = 0; |
| 441 | std::uint32_t first_instance = 0; | 450 | std::uint32_t first_instance = 0; |
| 442 | bool mesh = false; | 451 | bool mesh = false; |
| 452 | std::shared_ptr<const DrawViewports> viewports; // non-null when >1 viewport or >1 scissor was bound | ||
| 443 | }; | 453 | }; |
| 444 | 454 | ||
| 445 | // D3D12_TEXTURE_DATA_PITCH_ALIGNMENT and D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT. | 455 | // D3D12_TEXTURE_DATA_PITCH_ALIGNMENT and D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT. |
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index f4373787b..ab6786a51 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -101,6 +101,7 @@ struct VulkanDeviceFunctions { | |||
| 101 | X(vkCreateComputePipelines) X(vkDestroyPipeline) X(vkCmdBindPipeline) X(vkCmdBindDescriptorSets) \ | 101 | X(vkCreateComputePipelines) X(vkDestroyPipeline) X(vkCmdBindPipeline) X(vkCmdBindDescriptorSets) \ |
| 102 | X(vkCmdPushConstants) X(vkCmdDispatch) \ | 102 | X(vkCmdPushConstants) X(vkCmdDispatch) \ |
| 103 | X(vkCmdBindVertexBuffers2) X(vkCmdBindIndexBuffer) X(vkCmdSetViewport) X(vkCmdSetScissor) \ | 103 | X(vkCmdBindVertexBuffers2) X(vkCmdBindIndexBuffer) X(vkCmdSetViewport) X(vkCmdSetScissor) \ |
| 104 | X(vkCmdSetViewportWithCount) X(vkCmdSetScissorWithCount) \ | ||
| 104 | X(vkCmdSetPrimitiveTopology) X(vkCmdSetPrimitiveRestartEnable) X(vkCmdSetStencilReference) \ | 105 | X(vkCmdSetPrimitiveTopology) X(vkCmdSetPrimitiveRestartEnable) X(vkCmdSetStencilReference) \ |
| 105 | X(vkCmdSetBlendConstants) X(vkCmdDraw) \ | 106 | X(vkCmdSetBlendConstants) X(vkCmdDraw) \ |
| 106 | X(vkCmdDrawIndexed) X(vkCreateGraphicsPipelines) X(vkQueueBindSparse) X(vkGetImageSparseMemoryRequirements) \ | 107 | X(vkCmdDrawIndexed) X(vkCreateGraphicsPipelines) X(vkQueueBindSparse) X(vkGetImageSparseMemoryRequirements) \ |
| @@ -987,6 +988,8 @@ public: | |||
| 987 | replay_.bind_index_buffer = vk.vkCmdBindIndexBuffer; | 988 | replay_.bind_index_buffer = vk.vkCmdBindIndexBuffer; |
| 988 | replay_.set_viewport = vk.vkCmdSetViewport; | 989 | replay_.set_viewport = vk.vkCmdSetViewport; |
| 989 | replay_.set_scissor = vk.vkCmdSetScissor; | 990 | replay_.set_scissor = vk.vkCmdSetScissor; |
| 991 | replay_.set_viewport_with_count = vk.vkCmdSetViewportWithCount; | ||
| 992 | replay_.set_scissor_with_count = vk.vkCmdSetScissorWithCount; | ||
| 990 | replay_.set_primitive_topology = vk.vkCmdSetPrimitiveTopology; | 993 | replay_.set_primitive_topology = vk.vkCmdSetPrimitiveTopology; |
| 991 | replay_.set_primitive_restart_enable = vk.vkCmdSetPrimitiveRestartEnable; | 994 | replay_.set_primitive_restart_enable = vk.vkCmdSetPrimitiveRestartEnable; |
| 992 | replay_.set_stencil_reference = vk.vkCmdSetStencilReference; | 995 | replay_.set_stencil_reference = vk.vkCmdSetStencilReference; |
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 5c8444a05..619d53827 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h | |||
| @@ -776,6 +776,7 @@ struct GraphicsPipeline { | |||
| 776 | std::uint32_t zero_step_slots = 0; // per-instance slots with InstanceDataStepRate 0: stride 0 at draw time | 776 | std::uint32_t zero_step_slots = 0; // per-instance slots with InstanceDataStepRate 0: stride 0 at draw time |
| 777 | std::uint32_t strip_cut = 0; // D3D12_INDEX_BUFFER_STRIP_CUT_VALUE (0: off, 1: 0xFFFF, 2: 0xFFFFFFFF) | 777 | std::uint32_t strip_cut = 0; // D3D12_INDEX_BUFFER_STRIP_CUT_VALUE (0: off, 1: 0xFFFF, 2: 0xFFFFFFFF) |
| 778 | bool mesh = false; // a mesh shader instead of a vertex shader: drawn by DispatchMesh only | 778 | bool mesh = false; // a mesh shader instead of a vertex shader: drawn by DispatchMesh only |
| 779 | bool viewport_with_count = false; // pipeline uses VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT / SCISSOR_WITH_COUNT | ||
| 779 | // The same pipeline with some attachment formats VK_FORMAT_UNDEFINED, for a draw that leaves those slots | 780 | // The same pipeline with some attachment formats VK_FORMAT_UNDEFINED, for a draw that leaves those slots |
| 780 | // unbound (D3D12 allows it; dynamic rendering needs the formats to agree). Bit i: colour slot i, bit 8: depth | 781 | // unbound (D3D12 allows it; dynamic rendering needs the formats to agree). Bit i: colour slot i, bit 8: depth |
| 781 | // and stencil. Null when it cannot be built. | 782 | // and stencil. Null when it cannot be built. |
| @@ -858,6 +859,8 @@ struct ReplayFunctions { | |||
| 858 | PFN_vkCmdBindIndexBuffer bind_index_buffer = nullptr; | 859 | PFN_vkCmdBindIndexBuffer bind_index_buffer = nullptr; |
| 859 | PFN_vkCmdSetViewport set_viewport = nullptr; | 860 | PFN_vkCmdSetViewport set_viewport = nullptr; |
| 860 | PFN_vkCmdSetScissor set_scissor = nullptr; | 861 | PFN_vkCmdSetScissor set_scissor = nullptr; |
| 862 | PFN_vkCmdSetViewportWithCount set_viewport_with_count = nullptr; | ||
| 863 | PFN_vkCmdSetScissorWithCount set_scissor_with_count = nullptr; | ||
| 861 | PFN_vkCmdSetPrimitiveTopology set_primitive_topology = nullptr; | 864 | PFN_vkCmdSetPrimitiveTopology set_primitive_topology = nullptr; |
| 862 | PFN_vkCmdSetPrimitiveRestartEnable set_primitive_restart_enable = nullptr; | 865 | PFN_vkCmdSetPrimitiveRestartEnable set_primitive_restart_enable = nullptr; |
| 863 | PFN_vkCmdSetStencilReference set_stencil_reference = nullptr; | 866 | PFN_vkCmdSetStencilReference set_stencil_reference = nullptr; |
| @@ -2138,6 +2141,8 @@ ReplayResult prepare_draw(const Draw& draw, Resolve&& resolve, const ReplayFunct | |||
| 2138 | if (!pipeline || !pipeline->pipeline) return ReplayResult::unresolved_resource; | 2141 | if (!pipeline || !pipeline->pipeline) return ReplayResult::unresolved_resource; |
| 2139 | if (pipeline->mesh != draw.mesh) return ReplayResult::invalid_draw; | 2142 | if (pipeline->mesh != draw.mesh) return ReplayResult::invalid_draw; |
| 2140 | if (draw.mesh && !vk.draw_mesh_tasks) return ReplayResult::unsupported; | 2143 | if (draw.mesh && !vk.draw_mesh_tasks) return ReplayResult::unsupported; |
| 2144 | if (pipeline->viewport_with_count && (!vk.set_viewport_with_count || !vk.set_scissor_with_count)) | ||
| 2145 | return ReplayResult::unsupported; | ||
| 2141 | if (pipeline->bindless_set && !vk.bind_descriptor_sets) return ReplayResult::unsupported; | 2146 | if (pipeline->bindless_set && !vk.bind_descriptor_sets) return ReplayResult::unsupported; |
| 2142 | if (pipeline->color_formats.size() > max_render_targets) return ReplayResult::unsupported; | 2147 | if (pipeline->color_formats.size() > max_render_targets) return ReplayResult::unsupported; |
| 2143 | p.graphics = pipeline; | 2148 | p.graphics = pipeline; |
| @@ -2186,9 +2191,17 @@ ReplayResult prepare_draw(const Draw& draw, Resolve&& resolve, const ReplayFunct | |||
| 2186 | if (!a.view) return ReplayResult::unsupported; | 2191 | if (!a.view) return ReplayResult::unsupported; |
| 2187 | shrink(a.binding, draw.depth, a.range); | 2192 | shrink(a.binding, draw.depth, a.range); |
| 2188 | } | 2193 | } |
| 2189 | if (p.area.width == UINT32_MAX) { // no attachment: the viewport bounds the rendering | 2194 | if (p.area.width == UINT32_MAX) { // no attachment: the viewports bound the rendering |
| 2190 | p.area = {std::uint32_t(std::max(0.0f, draw.viewport.x + draw.viewport.width)), | 2195 | std::uint32_t max_w = std::uint32_t(std::max(0.0f, draw.viewport.x + draw.viewport.width)); |
| 2191 | std::uint32_t(std::max(0.0f, draw.viewport.y + draw.viewport.height))}; | 2196 | std::uint32_t max_h = std::uint32_t(std::max(0.0f, draw.viewport.y + draw.viewport.height)); |
| 2197 | if (draw.viewports) { | ||
| 2198 | for (std::uint32_t i = 0; i < draw.viewports->viewport_count; ++i) { | ||
| 2199 | const Viewport& v = draw.viewports->viewports[i]; | ||
| 2200 | max_w = std::max(max_w, std::uint32_t(std::max(0.0f, v.x + v.width))); | ||
| 2201 | max_h = std::max(max_h, std::uint32_t(std::max(0.0f, v.y + v.height))); | ||
| 2202 | } | ||
| 2203 | } | ||
| 2204 | p.area = {max_w, max_h}; | ||
| 2192 | } | 2205 | } |
| 2193 | p.layers = layers == UINT32_MAX ? 1 : std::max(1u, layers); | 2206 | p.layers = layers == UINT32_MAX ? 1 : std::max(1u, layers); |
| 2194 | 2207 | ||
| @@ -4253,14 +4266,46 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 4253 | vk.push_constants(command_buffer, graphics.layout, graphics.push_stages, 0, sizeof(data), &data); | 4266 | vk.push_constants(command_buffer, graphics.layout, graphics.push_stages, 0, sizeof(data), &data); |
| 4254 | // Translated vertex shaders write D3D clip coordinates (y up); a negative-height viewport keeps the | 4267 | // Translated vertex shaders write D3D clip coordinates (y up); a negative-height viewport keeps the |
| 4255 | // image upright, and winding as D3D12 sees it. | 4268 | // image upright, and winding as D3D12 sees it. |
| 4256 | const Viewport& v = draw->viewport; | 4269 | if (graphics.viewport_with_count) { |
| 4257 | const VkViewport viewport{v.x, v.y + v.height, v.width, -v.height, v.min_depth, v.max_depth}; | 4270 | std::array<VkViewport, DrawViewports::max_count> vps{}; |
| 4258 | vk.set_viewport(command_buffer, 0, 1, &viewport); | 4271 | std::array<VkRect2D, DrawViewports::max_count> scs{}; |
| 4259 | const Rect& s = draw->scissor; | 4272 | const std::uint32_t vp_n = draw->viewports ? draw->viewports->viewport_count : 1u; |
| 4260 | const VkRect2D scissor{{std::max(0, s.left), std::max(0, s.top)}, | 4273 | const std::uint32_t sc_n = draw->viewports ? draw->viewports->scissor_count : 1u; |
| 4261 | {std::uint32_t(std::max(0, s.right - std::max(0, s.left))), | 4274 | const std::uint32_t count = std::clamp(std::max(vp_n, sc_n), 1u, DrawViewports::max_count); |
| 4262 | std::uint32_t(std::max(0, s.bottom - std::max(0, s.top)))}}; | 4275 | for (std::uint32_t k = 0; k < count; ++k) { |
| 4263 | vk.set_scissor(command_buffer, 0, 1, &scissor); | 4276 | const Viewport v = (draw->viewports && k < vp_n) ? draw->viewports->viewports[k] |
| 4277 | : (k == 0 ? draw->viewport : Viewport{}); | ||
| 4278 | const bool valid_vp = (k < vp_n) && (v.width > 0.0f) && (v.height > 0.0f); | ||
| 4279 | if (valid_vp) { | ||
| 4280 | vps[k] = {v.x, v.y + v.height, v.width, -v.height, v.min_depth, v.max_depth}; | ||
| 4281 | } else { | ||
| 4282 | // Vulkan forbids width/height == 0; pair a 1x1 dummy viewport with an empty scissor so nothing | ||
| 4283 | // rasterizes into an unbound/empty D3D12 viewport slot. | ||
| 4284 | vps[k] = {0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f}; | ||
| 4285 | } | ||
| 4286 | if (valid_vp && k < sc_n) { | ||
| 4287 | const Rect s = draw->viewports ? draw->viewports->scissors[k] : draw->scissor; | ||
| 4288 | const std::int32_t left = std::max(0, s.left); | ||
| 4289 | const std::int32_t top = std::max(0, s.top); | ||
| 4290 | scs[k] = {{left, top}, | ||
| 4291 | {std::uint32_t(std::max(0, s.right - left)), | ||
| 4292 | std::uint32_t(std::max(0, s.bottom - top))}}; | ||
| 4293 | } else { | ||
| 4294 | scs[k] = {{0, 0}, {0, 0}}; | ||
| 4295 | } | ||
| 4296 | } | ||
| 4297 | vk.set_viewport_with_count(command_buffer, count, vps.data()); | ||
| 4298 | vk.set_scissor_with_count(command_buffer, count, scs.data()); | ||
| 4299 | } else { | ||
| 4300 | const Viewport& v = draw->viewport; | ||
| 4301 | const VkViewport viewport{v.x, v.y + v.height, v.width, -v.height, v.min_depth, v.max_depth}; | ||
| 4302 | vk.set_viewport(command_buffer, 0, 1, &viewport); | ||
| 4303 | const Rect& s = draw->scissor; | ||
| 4304 | const VkRect2D scissor{{std::max(0, s.left), std::max(0, s.top)}, | ||
| 4305 | {std::uint32_t(std::max(0, s.right - std::max(0, s.left))), | ||
| 4306 | std::uint32_t(std::max(0, s.bottom - std::max(0, s.top)))}}; | ||
| 4307 | vk.set_scissor(command_buffer, 0, 1, &scissor); | ||
| 4308 | } | ||
| 4264 | // A mesh pipeline has no input assembly: its topology comes from the shader, and neither the topology | 4309 | // A mesh pipeline has no input assembly: its topology comes from the shader, and neither the topology |
| 4265 | // nor primitive restart is dynamic state there (VUID-VkGraphicsPipelineCreateInfo-pDynamicStates-07065). | 4310 | // nor primitive restart is dynamic state there (VUID-VkGraphicsPipelineCreateInfo-pDynamicStates-07065). |
| 4266 | if (!draw->mesh) { | 4311 | if (!draw->mesh) { |