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
9 changes: 9 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2294,6 +2294,15 @@ if(TARGET Vulkan::Headers)
target_compile_definitions(d3d12_resource_desc_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(d3d12_resource_desc_test PRIVATE /W4 /permissive- /utf-8)
add_test(NAME d3d12_resource_desc COMMAND d3d12_resource_desc_test)

# Multi-viewport & multi-scissor rendering (RSSetViewports / RSSetScissorRects 1..16) against host D3D12.
add_executable(d3d12_multi_viewport_test test/d3d12_multi_viewport_test.cpp)
target_link_libraries(d3d12_multi_viewport_test PRIVATE gta6rt_d3d12_com d3d12 dxgi dxguid)
target_compile_definitions(d3d12_multi_viewport_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(d3d12_multi_viewport_test PRIVATE /W4 /permissive- /utf-8)
add_test(NAME d3d12_multi_viewport COMMAND d3d12_multi_viewport_test)
set_tests_properties(d3d12_multi_viewport PROPERTIES SKIP_RETURN_CODE 77
FAIL_REGULAR_EXPRESSION "Validation Error;VUID-")
set_tests_properties(d3d12_resource_desc PROPERTIES SKIP_RETURN_CODE 77)

# Typeless depth-stencil textures without ALLOW_DEPTH_STENCIL, copied from a depth buffer, against WARP.
Expand Down
69 changes: 60 additions & 9 deletions runtime/shims/d3d12_com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,20 @@ class GraphicsCommandList final
if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
if (!count) {
viewport_ = {};
viewports_.fill({});
viewport_count_ = 0;
draw_viewports_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};
viewports_.fill({});
for (UINT i = 0; i < count; ++i) {
const D3D12_VIEWPORT& v = viewports[i];
viewports_[i] = {v.TopLeftX, v.TopLeftY, v.Width, v.Height, v.MinDepth, v.MaxDepth};
}
viewport_count_ = count;
viewport_ = viewports_[0];
draw_viewports_stale_ = true;
}

void STDMETHODCALLTYPE RSSetScissorRects(UINT count, const D3D12_RECT* rects) override {
Expand All @@ -425,11 +433,20 @@ class GraphicsCommandList final
// With no scissor bound, the rasterizer clips every fragment, as on native D3D12.
if (!count) {
scissor_ = {};
scissors_.fill({});
scissor_count_ = 0;
draw_viewports_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);
scissors_.fill({});
for (UINT i = 0; i < count; ++i) {
scissors_[i] = {rects[i].left, rects[i].top, rects[i].right, rects[i].bottom};
}
scissor_count_ = count;
scissor_ = scissors_[0];
draw_viewports_stale_ = true;
}

// D3D12 reads render-target and depth descriptors here, so their views are copied now.
Expand Down Expand Up @@ -486,10 +503,11 @@ 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
if (!any_nonempty_viewport()) 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};
draw.mesh = true;
draw.viewports = draw_viewports();
check(list_.draw(std::move(draw)));
}

Expand Down Expand Up @@ -1266,6 +1284,12 @@ class GraphicsCommandList final
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::shared_ptr<const leonida::DrawViewports> draw_viewports_;
bool draw_viewports_stale_ = true;
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 @@ -1368,6 +1392,12 @@ class GraphicsCommandList final
index_ = {};
viewport_ = {};
scissor_ = {};
viewports_.fill({});
viewport_count_ = 0;
scissors_.fill({});
scissor_count_ = 0;
draw_viewports_.reset();
draw_viewports_stale_ = true;
render_targets_.fill({});
render_target_count_ = 0;
draw_views_stale_ = true;
Expand Down Expand Up @@ -1408,16 +1438,37 @@ class GraphicsCommandList final
return draw_views_;
}

bool any_nonempty_viewport() const {
for (UINT i = 0; i < viewport_count_; ++i)
if (viewports_[i].width > 0.0f && viewports_[i].height > 0.0f) return true;
return false;
}

std::shared_ptr<const leonida::DrawViewports> draw_viewports() {
if (viewport_count_ <= 1 && scissor_count_ <= 1) return nullptr;
if (draw_viewports_stale_) {
auto block = std::make_shared<leonida::DrawViewports>();
block->viewports = viewports_;
block->viewport_count = viewport_count_;
block->scissors = scissors_;
block->scissor_count = scissor_count_;
draw_viewports_ = std::move(block);
draw_viewports_stale_ = false;
}
return draw_viewports_;
}

// A draw needs a graphics pipeline, a graphics root signature and a topology.
void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) {
if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false);
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;
// No viewport (never set, or unbound with RSSetViewports(0)) or every bound viewport empty: D3D12 rasterizes
// nothing, and Vulkan forbids a 0x0 viewport, so the draw is not recorded.
if (!any_nonempty_viewport()) 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,
first, base_vertex, first_instance};
draw.viewports = draw_viewports();
check(list_.draw(std::move(draw)));
}
};
Expand Down
44 changes: 24 additions & 20 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3850,6 +3850,9 @@ class PipelineState final
if (raster.depthClampEnable && !features.depthClamp)
return refuse("DepthClipEnable = FALSE needs depthClamp, which the device lacks; flag", 0);

graphics_.viewport_with_count =
features.multiViewport && vk.vkCmdSetViewportWithCount && vk.vkCmdSetScissorWithCount;

graphics_.color_formats.assign(desc.NumRenderTargets, VK_FORMAT_UNDEFINED);
for (UINT i = 0; i < desc.NumRenderTargets; ++i) {
if (desc.RTVFormats[i] == DXGI_FORMAT_UNKNOWN) continue;
Expand Down Expand Up @@ -4103,14 +4106,6 @@ 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_;
std::vector<VkFormat> colors = graphics_.color_formats;
for (std::size_t slot = 0; slot < colors.size(); ++slot)
Expand All @@ -4137,8 +4132,10 @@ 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;
if (!graphics_.viewport_with_count) {
viewport.viewportCount = 1;
viewport.scissorCount = 1;
}
VkPipelineMultisampleStateCreateInfo multisample = r.multisample;
VkPipelineRasterizationStateCreateInfo raster = r.raster;
VkPipelineRasterizationConservativeStateCreateInfoEXT conservative{
Expand All @@ -4155,17 +4152,23 @@ class PipelineState final
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};
// With multiViewport enabled, VIEWPORT_WITH_COUNT / SCISSOR_WITH_COUNT allow 1..16 dynamic viewports/scissors.
VkDynamicState dyn_list[8]{};
std::uint32_t dyn_count = 0;
dyn_list[dyn_count++] =
graphics_.viewport_with_count ? VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT : VK_DYNAMIC_STATE_VIEWPORT;
dyn_list[dyn_count++] =
graphics_.viewport_with_count ? VK_DYNAMIC_STATE_SCISSOR_WITH_COUNT : VK_DYNAMIC_STATE_SCISSOR;
if (!r.mesh) {
dyn_list[dyn_count++] = VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY;
dyn_list[dyn_count++] = VK_DYNAMIC_STATE_PRIMITIVE_RESTART_ENABLE;
}
dyn_list[dyn_count++] = VK_DYNAMIC_STATE_STENCIL_REFERENCE;
dyn_list[dyn_count++] = VK_DYNAMIC_STATE_BLEND_CONSTANTS;
if (!r.mesh && !r.bindings.empty()) dyn_list[dyn_count++] = VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE;
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 = dyn_count;
dynamic.pDynamicStates = dyn_list;

VkGraphicsPipelineCreateInfo info{VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, &rendering};
info.stageCount = std::uint32_t(r.stages.size());
Expand Down Expand Up @@ -9796,6 +9799,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
10 changes: 10 additions & 0 deletions runtime/src/command_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,15 @@ struct DrawViews {
std::uint32_t render_target_count = 8;
};

// Shared across draws that keep the same multi-viewport / multi-scissor state (null for the common 1-viewport case).
struct DrawViewports {
static constexpr std::uint32_t max_count = 16;
std::array<Viewport, max_count> viewports{};
std::uint32_t viewport_count = 0;
std::array<Rect, max_count> scissors{};
std::uint32_t scissor_count = 0;
};

// DrawInstanced / DrawIndexedInstanced / DispatchMesh, with everything they use captured when they are recorded:
// D3D12 reads render-target and depth descriptors at OMSetRenderTargets, and every other piece of state is the
// list's state at the draw. resource 0 marks an unbound render-target slot or no depth buffer. A mesh draw
Expand All @@ -440,6 +449,7 @@ struct Draw {
std::int32_t base_vertex = 0;
std::uint32_t first_instance = 0;
bool mesh = false;
std::shared_ptr<const DrawViewports> viewports; // non-null when >1 viewport or >1 scissor was bound
};

// D3D12_TEXTURE_DATA_PITCH_ALIGNMENT and D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT.
Expand Down
3 changes: 3 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 @@ -970,6 +971,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
Loading