MVVM rebuild complete

This commit is contained in:
Nitload
2024-07-30 09:37:25 +08:00
parent da75200347
commit d082071f2d
68 changed files with 2690 additions and 762 deletions
@@ -7,6 +7,18 @@ using System.Text;
namespace StgSharp.Graphics.OpenGL
{
public sealed class GlExecutionException : Exception
{
public GlExecutionException()
{
}
public GlExecutionException(uint errorCode) : base($"Critical OpenGL error: {errorCode}")
{
}
}
public sealed class GlArrayFormatException : Exception
{
public GlArrayFormatException(PixelChannelLayout layout, string arrayname):
@@ -228,7 +228,7 @@ namespace StgSharp.Graphics.OpenGL
/// </summary>
public unsafe void Link()
{
if (InternalIO.InternalLinkShaderProgram((GLcontext*)this.GL.ContextHandle, handle.Value) == 0)
if (InternalIO.InternalLinkShaderProgram((OpenglContext*)this.GL.ContextHandle, handle.Value) == 0)
{
IntPtr logPtr = InternalIO.InternalReadSSCLog();
try
@@ -29,6 +29,9 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
using StgSharp.Math;
using StgSharp.MVVM;
using StgSharp.MVVM.View;
using System;
using System.Collections.Generic;
@@ -45,8 +48,7 @@ namespace StgSharp.Graphics.OpenGL
private const int glExtension = 0x1f03;
private const int glVersion = 0x1f02;
private static readonly string[] glBranch =
new string[] {
private static readonly string[] glBranch = {
"OpenGL ES-CM ",
"OpenGL ES-CL ",
"OpenGL ES ",
@@ -56,16 +58,14 @@ namespace StgSharp.Graphics.OpenGL
private static string glExtensionList;
/// <summary>
/// Load all supported OpenGL APIs and bind them to given <see cref="ViewPort"/> viewPortDisplay.
/// Load all supported OpenGL APIs and bind them to given <see cref="ViewPort"/>.
/// </summary>
/// <param name="vp">The <see cref="ViewPort"/> used as current OpenGL viewPortDisplay</param>
/// <param name="contextHandle">The handle to current OpenGL viewPortDisplay</param>
public static unsafe void LoadOpenGLApiTo(IntPtr contextHandle)
{
GLcontext* context = (GLcontext*)contextHandle;
OpenglContext* context = (OpenglContext*)contextHandle;
delegate*<string,IntPtr> loader = &InternalIO.glfwGetProcAddress;
#pragma warning disable CS8909
if (context->glGetString != (delegate*<uint, IntPtr>)IntPtr.Zero)
#pragma warning restore CS8909
if ((IntPtr)context->glGetString != IntPtr.Zero)
{
return;
}
@@ -84,7 +84,7 @@ namespace StgSharp.Graphics.OpenGL
{
InternalIO.InternalWriteLog("Failed to call glGetString", LogType.Error);
}
coreVersion = Marshal.PtrToStringAnsi(context->glGetString(glVersion));
coreVersion = Marshal.PtrToStringAnsi(context->glGetString(glVersion))??string.Empty;
foreach (string branch in glBranch)
{
@@ -132,20 +132,32 @@ namespace StgSharp.Graphics.OpenGL
}
}
IntPtr strPtr = context->glGetString(glExtension);
string extension;
if (strPtr == IntPtr.Zero)
uint code = context->glGetError();
Console.WriteLine(code);
if (code != 0)
{
throw new GlExecutionException(code);
}
int extensionCount;
context->glGetIntegerv(GLconst.NUM_EXTENSIONS,&extensionCount);
if (extensionCount == 0)
{
InternalIO.InternalWriteLog("No supported OpenGL extensions are found.", LogType.Info);
return;
}
else
List<string> extensions = new List<string>();
for (uint i = 0; i < extensionCount; i++)
{
extension = Marshal.PtrToStringAnsi(strPtr);
byte* strPtr = context->glGetStringi(GLconst.EXTENSIONS,i);
extensions.Add(Marshal.PtrToStringAnsi((IntPtr)strPtr));
}
List<string> extensions =
extension.Split(" ").ToList();
InternalIO.InternalWriteLog($"Find OpenGL extensions:" + Environment.NewLine +
$"{string.Join(';', extensions)}",
LogType.Info);
#region checkGLextensions
@@ -771,9 +783,11 @@ namespace StgSharp.Graphics.OpenGL
#endregion checkGLextensions
InternalIO.InternalWriteLog("Find OpenGL extensions.", LogType.Info);
}
//--------------------------------------------------------------------------------
/// <summary>
/// Get the function pointer of given OpenGL api name
/// </summary>
@@ -793,6 +807,11 @@ namespace StgSharp.Graphics.OpenGL
internal static void CheckCoreVersion(int majorVersion, int minorVersion)
{
if (majorVersion < 3)
{
throw new InvalidOperationException("StgSharp does not support OpenGL immediate mode.");
}
if ((majorVersion >= 1) || ((majorVersion == 1) && (minorVersion == 0)))
{
core10 = true;
@@ -971,7 +990,7 @@ namespace StgSharp.Graphics.OpenGL
}
}
internal static unsafe string GetExtensions(this GLcontext context)
internal static unsafe string GetExtensions(this OpenglContext context)
{
if (context.glGetString == null)
{
@@ -998,7 +1017,7 @@ namespace StgSharp.Graphics.OpenGL
}
}
internal static unsafe void LoadGLcore10(GLcontext* contextPtr, delegate*<string, IntPtr> load)
internal static unsafe void LoadGLcore10(OpenglContext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core10)
{
@@ -1062,7 +1081,7 @@ namespace StgSharp.Graphics.OpenGL
InternalIO.InternalAppendLog("Successfully load all OpenGL 1.0 APIs");
}
internal static unsafe void LoadGLcore11(GLcontext* contextPtr, delegate*<string, IntPtr> load)
internal static unsafe void LoadGLcore11(OpenglContext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core11)
{
@@ -1092,7 +1111,7 @@ namespace StgSharp.Graphics.OpenGL
InternalIO.InternalAppendLog("Successfully load all OpenGL 1.1 APIs");
}
internal static unsafe void LoadGLcore12(GLcontext* contextPtr, delegate*<string, IntPtr> load)
internal static unsafe void LoadGLcore12(OpenglContext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core12)
{
@@ -1112,7 +1131,7 @@ namespace StgSharp.Graphics.OpenGL
InternalIO.InternalAppendLog("Successfully load all OpenGL 1.2 APIs");
}
internal static unsafe void LoadGLcore13(GLcontext* contextPtr, delegate*<string, IntPtr> load)
internal static unsafe void LoadGLcore13(OpenglContext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core13)
{
@@ -1137,7 +1156,7 @@ namespace StgSharp.Graphics.OpenGL
InternalIO.InternalAppendLog("Successfully load all OpenGL 1.3 APIs");
}
internal static unsafe void LoadGLcore14(GLcontext* contextPtr, delegate*<string, IntPtr> load)
internal static unsafe void LoadGLcore14(OpenglContext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core14)
{
@@ -1162,7 +1181,7 @@ namespace StgSharp.Graphics.OpenGL
InternalIO.InternalAppendLog("Successfully load all OpenGL 1.4 APIs");
}
internal static unsafe void LoadGLcore15(GLcontext* contextPtr, delegate*<string, IntPtr> load)
internal static unsafe void LoadGLcore15(OpenglContext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core15)
{
@@ -1197,7 +1216,7 @@ namespace StgSharp.Graphics.OpenGL
InternalIO.InternalAppendLog("Successfully load all OpenGL 1.5 APIs");
}
internal static unsafe void LoadGLcore20(GLcontext* contextPtr, delegate*<string, IntPtr> load)
internal static unsafe void LoadGLcore20(OpenglContext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core20)
{
@@ -1306,7 +1325,7 @@ namespace StgSharp.Graphics.OpenGL
InternalIO.InternalAppendLog("Successfully load all OpenGL 2.0 APIs");
}
internal static unsafe void LoadGLcore21(GLcontext* contextPtr, delegate*<string, IntPtr> load)
internal static unsafe void LoadGLcore21(OpenglContext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core21)
{
@@ -1328,7 +1347,7 @@ namespace StgSharp.Graphics.OpenGL
InternalIO.InternalAppendLog("Successfully load all OpenGL 2.1 APIs");
}
internal static unsafe void LoadGLcore30(GLcontext* contextPtr, delegate*<string, IntPtr> load)
internal static unsafe void LoadGLcore30(OpenglContext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core30)
{
@@ -1428,7 +1447,7 @@ namespace StgSharp.Graphics.OpenGL
InternalIO.InternalAppendLog("Successfully load all OpenGL 3.0 APIs");
}
internal static unsafe void LoadGLcore31(GLcontext* contextPtr, delegate*<string, IntPtr> load)
internal static unsafe void LoadGLcore31(OpenglContext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core31)
{
@@ -1459,7 +1478,7 @@ namespace StgSharp.Graphics.OpenGL
InternalIO.InternalAppendLog("Successfully load all OpenGL 3.1 APIs");
}
internal static unsafe void LoadGLcore32(GLcontext* contextPtr, delegate*<string, IntPtr> load)
internal static unsafe void LoadGLcore32(OpenglContext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core32)
{
@@ -1494,7 +1513,7 @@ namespace StgSharp.Graphics.OpenGL
InternalIO.InternalAppendLog("Successfully load all OpenGL 3.2 APIs");
}
internal static unsafe void LoadGLcore33(GLcontext* contextPtr, delegate*<string, IntPtr> load)
internal static unsafe void LoadGLcore33(OpenglContext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core33)
{
@@ -1538,7 +1557,7 @@ namespace StgSharp.Graphics.OpenGL
InternalIO.InternalAppendLog("Successfully load all OpenGL 3.3 APIs");
}
internal static unsafe void LoadGLcore40(GLcontext* contextPtr, delegate*<string, IntPtr> load)
internal static unsafe void LoadGLcore40(OpenglContext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core40)
{
@@ -1600,7 +1619,7 @@ namespace StgSharp.Graphics.OpenGL
InternalIO.InternalAppendLog("Successfully load all OpenGL 4.0 APIs");
}
internal static unsafe void LoadGLcore41(GLcontext* contextPtr, delegate*<string, IntPtr> load)
internal static unsafe void LoadGLcore41(OpenglContext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core41)
{
@@ -1704,7 +1723,7 @@ namespace StgSharp.Graphics.OpenGL
InternalIO.InternalAppendLog("Successfully load all OpenGL 4.1 APIs");
}
internal static unsafe void LoadGLcore42(GLcontext* contextPtr, delegate*<string, IntPtr> load)
internal static unsafe void LoadGLcore42(OpenglContext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core42)
{
@@ -1732,7 +1751,7 @@ namespace StgSharp.Graphics.OpenGL
InternalIO.InternalAppendLog("Successfully load all OpenGL 4.2 APIs");
}
internal static unsafe void LoadGLcore43(GLcontext* contextPtr, delegate*<string, IntPtr> load)
internal static unsafe void LoadGLcore43(OpenglContext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core43)
{
@@ -1792,7 +1811,7 @@ namespace StgSharp.Graphics.OpenGL
InternalIO.InternalAppendLog("Successfully load all OpenGL 4.3 APIs");
}
internal static unsafe void LoadGLcore44(GLcontext* contextPtr, delegate*<string, IntPtr> load)
internal static unsafe void LoadGLcore44(OpenglContext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core44)
{
@@ -1817,7 +1836,7 @@ namespace StgSharp.Graphics.OpenGL
InternalIO.InternalAppendLog("Successfully load all OpenGL 4.4 APIs");
}
internal static unsafe void LoadGLcore45(GLcontext* contextPtr, delegate*<string, IntPtr> load)
internal static unsafe void LoadGLcore45(OpenglContext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core45)
{
@@ -1943,7 +1962,7 @@ namespace StgSharp.Graphics.OpenGL
InternalIO.InternalAppendLog("Successfully load all OpenGL 4.5 APIs");
}
internal static unsafe void LoadGLcore46(GLcontext* contextPtr, delegate*<string, IntPtr> load)
internal static unsafe void LoadGLcore46(OpenglContext* contextPtr, delegate*<string, IntPtr> load)
{
if (!core46)
{
@@ -37,6 +37,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Text;
@@ -48,19 +49,22 @@ namespace StgSharp
[DllImport(SSC_libName, EntryPoint = "glCheckShaderStat",
CallingConvention = CallingConvention.Cdecl)]
#pragma warning disable CA5392
internal static extern int glCheckShaderStatus(ref GLcontext context, uint shaderHandle, int key, ref IntPtr logPtr);
internal static extern int glCheckShaderStatus(ref OpenglContext context, uint shaderHandle, int key, ref IntPtr logPtr);
#pragma warning restore CA5392
}
}
namespace StgSharp.Graphics.OpenGL
{
public unsafe partial class GlFunction
{
private static Dictionary<IntPtr, GlFunction> contextToGLMap = new Dictionary<IntPtr, GlFunction>();
private GLcontext* _context;
private OpenglContext* _context;
private GlFunction()
{
@@ -70,7 +74,7 @@ namespace StgSharp.Graphics.OpenGL
internal IntPtr ContextHandle => (IntPtr)_context;
protected ref GLcontext Context
protected ref OpenglContext Context
{
get
{
@@ -156,6 +160,27 @@ namespace StgSharp.Graphics.OpenGL
while (Context.glGetError() != 0) { }
}
[Conditional("DEBUG")]
public void Assert(bool shouldReportWhenNormal)
{
try
{
uint code = Context.glGetError();
if (code != GLconst.NO_ERROR)
{
throw new GlExecutionException(code);
}
if (shouldReportWhenNormal)
{
Console.WriteLine("OpenGL runs normally");
}
}
catch (GlExecutionException ex)
{
throw ex;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void GenerateMipmap(Texture2DTarget target)
{
@@ -169,9 +194,9 @@ namespace StgSharp.Graphics.OpenGL
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CombineFrameBufferRenderBuffer(GlHandle fboHandle, GlHandle rboHandle)
public void CombineFrameBufferRenderBuffer(FrameBufferAttachment attachment, GlHandle rboHandle)
{
Context.glFramebufferRenderbuffer(GLconst.FRAMEBUFFER, fboHandle.Value, GLconst.RENDERBUFFER, rboHandle.Value);
Context.glFramebufferRenderbuffer(GLconst.FRAMEBUFFER, (uint)attachment, GLconst.RENDERBUFFER, rboHandle.Value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -708,7 +733,7 @@ namespace StgSharp.Graphics.OpenGL
{
return pack;
}
pack = new GlFunction { _context = (GLcontext*)handle };
pack = new GlFunction { _context = (OpenglContext*)handle };
contextToGLMap.Add(handle, pack);
return pack;
}
@@ -53,7 +53,7 @@ namespace StgSharp.Graphics.OpenGL
}
/// <summary>
/// Init an OpenGl viewPortDisplay if current form has not been binded to any viewPortDisplay.
/// Initialize an OpenGl viewPortDisplay if current form has not been binded to any viewPortDisplay.
/// and set the viewPortDisplay binded to current form as current OpenGL viewPortDisplay.
/// </summary>
/// <exception cref="Exception">
@@ -63,8 +63,8 @@ namespace StgSharp.Graphics.OpenGL
{
if (ContextHandle == default)
{
ContextHandle = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(GLcontext)));
GLcontext* contextID = (GLcontext*)ContextHandle;
ContextHandle = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OpenglContext)));
OpenglContext* contextID = (OpenglContext*)ContextHandle;
contextID->glGetString = (delegate*<uint, IntPtr>)IntPtr.Zero;
}
if (GL == null)
@@ -92,7 +92,7 @@ namespace StgSharp.Graphics.OpenGL
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal override sealed void PlatformSpecifiedInit()
internal override sealed void PlatformSpecifiedInitialize()
{
MakeAsCurrentContext();
glManager.LoadOpenGLApiTo(Port.GraphicHandle);