From 260f4b4232c75cf52facce03508d9c300181fe49 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Sun, 5 May 2024 02:00:06 +0200 Subject: [PATCH] load block textures --- kubi/src/prefabs.rs | 35 +++-------- kubi/src/prefabs/shaders.rs | 19 ------ kubi/src/prefabs/texture.rs | 118 ++++++++++++++++++++++-------------- 3 files changed, 84 insertions(+), 88 deletions(-) delete mode 100644 kubi/src/prefabs/shaders.rs diff --git a/kubi/src/prefabs.rs b/kubi/src/prefabs.rs index 7b9583e..d7503dc 100644 --- a/kubi/src/prefabs.rs +++ b/kubi/src/prefabs.rs @@ -5,9 +5,7 @@ use kubi_shared::block::BlockTexture; use crate::{filesystem::AssetManager, hui_integration::UiState, rendering::Renderer}; mod texture; -mod shaders; - -//use texture::load_texture2darray_prefab; +use texture::load_texture2darray_prefab; pub trait AssetPaths { fn file_name(self) -> &'static str; @@ -39,19 +37,7 @@ impl AssetPaths for BlockTexture { #[derive(Unique)] #[repr(transparent)] -pub struct BlockTexturesPrefab(pub wgpu::Texture); - -// #[derive(Unique)] -// #[repr(transparent)] -// pub struct ChunkShaderPrefab(pub Program); - -// #[derive(Unique)] -// #[repr(transparent)] -// pub struct ColoredShaderPrefab(pub Program); - -// #[derive(Unique)] -// #[repr(transparent)] -// pub struct Colored2ShaderPrefab(pub Program); +pub struct BlockDiffuseTexture(pub wgpu::Texture); #[derive(Unique)] #[repr(transparent)] @@ -63,15 +49,14 @@ pub fn load_prefabs( mut ui: NonSendSync>, assman: UniqueView ) { - // log::info!("Loading textures..."); - // storages.add_unique_non_send_sync(BlockTexturesPrefab( - // load_texture2darray_prefab::( - // &assman, - // "blocks".into(), - // &renderer.display, - // MipmapsOption::AutoGeneratedMipmaps - // ) - // )); + log::info!("Loading textures..."); + storages.add_unique_non_send_sync(BlockDiffuseTexture( + load_texture2darray_prefab::( + &renderer, + &assman, + "blocks".into(), + ) + )); log::info!("Loading the UI stuff..."); { diff --git a/kubi/src/prefabs/shaders.rs b/kubi/src/prefabs/shaders.rs deleted file mode 100644 index 7496a2f..0000000 --- a/kubi/src/prefabs/shaders.rs +++ /dev/null @@ -1,19 +0,0 @@ -macro_rules! include_shader_prefab { - ($name: literal, $vert: literal, $frag: literal, $facade: expr) => { - { - use ::glium::{Program, program::ProgramCreationInput}; - log::info!("compiling shader {}", $name); - Program::new(&*$facade, ProgramCreationInput::SourceCode { - vertex_shader: include_str!($vert), - fragment_shader: include_str!($frag), - geometry_shader: None, - tessellation_control_shader: None, - tessellation_evaluation_shader: None, - transform_feedback_varyings: None, - outputs_srgb: false, - uses_point_size: false, - }).expect("Failed to compile gpu program") - } - }; -} -pub(crate) use include_shader_prefab; diff --git a/kubi/src/prefabs/texture.rs b/kubi/src/prefabs/texture.rs index c10c71c..caa23dd 100644 --- a/kubi/src/prefabs/texture.rs +++ b/kubi/src/prefabs/texture.rs @@ -1,45 +1,75 @@ -// use strum::IntoEnumIterator; -// use rayon::prelude::*; -// use std::{path::PathBuf, io::BufReader}; -// use crate::filesystem::AssetManager; -// use super::AssetPaths; +use glam::UVec2; +use strum::IntoEnumIterator; +use rayon::prelude::*; +use wgpu::util::{DeviceExt, TextureDataOrder}; +use std::{io::BufReader, path::PathBuf}; +use crate::{filesystem::AssetManager, rendering::Renderer}; +use super::AssetPaths; -// pub fn load_texture2darray_prefab< -// T: AssetPaths + IntoEnumIterator, -// E: Facade -// >( -// assman: &AssetManager, -// directory: PathBuf, -// facade: &E, -// mipmaps: MipmapsOption, -// ) -> SrgbTexture2dArray { -// log::info!("started loading {}", directory.as_os_str().to_str().unwrap()); -// //Load raw images -// let tex_files: Vec<&'static str> = T::iter().map(|x| x.file_name()).collect(); -// let raw_images: Vec> = tex_files.par_iter().map(|&file_name| { -// log::info!("loading texture {}", file_name); -// //Get path to the image and open the file -// let reader = { -// let path = directory.join(file_name); -// BufReader::new(assman.open_asset(&path).expect("Failed to open texture file")) -// }; -// //Parse image data -// let (image_data, dimensions) = { -// let image = image::load( -// reader, -// image::ImageFormat::Png -// ).unwrap().to_rgba8(); -// let dimensions = image.dimensions(); -// (image.into_raw(), dimensions) -// }; -// //Create a glium RawImage -// RawImage2d::from_raw_rgba_reversed( -// &image_data, -// dimensions -// ) -// }).collect(); -// log::info!("done loading texture files, uploading to the gpu"); -// //Upload images to the GPU -// SrgbTexture2dArray::with_mipmaps(facade, raw_images, mipmaps) -// .expect("Failed to upload texture array to GPU") -// } +pub fn load_texture2darray_prefab( + renderer: &Renderer, + assman: &AssetManager, + directory: PathBuf, +) -> wgpu::Texture { + log::info!("started loading {}", directory.as_os_str().to_str().unwrap()); + + //Load raw images + let tex_files: Vec<&'static str> = T::iter().map(|x| x.file_name()).collect(); + let raw_images: Vec<(Vec, UVec2)> = tex_files.par_iter().map(|&file_name| { + log::info!("loading texture {}", file_name); + + //Get path to the image and open the file + let reader = { + let path = directory.join(file_name); + BufReader::new(assman.open_asset(&path).expect("Failed to open texture file")) + }; + + //Parse image data + let (image_data, dimensions) = { + let image = image::load( + reader, + image::ImageFormat::Png + ).unwrap().to_rgba8(); + let dimensions = image.dimensions(); + (image.into_raw(), dimensions) + }; + (image_data, UVec2::from(dimensions)) + }).collect(); + + assert!(!raw_images.is_empty(), "no images loaded"); + //TODO: check same size + + log::info!("done loading texture files, uploading to the gpu"); + + let size = raw_images[0].1; + let layers = raw_images.len() as u32; + + //Concat data into a single vec + let mut data = Vec::with_capacity((size.x * size.y * layers * 4) as usize); + for (layer_data, _) in raw_images { + data.extend_from_slice(&layer_data); + } + + //Upload images to the GPU + let desc = &wgpu::TextureDescriptor { + label: Some("block_diffuse_texture"), + size: wgpu::Extent3d { + width: size.x, + height: size.y, + depth_or_array_layers: layers, + }, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Rgba8UnormSrgb, + usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + mip_level_count: 1, + sample_count: 1, + view_formats: &[], + }; + + renderer.device().create_texture_with_data( + renderer.queue(), + desc, + TextureDataOrder::MipMajor, + &data + ) +}