GLSLEffect_TypTbl.cs 13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using static PoroCYon.FNAGLSL.GLSLType;
using static Microsoft.Xna.Framework.Graphics.EffectParameterClass;
using static Microsoft.Xna.Framework.Graphics.EffectParameterType ;

using C = Microsoft.Xna.Framework.Graphics.EffectParameterClass;
using T = Microsoft.Xna.Framework.Graphics.EffectParameterType ;

namespace PoroCYon.FNAGLSL {
	public partial class GLSLEffect : Effect {
		readonly static Dictionary<GLSLType, int                 > typ2size ;
		readonly static Dictionary<GLSLType, EffectParameterClass> typ2class;
		readonly static Dictionary<GLSLType, EffectParameterType > typ2type ;
pcy's avatar
pcy committed
20
		readonly static Dictionary<GLSLType, uint> typ2getn, typ2setn;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

		static GLSLEffect() {
			typ2size = new Dictionary<GLSLType, int>() {
				{ FLOAT, sizeof(float) },
				{ FLOAT_VEC2, sizeof(float)*2 },
				{ FLOAT_VEC3, sizeof(float)*3 },
				{ FLOAT_VEC4, sizeof(float)*4 },
				{ INT, sizeof(int) },
				{ INT_VEC2, sizeof(int)*2 },
				{ INT_VEC3, sizeof(int)*3 },
				{ INT_VEC4, sizeof(int)*4 },
				{ UNSIGNED_INT, sizeof(uint) },
				{ UNSIGNED_INT_VEC2, sizeof(uint)*2 },
				{ UNSIGNED_INT_VEC3, sizeof(uint)*3 },
				{ UNSIGNED_INT_VEC4, sizeof(uint)*4 },
				{ BOOL, sizeof(uint)/*!!!*/ },
				{ BOOL_VEC2, sizeof(uint)*2 },
				{ BOOL_VEC3, sizeof(uint)*3 },
				{ BOOL_VEC4, sizeof(uint)*4 },
				// NOTE: need to be converted when sending these thru the
				//       glUniform fns
				{ FLOAT_MAT2, sizeof(float)*4*4 },
				{ FLOAT_MAT3, sizeof(float)*4*4 },
				{ FLOAT_MAT4, sizeof(float)*4*4 },
				{ FLOAT_MAT2x3, sizeof(float)*4*4 },
				{ FLOAT_MAT2x4, sizeof(float)*4*4 },
				{ FLOAT_MAT3x2, sizeof(float)*4*4 },
				{ FLOAT_MAT3x4, sizeof(float)*4*4 },
				{ FLOAT_MAT4x2, sizeof(float)*4*4 },
				{ FLOAT_MAT4x3, sizeof(float)*4*4 },
pcy's avatar
pcy committed
51
52
53
54
55
56
57
58
59
60
61
62
63
				// int-sized bc. not used, but data ptr needs to be nonzero anyway
				{ SAMPLER_1D, sizeof(int) },
				{ SAMPLER_2D, sizeof(int) },
				{ SAMPLER_3D, sizeof(int) },
				{ SAMPLER_CUBE, sizeof(int) },
				{ INT_SAMPLER_1D, sizeof(int) },
				{ INT_SAMPLER_2D, sizeof(int) },
				{ INT_SAMPLER_3D, sizeof(int) },
				{ INT_SAMPLER_CUBE, sizeof(int) },
				{ UNSIGNED_INT_SAMPLER_1D, sizeof(int) },
				{ UNSIGNED_INT_SAMPLER_2D, sizeof(int) },
				{ UNSIGNED_INT_SAMPLER_3D, sizeof(int) },
				{ UNSIGNED_INT_SAMPLER_CUBE, sizeof(int) },
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
			};
			typ2class = new Dictionary<GLSLType, EffectParameterClass>() {
				{ FLOAT, Scalar },
				{ FLOAT_VEC2, Vector },
				{ FLOAT_VEC3, Vector },
				{ FLOAT_VEC4, Vector },
				{ INT, Scalar },
				{ INT_VEC2, Vector },
				{ INT_VEC3, Vector },
				{ INT_VEC4, Vector },
				{ UNSIGNED_INT, Scalar },
				{ UNSIGNED_INT_VEC2, Vector },
				{ UNSIGNED_INT_VEC3, Vector },
				{ UNSIGNED_INT_VEC4, Vector },
				{ BOOL, Scalar },
				{ BOOL_VEC2, Vector },
				{ BOOL_VEC3, Vector },
				{ BOOL_VEC4, Vector },
				{ FLOAT_MAT2, C.Matrix },
				{ FLOAT_MAT3, C.Matrix },
				{ FLOAT_MAT4, C.Matrix },
				{ FLOAT_MAT2x3, C.Matrix },
				{ FLOAT_MAT2x4, C.Matrix },
				{ FLOAT_MAT3x2, C.Matrix },
				{ FLOAT_MAT3x4, C.Matrix },
				{ FLOAT_MAT4x2, C.Matrix },
				{ FLOAT_MAT4x3, C.Matrix },
				{ SAMPLER_1D, C.Object },
				{ SAMPLER_2D, C.Object },
				{ SAMPLER_3D, C.Object },
				{ SAMPLER_CUBE, C.Object },
				{ INT_SAMPLER_1D, C.Object },
				{ INT_SAMPLER_2D, C.Object },
				{ INT_SAMPLER_3D, C.Object },
				{ INT_SAMPLER_CUBE, C.Object },
				{ UNSIGNED_INT_SAMPLER_1D, C.Object },
				{ UNSIGNED_INT_SAMPLER_2D, C.Object },
				{ UNSIGNED_INT_SAMPLER_3D, C.Object },
				{ UNSIGNED_INT_SAMPLER_CUBE, C.Object },
			};
			typ2type = new Dictionary<GLSLType, EffectParameterType>() {
				{ FLOAT, T.Single },
				{ FLOAT_VEC2, T.Single },
				{ FLOAT_VEC3, T.Single },
				{ FLOAT_VEC4, T.Single },
				{ INT, T.Int32 },
				{ INT_VEC2, T.Int32 },
				{ INT_VEC3, T.Int32 },
				{ INT_VEC4, T.Int32 },
				{ UNSIGNED_INT, T.Int32 },
				{ UNSIGNED_INT_VEC2, T.Int32 },
				{ UNSIGNED_INT_VEC3, T.Int32 },
				{ UNSIGNED_INT_VEC4, T.Int32 },
				{ BOOL, T.Bool },
				{ BOOL_VEC2, T.Bool },
				{ BOOL_VEC3, T.Bool },
				{ BOOL_VEC4, T.Bool },
				{ FLOAT_MAT2, T.Single },
				{ FLOAT_MAT3, T.Single },
				{ FLOAT_MAT4, T.Single },
				{ FLOAT_MAT2x3, T.Single },
				{ FLOAT_MAT2x4, T.Single },
				{ FLOAT_MAT3x2, T.Single },
				{ FLOAT_MAT3x4, T.Single },
				{ FLOAT_MAT4x2, T.Single },
				{ FLOAT_MAT4x3, T.Single },
				{ SAMPLER_1D, T.Texture1D },
				{ SAMPLER_2D, T.Texture2D },
				{ SAMPLER_3D, T.Texture3D },
				{ SAMPLER_CUBE, T.TextureCube },
				{ INT_SAMPLER_1D, T.Texture1D },
				{ INT_SAMPLER_2D, T.Texture2D },
				{ INT_SAMPLER_3D, T.Texture3D },
				{ INT_SAMPLER_CUBE, T.TextureCube },
				{ UNSIGNED_INT_SAMPLER_1D, T.Texture1D },
				{ UNSIGNED_INT_SAMPLER_2D, T.Texture2D },
				{ UNSIGNED_INT_SAMPLER_3D, T.Texture3D },
				{ UNSIGNED_INT_SAMPLER_CUBE, T.TextureCube },
			};
pcy's avatar
pcy committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

			typ2getn = new Dictionary<GLSLType, uint>() {
				{ SAMPLER_1D, GL.TEXTURE_BINDING_1D },
				{ SAMPLER_2D, GL.TEXTURE_BINDING_2D },
				{ SAMPLER_3D, GL.TEXTURE_BINDING_3D },
				{ SAMPLER_CUBE, GL.TEXTURE_BINDING_CUBE },
				{ INT_SAMPLER_1D, GL.TEXTURE_BINDING_1D },
				{ INT_SAMPLER_2D, GL.TEXTURE_BINDING_2D },
				{ INT_SAMPLER_3D, GL.TEXTURE_BINDING_3D },
				{ INT_SAMPLER_CUBE, GL.TEXTURE_BINDING_CUBE },
				{ UNSIGNED_INT_SAMPLER_1D, GL.TEXTURE_BINDING_1D },
				{ UNSIGNED_INT_SAMPLER_2D, GL.TEXTURE_BINDING_2D },
				{ UNSIGNED_INT_SAMPLER_3D, GL.TEXTURE_BINDING_3D },
				{ UNSIGNED_INT_SAMPLER_CUBE, GL.TEXTURE_BINDING_CUBE },
				/*GL_TEXTURE_BINDING_1D,
				GL_TEXTURE_BINDING_1D_ARRAY,
				GL_TEXTURE_BINDING_2D,
				GL_TEXTURE_BINDING_2D_ARRAY,
				GL_TEXTURE_BINDING_2D_MULTISAMPLE,
				GL_TEXTURE_BINDING_2D_MULTISAMPLE_ARRAY,
				GL_TEXTURE_BINDING_3D,
				GL_TEXTURE_BINDING_BUFFER,
				GL_TEXTURE_BINDING_CUBE_MAP,
				GL_TEXTURE_BINDING_RECTANGLE*/
			};
			typ2setn = new Dictionary<GLSLType, uint>() {
				{ SAMPLER_1D, GL.TEXTURE_1D },
				{ SAMPLER_2D, GL.TEXTURE_2D },
				{ SAMPLER_3D, GL.TEXTURE_3D },
				{ SAMPLER_CUBE, GL.TEXTURE_CUBE_MAP },
				{ INT_SAMPLER_1D, GL.TEXTURE_1D },
				{ INT_SAMPLER_2D, GL.TEXTURE_2D },
				{ INT_SAMPLER_3D, GL.TEXTURE_3D },
				{ INT_SAMPLER_CUBE, GL.TEXTURE_CUBE_MAP },
				{ UNSIGNED_INT_SAMPLER_1D, GL.TEXTURE_1D },
				{ UNSIGNED_INT_SAMPLER_2D, GL.TEXTURE_2D },
				{ UNSIGNED_INT_SAMPLER_3D, GL.TEXTURE_3D },
				{ UNSIGNED_INT_SAMPLER_CUBE, GL.TEXTURE_CUBE_MAP },
				/* GL_TEXTURE_1D,
				 * GL_TEXTURE_2D,
				 * GL_TEXTURE_3D,
				 * GL_TEXTURE_1D_ARRAY,
				 * GL_TEXTURE_2D_ARRAY,
				 * GL_TEXTURE_RECTANGLE,
				 * GL_TEXTURE_CUBE_MAP,
				 * GL_TEXTURE_CUBE_MAP_ARRAY,
				 * GL_TEXTURE_BUFFER,
				 * GL_TEXTURE_2D_MULTISAMPLE
				 * GL_TEXTURE_2D_MULTISAMPLE_ARRAY.*/
			};
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
		}

		static int GetRows(GLSLType t) {
			switch (t) {
				case FLOAT_MAT2: case FLOAT_MAT3x2: case FLOAT_MAT4x2:
					return 2;
				case FLOAT_MAT3: case FLOAT_MAT2x3: case FLOAT_MAT4x3:
					return 3;
				case FLOAT_MAT4: case FLOAT_MAT2x4: case FLOAT_MAT3x4:
					return 4;
			}

			return 0;
		}
		static int GetCols(GLSLType t) {
			switch (t) {
				case FLOAT_MAT2: case FLOAT_MAT2x3: case FLOAT_MAT2x4:
					return 2;
				case FLOAT_MAT3: case FLOAT_MAT3x2: case FLOAT_MAT3x4:
					return 3;
				case FLOAT_MAT4: case FLOAT_MAT4x2: case FLOAT_MAT4x3:
					return 4;
			}

			return 0;
		}
pcy's avatar
pcy committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404

		unsafe static void SetUniform(ref UnifData u, IntPtr data) {
			int loc = u.location;

			void* d = (void*)data;
			float* fd = (float*)d;
			int* id = (int*)d;
			uint* ud = (uint*)d;
			Matrix* m = (Matrix*)d;

			switch (u.type) {
			case FLOAT:
				/*if (u.size == 1) GL.Uniform1f(loc, *fd);
				else*/ GL.Uniform1fv(loc, u.size, fd);
				break;
			case FLOAT_VEC2:
				/*if (u.size == 1) GL.Uniform2f(loc, fd[0], fd[1]);
				else*/ GL.Uniform2fv(loc, u.size, fd);
				break;
			case FLOAT_VEC3:
				/*if (u.size == 1) GL.Uniform3f(loc, fd[0], fd[1], fd[2]);
				else*/ GL.Uniform3fv(loc, u.size, fd);
				break;
			case FLOAT_VEC4:
				/*if (u.size == 1) GL.Uniform4f(loc, fd[0], fd[1], fd[2], fd[3]);
				else*/ GL.Uniform4fv(loc, u.size, fd);
				break;
			case INT: case BOOL:
				GL.Uniform1iv(loc, u.size, id);
				break;
			case INT_VEC2: case BOOL_VEC2:
				GL.Uniform2iv(loc, u.size, id);
				break;
			case INT_VEC3: case BOOL_VEC3:
				GL.Uniform3iv(loc, u.size, id);
				break;
			case INT_VEC4: case BOOL_VEC4:
				GL.Uniform4iv(loc, u.size, id);
				break;
			case UNSIGNED_INT:
				GL.Uniform1uiv(loc, u.size, ud);
				break;
			case UNSIGNED_INT_VEC2:
				GL.Uniform2uiv(loc, u.size, ud);
				break;
			case UNSIGNED_INT_VEC3:
				GL.Uniform3uiv(loc, u.size, ud);
				break;
			case UNSIGNED_INT_VEC4:
				GL.Uniform4uiv(loc, u.size, ud);
				break;
			// NOTE: matrices layout:
			// Matrix.Mxy: row x, column y
			// FLOAT_MATXxY: X cols, Y rows => X row-vals, Y col-vals
			// transpose=false: column-major (column per column)
			// transpose=true : row-major (row per row)
			// FIXME: are these actually passed in the correct order? better check.
			case FLOAT_MAT2: {
					if (u.size != 1) throw new NotSupportedException("Matrix array");
					float* m22 = stackalloc float[2*2];
					m22[0*2+0] = m->M11;
					m22[0*2+1] = m->M12;
					m22[1*2+0] = m->M21;
					m22[1*2+1] = m->M22;
					GL.UniformMatrix2fv(loc, 1, GL.FALSE, m22);
				} break;
			case FLOAT_MAT3: {
					if (u.size != 1) throw new NotSupportedException("Matrix array");
					float* m33 = stackalloc float[3*3];
					m33[0*3+0] = m->M11;
					m33[0*3+1] = m->M12;
					m33[0*3+2] = m->M13;
					m33[1*3+0] = m->M21;
					m33[1*3+1] = m->M22;
					m33[1*3+2] = m->M23;
					m33[2*3+0] = m->M31;
					m33[2*3+1] = m->M32;
					m33[2*3+2] = m->M33;
					GL.UniformMatrix3fv(loc, 1, GL.FALSE, m33);
				} break;
			case FLOAT_MAT4: {
					if (u.size != 1) throw new NotSupportedException("Matrix array");
					float* m44 = stackalloc float[4*4];
					m44[0*4+0] = m->M11;
					m44[0*4+1] = m->M12;
					m44[0*4+2] = m->M13;
					m44[0*4+3] = m->M14;
					m44[1*4+0] = m->M21;
					m44[1*4+1] = m->M22;
					m44[1*4+2] = m->M23;
					m44[1*4+3] = m->M24;
					m44[2*4+0] = m->M31;
					m44[2*4+1] = m->M32;
					m44[2*4+2] = m->M33;
					m44[2*4+3] = m->M34;
					m44[3*4+0] = m->M41;
					m44[3*4+1] = m->M42;
					m44[3*4+2] = m->M43;
					m44[3*4+3] = m->M44;
					GL.UniformMatrix4fv(loc, 1, GL.FALSE, m44);
				} break;
			case FLOAT_MAT2x3: { // 2 cols, 3 rows -> 2 rowvals, 3 colvals
					if (u.size != 1) throw new NotSupportedException("Matrix array");
					float* m23 = stackalloc float[2*3];
					m23[0*3+0] = m->M11;
					m23[0*3+1] = m->M12;
					m23[0*3+2] = m->M13;
					m23[1*3+0] = m->M21;
					m23[1*3+1] = m->M22;
					m23[1*3+2] = m->M23;
					GL.UniformMatrix2x3fv(loc, 1, GL.FALSE, m23);
				} break;
			case FLOAT_MAT2x4: { // 2 cols, 4 rows -> 2 rowvals, 3 colvals
					if (u.size != 1) throw new NotSupportedException("Matrix array");
					float* m24 = stackalloc float[2*4];
					m24[0*4+0] = m->M11;
					m24[0*4+1] = m->M12;
					m24[0*4+2] = m->M13;
					m24[0*4+3] = m->M14;
					m24[1*4+0] = m->M21;
					m24[1*4+1] = m->M22;
					m24[1*4+2] = m->M23;
					m24[1*4+3] = m->M24;
					GL.UniformMatrix2x4fv(loc, 1, GL.FALSE, m24);
				} break;
			case FLOAT_MAT3x2: { // 3 cols, 2 rows -> 2 rowvals, 3 colvals
					if (u.size != 1) throw new NotSupportedException("Matrix array");
					float* m32 = stackalloc float[3*2];
					m32[0*2+0] = m->M11;
					m32[0*2+1] = m->M12;
					m32[1*2+0] = m->M21;
					m32[1*2+1] = m->M22;
					m32[2*2+0] = m->M31;
					m32[2*2+1] = m->M32;
					GL.UniformMatrix3x2fv(loc, 1, GL.FALSE, m32);
				} break;
			case FLOAT_MAT3x4: { // 3 cols, 4 rows -> 3 rowvals, 4 colvals
					if (u.size != 1) throw new NotSupportedException("Matrix array");
					float* m34 = stackalloc float[3*4];
					m34[0*4+0] = m->M11;
					m34[0*4+1] = m->M12;
					m34[0*4+2] = m->M13;
					m34[0*4+3] = m->M14;
					m34[1*4+0] = m->M21;
					m34[1*4+1] = m->M22;
					m34[1*4+2] = m->M23;
					m34[1*4+3] = m->M24;
					m34[2*4+0] = m->M31;
					m34[2*4+1] = m->M32;
					m34[2*4+2] = m->M33;
					m34[2*4+3] = m->M34;
					GL.UniformMatrix3x4fv(loc, 1, GL.FALSE, m34);
				} break;
			case FLOAT_MAT4x2: { // 4 cols, 2 rows -> 4 rowvals, 2 colvals
					if (u.size != 1) throw new NotSupportedException("Matrix array");
					float* m42 = stackalloc float[4*2];
					m42[0*2+0] = m->M11;
					m42[0*2+1] = m->M12;
					m42[1*2+0] = m->M21;
					m42[1*2+1] = m->M22;
					m42[2*2+0] = m->M31;
					m42[2*2+1] = m->M32;
					m42[3*2+0] = m->M41;
					m42[3*2+1] = m->M42;
					GL.UniformMatrix4x2fv(loc, 1, GL.FALSE, m42);
				} break;
			case FLOAT_MAT4x3: { // 4 cols, 3 rows -> 4 rowvals, 3 colvals
					if (u.size != 1) throw new NotSupportedException("Matrix array");
					float* m43 = stackalloc float[4*3];
					m43[0*3+0] = m->M11;
					m43[0*3+1] = m->M12;
					m43[0*3+2] = m->M13;
					m43[1*3+0] = m->M21;
					m43[1*3+1] = m->M22;
					m43[1*3+2] = m->M23;
					m43[2*3+0] = m->M31;
					m43[2*3+1] = m->M32;
					m43[2*3+2] = m->M33;
					m43[3*3+0] = m->M41;
					m43[3*3+1] = m->M42;
					m43[3*3+2] = m->M43;
					GL.UniformMatrix4x3fv(loc, 1, GL.FALSE, m43);
				} break;
			default:
				throw new NotSupportedException(u.type+"");
			}
pcy's avatar
pcy committed
405
			GL.Throw();
pcy's avatar
pcy committed
406
		}
407
408
409
	}
}