Ejemplo n.º 1
0
        public void LoadImage(STGenericTexture texture, ImageParameters parameters)
        {
            if (parameters == null)
            {
                parameters = new ImageParameters();
            }

            Bind();

            var format = texture.Platform.OutputFormat;
            var width  = CalculateMipDimension((int)texture.Width, 0);
            var height = CalculateMipDimension((int)texture.Height, 0);

            int numSurfaces = 1;

            if (Target == TextureTarget.Texture3D || Target == TextureTarget.Texture2DArray)
            {
                numSurfaces = (int)Math.Max(1, texture.Depth);
            }
            if (Target == TextureTarget.TextureCubeMap || Target == TextureTarget.TextureCubeMapArray)
            {
                numSurfaces = (int)Math.Max(1, texture.ArrayCount);
            }

            for (int i = 0; i < numSurfaces; i++)
            {
                var surface = texture.GetDeswizzledSurface(i, 0);
                int depth   = 1;

                bool loadAsBitmap = !IsPower2(width, height) && texture.IsBCNCompressed() && false;
                if (texture.IsASTC() || parameters.FlipY)
                {
                    loadAsBitmap = true;
                }

                int numMips = 1;

                for (int mipLevel = 0; mipLevel < numMips; mipLevel++)
                {
                    if (loadAsBitmap || parameters.UseSoftwareDecoder)
                    {
                        var rgbaData = texture.GetDecodedSurface(i, mipLevel);
                        if (parameters.FlipY)
                        {
                            rgbaData = FlipVertical(width, height, rgbaData);
                        }

                        var formatInfo = GLFormatHelper.ConvertPixelFormat(TexFormat.RGBA8_UNORM);
                        if (texture.IsSRGB)
                        {
                            formatInfo.InternalFormat = PixelInternalFormat.Srgb8Alpha8;
                        }

                        GLTextureDataLoader.LoadImage(Target, width, height, depth, formatInfo, rgbaData, mipLevel);
                    }
                    else if (texture.IsBCNCompressed())
                    {
                        var internalFormat = GLFormatHelper.ConvertCompressedFormat(format, true);
                        GLTextureDataLoader.LoadCompressedImage(Target, width, height, depth, internalFormat, surface, mipLevel);
                    }
                    else
                    {
                        var formatInfo = GLFormatHelper.ConvertPixelFormat(format);
                        GLTextureDataLoader.LoadImage(Target, width, height, depth, formatInfo, surface, mipLevel);
                    }
                }

                if (texture.MipCount > 1 && texture.Platform.OutputFormat != TexFormat.BC5_SNORM)
                {
                    GL.GenerateMipmap((GenerateMipmapTarget)Target);
                }
                else
                {
                    //Set level to base only
                    GL.TexParameter(Target, TextureParameterName.TextureBaseLevel, 0);
                    GL.TexParameter(Target, TextureParameterName.TextureMaxLevel, 0);
                }
            }

            Unbind();
        }