xref: /aosp_15_r20/external/OpenCL-CTS/test_conformance/gl/test_renderbuffer_info.cpp (revision 6467f958c7de8070b317fc65bcb0f6472e388d82)
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 #if defined(__APPLE__)
18 #include <OpenGL/glu.h>
19 #else
20 #include <GL/glu.h>
21 #include <CL/cl_gl.h>
22 #endif
23 
test_renderbuffer_object_info(cl_context context,cl_command_queue queue,GLsizei width,GLsizei height,GLenum attachment,GLenum format,GLenum internalFormat,GLenum glType,ExplicitType type,MTdata d)24 static int test_renderbuffer_object_info(cl_context context,
25                                          cl_command_queue queue, GLsizei width,
26                                          GLsizei height, GLenum attachment,
27                                          GLenum format, GLenum internalFormat,
28                                          GLenum glType, ExplicitType type,
29                                          MTdata d)
30 {
31     int error;
32 
33     if (type == kHalf)
34         if (DetectFloatToHalfRoundingMode(queue)) return 1;
35 
36     // Create the GL render buffer
37     glFramebufferWrapper glFramebuffer;
38     glRenderbufferWrapper glRenderbuffer;
39     BufferOwningPtr<char> inputBuffer(CreateGLRenderbuffer(
40         width, height, attachment, format, internalFormat, glType, type,
41         &glFramebuffer, &glRenderbuffer, &error, d, true));
42     if (error != 0) return error;
43 
44     clMemWrapper image = (*clCreateFromGLRenderbuffer_ptr)(
45         context, CL_MEM_READ_ONLY, glRenderbuffer, &error);
46     test_error(error, "clCreateFromGLRenderbuffer failed");
47 
48     log_info("- Given a GL format of %s, input type was %s, size was %d x %d\n",
49              GetGLFormatName(internalFormat), get_explicit_type_name(type),
50              (int)width, (int)height);
51 
52     // Verify the expected information here.
53     return CheckGLObjectInfo(image, CL_GL_OBJECT_RENDERBUFFER,
54                              (GLuint)glRenderbuffer, internalFormat, 0);
55 }
56 
test_renderbuffer_getinfo(cl_device_id device,cl_context context,cl_command_queue queue,int numElements)57 int test_renderbuffer_getinfo(cl_device_id device, cl_context context,
58                               cl_command_queue queue, int numElements)
59 {
60     GLenum attachments[] = { GL_COLOR_ATTACHMENT0_EXT };
61 
62     struct
63     {
64         GLenum internal;
65         GLenum format;
66         GLenum datatype;
67         ExplicitType type;
68 
69     } formats[] = { { GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, kUChar },
70                     { GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, kUChar },
71                     { GL_RGBA16, GL_RGBA, GL_UNSIGNED_SHORT, kUShort },
72                     { GL_RGBA32F_ARB, GL_RGBA, GL_FLOAT, kFloat },
73                     { GL_RGBA16F_ARB, GL_RGBA, GL_HALF_FLOAT, kHalf } };
74 
75     size_t fmtIdx, tgtIdx;
76     int error = 0;
77     size_t iter = 6;
78     RandomSeed seed(gRandomSeed);
79 
80     // Check if images are supported
81     if (checkForImageSupport(device))
82     {
83         log_info("Device does not support images. Skipping test.\n");
84         return 0;
85     }
86 
87     if (!gluCheckExtension((const GLubyte *)"GL_EXT_framebuffer_object",
88                            glGetString(GL_EXTENSIONS)))
89     {
90         log_info("Renderbuffers are not supported by this OpenGL "
91                  "implementation; skipping test\n");
92         return 0;
93     }
94 
95     // Loop through a set of GL formats, testing a set of sizes against each one
96     for (fmtIdx = 0; fmtIdx < sizeof(formats) / sizeof(formats[0]); fmtIdx++)
97     {
98         for (tgtIdx = 0; tgtIdx < sizeof(attachments) / sizeof(attachments[0]);
99              tgtIdx++)
100         {
101             log_info("Testing Renderbuffer object info for %s : %s : %s\n",
102                      GetGLFormatName(formats[fmtIdx].internal),
103                      GetGLBaseFormatName(formats[fmtIdx].format),
104                      GetGLTypeName(formats[fmtIdx].datatype));
105 
106             size_t i;
107             for (i = 0; i < iter; i++)
108             {
109                 GLsizei width = random_in_range(16, 512, seed);
110                 GLsizei height = random_in_range(16, 512, seed);
111 
112                 if (test_renderbuffer_object_info(
113                         context, queue, (int)width, (int)height,
114                         attachments[tgtIdx], formats[fmtIdx].format,
115                         formats[fmtIdx].internal, formats[fmtIdx].datatype,
116                         formats[fmtIdx].type, seed))
117                 {
118                     log_error("ERROR: Renderbuffer write test failed for GL "
119                               "format %s : %s\n\n",
120                               GetGLFormatName(formats[fmtIdx].internal),
121                               GetGLTypeName(formats[fmtIdx].datatype));
122 
123                     error++;
124                     break; // Skip other sizes for this combination
125                 }
126             }
127             if (i == iter)
128             {
129                 log_info("passed: Renderbuffer write test passed for GL format "
130                          "%s : %s\n\n",
131                          GetGLFormatName(formats[fmtIdx].internal),
132                          GetGLTypeName(formats[fmtIdx].datatype));
133             }
134         }
135     }
136 
137     return error;
138 }
139