diff --git a/src/render/light/mod.rs b/src/render/light/mod.rs index 8f084d1..67de61c 100644 --- a/src/render/light/mod.rs +++ b/src/render/light/mod.rs @@ -16,8 +16,12 @@ pub struct LightBuffer { _phantom: PhantomData, /// The max amount of light casters that could fit in this buffer. pub max_count: usize, - /// The current amount of light casters in this buffer. - pub current_count: usize, + /// The amount of light casters that are taking up space in the buffer. + /// + /// This means that a light may be inactive in the buffer, by being replaced + /// with a default caster as to not affect lighting. Its easier this way than + /// to recreate the array and remove the gaps. + pub buffer_count: usize, /// The buffer index for a specific entity/caster. used_indexes: HashMap, /// Indexes that were being used but are no longer needed. @@ -29,7 +33,7 @@ impl LightBuffer { Self { _phantom: PhantomData::default(), max_count, - current_count: 0, + buffer_count: 0, used_indexes: HashMap::new(), dead_indexes: VecDeque::new(), } @@ -52,8 +56,13 @@ impl LightBuffer { let buffer_idx = match self.dead_indexes.pop_front() { Some(i) => i, None => { - let i = self.current_count; - self.current_count += 1; + let i = self.buffer_count; + self.buffer_count += 1; + + // If this assert triggers, you are hitting trying to exceed + // the max amount of lights + assert!(self.buffer_count <= self.max_count); + i }, }; @@ -75,7 +84,7 @@ impl LightBuffer { pub fn remove_light(&mut self, lights_buffer: &mut [U; MAX_LIGHT_COUNT], entity: edict::EntityId) -> bool { if let Some(removed_idx) = self.used_indexes.remove(&entity) { self.dead_indexes.push_back(removed_idx); - self.current_count -= 1; + //self.current_count -= 1; lights_buffer[removed_idx] = U::default(); true @@ -99,7 +108,7 @@ impl LightUniformBuffers { &wgpu::BufferDescriptor { label: Some("Lights Uniform buffer"), usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, - size: mem::size_of::() as u64, + size: (mem::size_of::() * MAX_LIGHT_COUNT) as u64, mapped_at_creation: false, } ); @@ -129,7 +138,7 @@ impl LightUniformBuffers { wgpu::BufferBinding { buffer: &buffer, offset: 0, - size: Some(NonZeroU64::new(mem::size_of::() as u64).unwrap()) + size: None, // use the full buffer } ) } @@ -158,7 +167,7 @@ impl LightUniformBuffers { } } - self.lights_uniform.point_light_count = self.point_lights.current_count as u32; + self.lights_uniform.point_light_count = self.point_lights.buffer_count as u32; queue.write_buffer(&self.buffer, 0, bytemuck::cast_slice(&[self.lights_uniform])); } }