diff --git a/filament/src/View.cpp b/filament/src/View.cpp index e43395cdaf..e29f7db881 100644 --- a/filament/src/View.cpp +++ b/filament/src/View.cpp @@ -53,13 +53,6 @@ using namespace backend; namespace details { -// values of the 'VISIBLE_MASK' after culling (0: not visible) -static constexpr size_t VISIBLE_RENDERABLE_BIT = 0u; -static constexpr size_t VISIBLE_SHADOW_CASTER_BIT = 1u; -static constexpr uint8_t VISIBLE_RENDERABLE = 1u << VISIBLE_RENDERABLE_BIT; -static constexpr uint8_t VISIBLE_SHADOW_CASTER = 1u << VISIBLE_SHADOW_CASTER_BIT; -static constexpr uint8_t VISIBLE_ALL = VISIBLE_RENDERABLE | VISIBLE_SHADOW_CASTER; - FView::FView(FEngine& engine) : mFroxelizer(engine), mPerViewUb(PerViewUib::getUib().getSize()), @@ -512,7 +505,7 @@ void FView::prepare(FEngine& engine, backend::DriverApi& driver, ArenaScope& are /* * Shadowing: compute the shadow camera and cull shadow casters - * (this will set the VISIBLE_SHADOW_CASTER bit) + * (this will set the VISIBLE_DIR_SHADOW_CASTER bit) */ prepareShadowing(engine, driver, renderableData, scene->getLightData()); @@ -536,7 +529,7 @@ void FView::prepare(FEngine& engine, backend::DriverApi& driver, ArenaScope& are auto beginCasters = partition(beginRenderables, renderableData.end(), VISIBLE_RENDERABLE); auto beginCastersOnly = partition(beginCasters, renderableData.end(), VISIBLE_ALL); auto endCastersOnly = partition(beginCastersOnly, renderableData.end(), - VISIBLE_SHADOW_CASTER); + VISIBLE_DIR_SHADOW_CASTER); // convert to indices uint32_t iEnd = uint32_t(endCastersOnly - beginRenderables); @@ -598,10 +591,28 @@ void FView::computeVisibilityMasks( Culler::result_type mask = visibleMask[i]; FRenderableManager::Visibility v = visibility[i]; bool inVisibleLayer = layers[i] & visibleLayers; + + // The logic below essentially does the following: + // + // if inVisibleLayer: + // if !v.culling: + // set all bits in visibleMask to 1 + // else: + // set all bits in visibleMask to 0 + // if !v.castShadows: + // set shadow visibility bits in visibleMask to 0 + // + // It is written without if statements to avoid branches, which allows it to be vectorized 16x. + bool visRenderables = (!v.culling || (mask & VISIBLE_RENDERABLE)) && inVisibleLayer; - bool visShadowCasters = (!v.culling || (mask & VISIBLE_SHADOW_CASTER)) && inVisibleLayer && v.castShadows; + bool visShadowCasters = (!v.culling || (mask & VISIBLE_DIR_SHADOW_CASTER)) && inVisibleLayer && v.castShadows; visibleMask[i] = Culler::result_type(visRenderables) | - Culler::result_type(visShadowCasters << 1u); + Culler::result_type(visShadowCasters << 1u); + // this loop gets fully unrolled + for (size_t j = 0; j < CONFIG_MAX_SHADOW_CASTING_SPOTS; ++j) { + bool vIsSpotShadowCaster = (!v.culling || (mask & VISIBLE_SPOT_SHADOW_CASTER_N(j))) && inVisibleLayer && v.castShadows; + visibleMask[i] |= Culler::result_type(vIsSpotShadowCaster << (j + 2)); + } } } @@ -707,7 +718,7 @@ UTILS_NOINLINE void FView::prepareVisibleShadowCasters(JobSystem& js, Frustum const& lightFrustum, FScene::RenderableSoa& renderableData) noexcept { SYSTRACE_CALL(); - FView::cullRenderables(js, renderableData, lightFrustum, VISIBLE_SHADOW_CASTER_BIT); + FView::cullRenderables(js, renderableData, lightFrustum, VISIBLE_DIR_SHADOW_CASTER_BIT); } void FView::cullRenderables(JobSystem& js, diff --git a/filament/src/details/View.h b/filament/src/details/View.h index c959098a17..8b078dc41e 100644 --- a/filament/src/details/View.h +++ b/filament/src/details/View.h @@ -56,6 +56,40 @@ class FMaterialInstance; class FRenderer; class FScene; +// The value of the 'VISIBLE_MASK' after culling. Each bit represents visibility in a frustum +// (either camera or light). +// +// bits 7 6 5 4 3 2 1 0 +// +-------------------------------------------+ +// VISIBLE_RENDERABLE X +// VISIBLE_DIR_SHADOW_CASTER X +// VISIBLE_SPOT_SHADOW_CASTER_0 X +// VISIBLE_SPOT_SHADOW_CASTER_1 X +// ... + +static constexpr size_t VISIBLE_RENDERABLE_BIT = 0u; +static constexpr size_t VISIBLE_DIR_SHADOW_CASTER_BIT = 1u; +static constexpr size_t VISIBLE_SPOT_SHADOW_CASTER_N_BIT(size_t n) { return n + 2; } + +static constexpr uint8_t VISIBLE_RENDERABLE = 1u << VISIBLE_RENDERABLE_BIT; +static constexpr uint8_t VISIBLE_DIR_SHADOW_CASTER = 1u << VISIBLE_DIR_SHADOW_CASTER_BIT; +static constexpr uint8_t VISIBLE_SPOT_SHADOW_CASTER_N(size_t n) { + return 1u << VISIBLE_SPOT_SHADOW_CASTER_N_BIT(n); +} + +// ORing of all the VISIBLE_SPOT_SHADOW_CASTER bits +static constexpr uint8_t VISIBLE_SPOT_SHADOW_CASTER = + (0xFF >> (sizeof(uint8_t) * 8u - CONFIG_MAX_SHADOW_CASTING_SPOTS)) << 2u; + +static constexpr uint8_t VISIBLE_ALL = VISIBLE_RENDERABLE | VISIBLE_DIR_SHADOW_CASTER; + +// Because we're using a uint8_t for the visibility mask, we're limited to 6 spot light shadows. +// (2 of the bits are used for visible renderables + directional light shadow casters). +static_assert(CONFIG_MAX_SHADOW_CASTING_SPOTS <= 6, + "CONFIG_MAX_SHADOW_CASTING_SPOTS cannot be higher than 6."); + +// ------------------------------------------------------------------------------------------------ + class FView : public View { public: using Range = utils::Range; diff --git a/libs/filabridge/include/private/filament/EngineEnums.h b/libs/filabridge/include/private/filament/EngineEnums.h index 113ec22931..d3b592429d 100644 --- a/libs/filabridge/include/private/filament/EngineEnums.h +++ b/libs/filabridge/include/private/filament/EngineEnums.h @@ -49,6 +49,12 @@ static_assert(BindingPoints::PER_MATERIAL_INSTANCE == BindingPoints::COUNT - 1, constexpr size_t CONFIG_MAX_LIGHT_COUNT = 256; constexpr size_t CONFIG_MAX_LIGHT_INDEX = CONFIG_MAX_LIGHT_COUNT - 1; +// The maximum number of spot lights in a scene that can cast shadows. +// Light space coordinates are computed in the vertex shader and interpolated across fragments. +// Thus, each additional shadow-casting spot light adds 4 additional varying components. Higher +// values may cause the number of varyings to exceed the driver limit. +constexpr size_t CONFIG_MAX_SHADOW_CASTING_SPOTS = 0; + // This value is also limited by UBO size, ES3.0 only guarantees 16 KiB. // We store 64 bytes per bone. constexpr size_t CONFIG_MAX_BONE_COUNT = 256;