From d32278986371181bb5312e7c7d75fb92dc403a41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Fri, 13 Oct 2023 23:07:19 +0200 Subject: [PATCH] Use macros to define shaders --- src/engine.rs | 2 +- src/shaders.rs | 424 +++++++++++++++++++++---------------------------- 2 files changed, 183 insertions(+), 243 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index a1797f53e..a122e3537 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -21,7 +21,7 @@ use std::{ pub type Error = Box; -#[derive(Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)] pub struct ShaderId(pub usize); #[derive(Clone, Copy, PartialEq, Eq, Hash)] diff --git a/src/shaders.rs b/src/shaders.rs index 56febebe3..275bfffcb 100644 --- a/src/shaders.rs +++ b/src/shaders.rs @@ -88,6 +88,7 @@ pub struct FullShaders { pub fn full_shaders(device: &Device, engine: &mut WgpuEngine) -> Result { use crate::wgpu_engine::CpuShaderType; use crate::ANTIALIASING; + use BindType::*; let imports = SHARED_SHADERS .iter() @@ -110,255 +111,194 @@ pub fn full_shaders(device: &Device, engine: &mut WgpuEngine) -> Result = None; + + // Uncomment this to force use of GPU shaders from the specified shader and later even + // if `engine.use_cpu` is specified. + //let force_gpu_from = Some("binning"); + + macro_rules! add_shader { + ($name:ident, $bindings:expr, $defines:expr, $cpu:expr) => {{ + if force_gpu_from == Some(stringify!($name)) { + force_gpu = true; + } + engine.add_shader( + device, + stringify!($name), + preprocess::preprocess(shader!(stringify!($name)), &$defines, &imports).into(), + &$bindings, + if force_gpu { + CpuShaderType::Missing + } else { + $cpu + }, + )? + }}; + ($name:ident, $bindings:expr, $defines:expr) => { + add_shader!( + $name, + $bindings, + &$defines, + CpuShaderType::Present(cpu_shader::$name) + ) + }; + ($name:ident, $bindings:expr) => { + add_shader!($name, $bindings, &full_config) + }; + } + + let pathtag_reduce = add_shader!(pathtag_reduce, [Uniform, BufReadOnly, Buffer]); + let pathtag_reduce2 = add_shader!( + pathtag_reduce2, + [BufReadOnly, Buffer], + &full_config, + CpuShaderType::Ununsed + ); + let pathtag_scan1 = add_shader!( + pathtag_scan1, + [BufReadOnly, BufReadOnly, Buffer], + &full_config, + CpuShaderType::Ununsed + ); + let pathtag_scan = add_shader!( + pathtag_scan, + [Uniform, BufReadOnly, BufReadOnly, Buffer], + &small_config + ); + let pathtag_scan_large = add_shader!( + pathtag_scan, + [Uniform, BufReadOnly, BufReadOnly, Buffer], + &full_config, + CpuShaderType::Ununsed + ); + let bbox_clear = add_shader!(bbox_clear, [Uniform, Buffer], &empty); + let flatten = add_shader!( + flatten, + [Uniform, BufReadOnly, BufReadOnly, Buffer, Buffer, Buffer] + ); + let draw_reduce = add_shader!(draw_reduce, [Uniform, BufReadOnly, Buffer], &empty); + let draw_leaf = add_shader!( + draw_leaf, + [ + Uniform, + BufReadOnly, + BufReadOnly, + BufReadOnly, + Buffer, + Buffer, + Buffer, ], - CpuShaderType::Present(cpu_shader::binning), - )?; - let tile_alloc = engine.add_shader( - device, - "tile_alloc", - preprocess::preprocess(shader!("tile_alloc"), &empty, &imports).into(), - &[ - BindType::Uniform, - BindType::BufReadOnly, - BindType::BufReadOnly, - BindType::Buffer, - BindType::Buffer, - BindType::Buffer, + &empty + ); + let clip_reduce = add_shader!( + clip_reduce, + [BufReadOnly, BufReadOnly, Buffer, Buffer], + &empty + ); + let clip_leaf = add_shader!( + clip_leaf, + [ + Uniform, + BufReadOnly, + BufReadOnly, + BufReadOnly, + BufReadOnly, + Buffer, + Buffer, ], - CpuShaderType::Present(cpu_shader::tile_alloc), - )?; - let path_count_setup = engine.add_shader( - device, - "path_count_setup", - preprocess::preprocess(shader!("path_count_setup"), &empty, &imports).into(), - &[BindType::BufReadOnly, BindType::Buffer], - CpuShaderType::Present(cpu_shader::path_count_setup), - )?; - let path_count = engine.add_shader( - device, - "path_count", - preprocess::preprocess(shader!("path_count"), &full_config, &imports).into(), - &[ - BindType::Uniform, - BindType::Buffer, - BindType::BufReadOnly, - BindType::BufReadOnly, - BindType::Buffer, - BindType::Buffer, + &empty + ); + let binning = add_shader!( + binning, + [ + Uniform, + BufReadOnly, + BufReadOnly, + BufReadOnly, + Buffer, + Buffer, + Buffer, + Buffer, ], - CpuShaderType::Present(cpu_shader::path_count), - )?; - let backdrop = engine.add_shader( - device, - "backdrop_dyn", - preprocess::preprocess(shader!("backdrop_dyn"), &empty, &imports).into(), - &[BindType::Uniform, BindType::BufReadOnly, BindType::Buffer], - CpuShaderType::Present(cpu_shader::backdrop), - )?; - let coarse = engine.add_shader( - device, - "coarse", - preprocess::preprocess(shader!("coarse"), &empty, &imports).into(), - &[ - BindType::Uniform, - BindType::BufReadOnly, - BindType::BufReadOnly, - BindType::BufReadOnly, - BindType::BufReadOnly, - BindType::BufReadOnly, - BindType::Buffer, - BindType::Buffer, - BindType::Buffer, + &empty + ); + let tile_alloc = add_shader!( + tile_alloc, + [Uniform, BufReadOnly, BufReadOnly, Buffer, Buffer, Buffer], + &empty + ); + let path_count_setup = add_shader!(path_count_setup, [BufReadOnly, Buffer], &empty); + let path_count = add_shader!( + path_count, + [Uniform, Buffer, BufReadOnly, BufReadOnly, Buffer, Buffer] + ); + let backdrop = add_shader!( + backdrop_dyn, + [Uniform, BufReadOnly, Buffer], + &empty, + CpuShaderType::Present(cpu_shader::backdrop) + ); + let coarse = add_shader!( + coarse, + [ + Uniform, + BufReadOnly, + BufReadOnly, + BufReadOnly, + BufReadOnly, + BufReadOnly, + Buffer, + Buffer, + Buffer, ], - CpuShaderType::Present(cpu_shader::coarse), - )?; - let path_tiling_setup = engine.add_shader( - device, - "path_tiling_setup", - preprocess::preprocess(shader!("path_tiling_setup"), &empty, &imports).into(), - &[BindType::BufReadOnly, BindType::Buffer], - CpuShaderType::Present(cpu_shader::path_tiling_setup), - )?; - let path_tiling = engine.add_shader( - device, - "path_tiling", - preprocess::preprocess(shader!("path_tiling"), &empty, &imports).into(), - &[ - BindType::BufReadOnly, - BindType::BufReadOnly, - BindType::BufReadOnly, - BindType::BufReadOnly, - BindType::BufReadOnly, - BindType::Buffer, + &empty + ); + let path_tiling_setup = add_shader!(path_tiling_setup, [BufReadOnly, Buffer], &empty); + let path_tiling = add_shader!( + path_tiling, + [ + BufReadOnly, + BufReadOnly, + BufReadOnly, + BufReadOnly, + BufReadOnly, + Buffer, ], - CpuShaderType::Present(cpu_shader::path_tiling), - )?; + &empty + ); let fine = match ANTIALIASING { - crate::AaConfig::Area => engine.add_shader( - device, - "fine", - preprocess::preprocess(shader!("fine"), &full_config, &imports).into(), - &[ - BindType::Uniform, - BindType::BufReadOnly, - BindType::BufReadOnly, - BindType::BufReadOnly, - BindType::Image(ImageFormat::Rgba8), - BindType::ImageRead(ImageFormat::Rgba8), - BindType::ImageRead(ImageFormat::Rgba8), + crate::AaConfig::Area => add_shader!( + fine, + [ + Uniform, + BufReadOnly, + BufReadOnly, + BufReadOnly, + Image(ImageFormat::Rgba8), + ImageRead(ImageFormat::Rgba8), + ImageRead(ImageFormat::Rgba8), ], - CpuShaderType::Missing, - )?, - _ => { - engine.add_shader( - device, - "fine", - preprocess::preprocess(shader!("fine"), &full_config, &imports).into(), - &[ - BindType::Uniform, - BindType::BufReadOnly, - BindType::BufReadOnly, - BindType::BufReadOnly, - BindType::Image(ImageFormat::Rgba8), - BindType::ImageRead(ImageFormat::Rgba8), - BindType::ImageRead(ImageFormat::Rgba8), - BindType::BufReadOnly, // mask buffer - ], - CpuShaderType::Missing, - )? - } + &full_config, + CpuShaderType::Missing + ), + _ => add_shader!( + fine, + [ + Uniform, + BufReadOnly, + BufReadOnly, + BufReadOnly, + Image(ImageFormat::Rgba8), + ImageRead(ImageFormat::Rgba8), + ImageRead(ImageFormat::Rgba8), + BufReadOnly, // mask buffer + ], + &full_config, + CpuShaderType::Missing + ), }; Ok(FullShaders { pathtag_reduce,