mirror of
https://github.com/denoland/deno.git
synced 2025-09-26 12:19:12 +00:00
BREAKING(webgpu/unstable): move width
and height
options to UnsafeWindowSurface
constructor (#24200)
Fixes https://github.com/denoland/deno/issues/23508 `width` and `height` are required to configure the wgpu surface because Deno is headless and depends on user to create a window. The options were non-standard extension of `GPUCanvasConfiguration#configure`. This PR adds a required options parameter with the `width` and `height` options to `Deno.UnsafeWindowSurface` constructor. ```typescript // Old, non-standard extension of GPUCanvasConfiguration const surface = new Deno.UnsafeWindowSurface("x11", displayHandle, windowHandle); const context = surface.getContext(); context.configure({ width: 600, height: 800, /* ... */ }); ``` ```typescript // New const surface = new Deno.UnsafeWindowSurface({ system: "x11", windowHandle, displayHandle, width: 600, height: 800, }); const context = surface.getContext(); context.configure({ /* ... */ }); ```
This commit is contained in:
parent
9be8dce0c7
commit
0cb00a6e89
5 changed files with 57 additions and 23 deletions
10
cli/tsc/dts/lib.deno.unstable.d.ts
vendored
10
cli/tsc/dts/lib.deno.unstable.d.ts
vendored
|
@ -28,9 +28,13 @@ declare namespace Deno {
|
||||||
*/
|
*/
|
||||||
export class UnsafeWindowSurface {
|
export class UnsafeWindowSurface {
|
||||||
constructor(
|
constructor(
|
||||||
system: "cocoa" | "win32" | "x11" | "wayland",
|
options: {
|
||||||
windowHandle: Deno.PointerValue<unknown>,
|
system: "cocoa" | "win32" | "x11" | "wayland";
|
||||||
displayHandle: Deno.PointerValue<unknown>,
|
windowHandle: Deno.PointerValue<unknown>;
|
||||||
|
displayHandle: Deno.PointerValue<unknown>;
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
},
|
||||||
);
|
);
|
||||||
getContext(context: "webgpu"): GPUCanvasContext;
|
getContext(context: "webgpu"): GPUCanvasContext;
|
||||||
present(): void;
|
present(): void;
|
||||||
|
|
2
cli/tsc/dts/lib.deno_webgpu.d.ts
vendored
2
cli/tsc/dts/lib.deno_webgpu.d.ts
vendored
|
@ -1372,8 +1372,6 @@ declare interface GPUCanvasConfiguration {
|
||||||
viewFormats?: GPUTextureFormat[];
|
viewFormats?: GPUTextureFormat[];
|
||||||
colorSpace?: "srgb" | "display-p3";
|
colorSpace?: "srgb" | "display-p3";
|
||||||
alphaMode?: GPUCanvasAlphaMode;
|
alphaMode?: GPUCanvasAlphaMode;
|
||||||
width: number;
|
|
||||||
height: number;
|
|
||||||
}
|
}
|
||||||
/** @category GPU */
|
/** @category GPU */
|
||||||
declare interface GPUCanvasContext {
|
declare interface GPUCanvasContext {
|
||||||
|
|
|
@ -7434,16 +7434,6 @@ const dictMembersGPUCanvasConfiguration = [
|
||||||
key: "presentMode",
|
key: "presentMode",
|
||||||
converter: webidl.converters["GPUPresentMode"],
|
converter: webidl.converters["GPUPresentMode"],
|
||||||
},
|
},
|
||||||
{
|
|
||||||
key: "width",
|
|
||||||
converter: webidl.converters["long"],
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: "height",
|
|
||||||
converter: webidl.converters["long"],
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
key: "viewFormats",
|
key: "viewFormats",
|
||||||
converter: webidl.createSequenceConverter(
|
converter: webidl.createSequenceConverter(
|
||||||
|
|
|
@ -29,6 +29,8 @@ const _configuration = Symbol("[[configuration]]");
|
||||||
const _canvas = Symbol("[[canvas]]");
|
const _canvas = Symbol("[[canvas]]");
|
||||||
const _currentTexture = Symbol("[[currentTexture]]");
|
const _currentTexture = Symbol("[[currentTexture]]");
|
||||||
const _present = Symbol("[[present]]");
|
const _present = Symbol("[[present]]");
|
||||||
|
const _dim = Symbol("[[dimensions]]");
|
||||||
|
|
||||||
class GPUCanvasContext {
|
class GPUCanvasContext {
|
||||||
/** @type {number} */
|
/** @type {number} */
|
||||||
[_surfaceRid];
|
[_surfaceRid];
|
||||||
|
@ -36,6 +38,7 @@ class GPUCanvasContext {
|
||||||
[_canvas];
|
[_canvas];
|
||||||
/** @type {GPUTexture | undefined} */
|
/** @type {GPUTexture | undefined} */
|
||||||
[_currentTexture];
|
[_currentTexture];
|
||||||
|
[_dim];
|
||||||
|
|
||||||
get canvas() {
|
get canvas() {
|
||||||
webidl.assertBranded(this, GPUCanvasContextPrototype);
|
webidl.assertBranded(this, GPUCanvasContextPrototype);
|
||||||
|
@ -69,8 +72,8 @@ class GPUCanvasContext {
|
||||||
format: configuration.format,
|
format: configuration.format,
|
||||||
viewFormats: configuration.viewFormats,
|
viewFormats: configuration.viewFormats,
|
||||||
usage: configuration.usage,
|
usage: configuration.usage,
|
||||||
width: configuration.width,
|
width: this[_dim].width,
|
||||||
height: configuration.height,
|
height: this[_dim].height,
|
||||||
alphaMode: configuration.alphaMode,
|
alphaMode: configuration.alphaMode,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -110,8 +113,8 @@ class GPUCanvasContext {
|
||||||
const texture = createGPUTexture(
|
const texture = createGPUTexture(
|
||||||
{
|
{
|
||||||
size: {
|
size: {
|
||||||
width: this[_configuration].width,
|
width: this[_dim].width,
|
||||||
height: this[_configuration].height,
|
height: this[_dim].height,
|
||||||
depthOrArrayLayers: 1,
|
depthOrArrayLayers: 1,
|
||||||
},
|
},
|
||||||
mipLevelCount: 1,
|
mipLevelCount: 1,
|
||||||
|
@ -163,6 +166,8 @@ function createCanvasContext(options) {
|
||||||
const canvasContext = webidl.createBranded(GPUCanvasContext);
|
const canvasContext = webidl.createBranded(GPUCanvasContext);
|
||||||
canvasContext[_surfaceRid] = options.surfaceRid;
|
canvasContext[_surfaceRid] = options.surfaceRid;
|
||||||
canvasContext[_canvas] = options.canvas;
|
canvasContext[_canvas] = options.canvas;
|
||||||
|
canvasContext[_dim] = { width: options.width, height: options.height };
|
||||||
|
|
||||||
return canvasContext;
|
return canvasContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,16 +177,34 @@ function createCanvasContext(options) {
|
||||||
class UnsafeWindowSurface {
|
class UnsafeWindowSurface {
|
||||||
#ctx;
|
#ctx;
|
||||||
#surfaceRid;
|
#surfaceRid;
|
||||||
|
#options;
|
||||||
|
|
||||||
constructor(system, win, display) {
|
constructor(options) {
|
||||||
this.#surfaceRid = op_webgpu_surface_create(system, win, display);
|
if (typeof options !== "object") {
|
||||||
|
throw new TypeError("options must be provided.");
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
typeof options.width !== "number" || typeof options.height !== "number"
|
||||||
|
) {
|
||||||
|
throw new TypeError("width and height must be provided.");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.#surfaceRid = op_webgpu_surface_create(
|
||||||
|
options.system,
|
||||||
|
options.windowHandle,
|
||||||
|
options.displayHandle,
|
||||||
|
);
|
||||||
|
this.#options = options;
|
||||||
}
|
}
|
||||||
|
|
||||||
getContext(context) {
|
getContext(context) {
|
||||||
if (context !== "webgpu") {
|
if (context !== "webgpu") {
|
||||||
throw new TypeError("Only 'webgpu' context is supported");
|
throw new TypeError("Only 'webgpu' context is supported");
|
||||||
}
|
}
|
||||||
this.#ctx = createCanvasContext({ surfaceRid: this.#surfaceRid });
|
this.#ctx = createCanvasContext({
|
||||||
|
surfaceRid: this.#surfaceRid,
|
||||||
|
...this.#options,
|
||||||
|
});
|
||||||
return this.#ctx;
|
return this.#ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -236,13 +236,32 @@ Deno.test({
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(
|
||||||
() => {
|
() => {
|
||||||
new Deno.UnsafeWindowSurface("cocoa", null, null);
|
new Deno.UnsafeWindowSurface({
|
||||||
|
system: "cocoa",
|
||||||
|
windowHandle: null,
|
||||||
|
displayHandle: null,
|
||||||
|
width: 0,
|
||||||
|
height: 0,
|
||||||
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
device.destroy();
|
device.destroy();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test(function webgpuWindowSurfaceNoWidthHeight() {
|
||||||
|
assertThrows(
|
||||||
|
() => {
|
||||||
|
// @ts-expect-error width and height are required
|
||||||
|
new Deno.UnsafeWindowSurface({
|
||||||
|
system: "x11",
|
||||||
|
windowHandle: null,
|
||||||
|
displayHandle: null,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
Deno.test(function getPreferredCanvasFormat() {
|
Deno.test(function getPreferredCanvasFormat() {
|
||||||
const preferredFormat = navigator.gpu.getPreferredCanvasFormat();
|
const preferredFormat = navigator.gpu.getPreferredCanvasFormat();
|
||||||
assert(preferredFormat === "bgra8unorm" || preferredFormat === "rgba8unorm");
|
assert(preferredFormat === "bgra8unorm" || preferredFormat === "rgba8unorm");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue