Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸƒ Add buffer texture #72

Merged
merged 3 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Bearded.Graphics/Core/Buffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ public sealed class Buffer<T> : 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)
Expand All @@ -34,7 +34,7 @@ internal Target(Buffer<T> 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)
Expand Down Expand Up @@ -72,7 +72,7 @@ public void Dispose()

public void Dispose()
{
GL.DeleteBuffer(handle);
GL.DeleteBuffer(Handle);
}
}
}
23 changes: 0 additions & 23 deletions Bearded.Graphics/Core/RenderSettings/ArrayTextureUniform.cs

This file was deleted.

43 changes: 27 additions & 16 deletions Bearded.Graphics/Core/RenderSettings/TextureUniform.cs
Original file line number Diff line number Diff line change
@@ -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<Texture, Texture.Target>(name, unit, value);

public sealed class ArrayTextureUniform(string name, TextureUnit unit, ArrayTexture value)
: Uniform<ArrayTexture, ArrayTexture.Target>(name, unit, value);

public sealed class BufferTextureUniform(string name, TextureUnit unit, BufferTexture value)
: Uniform<BufferTexture, BufferTexture.Target>(name, unit, value);

public abstract class Uniform<TTexture, TTarget> : Uniform<TTexture>
where TTexture : IBindableTexture<TTarget>
where TTarget : struct, IDisposable
{
public sealed class TextureUniform : Uniform<Texture>
{
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);
}
}
27 changes: 13 additions & 14 deletions Bearded.Graphics/Core/Textures/ArrayTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@

namespace Bearded.Graphics.Textures
{
public sealed partial class ArrayTexture : IDisposable
public sealed partial class ArrayTexture : IBindableTexture<ArrayTexture.Target>, 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);
Expand All @@ -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);
}

Expand Down
59 changes: 59 additions & 0 deletions Bearded.Graphics/Core/Textures/BufferTexture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
ο»Ώusing System;
using OpenTK.Graphics.OpenGL;

namespace Bearded.Graphics.Textures;

public sealed class BufferTexture : IBindableTexture<BufferTexture.Target>, IDisposable
{
public int Handle { get; }

public static BufferTexture ForBuffer<T>(Buffer<T> 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<T>(Buffer<T> 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);
}
}
9 changes: 9 additions & 0 deletions Bearded.Graphics/Core/Textures/IBindableTexture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ο»Ώusing System;

namespace Bearded.Graphics.Textures;

public interface IBindableTexture<out TTarget>
where TTarget : struct, IDisposable
{
TTarget Bind();
}
25 changes: 12 additions & 13 deletions Bearded.Graphics/Core/Textures/Texture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@

namespace Bearded.Graphics.Textures
{
public sealed partial class Texture : IDisposable
public sealed partial class Texture : IBindableTexture<Texture.Target>, 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);
Expand All @@ -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);
}

Expand Down
Loading