Skip to content
4 changes: 4 additions & 0 deletions crates/lambda-rs-platform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
//! (graphics) that provide consistent defaults and ergonomic builders, along
//! with shader compilation backends and small helper modules (e.g., OBJ
//! loading and random number generation).
//!
//! Stability: this is an internal support layer for `lambda-rs`. Public
//! types are exposed as a convenience to the higher‑level crate and MAY change
//! between releases to fit engine needs.
pub mod obj;
pub mod rand;
pub mod shader;
Expand Down
27 changes: 17 additions & 10 deletions crates/lambda-rs-platform/src/wgpu/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

use std::num::NonZeroU64;

use crate::wgpu::types as wgpu;
use wgpu;

use crate::wgpu::{
buffer,
Gpu,
};

#[derive(Debug)]
/// Wrapper around `wgpu::BindGroupLayout` that preserves a label.
Expand Down Expand Up @@ -151,12 +156,14 @@ impl BindGroupLayoutBuilder {
}

/// Build the layout using the provided device.
pub fn build(self, device: &wgpu::Device) -> BindGroupLayout {
pub fn build(self, gpu: &Gpu) -> BindGroupLayout {
let raw =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: self.label.as_deref(),
entries: &self.entries,
});
gpu
.device()
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: self.label.as_deref(),
entries: &self.entries,
});
return BindGroupLayout {
raw,
label: self.label,
Expand Down Expand Up @@ -198,14 +205,14 @@ impl<'a> BindGroupBuilder<'a> {
pub fn with_uniform(
mut self,
binding: u32,
buffer: &'a wgpu::Buffer,
buffer: &'a buffer::Buffer,
offset: u64,
size: Option<NonZeroU64>,
) -> Self {
self.entries.push(wgpu::BindGroupEntry {
binding,
resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
buffer,
buffer: buffer.raw(),
offset,
size,
}),
Expand All @@ -214,11 +221,11 @@ impl<'a> BindGroupBuilder<'a> {
}

/// Build the bind group with the accumulated entries.
pub fn build(self, device: &wgpu::Device) -> BindGroup {
pub fn build(self, gpu: &Gpu) -> BindGroup {
let layout = self
.layout
.expect("BindGroupBuilder requires a layout before build");
let raw = device.create_bind_group(&wgpu::BindGroupDescriptor {
let raw = gpu.device().create_bind_group(&wgpu::BindGroupDescriptor {
label: self.label.as_deref(),
layout,
entries: &self.entries,
Expand Down
53 changes: 39 additions & 14 deletions crates/lambda-rs-platform/src/wgpu/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,29 @@
//! This module provides a thin wrapper over `wgpu::Buffer` plus a small
//! builder that handles common initialization patterns and keeps label and
//! usage metadata for debugging/inspection.

use crate::wgpu::{
types as wgpu,
types::util::DeviceExt,
use wgpu::{
self,
util::DeviceExt,
};

use crate::wgpu::Gpu;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
/// Index format for indexed drawing.
pub enum IndexFormat {
Uint16,
Uint32,
}

impl IndexFormat {
pub(crate) fn to_wgpu(self) -> wgpu::IndexFormat {
return match self {
IndexFormat::Uint16 => wgpu::IndexFormat::Uint16,
IndexFormat::Uint32 => wgpu::IndexFormat::Uint32,
};
}
}

#[derive(Clone, Copy, Debug)]
/// Platform buffer usage flags.
pub struct Usage(pub(crate) wgpu::BufferUsages);
Expand Down Expand Up @@ -54,7 +71,7 @@ pub struct Buffer {

impl Buffer {
/// Borrow the underlying `wgpu::Buffer`.
pub fn raw(&self) -> &wgpu::Buffer {
pub(crate) fn raw(&self) -> &wgpu::Buffer {
return &self.raw;
}

Expand All @@ -64,13 +81,18 @@ impl Buffer {
}

/// Size in bytes at creation time.
pub fn size(&self) -> wgpu::BufferAddress {
pub fn size(&self) -> u64 {
return self.size;
}

/// Usage flags used to create the buffer.
pub fn usage(&self) -> wgpu::BufferUsages {
return self.usage;
pub fn usage(&self) -> Usage {
return Usage(self.usage);
}

/// Write raw bytes into the buffer at the given offset.
pub fn write_bytes(&self, gpu: &Gpu, offset: u64, data: &[u8]) {
gpu.queue().write_buffer(&self.raw, offset, data);
}
}

Expand Down Expand Up @@ -119,7 +141,7 @@ impl BufferBuilder {
}

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

let raw = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: self.label.as_deref(),
contents,
usage,
});
let raw =
gpu
.device()
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: self.label.as_deref(),
contents,
usage,
});

return Buffer {
raw,
Expand Down
Loading
Loading