Browse Source

Merge branch 'PBR_ProbeArrayGLWIP' of https://github.com/Areloch/Torque3D into PBR_ProbeArrayGLWIP

AzaezelX 6 years ago
parent
commit
26b3ecff4c

+ 18 - 41
Engine/source/gfx/gl/gfxGLCubemap.cpp

@@ -343,27 +343,28 @@ void GFXGLCubemapArray::init(GFXCubemapHandle *cubemaps, const U32 cubemapCount)
    glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
 
 
-   glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, GFXGLTextureInternalFormat[mFormat], mSize, mSize, cubemapCount * 6, 0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], NULL);
-
    for (U32 i = 0; i < cubemapCount; i++)
    for (U32 i = 0; i < cubemapCount; i++)
    {
    {
       GFXGLCubemap* glTex = static_cast<GFXGLCubemap*>(cubemaps[i].getPointer());
       GFXGLCubemap* glTex = static_cast<GFXGLCubemap*>(cubemaps[i].getPointer());
       for (U32 face = 0; face < 6; face++)
       for (U32 face = 0; face < 6; face++)
       {
       {
          for (U32 currentMip = 0; currentMip < mMipMapLevels; currentMip++)
          for (U32 currentMip = 0; currentMip < mMipMapLevels; currentMip++)
-         //U32 currentMip = 0;
          {
          {
             U8 *pixelData = glTex->getTextureData(face, currentMip);
             U8 *pixelData = glTex->getTextureData(face, currentMip);
+
+            glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
             const U32 mipSize = getMax(U32(1), mSize >> currentMip);
             const U32 mipSize = getMax(U32(1), mSize >> currentMip);
-            /*if (isCompressed)
+            if (isCompressed)
             {
             {
                const U32 mipDataSize = getCompressedSurfaceSize(mFormat, mSize, mSize, currentMip);
                const U32 mipDataSize = getCompressedSurfaceSize(mFormat, mSize, mSize, currentMip);
-               glCompressedTexImage2D(faceList[face], currentMip, GFXGLTextureInternalFormat[mFormat], mipSize, mipSize, 0, mipDataSize, pixelData);
+               glCompressedTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, currentMip, GFXGLTextureInternalFormat[mFormat], 0, 0, i * 6 + face, 0, mipDataSize, pixelData);
             }
             }
             else
             else
-            {*/                                                      //TODO figure out xyzOffsets
-            glTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, currentMip, 0, 0, 0, mipSize, mipSize, i * face, GL_RGBA, GFXGLTextureType[mFormat], pixelData);
-            //}
+            {
+               glTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, currentMip, 0, 0, i * 6 + face, mipSize, mipSize, 1, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], pixelData);
+            }
+            glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, 0);
+
             delete[] pixelData;
             delete[] pixelData;
          }
          }
       }
       }
@@ -396,32 +397,6 @@ void GFXGLCubemapArray::init(const U32 cubemapCount, const U32 cubemapFaceSize,
    glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
-
-   
-
-   /*for (U32 i = 0; i < cubemapCount; i++)
-   {
-      GFXGLCubemap* glTex = static_cast<GFXGLCubemap*>(cubemaps[i].getPointer());
-      for (U32 face = 0; face < 6; face++)
-      {
-         for (U32 currentMip = 0; currentMip < mMipMapLevels; currentMip++)
-         {
-            U8 *pixelData = glTex->getTextureData(face, currentMip);
-            const U32 mipSize = getMax(U32(1), mSize >> currentMip);
-            if (isCompressed)
-            {
-               const U32 mipDataSize = getCompressedSurfaceSize(mFormat, mSize, mSize, currentMip);
-               glCompressedTexImage2D(faceList[face], currentMip, GFXGLTextureInternalFormat[mFormat], mipSize, mipSize, 0, mipDataSize, pixelData);
-            }
-            else
-            {
-               glTexImage2D(faceList[face], currentMip, GFXGLTextureInternalFormat[mFormat], mipSize, mipSize,
-                  0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], pixelData);
-            }
-            delete[] pixelData;
-         }
-      }
-   }*/
 }
 }
 
 
 void GFXGLCubemapArray::updateTexture(const GFXCubemapHandle &cubemap, const U32 slot)
 void GFXGLCubemapArray::updateTexture(const GFXCubemapHandle &cubemap, const U32 slot)
@@ -431,24 +406,26 @@ void GFXGLCubemapArray::updateTexture(const GFXCubemapHandle &cubemap, const U32
       return;
       return;
    const bool isCompressed = ImageUtil::isCompressedFormat(mFormat);
    const bool isCompressed = ImageUtil::isCompressedFormat(mFormat);
 
 
-    GFXGLCubemap* glTex = static_cast<GFXGLCubemap*>(cubemap.getPointer());
+   GFXGLCubemap* glTex = static_cast<GFXGLCubemap*>(cubemap.getPointer());
    for (U32 face = 0; face < 6; face++)
    for (U32 face = 0; face < 6; face++)
    {
    {
       for (U32 currentMip = 0; currentMip < mMipMapLevels; currentMip++)
       for (U32 currentMip = 0; currentMip < mMipMapLevels; currentMip++)
       {
       {
          U8 *pixelData = glTex->getTextureData(face, currentMip);
          U8 *pixelData = glTex->getTextureData(face, currentMip);
+
+         glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
          const U32 mipSize = getMax(U32(1), mSize >> currentMip);
          const U32 mipSize = getMax(U32(1), mSize >> currentMip);
-         /*if (isCompressed)
+         if (isCompressed)
          {
          {
             const U32 mipDataSize = getCompressedSurfaceSize(mFormat, mSize, mSize, currentMip);
             const U32 mipDataSize = getCompressedSurfaceSize(mFormat, mSize, mSize, currentMip);
-            glCompressedTexImage2D(faceList[face], currentMip, GFXGLTextureInternalFormat[mFormat], mipSize, mipSize, 0, mipDataSize, pixelData);
+            glCompressedTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, currentMip, GFXGLTextureInternalFormat[mFormat], 0, 0, slot * 6 + face, 0, mipDataSize, pixelData);
          }
          }
          else
          else
-         {*/                                                      //TODO figure out xyzOffsets
-         glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
-         glTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, currentMip, 0, 0, slot * 6 + face, mipSize, mipSize, 1, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], pixelData);
+         {                                          
+            glTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, currentMip, 0, 0, slot * 6 + face, mipSize, mipSize, 1, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], pixelData);
+         }
          glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, 0);
          glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, 0);
-         //}
+
          delete[] pixelData;
          delete[] pixelData;
       }
       }
    }
    }

+ 50 - 9
Engine/source/renderInstance/renderProbeMgr.cpp

@@ -171,6 +171,19 @@ void ProbeShaderConstants::init(GFXShader* shader)
    mInit = true;
    mInit = true;
 }
 }
 
 
+bool ProbeShaderConstants::isValid()
+{
+   if (mProbePositionSC->isValid() ||
+      mProbeConfigDataSC->isValid() ||
+      mProbeBoxMinSC->isValid() ||
+      mProbeBoxMaxSC->isValid() ||
+      mProbeSpecularCubemapSC->isValid() ||
+      mProbeIrradianceCubemapSC->isValid())
+      return true;
+
+   return false;
+}
+
 void ProbeShaderConstants::_onShaderReload()
 void ProbeShaderConstants::_onShaderReload()
 {
 {
    if (mShader.isValid())
    if (mShader.isValid())
@@ -536,12 +549,7 @@ void RenderProbeMgr::_update4ProbeConsts(const SceneData &sgData,
    PROFILE_SCOPE(ProbeManager_Update4ProbeConsts);
    PROFILE_SCOPE(ProbeManager_Update4ProbeConsts);
 
 
    // Skip over gathering lights if we don't have to!
    // Skip over gathering lights if we don't have to!
-   if (probeShaderConsts->mProbePositionSC->isValid() ||
-      probeShaderConsts->mProbeConfigDataSC->isValid() ||
-      probeShaderConsts->mProbeBoxMinSC->isValid() ||
-      probeShaderConsts->mProbeBoxMaxSC->isValid() ||
-      probeShaderConsts->mProbeSpecularCubemapSC->isValid() ||
-      probeShaderConsts->mProbeIrradianceCubemapSC->isValid()/* && (!ProbeRenderInst::all.empty())*/)
+   if (probeShaderConsts->isValid())
    {
    {
       PROFILE_SCOPE(ProbeManager_Update4ProbeConsts_setProbes);
       PROFILE_SCOPE(ProbeManager_Update4ProbeConsts_setProbes);
 
 
@@ -653,7 +661,7 @@ void RenderProbeMgr::_update4ProbeConsts(const SceneData &sgData,
          if (!curEntry.mIsEnabled)
          if (!curEntry.mIsEnabled)
             continue;
             continue;
 
 
-         if (curEntry.mIsSkylight)
+         /*if (curEntry.mIsSkylight)
          {
          {
             if (curEntry.mPrefilterCubemap.isValid() && curEntry.mPrefilterCubemap.isValid())
             if (curEntry.mPrefilterCubemap.isValid() && curEntry.mPrefilterCubemap.isValid())
             {
             {
@@ -664,8 +672,8 @@ void RenderProbeMgr::_update4ProbeConsts(const SceneData &sgData,
                hasSkylight = true;
                hasSkylight = true;
                continue;
                continue;
             }
             }
-         }
-         else
+         }*/
+         if(!curEntry.mIsSkylight)
          {
          {
             /*probePositions[effectiveProbeCount] = curEntry.getPosition();
             /*probePositions[effectiveProbeCount] = curEntry.getPosition();
             probeRefPositions[effectiveProbeCount] = curEntry.mProbeRefOffset;
             probeRefPositions[effectiveProbeCount] = curEntry.mProbeRefOffset;
@@ -694,6 +702,39 @@ void RenderProbeMgr::_update4ProbeConsts(const SceneData &sgData,
       //GFX->setCubeArrayTexture(probeShaderConsts->mProbeSpecularCubemapSC->getSamplerRegister(), mPrefilterArray);
       //GFX->setCubeArrayTexture(probeShaderConsts->mProbeSpecularCubemapSC->getSamplerRegister(), mPrefilterArray);
       //GFX->setCubeArrayTexture(probeShaderConsts->mProbeIrradianceCubemapSC->getSamplerRegister(), mIrradianceArray);
       //GFX->setCubeArrayTexture(probeShaderConsts->mProbeIrradianceCubemapSC->getSamplerRegister(), mIrradianceArray);
 
 
+      //if (!hasSkylight)
+      //   shaderConsts->setSafe(probeShaderConsts->mHasSkylight, 0.0f);
+   }
+
+   //check for skylight action
+   if (probeShaderConsts->mHasSkylight->isValid()
+      && probeShaderConsts->mSkylightIrradMap->isValid() 
+      && probeShaderConsts->mSkylightSpecularMap->isValid())
+   {
+      //Array rendering
+      U32 probeCount = ProbeRenderInst::all.size();
+
+      bool hasSkylight = false;
+      for (U32 i = 0; i < probeCount; i++)
+      {
+         const ProbeRenderInst& curEntry = *ProbeRenderInst::all[i];
+         if (!curEntry.mIsEnabled)
+            continue;
+
+         if (curEntry.mIsSkylight)
+         {
+            if (curEntry.mPrefilterCubemap.isValid() && curEntry.mPrefilterCubemap.isValid())
+            {
+               GFX->setCubeTexture(probeShaderConsts->mSkylightSpecularMap->getSamplerRegister(), curEntry.mPrefilterCubemap);
+               GFX->setCubeTexture(probeShaderConsts->mSkylightIrradMap->getSamplerRegister(), curEntry.mIrradianceCubemap);
+
+               shaderConsts->setSafe(probeShaderConsts->mHasSkylight, 1.0f);
+               hasSkylight = true;
+               break;
+            }
+         }
+      }
+
       if (!hasSkylight)
       if (!hasSkylight)
          shaderConsts->setSafe(probeShaderConsts->mHasSkylight, 0.0f);
          shaderConsts->setSafe(probeShaderConsts->mHasSkylight, 0.0f);
    }
    }

+ 2 - 0
Engine/source/renderInstance/renderProbeMgr.h

@@ -152,6 +152,8 @@ struct ProbeShaderConstants
 
 
    void init(GFXShader* buffer);
    void init(GFXShader* buffer);
 
 
+   bool isValid();
+
    void _onShaderReload();
    void _onShaderReload();
 };
 };
 
 

+ 8 - 4
Engine/source/shaderGen/HLSL/shaderFeatureHLSL.cpp

@@ -3114,7 +3114,11 @@ void ReflectionProbeFeatHLSL::processPix(Vector<ShaderComponent*> &componentList
 
 
    Var *wsEyePos = (Var*)LangElement::find("eyePosWorld");
    Var *wsEyePos = (Var*)LangElement::find("eyePosWorld");
 
 
-   Var *worldToCamera = (Var*)LangElement::find("worldToCamera");
+   Var *worldToTangent = (Var*)LangElement::find("worldToTangent");
+   if (!worldToTangent)
+      return;
+
+   /*Var *worldToCamera = (Var*)LangElement::find("worldToCamera");
    if (!worldToCamera)
    if (!worldToCamera)
    {
    {
       worldToCamera = new Var;
       worldToCamera = new Var;
@@ -3122,13 +3126,13 @@ void ReflectionProbeFeatHLSL::processPix(Vector<ShaderComponent*> &componentList
       worldToCamera->setName("worldToCamera");
       worldToCamera->setName("worldToCamera");
       worldToCamera->uniform = true;
       worldToCamera->uniform = true;
       worldToCamera->constSortPos = cspPass;
       worldToCamera->constSortPos = cspPass;
-   }
+   }*/
 
 
    //Reflection vec
    //Reflection vec
    Var *surface = new Var("surface", "Surface");
    Var *surface = new Var("surface", "Surface");
    meta->addStatement(new GenOp("  @ = createForwardSurface(@,@,@,@,@,@,@,@);\r\n\n", new DecOp(surface), diffuseColor, bumpNormal, matinfo,
    meta->addStatement(new GenOp("  @ = createForwardSurface(@,@,@,@,@,@,@,@);\r\n\n", new DecOp(surface), diffuseColor, bumpNormal, matinfo,
-                     inTex, wsPosition, wsEyePos, wsView, worldToCamera));
-   String computeForwardProbes = String::String("   @.rgb += computeForwardProbes(@,@,@,@,@,@,@,@,@,\r\n\t\t");
+                     inTex, wsPosition, wsEyePos, wsView, worldToTangent));
+   String computeForwardProbes = String::String("   @.rgb = computeForwardProbes(@,@,@,@,@,@,@,@,@,\r\n\t\t");
    computeForwardProbes += String::String("@,TORQUE_SAMPLER2D_MAKEARG(@),\r\n\t\t"); 
    computeForwardProbes += String::String("@,TORQUE_SAMPLER2D_MAKEARG(@),\r\n\t\t"); 
    computeForwardProbes += String::String("TORQUE_SAMPLERCUBE_MAKEARG(@), TORQUE_SAMPLERCUBE_MAKEARG(@), \r\n\t\t");
    computeForwardProbes += String::String("TORQUE_SAMPLERCUBE_MAKEARG(@), TORQUE_SAMPLERCUBE_MAKEARG(@), \r\n\t\t");
    computeForwardProbes += String::String("TORQUE_SAMPLERCUBEARRAY_MAKEARG(@),TORQUE_SAMPLERCUBEARRAY_MAKEARG(@)).rgb; \r\n");
    computeForwardProbes += String::String("TORQUE_SAMPLERCUBEARRAY_MAKEARG(@),TORQUE_SAMPLERCUBEARRAY_MAKEARG(@)).rgb; \r\n");

+ 8 - 6
Templates/Full/game/shaders/common/lighting.hlsl

@@ -148,17 +148,18 @@ inline Surface createSurface(float4 gbuffer0, TORQUE_SAMPLER2D(gbufferTex1), TOR
 	return surface;
 	return surface;
 }
 }
 
 
-inline Surface createForwardSurface(float4 baseColor, float4 normal, float4 pbrProperties, in float2 uv, in float3 wsPosition, in float3 wsEyePos, in float3 wsEyeRay, in float4x4 invView)
+inline Surface createForwardSurface(float4 baseColor, float4 normal, float4 pbrProperties, in float2 uv, 
+                            in float3 wsPosition, in float3 wsEyePos, in float3 wsEyeRay, in float3x3 worldToTangent)
 {
 {
 	Surface surface = (Surface)0;
 	Surface surface = (Surface)0;
 
 
   surface.depth = 0;
   surface.depth = 0;
 	surface.P = wsPosition;
 	surface.P = wsPosition;
-	surface.N = mul(invView, float4(normal.xyz,0)).xyz; //TODO move t3d to use WS normals
+	surface.N = normalize( mul( normal.xyz, worldToTangent ) );
 	surface.V = normalize(wsEyePos - surface.P);
 	surface.V = normalize(wsEyePos - surface.P);
 	surface.baseColor = baseColor;
 	surface.baseColor = baseColor;
   const float minRoughness=1e-4;
   const float minRoughness=1e-4;
-	surface.roughness = clamp(1.0 - pbrProperties.b, minRoughness, 1.0); //t3d uses smoothness, so we convert to roughness.
+	surface.roughness = clamp(1.0 - pbrProperties.b, minRoughness, 1); //t3d uses smoothness, so we convert to roughness.
 	surface.roughness_brdf = surface.roughness * surface.roughness;
 	surface.roughness_brdf = surface.roughness * surface.roughness;
 	surface.metalness = pbrProperties.a;
 	surface.metalness = pbrProperties.a;
   surface.ao = pbrProperties.g;
   surface.ao = pbrProperties.g;
@@ -372,7 +373,7 @@ float4 computeForwardProbes(Surface surface,
    float lod = surface.roughness*cubeMips;
    float lod = surface.roughness*cubeMips;
 
 
    float alpha = 1;
    float alpha = 1;
-   for (i = 0; i < numProbes; ++i)
+   /*for (i = 0; i < numProbes; ++i)
    {
    {
       float contrib = contribution[i];
       float contrib = contribution[i];
       if (contrib != 0)
       if (contrib != 0)
@@ -384,7 +385,7 @@ float4 computeForwardProbes(Surface surface,
          specular += TORQUE_TEXCUBEARRAYLOD(specularCubemapAR, dir, cubemapIdx, lod).xyz * contrib;
          specular += TORQUE_TEXCUBEARRAYLOD(specularCubemapAR, dir, cubemapIdx, lod).xyz * contrib;
          alpha -= contrib;
          alpha -= contrib;
       }
       }
-   }
+   }*/
 
 
    if (hasSkylight && alpha > 0.001)
    if (hasSkylight && alpha > 0.001)
    {
    {
@@ -396,7 +397,7 @@ float4 computeForwardProbes(Surface surface,
 
 
    //energy conservation
    //energy conservation
    float3 kD = 1.0.xxx - F;
    float3 kD = 1.0.xxx - F;
-   kD *= 1.0 - surface.metalness;
+   kD *= clamp(1.0 - surface.metalness, 0.1, 1);
 
 
    //apply brdf
    //apply brdf
    //Do it once to save on texture samples
    //Do it once to save on texture samples
@@ -407,5 +408,6 @@ float4 computeForwardProbes(Surface surface,
    float3 diffuse = kD * irradiance * surface.baseColor.rgb;
    float3 diffuse = kD * irradiance * surface.baseColor.rgb;
    float4 finalColor = float4(diffuse + specular * surface.ao, 1.0);
    float4 finalColor = float4(diffuse + specular * surface.ao, 1.0);
 
 
+   finalColor = float4(specular,1);
    return finalColor;
    return finalColor;
 }
 }