diff options
author | tcmal <me@aria.rip> | 2024-08-25 17:44:23 +0100 |
---|---|---|
committer | tcmal <me@aria.rip> | 2024-08-25 17:44:23 +0100 |
commit | c9fb3ae28fe491bc55243fb80d8c6be93f37ad99 (patch) | |
tree | c8d6519479d3b17f5bd657495c4d7fe2d2830a1a /stockton-render/src/draw/ui | |
parent | c52a05e6d3977efce6bd4479aa312dc90e0452e5 (diff) |
feat(render): ui working
Diffstat (limited to 'stockton-render/src/draw/ui')
-rw-r--r-- | stockton-render/src/draw/ui/data/stockton.frag | 4 | ||||
-rw-r--r-- | stockton-render/src/draw/ui/data/stockton.vert | 23 | ||||
-rw-r--r-- | stockton-render/src/draw/ui/mod.rs | 4 | ||||
-rw-r--r-- | stockton-render/src/draw/ui/pipeline.rs | 10 | ||||
-rw-r--r-- | stockton-render/src/draw/ui/render.rs | 60 | ||||
-rwxr-xr-x | stockton-render/src/draw/ui/texture.rs | 48 |
6 files changed, 100 insertions, 49 deletions
diff --git a/stockton-render/src/draw/ui/data/stockton.frag b/stockton-render/src/draw/ui/data/stockton.frag index 77685cc..c30c99e 100644 --- a/stockton-render/src/draw/ui/data/stockton.frag +++ b/stockton-render/src/draw/ui/data/stockton.frag @@ -6,10 +6,10 @@ layout(set = 0, binding = 0) uniform texture2D tex[8]; layout(set = 0, binding = 1) uniform sampler samp[8]; layout (location = 1) in vec2 frag_uv; -layout (location = 2) in flat uint frag_col; +layout (location = 2) in vec4 frag_col; layout (location = 0) out vec4 color; void main() { - color = texture(sampler2D(tex[0], samp[0]), frag_uv); + color = texture(sampler2D(tex[0], samp[0]), frag_uv) * frag_col; }
\ No newline at end of file diff --git a/stockton-render/src/draw/ui/data/stockton.vert b/stockton-render/src/draw/ui/data/stockton.vert index d84cc56..8912e96 100644 --- a/stockton-render/src/draw/ui/data/stockton.vert +++ b/stockton-render/src/draw/ui/data/stockton.vert @@ -4,23 +4,34 @@ layout (push_constant) uniform PushConsts { vec2 screen_size; } push; -layout(location = 0) in vec2 pos; +layout (location = 0) in vec2 pos; layout (location = 1) in vec2 uv; -layout (location = 2) in uint col; // rgba of u8s +layout (location = 2) in vec4 col; out gl_PerVertex { vec4 gl_Position; }; layout (location = 1) out vec2 frag_uv; -layout (location = 2) out uint frag_col; +layout (location = 2) out vec4 frag_col; + +vec3 linear_from_srgb(vec3 srgb) { + bvec3 cutoff = lessThan(srgb, vec3(10.31475)); + vec3 lower = srgb / vec3(3294.6); + vec3 higher = pow((srgb + vec3(14.025)) / vec3(269.025), vec3(2.4)); + return mix(higher, lower, cutoff); +} + +vec4 linear_from_srgba(vec4 srgba) { + return vec4(linear_from_srgb(srgba.rgb * 255.0), srgba.a); +} void main() { gl_Position = vec4( - ((pos.x / push.screen_size.x) * 2.0) - 1.0, - ((pos.y / push.screen_size.y) * 2.0) - 1.0, + 2.0 * pos.x / push.screen_size.x - 1.0, + 2.0 * pos.y / push.screen_size.y - 1.0, 0.0, 1.0 ); frag_uv = uv; - frag_col = col; + frag_col = linear_from_srgba(col); } diff --git a/stockton-render/src/draw/ui/mod.rs b/stockton-render/src/draw/ui/mod.rs index d50befc..5daa117 100644 --- a/stockton-render/src/draw/ui/mod.rs +++ b/stockton-render/src/draw/ui/mod.rs @@ -1,5 +1,3 @@ -use egui::paint::color::Srgba; - pub mod pipeline; pub mod render; pub mod texture; @@ -10,4 +8,4 @@ use stockton_types::Vector2; pub use texture::{ensure_textures, UiTextures}; #[derive(Debug)] -pub struct UiPoint(pub Vector2, pub Vector2, pub Srgba); +pub struct UiPoint(pub Vector2, pub Vector2, pub [f32; 4]); diff --git a/stockton-render/src/draw/ui/pipeline.rs b/stockton-render/src/draw/ui/pipeline.rs index 2a8b9fc..dfa4e88 100644 --- a/stockton-render/src/draw/ui/pipeline.rs +++ b/stockton-render/src/draw/ui/pipeline.rs @@ -183,8 +183,8 @@ impl UiPipeline { dst: Factor::OneMinusSrcAlpha, }, alpha: BlendOp::Add { - src: Factor::OneMinusSrcAlpha, - dst: Factor::Zero, + src: Factor::SrcAlpha, + dst: Factor::OneMinusSrcAlpha, }, }; @@ -203,7 +203,7 @@ impl UiPipeline { rect: extent.rect(), depth: (0.0..1.0), }), - scissor: Some(extent.rect()), + scissor: None, blend_constants: None, depth_bounds: None, }; @@ -212,7 +212,7 @@ impl UiPipeline { let primitive_assembler = PrimitiveAssemblerDesc::Vertex { buffers: &[VertexBufferDesc { binding: 0, - stride: (size_of::<f32>() * 6) as u32, + stride: (size_of::<f32>() * 8) as u32, rate: VertexInputRate::Vertex, }], attributes: &[ @@ -236,7 +236,7 @@ impl UiPipeline { location: 2, binding: 0, element: Element { - format: Format::R32Uint, + format: Format::Rgba32Sfloat, offset: (size_of::<f32>() * 4) as u32, }, }, diff --git a/stockton-render/src/draw/ui/render.rs b/stockton-render/src/draw/ui/render.rs index 40cc733..a88f811 100644 --- a/stockton-render/src/draw/ui/render.rs +++ b/stockton-render/src/draw/ui/render.rs @@ -1,11 +1,12 @@ use crate::draw::texture::TextureRepo; -use hal::pso::ShaderStageFlags; +use hal::pso::{Rect, ShaderStageFlags}; use super::UiPoint; use crate::draw::draw_buffers::DrawBuffers; use crate::types::*; use crate::UiState; -use anyhow::Result; +use anyhow::{anyhow, Result}; +use egui::{ClippedMesh, TextureId}; use std::{array::IntoIter, convert::TryInto, iter::empty}; use stockton_types::Vector2; @@ -17,19 +18,13 @@ pub fn do_render( ui: &mut UiState, ) -> Result<()> { // TODO: Actual UI Rendering - let (_out, paint) = ui.end_frame(); - let screen = ui.dimensions(); + let (_out, shapes) = ui.end_frame(); + let screen = ui.dimensions().ok_or(anyhow!("UI not set up properly."))?; + let shapes = ui.ctx().tessellate(shapes); - unsafe { - cmd_buffer.push_graphics_constants( - &pipeline_layout, - ShaderStageFlags::VERTEX, - 0, - &[screen.x.to_bits(), screen.y.to_bits()], - ); - } + for ClippedMesh(rect, tris) in shapes.iter() { + assert!(tris.texture_id == TextureId::Egui); - for (_rect, tris) in paint.iter() { // Copy triangles/indicies for i in (0..tris.indices.len()).step_by(3) { draw_buffers.index_buffer[i / 3] = ( @@ -37,18 +32,53 @@ pub fn do_render( tris.indices[i + 1].try_into()?, tris.indices[i + 2].try_into()?, ); + // eprintln!( + // "{} {}", + // tris.vertices[tris.indices[i] as usize].uv.x, + // tris.vertices[tris.indices[i] as usize].uv.y + // ); + // eprintln!( + // "{} {}", + // tris.vertices[tris.indices[i + 1] as usize].uv.x, + // tris.vertices[tris.indices[i + 1] as usize].uv.y + // ); + // eprintln!( + // "{} {}", + // tris.vertices[tris.indices[i + 2] as usize].uv.x, + // tris.vertices[tris.indices[i + 2] as usize].uv.y + // ); } for (i, vertex) in tris.vertices.iter().enumerate() { draw_buffers.vertex_buffer[i] = UiPoint( Vector2::new(vertex.pos.x, vertex.pos.y), Vector2::new(vertex.uv.x, vertex.uv.y), - vertex.color, + [ + vertex.color.r() as f32 / 255.0, + vertex.color.g() as f32 / 255.0, + vertex.color.b() as f32 / 255.0, + vertex.color.a() as f32 / 255.0, + ], ); } - // TODO: *Properly* deal with textures if let Some(ds) = tex_repo.attempt_get_descriptor_set(0) { unsafe { + cmd_buffer.push_graphics_constants( + &pipeline_layout, + ShaderStageFlags::VERTEX, + 0, + &[screen.x.to_bits(), screen.y.to_bits()], + ); + + cmd_buffer.set_scissors( + 0, + IntoIter::new([Rect { + x: rect.min.x as i16, + y: rect.min.y as i16, + w: rect.width() as i16, + h: rect.height() as i16, + }]), + ); cmd_buffer.bind_graphics_descriptor_sets( pipeline_layout, 0, diff --git a/stockton-render/src/draw/ui/texture.rs b/stockton-render/src/draw/ui/texture.rs index f5ddb3e..7cba1ac 100755 --- a/stockton-render/src/draw/ui/texture.rs +++ b/stockton-render/src/draw/ui/texture.rs @@ -1,24 +1,32 @@ -use crate::draw::texture::{LoadableImage, TextureRepo}; +use crate::draw::texture::{resolver::TextureResolver, LoadableImage, TextureRepo}; use crate::UiState; use anyhow::Result; -use egui::Texture; -use stockton_levels::{prelude::HasTextures, traits::textures::Texture as LTexture}; +use egui::{CtxRef, Texture}; +use log::debug; +use std::{convert::TryInto, sync::Arc}; -pub struct UiTextures; - -impl HasTextures for UiTextures { - type TexturesIter<'a> = std::slice::Iter<'a, LTexture>; +pub struct UiTextures { + ctx: CtxRef, +} - fn textures_iter(&self) -> Self::TexturesIter<'_> { - (&[]).iter() +impl TextureResolver for UiTextures { + type Image = Arc<Texture>; + fn resolve(&mut self, tex: u32) -> Option<Self::Image> { + if tex == 0 { + Some(self.ctx.texture()) + } else { + None + } } +} - fn get_texture(&self, _idx: u32) -> Option<&stockton_levels::prelude::textures::Texture> { - None +impl UiTextures { + pub fn new(ctx: CtxRef) -> Self { + UiTextures { ctx } } } -impl LoadableImage for &Texture { +impl LoadableImage for Arc<Texture> { fn width(&self) -> u32 { self.width as u32 } @@ -31,22 +39,26 @@ impl LoadableImage for &Texture { for (i, x) in pixels.iter().enumerate() { unsafe { - *ptr.offset(i as isize * 3) = *x; - *ptr.offset((i as isize * 3) + 1) = *x; - *ptr.offset((i as isize * 3) + 2) = *x; + *ptr.offset(i as isize * 4) = 255; + *ptr.offset((i as isize * 4) + 1) = 255; + *ptr.offset((i as isize * 4) + 2) = 255; + *ptr.offset((i as isize * 4) + 3) = *x; } } } - unsafe fn copy_into(&self, _ptr: *mut u8, _row_size: usize) { - todo!() + unsafe fn copy_into(&self, ptr: *mut u8, row_size: usize) { + for y in 0..self.height() { + self.copy_row(y, ptr.offset((row_size * y as usize).try_into().unwrap())); + } } } pub fn ensure_textures(tex_repo: &mut TextureRepo, ui: &mut UiState) -> Result<()> { - let tex = ui.ctx.texture(); + let tex = ui.ctx().texture(); if tex.version != ui.last_tex_ver { + debug!("Queueing UI Texture reload"); tex_repo.force_queue_load(0)?; ui.last_tex_ver = tex.version; } |