Skip to content
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

fix(ext/webgpu): GPUDevice.createTexture works when the argument descriptor.size is an Array #23413

Merged
merged 20 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions cli/tsc/dts/lib.deno_webgpu.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ declare type GPUBufferUsageFlags = number;
/** @category WebGPU */
declare type GPUFlagsConstant = number;

/** @category WebGPU */
declare type GPUImageCopyExternalImageSource =
| ImageBitmap
| ImageData
| GPUCanvasContext;
Hajime-san marked this conversation as resolved.
Show resolved Hide resolved

/** @category WebGPU */
declare class GPUBufferUsage {
static MAP_READ: 0x0001;
Expand Down Expand Up @@ -908,6 +914,13 @@ declare interface GPUImageCopyBuffer extends GPUImageDataLayout {
buffer: GPUBuffer;
}

/** @category WebGPU */
declare interface GPUImageCopyExternalImage {
source: GPUImageCopyExternalImageSource;
origin?: GPUOrigin2D;
flipY?: boolean;
}

/** @category WebGPU */
declare interface GPUImageCopyTexture {
texture: GPUTexture;
Expand All @@ -916,6 +929,12 @@ declare interface GPUImageCopyTexture {
aspect?: GPUTextureAspect;
}

/** @category WebGPU */
declare interface GPUImageCopyTextureTagged extends GPUImageCopyTexture {
colorSpace?: PredefinedColorSpace;
premultipliedAlpha?: boolean;
}

/** @category WebGPU */
interface GPUProgrammablePassEncoder {
setBindGroup(
Expand Down Expand Up @@ -1236,6 +1255,12 @@ declare class GPUQueue implements GPUObjectBase {
dataLayout: GPUImageDataLayout,
size: GPUExtent3D,
): undefined;

copyExternalImageToTexture(
source: GPUImageCopyExternalImage,
destination: GPUImageCopyTextureTagged,
copySize: GPUExtent3D,
): undefined;
}

/** @category WebGPU */
Expand Down Expand Up @@ -1295,6 +1320,15 @@ declare interface GPUColorDict {
/** @category WebGPU */
declare type GPUColor = number[] | GPUColorDict;

/** @category WebGPU */
declare interface GPUOrigin2DDict {
x?: number;
y?: number;
}

/** @category WebGPU */
declare type GPUOrigin2D = number[] | GPUOrigin2DDict;

/** @category WebGPU */
declare interface GPUOrigin3DDict {
x?: number;
Expand Down
49 changes: 48 additions & 1 deletion ext/webgpu/01_webgpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -1126,10 +1126,11 @@ class GPUDevice extends EventTarget {
"Argument 1",
);
const device = assertDevice(this, prefix, "this");
// assign normalized size to descriptor due to createGPUTexture needs it
descriptor.size = normalizeGPUExtent3D(descriptor.size);
const { rid, err } = op_webgpu_create_texture({
deviceRid: device.rid,
...descriptor,
size: normalizeGPUExtent3D(descriptor.size),
});
device.pushError(err);

Expand Down Expand Up @@ -1849,6 +1850,15 @@ class GPUQueue {
device.pushError(err);
}

/**
* @param {GPUImageCopyExternalImage} source
* @param {GPUImageCopyTextureTagged} destination
* @param {GPUExtent3D} copySize
*/
copyExternalImageToTexture(source, destination, copySize) {
throw new Error("not implemented");
}

[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
return inspect(
createFilteredInspectProxy({
Expand Down Expand Up @@ -5303,6 +5313,16 @@ webidl.converters["GPUExtent3D"] = (V, opts) => {
if (typeof V === "object") {
const method = V[SymbolIterator];
if (method !== undefined) {
// validate length of GPUExtent3D
const min = 1;
const max = 3;
if (V.length < min || V.length > max) {
throw webidl.makeException(
TypeError,
`A sequence of number used as a GPUExtent3D must have between ${min} and ${max} elements.`,
opts,
);
crowlKats marked this conversation as resolved.
Show resolved Hide resolved
}
return webidl.converters["sequence<GPUIntegerCoordinate>"](V, opts);
}
return webidl.converters["GPUExtent3DDict"](V, opts);
Expand Down Expand Up @@ -6635,6 +6655,15 @@ webidl.converters["GPUOrigin3D"] = (V, opts) => {
if (typeof V === "object") {
const method = V[SymbolIterator];
if (method !== undefined) {
// validate length of GPUOrigin3D
const length = 3;
if (V.length > length) {
throw webidl.makeException(
TypeError,
`A sequence of number used as a GPUOrigin3D must have at most ${length} elements.`,
opts,
);
}
return webidl.converters["sequence<GPUIntegerCoordinate>"](V, opts);
}
return webidl.converters["GPUOrigin3DDict"](V, opts);
Expand Down Expand Up @@ -6703,6 +6732,15 @@ webidl.converters["GPUOrigin2D"] = (V, opts) => {
if (typeof V === "object") {
const method = V[SymbolIterator];
if (method !== undefined) {
// validate length of GPUOrigin2D
const length = 2;
if (V.length > length) {
throw webidl.makeException(
TypeError,
`A sequence of number used as a GPUOrigin2D must have at most ${length} elements.`,
opts,
);
}
return webidl.converters["sequence<GPUIntegerCoordinate>"](V, opts);
}
return webidl.converters["GPUOrigin2DDict"](V, opts);
Expand Down Expand Up @@ -6788,6 +6826,15 @@ webidl.converters["GPUColor"] = (V, opts) => {
if (typeof V === "object") {
const method = V[SymbolIterator];
if (method !== undefined) {
// validate length of GPUColor
const length = 4;
if (V.length !== length) {
throw webidl.makeException(
TypeError,
`A sequence of number used as a GPUColor must have exactly ${length} elements.`,
opts,
);
}
return webidl.converters["sequence<double>"](V, opts);
}
return webidl.converters["GPUColorDict"](V, opts);
Expand Down
219 changes: 219 additions & 0 deletions tests/unit/webgpu_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,225 @@ Deno.test(function getPreferredCanvasFormat() {
assert(preferredFormat === "bgra8unorm" || preferredFormat === "rgba8unorm");
});

Deno.test({
ignore: isWsl || isLinuxOrMacCI,
}, async function validateGPUColor() {
const adapter = await navigator.gpu.requestAdapter();
assert(adapter);
const device = await adapter.requestDevice();
assert(device);

const format = "rgba8unorm-srgb";
const encoder = device.createCommandEncoder();
const texture = device.createTexture({
size: [256, 256],
format,
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC,
});
const view = texture.createView();
const storeOp = "store";
const loadOp = "clear";

// values for validating GPUColor
const invalidSize = [0, 0, 0];

// validate the argument of descriptor.colorAttachments[@@iterator].clearValue property's length of GPUCommandEncoder.beginRenderPass when its a sequence
// https://www.w3.org/TR/2024/WD-webgpu-20240409/#dom-gpucommandencoder-beginrenderpass
assertThrows(
() =>
encoder.beginRenderPass({
colorAttachments: [
{
view,
storeOp,
loadOp,
clearValue: invalidSize,
},
],
}),
);
const renderPass = encoder.beginRenderPass({
colorAttachments: [
{
view,
storeOp,
loadOp,
clearValue: [0, 0, 0, 1],
},
],
});
// validate the argument of color length of GPURenderPassEncoder.setBlendConstant when its a sequence
// https://www.w3.org/TR/2024/WD-webgpu-20240409/#dom-gpurenderpassencoder-setblendconstant
assertThrows(
() => renderPass.setBlendConstant(invalidSize),
);

device.destroy();
const resources = Object.keys(Deno.resources());
Deno.close(Number(resources[resources.length - 1]));
});

Deno.test({
ignore: isWsl || isLinuxOrMacCI,
}, async function validateGPUExtent3D() {
const adapter = await navigator.gpu.requestAdapter();
assert(adapter);
const device = await adapter.requestDevice();
assert(device);

const format = "rgba8unorm-srgb";
const encoder = device.createCommandEncoder();
const buffer = device.createBuffer({
size: new Uint32Array([1, 4, 3, 295]).byteLength,
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
});
const usage = GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC;
const texture = device.createTexture({
size: [256, 256],
format,
usage,
});

// values for validating GPUExtent3D
const belowSize: Array<number> = [];
const overSize = [256, 256, 1, 1];

// validate the argument of descriptor.size property's length of GPUDevice.createTexture when its a sequence
// https://www.w3.org/TR/2024/WD-webgpu-20240409/#dom-gpudevice-createtexture
assertThrows(
() => device.createTexture({ size: belowSize, format, usage }),
);
assertThrows(
() => device.createTexture({ size: overSize, format, usage }),
);
// validate the argument of copySize property's length of GPUCommandEncoder.copyBufferToTexture when its a sequence
// https://www.w3.org/TR/2024/WD-webgpu-20240409/#dom-gpucommandencoder-copybuffertotexture
assertThrows(
() => encoder.copyBufferToTexture({ buffer }, { texture }, belowSize),
);
assertThrows(
() => encoder.copyBufferToTexture({ buffer }, { texture }, overSize),
);
// validate the argument of copySize property's length of GPUCommandEncoder.copyTextureToBuffer when its a sequence
// https://www.w3.org/TR/2024/WD-webgpu-20240409/#dom-gpucommandencoder-copytexturetobuffer
assertThrows(
() => encoder.copyTextureToBuffer({ texture }, { buffer }, belowSize),
);
assertThrows(
() => encoder.copyTextureToBuffer({ texture }, { buffer }, overSize),
);
// validate the argument of copySize property's length of GPUCommandEncoder.copyTextureToTexture when its a sequence
// https://www.w3.org/TR/2024/WD-webgpu-20240409/#dom-gpucommandencoder-copytexturetotexture
assertThrows(
() => encoder.copyTextureToTexture({ texture }, { texture }, belowSize),
);
assertThrows(
() => encoder.copyTextureToTexture({ texture }, { texture }, overSize),
);
const data = new Uint8Array([1 * 255, 1 * 255, 1 * 255, 1 * 255]);
// validate the argument of size property's length of GPUQueue.writeTexture when its a sequence
// https://www.w3.org/TR/2024/WD-webgpu-20240409/#dom-gpuqueue-writetexture
assertThrows(
() => device.queue.writeTexture({ texture }, data, {}, belowSize),
);
assertThrows(
() => device.queue.writeTexture({ texture }, data, {}, overSize),
);
// NOTE: GPUQueue.copyExternalImageToTexture needs to be validated the argument of copySize property's length when its a sequence, but it is not implemented yet

device.destroy();
const resources = Object.keys(Deno.resources());
Deno.close(Number(resources[resources.length - 1]));
});

Deno.test({
ignore: true,
}, async function validateGPUOrigin2D() {
// NOTE: GPUQueue.copyExternalImageToTexture needs to be validated the argument of source.origin property's length when its a sequence, but it is not implemented yet
});

Deno.test({
ignore: isWsl || isLinuxOrMacCI,
}, async function validateGPUOrigin3D() {
const adapter = await navigator.gpu.requestAdapter();
assert(adapter);
const device = await adapter.requestDevice();
assert(device);

const format = "rgba8unorm-srgb";
const encoder = device.createCommandEncoder();
const buffer = device.createBuffer({
size: new Uint32Array([1, 4, 3, 295]).byteLength,
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
});
const usage = GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC;
const size = [256, 256, 1];
const texture = device.createTexture({
size,
format,
usage,
});

// value for validating GPUOrigin3D
const overSize = [256, 256, 1, 1];

// validate the argument of destination.origin property's length of GPUCommandEncoder.copyBufferToTexture when its a sequence
// https://www.w3.org/TR/2024/WD-webgpu-20240409/#dom-gpucommandencoder-copybuffertotexture
assertThrows(
() =>
encoder.copyBufferToTexture(
{ buffer },
{ texture, origin: overSize },
size,
),
);
// validate the argument of source.origin property's length of GPUCommandEncoder.copyTextureToBuffer when its a sequence
// https://www.w3.org/TR/2024/WD-webgpu-20240409/#dom-gpucommandencoder-copytexturetobuffer
assertThrows(
() =>
encoder.copyTextureToBuffer(
{ texture, origin: overSize },
{ buffer },
size,
),
);
// validate the argument of source.origin property's length of GPUCommandEncoder.copyTextureToTexture when its a sequence
// https://www.w3.org/TR/2024/WD-webgpu-20240409/#dom-gpucommandencoder-copytexturetotexture
assertThrows(
() =>
encoder.copyTextureToTexture(
{ texture, origin: overSize },
{ texture },
size,
),
);
// validate the argument of destination.origin property's length of GPUCommandEncoder.copyTextureToTexture when its a sequence
assertThrows(
() =>
encoder.copyTextureToTexture(
{ texture },
{ texture, origin: overSize },
size,
),
);
// validate the argument of destination.origin property's length of GPUQueue.writeTexture when its a sequence
// https://www.w3.org/TR/2024/WD-webgpu-20240409/#dom-gpuqueue-writetexture
assertThrows(
() =>
device.queue.writeTexture(
{ texture, origin: overSize },
new Uint8Array([1 * 255, 1 * 255, 1 * 255, 1 * 255]),
{},
size,
),
);
// NOTE: GPUQueue.copyExternalImageToTexture needs to be validated the argument of destination.origin property's length when its a sequence, but it is not implemented yet

device.destroy();
const resources = Object.keys(Deno.resources());
Deno.close(Number(resources[resources.length - 1]));
});

async function checkIsWsl() {
return Deno.build.os === "linux" && await hasMicrosoftProcVersion();

Expand Down