-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
mod.rs
609 lines (567 loc) · 20.7 KB
/
mod.rs
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
use std::{cmp::Ordering, ops::Range};
use crate::{
texture_atlas::{TextureAtlas, TextureAtlasSprite},
Rect, Sprite, SPRITE_SHADER_HANDLE,
};
use bevy_asset::{AssetEvent, Assets, Handle};
use bevy_core::FloatOrd;
use bevy_core_pipeline::Transparent2d;
use bevy_ecs::{
prelude::*,
system::{lifetimeless::*, SystemState},
};
use bevy_math::{const_vec3, Mat4, Vec2, Vec3, Vec4Swizzles};
use bevy_render::{
color::Color,
render_asset::RenderAssets,
render_phase::{Draw, DrawFunctions, RenderPhase, TrackedRenderPass},
render_resource::*,
renderer::{RenderDevice, RenderQueue},
texture::{BevyDefault, Image},
view::{ComputedVisibility, ViewUniform, ViewUniformOffset, ViewUniforms},
RenderWorld,
};
use bevy_transform::components::GlobalTransform;
use bevy_utils::HashMap;
use bytemuck::{Pod, Zeroable};
use crevice::std140::AsStd140;
pub struct SpritePipeline {
view_layout: BindGroupLayout,
material_layout: BindGroupLayout,
}
impl FromWorld for SpritePipeline {
fn from_world(world: &mut World) -> Self {
let world = world.cell();
let render_device = world.get_resource::<RenderDevice>().unwrap();
let view_layout = render_device.create_bind_group_layout(&BindGroupLayoutDescriptor {
entries: &[BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::VERTEX | ShaderStages::FRAGMENT,
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: true,
min_binding_size: BufferSize::new(ViewUniform::std140_size_static() as u64),
},
count: None,
}],
label: Some("sprite_view_layout"),
});
let material_layout = render_device.create_bind_group_layout(&BindGroupLayoutDescriptor {
entries: &[
BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::FRAGMENT,
ty: BindingType::Texture {
multisampled: false,
sample_type: TextureSampleType::Float { filterable: true },
view_dimension: TextureViewDimension::D2,
},
count: None,
},
BindGroupLayoutEntry {
binding: 1,
visibility: ShaderStages::FRAGMENT,
ty: BindingType::Sampler {
comparison: false,
filtering: true,
},
count: None,
},
],
label: Some("sprite_material_layout"),
});
SpritePipeline {
view_layout,
material_layout,
}
}
}
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
pub struct SpritePipelineKey {
colored: bool,
}
impl SpecializedPipeline for SpritePipeline {
type Key = SpritePipelineKey;
fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor {
let mut vertex_buffer_layout = VertexBufferLayout {
array_stride: 20,
step_mode: VertexStepMode::Vertex,
attributes: vec![
VertexAttribute {
format: VertexFormat::Float32x3,
offset: 0,
shader_location: 0,
},
VertexAttribute {
format: VertexFormat::Float32x2,
offset: 12,
shader_location: 1,
},
],
};
let mut shader_defs = Vec::new();
if key.colored {
shader_defs.push("COLORED".to_string());
vertex_buffer_layout.attributes.push(VertexAttribute {
format: VertexFormat::Uint32,
offset: 20,
shader_location: 2,
});
vertex_buffer_layout.array_stride += 4;
}
RenderPipelineDescriptor {
vertex: VertexState {
shader: SPRITE_SHADER_HANDLE.typed::<Shader>(),
entry_point: "vertex".into(),
shader_defs: shader_defs.clone(),
buffers: vec![vertex_buffer_layout],
},
fragment: Some(FragmentState {
shader: SPRITE_SHADER_HANDLE.typed::<Shader>(),
shader_defs,
entry_point: "fragment".into(),
targets: vec![ColorTargetState {
format: TextureFormat::bevy_default(),
blend: Some(BlendState::ALPHA_BLENDING),
write_mask: ColorWrites::ALL,
}],
}),
layout: Some(vec![self.view_layout.clone(), self.material_layout.clone()]),
primitive: PrimitiveState {
front_face: FrontFace::Ccw,
cull_mode: None,
polygon_mode: PolygonMode::Fill,
clamp_depth: false,
conservative: false,
topology: PrimitiveTopology::TriangleList,
strip_index_format: None,
},
depth_stencil: None,
multisample: MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
label: Some("sprite_pipeline".into()),
}
}
}
pub struct ExtractedSprite {
pub transform: Mat4,
pub color: Color,
pub rect: Rect,
pub handle: Handle<Image>,
pub atlas_size: Option<Vec2>,
pub flip_x: bool,
pub flip_y: bool,
}
#[derive(Default)]
pub struct ExtractedSprites {
pub sprites: Vec<ExtractedSprite>,
}
#[derive(Default)]
pub struct SpriteAssetEvents {
pub images: Vec<AssetEvent<Image>>,
}
pub fn extract_sprite_events(
mut render_world: ResMut<RenderWorld>,
mut image_events: EventReader<AssetEvent<Image>>,
) {
let mut events = render_world
.get_resource_mut::<SpriteAssetEvents>()
.unwrap();
let SpriteAssetEvents { ref mut images } = *events;
images.clear();
for image in image_events.iter() {
// AssetEvent: !Clone
images.push(match image {
AssetEvent::Created { handle } => AssetEvent::Created {
handle: handle.clone_weak(),
},
AssetEvent::Modified { handle } => AssetEvent::Modified {
handle: handle.clone_weak(),
},
AssetEvent::Removed { handle } => AssetEvent::Removed {
handle: handle.clone_weak(),
},
});
}
}
pub fn extract_sprites(
mut render_world: ResMut<RenderWorld>,
images: Res<Assets<Image>>,
texture_atlases: Res<Assets<TextureAtlas>>,
sprite_query: Query<(
&ComputedVisibility,
&Sprite,
&GlobalTransform,
&Handle<Image>,
)>,
atlas_query: Query<(
&ComputedVisibility,
&TextureAtlasSprite,
&GlobalTransform,
&Handle<TextureAtlas>,
)>,
) {
let mut extracted_sprites = render_world.get_resource_mut::<ExtractedSprites>().unwrap();
extracted_sprites.sprites.clear();
for (computed_visibility, sprite, transform, handle) in sprite_query.iter() {
if !computed_visibility.is_visible {
continue;
}
if let Some(image) = images.get(handle) {
let size = image.texture_descriptor.size;
extracted_sprites.sprites.push(ExtractedSprite {
atlas_size: None,
color: sprite.color,
transform: transform.compute_matrix(),
rect: Rect {
min: Vec2::ZERO,
max: sprite
.custom_size
.unwrap_or_else(|| Vec2::new(size.width as f32, size.height as f32)),
},
flip_x: sprite.flip_x,
flip_y: sprite.flip_y,
handle: handle.clone_weak(),
});
};
}
for (computed_visibility, atlas_sprite, transform, texture_atlas_handle) in atlas_query.iter() {
if !computed_visibility.is_visible {
continue;
}
if let Some(texture_atlas) = texture_atlases.get(texture_atlas_handle) {
if images.contains(&texture_atlas.texture) {
let rect = texture_atlas.textures[atlas_sprite.index as usize];
extracted_sprites.sprites.push(ExtractedSprite {
atlas_size: Some(texture_atlas.size),
color: atlas_sprite.color,
transform: transform.compute_matrix(),
rect,
flip_x: atlas_sprite.flip_x,
flip_y: atlas_sprite.flip_y,
handle: texture_atlas.texture.clone_weak(),
});
}
}
}
}
#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable)]
struct SpriteVertex {
pub position: [f32; 3],
pub uv: [f32; 2],
}
#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable)]
struct ColoredSpriteVertex {
pub position: [f32; 3],
pub uv: [f32; 2],
pub color: u32,
}
pub struct SpriteMeta {
vertices: BufferVec<SpriteVertex>,
colored_vertices: BufferVec<ColoredSpriteVertex>,
view_bind_group: Option<BindGroup>,
}
impl Default for SpriteMeta {
fn default() -> Self {
Self {
vertices: BufferVec::new(BufferUsages::VERTEX),
colored_vertices: BufferVec::new(BufferUsages::VERTEX),
view_bind_group: None,
}
}
}
const QUAD_VERTEX_POSITIONS: &[Vec3] = &[
const_vec3!([-0.5, -0.5, 0.0]),
const_vec3!([0.5, 0.5, 0.0]),
const_vec3!([-0.5, 0.5, 0.0]),
const_vec3!([-0.5, -0.5, 0.0]),
const_vec3!([0.5, -0.5, 0.0]),
const_vec3!([0.5, 0.5, 0.0]),
];
#[derive(Component)]
pub struct SpriteBatch {
range: Range<u32>,
handle: Handle<Image>,
z: f32,
colored: bool,
}
pub fn prepare_sprites(
mut commands: Commands,
render_device: Res<RenderDevice>,
render_queue: Res<RenderQueue>,
mut sprite_meta: ResMut<SpriteMeta>,
mut extracted_sprites: ResMut<ExtractedSprites>,
) {
sprite_meta.vertices.clear();
sprite_meta.colored_vertices.clear();
// sort first by z and then by handle. this ensures that, when possible, batches span multiple z layers
// batches won't span z-layers if there is another batch between them
extracted_sprites.sprites.sort_by(|a, b| {
match FloatOrd(a.transform.w_axis[2]).cmp(&FloatOrd(b.transform.w_axis[2])) {
Ordering::Equal => a.handle.cmp(&b.handle),
other => other,
}
});
let mut start = 0;
let mut end = 0;
let mut colored_start = 0;
let mut colored_end = 0;
let mut current_batch_handle: Option<Handle<Image>> = None;
let mut current_batch_colored = false;
let mut last_z = 0.0;
for extracted_sprite in extracted_sprites.sprites.iter() {
let colored = extracted_sprite.color != Color::WHITE;
if let Some(current_batch_handle) = ¤t_batch_handle {
if *current_batch_handle != extracted_sprite.handle || current_batch_colored != colored
{
if current_batch_colored {
commands.spawn_bundle((SpriteBatch {
range: colored_start..colored_end,
handle: current_batch_handle.clone_weak(),
z: last_z,
colored: true,
},));
colored_start = colored_end;
} else {
commands.spawn_bundle((SpriteBatch {
range: start..end,
handle: current_batch_handle.clone_weak(),
z: last_z,
colored: false,
},));
start = end;
}
}
}
current_batch_handle = Some(extracted_sprite.handle.clone_weak());
current_batch_colored = colored;
let sprite_rect = extracted_sprite.rect;
// Specify the corners of the sprite
let mut bottom_left = Vec2::new(sprite_rect.min.x, sprite_rect.max.y);
let mut top_left = sprite_rect.min;
let mut top_right = Vec2::new(sprite_rect.max.x, sprite_rect.min.y);
let mut bottom_right = sprite_rect.max;
if extracted_sprite.flip_x {
bottom_left.x = sprite_rect.max.x;
top_left.x = sprite_rect.max.x;
bottom_right.x = sprite_rect.min.x;
top_right.x = sprite_rect.min.x;
}
if extracted_sprite.flip_y {
bottom_left.y = sprite_rect.min.y;
bottom_right.y = sprite_rect.min.y;
top_left.y = sprite_rect.max.y;
top_right.y = sprite_rect.max.y;
}
let atlas_extent = extracted_sprite.atlas_size.unwrap_or(sprite_rect.max);
bottom_left /= atlas_extent;
bottom_right /= atlas_extent;
top_left /= atlas_extent;
top_right /= atlas_extent;
let uvs: [[f32; 2]; 6] = [
bottom_left.into(),
top_right.into(),
top_left.into(),
bottom_left.into(),
bottom_right.into(),
top_right.into(),
];
let rect_size = extracted_sprite.rect.size().extend(1.0);
if current_batch_colored {
let color = extracted_sprite.color.as_linear_rgba_f32();
// encode color as a single u32 to save space
let color = (color[0] * 255.0) as u32
| ((color[1] * 255.0) as u32) << 8
| ((color[2] * 255.0) as u32) << 16
| ((color[3] * 255.0) as u32) << 24;
for (index, vertex_position) in QUAD_VERTEX_POSITIONS.iter().enumerate() {
let mut final_position = *vertex_position * rect_size;
final_position = (extracted_sprite.transform * final_position.extend(1.0)).xyz();
sprite_meta.colored_vertices.push(ColoredSpriteVertex {
position: final_position.into(),
uv: uvs[index],
color,
});
}
} else {
for (index, vertex_position) in QUAD_VERTEX_POSITIONS.iter().enumerate() {
let mut final_position = *vertex_position * rect_size;
final_position = (extracted_sprite.transform * final_position.extend(1.0)).xyz();
sprite_meta.vertices.push(SpriteVertex {
position: final_position.into(),
uv: uvs[index],
});
}
}
last_z = extracted_sprite.transform.w_axis[2];
if current_batch_colored {
colored_end += QUAD_VERTEX_POSITIONS.len() as u32;
} else {
end += QUAD_VERTEX_POSITIONS.len() as u32;
}
}
// if start != end, there is one last batch to process
if start != end {
if let Some(current_batch_handle) = current_batch_handle {
commands.spawn_bundle((SpriteBatch {
range: start..end,
handle: current_batch_handle,
colored: false,
z: last_z,
},));
}
} else if colored_start != colored_end {
if let Some(current_batch_handle) = current_batch_handle {
commands.spawn_bundle((SpriteBatch {
range: colored_start..colored_end,
handle: current_batch_handle,
colored: true,
z: last_z,
},));
}
}
sprite_meta
.vertices
.write_buffer(&render_device, &render_queue);
sprite_meta
.colored_vertices
.write_buffer(&render_device, &render_queue);
}
#[derive(Default)]
pub struct ImageBindGroups {
values: HashMap<Handle<Image>, BindGroup>,
}
#[allow(clippy::too_many_arguments)]
pub fn queue_sprites(
draw_functions: Res<DrawFunctions<Transparent2d>>,
render_device: Res<RenderDevice>,
mut sprite_meta: ResMut<SpriteMeta>,
view_uniforms: Res<ViewUniforms>,
sprite_pipeline: Res<SpritePipeline>,
mut pipelines: ResMut<SpecializedPipelines<SpritePipeline>>,
mut pipeline_cache: ResMut<RenderPipelineCache>,
mut image_bind_groups: ResMut<ImageBindGroups>,
gpu_images: Res<RenderAssets<Image>>,
mut sprite_batches: Query<(Entity, &SpriteBatch)>,
mut views: Query<&mut RenderPhase<Transparent2d>>,
events: Res<SpriteAssetEvents>,
) {
// If an image has changed, the GpuImage has (probably) changed
for event in &events.images {
match event {
AssetEvent::Created { .. } => None,
AssetEvent::Modified { handle } => image_bind_groups.values.remove(handle),
AssetEvent::Removed { handle } => image_bind_groups.values.remove(handle),
};
}
if let Some(view_binding) = view_uniforms.uniforms.binding() {
sprite_meta.view_bind_group = Some(render_device.create_bind_group(&BindGroupDescriptor {
entries: &[BindGroupEntry {
binding: 0,
resource: view_binding,
}],
label: Some("sprite_view_bind_group"),
layout: &sprite_pipeline.view_layout,
}));
let draw_sprite_function = draw_functions.read().get_id::<DrawSprite>().unwrap();
let pipeline = pipelines.specialize(
&mut pipeline_cache,
&sprite_pipeline,
SpritePipelineKey { colored: false },
);
let colored_pipeline = pipelines.specialize(
&mut pipeline_cache,
&sprite_pipeline,
SpritePipelineKey { colored: true },
);
for mut transparent_phase in views.iter_mut() {
for (entity, batch) in sprite_batches.iter_mut() {
image_bind_groups
.values
.entry(batch.handle.clone_weak())
.or_insert_with(|| {
let gpu_image = gpu_images.get(&batch.handle).unwrap();
render_device.create_bind_group(&BindGroupDescriptor {
entries: &[
BindGroupEntry {
binding: 0,
resource: BindingResource::TextureView(&gpu_image.texture_view),
},
BindGroupEntry {
binding: 1,
resource: BindingResource::Sampler(&gpu_image.sampler),
},
],
label: Some("sprite_material_bind_group"),
layout: &sprite_pipeline.material_layout,
})
});
transparent_phase.add(Transparent2d {
draw_function: draw_sprite_function,
pipeline: if batch.colored {
colored_pipeline
} else {
pipeline
},
entity,
sort_key: FloatOrd(batch.z),
});
}
}
}
}
pub struct DrawSprite {
params: SystemState<(
SRes<SpriteMeta>,
SRes<ImageBindGroups>,
SRes<RenderPipelineCache>,
SQuery<Read<ViewUniformOffset>>,
SQuery<Read<SpriteBatch>>,
)>,
}
impl DrawSprite {
pub fn new(world: &mut World) -> Self {
Self {
params: SystemState::new(world),
}
}
}
impl Draw<Transparent2d> for DrawSprite {
fn draw<'w>(
&mut self,
world: &'w World,
pass: &mut TrackedRenderPass<'w>,
view: Entity,
item: &Transparent2d,
) {
let (sprite_meta, image_bind_groups, pipelines, views, sprites) = self.params.get(world);
let view_uniform = views.get(view).unwrap();
let sprite_meta = sprite_meta.into_inner();
let image_bind_groups = image_bind_groups.into_inner();
let sprite_batch = sprites.get(item.entity).unwrap();
if let Some(pipeline) = pipelines.into_inner().get(item.pipeline) {
pass.set_render_pipeline(pipeline);
if sprite_batch.colored {
pass.set_vertex_buffer(0, sprite_meta.colored_vertices.buffer().unwrap().slice(..));
} else {
pass.set_vertex_buffer(0, sprite_meta.vertices.buffer().unwrap().slice(..));
}
pass.set_bind_group(
0,
sprite_meta.view_bind_group.as_ref().unwrap(),
&[view_uniform.offset],
);
pass.set_bind_group(
1,
image_bind_groups.values.get(&sprite_batch.handle).unwrap(),
&[],
);
pass.draw(sprite_batch.range.clone(), 0..1);
}
}
}