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
|
/*
wearechat - generic multiplayer game with voip
Copyright (C) 2025 metamuffin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use super::{meshops::{generate_normals, generate_tangents, generate_texcoords}, ScenePreparer};
use anyhow::Result;
use log::debug;
use std::{sync::Arc, time::Instant};
use wgpu::{
BufferUsages,
util::{BufferInitDescriptor, DeviceExt},
};
impl ScenePreparer {
pub fn update_vertex_buffers(&self, num_done: &mut usize) -> Result<()> {
for pres in self.index_buffers.needed() {
let start = Instant::now();
if let Some(buf) = self.downloader.try_get(pres.clone())? {
let buffer = self.device.create_buffer_init(&BufferInitDescriptor {
label: Some("index"),
contents: bytemuck::cast_slice(buf.as_slice()),
usage: BufferUsages::INDEX | BufferUsages::COPY_DST,
});
self.index_buffers.insert(
pres.clone(),
(Arc::new(buffer), (buf.len() * 3) as u32),
buf.len() * size_of::<u32>() * 3,
);
debug!(
"index buffer created (len={}, took {:?}) {pres}",
buf.len() / size_of::<u32>(),
start.elapsed(),
);
*num_done += 1;
}
}
for pres in self.vertex_buffers.needed() {
let start = Instant::now();
if let Some(buf) = self.downloader.try_get(pres.clone())? {
let buffer = self.device.create_buffer_init(&BufferInitDescriptor {
contents: bytemuck::cast_slice(buf.as_slice()),
label: Some("vertex attribute"),
usage: BufferUsages::VERTEX | BufferUsages::COPY_DST,
});
self.vertex_buffers.insert(
pres.clone(),
Arc::new(buffer),
buf.len() * size_of::<f32>(),
);
debug!(
"vertex attribute buffer created (len={}, took {:?}) {pres}",
buf.len() / size_of::<f32>(),
start.elapsed()
);
*num_done += 1;
}
}
for spec in self.generated_tangent_buffers.needed() {
if let (Some(index), Some(position), texcoord) = (
self.downloader.try_get(spec.index.clone())?,
self.downloader.try_get(spec.position.clone())?,
spec.texcoord
.clone()
.map(|r| self.downloader.try_get(r))
.transpose()?,
) {
let texcoord = match texcoord {
Some(Some(x)) => Some(x),
Some(None) => continue, // tangents provided but still loading
None => None,
};
let texcoord = texcoord.unwrap_or_else(|| generate_texcoords(&index, &position));
let tangents = generate_tangents(&index, &position, &texcoord);
let buffer = self.device.create_buffer_init(&BufferInitDescriptor {
label: Some("generated tangent"),
usage: BufferUsages::COPY_DST | BufferUsages::VERTEX,
contents: bytemuck::cast_slice(tangents.as_slice()),
});
self.generated_tangent_buffers.insert(
spec,
Arc::new(buffer),
size_of::<f32>() * tangents.len() * 3,
);
}
}
for spec in self.generated_normal_buffers.needed() {
if let (Some(index), Some(position)) = (
self.downloader.try_get(spec.index.clone())?,
self.downloader.try_get(spec.position.clone())?,
) {
let normals = generate_normals(&index, &position);
let buffer = self.device.create_buffer_init(&BufferInitDescriptor {
label: Some("generated normal"),
usage: BufferUsages::COPY_DST | BufferUsages::VERTEX,
contents: bytemuck::cast_slice(normals.as_slice()),
});
self.generated_normal_buffers.insert(
spec,
Arc::new(buffer),
size_of::<f32>() * normals.len() * 3,
);
}
}
for spec in self.generated_texcoord_buffers.needed() {
if let (Some(index), Some(position)) = (
self.downloader.try_get(spec.index.clone())?,
self.downloader.try_get(spec.position.clone())?,
) {
let texcoords = generate_texcoords(&index, &position);
let buffer = self.device.create_buffer_init(&BufferInitDescriptor {
label: Some("generated texcoord"),
usage: BufferUsages::COPY_DST | BufferUsages::VERTEX,
contents: bytemuck::cast_slice(texcoords.as_slice()),
});
self.generated_texcoord_buffers.insert(
spec,
Arc::new(buffer),
size_of::<f32>() * texcoords.len() * 3,
);
}
}
Ok(())
}
}
|