load block textures

This commit is contained in:
griffi-gh 2024-05-05 02:00:06 +02:00
parent 72d03962f5
commit 33c418ff6f
3 changed files with 84 additions and 88 deletions

View file

@ -5,9 +5,7 @@ use kubi_shared::block::BlockTexture;
use crate::{filesystem::AssetManager, hui_integration::UiState, rendering::Renderer}; use crate::{filesystem::AssetManager, hui_integration::UiState, rendering::Renderer};
mod texture; mod texture;
mod shaders; use texture::load_texture2darray_prefab;
//use texture::load_texture2darray_prefab;
pub trait AssetPaths { pub trait AssetPaths {
fn file_name(self) -> &'static str; fn file_name(self) -> &'static str;
@ -39,19 +37,7 @@ impl AssetPaths for BlockTexture {
#[derive(Unique)] #[derive(Unique)]
#[repr(transparent)] #[repr(transparent)]
pub struct BlockTexturesPrefab(pub wgpu::Texture); pub struct BlockDiffuseTexture(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);
#[derive(Unique)] #[derive(Unique)]
#[repr(transparent)] #[repr(transparent)]
@ -63,15 +49,14 @@ pub fn load_prefabs(
mut ui: NonSendSync<UniqueViewMut<UiState>>, mut ui: NonSendSync<UniqueViewMut<UiState>>,
assman: UniqueView<AssetManager> assman: UniqueView<AssetManager>
) { ) {
// log::info!("Loading textures..."); log::info!("Loading textures...");
// storages.add_unique_non_send_sync(BlockTexturesPrefab( storages.add_unique_non_send_sync(BlockDiffuseTexture(
// load_texture2darray_prefab::<BlockTexture, _>( load_texture2darray_prefab::<BlockTexture>(
// &assman, &renderer,
// "blocks".into(), &assman,
// &renderer.display, "blocks".into(),
// MipmapsOption::AutoGeneratedMipmaps )
// ) ));
// ));
log::info!("Loading the UI stuff..."); log::info!("Loading the UI stuff...");
{ {

View file

@ -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;

View file

@ -1,45 +1,75 @@
// use strum::IntoEnumIterator; use glam::UVec2;
// use rayon::prelude::*; use strum::IntoEnumIterator;
// use std::{path::PathBuf, io::BufReader}; use rayon::prelude::*;
// use crate::filesystem::AssetManager; use wgpu::util::{DeviceExt, TextureDataOrder};
// use super::AssetPaths; use std::{io::BufReader, path::PathBuf};
use crate::{filesystem::AssetManager, rendering::Renderer};
use super::AssetPaths;
// pub fn load_texture2darray_prefab< pub fn load_texture2darray_prefab<T: AssetPaths + IntoEnumIterator>(
// T: AssetPaths + IntoEnumIterator, renderer: &Renderer,
// E: Facade assman: &AssetManager,
// >( directory: PathBuf,
// assman: &AssetManager, ) -> wgpu::Texture {
// directory: PathBuf, log::info!("started loading {}", directory.as_os_str().to_str().unwrap());
// facade: &E,
// mipmaps: MipmapsOption, //Load raw images
// ) -> SrgbTexture2dArray { let tex_files: Vec<&'static str> = T::iter().map(|x| x.file_name()).collect();
// log::info!("started loading {}", directory.as_os_str().to_str().unwrap()); let raw_images: Vec<(Vec<u8>, UVec2)> = tex_files.par_iter().map(|&file_name| {
// //Load raw images log::info!("loading texture {}", file_name);
// let tex_files: Vec<&'static str> = T::iter().map(|x| x.file_name()).collect();
// let raw_images: Vec<RawImage2d<u8>> = tex_files.par_iter().map(|&file_name| { //Get path to the image and open the file
// log::info!("loading texture {}", file_name); let reader = {
// //Get path to the image and open the file let path = directory.join(file_name);
// let reader = { BufReader::new(assman.open_asset(&path).expect("Failed to open texture file"))
// let path = directory.join(file_name); };
// BufReader::new(assman.open_asset(&path).expect("Failed to open texture file"))
// }; //Parse image data
// //Parse image data let (image_data, dimensions) = {
// let (image_data, dimensions) = { let image = image::load(
// let image = image::load( reader,
// reader, image::ImageFormat::Png
// image::ImageFormat::Png ).unwrap().to_rgba8();
// ).unwrap().to_rgba8(); let dimensions = image.dimensions();
// let dimensions = image.dimensions(); (image.into_raw(), dimensions)
// (image.into_raw(), dimensions) };
// }; (image_data, UVec2::from(dimensions))
// //Create a glium RawImage }).collect();
// RawImage2d::from_raw_rgba_reversed(
// &image_data, assert!(!raw_images.is_empty(), "no images loaded");
// dimensions //TODO: check same size
// )
// }).collect(); log::info!("done loading texture files, uploading to the gpu");
// log::info!("done loading texture files, uploading to the gpu");
// //Upload images to the GPU let size = raw_images[0].1;
// SrgbTexture2dArray::with_mipmaps(facade, raw_images, mipmaps) let layers = raw_images.len() as u32;
// .expect("Failed to upload texture array to GPU")
// } //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
)
}