kas-wgpu 0.17.1

KAS GUI / wgpu front-end
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
//     https://www.apache.org/licenses/LICENSE-2.0

//! Drawing API for `kas_wgpu`

use futures_lite::future::block_on;
use std::f32::consts::FRAC_PI_2;
use wgpu::util::DeviceExt;

use super::*;
use crate::DrawShadedImpl;
use crate::Options;
use kas::cast::traits::*;
use kas::config::RasterConfig;
use kas::draw::color::Rgba;
use kas::draw::*;
use kas::geom::{Quad, Size, Vec2};
use kas::runner::{GraphicsFeatures, RunError};
use kas::text::{Effect, TextDisplay};

impl<C: CustomPipe> DrawPipe<C> {
    /// Construct
    pub fn new<CB: CustomPipeBuilder<Pipe = C>>(
        instance: &wgpu::Instance,
        custom: &mut CB,
        options: &Options,
        surface: Option<&wgpu::Surface>,
        features: GraphicsFeatures,
    ) -> Result<Self, RunError> {
        let mut adapter_options = options.adapter_options();
        adapter_options.compatible_surface = surface;
        let req = instance.request_adapter(&adapter_options);
        let adapter = match block_on(req) {
            Ok(a) => a,
            Err(e) => return Err(RunError::Graphics(Box::new(e))),
        };
        log::info!("Using graphics adapter: {}", adapter.get_info().name);

        // Use adapter texture size limits to support the largest window surface possible
        let mut desc = CB::device_descriptor(&adapter);
        if features.subpixel_rendering
            && adapter
                .features()
                .contains(wgpu::Features::DUAL_SOURCE_BLENDING)
        {
            desc.required_features |= wgpu::Features::DUAL_SOURCE_BLENDING;
        }
        desc.required_limits = desc.required_limits.using_resolution(adapter.limits());

        let req = adapter.request_device(&desc);
        let (device, queue) = block_on(req).map_err(|e| RunError::Graphics(Box::new(e)))?;

        let shaders = ShaderManager::new(&device);

        // Create staging belt and a local pool
        let staging_belt = wgpu::util::StagingBelt::new(device.clone(), 1024);

        let bgl_common = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("common bind group layout"),
            entries: &[
                wgpu::BindGroupLayoutEntry {
                    binding: 0,
                    visibility: wgpu::ShaderStages::VERTEX,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Uniform,
                        has_dynamic_offset: false,
                        min_binding_size: wgpu::BufferSize::new(16),
                    },
                    count: None,
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 1,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Uniform,
                        has_dynamic_offset: false,
                        min_binding_size: wgpu::BufferSize::new(16),
                    },
                    count: None,
                },
            ],
        });

        // Light dir: `(a, b)` where `0 ≤ a < pi/2` is the angle to the screen
        // normal (i.e. `a = 0` is straight at the screen) and `b` is the bearing
        // (from UP, clockwise), both in radians.
        let dir: (f32, f32) = (0.3, 0.4);
        assert!(0.0 <= dir.0 && dir.0 < FRAC_PI_2);
        let a = (dir.0.sin(), dir.0.cos());
        // We normalise intensity:
        let f = a.0 / a.1;
        let light_norm = [dir.1.sin() * f, -dir.1.cos() * f, 1.0, 0.0];

        let light_norm_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("light_norm_buf"),
            contents: bytemuck::cast_slice(&light_norm),
            usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
        });

        let images = images::Images::new(&device, &shaders, &bgl_common);
        let shaded_square = shaded_square::Pipeline::new(&device, &shaders, &bgl_common);
        let shaded_round = shaded_round::Pipeline::new(&device, &shaders, &bgl_common);
        let flat_round = flat_round::Pipeline::new(&device, &shaders, &bgl_common);
        let round_2col = round_2col::Pipeline::new(&device, &shaders, &bgl_common);
        let custom = custom.build(&device, &bgl_common, RENDER_TEX_FORMAT);

        Ok(DrawPipe {
            adapter,
            device,
            queue,
            staging_belt,
            bgl_common,
            light_norm_buf,
            bg_common: vec![],
            images,
            text: kas::text::raster::State::default(),
            shaded_square,
            shaded_round,
            flat_round,
            round_2col,
            custom,
        })
    }

    /// Process window resize
    pub fn resize(&self, window: &mut DrawWindow<C::Window>, size: Size) {
        window.clip_regions[0].rect.size = size;

        let vsize = Vec2::conv(size);
        let off = vsize * -0.5;
        let scale = Vec2::splat(2.0) / vsize;
        window.scale = [off.0, off.1, scale.0, -scale.1];

        self.custom
            .resize(&mut window.custom, &self.device, &self.queue, size);

        self.queue.submit(std::iter::empty());
    }

    /// Render batched draw instructions via `rpass`
    pub fn render(
        &mut self,
        window: &mut DrawWindow<C::Window>,
        frame_view: &wgpu::TextureView,
        clear_color: wgpu::Color,
    ) {
        // Update all bind groups. We use a separate bind group for each clip
        // region and update on each render, although they don't always change.
        // NOTE: we could use push constants instead.
        let mut scale = window.scale;
        let base_offset = (scale[0], scale[1]);
        for (region, bg) in window.clip_regions.iter().zip(self.bg_common.iter()) {
            let offset = Vec2::conv(region.offset);
            scale[0] = base_offset.0 - offset.0;
            scale[1] = base_offset.1 - offset.1;
            self.queue
                .write_buffer(&bg.0, 0, bytemuck::cast_slice(&scale));
        }
        let device = &self.device;
        let bg_len = self.bg_common.len();
        if window.clip_regions.len() > bg_len {
            let (bgl_common, light_norm_buf) = (&self.bgl_common, &self.light_norm_buf);
            self.bg_common
                .extend(window.clip_regions[bg_len..].iter().map(|region| {
                    let offset = Vec2::conv(region.offset);
                    scale[0] = base_offset.0 - offset.0;
                    scale[1] = base_offset.1 - offset.1;
                    let scale_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
                        label: Some("scale_buf"),
                        contents: bytemuck::cast_slice(&scale),
                        usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                    });
                    let bg_common = device.create_bind_group(&wgpu::BindGroupDescriptor {
                        label: Some("common bind group"),
                        layout: bgl_common,
                        entries: &[
                            wgpu::BindGroupEntry {
                                binding: 0,
                                resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
                                    buffer: &scale_buf,
                                    offset: 0,
                                    size: None,
                                }),
                            },
                            wgpu::BindGroupEntry {
                                binding: 1,
                                resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
                                    buffer: light_norm_buf,
                                    offset: 0,
                                    size: None,
                                }),
                            },
                        ],
                    });
                    (scale_buf, bg_common)
                }));
        }
        self.queue.submit(std::iter::empty());

        let mut encoder = self
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("render"),
            });

        self.images.prepare(
            &mut window.images,
            &self.device,
            &self.queue,
            &mut self.staging_belt,
            &mut encoder,
            &mut self.text,
        );
        window
            .shaded_square
            .write_buffers(&self.device, &mut self.staging_belt, &mut encoder);
        window
            .shaded_round
            .write_buffers(&self.device, &mut self.staging_belt, &mut encoder);
        window
            .flat_round
            .write_buffers(&self.device, &mut self.staging_belt, &mut encoder);
        window
            .round_2col
            .write_buffers(&self.device, &mut self.staging_belt, &mut encoder);
        self.custom.prepare(
            &mut window.custom,
            &self.device,
            &mut self.staging_belt,
            &mut encoder,
        );

        let mut color_attachments = [Some(wgpu::RenderPassColorAttachment {
            view: frame_view,
            depth_slice: None,
            resolve_target: None,
            ops: wgpu::Operations {
                load: wgpu::LoadOp::Clear(clear_color),
                store: wgpu::StoreOp::Store,
            },
        })];

        // Order passes to ensure overlays are drawn after other content
        let mut passes: Vec<_> = window
            .clip_regions
            .iter()
            .map(|pass| pass.order)
            .enumerate()
            .collect();
        // Note that sorting is stable (does not re-order equal elements):
        passes.sort_by_key(|pass| pass.1);

        // We use a separate render pass for each clipped region.
        for (pass, _) in passes.drain(..) {
            let rect = window.clip_regions[pass].rect;
            if rect.size.0 == 0 || rect.size.1 == 0 {
                continue;
            }
            let bg_common = &self.bg_common[pass].1;

            {
                let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                    label: Some("kas-wgpu render pass"),
                    color_attachments: &color_attachments,
                    depth_stencil_attachment: None,
                    timestamp_writes: None,
                    occlusion_query_set: None,
                    multiview_mask: None,
                });
                rpass.set_scissor_rect(
                    rect.pos.0.cast(),
                    rect.pos.1.cast(),
                    rect.size.0.cast(),
                    rect.size.1.cast(),
                );

                self.round_2col
                    .render(&window.round_2col, pass, &mut rpass, bg_common);
                self.shaded_square
                    .render(&window.shaded_square, pass, &mut rpass, bg_common);
                self.images
                    .render(&window.images, pass, &mut rpass, bg_common);
                self.shaded_round
                    .render(&window.shaded_round, pass, &mut rpass, bg_common);
                self.flat_round
                    .render(&window.flat_round, pass, &mut rpass, bg_common);
                self.custom.render_pass(
                    &mut window.custom,
                    &self.device,
                    pass,
                    &mut rpass,
                    bg_common,
                );
            }

            color_attachments[0].as_mut().unwrap().ops.load = wgpu::LoadOp::Load;
        }

        let size = window.clip_regions[0].rect.size;

        self.custom.render_final(
            &mut window.custom,
            &self.device,
            &mut encoder,
            frame_view,
            size,
        );

        // Keep only first clip region (which is the entire window)
        window.clip_regions.truncate(1);

        self.staging_belt.finish();
        self.queue.submit(std::iter::once(encoder.finish()));

        self.staging_belt.recall();
    }
}

impl<C: CustomPipe> DrawSharedImpl for DrawPipe<C> {
    type Draw = DrawWindow<C::Window>;

    fn max_texture_dimension_2d(&self) -> u32 {
        self.device.limits().max_texture_dimension_2d
    }

    fn set_raster_config(&mut self, config: &RasterConfig) {
        self.text.set_raster_config(config);
    }

    #[inline]
    fn image_alloc(&mut self, format: ImageFormat, size: Size) -> Result<ImageId, AllocError> {
        self.images.alloc(format, size)
    }

    #[inline]
    fn image_upload(&mut self, id: ImageId, data: &[u8]) -> Result<(), UploadError> {
        self.images.upload(&self.device, &self.queue, id, data)
    }

    #[inline]
    fn image_free(&mut self, id: ImageId) {
        self.images.free(id);
    }

    #[inline]
    fn image_size(&self, id: ImageId) -> Option<Size> {
        self.images.image_size(id)
    }

    #[inline]
    fn draw_image(&self, draw: &mut Self::Draw, pass: PassId, id: ImageId, rect: Quad) {
        if let Some((atlas, tex)) = self.images.get_im_atlas_coords(id) {
            draw.images.rect(pass, atlas, tex, rect);
        };
    }

    #[inline]
    fn draw_text(
        &mut self,
        draw: &mut Self::Draw,
        pass: PassId,
        pos: Vec2,
        bb: Quad,
        text: &TextDisplay,
        col: Rgba,
    ) {
        let time = std::time::Instant::now();
        self.text
            .text(&mut self.images, &mut draw.images, pass, pos, bb, text, col);
        draw.common.report_dur_text(time.elapsed());
    }

    fn draw_text_effects(
        &mut self,
        draw: &mut Self::Draw,
        pass: PassId,
        pos: Vec2,
        bb: Quad,
        text: &TextDisplay,
        colors: &[Rgba],
        effects: &[Effect],
    ) {
        let time = std::time::Instant::now();
        self.text.text_effects(
            &mut self.images,
            &mut draw.images,
            pass,
            pos,
            bb,
            text,
            colors,
            effects,
            |quad, col| {
                draw.shaded_square.rect(pass, quad, col);
            },
        );
        draw.common.report_dur_text(time.elapsed());
    }
}

impl<CW: CustomWindow> DrawImpl for DrawWindow<CW> {
    fn common_mut(&mut self) -> &mut WindowCommon {
        &mut self.common
    }

    fn new_pass(
        &mut self,
        parent_pass: PassId,
        rect: Rect,
        offset: Offset,
        class: PassType,
    ) -> PassId {
        let parent = match class {
            PassType::Clip => &self.clip_regions[parent_pass.pass()],
            PassType::Overlay => {
                // NOTE: parent_pass.pass() is always zero in this case since
                // this is only invoked from the Window (root).
                &self.clip_regions[0]
            }
        };
        let order = match class {
            PassType::Clip => (parent.order << 4) + 1,
            PassType::Overlay => (parent.order << 16) + 1,
        };
        let rect = rect - parent.offset;
        let offset = offset + parent.offset;
        let rect = rect.intersection(&parent.rect).unwrap_or(Rect::ZERO);
        let pass = self.clip_regions.len().cast();
        self.clip_regions.push(ClipRegion {
            rect,
            offset,
            order,
        });
        PassId::new(pass)
    }

    #[inline]
    fn get_clip_rect(&self, pass: PassId) -> Rect {
        let region = &self.clip_regions[pass.pass()];
        region.rect + region.offset
    }

    #[inline]
    fn rect(&mut self, pass: PassId, rect: Quad, col: Rgba) {
        self.shaded_square.rect(pass, rect, col);
    }

    #[inline]
    fn frame(&mut self, pass: PassId, outer: Quad, inner: Quad, col: Rgba) {
        self.shaded_square.frame(pass, outer, inner, col);
    }

    #[inline]
    fn line(&mut self, pass: PassId, p1: Vec2, p2: Vec2, width: f32, col: Rgba) {
        self.flat_round.line(pass, p1, p2, 0.5 * width, col);
    }
}

impl<CW: CustomWindow> DrawRoundedImpl for DrawWindow<CW> {
    #[inline]
    fn rounded_line(&mut self, pass: PassId, p1: Vec2, p2: Vec2, radius: f32, col: Rgba) {
        self.flat_round.line(pass, p1, p2, radius, col);
    }

    #[inline]
    fn circle(&mut self, pass: PassId, rect: Quad, inner_radius: f32, col: Rgba) {
        self.flat_round.circle(pass, rect, inner_radius, col);
    }

    #[inline]
    fn circle_2col(&mut self, pass: PassId, rect: Quad, col1: Rgba, col2: Rgba) {
        self.round_2col.circle(pass, rect, col1, col2);
    }

    #[inline]
    fn rounded_frame(&mut self, pass: PassId, outer: Quad, inner: Quad, r1: f32, col: Rgba) {
        self.flat_round.rounded_frame(pass, outer, inner, r1, col);
    }

    #[inline]
    fn rounded_frame_2col(&mut self, pass: PassId, outer: Quad, inner: Quad, c1: Rgba, c2: Rgba) {
        self.round_2col.frame(pass, outer, inner, c1, c2);
    }
}

impl<CW: CustomWindow> DrawShadedImpl for DrawWindow<CW> {
    #[inline]
    fn shaded_square(&mut self, pass: PassId, rect: Quad, norm: (f32, f32), col: Rgba) {
        self.shaded_square
            .shaded_rect(pass, rect, Vec2::from(norm), col);
    }

    #[inline]
    fn shaded_circle(&mut self, pass: PassId, rect: Quad, norm: (f32, f32), col: Rgba) {
        self.shaded_round.circle(pass, rect, Vec2::from(norm), col);
    }

    #[inline]
    fn shaded_square_frame(
        &mut self,
        pass: PassId,
        outer: Quad,
        inner: Quad,
        norm: (f32, f32),
        outer_col: Rgba,
        inner_col: Rgba,
    ) {
        self.shaded_square
            .shaded_frame(pass, outer, inner, Vec2::from(norm), outer_col, inner_col);
    }

    #[inline]
    fn shaded_round_frame(
        &mut self,
        pass: PassId,
        outer: Quad,
        inner: Quad,
        norm: (f32, f32),
        col: Rgba,
    ) {
        self.shaded_round
            .shaded_frame(pass, outer, inner, Vec2::from(norm), col);
    }
}