blob: 021ec229d3084b4096cc411ef51b1f65a6a346bd [file] [log] [blame]
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001//
Geoff Langcec35902014-04-16 10:52:36 -04002// Copyright (c) 2013-2014 The ANGLE Project Authors. All rights reserved.
Geoff Lange8ebe7f2013-08-05 15:03:13 -04003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// validationES3.cpp: Validation functions for OpenGL ES 3.0 entry point parameters
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/validationES3.h"
10#include "libANGLE/validationES.h"
11#include "libANGLE/Context.h"
12#include "libANGLE/Texture.h"
13#include "libANGLE/Framebuffer.h"
14#include "libANGLE/Renderbuffer.h"
15#include "libANGLE/formatutils.h"
16#include "libANGLE/FramebufferAttachment.h"
Geoff Lange8ebe7f2013-08-05 15:03:13 -040017
18#include "common/mathutil.h"
Geoff Langa9be0dc2014-12-17 12:34:40 -050019#include "common/utilities.h"
Geoff Lange8ebe7f2013-08-05 15:03:13 -040020
21namespace gl
22{
23
Geoff Lang5d601382014-07-22 15:14:06 -040024struct ES3FormatCombination
25{
26 GLenum internalFormat;
27 GLenum format;
28 GLenum type;
29};
30
31bool operator<(const ES3FormatCombination& a, const ES3FormatCombination& b)
32{
33 return memcmp(&a, &b, sizeof(ES3FormatCombination)) < 0;
34}
35
36typedef std::set<ES3FormatCombination> ES3FormatCombinationSet;
37
38static inline void InsertES3FormatCombo(ES3FormatCombinationSet *set, GLenum internalFormat, GLenum format, GLenum type)
39{
40 ES3FormatCombination info;
41 info.internalFormat = internalFormat;
42 info.format = format;
43 info.type = type;
44 set->insert(info);
45}
46
47ES3FormatCombinationSet BuildES3FormatSet()
48{
49 ES3FormatCombinationSet set;
50
51 // Format combinations from ES 3.0.1 spec, table 3.2
52
53 // | Internal format | Format | Type |
54 // | | | |
55 InsertES3FormatCombo(&set, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE );
56 InsertES3FormatCombo(&set, GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_BYTE );
57 InsertES3FormatCombo(&set, GL_RGBA4, GL_RGBA, GL_UNSIGNED_BYTE );
58 InsertES3FormatCombo(&set, GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE );
59 InsertES3FormatCombo(&set, GL_RGBA8_SNORM, GL_RGBA, GL_BYTE );
60 InsertES3FormatCombo(&set, GL_RGBA4, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4 );
61 InsertES3FormatCombo(&set, GL_RGB10_A2, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV );
62 InsertES3FormatCombo(&set, GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV );
63 InsertES3FormatCombo(&set, GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1 );
64 InsertES3FormatCombo(&set, GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT );
65 InsertES3FormatCombo(&set, GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT_OES );
66 InsertES3FormatCombo(&set, GL_RGBA32F, GL_RGBA, GL_FLOAT );
67 InsertES3FormatCombo(&set, GL_RGBA16F, GL_RGBA, GL_FLOAT );
68 InsertES3FormatCombo(&set, GL_RGBA8UI, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE );
69 InsertES3FormatCombo(&set, GL_RGBA8I, GL_RGBA_INTEGER, GL_BYTE );
70 InsertES3FormatCombo(&set, GL_RGBA16UI, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT );
71 InsertES3FormatCombo(&set, GL_RGBA16I, GL_RGBA_INTEGER, GL_SHORT );
72 InsertES3FormatCombo(&set, GL_RGBA32UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT );
73 InsertES3FormatCombo(&set, GL_RGBA32I, GL_RGBA_INTEGER, GL_INT );
74 InsertES3FormatCombo(&set, GL_RGB10_A2UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV );
75 InsertES3FormatCombo(&set, GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE );
76 InsertES3FormatCombo(&set, GL_RGB565, GL_RGB, GL_UNSIGNED_BYTE );
77 InsertES3FormatCombo(&set, GL_SRGB8, GL_RGB, GL_UNSIGNED_BYTE );
78 InsertES3FormatCombo(&set, GL_RGB8_SNORM, GL_RGB, GL_BYTE );
79 InsertES3FormatCombo(&set, GL_RGB565, GL_RGB, GL_UNSIGNED_SHORT_5_6_5 );
80 InsertES3FormatCombo(&set, GL_R11F_G11F_B10F, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV );
81 InsertES3FormatCombo(&set, GL_RGB9_E5, GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV );
82 InsertES3FormatCombo(&set, GL_RGB16F, GL_RGB, GL_HALF_FLOAT );
83 InsertES3FormatCombo(&set, GL_RGB16F, GL_RGB, GL_HALF_FLOAT_OES );
84 InsertES3FormatCombo(&set, GL_R11F_G11F_B10F, GL_RGB, GL_HALF_FLOAT );
85 InsertES3FormatCombo(&set, GL_R11F_G11F_B10F, GL_RGB, GL_HALF_FLOAT_OES );
86 InsertES3FormatCombo(&set, GL_RGB9_E5, GL_RGB, GL_HALF_FLOAT );
87 InsertES3FormatCombo(&set, GL_RGB9_E5, GL_RGB, GL_HALF_FLOAT_OES );
88 InsertES3FormatCombo(&set, GL_RGB32F, GL_RGB, GL_FLOAT );
89 InsertES3FormatCombo(&set, GL_RGB16F, GL_RGB, GL_FLOAT );
90 InsertES3FormatCombo(&set, GL_R11F_G11F_B10F, GL_RGB, GL_FLOAT );
91 InsertES3FormatCombo(&set, GL_RGB9_E5, GL_RGB, GL_FLOAT );
92 InsertES3FormatCombo(&set, GL_RGB8UI, GL_RGB_INTEGER, GL_UNSIGNED_BYTE );
93 InsertES3FormatCombo(&set, GL_RGB8I, GL_RGB_INTEGER, GL_BYTE );
94 InsertES3FormatCombo(&set, GL_RGB16UI, GL_RGB_INTEGER, GL_UNSIGNED_SHORT );
95 InsertES3FormatCombo(&set, GL_RGB16I, GL_RGB_INTEGER, GL_SHORT );
96 InsertES3FormatCombo(&set, GL_RGB32UI, GL_RGB_INTEGER, GL_UNSIGNED_INT );
97 InsertES3FormatCombo(&set, GL_RGB32I, GL_RGB_INTEGER, GL_INT );
98 InsertES3FormatCombo(&set, GL_RG8, GL_RG, GL_UNSIGNED_BYTE );
99 InsertES3FormatCombo(&set, GL_RG8_SNORM, GL_RG, GL_BYTE );
100 InsertES3FormatCombo(&set, GL_RG16F, GL_RG, GL_HALF_FLOAT );
101 InsertES3FormatCombo(&set, GL_RG16F, GL_RG, GL_HALF_FLOAT_OES );
102 InsertES3FormatCombo(&set, GL_RG32F, GL_RG, GL_FLOAT );
103 InsertES3FormatCombo(&set, GL_RG16F, GL_RG, GL_FLOAT );
104 InsertES3FormatCombo(&set, GL_RG8UI, GL_RG_INTEGER, GL_UNSIGNED_BYTE );
105 InsertES3FormatCombo(&set, GL_RG8I, GL_RG_INTEGER, GL_BYTE );
106 InsertES3FormatCombo(&set, GL_RG16UI, GL_RG_INTEGER, GL_UNSIGNED_SHORT );
107 InsertES3FormatCombo(&set, GL_RG16I, GL_RG_INTEGER, GL_SHORT );
108 InsertES3FormatCombo(&set, GL_RG32UI, GL_RG_INTEGER, GL_UNSIGNED_INT );
109 InsertES3FormatCombo(&set, GL_RG32I, GL_RG_INTEGER, GL_INT );
110 InsertES3FormatCombo(&set, GL_R8, GL_RED, GL_UNSIGNED_BYTE );
111 InsertES3FormatCombo(&set, GL_R8_SNORM, GL_RED, GL_BYTE );
112 InsertES3FormatCombo(&set, GL_R16F, GL_RED, GL_HALF_FLOAT );
113 InsertES3FormatCombo(&set, GL_R16F, GL_RED, GL_HALF_FLOAT_OES );
114 InsertES3FormatCombo(&set, GL_R32F, GL_RED, GL_FLOAT );
115 InsertES3FormatCombo(&set, GL_R16F, GL_RED, GL_FLOAT );
116 InsertES3FormatCombo(&set, GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE );
117 InsertES3FormatCombo(&set, GL_R8I, GL_RED_INTEGER, GL_BYTE );
118 InsertES3FormatCombo(&set, GL_R16UI, GL_RED_INTEGER, GL_UNSIGNED_SHORT );
119 InsertES3FormatCombo(&set, GL_R16I, GL_RED_INTEGER, GL_SHORT );
120 InsertES3FormatCombo(&set, GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT );
121 InsertES3FormatCombo(&set, GL_R32I, GL_RED_INTEGER, GL_INT );
122
123 // Unsized formats
124 InsertES3FormatCombo(&set, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE );
125 InsertES3FormatCombo(&set, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4 );
126 InsertES3FormatCombo(&set, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1 );
127 InsertES3FormatCombo(&set, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE );
128 InsertES3FormatCombo(&set, GL_RGB, GL_RGB, GL_UNSIGNED_SHORT_5_6_5 );
129 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE );
130 InsertES3FormatCombo(&set, GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE );
131 InsertES3FormatCombo(&set, GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE );
132 InsertES3FormatCombo(&set, GL_SRGB_ALPHA_EXT, GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE );
133 InsertES3FormatCombo(&set, GL_SRGB_EXT, GL_SRGB_EXT, GL_UNSIGNED_BYTE );
Jamie Madill689325c2015-07-20 14:36:53 -0400134 InsertES3FormatCombo(&set, GL_RG, GL_RG, GL_UNSIGNED_BYTE );
135 InsertES3FormatCombo(&set, GL_RG, GL_RG, GL_FLOAT );
136 InsertES3FormatCombo(&set, GL_RG, GL_RG, GL_HALF_FLOAT );
137 InsertES3FormatCombo(&set, GL_RG, GL_RG, GL_HALF_FLOAT_OES );
138 InsertES3FormatCombo(&set, GL_RED, GL_RED, GL_UNSIGNED_BYTE );
139 InsertES3FormatCombo(&set, GL_RED, GL_RED, GL_FLOAT );
140 InsertES3FormatCombo(&set, GL_RED, GL_RED, GL_HALF_FLOAT );
141 InsertES3FormatCombo(&set, GL_RED, GL_RED, GL_HALF_FLOAT_OES );
142 InsertES3FormatCombo(&set, GL_DEPTH_STENCIL, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8 );
Geoff Lang5d601382014-07-22 15:14:06 -0400143
144 // Depth stencil formats
145 InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT );
146 InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT );
147 InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT );
148 InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT );
149 InsertES3FormatCombo(&set, GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8 );
150 InsertES3FormatCombo(&set, GL_DEPTH32F_STENCIL8, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
151
152 // From GL_EXT_sRGB
153 InsertES3FormatCombo(&set, GL_SRGB8_ALPHA8_EXT, GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE );
154 InsertES3FormatCombo(&set, GL_SRGB8, GL_SRGB_EXT, GL_UNSIGNED_BYTE );
155
156 // From GL_OES_texture_float
157 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_FLOAT );
158 InsertES3FormatCombo(&set, GL_LUMINANCE, GL_LUMINANCE, GL_FLOAT );
159 InsertES3FormatCombo(&set, GL_ALPHA, GL_ALPHA, GL_FLOAT );
160
161 // From GL_OES_texture_half_float
162 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT );
163 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES );
164 InsertES3FormatCombo(&set, GL_LUMINANCE, GL_LUMINANCE, GL_HALF_FLOAT );
165 InsertES3FormatCombo(&set, GL_LUMINANCE, GL_LUMINANCE, GL_HALF_FLOAT_OES );
166 InsertES3FormatCombo(&set, GL_ALPHA, GL_ALPHA, GL_HALF_FLOAT );
167 InsertES3FormatCombo(&set, GL_ALPHA, GL_ALPHA, GL_HALF_FLOAT_OES );
168
169 // From GL_EXT_texture_format_BGRA8888
170 InsertES3FormatCombo(&set, GL_BGRA_EXT, GL_BGRA_EXT, GL_UNSIGNED_BYTE );
171
172 // From GL_EXT_texture_storage
173 // | Internal format | Format | Type |
174 // | | | |
175 InsertES3FormatCombo(&set, GL_ALPHA8_EXT, GL_ALPHA, GL_UNSIGNED_BYTE );
176 InsertES3FormatCombo(&set, GL_LUMINANCE8_EXT, GL_LUMINANCE, GL_UNSIGNED_BYTE );
177 InsertES3FormatCombo(&set, GL_LUMINANCE8_ALPHA8_EXT, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE );
178 InsertES3FormatCombo(&set, GL_ALPHA32F_EXT, GL_ALPHA, GL_FLOAT );
179 InsertES3FormatCombo(&set, GL_LUMINANCE32F_EXT, GL_LUMINANCE, GL_FLOAT );
180 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA32F_EXT, GL_LUMINANCE_ALPHA, GL_FLOAT );
181 InsertES3FormatCombo(&set, GL_ALPHA16F_EXT, GL_ALPHA, GL_HALF_FLOAT );
182 InsertES3FormatCombo(&set, GL_ALPHA16F_EXT, GL_ALPHA, GL_HALF_FLOAT_OES );
183 InsertES3FormatCombo(&set, GL_LUMINANCE16F_EXT, GL_LUMINANCE, GL_HALF_FLOAT );
184 InsertES3FormatCombo(&set, GL_LUMINANCE16F_EXT, GL_LUMINANCE, GL_HALF_FLOAT_OES );
185 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA16F_EXT, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT );
186 InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA16F_EXT, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES );
187
188 // From GL_EXT_texture_storage and GL_EXT_texture_format_BGRA8888
189 InsertES3FormatCombo(&set, GL_BGRA8_EXT, GL_BGRA_EXT, GL_UNSIGNED_BYTE );
190 InsertES3FormatCombo(&set, GL_BGRA4_ANGLEX, GL_BGRA_EXT, GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT);
191 InsertES3FormatCombo(&set, GL_BGRA4_ANGLEX, GL_BGRA_EXT, GL_UNSIGNED_BYTE );
192 InsertES3FormatCombo(&set, GL_BGR5_A1_ANGLEX, GL_BGRA_EXT, GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT);
193 InsertES3FormatCombo(&set, GL_BGR5_A1_ANGLEX, GL_BGRA_EXT, GL_UNSIGNED_BYTE );
194
195 // From GL_ANGLE_depth_texture
196 InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT32_OES, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT_24_8_OES );
197
198 // Compressed formats
199 // From ES 3.0.1 spec, table 3.16
200 // | Internal format | Format | Type |
201 // | | | |
202 InsertES3FormatCombo(&set, GL_COMPRESSED_R11_EAC, GL_COMPRESSED_R11_EAC, GL_UNSIGNED_BYTE);
203 InsertES3FormatCombo(&set, GL_COMPRESSED_R11_EAC, GL_COMPRESSED_R11_EAC, GL_UNSIGNED_BYTE);
204 InsertES3FormatCombo(&set, GL_COMPRESSED_SIGNED_R11_EAC, GL_COMPRESSED_SIGNED_R11_EAC, GL_UNSIGNED_BYTE);
205 InsertES3FormatCombo(&set, GL_COMPRESSED_RG11_EAC, GL_COMPRESSED_RG11_EAC, GL_UNSIGNED_BYTE);
206 InsertES3FormatCombo(&set, GL_COMPRESSED_SIGNED_RG11_EAC, GL_COMPRESSED_SIGNED_RG11_EAC, GL_UNSIGNED_BYTE);
207 InsertES3FormatCombo(&set, GL_COMPRESSED_RGB8_ETC2, GL_COMPRESSED_RGB8_ETC2, GL_UNSIGNED_BYTE);
208 InsertES3FormatCombo(&set, GL_COMPRESSED_SRGB8_ETC2, GL_COMPRESSED_SRGB8_ETC2, GL_UNSIGNED_BYTE);
209 InsertES3FormatCombo(&set, GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, GL_UNSIGNED_BYTE);
210 InsertES3FormatCombo(&set, GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, GL_UNSIGNED_BYTE);
211 InsertES3FormatCombo(&set, GL_COMPRESSED_RGBA8_ETC2_EAC, GL_COMPRESSED_RGBA8_ETC2_EAC, GL_UNSIGNED_BYTE);
212 InsertES3FormatCombo(&set, GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC, GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC, GL_UNSIGNED_BYTE);
213
214
215 // From GL_EXT_texture_compression_dxt1
216 InsertES3FormatCombo(&set, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, GL_UNSIGNED_BYTE);
217 InsertES3FormatCombo(&set, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_UNSIGNED_BYTE);
218
219 // From GL_ANGLE_texture_compression_dxt3
220 InsertES3FormatCombo(&set, GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE, GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE, GL_UNSIGNED_BYTE);
221
222 // From GL_ANGLE_texture_compression_dxt5
223 InsertES3FormatCombo(&set, GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE, GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE, GL_UNSIGNED_BYTE);
224
225 return set;
226}
227
228static bool ValidateTexImageFormatCombination(gl::Context *context, GLenum internalFormat, GLenum format, GLenum type)
229{
230 // Note: dEQP 2013.4 expects an INVALID_VALUE error for TexImage3D with an invalid
231 // internal format. (dEQP-GLES3.functional.negative_api.texture.teximage3d)
Geoff Langbaadf232014-08-04 13:58:02 -0400232 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalFormat);
Geoff Lang5d601382014-07-22 15:14:06 -0400233 if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions()))
234 {
Geoff Langb1196682014-07-23 13:47:29 -0400235 context->recordError(Error(GL_INVALID_ENUM));
236 return false;
Geoff Lang5d601382014-07-22 15:14:06 -0400237 }
238
239 // The type and format are valid if any supported internal format has that type and format
240 bool formatSupported = false;
241 bool typeSupported = false;
242
243 static const ES3FormatCombinationSet es3FormatSet = BuildES3FormatSet();
244 for (ES3FormatCombinationSet::const_iterator i = es3FormatSet.begin(); i != es3FormatSet.end(); i++)
245 {
246 if (i->format == format || i->type == type)
247 {
248 const gl::InternalFormat &info = gl::GetInternalFormatInfo(i->internalFormat);
249 bool supported = info.textureSupport(context->getClientVersion(), context->getExtensions());
Geoff Langbaadf232014-08-04 13:58:02 -0400250 if (supported && i->type == type)
Geoff Lang5d601382014-07-22 15:14:06 -0400251 {
252 typeSupported = true;
253 }
Geoff Langbaadf232014-08-04 13:58:02 -0400254 if (supported && i->format == format)
Geoff Lang5d601382014-07-22 15:14:06 -0400255 {
256 formatSupported = true;
257 }
Geoff Langbaadf232014-08-04 13:58:02 -0400258
259 // Early-out if both type and format are supported now
260 if (typeSupported && formatSupported)
261 {
262 break;
263 }
Geoff Lang5d601382014-07-22 15:14:06 -0400264 }
265 }
266
267 if (!typeSupported || !formatSupported)
268 {
Geoff Langb1196682014-07-23 13:47:29 -0400269 context->recordError(Error(GL_INVALID_ENUM));
270 return false;
Geoff Lang5d601382014-07-22 15:14:06 -0400271 }
272
273 // Check if this is a valid format combination to load texture data
274 ES3FormatCombination searchFormat;
275 searchFormat.internalFormat = internalFormat;
276 searchFormat.format = format;
277 searchFormat.type = type;
278
279 if (es3FormatSet.find(searchFormat) == es3FormatSet.end())
280 {
Geoff Langb1196682014-07-23 13:47:29 -0400281 context->recordError(Error(GL_INVALID_OPERATION));
282 return false;
Geoff Lang5d601382014-07-22 15:14:06 -0400283 }
284
285 return true;
286}
287
Geoff Langb1196682014-07-23 13:47:29 -0400288bool ValidateES3TexImageParameters(Context *context, GLenum target, GLint level, GLenum internalformat, bool isCompressed, bool isSubImage,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400289 GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth,
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400290 GLint border, GLenum format, GLenum type, const GLvoid *pixels)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400291{
Jamie Madill6f38f822014-06-06 17:12:20 -0400292 if (!ValidTexture2DDestinationTarget(context, target))
293 {
Geoff Langb1196682014-07-23 13:47:29 -0400294 context->recordError(Error(GL_INVALID_ENUM));
295 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -0400296 }
297
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400298 // Validate image size
Geoff Langce635692013-09-24 13:56:32 -0400299 if (!ValidImageSize(context, target, level, width, height, depth))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400300 {
Geoff Langb1196682014-07-23 13:47:29 -0400301 context->recordError(Error(GL_INVALID_VALUE));
302 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400303 }
304
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400305 // Verify zero border
306 if (border != 0)
307 {
Geoff Langb1196682014-07-23 13:47:29 -0400308 context->recordError(Error(GL_INVALID_VALUE));
309 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400310 }
311
Jamie Madill6f38f822014-06-06 17:12:20 -0400312 if (xoffset < 0 || yoffset < 0 || zoffset < 0 ||
313 std::numeric_limits<GLsizei>::max() - xoffset < width ||
314 std::numeric_limits<GLsizei>::max() - yoffset < height ||
315 std::numeric_limits<GLsizei>::max() - zoffset < depth)
316 {
Geoff Langb1196682014-07-23 13:47:29 -0400317 context->recordError(Error(GL_INVALID_VALUE));
318 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -0400319 }
320
Geoff Langaae65a42014-05-26 12:43:44 -0400321 const gl::Caps &caps = context->getCaps();
322
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400323 switch (target)
324 {
325 case GL_TEXTURE_2D:
Geoff Langa9be0dc2014-12-17 12:34:40 -0500326 if (static_cast<GLuint>(width) > (caps.max2DTextureSize >> level) ||
327 static_cast<GLuint>(height) > (caps.max2DTextureSize >> level))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400328 {
Geoff Langa9be0dc2014-12-17 12:34:40 -0500329 context->recordError(Error(GL_INVALID_VALUE));
330 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400331 }
332 break;
333
334 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
335 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
336 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
337 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
338 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
339 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
Geoff Langa9be0dc2014-12-17 12:34:40 -0500340 if (!isSubImage && width != height)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400341 {
Geoff Langa9be0dc2014-12-17 12:34:40 -0500342 context->recordError(Error(GL_INVALID_VALUE));
343 return false;
344 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400345
Geoff Langa9be0dc2014-12-17 12:34:40 -0500346 if (static_cast<GLuint>(width) > (caps.maxCubeMapTextureSize >> level))
347 {
348 context->recordError(Error(GL_INVALID_VALUE));
349 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400350 }
351 break;
352
353 case GL_TEXTURE_3D:
Geoff Langa9be0dc2014-12-17 12:34:40 -0500354 if (static_cast<GLuint>(width) > (caps.max3DTextureSize >> level) ||
355 static_cast<GLuint>(height) > (caps.max3DTextureSize >> level) ||
356 static_cast<GLuint>(depth) > (caps.max3DTextureSize >> level))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400357 {
Geoff Langa9be0dc2014-12-17 12:34:40 -0500358 context->recordError(Error(GL_INVALID_VALUE));
359 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400360 }
361 break;
362
Geoff Langa9be0dc2014-12-17 12:34:40 -0500363 case GL_TEXTURE_2D_ARRAY:
364 if (static_cast<GLuint>(width) > (caps.max2DTextureSize >> level) ||
365 static_cast<GLuint>(height) > (caps.max2DTextureSize >> level) ||
Geoff Langb92c1332015-09-04 12:54:55 -0400366 static_cast<GLuint>(depth) > caps.maxArrayTextureLayers)
Geoff Langa9be0dc2014-12-17 12:34:40 -0500367 {
368 context->recordError(Error(GL_INVALID_VALUE));
369 return false;
370 }
371 break;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400372
373 default:
Geoff Langb1196682014-07-23 13:47:29 -0400374 context->recordError(Error(GL_INVALID_ENUM));
375 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400376 }
377
Geoff Lang691e58c2014-12-19 17:03:25 -0500378 gl::Texture *texture = context->getTargetTexture(IsCubeMapTextureTarget(target) ? GL_TEXTURE_CUBE_MAP : target);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400379 if (!texture)
380 {
Geoff Langb1196682014-07-23 13:47:29 -0400381 context->recordError(Error(GL_INVALID_OPERATION));
382 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400383 }
384
385 if (texture->isImmutable() && !isSubImage)
386 {
Geoff Langb1196682014-07-23 13:47:29 -0400387 context->recordError(Error(GL_INVALID_OPERATION));
388 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400389 }
390
391 // Validate texture formats
Geoff Langa9be0dc2014-12-17 12:34:40 -0500392 GLenum actualInternalFormat = isSubImage ? texture->getInternalFormat(target, level) : internalformat;
Geoff Lang5d601382014-07-22 15:14:06 -0400393 const gl::InternalFormat &actualFormatInfo = gl::GetInternalFormatInfo(actualInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400394 if (isCompressed)
395 {
Geoff Langd4f180b2013-09-24 13:57:44 -0400396 if (!ValidCompressedImageSize(context, actualInternalFormat, width, height))
397 {
Geoff Langb1196682014-07-23 13:47:29 -0400398 context->recordError(Error(GL_INVALID_OPERATION));
399 return false;
Geoff Langd4f180b2013-09-24 13:57:44 -0400400 }
401
Geoff Lang5d601382014-07-22 15:14:06 -0400402 if (!actualFormatInfo.compressed)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400403 {
Geoff Langb1196682014-07-23 13:47:29 -0400404 context->recordError(Error(GL_INVALID_ENUM));
405 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400406 }
407
408 if (target == GL_TEXTURE_3D)
409 {
Geoff Langb1196682014-07-23 13:47:29 -0400410 context->recordError(Error(GL_INVALID_OPERATION));
411 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400412 }
413 }
414 else
415 {
Geoff Langbaadf232014-08-04 13:58:02 -0400416 if (!ValidateTexImageFormatCombination(context, actualInternalFormat, format, type))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400417 {
Geoff Lang5d601382014-07-22 15:14:06 -0400418 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400419 }
420
421 if (target == GL_TEXTURE_3D && (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL))
422 {
Geoff Langb1196682014-07-23 13:47:29 -0400423 context->recordError(Error(GL_INVALID_OPERATION));
424 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400425 }
426 }
427
428 // Validate sub image parameters
429 if (isSubImage)
430 {
Geoff Langa9be0dc2014-12-17 12:34:40 -0500431 if (isCompressed != actualFormatInfo.compressed)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400432 {
Geoff Langb1196682014-07-23 13:47:29 -0400433 context->recordError(Error(GL_INVALID_OPERATION));
434 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400435 }
436
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400437 if (width == 0 || height == 0 || depth == 0)
438 {
439 return false;
440 }
441
442 if (xoffset < 0 || yoffset < 0 || zoffset < 0)
443 {
Geoff Langb1196682014-07-23 13:47:29 -0400444 context->recordError(Error(GL_INVALID_VALUE));
445 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400446 }
447
448 if (std::numeric_limits<GLsizei>::max() - xoffset < width ||
449 std::numeric_limits<GLsizei>::max() - yoffset < height ||
450 std::numeric_limits<GLsizei>::max() - zoffset < depth)
451 {
Geoff Langb1196682014-07-23 13:47:29 -0400452 context->recordError(Error(GL_INVALID_VALUE));
453 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400454 }
455
Geoff Langa9be0dc2014-12-17 12:34:40 -0500456 if (static_cast<size_t>(xoffset + width) > texture->getWidth(target, level) ||
457 static_cast<size_t>(yoffset + height) > texture->getHeight(target, level) ||
458 static_cast<size_t>(zoffset + depth) > texture->getDepth(target, level))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400459 {
Geoff Langb1196682014-07-23 13:47:29 -0400460 context->recordError(Error(GL_INVALID_VALUE));
461 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400462 }
463 }
464
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400465 // Check for pixel unpack buffer related API errors
Shannon Woods53a94a82014-06-24 15:20:36 -0400466 gl::Buffer *pixelUnpackBuffer = context->getState().getTargetBuffer(GL_PIXEL_UNPACK_BUFFER);
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400467 if (pixelUnpackBuffer != NULL)
468 {
469 // ...the data would be unpacked from the buffer object such that the memory reads required
470 // would exceed the data store size.
471 size_t widthSize = static_cast<size_t>(width);
472 size_t heightSize = static_cast<size_t>(height);
473 size_t depthSize = static_cast<size_t>(depth);
Geoff Lang5d601382014-07-22 15:14:06 -0400474 GLenum sizedFormat = GetSizedInternalFormat(actualInternalFormat, type);
Jamie Madill6f38f822014-06-06 17:12:20 -0400475
Geoff Lang5d601382014-07-22 15:14:06 -0400476 size_t pixelBytes = static_cast<size_t>(gl::GetInternalFormatInfo(sizedFormat).pixelBytes);
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400477
478 if (!rx::IsUnsignedMultiplicationSafe(widthSize, heightSize) ||
479 !rx::IsUnsignedMultiplicationSafe(widthSize * heightSize, depthSize) ||
480 !rx::IsUnsignedMultiplicationSafe(widthSize * heightSize * depthSize, pixelBytes))
481 {
482 // Overflow past the end of the buffer
Geoff Langb1196682014-07-23 13:47:29 -0400483 context->recordError(Error(GL_INVALID_OPERATION));
484 return false;
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400485 }
486
Jamie Madillc751d1e2014-10-21 17:46:29 -0400487 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(sizedFormat);
488 size_t copyBytes = formatInfo.computeBlockSize(type, width, height);
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400489 size_t offset = reinterpret_cast<size_t>(pixels);
490
Jamie Madill6f38f822014-06-06 17:12:20 -0400491 if (!rx::IsUnsignedAdditionSafe(offset, copyBytes) ||
Brandon Jonesd38f9262014-06-18 16:26:45 -0700492 ((offset + copyBytes) > static_cast<size_t>(pixelUnpackBuffer->getSize())))
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400493 {
494 // Overflow past the end of the buffer
Geoff Langb1196682014-07-23 13:47:29 -0400495 context->recordError(Error(GL_INVALID_OPERATION));
496 return false;
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400497 }
498
499 // ...data is not evenly divisible into the number of bytes needed to store in memory a datum
500 // indicated by type.
Jamie Madillc751d1e2014-10-21 17:46:29 -0400501 if (!isCompressed)
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400502 {
Jamie Madillc751d1e2014-10-21 17:46:29 -0400503 size_t dataBytesPerPixel = static_cast<size_t>(gl::GetTypeInfo(type).bytes);
504
505 if ((offset % dataBytesPerPixel) != 0)
506 {
507 context->recordError(Error(GL_INVALID_OPERATION));
508 return false;
509 }
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400510 }
511
Jamie Madill7a5f7382014-03-05 15:01:24 -0500512 // ...the buffer object's data store is currently mapped.
Brandon Jonesd38f9262014-06-18 16:26:45 -0700513 if (pixelUnpackBuffer->isMapped())
Jamie Madill7a5f7382014-03-05 15:01:24 -0500514 {
Geoff Langb1196682014-07-23 13:47:29 -0400515 context->recordError(Error(GL_INVALID_OPERATION));
516 return false;
Jamie Madill7a5f7382014-03-05 15:01:24 -0500517 }
Jamie Madillefb2a6f2013-09-24 10:22:42 -0400518 }
519
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400520 return true;
521}
522
Geoff Lang5d601382014-07-22 15:14:06 -0400523struct EffectiveInternalFormatInfo
524{
525 GLenum mEffectiveFormat;
526 GLenum mDestFormat;
527 GLuint mMinRedBits;
528 GLuint mMaxRedBits;
529 GLuint mMinGreenBits;
530 GLuint mMaxGreenBits;
531 GLuint mMinBlueBits;
532 GLuint mMaxBlueBits;
533 GLuint mMinAlphaBits;
534 GLuint mMaxAlphaBits;
535
536 EffectiveInternalFormatInfo(GLenum effectiveFormat, GLenum destFormat, GLuint minRedBits, GLuint maxRedBits,
537 GLuint minGreenBits, GLuint maxGreenBits, GLuint minBlueBits, GLuint maxBlueBits,
538 GLuint minAlphaBits, GLuint maxAlphaBits)
539 : mEffectiveFormat(effectiveFormat), mDestFormat(destFormat), mMinRedBits(minRedBits),
540 mMaxRedBits(maxRedBits), mMinGreenBits(minGreenBits), mMaxGreenBits(maxGreenBits),
541 mMinBlueBits(minBlueBits), mMaxBlueBits(maxBlueBits), mMinAlphaBits(minAlphaBits),
542 mMaxAlphaBits(maxAlphaBits) {};
543};
544
545typedef std::vector<EffectiveInternalFormatInfo> EffectiveInternalFormatList;
546
547static EffectiveInternalFormatList BuildSizedEffectiveInternalFormatList()
548{
549 EffectiveInternalFormatList list;
550
551 // OpenGL ES 3.0.3 Specification, Table 3.17, pg 141: Effective internal format coresponding to destination internal format and
552 // linear source buffer component sizes.
553 // | Source channel min/max sizes |
554 // Effective Internal Format | N/A | R | G | B | A |
555 list.push_back(EffectiveInternalFormatInfo(GL_ALPHA8_EXT, GL_NONE, 0, 0, 0, 0, 0, 0, 1, 8));
556 list.push_back(EffectiveInternalFormatInfo(GL_R8, GL_NONE, 1, 8, 0, 0, 0, 0, 0, 0));
557 list.push_back(EffectiveInternalFormatInfo(GL_RG8, GL_NONE, 1, 8, 1, 8, 0, 0, 0, 0));
558 list.push_back(EffectiveInternalFormatInfo(GL_RGB565, GL_NONE, 1, 5, 1, 6, 1, 5, 0, 0));
559 list.push_back(EffectiveInternalFormatInfo(GL_RGB8, GL_NONE, 6, 8, 7, 8, 6, 8, 0, 0));
560 list.push_back(EffectiveInternalFormatInfo(GL_RGBA4, GL_NONE, 1, 4, 1, 4, 1, 4, 1, 4));
561 list.push_back(EffectiveInternalFormatInfo(GL_RGB5_A1, GL_NONE, 5, 5, 5, 5, 5, 5, 1, 1));
562 list.push_back(EffectiveInternalFormatInfo(GL_RGBA8, GL_NONE, 5, 8, 5, 8, 5, 8, 2, 8));
563 list.push_back(EffectiveInternalFormatInfo(GL_RGB10_A2, GL_NONE, 9, 10, 9, 10, 9, 10, 2, 2));
564
565 return list;
566}
567
568static EffectiveInternalFormatList BuildUnsizedEffectiveInternalFormatList()
569{
570 EffectiveInternalFormatList list;
571
572 // OpenGL ES 3.0.3 Specification, Table 3.17, pg 141: Effective internal format coresponding to destination internal format and
573 // linear source buffer component sizes.
574 // | Source channel min/max sizes |
575 // Effective Internal Format | Dest Format | R | G | B | A |
576 list.push_back(EffectiveInternalFormatInfo(GL_ALPHA8_EXT, GL_ALPHA, 0, UINT_MAX, 0, UINT_MAX, 0, UINT_MAX, 1, 8));
577 list.push_back(EffectiveInternalFormatInfo(GL_LUMINANCE8_EXT, GL_LUMINANCE, 1, 8, 0, UINT_MAX, 0, UINT_MAX, 0, UINT_MAX));
578 list.push_back(EffectiveInternalFormatInfo(GL_LUMINANCE8_ALPHA8_EXT, GL_LUMINANCE_ALPHA, 1, 8, 0, UINT_MAX, 0, UINT_MAX, 1, 8));
579 list.push_back(EffectiveInternalFormatInfo(GL_RGB565, GL_RGB, 1, 5, 1, 6, 1, 5, 0, UINT_MAX));
580 list.push_back(EffectiveInternalFormatInfo(GL_RGB8, GL_RGB, 6, 8, 7, 8, 6, 8, 0, UINT_MAX));
581 list.push_back(EffectiveInternalFormatInfo(GL_RGBA4, GL_RGBA, 1, 4, 1, 4, 1, 4, 1, 4));
582 list.push_back(EffectiveInternalFormatInfo(GL_RGB5_A1, GL_RGBA, 5, 5, 5, 5, 5, 5, 1, 1));
583 list.push_back(EffectiveInternalFormatInfo(GL_RGBA8, GL_RGBA, 5, 8, 5, 8, 5, 8, 5, 8));
584
585 return list;
586}
587
588static bool GetEffectiveInternalFormat(const InternalFormat &srcFormat, const InternalFormat &destFormat,
589 GLenum *outEffectiveFormat)
590{
591 const EffectiveInternalFormatList *list = NULL;
592 GLenum targetFormat = GL_NONE;
593
594 if (destFormat.pixelBytes > 0)
595 {
596 static const EffectiveInternalFormatList sizedList = BuildSizedEffectiveInternalFormatList();
597 list = &sizedList;
598 }
599 else
600 {
601 static const EffectiveInternalFormatList unsizedList = BuildUnsizedEffectiveInternalFormatList();
602 list = &unsizedList;
603 targetFormat = destFormat.format;
604 }
605
606 for (size_t curFormat = 0; curFormat < list->size(); ++curFormat)
607 {
608 const EffectiveInternalFormatInfo& formatInfo = list->at(curFormat);
609 if ((formatInfo.mDestFormat == targetFormat) &&
610 (formatInfo.mMinRedBits <= srcFormat.redBits && formatInfo.mMaxRedBits >= srcFormat.redBits) &&
611 (formatInfo.mMinGreenBits <= srcFormat.greenBits && formatInfo.mMaxGreenBits >= srcFormat.greenBits) &&
612 (formatInfo.mMinBlueBits <= srcFormat.blueBits && formatInfo.mMaxBlueBits >= srcFormat.blueBits) &&
613 (formatInfo.mMinAlphaBits <= srcFormat.alphaBits && formatInfo.mMaxAlphaBits >= srcFormat.alphaBits))
614 {
615 *outEffectiveFormat = formatInfo.mEffectiveFormat;
616 return true;
617 }
618 }
619
620 return false;
621}
622
623struct CopyConversion
624{
625 GLenum mTextureFormat;
626 GLenum mFramebufferFormat;
627
628 CopyConversion(GLenum textureFormat, GLenum framebufferFormat)
629 : mTextureFormat(textureFormat), mFramebufferFormat(framebufferFormat) { }
630
631 bool operator<(const CopyConversion& other) const
632 {
633 return memcmp(this, &other, sizeof(CopyConversion)) < 0;
634 }
635};
636
637typedef std::set<CopyConversion> CopyConversionSet;
638
639static CopyConversionSet BuildValidES3CopyTexImageCombinations()
640{
641 CopyConversionSet set;
642
643 // From ES 3.0.1 spec, table 3.15
644 set.insert(CopyConversion(GL_ALPHA, GL_RGBA));
645 set.insert(CopyConversion(GL_LUMINANCE, GL_RED));
646 set.insert(CopyConversion(GL_LUMINANCE, GL_RG));
647 set.insert(CopyConversion(GL_LUMINANCE, GL_RGB));
648 set.insert(CopyConversion(GL_LUMINANCE, GL_RGBA));
649 set.insert(CopyConversion(GL_LUMINANCE_ALPHA, GL_RGBA));
650 set.insert(CopyConversion(GL_RED, GL_RED));
651 set.insert(CopyConversion(GL_RED, GL_RG));
652 set.insert(CopyConversion(GL_RED, GL_RGB));
653 set.insert(CopyConversion(GL_RED, GL_RGBA));
654 set.insert(CopyConversion(GL_RG, GL_RG));
655 set.insert(CopyConversion(GL_RG, GL_RGB));
656 set.insert(CopyConversion(GL_RG, GL_RGBA));
657 set.insert(CopyConversion(GL_RGB, GL_RGB));
658 set.insert(CopyConversion(GL_RGB, GL_RGBA));
659 set.insert(CopyConversion(GL_RGBA, GL_RGBA));
660
661 // Necessary for ANGLE back-buffers
662 set.insert(CopyConversion(GL_ALPHA, GL_BGRA_EXT));
663 set.insert(CopyConversion(GL_LUMINANCE, GL_BGRA_EXT));
664 set.insert(CopyConversion(GL_LUMINANCE_ALPHA, GL_BGRA_EXT));
665 set.insert(CopyConversion(GL_RED, GL_BGRA_EXT));
666 set.insert(CopyConversion(GL_RG, GL_BGRA_EXT));
667 set.insert(CopyConversion(GL_RGB, GL_BGRA_EXT));
668 set.insert(CopyConversion(GL_RGBA, GL_BGRA_EXT));
669
670 set.insert(CopyConversion(GL_RED_INTEGER, GL_RED_INTEGER));
671 set.insert(CopyConversion(GL_RED_INTEGER, GL_RG_INTEGER));
672 set.insert(CopyConversion(GL_RED_INTEGER, GL_RGB_INTEGER));
673 set.insert(CopyConversion(GL_RED_INTEGER, GL_RGBA_INTEGER));
674 set.insert(CopyConversion(GL_RG_INTEGER, GL_RG_INTEGER));
675 set.insert(CopyConversion(GL_RG_INTEGER, GL_RGB_INTEGER));
676 set.insert(CopyConversion(GL_RG_INTEGER, GL_RGBA_INTEGER));
677 set.insert(CopyConversion(GL_RGB_INTEGER, GL_RGB_INTEGER));
678 set.insert(CopyConversion(GL_RGB_INTEGER, GL_RGBA_INTEGER));
679 set.insert(CopyConversion(GL_RGBA_INTEGER, GL_RGBA_INTEGER));
680
681 return set;
682}
683
684static bool IsValidES3CopyTexImageCombination(GLenum textureInternalFormat, GLenum frameBufferInternalFormat, GLuint readBufferHandle)
685{
686 const InternalFormat &textureInternalFormatInfo = GetInternalFormatInfo(textureInternalFormat);
687 const InternalFormat &framebufferInternalFormatInfo = GetInternalFormatInfo(frameBufferInternalFormat);
688
689 static const CopyConversionSet conversionSet = BuildValidES3CopyTexImageCombinations();
690 if (conversionSet.find(CopyConversion(textureInternalFormatInfo.format, framebufferInternalFormatInfo.format)) != conversionSet.end())
691 {
692 // Section 3.8.5 of the GLES 3.0.3 spec states that source and destination formats
693 // must both be signed, unsigned, or fixed point and both source and destinations
694 // must be either both SRGB or both not SRGB. EXT_color_buffer_float adds allowed
695 // conversion between fixed and floating point.
696
697 if ((textureInternalFormatInfo.colorEncoding == GL_SRGB) != (framebufferInternalFormatInfo.colorEncoding == GL_SRGB))
698 {
699 return false;
700 }
701
702 if (((textureInternalFormatInfo.componentType == GL_INT) != (framebufferInternalFormatInfo.componentType == GL_INT )) ||
703 ((textureInternalFormatInfo.componentType == GL_UNSIGNED_INT) != (framebufferInternalFormatInfo.componentType == GL_UNSIGNED_INT)))
704 {
705 return false;
706 }
707
708 if ((textureInternalFormatInfo.componentType == GL_UNSIGNED_NORMALIZED ||
709 textureInternalFormatInfo.componentType == GL_SIGNED_NORMALIZED ||
710 textureInternalFormatInfo.componentType == GL_FLOAT) &&
711 !(framebufferInternalFormatInfo.componentType == GL_UNSIGNED_NORMALIZED ||
712 framebufferInternalFormatInfo.componentType == GL_SIGNED_NORMALIZED ||
713 framebufferInternalFormatInfo.componentType == GL_FLOAT))
714 {
715 return false;
716 }
717
718 // GLES specification 3.0.3, sec 3.8.5, pg 139-140:
719 // The effective internal format of the source buffer is determined with the following rules applied in order:
720 // * If the source buffer is a texture or renderbuffer that was created with a sized internal format then the
721 // effective internal format is the source buffer's sized internal format.
722 // * If the source buffer is a texture that was created with an unsized base internal format, then the
723 // effective internal format is the source image array's effective internal format, as specified by table
724 // 3.12, which is determined from the <format> and <type> that were used when the source image array was
725 // specified by TexImage*.
726 // * Otherwise the effective internal format is determined by the row in table 3.17 or 3.18 where
727 // Destination Internal Format matches internalformat and where the [source channel sizes] are consistent
728 // with the values of the source buffer's [channel sizes]. Table 3.17 is used if the
729 // FRAMEBUFFER_ATTACHMENT_ENCODING is LINEAR and table 3.18 is used if the FRAMEBUFFER_ATTACHMENT_ENCODING
730 // is SRGB.
731 const InternalFormat *sourceEffectiveFormat = NULL;
732 if (readBufferHandle != 0)
733 {
734 // Not the default framebuffer, therefore the read buffer must be a user-created texture or renderbuffer
735 if (framebufferInternalFormatInfo.pixelBytes > 0)
736 {
737 sourceEffectiveFormat = &framebufferInternalFormatInfo;
738 }
739 else
740 {
741 // Renderbuffers cannot be created with an unsized internal format, so this must be an unsized-format
742 // texture. We can use the same table we use when creating textures to get its effective sized format.
Geoff Lang051dbc72015-01-05 15:48:58 -0500743 GLenum sizedInternalFormat = GetSizedInternalFormat(framebufferInternalFormatInfo.format, framebufferInternalFormatInfo.type);
744 sourceEffectiveFormat = &GetInternalFormatInfo(sizedInternalFormat);
Geoff Lang5d601382014-07-22 15:14:06 -0400745 }
746 }
747 else
748 {
749 // The effective internal format must be derived from the source framebuffer's channel sizes.
750 // This is done in GetEffectiveInternalFormat for linear buffers (table 3.17)
751 if (framebufferInternalFormatInfo.colorEncoding == GL_LINEAR)
752 {
753 GLenum effectiveFormat;
754 if (GetEffectiveInternalFormat(framebufferInternalFormatInfo, textureInternalFormatInfo, &effectiveFormat))
755 {
756 sourceEffectiveFormat = &GetInternalFormatInfo(effectiveFormat);
757 }
758 else
759 {
760 return false;
761 }
762 }
763 else if (framebufferInternalFormatInfo.colorEncoding == GL_SRGB)
764 {
765 // SRGB buffers can only be copied to sized format destinations according to table 3.18
766 if ((textureInternalFormatInfo.pixelBytes > 0) &&
767 (framebufferInternalFormatInfo.redBits >= 1 && framebufferInternalFormatInfo.redBits <= 8) &&
768 (framebufferInternalFormatInfo.greenBits >= 1 && framebufferInternalFormatInfo.greenBits <= 8) &&
769 (framebufferInternalFormatInfo.blueBits >= 1 && framebufferInternalFormatInfo.blueBits <= 8) &&
770 (framebufferInternalFormatInfo.alphaBits >= 1 && framebufferInternalFormatInfo.alphaBits <= 8))
771 {
772 sourceEffectiveFormat = &GetInternalFormatInfo(GL_SRGB8_ALPHA8);
773 }
774 else
775 {
776 return false;
777 }
778 }
779 else
780 {
781 UNREACHABLE();
782 return false;
783 }
784 }
785
786 if (textureInternalFormatInfo.pixelBytes > 0)
787 {
788 // Section 3.8.5 of the GLES 3.0.3 spec, pg 139, requires that, if the destination format is sized,
789 // component sizes of the source and destination formats must exactly match
790 if (textureInternalFormatInfo.redBits != sourceEffectiveFormat->redBits ||
791 textureInternalFormatInfo.greenBits != sourceEffectiveFormat->greenBits ||
792 textureInternalFormatInfo.blueBits != sourceEffectiveFormat->blueBits ||
793 textureInternalFormatInfo.alphaBits != sourceEffectiveFormat->alphaBits)
794 {
795 return false;
796 }
797 }
798
799
800 return true; // A conversion function exists, and no rule in the specification has precluded conversion
801 // between these formats.
802 }
803
804 return false;
805}
806
Geoff Langb1196682014-07-23 13:47:29 -0400807bool ValidateES3CopyTexImageParameters(Context *context, GLenum target, GLint level, GLenum internalformat,
Jamie Madill6f38f822014-06-06 17:12:20 -0400808 bool isSubImage, GLint xoffset, GLint yoffset, GLint zoffset,
809 GLint x, GLint y, GLsizei width, GLsizei height, GLint border)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400810{
Jamie Madill560a8d82014-05-21 13:06:20 -0400811 GLenum textureInternalFormat;
812 if (!ValidateCopyTexImageParametersBase(context, target, level, internalformat, isSubImage,
Jamie Madill6f38f822014-06-06 17:12:20 -0400813 xoffset, yoffset, zoffset, x, y, width, height,
814 border, &textureInternalFormat))
Shannon Woods4dfed832014-03-17 20:03:39 -0400815 {
Jamie Madill560a8d82014-05-21 13:06:20 -0400816 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400817 }
818
Shannon Woods53a94a82014-06-24 15:20:36 -0400819 gl::Framebuffer *framebuffer = context->getState().getReadFramebuffer();
Jamie Madill3c7fa222014-06-05 13:08:51 -0400820
Geoff Lang748f74e2014-12-01 11:25:34 -0500821 if (framebuffer->checkStatus(context->getData()) != GL_FRAMEBUFFER_COMPLETE)
Jamie Madill3c7fa222014-06-05 13:08:51 -0400822 {
Geoff Langb1196682014-07-23 13:47:29 -0400823 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
824 return false;
Jamie Madill3c7fa222014-06-05 13:08:51 -0400825 }
826
Jamie Madill48faf802014-11-06 15:27:22 -0500827 if (context->getState().getReadFramebuffer()->id() != 0 &&
828 framebuffer->getSamples(context->getData()) != 0)
Jamie Madill3c7fa222014-06-05 13:08:51 -0400829 {
Geoff Langb1196682014-07-23 13:47:29 -0400830 context->recordError(Error(GL_INVALID_OPERATION));
831 return false;
Jamie Madill3c7fa222014-06-05 13:08:51 -0400832 }
833
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400834 const gl::FramebufferAttachment *source = framebuffer->getReadColorbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400835 GLenum colorbufferInternalFormat = source->getInternalFormat();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400836
837 if (isSubImage)
838 {
Geoff Lang5d601382014-07-22 15:14:06 -0400839 if (!IsValidES3CopyTexImageCombination(textureInternalFormat, colorbufferInternalFormat,
840 context->getState().getReadFramebuffer()->id()))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400841 {
Geoff Langb1196682014-07-23 13:47:29 -0400842 context->recordError(Error(GL_INVALID_OPERATION));
843 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400844 }
845 }
Shannon Woods4d161ba2014-03-17 18:13:30 -0400846 else
847 {
Geoff Lang5d601382014-07-22 15:14:06 -0400848 if (!gl::IsValidES3CopyTexImageCombination(internalformat, colorbufferInternalFormat,
849 context->getState().getReadFramebuffer()->id()))
Shannon Woods4d161ba2014-03-17 18:13:30 -0400850 {
Geoff Langb1196682014-07-23 13:47:29 -0400851 context->recordError(Error(GL_INVALID_OPERATION));
852 return false;
Shannon Woods4d161ba2014-03-17 18:13:30 -0400853 }
854 }
855
Geoff Lang784a8fd2013-09-24 12:33:16 -0400856 // If width or height is zero, it is a no-op. Return false without setting an error.
857 return (width > 0 && height > 0);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400858}
859
Geoff Langb1196682014-07-23 13:47:29 -0400860bool ValidateES3TexStorageParameters(Context *context, GLenum target, GLsizei levels, GLenum internalformat,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400861 GLsizei width, GLsizei height, GLsizei depth)
862{
863 if (width < 1 || height < 1 || depth < 1 || levels < 1)
864 {
Geoff Langb1196682014-07-23 13:47:29 -0400865 context->recordError(Error(GL_INVALID_VALUE));
866 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400867 }
868
Geoff Langb92c1332015-09-04 12:54:55 -0400869 GLsizei maxDim = std::max(width, height);
870 if (target != GL_TEXTURE_2D_ARRAY)
871 {
872 maxDim = std::max(maxDim, depth);
873 }
874
875 if (levels > gl::log2(maxDim) + 1)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400876 {
Geoff Langb1196682014-07-23 13:47:29 -0400877 context->recordError(Error(GL_INVALID_OPERATION));
878 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400879 }
880
Geoff Langaae65a42014-05-26 12:43:44 -0400881 const gl::Caps &caps = context->getCaps();
882
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400883 switch (target)
884 {
885 case GL_TEXTURE_2D:
886 {
Geoff Langaae65a42014-05-26 12:43:44 -0400887 if (static_cast<GLuint>(width) > caps.max2DTextureSize ||
888 static_cast<GLuint>(height) > caps.max2DTextureSize)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400889 {
Geoff Langb1196682014-07-23 13:47:29 -0400890 context->recordError(Error(GL_INVALID_VALUE));
891 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400892 }
893 }
894 break;
895
Geoff Lang01c21d22013-09-24 11:52:16 -0400896 case GL_TEXTURE_CUBE_MAP:
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400897 {
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400898 if (width != height)
899 {
Geoff Langb1196682014-07-23 13:47:29 -0400900 context->recordError(Error(GL_INVALID_VALUE));
901 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400902 }
903
Geoff Langaae65a42014-05-26 12:43:44 -0400904 if (static_cast<GLuint>(width) > caps.maxCubeMapTextureSize)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400905 {
Geoff Langb1196682014-07-23 13:47:29 -0400906 context->recordError(Error(GL_INVALID_VALUE));
907 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400908 }
909 }
910 break;
911
912 case GL_TEXTURE_3D:
913 {
Geoff Langaae65a42014-05-26 12:43:44 -0400914 if (static_cast<GLuint>(width) > caps.max3DTextureSize ||
915 static_cast<GLuint>(height) > caps.max3DTextureSize ||
916 static_cast<GLuint>(depth) > caps.max3DTextureSize)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400917 {
Geoff Langb1196682014-07-23 13:47:29 -0400918 context->recordError(Error(GL_INVALID_VALUE));
919 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400920 }
921 }
922 break;
923
924 case GL_TEXTURE_2D_ARRAY:
925 {
Geoff Langaae65a42014-05-26 12:43:44 -0400926 if (static_cast<GLuint>(width) > caps.max2DTextureSize ||
927 static_cast<GLuint>(height) > caps.max2DTextureSize ||
928 static_cast<GLuint>(depth) > caps.maxArrayTextureLayers)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400929 {
Geoff Langb1196682014-07-23 13:47:29 -0400930 context->recordError(Error(GL_INVALID_VALUE));
931 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400932 }
933 }
934 break;
935
936 default:
Geoff Langb1196682014-07-23 13:47:29 -0400937 context->recordError(Error(GL_INVALID_ENUM));
938 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400939 }
940
Geoff Lang691e58c2014-12-19 17:03:25 -0500941 gl::Texture *texture = context->getTargetTexture(target);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400942 if (!texture || texture->id() == 0)
943 {
Geoff Langb1196682014-07-23 13:47:29 -0400944 context->recordError(Error(GL_INVALID_OPERATION));
945 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400946 }
947
948 if (texture->isImmutable())
949 {
Geoff Langb1196682014-07-23 13:47:29 -0400950 context->recordError(Error(GL_INVALID_OPERATION));
951 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400952 }
953
Geoff Lang5d601382014-07-22 15:14:06 -0400954 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalformat);
955 if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions()))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400956 {
Geoff Langb1196682014-07-23 13:47:29 -0400957 context->recordError(Error(GL_INVALID_ENUM));
958 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400959 }
960
Geoff Lang5d601382014-07-22 15:14:06 -0400961 if (formatInfo.pixelBytes == 0)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400962 {
Geoff Langb1196682014-07-23 13:47:29 -0400963 context->recordError(Error(GL_INVALID_ENUM));
964 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400965 }
966
967 return true;
968}
969
Geoff Langb1196682014-07-23 13:47:29 -0400970bool ValidateFramebufferTextureLayer(Context *context, GLenum target, GLenum attachment,
Jamie Madill570f7c82014-07-03 10:38:54 -0400971 GLuint texture, GLint level, GLint layer)
972{
973 if (context->getClientVersion() < 3)
974 {
Geoff Langb1196682014-07-23 13:47:29 -0400975 context->recordError(Error(GL_INVALID_OPERATION));
976 return false;
Jamie Madill570f7c82014-07-03 10:38:54 -0400977 }
978
Jamie Madill55ec3b12014-07-03 10:38:57 -0400979 if (layer < 0)
980 {
Geoff Langb1196682014-07-23 13:47:29 -0400981 context->recordError(Error(GL_INVALID_VALUE));
982 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -0400983 }
984
985 if (!ValidateFramebufferTextureBase(context, target, attachment, texture, level))
986 {
987 return false;
988 }
989
990 const gl::Caps &caps = context->getCaps();
991 if (texture != 0)
992 {
993 gl::Texture *tex = context->getTexture(texture);
994 ASSERT(tex);
995
996 switch (tex->getTarget())
997 {
998 case GL_TEXTURE_2D_ARRAY:
999 {
1000 if (level > gl::log2(caps.max2DTextureSize))
1001 {
Geoff Langb1196682014-07-23 13:47:29 -04001002 context->recordError(Error(GL_INVALID_VALUE));
1003 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001004 }
1005
1006 if (static_cast<GLuint>(layer) >= caps.maxArrayTextureLayers)
1007 {
Geoff Langb1196682014-07-23 13:47:29 -04001008 context->recordError(Error(GL_INVALID_VALUE));
1009 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001010 }
Jamie Madill55ec3b12014-07-03 10:38:57 -04001011 }
1012 break;
1013
1014 case GL_TEXTURE_3D:
1015 {
1016 if (level > gl::log2(caps.max3DTextureSize))
1017 {
Geoff Langb1196682014-07-23 13:47:29 -04001018 context->recordError(Error(GL_INVALID_VALUE));
1019 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001020 }
1021
1022 if (static_cast<GLuint>(layer) >= caps.max3DTextureSize)
1023 {
Geoff Langb1196682014-07-23 13:47:29 -04001024 context->recordError(Error(GL_INVALID_VALUE));
1025 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001026 }
Jamie Madill55ec3b12014-07-03 10:38:57 -04001027 }
1028 break;
1029
1030 default:
Geoff Langb1196682014-07-23 13:47:29 -04001031 context->recordError(Error(GL_INVALID_OPERATION));
1032 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001033 }
Geoff Langa9be0dc2014-12-17 12:34:40 -05001034
1035 const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(tex->getInternalFormat(tex->getTarget(), level));
1036 if (internalFormatInfo.compressed)
1037 {
1038 context->recordError(Error(GL_INVALID_OPERATION));
1039 return false;
1040 }
Jamie Madill55ec3b12014-07-03 10:38:57 -04001041 }
1042
1043 return true;
Jamie Madill570f7c82014-07-03 10:38:54 -04001044}
1045
Geoff Langb1196682014-07-23 13:47:29 -04001046bool ValidES3ReadFormatType(Context *context, GLenum internalFormat, GLenum format, GLenum type)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001047{
Geoff Lang5d601382014-07-22 15:14:06 -04001048 const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(internalFormat);
1049
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001050 switch (format)
1051 {
1052 case GL_RGBA:
1053 switch (type)
1054 {
1055 case GL_UNSIGNED_BYTE:
1056 break;
1057 case GL_UNSIGNED_INT_2_10_10_10_REV:
1058 if (internalFormat != GL_RGB10_A2)
1059 {
1060 return false;
1061 }
1062 break;
Geoff Lang1ec57f82013-10-16 11:43:23 -04001063 case GL_FLOAT:
Geoff Lang5d601382014-07-22 15:14:06 -04001064 if (internalFormatInfo.componentType != GL_FLOAT)
Geoff Lang1ec57f82013-10-16 11:43:23 -04001065 {
1066 return false;
1067 }
1068 break;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001069 default:
1070 return false;
1071 }
1072 break;
1073 case GL_RGBA_INTEGER:
1074 switch (type)
1075 {
1076 case GL_INT:
Geoff Lang5d601382014-07-22 15:14:06 -04001077 if (internalFormatInfo.componentType != GL_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001078 {
1079 return false;
1080 }
1081 break;
1082 case GL_UNSIGNED_INT:
Geoff Lang5d601382014-07-22 15:14:06 -04001083 if (internalFormatInfo.componentType != GL_UNSIGNED_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001084 {
1085 return false;
1086 }
1087 break;
1088 default:
1089 return false;
1090 }
1091 break;
1092 case GL_BGRA_EXT:
1093 switch (type)
1094 {
1095 case GL_UNSIGNED_BYTE:
1096 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
1097 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
1098 break;
1099 default:
1100 return false;
1101 }
1102 break;
Geoff Langbdc9b2f2014-04-16 14:41:54 -04001103 case GL_RG_EXT:
1104 case GL_RED_EXT:
Geoff Langc0b9ef42014-07-02 10:02:37 -04001105 if (!context->getExtensions().textureRG)
Geoff Langbdc9b2f2014-04-16 14:41:54 -04001106 {
1107 return false;
1108 }
1109 switch (type)
1110 {
1111 case GL_UNSIGNED_BYTE:
1112 break;
1113 default:
1114 return false;
1115 }
1116 break;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001117 default:
1118 return false;
1119 }
1120 return true;
1121}
1122
Corentin Walleze0902642014-11-04 12:32:15 -08001123bool ValidateES3RenderbufferStorageParameters(gl::Context *context, GLenum target, GLsizei samples,
1124 GLenum internalformat, GLsizei width, GLsizei height)
1125{
1126 if (!ValidateRenderbufferStorageParametersBase(context, target, samples, internalformat, width, height))
1127 {
1128 return false;
1129 }
1130
1131 //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.
1132 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalformat);
1133 if ((formatInfo.componentType == GL_UNSIGNED_INT || formatInfo.componentType == GL_INT) && samples > 0)
1134 {
1135 context->recordError(Error(GL_INVALID_OPERATION));
1136 return false;
1137 }
1138
1139 // The behavior is different than the ANGLE version, which would generate a GL_OUT_OF_MEMORY.
1140 const TextureCaps &formatCaps = context->getTextureCaps().get(internalformat);
1141 if (static_cast<GLuint>(samples) > formatCaps.getMaxSamples())
1142 {
1143 context->recordError(Error(GL_INVALID_VALUE));
1144 return false;
1145 }
1146
1147 return true;
1148}
1149
Austin Kinross08332632015-05-05 13:35:47 -07001150bool ValidateInvalidateFramebuffer(Context *context, GLenum target, GLsizei numAttachments,
1151 const GLenum *attachments)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001152{
Austin Kinross08332632015-05-05 13:35:47 -07001153 if (context->getClientVersion() < 3)
1154 {
1155 context->recordError(Error(GL_INVALID_OPERATION, "Operation only supported on ES 3.0 and above"));
1156 return false;
1157 }
1158
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001159 bool defaultFramebuffer = false;
1160
1161 switch (target)
1162 {
1163 case GL_DRAW_FRAMEBUFFER:
1164 case GL_FRAMEBUFFER:
Shannon Woods53a94a82014-06-24 15:20:36 -04001165 defaultFramebuffer = context->getState().getDrawFramebuffer()->id() == 0;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001166 break;
1167 case GL_READ_FRAMEBUFFER:
Shannon Woods53a94a82014-06-24 15:20:36 -04001168 defaultFramebuffer = context->getState().getReadFramebuffer()->id() == 0;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001169 break;
1170 default:
Austin Kinross08332632015-05-05 13:35:47 -07001171 context->recordError(Error(GL_INVALID_ENUM, "Invalid framebuffer target"));
1172 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001173 }
1174
Austin Kinross08332632015-05-05 13:35:47 -07001175 return ValidateDiscardFramebufferBase(context, target, numAttachments, attachments, defaultFramebuffer);
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001176}
1177
Geoff Langb1196682014-07-23 13:47:29 -04001178bool ValidateClearBuffer(Context *context)
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001179{
1180 if (context->getClientVersion() < 3)
1181 {
Geoff Langb1196682014-07-23 13:47:29 -04001182 context->recordError(Error(GL_INVALID_OPERATION));
1183 return false;
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001184 }
1185
Shannon Woods53a94a82014-06-24 15:20:36 -04001186 const gl::Framebuffer *fbo = context->getState().getDrawFramebuffer();
Geoff Lang748f74e2014-12-01 11:25:34 -05001187 if (!fbo || fbo->checkStatus(context->getData()) != GL_FRAMEBUFFER_COMPLETE)
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001188 {
Geoff Langb1196682014-07-23 13:47:29 -04001189 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
1190 return false;
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001191 }
1192
1193 return true;
1194}
1195
Geoff Langb1196682014-07-23 13:47:29 -04001196bool ValidateGetUniformuiv(Context *context, GLuint program, GLint location, GLuint* params)
Jamie Madill0063c512014-08-25 15:47:53 -04001197{
1198 if (context->getClientVersion() < 3)
1199 {
Geoff Langb1196682014-07-23 13:47:29 -04001200 context->recordError(Error(GL_INVALID_OPERATION));
1201 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001202 }
1203
Jamie Madill78f41802014-08-25 15:47:55 -04001204 return ValidateGetUniformBase(context, program, location);
Jamie Madill0063c512014-08-25 15:47:53 -04001205}
1206
Jamie Madillb885e572015-02-03 16:16:04 -05001207bool ValidateReadBuffer(Context *context, GLenum src)
1208{
1209 if (context->getClientVersion() < 3)
1210 {
1211 context->recordError(Error(GL_INVALID_OPERATION));
1212 return false;
1213 }
1214
1215 Framebuffer *readFBO = context->getState().getReadFramebuffer();
1216
1217 if (readFBO == nullptr)
1218 {
1219 context->recordError(gl::Error(GL_INVALID_OPERATION, "No active read framebuffer."));
1220 return false;
1221 }
1222
1223 if (src == GL_NONE)
1224 {
1225 return true;
1226 }
1227
1228 if (src != GL_BACK && (src < GL_COLOR_ATTACHMENT0 || src > GL_COLOR_ATTACHMENT15))
1229 {
1230 context->recordError(gl::Error(GL_INVALID_ENUM, "Unknown enum for 'src' in ReadBuffer"));
1231 return false;
1232 }
1233
1234 if (readFBO->id() == 0)
1235 {
1236 if (src != GL_BACK)
1237 {
1238 const char *errorMsg = "'src' must be GL_NONE or GL_BACK when reading from the default framebuffer.";
1239 context->recordError(gl::Error(GL_INVALID_OPERATION, errorMsg));
1240 return false;
1241 }
1242 }
1243 else
1244 {
1245 GLuint drawBuffer = static_cast<GLuint>(src - GL_COLOR_ATTACHMENT0);
1246
1247 if (drawBuffer >= context->getCaps().maxDrawBuffers)
1248 {
1249 const char *errorMsg = "'src' is greater than MAX_DRAW_BUFFERS.";
1250 context->recordError(gl::Error(GL_INVALID_OPERATION, errorMsg));
1251 return false;
1252 }
1253 }
1254
1255 return true;
1256}
1257
Jamie Madill86af3d22015-07-21 15:14:07 -04001258bool ValidateCompressedTexImage3D(Context *context,
1259 GLenum target,
1260 GLint level,
1261 GLenum internalformat,
1262 GLsizei width,
1263 GLsizei height,
1264 GLsizei depth,
1265 GLint border,
1266 GLsizei imageSize,
1267 const GLvoid *data)
1268{
1269 if (context->getClientVersion() < 3)
1270 {
1271 context->recordError(Error(GL_INVALID_OPERATION));
1272 return false;
1273 }
1274
1275 const InternalFormat &formatInfo = GetInternalFormatInfo(internalformat);
1276 if (imageSize < 0 ||
1277 static_cast<GLuint>(imageSize) !=
1278 formatInfo.computeBlockSize(GL_UNSIGNED_BYTE, width, height))
1279 {
1280 context->recordError(Error(GL_INVALID_VALUE));
1281 return false;
1282 }
1283
1284 // 3D texture target validation
1285 if (target != GL_TEXTURE_3D && target != GL_TEXTURE_2D_ARRAY)
1286 {
1287 context->recordError(
1288 Error(GL_INVALID_ENUM, "Must specify a valid 3D texture destination target"));
1289 return false;
1290 }
1291
1292 // validateES3TexImageFormat sets the error code if there is an error
1293 if (!ValidateES3TexImageParameters(context, target, level, internalformat, true, false, 0, 0, 0,
1294 width, height, depth, border, GL_NONE, GL_NONE, data))
1295 {
1296 return false;
1297 }
1298
1299 return true;
1300}
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001301}