@@ -15,7 +15,7 @@ use bevy_ecs::prelude::*;
1515use bevy_time:: TimeSender ;
1616use bevy_utils:: Instant ;
1717use std:: sync:: Arc ;
18- use wgpu:: { AdapterInfo , CommandEncoder , Instance , Queue , RequestAdapterOptions } ;
18+ use wgpu:: { Adapter , AdapterInfo , CommandEncoder , Instance , Queue , RequestAdapterOptions } ;
1919
2020/// Updates the [`RenderGraph`] with all of its nodes and then runs it to render the entire frame.
2121pub fn render_system ( world : & mut World ) {
@@ -88,6 +88,11 @@ pub fn render_system(world: &mut World) {
8888#[ derive( Resource , Clone , Deref , DerefMut ) ]
8989pub struct RenderQueue ( pub Arc < Queue > ) ;
9090
91+ /// The handle to the physical device being used for rendering.
92+ /// See [`wgpu::Adapter`] for more info.
93+ #[ derive( Resource , Clone , Debug , Deref , DerefMut ) ]
94+ pub struct RenderAdapter ( pub Arc < Adapter > ) ;
95+
9196/// The GPU instance is used to initialize the [`RenderQueue`] and [`RenderDevice`],
9297/// as well as to create [`WindowSurfaces`](crate::view::window::WindowSurfaces).
9398#[ derive( Resource , Deref , DerefMut ) ]
@@ -97,13 +102,29 @@ pub struct RenderInstance(pub Instance);
97102#[ derive( Resource , Clone , Deref , DerefMut ) ]
98103pub struct RenderAdapterInfo ( pub AdapterInfo ) ;
99104
105+ /// The [`TextureFormat`](wgpu::TextureFormat) used for rendering.
106+ /// Initially it's the first element in `AvailableTextureFormats`.
107+ #[ derive( Resource , Clone , Deref , DerefMut ) ]
108+ pub struct RenderTextureFormat ( pub wgpu:: TextureFormat ) ;
109+
110+ /// The available [`TextureFormat`](wgpu::TextureFormat)s on the [`RenderAdapter`].
111+ /// Will be inserted as a `Resource` after the renderer is initialized.
112+ #[ derive( Resource , Clone , Deref , DerefMut ) ]
113+ pub struct AvailableTextureFormats ( pub Arc < Vec < wgpu:: TextureFormat > > ) ;
114+
100115/// Initializes the renderer by retrieving and preparing the GPU instance, device and queue
101116/// for the specified backend.
102117pub async fn initialize_renderer (
103118 instance : & Instance ,
104119 options : & WgpuSettings ,
105120 request_adapter_options : & RequestAdapterOptions < ' _ > ,
106- ) -> ( RenderDevice , RenderQueue , RenderAdapterInfo ) {
121+ ) -> (
122+ RenderDevice ,
123+ RenderQueue ,
124+ RenderAdapterInfo ,
125+ RenderAdapter ,
126+ AvailableTextureFormats ,
127+ ) {
107128 let adapter = instance
108129 . request_adapter ( request_adapter_options)
109130 . await
@@ -250,12 +271,25 @@ pub async fn initialize_renderer(
250271 )
251272 . await
252273 . unwrap ( ) ;
274+
253275 let device = Arc :: new ( device) ;
254276 let queue = Arc :: new ( queue) ;
277+ let adapter = Arc :: new ( adapter) ;
278+ let mut available_texture_formats = Vec :: new ( ) ;
279+ if let Some ( s) = request_adapter_options. compatible_surface {
280+ available_texture_formats = s. get_supported_formats ( & adapter) ;
281+ if available_texture_formats. is_empty ( ) {
282+ info ! ( "{:?}" , adapter_info) ;
283+ panic ! ( "No supported texture formats found!" ) ;
284+ }
285+ } ;
286+ let available_texture_formats = Arc :: new ( available_texture_formats) ;
255287 (
256288 RenderDevice :: from ( device) ,
257289 RenderQueue ( queue) ,
258290 RenderAdapterInfo ( adapter_info) ,
291+ RenderAdapter ( adapter) ,
292+ AvailableTextureFormats ( available_texture_formats) ,
259293 )
260294}
261295
0 commit comments