diff --git a/src/controller.rs b/src/controller.rs index 2e230e9..595fe23 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -15,6 +15,7 @@ where { pipelines: Vec>, mouse_pos: [f32; 2], + color: [f32; 3], } impl Controller<'_, I, W, O> @@ -39,6 +40,7 @@ where Controller { pipelines: pipelines_vec, mouse_pos: [0.5, 0.5], + color: [1.0, 1.0, 1.0], } } @@ -62,10 +64,17 @@ where } for input_key in &input_keys { match input_key { - Key::MouseMove{x,y} => self.mouse_pos = [ - (x/1280.0 * 2.0 - 1.0) as f32, - (y/720.0 * 2.0 - 1.0) as f32, - ], + Key::MouseMove{x,y} => { + self.mouse_pos = [ + (x/1280.0 * 2.0 - 1.0) as f32, + (y/720.0 * 2.0 - 1.0) as f32, + ]; + self.color = [ + (x/1280.0) as f32, + (y/720.0) as f32, + (((x + y)/2.0)/((1280.0 + 720.0)/2.0)) as f32, + ]; + }, Key::Close => return, _ => (), }; @@ -83,14 +92,18 @@ where points: [self.mouse_pos, [-0.5, 0.5], [-0.5, -0.5]], }; - debug!("Triangle : {:#?}", triangle); + let colors = [ + [self.color[0], self.color[1], self.color[2]], + [self.color[2], self.color[0], self.color[1]], + [self.color[1], self.color[2], self.color[0]], + ]; for (renderer, output) in &mut pipeline.renderers { // match renderer.draw_clear_frame(output, self.color) { // Err(err) => warn!("{}", err), // _ => (), // } - match renderer.draw_triangle_frame(output, triangle) { + match renderer.draw_triangle_frame(output, triangle, colors) { Err(err) => warn!("{}", err), _ => (), }; diff --git a/src/renderer.rs b/src/renderer.rs index 74fd363..32a9b4b 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -182,7 +182,10 @@ where Ok(()) } - pub fn draw_triangle_frame(&mut self, output: &RefCell, triangle: Triangle) + pub fn draw_triangle_frame(&mut self, + output: &RefCell, + triangle: Triangle, + colors: [[f32; 3]; 3]) -> Result<(), &'static str> where W: raw_window_handle::HasRawWindowHandle, @@ -223,6 +226,8 @@ where trace!("Uploading triangle data..."); let points = triangle.points_flat(); self.pipelines[0].write_vertex_buffer(&self.gpu, 0, (&points).to_vec())?; //TODO meh + let colors_flat = colors.iter().flatten().copied().collect(); + self.pipelines[0].write_vertex_buffer(&self.gpu, 1, colors_flat)?; trace!("Recording CommandBuffer..."); unsafe { @@ -237,7 +242,7 @@ where frame.command_buffer.begin_primary(CommandBufferFlags::ONE_TIME_SUBMIT); const TRIANGLE_CLEAR: ClearValue = - ClearValue {color : ClearColor{float32 : [0.1, 0.2, 0.3, 1.0]}}; + ClearValue {color : ClearColor{float32 : [0.5, 0.5, 0.5, 1.0]}}; frame.command_buffer.begin_render_pass( &swap_system.render_pass, diff --git a/src/renderer/pipeline.rs b/src/renderer/pipeline.rs index 2940a2c..03844c7 100644 --- a/src/renderer/pipeline.rs +++ b/src/renderer/pipeline.rs @@ -16,27 +16,31 @@ mod attachement; use self::attachement::Attachement; const VERTEX_SOURCE: &str = -"#version 440 core +"#version 450 core layout (location = 0) in vec2 position; +layout (location = 1) in vec3 color; -out gl_PerVertex { +layout (location = 0) out gl_PerVertex { vec4 gl_Position; }; +layout (location = 1) out vec3 frag_color; void main() { gl_Position = vec4(position, 0.0, 1.0); + frag_color = color; }"; const FRAGMENT_SOURCE: &str = -"#version 440 core +"#version 450 core layout (location = 0) out vec4 frag_color; +layout (location = 1) in vec3 color; void main() { - frag_color = vec4(0.5, 0.5, 0.5, 0.5); + frag_color = vec4(color, 1); }"; //--Pipeline implementation------------------------------------------------------------------------- @@ -137,19 +141,34 @@ where trace!("Creating PrimitiveAssembler..."); let buffers: Vec = - vec![VertexBufferDesc { + vec![ + VertexBufferDesc { binding: 0, stride: (size_of::()*2) as u32, rate: VertexInputRate::Vertex, + }, + VertexBufferDesc { + binding: 1, + stride: (size_of::()*3) as u32, + rate: VertexInputRate::Vertex, }]; let attributes: Vec = - vec![AttributeDesc { + vec![ + AttributeDesc { location: 0, binding: 0, element: Element { format: Format::Rgb32Sfloat, offset: 0, }, + }, + AttributeDesc { + location: 1, + binding: 1, + element: Element { + format: Format::Rgb32Sfloat, + offset: 0, + }, }]; let input_assembler = InputAssemblerDesc { primitive: Primitive::TriangleList, //TODO switch to strips @@ -276,7 +295,10 @@ where .destroy_shader_module(fragment_shader_module); }; - let vertex_buffers = vec![Attachement::new(gpu)?]; + let vertex_buffers = vec![ + Attachement::new(gpu, (size_of::()*2*3) as u64)?, + Attachement::new(gpu, (size_of::()*3*3) as u64)?, + ]; Ok( Pipeline { set_layout, diff --git a/src/renderer/pipeline/attachement.rs b/src/renderer/pipeline/attachement.rs index 6786990..9af3950 100644 --- a/src/renderer/pipeline/attachement.rs +++ b/src/renderer/pipeline/attachement.rs @@ -31,9 +31,7 @@ where } } - pub fn new(gpu: &mut Gpu) -> Result, &'static str> { - use std::mem::size_of; - + pub fn new(gpu: &mut Gpu, size: u64) -> Result, &'static str> { use gfx_hal::{ device::Device, adapter::PhysicalDevice, @@ -43,7 +41,7 @@ where debug!("Creating attachement..."); let mut buffer = unsafe { gpu.device() - .create_buffer((size_of::()*2*3) as u64, Usage::VERTEX) + .create_buffer(size, Usage::VERTEX) .map_err(|_| "Could not create buffer")? }; @@ -75,7 +73,7 @@ where .allocate_memory(memory_type, requirements.size) .map_err(|_| "Could not allocate buffer memory...")? }, - requirements.size, + size, )}; trace!("Binding memory to buffer..."); @@ -111,12 +109,15 @@ where .map_memory(&self.memory, Segment::ALL) .map_err(|_| "Could not map buffer memory")?; - //debug!("before : {}", std::ptr::read(mapped_memory as *mut f32)); + //debug!("data : {:?}", data); + //debug!("before : {:?}", std::slice::from_raw_parts(mapped_memory as *mut f32, + // (self.size/4) as usize)); std::ptr::copy_nonoverlapping(data.as_ptr() as *const u8, mapped_memory, self.size as usize); - //debug!("after : {}", std::ptr::read(mapped_memory as *mut f32)); + //debug!("after : {:?}", std::slice::from_raw_parts(mapped_memory as *mut f32, + // (self.size/4) as usize)); //manual deref for ManuallyDrop to not cause troubles let memory_ref: &B::Memory = &self.memory;