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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
use std::{collections::VecDeque, iter, mem};
use crate::{
Allocation, AsBufferPass, AtlasSet, GpuRenderer, GraphicsError,
InstanceBuffer, MAX_UNIFORMED_TEXT, SetBuffers, StaticVertexBuffer, Text,
TextRenderPipeline, TextUniformLayout, TextUniformRaw, TextVertex, Vec2,
instance_buffer::OrderedIndex,
};
use cosmic_text::{CacheKey, SwashCache, SwashImage};
#[cfg(feature = "logging")]
use log::{error, warn};
use wgpu::util::{DeviceExt as _, align_to};
/// [`Text`] text and Emoji AtlasSet holder.
///
pub struct TextAtlas {
/// AtlasSet holding data from Text only.
pub(crate) text: AtlasSet<Vec2>,
/// AtlasSet holding data from Colored Emoji's only.
pub(crate) emoji: AtlasSet<Vec2>,
}
impl TextAtlas {
/// Creates a new [`TextAtlas`].
///
/// # Arguments
/// - size: Used for both Width and Height. Limited to max of limits.max_texture_dimension_2d and min of 256.
/// - size: Used for both the Text Atlas and Emoji Atlas.
///
pub fn new(
renderer: &mut GpuRenderer,
size: u32,
) -> Result<Self, GraphicsError> {
Ok(Self {
text: AtlasSet::new(
renderer,
wgpu::TextureFormat::R8Unorm,
false,
size,
),
emoji: AtlasSet::new(
renderer,
wgpu::TextureFormat::Rgba8UnormSrgb,
false,
size,
),
})
}
/// Calles Trim on both internal [`AtlasSet`]'s
///
pub fn trim(&mut self) {
self.emoji.trim();
self.text.trim();
}
pub fn get_by_key(
&mut self,
key: &CacheKey,
) -> Option<(Allocation<Vec2>, bool)> {
if let Some(allocation) = self.text.get_by_key(key) {
Some((allocation, false))
} else {
self.emoji
.get_by_key(key)
.map(|allocation| (allocation, true))
}
}
pub fn upload_with_alloc(
&mut self,
renderer: &mut GpuRenderer,
is_color: bool,
key: CacheKey,
image: &SwashImage,
) -> Result<(Allocation<Vec2>, bool), GraphicsError> {
if is_color {
let (_, allocation) = self
.emoji
.upload_with_alloc(
key,
&image.data,
image.placement.width,
image.placement.height,
Vec2::new(
image.placement.left as f32,
image.placement.top as f32,
),
renderer,
)
.ok_or(GraphicsError::AtlasFull)?;
Ok((allocation, is_color))
} else {
let (_, allocation) = self
.text
.upload_with_alloc(
key,
&image.data,
image.placement.width,
image.placement.height,
Vec2::new(
image.placement.left as f32,
image.placement.top as f32,
),
renderer,
)
.ok_or(GraphicsError::AtlasFull)?;
Ok((allocation, is_color))
}
}
}
/// Instance Buffer Setup for [`Text`].
///
#[derive(Debug)]
pub struct TextRenderer {
pub buffer: InstanceBuffer<TextVertex>,
pub swash_cache: SwashCache,
/// Stores each unused buffer ID to be pulled into a map_index_buffer for the map ID.
pub unused_indexs: VecDeque<usize>,
/// Uniform buffer for the 2500 count array of [`crate::Text`]'s base shared data.
pub(crate) text_buffer: wgpu::Buffer,
/// Uniform buffer BindGroup for the 2500 count array of [`crate::Text`]'s base shared data.
text_bind_group: wgpu::BindGroup,
}
impl TextRenderer {
/// Creates a new [`TextRenderer`].
///
pub fn new(renderer: &mut GpuRenderer) -> Result<Self, GraphicsError> {
let text_alignment: usize =
align_to(mem::size_of::<TextUniformRaw>(), 16) as usize;
let text_uniforms: Vec<u8> =
iter::repeat_n(0u8, MAX_UNIFORMED_TEXT * text_alignment).collect();
let text_buffer = renderer.device().create_buffer_init(
&wgpu::util::BufferInitDescriptor {
label: Some("text uniform buffer"),
contents: &text_uniforms, //500
usage: wgpu::BufferUsages::UNIFORM
| wgpu::BufferUsages::COPY_DST,
},
);
// Create the bind group layout for the map
let layout = renderer.create_layout(TextUniformLayout);
// Create the bind group.
let text_bind_group =
renderer
.device()
.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: text_buffer.as_entire_binding(),
}],
label: Some("map_bind_group"),
});
let mut unused_indexs = VecDeque::with_capacity(MAX_UNIFORMED_TEXT);
for i in 1..MAX_UNIFORMED_TEXT {
unused_indexs.push_back(i);
}
Ok(Self {
buffer: InstanceBuffer::with_capacity(renderer.gpu_device(), 1024),
swash_cache: SwashCache::new(),
text_buffer,
text_bind_group,
unused_indexs,
})
}
/// Adds a Buffer [`OrderedIndex`] to the Rendering Store to get processed.
/// This must be done before [`TextRenderer::finalize`] but after [`Text::update`] in order for it to Render.
///
/// # Arguments
/// - index: The [`OrderedIndex`] of the Object we want to render.
/// - buffer_layer: The Buffer Layer we want to add this Object too.
///
pub fn add_buffer_store(
&mut self,
renderer: &GpuRenderer,
index: OrderedIndex,
buffer_layer: usize,
) {
self.buffer.add_buffer_store(renderer, index, buffer_layer);
}
/// Finalizes the Buffer by processing staged [`OrderedIndex`]'s and uploading it to the GPU.
/// Must be called after all the [`TextRenderer::add_buffer_store`]'s.
///
pub fn finalize(&mut self, renderer: &mut GpuRenderer) {
self.buffer.finalize(renderer);
}
/// Updates a [`Text`] and adds its [`TextOrderedIndex`] to staging using [`TextRenderer::add_buffer_store`].
/// This must be done before [`TextRenderer::finalize`] in order for it to Render.
///
/// # Arguments
/// - text: [`Text`] we want to update and prepare for rendering.
/// - atlas: [`TextAtlas`] the [`Text`] needs to render with.
/// - buffer_layer: The Buffer Layer we want to add this Object too.
///
pub fn update(
&mut self,
text: &mut Text,
atlas: &mut TextAtlas,
renderer: &mut GpuRenderer,
buffer_layer: usize,
) -> Result<(), GraphicsError> {
let index = text.update(self, atlas, renderer)?;
self.add_buffer_store(renderer, index, buffer_layer);
Ok(())
}
/// Returns a reference too [`wgpu::BindGroup`].
///
pub fn bind_group(&self) -> &wgpu::BindGroup {
&self.text_bind_group
}
}
/// Trait used to Grant Direct [`Text`] Rendering to [`wgpu::RenderPass`]
pub trait RenderText<'a, 'b>
where
'b: 'a,
{
/// Renders the all [`Text`]'s within the buffer layer to screen that have been processed and finalized.
///
fn render_text(
&mut self,
renderer: &'b GpuRenderer,
buffer: &'b TextRenderer,
atlas: &'b TextAtlas,
buffer_layer: usize,
);
}
impl<'a, 'b> RenderText<'a, 'b> for wgpu::RenderPass<'a>
where
'b: 'a,
{
fn render_text(
&mut self,
renderer: &'b GpuRenderer,
buffer: &'b TextRenderer,
atlas: &'b TextAtlas,
buffer_layer: usize,
) {
if let Some(Some(details)) = buffer.buffer.buffers.get(buffer_layer)
&& buffer.buffer.count() > 0
{
self.set_buffers(renderer.buffer_object.as_buffer_pass());
self.set_bind_group(1, atlas.text.bind_group(), &[]);
self.set_bind_group(2, atlas.emoji.bind_group(), &[]);
self.set_bind_group(3, buffer.bind_group(), &[]);
self.set_vertex_buffer(1, buffer.buffer.instances(None));
self.set_pipeline(
renderer.get_pipelines(TextRenderPipeline).unwrap(),
);
self.draw_indexed(
0..StaticVertexBuffer::index_count(),
0,
details.start..details.end,
);
}
}
}