123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using UnityEngine.Scripting.APIUpdating;
- #if POST_PROCESSING_STACK_2_0_0_OR_NEWER
- using UnityEngine.Rendering.PostProcessing;
- #endif
- namespace UnityEngine.Rendering.Universal
- {
-
-
-
- [MovedFrom("UnityEngine.Rendering.LWRP")] public static class RenderingUtils
- {
- static List<ShaderTagId> m_LegacyShaderPassNames = new List<ShaderTagId>()
- {
- new ShaderTagId("Always"),
- new ShaderTagId("ForwardBase"),
- new ShaderTagId("PrepassBase"),
- new ShaderTagId("Vertex"),
- new ShaderTagId("VertexLMRGBM"),
- new ShaderTagId("VertexLM"),
- };
- 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;
- s_FullscreenMesh = new Mesh { name = "Fullscreen Quad" };
- s_FullscreenMesh.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)
- });
- s_FullscreenMesh.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)
- });
- s_FullscreenMesh.SetIndices(new[] { 0, 1, 2, 2, 1, 3 }, MeshTopology.Triangles, 0, false);
- s_FullscreenMesh.UploadMeshData(true);
- return s_FullscreenMesh;
- }
- }
- #if POST_PROCESSING_STACK_2_0_0_OR_NEWER
- static readonly int m_PostProcessingTemporaryTargetId = Shader.PropertyToID("_TemporaryColorTexture");
- static PostProcessRenderContext m_PostProcessRenderContext;
- [Obsolete("The use of the Post-processing Stack V2 is deprecated in the Universal Render Pipeline. Use the builtin post-processing effects instead.")]
- public static PostProcessRenderContext postProcessRenderContext
- {
- get => m_PostProcessRenderContext ?? (m_PostProcessRenderContext = new PostProcessRenderContext());
- }
- #endif
- internal static bool useStructuredBuffer
- {
-
-
- get
- {
-
-
- return false;
-
-
-
-
-
- }
-
- }
- static Material s_ErrorMaterial;
- static Material errorMaterial
- {
- get
- {
- if (s_ErrorMaterial == null)
- {
-
-
-
- try
- {
- s_ErrorMaterial = new Material(Shader.Find("Hidden/Universal Render Pipeline/FallbackError"));
- }
- catch{ }
- }
- return s_ErrorMaterial;
- }
- }
- #if POST_PROCESSING_STACK_2_0_0_OR_NEWER
- #pragma warning disable 0618 // Obsolete
- internal static void RenderPostProcessingCompat(CommandBuffer cmd, ref CameraData cameraData, RenderTextureDescriptor sourceDescriptor,
- RenderTargetIdentifier source, RenderTargetIdentifier destination, bool opaqueOnly, bool flip)
- {
- var layer = cameraData.postProcessLayer;
- int effectsCount;
- if (opaqueOnly)
- {
- effectsCount = layer.sortedBundles[PostProcessEvent.BeforeTransparent].Count;
- }
- else
- {
- effectsCount = layer.sortedBundles[PostProcessEvent.BeforeStack].Count +
- layer.sortedBundles[PostProcessEvent.AfterStack].Count;
- }
- var camera = cameraData.camera;
- var postProcessRenderContext = RenderingUtils.postProcessRenderContext;
- postProcessRenderContext.Reset();
- postProcessRenderContext.camera = camera;
- postProcessRenderContext.source = source;
- postProcessRenderContext.sourceFormat = sourceDescriptor.colorFormat;
- postProcessRenderContext.destination = destination;
- postProcessRenderContext.command = cmd;
- postProcessRenderContext.flip = flip;
-
-
-
- if (effectsCount == 1 && source == destination)
- {
- var rtId = new RenderTargetIdentifier(m_PostProcessingTemporaryTargetId);
- var descriptor = sourceDescriptor;
- descriptor.msaaSamples = 1;
- descriptor.depthBufferBits = 0;
- postProcessRenderContext.destination = rtId;
- cmd.GetTemporaryRT(m_PostProcessingTemporaryTargetId, descriptor, FilterMode.Point);
- if (opaqueOnly)
- cameraData.postProcessLayer.RenderOpaqueOnly(postProcessRenderContext);
- else
- cameraData.postProcessLayer.Render(postProcessRenderContext);
- cmd.Blit(rtId, destination);
- cmd.ReleaseTemporaryRT(m_PostProcessingTemporaryTargetId);
- }
- else if (opaqueOnly)
- {
- cameraData.postProcessLayer.RenderOpaqueOnly(postProcessRenderContext);
- }
- else
- {
- cameraData.postProcessLayer.Render(postProcessRenderContext);
- }
- }
- #pragma warning restore 0618
- #endif
-
-
- [Conditional("DEVELOPMENT_BUILD"), Conditional("UNITY_EDITOR")]
- internal static void RenderObjectsWithError(ScriptableRenderContext context, ref CullingResults cullResults, Camera camera, FilteringSettings filterSettings, SortingCriteria sortFlags)
- {
-
-
-
- if (errorMaterial == null)
- return;
- SortingSettings sortingSettings = new SortingSettings(camera) { criteria = sortFlags };
- DrawingSettings errorSettings = new DrawingSettings(m_LegacyShaderPassNames[0], sortingSettings)
- {
- perObjectData = PerObjectData.None,
- overrideMaterial = errorMaterial,
- overrideMaterialPassIndex = 0
- };
- for (int i = 1; i < m_LegacyShaderPassNames.Count; ++i)
- errorSettings.SetShaderPassName(i, m_LegacyShaderPassNames[i]);
- context.DrawRenderers(cullResults, ref errorSettings, ref filterSettings);
- }
-
- static Dictionary<RenderTextureFormat, bool> m_RenderTextureFormatSupport = new Dictionary<RenderTextureFormat, bool>();
- internal static void ClearSystemInfoCache()
- {
- m_RenderTextureFormatSupport.Clear();
- }
-
-
-
-
-
-
- public static bool SupportsRenderTextureFormat(RenderTextureFormat format)
- {
- if (!m_RenderTextureFormatSupport.TryGetValue(format, out var support))
- {
- support = SystemInfo.SupportsRenderTextureFormat(format);
- m_RenderTextureFormatSupport.Add(format, support);
- }
- return support;
- }
-
-
-
-
-
- internal static int GetLastValidColorBufferIndex(RenderTargetIdentifier[] colorBuffers)
- {
- int i = colorBuffers.Length - 1;
- for(; i>=0; --i)
- {
- if (colorBuffers[i] != 0)
- break;
- }
- return i;
- }
-
-
-
-
-
- internal static uint GetValidColorBufferCount(RenderTargetIdentifier[] colorBuffers)
- {
- uint nonNullColorBuffers = 0;
- if (colorBuffers != null)
- {
- foreach (var identifier in colorBuffers)
- {
- if (identifier != 0)
- ++nonNullColorBuffers;
- }
- }
- return nonNullColorBuffers;
- }
-
-
-
-
-
- internal static bool IsMRT(RenderTargetIdentifier[] colorBuffers)
- {
- return GetValidColorBufferCount(colorBuffers) > 1;
- }
-
-
-
-
-
-
- internal static bool Contains(RenderTargetIdentifier[] source, RenderTargetIdentifier value)
- {
- foreach (var identifier in source)
- {
- if (identifier == value)
- return true;
- }
- return false;
- }
-
-
-
-
-
-
- internal static int IndexOf(RenderTargetIdentifier[] source, RenderTargetIdentifier value)
- {
- for (int i = 0; i < source.Length; ++i)
- {
- if (source[i] == value)
- return i;
- }
- return -1;
- }
-
-
-
-
-
-
- internal static uint CountDistinct(RenderTargetIdentifier[] source, RenderTargetIdentifier value)
- {
- uint count = 0;
- for (int i = 0; i < source.Length; ++i)
- {
- if (source[i] != value && source[i] != 0)
- ++count;
- }
- return count;
- }
-
-
-
-
-
- internal static int LastValid(RenderTargetIdentifier[] source)
- {
- for (int i = source.Length-1; i >= 0; --i)
- {
- if (source[i] != 0)
- return i;
- }
- return -1;
- }
-
-
-
-
-
-
- internal static bool Contains(ClearFlag a, ClearFlag b)
- {
- return (a & b) == b;
- }
-
-
-
-
-
-
- internal static bool SequenceEqual(RenderTargetIdentifier[] left, RenderTargetIdentifier[] right)
- {
- if (left.Length != right.Length)
- return false;
- for (int i = 0; i < left.Length; ++i)
- if (left[i] != right[i])
- return false;
- return true;
- }
- }
- }
|