@wasi-gfx/js-webgpu 0.2.0 → 0.3.0

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.
Files changed (63) hide show
  1. package/README.md +3 -3
  2. package/dist/canvas.d.ts +3 -3
  3. package/dist/canvas.js +36 -23
  4. package/dist/webgpu.d.ts +39 -12
  5. package/dist/webgpu.js +248 -42
  6. package/package.json +12 -6
  7. package/types/interfaces/wasi-cli-run.d.ts +2 -2
  8. package/types/interfaces/wasi-cli-stdout.d.ts +14 -3
  9. package/types/interfaces/wasi-cli-types.d.ts +15 -0
  10. package/types/interfaces/wasi-gfx-frame-buffer-frame-buffer.d.ts +13 -0
  11. package/types/interfaces/wasi-gfx-surface-surface-frame-buffer.d.ts +12 -0
  12. package/types/interfaces/wasi-gfx-surface-surface-webgpu.d.ts +29 -0
  13. package/types/interfaces/{wasi-surface-surface.d.ts → wasi-gfx-surface-surface.d.ts} +8 -18
  14. package/types/interfaces/wasi-webgpu-webgpu.d.ts +809 -810
  15. package/types/wit.d.ts +9 -10
  16. package/wit/deps/wasi-cli-0.3.0/package.wit +256 -0
  17. package/wit/deps/wasi-clocks-0.3.0/package.wit +43 -0
  18. package/wit/deps/wasi-filesystem-0.3.0/package.wit +149 -0
  19. package/wit/deps/wasi-gfx-frame-buffer-0.2.0/package.wit +13 -0
  20. package/wit/deps/{surface/surface.wit → wasi-gfx-surface-0.2.0/package.wit} +69 -44
  21. package/wit/deps/wasi-random-0.3.0/package.wit +18 -0
  22. package/wit/deps/wasi-sockets-0.3.0/package.wit +116 -0
  23. package/wit/deps/{webgpu/webgpu.wit → wasi-webgpu-0.3.0-rc.2/package.wit} +1007 -836
  24. package/wit/world.wit +8 -7
  25. package/types/interfaces/wasi-frame-buffer-frame-buffer.d.ts +0 -21
  26. package/types/interfaces/wasi-graphics-context-graphics-context.d.ts +0 -17
  27. package/types/interfaces/wasi-io-error.d.ts +0 -18
  28. package/types/interfaces/wasi-io-poll.d.ts +0 -43
  29. package/types/interfaces/wasi-io-streams.d.ts +0 -240
  30. package/wit/deps/cli/command.wit +0 -7
  31. package/wit/deps/cli/environment.wit +0 -18
  32. package/wit/deps/cli/exit.wit +0 -4
  33. package/wit/deps/cli/imports.wit +0 -20
  34. package/wit/deps/cli/run.wit +0 -4
  35. package/wit/deps/cli/stdio.wit +0 -17
  36. package/wit/deps/cli/terminal.wit +0 -49
  37. package/wit/deps/clocks/monotonic-clock.wit +0 -45
  38. package/wit/deps/clocks/wall-clock.wit +0 -42
  39. package/wit/deps/clocks/world.wit +0 -6
  40. package/wit/deps/filesystem/preopens.wit +0 -8
  41. package/wit/deps/filesystem/types.wit +0 -634
  42. package/wit/deps/filesystem/world.wit +0 -6
  43. package/wit/deps/frame-buffer/frame-buffer.wit +0 -23
  44. package/wit/deps/graphics-context/graphics-context.wit +0 -18
  45. package/wit/deps/io/error.wit +0 -34
  46. package/wit/deps/io/poll.wit +0 -41
  47. package/wit/deps/io/streams.wit +0 -262
  48. package/wit/deps/io/world.wit +0 -6
  49. package/wit/deps/random/insecure-seed.wit +0 -25
  50. package/wit/deps/random/insecure.wit +0 -22
  51. package/wit/deps/random/random.wit +0 -26
  52. package/wit/deps/random/world.wit +0 -7
  53. package/wit/deps/sockets/instance-network.wit +0 -9
  54. package/wit/deps/sockets/ip-name-lookup.wit +0 -51
  55. package/wit/deps/sockets/network.wit +0 -145
  56. package/wit/deps/sockets/tcp-create-socket.wit +0 -27
  57. package/wit/deps/sockets/tcp.wit +0 -353
  58. package/wit/deps/sockets/udp-create-socket.wit +0 -27
  59. package/wit/deps/sockets/udp.wit +0 -266
  60. package/wit/deps/sockets/world.wit +0 -11
  61. package/wit/deps/webgpu/imports.wit +0 -5
  62. package/wit/deps.lock +0 -51
  63. package/wit/deps.toml +0 -18
package/dist/webgpu.js CHANGED
@@ -1,5 +1,92 @@
1
- import * as gfx from 'wasi:webgpu/webgpu@0.0.1';
2
- import { inner, key, privateConstructorCalled, Todo, Unreachable } from './common';
1
+ import * as gfx from 'wasi:webgpu/webgpu@0.3.0-rc.2';
2
+ import { EventTarget, inner, key, privateConstructorCalled, Todo, Unreachable } from './common';
3
+ // flags converters: the web API uses numeric bitflags, wasi uses flag records.
4
+ function convertBufferUsageWebToWasi(usage) {
5
+ return {
6
+ mapRead: !!(usage & GPUBufferUsage.MAP_READ),
7
+ mapWrite: !!(usage & GPUBufferUsage.MAP_WRITE),
8
+ copySrc: !!(usage & GPUBufferUsage.COPY_SRC),
9
+ copyDst: !!(usage & GPUBufferUsage.COPY_DST),
10
+ index: !!(usage & GPUBufferUsage.INDEX),
11
+ vertex: !!(usage & GPUBufferUsage.VERTEX),
12
+ uniform: !!(usage & GPUBufferUsage.UNIFORM),
13
+ storage: !!(usage & GPUBufferUsage.STORAGE),
14
+ indirect: !!(usage & GPUBufferUsage.INDIRECT),
15
+ queryResolve: !!(usage & GPUBufferUsage.QUERY_RESOLVE),
16
+ };
17
+ }
18
+ function convertBufferUsageWasiToWeb(usage) {
19
+ let n = 0;
20
+ if (usage.mapRead)
21
+ n |= GPUBufferUsage.MAP_READ;
22
+ if (usage.mapWrite)
23
+ n |= GPUBufferUsage.MAP_WRITE;
24
+ if (usage.copySrc)
25
+ n |= GPUBufferUsage.COPY_SRC;
26
+ if (usage.copyDst)
27
+ n |= GPUBufferUsage.COPY_DST;
28
+ if (usage.index)
29
+ n |= GPUBufferUsage.INDEX;
30
+ if (usage.vertex)
31
+ n |= GPUBufferUsage.VERTEX;
32
+ if (usage.uniform)
33
+ n |= GPUBufferUsage.UNIFORM;
34
+ if (usage.storage)
35
+ n |= GPUBufferUsage.STORAGE;
36
+ if (usage.indirect)
37
+ n |= GPUBufferUsage.INDIRECT;
38
+ if (usage.queryResolve)
39
+ n |= GPUBufferUsage.QUERY_RESOLVE;
40
+ return n;
41
+ }
42
+ export function convertTextureUsageWebToWasi(usage) {
43
+ return {
44
+ copySrc: !!(usage & GPUTextureUsage.COPY_SRC),
45
+ copyDst: !!(usage & GPUTextureUsage.COPY_DST),
46
+ textureBinding: !!(usage & GPUTextureUsage.TEXTURE_BINDING),
47
+ storageBinding: !!(usage & GPUTextureUsage.STORAGE_BINDING),
48
+ renderAttachment: !!(usage & GPUTextureUsage.RENDER_ATTACHMENT),
49
+ transientAttachment: !!(usage & GPUTextureUsage.TRANSIENT_ATTACHMENT),
50
+ };
51
+ }
52
+ function convertTextureUsageWasiToWeb(usage) {
53
+ let n = 0;
54
+ if (usage.copySrc)
55
+ n |= GPUTextureUsage.COPY_SRC;
56
+ if (usage.copyDst)
57
+ n |= GPUTextureUsage.COPY_DST;
58
+ if (usage.textureBinding)
59
+ n |= GPUTextureUsage.TEXTURE_BINDING;
60
+ if (usage.storageBinding)
61
+ n |= GPUTextureUsage.STORAGE_BINDING;
62
+ if (usage.renderAttachment)
63
+ n |= GPUTextureUsage.RENDER_ATTACHMENT;
64
+ if (usage.transientAttachment)
65
+ n |= GPUTextureUsage.TRANSIENT_ATTACHMENT;
66
+ return n;
67
+ }
68
+ function convertShaderStageWebToWasi(visibility) {
69
+ return {
70
+ vertex: !!(visibility & GPUShaderStage.VERTEX),
71
+ fragment: !!(visibility & GPUShaderStage.FRAGMENT),
72
+ compute: !!(visibility & GPUShaderStage.COMPUTE),
73
+ };
74
+ }
75
+ function convertColorWriteWebToWasi(mask) {
76
+ return {
77
+ red: !!(mask & GPUColorWrite.RED),
78
+ green: !!(mask & GPUColorWrite.GREEN),
79
+ blue: !!(mask & GPUColorWrite.BLUE),
80
+ alpha: !!(mask & GPUColorWrite.ALPHA),
81
+ all: (mask & GPUColorWrite.ALL) === GPUColorWrite.ALL,
82
+ };
83
+ }
84
+ function convertMapModeWebToWasi(mode) {
85
+ return {
86
+ read: !!(mode & GPUMapMode.READ),
87
+ write: !!(mode & GPUMapMode.WRITE),
88
+ };
89
+ }
3
90
  // converters
4
91
  // don't use raw `Number(x)`/`BigInt(x)` because they accept non-numeric values
5
92
  function numToBigInt(n) {
@@ -21,11 +108,13 @@ function bigIntToNumOptional(n) {
21
108
  function convertFeatureNameWebToWasi(name) {
22
109
  if (name === "texture-compression-bc-sliced-3d")
23
110
  return "texture-compression-bc-sliced3d";
24
- if (name === "texture-compression-astc-sliced-3d" || name === "float32-blendable")
111
+ if (name === "texture-compression-astc-sliced-3d")
112
+ return "texture-compression-astc-sliced3d";
113
+ if (name === "subgroup-size-control" || name === "texture-compression-unaligned" || name === "atomic-vec2u-min-max")
25
114
  throw new Todo;
26
115
  return name;
27
116
  }
28
- function convertTextureFormatWebToWasi(name) {
117
+ export function convertTextureFormatWebToWasi(name) {
29
118
  switch (name) {
30
119
  case "astc-4x4-unorm":
31
120
  return "astc4x4-unorm";
@@ -153,16 +242,7 @@ function convertVertexFormatWebToWasi(name) {
153
242
  switch (name) {
154
243
  case "unorm10-10-10-2":
155
244
  return "unorm1010102";
156
- case "uint8":
157
- case "sint8":
158
- case "unorm8":
159
- case "snorm8":
160
- case "uint16":
161
- case "sint16":
162
- case "unorm16":
163
- case "snorm16":
164
- case "float16":
165
- case "unorm8x4-bgra":
245
+ case "snorm10-10-10-2":
166
246
  throw new Todo;
167
247
  default:
168
248
  return name;
@@ -309,23 +389,25 @@ function convertGpuBindingResourceWebToWasi(resource) {
309
389
  };
310
390
  }
311
391
  else if (resource instanceof GPUTextureView) {
312
- throw new Todo;
392
+ return {
393
+ tag: 'gpu-texture-view',
394
+ val: resource[inner],
395
+ };
313
396
  }
314
- else if (resource.buffer) {
315
- let bufferBinding = resource;
397
+ else if ('buffer' in resource && resource.buffer instanceof GPUBuffer) {
316
398
  let offsetBitInt;
317
- if (typeof bufferBinding.offset === "number")
318
- offsetBitInt = numToBigInt(bufferBinding.offset);
399
+ if (typeof resource.offset === "number")
400
+ offsetBitInt = numToBigInt(resource.offset);
319
401
  let sizeBitInt;
320
- if (typeof bufferBinding.size === "number")
321
- sizeBitInt = numToBigInt(bufferBinding.size);
402
+ if (typeof resource.size === "number")
403
+ sizeBitInt = numToBigInt(resource.size);
322
404
  return {
323
405
  tag: 'gpu-buffer-binding',
324
406
  val: {
325
407
  ...resource,
326
408
  size: sizeBitInt,
327
409
  offset: offsetBitInt,
328
- buffer: bufferBinding.buffer[inner],
410
+ buffer: resource.buffer[inner],
329
411
  }
330
412
  };
331
413
  }
@@ -384,6 +466,7 @@ export class GPU {
384
466
  }
385
467
  export class GPUAdapter {
386
468
  [inner];
469
+ #info;
387
470
  constructor(k, i) {
388
471
  privateConstructorCalled(k);
389
472
  this[inner] = i;
@@ -399,7 +482,7 @@ export class GPUAdapter {
399
482
  requiredLimits.add(key, numToBigIntOptional(value));
400
483
  }
401
484
  }
402
- return new GPUDevice(key, this[inner].requestDevice({
485
+ return new GPUDevice(key, await this[inner].requestDevice({
403
486
  ...descriptor,
404
487
  requiredFeatures,
405
488
  requiredLimits,
@@ -412,7 +495,12 @@ export class GPUAdapter {
412
495
  return "GPUAdapter";
413
496
  }
414
497
  get info() {
415
- return new GPUAdapterInfo(key, this[inner].info());
498
+ // From the spec:
499
+ // > The same object is returned each time. To create that object for the first time:
500
+ // https://www.w3.org/TR/webgpu/#dom-gpuadapter-info
501
+ if (!this.#info)
502
+ this.#info = new GPUAdapterInfo(key, this[inner].info());
503
+ return this.#info;
416
504
  }
417
505
  get features() {
418
506
  return new GPUSupportedFeatures(key, this[inner].features());
@@ -420,9 +508,6 @@ export class GPUAdapter {
420
508
  get limits() {
421
509
  return new GPUSupportedLimits(key, this[inner].limits());
422
510
  }
423
- get isFallbackAdapter() {
424
- return this[inner].isFallbackAdapter();
425
- }
426
511
  }
427
512
  export class GPUSupportedFeatures {
428
513
  [inner];
@@ -482,6 +567,9 @@ export class GPUSupportedLimits {
482
567
  get maxBindGroupsPlusVertexBuffers() {
483
568
  return this[inner].maxBindGroupsPlusVertexBuffers();
484
569
  }
570
+ get maxImmediateSize() {
571
+ return this[inner].maxImmediateSize();
572
+ }
485
573
  get maxBindingsPerBindGroup() {
486
574
  return this[inner].maxBindingsPerBindGroup();
487
575
  }
@@ -579,9 +667,13 @@ export class GPUAdapterInfo {
579
667
  get description() {
580
668
  return this[inner].description();
581
669
  }
670
+ get isFallbackAdapter() {
671
+ return this[inner].isFallbackAdapter();
672
+ }
582
673
  }
583
674
  export class GPUDevice extends EventTarget {
584
675
  [inner];
676
+ #adapterInfo;
585
677
  constructor(k, i) {
586
678
  privateConstructorCalled(k);
587
679
  super();
@@ -609,7 +701,12 @@ export class GPUDevice extends EventTarget {
609
701
  return new GPUQueue(key, this[inner].queue());
610
702
  }
611
703
  get adapterInfo() {
612
- return new GPUAdapterInfo(key, this[inner].adapterInfo());
704
+ // From the spec:
705
+ // > The same object is returned each time. To create that object for the first time:
706
+ // https://www.w3.org/TR/webgpu/#dom-gpudevice-adapterinfo
707
+ if (!this.#adapterInfo)
708
+ this.#adapterInfo = new GPUAdapterInfo(key, this[inner].adapterInfo());
709
+ return this.#adapterInfo;
613
710
  }
614
711
  destroy() {
615
712
  this[inner].destroy();
@@ -618,6 +715,7 @@ export class GPUDevice extends EventTarget {
618
715
  return new GPUBuffer(key, this[inner].createBuffer({
619
716
  ...descriptor,
620
717
  size: numToBigInt(descriptor.size),
718
+ usage: convertBufferUsageWebToWasi(descriptor.usage),
621
719
  }));
622
720
  }
623
721
  createTexture(descriptor) {
@@ -627,12 +725,17 @@ export class GPUDevice extends EventTarget {
627
725
  let viewFormats;
628
726
  if (descriptor.viewFormats)
629
727
  viewFormats = Array.from(descriptor.viewFormats).map(f => convertTextureFormatWebToWasi(f));
728
+ let textureBindingViewDimension;
729
+ if (descriptor.textureBindingViewDimension)
730
+ textureBindingViewDimension = convertTextureViewDimensionWebToWasi(descriptor.textureBindingViewDimension);
630
731
  return new GPUTexture(key, this[inner].createTexture({
631
732
  ...descriptor,
632
733
  size: convertExtent3DWebToWasi(descriptor.size),
633
734
  format: convertTextureFormatWebToWasi(descriptor.format),
735
+ usage: convertTextureUsageWebToWasi(descriptor.usage),
634
736
  dimension,
635
737
  viewFormats,
738
+ textureBindingViewDimension,
636
739
  }));
637
740
  }
638
741
  createSampler(descriptor) {
@@ -672,6 +775,7 @@ export class GPUDevice extends EventTarget {
672
775
  }
673
776
  return {
674
777
  ...entry,
778
+ visibility: convertShaderStageWebToWasi(entry.visibility),
675
779
  buffer,
676
780
  texture,
677
781
  storageTexture,
@@ -682,7 +786,17 @@ export class GPUDevice extends EventTarget {
682
786
  createPipelineLayout(descriptor) {
683
787
  return new GPUPipelineLayout(key, this[inner].createPipelineLayout({
684
788
  ...descriptor,
685
- bindGroupLayouts: Array.from(descriptor.bindGroupLayouts).map(b => b[inner])
789
+ bindGroupLayouts: Array.from(descriptor.bindGroupLayouts).map(bindGroup => {
790
+ if (bindGroup instanceof GPUBindGroupLayout) {
791
+ return bindGroup[inner];
792
+ }
793
+ else if (bindGroup === null || bindGroup === undefined) {
794
+ return undefined;
795
+ }
796
+ else {
797
+ throw new Unreachable;
798
+ }
799
+ }),
686
800
  }));
687
801
  }
688
802
  createBindGroup(descriptor) {
@@ -700,7 +814,7 @@ export class GPUDevice extends EventTarget {
700
814
  createShaderModule(descriptor) {
701
815
  let compilationHints;
702
816
  if (descriptor.compilationHints) {
703
- compilationHints = descriptor.compilationHints.map(hint => {
817
+ compilationHints = Array.from(descriptor.compilationHints).map(hint => {
704
818
  let layout;
705
819
  if (hint.layout) {
706
820
  layout = convertGpuLayoutWebToWasi(hint.layout);
@@ -771,9 +885,13 @@ export class GPUDevice extends EventTarget {
771
885
  module: descriptor.fragment.module[inner],
772
886
  targets: Array.from(descriptor.fragment.targets).map(target => {
773
887
  if (target) {
888
+ let writeMask;
889
+ if (target.writeMask !== undefined)
890
+ writeMask = convertColorWriteWebToWasi(target.writeMask);
774
891
  return {
775
892
  ...target,
776
893
  format: convertTextureFormatWebToWasi(target.format),
894
+ writeMask,
777
895
  };
778
896
  }
779
897
  else {
@@ -790,11 +908,19 @@ export class GPUDevice extends EventTarget {
790
908
  layout: convertGpuLayoutWebToWasi(descriptor.layout),
791
909
  }));
792
910
  }
793
- async createComputePipelineAsync(_descriptor) {
794
- throw new Todo();
911
+ async createComputePipelineAsync(descriptor) {
912
+ // TODO: make this really async
913
+ await Promise.resolve();
914
+ const result = this.createComputePipeline(descriptor);
915
+ await Promise.resolve();
916
+ return result;
795
917
  }
796
918
  async createRenderPipelineAsync(descriptor) {
797
- throw new Todo();
919
+ // TODO: make this really async
920
+ await Promise.resolve();
921
+ const result = this.createRenderPipeline(descriptor);
922
+ await Promise.resolve();
923
+ return result;
798
924
  }
799
925
  createCommandEncoder(descriptor = {}) {
800
926
  return new GPUCommandEncoder(key, this[inner].createCommandEncoder(descriptor));
@@ -823,7 +949,15 @@ export class GPUDevice extends EventTarget {
823
949
  this[inner].pushErrorScope(filter);
824
950
  }
825
951
  async popErrorScope() {
826
- throw new Todo();
952
+ const err = await this[inner].popErrorScope();
953
+ if (err) {
954
+ return {
955
+ message: err.message(),
956
+ };
957
+ }
958
+ else {
959
+ return null;
960
+ }
827
961
  }
828
962
  importExternalTexture(descriptor) {
829
963
  throw new Todo;
@@ -836,6 +970,9 @@ export class GPURenderBundleEncoder {
836
970
  privateConstructorCalled(k);
837
971
  this[inner] = i;
838
972
  }
973
+ setImmediates(rangeOffset, data, dataOffset, dataSize) {
974
+ this[inner].setImmediates(rangeOffset, convertBufferToUint8Array(data), numToBigIntOptional(dataOffset), numToBigIntOptional(dataSize));
975
+ }
839
976
  get __brand() {
840
977
  return "GPURenderBundleEncoder";
841
978
  }
@@ -867,7 +1004,10 @@ export class GPURenderBundleEncoder {
867
1004
  this[inner].setIndexBuffer(buffer[inner], indexFormat, numToBigIntOptional(offset), numToBigIntOptional(size));
868
1005
  }
869
1006
  setVertexBuffer(slot, buffer, offset, size) {
870
- this[inner].setVertexBuffer(slot, buffer[inner], numToBigIntOptional(offset), numToBigIntOptional(size));
1007
+ let gfxBuffer;
1008
+ if (buffer)
1009
+ gfxBuffer = buffer[inner];
1010
+ this[inner].setVertexBuffer(slot, gfxBuffer, numToBigIntOptional(offset), numToBigIntOptional(size));
871
1011
  }
872
1012
  draw(vertexCount, instanceCount, firstVertex, firstInstance) {
873
1013
  this[inner].draw(vertexCount, instanceCount, firstVertex, firstInstance);
@@ -913,8 +1053,49 @@ export class GPUShaderModule {
913
1053
  set label(value) {
914
1054
  this[inner].setLabel(value);
915
1055
  }
916
- getCompilationInfo() {
917
- throw new Todo();
1056
+ async getCompilationInfo() {
1057
+ return new GPUCompilationInfo(key, await this[inner].getCompilationInfo());
1058
+ }
1059
+ }
1060
+ export class GPUCompilationInfo {
1061
+ [inner];
1062
+ constructor(k, i) {
1063
+ privateConstructorCalled(k);
1064
+ this[inner] = i;
1065
+ }
1066
+ get __brand() {
1067
+ return 'GPUCompilationInfo';
1068
+ }
1069
+ get messages() {
1070
+ return this[inner].messages().map(m => new GPUCompilationMessage(key, m));
1071
+ }
1072
+ }
1073
+ export class GPUCompilationMessage {
1074
+ [inner];
1075
+ constructor(k, i) {
1076
+ privateConstructorCalled(k);
1077
+ this[inner] = i;
1078
+ }
1079
+ get __brand() {
1080
+ return 'GPUCompilationMessage';
1081
+ }
1082
+ get message() {
1083
+ return this[inner].message();
1084
+ }
1085
+ get type() {
1086
+ return this[inner].type();
1087
+ }
1088
+ get lineNum() {
1089
+ return bigIntToNum(this[inner].lineNum());
1090
+ }
1091
+ get linePos() {
1092
+ return bigIntToNum(this[inner].linePos());
1093
+ }
1094
+ get offset() {
1095
+ return bigIntToNum(this[inner].offset());
1096
+ }
1097
+ get length() {
1098
+ return bigIntToNum(this[inner].length());
918
1099
  }
919
1100
  }
920
1101
  export class GPUComputePipeline {
@@ -993,13 +1174,13 @@ export class GPUBuffer {
993
1174
  return bigIntToNum(this[inner].size());
994
1175
  }
995
1176
  get usage() {
996
- return this[inner].usage();
1177
+ return convertBufferUsageWasiToWeb(this[inner].usage());
997
1178
  }
998
1179
  get mapState() {
999
1180
  return this[inner].mapState();
1000
1181
  }
1001
1182
  async mapAsync(mode, offset, size) {
1002
- this[inner].mapAsync(mode, numToBigIntOptional(offset), numToBigIntOptional(size));
1183
+ await this[inner].mapAsync(convertMapModeWebToWasi(mode), numToBigIntOptional(offset), numToBigIntOptional(size));
1003
1184
  }
1004
1185
  getMappedRange(offset, size) {
1005
1186
  this._mappedRangeBuffer = this[inner].getMappedRangeGetWithCopy(numToBigIntOptional(offset), numToBigIntOptional(size));
@@ -1165,7 +1346,7 @@ export class GPUTexture {
1165
1346
  return convertTextureFormatWasiToWeb(this[inner].format());
1166
1347
  }
1167
1348
  get usage() {
1168
- return this[inner].usage();
1349
+ return convertTextureUsageWasiToWeb(this[inner].usage());
1169
1350
  }
1170
1351
  createView(descriptor) {
1171
1352
  let gfxDescriptor;
@@ -1176,10 +1357,14 @@ export class GPUTexture {
1176
1357
  let dimension;
1177
1358
  if (descriptor.dimension)
1178
1359
  dimension = convertTextureViewDimensionWebToWasi(descriptor.dimension);
1360
+ let usage;
1361
+ if (descriptor.usage !== undefined)
1362
+ usage = convertTextureUsageWebToWasi(descriptor.usage);
1179
1363
  gfxDescriptor = {
1180
1364
  ...descriptor,
1181
1365
  dimension,
1182
1366
  format,
1367
+ usage,
1183
1368
  };
1184
1369
  }
1185
1370
  return new GPUTextureView(key, this[inner].createView(gfxDescriptor));
@@ -1240,8 +1425,22 @@ export class GPUCommandEncoder {
1240
1425
  timestampWrites,
1241
1426
  }));
1242
1427
  }
1243
- copyBufferToBuffer(source, sourceOffset, destination, destinationOffset, size) {
1244
- this[inner].copyBufferToBuffer(source[inner], numToBigInt(sourceOffset), destination[inner], numToBigInt(destinationOffset), numToBigInt(size));
1428
+ copyBufferToBuffer(source, arg2, arg3, destinationOffset, size) {
1429
+ if (arg2 instanceof GPUBuffer && (typeof arg3 === "number" || arg3 === undefined)) {
1430
+ // (source, destination, size?)
1431
+ const destination = arg2;
1432
+ const size = arg3;
1433
+ this[inner].copyBufferToBuffer(source[inner], undefined, destination[inner], undefined, numToBigIntOptional(size));
1434
+ }
1435
+ else if (typeof arg2 === "number" && arg3 instanceof GPUBuffer) {
1436
+ // (source, sourceOffset, destination, destinationOffset, size?)
1437
+ const sourceOffset = arg2;
1438
+ const destination = arg3;
1439
+ this[inner].copyBufferToBuffer(source[inner], numToBigInt(sourceOffset), destination[inner], numToBigIntOptional(destinationOffset), numToBigIntOptional(size));
1440
+ }
1441
+ else {
1442
+ throw new Unreachable;
1443
+ }
1245
1444
  }
1246
1445
  copyBufferToTexture(source, destination, copySize) {
1247
1446
  let destinationOrigin;
@@ -1366,6 +1565,9 @@ export class GPUComputePassEncoder {
1366
1565
  throw new Todo;
1367
1566
  }
1368
1567
  }
1568
+ setImmediates(rangeOffset, data, dataOffset, dataSize) {
1569
+ this[inner].setImmediates(rangeOffset, convertBufferToUint8Array(data), numToBigIntOptional(dataOffset), numToBigIntOptional(dataSize));
1570
+ }
1369
1571
  }
1370
1572
  export class GPURenderPassEncoder {
1371
1573
  [inner];
@@ -1418,6 +1620,9 @@ export class GPURenderPassEncoder {
1418
1620
  setBindGroup(index, bindGroup, dynamicOffsetsData, dynamicOffsetsDataStart, dynamicOffsetsDataLength) {
1419
1621
  throw new Todo;
1420
1622
  }
1623
+ setImmediates(rangeOffset, data, dataOffset, dataSize) {
1624
+ this[inner].setImmediates(rangeOffset, convertBufferToUint8Array(data), numToBigIntOptional(dataOffset), numToBigIntOptional(dataSize));
1625
+ }
1421
1626
  setPipeline(pipeline) {
1422
1627
  this[inner].setPipeline(pipeline[inner]);
1423
1628
  }
@@ -1510,6 +1715,7 @@ export const GPUTextureUsage = {
1510
1715
  TEXTURE_BINDING: 0x04,
1511
1716
  STORAGE_BINDING: 0x08,
1512
1717
  RENDER_ATTACHMENT: 0x10,
1718
+ TRANSIENT_ATTACHMENT: 0x20,
1513
1719
  };
1514
1720
  export const GPUShaderStage = {
1515
1721
  VERTEX: 0x1,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wasi-gfx/js-webgpu",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "WebGPU JavaScript API on top of wasi-gfx",
5
5
  "exports": {
6
6
  "./canvas": {
@@ -21,26 +21,32 @@
21
21
  "wit/",
22
22
  "types/"
23
23
  ],
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/wasi-gfx/wasi-gfx-js.git"
27
+ },
24
28
  "type": "module",
25
29
  "scripts": {
26
30
  "build": "cd src && tsc",
27
31
  "check": "cd src && tsc --noEmit && cd ../examples && tsc --noEmit",
28
- "generate-types": "rm -rf types && jco types wit --out-dir=types",
32
+ "wit": "npm run wit:fetch && npm run wit:bindings",
33
+ "wit:fetch": "wkg fetch --config ./wkg-config.toml",
34
+ "wit:bindings": "rm -rf types && jco types wit --out-dir=types",
29
35
  "test": "npm run test:generate-spec-list && npm run test:rollup && npm run test:jco && echo 'Test wasm built but not run temp/tests.wasm'",
30
36
  "test:generate-spec-list": "node tests/generate-spec-list.js",
31
37
  "test:rollup": "rollup --config './tests/rollup.config.js'",
32
- "test:jco": "jco componentize ./temp/tests.js --wit ./wit -n imports -o out/tests.wasm --disable 'http'",
38
+ "test:jco": "jco componentize --backend qjs ./temp/tests.js --wit ./wit -n imports -o out/tests.wasm --disable 'http'",
33
39
  "example": "npm run example:rollup && npm run example:jco",
34
40
  "example:rollup": "rollup --config './examples/rollup.config.js' --configExample=$npm_config_example",
35
- "example:jco": "mkdir -p out && jco componentize temp/$npm_config_example.js --wit ./wit -n imports -o out/$npm_config_example.wasm --disable 'http' --debug-starlingmonkey-build"
41
+ "example:jco": "mkdir -p out && jco componentize --backend qjs temp/$npm_config_example.js --wit ./wit -n imports -o out/$npm_config_example.wasm"
36
42
  },
37
43
  "devDependencies": {
38
- "@bytecodealliance/jco": "^1.11.2",
44
+ "@bytecodealliance/jco": "^1.40.0",
39
45
  "@rollup/plugin-typescript": "^12.1.1",
40
46
  "@types/dom-mediacapture-transform": "^0.1.10",
41
47
  "@types/node": "22.15.7",
42
48
  "@types/w3c-image-capture": "^1.0.10",
43
- "@webgpu/types": "^0.1.52",
49
+ "@webgpu/types": "^0.1.74",
44
50
  "rollup": "^4.12.0",
45
51
  "tslib": "^2.8.1",
46
52
  "typescript": "^5.7.2"
@@ -1,5 +1,5 @@
1
- /** @module Interface wasi:cli/run@0.2.0 **/
1
+ /** @module Interface wasi:cli/run@0.3.0 **/
2
2
  /**
3
3
  * Run the program.
4
4
  */
5
- export function run(): void;
5
+ export function run(): Promise<void>;
@@ -1,3 +1,14 @@
1
- /** @module Interface wasi:cli/stdout@0.2.0 **/
2
- export function getStdout(): OutputStream;
3
- export type OutputStream = import('./wasi-io-streams.js').OutputStream;
1
+ /** @module Interface wasi:cli/stdout@0.3.0 **/
2
+ /**
3
+ * Write the given stream to stdout.
4
+ *
5
+ * If the stream's writable end is dropped this function will either return
6
+ * success once the entire contents of the stream have been written or an
7
+ * error-code representing a failure.
8
+ *
9
+ * Otherwise if there is an error the readable end of the stream will be
10
+ * dropped and this function will return an error-code.
11
+ */
12
+ export function writeViaStream(data: AsyncIterable<number>): PromiseLike<Result<void, ErrorCode>>;
13
+ export type ErrorCode = import('./wasi-cli-types.js').ErrorCode;
14
+ export type Result<T, E> = { tag: 'ok', val: T } | { tag: 'err', val: E };
@@ -0,0 +1,15 @@
1
+ /** @module Interface wasi:cli/types@0.3.0 **/
2
+ /**
3
+ * # Variants
4
+ *
5
+ * ## `"io"`
6
+ *
7
+ * Input/output error
8
+ * ## `"illegal-byte-sequence"`
9
+ *
10
+ * Invalid or incomplete multibyte or wide character
11
+ * ## `"pipe"`
12
+ *
13
+ * Broken pipe
14
+ */
15
+ export type ErrorCode = 'io' | 'illegal-byte-sequence' | 'pipe';
@@ -0,0 +1,13 @@
1
+ /** @module Interface wasi-gfx:frame-buffer/frame-buffer@0.2.0 **/
2
+
3
+ export class Buffer {
4
+ /**
5
+ * This type does not have a public constructor.
6
+ */
7
+ private constructor();
8
+ /**
9
+ * TODO: This should be replaced with something that doesn't require a copy.
10
+ */
11
+ getWithCopy(): Uint8Array;
12
+ setWithCopy(val: Uint8Array): void;
13
+ }
@@ -0,0 +1,12 @@
1
+ /** @module Interface wasi-gfx:surface/surface-frame-buffer@0.2.0 **/
2
+ export type Buffer = import('./wasi-gfx-frame-buffer-frame-buffer.js').Buffer;
3
+ export type Surface = import('./wasi-gfx-surface-surface.js').Surface;
4
+
5
+ export class Context {
6
+ constructor(surface: Surface)
7
+ getCurrentBuffer(): Buffer;
8
+ /**
9
+ * TODO: consider if needed
10
+ */
11
+ present(): void;
12
+ }
@@ -0,0 +1,29 @@
1
+ /** @module Interface wasi-gfx:surface/surface-webgpu@0.2.0 **/
2
+ export type GpuDevice = import('./wasi-webgpu-webgpu.js').GpuDevice;
3
+ export type GpuTexture = import('./wasi-webgpu-webgpu.js').GpuTexture;
4
+ export type GpuTextureFormat = import('./wasi-webgpu-webgpu.js').GpuTextureFormat;
5
+ export type GpuTextureUsage = import('./wasi-webgpu-webgpu.js').GpuTextureUsage;
6
+ export type PredefinedColorSpace = import('./wasi-webgpu-webgpu.js').PredefinedColorSpace;
7
+ export type GpuCanvasToneMapping = import('./wasi-webgpu-webgpu.js').GpuCanvasToneMapping;
8
+ export type GpuCanvasAlphaMode = import('./wasi-webgpu-webgpu.js').GpuCanvasAlphaMode;
9
+ export type Surface = import('./wasi-gfx-surface-surface.js').Surface;
10
+ export interface ContextConfiguration {
11
+ device: GpuDevice,
12
+ format: GpuTextureFormat,
13
+ usage?: GpuTextureUsage,
14
+ viewFormats?: Array<GpuTextureFormat>,
15
+ colorSpace?: PredefinedColorSpace,
16
+ toneMapping?: GpuCanvasToneMapping,
17
+ alphaMode?: GpuCanvasAlphaMode,
18
+ }
19
+
20
+ export class Context {
21
+ constructor(surface: Surface)
22
+ configure(configuration: ContextConfiguration): void;
23
+ unconfigure(): void;
24
+ getCurrentTexture(): GpuTexture;
25
+ /**
26
+ * TODO: consider if needed
27
+ */
28
+ present(): void;
29
+ }