summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bindgen/rust/Cargo.lock2
-rw-r--r--bindgen/rust/Cargo.toml2
-rw-r--r--bindgen/rust/celeritas-sys/src/lib.rs19
-rw-r--r--bindgen/rust/examples/shaders.rs48
-rw-r--r--bindgen/rust/src/lib.rs1
-rw-r--r--bindgen/rust/src/ral.rs111
-rw-r--r--bindgen/rust/src/shader.rs28
-rw-r--r--src/ral/ral_impl.h1
-rw-r--r--src/ral/ral_types.h2
9 files changed, 188 insertions, 26 deletions
diff --git a/bindgen/rust/Cargo.lock b/bindgen/rust/Cargo.lock
index 75bf0a5..29e8e92 100644
--- a/bindgen/rust/Cargo.lock
+++ b/bindgen/rust/Cargo.lock
@@ -96,12 +96,14 @@ checksum = "2aba8f4e9906c7ce3c73463f62a7f0c65183ada1a2d47e397cc8810827f9694f"
name = "celeritas"
version = "0.1.0"
dependencies = [
+ "bitflags 2.6.0",
"celeritas-sys",
"egui",
"egui_glfw",
"gl",
"serde",
"serde_json",
+ "thiserror",
]
[[package]]
diff --git a/bindgen/rust/Cargo.toml b/bindgen/rust/Cargo.toml
index 4ca1432..e6656ca 100644
--- a/bindgen/rust/Cargo.toml
+++ b/bindgen/rust/Cargo.toml
@@ -11,3 +11,5 @@ serde_json = "1.0.120"
egui = "0.27.1"
egui_glfw = { git = "https://github.com/omnisci3nce/egui_glfw.git", rev = "67b432695433feb59761f3ae984b966ca3da31db" }
gl = "0.14.0"
+thiserror = "1.0.63"
+bitflags = "2.6.0"
diff --git a/bindgen/rust/celeritas-sys/src/lib.rs b/bindgen/rust/celeritas-sys/src/lib.rs
index 676e7c2..c5939fd 100644
--- a/bindgen/rust/celeritas-sys/src/lib.rs
+++ b/bindgen/rust/celeritas-sys/src/lib.rs
@@ -4,4 +4,21 @@
use serde::{Deserialize, Serialize};
-include!(concat!(env!("OUT_DIR"), "/bindings.rs")); \ No newline at end of file
+include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
+
+
+impl Default for ShaderBinding {
+ fn default() -> Self {
+ Self { label: todo!(),
+ kind: ShaderBindingKind_BINDING_COUNT,
+ vis: ShaderVisibility_VISIBILITY_VERTEX,
+ data: todo!()
+ }
+ }
+}
+
+impl Default for ShaderDataLayout {
+ fn default() -> Self {
+ Self { bindings: [ShaderBinding::default(); 8], binding_count: 0 }
+ }
+} \ No newline at end of file
diff --git a/bindgen/rust/examples/shaders.rs b/bindgen/rust/examples/shaders.rs
new file mode 100644
index 0000000..842a8d2
--- /dev/null
+++ b/bindgen/rust/examples/shaders.rs
@@ -0,0 +1,48 @@
+use celeritas::ral::{ShaderData, ShaderDataLayout};
+use celeritas_sys::{
+ Mat4, ShaderVisibility_VISIBILITY_COMPUTE, ShaderVisibility_VISIBILITY_FRAGMENT,
+ ShaderVisibility_VISIBILITY_VERTEX,
+};
+
+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 = [ShaderBinding::default(); 8];
+ // bindings[0] = ShaderBinding {
+ // label: unsafe { CStr::from_bytes_with_nul_unchecked(b"MVP\0").as_ptr() },
+ // kind: ShaderBindingKind_BINDING_BYTES,
+ // vis: shader_vis_all(),
+ // data: ShaderBinding__bindgen_ty_1 {
+ // bytes: ShaderBinding__bindgen_ty_1__bindgen_ty_1 {
+ // size: std::mem::size_of::<MVP>() as u32,
+ // data: ptr::null_mut(),
+ // },
+ // },
+ // };
+ ShaderDataLayout {
+ bindings: todo!(),
+ binding_count: 1,
+ }
+ }
+
+ fn bind(&self) {
+ let layout = Self::layout();
+ for i in 0..layout.binding_count {
+ let binding = &layout.bindings[i];
+ // match binding.kind {}
+ }
+ }
+}
+
+fn main() {}
diff --git a/bindgen/rust/src/lib.rs b/bindgen/rust/src/lib.rs
index 757ba8c..e2a1ff3 100644
--- a/bindgen/rust/src/lib.rs
+++ b/bindgen/rust/src/lib.rs
@@ -9,6 +9,7 @@ pub use celeritas_sys as ffi;
pub mod prelude;
pub mod ral;
+pub mod shader;
use std::{
fs::{self, File},
diff --git a/bindgen/rust/src/ral.rs b/bindgen/rust/src/ral.rs
index 6c196f8..4132885 100644
--- a/bindgen/rust/src/ral.rs
+++ b/bindgen/rust/src/ral.rs
@@ -1,12 +1,15 @@
//! Wrapper around the RAL code in celeritas-core
-use std::{os::raw::c_void, ptr};
+use std::{ffi::c_void, ptr::addr_of_mut};
use celeritas_sys::{
BufferHandle, GPU_CmdEncoder, GPU_CmdEncoder_BeginRender, GPU_CmdEncoder_EndRender,
- GPU_GetDefaultEncoder, GPU_GetDefaultRenderpass, GPU_GraphicsPipeline_Create,
- GraphicsPipelineDesc, ShaderData,
+ 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);
@@ -47,49 +50,107 @@ impl FrameRenderEncoder {
// 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<()>,
+ data_layouts: Vec<ShaderDataLayout>,
}
+
+#[derive(Debug, Error)]
+pub enum RALError {
+ #[error("exceeded maximum of 8 layouts for a pipeline")]
+ TooManyShaderDataLayouts,
+}
+
impl PipelineBuilder {
- // pub fn add_
+ 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);
+ }
- pub fn build(self) -> Pipeline {
- let shad = ShaderData {
- get_layout: todo!(),
- data: ptr::null_mut(),
- };
- let desc = GraphicsPipelineDesc {
+ let mut desc = GraphicsPipelineDesc {
debug_name: todo!(),
vertex_desc: todo!(),
vs: todo!(),
fs: todo!(),
- data_layouts: todo!(),
- data_layouts_count: todo!(),
- wireframe: todo!(),
- depth_test: todo!(),
+ data_layouts: layouts,
+ data_layouts_count: layouts.len() as u32,
+ wireframe: false,
+ depth_test: true,
};
let p = unsafe {
GPU_GraphicsPipeline_Create(
- todo!(),
+ desc,
self.renderpass
.map(|r| r.0)
.unwrap_or(GPU_GetDefaultRenderpass()),
)
};
- Pipeline(p)
+ 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!()
}
}
-// impl Default for PipelineBuilder {
-// fn default() -> Self {
-// Self {
-// renderpass: Default::default(),
-
-// }
-// }
-// }
// --- types
diff --git a/bindgen/rust/src/shader.rs b/bindgen/rust/src/shader.rs
new file mode 100644
index 0000000..46db1f3
--- /dev/null
+++ b/bindgen/rust/src/shader.rs
@@ -0,0 +1,28 @@
+use std::{ffi::c_void, path::Path};
+
+use celeritas_sys::ShaderData;
+
+use crate::ral::{Pipeline, ShaderBinding};
+
+pub struct Shader {
+ pipeline: Pipeline,
+ binding_layouts: Vec<ShaderBinding>,
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn rust_function(data: *mut c_void) -> celeritas_sys::ShaderDataLayout {
+ todo!()
+}
+
+impl Shader {
+ pub fn new(name: String, vs_path: &Path, fs_path: &Path) -> Self {
+ todo!()
+ }
+ pub fn add_layout(&mut self) -> &mut Self {
+ let sd = ShaderData {
+ get_layout: Some(rust_function),
+ data: std::ptr::null_mut(),
+ };
+ self
+ }
+}
diff --git a/src/ral/ral_impl.h b/src/ral/ral_impl.h
index 20b9757..51d1066 100644
--- a/src/ral/ral_impl.h
+++ b/src/ral/ral_impl.h
@@ -74,6 +74,7 @@ void copy_buffer_to_image_oneshot(BufferHandle src, TextureHandle dst);
// --- Render commands
PUB void GPU_EncodeBindPipeline(GPU_CmdEncoder* encoder, GPU_Pipeline* pipeline);
PUB void GPU_EncodeBindShaderData(GPU_CmdEncoder* encoder, u32 group, ShaderData data);
+PUB void GPU_EncodeBindShaderDataRaw(GPU_CmdEncoder* encoder, u32 group, ShaderDataLayout layout, const char* data);
void GPU_EncodeSetDefaults(GPU_CmdEncoder* encoder);
PUB void GPU_EncodeSetVertexBuffer(GPU_CmdEncoder* encoder, BufferHandle buf);
PUB void GPU_EncodeSetIndexBuffer(GPU_CmdEncoder* encoder, BufferHandle buf);
diff --git a/src/ral/ral_types.h b/src/ral/ral_types.h
index c950fe2..8a73041 100644
--- a/src/ral/ral_types.h
+++ b/src/ral/ral_types.h
@@ -246,6 +246,8 @@ typedef struct GraphicsPipelineDesc {
bool depth_test;
} GraphicsPipelineDesc;
+bool GraphicsPipelineDesc_AddShaderDataLayout(GraphicsPipelineDesc* desc, ShaderDataLayout layout);
+
typedef struct GPU_RenderpassDesc {
bool default_framebuffer;
bool has_color_target;