|
1 | 1 | //! Primitive assembly for the graphics pipeline. |
2 | 2 |
|
3 | | -use gfx_hal::pso; |
| 3 | +pub use gfx_hal::pso::Element as VertexElement; |
| 4 | +use gfx_hal::pso::{ |
| 5 | + self, |
| 6 | + AttributeDesc, |
| 7 | + VertexBufferDesc, |
| 8 | +}; |
| 9 | + |
| 10 | +use super::{ |
| 11 | + buffer::Buffer, |
| 12 | + surface::ColorFormat, |
| 13 | +}; |
| 14 | + |
| 15 | +/// Attributes for a vertex. |
| 16 | +#[derive(Debug, Clone)] |
| 17 | +pub struct VertexAttribute { |
| 18 | + pub location: u32, |
| 19 | + pub offset: u32, |
| 20 | + pub element: VertexElement<ColorFormat>, |
| 21 | +} |
4 | 22 |
|
5 | 23 | /// PrimitiveAssemblerBuilder for preparing PrimitiveAssemblers to use in the |
6 | 24 | /// lambda-platform Rendering pipeline. |
7 | | -pub struct PrimitiveAssemblerBuilder {} |
| 25 | +pub struct PrimitiveAssemblerBuilder { |
| 26 | + buffer_descriptions: Vec<VertexBufferDesc>, |
| 27 | + attribute_descriptions: Vec<AttributeDesc>, |
| 28 | +} |
8 | 29 |
|
9 | 30 | impl PrimitiveAssemblerBuilder { |
10 | 31 | pub fn new() -> Self { |
11 | | - return Self {}; |
| 32 | + return Self { |
| 33 | + buffer_descriptions: Vec::new(), |
| 34 | + attribute_descriptions: Vec::new(), |
| 35 | + }; |
12 | 36 | } |
13 | 37 |
|
14 | 38 | /// Build a primitive assembler given the lambda-platform vertex shader |
15 | | - /// module. |
| 39 | + /// module. Buffers & attributes do not have to be tied to |
16 | 40 | pub fn build<'shader, RenderBackend: gfx_hal::Backend>( |
17 | | - self, |
| 41 | + &'shader mut self, |
18 | 42 | vertex_shader: &'shader super::shader::ShaderModule<RenderBackend>, |
| 43 | + buffers: Option<&Vec<&Buffer<RenderBackend>>>, |
| 44 | + attributes: Option<&[VertexAttribute]>, |
19 | 45 | ) -> PrimitiveAssembler<'shader, RenderBackend> { |
| 46 | + let binding = self.buffer_descriptions.len() as u32; |
| 47 | + |
| 48 | + match (buffers, attributes) { |
| 49 | + (Some(buffers), Some(attributes)) => { |
| 50 | + println!( |
| 51 | + "[DEBUG] Building primitive assembler with buffers and attributes" |
| 52 | + ); |
| 53 | + self.buffer_descriptions = buffers |
| 54 | + .iter() |
| 55 | + .map(|buffer| VertexBufferDesc { |
| 56 | + binding, |
| 57 | + stride: buffer.stride() as u32, |
| 58 | + rate: pso::VertexInputRate::Vertex, |
| 59 | + }) |
| 60 | + .collect(); |
| 61 | + |
| 62 | + self.attribute_descriptions = attributes |
| 63 | + .iter() |
| 64 | + .map(|attribute| { |
| 65 | + return AttributeDesc { |
| 66 | + location: attribute.location, |
| 67 | + binding, |
| 68 | + element: attribute.element, |
| 69 | + }; |
| 70 | + }) |
| 71 | + .collect(); |
| 72 | + } |
| 73 | + _ => {} |
| 74 | + } |
| 75 | + |
20 | 76 | let primitive_assembler = pso::PrimitiveAssemblerDesc::Vertex { |
21 | | - buffers: &[], |
22 | | - attributes: &[], |
| 77 | + buffers: self.buffer_descriptions.as_slice(), |
| 78 | + attributes: self.attribute_descriptions.as_slice(), |
23 | 79 | input_assembler: pso::InputAssemblerDesc::new( |
24 | 80 | pso::Primitive::TriangleList, |
25 | 81 | ), |
@@ -52,7 +108,7 @@ impl<'shader, RenderBackend: gfx_hal::Backend> |
52 | 108 |
|
53 | 109 | /// Internal functions for the primitive assembler. User applications most |
54 | 110 | /// likely should not use these functions directly nor should they need to. |
55 | | -pub mod internal { |
| 111 | +pub(crate) mod internal { |
56 | 112 | #[inline] |
57 | 113 | pub fn into_primitive_assembler<'shader, RenderBackend: gfx_hal::Backend>( |
58 | 114 | primitive_assembler: super::PrimitiveAssembler<'shader, RenderBackend>, |
|
0 commit comments