浏览代码

HDRenderLoop: Do misc refactor for the sky + few optim + write lot of todo

/main
Sebastien Lagarde 8 年前
当前提交
ceca355d
共有 8 个文件被更改,包括 110 次插入66 次删除
  1. 3
      Assets/ScriptableRenderLoop/HDRenderLoop/Editor/HDRenderLoopInspector.cs
  2. 7
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs
  3. 27
      Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/SinglePass/SinglePassLoop.hlsl
  4. 12
      Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Lit.hlsl
  5. 2
      Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LitSurfaceData.hlsl
  6. 4
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky/Resources/GGXConvolve.shader
  7. 6
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky/Resources/SkyHDRI.shader
  8. 115
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky/SkyRenderer.cs

3
Assets/ScriptableRenderLoop/HDRenderLoop/Editor/HDRenderLoopInspector.cs


public int[] debugViewMaterialValues = null;
public readonly GUIContent skyParameters = new GUIContent("Sky Parameters");
public readonly GUIContent skyResolution = new GUIContent("Sky Resolution");
public readonly GUIContent skyExposure = new GUIContent("Sky Exposure");
public readonly GUIContent skyRotation = new GUIContent("Sky Rotation");
public readonly GUIContent skyMultiplier = new GUIContent("Sky Multiplier");

EditorGUI.BeginChangeCheck();
skyParameters.skyHDRI = (Texture)EditorGUILayout.ObjectField("Cubemap", skyParameters.skyHDRI, typeof(Cubemap), false);
skyParameters.skyResolution = (SkyResolution)EditorGUILayout.EnumPopup(styles.skyResolution, skyParameters.skyResolution);
skyParameters.exposure = Mathf.Max(Mathf.Min(EditorGUILayout.FloatField(styles.skyExposure, skyParameters.exposure), 32), -32);
skyParameters.multiplier = Mathf.Max(EditorGUILayout.FloatField(styles.skyMultiplier, skyParameters.multiplier), 0);
skyParameters.rotation = Mathf.Max(Mathf.Min(EditorGUILayout.FloatField(styles.skyRotation, skyParameters.rotation), 360), -360);

7
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs


void Resize(Camera camera)
{
// TODO: Detect if renderdoc just load and force a resize in this case, as often renderdoc require to realloc resource.
// TODO: This is the wrong way to handle resize/allocation. We can have several different camera here, mean that the loop on camera will allocate and deallocate
// the below buffer which is bad. Best is to have a set of buffer for each camera that is persistent and reallocate resource if need
// For now consider we have only one camera that go to this code, the main one.
m_SkyRenderer.Resize(m_SkyParameters); // TODO: Also a bad naming, here we just want to realloc texture if skyparameters change (usefull for lookdev)
if (camera.pixelWidth != m_WidthOnRecord || camera.pixelHeight != m_HeightOnRecord || tilePassLightLoop.NeedResize())
{
if (m_WidthOnRecord > 0 && m_HeightOnRecord > 0)

27
Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/SinglePass/SinglePassLoop.hlsl


specularLighting += localSpecularLighting;
}
// TODO: Check the reflection hierarchy, for the current system (matching legacy unity) we must sort from bigger solid angle to lower (lower override bigger). So begging by sky
// TODO: Change the way it is done by reversing the order, from smaller solid angle to bigger, so we can early out when the weight is 1.
for (i = 0; i < _EnvLightCount; ++i)
// Only apply sky IBL if the sky texture is available.
if (_EnvLightSkyEnabled)
context.sampleReflection = SINGLE_PASS_CONTEXT_SAMPLE_REFLECTION_PROBES;
EvaluateBSDF_Env(context, V, positionWS, prelightData, _EnvLightList[i], bsdfData, localDiffuseLighting, localSpecularLighting, weight);
iblDiffuseLighting = lerp(iblDiffuseLighting, localDiffuseLighting, weight.x); // Should be remove by the compiler if it is smart as all is constant 0
// The sky is a single cubemap texture separate from the reflection probe texture array (different resolution and compression)
context.sampleReflection = SINGLE_PASS_CONTEXT_SAMPLE_SKY;
EnvLightData envLightSky = InitSkyEnvLightData(0); // The sky data are generated on the fly so the compiler can optimize the code
EvaluateBSDF_Env(context, V, positionWS, prelightData, envLightSky, bsdfData, localDiffuseLighting, localSpecularLighting, weight);
iblDiffuseLighting = lerp(iblDiffuseLighting, localDiffuseLighting, weight.x); // Should be remove by the compiler if it is smart as all is constant 0
<<<<<<< HEAD
/*
// Sky Ibl
=======
// Only apply sky IBL if the sky texture is available.
if (_EnvLightSkyEnabled)
>>>>>>> refs/remotes/origin/master
for (i = 0; i < _EnvLightCount; ++i)
context.sampleReflection = SINGLE_PASS_CONTEXT_SAMPLE_SKY;
EnvLightData envLightSky = InitSkyEnvLightData(0);
EvaluateBSDF_Env(context, V, positionWS, prelightData, envLightSky, bsdfData, localDiffuseLighting, localSpecularLighting, weight);
context.sampleReflection = SINGLE_PASS_CONTEXT_SAMPLE_REFLECTION_PROBES;
EvaluateBSDF_Env(context, V, positionWS, prelightData, _EnvLightList[i], bsdfData, localDiffuseLighting, localSpecularLighting, weight);
*/
diffuseLighting += iblDiffuseLighting;
specularLighting += iblSpecularLighting;

12
Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Lit.hlsl


ltcValue *= lightData.specularScale;
specularLighting = fresnelTerm * lightData.color * ltcValue;
}
#endif
#endif // LIT_DISPLAY_REFERENCE_AREA
}
//-----------------------------------------------------------------------------

ltcValue *= lightData.specularScale;
specularLighting = fresnelTerm * lightData.color * ltcValue;
}
#endif
#endif // LIT_DISPLAY_REFERENCE_AREA
}
//-----------------------------------------------------------------------------

if (lightData.envShapeType == ENVSHAPETYPE_BOX)
{
float3 dirLS = mul(preLightData.iblDirWS, worldToLocal);
float3 dirLS = mul(R, worldToLocal);
R = (positionWS + dist * preLightData.iblDirWS) - lightData.positionWS;
R = (positionWS + dist * R) - lightData.positionWS;
float3 dirLS = mul(preLightData.iblDirWS, worldToLocal);
float3 dirLS = mul(R, worldToLocal);
R = (positionWS + dist * preLightData.iblDirWS) - lightData.positionWS;
R = (positionWS + dist * R) - lightData.positionWS;
}
// 2. Apply the influence volume (Box volume is used for culling whatever the influence shape)

2
Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LitSurfaceData.hlsl


// TODO: think about using BC5
#ifdef _TANGENTMAP
#ifdef _NORMALMAP_TANGENT_SPACE // Normal and tangent use same space
float3 tangentTS = SAMPLE_LAYER_NORMALMAP(ADD_IDX(_TangentMap), ADD_ZERO_IDX(sampler_TangentMap), ADD_IDX(layerTexCoord.base)));
float3 tangentTS = SAMPLE_LAYER_NORMALMAP(ADD_IDX(_TangentMap), ADD_ZERO_IDX(sampler_TangentMap), ADD_IDX(layerTexCoord.base));
surfaceData.tangentWS = TransformTangentToWorld(tangentTS, input.tangentToWorld);
#else // Object space (TODO: We need to apply the world rotation here! - Require to pass world transform)
surfaceData.tangentWS = SAMPLE_LAYER_TEXTURE2D(ADD_IDX(_TangentMap), ADD_ZERO_IDX(sampler_TangentMap), ADD_IDX(layerTexCoord.base)).rgb;

4
Assets/ScriptableRenderLoop/HDRenderLoop/Sky/Resources/GGXConvolve.shader


PerceptualRoughnessToRoughness(perceptualRoughness),
_MipMapCount,
_InvOmegaP,
2048,
false);
64,
true);
return val;
}

6
Assets/ScriptableRenderLoop/HDRenderLoop/Sky/Resources/SkyHDRI.shader


float3 rotDirY = float3(sinPhi, 0, cosPhi);
dir = float3(dot(rotDirX, dir), dir.y, dot(rotDirY, dir));
/*
Coordinate coord = GetCoordinate(input.positionCS.xy, _ScreenSize.zw);
// If the sky box is too far away (depth set to 0), the resulting look is too foggy.

}
#endif
*/
return float4(skyColor, 1.0);
return float4(skyColor * (skyTexWeight * extinction) + scatter, extinction);
//return float4(skyColor * (skyTexWeight * extinction) + scatter, extinction);
}
ENDHLSL

115
Assets/ScriptableRenderLoop/HDRenderLoop/Sky/SkyRenderer.cs


namespace UnityEngine.Experimental.ScriptableRenderLoop
{
[Serializable]
public enum SkyResolution
{
SkyResolution128 = 128,
SkyResolution256 = 256,
SkyResolution512 = 512,
SkyResolution1024 = 1024,
// TODO: Anything above 1024 cause a crash in Unity...
//SkyResolution2048 = 2048,
//SkyResolution4096 = 4096
}
[Serializable]
public float multiplier = 1.0f;
public float multiplier = 1.0f;
public SkyResolution skyResolution = SkyResolution.SkyResolution256;
const int kSkyCubemapSize = 256;
RenderTexture m_SkyboxCubemapRT = null;
RenderTexture m_SkyboxGGXCubemapRT = null;

SkyParameters m_bakedSkyParameters = new SkyParameters(); // This is the SkyParam used when baking and convolving the sky.
Mesh[] m_CubemapFaceMesh = new Mesh[6];
Mesh BuildSkyMesh(Camera camera, bool forceUVBottom)
{

vertData[2] = new Vector3(vertData2.x, vertData2.y, vertData2.z);
vertData[3] = new Vector3(vertData3.x, vertData3.y, vertData3.z);
// Get view vector vased on the frustrum, i.e (invert transform frustrum get position etc...)
// Get view vector based on the frustum, i.e (invert transform frustum get position etc...)
Vector3[] eyeVectorData = new Vector3[4];
Matrix4x4 transformMatrix = camera.cameraToWorldMatrix * camera.projectionMatrix.inverse;

};
}
void RebuildTextures()
void RebuildTextures(SkyParameters skyParameters)
if(m_SkyboxCubemapRT == null)
if ((m_SkyboxCubemapRT != null) && (m_SkyboxCubemapRT.width != (int)skyParameters.skyResolution))
m_SkyboxCubemapRT = new RenderTexture(kSkyCubemapSize, kSkyCubemapSize, 1, RenderTextureFormat.ARGBHalf);
Utilities.Destroy(m_SkyboxCubemapRT);
m_SkyboxCubemapRT = null;
Utilities.Destroy(m_SkyboxGGXCubemapRT);
m_SkyboxGGXCubemapRT = null;
}
if (m_SkyboxCubemapRT == null)
{
m_SkyboxCubemapRT = new RenderTexture((int)skyParameters.skyResolution, (int)skyParameters.skyResolution, 1, RenderTextureFormat.ARGBHalf);
m_SkyboxCubemapRT.autoGenerateMips = true;
m_SkyboxCubemapRT.filterMode = FilterMode.Point;
m_SkyboxCubemapRT.autoGenerateMips = true; // Generate regular mipmap for filtered importance sampling
m_SkyboxCubemapRT.filterMode = FilterMode.Trilinear;
}
if(m_SkyboxGGXCubemapRT == null)
{
m_SkyboxGGXCubemapRT = new RenderTexture(kSkyCubemapSize, kSkyCubemapSize, 1, RenderTextureFormat.ARGBHalf);
m_SkyboxGGXCubemapRT = new RenderTexture((int)skyParameters.skyResolution, (int)skyParameters.skyResolution, 1, RenderTextureFormat.ARGBHalf);
m_SkyboxGGXCubemapRT.dimension = TextureDimension.Cube;
m_SkyboxGGXCubemapRT.useMipMap = true;
m_SkyboxGGXCubemapRT.autoGenerateMips = false;

Shader.SetGlobalTexture("_SkyTexture", m_SkyboxGGXCubemapRT);
}
public void Resize(SkyParameters skyParameters)
{
// When loading RenderDoc, RenderTextures will go null
RebuildTextures(skyParameters);
}
public void Rebuild()
{
// TODO: We need to have an API to send our sky information to Enlighten. For now use a workaround through skybox/cubemap material...

m_GGXConvolveMaterial = Utilities.CreateEngineMaterial("Hidden/HDRenderLoop/GGXConvolve");
m_RenderSkyPropertyBlock = new MaterialPropertyBlock();
RebuildTextures();
m_RenderSkyPropertyBlock = new MaterialPropertyBlock();
Matrix4x4 cubeProj = Matrix4x4.Perspective(90.0f, 1.0f, 0.1f, 1.0f);

camera.projectionMatrix = cubeProj;
Transform transform = camera.GetComponent<Transform>();
transform.LookAt(lookAtList[i], UpVectorList[i]);
// When rendering into a texture the render will be flip (due to legacy unity openGL behavior), so we need to flip UV here...
m_CubemapFaceMesh[i] = BuildSkyMesh(camera, true);
}
}

return parameters.skyHDRI != null;
}
private void RenderSky(Camera camera, SkyParameters skyParameters, bool forceUVBottom, RenderLoop renderLoop)
private void RenderSky(Camera camera, SkyParameters skyParameters, Mesh skyMesh, RenderLoop renderLoop)
Mesh skyMesh = BuildSkyMesh(camera, forceUVBottom);
Shader.EnableKeyword("PERFORM_SKY_OCCLUSION_TEST");
m_RenderSkyPropertyBlock.SetTexture("_Cubemap", skyParameters.skyHDRI);

{
Utilities.SetRenderTarget(renderLoop, target, 0, (CubemapFace)i);
Camera faceCamera = m_CubemapFaceCamera[i].GetComponent<Camera>();
RenderSky(faceCamera, skyParameters, true, renderLoop);
RenderSky(faceCamera, skyParameters, m_CubemapFaceMesh[i], renderLoop);
}
}

Utilities.SetRenderTarget(renderLoop, target, mip, (CubemapFace)face);
Camera faceCamera = m_CubemapFaceCamera[face].GetComponent<Camera>();
Mesh skyMesh = BuildSkyMesh(faceCamera, true);
cmd.DrawMesh(skyMesh, Matrix4x4.identity, m_GGXConvolveMaterial, 0, 0, propertyBlock);
cmd.DrawMesh(m_CubemapFaceMesh[face], Matrix4x4.identity, m_GGXConvolveMaterial, 0, 0, propertyBlock);
renderLoop.ExecuteCommandBuffer(cmd);
cmd.Dispose();
}

{
using (new Utilities.ProfilingSample("Sky Pass", renderLoop))
{
//using (new EditorGUI.DisabledScope(m_LookDevEnvLibrary.hdriList.Count <= 1))
// When loading RenderDoc, RenderTextures will go null
RebuildTextures();
// Trigger a rebuild of cubemap / convolution
// TODO: can we have some kind of hash value here ? +> use or override GetHashCode() + include a refresh rate value in parameters
if (skyParameters.skyResolution != m_bakedSkyParameters.skyResolution ||
skyParameters.exposure != m_bakedSkyParameters.exposure ||
skyParameters.rotation != m_bakedSkyParameters.rotation ||
skyParameters.multiplier != m_bakedSkyParameters.multiplier)
{
using (new Utilities.ProfilingSample("Sky Pass: Render Cubemap", renderLoop))
{
// Render sky into a cubemap - doesn't happen every frame, can be controlled
RenderSkyToCubemap(skyParameters, m_SkyboxCubemapRT, renderLoop);
// Convolve downsampled cubemap
RenderCubemapGGXConvolution(m_SkyboxCubemapRT, m_SkyboxGGXCubemapRT, renderLoop);
using (new Utilities.ProfilingSample("Sky Pass: Render Cubemap", renderLoop))
{
// Render sky into a cubemap - doesn't happen every frame, can be controlled
RenderSkyToCubemap(skyParameters, m_SkyboxCubemapRT, renderLoop);
// Convolve downsampled cubemap
RenderCubemapGGXConvolution(m_SkyboxCubemapRT, m_SkyboxGGXCubemapRT, renderLoop);
// TODO: Properly send the cubemap to Enlighten. Currently workaround is to set the cubemap in a Skybox/cubemap material
m_StandardSkyboxMaterial.SetTexture("_Tex", m_SkyboxCubemapRT);
RenderSettings.skybox = m_StandardSkyboxMaterial; // Setup this material as the default to be use in RenderSettings
RenderSettings.ambientIntensity = 1.0f; // fix this to 1, this parameter should not exist!
RenderSettings.ambientMode = UnityEngine.Rendering.AmbientMode.Skybox; // Force skybox for our HDRI
RenderSettings.reflectionIntensity = 1.0f;
RenderSettings.customReflection = null;
//DynamicGI.UpdateEnvironment();
}
// TODO: Properly send the cubemap to Enlighten. Currently workaround is to set the cubemap in a Skybox/cubemap material
m_StandardSkyboxMaterial.SetTexture("_Tex", m_SkyboxCubemapRT);
RenderSettings.skybox = m_StandardSkyboxMaterial; // Setup this material as the default to be use in RenderSettings
RenderSettings.ambientIntensity = 1.0f; // fix this to 1, this parameter should not exist!
RenderSettings.ambientMode = UnityEngine.Rendering.AmbientMode.Skybox; // Force skybox for our HDRI
RenderSettings.reflectionIntensity = 1.0f;
RenderSettings.customReflection = null;
//DynamicGI.UpdateEnvironment();
// Cleanup all this...
m_bakedSkyParameters.skyResolution = skyParameters.skyResolution;
m_bakedSkyParameters.exposure = skyParameters.exposure;
m_bakedSkyParameters.rotation = skyParameters.rotation;
m_bakedSkyParameters.multiplier = skyParameters.multiplier;
RenderSky(camera, skyParameters, false, renderLoop);
RenderSky(camera, skyParameters, BuildSkyMesh(camera, false), renderLoop);
}
}
}
正在加载...
取消
保存