@@ -8,19 +8,213 @@ use lambda_platform::wgpu as platform;
88use 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// ---------------------------------------------------------------------------
0 commit comments