Skip to content

Commit 1076a8f

Browse files
kurtkuehnertdataphractcart
committed
Document the new pipelined renderer (#3094)
This is a squash-and-rebase of @Ku95's documentation of the new renderer onto the latest `pipelined-rendering` branch. Original PR is #2884. Co-authored-by: dataphract <[email protected]> Co-authored-by: Carter Anderson <[email protected]>
1 parent 14ce281 commit 1076a8f

33 files changed

+590
-70
lines changed

crates/bevy_ecs/src/system/function_system.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,18 @@ impl<Param: SystemParam> SystemState<Param> {
170170
}
171171
}
172172

173+
/// A trait for defining systems with a [`SystemParam`] associated type.
174+
///
175+
/// This facilitates the creation of systems that are generic over some trait
176+
/// and that use that trait's associated types as `SystemParam`s.
173177
pub trait RunSystem: Send + Sync + 'static {
178+
/// The `SystemParam` type passed to the system when it runs.
174179
type Param: SystemParam;
180+
181+
/// Runs the system.
175182
fn run(param: SystemParamItem<Self::Param>);
183+
184+
/// Creates a concrete instance of the system for the specified `World`.
176185
fn system(world: &mut World) -> ParamSystem<Self::Param> {
177186
ParamSystem {
178187
run: Self::run,

pipelined/bevy_gltf2/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use bevy_reflect::TypeUuid;
1010
use bevy_render2::mesh::Mesh;
1111
use bevy_scene::Scene;
1212

13-
/// Adds support for GLTF file loading to Apps
13+
/// Adds support for glTF file loading to the app.
1414
#[derive(Default)]
1515
pub struct GltfPlugin;
1616

@@ -24,6 +24,7 @@ impl Plugin for GltfPlugin {
2424
}
2525
}
2626

27+
/// Representation of a loaded glTF file.
2728
#[derive(Debug, TypeUuid)]
2829
#[uuid = "5c7d5f8a-f7b0-4e45-a09e-406c0372fea2"]
2930
pub struct Gltf {
@@ -38,6 +39,8 @@ pub struct Gltf {
3839
pub default_scene: Option<Handle<Scene>>,
3940
}
4041

42+
/// A glTF node with all of its child nodes, its [`GltfMesh`] and
43+
/// [`Transform`](bevy_transform::prelude::Transform).
4144
#[derive(Debug, Clone, TypeUuid)]
4245
#[uuid = "dad74750-1fd6-460f-ac51-0a7937563865"]
4346
pub struct GltfNode {
@@ -46,12 +49,14 @@ pub struct GltfNode {
4649
pub transform: bevy_transform::prelude::Transform,
4750
}
4851

52+
/// A glTF mesh, which may consists of multiple [`GtlfPrimitives`](GltfPrimitive).
4953
#[derive(Debug, Clone, TypeUuid)]
5054
#[uuid = "8ceaec9a-926a-4f29-8ee3-578a69f42315"]
5155
pub struct GltfMesh {
5256
pub primitives: Vec<GltfPrimitive>,
5357
}
5458

59+
/// Part of a [`GltfMesh`] that consists of a [`Mesh`] and an optional [`StandardMaterial`].
5560
#[derive(Debug, Clone, TypeUuid)]
5661
#[uuid = "cbfca302-82fd-41cb-af77-cab6b3d50af1"]
5762
pub struct GltfPrimitive {

pipelined/bevy_gltf2/src/loader.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ use wgpu::{AddressMode, FilterMode, PrimitiveTopology, SamplerDescriptor, Textur
3535

3636
use crate::{Gltf, GltfNode};
3737

38-
/// An error that occurs when loading a GLTF file
38+
/// An error that occurs when loading a glTF file.
3939
#[derive(Error, Debug)]
4040
pub enum GltfError {
4141
#[error("unsupported primitive mode")]
4242
UnsupportedPrimitive { mode: Mode },
43-
#[error("invalid GLTF file: {0}")]
43+
#[error("invalid glTF file: {0}")]
4444
Gltf(#[from] gltf::Error),
4545
#[error("binary blob is missing")]
4646
MissingBlob,
@@ -56,7 +56,7 @@ pub enum GltfError {
5656
AssetIoError(#[from] AssetIoError),
5757
}
5858

59-
/// Loads meshes from GLTF files into Mesh assets
59+
/// Loads glTF files with all of their data as their corresponding bevy representations.
6060
#[derive(Default)]
6161
pub struct GltfLoader;
6262

@@ -74,6 +74,7 @@ impl AssetLoader for GltfLoader {
7474
}
7575
}
7676

77+
/// Loads an entire glTF file.
7778
async fn load_gltf<'a, 'b>(
7879
bytes: &'a [u8],
7980
load_context: &'a mut LoadContext<'b>,
@@ -265,7 +266,7 @@ async fn load_gltf<'a, 'b>(
265266
.into_iter()
266267
.filter_map(|res| {
267268
if let Err(err) = res.as_ref() {
268-
warn!("Error loading GLTF texture: {}", err);
269+
warn!("Error loading glTF texture: {}", err);
269270
}
270271
res.ok()
271272
})
@@ -320,6 +321,7 @@ async fn load_gltf<'a, 'b>(
320321
Ok(())
321322
}
322323

324+
/// Loads a glTF texture as a bevy [`Image`] and returns it together with its label.
323325
async fn load_texture<'a>(
324326
gltf_texture: gltf::Texture<'a>,
325327
buffer_data: &[Vec<u8>],
@@ -368,6 +370,7 @@ async fn load_texture<'a>(
368370
Ok((texture, texture_label(&gltf_texture)))
369371
}
370372

373+
/// Loads a glTF material as a bevy [`StandardMaterial`] and returns it.
371374
fn load_material(material: &Material, load_context: &mut LoadContext) -> Handle<StandardMaterial> {
372375
let material_label = material_label(material);
373376

@@ -444,6 +447,7 @@ fn load_material(material: &Material, load_context: &mut LoadContext) -> Handle<
444447
)
445448
}
446449

450+
/// Loads a glTF node.
447451
fn load_node(
448452
gltf_node: &gltf::Node,
449453
world_builder: &mut WorldChildBuilder,
@@ -559,14 +563,17 @@ fn load_node(
559563
}
560564
}
561565

566+
/// Returns the label for the `mesh`.
562567
fn mesh_label(mesh: &gltf::Mesh) -> String {
563568
format!("Mesh{}", mesh.index())
564569
}
565570

571+
/// Returns the label for the `mesh` and `primitive`.
566572
fn primitive_label(mesh: &gltf::Mesh, primitive: &Primitive) -> String {
567573
format!("Mesh{}/Primitive{}", mesh.index(), primitive.index())
568574
}
569575

576+
/// Returns the label for the `material`.
570577
fn material_label(material: &gltf::Material) -> String {
571578
if let Some(index) = material.index() {
572579
format!("Material{}", index)
@@ -575,18 +582,22 @@ fn material_label(material: &gltf::Material) -> String {
575582
}
576583
}
577584

585+
/// Returns the label for the `texture`.
578586
fn texture_label(texture: &gltf::Texture) -> String {
579587
format!("Texture{}", texture.index())
580588
}
581589

590+
/// Returns the label for the `node`.
582591
fn node_label(node: &gltf::Node) -> String {
583592
format!("Node{}", node.index())
584593
}
585594

595+
/// Returns the label for the `scene`.
586596
fn scene_label(scene: &gltf::Scene) -> String {
587597
format!("Scene{}", scene.index())
588598
}
589599

600+
/// Extracts the texture sampler data from the glTF texture.
590601
fn texture_sampler<'a>(texture: &gltf::Texture) -> SamplerDescriptor<'a> {
591602
let gltf_sampler = texture.sampler();
592603

@@ -631,6 +642,7 @@ fn texture_sampler<'a>(texture: &gltf::Texture) -> SamplerDescriptor<'a> {
631642
}
632643
}
633644

645+
/// Maps the texture address mode form glTF to wgpu.
634646
fn texture_address_mode(gltf_address_mode: &gltf::texture::WrappingMode) -> AddressMode {
635647
match gltf_address_mode {
636648
WrappingMode::ClampToEdge => AddressMode::ClampToEdge,
@@ -639,6 +651,7 @@ fn texture_address_mode(gltf_address_mode: &gltf::texture::WrappingMode) -> Addr
639651
}
640652
}
641653

654+
/// Maps the primitive_topology form glTF to wgpu.
642655
fn get_primitive_topology(mode: Mode) -> Result<PrimitiveTopology, GltfError> {
643656
match mode {
644657
Mode::Points => Ok(PrimitiveTopology::PointList),
@@ -658,6 +671,7 @@ fn alpha_mode(material: &Material) -> AlphaMode {
658671
}
659672
}
660673

674+
/// Loads the raw glTF buffer data for a specific glTF file.
661675
async fn load_buffers(
662676
gltf: &gltf::Gltf,
663677
load_context: &LoadContext<'_>,

pipelined/bevy_pbr2/src/bundle.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use bevy_render2::{
88
};
99
use bevy_transform::components::{GlobalTransform, Transform};
1010

11+
/// A component bundle for PBR entities with a [`Mesh`] and a [`StandardMaterial`].
1112
#[derive(Bundle, Clone)]
1213
pub struct PbrBundle {
1314
pub mesh: Handle<Mesh>,
@@ -56,7 +57,7 @@ impl CubemapVisibleEntities {
5657
}
5758
}
5859

59-
/// A component bundle for "point light" entities
60+
/// A component bundle for [`PointLight`] entities.
6061
#[derive(Debug, Bundle, Default)]
6162
pub struct PointLightBundle {
6263
pub point_light: PointLight,
@@ -66,7 +67,7 @@ pub struct PointLightBundle {
6667
pub global_transform: GlobalTransform,
6768
}
6869

69-
/// A component bundle for "directional light" entities
70+
/// A component bundle for [`DirectionalLight`] entities.
7071
#[derive(Debug, Bundle, Default)]
7172
pub struct DirectionalLightBundle {
7273
pub directional_light: DirectionalLight,

pipelined/bevy_pbr2/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use bevy_transform::TransformSystem;
2727

2828
pub mod draw_3d_graph {
2929
pub mod node {
30+
/// Label for the shadow pass node.
3031
pub const SHADOW_PASS: &str = "shadow_pass";
3132
}
3233
}
@@ -36,6 +37,7 @@ pub const PBR_SHADER_HANDLE: HandleUntyped =
3637
pub const SHADOW_SHADER_HANDLE: HandleUntyped =
3738
HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 1836745567947005696);
3839

40+
/// Sets up the entire PBR infrastructure of bevy.
3941
#[derive(Default)]
4042
pub struct PbrPlugin;
4143

pipelined/bevy_pbr2/src/light.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ impl Default for DirectionalLightShadowMap {
145145
}
146146
}
147147

148-
/// Ambient light.
148+
/// An ambient light, which lights the entire scene equally.
149149
#[derive(Debug)]
150150
pub struct AmbientLight {
151151
pub color: Color,
152-
/// A direct scale factor multiplied with `color` before being passed to the shader
152+
/// A direct scale factor multiplied with `color` before being passed to the shader.
153153
pub brightness: f32,
154154
}
155155

@@ -162,9 +162,9 @@ impl Default for AmbientLight {
162162
}
163163
}
164164

165-
/// Add this component to make a `Mesh` not cast shadows
165+
/// Add this component to make a [`Mesh`](bevy_render2::mesh::Mesh) not cast shadows.
166166
pub struct NotShadowCaster;
167-
/// Add this component to make a `Mesh` not receive shadows
167+
/// Add this component to make a [`Mesh`](bevy_render2::mesh::Mesh) not receive shadows.
168168
pub struct NotShadowReceiver;
169169

170170
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)]

pipelined/bevy_pbr2/src/material.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ use crevice::std140::{AsStd140, Std140};
1515
use wgpu::{BindGroupDescriptor, BindGroupEntry, BindingResource};
1616

1717
/// A material with "standard" properties used in PBR lighting
18-
/// Standard property values with pictures here https://google.github.io/filament/Material%20Properties.pdf
18+
/// Standard property values with pictures here
19+
/// <https://google.github.io/filament/Material%20Properties.pdf>.
20+
///
21+
/// May be created directly from a [`Color`] or an [`Image`].
1922
#[derive(Debug, Clone, TypeUuid)]
2023
#[uuid = "7494888b-c082-457b-aacf-517228cc0c22"]
2124
pub struct StandardMaterial {
@@ -56,7 +59,7 @@ impl Default for StandardMaterial {
5659
emissive: Color::BLACK,
5760
emissive_texture: None,
5861
// This is the minimum the roughness is clamped to in shader code
59-
// See https://google.github.io/filament/Filament.html#materialsystem/parameterization/
62+
// See <https://google.github.io/filament/Filament.html#materialsystem/parameterization/>
6063
// It's the minimum floating point value that won't be rounded down to 0 in the
6164
// calculations used. Although technically for 32-bit floats, 0.045 could be
6265
// used.
@@ -66,7 +69,8 @@ impl Default for StandardMaterial {
6669
metallic: 0.01,
6770
metallic_roughness_texture: None,
6871
// Minimum real-world reflectance is 2%, most materials between 2-5%
69-
// Expressed in a linear scale and equivalent to 4% reflectance see https://google.github.io/filament/Material%20Properties.pdf
72+
// Expressed in a linear scale and equivalent to 4% reflectance see
73+
// <https://google.github.io/filament/Material%20Properties.pdf>
7074
reflectance: 0.5,
7175
occlusion_texture: None,
7276
normal_map_texture: None,
@@ -95,6 +99,7 @@ impl From<Handle<Image>> for StandardMaterial {
9599
}
96100
}
97101

102+
/// The GPU representation of the uniform data of a [`StandardMaterial`].
98103
#[derive(Clone, Default, AsStd140)]
99104
pub struct StandardMaterialUniformData {
100105
/// Doubles as diffuse albedo for non-metallic, specular for metallic and a mix for everything
@@ -117,6 +122,7 @@ pub struct StandardMaterialUniformData {
117122
pub alpha_cutoff: f32,
118123
}
119124

125+
/// This plugin adds the [`StandardMaterial`] asset to the app.
120126
pub struct StandardMaterialPlugin;
121127

122128
impl Plugin for StandardMaterialPlugin {
@@ -126,9 +132,13 @@ impl Plugin for StandardMaterialPlugin {
126132
}
127133
}
128134

135+
/// The GPU representation of a [`StandardMaterial`].
129136
#[derive(Debug, Clone)]
130137
pub struct GpuStandardMaterial {
138+
/// A buffer containing the [`StandardMaterialUniformData`] of the material.
131139
pub buffer: Buffer,
140+
/// The bind group specifying how the [`StandardMaterialUniformData`] and
141+
/// all the textures of the material are bound.
132142
pub bind_group: BindGroup,
133143
pub has_normal_map: bool,
134144
pub flags: StandardMaterialFlags,

pipelined/bevy_render2/src/lib.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,32 @@ use bevy_ecs::prelude::*;
2929
use std::ops::{Deref, DerefMut};
3030
use wgpu::Backends;
3131

32+
/// Contains the default Bevy rendering backend based on wgpu.
3233
#[derive(Default)]
3334
pub struct RenderPlugin;
3435

35-
/// The names of the default App stages
36+
/// The labels of the default App rendering stages.
3637
#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
3738
pub enum RenderStage {
38-
/// Extract data from "app world" and insert it into "render world". This step should be kept
39-
/// as short as possible to increase the "pipelining potential" for running the next frame
40-
/// while rendering the current frame.
39+
/// Extract data from the "app world" and insert it into the "render world".
40+
/// This step should be kept as short as possible to increase the "pipelining potential" for
41+
/// running the next frame while rendering the current frame.
4142
Extract,
4243

43-
/// Prepare render resources from extracted data.
44+
/// Prepare render resources from the extracted data for the GPU.
4445
Prepare,
4546

46-
/// Create Bind Groups that depend on Prepare data and queue up draw calls to run during the Render stage.
47+
/// Create [`BindGroups`](crate::render_resource::BindGroup) that depend on
48+
/// [`Prepare`](RenderStage::Prepare) data and queue up draw calls to run during the
49+
/// [`Render`](RenderStage::Render) stage.
4750
Queue,
4851

4952
// TODO: This could probably be moved in favor of a system ordering abstraction in Render or Queue
50-
/// Sort RenderPhases here
53+
/// Sort the [`RenderPhases`](crate::render_phase::RenderPhase) here.
5154
PhaseSort,
5255

53-
/// Actual rendering happens here. In most cases, only the render backend should insert resources here
56+
/// Actual rendering happens here.
57+
/// In most cases, only the render backend should insert resources here.
5458
Render,
5559

5660
/// Cleanup render resources here.
@@ -75,16 +79,17 @@ impl DerefMut for RenderWorld {
7579
}
7680
}
7781

78-
/// Label for the rendering sub-app
82+
/// A Label for the rendering sub-app.
7983
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, AppLabel)]
8084
pub struct RenderApp;
8185

8286
/// A "scratch" world used to avoid allocating new worlds every frame when
83-
// swapping out the Render World.
87+
/// swapping out the [`RenderWorld`].
8488
#[derive(Default)]
8589
struct ScratchRenderWorld(World);
8690

8791
impl Plugin for RenderPlugin {
92+
/// Initializes the renderer, sets up the [`RenderStage`](RenderStage) and creates the rendering sub-app.
8893
fn build(&self, app: &mut App) {
8994
let default_backend = if cfg!(not(target_arch = "wasm32")) {
9095
Backends::PRIMARY
@@ -271,6 +276,8 @@ impl Plugin for RenderPlugin {
271276
}
272277
}
273278

279+
/// Executes the [`Extract`](RenderStage::Extract) stage of the renderer.
280+
/// This updates the render world with the extracted ECS data of the current frame.
274281
fn extract(app_world: &mut World, render_app: &mut App) {
275282
let extract = render_app
276283
.schedule

0 commit comments

Comments
 (0)