Expand description
Deferred PBR module.
This module connects PBR data (materials, models, lights) to the engine deferred render graph.
§PBR Overview
The PBR rendering stack is composed of two passes.
§GBuffer pass (see RenderPassDeferredGBuffer)
This pass is composed of a few steps that run every frame:
- It extracts every entity with a
TransformandPbrModel(which have a vector ofMeshandPbrMaterialelements) from the main world to the render world, and stores them in thePbrModelRegistryresource. Each model element is assigned a unique UUID, as well as a transform ID that corresponds to its entry in the GPU SsboTransform buffer. - When a transform or a model is added or changed in the main world, the corresponding transform is marked as dirty, and the SsboTransform buffer is updated with the new transform data at the next frame.
- Then, for each frame, a
Batchesresource is created from the extracted entities, sorted by their material handle to minimize render pipeline changes. EachBatchpoints to the corresponding mesh and material handles, as well as an instance index. It then uses another buffer to store pointers from the entity instance indices to their transform IDs in the SsboTransform buffer (seeModelUuidToTransformUuidRender). - Finally, the render pass iterates over the batches and renders them, writing to the G-buffer
DeferredTexturesand the depth texture. - It then resolves the G-buffer textures to
DeferredTexturesResolvedfor use in the lighting pass, which doesn’t use MSAA.
§Lighting pass (see RenderPassDeferredLighting)
- This pass first extracts the light data from the main world to the render world, and stores it in the
LightsSsbossbo. Supported light types are currently DirectionalLight, PointLight and SpotLight. - Then, for each frame, it reads the G-buffer textures and the light data, and computes the final lit colors, which it writes to a
RenderTexturein MSAA format.
§Limitations
The number of supported lights and tracked PBR transform entries is limited by:
MAX_LIGHTSlimits the GPU light buffer size.SSBO_TRANSFORM_MAX_ENTITYlimits tracked PBR transform entries.
§Custom rendering
To find out more about how to create custom subpasses, materials and render pipelines, see the documentation of the wde_renderer crate.
§Usage
// Spawn a directional PBR light
commands.spawn(DirectionalLight {
direction: Vec3::new(-0.5, -1.0, -0.25).normalize(),
..Default::default()
});
// Spawn a simple PBR model (with a single mesh and material)
commands.spawn((
Transform::from_xyz(0.0, 0.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
PbrModel(vec![(
asset_server.add("models/example_model.obj"),
asset_server.add(PbrMaterial {
albedo: Color::WHITE,
metallic: 0.5,
roughness: 0.5,
..Default::default()
})
)]
));Structs§
- Directional
Light - A directional light is a light that emits light in a single direction from an infinite distance.
- Extracted
PbrInstance - Lights
Data - PbrBatches
Marker - Marker component to indicate that an entity should be included in the PBR render batches. This will automatically add the PbrSsboTransformMarker to the entity, so that its transform will be included in the SSBO updates for rendering.
- PbrMaterial
- Describes a physically based rendering material. It contains the material properties such as albedo color, metallic and roughness intensity, as well as the textures for these properties. It also contains a uniform buffer that is automatically created and updated with the material data, which can be used in the shader to access the material properties. The PbrMaterial3d component is a wrapper around a handle to a PbrMaterial asset, which also adds the [PbrSsboTransformMarker] to the entity, indicating that its transform should be included in the PbrSsboTransform updates.
- PbrMaterial3d
- Marker component to indicate that an entity’s transform should be included in the [PbrSsboTransform] updates. This automatically adds the PbrBatchesMarker (and thus the [PbrSsboTransformMarker]) to the entity.
- PbrSsbo
Instance ToTransform - Contains pointers to the transforms of the entities in the SSBO, as well as the offset in the “instance to transform” buffer of the first entity of this batch. This is used to bind the correct transforms to the instances of each batch when rendering.
- PbrSsbo
Transform - Contains the transform data for the entities in the scene. See crate::deferred level documentation for more details on how this is used, and how it is updated.
- PbrSsbo
Transform Marker - Marker component to indicate that an entity should be included in the SSBO transform updates.
- PbrSsbo
Transform Registry - Resource to track the mapping between entities and their transform IDs in the SSBO, as well as the list of dirty transforms that need to be updated in the SSBO. This is used to update the PbrSsboTransform with the new transforms of the entities with a PBR material.
- Point
Light - A point light is a light that emits light in all directions from a single point.
- Spot
Light - A spotlight is a point light with a direction and cone angles.
Constants§
- MAX_
LIGHTS - Maximum number of lights in the scene.
- SSBO_
MAX_ BATCHES - The maximum number of batches in the ssbo.
- SSBO_
TRANSFORM_ MAX_ ENTITY - The maximum number of entity transforms in the ssbo.