diff --git a/docs/pipeline_check.md b/docs/pipeline_check.md index d5d76cb931..c3c6a28b20 100644 --- a/docs/pipeline_check.md +++ b/docs/pipeline_check.md @@ -457,6 +457,10 @@ driver gives it the same cache key in both programs. Measured on Windows with an `VK_KHR_maintenance8` into the pipeline cache key even for compute shaders, so the checker enables both whenever supported and keeps `meshShaderQueries` on. - On a Windows RTX 5070 Ti (617.42), a compute-only seed missed with mesh features omitted and hit with the supported mesh and mesh-query features enabled on both devices. The shader, push constants and pipeline layout were identical. Cache-producing runs request this device configuration for every selected shader batch. +- On Windows with an AMD Radeon RX 7900 XT, `VK_EXT_transform_feedback` (with `transformFeedback` enabled, and + `geometryStreams` when the device offers it) is part of the key: the runtime enables it whenever the extension is + present (D3D-302), so the checker enables it too. Without it, `pipeline_cache_seed` failed 3 of 3 on that GPU and + passed 3 of 3 on `main`. NVIDIA leaves it out of the key, which is why it did not show up on the RTX 3080 Ti. - The executable's file name is part of the key; its folder is not. A copy of the checker under another name misses the cache the original wrote, and the runtime's device hits the checker's cache only from a program with the checker's file name. So on this driver a cache written by `vk_pipeline_check.exe` does not hit in diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 86378dfc86..5006dc5810 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -3728,6 +3728,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) diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 6dee464a8f..b76cc83ff7 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp @@ -434,6 +434,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 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 copy = view ? std::optional(*view) : std::nullopt; @@ -1563,6 +1579,7 @@ class GraphicsCommandList final D3D12_GPU_VIRTUAL_ADDRESS sampler_base_ = 0; std::uint32_t topology_ = 0; // D3D_PRIMITIVE_TOPOLOGY_UNDEFINED std::array vertex_buffers_{}; + std::array so_buffers_{}; leonida::IndexBufferView index_{}; // SetGraphicsRootDescriptorTable / SetComputeRootDescriptorTable: VA, CBV/SRV/UAV slots the signature declares std::vector> tables_; @@ -1689,6 +1706,7 @@ class GraphicsCommandList final sampler_base_ = 0; topology_ = 0; vertex_buffers_ = {}; + so_buffers_ = {}; index_ = {}; viewport_ = {}; scissor_ = {}; @@ -1755,6 +1773,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; @@ -1885,6 +1911,7 @@ class GraphicsCommandList final auto views = std::make_shared(); 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; diff --git a/runtime/shims/d3d12_com.h b/runtime/shims/d3d12_com.h index 849791a262..ac9c928214 100644 --- a/runtime/shims/d3d12_com.h +++ b/runtime/shims/d3d12_com.h @@ -215,6 +215,18 @@ struct VulkanDeviceInfo { // VK_EXT_mesh_shader's meshShaderQueries feature is enabled on `device`, so the clipping invocations of a mesh // segment come from a mesh-primitives query (D3D-MESH-CLIPPING-COUNTER). Without it they read 0. bool mesh_shader_queries = false; + // VK_EXT_transform_feedback with its transformFeedback feature is detected and enabled on `device` (D3D-302). + // Nothing uses it yet: a graphics PSO that declares StreamOutput is still refused and SOSetTargets is still not + // implemented. (#1630 adds the SO_STATISTICS query pools, which use it.) + bool transform_feedback = false; + // VK_EXT_transform_feedback's geometryStreams feature is enabled when the device offers it. Nothing rasterizes a + // StreamOutput RasterizedStream yet; it is here for the capture implementation that follows. + bool transform_feedback_geometry_streams = 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; + std::uint32_t max_transform_feedback_streams = 0; // The core features enabled on `device`. Graphics PSOs that need independentBlend, fillModeNonSolid, // depthBiasClamp or dualSrcBlend are refused unless it is set here. VkPhysicalDeviceFeatures features{}; diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index aeecf05b0d..23cc3f7761 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -2995,6 +2995,9 @@ 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; + 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. @@ -3515,6 +3518,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 = [&] { @@ -5656,6 +5662,9 @@ 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; + 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 | @@ -5807,6 +5816,9 @@ 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; + 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; @@ -5983,6 +5995,9 @@ 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; + 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; @@ -8259,8 +8274,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 @@ -8878,6 +8911,9 @@ class PipelineState final // Kept for variants: a draw that leaves a slot unbound needs this pipeline with that format undefined. graphics_.mesh = mesh; + 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); @@ -10608,10 +10644,12 @@ class QueryHeap final break; } case h::so_statistics: - // The layer has no stream output: a graphics PSO with StreamOutput entries is refused (E_NOTIMPL) and - // SOSetTargets is not implemented, so no primitive ever reaches a stream and every query of this heap - // counts 0 written and 0 needed. That needs no Vulkan pool: Begin/EndQuery run nothing and a resolve fills zeros. When stream - // output lands, this becomes a VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT pool (D3D-302). + // D3D-302: StreamOutput PSOs and SOSetTargets now capture real primitives (prepare_draw, + // vulkan_replay.h), but this heap's counters are not wired to that yet: every query still counts 0 + // written and 0 needed. That needs no Vulkan pool: Begin/EndQuery run nothing and a resolve fills + // zeros. Follow-up: a VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT pool with the indexed + // vkCmdBeginQueryIndexedEXT/EndQueryIndexedEXT commands, read back through the counter buffers + // SOSetTargets' BufferFilledSizeLocation already resolves. return register_pool(desc); case h::pipeline_statistics: if (!device_.info.pipeline_statistics_query) { @@ -11373,6 +11411,9 @@ class Device final 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. 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 | @@ -17727,6 +17768,8 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_FEATURES_KHR}; VkPhysicalDeviceRayQueryFeaturesKHR rq_features{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_QUERY_FEATURES_KHR}; VkPhysicalDeviceMeshShaderFeaturesEXT mesh_features{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_FEATURES_EXT}; + VkPhysicalDeviceTransformFeedbackFeaturesEXT transform_feedback_features{ + VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT}; { std::uint32_t extension_count = 0; vkEnumerateDeviceExtensionProperties(info.physical, nullptr, &extension_count, nullptr); @@ -17751,10 +17794,28 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin vkGetPhysicalDeviceFeatures2(info.physical, &features); info.mesh_shader = mesh_features.meshShader == VK_TRUE; info.mesh_shader_queries = mesh_features.meshShaderQueries == VK_TRUE; + } else if (!std::strcmp(e.extensionName, VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME)) { + VkPhysicalDeviceFeatures2 features{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, + &transform_feedback_features}; + vkGetPhysicalDeviceFeatures2(info.physical, &features); + info.transform_feedback = transform_feedback_features.transformFeedback == VK_TRUE; + info.transform_feedback_geometry_streams = transform_feedback_features.geometryStreams == VK_TRUE; + if (info.transform_feedback) { + VkPhysicalDeviceTransformFeedbackPropertiesEXT tf_props{ + VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT}; + VkPhysicalDeviceProperties2 props{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &tf_props}; + vkGetPhysicalDeviceProperties2(info.physical, &props); + info.max_transform_feedback_buffers = tf_props.maxTransformFeedbackBuffers; + info.max_transform_feedback_streams = tf_props.maxTransformFeedbackStreams; + } } } if (!info.mesh_shader) std::fprintf(stderr, "[rt] d3d12: the GPU has no VK_EXT_mesh_shader: mesh-shader PSOs are unsupported\n"); + if (!info.transform_feedback) + std::fprintf(stderr, + "[rt] d3d12: the GPU has no VK_EXT_transform_feedback: stream output is unsupported and " + "SO_STATISTICS pools read zero\n"); if (has_as && has_dho && has_rq && chosen12.bufferDeviceAddress) { as_features.pNext = &rq_features; VkPhysicalDeviceFeatures2 features{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &as_features}; @@ -17790,11 +17851,22 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin // The mesh-primitives query, which counts the primitives a mesh shader emits (D3D-MESH-CLIPPING-COUNTER). enable_mesh.meshShaderQueries = info.mesh_shader_queries ? VK_TRUE : VK_FALSE; + // D3D-302: StreamOutput PSOs, SOSetTargets and real SO_STATISTICS queries need transformFeedback; geometryStreams + // is only requested when the device already offers it (RasterizedStream != 0 is the one thing it gates). + VkPhysicalDeviceTransformFeedbackFeaturesEXT enable_transform_feedback{ + VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT}; + enable_transform_feedback.transformFeedback = VK_TRUE; + enable_transform_feedback.geometryStreams = info.transform_feedback_geometry_streams ? VK_TRUE : VK_FALSE; + void* ext_chain = nullptr; if (info.mesh_shader) { enable_mesh.pNext = ext_chain; ext_chain = &enable_mesh; } + if (info.transform_feedback) { + enable_transform_feedback.pNext = ext_chain; + ext_chain = &enable_transform_feedback; + } if (info.ray_tracing) { enable_rq.pNext = ext_chain; enable_as.pNext = &enable_rq; @@ -18297,6 +18369,7 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin if (has_swapchain && instance_has_surface) device_extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); if (swapchain_m1_name) device_extensions.push_back(swapchain_m1_name); if (info.mesh_shader) device_extensions.push_back(VK_EXT_MESH_SHADER_EXTENSION_NAME); + if (info.transform_feedback) device_extensions.push_back(VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME); if (info.ray_tracing) { device_extensions.push_back(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME); device_extensions.push_back(VK_KHR_DEFERRED_HOST_OPERATIONS_EXTENSION_NAME); diff --git a/runtime/src/command_list.h b/runtime/src/command_list.h index 3d7ed093bb..73a493348a 100644 --- a/runtime/src/command_list.h +++ b/runtime/src/command_list.h @@ -406,12 +406,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 render_targets{}; std::array vertex_buffers{}; // by input slot + std::array so_buffers{}; // by SOSetTargets slot (D3D12_SO_BUFFER_SLOT_COUNT) std::uint32_t render_target_count = 8; }; diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 145fa11ee5..c164e661a4 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h @@ -130,7 +130,8 @@ struct VulkanDeviceFunctions { X(vkCreatePipelineCache) X(vkDestroyPipelineCache) X(vkGetPipelineCacheData) X(vkMergePipelineCaches) \ X(vkCreateEvent) X(vkDestroyEvent) X(vkResetEvent) X(vkCmdSetEvent) X(vkCmdWaitEvents) X(vkCmdDrawMeshTasksEXT) \ X(vkCreateRenderPass2) X(vkDestroyRenderPass) X(vkCreateFramebuffer) X(vkDestroyFramebuffer) \ - X(vkCmdBeginRenderPass) X(vkCmdEndRenderPass) + X(vkCmdBeginRenderPass) X(vkCmdEndRenderPass) X(vkCmdBindTransformFeedbackBuffersEXT) \ + X(vkCmdBeginTransformFeedbackEXT) X(vkCmdEndTransformFeedbackEXT) #define LEONIDA_VK_MEMBER(name) PFN_##name name = nullptr; LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_MEMBER) #undef LEONIDA_VK_MEMBER @@ -1151,6 +1152,10 @@ class VulkanContext { replay_.draw = vk.vkCmdDraw; replay_.draw_mesh_tasks = vk.vkCmdDrawMeshTasksEXT; // null unless VK_EXT_mesh_shader is enabled replay_.draw_indexed = vk.vkCmdDrawIndexed; + // null unless VK_EXT_transform_feedback is enabled (D3D-302) + replay_.bind_transform_feedback_buffers = vk.vkCmdBindTransformFeedbackBuffersEXT; + replay_.begin_transform_feedback = vk.vkCmdBeginTransformFeedbackEXT; + replay_.end_transform_feedback = vk.vkCmdEndTransformFeedbackEXT; replay_.graphics_pipeline = [this](std::uintptr_t id) -> const GraphicsPipeline* { const auto it = graphics_pipelines_.find(id); return it == graphics_pipelines_.end() ? nullptr : &it->second; diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index e20a291057..dc52713062 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -882,6 +882,11 @@ struct GraphicsPipeline { std::uint32_t samples = 1; // rasterizationSamples; every bound attachment must have this count bool async_build = false; bool nonunit_vertex_divisor = false; // an actual instance divisor > 1, not a zero-stride emulation + // D3D12_STREAM_OUTPUT_DESC, kept to capture and wrap draws with this pipeline bound (D3D-302). Empty + // so_strides: the PSO declared no StreamOutput, same as before this was tracked at all. + std::vector so_strides; // by buffer slot, NumStrides entries + std::uint32_t so_rasterized_stream = 0; // D3D12_SO_NO_RASTERIZED_STREAM: no stream is rasterized + bool has_stream_output() const { return !so_strides.empty(); } }; // The 144-byte push-constant block every translated shader declares: s0..s31, heap base, code base. @@ -1099,6 +1104,10 @@ struct ReplayFunctions { unsigned* mesh_draw_count = nullptr; // opt-in count for this submission PFN_vkCmdDrawMeshTasksEXT draw_mesh_tasks = nullptr; // VK_EXT_mesh_shader; null: mesh draws are unsupported PFN_vkCmdDrawIndexed draw_indexed = nullptr; + // VK_EXT_transform_feedback (D3D-302); null: StreamOutput pipelines are unsupported (create_graphics refuses them). + PFN_vkCmdBindTransformFeedbackBuffersEXT bind_transform_feedback_buffers = nullptr; + PFN_vkCmdBeginTransformFeedbackEXT begin_transform_feedback = nullptr; + PFN_vkCmdEndTransformFeedbackEXT end_transform_feedback = nullptr; std::function graphics_pipeline; // The buffer and offset a console VA range starts in (from ResourceBinding::va), false when none does. `size` is // cut to what the buffer holds from there: vertex and index views use what fits, WriteBufferImmediate needs all @@ -1383,6 +1392,15 @@ struct PreparedDraw { // The draw is dropped at emit: its pipeline, a vertex buffer or its index buffer could not be resolved (see // skip_unresolved in prepare). bool skipped = false; + // D3D-302: graphics->so_strides.size() entries when the pipeline declares StreamOutput, else empty. A slot + // with no bound SOSetTargets buffer leaves stream output inactive for the draw (an ordinary draw), rather than + // binding VK_NULL_HANDLE, which vkCmdBindTransformFeedbackBuffersEXT refuses. + std::vector so_buffers; + std::vector so_offsets, so_sizes; + // The counter (BufferFilledSizeLocation) for each so_buffers entry, or VK_NULL_HANDLE: Begin starts that + // stream's running count at 0 instead of resuming it, and End does not write one back. + std::vector so_counter_buffers; + std::vector so_counter_offsets; // Back to a new draw's state, keeping the vectors' storage. void reset() { @@ -1407,6 +1425,11 @@ struct PreparedDraw { indirect_count_offset = 0; area = {}; layers = 1; + so_buffers.clear(); + so_offsets.clear(); + so_sizes.clear(); + so_counter_buffers.clear(); + so_counter_offsets.clear(); } }; @@ -2716,6 +2739,43 @@ ReplayResult prepare_draw(const Draw& draw, const IndirectDrawArgs* arguments, R // A non-indexed quad list is drawn from the shared triangle index buffer (emit). if (!draw.indexed && !draw.mesh && draw.topology == topology_quad_list && !vk.quad_indices) return unresolved_at(__LINE__); + // D3D-302: resolve this draw's bound StreamOutput targets. The pipeline's so_strides.size() names how many + // buffer slots its declaration uses. Assumption, with no device to check it against (docs/d3d12_vulkan.md + // D3D-302): if any of those slots has no SOSetTargets buffer currently bound, stream output stays inactive + // for this draw (an ordinary draw, not a refusal) rather than failing it. + if (!pipeline->so_strides.empty()) { + const std::size_t count = pipeline->so_strides.size(); + std::vector so_buffers(count, VK_NULL_HANDLE); + std::vector so_offsets(count, 0), so_sizes(count, 0); + std::vector so_counters(count, VK_NULL_HANDLE); + std::vector so_counter_offsets(count, 0); + bool all_bound = true; + for (std::size_t slot = 0; slot < count && all_bound; ++slot) { + const bool bound = draw.views && slot < draw.views->so_buffers.size() && draw.views->so_buffers[slot].va; + if (!bound) { + all_bound = false; + break; + } + const SOBufferView& view = draw.views->so_buffers[slot]; + std::uint64_t size = view.size; + if (!vk.buffer_at(view.va, size, so_buffers[slot], so_offsets[slot])) + return ReplayResult::unresolved_resource; + so_sizes[slot] = size; + if (view.filled_size_va) { + // BufferFilledSizeLocation names a 32-bit UINT (D3D12), and the Vulkan counter is a 32-bit value. + std::uint64_t counter_size = sizeof(std::uint32_t); + if (!vk.buffer_at(view.filled_size_va, counter_size, so_counters[slot], so_counter_offsets[slot])) + return ReplayResult::unresolved_resource; + } + } + if (all_bound) { + p.so_buffers = std::move(so_buffers); + p.so_offsets = std::move(so_offsets); + p.so_sizes = std::move(so_sizes); + p.so_counter_buffers = std::move(so_counters); + p.so_counter_offsets = std::move(so_counter_offsets); + } + } if (draw.indexed) { if (arguments && (!draw.index.va || (draw.index.format != 57 && draw.index.format != 42) || draw.index.size < (draw.index.format == 57 ? 2u : 4u) || @@ -5201,6 +5261,18 @@ inline void emit_commands(const PreparedList& prepared, VkCommandBuffer command_ vk.bind_vertex_buffers2(command_buffer, slot, 1, &d.vertex_buffers[slot], &d.vertex_offsets[slot], &d.vertex_sizes[slot], &d.vertex_strides[slot]); } + // D3D-302: active only when every buffer slot the pipeline's StreamOutput declares is currently bound + // (prepare_draw leaves d.so_buffers empty otherwise). Bind must run before Begin, and both must be + // inside the render pass this draw is already in (VUID-vkCmdBeginTransformFeedbackEXT-None-02365). + const bool stream_output = !d.so_buffers.empty() && vk.bind_transform_feedback_buffers && + vk.begin_transform_feedback && vk.end_transform_feedback; + if (stream_output) { + const auto count = std::uint32_t(d.so_buffers.size()); + vk.bind_transform_feedback_buffers(command_buffer, 0, count, d.so_buffers.data(), d.so_offsets.data(), + d.so_sizes.data()); + vk.begin_transform_feedback(command_buffer, 0, count, d.so_counter_buffers.data(), + d.so_counter_offsets.data()); + } if (raster.arguments) { indirect_stats::note(indirect_stats::Event::emitted); const auto& arg = *raster.arguments; @@ -5256,6 +5328,11 @@ inline void emit_commands(const PreparedList& prepared, VkCommandBuffer command_ } else { vk.draw(command_buffer, draw->count, draw->instances, draw->first, draw->first_instance); } + if (stream_output) { + const auto count = std::uint32_t(d.so_buffers.size()); + vk.end_transform_feedback(command_buffer, 0, count, d.so_counter_buffers.data(), + d.so_counter_offsets.data()); + } vk.end_rendering(command_buffer); } } diff --git a/runtime/test/d3d12_stream_output_test.cpp b/runtime/test/d3d12_stream_output_test.cpp new file mode 100644 index 0000000000..1681101f6c --- /dev/null +++ b/runtime/test/d3d12_stream_output_test.cpp @@ -0,0 +1,208 @@ +// D3D-302: a StreamOutput PSO and SOSetTargets actually capture a draw's vertex output on the Vulkan-backed +// device. vs_stream_output.vert writes the fixed vec4 (1, 2, 3, 4) to an Xfb-decorated output (buffer 0, stride +// 16 bytes); the PSO declares one matching StreamOutput buffer slot. After a 3-vertex draw, the buffer is read +// back and every vertex's 16 bytes are checked against that constant. No game data; needs VK_EXT_transform_feedback. +#include "vk_test_device.h" +#include "d3d12_test_shaders.h" +#include "../shims/d3d12_com.h" + +#include +#include +#include +#include +#include +#include + +#define EXPECT(condition) do { \ + if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \ +} while (0) + +using namespace d3d12test; + +int main() { + vktest::Device vk; + vk.request_transform_feedback = true; + const auto status = vk.initialize(); + if (status == vktest::DeviceStatus::unavailable) { + std::fprintf(stderr, "SKIP: no usable Vulkan 1.3 device\n"); + return 77; + } + EXPECT(status == vktest::DeviceStatus::ready); + if (!vk.transform_feedback) { + std::fprintf(stderr, "SKIP: no VK_EXT_transform_feedback on this device\n"); + return 77; + } + + // Shader programs and the index that maps their hashes to the compiled test modules (fssm_sha256 is a + // lookup key the test controls on both sides; it has no relation to the real shader bytes). + const auto vs_program = fssm_program({0x56530302u, 0x00000001u}); + const auto ps_program = fssm_program({0x50530302u, 0x00000001u}); + const auto vs_container = shader_container(vs_program); + const auto ps_container = shader_container(ps_program); + const auto fixture = std::filesystem::temp_directory_path() / + ("leonida_stream_output_test_" + std::to_string(GetCurrentProcessId())); + std::filesystem::create_directories(fixture); + std::filesystem::copy_file(LEONIDA_TEST_VS_STREAM_OUTPUT_SPV, fixture / "vs_stream_output.spv", + std::filesystem::copy_options::overwrite_existing); + std::filesystem::copy_file(LEONIDA_TEST_PS_STREAM_OUTPUT_SPV, fixture / "ps_stream_output.spv", + std::filesystem::copy_options::overwrite_existing); + { + std::ofstream index(fixture / "index.json", std::ios::binary); + index << "{\"vs_stream_output\": {\"stage\": \"vs\", \"fssm_sha256\": \"" << leonida::sha256_hex(vs_program) + << "\", \"bindings\": [], \"push_constant_bytes\": 0, \"input_signature\": []},\n" + " \"ps_stream_output\": {\"stage\": \"ps\", \"fssm_sha256\": \"" << leonida::sha256_hex(ps_program) + << "\", \"bindings\": [], \"push_constant_bytes\": 0, \"outputs\": [0]}}"; + } + + rt::d3d12::VulkanDeviceInfo info{}; + info.get_instance_proc = vk.instance_proc; + info.instance = vk.instance; + info.physical = vk.physical; + info.device = vk.device; + info.queue = vk.queue; + info.queue_family = vk.family; + info.buffer_device_address = true; + info.subgroup = vk.subgroup; + info.shader_dir = fixture.string(); + info.transform_feedback = vk.transform_feedback; + info.transform_feedback_geometry_streams = vk.transform_feedback_geometry_streams; + info.max_transform_feedback_buffers = vk.max_transform_feedback_buffers; + info.max_transform_feedback_streams = vk.max_transform_feedback_streams; + Ref device; + EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&device.p)))); + + // Empty root signature: neither shader reads a root parameter. + D3D12_VERSIONED_ROOT_SIGNATURE_DESC root_desc{}; + root_desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_0; + root_desc.Desc_1_0 = {0, nullptr, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE}; + const auto root_blob = serialize_root_signature(root_desc); + EXPECT(!root_blob.empty()); + Ref root; + EXPECT(SUCCEEDED(device->CreateRootSignature(0, root_blob.data(), root_blob.size(), IID_PPV_ARGS(&root.p)))); + + constexpr UINT width = 64, height = 64; + const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT}; + D3D12_RESOURCE_DESC rt_desc{}; + rt_desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + rt_desc.Width = width; + rt_desc.Height = height; + rt_desc.DepthOrArraySize = 1; + rt_desc.MipLevels = 1; + rt_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + rt_desc.SampleDesc.Count = 1; + rt_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; + Ref target; + EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &rt_desc, + D3D12_RESOURCE_STATE_RENDER_TARGET, nullptr, + IID_PPV_ARGS(&target.p)))); + const D3D12_DESCRIPTOR_HEAP_DESC rtv_heap_desc{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1}; + Ref rtv_heap; + EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&rtv_heap_desc, IID_PPV_ARGS(&rtv_heap.p)))); + const D3D12_CPU_DESCRIPTOR_HANDLE rtv = rtv_heap->GetCPUDescriptorHandleForHeapStart(); + device->CreateRenderTargetView(target.p, nullptr, rtv); + + // One StreamOutput buffer slot, stride 16 bytes (one vec4), matching the shader's Xfb decoration. + const UINT stride = 16; + const D3D12_SO_DECLARATION_ENTRY so_entry{0, "OUT", 0, 0, 4, 0}; + D3D12_GRAPHICS_PIPELINE_STATE_DESC pso_desc{}; + pso_desc.pRootSignature = root.p; + pso_desc.VS = {vs_container.data(), vs_container.size()}; + pso_desc.PS = {ps_container.data(), ps_container.size()}; + pso_desc.StreamOutput.pSODeclaration = &so_entry; + pso_desc.StreamOutput.NumEntries = 1; + pso_desc.StreamOutput.pBufferStrides = &stride; + pso_desc.StreamOutput.NumStrides = 1; + pso_desc.StreamOutput.RasterizedStream = 0; + pso_desc.BlendState.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL; + pso_desc.SampleMask = 0xffffffff; + pso_desc.RasterizerState.FillMode = D3D12_FILL_MODE_SOLID; + pso_desc.RasterizerState.CullMode = D3D12_CULL_MODE_NONE; + pso_desc.RasterizerState.DepthClipEnable = TRUE; + pso_desc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE; + pso_desc.NumRenderTargets = 1; + pso_desc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM; + pso_desc.SampleDesc.Count = 1; + Ref pso; + EXPECT(SUCCEEDED(device->CreateGraphicsPipelineState(&pso_desc, IID_PPV_ARGS(&pso.p)))); + + // The SO buffer (DEFAULT heap, created directly in STREAM_OUT state) and a READBACK copy of it. + constexpr UINT64 so_bytes = 256; + D3D12_RESOURCE_DESC buf_desc{}; + buf_desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + buf_desc.Width = so_bytes; + buf_desc.Height = 1; + buf_desc.DepthOrArraySize = 1; + buf_desc.MipLevels = 1; + buf_desc.SampleDesc.Count = 1; + buf_desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + Ref so_buffer; + EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &buf_desc, + D3D12_RESOURCE_STATE_STREAM_OUT, nullptr, + IID_PPV_ARGS(&so_buffer.p)))); + const D3D12_HEAP_PROPERTIES readback_heap{D3D12_HEAP_TYPE_READBACK}; + Ref so_readback; + EXPECT(SUCCEEDED(device->CreateCommittedResource(&readback_heap, D3D12_HEAP_FLAG_NONE, &buf_desc, + D3D12_RESOURCE_STATE_COPY_DEST, nullptr, + IID_PPV_ARGS(&so_readback.p)))); + + const D3D12_COMMAND_QUEUE_DESC queue_desc{D3D12_COMMAND_LIST_TYPE_DIRECT}; + Ref queue; + Ref allocator; + Ref list; + Ref fence; + EXPECT(SUCCEEDED(device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue.p)))); + EXPECT(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p)))); + EXPECT(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, pso.p, + IID_PPV_ARGS(&list.p)))); + EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p)))); + + list->OMSetRenderTargets(1, &rtv, FALSE, nullptr); + const D3D12_VIEWPORT viewport{0, 0, float(width), float(height), 0, 1}; + const D3D12_RECT scissor{0, 0, LONG(width), LONG(height)}; + list->RSSetViewports(1, &viewport); + list->RSSetScissorRects(1, &scissor); + list->SetGraphicsRootSignature(root.p); + list->SetPipelineState(pso.p); + list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + const D3D12_STREAM_OUTPUT_BUFFER_VIEW so_view{so_buffer->GetGPUVirtualAddress(), so_bytes, 0}; + list->SOSetTargets(0, 1, &so_view); + list->DrawInstanced(3, 1, 0, 0); + const auto to_copy_src = transition(so_buffer.p, D3D12_RESOURCE_STATE_STREAM_OUT, D3D12_RESOURCE_STATE_COPY_SOURCE); + list->ResourceBarrier(1, &to_copy_src); + list->CopyBufferRegion(so_readback.p, 0, so_buffer.p, 0, so_bytes); + EXPECT(SUCCEEDED(list->Close())); + + ID3D12CommandList* lists[] = {list.p}; + queue->ExecuteCommandLists(1, lists); + EXPECT(SUCCEEDED(queue->Signal(fence.p, 1))); + const HANDLE ev = CreateEventW(nullptr, FALSE, FALSE, nullptr); + EXPECT(ev != nullptr); + const bool done = SUCCEEDED(fence->SetEventOnCompletion(1, ev)) && WaitForSingleObject(ev, 20000) == WAIT_OBJECT_0; + CloseHandle(ev); + EXPECT(done); + + void* mapped = nullptr; + EXPECT(SUCCEEDED(so_readback->Map(0, nullptr, &mapped)) && mapped); + const auto* floats = static_cast(mapped); + int mismatches = 0; + for (int vertex = 0; vertex < 3; ++vertex) { + const float expected[4] = {1.0f, 2.0f, 3.0f, 4.0f}; + for (int component = 0; component < 4; ++component) { + const float got = floats[vertex * 4 + component]; + if (got != expected[component]) { + std::fprintf(stderr, "vertex %d component %d: got %f, expected %f\n", vertex, component, + double(got), double(expected[component])); + ++mismatches; + } + } + } + const D3D12_RANGE no_write{0, 0}; + so_readback->Unmap(0, &no_write); + EXPECT(mismatches == 0); + EXPECT(vk.validation_errors() == 0); + + std::printf("stream_output: 3 vertices captured through SOSetTargets + a StreamOutput PSO, %d mismatches\n", + mismatches); + std::filesystem::remove_all(fixture); + return 0; +} diff --git a/runtime/test/glsl/ps_stream_output.frag b/runtime/test/glsl/ps_stream_output.frag new file mode 100644 index 0000000000..761c7f47e0 --- /dev/null +++ b/runtime/test/glsl/ps_stream_output.frag @@ -0,0 +1,9 @@ +#version 460 +// Trivial pixel shader for d3d12_stream_output_test (D3D-302): a render target is still needed so the draw has a +// valid area/layer count to rasterize into, but its colour is never checked. + +layout(location = 0) out vec4 target; + +void main() { + target = vec4(1.0, 0.0, 0.0, 1.0); +} diff --git a/runtime/test/glsl/vs_stream_output.vert b/runtime/test/glsl/vs_stream_output.vert new file mode 100644 index 0000000000..0c80b98c86 --- /dev/null +++ b/runtime/test/glsl/vs_stream_output.vert @@ -0,0 +1,11 @@ +#version 460 +// Minimal vertex shader for d3d12_stream_output_test (D3D-302): writes the fixed vec4 (1, 2, 3, 4) to both its +// Xfb-captured output (buffer 0, offset 0, stride 16 bytes: one vec4) and gl_Position. The rasterized output is +// unused by the test; only the captured buffer's contents, read back after the draw, are checked. + +layout(location = 0, xfb_buffer = 0, xfb_offset = 0, xfb_stride = 16) out vec4 out_val; + +void main() { + out_val = vec4(1.0, 2.0, 3.0, 4.0); + gl_Position = out_val; +} diff --git a/runtime/test/vk_pipeline_check.cpp b/runtime/test/vk_pipeline_check.cpp index 0e39d0f621..26eb5030cf 100644 --- a/runtime/test/vk_pipeline_check.cpp +++ b/runtime/test/vk_pipeline_check.cpp @@ -7,10 +7,10 @@ // descriptor-free shaders with the standard push-constant range // - ps/vs/ms/gs: graphics pipeline via vkCreateGraphicsPipelines (Vulkan 1.3 dynamic rendering, no render pass) // paired with minimal partner stubs from /stubs/ (generated by tools/spirv_stubs.py) -// VK_EXT_mesh_shader (with meshShaderQueries) and VK_KHR_maintenance8 are always enabled when supported so the -// saved pipeline cache matches the runtime's device key; other optional device extensions are enabled only when -// needed by the selected shaders. Shaders whose required features or partner stubs are unavailable are reported as -// skipped rather than failed. +// VK_EXT_mesh_shader (with meshShaderQueries), VK_KHR_maintenance8 and VK_EXT_transform_feedback are always enabled +// when supported so the saved pipeline cache matches the runtime's device key; other optional device extensions are +// enabled only when needed by the selected shaders. Shaders whose required features or partner stubs are unavailable +// are reported as skipped rather than failed. // [cache.bin] loaded first if it exists (a stopped run resumes: cached pipelines are instant), saved every // 10 minutes and at the end // --jobs N compile on up to N worker threads (default: half the hardware threads) @@ -1122,6 +1122,7 @@ int main(int argc, char** argv) { // Query the core and optional device features used by the checker. VkPhysicalDeviceMeshShaderFeaturesEXT mesh{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_FEATURES_EXT}; + VkPhysicalDeviceTransformFeedbackFeaturesEXT tf{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT}; VkPhysicalDeviceFragmentShaderBarycentricFeaturesKHR bary{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADER_BARYCENTRIC_FEATURES_KHR}; VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT lock{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADER_INTERLOCK_FEATURES_EXT}; #ifdef VK_KHR_MAINTENANCE_8_EXTENSION_NAME @@ -1144,6 +1145,10 @@ int main(int argc, char** argv) { link(&f12, reinterpret_cast(&f12), nullptr, true); link(&f13, reinterpret_cast(&f13), nullptr, true); const bool ext_mesh = link(&mesh, reinterpret_cast(&mesh), VK_EXT_MESH_SHADER_EXTENSION_NAME, true); + // The runtime's device enables VK_EXT_transform_feedback (transformFeedback, and geometryStreams when the device + // offers it) whenever the extension is present (D3D-302), and a driver makes it part of a pipeline's cache key, so + // the checker enables it too (docs/pipeline_check.md, "The cache and the runtime's device"). + link(&tf, reinterpret_cast(&tf), VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME, true); const bool ext_bary = link(&bary, reinterpret_cast(&bary), VK_KHR_FRAGMENT_SHADER_BARYCENTRIC_EXTENSION_NAME, want_bary); const bool ext_lock = link(&lock, reinterpret_cast(&lock), VK_EXT_FRAGMENT_SHADER_INTERLOCK_EXTENSION_NAME, want_lock); #ifdef VK_KHR_MAINTENANCE_8_EXTENSION_NAME diff --git a/runtime/test/vk_test_device.h b/runtime/test/vk_test_device.h index fd63fca665..6145673c9c 100644 --- a/runtime/test/vk_test_device.h +++ b/runtime/test/vk_test_device.h @@ -136,6 +136,12 @@ class Device { bool request_mesh_shader = false; // set before create(): enable VK_EXT_mesh_shader (meshShader) where available bool mesh_shader = false; // VK_EXT_mesh_shader and meshShader are enabled bool mesh_shader_queries = false; // the meshShaderQueries feature is enabled on the device + // set before create(): enable VK_EXT_transform_feedback (D3D-302) where available + bool request_transform_feedback = false; + bool transform_feedback = false; // VK_EXT_transform_feedback and transformFeedback are enabled + bool transform_feedback_geometry_streams = false; // the geometryStreams feature is enabled on the device + std::uint32_t max_transform_feedback_buffers = 0; // VkPhysicalDeviceTransformFeedbackPropertiesEXT; 0: disabled + std::uint32_t max_transform_feedback_streams = 0; bool debug_utils = false; // VK_EXT_debug_utils is enabled on the instance // The subgroup sizes of the device, with subgroupSizeControl and computeFullSubgroups enabled where it has them. leonida::SubgroupSizeCaps subgroup; @@ -634,6 +640,32 @@ class Device { mesh_features.meshShaderQueries = mesh_shader_queries ? VK_TRUE : VK_FALSE; *v13_.next() = &mesh_features; } + // VK_EXT_transform_feedback (D3D-302), for the tests that ask for it. + VkPhysicalDeviceTransformFeedbackFeaturesEXT transform_feedback_features{ + VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT}; + if (request_transform_feedback && std::any_of(extensions.begin(), extensions.end(), [](const auto& e) { + return std::strcmp(e.extensionName, VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME) == 0; + })) { + VkPhysicalDeviceFeatures2 query{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &transform_feedback_features}; + instance_fn("vkGetPhysicalDeviceFeatures2")(physical, &query); + transform_feedback = transform_feedback_features.transformFeedback == VK_TRUE; + transform_feedback_geometry_streams = transform_feedback_features.geometryStreams == VK_TRUE; + if (transform_feedback) { + VkPhysicalDeviceTransformFeedbackPropertiesEXT tf_props{ + VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT}; + VkPhysicalDeviceProperties2 props{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &tf_props}; + instance_fn("vkGetPhysicalDeviceProperties2")(physical, &props); + max_transform_feedback_buffers = tf_props.maxTransformFeedbackBuffers; + max_transform_feedback_streams = tf_props.maxTransformFeedbackStreams; + } + } + if (transform_feedback) { + transform_feedback_features = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT, + *v13_.next()}; + transform_feedback_features.transformFeedback = VK_TRUE; + transform_feedback_features.geometryStreams = transform_feedback_geometry_streams ? VK_TRUE : VK_FALSE; + *v13_.next() = &transform_feedback_features; + } enable.occlusionQueryPrecise = features.features.occlusionQueryPrecise; // query heaps, where available enable.pipelineStatisticsQuery = features.features.pipelineStatisticsQuery; occlusion_query_precise = features.features.occlusionQueryPrecise == VK_TRUE; @@ -767,6 +799,7 @@ class Device { if (custom_border_color) device_extensions.push_back(custom_border_color_name); if (null_descriptor || robust_index_view) device_extensions.push_back(VK_EXT_ROBUSTNESS_2_EXTENSION_NAME); if (mesh_shader) device_extensions.push_back(VK_EXT_MESH_SHADER_EXTENSION_NAME); + if (transform_feedback) device_extensions.push_back(VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME); device_info.enabledExtensionCount = std::uint32_t(device_extensions.size()); device_info.ppEnabledExtensionNames = device_extensions.data(); result = instance_fn("vkCreateDevice")(physical, &device_info, nullptr, &device);