From 3c83aa174eddd572fffec33e9129648f0784738b Mon Sep 17 00:00:00 2001 From: Unity Technologies <@unity> Date: Mon, 19 Apr 2021 00:00:00 +0000 Subject: [PATCH] com.unity.render-pipelines.core@10.5.0 ## [10.5.0] - 2021-04-19 Version Updated The version number for this package has increased due to a version update of a related graphics package. --- CHANGELOG.md | 12 +- Runtime/Common/ConstantBuffer.cs | 203 ++++++++++------ Runtime/Common/DynamicResolutionHandler.cs | 263 ++++++++++++++++----- Runtime/Documentation.cs | 2 +- Runtime/Textures/RTHandle.cs | 3 + Runtime/Textures/RTHandleSystem.cs | 8 +- Runtime/Textures/TextureXR.cs | 8 +- ShaderLibrary/ACES.hlsl | 8 + ShaderLibrary/AreaLighting.hlsl | 3 +- ShaderLibrary/BSDF.hlsl | 9 + ShaderLibrary/Color.hlsl | 8 + ShaderLibrary/Common.hlsl | 8 + ShaderLibrary/CommonLighting.hlsl | 8 + ShaderLibrary/CommonMaterial.hlsl | 9 + ShaderLibrary/EntityLighting.hlsl | 7 + ShaderLibrary/ImageBasedLighting.hlsl | 8 + ShaderLibrary/Packing.hlsl | 8 + ShaderLibrary/Refraction.hlsl | 2 +- ShaderLibrary/Sampling/Fibonacci.hlsl | 8 + ShaderLibrary/Sampling/Hammersley.hlsl | 8 + ShaderLibrary/Sampling/Sampling.hlsl | 8 + ShaderLibrary/SpaceTransforms.hlsl | 8 + ShaderLibrary/Version.hlsl | 2 +- ValidationExceptions.json | 2 +- package.json | 8 +- 25 files changed, 481 insertions(+), 140 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 690e3c0..8b24828 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,16 +4,24 @@ All notable changes to this package will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). -## [10.4.0] - 2020-01-26 +## [10.5.0] - 2021-04-19 + +Version Updated +The version number for this package has increased due to a version update of a related graphics package. + +## [10.4.0] - 2021-03-11 ### Added - Support for the XboxSeries platform has been added. +- New API in DynamicResolutionHandler to handle multicamera rendering for hardware mode. Changing cameras and resetting scaling per camera should be safe. +- New API functions with no side effects in DynamicResolutionHandler, to retrieve resolved drs scale and to apply DRS on a size. ### Fixed - Fixed parameters order on inspectors for Volume Components without custom editor - Fixed the display name of a Volume Parameter when is defined the attribute InspectorName +- Calculating correct rtHandleScale by considering the possible pixel rounding when DRS is on -## [10.3.1] - 2020-01-26 +## [10.3.1] - 2021-01-26 Version Updated The version number for this package has increased due to a version update of a related graphics package. diff --git a/Runtime/Common/ConstantBuffer.cs b/Runtime/Common/ConstantBuffer.cs index 17f40a2..5edd232 100644 --- a/Runtime/Common/ConstantBuffer.cs +++ b/Runtime/Common/ConstantBuffer.cs @@ -19,7 +19,7 @@ public class ConstantBuffer /// Shader porperty id to bind the constant buffer to. public static void PushGlobal(CommandBuffer cmd, in CBType data, int shaderId) where CBType : struct { - var cb = TypedConstantBuffer.instance; + var cb = ConstantBufferSingleton.instance; cb.UpdateData(cmd, data); cb.SetGlobal(cmd, shaderId); @@ -35,7 +35,7 @@ public class ConstantBuffer /// Shader porperty id to bind the constant buffer to. public static void Push(CommandBuffer cmd, in CBType data, ComputeShader cs, int shaderId) where CBType : struct { - var cb = TypedConstantBuffer.instance; + var cb = ConstantBufferSingleton.instance; cb.UpdateData(cmd, data); cb.Set(cmd, cs, shaderId); @@ -51,7 +51,7 @@ public class ConstantBuffer /// Shader porperty id to bind the constant buffer to. public static void Push(CommandBuffer cmd, in CBType data, Material mat, int shaderId) where CBType : struct { - var cb = TypedConstantBuffer.instance; + var cb = ConstantBufferSingleton.instance; cb.UpdateData(cmd, data); cb.Set(mat, shaderId); @@ -65,7 +65,7 @@ public class ConstantBuffer /// Input data of the constant buffer. public static void UpdateData(CommandBuffer cmd, in CBType data) where CBType : struct { - var cb = TypedConstantBuffer.instance; + var cb = ConstantBufferSingleton.instance; cb.UpdateData(cmd, data); } @@ -78,7 +78,7 @@ public class ConstantBuffer /// Shader porperty id to bind the constant buffer to. public static void SetGlobal(CommandBuffer cmd, int shaderId) where CBType : struct { - var cb = TypedConstantBuffer.instance; + var cb = ConstantBufferSingleton.instance; cb.SetGlobal(cmd, shaderId); } @@ -92,7 +92,7 @@ public class ConstantBuffer /// Shader porperty id to bind the constant buffer to. public static void Set(CommandBuffer cmd, ComputeShader cs, int shaderId) where CBType : struct { - var cb = TypedConstantBuffer.instance; + var cb = ConstantBufferSingleton.instance; cb.Set(cmd, cs, shaderId); } @@ -105,14 +105,14 @@ public class ConstantBuffer /// Shader porperty id to bind the constant buffer to. public static void Set(Material mat, int shaderId) where CBType : struct { - var cb = TypedConstantBuffer.instance; + var cb = ConstantBufferSingleton.instance; cb.Set(mat, shaderId); } /// - /// Release all currently allocated constant buffers. - /// This needs to be called before shutting down the application. + /// Release all currently allocated singleton constant buffers. + /// This needs to be called before shutting down the application. /// public static void ReleaseAll() { @@ -122,86 +122,149 @@ public static void ReleaseAll() m_RegisteredConstantBuffers.Clear(); } - internal abstract class ConstantBufferBase - { - public abstract void Release(); - } - internal static void Register(ConstantBufferBase cb) { m_RegisteredConstantBuffers.Add(cb); } - class TypedConstantBuffer : ConstantBufferBase where CBType : struct - { - // Used to track all global bindings used by this CB type. - HashSet m_GlobalBindings = new HashSet(); - // Array is required by the ComputeBuffer SetData API - CBType[] m_Data = new CBType[1]; + } - static TypedConstantBuffer s_Instance = null; - internal static TypedConstantBuffer instance - { - get - { - if (s_Instance == null) - s_Instance = new TypedConstantBuffer(); - return s_Instance; - } - set - { - s_Instance = value; - } - } - ComputeBuffer m_GPUConstantBuffer = null; + /// + /// The base class of Constant Buffer. + /// + public abstract class ConstantBufferBase + { + /// + /// Release the constant buffer. + /// + public abstract void Release(); + } - TypedConstantBuffer() - { - m_GPUConstantBuffer = new ComputeBuffer(1, UnsafeUtility.SizeOf(), ComputeBufferType.Constant); - ConstantBuffer.Register(this); - } - public void UpdateData(CommandBuffer cmd, in CBType data) - { - m_Data[0] = data; + /// + /// An instance of a constant buffer. + /// + /// The type of structure representing the constant buffer data. + public class ConstantBuffer : ConstantBufferBase where CBType : struct + { + // Used to track all global bindings used by this CB type. + HashSet m_GlobalBindings = new HashSet(); + // Array is required by the ComputeBuffer SetData API + CBType[] m_Data = new CBType[1]; + + + ComputeBuffer m_GPUConstantBuffer = null; + + /// + /// Constant Buffer constructor. + /// + public ConstantBuffer() + { + m_GPUConstantBuffer = new ComputeBuffer(1, UnsafeUtility.SizeOf(), ComputeBufferType.Constant); + } + + /// + /// Update the GPU data of the constant buffer. + /// + /// Command Buffer used to execute the graphic commands. + /// Input data of the constant buffer. + public void UpdateData(CommandBuffer cmd, in CBType data) + { + m_Data[0] = data; #if UNITY_2021_1_OR_NEWER - cmd.SetBufferData(m_GPUConstantBuffer, m_Data); + cmd.SetBufferData(m_GPUConstantBuffer, m_Data); #else cmd.SetComputeBufferData(m_GPUConstantBuffer, m_Data); #endif - } + } - public void SetGlobal(CommandBuffer cmd, int shaderId) - { - m_GlobalBindings.Add(shaderId); - cmd.SetGlobalConstantBuffer(m_GPUConstantBuffer, shaderId, 0, m_GPUConstantBuffer.stride); - } + /// + /// Bind the constant buffer globally. + /// + /// Command Buffer used to execute the graphic commands. + /// Shader porperty id to bind the constant buffer to. + public void SetGlobal(CommandBuffer cmd, int shaderId) + { + m_GlobalBindings.Add(shaderId); + cmd.SetGlobalConstantBuffer(m_GPUConstantBuffer, shaderId, 0, m_GPUConstantBuffer.stride); + } - public void Set(CommandBuffer cmd, ComputeShader cs, int shaderId) - { - cmd.SetComputeConstantBufferParam(cs, shaderId, m_GPUConstantBuffer, 0, m_GPUConstantBuffer.stride); - } + /// + /// Bind the constant buffer to a compute shader. + /// + /// Command Buffer used to execute the graphic commands. + /// Compute shader to which the constant buffer should be bound. + /// Shader porperty id to bind the constant buffer to. + public void Set(CommandBuffer cmd, ComputeShader cs, int shaderId) + { + cmd.SetComputeConstantBufferParam(cs, shaderId, m_GPUConstantBuffer, 0, m_GPUConstantBuffer.stride); + } + + /// + /// Bind the constant buffer to a material. + /// + /// Material to which the constant buffer should be bound. + /// Shader porperty id to bind the constant buffer to. + public void Set(Material mat, int shaderId) + { + // This isn't done via command buffer because as long as the buffer itself is not destroyed, + // the binding stays valid. Only the commit of data needs to go through the command buffer. + // We do it here anyway for now to simplify user API. + mat.SetConstantBuffer(shaderId, m_GPUConstantBuffer, 0, m_GPUConstantBuffer.stride); + } + + /// + /// Update the GPU data of the constant buffer and bind it globally. + /// + /// Command Buffer used to execute the graphic commands. + /// Input data of the constant buffer. + /// Shader porperty id to bind the constant buffer to. + public void PushGlobal(CommandBuffer cmd, in CBType data, int shaderId) + { + UpdateData(cmd, data); + SetGlobal(cmd, shaderId); + } + + /// + /// Release the constant buffers. + /// + public override void Release() + { + // Depending on the device, globally bound buffers can leave stale "valid" shader ids pointing to a destroyed buffer. + // In DX11 it does not cause issues but on Vulkan this will result in skipped drawcalls (even if the buffer is not actually accessed in the shader). + // To avoid this kind of issues, it's good practice to "unbind" all globally bound buffers upon destruction. + foreach (int shaderId in m_GlobalBindings) + Shader.SetGlobalConstantBuffer(shaderId, (ComputeBuffer)null, 0, 0); + m_GlobalBindings.Clear(); - public void Set(Material mat, int shaderId) + CoreUtils.SafeRelease(m_GPUConstantBuffer); + } + } + + class ConstantBufferSingleton : ConstantBuffer where CBType : struct + { + static ConstantBufferSingleton s_Instance = null; + internal static ConstantBufferSingleton instance + { + get { - // This isn't done via command buffer because as long as the buffer itself is not destroyed, - // the binding stays valid. Only the commit of data needs to go through the command buffer. - // We do it here anyway for now to simplify user API. - mat.SetConstantBuffer(shaderId, m_GPUConstantBuffer, 0, m_GPUConstantBuffer.stride); + if (s_Instance == null) + { + s_Instance = new ConstantBufferSingleton(); + ConstantBuffer.Register(s_Instance); + } + return s_Instance; } - - public override void Release() + set { - // Depending on the device, globally bound buffers can leave stale "valid" shader ids pointing to a destroyed buffer. - // In DX11 it does not cause issues but on Vulkan this will result in skipped drawcalls (even if the buffer is not actually accessed in the shader). - // To avoid this kind of issues, it's good practice to "unbind" all globally bound buffers upon destruction. - foreach (int shaderId in m_GlobalBindings) - Shader.SetGlobalConstantBuffer(shaderId, (ComputeBuffer)null, 0, 0); - m_GlobalBindings.Clear(); - - CoreUtils.SafeRelease(m_GPUConstantBuffer); - s_Instance = null; + s_Instance = value; } } + + public override void Release() + { + base.Release(); + s_Instance = null; + } } } diff --git a/Runtime/Common/DynamicResolutionHandler.cs b/Runtime/Common/DynamicResolutionHandler.cs index 964f85e..30aaeca 100644 --- a/Runtime/Common/DynamicResolutionHandler.cs +++ b/Runtime/Common/DynamicResolutionHandler.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; namespace UnityEngine.Rendering { @@ -30,20 +31,38 @@ public enum DynamicResScalePolicyType /// public class DynamicResolutionHandler { - private bool m_Enabled = false; - private float m_MinScreenFraction = 1.0f; - private float m_MaxScreenFraction = 1.0f; - private float m_CurrentFraction = 1.0f; - private float m_PrevFraction = -1.0f; - private bool m_ForcingRes = false; - private bool m_CurrentCameraRequest = true; - private bool m_ForceSoftwareFallback = false; - - private float m_PrevHWScaleWidth = 1.0f; - private float m_PrevHWScaleHeight = 1.0f; - private Vector2Int m_LastScaledSize = new Vector2Int(0, 0); + private bool m_Enabled; + private float m_MinScreenFraction; + private float m_MaxScreenFraction; + private float m_CurrentFraction; + private bool m_ForcingRes; + private bool m_CurrentCameraRequest; + private float m_PrevFraction; + private bool m_ForceSoftwareFallback; + + private float m_PrevHWScaleWidth; + private float m_PrevHWScaleHeight; + private Vector2Int m_LastScaledSize; + + private void Reset() + { + m_Enabled = false; + m_MinScreenFraction = 1.0f; + m_MaxScreenFraction = 1.0f; + m_CurrentFraction = 1.0f; + m_ForcingRes = false; + m_CurrentCameraRequest = true; + m_PrevFraction = -1.0f; + m_ForceSoftwareFallback = false; + + m_PrevHWScaleWidth = 1.0f; + m_PrevHWScaleHeight = 1.0f; + m_LastScaledSize = new Vector2Int(0, 0); + filter = DynamicResUpscaleFilter.Bilinear; + } - private DynamicResScalePolicyType m_ScalerType = DynamicResScalePolicyType.ReturnsMinMaxLerpFactor; + private static DynamicResScalePolicyType s_ScalerType = DynamicResScalePolicyType.ReturnsMinMaxLerpFactor; + private static PerformDynamicRes s_DynamicResMethod = DefaultDynamicResMethod; // Debug private Vector2Int cachedOriginalSize; @@ -59,23 +78,96 @@ public class DynamicResolutionHandler /// public Vector2Int finalViewport { get; set; } - private DynamicResolutionType type; - private PerformDynamicRes m_DynamicResMethod = null; - private static DynamicResolutionHandler s_Instance = new DynamicResolutionHandler(); + private GlobalDynamicResolutionSettings m_CachedSettings = GlobalDynamicResolutionSettings.NewDefault(); + + private const int CameraDictionaryMaxcCapacity = 32; + private WeakReference m_OwnerCameraWeakRef = null; + private static Dictionary s_CameraInstances = new Dictionary(CameraDictionaryMaxcCapacity); + private static DynamicResolutionHandler s_DefaultInstance = new DynamicResolutionHandler(); + + private static int s_ActiveCameraId = 0; + private static DynamicResolutionHandler s_ActiveInstance = s_DefaultInstance; + + //private global state of ScalableBufferManager + private static bool s_ActiveInstanceDirty = true; + private static float s_GlobalHwFraction = 1.0f; + private static bool s_GlobalHwUpresActive = false; + + private bool FlushScalableBufferManagerState() + { + if (s_GlobalHwUpresActive == HardwareDynamicResIsEnabled() && s_GlobalHwFraction == m_CurrentFraction) + return false; + + s_GlobalHwUpresActive = HardwareDynamicResIsEnabled(); + s_GlobalHwFraction = m_CurrentFraction; + ScalableBufferManager.ResizeBuffers(s_GlobalHwFraction, s_GlobalHwFraction); + return true; + } + + private static DynamicResolutionHandler GetOrCreateDrsInstanceHandler(Camera camera) + { + if (camera == null) + return null; + + DynamicResolutionHandler instance = null; + var key = camera.GetInstanceID(); + if (!s_CameraInstances.TryGetValue(key, out instance)) + { + //if this camera is not available in the map of cameras lets try creating one. + + //first and foremost, if we exceed the dictionary capacity, lets try and recycle an object that is dead. + if (s_CameraInstances.Count >= CameraDictionaryMaxcCapacity) + { + int recycledInstanceKey = 0; + DynamicResolutionHandler recycledInstance = null; + foreach (var kv in s_CameraInstances) + { + //is this object dead? that is, belongs to a camera that was destroyed? + if (kv.Value.m_OwnerCameraWeakRef == null || !kv.Value.m_OwnerCameraWeakRef.IsAlive) + { + recycledInstance = kv.Value; + recycledInstanceKey = kv.Key; + break; + } + } + + if (recycledInstance != null) + { + instance = recycledInstance; + s_CameraInstances.Remove(recycledInstanceKey); + } + } + + //if we didnt find a dead object, we create one from scratch. + if (instance == null) + { + instance = new DynamicResolutionHandler(); + instance.m_OwnerCameraWeakRef = new WeakReference(camera); + } + else + { + //otherwise, we found a dead object, lets reset it, and have a weak ref to this camera, + //so we can possibly recycle it in the future by checking the camera's weak pointer state. + instance.Reset(); + instance.m_OwnerCameraWeakRef.Target = camera; + } + + s_CameraInstances.Add(key, instance); + } + return instance; + } /// /// Get the instance of the global dynamic resolution handler. /// - public static DynamicResolutionHandler instance { get { return s_Instance; } } + public static DynamicResolutionHandler instance { get { return s_ActiveInstance; } } private DynamicResolutionHandler() { - m_DynamicResMethod = DefaultDynamicResMethod; - filter = DynamicResUpscaleFilter.Bilinear; - + Reset(); } // TODO: Eventually we will need to provide a good default implementation for this. @@ -108,6 +200,25 @@ private void ProcessSettings(GlobalDynamicResolutionSettings settings) m_CurrentFraction = fraction; } } + m_CachedSettings = settings; + } + + public Vector2 GetResolvedScale() + { + if (!m_Enabled || !m_CurrentCameraRequest) + { + return new Vector2(1.0f, 1.0f); + } + + float scaleFractionX = m_CurrentFraction; + float scaleFractionY = m_CurrentFraction; + if (!m_ForceSoftwareFallback && type == DynamicResolutionType.Hardware) + { + scaleFractionX = ScalableBufferManager.widthScaleFactor; + scaleFractionY = ScalableBufferManager.heightScaleFactor; + } + + return new Vector2(scaleFractionX, scaleFractionY); } /// @@ -117,8 +228,18 @@ private void ProcessSettings(GlobalDynamicResolutionSettings settings) /// The type of scaler that is used, this is used to indicate the return type of the scaler to the dynamic resolution system. static public void SetDynamicResScaler(PerformDynamicRes scaler, DynamicResScalePolicyType scalerType = DynamicResScalePolicyType.ReturnsMinMaxLerpFactor) { - s_Instance.m_ScalerType = scalerType; - s_Instance.m_DynamicResMethod = scaler; + s_ScalerType = scalerType; + s_DynamicResMethod = scaler; + } + + /// + /// Will clear the currently used camera. Use this function to restore the default instance when UpdateAndUseCamera is called. + /// + public static void ClearSelectedCamera() + { + s_ActiveInstance = s_DefaultInstance; + s_ActiveCameraId = 0; + s_ActiveInstanceDirty = true; } /// @@ -130,6 +251,34 @@ public void SetCurrentCameraRequest(bool cameraRequest) m_CurrentCameraRequest = cameraRequest; } + /// + /// Update the state of the dynamic resolution system for a specific camera. + /// Call this function also to switch context between cameras (will set the current camera as active). + // Passing a null camera has the same effect as calling Update without the camera parameter. + /// + /// Camera used to select a specific instance tied to this DynamicResolutionHandler instance. + /// + /// (optional) The settings that are to be used by the dynamic resolution system. passing null for the settings will result in the last update's settings used. + /// An action that will be called every time the dynamic resolution system triggers a change in resolution. + public static void UpdateAndUseCamera(Camera camera, GlobalDynamicResolutionSettings? settings = null, Action OnResolutionChange = null) + { + int newCameraId; + if (camera == null) + { + s_ActiveInstance = s_DefaultInstance; + newCameraId = 0; + } + else + { + s_ActiveInstance = GetOrCreateDrsInstanceHandler(camera); + newCameraId = camera.GetInstanceID(); + } + + s_ActiveInstanceDirty = newCameraId != s_ActiveCameraId; + s_ActiveCameraId = newCameraId; + s_ActiveInstance.Update(settings.HasValue ? settings.Value : s_ActiveInstance.m_CachedSettings, OnResolutionChange); + } + /// /// Update the state of the dynamic resolution system. /// @@ -139,49 +288,47 @@ public void Update(GlobalDynamicResolutionSettings settings, Action OnResolution { ProcessSettings(settings); - if (!m_Enabled) return; + if (!m_Enabled && !s_ActiveInstanceDirty) + { + s_ActiveInstanceDirty = false; + return; + } if (!m_ForcingRes) { - if(m_ScalerType == DynamicResScalePolicyType.ReturnsMinMaxLerpFactor) + if (s_ScalerType == DynamicResScalePolicyType.ReturnsMinMaxLerpFactor) { - float currLerp = m_DynamicResMethod(); + float currLerp = s_DynamicResMethod(); float lerpFactor = Mathf.Clamp(currLerp, 0.0f, 1.0f); m_CurrentFraction = Mathf.Lerp(m_MinScreenFraction, m_MaxScreenFraction, lerpFactor); } - else if(m_ScalerType == DynamicResScalePolicyType.ReturnsPercentage) + else if (s_ScalerType == DynamicResScalePolicyType.ReturnsPercentage) { - float percentageRequested = Mathf.Max(m_DynamicResMethod(), 5.0f); + float percentageRequested = Mathf.Max(s_DynamicResMethod(), 5.0f); m_CurrentFraction = Mathf.Clamp(percentageRequested / 100.0f, m_MinScreenFraction, m_MaxScreenFraction); } } - if (m_CurrentFraction != m_PrevFraction) - { - m_PrevFraction = m_CurrentFraction; + bool hardwareResolutionChanged = false; + bool softwareResolutionChanged = m_CurrentFraction != m_PrevFraction; - if (!m_ForceSoftwareFallback && type == DynamicResolutionType.Hardware) - { - ScalableBufferManager.ResizeBuffers(m_CurrentFraction, m_CurrentFraction); - } + m_PrevFraction = m_CurrentFraction; - if(OnResolutionChange != null) - OnResolutionChange(); - } - else + if (!m_ForceSoftwareFallback && type == DynamicResolutionType.Hardware) { - // Unity can change the scale factor by itself so we need to trigger the Action if that happens as well. - if (!m_ForceSoftwareFallback && type == DynamicResolutionType.Hardware) + hardwareResolutionChanged = FlushScalableBufferManagerState(); + if (ScalableBufferManager.widthScaleFactor != m_PrevHWScaleWidth || + ScalableBufferManager.heightScaleFactor != m_PrevHWScaleHeight) { - if(ScalableBufferManager.widthScaleFactor != m_PrevHWScaleWidth || - ScalableBufferManager.heightScaleFactor != m_PrevHWScaleHeight) - { - if (OnResolutionChange != null) - OnResolutionChange(); - } + hardwareResolutionChanged = true; } } + + if ((softwareResolutionChanged || hardwareResolutionChanged) && OnResolutionChange != null) + OnResolutionChange(); + + s_ActiveInstanceDirty = false; m_PrevHWScaleWidth = ScalableBufferManager.widthScaleFactor; m_PrevHWScaleHeight = ScalableBufferManager.heightScaleFactor; } @@ -235,6 +382,7 @@ public void ForceSoftwareFallback() /// /// Applies to the passed size the scale imposed by the dynamic resolution system. + /// Note: this function has the side effect of caching the last scale size. /// /// The starting size of the render target that will be scaled by dynamic resolution. /// The parameter size scaled by the dynamic resolution system. @@ -247,21 +395,26 @@ public Vector2Int GetScaledSize(Vector2Int size) return size; } - float scaleFractionX = m_CurrentFraction; - float scaleFractionY = m_CurrentFraction; - if (!m_ForceSoftwareFallback && type == DynamicResolutionType.Hardware) - { - scaleFractionX = ScalableBufferManager.widthScaleFactor; - scaleFractionY = ScalableBufferManager.heightScaleFactor; - } + Vector2Int scaledSize = ApplyScalesOnSize(size); + m_LastScaledSize = scaledSize; + return scaledSize; + } - Vector2Int scaledSize = new Vector2Int(Mathf.CeilToInt(size.x * scaleFractionX), Mathf.CeilToInt(size.y * scaleFractionY)); + /// + /// Applies to the passed size the scale imposed by the dynamic resolution system. + /// Note: this function is pure (has no side effects), this function does not cache the pre-scale size + /// + /// The size to apply the scaling + /// The parameter size scaled by the dynamic resolution system. + public Vector2Int ApplyScalesOnSize(Vector2Int size) + { + Vector2 resolvedScales = GetResolvedScale(); + Vector2Int scaledSize = new Vector2Int(Mathf.CeilToInt(size.x * resolvedScales.x), Mathf.CeilToInt(size.y * resolvedScales.y)); if (m_ForceSoftwareFallback || type != DynamicResolutionType.Hardware) { scaledSize.x += (1 & scaledSize.x); scaledSize.y += (1 & scaledSize.y); } - m_LastScaledSize = scaledSize; return scaledSize; } diff --git a/Runtime/Documentation.cs b/Runtime/Documentation.cs index af32674..41aca2b 100644 --- a/Runtime/Documentation.cs +++ b/Runtime/Documentation.cs @@ -15,7 +15,7 @@ public class DocumentationInfo /// /// Current version of the documentation. /// - public const string version = "10.4"; + public const string version = "10.5"; } //Need to live in Runtime as Attribute of documentation is on Runtime classes \o/ diff --git a/Runtime/Textures/RTHandle.cs b/Runtime/Textures/RTHandle.cs index e66b7b5..9b71eca 100644 --- a/Runtime/Textures/RTHandle.cs +++ b/Runtime/Textures/RTHandle.cs @@ -141,6 +141,9 @@ public void Release() /// Input size scaled by the RTHandle scale factor. public Vector2Int GetScaledSize(Vector2Int refSize) { + if (!useScaling) + return refSize; + if (scaleFunc != null) { return scaleFunc(refSize); diff --git a/Runtime/Textures/RTHandleSystem.cs b/Runtime/Textures/RTHandleSystem.cs index 7bbd913..95f0c6e 100644 --- a/Runtime/Textures/RTHandleSystem.cs +++ b/Runtime/Textures/RTHandleSystem.cs @@ -219,8 +219,12 @@ public void SetReferenceSize(int width, int height, MSAASamples msaaSamples, boo if (DynamicResolutionHandler.instance.HardwareDynamicResIsEnabled() && m_HardwareDynamicResRequested) { - float xScale = (float)DynamicResolutionHandler.instance.finalViewport.x / GetMaxWidth(); - float yScale = (float)DynamicResolutionHandler.instance.finalViewport.y / GetMaxHeight(); + Vector2Int maxSize = new Vector2Int(GetMaxWidth(), GetMaxHeight()); + // Making the final scale in 'drs' space, since the final scale must account for rounding pixel values. + var scaledFinalViewport = DynamicResolutionHandler.instance.ApplyScalesOnSize(DynamicResolutionHandler.instance.finalViewport); + var scaledMaxSize = DynamicResolutionHandler.instance.ApplyScalesOnSize(maxSize); + float xScale = (float)scaledFinalViewport.x / (float)scaledMaxSize.x; + float yScale = (float)scaledFinalViewport.y / (float)scaledMaxSize.y; m_RTHandleProperties.rtHandleScale = new Vector4(xScale, yScale, m_RTHandleProperties.rtHandleScale.x, m_RTHandleProperties.rtHandleScale.y); } else diff --git a/Runtime/Textures/TextureXR.cs b/Runtime/Textures/TextureXR.cs index 72bab51..eb84a24 100644 --- a/Runtime/Textures/TextureXR.cs +++ b/Runtime/Textures/TextureXR.cs @@ -92,6 +92,7 @@ public static TextureDimension dimension /// The default magenta texture. public static RTHandle GetMagentaTexture() { return useTexArray ? m_MagentaTexture2DArrayRTH : m_MagentaTextureRTH; } + static Texture2D m_BlackTexture; static Texture3D m_BlackTexture3D; static Texture2DArray m_BlackTexture2DArray; static RTHandle m_BlackTexture2DArrayRTH; @@ -161,9 +162,12 @@ public static void Initialize(CommandBuffer cmd, ComputeShader clearR32_UIntShad // Black RTHandles.Release(m_BlackTextureRTH); - m_BlackTextureRTH = RTHandles.Alloc(Texture2D.blackTexture); + m_BlackTexture = new Texture2D(1, 1, GraphicsFormat.R8G8B8A8_SRGB, TextureCreationFlags.None) { name = "Black Texture" }; + m_BlackTexture.SetPixel(0, 0, Color.black); + m_BlackTexture.Apply(); + m_BlackTextureRTH = RTHandles.Alloc(m_BlackTexture); RTHandles.Release(m_BlackTexture2DArrayRTH); - m_BlackTexture2DArray = CreateTexture2DArrayFromTexture2D(Texture2D.blackTexture, "Black Texture2DArray"); + m_BlackTexture2DArray = CreateTexture2DArrayFromTexture2D(m_BlackTexture, "Black Texture2DArray"); m_BlackTexture2DArrayRTH = RTHandles.Alloc(m_BlackTexture2DArray); RTHandles.Release(m_BlackTexture3DRTH); m_BlackTexture3D = CreateBlackTexture3D("Black Texture3D"); diff --git a/ShaderLibrary/ACES.hlsl b/ShaderLibrary/ACES.hlsl index 9ac26f0..51dfd6d 100644 --- a/ShaderLibrary/ACES.hlsl +++ b/ShaderLibrary/ACES.hlsl @@ -1,6 +1,10 @@ #ifndef __ACES__ #define __ACES__ +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (disable : 3205) // conversion of larger type to smaller +#endif + /** * https://github.com/ampas/aces-dev * @@ -1316,4 +1320,8 @@ half3 ODT_P3DCI_48nits(half3 oces) return outputCV; } +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (enable : 3205) // conversion of larger type to smaller +#endif + #endif // __ACES__ diff --git a/ShaderLibrary/AreaLighting.hlsl b/ShaderLibrary/AreaLighting.hlsl index 8b70327..b7adc83 100644 --- a/ShaderLibrary/AreaLighting.hlsl +++ b/ShaderLibrary/AreaLighting.hlsl @@ -21,7 +21,8 @@ real3 ComputeEdgeFactor(real3 V1, real3 V2) if (V1oV2 < 0) { // Undo range reduction. - y = PI * rsqrt(saturate(1 - V1oV2 * V1oV2)) - y; + const float epsilon = 1e-5f; + y = PI * rsqrt(max(epsilon, saturate(1 - V1oV2 * V1oV2))) - y; } return V1xV2 * y; diff --git a/ShaderLibrary/BSDF.hlsl b/ShaderLibrary/BSDF.hlsl index d470640..72b831d 100644 --- a/ShaderLibrary/BSDF.hlsl +++ b/ShaderLibrary/BSDF.hlsl @@ -1,6 +1,10 @@ #ifndef UNITY_BSDF_INCLUDED #define UNITY_BSDF_INCLUDED +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (disable : 3205) // conversion of larger type to smaller +#endif + #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl" // Note: All NDF and diffuse term have a version with and without divide by PI. @@ -637,4 +641,9 @@ real3 D_KajiyaKay(real3 T, real3 H, real specularExponent) return dirAttn * norm * PositivePow(sinTHSq, 0.5 * n); } + +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (enable : 3205) // conversion of larger type to smaller +#endif + #endif // UNITY_BSDF_INCLUDED diff --git a/ShaderLibrary/Color.hlsl b/ShaderLibrary/Color.hlsl index b09360d..1cfb5d9 100644 --- a/ShaderLibrary/Color.hlsl +++ b/ShaderLibrary/Color.hlsl @@ -1,6 +1,10 @@ #ifndef UNITY_COLOR_INCLUDED #define UNITY_COLOR_INCLUDED +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (disable : 3205) // conversion of larger type to smaller +#endif + #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/ACES.hlsl" //----------------------------------------------------------------------------- @@ -731,4 +735,8 @@ half3 DecodeRGBM(half4 rgbm) return rgbm.xyz * rgbm.w * kRGBMRange; } +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (enable : 3205) // conversion of larger type to smaller +#endif + #endif // UNITY_COLOR_INCLUDED diff --git a/ShaderLibrary/Common.hlsl b/ShaderLibrary/Common.hlsl index 2152baa..9c653ee 100644 --- a/ShaderLibrary/Common.hlsl +++ b/ShaderLibrary/Common.hlsl @@ -1,6 +1,10 @@ #ifndef UNITY_COMMON_INCLUDED #define UNITY_COMMON_INCLUDED +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (disable : 3205) // conversion of larger type to smaller +#endif + // Convention: // Unity is Y up and left handed in world space @@ -1340,4 +1344,8 @@ float SharpenAlpha(float alpha, float alphaClipTreshold) // These clamping function to max of floating point 16 bit are use to prevent INF in code in case of extreme value TEMPLATE_1_REAL(ClampToFloat16Max, value, return min(value, HALF_MAX)) +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (enable : 3205) // conversion of larger type to smaller +#endif + #endif // UNITY_COMMON_INCLUDED diff --git a/ShaderLibrary/CommonLighting.hlsl b/ShaderLibrary/CommonLighting.hlsl index 02cf0a9..8059fc2 100644 --- a/ShaderLibrary/CommonLighting.hlsl +++ b/ShaderLibrary/CommonLighting.hlsl @@ -1,6 +1,10 @@ #ifndef UNITY_COMMON_LIGHTING_INCLUDED #define UNITY_COMMON_LIGHTING_INCLUDED +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (disable : 3205) // conversion of larger type to smaller +#endif + // Ligthing convention // Light direction is oriented backward (-Z). i.e in shader code, light direction is -lightData.forward @@ -455,4 +459,8 @@ bool IsMatchingLightLayer(uint lightLayers, uint renderingLayers) return (lightLayers & renderingLayers) != 0; } +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (enable : 3205) // conversion of larger type to smaller +#endif + #endif // UNITY_COMMON_LIGHTING_INCLUDED diff --git a/ShaderLibrary/CommonMaterial.hlsl b/ShaderLibrary/CommonMaterial.hlsl index 86e0eaa..3c8729f 100644 --- a/ShaderLibrary/CommonMaterial.hlsl +++ b/ShaderLibrary/CommonMaterial.hlsl @@ -1,6 +1,10 @@ #ifndef UNITY_COMMON_MATERIAL_INCLUDED #define UNITY_COMMON_MATERIAL_INCLUDED +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (disable : 3205) // conversion of larger type to smaller +#endif + //----------------------------------------------------------------------------- // Define constants //----------------------------------------------------------------------------- @@ -329,4 +333,9 @@ real3 LerpWhiteTo(real3 b, real t) real oneMinusT = 1.0 - t; return real3(oneMinusT, oneMinusT, oneMinusT) + b * t; } + +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (enable : 3205) // conversion of larger type to smaller +#endif + #endif // UNITY_COMMON_MATERIAL_INCLUDED diff --git a/ShaderLibrary/EntityLighting.hlsl b/ShaderLibrary/EntityLighting.hlsl index 4232dca..57193b6 100644 --- a/ShaderLibrary/EntityLighting.hlsl +++ b/ShaderLibrary/EntityLighting.hlsl @@ -1,6 +1,10 @@ #ifndef UNITY_ENTITY_LIGHTING_INCLUDED #define UNITY_ENTITY_LIGHTING_INCLUDED +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (disable : 3205) // conversion of larger type to smaller +#endif + #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl" #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl" @@ -355,5 +359,8 @@ real3 SampleDirectionalLightmap(TEXTURE2D_LIGHTMAP_PARAM(lightmapTex, lightmapSa return bakeDiffuseLighting; } +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (enable : 3205) // conversion of larger type to smaller +#endif #endif // UNITY_ENTITY_LIGHTING_INCLUDED diff --git a/ShaderLibrary/ImageBasedLighting.hlsl b/ShaderLibrary/ImageBasedLighting.hlsl index 7deb639..48fe074 100644 --- a/ShaderLibrary/ImageBasedLighting.hlsl +++ b/ShaderLibrary/ImageBasedLighting.hlsl @@ -1,6 +1,10 @@ #ifndef UNITY_IMAGE_BASED_LIGHTING_INCLUDED #define UNITY_IMAGE_BASED_LIGHTING_INCLUDED +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (disable : 3205) // conversion of larger type to smaller +#endif + #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonLighting.hlsl" #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl" #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/BSDF.hlsl" @@ -738,4 +742,8 @@ float InfluenceFadeNormalWeight(float3 normal, float3 centerToPos) return saturate((-1.0f / 0.4f) * dot(normal, centerToPos) + (0.6f / 0.4f)); } +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (enable : 3205) // conversion of larger type to smaller +#endif + #endif // UNITY_IMAGE_BASED_LIGHTING_INCLUDED diff --git a/ShaderLibrary/Packing.hlsl b/ShaderLibrary/Packing.hlsl index 882aa4d..b9577ab 100644 --- a/ShaderLibrary/Packing.hlsl +++ b/ShaderLibrary/Packing.hlsl @@ -1,6 +1,10 @@ #ifndef UNITY_PACKING_INCLUDED #define UNITY_PACKING_INCLUDED +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (disable : 3205) // conversion of larger type to smaller +#endif + //----------------------------------------------------------------------------- // Normal packing //----------------------------------------------------------------------------- @@ -581,4 +585,8 @@ float2 Unpack8ToFloat2(float f) return float2(x, y); } +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (enable : 3205) // conversion of larger type to smaller +#endif + #endif // UNITY_PACKING_INCLUDED diff --git a/ShaderLibrary/Refraction.hlsl b/ShaderLibrary/Refraction.hlsl index 5ad1d16..66059e3 100644 --- a/ShaderLibrary/Refraction.hlsl +++ b/ShaderLibrary/Refraction.hlsl @@ -17,7 +17,7 @@ RefractionModelResult RefractionModelSphere(real3 V, float3 positionWS, real3 no // Sphere shape model: // We approximate locally the shape of the object as sphere, that is tangent to the shape. // The sphere has a diameter of {thickness} - // The center of the sphere is at {positionWS} - {normalWS} * {thickness} + // The center of the sphere is at {positionWS} - {normalWS} * {thickness} * 0.5 // // So the light is refracted twice: in and out of the tangent sphere diff --git a/ShaderLibrary/Sampling/Fibonacci.hlsl b/ShaderLibrary/Sampling/Fibonacci.hlsl index 80ffef3..0fa2682 100644 --- a/ShaderLibrary/Sampling/Fibonacci.hlsl +++ b/ShaderLibrary/Sampling/Fibonacci.hlsl @@ -1,6 +1,10 @@ #ifndef UNITY_FIBONACCI_INCLUDED #define UNITY_FIBONACCI_INCLUDED +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (disable : 3205) // conversion of larger type to smaller +#endif + // Computes a point using the Fibonacci sequence of length N. // Input: Fib[N - 1], Fib[N - 2], and the index 'i' of the point. // Ref: Efficient Quadrature Rules for Illumination Integrals @@ -295,4 +299,8 @@ real2 SampleSphereFibonacci(uint i, uint sampleCount) return real2(1 - 2 * f.x, TWO_PI * f.y); } +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (enable : 3205) // conversion of larger type to smaller +#endif + #endif // UNITY_FIBONACCI_INCLUDED diff --git a/ShaderLibrary/Sampling/Hammersley.hlsl b/ShaderLibrary/Sampling/Hammersley.hlsl index 21789e8..56a60a4 100644 --- a/ShaderLibrary/Sampling/Hammersley.hlsl +++ b/ShaderLibrary/Sampling/Hammersley.hlsl @@ -1,6 +1,10 @@ #ifndef UNITY_HAMMERSLEY_INCLUDED #define UNITY_HAMMERSLEY_INCLUDED +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (disable : 3205) // conversion of larger type to smaller +#endif + // Ref: http://holger.dammertz.org/stuff/notes_HammersleyOnHemisphere.html uint ReverseBits32(uint bits) { @@ -420,4 +424,8 @@ real2 Hammersley2d(uint i, uint sampleCount) } } +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (enable : 3205) // conversion of larger type to smaller +#endif + #endif // UNITY_HAMMERSLEY_INCLUDED diff --git a/ShaderLibrary/Sampling/Sampling.hlsl b/ShaderLibrary/Sampling/Sampling.hlsl index 62c8ffc..009e2b4 100644 --- a/ShaderLibrary/Sampling/Sampling.hlsl +++ b/ShaderLibrary/Sampling/Sampling.hlsl @@ -1,6 +1,10 @@ #ifndef UNITY_SAMPLING_INCLUDED #define UNITY_SAMPLING_INCLUDED +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (disable : 3205) // conversion of larger type to smaller +#endif + //----------------------------------------------------------------------------- // Sample generator //----------------------------------------------------------------------------- @@ -305,4 +309,8 @@ void SampleCone(real2 u, real cosHalfAngle, rcpPdf = TWO_PI * (1 - cosHalfAngle); } +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (enable : 3205) // conversion of larger type to smaller +#endif + #endif // UNITY_SAMPLING_INCLUDED diff --git a/ShaderLibrary/SpaceTransforms.hlsl b/ShaderLibrary/SpaceTransforms.hlsl index c148460..52903b1 100644 --- a/ShaderLibrary/SpaceTransforms.hlsl +++ b/ShaderLibrary/SpaceTransforms.hlsl @@ -1,6 +1,10 @@ #ifndef UNITY_SPACE_TRANSFORMS_INCLUDED #define UNITY_SPACE_TRANSFORMS_INCLUDED +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (disable : 3205) // conversion of larger type to smaller +#endif + // Caution: For HDRP, adding a function in this file requires adding the appropriate #define in PickingSpaceTransforms.hlsl // Return the PreTranslated ObjectToWorld Matrix (i.e matrix with _WorldSpaceCameraPos apply to it if we use camera relative rendering) @@ -236,4 +240,8 @@ real3 TransformObjectToTangent(real3 dirOS, real3x3 tangentToWorld) return TransformWorldToTangent(normalWS, tangentToWorld); } +#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3 +#pragma warning (enable : 3205) // conversion of larger type to smaller +#endif + #endif diff --git a/ShaderLibrary/Version.hlsl b/ShaderLibrary/Version.hlsl index 94f11fd..ab81535 100644 --- a/ShaderLibrary/Version.hlsl +++ b/ShaderLibrary/Version.hlsl @@ -1,5 +1,5 @@ #define SHADER_LIBRARY_VERSION_MAJOR 10 -#define SHADER_LIBRARY_VERSION_MINOR 4 +#define SHADER_LIBRARY_VERSION_MINOR 5 #define VERSION_GREATER_EQUAL(major, minor) ((SHADER_LIBRARY_VERSION_MAJOR > major) || ((SHADER_LIBRARY_VERSION_MAJOR == major) && (SHADER_LIBRARY_VERSION_MINOR >= minor))) #define VERSION_LOWER(major, minor) ((SHADER_LIBRARY_VERSION_MAJOR < major) || ((SHADER_LIBRARY_VERSION_MAJOR == major) && (SHADER_LIBRARY_VERSION_MINOR < minor))) diff --git a/ValidationExceptions.json b/ValidationExceptions.json index f64b984..1a193ff 100644 --- a/ValidationExceptions.json +++ b/ValidationExceptions.json @@ -4,7 +4,7 @@ { "ValidationTest": "API Validation", "ExceptionError": "Breaking changes require a new major version.", - "PackageVersion": "10.4.0" + "PackageVersion": "10.5.0" } ] } diff --git a/package.json b/package.json index a419e2f..e580856 100644 --- a/package.json +++ b/package.json @@ -1,19 +1,19 @@ { "name": "com.unity.render-pipelines.core", "description": "SRP Core makes it easier to create or customize a Scriptable Render Pipeline (SRP). SRP Core contains reusable code, including boilerplate code for working with platform-specific graphics APIs, utility functions for common rendering operations, and shader libraries. The code in SRP Core is use by the High Definition Render Pipeline (HDRP) and Universal Render Pipeline (URP). If you are creating a custom SRP from scratch or customizing a prebuilt SRP, using SRP Core will save you time.", - "version": "10.4.0", + "version": "10.5.0", "unity": "2020.3", - "unityRelease": "0f1", + "unityRelease": "4f1", "displayName": "Core RP Library", "dependencies": { "com.unity.ugui": "1.0.0" }, "upmCi": { - "footprint": "2ae423aef91e9b370863c9531c9372d6cb9a036c" + "footprint": "b5ac4852ce875513636c4c17ab6cb71a7a354e71" }, "repository": { "url": "https://github.com/Unity-Technologies/Graphics.git", "type": "git", - "revision": "e11ddbccf86bcd7ceaae2c0b8646420d2e53cf80" + "revision": "7ff8fd444c179fd9bb380d61f4865be6935b47dd" } }