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 "common.h"
18 
19 #if defined( __APPLE__ )
20 #include <OpenGL/glu.h>
21 #else
22 #include <GL/glu.h>
23 #include <CL/cl_gl.h>
24 #endif
25 #include <algorithm>
26 
27 using namespace std;
28 
29 #pragma mark -
30 #pragma mark _2D read tests
31 
calc_2D_test_size_descriptors(sizevec_t * sizes,size_t nsizes)32 void calc_2D_test_size_descriptors(sizevec_t* sizes, size_t nsizes)
33 {
34   // Need to limit array size according to GL device properties
35   // Need to limit texture size according to GL device properties
36   GLint maxTextureSize = 4096, maxTextureRectangleSize = 4096, size;
37   glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
38   glGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE_EXT, &maxTextureRectangleSize);
39 
40   size = min(maxTextureSize, maxTextureRectangleSize);
41 
42   RandomSeed seed( gRandomSeed );
43 
44   // Generate some random sizes (within reasonable ranges)
45   for (size_t i = 0; i < nsizes; i++) {
46     sizes[i].width  = random_in_range( 2, min(size, 1<<(i+4)), seed );
47     sizes[i].height = random_in_range( 2, min(size, 1<<(i+4)), seed );
48     sizes[i].depth  = 1;
49   }
50 }
51 
calc_cube_test_size_descriptors(sizevec_t * sizes,size_t nsizes)52 void calc_cube_test_size_descriptors(sizevec_t* sizes, size_t nsizes)
53 {
54   // Need to limit array size according to GL device properties
55   // Need to limit texture size according to GL device properties
56   GLint maxQubeMapSize = 4096;
57   glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &maxQubeMapSize);
58 
59   RandomSeed seed( gRandomSeed );
60 
61   // Generate some random sizes (within reasonable ranges)
62   for (size_t i = 0; i < nsizes; i++) {
63     sizes[i].width  = sizes[i].height = random_in_range( 2, min(maxQubeMapSize, 1<<(i+4)), seed );
64     sizes[i].depth  = 1;
65   }
66 }
67 
test_images_read_2D(cl_device_id device,cl_context context,cl_command_queue queue,int numElements)68 int test_images_read_2D( cl_device_id device, cl_context context,
69   cl_command_queue queue, int numElements )
70 {
71   GLenum targets[] = { GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE_EXT };
72   size_t ntargets = sizeof(targets) / sizeof(targets[0]);
73 
74   size_t nformats = sizeof(common_formats) / sizeof(common_formats[0]);
75 
76   const size_t nsizes = 8;
77   sizevec_t sizes[nsizes];
78   calc_2D_test_size_descriptors(sizes, nsizes);
79 
80   return test_images_read_common(device, context, queue, common_formats,
81     nformats, targets, ntargets, sizes, nsizes);
82 }
83 
test_images_read_cube(cl_device_id device,cl_context context,cl_command_queue queue,int numElements)84 int test_images_read_cube( cl_device_id device, cl_context context,
85   cl_command_queue queue, int numElements )
86 {
87   GLenum targets[] = {
88     GL_TEXTURE_CUBE_MAP_POSITIVE_X,
89     GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
90     GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
91     GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
92     GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
93     GL_TEXTURE_CUBE_MAP_NEGATIVE_Z };
94 
95   size_t ntargets = sizeof(targets) / sizeof(targets[0]);
96   size_t nformats = sizeof(common_formats) / sizeof(common_formats[0]);
97 
98   const size_t nsizes = 8;
99   sizevec_t sizes[nsizes];
100   calc_cube_test_size_descriptors(sizes, nsizes);
101 
102   return test_images_read_common(device, context, queue, common_formats,
103     nformats, targets, ntargets, sizes, nsizes);
104 }
105 
106 #pragma mark -
107 #pragma mark _2D write tests
108 
109 #include "common.h"
110 
test_images_write(cl_device_id device,cl_context context,cl_command_queue queue,int numElements)111 int test_images_write( cl_device_id device, cl_context context,
112   cl_command_queue queue, int numElements )
113 {
114   GLenum targets[] = { GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE_EXT };
115   size_t ntargets = sizeof(targets) / sizeof(targets[0]);
116   size_t nformats = sizeof(common_formats) / sizeof(common_formats[0]);
117 
118   const size_t nsizes = 8;
119   sizevec_t sizes[nsizes];
120   calc_2D_test_size_descriptors(sizes, nsizes);
121 
122   return test_images_write_common( device, context, queue, common_formats,
123     nformats, targets, ntargets, sizes, nsizes );
124 }
125 
test_images_write_cube(cl_device_id device,cl_context context,cl_command_queue queue,int numElements)126 int test_images_write_cube( cl_device_id device, cl_context context,
127   cl_command_queue queue, int numElements )
128 {
129   size_t nformats = sizeof(common_formats) / sizeof(common_formats[0]);
130 
131   GLenum targets[] = {
132     GL_TEXTURE_CUBE_MAP_POSITIVE_X,
133     GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
134     GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
135     GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
136     GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
137     GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
138   };
139   size_t ntargets = sizeof(targets) / sizeof(targets[0]);
140 
141   const size_t nsizes = 8;
142   sizevec_t sizes[nsizes];
143   calc_cube_test_size_descriptors(sizes, nsizes);
144 
145   return test_images_write_common( device, context, queue, common_formats,
146     nformats, targets, ntargets, sizes, nsizes );
147 }
148 
149 #pragma mark -
150 #pragma mark _2D get info tests
151 
test_images_2D_getinfo(cl_device_id device,cl_context context,cl_command_queue queue,int numElements)152 int test_images_2D_getinfo( cl_device_id device, cl_context context,
153   cl_command_queue queue, int numElements )
154 {
155   GLenum targets[] = { GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE_EXT };
156   size_t ntargets = sizeof(targets) / sizeof(targets[0]);
157 
158   size_t nformats = sizeof(common_formats) / sizeof(common_formats[0]);
159 
160   const size_t nsizes = 8;
161   sizevec_t sizes[nsizes];
162   calc_2D_test_size_descriptors(sizes, nsizes);
163 
164   return test_images_get_info_common(device, context, queue, common_formats,
165       nformats, targets, ntargets, sizes, nsizes);
166 }
167 
test_images_cube_getinfo(cl_device_id device,cl_context context,cl_command_queue queue,int numElements)168 int test_images_cube_getinfo( cl_device_id device, cl_context context,
169   cl_command_queue queue, int numElements )
170 {
171     GLenum targets[] = {
172     GL_TEXTURE_CUBE_MAP_POSITIVE_X,
173     GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
174     GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
175     GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
176     GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
177     GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
178   };
179   size_t ntargets = sizeof(targets) / sizeof(targets[0]);
180   size_t nformats = sizeof(common_formats) / sizeof(common_formats[0]);
181 
182   const size_t nsizes = 8;
183   sizevec_t sizes[nsizes];
184   calc_cube_test_size_descriptors(sizes, nsizes);
185 
186   return test_images_get_info_common(device, context, queue, common_formats,
187       nformats, targets, ntargets, sizes, nsizes);
188 }
189