summaryrefslogtreecommitdiff
path: root/bindgen/rust/src/ral.rs
blob: 413288521c7d33c1cc13be750b6044dd008a41b0 (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
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
//! Wrapper around the RAL code in celeritas-core

use std::{ffi::c_void, ptr::addr_of_mut};

use celeritas_sys::{
    BufferHandle, GPU_CmdEncoder, GPU_CmdEncoder_BeginRender, GPU_CmdEncoder_EndRender,
    GPU_EncodeBindShaderData, GPU_GetDefaultEncoder, GPU_GetDefaultRenderpass,
    GPU_GraphicsPipeline_Create, GraphicsPipelineDesc, ShaderVisibility_VISIBILITY_COMPUTE,
    ShaderVisibility_VISIBILITY_FRAGMENT, ShaderVisibility_VISIBILITY_VERTEX, TextureHandle,
    MAX_SHADER_DATA_LAYOUTS,
};
use thiserror::Error;

/// Holds a pointer to the raw `GPU_CmdEncoder`
pub struct FrameRenderEncoder(*mut GPU_CmdEncoder);

/// Holds a pointer to the raw `GPU_Renderpass`
pub struct RenderPass(*mut celeritas_sys::GPU_Renderpass);

/// Holds a pointer to the raw `GPU_Pipeline`
pub struct Pipeline(*mut celeritas_sys::GPU_Pipeline);

impl FrameRenderEncoder {
    pub fn new(renderpass: &RenderPass) -> Self {
        let enc = unsafe {
            let enc = GPU_GetDefaultEncoder();
            GPU_CmdEncoder_BeginRender(enc, renderpass.0);
            enc
        };
        FrameRenderEncoder(enc)
    }
}

impl Drop for FrameRenderEncoder {
    fn drop(&mut self) {
        unsafe {
            GPU_CmdEncoder_EndRender(self.0);
        }
    }
}

impl FrameRenderEncoder {
    pub fn set_vertex_buffer(&self, buf: BufferHandle) {
        // TODO: Get buffer ptr from handle
        // TODO: assert that buffer type is vertex
        todo!()
    }
    pub fn set_index_buffer(&self, buf: BufferHandle) {
        // TODO: Get buffer ptr from handle
        // TODO: assert that buffer type is index
        todo!()
    }
    pub fn bind<S: ShaderData>(&mut self, data: &S) {
        let sd = celeritas_sys::ShaderData {
            get_layout: todo!(),
            data: addr_of_mut!(data) as *mut c_void,
        };
        unsafe { GPU_EncodeBindShaderData(self.0, 0, todo!()) }
    }
}

pub struct PipelineBuilder {
    renderpass: Option<RenderPass>,
    data_layouts: Vec<ShaderDataLayout>,
}

#[derive(Debug, Error)]
pub enum RALError {
    #[error("exceeded maximum of 8 layouts for a pipeline")]
    TooManyShaderDataLayouts,
}

impl PipelineBuilder {
    pub fn build(self) -> Result<Pipeline, RALError> {
        let mut layouts = [celeritas_sys::ShaderDataLayout::default(); 8];
        if self.data_layouts.len() > MAX_SHADER_DATA_LAYOUTS as usize {
            return Err(RALError::TooManyShaderDataLayouts);
        }
        for (i, layout) in self.data_layouts.iter().enumerate().take(8) {
            layouts[i] = celeritas_sys::ShaderDataLayout::from(layout);
        }

        let mut desc = GraphicsPipelineDesc {
            debug_name: todo!(),
            vertex_desc: todo!(),
            vs: todo!(),
            fs: todo!(),
            data_layouts: layouts,
            data_layouts_count: layouts.len() as u32,
            wireframe: false,
            depth_test: true,
        };
        let p = unsafe {
            GPU_GraphicsPipeline_Create(
                desc,
                self.renderpass
                    .map(|r| r.0)
                    .unwrap_or(GPU_GetDefaultRenderpass()),
            )
        };
        Ok(Pipeline(p))
    }

    pub fn add_shader_layout<S: ShaderData>(&mut self) -> &mut Self {
        let layout = S::layout();
        self.data_layouts.push(layout);
        self
    }
}

pub trait ShaderData {
    fn layout() -> ShaderDataLayout;
    fn bind(&self);
}

pub struct ShaderBinding {
    pub label: String,
    // pub label: *const ::std::os::raw::c_char,
    pub kind: ShaderBindingKind,
    pub vis: ShaderVisibility,
    // pub data: ShaderBinding__bindgen_ty_1,
}

pub enum ShaderBindingKind {
    Bytes(u32),
    Buffer(BufferHandle),
    Texture(TextureHandle),
}

bitflags::bitflags! {
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct ShaderVisibility : u32 {
        const VERTEX = 1 << ShaderVisibility_VISIBILITY_VERTEX;
        const FRAGMENT = 1 << ShaderVisibility_VISIBILITY_FRAGMENT;
        const COMPUTE = 1 << ShaderVisibility_VISIBILITY_COMPUTE;
    }
}
impl Default for ShaderVisibility {
    fn default() -> Self {
        ShaderVisibility::all()
    }
}

#[derive(Default)]
pub struct ShaderDataLayout {
    pub bindings: [Option<ShaderBinding>; 8],
    pub binding_count: usize,
}
impl From<&ShaderDataLayout> for celeritas_sys::ShaderDataLayout {
    fn from(value: &ShaderDataLayout) -> Self {
        todo!()
    }
}

// --- types

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PrimitiveTopology {
    Point,
    Line,
    Triangle,
}
impl From<celeritas_sys::PrimitiveTopology> for PrimitiveTopology {
    fn from(value: celeritas_sys::PrimitiveTopology) -> Self {
        match value {
            celeritas_sys::PrimitiveTopology_PRIMITIVE_TOPOLOGY_POINT => PrimitiveTopology::Point,
            celeritas_sys::PrimitiveTopology_PRIMITIVE_TOPOLOGY_LINE => PrimitiveTopology::Line,
            celeritas_sys::PrimitiveTopology_PRIMITIVE_TOPOLOGY_TRIANGLE => {
                PrimitiveTopology::Triangle
            }
            _ => unreachable!("enum conversion should be infallible"),
        }
    }
}