diff --git a/crates/metal2vulkan-runtime/src/pipeline/render.rs b/crates/metal2vulkan-runtime/src/pipeline/render.rs index c26b037a6..62451f279 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/render.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/render.rs @@ -1421,7 +1421,9 @@ impl GraphicsState { // Metal answers nil with an `NSError` for a state with rasterization disabled whose vertex // function returns a value, and for a rasterizing one whose vertex function returns none // outside OpenGL mode, which makes it rasterizing nothing (`unrasterized.rs`), before it - // compiles anything. + // compiles anything. The second's answer outside OpenGL mode is the compile's, which reads + // it from the vertex entry it specializes anyway (`DescriptorError::RasterizedVoidVertex`), + // so only the others ask. // Each answer below is the one a creation of a descriptor of the same key worked out, where // one kept it in memory or in the cache directory (`shader_cache/creation.rs`), else worked // out now; kept where this creation worked one out and every question has its answer. @@ -1437,15 +1439,16 @@ impl GraphicsState { // earlier creation read reads its entries and its plain answers rather than decoding and // specializing them again (`pipeline/memo.rs`). let entries = cache.memo.stage_entries(); - let returns = match known.vertex_returns_value { - Some(returns) => Ok(returns), - None => { - learned = true; - device.shared().counts.linked_use_question_asked(); - descriptor.vertex_returns_value(&options.budget(), entries) - } - }; - answers.vertex_returns_value = returns.as_ref().ok().copied(); + let returns = + super::unrasterized::asks(descriptor).then(|| match known.vertex_returns_value { + Some(returns) => Ok(returns), + None => { + learned = true; + device.shared().counts.linked_use_question_asked(); + descriptor.vertex_returns_value(&options.budget(), entries) + } + }); + answers.vertex_returns_value = returns.as_ref().and_then(|r| r.as_ref().ok().copied()); let rasterizes = super::unrasterized::rasterizes("new_render_pipeline_state", descriptor, returns)?; // Metal links the fragment function whether or not the pipeline rasterizes, and answers @@ -1540,7 +1543,8 @@ impl GraphicsState { (descriptor, options, used, added_unsupported), entries, &made_key, - )?; + ) + .map_err(|e| super::unrasterized::compiled("new_render_pipeline_state", e))?; let reflected = match (kept, making) { (Some(Some(reflection)), _) => Some(Reflected::Kept(reflection)), (_, Some((key, making))) => Some(Reflected::Made( @@ -2584,14 +2588,15 @@ fn large_form( } /// Whether `answers` holds an answer to every linked-use question a creation of `descriptor` -/// asks (`GraphicsState::create_adding`): the stages using a direct argument where it supports -/// indirect command buffers, and always whether its vertex function returns a value, the -/// fragment's depth write and the unwritten fragment inputs. +/// asks (`GraphicsState::create_adding`): whether its vertex function returns a value where it +/// asks that (`unrasterized::asks`), the stages using a direct argument where it supports +/// indirect command buffers, and always the fragment's depth write and the unwritten fragment +/// inputs. fn answers_every_question( answers: &LinkedUseAnswers, descriptor: &RenderPipelineDescriptor, ) -> bool { - answers.vertex_returns_value.is_some() + (!super::unrasterized::asks(descriptor) || answers.vertex_returns_value.is_some()) && answers.fragment_writes_depth_unattached.is_some() && (!descriptor.support_indirect_command_buffers || answers.stages_using_direct_arguments.is_some()) diff --git a/crates/metal2vulkan-runtime/src/pipeline/unrasterized.rs b/crates/metal2vulkan-runtime/src/pipeline/unrasterized.rs index 6132ff321..9bb3a488b 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/unrasterized.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/unrasterized.rs @@ -36,12 +36,18 @@ //! of its descriptor with rasterization disabled, its fragment function dropped, which binds and //! draws as above. A post-tessellation vertex function is taken alike, unmeasured. //! - **Decided from the AIR.** Whether the vertex function returns a value is the compile layer's -//! answer (`RenderPipelineDescriptor::vertex_returns_value`); one it refuses (a stage it already -//! compiled, which a render descriptor's Metal form never holds) decides nothing. +//! answer (`RenderPipelineDescriptor::vertex_returns_value`), asked only where the descriptor's +//! state turns on it before its compile: with rasterization disabled, and in OpenGL mode +//! ([`asks`]). One it refuses (a stage it already compiled, which a render descriptor's Metal +//! form never holds) decides nothing. A rasterizing state outside OpenGL mode asks nothing: its +//! compile reads the answer from the vertex entry it specializes anyway, and its link refuses a +//! vertex stage without a position (`DescriptorError::RasterizedVoidVertex`), which the creation +//! answers as Metal's error ([`compiled`]). That refusal comes after the creation's linked-use +//! questions, where Metal's order of two refusals of one descriptor is not measured. use super::indirect_arguments::{CODE, DOMAIN}; use crate::error::{Error, MetalError, Problem}; -use metal2vulkan::error::Error as CompileError; +use metal2vulkan::error::{DescriptorError, Error as CompileError}; use metal2vulkan::pipeline::RenderPipelineDescriptor; /// Metal's `localizedDescription` for a state with rasterization disabled whose vertex function @@ -52,31 +58,49 @@ const DESCRIPTION: &str = /// Metal's `localizedDescription` for a rasterizing state whose vertex function returns none. const VOID: &str = "RasterizationEnabled is true but the vertex shader's return type is void"; +/// Whether a creation of `descriptor` asks whether its vertex function returns a value before its +/// compile: with rasterization disabled, and in OpenGL mode (the module docs). +pub(crate) fn asks(descriptor: &RenderPipelineDescriptor) -> bool { + !descriptor.rasterization_enabled || descriptor.private.open_gl_mode_enabled +} + +/// Metal's error, in `call`, for `problem` with `description`. +fn refused(call: &'static str, problem: Problem, description: &'static str) -> Error { + Error::MetalError(MetalError::new(call, problem, DOMAIN, CODE, description)) +} + /// Whether the state of `descriptor` rasterizes, its vertex function returning a value or not as -/// `returns` answers under the creation's budget: not with rasterization disabled, nor in OpenGL -/// mode where its vertex function returns none (the module docs). Refused in `call` where Metal -/// refuses the state; a refused answer decides nothing, the state rasterizing as `descriptor` -/// says. +/// `returns` answers under the creation's budget where the creation asks it ([`asks`]): not with +/// rasterization disabled, nor in OpenGL mode where its vertex function returns none (the module +/// docs). Refused in `call` where Metal refuses the state; a refused answer, or none asked, +/// decides nothing, the state rasterizing as `descriptor` says. pub(crate) fn rasterizes( call: &'static str, descriptor: &RenderPipelineDescriptor, - returns: Result, + returns: Option>, ) -> Result { - let refused = |problem, description| { - Err(Error::MetalError(MetalError::new( - call, - problem, - DOMAIN, - CODE, - description, - ))) - }; match (descriptor.rasterization_enabled, returns) { - (false, Ok(true)) => refused(Problem::UnrasterizedVertexReturn, DESCRIPTION), - (true, Ok(false)) if !descriptor.private.open_gl_mode_enabled => { - refused(Problem::RasterizedVoidVertex, VOID) + (false, Some(Ok(true))) => Err(refused( + call, + Problem::UnrasterizedVertexReturn, + DESCRIPTION, + )), + (true, Some(Ok(false))) if !descriptor.private.open_gl_mode_enabled => { + Err(refused(call, Problem::RasterizedVoidVertex, VOID)) } - (true, Ok(false)) => Ok(false), + (true, Some(Ok(false))) => Ok(false), (rasterizes, _) => Ok(rasterizes), } } + +/// `error`, a creation's compile's in `call`, as Metal answers it: the link's refusal of a +/// rasterizing vertex stage without a position is Metal's error for a rasterizing state whose +/// vertex function returns `void` (the module docs); any other is itself. +pub(crate) fn compiled(call: &'static str, error: Error) -> Error { + match error { + Error::Compile(CompileError::InvalidDescriptor(DescriptorError::RasterizedVoidVertex)) => { + refused(call, Problem::RasterizedVoidVertex, VOID) + } + other => other, + } +} diff --git a/crates/metal2vulkan-runtime/src/tests/descriptor_key_once.rs b/crates/metal2vulkan-runtime/src/tests/descriptor_key_once.rs index 8d6cb67e6..103b83692 100644 --- a/crates/metal2vulkan-runtime/src/tests/descriptor_key_once.rs +++ b/crates/metal2vulkan-runtime/src/tests/descriptor_key_once.rs @@ -111,6 +111,30 @@ fn a_render_creation_builds_one_descriptor_key() { ); } +/// A plain render creation -- rasterizing, a vertex and a fragment function, no indirect command +/// buffers -- on a fresh device asks two linked-use questions (the fragment's depth write and the +/// unwritten fragment inputs) and specializes its two stages once each. Whether its vertex +/// function returns a value is no question of its own: the compile reads it from the vertex entry +/// it specializes anyway, and its link refuses a `void` one (`pipeline/unrasterized.rs`). A +/// question for it cost every such creation a third question and a third stage entry, the +/// answer's, in the cost records' render creations. +#[test] +fn a_plain_render_creation_asks_two_questions_and_specializes_two_stages() { + let scratch = Scratch::new("plain-render-counts"); + let device = device(&scratch.0); + let descriptor = descriptor(&device); + let before = built(&device); + device + .new_render_pipeline_state(&descriptor, &PipelineOptions::default()) + .unwrap(); + let made = built(&device); + assert_eq!( + [made[4] - before[4], made[5] - before[5]], + [2, 2], + "stage entries specialized and linked-use questions asked" + ); +} + /// A state made of the descriptor again builds one key of its own: the key belongs to the /// creation, not to the state, so a second state of one descriptor neither keeps nor reuses the /// first's. diff --git a/crates/metal2vulkan/src/error/mod.rs b/crates/metal2vulkan/src/error/mod.rs index f33e87fe0..17921fe43 100644 --- a/crates/metal2vulkan/src/error/mod.rs +++ b/crates/metal2vulkan/src/error/mod.rs @@ -1867,6 +1867,13 @@ pub enum DescriptorError { UnknownEnumValue { ty: &'static str, value: u32 }, /// A pipeline descriptor without a required stage. MissingStage { stage: Stage }, + /// A render pipeline that rasterizes whose vertex function writes no `[[position]]`: one + /// returning `void`, as every other vertex function Metal's compiler builds returns its + /// position. `newRenderPipelineStateWithDescriptor:` refuses it ("RasterizationEnabled is true + /// but the vertex shader's return type is void", on an M3 Max under macOS 27.0.1), and makes it + /// under `PrivateRenderState::open_gl_mode_enabled` as a state that rasterizes nothing, which + /// is the descriptor with rasterization disabled: the link refuses it whatever the mode. + RasterizedVoidVertex, /// A device description (or the Khronos profile JSON it was read from) is malformed or /// inconsistent. DeviceDescription { detail: String }, diff --git a/crates/metal2vulkan/src/pipeline/descriptor.rs b/crates/metal2vulkan/src/pipeline/descriptor.rs index 02d9c32ca..8d8476c80 100644 --- a/crates/metal2vulkan/src/pipeline/descriptor.rs +++ b/crates/metal2vulkan/src/pipeline/descriptor.rs @@ -543,7 +543,10 @@ pub struct PrivateRenderState { /// states Metal otherwise refuses (`pipeline::private_state`'s docs): a fragment function /// writing depth without a depth attachment, its depth going nowhere, and a rasterizing state /// whose vertex function returns `void`, which rasterizes nothing. The runtime answers both - /// (`metal2vulkan-runtime`'s `new_render_pipeline_state`); the compile reads it not. + /// (`metal2vulkan-runtime`'s `new_render_pipeline_state`), the second as the state of the + /// descriptor with rasterization disabled; the compile reads it not, and its link refuses a + /// rasterizing `void` vertex function whatever the mode + /// ([`DescriptorError::RasterizedVoidVertex`](crate::error::DescriptorError::RasterizedVoidVertex)). pub open_gl_mode_enabled: bool, } diff --git a/crates/metal2vulkan/src/pipeline/link.rs b/crates/metal2vulkan/src/pipeline/link.rs index ecf5b0a34..f4e0b43fe 100644 --- a/crates/metal2vulkan/src/pipeline/link.rs +++ b/crates/metal2vulkan/src/pipeline/link.rs @@ -87,6 +87,7 @@ use crate::pipeline::layout::{ BindingSource, DescriptorBinding, DescriptorSetLayout, PipelineLayout, PushConstantRange, }; use crate::pipeline::lower_inputs::ColorGroup; +use crate::pipeline::stage_io::StageBuiltin; use crate::pipeline::sym::{ CombinedSampler, HandleClass, ResourceKind, ResourceSlot, SlotClass, SlotRef, SpecKey, Stage, }; @@ -653,7 +654,8 @@ fn mismatch(expected: Stage, found: Stage) -> Error { /// The stages match the pipeline kind: one kernel; a vertex stage and a fragment stage exactly /// when rasterization is enabled; an optional object stage, a mesh stage, and a fragment stage -/// exactly when rasterization is enabled; or any one stage alone. +/// exactly when rasterization is enabled; or any one stage alone. A rasterizing render +/// pipeline's vertex stage writes its position (`DescriptorError::RasterizedVoidVertex`). fn check_stages(ifaces: &[&StageInterface], inputs: &LinkInputs) -> Result<(), Error> { let stages: Vec = ifaces.iter().map(|i| i.stage).collect(); match inputs { @@ -707,9 +709,19 @@ fn check_stages(ifaces: &[&StageInterface], inputs: &LinkInputs) -> Result<(), E None => return Err(missing_stage(*want)), } } - match stages.get(expected.len()) { - Some(extra) => Err(mismatch(*expected.last().unwrap_or(&Stage::Vertex), *extra)), - None => Ok(()), + if let Some(extra) = stages.get(expected.len()) { + return Err(mismatch(*expected.last().unwrap_or(&Stage::Vertex), *extra)); + } + // Metal refuses a rasterizing pipeline whose vertex function returns `void`, the one + // vertex function without a position, which the compile read from its entry. + let positioned = |iface: &StageInterface| { + (iface.io.builtins.iter()).any(|b| matches!(b, StageBuiltin::Position { .. })) + }; + match ifaces.first() { + Some(vertex) if r.rasterization_enabled && !positioned(vertex) => Err( + Error::InvalidDescriptor(DescriptorError::RasterizedVoidVertex), + ), + _ => Ok(()), } } } diff --git a/crates/metal2vulkan/src/reflection/linked_tests.rs b/crates/metal2vulkan/src/reflection/linked_tests.rs index b2b3ef0f6..5165a3dcc 100644 --- a/crates/metal2vulkan/src/reflection/linked_tests.rs +++ b/crates/metal2vulkan/src/reflection/linked_tests.rs @@ -257,6 +257,49 @@ fn a_vertex_function_returns_a_value_where_its_type_is_not_void() { } } +/// A rasterizing render pipeline whose vertex function returns `void` is refused at its link, +/// with or without a fragment function, as Metal refuses it (`RasterizedVoidVertex`): the compile +/// reads it from the vertex entry it specializes anyway, so a creation asks no question for it. +/// With rasterization disabled it links, and so does every vertex function returning a value. +#[test] +fn a_rasterizing_void_vertex_function_is_refused_at_its_link() { + use crate::device::{Device, DeviceDescription, DevicePolicy}; + use crate::error::{DescriptorError, Error}; + let Some(library) = library() else { + return; + }; + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../metal2vulkan-validation/profiles/vk12-floor.json"); + let text = std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display())); + let description = DeviceDescription::from_khronos_json(&text, None).expect("profile"); + let device = Device::new(description, DevicePolicy::default()).expect("device"); + let refused = Err(Error::InvalidDescriptor( + DescriptorError::RasterizedVoidVertex, + )); + for fragment in [None, Some("inputless_fragment")] { + let mut descriptor = RenderPipelineDescriptor::new(source(&library, "void_store_vertex")); + descriptor.fragment = fragment.map(|name| source(&library, name)); + descriptor.color_attachments[0] = color(); + let linked = device.compile_render_pipeline(&descriptor, &Budget::standard()); + assert_eq!(linked.map(|_| ()), refused, "{fragment:?} rasterizing"); + descriptor.rasterization_enabled = false; + descriptor.fragment = None; + let unrasterized = device.compile_render_pipeline(&descriptor, &Budget::standard()); + assert!(unrasterized.is_ok(), "{fragment:?}: {unrasterized:?}"); + } + for vertex_function in [ + "position_only_vertex", + "plain_vertex", + "position_value_vertex", + ] { + let mut descriptor = RenderPipelineDescriptor::new(source(&library, vertex_function)); + descriptor.fragment = Some(source(&library, "inputless_fragment")); + descriptor.color_attachments[0] = color(); + let linked = device.compile_render_pipeline(&descriptor, &Budget::standard()); + assert!(linked.is_ok(), "{vertex_function}: {linked:?}"); + } +} + /// A mesh pipeline's fragment function writes depth without a depth attachment where it has a /// `[[depth(...)]]` output and the pipeline no depth format: with or without a /// color attachment; not with a depth attachment, nor for a fragment function without one. diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 63f3ba5db..29a2cc430 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -13396,6 +13396,7 @@ variant metal2vulkan::error::DescriptorError::MissingStage MissingStage { stage: variant metal2vulkan::error::DescriptorError::OpenClMode OpenClMode { compiled: bool } variant metal2vulkan::error::DescriptorError::PixelFormatUsage PixelFormatUsage { format: u32, usage: FormatUsage } variant metal2vulkan::error::DescriptorError::PlaceholderEnumValue PlaceholderEnumValue { ty: &'static str, value: u32 } +variant metal2vulkan::error::DescriptorError::RasterizedVoidVertex RasterizedVoidVertex variant metal2vulkan::error::DescriptorError::SamplerLodClamp SamplerLodClamp variant metal2vulkan::error::DescriptorError::SpecializationConflict SpecializationConflict { key: SpecKey } variant metal2vulkan::error::DescriptorError::StageInputAttribute StageInputAttribute { attribute: u32 } diff --git a/docs/REFUSALS.md b/docs/REFUSALS.md index 4be55cf13..27fc9481d 100644 --- a/docs/REFUSALS.md +++ b/docs/REFUSALS.md @@ -382,7 +382,7 @@ Columns: | `Rebind` | `rebind_bytes_no_copy` of a buffer or heap the runtime cannot rebind as asked (a runtime extension; `rebind.rs`) | every device (a public call's own check of its arguments) | `ours`: a runtime extension: Metal rebinds no bytes under a buffer | 5: `crates/metal2vulkan-runtime/src/buffer_descriptor.rs (rebind_host_pages)`, `crates/metal2vulkan-runtime/src/detach.rs (detach_backing, 2 sites)` (+1) | `-` | | `IndirectCommandsDirectArgument` | `new_render_pipeline_state`, `new_mesh_render_pipeline_state` or `new_compute_pipeline_state` of a descriptor with `supportIndirectCommandBuffers`... | every device (a public call's own check of its arguments) | `metal-measured`: an M3 Max under macOS 26.7 and an M4 under 27.0.1: plain Metal answers nil with an NSError | 1: `crates/metal2vulkan-runtime/src/pipeline/indirect_arguments.rs (refusal)` | `-` | | `UnrasterizedVertexReturn` | `new_render_pipeline_state` of a descriptor with rasterization disabled whose vertex function's return type is not `void` | every device (a public call's own check of its arguments) | `metal-measured`: an M4, macOS 27.0.1: plain Metal answers nil with an NSError | 1: `crates/metal2vulkan-runtime/src/pipeline/unrasterized.rs (rasterizes)` | `-` | -| `RasterizedVoidVertex` | `new_render_pipeline_state` of a descriptor with rasterization enabled whose vertex function's return type is `void`, with or without a fragment... | every device (a public call's own check of its arguments) | `metal-measured`: an M3 Max, macOS 27.0.1: plain Metal answers nil with an NSError | 1: `crates/metal2vulkan-runtime/src/pipeline/unrasterized.rs (rasterizes)` | `-` | +| `RasterizedVoidVertex` | `new_render_pipeline_state` of a descriptor with rasterization enabled whose vertex function's return type is `void`, with or without a fragment... | every device (a public call's own check of its arguments) | `metal-measured`: an M3 Max, macOS 27.0.1: plain Metal answers nil with an NSError | 2: `crates/metal2vulkan-runtime/src/pipeline/unrasterized.rs (compiled)`, `crates/metal2vulkan-runtime/src/pipeline/unrasterized.rs (rasterizes)` | `-` | | `IndirectCommandsDirectArgumentUnproven` | `new_render_pipeline_state` (a render or mesh state) of a descriptor with `supportIndirectCommandBuffers` whose function of `stage`, the first in... | every device (a public call's own check of its arguments) | `unknown`: Metal's answer for a use only this rule keeps is unmeasured, so the state is refused as unproven | 1: `crates/metal2vulkan-runtime/src/pipeline/indirect_arguments.rs (check_render)` | `-` | | `IndirectCommandsDirectArgumentUndecided` | `new_render_pipeline_state`, `new_mesh_render_pipeline_state` or `new_compute_pipeline_state` of a descriptor with `supportIndirectCommandBuffers`... | every device (a public call's own check of its arguments) | `ours`: the compile layer will not say whether the functions use one, so no state is made | 1: `crates/metal2vulkan-runtime/src/pipeline/indirect_arguments.rs (undecided)` | `-` | | `DepthWrittenWithoutAttachment` | `new_render_pipeline_state` of a descriptor without a depth attachment whose fragment function writes depth (a `[[depth(...)]]` output), outside... | every device (a public call's own check of its arguments) | `metal-measured`: an M4, macOS 27.0.1: plain Metal answers nil with an NSError | 1: `crates/metal2vulkan-runtime/src/pipeline/linked_fragment.rs (check_depth)` | `-` | @@ -859,7 +859,7 @@ Each refusal's sites, with the function that builds it. - `invalid::Rebind`: `crates/metal2vulkan-runtime/src/buffer_descriptor.rs (rebind_host_pages)`; `crates/metal2vulkan-runtime/src/detach.rs (detach_backing, 2 sites)`; `crates/metal2vulkan-runtime/src/rebind.rs (rebind_bytes_no_copy, 2 sites)` - `invalid::IndirectCommandsDirectArgument`: `crates/metal2vulkan-runtime/src/pipeline/indirect_arguments.rs (refusal)` - `invalid::UnrasterizedVertexReturn`: `crates/metal2vulkan-runtime/src/pipeline/unrasterized.rs (rasterizes)` -- `invalid::RasterizedVoidVertex`: `crates/metal2vulkan-runtime/src/pipeline/unrasterized.rs (rasterizes)` +- `invalid::RasterizedVoidVertex`: `crates/metal2vulkan-runtime/src/pipeline/unrasterized.rs (compiled)`; `crates/metal2vulkan-runtime/src/pipeline/unrasterized.rs (rasterizes)` - `invalid::IndirectCommandsDirectArgumentUnproven`: `crates/metal2vulkan-runtime/src/pipeline/indirect_arguments.rs (check_render)` - `invalid::IndirectCommandsDirectArgumentUndecided`: `crates/metal2vulkan-runtime/src/pipeline/indirect_arguments.rs (undecided)` - `invalid::DepthWrittenWithoutAttachment`: `crates/metal2vulkan-runtime/src/pipeline/linked_fragment.rs (check_depth)`