GLUtil_GL.cs 11.8 KB
Newer Older
pcy's avatar
initial  
pcy committed
1
2
3
using System;
using System.Collections.Generic;
using System.Linq;
pcy's avatar
pcy committed
4
using System.Runtime.InteropServices;
pcy's avatar
pcy committed
5
using System.Text;
pcy's avatar
initial  
pcy committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

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() {
pcy's avatar
pcy committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
			_GetError      = GetProcAddress<GetError_T>("GetError");
			_CreateProgram = GetProcAddress<CreateProgram_T>("CreateProgram");
			_CreateShader  = GetProcAddress<CreateShader_T>("CreateShader");
			_ShaderSource  = GetProcAddress<ShaderSource_T>("ShaderSource");
			_CompileShader = GetProcAddress<CompileShader_T>("CompileShader");
			_GetShaderiv   = GetProcAddress<GetShaderiv_T>("GetShaderiv");
			_GetShaderInfoLog = GetProcAddress<GetShaderInfoLog_T>("GetShaderInfoLog");
			_AttachShader  = GetProcAddress<AttachShader_T>("AttachShader");
			_LinkProgram   = GetProcAddress<LinkProgram_T>("LinkProgram");
			_GetProgramiv  = GetProcAddress<GetProgramiv_T>("GetProgramiv");
			_GetProgramInfoLog = GetProcAddress<GetProgramInfoLog_T>("GetProgramInfoLog");
			_DeleteShader  = GetProcAddress<DeleteShader_T>("DeleteShader");
			_DeleteProgram = GetProcAddress<DeleteProgram_T>("DeleteProgram");
			_UseProgram    = GetProcAddress<UseProgram_T>("UseProgram");
			_GetIntegerv   = GetProcAddress<GetIntegerv_T>("GetIntegerv");
pcy's avatar
pcy committed
42
43
44
45
46
47
48
			_GetActiveAttrib    = GetProcAddress<GetActiveAttrib_T>("GetActiveAttrib");
			_GetAttribLocation  = GetProcAddress<GetAttribLocation_T>("GetAttribLocation");
			_GetUniformIndices  = GetProcAddress<GetUniformIndices_T>("GetUniformIndices");
			_GetUniformLocation = GetProcAddress<GetUniformLocation_T>("GetUniformLocation");
			_GetActiveUniformName = GetProcAddress<GetActiveUniformName_T>("GetActiveUniformName");
			_GetActiveUniformsiv  = GetProcAddress<GetActiveUniformsiv_T>("GetActiveUniformsiv");
			_GetActiveUniform     = GetProcAddress<GetActiveUniform_T>("GetActiveUniform");
pcy's avatar
initial  
pcy committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
		}
		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;
64
65
			_UseProgram    = null;
			_GetIntegerv   = null;
pcy's avatar
pcy committed
66
67
68
69
70
71
72
			_GetActiveAttrib    = null;
			_GetAttribLocation  = null;
			_GetUniformIndices  = null;
			_GetUniformLocation = null;
			_GetActiveUniformName = null;
			_GetActiveUniformsiv  = null;
			_GetActiveUniform     = null;
pcy's avatar
initial  
pcy committed
73
74
		}

pcy's avatar
pcy committed
75
76
		delegate GLenum GetError_T();
		static GetError_T _GetError;
pcy's avatar
initial  
pcy committed
77
78
		public static GLError GetError() => (GLError)_GetError();

pcy's avatar
pcy committed
79
80
		delegate GLuint CreateProgram_T();
		static CreateProgram_T _CreateProgram;
pcy's avatar
initial  
pcy committed
81
82
		public static GLuint CreateProgram() => _CreateProgram();

pcy's avatar
pcy committed
83
84
		delegate GLuint CreateShader_T(GLenum shtyp);
		static CreateShader_T _CreateShader;
pcy's avatar
initial  
pcy committed
85
86
87
		public static GLuint CreateShader(GLSLPurpose shtyp)
			=> _CreateShader((GLenum)shtyp);

pcy's avatar
pcy committed
88
89
90
		delegate void ShaderSource_T(GLuint sh, GLsizei count,
				GLchar** str, GLint* len);
		static ShaderSource_T _ShaderSource;
pcy's avatar
pcy committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
		public static void ShaderSource(GLuint sh, string[] srcs) {
			int count = srcs.Length;

			// marshal the string array...
			byte[][] strs = new byte[count][];
			GLint[] lens = new GLint[count];

			// convert all the strings to utf8
			int totallen = 0;
			for (int i = 0; i < count; ++i) {
				strs[i] = GetNullTermUTF8(srcs[i]);
				totallen += (lens[i] = strs[i].Length);
			}

			// rearrange pointer crap so that GL can use it
			byte[] bytesbytes = new byte[totallen];
			GLchar*[] ptrs = new GLchar*[count];
			fixed (byte* bbp = bytesbytes) {
				int totalidx = 0;
				for (int i = 0; i < count; ++i) {
					Array.Copy(strs[i], 0, bytesbytes, totalidx, lens[i]);
					ptrs[i] = (GLchar*)&bbp[totalidx];
					totalidx += lens[i];
				}

				// now we can actually call the function
				fixed (GLchar** strp = ptrs) {
					fixed (GLint* lenp = lens) {
						_ShaderSource(sh, count, strp, lenp);
					}
				}
			}
		}
		readonly static string[] strarr = new string[1];
		public static void ShaderSource(GLuint sh, string src) {
			strarr[0] = src;
			ShaderSource(sh, strarr);
pcy's avatar
initial  
pcy committed
128
129
		}

pcy's avatar
pcy committed
130
131
		delegate void CompileShader_T(GLuint sh);
		static CompileShader_T _CompileShader;
pcy's avatar
initial  
pcy committed
132
133
		public static void CompileShader(GLuint sh) { _CompileShader(sh); }

pcy's avatar
pcy committed
134
135
		delegate void GetShaderiv_T(GLuint sh, GLenum pname, GLint* parm);
		static GetShaderiv_T _GetShaderiv;
pcy's avatar
initial  
pcy committed
136
137
138
		public static GLint GetShaderiv(GLuint sh, GLenum pname) {
			GLint* parm = stackalloc GLint[1];
			*parm = 0;
pcy's avatar
pcy committed
139
			_GetShaderiv(sh, pname, parm);
pcy's avatar
initial  
pcy committed
140
141
142
			return *parm;
		}

pcy's avatar
pcy committed
143
144
145
		delegate void GetShaderInfoLog_T(GLuint sh, GLsizei maxl, GLsizei* len,
				GLchar* infolog);
		static GetShaderInfoLog_T _GetShaderInfoLog;
pcy's avatar
pcy committed
146
147
148
149
150
		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);
pcy's avatar
pcy committed
151
				return StrFromNullTermUTF8(logp, len);
pcy's avatar
pcy committed
152
			}
pcy's avatar
initial  
pcy committed
153
154
		}

pcy's avatar
pcy committed
155
156
		delegate void AttachShader_T(GLuint pr, GLuint sh);
		static AttachShader_T _AttachShader;
pcy's avatar
initial  
pcy committed
157
158
159
160
		public static void AttachShader(GLuint prgm, GLuint sh) {
			_AttachShader(prgm, sh);
		}

pcy's avatar
pcy committed
161
162
		delegate void LinkProgram_T(GLuint pr);
		static LinkProgram_T _LinkProgram;
pcy's avatar
initial  
pcy committed
163
164
		public static void LinkProgram(GLuint prgm) { _LinkProgram(prgm); }

pcy's avatar
pcy committed
165
166
		delegate void GetProgramiv_T(GLuint pr, GLuint pnam, GLint* parm);
		static GetProgramiv_T _GetProgramiv;
pcy's avatar
initial  
pcy committed
167
168
169
170
		// 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;
pcy's avatar
pcy committed
171
			_GetProgramiv(prgm, pname, parm);
pcy's avatar
initial  
pcy committed
172
173
174
			return parm[0];
		}

pcy's avatar
pcy committed
175
176
177
		delegate void GetProgramInfoLog_T(GLuint pr, GLsizei maxl,
				GLsizei* len, GLchar* infolog);
		static GetProgramInfoLog_T _GetProgramInfoLog;
pcy's avatar
pcy committed
178
179
180
181
182
		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);
pcy's avatar
pcy committed
183
				return StrFromNullTermUTF8(logp, len);
pcy's avatar
pcy committed
184
			}
pcy's avatar
initial  
pcy committed
185
186
		}

pcy's avatar
pcy committed
187
188
		delegate void DeleteShader_T(GLuint sh);
		static DeleteShader_T _DeleteShader;
pcy's avatar
initial  
pcy committed
189
190
		public static void DeleteShader(GLuint sh) { _DeleteShader(sh); }

pcy's avatar
pcy committed
191
192
		delegate void DeleteProgram_T(GLuint pr);
		static DeleteProgram_T _DeleteProgram;
pcy's avatar
initial  
pcy committed
193
		public static void DeleteProgram(GLuint prgm) { _DeleteProgram(prgm); }
194

pcy's avatar
pcy committed
195
196
		delegate void UseProgram_T(GLuint pr);
		static UseProgram_T _UseProgram;
197
198
		public static void UseProgram(GLuint prgm) { _UseProgram(prgm); }

pcy's avatar
pcy committed
199
200
		delegate void GetIntegerv_T(GLenum n, GLint* parm);
		static GetIntegerv_T _GetIntegerv;
201
		public static GLint GetIntegerv(GLenum name) {
pcy's avatar
pcy committed
202
			GLint* parm = stackalloc GLint[4]; // let's hope 4 is good enough
203
			parm[3] = parm[2] = parm[1] = parm[0] = 0;
pcy's avatar
pcy committed
204
			_GetIntegerv(name, parm);
205
206
			return parm[0];
		}
pcy's avatar
pcy committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

		// 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);
			}
pcy's avatar
pcy committed
227
			name = StrFromNullTermUTF8(namebuf, namelen);
pcy's avatar
pcy committed
228
229
230
231
232
233
			type = (GLSLType)_typ;
		}

		delegate GLint GetAttribLocation_T(GLuint prgm, GLchar* name);
		static GetAttribLocation_T _GetAttribLocation;
		public static GLint GetAttribLocation(GLuint prgm, string name) {
pcy's avatar
pcy committed
234
			byte[] blargh = GetNullTermUTF8(name);
pcy's avatar
pcy committed
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251

			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...

pcy's avatar
pcy committed
252
			// convert all the strings to utf8
pcy's avatar
pcy committed
253
254
255
			int totallen = 0;
			byte[][] bytes = new byte[names.Length][];
			for (int i = 0; i < names.Length; ++i) {
pcy's avatar
pcy committed
256
				bytes[i] = GetNullTermUTF8(names[i], true);
pcy's avatar
pcy committed
257
258
				totallen += bytes[i].Length;
			}
pcy's avatar
pcy committed
259
			// rearrange pointer crap so that glGetUniformIndices can read it
pcy's avatar
pcy committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
			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);
			}
pcy's avatar
pcy committed
323
			name = StrFromNullTermUTF8(namebuf, namelen);
pcy's avatar
pcy committed
324
325
326
327
328
329
330
331
			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");

pcy's avatar
pcy committed
332
			byte[] bytes = GetNullTermUTF8(name);
pcy's avatar
pcy committed
333
334
335
336
337
338
			fixed (byte* bp = bytes) {
				return _GetUniformLocation(prgm, (GLchar*)bp);
			}
		}

		// TODO: glUniform functions
pcy's avatar
initial  
pcy committed
339
340
341
	}
}