浏览代码

Merge branch 'LightweightPipeline'

/sample_game
Felipe Lira 7 年前
当前提交
aeda1de5
共有 7 个文件被更改,包括 118 次插入60 次删除
  1. 4
      Assets/ScriptableRenderPipeline/LightweightPipeline/Editor/LightweightAssetInspector.cs
  2. 107
      Assets/ScriptableRenderPipeline/LightweightPipeline/LightweightPipeline.cs
  3. 1
      Assets/ScriptableRenderPipeline/LightweightPipeline/LightweightPipelineAsset.asset
  4. 7
      Assets/ScriptableRenderPipeline/LightweightPipeline/LightweightPipelineAsset.cs
  5. 18
      Assets/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipeline.shader
  6. 32
      Assets/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipelineCore.cginc
  7. 9
      Assets/Tests/GraphicsTests/RenderPipeline/LightweightPipeline/Assets/LDPipe_CommonAssets/Cornell Box.fbx.meta

4
Assets/ScriptableRenderPipeline/LightweightPipeline/Editor/LightweightAssetInspector.cs


public static GUIContent renderingLabel = new GUIContent("Rendering");
public static GUIContent shadowLabel = new GUIContent("Shadows");
public static GUIContent defaults = new GUIContent("Defaults");
public static GUIContent linearRenderingLabel = new GUIContent("Linear Colorspace", "When enabled Lightweight shader will perform gamma to linear conversion when linear rendering is not supported or disabled");
public static GUIContent maxPixelLights = new GUIContent("Per-Object Pixel Lights",
"Max amount of dynamic per-object pixel lights.");

public static GUIContent msaaContent = new GUIContent("Anti Aliasing", "Controls the global anti aliasing quality. When set to disabled, MSAA will not be performed even if the camera allows it.");
}
private SerializedProperty m_LinearRenderingProperty;
private SerializedProperty m_MaxPixelLights;
private SerializedProperty m_SupportsVertexLightProp;
private SerializedProperty m_EnableLightmapsProp;

void OnEnable()
{
m_LinearRenderingProperty = serializedObject.FindProperty("m_LinearRendering");
m_MaxPixelLights = serializedObject.FindProperty("m_MaxPixelLights");
m_SupportsVertexLightProp = serializedObject.FindProperty("m_SupportsVertexLight");
m_EnableLightmapsProp = serializedObject.FindProperty("m_EnableLightmaps");

EditorGUILayout.Space();
EditorGUILayout.LabelField(Styles.renderingLabel, EditorStyles.boldLabel);
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(m_LinearRenderingProperty, Styles.linearRenderingLabel);
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField(Styles.maxPixelLights);
m_MaxPixelLights.intValue = EditorGUILayout.IntSlider(m_MaxPixelLights.intValue, 0, 4);

107
Assets/ScriptableRenderPipeline/LightweightPipeline/LightweightPipeline.cs


using System;
using UnityEngine.Rendering;
using UnityEngine.Rendering.PostProcessing;
using UnityEngine.XR;
namespace UnityEngine.Experimental.Rendering.LightweightPipeline

private Vector4[] m_LightAttenuations = new Vector4[kMaxVisibleLights];
private Vector4[] m_LightSpotDirections = new Vector4[kMaxVisibleLights];
private Camera m_CurrCamera = null;
private int m_LightIndicesCount = 0;
private ComputeBuffer m_LightIndexListBuffer;

private static readonly ShaderPassName m_LitPassName = new ShaderPassName("LightweightForward");
private static readonly ShaderPassName m_UnlitPassName = new ShaderPassName("SRPDefaultUnlit");
private RenderTextureFormat m_ColorFormat = RenderTextureFormat.ARGB32;
private PostProcessRenderContext m_PostProcessRenderContext;
public LightweightPipeline(LightweightPipelineAsset asset)
{
m_Asset = asset;

m_CameraRTProperty = Shader.PropertyToID("_CameraRT");
m_ShadowMapRTID = new RenderTargetIdentifier(m_ShadowMapProperty);
m_CameraRTID = new RenderTargetIdentifier(m_CameraRTProperty);
m_PostProcessRenderContext = new PostProcessRenderContext();
// Let engine know we have MSAA on for cases where we support MSAA backbuffer
if (QualitySettings.antiAliasing != m_Asset.MSAASampleCount)

foreach (Camera camera in cameras)
{
m_CurrCamera = camera;
if (!CullResults.GetCullingParameters(camera, stereoEnabled, out cullingParameters))
if (!CullResults.GetCullingParameters(m_CurrCamera, stereoEnabled, out cullingParameters))
cullingParameters.shadowDistance = Mathf.Min(m_ShadowSettings.maxShadowDistance, camera.farClipPlane);
cullingParameters.shadowDistance = Mathf.Min(m_ShadowSettings.maxShadowDistance, m_CurrCamera.farClipPlane);
CullResults.Cull(ref cullingParameters, context,ref m_CullResults);
VisibleLight[] visibleLights = m_CullResults.visibleLights.ToArray();

lightData.shadowsRendered = RenderShadows(ref m_CullResults, ref visibleLights[lightData.shadowLightIndex], lightData.shadowLightIndex, ref context);
// Setup camera matrices and RT
context.SetupCameraProperties(camera, stereoEnabled);
context.SetupCameraProperties(m_CurrCamera, stereoEnabled);
// Setup light and shadow shader constants
SetupShaderLightConstants(visibleLights, ref lightData, ref m_CullResults, ref context);

if (!lightData.isSingleDirectionalLight)
configuration |= RendererConfiguration.PerObjectLightIndices8;
BeginForwardRendering(camera, ref context, stereoEnabled);
PostProcessLayer postProcessLayer = GetCurrCameraPostProcessLayer();
bool postProcessEnabled = postProcessLayer != null && postProcessLayer.enabled;
m_RenderToIntermediateTarget = postProcessEnabled || GetRenderToIntermediateTarget();
BeginForwardRendering(ref context, stereoEnabled);
var litSettings = new DrawRendererSettings(m_CullResults, camera, m_LitPassName);
var litSettings = new DrawRendererSettings(m_CullResults, m_CurrCamera, m_LitPassName);
var unlitSettings = new DrawRendererSettings(m_CullResults, camera, m_UnlitPassName);
var unlitSettings = new DrawRendererSettings(m_CullResults, m_CurrCamera, m_UnlitPassName);
unlitSettings.sorting.flags = SortFlags.CommonTransparent;
unlitSettings.inputFilter.SetQueuesTransparent();

context.DrawSkybox(camera);
context.DrawSkybox(m_CurrCamera);
RenderStateBlock renderStateBlock = new RenderStateBlock();
context.DrawRenderers(ref litSettings, renderStateBlock);
EndForwardRendering(camera, ref context, stereoEnabled);
if (postProcessEnabled)
RenderPostProcess(ref context, postProcessLayer);
EndForwardRendering(ref context, stereoEnabled, postProcessEnabled);
// Release temporary RT
var discardRT = CommandBufferPool.Get();
discardRT.ReleaseTemporaryRT(m_ShadowMapProperty);

private void SetShaderKeywords(CommandBuffer cmd, bool renderShadows, bool singleDirecitonal, bool vertexLightSupport)
{
if (m_Asset.ForceLinearRendering)
cmd.EnableShaderKeyword("LIGHTWEIGHT_LINEAR");
else
cmd.DisableShaderKeyword("LIGHTWEIGHT_LINEAR");
if (vertexLightSupport)
cmd.EnableShaderKeyword("_VERTEX_LIGHTS");
else

return (type == LightType.Directional || type == LightType.Spot);
}
private void BeginForwardRendering(Camera camera, ref ScriptableRenderContext context, bool stereoEnabled)
private void BeginForwardRendering(ref ScriptableRenderContext context, bool stereoEnabled)
context.StartMultiEye(camera);
m_RenderToIntermediateTarget = GetRenderToIntermediateTarget(camera);
context.StartMultiEye(m_CurrCamera);
if (camera.activeTexture == null)
if (m_CurrCamera.activeTexture == null)
{
m_IntermediateTextureArray = false;
if (stereoEnabled)

xrDesc.colorFormat = RenderTextureFormat.ARGB32;
xrDesc.colorFormat = m_ColorFormat;
xrDesc.msaaSamples = m_Asset.MSAASampleCount;
m_IntermediateTextureArray = (xrDesc.dimension == TextureDimension.Tex2DArray);

else
{
cmd.GetTemporaryRT(m_CameraRTProperty, Screen.width, Screen.height, kCameraDepthBufferBits,
FilterMode.Bilinear, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default, m_Asset.MSAASampleCount);
FilterMode.Bilinear, m_ColorFormat, RenderTextureReadWrite.Default, m_Asset.MSAASampleCount);
}
if (m_IntermediateTextureArray)

}
else
{
cmd.SetRenderTarget(new RenderTargetIdentifier(camera.activeTexture));
cmd.SetRenderTarget(new RenderTargetIdentifier(m_CurrCamera.activeTexture));
}
}
else

// Clear RenderTarget to avoid tile initialization on mobile GPUs
// https://community.arm.com/graphics/b/blog/posts/mali-performance-2-how-to-correctly-handle-framebuffers
if (camera.clearFlags != CameraClearFlags.Nothing)
if (m_CurrCamera.clearFlags != CameraClearFlags.Nothing)
bool clearDepth = (camera.clearFlags != CameraClearFlags.Nothing);
bool clearColor = (camera.clearFlags == CameraClearFlags.Color);
cmd.ClearRenderTarget(clearDepth, clearColor, camera.backgroundColor);
bool clearDepth = (m_CurrCamera.clearFlags != CameraClearFlags.Nothing);
bool clearColor = (m_CurrCamera.clearFlags == CameraClearFlags.Color);
cmd.ClearRenderTarget(clearDepth, clearColor, m_CurrCamera.backgroundColor);
}
context.ExecuteCommandBuffer(cmd);

private void EndForwardRendering(Camera camera, ref ScriptableRenderContext context, bool stereoEnabled)
private void EndForwardRendering(ref ScriptableRenderContext context, bool stereoEnabled, bool postProcessing)
if (m_RenderToIntermediateTarget)
if (m_RenderToIntermediateTarget || postProcessing)
{
var cmd = CommandBufferPool.Get("Blit");
if (m_IntermediateTextureArray)

}
else
// If PostProcessing is enabled, it is already blitted to CameraTarget.
else if (!postProcessing)
if (camera.cameraType == CameraType.SceneView)
cmd.SetRenderTarget(BuiltinRenderTextureType.CameraTarget);
cmd.SetRenderTarget(BuiltinRenderTextureType.CameraTarget);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}

context.StopMultiEye(camera);
context.StereoEndRender(camera);
context.StopMultiEye(m_CurrCamera);
context.StereoEndRender(m_CurrCamera);
private bool GetRenderToIntermediateTarget(Camera camera)
private void RenderPostProcess(ref ScriptableRenderContext renderContext, PostProcessLayer postProcessLayer)
bool allowMSAA = camera.allowMSAA && m_Asset.MSAASampleCount > 1 && !PlatformSupportsMSAABackBuffer();
if (camera.cameraType == CameraType.SceneView || allowMSAA || camera.activeTexture != null)
var postProcessCommand = CommandBufferPool.Get("Post Processing");
m_PostProcessRenderContext.Reset();
m_PostProcessRenderContext.camera = m_CurrCamera;
m_PostProcessRenderContext.source = BuiltinRenderTextureType.CurrentActive;
m_PostProcessRenderContext.sourceFormat = m_ColorFormat;
m_PostProcessRenderContext.destination = BuiltinRenderTextureType.CameraTarget;
m_PostProcessRenderContext.command = postProcessCommand;
m_PostProcessRenderContext.flip = true;
postProcessLayer.Render(m_PostProcessRenderContext);
renderContext.ExecuteCommandBuffer(postProcessCommand);
CommandBufferPool.Release(postProcessCommand);
}
private bool GetRenderToIntermediateTarget()
{
bool allowMSAA = m_CurrCamera.allowMSAA && m_Asset.MSAASampleCount > 1 && !PlatformSupportsMSAABackBuffer();
if (m_CurrCamera.cameraType == CameraType.SceneView || allowMSAA || m_CurrCamera.activeTexture != null)
}
private PostProcessLayer GetCurrCameraPostProcessLayer()
{
return m_CurrCamera.GetComponent<PostProcessLayer>();
}
private bool PlatformSupportsMSAABackBuffer()

1
Assets/ScriptableRenderPipeline/LightweightPipeline/LightweightPipelineAsset.asset


m_ShadowCascades: 1
m_Cascade2Split: 0.25
m_Cascade4Split: {x: 0.067, y: 0.2, z: 0.467}
m_LinearRendering: 1
m_DefaultDiffuseMaterial: {fileID: 2100000, guid: 6a1143ee683302f4aa628c052723efc1,
type: 2}
m_DefaultParticleMaterial: {fileID: 2100000, guid: e823cd5b5d27c0f4b8256e7c12ee3e6d,

7
Assets/ScriptableRenderPipeline/LightweightPipeline/LightweightPipelineAsset.cs


[SerializeField] private ShadowCascades m_ShadowCascades = ShadowCascades.NO_CASCADES;
[SerializeField] private float m_Cascade2Split = 0.25f;
[SerializeField] private Vector3 m_Cascade4Split = new Vector3(0.067f, 0.2f, 0.467f);
[SerializeField] private bool m_LinearRendering = true;
[SerializeField] private Material m_DefaultDiffuseMaterial;
[SerializeField] private Material m_DefaultParticleMaterial;

{
get { return m_Cascade4Split; }
private set { m_Cascade4Split = value; }
}
public bool ForceLinearRendering
{
get { return m_LinearRendering; }
set { m_LinearRendering = value; }
}
#endregion

18
Assets/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipeline.shader


// Shader targeted for low end devices. Single Pass Forward Rendering. Shader Model 2
// Shader targeted for low end devices. Single Pass Forward Rendering. Shader Model 2
Shader "ScriptableRenderPipeline/LightweightPipeline/NonPBR"
{

#pragma shader_feature _EMISSION
#pragma shader_feature _ _REFLECTION_CUBEMAP _REFLECTION_PROBE
#pragma multi_compile _ LIGHTWEIGHT_LINEAR
#pragma multi_compile _ UNITY_SINGLE_PASS_STEREO STEREO_INSTANCING_ON STEREO_MULTIVIEW_ON
#pragma multi_compile _ _SINGLE_DIRECTIONAL_LIGHT
#pragma multi_compile _ LIGHTMAP_ON

half4 frag(v2f i) : SV_Target
{
half4 diffuseAlpha = Tex2DLinearRGBA(_MainTex, i.uv01.xy);
half3 diffuse = diffuseAlpha.rgb * _Color.rgb;
half4 diffuseAlpha = tex2D(_MainTex, i.uv01.xy);
half3 diffuse = LIGHTWEIGHT_GAMMA_TO_LINEAR(diffuseAlpha.rgb) * _Color.rgb;
half alpha = diffuseAlpha.a * _Color.a;
// Keep for compatibility reasons. Shader Inpector throws a warning when using cutoff

#endif // SINGLE_DIRECTIONAL_LIGHT
Emission(i.uv01.xy, color);
#ifdef _EMISSION
color += LIGHTWEIGHT_GAMMA_TO_LINEAR(tex2D(_EmissionMap, i.uv01.xy).rgb) * _EmissionColor;
#else
color += _EmissionColor;
#endif
#if defined(LIGHTMAP_ON)
color += (DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv01.zw)) + i.fogCoord.yzw) * diffuse;

SpecularGloss(i.uv.xy, 1.0, specularColor);
o.SpecularColor = specularColor;
Emission(i.uv.xy, o.Emission);
#ifdef _EMISSION
o.Emission += LIGHTWEIGHT_GAMMA_TO_LINEAR(tex2D(_EmissionMap, i.uv).rgb) * _EmissionColor;
#else
o.Emission += _EmissionColor;
#endif
return UnityMetaFragment(o);
}

32
Assets/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipelineCore.cginc


#define _SHADOW_CASCADES
#endif
#if (defined(UNITY_COLORSPACE_GAMMA) || SHADER_TARGET < 30) && defined(LIGHTWEIGHT_FORCE_LINEAR)
#if defined(UNITY_COLORSPACE_GAMMA) && defined(LIGHTWEIGHT_LINEAR)
// Ideally we want an approximation of gamma curve 2.0 to save ALU on GPU but as off now it won't match the GammaToLinear conversion of props in engine
//#define LIGHTWEIGHT_GAMMA_TO_LINEAR(gammaColor) gammaColor * gammaColor
//#define LIGHTWEIGHT_LINEAR_TO_GAMMA(linColor) sqrt(color)

#include "LightweightPipelineShadows.cginc"
#endif
inline half3 Tex2DLinearRGB(sampler2D s, half2 uv)
{
return LIGHTWEIGHT_GAMMA_TO_LINEAR(tex2D(s, uv).rgb);
}
inline half4 Tex2DLinearRGBA(sampler2D s, half2 uv)
{
half4 color = tex2D(s, uv);
return half4(LIGHTWEIGHT_GAMMA_TO_LINEAR(color.rgb), color.a);
}
inline void NormalMap(v2f i, out half3 normal)
{
#if _NORMALMAP

inline void SpecularGloss(half2 uv, half alpha, out half4 specularGloss)
{
#ifdef _SPECGLOSSMAP
specularGloss = Tex2DLinearRGBA(_SpecGlossMap, uv) * _SpecColor;
specularGloss = tex2D(_SpecGlossMap, uv);
#if defined(UNITY_COLORSPACE_GAMMA) && defined(LIGHTWEIGHT_LINEAR)
specularGloss.rgb = LIGHTWEIGHT_GAMMA_TO_LINEAR(specularGloss.rgb);
#endif
specularGloss = Tex2DLinearRGBA(_SpecGlossMap, uv) * _SpecColor;
specularGloss = LIGHTWEIGHT_GAMMA_TO_LINEAR(tex2D(_SpecGlossMap, uv).rgb) * _SpecColor.rgb;
#endif
}
inline void Emission(half2 uv, inout half3 color)
{
#ifdef _EMISSION
color += Tex2DLinearRGB(_EmissionMap, uv) * _EmissionColor;
#else
color += _EmissionColor;
#endif
}

half NdotL = saturate(dot(normal, lightDir));
half3 diffuse = diffuseColor * NdotL;
#if defined(_SPECGLOSSMAP_BASE_ALPHA) || defined(_SPECGLOSSMAP) || defined(_SPECULAR_COLOR)
#if defined(_SPECGLOSSMAP_BASE_ALPHA) || defined(_SPECGLOSSMAP) || defined(_SPECULAR_COLOR)
half3 halfVec = normalize(lightDir + viewDir);
half NdotH = saturate(dot(normal, halfVec));
half3 specular = specularGloss.rgb * pow(NdotH, _Shininess * 128.0) * specularGloss.a;

9
Assets/Tests/GraphicsTests/RenderPipeline/LightweightPipeline/Assets/LDPipe_CommonAssets/Cornell Box.fbx.meta


fileIDToRecycleName:
100000: //RootNode
400000: //RootNode
2100000: Cornell Box
2100002: Cornell Box_green
2100004: Cornell Box_red
externalObjects: {}
autoMapExternalMaterials: 1
materialLocation: 0
animations:
legacyGenerateAnimations: 4
bakeSimulation: 0

animationImportWarnings:
animationRetargetingWarnings:
animationDoRetargetingWarnings: 0
importAnimatedCustomProperties: 0
animationCompression: 1
animationRotationError: 0.5
animationPositionError: 0.5

extraUserProperties: []
clipAnimations: []
isReadable: 1
meshes:

optimizeMeshForGPU: 1
keepQuads: 0
weldVertices: 1
preserveHierarchy: 0
secondaryUVAngleDistortion: 8
secondaryUVAreaDistortion: 15.000001
secondaryUVHardAngle: 88

正在加载...
取消
保存