Runtime: a viewport Vulkan takes in no form that covers no pixel of the render area draws nothing

set_viewport and set_viewports refused, as Gap::Draw of DrawKind::Viewport, every viewport that
VkViewport's valid usage forbids: an extent past maxViewportDimensions, a corner past
viewportBoundsRange (VUID-VkViewport-width-01771, -height-01773, -x-01774, -x-01232, -y-01775,
-y-01776, -y-01777, -y-01233), a negative width, and depths past [0, 1] (the runtime enables no
VK_EXT_depth_range_unrestricted). Metal documents no such limit. A user met the refusal four times
in one session, and a WebGL 1 page reaches it with a far gl.viewport: 32768 is the bounds range on
the 780M and Iris profiles, 65536 on NVIDIA's.

No other Vulkan viewport maps clip space as one of these does, since a viewport's transform is
fixed by its own origin and extent. But where such a viewport covers no pixel of Metal's render
area, Metal draws nothing through it, as the rt draws nothing through a viewport of no extent. So
checked() now keeps a viewport Vulkan takes as it is, makes one it does not take that covers no
pixel (viewport_pixels) a viewport of no width or height at the area's origin with its depths cut
to [0, 1], which Vulkan takes and whose draws record an empty scissor, and refuses the rest. With
the bounds Vulkan requires (at least twice the largest extent past either side of the origin) and
an area no larger than maxViewportDimensions, a viewport past the bounds within that extent covers
a pixel only where its edge lies inside the last pixel of an area of the largest size; WebGL's
integer origins never do. What stays refused: a negative width over the area (Metal mirrors it), a
depth range past [0, 1] over it, an extent past maxViewportDimensions over it, and that last-pixel
edge. DrawKind::Viewport, set_viewport and the runtime README (Render encoders, and its known gaps)
say so. docs/REFUSALS.md regenerates unchanged: the Gap::Draw row and its checked() site stay.

The refusal test's mirrored viewport now lies over the target (origin x 4, width -4); at origin 0
it covered no pixel and now draws nothing, as Metal's. The new test also sets a mirrored viewport
and one with depths past [0, 1], both off the target.

Verified:
- Red: the test commit 17aa4279b3 on lavapipe (job m3-max-78333): 0 passed, 1 failed, at
  draw.rs:501, the set_viewport of the first far viewport.
- Green, this commit's tree (fd7df2eccb), run as a scratch commit of the same tree: lavapipe (job
  m3-max-78345) 3 passed, 868 skipped: the new test, the triangle test and the refusal test, under
  the validation layer, "10 viewports past bounds [-32768, 32768] and dimensions [16384, 16384]
  drew nothing". The same three tests from one msi-built gpucfg archive under the carried
  validation layer 1.4.363.0, each 3 passed, 870 skipped: Radeon 780M, RADV, Mesa 26.2.6 (job
  m3-max-78347, bounds [-32768, 32767], dimensions 16384); Iris Xe, ANV, Mesa 26.2.6 (job
  m3-max-78348, the same limits); RTX 3090, NVIDIA 620.70.06 (job m3-max-78349, bounds
  [-65536, 65536], dimensions 32768); RX 7900 XTX, RADV, Mesa 26.2.6 (job m3-max-78350, bounds
  [-32768, 32767], dimensions 16384).
- Job m3-max-78346: clippy -D warnings on metal2vulkan-runtime, all targets and features, plain,
  --cfg m2v_gpu and --cfg m2v_cpu_icd, each exit 0; the runtime's whole suite on lavapipe, 868
  passed, 3 skipped. Job m3-max-78371: the runtime's rustdoc with warnings denied, public and with
  private items, 0 diagnostics. scripts/api-signatures/signatures.py --check and
  refusal_surface.py --check pass. cargo fmt --all.
This commit is contained in:
Anees Iqbal committed 2026-12-02 06:15:13 -08:00
1 parent b0a8bd866b
commit 472cfba642
4 files changed
+74 -22

No files matched your search

+13 -4
View File
@@ -4749,8 +4749,12 @@ cache is answered compile-required on both.
front-facing winding is Vulkan's front face of the same name. A viewport of no width or height,
or with a NaN origin x, draws nothing, as Metal's. Metal clips a draw to the pixels its viewport
covers, so the scissor is Metal's rectangle cut to them; a rectangle past Metal's render area,
which Metal's validation stops, is cut to it, as plain Metal cuts it. A stencil reference
saturates at 255, as Metal stores it.
which Metal's validation stops, is cut to it, as plain Metal cuts it. A viewport Vulkan takes no
form of (a negative width, a depth range past `[0, 1]`, or an extent or corner past the
device's `maxViewportDimensions` and `viewportBoundsRange`) that covers no pixel of Metal's
render area, such as one whose origin lies far past the area, draws nothing, as Metal's: the
draw records a viewport of no extent at the area's origin behind an empty scissor. A stencil
reference saturates at 255, as Metal stores it.
- `set_viewports` and `set_scissor_rects` are `setViewports:count:` and
`setScissorRects:count:`, 1 to 16 each, recorded with their count (`vkCmdSetViewportWithCount`,
`vkCmdSetScissorWithCount`, so no pipeline is made for a count). A vertex function's
@@ -4887,8 +4891,13 @@ cache is answered compile-required on both.
(`set_line_width`, which keeps every width) is not 1 (`LineWidth`): Metal draws such a line as
a parallelogram that width wide, and Vulkan's wider lines are a device feature (`wideLines`)
the floor does not require; a
viewport Vulkan's is not (a
negative width, a depth range past `[0, 1]`, bounds past the device's viewport limits), a
viewport Vulkan's is not that covers a pixel of Metal's render area (`set_viewport`,
`set_viewports`), as no Vulkan viewport maps clip space as it does: a negative width, which
Metal mirrors; a depth range past `[0, 1]`; or an extent or corner past the device's
`maxViewportDimensions` and `viewportBoundsRange`, which, as Vulkan's bounds reach at least
twice the largest extent past either side of the origin, is a viewport wider or taller than
`maxViewportDimensions` over the area, or one whose edge lies inside the last pixel of an area
of that largest size; a
pipeline state whose formats are not the pass's attachments' (but for colours past the pass's
last colour attachment, above), a draw that binds a texture
whose image the pass renders, where neither the feedback loop nor a copy reads it: on a device
+4 -2
View File
@@ -1749,8 +1749,10 @@ pub enum DrawKind {
/// No viewport or scissor rectangle, or more than Metal's 16 (`setViewports:count:`,
/// `setScissorRects:count:`), which Metal's validation stops.
Viewports,
/// A viewport Vulkan's is not: a negative width (Metal mirrors the draw), a depth range past
/// `[0, 1]`, or bounds past the device's viewport limits.
/// A viewport Vulkan's is not that covers a pixel of the render area: a negative width (Metal
/// mirrors the draw), a depth range past `[0, 1]`, or bounds past the device's viewport
/// limits (`maxViewportDimensions`, `viewportBoundsRange`); no Vulkan viewport maps clip space
/// as it does. One that covers no pixel draws nothing, as Metal's.
Viewport,
/// A pipeline state whose colour, depth or stencil formats are not its pass's attachments',
/// or whose `rasterSampleCount` is not their sample count: Metal's validation stops such
@@ -1417,10 +1417,13 @@ impl RenderCommandEncoder {
/// `setViewport:`. Metal's default covers Metal's render area with the depth range `[0, 1]`. A
/// viewport of no width or height draws nothing, as Metal's; so does one whose origin's x is
/// NaN, as plain Metal draws nothing through it. A negative height flips the draw, as Metal's.
/// A viewport Vulkan takes no form of (a negative width, a depth range past `[0, 1]`, or
/// bounds past the device's `maxViewportDimensions` and `viewportBoundsRange`) that covers no
/// pixel of Metal's render area draws nothing, as Metal draws nothing through it.
///
/// Err: [`Refusal::ViewportNotFinite`] for any other field NaN or infinite; [`Gap::Draw`] of
/// [`DrawKind::Viewport`] for a negative width, a depth range past `[0, 1]`, or bounds past
/// the device's `maxViewportDimensions` and `viewportBoundsRange`.
/// [`DrawKind::Viewport`] for a viewport Vulkan takes no form of that covers a pixel of the
/// render area.
pub fn set_viewport(&mut self, viewport: Viewport) -> Result<(), Error> {
let v = self.checked(viewport)?;
self.viewports[0] = v;
@@ -1429,6 +1432,12 @@ impl RenderCommandEncoder {
}
/// `viewport` as a draw takes it, checked as [`set_viewport`](Self::set_viewport) says.
///
/// A viewport Vulkan takes as it is stays itself. No other Vulkan viewport maps clip space as
/// one past Vulkan's rules does. One that covers no pixel of the render area, through which
/// Metal draws nothing, becomes a viewport of no width or height at the area's origin with
/// its depths cut to `[0, 1]`: Vulkan takes it, it covers no pixel either
/// (`viewport_pixels`), and a draw through it records an empty scissor.
fn checked(&self, viewport: Viewport) -> Result<Viewport, Error> {
let v = match viewport.origin_x.is_nan() {
true => Viewport {
@@ -1447,6 +1456,10 @@ impl RenderCommandEncoder {
let vk = vk_viewport(&v);
let [x, y, width, height] = [vk.x, vk.y, vk.width, vk.height].map(f64::from);
let unit = 0.0..=1.0;
// `VkViewport`'s valid usage: a positive width, depths within `[0, 1]` (the runtime
// enables no `VK_EXT_depth_range_unrestricted`), and the extent and corners within
// `maxViewportDimensions` and `viewportBoundsRange` (VUID-VkViewport-width-01771,
// -height-01773, -x-01774, -x-01232, -y-01775, -y-01776, -y-01777 and -y-01233).
let fits = v.width >= 0.0
&& unit.contains(&v.znear)
&& unit.contains(&v.zfar)
@@ -1455,12 +1468,23 @@ impl RenderCommandEncoder {
&& x >= low
&& x + width <= high
&& [y, y + height].iter().all(|y| (low..=high).contains(y));
if !fits {
return Err(Error::Unsupported(Gap::Draw {
if fits {
return Ok(v);
}
let [columns, rows] = viewport_pixels(&v, self.pass.area);
match columns[0] == columns[1] || rows[0] == rows[1] {
true => Ok(Viewport {
origin_x: 0.0,
origin_y: 0.0,
width: 0.0,
height: 0.0,
znear: v.znear.clamp(0.0, 1.0),
zfar: v.zfar.clamp(0.0, 1.0),
}),
false => Err(Error::Unsupported(Gap::Draw {
kind: DrawKind::Viewport,
}));
})),
}
Ok(v)
}
/// The first `count` viewports are set: each later draw records them, and one whose vertex
+27 -10
View File
@@ -12,9 +12,10 @@
//! pipeline and copies no byte of the runtime's own; a memory barrier after draws that wrote
//! no memory records nothing.
//! - viewports past the device's `viewportBoundsRange` or `maxViewportDimensions`, on one side
//! each and over no pixel of the target, draw nothing, set alone or among several, and a
//! draw after them through a viewport that fits lands where Metal puts it; a viewport wider
//! than the device's largest, over the target, is refused.
//! each, and a mirrored one and one with depths past `[0, 1]`, all over no pixel of the
//! target, draw nothing, set alone or among several, and a draw after them through a
//! viewport that fits lands where Metal puts it; a viewport wider than the device's largest,
//! over the target, is refused.
//! - 2,000 draws in one encoder move none of the device's counters while it is open; its end
//! adds the pass, the push and the 1,999 pushes held.
//! - on a GPU, the lock budget (`crate::tests::locks`): 2,000 draws more in a recording, and
@@ -71,7 +72,8 @@
//! is recorded, and 16-bit indices there draw.
//! - what Metal refuses is refused, and what a draw does not build is a gap: no pipeline state,
//! a pipeline state of other formats than the pass's, a depth test, a stencil test without a
//! stencil attachment, a misaligned index offset, a non-finite viewport and a mirrored one.
//! stencil attachment, a misaligned index offset, a non-finite viewport and a mirrored one over
//! the target.
//! - a pass with a visibility result buffer draws, its command buffer keeps the buffer until it
//! retires, and its bytes stay as they were; `Disabled` at any offset records nothing, and the
//! pass takes the submission, barrier and pass one without the buffer takes (`Boolean` and
@@ -427,11 +429,12 @@ pub(crate) mod on_device {
});
}
/// A viewport past the device's `viewportBoundsRange` or `maxViewportDimensions` that covers
/// no pixel of the render area draws nothing, as Metal draws nothing through it, and a draw
/// after it through a viewport that fits lands where Metal puts it; one past them that covers
/// a pixel is refused. The layer checks each viewport recorded against the device's limits
/// (`VUID-VkViewport-x-01232` and the rest of `VkViewport`'s).
/// A viewport past the device's `viewportBoundsRange` or `maxViewportDimensions`, or with a
/// negative width or a depth range past `[0, 1]`, that covers no pixel of the render area
/// draws nothing, as Metal draws nothing through it, and a draw after it through a viewport
/// that fits lands where Metal puts it; one past the device's limits that covers a pixel is
/// refused. The layer checks each viewport recorded against `VkViewport`'s valid usage
/// (`VUID-VkViewport-x-01232` and the rest).
#[test]
fn every_device_draws_nothing_through_a_viewport_past_its_bounds() {
let Some(library) = examples() else {
@@ -471,7 +474,9 @@ pub(crate) mod on_device {
// Each past one of the device's limits, over no pixel of the target: an origin whose
// x plus the width passes the range's top (a WebGL page's far viewport), an origin
// below the range's bottom, the same two in y, a flipped one whose origin passes the
// top, and one wider, and one taller, than the device's largest viewport.
// top, and one wider, and one taller, than the device's largest viewport; then the
// other two Vulkan takes in no form, over no pixel either: a negative width and a
// depth range past `[0, 1]`.
let past = [
(
"x plus width past the range",
@@ -495,6 +500,17 @@ pub(crate) mod on_device {
"taller than the largest",
at(0.0, -(tall + 2.0), side, tall + 1.0),
),
(
"a mirrored one left of the target",
at(0.0, 0.0, -side, side),
),
(
"depths past [0, 1] left of the target",
Viewport {
zfar: 2.0,
..at(-2.0 * side, 0.0, side, side)
},
),
];
for (case, viewport) in past {
(encoder.set_viewport(viewport))
@@ -3498,6 +3514,7 @@ define internal void @_GLOBAL__sub_I_() section "air.static_init" {
);
same(
encoder.set_viewport(Viewport {
origin_x: 4.0,
width: -4.0,
..viewport
}),