From 55d51468261c39e907fe50c877f05436dad2dc59 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Wed, 8 May 2024 03:21:49 +0200 Subject: [PATCH] k it kinda wokrs --- hui-wgpu/shaders/ui.wgsl | 11 +++++++---- hui-wgpu/src/lib.rs | 23 +++++++++++++++-------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/hui-wgpu/shaders/ui.wgsl b/hui-wgpu/shaders/ui.wgsl index 30501ab..4d332f7 100644 --- a/hui-wgpu/shaders/ui.wgsl +++ b/hui-wgpu/shaders/ui.wgsl @@ -1,7 +1,7 @@ struct VertexInput { @location(0) position: vec3, - @location(2) uv: vec2, - @location(3) color: vec4, + @location(1) uv: vec2, + @location(2) color: vec4, } struct VertexOutput { @@ -15,8 +15,9 @@ fn vs_main( in: VertexInput, ) -> VertexOutput { var out: VertexOutput; - out.uv = in.uv; out.clip_position = vec4(in.position, 1.0); + out.uv = in.uv; + out.color = in.color; return out; } @@ -28,5 +29,7 @@ var s_diffuse: sampler; @fragment fn fs_main(in: VertexOutput) -> @location(0) vec4 { - return textureSample(t_diffuse, s_diffuse, in.uv); + //HACK: This is a hack to convert the color to sRGB + let srgb_color = pow(in.color, vec4(2.2, 2.2, 2.2, 1.0)); + return textureSample(t_diffuse, s_diffuse, in.uv) * srgb_color; } diff --git a/hui-wgpu/src/lib.rs b/hui-wgpu/src/lib.rs index df2d380..8f48418 100644 --- a/hui-wgpu/src/lib.rs +++ b/hui-wgpu/src/lib.rs @@ -1,4 +1,4 @@ -use glam::Vec2; +use glam::{vec2, Vec2}; use hui::{draw::{TextureAtlasMeta, UiDrawCall, UiVertex}, UiInstance}; const DEFAULT_BUFFER_SIZE: u64 = 1024; @@ -202,13 +202,16 @@ impl WgpuUiRenderer { .copied() .map(|x| { let mut v = x; - v.position = (v.position / resolution) * 2.0 - 1.0; + v.position = vec2(1., -1.) * ((v.position / resolution) * 2.0 - 1.0); v }) .map(WgpuVertex::from) .collect::>(); let data_idx = &call.indices[..]; + let data_vtx_view = bytemuck::cast_slice(&data_vtx); + let data_idx_view = bytemuck::cast_slice(data_idx); + self.vertex_count = call.vertices.len(); self.index_count = call.indices.len(); @@ -216,26 +219,26 @@ impl WgpuUiRenderer { return } - if data_vtx.len() as u64 > self.vertex_buffer.size() { + if data_vtx_view.len() as u64 > self.vertex_buffer.size() { self.vertex_buffer = device.create_buffer(&wgpu::BufferDescriptor { label: Some("ui_vertex_buffer"), - size: (data_vtx.len() as u64).next_power_of_two(), + size: (data_vtx_view.len() as u64).next_power_of_two(), usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }); } - if data_idx.len() as u64 > self.index_buffer.size() { + if data_idx_view.len() as u64 > self.index_buffer.size() { self.index_buffer = device.create_buffer(&wgpu::BufferDescriptor { label: Some("ui_index_buffer"), - size: (data_idx.len() as u64).next_power_of_two(), + size: (data_idx_view.len() as u64).next_power_of_two(), usage: wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }); } - queue.write_buffer(&self.vertex_buffer, 0, bytemuck::cast_slice(&data_vtx)); - queue.write_buffer(&self.index_buffer, 0, bytemuck::cast_slice(data_idx)); + queue.write_buffer(&self.vertex_buffer, 0, data_vtx_view); + queue.write_buffer(&self.index_buffer, 0, data_idx_view); } fn update_texture(&self, meta: TextureAtlasMeta, queue: &wgpu::Queue) { @@ -289,6 +292,10 @@ impl WgpuUiRenderer { encoder: &mut wgpu::CommandEncoder, surface_view: &wgpu::TextureView, ) { + if self.vertex_count == 0 || self.index_count == 0 { + return + } + let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("ui_render_pass"), color_attachments: &[Some(wgpu::RenderPassColorAttachment {