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
// 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.

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

use smallvec::SmallVec;

use descriptor::descriptor::DescriptorDesc;
use descriptor::pipeline_layout::PipelineLayoutDesc;
use descriptor::pipeline_layout::PipelineLayoutDescPcRange;

/// Runtime description of a pipeline layout.
#[derive(Debug, Clone)]
pub struct RuntimePipelineDesc {
    descriptors: SmallVec<[SmallVec<[Option<DescriptorDesc>; 5]>; 3]>,
    push_constants: SmallVec<[PipelineLayoutDescPcRange; 6]>,
}

impl RuntimePipelineDesc {
    /// Builds a new `RuntimePipelineDesc` from the descriptors and push constants descriptions.
    pub fn new<TSetsIter, TPushConstsIter, TDescriptorsIter>(
        desc: TSetsIter, push_constants: TPushConstsIter)
        -> Result<RuntimePipelineDesc, RuntimePipelineDescError>
        where TSetsIter: IntoIterator<Item = TDescriptorsIter>,
              TDescriptorsIter: IntoIterator<Item = Option<DescriptorDesc>>,
              TPushConstsIter: IntoIterator<Item = PipelineLayoutDescPcRange>
    {
        let descriptors = desc.into_iter().map(|s| s.into_iter().collect()).collect();
        let push_constants: SmallVec<[PipelineLayoutDescPcRange; 6]> =
            push_constants.into_iter().collect();

        for (a_id, a) in push_constants.iter().enumerate() {
            for b in push_constants.iter().skip(a_id + 1) {
                if a.offset <= b.offset && a.offset + a.size > b.offset {
                    return Err(RuntimePipelineDescError::PushConstantsConflict {
                                   first_offset: a.offset,
                                   first_size: a.size,
                                   second_offset: b.offset,
                               });
                }

                if b.offset <= a.offset && b.offset + b.size > a.offset {
                    return Err(RuntimePipelineDescError::PushConstantsConflict {
                                   first_offset: b.offset,
                                   first_size: b.size,
                                   second_offset: a.offset,
                               });
                }
            }
        }

        Ok(RuntimePipelineDesc {
               descriptors,
               push_constants,
           })
    }
}

unsafe impl PipelineLayoutDesc for RuntimePipelineDesc {
    #[inline]
    fn num_sets(&self) -> usize {
        self.descriptors.len()
    }

    #[inline]
    fn num_bindings_in_set(&self, set: usize) -> Option<usize> {
        self.descriptors.get(set).map(|s| s.len())
    }

    #[inline]
    fn descriptor(&self, set: usize, binding: usize) -> Option<DescriptorDesc> {
        self.descriptors
            .get(set)
            .and_then(|s| s.get(binding).cloned().unwrap_or(None))
    }

    #[inline]
    fn num_push_constants_ranges(&self) -> usize {
        self.push_constants.len()
    }

    #[inline]
    fn push_constants_range(&self, num: usize) -> Option<PipelineLayoutDescPcRange> {
        self.push_constants.get(num).cloned()
    }
}

/// Error when building a persistent descriptor set.
#[derive(Debug, Clone)]
pub enum RuntimePipelineDescError {
    /// Conflict between different push constants ranges.
    PushConstantsConflict {
        first_offset: usize,
        first_size: usize,
        second_offset: usize,
    },
}

impl error::Error for RuntimePipelineDescError {
    #[inline]
    fn description(&self) -> &str {
        match *self {
            RuntimePipelineDescError::PushConstantsConflict { .. } => {
                "conflict between different push constants ranges"
            },
        }
    }
}

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

#[cfg(test)]
mod tests {
    use descriptor::descriptor::DescriptorDesc;
    use descriptor::descriptor::ShaderStages;
    use descriptor::pipeline_layout::PipelineLayoutDescPcRange;
    use descriptor::pipeline_layout::RuntimePipelineDesc;
    use descriptor::pipeline_layout::RuntimePipelineDescError;
    use std::iter;

    #[test]
    fn pc_conflict() {
        let range1 = PipelineLayoutDescPcRange {
            offset: 0,
            size: 8,
            stages: ShaderStages::all(),
        };

        let range2 = PipelineLayoutDescPcRange {
            offset: 4,
            size: 8,
            stages: ShaderStages::all(),
        };

        let r = RuntimePipelineDesc::new::<_, _, iter::Empty<Option<DescriptorDesc>>>
                                    (iter::empty(), iter::once(range1).chain(iter::once(range2)));

        match r {
            Err(RuntimePipelineDescError::PushConstantsConflict {
                    first_offset: 0,
                    first_size: 8,
                    second_offset: 4,
                }) => (),
            _ => panic!(),   // test failed
        }
    }
}