From 41a37735d13e02938655cdcb25a5949cc9b161ec Mon Sep 17 00:00:00 2001 From: Rua Date: Fri, 30 Dec 2022 10:18:25 +0100 Subject: [PATCH] Automatically derive `Pod` and `Zeroable` for structs generated by vulkano-shaders (#2117) --- .../deferred/frame/ambient_lighting_system.rs | 5 --- .../frame/directional_lighting_system.rs | 5 --- .../deferred/frame/point_lighting_system.rs | 5 --- .../fractal_compute_pipeline.rs | 5 --- .../multi_window_game_of_life/game_of_life.rs | 5 --- examples/src/bin/push-constants.rs | 5 --- examples/src/bin/shader-types-sharing.rs | 5 --- examples/src/bin/simple-particles.rs | 5 --- examples/src/bin/teapot/main.rs | 5 --- vulkano-shaders/src/lib.rs | 35 ++++--------------- vulkano-shaders/src/structs.rs | 8 ----- 11 files changed, 7 insertions(+), 81 deletions(-) diff --git a/examples/src/bin/deferred/frame/ambient_lighting_system.rs b/examples/src/bin/deferred/frame/ambient_lighting_system.rs index bdba3d3a..a295e74b 100644 --- a/examples/src/bin/deferred/frame/ambient_lighting_system.rs +++ b/examples/src/bin/deferred/frame/ambient_lighting_system.rs @@ -210,10 +210,5 @@ void main() { f_color.rgb = push_constants.color.rgb * in_diffuse; f_color.a = 1.0; }", - types_meta: { - use bytemuck::{Pod, Zeroable}; - - #[derive(Clone, Copy, Zeroable, Pod)] - }, } } diff --git a/examples/src/bin/deferred/frame/directional_lighting_system.rs b/examples/src/bin/deferred/frame/directional_lighting_system.rs index 4b2cbb48..be99351a 100644 --- a/examples/src/bin/deferred/frame/directional_lighting_system.rs +++ b/examples/src/bin/deferred/frame/directional_lighting_system.rs @@ -235,10 +235,5 @@ void main() { f_color.rgb = light_percent * push_constants.color.rgb * in_diffuse; f_color.a = 1.0; }", - types_meta: { - use bytemuck::{Pod, Zeroable}; - - #[derive(Clone, Copy, Zeroable, Pod)] - }, } } diff --git a/examples/src/bin/deferred/frame/point_lighting_system.rs b/examples/src/bin/deferred/frame/point_lighting_system.rs index 538c2a80..1a81fdd1 100644 --- a/examples/src/bin/deferred/frame/point_lighting_system.rs +++ b/examples/src/bin/deferred/frame/point_lighting_system.rs @@ -267,10 +267,5 @@ void main() { f_color.rgb = push_constants.color.rgb * light_percent * in_diffuse; f_color.a = 1.0; }", - types_meta: { - use bytemuck::{Pod, Zeroable}; - - #[derive(Clone, Copy, Zeroable, Pod)] - }, } } diff --git a/examples/src/bin/interactive_fractal/fractal_compute_pipeline.rs b/examples/src/bin/interactive_fractal/fractal_compute_pipeline.rs index cb525c49..6a379416 100644 --- a/examples/src/bin/interactive_fractal/fractal_compute_pipeline.rs +++ b/examples/src/bin/interactive_fractal/fractal_compute_pipeline.rs @@ -257,10 +257,5 @@ void main() { ); imageStore(img, ivec2(gl_GlobalInvocationID.xy), write_color); }", - types_meta: { - use bytemuck::{Pod, Zeroable}; - - #[derive(Clone, Copy, Zeroable, Pod)] - }, } } diff --git a/examples/src/bin/multi_window_game_of_life/game_of_life.rs b/examples/src/bin/multi_window_game_of_life/game_of_life.rs index 1023a72f..58eccbba 100644 --- a/examples/src/bin/multi_window_game_of_life/game_of_life.rs +++ b/examples/src/bin/multi_window_game_of_life/game_of_life.rs @@ -265,10 +265,5 @@ void main() { compute_color(); } }", - types_meta: { - use bytemuck::{Pod, Zeroable}; - - #[derive(Clone, Copy, Zeroable, Pod)] - }, } } diff --git a/examples/src/bin/push-constants.rs b/examples/src/bin/push-constants.rs index 5684b95a..2f254f9a 100644 --- a/examples/src/bin/push-constants.rs +++ b/examples/src/bin/push-constants.rs @@ -113,11 +113,6 @@ fn main() { } } ", - types_meta: { - use bytemuck::{Pod, Zeroable}; - - #[derive(Clone, Copy, Zeroable, Pod)] - }, } } diff --git a/examples/src/bin/shader-types-sharing.rs b/examples/src/bin/shader-types-sharing.rs index f32f3ef7..d5844e7f 100644 --- a/examples/src/bin/shader-types-sharing.rs +++ b/examples/src/bin/shader-types-sharing.rs @@ -177,11 +177,6 @@ fn main() { " } }, - types_meta: { - use bytemuck::{Pod, Zeroable}; - - #[derive(Clone, Copy, Zeroable, Pod)] - }, } // The macro will create the following things in this module: diff --git a/examples/src/bin/simple-particles.rs b/examples/src/bin/simple-particles.rs index 7f19e8e0..8e0cb869 100644 --- a/examples/src/bin/simple-particles.rs +++ b/examples/src/bin/simple-particles.rs @@ -262,11 +262,6 @@ fn main() { verticies[index].vel = vel * exp(friction * push.delta_time); } ", - types_meta: { - use bytemuck::{Pod, Zeroable}; - - #[derive(Clone, Copy, Zeroable, Pod)] - }, } } // Vertex shader determines color and is run once per particle. diff --git a/examples/src/bin/teapot/main.rs b/examples/src/bin/teapot/main.rs index 5e15d102..0ec16d47 100644 --- a/examples/src/bin/teapot/main.rs +++ b/examples/src/bin/teapot/main.rs @@ -456,11 +456,6 @@ mod vs { vulkano_shaders::shader! { ty: "vertex", path: "src/bin/teapot/vert.glsl", - types_meta: { - use bytemuck::{Pod, Zeroable}; - - #[derive(Clone, Copy, Zeroable, Pod)] - }, } } diff --git a/vulkano-shaders/src/lib.rs b/vulkano-shaders/src/lib.rs index c8611b40..e7653ceb 100644 --- a/vulkano-shaders/src/lib.rs +++ b/vulkano-shaders/src/lib.rs @@ -419,8 +419,6 @@ enum SourceKind { struct TypesMeta { custom_derives: Vec, - clone: bool, - copy: bool, display: bool, debug: bool, default: bool, @@ -433,9 +431,12 @@ impl Default for TypesMeta { #[inline] fn default() -> Self { Self { - custom_derives: vec![], - clone: true, - copy: true, + custom_derives: vec![ + parse_quote! { Clone }, + parse_quote! { Copy }, + parse_quote! { bytemuck::Pod }, + parse_quote! { bytemuck::Zeroable }, + ], partial_eq: false, debug: false, display: false, @@ -451,8 +452,6 @@ impl TypesMeta { fn empty() -> Self { Self { custom_derives: Vec::new(), - clone: false, - copy: false, partial_eq: false, debug: false, display: false, @@ -791,26 +790,6 @@ impl Parse for MacroInput { path.get_ident() { match derive_ident.to_string().as_str() { - "Clone" => { - if meta.default { - return Err(in_brackets - .error("Duplicate Clone derive")); - } - - meta.clone = true; - - false - } - "Copy" => { - if meta.copy { - return Err(in_brackets - .error("Duplicate Copy derive")); - } - - meta.copy = true; - - false - } "PartialEq" => { if meta.partial_eq { return Err(in_brackets @@ -861,7 +840,7 @@ impl Parse for MacroInput { if meta .custom_derives .iter() - .any(|candidate| candidate.eq(&path)) + .any(|candidate| *candidate == path) { return Err( in_braces.error("Duplicate derive declaration") diff --git a/vulkano-shaders/src/structs.rs b/vulkano-shaders/src/structs.rs index 4410c2e6..debd3e9a 100644 --- a/vulkano-shaders/src/structs.rs +++ b/vulkano-shaders/src/structs.rs @@ -220,14 +220,6 @@ fn register_struct( fn write_derives(types_meta: &TypesMeta) -> TokenStream { let mut derives = vec![]; - if types_meta.clone { - derives.push(quote! { Clone }); - } - - if types_meta.copy { - derives.push(quote! { Copy }); - } - derives.extend( types_meta .custom_derives