using StgSharp.Math; using System; 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 GLonRender(GLcontext context) { api = context; } internal GLcontext api; [MethodImpl(MethodImplOptions.AggressiveInlining)] void IglFunc.ActiveTexture(uint type) { api.ActiveTexture(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.WaitNextFrame(); form.timer.alignSign.byte0++; if (form.timer.) { } } [MethodImpl(MethodImplOptions.AggressiveInlining)] void IglFunc.AttachShader(uint program, uint shader) { api.AttachShader(program, shader); } void IglFunc.BindBuffer(BufferType bufferType, uint index) { api.BindBuffer((uint)bufferType, index); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void IglFunc.BindTexture(uint type, uint texture) { api.BindTexture(type, texture); } void IglFunc.BindVertexArray(uint index) { api.BindVertexArray(index); } void IglFunc.Clear(MaskBufferBit mask) { api.Clear((uint)mask); } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe void IglFunc.ClearColor(float R, float G, float B, float A) { api.ClearColor(R, G, B, A); } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe void IglFunc.ClearGraphicError() { while (api.GetError() != 0) { } } uint IglFunc.CompileShader(string shaderCode, ShaderType shaderType) { 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); } return shaderID; } [MethodImpl(MethodImplOptions.AggressiveInlining)] uint IglFunc.CreateProgram() { return api.CreateProgram(); } 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.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; } } unsafe uint IglFunc.CreatGraphicProgram() { return api.CreateProgram(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe void IglFunc.DeleteBuffers(int count, uint* ptr) { api.DeleteBuffers(count, ptr); } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe void IglFunc.DeleteVertexArrays(int count, uint* ptr) { api.DeleteVertexArrays(count, ptr); } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe void IglFunc.DrawElements(GepmetryType mode, int count, Type type, IntPtr ptr) { uint a; switch (type.Name) { case "UInt32": a = GLconst.UNSIGNED_INT; break; case "UInt16": a = GLconst.UNSIGNED_SHORT; break; case "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.DrawElements((uint)mode, count, a, (void*)ptr); } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe void IglFunc.GenBuffers(int count, uint* ptr) { api.GenBuffers(count, ptr); } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe void IglFunc.GenTextures(int count, uint* ptr) { api.GenTextures(count, ptr); } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe void IglFunc.GenVertexArrays(int count, uint* ptr) { api.GenVertexArrays(count, ptr); } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe void IglFunc.GetGraphicError() { string error = null; uint code = api.GetError(); while (code != 0) { error += $"OpenGL error {code}\n"; } try { if (error != null) { throw new Exception(); } } catch (Exception ex) { /* InternalIO.InternalWriteLog( error + ex.StackTrace, LogType.Error ); /**/ } return; } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe int IglFunc.GetUniformLocation(uint program, byte* ptr) { return api.GetUniformLocation(program, ptr); } unsafe bool IglFunc.GetWindowShouldClose(Form form) { return InternalIO.glfw.WindowShouldClose(form.windowID) == 0; } unsafe void IglFunc.MakeContexCurrent(Form form) { if (form.windowID == IntPtr.Zero) { return; } InternalIO.glfw.MakeContextCurrent(form.windowID); } [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe void IglFunc.SetBufferData(BufferType bufferType, T bufferData, BufferUsage usage) { int size = sizeof(T); api.BufferData( (uint)bufferType, 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)); fixed (void* bufferPtr = &bufferArray[0]) api.BufferData( (uint)bufferType, size, bufferPtr, (uint)usage); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void IglFunc.SetFrameBufferSizeCallback(IntPtr id, FrameBufferSizeHandler handler) { IntPtr handlerPtr = Marshal.GetFunctionPointerForDelegate(handler); InternalIO.glfw.SetFramebufferSizeCallback(id, handlerPtr); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void IglFunc.SetVertexAttribute(uint index, int size, Type t, bool normalized, int stride, int pointer) { uint type = InternalIO.GLtype[t]; api.VertexAttribPointer(index, size, type, normalized, stride, (void*)pointer); api.EnableVertexAttribArray(index); } unsafe void IglFunc.SwapBuffers(Form form) { if (form.windowID == IntPtr.Zero) { return; } InternalIO.glfw.SwapBuffers(form.windowID); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void IglFunc.TextureImage2d(uint target, int level, uint internalformat, int width, int height, uint format, uint type, void* pixels) { api.TexImage2D( target, level, internalformat, width, height, 0, format, type, pixels ); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void IglFunc.TextureParameter(uint target, uint pname, int param) { api.TexParameteri(target, pname, param); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void IglFunc.UseProgram(uint program) { api.UseProgram(program); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void IglFunc.Viewport(int x, int y, int width, int height) { api.Viewport(x, y, width, height); } } }