From 83b49cdea51c4476b04375b30faf8f744538f1d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Beaufort?= Date: Tue, 1 Dec 2026 14:17:37 +0100 Subject: [PATCH 1/2] Add frag_depth qualifier tests with position input Adds operation tests in webgpu:api,operation,rendering,depth covering @builtin(frag_depth, less) and @builtin(frag_depth, greater) combined with @builtin(position) input, including pixel-frequency vs sample-frequency inputs and verifying pixel-center @builtin(position) evaluation under 4x MSAA on partially covered pixels. --- .../api/operation/rendering/depth.spec.ts | 435 +++++++++++++++++- 1 file changed, 434 insertions(+), 1 deletion(-) diff --git a/src/webgpu/api/operation/rendering/depth.spec.ts b/src/webgpu/api/operation/rendering/depth.spec.ts index 8e0a51a4cc91..f340db56bbc3 100644 --- a/src/webgpu/api/operation/rendering/depth.spec.ts +++ b/src/webgpu/api/operation/rendering/depth.spec.ts @@ -5,7 +5,7 @@ Test related to depth buffer, depth op, compare func, etc. import { makeTestGroup } from '../../../../common/framework/test_group.js'; import { TypedArrayBufferView } from '../../../../common/util/util.js'; import { isStencilTextureFormat, kDepthTextureFormats } from '../../../format_info.js'; -import { AllFeaturesMaxLimitsGPUTest } from '../../../gpu_test.js'; +import { AllFeaturesMaxLimitsGPUTest, GPUTestSubcaseBatchState } from '../../../gpu_test.js'; import * as ttu from '../../../texture_test_utils.js'; import { TexelView } from '../../../util/texture/texel_view.js'; @@ -634,3 +634,436 @@ g.test('fragment_depth_qualifiers') }, ]); }); + +const kExtraFragmentInputs = [ + 'none', + 'sample_index', + 'sample_interpolation', + 'centroid_interpolation', +] as const; +type ExtraFragmentInput = (typeof kExtraFragmentInputs)[number]; + +function skipIfExtraFragmentInputNotSupported( + t: GPUTestSubcaseBatchState, + extraInput: ExtraFragmentInput +) { + switch (extraInput) { + case 'none': + break; + case 'sample_index': + t.skipIf(t.isCompatibility, 'sample_index is not supported in compatibility mode'); + break; + case 'sample_interpolation': + t.skipIfInterpolationTypeOrSamplingNotSupported({ + type: 'perspective', + sampling: 'sample', + }); + break; + case 'centroid_interpolation': + t.skipIfInterpolationTypeOrSamplingNotSupported({ + type: 'perspective', + sampling: 'centroid', + }); + break; + } +} + +function getExtraFragmentInputWGSL(extraInput: ExtraFragmentInput): { + vertexDecl: string; + vertexReturnType: string; + vertexReturnExpr: (posExpr: string) => string; + fragmentParam: string; + extraDepthOffsetExpr: string; +} { + switch (extraInput) { + case 'none': + return { + vertexDecl: '', + vertexReturnType: '@builtin(position) vec4f', + vertexReturnExpr: posExpr => posExpr, + fragmentParam: '', + extraDepthOffsetExpr: '0.0', + }; + case 'sample_index': + return { + vertexDecl: '', + vertexReturnType: '@builtin(position) vec4f', + vertexReturnExpr: posExpr => posExpr, + fragmentParam: ', @builtin(sample_index) sampleIndex: u32', + extraDepthOffsetExpr: 'select(0.0, 0.25, sampleIndex >= 4u)', + }; + case 'sample_interpolation': + return { + vertexDecl: ` + struct VertexOut { + @builtin(position) pos: vec4f, + @location(0) @interpolate(perspective, sample) v: f32, + } + `, + vertexReturnType: 'VertexOut', + vertexReturnExpr: posExpr => `VertexOut(${posExpr}, 0.0)`, + fragmentParam: ', @location(0) @interpolate(perspective, sample) v: f32', + extraDepthOffsetExpr: 'v', + }; + case 'centroid_interpolation': + return { + vertexDecl: ` + struct VertexOut { + @builtin(position) pos: vec4f, + @location(0) @interpolate(perspective, centroid) v: f32, + } + `, + vertexReturnType: 'VertexOut', + vertexReturnExpr: posExpr => `VertexOut(${posExpr}, 0.0)`, + fragmentParam: ', @location(0) @interpolate(perspective, centroid) v: f32', + extraDepthOffsetExpr: 'v', + }; + } +} + +function createDepthEqualCheckPipeline( + t: DepthTest, + depthFormat: GPUTextureFormat, + colorFormat: GPUTextureFormat, + sampleCount: number, + expectedDepth: number +): GPURenderPipeline { + const module = t.device.createShaderModule({ + code: ` + @vertex + fn vs() -> @builtin(position) vec4f { + return vec4f(0.0, 0.0, ${expectedDepth}, 1.0); + } + + @fragment + fn fs() -> @location(0) vec4f { + return vec4f(0.0, 1.0, 0.0, 1.0); + } + `, + }); + return t.device.createRenderPipeline({ + layout: 'auto', + vertex: { module, entryPoint: 'vs' }, + fragment: { module, entryPoint: 'fs', targets: [{ format: colorFormat }] }, + primitive: { topology: 'point-list' }, + depthStencil: { + format: depthFormat, + depthWriteEnabled: false, + depthCompare: 'equal', + }, + multisample: { count: sampleCount }, + }); +} + +g.test('fragment_depth_qualifiers_with_position') + .desc( + ` + Tests @builtin(frag_depth, less) and @builtin(frag_depth, greater) when the fragment shader + also consumes @builtin(position), with both pixel-frequency and sample-frequency inputs and + with both single-sampled and multisampled attachments. + + On D3D, outputting SV_DepthGreaterEqual or SV_DepthLessEqual with default center-interpolated + SV_Position at pixel frequency violates DXC rule Sm.PSConsistentInterp unless the conservative + depth mode is dropped to SV_Depth. + ` + ) + .params(u => + u + .combine('mode', ['less', 'greater'] as const) + .combine('sampleCount', [1, 4] as const) + .combine('extraInput', kExtraFragmentInputs) + ) + .beforeAllSubcases(t => { + t.skipIfLanguageFeatureNotSupported('fragment_depth'); + skipIfExtraFragmentInputNotSupported(t, t.params.extraInput); + }) + .fn(t => { + const { mode, sampleCount, extraInput } = t.params; + + const colorFormat = 'rgba8unorm'; + const depthFormat = 'depth32float'; + const expectedDepth = mode === 'less' ? 0.25 : 0.75; + const depthExpr = mode === 'less' ? 'min(pos.z + extra, 0.25)' : 'max(pos.z + extra, 0.75)'; + const wgsl = getExtraFragmentInputWGSL(extraInput); + + const module = t.device.createShaderModule({ + code: ` + requires fragment_depth; + ${wgsl.vertexDecl} + @vertex + fn vs() -> ${wgsl.vertexReturnType} { + return ${wgsl.vertexReturnExpr('vec4f(0.0, 0.0, 0.5, 1.0)')}; + } + + @fragment + fn fs( + @builtin(position) pos: vec4f${wgsl.fragmentParam} + ) -> @builtin(frag_depth, ${mode}) f32 { + let extra = ${wgsl.extraDepthOffsetExpr}; + return ${depthExpr}; + } + `, + }); + + const depthPipeline = t.device.createRenderPipeline({ + layout: 'auto', + vertex: { module, entryPoint: 'vs' }, + fragment: { module, entryPoint: 'fs', targets: [] }, + primitive: { topology: 'point-list' }, + depthStencil: { + format: depthFormat, + depthWriteEnabled: true, + depthCompare: 'always', + }, + multisample: { count: sampleCount }, + }); + + const checkPipeline = createDepthEqualCheckPipeline( + t, + depthFormat, + colorFormat, + sampleCount, + expectedDepth + ); + + const depthTexture = t.createTextureTracked({ + size: [1, 1], + format: depthFormat, + sampleCount, + usage: GPUTextureUsage.RENDER_ATTACHMENT, + }); + + const colorTexture = t.createTextureTracked({ + size: [1, 1], + format: colorFormat, + usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC, + }); + + const msaaColorTexture = + sampleCount > 1 + ? t.createTextureTracked({ + size: [1, 1], + format: colorFormat, + sampleCount, + usage: GPUTextureUsage.RENDER_ATTACHMENT, + }) + : undefined; + + const encoder = t.device.createCommandEncoder(); + { + const pass = encoder.beginRenderPass({ + colorAttachments: [], + depthStencilAttachment: { + view: depthTexture.createView(), + depthClearValue: 0.0, + depthLoadOp: 'clear', + depthStoreOp: 'store', + }, + }); + pass.setPipeline(depthPipeline); + pass.draw(1); + pass.end(); + } + { + const pass = encoder.beginRenderPass({ + colorAttachments: [ + msaaColorTexture + ? { + view: msaaColorTexture.createView(), + resolveTarget: colorTexture.createView(), + clearValue: { r: 1.0, g: 0.0, b: 0.0, a: 1.0 }, + loadOp: 'clear', + storeOp: 'discard', + } + : { + view: colorTexture.createView(), + clearValue: { r: 1.0, g: 0.0, b: 0.0, a: 1.0 }, + loadOp: 'clear', + storeOp: 'store', + }, + ], + depthStencilAttachment: { + view: depthTexture.createView(), + depthLoadOp: 'load', + depthStoreOp: 'discard', + }, + }); + pass.setPipeline(checkPipeline); + pass.draw(1); + pass.end(); + } + + t.device.queue.submit([encoder.finish()]); + + ttu.expectSinglePixelComparisonsAreOkInTexture(t, { texture: colorTexture }, [ + { + coord: { x: 0, y: 0 }, + exp: new Uint8Array([0, 255, 0, 255]), + }, + ]); + }); + +g.test('fragment_depth_qualifiers_position_multisample') + .desc( + ` + Tests that @builtin(position) remains evaluated at the pixel center (0.5, 0.5) under 4x MSAA + on a partially covered pixel (where the pixel center lies outside the primitive) when outputting + @builtin(frag_depth, less) or @builtin(frag_depth, greater). + + This verifies that implementations do not satisfy D3D's Sm.PSConsistentInterp rule by changing + SV_Position to centroid or sample interpolation, which would alter @builtin(position) under + multisampling. + ` + ) + .params(u => + u.combine('mode', ['less', 'greater'] as const).combine('extraInput', kExtraFragmentInputs) + ) + .beforeAllSubcases(t => { + t.skipIfLanguageFeatureNotSupported('fragment_depth'); + skipIfExtraFragmentInputNotSupported(t, t.params.extraInput); + }) + .fn(t => { + const { mode, extraInput } = t.params; + + const sampleCount = 4; + const colorFormat = 'rgba8unorm'; + const depthFormat = 'depth32float'; + const expectedDepth = mode === 'less' ? 0.25 : 0.75; + const depthExpr = mode === 'less' ? 'min(pos.z + extra, 0.25)' : 'max(pos.z + extra, 0.75)'; + const wgsl = getExtraFragmentInputWGSL(extraInput); + + // Covers window y in [0, 0.4375] of the 1x1 pixel (NDC y in [0.125, 1.0]): + // samples 0 (0.375, 0.125) and 1 (0.875, 0.375) are covered; + // pixel center (0.5, 0.5) and samples 2 (0.125, 0.625) and 3 (0.625, 0.875) are not. + const module = t.device.createShaderModule({ + code: ` + requires fragment_depth; + ${wgsl.vertexDecl} + @vertex + fn vs(@builtin(vertex_index) vertexIndex: u32) -> ${wgsl.vertexReturnType} { + let positions = array( + vec4f(-1.0, 0.125, 0.5, 1.0), + vec4f( 3.0, 0.125, 0.5, 1.0), + vec4f(-1.0, 3.0, 0.5, 1.0), + ); + return ${wgsl.vertexReturnExpr('positions[vertexIndex]')}; + } + + struct FragmentOut { + @location(0) color: vec4f, + @builtin(frag_depth, ${mode}) depth: f32, + } + + @fragment + fn fs(@builtin(position) pos: vec4f${wgsl.fragmentParam}) -> FragmentOut { + let isCenter = all(abs(pos.xy - vec2f(0.5, 0.5)) < vec2f(0.01)); + let color = select(vec4f(1.0, 0.0, 0.0, 1.0), vec4f(0.0, 1.0, 0.0, 1.0), isCenter); + let extra = ${wgsl.extraDepthOffsetExpr}; + return FragmentOut(color, ${depthExpr}); + } + `, + }); + + const pipeline = t.device.createRenderPipeline({ + layout: 'auto', + vertex: { module, entryPoint: 'vs' }, + fragment: { module, entryPoint: 'fs', targets: [{ format: colorFormat }] }, + primitive: { topology: 'triangle-list' }, + depthStencil: { + format: depthFormat, + depthWriteEnabled: true, + depthCompare: 'always', + }, + multisample: { count: sampleCount }, + }); + + const checkPipeline = createDepthEqualCheckPipeline( + t, + depthFormat, + colorFormat, + sampleCount, + expectedDepth + ); + + const depthTexture = t.createTextureTracked({ + size: [1, 1], + format: depthFormat, + sampleCount, + usage: GPUTextureUsage.RENDER_ATTACHMENT, + }); + + const msaaColorTexture = t.createTextureTracked({ + size: [1, 1], + format: colorFormat, + sampleCount, + usage: GPUTextureUsage.RENDER_ATTACHMENT, + }); + + const positionColorTexture = t.createTextureTracked({ + size: [1, 1], + format: colorFormat, + usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC, + }); + + const depthColorTexture = t.createTextureTracked({ + size: [1, 1], + format: colorFormat, + usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC, + }); + + const encoder = t.device.createCommandEncoder(); + { + const pass = encoder.beginRenderPass({ + colorAttachments: [ + { + view: msaaColorTexture.createView(), + resolveTarget: positionColorTexture.createView(), + clearValue: { r: 0.0, g: 0.0, b: 0.0, a: 1.0 }, + loadOp: 'clear', + storeOp: 'discard', + }, + ], + depthStencilAttachment: { + view: depthTexture.createView(), + depthClearValue: 0.0, + depthLoadOp: 'clear', + depthStoreOp: 'store', + }, + }); + pass.setPipeline(pipeline); + pass.draw(3); + pass.end(); + } + { + const pass = encoder.beginRenderPass({ + colorAttachments: [ + { + view: msaaColorTexture.createView(), + resolveTarget: depthColorTexture.createView(), + clearValue: { r: 0.0, g: 0.0, b: 0.0, a: 1.0 }, + loadOp: 'clear', + storeOp: 'discard', + }, + ], + depthStencilAttachment: { + view: depthTexture.createView(), + depthLoadOp: 'load', + depthStoreOp: 'discard', + }, + }); + pass.setPipeline(checkPipeline); + pass.draw(1); + pass.end(); + } + + t.device.queue.submit([encoder.finish()]); + + // 2 of 4 samples are covered and should be green (0, 1, 0, 1); 2 uncovered stay black (0, 0, 0, 1). + const expectedHalfGreen = { R: 0, G: 0.5, B: 0, A: 1 }; + ttu.expectSinglePixelComparisonsAreOkInTexture(t, { texture: positionColorTexture }, [ + { coord: { x: 0, y: 0 }, exp: expectedHalfGreen }, + ]); + ttu.expectSinglePixelComparisonsAreOkInTexture(t, { texture: depthColorTexture }, [ + { coord: { x: 0, y: 0 }, exp: expectedHalfGreen }, + ]); + }); From 8082ae2cae463527fabfc440abc3d921ce419282 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Beaufort?= Date: Tue, 1 Dec 2026 14:32:24 +0100 Subject: [PATCH 2/2] Cover unused sample_index and sample-interpolated inputs --- .../api/operation/rendering/depth.spec.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/webgpu/api/operation/rendering/depth.spec.ts b/src/webgpu/api/operation/rendering/depth.spec.ts index f340db56bbc3..cc78801f7f48 100644 --- a/src/webgpu/api/operation/rendering/depth.spec.ts +++ b/src/webgpu/api/operation/rendering/depth.spec.ts @@ -638,11 +638,14 @@ g.test('fragment_depth_qualifiers') const kExtraFragmentInputs = [ 'none', 'sample_index', + 'unused_sample_index', 'sample_interpolation', + 'unused_sample_interpolation', 'centroid_interpolation', ] as const; type ExtraFragmentInput = (typeof kExtraFragmentInputs)[number]; +/** Skips the test case if `extraInput` is not supported in the current mode. */ function skipIfExtraFragmentInputNotSupported( t: GPUTestSubcaseBatchState, extraInput: ExtraFragmentInput @@ -651,9 +654,11 @@ function skipIfExtraFragmentInputNotSupported( case 'none': break; case 'sample_index': + case 'unused_sample_index': t.skipIf(t.isCompatibility, 'sample_index is not supported in compatibility mode'); break; case 'sample_interpolation': + case 'unused_sample_interpolation': t.skipIfInterpolationTypeOrSamplingNotSupported({ type: 'perspective', sampling: 'sample', @@ -668,6 +673,7 @@ function skipIfExtraFragmentInputNotSupported( } } +/** Returns WGSL snippets for declaring and optionally referencing `extraInput`. */ function getExtraFragmentInputWGSL(extraInput: ExtraFragmentInput): { vertexDecl: string; vertexReturnType: string; @@ -685,14 +691,17 @@ function getExtraFragmentInputWGSL(extraInput: ExtraFragmentInput): { extraDepthOffsetExpr: '0.0', }; case 'sample_index': + case 'unused_sample_index': return { vertexDecl: '', vertexReturnType: '@builtin(position) vec4f', vertexReturnExpr: posExpr => posExpr, fragmentParam: ', @builtin(sample_index) sampleIndex: u32', - extraDepthOffsetExpr: 'select(0.0, 0.25, sampleIndex >= 4u)', + extraDepthOffsetExpr: + extraInput === 'sample_index' ? 'select(0.0, 0.25, sampleIndex >= 4u)' : '0.0', }; case 'sample_interpolation': + case 'unused_sample_interpolation': return { vertexDecl: ` struct VertexOut { @@ -703,7 +712,7 @@ function getExtraFragmentInputWGSL(extraInput: ExtraFragmentInput): { vertexReturnType: 'VertexOut', vertexReturnExpr: posExpr => `VertexOut(${posExpr}, 0.0)`, fragmentParam: ', @location(0) @interpolate(perspective, sample) v: f32', - extraDepthOffsetExpr: 'v', + extraDepthOffsetExpr: extraInput === 'sample_interpolation' ? 'v' : '0.0', }; case 'centroid_interpolation': return { @@ -721,6 +730,7 @@ function getExtraFragmentInputWGSL(extraInput: ExtraFragmentInput): { } } +/** Creates a render pipeline that draws green where stored depth equals `expectedDepth`. */ function createDepthEqualCheckPipeline( t: DepthTest, depthFormat: GPUTextureFormat, @@ -759,8 +769,8 @@ g.test('fragment_depth_qualifiers_with_position') .desc( ` Tests @builtin(frag_depth, less) and @builtin(frag_depth, greater) when the fragment shader - also consumes @builtin(position), with both pixel-frequency and sample-frequency inputs and - with both single-sampled and multisampled attachments. + also consumes @builtin(position), with pixel-frequency inputs as well as both used and unused + sample-frequency inputs, and with both single-sampled and multisampled attachments. On D3D, outputting SV_DepthGreaterEqual or SV_DepthLessEqual with default center-interpolated SV_Position at pixel frequency violates DXC rule Sm.PSConsistentInterp unless the conservative