diff options
Diffstat (limited to 'dom/webgpu/tests/cts/checkout/src/webgpu/util/texture.ts')
-rw-r--r-- | dom/webgpu/tests/cts/checkout/src/webgpu/util/texture.ts | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/dom/webgpu/tests/cts/checkout/src/webgpu/util/texture.ts b/dom/webgpu/tests/cts/checkout/src/webgpu/util/texture.ts new file mode 100644 index 0000000000..d26508878f --- /dev/null +++ b/dom/webgpu/tests/cts/checkout/src/webgpu/util/texture.ts @@ -0,0 +1,61 @@ +import { assert } from '../../common/util/util.js'; +import { kTextureFormatInfo } from '../capability_info.js'; + +import { align } from './math.js'; +import { TexelView } from './texture/texel_view.js'; +import { reifyExtent3D } from './unions.js'; + +/** + * Creates a texture with the contents of a TexelView. + */ +export function makeTextureWithContents( + device: GPUDevice, + texelView: TexelView, + desc: Omit<GPUTextureDescriptor, 'format'> +): GPUTexture { + const { width, height, depthOrArrayLayers } = reifyExtent3D(desc.size); + + const { bytesPerBlock, blockWidth } = kTextureFormatInfo[texelView.format]; + // Currently unimplemented for compressed textures. + assert(blockWidth === 1); + + // Compute bytes per row. + const bytesPerRow = align(bytesPerBlock * width, 256); + + // Create a staging buffer to upload the texture contents. + const stagingBuffer = device.createBuffer({ + mappedAtCreation: true, + size: bytesPerRow * height * depthOrArrayLayers, + usage: GPUBufferUsage.COPY_SRC, + }); + + // Write the texels into the staging buffer. + texelView.writeTextureData(new Uint8Array(stagingBuffer.getMappedRange()), { + bytesPerRow, + rowsPerImage: height, + subrectOrigin: [0, 0, 0], + subrectSize: [width, height, depthOrArrayLayers], + }); + stagingBuffer.unmap(); + + // Create the texture. + const texture = device.createTexture({ + ...desc, + format: texelView.format, + usage: desc.usage | GPUTextureUsage.COPY_DST, + }); + + // Copy from the staging buffer into the texture. + const commandEncoder = device.createCommandEncoder(); + commandEncoder.copyBufferToTexture( + { buffer: stagingBuffer, bytesPerRow }, + { texture }, + desc.size + ); + device.queue.submit([commandEncoder.finish()]); + + // Clean up the staging buffer. + stagingBuffer.destroy(); + + return texture; +} |