-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathrenderer.rs
More file actions
504 lines (437 loc) · 18.2 KB
/
renderer.rs
File metadata and controls
504 lines (437 loc) · 18.2 KB
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
use crate::{
CompiledRenderGraph, ExecutingRenderGraph, ExportedTemporalRenderGraphState,
PredefinedDescriptorSet, RenderGraphExecutionParams, TemporalRenderGraph,
TemporalRenderGraphState, TemporalResourceState,
};
use kajiya_backend::{
ash::vk,
dynamic_constants::*,
pipeline_cache::*,
rspirv_reflect,
transient_resource_cache::TransientResourceCache,
vk_sync,
vulkan::{self, swapchain::Swapchain, RenderBackend},
Device,
};
#[allow(unused_imports)]
use log::{debug, error, info, trace, warn};
use std::{collections::HashMap, sync::Arc};
use turbosloth::*;
use vulkan::buffer::{Buffer, BufferDesc};
enum TemporalRg {
Inert(TemporalRenderGraphState),
Exported(ExportedTemporalRenderGraphState),
}
impl Default for TemporalRg {
fn default() -> Self {
Self::Inert(Default::default())
}
}
pub struct Renderer {
device: Arc<Device>,
pipeline_cache: PipelineCache,
transient_resource_cache: TransientResourceCache,
dynamic_constants: DynamicConstants,
frame_descriptor_set: vk::DescriptorSet,
compiled_rg: Option<CompiledRenderGraph>,
temporal_rg_state: TemporalRg,
}
lazy_static::lazy_static! {
static ref FRAME_CONSTANTS_LAYOUT: HashMap<u32, rspirv_reflect::DescriptorInfo> = [
// frame_constants
(
0,
rspirv_reflect::DescriptorInfo {
ty: rspirv_reflect::DescriptorType::UNIFORM_BUFFER,
dimensionality: rspirv_reflect::DescriptorDimensionality::Single,
name: Default::default(),
},
),
// instance_dynamic_parameters_dyn
(
1,
rspirv_reflect::DescriptorInfo {
ty: rspirv_reflect::DescriptorType::STORAGE_BUFFER_DYNAMIC,
dimensionality: rspirv_reflect::DescriptorDimensionality::Single,
name: Default::default(),
},
),
// triangle_lights_dyn
(
2,
rspirv_reflect::DescriptorInfo {
ty: rspirv_reflect::DescriptorType::STORAGE_BUFFER_DYNAMIC,
dimensionality: rspirv_reflect::DescriptorDimensionality::Single,
name: Default::default(),
},
),
]
.iter()
.cloned()
.collect();
}
pub struct FrameConstantsLayout {
pub globals_offset: u32,
pub instance_dynamic_parameters_offset: u32,
pub triangle_lights_offset: u32,
}
impl Renderer {
pub fn new(backend: &RenderBackend) -> anyhow::Result<Self> {
let dynamic_constants = DynamicConstants::new({
backend.device.create_buffer(
BufferDesc::new_cpu_to_gpu(
DYNAMIC_CONSTANTS_SIZE_BYTES * DYNAMIC_CONSTANTS_BUFFER_COUNT,
vk::BufferUsageFlags::UNIFORM_BUFFER
| vk::BufferUsageFlags::STORAGE_BUFFER
| vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS,
),
"dynamic constants buffer",
None,
)?
});
let frame_descriptor_set =
Self::create_frame_descriptor_set(backend, &dynamic_constants.buffer);
Ok(Renderer {
device: backend.device.clone(),
dynamic_constants,
frame_descriptor_set,
pipeline_cache: PipelineCache::new(&LazyCache::create()),
transient_resource_cache: Default::default(),
compiled_rg: None,
temporal_rg_state: Default::default(),
})
}
pub fn draw_frame<PrepareFrameConstantsFn>(
&mut self,
prepare_frame_constants: PrepareFrameConstantsFn,
swapchain: &mut Swapchain,
) where
PrepareFrameConstantsFn: FnOnce(&mut DynamicConstants) -> FrameConstantsLayout,
{
let rg = if let Some(rg) = self.compiled_rg.take() {
rg
} else {
return;
};
let device = &*self.device;
let raw_device = &device.raw;
let current_frame = self.device.begin_frame();
// Both command buffers are accessible now, so begin recording.
for cb in [
¤t_frame.main_command_buffer,
¤t_frame.presentation_command_buffer,
] {
unsafe {
raw_device
.reset_command_buffer(cb.raw, vk::CommandBufferResetFlags::default())
.unwrap();
raw_device
.begin_command_buffer(
cb.raw,
&vk::CommandBufferBeginInfo::builder()
.flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT),
)
.unwrap();
}
}
// Now that we can write to GPU data, prepare global frame constants.
let frame_constants_layout = prepare_frame_constants(&mut self.dynamic_constants);
let mut executing_rg: ExecutingRenderGraph;
// Record and submit the main command buffer
{
let main_cb = ¤t_frame.main_command_buffer;
current_frame
.profiler_data
.begin_frame(&device.raw, main_cb.raw);
executing_rg = {
puffin::profile_scope!("rg begin_execute");
rg.begin_execute(
RenderGraphExecutionParams {
device: &self.device,
pipeline_cache: &mut self.pipeline_cache,
frame_descriptor_set: self.frame_descriptor_set,
frame_constants_layout,
profiler_data: ¤t_frame.profiler_data,
},
&mut self.transient_resource_cache,
&mut self.dynamic_constants,
)
};
// Record and submit the main command buffer
unsafe {
puffin::profile_scope!("main cb");
{
puffin::profile_scope!("rg::record_main_cb");
executing_rg.record_main_cb(main_cb)
}
raw_device.end_command_buffer(main_cb.raw).unwrap();
let submit_info = [vk::SubmitInfo::builder()
.command_buffers(std::slice::from_ref(&main_cb.raw))
.build()];
raw_device
.reset_fences(std::slice::from_ref(&main_cb.submit_done_fence))
.expect("reset_fences");
puffin::profile_scope!("submit main cb");
// Try to submit the command buffer to the GPU. We might encounter a GPU crash.
raw_device
.queue_submit(
self.device.universal_queue.raw,
&submit_info,
main_cb.submit_done_fence,
)
.map_err(|err| device.report_error(err.into()))
.expect("main queue_submit failed");
};
}
// Now that we've done the main submission and the GPU is busy, acquire the presentation image.
// This can block, so we're doing it as late as possible.
let swapchain_image = swapchain
.acquire_next_image()
.ok()
.expect("swapchain image");
// Execute the rest of the render graph, and submit the presentation command buffer.
let retired_rg = {
puffin::profile_scope!("presentation cb");
let presentation_cb = ¤t_frame.presentation_command_buffer;
// Transition the swapchain to CS write
vulkan::barrier::record_image_barrier(
device,
presentation_cb.raw,
vulkan::barrier::ImageBarrier::new(
swapchain_image.image.raw,
vk_sync::AccessType::Present,
vk_sync::AccessType::ComputeShaderWrite,
vk::ImageAspectFlags::COLOR,
)
.with_discard(true),
);
let retired_rg =
executing_rg.record_presentation_cb(presentation_cb, swapchain_image.image.clone());
// Transition the swapchain to present
vulkan::barrier::record_image_barrier(
device,
presentation_cb.raw,
vulkan::barrier::ImageBarrier::new(
swapchain_image.image.raw,
vk_sync::AccessType::ComputeShaderWrite,
vk_sync::AccessType::Present,
vk::ImageAspectFlags::COLOR,
),
);
current_frame
.profiler_data
.end_frame(&device.raw, presentation_cb.raw);
// Record and submit the presentation command buffer
unsafe {
raw_device.end_command_buffer(presentation_cb.raw).unwrap();
let submit_info = [vk::SubmitInfo::builder()
.wait_semaphores(std::slice::from_ref(&swapchain_image.acquire_semaphore))
.signal_semaphores(std::slice::from_ref(
&swapchain_image.rendering_finished_semaphore,
))
.wait_dst_stage_mask(&[vk::PipelineStageFlags::COMPUTE_SHADER])
.command_buffers(std::slice::from_ref(&presentation_cb.raw))
.build()];
raw_device
.reset_fences(std::slice::from_ref(&presentation_cb.submit_done_fence))
.expect("reset_fences");
puffin::profile_scope!("submit presentation cb");
raw_device
.queue_submit(
self.device.universal_queue.raw,
&submit_info,
presentation_cb.submit_done_fence,
)
.map_err(|err| device.report_error(err.into()))
.expect("presentation queue_submit failed");
}
swapchain.present_image(swapchain_image);
retired_rg
};
self.temporal_rg_state = match std::mem::take(&mut self.temporal_rg_state) {
TemporalRg::Inert(_) => {
panic!("Trying to retire the render graph, but it's inert. Was prepare_frame not caled?");
}
TemporalRg::Exported(rg) => TemporalRg::Inert(rg.retire_temporal(&retired_rg)),
};
retired_rg.release_resources(&mut self.transient_resource_cache);
self.dynamic_constants.advance_frame();
self.device.finish_frame(current_frame);
}
// Descriptor set for per-frame data
fn create_frame_descriptor_set(
backend: &RenderBackend,
dynamic_constants: &Buffer,
) -> vk::DescriptorSet {
let device = &backend.device.raw;
let set_binding_flags = [
vk::DescriptorBindingFlags::PARTIALLY_BOUND,
vk::DescriptorBindingFlags::PARTIALLY_BOUND,
vk::DescriptorBindingFlags::PARTIALLY_BOUND,
];
let mut binding_flags_create_info =
vk::DescriptorSetLayoutBindingFlagsCreateInfo::builder()
.binding_flags(&set_binding_flags)
.build();
let descriptor_set_layout = unsafe {
device
.create_descriptor_set_layout(
&vk::DescriptorSetLayoutCreateInfo::builder()
.bindings(&[
// frame_constants
vk::DescriptorSetLayoutBinding::builder()
.descriptor_count(1)
.descriptor_type(vk::DescriptorType::UNIFORM_BUFFER_DYNAMIC)
.stage_flags(vk::ShaderStageFlags::ALL)
.binding(0)
.build(),
// instance_dynamic_parameters
vk::DescriptorSetLayoutBinding::builder()
.descriptor_count(1)
.descriptor_type(vk::DescriptorType::STORAGE_BUFFER_DYNAMIC)
.stage_flags(vk::ShaderStageFlags::ALL)
.binding(1)
.build(),
// triangle_lights_dyn
vk::DescriptorSetLayoutBinding::builder()
.descriptor_count(1)
.descriptor_type(vk::DescriptorType::STORAGE_BUFFER_DYNAMIC)
.stage_flags(vk::ShaderStageFlags::ALL)
.binding(2)
.build(),
])
.push_next(&mut binding_flags_create_info)
.build(),
None,
)
.unwrap()
};
let descriptor_sizes = [
vk::DescriptorPoolSize {
ty: vk::DescriptorType::UNIFORM_BUFFER_DYNAMIC,
descriptor_count: 1,
},
vk::DescriptorPoolSize {
ty: vk::DescriptorType::STORAGE_BUFFER_DYNAMIC,
descriptor_count: 2,
},
];
let descriptor_pool_info = vk::DescriptorPoolCreateInfo::builder()
.pool_sizes(&descriptor_sizes)
.max_sets(1);
let descriptor_pool = unsafe {
device
.create_descriptor_pool(&descriptor_pool_info, None)
.unwrap()
};
let set = unsafe {
device
.allocate_descriptor_sets(
&vk::DescriptorSetAllocateInfo::builder()
.descriptor_pool(descriptor_pool)
.set_layouts(std::slice::from_ref(&descriptor_set_layout))
.build(),
)
.unwrap()[0]
};
{
let uniform_buffer_info = vk::DescriptorBufferInfo::builder()
.buffer(dynamic_constants.raw)
.range(MAX_DYNAMIC_CONSTANTS_BYTES_PER_DISPATCH as u64)
.build();
let storage_buffer_info = vk::DescriptorBufferInfo::builder()
.buffer(dynamic_constants.raw)
.range(MAX_DYNAMIC_CONSTANTS_STORAGE_BUFFER_BYTES as u64)
.build();
let descriptor_set_writes = [
// `frame_constants`
vk::WriteDescriptorSet::builder()
.dst_binding(0)
.dst_set(set)
.descriptor_type(vk::DescriptorType::UNIFORM_BUFFER_DYNAMIC)
.buffer_info(std::slice::from_ref(&uniform_buffer_info))
.build(),
// `instance_dynamic_parameters_dyn`
vk::WriteDescriptorSet::builder()
.dst_binding(1)
.dst_set(set)
.descriptor_type(vk::DescriptorType::STORAGE_BUFFER_DYNAMIC)
.buffer_info(std::slice::from_ref(&storage_buffer_info))
.build(),
// `triangle_lights_dyn`
vk::WriteDescriptorSet::builder()
.dst_binding(2)
.dst_set(set)
.descriptor_type(vk::DescriptorType::STORAGE_BUFFER_DYNAMIC)
.buffer_info(std::slice::from_ref(&storage_buffer_info))
.build(),
];
unsafe { device.update_descriptor_sets(&descriptor_set_writes, &[]) };
}
set
}
pub fn prepare_frame<PrepareRenderGraphFn>(
&mut self,
prepare_render_graph: PrepareRenderGraphFn,
) -> anyhow::Result<()>
where
PrepareRenderGraphFn: FnOnce(&mut TemporalRenderGraph),
{
let mut rg = TemporalRenderGraph::new(
match &self.temporal_rg_state {
TemporalRg::Inert(state) => state.clone_assuming_inert(),
TemporalRg::Exported(_) => {
panic!("Trying to prepare_frame but render graph is still active")
}
},
self.device.clone(),
);
rg.predefined_descriptor_set_layouts.insert(
2,
PredefinedDescriptorSet {
bindings: FRAME_CONSTANTS_LAYOUT.clone(),
},
);
prepare_render_graph(&mut rg);
let (rg, temporal_rg_state) = rg.export_temporal();
self.compiled_rg = Some(rg.compile(&mut self.pipeline_cache));
match self.pipeline_cache.prepare_frame(&self.device) {
Ok(()) => {
// If the frame preparation succeded, update stored temporal rg state and finish
self.temporal_rg_state = TemporalRg::Exported(temporal_rg_state);
Ok(())
}
Err(err) => {
// If frame preparation failed, we're not going to render anything, but we've potentially created
// some temporal resources, and we can reuse them in the next attempt.
//
// Import any new resources into our temporal rg state, but reset their access modes.
let self_temporal_rg_state = match &mut self.temporal_rg_state {
TemporalRg::Inert(state) => state,
TemporalRg::Exported(_) => unreachable!(),
};
for (res_key, res) in temporal_rg_state.0.resources {
// `insert` is infrequent here, and we can avoid cloning the key.
#[allow(clippy::map_entry)]
if !self_temporal_rg_state.resources.contains_key(&res_key) {
let res = match res {
res @ TemporalResourceState::Inert { .. } => res,
TemporalResourceState::Imported { resource, .. }
| TemporalResourceState::Exported { resource, .. } => {
TemporalResourceState::Inert {
resource,
access_type: vk_sync::AccessType::Nothing,
}
}
};
self_temporal_rg_state.resources.insert(res_key, res);
}
}
Err(err)
}
}
}
pub fn device(&self) -> &Arc<Device> {
&self.device
}
}