using System.Collections.Generic; using UnityEngine.Experimental.Rendering; using System; namespace UnityEngine.Rendering { class AtlasAllocator { private class AtlasNode { public AtlasNode m_RightChild = null; public AtlasNode m_BottomChild = null; public Vector4 m_Rect = new Vector4(0, 0, 0, 0); // x,y is width and height (scale) z,w offset into atlas (offset) public AtlasNode Allocate(ref ObjectPool pool, int width, int height, bool powerOfTwoPadding) { // not a leaf node, try children if (m_RightChild != null) { AtlasNode node = m_RightChild.Allocate(ref pool, width, height, powerOfTwoPadding); if (node == null) { node = m_BottomChild.Allocate(ref pool, width, height, powerOfTwoPadding); } return node; } int wPadd = 0; int hPadd = 0; if (powerOfTwoPadding) { wPadd = (int)m_Rect.x % width; hPadd = (int)m_Rect.y % height; } //leaf node, check for fit if ((width <= m_Rect.x - wPadd) && (height <= m_Rect.y - hPadd)) { // perform the split m_RightChild = pool.Get(); m_BottomChild = pool.Get(); m_Rect.z += wPadd; m_Rect.w += hPadd; m_Rect.x -= wPadd; m_Rect.y -= hPadd; if (width > height) // logic to decide which way to split { // +--------+------+ m_RightChild.m_Rect.z = m_Rect.z + width; // | | | m_RightChild.m_Rect.w = m_Rect.w; // +--------+------+ m_RightChild.m_Rect.x = m_Rect.x - width; // | | m_RightChild.m_Rect.y = height; // | | // +---------------+ m_BottomChild.m_Rect.z = m_Rect.z; m_BottomChild.m_Rect.w = m_Rect.w + height; m_BottomChild.m_Rect.x = m_Rect.x; m_BottomChild.m_Rect.y = m_Rect.y - height; } else { // +---+-----------+ m_RightChild.m_Rect.z = m_Rect.z + width; // | | | m_RightChild.m_Rect.w = m_Rect.w; // | | | m_RightChild.m_Rect.x = m_Rect.x - width; // +---+ + m_RightChild.m_Rect.y = m_Rect.y; // | | | // +---+-----------+ m_BottomChild.m_Rect.z = m_Rect.z; m_BottomChild.m_Rect.w = m_Rect.w + height; m_BottomChild.m_Rect.x = width; m_BottomChild.m_Rect.y = m_Rect.y - height; } m_Rect.x = width; m_Rect.y = height; return this; } return null; } public void Release(ref ObjectPool pool) { if (m_RightChild != null) { m_RightChild.Release(ref pool); m_BottomChild.Release(ref pool); pool.Release(m_RightChild); pool.Release(m_BottomChild); } m_RightChild = null; m_BottomChild = null; m_Rect = Vector4.zero; } } private AtlasNode m_Root; private int m_Width; private int m_Height; private bool powerOfTwoPadding; private ObjectPool m_NodePool; public AtlasAllocator(int width, int height, bool potPadding) { m_Root = new AtlasNode(); m_Root.m_Rect.Set(width, height, 0, 0); m_Width = width; m_Height = height; powerOfTwoPadding = potPadding; m_NodePool = new ObjectPool(_ => { }, _ => { }); } public bool Allocate(ref Vector4 result, int width, int height) { AtlasNode node = m_Root.Allocate(ref m_NodePool, width, height, powerOfTwoPadding); if (node != null) { result = node.m_Rect; return true; } else { result = Vector4.zero; return false; } } public void Reset() { m_Root.Release(ref m_NodePool); m_Root.m_Rect.Set(m_Width, m_Height, 0, 0); } } /// /// A generic Atlas texture of 2D textures. /// An atlas texture is a texture collection that collects multiple sub-textures into a single big texture. /// Sub-texture allocation for Texture2DAtlas is static and will not change after initial allocation. /// Does not add mipmap padding for sub-textures. /// public class Texture2DAtlas { private enum BlitType { Default, CubeTo2DOctahedral, SingleChannel, CubeTo2DOctahedralSingleChannel, } /// /// Texture is not on the GPU or is not up to date. /// private protected const int kGPUTexInvalid = 0; /// /// Texture Mip0 is on the GPU and up to date. /// private protected const int kGPUTexValidMip0 = 1; /// /// Texture and all mips are on the GPU and up to date. /// private protected const int kGPUTexValidMipAll = 2; /// /// The texture for the atlas. /// private protected RTHandle m_AtlasTexture = null; /// /// Width of the atlas. /// private protected int m_Width; /// /// Height of the atlas. /// private protected int m_Height; /// /// Format of the atlas. /// private protected GraphicsFormat m_Format; /// /// Atlas uses mip maps. /// private protected bool m_UseMipMaps; bool m_IsAtlasTextureOwner = false; private AtlasAllocator m_AtlasAllocator = null; private Dictionary m_AllocationCache = new Dictionary(); private Dictionary m_IsGPUTextureUpToDate = new Dictionary(); private Dictionary m_TextureHashes = new Dictionary(); static readonly Vector4 fullScaleOffset = new Vector4(1, 1, 0, 0); // Maximum mip padding that can be applied to the textures in the atlas (1 << 10 = 1024 pixels) static readonly int s_MaxMipLevelPadding = 10; /// /// Maximum mip padding (pow2) that can be applied to the textures in the atlas /// public static int maxMipLevelPadding => s_MaxMipLevelPadding; /// /// Handle to the texture of the atlas. /// public RTHandle AtlasTexture { get { return m_AtlasTexture; } } /// /// Creates a new empty texture atlas. /// /// Width of the atlas in pixels. /// Height of atlas in pixels. /// GraphicsFormat of the atlas. /// Filtering mode of the atlas. /// Power of two padding. /// Name of the atlas /// Use mip maps public Texture2DAtlas(int width, int height, GraphicsFormat format, FilterMode filterMode = FilterMode.Point, bool powerOfTwoPadding = false, string name = "", bool useMipMap = true) { m_Width = width; m_Height = height; m_Format = format; m_UseMipMaps = useMipMap; m_AtlasTexture = RTHandles.Alloc( width: m_Width, height: m_Height, format: m_Format, filterMode: filterMode, wrapMode: TextureWrapMode.Clamp, useMipMap: useMipMap, autoGenerateMips: false, name: name ); m_IsAtlasTextureOwner = true; // We clear on create to avoid garbage data to be present in the atlas int mipCount = useMipMap ? GetTextureMipmapCount(m_Width, m_Height) : 1; for (int mipIdx = 0; mipIdx < mipCount; ++mipIdx) { Graphics.SetRenderTarget(m_AtlasTexture, mipIdx); GL.Clear(false, true, Color.clear); } m_AtlasAllocator = new AtlasAllocator(width, height, powerOfTwoPadding); } /// /// Release atlas resources. /// public void Release() { ResetAllocator(); if (m_IsAtlasTextureOwner) { RTHandles.Release(m_AtlasTexture); } } /// /// Clear atlas sub-texture allocations. /// public void ResetAllocator() { m_AtlasAllocator.Reset(); m_AllocationCache.Clear(); m_IsGPUTextureUpToDate.Clear(); // mark all GPU textures as invalid. } /// /// Clear atlas texture. /// /// Target command buffer for graphics commands. public void ClearTarget(CommandBuffer cmd) { int mipCount = (m_UseMipMaps) ? GetTextureMipmapCount(m_Width, m_Height) : 1; // clear the atlas by blitting a black texture at every mips for (int mipLevel = 0; mipLevel < mipCount; mipLevel++) { cmd.SetRenderTarget(m_AtlasTexture, mipLevel); Blitter.BlitQuad(cmd, Texture2D.blackTexture, fullScaleOffset, fullScaleOffset, mipLevel, true); } m_IsGPUTextureUpToDate.Clear(); // mark all GPU textures as invalid. } /// /// Return texture mip map count based on the width and height. /// /// The texture width in pixels. /// The texture height in pixels. /// The number of mip maps. private protected int GetTextureMipmapCount(int width, int height) { if (!m_UseMipMaps) return 1; // We don't care about the real mipmap count in the texture because they are generated by the atlas float maxSize = Mathf.Max(width, height); return CoreUtils.GetMipCount(maxSize); } /// /// Test if a texture is a 2D texture. /// /// Source texture. /// True if texture is 2D, false otherwise. private protected bool Is2D(Texture texture) { RenderTexture rt = texture as RenderTexture; return (texture is Texture2D || rt?.dimension == TextureDimension.Tex2D); } /// /// Checks if single/multi/single channel format conversion is required. /// /// Blit source texture /// Blit destination texture /// true on single channel conversion false otherwise private protected bool IsSingleChannelBlit(Texture source, Texture destination) { var srcCount = GraphicsFormatUtility.GetComponentCount(source.graphicsFormat); var dstCount = GraphicsFormatUtility.GetComponentCount(destination.graphicsFormat); if (srcCount == 1 || dstCount == 1) { // One to many, many to one if (srcCount != dstCount) return true; // Single channel swizzle var srcSwizzle = ((1 << ((int)GraphicsFormatUtility.GetSwizzleA(source.graphicsFormat) & 0x7)) << 24) | ((1 << ((int)GraphicsFormatUtility.GetSwizzleB(source.graphicsFormat) & 0x7)) << 16) | ((1 << ((int)GraphicsFormatUtility.GetSwizzleG(source.graphicsFormat) & 0x7)) << 8) | ((1 << ((int)GraphicsFormatUtility.GetSwizzleR(source.graphicsFormat) & 0x7))); var dstSwizzle = ((1 << ((int)GraphicsFormatUtility.GetSwizzleA(destination.graphicsFormat) & 0x7)) << 24) | ((1 << ((int)GraphicsFormatUtility.GetSwizzleB(destination.graphicsFormat) & 0x7)) << 16) | ((1 << ((int)GraphicsFormatUtility.GetSwizzleG(destination.graphicsFormat) & 0x7)) << 8) | ((1 << ((int)GraphicsFormatUtility.GetSwizzleR(destination.graphicsFormat) & 0x7))); if (srcSwizzle != dstSwizzle) return true; } return false; } private void Blit2DTexture(CommandBuffer cmd, Vector4 scaleOffset, Texture texture, Vector4 sourceScaleOffset, bool blitMips, BlitType blitType) { int mipCount = GetTextureMipmapCount(texture.width, texture.height); if (!blitMips) mipCount = 1; for (int mipLevel = 0; mipLevel < mipCount; mipLevel++) { cmd.SetRenderTarget(m_AtlasTexture, mipLevel); switch (blitType) { case BlitType.Default: Blitter.BlitQuad(cmd, texture, sourceScaleOffset, scaleOffset, mipLevel, true); break; case BlitType.CubeTo2DOctahedral: Blitter.BlitCubeToOctahedral2DQuad(cmd, texture, scaleOffset, mipLevel); break; case BlitType.SingleChannel: Blitter.BlitQuadSingleChannel(cmd, texture, sourceScaleOffset, scaleOffset, mipLevel); break; case BlitType.CubeTo2DOctahedralSingleChannel: Blitter.BlitCubeToOctahedral2DQuadSingleChannel(cmd, texture, scaleOffset, mipLevel); break; } } } /// /// Mark texture valid on the GPU. /// /// Texture instance ID. /// Texture has valid mip maps. private protected void MarkGPUTextureValid(int instanceId, bool mipAreValid = false) { m_IsGPUTextureUpToDate[instanceId] = (mipAreValid) ? kGPUTexValidMipAll : kGPUTexValidMip0; } /// /// Mark texture invalid on the GPU. /// /// Texture instance ID. private protected void MarkGPUTextureInvalid(int instanceId) => m_IsGPUTextureUpToDate[instanceId] = kGPUTexInvalid; /// /// Blit 2D texture into the atlas. /// /// Target command buffer for graphics commands. /// Destination scale (.xy) and offset (.zw) /// Source Texture /// Source scale (.xy) and offset(.zw). /// Blit mip maps. /// Override texture instance ID. public virtual void BlitTexture(CommandBuffer cmd, Vector4 scaleOffset, Texture texture, Vector4 sourceScaleOffset, bool blitMips = true, int overrideInstanceID = -1) { // This atlas only support 2D texture so we only blit 2D textures if (Is2D(texture)) { BlitType blitType = BlitType.Default; if (IsSingleChannelBlit(texture, m_AtlasTexture.m_RT)) blitType = BlitType.SingleChannel; Blit2DTexture(cmd, scaleOffset, texture, sourceScaleOffset, blitMips, blitType); var instanceID = overrideInstanceID != -1 ? overrideInstanceID : GetTextureID(texture); MarkGPUTextureValid(instanceID, blitMips); m_TextureHashes[instanceID] = CoreUtils.GetTextureHash(texture); } } /// /// Blit octahedral texture into the atlas. /// /// Target command buffer for graphics commands. /// Destination scale (.xy) and offset (.zw) /// Source Texture /// Source scale (.xy) and offset(.zw). /// Blit mip maps. /// Override texture instance ID. public virtual void BlitOctahedralTexture(CommandBuffer cmd, Vector4 scaleOffset, Texture texture, Vector4 sourceScaleOffset, bool blitMips = true, int overrideInstanceID = -1) { // Default implementation. No padding in Texture2DAtlas, no need to handle specially. BlitTexture(cmd, scaleOffset, texture, sourceScaleOffset, blitMips, overrideInstanceID); } /// /// Blit and project Cube texture into a 2D texture in the atlas. /// /// Target command buffer for graphics commands. /// Destination scale (.xy) and offset (.zw) /// Source Texture /// Blit mip maps. /// Override texture instance ID. public virtual void BlitCubeTexture2D(CommandBuffer cmd, Vector4 scaleOffset, Texture texture, bool blitMips = true, int overrideInstanceID = -1) { Debug.Assert(texture.dimension == TextureDimension.Cube); // This atlas only support 2D texture so we map Cube into set of 2D textures if (texture.dimension == TextureDimension.Cube) { BlitType blitType = BlitType.CubeTo2DOctahedral; if (IsSingleChannelBlit(texture, m_AtlasTexture.m_RT)) blitType = BlitType.CubeTo2DOctahedralSingleChannel; // By default blit cube into a single octahedral 2D texture quad Blit2DTexture(cmd, scaleOffset, texture, new Vector4(1.0f, 1.0f, 0.0f, 0.0f), blitMips, blitType); var instanceID = overrideInstanceID != -1 ? overrideInstanceID : GetTextureID(texture); MarkGPUTextureValid(instanceID, blitMips); m_TextureHashes[instanceID] = CoreUtils.GetTextureHash(texture); } } /// /// Allocate space from the atlas for a texture and copy texture contents into the atlas. /// /// Target command buffer for graphics commands. /// Destination scale (.xy) and offset (.zw) /// Source Texture /// Request width in pixels. /// Request height in pixels. /// Override texture instance ID. /// True if the texture was successfully allocated and copied; false otherwise. public virtual bool AllocateTexture(CommandBuffer cmd, ref Vector4 scaleOffset, Texture texture, int width, int height, int overrideInstanceID = -1) { var instanceID = overrideInstanceID != -1 ? overrideInstanceID : GetTextureID(texture); bool allocated = AllocateTextureWithoutBlit(instanceID, width, height, ref scaleOffset); if (allocated) { if (Is2D(texture)) BlitTexture(cmd, scaleOffset, texture, fullScaleOffset); else BlitCubeTexture2D(cmd, scaleOffset, texture, true); // texture is up to date MarkGPUTextureValid(instanceID, true); m_TextureHashes[instanceID] = CoreUtils.GetTextureHash(texture); } return allocated; } /// /// Allocate space from the atlas for a texture. /// /// Source texture. /// Request width in pixels. /// Request height in pixels. /// Allocated scale (.xy) and offset (.zw). /// True on success, false otherwise. public bool AllocateTextureWithoutBlit(Texture texture, int width, int height, ref Vector4 scaleOffset) => AllocateTextureWithoutBlit(texture.GetInstanceID(), width, height, ref scaleOffset); /// /// Allocate space from the atlas for a texture. /// /// Source texture instance ID. /// Request width in pixels. /// Request height in pixels. /// Allocated scale (.xy) and offset (.zw). /// True on success, false otherwise. public virtual bool AllocateTextureWithoutBlit(int instanceId, int width, int height, ref Vector4 scaleOffset) { scaleOffset = Vector4.zero; if (m_AtlasAllocator.Allocate(ref scaleOffset, width, height)) { scaleOffset.Scale(new Vector4(1.0f / m_Width, 1.0f / m_Height, 1.0f / m_Width, 1.0f / m_Height)); m_AllocationCache[instanceId] = (scaleOffset, new Vector2Int(width, height)); MarkGPUTextureInvalid(instanceId); // the texture data haven't been uploaded m_TextureHashes[instanceId] = -1; return true; } else { return false; } } /// /// Compute hash from texture properties. /// /// Source texture A. /// Source texture B. /// Hash of texture porperties. private protected int GetTextureHash(Texture textureA, Texture textureB) { int hash = CoreUtils.GetTextureHash(textureA) + 23 * CoreUtils.GetTextureHash(textureB); return hash; } /// /// Get sub-texture ID for the atlas. /// /// Source texture. /// Texture instance ID. public int GetTextureID(Texture texture) { return texture.GetInstanceID(); } /// /// Get sub-texture ID for the atlas. /// /// Source texture A. /// Source texture B. /// Combined texture instance ID. public int GetTextureID(Texture textureA, Texture textureB) { return GetTextureID(textureA) + 23 * GetTextureID(textureB); } /// /// Check if the atlas contains the textures. /// /// Texture scale (.xy) and offset (.zw). /// Source texture A. /// Source texture B. /// True if the texture is in the atlas, false otherwise. public bool IsCached(out Vector4 scaleOffset, Texture textureA, Texture textureB) => IsCached(out scaleOffset, GetTextureID(textureA, textureB)); /// /// Check if the atlas contains the textures. /// /// Texture scale (.xy) and offset (.zw). /// Source texture /// True if the texture is in the atlas, false otherwise. public bool IsCached(out Vector4 scaleOffset, Texture texture) => IsCached(out scaleOffset, GetTextureID(texture)); /// /// Check if the atlas contains the texture. /// /// Texture scale (.xy) and offset (.zw). /// Source texture instance ID. /// True if the texture is in the atlas, false otherwise public bool IsCached(out Vector4 scaleOffset, int id) { bool cached = m_AllocationCache.TryGetValue(id, out var value); scaleOffset = value.scaleOffset; return cached; } /// /// Get cached texture size. /// /// Source texture instance ID. /// Texture size. internal Vector2Int GetCachedTextureSize(int id) { m_AllocationCache.TryGetValue(id, out var value); return value.size; } /// /// Check if contents of a texture needs to be updated in the atlas. /// /// Source texture. /// Texture uses mips. /// True if texture needs update, false otherwise. public virtual bool NeedsUpdate(Texture texture, bool needMips = false) { RenderTexture rt = texture as RenderTexture; int key = GetTextureID(texture); int textureHash = CoreUtils.GetTextureHash(texture); // Update the render texture if needed if (rt != null) { int updateCount; if (m_IsGPUTextureUpToDate.TryGetValue(key, out updateCount)) { if (rt.updateCount != updateCount) { m_IsGPUTextureUpToDate[key] = (int)rt.updateCount; return true; } } else { m_IsGPUTextureUpToDate[key] = (int)rt.updateCount; } } // In case the texture settings/import settings have changed, we need to update it else if (m_TextureHashes.TryGetValue(key, out int hash) && hash != textureHash) { m_TextureHashes[key] = textureHash; return true; } // For regular textures, values == 0 means that their GPU data needs to be updated (either because // the atlas have been re-layouted or the texture have never been uploaded. We also check if the mips // are valid for the texture if we need them else if (m_IsGPUTextureUpToDate.TryGetValue(key, out var value)) return value == kGPUTexInvalid || (needMips && value == kGPUTexValidMip0); return false; } /// /// Check if a slot needs to be updated in the atlas. /// /// The id. /// The update count. /// Texture uses mips. /// True if slot needs update, false otherwise. public virtual bool NeedsUpdate(int id, int updateCount, bool needMips = false) { int atlasUpdateCount; if (m_IsGPUTextureUpToDate.TryGetValue(id, out atlasUpdateCount)) { if (updateCount != atlasUpdateCount) { m_IsGPUTextureUpToDate[id] = updateCount; return true; } } else { m_IsGPUTextureUpToDate[id] = updateCount; } return false; } /// /// Check if contents of a texture needs to be updated in the atlas. /// /// Source texture A. /// Source texture B. /// Texture uses mips. /// True if texture needs update, false otherwise. public virtual bool NeedsUpdate(Texture textureA, Texture textureB, bool needMips = false) { RenderTexture rtA = textureA as RenderTexture; RenderTexture rtB = textureB as RenderTexture; int key = GetTextureID(textureA, textureB); int textureHash = GetTextureHash(textureA, textureB); // Update the render texture if needed if (rtA != null || rtB != null) { int updateCount; if (m_IsGPUTextureUpToDate.TryGetValue(key, out updateCount)) { if (rtA != null && rtB != null && Math.Min(rtA.updateCount, rtB.updateCount) != updateCount) { m_IsGPUTextureUpToDate[key] = (int)Math.Min(rtA.updateCount, rtB.updateCount); return true; } else if (rtA != null && rtA.updateCount != updateCount) { m_IsGPUTextureUpToDate[key] = (int)rtA.updateCount; return true; } else if (rtB != null && rtB.updateCount != updateCount) { m_IsGPUTextureUpToDate[key] = (int)rtB.updateCount; return true; } } else { m_IsGPUTextureUpToDate[key] = textureHash; } } // In case the texture settings/import settings have changed, we need to update it else if (m_TextureHashes.TryGetValue(key, out int hash) && hash != textureHash) { m_TextureHashes[key] = key; return true; } // For regular textures, values == 0 means that their GPU data needs to be updated (either because // the atlas have been re-layouted or the texture have never been uploaded. We also check if the mips // are valid for the texture if we need them else if (m_IsGPUTextureUpToDate.TryGetValue(key, out var value)) return value == kGPUTexInvalid || (needMips && value == kGPUTexValidMip0); return false; } /// /// Add a texture into the atlas. /// /// Command buffer used for texture copy. /// Sub-texture rectangle for the added texture. Scale in .xy, offset int .zw /// The texture to be added. /// True if the atlas contains the texture, false otherwise. public virtual bool AddTexture(CommandBuffer cmd, ref Vector4 scaleOffset, Texture texture) { if (IsCached(out scaleOffset, texture)) return true; return AllocateTexture(cmd, ref scaleOffset, texture, texture.width, texture.height); } /// /// Update a texture in the atlas. /// /// Target command buffer for graphics commands. /// Texture in atlas. /// Replacement source texture. /// Destination scale (.xy) and offset (.zw) /// Source scale (.xy) and offset(.zw). /// Enable texture blit. /// Blit mip maps. /// True on success, false otherwise. public virtual bool UpdateTexture(CommandBuffer cmd, Texture oldTexture, Texture newTexture, ref Vector4 scaleOffset, Vector4 sourceScaleOffset, bool updateIfNeeded = true, bool blitMips = true) { // In case the old texture is here, we Blit the new one at the scale offset of the old one if (IsCached(out scaleOffset, oldTexture)) { if (updateIfNeeded && NeedsUpdate(newTexture)) { if (Is2D(newTexture)) BlitTexture(cmd, scaleOffset, newTexture, sourceScaleOffset, blitMips); else BlitCubeTexture2D(cmd, scaleOffset, newTexture, blitMips); MarkGPUTextureValid(GetTextureID(newTexture), blitMips); // texture is up to date } return true; } else // else we try to allocate the updated texture { return AllocateTexture(cmd, ref scaleOffset, newTexture, newTexture.width, newTexture.height); } } /// /// Update a texture in the atlas. /// /// Target command buffer for graphics commands. /// Texture in atlas. /// Destination scale (.xy) and offset (.zw) /// Enable texture blit. /// Blit mip maps. /// True on success, false otherwise. public virtual bool UpdateTexture(CommandBuffer cmd, Texture texture, ref Vector4 scaleOffset, bool updateIfNeeded = true, bool blitMips = true) => UpdateTexture(cmd, texture, texture, ref scaleOffset, fullScaleOffset, updateIfNeeded, blitMips); internal bool EnsureTextureSlot(out bool isUploadNeeded, ref Vector4 scaleBias, int key, int width, int height) { isUploadNeeded = false; if (m_AllocationCache.TryGetValue(key, out var value)) { scaleBias = value.scaleOffset; return true; } if (!m_AtlasAllocator.Allocate(ref scaleBias, width, height)) return false; isUploadNeeded = true; scaleBias.Scale(new Vector4(1.0f / m_Width, 1.0f / m_Height, 1.0f / m_Width, 1.0f / m_Height)); m_AllocationCache.Add(key, (scaleBias, new Vector2Int(width, height))); return true; } } }