Skip to content
4 changes: 3 additions & 1 deletion runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4155,7 +4155,9 @@ if(TARGET Vulkan::Headers)
LEONIDA_TEST_VS_SPV="${LEONIDA_TEST_VS}" LEONIDA_TEST_PS_SPV="${LEONIDA_TEST_PS}")
target_compile_options(d3d12_msaa_draw_test PRIVATE /W4 /permissive- /utf-8)
add_test(NAME d3d12_msaa_draw COMMAND d3d12_msaa_draw_test)
set_tests_properties(d3d12_msaa_draw PROPERTIES SKIP_RETURN_CODE 77)
add_test(NAME d3d12_custom_samples COMMAND d3d12_msaa_draw_test --custom-samples)
add_test(NAME d3d12_sample_grid_reset COMMAND d3d12_msaa_draw_test --sample-grid)
set_tests_properties(d3d12_msaa_draw d3d12_custom_samples d3d12_sample_grid_reset PROPERTIES SKIP_RETURN_CODE 77)
# Geometry shaders: a VS+GS+PS pipeline whose GS emits each triangle twice.
add_executable(d3d12_geometry_test test/d3d12_geometry_test.cpp shims/d3d12_xs_device.cpp shims/d3d12_xs_pso_stream.cpp
${LEONIDA_TEST_VS} ${LEONIDA_TEST_GS_DOUBLE} ${LEONIDA_TEST_PS})
Expand Down
21 changes: 21 additions & 0 deletions runtime/shims/d3d12_com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ class GraphicsCommandList final
if (list_.closed()) return;
if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
if (!list_.closed()) check(list_.set_predication({0, 0, 0}));
if (custom_samples_) check(list_.set_sample_positions({}));
reset_state(state);
}

Expand Down Expand Up @@ -804,6 +805,24 @@ class GraphicsCommandList final
check(list_.resolve_subresource({resource_id(dst), dst_subresource, resource_id(src), src_subresource}));
}

void STDMETHODCALLTYPE SetSamplePositions(UINT samples, UINT pixels, D3D12_SAMPLE_POSITION* positions) override {
d3d12_trace::record("ID3D12GraphicsCommandList1::SetSamplePositions");
if (list_.closed() || type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return;
leonida::SamplePositions pattern{};
if (samples || pixels || positions) {
if (!positions || !leonida::SamplePositions::valid_grid(samples, pixels)) return;
pattern.samples = samples;
pattern.pixels = pixels;
for (UINT i = 0; i < samples * pixels; ++i) {
const auto p = positions[i];
if (p.X < -8 || p.X > 7 || p.Y < -8 || p.Y > 7) return;
pattern.positions[i] = {p.X, p.Y};
}
}
check(list_.set_sample_positions(pattern));
custom_samples_ = samples != 0;
}

void STDMETHODCALLTYPE ResolveSubresourceRegion(ID3D12Resource* dst, UINT dst_subresource, UINT x, UINT y,
ID3D12Resource* src, UINT src_subresource, D3D12_RECT* rect,
DXGI_FORMAT format, D3D12_RESOLVE_MODE mode) override {
Expand Down Expand Up @@ -1577,6 +1596,7 @@ class GraphicsCommandList final
leonida::CommandList list_;
bool invalid_ = false;
UINT front_stencil_ref_ = 0;
bool custom_samples_ = false;
UINT back_stencil_ref_ = 0;
RootState compute_;
RootState graphics_;
Expand Down Expand Up @@ -1794,6 +1814,7 @@ class GraphicsCommandList final

void reset_state(ID3D12PipelineState* state) {
front_stencil_ref_ = back_stencil_ref_ = 0;
custom_samples_ = false;
compute_ = {};
graphics_ = {};
cbv_srv_uav_heap_ = nullptr;
Expand Down
1 change: 1 addition & 0 deletions runtime/shims/d3d12_com.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ struct VulkanDeviceInfo {
// VK_EXT_conservative_rasterization is enabled on the logical device. Tier 1/2 depend on the physical
// precision/overestimation properties; Tier 2 also requires post-snap degenerate triangles.
bool conservative_rasterization = false;
bool sample_locations = false;
// Opt in only for shader libraries whose InnerCoverage input contract uses FullyCoveredEXT. The device
// still checks physical Tier 2 and fullyCoveredFragmentShaderInputVariable properties. The standalone
// device leaves this false until the Xbox PS input mapping is confirmed (synthetic GLSL is not that ABI).
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 @@ -2511,7 +2511,8 @@ struct TextureCreateInfo {
VkImageFormatListCreateInfo format_list{VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO};
std::array<VkFormat, 2> view_formats{};

TextureCreateInfo(const D3D12_RESOURCE_DESC& desc, const Format& format, bool attachment_usable) {
TextureCreateInfo(const D3D12_RESOURCE_DESC& desc, const Format& format, bool attachment_usable,
VkSampleCountFlags programmable_samples) {
const bool volume = desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D;
const bool one_d = desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE1D;
info.flags = format.typeless ? VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT : 0;
Expand All @@ -2524,6 +2525,9 @@ struct TextureCreateInfo {
info.mipLevels = desc.MipLevels;
info.arrayLayers = volume ? 1 : desc.DepthOrArraySize;
info.samples = VkSampleCountFlagBits(std::max(1u, desc.SampleDesc.Count));
if ((programmable_samples & info.samples) && (format.aspects & VK_IMAGE_ASPECT_DEPTH_BIT) &&
(desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL))
info.flags |= VK_IMAGE_CREATE_SAMPLE_LOCATIONS_COMPATIBLE_DEPTH_BIT_EXT;
info.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
if (desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET) info.usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
if (desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL) info.usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
Expand Down Expand Up @@ -3003,6 +3007,8 @@ struct DeviceState {
std::uint32_t f16_half_top_location = 29;
float max_sampler_lod_bias = 0;
D3D12_CONSERVATIVE_RASTERIZATION_TIER conservative_raster_tier = D3D12_CONSERVATIVE_RASTERIZATION_TIER_NOT_SUPPORTED;
VkSampleCountFlags sample_grid_1 = 0, sample_grid_4 = 0;
D3D12_PROGRAMMABLE_SAMPLE_POSITIONS_TIER sample_positions_tier = D3D12_PROGRAMMABLE_SAMPLE_POSITIONS_TIER_NOT_SUPPORTED;
bool conservative_post_depth_coverage = false;
// VkPhysicalDeviceDepthStencilResolveProperties: the modes a depth or stencil ResolveSubresourceRegion can use.
VkResolveModeFlags depth_resolve_modes = 0;
Expand Down Expand Up @@ -9114,7 +9120,7 @@ class Resource final
HRESULT create_sparse_image() {
auto& vk = device_.context->vk;
const auto format = device_.texture_format(desc_);
TextureCreateInfo create(desc_, format, false);
TextureCreateInfo create(desc_, format, false, device_.sample_grid_1);
create.info.flags |= VK_IMAGE_CREATE_SPARSE_BINDING_BIT | VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT |
VK_IMAGE_CREATE_SPARSE_ALIASED_BIT;
HRESULT support = device_.check_image_support(create.info);
Expand Down Expand Up @@ -9220,7 +9226,7 @@ class Resource final
!desc_.DepthOrArraySize || desc_.DepthOrArraySize > slice_limit || desc_.MipLevels > mips)
return E_INVALIDARG;
}
TextureCreateInfo create(desc_, format, device_.color_attachment_usable(format.format));
TextureCreateInfo create(desc_, format, device_.color_attachment_usable(format.format), device_.sample_grid_1);
VkImageCreateInfo& info = create.info;
if (!castable.empty()) {
// D3D12 always allows a view in the resource's own format, whether or not the castable list names
Expand Down Expand Up @@ -9488,6 +9494,7 @@ class Resource final
binding.volume = volume;
binding.line = one_d;
binding.samples = samples;
binding.sample_locations_depth = (info.flags & VK_IMAGE_CREATE_SAMPLE_LOCATIONS_COMPATIBLE_DEPTH_BIT_EXT) != 0;
binding.color_attachment = (info.usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) != 0;
binding.sampled = (info.usage & VK_IMAGE_USAGE_SAMPLED_BIT) != 0;
binding.simultaneous_access = (desc_.Flags & D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS) != 0;
Expand Down Expand Up @@ -12476,6 +12483,8 @@ class PipelineState final
recipe_.conservative = r.ConservativeRaster == D3D12_CONSERVATIVE_RASTERIZATION_MODE_ON;
recipe_.multisample = multisample;
graphics_.samples = samples;
graphics_.sample_grid_1 = device_.sample_grid_1;
graphics_.sample_grid_4 = device_.sample_grid_4;
recipe_.sample_mask = sample_mask;
recipe_.depth = depth;
recipe_.blends = std::move(blends);
Expand Down Expand Up @@ -13046,7 +13055,7 @@ class PipelineState final
// A mesh pipeline takes neither the topology nor the strides as dynamic state
// (VUID-VkGraphicsPipelineCreateInfo-pDynamicStates-07065 and -07066).
// With multiViewport enabled, VIEWPORT_WITH_COUNT / SCISSOR_WITH_COUNT allow 1..16 dynamic viewports/scissors.
VkDynamicState dyn_list[8]{};
VkDynamicState dyn_list[9]{};
std::uint32_t dyn_count = 0;
dyn_list[dyn_count++] =
graphics_.viewport_with_count ? VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT : VK_DYNAMIC_STATE_VIEWPORT;
Expand All @@ -13064,6 +13073,15 @@ class PipelineState final
dynamic.dynamicStateCount = dyn_count;
dynamic.pDynamicStates = dyn_list;

VkPipelineSampleLocationsStateCreateInfoEXT custom{VK_STRUCTURE_TYPE_PIPELINE_SAMPLE_LOCATIONS_STATE_CREATE_INFO_EXT};
if (unbound & leonida::GraphicsPipeline::custom_samples) {
dyn_list[dyn_count++] = VK_DYNAMIC_STATE_SAMPLE_LOCATIONS_EXT;
dynamic.dynamicStateCount = dyn_count;
custom.sampleLocationsEnable = VK_TRUE;
custom.sampleLocationsInfo.sType = VK_STRUCTURE_TYPE_SAMPLE_LOCATIONS_INFO_EXT;
custom.pNext = multisample.pNext;
multisample.pNext = &custom;
}
// A lazy pipeline's stages get their modules here, for this one call (Vulkan lets a module go once the
// pipeline exists), so the driver holds no SPIR-V for pipelines that are not drawn.
auto bytes = stage_bytes_;
Expand Down Expand Up @@ -14714,6 +14732,38 @@ class Device final
if (info.conservative_rasterization) resolve.pNext = &conservative;
VkPhysicalDeviceProperties2 props{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &resolve};
get_props2(info.physical, &props);
if (info.sample_locations) {
VkPhysicalDeviceSampleLocationsPropertiesEXT locations{
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLE_LOCATIONS_PROPERTIES_EXT};
VkPhysicalDeviceProperties2 sample_props{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &locations};
get_props2(info.physical, &sample_props);
const auto get_grid = reinterpret_cast<PFN_vkGetPhysicalDeviceMultisamplePropertiesEXT>(
info.get_instance_proc(info.instance, "vkGetPhysicalDeviceMultisamplePropertiesEXT"));
if (get_grid && props.properties.limits.standardSampleLocations && locations.variableSampleLocations && locations.sampleLocationSubPixelBits >= 4 &&
locations.sampleLocationCoordinateRange[0] <= 0 && locations.sampleLocationCoordinateRange[1] >= 15.f / 16) {
for (unsigned samples = 1; samples <= 16; samples *= 2) {
if (!(locations.sampleLocationSampleCounts & samples)) continue;
VkMultisamplePropertiesEXT grid{VK_STRUCTURE_TYPE_MULTISAMPLE_PROPERTIES_EXT};
get_grid(info.physical, VkSampleCountFlagBits(samples), &grid);
const auto extent = grid.maxSampleLocationGridSize;
if (extent.width && extent.height) state_.sample_grid_1 |= samples;
if (samples <= 4 && extent.width >= 2 && extent.height >= 2 &&
extent.width % 2 == 0 && extent.height % 2 == 0) state_.sample_grid_4 |= samples;
}
const auto counts = (props.properties.limits.framebufferColorSampleCounts |
props.properties.limits.framebufferDepthSampleCounts |
props.properties.limits.framebufferStencilSampleCounts |
props.properties.limits.framebufferNoAttachmentsSampleCounts) & 31u;
// Four repeating pixels can name at most sixteen sample locations altogether.
const auto grid_counts = counts & (VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT);
const auto tier1_counts = counts & (VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT | VK_SAMPLE_COUNT_8_BIT);
if (counts && (state_.sample_grid_1 & counts) == counts &&
(state_.sample_grid_4 & grid_counts) == grid_counts)
state_.sample_positions_tier = D3D12_PROGRAMMABLE_SAMPLE_POSITIONS_TIER_2;
else if (tier1_counts && (state_.sample_grid_1 & tier1_counts) == tier1_counts)
state_.sample_positions_tier = D3D12_PROGRAMMABLE_SAMPLE_POSITIONS_TIER_1;
}
}
state_.conservative_raster_tier = D3D12_CONSERVATIVE_RASTERIZATION_TIER(leonida::conservative_raster::tier(
info.conservative_rasterization, props.properties.limits.subPixelPrecisionBits,
conservative.primitiveOverestimationSize, conservative.degenerateTrianglesRasterized == VK_TRUE,
Expand Down Expand Up @@ -19733,7 +19783,7 @@ class Device final
auto* opts = static_cast<D3D12_FEATURE_DATA_D3D12_OPTIONS2*>(data);
*opts = {};
opts->DepthBoundsTestSupported = state_.info.features.depthBounds ? TRUE : FALSE;
opts->ProgrammableSamplePositionsTier = D3D12_PROGRAMMABLE_SAMPLE_POSITIONS_TIER_NOT_SUPPORTED;
opts->ProgrammableSamplePositionsTier = state_.sample_positions_tier;
return d3d12_trace::result(method, S_OK);
}
case D3D12_FEATURE_D3D12_OPTIONS3: {
Expand Down Expand Up @@ -20514,7 +20564,7 @@ class Device final
return kInvalidAllocation;
}
VkMemoryRequirements need{};
const TextureCreateInfo create(resolved, format, state_.color_attachment_usable(format.format));
const TextureCreateInfo create(resolved, format, state_.color_attachment_usable(format.format), state_.sample_grid_1);
if (!state_.memory_requirements(create.info, need)) return kInvalidAllocation;
if (tiled) {
leonida::ResourceTiling tiling;
Expand Down Expand Up @@ -22287,6 +22337,9 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin
info.conservative_rasterization = std::any_of(extensions.begin(), extensions.end(), [](const auto& e) {
return std::strcmp(e.extensionName, VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME) == 0;
});
info.sample_locations = std::any_of(extensions.begin(), extensions.end(), [](const auto& e) {
return std::strcmp(e.extensionName, VK_EXT_SAMPLE_LOCATIONS_EXTENSION_NAME) == 0;
});
const bool has_host_image_copy = std::any_of(extensions.begin(), extensions.end(), [](const auto& e) {
return std::strcmp(e.extensionName, VK_EXT_HOST_IMAGE_COPY_EXTENSION_NAME) == 0;
});
Expand Down Expand Up @@ -22617,6 +22670,7 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin
if (trace_pipeline_compile() && !v13.core && info.pipeline_creation_feedback)
device_extensions.push_back(VK_EXT_PIPELINE_CREATION_FEEDBACK_EXTENSION_NAME);
if (info.conservative_rasterization) device_extensions.push_back(VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME);
if (info.sample_locations) device_extensions.push_back(VK_EXT_SAMPLE_LOCATIONS_EXTENSION_NAME);
if (info.image_view_min_lod) device_extensions.push_back(VK_EXT_IMAGE_VIEW_MIN_LOD_EXTENSION_NAME);
if (info.vertex_attribute_divisor) device_extensions.push_back(divisor_extension);
if (info.conditional_rendering) device_extensions.push_back(conditional_extension);
Expand Down
18 changes: 17 additions & 1 deletion runtime/src/command_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,20 @@ struct Predication {
std::uint32_t op; // d3d12_predication
};

struct SamplePosition {
std::int8_t x, y;
};

struct SamplePositions {
std::uint32_t samples = 0;
std::uint32_t pixels = 0;
std::array<SamplePosition, 16> positions{};

static constexpr bool valid_grid(std::uint32_t samples, std::uint32_t pixels) {
return (pixels == 1 || pixels == 4) && samples && !(samples & (samples - 1)) && samples <= 16 / pixels;
}
};

// Query heaps are identified like resources, by the COM object's pointer value. A heap's type and size are checked
// at replay, when the heap is known.
using QueryHeapId = std::uintptr_t;
Expand Down Expand Up @@ -705,7 +719,7 @@ struct AccelerationStructurePostbuild {

using Command = std::variant<TransitionBarrier, UavBarrier, AliasingBarrier, GlobalBarrier, BufferBarrier,
TextureBarrier, ClearRenderTarget, ClearDepthStencil, CopyBuffer, CopyTexture,
CopyResource, ResolveSubresource, Predication, ClearUnorderedAccess, DiscardResource,
CopyResource, ResolveSubresource, Predication, SamplePositions, ClearUnorderedAccess, DiscardResource,
WriteImmediate, label::Write, AtomicCopyBuffer, BeginQuery, EndQuery, ResolveQueries, DebugMarker,
ExecuteIndirect, BuildAccelerationStructure, Dispatch, Draw, CopyAccelerationStructure,
AccelerationStructurePostbuild, IndirectRasterDraw>;
Expand Down Expand Up @@ -900,6 +914,8 @@ class CommandList {
return true;
}

bool set_sample_positions(const SamplePositions& pattern) { return append(pattern); }

// SetPredication. Turning it off ignores the offset and op, as D3D12 does.
bool set_predication(const Predication& predication) {
if (!predication.buffer) return append(Predication{0, 0, 0});
Expand Down
5 changes: 3 additions & 2 deletions runtime/src/vulkan_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ struct VulkanDeviceFunctions {
X(vkCmdPushConstants) X(vkCmdDispatch) \
X(vkCmdBindVertexBuffers2) X(vkCmdBindIndexBuffer) X(vkCmdSetViewport) X(vkCmdSetScissor) \
X(vkCmdSetViewportWithCount) X(vkCmdSetScissorWithCount) \
X(vkCmdSetPrimitiveTopology) X(vkCmdSetPrimitiveRestartEnable) X(vkCmdSetStencilReference) X(vkCmdSetDepthBounds) \
X(vkCmdSetBlendConstants) X(vkCmdDraw) \
X(vkCmdSetPrimitiveTopology) X(vkCmdSetPrimitiveRestartEnable) X(vkCmdSetStencilReference) \
X(vkCmdSetDepthBounds) X(vkCmdSetBlendConstants) X(vkCmdDraw) X(vkCmdSetSampleLocationsEXT) \
X(vkCmdDrawIndexed) X(vkCreateGraphicsPipelines) X(vkQueueBindSparse) X(vkGetImageSparseMemoryRequirements) \
X(vkCreatePipelineCache) X(vkDestroyPipelineCache) X(vkGetPipelineCacheData) X(vkMergePipelineCaches) \
X(vkCreateEvent) X(vkDestroyEvent) X(vkResetEvent) X(vkCmdSetEvent) X(vkCmdWaitEvents) X(vkCmdDrawMeshTasksEXT) X(vkCmdDrawMeshTasksIndirectEXT) X(vkCmdDrawMeshTasksIndirectCountEXT) \
Expand Down Expand Up @@ -1191,6 +1191,7 @@ class VulkanContext {
replay_.set_primitive_topology = vk.vkCmdSetPrimitiveTopology;
replay_.set_primitive_restart_enable = vk.vkCmdSetPrimitiveRestartEnable;
replay_.set_stencil_reference = vk.vkCmdSetStencilReference;
replay_.set_sample_locations = vk.vkCmdSetSampleLocationsEXT;
replay_.set_blend_constants = vk.vkCmdSetBlendConstants;
replay_.set_depth_bounds = vk.vkCmdSetDepthBounds;
replay_.draw = vk.vkCmdDraw;
Expand Down
Loading
Loading