summaryrefslogtreecommitdiff
path: root/ext/webgpu/02_surface.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/webgpu/02_surface.js')
-rw-r--r--ext/webgpu/02_surface.js107
1 files changed, 18 insertions, 89 deletions
diff --git a/ext/webgpu/02_surface.js b/ext/webgpu/02_surface.js
index fb767e52a..9ae7fb54d 100644
--- a/ext/webgpu/02_surface.js
+++ b/ext/webgpu/02_surface.js
@@ -11,22 +11,16 @@ const ops = core.ops;
import * as webidl from "ext:deno_webidl/00_webidl.js";
import { createFilteredInspectProxy } from "ext:deno_console/01_console.js";
const { Symbol, SymbolFor, ObjectPrototypeIsPrototypeOf } = primordials;
-import {
- _device,
- assertDevice,
- createGPUTexture,
- GPUTextureUsage,
-} from "ext:deno_webgpu/01_webgpu.js";
+import { loadWebGPU, webgpu } from "ext:deno_webgpu/00_init.js";
const _surfaceRid = Symbol("[[surfaceRid]]");
const _configuration = Symbol("[[configuration]]");
const _canvas = Symbol("[[canvas]]");
const _currentTexture = Symbol("[[currentTexture]]");
+const _present = Symbol("[[present]]");
class GPUCanvasContext {
/** @type {number} */
[_surfaceRid];
- /** @type {InnerGPUDevice} */
- [_device];
[_configuration];
[_canvas];
/** @type {GPUTexture | undefined} */
@@ -50,6 +44,7 @@ class GPUCanvasContext {
context: "Argument 1",
});
+ const { _device, assertDevice } = webgpu;
this[_device] = configuration.device[_device];
this[_configuration] = configuration;
const device = assertDevice(this, {
@@ -72,6 +67,8 @@ class GPUCanvasContext {
}
unconfigure() {
+ const { _device } = webgpu;
+
webidl.assertBranded(this, GPUCanvasContextPrototype);
this[_configuration] = null;
@@ -86,6 +83,7 @@ class GPUCanvasContext {
if (this[_configuration] === null) {
throw new DOMException("context is not configured.", "InvalidStateError");
}
+ const { createGPUTexture, assertDevice } = webgpu;
const device = assertDevice(this, { prefix, context: "this" });
@@ -119,8 +117,10 @@ class GPUCanvasContext {
return texture;
}
- // Extended from spec. Required to present the texture; browser don't need this.
- present() {
+ // Required to present the texture; browser don't need this.
+ [_present]() {
+ const { assertDevice } = webgpu;
+
webidl.assertBranded(this, GPUCanvasContextPrototype);
const prefix = "Failed to execute 'present' on 'GPUCanvasContext'";
const device = assertDevice(this[_currentTexture], {
@@ -148,88 +148,17 @@ class GPUCanvasContext {
const GPUCanvasContextPrototype = GPUCanvasContext.prototype;
function createCanvasContext(options) {
+ // lazy load webgpu if needed
+ loadWebGPU();
+
const canvasContext = webidl.createBranded(GPUCanvasContext);
canvasContext[_surfaceRid] = options.surfaceRid;
canvasContext[_canvas] = options.canvas;
return canvasContext;
}
-// Converters
-
-// ENUM: GPUCanvasAlphaMode
-webidl.converters["GPUCanvasAlphaMode"] = webidl.createEnumConverter(
- "GPUCanvasAlphaMode",
- [
- "opaque",
- "premultiplied",
- ],
-);
-
-// NON-SPEC: ENUM: GPUPresentMode
-webidl.converters["GPUPresentMode"] = webidl.createEnumConverter(
- "GPUPresentMode",
- [
- "autoVsync",
- "autoNoVsync",
- "fifo",
- "fifoRelaxed",
- "immediate",
- "mailbox",
- ],
-);
-
-// DICT: GPUCanvasConfiguration
-const dictMembersGPUCanvasConfiguration = [
- { key: "device", converter: webidl.converters.GPUDevice, required: true },
- {
- key: "format",
- converter: webidl.converters.GPUTextureFormat,
- required: true,
- },
- {
- key: "usage",
- converter: webidl.converters["GPUTextureUsageFlags"],
- defaultValue: GPUTextureUsage.RENDER_ATTACHMENT,
- },
- {
- key: "alphaMode",
- converter: webidl.converters["GPUCanvasAlphaMode"],
- defaultValue: "opaque",
- },
-
- // Extended from spec
- {
- key: "presentMode",
- converter: webidl.converters["GPUPresentMode"],
- },
- {
- key: "width",
- converter: webidl.converters["long"],
- required: true,
- },
- {
- key: "height",
- converter: webidl.converters["long"],
- required: true,
- },
- {
- key: "viewFormats",
- converter: webidl.createSequenceConverter(
- webidl.converters["GPUTextureFormat"],
- ),
- get defaultValue() {
- return [];
- },
- },
-];
-webidl.converters["GPUCanvasConfiguration"] = webidl
- .createDictionaryConverter(
- "GPUCanvasConfiguration",
- dictMembersGPUCanvasConfiguration,
- );
-
-window.__bootstrap.webgpu = {
- ...window.__bootstrap.webgpu,
- GPUCanvasContext,
- createCanvasContext,
-};
+function presentGPUCanvasContext(ctx) {
+ ctx[_present]();
+}
+
+export { createCanvasContext, GPUCanvasContext, presentGPUCanvasContext };