diff --git a/Bearded.Graphics/Core/Buffer.cs b/Bearded.Graphics/Core/Buffer.cs index b797041..91aa505 100644 --- a/Bearded.Graphics/Core/Buffer.cs +++ b/Bearded.Graphics/Core/Buffer.cs @@ -8,13 +8,13 @@ public sealed class Buffer : IDisposable where T : struct { private static readonly int itemSize = Marshal.SizeOf(typeof(T)); - private int handle { get; } + public int Handle { get; } public int Count { get; private set; } public Buffer() { - handle = GL.GenBuffer(); + Handle = GL.GenBuffer(); } public Target Bind(BufferTarget target = BufferTarget.ArrayBuffer) @@ -34,7 +34,7 @@ internal Target(Buffer buffer, BufferTarget target) this.buffer = buffer; this.target = target; - GL.BindBuffer(target, buffer.handle); + GL.BindBuffer(target, buffer.Handle); } public void Reserve(int count, BufferUsageHint usageHint = defaultUsageHint) @@ -72,7 +72,7 @@ public void Dispose() public void Dispose() { - GL.DeleteBuffer(handle); + GL.DeleteBuffer(Handle); } } } diff --git a/Bearded.Graphics/Core/RenderSettings/ArrayTextureUniform.cs b/Bearded.Graphics/Core/RenderSettings/ArrayTextureUniform.cs deleted file mode 100644 index 49df99b..0000000 --- a/Bearded.Graphics/Core/RenderSettings/ArrayTextureUniform.cs +++ /dev/null @@ -1,23 +0,0 @@ -using Bearded.Graphics.Textures; -using OpenTK.Graphics.OpenGL; - -namespace Bearded.Graphics.RenderSettings -{ - public sealed class ArrayTextureUniform : Uniform - { - public TextureUnit Unit { get; } - - public ArrayTextureUniform(string name, TextureUnit unit, ArrayTexture texture) - : base(name, texture) - { - Unit = unit; - } - - protected override void SetAtLocation(int location) - { - GL.ActiveTexture(Unit); - Value.Bind(); - GL.Uniform1(location, Unit - TextureUnit.Texture0); - } - } -} diff --git a/Bearded.Graphics/Core/RenderSettings/TextureUniform.cs b/Bearded.Graphics/Core/RenderSettings/TextureUniform.cs index 7286de8..f1e9c8c 100644 --- a/Bearded.Graphics/Core/RenderSettings/TextureUniform.cs +++ b/Bearded.Graphics/Core/RenderSettings/TextureUniform.cs @@ -1,23 +1,34 @@ -using Bearded.Graphics.Textures; +using System; +using Bearded.Graphics.Textures; using OpenTK.Graphics.OpenGL; -namespace Bearded.Graphics.RenderSettings +namespace Bearded.Graphics.RenderSettings; + +public sealed class TextureUniform(string name, TextureUnit unit, Texture value) + : Uniform(name, unit, value); + +public sealed class ArrayTextureUniform(string name, TextureUnit unit, ArrayTexture value) + : Uniform(name, unit, value); + +public sealed class BufferTextureUniform(string name, TextureUnit unit, BufferTexture value) + : Uniform(name, unit, value); + +public abstract class Uniform : Uniform + where TTexture : IBindableTexture + where TTarget : struct, IDisposable { - public sealed class TextureUniform : Uniform - { - public TextureUnit Unit { get; } + public TextureUnit Unit { get; } - public TextureUniform(string name, TextureUnit unit, Texture value) - : base(name, value) - { - Unit = unit; - } + internal Uniform(string name, TextureUnit unit, TTexture value) + : base(name, value) + { + Unit = unit; + } - protected override void SetAtLocation(int location) - { - GL.ActiveTexture(Unit); - Value.Bind(); - GL.Uniform1(location, Unit - TextureUnit.Texture0); - } + protected override void SetAtLocation(int location) + { + GL.ActiveTexture(Unit); + Value.Bind(); + GL.Uniform1(location, Unit - TextureUnit.Texture0); } } diff --git a/Bearded.Graphics/Core/Textures/ArrayTexture.cs b/Bearded.Graphics/Core/Textures/ArrayTexture.cs index 8701129..64b7bbd 100644 --- a/Bearded.Graphics/Core/Textures/ArrayTexture.cs +++ b/Bearded.Graphics/Core/Textures/ArrayTexture.cs @@ -3,22 +3,22 @@ namespace Bearded.Graphics.Textures { - public sealed partial class ArrayTexture : IDisposable + public sealed partial class ArrayTexture : IBindableTexture, IDisposable { + private readonly TextureTarget target; private PixelInternalFormat pixelInternalFormat; public int Handle { get; } - public int Width { get; private set; } - public int Height { get; private set; } - public int LayerCount { get; private set; } public static ArrayTexture Empty( - int width, int height, int layerCount, PixelInternalFormat pixelFormat = PixelInternalFormat.Rgba) + int width, int height, int layerCount, + PixelInternalFormat pixelFormat = PixelInternalFormat.Rgba, + TextureTarget textureTarget = TextureTarget.Texture2DArray) { - var arrayTexture = new ArrayTexture(); + var arrayTexture = new ArrayTexture(textureTarget); using var target = arrayTexture.Bind(); target.Resize(width, height, layerCount, pixelFormat); @@ -28,26 +28,25 @@ public static ArrayTexture Empty( return arrayTexture; } - public ArrayTexture() + public ArrayTexture(TextureTarget target = TextureTarget.Texture2DArray) { - GL.GenTextures(1, out int handle); - Handle = handle; + this.target = target; + Handle = GL.GenTexture(); } - public Target Bind(TextureTarget target = TextureTarget.Texture2DArray) + public Target Bind() { - return new Target(this, target); + return new Target(this); } public readonly struct Target : IDisposable { private readonly ArrayTexture arrayTexture; - private readonly TextureTarget target; + private TextureTarget target => arrayTexture.target; - internal Target(ArrayTexture arrayTexture, TextureTarget target) + internal Target(ArrayTexture arrayTexture) { this.arrayTexture = arrayTexture; - this.target = target; GL.BindTexture(target, arrayTexture.Handle); } diff --git a/Bearded.Graphics/Core/Textures/BufferTexture.cs b/Bearded.Graphics/Core/Textures/BufferTexture.cs new file mode 100644 index 0000000..aa77155 --- /dev/null +++ b/Bearded.Graphics/Core/Textures/BufferTexture.cs @@ -0,0 +1,59 @@ +using System; +using OpenTK.Graphics.OpenGL; + +namespace Bearded.Graphics.Textures; + +public sealed class BufferTexture : IBindableTexture, IDisposable +{ + public int Handle { get; } + + public static BufferTexture ForBuffer(Buffer buffer, SizedInternalFormat format) + where T : struct + { + var texture = new BufferTexture(); + + using var target = texture.Bind(); + target.AttachBuffer(buffer, format); + + return texture; + } + + public BufferTexture() + { + Handle = GL.GenTexture(); + } + + public Target Bind() + { + return new Target(this); + } + + public readonly struct Target : IDisposable + { + internal Target(BufferTexture texture) + { + GL.BindTexture(TextureTarget.TextureBuffer, texture.Handle); + } + + public void AttachBuffer(Buffer buffer, SizedInternalFormat internalFormat) + where T : struct + { + GL.TexBuffer(TextureBufferTarget.TextureBuffer, internalFormat, buffer.Handle); + } + + public void DetachBuffer() + { + GL.TexBuffer(TextureBufferTarget.TextureBuffer, SizedInternalFormat.R8, 0); + } + + public void Dispose() + { + GL.BindTexture(TextureTarget.TextureBuffer, 0); + } + } + + public void Dispose() + { + GL.DeleteTexture(Handle); + } +} diff --git a/Bearded.Graphics/Core/Textures/IBindableTexture.cs b/Bearded.Graphics/Core/Textures/IBindableTexture.cs new file mode 100644 index 0000000..0e96207 --- /dev/null +++ b/Bearded.Graphics/Core/Textures/IBindableTexture.cs @@ -0,0 +1,9 @@ +using System; + +namespace Bearded.Graphics.Textures; + +public interface IBindableTexture + where TTarget : struct, IDisposable +{ + TTarget Bind(); +} diff --git a/Bearded.Graphics/Core/Textures/Texture.cs b/Bearded.Graphics/Core/Textures/Texture.cs index c7cdbb7..89e50dd 100644 --- a/Bearded.Graphics/Core/Textures/Texture.cs +++ b/Bearded.Graphics/Core/Textures/Texture.cs @@ -3,24 +3,24 @@ namespace Bearded.Graphics.Textures { - public sealed partial class Texture : IDisposable + public sealed partial class Texture : IBindableTexture, IDisposable { + private readonly TextureTarget target; private PixelInternalFormat pixelInternalFormat; private PixelFormat pixelFormat; private PixelType pixelType; public int Handle { get; } - public int Height { get; private set; } - public int Width { get; private set; } public static Texture Empty(int width, int height, PixelInternalFormat pixelInternalFormat = PixelInternalFormat.Rgba, PixelFormat pixelFormat = PixelFormat.Rgba, - PixelType pixelType = PixelType.UnsignedByte) + PixelType pixelType = PixelType.UnsignedByte, + TextureTarget textureTarget = TextureTarget.Texture2D) { - var texture = new Texture(); + var texture = new Texture(textureTarget); using var target = texture.Bind(); target.Resize(width, height, pixelInternalFormat, pixelFormat, pixelType); @@ -30,26 +30,25 @@ public static Texture Empty(int width, int height, return texture; } - public Texture() + public Texture(TextureTarget target = TextureTarget.Texture2D) { - GL.GenTextures(1, out int handle); - Handle = handle; + this.target = target; + Handle = GL.GenTexture(); } - public Target Bind(TextureTarget target = TextureTarget.Texture2D) + public Target Bind() { - return new Target(this, target); + return new Target(this); } public readonly struct Target : IDisposable { private readonly Texture texture; - private readonly TextureTarget target; + private TextureTarget target => texture.target; - internal Target(Texture texture, TextureTarget target) + internal Target(Texture texture) { this.texture = texture; - this.target = target; GL.BindTexture(target, texture.Handle); }