summaryrefslogtreecommitdiff
path: root/client/src/ui.rs
blob: a5ff7759ed2f6e402821a95173eab135fe1704f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
    wearechat - generic multiplayer game with voip
    Copyright (C) 2025 metamuffin

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3 of the License only.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/
use crate::state::InputState;
use egui::{
    Context, Event, ImageData, TextureId, ViewportId, ViewportInfo,
    epaint::{ImageDelta, Primitive, Vertex},
};
use glam::{Affine3A, Mat2, Mat3, Mat4, Vec2, Vec3Swizzles, Vec4Swizzles, vec2, vec4};
use log::{info, warn};
use rand::random;
use std::{
    collections::HashMap,
    num::NonZeroU64,
    sync::{Arc, RwLock},
};
use wgpu::{
    AddressMode, BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout,
    BindGroupLayoutDescriptor, BindGroupLayoutEntry, BindingResource, BindingType, BlendState,
    Buffer, BufferDescriptor, BufferUsages, ColorTargetState, ColorWrites, CommandEncoder,
    CompareFunction, DepthStencilState, Device, Extent3d, FilterMode, FragmentState, FrontFace,
    ImageCopyTexture, ImageDataLayout, IndexFormat, LoadOp, MultisampleState, Operations, Origin3d,
    PipelineCompilationOptions, PipelineLayoutDescriptor, PolygonMode, PrimitiveState,
    PrimitiveTopology, PushConstantRange, Queue, RenderPassColorAttachment,
    RenderPassDepthStencilAttachment, RenderPassDescriptor, RenderPipeline,
    RenderPipelineDescriptor, SamplerBindingType, SamplerDescriptor, ShaderStages, StoreOp,
    SurfaceConfiguration, Texture, TextureAspect, TextureDescriptor, TextureDimension,
    TextureFormat, TextureSampleType, TextureUsages, TextureView, TextureViewDescriptor,
    TextureViewDimension, VertexBufferLayout, VertexState, VertexStepMode, include_wgsl,
    util::{DeviceExt, TextureDataOrder},
    vertex_attr_array,
};

pub struct UiRenderer {
    device: Arc<Device>,
    queue: Arc<Queue>,
    ctx: Context,
    pipeline: RenderPipeline,
    bind_group_layout: BindGroupLayout,
    textures: RwLock<HashMap<TextureId, (BindGroup, Texture, [u32; 2])>>,
    surfaces: RwLock<HashMap<ViewportId, UiSurface>>,

    last_pointer: Vec2,
}

pub struct UiSurface {
    pub transform: Affine3A,
    pub content: Arc<dyn Fn(&Context) -> bool + Send + Sync + 'static>,
    size: Vec2,
    index: Buffer,
    index_capacity: usize,
    vertex: Buffer,
    vertex_capacity: usize,
}

impl UiRenderer {
    pub fn new(device: Arc<Device>, queue: Arc<Queue>, format: TextureFormat) -> Self {
        let module = device.create_shader_module(include_wgsl!("ui.wgsl"));
        let bind_group_layout = device.create_bind_group_layout(&BindGroupLayoutDescriptor {
            entries: &[
                BindGroupLayoutEntry {
                    binding: 0,
                    count: None,
                    visibility: ShaderStages::FRAGMENT,
                    ty: BindingType::Texture {
                        sample_type: TextureSampleType::Float { filterable: true },
                        view_dimension: TextureViewDimension::D2,
                        multisampled: false,
                    },
                },
                BindGroupLayoutEntry {
                    binding: 1,
                    count: None,
                    visibility: ShaderStages::FRAGMENT,
                    ty: BindingType::Sampler(SamplerBindingType::Filtering),
                },
            ],
            label: None,
        });
        let pipeline_layout = device.create_pipeline_layout(&PipelineLayoutDescriptor {
            label: None,
            bind_group_layouts: &[&bind_group_layout],
            push_constant_ranges: &[PushConstantRange {
                range: 0..(4 * 4 * size_of::<f32>() as u32),
                stages: ShaderStages::VERTEX,
            }],
        });
        let pipeline = device.create_render_pipeline(&RenderPipelineDescriptor {
            label: None,
            layout: Some(&pipeline_layout),
            fragment: Some(FragmentState {
                module: &module,
                entry_point: Some("fs_main"),
                targets: &[Some(ColorTargetState {
                    blend: Some(BlendState::PREMULTIPLIED_ALPHA_BLENDING),
                    format,
                    write_mask: ColorWrites::all(),
                })],
                compilation_options: PipelineCompilationOptions::default(),
            }),
            vertex: VertexState {
                module: &module,
                entry_point: Some("vs_main"),
                buffers: &[VertexBufferLayout {
                    array_stride: size_of::<Vertex>() as u64,
                    step_mode: VertexStepMode::Vertex,
                    attributes: &vertex_attr_array![0 => Float32x2, 1 => Float32x2, 2 => Uint32],
                }],
                compilation_options: PipelineCompilationOptions::default(),
            },
            primitive: PrimitiveState {
                topology: PrimitiveTopology::TriangleList,
                front_face: FrontFace::Ccw,
                cull_mode: None, //Some(Face::Back),
                polygon_mode: PolygonMode::Fill,
                ..Default::default()
            },
            depth_stencil: Some(DepthStencilState {
                format: TextureFormat::Depth32Float,
                depth_write_enabled: false,
                depth_compare: CompareFunction::Always,
                stencil: Default::default(),
                bias: Default::default(),
            }),
            multisample: MultisampleState::default(),
            multiview: None,
            cache: None,
        });
        Self {
            ctx: Context::default(),
            pipeline,
            device,
            queue,
            bind_group_layout,
            last_pointer: Vec2::ZERO,
            textures: HashMap::new().into(),
            surfaces: HashMap::new().into(),
        }
    }

    pub fn add_surface(
        &mut self,
        transform: Affine3A,
        content: impl Fn(&Context) -> bool + Send + Sync + 'static,
    ) {
        let index_capacity = 1024;
        let vertex_capacity = 1024;
        let index = self.device.create_buffer(&BufferDescriptor {
            label: None,
            size: (size_of::<f32>() * index_capacity) as u64,
            usage: BufferUsages::INDEX | BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        let vertex = self.device.create_buffer(&BufferDescriptor {
            label: None,
            size: (size_of::<Vertex>() * vertex_capacity) as u64,
            usage: BufferUsages::VERTEX | BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        let id = ViewportId::from_hash_of(random::<u128>());
        info!("ui surface added: {id:?}");
        self.surfaces.write().unwrap().insert(id, UiSurface {
            transform,
            content: Arc::new(content),
            index,
            vertex,
            index_capacity,
            vertex_capacity,
            size: Vec2::ZERO,
        });
    }

    pub fn apply_texture_delta(&self, texid: TextureId, delta: ImageDelta) {
        let mut textures = self.textures.write().unwrap();
        let size = Extent3d {
            depth_or_array_layers: 1,
            width: delta.image.width() as u32,
            height: delta.image.height() as u32,
        };
        let pixels = match &delta.image {
            ImageData::Color(color_image) => color_image.pixels.clone(),
            ImageData::Font(font_image) => font_image.srgba_pixels(None).collect(),
        };

        if let Some((_texbg, tex, texsize)) = textures.get_mut(&texid) {
            let pos = delta.pos.unwrap_or([0, 0]);
            info!("updating UI texture at {pos:?}");
            self.queue.write_texture(
                ImageCopyTexture {
                    texture: &tex,
                    mip_level: 0,
                    origin: Origin3d {
                        x: pos[0] as u32,
                        y: pos[1] as u32,
                        z: 0,
                    },
                    aspect: TextureAspect::All,
                },
                bytemuck::cast_slice::<_, u8>(&pixels),
                ImageDataLayout {
                    offset: 0,
                    bytes_per_row: Some(texsize[0] * 4),
                    rows_per_image: None,
                },
                size,
            );
        } else {
            assert_eq!(
                delta.pos, None,
                "partial update impossible; texture does not yet exist"
            );
            info!(
                "uploading new UI texture: width={}, height={}",
                delta.image.width(),
                delta.image.height()
            );

            let texture = self.device.create_texture_with_data(
                &self.queue,
                &TextureDescriptor {
                    label: None,
                    size,
                    mip_level_count: 1,
                    sample_count: 1,
                    dimension: TextureDimension::D2,
                    format: TextureFormat::Rgba8UnormSrgb,
                    usage: TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST,
                    view_formats: &[],
                },
                TextureDataOrder::LayerMajor,
                bytemuck::cast_slice::<_, u8>(&pixels),
            );
            let textureview = texture.create_view(&TextureViewDescriptor::default());
            let sampler = self.device.create_sampler(&SamplerDescriptor {
                address_mode_u: AddressMode::ClampToEdge,
                address_mode_v: AddressMode::ClampToEdge,
                mag_filter: FilterMode::Linear,
                min_filter: FilterMode::Linear,
                ..Default::default()
            });
            let bindgroup = self.device.create_bind_group(&BindGroupDescriptor {
                label: None,
                layout: &self.bind_group_layout,
                entries: &[
                    BindGroupEntry {
                        binding: 0,
                        resource: BindingResource::TextureView(&textureview),
                    },
                    BindGroupEntry {
                        binding: 1,
                        resource: BindingResource::Sampler(&sampler),
                    },
                ],
            });
            textures.insert(
                texid,
                (bindgroup, texture, delta.image.size().map(|e| e as u32)),
            );
        }
    }

    pub fn draw(
        &mut self,
        commands: &mut CommandEncoder,
        target: &TextureView,
        depth: &TextureView,
        projection: Mat4,
        input_state: &InputState,
        surface_configuration: &SurfaceConfiguration,
    ) {
        let mut surfaces = self.surfaces.write().unwrap();
        if surfaces.is_empty() {
            return;
        }

        let mut rpass = commands.begin_render_pass(&RenderPassDescriptor {
            label: None,
            color_attachments: &[Some(RenderPassColorAttachment {
                view: target,
                resolve_target: None,
                ops: Operations {
                    load: LoadOp::Load,
                    store: StoreOp::Store,
                },
            })],
            depth_stencil_attachment: Some(RenderPassDepthStencilAttachment {
                view: &depth,
                depth_ops: Some(Operations {
                    load: LoadOp::Load,
                    store: StoreOp::Store,
                }),
                stencil_ops: None,
            }),
            ..Default::default()
        });

        rpass.set_pipeline(&self.pipeline);

        let mut raw_input = egui::RawInput::default();
        raw_input.viewport_id = surfaces.keys().next().copied().unwrap();
        raw_input.viewports = surfaces
            .keys()
            .map(|k| {
                (*k, ViewportInfo {
                    native_pixels_per_point: Some(2.),
                    ..Default::default()
                })
            })
            .collect();

        let mut surfaces_closed = Vec::new();
        for (viewport_id, surf) in surfaces.iter_mut() {
            let scale = 0.03;
            let projection = projection
                * Mat4::from_translation(surf.transform.translation.into())
                * Mat4::from_mat3a(surf.transform.matrix3)
                * Mat4::from_mat3(Mat3::from_mat2(Mat2::from_cols_array(&[
                    scale, 0., 0., -scale,
                ])));

            let screen_size = vec2(
                surface_configuration.width as f32,
                surface_configuration.height as f32,
            );

            let mouse_xy_clip = (input_state.cursor_pos / screen_size) * 2. - 1.;

            if projection.determinant() < 0e-4 {
                warn!("bad UI projection")
            }

            let unproject = projection.inverse();

            let mut mouse_world_1 = unproject * vec4(mouse_xy_clip.x, -mouse_xy_clip.y, 0.0, 1.0);
            let mut mouse_world_2 = unproject * vec4(mouse_xy_clip.x, -mouse_xy_clip.y, 1.0, 1.0);
            mouse_world_1 /= mouse_world_1.w;
            mouse_world_2 /= mouse_world_2.w;
            let mouse_world_1 = mouse_world_1.xyz();
            let mouse_world_2 = mouse_world_2.xyz();

            let ray_norm = (mouse_world_2 - mouse_world_1).normalize();
            let ray_t = mouse_world_1.z / ray_norm.z;
            let ray_hit = mouse_world_1 - ray_norm * ray_t;

            debug_assert!(ray_hit.z.abs() < 0.1, "mouse was not projected properly");

            eprintln!("z={} t={ray_t}", mouse_world_1.z);
            eprintln!("hit={ray_hit}");

            let cursor_pos = ray_hit.xy();

            let mut raw_input = raw_input.clone();
            if cursor_pos != self.last_pointer {
                raw_input.events.push(Event::PointerMoved(egui::Pos2::new(
                    cursor_pos.x,
                    cursor_pos.y,
                )));
                self.last_pointer = cursor_pos;
            }
            raw_input
                .events
                .extend(input_state.egui_events.iter().cloned());

            let mut close = false;
            let full_output = self.ctx.run(raw_input.clone(), |ctx| {
                close = !(surf.content)(ctx);
                surf.size = Vec2::new(ctx.used_size().x, ctx.used_size().y)
            });
            if close {
                surfaces_closed.push(*viewport_id)
            }

            for (texid, delta) in full_output.textures_delta.set {
                self.apply_texture_delta(texid, delta);
            }

            let clipped_primitives = self
                .ctx
                .tessellate(full_output.shapes, full_output.pixels_per_point);

            let mut index_count = 0;
            let mut vertex_count = 0;
            for p in &clipped_primitives {
                if let Primitive::Mesh(mesh) = &p.primitive {
                    index_count += mesh.indices.len();
                    vertex_count += mesh.vertices.len();
                }
            }

            if index_count == 0 || vertex_count == 0 {
                return;
            }

            while index_count > surf.index_capacity {
                info!(
                    "index buffer overflow. expanding {} -> {}",
                    surf.index_capacity,
                    surf.index_capacity * 2
                );
                surf.index_capacity *= 2;
                surf.index = self.device.create_buffer(&BufferDescriptor {
                    label: None,
                    size: (size_of::<f32>() * surf.index_capacity) as u64,
                    usage: BufferUsages::INDEX | BufferUsages::COPY_DST,
                    mapped_at_creation: false,
                });
            }
            while vertex_count > surf.vertex_capacity {
                info!(
                    "vertex buffer overflow. expanding {} -> {}",
                    surf.vertex_capacity,
                    surf.vertex_capacity * 2
                );
                surf.vertex_capacity *= 2;
                surf.vertex = self.device.create_buffer(&BufferDescriptor {
                    label: None,
                    size: (size_of::<f32>() * surf.vertex_capacity) as u64,
                    usage: BufferUsages::VERTEX | BufferUsages::COPY_DST,
                    mapped_at_creation: false,
                });
            }

            let mut mapped_index = self
                .queue
                .write_buffer_with(
                    &surf.index,
                    0,
                    NonZeroU64::new((size_of::<u32>() * index_count) as u64).unwrap(),
                )
                .expect("ui index buffer overflow");
            let mut mapped_vertex = self
                .queue
                .write_buffer_with(
                    &surf.vertex,
                    0,
                    NonZeroU64::new((size_of::<Vertex>() * vertex_count) as u64).unwrap(),
                )
                .expect("ui vertex buffer overflow");

            let mut index_offset = 0;
            let mut vertex_offset = 0;
            let mut slices = Vec::new();
            for p in clipped_primitives {
                if let Primitive::Mesh(mesh) = p.primitive {
                    mapped_index[index_offset * size_of::<u32>()
                        ..(index_offset + mesh.indices.len()) * size_of::<u32>()]
                        .copy_from_slice(bytemuck::cast_slice(&mesh.indices));
                    mapped_vertex[vertex_offset * size_of::<Vertex>()
                        ..(vertex_offset + mesh.vertices.len()) * size_of::<Vertex>()]
                        .copy_from_slice(bytemuck::cast_slice(&mesh.vertices));
                    slices.push((
                        index_offset as u32..index_offset as u32 + mesh.indices.len() as u32,
                        vertex_offset as i32,
                        mesh.texture_id,
                    ));
                    index_offset += mesh.indices.len();
                    vertex_offset += mesh.vertices.len();
                }
            }

            assert_eq!(index_count, index_offset);
            assert_eq!(vertex_count, vertex_offset);

            let projection = projection.to_cols_array().map(|v| v.to_le_bytes());

            rpass.set_push_constants(ShaderStages::VERTEX, 0, projection.as_flattened());
            rpass.set_index_buffer(surf.index.slice(..), IndexFormat::Uint32);
            rpass.set_vertex_buffer(0, surf.vertex.slice(..));
            for (index, base_vertex, texid) in slices {
                let tex_guard = self.textures.read().unwrap();
                let bind_group = &tex_guard.get(&texid).unwrap().0;
                rpass.set_bind_group(0, bind_group, &[]);
                rpass.draw_indexed(index, base_vertex, 0..1);
            }
        }
        for s in surfaces_closed {
            info!("ui surface closed: {s:?}");
            surfaces.remove(&s);
        }
    }
}