@@ -38,6 +38,130 @@ impl BevyDefault for TextureFormat {
3838 }
3939}
4040
41+ /// Trait used to provide texture srgb view formats with static lifetime for `TextureDescriptor.view_formats`.
42+ pub trait TextureSrgbViewFormats {
43+ /// Returns the srgb view formats for a type.
44+ fn srgb_view_formats ( & self ) -> & ' static [ TextureFormat ] ;
45+ }
46+
47+ impl TextureSrgbViewFormats for TextureFormat {
48+ /// Returns the srgb view formats if the format has an srgb variant, otherwise returns an empty slice.
49+ ///
50+ /// The return result covers all the results of [`TextureFormat::add_srgb_suffix`](wgpu_types::TextureFormat::add_srgb_suffix).
51+ fn srgb_view_formats ( & self ) -> & ' static [ TextureFormat ] {
52+ match self {
53+ TextureFormat :: Rgba8Unorm => & [ TextureFormat :: Rgba8UnormSrgb ] ,
54+ TextureFormat :: Bgra8Unorm => & [ TextureFormat :: Bgra8UnormSrgb ] ,
55+ TextureFormat :: Bc1RgbaUnorm => & [ TextureFormat :: Bc1RgbaUnormSrgb ] ,
56+ TextureFormat :: Bc2RgbaUnorm => & [ TextureFormat :: Bc2RgbaUnormSrgb ] ,
57+ TextureFormat :: Bc3RgbaUnorm => & [ TextureFormat :: Bc3RgbaUnormSrgb ] ,
58+ TextureFormat :: Bc7RgbaUnorm => & [ TextureFormat :: Bc7RgbaUnormSrgb ] ,
59+ TextureFormat :: Etc2Rgb8Unorm => & [ TextureFormat :: Etc2Rgb8UnormSrgb ] ,
60+ TextureFormat :: Etc2Rgb8A1Unorm => & [ TextureFormat :: Etc2Rgb8A1UnormSrgb ] ,
61+ TextureFormat :: Etc2Rgba8Unorm => & [ TextureFormat :: Etc2Rgba8UnormSrgb ] ,
62+ TextureFormat :: Astc {
63+ block : wgpu_types:: AstcBlock :: B4x4 ,
64+ channel : wgpu_types:: AstcChannel :: Unorm ,
65+ } => & [ TextureFormat :: Astc {
66+ block : wgpu_types:: AstcBlock :: B4x4 ,
67+ channel : wgpu_types:: AstcChannel :: UnormSrgb ,
68+ } ] ,
69+ TextureFormat :: Astc {
70+ block : wgpu_types:: AstcBlock :: B5x4 ,
71+ channel : wgpu_types:: AstcChannel :: Unorm ,
72+ } => & [ TextureFormat :: Astc {
73+ block : wgpu_types:: AstcBlock :: B5x4 ,
74+ channel : wgpu_types:: AstcChannel :: UnormSrgb ,
75+ } ] ,
76+ TextureFormat :: Astc {
77+ block : wgpu_types:: AstcBlock :: B5x5 ,
78+ channel : wgpu_types:: AstcChannel :: Unorm ,
79+ } => & [ TextureFormat :: Astc {
80+ block : wgpu_types:: AstcBlock :: B5x5 ,
81+ channel : wgpu_types:: AstcChannel :: UnormSrgb ,
82+ } ] ,
83+ TextureFormat :: Astc {
84+ block : wgpu_types:: AstcBlock :: B6x5 ,
85+ channel : wgpu_types:: AstcChannel :: Unorm ,
86+ } => & [ TextureFormat :: Astc {
87+ block : wgpu_types:: AstcBlock :: B6x5 ,
88+ channel : wgpu_types:: AstcChannel :: UnormSrgb ,
89+ } ] ,
90+ TextureFormat :: Astc {
91+ block : wgpu_types:: AstcBlock :: B6x6 ,
92+ channel : wgpu_types:: AstcChannel :: Unorm ,
93+ } => & [ TextureFormat :: Astc {
94+ block : wgpu_types:: AstcBlock :: B6x6 ,
95+ channel : wgpu_types:: AstcChannel :: UnormSrgb ,
96+ } ] ,
97+ TextureFormat :: Astc {
98+ block : wgpu_types:: AstcBlock :: B8x5 ,
99+ channel : wgpu_types:: AstcChannel :: Unorm ,
100+ } => & [ TextureFormat :: Astc {
101+ block : wgpu_types:: AstcBlock :: B8x5 ,
102+ channel : wgpu_types:: AstcChannel :: UnormSrgb ,
103+ } ] ,
104+ TextureFormat :: Astc {
105+ block : wgpu_types:: AstcBlock :: B8x6 ,
106+ channel : wgpu_types:: AstcChannel :: Unorm ,
107+ } => & [ TextureFormat :: Astc {
108+ block : wgpu_types:: AstcBlock :: B8x6 ,
109+ channel : wgpu_types:: AstcChannel :: UnormSrgb ,
110+ } ] ,
111+ TextureFormat :: Astc {
112+ block : wgpu_types:: AstcBlock :: B8x8 ,
113+ channel : wgpu_types:: AstcChannel :: Unorm ,
114+ } => & [ TextureFormat :: Astc {
115+ block : wgpu_types:: AstcBlock :: B8x8 ,
116+ channel : wgpu_types:: AstcChannel :: UnormSrgb ,
117+ } ] ,
118+ TextureFormat :: Astc {
119+ block : wgpu_types:: AstcBlock :: B10x5 ,
120+ channel : wgpu_types:: AstcChannel :: Unorm ,
121+ } => & [ TextureFormat :: Astc {
122+ block : wgpu_types:: AstcBlock :: B10x5 ,
123+ channel : wgpu_types:: AstcChannel :: UnormSrgb ,
124+ } ] ,
125+ TextureFormat :: Astc {
126+ block : wgpu_types:: AstcBlock :: B10x6 ,
127+ channel : wgpu_types:: AstcChannel :: Unorm ,
128+ } => & [ TextureFormat :: Astc {
129+ block : wgpu_types:: AstcBlock :: B10x6 ,
130+ channel : wgpu_types:: AstcChannel :: UnormSrgb ,
131+ } ] ,
132+ TextureFormat :: Astc {
133+ block : wgpu_types:: AstcBlock :: B10x8 ,
134+ channel : wgpu_types:: AstcChannel :: Unorm ,
135+ } => & [ TextureFormat :: Astc {
136+ block : wgpu_types:: AstcBlock :: B10x8 ,
137+ channel : wgpu_types:: AstcChannel :: UnormSrgb ,
138+ } ] ,
139+ TextureFormat :: Astc {
140+ block : wgpu_types:: AstcBlock :: B10x10 ,
141+ channel : wgpu_types:: AstcChannel :: Unorm ,
142+ } => & [ TextureFormat :: Astc {
143+ block : wgpu_types:: AstcBlock :: B10x10 ,
144+ channel : wgpu_types:: AstcChannel :: UnormSrgb ,
145+ } ] ,
146+ TextureFormat :: Astc {
147+ block : wgpu_types:: AstcBlock :: B12x10 ,
148+ channel : wgpu_types:: AstcChannel :: Unorm ,
149+ } => & [ TextureFormat :: Astc {
150+ block : wgpu_types:: AstcBlock :: B12x10 ,
151+ channel : wgpu_types:: AstcChannel :: UnormSrgb ,
152+ } ] ,
153+ TextureFormat :: Astc {
154+ block : wgpu_types:: AstcBlock :: B12x12 ,
155+ channel : wgpu_types:: AstcChannel :: Unorm ,
156+ } => & [ TextureFormat :: Astc {
157+ block : wgpu_types:: AstcBlock :: B12x12 ,
158+ channel : wgpu_types:: AstcChannel :: UnormSrgb ,
159+ } ] ,
160+ _ => & [ ] ,
161+ }
162+ }
163+ }
164+
41165/// A handle to a 1 x 1 transparent white image.
42166///
43167/// Like [`Handle<Image>::default`], this is a handle to a fallback image asset.
@@ -1010,7 +1134,12 @@ impl Image {
10101134 ///
10111135 /// [`Camera`]: https://docs.rs/bevy/latest/bevy/render/camera/struct.Camera.html
10121136 /// [`RenderTarget::Image`]: https://docs.rs/bevy/latest/bevy/render/camera/enum.RenderTarget.html#variant.Image
1013- pub fn new_target_texture ( width : u32 , height : u32 , format : TextureFormat ) -> Self {
1137+ pub fn new_target_texture (
1138+ width : u32 ,
1139+ height : u32 ,
1140+ format : TextureFormat ,
1141+ view_format : Option < TextureFormat > ,
1142+ ) -> Self {
10141143 let size = Extent3d {
10151144 width,
10161145 height,
@@ -1039,10 +1168,16 @@ impl Image {
10391168 mip_level_count : 1 ,
10401169 sample_count : 1 ,
10411170 usage,
1042- view_formats : & [ ] ,
1171+ view_formats : match view_format {
1172+ Some ( _) => format. srgb_view_formats ( ) ,
1173+ None => & [ ] ,
1174+ } ,
10431175 } ,
10441176 sampler : ImageSampler :: Default ,
1045- texture_view_descriptor : None ,
1177+ texture_view_descriptor : view_format. map ( |f| TextureViewDescriptor {
1178+ format : Some ( f) ,
1179+ ..Default :: default ( )
1180+ } ) ,
10461181 asset_usage : RenderAssetUsages :: default ( ) ,
10471182 copy_on_resize : true ,
10481183 }
0 commit comments