public Framebuffer(Renderer R, int W, int H)
        {
            this.R = R;
            this.W = W;
            this.H = H;

            Color = new GLTexture(TextureTarget.Texture2D);
            Color.Bind(false);
            Color.Image2D(0, PixelInternalFormat.Rgba8, W, H, PixelFormat.Rgba, PixelType.UnsignedByte);
            Color.TexParameter(TextureParameterName.TextureMinFilter, TextureMinFilter.Nearest);
            Color.TexParameter(TextureParameterName.TextureMagFilter, TextureMagFilter.Nearest);

            DepthStencil = new GLTexture(TextureTarget.Texture2D);
            DepthStencil.Bind(false);
            DepthStencil.Image2D(0, PixelInternalFormat.DepthComponent32, W, H,
                                 PixelFormat.DepthComponent, PixelType.UnsignedInt);
            DepthStencil.TexParameter(TextureParameterName.TextureMinFilter, TextureMinFilter.Nearest);
            DepthStencil.TexParameter(TextureParameterName.TextureMagFilter, TextureMagFilter.Nearest);

            ID = GLf.GenFramebuffer();
            GLf.BindFramebuffer(FramebufferTarget.Framebuffer, ID);
            GLf.FramebufferTexture2D(FramebufferTarget.Framebuffer,
                                     FramebufferAttachment.ColorAttachment0, Color.Target, Color.GetID, 0);
            GLf.FramebufferTexture2D(FramebufferTarget.Framebuffer,
                                     FramebufferAttachment.DepthAttachment, DepthStencil.Target, DepthStencil.GetID, 0);

            Check();
            Unbind();
        }
Exemple #2
0
        internal int GetLayout(VertexDescription description, Shader shader)
        {
            if (!OpenGLCapabilities.VertexAttribBinding)
            {
                throw new PlatformNotSupportedException("VertexAttribBinding (separat vertex attributes) are not supported by the driver");
            }

            int layout;

            if (inputLayoutPool.TryGetValue(description, out layout))
            {
                return(layout);
            }

            layout = GL.GenVertexArray();

            if (OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None)
            {
                BindManager.VertexArray = layout;

                int             offset   = 0;
                VertexElement[] elements = description.GetElements();
                for (int i = 0; i < description.ElementCount; i++)
                {
                    if (offset > OpenGLCapabilities.MaxVertexAttribBindingOffset)
                    {
                        throw new PlatformNotSupportedException("offset is higher than maximum supportet offset.");
                    }

                    GL.EnableVertexAttribArray(i);

                    GL.VertexAttribBinding(i, 0);
                    GL.VertexAttribFormat(i, GetComponentsOf(elements[i].Type), VertexAttribType.Float, false, offset);
                    offset += GetSizeOf(elements[i].Type);
                }
            }
            else if (OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension)
            {
                int             offset   = 0;
                VertexElement[] elements = description.GetElements();
                for (int i = 0; i < description.ElementCount; i++)
                {
                    if (offset > OpenGLCapabilities.MaxVertexAttribBindingOffset)
                    {
                        throw new PlatformNotSupportedException("offset is higher than maximum supportet offset.");
                    }

                    Ext.EnableVertexArrayAttrib(layout, i);

                    Ext.VertexArrayVertexAttribBinding(layout, i, elements[i].UsageIndex);
                    Ext.VertexArrayVertexAttribFormat(layout, i, GetComponentsOf(elements[i].Type), (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)VertexAttribType.Float, false, offset);
                    offset += GetSizeOf(elements[i].Type);
                }
            }

            inputLayoutPool.Add(description, layout);
            return(layout);
        }
Exemple #3
0
        internal void SetData(DataBox data, int mipLevel)
        {
            var format = EnumConverter.Convert(Format);

            if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None)
            {
                graphicsDevice.BindManager.SetTexture(this, 0);

                if (!TextureFormatHelper.IsCompressed(Format))
                {
                    GL.TexSubImage3D(TextureTarget.Texture3D, mipLevel, 0, 0, 0, Width, Height, Length, format.Item2, format.Item3, data.Pointer);
                }
                else
                {
                    if (isInitialized)
                    {
                        GL.CompressedTexSubImage3D(TextureTarget.Texture3D, mipLevel, 0, 0, 0, Width, Height, Length, format.Item2, data.Size, data.Pointer);
                    }
                    else
                    {
                        GL.CompressedTexImage3D(TextureTarget.Texture3D, mipLevel, format.Item1, Width, Height, Length, 0, data.Size, data.Pointer);
                    }
                }
            }
            else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension)
            {
                if (!TextureFormatHelper.IsCompressed(Format))
                {
                    Ext.TextureSubImage3D(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture3D, mipLevel, 0, 0, 0, Width, Height, Length, (OpenTK.Graphics.OpenGL.PixelFormat)format.Item2, (OpenTK.Graphics.OpenGL.PixelType)format.Item3, data.Pointer);
                }
                else
                {
                    if (isInitialized)
                    {
                        Ext.CompressedTextureSubImage3D(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture3D, mipLevel, 0, 0, 0, Width, Height, Length, (OpenTK.Graphics.OpenGL.PixelFormat)format.Item2, Marshal.SizeOf(data), data.Pointer);
                    }
                    else
                    {
                        Ext.CompressedTextureImage3D(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture3D, mipLevel, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)format.Item1, Width, Height, Length, 0, Marshal.SizeOf(data), data.Pointer);
                    }
                }
            }
            else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Core)
            {
                //OpenGL 4.5
            }

            graphicsDevice.CheckGLError("Texture3D GenerateMipMaps");
        }
Exemple #4
0
 internal void GenerateMipMaps()
 {
     if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None)
     {
         graphicsDevice.BindManager.SetTexture(this, 0);
         GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
     }
     else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension)
     {
         Ext.GenerateTextureMipmap(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture2D);
     }
     else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Core)
     {
     }
     graphicsDevice.CheckGLError("Texture2D GenerateMipMaps");
 }
        public void Check()
        {
            FramebufferErrorCode FEC;

            if ((FEC = GLf.CheckFramebufferStatus(FramebufferTarget.Framebuffer)) != FramebufferErrorCode.FramebufferComplete)
            {
                throw new Exception(FEC.ToString());
            }

            ErrorCode EC;

            if ((EC = GL.GetError()) != ErrorCode.NoError)
            {
                throw new Exception(EC.ToString());
            }
        }
Exemple #6
0
        internal void CheckStatus()
        {
            FramebufferErrorCode error = 0;

            if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None)
            {
                graphicsDevice.BindManager.Fbo = this;
                error = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer);
            }
            else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension)
            {
                error = (FramebufferErrorCode)Ext.CheckNamedFramebufferStatus(FboID, OpenTK.Graphics.OpenGL.FramebufferTarget.Framebuffer);
            }

            if (error != FramebufferErrorCode.FramebufferComplete)
            {
                throw new GraphicsException(error.ToString());
            }
        }
Exemple #7
0
        internal void SetData(DataRectangle data, int arrayIndex, int mipLevel)
        {
            var format = EnumConverter.Convert(Format);

            if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None)
            {
                graphicsDevice.BindManager.SetTexture(this, 0);

                if (!TextureFormatHelper.IsCompressed(Format))
                {
                    GL.TexSubImage3D(TextureTarget.Texture2DArray, mipLevel, 0, 0, 0, Width, Height, ArraySize, format.Item2, format.Item3, data.Pointer);
                }
                else
                {
                    GL.CompressedTexImage3D(TextureTarget.Texture2DArray, mipLevel, format.Item1, Width, Height, ArraySize, 0, data.Size, data.Pointer);
                }

                GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMaxLevel, this.MipLevels - 1);
            }
            else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension)
            {
                if (!TextureFormatHelper.IsCompressed(Format))
                {
                    Ext.TextureSubImage3D(TextureID, (OpenTK.Graphics.OpenGL.TextureTarget)TextureTarget.Texture2DArray, mipLevel, 0, 0, 0, Width, Height, ArraySize, (OpenTK.Graphics.OpenGL.PixelFormat)format.Item2, (OpenTK.Graphics.OpenGL.PixelType)format.Item3, data.Pointer);
                }
                else
                {
                    Ext.CompressedTextureImage3D(TextureID, (OpenTK.Graphics.OpenGL.TextureTarget)TextureTarget.Texture2DArray, mipLevel, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)EnumConverter.Convert(Format).Item1, Width, Height, ArraySize, 0, Marshal.SizeOf(data), data.Pointer);
                }

                OpenTK.Graphics.OpenGL.GL.Ext.TextureParameter(TextureID, (OpenTK.Graphics.OpenGL.TextureTarget)TextureTarget.Texture2DArray, OpenTK.Graphics.OpenGL.TextureParameterName.TextureMaxLevel, this.MipLevels - 1);
            }
            else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Core)
            {
                //OpenGL 4.5
            }

            graphicsDevice.CheckGLError("Texture2DArray SetData");
        }
Exemple #8
0
        } internal Texture3D(GraphicsDevice graphicsDevice, int width, int height, int length, TextureFormat format, bool generateMipMaps, ResourceUsage usage = ResourceUsage.Normal, DataBox data = new DataBox())
            : base(graphicsDevice, new System.Diagnostics.StackTrace(1))
        {
            if (width <= 0)
            {
                throw new ArgumentOutOfRangeException("width", "Width must be positive.");
            }
            if (height <= 0)
            {
                throw new ArgumentOutOfRangeException("height", "Height must be positive.");
            }
            if (format == TextureFormat.Unknown)
            {
                throw new ArgumentException("Format must not be TextureFormat.Unknown.", "format");
            }
            if (width > graphicsDevice.OpenGLCapabilities.MaxTextureSize)
            {
                throw new PlatformNotSupportedException("width exceeds the maximum texture size");
            }
            if (height > graphicsDevice.OpenGLCapabilities.MaxTextureSize)
            {
                throw new PlatformNotSupportedException("height exceeds the maximum texture size");
            }
            if ((width % 2 != 0 || height % 2 != 0 || length % 2 != 0) && graphicsDevice.OpenGLCapabilities.SupportsNonPowerOf2Textures)
            {
                throw new PlatformNotSupportedException("Driver doesn't support non power of two textures");
            }
            if (length > graphicsDevice.OpenGLCapabilities.MaxTextureSize)
            {
                throw new PlatformNotSupportedException("length exceeds the maximum texture size");
            }
            if (usage == ResourceUsage.Immutable && (data.IsNull))
            {
                throw new ArgumentException("data", "Immutable textures must be initialized with data.");
            }

            this.Width     = width;
            this.Height    = height;
            this.Length    = length;
            this.MipLevels = generateMipMaps ? OpenGL4.GraphicsDevice.MipLevels(width, height) : 1;
            this.Format    = format;
            this.Usage     = usage;
            var internalFormat = EnumConverter.Convert(Format);

            this.TextureID = GL.GenTexture();
            if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None)
            {
                graphicsDevice.BindManager.SetTexture(this, 0);
                //Höchstes Mipmap-Level setzen
                GL.TexParameter(TextureTarget.Texture3D, TextureParameterName.TextureMaxLevel, this.MipLevels - 1);

                //Die Textur erstellen (Null Daten funktionieren nicht mit Compression)
                if (graphicsDevice.OpenGLCapabilities.SupportsTextureStorage && graphicsDevice.OpenGLCapabilities.OpenGLVersion > new Version(4, 2))
                {
                    GL.TexStorage3D(TextureTarget3d.Texture3D, MipLevels, EnumConverter.ConvertSizedInternalFormat(Format), Width, Height, Length);
                }
                else
                {
                    int    size = 0;
                    IntPtr ptr  = IntPtr.Zero;
                    if (!data.IsNull)
                    {
                        size = data.Size;
                        ptr  = data.Pointer;
                    }
                    if (!TextureFormatHelper.IsCompressed(Format))
                    {
                        GL.TexImage3D(TextureTarget.Texture3D, 0, internalFormat.Item1, width, height, length, 0, internalFormat.Item2, internalFormat.Item3, ptr);
                    }
                    else if (ptr != IntPtr.Zero)
                    {
                        GL.CompressedTexImage3D(TextureTarget.Texture3D, 0, internalFormat.Item1, width, height, length, 0, size, ptr);
                        isInitialized = true;
                    }
                }
            }
            else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension)
            {
                //Höchstes Mipmap-Level setzen
                OpenTK.Graphics.OpenGL.GL.Ext.TextureParameter(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture3D, OpenTK.Graphics.OpenGL.TextureParameterName.TextureMaxLevel, this.MipLevels - 1);

                //Die Textur erstellen (Null Daten funktionieren nicht mit Compression)

                if (graphicsDevice.OpenGLCapabilities.SupportsTextureStorage && graphicsDevice.OpenGLCapabilities.OpenGLVersion > new Version(4, 2) && !TextureFormatHelper.IsCompressed(Format))
                {
                    Ext.TextureStorage3D(TextureID, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)TextureTarget3d.Texture3D, MipLevels, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)EnumConverter.ConvertSizedInternalFormat(Format), Width, Height, Length);
                }
                else
                {
                    int    size = 0;
                    IntPtr ptr  = IntPtr.Zero;
                    if (!data.IsNull)
                    {
                        size = data.Size;
                        ptr  = data.Pointer;
                    }
                    if (!TextureFormatHelper.IsCompressed(Format))
                    {
                        Ext.TextureImage3D(TextureID, (OpenTK.Graphics.OpenGL.TextureTarget)TextureTarget.Texture3D, 0, (int)internalFormat.Item1, width, height, length, 0, (OpenTK.Graphics.OpenGL.PixelFormat)internalFormat.Item2, (OpenTK.Graphics.OpenGL.PixelType)internalFormat.Item3, ptr);
                    }
                    else if (ptr != IntPtr.Zero)
                    {
                        Ext.CompressedTextureImage3D(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture3D, 0, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)internalFormat.Item1, width, height, length, 0, size, ptr);
                        isInitialized = true;
                    }
                }
            }
            else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Core)
            {
            }

            graphicsDevice.CheckGLError("Texture3D Constructor");
        }
Exemple #9
0
        private void Attach(FrameBufferDescription description)
        {
            if (!description.HasAttachments)
            {
                throw new ArgumentException("Can't create a framebuffer object without attachments.", "description");
            }

            Description = description;

            if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None)
            {
                graphicsDevice.BindManager.Fbo = this;

                if (description.ColorAttachmentIDs != null)
                {
                    DrawBuffersEnum[] buffers = new DrawBuffersEnum[description.ColorAttachmentIDs.Length];

                    for (int i = 0; i < description.ColorAttachmentIDs.Length; i++)
                    {
                        GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0 + i, description.ColorAttachmentIDs[i], 0);
                        buffers[i] = DrawBuffersEnum.ColorAttachment0 + i;
                    }

                    if (description.ColorAttachmentIDs.Length == 0)
                    {
                        GL.DrawBuffer(DrawBufferMode.None);
                    }
                    else
                    {
                        GL.DrawBuffers(buffers.Length, buffers);
                    }
                }
                else
                {
                    GL.DrawBuffer(DrawBufferMode.None);
                }

                if (description.DepthAttachmentID != -1)
                {
                    GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, description.DepthAttachmentID, 0);
                }
            }
            else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension)
            {
                if (description.ColorAttachmentIDs != null)
                {
                    OpenTK.Graphics.OpenGL.DrawBufferMode[] buffers = new OpenTK.Graphics.OpenGL.DrawBufferMode[description.ColorAttachmentIDs.Length];

                    for (int i = 0; i < description.ColorAttachmentIDs.Length; i++)
                    {
                        Ext.NamedFramebufferTexture(FboID, OpenTK.Graphics.OpenGL.FramebufferAttachment.ColorAttachment0 + i, description.ColorAttachmentIDs[i], 0);
                        buffers[i] = OpenTK.Graphics.OpenGL.DrawBufferMode.ColorAttachment0 + i;
                    }

                    if (description.ColorAttachmentIDs.Length == 0)
                    {
                        Ext.FramebufferDrawBuffer(FboID, OpenTK.Graphics.OpenGL.DrawBufferMode.None);
                    }
                    else
                    {
                        Ext.FramebufferDrawBuffers(FboID, buffers.Length, buffers);
                    }
                }
                else
                {
                    GL.DrawBuffer(DrawBufferMode.None);
                }

                if (description.DepthAttachmentID != -1)
                {
                    Ext.NamedFramebufferTexture(FboID, OpenTK.Graphics.OpenGL.FramebufferAttachment.DepthAttachment, description.DepthAttachmentID, 0);
                }
            }
        }
Exemple #10
0
        internal void SetData(DataRectangle data, int mipLevel)
        {
            if (mipLevel >= this.MipLevels)
            {
                throw new ArgumentOutOfRangeException("MipLevel exceeds the amount of mip levels.");
            }
            if (mipLevel < 0)
            {
                throw new ArgumentOutOfRangeException("MipLevel must not be smaller than zero.");
            }
            if (data.Size < 0)
            {
                throw new ArgumentOutOfRangeException("ImageSize must not be smaller than zero.");
            }

            var format = EnumConverter.Convert(Format);
            int width  = Width / (int)Math.Pow(2, mipLevel);
            int height = Height / (int)Math.Pow(2, mipLevel);

            if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None)
            {
                graphicsDevice.BindManager.SetTexture(this, 0);

                if (!TextureFormatHelper.IsCompressed(Format))
                {
                    GL.TexSubImage2D(TextureTarget.Texture2D, mipLevel, 0, 0, width, height, format.Item2, format.Item3, data.Pointer);
                }
                else
                {
                    if (isInitialized)
                    {
                        GL.CompressedTexSubImage2D(TextureTarget.Texture2D, mipLevel, 0, 0, width, height, format.Item2, data.Size, data.Pointer);
                    }
                    else
                    {
                        GL.CompressedTexImage2D(TextureTarget.Texture2D, mipLevel, format.Item1, width, height, 0, data.Size, data.Pointer);
                    }
                }
            }
            else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension)
            {
                if (!TextureFormatHelper.IsCompressed(Format))
                {
                    Ext.TextureSubImage2D(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, mipLevel, 0, 0, width, height, (OpenTK.Graphics.OpenGL.PixelFormat)format.Item2, (OpenTK.Graphics.OpenGL.PixelType)format.Item3, data.Pointer);
                }
                else
                {
                    if (isInitialized)
                    {
                        Ext.CompressedTextureSubImage2D(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, mipLevel, 0, 0, width, height, (OpenTK.Graphics.OpenGL.PixelFormat)format.Item2, data.Size, data.Pointer);
                    }
                    else
                    {
                        Ext.CompressedTextureImage2D(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, mipLevel, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)format.Item1, width, height, 0, data.Size, data.Pointer);
                    }
                }
            }
            else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Core)
            {
                //OpenGL 4.5
            }

            graphicsDevice.CheckGLError("Texture2D SetData");
        }
Exemple #11
0
        internal Texture2D(GraphicsDevice graphicsDevice, int width, int height, TextureFormat format, int mipLevels, ResourceUsage usage = ResourceUsage.Normal, params DataRectangle[] data)
            : base(graphicsDevice, new System.Diagnostics.StackTrace(1))
        {
            if (width <= 0)
            {
                throw new ArgumentOutOfRangeException("width", "Width must be positive.");
            }
            if (height <= 0)
            {
                throw new ArgumentOutOfRangeException("height", "Height must be positive.");
            }
            if (mipLevels < 0)
            {
                throw new ArgumentOutOfRangeException("mipLevels", "MipLevels must not be negative.");
            }
            if (format == TextureFormat.Unknown)
            {
                throw new ArgumentException("Format must not be TextureFormat.Unknown.", "format");
            }
            if (width > graphicsDevice.OpenGLCapabilities.MaxTextureSize)
            {
                throw new PlatformNotSupportedException("width exceeds the maximum texture size");
            }
            if (height > graphicsDevice.OpenGLCapabilities.MaxTextureSize)
            {
                throw new PlatformNotSupportedException("height exceeds the maximum texture size");
            }
            if (usage == ResourceUsage.Immutable && (data == null || data.Length == 0))
            {
                throw new ArgumentException("data", "Immutable textures must be initialized with data.");
            }
            if (data != null && data.Length != 0 && data.Length < mipLevels)
            {
                throw new ArgumentOutOfRangeException("data", data.Length, string.Format("data Lenght is too small for specified mipLevels, expected: {0}", mipLevels));
            }

            this.Width  = width;
            this.Height = height;
            this.Format = format;
            this.Usage  = usage;
            var internalFormat = EnumConverter.Convert(Format);

            this.MipLevels = mipLevels > 0 ? mipLevels : 1;

            this.TextureID = GL.GenTexture();
            if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None)
            {
                graphicsDevice.BindManager.SetTexture(this, 0);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, this.MipLevels - 1);

                if (graphicsDevice.OpenGLCapabilities.SupportsTextureStorage && graphicsDevice.OpenGLCapabilities.OpenGLVersion > new Version(4, 2) && !TextureFormatHelper.IsCompressed(Format))
                {
                    GL.TexStorage2D(TextureTarget2d.Texture2D, MipLevels, EnumConverter.ConvertSizedInternalFormat(Format), Width, Height);
                }
                else
                {
                    int    size = 0;
                    IntPtr ptr  = IntPtr.Zero;
                    if (data != null && data.Length > 0)
                    {
                        size = data[0].Size;
                        ptr  = data[0].Pointer;
                    }

                    if (!TextureFormatHelper.IsCompressed(Format))
                    {
                        GL.TexImage2D(TextureTarget.Texture2D, 0, internalFormat.Item1, width, height, 0, internalFormat.Item2, internalFormat.Item3, ptr);
                    }
                    else if (ptr != IntPtr.Zero)
                    {
                        GL.CompressedTexImage2D(TextureTarget.Texture2D, 0, internalFormat.Item1, width, height, 0, size, ptr);
                    }
                }
            }
            else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension)
            {
                OpenTK.Graphics.OpenGL.GL.Ext.TextureParameter(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, OpenTK.Graphics.OpenGL.TextureParameterName.TextureMaxLevel, this.MipLevels - 1);
                if (graphicsDevice.OpenGLCapabilities.SupportsTextureStorage && graphicsDevice.OpenGLCapabilities.OpenGLVersion > new Version(4, 2) && !TextureFormatHelper.IsCompressed(Format))
                {
                    Ext.TextureStorage2D(TextureID, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)TextureTarget2d.Texture2D, MipLevels, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)EnumConverter.ConvertSizedInternalFormat(Format), Width, Height);
                }
                else
                {
                    int    size = 0;
                    IntPtr ptr  = IntPtr.Zero;
                    if (data != null && data.Length > 0)
                    {
                        size = data[0].Size;
                        ptr  = data[0].Pointer;
                    }

                    if (!TextureFormatHelper.IsCompressed(Format))
                    {
                        Ext.TextureImage2D(TextureID, (OpenTK.Graphics.OpenGL.TextureTarget)TextureTarget.Texture2D, 0, (int)internalFormat.Item1, width, height, 0, (OpenTK.Graphics.OpenGL.PixelFormat)internalFormat.Item2, (OpenTK.Graphics.OpenGL.PixelType)internalFormat.Item3, ptr);
                    }
                    else if (ptr != IntPtr.Zero)
                    {
                        Ext.CompressedTextureImage2D(TextureID, (OpenTK.Graphics.OpenGL.TextureTarget)TextureTarget.Texture2D, 0, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)internalFormat.Item1, width, height, 0, size, ptr);
                        isInitialized = true;
                    }
                }
            }
            else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Core)
            {
            }

            graphicsDevice.CheckGLError("Texture2D Constructor");

            if (data != null && data.Length > 1)
            {
                for (int i = 1; i < data.Length; i++)
                {
                    SetData(data[i], i);
                }
            }
        }
Exemple #12
0
        internal Texture2DArray(GraphicsDevice graphicsDevice, int width, int height, int arraySize, TextureFormat format, bool generateMipMaps, ResourceUsage usage = ResourceUsage.Normal, params DataRectangle[] data)
            : base(graphicsDevice, new System.Diagnostics.StackTrace(1))
        {
            if (width <= 0)
            {
                throw new ArgumentOutOfRangeException("width", "Width must be positive.");
            }
            if (height <= 0)
            {
                throw new ArgumentOutOfRangeException("height", "Height must be positive.");
            }
            if (format == TextureFormat.Unknown)
            {
                throw new ArgumentException("Format must not be TextureFormat.Unknown.", "format");
            }
            if (width > graphicsDevice.OpenGLCapabilities.MaxTextureSize)
            {
                throw new PlatformNotSupportedException("width exceeds the maximum texture size");
            }
            if (height > graphicsDevice.OpenGLCapabilities.MaxTextureSize)
            {
                throw new PlatformNotSupportedException("height exceeds the maximum texture size");
            }
            if ((width % 2 != 0 || height % 2 != 0) && graphicsDevice.OpenGLCapabilities.SupportsNonPowerOf2Textures)
            {
                throw new PlatformNotSupportedException("Driver doesn't support non power of two textures");
            }
            if (width != height)
            {
                throw new PlatformNotSupportedException("Texture arrays must be quadratic");
            }
            if (arraySize <= 0)
            {
                throw new ArgumentOutOfRangeException("Array Size must be at least one", "arraySize");
            }
            if (usage == ResourceUsage.Immutable && (data == null || data.Length == 0))
            {
                throw new ArgumentException("data", "Immutable textures must be initialized with data.");
            }
            if (data != null && data.Length != 0 && data.Length < arraySize)
            {
                throw new ArgumentOutOfRangeException("data", data.Length, string.Format("data Lenght is too small for specified arraySize, expected: {0}", arraySize));
            }

            this.Width     = width;
            this.Height    = height;
            this.ArraySize = arraySize;
            this.MipLevels = generateMipMaps ? OpenGL4.GraphicsDevice.MipLevels(width, height) : 1;
            this.Format    = format;
            this.Usage     = usage;
            var internalFormat = EnumConverter.Convert(Format);

            this.TextureID = GL.GenTexture();
            if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None)
            {
                graphicsDevice.BindManager.SetTexture(this, 0);
                GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMaxLevel, this.MipLevels - 1);
                if (!TextureFormatHelper.IsCompressed(Format))
                {
                    if (graphicsDevice.OpenGLCapabilities.SupportsTextureStorage && graphicsDevice.OpenGLCapabilities.OpenGLVersion > new Version(4, 2))
                    {
                        GL.TexStorage3D(TextureTarget3d.Texture2DArray, MipLevels, EnumConverter.ConvertSizedInternalFormat(Format), Width, Height, ArraySize);
                    }
                    else
                    {
                        GL.TexImage3D(TextureTarget.Texture2DArray, 0, internalFormat.Item1, width, height, arraySize, 0, internalFormat.Item2, internalFormat.Item3, IntPtr.Zero);
                    }
                }
            }
            else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension)
            {
                Ext.TextureParameter(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, OpenTK.Graphics.OpenGL.TextureParameterName.TextureMaxLevel, this.MipLevels - 1);
                if (!TextureFormatHelper.IsCompressed(Format))
                {
                    if (graphicsDevice.OpenGLCapabilities.SupportsTextureStorage && graphicsDevice.OpenGLCapabilities.OpenGLVersion > new Version(4, 2))
                    {
                        Ext.TextureStorage3D(TextureID, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)TextureTarget3d.Texture2DArray, MipLevels, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)EnumConverter.ConvertSizedInternalFormat(Format), Width, Height, ArraySize);
                    }
                    else
                    {
                        Ext.TextureImage3D(TextureID, (OpenTK.Graphics.OpenGL.TextureTarget)TextureTarget.Texture3D, 0, (int)internalFormat.Item1, width, height, arraySize, 0, (OpenTK.Graphics.OpenGL.PixelFormat)internalFormat.Item2, (OpenTK.Graphics.OpenGL.PixelType)internalFormat.Item3, IntPtr.Zero);
                    }
                }
            }
            else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Core)
            {
            }

            if (data != null)
            {
                for (int i = 0; i < data.Length; i++)
                {
                    SetData(data[i], i, 0);
                }
            }

            graphicsDevice.CheckGLError();
        }
Exemple #13
0
        internal Texture2DArray(GraphicsDevice graphicsDevice, int width, int height, int arraySize, TextureFormat format, int mipLevels, ResourceUsage usage = ResourceUsage.Normal, params DataRectangle[] data)
            : base(graphicsDevice, new System.Diagnostics.StackTrace(1))
        {
            if (width <= 0)
            {
                throw new ArgumentOutOfRangeException("width", "Width must be positive.");
            }
            if (height <= 0)
            {
                throw new ArgumentOutOfRangeException("height", "Height must be positive.");
            }
            if (mipLevels < 0)
            {
                throw new ArgumentOutOfRangeException("mipLevels", "MipLevels must not be negative.");
            }
            if (format == TextureFormat.Unknown)
            {
                throw new ArgumentException("Format must not be TextureFormat.Unknown.", "format");
            }
            if (width > graphicsDevice.OpenGLCapabilities.MaxTextureSize)
            {
                throw new PlatformNotSupportedException("width exceeds the maximum texture size");
            }
            if (height > graphicsDevice.OpenGLCapabilities.MaxTextureSize)
            {
                throw new PlatformNotSupportedException("height exceeds the maximum texture size");
            }
            if (width != height)
            {
                throw new PlatformNotSupportedException("Texture arrays must be quadratic");
            }
            if (arraySize <= 0)
            {
                throw new ArgumentOutOfRangeException("Array Size must be at least one", "arraySize");
            }
            if (data != null && data.Length != 0 && data.Length < mipLevels * arraySize)
            {
                throw new ArgumentOutOfRangeException("data", data.Length, string.Format("data Lenght is too small for specified arraySize and mipLevels, expected: {0}", mipLevels * arraySize));
            }

            this.Width     = width;
            this.Height    = height;
            this.ArraySize = arraySize;
            this.MipLevels = mipLevels > 0 ? mipLevels : 1;
            this.Format    = format;
            this.Usage     = usage;
            var internalFormat = EnumConverter.Convert(Format);

            this.TextureID = GL.GenTexture();
            if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None)
            {
                graphicsDevice.BindManager.SetTexture(this, 0);
                GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMaxLevel, this.MipLevels - 1);
                if (!TextureFormatHelper.IsCompressed(Format))
                {
                    if (graphicsDevice.OpenGLCapabilities.SupportsTextureStorage && graphicsDevice.OpenGLCapabilities.OpenGLVersion > new Version(4, 2))
                    {
                        GL.TexStorage3D(TextureTarget3d.Texture2DArray, MipLevels, EnumConverter.ConvertSizedInternalFormat(Format), Width, Height, ArraySize);
                    }
                    else
                    {
                        GL.TexImage3D(TextureTarget.Texture2DArray, 0, internalFormat.Item1, width, height, arraySize, 0, internalFormat.Item2, internalFormat.Item3, IntPtr.Zero);
                    }
                }
            }
            else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension)
            {
                Ext.TextureParameter(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, OpenTK.Graphics.OpenGL.TextureParameterName.TextureMaxLevel, this.MipLevels - 1);
                if (!TextureFormatHelper.IsCompressed(Format))
                {
                    if (graphicsDevice.OpenGLCapabilities.SupportsTextureStorage && graphicsDevice.OpenGLCapabilities.OpenGLVersion > new Version(4, 2))
                    {
                        Ext.TextureStorage3D(TextureID, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)TextureTarget3d.Texture2DArray, MipLevels, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)EnumConverter.ConvertSizedInternalFormat(Format), Width, Height, ArraySize);
                    }
                    else
                    {
                        Ext.TextureImage3D(TextureID, (OpenTK.Graphics.OpenGL.TextureTarget)TextureTarget.Texture3D, 0, (int)internalFormat.Item1, width, height, arraySize, 0, (OpenTK.Graphics.OpenGL.PixelFormat)internalFormat.Item2, (OpenTK.Graphics.OpenGL.PixelType)internalFormat.Item3, IntPtr.Zero);
                    }
                }
            }
            else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Core)
            {
            }

            if (data != null)
            {
                for (int i = 0; i < data.Length; i++)
                {
                    int arrayIndex = i / MipLevels;
                    int mipIndex   = i - arrayIndex * MipLevels; //Macht Sinn weil int gerundet wird
                    SetData(data[i], arrayIndex, 0);
                }
            }

            graphicsDevice.CheckGLError();
        }
 public void Unbind()
 {
     R.GLViewport();
     GLf.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
 }
 public void Bind()
 {
     GL.Viewport(0, 0, W, H);
     GLf.BindFramebuffer(FramebufferTarget.Framebuffer, ID);
 }