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
//! This module provides functions for picking a physical device and creating
//! the logical device.

mod physical_device;
mod queue;
mod queue_family_indices;

pub use self::{queue::Queue, queue_family_indices::QueueFamilyIndices};

use crate::graphics::vulkan::{
    device_allocator::{self, Allocation},
    Instance, WindowSurface,
};

use anyhow::Result;
use ash::{version::DeviceV1_0, vk};
use std::{
    ffi::CString,
    sync::{Arc, Mutex},
};

use super::{
    command_pool::OwnedCommandPool, device_allocator::DeviceAllocator,
};

/// This struct holds all device-specific resources, the physical device and
/// logical device for interacting with it, and the associated queues.
pub struct Device {
    pub physical_device: vk::PhysicalDevice,
    pub logical_device: ash::Device,
    pub graphics_queue: Queue,
    pub present_queue: Queue,

    shared_graphics_pool: Mutex<OwnedCommandPool>,
    allocator: Mutex<Box<dyn DeviceAllocator>>,

    instance: Arc<Instance>,
}

impl Device {
    /// Create a new device based on this application's required features and
    /// properties.
    pub fn new(window_surface: &dyn WindowSurface) -> Result<Arc<Device>> {
        let instance = window_surface.clone_vulkan_instance();
        let physical_device =
            physical_device::find_optimal(&instance, window_surface)?;
        let queue_family_indices = QueueFamilyIndices::find(
            &physical_device,
            instance.raw(),
            window_surface,
        )?;
        let logical_device = instance.create_logical_device(
            &physical_device,
            physical_device::required_features(),
            &physical_device::required_extensions(),
            &queue_family_indices.as_queue_create_infos(),
        )?;

        let (graphics_queue, present_queue) =
            queue_family_indices.get_queues(&logical_device)?;

        let allocator = device_allocator::build_standard_allocator(
            instance.ash.clone(),
            logical_device.clone(),
            physical_device,
        );

        let shared_graphics_pool = Mutex::new(OwnedCommandPool::new(
            &logical_device,
            graphics_queue.family_id,
        )?);

        let device = Arc::new(Self {
            physical_device,
            logical_device,
            graphics_queue,
            present_queue,
            shared_graphics_pool,
            allocator: Mutex::new(allocator),
            instance,
        });

        device.name_vulkan_object(
            "Application Logical Device",
            vk::ObjectType::DEVICE,
            &device.logical_device.handle(),
        )?;

        device.name_vulkan_object(
            "Shared Graphics Pool",
            vk::ObjectType::COMMAND_POOL,
            unsafe { device.shared_graphics_pool.lock().unwrap().raw() },
        )?;

        if device.graphics_queue.is_same(&device.present_queue) {
            device
                .graphics_queue
                .name_vulkan_object("graphics/present queue", &device)?;
        } else {
            device
                .graphics_queue
                .name_vulkan_object("graphics queue", &device)?;
            device
                .present_queue
                .name_vulkan_object("present queue", &device)?;
        }

        Ok(device)
    }

    /// Allocate a a chunk of memory for use in a buffer or texture.
    ///
    /// # unsafe because
    ///
    /// - the caller is responsible for eventually calling 'free memory' before
    ///   the application quits
    ///
    pub unsafe fn allocate_memory(
        &self,
        memory_requirements: vk::MemoryRequirements,
        property_flags: vk::MemoryPropertyFlags,
    ) -> Result<Allocation> {
        use anyhow::Context;
        use ash::version::InstanceV1_0;

        let memory_properties = self
            .instance
            .ash
            .get_physical_device_memory_properties(self.physical_device);

        let memory_type_index = memory_properties
            .memory_types
            .iter()
            .enumerate()
            .find(|(i, memory_type)| {
                let type_supported =
                    memory_requirements.memory_type_bits & (1 << i) != 0;
                let properties_supported =
                    memory_type.property_flags.contains(property_flags);
                type_supported & properties_supported
            })
            .map(|(i, _memory_type)| i as u32)
            .with_context(|| {
                "unable to find a suitable memory type for this allocation!"
            })?;

        self.allocator
            .lock()
            .unwrap()
            .allocate(vk::MemoryAllocateInfo {
                memory_type_index,
                allocation_size: memory_requirements.size,
                ..Default::default()
            })
    }

    /// Free a memory allocation.
    ///
    /// # unsafe because
    ///
    /// - the caller is responsible for ensuring that the memory is no longer
    ///   in use by the gpu.
    ///
    pub unsafe fn free_memory(&self, allocation: &Allocation) -> Result<()> {
        self.allocator.lock().unwrap().free(allocation)
    }

    /// Give a debug name for a vulkan object owned by this device.
    ///
    /// Whatever name is provided here will show up in the debug logs if there
    /// are any issues detected by the validation layers.
    pub fn name_vulkan_object<Name, Handle>(
        &self,
        name: Name,
        object_type: vk::ObjectType,
        handle: &Handle,
    ) -> Result<()>
    where
        Handle: vk::Handle + Copy,
        Name: Into<String>,
    {
        let cname = CString::new(name.into()).unwrap();
        let name_info = vk::DebugUtilsObjectNameInfoEXT {
            object_type,
            p_object_name: cname.as_ptr(),
            object_handle: handle.as_raw(),
            ..Default::default()
        };

        unsafe {
            self.instance.debug.debug_utils_set_object_name(
                self.logical_device.handle(),
                &name_info,
            )?;
        }

        Ok(())
    }

    /// Synchronously submit commands for execution on the graphics queue.
    ///
    /// This method is internally synchronized and can be called on multiple
    /// threads without any additional synchronization.
    ///
    /// This method forces the device to wait idle after submitting commands,
    /// as such it is very slow (don't do it in a loop every frame!).
    ///
    /// # Unsafe Because
    ///
    /// - no internal synchronization is done, any resources used by graphcis
    ///   commands must be synchronized by the caller
    /// - note: the device idles after the submission, so no resources refereced
    ///   inside this method should be in-use after the call.
    pub unsafe fn sync_graphics_commands<R, Action>(
        &self,
        mut action: Action,
    ) -> Result<R>
    where
        Action: FnMut(vk::CommandBuffer) -> Result<R>,
    {
        let pool = self.shared_graphics_pool.lock().unwrap();
        let command_buffer =
            pool.allocate_command_buffer(&self.logical_device)?;

        let begin_info = vk::CommandBufferBeginInfo {
            flags: vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT,
            ..Default::default()
        };
        self.logical_device
            .begin_command_buffer(command_buffer, &&begin_info)?;

        let result = action(command_buffer);

        self.logical_device.end_command_buffer(command_buffer)?;
        self.submit_and_wait_idle(&self.graphics_queue, command_buffer)?;
        pool.free_command_buffer(&self.logical_device, command_buffer);

        result
    }

    /// Submit a command buffer to the specified queue, then wait for it to
    /// idle.
    pub unsafe fn submit_and_wait_idle(
        &self,
        queue: &Queue,
        command_buffer: vk::CommandBuffer,
    ) -> Result<()> {
        let command_buffers = &[command_buffer];
        self.logical_device.queue_submit(
            queue.raw(),
            &[vk::SubmitInfo {
                p_command_buffers: command_buffers.as_ptr(),
                command_buffer_count: 1,
                ..Default::default()
            }],
            vk::Fence::null(),
        )?;
        self.logical_device.queue_wait_idle(queue.raw())?;
        Ok(())
    }

    /// Create a new swapchain loader which will be owned by the caller.
    pub fn create_swapchain_loader(&self) -> ash::extensions::khr::Swapchain {
        ash::extensions::khr::Swapchain::new(
            &self.instance.ash,
            &self.logical_device,
        )
    }
}

impl Drop for Device {
    /// Destroy the logical device.
    ///
    /// Device owns an Arc<Instance> so it's guaranteed that the instance will
    /// not be destroyed until the logical device has been dropped.
    fn drop(&mut self) {
        unsafe {
            self.shared_graphics_pool
                .lock()
                .unwrap()
                .destroy(&self.logical_device);
            self.logical_device.destroy_device(None);
        }
    }
}