Skip to content

Commit

Permalink
Remove WebGL1 support from the clustered lighting shader (#6314)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Valigursky <[email protected]>
  • Loading branch information
mvaligursky and Martin Valigursky authored Apr 29, 2024
1 parent 118938d commit be57c47
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 113 deletions.
10 changes: 0 additions & 10 deletions src/scene/lighting/lights-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,6 @@ class LightsBuffer {
this._lightsTextureFloatId = undefined;
}

// inverse sizes for both textures
this._lightsTextureInvSizeId = this.device.scope.resolve('lightsTextureInvSize');
this._lightsTextureInvSizeData = new Float32Array(4);
this._lightsTextureInvSizeData[0] = pixelsPerLightFloat ? 1.0 / this.lightsTextureFloat.width : 0;
this._lightsTextureInvSizeData[1] = pixelsPerLightFloat ? 1.0 / this.lightsTextureFloat.height : 0;
this._lightsTextureInvSizeData[2] = 1.0 / this.lightsTexture8.width;
this._lightsTextureInvSizeData[3] = 1.0 / this.lightsTexture8.height;

// compression ranges
this.invMaxColorValue = 0;
this.invMaxAttenuation = 0;
Expand Down Expand Up @@ -249,8 +241,6 @@ class LightsBuffer {
if (this.lightTextureFormat === FORMAT_FLOAT) {
this._lightsTextureFloatId.setValue(this.lightsTextureFloat);
}

this._lightsTextureInvSizeId.setValue(this._lightsTextureInvSizeData);
}

getSpotDirection(direction, spot) {
Expand Down
136 changes: 33 additions & 103 deletions src/scene/shader-lib/chunks/lit/frag/clusteredLight.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,15 @@ uniform highp sampler2D lightsTextureFloat;
#endif
#ifdef CLUSTER_SHADOWS
#ifdef GL2
// TODO: when VSM shadow is supported, it needs to use sampler2D in webgl2
uniform sampler2DShadow shadowAtlasTexture;
#else
uniform sampler2D shadowAtlasTexture;
#endif
// TODO: when VSM shadow is supported, it needs to use sampler2D in webgl2
uniform sampler2DShadow shadowAtlasTexture;
#endif
#ifdef CLUSTER_COOKIES
uniform sampler2D cookieAtlasTexture;
#endif
#ifdef GL2
uniform int clusterMaxCells;
#else
uniform float clusterMaxCells;
uniform vec4 lightsTextureInvSize;
#endif
uniform int clusterMaxCells;
// 1.0 if clustered lighting can be skipped (0 lights in the clusters)
uniform float clusterSkip;
Expand Down Expand Up @@ -151,36 +142,18 @@ vec4 decodeClusterLowRange4Vec4(vec4 d0, vec4 d1, vec4 d2, vec4 d3) {
);
}
#ifdef GL2
vec4 sampleLightsTexture8(const ClusterLightData clusterLightData, int index) {
return texelFetch(lightsTexture8, ivec2(index, clusterLightData.lightIndex), 0);
}
vec4 sampleLightTextureF(const ClusterLightData clusterLightData, int index) {
return texelFetch(lightsTextureFloat, ivec2(index, clusterLightData.lightIndex), 0);
}
#else
vec4 sampleLightsTexture8(const ClusterLightData clusterLightData, float index) {
return texture2DLodEXT(lightsTexture8, vec2(index * lightsTextureInvSize.z, clusterLightData.lightV), 0.0);
}
vec4 sampleLightTextureF(const ClusterLightData clusterLightData, float index) {
return texture2DLodEXT(lightsTextureFloat, vec2(index * lightsTextureInvSize.x, clusterLightData.lightV), 0.0);
}
vec4 sampleLightsTexture8(const ClusterLightData clusterLightData, int index) {
return texelFetch(lightsTexture8, ivec2(index, clusterLightData.lightIndex), 0);
}
#endif
vec4 sampleLightTextureF(const ClusterLightData clusterLightData, int index) {
return texelFetch(lightsTextureFloat, ivec2(index, clusterLightData.lightIndex), 0);
}
void decodeClusterLightCore(inout ClusterLightData clusterLightData, float lightIndex) {
// light index
#ifdef GL2
clusterLightData.lightIndex = int(lightIndex);
#else
clusterLightData.lightV = (lightIndex + 0.5) * lightsTextureInvSize.w;
#endif
clusterLightData.lightIndex = int(lightIndex);
// shared data from 8bit texture
vec4 lightInfo = sampleLightsTexture8(clusterLightData, CLUSTER_TEXTURE_8_FLAGS);
Expand Down Expand Up @@ -689,79 +662,36 @@ void addClusteredLights(
float clusterV = floor(cellIndex * clusterTextureSize.y);
float clusterU = cellIndex - (clusterV * clusterTextureSize.x);
#ifdef GL2
// loop over maximum number of light cells
for (int lightCellIndex = 0; lightCellIndex < clusterMaxCells; lightCellIndex++) {
// using a single channel texture with data in alpha channel
float lightIndex = texelFetch(clusterWorldTexture, ivec2(int(clusterU) + lightCellIndex, clusterV), 0).x;
if (lightIndex <= 0.0)
return;
evaluateClusterLight(
lightIndex * 255.0,
worldNormal,
viewDir,
reflectionDir,
#if defined(LIT_CLEARCOAT)
clearcoatReflectionDir,
#endif
gloss,
specularity,
geometricNormal,
tbn,
#if defined(LIT_IRIDESCENCE)
iridescenceFresnel,
#endif
clearcoat_worldNormal,
clearcoat_gloss,
sheen_gloss,
iridescence_intensity
);
}
#else
clusterV = (clusterV + 0.5) * clusterTextureSize.z;
// loop over maximum possible number of supported light cells
const float maxLightCells = 256.0;
for (float lightCellIndex = 0.5; lightCellIndex < maxLightCells; lightCellIndex++) {
// loop over maximum number of light cells
for (int lightCellIndex = 0; lightCellIndex < clusterMaxCells; lightCellIndex++) {
float lightIndex = texture2DLodEXT(clusterWorldTexture, vec2(clusterTextureSize.y * (clusterU + lightCellIndex), clusterV), 0.0).x;
// using a single channel texture with data in alpha channel
float lightIndex = texelFetch(clusterWorldTexture, ivec2(int(clusterU) + lightCellIndex, clusterV), 0).x;
if (lightIndex <= 0.0)
if (lightIndex <= 0.0)
return;
evaluateClusterLight(
lightIndex * 255.0,
worldNormal,
viewDir,
reflectionDir,
evaluateClusterLight(
lightIndex * 255.0,
worldNormal,
viewDir,
reflectionDir,
#if defined(LIT_CLEARCOAT)
clearcoatReflectionDir,
clearcoatReflectionDir,
#endif
gloss,
specularity,
geometricNormal,
tbn,
gloss,
specularity,
geometricNormal,
tbn,
#if defined(LIT_IRIDESCENCE)
iridescenceFresnel,
iridescenceFresnel,
#endif
clearcoat_worldNormal,
clearcoat_gloss,
sheen_gloss,
iridescence_intensity
);
// end of the cell array
if (lightCellIndex >= clusterMaxCells) {
break;
}
}
#endif
clearcoat_worldNormal,
clearcoat_gloss,
sheen_gloss,
iridescence_intensity
);
}
}
}
`;

0 comments on commit be57c47

Please sign in to comment.