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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Copyright (c) 2017 The vulkano developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.

//! Contains the `check_desc_against_limits` function and the `PipelineLayoutLimitsError` error.

use std::error;
use std::fmt;

use descriptor::descriptor::DescriptorType;
use descriptor::descriptor::ShaderStages;
use descriptor::pipeline_layout::PipelineLayoutDesc;
use descriptor::pipeline_layout::PipelineLayoutDescPcRange;
use instance::Limits;

/// Checks whether the pipeline layout description fulfills the device limits requirements.
pub fn check_desc_against_limits<D>(desc: &D, limits: Limits)
                                    -> Result<(), PipelineLayoutLimitsError>
    where D: ?Sized + PipelineLayoutDesc
{
    let mut num_resources = Counter::default();
    let mut num_samplers = Counter::default();
    let mut num_uniform_buffers = Counter::default();
    let mut num_uniform_buffers_dynamic = 0;
    let mut num_storage_buffers = Counter::default();
    let mut num_storage_buffers_dynamic = 0;
    let mut num_sampled_images = Counter::default();
    let mut num_storage_images = Counter::default();
    let mut num_input_attachments = Counter::default();

    for set in 0 .. desc.num_sets() {
        let num_bindings_in_set = match desc.num_bindings_in_set(set) {
            None => continue,
            Some(n) => n,
        };

        for binding in 0 .. num_bindings_in_set {
            let descriptor = match desc.descriptor(set, binding) {
                None => continue,
                Some(n) => n,
            };

            num_resources.increment(descriptor.array_count, &descriptor.stages);

            match descriptor.ty.ty().expect("Not implemented yet") {        // TODO:
                DescriptorType::Sampler => {
                    num_samplers.increment(descriptor.array_count, &descriptor.stages);
                },
                DescriptorType::CombinedImageSampler => {
                    num_samplers.increment(descriptor.array_count, &descriptor.stages);
                    num_sampled_images.increment(descriptor.array_count, &descriptor.stages);
                },
                DescriptorType::SampledImage |
                DescriptorType::UniformTexelBuffer => {
                    num_sampled_images.increment(descriptor.array_count, &descriptor.stages);
                },
                DescriptorType::StorageImage |
                DescriptorType::StorageTexelBuffer => {
                    num_storage_images.increment(descriptor.array_count, &descriptor.stages);
                },
                DescriptorType::UniformBuffer => {
                    num_uniform_buffers.increment(descriptor.array_count, &descriptor.stages);
                },
                DescriptorType::UniformBufferDynamic => {
                    num_uniform_buffers.increment(descriptor.array_count, &descriptor.stages);
                    num_uniform_buffers_dynamic += 1;
                },
                DescriptorType::StorageBuffer => {
                    num_storage_buffers.increment(descriptor.array_count, &descriptor.stages);
                },
                DescriptorType::StorageBufferDynamic => {
                    num_storage_buffers.increment(descriptor.array_count, &descriptor.stages);
                    num_storage_buffers_dynamic += 1;
                },
                DescriptorType::InputAttachment => {
                    num_input_attachments.increment(descriptor.array_count, &descriptor.stages);
                },
            }
        }
    }

    if desc.num_sets() > limits.max_bound_descriptor_sets() as usize {
        return Err(PipelineLayoutLimitsError::MaxDescriptorSetsLimitExceeded {
                       limit: limits.max_bound_descriptor_sets() as usize,
                       requested: desc.num_sets(),
                   });
    }

    if num_resources.max_per_stage() > limits.max_per_stage_resources() {
        return Err(PipelineLayoutLimitsError::MaxPerStageResourcesLimitExceeded {
                       limit: limits.max_per_stage_resources(),
                       requested: num_resources.max_per_stage(),
                   });
    }

    if num_samplers.max_per_stage() > limits.max_per_stage_descriptor_samplers() {
        return Err(PipelineLayoutLimitsError::MaxPerStageDescriptorSamplersLimitExceeded {
                       limit: limits.max_per_stage_descriptor_samplers(),
                       requested: num_samplers.max_per_stage(),
                   });
    }
    if num_uniform_buffers.max_per_stage() > limits.max_per_stage_descriptor_uniform_buffers() {
        return Err(PipelineLayoutLimitsError::MaxPerStageDescriptorUniformBuffersLimitExceeded {
                       limit: limits.max_per_stage_descriptor_uniform_buffers(),
                       requested: num_uniform_buffers.max_per_stage(),
                   });
    }
    if num_storage_buffers.max_per_stage() > limits.max_per_stage_descriptor_storage_buffers() {
        return Err(PipelineLayoutLimitsError::MaxPerStageDescriptorStorageBuffersLimitExceeded {
                       limit: limits.max_per_stage_descriptor_storage_buffers(),
                       requested: num_storage_buffers.max_per_stage(),
                   });
    }
    if num_sampled_images.max_per_stage() > limits.max_per_stage_descriptor_sampled_images() {
        return Err(PipelineLayoutLimitsError::MaxPerStageDescriptorSampledImagesLimitExceeded {
                       limit: limits.max_per_stage_descriptor_sampled_images(),
                       requested: num_sampled_images.max_per_stage(),
                   });
    }
    if num_storage_images.max_per_stage() > limits.max_per_stage_descriptor_storage_images() {
        return Err(PipelineLayoutLimitsError::MaxPerStageDescriptorStorageImagesLimitExceeded {
                       limit: limits.max_per_stage_descriptor_storage_images(),
                       requested: num_storage_images.max_per_stage(),
                   });
    }
    if num_input_attachments.max_per_stage() > limits.max_per_stage_descriptor_input_attachments() {
        return Err(PipelineLayoutLimitsError::MaxPerStageDescriptorInputAttachmentsLimitExceeded {
                       limit: limits.max_per_stage_descriptor_input_attachments(),
                       requested: num_input_attachments.max_per_stage(),
                   });
    }

    if num_samplers.total > limits.max_descriptor_set_samplers() {
        return Err(PipelineLayoutLimitsError::MaxDescriptorSetSamplersLimitExceeded {
                       limit: limits.max_descriptor_set_samplers(),
                       requested: num_samplers.total,
                   });
    }
    if num_uniform_buffers.total > limits.max_descriptor_set_uniform_buffers() {
        return Err(PipelineLayoutLimitsError::MaxDescriptorSetUniformBuffersLimitExceeded {
                       limit: limits.max_descriptor_set_uniform_buffers(),
                       requested: num_uniform_buffers.total,
                   });
    }
    if num_uniform_buffers_dynamic > limits.max_descriptor_set_uniform_buffers_dynamic() {
        return Err(PipelineLayoutLimitsError::MaxDescriptorSetUniformBuffersDynamicLimitExceeded {
                       limit: limits.max_descriptor_set_uniform_buffers_dynamic(),
                       requested: num_uniform_buffers_dynamic,
                   });
    }
    if num_storage_buffers.total > limits.max_descriptor_set_storage_buffers() {
        return Err(PipelineLayoutLimitsError::MaxDescriptorSetStorageBuffersLimitExceeded {
                       limit: limits.max_descriptor_set_storage_buffers(),
                       requested: num_storage_buffers.total,
                   });
    }
    if num_storage_buffers_dynamic > limits.max_descriptor_set_storage_buffers_dynamic() {
        return Err(PipelineLayoutLimitsError::MaxDescriptorSetStorageBuffersDynamicLimitExceeded {
                       limit: limits.max_descriptor_set_storage_buffers_dynamic(),
                       requested: num_storage_buffers_dynamic,
                   });
    }
    if num_sampled_images.total > limits.max_descriptor_set_sampled_images() {
        return Err(PipelineLayoutLimitsError::MaxDescriptorSetSampledImagesLimitExceeded {
                       limit: limits.max_descriptor_set_sampled_images(),
                       requested: num_sampled_images.total,
                   });
    }
    if num_storage_images.total > limits.max_descriptor_set_storage_images() {
        return Err(PipelineLayoutLimitsError::MaxDescriptorSetStorageImagesLimitExceeded {
                       limit: limits.max_descriptor_set_storage_images(),
                       requested: num_storage_images.total,
                   });
    }
    if num_input_attachments.total > limits.max_descriptor_set_input_attachments() {
        return Err(PipelineLayoutLimitsError::MaxDescriptorSetInputAttachmentsLimitExceeded {
                       limit: limits.max_descriptor_set_input_attachments(),
                       requested: num_input_attachments.total,
                   });
    }

    for pc_id in 0 .. desc.num_push_constants_ranges() {
        let PipelineLayoutDescPcRange { offset, size, .. } = {
            match desc.push_constants_range(pc_id) {
                Some(o) => o,
                None => continue,
            }
        };

        if offset + size > limits.max_push_constants_size() as usize {
            return Err(PipelineLayoutLimitsError::MaxPushConstantsSizeExceeded {
                           limit: limits.max_push_constants_size() as usize,
                           requested: offset + size,
                       });
        }
    }

    Ok(())
}

/// The pipeline layout description isn't compatible with the hardware limits.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PipelineLayoutLimitsError {
    /// The maximum number of descriptor sets has been exceeded.
    MaxDescriptorSetsLimitExceeded {
        /// The limit that must be fulfilled.
        limit: usize,
        /// What was requested.
        requested: usize,
    },

    /// The maximum size of push constants has been exceeded.
    MaxPushConstantsSizeExceeded {
        /// The limit that must be fulfilled.
        limit: usize,
        /// What was requested.
        requested: usize,
    },

    /// The `max_per_stage_resources()` limit has been exceeded.
    MaxPerStageResourcesLimitExceeded {
        /// The limit that must be fulfilled.
        limit: u32,
        /// What was requested.
        requested: u32,
    },

    /// The `max_per_stage_descriptor_samplers()` limit has been exceeded.
    MaxPerStageDescriptorSamplersLimitExceeded {
        /// The limit that must be fulfilled.
        limit: u32,
        /// What was requested.
        requested: u32,
    },

    /// The `max_per_stage_descriptor_uniform_buffers()` limit has been exceeded.
    MaxPerStageDescriptorUniformBuffersLimitExceeded {
        /// The limit that must be fulfilled.
        limit: u32,
        /// What was requested.
        requested: u32,
    },

    /// The `max_per_stage_descriptor_storage_buffers()` limit has been exceeded.
    MaxPerStageDescriptorStorageBuffersLimitExceeded {
        /// The limit that must be fulfilled.
        limit: u32,
        /// What was requested.
        requested: u32,
    },

    /// The `max_per_stage_descriptor_sampled_images()` limit has been exceeded.
    MaxPerStageDescriptorSampledImagesLimitExceeded {
        /// The limit that must be fulfilled.
        limit: u32,
        /// What was requested.
        requested: u32,
    },

    /// The `max_per_stage_descriptor_storage_images()` limit has been exceeded.
    MaxPerStageDescriptorStorageImagesLimitExceeded {
        /// The limit that must be fulfilled.
        limit: u32,
        /// What was requested.
        requested: u32,
    },

    /// The `max_per_stage_descriptor_input_attachments()` limit has been exceeded.
    MaxPerStageDescriptorInputAttachmentsLimitExceeded {
        /// The limit that must be fulfilled.
        limit: u32,
        /// What was requested.
        requested: u32,
    },

    /// The `max_descriptor_set_samplers()` limit has been exceeded.
    MaxDescriptorSetSamplersLimitExceeded {
        /// The limit that must be fulfilled.
        limit: u32,
        /// What was requested.
        requested: u32,
    },

    /// The `max_descriptor_set_uniform_buffers()` limit has been exceeded.
    MaxDescriptorSetUniformBuffersLimitExceeded {
        /// The limit that must be fulfilled.
        limit: u32,
        /// What was requested.
        requested: u32,
    },

    /// The `max_descriptor_set_uniform_buffers_dynamic()` limit has been exceeded.
    MaxDescriptorSetUniformBuffersDynamicLimitExceeded {
        /// The limit that must be fulfilled.
        limit: u32,
        /// What was requested.
        requested: u32,
    },

    /// The `max_descriptor_set_storage_buffers()` limit has been exceeded.
    MaxDescriptorSetStorageBuffersLimitExceeded {
        /// The limit that must be fulfilled.
        limit: u32,
        /// What was requested.
        requested: u32,
    },

    /// The `max_descriptor_set_storage_buffers_dynamic()` limit has been exceeded.
    MaxDescriptorSetStorageBuffersDynamicLimitExceeded {
        /// The limit that must be fulfilled.
        limit: u32,
        /// What was requested.
        requested: u32,
    },

    /// The `max_descriptor_set_sampled_images()` limit has been exceeded.
    MaxDescriptorSetSampledImagesLimitExceeded {
        /// The limit that must be fulfilled.
        limit: u32,
        /// What was requested.
        requested: u32,
    },

    /// The `max_descriptor_set_storage_images()` limit has been exceeded.
    MaxDescriptorSetStorageImagesLimitExceeded {
        /// The limit that must be fulfilled.
        limit: u32,
        /// What was requested.
        requested: u32,
    },

    /// The `max_descriptor_set_input_attachments()` limit has been exceeded.
    MaxDescriptorSetInputAttachmentsLimitExceeded {
        /// The limit that must be fulfilled.
        limit: u32,
        /// What was requested.
        requested: u32,
    },
}

impl error::Error for PipelineLayoutLimitsError {
    #[inline]
    fn description(&self) -> &str {
        match *self {
            PipelineLayoutLimitsError::MaxDescriptorSetsLimitExceeded { .. } => {
                "the maximum number of descriptor sets has been exceeded"
            },
            PipelineLayoutLimitsError::MaxPushConstantsSizeExceeded { .. } => {
                "the maximum size of push constants has been exceeded"
            },
            PipelineLayoutLimitsError::MaxPerStageResourcesLimitExceeded { .. } => {
                "the `max_per_stage_resources()` limit has been exceeded"
            },
            PipelineLayoutLimitsError::MaxPerStageDescriptorSamplersLimitExceeded { .. } => {
                "the `max_per_stage_descriptor_samplers()` limit has been exceeded"
            },
            PipelineLayoutLimitsError::MaxPerStageDescriptorUniformBuffersLimitExceeded {
                ..
            } => {
                "the `max_per_stage_descriptor_uniform_buffers()` limit has been exceeded"
            },
            PipelineLayoutLimitsError::MaxPerStageDescriptorStorageBuffersLimitExceeded {
                ..
            } => {
                "the `max_per_stage_descriptor_storage_buffers()` limit has been exceeded"
            },
            PipelineLayoutLimitsError::MaxPerStageDescriptorSampledImagesLimitExceeded {
                ..
            } => {
                "the `max_per_stage_descriptor_sampled_images()` limit has been exceeded"
            },
            PipelineLayoutLimitsError::MaxPerStageDescriptorStorageImagesLimitExceeded {
                ..
            } => {
                "the `max_per_stage_descriptor_storage_images()` limit has been exceeded"
            },
            PipelineLayoutLimitsError::MaxPerStageDescriptorInputAttachmentsLimitExceeded {
                ..
            } => {
                "the `max_per_stage_descriptor_input_attachments()` limit has been exceeded"
            },
            PipelineLayoutLimitsError::MaxDescriptorSetSamplersLimitExceeded { .. } => {
                "the `max_descriptor_set_samplers()` limit has been exceeded"
            },
            PipelineLayoutLimitsError::MaxDescriptorSetUniformBuffersLimitExceeded { .. } => {
                "the `max_descriptor_set_uniform_buffers()` limit has been exceeded"
            },
            PipelineLayoutLimitsError::MaxDescriptorSetUniformBuffersDynamicLimitExceeded {
                ..
            } => {
                "the `max_descriptor_set_uniform_buffers_dynamic()` limit has been exceeded"
            },
            PipelineLayoutLimitsError::MaxDescriptorSetStorageBuffersLimitExceeded { .. } => {
                "the `max_descriptor_set_storage_buffers()` limit has been exceeded"
            },
            PipelineLayoutLimitsError::MaxDescriptorSetStorageBuffersDynamicLimitExceeded {
                ..
            } => {
                "the `max_descriptor_set_storage_buffers_dynamic()` limit has been exceeded"
            },
            PipelineLayoutLimitsError::MaxDescriptorSetSampledImagesLimitExceeded { .. } => {
                "the `max_descriptor_set_sampled_images()` limit has been exceeded"
            },
            PipelineLayoutLimitsError::MaxDescriptorSetStorageImagesLimitExceeded { .. } => {
                "the `max_descriptor_set_storage_images()` limit has been exceeded"
            },
            PipelineLayoutLimitsError::MaxDescriptorSetInputAttachmentsLimitExceeded { .. } => {
                "the `max_descriptor_set_input_attachments()` limit has been exceeded"
            },
        }
    }
}

impl fmt::Display for PipelineLayoutLimitsError {
    #[inline]
    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        write!(fmt, "{}", error::Error::description(self))
    }
}

// Helper struct for the main function.
#[derive(Default)]
struct Counter {
    total: u32,
    compute: u32,
    vertex: u32,
    geometry: u32,
    tess_ctl: u32,
    tess_eval: u32,
    frag: u32,
}

impl Counter {
    fn increment(&mut self, num: u32, stages: &ShaderStages) {
        self.total += num;
        if stages.compute {
            self.compute += num;
        }
        if stages.vertex {
            self.vertex += num;
        }
        if stages.tessellation_control {
            self.tess_ctl += num;
        }
        if stages.tessellation_evaluation {
            self.tess_eval += num;
        }
        if stages.geometry {
            self.geometry += num;
        }
        if stages.fragment {
            self.frag += num;
        }
    }

    fn max_per_stage(&self) -> u32 {
        let mut max = 0;
        if self.compute > max {
            max = self.compute;
        }
        if self.vertex > max {
            max = self.vertex;
        }
        if self.geometry > max {
            max = self.geometry;
        }
        if self.tess_ctl > max {
            max = self.tess_ctl;
        }
        if self.tess_eval > max {
            max = self.tess_eval;
        }
        if self.frag > max {
            max = self.frag;
        }
        max
    }
}