|
| 1 | +using System; |
| 2 | +using UnityEngine.Assertions; |
| 3 | + |
| 4 | +namespace UnityEngine.Rendering.PostProcessing |
| 5 | +{ |
| 6 | + [Serializable] |
| 7 | + public sealed class ScreenSpaceReflections |
| 8 | + { |
| 9 | + public enum Preset |
| 10 | + { |
| 11 | + Lower, |
| 12 | + Low, |
| 13 | + Medium, |
| 14 | + High, |
| 15 | + Higher, |
| 16 | + Ultra, |
| 17 | + Overkill, |
| 18 | + Custom |
| 19 | + } |
| 20 | + |
| 21 | + public enum Resolution |
| 22 | + { |
| 23 | + Downsampled, |
| 24 | + FullSize, |
| 25 | + Supersampled |
| 26 | + } |
| 27 | + |
| 28 | + [Tooltip("Enables screen-space reflections.")] |
| 29 | + public bool enabled; |
| 30 | + |
| 31 | + [Tooltip("Choose a quality preset, or use \"Custom\" to fine tune it. Don't use a preset higher than \"Medium\" if you care about performances on consoles.")] |
| 32 | + public Preset preset = Preset.Medium; |
| 33 | + |
| 34 | + [Range(0, 256), Tooltip("Maximum iteration count.")] |
| 35 | + public int maximumIterationCount; |
| 36 | + |
| 37 | + [Tooltip("Changes the size of the SSR buffer. Downsample it to maximize performances or supersample it to get slow but higher quality results.")] |
| 38 | + public Resolution resolution = Resolution.Downsampled; |
| 39 | + |
| 40 | + [Range(1f, 64f), Tooltip("Ray thickness. Lower values are more expensive but allow the effect to detect smaller details.")] |
| 41 | + public float thickness = 8f; |
| 42 | + |
| 43 | + [Tooltip("Maximum distance to traverse after which it will stop drawing reflections.")] |
| 44 | + public float maximumMarchDistance = 100f; |
| 45 | + |
| 46 | + [Range(0f, 1f), Tooltip("Fades reflections close to the near planes.")] |
| 47 | + public float distanceFade = 0.5f; |
| 48 | + |
| 49 | + [Range(0f, 1f), Tooltip("Fades reflections close to the screen borders.")] |
| 50 | + public float attenuation = 0.25f; |
| 51 | + |
| 52 | + class QualityPreset |
| 53 | + { |
| 54 | + public int maximumIterationCount; |
| 55 | + public float thickness; |
| 56 | + public Resolution downsampling; |
| 57 | + } |
| 58 | + |
| 59 | + QualityPreset[] m_Presets = |
| 60 | + { |
| 61 | + new QualityPreset { maximumIterationCount = 10, thickness = 32, downsampling = Resolution.Downsampled }, // Lower |
| 62 | + new QualityPreset { maximumIterationCount = 16, thickness = 32, downsampling = Resolution.Downsampled }, // Low |
| 63 | + new QualityPreset { maximumIterationCount = 32, thickness = 16, downsampling = Resolution.Downsampled }, // Medium |
| 64 | + new QualityPreset { maximumIterationCount = 48, thickness = 8, downsampling = Resolution.Downsampled }, // High |
| 65 | + new QualityPreset { maximumIterationCount = 16, thickness = 32, downsampling = Resolution.FullSize }, // Higher |
| 66 | + new QualityPreset { maximumIterationCount = 48, thickness = 16, downsampling = Resolution.FullSize }, // Ultra |
| 67 | + new QualityPreset { maximumIterationCount = 128, thickness = 12, downsampling = Resolution.Supersampled }, // Overkill |
| 68 | + }; |
| 69 | + |
| 70 | + RenderTexture m_Test; |
| 71 | + RenderTexture m_Resolve; |
| 72 | + RenderTexture m_History; |
| 73 | + int[] m_MipIDs; |
| 74 | + |
| 75 | + bool m_ResetHistory = true; |
| 76 | + |
| 77 | + enum Pass |
| 78 | + { |
| 79 | + Test, |
| 80 | + Resolve, |
| 81 | + Reproject, |
| 82 | + Composite |
| 83 | + } |
| 84 | + |
| 85 | + internal bool IsEnabledAndSupported(PostProcessRenderContext context) |
| 86 | + { |
| 87 | + return enabled |
| 88 | + && context.camera.actualRenderingPath == RenderingPath.DeferredShading |
| 89 | + && SystemInfo.supportsMotionVectors |
| 90 | + && SystemInfo.supportsComputeShaders |
| 91 | + && SystemInfo.copyTextureSupport > CopyTextureSupport.None; |
| 92 | + } |
| 93 | + |
| 94 | + internal DepthTextureMode GetCameraFlags() |
| 95 | + { |
| 96 | + return DepthTextureMode.Depth | DepthTextureMode.MotionVectors; |
| 97 | + } |
| 98 | + |
| 99 | + internal void CheckRT(ref RenderTexture rt, int width, int height, RenderTextureFormat format, FilterMode filterMode, bool useMipMap) |
| 100 | + { |
| 101 | + if (rt == null || !rt.IsCreated() || rt.width != width || rt.height != height) |
| 102 | + { |
| 103 | + if (rt != null) |
| 104 | + rt.Release(); |
| 105 | + |
| 106 | + rt = new RenderTexture(width, height, 0, format) |
| 107 | + { |
| 108 | + filterMode = filterMode, |
| 109 | + useMipMap = useMipMap, |
| 110 | + autoGenerateMips = false, |
| 111 | + hideFlags = HideFlags.HideAndDontSave |
| 112 | + }; |
| 113 | + |
| 114 | + rt.Create(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + internal void Render(PostProcessRenderContext context) |
| 119 | + { |
| 120 | + var cmd = context.command; |
| 121 | + cmd.BeginSample("Screen-space Reflections"); |
| 122 | + |
| 123 | + // Get quality settings |
| 124 | + if (preset != Preset.Custom) |
| 125 | + { |
| 126 | + int id = (int)preset; |
| 127 | + maximumIterationCount = m_Presets[id].maximumIterationCount; |
| 128 | + thickness = m_Presets[id].thickness; |
| 129 | + resolution = m_Presets[id].downsampling; |
| 130 | + } |
| 131 | + |
| 132 | + maximumMarchDistance = Mathf.Max(0f, maximumMarchDistance); |
| 133 | + |
| 134 | + // Square POT target |
| 135 | + int size = Mathf.ClosestPowerOfTwo(Mathf.Min(context.width, context.height)); |
| 136 | + |
| 137 | + if (resolution == Resolution.Downsampled) |
| 138 | + size >>= 1; |
| 139 | + else if (resolution == Resolution.Supersampled) |
| 140 | + size <<= 1; |
| 141 | + |
| 142 | + // The gaussian pyramid compute works in blocks of 8x8 so make sure the last lod has a |
| 143 | + // minimum size of 8x8 |
| 144 | + const int kMaxLods = 12; |
| 145 | + int lodCount = Mathf.FloorToInt(Mathf.Log(size, 2f) - 3f); |
| 146 | + lodCount = Mathf.Min(lodCount, kMaxLods); |
| 147 | + |
| 148 | + CheckRT(ref m_Resolve, size, size, context.sourceFormat, FilterMode.Trilinear, true); |
| 149 | + CheckRT(ref m_History, size, size, context.sourceFormat, FilterMode.Bilinear, false); |
| 150 | + |
| 151 | + if (m_ResetHistory) |
| 152 | + { |
| 153 | + context.command.BlitFullscreenTriangle(context.source, m_History); |
| 154 | + m_ResetHistory = false; |
| 155 | + } |
| 156 | + |
| 157 | + var noiseTex = context.resources.blueNoise256[0]; |
| 158 | + var sheet = context.propertySheets.Get(context.resources.shaders.screenSpaceReflections); |
| 159 | + sheet.properties.SetTexture(ShaderIDs.Noise, noiseTex); |
| 160 | + |
| 161 | + var screenSpaceProjectionMatrix = new Matrix4x4(); |
| 162 | + screenSpaceProjectionMatrix.SetRow(0, new Vector4(size * 0.5f, 0f, 0f, size * 0.5f)); |
| 163 | + screenSpaceProjectionMatrix.SetRow(1, new Vector4(0f, size * 0.5f, 0f, size * 0.5f)); |
| 164 | + screenSpaceProjectionMatrix.SetRow(2, new Vector4(0f, 0f, 1f, 0f)); |
| 165 | + screenSpaceProjectionMatrix.SetRow(3, new Vector4(0f, 0f, 0f, 1f)); |
| 166 | + |
| 167 | + var projectionMatrix = GL.GetGPUProjectionMatrix(context.camera.projectionMatrix, false); |
| 168 | + screenSpaceProjectionMatrix *= projectionMatrix; |
| 169 | + |
| 170 | + sheet.properties.SetMatrix(ShaderIDs.ViewMatrix, context.camera.worldToCameraMatrix); |
| 171 | + sheet.properties.SetMatrix(ShaderIDs.InverseViewMatrix, context.camera.worldToCameraMatrix.inverse); |
| 172 | + sheet.properties.SetMatrix(ShaderIDs.InverseProjectionMatrix, projectionMatrix.inverse); |
| 173 | + sheet.properties.SetMatrix(ShaderIDs.ScreenSpaceProjectionMatrix, screenSpaceProjectionMatrix); |
| 174 | + sheet.properties.SetVector(ShaderIDs.Params, new Vector4(attenuation, distanceFade, maximumMarchDistance, lodCount)); |
| 175 | + sheet.properties.SetVector(ShaderIDs.Params2, new Vector4((float)context.width / (float)context.height, (float)size / (float)noiseTex.width, thickness, maximumIterationCount)); |
| 176 | + |
| 177 | + cmd.GetTemporaryRT(ShaderIDs.Test, size, size, 0, FilterMode.Point, context.sourceFormat); |
| 178 | + cmd.BlitFullscreenTriangle(context.source, ShaderIDs.Test, sheet, (int)Pass.Test); |
| 179 | + |
| 180 | + cmd.GetTemporaryRT(ShaderIDs.SSRResolveTemp, size, size, 0, FilterMode.Bilinear, context.sourceFormat); |
| 181 | + cmd.BlitFullscreenTriangle(context.source, ShaderIDs.SSRResolveTemp, sheet, (int)Pass.Resolve); |
| 182 | + |
| 183 | + sheet.properties.SetTexture(ShaderIDs.History, m_History); |
| 184 | + cmd.BlitFullscreenTriangle(ShaderIDs.SSRResolveTemp, m_Resolve, sheet, (int)Pass.Reproject); |
| 185 | + |
| 186 | + cmd.ReleaseTemporaryRT(ShaderIDs.Test); |
| 187 | + cmd.ReleaseTemporaryRT(ShaderIDs.SSRResolveTemp); |
| 188 | + |
| 189 | + cmd.CopyTexture(m_Resolve, 0, 0, m_History, 0, 0); |
| 190 | + |
| 191 | + // Pre-cache mipmaps ids |
| 192 | + if (m_MipIDs == null || m_MipIDs.Length == 0) |
| 193 | + { |
| 194 | + m_MipIDs = new int[kMaxLods]; |
| 195 | + |
| 196 | + for (int i = 0; i < kMaxLods; i++) |
| 197 | + m_MipIDs[i] = Shader.PropertyToID("_SSRGaussianMip" + i); |
| 198 | + } |
| 199 | + |
| 200 | + var compute = context.resources.computeShaders.gaussianDownsample; |
| 201 | + int kernel = compute.FindKernel("KMain"); |
| 202 | + |
| 203 | + var last = new RenderTargetIdentifier(m_Resolve); |
| 204 | + |
| 205 | + for (int i = 0; i < lodCount; i++) |
| 206 | + { |
| 207 | + size >>= 1; |
| 208 | + Assert.IsTrue(size > 0); |
| 209 | + |
| 210 | + cmd.GetTemporaryRT(m_MipIDs[i], size, size, 0, FilterMode.Bilinear, context.sourceFormat, RenderTextureReadWrite.Default, 1, true); |
| 211 | + cmd.SetComputeTextureParam(compute, kernel, "_Source", last); |
| 212 | + cmd.SetComputeTextureParam(compute, kernel, "_Result", m_MipIDs[i]); |
| 213 | + cmd.SetComputeVectorParam(compute, "_Size", new Vector4(size, size, 1f / size, 1f / size)); |
| 214 | + cmd.DispatchCompute(compute, kernel, size / 8, size / 8, 1); |
| 215 | + cmd.CopyTexture(m_MipIDs[i], 0, 0, m_Resolve, 0, i + 1); |
| 216 | + |
| 217 | + last = m_MipIDs[i]; |
| 218 | + } |
| 219 | + |
| 220 | + for (int i = 0; i < lodCount; i++) |
| 221 | + cmd.ReleaseTemporaryRT(m_MipIDs[i]); |
| 222 | + |
| 223 | + sheet.properties.SetTexture(ShaderIDs.Resolve, m_Resolve); |
| 224 | + cmd.BlitFullscreenTriangle(context.source, context.destination, sheet, (int)Pass.Composite); |
| 225 | + cmd.EndSample("Screen-space Reflections"); |
| 226 | + } |
| 227 | + |
| 228 | + internal void Release() |
| 229 | + { |
| 230 | + RuntimeUtilities.Destroy(m_Test); |
| 231 | + RuntimeUtilities.Destroy(m_Resolve); |
| 232 | + RuntimeUtilities.Destroy(m_History); |
| 233 | + m_Test = null; |
| 234 | + m_Resolve = null; |
| 235 | + m_History = null; |
| 236 | + } |
| 237 | + |
| 238 | + internal void ResetHistory() |
| 239 | + { |
| 240 | + m_ResetHistory = true; |
| 241 | + } |
| 242 | + } |
| 243 | +} |
0 commit comments