Skip to content

Commit 2ff8f79

Browse files
authored
refactor(lambda-rs): add higher level implementations to rendering types
2 parents 1cca6eb + 3de0312 commit 2ff8f79

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+3365
-1050
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct CommandBuffer {
1717
}
1818

1919
impl CommandBuffer {
20+
/// Convert to the raw wgpu command buffer.
2021
pub(crate) fn into_raw(self) -> wgpu::CommandBuffer {
2122
self.raw
2223
}
@@ -32,10 +33,10 @@ impl CommandEncoder {
3233
}
3334

3435
/// 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> {
36+
pub(crate) fn begin_render_pass_raw<'pass>(
37+
&'pass mut self,
38+
desc: &wgpu::RenderPassDescriptor<'pass>,
39+
) -> wgpu::RenderPass<'pass> {
3940
return self.raw.begin_render_pass(desc);
4041
}
4142

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,10 @@ pub struct Gpu {
200200
}
201201

202202
impl Gpu {
203-
/// Whether the provided surface format supports the sample count for render attachments.
204-
pub fn supports_sample_count_for_surface(
203+
/// Whether the provided texture format supports the sample count for render attachments.
204+
pub fn supports_sample_count_for_format(
205205
&self,
206-
format: super::surface::SurfaceFormat,
206+
format: texture::TextureFormat,
207207
sample_count: u32,
208208
) -> bool {
209209
return self.supports_sample_count(format.to_wgpu(), sample_count);
@@ -350,11 +350,11 @@ mod tests {
350350
}
351351
};
352352
let surface_format =
353-
surface::SurfaceFormat::from_wgpu(wgpu::TextureFormat::Bgra8UnormSrgb);
353+
texture::TextureFormat::from_wgpu(wgpu::TextureFormat::Bgra8UnormSrgb);
354354
let depth_format = texture::DepthFormat::Depth32Float;
355355

356-
assert!(gpu.supports_sample_count_for_surface(surface_format, 1));
357-
assert!(gpu.supports_sample_count_for_surface(surface_format, 0));
356+
assert!(gpu.supports_sample_count_for_format(surface_format, 1));
357+
assert!(gpu.supports_sample_count_for_format(surface_format, 0));
358358
assert!(gpu.supports_sample_count_for_depth(depth_format, 1));
359359
assert!(gpu.supports_sample_count_for_depth(depth_format, 0));
360360
}
@@ -372,10 +372,10 @@ mod tests {
372372
}
373373
};
374374
let surface_format =
375-
surface::SurfaceFormat::from_wgpu(wgpu::TextureFormat::Bgra8Unorm);
375+
texture::TextureFormat::from_wgpu(wgpu::TextureFormat::Bgra8Unorm);
376376
let depth_format = texture::DepthFormat::Depth32Float;
377377

378-
assert!(!gpu.supports_sample_count_for_surface(surface_format, 3));
378+
assert!(!gpu.supports_sample_count_for_format(surface_format, 3));
379379
assert!(!gpu.supports_sample_count_for_depth(depth_format, 3));
380380
}
381381

@@ -393,7 +393,7 @@ no compatible GPU adapter"
393393
}
394394
};
395395
let surface_format =
396-
surface::SurfaceFormat::from_wgpu(wgpu::TextureFormat::Bgra8UnormSrgb);
396+
texture::TextureFormat::from_wgpu(wgpu::TextureFormat::Bgra8UnormSrgb);
397397
let features = gpu
398398
.adapter
399399
.get_texture_format_features(surface_format.to_wgpu());
@@ -405,7 +405,7 @@ no compatible GPU adapter"
405405
.contains(wgpu::TextureFormatFeatureFlags::MULTISAMPLE_X4);
406406

407407
assert_eq!(
408-
gpu.supports_sample_count_for_surface(surface_format, 4),
408+
gpu.supports_sample_count_for_format(surface_format, 4),
409409
expected
410410
);
411411
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ pub use crate::wgpu::vertex::VertexStepMode;
88
use crate::wgpu::{
99
bind,
1010
gpu::Gpu,
11-
surface::SurfaceFormat,
12-
texture::DepthFormat,
11+
texture::{
12+
DepthFormat,
13+
TextureFormat,
14+
},
1315
vertex::ColorFormat,
1416
};
1517

@@ -378,8 +380,8 @@ impl<'a> RenderPipelineBuilder<'a> {
378380
return self;
379381
}
380382

381-
/// Set single color target for fragment stage from a surface format.
382-
pub fn with_surface_color_target(mut self, format: SurfaceFormat) -> Self {
383+
/// Set single color target for fragment stage from a texture format.
384+
pub fn with_color_target(mut self, format: TextureFormat) -> Self {
383385
self.color_target_format = Some(format.to_wgpu());
384386
return self;
385387
}

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
//! `CommandEncoder` and texture view. The returned `RenderPass` borrows the
88
//! encoder and remains valid until dropped.
99
10-
use wgpu::{
11-
self,
12-
RenderPassColorAttachment,
13-
};
10+
use wgpu;
1411

1512
use super::{
1613
bind,
@@ -301,7 +298,11 @@ impl RenderPassBuilder {
301298
}
302299
}
303300

304-
/// Attach a debug label to the render pass.
301+
/// Attach a debug label to the render pass (used only for debugging during
302+
/// builder setup, the actual label must be passed to `build`).
303+
#[deprecated(
304+
note = "The label must be passed directly to build() for proper lifetime management"
305+
)]
305306
pub fn with_label(mut self, label: &str) -> Self {
306307
self.config.label = Some(label.to_string());
307308
return self;
@@ -337,13 +338,22 @@ impl RenderPassBuilder {
337338
/// Build (begin) the render pass on the provided encoder using the provided
338339
/// color attachments list. The attachments list MUST outlive the returned
339340
/// render pass value.
341+
///
342+
/// # Arguments
343+
/// * `encoder` - The command encoder to begin the pass on.
344+
/// * `attachments` - Color attachments for the pass.
345+
/// * `depth_view` - Optional depth view.
346+
/// * `depth_ops` - Optional depth operations.
347+
/// * `stencil_ops` - Optional stencil operations.
348+
/// * `label` - Optional debug label (must outlive the pass).
340349
pub fn build<'view>(
341-
&'view self,
350+
self,
342351
encoder: &'view mut command::CommandEncoder,
343352
attachments: &'view mut RenderColorAttachments<'view>,
344353
depth_view: Option<crate::wgpu::surface::TextureViewRef<'view>>,
345354
depth_ops: Option<DepthOperations>,
346355
stencil_ops: Option<StencilOperations>,
356+
label: Option<&'view str>,
347357
) -> RenderPass<'view> {
348358
let operations = match self.config.color_operations.load {
349359
ColorLoadOp::Load => wgpu::Operations {
@@ -417,8 +427,8 @@ impl RenderPassBuilder {
417427
}
418428
});
419429

420-
let desc: wgpu::RenderPassDescriptor<'view> = wgpu::RenderPassDescriptor {
421-
label: self.config.label.as_deref(),
430+
let desc = wgpu::RenderPassDescriptor {
431+
label,
422432
color_attachments: attachments.as_slice(),
423433
depth_stencil_attachment,
424434
timestamp_writes: None,

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

Lines changed: 19 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,30 @@ use wgpu::rwh::{
66
use super::{
77
gpu::Gpu,
88
instance::Instance,
9+
texture::{
10+
TextureFormat,
11+
TextureUsages,
12+
},
913
};
1014
use crate::winit::WindowHandle;
1115

1216
/// Present modes supported by the surface.
13-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1417
///
1518
/// This wrapper hides the underlying `wgpu` type from higher layers while
1619
/// preserving the same semantics.
20+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1721
pub enum PresentMode {
22+
/// Vsync enabled; frames wait for vertical blanking interval.
1823
Fifo,
24+
/// Vsync with relaxed timing; may tear if frames miss the interval.
1925
FifoRelaxed,
26+
/// No Vsync; immediate presentation (may tear).
2027
Immediate,
28+
/// Triple-buffered presentation when supported.
2129
Mailbox,
30+
/// Automatic Vsync selection by the platform.
2231
AutoVsync,
32+
/// Automatic non-Vsync selection by the platform.
2333
AutoNoVsync,
2434
}
2535

@@ -48,98 +58,30 @@ impl PresentMode {
4858
}
4959
}
5060

51-
/// Wrapper for texture usage flags used by surfaces.
52-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
53-
pub struct TextureUsages(wgpu::TextureUsages);
54-
55-
impl TextureUsages {
56-
/// Render attachment usage.
57-
pub const RENDER_ATTACHMENT: TextureUsages =
58-
TextureUsages(wgpu::TextureUsages::RENDER_ATTACHMENT);
59-
/// Texture binding usage.
60-
pub const TEXTURE_BINDING: TextureUsages =
61-
TextureUsages(wgpu::TextureUsages::TEXTURE_BINDING);
62-
/// Copy destination usage.
63-
pub const COPY_DST: TextureUsages =
64-
TextureUsages(wgpu::TextureUsages::COPY_DST);
65-
/// Copy source usage.
66-
pub const COPY_SRC: TextureUsages =
67-
TextureUsages(wgpu::TextureUsages::COPY_SRC);
68-
69-
pub(crate) fn to_wgpu(self) -> wgpu::TextureUsages {
70-
return self.0;
71-
}
72-
73-
pub(crate) fn from_wgpu(flags: wgpu::TextureUsages) -> Self {
74-
return TextureUsages(flags);
75-
}
76-
77-
/// Check whether this flags set contains another set.
78-
pub fn contains(self, other: TextureUsages) -> bool {
79-
return self.0.contains(other.0);
80-
}
81-
}
82-
83-
impl std::ops::BitOr for TextureUsages {
84-
type Output = TextureUsages;
85-
86-
fn bitor(self, rhs: TextureUsages) -> TextureUsages {
87-
return TextureUsages(self.0 | rhs.0);
88-
}
89-
}
90-
91-
/// Wrapper around a surface color format.
92-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
93-
pub struct SurfaceFormat(wgpu::TextureFormat);
94-
95-
impl SurfaceFormat {
96-
/// Common sRGB swapchain format used for windowed rendering.
97-
pub const BGRA8_UNORM_SRGB: SurfaceFormat =
98-
SurfaceFormat(wgpu::TextureFormat::Bgra8UnormSrgb);
99-
100-
pub(crate) fn to_wgpu(self) -> wgpu::TextureFormat {
101-
return self.0;
102-
}
103-
104-
pub(crate) fn from_wgpu(fmt: wgpu::TextureFormat) -> Self {
105-
return SurfaceFormat(fmt);
106-
}
107-
108-
/// Whether this format is sRGB.
109-
pub fn is_srgb(self) -> bool {
110-
return self.0.is_srgb();
111-
}
112-
113-
/// Return the sRGB variant of the format when applicable.
114-
pub fn add_srgb_suffix(self) -> Self {
115-
return SurfaceFormat(self.0.add_srgb_suffix());
116-
}
117-
}
118-
11961
/// Public, engine-facing surface configuration that avoids exposing `wgpu`.
12062
#[derive(Clone, Debug)]
12163
pub struct SurfaceConfig {
12264
pub width: u32,
12365
pub height: u32,
124-
pub format: SurfaceFormat,
66+
pub format: TextureFormat,
12567
pub present_mode: PresentMode,
12668
pub usage: TextureUsages,
127-
pub view_formats: Vec<SurfaceFormat>,
69+
pub view_formats: Vec<TextureFormat>,
12870
}
12971

13072
impl SurfaceConfig {
13173
pub(crate) fn from_wgpu(config: &wgpu::SurfaceConfiguration) -> Self {
13274
return SurfaceConfig {
13375
width: config.width,
13476
height: config.height,
135-
format: SurfaceFormat::from_wgpu(config.format),
77+
format: TextureFormat::from_wgpu(config.format),
13678
present_mode: PresentMode::from_wgpu(config.present_mode),
13779
usage: TextureUsages::from_wgpu(config.usage),
13880
view_formats: config
13981
.view_formats
14082
.iter()
14183
.copied()
142-
.map(SurfaceFormat::from_wgpu)
84+
.map(TextureFormat::from_wgpu)
14385
.collect(),
14486
};
14587
}
@@ -267,7 +209,7 @@ pub struct Surface<'window> {
267209
label: String,
268210
surface: wgpu::Surface<'window>,
269211
configuration: Option<SurfaceConfig>,
270-
format: Option<SurfaceFormat>,
212+
format: Option<TextureFormat>,
271213
}
272214

273215
impl<'window> Surface<'window> {
@@ -287,19 +229,19 @@ impl<'window> Surface<'window> {
287229
}
288230

289231
/// Preferred surface format if known (set during configuration).
290-
pub fn format(&self) -> Option<SurfaceFormat> {
232+
pub fn format(&self) -> Option<TextureFormat> {
291233
return self.format;
292234
}
293235

294236
/// Configure the surface and cache the result for queries such as `format()`.
295-
pub(crate) fn configure_raw(
237+
fn configure_raw(
296238
&mut self,
297239
device: &wgpu::Device,
298240
config: &wgpu::SurfaceConfiguration,
299241
) {
300242
self.surface.configure(device, config);
301243
self.configuration = Some(SurfaceConfig::from_wgpu(config));
302-
self.format = Some(SurfaceFormat::from_wgpu(config.format));
244+
self.format = Some(TextureFormat::from_wgpu(config.format));
303245
}
304246

305247
/// Configure the surface using common engine defaults:

0 commit comments

Comments
 (0)