Skip to content

Commit

Permalink
Use shader_def for oit resolve layer count (#15747)
Browse files Browse the repository at this point in the history
# Objective

- Size is currently hardcoded in the shader which means it will break if
a user uses anything higher than that.

## Solution

- Use a shader_def to define the size

## Testing

Tested with the OIT example
  • Loading branch information
IceSentry authored Oct 8, 2024
1 parent 675f8ad commit dec52a0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
24 changes: 19 additions & 5 deletions crates/bevy_core_pipeline/src/oit/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use bevy_render::{
BindGroup, BindGroupEntries, BindGroupLayout, BindGroupLayoutEntries, BlendComponent,
BlendState, CachedRenderPipelineId, ColorTargetState, ColorWrites, DownlevelFlags,
FragmentState, MultisampleState, PipelineCache, PrimitiveState, RenderPipelineDescriptor,
Shader, ShaderStages, TextureFormat,
Shader, ShaderDefVal, ShaderStages, TextureFormat,
},
renderer::{RenderAdapter, RenderDevice},
texture::BevyDefault,
Expand Down Expand Up @@ -121,22 +121,33 @@ pub struct OitResolvePipelineId(pub CachedRenderPipelineId);
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct OitResolvePipelineKey {
hdr: bool,
layer_count: u8,
}

#[allow(clippy::too_many_arguments)]
pub fn queue_oit_resolve_pipeline(
mut commands: Commands,
pipeline_cache: Res<PipelineCache>,
resolve_pipeline: Res<OitResolvePipeline>,
views: Query<(Entity, &ExtractedView), With<OrderIndependentTransparencySettings>>,
views: Query<
(
Entity,
&ExtractedView,
&OrderIndependentTransparencySettings,
),
With<OrderIndependentTransparencySettings>,
>,
// Store the key with the id to make the clean up logic easier.
// This also means it will always replace the entry if the key changes so nothing to clean up.
mut cached_pipeline_id: Local<EntityHashMap<(OitResolvePipelineKey, CachedRenderPipelineId)>>,
) {
let mut current_view_entities = EntityHashSet::default();
for (e, view) in &views {
for (e, view, oit_settings) in &views {
current_view_entities.insert(e);
let key = OitResolvePipelineKey { hdr: view.hdr };
let key = OitResolvePipelineKey {
hdr: view.hdr,
layer_count: oit_settings.layer_count,
};

if let Some((cached_key, id)) = cached_pipeline_id.get(&e) {
if *cached_key == key {
Expand Down Expand Up @@ -179,7 +190,10 @@ fn specialize_oit_resolve_pipeline(
fragment: Some(FragmentState {
entry_point: "fragment".into(),
shader: OIT_RESOLVE_SHADER_HANDLE,
shader_defs: vec![],
shader_defs: vec![ShaderDefVal::UInt(
"LAYER_COUNT".into(),
key.layer_count as u32,
)],
targets: vec![Some(ColorTargetState {
format,
blend: Some(BlendState {
Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_core_pipeline/src/oit/resolve/oit_resolve.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ struct OitFragment {
depth: f32,
}
// Contains all the colors and depth for this specific fragment
// TODO don't hardcode size
var<private> fragment_list: array<OitFragment, 32>;
var<private> fragment_list: array<OitFragment, #{LAYER_COUNT}>;

struct FullscreenVertexOutput {
@builtin(position) position: vec4<f32>,
Expand Down

0 comments on commit dec52a0

Please sign in to comment.