浏览代码

HDRenderLoop: Add PreLightData concept (optimization)

- Add PrelightData concept: variable that you can precompute ahead of
calling the lighting loop
- Various stuff related to that: BRDF recomputed LambdaV  version etc...
/main
sebastienlagarde 8 年前
当前提交
fe99ce30
共有 19 个文件被更改,包括 475 次插入84 次删除
  1. 4
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs
  2. 4
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoopInspector.cs
  3. 2
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Debug/DebugViewMaterialGBuffer.shader
  4. 15
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/LayeredLit.shader
  5. 11
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/Lighting.hlsl
  6. 5
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/LightingDeferred.shader
  7. 12
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/LightingForward.hlsl
  8. 15
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lit.shader
  9. 3
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit.cs
  10. 1
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit.cs.hlsl
  11. 228
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit.hlsl
  12. 5
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Material.hlsl
  13. 1
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderConfig.cs
  14. 3
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderVariables.hlsl
  15. 10
      Assets/ScriptableRenderLoop/ShaderLibrary/API/D3D11.hlsl
  16. 73
      Assets/ScriptableRenderLoop/ShaderLibrary/BSDF.hlsl
  17. 27
      Assets/ScriptableRenderLoop/ShaderLibrary/Common.hlsl
  18. 130
      Assets/TestScenes/HDTest/HDRenderLoopTest.unity
  19. 10
      Assets/TestScenes/HDTest/Material/HDRenderLoopMaterials/LayerMask.tga.meta

4
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs


Vector4 screenSize = ComputeScreenSize(camera);
m_DeferredMaterial.SetVector("_ScreenSize", screenSize);
m_DeferredMaterial.SetTexture("_reflCubeTextures", m_cubeReflTexArray.GetTexCache());
m_DeferredMaterial.SetTexture("_ReflCubeTextures", m_cubeReflTexArray.GetTexCache());
cmd.name = "Deferred Ligthing Pass";
cmd.name = "Deferred Lighting Pass";
cmd.Blit(null, new RenderTargetIdentifier(s_CameraColorBuffer), m_DeferredMaterial, 0);
renderLoop.ExecuteCommandBuffer(cmd);
cmd.Dispose();

4
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoopInspector.cs


}
}
public override void OnInspectorGUI()
public override void OnInspectorGUI()
{
HDRenderLoop renderLoop = target as HDRenderLoop;
if(renderLoop)

FillWithProperties(typeof(Lit.BSDFData), styles.debugViewMaterialStrings, styles.debugViewMaterialValues, true, ref index);
styles.isDebugViewMaterialInit = true;
}
}
debugParameters.debugViewMaterial = EditorGUILayout.IntPopup(styles.debugViewMaterial, (int)debugParameters.debugViewMaterial, styles.debugViewMaterialStrings, styles.debugViewMaterialValues);

2
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Debug/DebugViewMaterialGBuffer.shader


DECLARE_GBUFFER_BAKE_LIGHTING(_CameraGBufferTexture);
UNITY_DECLARE_TEX2D(_CameraDepthTexture);
float4 _ScreenSize;
int _DebugViewMaterial;
struct Attributes

15
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/LayeredLit.shader


)
{
Varyings input = UnpackVaryings(packedInput);
float3 V = GetWorldSpaceNormalizeViewDir(input.positionWS);
float3 positionWS = input.positionWS;
BSDFData bsdfData = ConvertSurfaceDataToBSDFData(surfaceData);
Coordinate coord = GetCoordinate(input.positionHS.xy, _ScreenSize.zw);
PreLightData preLightData = GetPreLightData(V, positionWS, coord, bsdfData);
ENCODE_BAKE_LIGHTING_INTO_GBUFFER(GetBakedDiffuseLigthing(surfaceData, builtinData), outGBuffer);
ENCODE_BAKE_LIGHTING_INTO_GBUFFER(GetBakedDiffuseLigthing(preLightData, surfaceData, builtinData, bsdfData), outGBuffer);
}
#endif

GetSurfaceAndBuiltinData(input, surfaceData, builtinData);
BSDFData bsdfData = ConvertSurfaceDataToBSDFData(surfaceData);
Coordinate coord = GetCoordinate(input.positionHS.xy, _ScreenSize.zw);
PreLightData preLightData = GetPreLightData(V, positionWS, coord, bsdfData);
ForwardLighting(V, positionWS, bsdfData, diffuseLighting, specularLighting);
ForwardLighting(V, positionWS, preLightData, bsdfData, diffuseLighting, specularLighting);
diffuseLighting.rgb += GetBakedDiffuseLigthing(surfaceData, builtinData);
diffuseLighting.rgb += GetBakedDiffuseLigthing(preLightData, surfaceData, builtinData, bsdfData);
return float4(diffuseLighting.rgb + specularLighting.rgb, builtinData.opacity);
}

11
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/Lighting.hlsl


#ifndef UNITY_LIGHTING_INCLUDED
#define UNITY_LIGHTING_INCLUDED
// We need to define the macro used for env map evaluation based on the different architecture.
// Like for material we have one define by architecture.
// TODO: who setup the define for a given architecture ?
// For now our loop looks use texture arrays (but we should support different define based on architecture).
// NOTE: How do we support a pass with tiled forward and single forward in the same renderer (i.e with tex array and single tex)
#define UNITY_DECLARE_ENV(tex) UNITY_DECLARE_TEXCUBEARRAY(tex)
#define UNITY_ARGS_ENV(tex) UNITY_ARGS_TEXCUBEARRAY(tex)
#define UNITY_PASS_ENV(tex) UNITY_PASS_TEXCUBEARRAY(tex)
#define UNITY_SAMPLE_ENV_LOD(tex,coord,lod) UNITY_SAMPLE_TEXCUBEARRAY_LOD(tex,coord,lod)
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Material.hlsl"
#include "LightingForward.hlsl"

5
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/LightingDeferred.shader


DECLARE_GBUFFER_BAKE_LIGHTING(_CameraGBufferTexture);
UNITY_DECLARE_TEX2D(_CameraDepthTexture);
float4 _ScreenSize;
float4x4 _InvViewProjMatrix;

FETCH_GBUFFER(gbuffer, _CameraGBufferTexture, coord.unPositionSS);
BSDFData bsdfData = DECODE_FROM_GBUFFER(gbuffer);
PreLightData preLightData = GetPreLightData(V, positionWS, coord, bsdfData);
ForwardLighting(V, positionWS, bsdfData, diffuseLighting, specularLighting);
ForwardLighting(V, positionWS, preLightData, bsdfData, diffuseLighting, specularLighting);
FETCH_BAKE_LIGHTING_GBUFFER(gbuffer, _CameraGBufferTexture, coord.unPositionSS);
diffuseLighting.rgb += DECODE_BAKE_LIGHTING_FROM_GBUFFER(gbuffer);

12
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/LightingForward.hlsl


StructuredBuffer<PunctualLightData> _PunctualLightList;
int _PunctualLightCount;
UNITY_DECLARE_TEXCUBEARRAY(_reflCubeTextures);
StructuredBuffer<PunctualLightData> _EnvLightList;
UNITY_DECLARE_ENV(_ReflCubeTextures);
StructuredBuffer<EnvLightData> _EnvLightList;
void ForwardLighting( float3 V, float3 positionWS, BSDFData bsdfData,
void ForwardLighting( float3 V, float3 positionWS, PreLightData prelightData, BSDFData bsdfData,
out float4 diffuseLighting,
out float4 specularLighting)
{

{
float4 localDiffuseLighting;
float4 localSpecularLighting;
EvaluateBSDF_Punctual(V, positionWS, _PunctualLightList[i], bsdfData, localDiffuseLighting, localSpecularLighting);
EvaluateBSDF_Punctual(V, positionWS, prelightData, _PunctualLightList[i], bsdfData, localDiffuseLighting, localSpecularLighting);
diffuseLighting += localDiffuseLighting;
specularLighting += localSpecularLighting;
}

}
*/
/*
EvaluateBSDF_Env(V, positionWS, _EnvLightList[i], bsdfData, UNITY_PASS_TEXCUBEARRAY(_reflCubeTextures), localDiffuseLighting, localSpecularLighting);
EvaluateBSDF_Env(V, positionWS, prelightData, _EnvLightList[i], bsdfData, UNITY_PASS_ENV(_ReflCubeTextures), localDiffuseLighting, localSpecularLighting);
*/
}
#endif // UNITY_LIGHTING_FORWARD_INCLUDED

15
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lit.shader


)
{
Varyings input = UnpackVaryings(packedInput);
float3 V = GetWorldSpaceNormalizeViewDir(input.positionWS);
float3 positionWS = input.positionWS;
BSDFData bsdfData = ConvertSurfaceDataToBSDFData(surfaceData);
Coordinate coord = GetCoordinate(input.positionHS.xy, _ScreenSize.zw);
PreLightData preLightData = GetPreLightData(V, positionWS, coord, bsdfData);
ENCODE_BAKE_LIGHTING_INTO_GBUFFER(GetBakedDiffuseLigthing(surfaceData, builtinData), outGBuffer);
ENCODE_BAKE_LIGHTING_INTO_GBUFFER(GetBakedDiffuseLigthing(preLightData, surfaceData, builtinData, bsdfData), outGBuffer);
}
#endif

GetSurfaceAndBuiltinData(input, surfaceData, builtinData);
BSDFData bsdfData = ConvertSurfaceDataToBSDFData(surfaceData);
Coordinate coord = GetCoordinate(input.positionHS.xy, _ScreenSize.zw);
PreLightData preLightData = GetPreLightData(V, positionWS, coord, bsdfData);
ForwardLighting(V, positionWS, bsdfData, diffuseLighting, specularLighting);
ForwardLighting(V, positionWS, preLightData, bsdfData, diffuseLighting, specularLighting);
diffuseLighting.rgb += GetBakedDiffuseLigthing(surfaceData, builtinData);
diffuseLighting.rgb += GetBakedDiffuseLigthing(preLightData, surfaceData, builtinData, bsdfData);
return float4(diffuseLighting.rgb + specularLighting.rgb, builtinData.opacity);
}

3
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit.cs


LitStandard = 0,
LitSSS = 1,
LitClearCoat = 2,
LitSpecular = 3
LitSpecular = 3,
LitAniso = 4 // Should be the last as it is not setup by the users but generated based on anisotropy property
};
//-----------------------------------------------------------------------------

1
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit.cs.hlsl


#define MATERIALID_LIT_SSS (1)
#define MATERIALID_LIT_CLEAR_COAT (2)
#define MATERIALID_LIT_SPECULAR (3)
#define MATERIALID_LIT_ANISO (4)
//
// UnityEngine.ScriptableRenderLoop.Lit.SurfaceData: static fields

228
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit.hlsl


// SurfaceData is define in Lit.cs which generate Lit.cs.hlsl
#include "Lit.cs.hlsl"
// TODO: Check if anisotropy with a dynamic if on anisotropy > 0 is performant. Because it may mean we always calculate both isotrpy and anisotropy case.
// Maybe we should always calculate anisotropy in case of standard ? Don't think the compile can optimize correctly.
//-----------------------------------------------------------------------------
// Helper functions/variable specific to this materia
//-----------------------------------------------------------------------------
float PackMaterialId(int materialId)
{
return float(materialId) / 3.0;
}
int UnpackMaterialId(float f)
{
return int(round(f * 3.0));
}
UNITY_DECLARE_TEX2D(_PreIntegratedDFG);
// For image based lighting, a part of the BSDF is pre-integrated.
// This is done both for specular and diffuse (in case of DisneyDiffuse)
void GetPreIntegratedDFG(float NdotV, float roughness, float3 fresnel0, out float3 specularDFG, out float diffuseDFG)
{
// Pre-integrate GGX DFG
// _PreIntegratedDFG.r = Gv * (1 - Fc) with Fc = (1 - H.L)^5
// _PreIntegratedDFG.g = Gv * Fc
// Pre integrate DisneyDiffuse DFG:
// _PreIntegratedDFG.z = DisneyDiffuse
float3 preDFG = UNITY_SAMPLE_TEX2D_LOD(_PreIntegratedDFG, float2(NdotV, roughness), 0).xyz;
// f0 * Gv * (1 - Fc) + Gv * Fc
specularDFG = fresnel0 * preDFG.r + preDFG.g;
diffuseDFG = preDFG.b;
}
//-----------------------------------------------------------------------------
// conversion function for forward
//-----------------------------------------------------------------------------

bsdfData.tangentWS = surfaceData.tangentWS;
bsdfData.bitangentWS = cross(surfaceData.normalWS, surfaceData.tangentWS);
ConvertAnisotropyToRoughness(bsdfData.roughness, surfaceData.anisotropy, bsdfData.roughnessT, bsdfData.roughnessB);
bsdfData.materialId = surfaceData.anisotropy > 0 ? MATERIALID_LIT_ANISO : bsdfData.materialId;
}
else if (bsdfData.materialId == MATERIALID_LIT_SSS)
{

bsdfData.diffuseColor = surfaceData.baseColor;
bsdfData.fresnel0 = surfaceData.specularColor;
}
return bsdfData;
}
//-----------------------------------------------------------------------------
// Packing helper functions specific to this surfaceData
//-----------------------------------------------------------------------------
float PackMaterialId(int materialId)
{
return float(materialId) / 3.0;
}
int UnpackMaterialId(float f)
{
return int(round(f * 3.0));
}
//-----------------------------------------------------------------------------
// bake lighting function
//-----------------------------------------------------------------------------
float3 GetBakedDiffuseLigthing(SurfaceData surfaceData, BuiltinData builtinData)
{
float3 diffuseColor;
if (surfaceData.materialId == MATERIALID_LIT_STANDARD)
{
diffuseColor = surfaceData.baseColor * (1.0 - surfaceData.metalic);
}
else if (surfaceData.materialId == MATERIALID_LIT_SSS)
{
diffuseColor = surfaceData.baseColor;
}
else if (surfaceData.materialId == MATERIALID_LIT_CLEAR_COAT)
{
diffuseColor = surfaceData.baseColor * (1.0 - surfaceData.metalic);
}
else if (surfaceData.materialId == MATERIALID_LIT_SPECULAR)
{
diffuseColor = surfaceData.baseColor;
}
return builtinData.bakeDiffuseLighting * surfaceData.ambientOcclusion * diffuseColor + builtinData.emissiveColor * builtinData.emissiveIntensity;
return bsdfData;
}
//-----------------------------------------------------------------------------

bsdfData.tangentWS = UnpackNormalOctEncode(float2(inGBuffer2.rg * 2.0 - 1.0));
bsdfData.bitangentWS = cross(bsdfData.normalWS, bsdfData.tangentWS);
ConvertAnisotropyToRoughness(bsdfData.roughness, anisotropy, bsdfData.roughnessT, bsdfData.roughnessB);
bsdfData.materialId = anisotropy > 0 ? MATERIALID_LIT_ANISO : bsdfData.materialId;
}
else if (bsdfData.materialId == MATERIALID_LIT_SSS)
{

}
//-----------------------------------------------------------------------------
// PreLightData
//-----------------------------------------------------------------------------
// Precomputed lighting data to send to the various lighting functions
struct PreLightData
{
float NdotV;
// Aniso
float TdotV;
float BdotV;
float ggxLambdaV;
float anisoGGXLambdaV;
// image based lighting
float3 R;
float3 specularDFG;
float diffuseDFG;
// TODO: if we want we can store ambient occlusion here from SSAO pass for example that can be use for IBL specular occlusion
// float ambientOcclusion; // Feed from an ambient occlusion buffer
};
PreLightData GetPreLightData(float3 V, float3 positionWS, Coordinate coord, BSDFData bsdfData)
{
PreLightData preLightData = (PreLightData)0;
// TODO: check Eric idea about doing that when writting into the GBuffer (with our forward decal)
#if 0
preLightData.NdotV = GetShiftedNdotV(bsdfData.normalWS, V);
#else
preLightData.NdotV = GetNdotV(bsdfData.normalWS, V);
#endif
preLightData.ggxLambdaV = GetSmithJointGGXLambdaV(preLightData.NdotV, bsdfData.roughness);
// Check if we precompute anisotropy too (should not be necessary as if the variables are not used they will be optimize out, but may avoid
if (bsdfData.materialId == MATERIALID_LIT_ANISO)
{
preLightData.TdotV = dot(bsdfData.tangentWS, bsdfData.normalWS);
preLightData.BdotV = dot(bsdfData.bitangentWS, bsdfData.normalWS);
preLightData.anisoGGXLambdaV = GetSmithJointGGXAnisoLambdaV(preLightData.TdotV, preLightData.BdotV, preLightData.NdotV, bsdfData.roughnessT, bsdfData.roughnessB);
}
preLightData.R = reflect(-V, bsdfData.normalWS);
GetPreIntegratedDFG(preLightData.NdotV, bsdfData.roughness, bsdfData.fresnel0, preLightData.specularDFG, preLightData.diffuseDFG);
// #if SHADERPASS == SHADERPASS_GBUFFER
// preLightData.ambientOcclusion = _AmbientOcclusion.Load(uint3(coord.unPositionSS, 0)).x;
// #endif
return preLightData;
}
//-----------------------------------------------------------------------------
// bake lighting function
//-----------------------------------------------------------------------------
// GetBakedDiffuseLigthing function compute the bake lighting + emissive color to be store in emissive buffer (Deferred case)
// In forward it must be add to the final contribution.
// This function require the 3 structure surfaceData, builtinData, bsdfData because it may require both the engine side data, and data that will not be store inside the gbuffer.
float3 GetBakedDiffuseLigthing(PreLightData prelightData, SurfaceData surfaceData, BuiltinData builtinData, BSDFData bsdfData)
{
// Premultiply bake diffuse lighting information with DisneyDiffuse pre-integration
return builtinData.bakeDiffuseLighting * prelightData.diffuseDFG * surfaceData.ambientOcclusion * bsdfData.diffuseColor + builtinData.emissiveColor * builtinData.emissiveIntensity;
}
//-----------------------------------------------------------------------------
void EvaluateBSDF_Punctual( float3 V, float3 positionWS, PunctualLightData lightData, BSDFData bsdfData,
void EvaluateBSDF_Punctual( float3 V, float3 positionWS, PreLightData prelightData, PunctualLightData lightData, BSDFData bsdfData,
out float4 diffuseLighting,
out float4 specularLighting)
{

if (illuminance > 0.0f)
{
float NdotV = abs(dot(bsdfData.normalWS, V)) + 1e-5f; // TODO: check Eric idea about doing that when writting into the GBuffer (with our forward decal)
float Vis = V_SmithJointGGX(NdotL, NdotV, bsdfData.roughness);
float D = D_GGXDividePI(NdotH, bsdfData.roughness);
float Vis;
float D;
// TODO: this way of handling aniso may not be efficient, or maybe with material classification, need to check perf here
// Maybe always using aniso maybe a win ?
if (bsdfData.materialId == MATERIALID_LIT_ANISO)
{
float TdotL = saturate(dot(bsdfData.tangentWS, L));
float BdotL = saturate(dot(bsdfData.bitangentWS, L));
#ifdef USE_BSDF_PRE_LAMBDAV
Vis = V_SmithJointGGXAnisoLambdaV( prelightData.TdotV, prelightData.BdotV, prelightData.NdotV, TdotL, BdotL, NdotL,
bsdfData.roughnessT, bsdfData.roughnessB, prelightData.anisoGGXlambdaV);
#else
Vis = V_SmithJointGGXAniso( prelightData.TdotV, prelightData.BdotV, prelightData.NdotV, TdotL, BdotL, NdotL,
bsdfData.roughnessT, bsdfData.roughnessB);
#endif
float TdotH = saturate(dot(bsdfData.tangentWS, H));
float BdotH = saturate(dot(bsdfData.bitangentWS, H));
D = D_GGXAnisoDividePI(TdotH, BdotH, NdotH, bsdfData.roughnessT, bsdfData.roughnessB);
}
else
{
#ifdef USE_BSDF_PRE_LAMBDAV
Vis = V_SmithJointGGX(NdotL, prelightData.NdotV, bsdfData.roughness, prelightData.ggxLambdaV);
#else
Vis = V_SmithJointGGX(NdotL, prelightData.NdotV, bsdfData.roughness);
#endif
D = D_GGXDividePI(NdotH, bsdfData.roughness);
}
float diffuseTerm = DisneyDiffuseDividePI(NdotV, NdotL, LdotH, bsdfData.perceptualRoughness);
float diffuseTerm = DisneyDiffuseDividePI(prelightData.NdotV, NdotL, LdotH, bsdfData.perceptualRoughness);
#endif
diffuseLighting.rgb = bsdfData.diffuseColor * diffuseTerm;

}
// _preIntegratedFG and _CubemapLD are unique for each BRDF
void EvaluateBSDF_Env( float3 V, float3 positionWS, PreLightData prelightData, EnvLightData lightData, BSDFData bsdfData,
UNITY_ARGS_ENV(_ReflCubeTextures),
out float4 diffuseLighting,
out float4 specularLighting)
{
/*
// Perform box collision to parallax correct the cubemap
// invTransform go from worldspace to local box space without scaling
float3 positionLS = mul(float4(positionWS, 1.0), light.invTransform).xyz;
float3 dirLS = mul(prelightData.R, (float3x3)light.invTransform);
float2 intersections = boxRayIntersect(positionLS, dirLS, -light.extend, light.extend);
diffuseLighting = float4(0.0, 0.0, 0.0, 1.0);
specularLighting = float4(0.0, 0.0, 0.0, 1.0);
if (intersections.y > intersections.x)
{
float3 R = positionLS + intersections.y * dirLS;
// local offset
R = R - light.localOffset;
float mipmapLevel = roughnessToMipmapLevel(bsdfData.roughness);
half4 rgbm = UNITY_SAMPLE_TEXCUBEARRAY_LOD(tex, float4(glossIn.reflUVW.xyz, sliceIndex), mip);
float3 preLD = texCube(_CubemapLD, float4(R, textureIndex), mipmapLevel);
specularLighting.rgb = preLD.rgb * prelightData.specularDFG;
specularLighting.rgb *= bsdfData.specularOcclusion;
}*/
diffuseLighting = float4(0.0, 0.0, 0.0, 1.0);
specularLighting = float4(0.0, 0.0, 0.0, 1.0);
}
#endif // UNITY_MATERIAL_LIT_INCLUDED

5
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Material.hlsl


#ifndef UNITY_MATERIAL_INCLUDED
#define UNITY_MATERIAL_INCLUDED
#include "Assets/ScriptableRenderLoop/ShaderLibrary/Common.hlsl"
#include "Assets/ScriptableRenderLoop/ShaderLibrary/Packing.hlsl"
#include "Assets/ScriptableRenderLoop/ShaderLibrary/BSDF.hlsl"
#include "Assets/ScriptableRenderLoop/ShaderLibrary/CommonLighting.hlsl"

#include "Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderConfig.cs"
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/LightDefinition.cs.hlsl"
#include "../ShaderConfig.cs"
#include "../LightDefinition.cs.hlsl"
//-----------------------------------------------------------------------------
// common Encode/Decode functions

1
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderConfig.cs


//-----------------------------------------------------------------------------
// #define DIFFUSE_LAMBERT_BRDF
// #define USE_BSDF_PRE_LAMBDAV
// #define VELOCITY_IN_GBUFFER

3
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderVariables.hlsl


// ----------------------------------------------------------------------------
// TODO: move this to constant buffer by Pass
float4 _ScreenSize;
float4x4 GetObjectToWorldMatrix()
{

10
Assets/ScriptableRenderLoop/ShaderLibrary/API/D3D11.hlsl


#define UNITY_DECLARE_TEX2D(tex) Texture2D tex; SamplerState sampler##tex
#define UNITY_DECLARE_TEX2D_NOSAMPLER(tex) Texture2D tex
#define UNITY_SAMPLE_TEX2D(tex,coord) tex.Sample (sampler##tex,coord)
#define UNITY_SAMPLE_TEX2D_LOD(tex,coord,lod) tex.SampleLevel (sampler##tex,coord, lod)
#define UNITY_SAMPLE_TEX2D_SAMPLER(tex,samplertex,coord) tex.Sample (sampler##samplertex,coord)
// Cubemaps

#define UNITY_SAMPLE_TEXCUBEARRAY(tex,coord) tex.Sample (sampler##tex,coord)
#define UNITY_SAMPLE_TEXCUBEARRAY_LOD(tex,coord,lod) tex.SampleLevel (sampler##tex,coord, lod)
#define UNITY_SAMPLE_TEXCUBEARRAY_SAMPLER(tex,samplertex,coord) tex.Sample (sampler##samplertex,coord)
#define TEXTURE2D(textureName) Texture2D textureName;
#define SAMPLER2D(samplerName) SamplerState samplerName;
#define TEXTURE2D_ARGS(textureName, samplerName) Texture2D textureName, SamplerState samplerName
#define TEXTURE2D_PASS(textureName, samplerName) textureName, samplerName
#define SAMPLE_TEXTURE2D(textureName, samplerName, coord) textureName.Sample(samplerName, coord)

73
Assets/ScriptableRenderLoop/ShaderLibrary/BSDF.hlsl


return INV_PI * D_GGX(NdotH, roughness);
}
#if 1
float a = roughness;
float a2 = a * a;
float lambdaV = NdotL * sqrt((-NdotV * a2 + NdotV) * NdotV + a2);
float lambdaL = NdotV * sqrt((-NdotL * a2 + NdotL) * NdotL + a2);
// Simplify visibility term: (2.0f * NdotL * NdotV) / ((4.0f * NdotL * NdotV) * (lambda_v + lambda_l));
return 0.5 / (lambdaV + lambdaL);
}
// Precompute part of lambdaV
float GetSmithJointGGXLambdaV(float NdotV, float roughness)
{
return sqrt((-NdotV * a2 + NdotV) * NdotV + a2);
}
float lambdaV = NdotL * sqrt((-NdotV * a2 + NdotV) * NdotV + a2);
float V_SmithJointGGX(float NdotL, float NdotV, float roughness, float lambdaV)
{
float a = roughness;
float a2 = a * a;
// Reorder code to be more optimal
lambdaV *= NdotL;
return 0.5f / (lambdaV + lambdaL);
#else
return 0.5 / (lambdaV + lambdaL);
}
float V_SmithJointGGXApprox(float NdotL, float NdotV, float roughness)
{
float a = roughness;
float lambdaV = NdotL * (NdotV * (1 - a) + a);
float lambdaL = NdotV * (NdotL * (1 - a) + a);
return 0.5 / (lambdaV + lambdaL);
}
// Precompute part of LambdaV
float GetSmithJointGGXApproxLambdaV(float NdotV, float roughness)
{
float lambdaV = NdotL * (NdotV * (1 - a) + a);
return (NdotV * (1 - a) + a);
}
float V_SmithJointGGXApprox(float NdotL, float NdotV, float roughness, float lambdaV)
{
float a = roughness;
// Approximation of the above formulation (simplify the sqrt, not mathematically correct but close enough)
lambdaV *= NdotL;
#endif
}
// roughnessT -> roughness in tangent direction

return 0.5 / (lambdaV + lambdaL);
}
// TODO: Optimize, lambdaV could be precomputed at the beginning of the loop and reuse for all lights.
float GetSmithJointGGXAnisoLambdaV(float TdotV, float BdotV, float NdotV, float roughnessT, float roughnessB)
{
float aT = roughnessT;
float aT2 = aT * aT;
float aB = roughnessB;
float aB2 = aB * aB;
return sqrt(aT2 * TdotV * TdotV + aB2 * BdotV * BdotV + NdotV * NdotV);
}
float V_SmithJointGGXAnisoLambdaV(float TdotV, float BdotV, float NdotV, float TdotL, float BdotL, float NdotL, float roughnessT, float roughnessB, float lambdaV)
{
float aT = roughnessT;
float aT2 = aT * aT;
float aB = roughnessB;
float aB2 = aB * aB;
lambdaV *= NdotL;
float lambdaL = NdotV * sqrt(aT2 * TdotL * TdotL + aB2 * BdotL * BdotL + NdotL * NdotL);
return 0.5 / (lambdaV + lambdaL);
}
//-----------------------------------------------------------------------------
// Diffuse BRDF - diffuseColor is expected to be multiply by the caller

27
Assets/ScriptableRenderLoop/ShaderLibrary/Common.hlsl


float FastATan(float x)
{
float t0 = FastATanPos(abs(x));
return (x < 0.0f) ? -t0 : t0;
return (x < 0.0) ? -t0 : t0;
}
// ----------------------------------------------------------------------------

// This function is use to provide an easy way to sample into a screen texture, either from a pixel or a compute shaders.
// This allow to easily share code.
// If a compute shader call this function inPositionSS is an interger usually calculate like: uint2 inPositionSS = groupId.xy * BLOCK_SIZE + groupThreadId.xy
// If a compute shader call this function inPositionSS is an integer usually calculate like: uint2 inPositionSS = groupId.xy * BLOCK_SIZE + groupThreadId.xy
// else it is current unormalized screen coordinate like return by VPOS
Coordinate GetCoordinate(float2 inPositionSS, float2 invScreenSize)
{

{
return 1.0 / (zBufferParam.z * depth + zBufferParam.w);
}
// NdotV should not be negative for visible pixels, but it can happen due to perspective projection and normal mapping + decal
// In this case this may cause weird artifact.
// GetNdotV return a 'valid' data
float GetNdotV(float3 N, float3 V)
{
return abs(dot(N, V)); // This abs allow to limit artifact
}
// NdotV should not be negative for visible pixels, but it can happen due to perspective projection and normal mapping + decal
// In this case normal should be modified to become valid (i.e facing camera) and not cause weird artifacts.
// but this operation adds few ALU and users may not want it. Alternative is to simply take the abs of NdotV (less correct but works too).
// Note: This code is not compatible with two sided lighting used in SpeedTree (TODO: investigate).
float GetShiftedNdotV(float3 N, float3 V)
{
// The amount we shift the normal toward the view vector is defined by the dot product.
float shiftAmount = dot(N, V);
N = shiftAmount < 0.0 ? N + V * (-shiftAmount + 1e-5f) : N;
N = normalize(N);
return saturate(dot(N, V)); // TODO: this saturate should not be necessary here
}
#endif // UNITY_COMMON_INCLUDED

130
Assets/TestScenes/HDTest/HDRenderLoopTest.unity


m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: e641a36bceddbf24a89656e94dafb3e5, type: 2}
m_IsPrefabParent: 0
--- !u!1 &785457124
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 785457126}
- component: {fileID: 785457125}
m_Layer: 0
m_Name: Point light (2)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!108 &785457125
Light:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 785457124}
m_Enabled: 1
serializedVersion: 7
m_Type: 2
m_Color: {r: 0.3784602, g: 0.7352941, b: 0.4842799, a: 1}
m_Intensity: 8
m_Range: 6.01
m_SpotAngle: 30
m_CookieSize: 10
m_Shadows:
m_Type: 0
m_Resolution: -1
m_CustomResolution: -1
m_Strength: 1
m_Bias: 0.05
m_NormalBias: 0.4
m_NearPlane: 0.2
m_Cookie: {fileID: 0}
m_DrawHalo: 0
m_Flare: {fileID: 0}
m_RenderMode: 0
m_CullingMask:
serializedVersion: 2
m_Bits: 4294967295
m_Lightmapping: 4
m_AreaSize: {x: 1, y: 1}
m_BounceIntensity: 1
m_ShadowRadius: 0
m_ShadowAngle: 0
--- !u!4 &785457126
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 785457124}
m_LocalRotation: {x: -0.0034919123, y: 0.0089843245, z: -0.007998787, w: 0.99992156}
m_LocalPosition: {x: -1.808, y: 1.504, z: 3.025}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 18
m_LocalEulerAnglesHint: {x: -0.39200002, y: 1.033, z: -0.92}
--- !u!1 &970745596
GameObject:
m_ObjectHideFlags: 0

m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1220024533}
m_LocalRotation: {x: -0.0034919123, y: 0.0089843245, z: -0.007998787, w: 0.99992156}
m_LocalPosition: {x: 0.19, y: 1.46, z: 1.286}
m_LocalPosition: {x: 0.632, y: 1.453, z: 1.334}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}

m_Name:
m_EditorClassIdentifier:
m_RenderLoop: {fileID: 11400000, guid: 2400b74f5ce370c4481e5dc417d03703, type: 2}
m_AssetVersion: 0
m_AssetVersion: 1
--- !u!1001 &1828950732
Prefab:
m_ObjectHideFlags: 0

m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: e641a36bceddbf24a89656e94dafb3e5, type: 2}
m_IsPrefabParent: 0
--- !u!1 &1854618464
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 1854618466}
- component: {fileID: 1854618465}
m_Layer: 0
m_Name: Point light (1)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!108 &1854618465
Light:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1854618464}
m_Enabled: 1
serializedVersion: 7
m_Type: 2
m_Color: {r: 0.3784602, g: 0.7352941, b: 0.4842799, a: 1}
m_Intensity: 8
m_Range: 6.01
m_SpotAngle: 30
m_CookieSize: 10
m_Shadows:
m_Type: 0
m_Resolution: -1
m_CustomResolution: -1
m_Strength: 1
m_Bias: 0.05
m_NormalBias: 0.4
m_NearPlane: 0.2
m_Cookie: {fileID: 0}
m_DrawHalo: 0
m_Flare: {fileID: 0}
m_RenderMode: 0
m_CullingMask:
serializedVersion: 2
m_Bits: 4294967295
m_Lightmapping: 4
m_AreaSize: {x: 1, y: 1}
m_BounceIntensity: 1
m_ShadowRadius: 0
m_ShadowAngle: 0
--- !u!4 &1854618466
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1854618464}
m_LocalRotation: {x: -0.0034919123, y: 0.0089843245, z: -0.007998787, w: 0.99992156}
m_LocalPosition: {x: 3.958, y: 1.411, z: 2.922}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 17
m_LocalEulerAnglesHint: {x: -0.39200002, y: 1.033, z: -0.92}

10
Assets/TestScenes/HDTest/Material/HDRenderLoopMaterials/LayerMask.tga.meta


fileFormatVersion: 2
guid: 6b43fa9736beb354dba359b5d2ec3699
timeCreated: 1476187467
timeCreated: 1476374214
licenseType: Pro
TextureImporter:
fileIDToRecycleName: {}

allowsAlphaSplitting: 0
overridden: 0
- buildTarget: Windows Store Apps
maxTextureSize: 2048
textureFormat: -1
textureCompression: 0
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
- buildTarget: WebGL
maxTextureSize: 2048
textureFormat: -1
textureCompression: 0

正在加载...
取消
保存