From 9f75d3cbbbaa752547a97ee227db325e04566e2e Mon Sep 17 00:00:00 2001 From: Daniel Norberg Date: Tue, 9 May 2023 12:54:27 +0200 Subject: [PATCH 1/2] move alpha mask flags to erlier in the material flags property On certain Adreno chipsets (such as the 619) the `StandardMaterial` flags are capped for some reason thus producing issues with how alpha is being processed. Specifically all meshes were treated by the `bevy_pbr` shader as being opaque. Moving them to below 16-bits solved the issue and since there is nothing used above bit-index 9 it should be safe for all platform/chipsets. --- crates/bevy_pbr/src/pbr_material.rs | 2 +- crates/bevy_pbr/src/render/pbr_types.wgsl | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/crates/bevy_pbr/src/pbr_material.rs b/crates/bevy_pbr/src/pbr_material.rs index 1eadd479af5af..8b855ec05800b 100644 --- a/crates/bevy_pbr/src/pbr_material.rs +++ b/crates/bevy_pbr/src/pbr_material.rs @@ -399,7 +399,7 @@ bitflags::bitflags! { impl StandardMaterialFlags { const ALPHA_MODE_MASK_BITS: u32 = 0b111; - const ALPHA_MODE_SHIFT_BITS: u32 = 32 - Self::ALPHA_MODE_MASK_BITS.count_ones(); + const ALPHA_MODE_SHIFT_BITS: u32 = 16 - Self::ALPHA_MODE_MASK_BITS.count_ones(); } /// The GPU representation of the uniform data of a [`StandardMaterial`]. diff --git a/crates/bevy_pbr/src/render/pbr_types.wgsl b/crates/bevy_pbr/src/render/pbr_types.wgsl index 85cbed505fb27..9b39ee72cc419 100644 --- a/crates/bevy_pbr/src/render/pbr_types.wgsl +++ b/crates/bevy_pbr/src/render/pbr_types.wgsl @@ -24,15 +24,18 @@ const STANDARD_MATERIAL_FLAGS_TWO_COMPONENT_NORMAL_MAP: u32 = 64u; const STANDARD_MATERIAL_FLAGS_FLIP_NORMAL_MAP_Y: u32 = 128u; const STANDARD_MATERIAL_FLAGS_FOG_ENABLED_BIT: u32 = 256u; const STANDARD_MATERIAL_FLAGS_DEPTH_MAP_BIT: u32 = 512u; -const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_RESERVED_BITS: u32 = 3758096384u; // (0b111u32 << 29) -const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE: u32 = 0u; // (0u32 << 29) -const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK: u32 = 536870912u; // (1u32 << 29) -const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_BLEND: u32 = 1073741824u; // (2u32 << 29) -const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_PREMULTIPLIED: u32 = 1610612736u; // (3u32 << 29) -const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_ADD: u32 = 2147483648u; // (4u32 << 29) -const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MULTIPLY: u32 = 2684354560u; // (5u32 << 29) +const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_RESERVED_BITS: u32 = 57344u; // (0b111u32 << 13) +const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE: u32 = 0u; // (0u32 << 13) +const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK: u32 = 8192u; // (1u32 << 13) +const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_BLEND: u32 = 16384u; // (2u32 << 13) +const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_PREMULTIPLIED: u32 = 24576u; // (3u32 << 13) +const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_ADD: u32 = 32768u; // (4u32 << 13) +const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MULTIPLY: u32 = 40960u; // (5u32 << 13) // ↑ To calculate/verify the values above, use the following playground: // https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=7792f8dd6fc6a8d4d0b6b1776898a7f4 +// NOTE: the gist is based on a shift of 29 since the blend modes used to be at +// the end of a u32. Because this created an issue with Adreno chipsets it has +// been moved to 13. // Creates a StandardMaterial with default values fn standard_material_new() -> StandardMaterial { From 1f2832c91aec28e477ed155887c272cbfd817e07 Mon Sep 17 00:00:00 2001 From: Daniel Norberg Date: Wed, 10 May 2023 16:06:11 +0200 Subject: [PATCH 2/2] add feature to limit directional lights to one This is useful when targetting certain mobile gpus (Qualcomm/Adreno) on WebGL. The shader will currently crash when set to the default 10. Possibly related to some memory issue in the gpu drivers. --- Cargo.toml | 6 +++++- crates/bevy_internal/Cargo.toml | 3 +++ crates/bevy_pbr/Cargo.toml | 1 + crates/bevy_pbr/src/render/light.rs | 3 +++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 4e25ee76b4d89..31fed13caed3f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,6 +53,7 @@ default = [ "android_shared_stdcxx", "tonemapping_luts", "default_font", + "limit_directional_light", ] # Force dynamic linking, which improves iterative compile times @@ -82,6 +83,9 @@ bevy_gltf = ["bevy_internal/bevy_gltf", "bevy_asset", "bevy_scene", "bevy_pbr"] # Adds PBR rendering bevy_pbr = ["bevy_internal/bevy_pbr", "bevy_asset", "bevy_render", "bevy_core_pipeline"] +# Limit Directional Lights in PBR +limit_directional_light = ["bevy_internal/limit_directional_light"] + # Provides rendering functionality bevy_render = ["bevy_internal/bevy_render"] @@ -241,7 +245,7 @@ bevy_internal = { path = "crates/bevy_internal", version = "0.11.0-dev", default [target.'cfg(target_arch = "wasm32")'.dependencies] bevy_internal = { path = "crates/bevy_internal", version = "0.11.0-dev", default-features = false, features = [ - "webgl", + "webgl" ] } [dev-dependencies] diff --git a/crates/bevy_internal/Cargo.toml b/crates/bevy_internal/Cargo.toml index 4c828dd848b68..e9352879f89e5 100644 --- a/crates/bevy_internal/Cargo.toml +++ b/crates/bevy_internal/Cargo.toml @@ -76,6 +76,9 @@ subpixel_glyph_atlas = ["bevy_text/subpixel_glyph_atlas"] # Optimise for WebGL2 webgl = ["bevy_core_pipeline?/webgl", "bevy_pbr?/webgl", "bevy_render?/webgl"] +# Limit directional light to 1 in PBR +limit_directional_light = ["bevy_pbr/limit_directional_light"] + # enable systems that allow for automated testing on CI bevy_ci_testing = ["bevy_app/bevy_ci_testing", "bevy_render?/ci_limits"] diff --git a/crates/bevy_pbr/Cargo.toml b/crates/bevy_pbr/Cargo.toml index 9f6cadb767553..ec525ce8341f9 100644 --- a/crates/bevy_pbr/Cargo.toml +++ b/crates/bevy_pbr/Cargo.toml @@ -10,6 +10,7 @@ keywords = ["bevy"] [features] webgl = [] +limit_directional_light = [] [dependencies] # bevy diff --git a/crates/bevy_pbr/src/render/light.rs b/crates/bevy_pbr/src/render/light.rs index 1e73198a8744d..b9e8fda1a727f 100644 --- a/crates/bevy_pbr/src/render/light.rs +++ b/crates/bevy_pbr/src/render/light.rs @@ -216,6 +216,9 @@ pub struct GpuLights { // NOTE: this must be kept in sync with the same constants in pbr.frag pub const MAX_UNIFORM_BUFFER_POINT_LIGHTS: usize = 256; +#[cfg(feature = "limit_directional_light")] +pub const MAX_DIRECTIONAL_LIGHTS: usize = 1; +#[cfg(not(feature = "limit_directional_light"))] pub const MAX_DIRECTIONAL_LIGHTS: usize = 10; #[cfg(not(feature = "webgl"))] pub const MAX_CASCADES_PER_LIGHT: usize = 4;