Files
stgsharp/源代码/StgSharp/Graphics/GL/GLonUpdate.cs
T

488 lines
14 KiB
C#

using StgSharp.Logic;
using StgSharp.Math;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace StgSharp.Graphics
{
internal unsafe partial class GLonUpdate : IglFunc
{
internal GLonUpdateInternal glUpdateInternal;
private ConcurrentQueue<IntPtr> glActionQueue;
private ConcurrentQueue<Vec4d> glParamQueue;
private FrameTimeoutException refreshSignal = new();
internal ConcurrentQueue<IntPtr> GLactionQueue
{
get
{
return glActionQueue;
}
}
internal GLonUpdate(GLcontext* context)
{
glUpdateInternal = new GLonUpdateInternal();
glParamQueue = glUpdateInternal.paramQueue;
glActionQueue = new ConcurrentQueue<IntPtr>();
}
void IglFunc.ActiveTexture(uint type)
{
glParamQueue.Enqueue(new Vec4d(type));
glActionQueue.Enqueue(glUpdateInternal.ActiveTexturePtr);
}
void IglFunc.AfterAlign(Form form, Action action)
{
throw new NotImplementedException();
}
void IglFunc.AlignFrame(Form form)
{
if (form.timer.alignSign.byte0 > 0)
{
throw new InvalidOperationException();
}
form.timer.alignSign.byte0++;
form.timer.WaitNextFrame();
throw refreshSignal;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.AttachShader(uint program, uint shader)
{
glActionQueue.Enqueue(
() => api->glAttachShader(program, shader)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.BindBuffer(BufferType bufferType, uint index)
{
glActionQueue.Enqueue(
() => api->glBindBuffer((uint)bufferType, index)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.BindTexture(uint type, uint texture)
{
glActionQueue.Enqueue(
() => api->glBindTexture((uint)type, texture)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.BindVertexArray(uint index)
{
glActionQueue.Enqueue(
() => api->glBindVertexArray((uint)index)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.Clear(MaskBufferBit mask)
{
glActionQueue.Enqueue(
() => api->glClear((uint)mask)
);
}
void IglFunc.ClearColor(float R, float G, float B, float A)
{
glActionQueue.Enqueue(
() => api->glClearColor(R, G, B, A)
);
}
void IglFunc.ClearGraphicError()
{
glActionQueue.Enqueue(
() =>
{
while (api->glGetError() != 0) { };
}
);
}
uint IglFunc.CompileShader(string shaderCode, ShaderType shaderType)
{
int id = -1;
glActionQueue.Enqueue(
() =>
{
uint shaderID;
shaderCode += '\0';
shaderID = InternalIO.InternalLoadShaderCode(shaderCode, (int)shaderType);
if (shaderID == 0)
{
IntPtr logPtr = InternalIO.InternalReadLog();
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);
}
Interlocked.Add(ref id, (int)(shaderID + 1));
}
);
while (id < 0) { }
return (uint)id;
}
uint IglFunc.CreateProgram()
{
int id = -1;
glActionQueue.Enqueue(
() =>
{
int ret = (int)api->glCreateProgram();
Interlocked.Add(ref id, ret + 1);
}
);
while (id < 0) { }
return (uint)id;
}
IntPtr IglFunc.CreateWindowHandle(int width, int height, string tittle, IntPtr monitor, Form share)
{
byte[] tittleBytes = Encoding.UTF8.GetBytes(tittle);
IntPtr windowPtr;
if (share == null)
{
windowPtr = InternalIO.glfw.CreateWindow(
width, height, tittleBytes, monitor, IntPtr.Zero);
}
else
{
windowPtr = InternalIO.glfw.CreateWindow(
width, height, tittleBytes, monitor, share.windowID);
}
if ((long)windowPtr == -1 || (long)windowPtr == -2)
{
throw new Exception("Cannot create window instance!");
}
else
{
return windowPtr;
}
}
uint IglFunc.CreatGraphicProgram()
{
int id = 0;
glActionQueue.Enqueue(
() =>
{
Interlocked.Add(ref id, (int)(api->glCreateProgram() + 1));
}
);
while (id < 0) { }
return (uint)id;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.SetWindowShouldClose(Form form, bool mark)
{
InternalIO.glfw.SetWindowShouldClose(form.windowID, mark ? 1 : 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.DeleteBuffer(uint handle)
{
glActionQueue.Enqueue(
() =>
{
uint target = handle;
api->glDeleteBuffers(1, &target);
}
);
}
unsafe void IglFunc.DeleteBuffers(uint count, uint[] handle)
{
glActionQueue.Enqueue(
() =>
{
uint[] h = handle;
fixed (uint* p = h)
{
api->glDeleteBuffers(count, p);
}
}
);
}
unsafe void IglFunc.DeleteVertexArrays(uint count, uint[] arrays)
{
glActionQueue.Enqueue(
() =>
{
uint[] target = arrays;
fixed (uint* ptr = target)
{
api->glDeleteVertexArrays(count, ptr);
}
}
);
}
void IglFunc.DrawElements(GepmetryType mode, uint count, Type type, IntPtr ptr)
{
uint a;
switch (Type.GetTypeCode(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 suporrted.",
LogType.Error);
return;
}
glActionQueue.Enqueue(
() =>
{ api->glDrawElements((uint)mode, count, a, (void*)ptr); }
);
}
unsafe void IglFunc.GenBuffers(uint count, uint* ptr)
{
glActionQueue.Enqueue(
() =>
{
api->glGenBuffers(count, ptr);
}
);
}
unsafe void IglFunc.GenTextures(uint count, uint* ptr)
{
glActionQueue.Enqueue(
() =>
{
api->glGenTextures(count, ptr);
}
);
}
unsafe void IglFunc.GenVertexArrays(uint count, uint* ptr)
{
glActionQueue.Enqueue(
() =>
{
api->glGenVertexArrays(count, ptr);
}
);
}
public void GetGraphicError()
{
throw new NotImplementedException();
}
void IglFunc.GetGraphicError()
{
throw new NotImplementedException();
}
unsafe int IglFunc.GetUniformLocation(uint program, byte* ptr)
{
int ret = -1;
glActionQueue.Enqueue(
() =>
{
int location = api->glGetUniformLocation(program, ptr);
Interlocked.Add(ref ret, location + 1);
}
);
while (ret < 0) { }
return ret;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
bool IglFunc.GetWindowShouldClose(Form form)
{
return InternalIO.glfw.WindowShouldClose(form.windowID) == 0;
}
public void GLswapBuffers(Form form)
{
if (form.windowID == IntPtr.Zero)
{
return;
}
InternalIO.glfw.SwapBuffers(form.windowID);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.MakeContexCurrent(Form form)
{
if (form.windowID == IntPtr.Zero)
{
return;
}
InternalIO.glfw.MakeContextCurrent(form.windowID);
}
void IglFunc.SetBufferData<T>(BufferType bufferType, T bufferData, BufferUsage usage)
{
glActionQueue.Enqueue(
() =>
{
T data = bufferData;
int size = sizeof(T);
api->glBufferData(
(uint)bufferType,
(UIntPtr)size,
&data,
(uint)usage);
}
);
}
void IglFunc.SetBufferData<T>(BufferType bufferType, T[] bufferArray, BufferUsage usage)
{
glActionQueue.Enqueue(
() =>
{
int size = bufferArray.Length * Marshal.SizeOf(typeof(T));
if (bufferArray == null)
{
return;
}
fixed (void* bufferPtr = bufferArray)
api->glBufferData(
(uint)bufferType,
(UIntPtr)size,
bufferPtr,
(uint)usage);
}
);
}
void IglFunc.SetFrameBufferSizeCallback(IntPtr id, FrameBufferSizeHandler handler)
{
IntPtr handlerPtr = Marshal.GetFunctionPointerForDelegate(handler);
InternalIO.glfw.SetFramebufferSizeCallback(id, handlerPtr);
}
void IglFunc.SetVertexAttribute(uint index, int size, Type t, bool normalized, uint stride, int pointer)
{
glActionQueue.Enqueue(
() =>
{
uint type = InternalIO.GLtype[t];
api->glVertexAttribPointer(index, size, type, normalized, stride, (void*)pointer);
api->glEnableVertexAttribArray(index);
}
);
}
void IglFunc.SwapBuffers(Form form)
{
glActionQueue.Enqueue(
() =>
{
if (form.windowID == IntPtr.Zero)
{
return;
}
InternalIO.glfw.SwapBuffers(form.windowID);
}
);
}
unsafe void IglFunc.TextureImage2d(uint target, int level, uint internalformat, uint width, uint height, uint format, uint type, void* pixels)
{
glActionQueue.Enqueue(
() =>
{
api->glTexImage2D(target, level, internalformat, width, height, 0, format, type, pixels);
}
);
}
void IglFunc.TextureParameter(uint target, uint pname, int param)
{
glActionQueue.Enqueue(
() =>
{
api->glTextureParameteri(target, pname, param);
}
);
}
void IglFunc.UseProgram(uint rogram)
{
glActionQueue.Enqueue(
() =>
{
api->glUseProgram(rogram);
}
);
}
void IglFunc.Viewport(int x, int y, uint width, uint height)
{
glActionQueue.Enqueue(
() =>
{
api->glViewport(x, y, width, height);
}
);
}
}
}