MultiScaleVO.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. using System;
  2. namespace UnityEngine.Rendering.PostProcessing
  3. {
  4. // Multi-scale volumetric obscurance
  5. // TODO: Fix VR support
  6. #if UNITY_2017_1_OR_NEWER
  7. [Serializable]
  8. internal sealed class MultiScaleVO : IAmbientOcclusionMethod
  9. {
  10. internal enum MipLevel { Original, L1, L2, L3, L4, L5, L6 }
  11. enum Pass
  12. {
  13. DepthCopy,
  14. CompositionDeferred,
  15. CompositionForward,
  16. DebugOverlay
  17. }
  18. // The arrays below are reused between frames to reduce GC allocation.
  19. readonly float[] m_SampleThickness =
  20. {
  21. Mathf.Sqrt(1f - 0.2f * 0.2f),
  22. Mathf.Sqrt(1f - 0.4f * 0.4f),
  23. Mathf.Sqrt(1f - 0.6f * 0.6f),
  24. Mathf.Sqrt(1f - 0.8f * 0.8f),
  25. Mathf.Sqrt(1f - 0.2f * 0.2f - 0.2f * 0.2f),
  26. Mathf.Sqrt(1f - 0.2f * 0.2f - 0.4f * 0.4f),
  27. Mathf.Sqrt(1f - 0.2f * 0.2f - 0.6f * 0.6f),
  28. Mathf.Sqrt(1f - 0.2f * 0.2f - 0.8f * 0.8f),
  29. Mathf.Sqrt(1f - 0.4f * 0.4f - 0.4f * 0.4f),
  30. Mathf.Sqrt(1f - 0.4f * 0.4f - 0.6f * 0.6f),
  31. Mathf.Sqrt(1f - 0.4f * 0.4f - 0.8f * 0.8f),
  32. Mathf.Sqrt(1f - 0.6f * 0.6f - 0.6f * 0.6f)
  33. };
  34. readonly float[] m_InvThicknessTable = new float[12];
  35. readonly float[] m_SampleWeightTable = new float[12];
  36. readonly int[] m_Widths = new int[7];
  37. readonly int[] m_Heights = new int[7];
  38. AmbientOcclusion m_Settings;
  39. PropertySheet m_PropertySheet;
  40. PostProcessResources m_Resources;
  41. // Can't use a temporary because we need to share it between cmdbuffers - also fixes a weird
  42. // command buffer warning
  43. RenderTexture m_AmbientOnlyAO;
  44. readonly RenderTargetIdentifier[] m_MRT =
  45. {
  46. BuiltinRenderTextureType.GBuffer0, // Albedo, Occ
  47. BuiltinRenderTextureType.CameraTarget // Ambient
  48. };
  49. public MultiScaleVO(AmbientOcclusion settings)
  50. {
  51. m_Settings = settings;
  52. }
  53. public DepthTextureMode GetCameraFlags()
  54. {
  55. return DepthTextureMode.Depth;
  56. }
  57. // Special case for AO [because SRPs], please don't do this in other effects, it's bad
  58. // practice in this framework
  59. public void SetResources(PostProcessResources resources)
  60. {
  61. m_Resources = resources;
  62. }
  63. void Alloc(CommandBuffer cmd, int id, MipLevel size, RenderTextureFormat format, bool uav)
  64. {
  65. int sizeId = (int)size;
  66. cmd.GetTemporaryRT(id, new RenderTextureDescriptor
  67. {
  68. width = m_Widths[sizeId],
  69. height = m_Heights[sizeId],
  70. colorFormat = format,
  71. depthBufferBits = 0,
  72. volumeDepth = 1,
  73. autoGenerateMips = false,
  74. msaaSamples = 1,
  75. enableRandomWrite = uav,
  76. dimension = TextureDimension.Tex2D,
  77. sRGB = false
  78. }, FilterMode.Point);
  79. }
  80. void AllocArray(CommandBuffer cmd, int id, MipLevel size, RenderTextureFormat format, bool uav)
  81. {
  82. int sizeId = (int)size;
  83. cmd.GetTemporaryRT(id, new RenderTextureDescriptor
  84. {
  85. width = m_Widths[sizeId],
  86. height = m_Heights[sizeId],
  87. colorFormat = format,
  88. depthBufferBits = 0,
  89. volumeDepth = 16,
  90. autoGenerateMips = false,
  91. msaaSamples = 1,
  92. enableRandomWrite = uav,
  93. dimension = TextureDimension.Tex2DArray,
  94. sRGB = false
  95. }, FilterMode.Point);
  96. }
  97. void Release(CommandBuffer cmd, int id)
  98. {
  99. cmd.ReleaseTemporaryRT(id);
  100. }
  101. // Calculate values in _ZBuferParams (built-in shader variable)
  102. // We can't use _ZBufferParams in compute shaders, so this function is
  103. // used to give the values in it to compute shaders.
  104. Vector4 CalculateZBufferParams(Camera camera)
  105. {
  106. float fpn = camera.farClipPlane / camera.nearClipPlane;
  107. if (SystemInfo.usesReversedZBuffer)
  108. return new Vector4(fpn - 1f, 1f, 0f, 0f);
  109. return new Vector4(1f - fpn, fpn, 0f, 0f);
  110. }
  111. float CalculateTanHalfFovHeight(Camera camera)
  112. {
  113. return 1f / camera.projectionMatrix[0, 0];
  114. }
  115. Vector2 GetSize(MipLevel mip)
  116. {
  117. return new Vector2(m_Widths[(int)mip], m_Heights[(int)mip]);
  118. }
  119. Vector3 GetSizeArray(MipLevel mip)
  120. {
  121. return new Vector3(m_Widths[(int)mip], m_Heights[(int)mip], 16);
  122. }
  123. public void GenerateAOMap(CommandBuffer cmd, Camera camera, RenderTargetIdentifier destination, RenderTargetIdentifier? depthMap, bool invert, bool isMSAA)
  124. {
  125. // Base size
  126. m_Widths[0] = camera.pixelWidth * (RuntimeUtilities.isSinglePassStereoEnabled ? 2 : 1);
  127. m_Heights[0] = camera.pixelHeight;
  128. // L1 -> L6 sizes
  129. for (int i = 1; i < 7; i++)
  130. {
  131. int div = 1 << i;
  132. m_Widths[i] = (m_Widths[0] + (div - 1)) / div;
  133. m_Heights[i] = (m_Heights[0] + (div - 1)) / div;
  134. }
  135. // Allocate temporary textures
  136. PushAllocCommands(cmd, isMSAA);
  137. // Render logic
  138. PushDownsampleCommands(cmd, camera, depthMap, isMSAA);
  139. float tanHalfFovH = CalculateTanHalfFovHeight(camera);
  140. PushRenderCommands(cmd, ShaderIDs.TiledDepth1, ShaderIDs.Occlusion1, GetSizeArray(MipLevel.L3), tanHalfFovH, isMSAA);
  141. PushRenderCommands(cmd, ShaderIDs.TiledDepth2, ShaderIDs.Occlusion2, GetSizeArray(MipLevel.L4), tanHalfFovH, isMSAA);
  142. PushRenderCommands(cmd, ShaderIDs.TiledDepth3, ShaderIDs.Occlusion3, GetSizeArray(MipLevel.L5), tanHalfFovH, isMSAA);
  143. PushRenderCommands(cmd, ShaderIDs.TiledDepth4, ShaderIDs.Occlusion4, GetSizeArray(MipLevel.L6), tanHalfFovH, isMSAA);
  144. PushUpsampleCommands(cmd, ShaderIDs.LowDepth4, ShaderIDs.Occlusion4, ShaderIDs.LowDepth3, ShaderIDs.Occlusion3, ShaderIDs.Combined3, GetSize(MipLevel.L4), GetSize(MipLevel.L3), isMSAA);
  145. PushUpsampleCommands(cmd, ShaderIDs.LowDepth3, ShaderIDs.Combined3, ShaderIDs.LowDepth2, ShaderIDs.Occlusion2, ShaderIDs.Combined2, GetSize(MipLevel.L3), GetSize(MipLevel.L2), isMSAA);
  146. PushUpsampleCommands(cmd, ShaderIDs.LowDepth2, ShaderIDs.Combined2, ShaderIDs.LowDepth1, ShaderIDs.Occlusion1, ShaderIDs.Combined1, GetSize(MipLevel.L2), GetSize(MipLevel.L1), isMSAA);
  147. PushUpsampleCommands(cmd, ShaderIDs.LowDepth1, ShaderIDs.Combined1, ShaderIDs.LinearDepth, null, destination, GetSize(MipLevel.L1), GetSize(MipLevel.Original), isMSAA, invert);
  148. // Cleanup
  149. PushReleaseCommands(cmd);
  150. }
  151. void PushAllocCommands(CommandBuffer cmd, bool isMSAA)
  152. {
  153. if(isMSAA)
  154. {
  155. Alloc(cmd, ShaderIDs.LinearDepth, MipLevel.Original, RenderTextureFormat.RGHalf, true);
  156. Alloc(cmd, ShaderIDs.LowDepth1, MipLevel.L1, RenderTextureFormat.RGFloat, true);
  157. Alloc(cmd, ShaderIDs.LowDepth2, MipLevel.L2, RenderTextureFormat.RGFloat, true);
  158. Alloc(cmd, ShaderIDs.LowDepth3, MipLevel.L3, RenderTextureFormat.RGFloat, true);
  159. Alloc(cmd, ShaderIDs.LowDepth4, MipLevel.L4, RenderTextureFormat.RGFloat, true);
  160. AllocArray(cmd, ShaderIDs.TiledDepth1, MipLevel.L3, RenderTextureFormat.RGHalf, true);
  161. AllocArray(cmd, ShaderIDs.TiledDepth2, MipLevel.L4, RenderTextureFormat.RGHalf, true);
  162. AllocArray(cmd, ShaderIDs.TiledDepth3, MipLevel.L5, RenderTextureFormat.RGHalf, true);
  163. AllocArray(cmd, ShaderIDs.TiledDepth4, MipLevel.L6, RenderTextureFormat.RGHalf, true);
  164. Alloc(cmd, ShaderIDs.Occlusion1, MipLevel.L1, RenderTextureFormat.RG16, true);
  165. Alloc(cmd, ShaderIDs.Occlusion2, MipLevel.L2, RenderTextureFormat.RG16, true);
  166. Alloc(cmd, ShaderIDs.Occlusion3, MipLevel.L3, RenderTextureFormat.RG16, true);
  167. Alloc(cmd, ShaderIDs.Occlusion4, MipLevel.L4, RenderTextureFormat.RG16, true);
  168. Alloc(cmd, ShaderIDs.Combined1, MipLevel.L1, RenderTextureFormat.RG16, true);
  169. Alloc(cmd, ShaderIDs.Combined2, MipLevel.L2, RenderTextureFormat.RG16, true);
  170. Alloc(cmd, ShaderIDs.Combined3, MipLevel.L3, RenderTextureFormat.RG16, true);
  171. }
  172. else
  173. {
  174. Alloc(cmd, ShaderIDs.LinearDepth, MipLevel.Original, RenderTextureFormat.RHalf, true);
  175. Alloc(cmd, ShaderIDs.LowDepth1, MipLevel.L1, RenderTextureFormat.RFloat, true);
  176. Alloc(cmd, ShaderIDs.LowDepth2, MipLevel.L2, RenderTextureFormat.RFloat, true);
  177. Alloc(cmd, ShaderIDs.LowDepth3, MipLevel.L3, RenderTextureFormat.RFloat, true);
  178. Alloc(cmd, ShaderIDs.LowDepth4, MipLevel.L4, RenderTextureFormat.RFloat, true);
  179. AllocArray(cmd, ShaderIDs.TiledDepth1, MipLevel.L3, RenderTextureFormat.RHalf, true);
  180. AllocArray(cmd, ShaderIDs.TiledDepth2, MipLevel.L4, RenderTextureFormat.RHalf, true);
  181. AllocArray(cmd, ShaderIDs.TiledDepth3, MipLevel.L5, RenderTextureFormat.RHalf, true);
  182. AllocArray(cmd, ShaderIDs.TiledDepth4, MipLevel.L6, RenderTextureFormat.RHalf, true);
  183. Alloc(cmd, ShaderIDs.Occlusion1, MipLevel.L1, RenderTextureFormat.R8, true);
  184. Alloc(cmd, ShaderIDs.Occlusion2, MipLevel.L2, RenderTextureFormat.R8, true);
  185. Alloc(cmd, ShaderIDs.Occlusion3, MipLevel.L3, RenderTextureFormat.R8, true);
  186. Alloc(cmd, ShaderIDs.Occlusion4, MipLevel.L4, RenderTextureFormat.R8, true);
  187. Alloc(cmd, ShaderIDs.Combined1, MipLevel.L1, RenderTextureFormat.R8, true);
  188. Alloc(cmd, ShaderIDs.Combined2, MipLevel.L2, RenderTextureFormat.R8, true);
  189. Alloc(cmd, ShaderIDs.Combined3, MipLevel.L3, RenderTextureFormat.R8, true);
  190. }
  191. }
  192. void PushDownsampleCommands(CommandBuffer cmd, Camera camera, RenderTargetIdentifier? depthMap, bool isMSAA)
  193. {
  194. RenderTargetIdentifier depthMapId;
  195. bool needDepthMapRelease = false;
  196. if (depthMap != null)
  197. {
  198. depthMapId = depthMap.Value;
  199. }
  200. else
  201. {
  202. // Make a copy of the depth texture, or reuse the resolved depth
  203. // buffer (it's only available in some specific situations).
  204. if (!RuntimeUtilities.IsResolvedDepthAvailable(camera))
  205. {
  206. Alloc(cmd, ShaderIDs.DepthCopy, MipLevel.Original, RenderTextureFormat.RFloat, false);
  207. depthMapId = new RenderTargetIdentifier(ShaderIDs.DepthCopy);
  208. cmd.BlitFullscreenTriangle(BuiltinRenderTextureType.None, depthMapId, m_PropertySheet, (int)Pass.DepthCopy);
  209. needDepthMapRelease = true;
  210. }
  211. else
  212. {
  213. depthMapId = BuiltinRenderTextureType.ResolvedDepth;
  214. }
  215. }
  216. // 1st downsampling pass.
  217. var cs = m_Resources.computeShaders.multiScaleAODownsample1;
  218. int kernel = cs.FindKernel(isMSAA ? "MultiScaleVODownsample1_MSAA" : "MultiScaleVODownsample1");
  219. cmd.SetComputeTextureParam(cs, kernel, "LinearZ", ShaderIDs.LinearDepth);
  220. cmd.SetComputeTextureParam(cs, kernel, "DS2x", ShaderIDs.LowDepth1);
  221. cmd.SetComputeTextureParam(cs, kernel, "DS4x", ShaderIDs.LowDepth2);
  222. cmd.SetComputeTextureParam(cs, kernel, "DS2xAtlas", ShaderIDs.TiledDepth1);
  223. cmd.SetComputeTextureParam(cs, kernel, "DS4xAtlas", ShaderIDs.TiledDepth2);
  224. cmd.SetComputeVectorParam(cs, "ZBufferParams", CalculateZBufferParams(camera));
  225. cmd.SetComputeTextureParam(cs, kernel, "Depth", depthMapId);
  226. cmd.DispatchCompute(cs, kernel, m_Widths[(int)MipLevel.L4], m_Heights[(int)MipLevel.L4], 1);
  227. if (needDepthMapRelease)
  228. Release(cmd, ShaderIDs.DepthCopy);
  229. // 2nd downsampling pass.
  230. cs = m_Resources.computeShaders.multiScaleAODownsample2;
  231. kernel = isMSAA ? cs.FindKernel("MultiScaleVODownsample2_MSAA") : cs.FindKernel("MultiScaleVODownsample2");
  232. cmd.SetComputeTextureParam(cs, kernel, "DS4x", ShaderIDs.LowDepth2);
  233. cmd.SetComputeTextureParam(cs, kernel, "DS8x", ShaderIDs.LowDepth3);
  234. cmd.SetComputeTextureParam(cs, kernel, "DS16x", ShaderIDs.LowDepth4);
  235. cmd.SetComputeTextureParam(cs, kernel, "DS8xAtlas", ShaderIDs.TiledDepth3);
  236. cmd.SetComputeTextureParam(cs, kernel, "DS16xAtlas", ShaderIDs.TiledDepth4);
  237. cmd.DispatchCompute(cs, kernel, m_Widths[(int)MipLevel.L6], m_Heights[(int)MipLevel.L6], 1);
  238. }
  239. void PushRenderCommands(CommandBuffer cmd, int source, int destination, Vector3 sourceSize, float tanHalfFovH, bool isMSAA)
  240. {
  241. // Here we compute multipliers that convert the center depth value into (the reciprocal
  242. // of) sphere thicknesses at each sample location. This assumes a maximum sample radius
  243. // of 5 units, but since a sphere has no thickness at its extent, we don't need to
  244. // sample that far out. Only samples whole integer offsets with distance less than 25
  245. // are used. This means that there is no sample at (3, 4) because its distance is
  246. // exactly 25 (and has a thickness of 0.)
  247. // The shaders are set up to sample a circular region within a 5-pixel radius.
  248. const float kScreenspaceDiameter = 10f;
  249. // SphereDiameter = CenterDepth * ThicknessMultiplier. This will compute the thickness
  250. // of a sphere centered at a specific depth. The ellipsoid scale can stretch a sphere
  251. // into an ellipsoid, which changes the characteristics of the AO.
  252. // TanHalfFovH: Radius of sphere in depth units if its center lies at Z = 1
  253. // ScreenspaceDiameter: Diameter of sample sphere in pixel units
  254. // ScreenspaceDiameter / BufferWidth: Ratio of the screen width that the sphere actually covers
  255. float thicknessMultiplier = 2f * tanHalfFovH * kScreenspaceDiameter / sourceSize.x;
  256. if (RuntimeUtilities.isSinglePassStereoEnabled)
  257. thicknessMultiplier *= 2f;
  258. // This will transform a depth value from [0, thickness] to [0, 1].
  259. float inverseRangeFactor = 1f / thicknessMultiplier;
  260. // The thicknesses are smaller for all off-center samples of the sphere. Compute
  261. // thicknesses relative to the center sample.
  262. for (int i = 0; i < 12; i++)
  263. m_InvThicknessTable[i] = inverseRangeFactor / m_SampleThickness[i];
  264. // These are the weights that are multiplied against the samples because not all samples
  265. // are equally important. The farther the sample is from the center location, the less
  266. // they matter. We use the thickness of the sphere to determine the weight. The scalars
  267. // in front are the number of samples with this weight because we sum the samples
  268. // together before multiplying by the weight, so as an aggregate all of those samples
  269. // matter more. After generating this table, the weights are normalized.
  270. m_SampleWeightTable[ 0] = 4 * m_SampleThickness[ 0]; // Axial
  271. m_SampleWeightTable[ 1] = 4 * m_SampleThickness[ 1]; // Axial
  272. m_SampleWeightTable[ 2] = 4 * m_SampleThickness[ 2]; // Axial
  273. m_SampleWeightTable[ 3] = 4 * m_SampleThickness[ 3]; // Axial
  274. m_SampleWeightTable[ 4] = 4 * m_SampleThickness[ 4]; // Diagonal
  275. m_SampleWeightTable[ 5] = 8 * m_SampleThickness[ 5]; // L-shaped
  276. m_SampleWeightTable[ 6] = 8 * m_SampleThickness[ 6]; // L-shaped
  277. m_SampleWeightTable[ 7] = 8 * m_SampleThickness[ 7]; // L-shaped
  278. m_SampleWeightTable[ 8] = 4 * m_SampleThickness[ 8]; // Diagonal
  279. m_SampleWeightTable[ 9] = 8 * m_SampleThickness[ 9]; // L-shaped
  280. m_SampleWeightTable[10] = 8 * m_SampleThickness[10]; // L-shaped
  281. m_SampleWeightTable[11] = 4 * m_SampleThickness[11]; // Diagonal
  282. // Zero out the unused samples.
  283. // FIXME: should we support SAMPLE_EXHAUSTIVELY mode?
  284. m_SampleWeightTable[0] = 0;
  285. m_SampleWeightTable[2] = 0;
  286. m_SampleWeightTable[5] = 0;
  287. m_SampleWeightTable[7] = 0;
  288. m_SampleWeightTable[9] = 0;
  289. // Normalize the weights by dividing by the sum of all weights
  290. var totalWeight = 0f;
  291. foreach (float w in m_SampleWeightTable)
  292. totalWeight += w;
  293. for (int i = 0; i < m_SampleWeightTable.Length; i++)
  294. m_SampleWeightTable[i] /= totalWeight;
  295. // Set the arguments for the render kernel.
  296. var cs = m_Resources.computeShaders.multiScaleAORender;
  297. int kernel = isMSAA ? cs.FindKernel("MultiScaleVORender_MSAA_interleaved") : cs.FindKernel("MultiScaleVORender_interleaved");
  298. cmd.SetComputeFloatParams(cs, "gInvThicknessTable", m_InvThicknessTable);
  299. cmd.SetComputeFloatParams(cs, "gSampleWeightTable", m_SampleWeightTable);
  300. cmd.SetComputeVectorParam(cs, "gInvSliceDimension", new Vector2(1f / sourceSize.x, 1f / sourceSize.y));
  301. cmd.SetComputeVectorParam(cs, "AdditionalParams", new Vector2(-1f / m_Settings.thicknessModifier.value, m_Settings.intensity.value));
  302. cmd.SetComputeTextureParam(cs, kernel, "DepthTex", source);
  303. cmd.SetComputeTextureParam(cs, kernel, "Occlusion", destination);
  304. // Calculate the thread group count and add a dispatch command with them.
  305. uint xsize, ysize, zsize;
  306. cs.GetKernelThreadGroupSizes(kernel, out xsize, out ysize, out zsize);
  307. cmd.DispatchCompute(
  308. cs, kernel,
  309. ((int)sourceSize.x + (int)xsize - 1) / (int)xsize,
  310. ((int)sourceSize.y + (int)ysize - 1) / (int)ysize,
  311. ((int)sourceSize.z + (int)zsize - 1) / (int)zsize
  312. );
  313. }
  314. void PushUpsampleCommands(CommandBuffer cmd, int lowResDepth, int interleavedAO, int highResDepth, int? highResAO, RenderTargetIdentifier dest, Vector3 lowResDepthSize, Vector2 highResDepthSize, bool isMSAA, bool invert = false)
  315. {
  316. var cs = m_Resources.computeShaders.multiScaleAOUpsample;
  317. int kernel = 0;
  318. if (!isMSAA)
  319. {
  320. kernel = cs.FindKernel(highResAO == null ? invert
  321. ? "MultiScaleVOUpSample_invert"
  322. : "MultiScaleVOUpSample"
  323. : "MultiScaleVOUpSample_blendout");
  324. }
  325. else
  326. {
  327. kernel = cs.FindKernel(highResAO == null ? invert
  328. ? "MultiScaleVOUpSample_MSAA_invert"
  329. : "MultiScaleVOUpSample_MSAA"
  330. : "MultiScaleVOUpSample_MSAA_blendout");
  331. }
  332. float stepSize = 1920f / lowResDepthSize.x;
  333. float bTolerance = 1f - Mathf.Pow(10f, m_Settings.blurTolerance.value) * stepSize;
  334. bTolerance *= bTolerance;
  335. float uTolerance = Mathf.Pow(10f, m_Settings.upsampleTolerance.value);
  336. float noiseFilterWeight = 1f / (Mathf.Pow(10f, m_Settings.noiseFilterTolerance.value) + uTolerance);
  337. cmd.SetComputeVectorParam(cs, "InvLowResolution", new Vector2(1f / lowResDepthSize.x, 1f / lowResDepthSize.y));
  338. cmd.SetComputeVectorParam(cs, "InvHighResolution", new Vector2(1f / highResDepthSize.x, 1f / highResDepthSize.y));
  339. cmd.SetComputeVectorParam(cs, "AdditionalParams", new Vector4(noiseFilterWeight, stepSize, bTolerance, uTolerance));
  340. cmd.SetComputeTextureParam(cs, kernel, "LoResDB", lowResDepth);
  341. cmd.SetComputeTextureParam(cs, kernel, "HiResDB", highResDepth);
  342. cmd.SetComputeTextureParam(cs, kernel, "LoResAO1", interleavedAO);
  343. if (highResAO != null)
  344. cmd.SetComputeTextureParam(cs, kernel, "HiResAO", highResAO.Value);
  345. cmd.SetComputeTextureParam(cs, kernel, "AoResult", dest);
  346. int xcount = ((int)highResDepthSize.x + 17) / 16;
  347. int ycount = ((int)highResDepthSize.y + 17) / 16;
  348. cmd.DispatchCompute(cs, kernel, xcount, ycount, 1);
  349. }
  350. void PushReleaseCommands(CommandBuffer cmd)
  351. {
  352. Release(cmd, ShaderIDs.LinearDepth);
  353. Release(cmd, ShaderIDs.LowDepth1);
  354. Release(cmd, ShaderIDs.LowDepth2);
  355. Release(cmd, ShaderIDs.LowDepth3);
  356. Release(cmd, ShaderIDs.LowDepth4);
  357. Release(cmd, ShaderIDs.TiledDepth1);
  358. Release(cmd, ShaderIDs.TiledDepth2);
  359. Release(cmd, ShaderIDs.TiledDepth3);
  360. Release(cmd, ShaderIDs.TiledDepth4);
  361. Release(cmd, ShaderIDs.Occlusion1);
  362. Release(cmd, ShaderIDs.Occlusion2);
  363. Release(cmd, ShaderIDs.Occlusion3);
  364. Release(cmd, ShaderIDs.Occlusion4);
  365. Release(cmd, ShaderIDs.Combined1);
  366. Release(cmd, ShaderIDs.Combined2);
  367. Release(cmd, ShaderIDs.Combined3);
  368. }
  369. void PreparePropertySheet(PostProcessRenderContext context)
  370. {
  371. var sheet = context.propertySheets.Get(m_Resources.shaders.multiScaleAO);
  372. sheet.ClearKeywords();
  373. sheet.properties.SetVector(ShaderIDs.AOColor, Color.white - m_Settings.color.value);
  374. m_PropertySheet = sheet;
  375. }
  376. void CheckAOTexture(PostProcessRenderContext context)
  377. {
  378. if (m_AmbientOnlyAO == null || !m_AmbientOnlyAO.IsCreated() || m_AmbientOnlyAO.width != context.width || m_AmbientOnlyAO.height != context.height)
  379. {
  380. RuntimeUtilities.Destroy(m_AmbientOnlyAO);
  381. m_AmbientOnlyAO = new RenderTexture(context.width, context.height, 0, RenderTextureFormat.R8, RenderTextureReadWrite.Linear)
  382. {
  383. hideFlags = HideFlags.DontSave,
  384. filterMode = FilterMode.Point,
  385. enableRandomWrite = true
  386. };
  387. m_AmbientOnlyAO.Create();
  388. }
  389. }
  390. void PushDebug(PostProcessRenderContext context)
  391. {
  392. if (context.IsDebugOverlayEnabled(DebugOverlay.AmbientOcclusion))
  393. context.PushDebugOverlay(context.command, m_AmbientOnlyAO, m_PropertySheet, (int)Pass.DebugOverlay);
  394. }
  395. public void RenderAfterOpaque(PostProcessRenderContext context)
  396. {
  397. var cmd = context.command;
  398. cmd.BeginSample("Ambient Occlusion");
  399. SetResources(context.resources);
  400. PreparePropertySheet(context);
  401. CheckAOTexture(context);
  402. // In Forward mode, fog is applied at the object level in the grometry pass so we need
  403. // to apply it to AO as well or it'll drawn on top of the fog effect.
  404. if (context.camera.actualRenderingPath == RenderingPath.Forward && RenderSettings.fog)
  405. {
  406. m_PropertySheet.EnableKeyword("APPLY_FORWARD_FOG");
  407. m_PropertySheet.properties.SetVector(
  408. ShaderIDs.FogParams,
  409. new Vector3(RenderSettings.fogDensity, RenderSettings.fogStartDistance, RenderSettings.fogEndDistance)
  410. );
  411. }
  412. GenerateAOMap(cmd, context.camera, m_AmbientOnlyAO, null, false, false);
  413. PushDebug(context);
  414. cmd.SetGlobalTexture(ShaderIDs.MSVOcclusionTexture, m_AmbientOnlyAO);
  415. cmd.BlitFullscreenTriangle(BuiltinRenderTextureType.None, BuiltinRenderTextureType.CameraTarget, m_PropertySheet, (int)Pass.CompositionForward, RenderBufferLoadAction.Load);
  416. cmd.EndSample("Ambient Occlusion");
  417. }
  418. public void RenderAmbientOnly(PostProcessRenderContext context)
  419. {
  420. var cmd = context.command;
  421. cmd.BeginSample("Ambient Occlusion Render");
  422. SetResources(context.resources);
  423. PreparePropertySheet(context);
  424. CheckAOTexture(context);
  425. GenerateAOMap(cmd, context.camera, m_AmbientOnlyAO, null, false, false);
  426. PushDebug(context);
  427. cmd.EndSample("Ambient Occlusion Render");
  428. }
  429. public void CompositeAmbientOnly(PostProcessRenderContext context)
  430. {
  431. var cmd = context.command;
  432. cmd.BeginSample("Ambient Occlusion Composite");
  433. cmd.SetGlobalTexture(ShaderIDs.MSVOcclusionTexture, m_AmbientOnlyAO);
  434. cmd.BlitFullscreenTriangle(BuiltinRenderTextureType.None, m_MRT, BuiltinRenderTextureType.CameraTarget, m_PropertySheet, (int)Pass.CompositionDeferred);
  435. cmd.EndSample("Ambient Occlusion Composite");
  436. }
  437. public void Release()
  438. {
  439. RuntimeUtilities.Destroy(m_AmbientOnlyAO);
  440. m_AmbientOnlyAO = null;
  441. }
  442. }
  443. #else
  444. [Serializable]
  445. public sealed class MultiScaleVO : IAmbientOcclusionMethod
  446. {
  447. public MultiScaleVO(AmbientOcclusion settings)
  448. {
  449. }
  450. public void SetResources(PostProcessResources resources)
  451. {
  452. }
  453. public DepthTextureMode GetCameraFlags()
  454. {
  455. return DepthTextureMode.None;
  456. }
  457. public void GenerateAOMap(CommandBuffer cmd, Camera camera, RenderTargetIdentifier destination, RenderTargetIdentifier? depthMap, bool invert, bool isMSAA)
  458. {
  459. }
  460. public void RenderAfterOpaque(PostProcessRenderContext context)
  461. {
  462. }
  463. public void RenderAmbientOnly(PostProcessRenderContext context)
  464. {
  465. }
  466. public void CompositeAmbientOnly(PostProcessRenderContext context)
  467. {
  468. }
  469. public void Release()
  470. {
  471. }
  472. }
  473. #endif
  474. }