Example #1
0
        /// <summary>
        /// Initializes a new instance of the OpenGLTexture2D class.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="pixels">A pointer to the raw pixel data with which to populate the texture.</param>
        /// <param name="width">The texture's width in pixels.</param>
        /// <param name="height">The texture's height in pixels.</param>
        /// <param name="bytesPerPixel">The number of bytes which represent each pixel in the raw data.</param>
        /// <param name="options">The texture's configuration options.</param>
        public OpenGLTexture2D(UltravioletContext uv, IntPtr pixels, Int32 width, Int32 height, Int32 bytesPerPixel, TextureOptions options)
            : base(uv)
        {
            Contract.EnsureRange(width > 0, nameof(width));
            Contract.EnsureRange(height > 0, nameof(height));
            Contract.EnsureRange(bytesPerPixel >= 3 || bytesPerPixel <= 4, nameof(bytesPerPixel));

            var isSrgb   = (options & TextureOptions.SrgbColor) == TextureOptions.SrgbColor;
            var isLinear = (options & TextureOptions.LinearColor) == TextureOptions.LinearColor;

            if (isSrgb && isLinear)
            {
                throw new ArgumentException(UltravioletStrings.TextureCannotHaveMultipleEncodings);
            }

            var caps        = uv.GetGraphics().Capabilities;
            var srgbEncoded = (isLinear ? false : (isSrgb ? true : uv.Properties.SrgbDefaultForTexture2D)) && caps.SrgbEncodingEnabled;

            var format         = OpenGLTextureUtil.GetFormatFromBytesPerPixel(bytesPerPixel);
            var internalformat = OpenGLTextureUtil.GetInternalFormatFromBytesPerPixel(bytesPerPixel, srgbEncoded);

            if (format == gl.GL_NONE || internalformat == gl.GL_NONE)
            {
                throw new NotSupportedException(OpenGLStrings.UnsupportedImageType);
            }

            CreateNativeTexture(uv, internalformat, width, height, format,
                                gl.GL_UNSIGNED_BYTE, (void *)pixels, (options & TextureOptions.ImmutableStorage) == TextureOptions.ImmutableStorage);
        }
Example #2
0
        /// <inheritdoc/>
        public override Texture3D ImportPreprocessed(ContentManager manager, IContentProcessorMetadata metadata, BinaryReader reader)
        {
            var caps = manager.Ultraviolet.GetGraphics().Capabilities;

            var version = 0u;
            var depth   = reader.ReadInt32();

            if (depth == Int32.MaxValue)
            {
                version = reader.ReadUInt32();
            }

            if (version > 0u)
            {
                depth = reader.ReadInt32();
            }

            var srgbEncoded = false;

            if (version > 0u)
            {
                srgbEncoded = reader.ReadBoolean() && caps.SrgbEncodingEnabled;
            }

            var layerSurfaces = new List <SurfaceSource>();
            var layerPointers = new List <IntPtr>();

            try
            {
                for (int i = 0; i < depth; i++)
                {
                    var length = reader.ReadInt32();
                    var bytes  = reader.ReadBytes(length);

                    using (var stream = new MemoryStream(bytes))
                    {
                        var surfaceSource = SurfaceSource.Create(stream);
                        layerSurfaces.Add(surfaceSource);
                        layerPointers.Add(surfaceSource.Data);
                    }
                }

                var layerWidth  = layerSurfaces[0].Width;
                var layerHeight = layerSurfaces[0].Height;

                var internalformat = OpenGLTextureUtil.GetInternalFormatFromBytesPerPixel(4, srgbEncoded);
                var format         = (layerSurfaces[0].DataFormat == SurfaceSourceDataFormat.RGBA) ? gl.GL_RGBA : gl.GL_BGRA;

                return(new OpenGLTexture3D(manager.Ultraviolet, internalformat, layerWidth, layerHeight, format,
                                           gl.GL_UNSIGNED_BYTE, layerPointers, true));
            }
            finally
            {
                foreach (var layerSurface in layerSurfaces)
                {
                    layerSurface.Dispose();
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="OpenGLTexture3D"/> class.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="data">A list of pointers to the raw pixel data for each of the texture's layers.</param>
        /// <param name="width">The texture's width in pixels.</param>
        /// <param name="height">The texture's height in pixels.</param>
        /// <param name="bytesPerPixel">The number of bytes which represent each pixel in the raw data.</param>
        /// <param name="options">The texture's configuration options.</param>
        public OpenGLTexture3D(UltravioletContext uv, IList <IntPtr> data, Int32 width, Int32 height, Int32 bytesPerPixel, TextureOptions options)
            : base(uv)
        {
            Contract.Require(data, nameof(data));
            Contract.EnsureRange(width > 0, nameof(width));
            Contract.EnsureRange(height > 0, nameof(height));
            Contract.EnsureRange(bytesPerPixel == 3 || bytesPerPixel == 4, nameof(bytesPerPixel));

            var isLinear = (options & TextureOptions.LinearColor) == TextureOptions.LinearColor;
            var isSrgb   = (options & TextureOptions.SrgbColor) == TextureOptions.SrgbColor;

            if (isLinear && isSrgb)
            {
                throw new ArgumentException(UltravioletStrings.TextureCannotHaveMultipleEncodings);
            }

            var caps        = uv.GetGraphics().Capabilities;
            var srgbEncoded = isLinear ? false : (isSrgb ? true : uv.Properties.SrgbDefaultForSurface3D) && caps.SrgbEncodingEnabled;

            var format         = OpenGLTextureUtil.GetFormatFromBytesPerPixel(bytesPerPixel);
            var internalformat = OpenGLTextureUtil.GetInternalFormatFromBytesPerPixel(bytesPerPixel, srgbEncoded);

            if (format == gl.GL_NONE || internalformat == gl.GL_NONE)
            {
                throw new NotSupportedException(OpenGLStrings.UnsupportedImageType);
            }

            var pixels = IntPtr.Zero;

            try
            {
                pixels = CreateConcatenatedPixelBuffer(data, width * height * bytesPerPixel);
                CreateNativeTexture(uv, internalformat, width, height, data.Count, format,
                                    gl.GL_UNSIGNED_BYTE, (void *)pixels, true);
            }
            finally
            {
                if (pixels != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pixels);
                }
            }
        }
Example #4
0
        /// <summary>
        /// Reallocates the renderbuffer's storage.
        /// </summary>
        /// <param name="width">The renderbuffer's width in pixels.</param>
        /// <param name="height">The renderbuffer's height in pixels.</param>
        private void AllocateRenderbufferStorage(Int32 width, Int32 height)
        {
            using (OpenGLState.ScopedBindRenderbuffer(renderbuffer, true))
            {
                switch (format)
                {
                case RenderBufferFormat.Color:
                {
                    var internalformat = OpenGLTextureUtil.GetInternalFormatFromBytesPerPixel(4, SrgbEncoded);
                    gl.RenderbufferStorage(gl.GL_RENDERBUFFER, internalformat, width, height);
                    gl.ThrowIfError();
                }
                break;

                case RenderBufferFormat.Depth24Stencil8:
                    gl.RenderbufferStorage(gl.GL_RENDERBUFFER, gl.GL_DEPTH24_STENCIL8, width, height);
                    gl.ThrowIfError();
                    break;

                case RenderBufferFormat.Depth32:
                    gl.RenderbufferStorage(gl.GL_RENDERBUFFER, gl.GL_DEPTH_COMPONENT32, width, height);
                    gl.ThrowIfError();
                    break;

                case RenderBufferFormat.Depth16:
                    gl.RenderbufferStorage(gl.GL_RENDERBUFFER, gl.GL_DEPTH_COMPONENT16, width, height);
                    gl.ThrowIfError();
                    break;

                case RenderBufferFormat.Stencil8:
                    gl.RenderbufferStorage(gl.GL_RENDERBUFFER, gl.GL_STENCIL_INDEX8, width, height);
                    break;

                default:
                    throw new NotSupportedException("format");
                }
            }
        }
        /// <inheritdoc/>
        public override Texture2D ImportPreprocessed(ContentManager manager, IContentProcessorMetadata metadata, BinaryReader reader)
        {
            var caps = manager.Ultraviolet.GetGraphics().Capabilities;

            var version = 0u;
            var length  = reader.ReadInt32();

            if (length == Int32.MaxValue)
            {
                version = reader.ReadUInt32();
            }

            if (version > 0u)
            {
                length = reader.ReadInt32();
            }

            var srgbEncoded = false;

            if (version > 0u)
            {
                srgbEncoded = reader.ReadBoolean() && caps.SrgbEncodingEnabled;
            }

            var bytes = reader.ReadBytes(length);

            using (var stream = new MemoryStream(bytes))
            {
                using (var source = SurfaceSource.Create(stream))
                {
                    var format         = (source.DataFormat == SurfaceSourceDataFormat.RGBA) ? gl.GL_RGBA : gl.GL_BGRA;
                    var internalformat = OpenGLTextureUtil.GetInternalFormatFromBytesPerPixel(4, srgbEncoded);

                    return(new OpenGLTexture2D(manager.Ultraviolet, internalformat,
                                               source.Width, source.Height, format, gl.GL_UNSIGNED_BYTE, source.Data, true));
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="OpenGLTexture3D"/> class.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="width">The texture's width in pixels.</param>
        /// <param name="height">The texture's height in pixels.</param>
        /// <param name="depth">The texture's depth in layers.</param>
        /// <param name="options">The texture's configuration options.</param>
        public OpenGLTexture3D(UltravioletContext uv, Int32 width, Int32 height, Int32 depth, TextureOptions options)
            : base(uv)
        {
            Contract.EnsureRange(width > 0, nameof(width));
            Contract.EnsureRange(height > 0, nameof(height));
            Contract.EnsureRange(depth > 0, nameof(depth));

            var isLinear = (options & TextureOptions.LinearColor) == TextureOptions.LinearColor;
            var isSrgb   = (options & TextureOptions.SrgbColor) == TextureOptions.SrgbColor;

            if (isLinear && isSrgb)
            {
                throw new ArgumentException(UltravioletStrings.TextureCannotHaveMultipleEncodings);
            }

            var caps        = uv.GetGraphics().Capabilities;
            var srgbEncoded = isLinear ? false : (isSrgb ? true : uv.Properties.SrgbDefaultForSurface3D) && caps.SrgbEncodingEnabled;

            var format         = OpenGLTextureUtil.GetFormatFromBytesPerPixel(4);
            var internalformat = OpenGLTextureUtil.GetInternalFormatFromBytesPerPixel(4, srgbEncoded);

            CreateNativeTexture(uv, internalformat, width, height, depth, format, gl.GL_UNSIGNED_BYTE, null, immutable);
        }
Example #7
0
        /// <summary>
        /// Initializes a new instance of the OpenGLRenderBuffer2D class.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="format">The render buffer's format.</param>
        /// <param name="width">The render buffer's width in pixels.</param>
        /// <param name="height">The render buffer's height in pixels.</param>
        /// <param name="options">The render buffer's configuration options.</param>
        public OpenGLRenderBuffer2D(UltravioletContext uv, RenderBufferFormat format, Int32 width, Int32 height, RenderBufferOptions options)
            : base(uv)
        {
            Contract.EnsureRange(width > 0, nameof(width));
            Contract.EnsureRange(height > 0, nameof(height));

            var isSrgb   = (options & RenderBufferOptions.SrgbColor) == RenderBufferOptions.SrgbColor;
            var isLinear = (options & RenderBufferOptions.LinearColor) == RenderBufferOptions.LinearColor;

            if (isSrgb && isLinear)
            {
                throw new ArgumentException(UltravioletStrings.BuffersCannotHaveMultipleEncodings);
            }

            if ((isSrgb || isLinear) && format != RenderBufferFormat.Color)
            {
                throw new ArgumentException(UltravioletStrings.EncodingSpecifiedForNonColorBuffer);
            }

            var caps        = uv.GetGraphics().Capabilities;
            var srgbEncoded = (isLinear ? false : (isSrgb ? true : uv.Properties.SrgbDefaultForRenderBuffer2D)) && caps.SrgbEncodingEnabled;

            this.format           = format;
            this.width            = width;
            this.height           = height;
            this.immutable        = (options & RenderBufferOptions.ImmutableStorage) == RenderBufferOptions.ImmutableStorage;
            this.willNotBeSampled = (options & RenderBufferOptions.WillNotBeSampled) == RenderBufferOptions.WillNotBeSampled;

            if (willNotBeSampled)
            {
                using (var state = OpenGLState.ScopedCreateRenderbuffer(out renderbuffer))
                {
                    AllocateRenderbufferStorage(width, height);
                }
            }
            else
            {
                switch (format)
                {
                case RenderBufferFormat.Color:
                {
                    var texformat         = OpenGLTextureUtil.GetFormatFromBytesPerPixel(4);
                    var texinternalformat = OpenGLTextureUtil.GetInternalFormatFromBytesPerPixel(4, srgbEncoded);
                    this.texture     = new OpenGLTexture2D(uv, texinternalformat, width, height, texformat, gl.GL_UNSIGNED_BYTE, IntPtr.Zero, immutable, true);
                    this.SrgbEncoded = this.texture.SrgbEncoded;
                }
                break;

                case RenderBufferFormat.Depth24Stencil8:
                    this.texture = new OpenGLTexture2D(uv, gl.GL_DEPTH24_STENCIL8, width, height, gl.GL_DEPTH_STENCIL, gl.GL_UNSIGNED_INT_24_8, IntPtr.Zero, immutable, true);
                    break;

                case RenderBufferFormat.Depth32:
                    this.texture = new OpenGLTexture2D(uv, gl.GL_DEPTH_COMPONENT32, width, height, gl.GL_DEPTH_COMPONENT, gl.GL_UNSIGNED_INT, IntPtr.Zero, immutable, true);
                    break;

                case RenderBufferFormat.Depth16:
                    this.texture = new OpenGLTexture2D(uv, gl.GL_DEPTH_COMPONENT16, width, height, gl.GL_DEPTH_COMPONENT, gl.GL_UNSIGNED_SHORT, IntPtr.Zero, immutable, true);
                    break;

                case RenderBufferFormat.Stencil8:
                    this.texture = new OpenGLTexture2D(uv, gl.GL_STENCIL_INDEX8, width, height, gl.GL_STENCIL, gl.GL_UNSIGNED_INT, IntPtr.Zero, immutable, true);
                    break;

                default:
                    throw new NotSupportedException(nameof(format));
                }
            }
        }