Benjamin Guignabert пре 5 година
родитељ
комит
9f3b4ed70a
2 измењених фајлова са 88 додато и 88 уклоњено
  1. 14 14
      src/Shaders/ShadersInclude/fibonacci.fx
  2. 74 74
      src/Shaders/subSurfaceScattering.fragment.fx

+ 14 - 14
src/Shaders/ShadersInclude/fibonacci.fx

@@ -23,11 +23,11 @@ vec2 Golden2dSeq(int i, float n)
     return vec2(float(i) / n + (0.5 / n), fract(float(i) * rcp(GOLDEN_RATIO)));
 }
 
-int k_FibonacciSeq[] = {
+int k_FibonacciSeq[20] = int[20](
     0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181
-};
+);
 
-vec2 k_Fibonacci2dSeq21[] = {
+vec2 k_Fibonacci2dSeq21[21] = vec2[21](
     vec2(0.02380952, 0.00000000),
     vec2(0.07142857, 0.61904764),
     vec2(0.11904762, 0.23809528),
@@ -49,9 +49,9 @@ vec2 k_Fibonacci2dSeq21[] = {
     vec2(0.88095236, 0.14285755),
     vec2(0.92857140, 0.76190567),
     vec2(0.97619045, 0.38095284)
-};
+);
 
-vec2 k_Fibonacci2dSeq34[] = {
+vec2 k_Fibonacci2dSeq34[34] = vec2[34](
     vec2(0.01470588, 0.00000000),
     vec2(0.04411765, 0.61764705),
     vec2(0.07352941, 0.23529410),
@@ -86,9 +86,9 @@ vec2 k_Fibonacci2dSeq34[] = {
     vec2(0.92647058, 0.14705849),
     vec2(0.95588237, 0.76470566),
     vec2(0.98529410, 0.38235283)
-};
+);
 
-vec2 k_Fibonacci2dSeq55[] = {
+vec2 k_Fibonacci2dSeq55[55] = vec2[55](
     vec2(0.00909091, 0.00000000),
     vec2(0.02727273, 0.61818182),
     vec2(0.04545455, 0.23636365),
@@ -144,9 +144,9 @@ vec2 k_Fibonacci2dSeq55[] = {
     vec2(0.95454544, 0.14545441),
     vec2(0.97272730, 0.76363754),
     vec2(0.99090910, 0.38181686)
-};
+);
 
-vec2 k_Fibonacci2dSeq89[] = {
+vec2 k_Fibonacci2dSeq89[89] = vec2[89](
     vec2(0.00561798, 0.00000000),
     vec2(0.01685393, 0.61797750),
     vec2(0.02808989, 0.23595500),
@@ -236,7 +236,7 @@ vec2 k_Fibonacci2dSeq89[] = {
     vec2(0.97191012, 0.14606476),
     vec2(0.98314607, 0.76404190),
     vec2(0.99438202, 0.38201904)
-};
+);
 
 // Loads elements from one of the precomputed tables for sample counts of 21, 34, 55, and 89.
 // Computes sample positions at runtime otherwise.
@@ -263,14 +263,14 @@ vec2 Fibonacci2d(int i, int sampleCount)
                 }
             }
 
-            return Fibonacci2dSeq(fibN1, fibN2, i);
+            return Fibonacci2dSeq(float(fibN1), float(fibN2), i);
         }
     }
 }
 
 vec2 SampleDiskGolden(int i, int sampleCount)
 {
-    vec2 f = Golden2dSeq(i, sampleCount);
+    vec2 f = Golden2dSeq(i, float(sampleCount));
     return vec2(sqrt(f.x), TWO_PI * f.y);
 }
 
@@ -285,12 +285,12 @@ vec2 SampleDiskFibonacci(int i, int sampleCount)
 vec2 SampleHemisphereFibonacci(int i, int sampleCount)
 {
     vec2 f = Fibonacci2d(i, sampleCount);
-    return vec2(1 - f.x, TWO_PI * f.y);
+    return vec2(1. - f.x, TWO_PI * f.y);
 }
 
 // Returns the zenith as the X coordinate, and the azimuthal angle as the Y coordinate.
 vec2 SampleSphereFibonacci(int i, int sampleCount)
 {
     vec2 f = Fibonacci2d(i, sampleCount);
-    return vec2(1 - 2 * f.x, TWO_PI * f.y);
+    return vec2(1. - 2. * f.x, TWO_PI * f.y);
 }

+ 74 - 74
src/Shaders/subSurfaceScattering.fragment.fx

@@ -2,7 +2,7 @@
 #include<fibonacci>
 
 varying vec2 vUV;
-varying vec2 texelSize;
+uniform vec2 texelSize;
 uniform sampler2D textureSampler;
 uniform sampler2D irradianceSampler;
 
@@ -11,7 +11,7 @@ uniform vec2 viewportSize;
 
 const float LOG2_E = 1.4426950408889634;
 const float PI = 3.1415926535897932;
-const int SSS_PIXELS_PER_SAMPLE = 1;
+const float SSS_PIXELS_PER_SAMPLE = 1.;
 const int _SssSampleBudget = 32;
 
 #define rcp(x) 1. / x
@@ -57,6 +57,73 @@ vec2 SampleBurleyDiffusionProfile(float u, float rcpS)
 }
 
 
+// TODO : inout vec3 totalIrradiance, inout vec3 totalWeight
+void EvaluateSample(int i, int n, vec3 S, float d, vec3 centerPosVS, float mmPerUnit, float pixelsPerMm,
+                    float phase, vec3 tangentX, vec3 tangentY,
+                    vec3 totalIrradiance, vec3 totalWeight)
+{
+    // The sample count is loop-invariant.
+    float scale  = rcp(float(n));
+    float offset = rcp(float(n)) * 0.5;
+
+    // The phase angle is loop-invariant.
+    float sinPhase, cosPhase;
+    sinPhase = sin(phase);
+    cosPhase = cos(phase);
+
+    vec2 bdp = SampleBurleyDiffusionProfile(float(i) * scale + offset, d);
+    float r = bdp.x;
+    float rcpPdf = bdp.y;
+
+    float phi = SampleDiskGolden(i, n).y;
+    float sinPhi, cosPhi;
+    sinPhi = sin(phi);
+    cosPhi = cos(phi);
+
+    float sinPsi = cosPhase * sinPhi + sinPhase * cosPhi; // sin(phase + phi)
+    float cosPsi = cosPhase * cosPhi - sinPhase * sinPhi; // cos(phase + phi)
+
+    vec2 vec = r * vec2(cosPsi, sinPsi);
+
+    // Compute the screen-space position and the squared distance (in mm) in the image plane.
+    vec2 position; 
+    float xy2;
+
+    // TODO : TANGENT PLANE
+    // floor((vUV + 0.5) + vec * pixelsPerMm)
+    // position = vUV + floor(0.5 + vec * pixelsPerMm);
+    // position = vUV + round(vec * pixelsPerMm);
+    // Note that (int) truncates towards 0, while floor() truncates towards -Inf!
+
+    position = vUV + round((pixelsPerMm * r) * vec2(cosPsi, sinPsi)) * texelSize;
+    xy2      = r * r;
+
+    vec4 textureSample = texture2D(irradianceSampler, position);
+    vec3 irradiance    = textureSample.rgb;
+
+    // Check the results of the stencil test.
+    if (true) //TestLightingForSSS(irradiance))
+    {
+        // Apply bilateral weighting.
+        // float  viewZ  = textureSample.a;
+        // float  relZ   = viewZ - centerPosVS.z;
+        vec3 weight = vec3(1.); //ComputeBilateralWeight(xy2, relZ, mmPerUnit, S, rcpPdf);
+
+        // Note: if the texture sample if off-screen, (z = 0) -> (viewZ = far) -> (weight ≈ 0).
+        totalIrradiance += weight * irradiance;
+        totalWeight     += weight;
+    }
+    else
+    {
+        // The irradiance is 0. This could happen for 2 reasons.
+        // Most likely, the surface fragment does not have an SSS material.
+        // Alternatively, our sample comes from a region without any geometry.
+        // Our blur is energy-preserving, so 'centerWeight' should be set to 0.
+        // We do not terminate the loop since we want to gather the contribution
+        // of the remaining samples (e.g. in case of hair covering skin).
+    }
+}
+
 void main(void) 
 {
 	vec3 centerIrradiance  = texture2D(irradianceSampler, vUV).rgb;
@@ -65,7 +132,7 @@ void main(void)
 
 	if (passedStencilTest)
 	{
-	    centerDepth = 0.; //texture2D(depthSampler, vUV).r; -> NDC !
+	    centerDepth = 0.; //texture2D(depthSampler, vUV).r NDC
 	}
 
     if (!passedStencilTest) { return; }
@@ -85,8 +152,8 @@ void main(void)
 
     // TODO : uniforms
 	float  distScale     = 1.; //sssData.subsurfaceMask;
-	vec3 S             = vec3(0.7568628, 0.32156864, 0.20000002); //_ShapeParamsAndMaxScatterDists[profileIndex].rgb; -> diffusion color
-	float  d             = 0.7568628; //_ShapeParamsAndMaxScatterDists[profileIndex].a; -> max scatter dist
+	vec3 S             = vec3(0.7568628, 0.32156864, 0.20000002); //_ShapeParamsAndMaxScatterDists[profileIndex].rgb diffusion color
+	float  d             = 0.7568628; //_ShapeParamsAndMaxScatterDists[profileIndex].a max scatter dist
 	float  metersPerUnit = 0.01; //_WorldScalesAndFilterRadiiAndThicknessRemaps[profileIndex].x;
 
 	// Reconstruct the view-space position corresponding to the central sample.
@@ -106,11 +173,11 @@ void main(void)
 
 	// Area of a disk.
 	float filterArea   = PI * Sq(filterRadius * pixelsPerMm);
-	int  sampleCount  = (int)(filterArea * rcp(SSS_PIXELS_PER_SAMPLE));
+	int  sampleCount  = int(filterArea * rcp(SSS_PIXELS_PER_SAMPLE));
 	int  sampleBudget = _SssSampleBudget;
 
 	int texturingMode = 0; // GetSubsurfaceScatteringTexturingMode(profileIndex);
-	vec3 albedo  = vec3(0.5) // texture2D(albedoSampler, vUV); //ApplySubsurfaceScatteringTexturingMode(texturingMode, sssData.diffuseColor);
+	vec3 albedo  = vec3(0.5); // texture2D(albedoSampler, vUV); //ApplySubsurfaceScatteringTexturingMode(texturingMode, sssData.diffuseColor);
 
 	if (distScale == 0. || sampleCount < 1)
 	{
@@ -146,71 +213,4 @@ void main(void)
     gl_FragColor = vec4(albedo * (totalIrradiance / totalWeight), 1.);
 
 	// gl_FragColor = mix(texture2D(textureSampler, vUV), centerIrradiance, 0.5);
-}
-
-// TODO : inout vec3 totalIrradiance, inout vec3 totalWeight
-void EvaluateSample(uint i, uint n, vec3 S, float d, vec3 centerPosVS, float mmPerUnit, float pixelsPerMm,
-                    float phase, vec3 tangentX, vec3 tangentY,
-                    vec3 totalIrradiance, vec3 totalWeight)
-{
-    // The sample count is loop-invariant.
-    const float scale  = rcp(n);
-    const float offset = rcp(n) * 0.5;
-
-    // The phase angle is loop-invariant.
-    float sinPhase, cosPhase;
-    sinPhase = sin(phase);
-    cosPhase = cos(phase);
-
-    vec2 bdp = SampleBurleyDiffusionProfile(i * scale + offset, d);
-    float r = bdp.x;
-    float rcpPdf = bdp.y;
-
-    float phi = SampleDiskGolden(i, n).y;
-    float sinPhi, cosPhi;
-    sinPhi = sin(phi);
-    cosPhi = cos(phi);
-
-    float sinPsi = cosPhase * sinPhi + sinPhase * cosPhi; // sin(phase + phi)
-    float cosPsi = cosPhase * cosPhi - sinPhase * sinPhi; // cos(phase + phi)
-
-    vec2 vec = r * vec2(cosPsi, sinPsi);
-
-    // Compute the screen-space position and the squared distance (in mm) in the image plane.
-    vec2 position; 
-    float xy2;
-
-    // TODO : TANGENT PLANE
-    // floor((vUV + 0.5) + vec * pixelsPerMm)
-    // position = vUV + floor(0.5 + vec * pixelsPerMm);
-    // position = vUV + round(vec * pixelsPerMm);
-    // Note that (int) truncates towards 0, while floor() truncates towards -Inf!
-
-    position = vUV + round((pixelsPerMm * r) * vec2(cosPsi, sinPsi)) * texelSize;
-    xy2      = r * r;
-
-    vec4 textureSample = sampler2D(irradianceSampler, position);
-    vec3 irradiance    = textureSample.rgb;
-
-    // Check the results of the stencil test.
-    if (true) //TestLightingForSSS(irradiance))
-    {
-        // Apply bilateral weighting.
-        // float  viewZ  = textureSample.a;
-        // float  relZ   = viewZ - centerPosVS.z;
-        vec3 weight = vec3(1.); //ComputeBilateralWeight(xy2, relZ, mmPerUnit, S, rcpPdf);
-
-        // Note: if the texture sample if off-screen, (z = 0) -> (viewZ = far) -> (weight ≈ 0).
-        totalIrradiance += weight * irradiance;
-        totalWeight     += weight;
-    }
-    else
-    {
-        // The irradiance is 0. This could happen for 2 reasons.
-        // Most likely, the surface fragment does not have an SSS material.
-        // Alternatively, our sample comes from a region without any geometry.
-        // Our blur is energy-preserving, so 'centerWeight' should be set to 0.
-        // We do not terminate the loop since we want to gather the contribution
-        // of the remaining samples (e.g. in case of hair covering skin).
-    }
 }