summaryrefslogtreecommitdiff
path: root/ext/webgpu/src/03_surface.js
diff options
context:
space:
mode:
authorLeo Kettmeir <crowlkats@toaxl.com>2023-02-07 20:22:46 +0100
committerGitHub <noreply@github.com>2023-02-07 20:22:46 +0100
commitb4aa1530970f7b9cc4e6f2f27e077852c4e178d3 (patch)
tree3d008912affe8550692183bd2697a386db5e3c79 /ext/webgpu/src/03_surface.js
parent65500f36e870b4ada3996b06aa287e30177d21a3 (diff)
refactor: Use ES modules for internal runtime code (#17648)
This PR refactors all internal js files (except core) to be written as ES modules. `__bootstrap`has been mostly replaced with static imports in form in `internal:[path to file from repo root]`. To specify if files are ESM, an `esm` method has been added to `Extension`, similar to the `js` method. A new ModuleLoader called `InternalModuleLoader` has been added to enable the loading of internal specifiers, which is used in all situations except when a snapshot is only loaded, and not a new one is created from it. --------- Co-authored-by: Bartek IwaƄczuk <biwanczuk@gmail.com>
Diffstat (limited to 'ext/webgpu/src/03_surface.js')
-rw-r--r--ext/webgpu/src/03_surface.js251
1 files changed, 124 insertions, 127 deletions
diff --git a/ext/webgpu/src/03_surface.js b/ext/webgpu/src/03_surface.js
index f9e422e82..b46db047c 100644
--- a/ext/webgpu/src/03_surface.js
+++ b/ext/webgpu/src/03_surface.js
@@ -6,144 +6,141 @@
/// <reference path="../web/lib.deno_web.d.ts" />
/// <reference path="./lib.deno_webgpu.d.ts" />
-"use strict";
-
-((window) => {
- const core = window.Deno.core;
- const ops = core.ops;
- const webidl = window.__bootstrap.webidl;
- const { Symbol } = window.__bootstrap.primordials;
- const { _device, assertDevice, createGPUTexture } = window.__bootstrap.webgpu;
-
- const _surfaceRid = Symbol("[[surfaceRid]]");
- const _configuration = Symbol("[[configuration]]");
- const _canvas = Symbol("[[canvas]]");
- const _currentTexture = Symbol("[[currentTexture]]");
- class GPUCanvasContext {
- /** @type {number} */
- [_surfaceRid];
- /** @type {InnerGPUDevice} */
- [_device];
- [_configuration];
- [_canvas];
- /** @type {GPUTexture | undefined} */
- [_currentTexture];
-
- get canvas() {
- webidl.assertBranded(this, GPUCanvasContextPrototype);
- return this[_canvas];
- }
-
- constructor() {
- webidl.illegalConstructor();
- }
-
- configure(configuration) {
- webidl.assertBranded(this, GPUCanvasContextPrototype);
- const prefix = "Failed to execute 'configure' on 'GPUCanvasContext'";
- webidl.requiredArguments(arguments.length, 1, { prefix });
- configuration = webidl.converters.GPUCanvasConfiguration(configuration, {
- prefix,
- context: "Argument 1",
- });
-
- this[_device] = configuration.device[_device];
- this[_configuration] = configuration;
- const device = assertDevice(this, {
- prefix,
- context: "configuration.device",
- });
-
- const { err } = ops.op_webgpu_surface_configure({
- surfaceRid: this[_surfaceRid],
- deviceRid: device.rid,
- format: configuration.format,
- viewFormats: configuration.viewFormats,
- usage: configuration.usage,
- width: configuration.width,
- height: configuration.height,
- alphaMode: configuration.alphaMode,
- });
-
- device.pushError(err);
- }
-
- unconfigure() {
- webidl.assertBranded(this, GPUCanvasContextPrototype);
-
- this[_configuration] = null;
- this[_device] = null;
- }
+const core = globalThis.Deno.core;
+const ops = core.ops;
+import * as webidl from "internal:ext/webidl/00_webidl.js";
+const primordials = globalThis.__bootstrap.primordials;
+const { Symbol } = primordials;
+import {
+ _device,
+ assertDevice,
+ createGPUTexture,
+} from "internal:ext/webgpu/01_webgpu.js";
+
+const _surfaceRid = Symbol("[[surfaceRid]]");
+const _configuration = Symbol("[[configuration]]");
+const _canvas = Symbol("[[canvas]]");
+const _currentTexture = Symbol("[[currentTexture]]");
+class GPUCanvasContext {
+ /** @type {number} */
+ [_surfaceRid];
+ /** @type {InnerGPUDevice} */
+ [_device];
+ [_configuration];
+ [_canvas];
+ /** @type {GPUTexture | undefined} */
+ [_currentTexture];
+
+ get canvas() {
+ webidl.assertBranded(this, GPUCanvasContextPrototype);
+ return this[_canvas];
+ }
- getCurrentTexture() {
- webidl.assertBranded(this, GPUCanvasContextPrototype);
- const prefix =
- "Failed to execute 'getCurrentTexture' on 'GPUCanvasContext'";
+ constructor() {
+ webidl.illegalConstructor();
+ }
- if (this[_configuration] === null) {
- throw new DOMException(
- "context is not configured.",
- "InvalidStateError",
- );
- }
+ configure(configuration) {
+ webidl.assertBranded(this, GPUCanvasContextPrototype);
+ const prefix = "Failed to execute 'configure' on 'GPUCanvasContext'";
+ webidl.requiredArguments(arguments.length, 1, { prefix });
+ configuration = webidl.converters.GPUCanvasConfiguration(configuration, {
+ prefix,
+ context: "Argument 1",
+ });
+
+ this[_device] = configuration.device[_device];
+ this[_configuration] = configuration;
+ const device = assertDevice(this, {
+ prefix,
+ context: "configuration.device",
+ });
+
+ const { err } = ops.op_webgpu_surface_configure({
+ surfaceRid: this[_surfaceRid],
+ deviceRid: device.rid,
+ format: configuration.format,
+ viewFormats: configuration.viewFormats,
+ usage: configuration.usage,
+ width: configuration.width,
+ height: configuration.height,
+ alphaMode: configuration.alphaMode,
+ });
+
+ device.pushError(err);
+ }
- const device = assertDevice(this, { prefix, context: "this" });
+ unconfigure() {
+ webidl.assertBranded(this, GPUCanvasContextPrototype);
- if (this[_currentTexture]) {
- return this[_currentTexture];
- }
+ this[_configuration] = null;
+ this[_device] = null;
+ }
- const { rid } = ops.op_webgpu_surface_get_current_texture(
- device.rid,
- this[_surfaceRid],
- );
+ getCurrentTexture() {
+ webidl.assertBranded(this, GPUCanvasContextPrototype);
+ const prefix =
+ "Failed to execute 'getCurrentTexture' on 'GPUCanvasContext'";
- const texture = createGPUTexture(
- {
- size: {
- width: this[_configuration].width,
- height: this[_configuration].height,
- depthOrArrayLayers: 1,
- },
- mipLevelCount: 1,
- sampleCount: 1,
- dimension: "2d",
- format: this[_configuration].format,
- usage: this[_configuration].usage,
- },
- device,
- rid,
+ if (this[_configuration] === null) {
+ throw new DOMException(
+ "context is not configured.",
+ "InvalidStateError",
);
- device.trackResource(texture);
- this[_currentTexture] = texture;
- return texture;
}
- // Extended from spec. Required to present the texture; browser don't need this.
- present() {
- webidl.assertBranded(this, GPUCanvasContextPrototype);
- const prefix = "Failed to execute 'present' on 'GPUCanvasContext'";
- const device = assertDevice(this[_currentTexture], {
- prefix,
- context: "this",
- });
- ops.op_webgpu_surface_present(device.rid, this[_surfaceRid]);
- this[_currentTexture].destroy();
- this[_currentTexture] = undefined;
+ const device = assertDevice(this, { prefix, context: "this" });
+
+ if (this[_currentTexture]) {
+ return this[_currentTexture];
}
+
+ const { rid } = ops.op_webgpu_surface_get_current_texture(
+ device.rid,
+ this[_surfaceRid],
+ );
+
+ const texture = createGPUTexture(
+ {
+ size: {
+ width: this[_configuration].width,
+ height: this[_configuration].height,
+ depthOrArrayLayers: 1,
+ },
+ mipLevelCount: 1,
+ sampleCount: 1,
+ dimension: "2d",
+ format: this[_configuration].format,
+ usage: this[_configuration].usage,
+ },
+ device,
+ rid,
+ );
+ device.trackResource(texture);
+ this[_currentTexture] = texture;
+ return texture;
}
- const GPUCanvasContextPrototype = GPUCanvasContext.prototype;
- function createCanvasContext(options) {
- const canvasContext = webidl.createBranded(GPUCanvasContext);
- canvasContext[_surfaceRid] = options.surfaceRid;
- canvasContext[_canvas] = options.canvas;
- return canvasContext;
+ // Extended from spec. Required to present the texture; browser don't need this.
+ present() {
+ webidl.assertBranded(this, GPUCanvasContextPrototype);
+ const prefix = "Failed to execute 'present' on 'GPUCanvasContext'";
+ const device = assertDevice(this[_currentTexture], {
+ prefix,
+ context: "this",
+ });
+ ops.op_webgpu_surface_present(device.rid, this[_surfaceRid]);
+ this[_currentTexture].destroy();
+ this[_currentTexture] = undefined;
}
+}
+const GPUCanvasContextPrototype = GPUCanvasContext.prototype;
+
+function createCanvasContext(options) {
+ const canvasContext = webidl.createBranded(GPUCanvasContext);
+ canvasContext[_surfaceRid] = options.surfaceRid;
+ canvasContext[_canvas] = options.canvas;
+ return canvasContext;
+}
- window.__bootstrap.webgpu = {
- ...window.__bootstrap.webgpu,
- GPUCanvasContext,
- createCanvasContext,
- };
-})(this);
+export { createCanvasContext, GPUCanvasContext };