1mod compositor;
2mod debug_overlay;
3mod depth_cloud;
4mod generic_skybox;
5mod lines;
6mod mesh_renderer;
7mod point_cloud;
8mod rectangles;
9mod test_triangle;
10mod world_grid;
11
12pub use self::depth_cloud::{DepthCloud, DepthCloudDrawData, DepthCloudRenderer, DepthClouds};
13pub use debug_overlay::{DebugOverlayDrawData, DebugOverlayError, DebugOverlayRenderer};
14pub use generic_skybox::{GenericSkyboxDrawData, GenericSkyboxType};
15pub use lines::{LineBatchInfo, LineDrawData, LineDrawDataError, LineStripFlags};
16pub use mesh_renderer::{GpuMeshInstance, MeshDrawData};
17pub use point_cloud::{
18 PointCloudBatchFlags, PointCloudBatchInfo, PointCloudDrawData, PointCloudDrawDataError,
19};
20pub use rectangles::{
21 ColorMapper, ColormappedTexture, RectangleDrawData, RectangleOptions, ShaderDecoding,
22 TextureAlpha, TextureFilterMag, TextureFilterMin, TexturedRect,
23};
24pub use test_triangle::TestTriangleDrawData;
25pub use world_grid::{WorldGridConfiguration, WorldGridDrawData, WorldGridRenderer};
26
27pub mod gpu_data {
28 pub use super::lines::gpu_data::{LineStripInfo, LineVertex};
29 pub use super::point_cloud::gpu_data::PositionRadius;
30}
31
32pub(crate) use compositor::CompositorDrawData;
33pub(crate) use mesh_renderer::MeshRenderer;
34
35use crate::{
38 Drawable, DrawableCollector, QueueableDrawData,
39 context::RenderContext,
40 draw_phases::DrawPhase,
41 include_shader_module,
42 wgpu_resources::{GpuRenderPipelinePoolAccessor, PoolError},
43};
44
45pub type DrawDataDrawablePayload = u32;
47
48#[derive(Debug, Clone, Copy)]
53pub struct DrawDataDrawable {
54 pub distance_sort_key: f32,
62
63 pub draw_data_payload: DrawDataDrawablePayload,
68}
69
70impl DrawDataDrawable {
71 #[inline]
72 pub fn from_affine(
73 view_info: &DrawableCollectionViewInfo,
74 world_from_rdf: &glam::Affine3A,
75 draw_data_payload: DrawDataDrawablePayload,
76 ) -> Self {
77 Self::from_world_position(view_info, world_from_rdf.translation, draw_data_payload)
78 }
79
80 #[inline]
81 pub fn from_world_position(
82 view_info: &DrawableCollectionViewInfo,
83 world_position: glam::Vec3A,
84 draw_data_payload: DrawDataDrawablePayload,
85 ) -> Self {
86 Self {
87 distance_sort_key: world_position.distance_squared(view_info.camera_world_position),
88 draw_data_payload,
89 }
90 }
91}
92
93pub struct DrawableCollectionViewInfo {
95 pub camera_world_position: glam::Vec3A,
97}
98
99pub trait DrawData {
106 type Renderer: Renderer<RendererDrawData = Self> + Send + Sync;
107
108 fn collect_drawables(
116 &self,
117 view_info: &DrawableCollectionViewInfo,
118 collector: &mut DrawableCollector<'_>,
119 );
120}
121
122#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
123pub enum DrawError {
124 #[error(transparent)]
125 Pool(#[from] PoolError),
126}
127
128pub struct DrawInstruction<'a, D> {
130 pub draw_data: &'a D,
132
133 pub drawables: &'a [Drawable],
138}
139
140pub trait Renderer {
146 type RendererDrawData: DrawData + 'static;
147
148 fn create_renderer(ctx: &RenderContext) -> Self;
149
150 fn draw(
154 &self,
155 render_pipelines: &GpuRenderPipelinePoolAccessor<'_>,
156 phase: DrawPhase,
157 pass: &mut wgpu::RenderPass<'_>,
158 draw_instructions: &[DrawInstruction<'_, Self::RendererDrawData>],
159 ) -> Result<(), DrawError>;
160}
161
162pub(crate) trait RendererExt: Send + Sync {
164 fn run_draw_instructions(
165 &self,
166 gpu_resources: &GpuRenderPipelinePoolAccessor<'_>,
167 phase: DrawPhase,
168 pass: &mut wgpu::RenderPass<'_>,
169 type_erased_draw_instructions: &[DrawInstruction<'_, QueueableDrawData>],
170 ) -> Result<(), DrawError>;
171
172 fn name(&self) -> &'static str;
174}
175
176impl<R: Renderer + Send + Sync> RendererExt for R {
177 fn run_draw_instructions(
178 &self,
179 gpu_resources: &GpuRenderPipelinePoolAccessor<'_>,
180 phase: DrawPhase,
181 pass: &mut wgpu::RenderPass<'_>,
182 type_erased_draw_instructions: &[DrawInstruction<'_, QueueableDrawData>],
183 ) -> Result<(), DrawError> {
184 let draw_instructions: Vec<DrawInstruction<'_, R::RendererDrawData>> =
185 type_erased_draw_instructions
186 .iter()
187 .map(|type_erased_draw_instruction| DrawInstruction {
188 draw_data: type_erased_draw_instruction.draw_data.expect_downcast(),
189 drawables: type_erased_draw_instruction.drawables,
190 })
191 .collect();
192
193 self.draw(gpu_resources, phase, pass, &draw_instructions)
194 }
195
196 fn name(&self) -> &'static str {
197 std::any::type_name::<R>()
198 }
199}
200
201pub fn screen_triangle_vertex_shader(
205 ctx: &RenderContext,
206) -> crate::wgpu_resources::GpuShaderModuleHandle {
207 ctx.gpu_resources.shader_modules.get_or_create(
208 ctx,
209 &include_shader_module!("../../shader/screen_triangle.wgsl"),
210 )
211}