Skip to main content

interstice_abi/host_calls/gpu/
mod.rs

1use serde::{Deserialize, Serialize};
2
3pub type GpuId = u32;
4
5#[derive(Serialize, Deserialize, Debug)]
6pub enum GpuCall {
7    // ----- Info -----
8    GetSurfaceFormat,
9    GetLimits,
10
11    // ----- Resources -----
12    CreateBuffer(CreateBuffer),
13    DestroyBuffer {
14        id: GpuId,
15    },
16    WriteBuffer(WriteBuffer),
17
18    CreateTexture(CreateTexture),
19    DestroyTexture {
20        id: GpuId,
21    },
22    WriteTexture(WriteTexture),
23    CreateTextureView(CreateTextureView),
24
25    CreateShaderModule(CreateShaderModule),
26
27    CreateBindGroupLayout(CreateBindGroupLayout),
28    CreateBindGroup(CreateBindGroup),
29    CreatePipelineLayout(CreatePipelineLayout),
30
31    CreateRenderPipeline(CreateRenderPipeline),
32    CreateComputePipeline(CreateComputePipeline),
33
34    // ----- Command Encoding -----
35    CreateCommandEncoder,
36    Submit {
37        encoder: GpuId,
38    },
39
40    // Render pass
41    BeginRenderPass(BeginRenderPass),
42    EndRenderPass {
43        pass: GpuId,
44    },
45
46    SetRenderPipeline {
47        pass: GpuId,
48        pipeline: GpuId,
49    },
50    SetBindGroup {
51        pass: GpuId,
52        index: u32,
53        bind_group: GpuId,
54    },
55
56    SetVertexBuffer(SetVertexBuffer),
57    SetIndexBuffer(SetIndexBuffer),
58
59    Draw(Draw),
60    DrawIndexed(DrawIndexed),
61
62    // Compute pass
63    BeginComputePass {
64        encoder: GpuId,
65    },
66    EndComputePass {
67        pass: GpuId,
68    },
69    SetComputePipeline {
70        pass: GpuId,
71        pipeline: GpuId,
72    },
73    Dispatch {
74        pass: GpuId,
75        x: u32,
76        y: u32,
77        z: u32,
78    },
79
80    // Copies
81    CopyBufferToBuffer(CopyBufferToBuffer),
82    CopyBufferToTexture(CopyBufferToTexture),
83    CopyTextureToBuffer(CopyTextureToBuffer),
84
85    // Surface
86    GetCurrentSurfaceTexture,
87    Present,
88    BeginFrame,
89}
90
91#[derive(Serialize, Deserialize, Debug)]
92pub struct CreateBuffer {
93    pub size: u64,
94    pub usage: BufferUsage, // your own bitflags enum
95    pub mapped_at_creation: bool,
96}
97
98#[derive(Serialize, Deserialize, Debug)]
99pub struct WriteBuffer {
100    pub buffer: GpuId,
101    pub offset: u64,
102    pub data: Vec<u8>,
103}
104
105#[derive(Serialize, Deserialize, Debug)]
106pub struct CreateTexture {
107    pub width: u32,
108    pub height: u32,
109    pub depth: u32,
110    pub mip_levels: u32,
111    pub sample_count: u32,
112    pub dimension: TextureDimension,
113    pub format: TextureFormat,
114    pub usage: TextureUsage,
115}
116
117#[derive(Serialize, Deserialize, Debug)]
118pub struct WriteTexture {
119    pub texture: GpuId,
120    pub data: Vec<u8>,
121    pub bytes_per_row: u32,
122    pub rows_per_image: u32,
123    pub width: u32,
124    pub height: u32,
125    pub depth: u32,
126}
127
128#[derive(Serialize, Deserialize, Debug)]
129pub struct CreateTextureView {
130    pub texture: GpuId,
131    pub format: Option<TextureFormat>,
132    pub dimension: Option<TextureViewDimension>,
133    pub base_mip_level: u32,
134    pub mip_level_count: Option<u32>,
135    pub base_array_layer: u32,
136    pub array_layer_count: Option<u32>,
137}
138
139#[derive(Serialize, Deserialize, Debug)]
140pub struct CreateShaderModule {
141    pub wgsl_source: String,
142}
143
144#[derive(Serialize, Deserialize, Debug)]
145pub struct CreateBindGroupLayout {
146    pub entries: Vec<BindGroupLayoutEntry>,
147}
148
149#[derive(Serialize, Deserialize, Debug)]
150pub struct CreateBindGroup {
151    pub layout: GpuId,
152    pub entries: Vec<BindGroupEntry>,
153}
154
155#[derive(Serialize, Deserialize, Debug)]
156pub struct CreatePipelineLayout {
157    pub bind_group_layouts: Vec<GpuId>,
158}
159
160#[derive(Serialize, Deserialize, Debug)]
161pub struct CreateRenderPipeline {
162    pub label: Option<String>,
163    pub layout: GpuId,
164    pub vertex: VertexState,
165    pub fragment: Option<FragmentState>,
166    pub primitive: PrimitiveState,
167    pub depth_stencil: Option<DepthStencilState>,
168    pub multisample: MultisampleState,
169    pub multiview: Option<u32>,
170}
171
172#[derive(Serialize, Deserialize, Debug)]
173pub struct VertexState {
174    pub module: GpuId,
175    pub entry_point: String,
176    pub buffers: Vec<VertexBufferLayout>,
177}
178
179#[derive(Serialize, Deserialize, Debug)]
180pub struct FragmentState {
181    pub module: GpuId,
182    pub entry_point: String,
183    pub targets: Vec<ColorTargetState>,
184}
185
186#[derive(Serialize, Deserialize, Debug)]
187pub struct BeginRenderPass {
188    pub encoder: GpuId,
189    pub color_attachments: Vec<RenderPassColorAttachment>,
190    pub depth_stencil: Option<RenderPassDepthStencilAttachment>,
191}
192
193#[derive(Serialize, Deserialize, Debug)]
194pub struct RenderPassColorAttachment {
195    pub view: GpuId,
196    pub resolve_target: Option<GpuId>,
197    pub load: LoadOp,
198    pub store: StoreOp,
199    pub clear_color: [f32; 4],
200}
201
202#[derive(Serialize, Deserialize, Debug)]
203pub struct SetVertexBuffer {
204    pub pass: GpuId,
205    pub slot: u32,
206    pub buffer: GpuId,
207    pub offset: u64,
208    pub size: Option<u64>,
209}
210
211#[derive(Serialize, Deserialize, Debug)]
212pub struct SetIndexBuffer {
213    pub pass: GpuId,
214    pub buffer: GpuId,
215    pub index_format: IndexFormat,
216    pub offset: u64,
217    pub size: Option<u64>,
218}
219
220#[derive(Serialize, Deserialize, Debug)]
221pub struct Draw {
222    pub pass: GpuId,
223    pub vertices: u32,
224    pub instances: u32,
225    pub first_vertex: u32,
226    pub first_instance: u32,
227}
228
229#[derive(Serialize, Deserialize, Debug)]
230pub struct DrawIndexed {
231    pub pass: GpuId,
232    pub indices: u32,
233    pub instances: u32,
234    pub first_index: u32,
235    pub base_vertex: i32,
236    pub first_instance: u32,
237}
238
239#[derive(Serialize, Deserialize, Debug)]
240pub struct CopyBufferToBuffer {
241    pub encoder: GpuId,
242    pub src: GpuId,
243    pub src_offset: u64,
244    pub dst: GpuId,
245    pub dst_offset: u64,
246    pub size: u64,
247}
248
249#[derive(Serialize, Deserialize, Debug)]
250pub struct CreateComputePipeline {
251    pub layout: GpuId,
252    pub module: GpuId,
253    pub entry_point: String,
254}
255
256#[derive(Serialize, Deserialize, Debug)]
257pub struct CopyBufferToTexture {
258    pub encoder: GpuId,
259    pub src_buffer: GpuId,
260    pub src_offset: u64,
261    pub bytes_per_row: u32,
262    pub rows_per_image: u32,
263
264    pub dst_texture: GpuId,
265    pub mip_level: u32,
266    pub origin: [u32; 3],
267
268    pub extent: [u32; 3],
269}
270
271#[derive(Serialize, Deserialize, Debug)]
272pub struct CopyTextureToBuffer {
273    pub encoder: GpuId,
274    pub src_texture: GpuId,
275    pub mip_level: u32,
276    pub origin: [u32; 3],
277
278    pub dst_buffer: GpuId,
279    pub dst_offset: u64,
280    pub bytes_per_row: u32,
281    pub rows_per_image: u32,
282
283    pub extent: [u32; 3],
284}
285
286bitflags::bitflags! {
287    #[derive(Serialize, Deserialize, Debug)]
288    pub struct BufferUsage: u32 {
289        const MAP_READ      = 1 << 0;
290        const MAP_WRITE     = 1 << 1;
291        const COPY_SRC      = 1 << 2;
292        const COPY_DST      = 1 << 3;
293        const INDEX         = 1 << 4;
294        const VERTEX        = 1 << 5;
295        const UNIFORM       = 1 << 6;
296        const STORAGE       = 1 << 7;
297        const INDIRECT      = 1 << 8;
298    }
299}
300
301bitflags::bitflags! {
302    #[derive(Serialize, Deserialize, Debug)]
303    pub struct TextureUsage: u32 {
304        const COPY_SRC          = 1 << 0;
305        const COPY_DST          = 1 << 1;
306        const TEXTURE_BINDING   = 1 << 2;
307        const STORAGE_BINDING   = 1 << 3;
308        const RENDER_ATTACHMENT = 1 << 4;
309    }
310}
311
312#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
313pub enum TextureDimension {
314    D1,
315    D2,
316    D3,
317}
318
319#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
320pub enum TextureViewDimension {
321    D1,
322    D2,
323    D2Array,
324    Cube,
325    CubeArray,
326    D3,
327}
328
329#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
330pub enum TextureFormat {
331    Rgba8Unorm,
332    Rgba8UnormSrgb,
333    Bgra8Unorm,
334    Bgra8UnormSrgb,
335    Depth24Plus,
336    Depth32Float,
337}
338
339#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
340pub enum IndexFormat {
341    Uint16,
342    Uint32,
343}
344
345#[derive(Serialize, Deserialize, Debug)]
346pub struct BindGroupLayoutEntry {
347    pub binding: u32,
348    pub visibility: ShaderStage,
349    pub ty: BindingType,
350}
351
352bitflags::bitflags! {
353    #[derive(Serialize, Deserialize, Debug)]
354    pub struct ShaderStage: u32 {
355        const VERTEX   = 1 << 0;
356        const FRAGMENT = 1 << 1;
357        const COMPUTE  = 1 << 2;
358    }
359}
360
361#[derive(Serialize, Deserialize, Debug)]
362pub enum BindingType {
363    UniformBuffer,
364    StorageBuffer {
365        read_only: bool,
366    },
367    Sampler {
368        comparison: bool,
369    },
370    Texture {
371        sample_type: TextureSampleType,
372        view_dimension: TextureViewDimension,
373        multisampled: bool,
374    },
375    StorageTexture {
376        format: TextureFormat,
377    },
378}
379
380#[derive(Serialize, Deserialize, Debug)]
381pub enum TextureSampleType {
382    Float { filterable: bool },
383    Depth,
384    Sint,
385    Uint,
386}
387
388#[derive(Serialize, Deserialize, Debug)]
389pub struct BindGroupEntry {
390    pub binding: u32,
391    pub resource: BindingResource,
392}
393
394#[derive(Serialize, Deserialize, Debug)]
395pub enum BindingResource {
396    Buffer {
397        buffer: GpuId,
398        offset: u64,
399        size: Option<u64>,
400    },
401    TextureView(GpuId),
402    Sampler(GpuId),
403}
404
405#[derive(Serialize, Deserialize, Debug, Clone)]
406pub struct PrimitiveState {
407    pub topology: PrimitiveTopology,
408    pub cull_mode: Option<CullMode>,
409    pub front_face: FrontFace,
410}
411
412#[derive(Serialize, Deserialize, Debug, Clone)]
413pub enum PrimitiveTopology {
414    TriangleList,
415    TriangleStrip,
416    LineList,
417}
418
419#[derive(Serialize, Deserialize, Debug, Clone)]
420pub enum CullMode {
421    Front,
422    Back,
423}
424
425#[derive(Serialize, Deserialize, Debug, Clone)]
426pub enum FrontFace {
427    Ccw,
428    Cw,
429}
430
431#[derive(Serialize, Deserialize, Debug)]
432pub struct DepthStencilState {
433    pub format: TextureFormat,
434    pub depth_write_enabled: bool,
435    pub depth_compare: CompareFunction,
436    pub stencil: StencilState,
437    pub bias: DepthBiasState,
438}
439
440#[derive(Serialize, Deserialize, Debug)]
441pub struct StencilState {
442    /// Front face mode.
443    pub front: StencilFaceState,
444    /// Back face mode.
445    pub back: StencilFaceState,
446    /// Stencil values are AND'd with this mask when reading and writing from the stencil buffer. Only low 8 bits are used.
447    pub read_mask: u32,
448    /// Stencil values are AND'd with this mask when writing to the stencil buffer. Only low 8 bits are used.
449    pub write_mask: u32,
450}
451
452#[derive(Serialize, Deserialize, Debug)]
453pub struct StencilFaceState {
454    /// Comparison function that determines if the fail_op or pass_op is used on the stencil buffer.
455    pub compare: CompareFunction,
456    /// Operation that is performed when stencil test fails.
457    pub fail_op: StencilOperation,
458    /// Operation that is performed when depth test fails but stencil test succeeds.
459    pub depth_fail_op: StencilOperation,
460    /// Operation that is performed when stencil test success.
461    pub pass_op: StencilOperation,
462}
463
464#[derive(Serialize, Deserialize, Debug)]
465pub enum StencilOperation {
466    /// Keep stencil value unchanged.
467    Keep = 0,
468    /// Set stencil value to zero.
469    Zero = 1,
470    /// Replace stencil value with value provided in most recent call to
471    /// [`RenderPass::set_stencil_reference`][RPssr].
472    ///
473    Replace = 2,
474    /// Bitwise inverts stencil value.
475    Invert = 3,
476    /// Increments stencil value by one, clamping on overflow.
477    IncrementClamp = 4,
478    /// Decrements stencil value by one, clamping on underflow.
479    DecrementClamp = 5,
480    /// Increments stencil value by one, wrapping on overflow.
481    IncrementWrap = 6,
482    /// Decrements stencil value by one, wrapping on underflow.
483    DecrementWrap = 7,
484}
485
486#[derive(Serialize, Deserialize, Debug)]
487pub struct DepthBiasState {
488    /// Constant depth biasing factor, in basic units of the depth format.
489    pub constant: i32,
490    /// Slope depth biasing factor.
491    pub slope_scale: f32,
492    /// Depth bias clamp value (absolute).
493    pub clamp: f32,
494}
495
496#[derive(Serialize, Deserialize, Debug)]
497pub enum CompareFunction {
498    Less,
499    LessEqual,
500    Greater,
501    Always,
502}
503
504#[derive(Serialize, Deserialize, Debug)]
505pub struct MultisampleState {
506    pub count: u32,
507    pub mask: u64,
508    pub alpha_to_coverage_enabled: bool,
509}
510
511#[derive(Serialize, Deserialize, Debug)]
512pub struct VertexBufferLayout {
513    pub array_stride: u64,
514    pub step_mode: VertexStepMode,
515    pub attributes: Vec<VertexAttribute>,
516}
517
518#[derive(Serialize, Deserialize, Debug)]
519pub enum VertexStepMode {
520    Vertex,
521    Instance,
522}
523
524#[derive(Serialize, Deserialize, Debug)]
525pub struct VertexAttribute {
526    pub format: VertexFormat,
527    pub offset: u64,
528    pub shader_location: u32,
529}
530
531#[derive(Serialize, Deserialize, Debug)]
532pub struct ColorTargetState {
533    pub format: TextureFormat,
534    pub blend: Option<BlendState>,
535    pub write_mask: ColorWrites,
536}
537
538#[derive(Serialize, Deserialize, Debug)]
539pub struct RenderPassDepthStencilAttachment {
540    pub view: GpuId,
541    pub depth_load: LoadOp,
542    pub depth_store: StoreOp,
543    pub depth_clear: f32,
544}
545
546#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
547pub enum LoadOp {
548    Load,
549    Clear,
550}
551
552#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
553pub enum StoreOp {
554    Store,
555    Discard,
556}
557
558#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
559pub enum VertexFormat {
560    Float32,
561    Float32x2,
562    Float32x3,
563    Float32x4,
564    Uint32,
565    Uint32x2,
566    Uint32x3,
567    Uint32x4,
568}
569
570#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
571pub struct BlendState {
572    pub color: BlendComponent,
573    pub alpha: BlendComponent,
574}
575
576#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
577pub struct BlendComponent {
578    pub src_factor: BlendFactor,
579    pub dst_factor: BlendFactor,
580    pub operation: BlendOperation,
581}
582
583#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
584pub enum BlendFactor {
585    Zero,
586    One,
587    Src,
588    OneMinusSrc,
589    SrcAlpha,
590    OneMinusSrcAlpha,
591    DstAlpha,
592    OneMinusDstAlpha,
593}
594
595#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
596pub enum BlendOperation {
597    Add,
598    Subtract,
599    ReverseSubtract,
600}
601
602bitflags::bitflags! {
603    #[derive(Serialize, Deserialize, Debug)]
604    pub struct ColorWrites: u32 {
605        const RED   = 1 << 0;
606        const GREEN = 1 << 1;
607        const BLUE  = 1 << 2;
608        const ALPHA = 1 << 3;
609        const ALL   = Self::RED.bits() | Self::GREEN.bits() | Self::BLUE.bits() | Self::ALPHA.bits();
610    }
611}