Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7141282
Use a more efficient representation for alpha modes in material flags
coreh Nov 16, 2022
bbb3420
Add `Premultiplied`, `Add` and `Multiply` blend modes
coreh Nov 16, 2022
e2a0c46
Add blend modes example
coreh Nov 16, 2022
e1b54dd
Add documentation for new blend modes
coreh Nov 16, 2022
63b7844
Fix clippy issue
coreh Nov 16, 2022
4efa604
Update examples/README.md
coreh Nov 16, 2022
0562f46
Fix clippy issues
coreh Nov 16, 2022
92ca5bd
Hide `premultiply_alpha()` behind a shader def
coreh Nov 17, 2022
b34341f
Separate multiply in its own shader def and document `premultiply_alp…
coreh Nov 17, 2022
fc33691
Add blend mode labels to example
coreh Nov 17, 2022
295d8d2
Add HDR toggle to blend modes example
coreh Nov 17, 2022
4187b15
Fix clippy issue
coreh Nov 17, 2022
c8f914c
Improve blend mode example
coreh Nov 17, 2022
95aad7e
Clarify meaning of alpha mode standard material flags
coreh Nov 17, 2022
2c9e22a
Clarify meaning of blend state bits in `MeshPipelineKey`
coreh Nov 17, 2022
7d9989e
Rename `x_BITS` to `x_RESERVED_BITS` for consistency/clarity
coreh Nov 17, 2022
a3810fd
Fix clippy issue
coreh Nov 17, 2022
d538109
Make sure HDR works for Blend Modes example in WASM
coreh Nov 21, 2022
fe052e7
Merge branch 'main' into blend-modes
coreh Nov 24, 2022
b2196b1
Merge branch 'main' into blend-modes
coreh Jan 15, 2023
e035576
Make wording of comment less confusing
coreh Jan 15, 2023
071a4c6
Add missing else statement
coreh Jan 15, 2023
1ee663a
Merge branch 'main' into blend-modes
coreh Jan 21, 2023
54591c8
Account for new blend modes when queueing meshes for prepass
coreh Jan 21, 2023
28bfb45
Move blend modes to upper bits, keeping “normal” flags sequential
coreh Jan 21, 2023
5a14a7e
Fix typo
coreh Jan 21, 2023
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
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub enum AlphaMode {
///
/// Useful for effects like holograms, ghosts, lasers and other energy beams.
Add,
/// Combines the color of the fragments with the colors behind them in an
/// Combines the color of the fragments with the colors behind them in a
/// multiplicative process, (i.e. like pigments) producing darker results.
///
/// White produces no effect. Alpha values can be used to modulate the result.
Expand Down
23 changes: 14 additions & 9 deletions crates/bevy_pbr/src/pbr_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,20 +298,25 @@ bitflags::bitflags! {
const OCCLUSION_TEXTURE = (1 << 3);
const DOUBLE_SIDED = (1 << 4);
const UNLIT = (1 << 5);
const ALPHA_MODE_RESERVED_BITS = (0b111 << 6); // ← Bitmask reserving three bits for the `AlphaMode`
const ALPHA_MODE_OPAQUE = (0 << 6); // ← Values are just sequential values bitshifted into
const ALPHA_MODE_MASK = (1 << 6); // the bitmask, and can range from 0 to 7.
const ALPHA_MODE_BLEND = (2 << 6); //
const ALPHA_MODE_PREMULTIPLIED = (3 << 6); //
const ALPHA_MODE_ADD = (4 << 6); // Right now only values 0–5 are used, which still gives
const ALPHA_MODE_MULTIPLY = (5 << 6); // ← us "room" for two more modes without adding more bits
const TWO_COMPONENT_NORMAL_MAP = (1 << 9);
const FLIP_NORMAL_MAP_Y = (1 << 10);
const TWO_COMPONENT_NORMAL_MAP = (1 << 6);
const FLIP_NORMAL_MAP_Y = (1 << 7);
const ALPHA_MODE_RESERVED_BITS = (Self::ALPHA_MODE_MASK_BITS << Self::ALPHA_MODE_SHIFT_BITS); // ← Bitmask reserving bits for the `AlphaMode`
const ALPHA_MODE_OPAQUE = (0 << Self::ALPHA_MODE_SHIFT_BITS); // ← Values are just sequential values bitshifted into
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 << n won’t work.

const ALPHA_MODE_MASK = (1 << Self::ALPHA_MODE_SHIFT_BITS); // the bitmask, and can range from 0 to 7.
const ALPHA_MODE_BLEND = (2 << Self::ALPHA_MODE_SHIFT_BITS); //
const ALPHA_MODE_PREMULTIPLIED = (3 << Self::ALPHA_MODE_SHIFT_BITS); //
const ALPHA_MODE_ADD = (4 << Self::ALPHA_MODE_SHIFT_BITS); // Right now only values 0–5 are used, which still gives
const ALPHA_MODE_MULTIPLY = (5 << Self::ALPHA_MODE_SHIFT_BITS); // ← us "room" for two more modes without adding more bits
const NONE = 0;
const UNINITIALIZED = 0xFFFF;
}
}

impl StandardMaterialFlags {
const ALPHA_MODE_MASK_BITS: u32 = 0b111;
const ALPHA_MODE_SHIFT_BITS: u32 = 32 - Self::ALPHA_MODE_MASK_BITS.count_ones();
}

/// The GPU representation of the uniform data of a [`StandardMaterial`].
#[derive(Clone, Default, ShaderType)]
pub struct StandardMaterialUniform {
Expand Down
10 changes: 8 additions & 2 deletions crates/bevy_pbr/src/prepass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,10 @@ pub fn queue_prepass_material_meshes<M: Material>(
match alpha_mode {
AlphaMode::Opaque => {}
AlphaMode::Mask(_) => mesh_key |= MeshPipelineKey::ALPHA_MASK,
AlphaMode::Blend => continue,
AlphaMode::Blend
| AlphaMode::Premultiplied
| AlphaMode::Add
| AlphaMode::Multiply => continue,
}

let pipeline_id = pipelines.specialize(
Expand Down Expand Up @@ -566,7 +569,10 @@ pub fn queue_prepass_material_meshes<M: Material>(
distance,
});
}
AlphaMode::Blend => {}
AlphaMode::Blend
| AlphaMode::Premultiplied
| AlphaMode::Add
| AlphaMode::Multiply => {}
}
}
}
Expand Down
23 changes: 15 additions & 8 deletions crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,13 +557,16 @@ bitflags::bitflags! {
/// MSAA uses the highest 3 bits for the MSAA log2(sample count) to support up to 128x MSAA.
pub struct MeshPipelineKey: u32 {
const NONE = 0;
const BLEND_RESERVED_BITS = 0b11; // ← Bitmask reserving two bits for the blend state
const BLEND_OPAQUE = 0; // ← Values are just sequential within the mask, and can range from 0 to 3
const BLEND_PREMULTIPLIED_ALPHA = 1; //
const BLEND_MULTIPLY = 2; // ← We still have room for one more value without adding more bits
const HDR = (1 << 2);
const TONEMAP_IN_SHADER = (1 << 3);
const DEBAND_DITHER = (1 << 4);
const HDR = (1 << 0);
const TONEMAP_IN_SHADER = (1 << 1);
const DEBAND_DITHER = (1 << 2);
const DEPTH_PREPASS = (1 << 3);
const NORMAL_PREPASS = (1 << 4);
const ALPHA_MASK = (1 << 5);
const BLEND_RESERVED_BITS = Self::BLEND_MASK_BITS << Self::BLEND_SHIFT_BITS; // ← Bitmask reserving bits for the blend state
const BLEND_OPAQUE = (0 << Self::BLEND_SHIFT_BITS); // ← Values are just sequential within the mask, and can range from 0 to 3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

const BLEND_PREMULTIPLIED_ALPHA = (1 << Self::BLEND_SHIFT_BITS); //
const BLEND_MULTIPLY = (2 << Self::BLEND_SHIFT_BITS); // ← We still have room for one more value without adding more bits
const MSAA_RESERVED_BITS = Self::MSAA_MASK_BITS << Self::MSAA_SHIFT_BITS;
const PRIMITIVE_TOPOLOGY_RESERVED_BITS = Self::PRIMITIVE_TOPOLOGY_MASK_BITS << Self::PRIMITIVE_TOPOLOGY_SHIFT_BITS;
}
Expand All @@ -573,7 +576,11 @@ impl MeshPipelineKey {
const MSAA_MASK_BITS: u32 = 0b111;
const MSAA_SHIFT_BITS: u32 = 32 - Self::MSAA_MASK_BITS.count_ones();
const PRIMITIVE_TOPOLOGY_MASK_BITS: u32 = 0b111;
const PRIMITIVE_TOPOLOGY_SHIFT_BITS: u32 = Self::MSAA_SHIFT_BITS - 3;
const PRIMITIVE_TOPOLOGY_SHIFT_BITS: u32 =
Self::MSAA_SHIFT_BITS - Self::PRIMITIVE_TOPOLOGY_MASK_BITS.count_ones();
const BLEND_MASK_BITS: u32 = 0b11;
const BLEND_SHIFT_BITS: u32 =
Self::PRIMITIVE_TOPOLOGY_SHIFT_BITS - Self::BLEND_MASK_BITS.count_ones();

pub fn from_msaa_samples(msaa_samples: u32) -> Self {
let msaa_bits =
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_pbr/src/render/pbr_functions.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

fn alpha_discard(material: StandardMaterial, output_color: vec4<f32>) -> vec4<f32> {
var color = output_color;
let alpha_mode = (material.flags & STANDARD_MATERIAL_FLAGS_ALPHA_MODE_RESERVED_BITS);
if (alpha_mode == STANDARD_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE) {
let alpha_mode = material.flags & STANDARD_MATERIAL_FLAGS_ALPHA_MODE_RESERVED_BITS;
if alpha_mode == STANDARD_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE {
// NOTE: If rendering as opaque, alpha should be ignored so set to 1.0
color.a = 1.0;
} else if (alpha_mode == STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK) {
if (color.a >= material.alpha_cutoff) {
} else if alpha_mode == STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK {
if color.a >= material.alpha_cutoff {
// NOTE: If rendering as masked alpha and >= the cutoff, render as fully opaque
color.a = 1.0;
} else {
Expand Down
20 changes: 11 additions & 9 deletions crates/bevy_pbr/src/render/pbr_types.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ let STANDARD_MATERIAL_FLAGS_METALLIC_ROUGHNESS_TEXTURE_BIT: u32 = 4u;
let STANDARD_MATERIAL_FLAGS_OCCLUSION_TEXTURE_BIT: u32 = 8u;
let STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT: u32 = 16u;
let STANDARD_MATERIAL_FLAGS_UNLIT_BIT: u32 = 32u;
let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_RESERVED_BITS: u32 = 448u; // (0b111 << 6)
let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE: u32 = 0u; // (0 << 6)
let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK: u32 = 64u; // (1 << 6)
let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_BLEND: u32 = 128u; // (2 << 6)
let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_PREMULTIPLIED: u32 = 192u; // (3 << 6)
let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_ADD: u32 = 256u; // (4 << 6)
let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MULTIPLY: u32 = 320u; // (5 << 6)
let STANDARD_MATERIAL_FLAGS_TWO_COMPONENT_NORMAL_MAP: u32 = 512u;
let STANDARD_MATERIAL_FLAGS_FLIP_NORMAL_MAP_Y: u32 = 1024u;
let STANDARD_MATERIAL_FLAGS_TWO_COMPONENT_NORMAL_MAP: u32 = 64u;
let STANDARD_MATERIAL_FLAGS_FLIP_NORMAL_MAP_Y: u32 = 128u;
let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_RESERVED_BITS: u32 = 3758096384u; // (0b111u32 << 29)
let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE: u32 = 0u; // (0u32 << 29)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 means no flags are set, you’ll have to start from 1 << 29.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh… I see. You mean that none of the 3 bits are set. Ok.

let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK: u32 = 536870912u; // (1u32 << 29)
let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_BLEND: u32 = 1073741824u; // (2u32 << 29)
let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_PREMULTIPLIED: u32 = 1610612736u; // (3u32 << 29)
let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_ADD: u32 = 2147483648u; // (4u32 << 29)
let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MULTIPLY: u32 = 2684354560u; // (5u32 << 29)
// ↑ To calculate/verify the values above, use the following playground:
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=7792f8dd6fc6a8d4d0b6b1776898a7f4

// Creates a StandardMaterial with default values
fn standard_material_new() -> StandardMaterial {
Expand Down