daily update on 20240501 by nitload youmu, tracking blueprint coruptr

This commit is contained in:
YoumuLiu
2024-05-02 05:47:27 +08:00
parent 0cd01ed9dd
commit 6324e578ab
60 changed files with 2564 additions and 307 deletions
+44 -34
View File
@@ -42,25 +42,35 @@ namespace StgSharp.Graphics
{
internal static unsafe partial class GL
{
private static IntPtr api;
internal static GLcontext* api;
private static ref GLcontext Context
{
get { return ref *(GLcontext*)api; }
}
public static IntPtr CurrentContextHandle
{
get { return (IntPtr)api; }
set { api = value; }
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ActiveTexture(uint type)
{
api->glActiveTexture(type);
Context.glActiveTexture(type);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AttachShader(glHandle program, glHandle shader)
{
api->glAttachShader(program.Value, shader.Value);
Context.glAttachShader(program.Value, shader.Value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void BindBuffer(BufferType bufferType, glHandle handle)
{
api->glBindBuffer((uint)bufferType, handle.Value);
Context.glBindBuffer((uint)bufferType, handle.Value);
}
/// <summary>
@@ -72,13 +82,13 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void BindFrameBuffer(FrameBufferTarget target, glHandle handle)
{
api->glBindFramebuffer((uint)target, handle.Value);
Context.glBindFramebuffer((uint)target, handle.Value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void BindTexture(uint type, glHandle texture)
{
api->glBindTexture(type, texture.Value);
Context.glBindTexture(type, texture.Value);
}
@@ -86,43 +96,43 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void BindVertexArray(glHandle handle)
{
api->glBindVertexArray(handle.Value);
Context.glBindVertexArray(handle.Value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Clear(MaskBufferBit mask)
{
api->glClear((uint)mask);
Context.glClear((uint)mask);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void ClearColor(float R, float G, float B, float A)
{
api->glClearColor(R, G, B, A);
Context.glClearColor(R, G, B, A);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void ClearGraphicError()
{
while (api->glGetError() != 0) { }
while (Context.glGetError() != 0) { }
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CompileShader(glHandle shaderhandle)
{
api->glCompileShader(shaderhandle.Value);
Context.glCompileShader(shaderhandle.Value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe glHandle CreateGraphicProgram()
{
return glHandle.Alloc(api->glCreateProgram());
return glHandle.Alloc(Context.glCreateProgram());
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static glHandle CreateProgram()
{
return glHandle.Alloc(api->glCreateProgram());
return glHandle.Alloc(Context.glCreateProgram());
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -131,7 +141,7 @@ namespace StgSharp.Graphics
glHandleSet h = new glHandleSet(count);
for (int i = 0; i < count; i++)
{
h.SetValue(i, api->glCreateProgram());
h.SetValue(i, Context.glCreateProgram());
}
return h;
}
@@ -143,10 +153,10 @@ namespace StgSharp.Graphics
uint t = (uint)type;
for (int i = 0; i < count; i++)
{
uint id = api->glCreateShader(t);
uint id = Context.glCreateShader(t);
if (id == 0)
{
uint error = api->glGetError();
uint error = Context.glGetError();
InternalIO.InternalWriteLog($"Failed in creating shader: {error}", LogType.Error);
throw new Exception();
}
@@ -184,20 +194,20 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void DeleteBuffer(glHandle handle)
{
api->glDeleteBuffers(1, (uint*)handle.Handle);
Context.glDeleteBuffers(1, (uint*)handle.Handle);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void DeleteBuffers(glHandleSet handle)
{
api->glDeleteBuffers(handle.length, (uint*)handle.Handle);
Context.glDeleteBuffers(handle.length, (uint*)handle.Handle);
handle.Release();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void DeleteVertexArrays(glHandleSet arrays)
{
api->glDeleteVertexArrays(arrays.length, (uint*)arrays.Handle);
Context.glDeleteVertexArrays(arrays.length, (uint*)arrays.Handle);
arrays.Release();
}
@@ -225,20 +235,20 @@ namespace StgSharp.Graphics
LogType.Error);
return;
}
api->glDrawElements((uint)mode, count, a, (void*)ptr);
Context.glDrawElements((uint)mode, count, a, (void*)ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Enable(GLOperation operation)
{
api->glEnable((uint)operation);
Context.glEnable((uint)operation);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe glHandleSet GenBuffers(int count)
{
glHandleSet h = new glHandleSet(count);
api->glGenBuffers(count, (uint*)h.Handle);
Context.glGenBuffers(count, (uint*)h.Handle);
return h;
}
@@ -246,7 +256,7 @@ namespace StgSharp.Graphics
public static unsafe glHandleSet GenTextures(int count)
{
glHandleSet h = new glHandleSet(count);
api->glGenTextures(count, (uint*)h.Handle);
Context.glGenTextures(count, (uint*)h.Handle);
return h;
}
@@ -254,7 +264,7 @@ namespace StgSharp.Graphics
public static unsafe glHandleSet GenVertexArrays(int count)
{
glHandleSet h = new glHandleSet(count);
api->glGenVertexArrays(count, (uint*)h.Handle);
Context.glGenVertexArrays(count, (uint*)h.Handle);
return h;
}
@@ -264,7 +274,7 @@ namespace StgSharp.Graphics
byte[] code = Encoding.UTF8.GetBytes(name);
fixed (byte* ptr = code)
{
return glSHandle.Alloc(api->glGetUniformLocation(program.Value, ptr));
return glSHandle.Alloc(Context.glGetUniformLocation(program.Value, ptr));
}
}
@@ -274,7 +284,7 @@ namespace StgSharp.Graphics
byte[] stream = Encoding.UTF8.GetBytes(codeStream);
fixed (byte* streamPtr = stream)
{
api->glShaderSource(shaderHandle.Value, 1, &streamPtr, (void*)0);
Context.glShaderSource(shaderHandle.Value, 1, &streamPtr, (void*)0);
}
}
@@ -294,7 +304,7 @@ namespace StgSharp.Graphics
{
int size = Marshal.SizeOf<T>();
T* p = &bufferData;
api->glBufferData(
Context.glBufferData(
(uint)bufferType,
(IntPtr)size,
(IntPtr)p,
@@ -311,7 +321,7 @@ namespace StgSharp.Graphics
}
fixed (void* bufferPtr = bufferArray)
{
api->glBufferData(
Context.glBufferData(
(uint)bufferType,
(IntPtr)size,
(IntPtr)bufferPtr,
@@ -323,14 +333,14 @@ namespace StgSharp.Graphics
public static void SetVertexAttribute(uint index, int size, TypeCode t, bool normalized, uint stride, int pointer)
{
uint type = InternalIO.GLtype[t];
api->glVertexAttribPointer(index, size, type, normalized ? 1 : 0, stride, (void*)pointer);
api->glEnableVertexAttribArray(index);
Context.glVertexAttribPointer(index, size, type, normalized ? 1 : 0, stride, (void*)pointer);
Context.glEnableVertexAttribArray(index);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void TextureImage2d<T>(int target, int level, uint internalformat, uint width, uint height, uint format, uint type, glHandle pixels)
{
api->glTexImage2D(
Context.glTexImage2D(
(uint)target, level, internalformat, width, height, 0, format, type, pixels.valuePtr
);
}
@@ -338,19 +348,19 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void TextureParameter(int target, uint pname, int param)
{
api->glTexParameteri((uint)target, pname, param);
Context.glTexParameteri((uint)target, pname, param);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void UseProgram(glHandle program)
{
api->glUseProgram(program.Value);
Context.glUseProgram(program.Value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Viewport(int x, int y, uint width, uint height)
{
api->glViewport(x, y, width, height);
Context.glViewport(x, y, width, height);
}
}
@@ -73,7 +73,7 @@ namespace StgSharp.Graphics
internal unsafe Shader(glHandleSet handle, ShaderType usage)
{
this.handle = handle;
context = (IntPtr)GL.api;
context = GL.CurrentContextHandle;
type = usage;
}
@@ -137,7 +137,7 @@ namespace StgSharp.Graphics
internal unsafe Uniform(
ShaderProgram program, string name)
{
this.binding = (IntPtr)GL.api;
this.binding = GL.CurrentContextHandle;
id = GL.GetUniformLocation(program.handle, name);
}
@@ -181,7 +181,7 @@ namespace StgSharp.Graphics
internal unsafe Uniform(
ShaderProgram program, string name)
{
this.binding = (IntPtr)GL.api;
this.binding = GL.CurrentContextHandle;
id = GL.GetUniformLocation(program.handle, name);
}
@@ -203,51 +203,51 @@ namespace StgSharp.Graphics
public unsafe static void SetValue(Uniform<Matrix44> uniform, Matrix44 mat)
{
Matrix44 temp = mat;
api->glUniformMatrix4fv(uniform.id.Value, 1, false, (float*)&mat);
Context.glUniformMatrix4fv(uniform.id.Value, 1, false, (float*)&mat);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static void SetValue(Uniform<float, float, float, float> uniform, vec4d vec)
{
vec4d temp = vec;
api->glUniform1fv(uniform.id.Value , 4, &temp);
Context.glUniform1fv(uniform.id.Value , 4, &temp);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static void SetValue(Uniform<vec4d> uniform, vec4d vec)
{
vec4d temp = vec;
api->glUniform1fv(uniform.id.Value , 4, &temp);
Context.glUniform1fv(uniform.id.Value , 4, &temp);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void SetValue(Uniform<float> uniform, float v0)
{
api->glUniform1f(uniform.id.Value , v0);
Context.glUniform1f(uniform.id.Value , v0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void SetValue(Uniform<int> uniform, int i0)
{
api->glUniform1i(uniform.id.Value , i0);
Context.glUniform1i(uniform.id.Value , i0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void SetValue(Uniform<float, float> uniform, float v0, float v1)
{
api->glUniform2f(uniform.id.Value , v0, v1);
Context.glUniform2f(uniform.id.Value , v0, v1);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void SetValue(Uniform<float, float, float> uniform, float v0, float v1, float v2)
{
api->glUniform3f(uniform.id.Value , v0, v1, v2);
Context.glUniform3f(uniform.id.Value , v0, v1, v2);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void SetValue(Uniform<float, float, float, float> uniform, float v0, float v1, float v2, float v3)
{
api->glUniform4f(uniform.id.Value , v0, v1, v2, v3);
Context.glUniform4f(uniform.id.Value , v0, v1, v2, v3);
}
}
+20 -20
View File
@@ -63,7 +63,7 @@ namespace StgSharp.Graphics
public static unsafe void LoadGLapi(this glRenderStream stream)
{
GLcontext* context = (GLcontext*)stream.ContextHandle;
GLfuncLoader loader = InternalIO.glfwGetProcAddress;
delegate*<string,IntPtr> loader = &InternalIO.glfwGetProcAddress;
#pragma warning disable CS8909
if (((GLcontext*)stream.ContextHandle)->glGetString != (delegate*<uint, IntPtr>)IntPtr.Zero)
#pragma warning restore CS8909
@@ -999,7 +999,7 @@ namespace StgSharp.Graphics
}
}
internal static unsafe void LoadGLcore10(GLcontext* contextPtr, GLfuncLoader load)
internal static unsafe void LoadGLcore10(GLcontext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core10)
{
@@ -1063,7 +1063,7 @@ namespace StgSharp.Graphics
InternalIO.InternalAppendLog("Successfully load all OpenGL 1.0 APIs");
}
internal static unsafe void LoadGLcore11(GLcontext* contextPtr, GLfuncLoader load)
internal static unsafe void LoadGLcore11(GLcontext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core11)
{
@@ -1093,7 +1093,7 @@ namespace StgSharp.Graphics
InternalIO.InternalAppendLog("Successfully load all OpenGL 1.1 APIs");
}
internal static unsafe void LoadGLcore12(GLcontext* contextPtr, GLfuncLoader load)
internal static unsafe void LoadGLcore12(GLcontext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core12)
{
@@ -1113,7 +1113,7 @@ namespace StgSharp.Graphics
InternalIO.InternalAppendLog("Successfully load all OpenGL 1.2 APIs");
}
internal static unsafe void LoadGLcore13(GLcontext* contextPtr, GLfuncLoader load)
internal static unsafe void LoadGLcore13(GLcontext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core13)
{
@@ -1138,7 +1138,7 @@ namespace StgSharp.Graphics
InternalIO.InternalAppendLog("Successfully load all OpenGL 1.3 APIs");
}
internal static unsafe void LoadGLcore14(GLcontext* contextPtr, GLfuncLoader load)
internal static unsafe void LoadGLcore14(GLcontext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core14)
{
@@ -1163,7 +1163,7 @@ namespace StgSharp.Graphics
InternalIO.InternalAppendLog("Successfully load all OpenGL 1.4 APIs");
}
internal static unsafe void LoadGLcore15(GLcontext* contextPtr, GLfuncLoader load)
internal static unsafe void LoadGLcore15(GLcontext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core15)
{
@@ -1198,7 +1198,7 @@ namespace StgSharp.Graphics
InternalIO.InternalAppendLog("Successfully load all OpenGL 1.5 APIs");
}
internal static unsafe void LoadGLcore20(GLcontext* contextPtr, GLfuncLoader load)
internal static unsafe void LoadGLcore20(GLcontext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core20)
{
@@ -1307,7 +1307,7 @@ namespace StgSharp.Graphics
InternalIO.InternalAppendLog("Successfully load all OpenGL 2.0 APIs");
}
internal static unsafe void LoadGLcore21(GLcontext* contextPtr, GLfuncLoader load)
internal static unsafe void LoadGLcore21(GLcontext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core21)
{
@@ -1329,7 +1329,7 @@ namespace StgSharp.Graphics
InternalIO.InternalAppendLog("Successfully load all OpenGL 2.1 APIs");
}
internal static unsafe void LoadGLcore30(GLcontext* contextPtr, GLfuncLoader load)
internal static unsafe void LoadGLcore30(GLcontext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core30)
{
@@ -1429,7 +1429,7 @@ namespace StgSharp.Graphics
InternalIO.InternalAppendLog("Successfully load all OpenGL 3.0 APIs");
}
internal static unsafe void LoadGLcore31(GLcontext* contextPtr, GLfuncLoader load)
internal static unsafe void LoadGLcore31(GLcontext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core31)
{
@@ -1460,7 +1460,7 @@ namespace StgSharp.Graphics
InternalIO.InternalAppendLog("Successfully load all OpenGL 3.1 APIs");
}
internal static unsafe void LoadGLcore32(GLcontext* contextPtr, GLfuncLoader load)
internal static unsafe void LoadGLcore32(GLcontext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core32)
{
@@ -1495,7 +1495,7 @@ namespace StgSharp.Graphics
InternalIO.InternalAppendLog("Successfully load all OpenGL 3.2 APIs");
}
internal static unsafe void LoadGLcore33(GLcontext* contextPtr, GLfuncLoader load)
internal static unsafe void LoadGLcore33(GLcontext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core33)
{
@@ -1539,7 +1539,7 @@ namespace StgSharp.Graphics
InternalIO.InternalAppendLog("Successfully load all OpenGL 3.3 APIs");
}
internal static unsafe void LoadGLcore40(GLcontext* contextPtr, GLfuncLoader load)
internal static unsafe void LoadGLcore40(GLcontext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core40)
{
@@ -1601,7 +1601,7 @@ namespace StgSharp.Graphics
InternalIO.InternalAppendLog("Successfully load all OpenGL 4.0 APIs");
}
internal static unsafe void LoadGLcore41(GLcontext* contextPtr, GLfuncLoader load)
internal static unsafe void LoadGLcore41(GLcontext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core41)
{
@@ -1705,7 +1705,7 @@ namespace StgSharp.Graphics
InternalIO.InternalAppendLog("Successfully load all OpenGL 4.1 APIs");
}
internal static unsafe void LoadGLcore42(GLcontext* contextPtr, GLfuncLoader load)
internal static unsafe void LoadGLcore42(GLcontext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core42)
{
@@ -1733,7 +1733,7 @@ namespace StgSharp.Graphics
InternalIO.InternalAppendLog("Successfully load all OpenGL 4.2 APIs");
}
internal static unsafe void LoadGLcore43(GLcontext* contextPtr, GLfuncLoader load)
internal static unsafe void LoadGLcore43(GLcontext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core43)
{
@@ -1793,7 +1793,7 @@ namespace StgSharp.Graphics
InternalIO.InternalAppendLog("Successfully load all OpenGL 4.3 APIs");
}
internal static unsafe void LoadGLcore44(GLcontext* contextPtr, GLfuncLoader load)
internal static unsafe void LoadGLcore44(GLcontext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core44)
{
@@ -1818,7 +1818,7 @@ namespace StgSharp.Graphics
InternalIO.InternalAppendLog("Successfully load all OpenGL 4.4 APIs");
}
internal static unsafe void LoadGLcore45(GLcontext* contextPtr, GLfuncLoader load)
internal static unsafe void LoadGLcore45(GLcontext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core45)
{
@@ -1944,7 +1944,7 @@ namespace StgSharp.Graphics
InternalIO.InternalAppendLog("Successfully load all OpenGL 4.5 APIs");
}
internal static unsafe void LoadGLcore46(GLcontext* contextPtr, GLfuncLoader load)
internal static unsafe void LoadGLcore46(GLcontext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core46)
{
@@ -1,4 +1,5 @@
using StgSharp.Math;
using StgSharp.Logic;
using StgSharp.Math;
using System;
using System.Collections.Generic;
@@ -12,6 +13,20 @@ namespace StgSharp.Graphics
{
public abstract partial class glRenderStream: RenderStream
{
public static T AsMainStreamOf<T>(Form f, (int x, int y, int z) unitCubeSize) where T : glRenderStream
{
RenderStreamConstructArgs args = new RenderStreamConstructArgs();
args.UnitCubeSize = unitCubeSize;
args.Name = f.name;
args.Height = f.Height;
args.Width = f.Width;
T ret = Activator.CreateInstance(typeof(T), args) as T;
ret!.InternalInit();
f.WindowID = ret.CanvasHandle;
return ret;
}
protected sealed override void NativeCameraViewTarget(vec3d position, vec3d direction, vec3d up)
{
NativeCamera.SetViewDirection(position, direction, up);
@@ -53,34 +68,18 @@ namespace StgSharp.Graphics
/// </exception>
public unsafe void MakeAsCurrentContext()
{
if (CanvasHandle == default)
if (ContextHandle == default)
{
IntPtr windowPtr;
byte[] tittleBytes = Encoding.UTF8.GetBytes(name);
if (SharedCanvas == IntPtr.Zero)
{
windowPtr = InternalIO.glfwCreateWindow(Width, Height, tittleBytes, Monitor, IntPtr.Zero);
}
else
{
windowPtr = InternalIO.glfwCreateWindow(Width, Height, tittleBytes, Monitor, SharedCanvas);
}
if (((long)windowPtr == -1) || ((long)windowPtr == -2))
{
throw new Exception("Cannot create window instance!");
}
else
{
CanvasHandle = windowPtr;
}
ContextHandle = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(GLcontext)));
GLcontext* contextID = (GLcontext*)ContextHandle;
*contextID = new GLcontext();
contextID->glGetString = (delegate*<uint, IntPtr>)IntPtr.Zero;
}
GL.api = (GLcontext*)ContextHandle;
InternalIO.glfwMakeContextCurrent(CanvasHandle);
if (GL.CurrentContextHandle != ContextHandle)
{
GL.CurrentContextHandle = ContextHandle;
Console.WriteLine($"context set as {(ulong)GL.CurrentContextHandle}");
InternalIO.glfwMakeContextCurrent(CanvasHandle);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -96,10 +95,12 @@ namespace StgSharp.Graphics
Init();
}
internal sealed override void InternalDraw()
internal sealed override void InternalDraw(
in Dictionary<string, BlueprintPipeline> input,
in Dictionary<string, BlueprintPipeline> output)
{
MakeAsCurrentContext();
Main();
Main(in input, in output);
}