blob: f2f1ebc84c94599b712cef905f8ed5c63c2ec78b [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"
19
20namespace gl
21{
22
Geoff Lang5d601382014-07-22 15:14:06 -040023struct ES3FormatCombination
24{
25 GLenum internalFormat;
26 GLenum format;
27 GLenum type;
28};
29
30bool operator<(const ES3FormatCombination& a, const ES3FormatCombination& b)
31{
32 return memcmp(&a, &b, sizeof(ES3FormatCombination)) < 0;
33}
34
35typedef std::set<ES3FormatCombination> ES3FormatCombinationSet;
36
37static inline void InsertES3FormatCombo(ES3FormatCombinationSet *set, GLenum internalFormat, GLenum format, GLenum type)
38{
39 ES3FormatCombination info;
40 info.internalFormat = internalFormat;
41 info.format = format;
42 info.type = type;
43 set->insert(info);
44}
45
46ES3FormatCombinationSet BuildES3FormatSet()
47{
48 ES3FormatCombinationSet set;
49
50 // Format combinations from ES 3.0.1 spec, table 3.2
51
52 // | Internal format | Format | Type |
53 // | | | |
54 InsertES3FormatCombo(&set, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE );
55 InsertES3FormatCombo(&set, GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_BYTE );
56 InsertES3FormatCombo(&set, GL_RGBA4, GL_RGBA, GL_UNSIGNED_BYTE );
57 InsertES3FormatCombo(&set, GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE );
58 InsertES3FormatCombo(&set, GL_RGBA8_SNORM, GL_RGBA, GL_BYTE );
59 InsertES3FormatCombo(&set, GL_RGBA4, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4 );
60 InsertES3FormatCombo(&set, GL_RGB10_A2, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV );
61 InsertES3FormatCombo(&set, GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV );
62 InsertES3FormatCombo(&set, GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1 );
63 InsertES3FormatCombo(&set, GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT );
64 InsertES3FormatCombo(&set, GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT_OES );
65 InsertES3FormatCombo(&set, GL_RGBA32F, GL_RGBA, GL_FLOAT );
66 InsertES3FormatCombo(&set, GL_RGBA16F, GL_RGBA, GL_FLOAT );
67 InsertES3FormatCombo(&set, GL_RGBA8UI, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE );
68 InsertES3FormatCombo(&set, GL_RGBA8I, GL_RGBA_INTEGER, GL_BYTE );
69 InsertES3FormatCombo(&set, GL_RGBA16UI, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT );
70 InsertES3FormatCombo(&set, GL_RGBA16I, GL_RGBA_INTEGER, GL_SHORT );
71 InsertES3FormatCombo(&set, GL_RGBA32UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT );
72 InsertES3FormatCombo(&set, GL_RGBA32I, GL_RGBA_INTEGER, GL_INT );
73 InsertES3FormatCombo(&set, GL_RGB10_A2UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV );
74 InsertES3FormatCombo(&set, GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE );
75 InsertES3FormatCombo(&set, GL_RGB565, GL_RGB, GL_UNSIGNED_BYTE );
76 InsertES3FormatCombo(&set, GL_SRGB8, GL_RGB, GL_UNSIGNED_BYTE );
77 InsertES3FormatCombo(&set, GL_RGB8_SNORM, GL_RGB, GL_BYTE );
78 InsertES3FormatCombo(&set, GL_RGB565, GL_RGB, GL_UNSIGNED_SHORT_5_6_5 );
79 InsertES3FormatCombo(&set, GL_R11F_G11F_B10F, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV );
80 InsertES3FormatCombo(&set, GL_RGB9_E5, GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV );
81 InsertES3FormatCombo(&set, GL_RGB16F, GL_RGB, GL_HALF_FLOAT );
82 InsertES3FormatCombo(&set, GL_RGB16F, GL_RGB, GL_HALF_FLOAT_OES );
83 InsertES3FormatCombo(&set, GL_R11F_G11F_B10F, GL_RGB, GL_HALF_FLOAT );
84 InsertES3FormatCombo(&set, GL_R11F_G11F_B10F, GL_RGB, GL_HALF_FLOAT_OES );
85 InsertES3FormatCombo(&set, GL_RGB9_E5, GL_RGB, GL_HALF_FLOAT );
86 InsertES3FormatCombo(&set, GL_RGB9_E5, GL_RGB, GL_HALF_FLOAT_OES );
87 InsertES3FormatCombo(&set, GL_RGB32F, GL_RGB, GL_FLOAT );
88 InsertES3FormatCombo(&set, GL_RGB16F, GL_RGB, GL_FLOAT );
89 InsertES3FormatCombo(&set, GL_R11F_G11F_B10F, GL_RGB, GL_FLOAT );
90 InsertES3FormatCombo(&set, GL_RGB9_E5, GL_RGB, GL_FLOAT );
91 InsertES3FormatCombo(&set, GL_RGB8UI, GL_RGB_INTEGER, GL_UNSIGNED_BYTE );
92 InsertES3FormatCombo(&set, GL_RGB8I, GL_RGB_INTEGER, GL_BYTE );
93 InsertES3FormatCombo(&set, GL_RGB16UI, GL_RGB_INTEGER, GL_UNSIGNED_SHORT );
94 InsertES3FormatCombo(&set, GL_RGB16I, GL_RGB_INTEGER, GL_SHORT );
95 InsertES3FormatCombo(&set, GL_RGB32UI, GL_RGB_INTEGER, GL_UNSIGNED_INT );
96 InsertES3FormatCombo(&set, GL_RGB32I, GL_RGB_INTEGER, GL_INT );
97 InsertES3FormatCombo(&set, GL_RG8, GL_RG, GL_UNSIGNED_BYTE );
98 InsertES3FormatCombo(&set, GL_RG8_SNORM, GL_RG, GL_BYTE );
99 InsertES3FormatCombo(&set, GL_RG16F, GL_RG, GL_HALF_FLOAT );
100 InsertES3FormatCombo(&set, GL_RG16F, GL_RG, GL_HALF_FLOAT_OES );
101 InsertES3FormatCombo(&set, GL_RG32F, GL_RG, GL_FLOAT );
102 InsertES3FormatCombo(&set, GL_RG16F, GL_RG, GL_FLOAT );
103 InsertES3FormatCombo(&set, GL_RG8UI, GL_RG_INTEGER, GL_UNSIGNED_BYTE );
104 InsertES3FormatCombo(&set, GL_RG8I, GL_RG_INTEGER, GL_BYTE );
105 InsertES3FormatCombo(&set, GL_RG16UI, GL_RG_INTEGER, GL_UNSIGNED_SHORT );
106 InsertES3FormatCombo(&set, GL_RG16I, GL_RG_INTEGER, GL_SHORT );
107 InsertES3FormatCombo(&set, GL_RG32UI, GL_RG_INTEGER, GL_UNSIGNED_INT );
108 InsertES3FormatCombo(&set, GL_RG32I, GL_RG_INTEGER, GL_INT );
109 InsertES3FormatCombo(&set, GL_R8, GL_RED, GL_UNSIGNED_BYTE );
110 InsertES3FormatCombo(&set, GL_R8_SNORM, GL_RED, GL_BYTE );
111 InsertES3FormatCombo(&set, GL_R16F, GL_RED, GL_HALF_FLOAT );
112 InsertES3FormatCombo(&set, GL_R16F, GL_RED, GL_HALF_FLOAT_OES );
113 InsertES3FormatCombo(&set, GL_R32F, GL_RED, GL_FLOAT );
114 InsertES3FormatCombo(&set, GL_R16F, GL_RED, GL_FLOAT );
115 InsertES3FormatCombo(&set, GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE );
116 InsertES3FormatCombo(&set, GL_R8I, GL_RED_INTEGER, GL_BYTE );
117 InsertES3FormatCombo(&set, GL_R16UI, GL_RED_INTEGER, GL_UNSIGNED_SHORT );
118 InsertES3FormatCombo(&set, GL_R16I, GL_RED_INTEGER, GL_SHORT );
119 InsertES3FormatCombo(&set, GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT );
120 InsertES3FormatCombo(&set, GL_R32I, GL_RED_INTEGER, GL_INT );
121
122 // Unsized formats
123 InsertES3FormatCombo(&set, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE );
124 InsertES3FormatCombo(&set, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4 );
125 InsertES3FormatCombo(&set, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1 );
126 InsertES3FormatCombo(&set, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE );
127 InsertES3FormatCombo(&set, GL_RGB, GL_RGB, GL_UNSIGNED_SHORT_5_6_5 );
128 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE );
129 InsertES3FormatCombo(&set, GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE );
130 InsertES3FormatCombo(&set, GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE );
131 InsertES3FormatCombo(&set, GL_SRGB_ALPHA_EXT, GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE );
132 InsertES3FormatCombo(&set, GL_SRGB_EXT, GL_SRGB_EXT, GL_UNSIGNED_BYTE );
133
134 // Depth stencil formats
135 InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT );
136 InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT );
137 InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT );
138 InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT );
139 InsertES3FormatCombo(&set, GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8 );
140 InsertES3FormatCombo(&set, GL_DEPTH32F_STENCIL8, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
141
142 // From GL_EXT_sRGB
143 InsertES3FormatCombo(&set, GL_SRGB8_ALPHA8_EXT, GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE );
144 InsertES3FormatCombo(&set, GL_SRGB8, GL_SRGB_EXT, GL_UNSIGNED_BYTE );
145
146 // From GL_OES_texture_float
147 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_FLOAT );
148 InsertES3FormatCombo(&set, GL_LUMINANCE, GL_LUMINANCE, GL_FLOAT );
149 InsertES3FormatCombo(&set, GL_ALPHA, GL_ALPHA, GL_FLOAT );
150
151 // From GL_OES_texture_half_float
152 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT );
153 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES );
154 InsertES3FormatCombo(&set, GL_LUMINANCE, GL_LUMINANCE, GL_HALF_FLOAT );
155 InsertES3FormatCombo(&set, GL_LUMINANCE, GL_LUMINANCE, GL_HALF_FLOAT_OES );
156 InsertES3FormatCombo(&set, GL_ALPHA, GL_ALPHA, GL_HALF_FLOAT );
157 InsertES3FormatCombo(&set, GL_ALPHA, GL_ALPHA, GL_HALF_FLOAT_OES );
158
159 // From GL_EXT_texture_format_BGRA8888
160 InsertES3FormatCombo(&set, GL_BGRA_EXT, GL_BGRA_EXT, GL_UNSIGNED_BYTE );
161
162 // From GL_EXT_texture_storage
163 // | Internal format | Format | Type |
164 // | | | |
165 InsertES3FormatCombo(&set, GL_ALPHA8_EXT, GL_ALPHA, GL_UNSIGNED_BYTE );
166 InsertES3FormatCombo(&set, GL_LUMINANCE8_EXT, GL_LUMINANCE, GL_UNSIGNED_BYTE );
167 InsertES3FormatCombo(&set, GL_LUMINANCE8_ALPHA8_EXT, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE );
168 InsertES3FormatCombo(&set, GL_ALPHA32F_EXT, GL_ALPHA, GL_FLOAT );
169 InsertES3FormatCombo(&set, GL_LUMINANCE32F_EXT, GL_LUMINANCE, GL_FLOAT );
170 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA32F_EXT, GL_LUMINANCE_ALPHA, GL_FLOAT );
171 InsertES3FormatCombo(&set, GL_ALPHA16F_EXT, GL_ALPHA, GL_HALF_FLOAT );
172 InsertES3FormatCombo(&set, GL_ALPHA16F_EXT, GL_ALPHA, GL_HALF_FLOAT_OES );
173 InsertES3FormatCombo(&set, GL_LUMINANCE16F_EXT, GL_LUMINANCE, GL_HALF_FLOAT );
174 InsertES3FormatCombo(&set, GL_LUMINANCE16F_EXT, GL_LUMINANCE, GL_HALF_FLOAT_OES );
175 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA16F_EXT, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT );
176 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA16F_EXT, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES );
177
178 // From GL_EXT_texture_storage and GL_EXT_texture_format_BGRA8888
179 InsertES3FormatCombo(&set, GL_BGRA8_EXT, GL_BGRA_EXT, GL_UNSIGNED_BYTE );
180 InsertES3FormatCombo(&set, GL_BGRA4_ANGLEX, GL_BGRA_EXT, GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT);
181 InsertES3FormatCombo(&set, GL_BGRA4_ANGLEX, GL_BGRA_EXT, GL_UNSIGNED_BYTE );
182 InsertES3FormatCombo(&set, GL_BGR5_A1_ANGLEX, GL_BGRA_EXT, GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT);
183 InsertES3FormatCombo(&set, GL_BGR5_A1_ANGLEX, GL_BGRA_EXT, GL_UNSIGNED_BYTE );
184
185 // From GL_ANGLE_depth_texture
186 InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT32_OES, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT_24_8_OES );
187
188 // Compressed formats
189 // From ES 3.0.1 spec, table 3.16
190 // | Internal format | Format | Type |
191 // | | | |
192 InsertES3FormatCombo(&set, GL_COMPRESSED_R11_EAC, GL_COMPRESSED_R11_EAC, GL_UNSIGNED_BYTE);
193 InsertES3FormatCombo(&set, GL_COMPRESSED_R11_EAC, GL_COMPRESSED_R11_EAC, GL_UNSIGNED_BYTE);
194 InsertES3FormatCombo(&set, GL_COMPRESSED_SIGNED_R11_EAC, GL_COMPRESSED_SIGNED_R11_EAC, GL_UNSIGNED_BYTE);
195 InsertES3FormatCombo(&set, GL_COMPRESSED_RG11_EAC, GL_COMPRESSED_RG11_EAC, GL_UNSIGNED_BYTE);
196 InsertES3FormatCombo(&set, GL_COMPRESSED_SIGNED_RG11_EAC, GL_COMPRESSED_SIGNED_RG11_EAC, GL_UNSIGNED_BYTE);
197 InsertES3FormatCombo(&set, GL_COMPRESSED_RGB8_ETC2, GL_COMPRESSED_RGB8_ETC2, GL_UNSIGNED_BYTE);
198 InsertES3FormatCombo(&set, GL_COMPRESSED_SRGB8_ETC2, GL_COMPRESSED_SRGB8_ETC2, GL_UNSIGNED_BYTE);
199 InsertES3FormatCombo(&set, GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, GL_UNSIGNED_BYTE);
200 InsertES3FormatCombo(&set, GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, GL_UNSIGNED_BYTE);
201 InsertES3FormatCombo(&set, GL_COMPRESSED_RGBA8_ETC2_EAC, GL_COMPRESSED_RGBA8_ETC2_EAC, GL_UNSIGNED_BYTE);
202 InsertES3FormatCombo(&set, GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC, GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC, GL_UNSIGNED_BYTE);
203
204
205 // From GL_EXT_texture_compression_dxt1
206 InsertES3FormatCombo(&set, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, GL_UNSIGNED_BYTE);
207 InsertES3FormatCombo(&set, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_UNSIGNED_BYTE);
208
209 // From GL_ANGLE_texture_compression_dxt3
210 InsertES3FormatCombo(&set, GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE, GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE, GL_UNSIGNED_BYTE);
211
212 // From GL_ANGLE_texture_compression_dxt5
213 InsertES3FormatCombo(&set, GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE, GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE, GL_UNSIGNED_BYTE);
214
215 return set;
216}
217
218static bool ValidateTexImageFormatCombination(gl::Context *context, GLenum internalFormat, GLenum format, GLenum type)
219{
220 // Note: dEQP 2013.4 expects an INVALID_VALUE error for TexImage3D with an invalid
221 // internal format. (dEQP-GLES3.functional.negative_api.texture.teximage3d)
Geoff Langbaadf232014-08-04 13:58:02 -0400222 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalFormat);
Geoff Lang5d601382014-07-22 15:14:06 -0400223 if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions()))
224 {
Geoff Langb1196682014-07-23 13:47:29 -0400225 context->recordError(Error(GL_INVALID_ENUM));
226 return false;
Geoff Lang5d601382014-07-22 15:14:06 -0400227 }
228
229 // The type and format are valid if any supported internal format has that type and format
230 bool formatSupported = false;
231 bool typeSupported = false;
232
233 static const ES3FormatCombinationSet es3FormatSet = BuildES3FormatSet();
234 for (ES3FormatCombinationSet::const_iterator i = es3FormatSet.begin(); i != es3FormatSet.end(); i++)
235 {
236 if (i->format == format || i->type == type)
237 {
238 const gl::InternalFormat &info = gl::GetInternalFormatInfo(i->internalFormat);
239 bool supported = info.textureSupport(context->getClientVersion(), context->getExtensions());
Geoff Langbaadf232014-08-04 13:58:02 -0400240 if (supported && i->type == type)
Geoff Lang5d601382014-07-22 15:14:06 -0400241 {
242 typeSupported = true;
243 }
Geoff Langbaadf232014-08-04 13:58:02 -0400244 if (supported && i->format == format)
Geoff Lang5d601382014-07-22 15:14:06 -0400245 {
246 formatSupported = true;
247 }
Geoff Langbaadf232014-08-04 13:58:02 -0400248
249 // Early-out if both type and format are supported now
250 if (typeSupported && formatSupported)
251 {
252 break;
253 }
Geoff Lang5d601382014-07-22 15:14:06 -0400254 }
255 }
256
257 if (!typeSupported || !formatSupported)
258 {
Geoff Langb1196682014-07-23 13:47:29 -0400259 context->recordError(Error(GL_INVALID_ENUM));
260 return false;
Geoff Lang5d601382014-07-22 15:14:06 -0400261 }
262
263 // Check if this is a valid format combination to load texture data
264 ES3FormatCombination searchFormat;
265 searchFormat.internalFormat = internalFormat;
266 searchFormat.format = format;
267 searchFormat.type = type;
268
269 if (es3FormatSet.find(searchFormat) == es3FormatSet.end())
270 {
Geoff Langb1196682014-07-23 13:47:29 -0400271 context->recordError(Error(GL_INVALID_OPERATION));
272 return false;
Geoff Lang5d601382014-07-22 15:14:06 -0400273 }
274
275 return true;
276}
277
Geoff Langb1196682014-07-23 13:47:29 -0400278bool ValidateES3TexImageParameters(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 {
Geoff Langb1196682014-07-23 13:47:29 -0400284 context->recordError(Error(GL_INVALID_ENUM));
285 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -0400286 }
287
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400288 // Validate image size
Geoff Langce635692013-09-24 13:56:32 -0400289 if (!ValidImageSize(context, target, level, width, height, depth))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400290 {
Geoff Langb1196682014-07-23 13:47:29 -0400291 context->recordError(Error(GL_INVALID_VALUE));
292 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400293 }
294
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400295 // Verify zero border
296 if (border != 0)
297 {
Geoff Langb1196682014-07-23 13:47:29 -0400298 context->recordError(Error(GL_INVALID_VALUE));
299 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400300 }
301
Jamie Madill6f38f822014-06-06 17:12:20 -0400302 if (xoffset < 0 || yoffset < 0 || zoffset < 0 ||
303 std::numeric_limits<GLsizei>::max() - xoffset < width ||
304 std::numeric_limits<GLsizei>::max() - yoffset < height ||
305 std::numeric_limits<GLsizei>::max() - zoffset < depth)
306 {
Geoff Langb1196682014-07-23 13:47:29 -0400307 context->recordError(Error(GL_INVALID_VALUE));
308 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -0400309 }
310
Geoff Langaae65a42014-05-26 12:43:44 -0400311 const gl::Caps &caps = context->getCaps();
312
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400313 gl::Texture *texture = NULL;
314 bool textureCompressed = false;
315 GLenum textureInternalFormat = GL_NONE;
316 GLint textureLevelWidth = 0;
317 GLint textureLevelHeight = 0;
318 GLint textureLevelDepth = 0;
319 switch (target)
320 {
321 case GL_TEXTURE_2D:
322 {
Geoff Langaae65a42014-05-26 12:43:44 -0400323 if (static_cast<GLuint>(width) > (caps.max2DTextureSize >> level) ||
324 static_cast<GLuint>(height) > (caps.max2DTextureSize >> level))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400325 {
Geoff Langb1196682014-07-23 13:47:29 -0400326 context->recordError(Error(GL_INVALID_VALUE));
327 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400328 }
329
330 gl::Texture2D *texture2d = context->getTexture2D();
331 if (texture2d)
332 {
333 textureCompressed = texture2d->isCompressed(level);
334 textureInternalFormat = texture2d->getInternalFormat(level);
335 textureLevelWidth = texture2d->getWidth(level);
336 textureLevelHeight = texture2d->getHeight(level);
337 textureLevelDepth = 1;
338 texture = texture2d;
339 }
340 }
341 break;
342
343 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
344 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
345 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
346 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
347 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
348 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
349 {
350 if (!isSubImage && width != height)
351 {
Geoff Langb1196682014-07-23 13:47:29 -0400352 context->recordError(Error(GL_INVALID_VALUE));
353 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400354 }
355
Geoff Langaae65a42014-05-26 12:43:44 -0400356 if (static_cast<GLuint>(width) > (caps.maxCubeMapTextureSize >> level))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400357 {
Geoff Langb1196682014-07-23 13:47:29 -0400358 context->recordError(Error(GL_INVALID_VALUE));
359 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400360 }
361
362 gl::TextureCubeMap *textureCube = context->getTextureCubeMap();
363 if (textureCube)
364 {
365 textureCompressed = textureCube->isCompressed(target, level);
366 textureInternalFormat = textureCube->getInternalFormat(target, level);
367 textureLevelWidth = textureCube->getWidth(target, level);
368 textureLevelHeight = textureCube->getHeight(target, level);
369 textureLevelDepth = 1;
370 texture = textureCube;
371 }
372 }
373 break;
374
375 case GL_TEXTURE_3D:
376 {
Geoff Langaae65a42014-05-26 12:43:44 -0400377 if (static_cast<GLuint>(width) > (caps.max3DTextureSize >> level) ||
378 static_cast<GLuint>(height) > (caps.max3DTextureSize >> level) ||
379 static_cast<GLuint>(depth) > (caps.max3DTextureSize >> level))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400380 {
Geoff Langb1196682014-07-23 13:47:29 -0400381 context->recordError(Error(GL_INVALID_VALUE));
382 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400383 }
384
385 gl::Texture3D *texture3d = context->getTexture3D();
386 if (texture3d)
387 {
388 textureCompressed = texture3d->isCompressed(level);
389 textureInternalFormat = texture3d->getInternalFormat(level);
390 textureLevelWidth = texture3d->getWidth(level);
391 textureLevelHeight = texture3d->getHeight(level);
392 textureLevelDepth = texture3d->getDepth(level);
393 texture = texture3d;
394 }
395 }
396 break;
397
398 case GL_TEXTURE_2D_ARRAY:
399 {
Geoff Langaae65a42014-05-26 12:43:44 -0400400 if (static_cast<GLuint>(width) > (caps.max2DTextureSize >> level) ||
401 static_cast<GLuint>(height) > (caps.max2DTextureSize >> level) ||
402 static_cast<GLuint>(depth) > (caps.maxArrayTextureLayers >> level))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400403 {
Geoff Langb1196682014-07-23 13:47:29 -0400404 context->recordError(Error(GL_INVALID_VALUE));
405 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400406 }
407
408 gl::Texture2DArray *texture2darray = context->getTexture2DArray();
409 if (texture2darray)
410 {
411 textureCompressed = texture2darray->isCompressed(level);
412 textureInternalFormat = texture2darray->getInternalFormat(level);
413 textureLevelWidth = texture2darray->getWidth(level);
414 textureLevelHeight = texture2darray->getHeight(level);
Jamie Madillb8f8b892014-01-07 10:12:50 -0500415 textureLevelDepth = texture2darray->getLayers(level);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400416 texture = texture2darray;
417 }
418 }
419 break;
420
421 default:
Geoff Langb1196682014-07-23 13:47:29 -0400422 context->recordError(Error(GL_INVALID_ENUM));
423 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400424 }
425
426 if (!texture)
427 {
Geoff Langb1196682014-07-23 13:47:29 -0400428 context->recordError(Error(GL_INVALID_OPERATION));
429 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400430 }
431
432 if (texture->isImmutable() && !isSubImage)
433 {
Geoff Langb1196682014-07-23 13:47:29 -0400434 context->recordError(Error(GL_INVALID_OPERATION));
435 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400436 }
437
438 // Validate texture formats
439 GLenum actualInternalFormat = isSubImage ? textureInternalFormat : internalformat;
Geoff Lang5d601382014-07-22 15:14:06 -0400440 const gl::InternalFormat &actualFormatInfo = gl::GetInternalFormatInfo(actualInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400441 if (isCompressed)
442 {
Geoff Langd4f180b2013-09-24 13:57:44 -0400443 if (!ValidCompressedImageSize(context, actualInternalFormat, width, height))
444 {
Geoff Langb1196682014-07-23 13:47:29 -0400445 context->recordError(Error(GL_INVALID_OPERATION));
446 return false;
Geoff Langd4f180b2013-09-24 13:57:44 -0400447 }
448
Geoff Lang5d601382014-07-22 15:14:06 -0400449 if (!actualFormatInfo.compressed)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400450 {
Geoff Langb1196682014-07-23 13:47:29 -0400451 context->recordError(Error(GL_INVALID_ENUM));
452 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400453 }
454
455 if (target == GL_TEXTURE_3D)
456 {
Geoff Langb1196682014-07-23 13:47:29 -0400457 context->recordError(Error(GL_INVALID_OPERATION));
458 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400459 }
460 }
461 else
462 {
Geoff Langbaadf232014-08-04 13:58:02 -0400463 if (!ValidateTexImageFormatCombination(context, actualInternalFormat, format, type))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400464 {
Geoff Lang5d601382014-07-22 15:14:06 -0400465 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400466 }
467
468 if (target == GL_TEXTURE_3D && (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL))
469 {
Geoff Langb1196682014-07-23 13:47:29 -0400470 context->recordError(Error(GL_INVALID_OPERATION));
471 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400472 }
473 }
474
475 // Validate sub image parameters
476 if (isSubImage)
477 {
478 if (isCompressed != textureCompressed)
479 {
Geoff Langb1196682014-07-23 13:47:29 -0400480 context->recordError(Error(GL_INVALID_OPERATION));
481 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400482 }
483
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400484 if (isCompressed)
485 {
486 if ((width % 4 != 0 && width != textureLevelWidth) ||
487 (height % 4 != 0 && height != textureLevelHeight))
488 {
Geoff Langb1196682014-07-23 13:47:29 -0400489 context->recordError(Error(GL_INVALID_OPERATION));
490 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400491 }
492 }
493
494 if (width == 0 || height == 0 || depth == 0)
495 {
496 return false;
497 }
498
499 if (xoffset < 0 || yoffset < 0 || zoffset < 0)
500 {
Geoff Langb1196682014-07-23 13:47:29 -0400501 context->recordError(Error(GL_INVALID_VALUE));
502 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400503 }
504
505 if (std::numeric_limits<GLsizei>::max() - xoffset < width ||
506 std::numeric_limits<GLsizei>::max() - yoffset < height ||
507 std::numeric_limits<GLsizei>::max() - zoffset < depth)
508 {
Geoff Langb1196682014-07-23 13:47:29 -0400509 context->recordError(Error(GL_INVALID_VALUE));
510 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400511 }
512
513 if (xoffset + width > textureLevelWidth ||
514 yoffset + height > textureLevelHeight ||
515 zoffset + depth > textureLevelDepth)
516 {
Geoff Langb1196682014-07-23 13:47:29 -0400517 context->recordError(Error(GL_INVALID_VALUE));
518 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400519 }
520 }
521
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400522 // Check for pixel unpack buffer related API errors
Shannon Woods53a94a82014-06-24 15:20:36 -0400523 gl::Buffer *pixelUnpackBuffer = context->getState().getTargetBuffer(GL_PIXEL_UNPACK_BUFFER);
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400524 if (pixelUnpackBuffer != NULL)
525 {
526 // ...the data would be unpacked from the buffer object such that the memory reads required
527 // would exceed the data store size.
528 size_t widthSize = static_cast<size_t>(width);
529 size_t heightSize = static_cast<size_t>(height);
530 size_t depthSize = static_cast<size_t>(depth);
Geoff Lang5d601382014-07-22 15:14:06 -0400531 GLenum sizedFormat = GetSizedInternalFormat(actualInternalFormat, type);
Jamie Madill6f38f822014-06-06 17:12:20 -0400532
Geoff Lang5d601382014-07-22 15:14:06 -0400533 size_t pixelBytes = static_cast<size_t>(gl::GetInternalFormatInfo(sizedFormat).pixelBytes);
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400534
535 if (!rx::IsUnsignedMultiplicationSafe(widthSize, heightSize) ||
536 !rx::IsUnsignedMultiplicationSafe(widthSize * heightSize, depthSize) ||
537 !rx::IsUnsignedMultiplicationSafe(widthSize * heightSize * depthSize, pixelBytes))
538 {
539 // Overflow past the end of the buffer
Geoff Langb1196682014-07-23 13:47:29 -0400540 context->recordError(Error(GL_INVALID_OPERATION));
541 return false;
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400542 }
543
Jamie Madillc751d1e2014-10-21 17:46:29 -0400544 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(sizedFormat);
545 size_t copyBytes = formatInfo.computeBlockSize(type, width, height);
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400546 size_t offset = reinterpret_cast<size_t>(pixels);
547
Jamie Madill6f38f822014-06-06 17:12:20 -0400548 if (!rx::IsUnsignedAdditionSafe(offset, copyBytes) ||
Brandon Jonesd38f9262014-06-18 16:26:45 -0700549 ((offset + copyBytes) > static_cast<size_t>(pixelUnpackBuffer->getSize())))
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400550 {
551 // Overflow past the end of the buffer
Geoff Langb1196682014-07-23 13:47:29 -0400552 context->recordError(Error(GL_INVALID_OPERATION));
553 return false;
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400554 }
555
556 // ...data is not evenly divisible into the number of bytes needed to store in memory a datum
557 // indicated by type.
Jamie Madillc751d1e2014-10-21 17:46:29 -0400558 if (!isCompressed)
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400559 {
Jamie Madillc751d1e2014-10-21 17:46:29 -0400560 size_t dataBytesPerPixel = static_cast<size_t>(gl::GetTypeInfo(type).bytes);
561
562 if ((offset % dataBytesPerPixel) != 0)
563 {
564 context->recordError(Error(GL_INVALID_OPERATION));
565 return false;
566 }
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400567 }
568
Jamie Madill7a5f7382014-03-05 15:01:24 -0500569 // ...the buffer object's data store is currently mapped.
Brandon Jonesd38f9262014-06-18 16:26:45 -0700570 if (pixelUnpackBuffer->isMapped())
Jamie Madill7a5f7382014-03-05 15:01:24 -0500571 {
Geoff Langb1196682014-07-23 13:47:29 -0400572 context->recordError(Error(GL_INVALID_OPERATION));
573 return false;
Jamie Madill7a5f7382014-03-05 15:01:24 -0500574 }
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400575 }
576
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400577 return true;
578}
579
Geoff Lang5d601382014-07-22 15:14:06 -0400580struct EffectiveInternalFormatInfo
581{
582 GLenum mEffectiveFormat;
583 GLenum mDestFormat;
584 GLuint mMinRedBits;
585 GLuint mMaxRedBits;
586 GLuint mMinGreenBits;
587 GLuint mMaxGreenBits;
588 GLuint mMinBlueBits;
589 GLuint mMaxBlueBits;
590 GLuint mMinAlphaBits;
591 GLuint mMaxAlphaBits;
592
593 EffectiveInternalFormatInfo(GLenum effectiveFormat, GLenum destFormat, GLuint minRedBits, GLuint maxRedBits,
594 GLuint minGreenBits, GLuint maxGreenBits, GLuint minBlueBits, GLuint maxBlueBits,
595 GLuint minAlphaBits, GLuint maxAlphaBits)
596 : mEffectiveFormat(effectiveFormat), mDestFormat(destFormat), mMinRedBits(minRedBits),
597 mMaxRedBits(maxRedBits), mMinGreenBits(minGreenBits), mMaxGreenBits(maxGreenBits),
598 mMinBlueBits(minBlueBits), mMaxBlueBits(maxBlueBits), mMinAlphaBits(minAlphaBits),
599 mMaxAlphaBits(maxAlphaBits) {};
600};
601
602typedef std::vector<EffectiveInternalFormatInfo> EffectiveInternalFormatList;
603
604static EffectiveInternalFormatList BuildSizedEffectiveInternalFormatList()
605{
606 EffectiveInternalFormatList list;
607
608 // OpenGL ES 3.0.3 Specification, Table 3.17, pg 141: Effective internal format coresponding to destination internal format and
609 // linear source buffer component sizes.
610 // | Source channel min/max sizes |
611 // Effective Internal Format | N/A | R | G | B | A |
612 list.push_back(EffectiveInternalFormatInfo(GL_ALPHA8_EXT, GL_NONE, 0, 0, 0, 0, 0, 0, 1, 8));
613 list.push_back(EffectiveInternalFormatInfo(GL_R8, GL_NONE, 1, 8, 0, 0, 0, 0, 0, 0));
614 list.push_back(EffectiveInternalFormatInfo(GL_RG8, GL_NONE, 1, 8, 1, 8, 0, 0, 0, 0));
615 list.push_back(EffectiveInternalFormatInfo(GL_RGB565, GL_NONE, 1, 5, 1, 6, 1, 5, 0, 0));
616 list.push_back(EffectiveInternalFormatInfo(GL_RGB8, GL_NONE, 6, 8, 7, 8, 6, 8, 0, 0));
617 list.push_back(EffectiveInternalFormatInfo(GL_RGBA4, GL_NONE, 1, 4, 1, 4, 1, 4, 1, 4));
618 list.push_back(EffectiveInternalFormatInfo(GL_RGB5_A1, GL_NONE, 5, 5, 5, 5, 5, 5, 1, 1));
619 list.push_back(EffectiveInternalFormatInfo(GL_RGBA8, GL_NONE, 5, 8, 5, 8, 5, 8, 2, 8));
620 list.push_back(EffectiveInternalFormatInfo(GL_RGB10_A2, GL_NONE, 9, 10, 9, 10, 9, 10, 2, 2));
621
622 return list;
623}
624
625static EffectiveInternalFormatList BuildUnsizedEffectiveInternalFormatList()
626{
627 EffectiveInternalFormatList list;
628
629 // OpenGL ES 3.0.3 Specification, Table 3.17, pg 141: Effective internal format coresponding to destination internal format and
630 // linear source buffer component sizes.
631 // | Source channel min/max sizes |
632 // Effective Internal Format | Dest Format | R | G | B | A |
633 list.push_back(EffectiveInternalFormatInfo(GL_ALPHA8_EXT, GL_ALPHA, 0, UINT_MAX, 0, UINT_MAX, 0, UINT_MAX, 1, 8));
634 list.push_back(EffectiveInternalFormatInfo(GL_LUMINANCE8_EXT, GL_LUMINANCE, 1, 8, 0, UINT_MAX, 0, UINT_MAX, 0, UINT_MAX));
635 list.push_back(EffectiveInternalFormatInfo(GL_LUMINANCE8_ALPHA8_EXT, GL_LUMINANCE_ALPHA, 1, 8, 0, UINT_MAX, 0, UINT_MAX, 1, 8));
636 list.push_back(EffectiveInternalFormatInfo(GL_RGB565, GL_RGB, 1, 5, 1, 6, 1, 5, 0, UINT_MAX));
637 list.push_back(EffectiveInternalFormatInfo(GL_RGB8, GL_RGB, 6, 8, 7, 8, 6, 8, 0, UINT_MAX));
638 list.push_back(EffectiveInternalFormatInfo(GL_RGBA4, GL_RGBA, 1, 4, 1, 4, 1, 4, 1, 4));
639 list.push_back(EffectiveInternalFormatInfo(GL_RGB5_A1, GL_RGBA, 5, 5, 5, 5, 5, 5, 1, 1));
640 list.push_back(EffectiveInternalFormatInfo(GL_RGBA8, GL_RGBA, 5, 8, 5, 8, 5, 8, 5, 8));
641
642 return list;
643}
644
645static bool GetEffectiveInternalFormat(const InternalFormat &srcFormat, const InternalFormat &destFormat,
646 GLenum *outEffectiveFormat)
647{
648 const EffectiveInternalFormatList *list = NULL;
649 GLenum targetFormat = GL_NONE;
650
651 if (destFormat.pixelBytes > 0)
652 {
653 static const EffectiveInternalFormatList sizedList = BuildSizedEffectiveInternalFormatList();
654 list = &sizedList;
655 }
656 else
657 {
658 static const EffectiveInternalFormatList unsizedList = BuildUnsizedEffectiveInternalFormatList();
659 list = &unsizedList;
660 targetFormat = destFormat.format;
661 }
662
663 for (size_t curFormat = 0; curFormat < list->size(); ++curFormat)
664 {
665 const EffectiveInternalFormatInfo& formatInfo = list->at(curFormat);
666 if ((formatInfo.mDestFormat == targetFormat) &&
667 (formatInfo.mMinRedBits <= srcFormat.redBits && formatInfo.mMaxRedBits >= srcFormat.redBits) &&
668 (formatInfo.mMinGreenBits <= srcFormat.greenBits && formatInfo.mMaxGreenBits >= srcFormat.greenBits) &&
669 (formatInfo.mMinBlueBits <= srcFormat.blueBits && formatInfo.mMaxBlueBits >= srcFormat.blueBits) &&
670 (formatInfo.mMinAlphaBits <= srcFormat.alphaBits && formatInfo.mMaxAlphaBits >= srcFormat.alphaBits))
671 {
672 *outEffectiveFormat = formatInfo.mEffectiveFormat;
673 return true;
674 }
675 }
676
677 return false;
678}
679
680struct CopyConversion
681{
682 GLenum mTextureFormat;
683 GLenum mFramebufferFormat;
684
685 CopyConversion(GLenum textureFormat, GLenum framebufferFormat)
686 : mTextureFormat(textureFormat), mFramebufferFormat(framebufferFormat) { }
687
688 bool operator<(const CopyConversion& other) const
689 {
690 return memcmp(this, &other, sizeof(CopyConversion)) < 0;
691 }
692};
693
694typedef std::set<CopyConversion> CopyConversionSet;
695
696static CopyConversionSet BuildValidES3CopyTexImageCombinations()
697{
698 CopyConversionSet set;
699
700 // From ES 3.0.1 spec, table 3.15
701 set.insert(CopyConversion(GL_ALPHA, GL_RGBA));
702 set.insert(CopyConversion(GL_LUMINANCE, GL_RED));
703 set.insert(CopyConversion(GL_LUMINANCE, GL_RG));
704 set.insert(CopyConversion(GL_LUMINANCE, GL_RGB));
705 set.insert(CopyConversion(GL_LUMINANCE, GL_RGBA));
706 set.insert(CopyConversion(GL_LUMINANCE_ALPHA, GL_RGBA));
707 set.insert(CopyConversion(GL_RED, GL_RED));
708 set.insert(CopyConversion(GL_RED, GL_RG));
709 set.insert(CopyConversion(GL_RED, GL_RGB));
710 set.insert(CopyConversion(GL_RED, GL_RGBA));
711 set.insert(CopyConversion(GL_RG, GL_RG));
712 set.insert(CopyConversion(GL_RG, GL_RGB));
713 set.insert(CopyConversion(GL_RG, GL_RGBA));
714 set.insert(CopyConversion(GL_RGB, GL_RGB));
715 set.insert(CopyConversion(GL_RGB, GL_RGBA));
716 set.insert(CopyConversion(GL_RGBA, GL_RGBA));
717
718 // Necessary for ANGLE back-buffers
719 set.insert(CopyConversion(GL_ALPHA, GL_BGRA_EXT));
720 set.insert(CopyConversion(GL_LUMINANCE, GL_BGRA_EXT));
721 set.insert(CopyConversion(GL_LUMINANCE_ALPHA, GL_BGRA_EXT));
722 set.insert(CopyConversion(GL_RED, GL_BGRA_EXT));
723 set.insert(CopyConversion(GL_RG, GL_BGRA_EXT));
724 set.insert(CopyConversion(GL_RGB, GL_BGRA_EXT));
725 set.insert(CopyConversion(GL_RGBA, GL_BGRA_EXT));
726
727 set.insert(CopyConversion(GL_RED_INTEGER, GL_RED_INTEGER));
728 set.insert(CopyConversion(GL_RED_INTEGER, GL_RG_INTEGER));
729 set.insert(CopyConversion(GL_RED_INTEGER, GL_RGB_INTEGER));
730 set.insert(CopyConversion(GL_RED_INTEGER, GL_RGBA_INTEGER));
731 set.insert(CopyConversion(GL_RG_INTEGER, GL_RG_INTEGER));
732 set.insert(CopyConversion(GL_RG_INTEGER, GL_RGB_INTEGER));
733 set.insert(CopyConversion(GL_RG_INTEGER, GL_RGBA_INTEGER));
734 set.insert(CopyConversion(GL_RGB_INTEGER, GL_RGB_INTEGER));
735 set.insert(CopyConversion(GL_RGB_INTEGER, GL_RGBA_INTEGER));
736 set.insert(CopyConversion(GL_RGBA_INTEGER, GL_RGBA_INTEGER));
737
738 return set;
739}
740
741static bool IsValidES3CopyTexImageCombination(GLenum textureInternalFormat, GLenum frameBufferInternalFormat, GLuint readBufferHandle)
742{
743 const InternalFormat &textureInternalFormatInfo = GetInternalFormatInfo(textureInternalFormat);
744 const InternalFormat &framebufferInternalFormatInfo = GetInternalFormatInfo(frameBufferInternalFormat);
745
746 static const CopyConversionSet conversionSet = BuildValidES3CopyTexImageCombinations();
747 if (conversionSet.find(CopyConversion(textureInternalFormatInfo.format, framebufferInternalFormatInfo.format)) != conversionSet.end())
748 {
749 // Section 3.8.5 of the GLES 3.0.3 spec states that source and destination formats
750 // must both be signed, unsigned, or fixed point and both source and destinations
751 // must be either both SRGB or both not SRGB. EXT_color_buffer_float adds allowed
752 // conversion between fixed and floating point.
753
754 if ((textureInternalFormatInfo.colorEncoding == GL_SRGB) != (framebufferInternalFormatInfo.colorEncoding == GL_SRGB))
755 {
756 return false;
757 }
758
759 if (((textureInternalFormatInfo.componentType == GL_INT) != (framebufferInternalFormatInfo.componentType == GL_INT )) ||
760 ((textureInternalFormatInfo.componentType == GL_UNSIGNED_INT) != (framebufferInternalFormatInfo.componentType == GL_UNSIGNED_INT)))
761 {
762 return false;
763 }
764
765 if ((textureInternalFormatInfo.componentType == GL_UNSIGNED_NORMALIZED ||
766 textureInternalFormatInfo.componentType == GL_SIGNED_NORMALIZED ||
767 textureInternalFormatInfo.componentType == GL_FLOAT) &&
768 !(framebufferInternalFormatInfo.componentType == GL_UNSIGNED_NORMALIZED ||
769 framebufferInternalFormatInfo.componentType == GL_SIGNED_NORMALIZED ||
770 framebufferInternalFormatInfo.componentType == GL_FLOAT))
771 {
772 return false;
773 }
774
775 // GLES specification 3.0.3, sec 3.8.5, pg 139-140:
776 // The effective internal format of the source buffer is determined with the following rules applied in order:
777 // * If the source buffer is a texture or renderbuffer that was created with a sized internal format then the
778 // effective internal format is the source buffer's sized internal format.
779 // * If the source buffer is a texture that was created with an unsized base internal format, then the
780 // effective internal format is the source image array's effective internal format, as specified by table
781 // 3.12, which is determined from the <format> and <type> that were used when the source image array was
782 // specified by TexImage*.
783 // * Otherwise the effective internal format is determined by the row in table 3.17 or 3.18 where
784 // Destination Internal Format matches internalformat and where the [source channel sizes] are consistent
785 // with the values of the source buffer's [channel sizes]. Table 3.17 is used if the
786 // FRAMEBUFFER_ATTACHMENT_ENCODING is LINEAR and table 3.18 is used if the FRAMEBUFFER_ATTACHMENT_ENCODING
787 // is SRGB.
788 const InternalFormat *sourceEffectiveFormat = NULL;
789 if (readBufferHandle != 0)
790 {
791 // Not the default framebuffer, therefore the read buffer must be a user-created texture or renderbuffer
792 if (framebufferInternalFormatInfo.pixelBytes > 0)
793 {
794 sourceEffectiveFormat = &framebufferInternalFormatInfo;
795 }
796 else
797 {
798 // Renderbuffers cannot be created with an unsized internal format, so this must be an unsized-format
799 // texture. We can use the same table we use when creating textures to get its effective sized format.
800 const FormatType &typeInfo = GetFormatTypeInfo(framebufferInternalFormatInfo.format, framebufferInternalFormatInfo.type);
801 sourceEffectiveFormat = &GetInternalFormatInfo(typeInfo.internalFormat);
802 }
803 }
804 else
805 {
806 // The effective internal format must be derived from the source framebuffer's channel sizes.
807 // This is done in GetEffectiveInternalFormat for linear buffers (table 3.17)
808 if (framebufferInternalFormatInfo.colorEncoding == GL_LINEAR)
809 {
810 GLenum effectiveFormat;
811 if (GetEffectiveInternalFormat(framebufferInternalFormatInfo, textureInternalFormatInfo, &effectiveFormat))
812 {
813 sourceEffectiveFormat = &GetInternalFormatInfo(effectiveFormat);
814 }
815 else
816 {
817 return false;
818 }
819 }
820 else if (framebufferInternalFormatInfo.colorEncoding == GL_SRGB)
821 {
822 // SRGB buffers can only be copied to sized format destinations according to table 3.18
823 if ((textureInternalFormatInfo.pixelBytes > 0) &&
824 (framebufferInternalFormatInfo.redBits >= 1 && framebufferInternalFormatInfo.redBits <= 8) &&
825 (framebufferInternalFormatInfo.greenBits >= 1 && framebufferInternalFormatInfo.greenBits <= 8) &&
826 (framebufferInternalFormatInfo.blueBits >= 1 && framebufferInternalFormatInfo.blueBits <= 8) &&
827 (framebufferInternalFormatInfo.alphaBits >= 1 && framebufferInternalFormatInfo.alphaBits <= 8))
828 {
829 sourceEffectiveFormat = &GetInternalFormatInfo(GL_SRGB8_ALPHA8);
830 }
831 else
832 {
833 return false;
834 }
835 }
836 else
837 {
838 UNREACHABLE();
839 return false;
840 }
841 }
842
843 if (textureInternalFormatInfo.pixelBytes > 0)
844 {
845 // Section 3.8.5 of the GLES 3.0.3 spec, pg 139, requires that, if the destination format is sized,
846 // component sizes of the source and destination formats must exactly match
847 if (textureInternalFormatInfo.redBits != sourceEffectiveFormat->redBits ||
848 textureInternalFormatInfo.greenBits != sourceEffectiveFormat->greenBits ||
849 textureInternalFormatInfo.blueBits != sourceEffectiveFormat->blueBits ||
850 textureInternalFormatInfo.alphaBits != sourceEffectiveFormat->alphaBits)
851 {
852 return false;
853 }
854 }
855
856
857 return true; // A conversion function exists, and no rule in the specification has precluded conversion
858 // between these formats.
859 }
860
861 return false;
862}
863
Geoff Langb1196682014-07-23 13:47:29 -0400864bool ValidateES3CopyTexImageParameters(Context *context, GLenum target, GLint level, GLenum internalformat,
Jamie Madill6f38f822014-06-06 17:12:20 -0400865 bool isSubImage, GLint xoffset, GLint yoffset, GLint zoffset,
866 GLint x, GLint y, GLsizei width, GLsizei height, 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
Shannon Woods53a94a82014-06-24 15:20:36 -0400876 gl::Framebuffer *framebuffer = context->getState().getReadFramebuffer();
Jamie Madill3c7fa222014-06-05 13:08:51 -0400877
Geoff Lang748f74e2014-12-01 11:25:34 -0500878 if (framebuffer->checkStatus(context->getData()) != GL_FRAMEBUFFER_COMPLETE)
Jamie Madill3c7fa222014-06-05 13:08:51 -0400879 {
Geoff Langb1196682014-07-23 13:47:29 -0400880 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
881 return false;
Jamie Madill3c7fa222014-06-05 13:08:51 -0400882 }
883
Jamie Madill48faf802014-11-06 15:27:22 -0500884 if (context->getState().getReadFramebuffer()->id() != 0 &&
885 framebuffer->getSamples(context->getData()) != 0)
Jamie Madill3c7fa222014-06-05 13:08:51 -0400886 {
Geoff Langb1196682014-07-23 13:47:29 -0400887 context->recordError(Error(GL_INVALID_OPERATION));
888 return false;
Jamie Madill3c7fa222014-06-05 13:08:51 -0400889 }
890
891 gl::FramebufferAttachment *source = framebuffer->getReadColorbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400892 GLenum colorbufferInternalFormat = source->getInternalFormat();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400893
894 if (isSubImage)
895 {
Geoff Lang5d601382014-07-22 15:14:06 -0400896 if (!IsValidES3CopyTexImageCombination(textureInternalFormat, colorbufferInternalFormat,
897 context->getState().getReadFramebuffer()->id()))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400898 {
Geoff Langb1196682014-07-23 13:47:29 -0400899 context->recordError(Error(GL_INVALID_OPERATION));
900 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400901 }
902 }
Shannon Woods4d161ba2014-03-17 18:13:30 -0400903 else
904 {
Geoff Lang5d601382014-07-22 15:14:06 -0400905 if (!gl::IsValidES3CopyTexImageCombination(internalformat, colorbufferInternalFormat,
906 context->getState().getReadFramebuffer()->id()))
Shannon Woods4d161ba2014-03-17 18:13:30 -0400907 {
Geoff Langb1196682014-07-23 13:47:29 -0400908 context->recordError(Error(GL_INVALID_OPERATION));
909 return false;
Shannon Woods4d161ba2014-03-17 18:13:30 -0400910 }
911 }
912
Geoff Lang784a8fd2013-09-24 12:33:16 -0400913 // If width or height is zero, it is a no-op. Return false without setting an error.
914 return (width > 0 && height > 0);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400915}
916
Geoff Langb1196682014-07-23 13:47:29 -0400917bool ValidateES3TexStorageParameters(Context *context, GLenum target, GLsizei levels, GLenum internalformat,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400918 GLsizei width, GLsizei height, GLsizei depth)
919{
920 if (width < 1 || height < 1 || depth < 1 || levels < 1)
921 {
Geoff Langb1196682014-07-23 13:47:29 -0400922 context->recordError(Error(GL_INVALID_VALUE));
923 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400924 }
925
926 if (levels > gl::log2(std::max(std::max(width, height), depth)) + 1)
927 {
Geoff Langb1196682014-07-23 13:47:29 -0400928 context->recordError(Error(GL_INVALID_OPERATION));
929 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400930 }
931
Geoff Langaae65a42014-05-26 12:43:44 -0400932 const gl::Caps &caps = context->getCaps();
933
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400934 gl::Texture *texture = NULL;
935 switch (target)
936 {
937 case GL_TEXTURE_2D:
938 {
939 texture = context->getTexture2D();
940
Geoff Langaae65a42014-05-26 12:43:44 -0400941 if (static_cast<GLuint>(width) > caps.max2DTextureSize ||
942 static_cast<GLuint>(height) > caps.max2DTextureSize)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400943 {
Geoff Langb1196682014-07-23 13:47:29 -0400944 context->recordError(Error(GL_INVALID_VALUE));
945 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400946 }
947 }
948 break;
949
Geoff Lang01c21d22013-09-24 11:52:16 -0400950 case GL_TEXTURE_CUBE_MAP:
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400951 {
952 texture = context->getTextureCubeMap();
953
954 if (width != height)
955 {
Geoff Langb1196682014-07-23 13:47:29 -0400956 context->recordError(Error(GL_INVALID_VALUE));
957 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400958 }
959
Geoff Langaae65a42014-05-26 12:43:44 -0400960 if (static_cast<GLuint>(width) > caps.maxCubeMapTextureSize)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400961 {
Geoff Langb1196682014-07-23 13:47:29 -0400962 context->recordError(Error(GL_INVALID_VALUE));
963 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400964 }
965 }
966 break;
967
968 case GL_TEXTURE_3D:
969 {
970 texture = context->getTexture3D();
971
Geoff Langaae65a42014-05-26 12:43:44 -0400972 if (static_cast<GLuint>(width) > caps.max3DTextureSize ||
973 static_cast<GLuint>(height) > caps.max3DTextureSize ||
974 static_cast<GLuint>(depth) > caps.max3DTextureSize)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400975 {
Geoff Langb1196682014-07-23 13:47:29 -0400976 context->recordError(Error(GL_INVALID_VALUE));
977 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400978 }
979 }
980 break;
981
982 case GL_TEXTURE_2D_ARRAY:
983 {
984 texture = context->getTexture2DArray();
985
Geoff Langaae65a42014-05-26 12:43:44 -0400986 if (static_cast<GLuint>(width) > caps.max2DTextureSize ||
987 static_cast<GLuint>(height) > caps.max2DTextureSize ||
988 static_cast<GLuint>(depth) > caps.maxArrayTextureLayers)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400989 {
Geoff Langb1196682014-07-23 13:47:29 -0400990 context->recordError(Error(GL_INVALID_VALUE));
991 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400992 }
993 }
994 break;
995
996 default:
Geoff Langb1196682014-07-23 13:47:29 -0400997 context->recordError(Error(GL_INVALID_ENUM));
998 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400999 }
1000
1001 if (!texture || texture->id() == 0)
1002 {
Geoff Langb1196682014-07-23 13:47:29 -04001003 context->recordError(Error(GL_INVALID_OPERATION));
1004 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001005 }
1006
1007 if (texture->isImmutable())
1008 {
Geoff Langb1196682014-07-23 13:47:29 -04001009 context->recordError(Error(GL_INVALID_OPERATION));
1010 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001011 }
1012
Geoff Lang5d601382014-07-22 15:14:06 -04001013 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalformat);
1014 if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions()))
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001015 {
Geoff Langb1196682014-07-23 13:47:29 -04001016 context->recordError(Error(GL_INVALID_ENUM));
1017 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001018 }
1019
Geoff Lang5d601382014-07-22 15:14:06 -04001020 if (formatInfo.pixelBytes == 0)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001021 {
Geoff Langb1196682014-07-23 13:47:29 -04001022 context->recordError(Error(GL_INVALID_ENUM));
1023 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001024 }
1025
1026 return true;
1027}
1028
Geoff Langb1196682014-07-23 13:47:29 -04001029bool ValidateFramebufferTextureLayer(Context *context, GLenum target, GLenum attachment,
Jamie Madill570f7c82014-07-03 10:38:54 -04001030 GLuint texture, GLint level, GLint layer)
1031{
1032 if (context->getClientVersion() < 3)
1033 {
Geoff Langb1196682014-07-23 13:47:29 -04001034 context->recordError(Error(GL_INVALID_OPERATION));
1035 return false;
Jamie Madill570f7c82014-07-03 10:38:54 -04001036 }
1037
Jamie Madill55ec3b12014-07-03 10:38:57 -04001038 if (layer < 0)
1039 {
Geoff Langb1196682014-07-23 13:47:29 -04001040 context->recordError(Error(GL_INVALID_VALUE));
1041 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001042 }
1043
1044 if (!ValidateFramebufferTextureBase(context, target, attachment, texture, level))
1045 {
1046 return false;
1047 }
1048
1049 const gl::Caps &caps = context->getCaps();
1050 if (texture != 0)
1051 {
1052 gl::Texture *tex = context->getTexture(texture);
1053 ASSERT(tex);
1054
1055 switch (tex->getTarget())
1056 {
1057 case GL_TEXTURE_2D_ARRAY:
1058 {
1059 if (level > gl::log2(caps.max2DTextureSize))
1060 {
Geoff Langb1196682014-07-23 13:47:29 -04001061 context->recordError(Error(GL_INVALID_VALUE));
1062 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001063 }
1064
1065 if (static_cast<GLuint>(layer) >= caps.maxArrayTextureLayers)
1066 {
Geoff Langb1196682014-07-23 13:47:29 -04001067 context->recordError(Error(GL_INVALID_VALUE));
1068 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001069 }
1070
1071 gl::Texture2DArray *texArray = static_cast<gl::Texture2DArray *>(tex);
1072 if (texArray->isCompressed(level))
1073 {
Geoff Langb1196682014-07-23 13:47:29 -04001074 context->recordError(Error(GL_INVALID_OPERATION));
1075 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001076 }
1077 }
1078 break;
1079
1080 case GL_TEXTURE_3D:
1081 {
1082 if (level > gl::log2(caps.max3DTextureSize))
1083 {
Geoff Langb1196682014-07-23 13:47:29 -04001084 context->recordError(Error(GL_INVALID_VALUE));
1085 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001086 }
1087
1088 if (static_cast<GLuint>(layer) >= caps.max3DTextureSize)
1089 {
Geoff Langb1196682014-07-23 13:47:29 -04001090 context->recordError(Error(GL_INVALID_VALUE));
1091 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001092 }
1093
1094 gl::Texture3D *tex3d = static_cast<gl::Texture3D *>(tex);
1095 if (tex3d->isCompressed(level))
1096 {
Geoff Langb1196682014-07-23 13:47:29 -04001097 context->recordError(Error(GL_INVALID_OPERATION));
1098 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001099 }
1100 }
1101 break;
1102
1103 default:
Geoff Langb1196682014-07-23 13:47:29 -04001104 context->recordError(Error(GL_INVALID_OPERATION));
1105 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001106 }
1107 }
1108
1109 return true;
Jamie Madill570f7c82014-07-03 10:38:54 -04001110}
1111
Geoff Langb1196682014-07-23 13:47:29 -04001112bool ValidES3ReadFormatType(Context *context, GLenum internalFormat, GLenum format, GLenum type)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001113{
Geoff Lang5d601382014-07-22 15:14:06 -04001114 const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(internalFormat);
1115
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001116 switch (format)
1117 {
1118 case GL_RGBA:
1119 switch (type)
1120 {
1121 case GL_UNSIGNED_BYTE:
1122 break;
1123 case GL_UNSIGNED_INT_2_10_10_10_REV:
1124 if (internalFormat != GL_RGB10_A2)
1125 {
1126 return false;
1127 }
1128 break;
Geoff Lang1ec57f82013-10-16 11:43:23 -04001129 case GL_FLOAT:
Geoff Lang5d601382014-07-22 15:14:06 -04001130 if (internalFormatInfo.componentType != GL_FLOAT)
Geoff Lang1ec57f82013-10-16 11:43:23 -04001131 {
1132 return false;
1133 }
1134 break;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001135 default:
1136 return false;
1137 }
1138 break;
1139 case GL_RGBA_INTEGER:
1140 switch (type)
1141 {
1142 case GL_INT:
Geoff Lang5d601382014-07-22 15:14:06 -04001143 if (internalFormatInfo.componentType != GL_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001144 {
1145 return false;
1146 }
1147 break;
1148 case GL_UNSIGNED_INT:
Geoff Lang5d601382014-07-22 15:14:06 -04001149 if (internalFormatInfo.componentType != GL_UNSIGNED_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001150 {
1151 return false;
1152 }
1153 break;
1154 default:
1155 return false;
1156 }
1157 break;
1158 case GL_BGRA_EXT:
1159 switch (type)
1160 {
1161 case GL_UNSIGNED_BYTE:
1162 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
1163 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
1164 break;
1165 default:
1166 return false;
1167 }
1168 break;
Geoff Langbdc9b2f2014-04-16 14:41:54 -04001169 case GL_RG_EXT:
1170 case GL_RED_EXT:
Geoff Langc0b9ef42014-07-02 10:02:37 -04001171 if (!context->getExtensions().textureRG)
Geoff Langbdc9b2f2014-04-16 14:41:54 -04001172 {
1173 return false;
1174 }
1175 switch (type)
1176 {
1177 case GL_UNSIGNED_BYTE:
1178 break;
1179 default:
1180 return false;
1181 }
1182 break;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001183 default:
1184 return false;
1185 }
1186 return true;
1187}
1188
Corentin Walleze0902642014-11-04 12:32:15 -08001189bool ValidateES3RenderbufferStorageParameters(gl::Context *context, GLenum target, GLsizei samples,
1190 GLenum internalformat, GLsizei width, GLsizei height)
1191{
1192 if (!ValidateRenderbufferStorageParametersBase(context, target, samples, internalformat, width, height))
1193 {
1194 return false;
1195 }
1196
1197 //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.
1198 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalformat);
1199 if ((formatInfo.componentType == GL_UNSIGNED_INT || formatInfo.componentType == GL_INT) && samples > 0)
1200 {
1201 context->recordError(Error(GL_INVALID_OPERATION));
1202 return false;
1203 }
1204
1205 // The behavior is different than the ANGLE version, which would generate a GL_OUT_OF_MEMORY.
1206 const TextureCaps &formatCaps = context->getTextureCaps().get(internalformat);
1207 if (static_cast<GLuint>(samples) > formatCaps.getMaxSamples())
1208 {
1209 context->recordError(Error(GL_INVALID_VALUE));
1210 return false;
1211 }
1212
1213 return true;
1214}
1215
Geoff Langb1196682014-07-23 13:47:29 -04001216bool ValidateInvalidateFramebufferParameters(Context *context, GLenum target, GLsizei numAttachments,
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001217 const GLenum* attachments)
1218{
1219 bool defaultFramebuffer = false;
1220
1221 switch (target)
1222 {
1223 case GL_DRAW_FRAMEBUFFER:
1224 case GL_FRAMEBUFFER:
Shannon Woods53a94a82014-06-24 15:20:36 -04001225 defaultFramebuffer = context->getState().getDrawFramebuffer()->id() == 0;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001226 break;
1227 case GL_READ_FRAMEBUFFER:
Shannon Woods53a94a82014-06-24 15:20:36 -04001228 defaultFramebuffer = context->getState().getReadFramebuffer()->id() == 0;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001229 break;
1230 default:
Geoff Langb1196682014-07-23 13:47:29 -04001231 context->recordError(Error(GL_INVALID_ENUM));
1232 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001233 }
1234
1235 for (int i = 0; i < numAttachments; ++i)
1236 {
1237 if (attachments[i] >= GL_COLOR_ATTACHMENT0 && attachments[i] <= GL_COLOR_ATTACHMENT15)
1238 {
1239 if (defaultFramebuffer)
1240 {
Geoff Langb1196682014-07-23 13:47:29 -04001241 context->recordError(Error(GL_INVALID_ENUM));
1242 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001243 }
1244
Geoff Langaae65a42014-05-26 12:43:44 -04001245 if (attachments[i] >= GL_COLOR_ATTACHMENT0 + context->getCaps().maxColorAttachments)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001246 {
Geoff Langb1196682014-07-23 13:47:29 -04001247 context->recordError(Error(GL_INVALID_OPERATION));
1248 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001249 }
1250 }
1251 else
1252 {
1253 switch (attachments[i])
1254 {
1255 case GL_DEPTH_ATTACHMENT:
1256 case GL_STENCIL_ATTACHMENT:
1257 case GL_DEPTH_STENCIL_ATTACHMENT:
1258 if (defaultFramebuffer)
1259 {
Geoff Langb1196682014-07-23 13:47:29 -04001260 context->recordError(Error(GL_INVALID_ENUM));
1261 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001262 }
1263 break;
1264 case GL_COLOR:
1265 case GL_DEPTH:
1266 case GL_STENCIL:
1267 if (!defaultFramebuffer)
1268 {
Geoff Langb1196682014-07-23 13:47:29 -04001269 context->recordError(Error(GL_INVALID_ENUM));
1270 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001271 }
1272 break;
1273 default:
Geoff Langb1196682014-07-23 13:47:29 -04001274 context->recordError(Error(GL_INVALID_ENUM));
1275 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001276 }
1277 }
1278 }
1279
1280 return true;
1281}
1282
Geoff Langb1196682014-07-23 13:47:29 -04001283bool ValidateClearBuffer(Context *context)
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001284{
1285 if (context->getClientVersion() < 3)
1286 {
Geoff Langb1196682014-07-23 13:47:29 -04001287 context->recordError(Error(GL_INVALID_OPERATION));
1288 return false;
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001289 }
1290
Shannon Woods53a94a82014-06-24 15:20:36 -04001291 const gl::Framebuffer *fbo = context->getState().getDrawFramebuffer();
Geoff Lang748f74e2014-12-01 11:25:34 -05001292 if (!fbo || fbo->checkStatus(context->getData()) != GL_FRAMEBUFFER_COMPLETE)
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001293 {
Geoff Langb1196682014-07-23 13:47:29 -04001294 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
1295 return false;
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001296 }
1297
1298 return true;
1299}
1300
Geoff Langb1196682014-07-23 13:47:29 -04001301bool ValidateGetUniformuiv(Context *context, GLuint program, GLint location, GLuint* params)
Jamie Madill0063c512014-08-25 15:47:53 -04001302{
1303 if (context->getClientVersion() < 3)
1304 {
Geoff Langb1196682014-07-23 13:47:29 -04001305 context->recordError(Error(GL_INVALID_OPERATION));
1306 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001307 }
1308
Jamie Madill78f41802014-08-25 15:47:55 -04001309 return ValidateGetUniformBase(context, program, location);
Jamie Madill0063c512014-08-25 15:47:53 -04001310}
1311
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001312}