From ae58706f10ff93c5f2d97cc8428db09f21d4d3eb Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Sun, 16 Jul 2023 02:36:25 +0200 Subject: [PATCH] shader macro code --- kubi/shaders/colored.wgsl | 1 + kubi/src/rendering/shaders.rs | 41 +++++++++++++++++++---------------- 2 files changed, 23 insertions(+), 19 deletions(-) create mode 100644 kubi/shaders/colored.wgsl diff --git a/kubi/shaders/colored.wgsl b/kubi/shaders/colored.wgsl new file mode 100644 index 0000000..38d45e2 --- /dev/null +++ b/kubi/shaders/colored.wgsl @@ -0,0 +1 @@ +//TODO migrate diff --git a/kubi/src/rendering/shaders.rs b/kubi/src/rendering/shaders.rs index 02bd72e..abdc47b 100644 --- a/kubi/src/rendering/shaders.rs +++ b/kubi/src/rendering/shaders.rs @@ -4,34 +4,37 @@ use super::Renderer; #[derive(Unique)] pub struct Shaders { - pub world: wgpu::ShaderModule + pub world: wgpu::ShaderModule, + pub colored: wgpu::ShaderModule } -macro_rules! shaders { - {($renderer: expr, $dir: literal), $($name: ident : $path: literal),*} => { - { - use super::Renderer; - let renderer: &Renderer = $renderer; - $( - let $name = { - let _is_string_literal: &str = $path; - let shader_descriptor = wgpu::include_wgsl!(concat!($dir, "/", $path)); - renderer.device.create_shader_module(shader_descriptor) +macro_rules! upload_shaders { + {$container: ident [$renderer: expr, $dir: literal] { $($name: ident : $path: literal $(,)*),* } } => {{ + //ensure types + let _: &super::Renderer = $renderer; + let _: &str = $dir; + $( let _: &str = $path; )* + $container { + $($name: { + let source = include_str!(concat!($dir, "/", $path)).into(); + let descriptor = wgpu::ShaderModuleDescriptor { + label: Some(stringify!($name)), + source: wgpu::ShaderSource::Wgsl(source), }; - )* - Shaders { - $($name,)* - } + $renderer.device.create_shader_module(descriptor) + },)* } - }; + }}; } pub fn compile_shaders( storages: AllStoragesView, ) { let renderer = &storages.borrow::>>().unwrap(); - shaders! { - (renderer, "../../shaders"), - world: "world.wgsl" + let shaders = upload_shaders! { + Shaders [renderer, "../../shaders"] { + world: "world.wgsl", + colored: "colored.wgsl" + } }; }