// // Copyright (c) 2017 The Khronos Group Inc. // // 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 "helpers.h" #include "harness/imageHelpers.h" #if defined( __APPLE__ ) #include #else #include #endif #if defined(__linux__) // On linux we dont link to GLU library to avoid comaptibility issues with // libstdc++ // FIXME: Implement this const GLubyte* gluErrorString (GLenum error) { const char* gl_Error = "OpenGL Error"; return (const GLubyte*)gl_Error; } #endif void * CreateGLTexture1DArray(size_t width, size_t length, GLenum target, GLenum glFormat, GLenum internalFormat, GLenum glType, ExplicitType type, GLuint *outTextureID, int *outError, bool allocateMem, MTdata d) { *outError = 0; GLenum err = 0; char * buffer; unsigned int size = 0; // width_in_pixels * pixel_width * number_of_images: if ( (glType == GL_UNSIGNED_INT_2_10_10_10_REV) || (glType == GL_UNSIGNED_INT_10_10_10_2) ) { size = width * length; } else { size = width * length * 4; } buffer = (char *)CreateRandomData(type, size, d); glGenTextures( 1, outTextureID ); glBindTexture( get_base_gl_target( target ), *outTextureID ); glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE ); glTexParameteri( get_base_gl_target( target ), GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexParameteri( get_base_gl_target( target ), GL_TEXTURE_MAG_FILTER, GL_NEAREST ); err = glGetError(); if( err != GL_NO_ERROR ) { log_error( "ERROR: Failed to create GL texture object: %s!\n", gluErrorString( err )); *outError = -1; free( buffer ); return NULL; } // use TexImage2D to pump the 1D array fill of bits: glTexImage2D( get_base_gl_target(target), 0, internalFormat, (GLsizei)width, (GLsizei)length, 0, glFormat, glType, buffer ); err = glGetError(); if( err != GL_NO_ERROR ) { if (err != GL_OUT_OF_MEMORY) { log_error( "ERROR: Unable to load data using glTexImage2D for " "TEXTURE_1D_ARRAY : %s : %s : %d : %d : %s : %s : Error %s\n", GetGLTargetName(target), GetGLFormatName(internalFormat), (int)(width), (int)(length), GetGLFormatName(glFormat), GetGLTypeName(glType), gluErrorString( err )); *outError = -1; } else { log_info( "WARNING: Unable to load data using glTexImage2D for " "TEXTURE_1D_ARRAY : %s : %s : %d : %d : %s : %s : Error %s\n", GetGLTargetName(target), GetGLFormatName(internalFormat), (int)(width), (int)(length), GetGLFormatName(glFormat), GetGLTypeName(glType), gluErrorString( err )); *outError = -2; } free( buffer ); return NULL; } if( !allocateMem ) { free( buffer ); return NULL; } if( glType == GL_UNSIGNED_INT_8_8_8_8_REV && glFormat == GL_BGRA && allocateMem ) { // Reverse and reorder to validate since in the // kernel the read_imagef() call always returns RGBA cl_uchar *p = (cl_uchar *)buffer; for( size_t i = 0; i < width * length; i++ ) { cl_uchar uc0 = p[i * 4 + 0]; cl_uchar uc1 = p[i * 4 + 1]; cl_uchar uc2 = p[i * 4 + 2]; cl_uchar uc3 = p[i * 4 + 3]; p[ i * 4 + 0 ] = uc2; p[ i * 4 + 1 ] = uc1; p[ i * 4 + 2 ] = uc0; p[ i * 4 + 3 ] = uc3; } } else if( glType == GL_UNSIGNED_INT_8_8_8_8 && glFormat == GL_BGRA && allocateMem ) { // Reverse and reorder to validate since in the // kernel the read_imagef() call always returns RGBA cl_uchar *p = (cl_uchar *)buffer; for( size_t i = 0; i < width * length; i++ ) { cl_uchar uc0 = p[i * 4 + 0]; cl_uchar uc1 = p[i * 4 + 1]; cl_uchar uc2 = p[i * 4 + 2]; cl_uchar uc3 = p[i * 4 + 3]; p[ i * 4 + 0 ] = uc1; p[ i * 4 + 1 ] = uc2; p[ i * 4 + 2 ] = uc3; p[ i * 4 + 3 ] = uc0; } } return buffer; } void * CreateGLTexture2DArray(size_t width, size_t height, size_t length, GLenum target, GLenum glFormat, GLenum internalFormat, GLenum glType, ExplicitType type, GLuint *outTextureID, int *outError, bool allocateMem, MTdata d) { *outError = 0; char * buffer; unsigned int size = 0; if ( (glType == GL_UNSIGNED_INT_2_10_10_10_REV) || (glType == GL_UNSIGNED_INT_10_10_10_2) ) { size = width * height * length; } else { size = width * height * length * 4; } buffer = (char *)CreateRandomData(type, size, d); if( type == kFloat && allocateMem ) { // Re-fill the created buffer to just have [0-1] floats, since that's what it'd expect cl_float *p = (cl_float *)buffer; for( size_t i = 0; i < size; i++ ) { p[ i ] = (float) genrand_real1( d ); } } else if( !allocateMem ) memset( buffer, 0, size * get_explicit_type_size( type ) ); glGenTextures( 1, outTextureID ); glBindTexture( target, *outTextureID ); glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE ); glTexParameteri( target, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexParameteri( target, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); glGetError(); //the default alignment in OpenGL is 4 bytes and need to be changed for GL_DEPTH_COMPONENT16 which is aligned to 2 bytes if (internalFormat == GL_DEPTH_COMPONENT16) glPixelStorei(GL_UNPACK_ALIGNMENT, get_explicit_type_size( type )); glTexImage3D( target, 0, internalFormat, (GLsizei)width, (GLsizei)height, (GLsizei)length, 0, glFormat, glType, buffer ); if (internalFormat == GL_DEPTH_COMPONENT16) glPixelStorei(GL_UNPACK_ALIGNMENT, 4); GLenum err = glGetError(); if( err != GL_NO_ERROR ) { if (err != GL_OUT_OF_MEMORY) { log_error( "ERROR: Unable to load data into GL texture (%s) format %s " "type %s internal format %s\n", gluErrorString( err ), GetGLFormatName( glFormat ), get_explicit_type_name( type ), GetGLFormatName( internalFormat ) ); *outError = -1; } else { log_info( "WARNING: Unable to load data into GL texture (%s) format %s " "type %s internal format %s\n", gluErrorString( err ), GetGLFormatName( glFormat ), get_explicit_type_name( type ), GetGLFormatName( internalFormat ) ); *outError = -2; } delete [] buffer; return NULL; } if( !allocateMem ) { delete [] buffer; return NULL; } if( glType == GL_UNSIGNED_INT_8_8_8_8_REV && glFormat == GL_BGRA && allocateMem ) { // Reverse and reorder to validate since in the // kernel the read_imagef() call always returns RGBA cl_uchar *p = (cl_uchar *)buffer; for( size_t i = 0; i < width * height * length; i++ ) { cl_uchar uc0 = p[i * 4 + 0]; cl_uchar uc1 = p[i * 4 + 1]; cl_uchar uc2 = p[i * 4 + 2]; cl_uchar uc3 = p[i * 4 + 3]; p[ i * 4 + 0 ] = uc2; p[ i * 4 + 1 ] = uc1; p[ i * 4 + 2 ] = uc0; p[ i * 4 + 3 ] = uc3; } } else if( glType == GL_UNSIGNED_INT_8_8_8_8 && glFormat == GL_BGRA && allocateMem ) { // Reverse and reorder to validate since in the // kernel the read_imagef() call always returns RGBA cl_uchar *p = (cl_uchar *)buffer; for( size_t i = 0; i < width * length; i++ ) { cl_uchar uc0 = p[i * 4 + 0]; cl_uchar uc1 = p[i * 4 + 1]; cl_uchar uc2 = p[i * 4 + 2]; cl_uchar uc3 = p[i * 4 + 3]; p[ i * 4 + 0 ] = uc1; p[ i * 4 + 1 ] = uc2; p[ i * 4 + 2 ] = uc3; p[ i * 4 + 3 ] = uc0; } } return buffer; } void * CreateGLTextureBuffer(size_t width, GLenum target, GLenum glFormat, GLenum internalFormat, GLenum glType, ExplicitType type, GLuint *outTex, GLuint *outBuf, int *outError, bool allocateMem, MTdata d) { // First, generate a regular GL Buffer from random data. *outError = 0; GLenum err = 0; char * buffer; unsigned int size = 0; // The buffer should be the array width * number of elements * element pitch if ( (glType == GL_UNSIGNED_INT_2_10_10_10_REV) || (glType == GL_UNSIGNED_INT_10_10_10_2) ) { size = width; } else { size = width * 4; } buffer = (char*)CreateRandomData(type, size, d); err = glGetError(); glGenBuffers(1, outBuf); glBindBuffer(GL_TEXTURE_BUFFER, *outBuf); // Need to multiply by the type size: size *= ( GetGLTypeSize( GetGLTypeForExplicitType(type) ) ); glBufferData(GL_TEXTURE_BUFFER, size, buffer, GL_DYNAMIC_DRAW); // Now make a Texture out of this Buffer: glGenTextures(1, outTex); glBindTexture(GL_TEXTURE_BUFFER, *outTex); glTexBuffer(GL_TEXTURE_BUFFER, internalFormat, *outBuf); if ((err = glGetError())) { log_error( "ERROR: Unable to load data into glTexBuffer : %s : %s : %d : %s : %s : Error %s\n", GetGLTargetName(target), GetGLFormatName(internalFormat), (int)(size), GetGLFormatName(glFormat), GetGLTypeName(glType), gluErrorString( err )); *outError = -1; delete [] buffer; return NULL; } if( !allocateMem ) { free( buffer ); return NULL; } if( glType == GL_UNSIGNED_INT_8_8_8_8_REV && glFormat == GL_BGRA && allocateMem ) { // Reverse and reorder to validate since in the // kernel the read_imagef() call always returns RGBA cl_uchar *p = (cl_uchar *)buffer; for( size_t i = 0; i < width; i++ ) { cl_uchar uc0 = p[i * 4 + 0]; cl_uchar uc1 = p[i * 4 + 1]; cl_uchar uc2 = p[i * 4 + 2]; cl_uchar uc3 = p[i * 4 + 3]; p[ i * 4 + 0 ] = uc2; p[ i * 4 + 1 ] = uc1; p[ i * 4 + 2 ] = uc0; p[ i * 4 + 3 ] = uc3; } } else if( glType == GL_UNSIGNED_INT_8_8_8_8 && glFormat == GL_BGRA && allocateMem ) { // Reverse and reorder to validate since in the // kernel the read_imagef() call always returns RGBA cl_uchar *p = (cl_uchar *)buffer; for( size_t i = 0; i < width; i++ ) { cl_uchar uc0 = p[i * 4 + 0]; cl_uchar uc1 = p[i * 4 + 1]; cl_uchar uc2 = p[i * 4 + 2]; cl_uchar uc3 = p[i * 4 + 3]; p[ i * 4 + 0 ] = uc1; p[ i * 4 + 1 ] = uc2; p[ i * 4 + 2 ] = uc3; p[ i * 4 + 3 ] = uc0; } } return buffer; } void* CreateGLTexture1D( size_t width, GLenum target, GLenum glFormat, GLenum internalFormat, GLenum glType, ExplicitType type, GLuint *outTextureID, int *outError, bool allocateMem, MTdata d ) { *outError = 0; GLenum err = 0; char * buffer; unsigned int size = 0; // The buffer should be the array width * number of elements * element pitch if ( (glType == GL_UNSIGNED_INT_2_10_10_10_REV) || (glType == GL_UNSIGNED_INT_10_10_10_2) ) { size = width; } else { size = width * 4; } buffer = (char*)CreateRandomData(type, size, d); glGenTextures( 1, outTextureID ); glBindTexture( get_base_gl_target( target ), *outTextureID ); glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE ); glTexParameteri( get_base_gl_target( target ), GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexParameteri( get_base_gl_target( target ), GL_TEXTURE_MAG_FILTER, GL_NEAREST ); err = glGetError(); if( err != GL_NO_ERROR ) { log_error( "ERROR: Failed to create GL texture object: %s!\n", gluErrorString( err )); *outError = -1; free( buffer ); return NULL; } glTexImage1D( get_base_gl_target(target), 0, internalFormat, (GLsizei)width, 0, glFormat, glType, buffer ); err = glGetError(); if( err != GL_NO_ERROR ) { if (err != GL_OUT_OF_MEMORY) { log_error( "ERROR: Unable to load data into glTexImage1D : %s : %s : %d : %s : %s : Error %s\n", GetGLTargetName(target), GetGLFormatName(internalFormat), (int)(width), GetGLFormatName(glFormat), GetGLTypeName(glType), gluErrorString( err )); *outError = -1; } else { log_info( "WARNING: Unable to load data into glTexImage1D : %s : %s : %d : %s : %s : Error %s\n", GetGLTargetName(target), GetGLFormatName(internalFormat), (int)(width), GetGLFormatName(glFormat), GetGLTypeName(glType), gluErrorString( err )); *outError = -2; } free( buffer ); return NULL; } if( !allocateMem ) { free( buffer ); return NULL; } if( glType == GL_UNSIGNED_INT_8_8_8_8_REV && glFormat == GL_BGRA && allocateMem ) { // Reverse and reorder to validate since in the // kernel the read_imagef() call always returns RGBA cl_uchar *p = (cl_uchar *)buffer; for( size_t i = 0; i < width; i++ ) { cl_uchar uc0 = p[i * 4 + 0]; cl_uchar uc1 = p[i * 4 + 1]; cl_uchar uc2 = p[i * 4 + 2]; cl_uchar uc3 = p[i * 4 + 3]; p[ i * 4 + 0 ] = uc2; p[ i * 4 + 1 ] = uc1; p[ i * 4 + 2 ] = uc0; p[ i * 4 + 3 ] = uc3; } } else if( glType == GL_UNSIGNED_INT_8_8_8_8 && glFormat == GL_BGRA && allocateMem ) { // Reverse and reorder to validate since in the // kernel the read_imagef() call always returns RGBA cl_uchar *p = (cl_uchar *)buffer; for( size_t i = 0; i < width; i++ ) { cl_uchar uc0 = p[i * 4 + 0]; cl_uchar uc1 = p[i * 4 + 1]; cl_uchar uc2 = p[i * 4 + 2]; cl_uchar uc3 = p[i * 4 + 3]; p[ i * 4 + 0 ] = uc1; p[ i * 4 + 1 ] = uc2; p[ i * 4 + 2 ] = uc3; p[ i * 4 + 3 ] = uc0; } } return buffer; } void * 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 ) { *outError = 0; GLenum err = 0; char * buffer; unsigned int size = 0; if ( (glType == GL_UNSIGNED_INT_2_10_10_10_REV) || (glType == GL_UNSIGNED_INT_10_10_10_2) ) { size = width * height; } else { size = width * height * 4; } buffer = (char *)CreateRandomData(type, size, d); glGenTextures( 1, outTextureID ); glBindTexture( get_base_gl_target( target ), *outTextureID ); glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE ); glTexParameteri( get_base_gl_target( target ), GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexParameteri( get_base_gl_target( target ), GL_TEXTURE_MAG_FILTER, GL_NEAREST ); err = glGetError(); if( err != GL_NO_ERROR ) { log_error( "ERROR: Failed to create GL texture object: %s!\n", gluErrorString( err )); *outError = -1; free( buffer ); return NULL; } if( get_base_gl_target( target ) == GL_TEXTURE_CUBE_MAP ) { char * temp = (char *)malloc(size * get_explicit_type_size( type ) * sizeof(cl_char)); if(allocateMem) memcpy( temp, buffer, size * get_explicit_type_size( type ) ); else memset( temp, 0, size * get_explicit_type_size( type ) ); glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, internalFormat, (GLsizei)width, (GLsizei)height, 0, glFormat, glType, temp ); glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, internalFormat, (GLsizei)width, (GLsizei)height, 0, glFormat, glType, temp ); glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, internalFormat, (GLsizei)width, (GLsizei)height, 0, glFormat, glType, temp ); glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, internalFormat, (GLsizei)width, (GLsizei)height, 0, glFormat, glType, temp ); glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, internalFormat, (GLsizei)width, (GLsizei)height, 0, glFormat, glType, temp ); glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, internalFormat, (GLsizei)width, (GLsizei)height, 0, glFormat, glType, temp ); free(temp); } else { #ifdef DEBUG log_info("- glTexImage2D : %s : %s : %d : %d : %s : %s\n", GetGLTargetName(target), GetGLFormatName(internalFormat), width, height, GetGLFormatName(glFormat), GetGLTypeName(glType)); DumpGLBuffer(glType, width, height, buffer); #endif //the default alignment in OpenGL is 4 bytes and need to be changed for GL_DEPTH_COMPONENT16 which is aligned to 2 bytes if (internalFormat == GL_DEPTH_COMPONENT16) glPixelStorei(GL_UNPACK_ALIGNMENT, get_explicit_type_size( type )); glTexImage2D( get_base_gl_target(target), 0, internalFormat, (GLsizei)width, (GLsizei)height, 0, glFormat, glType, buffer ); if (internalFormat == GL_DEPTH_COMPONENT16) glPixelStorei(GL_UNPACK_ALIGNMENT, 4); } err = glGetError(); if( err != GL_NO_ERROR ) { if (err != GL_OUT_OF_MEMORY) { log_error( "ERROR: Unable to load data into glTexImage2D : %s : %s : %d : %d : %s : %s : Error %s\n", GetGLTargetName(target), GetGLFormatName(internalFormat), (int)(width), (int)(height), GetGLFormatName(glFormat), GetGLTypeName(glType), gluErrorString( err )); *outError = -1; } else { log_info( "WARNING: Unable to load data into glTexImage2D : %s : %s : %d : %d : %s : %s : Error %s\n", GetGLTargetName(target), GetGLFormatName(internalFormat), (int)(width), (int)(height), GetGLFormatName(glFormat), GetGLTypeName(glType), gluErrorString( err )); *outError = -2; } free( buffer ); return NULL; } #ifdef DEBUG char * test = (char *)malloc(width * height * 4 * get_explicit_type_size( type )); memset(test, 0, width * height * 4 * get_explicit_type_size( type )); if ( (glType == GL_UNSIGNED_INT_2_10_10_10_REV) || (glType == GL_UNSIGNED_INT_10_10_10_2) ) { glFormat = GL_RGBA; glType = GL_FLOAT; } log_info("- glGetTexImage : %s : %s : %s\n", GetGLTargetName(target), GetGLFormatName(glFormat), GetGLTypeName(glType)); glGetTexImage(target, 0, glFormat, glType, test); DumpGLBuffer(glType, width, height, test); free(test); err = glGetError(); if( err != GL_NO_ERROR ) { log_error( "ERROR: Unable to read data from glGetTexImage : %s : %s : %s : Error %s\n", GetGLTargetName(target), GetGLFormatName(glFormat), GetGLTypeName(glType), gluErrorString( err )); return NULL; } #endif if( !allocateMem ) { free( buffer ); return NULL; } if( glType == GL_UNSIGNED_INT_8_8_8_8_REV && glFormat == GL_BGRA && allocateMem ) { // Reverse and reorder to validate since in the // kernel the read_imagef() call always returns RGBA cl_uchar *p = (cl_uchar *)buffer; for( size_t i = 0; i < width * height; i++ ) { cl_uchar uc0 = p[i * 4 + 0]; cl_uchar uc1 = p[i * 4 + 1]; cl_uchar uc2 = p[i * 4 + 2]; cl_uchar uc3 = p[i * 4 + 3]; p[ i * 4 + 0 ] = uc2; p[ i * 4 + 1 ] = uc1; p[ i * 4 + 2 ] = uc0; p[ i * 4 + 3 ] = uc3; } } else if( glType == GL_UNSIGNED_INT_8_8_8_8 && glFormat == GL_BGRA && allocateMem ) { // Reverse and reorder to validate since in the // kernel the read_imagef() call always returns RGBA cl_uchar *p = (cl_uchar *)buffer; for( size_t i = 0; i < width * height; i++ ) { cl_uchar uc0 = p[i * 4 + 0]; cl_uchar uc1 = p[i * 4 + 1]; cl_uchar uc2 = p[i * 4 + 2]; cl_uchar uc3 = p[i * 4 + 3]; p[ i * 4 + 0 ] = uc1; p[ i * 4 + 1 ] = uc2; p[ i * 4 + 2 ] = uc3; p[ i * 4 + 3 ] = uc0; } } return buffer; } void * 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) { *outError = 0; char * buffer; unsigned int size = 0; if ( (glType == GL_UNSIGNED_INT_2_10_10_10_REV) || (glType == GL_UNSIGNED_INT_10_10_10_2) ) { size = width * height * depth; } else { size = width * height * depth * 4; } buffer = (char *)create_random_data( type, d, size ); if( type == kFloat && allocateMem ) { // Re-fill the created buffer to just have [0-1] floats, since that's what it'd expect cl_float *p = (cl_float *)buffer; for( size_t i = 0; i < size; i++ ) { p[ i ] = (float) genrand_real1( d ); } } else if( !allocateMem ) memset( buffer, 0, size * get_explicit_type_size( type ) ); glGenTextures( 1, outTextureID ); glBindTexture( target, *outTextureID ); glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE ); glTexParameteri( target, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexParameteri( target, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); glGetError(); glTexImage3D( target, 0, internalFormat, (GLsizei)width, (GLsizei)height, (GLsizei)depth, 0, glFormat, glType, buffer ); GLenum err = glGetError(); if( err != GL_NO_ERROR ) { if (err != GL_OUT_OF_MEMORY) { log_error( "ERROR: Unable to load data into GL texture (%s) format %s type %s internal format %s\n", gluErrorString( err ), GetGLFormatName( glFormat ), get_explicit_type_name( type ), GetGLFormatName( internalFormat ) ); *outError = -1; } else { log_info( "WARNING: Unable to load data into GL texture (%s) format %s type %s internal format %s\n", gluErrorString( err ), GetGLFormatName( glFormat ), get_explicit_type_name( type ), GetGLFormatName( internalFormat ) ); *outError = -2; } delete [] buffer; return NULL; } if( !allocateMem ) { delete [] buffer; return NULL; } if( glType == GL_UNSIGNED_INT_8_8_8_8_REV && glFormat == GL_BGRA && allocateMem ) { // Reverse and reorder to validate since in the // kernel the read_imagef() call always returns RGBA cl_uchar *p = (cl_uchar *)buffer; for( size_t i = 0; i < width * height * depth; i++ ) { cl_uchar uc0 = p[i * 4 + 0]; cl_uchar uc1 = p[i * 4 + 1]; cl_uchar uc2 = p[i * 4 + 2]; cl_uchar uc3 = p[i * 4 + 3]; p[ i * 4 + 0 ] = uc2; p[ i * 4 + 1 ] = uc1; p[ i * 4 + 2 ] = uc0; p[ i * 4 + 3 ] = uc3; } } else if( glType == GL_UNSIGNED_INT_8_8_8_8 && glFormat == GL_BGRA && allocateMem ) { // Reverse and reorder to validate since in the // kernel the read_imagef() call always returns RGBA cl_uchar *p = (cl_uchar *)buffer; for( size_t i = 0; i < width * height * depth; i++ ) { cl_uchar uc0 = p[i * 4 + 0]; cl_uchar uc1 = p[i * 4 + 1]; cl_uchar uc2 = p[i * 4 + 2]; cl_uchar uc3 = p[i * 4 + 3]; p[ i * 4 + 0 ] = uc1; p[ i * 4 + 1 ] = uc2; p[ i * 4 + 2 ] = uc3; p[ i * 4 + 3 ] = uc0; } } return buffer; } void * ReadGLTexture( GLenum glTarget, GLuint glTexture, GLuint glBuf, GLint width, GLenum glFormat, GLenum glInternalFormat, GLenum glType, ExplicitType typeToReadAs, size_t outWidth, size_t outHeight ) { // Read results from the GL texture glBindTexture(get_base_gl_target(glTarget), glTexture); GLint realWidth, realHeight, realDepth; glGetTexLevelParameteriv( glTarget, 0, GL_TEXTURE_WIDTH, &realWidth ); glGetTexLevelParameteriv( glTarget, 0, GL_TEXTURE_HEIGHT, &realHeight ); glGetTexLevelParameteriv( glTarget, 0, GL_TEXTURE_DEPTH, &realDepth ); realDepth = (realDepth) ? realDepth : 1; GLint realInternalFormat; glGetTexLevelParameteriv( glTarget, 0, GL_TEXTURE_INTERNAL_FORMAT, &realInternalFormat ); #ifdef DEBUG log_info( "- Reading back from GL: %d x %d : %s : %s : %s : %s (stored as %s)\n", realWidth, realHeight, GetGLTargetName( glTarget), GetGLFormatName( glInternalFormat ), GetGLFormatName( glFormat ), GetGLTypeName( glType ), GetGLFormatName( realInternalFormat )); #endif GLenum readBackFormat; switch(glFormat) { case GL_RGBA_INTEGER_EXT: readBackFormat = GL_RGBA_INTEGER_EXT; break; case GL_DEPTH_COMPONENT: readBackFormat = GL_DEPTH_COMPONENT; break; case GL_DEPTH_STENCIL: readBackFormat = GL_DEPTH_STENCIL; break; default: readBackFormat = GL_RGBA; break; } GLenum readBackType; switch (glType) { #ifdef __APPLE__ case GL_UNSIGNED_INT_8_8_8_8: case GL_UNSIGNED_INT_8_8_8_8_REV: readBackType = GL_UNSIGNED_BYTE; break; #endif case GL_HALF_FLOAT: case GL_UNSIGNED_BYTE: case GL_UNSIGNED_SHORT: case GL_UNSIGNED_INT: case GL_BYTE: case GL_SHORT: case GL_INT: case GL_FLOAT: default: readBackType = glType; } size_t outBytes; if (get_base_gl_target(glTarget) != GL_TEXTURE_BUFFER) { outBytes = realWidth * realHeight * realDepth * 4 * GetGLTypeSize(readBackType); } else { outBytes = width * 4; outBytes *= ( GetGLTypeSize( GetGLTypeForExplicitType(typeToReadAs) ) ); } cl_char *outBuffer = (cl_char *)malloc( outBytes ); memset(outBuffer, 0, outBytes); if (get_base_gl_target(glTarget) != GL_TEXTURE_BUFFER) { //the default alignment in OpenGL is 4 bytes and need to be changed for GL_DEPTH_COMPONENT16 which is aligned to 2 bytes if (realInternalFormat == GL_DEPTH_COMPONENT16) glPixelStorei(GL_PACK_ALIGNMENT, 2); glGetTexImage( glTarget, 0, readBackFormat, readBackType, outBuffer ); if (realInternalFormat == GL_DEPTH_COMPONENT16) glPixelStorei(GL_PACK_ALIGNMENT, 4); } else { glBindBuffer(GL_ARRAY_BUFFER, glBuf); glGetBufferSubData(GL_ARRAY_BUFFER, 0, outBytes, outBuffer); } #ifdef DEBUG log_info( "- glGetTexImage: %s : %s : %s \n", GetGLTargetName( glTarget), GetGLFormatName(readBackFormat), GetGLTypeName(readBackType)); DumpGLBuffer(readBackType, realWidth, realHeight, (void*)outBuffer); #endif return (void *)outBuffer; } int CreateGLRenderbufferRaw( GLsizei width, GLsizei height, GLenum attachment, GLenum glFormat, GLenum internalFormat, GLenum glType, GLuint *outFramebuffer, GLuint *outRenderbuffer ) { GLenum err = 0; // Generate a renderbuffer and bind glGenRenderbuffersEXT( 1, outRenderbuffer ); glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, *outRenderbuffer ); // Allocate storage to the renderbuffer glGetError(); glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, internalFormat, (GLsizei)width, (GLsizei)height ); err = glGetError(); if( err != GL_NO_ERROR ) { log_error("Failed to allocate render buffer storage!\n"); return 1701; } GLint realInternalFormat; glGetRenderbufferParameterivEXT( GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_INTERNAL_FORMAT_EXT, &realInternalFormat ); internalFormat = realInternalFormat; #ifdef DEBUG GLint rsize, gsize, bsize, asize; glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_RED_SIZE_EXT,&rsize); glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_GREEN_SIZE_EXT,&gsize); glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_BLUE_SIZE_EXT,&bsize); glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_ALPHA_SIZE_EXT,&asize); log_info("Renderbuffer internal format requested: %s actual: %s sizes: r=%d g=%d b=%d a=%d\n", GetGLFormatName( internalFormat ), GetGLFormatName( realInternalFormat ), rsize, gsize, bsize, asize ); #endif // Create and bind a framebuffer to render with glGenFramebuffersEXT( 1, outFramebuffer ); glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, *outFramebuffer ); if( err != GL_NO_ERROR ) { log_error( "ERROR: Unable to bind framebuffer : Error %s\n", gluErrorString( err )); return -1; } // Attach to the framebuffer glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, attachment, GL_RENDERBUFFER_EXT, *outRenderbuffer ); err = glGetError(); GLint status = glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT ); if( status != GL_FRAMEBUFFER_COMPLETE_EXT ) { log_error( "ERROR: Unable to attach renderbuffer to framebuffer (%s, status %x)\n", gluErrorString( err ), (int)status ); return -1; } return 0; } void reorder_verification_buffer(GLenum glFormat, GLenum glType, char* buffer, size_t num_pixels) { if( glType == GL_UNSIGNED_INT_8_8_8_8_REV && glFormat == GL_BGRA) { // Reverse and reorder to validate since in the // kernel the read_imagef() call always returns RGBA cl_uchar *p = (cl_uchar *)buffer; for( size_t i = 0; i < num_pixels; i++ ) { cl_uchar uc0 = p[i * 4 + 0]; cl_uchar uc1 = p[i * 4 + 1]; cl_uchar uc2 = p[i * 4 + 2]; cl_uchar uc3 = p[i * 4 + 3]; p[ i * 4 + 0 ] = uc2; p[ i * 4 + 1 ] = uc1; p[ i * 4 + 2 ] = uc0; p[ i * 4 + 3 ] = uc3; } } else if( glType == GL_UNSIGNED_INT_8_8_8_8 && glFormat == GL_BGRA) { // Reverse and reorder to validate since in the // kernel the read_imagef() call always returns RGBA cl_uchar *p = (cl_uchar *)buffer; for( size_t i = 0; i < num_pixels; i++ ) { cl_uchar uc0 = p[i * 4 + 0]; cl_uchar uc1 = p[i * 4 + 1]; cl_uchar uc2 = p[i * 4 + 2]; cl_uchar uc3 = p[i * 4 + 3]; p[ i * 4 + 0 ] = uc1; p[ i * 4 + 1 ] = uc2; p[ i * 4 + 2 ] = uc3; p[ i * 4 + 3 ] = uc0; } } } #ifdef GL_VERSION_3_2 #define check_gl_error() \ { \ GLenum errnom = GL_NO_ERROR;\ if ((errnom = glGetError()) != GL_NO_ERROR)\ log_error("GL Error: 0x%04X at %s:%d\n", errnom, __FILE__, __LINE__);\ } const char *get_gl_vector_type( GLenum internalformat ) { switch (internalformat) { case GL_RGBA8: case GL_RGBA16: case GL_RGBA32F_ARB: case GL_RGBA16F_ARB: case GL_DEPTH_COMPONENT16: case GL_DEPTH_COMPONENT32F: case GL_DEPTH24_STENCIL8: case GL_DEPTH32F_STENCIL8: return "vec4"; break; case GL_RGBA8I_EXT: case GL_RGBA16I_EXT: case GL_RGBA32I_EXT: return "ivec4"; break; case GL_RGBA8UI_EXT: case GL_RGBA16UI_EXT: case GL_RGBA32UI_EXT: return "uvec4"; break; default: log_error("Test error: unsupported data type\n"); return ""; break; } } const char *get_gl_data_type( GLenum internalformat ) { switch (internalformat) { case GL_RGBA8: case GL_RGBA16: case GL_RGBA32F_ARB: case GL_RGBA16F_ARB: case GL_DEPTH_COMPONENT16: case GL_DEPTH_COMPONENT32F: case GL_DEPTH24_STENCIL8: case GL_DEPTH32F_STENCIL8: return "float"; break; case GL_RGBA8I_EXT: case GL_RGBA16I_EXT: case GL_RGBA32I_EXT: return "int"; break; case GL_RGBA8UI_EXT: case GL_RGBA16UI_EXT: case GL_RGBA32UI_EXT: return "uint"; break; default: log_error("Test error: unsupported data type\n"); return ""; break; } } void * CreateGLTexture2DMultisample( size_t width, size_t height, size_t samples, GLenum target, GLenum glFormat, GLenum internalFormat, GLenum glType, ExplicitType type, GLuint *outTextureID, int *outError, bool allocateMem, MTdata d , bool fixedSampleLocations) { // This function creates a multisample texture and renders into each sample // using a GLSL shader // Check if the renderer supports enough samples GLint max_samples = get_gl_max_samples(target, internalFormat); check_gl_error() if (max_samples < (GLint)samples) log_error("GL error: requested samples (%d) exceeds renderer max samples (%d)\n", samples, max_samples); // Setup the GLSL program const GLchar *vertex_source = "#version 140\n" "in vec4 att0;\n" "void main (void) {\n" " gl_Position = vec4(att0.xy,0.0,1.0);\n" "}\n"; const GLchar *fragmentSource = "#version 140\n" "out %s out0;\n" "uniform %s colorVal;\n" "uniform float depthVal;\n" "void main (void) {\n" " out0 = %s(colorVal);\n" " gl_FragDepth = depthVal;\n" "}\n"; GLchar fragmentShader[256]; sprintf(fragmentShader, fragmentSource, get_gl_vector_type(internalFormat), get_gl_data_type(internalFormat), get_gl_vector_type(internalFormat)); const GLchar *fragment_source = &fragmentShader[0]; glShaderWrapper vertex_shader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex_shader, 1, &vertex_source, NULL); glCompileShader(vertex_shader); check_gl_error() glShaderWrapper fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment_shader, 1, &fragment_source, NULL); glCompileShader(fragment_shader); check_gl_error() GLuint prog = glCreateProgram(); glAttachShader(prog, vertex_shader); glAttachShader(prog, fragment_shader); check_gl_error() glBindAttribLocation(prog, 0, "att0"); glLinkProgram(prog); check_gl_error() // Setup the FBO and texture glFramebufferWrapper fbo; glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); check_gl_error() glViewport(0, 0, width, height); check_gl_error() GLuint tex = 0; glGenTextures(1, &tex); glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, tex); glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, internalFormat, width, height, fixedSampleLocations); check_gl_error() GLint attachment; switch (internalFormat) { case GL_DEPTH_COMPONENT16: case GL_DEPTH_COMPONENT32F: attachment = GL_DEPTH_ATTACHMENT; break; case GL_DEPTH24_STENCIL8: case GL_DEPTH32F_STENCIL8: attachment = GL_DEPTH_STENCIL_ATTACHMENT; break; default: attachment = GL_COLOR_ATTACHMENT0; break; } glFramebufferTexture(GL_FRAMEBUFFER, attachment, tex, 0); check_gl_error() GLint status = glCheckFramebufferStatus(GL_FRAMEBUFFER); if (status == GL_FRAMEBUFFER_UNSUPPORTED) { log_info("GL status: GL_FRAMEBUFFER_UNSUPPORTED format %s multisample is not supported\n", GetGLFormatName(internalFormat)); *outTextureID = 0; *outError = GL_FRAMEBUFFER_UNSUPPORTED; return NULL; } if (status != GL_FRAMEBUFFER_COMPLETE) { log_error("GL error: framebuffer incomplete status 0x%04X\n",status); *outTextureID = 0; *outError = status; return NULL; } // Check if the framebuffer supports enough samples GLint fbo_samples = 0; glGetIntegerv(GL_SAMPLES, &fbo_samples); check_gl_error(); if (fbo_samples < (GLint)samples) log_error("GL Error: requested samples (%d) exceeds FBO capability (%d)\n", samples, fbo_samples); glUseProgram(prog); check_gl_error() if (attachment != GL_DEPTH_ATTACHMENT && attachment != GL_DEPTH_STENCIL_ATTACHMENT) { glDisable(GL_DEPTH_TEST); check_gl_error() } else { glEnable(GL_DEPTH_TEST); glDepthFunc(GL_ALWAYS); check_gl_error() } // Setup the VBO for rendering a quad GLfloat quad[] = { -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f }; glBufferWrapper vbo; glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STREAM_DRAW); check_gl_error() glVertexArraysWrapper vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*2, 0); check_gl_error() //clearing color and depth buffer glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT); glClearDepth(1.0); glClear(GL_DEPTH_BUFFER_BIT); //calculating colors double color_delta = 1.0 / samples; double color = color_delta; glEnable(GL_SAMPLE_MASK); for (size_t i=0;i!=samples;++i) { glSampleMaski(0, 1< GLboolean gluCheckExtension(const GLubyte *extName, const GLubyte *extString) { const size_t len = strlen((const char*)extName); const char* str = (const char*)extString; while (str != NULL) { str = strstr(str, (const char*)extName); if (str == NULL) { break; } if ((str > (const char*)extString || str[-1] == ' ') && (str[len] == ' ' || str[len] == '\0')) { return GL_TRUE; } str = strchr(str + len, ' '); } return GL_FALSE; } #endif // Function pointers for the GL/CL calls clCreateFromGLBuffer_fn clCreateFromGLBuffer_ptr; clCreateFromGLTexture_fn clCreateFromGLTexture_ptr; clCreateFromGLTexture2D_fn clCreateFromGLTexture2D_ptr; clCreateFromGLTexture3D_fn clCreateFromGLTexture3D_ptr; clCreateFromGLRenderbuffer_fn clCreateFromGLRenderbuffer_ptr; clGetGLObjectInfo_fn clGetGLObjectInfo_ptr; clGetGLTextureInfo_fn clGetGLTextureInfo_ptr; clEnqueueAcquireGLObjects_fn clEnqueueAcquireGLObjects_ptr; clEnqueueReleaseGLObjects_fn clEnqueueReleaseGLObjects_ptr; int init_clgl_ext() { // As OpenCL for the platforms. Warn if more than one platform found, // since this might not be the platform we want. By default, we simply // use the first returned platform. cl_uint nplatforms; cl_platform_id platform; clGetPlatformIDs(0, NULL, &nplatforms); clGetPlatformIDs(1, &platform, NULL); if (nplatforms > 1) { log_info("clGetPlatformIDs returned multiple values. This is not " "an error, but might result in obtaining incorrect function " "pointers if you do not want the first returned platform.\n"); // Show them the platform name, in case it is a problem. size_t size; char *name; clGetPlatformInfo(platform, CL_PLATFORM_NAME, 0, NULL, &size); name = (char*)malloc(size); clGetPlatformInfo(platform, CL_PLATFORM_NAME, size, name, NULL); log_info("Using platform with name: %s \n", name); free(name); } // Create the function pointer table clCreateFromGLBuffer_ptr = (clCreateFromGLBuffer_fn)clGetExtensionFunctionAddressForPlatform(platform,"clCreateFromGLBuffer"); if (clCreateFromGLBuffer_ptr == NULL) { log_error("clGetExtensionFunctionAddressForPlatform(platform,clCreateFromGLBuffer) returned NULL.\n"); return -1; } clCreateFromGLTexture2D_ptr = (clCreateFromGLTexture2D_fn)clGetExtensionFunctionAddressForPlatform(platform,"clCreateFromGLTexture2D"); if (clCreateFromGLTexture2D_ptr == NULL) { log_error("clGetExtensionFunctionAddressForPlatform(platform,clCreateFromGLTexture2D) returned NULL.\n"); return -1; } clCreateFromGLTexture3D_ptr = (clCreateFromGLTexture3D_fn)clGetExtensionFunctionAddressForPlatform(platform,"clCreateFromGLTexture3D"); if (clCreateFromGLTexture3D_ptr == NULL) { log_error("clGetExtensionFunctionAddressForPlatform(platform,clCreateFromGLTexture3D\") returned NULL.\n"); return -1; } clCreateFromGLTexture_ptr = (clCreateFromGLTexture_fn)clGetExtensionFunctionAddressForPlatform(platform,"clCreateFromGLTexture"); if (clCreateFromGLTexture_ptr == NULL) { log_error("clGetExtensionFunctionAddressForPlatform(platform,\"clCreateFromGLTexture\") returned NULL.\n"); return -1; } clCreateFromGLRenderbuffer_ptr = (clCreateFromGLRenderbuffer_fn)clGetExtensionFunctionAddressForPlatform(platform,"clCreateFromGLRenderbuffer"); if (clCreateFromGLRenderbuffer_ptr == NULL) { log_error("clGetExtensionFunctionAddressForPlatform(platform,clCreateFromGLRenderbuffer) returned NULL.\n"); return -1; } clGetGLObjectInfo_ptr = (clGetGLObjectInfo_fn)clGetExtensionFunctionAddressForPlatform(platform,"clGetGLObjectInfo"); if (clGetGLObjectInfo_ptr == NULL) { log_error("clGetExtensionFunctionAddressForPlatform(platform,clGetGLObjectInfo) returned NULL.\n"); return -1; } clGetGLTextureInfo_ptr = (clGetGLTextureInfo_fn)clGetExtensionFunctionAddressForPlatform(platform,"clGetGLTextureInfo"); if (clGetGLTextureInfo_ptr == NULL) { log_error("clGetExtensionFunctionAddressForPlatform(platform,clGetGLTextureInfo) returned NULL.\n"); return -1; } clEnqueueAcquireGLObjects_ptr = (clEnqueueAcquireGLObjects_fn)clGetExtensionFunctionAddressForPlatform(platform,"clEnqueueAcquireGLObjects"); if (clEnqueueAcquireGLObjects_ptr == NULL) { log_error("clGetExtensionFunctionAddressForPlatform(platform,clEnqueueAcquireGLObjects) returned NULL.\n"); return -1; } clEnqueueReleaseGLObjects_ptr = (clEnqueueReleaseGLObjects_fn)clGetExtensionFunctionAddressForPlatform(platform,"clEnqueueReleaseGLObjects"); if (clEnqueueReleaseGLObjects_ptr == NULL) { log_error("clGetExtensionFunctionAddressForPlatform(platform,clEnqueueReleaseGLObjects) returned NULL.\n"); return -1; } return 0; } GLint get_gl_max_samples( GLenum target, GLenum internalformat ) { GLint max_samples = 0; #ifdef GL_VERSION_4_2 glGetInternalformativ(target, internalformat, GL_SAMPLES, 1, &max_samples); #else switch (internalformat) { case GL_RGBA8I: case GL_RGBA16I: case GL_RGBA32I: case GL_RGBA8UI: case GL_RGBA16UI: case GL_RGBA32UI: glGetIntegerv(GL_MAX_INTEGER_SAMPLES, &max_samples); break; case GL_DEPTH_COMPONENT16: case GL_DEPTH_COMPONENT32F: case GL_DEPTH24_STENCIL8: case GL_DEPTH32F_STENCIL8: glGetIntegerv(GL_MAX_DEPTH_TEXTURE_SAMPLES, &max_samples); break; default: glGetIntegerv(GL_MAX_COLOR_TEXTURE_SAMPLES, &max_samples); break; } #endif return max_samples; }