From 5c568dec8418427b4272f60ff4e30abd1ec880de Mon Sep 17 00:00:00 2001 From: Quaternions Date: Wed, 20 Sep 2023 16:58:56 -0700 Subject: [PATCH] move camera_uniforms and skybox_texture --- src/main.rs | 153 ++++++++++++++++++++++++++-------------------------- 1 file changed, 76 insertions(+), 77 deletions(-) diff --git a/src/main.rs b/src/main.rs index c4170cd..68e330c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -390,11 +390,77 @@ impl strafe_client::framework::Example for GraphicsData { hitbox_halfsize: glam::vec3(1.0,2.5,1.0), }; - let camera_uniforms = camera.to_uniform_data(physics.body.extrapolated_position(0)); - let camera_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { - label: Some("Camera"), - contents: bytemuck::cast_slice(&camera_uniforms), - usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + let device_features = device.features(); + + let skybox_format = if device_features.contains(wgpu::Features::TEXTURE_COMPRESSION_ASTC) { + log::info!("Using ASTC"); + wgpu::TextureFormat::Astc { + block: AstcBlock::B4x4, + channel: AstcChannel::UnormSrgb, + } + } else if device_features.contains(wgpu::Features::TEXTURE_COMPRESSION_ETC2) { + log::info!("Using ETC2"); + wgpu::TextureFormat::Etc2Rgb8UnormSrgb + } else if device_features.contains(wgpu::Features::TEXTURE_COMPRESSION_BC) { + log::info!("Using BC"); + wgpu::TextureFormat::Bc1RgbaUnormSrgb + } else { + log::info!("Using plain"); + wgpu::TextureFormat::Bgra8UnormSrgb + }; + + let size = wgpu::Extent3d { + width: IMAGE_SIZE, + height: IMAGE_SIZE, + depth_or_array_layers: 6, + }; + + let layer_size = wgpu::Extent3d { + depth_or_array_layers: 1, + ..size + }; + let max_mips = layer_size.max_mips(wgpu::TextureDimension::D2); + + log::debug!( + "Copying {:?} skybox images of size {}, {}, 6 with {} mips to gpu", + skybox_format, + IMAGE_SIZE, + IMAGE_SIZE, + max_mips, + ); + + let bytes = match skybox_format { + wgpu::TextureFormat::Astc { + block: AstcBlock::B4x4, + channel: AstcChannel::UnormSrgb, + } => &include_bytes!("../images/astc.dds")[..], + wgpu::TextureFormat::Etc2Rgb8UnormSrgb => &include_bytes!("../images/etc2.dds")[..], + wgpu::TextureFormat::Bc1RgbaUnormSrgb => &include_bytes!("../images/bc1.dds")[..], + wgpu::TextureFormat::Bgra8UnormSrgb => &include_bytes!("../images/bgra.dds")[..], + _ => unreachable!(), + }; + + let skybox_image = ddsfile::Dds::read(&mut std::io::Cursor::new(&bytes)).unwrap(); + + let skybox_texture = device.create_texture_with_data( + queue, + &wgpu::TextureDescriptor { + size, + mip_level_count: max_mips, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: skybox_format, + usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + label: Some("Skybox Texture"), + view_formats: &[], + }, + &skybox_image.data, + ); + + let skybox_texture_view = skybox_texture.create_view(&wgpu::TextureViewDescriptor { + label: Some("Skybox Texture View"), + dimension: Some(wgpu::TextureViewDimension::Cube), + ..wgpu::TextureViewDescriptor::default() }); //drain the modeldata vec so entities can be /moved/ to models.entities @@ -504,79 +570,12 @@ impl strafe_client::framework::Example for GraphicsData { multiview: None, }); - let device_features = device.features(); - - let skybox_format = if device_features.contains(wgpu::Features::TEXTURE_COMPRESSION_ASTC) { - log::info!("Using ASTC"); - wgpu::TextureFormat::Astc { - block: AstcBlock::B4x4, - channel: AstcChannel::UnormSrgb, - } - } else if device_features.contains(wgpu::Features::TEXTURE_COMPRESSION_ETC2) { - log::info!("Using ETC2"); - wgpu::TextureFormat::Etc2Rgb8UnormSrgb - } else if device_features.contains(wgpu::Features::TEXTURE_COMPRESSION_BC) { - log::info!("Using BC"); - wgpu::TextureFormat::Bc1RgbaUnormSrgb - } else { - log::info!("Using plain"); - wgpu::TextureFormat::Bgra8UnormSrgb - }; - - let size = wgpu::Extent3d { - width: IMAGE_SIZE, - height: IMAGE_SIZE, - depth_or_array_layers: 6, - }; - - let layer_size = wgpu::Extent3d { - depth_or_array_layers: 1, - ..size - }; - let max_mips = layer_size.max_mips(wgpu::TextureDimension::D2); - - log::debug!( - "Copying {:?} skybox images of size {}, {}, 6 with {} mips to gpu", - skybox_format, - IMAGE_SIZE, - IMAGE_SIZE, - max_mips, - ); - - let bytes = match skybox_format { - wgpu::TextureFormat::Astc { - block: AstcBlock::B4x4, - channel: AstcChannel::UnormSrgb, - } => &include_bytes!("../images/astc.dds")[..], - wgpu::TextureFormat::Etc2Rgb8UnormSrgb => &include_bytes!("../images/etc2.dds")[..], - wgpu::TextureFormat::Bc1RgbaUnormSrgb => &include_bytes!("../images/bc1.dds")[..], - wgpu::TextureFormat::Bgra8UnormSrgb => &include_bytes!("../images/bgra.dds")[..], - _ => unreachable!(), - }; - - let skybox_image = ddsfile::Dds::read(&mut std::io::Cursor::new(&bytes)).unwrap(); - - let skybox_texture = device.create_texture_with_data( - queue, - &wgpu::TextureDescriptor { - size, - mip_level_count: max_mips, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format: skybox_format, - usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, - label: Some("Skybox Texture"), - view_formats: &[], - }, - &skybox_image.data, - ); - - let skybox_texture_view = skybox_texture.create_view(&wgpu::TextureViewDescriptor { - label: Some("Skybox Texture View"), - dimension: Some(wgpu::TextureViewDimension::Cube), - ..wgpu::TextureViewDescriptor::default() + let camera_uniforms = camera.to_uniform_data(physics.body.extrapolated_position(0)); + let camera_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: Some("Camera"), + contents: bytemuck::cast_slice(&camera_uniforms), + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, }); - let camera_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { layout: &camera_bind_group_layout, entries: &[