make new opengl source generator

This commit is contained in:
Youmu Liu
2026-08-26 18:12:08 +08:00
parent 65041ff8bc
commit 75bf7fc139
126 changed files with 8934 additions and 26139 deletions
@@ -1,149 +0,0 @@
// -----------------------------------------------------------------------------
// file="AutoTexture"
// Project: StgSharp
// Copyright (c) Nitload.
// SPDX-License-Identifier: MIT
// -----------------------------------------------------------------------------
using StgSharp.Collections;
using StgSharp.Graphics;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Graphics.OpenGL
{
/// <summary>
/// A collection of a sets of OpenGL textures. The collection can automatically select one of
/// the texture with minimum costs to upgrade pixels to BufferHandle. Usually <see ///
/// cref="AutoTextureGL" /> is used for <see cref="IImageProvider" /> loading, but not for
/// target of <see cref="OpenGLFunction.FrameBufferTexture2d(FrameBufferTarget, uint, ///
/// Texture2DTarget, GlHandle, int)" />.
/// </summary>
public sealed class AutoTextureGL
{
private TextureProperty[] propertyCache;
private BidirectionalDictionary<Image, int> imageIndexMap = new BidirectionalDictionary<Image, int>(
);
private Dictionary<Image, int> imageUpdateIndex = new Dictionary<Image, int>();
private int _size, _currentIndex;
private LinkedList<int> usedTextureIndex;
private Queue<int> unusedTextureUnitIndex;
private TextureGL texturePackage;
private TextureProperty _textureProperty;
private AutoTextureGL(
TextureGL texture
)
{
texturePackage = texture;
_size = texture.Count;
imageIndexMap = new BidirectionalDictionary<Image, int>();
unusedTextureUnitIndex = new Queue<int>(Enumerable.Range(0, _size));
usedTextureIndex = new LinkedList<int>();
propertyCache = new TextureProperty[_size];
}
public AutoTextureGL(
int textureObjectSize,
[NotNull]glRender binding
)
{
this._size = textureObjectSize;
imageIndexMap = new BidirectionalDictionary<Image, int>();
texturePackage = new TextureGL(textureObjectSize, binding);
unusedTextureUnitIndex = new Queue<int>(Enumerable.Range(0, _size));
usedTextureIndex = new LinkedList<int>();
propertyCache = new TextureProperty[_size];
}
public TextureProperty DefaultTextureProperty
{
get => _textureProperty;
set => _textureProperty = value;
}
private OpenGLFunction GL => texturePackage.GL;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SelectTextureAndBind(
Image i
)
{
TextureProperty property = _textureProperty;
SelectTextureAndBind(i, property);
}
public void SelectTextureAndBind(
[NotNull]Image i,
TextureProperty property
)
{
// usable texture with image match
if (imageIndexMap.TryGetValue(i, out int index))
{
// prop setting match
if (propertyCache[index] == property)
{
_currentIndex = index; //GL.Assert(true);
texturePackage.Bind2D(index); //GL.Assert(true);
if (i.PixelUpdateCount != imageUpdateIndex[i])
{
texturePackage.LoadTexture(index, i); //GL.Assert(true);
}
} else
{
_currentIndex = index; //GL.Assert(true);
texturePackage.Bind2D(index); //GL.Assert(true);
texturePackage.Set2dProperty(index, property); //GL.Assert(true);
if (i.PixelUpdateCount != imageUpdateIndex[i])
{
texturePackage.LoadTexture(index, i); //GL.Assert(true);
}
}
} else
{
if (unusedTextureUnitIndex.TryDequeue(out int newIndex))
{
// has extra unused texture object
imageIndexMap.Add(i, index);
texturePackage.Bind2D(index);
texturePackage.Set2dProperty(index, property);
texturePackage.LoadTexture(index, i);
usedTextureIndex.AddLast(newIndex);
propertyCache[newIndex] = property;
imageUpdateIndex[i] = i.PixelUpdateCount;
} else
{
// recycle from current
int reusedIndex = usedTextureIndex.First!.Value;
usedTextureIndex.RemoveFirst();
usedTextureIndex.AddLast(reusedIndex);
imageIndexMap[i] = reusedIndex;
propertyCache[newIndex] = property;
imageUpdateIndex[i] = i.PixelUpdateCount;
}
}
}
public static AutoTextureGL ToAuto(
[NotNull]TextureGL texture
)
{
if (texture.IndexOfCurrentTexture != -1) {
throw new InvalidOperationException("Texture to be converted has been editted.");
}
return new AutoTextureGL(texture);
}
}
}
+7 -62
View File
@@ -5,72 +5,17 @@
// SPDX-License-Identifier: MIT
// -----------------------------------------------------------------------------
using StgSharp.Graphics;
using StgSharp.Graphics.OpenGL;
using StgSharp.Mathematics;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace StgSharp.Graphics.OpenGL
{
/// <summary>
/// A collection of handle to Element BufferHandle Object in OpenglFunc. Each <see ///
/// langword="uint" /> value indexed from instance of this class this is the only handle to
/// one Element BufferHandle Object.
/// </summary>
public sealed unsafe class ElementBuffer : GlBufferObjectBase
/// <summary>A set of OpenGL element-buffer handles.</summary>
public sealed record class ElementBuffer(
GlHandle[] Handles
)
{
internal ElementBuffer(
int n,
glRender binding
)
: base(binding)
{
_bufferHandle = GL.GenBuffers(n);
}
public int Count => Handles.Length;
public sealed override void Bind(
int index
)
{
GL.BindBuffer(BufferType.ElementArrayBuffer, _bufferHandle[index]);
}
public void SetValue<T>(
int index,
T[] bufferArray,
BufferUsage usage
) where T : unmanaged, INumber<T>
{
GL.BindBuffer(BufferType.ElementArrayBuffer, _bufferHandle[index]);
GL.SetBufferData<T>(BufferType.ElementArrayBuffer, bufferArray, usage);
}
public void SetValue<T>(
int index,
ReadOnlySpan<T> bufferSpan,
BufferUsage usage
) where T : unmanaged, INumber<T>
{
GL.BindBuffer(BufferType.ElementArrayBuffer, _bufferHandle[index]);
GL.SetBufferData<T>(BufferType.ElementArrayBuffer, bufferSpan, usage);
}
protected sealed override void Dispose(
bool disposing
)
{
if (disposing) {
GL.DeleteBuffers(_bufferHandle);
}
}
public GlHandle this[int index] => Handles[index];
}
}
}
+10 -71
View File
@@ -5,22 +5,13 @@
// SPDX-License-Identifier: MIT
// -----------------------------------------------------------------------------
using StgSharp.Graphics;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Graphics.OpenGL
{
#pragma warning disable CA1008
#pragma warning disable CA1028
#pragma warning disable CA1008
#pragma warning disable CA1028
public enum FrameBufferTarget : uint
#pragma warning restore CA1028
#pragma warning restore CA1008
#pragma warning restore CA1028
#pragma warning restore CA1008
{
Read = glConst.READ_FRAMEBUFFER,
@@ -29,67 +20,15 @@ namespace StgSharp.Graphics.OpenGL
}
public sealed class FrameBuffer : GlBufferObjectBase
/// <summary>A set of OpenGL framebuffer handles.</summary>
public sealed record class FrameBuffer(
GlHandle[] Handles
)
{
internal unsafe FrameBuffer(
int count,
glRender binding
)
: base(binding)
{
_bufferHandle = new GlHandle[count];
GL.GenFrameBuffers(_bufferHandle);
}
public int Count => Handles.Length;
/// <summary>
/// Bind the frame BufferHandle object with _label frame BufferHandle to the frame
/// BufferHandle target specified by target. <see langword="FrameBufferTarget.All" /> will
/// be set as default target.
/// </summary>
/// <param _label="index">
/// Index of object to be binded in handle set
/// </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override unsafe void Bind(
int index
)
{
GL.BindFrameBuffer(FrameBufferTarget.All, _bufferHandle[index]);
}
/// <summary>
/// Bind the frame BufferHandle object with _label frame BufferHandle to the frame
/// BufferHandle target specified by target. Target of this frame BufferHandle should be
/// set manually.
/// </summary>
/// <param _label="target">
/// The frame BufferHandle target of the binding operation.
/// </param>
/// <param _label="index">
/// Index of object to be binded in handle set
/// </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void Bind(
FrameBufferTarget target,
int index
)
{
GL.BindFrameBuffer(target, _bufferHandle[index]);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void BindNull()
{
OpenGLFunction.CurrentGL.BindFrameBuffer(FrameBufferTarget.All, GlHandle.Zero);
}
protected override void Dispose(
bool disposing
)
{
throw new NotImplementedException();
}
public GlHandle this[int index] => Handles[index];
}
}
@@ -1,32 +0,0 @@
// -----------------------------------------------------------------------------
// file="GLBufferObjectBase"
// Project: StgSharp
// Copyright (c) Nitload.
// SPDX-License-Identifier: MIT
// -----------------------------------------------------------------------------
using StgSharp.Graphics.OpenGL;
using System;
using System.Collections.Generic;
using System.Text;
namespace StgSharp.Graphics.OpenGL
{
public abstract class GlBufferObjectBase : BufferObjectBase
{
private readonly glRender _contextBinding;
protected GlBufferObjectBase(
glRender contextBinding
)
{
binding = contextBinding;
_contextBinding = contextBinding;
}
protected OpenGLFunction GL => _contextBinding.GL;
}
}
+6 -42
View File
@@ -5,53 +5,17 @@
// SPDX-License-Identifier: MIT
// -----------------------------------------------------------------------------
using StgSharp.Graphics;
using System;
using System.Collections.Generic;
using System.Security.Principal;
using System.Text;
namespace StgSharp.Graphics.OpenGL
{
public sealed class RenderBuffer : GlBufferObjectBase
/// <summary>A set of OpenGL renderbuffer handles.</summary>
public sealed record class RenderBuffer(
GlHandle[] Handles
)
{
public RenderBuffer(
int count,
glRender binding
)
: base(binding)
{
_bufferHandle = GL.GenRenderBuffer(count);
}
public int Count => Handles.Length;
public override void Bind(
int index
)
{
GL.BindRenderBuffer(this[index]);
}
public void Store(
RenderBufferInternalFormat format,
(int width, int height) size
)
{
if ((size.width > binding.Width) || (size.height > binding.Height)) {
World.LogWarning("FrameBuffer is larger than current canvas binding");
}
}
protected override void Dispose(
bool disposing
)
{
foreach (GlHandle item in _bufferHandle)
{
Console.WriteLine("removing a render buffer");
GL.DeleteRenderBuffer(item);
}
}
public GlHandle this[int index] => Handles[index];
}
}
+15 -223
View File
@@ -5,235 +5,27 @@
// SPDX-License-Identifier: MIT
// -----------------------------------------------------------------------------
using StgSharp.Graphics;
using StgSharp.HighPerformance.ProcessorAbstraction;
using StgSharp.Mathematics;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Graphics.OpenGL
{
public sealed unsafe class TextureGL
/// <summary>A set of OpenGL texture handles.</summary>
public sealed record class TextureGL(
GlHandle[] Handles
)
{
private int currentEditingTextureIndex;
public int Count => Handles.Length;
private OpenGLFunction _gl;
internal GlHandle[] _textureHandle;
internal TextureGL(
int count,
glRender binding
)
{
// sourceList = new Image[count];
this._gl = binding.GL;
_textureHandle = _gl.GenTextures(count);
}
public GlHandle this[
int index
]
{
get => _textureHandle[index];
}
public int Count => _textureHandle.Length;
// internal Image[] sourceList;
public int IndexOfCurrentTexture => currentEditingTextureIndex;
internal OpenGLFunction GL => _gl;
public void ActivateAs(
TextureUnit unit
)
{
_gl.ActiveTextureUnit((uint)unit);
}
public void Bind2D(
int index
)
{
currentEditingTextureIndex = index;
_gl.BindTexture(glConst.TEXTURE_2D, _textureHandle[index]);
}
/// <summary>
/// Load an <see cref="Image" /> to certain texture.
/// </summary>
/// <param _label="index">
///
/// </param>
/// <param _label="i">
///
/// </param>
public unsafe void LoadTexture(
int index,
Image i
)
{
byte[] handle = i.PixelBuffer;
_gl.TextureImage2d(Texture2DTarget.Texture2D, 0, i.Channel, i.Width, i.Height,
i.Channel, i.PixelLayout, pixels:handle);
_gl.GenerateMipmap(Texture2DTarget.Texture2D);
}
#region texture setting
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Set2dFilterProperty(
int index,
TextureFilter onMinify,
TextureFilter onMagnify
)
{
#if DEBUG
if (index != currentEditingTextureIndex)
{
throw new InvalidOperationException(
"Texture to be editted is not the one being activated.");
}
#endif
_gl.TextureParameter(glConst.TEXTURE_2D, glConst.TEXTURE_MIN_FILTER, (int)onMinify);
_gl.TextureParameter(glConst.TEXTURE_2D, glConst.TEXTURE_MAG_FILTER,
(int)onMagnify);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Set2dWrapProperty(
int index,
TextureWrap onHorizontial,
TextureWrap onVertical
)
{
#if DEBUG
if (index != currentEditingTextureIndex)
{
throw new InvalidOperationException(
"Texture to be editted is not the one being activated.");
}
#endif
_gl.TextureParameter(glConst.TEXTURE_2D, glConst.TEXTURE_WRAP_S,
(int)onHorizontial);
_gl.TextureParameter(glConst.TEXTURE_2D, glConst.TEXTURE_WRAP_T, (int)onVertical);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Set2dProperty(
int index,
TextureProperty property
)
{
Set2dFilterProperty(index, property.FilterOnMinify, property.FilterOnMagnify);
Set2dWrapProperty(index, property.WrapOnHorizontial, property.WrapOnVertical);
}
#endregion texture setting
}
[StructLayout(LayoutKind.Explicit)]
public struct TextureProperty
{
[FieldOffset(0)] private M128 mask;
[FieldOffset(0)] private TextureFilter mag;
[FieldOffset(4)] private TextureFilter min;
[FieldOffset(8)] private TextureWrap hori;
[FieldOffset(12)] private TextureWrap vert;
public TextureProperty(
TextureFilter onMagnify,
TextureFilter onMinify,
TextureWrap onHorizontal,
TextureWrap onVertical
)
{
FilterOnMagnify = onMagnify;
FilterOnMinify = onMinify;
WrapOnHorizontial = onHorizontal;
WrapOnVertical = onVertical;
}
public TextureFilter FilterOnMagnify
{
get => mag;
set => mag = value;
}
public TextureFilter FilterOnMinify
{
get => min;
set => min = value;
}
public TextureWrap WrapOnHorizontial
{
get => hori;
set => hori = value;
}
public TextureWrap WrapOnVertical
{
get => vert;
set => vert = value;
}
public override bool Equals(
[NotNullWhen(true)] object obj
)
{
return base.Equals(obj);
}
public override int GetHashCode()
{
return HashCode.Combine(FilterOnMagnify, FilterOnMagnify, WrapOnHorizontial,
WrapOnVertical);
}
public override string ToString()
{
return $"FilterOnMagnify:{FilterOnMagnify}, " + $"FilterOnMagnify:{FilterOnMagnify}, " + $"WrapOnHorizontial:{WrapOnHorizontial}, " + $"WrapOnVertical:{WrapOnVertical}";
}
public static bool operator !=(
TextureProperty left,
TextureProperty right
)
{
return left.mask != right.mask;
}
public static bool operator ==(
TextureProperty left,
TextureProperty right
)
{
return left.mask == right.mask;
}
public static implicit operator TextureProperty(
(
TextureFilter onMagnify, TextureFilter onMinify,
TextureWrap onHorizontial, TextureWrap onVertical
) propertyValueTuple
)
{
return new TextureProperty(propertyValueTuple.onMagnify, propertyValueTuple.onMinify,
propertyValueTuple.onHorizontial,
propertyValueTuple.onVertical);
}
public GlHandle this[int index] => Handles[index];
}
public readonly record struct TextureProperty(
TextureFilter FilterOnMagnify,
TextureFilter FilterOnMinify,
TextureWrap WrapOnHorizontal,
TextureWrap WrapOnVertical
);
#pragma warning disable CA1008
public enum TextureFilter
#pragma warning restore CA1008
@@ -272,9 +64,9 @@ namespace StgSharp.Graphics.OpenGL
}
#pragma warning disable CA1008
#pragma warning disable CA1008
public enum TextureWrap
#pragma warning restore CA1008
#pragma warning restore CA1008
{
Repeat = glConst.REPEAT,
+7 -52
View File
@@ -5,62 +5,17 @@
// SPDX-License-Identifier: MIT
// -----------------------------------------------------------------------------
using StgSharp.Graphics;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
namespace StgSharp.Graphics.OpenGL
{
public sealed unsafe class VertexArray : GlBufferObjectBase
/// <summary>A set of OpenGL vertex-array handles.</summary>
public sealed record class VertexArray(
GlHandle[] Handles
)
{
internal VertexArray(
int n,
glRender binding
)
: base(binding)
{
_bufferHandle = GL.GenVertexArrays(n);
}
public int Count => Handles.Length;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public sealed override void Bind(
int index
)
{
GL.BindVertexArray(_bufferHandle[index]);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void BindNull()
{
OpenGLFunction.CurrentGL.BindVertexArray(GlHandle.Zero);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetVertexAttribute(
uint attributeIndex,
int vertexLength,
TypeCode dataType,
bool isNomalized,
int stride,
int pointer
)
{
GL.SetVertexAttribute(attributeIndex, vertexLength, dataType, isNomalized, stride,
pointer);
}
protected sealed override void Dispose(
bool disposing
)
{
if (disposing) {
GL.DeleteVertexArrays(_bufferHandle);
}
}
public GlHandle this[int index] => Handles[index];
}
}
}
+7 -147
View File
@@ -5,157 +5,17 @@
// SPDX-License-Identifier: MIT
// -----------------------------------------------------------------------------
using StgSharp.Graphics;
using StgSharp.Mathematics;
using StgSharp.Mathematics.Numeric;
using System;
using System.Collections.Generic;
using System.Net.Security;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace StgSharp.Graphics.OpenGL
{
public sealed unsafe class VertexBuffer : GlBufferObjectBase
/// <summary>A set of OpenGL vertex-buffer handles.</summary>
public sealed record class VertexBuffer(
GlHandle[] Handles
)
{
internal VertexBuffer(
int n,
glRender binding
)
: base(binding)
{
_bufferHandle = GL.GenBuffers(n);
}
public int Count => Handles.Length;
public sealed override void Bind(
int index
)
{
GL.BindBuffer(BufferType.ArrayBuffer, _bufferHandle[index]);
}
/// <summary>
///
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Unbind()
{
GL.BindBuffer(BufferType.ArrayBuffer, GlHandle.Zero);
}
/// <summary>
/// Set _data to current vertex BufferHandle object
/// </summary>
/// <typeparam _label="TItem">
/// Type of bufferData
/// </typeparam>
/// <param _label="index">
/// Index to find certain VBO in this instance
/// </param>
/// <param _label="bufferArray">
/// Data to write in
/// </param>
/// <param _label="usage">
/// How OpenGL use these _data, defined by <see cref="BufferUsage" />
/// </param>
public void WriteScalerData<T>(
int index,
T[] bufferArray,
BufferUsage usage
) where T : unmanaged, INumber<T>
{
GL.BindBuffer(BufferType.ArrayBuffer, _bufferHandle[index]);
GL.SetBufferData(BufferType.ArrayBuffer, bufferArray, usage);
}
/// <summary>
/// Set _data to current vertex BufferHandle object
/// </summary>
/// <typeparam _label="TItem">
/// Type of bufferData
/// </typeparam>
/// <param _label="index">
/// Index to find certain VBO in this instance
/// </param>
/// <param _label="scalerSpan">
/// A <see cref="Span{T}" /> of _data to write in
/// </param>
/// <param _label="usage">
/// How OpenGL use these _data, defined by <see cref="BufferUsage" />
/// </param>
public void WriteScalerData<T>(
int index,
ReadOnlySpan<T> scalerSpan,
BufferUsage usage
) where T : unmanaged,INumber<T>
{
GL.BindBuffer(BufferType.ArrayBuffer, _bufferHandle[index]);
GL.SetBufferData(BufferType.ArrayBuffer, scalerSpan, usage);
}
/// <summary>
/// Set data to current vertex BufferHandle object
/// </summary>
/// <typeparam _label="TItem">
/// Type of bufferData
/// </typeparam>
/// <param _label="index">
/// Index to find certain VBO in this instance
/// </param>
/// <param _label="vectorSpan">
/// A <see cref="Span{T}" /> of data to write in
/// </param>
/// <param _label="usage">
/// How OpenGL use these data, defined by <see cref="BufferUsage" />
/// </param>
public void WriteVectorData<T>(
int index,
ReadOnlySpan<T> vectorSpan,
BufferUsage usage
) where T : unmanaged, IUnmanagedVector<T>
{
GL.BindBuffer(BufferType.ArrayBuffer, _bufferHandle[index]);
GL.SetBufferVectorData(BufferType.ArrayBuffer, vectorSpan, usage);
}
/// <summary>
/// Set _data to current vertex BufferHandle object
/// </summary>
/// <typeparam _label="TItem">
/// Type of bufferData
/// </typeparam>
/// <param _label="index">
/// Index to find certain VBO in this instance
/// </param>
/// <param _label="vectorArray">
/// A array of _data to write in
/// </param>
/// <param _label="usage">
/// How OpenGL use these _data, defined by <see cref="BufferUsage" />
/// </param>
public void WriteVectorData<T>(
int index,
T[] vectorArray,
BufferUsage usage
) where T : unmanaged, IUnmanagedVector<T>
{
GL.BindBuffer(BufferType.ArrayBuffer, _bufferHandle[index]);
GL.SetBufferVectorData(BufferType.ArrayBuffer, vectorArray, usage);
}
protected sealed override void Dispose(
bool disposing
)
{
if (disposing) {
GL.DeleteBuffers(_bufferHandle);
}
}
public GlHandle this[int index] => Handles[index];
}
}
}