GetClosestGLInternalFormat() public static method

Takes the Axiom pixel format and returns the type that must be provided to GL as internal format. If no match exists, returns the closest match.
public static GetClosestGLInternalFormat ( PixelFormat mFormat, bool hwGamma = false ) : OpenTK.Graphics.ES11.All
mFormat PixelFormat mFormat The pixel format
hwGamma bool hwGamma Whether a hardware gamma-corrected version is requested
return OpenTK.Graphics.ES11.All
Esempio n. 1
0
        /// <summary>
        /// </summary>
        /// <param name="data"> </param>
        /// <param name="dest"> </param>
        protected override void Upload(PixelBox data, BasicBox dest)
        {
            OpenGL.BindTexture(this._target, this._textureId);
            GLESConfig.GlCheckError(this);

            if (PixelUtil.IsCompressed(data.Format))
            {
                if (data.Format != Format || !data.IsConsecutive)
                {
                    throw new AxiomException("Compressed images must be consecutive, in the source format");
                }

                if (data.Format != Format || !data.IsConsecutive)
                {
                    throw new AxiomException("Compressed images must be consecutive, in the source format.");
                }

                All format = GLESPixelUtil.GetClosestGLInternalFormat(Format);
                // Data must be consecutive and at beginning of buffer as PixelStorei not allowed
                // for compressed formats
                if (dest.Left == 0 && dest.Top == 0)
                {
                    OpenGL.CompressedTexImage2D(All.Texture2D, this._level, format, dest.Width, dest.Height, 0, data.ConsecutiveSize, data.Data);
                }
                else
                {
                    OpenGL.CompressedTexSubImage2D(All.Texture2D, this._level, dest.Left, dest.Top, dest.Width, dest.Height, format, data.ConsecutiveSize, data.Data);
                }
                GLESConfig.GlCheckError(this);
            }
            else if (this._softwareMipmap)
            {
                if (data.Width != data.RowPitch)
                {
                    //TODO
                    throw new AxiomException("Unsupported Texture format!");
                }
                if (data.Height * data.Width != data.SlicePitch)
                {
                    //TODO
                    throw new AxiomException("Unsupported Texture format!");
                }

                OpenGL.PixelStore(All.UnpackAlignment, 1);
                GLESConfig.GlCheckError(this);
                BuildMipmaps(data);
            }
            else
            {
                if (data.Width != data.RowPitch)
                {
                    //TODO
                    throw new AxiomException("Unsupported Texture format!");
                }
                if (data.Height * data.Width != data.SlicePitch)
                {
                    //TODO
                    throw new AxiomException("Unsupported Texture format!");
                }

                if (((data.Width * PixelUtil.GetNumElemBytes(data.Format)) & 3) != 0)
                {
                    // Standard alignment of 4 is not right
                    OpenGL.PixelStore(All.UnpackAlignment, 1);
                    GLESConfig.GlCheckError(this);
                }
                All form = GLESPixelUtil.GetGLOriginFormat(data.Format);
                All pix  = GLESPixelUtil.GetGLOriginDataType(data.Format);
                GLESConfig.GlCheckError(this);
                GL.TexSubImage2D(this._faceTarget, this._level, dest.Left, dest.Top, dest.Width, dest.Height, GLESPixelUtil.GetGLOriginFormat(data.Format), GLESPixelUtil.GetGLOriginDataType(data.Format), data.Data);
                GLESConfig.GlCheckError(this);
            }

            OpenGL.PixelStore(All.UnpackAlignment, 4);
            GLESConfig.GlCheckError(this);
        }
Esempio n. 2
0
        /// <summary>
        /// </summary>
        protected void CreateSurfaceList()
        {
            this._surfaceList.Clear();
            // For all faces and mipmaps, store surfaces as HardwarePixelBufferSharedPtr
            bool wantGenerateMips = (Usage & TextureUsage.AutoMipMap) == TextureUsage.AutoMipMap;
            // Do mipmapping in software? (uses GLU) For some cards, this is still needed. Of course,
            // only when mipmap generation is desired.
            bool doSoftware = wantGenerateMips && !MipmapsHardwareGenerated && MipmapCount > 0;

            for (int face = 0; face < faceCount; face++)
            {
                int width  = Width;
                int height = Height;
                for (int mip = 0; mip <= MipmapCount; mip++)
                {
                    HardwarePixelBuffer buf = new GLESTextureBuffer(Name, GLESTextureTarget, this._textureID, width, height, (int)GLESPixelUtil.GetClosestGLInternalFormat(Format, HardwareGammaEnabled), face, mip, (BufferUsage)Usage, doSoftware && mip == 0, HardwareGammaEnabled, FSAA);

                    this._surfaceList.Add(buf);

                    // If format is PVRTC then every mipmap is a custom one so to allow the upload of the compressed data
                    // provided by the file we need to adjust the current mip level's dimention
#warning Compressed Textures are not yet supported by axiom

                    /*
                     * if (mFormat == PF_PVRTC_RGB2 || mFormat == PF_PVRTC_RGBA2 ||
                     * mFormat == PF_PVRTC_RGB4 || mFormat == PF_PVRTC_RGBA4)
                     * {
                     *      if (width > 1)
                     *      {
                     *              width = width / 2;
                     *      }
                     *      if (height > 1)
                     *      {
                     *              height = height / 2;
                     *      }
                     * }*/
                    /// Check for error
                    if (buf.Width == 0 || buf.Height == 0 || buf.Depth == 0)
                    {
                        throw new AxiomException(string.Format("Zero sized texture surface on texture: {0} face: {1} mipmap : {2}. The GL driver probably refused to create the texture.", Name, face, mip));
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// </summary>
        protected override void createInternalResources()
        {
            // Convert to nearest power-of-two size if required
            Width  = GLESPixelUtil.OptionalPO2(Width);
            Height = GLESPixelUtil.OptionalPO2(Height);
            Depth  = GLESPixelUtil.OptionalPO2(Depth);

            //adjust format if required
            Format = TextureManager.Instance.GetNativeFormat(Graphics.TextureType.TwoD, Format, Usage);
            // Check requested number of mipmaps
            int maxMips = GLESPixelUtil.GetMaxMipmaps(Width, Height, Depth, Format);

            if (PixelUtil.IsCompressed(Format) && _mipmapCount == 0)
            {
                RequestedMipmapCount = 0;
            }

            _mipmapCount = RequestedMipmapCount;
            if (_mipmapCount > maxMips)
            {
                _mipmapCount = maxMips;
            }

            // Generate texture name
            OpenGL.GenTextures(1, ref this._textureID);
            GLESConfig.GlCheckError(this);
            // Set texture type
            OpenGL.BindTexture(All.Texture2D, this._textureID);
            GLESConfig.GlCheckError(this);
            // Set some misc default parameters, these can of course be changed later
            OpenGL.TexParameter(All.Texture2D, All.TextureMinFilter, (int)All.LinearMipmapNearest);
            GLESConfig.GlCheckError(this);
            OpenGL.TexParameter(All.Texture2D, All.TextureMagFilter, (int)All.Nearest);
            GLESConfig.GlCheckError(this);
            OpenGL.TexParameter(All.Texture2D, All.TextureWrapS, (int)All.ClampToEdge);
            GLESConfig.GlCheckError(this);
            OpenGL.TexParameter(All.Texture2D, All.TextureWrapT, (int)All.ClampToEdge);
            GLESConfig.GlCheckError(this);
            // If we can do automip generation and the user desires this, do so
            MipmapsHardwareGenerated = Root.Instance.RenderSystem.HardwareCapabilities.HasCapability(Capabilities.HardwareMipMaps) && !PixelUtil.IsCompressed(Format);

            if ((Usage & TextureUsage.AutoMipMap) == TextureUsage.AutoMipMap && RequestedMipmapCount > 0 && MipmapsHardwareGenerated)
            {
                OpenGL.TexParameter(All.Texture2D, All.GenerateMipmap, (int)All.True);
                GLESConfig.GlCheckError(this);
            }

            // Allocate internal buffer so that TexSubImageXD can be used
            // Internal format
            All format = GLESPixelUtil.GetClosestGLInternalFormat(Format, HardwareGammaEnabled);
            int width  = Width;
            int height = Height;
            int depth  = Depth;

            if (PixelUtil.IsCompressed(Format))
            {
                // Compressed formats
                int size = PixelUtil.GetMemorySize(Width, Height, Depth, Format);

                // Provide temporary buffer filled with zeroes as glCompressedTexImageXD does not
                // accept a 0 pointer like normal glTexImageXD
                // Run through this process for every mipmap to pregenerate mipmap pyramid

                var    tmpData    = new byte[size];
                IntPtr tmpDataptr = Memory.PinObject(tmpData);
                for (int mip = 0; mip <= MipmapCount; mip++)
                {
                    size = PixelUtil.GetMemorySize(Width, Height, Depth, Format);
                    OpenGL.CompressedTexImage2D(All.Texture2D, mip, format, width, height, 0, size, tmpDataptr);

                    GLESConfig.GlCheckError(this);

                    if (width > 1)
                    {
                        width = width / 2;
                    }
                    if (height > 1)
                    {
                        height = height / 2;
                    }
                    if (depth > 1)
                    {
                        depth = depth / 2;
                    }
                }
                Memory.UnpinObject(tmpData);
            }
            else
            {
                // Run through this process to pregenerate mipmap pyramid
                for (int mip = 0; mip <= MipmapCount; mip++)
                {
                    OpenGL.TexImage2D(All.Texture2D, mip, (int)format, width, height, 0, format, All.UnsignedByte, IntPtr.Zero);
                    GLESConfig.GlCheckError(this);

                    if (width > 1)
                    {
                        width = width / 2;
                    }
                    if (height > 1)
                    {
                        height = height / 2;
                    }
                }
            }

            CreateSurfaceList();

            // Get final internal format
            Format = GetBuffer(0, 0).Format;
        }
Esempio n. 4
0
        /// <summary>
        ///   Detect which internal formats are allowed as RTT Also detect what combinations of stencil and depth are allowed with this internal format.
        /// </summary>
        private void DetectFBOFormats()
        {
            // Try all formats, and report which ones work as target
            int fb = 0, tid = 0;
            All target = All.Texture2D;

            for (int x = 0; x < (int)Media.PixelFormat.Count; x++)
            {
                LogManager.Instance.Write("[GLES] [DEBUG] testing PixelFormat : {0}", (Media.PixelFormat)x);

                this._props[x]         = new FormatProperties();
                this._props[x].Modes   = new List <FormatProperties.Mode>();
                this._props[x].IsValid = false;

                // Fetch GL format token
                All fmt = GLESPixelUtil.GetClosestGLInternalFormat((Media.PixelFormat)x);
                LogManager.Instance.Write("[GLES] [DEBUG] fmt={0}", fmt);
                if (fmt == All.Zero && x != 0)
                {
                    continue;
                }

                // No test for compressed formats
                if (PixelUtil.IsCompressed((Media.PixelFormat)x))
                {
                    continue;
                }

                // Create and attach framebuffer
                OpenGLOES.GenRenderbuffers(1, ref fb);
                GLESConfig.GlCheckError(this);
                OpenGLOES.BindFramebuffer(All.FramebufferOes, fb);
                GLESConfig.GlCheckError(this);

                if (fmt != All.Zero)
                {
                    // Create and attach texture
                    OpenGL.GenTextures(1, ref tid);
                    GLESConfig.GlCheckError(this);
                    OpenGL.BindTexture(target, tid);
                    GLESConfig.GlCheckError(this);

                    // Set some default parameters
                    OpenGL.TexParameterx(target, All.TextureMinFilter, (int)All.LinearMipmapNearest);
                    GLESConfig.GlCheckError(this);
                    OpenGL.TexParameterx(target, All.TextureMagFilter, (int)All.Nearest);
                    GLESConfig.GlCheckError(this);
                    OpenGL.TexParameterx(target, All.TextureWrapS, (int)All.ClampToEdge);
                    GLESConfig.GlCheckError(this);
                    OpenGL.TexParameterx(target, All.TextureWrapT, (int)All.ClampToEdge);
                    GLESConfig.GlCheckError(this);

                    OpenGL.TexImage2D(target, 0, (int)fmt, ProbeSize, ProbeSize, 0, fmt, All.UnsignedByte, IntPtr.Zero);
                    GLESConfig.GlCheckError(this);
                    OpenGLOES.FramebufferTexture2D(All.FramebufferOes, All.ColorAttachment0Oes, target, tid, 0);
                    GLESConfig.GlCheckError(this);
                }

                // Check status
                All status = OpenGLOES.CheckFramebufferStatus(All.FramebufferOes);
                GLESConfig.GlCheckError(this);
                LogManager.Instance.Write("[GLES] [DEBUG] status={0}", status);

                // Ignore status in case of fmt==GL_NONE, because no implementation will accept
                // a buffer without *any* attachment. Buffers with only stencil and depth attachment
                // might still be supported, so we must continue probing.
                if (fmt == 0 || status == All.FramebufferCompleteOes)
                {
                    this._props[x].IsValid = true;
                    var str = new StringBuilder();
                    str.Append("FBO " + PixelUtil.GetFormatName((Media.PixelFormat)x) + " depth/stencil support: ");

                    // For each depth/stencil formats
                    for (int depth = 0; depth < DepthFormats.Length; ++depth)
                    {
                        if (DepthFormats[depth] != All.Depth24Stencil8Oes)
                        {
                            // General depth/stencil combination
                            for (int stencil = 0; stencil < StencilFormats.Length; ++stencil)
                            {
                                if (TryFormat(DepthFormats[depth], StencilFormats[stencil]))
                                {
                                    /// Add mode to allowed modes
                                    str.Append("D" + DepthBits[depth] + "S" + StencilBits[stencil] + " ");
                                    var mode = new FormatProperties.Mode();
                                    mode.Depth   = depth;
                                    mode.Stencil = stencil;
                                    this._props[x].Modes.Add(mode);
                                }
                            }                     //end for stencil
                        }                         //end if
                        else
                        {
                            // Packed depth/stencil format
                            if (TryPacketFormat(DepthFormats[depth]))
                            {
                                /// Add mode to allowed modes
                                str.Append("Packed-D" + DepthBits[depth] + "S8" + " ");
                                var mode = new FormatProperties.Mode();
                                mode.Depth   = depth;
                                mode.Stencil = 0;                                 //unused
                                this._props[x].Modes.Add(mode);
                            }
                        }
                    }             //end for depth
                    LogManager.Instance.Write(str.ToString());
                }                 //end if
                                  // Delete texture and framebuffer
#if AXIOM_PLATFORM_IPHONE
                // The screen buffer is 1 on iPhone
                OpenGLOES.BindFramebuffer(All.FramebufferOes, 1);
#else
                OpenGLOES.BindFramebuffer(All.FramebufferOes, 0);
#endif
                GLESConfig.GlCheckError(this);
                OpenGLOES.DeleteFramebuffers(1, ref fb);
                GLESConfig.GlCheckError(this);
                if (fmt != 0)
                {
                    OpenGL.DeleteTextures(1, ref tid);
                }
            }             //end for pixelformat count

            string fmtstring = string.Empty;
            for (int x = 0; x < (int)Media.PixelFormat.Count; x++)
            {
                if (this._props[x].IsValid)
                {
                    fmtstring += PixelUtil.GetFormatName((Media.PixelFormat)x);
                }
            }
            LogManager.Instance.Write("[GLES] : Valid FBO targets " + fmtstring);
        }