| 1 |
//------------------------------------------------------------------------------ |
| 2 |
// Punctual lights evaluation |
| 3 |
//------------------------------------------------------------------------------ |
| 4 |
|
| 5 |
// Make sure this matches the same constants in Froxel.cpp |
| 6 |
#define FROXEL_BUFFER_WIDTH_SHIFT 6u |
| 7 |
#define FROXEL_BUFFER_WIDTH (1u << FROXEL_BUFFER_WIDTH_SHIFT) |
| 8 |
#define FROXEL_BUFFER_WIDTH_MASK (FROXEL_BUFFER_WIDTH - 1u) |
| 9 |
|
| 10 |
#define RECORD_BUFFER_WIDTH_SHIFT 5u |
| 11 |
#define RECORD_BUFFER_WIDTH (1u << RECORD_BUFFER_WIDTH_SHIFT) |
| 12 |
#define RECORD_BUFFER_WIDTH_MASK (RECORD_BUFFER_WIDTH - 1u) |
| 13 |
|
| 14 |
#define LIGHT_TYPE_POINT 0u |
| 15 |
#define LIGHT_TYPE_SPOT 1u |
| 16 |
|
| 17 |
|
| 18 |
struct FroxelParams { |
| 19 |
uint recordOffset; // offset at which the list of lights for this froxel starts |
| 20 |
uint count; // number lights in this froxel |
| 21 |
}; |
| 22 |
|
| 23 |
/** |
| 24 |
* Returns the coordinates of the froxel at the specified fragment coordinates. |
| 25 |
* The coordinates are a 3D position in the froxel grid. |
| 26 |
*/ |
| 27 |
uvec3 getFroxelCoords(const highp vec3 fragCoords) { |
| 28 |
uvec3 froxelCoord; |
| 29 |
|
| 30 |
froxelCoord.xy = uvec2(fragCoords.xy * frameUniforms.resolution.xy * |
| 31 |
vec2(frameUniforms.oneOverFroxelDimension, frameUniforms.oneOverFroxelDimensionY)); |
| 32 |
|
| 33 |
froxelCoord.z = uint(max(0.0, |
| 34 |
log2(frameUniforms.zParams.x * fragCoords.z + frameUniforms.zParams.y) * |
| 35 |
frameUniforms.zParams.z + frameUniforms.zParams.w)); |
| 36 |
|
| 37 |
return froxelCoord; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Computes the froxel index of the fragment at the specified coordinates. |
| 42 |
* The froxel index is computed from the 3D coordinates of the froxel in the |
| 43 |
* froxel grid and later used to fetch from the froxel data texture |
| 44 |
* (light_froxels). |
| 45 |
*/ |
| 46 |
uint getFroxelIndex(const highp vec3 fragCoords) { |
| 47 |
uvec3 froxelCoord = getFroxelCoords(fragCoords); |
| 48 |
return froxelCoord.x * frameUniforms.fParamsX + |
| 49 |
froxelCoord.y * frameUniforms.fParams.x + |
| 50 |
froxelCoord.z * frameUniforms.fParams.y; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Computes the texture coordinates of the froxel data given a froxel index. |
| 55 |
*/ |
| 56 |
ivec2 getFroxelTexCoord(uint froxelIndex) { |
| 57 |
return ivec2(froxelIndex & FROXEL_BUFFER_WIDTH_MASK, froxelIndex >> FROXEL_BUFFER_WIDTH_SHIFT); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Returns the froxel data for the given froxel index. The data is fetched |
| 62 |
* from the light_froxels texture. |
| 63 |
*/ |
| 64 |
FroxelParams getFroxelParams(uint froxelIndex) { |
| 65 |
ivec2 texCoord = getFroxelTexCoord(froxelIndex); |
| 66 |
uvec2 entry = texelFetch(light_froxels, texCoord, 0).rg; |
| 67 |
|
| 68 |
FroxelParams froxel; |
| 69 |
froxel.recordOffset = entry.r; |
| 70 |
froxel.count = entry.g & 0xFFu; |
| 71 |
return froxel; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Returns the coordinates of the light record in the light_records texture |
| 76 |
* given the specified index. A light record is a single uint index into the |
| 77 |
* lights data buffer (lightsUniforms UBO). |
| 78 |
*/ |
| 79 |
ivec2 getRecordTexCoord(uint index) { |
| 80 |
return ivec2(index & RECORD_BUFFER_WIDTH_MASK, index >> RECORD_BUFFER_WIDTH_SHIFT); |
| 81 |
} |
| 82 |
|
| 83 |
float getSquareFalloffAttenuation(float distanceSquare, float falloff) { |
| 84 |
float factor = distanceSquare * falloff; |
| 85 |
float smoothFactor = saturate(1.0 - factor * factor); |
| 86 |
// We would normally divide by the square distance here |
| 87 |
// but we do it at the call site |
| 88 |
return smoothFactor * smoothFactor; |
| 89 |
} |
| 90 |
|
| 91 |
float getDistanceAttenuation(const highp vec3 posToLight, float falloff) { |
| 92 |
float distanceSquare = dot(posToLight, posToLight); |
| 93 |
float attenuation = getSquareFalloffAttenuation(distanceSquare, falloff); |
| 94 |
// Assume a punctual light occupies a volume of 1cm to avoid a division by 0 |
| 95 |
return attenuation * 1.0 / max(distanceSquare, 1e-4); |
| 96 |
} |
| 97 |
|
| 98 |
float getAngleAttenuation(const vec3 lightDir, const vec3 l, const vec2 scaleOffset) { |
| 99 |
float cd = dot(lightDir, l); |
| 100 |
float attenuation = saturate(cd * scaleOffset.x + scaleOffset.y); |
| 101 |
return attenuation * attenuation; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Returns a Light structure (see common_lighting.fs) describing a point or spot light. |
| 106 |
* The colorIntensity field will store the *pre-exposed* intensity of the light |
| 107 |
* in the w component. |
| 108 |
* |
| 109 |
* The light parameters used to compute the Light structure are fetched from the |
| 110 |
* lightsUniforms uniform buffer. |
| 111 |
*/ |
| 112 |
Light getLight(const uint index) { |
| 113 |
|
| 114 |
// retrieve the light data from the UBO |
| 115 |
ivec2 texCoord = getRecordTexCoord(index); |
| 116 |
uint lightIndex = texelFetch(light_records, texCoord, 0).r; |
| 117 |
highp vec4 positionFalloff = lightsUniforms.lights[lightIndex][0]; |
| 118 |
highp vec4 colorIntensity = lightsUniforms.lights[lightIndex][1]; |
| 119 |
vec4 directionIES = lightsUniforms.lights[lightIndex][2]; |
| 120 |
highp vec4 scaleOffsetShadowType = lightsUniforms.lights[lightIndex][3]; |
| 121 |
|
| 122 |
// poition-to-light vector |
| 123 |
highp vec3 worldPosition = vertex_worldPosition; |
| 124 |
highp vec3 posToLight = positionFalloff.xyz - worldPosition; |
| 125 |
|
| 126 |
// and populate the Light structure |
| 127 |
Light light; |
| 128 |
light.colorIntensity.rgb = colorIntensity.rgb; |
| 129 |
light.colorIntensity.w = computePreExposedIntensity(colorIntensity.w, frameUniforms.exposure); |
| 130 |
light.l = normalize(posToLight); |
| 131 |
light.attenuation = getDistanceAttenuation(posToLight, positionFalloff.w); |
| 132 |
light.NoL = saturate(dot(shading_normal, light.l)); |
| 133 |
light.worldPosition = positionFalloff.xyz; |
| 134 |
|
| 135 |
uint type = floatBitsToUint(scaleOffsetShadowType.w); |
| 136 |
if (type == LIGHT_TYPE_SPOT) { |
| 137 |
light.attenuation *= getAngleAttenuation(-directionIES.xyz, light.l, scaleOffsetShadowType.xy); |
| 138 |
uint shadowBits = floatBitsToUint(scaleOffsetShadowType.z); |
| 139 |
light.castsShadows = bool(shadowBits & 0x1u); |
| 140 |
light.contactShadows = bool((shadowBits >> 1u) & 0x1u); |
| 141 |
light.shadowIndex = (shadowBits >> 2u) & 0xFu; |
| 142 |
light.shadowLayer = (shadowBits >> 6u) & 0xFu; |
| 143 |
} |
| 144 |
|
| 145 |
return light; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Evaluates all punctual lights that my affect the current fragment. |
| 150 |
* The result of the lighting computations is accumulated in the color |
| 151 |
* parameter, as linear HDR RGB. |
| 152 |
*/ |
| 153 |
void evaluatePunctualLights(const PixelParams pixel, inout vec3 color) { |
| 154 |
// Fetch the light information stored in the froxel that contains the |
| 155 |
// current fragment |
| 156 |
FroxelParams froxel = getFroxelParams(getFroxelIndex(getNormalizedViewportCoord())); |
| 157 |
|
| 158 |
// Each froxel contains how many lights can influence |
| 159 |
// the current fragment. A froxel also contains a record offset that |
| 160 |
// tells us where the indices of those lights are in the records |
| 161 |
// texture. The records texture contains the indices of the actual |
| 162 |
// light data in the lightsUniforms uniform buffer |
| 163 |
|
| 164 |
uint index = froxel.recordOffset; |
| 165 |
uint end = index + froxel.count; |
| 166 |
|
| 167 |
// Iterate point lights |
| 168 |
for ( ; index < end; index++) { |
| 169 |
Light light = getLight(index); |
| 170 |
float visibility = 1.0; |
| 171 |
#if defined(HAS_SHADOWING) |
| 172 |
if (light.NoL > 0.0){ |
| 173 |
if (light.castsShadows) { |
| 174 |
#if defined(HAS_VSM) |
| 175 |
visibility = shadowVsm(light_shadowMap, light.shadowLayer, |
| 176 |
getSpotLightSpacePosition(light.shadowIndex)); |
| 177 |
#else |
| 178 |
visibility = shadow(light_shadowMap, light.shadowLayer, |
| 179 |
getSpotLightSpacePosition(light.shadowIndex)); |
| 180 |
#endif |
| 181 |
} |
| 182 |
if (light.contactShadows && visibility > 0.0) { |
| 183 |
if (objectUniforms.screenSpaceContactShadows != 0u) { |
| 184 |
visibility *= 1.0 - screenSpaceContactShadow(light.l); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
#endif |
| 189 |
#if defined(MATERIAL_CAN_SKIP_LIGHTING) |
| 190 |
if (light.NoL <= 0.0) { |
| 191 |
continue; |
| 192 |
} |
| 193 |
#endif |
| 194 |
color.rgb += surfaceShading(pixel, light, visibility); |
| 195 |
} |
| 196 |
} |