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