// 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, bool xrInstancing, 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; // XR override for instancing support VRTextureUsage vrUsage = TextureXR.OverrideRenderTexture(xrInstancing, ref dimension, ref slices); // 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, vrUsage = vrUsage, 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, vrUsage = vrUsage, 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); }
// 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, bool xrInstancing = 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; } // XR override for instancing support VRTextureUsage vrUsage = TextureXR.OverrideRenderTexture(xrInstancing, ref dimension, ref slices); // 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, vrUsage = vrUsage, 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, vrUsage = vrUsage, 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); }