pilka 0.8.2

Another live-coding tool for creating shaders demos.
Documentation
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
use ahash::{AHashMap, AHashSet};
use anyhow::Result;
use either::Either;
use slotmap::SlotMap;
use std::{
    path::{Path, PathBuf},
    sync::Arc,
};

use ash::{
    prelude::VkResult,
    vk::{self},
};

use crate::{Device, ShaderCompiler, ShaderKind, ShaderSource, Watcher};

pub struct ComputePipeline {
    pub layout: vk::PipelineLayout,
    pub pipeline: vk::Pipeline,
    shader_path: PathBuf,
    device: Arc<Device>,
}

impl Drop for ComputePipeline {
    fn drop(&mut self) {
        unsafe {
            self.device.destroy_pipeline(self.pipeline, None);
            self.device.destroy_pipeline_layout(self.layout, None);
        }
    }
}

impl ComputePipeline {
    fn new(
        device: &Arc<Device>,
        shader_compiler: &ShaderCompiler,
        shader_path: impl AsRef<Path>,
        push_constant_ranges: &[vk::PushConstantRange],
        descriptor_set_layouts: &[vk::DescriptorSetLayout],
    ) -> Result<Self> {
        let cs_bytes = shader_compiler.compile(&shader_path, shaderc::ShaderKind::Compute)?;

        let pipeline_layout = unsafe {
            device.create_pipeline_layout(
                &vk::PipelineLayoutCreateInfo::default()
                    .set_layouts(descriptor_set_layouts)
                    .push_constant_ranges(push_constant_ranges),
                None,
            )?
        };

        let mut shader_module = vk::ShaderModuleCreateInfo::default().code(cs_bytes.as_binary());
        let shader_stage = vk::PipelineShaderStageCreateInfo::default()
            .stage(vk::ShaderStageFlags::COMPUTE)
            .name(c"main")
            .push_next(&mut shader_module);

        let create_info = vk::ComputePipelineCreateInfo::default()
            .layout(pipeline_layout)
            .stage(shader_stage);
        let pipeline = unsafe {
            device.create_compute_pipelines(vk::PipelineCache::null(), &[create_info], None)
        };
        let pipeline = pipeline.map_err(|(_, err)| err)?[0];

        Ok(Self {
            pipeline,
            shader_path: shader_path.as_ref().to_path_buf(),
            layout: pipeline_layout,
            device: device.clone(),
        })
    }

    pub fn reload(&mut self, shader_compiler: &ShaderCompiler) -> Result<()> {
        let cs_bytes = shader_compiler.compile(&self.shader_path, shaderc::ShaderKind::Compute)?;

        unsafe { self.device.destroy_pipeline(self.pipeline, None) }

        let mut shader_module = vk::ShaderModuleCreateInfo::default().code(cs_bytes.as_binary());
        let shader_stage = vk::PipelineShaderStageCreateInfo::default()
            .stage(vk::ShaderStageFlags::COMPUTE)
            .name(c"main")
            .push_next(&mut shader_module);

        let create_info = vk::ComputePipelineCreateInfo::default()
            .layout(self.layout)
            .stage(shader_stage);
        let pipeline = unsafe {
            self.device
                .create_compute_pipelines(vk::PipelineCache::null(), &[create_info], None)
        };
        let pipeline = pipeline.map_err(|(_, err)| err)?[0];

        self.pipeline = pipeline;

        Ok(())
    }
}

pub struct VertexInputDesc {
    pub primitive_topology: vk::PrimitiveTopology,
    pub primitive_restart: bool,
}

impl Default for VertexInputDesc {
    fn default() -> Self {
        Self {
            primitive_topology: vk::PrimitiveTopology::TRIANGLE_LIST,
            primitive_restart: false,
        }
    }
}

pub struct VertexShaderDesc {
    pub shader_path: PathBuf,
    pub dynamic_state: Vec<vk::DynamicState>,
    pub line_width: f32,
    pub polygon_mode: vk::PolygonMode,
    pub cull_mode: vk::CullModeFlags,
    pub front_face: vk::FrontFace,
    pub viewport_count: u32,
    pub scissot_count: u32,
}

impl Default for VertexShaderDesc {
    fn default() -> Self {
        Self {
            shader_path: PathBuf::new(),
            dynamic_state: vec![vk::DynamicState::VIEWPORT, vk::DynamicState::SCISSOR],
            line_width: 1.0,
            polygon_mode: vk::PolygonMode::FILL,
            cull_mode: vk::CullModeFlags::BACK,
            front_face: vk::FrontFace::COUNTER_CLOCKWISE,
            viewport_count: 1,
            scissot_count: 1,
        }
    }
}

pub struct FragmentShaderDesc {
    pub shader_path: PathBuf,
}

pub struct FragmentOutputDesc {
    pub surface_format: vk::Format,
    pub multisample_state: vk::SampleCountFlags,
}

impl Default for FragmentOutputDesc {
    fn default() -> Self {
        Self {
            surface_format: vk::Format::B8G8R8A8_SRGB,
            multisample_state: vk::SampleCountFlags::TYPE_1,
        }
    }
}

pub struct RenderPipeline {
    pub layout: vk::PipelineLayout,
    pub pipeline: vk::Pipeline,
    vertex_input_lib: vk::Pipeline,
    vertex_shader_lib: vk::Pipeline,
    fragment_shader_lib: vk::Pipeline,
    fragment_output_lib: vk::Pipeline,
    device: Arc<Device>,
}

impl RenderPipeline {
    pub fn new(
        device: &Arc<Device>,
        shader_compiler: &ShaderCompiler,
        vertex_input_desc: &VertexInputDesc,
        vertex_shader_desc: &VertexShaderDesc,
        fragment_shader_desc: &FragmentShaderDesc,
        fragment_output_desc: &FragmentOutputDesc,
        push_constant_ranges: &[vk::PushConstantRange],
        descriptor_set_layouts: &[vk::DescriptorSetLayout],
    ) -> Result<Self> {
        let vs_bytes = shader_compiler
            .compile(&vertex_shader_desc.shader_path, shaderc::ShaderKind::Vertex)?;
        let fs_bytes = shader_compiler.compile(
            &fragment_shader_desc.shader_path,
            shaderc::ShaderKind::Fragment,
        )?;

        let pipeline_layout = unsafe {
            device.create_pipeline_layout(
                &vk::PipelineLayoutCreateInfo::default()
                    .set_layouts(descriptor_set_layouts)
                    .push_constant_ranges(push_constant_ranges),
                None,
            )?
        };

        use vk::GraphicsPipelineLibraryFlagsEXT as GPF;
        let vertex_input_lib = {
            let input_ass = vk::PipelineInputAssemblyStateCreateInfo::default()
                .topology(vertex_input_desc.primitive_topology)
                .primitive_restart_enable(vertex_input_desc.primitive_restart);
            let vertex_input = vk::PipelineVertexInputStateCreateInfo::default();

            create_library(device, GPF::VERTEX_INPUT_INTERFACE, |desc| {
                desc.vertex_input_state(&vertex_input)
                    .input_assembly_state(&input_ass)
            })?
        };

        let vertex_shader_lib = {
            let mut shader_module =
                vk::ShaderModuleCreateInfo::default().code(vs_bytes.as_binary());
            let shader_stage = vk::PipelineShaderStageCreateInfo::default()
                .stage(vk::ShaderStageFlags::VERTEX)
                .name(c"main")
                .push_next(&mut shader_module);
            let dynamic_state = vk::PipelineDynamicStateCreateInfo::default()
                .dynamic_states(&vertex_shader_desc.dynamic_state);
            let rasterization_state = vk::PipelineRasterizationStateCreateInfo::default()
                .line_width(vertex_shader_desc.line_width)
                .polygon_mode(vertex_shader_desc.polygon_mode)
                .cull_mode(vertex_shader_desc.cull_mode)
                .front_face(vertex_shader_desc.front_face);
            let viewport_state = vk::PipelineViewportStateCreateInfo::default()
                .viewport_count(vertex_shader_desc.viewport_count)
                .scissor_count(vertex_shader_desc.scissot_count);

            create_library(device, GPF::PRE_RASTERIZATION_SHADERS, |desc| {
                desc.layout(pipeline_layout)
                    .stages(std::slice::from_ref(&shader_stage))
                    .dynamic_state(&dynamic_state)
                    .viewport_state(&viewport_state)
                    .rasterization_state(&rasterization_state)
            })?
        };

        let fragment_shader_lib = {
            let mut shader_module =
                vk::ShaderModuleCreateInfo::default().code(fs_bytes.as_binary());
            let shader_stage = vk::PipelineShaderStageCreateInfo::default()
                .stage(vk::ShaderStageFlags::FRAGMENT)
                .name(c"main")
                .push_next(&mut shader_module);

            let depth_stencil_state = vk::PipelineDepthStencilStateCreateInfo::default();

            create_library(device, GPF::FRAGMENT_SHADER, |desc| {
                desc.layout(pipeline_layout)
                    .stages(std::slice::from_ref(&shader_stage))
                    .depth_stencil_state(&depth_stencil_state)
            })?
        };

        let fragment_output_lib = {
            let color_attachment_formats = [fragment_output_desc.surface_format];
            let mut dyn_render = vk::PipelineRenderingCreateInfo::default()
                .color_attachment_formats(&color_attachment_formats);

            let multisample_state = vk::PipelineMultisampleStateCreateInfo::default()
                .rasterization_samples(vk::SampleCountFlags::TYPE_1);

            create_library(device, GPF::FRAGMENT_OUTPUT_INTERFACE, |desc| {
                desc.multisample_state(&multisample_state)
                    .push_next(&mut dyn_render)
            })?
        };

        let pipeline = Self::link_libraries(
            device,
            &pipeline_layout,
            &vertex_input_lib,
            &vertex_shader_lib,
            &fragment_shader_lib,
            &fragment_output_lib,
        )?;

        Ok(Self {
            device: device.clone(),
            layout: pipeline_layout,
            pipeline,
            vertex_input_lib,
            vertex_shader_lib,
            fragment_shader_lib,
            fragment_output_lib,
        })
    }

    pub fn reload_vertex_lib(
        &mut self,
        shader_compiler: &ShaderCompiler,
        shader_path: impl AsRef<Path>,
    ) -> Result<()> {
        let vs_bytes = shader_compiler.compile(shader_path, shaderc::ShaderKind::Vertex)?;

        unsafe { self.device.destroy_pipeline(self.vertex_shader_lib, None) };

        let mut shader_module = vk::ShaderModuleCreateInfo::default().code(vs_bytes.as_binary());
        let shader_stage = vk::PipelineShaderStageCreateInfo::default()
            .stage(vk::ShaderStageFlags::VERTEX)
            .name(c"main")
            .push_next(&mut shader_module);
        let dynamic_state = vk::PipelineDynamicStateCreateInfo::default()
            .dynamic_states(&[vk::DynamicState::VIEWPORT, vk::DynamicState::SCISSOR]);
        let rasterization_state = vk::PipelineRasterizationStateCreateInfo::default()
            .line_width(1.0)
            .polygon_mode(vk::PolygonMode::FILL)
            .cull_mode(vk::CullModeFlags::BACK)
            .front_face(vk::FrontFace::COUNTER_CLOCKWISE);
        let viewport_state = vk::PipelineViewportStateCreateInfo::default()
            .viewport_count(1)
            .scissor_count(1);
        let vertex_shader_lib = create_library(
            &self.device,
            vk::GraphicsPipelineLibraryFlagsEXT::PRE_RASTERIZATION_SHADERS,
            |desc| {
                desc.layout(self.layout)
                    .stages(std::slice::from_ref(&shader_stage))
                    .dynamic_state(&dynamic_state)
                    .viewport_state(&viewport_state)
                    .rasterization_state(&rasterization_state)
            },
        )?;

        self.vertex_shader_lib = vertex_shader_lib;

        Ok(())
    }

    pub fn reload_fragment_lib(
        &mut self,
        shader_compiler: &ShaderCompiler,
        shader_path: impl AsRef<Path>,
    ) -> Result<()> {
        let fs_bytes = shader_compiler.compile(shader_path, shaderc::ShaderKind::Fragment)?;

        unsafe { self.device.destroy_pipeline(self.fragment_shader_lib, None) };

        let mut shader_module = vk::ShaderModuleCreateInfo::default().code(fs_bytes.as_binary());
        let shader_stage = vk::PipelineShaderStageCreateInfo::default()
            .stage(vk::ShaderStageFlags::FRAGMENT)
            .name(c"main")
            .push_next(&mut shader_module);

        let depth_stencil_state = vk::PipelineDepthStencilStateCreateInfo::default();

        let fragment_shader_lib = create_library(
            &self.device,
            vk::GraphicsPipelineLibraryFlagsEXT::FRAGMENT_SHADER,
            |desc| {
                desc.layout(self.layout)
                    .stages(std::slice::from_ref(&shader_stage))
                    .depth_stencil_state(&depth_stencil_state)
            },
        )?;

        self.fragment_shader_lib = fragment_shader_lib;

        Ok(())
    }

    pub fn link(&mut self) -> Result<()> {
        unsafe { self.device.destroy_pipeline(self.pipeline, None) };
        self.pipeline = Self::link_libraries(
            &self.device,
            &self.layout,
            &self.vertex_input_lib,
            &self.vertex_shader_lib,
            &self.fragment_shader_lib,
            &self.fragment_output_lib,
        )?;

        Ok(())
    }

    fn link_libraries(
        device: &ash::Device,
        layout: &vk::PipelineLayout,
        vertex_input_lib: &vk::Pipeline,
        vertex_shader_lib: &vk::Pipeline,
        fragment_shader_lib: &vk::Pipeline,
        fragment_output_lib: &vk::Pipeline,
    ) -> Result<vk::Pipeline> {
        let libraries = [
            *vertex_input_lib,
            *vertex_shader_lib,
            *fragment_shader_lib,
            *fragment_output_lib,
        ];
        let pipeline = {
            let mut linking_info =
                vk::PipelineLibraryCreateInfoKHR::default().libraries(&libraries);
            let pipeline_info = vk::GraphicsPipelineCreateInfo::default()
                .flags(vk::PipelineCreateFlags::LINK_TIME_OPTIMIZATION_EXT)
                .layout(*layout)
                .push_next(&mut linking_info);
            let pipeline = unsafe {
                device.create_graphics_pipelines(vk::PipelineCache::null(), &[pipeline_info], None)
            };
            pipeline.map_err(|(_, err)| err)?[0]
        };

        Ok(pipeline)
    }
}

impl Drop for RenderPipeline {
    fn drop(&mut self) {
        unsafe {
            self.device.destroy_pipeline(self.vertex_input_lib, None);
            self.device.destroy_pipeline(self.vertex_shader_lib, None);
            self.device.destroy_pipeline(self.fragment_shader_lib, None);
            self.device.destroy_pipeline(self.fragment_output_lib, None);
            self.device.destroy_pipeline(self.pipeline, None);
            self.device.destroy_pipeline_layout(self.layout, None);
        }
    }
}

fn create_library<'a, F>(
    device: &ash::Device,
    kind: vk::GraphicsPipelineLibraryFlagsEXT,
    f: F,
) -> VkResult<vk::Pipeline>
where
    F: FnOnce(vk::GraphicsPipelineCreateInfo<'a>) -> vk::GraphicsPipelineCreateInfo<'a>,
{
    let mut library_type = vk::GraphicsPipelineLibraryCreateInfoEXT::default().flags(kind);
    let pipeline = unsafe {
        let pipeline_info = vk::GraphicsPipelineCreateInfo::default().flags(
            vk::PipelineCreateFlags::LIBRARY_KHR
                | vk::PipelineCreateFlags::RETAIN_LINK_TIME_OPTIMIZATION_INFO_EXT,
        );

        // WARN: `let` introduces implicit copy on the struct that contains pointers
        let pipeline_info = f(pipeline_info).push_next(&mut library_type);

        device.create_graphics_pipelines(
            vk::PipelineCache::null(),
            std::slice::from_ref(&pipeline_info),
            None,
        )
    };

    Ok(pipeline.map_err(|(_, err)| err)?[0])
}

slotmap::new_key_type! {
    pub struct RenderHandle;
    pub struct ComputeHandle;
}

pub struct PipelineArena {
    pub render: RenderArena,
    pub compute: ComputeArena,
    pub path_mapping: AHashMap<PathBuf, AHashSet<Either<RenderHandle, ComputeHandle>>>,
    pub shader_compiler: ShaderCompiler,
    file_watcher: Watcher,
    device: Arc<Device>,
}

impl PipelineArena {
    pub fn new(device: &Arc<Device>, file_watcher: Watcher) -> Result<Self> {
        Ok(Self {
            render: RenderArena {
                pipelines: SlotMap::with_key(),
            },
            compute: ComputeArena {
                pipelines: SlotMap::with_key(),
            },
            shader_compiler: ShaderCompiler::new(&file_watcher)?,
            file_watcher,
            path_mapping: AHashMap::new(),
            device: device.clone(),
        })
    }

    pub fn create_compute_pipeline(
        &mut self,
        shader_path: impl AsRef<Path>,
        push_constant_ranges: &[vk::PushConstantRange],
        descriptor_set_layouts: &[vk::DescriptorSetLayout],
    ) -> Result<ComputeHandle> {
        let path = shader_path.as_ref().canonicalize()?;
        {
            self.file_watcher.watch_file(&path)?;
            let mut mapping = self.file_watcher.include_mapping.lock();
            mapping
                .entry(path.clone())
                .or_default()
                .insert(ShaderSource {
                    path: path.clone(),
                    kind: ShaderKind::Compute,
                });
        }
        let pipeline = ComputePipeline::new(
            &self.device,
            &self.shader_compiler,
            &path,
            push_constant_ranges,
            descriptor_set_layouts,
        )?;
        let handle = self.compute.pipelines.insert(pipeline);
        self.path_mapping
            .entry(path)
            .or_default()
            .insert(Either::Right(handle));
        Ok(handle)
    }

    pub fn create_render_pipeline(
        &mut self,
        vertex_input_desc: &VertexInputDesc,
        vertex_shader_desc: &VertexShaderDesc,
        fragment_shader_desc: &FragmentShaderDesc,
        fragment_output_desc: &FragmentOutputDesc,
        push_constant_ranges: &[vk::PushConstantRange],
        descriptor_set_layouts: &[vk::DescriptorSetLayout],
    ) -> Result<RenderHandle> {
        let vs_path = vertex_shader_desc.shader_path.canonicalize()?;
        let fs_path = fragment_shader_desc.shader_path.canonicalize()?;
        for (path, kind) in [
            (vs_path.clone(), ShaderKind::Vertex),
            (fs_path.clone(), ShaderKind::Fragment),
        ] {
            self.file_watcher.watch_file(&path)?;
            let mut mapping = self.file_watcher.include_mapping.lock();
            mapping
                .entry(path.clone())
                .or_default()
                .insert(ShaderSource { path, kind });
        }
        let pipeline = RenderPipeline::new(
            &self.device,
            &self.shader_compiler,
            vertex_input_desc,
            vertex_shader_desc,
            fragment_shader_desc,
            fragment_output_desc,
            push_constant_ranges,
            descriptor_set_layouts,
        )?;
        let handle = self.render.pipelines.insert(pipeline);
        self.path_mapping
            .entry(vs_path)
            .or_default()
            .insert(Either::Left(handle));
        self.path_mapping
            .entry(fs_path)
            .or_default()
            .insert(Either::Left(handle));
        Ok(handle)
    }

    pub fn get_pipeline<H: Handle>(&self, handle: H) -> &H::Pipeline {
        handle.get_pipeline(self)
    }

    pub fn get_pipeline_mut<H: Handle>(&mut self, handle: H) -> &mut H::Pipeline {
        handle.get_pipeline_mut(self)
    }
}

pub struct RenderArena {
    pub pipelines: SlotMap<RenderHandle, RenderPipeline>,
}

pub struct ComputeArena {
    pub pipelines: SlotMap<ComputeHandle, ComputePipeline>,
}

pub trait Handle {
    type Pipeline;
    fn get_pipeline(self, arena: &PipelineArena) -> &Self::Pipeline;
    fn get_pipeline_mut(self, arena: &mut PipelineArena) -> &mut Self::Pipeline;
}

impl Handle for RenderHandle {
    type Pipeline = RenderPipeline;

    fn get_pipeline(self, arena: &PipelineArena) -> &Self::Pipeline {
        &arena.render.pipelines[self]
    }

    fn get_pipeline_mut(self, arena: &mut PipelineArena) -> &mut Self::Pipeline {
        &mut arena.render.pipelines[self]
    }
}

impl Handle for ComputeHandle {
    type Pipeline = ComputePipeline;

    fn get_pipeline(self, arena: &PipelineArena) -> &Self::Pipeline {
        &arena.compute.pipelines[self]
    }

    fn get_pipeline_mut(self, arena: &mut PipelineArena) -> &mut Self::Pipeline {
        &mut arena.compute.pipelines[self]
    }
}