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 #ifndef _helpers_h 17 #define _helpers_h 18 19 #include "../harness/compat.h" 20 #include <stdio.h> 21 #include <stdlib.h> 22 #include <string.h> 23 24 #include <sys/types.h> 25 #include <sys/stat.h> 26 27 #if !defined (__APPLE__) 28 #include <CL/cl.h> 29 #include "gl_headers.h" 30 #include <CL/cl_gl.h> 31 #else 32 #include "gl_headers.h" 33 #endif 34 35 #include "harness/errorHelpers.h" 36 #include "harness/kernelHelpers.h" 37 #include "harness/threadTesting.h" 38 #include "harness/typeWrappers.h" 39 #include "harness/conversions.h" 40 #include "harness/mt19937.h" 41 42 typedef cl_mem 43 (CL_API_CALL *clCreateFromGLBuffer_fn)(cl_context context, 44 cl_mem_flags flags, 45 GLuint bufobj, 46 int * errcode_ret); 47 48 typedef cl_mem 49 (CL_API_CALL *clCreateFromGLTexture_fn)(cl_context context , 50 cl_mem_flags flags , 51 GLenum target , 52 GLint miplevel , 53 GLuint texture , 54 cl_int * errcode_ret) ; 55 56 typedef cl_mem 57 (CL_API_CALL *clCreateFromGLTexture2D_fn)(cl_context context , 58 cl_mem_flags flags , 59 GLenum target , 60 GLint miplevel , 61 GLuint texture , 62 cl_int * errcode_ret) ; 63 64 typedef cl_mem 65 (CL_API_CALL *clCreateFromGLTexture3D_fn)(cl_context context , 66 cl_mem_flags flags , 67 GLenum target , 68 GLint miplevel , 69 GLuint texture , 70 cl_int * errcode_ret) ; 71 72 typedef cl_mem 73 (CL_API_CALL *clCreateFromGLRenderbuffer_fn)(cl_context context , 74 cl_mem_flags flags , 75 GLuint renderbuffer , 76 cl_int * errcode_ret) ; 77 78 typedef cl_int 79 (CL_API_CALL *clGetGLObjectInfo_fn)(cl_mem memobj , 80 cl_gl_object_type * gl_object_type , 81 GLuint * gl_object_name) ; 82 83 typedef cl_int 84 (CL_API_CALL *clGetGLTextureInfo_fn)(cl_mem memobj , 85 cl_gl_texture_info param_name , 86 size_t param_value_size , 87 void * param_value , 88 size_t * param_value_size_ret) ; 89 90 typedef cl_int 91 (CL_API_CALL *clEnqueueAcquireGLObjects_fn)(cl_command_queue command_queue , 92 cl_uint num_objects , 93 const cl_mem * mem_objects , 94 cl_uint num_events_in_wait_list , 95 const cl_event * event_wait_list , 96 cl_event * event) ; 97 98 typedef cl_int 99 (CL_API_CALL *clEnqueueReleaseGLObjects_fn)(cl_command_queue command_queue , 100 cl_uint num_objects , 101 const cl_mem * mem_objects , 102 cl_uint num_events_in_wait_list , 103 const cl_event * event_wait_list , 104 cl_event * event) ; 105 106 107 extern clCreateFromGLBuffer_fn clCreateFromGLBuffer_ptr; 108 extern clCreateFromGLTexture_fn clCreateFromGLTexture_ptr; 109 extern clCreateFromGLTexture2D_fn clCreateFromGLTexture2D_ptr; 110 extern clCreateFromGLTexture3D_fn clCreateFromGLTexture3D_ptr; 111 extern clCreateFromGLRenderbuffer_fn clCreateFromGLRenderbuffer_ptr; 112 extern clGetGLObjectInfo_fn clGetGLObjectInfo_ptr; 113 extern clGetGLTextureInfo_fn clGetGLTextureInfo_ptr; 114 extern clEnqueueAcquireGLObjects_fn clEnqueueAcquireGLObjects_ptr; 115 extern clEnqueueReleaseGLObjects_fn clEnqueueReleaseGLObjects_ptr; 116 117 118 class glBufferWrapper 119 { 120 public: glBufferWrapper()121 glBufferWrapper() { mBuffer = 0; } glBufferWrapper(GLuint b)122 glBufferWrapper( GLuint b ) { mBuffer = b; } ~glBufferWrapper()123 ~glBufferWrapper() { if( mBuffer != 0 ) glDeleteBuffers( 1, &mBuffer ); } 124 125 glBufferWrapper & operator=( const GLuint &rhs ) { mBuffer = rhs; return *this; } GLuint()126 operator GLuint() { return mBuffer; } 127 operator GLuint *() { return &mBuffer; } 128 129 GLuint * operator&() { return &mBuffer; } 130 131 bool operator==( GLuint rhs ) { return mBuffer == rhs; } 132 133 protected: 134 135 GLuint mBuffer; 136 }; 137 138 class glTextureWrapper 139 { 140 public: glTextureWrapper()141 glTextureWrapper() { mHandle = 0; } glTextureWrapper(GLuint b)142 glTextureWrapper( GLuint b ) { mHandle = b; } ~glTextureWrapper()143 ~glTextureWrapper() { 144 if( mHandle != 0 ) glDeleteTextures( 1, &mHandle ); 145 } 146 147 glTextureWrapper & operator=( const GLuint &rhs ) { mHandle = rhs; return *this; } GLuint()148 operator GLuint() { return mHandle; } 149 operator GLuint *() { return &mHandle; } 150 151 GLuint * operator&() { return &mHandle; } 152 153 bool operator==( GLuint rhs ) { return mHandle == rhs; } 154 155 protected: 156 157 // The texture handle. 158 GLuint mHandle; 159 }; 160 161 class glRenderbufferWrapper 162 { 163 public: glRenderbufferWrapper()164 glRenderbufferWrapper() { mBuffer = 0; } glRenderbufferWrapper(GLuint b)165 glRenderbufferWrapper( GLuint b ) { mBuffer = b; } ~glRenderbufferWrapper()166 ~glRenderbufferWrapper() { if( mBuffer != 0 ) glDeleteRenderbuffersEXT( 1, &mBuffer ); } 167 168 glRenderbufferWrapper & operator=( const GLuint &rhs ) { mBuffer = rhs; return *this; } GLuint()169 operator GLuint() { return mBuffer; } 170 operator GLuint *() { return &mBuffer; } 171 172 GLuint * operator&() { return &mBuffer; } 173 174 bool operator==( GLuint rhs ) { return mBuffer == rhs; } 175 176 protected: 177 178 GLuint mBuffer; 179 }; 180 181 class glFramebufferWrapper 182 { 183 public: glFramebufferWrapper()184 glFramebufferWrapper() { mBuffer = 0; } glFramebufferWrapper(GLuint b)185 glFramebufferWrapper( GLuint b ) { mBuffer = b; } ~glFramebufferWrapper()186 ~glFramebufferWrapper() { if( mBuffer != 0 ) glDeleteFramebuffersEXT( 1, &mBuffer ); } 187 188 glFramebufferWrapper & operator=( const GLuint &rhs ) { mBuffer = rhs; return *this; } GLuint()189 operator GLuint() { return mBuffer; } 190 operator GLuint *() { return &mBuffer; } 191 192 GLuint * operator&() { return &mBuffer; } 193 194 bool operator==( GLuint rhs ) { return mBuffer == rhs; } 195 196 protected: 197 198 GLuint mBuffer; 199 }; 200 201 class glVertexArraysWrapper 202 { 203 public: glVertexArraysWrapper()204 glVertexArraysWrapper() { mBuffer = 0; } glVertexArraysWrapper(GLuint b)205 glVertexArraysWrapper( GLuint b ) { mBuffer = b; } ~glVertexArraysWrapper()206 ~glVertexArraysWrapper() { if( mBuffer != 0 ) glDeleteVertexArrays( 1, &mBuffer ); } 207 208 glVertexArraysWrapper & operator=( const GLuint &rhs ) { mBuffer = rhs; return *this; } GLuint()209 operator GLuint() { return mBuffer; } 210 operator GLuint *() { return &mBuffer; } 211 212 GLuint * operator&() { return &mBuffer; } 213 214 bool operator==( GLuint rhs ) { return mBuffer == rhs; } 215 216 protected: 217 218 GLuint mBuffer; 219 }; 220 221 class glProgramWrapper 222 { 223 public: glProgramWrapper()224 glProgramWrapper() { mProgram = 0; } glProgramWrapper(GLuint b)225 glProgramWrapper( GLuint b ) { mProgram = b; } ~glProgramWrapper()226 ~glProgramWrapper() { if( mProgram != 0 ) glDeleteProgram( mProgram ); } 227 228 glProgramWrapper & operator=( const GLuint &rhs ) { mProgram = rhs; return *this; } GLuint()229 operator GLuint() { return mProgram; } 230 operator GLuint *() { return &mProgram; } 231 232 GLuint * operator&() { return &mProgram; } 233 234 bool operator==( GLuint rhs ) { return mProgram == rhs; } 235 236 protected: 237 238 GLuint mProgram; 239 }; 240 241 class glShaderWrapper 242 { 243 public: glShaderWrapper()244 glShaderWrapper() { mShader = 0; } glShaderWrapper(GLuint b)245 glShaderWrapper( GLuint b ) { mShader = b; } ~glShaderWrapper()246 ~glShaderWrapper() { if( mShader != 0 ) glDeleteShader( mShader ); } 247 248 glShaderWrapper & operator=( const GLuint &rhs ) { mShader = rhs; return *this; } GLuint()249 operator GLuint() { return mShader; } 250 operator GLuint *() { return &mShader; } 251 252 GLuint * operator&() { return &mShader; } 253 254 bool operator==( GLuint rhs ) { return mShader == rhs; } 255 256 protected: 257 258 GLuint mShader; 259 }; 260 261 // Helper functions (defined in helpers.cpp) 262 263 extern void * CreateGLTexture1DArray( size_t width, size_t length, 264 GLenum target, GLenum glFormat, GLenum internalFormat, GLenum glType, 265 ExplicitType type, GLuint *outTextureID, int *outError, 266 bool allocateMem, MTdata d); 267 268 extern void * CreateGLTexture2DArray( size_t width, size_t height, size_t length, 269 GLenum target, GLenum glFormat, GLenum internalFormat, GLenum glType, 270 ExplicitType type, GLuint *outTextureID, int *outError, 271 bool allocateMem, MTdata d); 272 273 extern void * CreateGLTextureBuffer( size_t width, 274 GLenum target, GLenum glFormat, GLenum internalFormat, GLenum glType, 275 ExplicitType type, GLuint *outTex, GLuint *outBuf, int *outError, 276 bool allocateMem, MTdata d); 277 278 extern void * CreateGLTexture1D(size_t width, 279 GLenum target, GLenum glFormat, 280 GLenum internalFormat, GLenum glType, 281 ExplicitType type, GLuint *outTextureID, 282 int *outError, bool allocateMem, MTdata d ); 283 284 extern void * CreateGLTexture2D( size_t width, size_t height, 285 GLenum target, GLenum glFormat, 286 GLenum internalFormat, GLenum glType, 287 ExplicitType type, GLuint *outTextureID, 288 int *outError, bool allocateMem, MTdata d ); 289 290 291 extern void * CreateGLTexture3D( size_t width, size_t height, size_t depth, 292 GLenum target, GLenum glFormat, 293 GLenum internalFormat, GLenum glType, 294 ExplicitType type, GLuint *outTextureID, 295 int *outError, MTdata d, bool allocateMem = true ); 296 297 #ifdef GL_VERSION_3_2 298 extern void * CreateGLTexture2DMultisample( size_t width, size_t height, size_t samples, 299 GLenum target, GLenum glFormat, 300 GLenum internalFormat, GLenum glType, 301 ExplicitType type, GLuint *outTextureID, 302 int *outError, bool allocateMem, MTdata d, 303 bool fixedSampleLocations ); 304 305 extern void * CreateGLTexture2DArrayMultisample( size_t width, size_t height, 306 size_t length, size_t samples, 307 GLenum target, GLenum glFormat, 308 GLenum internalFormat, GLenum glType, 309 ExplicitType type, GLuint *outTextureID, 310 int *outError, bool allocateMem, MTdata d, 311 bool fixedSampleLocations ); 312 #endif 313 314 extern void * ReadGLTexture( GLenum glTarget, GLuint glTexture, GLuint glBuf, GLint width, 315 GLenum glFormat, GLenum glInternalFormat, 316 GLenum glType, ExplicitType typeToReadAs, 317 size_t outWidth, size_t outHeight ); 318 319 extern int CreateGLRenderbufferRaw( GLsizei width, GLsizei height, 320 GLenum target, GLenum glFormat, 321 GLenum internalFormat, GLenum glType, 322 GLuint *outFramebuffer, 323 GLuint *outRenderbuffer ); 324 325 extern void * CreateGLRenderbuffer( GLsizei width, GLsizei height, 326 GLenum target, GLenum glFormat, 327 GLenum internalFormat, GLenum glType, 328 ExplicitType type, 329 GLuint *outFramebuffer, 330 GLuint *outRenderbuffer, 331 int *outError, MTdata d, bool allocateMem ); 332 333 extern void * ReadGLRenderbuffer( GLuint glFramebuffer, GLuint glRenderbuffer, 334 GLenum attachment, GLenum glFormat, 335 GLenum glInternalFormat, GLenum glType, 336 ExplicitType typeToReadAs, 337 size_t outWidth, size_t outHeight ); 338 339 extern void DumpGLBuffer(GLenum type, size_t width, size_t height, void* buffer); 340 extern const char *GetGLTypeName( GLenum type ); 341 extern const char *GetGLAttachmentName( GLenum att ); 342 extern const char *GetGLTargetName( GLenum tgt ); 343 extern const char *GetGLBaseFormatName( GLenum baseformat ); 344 extern const char *GetGLFormatName( GLenum format ); 345 346 extern void* CreateRandomData( ExplicitType type, size_t count, MTdata d ); 347 348 extern GLenum GetGLFormat(GLenum internalFormat); 349 extern GLenum GetGLTypeForExplicitType(ExplicitType type); 350 extern size_t GetGLTypeSize(GLenum type); 351 extern ExplicitType GetExplicitTypeForGLType(GLenum type); 352 353 extern GLenum get_base_gl_target( GLenum target ); 354 355 extern int init_clgl_ext( void ); 356 357 extern GLint get_gl_max_samples( GLenum target, GLenum internalformat ); 358 359 #endif // _helpers_h 360 361 362 363