-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgles_util.c
728 lines (658 loc) · 19.1 KB
/
gles_util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
51
52
53
54
55
56
57
58
59
60
61
62
63
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
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
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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/**
* ====================================================================
* gles_util.h
*
* Copyright (c) 2006 Nokia Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "glesmodule.h"
static GLES_Arrays *arrayData;
int gles_PySequence_Dimension(PyObject *seq) {
assert(PySequence_Check(seq));
PyObject *item = PySequence_GetItem(seq, 0);
if( !PySequence_Check(item) ) {
return -1;
}
// The dimension is determined from the lenght of the first element
int dimension = PySequence_Length(item);
return dimension;
}
/**
* Convert a freely structured sequence into a flat sequence
* Also converts data to the given type.
*
* @param type Can be one of: GL_FLOAT, GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_FIXED.
* @param source A Python sequence where the data is read from.
* @param target A pointer to an existing target sequence. If NULL, a new sequence is created.
* @return A collapsed sequence.
*/
PyObject *gles_PySequence_Collapse(GLenum type, PyObject *source, PyObject *target)
{
int count;
int i;
PyObject *k;
// We need a sequence to operate.
if(!PySequence_Check(source)) {
PyErr_SetString(PyExc_TypeError, "Expecting a sequence");
return NULL;
}
// Make a new list if not supplied by caller.
if(target == NULL) {
target = PyList_New(0);
if(!target) {
assert(PyErr_Occurred() != NULL);
return NULL;
}
}
count = PySequence_Length(source);
for(i=0; i < count; i++) {
k = PySequence_GetItem(source, i);
// See if its a sequence. Strings are sequences too, but we should ignore them here.
if(PySequence_Check(k) && !PyString_Check(k)) {
// Call a new function go get the contents of the sequence.
if(gles_PySequence_Collapse(type, k, target) == NULL) {
// There should be an exception
assert(PyErr_Occurred() != NULL);
return NULL;
}
} else {
assert(k != NULL);
if(!PyNumber_Check(k)) {
PyErr_SetString(PyExc_TypeError, "Expecting a number");
return NULL;
}
// Try to convert the object into desired type.
// Using the number interface we can handle all
// types that can be converted into a number.
PyObject *t=NULL;
switch(type) {
case GL_FLOAT:
k = PyNumber_Float(k);
if(k==NULL) {
// Something went wrong with the conversion.
assert(PyErr_Occurred() != NULL);
return NULL;
}
t = Py_BuildValue("f", PyFloat_AsDouble(k));
break;
case GL_SHORT:
k = PyNumber_Int(k);
if(k==NULL) {
assert(PyErr_Occurred() != NULL);
return NULL;
}
t = Py_BuildValue("h", PyInt_AsLong(k));
break;
case GL_UNSIGNED_SHORT:
k = PyNumber_Int(k);
if(k==NULL) {
assert(PyErr_Occurred() != NULL);
return NULL;
}
t = Py_BuildValue("H", PyInt_AsLong(k));
break;
case GL_BYTE:
k = PyNumber_Int(k);
if(k==NULL) {
assert(PyErr_Occurred() != NULL);
return NULL;
}
t = Py_BuildValue("b", PyInt_AsLong(k));
break;
case GL_UNSIGNED_BYTE:
k = PyNumber_Int(k);
if(k==NULL) {
assert(PyErr_Occurred() != NULL);
return NULL;
}
t = Py_BuildValue("B", PyInt_AsLong(k));
break;
case GL_FIXED:
k = PyNumber_Int(k);
if(k==NULL) {
assert(PyErr_Occurred() != NULL);
return NULL;
}
t = Py_BuildValue("i", PyInt_AsLong(k));
break;
default:
PyErr_SetString(PyExc_TypeError, "Invalid type for conversion");
return NULL;
}
if(t == NULL) {
assert(PyErr_Occurred() != NULL);
return NULL;
}
// Add item to target.
PyList_Append(target, t);
}
}
return target;
}
/**
* Place given array into internal memory structure
*
* If there already is something in memory, it will be freed,
* if the memory does not belong to an array object
*
* @param targetarr Specifies where the given array should be placed in internal memory. Can be one of: GL_VERTEX_ARRAY, GL_NORMAL_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY.
* @param *ptr Pointer to an array.
* @param object Python object of type gles.array. Can be NULL, in which case the memory will be freed when the next array is placed in the same location.
* @return The given pointer
*/
void *gles_assign_array(GLenum targetarr, void *ptr, array_object *object) {
//GLES_Arrays *arrays = (GLES_Arrays*)Dll::Tls();
gles_array_t *arrptr;
assert(arrayData!=NULL);
// Get the array from internal struct.
switch(targetarr) {
case GL_VERTEX_ARRAY:
arrptr = &(arrayData->vertex);
#ifdef DEBUG_GLES
DEBUGMSG("Assigning GL_VERTEX_ARRAY\n");
#endif
break;
case GL_NORMAL_ARRAY:
arrptr = &(arrayData->normal);
#ifdef DEBUG_GLES
DEBUGMSG("Assigning GL_NORMAL_ARRAY\n");
#endif
break;
case GL_COLOR_ARRAY:
arrptr = &(arrayData->color);
#ifdef DEBUG_GLES
DEBUGMSG("Assigning GL_COLOR_ARRAY\n");
#endif
break;
case GL_TEXTURE_COORD_ARRAY:
arrptr = &(arrayData->texcoord);
#ifdef DEBUG_GLES
DEBUGMSG("Assigning GL_TEXTURE_COORD_ARRAY\n");
#endif
break;
#ifdef GL_OES_VERSION_1_1
case GL_MATRIX_INDEX_ARRAY_OES:
arrptr = &(arrayData->matrix);
#ifdef DEBUG_GLES
DEBUGMSG("Assigning GL_MATRIX_INDEX_ARRAY_OES\n");
#endif
break;
case GL_POINT_SIZE_ARRAY_OES:
arrptr = &(arrayData->pointsize);
#ifdef DEBUG_GLES
DEBUGMSG("Assigning GL_POINT_SIZE_ARRAY_OES\n");
#endif
break;
case GL_WEIGHT_ARRAY_OES:
arrptr = &(arrayData->weight);
#ifdef DEBUG_GLES
DEBUGMSG("Assigning GL_WEIGHT_ARRAY_OES\n");
#endif
break;
#endif
default:
return NULL;
}
// Set the previous array free
gles_free_array(targetarr);
// See if we got a new object
if(object != NULL) {
arrptr->obj = (PyObject*)object;
Py_XINCREF(object);
arrptr->ptr = object->arrdata;
} // Otherwise just take the pointer into custody
else {
arrptr->obj = NULL;
arrptr->ptr = ptr;
}
return arrptr->ptr;
}
/**
* Convert a Python sequence to a float array
*
* @param seq A Python sequence object
* @return A pointer to a C array of GLfloat values
*/
GLfloat *gles_PySequence_AsGLfloatArray(PyObject *seq) {
int count;
GLfloat *dataptr;
int i;
PyObject *item;
if(! PySequence_Check(seq)) {
PyErr_SetString(PyExc_TypeError, "Invalid type");
return NULL;
}
count = PySequence_Length(seq);
dataptr = (GLfloat*)gles_alloc( sizeof(GLfloat)*count);
if(dataptr == NULL) {
return (GLfloat*)PyErr_NoMemory();
}
for( i = 0; i < count; i++ ) {
item = PySequence_GetItem(seq, i);
if( !PyFloat_Check(item) ) {
PyErr_SetString(PyExc_TypeError, "Expecting a float");
return NULL;
}
dataptr[i] = (GLfloat)PyFloat_AsDouble(item);
}
return dataptr;
}
/**
* Convert a Python sequence into unsigned short array
*
* @param seq A Python sequence object
* @return A pointer to a C array of GLushort values
*/
GLushort *gles_PySequence_AsGLushortArray(PyObject *seq) {
int count;
GLushort *dataptr;
int i;
PyObject *item;
if(! PySequence_Check(seq)) {
PyErr_SetString(PyExc_TypeError, "Expecting a sequence");
return NULL;
}
count = PySequence_Length(seq);
dataptr = (GLushort*)gles_alloc( sizeof(GLushort)*count);
if(dataptr == NULL) {
return (GLushort*)PyErr_NoMemory();
}
for( i = 0; i < count; i++ ) {
item = PySequence_GetItem(seq, i);
if( !PyInt_Check(item) ) {
gles_free(dataptr);
PyErr_SetString(PyExc_TypeError, "Expecting an integer");
return NULL;
}
dataptr[i] = (GLushort)PyInt_AsLong(item);
}
return dataptr;
}
/**
* Convert a Python sequence into short array
*
* @param seq A Python sequence object
* @return A pointer to a C array of GLshort values
*/
GLshort *gles_PySequence_AsGLshortArray(PyObject *seq) {
int count;
GLshort *dataptr;
int i;
PyObject *item;
if(! PySequence_Check(seq)) {
PyErr_SetString(PyExc_TypeError, "Expecting a sequence");
return NULL;
}
count = PySequence_Length(seq);
dataptr = (GLshort*)gles_alloc( sizeof(GLshort)*count);
if(dataptr == NULL) {
return (GLshort*)PyErr_NoMemory();
}
for( i = 0; i < count; i++ ) {
item = PySequence_GetItem(seq, i);
if( !PyInt_Check(item) ) {
gles_free(dataptr);
PyErr_SetString(PyExc_TypeError, "Expecting an integer");
return NULL;
}
dataptr[i] = (GLshort)PyInt_AsLong(item);
}
return dataptr;
}
/**
* Convert a Python sequence into a byte array
*
* @param seq A Python sequence object
* @return A pointer to a C array of GLbyte values
*/
GLbyte *gles_PySequence_AsGLbyteArray(PyObject *seq) {
int count;
GLbyte *dataptr;
int i;
PyObject *item;
if(! PySequence_Check(seq)) {
PyErr_SetString(PyExc_TypeError, "Invalid type");
return NULL;
}
count = PySequence_Length(seq);
dataptr = (GLbyte*)gles_alloc( sizeof(GLbyte)*count);
if(dataptr == NULL) {
return (GLbyte*)PyErr_NoMemory();
}
for( i = 0; i < count; i++ ) {
item = PySequence_GetItem(seq, i);
if( !PyInt_Check(item) ) {
// Free the indices.
gles_free(dataptr);
PyErr_SetString(PyExc_TypeError, "Expecting an integer");
return NULL;
}
dataptr[i] = (GLbyte)PyInt_AsLong(item);
}
return dataptr;
}
/**
* Converts a Python sequence into unsigned byte array
*
* @param seq A Python sequence object
* @return A pointer to a C array of GLubyte values
*/
GLubyte *gles_PySequence_AsGLubyteArray(PyObject *seq) {
int count;
GLubyte *dataptr;
int i;
PyObject *item;
if(! PySequence_Check(seq)) {
PyErr_SetString(PyExc_TypeError, "Invalid type");
return NULL;
}
count = PySequence_Length(seq);
dataptr = (GLubyte*)gles_alloc( sizeof(GLubyte)*count);
if(dataptr == NULL) {
return (GLubyte*)PyErr_NoMemory();
}
for( i = 0; i < count; i++ ) {
item = PySequence_GetItem(seq, i);
if( !PyInt_Check(item) ) {
// Free the indices
gles_free(dataptr);
PyErr_SetString(PyExc_TypeError, "Expecting an integer");
return NULL;
}
dataptr[i] = (GLubyte)PyInt_AsLong(item);
}
return dataptr;
}
/**
* Converts a Python sequence into int array
*
* @param seq A Python sequence object
* @return A pointer to a C array of GLint values
*/
GLint *gles_PySequence_AsGLintArray(PyObject *seq) {
int count;
GLint *dataptr;
int i;
PyObject *item;
if(! PySequence_Check(seq)) {
PyErr_SetString(PyExc_TypeError, "Invalid type");
return NULL;
}
count = PySequence_Length(seq);
dataptr = (GLint*)gles_alloc( sizeof(GLint)*count);
if(dataptr == NULL) {
return (GLint*)PyErr_NoMemory();
}
for( i = 0; i < count; i++ ) {
item = PySequence_GetItem(seq, i);
if( !PyInt_Check(item) ) {
// Free the indices
gles_free(dataptr);
PyErr_SetString(PyExc_TypeError, "Expecting an integer");
return NULL;
}
dataptr[i] = (GLint)PyInt_AsLong(item);
}
return dataptr;
}
/**
* Converts a Python sequence into unsigned int array
*
* @param seq A Python sequence object
* @return A pointer to a C array of GLuint values
*/
GLuint *gles_PySequence_AsGLuintArray(PyObject *seq) {
int count;
GLuint *dataptr;
int i;
PyObject *item;
if(! PySequence_Check(seq)) {
PyErr_SetString(PyExc_TypeError, "Invalid type");
return NULL;
}
count = PySequence_Length(seq);
dataptr = (GLuint*)gles_alloc( sizeof(GLuint)*count);
if(dataptr == NULL) {
return (GLuint*)PyErr_NoMemory();
}
for( i = 0; i < count; i++ ) {
item = PySequence_GetItem(seq, i);
if( !PyInt_Check(item) ) {
// Free the indices
gles_free(dataptr);
PyErr_SetString(PyExc_TypeError, "Expecting an integer");
return NULL;
}
dataptr[i] = (GLuint)PyInt_AsLong(item);
}
return dataptr;
}
/**
* Converts a Python sequence into fixed array
*
* @param seq A Python sequence object
* @return A pointer to a C array of GLfixed values
*/
GLfixed *gles_PySequence_AsGLfixedArray(PyObject *seq) {
int count;
GLfixed *dataptr;
int i;
PyObject *item;
if(! PySequence_Check(seq)) {
PyErr_SetString(PyExc_TypeError, "Invalid type");
return NULL;
}
count = PySequence_Length(seq);
dataptr = (GLfixed*)gles_alloc( sizeof(GLfixed)*count);
if(dataptr == NULL) {
return (GLfixed*)PyErr_NoMemory();
}
for( i = 0; i < count; i++ ) {
item = PySequence_GetItem(seq, i);
if( !PyInt_Check(item) ) {
// Free the indices
gles_free(dataptr);
PyErr_SetString(PyExc_TypeError, "Expecting an integer");
return NULL;
}
dataptr[i] = (GLfixed)PyInt_AsLong(item);
}
return dataptr;
}
/**
* Frees all the internally used array memory
*
*/
void gles_uninit_arrays() {
//GLES_Arrays *arrayData;
//arrayData = (GLES_Arrays*)Dll::Tls();
if(arrayData != NULL) {
gles_free_array(GL_VERTEX_ARRAY);
gles_free_array(GL_NORMAL_ARRAY);
gles_free_array(GL_COLOR_ARRAY);
gles_free_array(GL_TEXTURE_COORD_ARRAY);
#ifdef GL_OES_VERSION_1_1
gles_free_array(GL_MATRIX_INDEX_ARRAY_OES);
gles_free_array(GL_POINT_SIZE_ARRAY_OES);
gles_free_array(GL_WEIGHT_ARRAY_OES);
#endif
gles_free(arrayData);
}
}
/**
* Initializes internal arrays used for glXXXPointer data
*
*/
void gles_init_arrays() {
arrayData = (GLES_Arrays*)gles_alloc( sizeof(GLES_Arrays) );
if(arrayData == NULL) {
PyErr_NoMemory();
return;
}
// Reset array pointers
arrayData->vertex.ptr = NULL;
arrayData->vertex.obj = NULL;
arrayData->color.ptr = NULL;
arrayData->color.obj = NULL;
arrayData->normal.ptr = NULL;
arrayData->normal.obj = NULL;
arrayData->texcoord.ptr = NULL;
arrayData->texcoord.obj = NULL;
#ifdef GL_OES_VERSION_1_1
arrayData->matrix.ptr = NULL;
arrayData->matrix.obj = NULL;
arrayData->pointsize.ptr = NULL;
arrayData->pointsize.obj = NULL;
arrayData->weight.ptr = NULL;
arrayData->weight.obj = NULL;
#endif
}
/**
* Frees a specified array from internal memory
*
* @param arrtype Which array to free. Can be one of: GL_VERTEX_ARRAY, GL_NORMAL_ARRAY, GL_COLOR_ARRAY or GL_TEXTURE_COORD_ARRAY.
*/
void gles_free_array(GLenum arrtype) {
gles_array_t *arr=NULL;
switch(arrtype) {
case GL_VERTEX_ARRAY:
arr = &(arrayData->vertex);
#ifdef DEBUG_GLES
DEBUGMSG("gles_free_array() (VERTEX)");
#endif
break;
case GL_NORMAL_ARRAY:
arr = &(arrayData->normal);
#ifdef DEBUG_GLES
DEBUGMSG("gles_free_array() (NORMAL)");
#endif
break;
case GL_COLOR_ARRAY:
arr = &(arrayData->color);
#ifdef DEBUG_GLES
DEBUGMSG("gles_free_array() (COLOR)");
#endif
break;
case GL_TEXTURE_COORD_ARRAY:
arr = &(arrayData->texcoord);
#ifdef DEBUG_GLES
DEBUGMSG("gles_free_array() (TEXTURE_COORD)");
#endif
break;
#ifdef GL_OES_VERSION_1_1
case GL_MATRIX_INDEX_ARRAY_OES:
arr = &(arrayData->matrix);
#ifdef DEBUG_GLES
DEBUGMSG("gles_free_array() (GL_MATRIX_INDEX_ARRAY_OES)");
#endif
break;
case GL_POINT_SIZE_ARRAY_OES:
arr = &(arrayData->pointsize);
#ifdef DEBUG_GLES
DEBUGMSG("gles_free_array() (GL_POINT_SIZE_ARRAY_OES)");
#endif
break;
case GL_WEIGHT_ARRAY_OES:
arr = &(arrayData->weight);
#ifdef DEBUG_GLES
DEBUGMSG("gles_free_array() (GL_WEIGHT_ARRAY_OES)");
#endif
break;
#endif
default:
return;
}
if(arr->obj != NULL) {
Py_XDECREF(arr->obj);
} else if(arr->ptr != NULL) {
gles_free(arr->ptr);
}
arr->ptr=NULL;
}
/**
* Checks if an OpenGL error has occurred, and sets an exception if necessary.
*
* @return 1 if an OpenGL error has occurred, 0 otherwise
*/
unsigned int gles_check_error() {
GLenum error = GL_NO_ERROR;
error = glGetError();
if(error != GL_NO_ERROR) {
PyObject *exc;
exc = PyErr_NewException("gles.GLerror", NULL, NULL);
switch(error) {
case GL_INVALID_ENUM:
PyErr_Format(exc, "Invalid enum [Errno %x]", error);
#ifdef DEBUG_GLES
DEBUGMSG1("gles_check_error(): Invalid enum [Errno %x]", error);
#endif
break;
case GL_INVALID_VALUE:
PyErr_Format(exc, "Invalid value [Errno %x]", error);
#ifdef DEBUG_GLES
DEBUGMSG1("gles_check_error(): Invalid value [Errno %x]", error);
#endif
break;
case GL_INVALID_OPERATION:
PyErr_Format(exc, "Invalid operation [Errno %x]", error);
#ifdef DEBUG_GLES
DEBUGMSG1("gles_check_error(): Invalid operation [Errno %x]", error);
#endif
break;
case GL_STACK_OVERFLOW:
PyErr_Format(exc, "Stack overflow [Errno %x]", error);
#ifdef DEBUG_GLES
DEBUGMSG1("gles_check_error(): Stack overflow [Errno %x]", error);
#endif
break;
case GL_STACK_UNDERFLOW:
PyErr_Format(exc, "Stack underflow [Errno %x]", error);
#ifdef DEBUG_GLES
DEBUGMSG1("gles_check_error(): Stack underflow [Errno %x]", error);
#endif
break;
case GL_OUT_OF_MEMORY:
PyErr_NoMemory();
#ifdef DEBUG_GLES
DEBUGMSG("gles_check_error(): Out of memory");
#endif
break;
default:
PyErr_Format(PyExc_Exception, "Unknown error: %x", error );
#ifdef DEBUG_GLES
DEBUGMSG1("gles_check_error(): Unknown error: %x", error);
#endif
break;
}
return 1;
}
return 0;
}
/**
* Helper function for allocating memory
*
* @param size Size of desired memory block
* @return A pointer to a newly allocated block of memory
*/
void *gles_alloc(size_t size) {
return malloc(size);
}
/**
* Helper function for freeing allocated memory
*
* @param ptr A pointer to a block of memory to be freed
*/
void gles_free(void *ptr) {
free(ptr);
}