aboutsummaryrefslogtreecommitdiff
path: root/test_conformance/gles/test_renderbuffer.cpp
blob: 0f6d289b9cceb22aef169beb00c4d26522e2e489 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//
// 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 "testBase.h"
#include "helpers.h"

#include "gl_headers.h"

extern int test_cl_image_write( cl_context context, cl_command_queue queue, cl_mem clImage,
                                size_t imageWidth, size_t imageHeight, cl_image_format *outFormat,
                                ExplicitType *outType, void **outSourceBuffer, MTdata d );

extern int test_cl_image_read( cl_context context, cl_command_queue queue, cl_mem clImage,
                               size_t imageWidth, size_t imageHeight, cl_image_format *outFormat,
                               ExplicitType *outType, void **outResultBuffer );

static int 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 )
{
    int error;

    // Create a CL image from the supplied GL renderbuffer
    clMemWrapper image = (*clCreateFromGLRenderbuffer_ptr)( context, CL_MEM_READ_ONLY, glRenderbuffer, &error );
    if( error != CL_SUCCESS )
    {
        print_error( error, "Unable to create CL image from GL renderbuffer" );
        return error;
    }

    return test_cl_image_read( context, queue, image, imageWidth, imageHeight, outFormat, outType, outResultBuffer );
}

int 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 )
{
    int error;


    // Create the GL renderbuffer
    glFramebufferWrapper glFramebuffer;
    glRenderbufferWrapper glRenderbuffer;
    void *tmp = CreateGLRenderbuffer( width, height, attachment, rbFormat, rbType, texFormat, texType,
                                     type, &glFramebuffer, &glRenderbuffer, &error, d, true );
    BufferOwningPtr<char> inputBuffer(tmp);
    if( error != 0 )
    {
        // GL_RGBA_INTEGER_EXT doesn't exist in GLES2. No need to check for it.
        return error;
    }

    // Run and get the results
    cl_image_format clFormat;
    ExplicitType actualType;
    char *outBuffer;
    error = test_attach_renderbuffer_read_image( context, queue, attachment, glRenderbuffer, width, height, &clFormat, &actualType, (void **)&outBuffer );
    if( error != 0 )
        return error;
    BufferOwningPtr<char> actualResults(outBuffer);

    log_info( "- Read [%4d x %4d] : GL renderbuffer : %s : %s : %s => CL Image : %s : %s \n", width, height,
                    GetGLFormatName( rbFormat ), GetGLFormatName( rbFormat ), GetGLTypeName( rbType ),
                    GetChannelOrderName( clFormat.image_channel_order ), GetChannelTypeName( clFormat.image_channel_data_type ));

#ifdef GLES_DEBUG
    log_info("- start read GL data -- \n");
    DumpGLBuffer(glType, width, height, actualResults);
    log_info("- end read GL data -- \n");
#endif

    // We have to convert our input buffer to the returned type, so we can validate.
    BufferOwningPtr<char> convertedInput(convert_to_expected( inputBuffer, width * height, type, actualType ));

#ifdef GLES_DEBUG
    log_info("- start input data -- \n");
    DumpGLBuffer(GetGLTypeForExplicitType(actualType), width, height, convertedInput);
    log_info("- end input data -- \n");
#endif

#ifdef GLES_DEBUG
    log_info("- start converted data -- \n");
    DumpGLBuffer(GetGLTypeForExplicitType(actualType), width, height, actualResults);
    log_info("- end converted data -- \n");
#endif

    // Now we validate
    int valid = 0;
    if(convertedInput) {
        if( actualType == kFloat )
            valid = validate_float_results( convertedInput, actualResults, width, height );
        else
            valid = validate_integer_results( convertedInput, actualResults, width, height, get_explicit_type_size( actualType ) );
    }

    return valid;
}

int test_renderbuffer_read( cl_device_id device, cl_context context, cl_command_queue queue, int numElements )
{
    GLenum attachments[] = { GL_COLOR_ATTACHMENT0_EXT };

    struct {
        GLenum rbFormat;
        GLenum rbType;
        GLenum texFormat;
        GLenum texType;
        ExplicitType type;

    } formats[] = {
        { GL_RGBA8_OES,    GL_UNSIGNED_BYTE,   GL_RGBA,           GL_UNSIGNED_BYTE,            kUChar },
        //{ GL_RGBA16F_QCOM, GL_HALF_FLOAT_OES,  GL_RGBA,           GL_HALF_FLOAT_OES,           kHalf  },  // Half-float not supported by ReadPixels
        { GL_RGBA32F,      GL_FLOAT,           GL_RGBA,           GL_FLOAT,                    kFloat},
        // XXX add others
    };

    size_t fmtIdx, attIdx;
    int error = 0;
#ifdef GLES_DEBUG
    size_t iter = 1;
#else
    size_t iter = 6;
#endif
    RandomSeed seed( gRandomSeed );

    // Check if images are supported
  if (checkForImageSupport(device)) {
    log_info("Device does not support images. Skipping test.\n");
    return 0;
  }

    // Loop through a set of GL formats, testing a set of sizes against each one
    for( fmtIdx = 0; fmtIdx < sizeof( formats ) / sizeof( formats[ 0 ] ); fmtIdx++ )
    {
        for( attIdx = 0; attIdx < sizeof( attachments ) / sizeof( attachments[ 0 ] ); attIdx++ )
        {
            size_t i;

            log_info( "Testing renderbuffer read for %s : %s : %s : %s\n",
                GetGLAttachmentName( attachments[ attIdx ] ),
                GetGLFormatName( formats[ fmtIdx ].rbFormat ),
                GetGLBaseFormatName( formats[ fmtIdx ].rbFormat ),
                GetGLTypeName( formats[ fmtIdx ].rbType ) );

            for( i = 0; i < iter; i++ )
            {
                GLsizei width = random_in_range( 16, 512, seed );
                GLsizei height = random_in_range( 16, 512, seed );
#ifdef GLES_DEBUG
                width = height = 4;
#endif

                if( test_renderbuffer_read_image( context, queue, width, height,
                                                  attachments[ attIdx ],
                                                  formats[ fmtIdx ].rbFormat,
                                                  formats[ fmtIdx ].rbType,
                                                  formats[ fmtIdx ].texFormat,
                                                  formats[ fmtIdx ].texType,
                                                  formats[ fmtIdx ].type, seed ) )

                {
                    log_error( "ERROR: Renderbuffer read test failed for %s : %s : %s : %s\n\n",
                                GetGLAttachmentName( attachments[ attIdx ] ),
                                GetGLFormatName( formats[ fmtIdx ].rbFormat ),
                                GetGLBaseFormatName( formats[ fmtIdx ].rbFormat ),
                                GetGLTypeName( formats[ fmtIdx ].rbType ) );

                    error++;
                    break;    // Skip other sizes for this combination
                }
            }
            if( i == iter )
            {
                log_info( "passed: Renderbuffer read test passed for %s : %s : %s : %s\n\n",
                          GetGLAttachmentName( attachments[ attIdx ] ),
                          GetGLFormatName( formats[ fmtIdx ].rbFormat ),
                          GetGLBaseFormatName( formats[ fmtIdx ].rbFormat ),
                          GetGLTypeName( formats[ fmtIdx ].rbType ) );
            }
        }
    }

    return error;
}


#ifdef __APPLE__
#pragma mark -------------------- Write tests -------------------------
#endif

int 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 )
{
    int error;

    // Create a CL image from the supplied GL renderbuffer
    clMemWrapper image = (*clCreateFromGLRenderbuffer_ptr)( context, CL_MEM_WRITE_ONLY, glRenderbuffer, &error );
    if( error != CL_SUCCESS )
    {
        print_error( error, "Unable to create CL image from GL renderbuffer" );
        return error;
    }

    return test_cl_image_write( context, queue, image, imageWidth, imageHeight, outFormat, outType, outSourceBuffer, d );
}

int 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)
{
    int error;

    // Create the GL renderbuffer
    glFramebufferWrapper glFramebuffer;
    glRenderbufferWrapper glRenderbuffer;
    CreateGLRenderbuffer( width, height, attachment, rbFormat, rbType, texFormat, texType,
                         type, &glFramebuffer, &glRenderbuffer, &error, d, false );
    if( error != 0 )
    {
        // GL_RGBA_INTEGER_EXT doesn't exist in GLES2. No need to check for it.
        return error;
    }

    // Run and get the results
    cl_image_format clFormat;
    ExplicitType sourceType;
    void *outSourceBuffer;
    error = test_attach_renderbuffer_write_to_image( context, queue, attachment, glRenderbuffer, width, height, &clFormat, &sourceType, d, (void **)&outSourceBuffer );
    if( error != 0 )
        return error;

    BufferOwningPtr<char> sourceData(outSourceBuffer);

    log_info( "- Write [%4d x %4d] : GL Renderbuffer : %s : %s : %s => CL Image : %s : %s \n", width, height,
                    GetGLFormatName( rbFormat ), GetGLFormatName( rbFormat ), GetGLTypeName( rbType),
                    GetChannelOrderName( clFormat.image_channel_order ), GetChannelTypeName( clFormat.image_channel_data_type ));

    // Now read the results from the GL renderbuffer
    void* tmp = ReadGLRenderbuffer( glFramebuffer, glRenderbuffer, attachment, rbFormat, rbType,
                                    texFormat, texType, type, width, height );
    BufferOwningPtr<char> resultData( tmp );

#ifdef GLES_DEBUG
    log_info("- start result data -- \n");
    DumpGLBuffer(glType, width, height, resultData);
    log_info("- end result data -- \n");
#endif

    // We have to convert our input buffer to the returned type, so we can validate.
    BufferOwningPtr<char> convertedData( convert_to_expected( resultData, width * height, type, sourceType ) );

#ifdef GLES_DEBUG
    log_info("- start input data -- \n");
    DumpGLBuffer(GetGLTypeForExplicitType(sourceType), width, height, sourceData);
    log_info("- end input data -- \n");
#endif

#ifdef GLES_DEBUG
    log_info("- start converted data -- \n");
    DumpGLBuffer(GetGLTypeForExplicitType(sourceType), width, height, convertedData);
    log_info("- end converted data -- \n");
#endif

    // Now we validate
    int valid = 0;
    if(convertedData) {
        if( sourceType == kFloat )
            valid = validate_float_results( sourceData, convertedData, width, height );
        else
            valid = validate_integer_results( sourceData, convertedData, width, height, get_explicit_type_size( type ) );
    }

    return valid;
}

int test_renderbuffer_write( cl_device_id device, cl_context context, cl_command_queue queue, int numElements )
{
    GLenum attachments[] = { GL_COLOR_ATTACHMENT0_EXT };

    struct {
        GLenum rbFormat;
        GLenum rbType;
        GLenum texFormat;
        GLenum texType;
        ExplicitType type;

    } formats[] = {
        { GL_RGBA8_OES,    GL_UNSIGNED_BYTE,   GL_RGBA,           GL_UNSIGNED_BYTE,            kUChar },
        //{ GL_RGBA16F_QCOM, GL_UNSIGNED_SHORT,  GL_RGBA,           GL_UNSIGNED_SHORT,           kHalf  },  // Half float not supported by ReadPixels
        { GL_RGBA32F,      GL_FLOAT,           GL_RGBA,           GL_FLOAT,                    kFloat},
        // XXX add others
    };

    size_t fmtIdx, attIdx;
    int error = 0;
    size_t iter = 6;
#ifdef GLES_DEBUG
    iter = 1;
#endif
    RandomSeed seed( gRandomSeed );

    // Check if images are supported
  if (checkForImageSupport(device)) {
    log_info("Device does not support images. Skipping test.\n");
    return 0;
  }

    // Loop through a set of GL formats, testing a set of sizes against each one
    for( fmtIdx = 0; fmtIdx < sizeof( formats ) / sizeof( formats[ 0 ] ); fmtIdx++ )
    {
        for( attIdx = 0; attIdx < sizeof( attachments ) / sizeof( attachments[ 0 ] ); attIdx++ )
        {
            log_info( "Testing Renderbuffer write test for %s : %s : %s : %s\n",
                GetGLAttachmentName( attachments[ attIdx ] ),
                GetGLFormatName( formats[ fmtIdx ].rbFormat ),
                GetGLBaseFormatName( formats[ fmtIdx ].rbFormat ),
                GetGLTypeName( formats[ fmtIdx ].rbType) );

            size_t i;
            for( i = 0; i < iter; i++ )
            {
                GLsizei width = random_in_range( 16, 512, seed );
                GLsizei height = random_in_range( 16, 512, seed );
#ifdef GLES_DEBUG
                width = height = 4;
#endif

                if( test_renderbuffer_image_write( context, queue, width, height,
                                                   attachments[ attIdx ],
                                                   formats[ fmtIdx ].rbFormat,
                                                   formats[ fmtIdx ].rbType,
                                                   formats[ fmtIdx ].texFormat,
                                                   formats[ fmtIdx ].texType,
                                                   formats[ fmtIdx ].type, seed ) )
                {
                    log_error( "ERROR: Renderbuffer write test failed for %s : %s : %s : %s\n\n",
                          GetGLAttachmentName( attachments[ attIdx ] ),
                          GetGLFormatName( formats[ fmtIdx ].rbFormat ),
                          GetGLBaseFormatName( formats[ fmtIdx ].rbFormat ),
                          GetGLTypeName( formats[ fmtIdx ].rbType ) );

                    error++;
                    break;    // Skip other sizes for this combination
                }
            }
            if( i == iter )
            {
                log_info( "passed: Renderbuffer write test passed for %s : %s : %s : %s\n\n",
                          GetGLAttachmentName( attachments[ attIdx ] ),
                          GetGLFormatName( formats[ fmtIdx ].rbFormat ),
                          GetGLBaseFormatName( formats[ fmtIdx ].rbFormat ),
                          GetGLTypeName( formats[ fmtIdx ].rbType ) );
            }
        }
    }

    return error;
}