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
|
//! Deals with all the Vulkan/HAL details.
//! This relies on draw passes for the actual drawing logic.
use std::{
any::{Any, TypeId},
collections::HashMap,
mem::ManuallyDrop,
ptr::read,
sync::{Arc, RwLock},
};
use anyhow::{Context, Result};
use hal::{pool::CommandPoolCreateFlags, PhysicalDeviceProperties};
use log::debug;
use winit::window::Window;
use super::{
draw_passes::{DrawPass, IntoDrawPass},
queue_negotiator::{DrawQueue, QueueNegotiator},
target::{SwapchainProperties, TargetChain},
};
use crate::{
draw_passes::Singular,
error::{EnvironmentError, LockPoisoned},
mem::MemoryPool,
types::*,
};
use stockton_types::Session;
/// Contains most root vulkan objects, and some precalculated info such as best formats to use.
/// In most cases, this and the DrawPass should contain all vulkan objects present.
pub struct RenderingContext {
/// Vulkan Instance
instance: ManuallyDrop<back::Instance>,
/// Device we're using
device: Arc<RwLock<DeviceT>>,
/// Adapter we're using
adapter: Adapter,
/// The properties of the physical device we're using
physical_device_properties: PhysicalDeviceProperties,
/// Swapchain and stuff
target_chain: ManuallyDrop<TargetChain>,
// Command pool and buffers
/// The command pool used for our buffers
cmd_pool: ManuallyDrop<CommandPoolT>,
/// The queue negotiator to use
queue_negotiator: QueueNegotiator,
/// The queue to use for drawing
queue: Arc<RwLock<QueueT>>,
/// Number of pixels per standard point
pixels_per_point: f32,
/// The list of memory pools
memory_pools: HashMap<TypeId, Box<dyn Any>>,
}
impl RenderingContext {
/// Create a new RenderingContext for the given window.
pub fn new<IDP: IntoDrawPass<DP, Singular>, DP: DrawPass<Singular>>(
window: &Window,
) -> Result<Self> {
// Create surface
let (instance, surface, mut adapters) = unsafe {
let instance =
back::Instance::create("stockton", 1).context("Error creating vulkan instance")?;
let surface = instance
.create_surface(window)
.context("Error creating surface")?;
let adapters = instance.enumerate_adapters();
(instance, surface, adapters)
};
// TODO: Properly figure out which adapter to use
let adapter = adapters.remove(0);
// Queue Negotiator
let mut queue_families_specs = Vec::new();
let (mut queue_negotiator, surface) = {
let dq: DrawQueue = DrawQueue { surface };
let mut qn = QueueNegotiator::default();
// Draw Queue
qn.find(&adapter, &dq)
.context("Couldn't find draw queue family")?;
queue_families_specs.push(
qn.family_spec::<DrawQueue>(&adapter.queue_families, 1)
.context("Couldn't find draw queue family")?,
);
// Auxiliary queues for DP
queue_families_specs.extend(
IDP::find_aux_queues(&adapter, &mut qn)
.context("Level pass couldn't populate queue negotiator")?,
);
(qn, dq.surface)
};
// Device & Queue groups
let (device_lock, queue_groups) = {
// TODO: This sucks, but hal is restrictive on how we can pass this specific argument.
// Deduplicate families & convert to specific type.
let mut queue_families_specs_real = Vec::with_capacity(queue_families_specs.len());
for (qf, ns) in queue_families_specs.iter_mut() {
if let Some(existing_family_spec) = queue_families_specs_real
.iter()
.position(|(qf2, _): &(&QueueFamilyT, &[f32])| qf2.id() == qf.id())
{
ns.extend(queue_families_specs_real[existing_family_spec].1.iter());
queue_families_specs_real[existing_family_spec] = (*qf, ns.as_slice());
} else {
queue_families_specs_real.push((*qf, ns.as_slice()))
}
}
let gpu = unsafe {
adapter
.physical_device
.open(queue_families_specs_real.as_slice(), hal::Features::empty())
.context("Error opening logical device")?
};
(Arc::new(RwLock::new(gpu.device)), gpu.queue_groups)
};
queue_negotiator.set_queue_groups(queue_groups);
// Figure out what our swapchain will look like
let swapchain_properties = SwapchainProperties::find_best(&adapter, &surface)
.context("Error getting properties for swapchain")?;
// Lock device
let mut device = device_lock
.write()
.map_err(|_| LockPoisoned::Device)
.context("Error getting device lock")?;
debug!("Detected swapchain properties: {:?}", swapchain_properties);
// Command pool
let mut cmd_pool = unsafe {
device.create_command_pool(
queue_negotiator
.family::<DrawQueue>()
.ok_or(EnvironmentError::NoSuitableFamilies)?,
CommandPoolCreateFlags::RESET_INDIVIDUAL,
)
}
.context("Error creating draw command pool")?;
// Swapchain and associated resources
let target_chain = TargetChain::new(
&mut device,
&adapter,
surface,
&mut cmd_pool,
swapchain_properties,
)
.context("Error creating target chain")?;
// Unlock device
drop(device);
let queue = queue_negotiator
.get_queue::<DrawQueue>()
.context("Error getting draw queue")?;
Ok(RenderingContext {
instance: ManuallyDrop::new(instance),
device: device_lock,
physical_device_properties: adapter.physical_device.properties(),
adapter,
queue_negotiator,
queue,
target_chain: ManuallyDrop::new(target_chain),
cmd_pool: ManuallyDrop::new(cmd_pool),
pixels_per_point: window.scale_factor() as f32,
memory_pools: HashMap::new(),
})
}
/// If this function fails the whole context is probably dead
/// # Safety
/// The context must not be used while this is being called
pub unsafe fn handle_surface_change(&mut self) -> Result<()> {
let mut device = self
.device
.write()
.map_err(|_| LockPoisoned::Device)
.context("Error getting device lock")?;
device
.wait_idle()
.context("Error waiting for device to become idle")?;
let surface = ManuallyDrop::into_inner(read(&self.target_chain))
.deactivate_with_recyling(&mut device, &mut self.cmd_pool);
let properties = SwapchainProperties::find_best(&self.adapter, &surface)
.context("Error finding best swapchain properties")?;
self.target_chain = ManuallyDrop::new(
TargetChain::new(
&mut device,
&self.adapter,
surface,
&mut self.cmd_pool,
properties,
)
.context("Error creating target chain")?,
);
Ok(())
}
/// Draw onto the next frame of the swapchain
pub fn draw_next_frame<DP: DrawPass<Singular>>(
&mut self,
session: &Session,
dp: &mut DP,
) -> Result<()> {
let mut device = self
.device
.write()
.map_err(|_| LockPoisoned::Device)
.context("Error getting device lock")?;
let mut queue = self
.queue
.write()
.map_err(|_| LockPoisoned::Queue)
.context("Error getting draw queue lock")?;
// Level draw pass
self.target_chain
.do_draw_with(&mut device, &mut queue, dp, session)
.context("Error preparing next target")?;
Ok(())
}
/// Get a reference to the rendering context's pixels per point.
pub fn pixels_per_point(&self) -> f32 {
self.pixels_per_point
}
/// Get a reference to the rendering context's device.
pub fn device(&self) -> &Arc<RwLock<DeviceT>> {
&self.device
}
/// Get a reference to the rendering context's target chain.
pub fn target_chain(&self) -> &TargetChain {
&self.target_chain
}
/// Get a reference to the rendering context's adapter.
pub fn adapter(&self) -> &Adapter {
&self.adapter
}
/// Get a mutable reference to the rendering context's queue negotiator.
pub fn queue_negotiator_mut(&mut self) -> &mut QueueNegotiator {
&mut self.queue_negotiator
}
/// Get a reference to the physical device's properties.
pub fn physical_device_properties(&self) -> &PhysicalDeviceProperties {
&self.physical_device_properties
}
/// Get the specified memory pool, lazily initialising it if it's not yet present
pub fn memory_pool<'a, P: MemoryPool>(&'a mut self) -> Result<&'a Arc<RwLock<P>>> {
self.ensure_memory_pool::<P>()?;
Ok(self.existing_memory_pool::<P>().unwrap())
}
/// Ensure the specified memory pool is initialised.
pub fn ensure_memory_pool<P: MemoryPool>(&mut self) -> Result<()> {
let tid = TypeId::of::<P>();
if !self.memory_pools.contains_key(&tid) {
self.memory_pools
.insert(tid, Box::new(P::from_context(self)?));
}
Ok(())
}
/// Get the specified memory pool, returning None if it's not yet present
/// You should only use this when you're certain it exists, such as when freeing memory
/// allocated from that pool
pub fn existing_memory_pool<P: MemoryPool>(&self) -> Option<&Arc<RwLock<P>>> {
self.memory_pools
.get(&TypeId::of::<P>())
.map(|x| x.downcast_ref().unwrap())
}
}
impl core::ops::Drop for RenderingContext {
fn drop(&mut self) {
{
self.device.write().unwrap().wait_idle().unwrap();
}
// TODO: Better deactivation code
unsafe {
let mut device = self.device.write().unwrap();
ManuallyDrop::into_inner(read(&self.target_chain)).deactivate(
&mut self.instance,
&mut device,
&mut self.cmd_pool,
);
device.destroy_command_pool(ManuallyDrop::into_inner(read(&self.cmd_pool)));
}
}
}
|