mod compositor;
mod debug_overlay;
mod depth_cloud;
mod generic_skybox;
mod lines;
mod mesh_renderer;
mod point_cloud;
mod rectangles;
mod test_triangle;
mod world_grid;
pub use debug_overlay::{DebugOverlayDrawData, DebugOverlayError, DebugOverlayRenderer};
pub use generic_skybox::{GenericSkyboxDrawData, GenericSkyboxType};
pub use lines::{LineBatchInfo, LineDrawData, LineDrawDataError, LineStripFlags};
pub use mesh_renderer::{GpuMeshInstance, MeshDrawData};
pub use point_cloud::{
PointCloudBatchFlags, PointCloudBatchInfo, PointCloudDrawData, PointCloudDrawDataError,
};
pub use rectangles::{
ColorMapper, ColormappedTexture, RectangleDrawData, RectangleOptions, ShaderDecoding,
TextureAlpha, TextureFilterMag, TextureFilterMin, TexturedRect,
};
pub use test_triangle::TestTriangleDrawData;
pub use world_grid::{WorldGridConfiguration, WorldGridDrawData, WorldGridRenderer};
pub use self::depth_cloud::{DepthCloud, DepthCloudDrawData, DepthCloudRenderer, DepthClouds};
pub mod gpu_data {
pub use super::lines::gpu_data::{LineStripInfo, LineVertex};
pub use super::point_cloud::gpu_data::PositionRadius;
}
pub(crate) use compositor::CompositorDrawData;
pub(crate) use mesh_renderer::MeshRenderer;
use crate::{
Drawable, DrawableCollector, QueueableDrawData,
context::RenderContext,
draw_phases::DrawPhase,
include_shader_module,
wgpu_resources::{GpuRenderPipelinePoolAccessor, PoolError},
};
pub type DrawDataDrawablePayload = u32;
#[derive(Debug, Clone, Copy)]
pub struct DrawDataDrawable {
pub distance_sort_key: f32,
pub draw_data_payload: DrawDataDrawablePayload,
}
impl DrawDataDrawable {
#[inline]
pub fn from_affine(
view_info: &DrawableCollectionViewInfo,
world_from_rdf: &glam::Affine3A,
draw_data_payload: DrawDataDrawablePayload,
) -> Self {
Self::from_world_position(view_info, world_from_rdf.translation, draw_data_payload)
}
#[inline]
pub fn from_world_position(
view_info: &DrawableCollectionViewInfo,
world_position: glam::Vec3A,
draw_data_payload: DrawDataDrawablePayload,
) -> Self {
Self {
distance_sort_key: world_position.distance_squared(view_info.camera_world_position),
draw_data_payload,
}
}
}
pub struct DrawableCollectionViewInfo {
pub camera_world_position: glam::Vec3A,
}
pub trait DrawData {
type Renderer: Renderer<RendererDrawData = Self> + Send + Sync;
fn collect_drawables(
&self,
view_info: &DrawableCollectionViewInfo,
collector: &mut DrawableCollector<'_>,
);
}
#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
pub enum DrawError {
#[error(transparent)]
Pool(#[from] PoolError),
}
pub struct DrawInstruction<'a, D> {
pub draw_data: &'a D,
pub drawables: &'a [Drawable],
}
pub trait Renderer {
type RendererDrawData: DrawData + 'static;
fn create_renderer(ctx: &RenderContext) -> Self;
fn draw(
&self,
render_pipelines: &GpuRenderPipelinePoolAccessor<'_>,
phase: DrawPhase,
pass: &mut wgpu::RenderPass<'_>,
draw_instructions: &[DrawInstruction<'_, Self::RendererDrawData>],
) -> Result<(), DrawError>;
}
pub(crate) trait RendererExt: Send + Sync {
fn run_draw_instructions(
&self,
gpu_resources: &GpuRenderPipelinePoolAccessor<'_>,
phase: DrawPhase,
pass: &mut wgpu::RenderPass<'_>,
type_erased_draw_instructions: &[DrawInstruction<'_, QueueableDrawData>],
) -> Result<(), DrawError>;
fn name(&self) -> &'static str;
}
impl<R: Renderer + Send + Sync> RendererExt for R {
fn run_draw_instructions(
&self,
gpu_resources: &GpuRenderPipelinePoolAccessor<'_>,
phase: DrawPhase,
pass: &mut wgpu::RenderPass<'_>,
type_erased_draw_instructions: &[DrawInstruction<'_, QueueableDrawData>],
) -> Result<(), DrawError> {
let draw_instructions: Vec<DrawInstruction<'_, R::RendererDrawData>> =
type_erased_draw_instructions
.iter()
.map(|type_erased_draw_instruction| DrawInstruction {
draw_data: type_erased_draw_instruction.draw_data.expect_downcast(),
drawables: type_erased_draw_instruction.drawables,
})
.collect();
self.draw(gpu_resources, phase, pass, &draw_instructions)
}
fn name(&self) -> &'static str {
std::any::type_name::<R>()
}
}
pub fn screen_triangle_vertex_shader(
ctx: &RenderContext,
) -> crate::wgpu_resources::GpuShaderModuleHandle {
ctx.gpu_resources.shader_modules.get_or_create(
ctx,
&include_shader_module!("../../shader/screen_triangle.wgsl"),
)
}