예제 #1
0
        VectorImageRenderInfo Register(VectorImage vi, VisualElement context)
        {
            s_MarkerRegister.Begin();

            VectorImageRenderInfo renderInfo = m_RenderInfoPool.Get();

            renderInfo.useCount = 1;
            m_Registered[vi]    = renderInfo;

            if (vi.settings?.Length > 0)
            {
                // We first attempt to allocate into the gradient settings atlas since it supports deallocation.
                int   gradientCount = vi.settings.Length;
                Alloc alloc         = m_GradientSettingsAtlas.Add(gradientCount);
                if (alloc.size > 0)
                {
                    // Then attempt to allocate in the texture atlas.
                    // TODO: Once the atlas actually processes returns, we should call it at some point.
                    if (m_Atlas.TryGetAtlas(context, vi.atlas, out TextureId atlasId, out RectInt uvs))
                    {
                        // Remap.
                        GradientRemap previous = null;
                        for (int i = 0; i < gradientCount; ++i)
                        {
                            // Chain.
                            GradientRemap current = m_GradientRemapPool.Get();
                            if (i > 0)
                            {
                                previous.next = current;
                            }
                            else
                            {
                                renderInfo.firstGradientRemap = current;
                            }
                            previous = current;

                            // Remap the index.
                            current.origIndex = i;
                            current.destIndex = (int)alloc.start + i;

                            // Remap the rect.
                            GradientSettings gradient = vi.settings[i];
                            RectInt          location = gradient.location;
                            location.x      += uvs.x;
                            location.y      += uvs.y;
                            current.location = location;
                            current.atlas    = atlasId;
                        }

                        // Write into the previously allocated gradient settings now that we are sure to use it.
                        m_GradientSettingsAtlas.Write(alloc, vi.settings, renderInfo.firstGradientRemap);
                    }
                    else
                    {
                        // If the texture atlas didn't fit, keep it as a standalone custom texture, only need to remap the setting indices
                        GradientRemap previous = null;
                        for (int i = 0; i < gradientCount; ++i)
                        {
                            GradientRemap current = m_GradientRemapPool.Get();
                            if (i > 0)
                            {
                                previous.next = current;
                            }
                            else
                            {
                                renderInfo.firstGradientRemap = current;
                            }
                            previous = current;

                            current.origIndex = i;
                            current.destIndex = (int)alloc.start + i;
                            current.atlas     = TextureId.invalid;
                        }

                        m_GradientSettingsAtlas.Write(alloc, vi.settings, null);
                    }
                }
                else
                {
                    if (!m_LoggedExhaustedSettingsAtlas)
                    {
                        Debug.LogError("Exhausted max gradient settings (" + m_GradientSettingsAtlas.length + ") for atlas: " + m_GradientSettingsAtlas.atlas?.name);
                        m_LoggedExhaustedSettingsAtlas = true;
                    }
                }
            }
예제 #2
0
        VectorImageRenderInfo Register(VectorImage vi)
        {
            k_RegisterSampler.Begin();

            VectorImageRenderInfo renderInfo = m_RenderInfoPool.Get();

            renderInfo.useCount = 1;
            m_Registered[vi]    = renderInfo;

            if (vi.settings?.Length > 0)
            {
                // We first attempt to allocate into the gradient settings atlas since it supports deallocation.
                int   gradientCount = vi.settings.Length;
                Alloc alloc         = m_GradientSettingsAtlas.Add(gradientCount);
                if (alloc.size > 0)
                {
                    // Then attempt to allocate in the texture atlas.
                    RectInt uvs;
                    if (m_AtlasManager.TryGetLocation(vi.atlas, out uvs))
                    {
                        // Remap.
                        GradientRemap previous = null;
                        for (int i = 0; i < gradientCount; ++i)
                        {
                            // Chain.
                            GradientRemap current = m_GradientRemapPool.Get();
                            if (i > 0)
                            {
                                previous.next = current;
                            }
                            else
                            {
                                renderInfo.firstGradientRemap = current;
                            }
                            previous = current;

                            // Remap the index.
                            current.origIndex = i;
                            current.destIndex = (int)alloc.start + i;

                            // Remap the rect.
                            GradientSettings gradient = vi.settings[i];
                            RectInt          location = gradient.location;
                            location.x        += uvs.x;
                            location.y        += uvs.y;
                            current.location   = location;
                            current.isAtlassed = true;
                        }

                        // Write into the previously allocated gradient settings now that we are sure to use it.
                        m_GradientSettingsAtlas.Write(alloc, vi.settings, renderInfo.firstGradientRemap);
                    }
                    else
                    {
                        // If the texture atlas didn't fit, keep it as a standalone custom texture, only need to remap the setting indices
                        GradientRemap previous = null;
                        for (int i = 0; i < gradientCount; ++i)
                        {
                            GradientRemap current = m_GradientRemapPool.Get();
                            if (i > 0)
                            {
                                previous.next = current;
                            }
                            else
                            {
                                renderInfo.firstGradientRemap = current;
                            }
                            previous = current;

                            current.origIndex  = i;
                            current.destIndex  = (int)alloc.start + i;
                            current.isAtlassed = false;
                        }

                        m_GradientSettingsAtlas.Write(alloc, vi.settings, null);
                    }
                }
                else
                {
                    if (!m_LoggedExhaustedSettingsAtlas)
                    {
                        Debug.LogError("Exhausted max gradient settings (" + m_GradientSettingsAtlas.length + ") for atlas: " + m_GradientSettingsAtlas.atlas?.name);
                        m_LoggedExhaustedSettingsAtlas = true;
                    }
                }
            }

            k_RegisterSampler.End();

            return(renderInfo);
        }