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 <stdio.h> 20 #include <stdlib.h> 21 #include <math.h> 22 #include <string.h> 23 24 #if !defined(_WIN32) 25 #include <stdbool.h> 26 #endif 27 28 #include <sys/types.h> 29 #include <sys/stat.h> 30 31 #if !defined (__APPLE__) 32 #include <CL/cl.h> 33 #include <CL/cl_gl.h> 34 #include <CL/cl_half.h> 35 #endif 36 #include "gl_headers.h" 37 38 #include "harness/errorHelpers.h" 39 #include "harness/kernelHelpers.h" 40 #include "harness/typeWrappers.h" 41 #include "harness/conversions.h" 42 #include "harness/mt19937.h" 43 44 typedef cl_mem 45 (CL_API_CALL *clCreateFromGLBuffer_fn)(cl_context context, 46 cl_mem_flags flags, 47 GLuint bufobj, 48 int * errcode_ret); 49 50 typedef cl_mem 51 (CL_API_CALL *clCreateFromGLTexture_fn)(cl_context context , 52 cl_mem_flags flags , 53 GLenum target , 54 GLint miplevel , 55 GLuint texture , 56 cl_int * errcode_ret) ; 57 58 typedef cl_mem 59 (CL_API_CALL *clCreateFromGLRenderbuffer_fn)(cl_context context , 60 cl_mem_flags flags , 61 GLuint renderbuffer , 62 cl_int * errcode_ret) ; 63 64 typedef cl_int 65 (CL_API_CALL *clGetGLObjectInfo_fn)(cl_mem memobj , 66 cl_gl_object_type * gl_object_type , 67 GLuint * gl_object_name) ; 68 69 typedef cl_int 70 (CL_API_CALL *clGetGLTextureInfo_fn)(cl_mem memobj , 71 cl_gl_texture_info param_name , 72 size_t param_value_size , 73 void * param_value , 74 size_t * param_value_size_ret) ; 75 76 typedef cl_int 77 (CL_API_CALL *clEnqueueAcquireGLObjects_fn)(cl_command_queue command_queue , 78 cl_uint num_objects , 79 const cl_mem * mem_objects , 80 cl_uint num_events_in_wait_list , 81 const cl_event * event_wait_list , 82 cl_event * event) ; 83 84 typedef cl_int 85 (CL_API_CALL *clEnqueueReleaseGLObjects_fn)(cl_command_queue command_queue , 86 cl_uint num_objects , 87 const cl_mem * mem_objects , 88 cl_uint num_events_in_wait_list , 89 const cl_event * event_wait_list , 90 cl_event * event) ; 91 92 93 extern clCreateFromGLBuffer_fn clCreateFromGLBuffer_ptr; 94 extern clCreateFromGLTexture_fn clCreateFromGLTexture_ptr; 95 extern clCreateFromGLRenderbuffer_fn clCreateFromGLRenderbuffer_ptr; 96 extern clGetGLObjectInfo_fn clGetGLObjectInfo_ptr; 97 extern clGetGLTextureInfo_fn clGetGLTextureInfo_ptr; 98 extern clEnqueueAcquireGLObjects_fn clEnqueueAcquireGLObjects_ptr; 99 extern clEnqueueReleaseGLObjects_fn clEnqueueReleaseGLObjects_ptr; 100 101 102 class glBufferWrapper 103 { 104 public: glBufferWrapper()105 glBufferWrapper() { mBuffer = 0; } glBufferWrapper(GLuint b)106 glBufferWrapper( GLuint b ) { mBuffer = b; } ~glBufferWrapper()107 ~glBufferWrapper() { if( mBuffer != 0 ) glDeleteBuffers( 1, &mBuffer ); } 108 109 glBufferWrapper & operator=( const GLuint &rhs ) { mBuffer = rhs; return *this; } GLuint()110 operator GLuint() { return mBuffer; } 111 operator GLuint *() { return &mBuffer; } 112 113 GLuint * operator&() { return &mBuffer; } 114 115 bool operator==( GLuint rhs ) { return mBuffer == rhs; } 116 117 protected: 118 119 GLuint mBuffer; 120 }; 121 122 class glTextureWrapper 123 { 124 public: glTextureWrapper()125 glTextureWrapper() { mBuffer = 0; } glTextureWrapper(GLuint b)126 glTextureWrapper( GLuint b ) { mBuffer = b; } ~glTextureWrapper()127 ~glTextureWrapper() { if( mBuffer != 0 ) glDeleteTextures( 1, &mBuffer ); } 128 129 glTextureWrapper & operator=( const GLuint &rhs ) { mBuffer = rhs; return *this; } GLuint()130 operator GLuint() { return mBuffer; } 131 operator GLuint *() { return &mBuffer; } 132 133 GLuint * operator&() { return &mBuffer; } 134 135 bool operator==( GLuint rhs ) { return mBuffer == rhs; } 136 137 protected: 138 139 GLuint mBuffer; 140 }; 141 142 class glRenderbufferWrapper 143 { 144 public: glRenderbufferWrapper()145 glRenderbufferWrapper() { mBuffer = 0; } glRenderbufferWrapper(GLuint b)146 glRenderbufferWrapper( GLuint b ) { mBuffer = b; } ~glRenderbufferWrapper()147 ~glRenderbufferWrapper() { if( mBuffer != 0 ) glDeleteRenderbuffersEXT( 1, &mBuffer ); } 148 149 glRenderbufferWrapper & operator=( const GLuint &rhs ) { mBuffer = rhs; return *this; } GLuint()150 operator GLuint() { return mBuffer; } 151 operator GLuint *() { return &mBuffer; } 152 153 GLuint * operator&() { return &mBuffer; } 154 155 bool operator==( GLuint rhs ) { return mBuffer == rhs; } 156 157 protected: 158 159 GLuint mBuffer; 160 }; 161 162 class glFramebufferWrapper 163 { 164 public: glFramebufferWrapper()165 glFramebufferWrapper() { mBuffer = 0; } glFramebufferWrapper(GLuint b)166 glFramebufferWrapper( GLuint b ) { mBuffer = b; } ~glFramebufferWrapper()167 ~glFramebufferWrapper() { if( mBuffer != 0 ) glDeleteFramebuffersEXT( 1, &mBuffer ); } 168 169 glFramebufferWrapper & operator=( const GLuint &rhs ) { mBuffer = rhs; return *this; } GLuint()170 operator GLuint() { return mBuffer; } 171 operator GLuint *() { return &mBuffer; } 172 173 GLuint * operator&() { return &mBuffer; } 174 175 bool operator==( GLuint rhs ) { return mBuffer == rhs; } 176 177 protected: 178 179 GLuint mBuffer; 180 }; 181 182 183 // Helper functions (defined in helpers.cpp) 184 extern void * CreateGLTexture2D( size_t width, size_t height, 185 GLenum target, GLenum glFormat, 186 GLenum internalFormat, GLenum glType, 187 ExplicitType type, GLuint *outTextureID, 188 int *outError, bool allocateMem, MTdata d ); 189 190 191 extern void * CreateGLTexture3D( size_t width, size_t height, size_t depth, 192 GLenum target, GLenum glFormat, 193 GLenum internalFormat, GLenum glType, 194 ExplicitType type, GLuint *outTextureID, 195 int *outError, MTdata d, bool allocateMem = true ); 196 197 extern void * ReadGLTexture( GLenum glTarget, GLuint glTexture, 198 GLenum glFormat, GLenum glInternalFormat, 199 GLenum glType, ExplicitType typeToReadAs, 200 size_t outWidth, size_t outHeight ); 201 202 void * CreateGLRenderbuffer( GLsizei width, GLsizei height, 203 GLenum attachment, 204 GLenum rbFormat, GLenum rbType, 205 GLenum texFormat, GLenum texType, 206 ExplicitType type, 207 GLuint *outFramebuffer, 208 GLuint *outRenderbuffer, 209 int *outError, MTdata d, bool allocateMem ); 210 211 int CreateGLRenderbufferRaw( GLsizei width, GLsizei height, 212 GLenum attachment, 213 GLenum rbFormat, GLenum rbType, 214 GLuint *outFramebuffer, 215 GLuint *outRenderbuffer ); 216 217 void * ReadGLRenderbuffer( GLuint glFramebuffer, GLuint glRenderbuffer, 218 GLenum attachment, 219 GLenum rbFormat, GLenum rbType, 220 GLenum texFormat, GLenum texType, 221 ExplicitType typeToReadAs, 222 size_t outWidth, size_t outHeight ); 223 224 extern void DumpGLBuffer(GLenum type, size_t width, size_t height, void* buffer); 225 extern const char *GetGLTypeName( GLenum type ); 226 extern const char *GetGLAttachmentName( GLenum att ); 227 extern const char *GetGLTargetName( GLenum tgt ); 228 extern const char *GetGLBaseFormatName( GLenum baseformat ); 229 extern const char *GetGLFormatName( GLenum format ); 230 231 extern void* CreateRandomData( ExplicitType type, size_t count, MTdata d ); 232 233 extern GLenum GetGLFormat(GLenum internalFormat); 234 extern GLenum GetGLTypeForExplicitType(ExplicitType type); 235 extern size_t GetGLTypeSize(GLenum type); 236 extern ExplicitType GetExplicitTypeForGLType(GLenum type); 237 238 extern GLenum get_base_gl_target( GLenum target ); 239 240 extern int init_clgl_ext( cl_platform_id platform_id ); 241 242 #endif // _helpers_h 243 244 245 246