interstice-abi 0.5.0

WASM ABI types and host-call interfaces for the Interstice runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
use serde::{Deserialize, Serialize};

pub type GpuId = u32;

#[derive(Serialize, Deserialize, Debug)]
pub enum GpuCall {
    // ----- Info -----
    GetSurfaceFormat,
    GetLimits,

    // ----- Resources -----
    CreateBuffer(CreateBuffer),
    DestroyBuffer {
        id: GpuId,
    },
    WriteBuffer(WriteBuffer),

    CreateTexture(CreateTexture),
    DestroyTexture {
        id: GpuId,
    },
    DestroyTextureView {
        id: GpuId,
    },
    WriteTexture(WriteTexture),
    CreateTextureView(CreateTextureView),

    CreateShaderModule(CreateShaderModule),
    DestroyShaderModule {
        id: GpuId,
    },

    CreateBindGroupLayout(CreateBindGroupLayout),
    DestroyBindGroupLayout {
        id: GpuId,
    },
    CreateBindGroup(CreateBindGroup),
    DestroyBindGroup {
        id: GpuId,
    },
    CreatePipelineLayout(CreatePipelineLayout),
    DestroyPipelineLayout {
        id: GpuId,
    },

    CreateRenderPipeline(CreateRenderPipeline),
    DestroyRenderPipeline {
        id: GpuId,
    },
    CreateComputePipeline(CreateComputePipeline),
    DestroyComputePipeline {
        id: GpuId,
    },

    // ----- Command Encoding -----
    CreateCommandEncoder,
    Submit {
        encoder: GpuId,
    },

    // Render pass
    BeginRenderPass(BeginRenderPass),
    EndRenderPass {
        pass: GpuId,
    },

    SetRenderPipeline {
        pass: GpuId,
        pipeline: GpuId,
    },
    SetBindGroup {
        pass: GpuId,
        index: u32,
        bind_group: GpuId,
    },

    SetVertexBuffer(SetVertexBuffer),
    SetIndexBuffer(SetIndexBuffer),

    Draw(Draw),
    DrawIndexed(DrawIndexed),

    // Compute pass
    BeginComputePass {
        encoder: GpuId,
    },
    EndComputePass {
        pass: GpuId,
    },
    SetComputePipeline {
        pass: GpuId,
        pipeline: GpuId,
    },
    Dispatch {
        pass: GpuId,
        x: u32,
        y: u32,
        z: u32,
    },

    // Copies
    CopyBufferToBuffer(CopyBufferToBuffer),
    CopyBufferToTexture(CopyBufferToTexture),
    CopyTextureToBuffer(CopyTextureToBuffer),

    // Surface
    GetCurrentSurfaceTexture,
    GetSurfaceSize,
    Present,
    BeginFrame,
    RequestRedraw,
}

#[derive(Serialize, Deserialize, Debug)]
pub enum GpuResponse {
    None,
    I64(i64),
    TextureFormat(TextureFormat),
    Extent2d { width: u32, height: u32 },
    Err(String),
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CreateBuffer {
    pub size: u64,
    pub usage: BufferUsage, // your own bitflags enum
    pub mapped_at_creation: bool,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct WriteBuffer {
    pub buffer: GpuId,
    pub offset: u64,
    pub data: Vec<u8>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CreateTexture {
    pub width: u32,
    pub height: u32,
    pub depth: u32,
    pub mip_levels: u32,
    pub sample_count: u32,
    pub dimension: TextureDimension,
    pub format: TextureFormat,
    pub usage: TextureUsage,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct WriteTexture {
    pub texture: GpuId,
    pub data: Vec<u8>,
    pub bytes_per_row: u32,
    pub rows_per_image: u32,
    pub width: u32,
    pub height: u32,
    pub depth: u32,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CreateTextureView {
    pub texture: GpuId,
    pub format: Option<TextureFormat>,
    pub dimension: Option<TextureViewDimension>,
    pub base_mip_level: u32,
    pub mip_level_count: Option<u32>,
    pub base_array_layer: u32,
    pub array_layer_count: Option<u32>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CreateShaderModule {
    pub wgsl_source: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CreateBindGroupLayout {
    pub entries: Vec<BindGroupLayoutEntry>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CreateBindGroup {
    pub layout: GpuId,
    pub entries: Vec<BindGroupEntry>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CreatePipelineLayout {
    pub bind_group_layouts: Vec<GpuId>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CreateRenderPipeline {
    pub label: Option<String>,
    pub layout: GpuId,
    pub vertex: VertexState,
    pub fragment: Option<FragmentState>,
    pub primitive: PrimitiveState,
    pub depth_stencil: Option<DepthStencilState>,
    pub multisample: MultisampleState,
    pub multiview: Option<u32>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct VertexState {
    pub module: GpuId,
    pub entry_point: String,
    pub buffers: Vec<VertexBufferLayout>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct FragmentState {
    pub module: GpuId,
    pub entry_point: String,
    pub targets: Vec<ColorTargetState>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct BeginRenderPass {
    pub encoder: GpuId,
    pub color_attachments: Vec<RenderPassColorAttachment>,
    pub depth_stencil: Option<RenderPassDepthStencilAttachment>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct RenderPassColorAttachment {
    pub view: GpuId,
    pub resolve_target: Option<GpuId>,
    pub load: LoadOp,
    pub store: StoreOp,
    pub clear_color: [f32; 4],
}

#[derive(Serialize, Deserialize, Debug)]
pub struct SetVertexBuffer {
    pub pass: GpuId,
    pub slot: u32,
    pub buffer: GpuId,
    pub offset: u64,
    pub size: Option<u64>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct SetIndexBuffer {
    pub pass: GpuId,
    pub buffer: GpuId,
    pub index_format: IndexFormat,
    pub offset: u64,
    pub size: Option<u64>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Draw {
    pub pass: GpuId,
    pub vertices: u32,
    pub instances: u32,
    pub first_vertex: u32,
    pub first_instance: u32,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct DrawIndexed {
    pub pass: GpuId,
    pub indices: u32,
    pub instances: u32,
    pub first_index: u32,
    pub base_vertex: i32,
    pub first_instance: u32,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CopyBufferToBuffer {
    pub encoder: GpuId,
    pub src: GpuId,
    pub src_offset: u64,
    pub dst: GpuId,
    pub dst_offset: u64,
    pub size: u64,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CreateComputePipeline {
    pub layout: GpuId,
    pub module: GpuId,
    pub entry_point: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CopyBufferToTexture {
    pub encoder: GpuId,
    pub src_buffer: GpuId,
    pub src_offset: u64,
    pub bytes_per_row: u32,
    pub rows_per_image: u32,

    pub dst_texture: GpuId,
    pub mip_level: u32,
    pub origin: [u32; 3],

    pub extent: [u32; 3],
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CopyTextureToBuffer {
    pub encoder: GpuId,
    pub src_texture: GpuId,
    pub mip_level: u32,
    pub origin: [u32; 3],

    pub dst_buffer: GpuId,
    pub dst_offset: u64,
    pub bytes_per_row: u32,
    pub rows_per_image: u32,

    pub extent: [u32; 3],
}

bitflags::bitflags! {
    #[derive(Serialize, Deserialize, Debug)]
    pub struct BufferUsage: u32 {
        const MAP_READ      = 1 << 0;
        const MAP_WRITE     = 1 << 1;
        const COPY_SRC      = 1 << 2;
        const COPY_DST      = 1 << 3;
        const INDEX         = 1 << 4;
        const VERTEX        = 1 << 5;
        const UNIFORM       = 1 << 6;
        const STORAGE       = 1 << 7;
        const INDIRECT      = 1 << 8;
    }
}

bitflags::bitflags! {
    #[derive(Serialize, Deserialize, Debug)]
    pub struct TextureUsage: u32 {
        const COPY_SRC          = 1 << 0;
        const COPY_DST          = 1 << 1;
        const TEXTURE_BINDING   = 1 << 2;
        const STORAGE_BINDING   = 1 << 3;
        const RENDER_ATTACHMENT = 1 << 4;
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub enum TextureDimension {
    D1,
    D2,
    D3,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub enum TextureViewDimension {
    D1,
    D2,
    D2Array,
    Cube,
    CubeArray,
    D3,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub enum TextureFormat {
    Rgba8Unorm,
    Rgba8UnormSrgb,
    Bgra8Unorm,
    Bgra8UnormSrgb,
    Depth24Plus,
    Depth32Float,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub enum IndexFormat {
    Uint16,
    Uint32,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct BindGroupLayoutEntry {
    pub binding: u32,
    pub visibility: ShaderStage,
    pub ty: BindingType,
}

bitflags::bitflags! {
    #[derive(Serialize, Deserialize, Debug)]
    pub struct ShaderStage: u32 {
        const VERTEX   = 1 << 0;
        const FRAGMENT = 1 << 1;
        const COMPUTE  = 1 << 2;
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub enum BindingType {
    UniformBuffer,
    StorageBuffer {
        read_only: bool,
    },
    Sampler {
        comparison: bool,
    },
    Texture {
        sample_type: TextureSampleType,
        view_dimension: TextureViewDimension,
        multisampled: bool,
    },
    StorageTexture {
        format: TextureFormat,
    },
}

#[derive(Serialize, Deserialize, Debug)]
pub enum TextureSampleType {
    Float { filterable: bool },
    Depth,
    Sint,
    Uint,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct BindGroupEntry {
    pub binding: u32,
    pub resource: BindingResource,
}

#[derive(Serialize, Deserialize, Debug)]
pub enum BindingResource {
    Buffer {
        buffer: GpuId,
        offset: u64,
        size: Option<u64>,
    },
    TextureView(GpuId),
    Sampler(GpuId),
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PrimitiveState {
    pub topology: PrimitiveTopology,
    pub cull_mode: Option<CullMode>,
    pub front_face: FrontFace,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum PrimitiveTopology {
    TriangleList,
    TriangleStrip,
    LineList,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum CullMode {
    Front,
    Back,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum FrontFace {
    Ccw,
    Cw,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct DepthStencilState {
    pub format: TextureFormat,
    pub depth_write_enabled: bool,
    pub depth_compare: CompareFunction,
    pub stencil: StencilState,
    pub bias: DepthBiasState,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct StencilState {
    /// Front face mode.
    pub front: StencilFaceState,
    /// Back face mode.
    pub back: StencilFaceState,
    /// Stencil values are AND'd with this mask when reading and writing from the stencil buffer. Only low 8 bits are used.
    pub read_mask: u32,
    /// Stencil values are AND'd with this mask when writing to the stencil buffer. Only low 8 bits are used.
    pub write_mask: u32,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct StencilFaceState {
    /// Comparison function that determines if the fail_op or pass_op is used on the stencil buffer.
    pub compare: CompareFunction,
    /// Operation that is performed when stencil test fails.
    pub fail_op: StencilOperation,
    /// Operation that is performed when depth test fails but stencil test succeeds.
    pub depth_fail_op: StencilOperation,
    /// Operation that is performed when stencil test success.
    pub pass_op: StencilOperation,
}

#[derive(Serialize, Deserialize, Debug)]
pub enum StencilOperation {
    /// Keep stencil value unchanged.
    Keep = 0,
    /// Set stencil value to zero.
    Zero = 1,
    /// Replace stencil value with value provided in most recent call to
    /// [`RenderPass::set_stencil_reference`][RPssr].
    ///
    Replace = 2,
    /// Bitwise inverts stencil value.
    Invert = 3,
    /// Increments stencil value by one, clamping on overflow.
    IncrementClamp = 4,
    /// Decrements stencil value by one, clamping on underflow.
    DecrementClamp = 5,
    /// Increments stencil value by one, wrapping on overflow.
    IncrementWrap = 6,
    /// Decrements stencil value by one, wrapping on underflow.
    DecrementWrap = 7,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct DepthBiasState {
    /// Constant depth biasing factor, in basic units of the depth format.
    pub constant: i32,
    /// Slope depth biasing factor.
    pub slope_scale: f32,
    /// Depth bias clamp value (absolute).
    pub clamp: f32,
}

#[derive(Serialize, Deserialize, Debug)]
pub enum CompareFunction {
    Less,
    LessEqual,
    Greater,
    Always,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct MultisampleState {
    pub count: u32,
    pub mask: u64,
    pub alpha_to_coverage_enabled: bool,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct VertexBufferLayout {
    pub array_stride: u64,
    pub step_mode: VertexStepMode,
    pub attributes: Vec<VertexAttribute>,
}

#[derive(Serialize, Deserialize, Debug)]
pub enum VertexStepMode {
    Vertex,
    Instance,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct VertexAttribute {
    pub format: VertexFormat,
    pub offset: u64,
    pub shader_location: u32,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ColorTargetState {
    pub format: TextureFormat,
    pub blend: Option<BlendState>,
    pub write_mask: ColorWrites,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct RenderPassDepthStencilAttachment {
    pub view: GpuId,
    pub depth_load: LoadOp,
    pub depth_store: StoreOp,
    pub depth_clear: f32,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub enum LoadOp {
    Load,
    Clear,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub enum StoreOp {
    Store,
    Discard,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub enum VertexFormat {
    Float32,
    Float32x2,
    Float32x3,
    Float32x4,
    Uint32,
    Uint32x2,
    Uint32x3,
    Uint32x4,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct BlendState {
    pub color: BlendComponent,
    pub alpha: BlendComponent,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct BlendComponent {
    pub src_factor: BlendFactor,
    pub dst_factor: BlendFactor,
    pub operation: BlendOperation,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub enum BlendFactor {
    Zero,
    One,
    Src,
    OneMinusSrc,
    SrcAlpha,
    OneMinusSrcAlpha,
    DstAlpha,
    OneMinusDstAlpha,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub enum BlendOperation {
    Add,
    Subtract,
    ReverseSubtract,
}

bitflags::bitflags! {
    #[derive(Serialize, Deserialize, Debug)]
    pub struct ColorWrites: u32 {
        const RED   = 1 << 0;
        const GREEN = 1 << 1;
        const BLUE  = 1 << 2;
        const ALPHA = 1 << 3;
        const ALL   = Self::RED.bits() | Self::GREEN.bits() | Self::BLUE.bits() | Self::ALPHA.bits();
    }
}