| <script src="../../resources/js-test.js"></script> | |
| <script> | |
| jsTestIsAsync = true; | |
| description("Tests WGSL immediate_address_space, setImmediates() validation, slot tracking, render bundles, and pipeline layout immediateSize."); | |
| const format = 'rgba8unorm'; | |
| const width = 4; | |
| onload = async () => { | |
| shouldBeTrue("navigator.gpu.wgslLanguageFeatures.has('immediate_address_space')"); | |
| const adapter = await navigator.gpu.requestAdapter(); | |
| globalThis.device = await adapter.requestDevice(); | |
| shouldBe("device.limits.maxImmediateSize", "64"); | |
| // 1. Synchronous OperationError validation on setImmediates. | |
| { | |
| const encoder = device.createCommandEncoder(); | |
| const pass = encoder.beginComputePass(); | |
| globalThis.unalignedLengthError = ""; | |
| try { | |
| pass.setImmediates(0, new Uint8Array(6)); | |
| } catch (e) { | |
| globalThis.unalignedLengthError = e.name; | |
| } | |
| shouldBeEqualToString("unalignedLengthError", "OperationError"); | |
| globalThis.outOfBoundsDataOffsetError = ""; | |
| try { | |
| pass.setImmediates(0, new Uint32Array(2), 3); | |
| } catch (e) { | |
| globalThis.outOfBoundsDataOffsetError = e.name; | |
| } | |
| shouldBeEqualToString("outOfBoundsDataOffsetError", "OperationError"); | |
| pass.end(); | |
| encoder.finish(); | |
| } | |
| // 2. GPU validation errors for unaligned rangeOffset and out-of-bounds maxImmediateSize. | |
| { | |
| device.pushErrorScope('validation'); | |
| const encoder = device.createCommandEncoder(); | |
| const pass = encoder.beginComputePass(); | |
| pass.setImmediates(2, new Uint32Array([1])); | |
| pass.end(); | |
| encoder.finish(); | |
| globalThis.unalignedRangeOffsetError = await device.popErrorScope(); | |
| shouldBeTrue("unalignedRangeOffsetError !== null"); | |
| } | |
| { | |
| device.pushErrorScope('validation'); | |
| const encoder = device.createCommandEncoder(); | |
| const pass = encoder.beginComputePass(); | |
| pass.setImmediates(64, new Uint32Array([1])); | |
| pass.end(); | |
| encoder.finish(); | |
| globalThis.exceedMaxImmediateSizeError = await device.popErrorScope(); | |
| shouldBeTrue("exceedMaxImmediateSizeError !== null"); | |
| } | |
| // 3. Compute auto-layout immediateSize, missing-required-slots error, and struct padding holes. | |
| const computeModule = device.createShaderModule({ | |
| code: ` | |
| requires immediate_address_space; | |
| struct PaddedImmediate { | |
| a: u32, | |
| @align(16) b: u32, | |
| } | |
| var<immediate> imm: PaddedImmediate; | |
| @group(0) @binding(0) var<storage, read_write> out: u32; | |
| @compute @workgroup_size(1) | |
| fn main() { | |
| out = imm.a + imm.b; | |
| } | |
| ` | |
| }); | |
| const autoComputePipeline = device.createComputePipeline({ | |
| layout: 'auto', | |
| compute: { module: computeModule, entryPoint: 'main' } | |
| }); | |
| const storage = device.createBuffer({ size: 4, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC }); | |
| const computeReadback = device.createBuffer({ size: 4, usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST }); | |
| const computeBindGroup = device.createBindGroup({ | |
| layout: autoComputePipeline.getBindGroupLayout(0), | |
| entries: [{ binding: 0, resource: { buffer: storage } }] | |
| }); | |
| { | |
| device.pushErrorScope('validation'); | |
| const encoder = device.createCommandEncoder(); | |
| const pass = encoder.beginComputePass(); | |
| pass.setPipeline(autoComputePipeline); | |
| pass.setBindGroup(0, computeBindGroup); | |
| pass.setImmediates(0, new Uint32Array([10])); | |
| pass.dispatchWorkgroups(1); | |
| pass.end(); | |
| encoder.finish(); | |
| globalThis.missingComputeSlotError = await device.popErrorScope(); | |
| shouldBeTrue("missingComputeSlotError !== null"); | |
| } | |
| { | |
| device.pushErrorScope('validation'); | |
| const encoder = device.createCommandEncoder(); | |
| const pass = encoder.beginComputePass(); | |
| pass.setPipeline(autoComputePipeline); | |
| pass.setBindGroup(0, computeBindGroup); | |
| // Slots 1..3 are struct padding and do not need to be set. | |
| pass.setImmediates(0, new Uint32Array([10])); | |
| pass.setImmediates(16, new Uint32Array([32])); | |
| pass.dispatchWorkgroups(1); | |
| pass.end(); | |
| encoder.copyBufferToBuffer(storage, 0, computeReadback, 0, 4); | |
| device.queue.submit([encoder.finish()]); | |
| globalThis.autoLayoutComputeError = await device.popErrorScope(); | |
| shouldBeNull("autoLayoutComputeError"); | |
| await computeReadback.mapAsync(GPUMapMode.READ); | |
| globalThis.computeResult = new Uint32Array(computeReadback.getMappedRange())[0]; | |
| shouldBe("computeResult", "42"); | |
| computeReadback.unmap(); | |
| } | |
| // 4. Explicit GPUPipelineLayoutDescriptor.immediateSize validation. | |
| { | |
| const bgl = device.createBindGroupLayout({ | |
| entries: [{ binding: 0, visibility: GPUShaderStage.COMPUTE, buffer: { type: 'storage' } }] | |
| }); | |
| device.pushErrorScope('validation'); | |
| const smallLayout = device.createPipelineLayout({ bindGroupLayouts: [bgl], immediateSize: 16 }); | |
| device.createComputePipeline({ | |
| layout: smallLayout, | |
| compute: { module: computeModule, entryPoint: 'main' } | |
| }); | |
| globalThis.tooSmallImmediateSizeError = await device.popErrorScope(); | |
| shouldBeTrue("tooSmallImmediateSizeError !== null"); | |
| device.pushErrorScope('validation'); | |
| const validLayout = device.createPipelineLayout({ bindGroupLayouts: [bgl], immediateSize: 32 }); | |
| device.createComputePipeline({ | |
| layout: validLayout, | |
| compute: { module: computeModule, entryPoint: 'main' } | |
| }); | |
| globalThis.sufficientImmediateSizeError = await device.popErrorScope(); | |
| shouldBeNull("sufficientImmediateSizeError"); | |
| } | |
| // 5. Render pass missing-required-slots, executeBundles clearing immediates, ICB bundle path, and replayed bundle path. | |
| const renderModule = device.createShaderModule({ | |
| code: ` | |
| requires immediate_address_space; | |
| struct DrawImmediate { | |
| x: f32, | |
| r: f32, | |
| g: f32, | |
| b: f32, | |
| } | |
| var<immediate> imm: DrawImmediate; | |
| @vertex fn vs() -> @builtin(position) vec4f { | |
| return vec4f(imm.x, 0.0, 0.5, 1.0); | |
| } | |
| @fragment fn fs() -> @location(0) vec4f { | |
| return vec4f(imm.r, imm.g, imm.b, 1.0); | |
| } | |
| ` | |
| }); | |
| const icbPipeline = device.createRenderPipeline({ | |
| layout: 'auto', | |
| vertex: { module: renderModule, entryPoint: 'vs' }, | |
| fragment: { module: renderModule, entryPoint: 'fs', targets: [{ format }] }, | |
| primitive: { topology: 'point-list' } | |
| }); | |
| const replayedPipeline = device.createRenderPipeline({ | |
| layout: 'auto', | |
| vertex: { module: renderModule, entryPoint: 'vs' }, | |
| fragment: { module: renderModule, entryPoint: 'fs', targets: [{ format }] }, | |
| primitive: { topology: 'point-list' }, | |
| // A non-default sample mask forces RenderBundleEncoder's command replay path | |
| // (m_requiresCommandReplay; see FIXME for https://bugs.webkit.org/show_bug.cgi?id=26499). | |
| multisample: { count: 1, mask: 0x7fffffff } | |
| }); | |
| const texture = device.createTexture({ | |
| size: [width, 1], | |
| format, | |
| usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC | |
| }); | |
| const rowReadback = device.createBuffer({ size: 256, usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ }); | |
| const pixelX = pixel => (pixel + 0.5) / width * 2 - 1; | |
| { | |
| device.pushErrorScope('validation'); | |
| const encoder = device.createCommandEncoder(); | |
| const pass = encoder.beginRenderPass({ | |
| colorAttachments: [{ view: texture.createView(), loadOp: 'clear', storeOp: 'store' }] | |
| }); | |
| pass.setPipeline(icbPipeline); | |
| pass.draw(1); | |
| pass.end(); | |
| encoder.finish(); | |
| globalThis.missingDrawSlotError = await device.popErrorScope(); | |
| shouldBeTrue("missingDrawSlotError !== null"); | |
| } | |
| { | |
| const emptyBundle = device.createRenderBundleEncoder({ colorFormats: [format] }).finish(); | |
| device.pushErrorScope('validation'); | |
| const encoder = device.createCommandEncoder(); | |
| const pass = encoder.beginRenderPass({ | |
| colorAttachments: [{ view: texture.createView(), loadOp: 'clear', storeOp: 'store' }] | |
| }); | |
| pass.setPipeline(icbPipeline); | |
| pass.setImmediates(0, new Float32Array([pixelX(0), 1, 0, 0])); | |
| pass.executeBundles([emptyBundle]); | |
| pass.setPipeline(icbPipeline); | |
| pass.draw(1); | |
| pass.end(); | |
| encoder.finish(); | |
| globalThis.clearedAfterExecuteBundlesError = await device.popErrorScope(); | |
| shouldBeTrue("clearedAfterExecuteBundlesError !== null"); | |
| } | |
| async function executeBundleAndReadRow(bundle) { | |
| const encoder = device.createCommandEncoder(); | |
| const pass = encoder.beginRenderPass({ | |
| colorAttachments: [{ view: texture.createView(), loadOp: 'clear', storeOp: 'store', clearValue: { r: 0, g: 0, b: 0, a: 1 } }] | |
| }); | |
| pass.executeBundles([bundle]); | |
| pass.end(); | |
| encoder.copyTextureToBuffer({ texture }, { buffer: rowReadback, bytesPerRow: 256 }, [width, 1]); | |
| device.queue.submit([encoder.finish()]); | |
| await rowReadback.mapAsync(GPUMapMode.READ); | |
| const bytes = new Uint8Array(rowReadback.getMappedRange(0, width * 4)); | |
| let row = ''; | |
| for (let i = 0; i < width; ++i) { | |
| const [r, g, b] = bytes.subarray(i * 4, i * 4 + 3); | |
| row += r > 128 ? 'R' : g > 128 ? 'G' : b > 128 ? 'B' : 'k'; | |
| } | |
| rowReadback.unmap(); | |
| return row; | |
| } | |
| { | |
| device.pushErrorScope('validation'); | |
| const bundleEncoder = device.createRenderBundleEncoder({ colorFormats: [format] }); | |
| bundleEncoder.setPipeline(icbPipeline); | |
| bundleEncoder.setImmediates(0, new Float32Array([pixelX(0), 1, 0, 0])); | |
| bundleEncoder.draw(1); | |
| bundleEncoder.setImmediates(0, new Float32Array([pixelX(1), 0, 1, 0])); | |
| bundleEncoder.draw(1); | |
| const bundle = bundleEncoder.finish(); | |
| globalThis.icbBundleRow = await executeBundleAndReadRow(bundle); | |
| globalThis.icbBundleError = await device.popErrorScope(); | |
| shouldBeNull("icbBundleError"); | |
| shouldBeEqualToString("icbBundleRow", "RGkk"); | |
| } | |
| { | |
| device.pushErrorScope('validation'); | |
| const bundleEncoder = device.createRenderBundleEncoder({ colorFormats: [format] }); | |
| bundleEncoder.setPipeline(replayedPipeline); | |
| bundleEncoder.setImmediates(0, new Float32Array([pixelX(2), 0, 0, 1])); | |
| bundleEncoder.draw(1); | |
| bundleEncoder.setImmediates(0, new Float32Array([pixelX(3), 0, 1, 0])); | |
| bundleEncoder.draw(1); | |
| const bundle = bundleEncoder.finish(); | |
| globalThis.replayedBundleRow = await executeBundleAndReadRow(bundle); | |
| globalThis.replayedBundleError = await device.popErrorScope(); | |
| shouldBeNull("replayedBundleError"); | |
| shouldBeEqualToString("replayedBundleRow", "kkBG"); | |
| } | |
| finishJSTest(); | |
| }; | |
| </script> |