static void LoadCompressedImage2D(int mipLevel, int width, int height, InternalFormat format, byte[] data)
        {
            int imageSize = GLFormatHelper.CalculateImageSize(width, height, format);

            GL.CompressedTexImage2D(TextureTarget.Texture2D, mipLevel,
                                    format, width, height, 0, imageSize, data);
        }
        public static GLTextureCube FromDDS(DDS dds, DDS mipLevel2)
        {
            var surfaces    = dds.GetSurfaces(0, false, 6);
            var surfacesMip = mipLevel2.GetSurfaces(0, false, 6);

            int size = (int)dds.Width;

            GLTextureCube texture = new GLTextureCube();

            texture.Width = size; texture.Height = size;
            texture.Bind();

            InternalFormat format = InternalFormat.CompressedRgbaS3tcDxt5Ext;

            if (dds.Platform.OutputFormat == TexFormat.BC6H_UF16)
            {
                format = InternalFormat.CompressedRgbBptcUnsignedFloat;
            }
            if (dds.Platform.OutputFormat == TexFormat.BC6H_SF16)
            {
                format = InternalFormat.CompressedRgbBptcSignedFloat;
            }

            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < dds.MipCount; j++)
                {
                    int mipWidth  = CalculateMipDimension(texture.Width, j);
                    int mipHeight = CalculateMipDimension(texture.Height, j);
                    int imageSize = GLFormatHelper.CalculateImageSize(mipWidth, mipHeight, format);

                    if (j == 0)
                    {
                        GL.CompressedTexImage2D(TextureTarget.TextureCubeMapPositiveX + i, j,
                                                format,
                                                mipWidth, mipHeight,
                                                0, imageSize, surfaces[i].mipmaps[0]);
                    }
                    else if (j == 1)
                    {
                        GL.CompressedTexImage2D(TextureTarget.TextureCubeMapPositiveX + i, j,
                                                format,
                                                mipWidth, mipHeight,
                                                0, imageSize, surfacesMip[i].mipmaps[0]);
                    }
                    else
                    {
                        GL.CompressedTexImage2D(TextureTarget.TextureCubeMapPositiveX + i, j,
                                                format,
                                                mipWidth, mipHeight,
                                                0, imageSize, IntPtr.Zero);
                    }
                }
            }

            texture.Unbind();
            return(texture);
        }
        public static GLTexture2DArray FromDDS(DDS dds)
        {
            GLTexture2DArray texture = new GLTexture2DArray();

            texture.Width = (int)dds.Width; texture.Height = (int)dds.Height;
            texture.Bind();

            GL.TexParameter(texture.Target, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            GL.TexParameter(texture.Target, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
            GL.TexParameter(texture.Target, TextureParameterName.TextureBaseLevel, 0);
            GL.TexParameter(texture.Target, TextureParameterName.TextureMaxLevel, 13);
            GL.TexParameter(texture.Target, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
            GL.TexParameter(texture.Target, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
            GL.TexParameter(texture.Target, TextureParameterName.TextureWrapR, (int)TextureWrapMode.ClampToEdge);

            InternalFormat format = InternalFormat.Rgba8;

            for (int j = 0; j < dds.MipCount; j++)
            {
                int mipWidth  = CalculateMipDimension(texture.Width, j);
                int mipHeight = CalculateMipDimension(texture.Height, j);
                int imageSize = GLFormatHelper.CalculateImageSize(mipWidth, mipHeight, format) * (int)dds.ArrayCount;

                List <byte[]> levels = new List <byte[]>();
                for (int i = 0; i < dds.ArrayCount; i++)
                {
                    var data = dds.GetDecodedSurface(i, j);
                    if (i == 0 || i == 1)
                    {
                        data = FlipHorizontal(mipWidth, mipHeight, data);
                    }

                    levels.Add(data);
                }

                var surface = ByteUtils.CombineArray(levels.ToArray());
                if (format == InternalFormat.Rgba8)
                {
                    GL.TexImage3D(TextureTarget.Texture2DArray, j,
                                  PixelInternalFormat.Rgba,
                                  mipWidth, mipHeight, (int)dds.ArrayCount, 0, PixelFormat.Bgra, PixelType.UnsignedByte,
                                  surface);
                }
                else
                {
                    GL.CompressedTexImage3D(TextureTarget.Texture2DArray, j,
                                            format,
                                            mipWidth, mipHeight, (int)dds.ArrayCount,
                                            0, imageSize, surface);
                }
            }

            texture.Unbind();
            return(texture);
        }
Exemple #4
0
        public static GLTextureCubeArray FromDDS(DDS dds)
        {
            int size = (int)dds.Width;

            GLTextureCubeArray texture = new GLTextureCubeArray();

            texture.Width  = size;
            texture.Height = size;
            texture.Bind();

            var format = dds.Platform.OutputFormat;

            var           surfaces        = dds.GetSurfaces();
            List <byte[]> cubemapSurfaces = new List <byte[]>();

            for (int a = 0; a < surfaces.Count; a++)
            {
                cubemapSurfaces.Add(surfaces[a].mipmaps[0]);
            }

            int depth = surfaces.Count;

            Console.WriteLine($"depth {depth}");

            byte[] buffer = ByteUtils.CombineArray(cubemapSurfaces.ToArray());

            for (int j = 0; j < dds.MipCount; j++)
            {
                int mipWidth  = CalculateMipDimension(texture.Width, j);
                int mipHeight = CalculateMipDimension(texture.Height, j);

                if (dds.IsBCNCompressed())
                {
                    var internalFormat = GLFormatHelper.ConvertCompressedFormat(format, true);
                    GLTextureDataLoader.LoadCompressedImage(texture.Target, mipWidth, mipHeight, depth, internalFormat, buffer, j);
                }
                else
                {
                    var formatInfo = GLFormatHelper.ConvertPixelFormat(format);
                    if (dds.Platform.OutputFormat == TexFormat.RGBA8_UNORM)
                    {
                        formatInfo.Format = PixelFormat.Rgba;
                    }

                    GLTextureDataLoader.LoadImage(texture.Target, mipWidth, mipHeight, depth, formatInfo, buffer, j);
                }
            }

            GL.TexParameter(texture.Target, TextureParameterName.TextureBaseLevel, 0);
            GL.TexParameter(texture.Target, TextureParameterName.TextureMaxLevel, 13);
            GL.GenerateMipmap(GenerateMipmapTarget.TextureCubeMapArray);

            texture.Unbind();
            return(texture);
        }
        public void LoadImage(int width, int height, TexFormat format, byte[] data)
        {
            if (TextureFormatHelper.IsBCNCompressed(format))
            {
                var internalFormat = GLFormatHelper.ConvertCompressedFormat(format, true);
                int imageSize      = GLFormatHelper.CalculateImageSize(width, height, internalFormat);

                GL.CompressedTexImage3D(TextureTarget.Texture2DArray, 0,
                                        internalFormat, width, height, 1, 0, imageSize, data);
            }
            else
            {
                var formatInfo = GLFormatHelper.ConvertPixelFormat(format);

                GL.TexImage3D(Target, 0, formatInfo.InternalFormat, width, height, 1, 0,
                              formatInfo.Format, formatInfo.Type, data);
            }
        }
        public static GLTextureCube FromDDS(DDS dds, bool flipY = false, bool isBGRA = false)
        {
            int size = (int)dds.Width;

            GLTextureCube texture = new GLTextureCube();

            texture.Width = size; texture.Height = size;
            texture.Bind();

            InternalFormat format = InternalFormat.CompressedRgbaS3tcDxt5Ext;

            if (dds.Platform.OutputFormat == TexFormat.BC6H_UF16)
            {
                format = InternalFormat.CompressedRgbBptcUnsignedFloat;
            }
            if (dds.Platform.OutputFormat == TexFormat.BC6H_SF16)
            {
                format = InternalFormat.CompressedRgbBptcSignedFloat;
            }
            if (dds.Platform.OutputFormat == TexFormat.RGBA8_UNORM)
            {
                format = InternalFormat.Rgba8;
            }

            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < dds.MipCount; j++)
                {
                    int mipWidth  = CalculateMipDimension(texture.Width, j);
                    int mipHeight = CalculateMipDimension(texture.Height, j);
                    int imageSize = GLFormatHelper.CalculateImageSize(mipWidth, mipHeight, format);
                    var surface   = dds.GetDeswizzledSurface(i, j);

                    if (dds.Parameters.UseSoftwareDecoder || flipY)
                    {
                        surface = dds.GetDecodedSurface(i, j);
                        format  = InternalFormat.Rgba8;
                    }

                    if (flipY)
                    {
                        surface = FlipVertical(mipWidth, mipHeight, surface);
                    }

                    if (format == InternalFormat.Rgba8)
                    {
                        GL.TexImage2D(TextureTarget.TextureCubeMapPositiveX + i, j,
                                      PixelInternalFormat.Rgba,
                                      mipWidth, mipHeight, 0, PixelFormat.Rgba, PixelType.UnsignedByte,
                                      surface);
                    }
                    else
                    {
                        GL.CompressedTexImage2D(TextureTarget.TextureCubeMapPositiveX + i, j,
                                                format,
                                                mipWidth, mipHeight,
                                                0, imageSize, surface);
                    }
                }
            }

            texture.Unbind();
            return(texture);
        }
        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();
        }