Exemplo n.º 1
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();
        }
Exemplo n.º 2
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();
        }