/// <summary> /// Processes a resize operation for this texture. /// </summary> private void ProcessResize() { using (OpenGLState.ScopedBindTexture2D(texture, true)) { gl.TexImage2D(gl.GL_TEXTURE_2D, 0, (int)internalformat, width, height, 0, format, type, null); } }
/// <summary> /// Sets the texture's data. /// </summary> /// <param name="level">The mipmap level for which to set data.</param> /// <param name="rect">A rectangle describing the position and size of the data to set.</param> /// <param name="data">A pointer to the data to set.</param> /// <param name="offset">The index of the first element to set.</param> /// <param name="count">The number of elements to set.</param> /// <param name="stride">The number of elements in one row of data.</param> /// <param name="format">The format of the data being set.</param> /// <param name="origin">A <see cref="SetDataOrigin"/> value specifying the origin of the texture data in <paramref name="data"/>.</param> private unsafe void SetDataInternal(Int32 level, Rectangle?rect, IntPtr data, Int32 offset, Int32 count, Int32 stride, TextureDataFormat format, SetDataOrigin origin) { var region = rect ?? new Rectangle(0, 0, width, height); if (region.Width * region.Height != count) { throw new InvalidOperationException(UltravioletStrings.BufferIsWrongSize); } const Int32 SizeOfTextureElementInBytes = 4; var flipHorizontally = (origin == SetDataOrigin.TopRight || origin == SetDataOrigin.BottomRight); var flipVertically = (origin == SetDataOrigin.TopLeft || origin == SetDataOrigin.TopRight); TextureUtil.ReorientTextureData(data.ToPointer(), region.Width, region.Height, SizeOfTextureElementInBytes, flipHorizontally, flipVertically); if (flipHorizontally) { region = new Rectangle((width - region.Width) - region.X, region.Y, region.Width, region.Height); } if (flipVertically) { region = new Rectangle(region.X, (height - region.Height) - region.Y, region.Width, region.Height); } using (OpenGLState.ScopedBindTexture2D(texture)) { gl.PixelStorei(gl.GL_UNPACK_ROW_LENGTH, stride); gl.ThrowIfError(); gl.PixelStorei(gl.GL_UNPACK_ALIGNMENT, 1); gl.ThrowIfError(); gl.TextureSubImage2D(texture, gl.GL_TEXTURE_2D, level, region.X, region.Y, region.Width, region.Height, GetOpenGLTextureFormat(format), gl.GL_UNSIGNED_BYTE, data.ToPointer()); gl.ThrowIfError(); gl.PixelStorei(gl.GL_UNPACK_ROW_LENGTH, 0); gl.ThrowIfError(); } }
/// <summary> /// Sets the texture's data. /// </summary> /// <param name="level">The mipmap level for which to set data.</param> /// <param name="rect">A rectangle describing the position and size of the data to set.</param> /// <param name="data">A pointer to the data to set.</param> /// <param name="offset">The index of the first element to set.</param> /// <param name="count">The number of elements to set.</param> /// <param name="stride">The number of elements in one row of data.</param> /// <param name="format">The format of the data being set.</param> /// <param name="origin">A <see cref="SetDataOrigin"/> value specifying the origin of the texture data in <paramref name="data"/>.</param> private unsafe void SetDataInternal(Int32 level, Rectangle?rect, IntPtr data, Int32 offset, Int32 count, Int32 stride, TextureDataFormat format, SetDataOrigin origin) { var region = rect ?? new Rectangle(0, 0, width, height); if (region.Width * region.Height != count) { throw new InvalidOperationException(UltravioletStrings.BufferIsWrongSize); } const Int32 SizeOfTextureElementInBytes = 4; var flipHorizontally = (origin == SetDataOrigin.TopRight || origin == SetDataOrigin.BottomRight); var flipVertically = (origin == SetDataOrigin.TopLeft || origin == SetDataOrigin.TopRight); TextureUtil.ReorientTextureData(data.ToPointer(), region.Width, region.Height, SizeOfTextureElementInBytes, flipHorizontally, flipVertically); if (flipHorizontally) { region = new Rectangle((width - region.Width) - region.X, region.Y, region.Width, region.Height); } if (flipVertically) { region = new Rectangle(region.X, (height - region.Height) - region.Y, region.Width, region.Height); } using (OpenGLState.ScopedBindTexture2D(texture)) { var rowLengthSupported = (!gl.IsGLES2 || gl.IsExtensionSupported("GL_EXT_unpack_subimage")); var rowLengthFallbackRequired = (stride != 0 && !rowLengthSupported); if (rowLengthFallbackRequired) { var ptr = (Byte *)data.ToPointer(); for (int i = 0; i < height; i++) { gl.TextureSubImage2D(texture, gl.GL_TEXTURE_2D, level, region.X, region.Y + i, region.Width, 1, GetOpenGLTextureFormat(format), gl.GL_UNSIGNED_BYTE, ptr); ptr += stride * 4; } } else { if (rowLengthSupported) { gl.PixelStorei(gl.GL_UNPACK_ROW_LENGTH, stride); gl.ThrowIfError(); } gl.PixelStorei(gl.GL_UNPACK_ALIGNMENT, 1); gl.ThrowIfError(); gl.TextureSubImage2D(texture, gl.GL_TEXTURE_2D, level, region.X, region.Y, region.Width, region.Height, GetOpenGLTextureFormat(format), gl.GL_UNSIGNED_BYTE, data.ToPointer()); gl.ThrowIfError(); if (rowLengthSupported) { gl.PixelStorei(gl.GL_UNPACK_ROW_LENGTH, 0); gl.ThrowIfError(); } } } }
/// <summary> /// Creates the underlying native OpenGL texture with the specified format and data. /// </summary> /// <param name="uv">The Ultraviolet context.</param> /// <param name="internalformat">The texture's internal format.</param> /// <param name="width">The texture's width in pixels.</param> /// <param name="height">The texture's height in pixels.</param> /// <param name="format">The texel format.</param> /// <param name="type">The texel data type.</param> /// <param name="pixels">A pointer to the beginning of the texture's pixel data.</param> /// <param name="immutable">A value indicating whether to use immutable texture storage.</param> private void CreateNativeTexture(UltravioletContext uv, UInt32 internalformat, Int32 width, Int32 height, UInt32 format, UInt32 type, void *pixels, Boolean immutable) { if (uv.IsRunningInServiceMode) { throw new NotSupportedException(UltravioletStrings.NotSupportedInServiceMode); } this.width = width; this.height = height; this.internalformat = internalformat; this.format = format; this.type = type; this.immutable = immutable; this.texture = uv.QueueWorkItemAndWait(() => { uint glname; using (OpenGLState.ScopedCreateTexture2D(out glname)) { if (!gl.IsGLES2) { gl.TextureParameteri(glname, gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAX_LEVEL, 0); gl.ThrowIfError(); } gl.TextureParameteri(glname, gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, (int)gl.GL_LINEAR); gl.ThrowIfError(); gl.TextureParameteri(glname, gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, (int)gl.GL_LINEAR); gl.ThrowIfError(); gl.TextureParameteri(glname, gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, (int)gl.GL_CLAMP_TO_EDGE); gl.ThrowIfError(); gl.TextureParameteri(glname, gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, (int)gl.GL_CLAMP_TO_EDGE); gl.ThrowIfError(); if (immutable) { if (gl.IsTextureStorageAvailable) { gl.TextureStorage2D(glname, gl.GL_TEXTURE_2D, 1, internalformat, width, height); gl.ThrowIfError(); if (pixels != null) { gl.TextureSubImage2D(glname, gl.GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, pixels); gl.ThrowIfError(); } } else { gl.TextureImage2D(glname, gl.GL_TEXTURE_2D, 0, (int)internalformat, width, height, 0, format, type, pixels); gl.ThrowIfError(); } } } if (!immutable) { using (OpenGLState.ScopedBindTexture2D(glname, true)) { gl.TexImage2D(gl.GL_TEXTURE_2D, 0, (int)internalformat, width, height, 0, format, type, pixels); gl.ThrowIfError(); } } return(glname); }); }