|
| 1 | +use crate::{ |
| 2 | + global::Global, |
| 3 | + hal_api::HalApi, |
| 4 | + id::{ |
| 5 | + AdapterId, BlasId, BufferId, CommandEncoderId, DeviceId, QueueId, SurfaceId, TextureId, |
| 6 | + TextureViewId, TlasId, |
| 7 | + }, |
| 8 | + resource::AccelerationStructure, |
| 9 | +}; |
| 10 | + |
| 11 | +impl Global { |
| 12 | + /// # Safety |
| 13 | + /// |
| 14 | + /// - The raw buffer handle must not be manually destroyed |
| 15 | + pub unsafe fn buffer_as_hal<A: HalApi, F: FnOnce(Option<&A::Buffer>) -> R, R>( |
| 16 | + &self, |
| 17 | + id: BufferId, |
| 18 | + hal_buffer_callback: F, |
| 19 | + ) -> R { |
| 20 | + profiling::scope!("Buffer::as_hal"); |
| 21 | + |
| 22 | + let hub = &self.hub; |
| 23 | + |
| 24 | + if let Ok(buffer) = hub.buffers.get(id).get() { |
| 25 | + let snatch_guard = buffer.device.snatchable_lock.read(); |
| 26 | + let hal_buffer = buffer |
| 27 | + .raw(&snatch_guard) |
| 28 | + .and_then(|b| b.as_any().downcast_ref()); |
| 29 | + hal_buffer_callback(hal_buffer) |
| 30 | + } else { |
| 31 | + hal_buffer_callback(None) |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /// # Safety |
| 36 | + /// |
| 37 | + /// - The raw texture handle must not be manually destroyed |
| 38 | + pub unsafe fn texture_as_hal<A: HalApi, F: FnOnce(Option<&A::Texture>) -> R, R>( |
| 39 | + &self, |
| 40 | + id: TextureId, |
| 41 | + hal_texture_callback: F, |
| 42 | + ) -> R { |
| 43 | + profiling::scope!("Texture::as_hal"); |
| 44 | + |
| 45 | + let hub = &self.hub; |
| 46 | + |
| 47 | + if let Ok(texture) = hub.textures.get(id).get() { |
| 48 | + let snatch_guard = texture.device.snatchable_lock.read(); |
| 49 | + let hal_texture = texture.raw(&snatch_guard); |
| 50 | + let hal_texture = hal_texture |
| 51 | + .as_ref() |
| 52 | + .and_then(|it| it.as_any().downcast_ref()); |
| 53 | + hal_texture_callback(hal_texture) |
| 54 | + } else { |
| 55 | + hal_texture_callback(None) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /// # Safety |
| 60 | + /// |
| 61 | + /// - The raw texture view handle must not be manually destroyed |
| 62 | + pub unsafe fn texture_view_as_hal<A: HalApi, F: FnOnce(Option<&A::TextureView>) -> R, R>( |
| 63 | + &self, |
| 64 | + id: TextureViewId, |
| 65 | + hal_texture_view_callback: F, |
| 66 | + ) -> R { |
| 67 | + profiling::scope!("TextureView::as_hal"); |
| 68 | + |
| 69 | + let hub = &self.hub; |
| 70 | + |
| 71 | + if let Ok(texture_view) = hub.texture_views.get(id).get() { |
| 72 | + let snatch_guard = texture_view.device.snatchable_lock.read(); |
| 73 | + let hal_texture_view = texture_view.raw(&snatch_guard); |
| 74 | + let hal_texture_view = hal_texture_view |
| 75 | + .as_ref() |
| 76 | + .and_then(|it| it.as_any().downcast_ref()); |
| 77 | + hal_texture_view_callback(hal_texture_view) |
| 78 | + } else { |
| 79 | + hal_texture_view_callback(None) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /// # Safety |
| 84 | + /// |
| 85 | + /// - The raw adapter handle must not be manually destroyed |
| 86 | + pub unsafe fn adapter_as_hal<A: HalApi, F: FnOnce(Option<&A::Adapter>) -> R, R>( |
| 87 | + &self, |
| 88 | + id: AdapterId, |
| 89 | + hal_adapter_callback: F, |
| 90 | + ) -> R { |
| 91 | + profiling::scope!("Adapter::as_hal"); |
| 92 | + |
| 93 | + let hub = &self.hub; |
| 94 | + let adapter = hub.adapters.get(id); |
| 95 | + let hal_adapter = adapter.raw.adapter.as_any().downcast_ref(); |
| 96 | + |
| 97 | + hal_adapter_callback(hal_adapter) |
| 98 | + } |
| 99 | + |
| 100 | + /// # Safety |
| 101 | + /// |
| 102 | + /// - The raw device handle must not be manually destroyed |
| 103 | + pub unsafe fn device_as_hal<A: HalApi, F: FnOnce(Option<&A::Device>) -> R, R>( |
| 104 | + &self, |
| 105 | + id: DeviceId, |
| 106 | + hal_device_callback: F, |
| 107 | + ) -> R { |
| 108 | + profiling::scope!("Device::as_hal"); |
| 109 | + |
| 110 | + let device = self.hub.devices.get(id); |
| 111 | + let hal_device = device.raw().as_any().downcast_ref(); |
| 112 | + |
| 113 | + hal_device_callback(hal_device) |
| 114 | + } |
| 115 | + |
| 116 | + /// # Safety |
| 117 | + /// |
| 118 | + /// - The raw fence handle must not be manually destroyed |
| 119 | + pub unsafe fn device_fence_as_hal<A: HalApi, F: FnOnce(Option<&A::Fence>) -> R, R>( |
| 120 | + &self, |
| 121 | + id: DeviceId, |
| 122 | + hal_fence_callback: F, |
| 123 | + ) -> R { |
| 124 | + profiling::scope!("Device::fence_as_hal"); |
| 125 | + |
| 126 | + let device = self.hub.devices.get(id); |
| 127 | + let fence = device.fence.read(); |
| 128 | + hal_fence_callback(fence.as_any().downcast_ref()) |
| 129 | + } |
| 130 | + |
| 131 | + /// # Safety |
| 132 | + /// - The raw surface handle must not be manually destroyed |
| 133 | + pub unsafe fn surface_as_hal<A: HalApi, F: FnOnce(Option<&A::Surface>) -> R, R>( |
| 134 | + &self, |
| 135 | + id: SurfaceId, |
| 136 | + hal_surface_callback: F, |
| 137 | + ) -> R { |
| 138 | + profiling::scope!("Surface::as_hal"); |
| 139 | + |
| 140 | + let surface = self.surfaces.get(id); |
| 141 | + let hal_surface = surface |
| 142 | + .raw(A::VARIANT) |
| 143 | + .and_then(|surface| surface.as_any().downcast_ref()); |
| 144 | + |
| 145 | + hal_surface_callback(hal_surface) |
| 146 | + } |
| 147 | + |
| 148 | + /// # Safety |
| 149 | + /// |
| 150 | + /// - The raw command encoder handle must not be manually destroyed |
| 151 | + pub unsafe fn command_encoder_as_hal_mut< |
| 152 | + A: HalApi, |
| 153 | + F: FnOnce(Option<&mut A::CommandEncoder>) -> R, |
| 154 | + R, |
| 155 | + >( |
| 156 | + &self, |
| 157 | + id: CommandEncoderId, |
| 158 | + hal_command_encoder_callback: F, |
| 159 | + ) -> R { |
| 160 | + profiling::scope!("CommandEncoder::as_hal"); |
| 161 | + |
| 162 | + let hub = &self.hub; |
| 163 | + |
| 164 | + let cmd_buf = hub.command_buffers.get(id.into_command_buffer_id()); |
| 165 | + let mut cmd_buf_data = cmd_buf.data.lock(); |
| 166 | + cmd_buf_data.record_as_hal_mut(|opt_cmd_buf| -> R { |
| 167 | + hal_command_encoder_callback(opt_cmd_buf.and_then(|cmd_buf| { |
| 168 | + cmd_buf |
| 169 | + .encoder |
| 170 | + .open() |
| 171 | + .ok() |
| 172 | + .and_then(|encoder| encoder.as_any_mut().downcast_mut()) |
| 173 | + })) |
| 174 | + }) |
| 175 | + } |
| 176 | + |
| 177 | + /// # Safety |
| 178 | + /// |
| 179 | + /// - The raw queue handle must not be manually destroyed |
| 180 | + pub unsafe fn queue_as_hal<A: HalApi, F, R>(&self, id: QueueId, hal_queue_callback: F) -> R |
| 181 | + where |
| 182 | + F: FnOnce(Option<&A::Queue>) -> R, |
| 183 | + { |
| 184 | + profiling::scope!("Queue::as_hal"); |
| 185 | + |
| 186 | + let queue = self.hub.queues.get(id); |
| 187 | + let hal_queue = queue.raw().as_any().downcast_ref(); |
| 188 | + |
| 189 | + hal_queue_callback(hal_queue) |
| 190 | + } |
| 191 | + |
| 192 | + /// # Safety |
| 193 | + /// |
| 194 | + /// - The raw blas handle must not be manually destroyed |
| 195 | + pub unsafe fn blas_as_hal<A: HalApi, F: FnOnce(Option<&A::AccelerationStructure>) -> R, R>( |
| 196 | + &self, |
| 197 | + id: BlasId, |
| 198 | + hal_blas_callback: F, |
| 199 | + ) -> R { |
| 200 | + profiling::scope!("Blas::as_hal"); |
| 201 | + |
| 202 | + let hub = &self.hub; |
| 203 | + |
| 204 | + if let Ok(blas) = hub.blas_s.get(id).get() { |
| 205 | + let snatch_guard = blas.device.snatchable_lock.read(); |
| 206 | + let hal_blas = blas |
| 207 | + .try_raw(&snatch_guard) |
| 208 | + .ok() |
| 209 | + .and_then(|b| b.as_any().downcast_ref()); |
| 210 | + hal_blas_callback(hal_blas) |
| 211 | + } else { |
| 212 | + hal_blas_callback(None) |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + /// # Safety |
| 217 | + /// |
| 218 | + /// - The raw tlas handle must not be manually destroyed |
| 219 | + pub unsafe fn tlas_as_hal<A: HalApi, F: FnOnce(Option<&A::AccelerationStructure>) -> R, R>( |
| 220 | + &self, |
| 221 | + id: TlasId, |
| 222 | + hal_tlas_callback: F, |
| 223 | + ) -> R { |
| 224 | + profiling::scope!("Blas::as_hal"); |
| 225 | + |
| 226 | + let hub = &self.hub; |
| 227 | + |
| 228 | + if let Ok(tlas) = hub.tlas_s.get(id).get() { |
| 229 | + let snatch_guard = tlas.device.snatchable_lock.read(); |
| 230 | + let hal_tlas = tlas |
| 231 | + .try_raw(&snatch_guard) |
| 232 | + .ok() |
| 233 | + .and_then(|t| t.as_any().downcast_ref()); |
| 234 | + hal_tlas_callback(hal_tlas) |
| 235 | + } else { |
| 236 | + hal_tlas_callback(None) |
| 237 | + } |
| 238 | + } |
| 239 | +} |
0 commit comments