Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind point light counts #25

Merged
merged 3 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## Changed
### Changed

- Point lights colours are now added to ambient light, instead of multiplied by it (#24).

## Migration guide
### Fixed

- Point lights rendering despite being despawned (#25).

### Migration guide

- Point light intensity needs to be adjusted to account for changes to ambient light. Generally this means point light intensity values need to be lowered. See the relevant changes to the `dungeon` example.

Expand Down
6 changes: 4 additions & 2 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use crate::{
ExtractedAmbientLight2d, ExtractedLightOccluder2d, ExtractedPointLight2d,
},
light_map::{
prepare_light_map_texture, LightMapNode, LightMapPass, LightMapPipeline,
LIGHT_MAP_SHADER,
prepare_light_map_texture, prepare_point_light_count, LightMapNode, LightMapPass,
LightMapPipeline, PointLightMetaBuffer, LIGHT_MAP_SHADER,
},
lighting::{
prepare_lighting_pipelines, LightingNode, LightingPass, LightingPipeline,
Expand Down Expand Up @@ -83,6 +83,7 @@ impl Plugin for Light2dPlugin {

render_app
.init_resource::<SpecializedRenderPipelines<LightingPipeline>>()
.init_resource::<PointLightMetaBuffer>()
.add_systems(
ExtractSchedule,
(
Expand All @@ -95,6 +96,7 @@ impl Plugin for Light2dPlugin {
Render,
(
prepare_lighting_pipelines.in_set(RenderSet::Prepare),
prepare_point_light_count.in_set(RenderSet::Prepare),
prepare_sdf_texture
.after(prepare_view_targets)
.in_set(RenderSet::ManageViews),
Expand Down
17 changes: 6 additions & 11 deletions src/render/light_map/light_map.wgsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#import bevy_core_pipeline::fullscreen_vertex_shader::FullscreenVertexOutput
#import bevy_render::view::View
#import bevy_light_2d::types::{AmbientLight2d, PointLight2d};
#import bevy_light_2d::types::{AmbientLight2d, PointLight2d, PointLightMeta};
#import bevy_light_2d::view_transformations::{
frag_coord_to_ndc,
ndc_to_world,
Expand Down Expand Up @@ -30,9 +30,12 @@ var<uniform> ambient_light: AmbientLight2d;
#endif

@group(0) @binding(3)
var sdf: texture_2d<f32>;
var<uniform> point_light_meta: PointLightMeta;

@group(0) @binding(4)
var sdf: texture_2d<f32>;

@group(0) @binding(5)
var sdf_sampler: sampler;

@fragment
Expand All @@ -45,15 +48,7 @@ fn fragment(in: FullscreenVertexOutput) -> @location(0) vec4<f32> {

var lighting_color = ambient_light.color.rgb;

// WebGL2 does not support storage buffers (or runtime sized arrays), so we
// need to use a fixed number of point lights.
#if AVAILABLE_STORAGE_BUFFER_BINDINGS >= 6
let point_light_count = arrayLength(&point_lights);
#else
let point_light_count = MAX_POINT_LIGHTS;
#endif

for (var i = 0u; i < point_light_count; i++) {
for (var i = 0u; i < point_light_meta.count; i++) {
let light = point_lights[i];
let dist = distance(light.center, pos);

Expand Down
32 changes: 29 additions & 3 deletions src/render/light_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ mod prepare;

use bevy::{
asset::Handle,
ecs::component::Component,
render::{render_graph::RenderLabel, render_resource::Shader, texture::CachedTexture},
ecs::{component::Component, system::Resource},
math::Vec3,
render::{
render_graph::RenderLabel,
render_resource::{Shader, ShaderType, UniformBuffer},
texture::CachedTexture,
},
};

pub use node::LightMapNode;
pub use pipeline::LightMapPipeline;
pub use prepare::prepare_light_map_texture;
pub use prepare::{prepare_light_map_texture, prepare_point_light_count};

pub const LIGHT_MAP_SHADER: Handle<Shader> =
Handle::weak_from_u128(320609826414128764415270070474935914193);
Expand All @@ -22,3 +27,24 @@ pub struct LightMapPass;
pub struct LightMapTexture {
pub light_map: CachedTexture,
}

#[derive(Resource, Default)]
pub struct PointLightMetaBuffer {
pub buffer: UniformBuffer<PointLightMeta>,
}

#[derive(Default, ShaderType)]
pub struct PointLightMeta {
pub count: u32,
// WebGL2 structs must be 16 byte aligned.
_padding: Vec3,
}

impl PointLightMeta {
pub fn new(count: u32) -> Self {
Self {
count,
_padding: Vec3::ZERO,
}
}
}
5 changes: 4 additions & 1 deletion src/render/light_map/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use smallvec::{smallvec, SmallVec};
use crate::render::extract::{ExtractedAmbientLight2d, ExtractedPointLight2d};
use crate::render::sdf::SdfTexture;

use super::{LightMapPipeline, LightMapTexture};
use super::{LightMapPipeline, LightMapTexture, PointLightMetaBuffer};

const LIGHT_MAP_PASS: &str = "light_map_pass";
const LIGHT_MAP_BIND_GROUP: &str = "light_map_bind_group";
Expand Down Expand Up @@ -48,6 +48,7 @@ impl ViewNode for LightMapNode {
Some(view_uniform_binding),
Some(ambient_light_uniform),
Some(point_light_binding),
Some(point_light_count_binding),
) = (
pipeline_cache.get_render_pipeline(light_map_pipeline.pipeline_id),
world.resource::<ViewUniforms>().uniforms.binding(),
Expand All @@ -58,6 +59,7 @@ impl ViewNode for LightMapNode {
world
.resource::<GpuArrayBuffer<ExtractedPointLight2d>>()
.binding(),
world.resource::<PointLightMetaBuffer>().buffer.binding(),
)
else {
return Ok(());
Expand All @@ -70,6 +72,7 @@ impl ViewNode for LightMapNode {
view_uniform_binding.clone(),
ambient_light_uniform.clone(),
point_light_binding.clone(),
point_light_count_binding.clone(),
&sdf_texture.sdf.default_view,
&light_map_pipeline.sdf_sampler,
)),
Expand Down
3 changes: 2 additions & 1 deletion src/render/light_map/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use bevy::render::view::ViewUniform;

use crate::render::extract::{ExtractedAmbientLight2d, ExtractedPointLight2d};

use super::LIGHT_MAP_SHADER;
use super::{PointLightMeta, LIGHT_MAP_SHADER};

const LIGHT_MAP_BIND_GROUP_LAYOUT: &str = "light_map_group_layout";
const LIGHT_MAP_PIPELINE: &str = "light_map_pipeline";
Expand All @@ -37,6 +37,7 @@ impl FromWorld for LightMapPipeline {
uniform_buffer::<ViewUniform>(true),
uniform_buffer::<ExtractedAmbientLight2d>(true),
GpuArrayBuffer::<ExtractedPointLight2d>::binding_layout(render_device),
uniform_buffer::<PointLightMeta>(false),
texture_2d(TextureSampleType::Float { filterable: true }),
sampler(SamplerBindingType::Filtering),
),
Expand Down
19 changes: 17 additions & 2 deletions src/render/light_map/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ use bevy::{
},
render::{
render_resource::{TextureDescriptor, TextureDimension, TextureFormat, TextureUsages},
renderer::RenderDevice,
renderer::{RenderDevice, RenderQueue},
texture::TextureCache,
view::ViewTarget,
},
};

use super::LightMapTexture;
use crate::render::extract::ExtractedPointLight2d;

use super::{LightMapTexture, PointLightMeta, PointLightMetaBuffer};

const LIGHT_MAP_TEXTURE: &str = "light_map_texture";

Expand Down Expand Up @@ -41,3 +43,16 @@ pub fn prepare_light_map_texture(
});
}
}

pub fn prepare_point_light_count(
render_device: Res<RenderDevice>,
render_queue: Res<RenderQueue>,
point_lights: Query<&ExtractedPointLight2d>,
mut point_light_count: ResMut<PointLightMetaBuffer>,
) {
let meta = PointLightMeta::new(point_lights.iter().len() as u32);
point_light_count.buffer.set(meta);
point_light_count
.buffer
.write_buffer(&render_device, &render_queue);
}
6 changes: 6 additions & 0 deletions src/render/types.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ struct PointLight2d {
falloff: f32,
cast_shadows: u32
}

struct PointLightMeta {
count: u32,
// WebGL2 structs must be 16 byte aligned.
_padding: vec3<u32>
}
Loading