Skip to content

Commit 84c672a

Browse files
committed
[update] TextureViewRef to live with the render target.
1 parent bb025e0 commit 84c672a

File tree

5 files changed

+225
-25
lines changed

5 files changed

+225
-25
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
use lambda_platform::wgpu as platform;
88

9-
use super::surface::TextureView;
9+
use super::targets::surface::TextureView;
1010

1111
#[derive(Debug, Default)]
1212
/// High‑level color attachments collection used when beginning a render pass.

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ pub mod pipeline;
4040
pub mod render_pass;
4141
pub mod scene_math;
4242
pub mod shader;
43-
pub mod surface;
4443
pub mod targets;
4544
pub mod texture;
4645
pub mod validation;
@@ -149,7 +148,7 @@ impl RenderContextBuilder {
149148
.configure_with_defaults(
150149
&gpu,
151150
size,
152-
surface::PresentMode::default(),
151+
targets::surface::PresentMode::default(),
153152
texture::TextureUsages::RENDER_ATTACHMENT,
154153
)
155154
.map_err(|e| {
@@ -226,7 +225,7 @@ pub struct RenderContext {
226225
instance: instance::Instance,
227226
surface: targets::surface::WindowSurface,
228227
gpu: gpu::Gpu,
229-
config: surface::SurfaceConfig,
228+
config: targets::surface::SurfaceConfig,
230229
texture_usage: texture::TextureUsages,
231230
size: (u32, u32),
232231
depth_texture: Option<texture::DepthTexture>,
@@ -484,7 +483,7 @@ impl RenderContext {
484483
fn ensure_msaa_color_texture(
485484
&mut self,
486485
sample_count: u32,
487-
) -> surface::TextureView<'_> {
486+
) -> targets::surface::TextureView<'_> {
488487
let need_recreate = match &self.msaa_color {
489488
Some(_) => self.msaa_sample_count != sample_count,
490489
None => true,
@@ -520,7 +519,8 @@ impl RenderContext {
520519
let frame = match self.surface.acquire_frame() {
521520
Ok(frame) => frame,
522521
Err(err) => match err {
523-
surface::SurfaceError::Lost | surface::SurfaceError::Outdated => {
522+
targets::surface::SurfaceError::Lost
523+
| targets::surface::SurfaceError::Outdated => {
524524
self.reconfigure_surface(self.size)?;
525525
self
526526
.surface
@@ -594,7 +594,7 @@ impl RenderContext {
594594
command_iter: &mut std::vec::IntoIter<RenderCommand>,
595595
render_pass: ResourceId,
596596
viewport: viewport::Viewport,
597-
surface_view: surface::TextureView<'view>,
597+
surface_view: targets::surface::TextureView<'view>,
598598
) -> Result<(), RenderError> {
599599
// Clone the render pass descriptor to avoid borrowing self while we need
600600
// mutable access for MSAA texture creation.
@@ -978,12 +978,12 @@ impl RenderContext {
978978
/// acquisition or command encoding. The renderer logs these and continues when
979979
/// possible; callers SHOULD treat them as warnings unless persistent.
980980
pub enum RenderError {
981-
Surface(surface::SurfaceError),
981+
Surface(targets::surface::SurfaceError),
982982
Configuration(String),
983983
}
984984

985-
impl From<surface::SurfaceError> for RenderError {
986-
fn from(error: surface::SurfaceError) -> Self {
985+
impl From<targets::surface::SurfaceError> for RenderError {
986+
fn from(error: targets::surface::SurfaceError) -> Self {
987987
return RenderError::Surface(error);
988988
}
989989
}

crates/lambda-rs/src/render/targets/offscreen.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
//! Provides `OffscreenTarget` and `OffscreenTargetBuilder` for render‑to‑texture
44
//! workflows without exposing platform texture types at call sites.
55
6+
use super::surface::TextureView;
67
use crate::render::{
78
gpu::Gpu,
8-
surface,
99
texture,
1010
validation,
1111
RenderContext,
@@ -66,11 +66,11 @@ impl OffscreenTarget {
6666
return self.msaa_color.as_ref();
6767
}
6868

69-
pub(crate) fn resolve_view(&self) -> surface::TextureView<'_> {
69+
pub(crate) fn resolve_view(&self) -> TextureView<'_> {
7070
return self.resolve_color.view_ref();
7171
}
7272

73-
pub(crate) fn msaa_view(&self) -> Option<surface::TextureView<'_>> {
73+
pub(crate) fn msaa_view(&self) -> Option<TextureView<'_>> {
7474
return self.msaa_color.as_ref().map(|t| t.view_ref());
7575
}
7676

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

Lines changed: 200 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,213 @@ use lambda_platform::wgpu as platform;
88
use crate::render::{
99
gpu::Gpu,
1010
instance::Instance,
11-
surface::{
12-
Frame,
13-
PresentMode,
14-
SurfaceConfig,
15-
SurfaceError,
16-
},
1711
texture::{
1812
TextureFormat,
1913
TextureUsages,
2014
},
2115
window::Window,
2216
};
2317

18+
// ---------------------------------------------------------------------------
19+
// TextureView
20+
// ---------------------------------------------------------------------------
21+
22+
/// High-level reference to a texture view for render pass attachments.
23+
///
24+
/// This type wraps the platform `TextureViewRef` and provides a stable
25+
/// engine-level API for referencing texture views without exposing `wgpu`
26+
/// types at call sites.
27+
#[derive(Clone, Copy)]
28+
pub struct TextureView<'a> {
29+
inner: platform::surface::TextureViewRef<'a>,
30+
}
31+
32+
impl<'a> TextureView<'a> {
33+
/// Create a high-level texture view from a platform texture view reference.
34+
#[inline]
35+
pub(crate) fn from_platform(
36+
view: platform::surface::TextureViewRef<'a>,
37+
) -> Self {
38+
return TextureView { inner: view };
39+
}
40+
41+
/// Convert to the platform texture view reference for internal use.
42+
#[inline]
43+
pub(crate) fn to_platform(&self) -> platform::surface::TextureViewRef<'a> {
44+
return self.inner;
45+
}
46+
}
47+
48+
impl<'a> std::fmt::Debug for TextureView<'a> {
49+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50+
return f.debug_struct("TextureView").finish_non_exhaustive();
51+
}
52+
}
53+
54+
// ---------------------------------------------------------------------------
55+
// Frame
56+
// ---------------------------------------------------------------------------
57+
58+
/// A single acquired frame from the presentation surface.
59+
///
60+
/// This type wraps the platform `Frame` and provides access to its texture
61+
/// view for rendering. The frame must be presented after rendering is complete
62+
/// by calling `present()`.
63+
pub struct Frame {
64+
inner: platform::surface::Frame,
65+
}
66+
67+
impl Frame {
68+
/// Create a high-level frame from a platform frame.
69+
#[inline]
70+
pub(crate) fn from_platform(frame: platform::surface::Frame) -> Self {
71+
return Frame { inner: frame };
72+
}
73+
74+
/// Borrow the default texture view for rendering to this frame.
75+
#[inline]
76+
pub fn texture_view(&self) -> TextureView<'_> {
77+
return TextureView::from_platform(self.inner.texture_view());
78+
}
79+
80+
/// Present the frame to the swapchain.
81+
///
82+
/// This consumes the frame and submits it for display. After calling this
83+
/// method, the frame's texture is no longer valid for rendering.
84+
#[inline]
85+
pub fn present(self) {
86+
self.inner.present();
87+
}
88+
}
89+
90+
impl std::fmt::Debug for Frame {
91+
fn fmt(&self, frame: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92+
return frame.debug_struct("Frame").finish_non_exhaustive();
93+
}
94+
}
95+
96+
// ---------------------------------------------------------------------------
97+
// PresentMode
98+
// ---------------------------------------------------------------------------
99+
100+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
101+
pub enum PresentMode {
102+
/// Vsync enabled; frames wait for vertical blanking interval.
103+
Fifo,
104+
/// Vsync with relaxed timing; may tear if frames miss the interval.
105+
FifoRelaxed,
106+
/// No Vsync; immediate presentation (may tear).
107+
Immediate,
108+
/// Triple-buffered presentation when supported.
109+
Mailbox,
110+
/// Automatic Vsync selection by the platform.
111+
AutoVsync,
112+
/// Automatic non-Vsync selection by the platform.
113+
AutoNoVsync,
114+
}
115+
116+
impl PresentMode {
117+
#[inline]
118+
pub(crate) fn to_platform(&self) -> platform::surface::PresentMode {
119+
return match self {
120+
PresentMode::Fifo => platform::surface::PresentMode::Fifo,
121+
PresentMode::FifoRelaxed => platform::surface::PresentMode::FifoRelaxed,
122+
PresentMode::Immediate => platform::surface::PresentMode::Immediate,
123+
PresentMode::Mailbox => platform::surface::PresentMode::Mailbox,
124+
PresentMode::AutoVsync => platform::surface::PresentMode::AutoVsync,
125+
PresentMode::AutoNoVsync => platform::surface::PresentMode::AutoNoVsync,
126+
};
127+
}
128+
129+
#[inline]
130+
pub(crate) fn from_platform(
131+
mode: platform::surface::PresentMode,
132+
) -> PresentMode {
133+
return match mode {
134+
platform::surface::PresentMode::Fifo => PresentMode::Fifo,
135+
platform::surface::PresentMode::FifoRelaxed => PresentMode::FifoRelaxed,
136+
platform::surface::PresentMode::Immediate => PresentMode::Immediate,
137+
platform::surface::PresentMode::Mailbox => PresentMode::Mailbox,
138+
platform::surface::PresentMode::AutoVsync => PresentMode::AutoVsync,
139+
platform::surface::PresentMode::AutoNoVsync => PresentMode::AutoNoVsync,
140+
};
141+
}
142+
}
143+
144+
impl Default for PresentMode {
145+
fn default() -> Self {
146+
return PresentMode::Fifo;
147+
}
148+
}
149+
150+
// ---------------------------------------------------------------------------
151+
// SurfaceConfig
152+
// ---------------------------------------------------------------------------
153+
154+
/// High-level surface configuration.
155+
///
156+
/// Contains the current surface dimensions, format, present mode, and usage
157+
/// flags without exposing platform types.
158+
#[derive(Clone, Debug)]
159+
pub struct SurfaceConfig {
160+
/// Width in pixels.
161+
pub width: u32,
162+
/// Height in pixels.
163+
pub height: u32,
164+
/// The texture format used by the surface.
165+
pub format: TextureFormat,
166+
/// The presentation mode (vsync behavior).
167+
pub present_mode: PresentMode,
168+
/// Texture usage flags for the surface.
169+
pub usage: TextureUsages,
170+
}
171+
172+
impl SurfaceConfig {
173+
pub(crate) fn from_platform(
174+
config: &platform::surface::SurfaceConfig,
175+
) -> Self {
176+
return SurfaceConfig {
177+
width: config.width,
178+
height: config.height,
179+
format: TextureFormat::from_platform(config.format)
180+
.unwrap_or(TextureFormat::Bgra8UnormSrgb),
181+
present_mode: PresentMode::from_platform(config.present_mode),
182+
usage: TextureUsages::from_platform(config.usage),
183+
};
184+
}
185+
}
186+
187+
// ---------------------------------------------------------------------------
188+
// SurfaceError
189+
// ---------------------------------------------------------------------------
190+
191+
/// Error wrapper for surface acquisition and presentation errors.
192+
#[derive(Clone, Debug)]
193+
pub enum SurfaceError {
194+
/// The surface has been lost and must be recreated.
195+
Lost,
196+
/// The surface configuration is outdated and must be reconfigured.
197+
Outdated,
198+
/// Out of memory.
199+
OutOfMemory,
200+
/// Timed out waiting for a frame.
201+
Timeout,
202+
/// Other/unclassified error.
203+
Other(String),
204+
}
205+
206+
impl From<platform::surface::SurfaceError> for SurfaceError {
207+
fn from(error: platform::surface::SurfaceError) -> Self {
208+
return match error {
209+
platform::surface::SurfaceError::Lost => SurfaceError::Lost,
210+
platform::surface::SurfaceError::Outdated => SurfaceError::Outdated,
211+
platform::surface::SurfaceError::OutOfMemory => SurfaceError::OutOfMemory,
212+
platform::surface::SurfaceError::Timeout => SurfaceError::Timeout,
213+
platform::surface::SurfaceError::Other(msg) => SurfaceError::Other(msg),
214+
};
215+
}
216+
}
217+
24218
// ---------------------------------------------------------------------------
25219
// RenderTarget trait
26220
// ---------------------------------------------------------------------------

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,10 @@ impl ColorAttachmentTexture {
214214
}
215215

216216
/// Borrow a texture view reference for use in render pass attachments.
217-
pub(crate) fn view_ref(&self) -> crate::render::surface::TextureView<'_> {
218-
return crate::render::surface::TextureView::from_platform(
217+
pub(crate) fn view_ref(
218+
&self,
219+
) -> crate::render::targets::surface::TextureView<'_> {
220+
return crate::render::targets::surface::TextureView::from_platform(
219221
self.inner.view_ref(),
220222
);
221223
}
@@ -318,8 +320,10 @@ impl DepthTexture {
318320
}
319321

320322
/// Borrow a texture view reference for use in render pass attachments.
321-
pub(crate) fn view_ref(&self) -> crate::render::surface::TextureView<'_> {
322-
return crate::render::surface::TextureView::from_platform(
323+
pub(crate) fn view_ref(
324+
&self,
325+
) -> crate::render::targets::surface::TextureView<'_> {
326+
return crate::render::targets::surface::TextureView::from_platform(
323327
self.inner.view_ref(),
324328
);
325329
}
@@ -351,8 +355,10 @@ impl Texture {
351355
}
352356

353357
/// Borrow a texture view reference for use in render pass attachments.
354-
pub(crate) fn view_ref(&self) -> crate::render::surface::TextureView<'_> {
355-
return crate::render::surface::TextureView::from_platform(
358+
pub(crate) fn view_ref(
359+
&self,
360+
) -> crate::render::targets::surface::TextureView<'_> {
361+
return crate::render::targets::surface::TextureView::from_platform(
356362
self.inner.view_ref(),
357363
);
358364
}

0 commit comments

Comments
 (0)