|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use anyhow::Result; |
| 4 | +use wgpu::util::DeviceExt; |
| 5 | + |
| 6 | +use crate::paint::{Color, PaintCommand}; |
| 7 | + |
| 8 | +#[repr(C)] |
| 9 | +#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] |
| 10 | +struct Vertex { |
| 11 | + position: [f32; 2], |
| 12 | + color: [f32; 4], |
| 13 | +} |
| 14 | + |
| 15 | +#[repr(C)] |
| 16 | +#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] |
| 17 | +struct Uniforms { |
| 18 | + viewport_size: [f32; 2], |
| 19 | + _padding: [f32; 2], |
| 20 | +} |
| 21 | + |
| 22 | +pub struct GpuRenderer { |
| 23 | + surface: wgpu::Surface<'static>, |
| 24 | + device: wgpu::Device, |
| 25 | + queue: wgpu::Queue, |
| 26 | + config: wgpu::SurfaceConfiguration, |
| 27 | + pipeline: wgpu::RenderPipeline, |
| 28 | + uniform_buffer: wgpu::Buffer, |
| 29 | + uniform_bind_group: wgpu::BindGroup, |
| 30 | + size: (u32, u32), |
| 31 | +} |
| 32 | + |
| 33 | +impl GpuRenderer { |
| 34 | + pub async fn new(window: Arc<winit::window::Window>) -> Result<Self> { |
| 35 | + let size = window.inner_size(); |
| 36 | + let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor::default()); |
| 37 | + let surface = instance.create_surface(window)?; |
| 38 | + let adapter = instance |
| 39 | + .request_adapter(&wgpu::RequestAdapterOptions { |
| 40 | + compatible_surface: Some(&surface), |
| 41 | + ..Default::default() |
| 42 | + }) |
| 43 | + .await |
| 44 | + .ok_or_else(|| anyhow::anyhow!("no suitable GPU adapter"))?; |
| 45 | + let (device, queue) = adapter |
| 46 | + .request_device(&wgpu::DeviceDescriptor::default(), None) |
| 47 | + .await?; |
| 48 | + |
| 49 | + let surface_caps = surface.get_capabilities(&adapter); |
| 50 | + let surface_format = surface_caps |
| 51 | + .formats |
| 52 | + .iter() |
| 53 | + .find(|f| f.is_srgb()) |
| 54 | + .copied() |
| 55 | + .unwrap_or(surface_caps.formats[0]); |
| 56 | + |
| 57 | + let config = wgpu::SurfaceConfiguration { |
| 58 | + usage: wgpu::TextureUsages::RENDER_ATTACHMENT, |
| 59 | + format: surface_format, |
| 60 | + width: size.width.max(1), |
| 61 | + height: size.height.max(1), |
| 62 | + present_mode: wgpu::PresentMode::AutoVsync, |
| 63 | + desired_maximum_frame_latency: 2, |
| 64 | + alpha_mode: surface_caps.alpha_modes[0], |
| 65 | + view_formats: vec![], |
| 66 | + }; |
| 67 | + surface.configure(&device, &config); |
| 68 | + |
| 69 | + let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { |
| 70 | + label: Some("rect shader"), |
| 71 | + source: wgpu::ShaderSource::Wgsl(include_str!("shaders/rect.wgsl").into()), |
| 72 | + }); |
| 73 | + |
| 74 | + let uniforms = Uniforms { |
| 75 | + viewport_size: [size.width.max(1) as f32, size.height.max(1) as f32], |
| 76 | + _padding: [0.0; 2], |
| 77 | + }; |
| 78 | + let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { |
| 79 | + label: Some("uniform buffer"), |
| 80 | + contents: bytemuck::cast_slice(&[uniforms]), |
| 81 | + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, |
| 82 | + }); |
| 83 | + |
| 84 | + let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { |
| 85 | + label: Some("uniform bind group layout"), |
| 86 | + entries: &[wgpu::BindGroupLayoutEntry { |
| 87 | + binding: 0, |
| 88 | + visibility: wgpu::ShaderStages::VERTEX, |
| 89 | + ty: wgpu::BindingType::Buffer { |
| 90 | + ty: wgpu::BufferBindingType::Uniform, |
| 91 | + has_dynamic_offset: false, |
| 92 | + min_binding_size: None, |
| 93 | + }, |
| 94 | + count: None, |
| 95 | + }], |
| 96 | + }); |
| 97 | + |
| 98 | + let uniform_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { |
| 99 | + label: Some("uniform bind group"), |
| 100 | + layout: &bind_group_layout, |
| 101 | + entries: &[wgpu::BindGroupEntry { |
| 102 | + binding: 0, |
| 103 | + resource: uniform_buffer.as_entire_binding(), |
| 104 | + }], |
| 105 | + }); |
| 106 | + |
| 107 | + let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { |
| 108 | + label: Some("pipeline layout"), |
| 109 | + bind_group_layouts: &[&bind_group_layout], |
| 110 | + push_constant_ranges: &[], |
| 111 | + }); |
| 112 | + |
| 113 | + let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { |
| 114 | + label: Some("rect pipeline"), |
| 115 | + layout: Some(&pipeline_layout), |
| 116 | + vertex: wgpu::VertexState { |
| 117 | + module: &shader, |
| 118 | + entry_point: Some("vs_main"), |
| 119 | + buffers: &[wgpu::VertexBufferLayout { |
| 120 | + array_stride: std::mem::size_of::<Vertex>() as u64, |
| 121 | + step_mode: wgpu::VertexStepMode::Vertex, |
| 122 | + attributes: &[ |
| 123 | + wgpu::VertexAttribute { |
| 124 | + offset: 0, |
| 125 | + shader_location: 0, |
| 126 | + format: wgpu::VertexFormat::Float32x2, |
| 127 | + }, |
| 128 | + wgpu::VertexAttribute { |
| 129 | + offset: 8, |
| 130 | + shader_location: 1, |
| 131 | + format: wgpu::VertexFormat::Float32x4, |
| 132 | + }, |
| 133 | + ], |
| 134 | + }], |
| 135 | + compilation_options: Default::default(), |
| 136 | + }, |
| 137 | + fragment: Some(wgpu::FragmentState { |
| 138 | + module: &shader, |
| 139 | + entry_point: Some("fs_main"), |
| 140 | + targets: &[Some(wgpu::ColorTargetState { |
| 141 | + format: surface_format, |
| 142 | + blend: Some(wgpu::BlendState::ALPHA_BLENDING), |
| 143 | + write_mask: wgpu::ColorWrites::ALL, |
| 144 | + })], |
| 145 | + compilation_options: Default::default(), |
| 146 | + }), |
| 147 | + primitive: wgpu::PrimitiveState { |
| 148 | + topology: wgpu::PrimitiveTopology::TriangleList, |
| 149 | + ..Default::default() |
| 150 | + }, |
| 151 | + depth_stencil: None, |
| 152 | + multisample: wgpu::MultisampleState::default(), |
| 153 | + multiview: None, |
| 154 | + cache: None, |
| 155 | + }); |
| 156 | + |
| 157 | + Ok(Self { |
| 158 | + surface, |
| 159 | + device, |
| 160 | + queue, |
| 161 | + config, |
| 162 | + pipeline, |
| 163 | + uniform_buffer, |
| 164 | + uniform_bind_group, |
| 165 | + size: (size.width.max(1), size.height.max(1)), |
| 166 | + }) |
| 167 | + } |
| 168 | + |
| 169 | + pub fn resize(&mut self, width: u32, height: u32) { |
| 170 | + if width == 0 || height == 0 { |
| 171 | + return; |
| 172 | + } |
| 173 | + self.size = (width, height); |
| 174 | + self.config.width = width; |
| 175 | + self.config.height = height; |
| 176 | + self.surface.configure(&self.device, &self.config); |
| 177 | + |
| 178 | + let uniforms = Uniforms { |
| 179 | + viewport_size: [width as f32, height as f32], |
| 180 | + _padding: [0.0; 2], |
| 181 | + }; |
| 182 | + self.queue |
| 183 | + .write_buffer(&self.uniform_buffer, 0, bytemuck::cast_slice(&[uniforms])); |
| 184 | + } |
| 185 | + |
| 186 | + pub fn render(&mut self, commands: &[PaintCommand]) -> Result<()> { |
| 187 | + let output = self.surface.get_current_texture()?; |
| 188 | + let view = output |
| 189 | + .texture |
| 190 | + .create_view(&wgpu::TextureViewDescriptor::default()); |
| 191 | + |
| 192 | + let mut vertices: Vec<Vertex> = Vec::new(); |
| 193 | + for cmd in commands { |
| 194 | + match cmd { |
| 195 | + PaintCommand::FillRect { |
| 196 | + x, |
| 197 | + y, |
| 198 | + width, |
| 199 | + height, |
| 200 | + color, |
| 201 | + } => { |
| 202 | + push_rect(&mut vertices, *x, *y, *width, *height, color); |
| 203 | + } |
| 204 | + PaintCommand::Text { |
| 205 | + text, |
| 206 | + x, |
| 207 | + y, |
| 208 | + font_size, |
| 209 | + color, |
| 210 | + } => { |
| 211 | + // Placeholder: character rectangles (replaced by glyphon in #76) |
| 212 | + let char_w = font_size * 0.5; |
| 213 | + let char_h = font_size * 0.7; |
| 214 | + let glyph_y = y + (font_size - char_h) * 0.5; |
| 215 | + let mut cx = *x; |
| 216 | + for ch in text.chars() { |
| 217 | + if ch != ' ' { |
| 218 | + let glyph_w = char_w * 0.7; |
| 219 | + push_rect(&mut vertices, cx, glyph_y, glyph_w, char_h, color); |
| 220 | + } |
| 221 | + cx += char_w; |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + let vertex_buffer = self |
| 228 | + .device |
| 229 | + .create_buffer_init(&wgpu::util::BufferInitDescriptor { |
| 230 | + label: Some("vertex buffer"), |
| 231 | + contents: bytemuck::cast_slice(&vertices), |
| 232 | + usage: wgpu::BufferUsages::VERTEX, |
| 233 | + }); |
| 234 | + |
| 235 | + let mut encoder = self |
| 236 | + .device |
| 237 | + .create_command_encoder(&wgpu::CommandEncoderDescriptor { |
| 238 | + label: Some("render encoder"), |
| 239 | + }); |
| 240 | + |
| 241 | + { |
| 242 | + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { |
| 243 | + label: Some("render pass"), |
| 244 | + color_attachments: &[Some(wgpu::RenderPassColorAttachment { |
| 245 | + view: &view, |
| 246 | + resolve_target: None, |
| 247 | + ops: wgpu::Operations { |
| 248 | + load: wgpu::LoadOp::Clear(wgpu::Color::WHITE), |
| 249 | + store: wgpu::StoreOp::Store, |
| 250 | + }, |
| 251 | + })], |
| 252 | + depth_stencil_attachment: None, |
| 253 | + ..Default::default() |
| 254 | + }); |
| 255 | + |
| 256 | + if !vertices.is_empty() { |
| 257 | + pass.set_pipeline(&self.pipeline); |
| 258 | + pass.set_bind_group(0, &self.uniform_bind_group, &[]); |
| 259 | + pass.set_vertex_buffer(0, vertex_buffer.slice(..)); |
| 260 | + pass.draw(0..vertices.len() as u32, 0..1); |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + self.queue.submit(std::iter::once(encoder.finish())); |
| 265 | + output.present(); |
| 266 | + Ok(()) |
| 267 | + } |
| 268 | + |
| 269 | + pub fn size(&self) -> (u32, u32) { |
| 270 | + self.size |
| 271 | + } |
| 272 | + |
| 273 | + pub fn device(&self) -> &wgpu::Device { |
| 274 | + &self.device |
| 275 | + } |
| 276 | + |
| 277 | + pub fn queue(&self) -> &wgpu::Queue { |
| 278 | + &self.queue |
| 279 | + } |
| 280 | + |
| 281 | + pub fn surface_format(&self) -> wgpu::TextureFormat { |
| 282 | + self.config.format |
| 283 | + } |
| 284 | +} |
| 285 | + |
| 286 | +fn push_rect(vertices: &mut Vec<Vertex>, x: f32, y: f32, w: f32, h: f32, color: &Color) { |
| 287 | + let c = [ |
| 288 | + color.r as f32 / 255.0, |
| 289 | + color.g as f32 / 255.0, |
| 290 | + color.b as f32 / 255.0, |
| 291 | + color.a as f32 / 255.0, |
| 292 | + ]; |
| 293 | + let (x0, y0, x1, y1) = (x, y, x + w, y + h); |
| 294 | + vertices.extend_from_slice(&[ |
| 295 | + Vertex { |
| 296 | + position: [x0, y0], |
| 297 | + color: c, |
| 298 | + }, |
| 299 | + Vertex { |
| 300 | + position: [x1, y0], |
| 301 | + color: c, |
| 302 | + }, |
| 303 | + Vertex { |
| 304 | + position: [x0, y1], |
| 305 | + color: c, |
| 306 | + }, |
| 307 | + Vertex { |
| 308 | + position: [x0, y1], |
| 309 | + color: c, |
| 310 | + }, |
| 311 | + Vertex { |
| 312 | + position: [x1, y0], |
| 313 | + color: c, |
| 314 | + }, |
| 315 | + Vertex { |
| 316 | + position: [x1, y1], |
| 317 | + color: c, |
| 318 | + }, |
| 319 | + ]); |
| 320 | +} |
0 commit comments