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
pub mod batch;
pub mod camera;
pub mod color;
pub mod draw;
pub mod entity;
pub mod mesh;
pub mod pass;
pub mod pipeline;
pub mod render_graph;
pub mod renderer;
pub mod shader;
pub mod texture;

pub use once_cell;

pub mod prelude {
    pub use crate::{
        base::Msaa,
        color::Color,
        draw::Draw,
        entity::*,
        mesh::{shape, Mesh},
        pass::ClearColor,
        pipeline::RenderPipelines,
        shader::Shader,
        texture::Texture,
    };
}

use crate::prelude::*;
use base::{MainPass, Msaa};
use bevy_app::prelude::*;
use bevy_asset::AddAsset;
use bevy_ecs::{IntoQuerySystem, IntoThreadLocalSystem};
use bevy_type_registry::RegisterType;
use camera::{
    ActiveCameras, Camera, OrthographicProjection, PerspectiveProjection, VisibleEntities,
};
use pipeline::{
    DynamicBinding, PipelineCompiler, PipelineDescriptor, PipelineSpecialization,
    PrimitiveTopology, ShaderSpecialization, VertexBufferDescriptors,
};
use render_graph::{
    base::{self, BaseRenderGraphBuilder, BaseRenderGraphConfig},
    RenderGraph,
};
use renderer::{AssetRenderResourceBindings, RenderResourceBindings};
use std::ops::Range;
#[cfg(feature = "hdr")]
use texture::HdrTextureLoader;
#[cfg(feature = "png")]
use texture::ImageTextureLoader;
use texture::TextureResourceSystemState;

/// The names of "render" App stages
pub mod stage {
    /// Stage where render resources are set up
    pub static RENDER_RESOURCE: &str = "render_resource";
    /// Stage where Render Graph systems are run. In general you shouldn't add systems to this stage manually.
    pub static RENDER_GRAPH_SYSTEMS: &str = "render_graph_systems";
    // Stage where draw systems are executed. This is generally where Draw components are setup
    pub static DRAW: &str = "draw";
    pub static RENDER: &str = "render";
    pub static POST_RENDER: &str = "post_render";
}

/// Adds core render types and systems to an App
pub struct RenderPlugin {
    /// configures the "base render graph". If this is not `None`, the "base render graph" will be added  
    pub base_render_graph_config: Option<BaseRenderGraphConfig>,
}

impl Default for RenderPlugin {
    fn default() -> Self {
        RenderPlugin {
            base_render_graph_config: Some(BaseRenderGraphConfig::default()),
        }
    }
}

impl Plugin for RenderPlugin {
    fn build(&self, app: &mut AppBuilder) {
        #[cfg(feature = "png")]
        {
            app.add_asset_loader::<Texture, ImageTextureLoader>();
        }
        #[cfg(feature = "hdr")]
        {
            app.add_asset_loader::<Texture, HdrTextureLoader>();
        }

        app.add_stage_after(bevy_asset::stage::ASSET_EVENTS, stage::RENDER_RESOURCE)
            .add_stage_after(stage::RENDER_RESOURCE, stage::RENDER_GRAPH_SYSTEMS)
            .add_stage_after(stage::RENDER_GRAPH_SYSTEMS, stage::DRAW)
            .add_stage_after(stage::DRAW, stage::RENDER)
            .add_stage_after(stage::RENDER, stage::POST_RENDER)
            .add_asset::<Mesh>()
            .add_asset::<Texture>()
            .add_asset::<Shader>()
            .add_asset::<PipelineDescriptor>()
            .register_component::<Camera>()
            .register_component::<Draw>()
            .register_component::<RenderPipelines>()
            .register_component::<OrthographicProjection>()
            .register_component::<PerspectiveProjection>()
            .register_component::<MainPass>()
            .register_component::<VisibleEntities>()
            .register_property::<Color>()
            .register_property::<Range<f32>>()
            .register_property::<ShaderSpecialization>()
            .register_property::<DynamicBinding>()
            .register_property::<PrimitiveTopology>()
            .register_properties::<PipelineSpecialization>()
            .init_resource::<RenderGraph>()
            .init_resource::<PipelineCompiler>()
            .init_resource::<RenderResourceBindings>()
            .init_resource::<VertexBufferDescriptors>()
            .init_resource::<TextureResourceSystemState>()
            .init_resource::<AssetRenderResourceBindings>()
            .init_resource::<ActiveCameras>()
            .add_system_to_stage(
                bevy_app::stage::PRE_UPDATE,
                draw::clear_draw_system.system(),
            )
            .add_system_to_stage(
                bevy_app::stage::POST_UPDATE,
                camera::active_cameras_system.system(),
            )
            .add_system_to_stage(
                bevy_app::stage::POST_UPDATE,
                camera::camera_system::<OrthographicProjection>.system(),
            )
            .add_system_to_stage(
                bevy_app::stage::POST_UPDATE,
                camera::camera_system::<PerspectiveProjection>.system(),
            )
            // registration order matters here. this must come after all camera_system::<T> systems
            .add_system_to_stage(
                bevy_app::stage::POST_UPDATE,
                camera::visible_entities_system.system(),
            )
            // TODO: turn these "resource systems" into graph nodes and remove the RENDER_RESOURCE stage
            .add_system_to_stage(
                stage::RENDER_RESOURCE,
                mesh::mesh_resource_provider_system.system(),
            )
            .add_system_to_stage(
                stage::RENDER_RESOURCE,
                Texture::texture_resource_system.system(),
            )
            .add_system_to_stage(
                stage::RENDER_GRAPH_SYSTEMS,
                render_graph::render_graph_schedule_executor_system.thread_local_system(),
            )
            .add_system_to_stage(stage::DRAW, pipeline::draw_render_pipelines_system.system())
            .add_system_to_stage(
                stage::POST_RENDER,
                shader::clear_shader_defs_system.system(),
            );

        if app.resources().get::<Msaa>().is_none() {
            app.init_resource::<Msaa>();
        }

        if let Some(ref config) = self.base_render_graph_config {
            let resources = app.resources();
            let mut render_graph = resources.get_mut::<RenderGraph>().unwrap();
            let msaa = resources.get::<Msaa>().unwrap();
            render_graph.add_base_graph(config, &msaa);
            let mut active_cameras = resources.get_mut::<ActiveCameras>().unwrap();
            if config.add_3d_camera {
                active_cameras.add(base::camera::CAMERA3D);
            }

            if config.add_2d_camera {
                active_cameras.add(base::camera::CAMERA2D);
            }
        }
    }
}