aetna-vulkano 0.3.6

Aetna — Vulkan backend via vulkano + naga WGSL→SPIR-V (native only)
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
//! GPU compositing for app-owned [`AppTexture`]s on the vulkano
//! backend. Mirrors `aetna-wgpu/src/surface.rs`.
//!
//! Three pipelines are built up front (one per [`SurfaceAlpha`])
//! sharing a single `stock::surface` shader module; the dispatch loop
//! picks the matching pipeline per run. Bind groups (descriptor sets
//! here) are cached on [`AppTextureId`]; cache entries unreferenced
//! for one frame are dropped at flush.

use std::any::Any;
use std::collections::HashMap;
use std::ops::Range;
use std::sync::Arc;

use aetna_core::affine::Affine2;
use aetna_core::paint::PhysicalScissor;
use aetna_core::shader::stock_wgsl;
use aetna_core::surface::{
    AppTexture, AppTextureBackend, AppTextureId, SurfaceAlpha, SurfaceFormat, next_app_texture_id,
};
use aetna_core::tree::Rect;
use bytemuck::{Pod, Zeroable};
use vulkano::{
    buffer::{
        BufferUsage, Subbuffer,
        allocator::{SubbufferAllocator, SubbufferAllocatorCreateInfo},
    },
    descriptor_set::{
        DescriptorSet, WriteDescriptorSet, allocator::StandardDescriptorSetAllocator,
    },
    device::Device,
    format::Format,
    image::{
        Image as VkImage, ImageAspects, ImageSubresourceRange, ImageUsage,
        sampler::{Filter, Sampler, SamplerAddressMode, SamplerCreateInfo, SamplerMipmapMode},
        view::{ImageView, ImageViewCreateInfo},
    },
    memory::allocator::{MemoryTypeFilter, StandardMemoryAllocator},
    pipeline::{
        DynamicState, GraphicsPipeline, Pipeline, PipelineShaderStageCreateInfo,
        graphics::{
            GraphicsPipelineCreateInfo,
            color_blend::{
                AttachmentBlend, BlendFactor, BlendOp, ColorBlendAttachmentState, ColorBlendState,
            },
            input_assembly::{InputAssemblyState, PrimitiveTopology},
            rasterization::RasterizationState,
            subpass::PipelineSubpassType,
            vertex_input::{
                VertexInputAttributeDescription, VertexInputBindingDescription, VertexInputRate,
                VertexInputState,
            },
            viewport::ViewportState,
        },
    },
    render_pass::Subpass,
    shader::{ShaderModule, ShaderModuleCreateInfo},
};

use crate::naga_compile::wgsl_to_spirv;
use crate::pipeline::{build_shared_pipeline_layout, multisample_state};

const INSTANCE_ARENA_SIZE: u64 = 32 * 1024;

#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable, Debug)]
pub(crate) struct SurfaceInstance {
    rect: [f32; 4],
    matrix: [f32; 4],
    translation: [f32; 2],
}

pub(crate) struct SurfaceRun {
    pub texture_idx: usize,
    pub scissor: Option<PhysicalScissor>,
    pub alpha: SurfaceAlpha,
    pub first: u32,
    pub count: u32,
}

struct CachedDescriptor {
    descriptor_set: Arc<DescriptorSet>,
    last_used_frame: u64,
}

pub(crate) struct SurfacePaint {
    instances: Vec<SurfaceInstance>,
    instance_alloc: SubbufferAllocator,
    instance_buf: Option<Subbuffer<[SurfaceInstance]>>,
    runs: Vec<SurfaceRun>,

    pipeline_premul: Arc<GraphicsPipeline>,
    pipeline_straight: Arc<GraphicsPipeline>,
    pipeline_opaque: Arc<GraphicsPipeline>,
    sampler: Arc<Sampler>,

    cache: HashMap<u64, CachedDescriptor>,
    bind_group_lookup: Vec<u64>,
    frame_counter: u64,

    descriptor_alloc: Arc<StandardDescriptorSetAllocator>,
}

impl SurfacePaint {
    pub(crate) fn new(
        device: Arc<Device>,
        memory_alloc: Arc<StandardMemoryAllocator>,
        descriptor_alloc: Arc<StandardDescriptorSetAllocator>,
        subpass: Subpass,
        sample_count: u32,
    ) -> Self {
        let (pipeline_premul, pipeline_straight, pipeline_opaque) =
            build_surface_pipelines(device.clone(), subpass, sample_count);
        let sampler = Sampler::new(
            device,
            SamplerCreateInfo {
                mag_filter: Filter::Linear,
                min_filter: Filter::Linear,
                mipmap_mode: SamplerMipmapMode::Linear,
                address_mode: [SamplerAddressMode::ClampToEdge; 3],
                ..Default::default()
            },
        )
        .expect("aetna-vulkano: surface sampler");
        let instance_alloc = SubbufferAllocator::new(
            memory_alloc,
            SubbufferAllocatorCreateInfo {
                arena_size: INSTANCE_ARENA_SIZE,
                buffer_usage: BufferUsage::VERTEX_BUFFER,
                memory_type_filter: MemoryTypeFilter::PREFER_HOST
                    | MemoryTypeFilter::HOST_SEQUENTIAL_WRITE,
                ..Default::default()
            },
        );

        Self {
            instances: Vec::new(),
            instance_alloc,
            instance_buf: None,
            runs: Vec::new(),
            pipeline_premul,
            pipeline_straight,
            pipeline_opaque,
            sampler,
            cache: HashMap::new(),
            bind_group_lookup: Vec::new(),
            frame_counter: 0,
            descriptor_alloc,
        }
    }

    pub(crate) fn frame_begin(&mut self) {
        self.instances.clear();
        self.runs.clear();
        self.bind_group_lookup.clear();
        self.frame_counter = self.frame_counter.wrapping_add(1);
    }

    pub(crate) fn record(
        &mut self,
        rect: Rect,
        scissor: Option<PhysicalScissor>,
        texture: &AppTexture,
        alpha: SurfaceAlpha,
        transform: Affine2,
    ) -> Range<usize> {
        if rect.w <= 0.0 || rect.h <= 0.0 {
            let start = self.runs.len();
            return start..start;
        }
        let start = self.runs.len();
        let texture_idx = self.ensure_descriptor(texture);
        let instance = SurfaceInstance {
            rect: [rect.x, rect.y, rect.w, rect.h],
            matrix: [transform.a, transform.b, transform.c, transform.d],
            translation: [transform.tx, transform.ty],
        };
        let first = self.instances.len() as u32;
        self.instances.push(instance);
        self.runs.push(SurfaceRun {
            texture_idx,
            scissor,
            alpha,
            first,
            count: 1,
        });
        start..self.runs.len()
    }

    fn ensure_descriptor(&mut self, texture: &AppTexture) -> usize {
        let id = texture.id().0;
        if !self.cache.contains_key(&id) {
            let backend = texture.backend();
            let vk_tex = backend
                .as_any()
                .downcast_ref::<VulkanoAppTexture>()
                .unwrap_or_else(|| {
                    panic!(
                        "AppTexture passed to aetna-vulkano was not constructed by \
                         aetna_vulkano::app_texture (actual backend: {}); mixing \
                         backends in one runtime is unsupported",
                        texture.backend_name(),
                    )
                });
            let descriptor_set = DescriptorSet::new(
                self.descriptor_alloc.clone(),
                self.pipeline_premul.layout().set_layouts()[1].clone(),
                [
                    WriteDescriptorSet::image_view(0, vk_tex.view.clone()),
                    WriteDescriptorSet::sampler(1, self.sampler.clone()),
                ],
                [],
            )
            .expect("aetna-vulkano: surface descriptor set");
            self.cache.insert(
                id,
                CachedDescriptor {
                    descriptor_set,
                    last_used_frame: 0,
                },
            );
        }
        let entry = self.cache.get_mut(&id).expect("just inserted");
        entry.last_used_frame = self.frame_counter;
        if let Some(idx) = self.bind_group_lookup.iter().position(|&i| i == id) {
            idx
        } else {
            self.bind_group_lookup.push(id);
            self.bind_group_lookup.len() - 1
        }
    }

    pub(crate) fn flush(&mut self) {
        let frame = self.frame_counter;
        self.cache.retain(|_, v| v.last_used_frame == frame);

        if self.instances.is_empty() {
            self.instance_buf = None;
            return;
        }
        let buf = self
            .instance_alloc
            .allocate_slice::<SurfaceInstance>(self.instances.len() as u64)
            .expect("aetna-vulkano: surface instance suballocate");
        buf.write()
            .expect("aetna-vulkano: surface instance suballocation write")
            .copy_from_slice(&self.instances);
        self.instance_buf = Some(buf);
    }

    pub(crate) fn run(&self, index: usize) -> &SurfaceRun {
        &self.runs[index]
    }

    pub(crate) fn pipeline_for(&self, alpha: SurfaceAlpha) -> &Arc<GraphicsPipeline> {
        match alpha {
            SurfaceAlpha::Premultiplied => &self.pipeline_premul,
            SurfaceAlpha::Straight => &self.pipeline_straight,
            SurfaceAlpha::Opaque => &self.pipeline_opaque,
        }
    }

    /// Per-frame instance suballocation. Panics if called for a frame
    /// with no surface draws — bind sites are gated by
    /// `PaintItem::AppTexture`, which `record(...)` only emits when
    /// `instances` is non-empty.
    pub(crate) fn instance_buf(&self) -> &Subbuffer<[SurfaceInstance]> {
        self.instance_buf
            .as_ref()
            .expect("aetna-vulkano: surface instance_buf accessed with no draws")
    }

    pub(crate) fn descriptor_for_run(&self, run: &SurfaceRun) -> &Arc<DescriptorSet> {
        let id = self.bind_group_lookup[run.texture_idx];
        &self
            .cache
            .get(&id)
            .expect("cache entry alive for the frame")
            .descriptor_set
    }
}

fn build_surface_pipelines(
    device: Arc<Device>,
    subpass: Subpass,
    sample_count: u32,
) -> (
    Arc<GraphicsPipeline>,
    Arc<GraphicsPipeline>,
    Arc<GraphicsPipeline>,
) {
    let words = wgsl_to_spirv("stock::surface", stock_wgsl::SURFACE)
        .expect("aetna-vulkano: surface WGSL compile");
    let module = unsafe {
        ShaderModule::new(device.clone(), ShaderModuleCreateInfo::new(&words))
            .expect("aetna-vulkano: surface ShaderModule::new")
    };
    let premul = build_one(
        device.clone(),
        subpass.clone(),
        sample_count,
        &module,
        "fs_premul",
        Some(premultiplied_blend()),
    );
    let straight = build_one(
        device.clone(),
        subpass.clone(),
        sample_count,
        &module,
        "fs_straight",
        Some(premultiplied_blend()),
    );
    let opaque = build_one(
        device,
        subpass,
        sample_count,
        &module,
        "fs_opaque",
        Some(opaque_blend()),
    );
    (premul, straight, opaque)
}

fn build_one(
    device: Arc<Device>,
    subpass: Subpass,
    sample_count: u32,
    module: &Arc<ShaderModule>,
    fs_entry: &'static str,
    blend: Option<AttachmentBlend>,
) -> Arc<GraphicsPipeline> {
    let vs = module
        .entry_point("vs_main")
        .expect("surface.wgsl: missing vs_main");
    let fs = module
        .entry_point(fs_entry)
        .unwrap_or_else(|| panic!("surface.wgsl: missing {fs_entry}"));
    let stages = [
        PipelineShaderStageCreateInfo::new(vs),
        PipelineShaderStageCreateInfo::new(fs),
    ];
    let layout = build_shared_pipeline_layout(device.clone(), &stages);

    let bind_vertex = VertexInputBindingDescription {
        stride: (2 * std::mem::size_of::<f32>()) as u32,
        input_rate: VertexInputRate::Vertex,
        ..Default::default()
    };
    let bind_instance = VertexInputBindingDescription {
        stride: std::mem::size_of::<SurfaceInstance>() as u32,
        input_rate: VertexInputRate::Instance { divisor: 1 },
        ..Default::default()
    };
    let attr = |binding: u32, offset: u32, format: Format| VertexInputAttributeDescription {
        binding,
        offset,
        format,
        ..Default::default()
    };
    let vertex_input_state = VertexInputState::new()
        .binding(0, bind_vertex)
        .binding(1, bind_instance)
        .attribute(0, attr(0, 0, Format::R32G32_SFLOAT))
        // location 1: rect @ offset 0 (4*f32 = 16)
        .attribute(1, attr(1, 0, Format::R32G32B32A32_SFLOAT))
        // location 2: affine matrix @ offset 16 (4*f32 = 16)
        .attribute(2, attr(1, 16, Format::R32G32B32A32_SFLOAT))
        // location 3: affine translation @ offset 32 (2*f32 = 8)
        .attribute(3, attr(1, 32, Format::R32G32_SFLOAT));

    GraphicsPipeline::new(
        device,
        None,
        GraphicsPipelineCreateInfo {
            stages: stages.into_iter().collect(),
            vertex_input_state: Some(vertex_input_state),
            input_assembly_state: Some(InputAssemblyState {
                topology: PrimitiveTopology::TriangleStrip,
                ..Default::default()
            }),
            viewport_state: Some(ViewportState::default()),
            rasterization_state: Some(RasterizationState::default()),
            multisample_state: Some(multisample_state(sample_count)),
            color_blend_state: Some(ColorBlendState::with_attachment_states(
                subpass.num_color_attachments(),
                ColorBlendAttachmentState {
                    blend,
                    ..Default::default()
                },
            )),
            dynamic_state: [DynamicState::Viewport, DynamicState::Scissor]
                .into_iter()
                .collect(),
            subpass: Some(PipelineSubpassType::BeginRenderPass(subpass)),
            ..GraphicsPipelineCreateInfo::layout(layout)
        },
    )
    .expect("aetna-vulkano: surface GraphicsPipeline::new")
}

fn premultiplied_blend() -> AttachmentBlend {
    AttachmentBlend {
        src_color_blend_factor: BlendFactor::One,
        dst_color_blend_factor: BlendFactor::OneMinusSrcAlpha,
        color_blend_op: BlendOp::Add,
        src_alpha_blend_factor: BlendFactor::One,
        dst_alpha_blend_factor: BlendFactor::OneMinusSrcAlpha,
        alpha_blend_op: BlendOp::Add,
    }
}

fn opaque_blend() -> AttachmentBlend {
    AttachmentBlend {
        src_color_blend_factor: BlendFactor::One,
        dst_color_blend_factor: BlendFactor::Zero,
        color_blend_op: BlendOp::Add,
        src_alpha_blend_factor: BlendFactor::One,
        dst_alpha_blend_factor: BlendFactor::Zero,
        alpha_blend_op: BlendOp::Add,
    }
}

// ---- Public AppTexture constructor ----

/// Concrete vulkano-side [`AppTextureBackend`]. Holds the image + a
/// default-view + cached id so the runtime can downcast and bind the
/// view directly into a descriptor set.
#[derive(Debug)]
pub struct VulkanoAppTexture {
    /// The app-owned image. Held as `Arc` so `AppTexture` can be
    /// cheaply cloned into the El tree without releasing the GPU
    /// resource.
    pub image: Arc<VkImage>,
    /// Default 2D view over the full image, created once at
    /// construction so the per-frame record path doesn't allocate.
    pub view: Arc<ImageView>,
    id: AppTextureId,
    size: (u32, u32),
    format: SurfaceFormat,
}

impl AppTextureBackend for VulkanoAppTexture {
    fn id(&self) -> AppTextureId {
        self.id
    }
    fn size_px(&self) -> (u32, u32) {
        self.size
    }
    fn format(&self) -> SurfaceFormat {
        self.format
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// Wrap an app-allocated `vulkano::image::Image` for compositing via a
/// [`aetna_core::tree::surface`] widget.
///
/// The image must have `SAMPLED` usage and one of the three supported
/// RGBA8 formats: `R8G8B8A8_SRGB`, `B8G8R8A8_SRGB`, or
/// `R8G8B8A8_UNORM`. Sample count must be 1.
///
/// # Panics
///
/// Panics if the image is missing `SAMPLED` usage, its format is outside
/// the supported set, or its sample count is not 1. These are app-side
/// mistakes, not runtime errors — fail loudly rather than silently
/// miscompositing.
pub fn app_texture(image: Arc<VkImage>) -> AppTexture {
    let format = match image.format() {
        Format::R8G8B8A8_SRGB => SurfaceFormat::Rgba8UnormSrgb,
        Format::B8G8R8A8_SRGB => SurfaceFormat::Bgra8UnormSrgb,
        Format::R8G8B8A8_UNORM => SurfaceFormat::Rgba8Unorm,
        f => panic!(
            "aetna_vulkano::app_texture: unsupported image format {:?} \
             (expected R8G8B8A8_SRGB / B8G8R8A8_SRGB / R8G8B8A8_UNORM)",
            f
        ),
    };
    assert!(
        image.usage().intersects(ImageUsage::SAMPLED),
        "aetna_vulkano::app_texture: source image must include SAMPLED usage (got {:?})",
        image.usage(),
    );
    let samples = image.samples();
    assert_eq!(
        samples as u32, 1,
        "aetna_vulkano::app_texture: source image must be single-sampled (got {:?})",
        samples,
    );
    let extent = image.extent();
    let size = (extent[0], extent[1]);
    let view = ImageView::new(
        image.clone(),
        ImageViewCreateInfo {
            subresource_range: ImageSubresourceRange {
                aspects: ImageAspects::COLOR,
                mip_levels: 0..1,
                array_layers: 0..1,
            },
            ..ImageViewCreateInfo::from_image(&image)
        },
    )
    .expect("aetna-vulkano: app_texture image view");
    AppTexture::from_backend(Arc::new(VulkanoAppTexture {
        image,
        view,
        id: next_app_texture_id(),
        size,
        format,
    }))
}