1 //
2 // Copyright (c) 2017 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #include "helpers.h"
17
18 #include "gl_headers.h"
19 #include "CL/cl_half.h"
20
21 #define CHECK_ERROR()\
22 {GLint __error = glGetError(); if(__error) {log_error( "GL ERROR: %s!\n", gluErrorString( err ));}}
23
24 #if defined(__linux__) || defined(GL_ES_VERSION_2_0)
25 // On linux we don't link to GLU library to avoid compatibility issues with
26 // libstdc++
27 // FIXME: Implement this
gluErrorString(GLenum error)28 const GLubyte* gluErrorString (GLenum error)
29 {
30 const char* gl_Error = "OpenGL Error";
31 return (const GLubyte*)gl_Error;
32 }
33 #endif
34
35 static void DrawQuad(void);
36
CreateGLTexture2D(size_t width,size_t height,GLenum target,GLenum glFormat,GLenum internalFormat,GLenum glType,ExplicitType type,GLuint * outTextureID,int * outError,bool allocateMem,MTdata d)37 void * CreateGLTexture2D( size_t width, size_t height,
38 GLenum target, GLenum glFormat,
39 GLenum internalFormat, GLenum glType,
40 ExplicitType type, GLuint *outTextureID,
41 int *outError, bool allocateMem, MTdata d )
42 {
43 *outError = 0;
44 GLenum err = 0;
45
46 char * buffer = (char *)CreateRandomData(type, width * height * 4, d);
47
48 glGenTextures( 1, outTextureID );
49 glBindTexture( get_base_gl_target( target ), *outTextureID );
50 err = glGetError();
51 if( err != GL_NO_ERROR )
52 {
53 log_error( "ERROR: Failed to create GL texture object: %s!\n", gluErrorString( err ));
54 *outError = -1;
55 free( buffer );
56 return NULL;
57 }
58
59 #ifndef GL_ES_VERSION_2_0
60 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
61 #endif
62 glTexParameteri( get_base_gl_target( target ), GL_TEXTURE_MIN_FILTER, GL_NEAREST );
63 glTexParameteri( get_base_gl_target( target ), GL_TEXTURE_MAG_FILTER, GL_NEAREST );
64
65 if( get_base_gl_target( target ) == GL_TEXTURE_CUBE_MAP )
66 {
67 char * temp = (char *)malloc(width * height * 4 * get_explicit_type_size( type ) * sizeof(cl_char));
68 if(allocateMem)
69 memcpy( temp, buffer, width * height * 4 * get_explicit_type_size( type ) );
70 else
71 memset( temp, 0, width * height * 4 * get_explicit_type_size( type ) );
72
73 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, internalFormat, (GLsizei)width, (GLsizei)height, 0, glFormat, glType, temp );
74 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, internalFormat, (GLsizei)width, (GLsizei)height, 0, glFormat, glType, temp );
75 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, internalFormat, (GLsizei)width, (GLsizei)height, 0, glFormat, glType, temp );
76 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, internalFormat, (GLsizei)width, (GLsizei)height, 0, glFormat, glType, temp );
77 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, internalFormat, (GLsizei)width, (GLsizei)height, 0, glFormat, glType, temp );
78 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, internalFormat, (GLsizei)width, (GLsizei)height, 0, glFormat, glType, temp );
79 free(temp);
80 }
81 else
82 {
83 #ifdef GLES_DEBUG
84 log_info("- glTexImage2D : %s : %s : %d : %d : %s : %s\n",
85 GetGLTargetName(target),
86 GetGLFormatName(internalFormat),
87 width, height,
88 GetGLFormatName(glFormat),
89 GetGLTypeName(glType));
90
91 DumpGLBuffer(glType, width, height, buffer);
92
93 #endif
94 glTexImage2D( get_base_gl_target(target), 0, internalFormat, (GLsizei)width, (GLsizei)height, 0, glFormat, glType, buffer );
95 }
96
97 err = glGetError();
98 if( err != GL_NO_ERROR )
99 {
100 /** In section 9.8.3.1. of the CL 1.1. spec it says that:
101 *
102 * If a GL texture object with an internal format from table 9.4 is successfully created by
103 * OpenGL, then there is guaranteed to be a mapping to one of the corresponding CL image
104 * format(s) in that table.
105 *
106 * Notice that some of the formats in table 9.4 are not supported in OpenGL ES 2.0.
107 */
108 log_info( "Warning: Skipping %s : %s : %d : %d : %s : %s : because glTexImage2D returned %s\n",
109 GetGLTargetName(target),
110 GetGLFormatName(internalFormat),
111 (int)(width), (int)(height),
112 GetGLFormatName(glFormat),
113 GetGLTypeName(glType),
114 gluErrorString( err ));
115
116 glDeleteTextures( 1, outTextureID );
117 *outTextureID = 0;
118 *outError = 0;
119 free( buffer );
120 err = glGetError();
121 return NULL;
122 }
123
124 #ifdef GLES_DEBUG
125 memset(buffer, 0, width * height * 4 * get_explicit_type_size( type ));
126
127 log_info("- glGetTexImage : %s : %s : %s\n",
128 GetGLTargetName(target),
129 GetGLFormatName(glFormat),
130 GetGLTypeName(glType));
131
132 glGetTexImage(target, 0, glFormat, glType, buffer);
133
134 DumpGLBuffer(type, width, height, buffer);
135
136 err = glGetError();
137 if( err != GL_NO_ERROR )
138 {
139 log_error( "ERROR: Unable to read data from glGetTexImage : %s : %s : %s : Error %s\n",
140 GetGLTargetName(target),
141 GetGLFormatName(glFormat),
142 GetGLTypeName(glType),
143 gluErrorString( err ));
144 return NULL;
145 }
146 #endif
147
148 if( !allocateMem )
149 {
150 free( buffer );
151 return NULL;
152 }
153
154 #ifndef GL_ES_VERSION_2_0
155 if( glType == GL_UNSIGNED_INT_8_8_8_8_REV && glFormat == GL_BGRA && allocateMem )
156 {
157 // Reverse and reorder to validate since in the
158 // kernel the read_imagef() call always returns RGBA
159 cl_uchar *p = (cl_uchar *)buffer;
160 for( size_t i = 0; i < width * height; i++ )
161 {
162 cl_uchar uc0 = p[i * 4 + 0];
163 cl_uchar uc1 = p[i * 4 + 1];
164 cl_uchar uc2 = p[i * 4 + 2];
165 cl_uchar uc3 = p[i * 4 + 3];
166
167 p[ i * 4 + 0 ] = uc2;
168 p[ i * 4 + 1 ] = uc1;
169 p[ i * 4 + 2 ] = uc0;
170 p[ i * 4 + 3 ] = uc3;
171 }
172 }
173 #endif
174
175 return buffer;
176 }
177
CreateGLTexture3D(size_t width,size_t height,size_t depth,GLenum target,GLenum glFormat,GLenum internalFormat,GLenum glType,ExplicitType type,GLuint * outTextureID,int * outError,MTdata d,bool allocateMem)178 void * CreateGLTexture3D( size_t width, size_t height, size_t depth,
179 GLenum target, GLenum glFormat,
180 GLenum internalFormat, GLenum glType,
181 ExplicitType type, GLuint *outTextureID,
182 int *outError, MTdata d, bool allocateMem)
183 {
184 *outError = 0;
185
186 char * buffer = (char *)create_random_data( type, d, width * height * depth * 4 );
187
188 if( type == kFloat && allocateMem )
189 {
190 // Re-fill the created buffer to just have [0-1] floats, since that's what it'd expect
191 cl_float *p = (cl_float *)buffer;
192 for( size_t i = 0; i < width * height * depth * 4; i++ )
193 {
194 p[ i ] = (float) genrand_real1( d );
195 }
196 }
197 else if( !allocateMem )
198 memset( buffer, 0, width * height * depth * 4 * get_explicit_type_size( type ) );
199
200 glGenTextures( 1, outTextureID );
201
202 glBindTexture( target, *outTextureID );
203 #ifndef GL_ES_VERSION_2_0
204 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
205 #endif
206 glTexParameteri( target, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
207 glTexParameteri( target, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
208
209 glGetError();
210 glTexImage3D( target, 0, internalFormat, (GLsizei)width, (GLsizei)height, (GLsizei)depth, 0, glFormat, glType, buffer );
211 GLenum err = glGetError();
212 if( err != GL_NO_ERROR )
213 {
214 /** In section 9.8.3.1. of the CL 1.1. spec it says that:
215 *
216 * If a GL texture object with an internal format from table 9.4 is successfully created by
217 * OpenGL, then there is guaranteed to be a mapping to one of the corresponding CL image
218 * format(s) in that table.
219 *
220 * Notice that some of the formats in table 9.4 are not supported in OpenGL ES 2.0.
221 */
222 log_info( "Warning: Skipping %s : %s : %d : %d : %s : %s : because glTexImage3D returned %s\n",
223 GetGLTargetName(target),
224 GetGLFormatName(internalFormat),
225 (int)(width), (int)(height),
226 GetGLFormatName(glFormat),
227 GetGLTypeName(glType),
228 gluErrorString( err ));
229
230 *outError = 0;
231 delete[] buffer;
232 return NULL;
233 }
234
235 if( !allocateMem )
236 {
237 delete [] buffer;
238 return NULL;
239 }
240
241 #ifndef GL_ES_VERSION_2_0
242 if( glType == GL_UNSIGNED_INT_8_8_8_8_REV && glFormat == GL_BGRA && allocateMem )
243 {
244 // Reverse and reorder to validate since in the
245 // kernel the read_imagef() call always returns RGBA
246
247 cl_uchar *p = (cl_uchar *)buffer;
248 for( size_t i = 0; i < width * height * depth; i++ )
249 {
250 cl_uchar uc0 = p[i * 4 + 0];
251 cl_uchar uc1 = p[i * 4 + 1];
252 cl_uchar uc2 = p[i * 4 + 2];
253 cl_uchar uc3 = p[i * 4 + 3];
254
255 p[ i * 4 + 0 ] = uc2;
256 p[ i * 4 + 1 ] = uc1;
257 p[ i * 4 + 2 ] = uc0;
258 p[ i * 4 + 3 ] = uc3;
259 }
260 }
261 #endif
262
263 return buffer;
264 }
265
ReadGLTexture(GLenum glTarget,GLuint glTexture,GLenum glFormat,GLenum glInternalFormat,GLenum glType,ExplicitType typeToReadAs,size_t outWidth,size_t outHeight)266 void * ReadGLTexture( GLenum glTarget, GLuint glTexture,
267 GLenum glFormat, GLenum glInternalFormat,
268 GLenum glType, ExplicitType typeToReadAs,
269 size_t outWidth, size_t outHeight )
270 {
271 // Read results from the GL texture
272 glBindTexture(get_base_gl_target(glTarget), glTexture);
273
274 GLenum readBackFormat = GL_RGBA;
275 GLenum readBackType = glType;
276 glFramebufferWrapper glFramebuffer;
277 glRenderbufferWrapper glRenderbuffer;
278 size_t outBytes = outWidth * outHeight * 4 * GetGLTypeSize(readBackType);
279 cl_char *outBuffer = (cl_char *)malloc( outBytes );
280 GLenum err = 0;
281
282 memset(outBuffer, 0, outBytes);
283 glGenFramebuffersEXT( 1, &glFramebuffer );
284 glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, glFramebuffer );
285 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, glTarget, glTexture, 0);
286 err = glGetError();
287 if (err != GL_NO_ERROR)
288 {
289 log_error("Failed to attach texture to FBO!\n");
290 return NULL;
291 }
292
293 glReadPixels( 0, 0, (GLsizei)outWidth, (GLsizei)outHeight, readBackFormat, readBackType, outBuffer );
294
295 #ifdef GLES_DEBUG
296
297 log_info( "- glGetTexImage: %s : %s : %s \n",
298 GetGLTargetName( glTarget),
299 GetGLFormatName(readBackFormat),
300 GetGLTypeName(readBackType));
301
302 DumpGLBuffer(readBackType, outWidth, outHeight, (void *)outBuffer);
303
304 #endif
305
306 return (void *)outBuffer;
307 }
308
CreateGLRenderbufferRaw(GLsizei width,GLsizei height,GLenum attachment,GLenum rbFormat,GLenum rbType,GLuint * outFramebuffer,GLuint * outRenderbuffer)309 int CreateGLRenderbufferRaw( GLsizei width, GLsizei height,
310 GLenum attachment,
311 GLenum rbFormat, GLenum rbType,
312 GLuint *outFramebuffer,
313 GLuint *outRenderbuffer )
314 {
315 GLenum err = 0;
316
317 // Generate a renderbuffer and bind
318 glGenRenderbuffersEXT( 1, outRenderbuffer );
319 glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, *outRenderbuffer );
320
321 // Allocate storage to the renderbuffer
322 glGetError();
323 glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, rbFormat, (GLsizei)width, (GLsizei)height );
324 err = glGetError();
325 if( err != GL_NO_ERROR )
326 {
327 log_error("Failed to allocate render buffer storage!\n");
328 return 1701;
329 }
330
331 GLint realInternalFormat;
332 glGetRenderbufferParameterivEXT( GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_INTERNAL_FORMAT_EXT, &realInternalFormat );
333 rbFormat = realInternalFormat;
334
335 #ifdef GLES_DEBUG
336 GLint rsize, gsize, bsize, asize;
337 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_RED_SIZE_EXT,&rsize);
338 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_GREEN_SIZE_EXT,&gsize);
339 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_BLUE_SIZE_EXT,&bsize);
340 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_ALPHA_SIZE_EXT,&asize);
341
342 log_info("Renderbuffer internal format requested: %s actual: %s sizes: r=%d g=%d b=%d a=%d\n",
343 GetGLFormatName( internalFormat ), GetGLFormatName( realInternalFormat ),
344 rsize, gsize, bsize, asize );
345 #endif
346
347 // Create and bind a framebuffer to render with
348 glGenFramebuffersEXT( 1, outFramebuffer );
349 glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, *outFramebuffer );
350 if( err != GL_NO_ERROR )
351 {
352 log_error( "ERROR: Unable to bind framebuffer : Error %s\n",
353 gluErrorString( err ));
354
355 return -1;
356 }
357
358 // Attach to the framebuffer
359 glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, attachment, GL_RENDERBUFFER_EXT, *outRenderbuffer );
360 err = glGetError();
361 GLint status = glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT );
362 if( status != GL_FRAMEBUFFER_COMPLETE_EXT )
363 {
364 log_error( "ERROR: Unable to attach renderbuffer to framebuffer (%s, status %x)\n", gluErrorString( err ), (int)status );
365 return -1;
366 }
367
368 return 0;
369 }
370
DrawQuad(void)371 static void DrawQuad(void)
372 {
373 const char *vssrc =
374 "varying mediump vec2 texCoord;\n"
375 "attribute vec2 inPosition;\n"
376 "void main() {\n"
377 " texCoord = vec2((inPosition.x+1.0)/2.0, (inPosition.y+1.0)/2.0);\n"
378 " gl_Position = vec4(inPosition.x, inPosition.y, 0.0, 1.0);\n"
379 "}\n";
380 const char *fssrc =
381 "uniform sampler2D tex;\n"
382 "varying mediump vec2 texCoord;\n"
383 "void main() {\n"
384 " gl_FragColor = texture2D(tex, texCoord);\n"
385 "}\n";
386 GLuint vs, fs, program;
387 GLuint positionIdx = 0;
388 GLfloat x1 = -1.0f, x2 = 1.0f, y1 = -1.0f, y2 = 1.0f;
389 GLfloat vertices[4][2];
390 vertices[0][0] = x1; vertices[0][1] = y1;
391 vertices[1][0] = x2; vertices[1][1] = y1;
392 vertices[2][0] = x1; vertices[2][1] = y2;
393 vertices[3][0] = x2; vertices[3][1] = y2;
394
395 vs = glCreateShader(GL_VERTEX_SHADER);
396 fs = glCreateShader(GL_FRAGMENT_SHADER);
397
398 glShaderSource(vs, 1, &vssrc, NULL);
399 glShaderSource(fs, 1, &fssrc, NULL);
400
401 glCompileShader(vs);
402 glCompileShader(fs);
403
404 program = glCreateProgram();
405 glAttachShader(program, vs);
406 glAttachShader(program, fs);
407 glLinkProgram(program);
408 glUseProgram(program);
409
410 positionIdx = glGetAttribLocation(program, "inPosition");
411 glEnableVertexAttribArray(positionIdx);
412 glVertexAttribPointer(positionIdx, 2, GL_FLOAT, GL_FALSE, 0, vertices);
413 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
414
415 glUseProgram(0);
416 glDeleteProgram(program);
417 glDeleteShader(vs);
418 glDeleteShader(fs);
419 }
420
CreateGLRenderbuffer(GLsizei width,GLsizei height,GLenum attachment,GLenum rbFormat,GLenum rbType,GLenum texFormat,GLenum texType,ExplicitType type,GLuint * outFramebuffer,GLuint * outRenderbuffer,int * outError,MTdata d,bool allocateMem)421 void * CreateGLRenderbuffer( GLsizei width, GLsizei height,
422 GLenum attachment,
423 GLenum rbFormat, GLenum rbType,
424 GLenum texFormat, GLenum texType,
425 ExplicitType type,
426 GLuint *outFramebuffer,
427 GLuint *outRenderbuffer,
428 int *outError, MTdata d, bool allocateMem )
429 {
430 *outError = CreateGLRenderbufferRaw( width, height, attachment, rbFormat, rbType, outFramebuffer, outRenderbuffer );
431
432 if( *outError != 0 )
433 return NULL;
434
435 GLenum err = 0;
436
437 // Generate a renderbuffer and bind
438 glGenRenderbuffersEXT( 1, outRenderbuffer );
439 glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, *outRenderbuffer );
440
441 // Allocate storage to the renderbuffer
442 glGetError();
443 glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, rbFormat, (GLsizei)width, (GLsizei)height );
444 err = glGetError();
445 if( err != GL_NO_ERROR )
446 {
447 *outError = 1701;
448 log_error("Failed to allocate render buffer storage!\n");
449 return NULL;
450 }
451
452 GLint realInternalFormat;
453 glGetRenderbufferParameterivEXT( GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_INTERNAL_FORMAT_EXT, &realInternalFormat );
454 rbFormat = realInternalFormat;
455
456 #ifdef GLES_DEBUG
457 GLint rsize, gsize, bsize, asize;
458 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_RED_SIZE_EXT,&rsize);
459 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_GREEN_SIZE_EXT,&gsize);
460 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_BLUE_SIZE_EXT,&bsize);
461 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_ALPHA_SIZE_EXT,&asize);
462
463 log_info("Renderbuffer internal format requested: %s actual: %s sizes: r=%d g=%d b=%d a=%d\n",
464 GetGLFormatName( internalFormat ), GetGLFormatName( realInternalFormat ),
465 rsize, gsize, bsize, asize );
466 #endif
467
468 // Create and bind a framebuffer to render with
469 glGenFramebuffersEXT( 1, outFramebuffer );
470 glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, *outFramebuffer );
471 err = glGetError();
472 if( err != GL_NO_ERROR )
473 {
474 log_error( "ERROR: Unable to bind framebuffer : Error %s\n",
475 gluErrorString( err ));
476
477 *outError = -1;
478 return NULL;
479 }
480
481 // Attach to the framebuffer
482 glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, attachment, GL_RENDERBUFFER_EXT, *outRenderbuffer );
483 CHECK_ERROR();
484 GLint status = glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT );
485 if( status != GL_FRAMEBUFFER_COMPLETE_EXT )
486 {
487 *outError = -1;
488 log_error( "ERROR: Unable to attach renderbuffer to framebuffer (%s, status %x)\n", gluErrorString( err ), (int)status );
489 return NULL;
490 }
491
492 void* buffer = CreateRandomData(type, width * height * 4, d);
493
494 #ifdef GLES_DEBUG
495 log_info( "- Fillling renderbuffer: %d : %d : %s : %s \n",
496 (int)width, (int)height,
497 GetGLFormatName(glFormat),
498 GetGLTypeName(glType));
499
500 DumpGLBuffer(glType, (int)width, (int)height, (void*)buffer);
501 #endif
502
503 CHECK_ERROR();
504
505 // Fill a texture with our input data
506 glTextureWrapper texture;
507 glGenTextures( 1, &texture );
508 glBindTexture( GL_TEXTURE_2D, texture );
509 CHECK_ERROR();
510 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
511 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
512 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
513 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
514 CHECK_ERROR();
515 glTexImage2D( GL_TEXTURE_2D, 0, texFormat, width, height, 0, texFormat, texType, buffer );
516 CHECK_ERROR();
517
518 // Render fullscreen textured quad
519 glViewport(0, 0, width, height);
520 DrawQuad();
521 CHECK_ERROR();
522
523 // Read back the data in the renderbuffer
524 memset(buffer, 0, width * height * 4 * get_explicit_type_size( type ));
525 glReadPixels( 0, 0, (GLsizei)width, (GLsizei)height, texFormat, texType, buffer );
526
527 err = glGetError();
528 if( err != GL_NO_ERROR )
529 {
530 log_error( "ERROR: Unable to read data via glReadPixels : %d : %d : %s : %s : Error %s\n",
531 (int)width, (int)height,
532 GetGLFormatName(texFormat),
533 GetGLTypeName(texType),
534 gluErrorString( err ));
535 *outError = -1;
536 }
537
538 #ifdef GLES_DEBUG
539 log_info( "- glReadPixels: %d : %d : %s : %s \n",
540 (int)width, (int)height,
541 GetGLFormatName(glFormat),
542 GetGLTypeName(glType));
543
544 DumpGLBuffer(glType, (int)width, (int)height, (void*)buffer);
545 #endif
546
547 if( !allocateMem )
548 {
549 free( buffer );
550 return NULL;
551 }
552
553 #ifndef GL_ES_VERSION_2_0
554 if( glType == GL_UNSIGNED_INT_8_8_8_8_REV && glFormat == GL_BGRA && allocateMem )
555 {
556 // Reverse and reorder to validate since in the
557 // kernel the read_imagef() call always returns RGBA
558 cl_uchar *p = (cl_uchar *)buffer;
559 for( size_t i = 0; i < (size_t)width * height; i++ )
560 {
561 cl_uchar uc0 = p[i * 4 + 0];
562 cl_uchar uc1 = p[i * 4 + 1];
563 cl_uchar uc2 = p[i * 4 + 2];
564 cl_uchar uc3 = p[i * 4 + 3];
565
566 p[ i * 4 + 0 ] = uc2;
567 p[ i * 4 + 1 ] = uc1;
568 p[ i * 4 + 2 ] = uc0;
569 p[ i * 4 + 3 ] = uc3;
570 }
571 }
572 #endif
573
574 return buffer;
575 }
576
ReadGLRenderbuffer(GLuint glFramebuffer,GLuint glRenderbuffer,GLenum attachment,GLenum rbFormat,GLenum rbType,GLenum texFormat,GLenum texType,ExplicitType typeToReadAs,size_t outWidth,size_t outHeight)577 void * ReadGLRenderbuffer( GLuint glFramebuffer, GLuint glRenderbuffer,
578 GLenum attachment,
579 GLenum rbFormat, GLenum rbType,
580 GLenum texFormat, GLenum texType,
581 ExplicitType typeToReadAs,
582 size_t outWidth, size_t outHeight )
583 {
584 glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, glFramebuffer );
585 glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, attachment, GL_RENDERBUFFER_EXT, glRenderbuffer );
586
587 // Attach to the framebuffer
588 GLint err = glGetError();
589 if( glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT ) != GL_FRAMEBUFFER_COMPLETE_EXT )
590 {
591 log_error( "ERROR: Unable to attach renderbuffer to framebuffer (%s)\n", gluErrorString( err ) );
592 return NULL;
593 }
594
595 // Read results from the GL renderbuffer
596 #ifdef GLES_DEBUG
597 log_info( "- Reading back from GL: %d x %d : %s : %s : %s\n",
598 (int)outWidth, (int)outHeight,
599 GetGLFormatName( glInternalFormat ),
600 GetGLFormatName( glFormat ),
601 GetGLTypeName( glType ));
602 #endif
603
604 GLenum readBackFormat = GL_RGBA;
605 GLenum readBackType = texType;
606
607 size_t outBytes = outWidth * outHeight * 4 * GetGLTypeSize(readBackType);
608 void *outBuffer = malloc( outBytes );
609 memset(outBuffer, 0, outBytes);
610
611 glReadPixels( 0, 0, (GLsizei)outWidth, (GLsizei)outHeight, readBackFormat, readBackType, outBuffer );
612
613 #ifdef GLES_DEBUG
614 log_info( "- glReadPixels: %d : %d : %s : %s \n",
615 (int)outWidth, (int)outHeight,
616 GetGLFormatName(readBackFormat),
617 GetGLTypeName(readBackType));
618
619 DumpGLBuffer(readBackType, outWidth, outHeight, outBuffer);
620 #endif
621
622 return (void *)outBuffer;
623 }
624
625 GLenum
GetGLFormat(GLenum internalFormat)626 GetGLFormat(GLenum internalFormat)
627 {
628 GLenum glFormat;
629 switch (internalFormat)
630 {
631 case GL_BGRA:
632 #ifndef GL_ES_VERSION_2_0
633 case GL_RGBA8:
634 case GL_RGBA16:
635 case GL_RGBA32F_ARB:
636 #endif
637 glFormat = GL_RGBA;
638 break;
639 #ifndef GL_ES_VERSION_2_0
640 case GL_RGBA8I_EXT:
641 case GL_RGBA16I_EXT:
642 case GL_RGBA32I_EXT:
643 case GL_RGBA8UI_EXT:
644 case GL_RGBA16UI_EXT:
645 case GL_RGBA32UI_EXT:
646 glFormat = GL_RGBA_INTEGER_EXT;
647 break;
648 #endif
649 default:
650 glFormat = GL_RGBA;
651 break;
652 }
653
654 return glFormat;
655 }
656
GetGLTypeForExplicitType(ExplicitType type)657 GLenum GetGLTypeForExplicitType(ExplicitType type)
658 {
659 switch( type )
660 {
661 case kFloat:
662 return GL_FLOAT;
663 case kInt:
664 return GL_INT;
665 case kUInt:
666 return GL_UNSIGNED_INT;
667 case kShort:
668 return GL_SHORT;
669 case kUShort:
670 return GL_UNSIGNED_SHORT;
671 case kChar:
672 return GL_BYTE;
673 case kUChar:
674 return GL_UNSIGNED_BYTE;
675 case kHalf:
676 #if defined( __APPLE__ )
677 return GL_HALF_FLOAT;
678 #else
679 return GL_HALF_FLOAT_ARB;
680 #endif
681 default:
682 return GL_INT;
683 };
684 }
685
GetGLTypeSize(GLenum type)686 size_t GetGLTypeSize(GLenum type)
687 {
688 switch( type )
689 {
690 case GL_FLOAT:
691 return sizeof(GLfloat);
692 case GL_INT:
693 return sizeof(GLint);
694 case GL_UNSIGNED_INT:
695 return sizeof(GLuint);
696 case GL_SHORT:
697 return sizeof(GLshort);
698 case GL_UNSIGNED_SHORT:
699 return sizeof(GLushort);
700 case GL_BYTE:
701 return sizeof(GLbyte);
702 case GL_UNSIGNED_BYTE:
703 return sizeof(GLubyte);
704 #if defined( __APPLE__ )
705 case GL_HALF_FLOAT:
706 #else
707 case GL_HALF_FLOAT_ARB:
708 #endif
709 return sizeof(GLhalf);
710 default:
711 return kFloat;
712 };
713 }
714
GetExplicitTypeForGLType(GLenum type)715 ExplicitType GetExplicitTypeForGLType(GLenum type)
716 {
717 switch( type )
718 {
719 case GL_FLOAT:
720 return kFloat;
721 case GL_INT:
722 return kInt;
723 case GL_UNSIGNED_INT:
724 return kUInt;
725 case GL_SHORT:
726 return kShort;
727 case GL_UNSIGNED_SHORT:
728 return kUShort;
729 case GL_BYTE:
730 return kChar;
731 case GL_UNSIGNED_BYTE:
732 return kUChar;
733 #if defined( __APPLE__ )
734 case GL_HALF_FLOAT:
735 #else
736 case GL_HALF_FLOAT_ARB:
737 #endif
738 return kHalf;
739 default:
740 return kFloat;
741 };
742 }
743
get_base_gl_target(GLenum target)744 GLenum get_base_gl_target( GLenum target )
745 {
746 switch( target )
747 {
748 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
749 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
750 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
751 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
752 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
753 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
754 return GL_TEXTURE_CUBE_MAP;
755 default:
756 return target;
757 }
758 }
759
GetGLTypeName(GLenum type)760 const char *GetGLTypeName( GLenum type )
761 {
762 switch( type )
763 {
764 case GL_BYTE: return "GL_BYTE";
765 case GL_UNSIGNED_BYTE: return "GL_UNSIGNED_BYTE";
766 case GL_INT: return "GL_INT";
767 case GL_UNSIGNED_INT: return "GL_UNSIGNED_INT";
768 case GL_SHORT: return "GL_SHORT";
769 case GL_UNSIGNED_SHORT: return "GL_UNSIGNED_SHORT";
770 #if defined( __APPLE__ )
771 case GL_HALF_FLOAT: return "GL_HALF_FLOAT";
772 #else
773 case GL_HALF_FLOAT_ARB: return "GL_HALF_FLOAT_ARB";
774 #endif
775 case GL_FLOAT: return "GL_FLOAT";
776 #ifndef GL_ES_VERSION_2_0
777 case GL_UNSIGNED_INT_8_8_8_8: return "GL_UNSIGNED_INT_8_8_8_8";
778 case GL_UNSIGNED_INT_8_8_8_8_REV: return "GL_UNSIGNED_INT_8_8_8_8_REV";
779 #endif
780 default:
781 {
782 static char foo[ 128 ];
783 sprintf( foo, "(Unknown:0x%08x)", (int)type );
784 return foo;
785 }
786 }
787 }
788
GetGLTargetName(GLenum tgt)789 const char *GetGLTargetName( GLenum tgt )
790 {
791 if( tgt == GL_TEXTURE_2D ) return "GL_TEXTURE_2D";
792 if( tgt == GL_TEXTURE_3D ) return "GL_TEXTURE_3D";
793 #ifndef GL_ES_VERSION_2_0
794 if( tgt == GL_TEXTURE_RECTANGLE_EXT ) return "GL_TEXTURE_RECTANGLE_EXT";
795 #endif
796 if( tgt == GL_TEXTURE_CUBE_MAP_POSITIVE_X ) return "GL_TEXTURE_CUBE_MAP_POSITIVE_X";
797 if( tgt == GL_TEXTURE_CUBE_MAP_POSITIVE_Y ) return "GL_TEXTURE_CUBE_MAP_POSITIVE_Y";
798 if( tgt == GL_TEXTURE_CUBE_MAP_POSITIVE_Z ) return "GL_TEXTURE_CUBE_MAP_POSITIVE_Z";
799 if( tgt == GL_TEXTURE_CUBE_MAP_NEGATIVE_X ) return "GL_TEXTURE_CUBE_MAP_NEGATIVE_X";
800 if( tgt == GL_TEXTURE_CUBE_MAP_NEGATIVE_Y ) return "GL_TEXTURE_CUBE_MAP_NEGATIVE_Y";
801 if( tgt == GL_TEXTURE_CUBE_MAP_NEGATIVE_Z ) return "GL_TEXTURE_CUBE_MAP_NEGATIVE_Z";
802 return "";
803 }
804
GetGLAttachmentName(GLenum att)805 const char *GetGLAttachmentName( GLenum att )
806 {
807 if( att == GL_COLOR_ATTACHMENT0_EXT ) return "GL_COLOR_ATTACHMENT0_EXT";
808 #ifndef GL_ES_VERSION_2_0
809 if( att == GL_COLOR_ATTACHMENT1_EXT ) return "GL_COLOR_ATTACHMENT1_EXT";
810 if( att == GL_COLOR_ATTACHMENT2_EXT ) return "GL_COLOR_ATTACHMENT2_EXT";
811 if( att == GL_COLOR_ATTACHMENT3_EXT ) return "GL_COLOR_ATTACHMENT3_EXT";
812 if( att == GL_COLOR_ATTACHMENT4_EXT ) return "GL_COLOR_ATTACHMENT4_EXT";
813 if( att == GL_COLOR_ATTACHMENT5_EXT ) return "GL_COLOR_ATTACHMENT5_EXT";
814 if( att == GL_COLOR_ATTACHMENT6_EXT ) return "GL_COLOR_ATTACHMENT6_EXT";
815 if( att == GL_COLOR_ATTACHMENT7_EXT ) return "GL_COLOR_ATTACHMENT7_EXT";
816 if( att == GL_COLOR_ATTACHMENT8_EXT ) return "GL_COLOR_ATTACHMENT8_EXT";
817 #endif
818 if( att == GL_DEPTH_ATTACHMENT_EXT ) return "GL_DEPTH_ATTACHMENT_EXT";
819 return "";
820 }
GetGLBaseFormatName(GLenum baseformat)821 const char *GetGLBaseFormatName( GLenum baseformat )
822 {
823 switch( baseformat )
824 {
825 case GL_RGBA: return "GL_RGBA";
826 #ifdef GL_ES_VERSION_2_0
827 case GL_BGRA_EXT: return "GL_BGRA_EXT";
828 #else
829 case GL_RGBA8: return "GL_RGBA";
830 case GL_RGBA16: return "GL_RGBA";
831 case GL_BGRA: return "GL_BGRA";
832 case GL_RGBA8I_EXT: return "GL_RGBA_INTEGER_EXT";
833 case GL_RGBA16I_EXT: return "GL_RGBA_INTEGER_EXT";
834 case GL_RGBA32I_EXT: return "GL_RGBA_INTEGER_EXT";
835 case GL_RGBA8UI_EXT: return "GL_RGBA_INTEGER_EXT";
836 case GL_RGBA16UI_EXT: return "GL_RGBA_INTEGER_EXT";
837 case GL_RGBA32UI_EXT: return "GL_RGBA_INTEGER_EXT";
838 case GL_RGBA32F_ARB: return "GL_RGBA";
839
840 case GL_RGBA_INTEGER_EXT: return "GL_RGBA_INTEGER_EXT";
841
842 case GL_ALPHA4: return "GL_ALPHA";
843 case GL_ALPHA8: return "GL_ALPHA";
844 case GL_ALPHA12: return "GL_ALPHA";
845 case GL_ALPHA16: return "GL_ALPHA";
846 case GL_LUMINANCE4: return "GL_LUMINANCE";
847 case GL_LUMINANCE8: return "GL_LUMINANCE";
848 case GL_LUMINANCE12: return "GL_LUMINANCE";
849 case GL_LUMINANCE16: return "GL_LUMINANCE";
850 case GL_LUMINANCE4_ALPHA4: return "GL_LUMINANCE_ALPHA";
851 case GL_LUMINANCE6_ALPHA2: return "GL_LUMINANCE_ALPHA";
852 case GL_LUMINANCE8_ALPHA8: return "GL_LUMINANCE_ALPHA";
853 case GL_LUMINANCE12_ALPHA4: return "GL_LUMINANCE_ALPHA";
854 case GL_LUMINANCE12_ALPHA12: return "GL_LUMINANCE_ALPHA";
855 case GL_LUMINANCE16_ALPHA16: return "GL_LUMINANCE_ALPHA";
856 case GL_INTENSITY: return "GL_INTENSITY";
857 case GL_INTENSITY4: return "GL_INTENSITY";
858 case GL_INTENSITY8: return "GL_INTENSITY";
859 case GL_INTENSITY12: return "GL_INTENSITY";
860 case GL_INTENSITY16: return "GL_INTENSITY";
861 case GL_R3_G3_B2: return "GL_RGB";
862 case GL_RGB4: return "GL_RGB";
863 case GL_RGB5: return "GL_RGB";
864 case GL_RGB8: return "GL_RGB";
865 case GL_RGB10: return "GL_RGB";
866 case GL_RGB12: return "GL_RGB";
867 case GL_RGB16: return "GL_RGB";
868 case GL_RGBA2: return "GL_RGBA";
869 case GL_RGBA4: return "GL_RGBA";
870 case GL_RGB5_A1: return "GL_RGBA";
871 case GL_RGB10_A2: return "GL_RGBA";
872 case GL_RGBA12: return "GL_RGBA";
873 #endif
874
875 default:
876 {
877 static char foo[ 128 ];
878 sprintf( foo, "(Unknown:0x%08x)", (int)baseformat );
879 return foo;
880 }
881 }
882 }
883
GetGLFormatName(GLenum format)884 const char *GetGLFormatName( GLenum format )
885 {
886 switch( format )
887 {
888 case GL_RGBA: return "GL_RGBA";
889 #ifdef GL_ES_VERSION_2_0
890 case GL_BGRA_EXT: return "GL_BGRA_EXT";
891 #else
892 case GL_RGBA8: return "GL_RGBA8";
893 case GL_RGBA16: return "GL_RGBA16";
894 case GL_BGRA: return "GL_BGRA";
895 case GL_RGBA8I_EXT: return "GL_RGBA8I_EXT";
896 case GL_RGBA16I_EXT: return "GL_RGBA16I_EXT";
897 case GL_RGBA32I_EXT: return "GL_RGBA32I_EXT";
898 case GL_RGBA8UI_EXT: return "GL_RGBA8UI_EXT";
899 case GL_RGBA16UI_EXT: return "GL_RGBA16UI_EXT";
900 case GL_RGBA32UI_EXT: return "GL_RGBA32UI_EXT";
901 case GL_RGBA32F_ARB: return "GL_RGBA32F_ARB";
902
903 case GL_RGBA_INTEGER_EXT: return "GL_RGBA_INTEGER_EXT";
904
905 case GL_ALPHA4: return "GL_ALPHA4";
906 case GL_ALPHA8: return "GL_ALPHA8";
907 case GL_ALPHA12: return "GL_ALPHA12";
908 case GL_ALPHA16: return "GL_ALPHA16";
909 case GL_LUMINANCE4: return "GL_LUMINANCE4";
910 case GL_LUMINANCE8: return "GL_LUMINANCE8";
911 case GL_LUMINANCE12: return "GL_LUMINANCE12";
912 case GL_LUMINANCE16: return "GL_LUMINANCE16";
913 case GL_LUMINANCE4_ALPHA4: return "GL_LUMINANCE4_ALPHA4";
914 case GL_LUMINANCE6_ALPHA2: return "GL_LUMINANCE6_ALPHA2";
915 case GL_LUMINANCE8_ALPHA8: return "GL_LUMINANCE8_ALPHA8";
916 case GL_LUMINANCE12_ALPHA4: return "GL_LUMINANCE12_ALPHA4";
917 case GL_LUMINANCE12_ALPHA12: return "GL_LUMINANCE12_ALPHA12";
918 case GL_LUMINANCE16_ALPHA16: return "GL_LUMINANCE16_ALPHA16";
919 case GL_INTENSITY: return "GL_INTENSITY";
920 case GL_INTENSITY4: return "GL_INTENSITY4";
921 case GL_INTENSITY8: return "GL_INTENSITY8";
922 case GL_INTENSITY12: return "GL_INTENSITY12";
923 case GL_INTENSITY16: return "GL_INTENSITY16";
924 case GL_R3_G3_B2: return "GL_R3_G3_B2";
925 case GL_RGB4: return "GL_RGB4";
926 case GL_RGB5: return "GL_RGB5";
927 case GL_RGB8: return "GL_RGB8";
928 case GL_RGB10: return "GL_RGB10";
929 case GL_RGB12: return "GL_RGB12";
930 case GL_RGB16: return "GL_RGB16";
931 case GL_RGBA2: return "GL_RGBA2";
932 case GL_RGBA4: return "GL_RGBA4";
933 case GL_RGB5_A1: return "GL_RGB5_A1";
934 case GL_RGB10_A2: return "GL_RGB10_A2";
935 case GL_RGBA12: return "GL_RGBA12";
936 #endif
937 case GL_INT: return "GL_INT";
938 case GL_UNSIGNED_INT: return "GL_UNSIGNED_INT";
939 case GL_SHORT: return "GL_SHORT";
940 case GL_UNSIGNED_SHORT: return "GL_UNSIGNED_SHORT";
941 case GL_BYTE: return "GL_BYTE";
942 case GL_UNSIGNED_BYTE: return "GL_UNSIGNED_BYTE";
943 case GL_FLOAT: return "GL_FLOAT";
944 #ifdef GL_ES_VERSION_2_0
945 case GL_HALF_FLOAT_OES: return "GL_HALF_FLOAT_OES";
946 #else
947 #if defined( __APPLE__ )
948 case GL_HALF_FLOAT: return "GL_HALF_FLOAT";
949 #else
950 case GL_HALF_FLOAT_ARB: return "GL_HALF_FLOAT_ARB";
951 #endif
952 #endif
953
954 default:
955 {
956 static char foo[ 128 ];
957 sprintf( foo, "(Unknown:0x%08x)", (int)format );
958 return foo;
959 }
960 }
961 }
962
CreateRandomData(ExplicitType type,size_t count,MTdata d)963 void* CreateRandomData( ExplicitType type, size_t count, MTdata d )
964 {
965 switch(type)
966 {
967 case (kChar):
968 {
969 cl_char *p = (cl_char *)malloc(count * sizeof(cl_char));
970 if(!p) return 0;
971
972 for( size_t i = 0; i < count; i++ )
973 {
974 p[ i ] = (cl_char)genrand_int32(d);
975 }
976 return (void*)p;
977 }
978 case (kUChar):
979 {
980 cl_uchar *p = (cl_uchar *)malloc(count * sizeof(cl_uchar));
981 if(!p) return 0;
982
983 for( size_t i = 0; i < count; i++ )
984 {
985 p[ i ] = (cl_uchar)genrand_int32(d);
986 }
987
988 return (void*)p;
989 }
990 case (kShort):
991 {
992 cl_short *p = (cl_short *)malloc(count * sizeof(cl_short));
993 if(!p) return 0;
994
995 for( size_t i = 0; i < count; i++ )
996 {
997 p[ i ] = (cl_short)genrand_int32(d);
998 }
999
1000 return (void*)p;
1001 }
1002 case (kUShort):
1003 {
1004 cl_ushort *p = (cl_ushort *)malloc(count * sizeof(cl_ushort));
1005 if(!p) return 0;
1006
1007 for( size_t i = 0; i < count; i++ )
1008 {
1009 p[ i ] = (cl_ushort)genrand_int32(d);
1010 }
1011
1012 return (void*)p;
1013 }
1014 case (kInt):
1015 {
1016 cl_int *p = (cl_int *)malloc(count * sizeof(cl_int));
1017 if(!p) return 0;
1018
1019 for( size_t i = 0; i < count; i++ )
1020 {
1021 p[ i ] = (cl_int)genrand_int32(d);
1022 }
1023
1024 return (void*)p;
1025 }
1026 case (kUInt):
1027 {
1028 cl_uint *p = (cl_uint *)malloc(count * sizeof(cl_uint));
1029 if(!p) return 0;
1030
1031 for( size_t i = 0; i < count; i++ )
1032 {
1033 p[ i ] = (cl_uint)genrand_int32(d);
1034 }
1035
1036 return (void*)p;
1037 }
1038
1039 case (kFloat):
1040 {
1041 cl_float *p = (cl_float *)malloc(count * sizeof(cl_float));
1042 if(!p) return 0;
1043
1044 for( size_t i = 0; i < count; i++ )
1045 {
1046 p[ i ] = get_random_float( 0.f, 1.f, d );
1047 }
1048
1049 return (void*)p;
1050 }
1051 /* added support for half floats */
1052 case (kHalf):
1053 {
1054 cl_half *p = (cl_half *)malloc(count * sizeof(cl_half));
1055 if(!p) return 0;
1056
1057 for( size_t i = 0; i < count; i++ )
1058 {
1059 p[i] = cl_half_from_float(get_random_float(0.f, 1.f, d),
1060 CL_HALF_RTE);
1061 }
1062
1063 return (void*)p;
1064 }
1065 default:
1066 {
1067 log_error("Invalid explicit type specified for create random data!\n");
1068 return 0;
1069 }
1070 }
1071 return 0;
1072 }
1073
DumpGLBuffer(GLenum type,size_t width,size_t height,void * buffer)1074 void DumpGLBuffer(GLenum type, size_t width, size_t height, void* buffer)
1075 {
1076 size_t i;
1077 size_t count = width * height;
1078 if(type == GL_BYTE)
1079 {
1080 cl_char* p = (cl_char*)buffer;
1081 for(i = 0; i < count; i++)
1082 log_info("[%4d] %3d %3d %3d %3d\n", (unsigned int)(i),
1083 p[i* 4 + 0],
1084 p[i* 4 + 1],
1085 p[i* 4 + 2],
1086 p[i* 4 + 3]);
1087 }
1088 else if(type == GL_UNSIGNED_BYTE)
1089 {
1090 cl_uchar* p = (cl_uchar*)buffer;
1091 for(i = 0; i < count; i++)
1092 log_info("[%4d] %3d %3d %3d %3d\n", (unsigned int)(i),
1093 p[i* 4 + 0],
1094 p[i* 4 + 1],
1095 p[i* 4 + 2],
1096 p[i* 4 + 3]);
1097 }
1098 else if(type == GL_INT)
1099 {
1100 cl_int* p = (cl_int*)buffer;
1101 for(i = 0; i < count; i++)
1102 log_info("[%4d] %3d %3d %3d %3d\n", (unsigned int)(i),
1103 p[i* 4 + 0],
1104 p[i* 4 + 1],
1105 p[i* 4 + 2],
1106 p[i* 4 + 3]);
1107 }
1108 else if(type == GL_UNSIGNED_INT)
1109 {
1110 cl_uint* p = (cl_uint*)buffer;
1111 for(i = 0; i < count; i++)
1112 log_info("[%4d] %3d %3d %3d %3d\n", (unsigned int)(i),
1113 p[i* 4 + 0],
1114 p[i* 4 + 1],
1115 p[i* 4 + 2],
1116 p[i* 4 + 3]);
1117 }
1118 else if(type == GL_SHORT)
1119 {
1120 cl_short* p = (cl_short*)buffer;
1121 for(i = 0; i < count; i++)
1122 log_info("[%4d] %3d %3d %3d %3d\n", (unsigned int)(i),
1123 p[i* 4 + 0],
1124 p[i* 4 + 1],
1125 p[i* 4 + 2],
1126 p[i* 4 + 3]);
1127 }
1128 else if(type == GL_UNSIGNED_SHORT)
1129 {
1130 cl_ushort* p = (cl_ushort*)buffer;
1131 for(i = 0; i < count; i++)
1132 log_info("[%4d] %3d %3d %3d %3d\n", (unsigned int)(i),
1133 p[i* 4 + 0],
1134 p[i* 4 + 1],
1135 p[i* 4 + 2],
1136 p[i* 4 + 3]);
1137 }
1138 else if(type == GL_FLOAT)
1139 {
1140 cl_float* p = (cl_float*)buffer;
1141 for(i = 0; i < count; i++)
1142 log_info("[%4d] %#f %#f %#f %#f\n", (unsigned int)(i),
1143 p[i* 4 + 0],
1144 p[i* 4 + 1],
1145 p[i* 4 + 2],
1146 p[i* 4 + 3]);
1147 }
1148 }
1149
1150 #if defined(_WIN32)
1151 #include <string.h>
1152
gluCheckExtension(const GLubyte * extName,const GLubyte * extString)1153 GLboolean gluCheckExtension(const GLubyte *extName, const GLubyte *extString)
1154 {
1155 const size_t len = strlen((const char*)extName);
1156 const char* str = (const char*)extString;
1157
1158 while (str != NULL) {
1159 str = strstr(str, (const char*)extName);
1160 if (str == NULL) {
1161 break;
1162 }
1163 if ((str > (const char*)extString || str[-1] == ' ')
1164 && (str[len] == ' ' || str[len] == '\0')) {
1165 return GL_TRUE;
1166 }
1167 str = strchr(str + len, ' ');
1168 }
1169
1170 return GL_FALSE;
1171 }
1172
1173 #endif
1174
1175 // Function pointers for the GL/CL calls
1176 clCreateFromGLBuffer_fn clCreateFromGLBuffer_ptr;
1177 clCreateFromGLTexture_fn clCreateFromGLTexture_ptr;
1178 clCreateFromGLRenderbuffer_fn clCreateFromGLRenderbuffer_ptr;
1179 clGetGLObjectInfo_fn clGetGLObjectInfo_ptr;
1180 clGetGLTextureInfo_fn clGetGLTextureInfo_ptr;
1181 clEnqueueAcquireGLObjects_fn clEnqueueAcquireGLObjects_ptr;
1182 clEnqueueReleaseGLObjects_fn clEnqueueReleaseGLObjects_ptr;
1183
init_clgl_ext(cl_platform_id platform_id)1184 int init_clgl_ext(cl_platform_id platform_id)
1185 {
1186 // Create the function pointer table
1187 clCreateFromGLBuffer_ptr = (clCreateFromGLBuffer_fn)clGetExtensionFunctionAddressForPlatform(platform_id, "clCreateFromGLBuffer");
1188 if (clCreateFromGLBuffer_ptr == NULL)
1189 {
1190 log_error("clGetExtensionFunctionAddressForPlatform(clCreateFromGLBuffer) returned NULL.\n");
1191 return -1;
1192 }
1193
1194 clCreateFromGLTexture_ptr = (clCreateFromGLTexture_fn)clGetExtensionFunctionAddressForPlatform(platform_id, "clCreateFromGLTexture");
1195 if (clCreateFromGLTexture_ptr == NULL)
1196 {
1197 log_error("clGetExtensionFunctionAddressForPlatform(clCreateFromGLTexture) returned NULL.\n");
1198 return -1;
1199 }
1200
1201 clCreateFromGLRenderbuffer_ptr = (clCreateFromGLRenderbuffer_fn)clGetExtensionFunctionAddressForPlatform(platform_id, "clCreateFromGLRenderbuffer");
1202 if (clCreateFromGLRenderbuffer_ptr == NULL)
1203 {
1204 log_error("clGetExtensionFunctionAddressForPlatform(clCreateFromGLRenderbuffer) returned NULL.\n");
1205 return -1;
1206 }
1207
1208 clGetGLObjectInfo_ptr = (clGetGLObjectInfo_fn)clGetExtensionFunctionAddressForPlatform(platform_id, "clGetGLObjectInfo");
1209 if (clGetGLObjectInfo_ptr == NULL)
1210 {
1211 log_error("clGetExtensionFunctionAddressForPlatform(clGetGLObjectInfo) returned NULL.\n");
1212 return -1;
1213 }
1214
1215 clGetGLTextureInfo_ptr = (clGetGLTextureInfo_fn)clGetExtensionFunctionAddressForPlatform(platform_id, "clGetGLTextureInfo");
1216 if (clGetGLTextureInfo_ptr == NULL)
1217 {
1218 log_error("clGetExtensionFunctionAddressForPlatform(clGetGLTextureInfo) returned NULL.\n");
1219 return -1;
1220 }
1221
1222 clEnqueueAcquireGLObjects_ptr = (clEnqueueAcquireGLObjects_fn)clGetExtensionFunctionAddressForPlatform(platform_id, "clEnqueueAcquireGLObjects");
1223 if (clEnqueueAcquireGLObjects_ptr == NULL)
1224 {
1225 log_error("clGetExtensionFunctionAddressForPlatform(clEnqueueAcquireGLObjects) returned NULL.\n");
1226 return -1;
1227 }
1228
1229 clEnqueueReleaseGLObjects_ptr = (clEnqueueReleaseGLObjects_fn)clGetExtensionFunctionAddressForPlatform(platform_id, "clEnqueueReleaseGLObjects");
1230 if (clEnqueueReleaseGLObjects_ptr == NULL)
1231 {
1232 log_error("clGetExtensionFunctionAddressForPlatform(clEnqueueReleaseGLObjects) returned NULL.\n");
1233 return -1;
1234 }
1235
1236 return 0;
1237 }
1238
1239
1240