Skip to content

Fix seals inside layers, gradient repetition and WebGPU scratch bounds - #428

Open
JimBobSquarePants wants to merge 6 commits into
mainfrom
js/gradient-repeat-scene-fixes
Open

JimBobSquarePants wants to merge 6 commits into
mainfrom
js/gradient-repeat-scene-fixes

Conversation

@JimBobSquarePants

@JimBobSquarePants JimBobSquarePants commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull-requests open
  • I have verified that I am following matches the existing coding patterns and practice as demonstrated in the repository. These follow strict Stylecop rules 👮.
  • I have provided test coverage for my change (where applicable)

Description

This PR corrects three faults in DrawingCanvas. It also changes how the WebGPU backend sets the size of its scratch buffers.

Calls inside a layer

A Flush, RenderScene, CreateScene or CopyPixelsFrom call between SaveLayer and Restore broke the layer:

  • The WebGPU backend threw an exception.
  • The CPU backend drew the later content outside the layer.
  • A layer with an Apply call lost its earlier content.

Now these calls keep the layer whole on the two backends:

  • Flush does nothing while a layer is open.
  • RenderScene draws the scene into the open layer.
  • CreateScene closes the open layer at the end of the scene. Then the canvas opens the layer again.
  • CopyPixelsFrom reads the source rectangle and draws it into the open layer.

Scenes in CreateScene

CreateScene lost the scenes that RenderScene recorded before it. Now the new scene contains these scenes, in their recorded order.

Gradient repetition

With Repeat or Reflect, a position before the start point got the color of the first stop. Now the two modes repeat the gradient in the two directions. The CPU backend and the WebGPU shaders use the same rule.

WebGPU scratch buffers

The encoder calculates an upper bound for each scratch buffer. If all the bounds hold, a render submits its work and returns. It does not read back the scratch status.

A scene with ordered work, for example Apply, still reads the status. If an offscreen render overflows a buffer, the backend renders the scene again with larger buffers.

With 100,000 lines, the flush call takes approximately 45 ms. On main, the same call takes 97 ms. The time for each frame does not change.

The fine stage adds pixel coverage as fixed-point values. Thus, the order of the GPU work does not change the output.

Other changes

  • The bounds of a LineJoin.Miter stroke now include the clipped bevel point.
  • The DrawingOptions.Transform documentation no longer says that strokes expand in local space.
  • The zxing-cpp decode tests keep the pixel array pinned until the read returns. Without the pin, a garbage collection can move the array during the read. The Aztec tests failed at random because of this fault.

The public API does not change. Some WebGPU reference images change. The new images match the CPU reference images.

Tests on Windows: net10.0 and net11.0 have 4414 passed, 0 failed and 9 skipped each.

Gradient brushes with Repeat and Reflect now repeat before the start point. Repeat subtracts the floor of the position, and Reflect uses the magnitude of the position. The WebGPU shaders use the same rules.

Flush, RenderScene, CreateScene and CopyPixelsFrom inside an open layer keep the layer whole. A retained scene renders inside a layer on both backends. The WebGPU encoder reads it in place through a scene segment table. CopyPixelsFrom inside a layer reads the source rectangle with ReadRegion and draws the image into the layer with Src.

The WebGPU encoder computes upper bounds for the scratch buffers, with row bounds for tile-row chunks and blend depth regions for the blend spill. fine adds the coverage of each pixel in fixed point, so the output does not depend on the GPU scheduling order, and it equals the CPU output.

An offscreen WebGPU render checks its scratch status before it returns, and it renders again with larger buffers after an overflow. The deferred corrective re-render is removed, so a scene releases its resources when it is disposed. After an overflow, fine writes the backdrop, so a deferred presentation frame keeps the previous pixels.

Each backend finds the retained scenes with ordered work through DrawingCommandBatch.FindOrderedScenes. The public API does not change.

Tests cover each seal inside a layer on both backends, the gradient repetition, the scratch bounds, and a 100k-line offscreen render that overflows and chunks. The WebGPU reference images that held the old output are updated: the new WebGPU output equals the CPU output.
A plain WebGPU render now submits its work and returns. It reads no scratch status when every capacity is an upper bound of the GPU use. Ordered ranges, a binning bound above the binding limit, and chunked scenes without row bounds still read the status. At 100k lines the flush call takes about 45 ms against 97 ms on the baseline, and the time per frame is the same.

The bounds are now true upper bounds:
- The row bounds keep one entry for each tile row, so a chunk shorter than one bin gets the bound of its own rows.
- The stroke style flags hold the CPU arc count, and the shader limits each round arc to it.
- A long inner join adds its own crossing bound, and the inner miter uses the shader limit.
- The PTCL bound comes from the chunk allocation in coarse.
- Each clip estimate grows by one pixel on each side.
- The outline radius of a Miter join is sqrt(m^2 + 1) half widths, which is the clipped bevel point of both strokers.
- path_count makes the y clip exact, so each line adds at most its tile count and the row index stays in the bbox.

A test renders 100k lines in chunks shorter than one bin and compares them with an unchunked render. The layer tests compare their pixels with the CPU backend.
The decode tests failed at random on Aztec symbols. The native ImageView of zxing-cpp keeps the pointer to the pixels, and BarcodeReader.Read reads through it later. The P/Invoke of the ImageView constructor pins the array only during that call, so a garbage collection between the two calls can move the array. The reader then reads other memory.

ReadBarcodes pins the array with fixed until the read returns, and both decode call sites use it.
…tation

The documentation said that the pen expands in local geometry space and that the outline is transformed after. Both backends stroke the transformed path in device space, with the pen width multiplied by the square root of the absolute determinant of the transform. The documentation now describes this behavior.
The documentation gives only the observable contract of the property: the transform that applies to vector output before rasterization.
The CopyPixelsFromSelf case composites green 128 at 50% over white. The exact green value is 191.5, which is a rounding tie. The WebGPU backend on macOS gives 191, and the reference image has 192. The difference is 0.0184% in 96 pixels.

The reference comparison of this case now uses 0.0185%, which is the measured difference plus one unit. The other cases keep 0.0003%.
@codecov

codecov Bot commented Oct 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.11584% with 163 lines in your changes missing coverage. Please review.
✅ Project coverage is 80%. Comparing base (cef01ae) to head (4c88950).

Files with missing lines Patch % Lines
...c/ImageSharp.Drawing.WebGPU/WebGPUSceneDispatch.cs 76% 39 Missing and 30 partials ⚠️
.../ImageSharp.Drawing.WebGPU/WebGPUDrawingBackend.cs 61% 18 Missing and 3 partials ⚠️
.../ImageSharp.Drawing.WebGPU/WebGPUSceneResources.cs 85% 12 Missing and 7 partials ⚠️
...Drawing/Processing/DrawingCanvasBatcher{TPixel}.cs 88% 8 Missing and 9 partials ⚠️
...ageSharp.Drawing/Processing/Backends/FlushScene.cs 88% 3 Missing and 5 partials ⚠️
...arp.Drawing/Processing/Backends/IDrawingBackend.cs 61% 5 Missing and 2 partials ⚠️
...Drawing/Processing/Backends/DrawingCommandBatch.cs 88% 2 Missing and 4 partials ⚠️
...arp.Drawing.WebGPU/WebGPUDrawingBackend.Ordered.cs 90% 3 Missing and 1 partial ⚠️
...eSharp.Drawing.WebGPU/WebGPUDrawingBackendScene.cs 80% 1 Missing and 2 partials ⚠️
.../ImageSharp.Drawing.WebGPU/WebGPUSceneRowBounds.cs 97% 1 Missing and 1 partial ⚠️
... and 5 more
Additional details and impacted files
@@           Coverage Diff           @@
##            main    #428     +/-   ##
=======================================
  Coverage     80%     80%             
=======================================
  Files        436     439      +3     
  Lines      48135   49840   +1705     
  Branches    5632    5868    +236     
=======================================
+ Hits       38577   40131   +1554     
- Misses      8030    8112     +82     
- Partials    1528    1597     +69     
Flag Coverage Δ
unittests 80% <86%> (+<1%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant