private void PushDownsampleCommands(CommandBuffer cmd, Camera camera, RenderTargetIdentifier?depthMap) { RenderTargetIdentifier depthMapId; var needDepthMapRelease = false; if (depthMap != null) { depthMapId = depthMap.Value; } else { // Make a copy of the depth texture, or reuse the resolved depth // buffer (it's only available in some specific situations). if (!RuntimeUtilities.IsResolvedDepthAvailable(camera)) { Alloc(cmd, ShaderIDs.DepthCopy, MipLevel.Original, RenderTextureFormat.RFloat, false); depthMapId = new RenderTargetIdentifier(ShaderIDs.DepthCopy); cmd.BlitFullscreenTriangle(BuiltinRenderTextureType.None, depthMapId, m_PropertySheet, (int)Pass.DepthCopy); needDepthMapRelease = true; } else { depthMapId = BuiltinRenderTextureType.ResolvedDepth; } } // 1st downsampling pass. var cs = m_Resources.computeShaders.multiScaleAODownsample1; var kernel = cs.FindKernel("main"); cmd.SetComputeTextureParam(cs, kernel, "LinearZ", ShaderIDs.LinearDepth); cmd.SetComputeTextureParam(cs, kernel, "DS2x", ShaderIDs.LowDepth1); cmd.SetComputeTextureParam(cs, kernel, "DS4x", ShaderIDs.LowDepth2); cmd.SetComputeTextureParam(cs, kernel, "DS2xAtlas", ShaderIDs.TiledDepth1); cmd.SetComputeTextureParam(cs, kernel, "DS4xAtlas", ShaderIDs.TiledDepth2); cmd.SetComputeVectorParam(cs, "ZBufferParams", CalculateZBufferParams(camera)); cmd.SetComputeTextureParam(cs, kernel, "Depth", depthMapId); cmd.DispatchCompute(cs, kernel, m_Widths[(int)MipLevel.L4], m_Heights[(int)MipLevel.L4], 1); if (needDepthMapRelease) { Release(cmd, ShaderIDs.DepthCopy); } // 2nd downsampling pass. cs = m_Resources.computeShaders.multiScaleAODownsample2; kernel = cs.FindKernel("main"); cmd.SetComputeTextureParam(cs, kernel, "DS4x", ShaderIDs.LowDepth2); cmd.SetComputeTextureParam(cs, kernel, "DS8x", ShaderIDs.LowDepth3); cmd.SetComputeTextureParam(cs, kernel, "DS16x", ShaderIDs.LowDepth4); cmd.SetComputeTextureParam(cs, kernel, "DS8xAtlas", ShaderIDs.TiledDepth3); cmd.SetComputeTextureParam(cs, kernel, "DS16xAtlas", ShaderIDs.TiledDepth4); cmd.DispatchCompute(cs, kernel, m_Widths[(int)MipLevel.L6], m_Heights[(int)MipLevel.L6], 1); }
private void PushDownsampleCommands(CommandBuffer cmd, Camera camera, RenderTargetIdentifier?depthMap, bool isMSAA) { bool flag = false; RenderTargetIdentifier renderTargetIdentifier; if (depthMap.HasValue) { renderTargetIdentifier = depthMap.Value; } else if (!RuntimeUtilities.IsResolvedDepthAvailable(camera)) { Alloc(cmd, ShaderIDs.DepthCopy, MipLevel.Original, RenderTextureFormat.RFloat, uav: false); renderTargetIdentifier = new RenderTargetIdentifier(ShaderIDs.DepthCopy); cmd.BlitFullscreenTriangle(BuiltinRenderTextureType.None, renderTargetIdentifier, m_PropertySheet, 0); flag = true; } else { renderTargetIdentifier = BuiltinRenderTextureType.ResolvedDepth; } ComputeShader multiScaleAODownsample = m_Resources.computeShaders.multiScaleAODownsample1; int kernelIndex = multiScaleAODownsample.FindKernel((!isMSAA) ? "MultiScaleVODownsample1" : "MultiScaleVODownsample1_MSAA"); cmd.SetComputeTextureParam(multiScaleAODownsample, kernelIndex, "LinearZ", ShaderIDs.LinearDepth); cmd.SetComputeTextureParam(multiScaleAODownsample, kernelIndex, "DS2x", ShaderIDs.LowDepth1); cmd.SetComputeTextureParam(multiScaleAODownsample, kernelIndex, "DS4x", ShaderIDs.LowDepth2); cmd.SetComputeTextureParam(multiScaleAODownsample, kernelIndex, "DS2xAtlas", ShaderIDs.TiledDepth1); cmd.SetComputeTextureParam(multiScaleAODownsample, kernelIndex, "DS4xAtlas", ShaderIDs.TiledDepth2); cmd.SetComputeVectorParam(multiScaleAODownsample, "ZBufferParams", CalculateZBufferParams(camera)); cmd.SetComputeTextureParam(multiScaleAODownsample, kernelIndex, "Depth", renderTargetIdentifier); cmd.DispatchCompute(multiScaleAODownsample, kernelIndex, m_Widths[4], m_Heights[4], 1); if (flag) { Release(cmd, ShaderIDs.DepthCopy); } multiScaleAODownsample = m_Resources.computeShaders.multiScaleAODownsample2; kernelIndex = ((!isMSAA) ? multiScaleAODownsample.FindKernel("MultiScaleVODownsample2") : multiScaleAODownsample.FindKernel("MultiScaleVODownsample2_MSAA")); cmd.SetComputeTextureParam(multiScaleAODownsample, kernelIndex, "DS4x", ShaderIDs.LowDepth2); cmd.SetComputeTextureParam(multiScaleAODownsample, kernelIndex, "DS8x", ShaderIDs.LowDepth3); cmd.SetComputeTextureParam(multiScaleAODownsample, kernelIndex, "DS16x", ShaderIDs.LowDepth4); cmd.SetComputeTextureParam(multiScaleAODownsample, kernelIndex, "DS8xAtlas", ShaderIDs.TiledDepth3); cmd.SetComputeTextureParam(multiScaleAODownsample, kernelIndex, "DS16xAtlas", ShaderIDs.TiledDepth4); cmd.DispatchCompute(multiScaleAODownsample, kernelIndex, m_Widths[6], m_Heights[6], 1); }
public override void Render(PostProcessRenderContext context) { var cmd = context.command; cmd.BeginSample("Outline"); var width = context.width; var height = context.height; context.GetScreenSpaceTemporaryRT(cmd, maskRT, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default, FilterMode.Bilinear, width, height); context.GetScreenSpaceTemporaryRT(cmd, bluredMaskRT, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default, FilterMode.Bilinear, width, height); var tempMask = bluredMaskRT; // borrowing bluredMask to use var useResolvedDepth = RuntimeUtilities.IsResolvedDepthAvailable(context.camera); var depthMap = useResolvedDepth ? BuiltinRenderTextureType.ResolvedDepth : BuiltinRenderTextureType.Depth; cmd.SetRenderTarget(tempMask, depthMap); cmd.ClearRenderTarget(false, true, Color.clear); foreach (var entity in OutlineEntityManager.instance.entities) { // TODO: check visible // TODO: handle submesh cmd.DrawRenderer(entity.renderer, entity.renderer.sharedMaterial, 0, 0); } var outlineSheet = context.propertySheets.Get(outlineShader); cmd.BlitFullscreenTriangle(tempMask, maskRT, outlineSheet, 0); var blurSheet = context.propertySheets.Get(blurShader); DualBlurMask(context, blurSheet, maskRT, bluredMaskRT, width, height, settings.iterations); outlineSheet.properties.SetColor(outlineColorId, settings.outlineColor); cmd.SetGlobalTexture("_OutlineMaskTex", maskRT); cmd.SetGlobalTexture("_BluredMaskTex", bluredMaskRT); cmd.BlitFullscreenTriangle(context.source, context.destination, outlineSheet, 1); cmd.ReleaseTemporaryRT(maskRT); cmd.ReleaseTemporaryRT(bluredMaskRT); cmd.EndSample("Outline"); }
void PushDownsampleCommands(PostProcessRenderContext context, CommandBuffer cmd) { // Make a copy of the depth texture, or reuse the resolved depth // buffer (it's only available in some specific situations). var useDepthCopy = !RuntimeUtilities.IsResolvedDepthAvailable(context.camera); if (useDepthCopy) { m_DepthCopy.PushAllocationCommand(cmd); cmd.BlitFullscreenTriangle(BuiltinRenderTextureType.None, m_DepthCopy.id, m_PropertySheet, (int)Pass.DepthCopy); } // Temporary buffer allocations. m_LinearDepth.PushAllocationCommand(cmd); m_LowDepth1.PushAllocationCommand(cmd); m_LowDepth2.PushAllocationCommand(cmd); m_LowDepth3.PushAllocationCommand(cmd); m_LowDepth4.PushAllocationCommand(cmd); #if UNITY_2017_1_OR_NEWER m_TiledDepth1.PushAllocationCommand(cmd); m_TiledDepth2.PushAllocationCommand(cmd); m_TiledDepth3.PushAllocationCommand(cmd); m_TiledDepth4.PushAllocationCommand(cmd); #endif // 1st downsampling pass. var cs = context.resources.computeShaders.multiScaleAODownsample1; int kernel = cs.FindKernel("main"); cmd.SetComputeTextureParam(cs, kernel, "LinearZ", m_LinearDepth.id); cmd.SetComputeTextureParam(cs, kernel, "DS2x", m_LowDepth1.id); cmd.SetComputeTextureParam(cs, kernel, "DS4x", m_LowDepth2.id); cmd.SetComputeTextureParam(cs, kernel, "DS2xAtlas", m_TiledDepth1.id); cmd.SetComputeTextureParam(cs, kernel, "DS4xAtlas", m_TiledDepth2.id); cmd.SetComputeVectorParam(cs, "ZBufferParams", CalculateZBufferParams(context.camera)); cmd.SetComputeTextureParam(cs, kernel, "Depth", useDepthCopy ? m_DepthCopy.id : BuiltinRenderTextureType.ResolvedDepth ); cmd.DispatchCompute(cs, kernel, m_TiledDepth2.width, m_TiledDepth2.height, 1); if (useDepthCopy) { cmd.ReleaseTemporaryRT(m_DepthCopy.nameID); } // 2nd downsampling pass. cs = context.resources.computeShaders.multiScaleAODownsample2; kernel = cs.FindKernel("main"); cmd.SetComputeTextureParam(cs, kernel, "DS4x", m_LowDepth2.id); cmd.SetComputeTextureParam(cs, kernel, "DS8x", m_LowDepth3.id); cmd.SetComputeTextureParam(cs, kernel, "DS16x", m_LowDepth4.id); cmd.SetComputeTextureParam(cs, kernel, "DS8xAtlas", m_TiledDepth3.id); cmd.SetComputeTextureParam(cs, kernel, "DS16xAtlas", m_TiledDepth4.id); cmd.DispatchCompute(cs, kernel, m_TiledDepth4.width, m_TiledDepth4.height, 1); }