blob: 0b6499f059551f04297a5fe72c6b5065965bede3 [file] [log] [blame]
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001//
Geoff Langcec35902014-04-16 10:52:36 -04002// Copyright (c) 2013-2014 The ANGLE Project Authors. All rights reserved.
Geoff Lange8ebe7f2013-08-05 15:03:13 -04003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// validationES3.cpp: Validation functions for OpenGL ES 3.0 entry point parameters
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/validationES3.h"
10#include "libANGLE/validationES.h"
11#include "libANGLE/Context.h"
12#include "libANGLE/Texture.h"
13#include "libANGLE/Framebuffer.h"
14#include "libANGLE/Renderbuffer.h"
15#include "libANGLE/formatutils.h"
16#include "libANGLE/FramebufferAttachment.h"
Geoff Lange8ebe7f2013-08-05 15:03:13 -040017
18#include "common/mathutil.h"
Geoff Langa9be0dc2014-12-17 12:34:40 -050019#include "common/utilities.h"
Geoff Lange8ebe7f2013-08-05 15:03:13 -040020
21namespace gl
22{
23
Geoff Lang5d601382014-07-22 15:14:06 -040024struct ES3FormatCombination
25{
26 GLenum internalFormat;
27 GLenum format;
28 GLenum type;
29};
30
31bool operator<(const ES3FormatCombination& a, const ES3FormatCombination& b)
32{
33 return memcmp(&a, &b, sizeof(ES3FormatCombination)) < 0;
34}
35
36typedef std::set<ES3FormatCombination> ES3FormatCombinationSet;
37
38static inline void InsertES3FormatCombo(ES3FormatCombinationSet *set, GLenum internalFormat, GLenum format, GLenum type)
39{
40 ES3FormatCombination info;
41 info.internalFormat = internalFormat;
42 info.format = format;
43 info.type = type;
44 set->insert(info);
45}
46
47ES3FormatCombinationSet BuildES3FormatSet()
48{
49 ES3FormatCombinationSet set;
50
51 // Format combinations from ES 3.0.1 spec, table 3.2
52
53 // | Internal format | Format | Type |
54 // | | | |
55 InsertES3FormatCombo(&set, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE );
56 InsertES3FormatCombo(&set, GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_BYTE );
57 InsertES3FormatCombo(&set, GL_RGBA4, GL_RGBA, GL_UNSIGNED_BYTE );
58 InsertES3FormatCombo(&set, GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE );
59 InsertES3FormatCombo(&set, GL_RGBA8_SNORM, GL_RGBA, GL_BYTE );
60 InsertES3FormatCombo(&set, GL_RGBA4, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4 );
61 InsertES3FormatCombo(&set, GL_RGB10_A2, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV );
62 InsertES3FormatCombo(&set, GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV );
63 InsertES3FormatCombo(&set, GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1 );
64 InsertES3FormatCombo(&set, GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT );
65 InsertES3FormatCombo(&set, GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT_OES );
66 InsertES3FormatCombo(&set, GL_RGBA32F, GL_RGBA, GL_FLOAT );
67 InsertES3FormatCombo(&set, GL_RGBA16F, GL_RGBA, GL_FLOAT );
68 InsertES3FormatCombo(&set, GL_RGBA8UI, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE );
69 InsertES3FormatCombo(&set, GL_RGBA8I, GL_RGBA_INTEGER, GL_BYTE );
70 InsertES3FormatCombo(&set, GL_RGBA16UI, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT );
71 InsertES3FormatCombo(&set, GL_RGBA16I, GL_RGBA_INTEGER, GL_SHORT );
72 InsertES3FormatCombo(&set, GL_RGBA32UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT );
73 InsertES3FormatCombo(&set, GL_RGBA32I, GL_RGBA_INTEGER, GL_INT );
74 InsertES3FormatCombo(&set, GL_RGB10_A2UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV );
75 InsertES3FormatCombo(&set, GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE );
76 InsertES3FormatCombo(&set, GL_RGB565, GL_RGB, GL_UNSIGNED_BYTE );
77 InsertES3FormatCombo(&set, GL_SRGB8, GL_RGB, GL_UNSIGNED_BYTE );
78 InsertES3FormatCombo(&set, GL_RGB8_SNORM, GL_RGB, GL_BYTE );
79 InsertES3FormatCombo(&set, GL_RGB565, GL_RGB, GL_UNSIGNED_SHORT_5_6_5 );
80 InsertES3FormatCombo(&set, GL_R11F_G11F_B10F, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV );
81 InsertES3FormatCombo(&set, GL_RGB9_E5, GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV );
82 InsertES3FormatCombo(&set, GL_RGB16F, GL_RGB, GL_HALF_FLOAT );
83 InsertES3FormatCombo(&set, GL_RGB16F, GL_RGB, GL_HALF_FLOAT_OES );
84 InsertES3FormatCombo(&set, GL_R11F_G11F_B10F, GL_RGB, GL_HALF_FLOAT );
85 InsertES3FormatCombo(&set, GL_R11F_G11F_B10F, GL_RGB, GL_HALF_FLOAT_OES );
86 InsertES3FormatCombo(&set, GL_RGB9_E5, GL_RGB, GL_HALF_FLOAT );
87 InsertES3FormatCombo(&set, GL_RGB9_E5, GL_RGB, GL_HALF_FLOAT_OES );
88 InsertES3FormatCombo(&set, GL_RGB32F, GL_RGB, GL_FLOAT );
89 InsertES3FormatCombo(&set, GL_RGB16F, GL_RGB, GL_FLOAT );
90 InsertES3FormatCombo(&set, GL_R11F_G11F_B10F, GL_RGB, GL_FLOAT );
91 InsertES3FormatCombo(&set, GL_RGB9_E5, GL_RGB, GL_FLOAT );
92 InsertES3FormatCombo(&set, GL_RGB8UI, GL_RGB_INTEGER, GL_UNSIGNED_BYTE );
93 InsertES3FormatCombo(&set, GL_RGB8I, GL_RGB_INTEGER, GL_BYTE );
94 InsertES3FormatCombo(&set, GL_RGB16UI, GL_RGB_INTEGER, GL_UNSIGNED_SHORT );
95 InsertES3FormatCombo(&set, GL_RGB16I, GL_RGB_INTEGER, GL_SHORT );
96 InsertES3FormatCombo(&set, GL_RGB32UI, GL_RGB_INTEGER, GL_UNSIGNED_INT );
97 InsertES3FormatCombo(&set, GL_RGB32I, GL_RGB_INTEGER, GL_INT );
98 InsertES3FormatCombo(&set, GL_RG8, GL_RG, GL_UNSIGNED_BYTE );
99 InsertES3FormatCombo(&set, GL_RG8_SNORM, GL_RG, GL_BYTE );
100 InsertES3FormatCombo(&set, GL_RG16F, GL_RG, GL_HALF_FLOAT );
101 InsertES3FormatCombo(&set, GL_RG16F, GL_RG, GL_HALF_FLOAT_OES );
102 InsertES3FormatCombo(&set, GL_RG32F, GL_RG, GL_FLOAT );
103 InsertES3FormatCombo(&set, GL_RG16F, GL_RG, GL_FLOAT );
104 InsertES3FormatCombo(&set, GL_RG8UI, GL_RG_INTEGER, GL_UNSIGNED_BYTE );
105 InsertES3FormatCombo(&set, GL_RG8I, GL_RG_INTEGER, GL_BYTE );
106 InsertES3FormatCombo(&set, GL_RG16UI, GL_RG_INTEGER, GL_UNSIGNED_SHORT );
107 InsertES3FormatCombo(&set, GL_RG16I, GL_RG_INTEGER, GL_SHORT );
108 InsertES3FormatCombo(&set, GL_RG32UI, GL_RG_INTEGER, GL_UNSIGNED_INT );
109 InsertES3FormatCombo(&set, GL_RG32I, GL_RG_INTEGER, GL_INT );
110 InsertES3FormatCombo(&set, GL_R8, GL_RED, GL_UNSIGNED_BYTE );
111 InsertES3FormatCombo(&set, GL_R8_SNORM, GL_RED, GL_BYTE );
112 InsertES3FormatCombo(&set, GL_R16F, GL_RED, GL_HALF_FLOAT );
113 InsertES3FormatCombo(&set, GL_R16F, GL_RED, GL_HALF_FLOAT_OES );
114 InsertES3FormatCombo(&set, GL_R32F, GL_RED, GL_FLOAT );
115 InsertES3FormatCombo(&set, GL_R16F, GL_RED, GL_FLOAT );
116 InsertES3FormatCombo(&set, GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE );
117 InsertES3FormatCombo(&set, GL_R8I, GL_RED_INTEGER, GL_BYTE );
118 InsertES3FormatCombo(&set, GL_R16UI, GL_RED_INTEGER, GL_UNSIGNED_SHORT );
119 InsertES3FormatCombo(&set, GL_R16I, GL_RED_INTEGER, GL_SHORT );
120 InsertES3FormatCombo(&set, GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT );
121 InsertES3FormatCombo(&set, GL_R32I, GL_RED_INTEGER, GL_INT );
122
123 // Unsized formats
124 InsertES3FormatCombo(&set, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE );
125 InsertES3FormatCombo(&set, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4 );
126 InsertES3FormatCombo(&set, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1 );
127 InsertES3FormatCombo(&set, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE );
128 InsertES3FormatCombo(&set, GL_RGB, GL_RGB, GL_UNSIGNED_SHORT_5_6_5 );
129 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE );
130 InsertES3FormatCombo(&set, GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE );
131 InsertES3FormatCombo(&set, GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE );
132 InsertES3FormatCombo(&set, GL_SRGB_ALPHA_EXT, GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE );
133 InsertES3FormatCombo(&set, GL_SRGB_EXT, GL_SRGB_EXT, GL_UNSIGNED_BYTE );
Jamie Madill689325c2015-07-20 14:36:53 -0400134 InsertES3FormatCombo(&set, GL_RG, GL_RG, GL_UNSIGNED_BYTE );
135 InsertES3FormatCombo(&set, GL_RG, GL_RG, GL_FLOAT );
136 InsertES3FormatCombo(&set, GL_RG, GL_RG, GL_HALF_FLOAT );
137 InsertES3FormatCombo(&set, GL_RG, GL_RG, GL_HALF_FLOAT_OES );
138 InsertES3FormatCombo(&set, GL_RED, GL_RED, GL_UNSIGNED_BYTE );
139 InsertES3FormatCombo(&set, GL_RED, GL_RED, GL_FLOAT );
140 InsertES3FormatCombo(&set, GL_RED, GL_RED, GL_HALF_FLOAT );
141 InsertES3FormatCombo(&set, GL_RED, GL_RED, GL_HALF_FLOAT_OES );
142 InsertES3FormatCombo(&set, GL_DEPTH_STENCIL, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8 );
Geoff Lang5d601382014-07-22 15:14:06 -0400143
144 // Depth stencil formats
145 InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT );
146 InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT );
147 InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT );
148 InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT );
149 InsertES3FormatCombo(&set, GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8 );
150 InsertES3FormatCombo(&set, GL_DEPTH32F_STENCIL8, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
151
152 // From GL_EXT_sRGB
153 InsertES3FormatCombo(&set, GL_SRGB8_ALPHA8_EXT, GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE );
154 InsertES3FormatCombo(&set, GL_SRGB8, GL_SRGB_EXT, GL_UNSIGNED_BYTE );
155
156 // From GL_OES_texture_float
Jeff Muizelaarbb1a5be2015-12-02 12:03:46 -0500157 InsertES3FormatCombo(&set, GL_RGBA, GL_RGBA, GL_FLOAT );
158 InsertES3FormatCombo(&set, GL_RGB, GL_RGB, GL_FLOAT );
Geoff Lang5d601382014-07-22 15:14:06 -0400159 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_FLOAT );
160 InsertES3FormatCombo(&set, GL_LUMINANCE, GL_LUMINANCE, GL_FLOAT );
161 InsertES3FormatCombo(&set, GL_ALPHA, GL_ALPHA, GL_FLOAT );
162
163 // From GL_OES_texture_half_float
164 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT );
165 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES );
166 InsertES3FormatCombo(&set, GL_LUMINANCE, GL_LUMINANCE, GL_HALF_FLOAT );
167 InsertES3FormatCombo(&set, GL_LUMINANCE, GL_LUMINANCE, GL_HALF_FLOAT_OES );
168 InsertES3FormatCombo(&set, GL_ALPHA, GL_ALPHA, GL_HALF_FLOAT );
169 InsertES3FormatCombo(&set, GL_ALPHA, GL_ALPHA, GL_HALF_FLOAT_OES );
170
171 // From GL_EXT_texture_format_BGRA8888
172 InsertES3FormatCombo(&set, GL_BGRA_EXT, GL_BGRA_EXT, GL_UNSIGNED_BYTE );
173
174 // From GL_EXT_texture_storage
175 // | Internal format | Format | Type |
176 // | | | |
177 InsertES3FormatCombo(&set, GL_ALPHA8_EXT, GL_ALPHA, GL_UNSIGNED_BYTE );
178 InsertES3FormatCombo(&set, GL_LUMINANCE8_EXT, GL_LUMINANCE, GL_UNSIGNED_BYTE );
179 InsertES3FormatCombo(&set, GL_LUMINANCE8_ALPHA8_EXT, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE );
180 InsertES3FormatCombo(&set, GL_ALPHA32F_EXT, GL_ALPHA, GL_FLOAT );
181 InsertES3FormatCombo(&set, GL_LUMINANCE32F_EXT, GL_LUMINANCE, GL_FLOAT );
182 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA32F_EXT, GL_LUMINANCE_ALPHA, GL_FLOAT );
183 InsertES3FormatCombo(&set, GL_ALPHA16F_EXT, GL_ALPHA, GL_HALF_FLOAT );
184 InsertES3FormatCombo(&set, GL_ALPHA16F_EXT, GL_ALPHA, GL_HALF_FLOAT_OES );
185 InsertES3FormatCombo(&set, GL_LUMINANCE16F_EXT, GL_LUMINANCE, GL_HALF_FLOAT );
186 InsertES3FormatCombo(&set, GL_LUMINANCE16F_EXT, GL_LUMINANCE, GL_HALF_FLOAT_OES );
187 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA16F_EXT, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT );
188 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA16F_EXT, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES );
189
190 // From GL_EXT_texture_storage and GL_EXT_texture_format_BGRA8888
191 InsertES3FormatCombo(&set, GL_BGRA8_EXT, GL_BGRA_EXT, GL_UNSIGNED_BYTE );
192 InsertES3FormatCombo(&set, GL_BGRA4_ANGLEX, GL_BGRA_EXT, GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT);
193 InsertES3FormatCombo(&set, GL_BGRA4_ANGLEX, GL_BGRA_EXT, GL_UNSIGNED_BYTE );
194 InsertES3FormatCombo(&set, GL_BGR5_A1_ANGLEX, GL_BGRA_EXT, GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT);
195 InsertES3FormatCombo(&set, GL_BGR5_A1_ANGLEX, GL_BGRA_EXT, GL_UNSIGNED_BYTE );
196
197 // From GL_ANGLE_depth_texture
198 InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT32_OES, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT_24_8_OES );
199
Geoff Lang5d601382014-07-22 15:14:06 -0400200 return set;
201}
202
203static bool ValidateTexImageFormatCombination(gl::Context *context, GLenum internalFormat, GLenum format, GLenum type)
204{
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500205 // For historical reasons, glTexImage2D and glTexImage3D pass in their internal format as a
206 // GLint instead of a GLenum. Therefor an invalid internal format gives a GL_INVALID_VALUE
207 // error instead of a GL_INVALID_ENUM error. As this validation function is only called in
208 // the validation codepaths for glTexImage2D/3D, we record a GL_INVALID_VALUE error.
Geoff Langbaadf232014-08-04 13:58:02 -0400209 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalFormat);
Geoff Lang5d601382014-07-22 15:14:06 -0400210 if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions()))
211 {
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500212 context->recordError(Error(GL_INVALID_VALUE));
Geoff Langb1196682014-07-23 13:47:29 -0400213 return false;
Geoff Lang5d601382014-07-22 15:14:06 -0400214 }
215
216 // The type and format are valid if any supported internal format has that type and format
217 bool formatSupported = false;
218 bool typeSupported = false;
219
220 static const ES3FormatCombinationSet es3FormatSet = BuildES3FormatSet();
221 for (ES3FormatCombinationSet::const_iterator i = es3FormatSet.begin(); i != es3FormatSet.end(); i++)
222 {
223 if (i->format == format || i->type == type)
224 {
225 const gl::InternalFormat &info = gl::GetInternalFormatInfo(i->internalFormat);
226 bool supported = info.textureSupport(context->getClientVersion(), context->getExtensions());
Geoff Langbaadf232014-08-04 13:58:02 -0400227 if (supported && i->type == type)
Geoff Lang5d601382014-07-22 15:14:06 -0400228 {
229 typeSupported = true;
230 }
Geoff Langbaadf232014-08-04 13:58:02 -0400231 if (supported && i->format == format)
Geoff Lang5d601382014-07-22 15:14:06 -0400232 {
233 formatSupported = true;
234 }
Geoff Langbaadf232014-08-04 13:58:02 -0400235
236 // Early-out if both type and format are supported now
237 if (typeSupported && formatSupported)
238 {
239 break;
240 }
Geoff Lang5d601382014-07-22 15:14:06 -0400241 }
242 }
243
244 if (!typeSupported || !formatSupported)
245 {
Geoff Langb1196682014-07-23 13:47:29 -0400246 context->recordError(Error(GL_INVALID_ENUM));
247 return false;
Geoff Lang5d601382014-07-22 15:14:06 -0400248 }
249
250 // Check if this is a valid format combination to load texture data
251 ES3FormatCombination searchFormat;
252 searchFormat.internalFormat = internalFormat;
253 searchFormat.format = format;
254 searchFormat.type = type;
255
256 if (es3FormatSet.find(searchFormat) == es3FormatSet.end())
257 {
Geoff Langb1196682014-07-23 13:47:29 -0400258 context->recordError(Error(GL_INVALID_OPERATION));
259 return false;
Geoff Lang5d601382014-07-22 15:14:06 -0400260 }
261
262 return true;
263}
264
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500265bool ValidateES3TexImageParametersBase(Context *context,
266 GLenum target,
267 GLint level,
268 GLenum internalformat,
269 bool isCompressed,
270 bool isSubImage,
271 GLint xoffset,
272 GLint yoffset,
273 GLint zoffset,
274 GLsizei width,
275 GLsizei height,
276 GLsizei depth,
277 GLint border,
278 GLenum format,
279 GLenum type,
280 const GLvoid *pixels)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400281{
282 // Validate image size
Austin Kinross08528e12015-10-07 16:24:40 -0700283 if (!ValidImageSizeParameters(context, target, level, width, height, depth, isSubImage))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400284 {
Geoff Langb1196682014-07-23 13:47:29 -0400285 context->recordError(Error(GL_INVALID_VALUE));
286 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400287 }
288
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400289 // Verify zero border
290 if (border != 0)
291 {
Geoff Langb1196682014-07-23 13:47:29 -0400292 context->recordError(Error(GL_INVALID_VALUE));
293 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400294 }
295
Jamie Madill6f38f822014-06-06 17:12:20 -0400296 if (xoffset < 0 || yoffset < 0 || zoffset < 0 ||
297 std::numeric_limits<GLsizei>::max() - xoffset < width ||
298 std::numeric_limits<GLsizei>::max() - yoffset < height ||
299 std::numeric_limits<GLsizei>::max() - zoffset < depth)
300 {
Geoff Langb1196682014-07-23 13:47:29 -0400301 context->recordError(Error(GL_INVALID_VALUE));
302 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -0400303 }
304
Geoff Langaae65a42014-05-26 12:43:44 -0400305 const gl::Caps &caps = context->getCaps();
306
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400307 switch (target)
308 {
309 case GL_TEXTURE_2D:
Geoff Langa9be0dc2014-12-17 12:34:40 -0500310 if (static_cast<GLuint>(width) > (caps.max2DTextureSize >> level) ||
311 static_cast<GLuint>(height) > (caps.max2DTextureSize >> level))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400312 {
Geoff Langa9be0dc2014-12-17 12:34:40 -0500313 context->recordError(Error(GL_INVALID_VALUE));
314 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400315 }
316 break;
317
318 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
319 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
320 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
321 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
322 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
323 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
Geoff Langa9be0dc2014-12-17 12:34:40 -0500324 if (!isSubImage && width != height)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400325 {
Geoff Langa9be0dc2014-12-17 12:34:40 -0500326 context->recordError(Error(GL_INVALID_VALUE));
327 return false;
328 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400329
Geoff Langa9be0dc2014-12-17 12:34:40 -0500330 if (static_cast<GLuint>(width) > (caps.maxCubeMapTextureSize >> level))
331 {
332 context->recordError(Error(GL_INVALID_VALUE));
333 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400334 }
335 break;
336
337 case GL_TEXTURE_3D:
Geoff Langa9be0dc2014-12-17 12:34:40 -0500338 if (static_cast<GLuint>(width) > (caps.max3DTextureSize >> level) ||
339 static_cast<GLuint>(height) > (caps.max3DTextureSize >> level) ||
340 static_cast<GLuint>(depth) > (caps.max3DTextureSize >> level))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400341 {
Geoff Langa9be0dc2014-12-17 12:34:40 -0500342 context->recordError(Error(GL_INVALID_VALUE));
343 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400344 }
345 break;
346
Geoff Langa9be0dc2014-12-17 12:34:40 -0500347 case GL_TEXTURE_2D_ARRAY:
348 if (static_cast<GLuint>(width) > (caps.max2DTextureSize >> level) ||
349 static_cast<GLuint>(height) > (caps.max2DTextureSize >> level) ||
Geoff Langb92c1332015-09-04 12:54:55 -0400350 static_cast<GLuint>(depth) > caps.maxArrayTextureLayers)
Geoff Langa9be0dc2014-12-17 12:34:40 -0500351 {
352 context->recordError(Error(GL_INVALID_VALUE));
353 return false;
354 }
355 break;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400356
357 default:
Geoff Langb1196682014-07-23 13:47:29 -0400358 context->recordError(Error(GL_INVALID_ENUM));
359 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400360 }
361
Geoff Lang691e58c2014-12-19 17:03:25 -0500362 gl::Texture *texture = context->getTargetTexture(IsCubeMapTextureTarget(target) ? GL_TEXTURE_CUBE_MAP : target);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400363 if (!texture)
364 {
Geoff Langb1196682014-07-23 13:47:29 -0400365 context->recordError(Error(GL_INVALID_OPERATION));
366 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400367 }
368
Geoff Lang69cce582015-09-17 13:20:36 -0400369 if (texture->getImmutableFormat() && !isSubImage)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400370 {
Geoff Langb1196682014-07-23 13:47:29 -0400371 context->recordError(Error(GL_INVALID_OPERATION));
372 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400373 }
374
375 // Validate texture formats
Geoff Langa9be0dc2014-12-17 12:34:40 -0500376 GLenum actualInternalFormat = isSubImage ? texture->getInternalFormat(target, level) : internalformat;
Geoff Lang5d601382014-07-22 15:14:06 -0400377 const gl::InternalFormat &actualFormatInfo = gl::GetInternalFormatInfo(actualInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400378 if (isCompressed)
379 {
tmartino7c102692015-10-02 16:43:40 -0400380 if (!actualFormatInfo.compressed)
Geoff Langd4f180b2013-09-24 13:57:44 -0400381 {
tmartino7c102692015-10-02 16:43:40 -0400382 context->recordError(Error(
383 GL_INVALID_ENUM, "internalformat is not a supported compressed internal format."));
Geoff Langb1196682014-07-23 13:47:29 -0400384 return false;
Geoff Langd4f180b2013-09-24 13:57:44 -0400385 }
386
tmartino7c102692015-10-02 16:43:40 -0400387 if (!ValidCompressedImageSize(context, actualInternalFormat, width, height))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400388 {
tmartino7c102692015-10-02 16:43:40 -0400389 context->recordError(Error(GL_INVALID_OPERATION));
Geoff Langb1196682014-07-23 13:47:29 -0400390 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400391 }
392
Geoff Lang839ce0b2015-10-23 13:13:12 -0400393 if (!actualFormatInfo.textureSupport(context->getClientVersion(), context->getExtensions()))
394 {
395 context->recordError(Error(GL_INVALID_ENUM));
396 return false;
397 }
398
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400399 if (target == GL_TEXTURE_3D)
400 {
Geoff Langb1196682014-07-23 13:47:29 -0400401 context->recordError(Error(GL_INVALID_OPERATION));
402 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400403 }
404 }
405 else
406 {
Geoff Langbaadf232014-08-04 13:58:02 -0400407 if (!ValidateTexImageFormatCombination(context, actualInternalFormat, format, type))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400408 {
Geoff Lang5d601382014-07-22 15:14:06 -0400409 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400410 }
411
412 if (target == GL_TEXTURE_3D && (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL))
413 {
Geoff Langb1196682014-07-23 13:47:29 -0400414 context->recordError(Error(GL_INVALID_OPERATION));
415 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400416 }
417 }
418
419 // Validate sub image parameters
420 if (isSubImage)
421 {
Geoff Langa9be0dc2014-12-17 12:34:40 -0500422 if (isCompressed != actualFormatInfo.compressed)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400423 {
Geoff Langb1196682014-07-23 13:47:29 -0400424 context->recordError(Error(GL_INVALID_OPERATION));
425 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400426 }
427
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400428 if (width == 0 || height == 0 || depth == 0)
429 {
430 return false;
431 }
432
433 if (xoffset < 0 || yoffset < 0 || zoffset < 0)
434 {
Geoff Langb1196682014-07-23 13:47:29 -0400435 context->recordError(Error(GL_INVALID_VALUE));
436 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400437 }
438
439 if (std::numeric_limits<GLsizei>::max() - xoffset < width ||
440 std::numeric_limits<GLsizei>::max() - yoffset < height ||
441 std::numeric_limits<GLsizei>::max() - zoffset < depth)
442 {
Geoff Langb1196682014-07-23 13:47:29 -0400443 context->recordError(Error(GL_INVALID_VALUE));
444 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400445 }
446
Geoff Langa9be0dc2014-12-17 12:34:40 -0500447 if (static_cast<size_t>(xoffset + width) > texture->getWidth(target, level) ||
448 static_cast<size_t>(yoffset + height) > texture->getHeight(target, level) ||
449 static_cast<size_t>(zoffset + depth) > texture->getDepth(target, level))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400450 {
Geoff Langb1196682014-07-23 13:47:29 -0400451 context->recordError(Error(GL_INVALID_VALUE));
452 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400453 }
454 }
455
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400456 // Check for pixel unpack buffer related API errors
Shannon Woods53a94a82014-06-24 15:20:36 -0400457 gl::Buffer *pixelUnpackBuffer = context->getState().getTargetBuffer(GL_PIXEL_UNPACK_BUFFER);
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400458 if (pixelUnpackBuffer != NULL)
459 {
460 // ...the data would be unpacked from the buffer object such that the memory reads required
461 // would exceed the data store size.
462 size_t widthSize = static_cast<size_t>(width);
463 size_t heightSize = static_cast<size_t>(height);
464 size_t depthSize = static_cast<size_t>(depth);
Geoff Lang5d601382014-07-22 15:14:06 -0400465 GLenum sizedFormat = GetSizedInternalFormat(actualInternalFormat, type);
Jamie Madill6f38f822014-06-06 17:12:20 -0400466
Geoff Lang5d601382014-07-22 15:14:06 -0400467 size_t pixelBytes = static_cast<size_t>(gl::GetInternalFormatInfo(sizedFormat).pixelBytes);
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400468
469 if (!rx::IsUnsignedMultiplicationSafe(widthSize, heightSize) ||
470 !rx::IsUnsignedMultiplicationSafe(widthSize * heightSize, depthSize) ||
471 !rx::IsUnsignedMultiplicationSafe(widthSize * heightSize * depthSize, pixelBytes))
472 {
473 // Overflow past the end of the buffer
Geoff Langb1196682014-07-23 13:47:29 -0400474 context->recordError(Error(GL_INVALID_OPERATION));
475 return false;
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400476 }
477
Jamie Madillc751d1e2014-10-21 17:46:29 -0400478 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(sizedFormat);
479 size_t copyBytes = formatInfo.computeBlockSize(type, width, height);
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400480 size_t offset = reinterpret_cast<size_t>(pixels);
481
Jamie Madill6f38f822014-06-06 17:12:20 -0400482 if (!rx::IsUnsignedAdditionSafe(offset, copyBytes) ||
Brandon Jonesd38f9262014-06-18 16:26:45 -0700483 ((offset + copyBytes) > static_cast<size_t>(pixelUnpackBuffer->getSize())))
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400484 {
485 // Overflow past the end of the buffer
Geoff Langb1196682014-07-23 13:47:29 -0400486 context->recordError(Error(GL_INVALID_OPERATION));
487 return false;
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400488 }
489
490 // ...data is not evenly divisible into the number of bytes needed to store in memory a datum
491 // indicated by type.
Jamie Madillc751d1e2014-10-21 17:46:29 -0400492 if (!isCompressed)
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400493 {
Jamie Madillc751d1e2014-10-21 17:46:29 -0400494 size_t dataBytesPerPixel = static_cast<size_t>(gl::GetTypeInfo(type).bytes);
495
496 if ((offset % dataBytesPerPixel) != 0)
497 {
498 context->recordError(Error(GL_INVALID_OPERATION));
499 return false;
500 }
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400501 }
502
Jamie Madill7a5f7382014-03-05 15:01:24 -0500503 // ...the buffer object's data store is currently mapped.
Brandon Jonesd38f9262014-06-18 16:26:45 -0700504 if (pixelUnpackBuffer->isMapped())
Jamie Madill7a5f7382014-03-05 15:01:24 -0500505 {
Geoff Langb1196682014-07-23 13:47:29 -0400506 context->recordError(Error(GL_INVALID_OPERATION));
507 return false;
Jamie Madill7a5f7382014-03-05 15:01:24 -0500508 }
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400509 }
510
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400511 return true;
512}
513
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500514bool ValidateES3TexImage2DParameters(Context *context,
515 GLenum target,
516 GLint level,
517 GLenum internalformat,
518 bool isCompressed,
519 bool isSubImage,
520 GLint xoffset,
521 GLint yoffset,
522 GLint zoffset,
523 GLsizei width,
524 GLsizei height,
525 GLsizei depth,
526 GLint border,
527 GLenum format,
528 GLenum type,
529 const GLvoid *pixels)
530{
531 if (!ValidTexture2DDestinationTarget(context, target))
532 {
533 context->recordError(Error(GL_INVALID_ENUM));
534 return false;
535 }
536
537 return ValidateES3TexImageParametersBase(context, target, level, internalformat, isCompressed,
538 isSubImage, xoffset, yoffset, zoffset, width, height,
539 depth, border, format, type, pixels);
540}
541
542bool ValidateES3TexImage3DParameters(Context *context,
543 GLenum target,
544 GLint level,
545 GLenum internalformat,
546 bool isCompressed,
547 bool isSubImage,
548 GLint xoffset,
549 GLint yoffset,
550 GLint zoffset,
551 GLsizei width,
552 GLsizei height,
553 GLsizei depth,
554 GLint border,
555 GLenum format,
556 GLenum type,
557 const GLvoid *pixels)
558{
559 if (!ValidTexture3DDestinationTarget(context, target))
560 {
561 context->recordError(Error(GL_INVALID_ENUM));
562 return false;
563 }
564
565 return ValidateES3TexImageParametersBase(context, target, level, internalformat, isCompressed,
566 isSubImage, xoffset, yoffset, zoffset, width, height,
567 depth, border, format, type, pixels);
568}
569
Geoff Lang5d601382014-07-22 15:14:06 -0400570struct EffectiveInternalFormatInfo
571{
572 GLenum mEffectiveFormat;
573 GLenum mDestFormat;
574 GLuint mMinRedBits;
575 GLuint mMaxRedBits;
576 GLuint mMinGreenBits;
577 GLuint mMaxGreenBits;
578 GLuint mMinBlueBits;
579 GLuint mMaxBlueBits;
580 GLuint mMinAlphaBits;
581 GLuint mMaxAlphaBits;
582
583 EffectiveInternalFormatInfo(GLenum effectiveFormat, GLenum destFormat, GLuint minRedBits, GLuint maxRedBits,
584 GLuint minGreenBits, GLuint maxGreenBits, GLuint minBlueBits, GLuint maxBlueBits,
585 GLuint minAlphaBits, GLuint maxAlphaBits)
586 : mEffectiveFormat(effectiveFormat), mDestFormat(destFormat), mMinRedBits(minRedBits),
587 mMaxRedBits(maxRedBits), mMinGreenBits(minGreenBits), mMaxGreenBits(maxGreenBits),
588 mMinBlueBits(minBlueBits), mMaxBlueBits(maxBlueBits), mMinAlphaBits(minAlphaBits),
589 mMaxAlphaBits(maxAlphaBits) {};
590};
591
592typedef std::vector<EffectiveInternalFormatInfo> EffectiveInternalFormatList;
593
594static EffectiveInternalFormatList BuildSizedEffectiveInternalFormatList()
595{
596 EffectiveInternalFormatList list;
597
598 // OpenGL ES 3.0.3 Specification, Table 3.17, pg 141: Effective internal format coresponding to destination internal format and
599 // linear source buffer component sizes.
600 // | Source channel min/max sizes |
601 // Effective Internal Format | N/A | R | G | B | A |
602 list.push_back(EffectiveInternalFormatInfo(GL_ALPHA8_EXT, GL_NONE, 0, 0, 0, 0, 0, 0, 1, 8));
603 list.push_back(EffectiveInternalFormatInfo(GL_R8, GL_NONE, 1, 8, 0, 0, 0, 0, 0, 0));
604 list.push_back(EffectiveInternalFormatInfo(GL_RG8, GL_NONE, 1, 8, 1, 8, 0, 0, 0, 0));
605 list.push_back(EffectiveInternalFormatInfo(GL_RGB565, GL_NONE, 1, 5, 1, 6, 1, 5, 0, 0));
606 list.push_back(EffectiveInternalFormatInfo(GL_RGB8, GL_NONE, 6, 8, 7, 8, 6, 8, 0, 0));
607 list.push_back(EffectiveInternalFormatInfo(GL_RGBA4, GL_NONE, 1, 4, 1, 4, 1, 4, 1, 4));
608 list.push_back(EffectiveInternalFormatInfo(GL_RGB5_A1, GL_NONE, 5, 5, 5, 5, 5, 5, 1, 1));
609 list.push_back(EffectiveInternalFormatInfo(GL_RGBA8, GL_NONE, 5, 8, 5, 8, 5, 8, 2, 8));
610 list.push_back(EffectiveInternalFormatInfo(GL_RGB10_A2, GL_NONE, 9, 10, 9, 10, 9, 10, 2, 2));
611
612 return list;
613}
614
615static EffectiveInternalFormatList BuildUnsizedEffectiveInternalFormatList()
616{
617 EffectiveInternalFormatList list;
618
619 // OpenGL ES 3.0.3 Specification, Table 3.17, pg 141: Effective internal format coresponding to destination internal format and
620 // linear source buffer component sizes.
621 // | Source channel min/max sizes |
622 // Effective Internal Format | Dest Format | R | G | B | A |
623 list.push_back(EffectiveInternalFormatInfo(GL_ALPHA8_EXT, GL_ALPHA, 0, UINT_MAX, 0, UINT_MAX, 0, UINT_MAX, 1, 8));
624 list.push_back(EffectiveInternalFormatInfo(GL_LUMINANCE8_EXT, GL_LUMINANCE, 1, 8, 0, UINT_MAX, 0, UINT_MAX, 0, UINT_MAX));
625 list.push_back(EffectiveInternalFormatInfo(GL_LUMINANCE8_ALPHA8_EXT, GL_LUMINANCE_ALPHA, 1, 8, 0, UINT_MAX, 0, UINT_MAX, 1, 8));
626 list.push_back(EffectiveInternalFormatInfo(GL_RGB565, GL_RGB, 1, 5, 1, 6, 1, 5, 0, UINT_MAX));
627 list.push_back(EffectiveInternalFormatInfo(GL_RGB8, GL_RGB, 6, 8, 7, 8, 6, 8, 0, UINT_MAX));
628 list.push_back(EffectiveInternalFormatInfo(GL_RGBA4, GL_RGBA, 1, 4, 1, 4, 1, 4, 1, 4));
629 list.push_back(EffectiveInternalFormatInfo(GL_RGB5_A1, GL_RGBA, 5, 5, 5, 5, 5, 5, 1, 1));
630 list.push_back(EffectiveInternalFormatInfo(GL_RGBA8, GL_RGBA, 5, 8, 5, 8, 5, 8, 5, 8));
631
632 return list;
633}
634
635static bool GetEffectiveInternalFormat(const InternalFormat &srcFormat, const InternalFormat &destFormat,
636 GLenum *outEffectiveFormat)
637{
638 const EffectiveInternalFormatList *list = NULL;
639 GLenum targetFormat = GL_NONE;
640
641 if (destFormat.pixelBytes > 0)
642 {
643 static const EffectiveInternalFormatList sizedList = BuildSizedEffectiveInternalFormatList();
644 list = &sizedList;
645 }
646 else
647 {
648 static const EffectiveInternalFormatList unsizedList = BuildUnsizedEffectiveInternalFormatList();
649 list = &unsizedList;
650 targetFormat = destFormat.format;
651 }
652
653 for (size_t curFormat = 0; curFormat < list->size(); ++curFormat)
654 {
655 const EffectiveInternalFormatInfo& formatInfo = list->at(curFormat);
656 if ((formatInfo.mDestFormat == targetFormat) &&
657 (formatInfo.mMinRedBits <= srcFormat.redBits && formatInfo.mMaxRedBits >= srcFormat.redBits) &&
658 (formatInfo.mMinGreenBits <= srcFormat.greenBits && formatInfo.mMaxGreenBits >= srcFormat.greenBits) &&
659 (formatInfo.mMinBlueBits <= srcFormat.blueBits && formatInfo.mMaxBlueBits >= srcFormat.blueBits) &&
660 (formatInfo.mMinAlphaBits <= srcFormat.alphaBits && formatInfo.mMaxAlphaBits >= srcFormat.alphaBits))
661 {
662 *outEffectiveFormat = formatInfo.mEffectiveFormat;
663 return true;
664 }
665 }
666
667 return false;
668}
669
670struct CopyConversion
671{
672 GLenum mTextureFormat;
673 GLenum mFramebufferFormat;
674
675 CopyConversion(GLenum textureFormat, GLenum framebufferFormat)
676 : mTextureFormat(textureFormat), mFramebufferFormat(framebufferFormat) { }
677
678 bool operator<(const CopyConversion& other) const
679 {
680 return memcmp(this, &other, sizeof(CopyConversion)) < 0;
681 }
682};
683
684typedef std::set<CopyConversion> CopyConversionSet;
685
686static CopyConversionSet BuildValidES3CopyTexImageCombinations()
687{
688 CopyConversionSet set;
689
690 // From ES 3.0.1 spec, table 3.15
691 set.insert(CopyConversion(GL_ALPHA, GL_RGBA));
692 set.insert(CopyConversion(GL_LUMINANCE, GL_RED));
693 set.insert(CopyConversion(GL_LUMINANCE, GL_RG));
694 set.insert(CopyConversion(GL_LUMINANCE, GL_RGB));
695 set.insert(CopyConversion(GL_LUMINANCE, GL_RGBA));
696 set.insert(CopyConversion(GL_LUMINANCE_ALPHA, GL_RGBA));
697 set.insert(CopyConversion(GL_RED, GL_RED));
698 set.insert(CopyConversion(GL_RED, GL_RG));
699 set.insert(CopyConversion(GL_RED, GL_RGB));
700 set.insert(CopyConversion(GL_RED, GL_RGBA));
701 set.insert(CopyConversion(GL_RG, GL_RG));
702 set.insert(CopyConversion(GL_RG, GL_RGB));
703 set.insert(CopyConversion(GL_RG, GL_RGBA));
704 set.insert(CopyConversion(GL_RGB, GL_RGB));
705 set.insert(CopyConversion(GL_RGB, GL_RGBA));
706 set.insert(CopyConversion(GL_RGBA, GL_RGBA));
707
708 // Necessary for ANGLE back-buffers
709 set.insert(CopyConversion(GL_ALPHA, GL_BGRA_EXT));
710 set.insert(CopyConversion(GL_LUMINANCE, GL_BGRA_EXT));
711 set.insert(CopyConversion(GL_LUMINANCE_ALPHA, GL_BGRA_EXT));
712 set.insert(CopyConversion(GL_RED, GL_BGRA_EXT));
713 set.insert(CopyConversion(GL_RG, GL_BGRA_EXT));
714 set.insert(CopyConversion(GL_RGB, GL_BGRA_EXT));
715 set.insert(CopyConversion(GL_RGBA, GL_BGRA_EXT));
716
717 set.insert(CopyConversion(GL_RED_INTEGER, GL_RED_INTEGER));
718 set.insert(CopyConversion(GL_RED_INTEGER, GL_RG_INTEGER));
719 set.insert(CopyConversion(GL_RED_INTEGER, GL_RGB_INTEGER));
720 set.insert(CopyConversion(GL_RED_INTEGER, GL_RGBA_INTEGER));
721 set.insert(CopyConversion(GL_RG_INTEGER, GL_RG_INTEGER));
722 set.insert(CopyConversion(GL_RG_INTEGER, GL_RGB_INTEGER));
723 set.insert(CopyConversion(GL_RG_INTEGER, GL_RGBA_INTEGER));
724 set.insert(CopyConversion(GL_RGB_INTEGER, GL_RGB_INTEGER));
725 set.insert(CopyConversion(GL_RGB_INTEGER, GL_RGBA_INTEGER));
726 set.insert(CopyConversion(GL_RGBA_INTEGER, GL_RGBA_INTEGER));
727
728 return set;
729}
730
731static bool IsValidES3CopyTexImageCombination(GLenum textureInternalFormat, GLenum frameBufferInternalFormat, GLuint readBufferHandle)
732{
733 const InternalFormat &textureInternalFormatInfo = GetInternalFormatInfo(textureInternalFormat);
734 const InternalFormat &framebufferInternalFormatInfo = GetInternalFormatInfo(frameBufferInternalFormat);
735
736 static const CopyConversionSet conversionSet = BuildValidES3CopyTexImageCombinations();
737 if (conversionSet.find(CopyConversion(textureInternalFormatInfo.format, framebufferInternalFormatInfo.format)) != conversionSet.end())
738 {
739 // Section 3.8.5 of the GLES 3.0.3 spec states that source and destination formats
740 // must both be signed, unsigned, or fixed point and both source and destinations
741 // must be either both SRGB or both not SRGB. EXT_color_buffer_float adds allowed
742 // conversion between fixed and floating point.
743
744 if ((textureInternalFormatInfo.colorEncoding == GL_SRGB) != (framebufferInternalFormatInfo.colorEncoding == GL_SRGB))
745 {
746 return false;
747 }
748
749 if (((textureInternalFormatInfo.componentType == GL_INT) != (framebufferInternalFormatInfo.componentType == GL_INT )) ||
750 ((textureInternalFormatInfo.componentType == GL_UNSIGNED_INT) != (framebufferInternalFormatInfo.componentType == GL_UNSIGNED_INT)))
751 {
752 return false;
753 }
754
755 if ((textureInternalFormatInfo.componentType == GL_UNSIGNED_NORMALIZED ||
756 textureInternalFormatInfo.componentType == GL_SIGNED_NORMALIZED ||
757 textureInternalFormatInfo.componentType == GL_FLOAT) &&
758 !(framebufferInternalFormatInfo.componentType == GL_UNSIGNED_NORMALIZED ||
759 framebufferInternalFormatInfo.componentType == GL_SIGNED_NORMALIZED ||
760 framebufferInternalFormatInfo.componentType == GL_FLOAT))
761 {
762 return false;
763 }
764
765 // GLES specification 3.0.3, sec 3.8.5, pg 139-140:
766 // The effective internal format of the source buffer is determined with the following rules applied in order:
767 // * If the source buffer is a texture or renderbuffer that was created with a sized internal format then the
768 // effective internal format is the source buffer's sized internal format.
769 // * If the source buffer is a texture that was created with an unsized base internal format, then the
770 // effective internal format is the source image array's effective internal format, as specified by table
771 // 3.12, which is determined from the <format> and <type> that were used when the source image array was
772 // specified by TexImage*.
773 // * Otherwise the effective internal format is determined by the row in table 3.17 or 3.18 where
774 // Destination Internal Format matches internalformat and where the [source channel sizes] are consistent
775 // with the values of the source buffer's [channel sizes]. Table 3.17 is used if the
776 // FRAMEBUFFER_ATTACHMENT_ENCODING is LINEAR and table 3.18 is used if the FRAMEBUFFER_ATTACHMENT_ENCODING
777 // is SRGB.
778 const InternalFormat *sourceEffectiveFormat = NULL;
779 if (readBufferHandle != 0)
780 {
781 // Not the default framebuffer, therefore the read buffer must be a user-created texture or renderbuffer
782 if (framebufferInternalFormatInfo.pixelBytes > 0)
783 {
784 sourceEffectiveFormat = &framebufferInternalFormatInfo;
785 }
786 else
787 {
788 // Renderbuffers cannot be created with an unsized internal format, so this must be an unsized-format
789 // texture. We can use the same table we use when creating textures to get its effective sized format.
Geoff Lang051dbc72015-01-05 15:48:58 -0500790 GLenum sizedInternalFormat = GetSizedInternalFormat(framebufferInternalFormatInfo.format, framebufferInternalFormatInfo.type);
791 sourceEffectiveFormat = &GetInternalFormatInfo(sizedInternalFormat);
Geoff Lang5d601382014-07-22 15:14:06 -0400792 }
793 }
794 else
795 {
796 // The effective internal format must be derived from the source framebuffer's channel sizes.
797 // This is done in GetEffectiveInternalFormat for linear buffers (table 3.17)
798 if (framebufferInternalFormatInfo.colorEncoding == GL_LINEAR)
799 {
800 GLenum effectiveFormat;
801 if (GetEffectiveInternalFormat(framebufferInternalFormatInfo, textureInternalFormatInfo, &effectiveFormat))
802 {
803 sourceEffectiveFormat = &GetInternalFormatInfo(effectiveFormat);
804 }
805 else
806 {
807 return false;
808 }
809 }
810 else if (framebufferInternalFormatInfo.colorEncoding == GL_SRGB)
811 {
812 // SRGB buffers can only be copied to sized format destinations according to table 3.18
813 if ((textureInternalFormatInfo.pixelBytes > 0) &&
814 (framebufferInternalFormatInfo.redBits >= 1 && framebufferInternalFormatInfo.redBits <= 8) &&
815 (framebufferInternalFormatInfo.greenBits >= 1 && framebufferInternalFormatInfo.greenBits <= 8) &&
816 (framebufferInternalFormatInfo.blueBits >= 1 && framebufferInternalFormatInfo.blueBits <= 8) &&
817 (framebufferInternalFormatInfo.alphaBits >= 1 && framebufferInternalFormatInfo.alphaBits <= 8))
818 {
819 sourceEffectiveFormat = &GetInternalFormatInfo(GL_SRGB8_ALPHA8);
820 }
821 else
822 {
823 return false;
824 }
825 }
826 else
827 {
828 UNREACHABLE();
829 return false;
830 }
831 }
832
833 if (textureInternalFormatInfo.pixelBytes > 0)
834 {
835 // Section 3.8.5 of the GLES 3.0.3 spec, pg 139, requires that, if the destination format is sized,
836 // component sizes of the source and destination formats must exactly match
837 if (textureInternalFormatInfo.redBits != sourceEffectiveFormat->redBits ||
838 textureInternalFormatInfo.greenBits != sourceEffectiveFormat->greenBits ||
839 textureInternalFormatInfo.blueBits != sourceEffectiveFormat->blueBits ||
840 textureInternalFormatInfo.alphaBits != sourceEffectiveFormat->alphaBits)
841 {
842 return false;
843 }
844 }
845
846
847 return true; // A conversion function exists, and no rule in the specification has precluded conversion
848 // between these formats.
849 }
850
851 return false;
852}
853
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500854bool ValidateES3CopyTexImageParametersBase(ValidationContext *context,
855 GLenum target,
856 GLint level,
857 GLenum internalformat,
858 bool isSubImage,
859 GLint xoffset,
860 GLint yoffset,
861 GLint zoffset,
862 GLint x,
863 GLint y,
864 GLsizei width,
865 GLsizei height,
866 GLint border)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400867{
Jamie Madill560a8d82014-05-21 13:06:20 -0400868 GLenum textureInternalFormat;
869 if (!ValidateCopyTexImageParametersBase(context, target, level, internalformat, isSubImage,
Jamie Madill6f38f822014-06-06 17:12:20 -0400870 xoffset, yoffset, zoffset, x, y, width, height,
871 border, &textureInternalFormat))
Shannon Woods4dfed832014-03-17 20:03:39 -0400872 {
Jamie Madill560a8d82014-05-21 13:06:20 -0400873 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400874 }
875
Jamie Madillc29968b2016-01-20 11:17:23 -0500876 const auto &state = context->getState();
877 const gl::Framebuffer *framebuffer = state.getReadFramebuffer();
878 GLuint readFramebufferID = framebuffer->id();
Jamie Madill3c7fa222014-06-05 13:08:51 -0400879
Geoff Lang748f74e2014-12-01 11:25:34 -0500880 if (framebuffer->checkStatus(context->getData()) != GL_FRAMEBUFFER_COMPLETE)
Jamie Madill3c7fa222014-06-05 13:08:51 -0400881 {
Geoff Langb1196682014-07-23 13:47:29 -0400882 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
883 return false;
Jamie Madill3c7fa222014-06-05 13:08:51 -0400884 }
885
Jamie Madillc29968b2016-01-20 11:17:23 -0500886 if (readFramebufferID != 0 && framebuffer->getSamples(context->getData()) != 0)
Jamie Madill3c7fa222014-06-05 13:08:51 -0400887 {
Geoff Langb1196682014-07-23 13:47:29 -0400888 context->recordError(Error(GL_INVALID_OPERATION));
889 return false;
Jamie Madill3c7fa222014-06-05 13:08:51 -0400890 }
891
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400892 const gl::FramebufferAttachment *source = framebuffer->getReadColorbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400893 GLenum colorbufferInternalFormat = source->getInternalFormat();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400894
895 if (isSubImage)
896 {
Geoff Lang5d601382014-07-22 15:14:06 -0400897 if (!IsValidES3CopyTexImageCombination(textureInternalFormat, colorbufferInternalFormat,
Jamie Madillc29968b2016-01-20 11:17:23 -0500898 readFramebufferID))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400899 {
Geoff Langb1196682014-07-23 13:47:29 -0400900 context->recordError(Error(GL_INVALID_OPERATION));
901 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400902 }
903 }
Shannon Woods4d161ba2014-03-17 18:13:30 -0400904 else
905 {
Geoff Lang5d601382014-07-22 15:14:06 -0400906 if (!gl::IsValidES3CopyTexImageCombination(internalformat, colorbufferInternalFormat,
Jamie Madillc29968b2016-01-20 11:17:23 -0500907 readFramebufferID))
Shannon Woods4d161ba2014-03-17 18:13:30 -0400908 {
Geoff Langb1196682014-07-23 13:47:29 -0400909 context->recordError(Error(GL_INVALID_OPERATION));
910 return false;
Shannon Woods4d161ba2014-03-17 18:13:30 -0400911 }
912 }
913
Geoff Lang784a8fd2013-09-24 12:33:16 -0400914 // If width or height is zero, it is a no-op. Return false without setting an error.
915 return (width > 0 && height > 0);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400916}
917
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500918bool ValidateES3CopyTexImage2DParameters(ValidationContext *context,
919 GLenum target,
920 GLint level,
921 GLenum internalformat,
922 bool isSubImage,
923 GLint xoffset,
924 GLint yoffset,
925 GLint zoffset,
926 GLint x,
927 GLint y,
928 GLsizei width,
929 GLsizei height,
930 GLint border)
931{
932 if (!ValidTexture2DDestinationTarget(context, target))
933 {
934 context->recordError(Error(GL_INVALID_ENUM));
935 return false;
936 }
937
938 return ValidateES3CopyTexImageParametersBase(context, target, level, internalformat, isSubImage,
939 xoffset, yoffset, zoffset, x, y, width, height,
940 border);
941}
942
943bool ValidateES3CopyTexImage3DParameters(ValidationContext *context,
944 GLenum target,
945 GLint level,
946 GLenum internalformat,
947 bool isSubImage,
948 GLint xoffset,
949 GLint yoffset,
950 GLint zoffset,
951 GLint x,
952 GLint y,
953 GLsizei width,
954 GLsizei height,
955 GLint border)
956{
957 if (!ValidTexture3DDestinationTarget(context, target))
958 {
959 context->recordError(Error(GL_INVALID_ENUM));
960 return false;
961 }
962
963 return ValidateES3CopyTexImageParametersBase(context, target, level, internalformat, isSubImage,
964 xoffset, yoffset, zoffset, x, y, width, height,
965 border);
966}
967
968bool ValidateES3TexStorageParametersBase(Context *context,
969 GLenum target,
970 GLsizei levels,
971 GLenum internalformat,
972 GLsizei width,
973 GLsizei height,
974 GLsizei depth)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400975{
976 if (width < 1 || height < 1 || depth < 1 || levels < 1)
977 {
Geoff Langb1196682014-07-23 13:47:29 -0400978 context->recordError(Error(GL_INVALID_VALUE));
979 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400980 }
981
Geoff Langb92c1332015-09-04 12:54:55 -0400982 GLsizei maxDim = std::max(width, height);
983 if (target != GL_TEXTURE_2D_ARRAY)
984 {
985 maxDim = std::max(maxDim, depth);
986 }
987
988 if (levels > gl::log2(maxDim) + 1)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400989 {
Geoff Langb1196682014-07-23 13:47:29 -0400990 context->recordError(Error(GL_INVALID_OPERATION));
991 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400992 }
993
Geoff Langaae65a42014-05-26 12:43:44 -0400994 const gl::Caps &caps = context->getCaps();
995
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400996 switch (target)
997 {
998 case GL_TEXTURE_2D:
999 {
Geoff Langaae65a42014-05-26 12:43:44 -04001000 if (static_cast<GLuint>(width) > caps.max2DTextureSize ||
1001 static_cast<GLuint>(height) > caps.max2DTextureSize)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001002 {
Geoff Langb1196682014-07-23 13:47:29 -04001003 context->recordError(Error(GL_INVALID_VALUE));
1004 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001005 }
1006 }
1007 break;
1008
Geoff Lang01c21d22013-09-24 11:52:16 -04001009 case GL_TEXTURE_CUBE_MAP:
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001010 {
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001011 if (width != height)
1012 {
Geoff Langb1196682014-07-23 13:47:29 -04001013 context->recordError(Error(GL_INVALID_VALUE));
1014 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001015 }
1016
Geoff Langaae65a42014-05-26 12:43:44 -04001017 if (static_cast<GLuint>(width) > caps.maxCubeMapTextureSize)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001018 {
Geoff Langb1196682014-07-23 13:47:29 -04001019 context->recordError(Error(GL_INVALID_VALUE));
1020 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001021 }
1022 }
1023 break;
1024
1025 case GL_TEXTURE_3D:
1026 {
Geoff Langaae65a42014-05-26 12:43:44 -04001027 if (static_cast<GLuint>(width) > caps.max3DTextureSize ||
1028 static_cast<GLuint>(height) > caps.max3DTextureSize ||
1029 static_cast<GLuint>(depth) > caps.max3DTextureSize)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001030 {
Geoff Langb1196682014-07-23 13:47:29 -04001031 context->recordError(Error(GL_INVALID_VALUE));
1032 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001033 }
1034 }
1035 break;
1036
1037 case GL_TEXTURE_2D_ARRAY:
1038 {
Geoff Langaae65a42014-05-26 12:43:44 -04001039 if (static_cast<GLuint>(width) > caps.max2DTextureSize ||
1040 static_cast<GLuint>(height) > caps.max2DTextureSize ||
1041 static_cast<GLuint>(depth) > caps.maxArrayTextureLayers)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001042 {
Geoff Langb1196682014-07-23 13:47:29 -04001043 context->recordError(Error(GL_INVALID_VALUE));
1044 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001045 }
1046 }
1047 break;
1048
1049 default:
Ian Ewellfc7cf8e2016-01-20 15:57:46 -05001050 UNREACHABLE();
Geoff Langb1196682014-07-23 13:47:29 -04001051 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001052 }
1053
Geoff Lang691e58c2014-12-19 17:03:25 -05001054 gl::Texture *texture = context->getTargetTexture(target);
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001055 if (!texture || texture->id() == 0)
1056 {
Geoff Langb1196682014-07-23 13:47:29 -04001057 context->recordError(Error(GL_INVALID_OPERATION));
1058 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001059 }
1060
Geoff Lang69cce582015-09-17 13:20:36 -04001061 if (texture->getImmutableFormat())
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001062 {
Geoff Langb1196682014-07-23 13:47:29 -04001063 context->recordError(Error(GL_INVALID_OPERATION));
1064 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001065 }
1066
Geoff Lang5d601382014-07-22 15:14:06 -04001067 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalformat);
1068 if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions()))
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001069 {
Geoff Langb1196682014-07-23 13:47:29 -04001070 context->recordError(Error(GL_INVALID_ENUM));
1071 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001072 }
1073
Geoff Lang5d601382014-07-22 15:14:06 -04001074 if (formatInfo.pixelBytes == 0)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001075 {
Geoff Langb1196682014-07-23 13:47:29 -04001076 context->recordError(Error(GL_INVALID_ENUM));
1077 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001078 }
1079
1080 return true;
1081}
1082
Ian Ewellfc7cf8e2016-01-20 15:57:46 -05001083bool ValidateES3TexStorage2DParameters(Context *context,
1084 GLenum target,
1085 GLsizei levels,
1086 GLenum internalformat,
1087 GLsizei width,
1088 GLsizei height,
1089 GLsizei depth)
1090{
1091 if (!ValidTexture2DTarget(context, target))
1092 {
1093 context->recordError(Error(GL_INVALID_ENUM));
1094 return false;
1095 }
1096
1097 return ValidateES3TexStorageParametersBase(context, target, levels, internalformat, width,
1098 height, depth);
1099}
1100
1101bool ValidateES3TexStorage3DParameters(Context *context,
1102 GLenum target,
1103 GLsizei levels,
1104 GLenum internalformat,
1105 GLsizei width,
1106 GLsizei height,
1107 GLsizei depth)
1108{
1109 if (!ValidTexture3DTarget(context, target))
1110 {
1111 context->recordError(Error(GL_INVALID_ENUM));
1112 return false;
1113 }
1114
1115 return ValidateES3TexStorageParametersBase(context, target, levels, internalformat, width,
1116 height, depth);
1117}
1118
Geoff Langb1196682014-07-23 13:47:29 -04001119bool ValidateFramebufferTextureLayer(Context *context, GLenum target, GLenum attachment,
Jamie Madill570f7c82014-07-03 10:38:54 -04001120 GLuint texture, GLint level, GLint layer)
1121{
1122 if (context->getClientVersion() < 3)
1123 {
Geoff Langb1196682014-07-23 13:47:29 -04001124 context->recordError(Error(GL_INVALID_OPERATION));
1125 return false;
Jamie Madill570f7c82014-07-03 10:38:54 -04001126 }
1127
Jamie Madill55ec3b12014-07-03 10:38:57 -04001128 if (layer < 0)
1129 {
Geoff Langb1196682014-07-23 13:47:29 -04001130 context->recordError(Error(GL_INVALID_VALUE));
1131 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001132 }
1133
1134 if (!ValidateFramebufferTextureBase(context, target, attachment, texture, level))
1135 {
1136 return false;
1137 }
1138
1139 const gl::Caps &caps = context->getCaps();
1140 if (texture != 0)
1141 {
1142 gl::Texture *tex = context->getTexture(texture);
1143 ASSERT(tex);
1144
1145 switch (tex->getTarget())
1146 {
1147 case GL_TEXTURE_2D_ARRAY:
1148 {
1149 if (level > gl::log2(caps.max2DTextureSize))
1150 {
Geoff Langb1196682014-07-23 13:47:29 -04001151 context->recordError(Error(GL_INVALID_VALUE));
1152 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001153 }
1154
1155 if (static_cast<GLuint>(layer) >= caps.maxArrayTextureLayers)
1156 {
Geoff Langb1196682014-07-23 13:47:29 -04001157 context->recordError(Error(GL_INVALID_VALUE));
1158 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001159 }
Jamie Madill55ec3b12014-07-03 10:38:57 -04001160 }
1161 break;
1162
1163 case GL_TEXTURE_3D:
1164 {
1165 if (level > gl::log2(caps.max3DTextureSize))
1166 {
Geoff Langb1196682014-07-23 13:47:29 -04001167 context->recordError(Error(GL_INVALID_VALUE));
1168 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001169 }
1170
1171 if (static_cast<GLuint>(layer) >= caps.max3DTextureSize)
1172 {
Geoff Langb1196682014-07-23 13:47:29 -04001173 context->recordError(Error(GL_INVALID_VALUE));
1174 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001175 }
Jamie Madill55ec3b12014-07-03 10:38:57 -04001176 }
1177 break;
1178
1179 default:
Geoff Langb1196682014-07-23 13:47:29 -04001180 context->recordError(Error(GL_INVALID_OPERATION));
1181 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001182 }
Geoff Langa9be0dc2014-12-17 12:34:40 -05001183
1184 const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(tex->getInternalFormat(tex->getTarget(), level));
1185 if (internalFormatInfo.compressed)
1186 {
1187 context->recordError(Error(GL_INVALID_OPERATION));
1188 return false;
1189 }
Jamie Madill55ec3b12014-07-03 10:38:57 -04001190 }
1191
1192 return true;
Jamie Madill570f7c82014-07-03 10:38:54 -04001193}
1194
Geoff Langb1196682014-07-23 13:47:29 -04001195bool ValidES3ReadFormatType(Context *context, GLenum internalFormat, GLenum format, GLenum type)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001196{
Geoff Lang5d601382014-07-22 15:14:06 -04001197 const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(internalFormat);
1198
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001199 switch (format)
1200 {
1201 case GL_RGBA:
1202 switch (type)
1203 {
1204 case GL_UNSIGNED_BYTE:
1205 break;
1206 case GL_UNSIGNED_INT_2_10_10_10_REV:
1207 if (internalFormat != GL_RGB10_A2)
1208 {
1209 return false;
1210 }
1211 break;
Geoff Lang1ec57f82013-10-16 11:43:23 -04001212 case GL_FLOAT:
Geoff Lang5d601382014-07-22 15:14:06 -04001213 if (internalFormatInfo.componentType != GL_FLOAT)
Geoff Lang1ec57f82013-10-16 11:43:23 -04001214 {
1215 return false;
1216 }
1217 break;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001218 default:
1219 return false;
1220 }
1221 break;
1222 case GL_RGBA_INTEGER:
1223 switch (type)
1224 {
1225 case GL_INT:
Geoff Lang5d601382014-07-22 15:14:06 -04001226 if (internalFormatInfo.componentType != GL_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001227 {
1228 return false;
1229 }
1230 break;
1231 case GL_UNSIGNED_INT:
Geoff Lang5d601382014-07-22 15:14:06 -04001232 if (internalFormatInfo.componentType != GL_UNSIGNED_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001233 {
1234 return false;
1235 }
1236 break;
1237 default:
1238 return false;
1239 }
1240 break;
1241 case GL_BGRA_EXT:
1242 switch (type)
1243 {
1244 case GL_UNSIGNED_BYTE:
1245 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
1246 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
1247 break;
1248 default:
1249 return false;
1250 }
1251 break;
Geoff Langbdc9b2f2014-04-16 14:41:54 -04001252 case GL_RG_EXT:
1253 case GL_RED_EXT:
Geoff Langc0b9ef42014-07-02 10:02:37 -04001254 if (!context->getExtensions().textureRG)
Geoff Langbdc9b2f2014-04-16 14:41:54 -04001255 {
1256 return false;
1257 }
1258 switch (type)
1259 {
1260 case GL_UNSIGNED_BYTE:
1261 break;
1262 default:
1263 return false;
1264 }
1265 break;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001266 default:
1267 return false;
1268 }
1269 return true;
1270}
1271
Corentin Walleze0902642014-11-04 12:32:15 -08001272bool ValidateES3RenderbufferStorageParameters(gl::Context *context, GLenum target, GLsizei samples,
1273 GLenum internalformat, GLsizei width, GLsizei height)
1274{
1275 if (!ValidateRenderbufferStorageParametersBase(context, target, samples, internalformat, width, height))
1276 {
1277 return false;
1278 }
1279
1280 //The ES3 spec(section 4.4.2) states that the internal format must be sized and not an integer format if samples is greater than zero.
1281 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalformat);
1282 if ((formatInfo.componentType == GL_UNSIGNED_INT || formatInfo.componentType == GL_INT) && samples > 0)
1283 {
1284 context->recordError(Error(GL_INVALID_OPERATION));
1285 return false;
1286 }
1287
1288 // The behavior is different than the ANGLE version, which would generate a GL_OUT_OF_MEMORY.
1289 const TextureCaps &formatCaps = context->getTextureCaps().get(internalformat);
1290 if (static_cast<GLuint>(samples) > formatCaps.getMaxSamples())
1291 {
1292 context->recordError(Error(GL_INVALID_VALUE));
1293 return false;
1294 }
1295
1296 return true;
1297}
1298
Austin Kinross08332632015-05-05 13:35:47 -07001299bool ValidateInvalidateFramebuffer(Context *context, GLenum target, GLsizei numAttachments,
1300 const GLenum *attachments)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001301{
Austin Kinross08332632015-05-05 13:35:47 -07001302 if (context->getClientVersion() < 3)
1303 {
1304 context->recordError(Error(GL_INVALID_OPERATION, "Operation only supported on ES 3.0 and above"));
1305 return false;
1306 }
1307
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001308 bool defaultFramebuffer = false;
1309
1310 switch (target)
1311 {
1312 case GL_DRAW_FRAMEBUFFER:
1313 case GL_FRAMEBUFFER:
Shannon Woods53a94a82014-06-24 15:20:36 -04001314 defaultFramebuffer = context->getState().getDrawFramebuffer()->id() == 0;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001315 break;
1316 case GL_READ_FRAMEBUFFER:
Shannon Woods53a94a82014-06-24 15:20:36 -04001317 defaultFramebuffer = context->getState().getReadFramebuffer()->id() == 0;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001318 break;
1319 default:
Austin Kinross08332632015-05-05 13:35:47 -07001320 context->recordError(Error(GL_INVALID_ENUM, "Invalid framebuffer target"));
1321 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001322 }
1323
Austin Kinross08332632015-05-05 13:35:47 -07001324 return ValidateDiscardFramebufferBase(context, target, numAttachments, attachments, defaultFramebuffer);
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001325}
1326
Jamie Madillc29968b2016-01-20 11:17:23 -05001327bool ValidateClearBuffer(ValidationContext *context)
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001328{
1329 if (context->getClientVersion() < 3)
1330 {
Geoff Langb1196682014-07-23 13:47:29 -04001331 context->recordError(Error(GL_INVALID_OPERATION));
1332 return false;
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001333 }
1334
Shannon Woods53a94a82014-06-24 15:20:36 -04001335 const gl::Framebuffer *fbo = context->getState().getDrawFramebuffer();
Geoff Lang748f74e2014-12-01 11:25:34 -05001336 if (!fbo || fbo->checkStatus(context->getData()) != GL_FRAMEBUFFER_COMPLETE)
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001337 {
Geoff Langb1196682014-07-23 13:47:29 -04001338 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
1339 return false;
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001340 }
1341
1342 return true;
1343}
1344
Geoff Langb1196682014-07-23 13:47:29 -04001345bool ValidateGetUniformuiv(Context *context, GLuint program, GLint location, GLuint* params)
Jamie Madill0063c512014-08-25 15:47:53 -04001346{
1347 if (context->getClientVersion() < 3)
1348 {
Geoff Langb1196682014-07-23 13:47:29 -04001349 context->recordError(Error(GL_INVALID_OPERATION));
1350 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001351 }
1352
Jamie Madill78f41802014-08-25 15:47:55 -04001353 return ValidateGetUniformBase(context, program, location);
Jamie Madill0063c512014-08-25 15:47:53 -04001354}
1355
Jamie Madillb885e572015-02-03 16:16:04 -05001356bool ValidateReadBuffer(Context *context, GLenum src)
1357{
1358 if (context->getClientVersion() < 3)
1359 {
1360 context->recordError(Error(GL_INVALID_OPERATION));
1361 return false;
1362 }
1363
1364 Framebuffer *readFBO = context->getState().getReadFramebuffer();
1365
1366 if (readFBO == nullptr)
1367 {
1368 context->recordError(gl::Error(GL_INVALID_OPERATION, "No active read framebuffer."));
1369 return false;
1370 }
1371
1372 if (src == GL_NONE)
1373 {
1374 return true;
1375 }
1376
1377 if (src != GL_BACK && (src < GL_COLOR_ATTACHMENT0 || src > GL_COLOR_ATTACHMENT15))
1378 {
1379 context->recordError(gl::Error(GL_INVALID_ENUM, "Unknown enum for 'src' in ReadBuffer"));
1380 return false;
1381 }
1382
1383 if (readFBO->id() == 0)
1384 {
1385 if (src != GL_BACK)
1386 {
1387 const char *errorMsg = "'src' must be GL_NONE or GL_BACK when reading from the default framebuffer.";
1388 context->recordError(gl::Error(GL_INVALID_OPERATION, errorMsg));
1389 return false;
1390 }
1391 }
1392 else
1393 {
1394 GLuint drawBuffer = static_cast<GLuint>(src - GL_COLOR_ATTACHMENT0);
1395
1396 if (drawBuffer >= context->getCaps().maxDrawBuffers)
1397 {
1398 const char *errorMsg = "'src' is greater than MAX_DRAW_BUFFERS.";
1399 context->recordError(gl::Error(GL_INVALID_OPERATION, errorMsg));
1400 return false;
1401 }
1402 }
1403
1404 return true;
1405}
1406
Jamie Madill86af3d22015-07-21 15:14:07 -04001407bool ValidateCompressedTexImage3D(Context *context,
1408 GLenum target,
1409 GLint level,
1410 GLenum internalformat,
1411 GLsizei width,
1412 GLsizei height,
1413 GLsizei depth,
1414 GLint border,
1415 GLsizei imageSize,
1416 const GLvoid *data)
1417{
1418 if (context->getClientVersion() < 3)
1419 {
1420 context->recordError(Error(GL_INVALID_OPERATION));
1421 return false;
1422 }
1423
1424 const InternalFormat &formatInfo = GetInternalFormatInfo(internalformat);
1425 if (imageSize < 0 ||
1426 static_cast<GLuint>(imageSize) !=
1427 formatInfo.computeBlockSize(GL_UNSIGNED_BYTE, width, height))
1428 {
1429 context->recordError(Error(GL_INVALID_VALUE));
1430 return false;
1431 }
1432
1433 // 3D texture target validation
1434 if (target != GL_TEXTURE_3D && target != GL_TEXTURE_2D_ARRAY)
1435 {
1436 context->recordError(
1437 Error(GL_INVALID_ENUM, "Must specify a valid 3D texture destination target"));
1438 return false;
1439 }
1440
1441 // validateES3TexImageFormat sets the error code if there is an error
Ian Ewellfc7cf8e2016-01-20 15:57:46 -05001442 if (!ValidateES3TexImage3DParameters(context, target, level, internalformat, true, false, 0, 0,
1443 0, width, height, depth, border, GL_NONE, GL_NONE, data))
Jamie Madill86af3d22015-07-21 15:14:07 -04001444 {
1445 return false;
1446 }
1447
1448 return true;
1449}
Austin Kinrossbc781f32015-10-26 09:27:38 -07001450
1451bool ValidateBindVertexArray(Context *context, GLuint array)
1452{
1453 if (context->getClientVersion() < 3)
1454 {
1455 context->recordError(Error(GL_INVALID_OPERATION));
1456 return false;
1457 }
1458
1459 return ValidateBindVertexArrayBase(context, array);
1460}
1461
1462bool ValidateDeleteVertexArrays(Context *context, GLsizei n)
1463{
1464 if (context->getClientVersion() < 3)
1465 {
1466 context->recordError(Error(GL_INVALID_OPERATION));
1467 return false;
1468 }
1469
1470 return ValidateDeleteVertexArraysBase(context, n);
1471}
1472
1473bool ValidateGenVertexArrays(Context *context, GLsizei n)
1474{
1475 if (context->getClientVersion() < 3)
1476 {
1477 context->recordError(Error(GL_INVALID_OPERATION));
1478 return false;
1479 }
1480
1481 return ValidateGenVertexArraysBase(context, n);
1482}
1483
1484bool ValidateIsVertexArray(Context *context)
1485{
1486 if (context->getClientVersion() < 3)
1487 {
1488 context->recordError(Error(GL_INVALID_OPERATION));
1489 return false;
1490 }
1491
1492 return true;
1493}
Geoff Langc5629752015-12-07 16:29:04 -05001494
1495bool ValidateProgramBinary(Context *context,
1496 GLuint program,
1497 GLenum binaryFormat,
1498 const void *binary,
1499 GLint length)
1500{
1501 if (context->getClientVersion() < 3)
1502 {
1503 context->recordError(Error(GL_INVALID_OPERATION));
1504 return false;
1505 }
1506
1507 return ValidateProgramBinaryBase(context, program, binaryFormat, binary, length);
1508}
1509
1510bool ValidateGetProgramBinary(Context *context,
1511 GLuint program,
1512 GLsizei bufSize,
1513 GLsizei *length,
1514 GLenum *binaryFormat,
1515 void *binary)
1516{
1517 if (context->getClientVersion() < 3)
1518 {
1519 context->recordError(Error(GL_INVALID_OPERATION));
1520 return false;
1521 }
1522
1523 return ValidateGetProgramBinaryBase(context, program, bufSize, length, binaryFormat, binary);
1524}
1525
1526bool ValidateProgramParameter(Context *context, GLuint program, GLenum pname, GLint value)
1527{
1528 if (context->getClientVersion() < 3)
1529 {
1530 context->recordError(Error(GL_INVALID_OPERATION));
1531 return false;
1532 }
1533
1534 if (GetValidProgram(context, program) == nullptr)
1535 {
1536 return false;
1537 }
1538
1539 switch (pname)
1540 {
1541 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT:
1542 break;
1543
1544 default:
1545 context->recordError(Error(GL_INVALID_ENUM, "Invalid pname: 0x%X", pname));
1546 return false;
1547 }
1548
1549 return true;
1550}
Jamie Madillc29968b2016-01-20 11:17:23 -05001551
1552bool ValidateBlitFramebuffer(Context *context,
1553 GLint srcX0,
1554 GLint srcY0,
1555 GLint srcX1,
1556 GLint srcY1,
1557 GLint dstX0,
1558 GLint dstY0,
1559 GLint dstX1,
1560 GLint dstY1,
1561 GLbitfield mask,
1562 GLenum filter)
1563{
1564 if (context->getClientVersion() < 3)
1565 {
1566 context->recordError(Error(GL_INVALID_OPERATION));
1567 return false;
1568 }
1569
1570 return ValidateBlitFramebufferParameters(context, srcX0, srcY0, srcX1, srcY1, dstX0, dstY0,
1571 dstX1, dstY1, mask, filter);
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001572}
Jamie Madillc29968b2016-01-20 11:17:23 -05001573
1574bool ValidateClearBufferiv(ValidationContext *context,
1575 GLenum buffer,
1576 GLint drawbuffer,
1577 const GLint *value)
1578{
1579 switch (buffer)
1580 {
1581 case GL_COLOR:
1582 if (drawbuffer < 0 ||
1583 static_cast<GLuint>(drawbuffer) >= context->getCaps().maxDrawBuffers)
1584 {
1585 context->recordError(Error(GL_INVALID_VALUE));
1586 return false;
1587 }
1588 break;
1589
1590 case GL_STENCIL:
1591 if (drawbuffer != 0)
1592 {
1593 context->recordError(Error(GL_INVALID_VALUE));
1594 return false;
1595 }
1596 break;
1597
1598 default:
1599 context->recordError(Error(GL_INVALID_ENUM));
1600 return false;
1601 }
1602
1603 return ValidateClearBuffer(context);
1604}
1605
1606bool ValidateClearBufferuiv(ValidationContext *context,
1607 GLenum buffer,
1608 GLint drawbuffer,
1609 const GLuint *value)
1610{
1611 switch (buffer)
1612 {
1613 case GL_COLOR:
1614 if (drawbuffer < 0 ||
1615 static_cast<GLuint>(drawbuffer) >= context->getCaps().maxDrawBuffers)
1616 {
1617 context->recordError(Error(GL_INVALID_VALUE));
1618 return false;
1619 }
1620 break;
1621
1622 default:
1623 context->recordError(Error(GL_INVALID_ENUM));
1624 return false;
1625 }
1626
1627 return ValidateClearBuffer(context);
1628}
1629
1630bool ValidateClearBufferfv(ValidationContext *context,
1631 GLenum buffer,
1632 GLint drawbuffer,
1633 const GLfloat *value)
1634{
1635 switch (buffer)
1636 {
1637 case GL_COLOR:
1638 if (drawbuffer < 0 ||
1639 static_cast<GLuint>(drawbuffer) >= context->getCaps().maxDrawBuffers)
1640 {
1641 context->recordError(Error(GL_INVALID_VALUE));
1642 return false;
1643 }
1644 break;
1645
1646 case GL_DEPTH:
1647 if (drawbuffer != 0)
1648 {
1649 context->recordError(Error(GL_INVALID_VALUE));
1650 return false;
1651 }
1652 break;
1653
1654 default:
1655 context->recordError(Error(GL_INVALID_ENUM));
1656 return false;
1657 }
1658
1659 return ValidateClearBuffer(context);
1660}
1661
1662bool ValidateClearBufferfi(ValidationContext *context,
1663 GLenum buffer,
1664 GLint drawbuffer,
1665 GLfloat depth,
1666 GLint stencil)
1667{
1668 switch (buffer)
1669 {
1670 case GL_DEPTH_STENCIL:
1671 if (drawbuffer != 0)
1672 {
1673 context->recordError(Error(GL_INVALID_VALUE));
1674 return false;
1675 }
1676 break;
1677
1678 default:
1679 context->recordError(Error(GL_INVALID_ENUM));
1680 return false;
1681 }
1682
1683 return ValidateClearBuffer(context);
1684}
1685
1686bool ValidateDrawBuffers(ValidationContext *context, GLsizei n, const GLenum *bufs)
1687{
1688 if (context->getClientVersion() < 3)
1689 {
1690 context->recordError(Error(GL_INVALID_OPERATION, "Context does not support GLES3."));
1691 return false;
1692 }
1693
1694 return ValidateDrawBuffersBase(context, n, bufs);
1695}
1696
1697bool ValidateCopyTexSubImage3D(Context *context,
1698 GLenum target,
1699 GLint level,
1700 GLint xoffset,
1701 GLint yoffset,
1702 GLint zoffset,
1703 GLint x,
1704 GLint y,
1705 GLsizei width,
1706 GLsizei height)
1707{
1708 if (context->getClientVersion() < 3)
1709 {
1710 context->recordError(Error(GL_INVALID_OPERATION));
1711 return false;
1712 }
1713
Ian Ewellfc7cf8e2016-01-20 15:57:46 -05001714 return ValidateES3CopyTexImage3DParameters(context, target, level, GL_NONE, true, xoffset,
1715 yoffset, zoffset, x, y, width, height, 0);
Jamie Madillc29968b2016-01-20 11:17:23 -05001716}
1717
1718} // namespace gl