three_d/core/buffer/vertex_buffer.rs
1use super::Buffer;
2use crate::core::*;
3
4///
5/// A buffer containing per vertex data, for example positions, normals, uv coordinates or colors.
6/// To send this data to a shader, use the [Program::use_vertex_attribute] method.
7///
8pub struct VertexBuffer<T: BufferDataType> {
9 buffer: Buffer<T>,
10}
11
12impl<T: BufferDataType> VertexBuffer<T> {
13 ///
14 /// Creates a new empty vertex buffer.
15 ///
16 pub fn new(context: &Context) -> Self {
17 Self {
18 buffer: Buffer::new(context),
19 }
20 }
21
22 ///
23 /// Creates a new vertex buffer and fills it with the given data. The data should be in the same format as specified in the shader.
24 /// As an example, if specified as `vec3` in the shader it needs to be specified as an array of `Vector3<T>` where `T` is a primitive type that implements [BufferDataType], for example can be f16 or f32.
25 ///
26 pub fn new_with_data(context: &Context, data: &[T]) -> Self {
27 Self {
28 buffer: Buffer::new_with_data(context, data),
29 }
30 }
31
32 ///
33 /// Fills the vertex buffer with the given data. The data should be in the same format as specified in the shader.
34 /// As an example, if specified as `vec3` in the shader it needs to be specified as an array of `Vector3<T>` where `T` is a primitive type that implements [BufferDataType], for example can be f16 or f32.
35 /// This function will resize the buffer to have the same size as the data, if that is not desired, use [fill_subset](Self::fill_subset) instead.
36 ///
37 pub fn fill(&mut self, data: &[T]) {
38 self.buffer.fill(data);
39 }
40
41 ///
42 /// Fills the vertex buffer with the given data starting at the given offset.
43 /// This will increase the size of the buffer if there's not enough room. Otherwise, the size will remain unchanged.
44 ///
45 pub fn fill_subset(&mut self, offset: u32, data: &[T]) {
46 self.buffer.fill_subset(offset, data);
47 }
48
49 ///
50 /// The number of values in the buffer.
51 ///
52 pub fn count(&self) -> u32 {
53 self.buffer.attribute_count() * T::size()
54 }
55
56 ///
57 /// The number of vertex attributes in the buffer.
58 ///
59 pub fn vertex_count(&self) -> u32 {
60 self.buffer.attribute_count()
61 }
62
63 pub(in crate::core) fn bind(&self) {
64 self.buffer.bind();
65 }
66}