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
//! These are Vulkan Instance and Device wrappers that contain a unique ID
//! This allows checking if any other Vulkan resource belongs to a specific
//! Instance or Device. This is required to ensure we are making a safe
//! call.

use {
    gfx_hal::Backend,
    std::{any::Any, marker::PhantomData, ops::Deref},
};

#[cfg(not(feature = "no-slow-safety-checks"))]
fn new_instance_id() -> InstanceId {
    static INSTANCE_ID: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);

    let id = INSTANCE_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    assert!(
        id < usize::max_value() && (id as u32) < u32::max_value(),
        "Too many instances created"
    );

    if id == 0 {
        // Warn once.
        log::info!("Slow safety checks are enabled! You can disable them in production by enabling the 'no-slow-safety-checks' feature!");
    }

    InstanceId { id: id as u32 }
}

#[cfg(not(feature = "no-slow-safety-checks"))]
fn new_device_id(instance: InstanceId) -> DeviceId {
    static DEVICE_ID: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);

    let id = DEVICE_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    assert!(
        id < usize::max_value() && (id as u32) < u32::max_value(),
        "Too many devices created"
    );

    DeviceId {
        id: id as u32,
        instance,
    }
}

#[cfg(feature = "no-slow-safety-checks")]
fn new_instance_id() -> InstanceId {
    InstanceId {}
}

#[cfg(feature = "no-slow-safety-checks")]
fn new_device_id(instance: InstanceId) -> DeviceId {
    DeviceId { instance }
}

/// Id of the hal instance.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct InstanceId {
    /// Unique id.
    #[cfg(not(feature = "no-slow-safety-checks"))]
    pub id: u32,
}

impl InstanceId {
    /// Create new instance id.
    pub fn new() -> Self {
        new_instance_id()
    }
}

/// Raw instance wrapper with id.
pub struct Instance<B: Backend> {
    instance: Box<dyn Any + Send + Sync>,
    id: InstanceId,
    marker: PhantomData<B>,
}

impl<B> Instance<B>
where
    B: Backend,
{
    /// Wrap instance value.
    pub fn new(instance: impl gfx_hal::Instance) -> Self {
        Instance {
            id: new_instance_id(),
            instance: Box::new(instance),
            marker: PhantomData,
        }
    }
}

impl<B> Instance<B>
where
    B: Backend,
{
    /// Get instance id.
    pub fn id(&self) -> InstanceId {
        self.id
    }

    /// Get reference to raw instance.
    pub fn raw(&self) -> &dyn Any {
        &*self.instance
    }

    /// Get mutable reference to raw instance.
    pub fn raw_mut(&mut self) -> &mut dyn Any {
        &mut *self.instance
    }

    /// Get reference to typed raw instance.
    pub fn raw_typed<T>(&self) -> Option<&T>
    where
        T: gfx_hal::Instance,
    {
        if std::any::TypeId::of::<T::Backend>() == std::any::TypeId::of::<B>() {
            Some(
                self.instance
                    .downcast_ref::<T>()
                    .expect("Bad instance wrapper"),
            )
        } else {
            None
        }
    }

    /// Get mutable reference to typed raw instance.
    pub fn raw_typed_mut<T>(&mut self) -> Option<&mut T>
    where
        T: gfx_hal::Instance,
    {
        if std::any::TypeId::of::<T::Backend>() == std::any::TypeId::of::<B>() {
            Some(
                self.instance
                    .downcast_mut::<T>()
                    .expect("Bad instance wrapper"),
            )
        } else {
            None
        }
    }
}

impl<B> std::fmt::Debug for Instance<B>
where
    B: Backend,
{
    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(fmt, "Instance {:?}", self.id)
    }
}

/// Id of the instance.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct DeviceId {
    /// Unique id.
    #[cfg(not(feature = "no-slow-safety-checks"))]
    pub id: u32,

    /// Instance id.
    pub instance: InstanceId,
}

impl DeviceId {
    /// Create new device id.
    pub fn new(instance: InstanceId) -> Self {
        new_device_id(instance)
    }
}

/// Raw device wrapper with id.
#[derive(Debug)]
pub struct Device<B: Backend> {
    device: B::Device,
    id: DeviceId,
}

impl<B> Device<B>
where
    B: Backend,
{
    /// Wrap device value.
    pub fn new(device: B::Device, instance: &Instance<B>) -> Self {
        Device {
            id: new_device_id(instance.id),
            device,
        }
    }

    /// Wrap device value.
    pub fn from_raw(device: B::Device, id: DeviceId) -> Self {
        Device { id, device }
    }

    /// Get device id.
    pub fn id(&self) -> DeviceId {
        self.id
    }

    /// Get reference to raw device.
    pub fn raw(&self) -> &B::Device {
        &self.device
    }

    /// Get mutable reference to raw device.
    pub fn raw_mut(&mut self) -> &mut B::Device {
        &mut self.device
    }
}

impl<B> Deref for Device<B>
where
    B: Backend,
{
    type Target = B::Device;

    fn deref(&self) -> &B::Device {
        self.raw()
    }
}

/// Implement ownership checking for value with `device: DeviceId` field.
#[macro_export]
macro_rules! device_owned {
    ($type:ident<B $(, $arg:ident $(: $(?$sized:ident)? $($bound:path)?)?)*> @ $getter:expr) => {
        #[allow(unused_qualifications)]
        impl<B $(, $arg)*> $type<B $(, $arg)*>
        where
            B: gfx_hal::Backend,
            $(
                $($arg: $(?$sized)* $($bound)?,)?
            )*
        {
            /// Get owned id.
            pub fn device_id(&self) -> $crate::DeviceId {
                ($getter)(self)
            }

            /// Assert specified device is owner.
            pub fn assert_device_owner(&self, device: &$crate::Device<B>) {
                assert_eq!(self.device_id(), device.id(), "Resource is not owned by specified device");
            }

            /// Get owned id.
            pub fn instance_id(&self) -> $crate::InstanceId {
                self.device_id().instance
            }

            /// Assert specified instance is owner.
            pub fn assert_instance_owner(&self, instance: &$crate::Instance<B>) {
                assert_eq!(self.instance_id(), instance.id(), "Resource is not owned by specified instance");
            }
        }
    };

    ($type:ident<B $(, $arg:ident $(: $(?$sized:ident)? $($bound:path)?)?)*>) => {
        device_owned!($type<B $(, $arg $(: $(?$sized)? $($bound)?)?)*> @ (|s: &Self| {s.device}));
    };
}

/// Implement ownership checking for value with `instance: InstanceId` field.
#[macro_export]
macro_rules! instance_owned {
    ($type:ident<B $(, $arg:ident $(: $(?$sized:ident)? $($bound:path)?)?)*>) => {
        #[allow(unused_qualifications)]
        impl<B $(, $arg)*> $type<B $(, $arg)*>
        where
            B: gfx_hal::Backend,
            $(
                $($arg: $(?$sized)? $($bound)?,)?
            )*
        {
            /// Get owned id.
            pub fn instance_id(&self) -> $crate::InstanceId {
                self.instance
            }

            /// Assert specified instance is owner.
            pub fn assert_instance_owner(&self, instance: &Instance<B>) {
                assert_eq!(self.instance_id(), instance.id());
            }
        }
    };
}