Browse Source

Ongoing tweaks to shaders to puzzle out good blending behavior.
Also tried optimization by shifting all the brdf samples to a single one for the final add-up.

Areloch 6 years ago
parent
commit
29660ce58b

+ 2 - 2
Engine/source/renderInstance/renderProbeMgr.cpp

@@ -434,8 +434,8 @@ void RenderProbeMgr::_setupStaticParameters()
       probeRefPositionsData[mEffectiveProbeCount] = Point4F(refPos.x, refPos.y, refPos.z, 0);
 
       probeWorldToObjData[mEffectiveProbeCount] = curEntry.getTransform();
-      Point3F bbMin = refPos - curEntry.mProbeRefScale/2;
-      Point3F bbMax = refPos + curEntry.mProbeRefScale/2;
+      Point3F bbMin = refPos - curEntry.mProbeRefScale/2 * curEntry.getTransform().getScale();
+      Point3F bbMax = refPos + curEntry.mProbeRefScale/2 * curEntry.getTransform().getScale();
       probeBBMinData[mEffectiveProbeCount] = Point4F(bbMin.x, bbMin.y, bbMin.z, 0);
       probeBBMaxData[mEffectiveProbeCount] = Point4F(bbMax.x, bbMax.y, bbMax.z, 0);
 

+ 22 - 9
Templates/Full/game/levels/AProbeTest.mis

@@ -148,7 +148,7 @@ new SimGroup(MissionGroup) {
 
       surface = "0 0 0 1 0 0 0.5";
       surface = "0 1 0 0 0 0 -0.5";
-      surface = "0.707107 0 0 0.707106 0 0.5 0";
+      surface = "0.707107 0 0 0.707107 0 0.5 0";
       surface = "0 0.707107 -0.707107 0 0 -0.5 -2.84217e-14";
       surface = "0.5 0.5 -0.5 0.5 -0.5 0 -9.93411e-08";
       surface = "0.5 -0.5 0.5 0.5 0.5 0 -9.93411e-08";
@@ -163,7 +163,7 @@ new SimGroup(MissionGroup) {
 
       surface = "0 0 0 1 0 0 0.5";
       surface = "0 1 0 0 0 0 -0.5";
-      surface = "0.707107 0 0 0.707106 0 0.5 0";
+      surface = "0.707107 0 0 0.707107 0 0.5 0";
       surface = "0 0.707107 -0.707107 0 0 -0.5 -2.84217e-14";
       surface = "0.5 0.5 -0.5 0.5 -0.5 0 -9.93411e-08";
       surface = "0.5 -0.5 0.5 0.5 0.5 0 -9.93411e-08";
@@ -178,7 +178,7 @@ new SimGroup(MissionGroup) {
 
       surface = "0 0 0 1 0 0 0.5";
       surface = "0 1 0 0 0 0 -0.5";
-      surface = "0.707107 0 0 0.707107 0 0.5 0";
+      surface = "0.707107 0 0 0.707106 0 0.5 0";
       surface = "0 0.707107 -0.707107 0 0 -0.5 -2.84217e-14";
       surface = "0.5 0.5 -0.5 0.5 -0.5 0 -9.93411e-08";
       surface = "0.5 -0.5 0.5 0.5 0.5 0 -9.93411e-08";
@@ -193,7 +193,7 @@ new SimGroup(MissionGroup) {
 
       surface = "0 0 0 1 0 0 0.5";
       surface = "0 1 0 0 0 0 -0.5";
-      surface = "0.707107 0 0 0.707107 0 0.5 0";
+      surface = "0.707107 0 0 0.707106 0 0.5 0";
       surface = "0 0.707107 -0.707107 0 0 -0.5 -2.84217e-14";
       surface = "0.5 0.5 -0.5 0.5 -0.5 0 -9.93411e-08";
       surface = "0.5 -0.5 0.5 0.5 0.5 0 -9.93411e-08";
@@ -208,7 +208,7 @@ new SimGroup(MissionGroup) {
 
       surface = "0 0 0 1 0 0 0.5";
       surface = "0 1 0 0 0 0 -0.5";
-      surface = "0.707107 0 0 0.707107 0 0.5 0";
+      surface = "0.707107 0 0 0.707106 0 0.5 0";
       surface = "0 0.707107 -0.707107 0 0 -0.5 -2.84217e-14";
       surface = "0.5 0.5 -0.5 0.5 -0.5 0 -9.93411e-08";
       surface = "0.5 -0.5 0.5 0.5 0.5 0 -9.93411e-08";
@@ -223,7 +223,7 @@ new SimGroup(MissionGroup) {
 
       surface = "0 0 0 1 0 0 0.5";
       surface = "0 1 0 0 0 0 -0.5";
-      surface = "0.707107 0 0 0.707106 0 0.5 0";
+      surface = "0.707107 0 0 0.707107 0 0.5 0";
       surface = "0 0.707107 -0.707107 0 0 -0.5 -2.84217e-14";
       surface = "0.5 0.5 -0.5 0.5 -0.5 0 -9.93411e-08";
       surface = "0.5 -0.5 0.5 0.5 0.5 0 -9.93411e-08";
@@ -238,7 +238,7 @@ new SimGroup(MissionGroup) {
 
       surface = "0 0 0 1 0 0 0.5";
       surface = "0 1 0 0 0 0 -0.5";
-      surface = "0.707107 0 0 0.707107 0 0.5 0";
+      surface = "0.707107 0 0 0.707106 0 0.5 0";
       surface = "0 0.707107 -0.707107 0 0 -0.5 -2.84217e-14";
       surface = "0.5 0.5 -0.5 0.5 -0.5 0 -9.93411e-08";
       surface = "0.5 -0.5 0.5 0.5 0.5 0 -9.93411e-08";
@@ -310,13 +310,26 @@ new SimGroup(MissionGroup) {
       refOffset = "0 0 0";
       refScale = "10 10 10";
       ReflectionMode = "Baked Cubemap";
-      position = "-22.7696 0.224002 4.70918";
+      position = "-22.7696 0.224002 1.72318";
       rotation = "1 0 0 0";
       scale = "10 10 10";
       canSave = "1";
       canSaveDynamicFields = "1";
       persistentId = "d84cbe5b-4f76-11e9-977c-a561a736e3eb";
-      attenuation = "0";
+      attenuation = "1";
+   };
+   new BoxEnvironmentProbe() {
+      enabled = "1";
+      refOffset = "0 0 0";
+      refScale = "10 10 10";
+      ReflectionMode = "Baked Cubemap";
+      position = "-23.5985 -8.60824 2.16156";
+      rotation = "0 0 1 37.5292";
+      scale = "10 10 10";
+      canSave = "1";
+      canSaveDynamicFields = "1";
+      persistentId = "db05bf42-503b-11e9-8eaf-b5e1839b86e6";
+      attenuation = "1";
    };
 };
 //--- OBJECT WRITE END ---

+ 45 - 43
Templates/Full/game/shaders/common/lighting/advanced/reflectionProbeArrayP.hlsl

@@ -111,7 +111,7 @@ float3 iblBoxDiffuse(Surface surface, ProbeData probe)
 float3 iblBoxSpecular(Surface surface, ProbeData probe)
 {
    // BRDF
-   float2 brdf = TORQUE_TEX2DLOD(BRDFTexture, float4(surface.roughness, surface.NdotV, 0.0, 0.0)).xy;
+   //float2 brdf = TORQUE_TEX2DLOD(BRDFTexture, float4(surface.roughness, surface.NdotV, 0.0, 0.0)).xy;
 
    float3 dir = boxProject(surface, probe);
 
@@ -122,7 +122,7 @@ float3 iblBoxSpecular(Surface surface, ProbeData probe)
    float lod = 0;
 #endif
 
-   float3 color = TORQUE_TEXCUBEARRAYLOD(cubeMapAR, dir, probe.cubemapIdx, lod).xyz * (brdf.x + brdf.y);
+   float3 color = TORQUE_TEXCUBEARRAYLOD(cubeMapAR, dir, probe.cubemapIdx, lod).xyz;
    return color;
 }
 
@@ -135,7 +135,7 @@ float3 iblSkylightDiffuse(Surface surface)
 float3 iblSkylightSpecular(Surface surface)
 {
    // BRDF
-   float2 brdf = TORQUE_TEX2DLOD(BRDFTexture, float4(surface.roughness, surface.NdotV, 0.0, 0.0)).xy;
+   //float2 brdf = TORQUE_TEX2DLOD(BRDFTexture, float4(surface.roughness, surface.NdotV, 0.0, 0.0)).xy;
 
    // Radiance (Specular)
 #if DEBUGVIZ_SPECCUBEMAP == 0
@@ -144,7 +144,7 @@ float3 iblSkylightSpecular(Surface surface)
    float lod = 0;
 #endif
 
-   float3 color = TORQUE_TEXCUBELOD(skylightPrefilterMap, float4(surface.R, lod)).xyz * (brdf.x + brdf.y);
+   float3 color = TORQUE_TEXCUBELOD(skylightPrefilterMap, float4(surface.R, lod)).xyz;
    return color;
 }
 
@@ -202,13 +202,12 @@ float4 main(PFXVertToPix IN) : SV_TARGET
             probehits++;
          }
 
-         if (probes[i].contribution>1 || probes[i].contribution<0)
-            probes[i].contribution = 0;
+         //probes[i].contribution = max(probes[i].contribution, 0);
 
          blendSum += probes[i].contribution;
          invBlendSum += (1.0f - probes[i].contribution);
 
-         alpha -= probes[i].contribution;
+         //alpha -= probes[i].contribution;
       }
 
       // Weight0 = normalized NDF, inverted to have 1 at center, 0 at boundary.
@@ -216,20 +215,20 @@ float4 main(PFXVertToPix IN) : SV_TARGET
 	   // respect constraint B.
 	   // Weight1 = normalized inverted NDF, so we have 1 at center, 0 at boundary
 	   // and respect constraint A.
-	   for (i = 0; i < numProbes; i++)
-	   {
-	      if (probehits>1.0)
-	      {
-	         blendFactor[i] = ((probes[i].contribution / blendSum)) / (probehits - 1);
-	         blendFactor[i] *= ((probes[i].contribution) / invBlendSum);
-	         blendFacSum += blendFactor[i];
-	      }
-	      else
-	      {
-	         blendFactor[i] = probes[i].contribution;
-	         blendFacSum = probes[i].contribution;
-	      }
-	   }
+      if (probehits>1.0)
+      {
+         for (i = 0; i < numProbes; i++)
+         {
+            blendFactor[i] = ((probes[i].contribution / blendSum)) / (probehits - 1);
+            blendFactor[i] *= ((probes[i].contribution) / invBlendSum);
+            blendFacSum += blendFactor[i];
+         }
+      }
+      else
+      {
+         blendFactor[i] = probes[i].contribution;
+         blendFacSum = probes[i].contribution;
+      }
 
 
       // Normalize blendVal
@@ -246,9 +245,10 @@ float4 main(PFXVertToPix IN) : SV_TARGET
 		  for (i = 0; i < numProbes; ++i)
 		  {
 		     blendFactor[i] *= invBlendSumWeighted;
-		     probes[i].contribution = saturate(blendFactor[i]);
+		     //probes[i].contribution = saturate(blendFactor[i]);
+           probes[i].contribution *= blendFactor[i];
 
-			alpha -= probes[i].contribution;
+			  alpha -= probes[i].contribution;
 		  }
 		}
 
@@ -258,18 +258,16 @@ float4 main(PFXVertToPix IN) : SV_TARGET
 		{
 		  attenVis += probes[i].contribution;
 		}
-		return float4(attenVis, attenVis, attenVis, 1);
+
+		//return float4(attenVis, attenVis, attenVis, 1);
+      return float4(blendSum, blendSum, blendSum, 1);
 #endif
 
 #if DEBUGVIZ_CONTRIB == 1
-
       float3 finalContribColor = float3(0, 0, 0);
       for (i = 0; i < numProbes; ++i)
       {
-         if (probes[i].contribution == 0)
-            continue;
-
-         finalContribColor += probes[i].contribution * probeContribColors[i].rgb;
+         finalContribColor += probes[i].contribution *probeContribColors[i].rgb;
       }
 
       return float4(finalContribColor, 1);
@@ -280,32 +278,36 @@ float4 main(PFXVertToPix IN) : SV_TARGET
 
    float3 irradiance = float3(0, 0, 0);
    float3 specular = float3(0, 0, 0);
-   float3 F = FresnelSchlickRoughness(surface.NdotV, surface.f0, surface.roughness);
 
-   //energy conservation
-   float3 kD = 1.0.xxx - F;
-   kD *= 1.0 - surface.metalness;
-   float contrib = 0;
+   //float contrib = 0;
    for (i = 0; i < numProbes; ++i)
    {
       if (probes[i].contribution == 0)
          continue;
 
-      if (probes[i].type == 2) //skip skylight
-         continue;
-
-      irradiance += iblBoxDiffuse(surface, probes[i])*probes[i].contribution;
-      specular += F*iblBoxSpecular(surface, probes[i])*probes[i].contribution;
-      contrib +=probes[i].contribution;
+      irradiance += iblBoxDiffuse(surface, probes[i]) * probes[i].contribution;
+      specular += iblBoxSpecular(surface, probes[i]) * probes[i].contribution;
+      //contrib +=probes[i].contribution;
    }
    //contrib = saturate(contrib);
 
-   if (hasSkylight && alpha != 0)
+   if (hasSkylight && alpha != 1)
    {
-      irradiance = lerp(irradiance, iblSkylightDiffuse(surface), alpha);
-      specular = lerp(specular, F*iblSkylightSpecular(surface), alpha);
+      irradiance = lerp(irradiance, iblSkylightDiffuse(surface), blendSum);
+      specular = lerp(specular, iblSkylightSpecular(surface), blendSum);
    }
 
+   float3 F = FresnelSchlickRoughness(surface.NdotV, surface.f0, surface.roughness);
+
+   //energy conservation
+   float3 kD = 1.0.xxx - F;
+   kD *= 1.0 - surface.metalness;
+
+   //apply brdf
+   //Do it once to save on texture samples
+   float2 brdf = TORQUE_TEX2DLOD(BRDFTexture, float4(surface.roughness, surface.NdotV, 0.0, 0.0)).xy;
+   specular *= brdf.x * F + brdf.y;
+
    //final diffuse color
    float3 diffuse = kD * irradiance * surface.baseColor.rgb;
    float4 finalColor = float4(diffuse + specular * surface.ao, 1.0);