GitHub
7 年前
当前提交
231beaa3
共有 50 个文件被更改,包括 866 次插入 和 435 次删除
-
15ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/API/D3D11.hlsl
-
11ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/API/GLCore.hlsl
-
1ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/API/GLES2.hlsl
-
1ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/API/GLES3.hlsl
-
15ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/API/Metal.hlsl
-
15ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/API/PSSL.hlsl
-
15ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/API/Vulkan.hlsl
-
15ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/API/XBoxOne.hlsl
-
4ScriptableRenderPipeline/Core/CoreRP/Utilities/CoreUtils.cs
-
64ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineAsset.cs
-
2ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineEditorResources.cs
-
2ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineResources.asset
-
2ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineResources.cs
-
102ScriptableRenderPipeline/LightweightPipeline/LWRP/Editor/LightweightAssetEditor.cs
-
6ScriptableRenderPipeline/LightweightPipeline/LWRP/Editor/ShaderGUI/LightweightStandardSimpleLightingGUI.cs
-
2ScriptableRenderPipeline/LightweightPipeline/LWRP/Editor/ShaderGraph/LightWeightPBRSubShader.cs
-
2ScriptableRenderPipeline/LightweightPipeline/LWRP/Editor/ShaderGraph/LightWeightUnlitSubShader.cs
-
3ScriptableRenderPipeline/LightweightPipeline/LWRP/Editor/ShaderGraph/lightweightPBRExtraPasses.template
-
3ScriptableRenderPipeline/LightweightPipeline/LWRP/Editor/ShaderGraph/lightweightPBRForwardPass.template
-
1ScriptableRenderPipeline/LightweightPipeline/LWRP/Editor/ShaderGraph/lightweightUnlitExtraPasses.template
-
1ScriptableRenderPipeline/LightweightPipeline/LWRP/Editor/ShaderGraph/lightweightUnlitPass.template
-
176ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipeline.cs
-
74ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightShadowPass.cs
-
4ScriptableRenderPipeline/LightweightPipeline/LWRP/SceneViewDrawMode.cs
-
15ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/Shadows.hlsl
-
1ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/Terrain/LightweightPassLitTerrain.hlsl
-
1ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightBlit.shader
-
37ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightCopyDepth.shader
-
1ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightSampling.shader
-
1ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightScreenSpaceShadows.shader
-
9ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightStandard.shader
-
3ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightStandardParticles.shader
-
2ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightStandardParticlesSimpleLighting.shader
-
3ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightStandardParticlesUnlit.shader
-
9ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightStandardSimpleLighting.shader
-
1ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightStandardUnlit.shader
-
7ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/Terrain/LightweightStandardTerrain.shader
-
5ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/Terrain/LightweightStandardTerrainAddPass.shader
-
6ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/Terrain/LightweightStandardTerrainBase.shader
-
9ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/Terrain/LightweightWavingGrass.shader
-
9ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/Terrain/LightweightWavingGrassBillboard.shader
-
13Tests/GraphicsTests/RenderPipeline/LightweightPipeline/LightweightPipelineAsset.asset
-
158ScriptableRenderPipeline/LightweightPipeline/LWRP/Editor/ShaderPreprocessor.cs
-
11ScriptableRenderPipeline/LightweightPipeline/LWRP/Editor/ShaderPreprocessor.cs.meta
-
203ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipelineCore.cs
-
11ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipelineCore.cs.meta
-
81ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/DepthCopy.hlsl
-
9ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/DepthCopy.hlsl.meta
-
13ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipelineUtils.cs.meta
-
147ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipelineUtils.cs
|
|||
//#define LOG_VARIANTS
|
|||
//#define LOG_ONLY_LWRP_VARIANTS
|
|||
|
|||
using System.Collections.Generic; |
|||
using UnityEditor.Build; |
|||
using UnityEditor.Rendering; |
|||
using UnityEngine; |
|||
using UnityEngine.Experimental.Rendering; |
|||
using UnityEngine.Experimental.Rendering.LightweightPipeline; |
|||
using UnityEngine.Rendering; |
|||
using LightweightRP = UnityEngine.Experimental.Rendering.LightweightPipeline.LightweightPipeline; |
|||
|
|||
namespace UnityEditor.Experimental.Rendering.LightweightPipeline |
|||
{ |
|||
public class ShaderPreprocessor : IPreprocessShaders |
|||
{ |
|||
#if LOG_VARIANTS
|
|||
int m_TotalVariantsInputCount; |
|||
int m_TotalVariantsOutputCount; |
|||
#endif
|
|||
|
|||
// Multiple callback may be implemented.
|
|||
// The first one executed is the one where callbackOrder is returning the smallest number.
|
|||
public int callbackOrder { get { return 0; } } |
|||
|
|||
bool StripUnusedShader(PipelineCapabilities capabilities, Shader shader) |
|||
{ |
|||
if (shader.name.Contains("Debug")) |
|||
return true; |
|||
|
|||
if (shader.name.Contains("HDRenderPipeline")) |
|||
return true; |
|||
|
|||
if (!CoreUtils.HasFlag(capabilities, PipelineCapabilities.DirectionalShadows) && |
|||
shader.name.Contains("ScreenSpaceShadows")) |
|||
return true; |
|||
|
|||
return false; |
|||
} |
|||
|
|||
bool StripUnusedPass(PipelineCapabilities capabilities, ShaderSnippetData snippetData) |
|||
{ |
|||
if (snippetData.passType == PassType.Meta) |
|||
return true; |
|||
|
|||
if (snippetData.passType == PassType.ShadowCaster) |
|||
if (!CoreUtils.HasFlag(capabilities, PipelineCapabilities.DirectionalShadows) && !CoreUtils.HasFlag(capabilities, PipelineCapabilities.LocalShadows)) |
|||
return true; |
|||
|
|||
return false; |
|||
} |
|||
|
|||
bool StripUnusedVariant(PipelineCapabilities capabilities, ShaderCompilerData compilerData) |
|||
{ |
|||
if (compilerData.shaderKeywordSet.IsEnabled(LightweightKeywords.AdditionalLights) && |
|||
!CoreUtils.HasFlag(capabilities, PipelineCapabilities.AdditionalLights)) |
|||
return true; |
|||
|
|||
if (compilerData.shaderKeywordSet.IsEnabled(LightweightKeywords.VertexLights) && |
|||
!CoreUtils.HasFlag(capabilities, PipelineCapabilities.VertexLights)) |
|||
return true; |
|||
|
|||
if (compilerData.shaderKeywordSet.IsEnabled(LightweightKeywords.DirectionalShadows) && |
|||
!CoreUtils.HasFlag(capabilities, PipelineCapabilities.DirectionalShadows)) |
|||
return true; |
|||
|
|||
if (compilerData.shaderKeywordSet.IsEnabled(LightweightKeywords.LocalShadows) && |
|||
!CoreUtils.HasFlag(capabilities, PipelineCapabilities.LocalShadows)) |
|||
return true; |
|||
|
|||
if (compilerData.shaderKeywordSet.IsEnabled(LightweightKeywords.SoftShadows) && |
|||
!CoreUtils.HasFlag(capabilities, PipelineCapabilities.SoftShadows)) |
|||
return true; |
|||
|
|||
return false; |
|||
} |
|||
|
|||
bool StripInvalidVariants(ShaderCompilerData compilerData) |
|||
{ |
|||
bool isShadowVariant = compilerData.shaderKeywordSet.IsEnabled(LightweightKeywords.DirectionalShadows) || |
|||
compilerData.shaderKeywordSet.IsEnabled(LightweightKeywords.LocalShadows); |
|||
|
|||
if (compilerData.shaderKeywordSet.IsEnabled(LightweightKeywords.SoftShadows) && !isShadowVariant) |
|||
return true; |
|||
|
|||
if (compilerData.shaderKeywordSet.IsEnabled(LightweightKeywords.VertexLights) && |
|||
!compilerData.shaderKeywordSet.IsEnabled(LightweightKeywords.AdditionalLights)) |
|||
return true; |
|||
|
|||
// Note: LWRP doesn't support Dynamic Lightmap.
|
|||
if (compilerData.shaderKeywordSet.IsEnabled(LightweightKeywords.DirectionalLightmap) && |
|||
!compilerData.shaderKeywordSet.IsEnabled(LightweightKeywords.Lightmap)) |
|||
return true; |
|||
|
|||
return false; |
|||
} |
|||
|
|||
bool StripUnused(PipelineCapabilities capabilities, Shader shader, ShaderSnippetData snippetData, ShaderCompilerData compilerData) |
|||
{ |
|||
if (StripUnusedShader(capabilities, shader)) |
|||
return true; |
|||
|
|||
if (StripUnusedPass(capabilities, snippetData)) |
|||
return true; |
|||
|
|||
if (StripUnusedVariant(capabilities, compilerData)) |
|||
return true; |
|||
|
|||
if (StripInvalidVariants(compilerData)) |
|||
return true; |
|||
|
|||
return false; |
|||
} |
|||
|
|||
#if LOG_VARIANTS
|
|||
void LogVariants(Shader shader, ShaderSnippetData snippetData, int prevVariantsCount, int currVariantsCount) |
|||
{ |
|||
#if LOG_ONLY_LWRP_VARIANTS
|
|||
if (shader.name.Contains("LightweightPipeline")) |
|||
#endif
|
|||
{ |
|||
float percentageCurrent = (float)currVariantsCount / (float)prevVariantsCount * 100f; |
|||
float percentageTotal = (float)m_TotalVariantsOutputCount / (float)m_TotalVariantsInputCount * 100f; |
|||
|
|||
string result = string.Format("STRIPPING: {0} ({1} pass) ({2}) -" + |
|||
" Remaining shader variants = {3}/{4} = {5}% - Total = {6}/{7} = {8}%", |
|||
shader.name, snippetData.passName, snippetData.shaderType.ToString(), currVariantsCount, |
|||
prevVariantsCount, percentageCurrent, m_TotalVariantsOutputCount, m_TotalVariantsInputCount, |
|||
percentageTotal); |
|||
Debug.Log(result); |
|||
} |
|||
} |
|||
|
|||
#endif
|
|||
|
|||
public void OnProcessShader(Shader shader, ShaderSnippetData snippetData, IList<ShaderCompilerData> compilerDataList) |
|||
{ |
|||
PipelineCapabilities capabilities = LightweightRP.GetPipelineCapabilities(); |
|||
int prevVariantCount = compilerDataList.Count; |
|||
|
|||
for (int i = 0; i < compilerDataList.Count; ++i) |
|||
{ |
|||
if (StripUnused(capabilities, shader, snippetData, compilerDataList[i])) |
|||
{ |
|||
compilerDataList.RemoveAt(i); |
|||
--i; |
|||
} |
|||
} |
|||
|
|||
#if LOG_VARIANTS
|
|||
m_TotalVariantsInputCount += prevVariantCount; |
|||
m_TotalVariantsOutputCount += compilerDataList.Count; |
|||
|
|||
LogVariants(shader, snippetData, prevVariantCount, compilerDataList.Count); |
|||
#endif
|
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 9f7bf504ded5bcb48835f194831914c8 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using UnityEngine.Rendering; |
|||
|
|||
namespace UnityEngine.Experimental.Rendering.LightweightPipeline |
|||
{ |
|||
[Flags] |
|||
public enum FrameRenderingConfiguration |
|||
{ |
|||
None = (0 << 0), |
|||
Stereo = (1 << 0), |
|||
Msaa = (1 << 1), |
|||
BeforeTransparentPostProcess = (1 << 2), |
|||
PostProcess = (1 << 3), |
|||
DepthPrePass = (1 << 4), |
|||
DepthCopy = (1 << 5), |
|||
DefaultViewport = (1 << 6), |
|||
IntermediateTexture = (1 << 7) |
|||
} |
|||
|
|||
[Flags] |
|||
public enum PipelineCapabilities |
|||
{ |
|||
AdditionalLights = (1 << 0), |
|||
VertexLights = (1 << 1), |
|||
DirectionalShadows = (1 << 2), |
|||
LocalShadows = (1 << 3), |
|||
SoftShadows = (1 << 4), |
|||
} |
|||
|
|||
public class CameraComparer : IComparer<Camera> |
|||
{ |
|||
public int Compare(Camera lhs, Camera rhs) |
|||
{ |
|||
return (int)(lhs.depth - rhs.depth); |
|||
} |
|||
} |
|||
|
|||
public class LightweightKeywords |
|||
{ |
|||
public static readonly string AdditionalLightsText = "_ADDITIONAL_LIGHTS"; |
|||
public static readonly string VertexLightsText = "_VERTEX_LIGHTS"; |
|||
public static readonly string MixedLightingSubtractiveText = "_MIXED_LIGHTING_SUBTRACTIVE"; |
|||
public static readonly string MainLightCookieText = "_MAIN_LIGHT_COOKIE"; |
|||
public static readonly string DirectionalShadowsText = "_SHADOWS_ENABLED"; |
|||
public static readonly string LocalShadowsText = "_LOCAL_SHADOWS_ENABLED"; |
|||
public static readonly string SoftShadowsText = "_SHADOWS_SOFT"; |
|||
public static readonly string CascadeShadowsText = "_SHADOWS_CASCADE"; |
|||
|
|||
public static readonly ShaderKeyword AdditionalLights = new ShaderKeyword(AdditionalLightsText); |
|||
public static readonly ShaderKeyword VertexLights = new ShaderKeyword(VertexLightsText); |
|||
public static readonly ShaderKeyword MixedLightingSubtractive = new ShaderKeyword(MixedLightingSubtractiveText); |
|||
public static readonly ShaderKeyword MainLightCookie = new ShaderKeyword(MainLightCookieText); |
|||
public static readonly ShaderKeyword DirectionalShadows = new ShaderKeyword(DirectionalShadowsText); |
|||
public static readonly ShaderKeyword LocalShadows = new ShaderKeyword(LocalShadowsText); |
|||
public static readonly ShaderKeyword SoftShadows = new ShaderKeyword(SoftShadowsText); |
|||
|
|||
public static readonly ShaderKeyword Lightmap = new ShaderKeyword("LIGHTMAP_ON"); |
|||
public static readonly ShaderKeyword DirectionalLightmap = new ShaderKeyword("DIRLIGHTMAP_COMBINED"); |
|||
} |
|||
|
|||
public partial class LightweightPipeline |
|||
{ |
|||
static Mesh s_FullscreenMesh = null; |
|||
public static Mesh fullscreenMesh |
|||
{ |
|||
get |
|||
{ |
|||
if (s_FullscreenMesh != null) |
|||
return s_FullscreenMesh; |
|||
|
|||
float topV = 1.0f; |
|||
float bottomV = 0.0f; |
|||
|
|||
Mesh mesh = new Mesh { name = "Fullscreen Quad" }; |
|||
mesh.SetVertices(new List<Vector3> |
|||
{ |
|||
new Vector3(-1.0f, -1.0f, 0.0f), |
|||
new Vector3(-1.0f, 1.0f, 0.0f), |
|||
new Vector3(1.0f, -1.0f, 0.0f), |
|||
new Vector3(1.0f, 1.0f, 0.0f) |
|||
}); |
|||
|
|||
mesh.SetUVs(0, new List<Vector2> |
|||
{ |
|||
new Vector2(0.0f, bottomV), |
|||
new Vector2(0.0f, topV), |
|||
new Vector2(1.0f, bottomV), |
|||
new Vector2(1.0f, topV) |
|||
}); |
|||
|
|||
mesh.SetIndices(new[] { 0, 1, 2, 2, 1, 3 }, MeshTopology.Triangles, 0, false); |
|||
mesh.UploadMeshData(true); |
|||
return mesh; |
|||
} |
|||
} |
|||
|
|||
static PipelineCapabilities s_PipelineCapabilities; |
|||
|
|||
public static PipelineCapabilities GetPipelineCapabilities() |
|||
{ |
|||
return s_PipelineCapabilities; |
|||
} |
|||
|
|||
static void SetPipelineCapabilities(LightweightPipelineAsset pipelineAsset) |
|||
{ |
|||
s_PipelineCapabilities = 0U; |
|||
|
|||
if (pipelineAsset.MaxPixelLights > 1 || pipelineAsset.SupportsVertexLight) |
|||
s_PipelineCapabilities |= PipelineCapabilities.AdditionalLights; |
|||
|
|||
if (pipelineAsset.SupportsVertexLight) |
|||
s_PipelineCapabilities |= PipelineCapabilities.VertexLights; |
|||
|
|||
if (pipelineAsset.SupportsDirectionalShadows) |
|||
s_PipelineCapabilities |= PipelineCapabilities.DirectionalShadows; |
|||
|
|||
if (pipelineAsset.SupportsLocalShadows) |
|||
s_PipelineCapabilities |= PipelineCapabilities.LocalShadows; |
|||
|
|||
bool anyShadows = pipelineAsset.SupportsDirectionalShadows || pipelineAsset.SupportsLocalShadows; |
|||
if (pipelineAsset.SupportsSoftShadows && anyShadows) |
|||
s_PipelineCapabilities |= PipelineCapabilities.SoftShadows; |
|||
} |
|||
|
|||
public static void DrawFullScreen(CommandBuffer commandBuffer, Material material, |
|||
MaterialPropertyBlock properties = null, int shaderPassId = 0) |
|||
{ |
|||
commandBuffer.DrawMesh(fullscreenMesh, Matrix4x4.identity, material, 0, shaderPassId, properties); |
|||
} |
|||
|
|||
public static void StartStereoRendering(Camera camera, ref ScriptableRenderContext context, FrameRenderingConfiguration renderingConfiguration) |
|||
{ |
|||
if (CoreUtils.HasFlag(renderingConfiguration, FrameRenderingConfiguration.Stereo)) |
|||
context.StartMultiEye(camera); |
|||
} |
|||
|
|||
public static void StopStereoRendering(Camera camera, ref ScriptableRenderContext context, FrameRenderingConfiguration renderingConfiguration) |
|||
{ |
|||
if (CoreUtils.HasFlag(renderingConfiguration, FrameRenderingConfiguration.Stereo)) |
|||
context.StopMultiEye(camera); |
|||
} |
|||
|
|||
public static void GetLightCookieMatrix(VisibleLight light, out Matrix4x4 cookieMatrix) |
|||
{ |
|||
cookieMatrix = Matrix4x4.Inverse(light.localToWorld); |
|||
|
|||
if (light.lightType == LightType.Directional) |
|||
{ |
|||
float scale = 1.0f / light.light.cookieSize; |
|||
|
|||
// apply cookie scale and offset by 0.5 to convert from [-0.5, 0.5] to texture space [0, 1]
|
|||
Vector4 row0 = cookieMatrix.GetRow(0); |
|||
Vector4 row1 = cookieMatrix.GetRow(1); |
|||
cookieMatrix.SetRow(0, new Vector4(row0.x * scale, row0.y * scale, row0.z * scale, row0.w * scale + 0.5f)); |
|||
cookieMatrix.SetRow(1, new Vector4(row1.x * scale, row1.y * scale, row1.z * scale, row1.w * scale + 0.5f)); |
|||
} |
|||
else if (light.lightType == LightType.Spot) |
|||
{ |
|||
// we want out.w = 2.0 * in.z / m_CotanHalfSpotAngle
|
|||
// c = cotHalfSpotAngle
|
|||
// 1 0 0 0
|
|||
// 0 1 0 0
|
|||
// 0 0 1 0
|
|||
// 0 0 2/c 0
|
|||
// the "2" will be used to scale .xy for the cookie as in .xy/2 + 0.5
|
|||
float scale = 1.0f / light.range; |
|||
float halfSpotAngleRad = Mathf.Deg2Rad * light.spotAngle * 0.5f; |
|||
float cs = Mathf.Cos(halfSpotAngleRad); |
|||
float ss = Mathf.Sin(halfSpotAngleRad); |
|||
float cotHalfSpotAngle = cs / ss; |
|||
|
|||
Matrix4x4 scaleMatrix = Matrix4x4.identity; |
|||
scaleMatrix.m00 = scaleMatrix.m11 = scaleMatrix.m22 = scale; |
|||
scaleMatrix.m33 = 0.0f; |
|||
scaleMatrix.m32 = scale * (2.0f / cotHalfSpotAngle); |
|||
|
|||
cookieMatrix = scaleMatrix * cookieMatrix; |
|||
} |
|||
|
|||
// Remaining light types don't support cookies
|
|||
} |
|||
|
|||
public static bool IsSupportedShadowType(LightType lightType) |
|||
{ |
|||
return lightType == LightType.Directional || lightType == LightType.Spot; |
|||
} |
|||
|
|||
public static bool IsSupportedCookieType(LightType lightType) |
|||
{ |
|||
return lightType == LightType.Directional || lightType == LightType.Spot; |
|||
} |
|||
|
|||
public static bool PlatformSupportsMSAABackBuffer() |
|||
{ |
|||
#if UNITY_ANDROID || UNITY_IPHONE || UNITY_TVOS || UNITY_SAMSUNGTV
|
|||
return true; |
|||
#else
|
|||
return false; |
|||
#endif
|
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: d973de1eba2f6934e9c31429e8e40c09 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
#ifndef LIGHTWEIGHT_DEPTH_COPY_INCLUDED |
|||
#define LIGHTWEIGHT_DEPTH_COPY_INCLUDED |
|||
|
|||
#include "LWRP/ShaderLibrary/Core.hlsl" |
|||
|
|||
struct VertexInput |
|||
{ |
|||
float4 vertex : POSITION; |
|||
float2 uv : TEXCOORD0; |
|||
UNITY_VERTEX_INPUT_INSTANCE_ID |
|||
}; |
|||
|
|||
struct VertexOutput |
|||
{ |
|||
float4 position : SV_POSITION; |
|||
float2 uv : TEXCOORD0; |
|||
UNITY_VERTEX_INPUT_INSTANCE_ID |
|||
UNITY_VERTEX_OUTPUT_STEREO |
|||
}; |
|||
|
|||
VertexOutput vert(VertexInput i) |
|||
{ |
|||
VertexOutput o; |
|||
UNITY_SETUP_INSTANCE_ID(i); |
|||
UNITY_TRANSFER_INSTANCE_ID(i, o); |
|||
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); |
|||
o.uv = i.uv; |
|||
o.position = TransformObjectToHClip(i.vertex.xyz); |
|||
return o; |
|||
} |
|||
|
|||
#if defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_STEREO_MULTIVIEW_ENABLED) |
|||
#define DEPTH_TEXTURE_MS Texture2DMSArray |
|||
#define DEPTH_TEXTURE(name) TEXTURE2D_ARRAY(name) |
|||
#define LOAD(uv, sampleIndex) LOAD_TEXTURE2D_ARRAY_MSAA(_CameraDepthTexture, uv, unity_StereoEyeIndex, sampleIndex) |
|||
#define SAMPLE(uv) SAMPLE_TEXTURE2D_ARRAY(_CameraDepthTexture, sampler_CameraDepthTexture, uv, unity_StereoEyeIndex).r |
|||
#else |
|||
#define DEPTH_TEXTURE_MS Texture2DMS |
|||
#define DEPTH_TEXTURE(name) TEXTURE2D(name) |
|||
#define LOAD(uv, sampleIndex) LOAD_TEXTURE2D_MSAA(_CameraDepthTexture, uv, sampleIndex) |
|||
#define SAMPLE(uv) SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, uv) |
|||
#endif |
|||
|
|||
#ifdef _MSAA_DEPTH |
|||
DEPTH_TEXTURE_MS<float> _CameraDepthTexture; |
|||
float _SampleCount; |
|||
float4 _CameraDepthTexture_TexelSize; |
|||
#else |
|||
DEPTH_TEXTURE(_CameraDepthTexture); |
|||
SAMPLER(sampler_CameraDepthTexture); |
|||
#endif |
|||
|
|||
float SampleDepth(float2 uv) |
|||
{ |
|||
#ifdef _MSAA_DEPTH |
|||
int2 coord = int2(uv * _CameraDepthTexture_TexelSize.zw); |
|||
int samples = (int)_SampleCount; |
|||
#if UNITY_REVERSED_Z |
|||
float outDepth = 1.0; |
|||
#define DEPTH_OP min |
|||
#else |
|||
float outDepth = 0.0; |
|||
#define DEPTH_OP max |
|||
#endif |
|||
|
|||
for (int i = 0; i < samples; ++i) |
|||
outDepth = DEPTH_OP(LOAD(uv, i), outDepth); |
|||
|
|||
return outDepth; |
|||
#else |
|||
return SAMPLE(uv); |
|||
#endif |
|||
} |
|||
|
|||
float frag(VertexOutput i) : SV_Depth |
|||
{ |
|||
UNITY_SETUP_INSTANCE_ID(i); |
|||
return SampleDepth(i.uv); |
|||
} |
|||
|
|||
#endif // LIGHTWEIGHT_DEPTH_COPY_INCLUDED |
|
|||
fileFormatVersion: 2 |
|||
guid: 971e7c84ed6bc40fc95bdbb2e008013f |
|||
ShaderImporter: |
|||
externalObjects: {} |
|||
defaultTextures: [] |
|||
nonModifiableTextures: [] |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 1556a6d19597541c8bed4b90c704fb06 |
|||
timeCreated: 1505115136 |
|||
licenseType: Free |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using UnityEngine.Rendering; |
|||
|
|||
namespace UnityEngine.Experimental.Rendering.LightweightPipeline |
|||
{ |
|||
public class CameraComparer : IComparer<Camera> |
|||
{ |
|||
public int Compare(Camera lhs, Camera rhs) |
|||
{ |
|||
return (int)(lhs.depth - rhs.depth); |
|||
} |
|||
} |
|||
|
|||
[Flags] |
|||
public enum FrameRenderingConfiguration |
|||
{ |
|||
None = 0, |
|||
Stereo = (1 << 0), |
|||
Msaa = (1 << 1), |
|||
BeforeTransparentPostProcess = (1 << 2), |
|||
PostProcess = (1 << 3), |
|||
DepthPrePass = (1 << 4), |
|||
DepthCopy = (1 << 5), |
|||
DefaultViewport = (1 << 6), |
|||
IntermediateTexture = (1 << 7) |
|||
} |
|||
|
|||
public static class LightweightUtils |
|||
{ |
|||
static Mesh s_FullscreenMesh = null; |
|||
public static Mesh fullscreenMesh |
|||
{ |
|||
get |
|||
{ |
|||
if (s_FullscreenMesh != null) |
|||
return s_FullscreenMesh; |
|||
|
|||
float topV = 1.0f; |
|||
float bottomV = 0.0f; |
|||
|
|||
Mesh mesh = new Mesh {name = "Fullscreen Quad"}; |
|||
mesh.SetVertices(new List<Vector3> |
|||
{ |
|||
new Vector3(-1.0f, -1.0f, 0.0f), |
|||
new Vector3(-1.0f, 1.0f, 0.0f), |
|||
new Vector3( 1.0f, -1.0f, 0.0f), |
|||
new Vector3( 1.0f, 1.0f, 0.0f) |
|||
}); |
|||
|
|||
mesh.SetUVs(0, new List<Vector2> |
|||
{ |
|||
new Vector2(0.0f, bottomV), |
|||
new Vector2(0.0f, topV), |
|||
new Vector2(1.0f, bottomV), |
|||
new Vector2(1.0f, topV) |
|||
}); |
|||
|
|||
mesh.SetIndices(new[] { 0, 1, 2, 2, 1, 3 }, MeshTopology.Triangles, 0, false); |
|||
mesh.UploadMeshData(true); |
|||
return mesh; |
|||
} |
|||
} |
|||
|
|||
public static void DrawFullScreen(CommandBuffer commandBuffer, Material material, |
|||
MaterialPropertyBlock properties = null, int shaderPassId = 0) |
|||
{ |
|||
commandBuffer.DrawMesh(fullscreenMesh, Matrix4x4.identity, material, 0, shaderPassId, properties); |
|||
} |
|||
|
|||
public static void StartStereoRendering(Camera camera, ref ScriptableRenderContext context, FrameRenderingConfiguration renderingConfiguration) |
|||
{ |
|||
if (HasFlag(renderingConfiguration, FrameRenderingConfiguration.Stereo)) |
|||
context.StartMultiEye(camera); |
|||
} |
|||
|
|||
public static void StopStereoRendering(Camera camera, ref ScriptableRenderContext context, FrameRenderingConfiguration renderingConfiguration) |
|||
{ |
|||
if (HasFlag(renderingConfiguration, FrameRenderingConfiguration.Stereo)) |
|||
context.StopMultiEye(camera); |
|||
} |
|||
|
|||
public static void GetLightCookieMatrix(VisibleLight light, out Matrix4x4 cookieMatrix) |
|||
{ |
|||
cookieMatrix = Matrix4x4.Inverse(light.localToWorld); |
|||
|
|||
if (light.lightType == LightType.Directional) |
|||
{ |
|||
float scale = 1.0f / light.light.cookieSize; |
|||
|
|||
// apply cookie scale and offset by 0.5 to convert from [-0.5, 0.5] to texture space [0, 1]
|
|||
Vector4 row0 = cookieMatrix.GetRow(0); |
|||
Vector4 row1 = cookieMatrix.GetRow(1); |
|||
cookieMatrix.SetRow(0, new Vector4(row0.x * scale, row0.y * scale, row0.z * scale, row0.w * scale + 0.5f)); |
|||
cookieMatrix.SetRow(1, new Vector4(row1.x * scale, row1.y * scale, row1.z * scale, row1.w * scale + 0.5f)); |
|||
} |
|||
else if (light.lightType == LightType.Spot) |
|||
{ |
|||
// we want out.w = 2.0 * in.z / m_CotanHalfSpotAngle
|
|||
// c = cotHalfSpotAngle
|
|||
// 1 0 0 0
|
|||
// 0 1 0 0
|
|||
// 0 0 1 0
|
|||
// 0 0 2/c 0
|
|||
// the "2" will be used to scale .xy for the cookie as in .xy/2 + 0.5
|
|||
float scale = 1.0f / light.range; |
|||
float halfSpotAngleRad = Mathf.Deg2Rad * light.spotAngle * 0.5f; |
|||
float cs = Mathf.Cos(halfSpotAngleRad); |
|||
float ss = Mathf.Sin(halfSpotAngleRad); |
|||
float cotHalfSpotAngle = cs / ss; |
|||
|
|||
Matrix4x4 scaleMatrix = Matrix4x4.identity; |
|||
scaleMatrix.m00 = scaleMatrix.m11 = scaleMatrix.m22 = scale; |
|||
scaleMatrix.m33 = 0.0f; |
|||
scaleMatrix.m32 = scale * (2.0f / cotHalfSpotAngle); |
|||
|
|||
cookieMatrix = scaleMatrix * cookieMatrix; |
|||
} |
|||
|
|||
// Remaining light types don't support cookies
|
|||
} |
|||
|
|||
public static bool IsSupportedShadowType(LightType lightType) |
|||
{ |
|||
return lightType == LightType.Directional || lightType == LightType.Spot; |
|||
} |
|||
|
|||
public static bool IsSupportedCookieType(LightType lightType) |
|||
{ |
|||
return lightType == LightType.Directional || lightType == LightType.Spot; |
|||
} |
|||
|
|||
public static bool PlatformSupportsMSAABackBuffer() |
|||
{ |
|||
#if UNITY_ANDROID || UNITY_IPHONE || UNITY_TVOS || UNITY_SAMSUNGTV
|
|||
return true; |
|||
#else
|
|||
return false; |
|||
#endif
|
|||
} |
|||
|
|||
public static bool HasFlag(FrameRenderingConfiguration mask, FrameRenderingConfiguration flag) |
|||
{ |
|||
return (mask & flag) != 0; |
|||
} |
|||
} |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue