diff options
author | tcmal <me@aria.rip> | 2024-08-25 17:44:21 +0100 |
---|---|---|
committer | tcmal <me@aria.rip> | 2024-08-25 17:44:21 +0100 |
commit | 939fde5ca405342ab4c52ee3da81fb2daa192e3e (patch) | |
tree | 0920ca9e70760b2cad50707020a9808ddaa0eca4 /stockton-render/src/draw | |
parent | 48ebe87925c4aca8fb9ab8f120b327b677f6812c (diff) |
refactor(draw): use macro for buffer attribute descriptions
Diffstat (limited to 'stockton-render/src/draw')
-rw-r--r-- | stockton-render/src/draw/context.rs | 27 | ||||
-rw-r--r-- | stockton-render/src/draw/macros.rs | 101 | ||||
-rw-r--r-- | stockton-render/src/draw/mod.rs | 2 |
3 files changed, 108 insertions, 22 deletions
diff --git a/stockton-render/src/draw/context.rs b/stockton-render/src/draw/context.rs index 86c2270..789b4ce 100644 --- a/stockton-render/src/draw/context.rs +++ b/stockton-render/src/draw/context.rs @@ -538,28 +538,11 @@ impl<'a> RenderingContext<'a> { rate: VertexInputRate::Vertex, }]; - let attributes: Vec<AttributeDesc> = vec![AttributeDesc { // XYZ Attribute - location: 0, - binding: 0, - element: Element { - format: Format::Rgb32Sfloat, - offset: 0, - }, - }, AttributeDesc { // UV Attribute - location: 1, - binding: 0, - element: Element { - format: Format::Rg32Sfloat, - offset: (size_of::<f32>() * 3) as ElemOffset, - } - }, AttributeDesc { // Tex Attribute - location: 2, - binding: 0, - element: Element { - format: Format::R32Sint, - offset: (size_of::<f32>() * 5) as ElemOffset - } - }]; + let attributes: Vec<AttributeDesc> = pipeline_vb_attributes!(0, + size_of::<f32>() * 3; Rgb32Sfloat, + size_of::<f32>() * 2; Rg32Sfloat, + size_of::<u32>(); R32Sint + ); // Rasterizer let rasterizer = Rasterizer { diff --git a/stockton-render/src/draw/macros.rs b/stockton-render/src/draw/macros.rs new file mode 100644 index 0000000..d191d5b --- /dev/null +++ b/stockton-render/src/draw/macros.rs @@ -0,0 +1,101 @@ +// Copyright (C) Oscar Shrimpton 2019 + +// This program is free software: you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation, either version 3 of the License, or (at your option) +// any later version. + +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +// more details. + +// You should have received a copy of the GNU General Public License along +// with this program. If not, see <http://www.gnu.org/licenses/>. +//! Helper macros, mostly for the graphics pipeline definitions + +/// Macro for easily defining buffer attribute descriptions +/// Usage: +/// ``` +/// // 0 is the binding value +/// let attributes: Vec<AttributeDesc> = pipeline_vb_attributes!(0, +/// size_of::<f32>() * 3; Rgb32Sfloat +/// size_of::<f32>() * 2; Rg32Sfloat, +/// size_of::<u32>(); R32Sint +/// ); +/// ``` +/// See the hal::pso::Format enum for possible types +macro_rules! pipeline_vb_attributes { + // Start of recursion + ( $binding:expr, + $firstSize:expr; $firstType:ident, + $( $size:expr; $type:ident ),* + ) => ({ + use hal::pso::{AttributeDesc, Element}; + + let mut vec = Vec::new(); + + vec.push(AttributeDesc { + location: 0, + binding: $binding, + element: Element { + format: Format::$firstType, + offset: 0 + } + }); + + pipeline_vb_attributes!( + vec; $binding; 1; $firstSize, + $($size; $type),* + ); + + vec + }); + + // Special case for single item + ( $binding:expr; $firstSize:expr; $firstType:ident ) => ({ + vec![ + AttributeDesc { + location: 0, + binding: $binding, + element: Element { + format: Format::$firstType, + offset: $firstSize as u32 + } + } + ] + }); + + // Middle of recursion + ( $vec:ident; $binding:expr; $location:expr; $prevSize:expr, + $firstSize:expr; $firstType:ident, + $($size:expr; $type:ident),* ) => ({ + + $vec.push(AttributeDesc { + location: $location, + binding: $binding, + element: Element { + format: Format::$firstType, + offset: $prevSize as u32 + } + }); + + pipeline_vb_attributes!( + $vec; $binding; ($location + 1); ($prevSize + $firstSize), + $($size; $type),* + ); + }); + + // End of recursion + ( $vec:ident; $binding:expr; $location:expr; $prevSize:expr, + $firstSize:expr; $firstType:ident ) => ({ + $vec.push(AttributeDesc { + location: $location, + binding: $binding, + element: Element { + format: Format::$firstType, + offset: $prevSize as u32 + } + }); + }); +}
\ No newline at end of file diff --git a/stockton-render/src/draw/mod.rs b/stockton-render/src/draw/mod.rs index 00b5bb6..2834aa4 100644 --- a/stockton-render/src/draw/mod.rs +++ b/stockton-render/src/draw/mod.rs @@ -15,6 +15,8 @@ //! Given 3D points and some camera information, renders to the screen. +#[macro_use] +mod macros; mod context; mod buffer; mod texture; |