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
|
use std::any::TypeId;
use opengl_bindings::CurrentContextWithFns as GlCurrentContextWithFns;
use opengl_bindings::buffer::{Buffer as GlBuffer, Usage as GlBufferUsage};
use opengl_bindings::vertex_array::{
AttributeFormat as GlVertexArrayAttributeFormat,
BindVertexBufferError as GlVertexArrayBindVertexBufferError,
DataType as GlVertexArrayDataType,
VertexArray as GlVertexArray,
VertexBufferSpec as GlVertexArrayVertexBufferSpec,
};
use zerocopy::IntoBytes;
use crate::mesh::Mesh;
use crate::reflection::Reflection;
use crate::shader::VertexSubset as ShaderVertexSubset;
#[derive(Debug)]
pub struct GraphicsMesh
{
/// Vertex and index buffer has to live as long as the vertex array
vertex_buffer: GlBuffer<u8>,
pub index_buffer: Option<GlBuffer<u32>>,
pub element_cnt: u32,
pub vertex_arr: GlVertexArray,
}
impl GraphicsMesh
{
#[tracing::instrument(skip_all)]
pub fn new(
current_context: &GlCurrentContextWithFns<'_>,
mesh: &Mesh,
vertex_subset: &ShaderVertexSubset,
) -> Result<Self, Error>
{
let vertex_arr = GlVertexArray::new(current_context);
let vertex_buffer = GlBuffer::new(current_context);
vertex_buffer
.init(
current_context,
vertex_subset.layout.size() * mesh.vertices().len(),
GlBufferUsage::Static,
)
.map_err(Error::InitVertexBufferFailed)?;
for (vertex_index, vertex) in mesh.vertices().iter().enumerate() {
let vertex_bytes = vertex.as_bytes();
for vertex_subset_field in vertex_subset.fields.iter().flatten() {
let vertex_buffer_offset = (vertex_subset.layout.size() * vertex_index)
+ vertex_subset_field.offset;
let vertex_field_start_offset =
vertex_subset_field.reflection.byte_offset;
let vertex_field_size = vertex_subset_field.reflection.layout.size();
vertex_buffer
.store_at_byte_offset(
current_context,
vertex_buffer_offset,
&vertex_bytes[vertex_field_start_offset
..vertex_field_start_offset + vertex_field_size],
)
.map_err(Error::StoreVerticesFailed)?;
}
}
let vertex_buf_binding_index = 0;
if let Err(err) = vertex_arr.bind_vertex_buffer(
current_context,
vertex_buf_binding_index,
&vertex_buffer,
GlVertexArrayVertexBufferSpec {
offset: 0,
vertex_size: vertex_subset.layout.size(),
},
) {
match err {
GlVertexArrayBindVertexBufferError::OffsetValueTooLarge {
value: _,
max_value: _,
} => unreachable!(),
GlVertexArrayBindVertexBufferError::VertexSizeValueTooLarge {
value,
max_value,
} => {
panic!(
"Size of vertex ({}) is too large. Must be less than {max_value}",
value
);
}
}
}
for vertex_subset_field in vertex_subset.fields.iter().flatten() {
let attrib_index: u32 =
vertex_subset_field.varying_input_offset.try_into().unwrap();
vertex_arr.enable_attrib(current_context, attrib_index);
vertex_arr.set_attrib_format(
current_context,
attrib_index,
match vertex_subset_field.reflection.reflection {
Reflection::Literal(_) => {
if vertex_subset_field.reflection.type_id != TypeId::of::<f32>() {
panic!("Unsupported vertex field data type");
}
GlVertexArrayAttributeFormat {
data_type: GlVertexArrayDataType::Float,
count: 1,
normalized: false,
offset: vertex_subset_field.offset.try_into().unwrap(),
}
}
Reflection::Array(array_vertex_field) => {
let Reflection::Literal(array_vertex_field_item) =
array_vertex_field.item_reflection
else {
panic!("Unsupported array item type in vertex field");
};
if array_vertex_field_item.type_id != TypeId::of::<f32>() {
panic!("Unsupported array item type in vertex field");
}
GlVertexArrayAttributeFormat {
data_type: GlVertexArrayDataType::Float,
count: array_vertex_field.length.try_into().unwrap(),
normalized: false,
offset: vertex_subset_field.offset.try_into().unwrap(),
}
}
_ => panic!("Unsupported vertex field data type"),
},
);
vertex_arr.set_attrib_vertex_buf_binding(
current_context,
attrib_index,
vertex_buf_binding_index,
);
}
if let Some(indices) = mesh.indices() {
let index_buffer = GlBuffer::new(current_context);
index_buffer
.store(current_context, indices, GlBufferUsage::Static)
.map_err(Error::StoreIndicesFailed)?;
vertex_arr.bind_element_buffer(current_context, &index_buffer);
return Ok(Self {
vertex_buffer: vertex_buffer,
index_buffer: Some(index_buffer),
element_cnt: indices
.len()
.try_into()
.expect("Mesh index count does not fit into a 32-bit unsigned int"),
vertex_arr,
});
}
Ok(Self {
vertex_buffer: vertex_buffer,
index_buffer: None,
element_cnt: mesh
.vertices()
.len()
.try_into()
.expect("Mesh vertex count does not fit into a 32-bit unsigned int"),
vertex_arr,
})
}
pub fn destroy(&mut self, curr_gl_ctx: &GlCurrentContextWithFns<'_>)
{
self.vertex_arr.delete(curr_gl_ctx);
self.vertex_buffer.delete(curr_gl_ctx);
if let Some(index_buffer) = &self.index_buffer {
index_buffer.delete(curr_gl_ctx);
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum Error
{
#[error("Failed to initialize vertex buffer")]
InitVertexBufferFailed(#[source] opengl_bindings::buffer::Error),
#[error("Failed to store vertices in vertex buffer")]
StoreVerticesFailed(#[source] opengl_bindings::buffer::Error),
#[error("Failed to store indices in index buffer")]
StoreIndicesFailed(#[source] opengl_bindings::buffer::Error),
}
|