Conformance probe: the viewport depth range's values row, Clamp mode, Depth16Unorm and the depth test

The probe takes a table of cases: each a range, a depth attachment (none,
Depth32Float or Depth16Unorm), a fragment (plain, or one that writes
[[depth(any)]] = [[position]].z), a depth clip mode, five quads' clip z, a
clear depth and a compare function. Each column prints whether a fragment
landed, the position.z and .w it read, and the stored depth.

Recorded on the Apple M3 Max, macOS 27.0.1 (job m3-max-85617), plain and
under API validation, byte for byte alike, in the probe's header:
- Clip mode: position.z is never clamped; clip space's 0 <= z <= w holds
  whatever the range; Depth32Float, Depth16Unorm and a [[depth]] output
  store the z clamped to [0, 1]; the depth test compares that clamped
  value on both sides of 0 and of 1.
- Clamp mode: no z clip; position.z is never clamped; the stored depth is
  clamped to the range's part in [0, 1] ([0.5, 2] stores 0.5 to 1).
This commit is contained in:
Anees Iqbal committed 2026-12-03 15:15:17 -08:00
1 parent 1d6054b189
commit 458b5420d2
1 file changed
+149 -40
@@ -1,13 +1,19 @@
// What Metal does with a viewport whose depth range lies past [0, 1]: znear -1, zfar 1, beside
// the control range znear 0, zfar 1.
// What Metal does with a viewport whose depth range lies past [0, 1], beside control ranges in it.
//
// One render pass per case into a 5 by 1 RGBA32Float colour target and, where the case has one, a
// 5 by 1 Depth32Float attachment cleared to 0.5 (depth compare Always, writes on). Five quads
// cover the five columns, each at its own clip-space z (w = 1): 0, 0.25, 0.5, 0.75 and 1. Each
// fragment writes its [[position]].z and .w into the colour target; the `depth` fragment also
// writes [[depth(any)]] = [[position]].z. For each case and column it prints the z the quad
// was drawn at, the [[position]].z and .w the fragment read, and the depth the attachment
// stored, with the command buffer's status.
// 5 by 1 depth attachment (Depth32Float or Depth16Unorm) cleared to the case's depth, under the
// case's depth compare function with writes on, in the case's depth clip mode. Five quads cover
// the five columns, each at its own clip-space z (w = 1). Each fragment writes its
// [[position]].z, its .w and a 1 (the column was drawn) into the colour target, which is cleared
// to -9; the `depth` fragment also writes [[depth(any)]] = [[position]].z. For each case and
// column it prints the z the quad was drawn at, whether a fragment landed, the [[position]].z and
// .w it read, and the depth the attachment stored, with the command buffer's status.
//
// The cases ask, for a range past [0, 1]: what position.z reads; what a Depth32Float and a
// Depth16Unorm attachment store, from the rasterized depth and from a [[depth]] output; which
// value the depth test compares, on both sides of 0 and of 1; whether a quad past clip space's z
// bounds is clipped; and, in MTLDepthClipModeClamp, what the stored depth is clamped to (the
// viewport's range or [0, 1]). The [0, 1] and [0.25, 0.75] cases are the controls.
//
// xcrun clang -fobjc-arc -O1 -framework Foundation -framework Metal \
// -o viewport_depth_past_unit viewport_depth_past_unit.m
@@ -17,8 +23,9 @@
// The layer run says whether Metal's API validation takes the range; the plain run is Metal's
// answer (the layer is not passive).
//
// WHAT IT MEASURED, Apple M3 Max, macOS 27.0.1 (job m3-max-85541), the plain run and the layer
// run line for line alike, the layer printing nothing past "Metal API Validation Enabled":
// WHAT IT MEASURED, Apple M3 Max, macOS 27.0.1 (job m3-max-85541, the first five cases, which
// then printed no "drawn" field), the plain run and the layer run line for line alike, the layer
// printing nothing past "Metal API Validation Enabled":
//
// - setViewport takes znear -1, zfar 1, and every command buffer completes with no error;
// - [-1, 1]: position.z reads -1, -0.5, 0, 0.5 and 1 at clip z 0, 0.25, 0.5, 0.75 and 1
@@ -26,6 +33,28 @@
// - [-1, 1]: the Depth32Float attachment stores 0, 0, 0, 0.5 and 1, that z clamped to [0, 1];
// a [[depth(any)]] output of position.z stores the same;
// - [0, 1], the control: position.z and the stored depth are clip z, with either fragment.
//
// WHAT IT MEASURED, Apple M3 Max, macOS 27.0.1 (job m3-max-85617, every case above), the plain
// run and the layer run byte for byte alike, every command buffer completed with no error, the
// layer printing nothing past "Metal API Validation Enabled":
//
// - the first five cases as before, each column drawn;
// - Clip mode: position.z is znear + z * (zfar - znear) unclamped, under every range ([-1, 1],
// [0, 2], the reversed [1, -1], [2, 3]); a quad at clip z -0.5, -0.01, 1.01 or 1.5 is not
// drawn under [-1, 1] or [0, 1], with a depth attachment or without: clip space's 0 <= z <= w
// holds whatever the range;
// - Clip mode: Depth32Float and Depth16Unorm store that z clamped to [0, 1], and so does a
// [[depth(any)]] output of it ([0, 2]: 0, 0.5, 1, 1, 1; Depth16Unorm raw 0, 32768, 65535,
// 65535, 65535; [2, 3]: 1 at every column);
// - Clip mode: the depth test compares the clamped value: Equal against a clear of 0 passes at
// position.z -1, -0.5 and 0 under [-1, 1], and Less against 0 at none, through [[depth]]
// too; Equal against a clear of 1 passes at 1, 1.5 and 2 under [0, 2], and Greater at none;
// Depth16Unorm alike;
// - Clamp mode: every quad is drawn (no z clip); position.z is unclamped (-0.25 to 2.75 under
// [0.5, 2]); the stored depth, from the rasterized z or a [[depth]] output, on Depth32Float
// and Depth16Unorm, is clamped to the range's part in [0, 1]: [0.25, 0.75] stores 0.25 to
// 0.75, [0.5, 2] stores 0.5, 0.5, 1, 1, 1 (raw 32768 and 65535), [-1, 1] and [0, 1] store 0
// to 1.
#import <Foundation/Foundation.h>
#import <Metal/Metal.h>
@@ -44,16 +73,15 @@ static NSString *const SOURCE =
@"#include <metal_stdlib>\n"
"using namespace metal;\n"
"struct V { float4 pos [[position]]; };\n"
"constant float Z[5] = {0.0, 0.25, 0.5, 0.75, 1.0};\n"
"vertex V quads(uint vid [[vertex_id]]) {\n"
"vertex V quads(uint vid [[vertex_id]], constant float *Z [[buffer(0)]]) {\n"
" uint q = vid / 6, k = vid % 6;\n"
" float2 c[6] = {float2(0, 0), float2(1, 0), float2(0, 1), float2(1, 0), float2(1, 1), float2(0, 1)};\n"
" float x0 = -1.0 + 2.0 * float(q) / 5.0, x1 = -1.0 + 2.0 * float(q + 1) / 5.0;\n"
" V o; o.pos = float4(mix(x0, x1, c[k].x), mix(-1.0, 1.0, c[k].y), Z[q], 1.0); return o; }\n"
"fragment float4 plain(V in [[stage_in]]) { return float4(in.pos.z, in.pos.w, 0, 1); }\n"
"fragment float4 plain(V in [[stage_in]]) { return float4(in.pos.z, in.pos.w, 1, 1); }\n"
"struct D { float4 c [[color(0)]]; float d [[depth(any)]]; };\n"
"fragment D depth(V in [[stage_in]]) {\n"
" D o; o.c = float4(in.pos.z, in.pos.w, 0, 1); o.d = in.pos.z; return o; }\n";
" D o; o.c = float4(in.pos.z, in.pos.w, 1, 1); o.d = in.pos.z; return o; }\n";
static const char *STATUS[] = {"NotEnqueued", "Enqueued", "Committed", "Scheduled", "Completed",
"Error"};
@@ -62,25 +90,57 @@ static id<MTLDevice> DEV;
static id<MTLCommandQueue> QUEUE;
static id<MTLLibrary> LIB;
static void run_case(const char *name, double znear, double zfar, int with_depth,
const char *fragment) {
// The clip-space z of the five quads: inside clip space, and across its bounds.
static const float INSIDE[5] = {0.0f, 0.25f, 0.5f, 0.75f, 1.0f};
static const float ACROSS[5] = {-0.5f, -0.01f, 0.5f, 1.01f, 1.5f};
enum DepthKind { NONE, D32, D16 };
typedef struct {
const char *name;
double znear, zfar;
enum DepthKind depth;
const char *fragment;
MTLDepthClipMode clip;
const float *z;
double clear;
MTLCompareFunction compare;
} Case;
static const char *depth_name(enum DepthKind k) {
return k == D32 ? "Depth32Float" : k == D16 ? "Depth16Unorm" : "none";
}
static const char *compare_name(MTLCompareFunction f) {
switch (f) {
case MTLCompareFunctionAlways: return "Always";
case MTLCompareFunctionEqual: return "Equal";
case MTLCompareFunctionLess: return "Less";
case MTLCompareFunctionGreater: return "Greater";
default: return "?";
}
}
static void run_case(const Case *k) {
NSError *e = nil;
MTLPixelFormat df = k->depth == D32 ? MTLPixelFormatDepth32Float
: k->depth == D16 ? MTLPixelFormatDepth16Unorm : MTLPixelFormatInvalid;
MTLRenderPipelineDescriptor *pd = [[MTLRenderPipelineDescriptor alloc] init];
pd.vertexFunction = [LIB newFunctionWithName:@"quads"];
pd.fragmentFunction = [LIB newFunctionWithName:[NSString stringWithUTF8String:fragment]];
pd.fragmentFunction = [LIB newFunctionWithName:[NSString stringWithUTF8String:k->fragment]];
pd.colorAttachments[0].pixelFormat = MTLPixelFormatRGBA32Float;
if (with_depth) pd.depthAttachmentPixelFormat = MTLPixelFormatDepth32Float;
if (k->depth != NONE) pd.depthAttachmentPixelFormat = df;
id<MTLRenderPipelineState> ps = [DEV newRenderPipelineStateWithDescriptor:pd error:&e];
if (!ps) { P("%s: pipeline failed: %s", name, [[e description] UTF8String]); return; }
if (!ps) { P("%s: pipeline failed: %s", k->name, [[e description] UTF8String]); return; }
MTLTextureDescriptor *cd = [MTLTextureDescriptor
texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA32Float width:5 height:1 mipmapped:NO];
cd.usage = MTLTextureUsageRenderTarget; cd.storageMode = MTLStorageModePrivate;
id<MTLTexture> colour = [DEV newTextureWithDescriptor:cd];
id<MTLTexture> depth = nil;
if (with_depth) {
if (k->depth != NONE) {
MTLTextureDescriptor *dd = [MTLTextureDescriptor
texture2DDescriptorWithPixelFormat:MTLPixelFormatDepth32Float width:5 height:1 mipmapped:NO];
texture2DDescriptorWithPixelFormat:df width:5 height:1 mipmapped:NO];
dd.usage = MTLTextureUsageRenderTarget; dd.storageMode = MTLStorageModePrivate;
depth = [DEV newTextureWithDescriptor:dd];
}
@@ -96,48 +156,61 @@ static void run_case(const char *name, double znear, double zfar, int with_depth
if (depth) {
rp.depthAttachment.texture = depth;
rp.depthAttachment.loadAction = MTLLoadActionClear;
rp.depthAttachment.clearDepth = 0.5;
rp.depthAttachment.clearDepth = k->clear;
rp.depthAttachment.storeAction = MTLStoreActionStore;
}
MTLDepthStencilDescriptor *dsd = [[MTLDepthStencilDescriptor alloc] init];
dsd.depthCompareFunction = MTLCompareFunctionAlways;
dsd.depthWriteEnabled = with_depth ? YES : NO;
dsd.depthCompareFunction = k->compare;
dsd.depthWriteEnabled = depth ? YES : NO;
id<MTLDepthStencilState> dss = [DEV newDepthStencilStateWithDescriptor:dsd];
id<MTLCommandBuffer> cb = [QUEUE commandBuffer];
id<MTLRenderCommandEncoder> re = [cb renderCommandEncoderWithDescriptor:rp];
[re setRenderPipelineState:ps];
[re setDepthStencilState:dss];
MTLViewport vp = {0, 0, 5, 1, znear, zfar};
[re setDepthClipMode:k->clip];
[re setVertexBytes:k->z length:5 * sizeof(float) atIndex:0];
MTLViewport vp = {0, 0, 5, 1, k->znear, k->zfar};
[re setViewport:vp];
P("%s: setViewport znear %g zfar %g returned", name, znear, zfar);
P("%s: znear %g zfar %g depth %s fragment %s clip mode %s clear %g compare %s", k->name, k->znear,
k->zfar, depth_name(k->depth), k->fragment,
k->clip == MTLDepthClipModeClamp ? "Clamp" : "Clip", k->clear, compare_name(k->compare));
[re drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:30];
[re endEncoding];
id<MTLBlitCommandEncoder> be = [cb blitCommandEncoder];
[be copyFromTexture:colour sourceSlice:0 sourceLevel:0 sourceOrigin:MTLOriginMake(0, 0, 0)
sourceSize:MTLSizeMake(5, 1, 1) toBuffer:cread destinationOffset:0
destinationBytesPerRow:5 * 16 destinationBytesPerImage:5 * 16];
NSUInteger unit = k->depth == D16 ? 2 : 4;
if (depth) {
[be copyFromTexture:depth sourceSlice:0 sourceLevel:0 sourceOrigin:MTLOriginMake(0, 0, 0)
sourceSize:MTLSizeMake(5, 1, 1) toBuffer:dread destinationOffset:0
destinationBytesPerRow:5 * 4 destinationBytesPerImage:5 * 4];
destinationBytesPerRow:5 * unit destinationBytesPerImage:5 * unit];
}
[be endEncoding];
[cb commit];
[cb waitUntilCompleted];
NSError *err = cb.error;
P("%s: status %s error %s", name, ((unsigned)cb.status < 6) ? STATUS[cb.status] : "?",
P("%s: status %s error %s", k->name, ((unsigned)cb.status < 6) ? STATUS[cb.status] : "?",
err ? [[err localizedDescription] UTF8String] : "none");
static const float Z[5] = {0.0f, 0.25f, 0.5f, 0.75f, 1.0f};
const float *c = (const float *)cread.contents;
const float *d = (const float *)dread.contents;
for (int i = 0; i < 5; i++) {
if (depth)
P("%s: clip z %.2f position.z %.9g position.w %.9g stored depth %.9g", name, Z[i],
c[i * 4], c[i * 4 + 1], d[i]);
const float *t = c + i * 4;
int drawn = t[2] == 1.0f;
char stored[64];
if (k->depth == D32)
snprintf(stored, sizeof stored, "%.9g", ((const float *)dread.contents)[i]);
else if (k->depth == D16) {
unsigned raw = ((const unsigned short *)dread.contents)[i];
snprintf(stored, sizeof stored, "%.9g (raw %u)", raw / 65535.0, raw);
} else
snprintf(stored, sizeof stored, "(no depth attachment)");
if (drawn)
P("%s: clip z %.2f drawn position.z %.9g position.w %.9g stored depth %s", k->name, k->z[i],
t[0], t[1], stored);
else
P("%s: clip z %.2f position.z %.9g position.w %.9g (no depth attachment)", name, Z[i],
c[i * 4], c[i * 4 + 1]);
P("%s: clip z %.2f not drawn (texel %g %g %g %g) stored depth %s", k->name, k->z[i], t[0],
t[1], t[2], t[3], stored);
}
}
@@ -153,11 +226,47 @@ int main(void) {
NSError *e = nil;
LIB = [DEV newLibraryWithSource:SOURCE options:nil error:&e];
if (!LIB) { P("fatal: library: %s", [[e description] UTF8String]); return 3; }
run_case("unit-depth", 0.0, 1.0, 1, "plain");
run_case("past-depth", -1.0, 1.0, 1, "plain");
run_case("past-fragdepth", -1.0, 1.0, 1, "depth");
run_case("unit-fragdepth", 0.0, 1.0, 1, "depth");
run_case("past-nodepth", -1.0, 1.0, 0, "plain");
const MTLDepthClipMode CLIP = MTLDepthClipModeClip, CLAMP = MTLDepthClipModeClamp;
const MTLCompareFunction ALWAYS = MTLCompareFunctionAlways;
const Case cases[] = {
// The first run's five.
{"unit-depth", 0, 1, D32, "plain", CLIP, INSIDE, 0.5, ALWAYS},
{"past-depth", -1, 1, D32, "plain", CLIP, INSIDE, 0.5, ALWAYS},
{"past-fragdepth", -1, 1, D32, "depth", CLIP, INSIDE, 0.5, ALWAYS},
{"unit-fragdepth", 0, 1, D32, "depth", CLIP, INSIDE, 0.5, ALWAYS},
{"past-nodepth", -1, 1, NONE, "plain", CLIP, INSIDE, 0.5, ALWAYS},
// Above 1, and Depth16Unorm on both sides.
{"high-depth", 0, 2, D32, "plain", CLIP, INSIDE, 0.5, ALWAYS},
{"high-fragdepth", 0, 2, D32, "depth", CLIP, INSIDE, 0.5, ALWAYS},
{"past-d16", -1, 1, D16, "plain", CLIP, INSIDE, 0.5, ALWAYS},
{"past-d16-fragdepth", -1, 1, D16, "depth", CLIP, INSIDE, 0.5, ALWAYS},
{"high-d16", 0, 2, D16, "plain", CLIP, INSIDE, 0.5, ALWAYS},
{"high-d16-fragdepth", 0, 2, D16, "depth", CLIP, INSIDE, 0.5, ALWAYS},
// The depth test on both sides of 0 and of 1: the clamped value or the unclamped one.
{"test-low-equal", -1, 1, D32, "plain", CLIP, INSIDE, 0.0, MTLCompareFunctionEqual},
{"test-low-less", -1, 1, D32, "plain", CLIP, INSIDE, 0.0, MTLCompareFunctionLess},
{"test-high-equal", 0, 2, D32, "plain", CLIP, INSIDE, 1.0, MTLCompareFunctionEqual},
{"test-high-greater", 0, 2, D32, "plain", CLIP, INSIDE, 1.0, MTLCompareFunctionGreater},
{"test-low-equal-d16", -1, 1, D16, "plain", CLIP, INSIDE, 0.0, MTLCompareFunctionEqual},
{"test-high-greater-d16", 0, 2, D16, "plain", CLIP, INSIDE, 1.0, MTLCompareFunctionGreater},
{"test-low-less-fragdepth", -1, 1, D32, "depth", CLIP, INSIDE, 0.0, MTLCompareFunctionLess},
// Clip space's z bounds under the range, in Clip mode.
{"clip-past", -1, 1, D32, "plain", CLIP, ACROSS, 0.5, ALWAYS},
{"clip-past-nodepth", -1, 1, NONE, "plain", CLIP, ACROSS, 0.5, ALWAYS},
{"clip-unit", 0, 1, D32, "plain", CLIP, ACROSS, 0.5, ALWAYS},
// Clamp mode: what the stored depth is clamped to, and what position.z reads.
{"clamp-unit", 0, 1, D32, "plain", CLAMP, ACROSS, 0.5, ALWAYS},
{"clamp-mid", 0.25, 0.75, D32, "plain", CLAMP, ACROSS, 0.5, ALWAYS},
{"clamp-past", -1, 1, D32, "plain", CLAMP, ACROSS, 0.5, ALWAYS},
{"clamp-high", 0.5, 2, D32, "plain", CLAMP, ACROSS, 0.5, ALWAYS},
{"clamp-high-fragdepth", 0.5, 2, D32, "depth", CLAMP, ACROSS, 0.5, ALWAYS},
{"clamp-high-d16", 0.5, 2, D16, "plain", CLAMP, ACROSS, 0.5, ALWAYS},
{"clamp-past-nodepth", -1, 1, NONE, "plain", CLAMP, ACROSS, 0.5, ALWAYS},
// A reversed range, and one wholly above 1.
{"reversed", 1, -1, D32, "plain", CLIP, INSIDE, 0.5, ALWAYS},
{"both-past", 2, 3, D32, "plain", CLIP, INSIDE, 0.5, ALWAYS},
};
for (size_t i = 0; i < sizeof cases / sizeof cases[0]; i++) run_case(&cases[i]);
P("done");
}
return 0;