|
| 1 | +import test from 'tape'; |
| 2 | +import testUtils from 'vtk.js/Sources/Testing/testUtils'; |
| 3 | + |
| 4 | +import vtkActor from 'vtk.js/Sources/Rendering/Core/Actor'; |
| 5 | +import vtkMapper from 'vtk.js/Sources/Rendering/Core/Mapper'; |
| 6 | +import 'vtk.js/Sources/Rendering/Misc/RenderingAPIs'; |
| 7 | +import vtkRenderer from 'vtk.js/Sources/Rendering/Core/Renderer'; |
| 8 | +import vtkRenderWindow from 'vtk.js/Sources/Rendering/Core/RenderWindow'; |
| 9 | +import vtkConeSource from 'vtk.js/Sources/Filters/Sources/ConeSource'; |
| 10 | +import vtkSphereSource from 'vtk.js/Sources/Filters/Sources/SphereSource'; |
| 11 | + |
| 12 | +// --------------------------------------------------------------------------- |
| 13 | +// Helpers |
| 14 | +// --------------------------------------------------------------------------- |
| 15 | + |
| 16 | +// Detect whether the current runtime actually supports WebGPU. |
| 17 | +function isWebGPUAvailable() { |
| 18 | + return typeof navigator !== 'undefined' && !!navigator.gpu; |
| 19 | +} |
| 20 | + |
| 21 | +// --------------------------------------------------------------------------- |
| 22 | +// Test: MSAA opaque + translucent rendering (WebGPU) |
| 23 | +// --------------------------------------------------------------------------- |
| 24 | + |
| 25 | +test.onlyIfWebGPU('Test WebGPU MSAA rendering', (t) => { |
| 26 | + const gc = testUtils.createGarbageCollector(t); |
| 27 | + |
| 28 | + const container = document.querySelector('body'); |
| 29 | + const renderWindowContainer = gc.registerDOMElement( |
| 30 | + document.createElement('div') |
| 31 | + ); |
| 32 | + container.appendChild(renderWindowContainer); |
| 33 | + |
| 34 | + // ------ Scene setup ------ |
| 35 | + const renderWindow = gc.registerResource(vtkRenderWindow.newInstance()); |
| 36 | + const renderer = gc.registerResource(vtkRenderer.newInstance()); |
| 37 | + renderWindow.addRenderer(renderer); |
| 38 | + renderer.setBackground(0.32, 0.34, 0.43); |
| 39 | + |
| 40 | + // Opaque cone |
| 41 | + const coneSource = gc.registerResource( |
| 42 | + vtkConeSource.newInstance({ height: 1.0, resolution: 60 }) |
| 43 | + ); |
| 44 | + const coneMapper = gc.registerResource(vtkMapper.newInstance()); |
| 45 | + coneMapper.setInputConnection(coneSource.getOutputPort()); |
| 46 | + const coneActor = gc.registerResource(vtkActor.newInstance()); |
| 47 | + coneActor.setMapper(coneMapper); |
| 48 | + renderer.addActor(coneActor); |
| 49 | + |
| 50 | + // Translucent sphere (exercises OrderIndependentTranslucentPass MSAA path) |
| 51 | + const sphereSource = gc.registerResource( |
| 52 | + vtkSphereSource.newInstance({ radius: 0.35, center: [0.3, 0.3, 0.0] }) |
| 53 | + ); |
| 54 | + const sphereMapper = gc.registerResource(vtkMapper.newInstance()); |
| 55 | + sphereMapper.setInputConnection(sphereSource.getOutputPort()); |
| 56 | + const sphereActor = gc.registerResource(vtkActor.newInstance()); |
| 57 | + sphereActor.setMapper(sphereMapper); |
| 58 | + sphereActor.getProperty().setOpacity(0.5); |
| 59 | + sphereActor.getProperty().setColor(0.2, 0.6, 0.9); |
| 60 | + renderer.addActor(sphereActor); |
| 61 | + |
| 62 | + // ------ Render window view ------ |
| 63 | + const apiView = gc.registerResource( |
| 64 | + renderWindow.newAPISpecificView('WebGPU') |
| 65 | + ); |
| 66 | + apiView.setContainer(renderWindowContainer); |
| 67 | + renderWindow.addView(apiView); |
| 68 | + apiView.setSize(400, 400); |
| 69 | + |
| 70 | + // ------ MSAA configuration ------ |
| 71 | + const webgpuAvailable = isWebGPUAvailable(); |
| 72 | + const desiredSampleCount = webgpuAvailable ? 4 : 1; |
| 73 | + |
| 74 | + if (apiView.setMultiSample) { |
| 75 | + // Validate that invalid sample counts are rejected |
| 76 | + t.notOk( |
| 77 | + apiView.setMultiSample(2), |
| 78 | + 'setMultiSample(2) should return false (invalid)' |
| 79 | + ); |
| 80 | + |
| 81 | + // Set the desired sample count |
| 82 | + apiView.setMultiSample(desiredSampleCount); |
| 83 | + } |
| 84 | + |
| 85 | + t.equal( |
| 86 | + apiView.getMultiSample ? apiView.getMultiSample() : 1, |
| 87 | + desiredSampleCount, |
| 88 | + `multiSample should be ${desiredSampleCount}` |
| 89 | + ); |
| 90 | + |
| 91 | + renderer.resetCamera(); |
| 92 | + |
| 93 | + // ------ Capture and verify ------ |
| 94 | + const promise = apiView |
| 95 | + .captureNextImage() |
| 96 | + .then((image) => { |
| 97 | + // The rendering completed without errors — this is the primary |
| 98 | + // regression check. MSAA misconfiguration (sample count mismatches, |
| 99 | + // missing resolve targets, etc.) would cause a GPU validation error |
| 100 | + // before we reach this point. |
| 101 | + t.ok(image, 'MSAA render produced an image without GPU errors'); |
| 102 | + }) |
| 103 | + .finally(gc.releaseResources); |
| 104 | + |
| 105 | + renderWindow.render(); |
| 106 | + return promise; |
| 107 | +}); |
0 commit comments