From 4fe9443feb00e14646b68879df9feed888f30f1d Mon Sep 17 00:00:00 2001 From: Robin KAY Date: Tue, 23 Aug 2022 22:16:59 +0100 Subject: [PATCH] Add feature to control 16 byte alignment of uniforms. --- Cargo.toml | 3 ++- src/outline.wgsl | 6 ++++++ src/pipeline.rs | 9 +++++++-- src/uniforms.rs | 2 +- src/view_uniforms.rs | 2 +- 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 011609d..c45bf5c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,8 @@ bevy = { version = "0.8", default-features = false, features = [ ] } [features] -default = ["bevy_ui"] +default = ["align16", "bevy_ui"] +align16 = [] bevy_ui = ["bevy/bevy_ui", "bevy/bevy_sprite", "bevy/bevy_text"] [[example]] diff --git a/src/outline.wgsl b/src/outline.wgsl index b6710a2..9f3d54b 100644 --- a/src/outline.wgsl +++ b/src/outline.wgsl @@ -6,17 +6,23 @@ struct VertexInput { }; struct OutlineViewUniform { +#ifdef ALIGN_16 @align(16) +#endif scale: vec2, }; struct OutlineVertexUniform { +#ifdef ALIGN_16 @align(16) +#endif width: f32, }; struct OutlineFragmentUniform { +#ifdef ALIGN_16 @align(16) +#endif colour: vec4, }; diff --git a/src/pipeline.rs b/src/pipeline.rs index ca969f8..2076f46 100644 --- a/src/pipeline.rs +++ b/src/pipeline.rs @@ -146,17 +146,22 @@ impl SpecializedMeshPipeline for OutlinePipeline { ); } } + let shader_defs = if cfg!(feature = "align16") { + vec!["ALIGN_16".to_string()] + } else { + vec![] + }; let buffers = vec![mesh_layout.get_layout(&buffer_attrs)?]; Ok(RenderPipelineDescriptor { vertex: VertexState { shader: shader.clone().typed::(), entry_point: "vertex".into(), - shader_defs: vec![], + shader_defs: shader_defs.clone(), buffers, }, fragment: Some(FragmentState { shader: shader.typed::(), - shader_defs: vec![], + shader_defs, entry_point: "fragment".into(), targets, }), diff --git a/src/uniforms.rs b/src/uniforms.rs index 5bcfe4c..14d57c4 100644 --- a/src/uniforms.rs +++ b/src/uniforms.rs @@ -17,7 +17,7 @@ use crate::{pipeline::OutlinePipeline, Outline}; #[derive(Clone, Component, ShaderType)] pub struct OutlineVertexUniform { - #[align(16)] + #[cfg_attr(feature = "align16", align(16))] pub width: f32, } diff --git a/src/view_uniforms.rs b/src/view_uniforms.rs index 59bce4c..551f206 100644 --- a/src/view_uniforms.rs +++ b/src/view_uniforms.rs @@ -15,7 +15,7 @@ use crate::pipeline::OutlinePipeline; #[derive(Clone, Component, ShaderType)] pub struct OutlineViewUniform { - #[align(16)] + #[cfg_attr(feature = "align16", align(16))] scale: Vec2, }