Repository navigation
Add test case for copyImageBitmapToTexture #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| export const description = ` | ||
| copy imageBitmap To texture tests. | ||
| `; | ||
|
|
||
| import { TestGroup, pcombine, poptions } from '../../framework/index.js'; | ||
|
|
||
| import { GPUTest } from './gpu_test.js'; | ||
|
|
||
| function calculateRowPitch(width: number, bytesPerPixel: number): number { | ||
| const bytesPerRow = width * bytesPerPixel; | ||
| // Rounds up to a multiple of 256 according to WebGPU requirements. | ||
| return (((bytesPerRow - 1) >> 8) + 1) << 8; | ||
| } | ||
|
|
||
| class F extends GPUTest { | ||
| checkCopyImageBitmapResult( | ||
| src: GPUBuffer, | ||
| expected: ArrayBufferView, | ||
| width: number, | ||
| height: number, | ||
| bytesPerPixel: number | ||
| ): void { | ||
| const exp = new Uint8Array(expected.buffer, expected.byteOffset, expected.byteLength); | ||
| const rowPitch = calculateRowPitch(width, bytesPerPixel); | ||
| const dst = this.createCopyForMapRead(src, rowPitch * height); | ||
|
|
||
| this.eventualAsyncExpectation(async niceStack => { | ||
| const actual = new Uint8Array(await dst.mapReadAsync()); | ||
| const check = this.checkBufferWithRowPitch( | ||
| actual, | ||
| exp, | ||
| width, | ||
| height, | ||
| rowPitch, | ||
| bytesPerPixel | ||
| ); | ||
| if (check !== undefined) { | ||
| niceStack.message = check; | ||
| this.rec.fail(niceStack); | ||
| } | ||
| dst.destroy(); | ||
| }); | ||
| } | ||
|
|
||
| checkBufferWithRowPitch( | ||
| actual: Uint8Array, | ||
| exp: Uint8Array, | ||
| width: number, | ||
| height: number, | ||
| rowPitch: number, | ||
| bytesPerPixel: number | ||
| ): string | undefined { | ||
| const lines = []; | ||
| let failedPixels = 0; | ||
| for (let i = 0; i < height; ++i) { | ||
| const bytesPerRow = width * bytesPerPixel; | ||
| for (let j = 0; j < bytesPerRow; ++j) { | ||
| const indexExp = j + i * bytesPerRow; | ||
| const indexActual = j + rowPitch * i; | ||
| if (actual[indexActual] !== exp[indexExp]) { | ||
| if (failedPixels > 4) { | ||
| break; | ||
| } | ||
| failedPixels++; | ||
| lines.push(`at [${indexExp}], expected ${exp[indexExp]}, got ${actual[indexActual]}`); | ||
| } | ||
| } | ||
| if (failedPixels > 4) { | ||
| lines.push('... and more'); | ||
| break; | ||
| } | ||
| } | ||
| return failedPixels > 0 ? lines.join('\n') : undefined; | ||
| } | ||
|
|
||
| // Using drawImage to extract imageBitmap content. | ||
| imageBitmapToData(imageBitmap: ImageBitmap): ArrayBufferView { | ||
| const imageCanvas = document.createElement('canvas'); | ||
| imageCanvas.width = imageBitmap.width; | ||
| imageCanvas.height = imageBitmap.height; | ||
|
|
||
| const imageCanvasContext = imageCanvas.getContext('2d'); | ||
| if (!imageCanvasContext) { | ||
| throw new Error('Cannot create canvas context for reading back contents from imageBitmap.'); | ||
| } | ||
|
|
||
| imageCanvasContext.drawImage(imageBitmap, 0, 0, imageBitmap.width, imageBitmap.height); | ||
| return imageCanvasContext.getImageData(0, 0, imageBitmap.width, imageBitmap.height).data; | ||
| } | ||
| } | ||
|
kainino0x marked this conversation as resolved.
Resolved
|
||
|
|
||
| export const g = new TestGroup(F); | ||
|
|
||
| g.test('from image element', async t => { | ||
| const { width, height } = t.params; | ||
|
|
||
| // The texture format is rgba8uint, so the bytes per pixel is 4. | ||
| const bytesPerPixel = 4; | ||
|
|
||
| const imagePixels = new Uint8ClampedArray(bytesPerPixel * width * height); | ||
| for (let i = 0; i < width * height * bytesPerPixel; ++i) { | ||
| imagePixels[i] = i % 4 === 3 ? 255 : i % 256; | ||
| } | ||
|
|
||
| const imageData = new ImageData(imagePixels, width, height); | ||
| const imageBitmap = await createImageBitmap(imageData); | ||
|
|
||
| const dst = t.device.createTexture({ | ||
| size: { | ||
| width: imageBitmap.width, | ||
| height: imageBitmap.height, | ||
| depth: 1, | ||
| }, | ||
| format: 'rgba8uint', | ||
| usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.COPY_SRC, | ||
| }); | ||
|
|
||
| t.device.defaultQueue.copyImageBitmapToTexture( | ||
| { | ||
| imageBitmap, | ||
| origin: { | ||
| x: 0, | ||
| y: 0, | ||
| }, | ||
| }, | ||
| { | ||
| texture: dst, | ||
| }, | ||
| { | ||
| width: imageBitmap.width, | ||
| height: imageBitmap.height, | ||
| depth: 1, | ||
| } | ||
| ); | ||
|
|
||
| const data = t.imageBitmapToData(imageBitmap); | ||
|
|
||
| const rowPitchValue = calculateRowPitch(imageBitmap.width, bytesPerPixel); | ||
| const testBuffer = t.device.createBuffer({ | ||
| size: rowPitchValue * imageBitmap.height, | ||
| usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST, | ||
| }); | ||
|
|
||
| const encoder = t.device.createCommandEncoder(); | ||
|
|
||
| encoder.copyTextureToBuffer( | ||
| { texture: dst, mipLevel: 0, origin: { x: 0, y: 0, z: 0 } }, | ||
| { buffer: testBuffer, rowPitch: rowPitchValue, imageHeight: 0 }, | ||
| { width: imageBitmap.width, height: imageBitmap.height, depth: 1 } | ||
| ); | ||
| t.device.defaultQueue.submit([encoder.finish()]); | ||
|
|
||
| t.checkCopyImageBitmapResult( | ||
| testBuffer, | ||
| data, | ||
| imageBitmap.width, | ||
| imageBitmap.height, | ||
| bytesPerPixel | ||
| ); | ||
| }).params( | ||
| pcombine( | ||
| poptions('width', [1, 2, 4, 15, 255, 256]), // | ||
| poptions('height', [1, 2, 4, 15, 255, 256]) | ||
| ) | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.