Skip to main content

interstice_sdk_core/host_calls/
gpu.rs

1use crate::host_calls::{host_call, unpack};
2use interstice_abi::*;
3
4pub fn begin_frame() {
5    host_call(HostCall::Gpu(GpuCall::BeginFrame));
6}
7
8pub fn get_surface_format() -> TextureFormat {
9    let pack = host_call(HostCall::Gpu(GpuCall::GetSurfaceFormat));
10    let result: TextureFormat = unpack(pack);
11    return result;
12}
13
14pub fn get_current_surface_texture() -> GpuId {
15    host_call(HostCall::Gpu(GpuCall::GetCurrentSurfaceTexture)) as GpuId
16}
17
18pub fn present() {
19    host_call(HostCall::Gpu(GpuCall::Present));
20}
21
22pub fn create_buffer(size: u64, usage: BufferUsage, mapped_at_creation: bool) -> GpuId {
23    host_call(HostCall::Gpu(GpuCall::CreateBuffer(CreateBuffer {
24        size,
25        usage,
26        mapped_at_creation,
27    }))) as GpuId
28}
29
30pub fn write_buffer(buffer: GpuId, offset: u64, data: Vec<u8>) {
31    host_call(HostCall::Gpu(GpuCall::WriteBuffer(WriteBuffer {
32        buffer,
33        offset,
34        data,
35    })));
36}
37
38pub fn destroy_buffer(id: GpuId) {
39    host_call(HostCall::Gpu(GpuCall::DestroyBuffer { id }));
40}
41
42pub fn create_texture(desc: CreateTexture) -> GpuId {
43    host_call(HostCall::Gpu(GpuCall::CreateTexture(desc))) as GpuId
44}
45
46pub fn create_texture_view(desc: CreateTextureView) -> GpuId {
47    host_call(HostCall::Gpu(GpuCall::CreateTextureView(desc))) as GpuId
48}
49
50pub fn destroy_texture(id: GpuId) {
51    host_call(HostCall::Gpu(GpuCall::DestroyTexture { id }));
52}
53
54pub fn create_shader_module(wgsl_source: String) -> GpuId {
55    host_call(HostCall::Gpu(GpuCall::CreateShaderModule(
56        CreateShaderModule { wgsl_source },
57    ))) as GpuId
58}
59
60pub fn create_bind_group_layout(desc: CreateBindGroupLayout) -> GpuId {
61    host_call(HostCall::Gpu(GpuCall::CreateBindGroupLayout(desc))) as GpuId
62}
63
64pub fn create_bind_group(desc: CreateBindGroup) -> GpuId {
65    host_call(HostCall::Gpu(GpuCall::CreateBindGroup(desc))) as GpuId
66}
67
68pub fn create_pipeline_layout(desc: CreatePipelineLayout) -> GpuId {
69    host_call(HostCall::Gpu(GpuCall::CreatePipelineLayout(desc))) as GpuId
70}
71
72pub fn create_render_pipeline(desc: CreateRenderPipeline) -> GpuId {
73    host_call(HostCall::Gpu(GpuCall::CreateRenderPipeline(desc))) as GpuId
74}
75
76pub fn create_compute_pipeline(desc: CreateComputePipeline) -> GpuId {
77    host_call(HostCall::Gpu(GpuCall::CreateComputePipeline(desc))) as GpuId
78}
79
80pub fn create_command_encoder() -> GpuId {
81    host_call(HostCall::Gpu(GpuCall::CreateCommandEncoder)) as GpuId
82}
83
84pub fn submit(encoder: GpuId) {
85    host_call(HostCall::Gpu(GpuCall::Submit { encoder }));
86}
87
88pub fn begin_render_pass(desc: BeginRenderPass) -> GpuId {
89    host_call(HostCall::Gpu(GpuCall::BeginRenderPass(desc))) as GpuId
90}
91
92pub fn end_render_pass(pass: GpuId) {
93    host_call(HostCall::Gpu(GpuCall::EndRenderPass { pass }));
94}
95
96pub fn set_render_pipeline(pass: GpuId, pipeline: GpuId) {
97    host_call(HostCall::Gpu(GpuCall::SetRenderPipeline { pass, pipeline }));
98}
99
100pub fn set_bind_group(pass: GpuId, index: u32, bind_group: GpuId) {
101    host_call(HostCall::Gpu(GpuCall::SetBindGroup {
102        pass,
103        index,
104        bind_group,
105    }));
106}
107
108pub fn set_vertex_buffer(pass: GpuId, buffer: GpuId, offset: u64, slot: u32, size: Option<u64>) {
109    host_call(HostCall::Gpu(GpuCall::SetVertexBuffer(SetVertexBuffer {
110        pass,
111        buffer,
112        offset,
113        slot,
114        size,
115    })));
116}
117
118pub fn set_index_buffer(
119    pass: GpuId,
120    buffer: GpuId,
121    offset: u64,
122    format: IndexFormat,
123    size: Option<u64>,
124) {
125    host_call(HostCall::Gpu(GpuCall::SetIndexBuffer(SetIndexBuffer {
126        pass,
127        buffer,
128        offset,
129        index_format: format,
130        size,
131    })));
132}
133
134pub fn draw(pass: GpuId, vertices: u32, instances: u32) {
135    host_call(HostCall::Gpu(GpuCall::Draw(Draw {
136        pass,
137        vertices,
138        instances,
139        first_vertex: 0,
140        first_instance: 0,
141    })));
142}
143
144pub fn draw_indexed(pass: GpuId, indices: u32, instances: u32) {
145    host_call(HostCall::Gpu(GpuCall::DrawIndexed(DrawIndexed {
146        pass,
147        indices,
148        instances,
149        first_index: 0,
150        base_vertex: 0,
151        first_instance: 0,
152    })));
153}
154
155// compute pass
156
157pub fn begin_compute_pass(encoder: GpuId) -> GpuId {
158    host_call(HostCall::Gpu(GpuCall::BeginComputePass { encoder })) as GpuId
159}
160
161pub fn end_compute_pass(pass: GpuId) {
162    host_call(HostCall::Gpu(GpuCall::EndComputePass { pass }));
163}
164
165pub fn set_compute_pipeline(pass: GpuId, pipeline: GpuId) {
166    host_call(HostCall::Gpu(GpuCall::SetComputePipeline {
167        pass,
168        pipeline,
169    }));
170}
171
172pub fn dispatch(pass: GpuId, x: u32, y: u32, z: u32) {
173    host_call(HostCall::Gpu(GpuCall::Dispatch { pass, x, y, z }));
174}
175
176// Copies
177
178pub fn copy_buffer_to_buffer(cmd: CopyBufferToBuffer) {
179    host_call(HostCall::Gpu(GpuCall::CopyBufferToBuffer(cmd)));
180}
181
182pub fn copy_buffer_to_texture(cmd: CopyBufferToTexture) {
183    host_call(HostCall::Gpu(GpuCall::CopyBufferToTexture(cmd)));
184}
185
186pub fn copy_texture_to_buffer(cmd: CopyTextureToBuffer) {
187    host_call(HostCall::Gpu(GpuCall::CopyTextureToBuffer(cmd)));
188}
189
190pub struct Buffer(pub GpuId);
191pub struct RenderPipeline(pub GpuId);
192pub struct BindGroup(pub GpuId);
193pub struct Texture(pub GpuId);
194
195pub struct Gpu;
196
197impl Gpu {
198    pub fn begin_frame(&self) {
199        begin_frame();
200    }
201
202    pub fn get_surface_format(&self) -> TextureFormat {
203        get_surface_format()
204    }
205
206    pub fn get_current_surface_texture(&self) -> GpuId {
207        get_current_surface_texture()
208    }
209
210    pub fn present(&self) {
211        present();
212    }
213
214    pub fn create_buffer(&self, size: u64, usage: BufferUsage, mapped_at_creation: bool) -> GpuId {
215        create_buffer(size, usage, mapped_at_creation)
216    }
217
218    pub fn write_buffer(&self, buffer: GpuId, offset: u64, data: Vec<u8>) {
219        write_buffer(buffer, offset, data)
220    }
221
222    pub fn destroy_buffer(&self, id: GpuId) {
223        destroy_buffer(id)
224    }
225
226    pub fn create_texture(&self, desc: CreateTexture) -> GpuId {
227        create_texture(desc)
228    }
229
230    pub fn create_texture_view(&self, desc: CreateTextureView) -> GpuId {
231        create_texture_view(desc)
232    }
233
234    pub fn destroy_texture(&self, id: GpuId) {
235        destroy_texture(id)
236    }
237
238    pub fn create_shader_module(&self, wgsl_source: String) -> GpuId {
239        create_shader_module(wgsl_source)
240    }
241
242    pub fn create_bind_group_layout(&self, desc: CreateBindGroupLayout) -> GpuId {
243        create_bind_group_layout(desc)
244    }
245
246    pub fn create_bind_group(&self, desc: CreateBindGroup) -> GpuId {
247        create_bind_group(desc)
248    }
249
250    pub fn create_pipeline_layout(&self, desc: CreatePipelineLayout) -> GpuId {
251        create_pipeline_layout(desc)
252    }
253
254    pub fn create_render_pipeline(&self, desc: CreateRenderPipeline) -> GpuId {
255        host_call(HostCall::Gpu(GpuCall::CreateRenderPipeline(desc))) as GpuId
256    }
257
258    pub fn create_compute_pipeline(&self, desc: CreateComputePipeline) -> GpuId {
259        create_compute_pipeline(desc)
260    }
261
262    pub fn create_command_encoder(&self) -> GpuId {
263        create_command_encoder()
264    }
265
266    pub fn submit(&self, encoder: GpuId) {
267        submit(encoder)
268    }
269
270    pub fn begin_render_pass(&self, desc: BeginRenderPass) -> GpuId {
271        begin_render_pass(desc)
272    }
273
274    pub fn end_render_pass(&self, pass: GpuId) {
275        end_render_pass(pass)
276    }
277
278    pub fn set_render_pipeline(&self, pass: GpuId, pipeline: GpuId) {
279        set_render_pipeline(pass, pipeline)
280    }
281
282    pub fn set_bind_group(&self, pass: GpuId, index: u32, bind_group: GpuId) {
283        set_bind_group(pass, index, bind_group)
284    }
285
286    pub fn set_vertex_buffer(
287        &self,
288        pass: GpuId,
289        buffer: GpuId,
290        offset: u64,
291        slot: u32,
292        size: Option<u64>,
293    ) {
294        set_vertex_buffer(pass, buffer, offset, slot, size)
295    }
296
297    pub fn set_index_buffer(
298        &self,
299        pass: GpuId,
300        buffer: GpuId,
301        offset: u64,
302        format: IndexFormat,
303        size: Option<u64>,
304    ) {
305        set_index_buffer(pass, buffer, offset, format, size)
306    }
307
308    pub fn draw(&self, pass: GpuId, vertices: u32, instances: u32) {
309        draw(pass, vertices, instances)
310    }
311
312    pub fn draw_indexed(&self, pass: GpuId, indices: u32, instances: u32) {
313        draw_indexed(pass, indices, instances)
314    }
315
316    // compute pass
317
318    pub fn begin_compute_pass(&self, encoder: GpuId) -> GpuId {
319        begin_compute_pass(encoder)
320    }
321
322    pub fn end_compute_pass(&self, pass: GpuId) {
323        end_compute_pass(pass)
324    }
325
326    pub fn set_compute_pipeline(&self, pass: GpuId, pipeline: GpuId) {
327        set_compute_pipeline(pass, pipeline)
328    }
329
330    pub fn dispatch(&self, pass: GpuId, x: u32, y: u32, z: u32) {
331        dispatch(pass, x, y, z)
332    }
333
334    // Copies
335
336    pub fn copy_buffer_to_buffer(&self, cmd: CopyBufferToBuffer) {
337        copy_buffer_to_buffer(cmd)
338    }
339
340    pub fn copy_buffer_to_texture(&self, cmd: CopyBufferToTexture) {
341        copy_buffer_to_texture(cmd)
342    }
343
344    pub fn copy_texture_to_buffer(&self, cmd: CopyTextureToBuffer) {
345        copy_texture_to_buffer(cmd)
346    }
347}