Skip to content

Commit 3103b09

Browse files
committed
[add] initial implementations for high level surface types.
1 parent 4932f60 commit 3103b09

4 files changed

Lines changed: 101 additions & 30 deletions

File tree

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

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl RenderContextBuilder {
138138
.configure_with_defaults(
139139
&gpu,
140140
size,
141-
platform::surface::PresentMode::Fifo,
141+
surface::PresentMode::default().to_platform(),
142142
texture::TextureUsages::RENDER_ATTACHMENT.to_platform(),
143143
)
144144
.map_err(|e| {
@@ -148,13 +148,14 @@ impl RenderContextBuilder {
148148
))
149149
})?;
150150

151-
let config = surface.configuration().cloned().ok_or_else(|| {
151+
let config = surface.configuration().ok_or_else(|| {
152152
RenderContextError::SurfaceConfig(
153153
"Surface was not configured".to_string(),
154154
)
155155
})?;
156+
let config = surface::SurfaceConfig::from_platform(config);
156157
let present_mode = config.present_mode;
157-
let texture_usage = texture::TextureUsages::from_platform(config.usage);
158+
let texture_usage = config.usage;
158159

159160
// Initialize a depth texture matching the surface size.
160161
let depth_format = platform::texture::DepthFormat::Depth32Float;
@@ -211,8 +212,8 @@ pub struct RenderContext {
211212
instance: platform::instance::Instance,
212213
surface: platform::surface::Surface<'static>,
213214
gpu: platform::gpu::Gpu,
214-
config: platform::surface::SurfaceConfig,
215-
present_mode: platform::surface::PresentMode,
215+
config: surface::SurfaceConfig,
216+
present_mode: surface::PresentMode,
216217
texture_usage: texture::TextureUsages,
217218
size: (u32, u32),
218219
depth_texture: Option<platform::texture::DepthTexture>,
@@ -344,7 +345,7 @@ impl RenderContext {
344345
return &self.gpu;
345346
}
346347

347-
pub(crate) fn surface_format(&self) -> platform::texture::TextureFormat {
348+
pub(crate) fn surface_format(&self) -> texture::TextureFormat {
348349
return self.config.format;
349350
}
350351

@@ -356,9 +357,10 @@ impl RenderContext {
356357
&self,
357358
sample_count: u32,
358359
) -> bool {
359-
return self
360-
.gpu
361-
.supports_sample_count_for_format(self.config.format, sample_count);
360+
return self.gpu.supports_sample_count_for_format(
361+
self.config.format.to_platform(),
362+
sample_count,
363+
);
362364
}
363365

364366
pub(crate) fn supports_depth_sample_count(
@@ -407,15 +409,18 @@ impl RenderContext {
407409

408410
let mut frame = match self.surface.acquire_next_frame() {
409411
Ok(frame) => frame,
410-
Err(platform::surface::SurfaceError::Lost)
411-
| Err(platform::surface::SurfaceError::Outdated) => {
412-
self.reconfigure_surface(self.size)?;
413-
self
414-
.surface
415-
.acquire_next_frame()
416-
.map_err(RenderError::Surface)?
412+
Err(err) => {
413+
let high_level_err = surface::SurfaceError::from(err);
414+
match high_level_err {
415+
surface::SurfaceError::Lost | surface::SurfaceError::Outdated => {
416+
self.reconfigure_surface(self.size)?;
417+
self.surface.acquire_next_frame().map_err(|e| {
418+
RenderError::Surface(surface::SurfaceError::from(e))
419+
})?
420+
}
421+
_ => return Err(RenderError::Surface(high_level_err)),
422+
}
417423
}
418-
Err(err) => return Err(RenderError::Surface(err)),
419424
};
420425

421426
let view = frame.texture_view();
@@ -472,7 +477,7 @@ impl RenderContext {
472477
if need_recreate {
473478
self.msaa_color = Some(
474479
platform::texture::ColorAttachmentTextureBuilder::new(
475-
self.config.format,
480+
self.config.format.to_platform(),
476481
)
477482
.with_size(self.size.0.max(1), self.size.1.max(1))
478483
.with_sample_count(sample_count)
@@ -1039,12 +1044,13 @@ impl RenderContext {
10391044
.resize(&self.gpu, size)
10401045
.map_err(RenderError::Configuration)?;
10411046

1042-
let config = self.surface.configuration().cloned().ok_or_else(|| {
1047+
let platform_config = self.surface.configuration().ok_or_else(|| {
10431048
RenderError::Configuration("Surface was not configured".to_string())
10441049
})?;
10451050

1051+
let config = surface::SurfaceConfig::from_platform(platform_config);
10461052
self.present_mode = config.present_mode;
1047-
self.texture_usage = texture::TextureUsages::from_platform(config.usage);
1053+
self.texture_usage = config.usage;
10481054
self.config = config;
10491055
return Ok(());
10501056
}
@@ -1089,12 +1095,12 @@ impl RenderContext {
10891095
/// acquisition or command encoding. The renderer logs these and continues when
10901096
/// possible; callers SHOULD treat them as warnings unless persistent.
10911097
pub enum RenderError {
1092-
Surface(platform::surface::SurfaceError),
1098+
Surface(surface::SurfaceError),
10931099
Configuration(String),
10941100
}
10951101

1096-
impl From<platform::surface::SurfaceError> for RenderError {
1097-
fn from(error: platform::surface::SurfaceError) -> Self {
1102+
impl From<surface::SurfaceError> for RenderError {
1103+
fn from(error: surface::SurfaceError) -> Self {
10981104
return RenderError::Surface(error);
10991105
}
11001106
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ impl RenderPipelineBuilder {
555555
}
556556

557557
if fragment_module.is_some() {
558-
rp_builder = rp_builder.with_color_target(surface_format);
558+
rp_builder = rp_builder.with_color_target(surface_format.to_platform());
559559
}
560560

561561
if self.use_depth {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl RenderPassBuilder {
280280
pub fn build(self, render_context: &RenderContext) -> RenderPass {
281281
let sample_count = self.resolve_sample_count(
282282
self.sample_count,
283-
render_context.surface_format(),
283+
render_context.surface_format().to_platform(),
284284
render_context.depth_format(),
285285
|count| render_context.supports_surface_sample_count(count),
286286
|format, count| render_context.supports_depth_sample_count(format, count),

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

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
use lambda_platform::wgpu::surface as platform_surface;
22

3+
use super::texture::{
4+
TextureFormat,
5+
TextureUsages,
6+
};
7+
38
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
49
pub enum PresentMode {
510
/// Vsync enabled; frames wait for vertical blanking interval.
@@ -19,28 +24,28 @@ pub enum PresentMode {
1924
impl PresentMode {
2025
#[inline]
2126
pub(crate) fn to_platform(&self) -> platform_surface::PresentMode {
22-
match self {
27+
return match self {
2328
PresentMode::Fifo => platform_surface::PresentMode::Fifo,
2429
PresentMode::FifoRelaxed => platform_surface::PresentMode::FifoRelaxed,
2530
PresentMode::Immediate => platform_surface::PresentMode::Immediate,
2631
PresentMode::Mailbox => platform_surface::PresentMode::Mailbox,
2732
PresentMode::AutoVsync => platform_surface::PresentMode::AutoVsync,
2833
PresentMode::AutoNoVsync => platform_surface::PresentMode::AutoNoVsync,
29-
}
34+
};
3035
}
3136

3237
#[inline]
3338
pub(crate) fn from_platform(
34-
mode: &platform_surface::PresentMode,
39+
mode: platform_surface::PresentMode,
3540
) -> PresentMode {
36-
match mode {
41+
return match mode {
3742
platform_surface::PresentMode::Fifo => PresentMode::Fifo,
3843
platform_surface::PresentMode::FifoRelaxed => PresentMode::FifoRelaxed,
3944
platform_surface::PresentMode::Immediate => PresentMode::Immediate,
4045
platform_surface::PresentMode::Mailbox => PresentMode::Mailbox,
4146
platform_surface::PresentMode::AutoVsync => PresentMode::AutoVsync,
4247
platform_surface::PresentMode::AutoNoVsync => PresentMode::AutoNoVsync,
43-
}
48+
};
4449
}
4550
}
4651

@@ -49,3 +54,63 @@ impl Default for PresentMode {
4954
return PresentMode::Fifo;
5055
}
5156
}
57+
58+
/// High-level surface configuration.
59+
///
60+
/// Contains the current surface dimensions, format, present mode, and usage
61+
/// flags without exposing platform types.
62+
#[derive(Clone, Debug)]
63+
pub struct SurfaceConfig {
64+
/// Width in pixels.
65+
pub width: u32,
66+
/// Height in pixels.
67+
pub height: u32,
68+
/// The texture format used by the surface.
69+
pub format: TextureFormat,
70+
/// The presentation mode (vsync behavior).
71+
pub present_mode: PresentMode,
72+
/// Texture usage flags for the surface.
73+
pub usage: TextureUsages,
74+
}
75+
76+
impl SurfaceConfig {
77+
pub(crate) fn from_platform(
78+
config: &platform_surface::SurfaceConfig,
79+
) -> Self {
80+
return SurfaceConfig {
81+
width: config.width,
82+
height: config.height,
83+
format: TextureFormat::from_platform(config.format)
84+
.unwrap_or(TextureFormat::Bgra8UnormSrgb),
85+
present_mode: PresentMode::from_platform(config.present_mode),
86+
usage: TextureUsages::from_platform(config.usage),
87+
};
88+
}
89+
}
90+
91+
/// Error wrapper for surface acquisition and presentation errors.
92+
#[derive(Clone, Debug)]
93+
pub enum SurfaceError {
94+
/// The surface has been lost and must be recreated.
95+
Lost,
96+
/// The surface configuration is outdated and must be reconfigured.
97+
Outdated,
98+
/// Out of memory.
99+
OutOfMemory,
100+
/// Timed out waiting for a frame.
101+
Timeout,
102+
/// Other/unclassified error.
103+
Other(String),
104+
}
105+
106+
impl From<platform_surface::SurfaceError> for SurfaceError {
107+
fn from(error: platform_surface::SurfaceError) -> Self {
108+
return match error {
109+
platform_surface::SurfaceError::Lost => SurfaceError::Lost,
110+
platform_surface::SurfaceError::Outdated => SurfaceError::Outdated,
111+
platform_surface::SurfaceError::OutOfMemory => SurfaceError::OutOfMemory,
112+
platform_surface::SurfaceError::Timeout => SurfaceError::Timeout,
113+
platform_surface::SurfaceError::Other(msg) => SurfaceError::Other(msg),
114+
};
115+
}
116+
}

0 commit comments

Comments
 (0)