blob: 956dd2099ec100751d493d211bdba3c32a0591dd [file] [log] [blame]
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001#include "precompiled.h"
2//
Geoff Langcec35902014-04-16 10:52:36 -04003// Copyright (c) 2013-2014 The ANGLE Project Authors. All rights reserved.
Geoff Lange8ebe7f2013-08-05 15:03:13 -04004// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
8// validationES3.cpp: Validation functions for OpenGL ES 3.0 entry point parameters
9
10#include "libGLESv2/validationES3.h"
Geoff Langce635692013-09-24 13:56:32 -040011#include "libGLESv2/validationES.h"
Geoff Lange8ebe7f2013-08-05 15:03:13 -040012#include "libGLESv2/Context.h"
13#include "libGLESv2/Texture.h"
14#include "libGLESv2/Framebuffer.h"
15#include "libGLESv2/Renderbuffer.h"
16#include "libGLESv2/formatutils.h"
17#include "libGLESv2/main.h"
Jamie Madille261b442014-06-25 12:42:21 -040018#include "libGLESv2/FramebufferAttachment.h"
Geoff Lange8ebe7f2013-08-05 15:03:13 -040019
20#include "common/mathutil.h"
21
22namespace gl
23{
24
Geoff Lang5d601382014-07-22 15:14:06 -040025// ES3 has a specific set of permutations of internal formats, formats and types which are acceptable.
26struct ES3FormatCombination
27{
28 GLenum internalFormat;
29 GLenum format;
30 GLenum type;
31};
32
33bool operator<(const ES3FormatCombination& a, const ES3FormatCombination& b)
34{
35 return memcmp(&a, &b, sizeof(ES3FormatCombination)) < 0;
36}
37
38typedef std::set<ES3FormatCombination> ES3FormatCombinationSet;
39
40static inline void InsertES3FormatCombo(ES3FormatCombinationSet *set, GLenum internalFormat, GLenum format, GLenum type)
41{
42 ES3FormatCombination info;
43 info.internalFormat = internalFormat;
44 info.format = format;
45 info.type = type;
46 set->insert(info);
47}
48
49ES3FormatCombinationSet BuildES3FormatSet()
50{
51 ES3FormatCombinationSet set;
52
53 // Format combinations from ES 3.0.1 spec, table 3.2
54
55 // | Internal format | Format | Type |
56 // | | | |
57 InsertES3FormatCombo(&set, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE );
58 InsertES3FormatCombo(&set, GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_BYTE );
59 InsertES3FormatCombo(&set, GL_RGBA4, GL_RGBA, GL_UNSIGNED_BYTE );
60 InsertES3FormatCombo(&set, GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE );
61 InsertES3FormatCombo(&set, GL_RGBA8_SNORM, GL_RGBA, GL_BYTE );
62 InsertES3FormatCombo(&set, GL_RGBA4, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4 );
63 InsertES3FormatCombo(&set, GL_RGB10_A2, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV );
64 InsertES3FormatCombo(&set, GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV );
65 InsertES3FormatCombo(&set, GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1 );
66 InsertES3FormatCombo(&set, GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT );
67 InsertES3FormatCombo(&set, GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT_OES );
68 InsertES3FormatCombo(&set, GL_RGBA32F, GL_RGBA, GL_FLOAT );
69 InsertES3FormatCombo(&set, GL_RGBA16F, GL_RGBA, GL_FLOAT );
70 InsertES3FormatCombo(&set, GL_RGBA8UI, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE );
71 InsertES3FormatCombo(&set, GL_RGBA8I, GL_RGBA_INTEGER, GL_BYTE );
72 InsertES3FormatCombo(&set, GL_RGBA16UI, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT );
73 InsertES3FormatCombo(&set, GL_RGBA16I, GL_RGBA_INTEGER, GL_SHORT );
74 InsertES3FormatCombo(&set, GL_RGBA32UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT );
75 InsertES3FormatCombo(&set, GL_RGBA32I, GL_RGBA_INTEGER, GL_INT );
76 InsertES3FormatCombo(&set, GL_RGB10_A2UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV );
77 InsertES3FormatCombo(&set, GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE );
78 InsertES3FormatCombo(&set, GL_RGB565, GL_RGB, GL_UNSIGNED_BYTE );
79 InsertES3FormatCombo(&set, GL_SRGB8, GL_RGB, GL_UNSIGNED_BYTE );
80 InsertES3FormatCombo(&set, GL_RGB8_SNORM, GL_RGB, GL_BYTE );
81 InsertES3FormatCombo(&set, GL_RGB565, GL_RGB, GL_UNSIGNED_SHORT_5_6_5 );
82 InsertES3FormatCombo(&set, GL_R11F_G11F_B10F, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV );
83 InsertES3FormatCombo(&set, GL_RGB9_E5, GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV );
84 InsertES3FormatCombo(&set, GL_RGB16F, GL_RGB, GL_HALF_FLOAT );
85 InsertES3FormatCombo(&set, GL_RGB16F, GL_RGB, GL_HALF_FLOAT_OES );
86 InsertES3FormatCombo(&set, GL_R11F_G11F_B10F, GL_RGB, GL_HALF_FLOAT );
87 InsertES3FormatCombo(&set, GL_R11F_G11F_B10F, GL_RGB, GL_HALF_FLOAT_OES );
88 InsertES3FormatCombo(&set, GL_RGB9_E5, GL_RGB, GL_HALF_FLOAT );
89 InsertES3FormatCombo(&set, GL_RGB9_E5, GL_RGB, GL_HALF_FLOAT_OES );
90 InsertES3FormatCombo(&set, GL_RGB32F, GL_RGB, GL_FLOAT );
91 InsertES3FormatCombo(&set, GL_RGB16F, GL_RGB, GL_FLOAT );
92 InsertES3FormatCombo(&set, GL_R11F_G11F_B10F, GL_RGB, GL_FLOAT );
93 InsertES3FormatCombo(&set, GL_RGB9_E5, GL_RGB, GL_FLOAT );
94 InsertES3FormatCombo(&set, GL_RGB8UI, GL_RGB_INTEGER, GL_UNSIGNED_BYTE );
95 InsertES3FormatCombo(&set, GL_RGB8I, GL_RGB_INTEGER, GL_BYTE );
96 InsertES3FormatCombo(&set, GL_RGB16UI, GL_RGB_INTEGER, GL_UNSIGNED_SHORT );
97 InsertES3FormatCombo(&set, GL_RGB16I, GL_RGB_INTEGER, GL_SHORT );
98 InsertES3FormatCombo(&set, GL_RGB32UI, GL_RGB_INTEGER, GL_UNSIGNED_INT );
99 InsertES3FormatCombo(&set, GL_RGB32I, GL_RGB_INTEGER, GL_INT );
100 InsertES3FormatCombo(&set, GL_RG8, GL_RG, GL_UNSIGNED_BYTE );
101 InsertES3FormatCombo(&set, GL_RG8_SNORM, GL_RG, GL_BYTE );
102 InsertES3FormatCombo(&set, GL_RG16F, GL_RG, GL_HALF_FLOAT );
103 InsertES3FormatCombo(&set, GL_RG16F, GL_RG, GL_HALF_FLOAT_OES );
104 InsertES3FormatCombo(&set, GL_RG32F, GL_RG, GL_FLOAT );
105 InsertES3FormatCombo(&set, GL_RG16F, GL_RG, GL_FLOAT );
106 InsertES3FormatCombo(&set, GL_RG8UI, GL_RG_INTEGER, GL_UNSIGNED_BYTE );
107 InsertES3FormatCombo(&set, GL_RG8I, GL_RG_INTEGER, GL_BYTE );
108 InsertES3FormatCombo(&set, GL_RG16UI, GL_RG_INTEGER, GL_UNSIGNED_SHORT );
109 InsertES3FormatCombo(&set, GL_RG16I, GL_RG_INTEGER, GL_SHORT );
110 InsertES3FormatCombo(&set, GL_RG32UI, GL_RG_INTEGER, GL_UNSIGNED_INT );
111 InsertES3FormatCombo(&set, GL_RG32I, GL_RG_INTEGER, GL_INT );
112 InsertES3FormatCombo(&set, GL_R8, GL_RED, GL_UNSIGNED_BYTE );
113 InsertES3FormatCombo(&set, GL_R8_SNORM, GL_RED, GL_BYTE );
114 InsertES3FormatCombo(&set, GL_R16F, GL_RED, GL_HALF_FLOAT );
115 InsertES3FormatCombo(&set, GL_R16F, GL_RED, GL_HALF_FLOAT_OES );
116 InsertES3FormatCombo(&set, GL_R32F, GL_RED, GL_FLOAT );
117 InsertES3FormatCombo(&set, GL_R16F, GL_RED, GL_FLOAT );
118 InsertES3FormatCombo(&set, GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE );
119 InsertES3FormatCombo(&set, GL_R8I, GL_RED_INTEGER, GL_BYTE );
120 InsertES3FormatCombo(&set, GL_R16UI, GL_RED_INTEGER, GL_UNSIGNED_SHORT );
121 InsertES3FormatCombo(&set, GL_R16I, GL_RED_INTEGER, GL_SHORT );
122 InsertES3FormatCombo(&set, GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT );
123 InsertES3FormatCombo(&set, GL_R32I, GL_RED_INTEGER, GL_INT );
124
125 // Unsized formats
126 InsertES3FormatCombo(&set, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE );
127 InsertES3FormatCombo(&set, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4 );
128 InsertES3FormatCombo(&set, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1 );
129 InsertES3FormatCombo(&set, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE );
130 InsertES3FormatCombo(&set, GL_RGB, GL_RGB, GL_UNSIGNED_SHORT_5_6_5 );
131 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE );
132 InsertES3FormatCombo(&set, GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE );
133 InsertES3FormatCombo(&set, GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE );
134 InsertES3FormatCombo(&set, GL_SRGB_ALPHA_EXT, GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE );
135 InsertES3FormatCombo(&set, GL_SRGB_EXT, GL_SRGB_EXT, GL_UNSIGNED_BYTE );
136
137 // Depth stencil formats
138 InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT );
139 InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT );
140 InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT );
141 InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT );
142 InsertES3FormatCombo(&set, GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8 );
143 InsertES3FormatCombo(&set, GL_DEPTH32F_STENCIL8, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
144
145 // From GL_EXT_sRGB
146 InsertES3FormatCombo(&set, GL_SRGB8_ALPHA8_EXT, GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE );
147 InsertES3FormatCombo(&set, GL_SRGB8, GL_SRGB_EXT, GL_UNSIGNED_BYTE );
148
149 // From GL_OES_texture_float
150 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_FLOAT );
151 InsertES3FormatCombo(&set, GL_LUMINANCE, GL_LUMINANCE, GL_FLOAT );
152 InsertES3FormatCombo(&set, GL_ALPHA, GL_ALPHA, GL_FLOAT );
153
154 // From GL_OES_texture_half_float
155 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT );
156 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES );
157 InsertES3FormatCombo(&set, GL_LUMINANCE, GL_LUMINANCE, GL_HALF_FLOAT );
158 InsertES3FormatCombo(&set, GL_LUMINANCE, GL_LUMINANCE, GL_HALF_FLOAT_OES );
159 InsertES3FormatCombo(&set, GL_ALPHA, GL_ALPHA, GL_HALF_FLOAT );
160 InsertES3FormatCombo(&set, GL_ALPHA, GL_ALPHA, GL_HALF_FLOAT_OES );
161
162 // From GL_EXT_texture_format_BGRA8888
163 InsertES3FormatCombo(&set, GL_BGRA_EXT, GL_BGRA_EXT, GL_UNSIGNED_BYTE );
164
165 // From GL_EXT_texture_storage
166 // | Internal format | Format | Type |
167 // | | | |
168 InsertES3FormatCombo(&set, GL_ALPHA8_EXT, GL_ALPHA, GL_UNSIGNED_BYTE );
169 InsertES3FormatCombo(&set, GL_LUMINANCE8_EXT, GL_LUMINANCE, GL_UNSIGNED_BYTE );
170 InsertES3FormatCombo(&set, GL_LUMINANCE8_ALPHA8_EXT, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE );
171 InsertES3FormatCombo(&set, GL_ALPHA32F_EXT, GL_ALPHA, GL_FLOAT );
172 InsertES3FormatCombo(&set, GL_LUMINANCE32F_EXT, GL_LUMINANCE, GL_FLOAT );
173 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA32F_EXT, GL_LUMINANCE_ALPHA, GL_FLOAT );
174 InsertES3FormatCombo(&set, GL_ALPHA16F_EXT, GL_ALPHA, GL_HALF_FLOAT );
175 InsertES3FormatCombo(&set, GL_ALPHA16F_EXT, GL_ALPHA, GL_HALF_FLOAT_OES );
176 InsertES3FormatCombo(&set, GL_LUMINANCE16F_EXT, GL_LUMINANCE, GL_HALF_FLOAT );
177 InsertES3FormatCombo(&set, GL_LUMINANCE16F_EXT, GL_LUMINANCE, GL_HALF_FLOAT_OES );
178 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA16F_EXT, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT );
179 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA16F_EXT, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES );
180
181 // From GL_EXT_texture_storage and GL_EXT_texture_format_BGRA8888
182 InsertES3FormatCombo(&set, GL_BGRA8_EXT, GL_BGRA_EXT, GL_UNSIGNED_BYTE );
183 InsertES3FormatCombo(&set, GL_BGRA4_ANGLEX, GL_BGRA_EXT, GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT);
184 InsertES3FormatCombo(&set, GL_BGRA4_ANGLEX, GL_BGRA_EXT, GL_UNSIGNED_BYTE );
185 InsertES3FormatCombo(&set, GL_BGR5_A1_ANGLEX, GL_BGRA_EXT, GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT);
186 InsertES3FormatCombo(&set, GL_BGR5_A1_ANGLEX, GL_BGRA_EXT, GL_UNSIGNED_BYTE );
187
188 // From GL_ANGLE_depth_texture
189 InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT32_OES, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT_24_8_OES );
190
191 // Compressed formats
192 // From ES 3.0.1 spec, table 3.16
193 // | Internal format | Format | Type |
194 // | | | |
195 InsertES3FormatCombo(&set, GL_COMPRESSED_R11_EAC, GL_COMPRESSED_R11_EAC, GL_UNSIGNED_BYTE);
196 InsertES3FormatCombo(&set, GL_COMPRESSED_R11_EAC, GL_COMPRESSED_R11_EAC, GL_UNSIGNED_BYTE);
197 InsertES3FormatCombo(&set, GL_COMPRESSED_SIGNED_R11_EAC, GL_COMPRESSED_SIGNED_R11_EAC, GL_UNSIGNED_BYTE);
198 InsertES3FormatCombo(&set, GL_COMPRESSED_RG11_EAC, GL_COMPRESSED_RG11_EAC, GL_UNSIGNED_BYTE);
199 InsertES3FormatCombo(&set, GL_COMPRESSED_SIGNED_RG11_EAC, GL_COMPRESSED_SIGNED_RG11_EAC, GL_UNSIGNED_BYTE);
200 InsertES3FormatCombo(&set, GL_COMPRESSED_RGB8_ETC2, GL_COMPRESSED_RGB8_ETC2, GL_UNSIGNED_BYTE);
201 InsertES3FormatCombo(&set, GL_COMPRESSED_SRGB8_ETC2, GL_COMPRESSED_SRGB8_ETC2, GL_UNSIGNED_BYTE);
202 InsertES3FormatCombo(&set, GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, GL_UNSIGNED_BYTE);
203 InsertES3FormatCombo(&set, GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, GL_UNSIGNED_BYTE);
204 InsertES3FormatCombo(&set, GL_COMPRESSED_RGBA8_ETC2_EAC, GL_COMPRESSED_RGBA8_ETC2_EAC, GL_UNSIGNED_BYTE);
205 InsertES3FormatCombo(&set, GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC, GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC, GL_UNSIGNED_BYTE);
206
207
208 // From GL_EXT_texture_compression_dxt1
209 InsertES3FormatCombo(&set, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, GL_UNSIGNED_BYTE);
210 InsertES3FormatCombo(&set, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_UNSIGNED_BYTE);
211
212 // From GL_ANGLE_texture_compression_dxt3
213 InsertES3FormatCombo(&set, GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE, GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE, GL_UNSIGNED_BYTE);
214
215 // From GL_ANGLE_texture_compression_dxt5
216 InsertES3FormatCombo(&set, GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE, GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE, GL_UNSIGNED_BYTE);
217
218 return set;
219}
220
221static bool ValidateTexImageFormatCombination(gl::Context *context, GLenum internalFormat, GLenum format, GLenum type)
222{
223 // Note: dEQP 2013.4 expects an INVALID_VALUE error for TexImage3D with an invalid
224 // internal format. (dEQP-GLES3.functional.negative_api.texture.teximage3d)
Geoff Langbaadf232014-08-04 13:58:02 -0400225 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalFormat);
Geoff Lang5d601382014-07-22 15:14:06 -0400226 if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions()))
227 {
228 return gl::error(GL_INVALID_ENUM, false);
229 }
230
231 // The type and format are valid if any supported internal format has that type and format
232 bool formatSupported = false;
233 bool typeSupported = false;
234
235 static const ES3FormatCombinationSet es3FormatSet = BuildES3FormatSet();
236 for (ES3FormatCombinationSet::const_iterator i = es3FormatSet.begin(); i != es3FormatSet.end(); i++)
237 {
238 if (i->format == format || i->type == type)
239 {
240 const gl::InternalFormat &info = gl::GetInternalFormatInfo(i->internalFormat);
241 bool supported = info.textureSupport(context->getClientVersion(), context->getExtensions());
Geoff Langbaadf232014-08-04 13:58:02 -0400242 if (supported && i->type == type)
Geoff Lang5d601382014-07-22 15:14:06 -0400243 {
244 typeSupported = true;
245 }
Geoff Langbaadf232014-08-04 13:58:02 -0400246 if (supported && i->format == format)
Geoff Lang5d601382014-07-22 15:14:06 -0400247 {
248 formatSupported = true;
249 }
Geoff Langbaadf232014-08-04 13:58:02 -0400250
251 // Early-out if both type and format are supported now
252 if (typeSupported && formatSupported)
253 {
254 break;
255 }
Geoff Lang5d601382014-07-22 15:14:06 -0400256 }
257 }
258
259 if (!typeSupported || !formatSupported)
260 {
261 return gl::error(GL_INVALID_ENUM, false);
262 }
263
264 // Check if this is a valid format combination to load texture data
265 ES3FormatCombination searchFormat;
266 searchFormat.internalFormat = internalFormat;
267 searchFormat.format = format;
268 searchFormat.type = type;
269
270 if (es3FormatSet.find(searchFormat) == es3FormatSet.end())
271 {
272 return gl::error(GL_INVALID_OPERATION, false);
273 }
274
275 return true;
276}
277
Geoff Lang005df412013-10-16 14:12:50 -0400278bool ValidateES3TexImageParameters(gl::Context *context, GLenum target, GLint level, GLenum internalformat, bool isCompressed, bool isSubImage,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400279 GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth,
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400280 GLint border, GLenum format, GLenum type, const GLvoid *pixels)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400281{
Jamie Madill6f38f822014-06-06 17:12:20 -0400282 if (!ValidTexture2DDestinationTarget(context, target))
283 {
284 return gl::error(GL_INVALID_ENUM, false);
285 }
286
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400287 // Validate image size
Geoff Langce635692013-09-24 13:56:32 -0400288 if (!ValidImageSize(context, target, level, width, height, depth))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400289 {
290 return gl::error(GL_INVALID_VALUE, false);
291 }
292
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400293 // Verify zero border
294 if (border != 0)
295 {
296 return gl::error(GL_INVALID_VALUE, false);
297 }
298
Jamie Madill6f38f822014-06-06 17:12:20 -0400299 if (xoffset < 0 || yoffset < 0 || zoffset < 0 ||
300 std::numeric_limits<GLsizei>::max() - xoffset < width ||
301 std::numeric_limits<GLsizei>::max() - yoffset < height ||
302 std::numeric_limits<GLsizei>::max() - zoffset < depth)
303 {
304 return gl::error(GL_INVALID_VALUE, false);
305 }
306
Geoff Langaae65a42014-05-26 12:43:44 -0400307 const gl::Caps &caps = context->getCaps();
308
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400309 gl::Texture *texture = NULL;
310 bool textureCompressed = false;
311 GLenum textureInternalFormat = GL_NONE;
312 GLint textureLevelWidth = 0;
313 GLint textureLevelHeight = 0;
314 GLint textureLevelDepth = 0;
315 switch (target)
316 {
317 case GL_TEXTURE_2D:
318 {
Geoff Langaae65a42014-05-26 12:43:44 -0400319 if (static_cast<GLuint>(width) > (caps.max2DTextureSize >> level) ||
320 static_cast<GLuint>(height) > (caps.max2DTextureSize >> level))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400321 {
322 return gl::error(GL_INVALID_VALUE, false);
323 }
324
325 gl::Texture2D *texture2d = context->getTexture2D();
326 if (texture2d)
327 {
328 textureCompressed = texture2d->isCompressed(level);
329 textureInternalFormat = texture2d->getInternalFormat(level);
330 textureLevelWidth = texture2d->getWidth(level);
331 textureLevelHeight = texture2d->getHeight(level);
332 textureLevelDepth = 1;
333 texture = texture2d;
334 }
335 }
336 break;
337
338 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
339 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
340 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
341 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
342 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
343 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
344 {
345 if (!isSubImage && width != height)
346 {
347 return gl::error(GL_INVALID_VALUE, false);
348 }
349
Geoff Langaae65a42014-05-26 12:43:44 -0400350 if (static_cast<GLuint>(width) > (caps.maxCubeMapTextureSize >> level))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400351 {
352 return gl::error(GL_INVALID_VALUE, false);
353 }
354
355 gl::TextureCubeMap *textureCube = context->getTextureCubeMap();
356 if (textureCube)
357 {
358 textureCompressed = textureCube->isCompressed(target, level);
359 textureInternalFormat = textureCube->getInternalFormat(target, level);
360 textureLevelWidth = textureCube->getWidth(target, level);
361 textureLevelHeight = textureCube->getHeight(target, level);
362 textureLevelDepth = 1;
363 texture = textureCube;
364 }
365 }
366 break;
367
368 case GL_TEXTURE_3D:
369 {
Geoff Langaae65a42014-05-26 12:43:44 -0400370 if (static_cast<GLuint>(width) > (caps.max3DTextureSize >> level) ||
371 static_cast<GLuint>(height) > (caps.max3DTextureSize >> level) ||
372 static_cast<GLuint>(depth) > (caps.max3DTextureSize >> level))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400373 {
374 return gl::error(GL_INVALID_VALUE, false);
375 }
376
377 gl::Texture3D *texture3d = context->getTexture3D();
378 if (texture3d)
379 {
380 textureCompressed = texture3d->isCompressed(level);
381 textureInternalFormat = texture3d->getInternalFormat(level);
382 textureLevelWidth = texture3d->getWidth(level);
383 textureLevelHeight = texture3d->getHeight(level);
384 textureLevelDepth = texture3d->getDepth(level);
385 texture = texture3d;
386 }
387 }
388 break;
389
390 case GL_TEXTURE_2D_ARRAY:
391 {
Geoff Langaae65a42014-05-26 12:43:44 -0400392 if (static_cast<GLuint>(width) > (caps.max2DTextureSize >> level) ||
393 static_cast<GLuint>(height) > (caps.max2DTextureSize >> level) ||
394 static_cast<GLuint>(depth) > (caps.maxArrayTextureLayers >> level))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400395 {
396 return gl::error(GL_INVALID_VALUE, false);
397 }
398
399 gl::Texture2DArray *texture2darray = context->getTexture2DArray();
400 if (texture2darray)
401 {
402 textureCompressed = texture2darray->isCompressed(level);
403 textureInternalFormat = texture2darray->getInternalFormat(level);
404 textureLevelWidth = texture2darray->getWidth(level);
405 textureLevelHeight = texture2darray->getHeight(level);
Jamie Madillb8f8b892014-01-07 10:12:50 -0500406 textureLevelDepth = texture2darray->getLayers(level);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400407 texture = texture2darray;
408 }
409 }
410 break;
411
412 default:
413 return gl::error(GL_INVALID_ENUM, false);
414 }
415
416 if (!texture)
417 {
418 return gl::error(GL_INVALID_OPERATION, false);
419 }
420
421 if (texture->isImmutable() && !isSubImage)
422 {
423 return gl::error(GL_INVALID_OPERATION, false);
424 }
425
426 // Validate texture formats
427 GLenum actualInternalFormat = isSubImage ? textureInternalFormat : internalformat;
Geoff Lang5d601382014-07-22 15:14:06 -0400428 const gl::InternalFormat &actualFormatInfo = gl::GetInternalFormatInfo(actualInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400429 if (isCompressed)
430 {
Geoff Langd4f180b2013-09-24 13:57:44 -0400431 if (!ValidCompressedImageSize(context, actualInternalFormat, width, height))
432 {
433 return gl::error(GL_INVALID_OPERATION, false);
434 }
435
Geoff Lang5d601382014-07-22 15:14:06 -0400436 if (!actualFormatInfo.compressed)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400437 {
438 return gl::error(GL_INVALID_ENUM, false);
439 }
440
441 if (target == GL_TEXTURE_3D)
442 {
443 return gl::error(GL_INVALID_OPERATION, false);
444 }
445 }
446 else
447 {
Geoff Langbaadf232014-08-04 13:58:02 -0400448 if (!ValidateTexImageFormatCombination(context, actualInternalFormat, format, type))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400449 {
Geoff Lang5d601382014-07-22 15:14:06 -0400450 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400451 }
452
453 if (target == GL_TEXTURE_3D && (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL))
454 {
455 return gl::error(GL_INVALID_OPERATION, false);
456 }
457 }
458
459 // Validate sub image parameters
460 if (isSubImage)
461 {
462 if (isCompressed != textureCompressed)
463 {
464 return gl::error(GL_INVALID_OPERATION, false);
465 }
466
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400467 if (isCompressed)
468 {
469 if ((width % 4 != 0 && width != textureLevelWidth) ||
470 (height % 4 != 0 && height != textureLevelHeight))
471 {
472 return gl::error(GL_INVALID_OPERATION, false);
473 }
474 }
475
476 if (width == 0 || height == 0 || depth == 0)
477 {
478 return false;
479 }
480
481 if (xoffset < 0 || yoffset < 0 || zoffset < 0)
482 {
483 return gl::error(GL_INVALID_VALUE, false);
484 }
485
486 if (std::numeric_limits<GLsizei>::max() - xoffset < width ||
487 std::numeric_limits<GLsizei>::max() - yoffset < height ||
488 std::numeric_limits<GLsizei>::max() - zoffset < depth)
489 {
490 return gl::error(GL_INVALID_VALUE, false);
491 }
492
493 if (xoffset + width > textureLevelWidth ||
494 yoffset + height > textureLevelHeight ||
495 zoffset + depth > textureLevelDepth)
496 {
497 return gl::error(GL_INVALID_VALUE, false);
498 }
499 }
500
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400501 // Check for pixel unpack buffer related API errors
Shannon Woods53a94a82014-06-24 15:20:36 -0400502 gl::Buffer *pixelUnpackBuffer = context->getState().getTargetBuffer(GL_PIXEL_UNPACK_BUFFER);
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400503 if (pixelUnpackBuffer != NULL)
504 {
505 // ...the data would be unpacked from the buffer object such that the memory reads required
506 // would exceed the data store size.
507 size_t widthSize = static_cast<size_t>(width);
508 size_t heightSize = static_cast<size_t>(height);
509 size_t depthSize = static_cast<size_t>(depth);
Geoff Lang5d601382014-07-22 15:14:06 -0400510 GLenum sizedFormat = GetSizedInternalFormat(actualInternalFormat, type);
Jamie Madill6f38f822014-06-06 17:12:20 -0400511
Geoff Lang5d601382014-07-22 15:14:06 -0400512 size_t pixelBytes = static_cast<size_t>(gl::GetInternalFormatInfo(sizedFormat).pixelBytes);
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400513
514 if (!rx::IsUnsignedMultiplicationSafe(widthSize, heightSize) ||
515 !rx::IsUnsignedMultiplicationSafe(widthSize * heightSize, depthSize) ||
516 !rx::IsUnsignedMultiplicationSafe(widthSize * heightSize * depthSize, pixelBytes))
517 {
518 // Overflow past the end of the buffer
519 return gl::error(GL_INVALID_OPERATION, false);
520 }
521
522 size_t copyBytes = widthSize * heightSize * depthSize * pixelBytes;
523 size_t offset = reinterpret_cast<size_t>(pixels);
524
Jamie Madill6f38f822014-06-06 17:12:20 -0400525 if (!rx::IsUnsignedAdditionSafe(offset, copyBytes) ||
Brandon Jonesd38f9262014-06-18 16:26:45 -0700526 ((offset + copyBytes) > static_cast<size_t>(pixelUnpackBuffer->getSize())))
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400527 {
528 // Overflow past the end of the buffer
529 return gl::error(GL_INVALID_OPERATION, false);
530 }
531
532 // ...data is not evenly divisible into the number of bytes needed to store in memory a datum
533 // indicated by type.
Geoff Lang5d601382014-07-22 15:14:06 -0400534 size_t dataBytesPerPixel = static_cast<size_t>(gl::GetTypeInfo(type).bytes);
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400535
536 if ((offset % dataBytesPerPixel) != 0)
537 {
538 return gl::error(GL_INVALID_OPERATION, false);
539 }
540
Jamie Madill7a5f7382014-03-05 15:01:24 -0500541 // ...the buffer object's data store is currently mapped.
Brandon Jonesd38f9262014-06-18 16:26:45 -0700542 if (pixelUnpackBuffer->isMapped())
Jamie Madill7a5f7382014-03-05 15:01:24 -0500543 {
544 return gl::error(GL_INVALID_OPERATION, false);
545 }
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400546 }
547
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400548 return true;
549}
550
Geoff Lang5d601382014-07-22 15:14:06 -0400551struct EffectiveInternalFormatInfo
552{
553 GLenum mEffectiveFormat;
554 GLenum mDestFormat;
555 GLuint mMinRedBits;
556 GLuint mMaxRedBits;
557 GLuint mMinGreenBits;
558 GLuint mMaxGreenBits;
559 GLuint mMinBlueBits;
560 GLuint mMaxBlueBits;
561 GLuint mMinAlphaBits;
562 GLuint mMaxAlphaBits;
563
564 EffectiveInternalFormatInfo(GLenum effectiveFormat, GLenum destFormat, GLuint minRedBits, GLuint maxRedBits,
565 GLuint minGreenBits, GLuint maxGreenBits, GLuint minBlueBits, GLuint maxBlueBits,
566 GLuint minAlphaBits, GLuint maxAlphaBits)
567 : mEffectiveFormat(effectiveFormat), mDestFormat(destFormat), mMinRedBits(minRedBits),
568 mMaxRedBits(maxRedBits), mMinGreenBits(minGreenBits), mMaxGreenBits(maxGreenBits),
569 mMinBlueBits(minBlueBits), mMaxBlueBits(maxBlueBits), mMinAlphaBits(minAlphaBits),
570 mMaxAlphaBits(maxAlphaBits) {};
571};
572
573typedef std::vector<EffectiveInternalFormatInfo> EffectiveInternalFormatList;
574
575static EffectiveInternalFormatList BuildSizedEffectiveInternalFormatList()
576{
577 EffectiveInternalFormatList list;
578
579 // OpenGL ES 3.0.3 Specification, Table 3.17, pg 141: Effective internal format coresponding to destination internal format and
580 // linear source buffer component sizes.
581 // | Source channel min/max sizes |
582 // Effective Internal Format | N/A | R | G | B | A |
583 list.push_back(EffectiveInternalFormatInfo(GL_ALPHA8_EXT, GL_NONE, 0, 0, 0, 0, 0, 0, 1, 8));
584 list.push_back(EffectiveInternalFormatInfo(GL_R8, GL_NONE, 1, 8, 0, 0, 0, 0, 0, 0));
585 list.push_back(EffectiveInternalFormatInfo(GL_RG8, GL_NONE, 1, 8, 1, 8, 0, 0, 0, 0));
586 list.push_back(EffectiveInternalFormatInfo(GL_RGB565, GL_NONE, 1, 5, 1, 6, 1, 5, 0, 0));
587 list.push_back(EffectiveInternalFormatInfo(GL_RGB8, GL_NONE, 6, 8, 7, 8, 6, 8, 0, 0));
588 list.push_back(EffectiveInternalFormatInfo(GL_RGBA4, GL_NONE, 1, 4, 1, 4, 1, 4, 1, 4));
589 list.push_back(EffectiveInternalFormatInfo(GL_RGB5_A1, GL_NONE, 5, 5, 5, 5, 5, 5, 1, 1));
590 list.push_back(EffectiveInternalFormatInfo(GL_RGBA8, GL_NONE, 5, 8, 5, 8, 5, 8, 2, 8));
591 list.push_back(EffectiveInternalFormatInfo(GL_RGB10_A2, GL_NONE, 9, 10, 9, 10, 9, 10, 2, 2));
592
593 return list;
594}
595
596static EffectiveInternalFormatList BuildUnsizedEffectiveInternalFormatList()
597{
598 EffectiveInternalFormatList list;
599
600 // OpenGL ES 3.0.3 Specification, Table 3.17, pg 141: Effective internal format coresponding to destination internal format and
601 // linear source buffer component sizes.
602 // | Source channel min/max sizes |
603 // Effective Internal Format | Dest Format | R | G | B | A |
604 list.push_back(EffectiveInternalFormatInfo(GL_ALPHA8_EXT, GL_ALPHA, 0, UINT_MAX, 0, UINT_MAX, 0, UINT_MAX, 1, 8));
605 list.push_back(EffectiveInternalFormatInfo(GL_LUMINANCE8_EXT, GL_LUMINANCE, 1, 8, 0, UINT_MAX, 0, UINT_MAX, 0, UINT_MAX));
606 list.push_back(EffectiveInternalFormatInfo(GL_LUMINANCE8_ALPHA8_EXT, GL_LUMINANCE_ALPHA, 1, 8, 0, UINT_MAX, 0, UINT_MAX, 1, 8));
607 list.push_back(EffectiveInternalFormatInfo(GL_RGB565, GL_RGB, 1, 5, 1, 6, 1, 5, 0, UINT_MAX));
608 list.push_back(EffectiveInternalFormatInfo(GL_RGB8, GL_RGB, 6, 8, 7, 8, 6, 8, 0, UINT_MAX));
609 list.push_back(EffectiveInternalFormatInfo(GL_RGBA4, GL_RGBA, 1, 4, 1, 4, 1, 4, 1, 4));
610 list.push_back(EffectiveInternalFormatInfo(GL_RGB5_A1, GL_RGBA, 5, 5, 5, 5, 5, 5, 1, 1));
611 list.push_back(EffectiveInternalFormatInfo(GL_RGBA8, GL_RGBA, 5, 8, 5, 8, 5, 8, 5, 8));
612
613 return list;
614}
615
616static bool GetEffectiveInternalFormat(const InternalFormat &srcFormat, const InternalFormat &destFormat,
617 GLenum *outEffectiveFormat)
618{
619 const EffectiveInternalFormatList *list = NULL;
620 GLenum targetFormat = GL_NONE;
621
622 if (destFormat.pixelBytes > 0)
623 {
624 static const EffectiveInternalFormatList sizedList = BuildSizedEffectiveInternalFormatList();
625 list = &sizedList;
626 }
627 else
628 {
629 static const EffectiveInternalFormatList unsizedList = BuildUnsizedEffectiveInternalFormatList();
630 list = &unsizedList;
631 targetFormat = destFormat.format;
632 }
633
634 for (size_t curFormat = 0; curFormat < list->size(); ++curFormat)
635 {
636 const EffectiveInternalFormatInfo& formatInfo = list->at(curFormat);
637 if ((formatInfo.mDestFormat == targetFormat) &&
638 (formatInfo.mMinRedBits <= srcFormat.redBits && formatInfo.mMaxRedBits >= srcFormat.redBits) &&
639 (formatInfo.mMinGreenBits <= srcFormat.greenBits && formatInfo.mMaxGreenBits >= srcFormat.greenBits) &&
640 (formatInfo.mMinBlueBits <= srcFormat.blueBits && formatInfo.mMaxBlueBits >= srcFormat.blueBits) &&
641 (formatInfo.mMinAlphaBits <= srcFormat.alphaBits && formatInfo.mMaxAlphaBits >= srcFormat.alphaBits))
642 {
643 *outEffectiveFormat = formatInfo.mEffectiveFormat;
644 return true;
645 }
646 }
647
648 return false;
649}
650
651struct CopyConversion
652{
653 GLenum mTextureFormat;
654 GLenum mFramebufferFormat;
655
656 CopyConversion(GLenum textureFormat, GLenum framebufferFormat)
657 : mTextureFormat(textureFormat), mFramebufferFormat(framebufferFormat) { }
658
659 bool operator<(const CopyConversion& other) const
660 {
661 return memcmp(this, &other, sizeof(CopyConversion)) < 0;
662 }
663};
664
665typedef std::set<CopyConversion> CopyConversionSet;
666
667static CopyConversionSet BuildValidES3CopyTexImageCombinations()
668{
669 CopyConversionSet set;
670
671 // From ES 3.0.1 spec, table 3.15
672 set.insert(CopyConversion(GL_ALPHA, GL_RGBA));
673 set.insert(CopyConversion(GL_LUMINANCE, GL_RED));
674 set.insert(CopyConversion(GL_LUMINANCE, GL_RG));
675 set.insert(CopyConversion(GL_LUMINANCE, GL_RGB));
676 set.insert(CopyConversion(GL_LUMINANCE, GL_RGBA));
677 set.insert(CopyConversion(GL_LUMINANCE_ALPHA, GL_RGBA));
678 set.insert(CopyConversion(GL_RED, GL_RED));
679 set.insert(CopyConversion(GL_RED, GL_RG));
680 set.insert(CopyConversion(GL_RED, GL_RGB));
681 set.insert(CopyConversion(GL_RED, GL_RGBA));
682 set.insert(CopyConversion(GL_RG, GL_RG));
683 set.insert(CopyConversion(GL_RG, GL_RGB));
684 set.insert(CopyConversion(GL_RG, GL_RGBA));
685 set.insert(CopyConversion(GL_RGB, GL_RGB));
686 set.insert(CopyConversion(GL_RGB, GL_RGBA));
687 set.insert(CopyConversion(GL_RGBA, GL_RGBA));
688
689 // Necessary for ANGLE back-buffers
690 set.insert(CopyConversion(GL_ALPHA, GL_BGRA_EXT));
691 set.insert(CopyConversion(GL_LUMINANCE, GL_BGRA_EXT));
692 set.insert(CopyConversion(GL_LUMINANCE_ALPHA, GL_BGRA_EXT));
693 set.insert(CopyConversion(GL_RED, GL_BGRA_EXT));
694 set.insert(CopyConversion(GL_RG, GL_BGRA_EXT));
695 set.insert(CopyConversion(GL_RGB, GL_BGRA_EXT));
696 set.insert(CopyConversion(GL_RGBA, GL_BGRA_EXT));
697
698 set.insert(CopyConversion(GL_RED_INTEGER, GL_RED_INTEGER));
699 set.insert(CopyConversion(GL_RED_INTEGER, GL_RG_INTEGER));
700 set.insert(CopyConversion(GL_RED_INTEGER, GL_RGB_INTEGER));
701 set.insert(CopyConversion(GL_RED_INTEGER, GL_RGBA_INTEGER));
702 set.insert(CopyConversion(GL_RG_INTEGER, GL_RG_INTEGER));
703 set.insert(CopyConversion(GL_RG_INTEGER, GL_RGB_INTEGER));
704 set.insert(CopyConversion(GL_RG_INTEGER, GL_RGBA_INTEGER));
705 set.insert(CopyConversion(GL_RGB_INTEGER, GL_RGB_INTEGER));
706 set.insert(CopyConversion(GL_RGB_INTEGER, GL_RGBA_INTEGER));
707 set.insert(CopyConversion(GL_RGBA_INTEGER, GL_RGBA_INTEGER));
708
709 return set;
710}
711
712static bool IsValidES3CopyTexImageCombination(GLenum textureInternalFormat, GLenum frameBufferInternalFormat, GLuint readBufferHandle)
713{
714 const InternalFormat &textureInternalFormatInfo = GetInternalFormatInfo(textureInternalFormat);
715 const InternalFormat &framebufferInternalFormatInfo = GetInternalFormatInfo(frameBufferInternalFormat);
716
717 static const CopyConversionSet conversionSet = BuildValidES3CopyTexImageCombinations();
718 if (conversionSet.find(CopyConversion(textureInternalFormatInfo.format, framebufferInternalFormatInfo.format)) != conversionSet.end())
719 {
720 // Section 3.8.5 of the GLES 3.0.3 spec states that source and destination formats
721 // must both be signed, unsigned, or fixed point and both source and destinations
722 // must be either both SRGB or both not SRGB. EXT_color_buffer_float adds allowed
723 // conversion between fixed and floating point.
724
725 if ((textureInternalFormatInfo.colorEncoding == GL_SRGB) != (framebufferInternalFormatInfo.colorEncoding == GL_SRGB))
726 {
727 return false;
728 }
729
730 if (((textureInternalFormatInfo.componentType == GL_INT) != (framebufferInternalFormatInfo.componentType == GL_INT )) ||
731 ((textureInternalFormatInfo.componentType == GL_UNSIGNED_INT) != (framebufferInternalFormatInfo.componentType == GL_UNSIGNED_INT)))
732 {
733 return false;
734 }
735
736 if ((textureInternalFormatInfo.componentType == GL_UNSIGNED_NORMALIZED ||
737 textureInternalFormatInfo.componentType == GL_SIGNED_NORMALIZED ||
738 textureInternalFormatInfo.componentType == GL_FLOAT) &&
739 !(framebufferInternalFormatInfo.componentType == GL_UNSIGNED_NORMALIZED ||
740 framebufferInternalFormatInfo.componentType == GL_SIGNED_NORMALIZED ||
741 framebufferInternalFormatInfo.componentType == GL_FLOAT))
742 {
743 return false;
744 }
745
746 // GLES specification 3.0.3, sec 3.8.5, pg 139-140:
747 // The effective internal format of the source buffer is determined with the following rules applied in order:
748 // * If the source buffer is a texture or renderbuffer that was created with a sized internal format then the
749 // effective internal format is the source buffer's sized internal format.
750 // * If the source buffer is a texture that was created with an unsized base internal format, then the
751 // effective internal format is the source image array's effective internal format, as specified by table
752 // 3.12, which is determined from the <format> and <type> that were used when the source image array was
753 // specified by TexImage*.
754 // * Otherwise the effective internal format is determined by the row in table 3.17 or 3.18 where
755 // Destination Internal Format matches internalformat and where the [source channel sizes] are consistent
756 // with the values of the source buffer's [channel sizes]. Table 3.17 is used if the
757 // FRAMEBUFFER_ATTACHMENT_ENCODING is LINEAR and table 3.18 is used if the FRAMEBUFFER_ATTACHMENT_ENCODING
758 // is SRGB.
759 const InternalFormat *sourceEffectiveFormat = NULL;
760 if (readBufferHandle != 0)
761 {
762 // Not the default framebuffer, therefore the read buffer must be a user-created texture or renderbuffer
763 if (framebufferInternalFormatInfo.pixelBytes > 0)
764 {
765 sourceEffectiveFormat = &framebufferInternalFormatInfo;
766 }
767 else
768 {
769 // Renderbuffers cannot be created with an unsized internal format, so this must be an unsized-format
770 // texture. We can use the same table we use when creating textures to get its effective sized format.
771 const FormatType &typeInfo = GetFormatTypeInfo(framebufferInternalFormatInfo.format, framebufferInternalFormatInfo.type);
772 sourceEffectiveFormat = &GetInternalFormatInfo(typeInfo.internalFormat);
773 }
774 }
775 else
776 {
777 // The effective internal format must be derived from the source framebuffer's channel sizes.
778 // This is done in GetEffectiveInternalFormat for linear buffers (table 3.17)
779 if (framebufferInternalFormatInfo.colorEncoding == GL_LINEAR)
780 {
781 GLenum effectiveFormat;
782 if (GetEffectiveInternalFormat(framebufferInternalFormatInfo, textureInternalFormatInfo, &effectiveFormat))
783 {
784 sourceEffectiveFormat = &GetInternalFormatInfo(effectiveFormat);
785 }
786 else
787 {
788 return false;
789 }
790 }
791 else if (framebufferInternalFormatInfo.colorEncoding == GL_SRGB)
792 {
793 // SRGB buffers can only be copied to sized format destinations according to table 3.18
794 if ((textureInternalFormatInfo.pixelBytes > 0) &&
795 (framebufferInternalFormatInfo.redBits >= 1 && framebufferInternalFormatInfo.redBits <= 8) &&
796 (framebufferInternalFormatInfo.greenBits >= 1 && framebufferInternalFormatInfo.greenBits <= 8) &&
797 (framebufferInternalFormatInfo.blueBits >= 1 && framebufferInternalFormatInfo.blueBits <= 8) &&
798 (framebufferInternalFormatInfo.alphaBits >= 1 && framebufferInternalFormatInfo.alphaBits <= 8))
799 {
800 sourceEffectiveFormat = &GetInternalFormatInfo(GL_SRGB8_ALPHA8);
801 }
802 else
803 {
804 return false;
805 }
806 }
807 else
808 {
809 UNREACHABLE();
810 return false;
811 }
812 }
813
814 if (textureInternalFormatInfo.pixelBytes > 0)
815 {
816 // Section 3.8.5 of the GLES 3.0.3 spec, pg 139, requires that, if the destination format is sized,
817 // component sizes of the source and destination formats must exactly match
818 if (textureInternalFormatInfo.redBits != sourceEffectiveFormat->redBits ||
819 textureInternalFormatInfo.greenBits != sourceEffectiveFormat->greenBits ||
820 textureInternalFormatInfo.blueBits != sourceEffectiveFormat->blueBits ||
821 textureInternalFormatInfo.alphaBits != sourceEffectiveFormat->alphaBits)
822 {
823 return false;
824 }
825 }
826
827
828 return true; // A conversion function exists, and no rule in the specification has precluded conversion
829 // between these formats.
830 }
831
832 return false;
833}
834
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400835bool ValidateES3CopyTexImageParameters(gl::Context *context, GLenum target, GLint level, GLenum internalformat,
Jamie Madill6f38f822014-06-06 17:12:20 -0400836 bool isSubImage, GLint xoffset, GLint yoffset, GLint zoffset,
837 GLint x, GLint y, GLsizei width, GLsizei height, GLint border)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400838{
Jamie Madill560a8d82014-05-21 13:06:20 -0400839 GLenum textureInternalFormat;
840 if (!ValidateCopyTexImageParametersBase(context, target, level, internalformat, isSubImage,
Jamie Madill6f38f822014-06-06 17:12:20 -0400841 xoffset, yoffset, zoffset, x, y, width, height,
842 border, &textureInternalFormat))
Shannon Woods4dfed832014-03-17 20:03:39 -0400843 {
Jamie Madill560a8d82014-05-21 13:06:20 -0400844 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400845 }
846
Shannon Woods53a94a82014-06-24 15:20:36 -0400847 gl::Framebuffer *framebuffer = context->getState().getReadFramebuffer();
Jamie Madill3c7fa222014-06-05 13:08:51 -0400848
849 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
850 {
851 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
852 }
853
Shannon Woods53a94a82014-06-24 15:20:36 -0400854 if (context->getState().getReadFramebuffer()->id() != 0 && framebuffer->getSamples() != 0)
Jamie Madill3c7fa222014-06-05 13:08:51 -0400855 {
856 return gl::error(GL_INVALID_OPERATION, false);
857 }
858
859 gl::FramebufferAttachment *source = framebuffer->getReadColorbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400860 GLenum colorbufferInternalFormat = source->getInternalFormat();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400861
862 if (isSubImage)
863 {
Geoff Lang5d601382014-07-22 15:14:06 -0400864 if (!IsValidES3CopyTexImageCombination(textureInternalFormat, colorbufferInternalFormat,
865 context->getState().getReadFramebuffer()->id()))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400866 {
867 return gl::error(GL_INVALID_OPERATION, false);
868 }
869 }
Shannon Woods4d161ba2014-03-17 18:13:30 -0400870 else
871 {
Geoff Lang5d601382014-07-22 15:14:06 -0400872 if (!gl::IsValidES3CopyTexImageCombination(internalformat, colorbufferInternalFormat,
873 context->getState().getReadFramebuffer()->id()))
Shannon Woods4d161ba2014-03-17 18:13:30 -0400874 {
875 return gl::error(GL_INVALID_OPERATION, false);
876 }
877 }
878
Geoff Lang784a8fd2013-09-24 12:33:16 -0400879 // If width or height is zero, it is a no-op. Return false without setting an error.
880 return (width > 0 && height > 0);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400881}
882
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400883bool ValidateES3TexStorageParameters(gl::Context *context, GLenum target, GLsizei levels, GLenum internalformat,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400884 GLsizei width, GLsizei height, GLsizei depth)
885{
886 if (width < 1 || height < 1 || depth < 1 || levels < 1)
887 {
888 return gl::error(GL_INVALID_VALUE, false);
889 }
890
891 if (levels > gl::log2(std::max(std::max(width, height), depth)) + 1)
892 {
893 return gl::error(GL_INVALID_OPERATION, false);
894 }
895
Geoff Langaae65a42014-05-26 12:43:44 -0400896 const gl::Caps &caps = context->getCaps();
897
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400898 gl::Texture *texture = NULL;
899 switch (target)
900 {
901 case GL_TEXTURE_2D:
902 {
903 texture = context->getTexture2D();
904
Geoff Langaae65a42014-05-26 12:43:44 -0400905 if (static_cast<GLuint>(width) > caps.max2DTextureSize ||
906 static_cast<GLuint>(height) > caps.max2DTextureSize)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400907 {
908 return gl::error(GL_INVALID_VALUE, false);
909 }
910 }
911 break;
912
Geoff Lang01c21d22013-09-24 11:52:16 -0400913 case GL_TEXTURE_CUBE_MAP:
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400914 {
915 texture = context->getTextureCubeMap();
916
917 if (width != height)
918 {
919 return gl::error(GL_INVALID_VALUE, false);
920 }
921
Geoff Langaae65a42014-05-26 12:43:44 -0400922 if (static_cast<GLuint>(width) > caps.maxCubeMapTextureSize)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400923 {
924 return gl::error(GL_INVALID_VALUE, false);
925 }
926 }
927 break;
928
929 case GL_TEXTURE_3D:
930 {
931 texture = context->getTexture3D();
932
Geoff Langaae65a42014-05-26 12:43:44 -0400933 if (static_cast<GLuint>(width) > caps.max3DTextureSize ||
934 static_cast<GLuint>(height) > caps.max3DTextureSize ||
935 static_cast<GLuint>(depth) > caps.max3DTextureSize)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400936 {
937 return gl::error(GL_INVALID_VALUE, false);
938 }
939 }
940 break;
941
942 case GL_TEXTURE_2D_ARRAY:
943 {
944 texture = context->getTexture2DArray();
945
Geoff Langaae65a42014-05-26 12:43:44 -0400946 if (static_cast<GLuint>(width) > caps.max2DTextureSize ||
947 static_cast<GLuint>(height) > caps.max2DTextureSize ||
948 static_cast<GLuint>(depth) > caps.maxArrayTextureLayers)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400949 {
950 return gl::error(GL_INVALID_VALUE, false);
951 }
952 }
953 break;
954
955 default:
956 return gl::error(GL_INVALID_ENUM, false);
957 }
958
959 if (!texture || texture->id() == 0)
960 {
961 return gl::error(GL_INVALID_OPERATION, false);
962 }
963
964 if (texture->isImmutable())
965 {
966 return gl::error(GL_INVALID_OPERATION, false);
967 }
968
Geoff Lang5d601382014-07-22 15:14:06 -0400969 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalformat);
970 if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions()))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400971 {
972 return gl::error(GL_INVALID_ENUM, false);
973 }
974
Geoff Lang5d601382014-07-22 15:14:06 -0400975 if (formatInfo.pixelBytes == 0)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400976 {
977 return gl::error(GL_INVALID_ENUM, false);
978 }
979
980 return true;
981}
982
Jamie Madill570f7c82014-07-03 10:38:54 -0400983bool ValidateFramebufferTextureLayer(const gl::Context *context, GLenum target, GLenum attachment,
984 GLuint texture, GLint level, GLint layer)
985{
986 if (context->getClientVersion() < 3)
987 {
988 return gl::error(GL_INVALID_OPERATION, false);
989 }
990
Jamie Madill55ec3b12014-07-03 10:38:57 -0400991 if (layer < 0)
992 {
993 return gl::error(GL_INVALID_VALUE, false);
994 }
995
996 if (!ValidateFramebufferTextureBase(context, target, attachment, texture, level))
997 {
998 return false;
999 }
1000
1001 const gl::Caps &caps = context->getCaps();
1002 if (texture != 0)
1003 {
1004 gl::Texture *tex = context->getTexture(texture);
1005 ASSERT(tex);
1006
1007 switch (tex->getTarget())
1008 {
1009 case GL_TEXTURE_2D_ARRAY:
1010 {
1011 if (level > gl::log2(caps.max2DTextureSize))
1012 {
1013 return gl::error(GL_INVALID_VALUE, false);
1014 }
1015
1016 if (static_cast<GLuint>(layer) >= caps.maxArrayTextureLayers)
1017 {
1018 return gl::error(GL_INVALID_VALUE, false);
1019 }
1020
1021 gl::Texture2DArray *texArray = static_cast<gl::Texture2DArray *>(tex);
1022 if (texArray->isCompressed(level))
1023 {
1024 return gl::error(GL_INVALID_OPERATION, false);
1025 }
1026 }
1027 break;
1028
1029 case GL_TEXTURE_3D:
1030 {
1031 if (level > gl::log2(caps.max3DTextureSize))
1032 {
1033 return gl::error(GL_INVALID_VALUE, false);
1034 }
1035
1036 if (static_cast<GLuint>(layer) >= caps.max3DTextureSize)
1037 {
1038 return gl::error(GL_INVALID_VALUE, false);
1039 }
1040
1041 gl::Texture3D *tex3d = static_cast<gl::Texture3D *>(tex);
1042 if (tex3d->isCompressed(level))
1043 {
1044 return gl::error(GL_INVALID_OPERATION, false);
1045 }
1046 }
1047 break;
1048
1049 default:
1050 return gl::error(GL_INVALID_OPERATION, false);
1051 }
1052 }
1053
1054 return true;
Jamie Madill570f7c82014-07-03 10:38:54 -04001055}
1056
Geoff Langbdc9b2f2014-04-16 14:41:54 -04001057bool ValidES3ReadFormatType(gl::Context *context, GLenum internalFormat, GLenum format, GLenum type)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001058{
Geoff Lang5d601382014-07-22 15:14:06 -04001059 const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(internalFormat);
1060
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001061 switch (format)
1062 {
1063 case GL_RGBA:
1064 switch (type)
1065 {
1066 case GL_UNSIGNED_BYTE:
1067 break;
1068 case GL_UNSIGNED_INT_2_10_10_10_REV:
1069 if (internalFormat != GL_RGB10_A2)
1070 {
1071 return false;
1072 }
1073 break;
Geoff Lang1ec57f82013-10-16 11:43:23 -04001074 case GL_FLOAT:
Geoff Lang5d601382014-07-22 15:14:06 -04001075 if (internalFormatInfo.componentType != GL_FLOAT)
Geoff Lang1ec57f82013-10-16 11:43:23 -04001076 {
1077 return false;
1078 }
1079 break;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001080 default:
1081 return false;
1082 }
1083 break;
1084 case GL_RGBA_INTEGER:
1085 switch (type)
1086 {
1087 case GL_INT:
Geoff Lang5d601382014-07-22 15:14:06 -04001088 if (internalFormatInfo.componentType != GL_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001089 {
1090 return false;
1091 }
1092 break;
1093 case GL_UNSIGNED_INT:
Geoff Lang5d601382014-07-22 15:14:06 -04001094 if (internalFormatInfo.componentType != GL_UNSIGNED_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001095 {
1096 return false;
1097 }
1098 break;
1099 default:
1100 return false;
1101 }
1102 break;
1103 case GL_BGRA_EXT:
1104 switch (type)
1105 {
1106 case GL_UNSIGNED_BYTE:
1107 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
1108 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
1109 break;
1110 default:
1111 return false;
1112 }
1113 break;
Geoff Langbdc9b2f2014-04-16 14:41:54 -04001114 case GL_RG_EXT:
1115 case GL_RED_EXT:
Geoff Langc0b9ef42014-07-02 10:02:37 -04001116 if (!context->getExtensions().textureRG)
Geoff Langbdc9b2f2014-04-16 14:41:54 -04001117 {
1118 return false;
1119 }
1120 switch (type)
1121 {
1122 case GL_UNSIGNED_BYTE:
1123 break;
1124 default:
1125 return false;
1126 }
1127 break;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001128 default:
1129 return false;
1130 }
1131 return true;
1132}
1133
Geoff Lang34dbb6f2013-08-05 15:05:47 -04001134bool ValidateInvalidateFramebufferParameters(gl::Context *context, GLenum target, GLsizei numAttachments,
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001135 const GLenum* attachments)
1136{
1137 bool defaultFramebuffer = false;
1138
1139 switch (target)
1140 {
1141 case GL_DRAW_FRAMEBUFFER:
1142 case GL_FRAMEBUFFER:
Shannon Woods53a94a82014-06-24 15:20:36 -04001143 defaultFramebuffer = context->getState().getDrawFramebuffer()->id() == 0;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001144 break;
1145 case GL_READ_FRAMEBUFFER:
Shannon Woods53a94a82014-06-24 15:20:36 -04001146 defaultFramebuffer = context->getState().getReadFramebuffer()->id() == 0;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001147 break;
1148 default:
1149 return gl::error(GL_INVALID_ENUM, false);
1150 }
1151
1152 for (int i = 0; i < numAttachments; ++i)
1153 {
1154 if (attachments[i] >= GL_COLOR_ATTACHMENT0 && attachments[i] <= GL_COLOR_ATTACHMENT15)
1155 {
1156 if (defaultFramebuffer)
1157 {
1158 return gl::error(GL_INVALID_ENUM, false);
1159 }
1160
Geoff Langaae65a42014-05-26 12:43:44 -04001161 if (attachments[i] >= GL_COLOR_ATTACHMENT0 + context->getCaps().maxColorAttachments)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001162 {
1163 return gl::error(GL_INVALID_OPERATION, false);
1164 }
1165 }
1166 else
1167 {
1168 switch (attachments[i])
1169 {
1170 case GL_DEPTH_ATTACHMENT:
1171 case GL_STENCIL_ATTACHMENT:
1172 case GL_DEPTH_STENCIL_ATTACHMENT:
1173 if (defaultFramebuffer)
1174 {
1175 return gl::error(GL_INVALID_ENUM, false);
1176 }
1177 break;
1178 case GL_COLOR:
1179 case GL_DEPTH:
1180 case GL_STENCIL:
1181 if (!defaultFramebuffer)
1182 {
1183 return gl::error(GL_INVALID_ENUM, false);
1184 }
1185 break;
1186 default:
1187 return gl::error(GL_INVALID_ENUM, false);
1188 }
1189 }
1190 }
1191
1192 return true;
1193}
1194
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001195bool ValidateClearBuffer(const gl::Context *context)
1196{
1197 if (context->getClientVersion() < 3)
1198 {
1199 return gl::error(GL_INVALID_OPERATION, false);
1200 }
1201
Shannon Woods53a94a82014-06-24 15:20:36 -04001202 const gl::Framebuffer *fbo = context->getState().getDrawFramebuffer();
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001203 if (!fbo || fbo->completeness() != GL_FRAMEBUFFER_COMPLETE)
1204 {
1205 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
1206 }
1207
1208 return true;
1209}
1210
Jamie Madill0063c512014-08-25 15:47:53 -04001211bool ValidateGetUniformuiv(const gl::Context *context, GLuint program, GLint location, GLuint* params)
1212{
1213 if (context->getClientVersion() < 3)
1214 {
1215 return gl::error(GL_INVALID_OPERATION, false);
1216 }
1217
Jamie Madill78f41802014-08-25 15:47:55 -04001218 return ValidateGetUniformBase(context, program, location);
Jamie Madill0063c512014-08-25 15:47:53 -04001219}
1220
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001221}