浏览代码

Started work on light optimizations for low tier mobiles

/LW-LightOptimizations
Joel de Vahl 6 年前
当前提交
f3cd88c5
共有 8 个文件被更改,包括 116 次插入7 次删除
  1. 15
      ScriptableRenderPipeline/Core/CoreRP/CoreUtils.cs
  2. 6
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineAsset.cs
  3. 6
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Editor/LightweightAssetEditor.cs
  4. 36
      ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipeline.cs
  5. 54
      ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/Lighting.hlsl
  6. 2
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightStandard.shader
  7. 2
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightStandardSimpleLighting.shader
  8. 2
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightStandardTerrain.shader

15
ScriptableRenderPipeline/Core/CoreRP/CoreUtils.cs


material.DisableKeyword(enableFirst ? keyword2 : keyword1);
}
public static void SelectKeyword(CommandBuffer cmd, string[] keywords, int enabledKeywordIndex)
{
if(enabledKeywordIndex < keywords.Length && enabledKeywordIndex >= 0)
cmd.EnableShaderKeyword(keywords[enabledKeywordIndex]);
for (int i = 0; i < keywords.Length; i++)
{
if (i != enabledKeywordIndex)
cmd.DisableShaderKeyword(keywords[i]);
}
}
material.EnableKeyword(keywords[enabledKeywordIndex]);
if(enabledKeywordIndex < keywords.Length && enabledKeywordIndex >= 0)
material.EnableKeyword(keywords[enabledKeywordIndex]);
for (int i = 0; i < keywords.Length; i++)
{

6
ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineAsset.cs


// Default values set when a new LightweightPipeline asset is created
[SerializeField] private int kAssetVersion = 2;
[SerializeField] private int m_MaxPixelLights = 4;
[SerializeField] private bool m_BaseTierLighting = false;
[SerializeField] private bool m_SupportsVertexLight = false;
[SerializeField] private bool m_RequireDepthTexture = false;
[SerializeField] private bool m_RequireSoftParticles = false;

public int MaxPixelLights
{
get { return m_MaxPixelLights; }
}
public bool BaseTierLighting
{
get { return m_BaseTierLighting; }
}
public bool SupportsVertexLight

6
ScriptableRenderPipeline/LightweightPipeline/LWRP/Editor/LightweightAssetEditor.cs


public static GUIContent maxPixelLightsLabel = new GUIContent("Pixel Lights",
"Controls the amount of pixel lights that run in fragment light loop. Lights are sorted and culled per-object.");
public static GUIContent enableLowTierLightingLabel = new GUIContent("Low Tier Lighting",
"If enabled lighting evaluation will be capped at max 4 per pixel, and 4 per vertex. Evaluation will also be changed to a branch less style that suits older mobile hardware tiers better");
public static GUIContent enableVertexLightLabel = new GUIContent("Vertex Lighting",
"If enabled shades additional lights exceeding the maximum number of pixel lights per-vertex up to the maximum of 8 lights.");

private float kMaxRenderScale = 4.0f;
private SerializedProperty m_RenderScale;
private SerializedProperty m_MaxPixelLights;
private SerializedProperty m_BaseTierLightingProp;
private SerializedProperty m_SupportsVertexLightProp;
private SerializedProperty m_RequireDepthTextureProp;
private SerializedProperty m_RequireSoftParticlesProp;

{
m_RenderScale = serializedObject.FindProperty("m_RenderScale");
m_MaxPixelLights = serializedObject.FindProperty("m_MaxPixelLights");
m_BaseTierLightingProp = serializedObject.FindProperty("m_BaseTierLighting");
m_SupportsVertexLightProp = serializedObject.FindProperty("m_SupportsVertexLight");
m_RequireDepthTextureProp = serializedObject.FindProperty("m_RequireDepthTexture");
m_RequireSoftParticlesProp = serializedObject.FindProperty("m_RequireSoftParticles");

EditorGUILayout.LabelField(Styles.maxPixelLightsLabel);
m_MaxPixelLights.intValue = EditorGUILayout.IntSlider(m_MaxPixelLights.intValue, 0, kMaxSupportedPixelLights);
EditorGUILayout.EndHorizontal();
EditorGUILayout.PropertyField(m_BaseTierLightingProp, Styles.enableLowTierLightingLabel);
EditorGUILayout.PropertyField(m_SupportsVertexLightProp, Styles.enableVertexLightLabel);
EditorGUILayout.PropertyField(m_RequireDepthTextureProp, Styles.requireDepthTexture);
DrawAnimatedProperty(m_RequireSoftParticlesProp, Styles.requireSoftParticles, m_ShowSoftParticles);

36
ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipeline.cs


"_MAIN_LIGHT_SPOT_SHADOW",
"_MAIN_LIGHT_SPOT_SHADOW_SOFT"
};
private static readonly string[] kAdditionalLightKeywords = {
"_ADDITIONAL_LIGHTS",
"_ADDITIONAL_LIGHT0",
"_ADDITIONAL_LIGHT2",
};
private static readonly string[] kVertexLightKeywords = {
"_VERTEX_LIGHTS",
"_VERTEX_LIGHTS_UNROLL",
};
private StringBuilder m_MainLightKeywordString = new StringBuilder(43);

cmd.SetGlobalVector(ShadowConstantBuffer._ShadowmapSize, new Vector4(invShadowResolution, invShadowResolution, m_Asset.ShadowAtlasResolution, m_Asset.ShadowAtlasResolution));
}
private int CalcAdditionalLightKeywordIndex(int numAdditionalLights)
{
if(numAdditionalLights == 0)
return -1;
else if(!m_Asset.BaseTierLighting)
return 0;
else if(numAdditionalLights == 1)
return 1;
else
return 2;
}
private int CalcVertexLightKeywordIndex(int numVertexLights)
{
if(numVertexLights == 0)
return -1;
else if(!m_Asset.BaseTierLighting)
return 0;
else
return 1;
}
private void SetShaderKeywords(CommandBuffer cmd, ref LightData lightData, List<VisibleLight> visibleLights)
{
int vertexLightsCount = lightData.totalAdditionalLightsCount - lightData.pixelAdditionalLightsCount;

}
CoreUtils.SetKeyword(cmd, "_MAIN_LIGHT_COOKIE", mainLightIndex != -1 && LightweightUtils.IsSupportedCookieType(visibleLights[mainLightIndex].lightType) && visibleLights[mainLightIndex].light.cookie != null);
CoreUtils.SetKeyword(cmd, "_ADDITIONAL_LIGHTS", lightData.totalAdditionalLightsCount > 0);
CoreUtils.SelectKeyword(cmd, kAdditionalLightKeywords, CalcAdditionalLightKeywordIndex(lightData.totalAdditionalLightsCount));
CoreUtils.SelectKeyword(cmd, kVertexLightKeywords, CalcVertexLightKeywordIndex(vertexLightsCount));
CoreUtils.SetKeyword(cmd, "SOFTPARTICLES_ON", m_RequireDepthTexture && m_Asset.RequireSoftParticles);
bool linearFogModeEnabled = false;

54
ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/Lighting.hlsl


#include "Shadows.hlsl"
#ifdef NO_ADDITIONAL_LIGHTS
#undef _ADDITIONAL_LIGHT0
#undef _ADDITIONAL_LIGHT2
#undef _ADDITIONAL_LIGHTS
#endif

half3 lightColor = light.color * light.attenuation;
vertexLightColor += LightingLambert(lightColor, light.direction, normalWS);
}
#elif defined(_VERTEX_LIGHTS_UNROLLED)
int vertexLightStart = _AdditionalLightCount.x;
Light light0 = GetLight(vertexLightStart + 0, positionWS);
half3 lightColor0 = light0.color * light0.attenuation;
vertexLightColor += LightingLambert(lightColor0, light0.direction, normalWS);
Light light1 = GetLight(vertexLightStart + 1, positionWS);
half3 lightColor1 = light1.color * light1.attenuation;
vertexLightColor += LightingLambert(lightColor1, light1.direction, normalWS);
Light light2 = GetLight(vertexLightStart + 2, positionWS);
half3 lightColor2 = light2.color * light2.attenuation;
vertexLightColor += LightingLambert(lightColor2, light2.direction, normalWS);
Light light3 = GetLight(vertexLightStart + 3, positionWS);
half3 lightColor3 = light3.color * light3.attenuation;
vertexLightColor += LightingLambert(lightColor03 light3.direction, normalWS);
#endif
return vertexLightColor;

half3 color = GlobalIllumination(brdfData, inputData.bakedGI, occlusion, inputData.normalWS, inputData.viewDirectionWS);
color += LightingPhysicallyBased(brdfData, mainLight, inputData.normalWS, inputData.viewDirectionWS);
#ifdef _ADDITIONAL_LIGHTS
#if defined(_ADDITIONAL_LIGHT0) || defined(_ADDITIONAL_LIGHT2)
Light light0 = GetLight(0, inputData.positionWS);
color += LightingPhysicallyBased(brdfData, light0, inputData.normalWS, inputData.viewDirectionWS);
#if defined(_ADDITIONAL_LIGHT2)
Light light1 = GetLight(1, inputData.positionWS);
color += LightingPhysicallyBased(brdfData, light1, inputData.normalWS, inputData.viewDirectionWS);
Light light2 = GetLight(2, inputData.positionWS);
color += LightingPhysicallyBased(brdfData, light2, inputData.normalWS, inputData.viewDirectionWS);
#endif
#elif defined(_ADDITIONAL_LIGHTS)
int pixelLightCount = GetPixelLightCount();
for (int i = 0; i < pixelLightCount; ++i)
{

half3 diffuseColor = inputData.bakedGI + LightingLambert(attenuatedLightColor, mainLight.direction, inputData.normalWS);
half3 specularColor = LightingSpecular(attenuatedLightColor, mainLight.direction, inputData.normalWS, inputData.viewDirectionWS, specularGloss, shininess);
#ifdef _ADDITIONAL_LIGHTS
#if defined(_ADDITIONAL_LIGHT0) || defined(_ADDITIONAL_LIGHT2)
Light light0 = GetLight(0, inputData.positionWS);
half3 attenuatedLightColor0 = light0.color * light0.attenuation;
diffuseColor += LightingLambert(attenuatedLightColor0, light0.direction, inputData.normalWS);
specularColor += LightingSpecular(attenuatedLightColor0, light0.direction, inputData.normalWS, inputData.viewDirectionWS, specularGloss, shininess);
#if defined(_ADDITIONAL_LIGHT2)
Light light1 = GetLight(1, inputData.positionWS);
half3 attenuatedLightColor1 = light1.color * light1.attenuation;
diffuseColor += LightingLambert(attenuatedLightColor1, light1.direction, inputData.normalWS);
specularColor += LightingSpecular(attenuatedLightColor1, light1.direction, inputData.normalWS, inputData.viewDirectionWS, specularGloss, shininess);
Light light2 = GetLight(2, inputData.positionWS);
half3 attenuatedLightColor2 = light2.color * light2.attenuation;
diffuseColor += LightingLambert(attenuatedLightColor2, light2.direction, inputData.normalWS);
specularColor += LightingSpecular(attenuatedLightColor2, light2.direction, inputData.normalWS, inputData.viewDirectionWS, specularGloss, shininess);
#endif
#elif defined(_ADDITIONAL_LIGHTS)
int pixelLightCount = GetPixelLightCount();
for (int i = 0; i < pixelLightCount; ++i)
{

2
ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightStandard.shader


// Shadow.hlsl defines _SHADOWS_ENABLED, _SHADOWS_SOFT, _SHADOWS_CASCADE, _SHADOWS_PERSPECTIVE
#pragma multi_compile _ _MAIN_LIGHT_DIRECTIONAL_SHADOW _MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE _MAIN_LIGHT_DIRECTIONAL_SHADOW_SOFT _MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE_SOFT _MAIN_LIGHT_SPOT_SHADOW _MAIN_LIGHT_SPOT_SHADOW_SOFT
#pragma multi_compile _ _MAIN_LIGHT_COOKIE
#pragma multi_compile _ _ADDITIONAL_LIGHTS
#pragma multi_compile _ _ADDITIONAL_LIGHT0 _ADDITIONAL_LIGHT2 _ADDITIONAL_LIGHTS
#pragma multi_compile _ _VERTEX_LIGHTS
#pragma multi_compile _ _MIXED_LIGHTING_SUBTRACTIVE
#pragma multi_compile _ FOG_LINEAR FOG_EXP2

2
ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightStandardSimpleLighting.shader


// Shadow.hlsl defines _SHADOWS_ENABLED, _SHADOWS_SOFT, _SHADOWS_CASCADE, _SHADOWS_PERSPECTIVE
#pragma multi_compile _ _MAIN_LIGHT_DIRECTIONAL_SHADOW _MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE _MAIN_LIGHT_DIRECTIONAL_SHADOW_SOFT _MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE_SOFT _MAIN_LIGHT_SPOT_SHADOW _MAIN_LIGHT_SPOT_SHADOW_SOFT
#pragma multi_compile _ _MAIN_LIGHT_COOKIE
#pragma multi_compile _ _ADDITIONAL_LIGHTS
#pragma multi_compile _ _ADDITIONAL_LIGHT1 _ADDITIONAL_LIGHT2 _ADDITIONAL_LIGHTS
#pragma multi_compile _ _VERTEX_LIGHTS
#pragma multi_compile _ _MIXED_LIGHTING_SUBTRACTIVE
#pragma multi_compile _ FOG_LINEAR FOG_EXP2

2
ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightStandardTerrain.shader


// Shadow.hlsl defines _SHADOWS_ENABLED, _SHADOWS_SOFT, _SHADOWS_CASCADE, _SHADOWS_PERSPECTIVE
#pragma multi_compile _ _MAIN_LIGHT_DIRECTIONAL_SHADOW _MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE _MAIN_LIGHT_DIRECTIONAL_SHADOW_SOFT _MAIN_LIGHT_DIRECTIONAL_SHADOW_CASCADE_SOFT _MAIN_LIGHT_SPOT_SHADOW _MAIN_LIGHT_SPOT_SHADOW_SOFT
#pragma multi_compile _ _MAIN_LIGHT_COOKIE
#pragma multi_compile _ _ADDITIONAL_LIGHTS
#pragma multi_compile _ _ADDITIONAL_LIGHT0 _ADDITIONAL_LIGHT2 _ADDITIONAL_LIGHTS
#pragma multi_compile _ _VERTEX_LIGHTS
#pragma multi_compile _ _MIXED_LIGHTING_SUBTRACTIVE
#pragma multi_compile _ FOG_LINEAR FOG_EXP2

正在加载...
取消
保存