simpify froxel GPU data structure

instead of storing two counters for point and spot lights, we now
only have a single counter for both, and the light data structure
has a type, the shader can use to decide what to do -- instead of
using two loops. In practice spot and point light code is very similar 
anyways. 

This simplify a lot the CPU side (as in make it less complex) and frees
up 8-bits in the per-froxel GPU data structure.
This commit is contained in:
Pixelflinger
2020-04-24 01:42:27 -07:00
committed by Mathias Agopian
parent 880bf0f061
commit 043cdcfcff
6 changed files with 52 additions and 134 deletions
+10 -35
View File
@@ -540,7 +540,7 @@ void Froxelizer::froxelizeLights(FEngine& engine,
mFroxelCountX * mFroxelCountY * mFroxelCountZ);
for (auto const& entry : gpuFroxelEntries) {
// go through every lights for that froxel
for (size_t i = 0; i < entry.pointLightCount + entry.spotLightCount; i++) {
for (size_t i = 0; i < entry.count; i++) {
// get the light index
assert(entry.offset + i < RECORD_BUFFER_ENTRY_COUNT);
@@ -591,8 +591,6 @@ void Froxelizer::froxelizeLoop(FEngine& engine,
assert(bit < LIGHT_PER_GROUP);
FroxelThreadData& threadData = froxelThreadData[group];
const bool isSpot = light.invSin != std::numeric_limits<float>::infinity();
threadData[0] |= isSpot << bit;
froxelizePointAndSpotLight(threadData, bit, projection, light);
}
};
@@ -625,21 +623,9 @@ void Froxelizer::froxelizeAssignRecordsCompress() noexcept {
// easily compare adjacent froxels, for compaction. The conversion loops below get
// inlined and vectorized in release builds.
// keep these two loops separate, it helps the compiler a lot
LightRecord::bitset spotLights;
for (size_t i = 0; i < LightRecord::bitset::WORLD_COUNT; i++) {
using container_type = LightRecord::bitset::container_type;
constexpr size_t r = sizeof(container_type) / sizeof(LightGroupType);
container_type b = froxelThreadData[i * r][0];
for (size_t k = 0; k < r; k++) {
b |= (container_type(froxelThreadData[i * r + k][0]) << (LIGHT_PER_GROUP * k));
}
spotLights.getBitsAt(i) = b;
}
// this gets very well vectorized...
utils::Slice<LightRecord> records(mLightRecords);
for (size_t j = 1, jc = FROXEL_BUFFER_ENTRY_COUNT_MAX; j < jc; j++) {
for (size_t j = 0, jc = FROXEL_BUFFER_ENTRY_COUNT_MAX; j < jc; j++) {
for (size_t i = 0; i < LightRecord::bitset::WORLD_COUNT; i++) {
using container_type = LightRecord::bitset::container_type;
constexpr size_t r = sizeof(container_type) / sizeof(LightGroupType);
@@ -647,7 +633,7 @@ void Froxelizer::froxelizeAssignRecordsCompress() noexcept {
for (size_t k = 0; k < r; k++) {
b |= (container_type(froxelThreadData[i * r + k][j]) << (LIGHT_PER_GROUP * k));
}
records[j - 1].lights.getBitsAt(i) = b;
records[j].lights.getBitsAt(i) = b;
}
}
@@ -671,10 +657,9 @@ void Froxelizer::froxelizeAssignRecordsCompress() noexcept {
// note: initializer list for union cannot have more than one element
FroxelEntry entry;
entry.offset = offset;
entry.pointLightCount = (uint8_t)std::min(size_t(255), (b.lights & ~spotLights).count());
entry.spotLightCount = (uint8_t)std::min(size_t(255), (b.lights & spotLights).count());
entry.count = (uint8_t)std::min(size_t(255), b.lights.count());
const size_t lightCount = entry.count[0] + entry.count[1];
const size_t lightCount = entry.count;
if (UTILS_UNLIKELY(offset + lightCount >= RECORD_BUFFER_ENTRY_COUNT)) {
#ifndef NDEBUG
@@ -690,24 +675,15 @@ void Froxelizer::froxelizeAssignRecordsCompress() noexcept {
// iterate the bitfield
auto * const beginPoint = froxelRecords + offset;
auto * const beginSpot = froxelRecords + offset + entry.count[0];
b.lights.forEachSetBit([&spotLights,
point = beginPoint, spot = beginSpot, beginPoint, beginSpot]
(size_t l) mutable {
b.lights.forEachSetBit([point = beginPoint, beginPoint](size_t l) mutable {
// make sure to keep this code branch-less
const bool isSpot = spotLights[l];
auto& p = isSpot ? spot : point;
auto *s = isSpot ? beginSpot : beginPoint;
const size_t word = l / LIGHT_PER_GROUP;
const size_t bit = l % LIGHT_PER_GROUP;
l = (bit * GROUP_COUNT) | (word % GROUP_COUNT);
*p = (RecordBufferType)l;
*point = (RecordBufferType)l;
// we need to "cancel" the write if we have more than 255 spot or point lights
// (this is a limitation of the data type used to store the light counts per froxel)
p += (p - s < 255) ? 1 : 0;
point += (point - beginPoint < 255) ? 1 : 0;
});
offset += lightCount;
@@ -876,14 +852,13 @@ void Froxelizer::froxelizePointAndSpotLight(
assert(bx < mFroxelCountX && ex <= mFroxelCountX);
// The first entry reserved for type of light, i.e. point/spot
size_t fi = getFroxelIndex(bx, iy, iz) + 1;
size_t fi = getFroxelIndex(bx, iy, iz);
if (light.invSin != std::numeric_limits<float>::infinity()) {
// This is a spotlight (common case)
// this loops gets vectorized (on arm64) w/ clang
while (bx++ != ex) {
// see if this froxel intersects the cone
bool intersect = sphereConeIntersectionFast(boundingSpheres[fi - 1],
bool intersect = sphereConeIntersectionFast(boundingSpheres[fi],
light.position, light.axis, light.invSin, light.cosSqr);
froxelThread[fi++] |= LightGroupType(intersect) << bit;
}
+3 -2
View File
@@ -292,9 +292,10 @@ void FScene::prepareDynamicLights(const CameraInfo& camera, ArenaScope& rootAren
auto li = instances[i];
lp[gpuIndex].positionFalloff = { spheres[i].xyz, lcm.getSquaredFalloffInv(li) };
lp[gpuIndex].colorIntensity = { lcm.getColor(li), lcm.getIntensity(li) };
lp[gpuIndex].directionIES = { directions[i], 0 };
lp[gpuIndex].directionIES = { directions[i], 0.0f };
lp[gpuIndex].spotScaleOffset = lcm.getSpotParams(li).scaleOffset;
lp[gpuIndex].shadow = { shadowInfo[i].pack(), 0 };
lp[gpuIndex].shadow = { shadowInfo[i].pack() };
lp[gpuIndex].type = lcm.isPointLight(li) ? 0u : 1u;
}
driver.loadUniformBuffer(lightUbh, { lp, positionalLightCount * sizeof(LightsUib) });
+3 -9
View File
@@ -148,13 +148,8 @@ public:
uint32_t u32 = 0;
struct {
uint16_t offset;
union {
uint8_t count[2];
struct {
uint8_t pointLightCount;
uint8_t spotLightCount;
};
};
uint8_t count;
uint8_t reserved;
};
};
};
@@ -196,8 +191,7 @@ private:
uint16_t reserved;
};
// The first entry always encodes the type of light, i.e. point/spot
using FroxelThreadData = std::array<LightGroupType, FROXEL_BUFFER_ENTRY_COUNT_MAX + 1>;
using FroxelThreadData = std::array<LightGroupType, FROXEL_BUFFER_ENTRY_COUNT_MAX>;
void setViewport(Viewport const& viewport) noexcept;
void setProjection(const math::mat4f& projection, float near, float far) noexcept;
+4 -6
View File
@@ -698,9 +698,8 @@ TEST(FilamentTest, FroxelData) {
// light straddles the "light near" plane
size_t pointCount = 0;
for (const auto& entry : froxelBuffer) {
EXPECT_LE(entry.pointLightCount, 1);
EXPECT_EQ(entry.spotLightCount, 0);
pointCount += entry.pointLightCount;
EXPECT_LE(entry.count, 1);
pointCount += entry.count;
}
EXPECT_GT(pointCount, 0);
}
@@ -717,9 +716,8 @@ TEST(FilamentTest, FroxelData) {
auto const& recordBuffer = froxelData.getRecordBufferUser();
size_t pointCount = 0;
for (const auto& entry : froxelBuffer) {
EXPECT_LE(entry.pointLightCount, 1);
EXPECT_EQ(entry.spotLightCount, 0);
pointCount += entry.pointLightCount;
EXPECT_LE(entry.count, 1);
pointCount += entry.count;
}
EXPECT_GT(pointCount, 0);
}
@@ -134,7 +134,8 @@ struct LightsUib {
filament::math::float4 colorIntensity; // { float3(col), intensity }
filament::math::float4 directionIES; // { float3(dir), IES index }
filament::math::float2 spotScaleOffset; // { scale, offset }
filament::math::uint2 shadow; // { shadow bits (see ShadowInfo), unused }
uint32_t shadow; // { shadow bits (see ShadowInfo) }
uint32_t type; // { 0=point, 1=spot }
};
// UBO for punctual (spot light) shadows.
+30 -81
View File
@@ -11,10 +11,13 @@
#define RECORD_BUFFER_WIDTH (1u << RECORD_BUFFER_WIDTH_SHIFT)
#define RECORD_BUFFER_WIDTH_MASK (RECORD_BUFFER_WIDTH - 1u)
#define LIGHT_TYPE_POINT 0u
#define LIGHT_TYPE_SPOT 1u
struct FroxelParams {
uint recordOffset; // offset at which the list of lights for this froxel starts
uint pointCount; // number of point lights in this froxel
uint spotCount; // number of spot lights in this froxel
uint count; // number lights in this froxel
};
/**
@@ -70,8 +73,7 @@ FroxelParams getFroxelParams(uint froxelIndex) {
FroxelParams froxel;
froxel.recordOffset = entry.r;
froxel.pointCount = entry.g & 0xFFu;
froxel.spotCount = entry.g >> 8u;
froxel.count = entry.g & 0xFFu;
return froxel;
}
@@ -106,74 +108,44 @@ float getAngleAttenuation(const vec3 lightDir, const vec3 l, const vec2 scaleOff
}
/**
* Light setup common to point and spot light. This function sets the light vector
* "l" and the attenuation factor in the Light structure. The attenuation factor
* can be partial: it only takes distance attenuation into account. Spot lights
* must compute an additional angle attenuation.
*/
void setupPunctualLight(inout Light light, const highp vec4 positionFalloff) {
highp vec3 worldPosition = vertex_worldPosition;
highp vec3 posToLight = positionFalloff.xyz - worldPosition;
light.l = normalize(posToLight);
light.attenuation = getDistanceAttenuation(posToLight, positionFalloff.w);
light.NoL = saturate(dot(shading_normal, light.l));
}
/**
* Returns a Light structure (see common_lighting.fs) describing a spot light.
* Returns a Light structure (see common_lighting.fs) describing a point or spot light.
* The colorIntensity field will store the *pre-exposed* intensity of the light
* in the w component.
*
* The light parameters used to compute the Light structure are fetched from the
* lightsUniforms uniform buffer.
*/
Light getSpotLight(uint index) {
Light light;
Light getLight(const uint index) {
// retrieve the light data from the UBO
ivec2 texCoord = getRecordTexCoord(index);
uint lightIndex = texelFetch(light_records, texCoord, 0).r;
highp vec4 positionFalloff = lightsUniforms.lights[lightIndex][0];
highp vec4 colorIntensity = lightsUniforms.lights[lightIndex][1];
vec4 directionIES = lightsUniforms.lights[lightIndex][2];
highp vec4 scaleOffsetShadow = lightsUniforms.lights[lightIndex][3];
highp vec4 scaleOffsetShadowType = lightsUniforms.lights[lightIndex][3];
light.colorIntensity.rgb = colorIntensity.rgb;
light.colorIntensity.w = computePreExposedIntensity(colorIntensity.w, frameUniforms.exposure);
// poition-to-light vector
highp vec3 worldPosition = vertex_worldPosition;
highp vec3 posToLight = positionFalloff.xyz - worldPosition;
setupPunctualLight(light, positionFalloff);
light.attenuation *= getAngleAttenuation(-directionIES.xyz, light.l, scaleOffsetShadow.xy);
uint shadowBits = floatBitsToUint(scaleOffsetShadow.z);
light.castsShadows = bool(shadowBits & 0x1u);
light.contactShadows = bool((shadowBits >> 1u) & 0x1u);
light.shadowIndex = (shadowBits >> 2u) & 0xFu;
light.shadowLayer = (shadowBits >> 6u) & 0xFu;
return light;
}
/**
* Returns a Light structure (see common_lighting.fs) describing a point light.
* The colorIntensity field will store the *pre-exposed* intensity of the light
* in the w component.
*
* The light parameters used to compute the Light structure are fetched from the
* lightsUniforms uniform buffer.
*/
Light getPointLight(uint index) {
// and populate the Light structure
Light light;
ivec2 texCoord = getRecordTexCoord(index);
uint lightIndex = texelFetch(light_records, texCoord, 0).r;
highp vec4 positionFalloff = lightsUniforms.lights[lightIndex][0];
highp vec4 colorIntensity = lightsUniforms.lights[lightIndex][1];
light.colorIntensity.rgb = colorIntensity.rgb;
light.colorIntensity.w = computePreExposedIntensity(colorIntensity.w, frameUniforms.exposure);
light.l = normalize(posToLight);
light.attenuation = getDistanceAttenuation(posToLight, positionFalloff.w);
light.NoL = saturate(dot(shading_normal, light.l));
setupPunctualLight(light, positionFalloff);
uint type = floatBitsToUint(scaleOffsetShadowType.w);
if (type == LIGHT_TYPE_SPOT) {
light.attenuation *= getAngleAttenuation(-directionIES.xyz, light.l, scaleOffsetShadowType.xy);
uint shadowBits = floatBitsToUint(scaleOffsetShadowType.z);
light.castsShadows = bool(shadowBits & 0x1u);
light.contactShadows = bool((shadowBits >> 1u) & 0x1u);
light.shadowIndex = (shadowBits >> 2u) & 0xFu;
light.shadowLayer = (shadowBits >> 6u) & 0xFu;
}
return light;
}
@@ -188,41 +160,18 @@ void evaluatePunctualLights(const PixelParams pixel, inout vec3 color) {
// current fragment
FroxelParams froxel = getFroxelParams(getFroxelIndex(gl_FragCoord.xyz));
// Each froxel contains how many point and spot lights can influence
// Each froxel contains how many lights can influence
// the current fragment. A froxel also contains a record offset that
// tells us where the indices of those lights are in the records
// texture. The records texture contains the indices of the actual
// light data in the lightsUniforms uniform buffer
uint index = froxel.recordOffset;
uint end = index + froxel.pointCount;
uint end = index + froxel.count;
// Iterate point lights
for ( ; index < end; index++) {
Light light = getPointLight(index);
float visibility = 1.0;
#if defined(HAS_SHADOWING)
if (light.NoL > 0.0){
if (light.contactShadows) {
if (objectUniforms.screenSpaceContactShadows != 0u) {
visibility -= screenSpaceContactShadow(light.l);
}
}
}
#endif
#if defined(MATERIAL_CAN_SKIP_LIGHTING)
if (light.NoL <= 0.0) {
continue;
}
#endif
color.rgb += surfaceShading(pixel, light, visibility);
}
end += froxel.spotCount;
// Iterate spotlights
for ( ; index < end; index++) {
Light light = getSpotLight(index);
Light light = getLight(index);
float visibility = 1.0;
#if defined(HAS_SHADOWING)
if (light.NoL > 0.0){