Skip to content

Commit e0e9459

Browse files
committed
Generate spec from official WebGPU spec!
1 parent 3706d9c commit e0e9459

File tree

16 files changed

+1861
-46
lines changed

16 files changed

+1861
-46
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "webgpu/generate/webgpu-spec"]
2+
path = webgpu/generate/webgpu-spec
3+
url = https://github.com/gpuweb/gpuweb.git

webgpu/deps.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@
22
path = "../graphics-context"
33
sha256 = "cf022dcf41c7e9f6d3ca8af2f80f523306472a5548d38199d57986fc75aafb7b"
44
sha512 = "b5cf0248f2cd1dbfbeb500b375e56fecbe8b1ac147b1aece73032543e696e0f7dad3da5aa99fbade290369a065059ae606def00d7d1aa94cde7f19ee03741479"
5+
6+
[io]
7+
url = "https://github.com/WebAssembly/wasi-io/archive/fef02cddb4ac28dcde97bf11a4c7b856833a948f.tar.gz"
8+
sha256 = "7210e5653539a15478f894d4da24cc69d61924cbcba21d2804d69314a88e5a4c"
9+
sha512 = "49184a1b0945a889abd52d25271172ed3dc2db6968fcdddb1bab7ee0081f4a3eeee0977ad2291126a37631c0d86eeea75d822fa8af224c422134500bf9f0f2bb"

webgpu/deps.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
io = "https://github.com/WebAssembly/wasi-io/archive/fef02cddb4ac28dcde97bf11a4c7b856833a948f.tar.gz"
2+
13
[graphics-context]
24
path = "../graphics-context"

webgpu/deps/io/error.wit

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package wasi:io@0.2.0;
2+
3+
4+
interface error {
5+
/// A resource which represents some error information.
6+
///
7+
/// The only method provided by this resource is `to-debug-string`,
8+
/// which provides some human-readable information about the error.
9+
///
10+
/// In the `wasi:io` package, this resource is returned through the
11+
/// `wasi:io/streams/stream-error` type.
12+
///
13+
/// To provide more specific error information, other interfaces may
14+
/// provide functions to further "downcast" this error into more specific
15+
/// error information. For example, `error`s returned in streams derived
16+
/// from filesystem types to be described using the filesystem's own
17+
/// error-code type, using the function
18+
/// `wasi:filesystem/types/filesystem-error-code`, which takes a parameter
19+
/// `borrow<error>` and returns
20+
/// `option<wasi:filesystem/types/error-code>`.
21+
///
22+
/// The set of functions which can "downcast" an `error` into a more
23+
/// concrete type is open.
24+
resource error {
25+
/// Returns a string that is suitable to assist humans in debugging
26+
/// this error.
27+
///
28+
/// WARNING: The returned string should not be consumed mechanically!
29+
/// It may change across platforms, hosts, or other implementation
30+
/// details. Parsing this string is a major platform-compatibility
31+
/// hazard.
32+
to-debug-string: func() -> string;
33+
}
34+
}

webgpu/deps/io/poll.wit

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package wasi:io@0.2.0;
2+
3+
/// A poll API intended to let users wait for I/O events on multiple handles
4+
/// at once.
5+
interface poll {
6+
/// `pollable` represents a single I/O event which may be ready, or not.
7+
resource pollable {
8+
9+
/// Return the readiness of a pollable. This function never blocks.
10+
///
11+
/// Returns `true` when the pollable is ready, and `false` otherwise.
12+
ready: func() -> bool;
13+
14+
/// `block` returns immediately if the pollable is ready, and otherwise
15+
/// blocks until ready.
16+
///
17+
/// This function is equivalent to calling `poll.poll` on a list
18+
/// containing only this pollable.
19+
block: func();
20+
}
21+
22+
/// Poll for completion on a set of pollables.
23+
///
24+
/// This function takes a list of pollables, which identify I/O sources of
25+
/// interest, and waits until one or more of the events is ready for I/O.
26+
///
27+
/// The result `list<u32>` contains one or more indices of handles in the
28+
/// argument list that is ready for I/O.
29+
///
30+
/// If the list contains more elements than can be indexed with a `u32`
31+
/// value, this function traps.
32+
///
33+
/// A timeout can be implemented by adding a pollable from the
34+
/// wasi-clocks API to the list.
35+
///
36+
/// This function does not return a `result`; polling in itself does not
37+
/// do any I/O so it doesn't fail. If any of the I/O sources identified by
38+
/// the pollables has an error, it is indicated by marking the source as
39+
/// being reaedy for I/O.
40+
poll: func(in: list<borrow<pollable>>) -> list<u32>;
41+
}

webgpu/deps/io/streams.wit

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
package wasi:io@0.2.0;
2+
3+
/// WASI I/O is an I/O abstraction API which is currently focused on providing
4+
/// stream types.
5+
///
6+
/// In the future, the component model is expected to add built-in stream types;
7+
/// when it does, they are expected to subsume this API.
8+
interface streams {
9+
use error.{error};
10+
use poll.{pollable};
11+
12+
/// An error for input-stream and output-stream operations.
13+
variant stream-error {
14+
/// The last operation (a write or flush) failed before completion.
15+
///
16+
/// More information is available in the `error` payload.
17+
last-operation-failed(error),
18+
/// The stream is closed: no more input will be accepted by the
19+
/// stream. A closed output-stream will return this error on all
20+
/// future operations.
21+
closed
22+
}
23+
24+
/// An input bytestream.
25+
///
26+
/// `input-stream`s are *non-blocking* to the extent practical on underlying
27+
/// platforms. I/O operations always return promptly; if fewer bytes are
28+
/// promptly available than requested, they return the number of bytes promptly
29+
/// available, which could even be zero. To wait for data to be available,
30+
/// use the `subscribe` function to obtain a `pollable` which can be polled
31+
/// for using `wasi:io/poll`.
32+
resource input-stream {
33+
/// Perform a non-blocking read from the stream.
34+
///
35+
/// When the source of a `read` is binary data, the bytes from the source
36+
/// are returned verbatim. When the source of a `read` is known to the
37+
/// implementation to be text, bytes containing the UTF-8 encoding of the
38+
/// text are returned.
39+
///
40+
/// This function returns a list of bytes containing the read data,
41+
/// when successful. The returned list will contain up to `len` bytes;
42+
/// it may return fewer than requested, but not more. The list is
43+
/// empty when no bytes are available for reading at this time. The
44+
/// pollable given by `subscribe` will be ready when more bytes are
45+
/// available.
46+
///
47+
/// This function fails with a `stream-error` when the operation
48+
/// encounters an error, giving `last-operation-failed`, or when the
49+
/// stream is closed, giving `closed`.
50+
///
51+
/// When the caller gives a `len` of 0, it represents a request to
52+
/// read 0 bytes. If the stream is still open, this call should
53+
/// succeed and return an empty list, or otherwise fail with `closed`.
54+
///
55+
/// The `len` parameter is a `u64`, which could represent a list of u8 which
56+
/// is not possible to allocate in wasm32, or not desirable to allocate as
57+
/// as a return value by the callee. The callee may return a list of bytes
58+
/// less than `len` in size while more bytes are available for reading.
59+
read: func(
60+
/// The maximum number of bytes to read
61+
len: u64
62+
) -> result<list<u8>, stream-error>;
63+
64+
/// Read bytes from a stream, after blocking until at least one byte can
65+
/// be read. Except for blocking, behavior is identical to `read`.
66+
blocking-read: func(
67+
/// The maximum number of bytes to read
68+
len: u64
69+
) -> result<list<u8>, stream-error>;
70+
71+
/// Skip bytes from a stream. Returns number of bytes skipped.
72+
///
73+
/// Behaves identical to `read`, except instead of returning a list
74+
/// of bytes, returns the number of bytes consumed from the stream.
75+
skip: func(
76+
/// The maximum number of bytes to skip.
77+
len: u64,
78+
) -> result<u64, stream-error>;
79+
80+
/// Skip bytes from a stream, after blocking until at least one byte
81+
/// can be skipped. Except for blocking behavior, identical to `skip`.
82+
blocking-skip: func(
83+
/// The maximum number of bytes to skip.
84+
len: u64,
85+
) -> result<u64, stream-error>;
86+
87+
/// Create a `pollable` which will resolve once either the specified stream
88+
/// has bytes available to read or the other end of the stream has been
89+
/// closed.
90+
/// The created `pollable` is a child resource of the `input-stream`.
91+
/// Implementations may trap if the `input-stream` is dropped before
92+
/// all derived `pollable`s created with this function are dropped.
93+
subscribe: func() -> pollable;
94+
}
95+
96+
97+
/// An output bytestream.
98+
///
99+
/// `output-stream`s are *non-blocking* to the extent practical on
100+
/// underlying platforms. Except where specified otherwise, I/O operations also
101+
/// always return promptly, after the number of bytes that can be written
102+
/// promptly, which could even be zero. To wait for the stream to be ready to
103+
/// accept data, the `subscribe` function to obtain a `pollable` which can be
104+
/// polled for using `wasi:io/poll`.
105+
resource output-stream {
106+
/// Check readiness for writing. This function never blocks.
107+
///
108+
/// Returns the number of bytes permitted for the next call to `write`,
109+
/// or an error. Calling `write` with more bytes than this function has
110+
/// permitted will trap.
111+
///
112+
/// When this function returns 0 bytes, the `subscribe` pollable will
113+
/// become ready when this function will report at least 1 byte, or an
114+
/// error.
115+
check-write: func() -> result<u64, stream-error>;
116+
117+
/// Perform a write. This function never blocks.
118+
///
119+
/// When the destination of a `write` is binary data, the bytes from
120+
/// `contents` are written verbatim. When the destination of a `write` is
121+
/// known to the implementation to be text, the bytes of `contents` are
122+
/// transcoded from UTF-8 into the encoding of the destination and then
123+
/// written.
124+
///
125+
/// Precondition: check-write gave permit of Ok(n) and contents has a
126+
/// length of less than or equal to n. Otherwise, this function will trap.
127+
///
128+
/// returns Err(closed) without writing if the stream has closed since
129+
/// the last call to check-write provided a permit.
130+
write: func(
131+
contents: list<u8>
132+
) -> result<_, stream-error>;
133+
134+
/// Perform a write of up to 4096 bytes, and then flush the stream. Block
135+
/// until all of these operations are complete, or an error occurs.
136+
///
137+
/// This is a convenience wrapper around the use of `check-write`,
138+
/// `subscribe`, `write`, and `flush`, and is implemented with the
139+
/// following pseudo-code:
140+
///
141+
/// ```text
142+
/// let pollable = this.subscribe();
143+
/// while !contents.is_empty() {
144+
/// // Wait for the stream to become writable
145+
/// pollable.block();
146+
/// let Ok(n) = this.check-write(); // eliding error handling
147+
/// let len = min(n, contents.len());
148+
/// let (chunk, rest) = contents.split_at(len);
149+
/// this.write(chunk ); // eliding error handling
150+
/// contents = rest;
151+
/// }
152+
/// this.flush();
153+
/// // Wait for completion of `flush`
154+
/// pollable.block();
155+
/// // Check for any errors that arose during `flush`
156+
/// let _ = this.check-write(); // eliding error handling
157+
/// ```
158+
blocking-write-and-flush: func(
159+
contents: list<u8>
160+
) -> result<_, stream-error>;
161+
162+
/// Request to flush buffered output. This function never blocks.
163+
///
164+
/// This tells the output-stream that the caller intends any buffered
165+
/// output to be flushed. the output which is expected to be flushed
166+
/// is all that has been passed to `write` prior to this call.
167+
///
168+
/// Upon calling this function, the `output-stream` will not accept any
169+
/// writes (`check-write` will return `ok(0)`) until the flush has
170+
/// completed. The `subscribe` pollable will become ready when the
171+
/// flush has completed and the stream can accept more writes.
172+
flush: func() -> result<_, stream-error>;
173+
174+
/// Request to flush buffered output, and block until flush completes
175+
/// and stream is ready for writing again.
176+
blocking-flush: func() -> result<_, stream-error>;
177+
178+
/// Create a `pollable` which will resolve once the output-stream
179+
/// is ready for more writing, or an error has occured. When this
180+
/// pollable is ready, `check-write` will return `ok(n)` with n>0, or an
181+
/// error.
182+
///
183+
/// If the stream is closed, this pollable is always ready immediately.
184+
///
185+
/// The created `pollable` is a child resource of the `output-stream`.
186+
/// Implementations may trap if the `output-stream` is dropped before
187+
/// all derived `pollable`s created with this function are dropped.
188+
subscribe: func() -> pollable;
189+
190+
/// Write zeroes to a stream.
191+
///
192+
/// This should be used precisely like `write` with the exact same
193+
/// preconditions (must use check-write first), but instead of
194+
/// passing a list of bytes, you simply pass the number of zero-bytes
195+
/// that should be written.
196+
write-zeroes: func(
197+
/// The number of zero-bytes to write
198+
len: u64
199+
) -> result<_, stream-error>;
200+
201+
/// Perform a write of up to 4096 zeroes, and then flush the stream.
202+
/// Block until all of these operations are complete, or an error
203+
/// occurs.
204+
///
205+
/// This is a convenience wrapper around the use of `check-write`,
206+
/// `subscribe`, `write-zeroes`, and `flush`, and is implemented with
207+
/// the following pseudo-code:
208+
///
209+
/// ```text
210+
/// let pollable = this.subscribe();
211+
/// while num_zeroes != 0 {
212+
/// // Wait for the stream to become writable
213+
/// pollable.block();
214+
/// let Ok(n) = this.check-write(); // eliding error handling
215+
/// let len = min(n, num_zeroes);
216+
/// this.write-zeroes(len); // eliding error handling
217+
/// num_zeroes -= len;
218+
/// }
219+
/// this.flush();
220+
/// // Wait for completion of `flush`
221+
/// pollable.block();
222+
/// // Check for any errors that arose during `flush`
223+
/// let _ = this.check-write(); // eliding error handling
224+
/// ```
225+
blocking-write-zeroes-and-flush: func(
226+
/// The number of zero-bytes to write
227+
len: u64
228+
) -> result<_, stream-error>;
229+
230+
/// Read from one stream and write to another.
231+
///
232+
/// The behavior of splice is equivelant to:
233+
/// 1. calling `check-write` on the `output-stream`
234+
/// 2. calling `read` on the `input-stream` with the smaller of the
235+
/// `check-write` permitted length and the `len` provided to `splice`
236+
/// 3. calling `write` on the `output-stream` with that read data.
237+
///
238+
/// Any error reported by the call to `check-write`, `read`, or
239+
/// `write` ends the splice and reports that error.
240+
///
241+
/// This function returns the number of bytes transferred; it may be less
242+
/// than `len`.
243+
splice: func(
244+
/// The stream to read from
245+
src: borrow<input-stream>,
246+
/// The number of bytes to splice
247+
len: u64,
248+
) -> result<u64, stream-error>;
249+
250+
/// Read from one stream and write to another, with blocking.
251+
///
252+
/// This is similar to `splice`, except that it blocks until the
253+
/// `output-stream` is ready for writing, and the `input-stream`
254+
/// is ready for reading, before performing the `splice`.
255+
blocking-splice: func(
256+
/// The stream to read from
257+
src: borrow<input-stream>,
258+
/// The number of bytes to splice
259+
len: u64,
260+
) -> result<u64, stream-error>;
261+
}
262+
}

webgpu/deps/io/world.wit

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package wasi:io@0.2.0;
2+
3+
world imports {
4+
import streams;
5+
import poll;
6+
}

webgpu/generate/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

0 commit comments

Comments
 (0)