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)]
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::<NonSendSync<UniqueView<Renderer>>>().unwrap();
shaders! {
(renderer, "../../shaders"),
world: "world.wgsl"
let shaders = upload_shaders! {
Shaders [renderer, "../../shaders"] {
world: "world.wgsl",
colored: "colored.wgsl"
}
};
}