This commit is contained in:
iris committed 2025-08-05 14:18:37 -04:00
commit eba8481bde
20 files changed
+3814

No files matched your search

+74
View File
@@ -0,0 +1,74 @@
use wgpu::{RenderPass, VertexAttribute};
use crate::render::primitive::RoundedRect;
pub struct RoundedRectBuffer {
buffer: wgpu::Buffer,
len: usize,
}
#[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable, Default)]
pub struct WindowUniform {
pub width: f32,
pub height: f32,
}
impl RoundedRectBuffer {
pub fn new(device: &wgpu::Device) -> Self {
Self {
buffer: Self::init_buf(device, 0),
len: 0,
}
}
pub fn update(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
rects: &[RoundedRect],
) {
if self.len != rects.len() {
self.len = rects.len();
self.buffer = Self::init_buf(device, std::mem::size_of_val(rects));
}
queue.write_buffer(&self.buffer, 0, bytemuck::cast_slice(rects));
}
fn init_buf(device: &wgpu::Device, size: usize) -> wgpu::Buffer {
device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Instance Buffer"),
size: size as u64,
mapped_at_creation: false,
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
})
}
pub fn len(&self) -> usize {
self.len
}
pub fn set_in<'a>(&'a self, pass: &mut RenderPass<'a>) {
pass.set_vertex_buffer(0, self.buffer.slice(..));
}
}
impl RoundedRect {
const ATTRIBS: [VertexAttribute; 11] = wgpu::vertex_attr_array![
0 => Float32x2,
1 => Float32x2,
2 => Float32x2,
3 => Float32x2,
4 => Float32x4,
5 => Float32x4,
6 => Float32x4,
7 => Float32x4,
8 => Float32,
9 => Float32,
10 => Float32,
];
pub fn desc() -> wgpu::VertexBufferLayout<'static> {
wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<RoundedRect>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Instance,
attributes: &Self::ATTRIBS,
}
}
}
+106
View File
@@ -0,0 +1,106 @@
use wgpu::{
util::{BufferInitDescriptor, DeviceExt},
BufferUsages,
};
use crate::render::primitive::RoundedRect;
use super::{
data::{RoundedRectBuffer, WindowUniform},
ShapeBuffers, ShapePipeline, SHAPE_SHADER,
};
impl ShapePipeline {
pub fn new(device: &wgpu::Device, config: &wgpu::SurfaceConfiguration) -> Self {
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("UI Shape Shader"),
source: wgpu::ShaderSource::Wgsl(SHAPE_SHADER.into()),
});
let window_uniform = WindowUniform::default();
let window_buffer = device.create_buffer_init(&BufferInitDescriptor {
label: Some("Camera Buffer"),
contents: bytemuck::cast_slice(&[window_uniform]),
usage: BufferUsages::UNIFORM | BufferUsages::COPY_DST,
});
let instance_buffer = RoundedRectBuffer::new(device);
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
}],
label: Some("camera_bind_group_layout"),
});
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: window_buffer.as_entire_binding(),
}],
label: Some("camera_bind_group"),
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("UI Shape Pipeline Layout"),
bind_group_layouts: &[&bind_group_layout],
push_constant_ranges: &[],
});
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("UI Shape Pipeline"),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: Some("vs_main"),
buffers: &[RoundedRect::desc()],
compilation_options: Default::default(),
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: Some("fs_main"),
targets: &[Some(wgpu::ColorTargetState {
format: config.format,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
write_mask: wgpu::ColorWrites::ALL,
})],
compilation_options: Default::default(),
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleStrip,
strip_index_format: None,
front_face: wgpu::FrontFace::Cw,
cull_mode: Some(wgpu::Face::Back),
polygon_mode: wgpu::PolygonMode::Fill,
unclipped_depth: false,
conservative: false,
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview: None,
cache: None,
});
let buffers = ShapeBuffers {
window: window_buffer,
instance: instance_buffer,
};
Self {
bind_group,
pipeline,
buffers,
}
}
}
+44
View File
@@ -0,0 +1,44 @@
use crate::render::primitive::RoundedRect;
use data::{RoundedRectBuffer, WindowUniform};
use wgpu::{BindGroup, Buffer, RenderPass, RenderPipeline};
use winit::dpi::PhysicalSize;
mod data;
mod layout;
pub const SHAPE_SHADER: &str = include_str!("./shader.wgsl");
pub struct ShapeBuffers {
pub window: Buffer,
pub instance: RoundedRectBuffer,
}
pub struct ShapePipeline {
pub bind_group: BindGroup,
pub pipeline: RenderPipeline,
pub buffers: ShapeBuffers,
}
impl ShapePipeline {
pub fn draw<'a>(&'a self, pass: &mut RenderPass<'a>) {
pass.set_pipeline(&self.pipeline);
pass.set_bind_group(0, &self.bind_group, &[]);
if self.buffers.instance.len() != 0 {
self.buffers.instance.set_in(pass);
pass.draw(0..4, 0..self.buffers.instance.len() as u32);
}
}
pub fn update(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, rects: &[RoundedRect]) {
self.buffers.instance.update(device, queue, rects);
}
pub fn resize(&mut self, size: &PhysicalSize<u32>, queue: &wgpu::Queue) {
let slice = &[WindowUniform {
width: size.width as f32,
height: size.height as f32,
}];
queue.write_buffer(&self.buffers.window, 0, bytemuck::cast_slice(slice));
}
}
+97
View File
@@ -0,0 +1,97 @@
// vertex shader
struct VertexOutput {
@location(0) color: vec4<f32>,
@location(1) center: vec2<f32>,
@location(2) corner: vec2<f32>,
@location(3) radius: f32,
@location(4) inner_radius: f32,
@location(5) thickness: f32,
@builtin(position) clip_position: vec4<f32>,
};
struct WindowUniform {
dim: vec2<f32>,
};
struct InstanceInput {
@location(0) top_left_anchor: vec2<f32>,
@location(1) top_left_offset: vec2<f32>,
@location(2) bottom_right_anchor: vec2<f32>,
@location(3) bottom_right_offset: vec2<f32>,
@location(4) top_right_color: vec4<f32>,
@location(5) top_left_color: vec4<f32>,
@location(6) bottom_right_color: vec4<f32>,
@location(7) bottom_left_color: vec4<f32>,
@location(8) radius: f32,
@location(9) inner_radius: f32,
@location(10) thickness: f32,
}
@group(0) @binding(0)
var<uniform> window: WindowUniform;
@vertex
fn vs_main(
@builtin(vertex_index) vi: u32,
in: InstanceInput,
) -> VertexOutput {
var out: VertexOutput;
let top_left = in.top_left_anchor * window.dim + in.top_left_offset;
let bottom_right = in.bottom_right_anchor * window.dim + in.bottom_right_offset;
let size = bottom_right - top_left;
var pos = top_left + vec2<f32>(
f32(vi % 2u),
f32(vi / 2u)
) * size;
pos = pos / window.dim * 2.0 - 1.0;
out.clip_position = vec4<f32>(pos.x, -pos.y, 0.0, 1.0);
if vi == 0u {
out.color = in.top_left_color;
} else if vi == 1u {
out.color = in.top_right_color;
} else if vi == 2u {
out.color = in.bottom_left_color;
} else if vi == 3u {
out.color = in.bottom_right_color;
}
out.corner = size / 2.0;
out.center = top_left + out.corner;
out.radius = in.radius;
out.inner_radius = in.inner_radius;
out.thickness = in.thickness;
return out;
}
@fragment
fn fs_main(
in: VertexOutput
) -> @location(0) vec4<f32> {
var color = in.color;
let edge = 0.5;
let dist = distance_from_rect(in.clip_position.xy, in.center, in.corner, in.radius);
color.a *= 1.0 - smoothstep(-min(edge, in.radius), edge, dist);
if in.thickness > 0.0 {
let dist2 = distance_from_rect(in.clip_position.xy, in.center, in.corner - in.thickness, in.inner_radius);
color.a *= smoothstep(-min(edge, in.inner_radius), edge, dist2);
}
return color;
}
fn distance_from_rect(pixel_pos: vec2<f32>, rect_center: vec2<f32>, rect_corner: vec2<f32>, radius: f32) -> f32 {
// vec from center to pixel
let p = pixel_pos - rect_center;
// vec from inner rect corner to pixel
let q = abs(p) - (rect_corner - radius);
return length(max(q, vec2<f32>(0.0, 0.0))) - radius;
}