Skip to content

Commit

Permalink
Merge branch 'crevice-padded-size' into directional-light-and-shadow
Browse files Browse the repository at this point in the history
  • Loading branch information
superdump committed Jul 1, 2021
2 parents df6074d + 0530368 commit 6226865
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
7 changes: 7 additions & 0 deletions crates/crevice/src/std140/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ pub trait AsStd140 {
size_of::<Self::Std140Type>()
}

/// Returns the size of the `std140` version of this type, rounded up to the alignment.
/// Useful for pre-sizing buffers.
fn std140_padded_size_static() -> usize {
(Self::std140_size_static() + Self::Std140Type::ALIGNMENT - 1)
& !(Self::Std140Type::ALIGNMENT - 1)
}

/// Converts from `std140` version of self to self.
fn from_std140(val: Self::Std140Type) -> Self;
}
Expand Down
7 changes: 7 additions & 0 deletions crates/crevice/src/std430/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ pub trait AsStd430 {
size_of::<Self::Std430Type>()
}

/// Returns the size of the `std430` version of this type, rounded up to the alignment.
/// Useful for pre-sizing buffers.
fn std430_padded_size_static() -> usize {
(Self::std430_size_static() + Self::Std430Type::ALIGNMENT - 1)
& !(Self::Std430Type::ALIGNMENT - 1)
}

/// Converts from `std430` version of self to self.
fn from_std430(value: Self::Std430Type) -> Self;
}
Expand Down
6 changes: 4 additions & 2 deletions pipelined/bevy_pbr2/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ impl FromWorld for ShadowShaders {
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: true,
// TODO: change this to ViewUniform::std140_size_static once crevice fixes this!
// TODO: change this to ViewUniform::std140_padded_size_static once crevice fixes this!
// Context: https://github.com/LPGhatguy/crevice/issues/29
min_binding_size: BufferSize::new(80),
min_binding_size: BufferSize::new(
ViewUniform::std140_padded_size_static() as u64
),
},
count: None,
},
Expand Down
18 changes: 11 additions & 7 deletions pipelined/bevy_pbr2/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use bevy_render2::{
renderer::{RenderContext, RenderDevice, RenderQueue},
shader::Shader,
texture::{BevyDefault, GpuImage, Image, TextureFormatPixelInfo},
view::{ExtractedView, ViewMeta, ViewUniformOffset},
view::{ExtractedView, ViewMeta, ViewUniform, ViewUniformOffset},
};
use bevy_transform::components::GlobalTransform;
use bevy_utils::slab::{FrameSlabMap, FrameSlabMapKey};
Expand Down Expand Up @@ -53,9 +53,11 @@ impl FromWorld for PbrShaders {
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: true,
// TODO: change this to ViewUniform::std140_size_static once crevice fixes this!
// TODO: change this to ViewUniform::std140_padded_size_static once crevice fixes this!
// Context: https://github.com/LPGhatguy/crevice/issues/29
min_binding_size: BufferSize::new(80),
min_binding_size: BufferSize::new(
ViewUniform::std140_padded_size_static() as u64
),
},
count: None,
},
Expand All @@ -66,9 +68,11 @@ impl FromWorld for PbrShaders {
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: true,
// TODO: change this to ViewUniform::std140_size_static once crevice fixes this!
// TODO: change this to GpuLights::std140_padded_size_static once crevice fixes this!
// Context: https://github.com/LPGhatguy/crevice/issues/29
min_binding_size: BufferSize::new(1264),
min_binding_size: BufferSize::new(
GpuLights::std140_padded_size_static() as u64
),
},
count: None,
},
Expand Down Expand Up @@ -104,7 +108,7 @@ impl FromWorld for PbrShaders {
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: true,
min_binding_size: BufferSize::new(Mat4::std140_size_static() as u64),
min_binding_size: BufferSize::new(Mat4::std140_padded_size_static() as u64),
},
count: None,
}],
Expand All @@ -120,7 +124,7 @@ impl FromWorld for PbrShaders {
ty: BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: BufferSize::new(
StandardMaterialUniformData::std140_size_static() as u64,
StandardMaterialUniformData::std140_padded_size_static() as u64,
),
},
count: None,
Expand Down
5 changes: 2 additions & 3 deletions pipelined/bevy_render2/src/render_resource/uniform_vec.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{render_resource::Buffer, renderer::RenderDevice};
use crevice::std140::{self, AsStd140, DynamicUniform, Std140};
use crevice::std140::{self, AsStd140, DynamicUniform};
use std::{num::NonZeroU64, ops::DerefMut};
use wgpu::{BindingResource, BufferBinding, BufferDescriptor, BufferUsage, CommandEncoder};

Expand All @@ -18,8 +18,7 @@ impl<T: AsStd140> Default for UniformVec<T> {
staging_buffer: None,
uniform_buffer: None,
capacity: 0,
item_size: (T::std140_size_static() + <T as AsStd140>::Std140Type::ALIGNMENT - 1)
& !(<T as AsStd140>::Std140Type::ALIGNMENT - 1),
item_size: T::std140_padded_size_static(),
}
}
}
Expand Down

0 comments on commit 6226865

Please sign in to comment.