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
use super::{Join, Join3, Join4, Join5, Sequence, Sequence3, Sequence4, Sequence5}; use crate::task::Map; /// Trait for types that represent a computational task is to be partly or completely executed on a /// GPU. /// /// A [GpuTask] implementation is associated with a specific execution context type `Ec` (e.g. the /// base [Connection], a [RenderPassContext], or a [PipelineTaskContext]). A task executor /// associated with that context may attempt to make progress on the task by calling /// [GpuTask::progress] and providing an exclusive reference to an instance of that context, but /// only if this context instance is compatible with [GpuTask::context_id]. /// /// # Unsafe /// /// If a context instance is compatible with [GpuTask::context_id], then invoking /// [GpuTask::progress] with this instance must not result in undefined behaviour. /// /// [Connection]: web_glitz::runtime::Connection; /// [RenderPassContext]: web_glitz::rendering::RenderPassContext; /// [PipelineTaskContext]: web_glitz::rendering::PipelineTaskContext; pub unsafe trait GpuTask<Ec> { /// The type of output that results from this task finishing. type Output; /// Identifies the context(s) a [GpuTask] may be used with. /// /// If the [GpuTask] may be used with any context, then [GpuTask::context_id] should return /// `ContextId::Any`; if it may only be used with one specific context instance, then it should /// return `ContextId::Id(context_id)`, where `context_id` is the ID associated with the context /// (see [RenderingContext::id]). fn context_id(&self) -> ContextId; /// Attempts to progress this [GpuTask] towards its finished state using the given /// [execution_context]. /// /// If this call to [GpuTask::progress] resulted in the task finishing, then it should return /// `Progress::Finished(output)`, where `output` is the task's output. The task executor should /// then drop this [GpuTask]; it should never call this method again. /// /// Otherwise, [GpuTask::progress] may return `Progress::ContinueFenced`. In this case the task /// executor will insert a GPU fence into the command stream. It will call this method again /// once that fence has become signalled. fn progress(&mut self, execution_context: &mut Ec) -> Progress<Self::Output>; } unsafe impl<T, Ec> GpuTask<Ec> for Box<T> where T: GpuTask<Ec> + ?Sized, { type Output = T::Output; fn context_id(&self) -> ContextId { self.as_ref().context_id() } fn progress(&mut self, execution_context: &mut Ec) -> Progress<Self::Output> { self.as_mut().progress(execution_context) } } pub trait GpuTaskExt<Ec>: GpuTask<Ec> { fn map<F, U>(self, f: F) -> Map<Self, F> where F: FnOnce(Self::Output) -> U, Self: Sized; /// Combines this task with another task `b`, waiting for both tasks to complete in no /// particular order. /// /// This returns a new "joined" task. This joined task may progress the its sub-tasks in any /// order. The joined task will finish when both sub-tasks have finished. When it finishes, it /// will output a tuple `(A, B)` where `A` is this tasks output and `B` is task `b`'s output. /// /// # Panics /// /// Panics if the [ContextId] of `b` is not compatible with this task's [ContextId]. fn join<B>(self, b: B) -> Join<Self, B, Ec> where B: GpuTask<Ec>, Self: Sized; /// Combines this task with 2 other tasks `b` and `c`, waiting for all tasks to complete in no /// particular order. /// /// This returns a new "joined" task. This joined task may progress the its sub-tasks in any /// order. The joined task will finish when all sub-tasks have finished. When it finishes, it /// will output a tuple `(A, B, C)` where `A` is this tasks output, `B` is task `b`'s output and /// `C` is task `c`'s output. /// /// # Panics /// /// Panics if any of the [ContextId]s of `b` and `c` are not compatible with this task's /// [ContextId]. fn join3<B, C>(self, b: B, c: C) -> Join3<Self, B, C, Ec> where B: GpuTask<Ec>, C: GpuTask<Ec>, Self: Sized; /// Combines this task with 3 other tasks `b`, `c` and `d`, waiting for all tasks to complete in /// no particular order. /// /// This returns a new "joined" task. This joined task may progress the its sub-tasks in any /// order. The joined task will finish when all sub-tasks have finished. When it finishes, it /// will output a tuple `(A, B, C, D)` where `A` is this tasks output, `B` is task `b`'s output, /// `C` is task `c`'s output and `D` is task `d`'s output. /// /// # Panics /// /// Panics if any of the [ContextId]s of `b`, `c` and `d` are not compatible with this task's /// [ContextId]. fn join4<B, C, D>(self, b: B, c: C, d: D) -> Join4<Self, B, C, D, Ec> where B: GpuTask<Ec>, C: GpuTask<Ec>, D: GpuTask<Ec>, Self: Sized; /// Combines this task with 4 other tasks `b`, `c`, `d` and `e`, waiting for all tasks to /// complete in no particular order. /// /// This returns a new "joined" task. This joined task may progress the its sub-tasks in any /// order. The joined task will finish when all sub-tasks have finished. When it finishes, it /// will output a tuple `(A, B, C, D, E)` where `A` is this tasks output, `B` is task `b`'s /// output, `C` is task `c`'s output, `D` is task `d`'s output and `E` is task `e`'s output. /// /// # Panics /// /// Panics if any of the [ContextId]s of `b`, `c`, `d` and `e` are not compatible with this /// task's [ContextId]. fn join5<B, C, D, E>(self, b: B, c: C, d: D, e: E) -> Join5<Self, B, C, D, E, Ec> where B: GpuTask<Ec>, C: GpuTask<Ec>, D: GpuTask<Ec>, E: GpuTask<Ec>, Self: Sized; /// Combines this task with another task `b`, waiting for both tasks to complete in order. /// /// This returns a new "sequenced" task. This sequenced task must progress its sub-tasks in /// order: it may only start to progress the next task if the previous task has finished. The /// sequenced task will finish when the last sub-task has finished. When it finishes, it will /// output a tuple `(A, B)` where `A` is this tasks output and `B` is task `b`'s output. /// /// # Panics /// /// Panics if the [ContextId] of `b` is not compatible with this task's [ContextId]. fn sequence<B>(self, b: B) -> Sequence<Self, B, Ec> where B: GpuTask<Ec>, Self: Sized; /// Combines this task with 2 other tasks `b` and `c`, waiting for all tasks to complete in /// order. /// /// This returns a new "sequenced" task. This sequenced task must progress its sub-tasks in /// order: it may only start to progress the next task if the previous task has finished. The /// sequenced task will finish when the last sub-task has finished. When it finishes, it will /// output a tuple `(A, B, C)` where `A` is this tasks output, `B` is task `b`'s output and `C` /// is task `c`'s output. /// /// # Panics /// /// Panics if any of the [ContextId]s of `b` and `c` are not compatible with this task's /// [ContextId]. fn sequence3<B, C>(self, b: B, c: C) -> Sequence3<Self, B, C, Ec> where B: GpuTask<Ec>, C: GpuTask<Ec>, Self: Sized; /// Combines this task with 3 other tasks `b`, `c` and `d`, waiting for all tasks to complete in /// order. /// /// This returns a new "sequenced" task. This sequenced task must progress its sub-tasks in /// order: it may only start to progress the next task if the previous task has finished. The /// sequenced task will finish when the last sub-task has finished. When it finishes, it will /// output a tuple `(A, B, C, D)` where `A` is this tasks output, `B` is task `b`'s output, `C` /// is task `c`'s output and `D` is task `d`'s output. /// /// # Panics /// /// Panics if any of the [ContextId]s of `b`, `c` and `d` are not compatible with this task's /// [ContextId]. fn sequence4<B, C, D>(self, b: B, c: C, d: D) -> Sequence4<Self, B, C, D, Ec> where B: GpuTask<Ec>, C: GpuTask<Ec>, D: GpuTask<Ec>, Self: Sized; /// Combines this task with 4 other tasks `b`, `c`, `d` and `e`, waiting for all tasks to /// complete in order. /// /// This returns a new "sequenced" task. This sequenced task must progress its sub-tasks in /// order: it may only start to progress the next task if the previous task has finished. The /// sequenced task will finish when the last sub-task has finished. When it finishes, it will /// output a tuple `(A, B, C, D, E)` where `A` is this tasks output, `B` is task `b`'s output, /// `C` is task `c`'s output, `D` is task `d`'s output and `E` is task `e`'s output. /// /// # Panics /// /// Panics if any of the [ContextId]s of `b`, `c`, `d` and `e` are not compatible with this /// task's [ContextId]. fn sequence5<B, C, D, E>(self, b: B, c: C, d: D, e: E) -> Sequence5<Self, B, C, D, E, Ec> where B: GpuTask<Ec>, C: GpuTask<Ec>, D: GpuTask<Ec>, E: GpuTask<Ec>, Self: Sized; } impl<T, Ec> GpuTaskExt<Ec> for T where T: GpuTask<Ec>, { fn map<F, U>(self, f: F) -> Map<Self, F> where F: FnOnce(Self::Output) -> U, { Map::new(self, f) } fn join<B>(self, b: B) -> Join<T, B, Ec> where B: GpuTask<Ec>, { Join::new(self, b) } fn join3<B, C>(self, b: B, c: C) -> Join3<T, B, C, Ec> where B: GpuTask<Ec>, C: GpuTask<Ec>, { Join3::new(self, b, c) } fn join4<B, C, D>(self, b: B, c: C, d: D) -> Join4<T, B, C, D, Ec> where B: GpuTask<Ec>, C: GpuTask<Ec>, D: GpuTask<Ec>, { Join4::new(self, b, c, d) } fn join5<B, C, D, E>(self, b: B, c: C, d: D, e: E) -> Join5<T, B, C, D, E, Ec> where B: GpuTask<Ec>, C: GpuTask<Ec>, D: GpuTask<Ec>, E: GpuTask<Ec>, { Join5::new(self, b, c, d, e) } fn sequence<B>(self, b: B) -> Sequence<T, B, Ec> where B: GpuTask<Ec>, { Sequence::new(self, b) } fn sequence3<B, C>(self, b: B, c: C) -> Sequence3<T, B, C, Ec> where B: GpuTask<Ec>, C: GpuTask<Ec>, { Sequence3::new(self, b, c) } fn sequence4<B, C, D>(self, b: B, c: C, d: D) -> Sequence4<T, B, C, D, Ec> where B: GpuTask<Ec>, C: GpuTask<Ec>, D: GpuTask<Ec>, { Sequence4::new(self, b, c, d) } fn sequence5<B, C, D, E>(self, b: B, c: C, d: D, e: E) -> Sequence5<T, B, C, D, E, Ec> where B: GpuTask<Ec>, C: GpuTask<Ec>, D: GpuTask<Ec>, E: GpuTask<Ec>, { Sequence5::new(self, b, c, d, e) } } /// Returned from [GpuTask::progress], signifies the current state of progress for the task. /// /// See [GpuTask::progress] for details. pub enum Progress<T> { Finished(T), ContinueFenced, } impl<T> Progress<T> { /// Modifies the output by applying `f`. pub fn map<U, F>(self, f: F) -> Progress<U> where F: FnOnce(T) -> U, { match self { Progress::Finished(value) => Progress::Finished(f(value)), Progress::ContinueFenced => Progress::ContinueFenced, } } } /// Returned from [GpuTask::context_id], identifies the context(s) a [GpuTask] may be used with. /// /// See [GpuTask::context_id] for details. #[derive(Clone, Copy, PartialEq, Debug)] pub enum ContextId { Any, Id(u64), } impl ContextId { /// Attempts to combine two [ContextId]s, or returns an [IncompatibleContextIds] error when they /// are incompatible. /// /// Two [ContextId]s are compatible if: /// /// - Either or both [ContextId]s are [ContextId::Any]. In this case [ContextId::Any] will be /// returned. /// - Both are [ContextId::Id] and the two IDs are identical. In this case [ContextId::Id(id)] /// will be returned, where `id` is the ID shared by both [ContextId]s. /// /// Two [ContextId]s are incompatible if: /// /// - Both are [ContextId::Id] and the two IDs are not identical. In this case an /// [IncompatibleContextIds] error will be returned. pub fn combine(&self, other: ContextId) -> Result<ContextId, IncompatibleContextIds> { match self { ContextId::Any => Ok(other), ContextId::Id(id) => { if other == ContextId::Any || other == ContextId::Id(*id) { Ok(*self) } else { Err(IncompatibleContextIds(*self, other)) } } } } } /// Error returned by [ContextId::combine] if the two context IDs that are combined are /// incompatible. /// /// Two context IDs are incompatible if they are both [ContextId::Id] and the ID values are not /// identical. #[derive(Clone, Copy, PartialEq, Debug)] pub struct IncompatibleContextIds(ContextId, ContextId); #[derive(Clone)] pub struct Empty; unsafe impl<Ec> GpuTask<Ec> for Empty { type Output = (); fn context_id(&self) -> ContextId { ContextId::Any } fn progress(&mut self, _execution_context: &mut Ec) -> Progress<Self::Output> { Progress::Finished(()) } }