using System; using System.Collections.Generic; using System.Linq; using System.Text; using GLenum = System.UInt32; using GLboolean = System.Byte ; using GLbitfield = System.UInt32; using GLvoid = System.Void ; using GLbyte = System.SByte ; using GLshort = System.Int16 ; using GLint = System.Int32 ; using GLubyte = System.Byte ; using GLushort = System.UInt16; using GLuint = System.UInt32; using GLsizei = System.Int32 ; using GLfloat = System.Single; using GLclampf = System.Single; using GLdouble = System.Double; using GLclampd = System.Double; using GLchar = System.SByte ; namespace PoroCYon.FNAGLSL { public static unsafe partial class GL { static void InitProcs() { _GetError = GetProcAddress("GetError"); _CreateProgram = GetProcAddress("CreateProgram"); _CreateShader = GetProcAddress("CreateShader"); _ShaderSource = GetProcAddress("ShaderSource"); _CompileShader = GetProcAddress("CompileShader"); _GetShaderiv = GetProcAddress("GetShaderiv"); _GetShaderInfoLog = GetProcAddress("GetShaderInfoLog"); _AttachShader = GetProcAddress("AttachShader"); _LinkProgram = GetProcAddress("LinkProgram"); _GetProgramiv = GetProcAddress("GetProgramiv"); _GetProgramInfoLog = GetProcAddress("GetProgramInfoLog"); _DeleteShader = GetProcAddress("DeleteShader"); _DeleteProgram = GetProcAddress("DeleteProgram"); _UseProgram = GetProcAddress("UseProgram"); _GetIntegerv = GetProcAddress("GetIntegerv"); _GetActiveAttrib = GetProcAddress("GetActiveAttrib"); _GetAttribLocation = GetProcAddress("GetAttribLocation"); _GetUniformIndices = GetProcAddress("GetUniformIndices"); _GetUniformLocation = GetProcAddress("GetUniformLocation"); _GetActiveUniformName = GetProcAddress("GetActiveUniformName"); _GetActiveUniformsiv = GetProcAddress("GetActiveUniformsiv"); _GetActiveUniform = GetProcAddress("GetActiveUniform"); } static void DeinitProcs() { _GetError = null; _CreateProgram = null; _CreateShader = null; _ShaderSource = null; _CompileShader = null; _GetShaderiv = null; _GetShaderInfoLog = null; _AttachShader = null; _LinkProgram = null; _GetProgramiv = null; _GetProgramInfoLog = null; _DeleteShader = null; _DeleteProgram = null; _UseProgram = null; _GetIntegerv = null; _GetActiveAttrib = null; _GetAttribLocation = null; _GetUniformIndices = null; _GetUniformLocation = null; _GetActiveUniformName = null; _GetActiveUniformsiv = null; _GetActiveUniform = null; } delegate GLenum GetError_T(); static GetError_T _GetError; public static GLError GetError() => (GLError)_GetError(); delegate GLuint CreateProgram_T(); static CreateProgram_T _CreateProgram; public static GLuint CreateProgram() => _CreateProgram(); delegate GLuint CreateShader_T(GLenum shtyp); static CreateShader_T _CreateShader; public static GLuint CreateShader(GLSLPurpose shtyp) => _CreateShader((GLenum)shtyp); delegate void ShaderSource_T(GLuint sh, GLsizei count, GLchar** str, GLint* len); static ShaderSource_T _ShaderSource; public static void ShaderSource(GLuint sh, GLsizei count, GLchar** str, GLint* len) { _ShaderSource(sh, count, str, len); } delegate void CompileShader_T(GLuint sh); static CompileShader_T _CompileShader; public static void CompileShader(GLuint sh) { _CompileShader(sh); } delegate void GetShaderiv_T(GLuint sh, GLenum pname, GLint* parm); static GetShaderiv_T _GetShaderiv; public static GLint GetShaderiv(GLuint sh, GLenum pname) { GLint* parm = stackalloc GLint[1]; *parm = 0; _GetShaderiv(sh, pname, parm); return *parm; } delegate void GetShaderInfoLog_T(GLuint sh, GLsizei maxl, GLsizei* len, GLchar* infolog); static GetShaderInfoLog_T _GetShaderInfoLog; public static string GetShaderInfoLog(GLuint sh, GLsizei maxl) { GLchar[] logbuf = new GLchar[maxl]; GLsizei len = 0; fixed (GLchar* logp = logbuf) { _GetShaderInfoLog(sh, maxl, &len, logp); return new String(logp, 0, len, Encoding.UTF8); } } delegate void AttachShader_T(GLuint pr, GLuint sh); static AttachShader_T _AttachShader; public static void AttachShader(GLuint prgm, GLuint sh) { _AttachShader(prgm, sh); } delegate void LinkProgram_T(GLuint pr); static LinkProgram_T _LinkProgram; public static void LinkProgram(GLuint prgm) { _LinkProgram(prgm); } delegate void GetProgramiv_T(GLuint pr, GLuint pnam, GLint* parm); static GetProgramiv_T _GetProgramiv; // FIXME: won't work with GL_COMPUTE_WORK_GROUP_SIZE public static GLint GetProgramiv(GLuint prgm, GLenum pname) { GLint* parm = stackalloc GLint[3]; parm[2] = parm[1] = parm[0] = 0; _GetProgramiv(prgm, pname, parm); return parm[0]; } delegate void GetProgramInfoLog_T(GLuint pr, GLsizei maxl, GLsizei* len, GLchar* infolog); static GetProgramInfoLog_T _GetProgramInfoLog; public static string GetProgramInfoLog(GLuint prgm, GLsizei maxl) { GLchar[] logbuf = new GLchar[maxl]; GLsizei len = 0; fixed (GLchar* logp = logbuf) { _GetProgramInfoLog(prgm, maxl, &len, logp); return new String(logp, 0, len, Encoding.UTF8); } } delegate void DeleteShader_T(GLuint sh); static DeleteShader_T _DeleteShader; public static void DeleteShader(GLuint sh) { _DeleteShader(sh); } delegate void DeleteProgram_T(GLuint pr); static DeleteProgram_T _DeleteProgram; public static void DeleteProgram(GLuint prgm) { _DeleteProgram(prgm); } delegate void UseProgram_T(GLuint pr); static UseProgram_T _UseProgram; public static void UseProgram(GLuint prgm) { _UseProgram(prgm); } delegate void GetIntegerv_T(GLenum n, GLint* parm); static GetIntegerv_T _GetIntegerv; public static GLint GetIntegerv(GLenum name) { GLint* parm = stackalloc GLint[4]; // let's hope 4 is good enough parm[3] = parm[2] = parm[1] = parm[0] = 0; _GetIntegerv(name, parm); return parm[0]; } // TODO: const int NAMEBUF_MAX_LEN = 64; delegate void GetActiveAttrib_T(GLuint prgm, GLuint idx, GLsizei bufSize, GLsizei* len, GLint* size, GLenum* type, GLchar* name); static GetActiveAttrib_T _GetActiveAttrib; public static void GetActiveAttrib(GLuint prgm, GLuint index, out GLint size, out GLSLType type, out string name) { GLchar* namebuf = stackalloc GLchar[NAMEBUF_MAX_LEN]; GLsizei namelen = 0; size = 0; GLenum _typ = 0; fixed (GLint* psize = &size) { _GetActiveAttrib(prgm, index, (GLsizei)(NAMEBUF_MAX_LEN*sizeof(GLchar)), &namelen, psize, &_typ, namebuf); } name = new String(namebuf, 0, namelen, Encoding.UTF8); type = (GLSLType)_typ; } delegate GLint GetAttribLocation_T(GLuint prgm, GLchar* name); static GetAttribLocation_T _GetAttribLocation; public static GLint GetAttribLocation(GLuint prgm, string name) { byte[] blargh = Encoding.UTF8.GetBytes(name); fixed (byte* bb = blargh) { return _GetAttribLocation(prgm, (GLchar*)bb); } } delegate void GetUniformIndices_T(GLuint prgm, GLsizei unifCnt, GLchar** unifNames, GLuint* unifIdxs); static GetUniformIndices_T _GetUniformIndices; public static void GetUniformIndices(GLuint prgm, string[] names, GLuint[] idxs) { if (names == null) throw new ArgumentNullException("names"); if (idxs == null) throw new ArgumentNullException("idxs" ); if (names.Length != idxs.Length) throw new ArgumentException("#names != #idxs"); // marshal the string array... // convert all the string to utf8 int totallen = 0; byte[][] bytes = new byte[names.Length][]; for (int i = 0; i < names.Length; ++i) { bytes[i] = Encoding.UTF8.GetBytes(names[i]); totallen += bytes[i].Length; } // rearrange pointer crap to that glGetUniformIndices can read it byte[] bytesbytes = new byte[totallen]; GLchar*[] ptrs = new GLchar*[names.Length]; fixed (byte* bbp = bytesbytes) { int totalidx = 0; for (int i = 0; i < names.Length; ++i) { Array.Copy(bytes[i], 0, bytesbytes, totalidx, bytes[i].Length); ptrs[i] = (GLchar*)&bbp[totalidx]; totalidx += bytes[i].Length; } // now we can actually call the function fixed (GLchar** strp = ptrs) { fixed (GLuint* idxp = idxs) { _GetUniformIndices(prgm, names.Length, strp, idxp); } } } } delegate void GetActiveUniformName_T(GLuint prgm, GLuint unifIdx, GLsizei bufSize, GLsizei* length, GLchar* unifName); static GetActiveUniformName_T _GetActiveUniformName; public static string GetActiveUniformName(GLuint prgm, GLuint unifIdx) { GLchar* namebuf = stackalloc GLchar[NAMEBUF_MAX_LEN]; GLsizei namelen = 0; _GetActiveUniformName(prgm, unifIdx, (GLsizei)(NAMEBUF_MAX_LEN*sizeof(GLchar)), &namelen, namebuf); return new String(namebuf, 0, namelen, Encoding.UTF8); } delegate void GetActiveUniformsiv_T(GLuint prgm, GLsizei unifCnt, GLuint* unifIdxs, GLenum pname, GLint* parms); static GetActiveUniformsiv_T _GetActiveUniformsiv; public static void GetActiveUniformsiv(GLuint prgm, GLuint[] unifIdxs, GLenum pname, GLint[] parms) { if (unifIdxs == null) throw new ArgumentNullException("unifIdxs"); if (parms == null) throw new ArgumentNullException("parms" ); if (unifIdxs.Length != parms.Length) throw new ArgumentException("#unifIdxs != #parms"); fixed (GLuint* idxs = unifIdxs) { fixed (GLint* ps = parms) { _GetActiveUniformsiv(prgm, parms.Length, idxs, pname, ps); } } } delegate void GetActiveUniform_T(GLuint prgm, GLuint idx, GLsizei bufSize, GLsizei* length, GLint* size, GLenum* type, GLchar* name); static GetActiveUniform_T _GetActiveUniform; public static void GetActiveUniform(GLuint prgm, GLuint idx, out GLint size, out GLSLType type, out string name) { GLchar* namebuf = stackalloc GLchar[NAMEBUF_MAX_LEN]; GLsizei namelen = 0; size = 0; GLenum _typ = 0; fixed (GLint* psize = &size) { _GetActiveUniform(prgm, idx, (GLsizei)(NAMEBUF_MAX_LEN*sizeof(GLchar)), &namelen, psize, &_typ, namebuf); } name = new String(namebuf, 0, namelen, Encoding.UTF8); type = (GLSLType)_typ; } delegate GLint GetUniformLocation_T(GLuint prgm, GLchar* name); static GetUniformLocation_T _GetUniformLocation; public static GLint GetUniformLocation(GLuint prgm, string name) { if (name == null) throw new ArgumentNullException("name"); byte[] bytes = Encoding.UTF8.GetBytes(name); fixed (byte* bp = bytes) { return _GetUniformLocation(prgm, (GLchar*)bp); } } // TODO: glUniform functions } }