Skip to content

Commit

Permalink
Change render order of entities and models
Browse files Browse the repository at this point in the history
This lets entities properly draw their transparent parts. Our draw order now is:

Model Opaque -> Entities -> Model Transparent
  • Loading branch information
hasenbanck committed Dec 18, 2024
1 parent 92d3c57 commit 1077bc5
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 44 deletions.
12 changes: 10 additions & 2 deletions korangar/src/graphics/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -991,20 +991,28 @@ impl GraphicsEngine {
.forward_pass_context
.create_pass(&mut forward_encoder, &engine_context.global_context, None);

let draw_data = ModelBatchDrawData {
let batch_data = &ModelBatchDrawData {
batches: instruction.model_batches,
instructions: instruction.models,
#[cfg(feature = "debug")]
show_wireframe: instruction.render_settings.show_wireframe,
};

engine_context.forward_model_drawer.draw(&mut render_pass, ForwardModelDrawData {
batch_data,
draw_transparent: false,
});

engine_context.forward_entity_drawer.draw(&mut render_pass, instruction.entities);

engine_context
.forward_indicator_drawer
.draw(&mut render_pass, instruction.indicator.as_ref());

engine_context.forward_model_drawer.draw(&mut render_pass, draw_data);
engine_context.forward_model_drawer.draw(&mut render_pass, ForwardModelDrawData {
batch_data,
draw_transparent: true,
});

#[cfg(feature = "debug")]
{
Expand Down
2 changes: 1 addition & 1 deletion korangar/src/graphics/passes/forward/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub(crate) use aabb::ForwardAabbDrawer;
pub(crate) use circle::ForwardCircleDrawer;
pub(crate) use entity::ForwardEntityDrawer;
pub(crate) use indicator::ForwardIndicatorDrawer;
pub(crate) use model::ForwardModelDrawer;
pub(crate) use model::{ForwardModelDrawData, ForwardModelDrawer};
#[cfg(feature = "debug")]
pub(crate) use rectangle::ForwardRectangleDrawer;
use wgpu::{
Expand Down
94 changes: 53 additions & 41 deletions korangar/src/graphics/passes/forward/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ struct InstanceData {
inv_world: [[f32; 4]; 4],
}

pub(crate) struct ForwardModelDrawData<'a> {
pub(crate) batch_data: &'a ModelBatchDrawData<'a>,
pub(crate) draw_transparent: bool,
}

pub(crate) struct ForwardModelDrawer {
multi_draw_indirect_support: bool,
instance_data_buffer: Buffer<InstanceData>,
Expand All @@ -51,7 +56,7 @@ pub(crate) struct ForwardModelDrawer {

impl Drawer<{ BindGroupCount::Two }, { ColorAttachmentCount::One }, { DepthAttachmentCount::One }> for ForwardModelDrawer {
type Context = ForwardRenderPassContext;
type DrawData<'data> = ModelBatchDrawData<'data>;
type DrawData<'data> = ForwardModelDrawData<'data>;

fn new(
capabilities: &Capabilities,
Expand Down Expand Up @@ -192,7 +197,9 @@ impl Drawer<{ BindGroupCount::Two }, { ColorAttachmentCount::One }, { DepthAttac
}

fn draw(&mut self, pass: &mut RenderPass<'_>, draw_data: Self::DrawData<'_>) {
if self.opaque_batches.is_empty() && self.transparent_batches.is_empty() {
if (!draw_data.draw_transparent && self.opaque_batches.is_empty())
|| (draw_data.draw_transparent && self.transparent_batches.is_empty())
{
return;
}

Expand Down Expand Up @@ -232,45 +239,50 @@ impl Drawer<{ BindGroupCount::Two }, { ColorAttachmentCount::One }, { DepthAttac

pass.set_bind_group(2, &self.bind_group, &[]);

#[cfg(feature = "debug")]
let opaque_pipeline = if draw_data.show_wireframe {
&self.wireframe_pipeline
} else {
&self.opaque_pipeline
};
#[cfg(not(feature = "debug"))]
let opaque_pipeline = &self.opaque_pipeline;

pass.set_pipeline(opaque_pipeline);

process_batches(
pass,
&self.opaque_batches,
&draw_data,
&self.instance_index_vertex_buffer,
&self.command_buffer,
self.multi_draw_indirect_support,
);

#[cfg(feature = "debug")]
let transparent_pipeline = if draw_data.show_wireframe {
&self.wireframe_pipeline
} else {
&self.transparent_pipeline
};
#[cfg(not(feature = "debug"))]
let transparent_pipeline = &self.transparent_pipeline;

pass.set_pipeline(transparent_pipeline);

process_batches(
pass,
&self.transparent_batches,
&draw_data,
&self.instance_index_vertex_buffer,
&self.command_buffer,
self.multi_draw_indirect_support,
);
match draw_data.draw_transparent {
false => {
#[cfg(feature = "debug")]
let opaque_pipeline = if draw_data.batch_data.show_wireframe {
&self.wireframe_pipeline
} else {
&self.opaque_pipeline
};
#[cfg(not(feature = "debug"))]
let opaque_pipeline = &self.opaque_pipeline;

pass.set_pipeline(opaque_pipeline);

process_batches(
pass,
&self.opaque_batches,
&draw_data.batch_data,
&self.instance_index_vertex_buffer,
&self.command_buffer,
self.multi_draw_indirect_support,
);
}
true => {
#[cfg(feature = "debug")]
let transparent_pipeline = if draw_data.batch_data.show_wireframe {
&self.wireframe_pipeline
} else {
&self.transparent_pipeline
};
#[cfg(not(feature = "debug"))]
let transparent_pipeline = &self.transparent_pipeline;

pass.set_pipeline(transparent_pipeline);

process_batches(
pass,
&self.transparent_batches,
&draw_data.batch_data,
&self.instance_index_vertex_buffer,
&self.command_buffer,
self.multi_draw_indirect_support,
);
}
}
}
}

Expand Down

0 comments on commit 1077bc5

Please sign in to comment.