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
27 changes: 27 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4558,6 +4558,33 @@ if(TARGET Vulkan::Headers)
# the thread (d3d12_compile_teardown_test).
set_tests_properties(d3d12_pso_validation PROPERTIES SKIP_RETURN_CODE 77
FAIL_REGULAR_EXPRESSION "Validation Error;VUID-")
# D3D-302: a StreamOutput PSO and SOSetTargets actually capture a draw's output, read back and checked
# against the shader's fixed vec4. The PS just needs a valid render target to rasterize into (its own
# output colour is not checked), so it is its own trivial shader rather than ps_draw's CBV-reading one.
set(LEONIDA_TEST_VS_STREAM_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generated/vs_stream_output.spv)
set(LEONIDA_TEST_PS_STREAM_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generated/ps_stream_output.spv)
foreach(pair "vs_stream_output.vert;${LEONIDA_TEST_VS_STREAM_OUTPUT}"
"ps_stream_output.frag;${LEONIDA_TEST_PS_STREAM_OUTPUT}")
list(GET pair 0 source)
list(GET pair 1 output)
add_custom_command(
OUTPUT ${output}
COMMAND ${Vulkan_GLSLANG_VALIDATOR_EXECUTABLE} -V --target-env vulkan1.3 -o ${output}
${CMAKE_CURRENT_SOURCE_DIR}/test/glsl/${source}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/test/glsl/${source}
COMMENT "Compiling the stream-output test shader ${source}"
VERBATIM)
endforeach()
add_executable(d3d12_stream_output_test test/d3d12_stream_output_test.cpp shims/d3d12_xs_device.cpp
${LEONIDA_TEST_VS_STREAM_OUTPUT} ${LEONIDA_TEST_PS_STREAM_OUTPUT})
target_link_libraries(d3d12_stream_output_test PRIVATE gta6rt_d3d12_com)
target_compile_definitions(d3d12_stream_output_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX
LEONIDA_TEST_VS_STREAM_OUTPUT_SPV="${LEONIDA_TEST_VS_STREAM_OUTPUT}"
LEONIDA_TEST_PS_STREAM_OUTPUT_SPV="${LEONIDA_TEST_PS_STREAM_OUTPUT}")
target_compile_options(d3d12_stream_output_test PRIVATE /W4 /permissive- /utf-8)
add_test(NAME d3d12_stream_output COMMAND d3d12_stream_output_test)
set_tests_properties(d3d12_stream_output PROPERTIES SKIP_RETURN_CODE 77
FAIL_REGULAR_EXPRESSION "Validation Error;VUID-")
# Conservative raster Tier 1, with geometric coverage bounds and host D3D12 references.
set(LEONIDA_CONSERVATIVE_VS ${CMAKE_CURRENT_BINARY_DIR}/generated/vs_conservative.spv)
set(LEONIDA_CONSERVATIVE_PS ${CMAKE_CURRENT_BINARY_DIR}/generated/ps_conservative.spv)
Expand Down
27 changes: 27 additions & 0 deletions runtime/shims/d3d12_com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,22 @@ class GraphicsCommandList final
set_vertex_buffers(start, count, views);
}

// D3D-302. Unlike vertex buffers, an unbound slot inside [start, start+count) stays unbound (no zero-page
// fallback): the pipeline's StreamOutput declaration says how many slots it needs, and prepare_draw treats any
// of those left unbound as "stream output not active for this draw" rather than inventing a destination.
void STDMETHODCALLTYPE SOSetTargets(UINT start, UINT count, const D3D12_STREAM_OUTPUT_BUFFER_VIEW* views) override {
if (std::uint64_t(start) + count > so_buffers_.size()) return check(false);
if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) {
std::vector<D3D12_STREAM_OUTPUT_BUFFER_VIEW> copy;
if (views) copy.assign(views, views + count);
defer([=, copy = std::move(copy), bound = views != nullptr](GraphicsCommandList& l) {
l.set_so_targets(start, count, bound ? copy.data() : nullptr);
});
return;
}
set_so_targets(start, count, views);
}

void STDMETHODCALLTYPE IASetIndexBuffer(const D3D12_INDEX_BUFFER_VIEW* view) override {
if (list_->closed()) return;
const std::optional<D3D12_INDEX_BUFFER_VIEW> copy = view ? std::optional(*view) : std::nullopt;
Expand Down Expand Up @@ -1839,6 +1855,7 @@ class GraphicsCommandList final
std::uint64_t console_high_base_ = 0; // the console heap table_high_[2] is for
std::uint32_t topology_ = 0; // D3D_PRIMITIVE_TOPOLOGY_UNDEFINED
std::array<leonida::VertexBufferView, D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT> vertex_buffers_{};
std::array<leonida::SOBufferView, D3D12_SO_BUFFER_SLOT_COUNT> so_buffers_{};
leonida::IndexBufferView index_{};
void note_write_va(std::uint64_t va, std::uint64_t bytes) {
if (va && bytes) write_ranges_.emplace_back(va, bytes);
Expand Down Expand Up @@ -2117,6 +2134,7 @@ class GraphicsCommandList final
direct_cbv_table_recorded_ = false;
topology_ = 0;
vertex_buffers_ = {};
so_buffers_ = {};
index_ = {};
viewport_ = {};
scissor_ = {};
Expand Down Expand Up @@ -2185,6 +2203,14 @@ class GraphicsCommandList final
}
}

void set_so_targets(UINT start, UINT count, const D3D12_STREAM_OUTPUT_BUFFER_VIEW* views) {
if (count) draw_views_stale_ = true;
for (UINT i = 0; i < count; ++i)
so_buffers_[start + i] = views ? leonida::SOBufferView{views[i].BufferLocation, views[i].SizeInBytes,
views[i].BufferFilledSizeLocation}
: leonida::SOBufferView{};
}

void set_index_buffer(const D3D12_INDEX_BUFFER_VIEW* view) {
const std::uint64_t va =
view ? leonida::title_gpu_views::to_host_view(view->BufferLocation, view->SizeInBytes) : 0;
Expand Down Expand Up @@ -2325,6 +2351,7 @@ class GraphicsCommandList final
auto views = std::make_shared<leonida::DrawViews>();
views->render_targets = render_targets_;
views->vertex_buffers = vertex_buffers_;
views->so_buffers = so_buffers_;
views->render_target_count = render_target_count_;
draw_views_ = std::move(views);
draw_views_stale_ = false;
Expand Down
4 changes: 4 additions & 0 deletions runtime/shims/d3d12_com.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ struct VulkanDeviceInfo {
// VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT query. Without it an SO_STATISTICS heap is made without a pool and
// every SO_STATISTICS query resolves zero, even when the transformFeedback feature is enabled.
bool transform_feedback_queries = false;
// VkPhysicalDeviceTransformFeedbackPropertiesEXT limits, read when VK_EXT_transform_feedback is enabled: a
// StreamOutput PSO's NumStrides must not exceed max_transform_feedback_buffers, and a non-default
// RasterizedStream must be below max_transform_feedback_streams. Both 0 when the extension is not enabled.
std::uint32_t max_transform_feedback_buffers = 0;
// The device's maxTransformFeedbackStreams property: an SO_STATISTICS_STREAMn query runs only for n below it.
// Streams from this count up (to the D3D12 maximum of four) run nothing and resolve zero.
std::uint32_t max_transform_feedback_streams = 0;
Expand Down
67 changes: 60 additions & 7 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6087,6 +6087,12 @@ struct DeviceState {
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
if (info.conditional_rendering) result.usage |= VK_BUFFER_USAGE_CONDITIONAL_RENDERING_BIT_EXT;
// D3D-302 (BubsMcgee): a buffer bound to vkCmdBindTransformFeedbackBuffersEXT or passed as a counter
// buffer must have been created with these usage bits (VUID-vkCmdBindTransformFeedbackBuffersEXT-
// pBuffers-02360); NVIDIA does not enforce it, stricter drivers do.
if (info.transform_feedback)
result.usage |= VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT |
VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_COUNTER_BUFFER_BIT_EXT;
// D3D12 reserved buffers are visible to DIRECT, COPY and COMPUTE queues without exposing Vulkan
// queue-family ownership. Their tile bindings and accesses may use distinct families, so match the
// CONCURRENT sharing policy used for ordinary buffers and the GPU address arena.
Expand Down Expand Up @@ -10259,6 +10265,9 @@ struct DeviceState {
create.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT |
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
if (info.transform_feedback)
create.usage |= VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT |
VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_COUNTER_BUFFER_BIT_EXT;
share(create);
if (vk.vkCreateBuffer(info.device, &create, nullptr, &heap.buffer) != VK_SUCCESS) return false;
const auto fail = [&] {
Expand Down Expand Up @@ -14664,6 +14673,12 @@ class Resource final
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT;
if (device_.info.conditional_rendering) info.usage |= VK_BUFFER_USAGE_CONDITIONAL_RENDERING_BIT_EXT;
// D3D-302 (BubsMcgee): a resource bound through SOSetTargets, or as its counter, needs these usage bits
// at creation (VUID-vkCmdBindTransformFeedbackBuffersEXT-pBuffers-02360); not enforced on NVIDIA, is on
// stricter drivers.
if (device_.info.transform_feedback)
info.usage |= VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT |
VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_COUNTER_BUFFER_BIT_EXT;
if (addressable || need_bda) info.usage |= VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
if (device_.info.ray_tracing) {
info.usage |= VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR |
Expand Down Expand Up @@ -14816,6 +14831,12 @@ class Resource final
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT;
if (device_.info.conditional_rendering) info.usage |= VK_BUFFER_USAGE_CONDITIONAL_RENDERING_BIT_EXT;
// D3D-302 (BubsMcgee): a resource bound through SOSetTargets, or as its counter, needs these usage bits
// at creation (VUID-vkCmdBindTransformFeedbackBuffersEXT-pBuffers-02360); not enforced on NVIDIA, is on
// stricter drivers.
if (device_.info.transform_feedback)
info.usage |= VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT |
VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_COUNTER_BUFFER_BIT_EXT;
if (need_bda) info.usage |= VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
device_.share(info);
if (vk.vkCreateBuffer(device_.info.device, &info, nullptr, &buffer_) != VK_SUCCESS) return E_OUTOFMEMORY;
Expand Down Expand Up @@ -15019,6 +15040,12 @@ class Resource final
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT;
if (device_.info.conditional_rendering) info.usage |= VK_BUFFER_USAGE_CONDITIONAL_RENDERING_BIT_EXT;
// D3D-302 (BubsMcgee): a resource bound through SOSetTargets, or as its counter, needs these usage bits
// at creation (VUID-vkCmdBindTransformFeedbackBuffersEXT-pBuffers-02360); not enforced on NVIDIA, is on
// stricter drivers.
if (device_.info.transform_feedback)
info.usage |= VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT |
VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_COUNTER_BUFFER_BIT_EXT;
if (need_bda) info.usage |= VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
device_.share(info);
if (vk.vkCreateBuffer(device_.info.device, &info, nullptr, &buffer_) != VK_SUCCESS) return E_OUTOFMEMORY;
Expand Down Expand Up @@ -18148,8 +18175,26 @@ class PipelineState final
if (rt0.BlendEnable && rt0.LogicOpEnable) return E_INVALIDARG;
if (rt0.LogicOpEnable && unsigned(rt0.LogicOp) > D3D12_LOGIC_OP_OR_INVERTED) return E_INVALIDARG;
}
if (desc.HS.BytecodeLength || desc.DS.BytecodeLength || desc.StreamOutput.NumEntries) {
return stream_unsupported("graphics pipeline stage not supported yet (hull/domain/stream output):", 0);
if (desc.HS.BytecodeLength || desc.DS.BytecodeLength) {
return stream_unsupported("graphics pipeline stage not supported yet (hull/domain):", 0);
}
// D3D-302: the plumbing only covers the common case (one rasterized stream, the default stream 0). A
// RasterizedStream other than 0 (an explicit stream, or D3D12_SO_NO_RASTERIZED_STREAM for capture-only)
// needs VkPipelineRasterizationStateStreamCreateInfoEXT / rasterizerDiscardEnable at pipeline creation,
// which is follow-up work; refuse it cleanly instead of silently rasterizing the wrong stream.
const D3D12_STREAM_OUTPUT_DESC& stream_output_desc = desc.StreamOutput;
const bool stream_output = stream_output_desc.NumEntries > 0;
if (stream_output) {
if (!device_.info.transform_feedback || !vk.vkCmdBeginTransformFeedbackEXT) {
log_once("StreamOutput PSOs need VK_EXT_transform_feedback, which the device has not enabled; stage",
1);
return E_NOTIMPL;
}
if (stream_output_desc.NumStrides > device_.info.max_transform_feedback_buffers) return E_INVALIDARG;
if (stream_output_desc.RasterizedStream != 0) {
log_once("StreamOutput with a RasterizedStream other than 0 is not supported yet; stage", 1);
return E_NOTIMPL;
}
}
// A geometry shader is the translator's Vulkan geometry stage (docs/design.md, "Geometry shaders"): it takes
// triangles and emits a triangle strip. It needs the geometryShader feature, and the PSO's topology type must
Expand Down Expand Up @@ -18932,6 +18977,9 @@ class PipelineState final
graphics_.stencil_writes =
depth.stencilTestEnable != VK_FALSE &&
(stencil_face_writes(depth.front) || stencil_face_writes(depth.back));
if (stream_output)
graphics_.so_strides.assign(stream_output_desc.pBufferStrides,
stream_output_desc.pBufferStrides + stream_output_desc.NumStrides);
recipe_.mesh = mesh;
recipe_.stages = std::move(stage_infos);
recipe_.attributes = std::move(attributes);
Expand Down Expand Up @@ -21591,9 +21639,9 @@ class QueryHeap final
case h::so_statistics:
// D3D-302: with VK_EXT_transform_feedback enabled, an SO_STATISTICS heap is a real transform-feedback
// pool whose queries begin/end on a stream (vkCmdBeginQueryIndexedEXT); a resolve copies 16 bytes per
// query (NumPrimitivesWritten, PrimitivesStorageNeeded), in the same order. StreamOutput PSOs are still
// refused and SOSetTargets is still not implemented, so no primitive reaches a stream yet and every
// query counts 0. The pool needs the transformFeedbackQueries property and at least one stream; without
// query (NumPrimitivesWritten, PrimitivesStorageNeeded), in the same order. StreamOutput PSOs and
// SOSetTargets now capture real primitives (prepare_draw, vulkan_replay.h), so this pool counts what
// they wrote. The pool needs the transformFeedbackQueries property and at least one stream; without
// them (or without the feature) the heap is made without a pool and every query resolves to zero.
if (!device_.info.transform_feedback || !device_.info.transform_feedback_queries ||
!device_.info.max_transform_feedback_streams)
Expand Down Expand Up @@ -22920,8 +22968,12 @@ class Device final
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT |
VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
// Default-heap buffers live here, and any of them can be a SetPredication buffer.
// Default-heap buffers live here, and any of them can be a SetPredication buffer or a SOSetTargets
// buffer/counter (D3D-302, BubsMcgee): both need their own usage bit at creation.
if (state_.info.conditional_rendering) info.usage |= VK_BUFFER_USAGE_CONDITIONAL_RENDERING_BIT_EXT;
if (state_.info.transform_feedback)
info.usage |= VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT |
VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_COUNTER_BUFFER_BIT_EXT;
// With ray tracing, AS result, scratch and build-input buffers are default-heap ranges of the arena.
if (state_.info.ray_tracing)
info.usage |= VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR |
Expand Down Expand Up @@ -31565,11 +31617,12 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin
vkGetPhysicalDeviceFeatures2(info.physical, &features);
info.transform_feedback = transform_feedback_features.transformFeedback == VK_TRUE;
info.transform_feedback_geometry_streams = transform_feedback_features.geometryStreams == VK_TRUE;
// D3D-302: the queries and the stream count are properties, not features.
// D3D-302: the queries, buffer count and stream count are properties, not features.
VkPhysicalDeviceProperties2 properties{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
&transform_feedback_properties};
vkGetPhysicalDeviceProperties2(info.physical, &properties);
info.transform_feedback_queries = transform_feedback_properties.transformFeedbackQueries == VK_TRUE;
info.max_transform_feedback_buffers = transform_feedback_properties.maxTransformFeedbackBuffers;
info.max_transform_feedback_streams = transform_feedback_properties.maxTransformFeedbackStreams;
}
}
Expand Down
9 changes: 9 additions & 0 deletions runtime/src/command_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,21 @@ struct IndexBufferView {
std::uint32_t format; // DXGI_FORMAT_R16_UINT (57) or DXGI_FORMAT_R32_UINT (42)
};

// D3D12_STREAM_OUTPUT_BUFFER_VIEW: console VAs, resolved to buffers at replay. filled_size_va is where the GPU
// keeps the running byte count written so far (D3D12_STREAM_OUTPUT_BUFFER_VIEW::BufferFilledSizeLocation); 0: unset.
struct SOBufferView {
std::uint64_t va;
std::uint64_t size;
std::uint64_t filled_size_va;
};

// The views a draw captured, at the D3D12 maxima. Kept by value because the CPU descriptor slots and the bound
// views can change after the draw is recorded, and out of line because the command variant is sized by its largest
// member: inline they made every recorded command, barriers included, 976 bytes.
struct DrawViews {
std::array<TargetView, 8> render_targets{};
std::array<VertexBufferView, 32> vertex_buffers{}; // by input slot
std::array<SOBufferView, 4> so_buffers{}; // by SOSetTargets slot (D3D12_SO_BUFFER_SLOT_COUNT)
std::uint32_t render_target_count = 8;
};

Expand Down
Loading
Loading