daily update on 20240513 by nitload youmu, instancing render added

This commit is contained in:
YoumuLiu
2024-05-13 03:39:54 +08:00
parent 56459deec5
commit a3506c96a3
78 changed files with 2562 additions and 1098 deletions
+55 -3
View File
@@ -61,7 +61,8 @@ namespace StgSharp.Graphics
/// <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 SetValue<T>(int index, T bufferData, BufferUsage usage) where T : struct, IMat
public void WriteMatrixData<T>(int index, T bufferData, BufferUsage usage)
where T : struct, IMat
{
GL.BindBuffer(BufferType.ARRAY_BUFFER, _bufferHandle[index]);
GL.SetBufferData(BufferType.ARRAY_BUFFER, bufferData, usage);
@@ -72,14 +73,65 @@ namespace StgSharp.Graphics
/// </summary>
/// <typeparam name="T">Type of bufferData</typeparam>
/// <param name="index">Index to find certain VBO in this instance</param>
/// <param name="bufferArray">A array of data to write in</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 SetValueArray<T>(int index, T[] bufferArray, BufferUsage usage) where T : struct, IConvertible
public void WriteScalerData<T>(int index, T[] bufferArray, BufferUsage usage)
where T : struct,
#if NET8_0_OR_GREATER
INumber
#else
IConvertible
#endif
{
GL.BindBuffer(BufferType.ARRAY_BUFFER, _bufferHandle[index]);
GL.SetBufferData(BufferType.ARRAY_BUFFER, 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, Span<T> scalerSpan, BufferUsage usage)
where T : struct,
#if NET8_0_OR_GREATER
INumber
#else
IConvertible
#endif
{
GL.BindBuffer(BufferType.ARRAY_BUFFER, _bufferHandle[index]);
GL.SetBufferData(BufferType.ARRAY_BUFFER, 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, Span<T> vectorSpan, BufferUsage usage) where T : struct, IVector
{
GL.BindBuffer(BufferType.ARRAY_BUFFER, _bufferHandle[index]);
GL.SetBufferVectorData(BufferType.ARRAY_BUFFER, 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
{
GL.BindBuffer(BufferType.ARRAY_BUFFER, _bufferHandle[index]);
GL.SetBufferVectorData(BufferType.ARRAY_BUFFER, vectorArray, usage);
}
/// <summary>
///
/// </summary>
+137 -2
View File
@@ -28,6 +28,7 @@
//
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
using StgSharp.Graphics;
using StgSharp.Logic;
using StgSharp.Math;
@@ -38,6 +39,15 @@ using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace StgSharp
{
internal static unsafe partial class InternalIO
{
[DllImport(SSC_libName, EntryPoint = "glCheckShaderStat",
CallingConvention = CallingConvention.Cdecl)]
internal static extern int glCheckShaderStatus(ref GLcontext context, uint shaderHandle,int key, ref IntPtr logPtr);
}
}
namespace StgSharp.Graphics
{
internal static unsafe partial class GL
@@ -86,6 +96,16 @@ namespace StgSharp.Graphics
Context.glBindBuffer((uint)bufferType, handle.Value);
}
public static string GetShaderStatus(Shader s, int index, int key)
{
IntPtr sptr = IntPtr.Zero;
if (InternalIO.glCheckShaderStatus(ref Context, s.handle[index].Value, key,ref sptr) == 0)
{
return Marshal.PtrToStringAnsi(sptr);
}
return "";
}
/// <summary>
/// Bind a frame buffer to a frame buffer target
/// Same function as <see href="https://docs.gl/gl3/glBindFramebuffer">glBindFramebuffer</see>.
@@ -105,13 +125,18 @@ namespace StgSharp.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void BindVertexArray(glHandle handle)
{
Context.glBindVertexArray(handle.Value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void VertexAttributeDivisor(uint layoutIndex, uint divisor)
{
Context.glVertexAttribDivisor(layoutIndex, divisor);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Clear(MaskBufferBit mask)
{
@@ -225,7 +250,7 @@ namespace StgSharp.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void DrawElements(GepmetryType mode, uint count, TypeCode type, IntPtr ptr)
public static unsafe void DrawElements(GeometryType mode, uint count, TypeCode type, IntPtr ptr)
{
uint a;
switch (type)
@@ -251,6 +276,35 @@ namespace StgSharp.Graphics
Context.glDrawElements((uint)mode, count, a, (void*)ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void DrawElementsInstanced(
GeometryType mode , uint count, TypeCode type, IntPtr ptr, uint amount
)
{
uint a;
switch (type)
{
case TypeCode.UInt32:
a = GLconst.UNSIGNED_INT;
break;
case TypeCode.UInt16:
a = GLconst.UNSIGNED_SHORT;
break;
case TypeCode.Byte:
a = GLconst.UNSIGNED_BYTE;
break;
default:
InternalIO.InternalWriteLog(
$"{$"Parameter error in parameter {nameof(type)} method DrawElements. "}{$"Only {typeof(uint).Name},{typeof(ushort).Name},{typeof(byte).Name} types are supported."}",
LogType.Error);
return;
}
Context.glDrawElementsInstanced((uint)mode, count, a, (void*)ptr,amount);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Enable(GLOperation operation)
{
@@ -327,9 +381,12 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void SetBufferData<T>(BufferType bufferType, T bufferData, BufferUsage usage)
where T : struct
{
int size = Marshal.SizeOf<T>();
#pragma warning disable CS8500
T* p = &bufferData;
#pragma warning restore CS8500
Context.glBufferData(
(uint)bufferType,
(IntPtr)size,
@@ -339,13 +396,91 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void SetBufferData<T>(BufferType bufferType, T[] bufferArray, BufferUsage usage)
where T : struct,
#if NET8_0_OR_GREATER
INumber
#else
IConvertible
#endif
{
int size = bufferArray.Length * Marshal.SizeOf(typeof(T));
if (bufferArray == null)
{
return;
}
#pragma warning disable CS8500
fixed (void* bufferPtr = bufferArray)
#pragma warning restore CS8500
{
Context.glBufferData(
(uint)bufferType,
(IntPtr)size,
(IntPtr)bufferPtr,
(uint)usage);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void SetBufferData<T>(BufferType bufferType, Span<T> bufferArray, BufferUsage usage)
where T : struct,
#if NET8_0_OR_GREATER
INumber
#else
IConvertible
#endif
{
int size = bufferArray.Length * Marshal.SizeOf(typeof(T));
if (bufferArray == null)
{
return;
}
#pragma warning disable CS8500
fixed (void* bufferPtr = &bufferArray.GetPinnableReference())
#pragma warning restore CS8500
{
Context.glBufferData(
(uint)bufferType,
(IntPtr)size,
(IntPtr)bufferPtr,
(uint)usage);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void SetBufferVectorData<T>(BufferType bufferType, Span<T> bufferArray, BufferUsage usage)
where T : struct, IVector
{
int size = bufferArray.Length * Marshal.SizeOf(typeof(T));
if (bufferArray == null)
{
return;
}
#pragma warning disable CS8500
fixed (void* bufferPtr = &bufferArray.GetPinnableReference())
#pragma warning restore CS8500
{
Context.glBufferData(
(uint)bufferType,
(IntPtr)size,
(IntPtr)bufferPtr,
(uint)usage);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void SetBufferVectorData<T>(BufferType bufferType, T[] bufferArray, BufferUsage usage)
where T : struct, IVector
{
int size = bufferArray.Length * Marshal.SizeOf(typeof(T));
if (bufferArray == null)
{
return;
}
#pragma warning disable CS8500
fixed (void* bufferPtr = bufferArray)
#pragma warning restore CS8500
{
Context.glBufferData(
(uint)bufferType,
@@ -33,9 +33,11 @@ using StgSharp.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml.Linq;
namespace StgSharp.Graphics
{
@@ -85,6 +87,16 @@ namespace StgSharp.Graphics
GL.AttachShader(target.handle, handle[index]);
}
public static string FromCodeFile(string fileRoute)
{
string str = File.ReadAllText(fileRoute);
if (str == "")
{
throw new Exception($"Cannot load and GLSL code from {fileRoute}");
}
return str;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void Compile(int index, string code)
{
@@ -94,6 +106,7 @@ namespace StgSharp.Graphics
}
GL.LoadShaderSource(handle[index], $"{code}\0");
GL.CompileShader(handle[index]);
CheckStatus(index,ShaderStatus.CompileStatus);
}
public override string ToString()
@@ -107,6 +120,15 @@ namespace StgSharp.Graphics
throw new NotImplementedException();
}
public void CheckStatus(int index, ShaderStatus status)
{
string log = GL.GetShaderStatus(this,index, (int)status);
if (log != "")
{
throw new Exception($"Shader Error\n{status}\n" + log);
}
}
public override byte[] GetBytes(out int length)
{
throw new NotImplementedException();
@@ -118,6 +140,11 @@ namespace StgSharp.Graphics
}
}
public enum ShaderStatus : int
{
CompileStatus = GLconst.COMPILE_STATUS,
}
public class ShaderProgram
{
+105 -47
View File
@@ -1,4 +1,34 @@
using StgSharp.Logic;
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="glRenderStream.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.Logic;
using StgSharp.Math;
using System;
@@ -11,12 +41,19 @@ using System.Threading.Tasks;
namespace StgSharp.Graphics
{
public abstract partial class glRenderStream: RenderStream
public abstract partial class glRenderStream : RenderStream
{
protected glRenderStream(RenderStreamConstructArgs args) : base(args)
{
}
protected IntPtr glContextHandle
{
get { return ContextHandle; }
set { ContextHandle = value; }
}
public static T AsMainStreamOf<T>(Form f, (int x, int y, int z) unitCubeSize) where T : glRenderStream
{
RenderStreamConstructArgs args = new RenderStreamConstructArgs();
@@ -30,38 +67,6 @@ namespace StgSharp.Graphics
return ret;
}
protected sealed override void NativeCameraViewTarget(vec3d position, vec3d direction, vec3d up)
{
NativeCamera.SetViewDirection(position, direction, up);
}
protected sealed override Uniform<Matrix44> NativeCameraUniform(ShaderProgram source, string name)
{
NativeCamera.GainAllUniforms(source, name);
return NativeCamera.convertedUniform;
}
protected IntPtr glContextHandle
{
get { return ContextHandle; }
set { ContextHandle = value; }
}
internal unsafe sealed override void CreateCanvas()
{
byte[] nameCode = Encoding.UTF8.GetBytes(name);
CanvasHandle = InternalIO.glfwCreateWindow(
Width,Height,nameCode,Monitor,IntPtr.Zero
);
NativeCamera = new Camera();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected sealed override void NativeCameraViewRange(Radius fieldOfRange,vec2d offset, (float frontDepth, float backDepth) viewDepth)
{
NativeCamera.SetViewRange(fieldOfRange,this.Size, offset, viewDepth);
}
/// <summary>
/// Init an OpenGl context if current form has not been binded to any context. and set the context binded to
/// current form as current OpenGL context.
@@ -90,7 +95,25 @@ namespace StgSharp.Graphics
{
InternalIO.glfwSetWindowSizeLimits(this.CanvasHandle, minWidth, minHeight, maxWidth, maxHeight);
}
internal sealed override void InternalInit()
internal override sealed unsafe void CreateCanvas()
{
byte[] nameCode = Encoding.UTF8.GetBytes(name);
CanvasHandle = InternalIO.glfwCreateWindow(
Width, Height, nameCode, Monitor, IntPtr.Zero
);
NativeCamera = new Camera();
}
internal override sealed void InternalDraw(
in Dictionary<string, BlueprintPipeline> input,
in Dictionary<string, BlueprintPipeline> output)
{
MakeAsCurrentContext();
ProcessInput();
Main(in input, in output);
}
internal override sealed void InternalInit()
{
CreateCanvas();
MakeAsCurrentContext();
@@ -98,21 +121,56 @@ namespace StgSharp.Graphics
Init();
}
internal sealed override void InternalDraw(
in Dictionary<string, BlueprintPipeline> input,
in Dictionary<string, BlueprintPipeline> output)
{
MakeAsCurrentContext();
ProcessInput();
Main(in input, in output);
InternalIO.glfwSwapBuffers(CanvasHandle);
InternalIO.glfwPollEvents();
}
internal sealed override void InternalTerminate()
internal override sealed void InternalTerminate()
{
Deinit();
}
protected override sealed Uniform<Matrix44> NativeCameraUniform(ShaderProgram source, string name)
{
NativeCamera.GainAllUniforms(source, name);
return NativeCamera.convertedUniform;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override sealed void NativeCameraViewRange(Radius fieldOfRange, vec2d offset, (float frontDepth, float backDepth) viewDepth)
{
NativeCamera.SetViewRange(fieldOfRange, this.Size, offset, viewDepth);
}
protected override sealed void NativeCameraViewTarget(vec3d position, vec3d direction, vec3d up)
{
NativeCamera.SetViewDirection(position, direction, up);
}
protected static void PollEvents()
{
InternalIO.glfwPollEvents();
}
protected void SwapBuffers()
{
InternalIO.glfwSwapBuffers(CanvasHandle);
}
#region Key Control
protected KeyStatus CheckKey(KeyboardKey key)
{
return InternalIO.glfwGetKey(this.CanvasHandle, (int)key);
}
protected KeyStatus CheckKey(Mouse key)
{
return InternalIO.glfwGetKey(this.CanvasHandle, (int)key);
}
protected KeyStatus CheckKey(Joystick key)
{
return InternalIO.glfwGetKey(this.CanvasHandle, (int)key);
}
#endregion
}
}