Skip to content
Closed
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
4 changes: 3 additions & 1 deletion docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,9 @@ The compute path, end to end:
dynamic-rendering scope. Its layer count is the smallest layer count of the bound views.
- **Viewport:** it is flipped (negative height), because translated vertex shaders write D3D clip coordinates;
front-face winding then matches D3D12's.
- Only the first viewport and scissor are used, and primitive restart is not modelled yet.
- Up to 16 viewports and scissor rectangles are forwarded through `VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT` /
`VK_DYNAMIC_STATE_SCISSOR_WITH_COUNT` when `VkPhysicalDeviceFeatures::multiViewport` is enabled (clamped to the
first entry with a `log_once` notice otherwise); primitive restart is not modelled yet.

**Resources and heaps:**
- A buffer of 0 bytes, or one whose size wraps when aligned, is `E_INVALIDARG`. One the arena cannot hold is
Expand Down
61 changes: 43 additions & 18 deletions runtime/shims/d3d12_com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,26 +410,35 @@ class GraphicsCommandList final
void STDMETHODCALLTYPE RSSetViewports(UINT count, const D3D12_VIEWPORT* viewports) override {
if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
if (!count) {
viewport_ = {};
viewports_.fill({});
viewport_count_ = 0;
draw_views_stale_ = true;
return;
}
if (!viewports || count > D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE) return check(false);
if (count > 1) stub("ID3D12GraphicsCommandList::RSSetViewports (more than one viewport; the first is used)");
const D3D12_VIEWPORT& v = viewports[0];
viewport_ = {v.TopLeftX, v.TopLeftY, v.Width, v.Height, v.MinDepth, v.MaxDepth};
for (UINT i = 0; i < count; ++i)
viewports_[i] = {viewports[i].TopLeftX, viewports[i].TopLeftY, viewports[i].Width, viewports[i].Height,
viewports[i].MinDepth, viewports[i].MaxDepth};
viewport_count_ = count;
draw_views_stale_ = true;
}

void STDMETHODCALLTYPE RSSetScissorRects(UINT count, const D3D12_RECT* rects) override {
if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
// A zero-count call disables all previously bound rectangles; it is not a no-op.
// With no scissor bound, the rasterizer clips every fragment, as on native D3D12.
if (!count) {
scissor_ = {};
scissors_.fill({});
scissor_count_ = 0;
draw_views_stale_ = true;
return;
}
if (!rects) return;
if (count > 1) stub("ID3D12GraphicsCommandList::RSSetScissorRects (more than one rectangle; the first is used)");
scissor_ = {rects[0].left, rects[0].top, rects[0].right, rects[0].bottom};
if (count > D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE) return check(false);
for (UINT i = 0; i < count; ++i)
scissors_[i] = {rects[i].left, rects[i].top, rects[i].right, rects[i].bottom};
scissor_count_ = count;
draw_views_stale_ = true;
}

// D3D12 reads render-target and depth descriptors here, so their views are copied now.
Expand Down Expand Up @@ -486,9 +495,9 @@ class GraphicsCommandList final
d3d12_trace::record("ID3D12GraphicsCommandList6::DispatchMesh");
if (x > 65535 || y > 65535 || z > 65535 || std::uint64_t(x) * y * z > (1u << 22)) return check(false);
if (!graphics_pipeline_ || !graphics_.layout) return check(false);
if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; // as record_draw: nothing is rasterized
leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, {}, 0, viewport_,
scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0};
if (!has_nonempty_viewport()) return; // as record_draw: nothing is rasterized
leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, {}, 0, viewports_[0],
scissors_[0], blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0};
draw.mesh = true;
check(list_.draw(std::move(draw)));
}
Expand Down Expand Up @@ -1264,8 +1273,10 @@ class GraphicsCommandList final
std::uint32_t topology_ = 0; // D3D_PRIMITIVE_TOPOLOGY_UNDEFINED
std::array<leonida::VertexBufferView, D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT> vertex_buffers_{};
leonida::IndexBufferView index_{};
leonida::Viewport viewport_{};
leonida::Rect scissor_{};
std::array<leonida::Viewport, D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE> viewports_{};
UINT viewport_count_ = 0;
std::array<leonida::Rect, D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE> scissors_{};
UINT scissor_count_ = 0;
std::array<leonida::TargetView, D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT> render_targets_{};
UINT render_target_count_ = 0;
// One block shared by every draw that keeps the same targets and buffers, rebuilt when either changes, so a
Expand Down Expand Up @@ -1366,8 +1377,10 @@ class GraphicsCommandList final
topology_ = 0;
vertex_buffers_ = {};
index_ = {};
viewport_ = {};
scissor_ = {};
viewports_.fill({});
viewport_count_ = 0;
scissors_.fill({});
scissor_count_ = 0;
render_targets_.fill({});
render_target_count_ = 0;
draw_views_stale_ = true;
Expand Down Expand Up @@ -1395,13 +1408,25 @@ class GraphicsCommandList final
check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z}));
}

// The views block a draw records. Rebuilt only when the render targets or the bound vertex buffers change.
bool has_nonempty_viewport() const {
for (UINT i = 0; i < viewport_count_; ++i)
if (viewports_[i].width > 0 && viewports_[i].height > 0) return true;
return false;
}

// The views block a draw records. Rebuilt only when render targets, vertex buffers, or multi-viewport/scissor arrays change.
const std::shared_ptr<const leonida::DrawViews>& draw_views() {
if (draw_views_stale_ || !draw_views_) {
auto views = std::make_shared<leonida::DrawViews>();
views->render_targets = render_targets_;
views->vertex_buffers = vertex_buffers_;
views->render_target_count = render_target_count_;
if (viewport_count_ > 1 || scissor_count_ > 1) {
views->viewports = viewports_;
views->scissors = scissors_;
views->viewport_count = viewport_count_;
views->scissor_count = scissor_count_;
}
draw_views_ = std::move(views);
draw_views_stale_ = false;
}
Expand All @@ -1414,9 +1439,9 @@ class GraphicsCommandList final
note_direct_heap_indexing(graphics_.layout);
// No viewport (never set, or unbound with RSSetViewports(0)) or an empty one: D3D12 rasterizes nothing,
// and Vulkan forbids a 0x0 viewport, so the draw is not recorded.
if (!(viewport_.width > 0) || !(viewport_.height > 0)) return;
leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, index_, topology_, viewport_,
scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances,
if (!has_nonempty_viewport()) return;
leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, index_, topology_, viewports_[0],
scissors_[0], blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances,
first, base_vertex, first_instance};
check(list_.draw(std::move(draw)));
}
Expand Down
45 changes: 23 additions & 22 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3936,7 +3936,9 @@ class PipelineState final

// Kept for variants: a draw that leaves a slot unbound needs this pipeline with that format undefined.
graphics_.mesh = mesh;
graphics_.dynamic_viewport_count = vk.vkCmdSetViewportWithCount && vk.vkCmdSetScissorWithCount;
recipe_.mesh = mesh;
recipe_.dynamic_viewport_count = graphics_.dynamic_viewport_count;
recipe_.stages = std::move(stage_infos);
recipe_.attributes = std::move(attributes);
recipe_.bindings = std::move(bindings);
Expand Down Expand Up @@ -4085,6 +4087,7 @@ class PipelineState final
// What a graphics pipeline is made from, so variants can be built at draw time.
struct Recipe {
bool mesh = false; // no vertex input or input assembly state, and no dynamic topology or strides
bool dynamic_viewport_count = false;
std::vector<VkPipelineShaderStageCreateInfo> stages;
// One per entry of create_graphics's stage table (VS, GS, PS), static_assert there; what `stages` chain.
VkPipelineShaderStageRequiredSubgroupSizeCreateInfo required_sizes[3]{};
Expand All @@ -4103,15 +4106,19 @@ class PipelineState final

// The graphics pipeline with the formats of the `unbound` slots (leonida::GraphicsPipeline::variant) undefined.
VkPipeline build(std::uint32_t unbound) const {
// The stride state is last: a pipeline without vertex bindings (a full-screen pass that builds its vertices
// from SV_VertexID) leaves it out, since no vkCmdBindVertexBuffers2 would ever set it
// (VUID-vkCmdDraw-pStrides-04913).
static constexpr VkDynamicState dynamic_states[] = {
VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR,
VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY, VK_DYNAMIC_STATE_PRIMITIVE_RESTART_ENABLE,
VK_DYNAMIC_STATE_STENCIL_REFERENCE, VK_DYNAMIC_STATE_BLEND_CONSTANTS,
VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE};
const Recipe& r = recipe_;
VkDynamicState dynamic_states[8]{};
std::uint32_t dynamic_count = 0;
const auto push_dyn = [&](VkDynamicState s) { dynamic_states[dynamic_count++] = s; };
push_dyn(r.dynamic_viewport_count ? VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT : VK_DYNAMIC_STATE_VIEWPORT);
push_dyn(r.dynamic_viewport_count ? VK_DYNAMIC_STATE_SCISSOR_WITH_COUNT : VK_DYNAMIC_STATE_SCISSOR);
if (!r.mesh) {
push_dyn(VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY);
push_dyn(VK_DYNAMIC_STATE_PRIMITIVE_RESTART_ENABLE);
}
push_dyn(VK_DYNAMIC_STATE_STENCIL_REFERENCE);
push_dyn(VK_DYNAMIC_STATE_BLEND_CONSTANTS);
if (!r.mesh && !r.bindings.empty()) push_dyn(VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE);
std::vector<VkFormat> colors = graphics_.color_formats;
for (std::size_t slot = 0; slot < colors.size(); ++slot)
if (unbound & (1u << slot)) colors[slot] = VK_FORMAT_UNDEFINED;
Expand All @@ -4137,8 +4144,8 @@ class PipelineState final
vertex_input.vertexAttributeDescriptionCount = std::uint32_t(r.attributes.size());
vertex_input.pVertexAttributeDescriptions = r.attributes.data();
VkPipelineViewportStateCreateInfo viewport{VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO};
viewport.viewportCount = 1;
viewport.scissorCount = 1;
viewport.viewportCount = r.dynamic_viewport_count ? 0u : 1u;
viewport.scissorCount = r.dynamic_viewport_count ? 0u : 1u;
VkPipelineMultisampleStateCreateInfo multisample = r.multisample;
VkPipelineRasterizationStateCreateInfo raster = r.raster;
VkPipelineRasterizationConservativeStateCreateInfoEXT conservative{
Expand All @@ -4153,19 +4160,9 @@ class PipelineState final
VkPipelineColorBlendStateCreateInfo blend = r.blend;
blend.attachmentCount = std::uint32_t(r.blends.size());
blend.pAttachments = r.blends.data();
// A mesh pipeline takes neither the topology nor the strides as dynamic state
// (VUID-VkGraphicsPipelineCreateInfo-pDynamicStates-07065 and -07066).
static constexpr VkDynamicState mesh_dynamic_states[] = {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR,
VK_DYNAMIC_STATE_STENCIL_REFERENCE,
VK_DYNAMIC_STATE_BLEND_CONSTANTS};
VkPipelineDynamicStateCreateInfo dynamic{VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO};
if (r.mesh) {
dynamic.dynamicStateCount = std::uint32_t(std::size(mesh_dynamic_states));
dynamic.pDynamicStates = mesh_dynamic_states;
} else {
dynamic.dynamicStateCount = std::uint32_t(std::size(dynamic_states)) - (r.bindings.empty() ? 1 : 0);
dynamic.pDynamicStates = dynamic_states;
}
dynamic.dynamicStateCount = dynamic_count;
dynamic.pDynamicStates = dynamic_states;

VkGraphicsPipelineCreateInfo info{VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, &rendering};
info.stageCount = std::uint32_t(r.stages.size());
Expand Down Expand Up @@ -5461,6 +5458,9 @@ class Device final
arena.log = getenv_s(&length, log, "LEONIDA_ARENA_LOG") == 0 && length > 1 && log[0] != '0';
state_.context->set_heap_base(arena.address);
state_.context->set_zero_page(arena.buffer, 0, kArenaReserved);
state_.context->set_multi_viewport(state_.info.features.multiViewport == VK_TRUE);
if (state_.info.features.multiViewport != VK_TRUE)
log_once("multiViewport disabled; clamping viewports/scissors to", 1);
return true;
}

Expand Down Expand Up @@ -9807,6 +9807,7 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin
enable.dualSrcBlend = chosen_features.dualSrcBlend;
enable.logicOp = chosen_features.logicOp;
enable.depthClamp = chosen_features.depthClamp;
enable.multiViewport = chosen_features.multiViewport;
enable.geometryShader = chosen_features.geometryShader; // translated geometry shaders (195 in the corpus)
// What translated shaders declare (the index's `features`): 64-bit integers in most of the corpus (25,803 of
// 26,503 entries), doubles, 16-bit integers, clip and cull distances in a few vertex shaders, image stores
Expand Down
4 changes: 4 additions & 0 deletions runtime/src/command_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,10 @@ struct DrawViews {
std::array<TargetView, 8> render_targets{};
std::array<VertexBufferView, 32> vertex_buffers{}; // by input slot
std::uint32_t render_target_count = 8;
std::array<Viewport, 16> viewports{};
std::array<Rect, 16> scissors{};
std::uint32_t viewport_count = 0; // 0: single Draw::viewport; >0: all viewports_[0..count-1]
std::uint32_t scissor_count = 0; // 0: single Draw::scissor; >0: all scissors_[0..count-1]
};

// DrawInstanced / DrawIndexedInstanced / DispatchMesh, with everything they use captured when they are recorded:
Expand Down
8 changes: 8 additions & 0 deletions runtime/src/vulkan_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ struct VulkanDeviceFunctions {
X(vkCreateComputePipelines) X(vkDestroyPipeline) X(vkCmdBindPipeline) X(vkCmdBindDescriptorSets) \
X(vkCmdPushConstants) X(vkCmdDispatch) \
X(vkCmdBindVertexBuffers2) X(vkCmdBindIndexBuffer) X(vkCmdSetViewport) X(vkCmdSetScissor) \
X(vkCmdSetViewportWithCount) X(vkCmdSetScissorWithCount) \
X(vkCmdSetPrimitiveTopology) X(vkCmdSetPrimitiveRestartEnable) X(vkCmdSetStencilReference) \
X(vkCmdSetBlendConstants) X(vkCmdDraw) \
X(vkCmdDrawIndexed) X(vkCreateGraphicsPipelines) X(vkQueueBindSparse) X(vkGetImageSparseMemoryRequirements) \
Expand Down Expand Up @@ -987,6 +988,8 @@ class VulkanContext {
replay_.bind_index_buffer = vk.vkCmdBindIndexBuffer;
replay_.set_viewport = vk.vkCmdSetViewport;
replay_.set_scissor = vk.vkCmdSetScissor;
replay_.set_viewport_with_count = vk.vkCmdSetViewportWithCount;
replay_.set_scissor_with_count = vk.vkCmdSetScissorWithCount;
replay_.set_primitive_topology = vk.vkCmdSetPrimitiveTopology;
replay_.set_primitive_restart_enable = vk.vkCmdSetPrimitiveRestartEnable;
replay_.set_stencil_reference = vk.vkCmdSetStencilReference;
Expand Down Expand Up @@ -1277,6 +1280,11 @@ class VulkanContext {
replay_.heap_base = heap_base;
}

void set_multi_viewport(bool enabled) {
std::unique_lock lock(resource_mutex_);
replay_.multi_viewport = enabled;
}

// Zeroed memory for unbound vertex and index buffers to read (ReplayFunctions::zero_buffer).
void set_zero_page(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize size) {
std::unique_lock lock(resource_mutex_);
Expand Down
Loading
Loading