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
#![allow(
    irrefutable_let_patterns,
    clippy::new_without_default,
    // Conflicts with `pattern_type_mismatch`
    clippy::needless_borrowed_reference,
)]
#![warn(
    trivial_casts,
    trivial_numeric_casts,
    unused_extern_crates,
    unused_qualifications,
    // We don't match on a reference, unless required.
    clippy::pattern_type_mismatch,
)]

mod belt;

const SHADER_SOURCE: &'static str = include_str!("../shader.wgsl");

use belt::{BeltDescriptor, BufferBelt};
use std::{
    collections::hash_map::{Entry, HashMap},
    mem, ptr,
};

#[repr(C)]
#[derive(Clone, Copy, bytemuck::Zeroable, bytemuck::Pod)]
struct Uniforms {
    screen_size: [f32; 2],
    padding: [f32; 2],
}

#[derive(blade_macros::ShaderData)]
struct Globals {
    r_uniforms: Uniforms,
    r_sampler: blade_graphics::Sampler,
}

#[derive(blade_macros::ShaderData)]
struct Locals {
    r_vertex_data: blade_graphics::BufferPiece,
    r_texture: blade_graphics::TextureView,
}

pub struct ScreenDescriptor {
    pub physical_size: (u32, u32),
    pub scale_factor: f32,
}

impl ScreenDescriptor {
    fn logical_size(&self) -> (f32, f32) {
        let logical_width = self.physical_size.0 as f32 / self.scale_factor;
        let logical_height = self.physical_size.1 as f32 / self.scale_factor;
        (logical_width, logical_height)
    }
}

struct GuiTexture {
    allocation: blade_graphics::Texture,
    view: blade_graphics::TextureView,
}

impl GuiTexture {
    fn create(context: &blade_graphics::Context, name: &str, size: blade_graphics::Extent) -> Self {
        let format = blade_graphics::TextureFormat::Rgba8UnormSrgb;
        let allocation = context.create_texture(blade_graphics::TextureDesc {
            name,
            format,
            size,
            array_layer_count: 1,
            mip_level_count: 1,
            dimension: blade_graphics::TextureDimension::D2,
            usage: blade_graphics::TextureUsage::COPY | blade_graphics::TextureUsage::RESOURCE,
        });
        let view = context.create_texture_view(blade_graphics::TextureViewDesc {
            name,
            texture: allocation,
            format,
            dimension: blade_graphics::ViewDimension::D2,
            subresources: &blade_graphics::TextureSubresources::default(),
        });
        Self { allocation, view }
    }

    fn delete(self, context: &blade_graphics::Context) {
        context.destroy_texture(self.allocation);
        context.destroy_texture_view(self.view);
    }
}

//TODO: scissor test

/// GUI painter based on egui.
///
/// It can render egui primitives into a render pass.
pub struct GuiPainter {
    pipeline: blade_graphics::RenderPipeline,
    //TODO: find a better way to allocate temporary buffers.
    belt: BufferBelt,
    textures: HashMap<egui::TextureId, GuiTexture>,
    //TODO: this could also look better
    textures_dropped: Vec<GuiTexture>,
    textures_to_delete: Vec<(GuiTexture, blade_graphics::SyncPoint)>,
    sampler: blade_graphics::Sampler,
}

impl GuiPainter {
    /// Destroy the contents of the painter.
    pub fn destroy(&mut self, context: &blade_graphics::Context) {
        self.belt.destroy(context);
        for (_, gui_texture) in self.textures.drain() {
            gui_texture.delete(context);
        }
        context.destroy_sampler(self.sampler);
    }

    /// Create a new painter with a given GPU context.
    ///
    /// It supports renderpasses with only a color attachment,
    /// and this attachment format must be The `output_format`.
    #[profiling::function]
    pub fn new(
        output_format: blade_graphics::TextureFormat,
        context: &blade_graphics::Context,
    ) -> Self {
        let shader = context.create_shader(blade_graphics::ShaderDesc {
            source: SHADER_SOURCE,
        });
        let globals_layout = <Globals as blade_graphics::ShaderData>::layout();
        let locals_layout = <Locals as blade_graphics::ShaderData>::layout();
        let pipeline = context.create_render_pipeline(blade_graphics::RenderPipelineDesc {
            name: "gui",
            data_layouts: &[&globals_layout, &locals_layout],
            vertex: shader.at("vs_main"),
            primitive: blade_graphics::PrimitiveState {
                topology: blade_graphics::PrimitiveTopology::TriangleList,
                ..Default::default()
            },
            depth_stencil: None, //TODO?
            fragment: shader.at("fs_main"),
            color_targets: &[blade_graphics::ColorTargetState {
                format: output_format,
                blend: Some(blade_graphics::BlendState::ALPHA_BLENDING),
                write_mask: blade_graphics::ColorWrites::all(),
            }],
        });

        let belt = BufferBelt::new(BeltDescriptor {
            memory: blade_graphics::Memory::Shared,
            min_chunk_size: 0x1000,
        });

        let sampler = context.create_sampler(blade_graphics::SamplerDesc {
            name: "gui",
            address_modes: [blade_graphics::AddressMode::ClampToEdge; 3],
            mag_filter: blade_graphics::FilterMode::Linear,
            min_filter: blade_graphics::FilterMode::Linear,
            ..Default::default()
        });

        Self {
            pipeline,
            belt,
            textures: Default::default(),
            textures_dropped: Vec::new(),
            textures_to_delete: Vec::new(),
            sampler,
        }
    }

    #[profiling::function]
    fn triage_deletions(&mut self, context: &blade_graphics::Context) {
        let valid_pos = self
            .textures_to_delete
            .iter()
            .position(|&(_, ref sp)| !context.wait_for(sp, 0))
            .unwrap_or_default();
        for (texture, _) in self.textures_to_delete.drain(..valid_pos) {
            context.destroy_texture_view(texture.view);
            context.destroy_texture(texture.allocation);
        }
    }

    /// Updates the texture used by egui for the fonts etc.
    /// New textures should be added before the call to `execute()`,
    /// and old textures should be removed after.
    #[profiling::function]
    pub fn update_textures(
        &mut self,
        command_encoder: &mut blade_graphics::CommandEncoder,
        textures_delta: &egui::TexturesDelta,
        context: &blade_graphics::Context,
    ) {
        if textures_delta.set.is_empty() && textures_delta.free.is_empty() {
            return;
        }

        let mut copies = Vec::new();
        for &(texture_id, ref image_delta) in textures_delta.set.iter() {
            let src = match image_delta.image {
                egui::ImageData::Color(ref c) => self.belt.alloc_data(c.pixels.as_slice(), context),
                egui::ImageData::Font(ref a) => {
                    let color_iter = a.srgba_pixels(None);
                    let stage = self.belt.alloc(
                        (color_iter.len() * mem::size_of::<egui::Color32>()) as u64,
                        context,
                    );
                    let mut ptr = stage.data() as *mut egui::Color32;
                    for color in color_iter {
                        unsafe {
                            ptr::write(ptr, color);
                            ptr = ptr.offset(1);
                        }
                    }
                    stage
                }
            };

            let image_size = image_delta.image.size();
            let extent = blade_graphics::Extent {
                width: image_size[0] as u32,
                height: image_size[1] as u32,
                depth: 1,
            };

            let label = match texture_id {
                egui::TextureId::Managed(m) => format!("egui_image_{}", m),
                egui::TextureId::User(u) => format!("egui_user_image_{}", u),
            };

            let texture = match self.textures.entry(texture_id) {
                Entry::Occupied(mut o) => {
                    if image_delta.pos.is_none() {
                        let texture = GuiTexture::create(context, &label, extent);
                        command_encoder.init_texture(texture.allocation);
                        let old = o.insert(texture);
                        self.textures_dropped.push(old);
                    }
                    o.into_mut()
                }
                Entry::Vacant(v) => {
                    let texture = GuiTexture::create(context, &label, extent);
                    command_encoder.init_texture(texture.allocation);
                    v.insert(texture)
                }
            };

            let dst = blade_graphics::TexturePiece {
                texture: texture.allocation,
                mip_level: 0,
                array_layer: 0,
                origin: match image_delta.pos {
                    Some([x, y]) => [x as u32, y as u32, 0],
                    None => [0; 3],
                },
            };
            copies.push((src, dst, extent));
        }

        if let mut transfer = command_encoder.transfer() {
            for (src, dst, extent) in copies {
                transfer.copy_buffer_to_texture(src, 4 * extent.width, dst, extent);
            }
        }

        for texture_id in textures_delta.free.iter() {
            let texture = self.textures.remove(texture_id).unwrap();
            self.textures_dropped.push(texture);
        }

        self.triage_deletions(context);
    }

    /// Render the set of clipped primitives into a render pass.
    /// The `sd` must contain dimensions of the render target.
    #[profiling::function]
    pub fn paint(
        &mut self,
        pass: &mut blade_graphics::RenderCommandEncoder,
        paint_jobs: &[egui::epaint::ClippedPrimitive],
        sd: &ScreenDescriptor,
        context: &blade_graphics::Context,
    ) {
        let logical_size = sd.logical_size();
        let mut pc = pass.with(&self.pipeline);
        pc.bind(
            0,
            &Globals {
                r_uniforms: Uniforms {
                    screen_size: [logical_size.0, logical_size.1],
                    padding: [0.0; 2],
                },
                r_sampler: self.sampler,
            },
        );

        for clipped_prim in paint_jobs {
            let clip_rect = &clipped_prim.clip_rect;

            // Make sure clip rect can fit within an `u32`.
            let clip_min_x = (sd.scale_factor * clip_rect.min.x)
                .clamp(0.0, sd.physical_size.0 as f32)
                .trunc() as u32;
            let clip_min_y = (sd.scale_factor * clip_rect.min.y)
                .clamp(0.0, sd.physical_size.1 as f32)
                .trunc() as u32;
            let clip_max_x = (sd.scale_factor * clip_rect.max.x)
                .clamp(0.0, sd.physical_size.0 as f32)
                .ceil() as u32;
            let clip_max_y = (sd.scale_factor * clip_rect.max.y)
                .clamp(0.0, sd.physical_size.1 as f32)
                .ceil() as u32;

            if clip_max_x <= clip_min_x || clip_max_y == clip_min_y {
                continue;
            }

            pc.set_scissor_rect(&blade_graphics::ScissorRect {
                x: clip_min_x,
                y: clip_min_y,
                w: clip_max_x - clip_min_x,
                h: clip_max_y - clip_min_y,
            });

            if let egui::epaint::Primitive::Mesh(ref mesh) = clipped_prim.primitive {
                let texture = self.textures.get(&mesh.texture_id).unwrap();
                let index_buf = self.belt.alloc_data(&mesh.indices, context);
                let vertex_buf = self.belt.alloc_data(&mesh.vertices, context);

                pc.bind(
                    1,
                    &Locals {
                        r_vertex_data: vertex_buf,
                        r_texture: texture.view,
                    },
                );

                pc.draw_indexed(
                    index_buf,
                    blade_graphics::IndexType::U32,
                    mesh.indices.len() as u32,
                    0,
                    0,
                    1,
                );
            }
        }
    }

    /// Call this after submitting work at the given `sync_point`.
    #[profiling::function]
    pub fn after_submit(&mut self, sync_point: &blade_graphics::SyncPoint) {
        self.textures_to_delete.extend(
            self.textures_dropped
                .drain(..)
                .map(|texture| (texture, sync_point.clone())),
        );
        self.belt.flush(sync_point);
    }
}