From ef6e2593139520b392a8feacf34e407bc0cf86c5 Mon Sep 17 00:00:00 2001 From: hodasemi Date: Mon, 13 May 2024 15:03:54 +0200 Subject: [PATCH] Improve vertex usage --- src/elements/button.rs | 24 ++++++++--------- src/guihandler/gui/colorable.rs | 23 +++++++++-------- src/guihandler/gui/displayable.rs | 12 ++++----- src/guihandler/gui/iconizable.rs | 12 ++++----- src/guihandler/gui/textable.rs | 30 +++++++++++++--------- src/guihandler/gui/texturedvertex.rs | 20 +++++++++++---- src/guihandler/guihandler.rs | 14 +++++----- src/guihandler/guishader/rect.vert | 4 +-- src/guihandler/guishader/single_color.vert | 4 +-- src/guihandler/guishader/text.vert | 4 +-- 10 files changed, 83 insertions(+), 64 deletions(-) diff --git a/src/elements/button.rs b/src/elements/button.rs index 08e3d60..2002c26 100644 --- a/src/elements/button.rs +++ b/src/elements/button.rs @@ -598,12 +598,12 @@ impl Button { let top = self.framable.top() as f32; let bottom = self.framable.bottom() as f32; - frame[0].position = ortho * vec4(left - offset, bottom + offset, 0.0, 1.0); - frame[1].position = ortho * vec4(right + offset, bottom + offset, 0.0, 1.0); - frame[2].position = ortho * vec4(right + offset, top - offset, 0.0, 1.0); - frame[3].position = ortho * vec4(right + offset, top - offset, 0.0, 1.0); - frame[4].position = ortho * vec4(left - offset, top - offset, 0.0, 1.0); - frame[5].position = ortho * vec4(left - offset, bottom + offset, 0.0, 1.0); + frame[0].position = (ortho * vec4(left - offset, bottom + offset, 0.0, 1.0)).xy(); + frame[1].position = (ortho * vec4(right + offset, bottom + offset, 0.0, 1.0)).xy(); + frame[2].position = (ortho * vec4(right + offset, top - offset, 0.0, 1.0)).xy(); + frame[3].position = (ortho * vec4(right + offset, top - offset, 0.0, 1.0)).xy(); + frame[4].position = (ortho * vec4(left - offset, top - offset, 0.0, 1.0)).xy(); + frame[5].position = (ortho * vec4(left - offset, bottom + offset, 0.0, 1.0)).xy(); frame[0].texture_coordinates = vec2(0.0, 1.0); frame[1].texture_coordinates = vec2(1.0, 1.0); @@ -622,12 +622,12 @@ impl Button { let top = self.framable.top() as f32; let bottom = self.framable.bottom() as f32; - frame[0].position = ortho * vec4(left - offset, bottom + offset, 0.0, 1.0); - frame[1].position = ortho * vec4(right + offset, bottom + offset, 0.0, 1.0); - frame[2].position = ortho * vec4(right + offset, top - offset, 0.0, 1.0); - frame[3].position = ortho * vec4(right + offset, top - offset, 0.0, 1.0); - frame[4].position = ortho * vec4(left - offset, top - offset, 0.0, 1.0); - frame[5].position = ortho * vec4(left - offset, bottom + offset, 0.0, 1.0); + frame[0].position = (ortho * vec4(left - offset, bottom + offset, 0.0, 1.0)).xy(); + frame[1].position = (ortho * vec4(right + offset, bottom + offset, 0.0, 1.0)).xy(); + frame[2].position = (ortho * vec4(right + offset, top - offset, 0.0, 1.0)).xy(); + frame[3].position = (ortho * vec4(right + offset, top - offset, 0.0, 1.0)).xy(); + frame[4].position = (ortho * vec4(left - offset, top - offset, 0.0, 1.0)).xy(); + frame[5].position = (ortho * vec4(left - offset, bottom + offset, 0.0, 1.0)).xy(); } } diff --git a/src/guihandler/gui/colorable.rs b/src/guihandler/gui/colorable.rs index 505daf8..ad7db1f 100644 --- a/src/guihandler/gui/colorable.rs +++ b/src/guihandler/gui/colorable.rs @@ -5,7 +5,7 @@ use anyhow::Result; use utilities::{impl_reprc, prelude::*}; use vulkan_rs::prelude::*; -use cgmath::{vec4, Vector4}; +use cgmath::vec4; use std::sync::atomic::{AtomicI32, Ordering::SeqCst}; use std::sync::{Arc, RwLock}; @@ -177,12 +177,12 @@ impl Colorable { let top = y_start + height * *self.top_factor.read().unwrap(); let bottom = y_start + height * *self.bottom_factor.read().unwrap(); - frame[0].position = self.framable.ortho() * vec4(left, bottom, 0.0, 1.0); - frame[1].position = self.framable.ortho() * vec4(right, bottom, 0.0, 1.0); - frame[2].position = self.framable.ortho() * vec4(right, top, 0.0, 1.0); - frame[3].position = self.framable.ortho() * vec4(right, top, 0.0, 1.0); - frame[4].position = self.framable.ortho() * vec4(left, top, 0.0, 1.0); - frame[5].position = self.framable.ortho() * vec4(left, bottom, 0.0, 1.0); + frame[0].position = (self.framable.ortho() * vec4(left, bottom, 0.0, 1.0)).xy(); + frame[1].position = (self.framable.ortho() * vec4(right, bottom, 0.0, 1.0)).xy(); + frame[2].position = (self.framable.ortho() * vec4(right, top, 0.0, 1.0)).xy(); + frame[3].position = (self.framable.ortho() * vec4(right, top, 0.0, 1.0)).xy(); + frame[4].position = (self.framable.ortho() * vec4(left, top, 0.0, 1.0)).xy(); + frame[5].position = (self.framable.ortho() * vec4(left, bottom, 0.0, 1.0)).xy(); Ok(()) } @@ -192,7 +192,10 @@ impl_reprc!( #[derive(Debug)] pub struct ColorableVertex { #[assume_reprc] - position: Vector4, + pub position: cgmath::Vector2, + + #[assume_reprc] + filler: [f32; 2], } ); @@ -200,7 +203,7 @@ impl VertexInputDescription for ColorableVertex { fn bindings() -> Vec { vec![VkVertexInputBindingDescription { binding: 0, - stride: std::mem::size_of::>() as u32, + stride: std::mem::size_of::() as u32, inputRate: VK_VERTEX_INPUT_RATE_VERTEX, }] } @@ -209,7 +212,7 @@ impl VertexInputDescription for ColorableVertex { vec![VkVertexInputAttributeDescription { location: 0, binding: 0, - format: VK_FORMAT_R32G32B32A32_SFLOAT, + format: VK_FORMAT_R32G32_SFLOAT, offset: 0, }] } diff --git a/src/guihandler/gui/displayable.rs b/src/guihandler/gui/displayable.rs index c44d301..62d8656 100644 --- a/src/guihandler/gui/displayable.rs +++ b/src/guihandler/gui/displayable.rs @@ -286,12 +286,12 @@ impl Displayable { let top = y_start + height * *self.top_factor.read().unwrap(); let bottom = y_start + height * *self.bottom_factor.read().unwrap(); - frame[0].position = self.framable.ortho() * vec4(left, bottom, 0.0, 1.0); - frame[1].position = self.framable.ortho() * vec4(right, bottom, 0.0, 1.0); - frame[2].position = self.framable.ortho() * vec4(right, top, 0.0, 1.0); - frame[3].position = self.framable.ortho() * vec4(right, top, 0.0, 1.0); - frame[4].position = self.framable.ortho() * vec4(left, top, 0.0, 1.0); - frame[5].position = self.framable.ortho() * vec4(left, bottom, 0.0, 1.0); + frame[0].position = (self.framable.ortho() * vec4(left, bottom, 0.0, 1.0)).xy(); + frame[1].position = (self.framable.ortho() * vec4(right, bottom, 0.0, 1.0)).xy(); + frame[2].position = (self.framable.ortho() * vec4(right, top, 0.0, 1.0)).xy(); + frame[3].position = (self.framable.ortho() * vec4(right, top, 0.0, 1.0)).xy(); + frame[4].position = (self.framable.ortho() * vec4(left, top, 0.0, 1.0)).xy(); + frame[5].position = (self.framable.ortho() * vec4(left, bottom, 0.0, 1.0)).xy(); frame[0].texture_coordinates = vec2( *self.left_uv_factor.read().unwrap(), diff --git a/src/guihandler/gui/iconizable.rs b/src/guihandler/gui/iconizable.rs index 5a5779b..acf695c 100644 --- a/src/guihandler/gui/iconizable.rs +++ b/src/guihandler/gui/iconizable.rs @@ -248,12 +248,12 @@ impl Iconizable { let abs_top = top + margin as f32; let abs_bottom = bottom - margin as f32; - frame[0].position = ortho * cgmath::Vector4::new(abs_left, abs_bottom, 0.0, 1.0); - frame[1].position = ortho * cgmath::Vector4::new(abs_right, abs_bottom, 0.0, 1.0); - frame[2].position = ortho * cgmath::Vector4::new(abs_right, abs_top, 0.0, 1.0); - frame[3].position = ortho * cgmath::Vector4::new(abs_right, abs_top, 0.0, 1.0); - frame[4].position = ortho * cgmath::Vector4::new(abs_left, abs_top, 0.0, 1.0); - frame[5].position = ortho * cgmath::Vector4::new(abs_left, abs_bottom, 0.0, 1.0); + frame[0].position = (ortho * cgmath::Vector4::new(abs_left, abs_bottom, 0.0, 1.0)).xy(); + frame[1].position = (ortho * cgmath::Vector4::new(abs_right, abs_bottom, 0.0, 1.0)).xy(); + frame[2].position = (ortho * cgmath::Vector4::new(abs_right, abs_top, 0.0, 1.0)).xy(); + frame[3].position = (ortho * cgmath::Vector4::new(abs_right, abs_top, 0.0, 1.0)).xy(); + frame[4].position = (ortho * cgmath::Vector4::new(abs_left, abs_top, 0.0, 1.0)).xy(); + frame[5].position = (ortho * cgmath::Vector4::new(abs_left, abs_bottom, 0.0, 1.0)).xy(); frame[0].texture_coordinates = cgmath::Vector2::new(0.0, 1.0); frame[1].texture_coordinates = cgmath::Vector2::new(1.0, 1.0); diff --git a/src/guihandler/gui/textable.rs b/src/guihandler/gui/textable.rs index 69c293e..44f6b88 100644 --- a/src/guihandler/gui/textable.rs +++ b/src/guihandler/gui/textable.rs @@ -380,13 +380,14 @@ impl Textable { let x = (mod_number - (y as u32 * letters_in_row)) as f32; buffer_mapping[i] = TexturedVertex { - position: ortho + position: (ortho * cgmath::Vector4::new( win_x + offset, win_y + letter_height, priority, 1.0, - ), + )) + .xy(), texture_coordinates: cgmath::Vector2::new( inverse_row * x, inverse_col + inverse_col * y, @@ -396,13 +397,14 @@ impl Textable { i += 1; buffer_mapping[i] = TexturedVertex { - position: ortho + position: (ortho * cgmath::Vector4::new( win_x + offset + letter_width, win_y + letter_height, priority, 1.0, - ), + )) + .xy(), texture_coordinates: cgmath::Vector2::new( inverse_row + inverse_row * x, inverse_col + inverse_col * y, @@ -412,13 +414,14 @@ impl Textable { i += 1; buffer_mapping[i] = TexturedVertex { - position: ortho + position: (ortho * cgmath::Vector4::new( win_x + offset + letter_width, win_y, priority, 1.0, - ), + )) + .xy(), texture_coordinates: cgmath::Vector2::new( inverse_row + inverse_row * x, inverse_col * y, @@ -428,13 +431,14 @@ impl Textable { i += 1; buffer_mapping[i] = TexturedVertex { - position: ortho + position: (ortho * cgmath::Vector4::new( win_x + offset + letter_width, win_y, priority, 1.0, - ), + )) + .xy(), texture_coordinates: cgmath::Vector2::new( inverse_row + inverse_row * x, inverse_col * y, @@ -444,8 +448,9 @@ impl Textable { i += 1; buffer_mapping[i] = TexturedVertex { - position: ortho - * cgmath::Vector4::new(win_x + offset, win_y, priority, 1.0), + position: (ortho + * cgmath::Vector4::new(win_x + offset, win_y, priority, 1.0)) + .xy(), texture_coordinates: cgmath::Vector2::new( inverse_row * x, inverse_col * y, @@ -455,13 +460,14 @@ impl Textable { i += 1; buffer_mapping[i] = TexturedVertex { - position: ortho + position: (ortho * cgmath::Vector4::new( win_x + offset, win_y + letter_height, priority, 1.0, - ), + )) + .xy(), texture_coordinates: cgmath::Vector2::new( inverse_row * x, inverse_col + inverse_col * y, diff --git a/src/guihandler/gui/texturedvertex.rs b/src/guihandler/gui/texturedvertex.rs index 3b452bc..e35db2d 100644 --- a/src/guihandler/gui/texturedvertex.rs +++ b/src/guihandler/gui/texturedvertex.rs @@ -6,12 +6,22 @@ use std::mem; impl_reprc!( pub struct TexturedVertex { #[assume_reprc] - position: cgmath::Vector4, + pub position: cgmath::Vector2, + #[assume_reprc] - texture_coordinates: cgmath::Vector2, + pub texture_coordinates: cgmath::Vector2, } ); +impl TexturedVertex { + pub fn new(position: cgmath::Vector2, texture_coordinates: cgmath::Vector2) -> Self { + Self { + position, + texture_coordinates, + } + } +} + impl VertexInputDescription for TexturedVertex { fn bindings() -> Vec { vec![VkVertexInputBindingDescription { @@ -26,14 +36,14 @@ impl VertexInputDescription for TexturedVertex { VkVertexInputAttributeDescription { location: 0, binding: 0, - format: VK_FORMAT_R32G32B32A32_SFLOAT, + format: VK_FORMAT_R32G32_SFLOAT, offset: 0, }, VkVertexInputAttributeDescription { location: 1, binding: 0, format: VK_FORMAT_R32G32_SFLOAT, - offset: 16, // mem::size_of::>() as u32 + offset: 8, // mem::size_of::>() as u32 }, ] } @@ -42,7 +52,7 @@ impl VertexInputDescription for TexturedVertex { impl Default for TexturedVertex { fn default() -> TexturedVertex { TexturedVertex { - position: cgmath::Vector4::zero(), + position: cgmath::Vector2::zero(), texture_coordinates: cgmath::Vector2::zero(), } } diff --git a/src/guihandler/guihandler.rs b/src/guihandler/guihandler.rs index 241fcc0..c99ebad 100644 --- a/src/guihandler/guihandler.rs +++ b/src/guihandler/guihandler.rs @@ -12,7 +12,7 @@ use super::{ }, }; -use cgmath::{ortho, vec2, vec4}; +use cgmath::{ortho, vec2}; use std::sync::Weak; use std::sync::{ @@ -129,27 +129,27 @@ impl TextToScreen { let edges = [ TexturedVertex { - position: vec4(-1.0, -1.0, 0.0, 1.0), + position: vec2(-1.0, -1.0), texture_coordinates: vec2(0.0, 0.0), }, TexturedVertex { - position: vec4(-1.0, 1.0, 0.0, 1.0), + position: vec2(-1.0, 1.0), texture_coordinates: vec2(0.0, 1.0), }, TexturedVertex { - position: vec4(1.0, 1.0, 0.0, 1.0), + position: vec2(1.0, 1.0), texture_coordinates: vec2(1.0, 1.0), }, TexturedVertex { - position: vec4(1.0, 1.0, 0.0, 1.0), + position: vec2(1.0, 1.0), texture_coordinates: vec2(1.0, 1.0), }, TexturedVertex { - position: vec4(1.0, -1.0, 0.0, 1.0), + position: vec2(1.0, -1.0), texture_coordinates: vec2(1.0, 0.0), }, TexturedVertex { - position: vec4(-1.0, -1.0, 0.0, 1.0), + position: vec2(-1.0, -1.0), texture_coordinates: vec2(0.0, 0.0), }, ]; diff --git a/src/guihandler/guishader/rect.vert b/src/guihandler/guishader/rect.vert index 0b55291..16152f5 100644 --- a/src/guihandler/guishader/rect.vert +++ b/src/guihandler/guishader/rect.vert @@ -1,6 +1,6 @@ #version 450 -layout (location = 0) in vec4 position; +layout (location = 0) in vec2 position; layout (location = 1) in vec2 texcoord; layout (location = 0) out vec2 uv; @@ -8,5 +8,5 @@ layout (location = 0) out vec2 uv; void main() { uv = texcoord; - gl_Position = position; + gl_Position = vec4(position, 0.0, 1.0); } \ No newline at end of file diff --git a/src/guihandler/guishader/single_color.vert b/src/guihandler/guishader/single_color.vert index 126ef86..b765038 100644 --- a/src/guihandler/guishader/single_color.vert +++ b/src/guihandler/guishader/single_color.vert @@ -1,8 +1,8 @@ #version 450 -layout (location = 0) in vec4 position; +layout (location = 0) in vec2 position; void main() { - gl_Position = position; + gl_Position = vec4(position, 0.0, 1.0); } \ No newline at end of file diff --git a/src/guihandler/guishader/text.vert b/src/guihandler/guishader/text.vert index 0b55291..16152f5 100644 --- a/src/guihandler/guishader/text.vert +++ b/src/guihandler/guishader/text.vert @@ -1,6 +1,6 @@ #version 450 -layout (location = 0) in vec4 position; +layout (location = 0) in vec2 position; layout (location = 1) in vec2 texcoord; layout (location = 0) out vec2 uv; @@ -8,5 +8,5 @@ layout (location = 0) out vec2 uv; void main() { uv = texcoord; - gl_Position = position; + gl_Position = vec4(position, 0.0, 1.0); } \ No newline at end of file