-
Notifications
You must be signed in to change notification settings - Fork 124
/
lib.rs
540 lines (492 loc) · 16.1 KB
/
lib.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
use core::fmt;
use std::{
f32::consts::PI,
fmt::{Debug, Formatter},
str::FromStr,
sync::Arc,
};
use ambient_core::{
asset_cache, async_ecs::async_run, mesh, runtime, transform::get_world_rotation,
};
use ambient_ecs::{
components, copy_component_recursive, query_mut, Debuggable, Entity, EntityId, Resource,
SystemGroup, World,
};
use ambient_gpu::{
gpu::Gpu,
mesh_buffer::GpuMesh,
shader_module::{BindGroupDesc, Shader, ShaderIdent, ShaderModule},
wgsl_utils::wgsl_interpolate,
};
use ambient_gpu_ecs::{
gpu_components, ComponentToGpuSystem, GpuComponentFormat, GpuWorldShaderModuleKey,
GpuWorldSyncEvent,
};
use ambient_native_std::{asset_cache::*, asset_url::AbsAssetUrl, cb, include_file, Cb};
use derive_more::*;
use glam::{uvec4, UVec2, UVec4, Vec3};
use serde::{Deserialize, Serialize};
pub mod bind_groups;
mod collect;
mod culling;
mod globals;
pub mod lod;
pub mod materials;
mod outlines;
mod overlay_renderer;
mod renderer;
mod shaders;
mod shadow_renderer;
pub mod skinning;
mod target;
mod transparent_renderer;
mod tree_renderer;
use ambient_ecs::{query, Component};
pub use collect::*;
pub use culling::*;
pub use globals::*;
use materials::pbr_material::PbrMaterialFromUrl;
pub use materials::*;
use ordered_float::OrderedFloat;
pub use outlines::*;
pub use renderer::*;
pub use shaders::*;
pub use shadow_renderer::*;
pub use target::*;
pub use transparent_renderer::*;
pub use tree_renderer::*;
pub const MAX_PRIMITIVE_COUNT: usize = 16;
pub use ambient_ecs::generated::rendering::components::{
cast_shadows, color, double_sided, fog_color, fog_density, fog_height_falloff, light_ambient,
light_diffuse, overlay, pbr_material_from_url, scissors, scissors_recursive, sun,
transparency_group,
};
components!("rendering", {
@[Debuggable]
primitives: Vec<RenderPrimitive>,
/// The (cpu) primitives are split into an SoA on the gpu side
@[Debuggable]
gpu_primitives_mesh: [u32; MAX_PRIMITIVE_COUNT],
@[Debuggable]
gpu_primitives_lod: [u32; MAX_PRIMITIVE_COUNT],
renderer_shader: RendererShaderProducer,
material: SharedMaterial,
@[Resource]
renderer_stats: String,
});
gpu_components! {
color() => color: GpuComponentFormat::Vec4,
gpu_primitives_mesh() => gpu_primitives_mesh: GpuComponentFormat::Mat4,
gpu_primitives_lod() => gpu_primitives_lod: GpuComponentFormat::Mat4,
}
pub fn init_all_components() {
init_components();
init_gpu_components();
outlines::init_gpu_components();
culling::init_gpu_components();
lod::init_components();
lod::init_gpu_components();
skinning::init_components();
skinning::init_gpu_components();
}
pub fn systems() -> SystemGroup {
SystemGroup::new(
"renderer",
vec![
Box::new(copy_component_recursive(
"scissors",
scissors_recursive(),
scissors(),
)),
query(pbr_material_from_url().changed()).to_system(|q, world, qs, _| {
for (id, url) in q.collect_cloned(world, qs) {
let url = match AbsAssetUrl::from_str(&url) {
Ok(value) => value,
Err(err) => {
tracing::warn!("Failed to parse pbr_material_from_url url: {:?}", err);
continue;
}
};
let assets = world.resource(asset_cache()).clone();
let async_run = world.resource(async_run()).clone();
world.resource(runtime()).spawn(async move {
match PbrMaterialFromUrl(url).get(&assets).await {
Err(err) => {
tracing::warn!("Failed to load pbr material from url: {:?}", err);
}
Ok(mat) => {
async_run.run(move |world| {
world
.add_components(
id,
Entity::new()
.with(
renderer_shader(),
cb(pbr_material::get_pbr_shader),
)
.with(material(), mat.into()),
)
.ok();
});
}
}
});
}
}),
query_mut(
(primitives(),),
(
renderer_shader().changed(),
material().changed(),
mesh().changed(),
),
)
.to_system(|q, world, qs, _| {
for (_, (primitives,), (shader, material, mesh)) in q.iter(world, qs) {
*primitives = vec![RenderPrimitive {
shader: shader.clone(),
material: material.clone(),
mesh: mesh.clone(),
lod: 0,
}];
}
}),
query_mut(
(gpu_primitives_mesh(), gpu_primitives_lod()),
(primitives().changed(),),
)
.to_system(|q, world, qs, _| {
for (id, (p_mesh, p_lod), (primitives,)) in q.iter(world, qs) {
if primitives.len() > MAX_PRIMITIVE_COUNT {
tracing::warn!(
"Entity {} has more than {MAX_PRIMITIVE_COUNT} primitives",
id
);
}
for (i, p) in primitives.iter().enumerate().take(MAX_PRIMITIVE_COUNT) {
p_mesh[i] = p.mesh.index();
p_lod[i] = p.lod as u32;
}
}
}),
Box::new(outlines::systems()),
],
)
}
pub fn gpu_world_systems(gpu: Arc<Gpu>) -> SystemGroup<GpuWorldSyncEvent> {
SystemGroup::new(
"renderer/gpu_world_update",
vec![
Box::new(outlines::gpu_world_systems(gpu.clone())),
Box::new(ComponentToGpuSystem::new(
gpu.clone(),
GpuComponentFormat::Vec4,
color(),
gpu_components::color(),
)),
Box::new(ComponentToGpuSystem::new(
gpu.clone(),
GpuComponentFormat::Mat4,
gpu_primitives_mesh(),
gpu_components::gpu_primitives_mesh(),
)),
Box::new(ComponentToGpuSystem::new(
gpu.clone(),
GpuComponentFormat::Mat4,
gpu_primitives_lod(),
gpu_components::gpu_primitives_lod(),
)),
Box::new(lod::gpu_world_system(gpu.clone())),
Box::new(skinning::gpu_world_systems(gpu)),
],
)
}
pub fn get_active_sun(world: &World, scene: Component<()>) -> Option<EntityId> {
query((scene, sun()))
.iter(world, None)
.max_by_key(|(_, (_, x))| OrderedFloat(**x))
.map(|(id, _)| id)
}
pub fn get_sun_light_direction(world: &World, scene: Component<()>) -> Vec3 {
get_active_sun(world, scene)
.and_then(|sun| get_world_rotation(world, sun).ok())
.map(|rot| rot.mul_vec3(Vec3::X))
.unwrap_or(default_sun_direction())
}
#[derive(Clone, Debug)]
pub struct RenderPrimitive {
pub material: SharedMaterial,
pub shader: RendererShaderProducer,
pub mesh: Arc<GpuMesh>,
pub lod: usize,
}
pub type PrimitiveIndex = usize;
pub fn get_gpu_primitive_id(
world: &World,
id: EntityId,
primitive_index: PrimitiveIndex,
material_index: u32,
) -> UVec4 {
let loc = world.entity_loc(id).unwrap();
uvec4(
loc.archetype as u32,
loc.index as u32,
primitive_index as u32,
material_index,
)
}
#[repr(C)]
#[derive(Debug, Clone, Copy, Default, bytemuck::Pod, bytemuck::Zeroable)]
pub struct GpuRenderPrimitive {
pub mesh: u32,
pub lod: u32,
pub _padding: UVec2,
}
#[derive(Clone, Deref, DerefMut)]
pub struct SharedMaterial(pub Arc<dyn Material + 'static>);
impl Debug for SharedMaterial {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Debug::fmt(&*self.0, f)
}
}
impl<T: Material + 'static> From<Arc<T>> for SharedMaterial {
fn from(v: Arc<T>) -> Self {
Self(v as Arc<dyn Material>)
}
}
impl SharedMaterial {
pub fn replace(&mut self, value: impl Material + 'static) {
self.0 = Arc::new(value)
}
pub fn new(value: impl Material + 'static) -> Self {
Self(Arc::new(value))
}
pub fn borrow_downcast<T: Material>(&self) -> &T {
use as_any::Downcast;
(*self.0).downcast_ref::<T>().unwrap()
}
}
impl<T> From<T> for SharedMaterial
where
T: Material + 'static,
{
fn from(v: T) -> Self {
Self::new(v)
}
}
/// No bind groups
pub fn get_defs_module() -> Arc<ShaderModule> {
let iter = [("PI", PI)].iter();
let iter = iter.map(|(k, v)| format!("const {k}: f32 = {v};\n"));
let iter = iter
.chain([wgsl_interpolate(), include_file!("polyfill.wgsl")])
.collect::<String>();
Arc::new(ShaderModule::new("defs", iter))
}
pub fn get_mesh_meta_module(bind_group_offset: u32) -> Arc<ShaderModule> {
Arc::new(
ShaderModule::new("mesh_meta", include_file!("mesh_meta.wgsl"))
.with_ident(ShaderIdent::constant(
"MESH_METADATA_BINDING",
bind_group_offset + MESH_METADATA_BINDING,
))
.with_binding_desc(get_mesh_meta_layout(bind_group_offset)),
)
}
pub fn get_mesh_data_module(bind_group_offset: u32) -> Arc<ShaderModule> {
Arc::new(
ShaderModule::new("mesh_data", include_file!("mesh_data.wgsl"))
.with_ident(ShaderIdent::constant(
"MESH_BASE_BINDING",
bind_group_offset + MESH_BASE_BINDING,
))
.with_ident(ShaderIdent::constant(
"MESH_SKIN_BINDING",
bind_group_offset + MESH_SKIN_BINDING,
))
.with_ident(ShaderIdent::constant(
"SKINS_BINDING",
bind_group_offset + SKINS_BINDING,
))
.with_binding_desc(get_mesh_data_layout(bind_group_offset))
.with_dependency(get_mesh_meta_module(bind_group_offset)),
)
}
pub fn primitives_layout() -> BindGroupDesc<'static> {
BindGroupDesc {
entries: vec![wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: true },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
}],
label: PRIMITIVES_BIND_GROUP.into(),
}
}
pub fn get_common_layout() -> BindGroupDesc<'static> {
BindGroupDesc {
entries: vec![wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: true },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
}],
label: PRIMITIVES_BIND_GROUP.into(),
}
}
/// Includes entity locs
pub fn get_common_module(_: &AssetCache) -> Arc<ShaderModule> {
Arc::new(
ShaderModule::new("renderer_common", include_file!("renderer_common.wgsl"))
.with_binding_desc(get_common_layout())
.with_dependency(get_mesh_data_module(GLOBALS_BIND_GROUP_SIZE)),
)
}
/// Contains scene globals and shadow maps
pub fn get_globals_module(_assets: &AssetCache, shadow_cascades: u32) -> Arc<ShaderModule> {
Arc::new(
ShaderModule::new("globals", include_file!("globals.wgsl"))
.with_ident(ShaderIdent::constant("SHADOW_CASCADES", shadow_cascades))
.with_binding_desc(globals_layout()),
)
}
pub fn get_forward_modules(assets: &AssetCache, shadow_cascades: u32) -> Vec<Arc<ShaderModule>> {
vec![
get_defs_module(),
get_mesh_data_module(GLOBALS_BIND_GROUP_SIZE),
get_globals_module(assets, shadow_cascades),
GpuWorldShaderModuleKey { read_only: true }.get(assets),
get_common_module(assets),
]
}
pub fn get_overlay_modules(assets: &AssetCache, shadow_cascades: u32) -> Vec<Arc<ShaderModule>> {
vec![
get_defs_module(),
get_globals_module(assets, shadow_cascades),
get_mesh_data_module(GLOBALS_BIND_GROUP_SIZE),
]
}
pub struct MaterialShader {
pub id: String,
pub shader: Arc<ShaderModule>,
}
pub trait Material: Debug + Sync + Send + as_any::AsAny {
fn id(&self) -> &str;
fn name(&self) -> &str {
self.id()
}
fn update(&self, _: &Gpu, _: &World) {}
fn bind_group(&self) -> &wgpu::BindGroup;
fn transparent(&self) -> Option<bool> {
None
}
fn double_sided(&self) -> Option<bool> {
None
}
/// TODO: Apply to tree renderer too (only applies to transparent now)
fn depth_write_enabled(&self) -> Option<bool> {
None
}
fn transparency_group(&self) -> Option<i32> {
None
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
#[repr(u32)]
pub enum AlphaMode {
Opaque,
Mask,
Blend,
}
impl Default for AlphaMode {
fn default() -> Self {
Self::Opaque
}
}
#[derive(Debug, Clone, Copy)]
pub enum FSMain {
Forward,
Shadow,
Outline,
}
pub struct RendererShader {
pub id: String,
pub shader: Arc<Shader>,
pub vs_main: String,
pub fs_shadow_main: String,
pub fs_forward_main: String,
pub fs_outline_main: String,
pub transparent: bool,
pub double_sided: bool,
/// TODO: Apply to tree renderer too (only applies to transparent now)
pub depth_write_enabled: bool,
pub transparency_group: i32,
}
impl RendererShader {
fn get_fs_main_name(&self, main: FSMain) -> &str {
match main {
FSMain::Forward => &self.fs_forward_main,
FSMain::Shadow => &self.fs_shadow_main,
FSMain::Outline => &self.fs_outline_main,
}
}
}
impl Debug for RendererShader {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RendererShader")
.field("id", &self.id)
.finish()
}
}
pub type RendererShaderProducer =
Cb<dyn Fn(&AssetCache, &RendererConfig) -> Arc<RendererShader> + Sync + Send>;
#[repr(C)]
#[derive(Debug, Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub struct DrawIndexedIndirect {
index_count: u32,
instance_count: u32,
first_index: u32,
base_vertex: u32,
first_instance: u32,
}
fn is_transparent(
world: &World,
id: EntityId,
material: &SharedMaterial,
shader: &RendererShader,
) -> bool {
world.get(id, transparency_group()).is_ok()
|| material.transparent().unwrap_or(shader.transparent)
}
/// wgpu will throw an exception if the scissor value is outside the viewport
pub(crate) fn set_scissors_safe(
render_pass: &mut wgpu::RenderPass,
render_target_size: wgpu::Extent3d,
scissors: Option<UVec4>,
) -> bool {
if let Some(scissors) = scissors {
let left = scissors.x.clamp(0, render_target_size.width);
let top = scissors.y.clamp(0, render_target_size.height);
let right = (left + scissors.z).clamp(0, render_target_size.width);
let bottom = (top + scissors.w).clamp(0, render_target_size.height);
let width = right - left;
let height = bottom - top;
if width > 0 && height > 0 {
render_pass.set_scissor_rect(left, top, width, height);
true
} else {
false
}
} else {
render_pass.set_scissor_rect(0, 0, render_target_size.width, render_target_size.height);
true
}
}