Skip to content

Commit d0a5e48

Browse files
authored
chore: backport deno changes (#5686)
1 parent 2f45227 commit d0a5e48

File tree

7 files changed

+128
-42
lines changed

7 files changed

+128
-42
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ deno_core = "0.272.0"
170170
deno_url = "0.143.0"
171171
deno_web = "0.174.0"
172172
deno_webidl = "0.143.0"
173-
deno_webgpu = { version = "0.110.0", path = "./deno_webgpu" }
173+
deno_webgpu = { version = "0.118.0", path = "./deno_webgpu" }
174174
tokio = "1.37.0"
175175
termcolor = "1.4.1"
176176

deno_webgpu/01_webgpu.js

Lines changed: 82 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,10 @@ const {
121121

122122
import * as webidl from "ext:deno_webidl/00_webidl.js";
123123
import {
124+
defineEventHandler,
124125
Event,
125126
EventTarget,
126-
defineEventHandler,
127+
setEventTargetData,
127128
} from "ext:deno_web/02_event.js";
128129
import { DOMException } from "ext:deno_web/01_dom_exception.js";
129130
import { createFilteredInspectProxy } from "ext:deno_console/01_console.js";
@@ -299,7 +300,6 @@ class GPUValidationError extends GPUError {
299300
this[_message] = message;
300301
}
301302
}
302-
const GPUValidationErrorPrototype = GPUValidationError.prototype;
303303

304304
class GPUOutOfMemoryError extends GPUError {
305305
name = "GPUOutOfMemoryError";
@@ -312,7 +312,6 @@ class GPUOutOfMemoryError extends GPUError {
312312
this[_message] = message;
313313
}
314314
}
315-
const GPUOutOfMemoryErrorPrototype = GPUOutOfMemoryError.prototype;
316315

317316
class GPUInternalError extends GPUError {
318317
name = "GPUInternalError";
@@ -321,7 +320,6 @@ class GPUInternalError extends GPUError {
321320
this[webidl.brand] = webidl.brand;
322321
}
323322
}
324-
const GPUInternalErrorPrototype = GPUInternalError.prototype;
325323

326324
class GPUUncapturedErrorEvent extends Event {
327325
#error;
@@ -333,7 +331,7 @@ class GPUUncapturedErrorEvent extends Event {
333331
const prefix = "Failed to construct 'GPUUncapturedErrorEvent'";
334332
webidl.requiredArguments(arguments.length, 2, prefix);
335333
gpuUncapturedErrorEventInitDict = webidl.converters
336-
.gpuUncapturedErrorEventInitDict(
334+
.GPUUncapturedErrorEventInit(
337335
gpuUncapturedErrorEventInitDict,
338336
prefix,
339337
"Argument 2",
@@ -348,7 +346,6 @@ class GPUUncapturedErrorEvent extends Event {
348346
}
349347
}
350348
const GPUUncapturedErrorEventPrototype = GPUUncapturedErrorEvent.prototype;
351-
defineEventHandler(GPUUncapturedErrorEvent.prototype, "uncapturederror");
352349

353350
class GPU {
354351
[webidl.brand] = webidl.brand;
@@ -420,9 +417,12 @@ function createGPUAdapter(inner) {
420417
return adapter;
421418
}
422419

420+
const _invalid = Symbol("[[invalid]]");
423421
class GPUAdapter {
424422
/** @type {InnerGPUAdapter} */
425423
[_adapter];
424+
/** @type {bool} */
425+
[_invalid];
426426

427427
/** @returns {GPUSupportedFeatures} */
428428
get features() {
@@ -469,23 +469,33 @@ class GPUAdapter {
469469
}
470470
}
471471

472+
if (this[_invalid]) {
473+
throw new TypeError(
474+
"The adapter cannot be reused, as it has been invalidated by a device creation",
475+
);
476+
}
477+
472478
const { rid, queueRid, features, limits } = op_webgpu_request_device(
473479
this[_adapter].rid,
474480
descriptor.label,
475481
requiredFeatures,
476482
descriptor.requiredLimits,
477483
);
478484

485+
this[_invalid] = true;
486+
479487
const inner = new InnerGPUDevice({
480488
rid,
481489
adapter: this,
482490
features: createGPUSupportedFeatures(features),
483491
limits: createGPUSupportedLimits(limits),
484492
});
493+
const queue = createGPUQueue(descriptor.label, inner, queueRid);
494+
inner.trackResource(queue);
485495
const device = createGPUDevice(
486496
descriptor.label,
487497
inner,
488-
createGPUQueue(descriptor.label, inner, queueRid),
498+
queue,
489499
);
490500
inner.device = device;
491501
return device;
@@ -497,6 +507,12 @@ class GPUAdapter {
497507
requestAdapterInfo() {
498508
webidl.assertBranded(this, GPUAdapterPrototype);
499509

510+
if (this[_invalid]) {
511+
throw new TypeError(
512+
"The adapter cannot be reused, as it has been invalidated by a device creation",
513+
);
514+
}
515+
500516
const {
501517
vendor,
502518
architecture,
@@ -977,7 +993,9 @@ class InnerGPUDevice {
977993
);
978994
return;
979995
case "validation":
980-
constructedError = new GPUValidationError(error.value ?? "validation error");
996+
constructedError = new GPUValidationError(
997+
error.value ?? "validation error",
998+
);
981999
break;
9821000
case "out-of-memory":
9831001
constructedError = new GPUOutOfMemoryError();
@@ -996,11 +1014,13 @@ class InnerGPUDevice {
9961014
({ filter }) => filter === error.type,
9971015
);
9981016
if (scope) {
999-
scope.errors.push(constructedError);
1017+
ArrayPrototypePush(scope.errors, constructedError);
10001018
} else {
1001-
this.device.dispatchEvent(new GPUUncapturedErrorEvent("uncapturederror", {
1002-
error: constructedError,
1003-
}));
1019+
this.device.dispatchEvent(
1020+
new GPUUncapturedErrorEvent("uncapturederror", {
1021+
error: constructedError,
1022+
}),
1023+
);
10041024
}
10051025
}
10061026
}
@@ -1017,6 +1037,7 @@ function createGPUDevice(label, inner, queue) {
10171037
device[_label] = label;
10181038
device[_device] = inner;
10191039
device[_queue] = queue;
1040+
setEventTargetData(device);
10201041
return device;
10211042
}
10221043

@@ -1132,10 +1153,11 @@ class GPUDevice extends EventTarget {
11321153
"Argument 1",
11331154
);
11341155
const device = assertDevice(this, prefix, "this");
1156+
// assign normalized size to descriptor due to createGPUTexture needs it
1157+
descriptor.size = normalizeGPUExtent3D(descriptor.size);
11351158
const { rid, err } = op_webgpu_create_texture({
11361159
deviceRid: device.rid,
11371160
...descriptor,
1138-
size: normalizeGPUExtent3D(descriptor.size),
11391161
});
11401162
device.pushError(err);
11411163

@@ -1307,7 +1329,6 @@ class GPUDevice extends EventTarget {
13071329
} else {
13081330
// deno-lint-ignore prefer-primordials
13091331
const rid = assertResource(resource.buffer, prefix, context);
1310-
// deno-lint-ignore prefer-primordials
13111332
return {
13121333
binding: entry.binding,
13131334
kind: "GPUBufferBinding",
@@ -1816,6 +1837,7 @@ class GPUDevice extends EventTarget {
18161837
}
18171838
GPUObjectBaseMixin("GPUDevice", GPUDevice);
18181839
const GPUDevicePrototype = GPUDevice.prototype;
1840+
defineEventHandler(GPUDevice.prototype, "uncapturederror");
18191841

18201842
class GPUPipelineError extends DOMException {
18211843
#reason;
@@ -1861,6 +1883,15 @@ class GPUQueue {
18611883
/** @type {number} */
18621884
[_rid];
18631885

1886+
[_cleanup]() {
1887+
const rid = this[_rid];
1888+
if (rid !== undefined) {
1889+
core.close(rid);
1890+
/** @type {number | undefined} */
1891+
this[_rid] = undefined;
1892+
}
1893+
}
1894+
18641895
constructor() {
18651896
webidl.illegalConstructor();
18661897
}
@@ -5488,6 +5519,16 @@ webidl.converters["GPUExtent3D"] = (V, opts) => {
54885519
if (typeof V === "object") {
54895520
const method = V[SymbolIterator];
54905521
if (method !== undefined) {
5522+
// validate length of GPUExtent3D
5523+
const min = 1;
5524+
const max = 3;
5525+
if (V.length < min || V.length > max) {
5526+
throw webidl.makeException(
5527+
TypeError,
5528+
`A sequence of number used as a GPUExtent3D must have between ${min} and ${max} elements.`,
5529+
opts,
5530+
);
5531+
}
54915532
return webidl.converters["sequence<GPUIntegerCoordinate>"](V, opts);
54925533
}
54935534
return webidl.converters["GPUExtent3DDict"](V, opts);
@@ -6823,6 +6864,15 @@ webidl.converters["GPUOrigin3D"] = (V, opts) => {
68236864
if (typeof V === "object") {
68246865
const method = V[SymbolIterator];
68256866
if (method !== undefined) {
6867+
// validate length of GPUOrigin3D
6868+
const length = 3;
6869+
if (V.length > length) {
6870+
throw webidl.makeException(
6871+
TypeError,
6872+
`A sequence of number used as a GPUOrigin3D must have at most ${length} elements.`,
6873+
opts,
6874+
);
6875+
}
68266876
return webidl.converters["sequence<GPUIntegerCoordinate>"](V, opts);
68276877
}
68286878
return webidl.converters["GPUOrigin3DDict"](V, opts);
@@ -6891,6 +6941,15 @@ webidl.converters["GPUOrigin2D"] = (V, opts) => {
68916941
if (typeof V === "object") {
68926942
const method = V[SymbolIterator];
68936943
if (method !== undefined) {
6944+
// validate length of GPUOrigin2D
6945+
const length = 2;
6946+
if (V.length > length) {
6947+
throw webidl.makeException(
6948+
TypeError,
6949+
`A sequence of number used as a GPUOrigin2D must have at most ${length} elements.`,
6950+
opts,
6951+
);
6952+
}
68946953
return webidl.converters["sequence<GPUIntegerCoordinate>"](V, opts);
68956954
}
68966955
return webidl.converters["GPUOrigin2DDict"](V, opts);
@@ -6976,6 +7035,15 @@ webidl.converters["GPUColor"] = (V, opts) => {
69767035
if (typeof V === "object") {
69777036
const method = V[SymbolIterator];
69787037
if (method !== undefined) {
7038+
// validate length of GPUColor
7039+
const length = 4;
7040+
if (V.length !== length) {
7041+
throw webidl.makeException(
7042+
TypeError,
7043+
`A sequence of number used as a GPUColor must have exactly ${length} elements.`,
7044+
opts,
7045+
);
7046+
}
69797047
return webidl.converters["sequence<double>"](V, opts);
69807048
}
69817049
return webidl.converters["GPUColorDict"](V, opts);

deno_webgpu/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[package]
44
name = "deno_webgpu"
5-
version = "0.110.0"
5+
version = "0.118.0"
66
authors = ["the Deno authors"]
77
edition.workspace = true
88
license = "MIT"

deno_webgpu/byow.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use deno_core::op2;
66
use deno_core::OpState;
77
use deno_core::ResourceId;
88
use std::ffi::c_void;
9+
#[cfg(any(target_os = "linux", target_os = "macos"))]
910
use std::ptr::NonNull;
1011

1112
use crate::surface::WebGpuSurface;
@@ -37,6 +38,7 @@ pub fn op_webgpu_surface_create(
3738
}
3839

3940
let (win_handle, display_handle) = raw_window(system, p1, p2)?;
41+
// SAFETY: see above comment
4042
let surface = unsafe { instance.instance_create_surface(display_handle, win_handle, None)? };
4143

4244
let rid = state
@@ -82,9 +84,10 @@ fn raw_window(
8284
}
8385

8486
let win_handle = {
85-
let mut handle = raw_window_handle::Win32WindowHandle::new();
86-
handle.hwnd = window as *mut c_void;
87-
handle.hinstance = hinstance as *mut c_void;
87+
let mut handle = raw_window_handle::Win32WindowHandle::new(
88+
std::num::NonZeroIsize::new(window as isize).ok_or(type_error("window is null"))?,
89+
);
90+
handle.hinstance = std::num::NonZeroIsize::new(hinstance as isize);
8891

8992
raw_window_handle::RawWindowHandle::Win32(handle)
9093
};
@@ -99,17 +102,30 @@ fn raw_window(
99102
window: *const c_void,
100103
display: *const c_void,
101104
) -> Result<RawHandles, AnyError> {
102-
if system != "x11" {
105+
let (win_handle, display_handle);
106+
if system == "x11" {
107+
win_handle = raw_window_handle::RawWindowHandle::Xlib(
108+
raw_window_handle::XlibWindowHandle::new(window as *mut c_void as _),
109+
);
110+
111+
display_handle = raw_window_handle::RawDisplayHandle::Xlib(
112+
raw_window_handle::XlibDisplayHandle::new(NonNull::new(display as *mut c_void), 0),
113+
);
114+
} else if system == "wayland" {
115+
win_handle = raw_window_handle::RawWindowHandle::Wayland(
116+
raw_window_handle::WaylandWindowHandle::new(
117+
NonNull::new(window as *mut c_void).ok_or(type_error("window is null"))?,
118+
),
119+
);
120+
121+
display_handle = raw_window_handle::RawDisplayHandle::Wayland(
122+
raw_window_handle::WaylandDisplayHandle::new(
123+
NonNull::new(display as *mut c_void).ok_or(type_error("display is null"))?,
124+
),
125+
);
126+
} else {
103127
return Err(type_error("Invalid system on Linux"));
104128
}
105129

106-
let win_handle = raw_window_handle::RawWindowHandle::Xlib(
107-
raw_window_handle::XlibWindowHandle::new(window as *mut c_void as _),
108-
);
109-
110-
let display_handle = raw_window_handle::RawDisplayHandle::Xlib(
111-
raw_window_handle::XlibDisplayHandle::new(NonNull::new(display as *mut c_void), 0),
112-
);
113-
114130
Ok((win_handle, display_handle))
115131
}

deno_webgpu/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ pub fn op_webgpu_request_device(
667667
#[serde] required_limits: Option<wgpu_types::Limits>,
668668
) -> Result<GpuDeviceRes, AnyError> {
669669
let mut state = state.borrow_mut();
670-
let adapter_resource = state.resource_table.get::<WebGpuAdapter>(adapter_rid)?;
670+
let adapter_resource = state.resource_table.take::<WebGpuAdapter>(adapter_rid)?;
671671
let adapter = adapter_resource.1;
672672
let instance = state.borrow::<Instance>();
673673

@@ -684,6 +684,7 @@ pub fn op_webgpu_request_device(
684684
None,
685685
None
686686
));
687+
adapter_resource.close();
687688
if let Some(err) = maybe_err {
688689
return Err(DomExceptionOperationError::new(&err.to_string()).into());
689690
}
@@ -724,12 +725,13 @@ pub fn op_webgpu_request_adapter_info(
724725
state: Rc<RefCell<OpState>>,
725726
#[smi] adapter_rid: ResourceId,
726727
) -> Result<GPUAdapterInfo, AnyError> {
727-
let state = state.borrow_mut();
728-
let adapter_resource = state.resource_table.get::<WebGpuAdapter>(adapter_rid)?;
728+
let mut state = state.borrow_mut();
729+
let adapter_resource = state.resource_table.take::<WebGpuAdapter>(adapter_rid)?;
729730
let adapter = adapter_resource.1;
730731
let instance = state.borrow::<Instance>();
731732

732733
let info = gfx_select!(adapter => instance.adapter_get_info(adapter))?;
734+
adapter_resource.close();
733735

734736
Ok(GPUAdapterInfo {
735737
vendor: info.vendor.to_string(),

0 commit comments

Comments
 (0)