k it kinda wokrs

This commit is contained in:
griffi-gh 2024-05-08 03:21:49 +02:00
parent 485f43eefa
commit 55d5146826
2 changed files with 22 additions and 12 deletions

View file

@ -1,7 +1,7 @@
struct VertexInput { struct VertexInput {
@location(0) position: vec3<f32>, @location(0) position: vec3<f32>,
@location(2) uv: vec2<f32>, @location(1) uv: vec2<f32>,
@location(3) color: vec4<f32>, @location(2) color: vec4<f32>,
} }
struct VertexOutput { struct VertexOutput {
@ -15,8 +15,9 @@ fn vs_main(
in: VertexInput, in: VertexInput,
) -> VertexOutput { ) -> VertexOutput {
var out: VertexOutput; var out: VertexOutput;
out.uv = in.uv;
out.clip_position = vec4<f32>(in.position, 1.0); out.clip_position = vec4<f32>(in.position, 1.0);
out.uv = in.uv;
out.color = in.color;
return out; return out;
} }
@ -28,5 +29,7 @@ var s_diffuse: sampler;
@fragment @fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> { fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
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<f32>(2.2, 2.2, 2.2, 1.0));
return textureSample(t_diffuse, s_diffuse, in.uv) * srgb_color;
} }

View file

@ -1,4 +1,4 @@
use glam::Vec2; use glam::{vec2, Vec2};
use hui::{draw::{TextureAtlasMeta, UiDrawCall, UiVertex}, UiInstance}; use hui::{draw::{TextureAtlasMeta, UiDrawCall, UiVertex}, UiInstance};
const DEFAULT_BUFFER_SIZE: u64 = 1024; const DEFAULT_BUFFER_SIZE: u64 = 1024;
@ -202,13 +202,16 @@ impl WgpuUiRenderer {
.copied() .copied()
.map(|x| { .map(|x| {
let mut v = 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 v
}) })
.map(WgpuVertex::from) .map(WgpuVertex::from)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let data_idx = &call.indices[..]; 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.vertex_count = call.vertices.len();
self.index_count = call.indices.len(); self.index_count = call.indices.len();
@ -216,26 +219,26 @@ impl WgpuUiRenderer {
return 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 { self.vertex_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("ui_vertex_buffer"), 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, usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false, 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 { self.index_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("ui_index_buffer"), 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, usage: wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false, mapped_at_creation: false,
}); });
} }
queue.write_buffer(&self.vertex_buffer, 0, bytemuck::cast_slice(&data_vtx)); queue.write_buffer(&self.vertex_buffer, 0, data_vtx_view);
queue.write_buffer(&self.index_buffer, 0, bytemuck::cast_slice(data_idx)); queue.write_buffer(&self.index_buffer, 0, data_idx_view);
} }
fn update_texture(&self, meta: TextureAtlasMeta, queue: &wgpu::Queue) { fn update_texture(&self, meta: TextureAtlasMeta, queue: &wgpu::Queue) {
@ -289,6 +292,10 @@ impl WgpuUiRenderer {
encoder: &mut wgpu::CommandEncoder, encoder: &mut wgpu::CommandEncoder,
surface_view: &wgpu::TextureView, surface_view: &wgpu::TextureView,
) { ) {
if self.vertex_count == 0 || self.index_count == 0 {
return
}
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("ui_render_pass"), label: Some("ui_render_pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment { color_attachments: &[Some(wgpu::RenderPassColorAttachment {