Skip to content

Commit

Permalink
Merge branch 'main' into meshlet-lods
Browse files Browse the repository at this point in the history
  • Loading branch information
JMS55 authored Apr 10, 2024
2 parents cf7d3a9 + 5c3ae32 commit 48026f2
Show file tree
Hide file tree
Showing 16 changed files with 380 additions and 76 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ semicolon_if_nothing_returned = "warn"

ptr_as_ptr = "warn"
ptr_cast_constness = "warn"
#TODO(rust 1.77): enable `ref_as_ptr`
# ref_as_ptr = "warn"
ref_as_ptr = "warn"

[workspace.lints.rust]
unsafe_op_in_unsafe_fn = "warn"
Expand Down
31 changes: 30 additions & 1 deletion crates/bevy_gizmos/src/primitives/dim3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::f32::consts::TAU;
use bevy_color::Color;
use bevy_math::primitives::{
BoxedPolyline3d, Capsule3d, Cone, ConicalFrustum, Cuboid, Cylinder, Line3d, Plane3d,
Polyline3d, Primitive3d, Segment3d, Sphere, Torus,
Polyline3d, Primitive3d, Segment3d, Sphere, Tetrahedron, Torus,
};
use bevy_math::{Dir3, Quat, Vec3};

Expand Down Expand Up @@ -943,3 +943,32 @@ impl<T: GizmoConfigGroup> Drop for Torus3dBuilder<'_, '_, '_, T> {
});
}
}

// tetrahedron

impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive3d<Tetrahedron> for Gizmos<'w, 's, T> {
type Output<'a> = () where Self: 'a;

fn primitive_3d(
&mut self,
primitive: Tetrahedron,
position: Vec3,
rotation: Quat,
color: impl Into<Color>,
) -> Self::Output<'_> {
if !self.enabled {
return;
}

let [a, b, c, d] = primitive
.vertices
.map(rotate_then_translate_3d(rotation, position));

let lines = [(a, b), (a, c), (a, d), (b, c), (b, d), (c, d)];

let color = color.into();
for (a, b) in lines.into_iter() {
self.line(a, b, color);
}
}
}
6 changes: 3 additions & 3 deletions crates/bevy_mikktspace/src/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
unsafe_code
)]

use std::ptr::null_mut;
use std::ptr::{self, null_mut};

use glam::Vec3;

Expand Down Expand Up @@ -830,15 +830,15 @@ unsafe fn Build4RuleGroups(
let mut neigh_indexR: i32 = 0;
let vert_index: i32 = *piTriListIn.offset((f * 3i32 + i) as isize);
let ref mut fresh2 = (*pTriInfos.offset(f as isize)).AssignedGroup[i as usize];
*fresh2 = &mut *pGroups.offset(iNrActiveGroups as isize) as *mut SGroup;
*fresh2 = ptr::from_mut(&mut *pGroups.offset(iNrActiveGroups as isize));
(*(*pTriInfos.offset(f as isize)).AssignedGroup[i as usize])
.iVertexRepresentative = vert_index;
(*(*pTriInfos.offset(f as isize)).AssignedGroup[i as usize]).bOrientPreservering =
(*pTriInfos.offset(f as isize)).iFlag & 8i32 != 0i32;
(*(*pTriInfos.offset(f as isize)).AssignedGroup[i as usize]).iNrFaces = 0i32;
let ref mut fresh3 =
(*(*pTriInfos.offset(f as isize)).AssignedGroup[i as usize]).pFaceIndices;
*fresh3 = &mut *piGroupTrianglesBuffer.offset(iOffset as isize) as *mut i32;
*fresh3 = ptr::from_mut(&mut *piGroupTrianglesBuffer.offset(iOffset as isize));
iNrActiveGroups += 1;
AddTriToGroup((*pTriInfos.offset(f as isize)).AssignedGroup[i as usize], f);
bOrPre = if (*pTriInfos.offset(f as isize)).iFlag & 8i32 != 0i32 {
Expand Down
16 changes: 8 additions & 8 deletions crates/bevy_pbr/src/deferred/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,10 @@ impl SpecializedRenderPipeline for DeferredLightingLayout {
key.intersection(MeshPipelineKey::SHADOW_FILTER_METHOD_RESERVED_BITS);
if shadow_filter_method == MeshPipelineKey::SHADOW_FILTER_METHOD_HARDWARE_2X2 {
shader_defs.push("SHADOW_FILTER_METHOD_HARDWARE_2X2".into());
} else if shadow_filter_method == MeshPipelineKey::SHADOW_FILTER_METHOD_CASTANO_13 {
shader_defs.push("SHADOW_FILTER_METHOD_CASTANO_13".into());
} else if shadow_filter_method == MeshPipelineKey::SHADOW_FILTER_METHOD_JIMENEZ_14 {
shader_defs.push("SHADOW_FILTER_METHOD_JIMENEZ_14".into());
} else if shadow_filter_method == MeshPipelineKey::SHADOW_FILTER_METHOD_GAUSSIAN {
shader_defs.push("SHADOW_FILTER_METHOD_GAUSSIAN".into());
} else if shadow_filter_method == MeshPipelineKey::SHADOW_FILTER_METHOD_TEMPORAL {
shader_defs.push("SHADOW_FILTER_METHOD_TEMPORAL".into());
}

#[cfg(all(feature = "webgl", target_arch = "wasm32", not(feature = "webgpu")))]
Expand Down Expand Up @@ -489,11 +489,11 @@ pub fn prepare_deferred_lighting_pipelines(
ShadowFilteringMethod::Hardware2x2 => {
view_key |= MeshPipelineKey::SHADOW_FILTER_METHOD_HARDWARE_2X2;
}
ShadowFilteringMethod::Castano13 => {
view_key |= MeshPipelineKey::SHADOW_FILTER_METHOD_CASTANO_13;
ShadowFilteringMethod::Gaussian => {
view_key |= MeshPipelineKey::SHADOW_FILTER_METHOD_GAUSSIAN;
}
ShadowFilteringMethod::Jimenez14 => {
view_key |= MeshPipelineKey::SHADOW_FILTER_METHOD_JIMENEZ_14;
ShadowFilteringMethod::Temporal => {
view_key |= MeshPipelineKey::SHADOW_FILTER_METHOD_TEMPORAL;
}
}

Expand Down
25 changes: 16 additions & 9 deletions crates/bevy_pbr/src/light/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,29 +460,36 @@ pub struct TransmittedShadowReceiver;
///
/// The different modes use different approaches to
/// [Percentage Closer Filtering](https://developer.nvidia.com/gpugems/gpugems/part-ii-lighting-and-shadows/chapter-11-shadow-map-antialiasing).
///
/// Currently does not affect point lights.
#[derive(Component, ExtractComponent, Reflect, Clone, Copy, PartialEq, Eq, Default)]
#[reflect(Component, Default)]
pub enum ShadowFilteringMethod {
/// Hardware 2x2.
///
/// Fast but poor quality.
Hardware2x2,
/// Method by Ignacio Castaño for The Witness using 9 samples and smart
/// filtering to achieve the same as a regular 5x5 filter kernel.
/// Approximates a fixed Gaussian blur, good when TAA isn't in use.
///
/// Good quality, good performance.
///
/// For directional and spot lights, this uses a [method by Ignacio Castaño
/// for *The Witness*] using 9 samples and smart filtering to achieve the same
/// as a regular 5x5 filter kernel.
///
/// [method by Ignacio Castaño for *The Witness*]: https://web.archive.org/web/20230210095515/http://the-witness.net/news/2013/09/shadow-mapping-summary-part-1/
#[default]
Castano13,
/// Method by Jorge Jimenez for Call of Duty: Advanced Warfare using 8
/// samples in spiral pattern, randomly-rotated by interleaved gradient
/// noise with spatial variation.
Gaussian,
/// A randomized filter that varies over time, good when TAA is in use.
///
/// Good quality when used with
/// [`TemporalAntiAliasSettings`](bevy_core_pipeline::experimental::taa::TemporalAntiAliasSettings)
/// and good performance.
Jimenez14,
///
/// For directional and spot lights, this uses a [method by Jorge Jimenez for
/// *Call of Duty: Advanced Warfare*] using 8 samples in spiral pattern,
/// randomly-rotated by interleaved gradient noise with spatial variation.
///
/// [method by Jorge Jimenez for *Call of Duty: Advanced Warfare*]: https://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare/
Temporal,
}

#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/light/point_light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ impl Default for PointLight {
}

impl PointLight {
pub const DEFAULT_SHADOW_DEPTH_BIAS: f32 = 0.02;
pub const DEFAULT_SHADOW_DEPTH_BIAS: f32 = 0.08;
pub const DEFAULT_SHADOW_NORMAL_BIAS: f32 = 0.6;
}
8 changes: 4 additions & 4 deletions crates/bevy_pbr/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,11 +618,11 @@ pub fn queue_material_meshes<M: Material>(
ShadowFilteringMethod::Hardware2x2 => {
view_key |= MeshPipelineKey::SHADOW_FILTER_METHOD_HARDWARE_2X2;
}
ShadowFilteringMethod::Castano13 => {
view_key |= MeshPipelineKey::SHADOW_FILTER_METHOD_CASTANO_13;
ShadowFilteringMethod::Gaussian => {
view_key |= MeshPipelineKey::SHADOW_FILTER_METHOD_GAUSSIAN;
}
ShadowFilteringMethod::Jimenez14 => {
view_key |= MeshPipelineKey::SHADOW_FILTER_METHOD_JIMENEZ_14;
ShadowFilteringMethod::Temporal => {
view_key |= MeshPipelineKey::SHADOW_FILTER_METHOD_TEMPORAL;
}
}

Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_pbr/src/meshlet/material_draw_prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ pub fn prepare_material_meshlet_meshes_main_opaque_pass<M: Material>(
ShadowFilteringMethod::Hardware2x2 => {
view_key |= MeshPipelineKey::SHADOW_FILTER_METHOD_HARDWARE_2X2;
}
ShadowFilteringMethod::Castano13 => {
view_key |= MeshPipelineKey::SHADOW_FILTER_METHOD_CASTANO_13;
ShadowFilteringMethod::Gaussian => {
view_key |= MeshPipelineKey::SHADOW_FILTER_METHOD_GAUSSIAN;
}
ShadowFilteringMethod::Jimenez14 => {
view_key |= MeshPipelineKey::SHADOW_FILTER_METHOD_JIMENEZ_14;
ShadowFilteringMethod::Temporal => {
view_key |= MeshPipelineKey::SHADOW_FILTER_METHOD_TEMPORAL;
}
}

Expand Down
62 changes: 61 additions & 1 deletion crates/bevy_pbr/src/pbr_material.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy_asset::Asset;
use bevy_color::Alpha;
use bevy_math::{Affine2, Mat3, Vec4};
use bevy_math::{Affine2, Affine3, Mat2, Mat3, Vec2, Vec3, Vec4};
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_render::{
mesh::MeshVertexBufferLayoutRef, render_asset::RenderAssets, render_resource::*,
Expand Down Expand Up @@ -487,6 +487,66 @@ pub struct StandardMaterial {
pub uv_transform: Affine2,
}

impl StandardMaterial {
/// Horizontal flipping transform
///
/// Multiplying this with another Affine2 returns transformation with horizontally flipped texture coords
pub const FLIP_HORIZONTAL: Affine2 = Affine2 {
matrix2: Mat2::from_cols(Vec2::new(-1.0, 0.0), Vec2::Y),
translation: Vec2::X,
};

/// Vertical flipping transform
///
/// Multiplying this with another Affine2 returns transformation with vertically flipped texture coords
pub const FLIP_VERTICAL: Affine2 = Affine2 {
matrix2: Mat2::from_cols(Vec2::X, Vec2::new(0.0, -1.0)),
translation: Vec2::Y,
};

/// Flipping X 3D transform
///
/// Multiplying this with another Affine3 returns transformation with flipped X coords
pub const FLIP_X: Affine3 = Affine3 {
matrix3: Mat3::from_cols(Vec3::new(-1.0, 0.0, 0.0), Vec3::Y, Vec3::Z),
translation: Vec3::X,
};

/// Flipping Y 3D transform
///
/// Multiplying this with another Affine3 returns transformation with flipped Y coords
pub const FLIP_Y: Affine3 = Affine3 {
matrix3: Mat3::from_cols(Vec3::X, Vec3::new(0.0, -1.0, 0.0), Vec3::Z),
translation: Vec3::Y,
};

/// Flipping Z 3D transform
///
/// Multiplying this with another Affine3 returns transformation with flipped Z coords
pub const FLIP_Z: Affine3 = Affine3 {
matrix3: Mat3::from_cols(Vec3::X, Vec3::Y, Vec3::new(0.0, 0.0, -1.0)),
translation: Vec3::Z,
};

/// Flip the texture coordinates of the material.
pub fn flip(&mut self, horizontal: bool, vertical: bool) {
if horizontal {
// Multiplication of `Affine2` is order dependent, which is why
// we do not use the `*=` operator.
self.uv_transform = Self::FLIP_HORIZONTAL * self.uv_transform;
}
if vertical {
self.uv_transform = Self::FLIP_VERTICAL * self.uv_transform;
}
}

/// Consumes the material and returns a material with flipped texture coordinates
pub fn flipped(mut self, horizontal: bool, vertical: bool) -> Self {
self.flip(horizontal, vertical);
self
}
}

impl Default for StandardMaterial {
fn default() -> Self {
StandardMaterial {
Expand Down
12 changes: 6 additions & 6 deletions crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1068,8 +1068,8 @@ bitflags::bitflags! {
const TONEMAP_METHOD_BLENDER_FILMIC = 7 << Self::TONEMAP_METHOD_SHIFT_BITS;
const SHADOW_FILTER_METHOD_RESERVED_BITS = Self::SHADOW_FILTER_METHOD_MASK_BITS << Self::SHADOW_FILTER_METHOD_SHIFT_BITS;
const SHADOW_FILTER_METHOD_HARDWARE_2X2 = 0 << Self::SHADOW_FILTER_METHOD_SHIFT_BITS;
const SHADOW_FILTER_METHOD_CASTANO_13 = 1 << Self::SHADOW_FILTER_METHOD_SHIFT_BITS;
const SHADOW_FILTER_METHOD_JIMENEZ_14 = 2 << Self::SHADOW_FILTER_METHOD_SHIFT_BITS;
const SHADOW_FILTER_METHOD_GAUSSIAN = 1 << Self::SHADOW_FILTER_METHOD_SHIFT_BITS;
const SHADOW_FILTER_METHOD_TEMPORAL = 2 << Self::SHADOW_FILTER_METHOD_SHIFT_BITS;
const VIEW_PROJECTION_RESERVED_BITS = Self::VIEW_PROJECTION_MASK_BITS << Self::VIEW_PROJECTION_SHIFT_BITS;
const VIEW_PROJECTION_NONSTANDARD = 0 << Self::VIEW_PROJECTION_SHIFT_BITS;
const VIEW_PROJECTION_PERSPECTIVE = 1 << Self::VIEW_PROJECTION_SHIFT_BITS;
Expand Down Expand Up @@ -1397,10 +1397,10 @@ impl SpecializedMeshPipeline for MeshPipeline {
key.intersection(MeshPipelineKey::SHADOW_FILTER_METHOD_RESERVED_BITS);
if shadow_filter_method == MeshPipelineKey::SHADOW_FILTER_METHOD_HARDWARE_2X2 {
shader_defs.push("SHADOW_FILTER_METHOD_HARDWARE_2X2".into());
} else if shadow_filter_method == MeshPipelineKey::SHADOW_FILTER_METHOD_CASTANO_13 {
shader_defs.push("SHADOW_FILTER_METHOD_CASTANO_13".into());
} else if shadow_filter_method == MeshPipelineKey::SHADOW_FILTER_METHOD_JIMENEZ_14 {
shader_defs.push("SHADOW_FILTER_METHOD_JIMENEZ_14".into());
} else if shadow_filter_method == MeshPipelineKey::SHADOW_FILTER_METHOD_GAUSSIAN {
shader_defs.push("SHADOW_FILTER_METHOD_GAUSSIAN".into());
} else if shadow_filter_method == MeshPipelineKey::SHADOW_FILTER_METHOD_TEMPORAL {
shader_defs.push("SHADOW_FILTER_METHOD_TEMPORAL".into());
}

let blur_quality =
Expand Down
Loading

0 comments on commit 48026f2

Please sign in to comment.