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
// Copyright (c) 2016 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::ops::BitOr;
use vk;

/// Describes how a buffer is going to be used. This is **not** just an optimization.
///
/// If you try to use a buffer in a way that you didn't declare, a panic will happen.
///
/// Some methods are provided to build `BufferUsage` structs for some common situations. However
/// there is no restriction in the combination of BufferUsages that can be enabled.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct BufferUsage {
    pub transfer_source: bool,
    pub transfer_destination: bool,
    pub uniform_texel_buffer: bool,
    pub storage_texel_buffer: bool,
    pub uniform_buffer: bool,
    pub storage_buffer: bool,
    pub index_buffer: bool,
    pub vertex_buffer: bool,
    pub indirect_buffer: bool,
}

impl BufferUsage {
    /// Turns this `BufferUsage` into raw Vulkan bits.
    pub(crate) fn to_vulkan_bits(&self) -> vk::BufferUsageFlagBits {
        let mut result = 0;
        if self.transfer_source {
            result |= vk::BUFFER_USAGE_TRANSFER_SRC_BIT;
        }
        if self.transfer_destination {
            result |= vk::BUFFER_USAGE_TRANSFER_DST_BIT;
        }
        if self.uniform_texel_buffer {
            result |= vk::BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT;
        }
        if self.storage_texel_buffer {
            result |= vk::BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT;
        }
        if self.uniform_buffer {
            result |= vk::BUFFER_USAGE_UNIFORM_BUFFER_BIT;
        }
        if self.storage_buffer {
            result |= vk::BUFFER_USAGE_STORAGE_BUFFER_BIT;
        }
        if self.index_buffer {
            result |= vk::BUFFER_USAGE_INDEX_BUFFER_BIT;
        }
        if self.vertex_buffer {
            result |= vk::BUFFER_USAGE_VERTEX_BUFFER_BIT;
        }
        if self.indirect_buffer {
            result |= vk::BUFFER_USAGE_INDIRECT_BUFFER_BIT;
        }
        result
    }

    /// Builds a `BufferUsage` with all values set to false.
    #[inline]
    pub fn none() -> BufferUsage {
        BufferUsage {
            transfer_source: false,
            transfer_destination: false,
            uniform_texel_buffer: false,
            storage_texel_buffer: false,
            uniform_buffer: false,
            storage_buffer: false,
            index_buffer: false,
            vertex_buffer: false,
            indirect_buffer: false,
        }
    }

    /// Builds a `BufferUsage` with all values set to true. Can be used for quick prototyping.
    #[inline]
    pub fn all() -> BufferUsage {
        BufferUsage {
            transfer_source: true,
            transfer_destination: true,
            uniform_texel_buffer: true,
            storage_texel_buffer: true,
            uniform_buffer: true,
            storage_buffer: true,
            index_buffer: true,
            vertex_buffer: true,
            indirect_buffer: true,
        }
    }

    /// Builds a `BufferUsage` with `transfer_source` set to true and the rest to false.
    #[inline]
    pub fn transfer_source() -> BufferUsage {
        BufferUsage {
            transfer_source: true,
            ..BufferUsage::none()
        }
    }

    /// Builds a `BufferUsage` with `transfer_destination` set to true and the rest to false.
    #[inline]
    pub fn transfer_destination() -> BufferUsage {
        BufferUsage {
            transfer_destination: true,
            ..BufferUsage::none()
        }
    }

    /// Builds a `BufferUsage` with `vertex_buffer` set to true and the rest to false.
    #[inline]
    pub fn vertex_buffer() -> BufferUsage {
        BufferUsage {
            vertex_buffer: true,
            ..BufferUsage::none()
        }
    }

    /// Builds a `BufferUsage` with `vertex_buffer` and `transfer_destination` set to true and the rest
    /// to false.
    #[inline]
    pub fn vertex_buffer_transfer_destination() -> BufferUsage {
        BufferUsage {
            vertex_buffer: true,
            transfer_destination: true,
            ..BufferUsage::none()
        }
    }

    /// Builds a `BufferUsage` with `index_buffer` set to true and the rest to false.
    #[inline]
    pub fn index_buffer() -> BufferUsage {
        BufferUsage {
            index_buffer: true,
            ..BufferUsage::none()
        }
    }

    /// Builds a `BufferUsage` with `index_buffer` and `transfer_destination` set to true and the rest to false.
    #[inline]
    pub fn index_buffer_transfer_destination() -> BufferUsage {
        BufferUsage {
            index_buffer: true,
            transfer_destination: true,
            ..BufferUsage::none()
        }
    }

    /// Builds a `BufferUsage` with `uniform_buffer` set to true and the rest to false.
    #[inline]
    pub fn uniform_buffer() -> BufferUsage {
        BufferUsage {
            uniform_buffer: true,
            ..BufferUsage::none()
        }
    }

    /// Builds a `BufferUsage` with `uniform_buffer` and `transfer_destination` set to true and the rest
    /// to false.
    #[inline]
    pub fn uniform_buffer_transfer_destination() -> BufferUsage {
        BufferUsage {
            uniform_buffer: true,
            transfer_destination: true,
            ..BufferUsage::none()
        }
    }

    /// Builds a `BufferUsage` with `indirect_buffer` set to true and the rest to false.
    #[inline]
    pub fn indirect_buffer() -> BufferUsage {
        BufferUsage {
            indirect_buffer: true,
            ..BufferUsage::none()
        }
    }

    /// Builds a `BufferUsage` with `indirect_buffer` and `transfer_destination` set to true and the rest
    /// to false.
    #[inline]
    pub fn indirect_buffer_transfer_destination() -> BufferUsage {
        BufferUsage {
            indirect_buffer: true,
            transfer_destination: true,
            ..BufferUsage::none()
        }
    }
}

impl BitOr for BufferUsage {
    type Output = Self;

    #[inline]
    fn bitor(self, rhs: Self) -> Self {
        BufferUsage {
            transfer_source: self.transfer_source || rhs.transfer_source,
            transfer_destination: self.transfer_destination || rhs.transfer_destination,
            uniform_texel_buffer: self.uniform_texel_buffer || rhs.uniform_texel_buffer,
            storage_texel_buffer: self.storage_texel_buffer || rhs.storage_texel_buffer,
            uniform_buffer: self.uniform_buffer || rhs.uniform_buffer,
            storage_buffer: self.storage_buffer || rhs.storage_buffer,
            index_buffer: self.index_buffer || rhs.index_buffer,
            vertex_buffer: self.vertex_buffer || rhs.vertex_buffer,
            indirect_buffer: self.indirect_buffer || rhs.indirect_buffer,
        }
    }
}