summaryrefslogtreecommitdiff
path: root/bindgen/rust/examples/shaders.rs
blob: 09f13b4468edf9bedfe4ce76f78d4949eaab107c (plain)
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
use celeritas::ral::{
    ShaderBinding, ShaderBindingKind, ShaderData, ShaderDataLayout, ShaderVisibility,
};
use celeritas_sys::{
    GPU_EncodeBindShaderDataRaw, GPU_GetDefaultEncoder, Mat4, ShaderVisibility_VISIBILITY_COMPUTE,
    ShaderVisibility_VISIBILITY_FRAGMENT, ShaderVisibility_VISIBILITY_VERTEX,
};

#[repr(C)]
struct MVP {
    model: Mat4,
    view: Mat4,
    proj: Mat4,
}

fn shader_vis_all() -> u32 {
    ShaderVisibility_VISIBILITY_VERTEX
        | ShaderVisibility_VISIBILITY_FRAGMENT
        | ShaderVisibility_VISIBILITY_COMPUTE
}

impl ShaderData for MVP {
    fn layout() -> ShaderDataLayout {
        let mut bindings: heapless::Vec<ShaderBinding, 8> = heapless::Vec::new();
        let _ = bindings.push(ShaderBinding {
            label: "MVP".to_string(),
            kind: ShaderBindingKind::Bytes {
                size: std::mem::size_of::<MVP>(),
                data: None,
            },
            vis: ShaderVisibility::all(),
        });
        ShaderDataLayout { bindings }
    }

    fn bind(&self) {
        let mut layout = Self::layout();
        let b0 = &mut layout.bindings[0];
        b0.kind = ShaderBindingKind::Bytes {
            size: std::mem::size_of::<MVP>(),
            data: Some((self as *const MVP) as *mut u8),
        };

        unsafe {
            GPU_EncodeBindShaderDataRaw(GPU_GetDefaultEncoder(), 0, layout.into_ffi_type());
        }
    }
}

fn main() {}