don't store the light direction in the shadowmap structure

We already have it in the Light structure.
This commit is contained in:
Mathias Agopian
2022-11-14 23:38:59 -08:00
committed by Mathias Agopian
parent 46a8b242a8
commit 77fcf4d260
12 changed files with 25 additions and 33 deletions

View File

@@ -5,6 +5,8 @@ A new header is inserted each time a *tag* is created.
## main branch
- engine: optimize per-shadow UBO [⚠️ **Recompile Materials**]
## v1.29.0
- gltfio: calculate primitive's AABB correctly.

View File

@@ -681,7 +681,6 @@ void ShadowMapManager::prepareSpotShadowMap(ShadowMap& shadowMap,
const double f = shadowMap.getCamera().getCullingFar();
s.shadows[shadowIndex].layer = shadowMap.getLayer();
s.shadows[shadowIndex].lightFromWorldMatrix = shaderParameters.lightSpace;
s.shadows[shadowIndex].direction = direction;
s.shadows[shadowIndex].normalBias = normalBias * wsTexelSizeAtOneMeter;
s.shadows[shadowIndex].lightFromWorldZ = shaderParameters.lightFromWorldZ;
s.shadows[shadowIndex].texelSizeAtOneMeter = wsTexelSizeAtOneMeter;
@@ -763,7 +762,6 @@ void ShadowMapManager::preparePointShadowMap(ShadowMap& shadowMap,
const double f = shadowMap.getCamera().getCullingFar();
s.shadows[shadowIndex].layer = shadowMap.getLayer();
s.shadows[shadowIndex].lightFromWorldMatrix = shaderParameters.lightSpace;
s.shadows[shadowIndex].direction = {}; // no direction of point lights
s.shadows[shadowIndex].normalBias = normalBias * wsTexelSizeAtOneMeter;
s.shadows[shadowIndex].lightFromWorldZ = shaderParameters.lightFromWorldZ;
s.shadows[shadowIndex].texelSizeAtOneMeter = wsTexelSizeAtOneMeter;

View File

@@ -27,7 +27,7 @@
namespace filament {
// update this when a new version of filament wouldn't work with older materials
static constexpr size_t MATERIAL_VERSION = 29;
static constexpr size_t MATERIAL_VERSION = 30;
/**
* Supported shading models

View File

@@ -248,17 +248,14 @@ struct ShadowUib { // NOLINT(cppcoreguidelines-pro-type-member-init)
static constexpr std::string_view _name{ "ShadowUniforms" };
struct alignas(16) ShadowData {
math::mat4f lightFromWorldMatrix; // 64
math::float3 direction; // 12
float normalBias; // 4
math::float4 lightFromWorldZ; // 16
math::float4 reserved0; // 12
float texelSizeAtOneMeter; // 4
float bulbRadiusLs; // 4
float nearOverFarMinusNear; // 4
float normalBias; // 4
bool elvsm; // 4
uint32_t layer; // 4
uint32_t reserved0; // 4
uint32_t reserved1; // 4
uint32_t reserved2; // 4
};

View File

@@ -2,8 +2,9 @@ struct Light {
vec4 colorIntensity; // rgb, pre-exposed intensity
vec3 l;
float attenuation;
vec3 worldPosition;
highp vec3 worldPosition;
float NoL;
highp vec3 direction;
float zLight;
bool castsShadows;
bool contactShadows;

View File

@@ -12,11 +12,11 @@
*/
highp vec4 computeLightSpacePosition(highp vec3 p, const highp vec3 n,
const highp vec3 l, const float b, const highp mat4 lightFromWorldMatrix) {
const highp vec3 dir, const float b, const highp mat4 lightFromWorldMatrix) {
#if !defined(VARIANT_HAS_VSM)
highp float NoL = saturate(dot(n, l));
highp float sinTheta = sqrt(1.0 - NoL * NoL);
highp float cosTheta = saturate(dot(n, dir));
highp float sinTheta = sqrt(1.0 - cosTheta * cosTheta);
p += n * (sinTheta * b);
#endif

View File

@@ -2,15 +2,14 @@
// UBOs, which is is the case here.
struct ShadowData {
highp mat4 lightFromWorldMatrix;
highp vec3 direction;
float normalBias;
highp vec4 lightFromWorldZ;
highp vec4 reserved0;
float texelSizeAtOneMeter;
float bulbRadiusLs;
float nearOverFarMinusNear;
float normalBias;
bool elvsm;
uint layer;
uint reserved0;
uint reserved1;
uint reserved2;
};

View File

@@ -94,9 +94,8 @@ highp vec3 getNormalizedViewportCoord2() {
}
#if defined(VARIANT_HAS_SHADOWING) && defined(VARIANT_HAS_DYNAMIC_LIGHTING)
highp vec4 getSpotLightSpacePosition(uint index, highp float zLight) {
highp vec4 getSpotLightSpacePosition(uint index, highp vec3 dir, highp float zLight) {
highp mat4 lightFromWorldMatrix = shadowUniforms.shadows[index].lightFromWorldMatrix;
highp vec3 dir = shadowUniforms.shadows[index].direction;
// for spotlights, the bias depends on z
float bias = shadowUniforms.shadows[index].normalBias * zLight;

View File

@@ -56,7 +56,7 @@ void evaluateDirectionalLight(const MaterialInputs material,
bool cascadeHasVisibleShadows = bool(frameUniforms.cascades & ((1u << cascade) << 8u));
bool hasDirectionalShadows = bool(frameUniforms.directionalShadows & 1u);
if (hasDirectionalShadows && cascadeHasVisibleShadows) {
highp vec4 shadowPosition = getShadowPosition(true, 0u, cascade, 0.0f);
highp vec4 shadowPosition = getShadowPosition(cascade);
visibility = shadow(true, light_shadowMap, cascade, shadowPosition, 0.0f);
}
if ((frameUniforms.directionalShadows & 0x2u) != 0u && visibility > 0.0) {

View File

@@ -150,6 +150,7 @@ Light getLight(const uint lightIndex) {
light.colorIntensity.w = computePreExposedIntensity(intensity, frameUniforms.exposure);
light.l = normalize(posToLight);
light.attenuation = getDistanceAttenuation(posToLight, positionFalloff.w);
light.direction = direction;
light.NoL = saturate(dot(shading_normal, light.l));
light.worldPosition = positionFalloff.xyz;
light.channels = channels;
@@ -219,7 +220,7 @@ void evaluatePunctualLights(const MaterialInputs material,
light.zLight = dot(shadowUniforms.shadows[shadowIndex].lightFromWorldZ,
vec4(getWorldPosition(), 1.0));
}
highp vec4 shadowPosition = getShadowPosition(false, shadowIndex, 0u, light.zLight);
highp vec4 shadowPosition = getShadowPosition(shadowIndex, light.direction, light.zLight);
visibility = shadow(false, light_shadowMap, shadowIndex,
shadowPosition, light.zLight);
}

View File

@@ -50,7 +50,7 @@ vec4 evaluateMaterial(const MaterialInputs material) {
bool cascadeHasVisibleShadows = bool(frameUniforms.cascades & ((1u << cascade) << 8u));
bool hasDirectionalShadows = bool(frameUniforms.directionalShadows & 1u);
if (hasDirectionalShadows && cascadeHasVisibleShadows) {
highp vec4 shadowPosition = getShadowPosition(true, 0u, cascade, 0.0f);
highp vec4 shadowPosition = getShadowPosition(cascade);
visibility = shadow(true, light_shadowMap, cascade, shadowPosition, 0.0f);
}
if ((frameUniforms.directionalShadows & 0x2u) != 0u && visibility > 0.0) {

View File

@@ -477,22 +477,17 @@ float ShadowSample_VSM(const bool ELVSM, const mediump sampler2DArray shadowMap,
*/
// get texture coordinate for directional and spot shadow maps
highp vec4 getShadowPosition(const bool DIRECTIONAL,
const uint index, const uint cascade, const highp float zLight) {
highp vec4 p;
// This conditional is resolved at compile time
if (DIRECTIONAL) {
#if defined(VARIANT_HAS_DIRECTIONAL_LIGHTING)
p = getCascadeLightSpacePosition(cascade);
#endif
} else {
#if defined(VARIANT_HAS_DYNAMIC_LIGHTING)
p = getSpotLightSpacePosition(index, zLight);
#endif
}
return p;
highp vec4 getShadowPosition(const uint cascade) {
return getCascadeLightSpacePosition(cascade);
}
#endif
#if defined(VARIANT_HAS_DYNAMIC_LIGHTING)
highp vec4 getShadowPosition(const uint index, const highp vec3 dir, const highp float zLight) {
return getSpotLightSpacePosition(index, dir, zLight);
}
#endif
uint getPointLightFace(const highp vec3 r) {
highp vec4 tc;