This commit is contained in:
Nitload
2024-06-10 15:12:08 +08:00
parent f6963b8e00
commit be6c2e6b92
305 changed files with 28336 additions and 5938 deletions
@@ -0,0 +1,80 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="BufferObjectBase.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and
// this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”,
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Graphics.GL
{
/// <summary>
/// Interface of all kinds OpenGL buffer objects
/// </summary>
public abstract class BufferObjectBase : IDisposable
{
protected RenderStream binding;
protected glHandleSet _bufferHandle;
/// <summary>
/// Get the only handle to one of the Object instance
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public glHandle this[int index] => _bufferHandle[index];
/// <summary>
/// Bind a buffer instance to OpenGL
/// </summary>
/// <param name="index">Index of handle of the object in this instance to be bind</param>
public abstract void Bind(int index);
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
/// <inheritdoc/>
public override string ToString()
{
return $"This is a {GetType().FullName}, containing {_bufferHandle.length} buffer handles: {_bufferHandle.ToString()}.";
}
protected abstract void Dispose(bool disposing);
~BufferObjectBase()
{
Dispose(disposing: false);
}
}
}
@@ -0,0 +1,84 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="EBO.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and
// this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”,
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace StgSharp.Graphics.GL
{
/// <summary>
/// A collection of handle to Element Buffer Object in OpenglFunc.
/// Each <see langword="uint"/> value indexed from instance of this class this
/// is the only handle to one Element Buffer Object.
/// </summary>
public unsafe class ElementBuffer : BufferObjectBase
{
internal ElementBuffer(int n, glRenderStream binding)
{
this.binding = binding;
_bufferHandle = glFunc.GenBuffers(n);
}
public override sealed void Bind(int index)
{
glFunc.BindBuffer(BufferType.ElementArrayBuffer, _bufferHandle[index]);
}
public void SetValue<T>(int index, T[] bufferArray, BufferUsage usage)
where T : struct, IConvertible
{
glFunc.BindBuffer(BufferType.ElementArrayBuffer, _bufferHandle[index]);
glFunc.SetBufferData(BufferType.ElementArrayBuffer, bufferArray, usage);
}
public void SetValue<T>(int index, ReadOnlySpan<T> bufferSpan, BufferUsage usage)
where T : struct, IConvertible
{
glFunc.BindBuffer(BufferType.ElementArrayBuffer, _bufferHandle[index]);
glFunc.SetBufferData(BufferType.ElementArrayBuffer, bufferSpan, usage);
}
protected override sealed void Dispose(bool disposing)
{
if (disposing)
{
glFunc.DeleteBuffers(_bufferHandle);
_bufferHandle.Release();
}
}
}
}
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Graphics.GL
{
#pragma warning disable CA1008
#pragma warning disable CA1028
public enum FrameBufferTarget:uint
#pragma warning restore CA1028
#pragma warning restore CA1008
{
Read = GLconst.READ_FRAMEBUFFER,
All = GLconst.FRAMEBUFFER,
Draw = GLconst.DRAW_FRAMEBUFFER,
}
public class FrameBuffer : BufferObjectBase
{
internal unsafe FrameBuffer(int count, glRenderStream contextBinding)
{
binding = contextBinding;
_bufferHandle = new glHandleSet(count);
((GLcontext*)binding.ContextHandle)->glGenFramebuffers((uint)count,_bufferHandle.handle);
}
/// <summary>
/// Bind the frame buffer object with name frame buffer to the frame buffer target specified by target.
/// <see langword="FrameBufferTarget.All"/> will be set as default target.
/// </summary>
/// <param name="index">Index of object to be binded in handle set</param>
public unsafe override void Bind(int index)
{
glFunc.BindFrameBuffer(FrameBufferTarget.All,_bufferHandle[index]);
}
public static void BindNull()
{
glFunc.BindFrameBuffer(FrameBufferTarget.All,glHandle.Zero);
}
/// <summary>
/// Bind the frame buffer object with name frame buffer to the frame buffer target specified by target.
/// Target of this frame buffer should be set manually.
/// </summary>
/// <param name="target">The frame buffer target of the binding operation.</param>
/// <param name="index">Index of object to be binded in handle set</param>
public unsafe void Bind(FrameBufferTarget target, int index)
{
((GLcontext*)binding.ContextHandle)->glBindFramebuffer((uint)target, _bufferHandle[index].Value);
}
protected override void Dispose(bool disposing)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Security.Principal;
using System.Text;
namespace StgSharp.Graphics.GL
{
public class RenderBuffer : BufferObjectBase
{
public RenderBuffer(int count, glRenderStream binding)
{
this.binding = binding;
_bufferHandle = glFunc.GenRenderBuffer(count);
}
public override void Bind(int index)
{
glFunc.BindRenderBuffer(this[index]);
}
protected override void Dispose(bool disposing)
{
throw new NotImplementedException();
}
public void Store(RenderBufferInternalFormat format, (int width,int height) size)
{
if (size.width > binding.Width || size.height>binding.Height)
{
StgSharp.LogWarning("FrameBuffer is larger than current canvas binding");
}
}
}
}
@@ -0,0 +1,155 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="texture.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and
// this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”,
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Graphics.GL
{
public unsafe class TextureGL
{
internal static bool[] unitActivated = new bool[16];
private IntPtr binding;
internal glHandleSet _textureHandle;
internal Image[] sourceList;
public glHandle this[int index]
{
get => _textureHandle[index];
}
internal TextureGL(int n, glRenderStream binding)
{
sourceList = new Image[n];
this.binding = binding.ContextHandle;
_textureHandle = glFunc.GenTextures(n);
}
public static void Activate(TextureUnit unit)
{
glFunc.ActiveTexture((uint)unit);
}
public void Bind2D(int index)
{
glFunc.BindTexture(GLconst.TEXTURE_2D, _textureHandle[index]);
}
/// <summary>
/// </summary>
/// <param name="index"> </param>
/// <param name="i"> </param>
public unsafe void LoadTexture(int index, Image i)
{
sourceList[index] = i;
glArrayHandle handle = sourceList[index].GetArrayHandle();
glFunc.TextureImage2d(
Texture2DTarget.Texture2D, 0, (uint)i.Channel,
(uint)sourceList[index].Width, (uint)sourceList[index].Height,
(uint)i.Channel, ImageChannelLayout.Byte,
pixelArrayHandle: handle
);
handle.Release();
}
#region texture setting
public void Set2dFilterProperty(
Filter onMinify, Filter onMagnify)
{
glFunc.TextureParameter(GLconst.TEXTURE_2D, GLconst.TEXTURE_MIN_FILTER, (int)onMinify);
glFunc.TextureParameter(GLconst.TEXTURE_2D, GLconst.TEXTURE_MAG_FILTER, (int)onMagnify);
}
public void Set2dWrapProperty(
Wrap onHorizontial, Wrap onVertical)
{
glFunc.TextureParameter(GLconst.TEXTURE_2D, GLconst.TEXTURE_WRAP_S, (int)onHorizontial);
glFunc.TextureParameter(GLconst.TEXTURE_2D, GLconst.TEXTURE_WRAP_T, (int)onVertical);
}
#endregion texture setting
}
#pragma warning disable CA1008
public enum Filter
#pragma warning restore CA1008
{
Nearest = GLconst.NEAREST,
Linear = GLconst.LINEAR,
NearestLinearMipmap = GLconst.NEAREST_MIPMAP_LINEAR,
LinearLinearMipmap = GLconst.LINEAR_MIPMAP_LINEAR,
NearestNearestMipmap = GLconst.NEAREST_MIPMAP_NEAREST,
LinearNearestMipmap = GLconst.LINEAR_MIPMAP_NEAREST,
}
#pragma warning disable CA1008
public enum TextureUnit
#pragma warning restore CA1008
{
Unit0 = GLconst.TEXTURE0,
Unit1 = GLconst.TEXTURE1,
Unit2 = GLconst.TEXTURE2,
Unit3 = GLconst.TEXTURE3,
Unit4 = GLconst.TEXTURE4,
Unit5 = GLconst.TEXTURE5,
Unit6 = GLconst.TEXTURE6,
Unit7 = GLconst.TEXTURE7,
Unit8 = GLconst.TEXTURE8,
Unit9 = GLconst.TEXTURE9,
Unit10 = GLconst.TEXTURE10,
Unit11 = GLconst.TEXTURE11,
Unit12 = GLconst.TEXTURE12,
Unit13 = GLconst.TEXTURE13,
Unit14 = GLconst.TEXTURE14,
Unit15 = GLconst.TEXTURE15,
}
#pragma warning disable CA1008
public enum Wrap
#pragma warning restore CA1008
{
Repeat = GLconst.REPEAT,
MirroredRepeat = GLconst.MIRRORED_REPEAT,
ClampToEdge = GLconst.CLAMP_TO_EDGE,
ClampToBorder = GLconst.CLAMP_TO_BORDER,
}
}
@@ -0,0 +1,80 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="VAO.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and
// this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”,
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
namespace StgSharp.Graphics.GL
{
public unsafe class VertexArray : BufferObjectBase
{
internal VertexArray(int n, glRenderStream binding)
{
this.binding = binding;
_bufferHandle = glFunc.GenVertexArrays(n);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override sealed void Bind(int index)
{
glFunc.BindVertexArray(_bufferHandle[index]);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void BindNull()
{
glFunc.BindVertexArray(glHandle.Zero);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetVertexAttribute(uint index, int vertexLength, TypeCode dataType, bool isNomalized, uint stride, int pointer)
{
glFunc.SetVertexAttribute(
index, vertexLength, dataType,
isNomalized,
stride, pointer);
}
protected override sealed void Dispose(bool disposing)
{
if (disposing)
{
glFunc.DeleteVertexArrays(_bufferHandle);
_bufferHandle.Release();
}
}
}
}
@@ -0,0 +1,155 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="VBO.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and
// this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”,
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
using StgSharp.Graphics.GL;
using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.Net.Security;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace StgSharp.Graphics.GL
{
public unsafe class VertexBuffer : BufferObjectBase
{
internal VertexBuffer(int n, glRenderStream binding)
{
this.binding = binding;
_bufferHandle = glFunc.GenBuffers(n);
}
public override sealed void Bind(int index)
{
glFunc.BindBuffer(BufferType.ArrayBuffer, _bufferHandle[index]);
}
/// <summary>
/// Set data to current vertex buffer object
/// </summary>
/// <typeparam name="T">Type of bufferData</typeparam>
/// <param name="index">Index to find certain VBO in this instance</param>
/// <param name="bufferData">Data to write in</param>
/// <param name="usage">How OpenGL use these data, defined by <see cref="BufferUsage"/></param>
public void WriteMatrixData<T>(int index, T bufferData, BufferUsage usage)
where T : struct, IMat
{
glFunc.BindBuffer(BufferType.ArrayBuffer, _bufferHandle[index]);
glFunc.SetBufferData(BufferType.ArrayBuffer, bufferData, usage);
}
/// <summary>
/// Set data to current vertex buffer object
/// </summary>
/// <typeparam name="T">Type of bufferData</typeparam>
/// <param name="index">Index to find certain VBO in this instance</param>
/// <param name="bufferArray">Data to write in</param>
/// <param name="usage">How OpenGL use these data, defined by <see cref="BufferUsage"/></param>
public void WriteScalerData<T>(int index, T[] bufferArray, BufferUsage usage)
where T : struct,
#if NET8_0_OR_GREATER
INumber
#else
IConvertible
#endif
{
glFunc.BindBuffer(BufferType.ArrayBuffer, _bufferHandle[index]);
glFunc.SetBufferData(BufferType.ArrayBuffer, bufferArray, usage);
}
/// <summary>
/// Set data to current vertex buffer object
/// </summary>
/// <typeparam name="T">Type of bufferData</typeparam>
/// <param name="index">Index to find certain VBO in this instance</param>
/// <param name="scalerSpan">A <see cref="Span{T}"/> of data to write in</param>
/// <param name="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 : struct,
#if NET8_0_OR_GREATER
INumber
#else
IConvertible
#endif
{
glFunc.BindBuffer(BufferType.ArrayBuffer, _bufferHandle[index]);
glFunc.SetBufferData(BufferType.ArrayBuffer, scalerSpan, usage);
}
/// <summary>
/// Set data to current vertex buffer object
/// </summary>
/// <typeparam name="T">Type of bufferData</typeparam>
/// <param name="index">Index to find certain VBO in this instance</param>
/// <param name="vectorSpan">A <see cref="Span{T}"/> of data to write in</param>
/// <param name="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 : struct, IVector
{
glFunc.BindBuffer(BufferType.ArrayBuffer, _bufferHandle[index]);
glFunc.SetBufferVectorData(BufferType.ArrayBuffer, vectorSpan, usage);
}
/// <summary>
/// Set data to current vertex buffer object
/// </summary>
/// <typeparam name="T">Type of bufferData</typeparam>
/// <param name="index">Index to find certain VBO in this instance</param>
/// <param name="vectorArray">A array of data to write in</param>
/// <param name="usage">How OpenGL use these data, defined by <see cref="BufferUsage"/></param>
public void WriteVectorData<T>(int index, T[] vectorArray, BufferUsage usage) where T : struct, IVector
{
glFunc.BindBuffer(BufferType.ArrayBuffer, _bufferHandle[index]);
glFunc.SetBufferVectorData(BufferType.ArrayBuffer, vectorArray, usage);
}
/// <summary>
///
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Unbind()
{
glFunc.BindBuffer(BufferType.ArrayBuffer, glHandle.Zero);
}
protected override sealed void Dispose(bool disposing)
{
if (disposing)
{
glFunc.DeleteBuffers(_bufferHandle);
_bufferHandle.Release();
}
}
}
}