concinnity-device 0.19.23

GPU backends (Metal, Vulkan, DirectX) behind a device facade for Concinnity
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
// src/vulkan/render_pass.rs
//
// Vulkan render-pass construction for the main, shadow, composite, and
// bloom passes.
use ash::vk;

use crate::vulkan::owned::{OwnedRenderPass, VkDevice};

pub(super) fn create_main_render_pass(
    device: &VkDevice,
    format: vk::Format,
    msaa: vk::SampleCountFlags,
) -> Result<OwnedRenderPass, String> {
    let multisampled = msaa != vk::SampleCountFlags::TYPE_1;

    // When multisampled the resolve attachment ends shader-readable; the MSAA
    // colour image is transient. When single-sampled, attachment [0] is itself
    // the resolve image, so it ends shader-readable.
    let color_final_layout = if multisampled {
        vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL
    } else {
        vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL
    };

    let mut attachments = vec![
        // [0] color (MSAA HDR, or the single-sample HDR resolve image)
        vk::AttachmentDescription::default()
            .format(format)
            .samples(msaa)
            .load_op(vk::AttachmentLoadOp::CLEAR)
            .store_op(if multisampled {
                vk::AttachmentStoreOp::DONT_CARE
            } else {
                vk::AttachmentStoreOp::STORE
            })
            .stencil_load_op(vk::AttachmentLoadOp::DONT_CARE)
            .stencil_store_op(vk::AttachmentStoreOp::DONT_CARE)
            .initial_layout(vk::ImageLayout::UNDEFINED)
            .final_layout(color_final_layout),
        // [1] depth: STORE because the projected-decal and volumetric-fog
        // passes sample this attachment after the main pass ends. Without
        // STORE the contents are spec-undefined post-pass: tilers may
        // discard them and the depth-read in fog returns garbage values
        // that bias toward the `depth >= 1.0` "no scene hit" branch,
        // producing per-pixel bright sparkles after bloom.
        vk::AttachmentDescription::default()
            .format(vk::Format::D32_SFLOAT)
            .samples(msaa)
            .load_op(vk::AttachmentLoadOp::CLEAR)
            .store_op(vk::AttachmentStoreOp::STORE)
            .stencil_load_op(vk::AttachmentLoadOp::DONT_CARE)
            .stencil_store_op(vk::AttachmentStoreOp::DONT_CARE)
            .initial_layout(vk::ImageLayout::UNDEFINED)
            .final_layout(vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
    ];
    if multisampled {
        // [2] resolve target (single-sample HDR resolve image)
        attachments.push(
            vk::AttachmentDescription::default()
                .format(format)
                .samples(vk::SampleCountFlags::TYPE_1)
                .load_op(vk::AttachmentLoadOp::DONT_CARE)
                .store_op(vk::AttachmentStoreOp::STORE)
                .stencil_load_op(vk::AttachmentLoadOp::DONT_CARE)
                .stencil_store_op(vk::AttachmentStoreOp::DONT_CARE)
                .initial_layout(vk::ImageLayout::UNDEFINED)
                .final_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL),
        );
    }

    let color_ref = vk::AttachmentReference::default()
        .attachment(0)
        .layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL);
    let depth_ref = vk::AttachmentReference::default()
        .attachment(1)
        .layout(vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
    let resolve_ref = vk::AttachmentReference::default()
        .attachment(2)
        .layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL);

    let mut subpass = vk::SubpassDescription::default()
        .pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS)
        .color_attachments(std::slice::from_ref(&color_ref))
        .depth_stencil_attachment(&depth_ref);
    if multisampled {
        subpass = subpass.resolve_attachments(std::slice::from_ref(&resolve_ref));
    }

    let dependency = vk::SubpassDependency::default()
        .src_subpass(vk::SUBPASS_EXTERNAL)
        .dst_subpass(0)
        .src_stage_mask(
            vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT
                | vk::PipelineStageFlags::EARLY_FRAGMENT_TESTS,
        )
        .src_access_mask(vk::AccessFlags::empty())
        .dst_stage_mask(
            vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT
                | vk::PipelineStageFlags::EARLY_FRAGMENT_TESTS,
        )
        .dst_access_mask(
            vk::AccessFlags::COLOR_ATTACHMENT_WRITE
                | vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_WRITE,
        );

    let rp_info = vk::RenderPassCreateInfo::default()
        .attachments(&attachments)
        .subpasses(std::slice::from_ref(&subpass))
        .dependencies(std::slice::from_ref(&dependency));

    device
        .create_render_pass(&rp_info)
        .map_err(|e| format!("main render pass: {e}"))
}

// Main-pass render pass for two-pass occlusion culling. Two variants share
// this builder, selected by `load`:
//
//   * `load = false` (phase 1, `Main`): same as `create_main_render_pass` but
//     the MSAA colour is STORE'd (not DONT_CARE) so the phase-2 pass can load
//     the samples back and composite onto them. Colour ends
//     COLOR_ATTACHMENT_OPTIMAL so `Main2` loads it directly.
//   * `load = true` (phase 2, `Main2`): loads (does not clear) the phase-1
//     colour + depth, redraws the disoccluded statics, and resolves the
//     combined scene into `hdr_resolve` for the post stack.
//
// Both are render-pass-compatible with the existing main framebuffers (same
// attachment count / formats / sample counts), so no extra framebuffers are
// needed. `Main` (phase 1) still resolves into `hdr_resolve`; that resolve is
// overwritten by `Main2`'s and nothing reads it in between (HizBuild / Cull2
// are compute), so the combined result is what the post stack sees.
pub(super) fn create_main_render_pass_two_pass(
    device: &VkDevice,
    format: vk::Format,
    msaa: vk::SampleCountFlags,
    load: bool,
) -> Result<OwnedRenderPass, String> {
    let multisampled = msaa != vk::SampleCountFlags::TYPE_1;

    // Phase 1 leaves the colour in COLOR_ATTACHMENT_OPTIMAL for phase 2 to
    // load. Phase 2 ends shader-readable when single-sampled (the colour image
    // is itself the resolve target the post stack samples); when multisampled
    // the resolve attachment carries that role and the MSAA colour stays
    // transient in COLOR_ATTACHMENT_OPTIMAL.
    let color_final_layout = if multisampled || !load {
        vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL
    } else {
        vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL
    };
    let color_load_op = if load {
        vk::AttachmentLoadOp::LOAD
    } else {
        vk::AttachmentLoadOp::CLEAR
    };
    let color_initial_layout = if load {
        vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL
    } else {
        vk::ImageLayout::UNDEFINED
    };
    let depth_load_op = if load {
        vk::AttachmentLoadOp::LOAD
    } else {
        vk::AttachmentLoadOp::CLEAR
    };
    let depth_initial_layout = if load {
        vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL
    } else {
        vk::ImageLayout::UNDEFINED
    };

    let mut attachments = vec![
        // [0] colour (MSAA HDR, or the single-sample HDR resolve image). STORE
        // unconditionally: phase 1 must keep the MSAA samples for phase 2's
        // load, and the single-sample image is the post-stack input.
        vk::AttachmentDescription::default()
            .format(format)
            .samples(msaa)
            .load_op(color_load_op)
            .store_op(vk::AttachmentStoreOp::STORE)
            .stencil_load_op(vk::AttachmentLoadOp::DONT_CARE)
            .stencil_store_op(vk::AttachmentStoreOp::DONT_CARE)
            .initial_layout(color_initial_layout)
            .final_layout(color_final_layout),
        // [1] depth: STORE (HizBuild reduces it, the post decals/fog sample it,
        // and phase 2 depth-tests against it).
        vk::AttachmentDescription::default()
            .format(vk::Format::D32_SFLOAT)
            .samples(msaa)
            .load_op(depth_load_op)
            .store_op(vk::AttachmentStoreOp::STORE)
            .stencil_load_op(vk::AttachmentLoadOp::DONT_CARE)
            .stencil_store_op(vk::AttachmentStoreOp::DONT_CARE)
            .initial_layout(depth_initial_layout)
            .final_layout(vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
    ];
    if multisampled {
        // [2] resolve target (single-sample HDR resolve image). Always
        // overwritten by the resolve, so its initial layout is irrelevant.
        attachments.push(
            vk::AttachmentDescription::default()
                .format(format)
                .samples(vk::SampleCountFlags::TYPE_1)
                .load_op(vk::AttachmentLoadOp::DONT_CARE)
                .store_op(vk::AttachmentStoreOp::STORE)
                .stencil_load_op(vk::AttachmentLoadOp::DONT_CARE)
                .stencil_store_op(vk::AttachmentStoreOp::DONT_CARE)
                .initial_layout(vk::ImageLayout::UNDEFINED)
                .final_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL),
        );
    }

    let color_ref = vk::AttachmentReference::default()
        .attachment(0)
        .layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL);
    let depth_ref = vk::AttachmentReference::default()
        .attachment(1)
        .layout(vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
    let resolve_ref = vk::AttachmentReference::default()
        .attachment(2)
        .layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL);

    let mut subpass = vk::SubpassDescription::default()
        .pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS)
        .color_attachments(std::slice::from_ref(&color_ref))
        .depth_stencil_attachment(&depth_ref);
    if multisampled {
        subpass = subpass.resolve_attachments(std::slice::from_ref(&resolve_ref));
    }

    // Use the EXACT same subpass dependency as `create_main_render_pass`. Both
    // two-pass variants share the main framebuffers + the bindless pipeline,
    // which were created against `main_render_pass`; render-pass compatibility
    // (validated on `vkCmdBeginRenderPass` / `vkCmdDraw`) treats differing
    // dependencies as incompatible, so the dependency must match. Phase 2's
    // LOAD of the phase-1 colour + depth is instead ordered by an explicit
    // `vkCmdPipelineBarrier` in `encode_main_pass_phase2` (this backend owns
    // its cross-pass sync inline anyway).
    let dependency = vk::SubpassDependency::default()
        .src_subpass(vk::SUBPASS_EXTERNAL)
        .dst_subpass(0)
        .src_stage_mask(
            vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT
                | vk::PipelineStageFlags::EARLY_FRAGMENT_TESTS,
        )
        .src_access_mask(vk::AccessFlags::empty())
        .dst_stage_mask(
            vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT
                | vk::PipelineStageFlags::EARLY_FRAGMENT_TESTS,
        )
        .dst_access_mask(
            vk::AccessFlags::COLOR_ATTACHMENT_WRITE
                | vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_WRITE,
        );

    let rp_info = vk::RenderPassCreateInfo::default()
        .attachments(&attachments)
        .subpasses(std::slice::from_ref(&subpass))
        .dependencies(std::slice::from_ref(&dependency));

    device
        .create_render_pass(&rp_info)
        .map_err(|e| format!("two-pass main render pass: {e}"))
}

pub(super) fn create_shadow_render_pass(device: &VkDevice) -> Result<OwnedRenderPass, String> {
    let attachment = vk::AttachmentDescription::default()
        .format(vk::Format::D32_SFLOAT)
        .samples(vk::SampleCountFlags::TYPE_1)
        .load_op(vk::AttachmentLoadOp::CLEAR)
        .store_op(vk::AttachmentStoreOp::STORE)
        .stencil_load_op(vk::AttachmentLoadOp::DONT_CARE)
        .stencil_store_op(vk::AttachmentStoreOp::DONT_CARE)
        .initial_layout(vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL)
        .final_layout(vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL);

    let depth_ref = vk::AttachmentReference::default()
        .attachment(0)
        .layout(vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL);

    let subpass = vk::SubpassDescription::default()
        .pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS)
        .depth_stencil_attachment(&depth_ref);

    let rp_info = vk::RenderPassCreateInfo::default()
        .attachments(std::slice::from_ref(&attachment))
        .subpasses(std::slice::from_ref(&subpass));

    device
        .create_render_pass(&rp_info)
        .map_err(|e| format!("shadow render pass: {e}"))
}

// Composite render pass. A single subpass renders the fullscreen tonemap +
// FXAA triangle (and the text overlay) into the swapchain backbuffer.
pub(super) fn create_composite_render_pass(
    device: &VkDevice,
    swapchain_format: vk::Format,
) -> Result<OwnedRenderPass, String> {
    // The fullscreen triangle overwrites every pixel, so the backbuffer is
    // not cleared or loaded.
    let attachment = vk::AttachmentDescription::default()
        .format(swapchain_format)
        .samples(vk::SampleCountFlags::TYPE_1)
        .load_op(vk::AttachmentLoadOp::DONT_CARE)
        .store_op(vk::AttachmentStoreOp::STORE)
        .stencil_load_op(vk::AttachmentLoadOp::DONT_CARE)
        .stencil_store_op(vk::AttachmentStoreOp::DONT_CARE)
        .initial_layout(vk::ImageLayout::UNDEFINED)
        .final_layout(vk::ImageLayout::PRESENT_SRC_KHR);

    let color_ref = vk::AttachmentReference::default()
        .attachment(0)
        .layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL);

    let subpass = vk::SubpassDescription::default()
        .pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS)
        .color_attachments(std::slice::from_ref(&color_ref));

    // External dependency: the composite fragment shader must wait for the
    // main pass to finish writing (and resolving into) the HDR image, and the
    // backbuffer write must wait for the acquire semaphore's stage.
    let dependency = vk::SubpassDependency::default()
        .src_subpass(vk::SUBPASS_EXTERNAL)
        .dst_subpass(0)
        .src_stage_mask(vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT)
        .src_access_mask(vk::AccessFlags::COLOR_ATTACHMENT_WRITE)
        .dst_stage_mask(
            vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT
                | vk::PipelineStageFlags::FRAGMENT_SHADER,
        )
        .dst_access_mask(vk::AccessFlags::COLOR_ATTACHMENT_WRITE | vk::AccessFlags::SHADER_READ);

    let rp_info = vk::RenderPassCreateInfo::default()
        .attachments(std::slice::from_ref(&attachment))
        .subpasses(std::slice::from_ref(&subpass))
        .dependencies(std::slice::from_ref(&dependency));

    device
        .create_render_pass(&rp_info)
        .map_err(|e| format!("composite render pass: {e}"))
}

// Create the off-screen HDR attachment set, `count` slots deep (one per
// frame-in-flight). Returns `(msaa_color, depth, hdr_resolve)`; `msaa_color`
// is empty when MSAA is disabled, in which case the main pass renders
// straight into the resolve image.
// A bloom-chain render pass: one single-sample HDR colour attachment, no
// depth. With `load` set the attachment is loaded (the additive upsample
// blends onto existing content) and its initial layout is the
// shader-readable layout the prior write pass left it in; otherwise the
// attachment is discarded on load. Either way it ends `SHADER_READ_ONLY`.
pub(super) fn create_bloom_render_pass(
    device: &VkDevice,
    format: vk::Format,
    load: bool,
) -> Result<OwnedRenderPass, String> {
    let attachment = vk::AttachmentDescription::default()
        .format(format)
        .samples(vk::SampleCountFlags::TYPE_1)
        .load_op(if load {
            vk::AttachmentLoadOp::LOAD
        } else {
            vk::AttachmentLoadOp::DONT_CARE
        })
        .store_op(vk::AttachmentStoreOp::STORE)
        .stencil_load_op(vk::AttachmentLoadOp::DONT_CARE)
        .stencil_store_op(vk::AttachmentStoreOp::DONT_CARE)
        .initial_layout(if load {
            vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL
        } else {
            vk::ImageLayout::UNDEFINED
        })
        .final_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL);

    let color_ref = vk::AttachmentReference::default()
        .attachment(0)
        .layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL);

    let subpass = vk::SubpassDescription::default()
        .pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS)
        .color_attachments(std::slice::from_ref(&color_ref));

    // Order this pass after the prior bloom pass: its fragment shader samples
    // the mip the prior pass wrote, and (in the LOAD case) its own attachment
    // was also written by an earlier pass.
    let dependency = vk::SubpassDependency::default()
        .src_subpass(vk::SUBPASS_EXTERNAL)
        .dst_subpass(0)
        .src_stage_mask(vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT)
        .src_access_mask(vk::AccessFlags::COLOR_ATTACHMENT_WRITE)
        .dst_stage_mask(
            vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT
                | vk::PipelineStageFlags::FRAGMENT_SHADER,
        )
        .dst_access_mask(
            vk::AccessFlags::COLOR_ATTACHMENT_WRITE
                | vk::AccessFlags::COLOR_ATTACHMENT_READ
                | vk::AccessFlags::SHADER_READ,
        );

    let rp_info = vk::RenderPassCreateInfo::default()
        .attachments(std::slice::from_ref(&attachment))
        .subpasses(std::slice::from_ref(&subpass))
        .dependencies(std::slice::from_ref(&dependency));

    device
        .create_render_pass(&rp_info)
        .map_err(|e| format!("bloom render pass: {e}"))
}