void CreateIfNeeded(RTCategory category)
        {
            // If a RT was first created for MSAA then the regular one might be null, in this case we create it.
            // That's why we never test the MSAA version: It should always be there if RT was declared correctly.
            if (category == RTCategory.Regular && m_RTs[(int)RTCategory.Regular] == null)
            {
                RenderTexture refRT = m_RTs[(int)RTCategory.MSAA];
                Debug.Assert(refRT != null);
                Vector2Int scaledSize = GetScaledSize(new Vector2Int(s_MaxWidth, s_MaxHeight));

                RenderTexture newRT = new RenderTexture(scaledSize.x, scaledSize.y, refRT.depth, refRT.format, refRT.sRGB ? RenderTextureReadWrite.sRGB : RenderTextureReadWrite.Linear)
                {
                    hideFlags         = HideFlags.HideAndDontSave,
                    volumeDepth       = refRT.volumeDepth,
                    filterMode        = refRT.filterMode,
                    wrapMode          = refRT.wrapMode,
                    dimension         = refRT.dimension,
                    enableRandomWrite = m_EnableRandomWrite, // We cannot take the info from the msaa rt since we force it to 1
                    useMipMap         = refRT.useMipMap,
                    autoGenerateMips  = refRT.autoGenerateMips,
                    anisoLevel        = refRT.anisoLevel,
                    mipMapBias        = refRT.mipMapBias,
                    antiAliasing      = 1,     // No MSAA for the regular version of the texture.
                    bindTextureMS     = false, // Somehow, this can be true even if antiAliasing == 1. Leads to Unity-internal binding errors.
                    useDynamicScale   = refRT.useDynamicScale,
                    vrUsage           = refRT.vrUsage,
                    memorylessMode    = refRT.memorylessMode,
                    name = CoreUtils.GetRenderTargetAutoName(refRT.width, refRT.height, refRT.format, m_Name, mips: refRT.useMipMap)
                };
                newRT.Create();

                m_RTs[(int)RTCategory.Regular]     = newRT;
                m_NameIDs[(int)RTCategory.Regular] = new RenderTargetIdentifier(newRT);
            }
        }
        static void Resize(int width, int height, RTCategory category, MSAASamples msaaSamples)
        {
            s_MaxWidths[(int)category]   = width;
            s_MaxHeights[(int)category]  = height;
            s_ScaledRTCurrentMSAASamples = msaaSamples;

            var maxSize = new Vector2Int(width, height);

            s_ScaledRTCurrentCategory = category;

            foreach (var rth in s_AutoSizedRTs)
            {
                var rt = rth.m_RTs[(int)category];

                // This can happen if you create a RTH for MSAA. By default we only create the MSAA version of the target.
                // Missing version will be created when needed in the getter.
                if (rt != null)
                {
                    rt.Release();

                    Vector2Int scaledSize = rth.GetScaledSize(maxSize);

                    rt.width  = Mathf.Max(scaledSize.x, 1);
                    rt.height = Mathf.Max(scaledSize.y, 1);

                    if (category == RTCategory.MSAA)
                    {
                        rt.antiAliasing = (int)s_ScaledRTCurrentMSAASamples;
                    }

                    rt.name = CoreUtils.GetRenderTargetAutoName(rt.width, rt.height, rt.format, rth.m_Name, mips: rt.useMipMap, enableMSAA: category == RTCategory.MSAA, msaaSamples: s_ScaledRTCurrentMSAASamples);
                    rt.Create();
                }
            }
        }
        public bool AllocTextureArray(int numCubeMaps, int width, TextureFormat format, bool isMipMapped, Material cubeBlitMaterial)
        {
            var res = AllocTextureArray(numCubeMaps);

            m_NumMipLevels = GetNumMips(width, width);      // will calculate same way whether we have cube array or not

            if (!TextureCache.supportsCubemapArrayTextures)
            {
                m_CubeBlitMaterial = cubeBlitMaterial;

                int panoWidthTop  = 4 * width;
                int panoHeightTop = 2 * width;

                // create panorama 2D array. Hardcoding the render target for now. No convenient way atm to
                // map from TextureFormat to RenderTextureFormat and don't want to deal with sRGB issues for now.
                m_CacheNoCubeArray = new Texture2DArray(panoWidthTop, panoHeightTop, numCubeMaps, TextureFormat.RGBAHalf, isMipMapped)
                {
                    hideFlags  = HideFlags.HideAndDontSave,
                    wrapMode   = TextureWrapMode.Repeat,
                    wrapModeV  = TextureWrapMode.Clamp,
                    filterMode = FilterMode.Trilinear,
                    anisoLevel = 0,
                    name       = CoreUtils.GetTextureAutoName(panoWidthTop, panoHeightTop, format, TextureDimension.Tex2DArray, depth: numCubeMaps, name: m_CacheName)
                };

                m_NumPanoMipLevels = isMipMapped ? GetNumMips(panoWidthTop, panoHeightTop) : 1;
                m_StagingRTs       = new RenderTexture[m_NumPanoMipLevels];
                for (int m = 0; m < m_NumPanoMipLevels; m++)
                {
                    m_StagingRTs[m] = new RenderTexture(Mathf.Max(1, panoWidthTop >> m), Mathf.Max(1, panoHeightTop >> m), 0, RenderTextureFormat.ARGBHalf)
                    {
                        hideFlags = HideFlags.HideAndDontSave
                    };
                    m_StagingRTs[m].name = CoreUtils.GetRenderTargetAutoName(Mathf.Max(1, panoWidthTop >> m), Mathf.Max(1, panoHeightTop >> m), 1, RenderTextureFormat.ARGBHalf, String.Format("PanaCache{0}", m));
                }

                if (m_CubeBlitMaterial)
                {
                    m_CubeMipLevelPropName = Shader.PropertyToID("_cubeMipLvl");
                    m_cubeSrcTexPropName   = Shader.PropertyToID("_srcCubeTexture");
                }
            }
            else
            {
                m_Cache = new CubemapArray(width, numCubeMaps, format, isMipMapped)
                {
                    hideFlags  = HideFlags.HideAndDontSave,
                    wrapMode   = TextureWrapMode.Clamp,
                    filterMode = FilterMode.Trilinear,
                    anisoLevel = 0, // It is important to set 0 here, else unity force anisotropy filtering
                    name       = CoreUtils.GetTextureAutoName(width, width, format, TextureDimension.CubeArray, depth: numCubeMaps, name: m_CacheName)
                };
            }

            return(res);
        }
        void Resize(int width, int height, MSAASamples msaaSamples, bool sizeChanged, bool msaaSampleChanged)
        {
            m_MaxWidths  = Math.Max(width, m_MaxWidths);
            m_MaxHeights = Math.Max(height, m_MaxHeights);
            m_ScaledRTCurrentMSAASamples = msaaSamples;

            var maxSize = new Vector2Int(m_MaxWidths, m_MaxHeights);

            Array.Resize(ref m_AutoSizedRTsArray, m_AutoSizedRTs.Count);
            m_AutoSizedRTs.CopyTo(m_AutoSizedRTsArray);

            for (int i = 0, c = m_AutoSizedRTsArray.Length; i < c; ++i)
            {
                // Grab the RT Handle
                var rth = m_AutoSizedRTsArray[i];

                // If we are only processing MSAA sample count change, make sure this RT is an MSAA one
                if (!sizeChanged && msaaSampleChanged && !rth.m_EnableMSAA)
                {
                    continue;
                }

                // Force its new reference size
                rth.referenceSize = maxSize;

                // Grab the render texture
                var renderTexture = rth.m_RT;

                // Free the previous version
                renderTexture.Release();

                // Get the scaled size
                var scaledSize = rth.GetScaledSize(maxSize);

                renderTexture.width  = Mathf.Max(scaledSize.x, 1);
                renderTexture.height = Mathf.Max(scaledSize.y, 1);

                // If this is a msaa texture, make sure to update its msaa count
                if (rth.m_EnableMSAA)
                {
                    renderTexture.antiAliasing = (int)m_ScaledRTCurrentMSAASamples;
                }

                // Regenerate the name
                renderTexture.name = CoreUtils.GetRenderTargetAutoName(renderTexture.width, renderTexture.height, renderTexture.volumeDepth, renderTexture.format, rth.m_Name, mips: renderTexture.useMipMap, enableMSAA: rth.m_EnableMSAA, msaaSamples: m_ScaledRTCurrentMSAASamples);

                // Create the render texture
                renderTexture.Create();
            }
        }
        public void DemandResize(RTHandle rth)
        {
            Assert.IsTrue(m_ResizeOnDemandRTs.Contains(rth), "The RTHandle is not an resize on demand handle in this RTHandleSystem. Please call SwitchToResizeOnDemand(rth, true) before resizing on demand.");

            // Grab the render texture
            var rt = rth.m_RT;

            rth.referenceSize = new Vector2Int(m_MaxWidths, m_MaxHeights);
            var scaledSize = rth.GetScaledSize(rth.referenceSize);

            scaledSize = Vector2Int.Max(Vector2Int.one, scaledSize);

            // Did the size change?
            var sizeChanged = rt.width != scaledSize.x || rt.height != scaledSize.y;
            // If this is an MSAA texture, did the sample count change?
            var msaaSampleChanged = rth.m_EnableMSAA && rt.antiAliasing != (int)m_ScaledRTCurrentMSAASamples;

            if (sizeChanged || msaaSampleChanged)
            {
                // Free this render texture
                rt.Release();

                // Update the antialiasing count
                if (rth.m_EnableMSAA)
                {
                    rt.antiAliasing = (int)m_ScaledRTCurrentMSAASamples;
                }

                // Update the size
                rt.width  = scaledSize.x;
                rt.height = scaledSize.y;

                // Generate a new name
                rt.name = CoreUtils.GetRenderTargetAutoName(
                    rt.width,
                    rt.height,
                    rt.volumeDepth,
                    rt.format,
                    rth.m_Name,
                    mips: rt.useMipMap,
                    enableMSAA: rth.m_EnableMSAA,
                    msaaSamples: m_ScaledRTCurrentMSAASamples
                    );

                // Create the new texture
                rt.Create();
            }
        }
        public void DemandResize(RTHandle rth)
        {
            Assert.IsTrue(m_ResizeOnDemandRTs.Contains(rth), "The RTHandle is not an resize on demand handle in this RTHandleSystem. Please call SwitchToResizeOnDemand(rth, true) before resizing on demand.");

            for (int i = 0, c = (int)RTCategory.Count; i < c; ++i)
            {
                if (rth.m_RTs[i] == null)
                {
                    continue;
                }

                var rt = rth.m_RTs[i];
                rth.referenceSize = new Vector2Int(m_MaxWidths[i], m_MaxHeights[i]);
                var scaledSize = rth.GetScaledSize(rth.referenceSize);
                scaledSize = Vector2Int.Max(Vector2Int.one, scaledSize);

                var enableMSAA        = i == (int)RTCategory.MSAA;
                var sizeChanged       = rt.width != scaledSize.x || rt.height != scaledSize.y;
                var msaaSampleChanged = enableMSAA && rt.antiAliasing != (int)m_ScaledRTCurrentMSAASamples;

                if (sizeChanged || msaaSampleChanged)
                {
                    rt.Release();

                    if (enableMSAA)
                    {
                        rt.antiAliasing = (int)m_ScaledRTCurrentMSAASamples;
                    }

                    rt.width  = scaledSize.x;
                    rt.height = scaledSize.y;

                    rt.name = CoreUtils.GetRenderTargetAutoName(
                        rt.width,
                        rt.height,
                        rt.volumeDepth,
                        rt.format,
                        rth.m_Name,
                        mips: rt.useMipMap,
                        enableMSAA: enableMSAA,
                        msaaSamples: m_ScaledRTCurrentMSAASamples
                        );
                    rt.Create();
                }
            }
        }
        void Resize(int width, int height, RTCategory category, MSAASamples msaaSamples)
        {
            m_MaxWidths[(int)category]   = width;
            m_MaxHeights[(int)category]  = height;
            m_ScaledRTCurrentMSAASamples = msaaSamples;

            var maxSize = new Vector2Int(width, height);

            m_ScaledRTCurrentCategory = category;

            Array.Resize(ref m_AutoSizedRTsArray, m_AutoSizedRTs.Count);
            m_AutoSizedRTs.CopyTo(m_AutoSizedRTsArray);
            for (int i = 0, c = m_AutoSizedRTsArray.Length; i < c; ++i)
            {
                var rth = m_AutoSizedRTsArray[i];
                rth.referenceSize = maxSize;

                var rt = rth.m_RTs[(int)category];

                // This can happen if you create a RTH for MSAA. By default we only create the MSAA version of the target.
                // Missing version will be created when needed in the getter.
                if (rt != null)
                {
                    rt.Release();

                    var scaledSize = rth.GetScaledSize(maxSize);

                    rt.width  = Mathf.Max(scaledSize.x, 1);
                    rt.height = Mathf.Max(scaledSize.y, 1);

                    if (category == RTCategory.MSAA)
                    {
                        rt.antiAliasing = (int)m_ScaledRTCurrentMSAASamples;
                    }

                    rt.name = CoreUtils.GetRenderTargetAutoName(rt.width, rt.height, rt.volumeDepth, rt.format, rth.m_Name, mips: rt.useMipMap, enableMSAA: category == RTCategory.MSAA, msaaSamples: m_ScaledRTCurrentMSAASamples);
                    rt.Create();
                }
            }
        }
        // Internal function
        static RTHandle AllocAutoSizedRenderTexture(
            int width,
            int height,
            int slices,
            DepthBits depthBufferBits,
            RenderTextureFormat colorFormat,
            FilterMode filterMode,
            TextureWrapMode wrapMode,
            TextureDimension dimension,
            bool sRGB,
            bool enableRandomWrite,
            bool useMipMap,
            bool autoGenerateMips,
            int anisoLevel,
            float mipMapBias,
            bool enableMSAA,
            bool bindTextureMS,
            bool useDynamicScale,
            VRTextureUsage vrUsage,
            RenderTextureMemoryless memoryless,
            string name
            )
        {
            // Here user made a mistake in setting up msaa/bindMS, hence the warning
            if (!enableMSAA && bindTextureMS == true)
            {
                Debug.LogWarning("RTHandle allocated without MSAA but with bindMS set to true, forcing bindMS to false.");
                bindTextureMS = false;
            }

            bool allocForMSAA = s_ScaledRTSupportsMSAA ? enableMSAA : false;

            // Here we purposefully disable MSAA so we just force the bindMS param to false.
            if (!allocForMSAA)
            {
                bindTextureMS = false;
            }

            // MSAA Does not support random read/write.
            bool UAV = enableRandomWrite;

            if (allocForMSAA && (UAV == true))
            {
                Debug.LogWarning("RTHandle that is MSAA-enabled cannot allocate MSAA RT with 'enableRandomWrite = true'.");
                UAV = false;
            }

            int        msaaSamples = allocForMSAA ? (int)s_ScaledRTCurrentMSAASamples : 1;
            RTCategory category    = allocForMSAA ? RTCategory.MSAA : RTCategory.Regular;

            var rt = new RenderTexture(width, height, (int)depthBufferBits, colorFormat, sRGB ? RenderTextureReadWrite.sRGB : RenderTextureReadWrite.Linear)
            {
                hideFlags         = HideFlags.HideAndDontSave,
                volumeDepth       = slices,
                filterMode        = filterMode,
                wrapMode          = wrapMode,
                dimension         = dimension,
                enableRandomWrite = UAV,
                useMipMap         = useMipMap,
                autoGenerateMips  = autoGenerateMips,
                anisoLevel        = anisoLevel,
                mipMapBias        = mipMapBias,
                antiAliasing      = msaaSamples,
                bindTextureMS     = bindTextureMS,
                useDynamicScale   = useDynamicScale,
                vrUsage           = vrUsage,
                memorylessMode    = memoryless,
                name = CoreUtils.GetRenderTargetAutoName(width, height, colorFormat, name, mips: useMipMap, enableMSAA: allocForMSAA, msaaSamples: s_ScaledRTCurrentMSAASamples)
            };

            rt.Create();

            RTHandle rth = new RTHandle();

            rth.SetRenderTexture(rt, category);
            rth.m_EnableMSAA        = enableMSAA;
            rth.m_EnableRandomWrite = enableRandomWrite;
            rth.useScaling          = true;
            rth.m_Name = name;
            s_AutoSizedRTs.Add(rth);
            return(rth);
        }
        // This method wraps around regular RenderTexture creation.
        // There is no specific logic applied to RenderTextures created this way.
        public static RTHandle Alloc(
            int width,
            int height,
            int slices = 1,
            DepthBits depthBufferBits       = DepthBits.None,
            RenderTextureFormat colorFormat = RenderTextureFormat.Default,
            FilterMode filterMode           = FilterMode.Point,
            TextureWrapMode wrapMode        = TextureWrapMode.Repeat,
            TextureDimension dimension      = TextureDimension.Tex2D,
            bool sRGB = true,
            bool enableRandomWrite             = false,
            bool useMipMap                     = false,
            bool autoGenerateMips              = true,
            int anisoLevel                     = 1,
            float mipMapBias                   = 0f,
            MSAASamples msaaSamples            = MSAASamples.None,
            bool bindTextureMS                 = false,
            bool useDynamicScale               = false,
            VRTextureUsage vrUsage             = VRTextureUsage.None,
            RenderTextureMemoryless memoryless = RenderTextureMemoryless.None,
            string name = ""
            )
        {
            bool enableMSAA = msaaSamples != MSAASamples.None;

            if (!enableMSAA && bindTextureMS == true)
            {
                Debug.LogWarning("RTHandle allocated without MSAA but with bindMS set to true, forcing bindMS to false.");
                bindTextureMS = false;
            }

            var rt = new RenderTexture(width, height, (int)depthBufferBits, colorFormat, sRGB ? RenderTextureReadWrite.sRGB : RenderTextureReadWrite.Linear)
            {
                hideFlags         = HideFlags.HideAndDontSave,
                volumeDepth       = slices,
                filterMode        = filterMode,
                wrapMode          = wrapMode,
                dimension         = dimension,
                enableRandomWrite = enableRandomWrite,
                useMipMap         = useMipMap,
                autoGenerateMips  = autoGenerateMips,
                anisoLevel        = anisoLevel,
                mipMapBias        = mipMapBias,
                antiAliasing      = (int)msaaSamples,
                bindTextureMS     = bindTextureMS,
                useDynamicScale   = useDynamicScale,
                vrUsage           = vrUsage,
                memorylessMode    = memoryless,
                name = CoreUtils.GetRenderTargetAutoName(width, height, colorFormat, name, mips: useMipMap, enableMSAA: enableMSAA, msaaSamples: msaaSamples)
            };

            rt.Create();

            RTCategory category = enableMSAA ? RTCategory.MSAA : RTCategory.Regular;
            var        newRT    = new RTHandle();

            newRT.SetRenderTexture(rt, category);
            newRT.useScaling          = false;
            newRT.m_EnableRandomWrite = enableRandomWrite;
            newRT.m_EnableMSAA        = enableMSAA;
            newRT.m_Name = name;
            return(newRT);
        }
Beispiel #10
0
        // Internal function
        RTHandle AllocAutoSizedRenderTexture(
            int width,
            int height,
            int slices,
            DepthBits depthBufferBits,
            GraphicsFormat colorFormat,
            FilterMode filterMode,
            TextureWrapMode wrapMode,
            TextureDimension dimension,
            bool enableRandomWrite,
            bool useMipMap,
            bool autoGenerateMips,
            bool isShadowMap,
            int anisoLevel,
            float mipMapBias,
            bool enableMSAA,
            bool bindTextureMS,
            bool useDynamicScale,
            RenderTextureMemoryless memoryless,
            string name
            )
        {
            // Here user made a mistake in setting up msaa/bindMS, hence the warning
            if (!enableMSAA && bindTextureMS == true)
            {
                Debug.LogWarning("RTHandle allocated without MSAA but with bindMS set to true, forcing bindMS to false.");
                bindTextureMS = false;
            }

            bool allocForMSAA = m_ScaledRTSupportsMSAA ? enableMSAA : false;

            // Here we purposefully disable MSAA so we just force the bindMS param to false.
            if (!allocForMSAA)
            {
                bindTextureMS = false;
            }

            // MSAA Does not support random read/write.
            bool UAV = enableRandomWrite;

            if (allocForMSAA && (UAV == true))
            {
                Debug.LogWarning("RTHandle that is MSAA-enabled cannot allocate MSAA RT with 'enableRandomWrite = true'.");
                UAV = false;
            }

            int        msaaSamples = allocForMSAA ? (int)m_ScaledRTCurrentMSAASamples : 1;
            RTCategory category    = allocForMSAA ? RTCategory.MSAA : RTCategory.Regular;

            // We need to handle this in an explicit way since GraphicsFormat does not expose depth formats. TODO: Get rid of this branch once GraphicsFormat'll expose depth related formats
            RenderTexture rt;

            if (isShadowMap || depthBufferBits != DepthBits.None)
            {
                RenderTextureFormat format = isShadowMap ? RenderTextureFormat.Shadowmap : RenderTextureFormat.Depth;
                rt = new RenderTexture(width, height, (int)depthBufferBits, format, RenderTextureReadWrite.Linear)
                {
                    hideFlags         = HideFlags.HideAndDontSave,
                    volumeDepth       = slices,
                    filterMode        = filterMode,
                    wrapMode          = wrapMode,
                    dimension         = dimension,
                    enableRandomWrite = UAV,
                    useMipMap         = useMipMap,
                    autoGenerateMips  = autoGenerateMips,
                    anisoLevel        = anisoLevel,
                    mipMapBias        = mipMapBias,
                    antiAliasing      = msaaSamples,
                    bindTextureMS     = bindTextureMS,
                    useDynamicScale   = m_HardwareDynamicResRequested && useDynamicScale,
                    memorylessMode    = memoryless,
                    name = CoreUtils.GetRenderTargetAutoName(width, height, slices, GraphicsFormatUtility.GetRenderTextureFormat(colorFormat), name, mips: useMipMap, enableMSAA: allocForMSAA, msaaSamples: m_ScaledRTCurrentMSAASamples)
                };
            }
            else
            {
                rt = new RenderTexture(width, height, (int)depthBufferBits, colorFormat)
                {
                    hideFlags         = HideFlags.HideAndDontSave,
                    volumeDepth       = slices,
                    filterMode        = filterMode,
                    wrapMode          = wrapMode,
                    dimension         = dimension,
                    enableRandomWrite = UAV,
                    useMipMap         = useMipMap,
                    autoGenerateMips  = autoGenerateMips,
                    anisoLevel        = anisoLevel,
                    mipMapBias        = mipMapBias,
                    antiAliasing      = msaaSamples,
                    bindTextureMS     = bindTextureMS,
                    useDynamicScale   = m_HardwareDynamicResRequested && useDynamicScale,
                    memorylessMode    = memoryless,
                    name = CoreUtils.GetRenderTargetAutoName(width, height, slices, GraphicsFormatUtility.GetRenderTextureFormat(colorFormat), name, mips: useMipMap, enableMSAA: allocForMSAA, msaaSamples: m_ScaledRTCurrentMSAASamples)
                };
            }

            rt.Create();

            var rth = new RTHandle(this);

            rth.SetRenderTexture(rt, category);
            rth.m_EnableMSAA           = enableMSAA;
            rth.m_EnableRandomWrite    = enableRandomWrite;
            rth.useScaling             = true;
            rth.m_EnableHWDynamicScale = useDynamicScale;
            rth.m_Name = name;
            m_AutoSizedRTs.Add(rth);
            return(rth);
        }
Beispiel #11
0
        // This method wraps around regular RenderTexture creation.
        // There is no specific logic applied to RenderTextures created this way.
        public RTHandle Alloc(
            int width,
            int height,
            int slices = 1,
            DepthBits depthBufferBits          = DepthBits.None,
            GraphicsFormat colorFormat         = GraphicsFormat.R8G8B8A8_SRGB,
            FilterMode filterMode              = FilterMode.Point,
            TextureWrapMode wrapMode           = TextureWrapMode.Repeat,
            TextureDimension dimension         = TextureDimension.Tex2D,
            bool enableRandomWrite             = false,
            bool useMipMap                     = false,
            bool autoGenerateMips              = true,
            bool isShadowMap                   = false,
            int anisoLevel                     = 1,
            float mipMapBias                   = 0f,
            MSAASamples msaaSamples            = MSAASamples.None,
            bool bindTextureMS                 = false,
            bool useDynamicScale               = false,
            RenderTextureMemoryless memoryless = RenderTextureMemoryless.None,
            string name = ""
            )
        {
            bool enableMSAA = msaaSamples != MSAASamples.None;

            if (!enableMSAA && bindTextureMS == true)
            {
                Debug.LogWarning("RTHandle allocated without MSAA but with bindMS set to true, forcing bindMS to false.");
                bindTextureMS = false;
            }

            // We need to handle this in an explicit way since GraphicsFormat does not expose depth formats. TODO: Get rid of this branch once GraphicsFormat'll expose depth related formats
            RenderTexture rt;

            if (isShadowMap || depthBufferBits != DepthBits.None)
            {
                RenderTextureFormat format = isShadowMap ? RenderTextureFormat.Shadowmap : RenderTextureFormat.Depth;
                rt = new RenderTexture(width, height, (int)depthBufferBits, format, RenderTextureReadWrite.Linear)
                {
                    hideFlags         = HideFlags.HideAndDontSave,
                    volumeDepth       = slices,
                    filterMode        = filterMode,
                    wrapMode          = wrapMode,
                    dimension         = dimension,
                    enableRandomWrite = enableRandomWrite,
                    useMipMap         = useMipMap,
                    autoGenerateMips  = autoGenerateMips,
                    anisoLevel        = anisoLevel,
                    mipMapBias        = mipMapBias,
                    antiAliasing      = (int)msaaSamples,
                    bindTextureMS     = bindTextureMS,
                    useDynamicScale   = m_HardwareDynamicResRequested && useDynamicScale,
                    memorylessMode    = memoryless,
                    name = CoreUtils.GetRenderTargetAutoName(width, height, slices, format, name, mips: useMipMap, enableMSAA: enableMSAA, msaaSamples: msaaSamples)
                };
            }
            else
            {
                rt = new RenderTexture(width, height, (int)depthBufferBits, colorFormat)
                {
                    hideFlags         = HideFlags.HideAndDontSave,
                    volumeDepth       = slices,
                    filterMode        = filterMode,
                    wrapMode          = wrapMode,
                    dimension         = dimension,
                    enableRandomWrite = enableRandomWrite,
                    useMipMap         = useMipMap,
                    autoGenerateMips  = autoGenerateMips,
                    anisoLevel        = anisoLevel,
                    mipMapBias        = mipMapBias,
                    antiAliasing      = (int)msaaSamples,
                    bindTextureMS     = bindTextureMS,
                    useDynamicScale   = m_HardwareDynamicResRequested && useDynamicScale,
                    memorylessMode    = memoryless,
                    name = CoreUtils.GetRenderTargetAutoName(width, height, slices, GraphicsFormatUtility.GetRenderTextureFormat(colorFormat), name, mips: useMipMap, enableMSAA: enableMSAA, msaaSamples: msaaSamples)
                };
            }

            rt.Create();

            RTCategory category = enableMSAA ? RTCategory.MSAA : RTCategory.Regular;
            var        newRT    = new RTHandle(this);

            newRT.SetRenderTexture(rt, category);
            newRT.useScaling             = false;
            newRT.m_EnableRandomWrite    = enableRandomWrite;
            newRT.m_EnableMSAA           = enableMSAA;
            newRT.m_EnableHWDynamicScale = useDynamicScale;
            newRT.m_Name = name;

            newRT.referenceSize = new Vector2Int(width, height);

            return(newRT);
        }