simple_wgpu/
buffer.rs

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
use std::{
    hash::Hash,
    num::NonZeroU64,
    ops::{Bound, Range, RangeBounds},
    sync::Arc,
};

use uuid::Uuid;
use wgpu::util::DeviceExt;

use crate::context::Context;

#[derive(Debug)]
struct BufferInternal {
    buffer: wgpu::Buffer,
    size: usize,
    usage: wgpu::BufferUsages,
}

/// A handle to a GPU buffer
///
/// The equivalent to [wgpu::Buffer]
#[derive(Clone, Debug)]
pub struct Buffer {
    id: Uuid,
    data: Arc<BufferInternal>,
}

/// How to bind a [Buffer] to a [BindGroup](crate::BindGroup)
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct BufferBinding {
    pub(crate) buffer: Buffer,
    pub(crate) binding_type: wgpu::BufferBindingType,
    pub(crate) has_dynamic_offset: bool,
    pub(crate) min_binding_size: Option<NonZeroU64>,
}

impl Buffer {
    /// Create an empty buffer
    pub fn new(
        label: wgpu::Label,
        usage: wgpu::BufferUsages,
        size: usize,
        context: &Context,
    ) -> Self {
        let buffer = context.device().create_buffer(&wgpu::BufferDescriptor {
            label,
            usage,
            size: size as u64,
            mapped_at_creation: false,
        });

        Self {
            id: Uuid::new_v4(),
            data: Arc::new(BufferInternal {
                buffer,
                size,
                usage,
            }),
        }
    }

    /// Create a buffer and immediately upload data to it
    pub fn with_data(
        label: wgpu::Label,
        usage: wgpu::BufferUsages,
        data: &[u8],
        context: &Context,
    ) -> Self {
        let buffer = context
            .device()
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label,
                usage,
                contents: data,
            });

        Self {
            id: Uuid::new_v4(),
            data: Arc::new(BufferInternal {
                buffer,
                size: data.len(),
                usage,
            }),
        }
    }

    /// Grow the buffer to `new_size`. Does nothing if the buffer is already larger than `new_size`
    pub fn ensure_capacity(&mut self, new_size: usize, context: &Context) {
        if new_size > self.data.size {
            Arc::get_mut(&mut self.data)
                .map(|data| {
                    data.size = new_size;
                    data.buffer = context.device().create_buffer(&wgpu::BufferDescriptor {
                        label: None,
                        usage: data.usage,
                        size: new_size as u64,
                        mapped_at_creation: false,
                    });
                })
                .expect("couldn't get exclusive access to resize buffer");
        }
    }

    /// Write data to the buffer
    pub fn write(&self, data: &[u8], context: &Context) {
        context.queue().write_buffer(&self.data.buffer, 0, data);
    }

    pub(crate) fn buffer(&self) -> &wgpu::Buffer {
        &self.data.buffer
    }

    /// Obtain a (sub) slice of the buffer
    pub fn slice<R>(&self, bounds: R) -> BufferSlice
    where
        R: RangeBounds<wgpu::BufferAddress>,
    {
        BufferSlice {
            data: self.data.clone(),
            bounds: constrain_range_to_container_len(bounds, self.data.size as u64),
        }
    }

    /// Bind this buffer as a uniform buffer. Must be passed to a [BindGroup](crate::BindGroup)
    #[must_use]
    pub fn uniform_binding(&self) -> BufferBinding {
        BufferBinding {
            buffer: self.clone(),
            binding_type: wgpu::BufferBindingType::Uniform,
            has_dynamic_offset: false,
            min_binding_size: None,
        }
    }

    /// Bind this buffer as a storage buffer. Must be passed to a [BindGroup](crate::BindGroup)
    #[must_use]
    pub fn storage_binding(&self, read_only: bool) -> BufferBinding {
        BufferBinding {
            buffer: self.clone(),
            binding_type: wgpu::BufferBindingType::Storage { read_only },
            has_dynamic_offset: false,
            min_binding_size: None,
        }
    }

    /// See wgpu's [Buffer::unmap](wgpu::Buffer::unmap)
    pub fn unmap(&self) {
        self.data.buffer.unmap();
    }
}

impl BufferBinding {
    pub fn dynamic_offset(mut self, min_binding_size: u64) -> Self {
        self.has_dynamic_offset = true;
        self.min_binding_size = NonZeroU64::new(min_binding_size);
        self
    }
}

impl Hash for Buffer {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.id.hash(state);
    }
}

impl PartialEq for Buffer {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for Buffer {}

/// A sub-slice of a [Buffer](Buffer)
#[derive(Debug)]
pub struct BufferSlice {
    data: Arc<BufferInternal>,
    bounds: Range<wgpu::BufferAddress>,
}

// todo: figure out how to deal with mapping sanely here
impl BufferSlice {
    /// Get the underlying wgpu [Buffer](wgpu::Buffer). You'll need this to map the contents of the buffer
    pub fn get(&self) -> wgpu::BufferSlice {
        self.data.buffer.slice(self.bounds.clone())
    }
}

fn constrain_range_to_container_len<R>(range: R, container_len: u64) -> Range<u64>
where
    R: RangeBounds<u64>,
{
    let start = match range.start_bound() {
        Bound::Included(t) => *t,
        Bound::Excluded(t) => *t + 1,
        Bound::Unbounded => 0,
    };

    let end = match range.end_bound() {
        Bound::Included(t) => *t + 1,
        Bound::Excluded(t) => *t,
        Bound::Unbounded => container_len,
    };

    start..end
}