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

403 lines
13 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
{
private static FrameOperationBuffer frameOpBuffer;
private GLcontext* api;
private IntPtr SwapBufferPtr = (IntPtr)(delegate*<IntPtr, void>)&InternalIO.glfwSwapBuffers;
private glSHandle unupdatedFrame = glSHandle.Alloc(0);
internal GLonUpdate(GLcontext* context)
{
if (frameOpBuffer == null)
{
frameOpBuffer = new FrameOperationBuffer();
}
api = context;
}
GLcontext* IglFunc.Context
{
get => api;
}
public FrameOperationBuffer ActivatedOperationBuffer
{
get { return frameOpBuffer; }
set { frameOpBuffer = value; }
}
void IglFunc.ActiveTexture(uint type)
{
frameOpBuffer.Enqueue(type);
frameOpBuffer.Enqueue(ActiveTexturePtr);
}
void IglFunc.AfterAlign(Form form, Action action)
{
throw new NotImplementedException();
}
void IglFunc.AlignFrame(Form form)
{
throw new NotImplementedException();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.AttachShader(glHandle program, glHandle shader)
{
frameOpBuffer.Enqueue(program, shader);
frameOpBuffer.Enqueue(AttachShaderPtr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.BindBuffer(BufferType bufferType, glHandle handle)
{
M128 m = default;
m.Write<uint>(0, (uint)bufferType);
m.Write<ulong>(1, (ulong)handle.valuePtr);
frameOpBuffer.Enqueue(m);
frameOpBuffer.Enqueue(BindBufferPtr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.BindTexture(uint type, glHandle texture)
{
M128 m = default;
m.Write<uint>(0, (uint)type);
m.Write<ulong>(1, (ulong)texture.valuePtr);
frameOpBuffer.Enqueue(m);
frameOpBuffer.Enqueue(BindTexturePtr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.BindVertexArray(glHandle index)
{
frameOpBuffer.Enqueue(index);
frameOpBuffer.Enqueue(BindVertexArrayPtr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.Clear(MaskBufferBit mask)
{
frameOpBuffer.Enqueue((int)mask);
frameOpBuffer.Enqueue(ClearPtr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.ClearColor(float R, float G, float B, float A)
{
frameOpBuffer.Enqueue(R, G, B, A);
frameOpBuffer.Enqueue(ClearColorPtr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.ClearGraphicError()
{
frameOpBuffer.Enqueue(ClearGraphicErrorPtr);
}
unsafe void IglFunc.CompileShader(glHandle shaderhandle)
{
frameOpBuffer.Enqueue((IntPtr)shaderhandle.valuePtr);
frameOpBuffer.Enqueue(CompileShaderPtr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
glHandle IglFunc.CreateProgram()
{
glHandle h = glHandle.Alloc(1);
frameOpBuffer.Enqueue(h);
frameOpBuffer.Enqueue(CreateProgramPtr);
return h;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
glHandleSet IglFunc.CreateProgram(int count)
{
M128 m = default;
glHandleSet h = new glHandleSet(count);
m.Write<ulong>(0, (ulong)h.handle);
m.Write<int>(2, count);
frameOpBuffer.Enqueue(m);
frameOpBuffer.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);
frameOpBuffer.Enqueue(m);
frameOpBuffer.Enqueue(CreateShaderPtr);
return ret;
}
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.glfwCreateWindow(
width, height, tittleBytes, monitor, IntPtr.Zero);
}
else
{
windowPtr = InternalIO.glfwCreateWindow(
width, height, tittleBytes, monitor, share.windowID);
}
if ((long)windowPtr == -1 || (long)windowPtr == -2)
{
throw new Exception("Cannot create window instance!");
}
else
{
return windowPtr;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
glHandle IglFunc.CreatGraphicProgram()
{
glHandle h = glHandle.Alloc(1);
frameOpBuffer.Enqueue(h);
frameOpBuffer.Enqueue(CreateProgramPtr);
return h;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.DeleteBuffer(glHandle handle)
{
M128 m = default;
m.Write<ulong>(0, (ulong)handle.valuePtr);
m.Write(2, 1);
frameOpBuffer.Enqueue(m);
frameOpBuffer.Enqueue(DeleBufferPtr);
}
unsafe void IglFunc.DeleteBuffers(glHandleSet handle)
{
M128 m = default;
m.Write<ulong>(0, (ulong)handle.handle);
m.Write<uint>(2, (uint)handle.length);
frameOpBuffer.Enqueue(m);
frameOpBuffer.Enqueue(DeleBufferPtr);
}
unsafe void IglFunc.DeleteVertexArrays(glHandleSet arrays)
{
M128 m = default; m.Write<ulong>(1, (ulong)arrays.handle);
m.Write<int>(0, arrays.length);
frameOpBuffer.Enqueue(m);
frameOpBuffer.Enqueue(DeleteVertexArrayPtr);
}
void IglFunc.DrawElements(GepmetryType mode, uint count, TypeCode type, IntPtr ptr)
{
uint a;
switch (type)
{
case TypeCode.UInt32:
a = GLconst.UNSIGNED_INT;
break;
case TypeCode.UInt16:
a = GLconst.UNSIGNED_SHORT;
break;
case TypeCode.Byte:
a = GLconst.UNSIGNED_BYTE;
break;
default:
InternalIO.InternalWriteLog(
$"Parameter error in parameter {type} method DrawElements. " +
$"Only {TypeCode.Byte}, {TypeCode.UInt16}, {TypeCode.UInt32} types are suporrted.",
LogType.Error);
return;
}
frameOpBuffer.Enqueue((uint)mode, count, a);
frameOpBuffer.Enqueue(ptr);
frameOpBuffer.Enqueue(DrawElementsPtr);
}
unsafe glHandleSet IglFunc.GenBuffers(int count)
{
glHandleSet h = new glHandleSet(count);
M128 m = default; m.Write<uint>(2, (uint)count); m.Write<ulong>(0, (ulong)h.handle);
frameOpBuffer.Enqueue(m);
frameOpBuffer.Enqueue(GenBufferPtr);
return h;
}
unsafe glHandleSet IglFunc.GenTextures(int count)
{
glHandleSet h = new glHandleSet(count);
M128 m = default; m.Write<uint>(2, (uint)count); m.Write<ulong>(0, (ulong)h.handle);
frameOpBuffer.Enqueue(m);
frameOpBuffer.Enqueue(GenTexturesPtr);
return h;
}
unsafe glHandleSet IglFunc.GenVertexArrays(int count)
{
glHandleSet h = new glHandleSet(count);
M128 m = default; m.Write<uint>(2, (uint)count); m.Write<ulong>(0, (ulong)h.handle);
frameOpBuffer.Enqueue(m);
frameOpBuffer.Enqueue(GenTexturesPtr);
return h;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe glSHandle IglFunc.GetUniformLocation(glHandle program, string ptr)
{
glSHandle h = glSHandle.Alloc(0);
M128 m = default;
m.Write<ulong>(0, (ulong)program.Handle);
m.Write<ulong>(1, (ulong)GCHandle.Alloc(Encoding.UTF8.GetBytes(ptr), GCHandleType.Pinned).AddrOfPinnedObject());
frameOpBuffer.Enqueue(m);
frameOpBuffer.Enqueue((IntPtr)h.valuePtr);
frameOpBuffer.Enqueue(GetUniformLocationPtr);
return h;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
bool IglFunc.GetWindowShouldClose(Form form)
{
return InternalIO.glfwWindowShouldClose(form.windowID) == 0;
}
public void GLswapBuffers(Form form)
{
if (form.windowID == IntPtr.Zero)
{
return;
}
InternalIO.glfwSwapBuffers(form.windowID);
}
void IglFunc.LoadShaderSource(glHandle shaderHandle, string codeStream)
{
frameOpBuffer.Enqueue(shaderHandle);
frameOpBuffer.Enqueue(codeStream);
frameOpBuffer.Enqueue(ShaderSourcePtr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.MakeContexCurrent(Form form)
{
if (form.windowID == IntPtr.Zero)
{
return;
}
InternalIO.glfwMakeContextCurrent(form.windowID);
}
void IglFunc.SetBufferData<T>(BufferType bufferType, T bufferData, BufferUsage usage)
{
frameOpBuffer.Enqueue((uint)bufferType, (uint)usage);
frameOpBuffer.Enqueue((IntPtr)sizeof(T));
frameOpBuffer.Enqueue<T>(new T[] { bufferData });
frameOpBuffer.Enqueue(BufferDataPtr);
}
void IglFunc.SetBufferData<T>(BufferType bufferType, T[] bufferArray, BufferUsage usage)
{
frameOpBuffer.Enqueue((uint)bufferType, (uint)usage);
frameOpBuffer.Enqueue((IntPtr)(Marshal.SizeOf<T>() * bufferArray.Length));
frameOpBuffer.Enqueue<T>(bufferArray);
frameOpBuffer.Enqueue(BufferDataPtr);
}
void IglFunc.SetVertexAttribute(uint index, int size, TypeCode t, bool normalized, uint stride, int pointer)
{
uint type = InternalIO.GLtype[t];
frameOpBuffer.Enqueue(index, type, stride);
frameOpBuffer.Enqueue(size, normalized ? 1 : 0, pointer);
frameOpBuffer.Enqueue(SetVertexArrayPtr);
}
//TODO:双线程,换运行环境
void IglFunc.SwitchContext()
{
frameOpBuffer.context = api;
}
unsafe void IglFunc.TextureImage2d<T>(int target, int level, uint internalformat, uint width, uint height, uint format, uint type, glHandle pixels)
{
frameOpBuffer.Enqueue(pixels);
frameOpBuffer.Enqueue((uint)level, internalformat, width, height);
frameOpBuffer.Enqueue((uint)target, format, type);
frameOpBuffer.Enqueue(textureImage2dPtr);
}
void IglFunc.TextureParameter(int target, uint pname, int param)
{
M128 m = default;
m.Write<int>(0, target);
m.Write<uint>(2, pname);
m.Write<int>(3, param);
frameOpBuffer.Enqueue(m);
frameOpBuffer.Enqueue(TextureParameterPtr);
}
void IglFunc.UseProgram(glHandle program)
{
frameOpBuffer.Enqueue((IntPtr)program.valuePtr);
frameOpBuffer.Enqueue(UseProgramPtr);
}
void IglFunc.Viewport(int x, int y, uint width, uint height)
{
Vec4d v = new Vec4d(x, y, (int)width, (int)height);
frameOpBuffer.Enqueue(v);
frameOpBuffer.Enqueue(ViewPortPtr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.frameworkHandleEnqueue(IntPtr methodHandle, params M128[] param)
{
foreach (M128 item in param)
{
frameOpBuffer.Enqueue(item);
}
frameOpBuffer.Enqueue(methodHandle);
}
bool IglFunc.FrameCanUpdate
{
get
{
if (unupdatedFrame.DirectValue > 5)
{
return false;
}
else
{
*unupdatedFrame.valuePtr += 1;
frameOpBuffer.Enqueue((IntPtr)unupdatedFrame.valuePtr);
frameOpBuffer.Enqueue(CanUpdatePtr);
return true;
}
}
}
}
}