Skip to content

Commit

Permalink
Remove parts of shader chunks only used on WebGL1 (#6327)
Browse files Browse the repository at this point in the history
* Remove parts of shader chunks only used on WebGL1

* removed the define

---------

Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
  • Loading branch information
mvaligursky and Martin Valigursky authored May 1, 2024
1 parent e7cd997 commit cbd7160
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 301 deletions.
7 changes: 2 additions & 5 deletions examples/src/examples/graphics/grab-pass/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,8 @@ export default {
vec3 grabColor = texture2DLodEXT(uSceneColorMap, grabUv + offset, mipmap).rgb;
// tint the material based on mipmap
// (note - this could be worked around by using a series of if statements in this case)
#ifdef GL2
float tintIndex = clamp(mipmap, 0.0, 3.0);
grabColor *= tints[int(tintIndex)];
#endif
float tintIndex = clamp(mipmap, 0.0, 3.0);
grabColor *= tints[int(tintIndex)];
// brighten the refracted texture a little bit
// brighten even more the rough parts of the glass
Expand Down
16 changes: 6 additions & 10 deletions src/extras/gizmo/axis-shapes.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,19 @@ const SHADER = {
void main(void) {
gl_Position = matrix_viewProjection * matrix_model * vec4(vertex_position, 1.0);
vColor = vertex_color;
#ifdef GL2
// store z/w for later use in fragment shader
vZW = gl_Position.zw;
// disable depth clipping
gl_Position.z = 0.0;
#endif
// store z/w for later use in fragment shader
vZW = gl_Position.zw;
// disable depth clipping
gl_Position.z = 0.0;
}`,
frag: /* glsl */`
precision highp float;
varying vec4 vColor;
varying vec2 vZW;
void main(void) {
gl_FragColor = vColor;
#ifdef GL2
// clamp depth in Z to [0, 1] range
gl_FragDepth = max(0.0, min(1.0, (vZW.x / vZW.y + 1.0) * 0.5));
#endif
// clamp depth in Z to [0, 1] range
gl_FragDepth = max(0.0, min(1.0, (vZW.x / vZW.y + 1.0) * 0.5));
}`
};

Expand Down
10 changes: 0 additions & 10 deletions src/scene/shader-lib/chunks/common/frag/linearizeDepth.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,8 @@ float linearizeDepth(float z, vec4 cameraParams) {
uniform vec4 camera_params; // x: 1 / camera_far, y: camera_far, z: camera_near, w: is_ortho
#endif
#ifdef GL2
float linearizeDepth(float z) {
return linearizeDepth(z, camera_params);
}
#else
#ifndef UNPACKFLOAT
#define UNPACKFLOAT
float unpackFloat(vec4 rgbaDepth) {
const vec4 bitShift = vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0);
return dot(rgbaDepth, bitShift);
}
#endif
#endif
#endif
`;
16 changes: 0 additions & 16 deletions src/scene/shader-lib/chunks/common/frag/msdf.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
export default /* glsl */`
uniform sampler2D texture_msdfMap;
#ifdef GL_OES_standard_derivatives
#define USE_FWIDTH
#endif
#ifdef GL2
#define USE_FWIDTH
#endif
float median(float r, float g, float b) {
return max(min(r, g), min(max(r, g), b));
}
Expand Down Expand Up @@ -46,17 +38,9 @@ vec4 applyMsdf(vec4 color) {
// too large value (0.5) creates a dark glow around the letters
float smoothingMax = 0.2;
#ifdef USE_FWIDTH
// smoothing depends on size of texture on screen
vec2 w = fwidth(vUv0);
float smoothing = clamp(w.x * font_textureWidth / font_pxrange, 0.0, smoothingMax);
#else
float font_size = 16.0; // TODO fix this
// smoothing gets smaller as the font size gets bigger
// don't have fwidth we can approximate from font size, this doesn't account for scaling
// so a big font scaled down will be wrong...
float smoothing = clamp(font_pxrange / font_size, 0.0, smoothingMax);
#endif
float mapMin = 0.05;
float mapMax = clamp(1.0 - font_sdfIntensity, mapMin, 1.0);
Expand Down
16 changes: 1 addition & 15 deletions src/scene/shader-lib/chunks/common/frag/screenDepth.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,17 @@ uniform vec4 camera_params; // x: 1 / camera_far, y: camera_far, z: cam
#endif
#define LINEARIZE_DEPTH
#ifdef GL2
float linearizeDepth(float z) {
if (camera_params.w == 0.0)
return (camera_params.z * camera_params.y) / (camera_params.y + z * (camera_params.z - camera_params.y));
else
return camera_params.z + z * (camera_params.y - camera_params.z);
}
#else // GL2
#ifndef UNPACKFLOAT
#define UNPACKFLOAT
float unpackFloat(vec4 rgbaDepth) {
const vec4 bitShift = vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0);
return dot(rgbaDepth, bitShift);
}
#endif
#endif
#endif // LINEARIZE_DEPTH
// Retrieves rendered linear camera depth by UV
float getLinearScreenDepth(vec2 uv) {
#ifdef GL2
return linearizeDepth(texture2D(uSceneDepthMap, uv).r);
#else
return unpackFloat(texture2D(uSceneDepthMap, uv)) * camera_params.y;
#endif
return linearizeDepth(texture2D(uSceneDepthMap, uv).r);
}
#ifndef VERTEXSHADER
Expand Down
9 changes: 2 additions & 7 deletions src/scene/shader-lib/chunks/lit/frag/clusteredLight.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,8 @@ struct ClusterLightData {
// area light sizes / orientation
vec3 halfHeight;
#ifdef GL2
// light index
int lightIndex;
#else
// v coordinate to look up the light textures - this is the same as lightIndex but in 0..1 range
float lightV;
#endif
// light index
int lightIndex;
// world space position
vec3 position;
Expand Down
167 changes: 39 additions & 128 deletions src/scene/shader-lib/chunks/lit/frag/clusteredLightShadows.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export default /* glsl */`
// Clustered Omni Sampling using atlas
void _getShadowCoordPerspZbuffer(mat4 shadowMatrix, vec4 shadowParams, vec3 wPos) {
vec4 projPos = shadowMatrix * vec4(wPos, 1.0);
projPos.xyz /= projPos.w;
Expand All @@ -21,157 +20,69 @@ vec3 normalOffsetPointShadow(vec4 shadowParams, vec3 lightPos, inout vec3 lightD
return dir;
}
#ifdef GL2
#if defined(CLUSTER_SHADOW_TYPE_PCF1)
float getShadowOmniClusteredPCF1(SHADOWMAP_ACCEPT(shadowMap), vec4 shadowParams, vec3 omniAtlasViewport, float shadowEdgePixels, vec3 lightDir) {
float shadowTextureResolution = shadowParams.x;
vec2 uv = getCubemapAtlasCoordinates(omniAtlasViewport, shadowEdgePixels, shadowTextureResolution, lightDir);
float shadowZ = length(lightDir) * shadowParams.w + shadowParams.z;
return textureShadow(shadowMap, vec3(uv, shadowZ));
}
#endif
#if defined(CLUSTER_SHADOW_TYPE_PCF3)
float getShadowOmniClusteredPCF3(SHADOWMAP_ACCEPT(shadowMap), vec4 shadowParams, vec3 omniAtlasViewport, float shadowEdgePixels, vec3 lightDir) {
float shadowTextureResolution = shadowParams.x;
vec2 uv = getCubemapAtlasCoordinates(omniAtlasViewport, shadowEdgePixels, shadowTextureResolution, lightDir);
float shadowZ = length(lightDir) * shadowParams.w + shadowParams.z;
vec3 shadowCoord = vec3(uv, shadowZ);
return getShadowPCF3x3(SHADOWMAP_PASS(shadowMap), shadowCoord, shadowParams);
}
#endif
#if defined(CLUSTER_SHADOW_TYPE_PCF5)
#if defined(CLUSTER_SHADOW_TYPE_PCF1)
float getShadowOmniClusteredPCF5(SHADOWMAP_ACCEPT(shadowMap), vec4 shadowParams, vec3 omniAtlasViewport, float shadowEdgePixels, vec3 lightDir) {
float getShadowOmniClusteredPCF1(SHADOWMAP_ACCEPT(shadowMap), vec4 shadowParams, vec3 omniAtlasViewport, float shadowEdgePixels, vec3 lightDir) {
float shadowTextureResolution = shadowParams.x;
vec2 uv = getCubemapAtlasCoordinates(omniAtlasViewport, shadowEdgePixels, shadowTextureResolution, lightDir);
float shadowTextureResolution = shadowParams.x;
vec2 uv = getCubemapAtlasCoordinates(omniAtlasViewport, shadowEdgePixels, shadowTextureResolution, lightDir);
float shadowZ = length(lightDir) * shadowParams.w + shadowParams.z;
vec3 shadowCoord = vec3(uv, shadowZ);
return getShadowPCF5x5(SHADOWMAP_PASS(shadowMap), shadowCoord, shadowParams);
}
#endif
#else
#if defined(CLUSTER_SHADOW_TYPE_PCF1)
float getShadowOmniClusteredPCF1(sampler2D shadowMap, vec4 shadowParams, vec3 omniAtlasViewport, float shadowEdgePixels, vec3 lightDir) {
float shadowTextureResolution = shadowParams.x;
vec2 uv = getCubemapAtlasCoordinates(omniAtlasViewport, shadowEdgePixels, shadowTextureResolution, lightDir);
// no filter shadow sampling
float depth = unpackFloat(textureShadow(shadowMap, uv));
float shadowZ = length(lightDir) * shadowParams.w + shadowParams.z;
return depth > shadowZ ? 1.0 : 0.0;
}
#endif
float shadowZ = length(lightDir) * shadowParams.w + shadowParams.z;
return textureShadow(shadowMap, vec3(uv, shadowZ));
}
#if defined(CLUSTER_SHADOW_TYPE_PCF3)
#endif
float getShadowOmniClusteredPCF3(sampler2D shadowMap, vec4 shadowParams, vec3 omniAtlasViewport, float shadowEdgePixels, vec3 lightDir) {
#if defined(CLUSTER_SHADOW_TYPE_PCF3)
float shadowTextureResolution = shadowParams.x;
vec2 uv = getCubemapAtlasCoordinates(omniAtlasViewport, shadowEdgePixels, shadowTextureResolution, lightDir);
float getShadowOmniClusteredPCF3(SHADOWMAP_ACCEPT(shadowMap), vec4 shadowParams, vec3 omniAtlasViewport, float shadowEdgePixels, vec3 lightDir) {
// pcf3
float shadowZ = length(lightDir) * shadowParams.w + shadowParams.z;
vec3 shadowCoord = vec3(uv, shadowZ);
return getShadowPCF3x3(shadowMap, shadowCoord, shadowParams);
}
float shadowTextureResolution = shadowParams.x;
vec2 uv = getCubemapAtlasCoordinates(omniAtlasViewport, shadowEdgePixels, shadowTextureResolution, lightDir);
#endif
float shadowZ = length(lightDir) * shadowParams.w + shadowParams.z;
vec3 shadowCoord = vec3(uv, shadowZ);
return getShadowPCF3x3(SHADOWMAP_PASS(shadowMap), shadowCoord, shadowParams);
}
#if defined(CLUSTER_SHADOW_TYPE_PCF5)
#endif
// we don't have PCF5 implementation for webgl1, use PCF3
float getShadowOmniClusteredPCF5(sampler2D shadowMap, vec4 shadowParams, vec3 omniAtlasViewport, float shadowEdgePixels, vec3 lightDir) {
#if defined(CLUSTER_SHADOW_TYPE_PCF5)
float shadowTextureResolution = shadowParams.x;
vec2 uv = getCubemapAtlasCoordinates(omniAtlasViewport, shadowEdgePixels, shadowTextureResolution, lightDir);
float getShadowOmniClusteredPCF5(SHADOWMAP_ACCEPT(shadowMap), vec4 shadowParams, vec3 omniAtlasViewport, float shadowEdgePixels, vec3 lightDir) {
// pcf3
float shadowZ = length(lightDir) * shadowParams.w + shadowParams.z;
vec3 shadowCoord = vec3(uv, shadowZ);
return getShadowPCF3x3(shadowMap, shadowCoord, shadowParams);
}
float shadowTextureResolution = shadowParams.x;
vec2 uv = getCubemapAtlasCoordinates(omniAtlasViewport, shadowEdgePixels, shadowTextureResolution, lightDir);
#endif
float shadowZ = length(lightDir) * shadowParams.w + shadowParams.z;
vec3 shadowCoord = vec3(uv, shadowZ);
return getShadowPCF5x5(SHADOWMAP_PASS(shadowMap), shadowCoord, shadowParams);
}
#endif
// Clustered Spot Sampling using atlas
#ifdef GL2
#if defined(CLUSTER_SHADOW_TYPE_PCF1)
float getShadowSpotClusteredPCF1(SHADOWMAP_ACCEPT(shadowMap), vec3 shadowCoord, vec4 shadowParams) {
return textureShadow(shadowMap, shadowCoord);
}
#endif
#if defined(CLUSTER_SHADOW_TYPE_PCF3)
float getShadowSpotClusteredPCF3(SHADOWMAP_ACCEPT(shadowMap), vec3 shadowCoord, vec4 shadowParams) {
return getShadowSpotPCF3x3(SHADOWMAP_PASS(shadowMap), shadowCoord, shadowParams);
}
#endif
#if defined(CLUSTER_SHADOW_TYPE_PCF5)
float getShadowSpotClusteredPCF5(SHADOWMAP_ACCEPT(shadowMap), vec3 shadowCoord, vec4 shadowParams) {
return getShadowPCF5x5(SHADOWMAP_PASS(shadowMap), shadowCoord, shadowParams);
}
#endif
#else
#if defined(CLUSTER_SHADOW_TYPE_PCF1)
#if defined(CLUSTER_SHADOW_TYPE_PCF1)
float getShadowSpotClusteredPCF1(sampler2D shadowMap, vec3 shadowCoord, vec4 shadowParams) {
float depth = unpackFloat(textureShadow(shadowMap, shadowCoord.xy));
return depth > shadowCoord.z ? 1.0 : 0.0;
}
#endif
#if defined(CLUSTER_SHADOW_TYPE_PCF3)
float getShadowSpotClusteredPCF1(SHADOWMAP_ACCEPT(shadowMap), vec3 shadowCoord, vec4 shadowParams) {
return textureShadow(shadowMap, shadowCoord);
}
float getShadowSpotClusteredPCF3(sampler2D shadowMap, vec3 shadowCoord, vec4 shadowParams) {
return getShadowSpotPCF3x3(shadowMap, shadowCoord, shadowParams);
}
#endif
#endif
#if defined(CLUSTER_SHADOW_TYPE_PCF3)
#if defined(CLUSTER_SHADOW_TYPE_PCF5)
float getShadowSpotClusteredPCF3(SHADOWMAP_ACCEPT(shadowMap), vec3 shadowCoord, vec4 shadowParams) {
return getShadowSpotPCF3x3(SHADOWMAP_PASS(shadowMap), shadowCoord, shadowParams);
}
// we don't have PCF5 implementation for webgl1, use PCF3
float getShadowSpotClusteredPCF5(sampler2D shadowMap, vec3 shadowCoord, vec4 shadowParams) {
return getShadowSpotPCF3x3(shadowMap, shadowCoord, shadowParams);
}
#endif
#endif
#if defined(CLUSTER_SHADOW_TYPE_PCF5)
float getShadowSpotClusteredPCF5(SHADOWMAP_ACCEPT(shadowMap), vec3 shadowCoord, vec4 shadowParams) {
return getShadowPCF5x5(SHADOWMAP_PASS(shadowMap), shadowCoord, shadowParams);
}
#endif
`;
18 changes: 1 addition & 17 deletions src/scene/shader-lib/chunks/lit/frag/shadowCascades.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,7 @@ void getShadowCascadeMatrix(mat4 shadowMatrixPalette[4], float shadowCascadeDist
cascadeIndex = min(cascadeIndex, shadowCascadeCount - 1.0);
// pick shadow matrix
#ifdef GL2
cascadeShadowMat = shadowMatrixPalette[int(cascadeIndex)];
#else
// webgl 1 does not allow non-cost index array lookup
if (cascadeIndex == 0.0) {
cascadeShadowMat = shadowMatrixPalette[0];
}
else if (cascadeIndex == 1.0) {
cascadeShadowMat = shadowMatrixPalette[1];
}
else if (cascadeIndex == 2.0) {
cascadeShadowMat = shadowMatrixPalette[2];
}
else {
cascadeShadowMat = shadowMatrixPalette[3];
}
#endif
cascadeShadowMat = shadowMatrixPalette[int(cascadeIndex)];
}
void fadeShadow(float shadowCascadeDistances[4]) {
Expand Down
Loading

0 comments on commit cbd7160

Please sign in to comment.