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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use librashader_reflect::reflect::semantics::{MemberOffset, UniformMemberBlock};
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
pub trait UniformScalar: Copy + bytemuck::Pod {}
impl UniformScalar for f32 {}
impl UniformScalar for i32 {}
impl UniformScalar for u32 {}
pub trait BindUniform<C, T> {
fn bind_uniform(block: UniformMemberBlock, value: T, ctx: C) -> Option<()>;
}
pub trait UniformStorageAccess {
fn ubo_pointer(&self) -> *const u8;
fn ubo_slice(&self) -> &[u8];
fn push_pointer(&self) -> *const u8;
fn push_slice(&self) -> &[u8];
}
impl<T, H, U, P> UniformStorageAccess for UniformStorage<T, H, U, P>
where
U: Deref<Target = [u8]> + DerefMut,
P: Deref<Target = [u8]> + DerefMut,
{
fn ubo_pointer(&self) -> *const u8 {
self.ubo.as_ptr()
}
fn ubo_slice(&self) -> &[u8] {
&self.ubo
}
fn push_pointer(&self) -> *const u8 {
self.push.as_ptr()
}
fn push_slice(&self) -> &[u8] {
&self.push
}
}
pub struct NoUniformBinder;
impl<T> BindUniform<Option<()>, T> for NoUniformBinder {
fn bind_uniform(_: UniformMemberBlock, _: T, _: Option<()>) -> Option<()> {
None
}
}
pub struct UniformStorage<H = NoUniformBinder, C = Option<()>, U = Box<[u8]>, P = Box<[u8]>>
where
U: Deref<Target = [u8]> + DerefMut,
P: Deref<Target = [u8]> + DerefMut,
{
ubo: U,
push: P,
_h: PhantomData<H>,
_c: PhantomData<C>,
}
impl<H, C, U, P> UniformStorage<H, C, U, P>
where
U: Deref<Target = [u8]> + DerefMut,
P: Deref<Target = [u8]> + DerefMut,
{
pub fn inner_ubo(&self) -> &U {
&self.ubo
}
pub fn inner_push(&self) -> &P {
&self.push
}
pub(crate) fn buffer(&mut self, ty: UniformMemberBlock) -> &mut [u8] {
match ty {
UniformMemberBlock::Ubo => self.ubo.deref_mut(),
UniformMemberBlock::PushConstant => self.push.deref_mut(),
}
}
}
impl<H, C, U, P> UniformStorage<H, C, U, P>
where
C: Copy,
U: Deref<Target = [u8]> + DerefMut,
P: Deref<Target = [u8]> + DerefMut,
{
#[inline(always)]
fn write_scalar_inner<T: UniformScalar>(buffer: &mut [u8], value: T) {
let buffer = bytemuck::cast_slice_mut(buffer);
buffer[0] = value;
}
#[inline(always)]
pub fn bind_scalar<T: UniformScalar>(&mut self, offset: MemberOffset, value: T, ctx: C)
where
H: BindUniform<C, T>,
{
for ty in UniformMemberBlock::TYPES {
if H::bind_uniform(ty, value, ctx).is_some() {
continue;
}
if let Some(offset) = offset.offset(ty) {
let buffer = self.buffer(ty);
Self::write_scalar_inner(&mut buffer[offset..][..std::mem::size_of::<T>()], value)
}
}
}
pub fn new_with_storage(ubo: U, push: P) -> UniformStorage<H, C, U, P> {
UniformStorage {
ubo,
push,
_h: Default::default(),
_c: Default::default(),
}
}
}
impl<H, C, U> UniformStorage<H, C, U, Box<[u8]>>
where
C: Copy,
U: Deref<Target = [u8]> + DerefMut,
{
pub fn new_with_ubo_storage(
storage: U,
push_size: usize,
) -> UniformStorage<H, C, U, Box<[u8]>> {
UniformStorage {
ubo: storage,
push: vec![0u8; push_size].into_boxed_slice(),
_h: Default::default(),
_c: Default::default(),
}
}
}
impl<H, C> UniformStorage<H, C, Box<[u8]>, Box<[u8]>> {
pub fn new(ubo_size: usize, push_size: usize) -> UniformStorage<H, C, Box<[u8]>> {
UniformStorage {
ubo: vec![0u8; ubo_size].into_boxed_slice(),
push: vec![0u8; push_size].into_boxed_slice(),
_h: Default::default(),
_c: Default::default(),
}
}
}
impl<H, C, U, P> UniformStorage<H, C, U, P>
where
C: Copy,
U: Deref<Target = [u8]> + DerefMut,
P: Deref<Target = [u8]> + DerefMut,
H: for<'a> BindUniform<C, &'a [f32; 4]>,
{
#[inline(always)]
fn write_vec4_inner(buffer: &mut [u8], vec4: &[f32; 4]) {
let vec4 = bytemuck::cast_slice(vec4);
buffer.copy_from_slice(vec4);
}
#[inline(always)]
pub fn bind_vec4(&mut self, offset: MemberOffset, value: impl Into<[f32; 4]>, ctx: C) {
let vec4 = value.into();
for ty in UniformMemberBlock::TYPES {
if H::bind_uniform(ty, &vec4, ctx).is_some() {
continue;
}
if let Some(offset) = offset.offset(ty) {
let buffer = self.buffer(ty);
Self::write_vec4_inner(
&mut buffer[offset..][..4 * std::mem::size_of::<f32>()],
&vec4,
);
}
}
}
}
impl<H, C, U, P> UniformStorage<H, C, U, P>
where
C: Copy,
U: Deref<Target = [u8]> + DerefMut,
P: Deref<Target = [u8]> + DerefMut,
H: for<'a> BindUniform<C, &'a [f32; 16]>,
{
#[inline(always)]
fn write_mat4_inner(buffer: &mut [u8], mat4: &[f32; 16]) {
let mat4 = bytemuck::cast_slice(mat4);
buffer.copy_from_slice(mat4);
}
#[inline(always)]
pub fn bind_mat4(&mut self, offset: MemberOffset, value: &[f32; 16], ctx: C) {
for ty in UniformMemberBlock::TYPES {
if H::bind_uniform(ty, value, ctx).is_some() {
continue;
}
if let Some(offset) = offset.offset(ty) {
let buffer = self.buffer(ty);
Self::write_mat4_inner(
&mut buffer[offset..][..16 * std::mem::size_of::<f32>()],
value,
);
}
}
}
}