Exemple #1
0
        public bool CheckConsistency(string[] inputFiles)
        {
            MyRenderProxy.Assert(inputFiles.Length != 0);
            MyFileTextureManager texManager       = MyManagers.FileTextures;
            ISrvBindable         firstSrvBindable = texManager.GetTexture(inputFiles[0], MyFileTextureEnum.GPUPARTICLES, true);
            Texture2D            firstTex2D       = firstSrvBindable.Resource as Texture2D;

            if (firstTex2D == null)
            {
                return(false);
            }
            for (int i = 1; i < inputFiles.Length; i++)
            {
                ISrvBindable srvBindable = texManager.GetTexture(inputFiles[i], MyFileTextureEnum.GPUPARTICLES, true);
                Texture2D    tex2D       = srvBindable.Resource as Texture2D;
                if (tex2D == null)
                {
                    return(false);
                }
                bool consistent = MyResourceUtils.CheckTexturesConsistency(firstTex2D.Description, tex2D.Description);

                if (!consistent)
                {
                    return(false);
                }
            }
            return(true);
        }
Exemple #2
0
        /// <remarks>On big loops, or whenever recommendable, cache the returned reference</remarks>
        public ITexture GetTexture(string name, MyFileTextureEnum type, bool waitTillLoaded = false, bool skipQualityReduction = false)
        {
            if (name == null || name.Length <= 0)
            {
                return(ReturnDefaultTexture(type));
            }

            Uri uri;

            if (!MyResourceUtils.NormalizeFileTextureName(ref name, out uri))
            {
                IGeneratedTexture texture;
                if (m_generatedTextures.TryGetValue(name, out texture))
                {
                    return(texture);
                }
                else
                {
                    MyRenderProxy.Assert(false, "Can't find generated texture with name \"" + name + "\"");
                    return(ReturnDefaultTexture(type));
                }
            }

            MyFileTexture texOut;

            if (!m_textures.TryGetValue(name, out texOut))
            {
                if (uri.Scheme != FILE_SCHEME)
                {
                    Debug.Assert(false, "Cannot initialize a non file texture");
                    return(ReturnDefaultTexture(type));
                }

                m_texturesPool.AllocateOrCreate(out texOut);
                texOut.Init(name, uri.LocalPath, type, waitTillLoaded, skipQualityReduction);
                m_textures.Add(name, texOut);
            }

            switch (texOut.TextureState)
            {
            case FileTextureState.Unloaded:
            case FileTextureState.Requested:
                if (waitTillLoaded)
                {
                    LoadInternal(name);
                }
                else
                {
                    texOut.TextureState = FileTextureState.Requested;
                    m_requestedTextures.Add(name);
                }
                break;

            case FileTextureState.Loaded:
                break;
            }

            return(texOut);
        }
Exemple #3
0
        public bool TryGetTexture(string name, out IUserGeneratedTexture texture)
        {
            Uri uri;

            if (MyResourceUtils.NormalizeFileTextureName(ref name, out uri))
            {
                texture = null;
                return(false);
            }

            IGeneratedTexture generatedTexture;

            m_generatedTextures.TryGetValue(name, out generatedTexture);
            texture = generatedTexture as IUserGeneratedTexture;
            return(texture != null);
        }
Exemple #4
0
        public bool TryGetTexture(string name, out ITexture texture)
        {
            Uri  uri;
            bool found;

            if (!MyResourceUtils.NormalizeFileTextureName(ref name, out uri))
            {
                IGeneratedTexture generatedTexture;
                found   = m_generatedTextures.TryGetValue(name, out generatedTexture);
                texture = generatedTexture;
                return(found);
            }

            MyFileTexture fileTexture;

            found   = m_textures.TryGetValue(name, out fileTexture);
            texture = fileTexture;
            return(found);
        }
Exemple #5
0
        void DisposeTexInternal(string name, bool alterLoaded = true, bool ignoreFailure = false)
        {
            if (!MyResourceUtils.NormalizeFileTextureName(ref name))
            {
                IGeneratedTexture texture;
                if (m_generatedTextures.TryGetValue(name, out texture))
                {
                    IUserGeneratedTexture userTexture = texture as IUserGeneratedTexture;
                    if (userTexture == null)
                    {
                        MyRenderProxy.Assert(false, "Can't dispose system texture");
                    }
                    else
                    {
                        MyManagers.GeneratedTextures.DisposeTex(userTexture);
                    }

                    return;
                }
                else
                {
                    MyRenderProxy.Assert(false, "Can't find generated texture with name \"" + name + "\"");
                    return;
                }
            }

            MyRenderProxy.Assert(m_textures.ContainsKey(name) || ignoreFailure, "The texture has not been created by this manager");

            if (!m_loadedTextures.Contains(name))
            {
                return;
            }

            if (alterLoaded)
            {
                m_loadedTextures.Remove(name); // Will not throw if not found
            }
            m_requestedTextures.Remove(name);  // Will not throw if not found

            // We keep the texture object but destroy the dx resources
            m_textures[name].Destroy();
        }
        public static bool LoadFromFile(string filepath, out MyFileTextureParams outParams)
        {
            outParams = new MyFileTextureParams();
            if (string.IsNullOrEmpty(filepath))
            {
                return(false);
            }

            filepath = MyResourceUtils.GetTextureFullPath(filepath);
            if (m_dictCached.TryGetValue(filepath, out outParams))
            {
                return(true);
            }
            if (!MyFileSystem.FileExists(filepath))
            {
                MyRender11.Log.WriteLine("Missing texture: " + filepath);
                return(false);
            }

            try
            {
                using (var s = MyFileSystem.OpenRead(filepath))
                {
                    Image image = Image.Load(s);
                    outParams.Resolution.X = image.Description.Width;
                    outParams.Resolution.Y = image.Description.Height;
                    outParams.Format       = image.Description.Format;
                    outParams.Mipmaps      = image.Description.MipLevels;
                    outParams.ArraySize    = image.Description.ArraySize;
                }

                m_dictCached.Add(filepath, outParams);
                return(true);
            }
            catch (Exception)
            {
                MyRenderProxy.Assert(false, "The file in textures exists, but cannot be loaded. Please, investigate!");
                return(false);
            }
        }
        public static byte[] GetBytePattern(MyChannel channel, Format format)
        {
            if (format == Format.Unknown)
            {
                return(null);
            }
            switch (channel)
            {
            case MyChannel.ColorMetal:
                if (format == Format.BC7_UNorm_SRgb ||
                    format == Format.BC7_UNorm)
                {
                    return(MyGeneratedTexturePatterns.ColorMetal_BC7_SRgb);
                }
                else
                {
                    break;
                }

            case MyChannel.NormalGloss:
                if (format == Format.BC7_UNorm)
                {
                    return(MyGeneratedTexturePatterns.NormalGloss_BC7);
                }
                else
                {
                    break;
                }

            case MyChannel.Extension:
                if (format == Format.BC7_UNorm_SRgb ||
                    format == Format.BC7_UNorm)
                {
                    return(MyGeneratedTexturePatterns.Extension_BC7_SRgb);
                }
                else
                {
                    break;
                }

            case MyChannel.Alphamask:
                if (format == Format.BC4_UNorm)
                {
                    return(MyGeneratedTexturePatterns.Alphamask_BC4);
                }
                else
                {
                    break;
                }

            default:
                break;
            }

            // No correct pattern is found, therefore we will use generated:
            int       texelBitSize = MyResourceUtils.GetTexelBitSize(format);
            const int bitsInByte   = 8;
            // Blocks are 4x4 texels in memory
            const int blockTexelCount = 16;
            int       blockBitCount   = texelBitSize * blockTexelCount;

            return(new byte[blockBitCount / bitsInByte]);
        }