Skip to content

Commit 228a3f7

Browse files
committed
chore: shader cleanup & tweaks
1 parent 071fb96 commit 228a3f7

File tree

7 files changed

+371
-683
lines changed

7 files changed

+371
-683
lines changed

shaders/deferred_lighting.frag

Lines changed: 1 addition & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -97,195 +97,7 @@ layout(location = 0) in struct plShaderIn {
9797
vec2 tUV;
9898
} tShaderIn;
9999

100-
vec3
101-
getDiffuseLight(vec3 n, int iProbeIndex)
102-
{
103-
// n.z = -n.z; // uncomment if not reverse z
104-
// return texture(samplerCube(u_LambertianEnvSampler, tEnvSampler), n).rgb;
105-
return texture(samplerCube(atCubeTextures[nonuniformEXT(tProbeData.atData[iProbeIndex].uLambertianEnvSampler)], tEnvSampler), n).rgb;
106-
}
107-
108-
vec4
109-
getSpecularSample(vec3 reflection, float lod, int iProbeIndex)
110-
{
111-
// reflection.z = -reflection.z; // uncomment if not reverse z
112-
// reflection.x = -reflection.x; // uncomment if not reverse z
113-
// return textureLod(samplerCube(u_GGXEnvSampler, tEnvSampler), reflection, lod);
114-
return textureLod(samplerCube(atCubeTextures[nonuniformEXT(tProbeData.atData[iProbeIndex].uGGXEnvSampler)], tEnvSampler), reflection, lod);
115-
}
116-
117-
vec3
118-
getIBLRadianceGGX(vec3 n, vec3 v, float roughness, vec3 F0, float specularWeight, int u_MipCount, int iProbeIndex, vec3 tWorldPos)
119-
{
120-
121-
float lod = roughness * float(u_MipCount - 1);
122-
vec3 reflection = normalize(reflect(-v, n));
123-
124-
if(bool(tProbeData.atData[iProbeIndex].iParallaxCorrection))
125-
{
126-
127-
// Find the ray intersection with box plane
128-
vec3 FirstPlaneIntersect = (tProbeData.atData[iProbeIndex].tMax.xyz - tWorldPos) / reflection;
129-
vec3 SecondPlaneIntersect = (tProbeData.atData[iProbeIndex].tMin.xyz - tWorldPos) / reflection;
130-
131-
// Get the furthest of these intersections along the ray
132-
// (Ok because x/0 give +inf and -x/0 give –inf )
133-
vec3 FurthestPlane = max(FirstPlaneIntersect, SecondPlaneIntersect);
134-
135-
// Find the closest far intersection
136-
float Distance = min(min(FurthestPlane.x, FurthestPlane.y), FurthestPlane.z);
137-
138-
// Get the intersection position
139-
vec3 IntersectPositionWS = tWorldPos + reflection * Distance;
140-
141-
// Get corrected reflection
142-
reflection = IntersectPositionWS - tProbeData.atData[iProbeIndex].tPosition;
143-
}
144-
145-
// End parallax-correction code
146-
147-
vec4 specularSample = getSpecularSample(reflection, lod, iProbeIndex);
148-
149-
float NdotV = clampedDot(n, v);
150-
vec2 brdfSamplePoint = clamp(vec2(NdotV, roughness), vec2(0.0, 0.0), vec2(1.0, 1.0));
151-
// vec2 f_ab = texture(sampler2D(u_GGXLUT, tEnvSampler), brdfSamplePoint).rg;
152-
vec2 f_ab = texture(sampler2D(at2DTextures[nonuniformEXT(tProbeData.atData[iProbeIndex].uGGXLUT)], tEnvSampler), brdfSamplePoint).rg;
153-
154-
vec3 specularLight = specularSample.rgb;
155-
156-
vec3 Fr = max(vec3(1.0 - roughness), F0) - F0;
157-
vec3 k_S = F0 + Fr * pow(1.0 - NdotV, 5.0);
158-
vec3 FssEss = k_S * f_ab.x + f_ab.y;
159-
160-
return specularWeight * specularLight * FssEss;
161-
}
162-
163-
// specularWeight is introduced with KHR_materials_specular
164-
vec3
165-
getIBLRadianceLambertian(vec3 n, vec3 v, float roughness, vec3 diffuseColor, vec3 F0, float specularWeight, int iProbeIndex, vec3 tWorldPos)
166-
{
167-
168-
// if(bool(tProbeData.atData[iProbeIndex].iParallaxCorrection))
169-
// {
170-
171-
// // Find the ray intersection with box plane
172-
// vec3 FirstPlaneIntersect = (tProbeData.atData[iProbeIndex].tMax.xyz - tWorldPos) / n;
173-
// vec3 SecondPlaneIntersect = (tProbeData.atData[iProbeIndex].tMin.xyz - tWorldPos) / n;
174-
175-
// // Get the furthest of these intersections along the ray
176-
// // (Ok because x/0 give +inf and -x/0 give –inf )
177-
// vec3 FurthestPlane = max(FirstPlaneIntersect, SecondPlaneIntersect);
178-
179-
// // Find the closest far intersection
180-
// float Distance = min(min(FurthestPlane.x, FurthestPlane.y), FurthestPlane.z);
181-
182-
// // Get the intersection position
183-
// vec3 IntersectPositionWS = tWorldPos + n * Distance;
184-
185-
// // Get corrected reflection
186-
// n = IntersectPositionWS - tProbeData.atData[iProbeIndex].tPosition;
187-
// }
188-
189-
// End parallax-correction code
190-
191-
vec3 irradiance = getDiffuseLight(n, iProbeIndex);
192-
193-
float NdotV = clampedDot(n, v);
194-
vec2 brdfSamplePoint = clamp(vec2(NdotV, roughness), vec2(0.0, 0.0), vec2(1.0, 1.0));
195-
// vec2 f_ab = texture(sampler2D(u_GGXLUT, tEnvSampler), brdfSamplePoint).rg;
196-
vec2 f_ab = texture(sampler2D(at2DTextures[nonuniformEXT(tProbeData.atData[iProbeIndex].uGGXLUT)], tEnvSampler), brdfSamplePoint).rg;
197-
198-
// see https://bruop.github.io/ibl/#single_scattering_results at Single Scattering Results
199-
// Roughness dependent fresnel, from Fdez-Aguera
200-
201-
vec3 Fr = max(vec3(1.0 - roughness), F0) - F0;
202-
vec3 k_S = F0 + Fr * pow(1.0 - NdotV, 5.0);
203-
vec3 FssEss = specularWeight * k_S * f_ab.x + f_ab.y; // <--- GGX / specular light contribution (scale it down if the specularWeight is low)
204-
205-
// Multiple scattering, from Fdez-Aguera
206-
float Ems = (1.0 - (f_ab.x + f_ab.y));
207-
vec3 F_avg = specularWeight * (F0 + (1.0 - F0) / 21.0);
208-
vec3 FmsEms = Ems * FssEss * F_avg / (1.0 - F_avg * Ems);
209-
vec3 k_D = diffuseColor * (1.0 - FssEss + FmsEms); // we use +FmsEms as indicated by the formula in the blog post (might be a typo in the implementation)
210-
211-
return (FmsEms + k_D) * irradiance;
212-
}
213-
214-
float
215-
textureProj(vec4 shadowCoord, vec2 offset, int textureIndex)
216-
{
217-
float shadow = 1.0;
218-
vec2 comp2 = shadowCoord.st + offset;
219-
220-
if ( shadowCoord.z > -1.0 && shadowCoord.z < 1.0 )
221-
{
222-
float dist = 1.0 - texture(sampler2D(at2DTextures[nonuniformEXT(textureIndex)], tShadowSampler), comp2).r * shadowCoord.w;
223-
if (shadowCoord.w > 0 && dist < shadowCoord.z)
224-
{
225-
shadow = 0.0; // ambient
226-
}
227-
}
228-
return shadow;
229-
}
230-
231-
float
232-
textureProj2(vec4 shadowCoord, vec2 offset, int textureIndex)
233-
{
234-
float shadow = 1.0;
235-
vec2 comp2 = shadowCoord.st + offset;
236-
237-
if ( shadowCoord.z > -1.0 && shadowCoord.z < 1.0 )
238-
{
239-
float dist = texture(sampler2D(at2DTextures[nonuniformEXT(textureIndex)], tShadowSampler), comp2).r;
240-
if (shadowCoord.w > 0 && dist > shadowCoord.z)
241-
{
242-
shadow = 0.0; // ambient
243-
}
244-
}
245-
return shadow;
246-
}
247-
248-
float
249-
filterPCF(vec4 sc, vec2 offset, int textureIndex)
250-
{
251-
ivec2 texDim = textureSize(sampler2D(at2DTextures[nonuniformEXT(textureIndex)], tShadowSampler), 0).xy;
252-
float scale = 1.0;
253-
float dx = scale * 1.0 / (float(texDim.x));
254-
float dy = scale * 1.0 / (float(texDim.y));
255-
256-
float shadowFactor = 0.0;
257-
int count = 0;
258-
int range = 1;
259-
260-
for (int x = -range; x <= range; x++) {
261-
for (int y = -range; y <= range; y++) {
262-
shadowFactor += textureProj(sc, vec2(dx*x, dy*y) + offset, textureIndex);
263-
count++;
264-
}
265-
}
266-
return shadowFactor / count;
267-
}
268-
269-
float
270-
filterPCF2(vec4 sc, vec2 offset, int textureIndex)
271-
{
272-
ivec2 texDim = textureSize(sampler2D(at2DTextures[nonuniformEXT(textureIndex)], tShadowSampler), 0).xy;
273-
float scale = 1.0;
274-
float dx = scale * 1.0 / (float(texDim.x));
275-
float dy = scale * 1.0 / (float(texDim.y));
276-
277-
float shadowFactor = 0.0;
278-
int count = 0;
279-
int range = 1;
280-
281-
for (int x = -range; x <= range; x++) {
282-
for (int y = -range; y <= range; y++) {
283-
shadowFactor += textureProj2(sc, vec2(dx*x, dy*y) + offset, textureIndex);
284-
count++;
285-
}
286-
}
287-
return shadowFactor / count;
288-
}
100+
#include "lighting.glsl"
289101

290102
void main()
291103
{

shaders/filter_environment.comp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,27 +74,35 @@ pl_write_face(int iPixel, int iFace, vec3 tColorIn)
7474
tFaceOut5.atPixelData[iPixel] = tColor;
7575
}
7676

77-
float
78-
pl_radical_inverse_vdc(uint uBits)
77+
vec2
78+
pl_hammersley_2d(int i, int iN)
7979
{
8080
// Hammersley Points on the Hemisphere
8181
// CC BY 3.0 (Holger Dammertz)
8282
// http://holger.dammertz.org/stuff/notes_HammersleyOnHemisphere.html
8383
// with adapted interface
84+
uint uBits = uint(i);
8485
uBits = (uBits << 16u) | (uBits >> 16u);
8586
uBits = ((uBits & 0x55555555u) << 1u) | ((uBits & 0xAAAAAAAAu) >> 1u);
8687
uBits = ((uBits & 0x33333333u) << 2u) | ((uBits & 0xCCCCCCCCu) >> 2u);
8788
uBits = ((uBits & 0x0F0F0F0Fu) << 4u) | ((uBits & 0xF0F0F0F0u) >> 4u);
8889
uBits = ((uBits & 0x00FF00FFu) << 8u) | ((uBits & 0xFF00FF00u) >> 8u);
89-
return float(uBits) * 2.3283064365386963e-10; // / 0x100000000
90-
}
90+
float rdi = float(uBits) * 2.3283064365386963e-10; // / 0x100000000
9191

92-
vec2
93-
pl_hammersley_2d(int i, int iN)
94-
{
9592
// hammersley2d describes a sequence of points in the 2d unit square [0,1)^2
9693
// that can be used for quasi Monte Carlo integration
97-
return vec2(float(i)/float(iN), pl_radical_inverse_vdc(uint(i)));
94+
return vec2(float(i)/float(iN), rdi);
95+
}
96+
97+
float
98+
pl_random(vec2 co)
99+
{
100+
float a = 12.9898;
101+
float b = 78.233;
102+
float c = 43758.5453;
103+
float dt = dot(co.xy, vec2(a, b));
104+
float sn = mod(dt, 3.14);
105+
return fract(sin(sn) * c);
98106
}
99107

100108
mat3
@@ -243,6 +251,8 @@ pl_get_importance_sample(int iSampleIndex, vec3 tN, float fRoughness)
243251
tImportanceSample = pl_charlie(tXi, fRoughness);
244252
}
245253

254+
tImportanceSample.phi = tImportanceSample.phi + pl_random(tN.xz) * 0.1;
255+
246256
// transform the hemisphere sample to the normal coordinate frame
247257
// i.e. rotate the hemisphere to the normal direction
248258
const vec3 tLocalSpaceDirection = normalize(vec3(

0 commit comments

Comments
 (0)