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 "testBase.h"
17 #include "helpers.h"
18
19 #include "gl_headers.h"
20
21 extern int test_cl_image_write( cl_context context, cl_command_queue queue, cl_mem clImage,
22 size_t imageWidth, size_t imageHeight, cl_image_format *outFormat,
23 ExplicitType *outType, void **outSourceBuffer, MTdata d );
24
25 extern int test_cl_image_read( cl_context context, cl_command_queue queue, cl_mem clImage,
26 size_t imageWidth, size_t imageHeight, cl_image_format *outFormat,
27 ExplicitType *outType, void **outResultBuffer );
28
test_attach_renderbuffer_read_image(cl_context context,cl_command_queue queue,GLenum glTarget,GLuint glRenderbuffer,size_t imageWidth,size_t imageHeight,cl_image_format * outFormat,ExplicitType * outType,void ** outResultBuffer)29 static int test_attach_renderbuffer_read_image( cl_context context, cl_command_queue queue, GLenum glTarget, GLuint glRenderbuffer,
30 size_t imageWidth, size_t imageHeight, cl_image_format *outFormat, ExplicitType *outType, void **outResultBuffer )
31 {
32 int error;
33
34 // Create a CL image from the supplied GL renderbuffer
35 clMemWrapper image = (*clCreateFromGLRenderbuffer_ptr)( context, CL_MEM_READ_ONLY, glRenderbuffer, &error );
36 if( error != CL_SUCCESS )
37 {
38 print_error( error, "Unable to create CL image from GL renderbuffer" );
39 return error;
40 }
41
42 return test_cl_image_read( context, queue, image, imageWidth, imageHeight, outFormat, outType, outResultBuffer );
43 }
44
test_renderbuffer_read_image(cl_context context,cl_command_queue queue,GLsizei width,GLsizei height,GLenum attachment,GLenum rbFormat,GLenum rbType,GLenum texFormat,GLenum texType,ExplicitType type,MTdata d)45 int test_renderbuffer_read_image( cl_context context, cl_command_queue queue,
46 GLsizei width, GLsizei height, GLenum attachment,
47 GLenum rbFormat, GLenum rbType,
48 GLenum texFormat, GLenum texType,
49 ExplicitType type, MTdata d )
50 {
51 int error;
52
53
54 // Create the GL renderbuffer
55 glFramebufferWrapper glFramebuffer;
56 glRenderbufferWrapper glRenderbuffer;
57 void *tmp = CreateGLRenderbuffer( width, height, attachment, rbFormat, rbType, texFormat, texType,
58 type, &glFramebuffer, &glRenderbuffer, &error, d, true );
59 BufferOwningPtr<char> inputBuffer(tmp);
60 if( error != 0 )
61 {
62 // GL_RGBA_INTEGER_EXT doesn't exist in GLES2. No need to check for it.
63 return error;
64 }
65
66 // Run and get the results
67 cl_image_format clFormat;
68 ExplicitType actualType;
69 char *outBuffer;
70 error = test_attach_renderbuffer_read_image( context, queue, attachment, glRenderbuffer, width, height, &clFormat, &actualType, (void **)&outBuffer );
71 if( error != 0 )
72 return error;
73 BufferOwningPtr<char> actualResults(outBuffer);
74
75 log_info( "- Read [%4d x %4d] : GL renderbuffer : %s : %s : %s => CL Image : %s : %s \n", width, height,
76 GetGLFormatName( rbFormat ), GetGLFormatName( rbFormat ), GetGLTypeName( rbType ),
77 GetChannelOrderName( clFormat.image_channel_order ), GetChannelTypeName( clFormat.image_channel_data_type ));
78
79 #ifdef GLES_DEBUG
80 log_info("- start read GL data -- \n");
81 DumpGLBuffer(glType, width, height, actualResults);
82 log_info("- end read GL data -- \n");
83 #endif
84
85 // We have to convert our input buffer to the returned type, so we can validate.
86 BufferOwningPtr<char> convertedInput(convert_to_expected( inputBuffer, width * height, type, actualType ));
87
88 #ifdef GLES_DEBUG
89 log_info("- start input data -- \n");
90 DumpGLBuffer(GetGLTypeForExplicitType(actualType), width, height, convertedInput);
91 log_info("- end input data -- \n");
92 #endif
93
94 #ifdef GLES_DEBUG
95 log_info("- start converted data -- \n");
96 DumpGLBuffer(GetGLTypeForExplicitType(actualType), width, height, actualResults);
97 log_info("- end converted data -- \n");
98 #endif
99
100 // Now we validate
101 int valid = 0;
102 if(convertedInput) {
103 if( actualType == kFloat )
104 valid = validate_float_results( convertedInput, actualResults, width, height );
105 else
106 valid = validate_integer_results( convertedInput, actualResults, width, height, get_explicit_type_size( actualType ) );
107 }
108
109 return valid;
110 }
111
test_renderbuffer_read(cl_device_id device,cl_context context,cl_command_queue queue,int numElements)112 int test_renderbuffer_read( cl_device_id device, cl_context context, cl_command_queue queue, int numElements )
113 {
114 GLenum attachments[] = { GL_COLOR_ATTACHMENT0_EXT };
115
116 struct {
117 GLenum rbFormat;
118 GLenum rbType;
119 GLenum texFormat;
120 GLenum texType;
121 ExplicitType type;
122
123 } formats[] = {
124 { GL_RGBA8_OES, GL_UNSIGNED_BYTE, GL_RGBA, GL_UNSIGNED_BYTE, kUChar },
125 //{ GL_RGBA16F_QCOM, GL_HALF_FLOAT_OES, GL_RGBA, GL_HALF_FLOAT_OES, kHalf }, // Half-float not supported by ReadPixels
126 { GL_RGBA32F, GL_FLOAT, GL_RGBA, GL_FLOAT, kFloat},
127 // XXX add others
128 };
129
130 size_t fmtIdx, attIdx;
131 int error = 0;
132 #ifdef GLES_DEBUG
133 size_t iter = 1;
134 #else
135 size_t iter = 6;
136 #endif
137 RandomSeed seed( gRandomSeed );
138
139 // Check if images are supported
140 if (checkForImageSupport(device)) {
141 log_info("Device does not support images. Skipping test.\n");
142 return 0;
143 }
144
145 // Loop through a set of GL formats, testing a set of sizes against each one
146 for( fmtIdx = 0; fmtIdx < sizeof( formats ) / sizeof( formats[ 0 ] ); fmtIdx++ )
147 {
148 for( attIdx = 0; attIdx < sizeof( attachments ) / sizeof( attachments[ 0 ] ); attIdx++ )
149 {
150 size_t i;
151
152 log_info( "Testing renderbuffer read for %s : %s : %s : %s\n",
153 GetGLAttachmentName( attachments[ attIdx ] ),
154 GetGLFormatName( formats[ fmtIdx ].rbFormat ),
155 GetGLBaseFormatName( formats[ fmtIdx ].rbFormat ),
156 GetGLTypeName( formats[ fmtIdx ].rbType ) );
157
158 for( i = 0; i < iter; i++ )
159 {
160 GLsizei width = random_in_range( 16, 512, seed );
161 GLsizei height = random_in_range( 16, 512, seed );
162 #ifdef GLES_DEBUG
163 width = height = 4;
164 #endif
165
166 if( test_renderbuffer_read_image( context, queue, width, height,
167 attachments[ attIdx ],
168 formats[ fmtIdx ].rbFormat,
169 formats[ fmtIdx ].rbType,
170 formats[ fmtIdx ].texFormat,
171 formats[ fmtIdx ].texType,
172 formats[ fmtIdx ].type, seed ) )
173
174 {
175 log_error( "ERROR: Renderbuffer read test failed for %s : %s : %s : %s\n\n",
176 GetGLAttachmentName( attachments[ attIdx ] ),
177 GetGLFormatName( formats[ fmtIdx ].rbFormat ),
178 GetGLBaseFormatName( formats[ fmtIdx ].rbFormat ),
179 GetGLTypeName( formats[ fmtIdx ].rbType ) );
180
181 error++;
182 break; // Skip other sizes for this combination
183 }
184 }
185 if( i == iter )
186 {
187 log_info( "passed: Renderbuffer read test passed for %s : %s : %s : %s\n\n",
188 GetGLAttachmentName( attachments[ attIdx ] ),
189 GetGLFormatName( formats[ fmtIdx ].rbFormat ),
190 GetGLBaseFormatName( formats[ fmtIdx ].rbFormat ),
191 GetGLTypeName( formats[ fmtIdx ].rbType ) );
192 }
193 }
194 }
195
196 return error;
197 }
198
199
200 #ifdef __APPLE__
201 #pragma mark -------------------- Write tests -------------------------
202 #endif
203
test_attach_renderbuffer_write_to_image(cl_context context,cl_command_queue queue,GLenum glTarget,GLuint glRenderbuffer,size_t imageWidth,size_t imageHeight,cl_image_format * outFormat,ExplicitType * outType,MTdata d,void ** outSourceBuffer)204 int test_attach_renderbuffer_write_to_image( cl_context context, cl_command_queue queue, GLenum glTarget, GLuint glRenderbuffer,
205 size_t imageWidth, size_t imageHeight, cl_image_format *outFormat, ExplicitType *outType, MTdata d, void **outSourceBuffer )
206 {
207 int error;
208
209 // Create a CL image from the supplied GL renderbuffer
210 clMemWrapper image = (*clCreateFromGLRenderbuffer_ptr)( context, CL_MEM_WRITE_ONLY, glRenderbuffer, &error );
211 if( error != CL_SUCCESS )
212 {
213 print_error( error, "Unable to create CL image from GL renderbuffer" );
214 return error;
215 }
216
217 return test_cl_image_write( context, queue, image, imageWidth, imageHeight, outFormat, outType, outSourceBuffer, d );
218 }
219
test_renderbuffer_image_write(cl_context context,cl_command_queue queue,GLsizei width,GLsizei height,GLenum attachment,GLenum rbFormat,GLenum rbType,GLenum texFormat,GLenum texType,ExplicitType type,MTdata d)220 int test_renderbuffer_image_write( cl_context context, cl_command_queue queue,
221 GLsizei width, GLsizei height, GLenum attachment,
222 GLenum rbFormat, GLenum rbType,
223 GLenum texFormat, GLenum texType,
224 ExplicitType type, MTdata d)
225 {
226 int error;
227
228 // Create the GL renderbuffer
229 glFramebufferWrapper glFramebuffer;
230 glRenderbufferWrapper glRenderbuffer;
231 CreateGLRenderbuffer( width, height, attachment, rbFormat, rbType, texFormat, texType,
232 type, &glFramebuffer, &glRenderbuffer, &error, d, false );
233 if( error != 0 )
234 {
235 // GL_RGBA_INTEGER_EXT doesn't exist in GLES2. No need to check for it.
236 return error;
237 }
238
239 // Run and get the results
240 cl_image_format clFormat;
241 ExplicitType sourceType;
242 void *outSourceBuffer;
243 error = test_attach_renderbuffer_write_to_image( context, queue, attachment, glRenderbuffer, width, height, &clFormat, &sourceType, d, (void **)&outSourceBuffer );
244 if( error != 0 )
245 return error;
246
247 BufferOwningPtr<char> sourceData(outSourceBuffer);
248
249 log_info( "- Write [%4d x %4d] : GL Renderbuffer : %s : %s : %s => CL Image : %s : %s \n", width, height,
250 GetGLFormatName( rbFormat ), GetGLFormatName( rbFormat ), GetGLTypeName( rbType),
251 GetChannelOrderName( clFormat.image_channel_order ), GetChannelTypeName( clFormat.image_channel_data_type ));
252
253 // Now read the results from the GL renderbuffer
254 void* tmp = ReadGLRenderbuffer( glFramebuffer, glRenderbuffer, attachment, rbFormat, rbType,
255 texFormat, texType, type, width, height );
256 BufferOwningPtr<char> resultData( tmp );
257
258 #ifdef GLES_DEBUG
259 log_info("- start result data -- \n");
260 DumpGLBuffer(glType, width, height, resultData);
261 log_info("- end result data -- \n");
262 #endif
263
264 // We have to convert our input buffer to the returned type, so we can validate.
265 BufferOwningPtr<char> convertedData( convert_to_expected( resultData, width * height, type, sourceType ) );
266
267 #ifdef GLES_DEBUG
268 log_info("- start input data -- \n");
269 DumpGLBuffer(GetGLTypeForExplicitType(sourceType), width, height, sourceData);
270 log_info("- end input data -- \n");
271 #endif
272
273 #ifdef GLES_DEBUG
274 log_info("- start converted data -- \n");
275 DumpGLBuffer(GetGLTypeForExplicitType(sourceType), width, height, convertedData);
276 log_info("- end converted data -- \n");
277 #endif
278
279 // Now we validate
280 int valid = 0;
281 if(convertedData) {
282 if( sourceType == kFloat )
283 valid = validate_float_results( sourceData, convertedData, width, height );
284 else
285 valid = validate_integer_results( sourceData, convertedData, width, height, get_explicit_type_size( type ) );
286 }
287
288 return valid;
289 }
290
test_renderbuffer_write(cl_device_id device,cl_context context,cl_command_queue queue,int numElements)291 int test_renderbuffer_write( cl_device_id device, cl_context context, cl_command_queue queue, int numElements )
292 {
293 GLenum attachments[] = { GL_COLOR_ATTACHMENT0_EXT };
294
295 struct {
296 GLenum rbFormat;
297 GLenum rbType;
298 GLenum texFormat;
299 GLenum texType;
300 ExplicitType type;
301
302 } formats[] = {
303 { GL_RGBA8_OES, GL_UNSIGNED_BYTE, GL_RGBA, GL_UNSIGNED_BYTE, kUChar },
304 //{ GL_RGBA16F_QCOM, GL_UNSIGNED_SHORT, GL_RGBA, GL_UNSIGNED_SHORT, kHalf }, // Half float not supported by ReadPixels
305 { GL_RGBA32F, GL_FLOAT, GL_RGBA, GL_FLOAT, kFloat},
306 // XXX add others
307 };
308
309 size_t fmtIdx, attIdx;
310 int error = 0;
311 size_t iter = 6;
312 #ifdef GLES_DEBUG
313 iter = 1;
314 #endif
315 RandomSeed seed( gRandomSeed );
316
317 // Check if images are supported
318 if (checkForImageSupport(device)) {
319 log_info("Device does not support images. Skipping test.\n");
320 return 0;
321 }
322
323 // Loop through a set of GL formats, testing a set of sizes against each one
324 for( fmtIdx = 0; fmtIdx < sizeof( formats ) / sizeof( formats[ 0 ] ); fmtIdx++ )
325 {
326 for( attIdx = 0; attIdx < sizeof( attachments ) / sizeof( attachments[ 0 ] ); attIdx++ )
327 {
328 log_info( "Testing Renderbuffer write test for %s : %s : %s : %s\n",
329 GetGLAttachmentName( attachments[ attIdx ] ),
330 GetGLFormatName( formats[ fmtIdx ].rbFormat ),
331 GetGLBaseFormatName( formats[ fmtIdx ].rbFormat ),
332 GetGLTypeName( formats[ fmtIdx ].rbType) );
333
334 size_t i;
335 for( i = 0; i < iter; i++ )
336 {
337 GLsizei width = random_in_range( 16, 512, seed );
338 GLsizei height = random_in_range( 16, 512, seed );
339 #ifdef GLES_DEBUG
340 width = height = 4;
341 #endif
342
343 if( test_renderbuffer_image_write( context, queue, width, height,
344 attachments[ attIdx ],
345 formats[ fmtIdx ].rbFormat,
346 formats[ fmtIdx ].rbType,
347 formats[ fmtIdx ].texFormat,
348 formats[ fmtIdx ].texType,
349 formats[ fmtIdx ].type, seed ) )
350 {
351 log_error( "ERROR: Renderbuffer write test failed for %s : %s : %s : %s\n\n",
352 GetGLAttachmentName( attachments[ attIdx ] ),
353 GetGLFormatName( formats[ fmtIdx ].rbFormat ),
354 GetGLBaseFormatName( formats[ fmtIdx ].rbFormat ),
355 GetGLTypeName( formats[ fmtIdx ].rbType ) );
356
357 error++;
358 break; // Skip other sizes for this combination
359 }
360 }
361 if( i == iter )
362 {
363 log_info( "passed: Renderbuffer write test passed for %s : %s : %s : %s\n\n",
364 GetGLAttachmentName( attachments[ attIdx ] ),
365 GetGLFormatName( formats[ fmtIdx ].rbFormat ),
366 GetGLBaseFormatName( formats[ fmtIdx ].rbFormat ),
367 GetGLTypeName( formats[ fmtIdx ].rbType ) );
368 }
369 }
370 }
371
372 return error;
373 }
374