daily update on 20240305 by nitload youmu

This commit is contained in:
YoumuLiu
2024-03-05 10:17:02 +08:00
parent 1f2fca4c8f
commit 6a0cccae79
156 changed files with 23040 additions and 1385 deletions
@@ -1,36 +1,37 @@
using System;
using System.Runtime.InteropServices;
/**/
namespace StgSharp.Graphics
{
//rename types in style of GLFW
using GLFWbool = Boolean;
//strucs defined in GLFW3.h
#region GLFW3 struct
[StructLayout(LayoutKind.Sequential)]
public unsafe struct GLFWcontext
{
int client;
int source;
int major, minor, revision;
GLFWbool forward, debug, noerror;
int profile;
int robustness;
int release;
private int client;
private int source;
private int major, minor, revision;
private GLFWbool forward, debug, noerror;
private int profile;
private int robustness;
private int release;
//PFNGLGETSTRINGIPROC GetStringi;
IntPtr GetStringi;
private IntPtr GetStringi;
//PFNGLGETINTEGERVPROC GetIntegerv;
IntPtr GetIntegerv;
private IntPtr GetIntegerv;
//PFNGLGETSTRINGPROC GetString;
IntPtr GetString;
private IntPtr GetString;
/*
void (* makeCurrent) (GLFWwindow*);
@@ -41,12 +42,12 @@ namespace StgSharp.Graphics
void (* destroy) (GLFWwindow*);
*/
delegate*<GLFWwindow*> makeCurrent;
delegate*<GLFWwindow*> swapBuffer;
delegate*<int> swapInterval;
delegate*<byte*> extensionSupported;
delegate*<delegate*<void>, void> getProcAddress;
IntPtr destroy;
private delegate*<GLFWwindow*> makeCurrent;
private delegate*<GLFWwindow*> swapBuffer;
private delegate*<int> swapInterval;
private delegate*<byte*> extensionSupported;
private delegate*<delegate*<void>, void> getProcAddress;
private IntPtr destroy;
internal struct egl
{
@@ -55,24 +56,24 @@ namespace StgSharp.Graphics
EGLContext handle;
EGLSurface surface;
*/
void* config;
void* handle;
void* surface;
void* client;
private void* config;
private void* handle;
private void* surface;
private void* client;
}
internal struct osmesa
{
//OSMesaContext handle;
void* handle;
int width;
int height;
void* buffer;
private void* handle;
private int width;
private int height;
private void* buffer;
}
// This is defined in platform.h
//GLFW_PLATFORM_CONTEXT_STATE
}
[StructLayout(LayoutKind.Sequential)]
@@ -95,46 +96,43 @@ namespace StgSharp.Graphics
public int blueBits;
/*! The refresh rate, in Hz, of the video mode.
*/
int refreshRate;
private int refreshRate;
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct GLFWgammaramp
{
/*! An array of value describing the response of the red channel.
*/
ushort* red;
private ushort* red;
/*! An array of value describing the response of the green channel.
*/
ushort* green;
private ushort* green;
/*! An array of value describing the response of the blue channel.
*/
ushort* blue;
private ushort* blue;
/*! The number of elements in each array.
*/
uint size;
private uint size;
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct GLFWimage
{
/*! The width, in pixels, of this image.
*/
int width;
private int width;
/*! The height, in pixels, of this image.
*/
int height;
private int height;
/*! The pixel data of this image, arranged left-to-right, top-to-bottom.
*/
byte* pixels;
private byte* pixels;
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct GLFWgamepadstate
{
/*! The states of each [gamepad button](@ref gamepad_buttons), `GLFW_PRESS`
* or `GLFW_RELEASE`.
*/
@@ -149,40 +147,44 @@ namespace StgSharp.Graphics
public unsafe struct GLFWallocator
{
//GLFWallocatefun allocate;
IntPtr allocate;
private IntPtr allocate;
//GLFWreallocatefun reallocate;
IntPtr reallocate;
private IntPtr reallocate;
//GLFWdeallocatefun deallocate;
IntPtr deallocate;
void* user;
private IntPtr deallocate;
private void* user;
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct GLFWmonitor
{
byte[] name = new byte[128];
void* userPointer;
private byte[] name = new byte[128];
private void* userPointer;
// Physical dimensions in millimeters.
int widthMM, heightMM;
private int widthMM, heightMM;
// The window whose video mode is current on this monitor
//GLFWwindow* window;
IntPtr window;
private IntPtr window;
//GLFWvidmode* modes;
IntPtr modes;
int modeCount;
GLFWvidmode currentMode;
private IntPtr modes;
GLFWgammaramp originalRamp;
GLFWgammaramp currentRamp;
private int modeCount;
private GLFWvidmode currentMode;
private GLFWgammaramp originalRamp;
private GLFWgammaramp currentRamp;
// not transformed
//GLFW_PLATFORM_MONITOR_STATE;
public GLFWmonitor() { }
public GLFWmonitor()
{ }
}
[StructLayout(LayoutKind.Sequential)]
@@ -214,15 +216,16 @@ namespace StgSharp.Graphics
public int cursorMode = 0;
public fixed byte mouseButtons[8];
public fixed byte keys[349];
// Virtual cursor position when cursor is disabled
public double virtualCursorPosX = 0, virtualCursorPosY = 0;
public bool rawMouseMotion = false;
public GLFWcontext context = default;
public GLFWwindow()
{
}
}
@@ -230,7 +233,7 @@ namespace StgSharp.Graphics
public struct GLFWcursor
{
//GLFWcursor* next;
IntPtr next;
private IntPtr next;
// This is defined in platform.h
// not completed in tranformation
@@ -241,7 +244,6 @@ namespace StgSharp.Graphics
[StructLayout(LayoutKind.Sequential)]
public unsafe struct GLFWerror
{
//GLFWerror* next;
IntPtr next;
int code;
@@ -257,61 +259,64 @@ namespace StgSharp.Graphics
[StructLayout(LayoutKind.Sequential)]
public unsafe struct GLFWinitconfig
{
private GLFWbool hatButtons;
private int angleType;
private int platformID;
GLFWbool hatButtons;
int angleType;
int platformID;
//PFN_vkGetInstanceProcAddr vulkanLoader;
IntPtr vulkanLoader;
struct ns
private IntPtr vulkanLoader;
private struct ns
{
GLFWbool menubar;
GLFWbool chdir;
private GLFWbool menubar;
private GLFWbool chdir;
}
struct x11
private struct x11
{
GLFWbool xcbVulkanSurface;
private GLFWbool xcbVulkanSurface;
}
struct wl
private struct wl
{
int libdecorMode;
private int libdecorMode;
}
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct GLFWwndconfig
{
private int xpos;
private int ypos;
private int width;
private int height;
private byte* title;
private GLFWbool resizable;
private GLFWbool visible;
private GLFWbool decorated;
private GLFWbool focused;
private GLFWbool autoIconify;
private GLFWbool floating;
private GLFWbool maximized;
private GLFWbool centerCursor;
private GLFWbool focusOnShow;
private GLFWbool mousePassthrough;
private GLFWbool scaleToMonitor;
int xpos;
int ypos;
int width;
int height;
byte* title;
GLFWbool resizable;
GLFWbool visible;
GLFWbool decorated;
GLFWbool focused;
GLFWbool autoIconify;
GLFWbool floating;
GLFWbool maximized;
GLFWbool centerCursor;
GLFWbool focusOnShow;
GLFWbool mousePassthrough;
GLFWbool scaleToMonitor;
internal struct Ns
{
GLFWbool retina = false;
private GLFWbool retina = false;
public fixed byte frameName[256];
public Ns()
{
}
}
internal Ns ns = new Ns();
internal struct X11
{
public fixed byte className[256];
public fixed byte instanceName[256];
@@ -319,12 +324,14 @@ namespace StgSharp.Graphics
{
}
}
internal X11 x11 = new X11();
internal struct Win32
{
GLFWbool keymenu;
private GLFWbool keymenu;
}
internal Win32 win32 = new Win32();
internal struct Wl
@@ -335,6 +342,7 @@ namespace StgSharp.Graphics
{
}
}
internal Wl wl = new Wl();
public GLFWwndconfig()
@@ -345,22 +353,25 @@ namespace StgSharp.Graphics
[StructLayout(LayoutKind.Sequential)]
public unsafe struct GLFWctxconfig
{
int client;
int source;
int major;
int minor;
GLFWbool forward;
GLFWbool debug;
GLFWbool noerror;
int profile;
int robustness;
int release;
private int client;
private int source;
private int major;
private int minor;
private GLFWbool forward;
private GLFWbool debug;
private GLFWbool noerror;
private int profile;
private int robustness;
private int release;
//GLFWwindow* share;
IntPtr share;
private IntPtr share;
internal struct Nsgl
{
GLFWbool offline;
private GLFWbool offline;
}
internal Nsgl nsgl = new Nsgl();
public GLFWctxconfig()
@@ -371,78 +382,81 @@ namespace StgSharp.Graphics
[StructLayout(LayoutKind.Sequential)]
public unsafe struct GLFWfbconfig
{
int redBits;
int greenBits;
int blueBits;
int alphaBits;
int depthBits;
int stencilBits;
int accumRedBits;
int accumGreenBits;
int accumBlueBits;
int accumAlphaBits;
int auxBuffers;
GLFWbool stereo;
int samples;
GLFWbool sRGB;
GLFWbool doublebuffer;
GLFWbool transparent;
uintptr_t handle;
private int redBits;
private int greenBits;
private int blueBits;
private int alphaBits;
private int depthBits;
private int stencilBits;
private int accumRedBits;
private int accumGreenBits;
private int accumBlueBits;
private int accumAlphaBits;
private int auxBuffers;
private GLFWbool stereo;
private int samples;
private GLFWbool sRGB;
private GLFWbool doublebuffer;
private GLFWbool transparent;
private uintptr_t handle;
}
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct GLFWlibrary
{
GLFWbool initialized;
GLFWallocator allocator;
private GLFWbool initialized;
private GLFWallocator allocator;
GLFWplatform platform;
private GLFWplatform platform;
internal unsafe struct Hints
{
GLFWinitconfig init;
GLFWfbconfig framebuffer;
GLFWwndconfig window;
GLFWctxconfig context;
int refreshRate;
private GLFWinitconfig init;
private GLFWfbconfig framebuffer;
private GLFWwndconfig window;
private GLFWctxconfig context;
private int refreshRate;
}
internal Hints hints = new Hints();
//GLFWerror* errorListHead;
IntPtr errorListHead;
private IntPtr errorListHead;
//GLFWcursor* cursorListHead;
IntPtr cursorListHead;
private IntPtr cursorListHead;
//GLFWwindow* windowListHead;
IntPtr windowListHead;
private IntPtr windowListHead;
//GLFWmonitor** monitors;
IntPtr monitors;
int monitorCount;
private IntPtr monitors;
private int monitorCount;
private GLFWbool joysticksInitialized;
GLFWbool joysticksInitialized;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = GLconst.GLFW_JOYSTICK_LAST + 1)]
GLFWjoystick[] joysticks = new GLFWjoystick[GLconst.GLFW_JOYSTICK_LAST + 1];
//GLFWmapping* mappings;
IntPtr mappings;
int mappingCount;
private GLFWjoystick[] joysticks = new GLFWjoystick[GLconst.GLFW_JOYSTICK_LAST + 1];
GLFWls errorSlot;
GLFWls contextSlot;
GLFWmutex errorLock;
//GLFWmapping* mappings;
private IntPtr mappings;
private int mappingCount;
private GLFWls errorSlot;
private GLFWls contextSlot;
private GLFWmutex errorLock;
internal unsafe struct timer
{
ulong offset;
private ulong offset;
// This is defined in platform.h
//not transformed function defination
//GLFW_PLATFORM_LIBRARY_TIMER_STATE
}
internal unsafe struct Egl
{
internal int platform;
@@ -488,101 +502,105 @@ namespace StgSharp.Graphics
internal IntPtr GetPlatformDisplayEXT;
internal IntPtr CreatePlatformWindowSurfaceEXT;
}
internal Egl egl = new Egl();
internal unsafe struct Osmesa
{
void* handle;
private void* handle;
IntPtr CreateContextExt;
IntPtr CreateContextAttribs;
IntPtr DestroyContext;
IntPtr MakeCurrent;
IntPtr GetColorBuffer;
IntPtr GetDepthBuffer;
IntPtr GetProcAddress;
private IntPtr CreateContextExt;
private IntPtr CreateContextAttribs;
private IntPtr DestroyContext;
private IntPtr MakeCurrent;
private IntPtr GetColorBuffer;
private IntPtr GetDepthBuffer;
private IntPtr GetProcAddress;
}
internal Osmesa osmesa = new Osmesa();
internal unsafe struct vk
{
GLFWbool available;
void* handle;
fixed ulong extensions[2];
IntPtr GetInstanceProcAddr;
GLFWbool KHR_surface;
GLFWbool KHR_win32_surface;
GLFWbool MVK_macos_surface;
GLFWbool EXT_metal_surface;
GLFWbool KHR_xlib_surface;
GLFWbool KHR_xcb_surface;
GLFWbool KHR_wayland_surface;
private GLFWbool available;
private void* handle;
private fixed ulong extensions[2];
private IntPtr GetInstanceProcAddr;
private GLFWbool KHR_surface;
private GLFWbool KHR_win32_surface;
private GLFWbool MVK_macos_surface;
private GLFWbool EXT_metal_surface;
private GLFWbool KHR_xlib_surface;
private GLFWbool KHR_xcb_surface;
private GLFWbool KHR_wayland_surface;
public vk()
{
}
}
internal unsafe struct Callbacks
{
IntPtr monitor;
IntPtr joystick;
private IntPtr monitor;
private IntPtr joystick;
}
internal Callbacks callbacks = new Callbacks();
public GLFWlibrary()
{
}
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct GLFWmapelement
{
byte type;
byte index;
sbyte axisScale;
sbyte axisOffset;
private byte type;
private byte index;
private sbyte axisScale;
private sbyte axisOffset;
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct GLFWmapping
{
private fixed byte name[128];
private fixed byte guid[33];
fixed byte name[128];
fixed byte guid[33];
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 15)]
GLFWmapelement[] buttons = new GLFWmapelement[15];
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
GLFWmapelement[] axes = new GLFWmapelement[6];
private GLFWmapelement[] buttons = new GLFWmapelement[15];
public GLFWmapping() { }
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
private GLFWmapelement[] axes = new GLFWmapelement[6];
public GLFWmapping()
{ }
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct GLFWjoystick
{
private GLFWbool allocated;
private GLFWbool connected;
private float* axes;
private int axisCount;
private byte* buttons;
private int buttonCount;
private byte* hats;
private int hatCount;
private fixed byte name[128];
private void* userPointer;
private fixed byte guid[33];
GLFWbool allocated;
GLFWbool connected;
float* axes;
int axisCount;
byte* buttons;
int buttonCount;
byte* hats;
int hatCount;
fixed byte name[128];
void* userPointer;
fixed byte guid[33];
//GLFWmapping* mapping;
IntPtr mapping;
private IntPtr mapping;
// This is defined in platform.h
// not completed in transforming
// GLFW_PLATFORM_JOYSTICK_STATE;
public GLFWjoystick() { }
public GLFWjoystick()
{ }
}
[StructLayout(LayoutKind.Sequential)]
@@ -596,13 +614,14 @@ namespace StgSharp.Graphics
[StructLayout(LayoutKind.Sequential)]
public unsafe struct GLFWplatform
{
private int platformID;
int platformID;
// init
//GLFWbool(*init)(void);
IntPtr init;
private IntPtr init;
//void (* terminate) (void);
IntPtr terminate;
private IntPtr terminate;
// input
/* void (* getCursorPos) (GLFWwindow*, double*, double*);
@@ -688,8 +707,10 @@ namespace StgSharp.Graphics
internal IntPtr pollJoystick;
internal IntPtr getMappingName;
internal IntPtr updateGamepadGUID;
// monitor
internal IntPtr freeMonitor;
internal IntPtr getMonitorPos;
internal IntPtr getMonitorContentScale;
internal IntPtr getMonitorWorkarea;
@@ -697,8 +718,10 @@ namespace StgSharp.Graphics
internal IntPtr getVideoMode;
internal IntPtr getGammaRamp;
internal IntPtr setGammaRamp;
// window
internal IntPtr createWindow;
internal IntPtr destroyWindow;
internal IntPtr setWindowTitle;
internal IntPtr setWindowIcon;
@@ -736,25 +759,23 @@ namespace StgSharp.Graphics
internal IntPtr waitEventsTimeout;
internal IntPtr postEmptyEvent;
// EGL
//EGLenum(*getEGLPlatform)(EGLint**);
//EGLNativeDisplayType(*getEGLNativeDisplay)(void);
//EGLNativeWindowType(*getEGLNativeWindow)(GLFWwindow*);
internal IntPtr getEGLPlatform;
internal IntPtr getEGLNativeDisplay;
internal IntPtr getEGLNativeWindow;
// vulkan
//void (* getRequiredInstanceExtensions) (byte**);
//GLFWbool(*getPhysicalDevicePresentationSupport)(VkInstance, VkPhysicalDevice, uint32_t);
//VkResult(*createWindowSurface)(VkInstance, GLFWwindow*,const VkAllocationCallbacks*, VkSurfaceKHR*);
internal IntPtr getRequiredInstanceExtensions;
internal IntPtr VkPhysicalDevice;
internal IntPtr GLFWwindow;
}
[StructLayout(LayoutKind.Sequential)]
@@ -769,8 +790,8 @@ namespace StgSharp.Graphics
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct VkExtensionProperties
{
fixed byte extensionName[256];
uint specVersion;
private fixed byte extensionName[256];
private uint specVersion;
public VkExtensionProperties()
{
@@ -792,27 +813,26 @@ namespace StgSharp.Graphics
}
*/
[StructLayout(LayoutKind.Explicit, Size = 16)]
public struct uintptr_t
{
[FieldOffset(0)]
long part1;
private long part1;
[FieldOffset(8)]
long part2;
private long part2;
}
#endregion
#endregion GLFW3 struct
#region glad struct
[StructLayout(LayoutKind.Sequential)]
internal struct GLsync
{
}
#endregion
#endregion glad struct
}
/**/
+19 -4
View File
@@ -2,6 +2,7 @@
using StgSharp.Controlling;
using StgSharp.Logic;
using StgSharp.Math;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
@@ -272,7 +273,7 @@ namespace StgSharp.Graphics
else
{
M128 m = default;
m.UInt64[0] = (ulong)windowID;
m.Write<ulong>(0, (ulong)windowID);
GLonUpdate glu = gl as GLonUpdate;
glu.frameworkHandleEnqueue(
(IntPtr)(delegate*<void>)&InternalSwapBuffer, m);
@@ -405,13 +406,27 @@ namespace StgSharp.Graphics
ConcurrentQueue<IntPtr> glActions = (gl as GLonUpdate).GLactionQueue;
#pragma warning restore CS8602
IntPtr currentOperation;
while (gl.GetWindowShouldClose(this))
TimeSpan span = new TimeSpan(100L);
try
{
if (glActions.TryDequeue(out currentOperation))
while (gl.GetWindowShouldClose(this))
{
((delegate*<void>)currentOperation)();
if (glActions.TryDequeue(out currentOperation))
{
((delegate*<void>)currentOperation)();
}
else
{
Thread.Sleep(span);
}
}
}
catch (Exception e)
{
this.ShouldClose = true;
(gl as GLonUpdate).ShowDequeueStream();
throw;
}
}
#endregion form main
+2 -1
View File
@@ -1,5 +1,6 @@
using StgSharp;
using StgSharp.Logic;
using System;
using System.Runtime.CompilerServices;
using System.Threading;
@@ -9,7 +10,6 @@ namespace StgSharp.Graphics
{
public partial class Form
{
private bool isFullyRendered = false;
internal class FrameUpdateTimer
@@ -29,6 +29,7 @@ namespace StgSharp.Graphics
{
get { return tickCounter; }
}
internal void Refresh()
{
refreshed = false;
+7 -7
View File
@@ -1,4 +1,5 @@
using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
@@ -7,11 +8,9 @@ using System.Text;
namespace StgSharp.Graphics
{
/// <summary>
/// A collecion of handle to Element Buffer Object in OpenGL.
/// Each <see langword="int"/> value indexed from instance of this class this
/// 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
@@ -22,8 +21,7 @@ namespace StgSharp.Graphics
_bufferHandle = GL.GenBuffers(n);
}
public override void Bind(int index)
public override sealed void Bind(int index)
{
GL.BindBuffer(BufferType.ELEMENT_ARRAY_BUFFER, _bufferHandle[index]);
}
@@ -31,16 +29,18 @@ namespace StgSharp.Graphics
public void SetValue<T>(int index, T[] bufferArray, BufferUsage usage)
where T : struct, IConvertible
{
Console.WriteLine("Setting ebo data.");
GL.BindBuffer(BufferType.ELEMENT_ARRAY_BUFFER, _bufferHandle[index]);
GL.SetBufferData(BufferType.ELEMENT_ARRAY_BUFFER, bufferArray, usage);
}
protected sealed override void Dispose(bool disposing)
protected override sealed void Dispose(bool disposing)
{
if (disposing)
{
GL.DeleteBuffers(_bufferHandle);
_bufferHandle.Release();
}
}
}
}
}
+5 -5
View File
@@ -5,7 +5,6 @@ using System.Text;
namespace StgSharp.Graphics
{
public unsafe class VertexArray : BufferObjectBase
{
internal VertexArray(int n, IglFunc gl)
@@ -15,7 +14,7 @@ namespace StgSharp.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public sealed override void Bind(int index)
public override sealed void Bind(int index)
{
GL.BindVertexArray(_bufferHandle[index]);
}
@@ -23,7 +22,7 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void BindNull()
{
GL.BindVertexArray(glHandle.Alloc(0));
GL.BindVertexArray(glHandle.Zero);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -35,12 +34,13 @@ namespace StgSharp.Graphics
stride, pointer);
}
protected sealed override void Dispose(bool disposing)
protected override sealed void Dispose(bool disposing)
{
if (disposing)
{
GL.DeleteVertexArrays(_bufferHandle);
_bufferHandle.Release();
}
}
}
}
}
+7 -7
View File
@@ -1,4 +1,5 @@
using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.Net.Security;
@@ -9,7 +10,6 @@ using System.Text;
namespace StgSharp.Graphics
{
public unsafe class VertexBuffer : BufferObjectBase
{
internal VertexBuffer(int n, IglFunc glFunc)
@@ -18,7 +18,7 @@ namespace StgSharp.Graphics
_bufferHandle = GL.GenBuffers(n);
}
public sealed override void Bind(int index)
public override sealed void Bind(int index)
{
GL.BindBuffer(BufferType.ARRAY_BUFFER, _bufferHandle[index]);
}
@@ -32,9 +32,9 @@ namespace StgSharp.Graphics
/// <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 : IMat
{
Console.WriteLine("Setting vbo data.");
GL.BindBuffer(BufferType.ARRAY_BUFFER, _bufferHandle[index]);
GL.SetBufferData(BufferType.ARRAY_BUFFER, bufferData, usage);
}
/// <summary>
@@ -51,7 +51,7 @@ namespace StgSharp.Graphics
}
/// <summary>
///
///
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Unbind()
@@ -65,13 +65,13 @@ namespace StgSharp.Graphics
GL = gl;
}
protected sealed override void Dispose(bool disposing)
protected override sealed void Dispose(bool disposing)
{
if (disposing)
{
GL.DeleteBuffers(_bufferHandle);
_bufferHandle.Release();
}
}
}
}
}
+28 -1
View File
@@ -9,8 +9,35 @@ using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
namespace System
namespace StgSharp.Graphics
{
//TODO 发现一个大问题,paramQueue大多数时候取出的是数组,不是数组句柄,也就是说其内存将被长期固定且不能回收,这是内存泄漏
public unsafe class glArrayHandle
{
internal readonly Array copy;
internal GCHandle handle;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IntPtr AddressOfArray()
{
return handle.AddrOfPinnedObject();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static glArrayHandle Alloc<T>(T[] array) where T : struct
{
Array copy = new T[array.Length];
array.CopyTo(copy, 0);
return new glArrayHandle(copy);
}
internal glArrayHandle(Array array)
{
copy = array;
handle = GCHandle.Alloc(array, GCHandleType.Pinned);
}
}
/// <summary>
/// A handle to an OpenGL object,
/// must be indexed from a <see cref="glHandleSet"/>.
+1 -3
View File
@@ -9,7 +9,5 @@ namespace StgSharp.Graphics
{
public unsafe partial class Form
{
}
}
}
+30 -48
View File
@@ -54,13 +54,13 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.AttachShader(glHandle program, glHandle shader)
{
api->glAttachShader(*program.valuePtr, *shader.valuePtr);
api->glAttachShader(*program.valuePtr, shader.DirectValue);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.BindBuffer(BufferType bufferType, glHandle handle)
{
api->glBindBuffer((uint)bufferType, *handle.valuePtr);
api->glBindBuffer((uint)bufferType, handle.DirectValue);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -93,57 +93,39 @@ namespace StgSharp.Graphics
while (api->glGetError() != 0) { }
}
glHandleSet IglFunc.CompileShader(ShaderType shaderType, params string[] shaderCodeArray)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.LoadShaderSource(glHandle shaderHandle, string codeStream)
{
int l = shaderCodeArray.Length;
if (l == 0)
byte[] stream = Encoding.UTF8.GetBytes(codeStream);
fixed (byte* streamPtr = stream)
{
throw new ArgumentException("At least one pice of code is required.");
api->glShaderSource(shaderHandle.DirectValue, 1, &streamPtr, (void*)0);
}
glHandleSet handleSet = new glHandleSet(l);
for (int i = 0; i < l; i++)
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
glHandleSet IglFunc.CreateShaderSet(int count, ShaderType type)
{
glHandleSet ret = new glHandleSet(count);
uint t = (uint)type;
for (int i = 0; i < count; i++)
{
uint shaderID;
byte[] data = Encoding.UTF8.GetBytes(shaderCodeArray[i] + '\0');
fixed (byte* codePtr = data)
uint id = api->glCreateShader(t);
if (id == 0)
{
shaderID = InternalIO.InternalLoadShaderCode(api, codePtr, (int)shaderType);
uint error = api->glGetError();
InternalIO.InternalWriteLog($"Failed in creating shader: {error}", LogType.Error);
throw new Exception();
}
if (shaderID == 0) //compile error handler
{
IntPtr logPtr = InternalIO.InternalReadSSCLog();
string log;
try
{
byte[] logByte = new byte[512];
Marshal.Copy(logPtr, logByte, 0, 512);
log = Encoding.UTF8.GetString(logByte);
log = log.Replace("\0", "");
bool isUseful = false;
foreach (char item in log)
{
if (!char.IsControl(item))
{
isUseful = true; break;
}
}
if (!isUseful)
{
throw new Exception();
}
}
catch (Exception)
{
log = "Failed to compile shader.\n" +
"Failed to get GLSL compiler log";
}
InternalIO.InternalWriteLog(log, LogType.Error);
}
handleSet.SetValue(i, shaderID);
ret.SetValue(i, id);
}
return handleSet;
return ret;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.CompileShader(glHandle shaderhandle)
{
api->glCompileShader(shaderhandle.DirectValue);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -301,7 +283,7 @@ namespace StgSharp.Graphics
api->glBufferData(
(uint)bufferType,
(IntPtr)size,
&bufferData,
(IntPtr)(&bufferData),
(uint)usage);
}
@@ -317,7 +299,7 @@ namespace StgSharp.Graphics
api->glBufferData(
(uint)bufferType,
(IntPtr)size,
bufferPtr,
(IntPtr)bufferPtr,
(uint)usage);
}
+18 -8
View File
@@ -18,11 +18,11 @@ namespace StgSharp.Graphics
private FrameTimeoutException refreshSIgnal = new FrameTimeoutException();
private FrameTimeoutException signal = new FrameTimeoutException("This frame is timeout");
unsafe GLcontext* IglFunc.Context => throw signal;
private GLonTimeout()
{ }
unsafe GLcontext* IglFunc.Context => throw signal;
void IglFunc.ActiveTexture(uint type) => throw signal;
void IglFunc.AfterAlign(Form form, Action action) => throw signal;
@@ -43,12 +43,17 @@ namespace StgSharp.Graphics
void IglFunc.ClearGraphicError() => throw signal;
glHandleSet IglFunc.CompileShader(ShaderType shaderType, params string[] shaderCode) => throw signal;
public void CompileShader(glHandle shaderhandle)
{
throw signal;
}
glHandle IglFunc.CreateProgram() => throw signal;
glHandleSet IglFunc.CreateProgram(int count) => throw signal;
glHandleSet IglFunc.CreateShaderSet(int count, StgSharp.Graphics.ShaderType type) => throw signal;
IntPtr IglFunc.CreateWindowHandle(int width, int height, string tittle, IntPtr monitor, Form share) => throw signal;
glHandle IglFunc.CreatGraphicProgram() => throw signal;
@@ -71,6 +76,11 @@ namespace StgSharp.Graphics
bool IglFunc.GetWindowShouldClose(Form form) => throw signal;
void IglFunc.LoadShaderSource(glHandle shaderHandle, string codeStream)
{
throw signal;
}
void IglFunc.MakeContexCurrent(Form form) => throw signal;
void IglFunc.SetBufferData<T>(BufferType bufferType, T bufferData, BufferUsage usage) => throw signal;
@@ -93,6 +103,11 @@ namespace StgSharp.Graphics
void IglFunc.SetVertexAttribute(uint index, int size, TypeCode t, bool normalized, uint stride, int pointer) => throw signal;
void IglFunc.SwitchContext()
{
throw signal;
}
unsafe void IglFunc.TextureImage2d<T>(int target, int level, uint internalformat, uint width, uint height, uint format, uint type, glHandle pixels) => throw signal;
void IglFunc.TextureParameter(int target, uint pname, int param) => throw signal;
@@ -102,10 +117,5 @@ namespace StgSharp.Graphics
void IglFunc.Viewport(int x, int y, uint width, uint height) => throw signal;
internal static GLonTimeout Set() => singleInstance;
void IglFunc.SwitchContext()
{
throw signal;
}
}
}
+54 -42
View File
@@ -17,15 +17,13 @@ namespace StgSharp.Graphics
{
private static ConcurrentQueue<IntPtr> glActionQueue;
private static GLparamQueue paramQueue;
private FrameTimeoutException refreshSignal = new();
private GLcontext* api;
private FrameTimeoutException refreshSignal = new();
private IntPtr SwapBufferPtr = (IntPtr)(delegate*<IntPtr, void>)&InternalIO.glfwSwapBuffers;
GLcontext* IglFunc.Context
internal void ShowDequeueStream()
{
get => api;
paramQueue.ShowDequeueLog();
}
internal GLonUpdate(GLcontext* context)
@@ -38,6 +36,11 @@ namespace StgSharp.Graphics
api = context;
}
GLcontext* IglFunc.Context
{
get => api;
}
internal static GLparamQueue ParamQueue
{
get { return paramQueue; }
@@ -84,8 +87,8 @@ namespace StgSharp.Graphics
void IglFunc.BindBuffer(BufferType bufferType, glHandle handle)
{
M128 m = default;
m.UInt32[0] = (uint)bufferType;
m.UInt64[1] = (ulong)handle.Handle;
m.Write<uint>(0, (uint)bufferType);
m.Write<ulong>(1, (ulong)handle.valuePtr);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(BindBufferPtr);
}
@@ -94,8 +97,8 @@ namespace StgSharp.Graphics
void IglFunc.BindTexture(uint type, glHandle texture)
{
M128 m = default;
m.UInt64[1] = (ulong)texture.valuePtr;
m.UInt32[0] = (uint)type;
m.Write<uint>(0, (uint)type);
m.Write<ulong>(1, (ulong)texture.valuePtr);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(BindTexturePtr);
}
@@ -110,6 +113,7 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.Clear(MaskBufferBit mask)
{
paramQueue.Enqueue((int)mask);
glActionQueue.Enqueue(ClearPtr);
}
@@ -126,22 +130,10 @@ namespace StgSharp.Graphics
glActionQueue.Enqueue(ClearGraphicErrorPtr);
}
glHandleSet IglFunc.CompileShader(ShaderType shaderType, params string[] shaderCode)
unsafe void IglFunc.CompileShader(glHandle shaderhandle)
{
int l = shaderCode.Length;
if (l == 0)
{
throw new ArgumentException("Should input at least one piece of shader code");
}
glHandleSet shaderHandle = new glHandleSet(shaderCode.Length);
paramQueue.Enqueue((int)shaderType, l);
for (int i = 0; i < l; i++)
{
GCHandle codePtr = GCHandle.Alloc(Encoding.UTF8.GetBytes(shaderCode[i] + '\0'), GCHandleType.Pinned);
paramQueue.Enqueue((IntPtr)(shaderHandle.handle + i), codePtr.AddrOfPinnedObject());
}
paramQueue.Enqueue((IntPtr)shaderhandle.valuePtr);
glActionQueue.Enqueue(CompileShaderPtr);
return shaderHandle;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -153,18 +145,31 @@ namespace StgSharp.Graphics
return h;
}
//TODO
[MethodImpl(MethodImplOptions.AggressiveInlining)]
glHandleSet IglFunc.CreateProgram(int count)
{
M128 m = default;
glHandleSet h = new glHandleSet(count);
m.UInt64[0] = (ulong)h.handle;
m.Int32[2] = count;
m.Write<ulong>(0, (ulong)h.handle);
m.Write<int>(2, count);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(CreateProgramSetPtr);
return h;
}
glHandleSet IglFunc.CreateShaderSet(int count, ShaderType type)
{
glHandleSet ret = new glHandleSet(count);
M128 m = new M128();
m.Write<int>(0, count);
m.Write<uint>(1, (uint)type);
m.Write<ulong>(1, (ulong)ret.handle);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(CreateShaderPtr);
return ret;
}
IntPtr IglFunc.CreateWindowHandle(int width, int height, string tittle, IntPtr monitor, Form share)
{
byte[] tittleBytes = Encoding.UTF8.GetBytes(tittle);
@@ -203,8 +208,8 @@ namespace StgSharp.Graphics
unsafe void IglFunc.DeleteBuffer(glHandle handle)
{
M128 m = default;
m.UInt64[0] = (ulong)handle.Handle;
m.UInt32[2] = 1;
m.Write<ulong>(0, (ulong)handle.valuePtr);
m.Write(2, 1);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(DeleBufferPtr);
}
@@ -212,16 +217,16 @@ namespace StgSharp.Graphics
unsafe void IglFunc.DeleteBuffers(glHandleSet handle)
{
M128 m = default;
m.UInt32[2] = (uint)handle.length;
m.UInt64[0] = (ulong)handle.handle;
m.Write<uint>(2, (uint)handle.length);
m.Write<ulong>(0, (ulong)handle.handle);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(DeleBufferPtr);
}
unsafe void IglFunc.DeleteVertexArrays(glHandleSet arrays)
{
M128 m = default; m.UInt64[1] = (ulong)arrays.handle;
m.Int32[0] = arrays.length;
M128 m = default; m.Write<ulong>(1, (ulong)arrays.handle);
m.Write<int>(0, arrays.length);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(DeleteVertexArrayPtr);
}
@@ -258,7 +263,7 @@ namespace StgSharp.Graphics
unsafe glHandleSet IglFunc.GenBuffers(int count)
{
glHandleSet h = new glHandleSet(count);
M128 m = default; m.UInt32[2] = (uint)count; m.UInt64[0] = (ulong)h.handle;
M128 m = default; m.Write<uint>(2, (uint)count); m.Write<ulong>(0, (ulong)h.handle);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(GenBufferPtr);
return h;
@@ -267,7 +272,7 @@ namespace StgSharp.Graphics
unsafe glHandleSet IglFunc.GenTextures(int count)
{
glHandleSet h = new glHandleSet(count);
M128 m = default; m.UInt32[2] = (uint)count; m.UInt64[0] = (ulong)h.handle;
M128 m = default; m.Write<uint>(2, (uint)count); m.Write<ulong>(0, (ulong)h.handle);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(GenTexturesPtr);
return h;
@@ -276,7 +281,7 @@ namespace StgSharp.Graphics
unsafe glHandleSet IglFunc.GenVertexArrays(int count)
{
glHandleSet h = new glHandleSet(count);
M128 m = default; m.UInt32[2] = (uint)count; m.UInt64[0] = (ulong)h.handle;
M128 m = default; m.Write<uint>(2, (uint)count); m.Write<ulong>(0, (ulong)h.handle);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(GenTexturesPtr);
return h;
@@ -286,9 +291,9 @@ namespace StgSharp.Graphics
unsafe glSHandle IglFunc.GetUniformLocation(glHandle program, string ptr)
{
glSHandle h = glSHandle.Alloc(0);
M128 m;
m.UInt64[0] = (ulong)program.Handle;
m.UInt64[1] = (ulong)GCHandle.Alloc(Encoding.UTF8.GetBytes(ptr)).AddrOfPinnedObject();
M128 m = default;
m.Write<ulong>(0, (ulong)program.Handle);
m.Write<ulong>(1, (ulong)GCHandle.Alloc(Encoding.UTF8.GetBytes(ptr)).AddrOfPinnedObject());
paramQueue.Enqueue(m);
paramQueue.Enqueue((IntPtr)h.valuePtr);
glActionQueue.Enqueue(GetUniformLocationPtr);
@@ -310,6 +315,12 @@ namespace StgSharp.Graphics
InternalIO.glfwSwapBuffers(form.windowID);
}
void IglFunc.LoadShaderSource(glHandle shaderHandle, string codeStream)
{
paramQueue.Enqueue(shaderHandle);
paramQueue.Enqueue(codeStream);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.MakeContexCurrent(Form form)
{
@@ -330,7 +341,8 @@ namespace StgSharp.Graphics
void IglFunc.SetBufferData<T>(BufferType bufferType, T[] bufferArray, BufferUsage usage)
{
paramQueue.Enqueue((uint)bufferType, (uint)usage);
paramQueue.Enqueue((IntPtr)(Marshal.SizeOf<T>() * bufferArray.Length), GCHandle.Alloc(bufferArray).AddrOfPinnedObject());
paramQueue.Enqueue((IntPtr)(Marshal.SizeOf<T>() * bufferArray.Length));
paramQueue.Enqueue<T>(bufferArray);
glActionQueue.Enqueue(BufferDataPtr);
}
@@ -359,9 +371,9 @@ namespace StgSharp.Graphics
void IglFunc.TextureParameter(int target, uint pname, int param)
{
M128 m = default;
m.Int32[0] = target;
m.UInt32[2] = pname;
m.Int32[3] = param;
m.Write<int>(0, target);
m.Write<uint>(2, pname);
m.Write<int>(3, param);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(TextureParameterPtr);
}
@@ -382,11 +394,11 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void frameworkHandleEnqueue(IntPtr methodHandle, params M128[] param)
{
glActionQueue.Enqueue(methodHandle);
foreach (M128 item in param)
{
paramQueue.Enqueue(item);
}
glActionQueue.Enqueue(methodHandle);
}
}
}
@@ -18,16 +18,17 @@ namespace StgSharp.Graphics
private IntPtr ActiveTexturePtr = (IntPtr)(delegate*<void>)&InternalActiveTexture;
private IntPtr AttachShaderPtr = (IntPtr)(delegate*<void>)&InternalAttachShader;
private IntPtr BindBufferPtr = (IntPtr)(delegate*<void>)&InternalBindBuffer;
private IntPtr ClearColorPtr = (IntPtr)(delegate*<void>)&InternalClearColor;
private IntPtr ClearGraphicErrorPtr = (IntPtr)(delegate*<void>)&InternalClearGraphicError;
private IntPtr CompileShaderPtr = (IntPtr)(delegate*<void>)&InternalCompileShader;
private IntPtr DeleBufferPtr = (IntPtr)(delegate*<void>)&InternalDeleteBuffer;
private IntPtr BindTexturePtr = (IntPtr)(delegate*<void>)&InternalBindTexture;
private IntPtr BindVertexArrayPtr = (IntPtr)(delegate*<void>)&InternalBindVertexArray;
private IntPtr BufferDataPtr = (IntPtr)(delegate*<void>)&InternalSetBufferData;
private IntPtr ClearColorPtr = (IntPtr)(delegate*<void>)&InternalClearColor;
private IntPtr ClearGraphicErrorPtr = (IntPtr)(delegate*<void>)&InternalClearGraphicError;
private IntPtr ClearPtr = (IntPtr)(delegate*<void>)&InternalClear;
private IntPtr CompileShaderPtr = (IntPtr)(delegate*<void>)&InternalCompileShader;
private IntPtr CreateProgramPtr = (IntPtr)(delegate*<void>)&InternalCreateProgram;
private IntPtr CreateProgramSetPtr = (IntPtr)(delegate*<void>)&InternalCreateProgramSet;
private IntPtr CreateShaderPtr = (IntPtr)(delegate*<void>)&InternalCreateShader;
private IntPtr DeleBufferPtr = (IntPtr)(delegate*<void>)&InternalDeleteBuffer;
private IntPtr DeleteVertexArrayPtr = (IntPtr)(delegate*<void>)&InternalDeleteVertexArrays;
private IntPtr DrawElementsPtr = (IntPtr)(delegate*<void>)&InternalDrawElements;
private IntPtr GenBufferPtr = (IntPtr)(delegate*<void>)&InternalGenBuffers;
@@ -35,6 +36,7 @@ namespace StgSharp.Graphics
private IntPtr GenVertexArraysPtr = (IntPtr)(delegate*<void>)&InternalGenVertexArrays;
private IntPtr GetUniformLocationPtr = (IntPtr)(delegate*<void>)&InternalGetUniformLocation;
private IntPtr SetVertexArrayPtr = (IntPtr)(delegate*<void>)&InternalSetVertexAttribute;
private IntPtr ShaderSourcePtr = (IntPtr)(delegate*<void>)&InternalShaderSource;
private IntPtr textureImage2dPtr = (IntPtr)(delegate*<void>)&InternalTextureImage2d;
private IntPtr TextureParameterPtr = (IntPtr)(delegate*<void>)&InternalTextureParameter;
private IntPtr UseProgramPtr = (IntPtr)(delegate*<void>)&InternalUseProgram;
@@ -56,16 +58,21 @@ namespace StgSharp.Graphics
private static void InternalBindBuffer()
{
M128 m = default;
M128 m = new M128();
paramQueue.TryDequeue(out m);
paramQueue.context->glBindBuffer(m.UInt32[0], *(uint*)m.UInt64[1]);
uint usage = m.Read<uint>(0);
uint handle = *(uint*)m.Read<ulong>(1);
paramQueue.context->glBindBuffer(usage, handle);
}
private static void InternalBindTexture()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glBindTexture(m.UInt32[0], *(uint*)m.UInt64[1]);
paramQueue.context->glBindTexture(
m.Read<uint>(0),
*(uint*)m.Read<ulong>(1)
);
}
private static void InternalBindVertexArray()
@@ -94,53 +101,14 @@ namespace StgSharp.Graphics
while (paramQueue.context->glGetError() != 0) { }
}
private static void InternalCompileShader()
private static unsafe void InternalCompileShader()
{
IntPtr shaderHandle, codeHandle;
int count, type;
paramQueue.TryDequeue(out type, out count);
for (int i = 0; i < count; i++)
{
paramQueue.TryDequeue(out shaderHandle, out codeHandle);
GCHandle code = GCHandle.FromIntPtr(codeHandle);
byte[] c = (byte[])code.Target;
Console.WriteLine(Encoding.UTF8.GetString(c));
*(uint*)shaderHandle = InternalIO.InternalLoadShaderCode(paramQueue.context, (byte*)codeHandle, type);
if (*(uint*)shaderHandle == 0)
{
IntPtr logPtr = InternalIO.InternalReadSSCLog();
string log;
try
{
byte[] logByte = new byte[512];
Marshal.Copy(logPtr, logByte, 0, 512);
log = Encoding.UTF8.GetString(logByte);
log = log.Replace("\0", "");
bool isUseful = false;
foreach (char item in log)
{
if (!char.IsControl(item))
{
isUseful = true; break;
}
}
if (!isUseful)
{
throw new Exception();
}
}
catch (Exception)
{
log = "Failed to compile shader.\nFailed to get GLSL compiler log";
}
InternalIO.InternalWriteLog(log, LogType.Error);
}
GCHandle.FromIntPtr(codeHandle).Free();
}
IntPtr handle = default;
paramQueue.TryDequeue(out handle);
paramQueue.context->glCompileShader(*(uint*)handle);
}
//TODO
private static void InternalCreateProgram()
{
IntPtr programHandle = default;
@@ -153,25 +121,36 @@ namespace StgSharp.Graphics
uint* programHandle;
M128 m = default;
paramQueue.TryDequeue(out m);
programHandle = (uint*)m.UInt64[0];
for (int i = 0; i < m.Int32[2]; i++)
programHandle = (uint*)m.Read<ulong>(0);
for (int i = 0; i < m.Read<int>(2); i++)
{
*(programHandle + i) = paramQueue.context->glCreateProgram();
}
}
private static unsafe void InternalCreateShader()
{
M128 m = default;
paramQueue.TryDequeue(out m);
int count = m.Read<int>(0); uint type = m.Read<uint>(1); uint* handle = (uint*)m.Read<ulong>(1);
for (int i = 0; i < count; i++)
{
*(handle + i) = paramQueue.context->glCreateShader(type);
}
}
private static void InternalDeleteBuffer()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glDeleteBuffers(m.UInt32[2], (uint*)m.UInt64[0]);
paramQueue.context->glDeleteBuffers(m.Read<uint>(2), (uint*)m.Read<ulong>(0));
}
private static void InternalDeleteVertexArrays()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glDeleteVertexArrays((uint)m.Int32[0], (uint*)m.UInt64[0]);
paramQueue.context->glDeleteVertexArrays((uint)m.Read<int>(0), (uint*)m.Read<ulong>(0));
}
private static void InternalDrawElements()
@@ -187,21 +166,21 @@ namespace StgSharp.Graphics
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glGenBuffers(m.UInt32[2], (uint*)m.UInt64[0]);
paramQueue.context->glGenBuffers(m.Read<uint>(2), (uint*)m.Read<ulong>(0));
}
private static void InternalGenTextures()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glGenTextures(m.UInt32[2], (uint*)m.UInt64[0]);
paramQueue.context->glGenTextures(m.Read<uint>(2), (uint*)m.Read<ulong>(0));
}
private static void InternalGenVertexArrays()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glGenVertexArrays(m.UInt32[2], (uint*)m.UInt64[0]);
paramQueue.context->glGenVertexArrays(m.Read<uint>(2), (uint*)m.Read<ulong>(0));
}
private static void InternalGetUniformLocation()
@@ -215,11 +194,12 @@ namespace StgSharp.Graphics
private static void InternalSetBufferData()
{
uint type = 0, use = 0;
IntPtr size = default, handle = default;
IntPtr size = default;
glArrayHandle array = default;
paramQueue.TryDequeue(out type, out use);
paramQueue.TryDequeue(out size, out handle);
paramQueue.context->glBufferData(type, size, (void*)handle, use);
GCHandle.FromIntPtr(handle).Free();
paramQueue.TryDequeue(out size);
paramQueue.TryDequeue(out array);
paramQueue.context->glBufferData(type, size, array.AddressOfArray(), use);
}
private static void InternalSetVertexAttribute()
@@ -232,6 +212,18 @@ namespace StgSharp.Graphics
paramQueue.context->glEnableVertexAttribArray(index);
}
private static unsafe void InternalShaderSource()
{
IntPtr handle = default;
byte[] stream = Array.Empty<byte>();
paramQueue.TryDequeue(out handle);
paramQueue.TryDequeue(out stream);
fixed (byte* sPtr = stream)
{
paramQueue.context->glShaderSource(*(uint*)handle, 1, &sPtr, (void*)0);
}
}
private static void InternalTextureImage2d()
{
IntPtr pix = IntPtr.Zero;
@@ -240,14 +232,13 @@ namespace StgSharp.Graphics
paramQueue.TryDequeue(out level, out internalFormat, out width, out height);
paramQueue.TryDequeue(out target, out format, out type);
paramQueue.context->glTexImage2D(target, (int)level, internalFormat, width, height, 0, format, type, (void*)pix);
GCHandle.FromIntPtr(pix).Free();
}
private static void InternalTextureParameter()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glTextureParameteri(m.UInt32[0], m.UInt32[2], m.Int32[3]);
paramQueue.context->glTextureParameteri(m.Read<uint>(0), m.Read<uint>(2), m.Read<int>(3));
}
private static void InternalUseProgram()
@@ -261,7 +252,7 @@ namespace StgSharp.Graphics
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glViewport(m.Int32[0], m.Int32[1], m.UInt32[2], m.UInt32[3]);
paramQueue.context->glViewport(m.Read<int>(0), m.Read<int>(1), m.Read<uint>(2), m.Read<uint>(3));
}
private void InternalMakeContexCurrent(Form form)
@@ -269,11 +260,6 @@ namespace StgSharp.Graphics
throw new NotImplementedException();
}
private void InternalSetValue(Uniform<float, float, float, float> uniform, float v0, float v1, float v2, float v3)
{
throw new NotImplementedException();
}
private void InternalSwitchContext()
{
throw new NotImplementedException();
@@ -1,4 +1,5 @@
using StgSharp;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
@@ -19,9 +20,10 @@ namespace StgSharp.Graphics
return new ShaderProgram(GL);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected Shader CreateShader(ShaderType type, int count)
{
return new Shader(this.gl, new glHandleSet(count), type);
return new Shader(this.gl, gl.CreateShaderSet(count, type), type);
}
}
@@ -38,18 +40,20 @@ namespace StgSharp.Graphics
type = usage;
}
/// <summary>
/// Compile the vertex shader to be attached to this shader.
/// </summary>
/// <param name="code">GLSL code of vertex shader.</param>
public unsafe void Compile(params string[] code)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void Compile(int index, string code)
{
glHandleSet.Copy(GL.CompileShader(type, code), handle);
if (code == null)
{
throw new ArgumentNullException("Must provide enough code");
}
GL.LoadShaderSource(handle[index], code + '\0');
GL.CompileShader(handle[index]);
}
//TODO:写链接着色器的文档注释
/// <summary>
///
///
/// </summary>
/// <param name="target"></param>
/// <param name="index"></param>
@@ -103,8 +107,8 @@ namespace StgSharp.Graphics
}
/// <summary>
/// Get a uniform form current shader,
/// the value type of the uniform should be provided.
/// Get a uniform form current shader,
/// the value type of the uniform should be provided.
/// </summary>
/// <param name="name">Name of the Uniform in shader code</param>
/// <returns></returns>
@@ -113,9 +117,10 @@ namespace StgSharp.Graphics
{
return new Uniform<T>(this, name, GL);
}
/// <summary>
/// Get a uniform form current shader,
/// the value type of the uniform should be provided.
/// Get a uniform form current shader,
/// the value type of the uniform should be provided.
/// </summary>
/// <param name="name">Name of the Uniform in shader code</param>
/// <returns></returns>
@@ -125,9 +130,10 @@ namespace StgSharp.Graphics
{
return new Uniform<T, U>(this, name, GL);
}
/// <summary>
/// Get a uniform form current shader,
/// the value type of the uniform should be provided.
/// Get a uniform form current shader,
/// the value type of the uniform should be provided.
/// </summary>
/// <param name="name">Name of the Uniform in shader code</param>
/// <returns></returns>
@@ -148,4 +154,4 @@ namespace StgSharp.Graphics
GL.UseProgram(handle);
}
}
}
}
@@ -0,0 +1,108 @@
using StgSharp.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace StgSharpDebug.Graphics.GL.ShaderEdit
{
public class ShaderGenerator
{
private List<string> codeList;
private List<ShaderFunction> usedShaderFunctions;
public Uniform<T> DefineUniform<T>() where T : struct
{
throw new NotImplementedException();
}
}
public class ShaderFunction
{
private string name;
private List<string> codeList;
private List<ShaderParameter> inputList;
private ShaderParameter outPut;
public int ParameterCount
{
get { return inputList.Count; }
}
public void EmitCall(ShaderFunction function, ShaderParameter outPut, params ShaderParameter[] inputParams)
{
if (inputParams.Length != function.ParameterCount)
{
throw new ArgumentException("Provided parameters does not match function's parameter list.");
}
if (!ShaderParameter.IsSameType(outPut, function.outPut))
{
throw new Exception("Provided parameters does not match function's parameter list.");
}
for (int i = 0; i < inputParams.Length; i++)
{
if (!ShaderParameter.IsSameType(function.inputList[i], inputParams[i]))
{
throw new Exception("Provided parameters does not match function's parameter list.");
}
}
string codeLine = "";
if (outPut != ShaderParameter.Void)
{
codeLine += $"{outPut.name} = ";
}
codeLine += $"{function.name}(";
foreach (ShaderParameter inputParam in inputParams)
{
codeLine += inputParam.name;
}
codeLine += ");\n";
codeList.Add(codeLine);
}
}
public class ShaderParameter
{
internal readonly InternalShaderType type;
internal readonly string name;
internal readonly bool isReadOnly;
public ShaderParameter(string name, InternalShaderType type)
{
this.name = name;
this.type = type;
isReadOnly = false;
}
public static readonly ShaderParameter Void = new ShaderParameter("", InternalShaderType.Void);
public static bool IsSameType(ShaderParameter param1, ShaderParameter param2)
{
if (param1.type != InternalShaderType.Struct)
{
return param1.type == param2.type;
}
else
{
throw new Exception("Struct is currently not supported.");
}
}
}
public enum InternalShaderType
{
#pragma warning disable CS1591
Struct, Void,
Int, UInt,
Float, Double,
Vector2, Vector3, Vector4,
VectorD2, VectorD3, VectorD4,
Matrix2x2, Matrix2x3, Matrix2x4,
Matrix3x2, Matrix3x3, Matrix3x4,
Matrix4x2, Matrix4x3, Matrix4x4,
#pragma warning restore CS1591
}
}
@@ -1,4 +1,5 @@
using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -8,7 +9,6 @@ using System.Text;
namespace StgSharp.Graphics
{
public class Uniform<T> where T : struct
{
internal glSHandle id;
@@ -25,10 +25,8 @@ namespace StgSharp.Graphics
{
this.id = id;
}
}
public class Uniform<T, U>
where T : struct
where U : struct
@@ -88,12 +86,10 @@ namespace StgSharp.Graphics
{
this.id = id;
}
}
internal partial class GLonRender : IglFunc
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.SetValue(Uniform<float> uniform, float v0)
{
@@ -156,8 +152,9 @@ namespace StgSharp.Graphics
unsafe void IglFunc.SetValue(Uniform<float, float> uniform, float f0, float f1)
{
M128 m = default;
m.UInt64[0] = (ulong)uniform.id.valuePtr;
m.Single[2] = f0; m.Single[3] = f1;
m.Write<ulong>(0, (ulong)uniform.id.valuePtr);
m.Write<float>(2, f0);
m.Write<float>(3, f1);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(UniformVal2fPtr);
}
@@ -179,8 +176,8 @@ namespace StgSharp.Graphics
unsafe void IglFunc.SetValue(Uniform<float> uniform, float f0)
{
M128 m = default;
m.UInt64[0] = (ulong)uniform.id.valuePtr;
m.Single[2] = f0;
m.Write<ulong>(0, (ulong)uniform.id.valuePtr);
m.Write<float>(2, f0);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(UniformVal1fPtr);
}
@@ -188,8 +185,8 @@ namespace StgSharp.Graphics
unsafe void IglFunc.SetValue(Uniform<int> uniform, int i0)
{
M128 m = default;
m.UInt64[0] = (ulong)uniform.id.valuePtr;
m.Int32[2] = i0;
m.Write<ulong>(0, (ulong)uniform.id.valuePtr);
m.Write<int>(2, i0);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(UniformVal1fPtr);
}
@@ -205,28 +202,28 @@ namespace StgSharp.Graphics
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glUniform1f(*(int*)m.UInt64[0], m.Single[2]);
paramQueue.context->glUniform1f(*(int*)m.Read<uint>(0), m.Read<uint>(2));
}
private static unsafe void UniformVal1fv()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glUniform1fv(*(int*)m.UInt64[0], 4, (Vec4d*)m.UInt64[0]);
paramQueue.context->glUniform1fv(*(int*)m.Read<ulong>(0), 4, (Vec4d*)&m.vec);
}
private static void UniformVal1i()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glUniform1i(*(int*)m.UInt64[0], m.Int32[2]);
paramQueue.context->glUniform1i(*(int*)m.Read<uint>(0), m.Read<int>(2));
}
private static void UniformVal2f()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glUniform2f(*(int*)m.UInt64[0], m.Single[2], m.Single[3]);
paramQueue.context->glUniform2f(*(int*)m.Read<uint>(0), m.Read<float>(2), m.Read<float>(3));
}
private static void UniformVal3f()
@@ -235,16 +232,16 @@ namespace StgSharp.Graphics
M128 m = default;
paramQueue.TryDequeue(out handle);
paramQueue.TryDequeue(out m);
paramQueue.context->glUniform3f(*(int*)handle, m.Single[0], m.Single[1], m.Single[2]);
paramQueue.context->glUniform3f(*(int*)handle, m.Read<float>(0), m.Read<float>(1), m.Read<float>(2));
}
private static void UniformVal4f()
{
IntPtr handle = IntPtr.Zero;
M128 m = default;
paramQueue.TryDequeue(out handle);
paramQueue.TryDequeue(out m);
paramQueue.context->glUniform4f(*(int*)handle, m.Single[0], m.Single[1], m.Single[2], m.Single[2]);
paramQueue.context->glUniform4f(*(int*)handle, m.Read<float>(0), m.Read<float>(1), m.Read<float>(2), m.Read<float>(3));
}
}
}
}
@@ -1,4 +1,5 @@
using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -746,7 +747,6 @@ namespace StgSharp.Graphics
{
if (!form.IsSingleThread)
{
Console.WriteLine("This context runs on two threads");
form.RenderThread.Start();
}
}
@@ -1001,7 +1001,7 @@ namespace StgSharp.Graphics
contextPtr->glBeginQuery = (delegate*<uint, uint, void>)load("glBeginQuery");
contextPtr->glBindBuffer = (delegate*<uint, uint, void>)load("glBindBuffer");
contextPtr->glBufferData = (delegate*<uint, IntPtr, void*, uint, void>)load("glBufferData");
contextPtr->glBufferData = (delegate*<uint, IntPtr, IntPtr, uint, void>)load("glBufferData");
contextPtr->glBufferSubData = (delegate*<uint, IntPtr, UIntPtr, void*, void>)load("glBufferSubData");
contextPtr->glDeleteBuffers = (delegate*<uint, uint*, void>)load("glDeleteBuffers");
contextPtr->glDeleteQueries = (delegate*<uint, uint*, void>)load("glDeleteQueries");
@@ -1065,7 +1065,7 @@ namespace StgSharp.Graphics
contextPtr->glIsProgram = (delegate*<uint, bool>)load("glIsProgram");
contextPtr->glIsShader = (delegate*<uint, bool>)load("glIsShader");
contextPtr->glLinkProgram = (delegate*<uint, void>)load("glLinkProgram");
contextPtr->glShaderSource = (delegate*<uint, uint, byte*, string, byte*, void>)load("glShaderSource");
contextPtr->glShaderSource = (delegate* unmanaged[Stdcall]<uint, uint, byte**, void*, void>)load("glShaderSource");
contextPtr->glStencilFuncSeparate = (delegate*<uint, uint, int, uint, void>)load("glStencilFuncSeparate");
contextPtr->glStencilMaskSeparate = (delegate*<uint, uint, void>)load("glStencilMaskSeparate");
contextPtr->glStencilOpSeparate = (delegate*<uint, uint, uint, uint, void>)load("glStencilOpSeparate");
+8 -9
View File
@@ -1,6 +1,8 @@
using StgSharp.Math;
using System;
using System.Collections.Concurrent;
using System.ComponentModel.Design;
namespace StgSharp.Graphics
{
@@ -84,21 +86,18 @@ namespace StgSharp.Graphics
void ClearGraphicError();
/// <summary>
/// Conpile the provided shader code in string form.
/// </summary>
/// <param name="shaderCodeArray">A string array contains all shaders' code</param>
/// <param name="shaderType">An <see cref="ShaderType"/> enum represent the type of the shader to
/// conpile, including vertex shader and fragment shader.</param>
/// <returns>An handle of conpiled shader</returns>
glHandleSet CompileShader(ShaderType shaderType, params string[] shaderCodeArray);
/// <summary>
/// Same function as OpenGL api, glCreateProgram
/// </summary>
/// <returns>A handle representing a single shader program</returns>
glHandle CreateProgram();
glHandleSet CreateShaderSet(int count, ShaderType type);
void LoadShaderSource(glHandle shaderHandle, string codeStream);
void CompileShader(glHandle shaderhandle);
/// <summary>
/// Same function as OpenGL api, glCreateProgram.
/// Used for creating multiple shader program.