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
// Ports include/mln/webgpu/uniform_buffer.hpp and src/mln/webgpu/uniform_buffer.cpp
//
// `webgpu::UniformBufferArray` only overrides `bind` (a no-op: bind groups
// are built per drawable in `Drawable::draw`) and `copy`, so the port uses
// `gfx::UniformBufferArray` directly.
use std::any::Any;
use std::cell::Cell;
use std::rc::{Rc, Weak};
use mln_gfx::uniform_buffer;
use crate::context::Context;
use crate::renderer_backend::BackendImpl;
/// WebGPU's largest allowed `minUniformBufferOffsetAlignment`, so a valid
/// uniform binding offset on any device.
pub(crate) const UNIFORM_BUFFER_ALIGNMENT: usize = 256;
const fn aligned_uniform_size(size: usize) -> usize {
if size == 0 {
return 0;
}
size.div_ceil(UNIFORM_BUFFER_ALIGNMENT) * UNIFORM_BUFFER_ALIGNMENT
}
pub struct UniformBuffer {
/// The size of the data, which `get_size` reports. The GPU buffer is
/// padded to 256 bytes. C++ reports the padded size, so
/// `UniformBufferArray::createOrUpdate`, which compares it to the data
/// size, creates a new GPU buffer on every update of a UBO whose size
/// isn't a multiple of 256 bytes. Reporting the data size updates it in
/// place. Bind groups still bind the whole padded buffer, because some
/// UBOs hold less data than their shader struct.
size: usize,
/// C++ keeps `Context&`; only device and queue are used. Holding the
/// backend state rather than `Rc<Context>` avoids an Rc cycle through the
/// context's global uniform buffer array.
backend: Rc<BackendImpl>,
/// The context whose stats count this buffer. Weak for the same reason;
/// a buffer the context drops has no stats left to update.
context: Weak<Context>,
/// The GPU buffer, which other uniform buffers may share, and where in it
/// this one's padded bytes start.
buffer: Option<wgpu::Buffer>,
offset: u64,
/// The end of the bytes that may not be zeros: an update ending there or
/// later can write the padding's zeros with it.
written: Cell<usize>,
}
impl UniformBuffer {
/// `UniformBuffer(context, data, size)`; `data == None` is the C++ null
/// pointer.
pub fn new(context: &Context, data: Option<&[u8]>, size: usize) -> Self {
Self::with_backend(context.get_backend().clone(), context.weak(), data, size)
}
fn with_backend(backend: Rc<BackendImpl>, context: Weak<Context>, data: Option<&[u8]>, size: usize) -> Self {
let aligned_size = aligned_uniform_size(size);
let mut buffer = None;
let mut offset = 0;
let mut written = 0;
if let Some(device) = backend.get_device()
&& aligned_size > 0
{
// C++ creates the buffer mapped and copies the data (zero padded);
// a queue write of the data has the same effect for the next
// submission.
let (b, at) = backend.allocate_uniform_buffer(&device, aligned_size as u64);
if let Some(data) = data {
backend.write_buffer_padded(&b, at, &data[..size], aligned_size as u64);
written = size;
}
if let Some(context) = context.upgrade() {
context.count_buffer_created(aligned_size as u64);
let mut stats = context.rendering_stats_mut();
stats.num_uniform_buffers += 1;
stats.mem_uniform_buffers += aligned_size as i32;
}
buffer = Some(b);
offset = at;
}
UniformBuffer { size, backend, context, buffer, offset, written: Cell::new(written) }
}
pub fn get_buffer(&self) -> Option<&wgpu::Buffer> {
self.buffer.as_ref()
}
/// Where this uniform buffer starts in [`get_buffer`](Self::get_buffer).
pub fn get_offset(&self) -> u64 {
self.offset
}
/// The size of this uniform buffer in its GPU buffer: the data's, padded
/// to 256 bytes.
pub fn get_aligned_size(&self) -> u64 {
aligned_uniform_size(self.size) as u64
}
}
impl Drop for UniformBuffer {
fn drop(&mut self) {
if self.buffer.is_some()
&& let Some(context) = self.context.upgrade()
{
let size = self.get_aligned_size();
context.count_buffer_released(size);
let mut stats = context.rendering_stats_mut();
stats.num_uniform_buffers -= 1;
stats.mem_uniform_buffers -= size as i32;
}
}
}
impl uniform_buffer::UniformBuffer for UniformBuffer {
fn update(&self, data: &[u8]) {
debug_assert!(data.len() <= aligned_uniform_size(self.size), "update larger than the uniform buffer");
if !data.is_empty()
&& let Some(buffer) = &self.buffer
{
let written = self.written.get();
if data.len() >= written {
self.backend.write_buffer_padded(buffer, self.offset, data, self.get_aligned_size());
} else {
self.backend.write_buffer(buffer, self.offset, data);
}
self.written.set(written.max(data.len()));
if let Some(context) = self.context.upgrade() {
context.count_buffer_update(data.len());
let mut stats = context.rendering_stats_mut();
stats.num_uniform_updates += 1;
stats.uniform_update_bytes = stats.uniform_update_bytes.wrapping_add(data.len());
}
}
}
fn get_size(&self) -> usize {
self.size
}
fn as_any(&self) -> &dyn Any {
self
}
}