@wasi-gfx/js-webgpu 0.3.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/globals.js CHANGED
@@ -1,4 +1,4 @@
1
- import { getGpu, GPU, GPUAdapter, GPUDevice, GPUQueue, GPUBuffer, GPUBufferUsage, GPUMapMode, GPUTextureUsage, GPUTexture, GPUTextureView, GPUSampler, GPUBindGroupLayout, GPUPipelineLayout, GPUBindGroup, GPUShaderModule, GPUShaderStage, GPUComputePipeline, GPURenderPipeline, GPUColorWrite, GPUCommandEncoder, GPURenderPassEncoder, GPUComputePassEncoder, GPUCommandBuffer, GPUQuerySet } from "./webgpu";
1
+ import { getGpu, GPU, GPUAdapter, GPUDeviceLostInfo, GPUDevice, GPUQueue, GPUBuffer, GPUBufferUsage, GPUMapMode, GPUTextureUsage, GPUTexture, GPUTextureView, GPUSampler, GPUBindGroupLayout, GPUPipelineLayout, GPUBindGroup, GPUShaderModule, GPUShaderStage, GPUComputePipeline, GPURenderPipeline, GPUColorWrite, GPUCommandEncoder, GPURenderPassEncoder, GPUComputePassEncoder, GPUCommandBuffer, GPUQuerySet, GPUAdapterInfo, GPUSupportedLimits, GPUSupportedFeatures, GPURenderBundleEncoder, GPURenderBundle, GPUError, GPUValidationError, GPUOutOfMemoryError, GPUInternalError, GPUPipelineError } from "./webgpu";
2
2
  class Navigator {
3
3
  gpu;
4
4
  constructor() {
@@ -18,10 +18,10 @@ export function declareGlobals() {
18
18
  defineGlobal("navigator", new Navigator());
19
19
  defineGlobal("GPU", GPU);
20
20
  defineGlobal("GPUAdapter", GPUAdapter);
21
- // defineGlobal("GPUAdapterInfo", GPUAdapterInfo);
22
- // defineGlobal("GPUSupportedLimits", GPUSupportedLimits);
23
- // defineGlobal("GPUSupportedFeatures", GPUSupportedFeatures);
24
- // defineGlobal("GPUDeviceLostInfo", GPUDeviceLostInfo);
21
+ defineGlobal("GPUAdapterInfo", GPUAdapterInfo);
22
+ defineGlobal("GPUSupportedLimits", GPUSupportedLimits);
23
+ defineGlobal("GPUSupportedFeatures", GPUSupportedFeatures);
24
+ defineGlobal("GPUDeviceLostInfo", GPUDeviceLostInfo);
25
25
  defineGlobal("GPUDevice", GPUDevice);
26
26
  defineGlobal("GPUQueue", GPUQueue);
27
27
  defineGlobal("GPUBuffer", GPUBuffer);
@@ -43,10 +43,12 @@ export function declareGlobals() {
43
43
  defineGlobal("GPURenderPassEncoder", GPURenderPassEncoder);
44
44
  defineGlobal("GPUComputePassEncoder", GPUComputePassEncoder);
45
45
  defineGlobal("GPUCommandBuffer", GPUCommandBuffer);
46
- // defineGlobal("GPURenderBundleEncoder", GPURenderBundleEncoder);
47
- // defineGlobal("GPURenderBundle", GPURenderBundle);
46
+ defineGlobal("GPURenderBundleEncoder", GPURenderBundleEncoder);
47
+ defineGlobal("GPURenderBundle", GPURenderBundle);
48
48
  defineGlobal("GPUQuerySet", GPUQuerySet);
49
- // defineGlobal("GPUError", GPUError);
50
- // defineGlobal("GPUValidationError", GPUValidationError);
51
- // defineGlobal("GPUOutOfMemoryError", GPUOutOfMemoryError);
49
+ defineGlobal("GPUError", GPUError);
50
+ defineGlobal("GPUValidationError", GPUValidationError);
51
+ defineGlobal("GPUOutOfMemoryError", GPUOutOfMemoryError);
52
+ defineGlobal("GPUInternalError", GPUInternalError);
53
+ defineGlobal("GPUPipelineError", GPUPipelineError);
52
54
  }
package/dist/webgpu.d.ts CHANGED
@@ -80,6 +80,35 @@ export declare class GPUAdapterInfo implements globalThis.GPUAdapterInfo {
80
80
  get description(): string;
81
81
  get isFallbackAdapter(): boolean;
82
82
  }
83
+ export declare class GPUDeviceLostInfo implements globalThis.GPUDeviceLostInfo {
84
+ [inner]: gfx.GpuDeviceLostInfo;
85
+ constructor(k: symbol, i: gfx.GpuDeviceLostInfo);
86
+ get __brand(): 'GPUDeviceLostInfo';
87
+ get reason(): GPUDeviceLostReason;
88
+ get message(): string;
89
+ }
90
+ export declare class GPUError implements globalThis.GPUError {
91
+ #private;
92
+ constructor(k: symbol, message: string);
93
+ get message(): string;
94
+ }
95
+ export declare class GPUValidationError extends GPUError implements globalThis.GPUValidationError {
96
+ constructor(message: string);
97
+ get __brand(): 'GPUValidationError';
98
+ }
99
+ export declare class GPUOutOfMemoryError extends GPUError implements globalThis.GPUOutOfMemoryError {
100
+ constructor(message: string);
101
+ get __brand(): 'GPUOutOfMemoryError';
102
+ }
103
+ export declare class GPUInternalError extends GPUError implements globalThis.GPUInternalError {
104
+ constructor(message: string);
105
+ get __brand(): 'GPUInternalError';
106
+ }
107
+ export declare class GPUPipelineError extends DOMException implements globalThis.GPUPipelineError {
108
+ readonly reason: GPUPipelineErrorReason;
109
+ constructor(message: string | undefined, options: GPUPipelineErrorInit);
110
+ get __brand(): 'GPUPipelineError';
111
+ }
83
112
  export declare class GPUDevice extends EventTarget implements globalThis.GPUDevice {
84
113
  #private;
85
114
  [inner]: gfx.GpuDevice;
package/dist/webgpu.js CHANGED
@@ -306,6 +306,15 @@ function convertTextureViewDimensionWasiToWeb(name) {
306
306
  return name;
307
307
  }
308
308
  }
309
+ function convertConstantsWebToWasi(constants) {
310
+ if (constants === undefined)
311
+ return undefined;
312
+ const record = new gfx.RecordGpuPipelineConstantValue();
313
+ for (const [key, value] of Object.entries(constants)) {
314
+ record.add(key, value);
315
+ }
316
+ return record;
317
+ }
309
318
  function convertGpuLayoutWebToWasi(layout) {
310
319
  if (layout instanceof GPUPipelineLayout) {
311
320
  return {
@@ -415,6 +424,21 @@ function convertGpuBindingResourceWebToWasi(resource) {
415
424
  throw new Unreachable;
416
425
  }
417
426
  }
427
+ function convertGpuErrorWasiToWeb(err) {
428
+ const message = err.message();
429
+ const kind = err.kind();
430
+ switch (kind.tag) {
431
+ case "validation-error":
432
+ return new GPUValidationError(message);
433
+ case "out-of-memory-error":
434
+ return new GPUOutOfMemoryError(message);
435
+ case "internal-error":
436
+ return new GPUInternalError(message);
437
+ default:
438
+ kind;
439
+ throw new Unreachable();
440
+ }
441
+ }
418
442
  function convertBufferToUint8Array(buffer) {
419
443
  if (buffer instanceof Uint8Array) {
420
444
  return buffer;
@@ -671,9 +695,70 @@ export class GPUAdapterInfo {
671
695
  return this[inner].isFallbackAdapter();
672
696
  }
673
697
  }
698
+ export class GPUDeviceLostInfo {
699
+ [inner];
700
+ constructor(k, i) {
701
+ privateConstructorCalled(k);
702
+ this[inner] = i;
703
+ }
704
+ get __brand() {
705
+ return "GPUDeviceLostInfo";
706
+ }
707
+ get reason() {
708
+ return this[inner].reason();
709
+ }
710
+ get message() {
711
+ return this[inner].message();
712
+ }
713
+ }
714
+ export class GPUError {
715
+ #message;
716
+ constructor(k, message) {
717
+ privateConstructorCalled(k);
718
+ this.#message = message;
719
+ }
720
+ get message() {
721
+ return this.#message;
722
+ }
723
+ }
724
+ export class GPUValidationError extends GPUError {
725
+ constructor(message) {
726
+ super(key, message);
727
+ }
728
+ get __brand() {
729
+ return "GPUValidationError";
730
+ }
731
+ }
732
+ export class GPUOutOfMemoryError extends GPUError {
733
+ constructor(message) {
734
+ super(key, message);
735
+ }
736
+ get __brand() {
737
+ return "GPUOutOfMemoryError";
738
+ }
739
+ }
740
+ export class GPUInternalError extends GPUError {
741
+ constructor(message) {
742
+ super(key, message);
743
+ }
744
+ get __brand() {
745
+ return "GPUInternalError";
746
+ }
747
+ }
748
+ export class GPUPipelineError extends DOMException {
749
+ reason;
750
+ constructor(message = "", options) {
751
+ super(message, "GPUPipelineError");
752
+ this.reason = options.reason;
753
+ }
754
+ get __brand() {
755
+ return "GPUPipelineError";
756
+ }
757
+ }
674
758
  export class GPUDevice extends EventTarget {
675
759
  [inner];
676
760
  #adapterInfo;
761
+ #lost;
677
762
  constructor(k, i) {
678
763
  privateConstructorCalled(k);
679
764
  super();
@@ -689,7 +774,20 @@ export class GPUDevice extends EventTarget {
689
774
  this[inner].setLabel(value);
690
775
  }
691
776
  get lost() {
692
- throw new Todo();
777
+ if (!this.#lost) {
778
+ // The generated types declare `lost()` as `PromiseLike`, but componentize-qjs returns a non-thenable future reader (`.read()` yields the value). So read it only if it isn't thenable.
779
+ // See https://github.com/bytecodealliance/jco/issues/2532
780
+ const lost = this[inner].lost();
781
+ let info;
782
+ if ('then' in lost && typeof lost.then === 'function') {
783
+ info = Promise.resolve(lost);
784
+ }
785
+ else {
786
+ info = lost.read();
787
+ }
788
+ this.#lost = info.then(info => new GPUDeviceLostInfo(key, info));
789
+ }
790
+ return this.#lost;
693
791
  }
694
792
  get features() {
695
793
  return new GPUSupportedFeatures(key, this[inner].features());
@@ -834,7 +932,7 @@ export class GPUDevice extends EventTarget {
834
932
  let compute = {
835
933
  module: descriptor.compute.module[inner],
836
934
  entryPoint: descriptor.compute.entryPoint,
837
- constants: undefined, // TODO:
935
+ constants: convertConstantsWebToWasi(descriptor.compute.constants),
838
936
  };
839
937
  return new GPUComputePipeline(key, this[inner].createComputePipeline({
840
938
  ...descriptor,
@@ -866,7 +964,7 @@ export class GPUDevice extends EventTarget {
866
964
  }
867
965
  const vertex = {
868
966
  ...descriptor.vertex,
869
- constants: undefined, // TODO:
967
+ constants: convertConstantsWebToWasi(descriptor.vertex.constants),
870
968
  module: descriptor.vertex.module[inner],
871
969
  buffers,
872
970
  };
@@ -881,7 +979,7 @@ export class GPUDevice extends EventTarget {
881
979
  if (descriptor.fragment) {
882
980
  fragment = {
883
981
  ...descriptor.fragment,
884
- constants: undefined, // TODO:
982
+ constants: convertConstantsWebToWasi(descriptor.fragment.constants),
885
983
  module: descriptor.fragment.module[inner],
886
984
  targets: Array.from(descriptor.fragment.targets).map(target => {
887
985
  if (target) {
@@ -951,9 +1049,7 @@ export class GPUDevice extends EventTarget {
951
1049
  async popErrorScope() {
952
1050
  const err = await this[inner].popErrorScope();
953
1051
  if (err) {
954
- return {
955
- message: err.message(),
956
- };
1052
+ return convertGpuErrorWasiToWeb(err);
957
1053
  }
958
1054
  else {
959
1055
  return null;
@@ -1220,8 +1316,8 @@ export class GPUQueue {
1220
1316
  submit(commandBuffers) {
1221
1317
  this[inner].submit(Array.from(commandBuffers).map((cb) => cb[inner]));
1222
1318
  }
1223
- onSubmittedWorkDone() {
1224
- throw new Todo();
1319
+ async onSubmittedWorkDone() {
1320
+ await this[inner].onSubmittedWorkDone();
1225
1321
  }
1226
1322
  writeBuffer(buffer, bufferOffset, data, dataOffset, size) {
1227
1323
  this[inner].writeBufferWithCopy(buffer[inner], numToBigInt(bufferOffset), convertBufferToUint8Array(data), numToBigIntOptional(dataOffset), numToBigIntOptional(size));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wasi-gfx/js-webgpu",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "description": "WebGPU JavaScript API on top of wasi-gfx",
5
5
  "exports": {
6
6
  "./canvas": {