@@ -40,7 +40,8 @@ pub struct GraphicsPlugin;
4040
4141impl Plugin for GraphicsPlugin {
4242 fn build ( & self , app : & mut App ) {
43- app. init_resource :: < RenderLayersManager > ( ) ;
43+ app. init_resource :: < RenderLayersManager > ( )
44+ . add_systems ( PostUpdate , sync_to_surface) ;
4445 }
4546}
4647
@@ -149,14 +150,26 @@ pub fn create(
149150 mut commands : Commands ,
150151 mut layer_manager : ResMut < RenderLayersManager > ,
151152 p_images : Query < & Image , With < Surface > > ,
153+ windows : Query < & Window , With < Surface > > ,
152154 render_device : Res < RenderDevice > ,
153155) -> Result < Entity > {
154156 // find the surface entity, if it is an image, we will render to that image
155157 // otherwise we will render to the window
156- let target = match p_images. get ( surface_entity) {
157- Ok ( p_image) => RenderTarget :: Image ( ImageRenderTarget :: from ( p_image. handle . clone ( ) ) ) ,
158+ let ( target, physical_width, physical_height) = match p_images. get ( surface_entity) {
159+ Ok ( p_image) => (
160+ RenderTarget :: Image ( ImageRenderTarget :: from ( p_image. handle . clone ( ) ) ) ,
161+ p_image. size . width ,
162+ p_image. size . height ,
163+ ) ,
158164 Err ( QueryEntityError :: QueryDoesNotMatch ( ..) ) => {
159- RenderTarget :: Window ( WindowRef :: Entity ( surface_entity) )
165+ let window = windows
166+ . get ( surface_entity)
167+ . map_err ( |_| ProcessingError :: SurfaceNotFound ) ?;
168+ (
169+ RenderTarget :: Window ( WindowRef :: Entity ( surface_entity) ) ,
170+ window. resolution . physical_width ( ) ,
171+ window. resolution . physical_height ( ) ,
172+ )
160173 }
161174 Err ( _) => return Err ( ProcessingError :: SurfaceNotFound ) ,
162175 } ;
@@ -165,14 +178,14 @@ pub fn create(
165178 let render_layer = layer_manager. allocate ( ) ;
166179
167180 let size = Extent3d {
168- width,
169- height,
181+ width : physical_width ,
182+ height : physical_height ,
170183 depth_or_array_layers : 1 ,
171184 } ;
172185 let readback_buffer = create_readback_buffer (
173186 & render_device,
174- width ,
175- height ,
187+ physical_width ,
188+ physical_height ,
176189 texture_format,
177190 "Graphics Readback Buffer" ,
178191 )
@@ -241,6 +254,39 @@ pub fn resize(
241254 }
242255}
243256
257+ pub fn sync_to_surface (
258+ mut graphics_query : Query < ( & mut Graphics , & RenderTarget ) > ,
259+ windows : Query < & Window , ( With < Surface > , Changed < Window > ) > ,
260+ render_device : Res < RenderDevice > ,
261+ ) {
262+ for ( mut graphics, target) in graphics_query. iter_mut ( ) {
263+ let RenderTarget :: Window ( WindowRef :: Entity ( surface_entity) ) = * target else {
264+ continue ;
265+ } ;
266+ let Ok ( window) = windows. get ( surface_entity) else {
267+ continue ;
268+ } ;
269+ let physical_w = window. resolution . physical_width ( ) ;
270+ let physical_h = window. resolution . physical_height ( ) ;
271+ if graphics. size . width == physical_w && graphics. size . height == physical_h {
272+ continue ;
273+ }
274+ graphics. size = Extent3d {
275+ width : physical_w,
276+ height : physical_h,
277+ depth_or_array_layers : 1 ,
278+ } ;
279+ graphics. readback_buffer = create_readback_buffer (
280+ & render_device,
281+ physical_w,
282+ physical_h,
283+ graphics. texture_format ,
284+ "Graphics Readback Buffer" ,
285+ )
286+ . expect ( "Failed to reallocate readback buffer" ) ;
287+ }
288+ }
289+
244290pub fn mode_3d (
245291 In ( entity) : In < Entity > ,
246292 mut projections : Query < & mut Projection > ,
@@ -396,7 +442,7 @@ pub fn begin_draw(In(entity): In<Entity>, mut state_query: Query<&mut RenderStat
396442 let mut state = state_query
397443 . get_mut ( entity)
398444 . map_err ( |_| ProcessingError :: GraphicsNotFound ) ?;
399- state. reset ( ) ;
445+ state. begin_frame ( ) ;
400446 Ok ( ( ) )
401447}
402448
0 commit comments