summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/tests/cts/checkout/src/webgpu/util/copy_to_texture.ts
blob: 2f751ff6a24b72d69642af406917ecd7592709fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { assert, memcpy } from '../../common/util/util.js';
import { RegularTextureFormat } from '../capability_info.js';
import { GPUTest } from '../gpu_test.js';
import { reifyExtent3D, reifyOrigin3D } from '../util/unions.js';

import { makeInPlaceColorConversion } from './color_space_conversion.js';
import { TexelView } from './texture/texel_view.js';
import { TexelCompareOptions, textureContentIsOKByT2B } from './texture/texture_ok.js';

/**
 * Predefined copy sub rect meta infos.
 */
export const kCopySubrectInfo = [
  {
    srcOrigin: { x: 2, y: 2 },
    dstOrigin: { x: 0, y: 0, z: 0 },
    srcSize: { width: 16, height: 16 },
    dstSize: { width: 4, height: 4 },
    copyExtent: { width: 4, height: 4, depthOrArrayLayers: 1 },
  },
  {
    srcOrigin: { x: 10, y: 2 },
    dstOrigin: { x: 0, y: 0, z: 0 },
    srcSize: { width: 16, height: 16 },
    dstSize: { width: 4, height: 4 },
    copyExtent: { width: 4, height: 4, depthOrArrayLayers: 1 },
  },
  {
    srcOrigin: { x: 2, y: 10 },
    dstOrigin: { x: 0, y: 0, z: 0 },
    srcSize: { width: 16, height: 16 },
    dstSize: { width: 4, height: 4 },
    copyExtent: { width: 4, height: 4, depthOrArrayLayers: 1 },
  },
  {
    srcOrigin: { x: 10, y: 10 },
    dstOrigin: { x: 0, y: 0, z: 0 },
    srcSize: { width: 16, height: 16 },
    dstSize: { width: 4, height: 4 },
    copyExtent: { width: 4, height: 4, depthOrArrayLayers: 1 },
  },
  {
    srcOrigin: { x: 2, y: 2 },
    dstOrigin: { x: 2, y: 2, z: 0 },
    srcSize: { width: 16, height: 16 },
    dstSize: { width: 16, height: 16 },
    copyExtent: { width: 4, height: 4, depthOrArrayLayers: 1 },
  },
  {
    srcOrigin: { x: 10, y: 2 },
    dstOrigin: { x: 2, y: 2, z: 0 },
    srcSize: { width: 16, height: 16 },
    dstSize: { width: 16, height: 16 },
    copyExtent: { width: 4, height: 4, depthOrArrayLayers: 1 },
  },
] as const;

export class CopyToTextureUtils extends GPUTest {
  doFlipY(
    sourcePixels: Uint8ClampedArray,
    width: number,
    height: number,
    bytesPerPixel: number
  ): Uint8ClampedArray {
    const dstPixels = new Uint8ClampedArray(width * height * bytesPerPixel);
    for (let i = 0; i < height; ++i) {
      for (let j = 0; j < width; ++j) {
        const srcPixelPos = i * width + j;
        // WebGL readPixel returns pixels from bottom-left origin. Using CopyExternalImageToTexture
        // to copy from WebGL Canvas keeps top-left origin. So the expectation from webgl.readPixel should
        // be flipped.
        const dstPixelPos = (height - i - 1) * width + j;

        memcpy(
          { src: sourcePixels, start: srcPixelPos * bytesPerPixel, length: bytesPerPixel },
          { dst: dstPixels, start: dstPixelPos * bytesPerPixel }
        );
      }
    }

    return dstPixels;
  }

  getExpectedDstPixelsFromSrcPixels({
    srcPixels,
    srcOrigin,
    srcSize,
    dstOrigin,
    dstSize,
    subRectSize,
    format,
    flipSrcBeforeCopy,
    srcDoFlipYDuringCopy,
    conversion,
  }: {
    srcPixels: Uint8ClampedArray;
    srcOrigin: GPUOrigin2D;
    srcSize: GPUExtent3D;
    dstOrigin: GPUOrigin3D;
    dstSize: GPUExtent3D;
    subRectSize: GPUExtent3D;
    format: RegularTextureFormat;
    flipSrcBeforeCopy: boolean;
    srcDoFlipYDuringCopy: boolean;
    conversion: {
      srcPremultiplied: boolean;
      dstPremultiplied: boolean;
      srcColorSpace?: PredefinedColorSpace;
      dstColorSpace?: PredefinedColorSpace;
    };
  }): TexelView {
    const applyConversion = makeInPlaceColorConversion(conversion);

    const reifySrcOrigin = reifyOrigin3D(srcOrigin);
    const reifySrcSize = reifyExtent3D(srcSize);
    const reifyDstOrigin = reifyOrigin3D(dstOrigin);
    const reifyDstSize = reifyExtent3D(dstSize);
    const reifySubRectSize = reifyExtent3D(subRectSize);

    assert(
      reifyDstOrigin.x + reifySubRectSize.width <= reifyDstSize.width &&
        reifyDstOrigin.y + reifySubRectSize.height <= reifyDstSize.height,
      'subrect is out of bounds'
    );

    const divide = 255.0;
    return TexelView.fromTexelsAsColors(
      format,
      coords => {
        assert(
          coords.x >= reifyDstOrigin.x &&
            coords.y >= reifyDstOrigin.y &&
            coords.x < reifyDstOrigin.x + reifySubRectSize.width &&
            coords.y < reifyDstOrigin.y + reifySubRectSize.height &&
            coords.z === 0,
          'out of bounds'
        );
        // Map dst coords to get candidate src pixel position in y.
        let yInSubRect = coords.y - reifyDstOrigin.y;

        // If srcDoFlipYDuringCopy is true, a flipY op has been applied to src during copy.
        // WebGPU spec requires origin option relative to the top-left corner of the source image,
        // increasing downward consistently.
        // https://www.w3.org/TR/webgpu/#dom-gpuimagecopyexternalimage-flipy
        // Flip only happens in copy rect contents and src origin always top-left.
        // Get candidate src pixel position in y by mirroring in copy sub rect.
        if (srcDoFlipYDuringCopy) yInSubRect = reifySubRectSize.height - 1 - yInSubRect;

        let src_y = yInSubRect + reifySrcOrigin.y;

        // Test might generate flipped source based on srcPixels, e.g. Create ImageBitmap based on srcPixels but set orientation to 'flipY'
        // Get candidate src pixel position in y by mirroring in source.
        if (flipSrcBeforeCopy) src_y = reifySrcSize.height - src_y - 1;

        const pixelPos =
          src_y * reifySrcSize.width + (coords.x - reifyDstOrigin.x) + reifySrcOrigin.x;

        const rgba = {
          R: srcPixels[pixelPos * 4] / divide,
          G: srcPixels[pixelPos * 4 + 1] / divide,
          B: srcPixels[pixelPos * 4 + 2] / divide,
          A: srcPixels[pixelPos * 4 + 3] / divide,
        };
        applyConversion(rgba);
        return rgba;
      },
      { clampToFormatRange: true }
    );
  }

  doTestAndCheckResult(
    imageCopyExternalImage: GPUImageCopyExternalImage,
    dstTextureCopyView: GPUImageCopyTextureTagged,
    expTexelView: TexelView,
    copySize: Required<GPUExtent3DDict>,
    texelCompareOptions: TexelCompareOptions
  ): void {
    this.device.queue.copyExternalImageToTexture(
      imageCopyExternalImage,
      dstTextureCopyView,
      copySize
    );

    const resultPromise = textureContentIsOKByT2B(
      this,
      { texture: dstTextureCopyView.texture, origin: dstTextureCopyView.origin },
      copySize,
      { expTexelView },
      texelCompareOptions
    );
    this.eventualExpectOK(resultPromise);
    this.trackForCleanup(dstTextureCopyView.texture);
  }
}