Skip to content

Commit 07e221a

Browse files
authored
Merge pull request #66 from lambda-sh/vmarcella/refactor-render-pipeline
Refactor rendering API
2 parents baa4ca8 + d59603e commit 07e221a

File tree

29 files changed

+2497
-1049
lines changed

29 files changed

+2497
-1049
lines changed

.ignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
docs
21
archive/lambda_cpp/vendor
32
\.git
43
target

crates/lambda-rs-args/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ pub struct ArgumentParser {
3636
is_subcommand: bool,
3737
}
3838

39-
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
4039
/// Supported value types for an argument definition.
40+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
4141
pub enum ArgumentType {
4242
/// `true`/`false` (or implied by presence when compiled as a flag).
4343
Boolean,
@@ -61,8 +61,8 @@ pub enum ArgumentType {
6161
DoubleList,
6262
}
6363

64-
#[derive(Debug, Clone, PartialEq, PartialOrd)]
6564
/// Parsed value container used in results and defaults.
65+
#[derive(Debug, Clone, PartialEq, PartialOrd)]
6666
pub enum ArgumentValue {
6767
None,
6868
Boolean(bool),
@@ -112,8 +112,8 @@ impl Into<f64> for ArgumentValue {
112112
}
113113
}
114114

115-
#[derive(Debug)]
116115
/// Declarative definition for a single CLI argument or positional parameter.
116+
#[derive(Debug)]
117117
pub struct Argument {
118118
name: String,
119119
description: String,
@@ -222,8 +222,8 @@ impl Argument {
222222
}
223223
}
224224

225-
#[derive(Debug, Clone)]
226225
/// A single parsed argument result as `(name, value)`.
226+
#[derive(Debug, Clone)]
227227
pub struct ParsedArgument {
228228
name: String,
229229
value: ArgumentValue,
@@ -830,8 +830,8 @@ fn parse_value(arg: &Argument, raw: &str) -> Result<ArgumentValue, ArgsError> {
830830
}
831831
}
832832

833-
#[derive(Debug)]
834833
/// Errors that may occur during argument parsing.
834+
#[derive(Debug)]
835835
pub enum ArgsError {
836836
/// An unknown flag or option was encountered.
837837
UnknownArgument(String),

crates/lambda-rs-platform/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
//! (graphics) that provide consistent defaults and ergonomic builders, along
66
//! with shader compilation backends and small helper modules (e.g., OBJ
77
//! loading and random number generation).
8+
//!
9+
//! Stability: this is an internal support layer for `lambda-rs`. Public
10+
//! types are exposed as a convenience to the higher‑level crate and MAY change
11+
//! between releases to fit engine needs.
812
pub mod obj;
913
pub mod rand;
1014
pub mod shader;

crates/lambda-rs-platform/src/wgpu/bind.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
77
use std::num::NonZeroU64;
88

9-
use crate::wgpu::types as wgpu;
9+
use wgpu;
10+
11+
use crate::wgpu::{
12+
buffer,
13+
gpu::Gpu,
14+
};
1015

11-
#[derive(Debug)]
1216
/// Wrapper around `wgpu::BindGroupLayout` that preserves a label.
17+
#[derive(Debug)]
1318
pub struct BindGroupLayout {
1419
pub(crate) raw: wgpu::BindGroupLayout,
1520
pub(crate) label: Option<String>,
@@ -27,8 +32,8 @@ impl BindGroupLayout {
2732
}
2833
}
2934

30-
#[derive(Debug)]
3135
/// Wrapper around `wgpu::BindGroup` that preserves a label.
36+
#[derive(Debug)]
3237
pub struct BindGroup {
3338
pub(crate) raw: wgpu::BindGroup,
3439
pub(crate) label: Option<String>,
@@ -46,8 +51,8 @@ impl BindGroup {
4651
}
4752
}
4853

49-
#[derive(Clone, Copy, Debug)]
5054
/// Visibility of a binding across shader stages.
55+
#[derive(Clone, Copy, Debug)]
5156
pub enum Visibility {
5257
Vertex,
5358
Fragment,
@@ -94,8 +99,8 @@ mod tests {
9499
}
95100
}
96101

97-
#[derive(Default)]
98102
/// Builder for creating a `wgpu::BindGroupLayout`.
103+
#[derive(Default)]
99104
pub struct BindGroupLayoutBuilder {
100105
label: Option<String>,
101106
entries: Vec<wgpu::BindGroupLayoutEntry>,
@@ -151,21 +156,23 @@ impl BindGroupLayoutBuilder {
151156
}
152157

153158
/// Build the layout using the provided device.
154-
pub fn build(self, device: &wgpu::Device) -> BindGroupLayout {
159+
pub fn build(self, gpu: &Gpu) -> BindGroupLayout {
155160
let raw =
156-
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
157-
label: self.label.as_deref(),
158-
entries: &self.entries,
159-
});
161+
gpu
162+
.device()
163+
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
164+
label: self.label.as_deref(),
165+
entries: &self.entries,
166+
});
160167
return BindGroupLayout {
161168
raw,
162169
label: self.label,
163170
};
164171
}
165172
}
166173

167-
#[derive(Default)]
168174
/// Builder for creating a `wgpu::BindGroup`.
175+
#[derive(Default)]
169176
pub struct BindGroupBuilder<'a> {
170177
label: Option<String>,
171178
layout: Option<&'a wgpu::BindGroupLayout>,
@@ -198,14 +205,14 @@ impl<'a> BindGroupBuilder<'a> {
198205
pub fn with_uniform(
199206
mut self,
200207
binding: u32,
201-
buffer: &'a wgpu::Buffer,
208+
buffer: &'a buffer::Buffer,
202209
offset: u64,
203210
size: Option<NonZeroU64>,
204211
) -> Self {
205212
self.entries.push(wgpu::BindGroupEntry {
206213
binding,
207214
resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
208-
buffer,
215+
buffer: buffer.raw(),
209216
offset,
210217
size,
211218
}),
@@ -214,11 +221,11 @@ impl<'a> BindGroupBuilder<'a> {
214221
}
215222

216223
/// Build the bind group with the accumulated entries.
217-
pub fn build(self, device: &wgpu::Device) -> BindGroup {
224+
pub fn build(self, gpu: &Gpu) -> BindGroup {
218225
let layout = self
219226
.layout
220227
.expect("BindGroupBuilder requires a layout before build");
221-
let raw = device.create_bind_group(&wgpu::BindGroupDescriptor {
228+
let raw = gpu.device().create_bind_group(&wgpu::BindGroupDescriptor {
222229
label: self.label.as_deref(),
223230
layout,
224231
entries: &self.entries,

crates/lambda-rs-platform/src/wgpu/buffer.rs

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,31 @@
33
//! This module provides a thin wrapper over `wgpu::Buffer` plus a small
44
//! builder that handles common initialization patterns and keeps label and
55
//! usage metadata for debugging/inspection.
6-
7-
use crate::wgpu::{
8-
types as wgpu,
9-
types::util::DeviceExt,
6+
use wgpu::{
7+
self,
8+
util::DeviceExt,
109
};
1110

12-
#[derive(Clone, Copy, Debug)]
11+
use crate::wgpu::gpu::Gpu;
12+
13+
/// Index format for indexed drawing.
14+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15+
pub enum IndexFormat {
16+
Uint16,
17+
Uint32,
18+
}
19+
20+
impl IndexFormat {
21+
pub(crate) fn to_wgpu(self) -> wgpu::IndexFormat {
22+
return match self {
23+
IndexFormat::Uint16 => wgpu::IndexFormat::Uint16,
24+
IndexFormat::Uint32 => wgpu::IndexFormat::Uint32,
25+
};
26+
}
27+
}
28+
1329
/// Platform buffer usage flags.
30+
#[derive(Clone, Copy, Debug)]
1431
pub struct Usage(pub(crate) wgpu::BufferUsages);
1532

1633
impl Usage {
@@ -43,8 +60,8 @@ impl Default for Usage {
4360
}
4461
}
4562

46-
#[derive(Debug)]
4763
/// Wrapper around `wgpu::Buffer` with metadata.
64+
#[derive(Debug)]
4865
pub struct Buffer {
4966
pub(crate) raw: wgpu::Buffer,
5067
pub(crate) label: Option<String>,
@@ -54,7 +71,7 @@ pub struct Buffer {
5471

5572
impl Buffer {
5673
/// Borrow the underlying `wgpu::Buffer`.
57-
pub fn raw(&self) -> &wgpu::Buffer {
74+
pub(crate) fn raw(&self) -> &wgpu::Buffer {
5875
return &self.raw;
5976
}
6077

@@ -64,18 +81,23 @@ impl Buffer {
6481
}
6582

6683
/// Size in bytes at creation time.
67-
pub fn size(&self) -> wgpu::BufferAddress {
84+
pub fn size(&self) -> u64 {
6885
return self.size;
6986
}
7087

7188
/// Usage flags used to create the buffer.
72-
pub fn usage(&self) -> wgpu::BufferUsages {
73-
return self.usage;
89+
pub fn usage(&self) -> Usage {
90+
return Usage(self.usage);
91+
}
92+
93+
/// Write raw bytes into the buffer at the given offset.
94+
pub fn write_bytes(&self, gpu: &Gpu, offset: u64, data: &[u8]) {
95+
gpu.queue().write_buffer(&self.raw, offset, data);
7496
}
7597
}
7698

77-
#[derive(Default)]
7899
/// Builder for creating a `Buffer` with optional initial contents.
100+
#[derive(Default)]
79101
pub struct BufferBuilder {
80102
label: Option<String>,
81103
size: usize,
@@ -119,7 +141,7 @@ impl BufferBuilder {
119141
}
120142

121143
/// Create a buffer initialized with `contents`.
122-
pub fn build_init(self, device: &wgpu::Device, contents: &[u8]) -> Buffer {
144+
pub fn build_init(self, gpu: &Gpu, contents: &[u8]) -> Buffer {
123145
let size = if self.size == 0 {
124146
contents.len()
125147
} else {
@@ -131,11 +153,14 @@ impl BufferBuilder {
131153
usage |= wgpu::BufferUsages::COPY_DST;
132154
}
133155

134-
let raw = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
135-
label: self.label.as_deref(),
136-
contents,
137-
usage,
138-
});
156+
let raw =
157+
gpu
158+
.device()
159+
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
160+
label: self.label.as_deref(),
161+
contents,
162+
usage,
163+
});
139164

140165
return Buffer {
141166
raw,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//! Command encoding abstractions around `wgpu::CommandEncoder`, `wgpu::RenderPass`,
2+
//! and `wgpu::CommandBuffer` that expose only the operations needed by the
3+
//! engine while keeping raw `wgpu` types crate-internal.
4+
5+
use super::gpu;
6+
7+
/// Thin wrapper around `wgpu::CommandEncoder` with convenience helpers.
8+
#[derive(Debug)]
9+
pub struct CommandEncoder {
10+
raw: wgpu::CommandEncoder,
11+
}
12+
13+
/// Wrapper around `wgpu::CommandBuffer` to avoid exposing raw types upstream.
14+
#[derive(Debug)]
15+
pub struct CommandBuffer {
16+
raw: wgpu::CommandBuffer,
17+
}
18+
19+
impl CommandBuffer {
20+
pub(crate) fn into_raw(self) -> wgpu::CommandBuffer {
21+
self.raw
22+
}
23+
}
24+
25+
impl CommandEncoder {
26+
/// Create a new command encoder with an optional label.
27+
pub fn new(gpu: &gpu::Gpu, label: Option<&str>) -> Self {
28+
let raw = gpu
29+
.device()
30+
.create_command_encoder(&wgpu::CommandEncoderDescriptor { label });
31+
return Self { raw };
32+
}
33+
34+
/// Internal helper for beginning a render pass. Used by the render pass builder.
35+
pub(crate) fn begin_render_pass_raw<'view>(
36+
&'view mut self,
37+
desc: &wgpu::RenderPassDescriptor<'view>,
38+
) -> wgpu::RenderPass<'view> {
39+
return self.raw.begin_render_pass(desc);
40+
}
41+
42+
/// Finish recording and return the command buffer.
43+
pub fn finish(self) -> CommandBuffer {
44+
return CommandBuffer {
45+
raw: self.raw.finish(),
46+
};
47+
}
48+
}
49+
50+
// RenderPass wrapper and its methods now live under `wgpu::render_pass`.

0 commit comments

Comments
 (0)