Skip to content
Open
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 @@ -2887,6 +2887,33 @@ if(TARGET Vulkan::Headers)
add_test(NAME d3d12_pso_validation COMMAND d3d12_pso_validation_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 @@ -399,6 +399,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 @@ -1263,6 +1279,7 @@ class GraphicsCommandList final
D3D12_GPU_VIRTUAL_ADDRESS sampler_base_ = 0;
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_{};
leonida::Viewport viewport_{};
leonida::Rect scissor_{};
Expand Down Expand Up @@ -1365,6 +1382,7 @@ class GraphicsCommandList final
sampler_base_ = 0;
topology_ = 0;
vertex_buffers_ = {};
so_buffers_ = {};
index_ = {};
viewport_ = {};
scissor_ = {};
Expand All @@ -1384,6 +1402,14 @@ class GraphicsCommandList final
: leonida::VertexBufferView{};
}

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) {
index_ = view ? leonida::IndexBufferView{view->BufferLocation, view->SizeInBytes, u(view->Format)}
: leonida::IndexBufferView{};
Expand All @@ -1401,6 +1427,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
12 changes: 12 additions & 0 deletions runtime/shims/d3d12_com.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,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 enabled on `device` (D3D-302): a graphics PSO
// that declares StreamOutput is accepted, SOSetTargets binds real buffers, and draws between them are captured
// with vkCmdBeginTransformFeedbackEXT/EndTransformFeedbackEXT. Without it, StreamOutput PSOs stay refused.
bool transform_feedback = false;
// VK_EXT_transform_feedback's geometryStreams feature is enabled: a StreamOutput RasterizedStream other than the
// vertex/geometry shader's stream 0 can be rasterized. Without it, only RasterizedStream 0 is accepted.
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{};
Expand Down
66 changes: 60 additions & 6 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3448,10 +3448,28 @@ 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) {
log_once("graphics pipeline stage not supported yet (hull/domain/stream output):", 0);
if (desc.HS.BytecodeLength || desc.DS.BytecodeLength) {
log_once("graphics pipeline stage not supported yet (hull/domain):", 0);
return E_NOTIMPL;
}
// 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
// be its input primitive, as D3D12 requires.
Expand Down Expand Up @@ -3936,6 +3954,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);
Expand Down Expand Up @@ -4943,10 +4964,12 @@ class QueryHeap final
info.queryType = VK_QUERY_TYPE_TIMESTAMP;
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) {
Expand Down Expand Up @@ -9476,6 +9499,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);
Expand All @@ -9500,10 +9525,27 @@ 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: StreamOutput PSOs are unsupported\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};
Expand Down Expand Up @@ -9539,11 +9581,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;
Expand Down Expand Up @@ -9932,6 +9985,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);
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 @@ -405,12 +405,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
7 changes: 6 additions & 1 deletion runtime/src/vulkan_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ struct VulkanDeviceFunctions {
X(vkCreatePipelineCache) X(vkDestroyPipelineCache) \
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
Expand Down Expand Up @@ -994,6 +995,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;
Expand Down
Loading