浏览代码

Merge pull request #1219 from Unity-Technologies/FactorLTCAreaCode

HDRenderPipeline: FactorLTCAreaCode
/main
GitHub 7 年前
当前提交
ff5089b7
共有 14 个文件被更改,包括 179 次插入92 次删除
  1. 80
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.cs
  2. 21
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.hlsl
  3. 6
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LtcData.DisneyDiffuse.cs
  4. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LtcData.GGX.cs
  5. 8
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight.meta
  6. 124
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LTCAreaLight.cs
  7. 11
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LTCAreaLight.cs.meta
  8. 10
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LTCAreaLight.hlsl
  9. 9
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LTCAreaLight.hlsl.meta
  10. 0
      /ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LtcData.GGX.cs.meta
  11. 0
      /ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LtcData.DisneyDiffuse.cs.meta
  12. 0
      /ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LtcData.DisneyDiffuse.cs
  13. 0
      /ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LtcData.GGX.cs

80
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.cs


Material m_InitPreFGD;
RenderTexture m_PreIntegratedFGD;
// For area lighting - We pack all texture inside a texture array to reduce the number of resource required
Texture2DArray m_LtcData; // 0: m_LtcGGXMatrix - RGBA, 2: m_LtcDisneyDiffuseMatrix - RGBA, 3: m_LtcMultiGGXFresnelDisneyDiffuse - RGB, A unused
const int k_LtcLUTMatrixDim = 3; // size of the matrix (3x3)
const int k_LtcLUTResolution = 64;
// Load LUT with one scalar in alpha of a tex2D
void LoadLUT(Texture2DArray tex, int arrayElement, TextureFormat format, float[] LUTScalar)
{
const int count = k_LtcLUTResolution * k_LtcLUTResolution;
Color[] pixels = new Color[count];
for (int i = 0; i < count; i++)
{
pixels[i] = new Color(0, 0, 0, LUTScalar[i]);
}
tex.SetPixels(pixels, arrayElement);
}
// Load LUT with 3x3 matrix in RGBA of a tex2D (some part are zero)
void LoadLUT(Texture2DArray tex, int arrayElement, TextureFormat format, double[,] LUTTransformInv)
{
const int count = k_LtcLUTResolution * k_LtcLUTResolution;
Color[] pixels = new Color[count];
for (int i = 0; i < count; i++)
{
// Both GGX and Disney Diffuse BRDFs have zero values in columns 1, 3, 5, 7.
// Column 8 contains only ones.
pixels[i] = new Color((float)LUTTransformInv[i, 0],
(float)LUTTransformInv[i, 2],
(float)LUTTransformInv[i, 4],
(float)LUTTransformInv[i, 6]);
}
tex.SetPixels(pixels, arrayElement);
}
// Special-case function for 'm_LtcMultiGGXFresnelDisneyDiffuse'.
void LoadLUT(Texture2DArray tex, int arrayElement, TextureFormat format, float[] LtcGGXMagnitudeData,
float[] LtcGGXFresnelData,
float[] LtcDisneyDiffuseMagnitudeData)
{
const int count = k_LtcLUTResolution * k_LtcLUTResolution;
Color[] pixels = new Color[count];
for (int i = 0; i < count; i++)
{
// We store the result of the subtraction as a run-time optimization.
// See the footnote 2 of "LTC Fresnel Approximation" by Stephen Hill.
pixels[i] = new Color(LtcGGXMagnitudeData[i] - LtcGGXFresnelData[i],
LtcGGXFresnelData[i], LtcDisneyDiffuseMagnitudeData[i], 1);
}
tex.SetPixels(pixels, arrayElement);
}
public Lit() {}
public override void Build(HDRenderPipelineAsset hdAsset)

m_PreIntegratedFGD.name = CoreUtils.GetRenderTargetAutoName(128, 128, RenderTextureFormat.ARGB2101010, "PreIntegratedFGD");
m_PreIntegratedFGD.Create();
m_LtcData = new Texture2DArray(k_LtcLUTResolution, k_LtcLUTResolution, 3, TextureFormat.RGBAHalf, false /*mipmap*/, true /* linear */)
{
hideFlags = HideFlags.HideAndDontSave,
wrapMode = TextureWrapMode.Clamp,
filterMode = FilterMode.Bilinear,
name = CoreUtils.GetTextureAutoName(k_LtcLUTResolution, k_LtcLUTResolution, TextureFormat.RGBAHalf, depth: 3, dim: TextureDimension.Tex2DArray, name: "LTC_LUT")
};
LoadLUT(m_LtcData, 0, TextureFormat.RGBAHalf, s_LtcGGXMatrixData);
LoadLUT(m_LtcData, 1, TextureFormat.RGBAHalf, s_LtcDisneyDiffuseMatrixData);
// TODO: switch to RGBA64 when it becomes available.
LoadLUT(m_LtcData, 2, TextureFormat.RGBAHalf, s_LtcGGXMagnitudeData, s_LtcGGXFresnelData, s_LtcDisneyDiffuseMagnitudeData);
m_LtcData.Apply();
LTCAreaLight.instance.Build();
m_isInit = false;
}

CoreUtils.Destroy(m_InitPreFGD);
CoreUtils.Destroy(m_PreIntegratedFGD);
CoreUtils.Destroy(m_LtcData);
// TODO: how to delete RenderTexture ? or do we need to do it ?
LTCAreaLight.instance.Cleanup();
m_isInit = false;
}

public override void Bind()
{
Shader.SetGlobalTexture("_PreIntegratedFGD", m_PreIntegratedFGD);
Shader.SetGlobalTexture("_LtcData", m_LtcData);
LTCAreaLight.instance.Bind();
}
}
}

21
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.hlsl


TEXTURE2D(_GBufferTexture2);
TEXTURE2D(_GBufferTexture3);
// Area light textures
// TODO: This one should be set into a constant Buffer at pass frequency (with _Screensize)
#include "../LTCAreaLight/LTCAreaLight.hlsl"
TEXTURE2D_ARRAY(_LtcData); // We pack the 3 Ltc data inside a texture array
#define LTC_GGX_MATRIX_INDEX 0 // RGBA
#define LTC_DISNEY_DIFFUSE_MATRIX_INDEX 1 // RGBA
#define LTC_MULTI_GGX_FRESNEL_DISNEY_DIFFUSE_INDEX 2 // RGB, A unused
#define LTC_LUT_SIZE 64
#define LTC_LUT_SCALE ((LTC_LUT_SIZE - 1) * rcp(LTC_LUT_SIZE))
#define LTC_LUT_OFFSET (0.5 * rcp(LTC_LUT_SIZE))
//-----------------------------------------------------------------------------
// Definition

UpdateLightingHierarchyWeights(hierarchyWeight, weight); // Shouldn't be needed, but safer in case we decide to change hierarchy priority
float3 preLD = SAMPLE_TEXTURE2D_LOD(
_ColorPyramidTexture,
s_trilinear_clamp_sampler,
hit.positionNDC * _ColorPyramidScale.xy,
_ColorPyramidTexture,
s_trilinear_clamp_sampler,
hit.positionNDC * _ColorPyramidScale.xy,
preLightData.transparentSSMipLevel
).rgb;

diffuseLighting = modifiedDiffuseColor * lighting.direct.diffuse + bakeDiffuseLighting;
// If refraction is enable we use the transmittanceMask to lerp between current diffuse lighting and refraction value
// Physically speaking, it should be transmittanceMask should be 1, but for artistic reasons, we let the value vary
// Physically speaking, transmittanceMask should be 1, but for artistic reasons, we let the value vary
#if HAS_REFRACTION
diffuseLighting = lerp(diffuseLighting, lighting.indirect.specularTransmitted, bsdfData.transmittanceMask);
#endif

specularLighting *= 1.0 + bsdfData.fresnel0 * preLightData.energyCompensation;
#ifdef DEBUG_DISPLAY
if (_DebugLightingMode != 0)
{
specularLighting = float3(0.0, 0.0, 0.0); // Disable specular lighting

6
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LtcData.DisneyDiffuse.cs


namespace UnityEngine.Experimental.Rendering.HDPipeline
{
public partial class Lit : RenderPipelineMaterial
public partial class LTCAreaLight
//-------------------------------------------------------------------------------------------
// LTC area light Look up table (fit for Disney Diffuse)
//-------------------------------------------------------------------------------------------
public static double[,] s_LtcDisneyDiffuseMatrixData = new double[k_LtcLUTResolution * k_LtcLUTResolution, k_LtcLUTMatrixDim * k_LtcLUTMatrixDim]
{
{1.018309, 0, 0.000000, 0, 1.018309, 0, 0.000000, 0, 1},

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LtcData.GGX.cs


namespace UnityEngine.Experimental.Rendering.HDPipeline
{
public partial class Lit : RenderPipelineMaterial
public partial class LTCAreaLight
{
//-------------------------------------------------------------------------------------------
// LTC area light Look up table (fit for GGX with height-correlated Smith's visibility term)

8
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight.meta


fileFormatVersion: 2
guid: 0cc808eb94961c54791ef0b41f113496
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

124
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LTCAreaLight.cs


using System;
using UnityEngine.Rendering;
namespace UnityEngine.Experimental.Rendering.HDPipeline
{
public partial class LTCAreaLight
{
static LTCAreaLight s_Instance;
public static LTCAreaLight instance
{
get
{
if (s_Instance == null)
s_Instance = new LTCAreaLight();
return s_Instance;
}
}
bool m_isInit;
int m_refCounting;
// For area lighting - We pack all texture inside a texture array to reduce the number of resource required
Texture2DArray m_LtcData; // 0: m_LtcGGXMatrix - RGBA, 2: m_LtcDisneyDiffuseMatrix - RGBA, 3: m_LtcMultiGGXFresnelDisneyDiffuse - RGB, A unused
const int k_LtcLUTMatrixDim = 3; // size of the matrix (3x3)
const int k_LtcLUTResolution = 64;
LTCAreaLight()
{
m_isInit = false;
m_refCounting = 0;
}
// Load LUT with one scalar in alpha of a tex2D
void LoadLUT(Texture2DArray tex, int arrayElement, TextureFormat format, float[] LUTScalar)
{
const int count = k_LtcLUTResolution * k_LtcLUTResolution;
Color[] pixels = new Color[count];
for (int i = 0; i < count; i++)
{
pixels[i] = new Color(0, 0, 0, LUTScalar[i]);
}
tex.SetPixels(pixels, arrayElement);
}
// Load LUT with 3x3 matrix in RGBA of a tex2D (some part are zero)
void LoadLUT(Texture2DArray tex, int arrayElement, TextureFormat format, double[,] LUTTransformInv)
{
const int count = k_LtcLUTResolution * k_LtcLUTResolution;
Color[] pixels = new Color[count];
for (int i = 0; i < count; i++)
{
// Both GGX and Disney Diffuse BRDFs have zero values in columns 1, 3, 5, 7.
// Column 8 contains only ones.
pixels[i] = new Color((float)LUTTransformInv[i, 0],
(float)LUTTransformInv[i, 2],
(float)LUTTransformInv[i, 4],
(float)LUTTransformInv[i, 6]);
}
tex.SetPixels(pixels, arrayElement);
}
// Special-case function for 'm_LtcMultiGGXFresnelDisneyDiffuse'.
void LoadLUT(Texture2DArray tex, int arrayElement, TextureFormat format, float[] LtcGGXMagnitudeData, float[] LtcGGXFresnelData, float[] LtcDisneyDiffuseMagnitudeData)
{
const int count = k_LtcLUTResolution * k_LtcLUTResolution;
Color[] pixels = new Color[count];
for (int i = 0; i < count; i++)
{
// We store the result of the subtraction as a run-time optimization.
// See the footnote 2 of "LTC Fresnel Approximation" by Stephen Hill.
pixels[i] = new Color(LtcGGXMagnitudeData[i] - LtcGGXFresnelData[i], LtcGGXFresnelData[i], LtcDisneyDiffuseMagnitudeData[i], 1);
}
tex.SetPixels(pixels, arrayElement);
}
public void Build()
{
m_refCounting++;
m_LtcData = new Texture2DArray(k_LtcLUTResolution, k_LtcLUTResolution, 3, TextureFormat.RGBAHalf, false /*mipmap*/, true /* linear */)
{
hideFlags = HideFlags.HideAndDontSave,
wrapMode = TextureWrapMode.Clamp,
filterMode = FilterMode.Bilinear,
name = CoreUtils.GetTextureAutoName(k_LtcLUTResolution, k_LtcLUTResolution, TextureFormat.RGBAHalf, depth: 3, dim: TextureDimension.Tex2DArray, name: "LTC_LUT")
};
LoadLUT(m_LtcData, 0, TextureFormat.RGBAHalf, s_LtcGGXMatrixData);
LoadLUT(m_LtcData, 1, TextureFormat.RGBAHalf, s_LtcDisneyDiffuseMatrixData);
// TODO: switch to RGBA64 when it becomes available.
LoadLUT(m_LtcData, 2, TextureFormat.RGBAHalf, s_LtcGGXMagnitudeData, s_LtcGGXFresnelData, s_LtcDisneyDiffuseMagnitudeData);
m_LtcData.Apply();
m_isInit = true;
}
public void Cleanup()
{
m_refCounting--;
if (m_refCounting <= 0)
{
CoreUtils.Destroy(m_LtcData);
}
m_isInit = false;
}
public void Bind()
{
Shader.SetGlobalTexture("_LtcData", m_LtcData);
}
}
}

11
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LTCAreaLight.cs.meta


fileFormatVersion: 2
guid: e6d8f4a97e711d6488965da2f3baeb51
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

10
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LTCAreaLight.hlsl


// Area light textures
TEXTURE2D_ARRAY(_LtcData); // We pack all Ltc data inside one texture array to limit the number of resource used
#define LTC_GGX_MATRIX_INDEX 0 // RGBA
#define LTC_DISNEY_DIFFUSE_MATRIX_INDEX 1 // RGBA
#define LTC_MULTI_GGX_FRESNEL_DISNEY_DIFFUSE_INDEX 2 // RGB, A unused
#define LTC_LUT_SIZE 64
#define LTC_LUT_SCALE ((LTC_LUT_SIZE - 1) * rcp(LTC_LUT_SIZE))
#define LTC_LUT_OFFSET (0.5 * rcp(LTC_LUT_SIZE))

9
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LTCAreaLight.hlsl.meta


fileFormatVersion: 2
guid: 726ce9f653b80b940b9e5a2244acfcc2
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

/ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/LtcData.GGX.cs.meta → /ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LtcData.GGX.cs.meta

/ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/LtcData.DisneyDiffuse.cs.meta → /ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LtcData.DisneyDiffuse.cs.meta

/ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/LtcData.DisneyDiffuse.cs → /ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LtcData.DisneyDiffuse.cs

/ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/LtcData.GGX.cs → /ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LtcData.GGX.cs

正在加载...
取消
保存