Module deferred

Module deferred 

Source
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 Transform and PbrModel (which have a vector of Mesh and PbrMaterial elements) from the main world to the render world, and stores them in the PbrModelRegistry resource. 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 Batches resource is created from the extracted entities, sorted by their material handle to minimize render pipeline changes. Each Batch points 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 (see ModelUuidToTransformUuidRender).
  • Finally, the render pass iterates over the batches and renders them, writing to the G-buffer DeferredTextures and the depth texture.
  • It then resolves the G-buffer textures to DeferredTexturesResolved for 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 LightsSsbo ssbo. 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 RenderTexture in MSAA format.

§Limitations

The number of supported lights and tracked PBR transform entries is limited by:

§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§

DirectionalLight
A directional light is a light that emits light in a single direction from an infinite distance.
ExtractedPbrInstance
LightsData
PbrBatchesMarker
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.
PbrSsboInstanceToTransform
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.
PbrSsboTransform
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.
PbrSsboTransformMarker
Marker component to indicate that an entity should be included in the SSBO transform updates.
PbrSsboTransformRegistry
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.
PointLight
A point light is a light that emits light in all directions from a single point.
SpotLight
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.