|
| 1 | +import { expect, it, onTestFinished } from 'vitest'; |
| 2 | + |
| 3 | +import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray'; |
| 4 | +import vtkImageData from 'vtk.js/Sources/Common/DataModel/ImageData'; |
| 5 | +import vtkWebGPUDevice from 'vtk.js/Sources/Rendering/WebGPU/Device'; |
| 6 | + |
| 7 | +/* eslint-disable no-undef, no-bitwise */ |
| 8 | +it.skipIf(!__VTK_TEST_WEBGPU__).for([false, true])( |
| 9 | + 'linearly samples 16 bit image scalars with tier1 enabled %s', |
| 10 | + async (enableTier1, { skip }) => { |
| 11 | + const adapter = await navigator.gpu.requestAdapter(); |
| 12 | + expect(adapter).not.toBeNull(); |
| 13 | + skip( |
| 14 | + enableTier1 && !adapter.features.has('texture-formats-tier1'), |
| 15 | + 'texture-formats-tier1 unavailable' |
| 16 | + ); |
| 17 | + const handle = await adapter.requestDevice({ |
| 18 | + requiredFeatures: enableTier1 ? ['texture-formats-tier1'] : [], |
| 19 | + }); |
| 20 | + onTestFinished(() => handle.destroy()); |
| 21 | + const device = vtkWebGPUDevice.newInstance(); |
| 22 | + device.initialize(handle); |
| 23 | + |
| 24 | + const image = vtkImageData.newInstance(); |
| 25 | + image.setDimensions(1, 1, 2); |
| 26 | + image.getPointData().setScalars( |
| 27 | + vtkDataArray.newInstance({ |
| 28 | + values: new Int16Array([-2048, 1024, 1024, -512]), |
| 29 | + numberOfComponents: 2, |
| 30 | + }) |
| 31 | + ); |
| 32 | + const texture = device.getTextureManager().getTextureForImageData(image); |
| 33 | + const view = texture.createView('scalars', { dimension: '3d' }); |
| 34 | + const sampler = handle.createSampler({ |
| 35 | + minFilter: 'linear', |
| 36 | + magFilter: 'linear', |
| 37 | + }); |
| 38 | + |
| 39 | + const pipeline = await handle.createComputePipelineAsync({ |
| 40 | + layout: 'auto', |
| 41 | + compute: { |
| 42 | + module: handle.createShaderModule({ |
| 43 | + code: ` |
| 44 | + @group(0) @binding(0) var image: texture_3d<f32>; |
| 45 | + @group(0) @binding(1) var linearSampler: sampler; |
| 46 | + @group(0) @binding(2) var<storage, read_write> result: vec2f; |
| 47 | + @compute @workgroup_size(1) fn main() { |
| 48 | + result = textureSampleLevel(image, linearSampler, vec3f(0.5), 0).rg; |
| 49 | + }`, |
| 50 | + }), |
| 51 | + entryPoint: 'main', |
| 52 | + }, |
| 53 | + }); |
| 54 | + const output = handle.createBuffer({ |
| 55 | + size: 8, |
| 56 | + usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC, |
| 57 | + }); |
| 58 | + const readback = handle.createBuffer({ |
| 59 | + size: 8, |
| 60 | + usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ, |
| 61 | + }); |
| 62 | + handle.pushErrorScope('validation'); |
| 63 | + const bindings = handle.createBindGroup({ |
| 64 | + layout: pipeline.getBindGroupLayout(0), |
| 65 | + entries: [ |
| 66 | + { binding: 0, resource: view.getHandle() }, |
| 67 | + { binding: 1, resource: sampler }, |
| 68 | + { binding: 2, resource: { buffer: output } }, |
| 69 | + ], |
| 70 | + }); |
| 71 | + const encoder = handle.createCommandEncoder(); |
| 72 | + const pass = encoder.beginComputePass(); |
| 73 | + pass.setPipeline(pipeline); |
| 74 | + pass.setBindGroup(0, bindings); |
| 75 | + pass.dispatchWorkgroups(1); |
| 76 | + pass.end(); |
| 77 | + encoder.copyBufferToBuffer(output, 0, readback, 0, 8); |
| 78 | + handle.queue.submit([encoder.finish()]); |
| 79 | + expect(await handle.popErrorScope()).toBeNull(); |
| 80 | + |
| 81 | + await readback.mapAsync(GPUMapMode.READ); |
| 82 | + const samples = new Float32Array(readback.getMappedRange()); |
| 83 | + expect(Array.from(samples, (value) => value * texture.getScale())).toEqual([ |
| 84 | + -512, 256, |
| 85 | + ]); |
| 86 | + } |
| 87 | +); |
0 commit comments