|
| 1 | +/* |
| 2 | +Copyright (c) 2026, Lars Brubaker |
| 3 | +All rights reserved. |
| 4 | +
|
| 5 | +Redistribution and use in source and binary forms, with or without |
| 6 | +modification, are permitted provided that the following conditions are met: |
| 7 | +
|
| 8 | +1. Redistributions of source code must retain the above copyright notice, this |
| 9 | + list of conditions and the following disclaimer. |
| 10 | +2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | + this list of conditions and the following disclaimer in the documentation |
| 12 | + and/or other materials provided with the distribution. |
| 13 | +
|
| 14 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 15 | +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 17 | +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 18 | +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 19 | +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 20 | +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 21 | +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 23 | +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | +*/ |
| 25 | + |
| 26 | +using System.Threading.Tasks; |
| 27 | +using MatterHackers.WebGpu; |
| 28 | +using MatterHackers.WebGpuRender; |
| 29 | +using TUnit.Core; |
| 30 | + |
| 31 | +namespace MatterHackers.Agg.Tests |
| 32 | +{ |
| 33 | + /// <summary> |
| 34 | + /// The swapchain policy decisions - what to do with each acquire status, how a surface size is |
| 35 | + /// clamped, and which surface format the swapchain is configured with - as pure functions, so they |
| 36 | + /// are pinned without a live GPU. The same three policies live in agg-gui-wgpu's <c>gpu.rs</c> |
| 37 | + /// (<c>surface_acquire_action</c>, <c>clamp_surface_size</c>, <c>pick_surface_format</c>) and the two |
| 38 | + /// implementations are kept in step deliberately. |
| 39 | + /// </summary> |
| 40 | + public class WebGpuSurfaceAcquireTests |
| 41 | + { |
| 42 | + [Test] |
| 43 | + public async Task AStaleSwapchainReconfiguresRatherThanSkipping() |
| 44 | + { |
| 45 | + // What a resize looks like from the acquire: reconfiguring at the known size is the only way |
| 46 | + // back, so these must not be a silent skip (the resize-black-screen regression). |
| 47 | + await Assert.That(WebGpuSurfaceTarget.ActionFor(WGPUSurfaceGetCurrentTextureStatus.Outdated)) |
| 48 | + .IsEqualTo(SurfaceAcquireAction.Reconfigure); |
| 49 | + await Assert.That(WebGpuSurfaceTarget.ActionFor(WGPUSurfaceGetCurrentTextureStatus.Lost)) |
| 50 | + .IsEqualTo(SurfaceAcquireAction.Reconfigure); |
| 51 | + } |
| 52 | + |
| 53 | + [Test] |
| 54 | + public async Task ATimeoutSkipsTheFrameAndAsksForAnother() |
| 55 | + { |
| 56 | + // A Timeout means the compositor was simply not ready - the swapchain is still valid, so |
| 57 | + // reconfiguring it would throw away a perfectly good one and can itself provoke another |
| 58 | + // Timeout. Skip, but wake back up: a reactive host would otherwise idle forever. |
| 59 | + await Assert.That(WebGpuSurfaceTarget.ActionFor(WGPUSurfaceGetCurrentTextureStatus.Timeout)) |
| 60 | + .IsEqualTo(SurfaceAcquireAction.SkipAndRetry); |
| 61 | + } |
| 62 | + |
| 63 | + [Test] |
| 64 | + public async Task OccludedAndValidationErrorsSkipWithoutSpinning() |
| 65 | + { |
| 66 | + // Occluded: the window is not visible, so a self-requested redraw would just burn CPU. |
| 67 | + // Error: the C-API validation status - a retry gets the same answer, and it must not take the |
| 68 | + // window down the way an unknown status does. |
| 69 | + await Assert.That(WebGpuSurfaceTarget.ActionFor(WebGpuSurfaceTarget.OccludedStatus)) |
| 70 | + .IsEqualTo(SurfaceAcquireAction.Skip); |
| 71 | + await Assert.That(WebGpuSurfaceTarget.ActionFor(WGPUSurfaceGetCurrentTextureStatus.Error)) |
| 72 | + .IsEqualTo(SurfaceAcquireAction.Skip); |
| 73 | + } |
| 74 | + |
| 75 | + [Test] |
| 76 | + public async Task ASuccessfulAcquirePresents() |
| 77 | + { |
| 78 | + await Assert.That(WebGpuSurfaceTarget.ActionFor(WGPUSurfaceGetCurrentTextureStatus.SuccessOptimal)) |
| 79 | + .IsEqualTo(SurfaceAcquireAction.Present); |
| 80 | + await Assert.That(WebGpuSurfaceTarget.ActionFor(WGPUSurfaceGetCurrentTextureStatus.SuccessSuboptimal)) |
| 81 | + .IsEqualTo(SurfaceAcquireAction.Present); |
| 82 | + } |
| 83 | + |
| 84 | + [Test] |
| 85 | + public async Task AnUnknownStatusIsAFailure() |
| 86 | + { |
| 87 | + // Anything the header does not define is a driver or binding bug; the acquire throws so it is |
| 88 | + // seen rather than silently dropping every frame. |
| 89 | + await Assert.That(WebGpuSurfaceTarget.ActionFor((WGPUSurfaceGetCurrentTextureStatus)0x7FFF0001)) |
| 90 | + .IsEqualTo(SurfaceAcquireAction.Fail); |
| 91 | + } |
| 92 | + |
| 93 | + [Test] |
| 94 | + public async Task SurfaceSizesAreClampedToTheDeviceLimit() |
| 95 | + { |
| 96 | + // An over-large window, or a corrupted restored size, degrades to the GPU limit instead of |
| 97 | + // failing wgpu validation; zero (minimized) becomes one. |
| 98 | + await Assert.That(WebGpuSurfaceTarget.ClampSurfaceSize(0, 0, 8192)).IsEqualTo((1u, 1u)); |
| 99 | + await Assert.That(WebGpuSurfaceTarget.ClampSurfaceSize(20000, 100, 8192)).IsEqualTo((8192u, 100u)); |
| 100 | + await Assert.That(WebGpuSurfaceTarget.ClampSurfaceSize(320, 240, 8192)).IsEqualTo((320u, 240u)); |
| 101 | + |
| 102 | + // A device that reports a zero limit still has to produce a legal (1x1) configuration. |
| 103 | + await Assert.That(WebGpuSurfaceTarget.ClampSurfaceSize(320, 240, 0)).IsEqualTo((1u, 1u)); |
| 104 | + } |
| 105 | + |
| 106 | + [Test] |
| 107 | + public async Task Bgra8IsPreferredAndSrgbIsAvoided() |
| 108 | + { |
| 109 | + // Bgra8Unorm keeps the golden images the same pixels the window shows. |
| 110 | + await Assert.That(WebGpuRenderDevice.PickSurfaceFormat(new[] |
| 111 | + { |
| 112 | + WGPUTextureFormat.RGBA8Unorm, |
| 113 | + WGPUTextureFormat.BGRA8Unorm, |
| 114 | + })) |
| 115 | + .IsEqualTo(WGPUTextureFormat.BGRA8Unorm); |
| 116 | + |
| 117 | + // Without Bgra8Unorm, any non-sRGB format beats the surface's own first preference: the 2D |
| 118 | + // stack already writes gamma-encoded bytes, so an sRGB view would encode them a second time. |
| 119 | + await Assert.That(WebGpuRenderDevice.PickSurfaceFormat(new[] |
| 120 | + { |
| 121 | + WGPUTextureFormat.BGRA8UnormSrgb, |
| 122 | + WGPUTextureFormat.RGBA8Unorm, |
| 123 | + })) |
| 124 | + .IsEqualTo(WGPUTextureFormat.RGBA8Unorm); |
| 125 | + |
| 126 | + // All-sRGB surface: nothing better exists, so take the surface's first preference. |
| 127 | + await Assert.That(WebGpuRenderDevice.PickSurfaceFormat(new[] |
| 128 | + { |
| 129 | + WGPUTextureFormat.BGRA8UnormSrgb, |
| 130 | + WGPUTextureFormat.RGBA8UnormSrgb, |
| 131 | + })) |
| 132 | + .IsEqualTo(WGPUTextureFormat.BGRA8UnormSrgb); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments