shader macro code

This commit is contained in:
griffi-gh 2023-07-16 02:36:25 +02:00
parent 88a767368b
commit ae58706f10
2 changed files with 23 additions and 19 deletions

View file

@ -0,0 +1 @@
//TODO migrate

View file

@ -4,34 +4,37 @@ use super::Renderer;
#[derive(Unique)] #[derive(Unique)]
pub struct Shaders { pub struct Shaders {
pub world: wgpu::ShaderModule pub world: wgpu::ShaderModule,
pub colored: wgpu::ShaderModule
} }
macro_rules! shaders { macro_rules! upload_shaders {
{($renderer: expr, $dir: literal), $($name: ident : $path: literal),*} => { {$container: ident [$renderer: expr, $dir: literal] { $($name: ident : $path: literal $(,)*),* } } => {{
{ //ensure types
use super::Renderer; let _: &super::Renderer = $renderer;
let renderer: &Renderer = $renderer; let _: &str = $dir;
$( $( let _: &str = $path; )*
let $name = { $container {
let _is_string_literal: &str = $path; $($name: {
let shader_descriptor = wgpu::include_wgsl!(concat!($dir, "/", $path)); let source = include_str!(concat!($dir, "/", $path)).into();
renderer.device.create_shader_module(shader_descriptor) let descriptor = wgpu::ShaderModuleDescriptor {
label: Some(stringify!($name)),
source: wgpu::ShaderSource::Wgsl(source),
}; };
)* $renderer.device.create_shader_module(descriptor)
Shaders { },)*
$($name,)*
}
} }
}; }};
} }
pub fn compile_shaders( pub fn compile_shaders(
storages: AllStoragesView, storages: AllStoragesView,
) { ) {
let renderer = &storages.borrow::<NonSendSync<UniqueView<Renderer>>>().unwrap(); let renderer = &storages.borrow::<NonSendSync<UniqueView<Renderer>>>().unwrap();
shaders! { let shaders = upload_shaders! {
(renderer, "../../shaders"), Shaders [renderer, "../../shaders"] {
world: "world.wgsl" world: "world.wgsl",
colored: "colored.wgsl"
}
}; };
} }