diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs index 45f448eab..d943c099e 100644 --- a/crates/metal2vulkan-runtime/src/render.rs +++ b/crates/metal2vulkan-runtime/src/render.rs @@ -1044,6 +1044,22 @@ fn region_batch(variant: &RenderVariant, device: &Device) -> Result( + region_fan_out: Option<(RegionFanOut, u32)>, + settled: &Result, +) -> bool { + matches!( + (region_fan_out, settled), + (Some(_), Err(Error::Unsupported(refused))) + if refused.feature == ErrorFeature::RasterizationRateMap + ) +} + /// The states a draw of `state` records a rate-mapped pass's `regions` with /// (`rate_mapped_pass.rs`), each region's viewport taking the depth range `depth` where the draw /// compensates it: one a region, or, `batch` the most one multi-draw takes @@ -9094,15 +9110,11 @@ impl RenderLowering { // state compiled for its first band: the draw takes the nearest width, routed again, // as the state's later draws of the primitive do (`render/line_width.rs`). Routed // again, it draws no band of the primitive, so it settles once more at most. - // A fan-out variant the translator refuses for the device's limits - // (`pipeline::link::fan_out`): the draw is drawn region by region, as every draw is - // under `RateMapRegionPolicy::Exact` (`render/fan_out.rs`). So it settles once more - // at most. - if let (Some(_), Err(Error::Unsupported(refused))) = (routed.region_fan_out, &settled) { - if refused.feature == ErrorFeature::RasterizationRateMap { - routed.region_fan_out = None; - continue; - } + // A fan-out variant the translator refuses: the draw is drawn region by region + // (`fan_out_refused`). So it settles once more at most. + if fan_out_refused(routed.region_fan_out, &settled) { + routed.region_fan_out = None; + continue; } let refused = settled .as_ref() diff --git a/crates/metal2vulkan-runtime/src/render/executions.rs b/crates/metal2vulkan-runtime/src/render/executions.rs index 7fb2bf8e9..1f2971f0d 100644 --- a/crates/metal2vulkan-runtime/src/render/executions.rs +++ b/crates/metal2vulkan-runtime/src/render/executions.rs @@ -2135,7 +2135,8 @@ impl RenderLowering { /// settle with the bind state `live`. Under the encoder's fan-out (`render/fan_out.rs`), the /// probe's or the fanned-out draw's variant of each draw it takes (a command the step boxes /// makes its region-copy variant there), none of another; outside it, a draw's region-copy - /// variant where the device fans out. + /// variant where the device fans out, or its variant region by region where the translator + /// refuses that one. fn settle_draws( &mut self, live: &mut LiveBind, @@ -2193,7 +2194,9 @@ impl RenderLowering { /// own, or places its colors elsewhere), or a command's own vertex strides key the pipeline /// (a layout of per-bind strides that are not dynamic state). Under the encoder's fan-out, /// its variant of the draw, `None` where the fan-out does not take the draw - /// (`fan_out_asked`). + /// (`fan_out_asked`, or a fan-out variant the translator refuses); outside it, the variant + /// region by region where the translator refuses the region-copy variant, as the step draws + /// it (`fan_out_refused`). fn settle_draw_of( &mut self, live: &mut LiveBind, @@ -2215,28 +2218,41 @@ impl RenderLowering { // Outside the encoder's fan-out, the region-copy variant the step records where the device // fans out (`render/fan_out.rs`, **Region-copy variant**), else its region by region // variant. - let region_fan_out = match self.fan_out { + let mut region_fan_out = match self.fan_out { None => self.fan_out_asked(state, &settings, assembly, None)?, Some(_) => match self.fan_out_asked(state, &settings, assembly, None)? { Some(fan) => Some(fan), None => return Ok(None), }, }; - let settled = self.settle_draw( - live, - ( - state, - &settings, - None, - PrivateForm::default(), - ProvokingForms::default(), - &BTreeSet::new(), - (region_viewports, region_fan_out), - ), - (&pass_start, &sampler_maps), - (&mut argument_samplers, &mut None), - (&function_tables, &mut None), - )?; + let settled = loop { + let settled = self.settle_draw( + live, + ( + state, + &settings, + None, + PrivateForm::default(), + ProvokingForms::default(), + &BTreeSet::new(), + (region_viewports, region_fan_out), + ), + (&pass_start, &sampler_maps), + (&mut argument_samplers, &mut None), + (&function_tables, &mut None), + ); + // A fan-out variant the translator refuses: the step draws the draw region by region + // (`fan_out_refused`), the variant settled outside the encoder's fan-out, which does + // not take the draw. So it settles once more at most. + if super::fan_out_refused(region_fan_out, &settled) { + if self.fan_out.is_some() { + return Ok(None); + } + region_fan_out = None; + continue; + } + break settled?; + }; let (Settled { variant, plan, .. }, _) = settled; let device = self.core.device(); let stages = &variant.pipeline().stages; diff --git a/crates/metal2vulkan-runtime/src/tests/rate_mapped_fan_out.rs b/crates/metal2vulkan-runtime/src/tests/rate_mapped_fan_out.rs index c41713b27..9d0720cad 100644 --- a/crates/metal2vulkan-runtime/src/tests/rate_mapped_fan_out.rs +++ b/crates/metal2vulkan-runtime/src/tests/rate_mapped_fan_out.rs @@ -113,6 +113,13 @@ fn draw_of( impl Workload { /// The workload of `commands` commands on `device`. fn new(device: Device, commands: u32) -> Workload { + Workload::placed_by(device, commands, ("engine/tob_renders.ll", "tob_triangle")) + } + + /// The workload of `commands` commands on `device`, whose commands but the one across `w = 0` + /// place their vertices with the vertex function `name` of the fixture `fixture`, which places + /// each where `tob_triangle` does. + fn placed_by(device: Device, commands: u32, (fixture, name): (&str, &str)) -> Workload { let renders = library(&device, "engine/tob_renders.ll"); let function = |name| { StageSource::Function( @@ -122,7 +129,10 @@ impl Workload { .function_descriptor(), ) }; - let mut descriptor = RenderPipelineDescriptor::new(function("tob_triangle")); + let placing = library(&device, fixture); + let vertex = placing.new_function_with_name(name).unwrap(); + let mut descriptor = + RenderPipelineDescriptor::new(StageSource::Function(vertex.function_descriptor())); descriptor.fragment = Some(function("tob_solid")); let mut color = ColorAttachmentDescriptor::new(PixelFormat::RGBA8Unorm); color.blending_enabled = true; @@ -678,6 +688,49 @@ fn exact_regions_draw_every_command_region_by_region() { ); } +/// A vertex function of six clip distances (`fan_clip_triangle` of the owned fixture +/// `engine/fan_clip_distances.ll`), which leave the fan-out's four too few of a device's eight: the +/// translator refuses the fan-out variants of the commands drawn with it +/// (`pipeline::link::fan_out`), so under `FanOut` none of them is fanned out or drawn through the +/// region-copy variant, and each is drawn region by region through its regions variant, as under +/// `Exact`. `commit`'s settle, finding the region-copy variant refused, began that variant's +/// pipelines as the step draws it (`render.rs`, `fan_out_refused`), so the host step's first frame +/// of a device that drew none of the states before makes none itself +/// (`WorkCounts::host_step_pipelines_made`). The pixels are those of the same commands drawn on the +/// encoder after. +#[test] +fn a_refused_fan_out_variant_s_first_frame_makes_no_pipeline() { + let scratch = Scratch::new("rate-mapped-fan-out-refused"); + let device = device_under(&scratch.0, RateMapRegionPolicy::FanOut); + let clipped = ("engine/fan_clip_distances.ll", "fan_clip_triangle"); + let workload = Workload::placed_by(device, COMMANDS, clipped); + let (executed, before, after) = workload.frame(true); + let made = after.host_step_pipelines_made - before.host_step_pipelines_made; + let counts = fan_counts(&before, &after); + let copies = copy_counts(&before, &after); + let limits = workload.device.compile_device().profile().limits(); + let clip_distances = + (limits.max_clip_distances).min(limits.max_combined_clip_and_cull_distances); + let refused = fans_out(&workload.device) && clip_distances < 6 + 4; + eprintln!( + "RATE-MAP-REFUSED {made} made at the step; counts {counts:?}; region-copy draws and \ + recordings {copies:?}; {clip_distances} clip distances, fan-out refused {refused}" + ); + if refused { + // Of the eight, the command across `w = 0` alone took a fan-out variant: its probe flags + // it, and it is drawn in parts, its one triangle through the region-copy variant. + assert_eq!(counts, [1, 1, 0, 1], "probes, flagged, fanned, in parts"); + assert_eq!(copies[0], 1, "draws through the region-copy variant"); + } + assert_eq!(made, 0, "pipelines the host step made"); + let (drawn, _, _) = workload.frame(false); + let (differ, first) = differing(&drawn, &executed); + assert_eq!( + differ, 0, + "texels the execution draws otherwise, the first {first:?}" + ); +} + /// The first frame of a device that drew none of the commands' states before: its host step makes /// no `VkPipeline` itself (`WorkCounts::host_step_pipelines_made`), under `FanOut` with the /// eight's pattern and with [`PARTED`] commands probed in parts, and under `Exact`. `commit`'s diff --git a/crates/metal2vulkan-validation/fixtures/public/engine/TOOLCHAIN b/crates/metal2vulkan-validation/fixtures/public/engine/TOOLCHAIN index c597abb5e..43d45da0a 100644 --- a/crates/metal2vulkan-validation/fixtures/public/engine/TOOLCHAIN +++ b/crates/metal2vulkan-validation/fixtures/public/engine/TOOLCHAIN @@ -80,6 +80,7 @@ fixture dsx_int_attributes: metal: Apple metal version 32023.921 (metalfe-32023. fixture dsx_lane_reads: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425) fixture dsx_wide_int_attributes: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425) fixture dvp_rog_coherence: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425) +fixture fan_clip_distances: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118) fixture fbl_fetch_blend16: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425) fixture fbl_fetch_blend16_narrow: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425) fixture fbl_fetch_blend16_safe: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425) diff --git a/crates/metal2vulkan-validation/fixtures/public/engine/fan_clip_distances.air.ll b/crates/metal2vulkan-validation/fixtures/public/engine/fan_clip_distances.air.ll new file mode 100644 index 000000000..cf3a0d167 --- /dev/null +++ b/crates/metal2vulkan-validation/fixtures/public/engine/fan_clip_distances.air.ll @@ -0,0 +1,63 @@ +; Generated by scripts/regen-air-fixtures from fan_clip_distances.metal (typed pointers; xcrun metal-as reassembles it). Do not edit. +; ModuleID = '-' +source_filename = "-" +target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-n8:16:32" +target triple = "air64_v28-apple-macosx26.0.0" + +; Function Attrs: mustprogress nofree norecurse nosync nounwind readnone willreturn +define <{ <4 x float>, [6 x float] }> @fan_clip_triangle(i32 noundef %0, <2 x float> addrspace(2)* nocapture noundef readonly "air-buffer-no-alias" %1) local_unnamed_addr #0 { + %3 = zext i32 %0 to i64 + %4 = getelementptr inbounds <2 x float>, <2 x float> addrspace(2)* %1, i64 %3 + %5 = load <2 x float>, <2 x float> addrspace(2)* %4, align 8, !tbaa !22, !alias.scope !25 + %6 = shufflevector <2 x float> %5, <2 x float> poison, <4 x i32> + %7 = shufflevector <4 x float> %6, <4 x float> , <4 x i32> + %8 = extractelement <2 x float> %5, i64 0 + %9 = fadd fast float %8, 4.000000e+00 + %10 = insertvalue [6 x float] poison, float %9, 0 + %11 = insertvalue [6 x float] %10, float %9, 1 + %12 = insertvalue [6 x float] %11, float %9, 2 + %13 = insertvalue [6 x float] %12, float %9, 3 + %14 = insertvalue [6 x float] %13, float %9, 4 + %15 = insertvalue [6 x float] %14, float %9, 5 + %16 = insertvalue <{ <4 x float>, [6 x float] }> undef, <4 x float> %7, 0 + %17 = insertvalue <{ <4 x float>, [6 x float] }> %16, [6 x float] %15, 1 + ret <{ <4 x float>, [6 x float] }> %17 +} + +attributes #0 = { mustprogress nofree norecurse nosync nounwind readnone willreturn "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="0" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" } + +!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8} +!air.vertex = !{!9} +!air.compile_options = !{!16, !17, !18} +!llvm.ident = !{!19} +!air.version = !{!20} +!air.language_version = !{!21} + +!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 27, i32 2]} +!1 = !{i32 1, !"wchar_size", i32 4} +!2 = !{i32 7, !"frame-pointer", i32 2} +!3 = !{i32 7, !"air.max_device_buffers", i32 31} +!4 = !{i32 7, !"air.max_constant_buffers", i32 31} +!5 = !{i32 7, !"air.max_threadgroup_buffers", i32 31} +!6 = !{i32 7, !"air.max_textures", i32 128} +!7 = !{i32 7, !"air.max_read_write_textures", i32 8} +!8 = !{i32 7, !"air.max_samplers", i32 16} +!9 = !{<{ <4 x float>, [6 x float] }> (i32, <2 x float> addrspace(2)*)* @fan_clip_triangle, !10, !13} +!10 = !{!11, !12} +!11 = !{!"air.position", !"air.arg_type_name", !"float4", !"air.arg_name", !"position"} +!12 = !{!"air.clip_distance", !"air.clip_distance_array_size", i32 6, !"air.arg_type_name", !"float", !"air.arg_name", !"clip"} +!13 = !{!14, !15} +!14 = !{i32 0, !"air.vertex_id", !"air.arg_type_name", !"uint", !"air.arg_name", !"vid"} +!15 = !{i32 1, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.read", !"air.address_space", i32 2, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"float2", !"air.arg_name", !"p"} +!16 = !{!"air.compile.denorms_disable"} +!17 = !{!"air.compile.fast_math_enable"} +!18 = !{!"air.compile.framebuffer_fetch_enable"} +!19 = !{!"Apple metal version 32023.922 (metalfe-32023.922.4)"} +!20 = !{i32 2, i32 8, i32 0} +!21 = !{!"Metal", i32 4, i32 0, i32 0} +!22 = !{!23, !23, i64 0} +!23 = !{!"omnipotent char", !24, i64 0} +!24 = !{!"Simple C++ TBAA"} +!25 = !{!26} +!26 = distinct !{!26, !27, !"air-alias-scope-arg(1)"} +!27 = distinct !{!27, !"air-alias-scopes(fan_clip_triangle)"} diff --git a/crates/metal2vulkan-validation/fixtures/public/engine/fan_clip_distances.ll b/crates/metal2vulkan-validation/fixtures/public/engine/fan_clip_distances.ll new file mode 100644 index 000000000..4f305240b --- /dev/null +++ b/crates/metal2vulkan-validation/fixtures/public/engine/fan_clip_distances.ll @@ -0,0 +1,63 @@ +; Generated by scripts/regen-air-fixtures from fan_clip_distances.metal (llvm-dis of fan_clip_distances.air). Do not edit. +; ModuleID = '' +source_filename = "-" +target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-n8:16:32" +target triple = "air64_v28-apple-macosx26.0.0" + +; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) +define <{ <4 x float>, [6 x float] }> @fan_clip_triangle(i32 noundef %0, ptr addrspace(2) noundef readonly captures(none) "air-buffer-no-alias" %1) local_unnamed_addr #0 { + %3 = zext i32 %0 to i64 + %4 = getelementptr inbounds <2 x float>, ptr addrspace(2) %1, i64 %3 + %5 = load <2 x float>, ptr addrspace(2) %4, align 8, !tbaa !22, !alias.scope !25 + %6 = shufflevector <2 x float> %5, <2 x float> poison, <4 x i32> + %7 = shufflevector <4 x float> %6, <4 x float> , <4 x i32> + %8 = extractelement <2 x float> %5, i64 0 + %9 = fadd fast float %8, 4.000000e+00 + %10 = insertvalue [6 x float] poison, float %9, 0 + %11 = insertvalue [6 x float] %10, float %9, 1 + %12 = insertvalue [6 x float] %11, float %9, 2 + %13 = insertvalue [6 x float] %12, float %9, 3 + %14 = insertvalue [6 x float] %13, float %9, 4 + %15 = insertvalue [6 x float] %14, float %9, 5 + %16 = insertvalue <{ <4 x float>, [6 x float] }> undef, <4 x float> %7, 0 + %17 = insertvalue <{ <4 x float>, [6 x float] }> %16, [6 x float] %15, 1 + ret <{ <4 x float>, [6 x float] }> %17 +} + +attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="0" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" } + +!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8} +!air.vertex = !{!9} +!air.compile_options = !{!16, !17, !18} +!llvm.ident = !{!19} +!air.version = !{!20} +!air.language_version = !{!21} + +!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 27, i32 2]} +!1 = !{i32 1, !"wchar_size", i32 4} +!2 = !{i32 7, !"frame-pointer", i32 2} +!3 = !{i32 7, !"air.max_device_buffers", i32 31} +!4 = !{i32 7, !"air.max_constant_buffers", i32 31} +!5 = !{i32 7, !"air.max_threadgroup_buffers", i32 31} +!6 = !{i32 7, !"air.max_textures", i32 128} +!7 = !{i32 7, !"air.max_read_write_textures", i32 8} +!8 = !{i32 7, !"air.max_samplers", i32 16} +!9 = !{ptr @fan_clip_triangle, !10, !13} +!10 = !{!11, !12} +!11 = !{!"air.position", !"air.arg_type_name", !"float4", !"air.arg_name", !"position"} +!12 = !{!"air.clip_distance", !"air.clip_distance_array_size", i32 6, !"air.arg_type_name", !"float", !"air.arg_name", !"clip"} +!13 = !{!14, !15} +!14 = !{i32 0, !"air.vertex_id", !"air.arg_type_name", !"uint", !"air.arg_name", !"vid"} +!15 = !{i32 1, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.read", !"air.address_space", i32 2, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"float2", !"air.arg_name", !"p"} +!16 = !{!"air.compile.denorms_disable"} +!17 = !{!"air.compile.fast_math_enable"} +!18 = !{!"air.compile.framebuffer_fetch_enable"} +!19 = !{!"Apple metal version 32023.922 (metalfe-32023.922.4)"} +!20 = !{i32 2, i32 8, i32 0} +!21 = !{!"Metal", i32 4, i32 0, i32 0} +!22 = !{!23, !23, i64 0} +!23 = !{!"omnipotent char", !24, i64 0} +!24 = !{!"Simple C++ TBAA"} +!25 = !{!26} +!26 = distinct !{!26, !27, !"air-alias-scope-arg(1)"} +!27 = distinct !{!27, !"air-alias-scopes(fan_clip_triangle)"} diff --git a/crates/metal2vulkan-validation/fixtures/public/engine/fan_clip_distances.metal b/crates/metal2vulkan-validation/fixtures/public/engine/fan_clip_distances.metal new file mode 100644 index 000000000..f4419987b --- /dev/null +++ b/crates/metal2vulkan-validation/fixtures/public/engine/fan_clip_distances.metal @@ -0,0 +1,26 @@ +// Owned fixture: a vertex function that writes six clip distances, so a rate-mapped pass's fan-out +// variant of a draw of it, which adds four of its own, needs ten, more than a device of eight +// carries (`pipeline::link::geometry`), and the runtime draws it region by region. Each vertex lies +// at the point of buffer 0 its vertex ID names, as `tob_triangle` of `tob_renders.metal` places it, +// and each clip distance is 4 more than its x, which no point of x at least -1 clips. Written for +// this repository; no third-party source. Loop-free. +#include +using namespace metal; + +struct FanClipVertex { + float4 position [[position]]; + float clip [[clip_distance]] [6]; +}; + +vertex FanClipVertex fan_clip_triangle(uint vid [[vertex_id]], constant float2 *p [[buffer(0)]]) { + float2 at = p[vid]; + FanClipVertex out; + out.position = float4(at, 0.0, 1.0); + out.clip[0] = 4.0 + at.x; + out.clip[1] = 4.0 + at.x; + out.clip[2] = 4.0 + at.x; + out.clip[3] = 4.0 + at.x; + out.clip[4] = 4.0 + at.x; + out.clip[5] = 4.0 + at.x; + return out; +} diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index bf5577e30..d26ea1527 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -94,7 +94,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 523; /// changes no behaviour, and then updates that hash alone with the commit saying why, or changes /// behaviour and also bumps this version. The commit that bumps it says what changed; this comment /// keeps no history. -pub const RUNTIME_VERSION: u32 = 2427; +pub const RUNTIME_VERSION: u32 = 2428; /// The digest of the sources this build of the crate was compiled from: SHA-256, as lowercase hex, /// of the manifest, the build script, and exactly the files the crate roots' module tree compiles