Creates a new texture data
(
&mut self,
name: impl AsRef<str>,
texture_data: TextureData,
texture_mode: TextureMode,
)
| 261 | |
| 262 | /// Creates a new texture data |
| 263 | pub fn build_texture( |
| 264 | &mut self, |
| 265 | name: impl AsRef<str>, |
| 266 | texture_data: TextureData, |
| 267 | texture_mode: TextureMode, |
| 268 | ) -> Result<Textures, crate::error::Error> { |
| 269 | let mode: wgpu::AddressMode = match texture_mode { |
| 270 | TextureMode::Clamp => wgpu::AddressMode::Repeat, |
| 271 | TextureMode::Repeat => wgpu::AddressMode::MirrorRepeat, |
| 272 | TextureMode::MirrorRepeat => wgpu::AddressMode::ClampToEdge, |
| 273 | }; |
| 274 | |
| 275 | let img = match texture_data { |
| 276 | TextureData::Bytes(data) => image::load_from_memory(data.as_slice())?, |
| 277 | TextureData::Image(data) => data, |
| 278 | TextureData::Path(path) => image::open(path)?, |
| 279 | }; |
| 280 | |
| 281 | fn rgba_to_bgra_pixels_par( |
| 282 | mut buf: image::ImageBuffer<image::Rgba<u8>, Vec<u8>>, |
| 283 | ) -> image::ImageBuffer<image::Rgba<u8>, Vec<u8>> { |
| 284 | buf.enumerate_pixels_mut().for_each(|(_, _, px)| { |
| 285 | let data = px.0; |
| 286 | *px = image::Rgba([data[2], data[1], data[0], data[3]]); |
| 287 | }); |
| 288 | buf |
| 289 | } |
| 290 | |
| 291 | let pixels = match self.config.format { |
| 292 | wgpu::TextureFormat::Bgra8Unorm => rgba_to_bgra_pixels_par(img.to_rgba8()), |
| 293 | wgpu::TextureFormat::Bgra8UnormSrgb => rgba_to_bgra_pixels_par(img.to_rgba8()), |
| 294 | _ => img.to_rgba8(), |
| 295 | }; |
| 296 | let dimensions = img.dimensions(); |
| 297 | |
| 298 | let size = wgpu::Extent3d { |
| 299 | width: dimensions.0, |
| 300 | height: dimensions.1, |
| 301 | depth_or_array_layers: 1, |
| 302 | }; |
| 303 | let texture = self.device.create_texture(&wgpu::TextureDescriptor { |
| 304 | label: Some(name.as_ref()), |
| 305 | size, |
| 306 | mip_level_count: 1, |
| 307 | sample_count: 1, |
| 308 | dimension: wgpu::TextureDimension::D2, |
| 309 | format: self.config.format, |
| 310 | usage: wgpu::TextureUsages::TEXTURE_BINDING |
| 311 | | wgpu::TextureUsages::COPY_DST |
| 312 | | wgpu::TextureUsages::COPY_SRC |
| 313 | | wgpu::TextureUsages::RENDER_ATTACHMENT, |
| 314 | view_formats: &[], |
| 315 | }); |
| 316 | |
| 317 | self.queue.write_texture( |
| 318 | wgpu::TexelCopyTextureInfo { |
| 319 | texture: &texture, |
| 320 | mip_level: 0, |
no outgoing calls
no test coverage detected