Exemple #1
0
 private static void SetAutomaticMipmapCreation(KTXLoadInstructions texinfo)
 {
     // DISABLED ON PURPOSE
     //			// Prefer glGenerateMipmaps over GL_GENERATE_MIPMAP
     //			if (texinfo.GenerateMipmaps && IsGenerateMipmapMissing ())
     //			{
     //				GL.TexParameter ((TextureTarget)texinfo.GlTarget, TextureParameterName.GenerateMipmap, (int)All.True);
     //			}
 }
Exemple #2
0
        public void Populate(byte[] headerChunk)
        {
            Instructions = new KTXLoadInstructions();

            /* Compare identifier, is this a KTX file? */
            for (int i = 0; i < KTXIdentifier.Length; ++i)
            {
                if (headerChunk[i] != KTXIdentifier[i])
                {
                    Instructions.Result = KTXError.UnknownFileFormat;
                }
            }

            const int AFTER_IDENTIFIER = 12;

            Endianness = BitConverter.ToUInt32(headerChunk, AFTER_IDENTIFIER);
            if (RequiresSwap())
            {
                /* Convert endianness of header fields if necessary */
                SwapEndian32(headerChunk, KTX_HEADER_SIZE - AFTER_IDENTIFIER);
            }
            else if (Endianness != KTX_ENDIAN_REF)
            {
                Instructions.Result = KTXError.InvalidValue;
                return;
            }
            Read(headerChunk, AFTER_IDENTIFIER + 4);

            if (!(GlTypeSize == 1 || GlTypeSize != 2 || GlTypeSize != 4))
            {
                /* Only 8, 16, and 32-bit types supported so far */
                Instructions.Result = KTXError.InvalidValue;
                return;
            }

            /* Check glType and glFormat */
            Instructions.IsCompressed = false;
            if (GlType == 0 || GlFormat == 0)
            {
                if (GlType + GlFormat != 0)
                {
                    /* either both or none of glType, glFormat must be zero */
                    Instructions.Result = KTXError.InvalidValue;
                    return;
                }
                Instructions.IsCompressed = true;
            }

            /* Check texture dimensions. KTX files can store 8 types of textures:
            *  1D, 2D, 3D, cube, and array variants of these. There is currently
            *  no GL extension that would accept 3D array or cube array textures. */
            if ((PixelWidth == 0) ||
                (PixelDepth > 0 && PixelHeight == 0))
            {
                /* texture must have width */
                /* texture must have height if it has depth */
                Instructions.Result = KTXError.InvalidValue;
                return;
            }

            Instructions.TextureDimensions = 1;
            Instructions.ViewType          = MgImageViewType.TYPE_1D;    //TextureTarget.Texture1D;
            Instructions.GenerateMipmaps   = false;
            if (PixelHeight > 0)
            {
                Instructions.TextureDimensions = 2;
                Instructions.ViewType          = MgImageViewType.TYPE_2D;        // TextureTarget.Texture2D;
            }
            if (PixelDepth > 0)
            {
                Instructions.TextureDimensions = 3;
                Instructions.ViewType          = MgImageViewType.TYPE_2D;        // TextureTarget.Texture3D;
            }

            if (NumberOfFaces == 6)
            {
                if (Instructions.TextureDimensions == 2)
                {
                    Instructions.ViewType = MgImageViewType.TYPE_CUBE;                     // TextureTarget.TextureCubeMap;
                }
                else
                {
                    /* cube map needs 2D faces */
                    Instructions.Result = KTXError.InvalidValue;
                    return;
                }
            }
            else if (NumberOfFaces != 1)
            {
                /* numberOfFaces must be either 1 or 6 */
                Instructions.Result = KTXError.InvalidValue;
                return;
            }

            /* load as 2D texture if 1D textures are not supported */
            if (Instructions.TextureDimensions == 1 &&
                (Instructions.IsCompressed && IsGLCompressedTexImage1DMissing()) ||
                (!Instructions.IsCompressed && IsGLTexImage1DMissing()))
            {
                Instructions.TextureDimensions = 2;
                Instructions.ViewType          = MgImageViewType.TYPE_2D;        //(uint) TextureTarget.Texture2D;
                PixelHeight = 1;
            }

            if (NumberOfArrayElements > 0)
            {
                if (Instructions.ViewType == MgImageViewType.TYPE_1D)                 // (uint) TextureTarget.Texture1D)
                {
                    Instructions.ViewType = MgImageViewType.TYPE_1D_ARRAY;            //(uint) TextureTarget.Texture1DArray;
                }
                else if (Instructions.ViewType == MgImageViewType.TYPE_2D)            // (uint) TextureTarget.Texture2D)
                {
                    Instructions.ViewType = MgImageViewType.TYPE_2D_ARRAY;            // (uint) TextureTarget.Texture2DArray;
                }
                else
                {
                    /* No API for 3D and cube arrays yet */
                    Instructions.Result = KTXError.UnsupportedTextureType;
                    return;
                }
                Instructions.TextureDimensions++;
            }

            /* reject 3D texture if unsupported */
            if (Instructions.TextureDimensions == 3 &&
                (Instructions.IsCompressed && IsGLCompressedTexImage3DMissing()) ||
                (!Instructions.IsCompressed && IsGLTexImage3DMissing()))
            {
                Instructions.Result = KTXError.UnsupportedTextureType;
                return;
            }

            /* Check number of mipmap levels */
            if (NumberOfMipmapLevels == 0)
            {
                Instructions.GenerateMipmaps = true;
                NumberOfMipmapLevels         = 1;
            }

            UInt32 max_dim = Math.Max(Math.Max(PixelWidth, PixelHeight), PixelDepth);

            if (max_dim < (1 << ((int)(NumberOfMipmapLevels - 1))))
            {
                /* Can't have more mip levels than 1 + log2(max(width, height, depth)) */
                Instructions.Result = KTXError.InvalidValue;
                return;
            }

            Instructions.Result = KTXError.Success;
        }