using StgSharp.Logic; using StgSharp.Math; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; namespace StgSharp.Graphics { internal unsafe partial class GLonRender : IglFunc { internal GLcontext* api; FrameTimeoutException refreshSIgnal = new FrameTimeoutException(); internal GLonRender(GLcontext* context) { api = context; } GLcontext* IglFunc.Context { get => api; } void IglFunc.SwitchContext() { } [MethodImpl(MethodImplOptions.AggressiveInlining)] void IglFunc.ActiveTexture(uint type) { api->glActiveTexture(type); } 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)] void IglFunc.AttachShader(glHandle program, glHandle shader) { api->glAttachShader(*program.valuePtr, *shader.valuePtr); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void IglFunc.BindBuffer(BufferType bufferType, glHandle handle) { api->glBindBuffer((uint)bufferType, *handle.valuePtr); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void IglFunc.BindTexture(uint type, glHandle texture) { api->glBindTexture(type, texture.DirectValue); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void IglFunc.BindVertexArray(glHandle handle) { api->glBindVertexArray(handle.Value); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void IglFunc.Clear(MaskBufferBit mask) { api->glClear((uint)mask); } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe void IglFunc.ClearColor(float R, float G, float B, float A) { api->glClearColor(R, G, B, A); } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe void IglFunc.ClearGraphicError() { while (api->glGetError() != 0) { } } glHandleSet IglFunc.CompileShader(ShaderType shaderType, params string[] shaderCodeArray) { int l = shaderCodeArray.Length; if (l == 0) { throw new ArgumentException("At least one pice of code is required."); } glHandleSet handleSet = new glHandleSet(l); for (int i = 0; i < l; i++) { uint shaderID; byte[] data = Encoding.UTF8.GetBytes(shaderCodeArray[i] + '\0'); fixed (byte* codePtr = data) { shaderID = InternalIO.InternalLoadShaderCode(api, codePtr, (int)shaderType); } 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); } return handleSet; } [MethodImpl(MethodImplOptions.AggressiveInlining)] glHandle IglFunc.CreateProgram() { return glHandle.Alloc(api->glCreateProgram()); } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe glHandleSet IglFunc.CreateProgram(int count) { glHandleSet h = new glHandleSet(count); for (int i = 0; i < count; i++) { h.SetValue(i, api->glCreateProgram()); } return h; } unsafe 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)] unsafe glHandle IglFunc.CreatGraphicProgram() { return glHandle.Alloc(api->glCreateProgram()); } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe void IglFunc.DeleteBuffer(glHandle handle) { api->glDeleteBuffers(1, (uint*)handle.Handle); } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe void IglFunc.DeleteBuffers(glHandleSet handle) { api->glDeleteBuffers((uint)handle.length, (uint*)handle.handle); handle.Free(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe void IglFunc.DeleteVertexArrays(glHandleSet arrays) { api->glDeleteVertexArrays((uint)arrays.length, (uint*)arrays.handle); arrays.Free(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe 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 {nameof(type)} method DrawElements. " + $"Only {typeof(uint).Name},{typeof(ushort).Name},{typeof(byte).Name} types are suporrted.", LogType.Error); return; } api->glDrawElements((uint)mode, count, a, (void*)ptr); } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe glHandleSet IglFunc.GenBuffers(int count) { glHandleSet h = new glHandleSet((int)count); api->glGenBuffers((uint)count, h.handle); return h; } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe glHandleSet IglFunc.GenTextures(int count) { glHandleSet h = new glHandleSet((int)count); api->glGenTextures((uint)count, h.handle); return h; } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe glHandleSet IglFunc.GenVertexArrays(int count) { glHandleSet h = new glHandleSet(count); api->glGenVertexArrays((uint)count, h.handle); return h; } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe glSHandle IglFunc.GetUniformLocation(glHandle program, string name) { byte[] code = Encoding.UTF8.GetBytes(name); fixed (byte* ptr = code) { return glSHandle.Alloc(api->glGetUniformLocation(program.DirectValue, ptr)); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe bool IglFunc.GetWindowShouldClose(Form form) { return InternalIO.glfwWindowShouldClose(form.windowID) == 0; } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe void IglFunc.MakeContexCurrent(Form form) { if (form.windowID == IntPtr.Zero) { InternalIO.InternalWriteLog($"From {nameof(form)} is not initted.", LogType.Warning); return; } InternalIO.glfwMakeContextCurrent(form.windowID); } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe void IglFunc.SetBufferData(BufferType bufferType, T bufferData, BufferUsage usage) { int size = sizeof(T); api->glBufferData( (uint)bufferType, (IntPtr)size, &bufferData, (uint)usage); } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe void IglFunc.SetBufferData(BufferType bufferType, T[] bufferArray, BufferUsage usage) { int size = bufferArray.Length * Marshal.SizeOf(typeof(T)); if (bufferArray == null) { return; } fixed (void* bufferPtr = bufferArray) api->glBufferData( (uint)bufferType, (IntPtr)size, bufferPtr, (uint)usage); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void IglFunc.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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void IglFunc.TextureImage2d(int target, int level, uint internalformat, uint width, uint height, uint format, uint type, T[] pixels) { Console.WriteLine(pixels); IntPtr source = GCHandle.Alloc(pixels, GCHandleType.Pinned).AddrOfPinnedObject(); api->glTexImage2D( (uint)target, level, internalformat, width, height, 0, format, type, (void*)source ); GCHandle.FromIntPtr(source).Free(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void IglFunc.TextureParameter(int target, uint pname, int param) { api->glTexParameteri((uint)target, pname, param); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void IglFunc.UseProgram(glHandle program) { api->glUseProgram(program.DirectValue); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void IglFunc.Viewport(int x, int y, uint width, uint height) { api->glViewport(x, y, width, height); } } }