Skip to main content

gasm/
sys.rs

1//! Raw ABI. On wasm32 these are the real imports (see spec/ABI.md). On other
2//! targets they are backed by [`crate::native`], an in-process stub host used
3//! for native parity tests and benchmarks.
4//!
5//! GENERATED from spec/abi.json by scripts/gen-abi.mjs. Do not edit by hand.
6
7#![allow(clippy::missing_safety_doc)]
8
9#[cfg(target_arch = "wasm32")]
10mod imports {
11    #[link(wasm_import_module = "gasm")]
12    unsafe extern "C" {
13        /// Write a line to the runner's log.
14        pub fn log(msg: *const u8, msg_len: u32);
15        /// Monotonic time in milliseconds (virtual, frame-derived in headless runs).
16        pub fn time_ms() -> f64;
17        /// Rate (Hz) at which the runner calls gasm_frame(). Default 60; 1-1000.
18        pub fn set_frame_rate(hz: f64);
19        /// Present RGBA8 pixels (bytes R,G,B,A), stride bytes per row, w,h <= 4096. Copied before returning; letterboxed by the runner.
20        pub fn video_present(rgba: *const u8, width: u32, height: u32, stride: u32);
21        /// Format for audio_push: 8-192 kHz, 1 or 2 channels. Default 44100/2.
22        pub fn audio_config(sample_rate: u32, channels: u32);
23        /// Queue frames x channels interleaved f32 samples in [-1, 1]; the runner resamples.
24        pub fn audio_push(samples: *const f32, frames: u32);
25        /// Bitmask of GASM_BTN_* held on virtual pad player (0..3), stable within a frame.
26        pub fn input_pad(player: u32) -> u32;
27        /// UTF-8 text typed since the previous frame (backspace = \b, enter = \n), stable within a frame. Returns its length (copied only if length <= cap; cap = 0 queries), or -1 if the runner has no keyboard.
28        pub fn text_input(dst: *mut u8, cap: u32) -> i32;
29        /// Size in bytes of asset name, or -1 if it does not exist.
30        pub fn asset_size(name: *const u8, name_len: u32) -> i32;
31        /// Copy up to cap bytes of asset name into dst. Bytes copied, or -1 if missing.
32        pub fn asset_read(name: *const u8, name_len: u32, dst: *mut u8, cap: u32) -> i32;
33        /// Copy up to len bytes of asset name starting at offset (streaming). Bytes copied (0 at the end), or -1 if missing.
34        pub fn asset_read_at(name: *const u8, name_len: u32, offset: u32, dst: *mut u8, len: u32) -> i32;
35        /// Number of assets.
36        pub fn asset_count() -> u32;
37        /// Name of asset index (0 .. asset_count-1, sorted by UTF-8 bytes; folder entries as named on disk). Its length (copied only if length <= cap; cap = 0 queries), or -1 if index is out of range.
38        pub fn asset_name(index: u32, dst: *mut u8, cap: u32) -> i32;
39        /// Launch parameter value length, or -1 if unset. Copied only if length <= cap (cap = 0 queries the length).
40        pub fn param(name: *const u8, name_len: u32, dst: *mut u8, cap: u32) -> i32;
41    }
42
43    #[link(wasm_import_module = "gasm:gfx")]
44    unsafe extern "C" {
45        /// Current drawable width in pixels.
46        #[link_name = "width"]
47        pub fn gfx_width() -> u32;
48        /// Current drawable height in pixels.
49        #[link_name = "height"]
50        pub fn gfx_height() -> u32;
51        /// Compile WGSL source.
52        #[link_name = "create_shader"]
53        pub fn gfx_create_shader(wgsl: *const u8, wgsl_len: u32) -> u32;
54        /// size: non-zero multiple of 4; usage: GASM_BUF_* (WebGPU GPUBufferUsage bits; COPY_DST always added).
55        #[link_name = "create_buffer"]
56        pub fn gfx_create_buffer(size: u32, usage: u32) -> u32;
57        /// GPURenderPipelineDescriptor as JSON. layout: "auto" (default) or an array of bind group layout handles.
58        #[link_name = "create_pipeline"]
59        pub fn gfx_create_pipeline(json: *const u8, json_len: u32) -> u32;
60        /// {"layout":L,"entries":[...]} or {"pipeline":P,"group":G,"entries":[...]}; entries {"binding":B,"buffer":H,"offset":O,"size":S}, {"binding":B,"texture":T} or {"binding":B,"sampler":S}.
61        #[link_name = "create_bind_group"]
62        pub fn gfx_create_bind_group(json: *const u8, json_len: u32) -> u32;
63        /// GPUBindGroupLayoutDescriptor subset: {"entries":[{"binding":B,"visibility":GASM_STAGE_*,"buffer":{"type":"uniform"|"read-only-storage","hasDynamicOffset":bool,"minBindingSize":N} | "texture":{"sampleType":"float"} | "sampler":{"type":"filtering"}}]}.
64        #[link_name = "create_bind_group_layout"]
65        pub fn gfx_create_bind_group_layout(json: *const u8, json_len: u32) -> u32;
66        /// 2D texture: {"size":[w,h],"format":"rgba8unorm"|"rgba8unorm-srgb","mipLevelCount":n}, w,h 1-8192. Usage is TEXTURE_BINDING | COPY_DST.
67        #[link_name = "create_texture"]
68        pub fn gfx_create_texture(json: *const u8, json_len: u32) -> u32;
69        /// Upload a tightly packed RGBA8 region (len = width*height*4) of mip level mip at (x, y). Queued like write_buffer.
70        #[link_name = "write_texture"]
71        pub fn gfx_write_texture(texture: u32, mip: u32, x: u32, y: u32, width: u32, height: u32, data: *const u8, len: u32);
72        /// GPUSamplerDescriptor subset: addressModeU/V, magFilter, minFilter, mipmapFilter, lodMinClamp, lodMaxClamp, maxAnisotropy (1-16).
73        #[link_name = "create_sampler"]
74        pub fn gfx_create_sampler(json: *const u8, json_len: u32) -> u32;
75        /// Queue a write, applied before the frame's draws. offset/len: multiples of 4.
76        #[link_name = "write_buffer"]
77        pub fn gfx_write_buffer(buffer: u32, offset: u32, data: *const u8, len: u32);
78        /// Start the frame (clears color and depth). 1 = will be shown, 0 = discarded (the guest may skip draws).
79        #[link_name = "begin_frame"]
80        pub fn gfx_begin_frame(r: f32, g: f32, b: f32, a: f32) -> u32;
81        #[link_name = "set_pipeline"]
82        pub fn gfx_set_pipeline(pipeline: u32);
83        #[link_name = "set_bind_group"]
84        pub fn gfx_set_bind_group(index: u32, bind_group: u32);
85        /// set_bind_group with count dynamic offsets (multiples of 256), one per dynamic-offset entry of the layout, in binding order.
86        #[link_name = "set_bind_group_offsets"]
87        pub fn gfx_set_bind_group_offsets(index: u32, bind_group: u32, offsets: *const u32, count: u32);
88        /// Viewport in drawable pixels (clamped to the drawable), depth range 0-1. Reset to the whole drawable by begin_frame.
89        #[link_name = "set_viewport"]
90        pub fn gfx_set_viewport(x: f32, y: f32, width: f32, height: f32, min_depth: f32, max_depth: f32);
91        /// Scissor rectangle in drawable pixels (clamped to the drawable). Reset to the whole drawable by begin_frame.
92        #[link_name = "set_scissor_rect"]
93        pub fn gfx_set_scissor_rect(x: u32, y: u32, width: u32, height: u32);
94        #[link_name = "set_vertex_buffer"]
95        pub fn gfx_set_vertex_buffer(slot: u32, buffer: u32, offset: u32);
96        /// format: GASM_INDEX_U16 or GASM_INDEX_U32.
97        #[link_name = "set_index_buffer"]
98        pub fn gfx_set_index_buffer(buffer: u32, format: u32, offset: u32);
99        #[link_name = "draw"]
100        pub fn gfx_draw(vertex_count: u32, instance_count: u32, first_vertex: u32, first_instance: u32);
101        #[link_name = "draw_indexed"]
102        pub fn gfx_draw_indexed(index_count: u32, instance_count: u32, first_index: u32, base_vertex: i32, first_instance: u32);
103        /// Submit and present.
104        #[link_name = "end_frame"]
105        pub fn gfx_end_frame();
106    }
107
108    #[link(wasm_import_module = "gasm:net")]
109    unsafe extern "C" {
110        /// Open a ws:// or wss:// URL. Handle > 0, or -1 if denied/invalid.
111        #[link_name = "open"]
112        pub fn net_open(url: *const u8, url_len: u32) -> i32;
113        /// GASM_NET_CONNECTING / OPEN / CLOSED / ERROR.
114        #[link_name = "state"]
115        pub fn net_state(conn: i32) -> u32;
116        /// Send one message (len > 0). 0, or -1 if not open.
117        #[link_name = "send"]
118        pub fn net_send(conn: i32, data: *const u8, len: u32) -> i32;
119        /// Next message's length (copied only if <= cap, else it stays queued), 0 if none, -1 if closed and drained.
120        #[link_name = "recv"]
121        pub fn net_recv(conn: i32, dst: *mut u8, cap: u32) -> i32;
122        #[link_name = "close"]
123        pub fn net_close(conn: i32);
124    }
125
126    #[link(wasm_import_module = "gasm:storage")]
127    unsafe extern "C" {
128        /// Value length, or -1 if missing. Copied only if length <= cap.
129        #[link_name = "get"]
130        pub fn storage_get(key: *const u8, key_len: u32, dst: *mut u8, cap: u32) -> i32;
131        /// 0, or -1 on invalid key, too large, quota exceeded or I/O error.
132        #[link_name = "set"]
133        pub fn storage_set(key: *const u8, key_len: u32, data: *const u8, len: u32) -> i32;
134        /// 0 if deleted, -1 if it did not exist.
135        #[link_name = "delete"]
136        pub fn storage_delete(key: *const u8, key_len: u32) -> i32;
137    }
138
139    #[link(wasm_import_module = "wasi_snapshot_preview1")]
140    unsafe extern "C" {
141        /// End the game; 0 is a normal exit.
142        pub fn proc_exit(code: i32) -> !;
143    }
144}
145
146#[cfg(target_arch = "wasm32")]
147pub use imports::*;
148
149#[cfg(not(target_arch = "wasm32"))]
150pub use crate::native::abi::*;