Skip to content

Commit 3b34c26

Browse files
committed
[add] some polish to the API.
1 parent 87b00d7 commit 3b34c26

6 files changed

Lines changed: 31 additions & 20 deletions

File tree

crates/lambda-rs/src/render/buffer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub struct Buffer {
121121
}
122122

123123
impl Buffer {
124-
/// Destroy the buffer and all it's resources with the render context that
124+
/// Destroy the buffer and all its resources with the render context that
125125
/// created it. Dropping the buffer will release GPU resources.
126126
pub fn destroy(self, _render_context: &RenderContext) {}
127127

@@ -294,8 +294,8 @@ impl BufferBuilder {
294294
let element_size = std::mem::size_of::<Data>();
295295
let buffer_length = self.resolve_length(element_size, data.len())?;
296296

297-
// SAFETY: Converting data to bytes is safe because it's underlying
298-
// type, Data, is constrianed to Copy and the lifetime of the slice does
297+
// SAFETY: Converting data to bytes is safe because its underlying
298+
// type, Data, is constrained to Copy and the lifetime of the slice does
299299
// not outlive data.
300300
let bytes = unsafe {
301301
std::slice::from_raw_parts(

crates/lambda-rs/src/render/mesh.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ impl Mesh {
4343
/// Builder for constructing a `Mesh` from vertices and attributes.
4444
#[derive(Clone, Debug)]
4545
pub struct MeshBuilder {
46-
capacity: usize,
4746
vertices: Vec<Vertex>,
4847
attributes: Vec<VertexAttribute>,
4948
}
@@ -52,7 +51,6 @@ impl MeshBuilder {
5251
/// Creates a new mesh builder.
5352
pub fn new() -> Self {
5453
return Self {
55-
capacity: 0,
5654
vertices: Vec::new(),
5755
attributes: Vec::new(),
5856
};
@@ -61,7 +59,6 @@ impl MeshBuilder {
6159
/// Allocates memory for the given number of vertices and fills
6260
/// the mesh with empty vertices.
6361
pub fn with_capacity(&mut self, size: usize) -> &mut Self {
64-
self.capacity = size;
6562
self.vertices.resize(
6663
size,
6764
Vertex {

crates/lambda-rs/src/render/pipeline.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,13 @@ impl RenderPipelineBuilder {
161161
// Shader modules
162162
let vertex_module = platform_pipeline::ShaderModule::from_spirv(
163163
render_context.gpu(),
164-
&vertex_shader.as_binary(),
164+
vertex_shader.binary(),
165165
Some("lambda-vertex-shader"),
166166
);
167167
let fragment_module = fragment_shader.map(|shader| {
168168
platform_pipeline::ShaderModule::from_spirv(
169169
render_context.gpu(),
170-
&shader.as_binary(),
170+
shader.binary(),
171171
Some("lambda-fragment-shader"),
172172
)
173173
});

crates/lambda-rs/src/render/scene_math.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ use crate::math::{
1010
matrix::Matrix,
1111
};
1212

13+
/// Build a 4x4 uniform scaling matrix with `uniform_scale` on the diagonal
14+
/// and `1.0` in the homogeneous component.
15+
fn scaling_matrix(uniform_scale: f32) -> [[f32; 4]; 4] {
16+
return [
17+
[uniform_scale, 0.0, 0.0, 0.0],
18+
[0.0, uniform_scale, 0.0, 0.0],
19+
[0.0, 0.0, uniform_scale, 0.0],
20+
[0.0, 0.0, 0.0, 1.0],
21+
];
22+
}
23+
1324
/// Convert OpenGL-style normalized device coordinates (Z in [-1, 1]) to
1425
/// wgpu/Vulkan/Direct3D normalized device coordinates (Z in [0, 1]).
1526
///
@@ -48,17 +59,7 @@ pub fn compute_model_matrix(
4859
let mut model: [[f32; 4]; 4] = matrix::identity_matrix(4, 4);
4960
// Apply rotation first, then scaling via a diagonal matrix, and finally translation.
5061
model = matrix::rotate_matrix(model, rotation_axis, angle_in_turns);
51-
52-
let mut scaled: [[f32; 4]; 4] = [[0.0; 4]; 4];
53-
for i in 0..4 {
54-
for j in 0..4 {
55-
if i == j {
56-
scaled[i][j] = if i == 3 { 1.0 } else { uniform_scale };
57-
} else {
58-
scaled[i][j] = 0.0;
59-
}
60-
}
61-
}
62+
let scaled = scaling_matrix(uniform_scale);
6263
model = model.multiply(&scaled);
6364

6465
let translation_matrix: [[f32; 4]; 4] =

crates/lambda-rs/src/render/shader.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,19 @@ pub struct Shader {
6262
}
6363

6464
impl Shader {
65-
/// Returns a copy of the SPIR-V binary representation of the shader.
65+
/// Borrow the SPIR‑V binary representation of the shader as a word slice.
66+
///
67+
/// Prefer this accessor to avoid unnecessary allocations when passing the
68+
/// shader to pipeline builders. Use `as_binary` when an owned buffer is
69+
/// explicitly required.
70+
pub fn binary(&self) -> &[u32] {
71+
return &self.binary;
72+
}
73+
74+
/// Returns a copy of the SPIR‑V binary representation of the shader.
75+
///
76+
/// Retained for compatibility with existing code that expects an owned
77+
/// `Vec<u32>`. Prefer `binary()` for zero‑copy borrowing.
6678
pub fn as_binary(&self) -> Vec<u32> {
6779
return self.binary.clone();
6880
}

crates/lambda-rs/src/render/window.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ impl WindowBuilder {
5252
/// surface in `RenderContextBuilder`. This flag is reserved to influence
5353
/// that choice and is currently a no‑op.
5454
pub fn with_vsync(mut self, vsync: bool) -> Self {
55+
self.vsync = vsync;
5556
return self;
5657
}
5758

0 commit comments

Comments
 (0)