blob: d9e0dad26230a6ddaf312737489999cacb659958 [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// validationES.h: Validation functions for generic OpenGL ES entry point parameters
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/validationES.h"
Jamie Madille2e406c2016-06-02 13:04:10 -040010
Geoff Lang2b5420c2014-11-19 14:20:15 -050011#include "libANGLE/Context.h"
Geoff Langa8406172015-07-21 16:53:39 -040012#include "libANGLE/Display.h"
Brandon Jones6cad5662017-06-14 13:25:13 -070013#include "libANGLE/ErrorStrings.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050014#include "libANGLE/Framebuffer.h"
15#include "libANGLE/FramebufferAttachment.h"
Geoff Langa8406172015-07-21 16:53:39 -040016#include "libANGLE/Image.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050017#include "libANGLE/Program.h"
Jamie Madill231c7f52017-04-26 13:45:37 -040018#include "libANGLE/Query.h"
19#include "libANGLE/Texture.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050020#include "libANGLE/TransformFeedback.h"
21#include "libANGLE/VertexArray.h"
James Darpinian30b604d2018-03-12 17:26:57 -070022#include "libANGLE/angletypes.h"
Jamie Madill231c7f52017-04-26 13:45:37 -040023#include "libANGLE/formatutils.h"
jchen10a99ed552017-09-22 08:10:32 +080024#include "libANGLE/queryconversions.h"
Lingfeng Yangf97641c2018-06-21 19:22:45 -070025#include "libANGLE/queryutils.h"
Jamie Madill231c7f52017-04-26 13:45:37 -040026#include "libANGLE/validationES2.h"
27#include "libANGLE/validationES3.h"
Geoff Lange8ebe7f2013-08-05 15:03:13 -040028
29#include "common/mathutil.h"
30#include "common/utilities.h"
31
Jamie Madille2e406c2016-06-02 13:04:10 -040032using namespace angle;
33
Geoff Lange8ebe7f2013-08-05 15:03:13 -040034namespace gl
35{
Jamie Madill1ca74672015-07-21 15:14:11 -040036namespace
37{
Luc Ferron9dbaeba2018-02-01 07:26:59 -050038bool CompressedTextureFormatRequiresExactSize(GLenum internalFormat)
39{
40 // List of compressed format that require that the texture size is smaller than or a multiple of
41 // the compressed block size.
42 switch (internalFormat)
43 {
44 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
45 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
46 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
47 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
48 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
49 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
50 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
51 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
52 case GL_ETC1_RGB8_LOSSY_DECODE_ANGLE:
53 case GL_COMPRESSED_RGB8_LOSSY_DECODE_ETC2_ANGLE:
54 case GL_COMPRESSED_SRGB8_LOSSY_DECODE_ETC2_ANGLE:
55 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_LOSSY_DECODE_ETC2_ANGLE:
56 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_LOSSY_DECODE_ETC2_ANGLE:
57 case GL_COMPRESSED_RGBA8_LOSSY_DECODE_ETC2_EAC_ANGLE:
58 case GL_COMPRESSED_SRGB8_ALPHA8_LOSSY_DECODE_ETC2_EAC_ANGLE:
59 return true;
jchen10a99ed552017-09-22 08:10:32 +080060
Luc Ferron9dbaeba2018-02-01 07:26:59 -050061 default:
62 return false;
63 }
64}
65bool CompressedSubTextureFormatRequiresExactSize(GLenum internalFormat)
66{
67 // Compressed sub textures have additional formats that requires exact size.
68 // ES 3.1, Section 8.7, Page 171
69 return CompressedTextureFormatRequiresExactSize(internalFormat) ||
70 IsETC2EACFormat(internalFormat);
71}
Olli Etuaho8d5571a2018-04-23 12:29:31 +030072
73bool DifferenceCanOverflow(GLint a, GLint b)
74{
75 CheckedNumeric<GLint> checkedA(a);
76 checkedA -= b;
77 // Use negation to make sure that the difference can't overflow regardless of the order.
78 checkedA = -checkedA;
79 return !checkedA.IsValid();
80}
81
Jamie Madillac43aaa2018-07-31 11:22:13 -040082bool ValidateDrawClientAttribs(Context *context)
83{
Jamie Madill2da53562018-08-01 11:34:47 -040084 ASSERT(context->getStateCache().hasAnyEnabledClientAttrib());
Jamie Madillac43aaa2018-07-31 11:22:13 -040085
86 const gl::State &state = context->getGLState();
87
88 if (context->getExtensions().webglCompatibility || !state.areClientArraysEnabled())
89 {
90 // [WebGL 1.0] Section 6.5 Enabled Vertex Attributes and Range Checking
91 // If a vertex attribute is enabled as an array via enableVertexAttribArray but no
92 // buffer is bound to that attribute via bindBuffer and vertexAttribPointer, then calls
93 // to drawArrays or drawElements will generate an INVALID_OPERATION error.
94 ANGLE_VALIDATION_ERR(context, InvalidOperation(), VertexArrayNoBuffer);
95 return false;
96 }
97
98 if (state.getVertexArray()->hasEnabledNullPointerClientArray())
99 {
100 // This is an application error that would normally result in a crash, but we catch it
101 // and return an error
102 ANGLE_VALIDATION_ERR(context, InvalidOperation(), VertexArrayNoBufferPointer);
103 return false;
104 }
105
106 return true;
107}
108
Jamie Madill2da53562018-08-01 11:34:47 -0400109bool ValidateDrawAttribs(Context *context, GLint primcount, GLint maxVertex)
Jamie Madill1ca74672015-07-21 15:14:11 -0400110{
Jamie Madill51af38b2018-04-15 08:50:56 -0400111 // If we're drawing zero vertices, we have enough data.
Jamie Madill2da53562018-08-01 11:34:47 -0400112 ASSERT(primcount > 0);
Jamie Madill51af38b2018-04-15 08:50:56 -0400113
Jamie Madilla2d1d2d2018-08-01 11:34:46 -0400114 if (maxVertex <= context->getStateCache().getNonInstancedVertexElementLimit() &&
115 (primcount - 1) <= context->getStateCache().getInstancedVertexElementLimit())
Jamie Madill1ca74672015-07-21 15:14:11 -0400116 {
Jamie Madilla2d1d2d2018-08-01 11:34:46 -0400117 return true;
Jamie Madill02c9c042018-04-17 13:43:48 -0400118 }
James Darpiniane8a93c62018-01-04 18:02:24 -0800119
Jamie Madilla2d1d2d2018-08-01 11:34:46 -0400120 // An overflow can happen when adding the offset. Negative indicates overflow.
121 if (context->getStateCache().getNonInstancedVertexElementLimit() < 0 ||
122 context->getStateCache().getInstancedVertexElementLimit() < 0)
123 {
124 ANGLE_VALIDATION_ERR(context, InvalidOperation(), IntegerOverflow);
125 return false;
126 }
127
128 // [OpenGL ES 3.0.2] section 2.9.4 page 40:
129 // We can return INVALID_OPERATION if our buffer does not have enough backing data.
130 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InsufficientVertexBufferSize);
131 return false;
Jamie Madill1ca74672015-07-21 15:14:11 -0400132}
133
Jamie Madill5b772312018-03-08 20:28:32 -0500134bool ValidReadPixelsTypeEnum(Context *context, GLenum type)
Geoff Lang280ba992017-04-18 16:30:58 -0400135{
136 switch (type)
137 {
138 // Types referenced in Table 3.4 of the ES 2.0.25 spec
139 case GL_UNSIGNED_BYTE:
140 case GL_UNSIGNED_SHORT_4_4_4_4:
141 case GL_UNSIGNED_SHORT_5_5_5_1:
142 case GL_UNSIGNED_SHORT_5_6_5:
143 return context->getClientVersion() >= ES_2_0;
144
145 // Types referenced in Table 3.2 of the ES 3.0.5 spec (Except depth stencil)
146 case GL_BYTE:
147 case GL_INT:
148 case GL_SHORT:
149 case GL_UNSIGNED_INT:
150 case GL_UNSIGNED_INT_10F_11F_11F_REV:
151 case GL_UNSIGNED_INT_24_8:
152 case GL_UNSIGNED_INT_2_10_10_10_REV:
153 case GL_UNSIGNED_INT_5_9_9_9_REV:
154 case GL_UNSIGNED_SHORT:
155 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
156 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
157 return context->getClientVersion() >= ES_3_0;
158
159 case GL_FLOAT:
Geoff Lang7d4602f2017-09-13 10:45:09 -0400160 return context->getClientVersion() >= ES_3_0 || context->getExtensions().textureFloat ||
161 context->getExtensions().colorBufferHalfFloat;
Geoff Lang280ba992017-04-18 16:30:58 -0400162
163 case GL_HALF_FLOAT:
164 return context->getClientVersion() >= ES_3_0 ||
165 context->getExtensions().textureHalfFloat;
166
167 case GL_HALF_FLOAT_OES:
168 return context->getExtensions().colorBufferHalfFloat;
169
170 default:
171 return false;
172 }
173}
174
Jamie Madill5b772312018-03-08 20:28:32 -0500175bool ValidReadPixelsFormatEnum(Context *context, GLenum format)
Geoff Lang280ba992017-04-18 16:30:58 -0400176{
177 switch (format)
178 {
179 // Formats referenced in Table 3.4 of the ES 2.0.25 spec (Except luminance)
180 case GL_RGBA:
181 case GL_RGB:
182 case GL_ALPHA:
183 return context->getClientVersion() >= ES_2_0;
184
185 // Formats referenced in Table 3.2 of the ES 3.0.5 spec
186 case GL_RG:
187 case GL_RED:
188 case GL_RGBA_INTEGER:
189 case GL_RGB_INTEGER:
190 case GL_RG_INTEGER:
191 case GL_RED_INTEGER:
192 return context->getClientVersion() >= ES_3_0;
193
194 case GL_SRGB_ALPHA_EXT:
195 case GL_SRGB_EXT:
196 return context->getExtensions().sRGB;
197
198 case GL_BGRA_EXT:
199 return context->getExtensions().readFormatBGRA;
200
201 default:
202 return false;
203 }
204}
205
Jamie Madill5b772312018-03-08 20:28:32 -0500206bool ValidReadPixelsFormatType(Context *context,
Geoff Langf607c602016-09-21 11:46:48 -0400207 GLenum framebufferComponentType,
208 GLenum format,
209 GLenum type)
210{
211 switch (framebufferComponentType)
212 {
213 case GL_UNSIGNED_NORMALIZED:
214 // TODO(geofflang): Don't accept BGRA here. Some chrome internals appear to try to use
215 // ReadPixels with BGRA even if the extension is not present
216 return (format == GL_RGBA && type == GL_UNSIGNED_BYTE) ||
217 (context->getExtensions().readFormatBGRA && format == GL_BGRA_EXT &&
218 type == GL_UNSIGNED_BYTE);
219
220 case GL_SIGNED_NORMALIZED:
221 return (format == GL_RGBA && type == GL_UNSIGNED_BYTE);
222
223 case GL_INT:
224 return (format == GL_RGBA_INTEGER && type == GL_INT);
225
226 case GL_UNSIGNED_INT:
227 return (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_INT);
228
229 case GL_FLOAT:
230 return (format == GL_RGBA && type == GL_FLOAT);
231
232 default:
233 UNREACHABLE();
234 return false;
235 }
236}
237
Geoff Langc1984ed2016-10-07 12:41:00 -0400238template <typename ParamType>
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400239bool ValidateTextureWrapModeValue(Context *context, ParamType *params, bool restrictedWrapModes)
Geoff Langc1984ed2016-10-07 12:41:00 -0400240{
241 switch (ConvertToGLenum(params[0]))
242 {
243 case GL_CLAMP_TO_EDGE:
244 break;
245
246 case GL_REPEAT:
247 case GL_MIRRORED_REPEAT:
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400248 if (restrictedWrapModes)
Geoff Langc1984ed2016-10-07 12:41:00 -0400249 {
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400250 // OES_EGL_image_external and ANGLE_texture_rectangle specifies this error.
Brandon Jonesafa75152017-07-21 13:11:29 -0700251 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidWrapModeTexture);
Geoff Langc1984ed2016-10-07 12:41:00 -0400252 return false;
253 }
254 break;
255
256 default:
Brandon Jonesafa75152017-07-21 13:11:29 -0700257 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTextureWrap);
Geoff Langc1984ed2016-10-07 12:41:00 -0400258 return false;
259 }
260
261 return true;
262}
263
264template <typename ParamType>
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400265bool ValidateTextureMinFilterValue(Context *context, ParamType *params, bool restrictedMinFilter)
Geoff Langc1984ed2016-10-07 12:41:00 -0400266{
267 switch (ConvertToGLenum(params[0]))
268 {
269 case GL_NEAREST:
270 case GL_LINEAR:
271 break;
272
273 case GL_NEAREST_MIPMAP_NEAREST:
274 case GL_LINEAR_MIPMAP_NEAREST:
275 case GL_NEAREST_MIPMAP_LINEAR:
276 case GL_LINEAR_MIPMAP_LINEAR:
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400277 if (restrictedMinFilter)
Geoff Langc1984ed2016-10-07 12:41:00 -0400278 {
279 // OES_EGL_image_external specifies this error.
Brandon Jonesafa75152017-07-21 13:11:29 -0700280 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidFilterTexture);
Geoff Langc1984ed2016-10-07 12:41:00 -0400281 return false;
282 }
283 break;
284
285 default:
Brandon Jones6cad5662017-06-14 13:25:13 -0700286 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTextureFilterParam);
Geoff Langc1984ed2016-10-07 12:41:00 -0400287 return false;
288 }
289
290 return true;
291}
292
293template <typename ParamType>
294bool ValidateTextureMagFilterValue(Context *context, ParamType *params)
295{
296 switch (ConvertToGLenum(params[0]))
297 {
298 case GL_NEAREST:
299 case GL_LINEAR:
300 break;
301
302 default:
Brandon Jones6cad5662017-06-14 13:25:13 -0700303 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTextureFilterParam);
Geoff Langc1984ed2016-10-07 12:41:00 -0400304 return false;
305 }
306
307 return true;
308}
309
310template <typename ParamType>
311bool ValidateTextureCompareModeValue(Context *context, ParamType *params)
312{
313 // Acceptable mode parameters from GLES 3.0.2 spec, table 3.17
314 switch (ConvertToGLenum(params[0]))
315 {
316 case GL_NONE:
317 case GL_COMPARE_REF_TO_TEXTURE:
318 break;
319
320 default:
Brandon Jonesafa75152017-07-21 13:11:29 -0700321 ANGLE_VALIDATION_ERR(context, InvalidEnum(), UnknownParameter);
Geoff Langc1984ed2016-10-07 12:41:00 -0400322 return false;
323 }
324
325 return true;
326}
327
328template <typename ParamType>
329bool ValidateTextureCompareFuncValue(Context *context, ParamType *params)
330{
331 // Acceptable function parameters from GLES 3.0.2 spec, table 3.17
332 switch (ConvertToGLenum(params[0]))
333 {
334 case GL_LEQUAL:
335 case GL_GEQUAL:
336 case GL_LESS:
337 case GL_GREATER:
338 case GL_EQUAL:
339 case GL_NOTEQUAL:
340 case GL_ALWAYS:
341 case GL_NEVER:
342 break;
343
344 default:
Brandon Jonesafa75152017-07-21 13:11:29 -0700345 ANGLE_VALIDATION_ERR(context, InvalidEnum(), UnknownParameter);
Geoff Langc1984ed2016-10-07 12:41:00 -0400346 return false;
347 }
348
349 return true;
350}
351
352template <typename ParamType>
Geoff Lang81c6b572016-10-19 14:07:52 -0700353bool ValidateTextureSRGBDecodeValue(Context *context, ParamType *params)
354{
355 if (!context->getExtensions().textureSRGBDecode)
356 {
Brandon Jones6cad5662017-06-14 13:25:13 -0700357 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ExtensionNotEnabled);
Geoff Lang81c6b572016-10-19 14:07:52 -0700358 return false;
359 }
360
361 switch (ConvertToGLenum(params[0]))
362 {
363 case GL_DECODE_EXT:
364 case GL_SKIP_DECODE_EXT:
365 break;
366
367 default:
Brandon Jonesafa75152017-07-21 13:11:29 -0700368 ANGLE_VALIDATION_ERR(context, InvalidEnum(), UnknownParameter);
Geoff Lang81c6b572016-10-19 14:07:52 -0700369 return false;
370 }
371
372 return true;
373}
374
Luc Ferron1b1a8642018-01-23 15:12:01 -0500375bool ValidateTextureMaxAnisotropyExtensionEnabled(Context *context)
376{
377 if (!context->getExtensions().textureFilterAnisotropic)
378 {
379 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ExtensionNotEnabled);
380 return false;
381 }
382
383 return true;
384}
385
386bool ValidateTextureMaxAnisotropyValue(Context *context, GLfloat paramValue)
387{
388 if (!ValidateTextureMaxAnisotropyExtensionEnabled(context))
389 {
390 return false;
391 }
392
393 GLfloat largest = context->getExtensions().maxTextureAnisotropy;
394
395 if (paramValue < 1 || paramValue > largest)
396 {
397 ANGLE_VALIDATION_ERR(context, InvalidValue(), OutsideOfBounds);
398 return false;
399 }
400
401 return true;
402}
403
Jamie Madill5b772312018-03-08 20:28:32 -0500404bool ValidateFragmentShaderColorBufferTypeMatch(Context *context)
Geoff Lange0cff192017-05-30 13:04:56 -0400405{
406 const Program *program = context->getGLState().getProgram();
407 const Framebuffer *framebuffer = context->getGLState().getDrawFramebuffer();
408
Brandon Jonesc405ae72017-12-06 14:15:03 -0800409 if (!ComponentTypeMask::Validate(program->getDrawBufferTypeMask().to_ulong(),
410 framebuffer->getDrawBufferTypeMask().to_ulong(),
411 program->getActiveOutputVariables().to_ulong(),
412 framebuffer->getDrawBufferMask().to_ulong()))
Geoff Lange0cff192017-05-30 13:04:56 -0400413 {
Brandon Jones76746f92017-11-22 11:44:41 -0800414 ANGLE_VALIDATION_ERR(context, InvalidOperation(), DrawBufferTypeMismatch);
415 return false;
Geoff Lange0cff192017-05-30 13:04:56 -0400416 }
417
418 return true;
419}
420
Jamie Madill5b772312018-03-08 20:28:32 -0500421bool ValidateVertexShaderAttributeTypeMatch(Context *context)
Geoff Lang9ab5b822017-05-30 16:19:23 -0400422{
Lingfeng Yang038dd532018-03-29 17:31:52 -0700423 const auto &glState = context->getGLState();
Geoff Lang9ab5b822017-05-30 16:19:23 -0400424 const Program *program = context->getGLState().getProgram();
425 const VertexArray *vao = context->getGLState().getVertexArray();
426
Brandon Jonesc405ae72017-12-06 14:15:03 -0800427 unsigned long stateCurrentValuesTypeBits = glState.getCurrentValuesTypeMask().to_ulong();
428 unsigned long vaoAttribTypeBits = vao->getAttributesTypeMask().to_ulong();
429 unsigned long vaoAttribEnabledMask = vao->getAttributesMask().to_ulong();
430
431 vaoAttribEnabledMask |= vaoAttribEnabledMask << MAX_COMPONENT_TYPE_MASK_INDEX;
432 vaoAttribTypeBits = (vaoAttribEnabledMask & vaoAttribTypeBits);
433 vaoAttribTypeBits |= (~vaoAttribEnabledMask & stateCurrentValuesTypeBits);
434
435 if (!ComponentTypeMask::Validate(program->getAttributesTypeMask().to_ulong(), vaoAttribTypeBits,
436 program->getAttributesMask().to_ulong(), 0xFFFF))
Geoff Lang9ab5b822017-05-30 16:19:23 -0400437 {
Brandon Jonesc405ae72017-12-06 14:15:03 -0800438 ANGLE_VALIDATION_ERR(context, InvalidOperation(), VertexShaderTypeMismatch);
439 return false;
Geoff Lang9ab5b822017-05-30 16:19:23 -0400440 }
Geoff Lang9ab5b822017-05-30 16:19:23 -0400441 return true;
442}
443
Jamie Madill493f9572018-05-24 19:52:15 -0400444bool IsCompatibleDrawModeWithGeometryShader(PrimitiveMode drawMode,
445 PrimitiveMode geometryShaderInputPrimitiveType)
Jiawei Shaofccebff2018-03-08 13:51:02 +0800446{
447 // [EXT_geometry_shader] Section 11.1gs.1, Geometry Shader Input Primitives
Jamie Madill493f9572018-05-24 19:52:15 -0400448 switch (drawMode)
Jiawei Shaofccebff2018-03-08 13:51:02 +0800449 {
Jamie Madill493f9572018-05-24 19:52:15 -0400450 case PrimitiveMode::Points:
451 return geometryShaderInputPrimitiveType == PrimitiveMode::Points;
452 case PrimitiveMode::Lines:
453 case PrimitiveMode::LineStrip:
454 case PrimitiveMode::LineLoop:
455 return geometryShaderInputPrimitiveType == PrimitiveMode::Lines;
456 case PrimitiveMode::LinesAdjacency:
457 case PrimitiveMode::LineStripAdjacency:
458 return geometryShaderInputPrimitiveType == PrimitiveMode::LinesAdjacency;
459 case PrimitiveMode::Triangles:
460 case PrimitiveMode::TriangleFan:
461 case PrimitiveMode::TriangleStrip:
462 return geometryShaderInputPrimitiveType == PrimitiveMode::Triangles;
463 case PrimitiveMode::TrianglesAdjacency:
464 case PrimitiveMode::TriangleStripAdjacency:
465 return geometryShaderInputPrimitiveType == PrimitiveMode::TrianglesAdjacency;
Jiawei Shaofccebff2018-03-08 13:51:02 +0800466 default:
467 UNREACHABLE();
468 return false;
469 }
470}
471
Lingfeng Yangf97641c2018-06-21 19:22:45 -0700472// GLES1 texture parameters are a small subset of the others
473bool IsValidGLES1TextureParameter(GLenum pname)
474{
475 switch (pname)
476 {
477 case GL_TEXTURE_MAG_FILTER:
478 case GL_TEXTURE_MIN_FILTER:
479 case GL_TEXTURE_WRAP_S:
480 case GL_TEXTURE_WRAP_T:
481 case GL_TEXTURE_WRAP_R:
482 case GL_GENERATE_MIPMAP:
483 case GL_TEXTURE_CROP_RECT_OES:
484 return true;
485 default:
486 return false;
487 }
488}
489
Geoff Langf41a7152016-09-19 15:11:17 -0400490} // anonymous namespace
491
Brandon Jonesd1049182018-03-28 10:02:20 -0700492void SetRobustLengthParam(GLsizei *length, GLsizei value)
493{
494 if (length)
495 {
496 *length = value;
497 }
498}
499
Luc Ferron9dbaeba2018-02-01 07:26:59 -0500500bool IsETC2EACFormat(const GLenum format)
501{
502 // ES 3.1, Table 8.19
503 switch (format)
504 {
505 case GL_COMPRESSED_R11_EAC:
506 case GL_COMPRESSED_SIGNED_R11_EAC:
507 case GL_COMPRESSED_RG11_EAC:
508 case GL_COMPRESSED_SIGNED_RG11_EAC:
509 case GL_COMPRESSED_RGB8_ETC2:
510 case GL_COMPRESSED_SRGB8_ETC2:
511 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
512 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
513 case GL_COMPRESSED_RGBA8_ETC2_EAC:
514 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
515 return true;
516
517 default:
518 return false;
519 }
520}
521
Jamie Madill5b772312018-03-08 20:28:32 -0500522bool ValidTextureTarget(const Context *context, TextureType type)
Jamie Madill35d15012013-10-07 10:46:37 -0400523{
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800524 switch (type)
Jamie Madill35d15012013-10-07 10:46:37 -0400525 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800526 case TextureType::_2D:
527 case TextureType::CubeMap:
He Yunchaoced53ae2016-11-29 15:00:51 +0800528 return true;
Jamie Madill35d15012013-10-07 10:46:37 -0400529
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800530 case TextureType::Rectangle:
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400531 return context->getExtensions().textureRectangle;
532
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800533 case TextureType::_3D:
534 case TextureType::_2DArray:
He Yunchaoced53ae2016-11-29 15:00:51 +0800535 return (context->getClientMajorVersion() >= 3);
Jamie Madilld7460c72014-01-21 16:38:14 -0500536
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800537 case TextureType::_2DMultisample:
He Yunchaoced53ae2016-11-29 15:00:51 +0800538 return (context->getClientVersion() >= Version(3, 1));
Geoff Lang3b573612016-10-31 14:08:10 -0400539
He Yunchaoced53ae2016-11-29 15:00:51 +0800540 default:
541 return false;
Jamie Madilld7460c72014-01-21 16:38:14 -0500542 }
Jamie Madill35d15012013-10-07 10:46:37 -0400543}
544
Jamie Madill5b772312018-03-08 20:28:32 -0500545bool ValidTexture2DTarget(const Context *context, TextureType type)
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500546{
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800547 switch (type)
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500548 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800549 case TextureType::_2D:
550 case TextureType::CubeMap:
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500551 return true;
552
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800553 case TextureType::Rectangle:
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400554 return context->getExtensions().textureRectangle;
555
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500556 default:
557 return false;
558 }
559}
560
Jamie Madill5b772312018-03-08 20:28:32 -0500561bool ValidTexture3DTarget(const Context *context, TextureType target)
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500562{
563 switch (target)
564 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800565 case TextureType::_3D:
566 case TextureType::_2DArray:
Martin Radev1be913c2016-07-11 17:59:16 +0300567 return (context->getClientMajorVersion() >= 3);
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500568
569 default:
570 return false;
571 }
572}
573
Ian Ewellbda75592016-04-18 17:25:54 -0400574// Most texture GL calls are not compatible with external textures, so we have a separate validation
575// function for use in the GL calls that do
Jamie Madill5b772312018-03-08 20:28:32 -0500576bool ValidTextureExternalTarget(const Context *context, TextureType target)
Ian Ewellbda75592016-04-18 17:25:54 -0400577{
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800578 return (target == TextureType::External) &&
Ian Ewellbda75592016-04-18 17:25:54 -0400579 (context->getExtensions().eglImageExternal ||
580 context->getExtensions().eglStreamConsumerExternal);
581}
582
Shannon Woods4dfed832014-03-17 20:03:39 -0400583// This function differs from ValidTextureTarget in that the target must be
584// usable as the destination of a 2D operation-- so a cube face is valid, but
585// GL_TEXTURE_CUBE_MAP is not.
Jamie Madill560a8d82014-05-21 13:06:20 -0400586// Note: duplicate of IsInternalTextureTarget
Jamie Madill5b772312018-03-08 20:28:32 -0500587bool ValidTexture2DDestinationTarget(const Context *context, TextureTarget target)
Shannon Woods4dfed832014-03-17 20:03:39 -0400588{
589 switch (target)
590 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800591 case TextureTarget::_2D:
592 case TextureTarget::CubeMapNegativeX:
593 case TextureTarget::CubeMapNegativeY:
594 case TextureTarget::CubeMapNegativeZ:
595 case TextureTarget::CubeMapPositiveX:
596 case TextureTarget::CubeMapPositiveY:
597 case TextureTarget::CubeMapPositiveZ:
He Yunchaoced53ae2016-11-29 15:00:51 +0800598 return true;
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800599 case TextureTarget::Rectangle:
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400600 return context->getExtensions().textureRectangle;
He Yunchaoced53ae2016-11-29 15:00:51 +0800601 default:
602 return false;
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500603 }
604}
605
Jiawei Shao80c32cc2018-04-25 09:48:36 +0800606bool ValidateTransformFeedbackPrimitiveMode(const Context *context,
Jamie Madill493f9572018-05-24 19:52:15 -0400607 PrimitiveMode transformFeedbackPrimitiveMode,
608 PrimitiveMode renderPrimitiveMode)
Jiawei Shao80c32cc2018-04-25 09:48:36 +0800609{
610 ASSERT(context);
611
612 if (!context->getExtensions().geometryShader)
613 {
614 // It is an invalid operation to call DrawArrays or DrawArraysInstanced with a draw mode
615 // that does not match the current transform feedback object's draw mode (if transform
616 // feedback is active), (3.0.2, section 2.14, pg 86)
617 return transformFeedbackPrimitiveMode == renderPrimitiveMode;
618 }
619
620 // [GL_EXT_geometry_shader] Table 12.1gs
Jamie Madill493f9572018-05-24 19:52:15 -0400621 switch (renderPrimitiveMode)
Jiawei Shao80c32cc2018-04-25 09:48:36 +0800622 {
Jamie Madill493f9572018-05-24 19:52:15 -0400623 case PrimitiveMode::Points:
624 return transformFeedbackPrimitiveMode == PrimitiveMode::Points;
625 case PrimitiveMode::Lines:
626 case PrimitiveMode::LineStrip:
627 case PrimitiveMode::LineLoop:
628 return transformFeedbackPrimitiveMode == PrimitiveMode::Lines;
629 case PrimitiveMode::Triangles:
630 case PrimitiveMode::TriangleFan:
631 case PrimitiveMode::TriangleStrip:
632 return transformFeedbackPrimitiveMode == PrimitiveMode::Triangles;
Jiawei Shao80c32cc2018-04-25 09:48:36 +0800633 default:
634 UNREACHABLE();
635 return false;
636 }
637}
638
Jamie Madill5b772312018-03-08 20:28:32 -0500639bool ValidateDrawElementsInstancedBase(Context *context,
Jamie Madill493f9572018-05-24 19:52:15 -0400640 PrimitiveMode mode,
Jamie Madillbe849e42017-05-02 15:49:00 -0400641 GLsizei count,
642 GLenum type,
643 const GLvoid *indices,
644 GLsizei primcount)
645{
646 if (primcount < 0)
647 {
Brandon Jonesafa75152017-07-21 13:11:29 -0700648 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativePrimcount);
Jamie Madillbe849e42017-05-02 15:49:00 -0400649 return false;
650 }
651
652 if (!ValidateDrawElementsCommon(context, mode, count, type, indices, primcount))
653 {
654 return false;
655 }
656
Jamie Madill9fdaa492018-02-16 10:52:11 -0500657 return true;
Jamie Madillbe849e42017-05-02 15:49:00 -0400658}
659
660bool ValidateDrawArraysInstancedBase(Context *context,
Jamie Madill493f9572018-05-24 19:52:15 -0400661 PrimitiveMode mode,
Jamie Madillbe849e42017-05-02 15:49:00 -0400662 GLint first,
663 GLsizei count,
664 GLsizei primcount)
665{
666 if (primcount < 0)
667 {
Brandon Jonesafa75152017-07-21 13:11:29 -0700668 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativePrimcount);
Jamie Madillbe849e42017-05-02 15:49:00 -0400669 return false;
670 }
671
672 if (!ValidateDrawArraysCommon(context, mode, first, count, primcount))
673 {
674 return false;
675 }
676
Jamie Madill9fdaa492018-02-16 10:52:11 -0500677 return true;
Jamie Madillbe849e42017-05-02 15:49:00 -0400678}
679
Jamie Madill5b772312018-03-08 20:28:32 -0500680bool ValidateDrawInstancedANGLE(Context *context)
Jamie Madillbe849e42017-05-02 15:49:00 -0400681{
682 // Verify there is at least one active attribute with a divisor of zero
683 const State &state = context->getGLState();
684
685 Program *program = state.getProgram();
686
687 const auto &attribs = state.getVertexArray()->getVertexAttributes();
688 const auto &bindings = state.getVertexArray()->getVertexBindings();
689 for (size_t attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
690 {
691 const VertexAttribute &attrib = attribs[attributeIndex];
692 const VertexBinding &binding = bindings[attrib.bindingIndex];
Martin Radevdd5f27e2017-06-07 10:17:09 +0300693 if (program->isAttribLocationActive(attributeIndex) && binding.getDivisor() == 0)
Jamie Madillbe849e42017-05-02 15:49:00 -0400694 {
695 return true;
696 }
697 }
698
Brandon Jonesafa75152017-07-21 13:11:29 -0700699 ANGLE_VALIDATION_ERR(context, InvalidOperation(), NoZeroDivisor);
Jamie Madillbe849e42017-05-02 15:49:00 -0400700 return false;
701}
702
Jamie Madill5b772312018-03-08 20:28:32 -0500703bool ValidTexture3DDestinationTarget(const Context *context, TextureType target)
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500704{
705 switch (target)
706 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800707 case TextureType::_3D:
708 case TextureType::_2DArray:
He Yunchaoced53ae2016-11-29 15:00:51 +0800709 return true;
710 default:
711 return false;
Shannon Woods4dfed832014-03-17 20:03:39 -0400712 }
713}
714
Jamie Madill5b772312018-03-08 20:28:32 -0500715bool ValidTexLevelDestinationTarget(const Context *context, TextureType type)
He Yunchao11b038b2016-11-22 21:24:04 +0800716{
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800717 switch (type)
He Yunchao11b038b2016-11-22 21:24:04 +0800718 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800719 case TextureType::_2D:
720 case TextureType::_2DArray:
721 case TextureType::_2DMultisample:
722 case TextureType::CubeMap:
723 case TextureType::_3D:
He Yunchao11b038b2016-11-22 21:24:04 +0800724 return true;
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800725 case TextureType::Rectangle:
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400726 return context->getExtensions().textureRectangle;
He Yunchao11b038b2016-11-22 21:24:04 +0800727 default:
728 return false;
729 }
730}
731
Jamie Madill5b772312018-03-08 20:28:32 -0500732bool ValidFramebufferTarget(const Context *context, GLenum target)
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500733{
He Yunchaoced53ae2016-11-29 15:00:51 +0800734 static_assert(GL_DRAW_FRAMEBUFFER_ANGLE == GL_DRAW_FRAMEBUFFER &&
735 GL_READ_FRAMEBUFFER_ANGLE == GL_READ_FRAMEBUFFER,
Geoff Langd4475812015-03-18 10:53:05 -0400736 "ANGLE framebuffer enums must equal the ES3 framebuffer enums.");
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500737
738 switch (target)
739 {
He Yunchaoced53ae2016-11-29 15:00:51 +0800740 case GL_FRAMEBUFFER:
741 return true;
Geoff Lange8afa902017-09-27 15:00:43 -0400742
He Yunchaoced53ae2016-11-29 15:00:51 +0800743 case GL_READ_FRAMEBUFFER:
He Yunchaoced53ae2016-11-29 15:00:51 +0800744 case GL_DRAW_FRAMEBUFFER:
Geoff Lange8afa902017-09-27 15:00:43 -0400745 return (context->getExtensions().framebufferBlit ||
746 context->getClientMajorVersion() >= 3);
747
He Yunchaoced53ae2016-11-29 15:00:51 +0800748 default:
749 return false;
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500750 }
751}
752
Jamie Madill5b772312018-03-08 20:28:32 -0500753bool ValidMipLevel(const Context *context, TextureType type, GLint level)
Geoff Langce635692013-09-24 13:56:32 -0400754{
Jamie Madillc29968b2016-01-20 11:17:23 -0500755 const auto &caps = context->getCaps();
Geoff Langaae65a42014-05-26 12:43:44 -0400756 size_t maxDimension = 0;
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800757 switch (type)
Geoff Langce635692013-09-24 13:56:32 -0400758 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800759 case TextureType::_2D:
760 case TextureType::_2DArray:
761 case TextureType::_2DMultisample:
Jamie Madillc29968b2016-01-20 11:17:23 -0500762 maxDimension = caps.max2DTextureSize;
763 break;
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800764 case TextureType::CubeMap:
He Yunchaoced53ae2016-11-29 15:00:51 +0800765 maxDimension = caps.maxCubeMapTextureSize;
766 break;
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800767 case TextureType::Rectangle:
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400768 return level == 0;
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800769 case TextureType::_3D:
He Yunchaoced53ae2016-11-29 15:00:51 +0800770 maxDimension = caps.max3DTextureSize;
771 break;
He Yunchaoced53ae2016-11-29 15:00:51 +0800772 default:
773 UNREACHABLE();
Geoff Langce635692013-09-24 13:56:32 -0400774 }
775
Brandon Jones6cad5662017-06-14 13:25:13 -0700776 return level <= gl::log2(static_cast<int>(maxDimension)) && level >= 0;
Geoff Langce635692013-09-24 13:56:32 -0400777}
778
Jamie Madill5b772312018-03-08 20:28:32 -0500779bool ValidImageSizeParameters(Context *context,
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800780 TextureType target,
Austin Kinross08528e12015-10-07 16:24:40 -0700781 GLint level,
782 GLsizei width,
783 GLsizei height,
784 GLsizei depth,
785 bool isSubImage)
Geoff Langce635692013-09-24 13:56:32 -0400786{
Brandon Jones6cad5662017-06-14 13:25:13 -0700787 if (width < 0 || height < 0 || depth < 0)
Geoff Langce635692013-09-24 13:56:32 -0400788 {
Brandon Jones6cad5662017-06-14 13:25:13 -0700789 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeSize);
Geoff Langce635692013-09-24 13:56:32 -0400790 return false;
791 }
Austin Kinross08528e12015-10-07 16:24:40 -0700792 // TexSubImage parameters can be NPOT without textureNPOT extension,
793 // as long as the destination texture is POT.
Geoff Langcc507aa2016-12-12 10:09:52 -0500794 bool hasNPOTSupport =
Geoff Lang5f319a42017-01-09 16:49:19 -0500795 context->getExtensions().textureNPOT || context->getClientVersion() >= Version(3, 0);
Geoff Langcc507aa2016-12-12 10:09:52 -0500796 if (!isSubImage && !hasNPOTSupport &&
Jamie Madill4fd75c12014-06-23 10:53:54 -0400797 (level != 0 && (!gl::isPow2(width) || !gl::isPow2(height) || !gl::isPow2(depth))))
Geoff Langce635692013-09-24 13:56:32 -0400798 {
Brandon Jones6cad5662017-06-14 13:25:13 -0700799 ANGLE_VALIDATION_ERR(context, InvalidValue(), TextureNotPow2);
Geoff Langce635692013-09-24 13:56:32 -0400800 return false;
801 }
802
803 if (!ValidMipLevel(context, target, level))
804 {
Brandon Jones6cad5662017-06-14 13:25:13 -0700805 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidMipLevel);
Geoff Langce635692013-09-24 13:56:32 -0400806 return false;
807 }
808
809 return true;
810}
811
Geoff Lang966c9402017-04-18 12:38:27 -0400812bool ValidCompressedDimension(GLsizei size, GLuint blockSize, bool smallerThanBlockSizeAllowed)
813{
814 return (smallerThanBlockSizeAllowed && (size > 0) && (blockSize % size == 0)) ||
815 (size % blockSize == 0);
816}
817
Jamie Madill5b772312018-03-08 20:28:32 -0500818bool ValidCompressedImageSize(const Context *context,
Jamie Madillc29968b2016-01-20 11:17:23 -0500819 GLenum internalFormat,
Geoff Lang966c9402017-04-18 12:38:27 -0400820 GLint level,
Jamie Madillc29968b2016-01-20 11:17:23 -0500821 GLsizei width,
822 GLsizei height)
Geoff Langd4f180b2013-09-24 13:57:44 -0400823{
Geoff Langca271392017-04-05 12:30:00 -0400824 const gl::InternalFormat &formatInfo = gl::GetSizedInternalFormatInfo(internalFormat);
Geoff Lang5d601382014-07-22 15:14:06 -0400825 if (!formatInfo.compressed)
Geoff Langd4f180b2013-09-24 13:57:44 -0400826 {
827 return false;
828 }
829
Geoff Lang966c9402017-04-18 12:38:27 -0400830 if (width < 0 || height < 0)
831 {
832 return false;
833 }
834
835 if (CompressedTextureFormatRequiresExactSize(internalFormat))
836 {
837 // The ANGLE extensions allow specifying compressed textures with sizes smaller than the
838 // block size for level 0 but WebGL disallows this.
839 bool smallerThanBlockSizeAllowed =
840 level > 0 || !context->getExtensions().webglCompatibility;
841
842 if (!ValidCompressedDimension(width, formatInfo.compressedBlockWidth,
843 smallerThanBlockSizeAllowed) ||
844 !ValidCompressedDimension(height, formatInfo.compressedBlockHeight,
845 smallerThanBlockSizeAllowed))
846 {
847 return false;
848 }
849 }
850
851 return true;
852}
853
Jamie Madill5b772312018-03-08 20:28:32 -0500854bool ValidCompressedSubImageSize(const Context *context,
Geoff Lang966c9402017-04-18 12:38:27 -0400855 GLenum internalFormat,
856 GLint xoffset,
857 GLint yoffset,
858 GLsizei width,
859 GLsizei height,
860 size_t textureWidth,
861 size_t textureHeight)
862{
863 const gl::InternalFormat &formatInfo = gl::GetSizedInternalFormatInfo(internalFormat);
864 if (!formatInfo.compressed)
865 {
866 return false;
867 }
868
Geoff Lang44ff5a72017-02-03 15:15:43 -0500869 if (xoffset < 0 || yoffset < 0 || width < 0 || height < 0)
Geoff Langd4f180b2013-09-24 13:57:44 -0400870 {
871 return false;
872 }
873
Luc Ferron9dbaeba2018-02-01 07:26:59 -0500874 if (CompressedSubTextureFormatRequiresExactSize(internalFormat))
Geoff Lang0d8b7242015-09-09 14:56:53 -0400875 {
Geoff Lang44ff5a72017-02-03 15:15:43 -0500876 if (xoffset % formatInfo.compressedBlockWidth != 0 ||
Geoff Lang966c9402017-04-18 12:38:27 -0400877 yoffset % formatInfo.compressedBlockHeight != 0)
878 {
879 return false;
880 }
881
882 // Allowed to either have data that is a multiple of block size or is smaller than the block
883 // size but fills the entire mip
884 bool fillsEntireMip = xoffset == 0 && yoffset == 0 &&
885 static_cast<size_t>(width) == textureWidth &&
886 static_cast<size_t>(height) == textureHeight;
887 bool sizeMultipleOfBlockSize = (width % formatInfo.compressedBlockWidth) == 0 &&
888 (height % formatInfo.compressedBlockHeight) == 0;
889 if (!sizeMultipleOfBlockSize && !fillsEntireMip)
Geoff Lang0d8b7242015-09-09 14:56:53 -0400890 {
891 return false;
892 }
893 }
894
Geoff Langd4f180b2013-09-24 13:57:44 -0400895 return true;
896}
897
Jamie Madill5b772312018-03-08 20:28:32 -0500898bool ValidImageDataSize(Context *context,
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800899 TextureType texType,
Geoff Langff5b2d52016-09-07 11:32:23 -0400900 GLsizei width,
901 GLsizei height,
902 GLsizei depth,
Geoff Langdbcced82017-06-06 15:55:54 -0400903 GLenum format,
Geoff Langff5b2d52016-09-07 11:32:23 -0400904 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -0400905 const void *pixels,
Geoff Langff5b2d52016-09-07 11:32:23 -0400906 GLsizei imageSize)
907{
Corentin Wallez336129f2017-10-17 15:55:40 -0400908 gl::Buffer *pixelUnpackBuffer =
909 context->getGLState().getTargetBuffer(BufferBinding::PixelUnpack);
Geoff Langff5b2d52016-09-07 11:32:23 -0400910 if (pixelUnpackBuffer == nullptr && imageSize < 0)
911 {
912 // Checks are not required
913 return true;
914 }
915
916 // ...the data would be unpacked from the buffer object such that the memory reads required
917 // would exceed the data store size.
Geoff Langdbcced82017-06-06 15:55:54 -0400918 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(format, type);
919 ASSERT(formatInfo.internalFormat != GL_NONE);
Geoff Langff5b2d52016-09-07 11:32:23 -0400920 const gl::Extents size(width, height, depth);
921 const auto &unpack = context->getGLState().getUnpackState();
922
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800923 bool targetIs3D = texType == TextureType::_3D || texType == TextureType::_2DArray;
Jamie Madillca2ff382018-07-11 09:01:17 -0400924 GLuint endByte = 0;
925 if (!formatInfo.computePackUnpackEndByte(type, size, unpack, targetIs3D, &endByte))
Geoff Langff5b2d52016-09-07 11:32:23 -0400926 {
Jamie Madillca2ff382018-07-11 09:01:17 -0400927 ANGLE_VALIDATION_ERR(context, InvalidOperation(), IntegerOverflow);
Geoff Langff5b2d52016-09-07 11:32:23 -0400928 return false;
929 }
930
Geoff Langff5b2d52016-09-07 11:32:23 -0400931 if (pixelUnpackBuffer)
932 {
Jamie Madillca2ff382018-07-11 09:01:17 -0400933 CheckedNumeric<size_t> checkedEndByte(endByte);
Geoff Langff5b2d52016-09-07 11:32:23 -0400934 CheckedNumeric<size_t> checkedOffset(reinterpret_cast<size_t>(pixels));
935 checkedEndByte += checkedOffset;
936
937 if (!checkedEndByte.IsValid() ||
938 (checkedEndByte.ValueOrDie() > static_cast<size_t>(pixelUnpackBuffer->getSize())))
939 {
940 // Overflow past the end of the buffer
Jamie Madillca2ff382018-07-11 09:01:17 -0400941 ANGLE_VALIDATION_ERR(context, InvalidOperation(), IntegerOverflow);
Geoff Langff5b2d52016-09-07 11:32:23 -0400942 return false;
943 }
James Darpiniane8a93c62018-01-04 18:02:24 -0800944 if (context->getExtensions().webglCompatibility &&
945 pixelUnpackBuffer->isBoundForTransformFeedbackAndOtherUse())
946 {
947 ANGLE_VALIDATION_ERR(context, InvalidOperation(),
948 PixelUnpackBufferBoundForTransformFeedback);
949 return false;
950 }
Geoff Langff5b2d52016-09-07 11:32:23 -0400951 }
952 else
953 {
954 ASSERT(imageSize >= 0);
955 if (pixels == nullptr && imageSize != 0)
956 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500957 context->handleError(InvalidOperation()
958 << "imageSize must be 0 if no texture data is provided.");
Geoff Lang3feb3ff2016-10-26 10:57:45 -0400959 return false;
Geoff Langff5b2d52016-09-07 11:32:23 -0400960 }
961
Geoff Lang3feb3ff2016-10-26 10:57:45 -0400962 if (pixels != nullptr && endByte > static_cast<GLuint>(imageSize))
Geoff Langff5b2d52016-09-07 11:32:23 -0400963 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500964 context->handleError(InvalidOperation() << "imageSize must be at least " << endByte);
Geoff Langff5b2d52016-09-07 11:32:23 -0400965 return false;
966 }
967 }
968
969 return true;
970}
971
Corentin Wallezad3ae902018-03-09 13:40:42 -0500972bool ValidQueryType(const Context *context, QueryType queryType)
Geoff Lang37dde692014-01-31 16:34:54 -0500973{
Geoff Lang37dde692014-01-31 16:34:54 -0500974 switch (queryType)
975 {
Corentin Wallezad3ae902018-03-09 13:40:42 -0500976 case QueryType::AnySamples:
977 case QueryType::AnySamplesConservative:
Geoff Lang8c5b31c2017-09-26 18:07:44 -0400978 return context->getClientMajorVersion() >= 3 ||
979 context->getExtensions().occlusionQueryBoolean;
Corentin Wallezad3ae902018-03-09 13:40:42 -0500980 case QueryType::TransformFeedbackPrimitivesWritten:
He Yunchaoced53ae2016-11-29 15:00:51 +0800981 return (context->getClientMajorVersion() >= 3);
Corentin Wallezad3ae902018-03-09 13:40:42 -0500982 case QueryType::TimeElapsed:
He Yunchaoced53ae2016-11-29 15:00:51 +0800983 return context->getExtensions().disjointTimerQuery;
Corentin Wallezad3ae902018-03-09 13:40:42 -0500984 case QueryType::CommandsCompleted:
He Yunchaoced53ae2016-11-29 15:00:51 +0800985 return context->getExtensions().syncQuery;
Corentin Wallezad3ae902018-03-09 13:40:42 -0500986 case QueryType::PrimitivesGenerated:
Jiawei Shaod2fa07e2018-03-15 09:20:25 +0800987 return context->getExtensions().geometryShader;
He Yunchaoced53ae2016-11-29 15:00:51 +0800988 default:
989 return false;
Geoff Lang37dde692014-01-31 16:34:54 -0500990 }
991}
992
Jamie Madill5b772312018-03-08 20:28:32 -0500993bool ValidateWebGLVertexAttribPointer(Context *context,
Geoff Lang2d62ab72017-03-23 16:54:40 -0400994 GLenum type,
995 GLboolean normalized,
996 GLsizei stride,
Jamie Madill876429b2017-04-20 15:46:24 -0400997 const void *ptr,
Geoff Lang2d62ab72017-03-23 16:54:40 -0400998 bool pureInteger)
999{
1000 ASSERT(context->getExtensions().webglCompatibility);
Geoff Lang2d62ab72017-03-23 16:54:40 -04001001 // WebGL 1.0 [Section 6.11] Vertex Attribute Data Stride
1002 // The WebGL API supports vertex attribute data strides up to 255 bytes. A call to
1003 // vertexAttribPointer will generate an INVALID_VALUE error if the value for the stride
1004 // parameter exceeds 255.
1005 constexpr GLsizei kMaxWebGLStride = 255;
1006 if (stride > kMaxWebGLStride)
1007 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001008 context->handleError(InvalidValue()
1009 << "Stride is over the maximum stride allowed by WebGL.");
Geoff Lang2d62ab72017-03-23 16:54:40 -04001010 return false;
1011 }
1012
1013 // WebGL 1.0 [Section 6.4] Buffer Offset and Stride Requirements
1014 // The offset arguments to drawElements and vertexAttribPointer, and the stride argument to
1015 // vertexAttribPointer, must be a multiple of the size of the data type passed to the call,
1016 // or an INVALID_OPERATION error is generated.
1017 VertexFormatType internalType = GetVertexFormatType(type, normalized, 1, pureInteger);
1018 size_t typeSize = GetVertexFormatTypeSize(internalType);
1019
1020 ASSERT(isPow2(typeSize) && typeSize > 0);
1021 size_t sizeMask = (typeSize - 1);
1022 if ((reinterpret_cast<intptr_t>(ptr) & sizeMask) != 0)
1023 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001024 ANGLE_VALIDATION_ERR(context, InvalidOperation(), OffsetMustBeMultipleOfType);
Geoff Lang2d62ab72017-03-23 16:54:40 -04001025 return false;
1026 }
1027
1028 if ((stride & sizeMask) != 0)
1029 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001030 ANGLE_VALIDATION_ERR(context, InvalidOperation(), StrideMustBeMultipleOfType);
Geoff Lang2d62ab72017-03-23 16:54:40 -04001031 return false;
1032 }
1033
1034 return true;
1035}
1036
Jamie Madill5b772312018-03-08 20:28:32 -05001037Program *GetValidProgram(Context *context, GLuint id)
Geoff Lang48dcae72014-02-05 16:28:24 -05001038{
He Yunchaoced53ae2016-11-29 15:00:51 +08001039 // ES3 spec (section 2.11.1) -- "Commands that accept shader or program object names will
1040 // generate the error INVALID_VALUE if the provided name is not the name of either a shader
1041 // or program object and INVALID_OPERATION if the provided name identifies an object
1042 // that is not the expected type."
Geoff Lang48dcae72014-02-05 16:28:24 -05001043
Dian Xiang769769a2015-09-09 15:20:08 -07001044 Program *validProgram = context->getProgram(id);
1045
1046 if (!validProgram)
Geoff Lang48dcae72014-02-05 16:28:24 -05001047 {
Dian Xiang769769a2015-09-09 15:20:08 -07001048 if (context->getShader(id))
1049 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001050 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExpectedProgramName);
Dian Xiang769769a2015-09-09 15:20:08 -07001051 }
1052 else
1053 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001054 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidProgramName);
Dian Xiang769769a2015-09-09 15:20:08 -07001055 }
Geoff Lang48dcae72014-02-05 16:28:24 -05001056 }
Dian Xiang769769a2015-09-09 15:20:08 -07001057
1058 return validProgram;
1059}
1060
Jamie Madill5b772312018-03-08 20:28:32 -05001061Shader *GetValidShader(Context *context, GLuint id)
Dian Xiang769769a2015-09-09 15:20:08 -07001062{
1063 // See ValidProgram for spec details.
1064
1065 Shader *validShader = context->getShader(id);
1066
1067 if (!validShader)
Geoff Lang48dcae72014-02-05 16:28:24 -05001068 {
Dian Xiang769769a2015-09-09 15:20:08 -07001069 if (context->getProgram(id))
1070 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001071 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExpectedShaderName);
Dian Xiang769769a2015-09-09 15:20:08 -07001072 }
1073 else
1074 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001075 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidShaderName);
Dian Xiang769769a2015-09-09 15:20:08 -07001076 }
Geoff Lang48dcae72014-02-05 16:28:24 -05001077 }
Dian Xiang769769a2015-09-09 15:20:08 -07001078
1079 return validShader;
Geoff Lang48dcae72014-02-05 16:28:24 -05001080}
1081
Geoff Langb1196682014-07-23 13:47:29 -04001082bool ValidateAttachmentTarget(gl::Context *context, GLenum attachment)
Jamie Madillb4472272014-07-03 10:38:55 -04001083{
Geoff Langfa125c92017-10-24 13:01:46 -04001084 if (attachment >= GL_COLOR_ATTACHMENT1_EXT && attachment <= GL_COLOR_ATTACHMENT15_EXT)
Jamie Madillb4472272014-07-03 10:38:55 -04001085 {
Geoff Langfa125c92017-10-24 13:01:46 -04001086 if (context->getClientMajorVersion() < 3 && !context->getExtensions().drawBuffers)
1087 {
1088 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidAttachment);
1089 return false;
1090 }
Jamie Madillb4472272014-07-03 10:38:55 -04001091
Geoff Langfa125c92017-10-24 13:01:46 -04001092 // Color attachment 0 is validated below because it is always valid
1093 const unsigned int colorAttachment = (attachment - GL_COLOR_ATTACHMENT0_EXT);
Geoff Langaae65a42014-05-26 12:43:44 -04001094 if (colorAttachment >= context->getCaps().maxColorAttachments)
Jamie Madillb4472272014-07-03 10:38:55 -04001095 {
Geoff Langfa125c92017-10-24 13:01:46 -04001096 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidAttachment);
Geoff Langb1196682014-07-23 13:47:29 -04001097 return false;
Jamie Madillb4472272014-07-03 10:38:55 -04001098 }
1099 }
1100 else
1101 {
1102 switch (attachment)
1103 {
Geoff Langfa125c92017-10-24 13:01:46 -04001104 case GL_COLOR_ATTACHMENT0:
He Yunchaoced53ae2016-11-29 15:00:51 +08001105 case GL_DEPTH_ATTACHMENT:
1106 case GL_STENCIL_ATTACHMENT:
1107 break;
Jamie Madillb4472272014-07-03 10:38:55 -04001108
He Yunchaoced53ae2016-11-29 15:00:51 +08001109 case GL_DEPTH_STENCIL_ATTACHMENT:
1110 if (!context->getExtensions().webglCompatibility &&
1111 context->getClientMajorVersion() < 3)
1112 {
Geoff Langfa125c92017-10-24 13:01:46 -04001113 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidAttachment);
He Yunchaoced53ae2016-11-29 15:00:51 +08001114 return false;
1115 }
1116 break;
Jamie Madillb4472272014-07-03 10:38:55 -04001117
He Yunchaoced53ae2016-11-29 15:00:51 +08001118 default:
Geoff Langfa125c92017-10-24 13:01:46 -04001119 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidAttachment);
He Yunchaoced53ae2016-11-29 15:00:51 +08001120 return false;
Jamie Madillb4472272014-07-03 10:38:55 -04001121 }
1122 }
1123
1124 return true;
1125}
1126
Jamie Madill5b772312018-03-08 20:28:32 -05001127bool ValidateRenderbufferStorageParametersBase(Context *context,
He Yunchaoced53ae2016-11-29 15:00:51 +08001128 GLenum target,
1129 GLsizei samples,
1130 GLenum internalformat,
1131 GLsizei width,
1132 GLsizei height)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001133{
1134 switch (target)
1135 {
He Yunchaoced53ae2016-11-29 15:00:51 +08001136 case GL_RENDERBUFFER:
1137 break;
1138 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07001139 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidRenderbufferTarget);
He Yunchaoced53ae2016-11-29 15:00:51 +08001140 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001141 }
1142
1143 if (width < 0 || height < 0 || samples < 0)
1144 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001145 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidRenderbufferWidthHeight);
Geoff Langb1196682014-07-23 13:47:29 -04001146 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001147 }
1148
Jamie Madill4e0e6f82017-02-17 11:06:03 -05001149 // Hack for the special WebGL 1 "DEPTH_STENCIL" internal format.
1150 GLenum convertedInternalFormat = context->getConvertedRenderbufferFormat(internalformat);
1151
1152 const TextureCaps &formatCaps = context->getTextureCaps().get(convertedInternalFormat);
Yuly Novikovf15f8862018-06-04 18:59:41 -04001153 if (!formatCaps.renderbuffer)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001154 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001155 context->handleError(InvalidEnum());
Geoff Langb1196682014-07-23 13:47:29 -04001156 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001157 }
1158
1159 // ANGLE_framebuffer_multisample does not explicitly state that the internal format must be
1160 // sized but it does state that the format must be in the ES2.0 spec table 4.5 which contains
Corentin Walleze0902642014-11-04 12:32:15 -08001161 // only sized internal formats.
Geoff Langca271392017-04-05 12:30:00 -04001162 const gl::InternalFormat &formatInfo = gl::GetSizedInternalFormatInfo(convertedInternalFormat);
1163 if (formatInfo.internalFormat == GL_NONE)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001164 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001165 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidRenderbufferInternalFormat);
Geoff Langb1196682014-07-23 13:47:29 -04001166 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001167 }
1168
Geoff Langaae65a42014-05-26 12:43:44 -04001169 if (static_cast<GLuint>(std::max(width, height)) > context->getCaps().maxRenderbufferSize)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001170 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001171 context->handleError(InvalidValue());
Geoff Langb1196682014-07-23 13:47:29 -04001172 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001173 }
1174
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001175 GLuint handle = context->getGLState().getRenderbufferId();
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001176 if (handle == 0)
1177 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001178 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidRenderbufferTarget);
Geoff Langb1196682014-07-23 13:47:29 -04001179 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001180 }
1181
1182 return true;
1183}
1184
He Yunchaoced53ae2016-11-29 15:00:51 +08001185bool ValidateFramebufferRenderbufferParameters(gl::Context *context,
1186 GLenum target,
1187 GLenum attachment,
1188 GLenum renderbuffertarget,
1189 GLuint renderbuffer)
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001190{
Geoff Lange8afa902017-09-27 15:00:43 -04001191 if (!ValidFramebufferTarget(context, target))
Shannon Woods1da3cf62014-06-27 15:32:23 -04001192 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001193 context->handleError(InvalidEnum());
Geoff Langb1196682014-07-23 13:47:29 -04001194 return false;
Shannon Woods1da3cf62014-06-27 15:32:23 -04001195 }
1196
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001197 gl::Framebuffer *framebuffer = context->getGLState().getTargetFramebuffer(target);
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001198
Jamie Madill84115c92015-04-23 15:00:07 -04001199 ASSERT(framebuffer);
1200 if (framebuffer->id() == 0)
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001201 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001202 ANGLE_VALIDATION_ERR(context, InvalidOperation(), DefaultFramebufferTarget);
Geoff Langb1196682014-07-23 13:47:29 -04001203 return false;
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001204 }
1205
Jamie Madillb4472272014-07-03 10:38:55 -04001206 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001207 {
Jamie Madillb4472272014-07-03 10:38:55 -04001208 return false;
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001209 }
1210
Jamie Madillab9d82c2014-01-21 16:38:14 -05001211 // [OpenGL ES 2.0.25] Section 4.4.3 page 112
1212 // [OpenGL ES 3.0.2] Section 4.4.2 page 201
1213 // 'renderbuffer' must be either zero or the name of an existing renderbuffer object of
1214 // type 'renderbuffertarget', otherwise an INVALID_OPERATION error is generated.
1215 if (renderbuffer != 0)
1216 {
1217 if (!context->getRenderbuffer(renderbuffer))
1218 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001219 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidRenderbufferTarget);
Geoff Langb1196682014-07-23 13:47:29 -04001220 return false;
Jamie Madillab9d82c2014-01-21 16:38:14 -05001221 }
1222 }
1223
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001224 return true;
1225}
1226
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -08001227bool ValidateBlitFramebufferParameters(Context *context,
Jamie Madillc29968b2016-01-20 11:17:23 -05001228 GLint srcX0,
1229 GLint srcY0,
1230 GLint srcX1,
1231 GLint srcY1,
1232 GLint dstX0,
1233 GLint dstY0,
1234 GLint dstX1,
1235 GLint dstY1,
1236 GLbitfield mask,
1237 GLenum filter)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001238{
1239 switch (filter)
1240 {
He Yunchaoced53ae2016-11-29 15:00:51 +08001241 case GL_NEAREST:
1242 break;
1243 case GL_LINEAR:
1244 break;
1245 default:
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001246 context->handleError(InvalidEnum());
He Yunchaoced53ae2016-11-29 15:00:51 +08001247 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001248 }
1249
1250 if ((mask & ~(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)) != 0)
1251 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001252 context->handleError(InvalidValue());
Geoff Langb1196682014-07-23 13:47:29 -04001253 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001254 }
1255
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001256 // ES3.0 spec, section 4.3.2 states that linear filtering is only available for the
1257 // color buffer, leaving only nearest being unfiltered from above
1258 if ((mask & ~GL_COLOR_BUFFER_BIT) != 0 && filter != GL_NEAREST)
1259 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001260 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001261 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001262 }
1263
Jamie Madill51f40ec2016-06-15 14:06:00 -04001264 const auto &glState = context->getGLState();
1265 gl::Framebuffer *readFramebuffer = glState.getReadFramebuffer();
1266 gl::Framebuffer *drawFramebuffer = glState.getDrawFramebuffer();
Jamie Madill48faf802014-11-06 15:27:22 -05001267
1268 if (!readFramebuffer || !drawFramebuffer)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001269 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001270 context->handleError(InvalidFramebufferOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001271 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001272 }
1273
Jamie Madill427064d2018-04-13 16:20:34 -04001274 if (!ValidateFramebufferComplete(context, readFramebuffer))
Jamie Madill48faf802014-11-06 15:27:22 -05001275 {
Jamie Madill48faf802014-11-06 15:27:22 -05001276 return false;
1277 }
1278
Jamie Madill427064d2018-04-13 16:20:34 -04001279 if (!ValidateFramebufferComplete(context, drawFramebuffer))
Jamie Madill48faf802014-11-06 15:27:22 -05001280 {
Jamie Madill48faf802014-11-06 15:27:22 -05001281 return false;
1282 }
1283
Qin Jiajiaaef92162018-02-27 13:51:44 +08001284 if (readFramebuffer->id() == drawFramebuffer->id())
1285 {
1286 context->handleError(InvalidOperation());
1287 return false;
1288 }
1289
Jamie Madille98b1b52018-03-08 09:47:23 -05001290 if (!ValidateFramebufferNotMultisampled(context, drawFramebuffer))
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001291 {
Geoff Langb1196682014-07-23 13:47:29 -04001292 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001293 }
1294
Olli Etuaho9aef81c2018-04-30 14:56:15 +03001295 // This validation is specified in the WebGL 2.0 spec and not in the GLES 3.0.5 spec, but we
1296 // always run it in order to avoid triggering driver bugs.
1297 if (DifferenceCanOverflow(srcX0, srcX1) || DifferenceCanOverflow(srcY0, srcY1) ||
1298 DifferenceCanOverflow(dstX0, dstX1) || DifferenceCanOverflow(dstY0, dstY1))
Olli Etuaho8d5571a2018-04-23 12:29:31 +03001299 {
Olli Etuaho9aef81c2018-04-30 14:56:15 +03001300 ANGLE_VALIDATION_ERR(context, InvalidValue(), BlitDimensionsOutOfRange);
1301 return false;
Olli Etuaho8d5571a2018-04-23 12:29:31 +03001302 }
1303
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001304 bool sameBounds = srcX0 == dstX0 && srcY0 == dstY0 && srcX1 == dstX1 && srcY1 == dstY1;
1305
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001306 if (mask & GL_COLOR_BUFFER_BIT)
1307 {
Jamie Madillb6bda4a2015-04-20 12:53:26 -04001308 const gl::FramebufferAttachment *readColorBuffer = readFramebuffer->getReadColorbuffer();
Jamie Madill6163c752015-12-07 16:32:59 -05001309 const Extensions &extensions = context->getExtensions();
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001310
He Yunchao66a41a22016-12-15 16:45:05 +08001311 if (readColorBuffer)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001312 {
Jamie Madilla3944d42016-07-22 22:13:26 -04001313 const Format &readFormat = readColorBuffer->getFormat();
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001314
Geoff Langa15472a2015-08-11 11:48:03 -04001315 for (size_t drawbufferIdx = 0;
1316 drawbufferIdx < drawFramebuffer->getDrawbufferStateCount(); ++drawbufferIdx)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001317 {
Geoff Langa15472a2015-08-11 11:48:03 -04001318 const FramebufferAttachment *attachment =
1319 drawFramebuffer->getDrawBuffer(drawbufferIdx);
1320 if (attachment)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001321 {
Jamie Madilla3944d42016-07-22 22:13:26 -04001322 const Format &drawFormat = attachment->getFormat();
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001323
Geoff Langb2f3d052013-08-13 12:49:27 -04001324 // The GL ES 3.0.2 spec (pg 193) states that:
1325 // 1) If the read buffer is fixed point format, the draw buffer must be as well
He Yunchaoced53ae2016-11-29 15:00:51 +08001326 // 2) If the read buffer is an unsigned integer format, the draw buffer must be
1327 // as well
1328 // 3) If the read buffer is a signed integer format, the draw buffer must be as
1329 // well
Jamie Madill6163c752015-12-07 16:32:59 -05001330 // Changes with EXT_color_buffer_float:
1331 // Case 1) is changed to fixed point OR floating point
Jamie Madilla3944d42016-07-22 22:13:26 -04001332 GLenum readComponentType = readFormat.info->componentType;
1333 GLenum drawComponentType = drawFormat.info->componentType;
He Yunchaoced53ae2016-11-29 15:00:51 +08001334 bool readFixedPoint = (readComponentType == GL_UNSIGNED_NORMALIZED ||
Jamie Madill6163c752015-12-07 16:32:59 -05001335 readComponentType == GL_SIGNED_NORMALIZED);
Lingfeng Yang038dd532018-03-29 17:31:52 -07001336 bool drawFixedPoint = (drawComponentType == GL_UNSIGNED_NORMALIZED ||
Jamie Madill6163c752015-12-07 16:32:59 -05001337 drawComponentType == GL_SIGNED_NORMALIZED);
1338
1339 if (extensions.colorBufferFloat)
1340 {
1341 bool readFixedOrFloat = (readFixedPoint || readComponentType == GL_FLOAT);
1342 bool drawFixedOrFloat = (drawFixedPoint || drawComponentType == GL_FLOAT);
1343
1344 if (readFixedOrFloat != drawFixedOrFloat)
1345 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001346 context->handleError(InvalidOperation()
1347 << "If the read buffer contains fixed-point or "
1348 "floating-point values, the draw buffer must "
1349 "as well.");
Jamie Madill6163c752015-12-07 16:32:59 -05001350 return false;
1351 }
1352 }
1353 else if (readFixedPoint != drawFixedPoint)
1354 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001355 context->handleError(InvalidOperation()
1356 << "If the read buffer contains fixed-point values, "
1357 "the draw buffer must as well.");
Jamie Madill6163c752015-12-07 16:32:59 -05001358 return false;
1359 }
1360
1361 if (readComponentType == GL_UNSIGNED_INT &&
1362 drawComponentType != GL_UNSIGNED_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001363 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001364 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001365 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001366 }
1367
Jamie Madill6163c752015-12-07 16:32:59 -05001368 if (readComponentType == GL_INT && drawComponentType != GL_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001369 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001370 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001371 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001372 }
1373
Jamie Madilla3944d42016-07-22 22:13:26 -04001374 if (readColorBuffer->getSamples() > 0 &&
Kenneth Russell69382852017-07-21 16:38:44 -04001375 (!Format::EquivalentForBlit(readFormat, drawFormat) || !sameBounds))
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001376 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001377 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001378 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001379 }
Geoff Lange4915782017-04-12 15:19:07 -04001380
1381 if (context->getExtensions().webglCompatibility &&
1382 *readColorBuffer == *attachment)
1383 {
1384 context->handleError(
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001385 InvalidOperation()
1386 << "Read and write color attachments cannot be the same image.");
Geoff Lange4915782017-04-12 15:19:07 -04001387 return false;
1388 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001389 }
1390 }
1391
Jamie Madilla3944d42016-07-22 22:13:26 -04001392 if ((readFormat.info->componentType == GL_INT ||
1393 readFormat.info->componentType == GL_UNSIGNED_INT) &&
1394 filter == GL_LINEAR)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001395 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001396 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001397 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001398 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001399 }
He Yunchao66a41a22016-12-15 16:45:05 +08001400 // WebGL 2.0 BlitFramebuffer when blitting from a missing attachment
1401 // In OpenGL ES it is undefined what happens when an operation tries to blit from a missing
1402 // attachment and WebGL defines it to be an error. We do the check unconditionally as the
1403 // situation is an application error that would lead to a crash in ANGLE.
1404 else if (drawFramebuffer->hasEnabledDrawBuffer())
1405 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001406 context->handleError(
1407 InvalidOperation()
1408 << "Attempt to read from a missing color attachment of a complete framebuffer.");
He Yunchao66a41a22016-12-15 16:45:05 +08001409 return false;
1410 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001411 }
1412
He Yunchaoced53ae2016-11-29 15:00:51 +08001413 GLenum masks[] = {GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT};
Dongseong Hwang44b422c2014-12-09 15:42:01 +02001414 GLenum attachments[] = {GL_DEPTH_ATTACHMENT, GL_STENCIL_ATTACHMENT};
1415 for (size_t i = 0; i < 2; i++)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001416 {
Dongseong Hwang44b422c2014-12-09 15:42:01 +02001417 if (mask & masks[i])
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001418 {
He Yunchaoced53ae2016-11-29 15:00:51 +08001419 const gl::FramebufferAttachment *readBuffer =
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -08001420 readFramebuffer->getAttachment(context, attachments[i]);
He Yunchaoced53ae2016-11-29 15:00:51 +08001421 const gl::FramebufferAttachment *drawBuffer =
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -08001422 drawFramebuffer->getAttachment(context, attachments[i]);
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001423
Dongseong Hwang44b422c2014-12-09 15:42:01 +02001424 if (readBuffer && drawBuffer)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001425 {
Kenneth Russell69382852017-07-21 16:38:44 -04001426 if (!Format::EquivalentForBlit(readBuffer->getFormat(), drawBuffer->getFormat()))
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001427 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001428 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001429 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001430 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001431
Dongseong Hwang44b422c2014-12-09 15:42:01 +02001432 if (readBuffer->getSamples() > 0 && !sameBounds)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001433 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001434 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001435 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001436 }
Geoff Lange4915782017-04-12 15:19:07 -04001437
1438 if (context->getExtensions().webglCompatibility && *readBuffer == *drawBuffer)
1439 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001440 context->handleError(
1441 InvalidOperation()
1442 << "Read and write depth stencil attachments cannot be the same image.");
Geoff Lange4915782017-04-12 15:19:07 -04001443 return false;
1444 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001445 }
He Yunchao66a41a22016-12-15 16:45:05 +08001446 // WebGL 2.0 BlitFramebuffer when blitting from a missing attachment
1447 else if (drawBuffer)
1448 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001449 context->handleError(InvalidOperation() << "Attempt to read from a missing "
1450 "depth/stencil attachment of a "
1451 "complete framebuffer.");
He Yunchao66a41a22016-12-15 16:45:05 +08001452 return false;
1453 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001454 }
1455 }
1456
Martin Radeva3ed4572017-07-27 18:29:37 +03001457 // ANGLE_multiview, Revision 1:
1458 // Calling BlitFramebuffer will result in an INVALID_FRAMEBUFFER_OPERATION error if the
Olli Etuaho8acb1b62018-07-30 16:20:54 +03001459 // multi-view layout of the current draw framebuffer is not NONE, or if the multi-view layout of
1460 // the current read framebuffer is FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE or the number of
1461 // views in the current read framebuffer is more than one.
1462 if (readFramebuffer->readDisallowedByMultiview())
Martin Radeva3ed4572017-07-27 18:29:37 +03001463 {
1464 context->handleError(InvalidFramebufferOperation()
1465 << "Attempt to read from a multi-view framebuffer.");
1466 return false;
1467 }
1468 if (drawFramebuffer->getMultiviewLayout() != GL_NONE)
1469 {
1470 context->handleError(InvalidFramebufferOperation()
1471 << "Attempt to write to a multi-view framebuffer.");
1472 return false;
1473 }
1474
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001475 return true;
1476}
1477
Jamie Madill4928b7c2017-06-20 12:57:39 -04001478bool ValidateReadPixelsRobustANGLE(Context *context,
Geoff Lang62fce5b2016-09-30 10:46:35 -04001479 GLint x,
1480 GLint y,
1481 GLsizei width,
1482 GLsizei height,
1483 GLenum format,
1484 GLenum type,
1485 GLsizei bufSize,
1486 GLsizei *length,
Geoff Lange93daba2017-03-30 13:54:40 -04001487 GLsizei *columns,
1488 GLsizei *rows,
Jamie Madill876429b2017-04-20 15:46:24 -04001489 void *pixels)
Geoff Lang62fce5b2016-09-30 10:46:35 -04001490{
1491 if (!ValidateRobustEntryPoint(context, bufSize))
Jamie Madillc29968b2016-01-20 11:17:23 -05001492 {
Jamie Madillc29968b2016-01-20 11:17:23 -05001493 return false;
1494 }
1495
Brandon Jonesd1049182018-03-28 10:02:20 -07001496 GLsizei writeLength = 0;
1497 GLsizei writeColumns = 0;
1498 GLsizei writeRows = 0;
1499
1500 if (!ValidateReadPixelsBase(context, x, y, width, height, format, type, bufSize, &writeLength,
1501 &writeColumns, &writeRows, pixels))
Jamie Madill26e91952014-03-05 15:01:27 -05001502 {
Geoff Langb1196682014-07-23 13:47:29 -04001503 return false;
Jamie Madill26e91952014-03-05 15:01:27 -05001504 }
1505
Brandon Jonesd1049182018-03-28 10:02:20 -07001506 if (!ValidateRobustBufferSize(context, bufSize, writeLength))
Jamie Madill26e91952014-03-05 15:01:27 -05001507 {
Geoff Langb1196682014-07-23 13:47:29 -04001508 return false;
Jamie Madill26e91952014-03-05 15:01:27 -05001509 }
1510
Brandon Jonesd1049182018-03-28 10:02:20 -07001511 SetRobustLengthParam(length, writeLength);
1512 SetRobustLengthParam(columns, writeColumns);
1513 SetRobustLengthParam(rows, writeRows);
1514
Jamie Madillc29968b2016-01-20 11:17:23 -05001515 return true;
1516}
1517
1518bool ValidateReadnPixelsEXT(Context *context,
1519 GLint x,
1520 GLint y,
1521 GLsizei width,
1522 GLsizei height,
1523 GLenum format,
1524 GLenum type,
1525 GLsizei bufSize,
Jamie Madill876429b2017-04-20 15:46:24 -04001526 void *pixels)
Jamie Madillc29968b2016-01-20 11:17:23 -05001527{
1528 if (bufSize < 0)
1529 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001530 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeBufferSize);
Jamie Madillc29968b2016-01-20 11:17:23 -05001531 return false;
1532 }
1533
Geoff Lang62fce5b2016-09-30 10:46:35 -04001534 return ValidateReadPixelsBase(context, x, y, width, height, format, type, bufSize, nullptr,
Geoff Lange93daba2017-03-30 13:54:40 -04001535 nullptr, nullptr, pixels);
Geoff Lang62fce5b2016-09-30 10:46:35 -04001536}
Jamie Madill26e91952014-03-05 15:01:27 -05001537
Jamie Madill4928b7c2017-06-20 12:57:39 -04001538bool ValidateReadnPixelsRobustANGLE(Context *context,
Geoff Lang62fce5b2016-09-30 10:46:35 -04001539 GLint x,
1540 GLint y,
1541 GLsizei width,
1542 GLsizei height,
1543 GLenum format,
1544 GLenum type,
1545 GLsizei bufSize,
1546 GLsizei *length,
Geoff Lange93daba2017-03-30 13:54:40 -04001547 GLsizei *columns,
1548 GLsizei *rows,
Jamie Madill876429b2017-04-20 15:46:24 -04001549 void *data)
Geoff Lang62fce5b2016-09-30 10:46:35 -04001550{
Brandon Jonesd1049182018-03-28 10:02:20 -07001551 GLsizei writeLength = 0;
1552 GLsizei writeColumns = 0;
1553 GLsizei writeRows = 0;
1554
Geoff Lang62fce5b2016-09-30 10:46:35 -04001555 if (!ValidateRobustEntryPoint(context, bufSize))
Jamie Madille2e406c2016-06-02 13:04:10 -04001556 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001557 return false;
1558 }
1559
Brandon Jonesd1049182018-03-28 10:02:20 -07001560 if (!ValidateReadPixelsBase(context, x, y, width, height, format, type, bufSize, &writeLength,
1561 &writeColumns, &writeRows, data))
Jamie Madille2e406c2016-06-02 13:04:10 -04001562 {
Jamie Madillc29968b2016-01-20 11:17:23 -05001563 return false;
Jamie Madill26e91952014-03-05 15:01:27 -05001564 }
1565
Brandon Jonesd1049182018-03-28 10:02:20 -07001566 if (!ValidateRobustBufferSize(context, bufSize, writeLength))
Geoff Lang62fce5b2016-09-30 10:46:35 -04001567 {
1568 return false;
1569 }
1570
Brandon Jonesd1049182018-03-28 10:02:20 -07001571 SetRobustLengthParam(length, writeLength);
1572 SetRobustLengthParam(columns, writeColumns);
1573 SetRobustLengthParam(rows, writeRows);
1574
Geoff Lang62fce5b2016-09-30 10:46:35 -04001575 return true;
Jamie Madill26e91952014-03-05 15:01:27 -05001576}
1577
Jamie Madillf0e04492017-08-26 15:28:42 -04001578bool ValidateGenQueriesEXT(gl::Context *context, GLsizei n, GLuint *ids)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001579{
1580 if (!context->getExtensions().occlusionQueryBoolean &&
1581 !context->getExtensions().disjointTimerQuery)
1582 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001583 ANGLE_VALIDATION_ERR(context, InvalidOperation(), QueryExtensionNotEnabled);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001584 return false;
1585 }
1586
Olli Etuaho41997e72016-03-10 13:38:39 +02001587 return ValidateGenOrDelete(context, n);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001588}
1589
Jamie Madillf0e04492017-08-26 15:28:42 -04001590bool ValidateDeleteQueriesEXT(gl::Context *context, GLsizei n, const GLuint *ids)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001591{
1592 if (!context->getExtensions().occlusionQueryBoolean &&
1593 !context->getExtensions().disjointTimerQuery)
1594 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001595 ANGLE_VALIDATION_ERR(context, InvalidOperation(), QueryExtensionNotEnabled);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001596 return false;
1597 }
1598
Olli Etuaho41997e72016-03-10 13:38:39 +02001599 return ValidateGenOrDelete(context, n);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001600}
1601
Jamie Madillf0e04492017-08-26 15:28:42 -04001602bool ValidateIsQueryEXT(gl::Context *context, GLuint id)
1603{
1604 if (!context->getExtensions().occlusionQueryBoolean &&
1605 !context->getExtensions().disjointTimerQuery)
1606 {
1607 ANGLE_VALIDATION_ERR(context, InvalidOperation(), QueryExtensionNotEnabled);
1608 return false;
1609 }
1610
1611 return true;
1612}
1613
Corentin Wallezad3ae902018-03-09 13:40:42 -05001614bool ValidateBeginQueryBase(gl::Context *context, QueryType target, GLuint id)
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001615{
1616 if (!ValidQueryType(context, target))
1617 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001618 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidQueryType);
Geoff Langb1196682014-07-23 13:47:29 -04001619 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001620 }
1621
1622 if (id == 0)
1623 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001624 context->handleError(InvalidOperation() << "Query id is 0");
Geoff Langb1196682014-07-23 13:47:29 -04001625 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001626 }
1627
1628 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
1629 // of zero, if the active query object name for <target> is non-zero (for the
1630 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
1631 // the active query for either target is non-zero), if <id> is the name of an
1632 // existing query object whose type does not match <target>, or if <id> is the
1633 // active query object name for any query type, the error INVALID_OPERATION is
1634 // generated.
1635
1636 // Ensure no other queries are active
1637 // NOTE: If other queries than occlusion are supported, we will need to check
1638 // separately that:
1639 // a) The query ID passed is not the current active query for any target/type
1640 // b) There are no active queries for the requested target (and in the case
1641 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
1642 // no query may be active for either if glBeginQuery targets either.
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001643
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001644 if (context->getGLState().isQueryActive(target))
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001645 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001646 context->handleError(InvalidOperation() << "Other query is active");
Geoff Langb1196682014-07-23 13:47:29 -04001647 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001648 }
1649
1650 Query *queryObject = context->getQuery(id, true, target);
1651
1652 // check that name was obtained with glGenQueries
1653 if (!queryObject)
1654 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001655 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidQueryId);
Geoff Langb1196682014-07-23 13:47:29 -04001656 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001657 }
1658
1659 // check for type mismatch
1660 if (queryObject->getType() != target)
1661 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001662 context->handleError(InvalidOperation() << "Query type does not match target");
Geoff Langb1196682014-07-23 13:47:29 -04001663 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001664 }
1665
1666 return true;
1667}
1668
Corentin Wallezad3ae902018-03-09 13:40:42 -05001669bool ValidateBeginQueryEXT(gl::Context *context, QueryType target, GLuint id)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001670{
1671 if (!context->getExtensions().occlusionQueryBoolean &&
Geoff Lang2b4ce802016-04-28 13:34:50 -04001672 !context->getExtensions().disjointTimerQuery && !context->getExtensions().syncQuery)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001673 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001674 ANGLE_VALIDATION_ERR(context, InvalidOperation(), QueryExtensionNotEnabled);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001675 return false;
1676 }
1677
1678 return ValidateBeginQueryBase(context, target, id);
1679}
1680
Corentin Wallezad3ae902018-03-09 13:40:42 -05001681bool ValidateEndQueryBase(gl::Context *context, QueryType target)
Jamie Madill45c785d2014-05-13 14:09:34 -04001682{
1683 if (!ValidQueryType(context, target))
1684 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001685 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidQueryType);
Geoff Langb1196682014-07-23 13:47:29 -04001686 return false;
Jamie Madill45c785d2014-05-13 14:09:34 -04001687 }
1688
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001689 const Query *queryObject = context->getGLState().getActiveQuery(target);
Jamie Madill45c785d2014-05-13 14:09:34 -04001690
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001691 if (queryObject == nullptr)
Jamie Madill45c785d2014-05-13 14:09:34 -04001692 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001693 context->handleError(InvalidOperation() << "Query target not active");
Geoff Langb1196682014-07-23 13:47:29 -04001694 return false;
Jamie Madill45c785d2014-05-13 14:09:34 -04001695 }
1696
Jamie Madill45c785d2014-05-13 14:09:34 -04001697 return true;
1698}
1699
Corentin Wallezad3ae902018-03-09 13:40:42 -05001700bool ValidateEndQueryEXT(gl::Context *context, QueryType target)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001701{
1702 if (!context->getExtensions().occlusionQueryBoolean &&
Geoff Lang2b4ce802016-04-28 13:34:50 -04001703 !context->getExtensions().disjointTimerQuery && !context->getExtensions().syncQuery)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001704 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001705 ANGLE_VALIDATION_ERR(context, InvalidOperation(), QueryExtensionNotEnabled);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001706 return false;
1707 }
1708
1709 return ValidateEndQueryBase(context, target);
1710}
1711
Corentin Wallezad3ae902018-03-09 13:40:42 -05001712bool ValidateQueryCounterEXT(Context *context, GLuint id, QueryType target)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001713{
1714 if (!context->getExtensions().disjointTimerQuery)
1715 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001716 context->handleError(InvalidOperation() << "Disjoint timer query not enabled");
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001717 return false;
1718 }
1719
Corentin Wallezad3ae902018-03-09 13:40:42 -05001720 if (target != QueryType::Timestamp)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001721 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001722 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidQueryTarget);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001723 return false;
1724 }
1725
1726 Query *queryObject = context->getQuery(id, true, target);
1727 if (queryObject == nullptr)
1728 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001729 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidQueryId);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001730 return false;
1731 }
1732
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001733 if (context->getGLState().isQueryActive(queryObject))
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001734 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001735 ANGLE_VALIDATION_ERR(context, InvalidOperation(), QueryActive);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001736 return false;
1737 }
1738
1739 return true;
1740}
1741
Corentin Wallezad3ae902018-03-09 13:40:42 -05001742bool ValidateGetQueryivBase(Context *context, QueryType target, GLenum pname, GLsizei *numParams)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001743{
Geoff Lang2186c382016-10-14 10:54:54 -04001744 if (numParams)
1745 {
1746 *numParams = 0;
1747 }
1748
Corentin Wallezad3ae902018-03-09 13:40:42 -05001749 if (!ValidQueryType(context, target) && target != QueryType::Timestamp)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001750 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001751 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidQueryType);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001752 return false;
1753 }
1754
1755 switch (pname)
1756 {
1757 case GL_CURRENT_QUERY_EXT:
Corentin Wallezad3ae902018-03-09 13:40:42 -05001758 if (target == QueryType::Timestamp)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001759 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001760 context->handleError(InvalidEnum() << "Cannot use current query for timestamp");
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001761 return false;
1762 }
1763 break;
1764 case GL_QUERY_COUNTER_BITS_EXT:
1765 if (!context->getExtensions().disjointTimerQuery ||
Corentin Wallezad3ae902018-03-09 13:40:42 -05001766 (target != QueryType::Timestamp && target != QueryType::TimeElapsed))
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001767 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001768 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidPname);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001769 return false;
1770 }
1771 break;
1772 default:
Brandon Jonesafa75152017-07-21 13:11:29 -07001773 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidPname);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001774 return false;
1775 }
1776
Geoff Lang2186c382016-10-14 10:54:54 -04001777 if (numParams)
1778 {
1779 // All queries return only one value
1780 *numParams = 1;
1781 }
1782
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001783 return true;
1784}
1785
Corentin Wallezad3ae902018-03-09 13:40:42 -05001786bool ValidateGetQueryivEXT(Context *context, QueryType target, GLenum pname, GLint *params)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001787{
1788 if (!context->getExtensions().occlusionQueryBoolean &&
Geoff Lang2b4ce802016-04-28 13:34:50 -04001789 !context->getExtensions().disjointTimerQuery && !context->getExtensions().syncQuery)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001790 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001791 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001792 return false;
1793 }
1794
Geoff Lang2186c382016-10-14 10:54:54 -04001795 return ValidateGetQueryivBase(context, target, pname, nullptr);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001796}
1797
Geoff Lang2186c382016-10-14 10:54:54 -04001798bool ValidateGetQueryivRobustANGLE(Context *context,
Corentin Wallezad3ae902018-03-09 13:40:42 -05001799 QueryType target,
Geoff Lang2186c382016-10-14 10:54:54 -04001800 GLenum pname,
1801 GLsizei bufSize,
1802 GLsizei *length,
1803 GLint *params)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001804{
Geoff Lang2186c382016-10-14 10:54:54 -04001805 if (!ValidateRobustEntryPoint(context, bufSize))
1806 {
1807 return false;
1808 }
1809
Brandon Jonesd1049182018-03-28 10:02:20 -07001810 GLsizei numParams = 0;
1811
1812 if (!ValidateGetQueryivBase(context, target, pname, &numParams))
Geoff Lang2186c382016-10-14 10:54:54 -04001813 {
1814 return false;
1815 }
1816
Brandon Jonesd1049182018-03-28 10:02:20 -07001817 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Lang2186c382016-10-14 10:54:54 -04001818 {
1819 return false;
1820 }
1821
Brandon Jonesd1049182018-03-28 10:02:20 -07001822 SetRobustLengthParam(length, numParams);
1823
Geoff Lang2186c382016-10-14 10:54:54 -04001824 return true;
1825}
1826
1827bool ValidateGetQueryObjectValueBase(Context *context, GLuint id, GLenum pname, GLsizei *numParams)
1828{
1829 if (numParams)
1830 {
1831 *numParams = 0;
1832 }
1833
Corentin Wallezad3ae902018-03-09 13:40:42 -05001834 Query *queryObject = context->getQuery(id, false, QueryType::InvalidEnum);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001835
1836 if (!queryObject)
1837 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001838 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidQueryId);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001839 return false;
1840 }
1841
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001842 if (context->getGLState().isQueryActive(queryObject))
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001843 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001844 ANGLE_VALIDATION_ERR(context, InvalidOperation(), QueryActive);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001845 return false;
1846 }
1847
1848 switch (pname)
1849 {
1850 case GL_QUERY_RESULT_EXT:
1851 case GL_QUERY_RESULT_AVAILABLE_EXT:
1852 break;
1853
1854 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07001855 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001856 return false;
1857 }
1858
Geoff Lang2186c382016-10-14 10:54:54 -04001859 if (numParams)
1860 {
1861 *numParams = 1;
1862 }
1863
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001864 return true;
1865}
1866
1867bool ValidateGetQueryObjectivEXT(Context *context, GLuint id, GLenum pname, GLint *params)
1868{
1869 if (!context->getExtensions().disjointTimerQuery)
1870 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001871 context->handleError(InvalidOperation() << "Timer query extension not enabled");
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001872 return false;
1873 }
Geoff Lang2186c382016-10-14 10:54:54 -04001874 return ValidateGetQueryObjectValueBase(context, id, pname, nullptr);
1875}
1876
1877bool ValidateGetQueryObjectivRobustANGLE(Context *context,
1878 GLuint id,
1879 GLenum pname,
1880 GLsizei bufSize,
1881 GLsizei *length,
1882 GLint *params)
1883{
1884 if (!context->getExtensions().disjointTimerQuery)
1885 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001886 context->handleError(InvalidOperation() << "Timer query extension not enabled");
Geoff Lang2186c382016-10-14 10:54:54 -04001887 return false;
1888 }
1889
1890 if (!ValidateRobustEntryPoint(context, bufSize))
1891 {
1892 return false;
1893 }
1894
Brandon Jonesd1049182018-03-28 10:02:20 -07001895 GLsizei numParams = 0;
1896
1897 if (!ValidateGetQueryObjectValueBase(context, id, pname, &numParams))
Geoff Lang2186c382016-10-14 10:54:54 -04001898 {
1899 return false;
1900 }
1901
Brandon Jonesd1049182018-03-28 10:02:20 -07001902 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Lang2186c382016-10-14 10:54:54 -04001903 {
1904 return false;
1905 }
1906
Brandon Jonesd1049182018-03-28 10:02:20 -07001907 SetRobustLengthParam(length, numParams);
1908
Geoff Lang2186c382016-10-14 10:54:54 -04001909 return true;
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001910}
1911
1912bool ValidateGetQueryObjectuivEXT(Context *context, GLuint id, GLenum pname, GLuint *params)
1913{
1914 if (!context->getExtensions().disjointTimerQuery &&
Geoff Lang2b4ce802016-04-28 13:34:50 -04001915 !context->getExtensions().occlusionQueryBoolean && !context->getExtensions().syncQuery)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001916 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001917 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001918 return false;
1919 }
Geoff Lang2186c382016-10-14 10:54:54 -04001920 return ValidateGetQueryObjectValueBase(context, id, pname, nullptr);
1921}
1922
1923bool ValidateGetQueryObjectuivRobustANGLE(Context *context,
1924 GLuint id,
1925 GLenum pname,
1926 GLsizei bufSize,
1927 GLsizei *length,
1928 GLuint *params)
1929{
1930 if (!context->getExtensions().disjointTimerQuery &&
1931 !context->getExtensions().occlusionQueryBoolean && !context->getExtensions().syncQuery)
1932 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001933 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
Geoff Lang2186c382016-10-14 10:54:54 -04001934 return false;
1935 }
1936
1937 if (!ValidateRobustEntryPoint(context, bufSize))
1938 {
1939 return false;
1940 }
1941
Brandon Jonesd1049182018-03-28 10:02:20 -07001942 GLsizei numParams = 0;
1943
1944 if (!ValidateGetQueryObjectValueBase(context, id, pname, &numParams))
Geoff Lang2186c382016-10-14 10:54:54 -04001945 {
1946 return false;
1947 }
1948
Brandon Jonesd1049182018-03-28 10:02:20 -07001949 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Lang2186c382016-10-14 10:54:54 -04001950 {
1951 return false;
1952 }
1953
Brandon Jonesd1049182018-03-28 10:02:20 -07001954 SetRobustLengthParam(length, numParams);
1955
Geoff Lang2186c382016-10-14 10:54:54 -04001956 return true;
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001957}
1958
1959bool ValidateGetQueryObjecti64vEXT(Context *context, GLuint id, GLenum pname, GLint64 *params)
1960{
1961 if (!context->getExtensions().disjointTimerQuery)
1962 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001963 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001964 return false;
1965 }
Geoff Lang2186c382016-10-14 10:54:54 -04001966 return ValidateGetQueryObjectValueBase(context, id, pname, nullptr);
1967}
1968
1969bool ValidateGetQueryObjecti64vRobustANGLE(Context *context,
1970 GLuint id,
1971 GLenum pname,
1972 GLsizei bufSize,
1973 GLsizei *length,
1974 GLint64 *params)
1975{
1976 if (!context->getExtensions().disjointTimerQuery)
1977 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001978 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
Geoff Lang2186c382016-10-14 10:54:54 -04001979 return false;
1980 }
1981
1982 if (!ValidateRobustEntryPoint(context, bufSize))
1983 {
1984 return false;
1985 }
1986
Brandon Jonesd1049182018-03-28 10:02:20 -07001987 GLsizei numParams = 0;
1988
1989 if (!ValidateGetQueryObjectValueBase(context, id, pname, &numParams))
Geoff Lang2186c382016-10-14 10:54:54 -04001990 {
1991 return false;
1992 }
1993
Brandon Jonesd1049182018-03-28 10:02:20 -07001994 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Lang2186c382016-10-14 10:54:54 -04001995 {
1996 return false;
1997 }
1998
Brandon Jonesd1049182018-03-28 10:02:20 -07001999 SetRobustLengthParam(length, numParams);
2000
Geoff Lang2186c382016-10-14 10:54:54 -04002001 return true;
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002002}
2003
2004bool ValidateGetQueryObjectui64vEXT(Context *context, GLuint id, GLenum pname, GLuint64 *params)
2005{
2006 if (!context->getExtensions().disjointTimerQuery)
2007 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002008 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002009 return false;
2010 }
Geoff Lang2186c382016-10-14 10:54:54 -04002011 return ValidateGetQueryObjectValueBase(context, id, pname, nullptr);
2012}
2013
2014bool ValidateGetQueryObjectui64vRobustANGLE(Context *context,
2015 GLuint id,
2016 GLenum pname,
2017 GLsizei bufSize,
2018 GLsizei *length,
2019 GLuint64 *params)
2020{
2021 if (!context->getExtensions().disjointTimerQuery)
2022 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002023 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
Geoff Lang2186c382016-10-14 10:54:54 -04002024 return false;
2025 }
2026
2027 if (!ValidateRobustEntryPoint(context, bufSize))
2028 {
2029 return false;
2030 }
2031
Brandon Jonesd1049182018-03-28 10:02:20 -07002032 GLsizei numParams = 0;
2033
2034 if (!ValidateGetQueryObjectValueBase(context, id, pname, &numParams))
Geoff Lang2186c382016-10-14 10:54:54 -04002035 {
2036 return false;
2037 }
2038
Brandon Jonesd1049182018-03-28 10:02:20 -07002039 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Lang2186c382016-10-14 10:54:54 -04002040 {
2041 return false;
2042 }
2043
Brandon Jonesd1049182018-03-28 10:02:20 -07002044 SetRobustLengthParam(length, numParams);
2045
Geoff Lang2186c382016-10-14 10:54:54 -04002046 return true;
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002047}
2048
Jamie Madill5b772312018-03-08 20:28:32 -05002049bool ValidateUniformCommonBase(Context *context,
Jiajia Qin5451d532017-11-16 17:16:34 +08002050 gl::Program *program,
Frank Henigmana98a6472017-02-02 21:38:32 -05002051 GLint location,
2052 GLsizei count,
Jiajia Qin5451d532017-11-16 17:16:34 +08002053 const LinkedUniform **uniformOut)
Frank Henigmana98a6472017-02-02 21:38:32 -05002054{
Jiajia Qin5451d532017-11-16 17:16:34 +08002055 // TODO(Jiajia): Add image uniform check in future.
2056 if (count < 0)
Frank Henigmana98a6472017-02-02 21:38:32 -05002057 {
Jiajia Qin5451d532017-11-16 17:16:34 +08002058 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeCount);
Frank Henigmana98a6472017-02-02 21:38:32 -05002059 return false;
2060 }
2061
Jiajia Qin5451d532017-11-16 17:16:34 +08002062 if (!program)
2063 {
2064 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidProgramName);
2065 return false;
2066 }
2067
2068 if (!program->isLinked())
2069 {
2070 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ProgramNotLinked);
2071 return false;
2072 }
2073
2074 if (location == -1)
2075 {
2076 // Silently ignore the uniform command
2077 return false;
2078 }
2079
2080 const auto &uniformLocations = program->getUniformLocations();
2081 size_t castedLocation = static_cast<size_t>(location);
2082 if (castedLocation >= uniformLocations.size())
2083 {
2084 context->handleError(InvalidOperation() << "Invalid uniform location");
2085 return false;
2086 }
2087
2088 const auto &uniformLocation = uniformLocations[castedLocation];
2089 if (uniformLocation.ignored)
2090 {
2091 // Silently ignore the uniform command
2092 return false;
2093 }
2094
2095 if (!uniformLocation.used())
2096 {
2097 context->handleError(InvalidOperation());
2098 return false;
2099 }
2100
2101 const auto &uniform = program->getUniformByIndex(uniformLocation.index);
2102
2103 // attempting to write an array to a non-array uniform is an INVALID_OPERATION
Jamie Madill2fc08062018-05-10 15:10:55 -04002104 if (count > 1 && !uniform.isArray())
Jiajia Qin5451d532017-11-16 17:16:34 +08002105 {
2106 context->handleError(InvalidOperation());
2107 return false;
2108 }
2109
2110 *uniformOut = &uniform;
2111 return true;
Frank Henigmana98a6472017-02-02 21:38:32 -05002112}
2113
Jamie Madill5b772312018-03-08 20:28:32 -05002114bool ValidateUniform1ivValue(Context *context,
Jiajia Qin5451d532017-11-16 17:16:34 +08002115 GLenum uniformType,
2116 GLsizei count,
2117 const GLint *value)
Jiajia Qinee9f08c2016-11-16 10:06:10 +08002118{
Jiajia Qin5451d532017-11-16 17:16:34 +08002119 // Value type is GL_INT, because we only get here from glUniform1i{v}.
2120 // It is compatible with INT or BOOL.
2121 // Do these cheap tests first, for a little extra speed.
2122 if (GL_INT == uniformType || GL_BOOL == uniformType)
Jiajia Qinee9f08c2016-11-16 10:06:10 +08002123 {
Jiajia Qin5451d532017-11-16 17:16:34 +08002124 return true;
Jiajia Qinee9f08c2016-11-16 10:06:10 +08002125 }
2126
Jiajia Qin5451d532017-11-16 17:16:34 +08002127 if (IsSamplerType(uniformType))
2128 {
2129 // Check that the values are in range.
2130 const GLint max = context->getCaps().maxCombinedTextureImageUnits;
2131 for (GLsizei i = 0; i < count; ++i)
2132 {
2133 if (value[i] < 0 || value[i] >= max)
2134 {
2135 context->handleError(InvalidValue() << "sampler uniform value out of range");
2136 return false;
2137 }
2138 }
2139 return true;
2140 }
2141
2142 context->handleError(InvalidOperation() << "wrong type of value for uniform");
2143 return false;
2144}
2145
Jamie Madill5b772312018-03-08 20:28:32 -05002146bool ValidateUniformValue(Context *context, GLenum valueType, GLenum uniformType)
Jiajia Qin5451d532017-11-16 17:16:34 +08002147{
2148 // Check that the value type is compatible with uniform type.
2149 // Do the cheaper test first, for a little extra speed.
2150 if (valueType == uniformType || VariableBoolVectorType(valueType) == uniformType)
2151 {
2152 return true;
2153 }
2154
2155 ANGLE_VALIDATION_ERR(context, InvalidOperation(), UniformSizeMismatch);
2156 return false;
2157}
2158
Jamie Madill5b772312018-03-08 20:28:32 -05002159bool ValidateUniformMatrixValue(Context *context, GLenum valueType, GLenum uniformType)
Jiajia Qin5451d532017-11-16 17:16:34 +08002160{
2161 // Check that the value type is compatible with uniform type.
2162 if (valueType == uniformType)
2163 {
2164 return true;
2165 }
2166
2167 context->handleError(InvalidOperation() << "wrong type of value for uniform");
2168 return false;
Jiajia Qinee9f08c2016-11-16 10:06:10 +08002169}
2170
Jamie Madill5b772312018-03-08 20:28:32 -05002171bool ValidateUniform(Context *context, GLenum valueType, GLint location, GLsizei count)
Jamie Madillaa981bd2014-05-20 10:55:55 -04002172{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002173 const LinkedUniform *uniform = nullptr;
Frank Henigmanf5f74ae2017-02-02 21:14:23 -05002174 gl::Program *programObject = context->getGLState().getProgram();
2175 return ValidateUniformCommonBase(context, programObject, location, count, &uniform) &&
2176 ValidateUniformValue(context, valueType, uniform->type);
Jamie Madillaa981bd2014-05-20 10:55:55 -04002177}
2178
Jamie Madill5b772312018-03-08 20:28:32 -05002179bool ValidateUniform1iv(Context *context, GLint location, GLsizei count, const GLint *value)
Frank Henigmana98a6472017-02-02 21:38:32 -05002180{
2181 const LinkedUniform *uniform = nullptr;
2182 gl::Program *programObject = context->getGLState().getProgram();
2183 return ValidateUniformCommonBase(context, programObject, location, count, &uniform) &&
2184 ValidateUniform1ivValue(context, uniform->type, count, value);
2185}
2186
Jamie Madill5b772312018-03-08 20:28:32 -05002187bool ValidateUniformMatrix(Context *context,
Frank Henigmanf5f74ae2017-02-02 21:14:23 -05002188 GLenum valueType,
He Yunchaoced53ae2016-11-29 15:00:51 +08002189 GLint location,
2190 GLsizei count,
Jamie Madillaa981bd2014-05-20 10:55:55 -04002191 GLboolean transpose)
2192{
Geoff Lang92019432017-11-20 13:09:34 -05002193 if (ConvertToBool(transpose) && context->getClientMajorVersion() < 3)
Jamie Madillaa981bd2014-05-20 10:55:55 -04002194 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002195 context->handleError(InvalidValue());
Geoff Langb1196682014-07-23 13:47:29 -04002196 return false;
Jamie Madillaa981bd2014-05-20 10:55:55 -04002197 }
2198
Jamie Madill62d31cb2015-09-11 13:25:51 -04002199 const LinkedUniform *uniform = nullptr;
Frank Henigmanf5f74ae2017-02-02 21:14:23 -05002200 gl::Program *programObject = context->getGLState().getProgram();
2201 return ValidateUniformCommonBase(context, programObject, location, count, &uniform) &&
2202 ValidateUniformMatrixValue(context, valueType, uniform->type);
Jamie Madillaa981bd2014-05-20 10:55:55 -04002203}
2204
Jamie Madill5b772312018-03-08 20:28:32 -05002205bool ValidateStateQuery(Context *context, GLenum pname, GLenum *nativeType, unsigned int *numParams)
Jamie Madill893ab082014-05-16 16:56:10 -04002206{
2207 if (!context->getQueryParameterInfo(pname, nativeType, numParams))
2208 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002209 context->handleError(InvalidEnum());
Geoff Langb1196682014-07-23 13:47:29 -04002210 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04002211 }
2212
Jamie Madill0af26e12015-03-05 19:54:33 -05002213 const Caps &caps = context->getCaps();
2214
Jamie Madill893ab082014-05-16 16:56:10 -04002215 if (pname >= GL_DRAW_BUFFER0 && pname <= GL_DRAW_BUFFER15)
2216 {
2217 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0);
2218
Jamie Madill0af26e12015-03-05 19:54:33 -05002219 if (colorAttachment >= caps.maxDrawBuffers)
Jamie Madill893ab082014-05-16 16:56:10 -04002220 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002221 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04002222 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04002223 }
2224 }
2225
2226 switch (pname)
2227 {
He Yunchaoced53ae2016-11-29 15:00:51 +08002228 case GL_TEXTURE_BINDING_2D:
2229 case GL_TEXTURE_BINDING_CUBE_MAP:
2230 case GL_TEXTURE_BINDING_3D:
2231 case GL_TEXTURE_BINDING_2D_ARRAY:
JiangYizhou24fe74c2017-07-06 16:56:50 +08002232 case GL_TEXTURE_BINDING_2D_MULTISAMPLE:
He Yunchaoced53ae2016-11-29 15:00:51 +08002233 break;
Corentin Wallez13c0dd42017-07-04 18:27:01 -04002234 case GL_TEXTURE_BINDING_RECTANGLE_ANGLE:
2235 if (!context->getExtensions().textureRectangle)
2236 {
2237 context->handleError(InvalidEnum()
2238 << "ANGLE_texture_rectangle extension not present");
2239 return false;
2240 }
2241 break;
He Yunchaoced53ae2016-11-29 15:00:51 +08002242 case GL_TEXTURE_BINDING_EXTERNAL_OES:
2243 if (!context->getExtensions().eglStreamConsumerExternal &&
2244 !context->getExtensions().eglImageExternal)
2245 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002246 context->handleError(InvalidEnum() << "Neither NV_EGL_stream_consumer_external "
2247 "nor GL_OES_EGL_image_external "
2248 "extensions enabled");
He Yunchaoced53ae2016-11-29 15:00:51 +08002249 return false;
2250 }
2251 break;
Jamie Madill893ab082014-05-16 16:56:10 -04002252
He Yunchaoced53ae2016-11-29 15:00:51 +08002253 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
2254 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
Jamie Madill893ab082014-05-16 16:56:10 -04002255 {
Jamie Madille98b1b52018-03-08 09:47:23 -05002256 Framebuffer *readFramebuffer = context->getGLState().getReadFramebuffer();
2257 ASSERT(readFramebuffer);
2258
Jamie Madill427064d2018-04-13 16:20:34 -04002259 if (!ValidateFramebufferComplete<InvalidOperation>(context, readFramebuffer))
Jamie Madill893ab082014-05-16 16:56:10 -04002260 {
Geoff Langb1196682014-07-23 13:47:29 -04002261 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04002262 }
2263
Jamie Madille98b1b52018-03-08 09:47:23 -05002264 if (readFramebuffer->getReadBufferState() == GL_NONE)
Martin Radev138064f2016-07-15 12:03:41 +03002265 {
Brandon Jonesafa75152017-07-21 13:11:29 -07002266 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ReadBufferNone);
Martin Radev138064f2016-07-15 12:03:41 +03002267 return false;
2268 }
2269
Jamie Madille98b1b52018-03-08 09:47:23 -05002270 const FramebufferAttachment *attachment = readFramebuffer->getReadColorbuffer();
Jamie Madill3c7fa222014-06-05 13:08:51 -04002271 if (!attachment)
Jamie Madill893ab082014-05-16 16:56:10 -04002272 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002273 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04002274 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04002275 }
2276 }
2277 break;
2278
He Yunchaoced53ae2016-11-29 15:00:51 +08002279 default:
2280 break;
Jamie Madill893ab082014-05-16 16:56:10 -04002281 }
2282
2283 // pname is valid, but there are no parameters to return
Geoff Langff5b2d52016-09-07 11:32:23 -04002284 if (*numParams == 0)
2285 {
2286 return false;
2287 }
2288
2289 return true;
2290}
2291
Brandon Jonesd1049182018-03-28 10:02:20 -07002292bool ValidateGetBooleanvRobustANGLE(Context *context,
2293 GLenum pname,
2294 GLsizei bufSize,
2295 GLsizei *length,
2296 GLboolean *params)
2297{
2298 GLenum nativeType;
2299 unsigned int numParams = 0;
2300
2301 if (!ValidateRobustStateQuery(context, pname, bufSize, &nativeType, &numParams))
2302 {
2303 return false;
2304 }
2305
2306 SetRobustLengthParam(length, numParams);
2307
2308 return true;
2309}
2310
2311bool ValidateGetFloatvRobustANGLE(Context *context,
2312 GLenum pname,
2313 GLsizei bufSize,
2314 GLsizei *length,
2315 GLfloat *params)
2316{
2317 GLenum nativeType;
2318 unsigned int numParams = 0;
2319
2320 if (!ValidateRobustStateQuery(context, pname, bufSize, &nativeType, &numParams))
2321 {
2322 return false;
2323 }
2324
2325 SetRobustLengthParam(length, numParams);
2326
2327 return true;
2328}
2329
2330bool ValidateGetIntegervRobustANGLE(Context *context,
2331 GLenum pname,
2332 GLsizei bufSize,
2333 GLsizei *length,
2334 GLint *data)
2335{
2336 GLenum nativeType;
2337 unsigned int numParams = 0;
2338
2339 if (!ValidateRobustStateQuery(context, pname, bufSize, &nativeType, &numParams))
2340 {
2341 return false;
2342 }
2343
2344 SetRobustLengthParam(length, numParams);
2345
2346 return true;
2347}
2348
2349bool ValidateGetInteger64vRobustANGLE(Context *context,
2350 GLenum pname,
2351 GLsizei bufSize,
2352 GLsizei *length,
2353 GLint64 *data)
2354{
2355 GLenum nativeType;
2356 unsigned int numParams = 0;
2357
2358 if (!ValidateRobustStateQuery(context, pname, bufSize, &nativeType, &numParams))
2359 {
2360 return false;
2361 }
2362
2363 if (nativeType == GL_INT_64_ANGLEX)
2364 {
2365 CastStateValues(context, nativeType, pname, numParams, data);
2366 return false;
2367 }
2368
2369 SetRobustLengthParam(length, numParams);
2370 return true;
2371}
2372
Jamie Madill5b772312018-03-08 20:28:32 -05002373bool ValidateRobustStateQuery(Context *context,
Geoff Langff5b2d52016-09-07 11:32:23 -04002374 GLenum pname,
2375 GLsizei bufSize,
2376 GLenum *nativeType,
2377 unsigned int *numParams)
2378{
2379 if (!ValidateRobustEntryPoint(context, bufSize))
2380 {
2381 return false;
2382 }
2383
2384 if (!ValidateStateQuery(context, pname, nativeType, numParams))
2385 {
2386 return false;
2387 }
2388
2389 if (!ValidateRobustBufferSize(context, bufSize, *numParams))
Jamie Madill893ab082014-05-16 16:56:10 -04002390 {
2391 return false;
2392 }
2393
2394 return true;
2395}
2396
Jamie Madill5b772312018-03-08 20:28:32 -05002397bool ValidateCopyTexImageParametersBase(Context *context,
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002398 TextureTarget target,
Jamie Madillc29968b2016-01-20 11:17:23 -05002399 GLint level,
2400 GLenum internalformat,
2401 bool isSubImage,
2402 GLint xoffset,
2403 GLint yoffset,
2404 GLint zoffset,
2405 GLint x,
2406 GLint y,
2407 GLsizei width,
2408 GLsizei height,
2409 GLint border,
Jamie Madill0c8abca2016-07-22 20:21:26 -04002410 Format *textureFormatOut)
Jamie Madill560a8d82014-05-21 13:06:20 -04002411{
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002412 TextureType texType = TextureTargetToType(target);
2413
Brandon Jones6cad5662017-06-14 13:25:13 -07002414 if (xoffset < 0 || yoffset < 0 || zoffset < 0)
Jamie Madill560a8d82014-05-21 13:06:20 -04002415 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002416 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeOffset);
2417 return false;
2418 }
2419
2420 if (width < 0 || height < 0)
2421 {
2422 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeSize);
Geoff Langb1196682014-07-23 13:47:29 -04002423 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002424 }
2425
He Yunchaoced53ae2016-11-29 15:00:51 +08002426 if (std::numeric_limits<GLsizei>::max() - xoffset < width ||
2427 std::numeric_limits<GLsizei>::max() - yoffset < height)
Jamie Madill560a8d82014-05-21 13:06:20 -04002428 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002429 context->handleError(InvalidValue());
Geoff Langb1196682014-07-23 13:47:29 -04002430 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002431 }
2432
2433 if (border != 0)
2434 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002435 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidBorder);
Geoff Langb1196682014-07-23 13:47:29 -04002436 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002437 }
2438
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002439 if (!ValidMipLevel(context, texType, level))
Jamie Madill560a8d82014-05-21 13:06:20 -04002440 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002441 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidMipLevel);
Geoff Langb1196682014-07-23 13:47:29 -04002442 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002443 }
2444
Jamie Madille98b1b52018-03-08 09:47:23 -05002445 const gl::State &state = context->getGLState();
Jamie Madillacf2f3a2017-11-21 19:22:44 -05002446 Framebuffer *readFramebuffer = state.getReadFramebuffer();
Jamie Madill427064d2018-04-13 16:20:34 -04002447 if (!ValidateFramebufferComplete(context, readFramebuffer))
Jamie Madill560a8d82014-05-21 13:06:20 -04002448 {
Geoff Langb1196682014-07-23 13:47:29 -04002449 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002450 }
2451
Jamie Madille98b1b52018-03-08 09:47:23 -05002452 if (readFramebuffer->id() != 0 && !ValidateFramebufferNotMultisampled(context, readFramebuffer))
Jamie Madill560a8d82014-05-21 13:06:20 -04002453 {
Geoff Langb1196682014-07-23 13:47:29 -04002454 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002455 }
2456
Martin Radev138064f2016-07-15 12:03:41 +03002457 if (readFramebuffer->getReadBufferState() == GL_NONE)
2458 {
Brandon Jonesafa75152017-07-21 13:11:29 -07002459 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ReadBufferNone);
Martin Radev138064f2016-07-15 12:03:41 +03002460 return false;
2461 }
2462
Corentin Wallez3c90ed62016-12-16 16:19:28 -05002463 // WebGL 1.0 [Section 6.26] Reading From a Missing Attachment
2464 // In OpenGL ES it is undefined what happens when an operation tries to read from a missing
He Yunchao66a41a22016-12-15 16:45:05 +08002465 // attachment and WebGL defines it to be an error. We do the check unconditionally as the
Corentin Wallez3c90ed62016-12-16 16:19:28 -05002466 // situation is an application error that would lead to a crash in ANGLE.
Martin Radev04e2c3b2017-07-27 16:54:35 +03002467 const FramebufferAttachment *source = readFramebuffer->getReadColorbuffer();
2468 if (source == nullptr)
Corentin Wallez3c90ed62016-12-16 16:19:28 -05002469 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002470 ANGLE_VALIDATION_ERR(context, InvalidOperation(), MissingReadAttachment);
Corentin Wallez3c90ed62016-12-16 16:19:28 -05002471 return false;
2472 }
2473
Martin Radev04e2c3b2017-07-27 16:54:35 +03002474 // ANGLE_multiview spec, Revision 1:
2475 // Calling CopyTexSubImage3D, CopyTexImage2D, or CopyTexSubImage2D will result in an
2476 // INVALID_FRAMEBUFFER_OPERATION error if the multi-view layout of the current read framebuffer
Olli Etuaho8acb1b62018-07-30 16:20:54 +03002477 // is FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE or the number of views in the current read
2478 // framebuffer is more than one.
2479 if (readFramebuffer->readDisallowedByMultiview())
Martin Radev04e2c3b2017-07-27 16:54:35 +03002480 {
2481 context->handleError(InvalidFramebufferOperation()
2482 << "The active read framebuffer object has multiview attachments.");
2483 return false;
2484 }
2485
Geoff Langaae65a42014-05-26 12:43:44 -04002486 const gl::Caps &caps = context->getCaps();
2487
Geoff Langaae65a42014-05-26 12:43:44 -04002488 GLuint maxDimension = 0;
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002489 switch (texType)
Jamie Madill560a8d82014-05-21 13:06:20 -04002490 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002491 case TextureType::_2D:
He Yunchaoced53ae2016-11-29 15:00:51 +08002492 maxDimension = caps.max2DTextureSize;
2493 break;
Jamie Madill560a8d82014-05-21 13:06:20 -04002494
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002495 case TextureType::CubeMap:
He Yunchaoced53ae2016-11-29 15:00:51 +08002496 maxDimension = caps.maxCubeMapTextureSize;
2497 break;
Jamie Madill560a8d82014-05-21 13:06:20 -04002498
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002499 case TextureType::Rectangle:
Corentin Wallez13c0dd42017-07-04 18:27:01 -04002500 maxDimension = caps.maxRectangleTextureSize;
2501 break;
2502
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002503 case TextureType::_2DArray:
He Yunchaoced53ae2016-11-29 15:00:51 +08002504 maxDimension = caps.max2DTextureSize;
2505 break;
Jamie Madill560a8d82014-05-21 13:06:20 -04002506
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002507 case TextureType::_3D:
He Yunchaoced53ae2016-11-29 15:00:51 +08002508 maxDimension = caps.max3DTextureSize;
2509 break;
Jamie Madill560a8d82014-05-21 13:06:20 -04002510
He Yunchaoced53ae2016-11-29 15:00:51 +08002511 default:
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002512 context->handleError(InvalidEnum());
He Yunchaoced53ae2016-11-29 15:00:51 +08002513 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002514 }
2515
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002516 gl::Texture *texture = state.getTargetTexture(texType);
Jamie Madill560a8d82014-05-21 13:06:20 -04002517 if (!texture)
2518 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002519 ANGLE_VALIDATION_ERR(context, InvalidOperation(), TextureNotBound);
Geoff Langb1196682014-07-23 13:47:29 -04002520 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002521 }
2522
Geoff Lang69cce582015-09-17 13:20:36 -04002523 if (texture->getImmutableFormat() && !isSubImage)
Jamie Madill560a8d82014-05-21 13:06:20 -04002524 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002525 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04002526 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002527 }
2528
Geoff Langca271392017-04-05 12:30:00 -04002529 const gl::InternalFormat &formatInfo =
Geoff Lang86f81162017-10-30 15:10:45 -04002530 isSubImage ? *texture->getFormat(target, level).info
2531 : gl::GetInternalFormatInfo(internalformat, GL_UNSIGNED_BYTE);
Geoff Lang5d601382014-07-22 15:14:06 -04002532
Geoff Lang966c9402017-04-18 12:38:27 -04002533 if (formatInfo.depthBits > 0 || formatInfo.compressed)
Jamie Madill560a8d82014-05-21 13:06:20 -04002534 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002535 context->handleError(InvalidOperation());
Geoff Langa9be0dc2014-12-17 12:34:40 -05002536 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002537 }
2538
2539 if (isSubImage)
2540 {
Geoff Langa9be0dc2014-12-17 12:34:40 -05002541 if (static_cast<size_t>(xoffset + width) > texture->getWidth(target, level) ||
2542 static_cast<size_t>(yoffset + height) > texture->getHeight(target, level) ||
2543 static_cast<size_t>(zoffset) >= texture->getDepth(target, level))
Jamie Madill560a8d82014-05-21 13:06:20 -04002544 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002545 context->handleError(InvalidValue());
Geoff Langb1196682014-07-23 13:47:29 -04002546 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002547 }
2548 }
Jamie Madill6f38f822014-06-06 17:12:20 -04002549 else
2550 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002551 if (texType == TextureType::CubeMap && width != height)
Jamie Madill6f38f822014-06-06 17:12:20 -04002552 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002553 ANGLE_VALIDATION_ERR(context, InvalidValue(), CubemapIncomplete);
Geoff Langb1196682014-07-23 13:47:29 -04002554 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04002555 }
2556
Geoff Langeb66a6e2016-10-31 13:06:12 -04002557 if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions()))
Jamie Madill6f38f822014-06-06 17:12:20 -04002558 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002559 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Geoff Langb1196682014-07-23 13:47:29 -04002560 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04002561 }
2562
2563 int maxLevelDimension = (maxDimension >> level);
He Yunchaoced53ae2016-11-29 15:00:51 +08002564 if (static_cast<int>(width) > maxLevelDimension ||
2565 static_cast<int>(height) > maxLevelDimension)
Jamie Madill6f38f822014-06-06 17:12:20 -04002566 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002567 ANGLE_VALIDATION_ERR(context, InvalidValue(), ResourceMaxTextureSize);
Geoff Langb1196682014-07-23 13:47:29 -04002568 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04002569 }
2570 }
Jamie Madill560a8d82014-05-21 13:06:20 -04002571
Jamie Madill0c8abca2016-07-22 20:21:26 -04002572 if (textureFormatOut)
2573 {
2574 *textureFormatOut = texture->getFormat(target, level);
2575 }
Jamie Madillf695a3a2017-01-11 17:36:35 -05002576
2577 // Detect texture copying feedback loops for WebGL.
2578 if (context->getExtensions().webglCompatibility)
2579 {
Jamie Madillfd3dd432017-02-02 19:59:59 -05002580 if (readFramebuffer->formsCopyingFeedbackLoopWith(texture->id(), level, zoffset))
Jamie Madillf695a3a2017-01-11 17:36:35 -05002581 {
Brandon Jonesafa75152017-07-21 13:11:29 -07002582 ANGLE_VALIDATION_ERR(context, InvalidOperation(), FeedbackLoop);
Jamie Madillf695a3a2017-01-11 17:36:35 -05002583 return false;
2584 }
2585 }
2586
Jamie Madill560a8d82014-05-21 13:06:20 -04002587 return true;
2588}
2589
Jamie Madill493f9572018-05-24 19:52:15 -04002590bool ValidateDrawBase(Context *context, PrimitiveMode mode, GLsizei count)
Jamie Madill250d33f2014-06-06 17:09:03 -04002591{
Jiawei Shaofccebff2018-03-08 13:51:02 +08002592 const Extensions &extensions = context->getExtensions();
2593
Jamie Madill1aeb1312014-06-20 13:21:25 -04002594 switch (mode)
2595 {
Jamie Madill493f9572018-05-24 19:52:15 -04002596 case PrimitiveMode::Points:
2597 case PrimitiveMode::Lines:
2598 case PrimitiveMode::LineLoop:
2599 case PrimitiveMode::LineStrip:
2600 case PrimitiveMode::Triangles:
2601 case PrimitiveMode::TriangleStrip:
2602 case PrimitiveMode::TriangleFan:
He Yunchaoced53ae2016-11-29 15:00:51 +08002603 break;
Jiawei Shaofccebff2018-03-08 13:51:02 +08002604
Jamie Madill493f9572018-05-24 19:52:15 -04002605 case PrimitiveMode::LinesAdjacency:
2606 case PrimitiveMode::LineStripAdjacency:
2607 case PrimitiveMode::TrianglesAdjacency:
2608 case PrimitiveMode::TriangleStripAdjacency:
Jiawei Shaofccebff2018-03-08 13:51:02 +08002609 if (!extensions.geometryShader)
2610 {
2611 ANGLE_VALIDATION_ERR(context, InvalidEnum(), GeometryShaderExtensionNotEnabled);
2612 return false;
2613 }
2614 break;
He Yunchaoced53ae2016-11-29 15:00:51 +08002615 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07002616 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidDrawMode);
He Yunchaoced53ae2016-11-29 15:00:51 +08002617 return false;
Jamie Madill1aeb1312014-06-20 13:21:25 -04002618 }
2619
Jamie Madill250d33f2014-06-06 17:09:03 -04002620 if (count < 0)
2621 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002622 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeCount);
Geoff Langb1196682014-07-23 13:47:29 -04002623 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04002624 }
2625
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002626 const State &state = context->getGLState();
Geoff Langb1196682014-07-23 13:47:29 -04002627
Jiawei Shao3ef06a92017-11-03 18:41:33 +08002628 // WebGL buffers cannot be mapped/unmapped because the MapBufferRange, FlushMappedBufferRange,
2629 // and UnmapBuffer entry points are removed from the WebGL 2.0 API.
2630 // https://www.khronos.org/registry/webgl/specs/latest/2.0/#5.14
Jamie Madillcc73f242018-08-01 11:34:48 -04002631 if (!extensions.webglCompatibility && state.getVertexArray()->hasMappedEnabledArrayBuffer())
Jamie Madill250d33f2014-06-06 17:09:03 -04002632 {
Jamie Madillcc73f242018-08-01 11:34:48 -04002633 context->handleError(InvalidOperation());
2634 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04002635 }
2636
Jamie Madillcbcde722017-01-06 14:50:00 -05002637 // Note: these separate values are not supported in WebGL, due to D3D's limitations. See
2638 // Section 6.10 of the WebGL 1.0 spec.
Jamie Madill51f40ec2016-06-15 14:06:00 -04002639 Framebuffer *framebuffer = state.getDrawFramebuffer();
Martin Radevffe754b2017-07-31 10:38:07 +03002640 if (context->getLimitations().noSeparateStencilRefsAndMasks || extensions.webglCompatibility)
Jamie Madillac528012014-06-20 13:21:23 -04002641 {
Ken Russellb9f92502018-01-27 19:00:26 -08002642 ASSERT(framebuffer);
Corentin Wallezb1d0a2552016-12-19 16:15:54 -05002643 const FramebufferAttachment *dsAttachment =
2644 framebuffer->getStencilOrDepthStencilAttachment();
Ken Russellb9f92502018-01-27 19:00:26 -08002645 const GLuint stencilBits = dsAttachment ? dsAttachment->getStencilSize() : 0;
2646 ASSERT(stencilBits <= 8);
2647
Jinyoung Hur85769f02015-10-20 17:08:44 -04002648 const DepthStencilState &depthStencilState = state.getDepthStencilState();
Ken Russellb9f92502018-01-27 19:00:26 -08002649 if (depthStencilState.stencilTest && stencilBits > 0)
Geoff Lang3a86ad32015-09-01 11:47:05 -04002650 {
Ken Russellb9f92502018-01-27 19:00:26 -08002651 GLuint maxStencilValue = (1 << stencilBits) - 1;
2652
2653 bool differentRefs =
2654 clamp(state.getStencilRef(), 0, static_cast<GLint>(maxStencilValue)) !=
2655 clamp(state.getStencilBackRef(), 0, static_cast<GLint>(maxStencilValue));
2656 bool differentWritemasks = (depthStencilState.stencilWritemask & maxStencilValue) !=
2657 (depthStencilState.stencilBackWritemask & maxStencilValue);
2658 bool differentMasks = (depthStencilState.stencilMask & maxStencilValue) !=
2659 (depthStencilState.stencilBackMask & maxStencilValue);
2660
2661 if (differentRefs || differentWritemasks || differentMasks)
Jamie Madillcbcde722017-01-06 14:50:00 -05002662 {
Ken Russellb9f92502018-01-27 19:00:26 -08002663 if (!extensions.webglCompatibility)
2664 {
Jamie Madilla2f043d2018-07-10 17:21:20 -04002665 WARN() << "This ANGLE implementation does not support separate front/back "
2666 "stencil writemasks, reference values, or stencil mask values.";
Ken Russellb9f92502018-01-27 19:00:26 -08002667 }
2668 ANGLE_VALIDATION_ERR(context, InvalidOperation(), StencilReferenceMaskOrMismatch);
2669 return false;
Jamie Madillcbcde722017-01-06 14:50:00 -05002670 }
Geoff Lang3a86ad32015-09-01 11:47:05 -04002671 }
Jamie Madillac528012014-06-20 13:21:23 -04002672 }
2673
Jamie Madill427064d2018-04-13 16:20:34 -04002674 if (!ValidateFramebufferComplete(context, framebuffer))
Jamie Madill13f7d7d2014-06-20 13:21:27 -04002675 {
Geoff Langb1196682014-07-23 13:47:29 -04002676 return false;
Jamie Madill13f7d7d2014-06-20 13:21:27 -04002677 }
2678
Jamie Madill2da53562018-08-01 11:34:47 -04002679 if (context->getStateCache().hasAnyEnabledClientAttrib())
2680 {
2681 if (!ValidateDrawClientAttribs(context))
2682 {
2683 return false;
2684 }
2685 }
2686
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002687 // If we are running GLES1, there is no current program.
2688 if (context->getClientVersion() >= Version(2, 0))
Jamie Madilld4cfa572014-07-08 10:00:32 -04002689 {
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002690 gl::Program *program = state.getProgram();
2691 if (!program)
Martin Radev7cf61662017-07-26 17:10:53 +03002692 {
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002693 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ProgramNotBound);
Martin Radev7cf61662017-07-26 17:10:53 +03002694 return false;
2695 }
Martin Radev7e69f762017-07-27 14:54:13 +03002696
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002697 // In OpenGL ES spec for UseProgram at section 7.3, trying to render without
2698 // vertex shader stage or fragment shader stage is a undefined behaviour.
2699 // But ANGLE should clearly generate an INVALID_OPERATION error instead of
2700 // produce undefined result.
2701 if (!program->hasLinkedShaderStage(ShaderType::Vertex) ||
2702 !program->hasLinkedShaderStage(ShaderType::Fragment))
Martin Radev7e69f762017-07-27 14:54:13 +03002703 {
2704 context->handleError(InvalidOperation()
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002705 << "It is a undefined behaviour to render without "
2706 "vertex shader stage or fragment shader stage.");
Martin Radev7e69f762017-07-27 14:54:13 +03002707 return false;
2708 }
Martin Radevffe754b2017-07-31 10:38:07 +03002709
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002710 if (!program->validateSamplers(nullptr, context->getCaps()))
Martin Radevffe754b2017-07-31 10:38:07 +03002711 {
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002712 context->handleError(InvalidOperation());
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002713 return false;
2714 }
2715
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002716 if (extensions.multiview)
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002717 {
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002718 const int programNumViews = program->usesMultiview() ? program->getNumViews() : 1;
2719 const int framebufferNumViews = framebuffer->getNumViews();
2720 if (framebufferNumViews != programNumViews)
2721 {
2722 context->handleError(InvalidOperation()
2723 << "The number of views in the active program "
2724 "and draw framebuffer does not match.");
2725 return false;
2726 }
2727
2728 const TransformFeedback *transformFeedbackObject = state.getCurrentTransformFeedback();
2729 if (transformFeedbackObject != nullptr && transformFeedbackObject->isActive() &&
2730 framebufferNumViews > 1)
2731 {
2732 context->handleError(InvalidOperation()
2733 << "There is an active transform feedback object "
2734 "when the number of views in the active draw "
2735 "framebuffer is greater than 1.");
2736 return false;
2737 }
2738
2739 if (extensions.disjointTimerQuery && framebufferNumViews > 1 &&
2740 state.isQueryActive(QueryType::TimeElapsed))
2741 {
2742 context->handleError(InvalidOperation()
2743 << "There is an active query for target "
2744 "GL_TIME_ELAPSED_EXT when the number of "
2745 "views in the active draw framebuffer is "
2746 "greater than 1.");
2747 return false;
2748 }
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002749 }
James Darpiniane8a93c62018-01-04 18:02:24 -08002750
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002751 // Do geometry shader specific validations
2752 if (program->hasLinkedShaderStage(ShaderType::Geometry))
James Darpiniane8a93c62018-01-04 18:02:24 -08002753 {
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002754 if (!IsCompatibleDrawModeWithGeometryShader(
2755 mode, program->getGeometryShaderInputPrimitiveType()))
2756 {
2757 ANGLE_VALIDATION_ERR(context, InvalidOperation(),
2758 IncompatibleDrawModeAgainstGeometryShader);
2759 return false;
2760 }
Jamie Madilla4595b82017-01-11 17:36:34 -05002761 }
Geoff Lange0cff192017-05-30 13:04:56 -04002762
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002763 // Uniform buffer validation
2764 for (unsigned int uniformBlockIndex = 0;
2765 uniformBlockIndex < program->getActiveUniformBlockCount(); uniformBlockIndex++)
Geoff Lang9ab5b822017-05-30 16:19:23 -04002766 {
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002767 const gl::InterfaceBlock &uniformBlock =
2768 program->getUniformBlockByIndex(uniformBlockIndex);
2769 GLuint blockBinding = program->getUniformBlockBinding(uniformBlockIndex);
2770 const OffsetBindingPointer<Buffer> &uniformBuffer =
2771 state.getIndexedUniformBuffer(blockBinding);
2772
2773 if (uniformBuffer.get() == nullptr)
2774 {
2775 // undefined behaviour
2776 context->handleError(
2777 InvalidOperation()
2778 << "It is undefined behaviour to have a used but unbound uniform buffer.");
2779 return false;
2780 }
2781
2782 size_t uniformBufferSize = GetBoundBufferAvailableSize(uniformBuffer);
2783 if (uniformBufferSize < uniformBlock.dataSize)
2784 {
2785 // undefined behaviour
2786 context->handleError(
2787 InvalidOperation()
2788 << "It is undefined behaviour to use a uniform buffer that is too small.");
2789 return false;
2790 }
2791
2792 if (extensions.webglCompatibility &&
2793 uniformBuffer->isBoundForTransformFeedbackAndOtherUse())
2794 {
2795 ANGLE_VALIDATION_ERR(context, InvalidOperation(),
2796 UniformBufferBoundForTransformFeedback);
2797 return false;
2798 }
Geoff Lang9ab5b822017-05-30 16:19:23 -04002799 }
2800
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002801 // Do some additonal WebGL-specific validation
2802 if (extensions.webglCompatibility)
Geoff Lange0cff192017-05-30 13:04:56 -04002803 {
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002804 const TransformFeedback *transformFeedbackObject = state.getCurrentTransformFeedback();
2805 if (transformFeedbackObject != nullptr && transformFeedbackObject->isActive() &&
2806 transformFeedbackObject->buffersBoundForOtherUse())
2807 {
2808 ANGLE_VALIDATION_ERR(context, InvalidOperation(),
2809 TransformFeedbackBufferDoubleBound);
2810 return false;
2811 }
2812 // Detect rendering feedback loops for WebGL.
2813 if (framebuffer->formsRenderingFeedbackLoopWith(state))
2814 {
2815 ANGLE_VALIDATION_ERR(context, InvalidOperation(), FeedbackLoop);
2816 return false;
2817 }
2818
2819 // Detect that the vertex shader input types match the attribute types
2820 if (!ValidateVertexShaderAttributeTypeMatch(context))
2821 {
2822 return false;
2823 }
2824
2825 // Detect that the color buffer types match the fragment shader output types
2826 if (!ValidateFragmentShaderColorBufferTypeMatch(context))
2827 {
2828 return false;
2829 }
Jamie Madillac43aaa2018-07-31 11:22:13 -04002830
2831 if (count > 0)
2832 {
2833 const VertexArray *vao = context->getGLState().getVertexArray();
2834 if (vao->hasTransformFeedbackBindingConflict(context))
2835 {
2836 ANGLE_VALIDATION_ERR(context, InvalidOperation(),
2837 VertexBufferBoundForTransformFeedback);
2838 return false;
2839 }
2840 }
Geoff Lange0cff192017-05-30 13:04:56 -04002841 }
Jamie Madilla4595b82017-01-11 17:36:34 -05002842 }
2843
Jamie Madill9fdaa492018-02-16 10:52:11 -05002844 return true;
Jamie Madill250d33f2014-06-06 17:09:03 -04002845}
2846
Jamie Madill5b772312018-03-08 20:28:32 -05002847bool ValidateDrawArraysCommon(Context *context,
Jamie Madill493f9572018-05-24 19:52:15 -04002848 PrimitiveMode mode,
Jamie Madillc1d770e2017-04-13 17:31:24 -04002849 GLint first,
2850 GLsizei count,
2851 GLsizei primcount)
Jamie Madill250d33f2014-06-06 17:09:03 -04002852{
Jamie Madillfd716582014-06-06 17:09:04 -04002853 if (first < 0)
Jamie Madill250d33f2014-06-06 17:09:03 -04002854 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002855 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeStart);
Geoff Langb1196682014-07-23 13:47:29 -04002856 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04002857 }
2858
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002859 const State &state = context->getGLState();
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002860 gl::TransformFeedback *curTransformFeedback = state.getCurrentTransformFeedback();
He Yunchaoced53ae2016-11-29 15:00:51 +08002861 if (curTransformFeedback && curTransformFeedback->isActive() &&
James Darpinian30b604d2018-03-12 17:26:57 -07002862 !curTransformFeedback->isPaused())
Jamie Madillfd716582014-06-06 17:09:04 -04002863 {
Jiawei Shao80c32cc2018-04-25 09:48:36 +08002864 if (!ValidateTransformFeedbackPrimitiveMode(context,
2865 curTransformFeedback->getPrimitiveMode(), mode))
James Darpinian30b604d2018-03-12 17:26:57 -07002866 {
James Darpinian30b604d2018-03-12 17:26:57 -07002867 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidDrawModeTransformFeedback);
2868 return false;
2869 }
2870
2871 if (!curTransformFeedback->checkBufferSpaceForDraw(count, primcount))
2872 {
2873 ANGLE_VALIDATION_ERR(context, InvalidOperation(), TransformFeedbackBufferTooSmall);
2874 return false;
2875 }
Jamie Madillfd716582014-06-06 17:09:04 -04002876 }
2877
Jiajia Qind9671222016-11-29 16:30:31 +08002878 if (!ValidateDrawBase(context, mode, count))
Corentin Wallez18a2fb32015-08-10 12:58:14 -07002879 {
2880 return false;
2881 }
2882
Corentin Wallez71168a02016-12-19 15:11:18 -08002883 // Check the computation of maxVertex doesn't overflow.
Jamie Madill9fdaa492018-02-16 10:52:11 -05002884 // - first < 0 has been checked as an error condition.
2885 // - if count < 0, skip validating no-op draw calls.
Corentin Wallez71168a02016-12-19 15:11:18 -08002886 // From this we know maxVertex will be positive, and only need to check if it overflows GLint.
Jamie Madill9fdaa492018-02-16 10:52:11 -05002887 ASSERT(first >= 0);
Jamie Madill2da53562018-08-01 11:34:47 -04002888 if (count > 0 && primcount > 0)
Corentin Wallez92db6942016-12-09 13:10:36 -05002889 {
Jamie Madill9fdaa492018-02-16 10:52:11 -05002890 int64_t maxVertex = static_cast<int64_t>(first) + static_cast<int64_t>(count) - 1;
2891 if (maxVertex > static_cast<int64_t>(std::numeric_limits<GLint>::max()))
2892 {
2893 ANGLE_VALIDATION_ERR(context, InvalidOperation(), IntegerOverflow);
2894 return false;
2895 }
Corentin Wallez92db6942016-12-09 13:10:36 -05002896
Jamie Madill2da53562018-08-01 11:34:47 -04002897 if (!ValidateDrawAttribs(context, primcount, static_cast<GLint>(maxVertex)))
Jamie Madill9fdaa492018-02-16 10:52:11 -05002898 {
2899 return false;
2900 }
Jamie Madillfd716582014-06-06 17:09:04 -04002901 }
2902
2903 return true;
2904}
2905
He Yunchaoced53ae2016-11-29 15:00:51 +08002906bool ValidateDrawArraysInstancedANGLE(Context *context,
Jamie Madill493f9572018-05-24 19:52:15 -04002907 PrimitiveMode mode,
He Yunchaoced53ae2016-11-29 15:00:51 +08002908 GLint first,
2909 GLsizei count,
2910 GLsizei primcount)
Geoff Lang87a93302014-09-16 13:29:43 -04002911{
Geoff Lang63c5a592017-09-27 14:08:16 -04002912 if (!context->getExtensions().instancedArrays)
2913 {
2914 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
2915 return false;
2916 }
2917
Corentin Wallez170efbf2017-05-02 13:45:01 -04002918 if (!ValidateDrawArraysInstancedBase(context, mode, first, count, primcount))
Geoff Lang87a93302014-09-16 13:29:43 -04002919 {
2920 return false;
2921 }
2922
Corentin Wallez0dc97812017-06-22 14:38:44 -04002923 return ValidateDrawInstancedANGLE(context);
Geoff Lang87a93302014-09-16 13:29:43 -04002924}
2925
Jamie Madill493f9572018-05-24 19:52:15 -04002926bool ValidateDrawElementsBase(Context *context, PrimitiveMode mode, GLenum type)
Jamie Madillfd716582014-06-06 17:09:04 -04002927{
Jamie Madill250d33f2014-06-06 17:09:03 -04002928 switch (type)
2929 {
He Yunchaoced53ae2016-11-29 15:00:51 +08002930 case GL_UNSIGNED_BYTE:
2931 case GL_UNSIGNED_SHORT:
2932 break;
2933 case GL_UNSIGNED_INT:
2934 if (context->getClientMajorVersion() < 3 && !context->getExtensions().elementIndexUint)
2935 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002936 ANGLE_VALIDATION_ERR(context, InvalidEnum(), TypeNotUnsignedShortByte);
He Yunchaoced53ae2016-11-29 15:00:51 +08002937 return false;
2938 }
2939 break;
2940 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07002941 ANGLE_VALIDATION_ERR(context, InvalidEnum(), TypeNotUnsignedShortByte);
He Yunchaoced53ae2016-11-29 15:00:51 +08002942 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04002943 }
2944
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002945 const State &state = context->getGLState();
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002946
2947 gl::TransformFeedback *curTransformFeedback = state.getCurrentTransformFeedback();
He Yunchaoced53ae2016-11-29 15:00:51 +08002948 if (curTransformFeedback && curTransformFeedback->isActive() &&
2949 !curTransformFeedback->isPaused())
Jamie Madill250d33f2014-06-06 17:09:03 -04002950 {
Jiawei Shao80c32cc2018-04-25 09:48:36 +08002951 // EXT_geometry_shader allows transform feedback to work with all draw commands.
2952 // [EXT_geometry_shader] Section 12.1, "Transform Feedback"
2953 if (context->getExtensions().geometryShader)
2954 {
2955 if (!ValidateTransformFeedbackPrimitiveMode(
2956 context, curTransformFeedback->getPrimitiveMode(), mode))
2957 {
2958 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidDrawModeTransformFeedback);
2959 return false;
2960 }
2961 }
2962 else
2963 {
2964 // It is an invalid operation to call DrawElements, DrawRangeElements or
2965 // DrawElementsInstanced while transform feedback is active, (3.0.2, section 2.14, pg
2966 // 86)
2967 ANGLE_VALIDATION_ERR(context, InvalidOperation(),
2968 UnsupportedDrawModeForTransformFeedback);
2969 return false;
2970 }
Jamie Madill250d33f2014-06-06 17:09:03 -04002971 }
2972
Jiajia Qind9671222016-11-29 16:30:31 +08002973 return true;
2974}
2975
Jamie Madill5b772312018-03-08 20:28:32 -05002976bool ValidateDrawElementsCommon(Context *context,
Jamie Madill493f9572018-05-24 19:52:15 -04002977 PrimitiveMode mode,
Jamie Madill9c9b40a2017-04-26 16:31:57 -04002978 GLsizei count,
2979 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -04002980 const void *indices,
Jamie Madill9c9b40a2017-04-26 16:31:57 -04002981 GLsizei primcount)
Jiajia Qind9671222016-11-29 16:30:31 +08002982{
Jiawei Shao80c32cc2018-04-25 09:48:36 +08002983 if (!ValidateDrawElementsBase(context, mode, type))
Jiajia Qind9671222016-11-29 16:30:31 +08002984 return false;
2985
2986 const State &state = context->getGLState();
2987
Corentin Wallez170efbf2017-05-02 13:45:01 -04002988 if (!ValidateDrawBase(context, mode, count))
2989 {
2990 return false;
2991 }
2992
He Yunchaoced53ae2016-11-29 15:00:51 +08002993 const gl::VertexArray *vao = state.getVertexArray();
Jamie Madill8e344942015-07-09 14:22:07 -04002994 gl::Buffer *elementArrayBuffer = vao->getElementArrayBuffer().get();
Jamie Madilld4cfa572014-07-08 10:00:32 -04002995
Corentin Wallez3f6d4df2017-01-30 18:04:36 -05002996 GLuint typeBytes = gl::GetTypeInfo(type).bytes;
2997
2998 if (context->getExtensions().webglCompatibility)
2999 {
3000 ASSERT(isPow2(typeBytes) && typeBytes > 0);
3001 if ((reinterpret_cast<uintptr_t>(indices) & static_cast<uintptr_t>(typeBytes - 1)) != 0)
3002 {
3003 // [WebGL 1.0] Section 6.4 Buffer Offset and Stride Requirements
3004 // The offset arguments to drawElements and [...], must be a multiple of the size of the
3005 // data type passed to the call, or an INVALID_OPERATION error is generated.
Brandon Jones6cad5662017-06-14 13:25:13 -07003006 ANGLE_VALIDATION_ERR(context, InvalidOperation(), OffsetMustBeMultipleOfType);
Corentin Wallez3f6d4df2017-01-30 18:04:36 -05003007 return false;
3008 }
Corentin Wallezfe9306a2017-02-01 17:41:05 -05003009
3010 // [WebGL 1.0] Section 6.4 Buffer Offset and Stride Requirements
3011 // In addition the offset argument to drawElements must be non-negative or an INVALID_VALUE
3012 // error is generated.
3013 if (reinterpret_cast<intptr_t>(indices) < 0)
3014 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003015 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeOffset);
Corentin Wallezfe9306a2017-02-01 17:41:05 -05003016 return false;
3017 }
Geoff Langfeb8c682017-02-13 16:07:35 -05003018 }
Jamie Madillcc73f242018-08-01 11:34:48 -04003019 else if (elementArrayBuffer && elementArrayBuffer->isMapped())
3020 {
3021 // WebGL buffers cannot be mapped/unmapped because the MapBufferRange,
3022 // FlushMappedBufferRange, and UnmapBuffer entry points are removed from the WebGL 2.0 API.
3023 // https://www.khronos.org/registry/webgl/specs/latest/2.0/#5.14
3024 context->handleError(InvalidOperation() << "Index buffer is mapped.");
3025 return false;
3026 }
Geoff Langfeb8c682017-02-13 16:07:35 -05003027
3028 if (context->getExtensions().webglCompatibility ||
3029 !context->getGLState().areClientArraysEnabled())
3030 {
Brandon Jones2a018152018-06-08 15:59:26 -07003031 if (!elementArrayBuffer)
Corentin Wallez3f6d4df2017-01-30 18:04:36 -05003032 {
3033 // [WebGL 1.0] Section 6.2 No Client Side Arrays
Brandon Jones2a018152018-06-08 15:59:26 -07003034 // If an indexed draw command (drawElements) is called and no WebGLBuffer is bound to
3035 // the ELEMENT_ARRAY_BUFFER binding point, an INVALID_OPERATION error is generated.
Brandon Jones6cad5662017-06-14 13:25:13 -07003036 ANGLE_VALIDATION_ERR(context, InvalidOperation(), MustHaveElementArrayBinding);
Corentin Wallez3f6d4df2017-01-30 18:04:36 -05003037 return false;
3038 }
3039 }
3040
Jamie Madill9fdaa492018-02-16 10:52:11 -05003041 if (count > 0 && !elementArrayBuffer && !indices)
Jamie Madillae3000b2014-08-25 15:47:51 -04003042 {
Jamie Madill9fdaa492018-02-16 10:52:11 -05003043 // This is an application error that would normally result in a crash, but we catch it and
3044 // return an error
3045 context->handleError(InvalidOperation() << "No element array buffer and no pointer.");
3046 return false;
3047 }
3048
3049 if (count > 0 && elementArrayBuffer)
3050 {
3051 // The max possible type size is 8 and count is on 32 bits so doing the multiplication
3052 // in a 64 bit integer is safe. Also we are guaranteed that here count > 0.
3053 static_assert(std::is_same<int, GLsizei>::value, "GLsizei isn't the expected type");
3054 constexpr uint64_t kMaxTypeSize = 8;
3055 constexpr uint64_t kIntMax = std::numeric_limits<int>::max();
3056 constexpr uint64_t kUint64Max = std::numeric_limits<uint64_t>::max();
3057 static_assert(kIntMax < kUint64Max / kMaxTypeSize, "");
3058
3059 uint64_t typeSize = typeBytes;
3060 uint64_t elementCount = static_cast<uint64_t>(count);
3061 ASSERT(elementCount > 0 && typeSize <= kMaxTypeSize);
3062
3063 // Doing the multiplication here is overflow-safe
3064 uint64_t elementDataSizeNoOffset = typeSize * elementCount;
3065
3066 // The offset can be any value, check for overflows
3067 uint64_t offset = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(indices));
3068 if (elementDataSizeNoOffset > kUint64Max - offset)
Jamie Madillae3000b2014-08-25 15:47:51 -04003069 {
Jamie Madill9fdaa492018-02-16 10:52:11 -05003070 ANGLE_VALIDATION_ERR(context, InvalidOperation(), IntegerOverflow);
3071 return false;
Corentin Wallez0844f2d2017-01-31 17:02:59 -05003072 }
Jamie Madill9fdaa492018-02-16 10:52:11 -05003073
3074 uint64_t elementDataSizeWithOffset = elementDataSizeNoOffset + offset;
3075 if (elementDataSizeWithOffset > static_cast<uint64_t>(elementArrayBuffer->getSize()))
Corentin Wallez0844f2d2017-01-31 17:02:59 -05003076 {
Jamie Madill9fdaa492018-02-16 10:52:11 -05003077 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InsufficientBufferSize);
3078 return false;
3079 }
3080
3081 ASSERT(isPow2(typeSize) && typeSize > 0);
3082 if ((elementArrayBuffer->getSize() & (typeSize - 1)) != 0)
3083 {
3084 ANGLE_VALIDATION_ERR(context, InvalidOperation(), MismatchedByteCountType);
Geoff Langb1196682014-07-23 13:47:29 -04003085 return false;
Jamie Madillae3000b2014-08-25 15:47:51 -04003086 }
James Darpiniane8a93c62018-01-04 18:02:24 -08003087
3088 if (context->getExtensions().webglCompatibility &&
3089 elementArrayBuffer->isBoundForTransformFeedbackAndOtherUse())
3090 {
3091 ANGLE_VALIDATION_ERR(context, InvalidOperation(),
3092 ElementArrayBufferBoundForTransformFeedback);
3093 return false;
3094 }
Jamie Madillae3000b2014-08-25 15:47:51 -04003095 }
3096
Jamie Madill2da53562018-08-01 11:34:47 -04003097 if (!context->getExtensions().robustBufferAccessBehavior && count > 0 && primcount > 0)
Jiajia Qin8a7b3a02017-08-25 16:05:48 +08003098 {
3099 // Use the parameter buffer to retrieve and cache the index range.
Jamie Madill6f5444d2018-03-14 10:08:11 -04003100 const DrawCallParams &params = context->getParams<DrawCallParams>();
3101 ANGLE_VALIDATION_TRY(params.ensureIndexRangeResolved(context));
3102 const IndexRange &indexRange = params.getIndexRange();
Jiajia Qin8a7b3a02017-08-25 16:05:48 +08003103
3104 // If we use an index greater than our maximum supported index range, return an error.
3105 // The ES3 spec does not specify behaviour here, it is undefined, but ANGLE should always
3106 // return an error if possible here.
Jamie Madill6f5444d2018-03-14 10:08:11 -04003107 if (static_cast<GLuint64>(indexRange.end) >= context->getCaps().maxElementIndex)
Jiajia Qin8a7b3a02017-08-25 16:05:48 +08003108 {
3109 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExceedsMaxElement);
3110 return false;
3111 }
3112
Jamie Madill2da53562018-08-01 11:34:47 -04003113 if (!ValidateDrawAttribs(context, primcount, static_cast<GLint>(indexRange.end)))
Jiajia Qin8a7b3a02017-08-25 16:05:48 +08003114 {
3115 return false;
3116 }
3117
3118 // No op if there are no real indices in the index data (all are primitive restart).
Jamie Madill6f5444d2018-03-14 10:08:11 -04003119 return (indexRange.vertexIndexCount > 0);
Corentin Wallezc1346fb2017-08-24 16:11:26 +00003120 }
3121
Jiajia Qin8a7b3a02017-08-25 16:05:48 +08003122 return true;
Jamie Madillfd716582014-06-06 17:09:04 -04003123}
3124
Jamie Madill5b772312018-03-08 20:28:32 -05003125bool ValidateDrawElementsInstancedCommon(Context *context,
Jamie Madill493f9572018-05-24 19:52:15 -04003126 PrimitiveMode mode,
Jamie Madill9c9b40a2017-04-26 16:31:57 -04003127 GLsizei count,
3128 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -04003129 const void *indices,
Jamie Madill9c9b40a2017-04-26 16:31:57 -04003130 GLsizei primcount)
Jamie Madillfd716582014-06-06 17:09:04 -04003131{
Corentin Wallez0dc97812017-06-22 14:38:44 -04003132 return ValidateDrawElementsInstancedBase(context, mode, count, type, indices, primcount);
Jamie Madill250d33f2014-06-06 17:09:03 -04003133}
3134
Geoff Lang3edfe032015-09-04 16:38:24 -04003135bool ValidateDrawElementsInstancedANGLE(Context *context,
Jamie Madill493f9572018-05-24 19:52:15 -04003136 PrimitiveMode mode,
Geoff Lang3edfe032015-09-04 16:38:24 -04003137 GLsizei count,
3138 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -04003139 const void *indices,
Jamie Madill9c9b40a2017-04-26 16:31:57 -04003140 GLsizei primcount)
Geoff Lang87a93302014-09-16 13:29:43 -04003141{
Geoff Lang63c5a592017-09-27 14:08:16 -04003142 if (!context->getExtensions().instancedArrays)
3143 {
3144 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
3145 return false;
3146 }
3147
Corentin Wallez170efbf2017-05-02 13:45:01 -04003148 if (!ValidateDrawElementsInstancedBase(context, mode, count, type, indices, primcount))
Geoff Lang87a93302014-09-16 13:29:43 -04003149 {
3150 return false;
3151 }
3152
Corentin Wallez0dc97812017-06-22 14:38:44 -04003153 return ValidateDrawInstancedANGLE(context);
Geoff Lang87a93302014-09-16 13:29:43 -04003154}
3155
He Yunchaoced53ae2016-11-29 15:00:51 +08003156bool ValidateFramebufferTextureBase(Context *context,
3157 GLenum target,
3158 GLenum attachment,
3159 GLuint texture,
3160 GLint level)
Jamie Madill570f7c82014-07-03 10:38:54 -04003161{
Geoff Lange8afa902017-09-27 15:00:43 -04003162 if (!ValidFramebufferTarget(context, target))
Jamie Madill55ec3b12014-07-03 10:38:57 -04003163 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003164 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidFramebufferTarget);
Geoff Langb1196682014-07-23 13:47:29 -04003165 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04003166 }
3167
3168 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill570f7c82014-07-03 10:38:54 -04003169 {
3170 return false;
3171 }
3172
Jamie Madill55ec3b12014-07-03 10:38:57 -04003173 if (texture != 0)
3174 {
3175 gl::Texture *tex = context->getTexture(texture);
3176
Luc Ferronadcf0ae2018-01-24 08:27:37 -05003177 if (tex == nullptr)
Jamie Madill55ec3b12014-07-03 10:38:57 -04003178 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003179 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04003180 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04003181 }
3182
3183 if (level < 0)
3184 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003185 context->handleError(InvalidValue());
Geoff Langb1196682014-07-23 13:47:29 -04003186 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04003187 }
3188 }
3189
Jamie Madilldfde6ab2016-06-09 07:07:18 -07003190 const gl::Framebuffer *framebuffer = context->getGLState().getTargetFramebuffer(target);
Jamie Madill84115c92015-04-23 15:00:07 -04003191 ASSERT(framebuffer);
Jamie Madill55ec3b12014-07-03 10:38:57 -04003192
Jamie Madill84115c92015-04-23 15:00:07 -04003193 if (framebuffer->id() == 0)
Jamie Madill55ec3b12014-07-03 10:38:57 -04003194 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003195 ANGLE_VALIDATION_ERR(context, InvalidOperation(), DefaultFramebufferTarget);
Geoff Langb1196682014-07-23 13:47:29 -04003196 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04003197 }
3198
3199 return true;
3200}
3201
Geoff Langb1196682014-07-23 13:47:29 -04003202bool ValidateGetUniformBase(Context *context, GLuint program, GLint location)
Jamie Madill0063c512014-08-25 15:47:53 -04003203{
3204 if (program == 0)
3205 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003206 context->handleError(InvalidValue());
Geoff Langb1196682014-07-23 13:47:29 -04003207 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04003208 }
3209
Dian Xiang769769a2015-09-09 15:20:08 -07003210 gl::Program *programObject = GetValidProgram(context, program);
3211 if (!programObject)
Shannon Woods4de4fd62014-11-07 16:22:02 -05003212 {
3213 return false;
3214 }
3215
Jamie Madill0063c512014-08-25 15:47:53 -04003216 if (!programObject || !programObject->isLinked())
3217 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003218 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ProgramNotLinked);
Geoff Langb1196682014-07-23 13:47:29 -04003219 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04003220 }
3221
Geoff Lang7dd2e102014-11-10 15:19:26 -05003222 if (!programObject->isValidUniformLocation(location))
Jamie Madill549c7fd2014-08-25 15:47:56 -04003223 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003224 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04003225 return false;
Jamie Madill549c7fd2014-08-25 15:47:56 -04003226 }
3227
Jamie Madill0063c512014-08-25 15:47:53 -04003228 return true;
3229}
3230
Geoff Langf41d0ee2016-10-07 13:04:23 -04003231static bool ValidateSizedGetUniform(Context *context,
3232 GLuint program,
3233 GLint location,
3234 GLsizei bufSize,
3235 GLsizei *length)
Jamie Madill78f41802014-08-25 15:47:55 -04003236{
Geoff Langf41d0ee2016-10-07 13:04:23 -04003237 if (length)
3238 {
3239 *length = 0;
3240 }
3241
Jamie Madill78f41802014-08-25 15:47:55 -04003242 if (!ValidateGetUniformBase(context, program, location))
Jamie Madill0063c512014-08-25 15:47:53 -04003243 {
Jamie Madill78f41802014-08-25 15:47:55 -04003244 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04003245 }
3246
Geoff Langf41d0ee2016-10-07 13:04:23 -04003247 if (bufSize < 0)
3248 {
Brandon Jonesafa75152017-07-21 13:11:29 -07003249 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeBufferSize);
Geoff Langf41d0ee2016-10-07 13:04:23 -04003250 return false;
3251 }
3252
Jamie Madilla502c742014-08-28 17:19:13 -04003253 gl::Program *programObject = context->getProgram(program);
3254 ASSERT(programObject);
Jamie Madill0063c512014-08-25 15:47:53 -04003255
Jamie Madill78f41802014-08-25 15:47:55 -04003256 // sized queries -- ensure the provided buffer is large enough
Jamie Madill62d31cb2015-09-11 13:25:51 -04003257 const LinkedUniform &uniform = programObject->getUniformByLocation(location);
He Yunchaoced53ae2016-11-29 15:00:51 +08003258 size_t requiredBytes = VariableExternalSize(uniform.type);
Jamie Madill78f41802014-08-25 15:47:55 -04003259 if (static_cast<size_t>(bufSize) < requiredBytes)
Jamie Madill0063c512014-08-25 15:47:53 -04003260 {
Brandon Jonesafa75152017-07-21 13:11:29 -07003261 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InsufficientBufferSize);
Geoff Langb1196682014-07-23 13:47:29 -04003262 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04003263 }
3264
Geoff Langf41d0ee2016-10-07 13:04:23 -04003265 if (length)
3266 {
Geoff Lang94177fb2016-11-14 16:12:26 -05003267 *length = VariableComponentCount(uniform.type);
Geoff Langf41d0ee2016-10-07 13:04:23 -04003268 }
3269
Jamie Madill0063c512014-08-25 15:47:53 -04003270 return true;
3271}
3272
He Yunchaoced53ae2016-11-29 15:00:51 +08003273bool ValidateGetnUniformfvEXT(Context *context,
3274 GLuint program,
3275 GLint location,
3276 GLsizei bufSize,
3277 GLfloat *params)
Jamie Madill0063c512014-08-25 15:47:53 -04003278{
Geoff Langf41d0ee2016-10-07 13:04:23 -04003279 return ValidateSizedGetUniform(context, program, location, bufSize, nullptr);
Jamie Madill0063c512014-08-25 15:47:53 -04003280}
3281
Brandon Jonesfe4bbe62018-04-06 13:50:14 -07003282bool ValidateGetnUniformfvRobustANGLE(Context *context,
3283 GLuint program,
3284 GLint location,
3285 GLsizei bufSize,
3286 GLsizei *length,
3287 GLfloat *params)
3288{
3289 UNIMPLEMENTED();
3290 return false;
3291}
3292
He Yunchaoced53ae2016-11-29 15:00:51 +08003293bool ValidateGetnUniformivEXT(Context *context,
3294 GLuint program,
3295 GLint location,
3296 GLsizei bufSize,
3297 GLint *params)
Jamie Madill0063c512014-08-25 15:47:53 -04003298{
Geoff Langf41d0ee2016-10-07 13:04:23 -04003299 return ValidateSizedGetUniform(context, program, location, bufSize, nullptr);
3300}
3301
Brandon Jonesfe4bbe62018-04-06 13:50:14 -07003302bool ValidateGetnUniformivRobustANGLE(Context *context,
3303 GLuint program,
3304 GLint location,
3305 GLsizei bufSize,
3306 GLsizei *length,
3307 GLint *params)
3308{
3309 UNIMPLEMENTED();
3310 return false;
3311}
3312
3313bool ValidateGetnUniformuivRobustANGLE(Context *context,
3314 GLuint program,
3315 GLint location,
3316 GLsizei bufSize,
3317 GLsizei *length,
3318 GLuint *params)
3319{
3320 UNIMPLEMENTED();
3321 return false;
3322}
3323
Geoff Langf41d0ee2016-10-07 13:04:23 -04003324bool ValidateGetUniformfvRobustANGLE(Context *context,
3325 GLuint program,
3326 GLint location,
3327 GLsizei bufSize,
3328 GLsizei *length,
3329 GLfloat *params)
3330{
3331 if (!ValidateRobustEntryPoint(context, bufSize))
3332 {
3333 return false;
3334 }
3335
Brandon Jonesd1049182018-03-28 10:02:20 -07003336 GLsizei writeLength = 0;
3337
Geoff Langf41d0ee2016-10-07 13:04:23 -04003338 // bufSize is validated in ValidateSizedGetUniform
Brandon Jonesd1049182018-03-28 10:02:20 -07003339 if (!ValidateSizedGetUniform(context, program, location, bufSize, &writeLength))
3340 {
3341 return false;
3342 }
3343
3344 SetRobustLengthParam(length, writeLength);
3345
3346 return true;
Geoff Langf41d0ee2016-10-07 13:04:23 -04003347}
3348
3349bool ValidateGetUniformivRobustANGLE(Context *context,
3350 GLuint program,
3351 GLint location,
3352 GLsizei bufSize,
3353 GLsizei *length,
3354 GLint *params)
3355{
3356 if (!ValidateRobustEntryPoint(context, bufSize))
3357 {
3358 return false;
3359 }
3360
Brandon Jonesd1049182018-03-28 10:02:20 -07003361 GLsizei writeLength = 0;
3362
Geoff Langf41d0ee2016-10-07 13:04:23 -04003363 // bufSize is validated in ValidateSizedGetUniform
Brandon Jonesd1049182018-03-28 10:02:20 -07003364 if (!ValidateSizedGetUniform(context, program, location, bufSize, &writeLength))
3365 {
3366 return false;
3367 }
3368
3369 SetRobustLengthParam(length, writeLength);
3370
3371 return true;
Geoff Langf41d0ee2016-10-07 13:04:23 -04003372}
3373
3374bool ValidateGetUniformuivRobustANGLE(Context *context,
3375 GLuint program,
3376 GLint location,
3377 GLsizei bufSize,
3378 GLsizei *length,
3379 GLuint *params)
3380{
3381 if (!ValidateRobustEntryPoint(context, bufSize))
3382 {
3383 return false;
3384 }
3385
3386 if (context->getClientMajorVersion() < 3)
3387 {
Yunchao Hef0fd87d2017-09-12 04:55:05 +08003388 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ES3Required);
Geoff Langf41d0ee2016-10-07 13:04:23 -04003389 return false;
3390 }
3391
Brandon Jonesd1049182018-03-28 10:02:20 -07003392 GLsizei writeLength = 0;
3393
Geoff Langf41d0ee2016-10-07 13:04:23 -04003394 // bufSize is validated in ValidateSizedGetUniform
Brandon Jonesd1049182018-03-28 10:02:20 -07003395 if (!ValidateSizedGetUniform(context, program, location, bufSize, &writeLength))
3396 {
3397 return false;
3398 }
3399
3400 SetRobustLengthParam(length, writeLength);
3401
3402 return true;
Jamie Madill0063c512014-08-25 15:47:53 -04003403}
3404
He Yunchaoced53ae2016-11-29 15:00:51 +08003405bool ValidateDiscardFramebufferBase(Context *context,
3406 GLenum target,
3407 GLsizei numAttachments,
3408 const GLenum *attachments,
3409 bool defaultFramebuffer)
Austin Kinross08332632015-05-05 13:35:47 -07003410{
3411 if (numAttachments < 0)
3412 {
Brandon Jonesafa75152017-07-21 13:11:29 -07003413 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeAttachments);
Austin Kinross08332632015-05-05 13:35:47 -07003414 return false;
3415 }
3416
3417 for (GLsizei i = 0; i < numAttachments; ++i)
3418 {
Olli Etuaho84c9f592016-03-09 14:37:25 +02003419 if (attachments[i] >= GL_COLOR_ATTACHMENT0 && attachments[i] <= GL_COLOR_ATTACHMENT31)
Austin Kinross08332632015-05-05 13:35:47 -07003420 {
3421 if (defaultFramebuffer)
3422 {
Brandon Jonesafa75152017-07-21 13:11:29 -07003423 ANGLE_VALIDATION_ERR(context, InvalidEnum(), DefaultFramebufferInvalidAttachment);
Austin Kinross08332632015-05-05 13:35:47 -07003424 return false;
3425 }
3426
3427 if (attachments[i] >= GL_COLOR_ATTACHMENT0 + context->getCaps().maxColorAttachments)
3428 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003429 context->handleError(InvalidOperation() << "Requested color attachment is "
3430 "greater than the maximum supported "
3431 "color attachments");
Austin Kinross08332632015-05-05 13:35:47 -07003432 return false;
3433 }
3434 }
3435 else
3436 {
3437 switch (attachments[i])
3438 {
He Yunchaoced53ae2016-11-29 15:00:51 +08003439 case GL_DEPTH_ATTACHMENT:
3440 case GL_STENCIL_ATTACHMENT:
3441 case GL_DEPTH_STENCIL_ATTACHMENT:
3442 if (defaultFramebuffer)
3443 {
Brandon Jonesafa75152017-07-21 13:11:29 -07003444 ANGLE_VALIDATION_ERR(context, InvalidEnum(),
3445 DefaultFramebufferInvalidAttachment);
He Yunchaoced53ae2016-11-29 15:00:51 +08003446 return false;
3447 }
3448 break;
3449 case GL_COLOR:
3450 case GL_DEPTH:
3451 case GL_STENCIL:
3452 if (!defaultFramebuffer)
3453 {
Brandon Jonesafa75152017-07-21 13:11:29 -07003454 ANGLE_VALIDATION_ERR(context, InvalidEnum(),
3455 DefaultFramebufferInvalidAttachment);
He Yunchaoced53ae2016-11-29 15:00:51 +08003456 return false;
3457 }
3458 break;
3459 default:
Brandon Jonesafa75152017-07-21 13:11:29 -07003460 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidAttachment);
Austin Kinross08332632015-05-05 13:35:47 -07003461 return false;
Austin Kinross08332632015-05-05 13:35:47 -07003462 }
3463 }
3464 }
3465
3466 return true;
3467}
3468
Austin Kinross6ee1e782015-05-29 17:05:37 -07003469bool ValidateInsertEventMarkerEXT(Context *context, GLsizei length, const char *marker)
3470{
Jamie Madill007530e2017-12-28 14:27:04 -05003471 if (!context->getExtensions().debugMarker)
3472 {
3473 // The debug marker calls should not set error state
3474 // However, it seems reasonable to set an error state if the extension is not enabled
3475 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
3476 return false;
3477 }
Austin Kinross6ee1e782015-05-29 17:05:37 -07003478
Jamie Madill007530e2017-12-28 14:27:04 -05003479 // Note that debug marker calls must not set error state
Austin Kinross6ee1e782015-05-29 17:05:37 -07003480 if (length < 0)
3481 {
3482 return false;
3483 }
3484
3485 if (marker == nullptr)
3486 {
3487 return false;
3488 }
3489
3490 return true;
3491}
3492
3493bool ValidatePushGroupMarkerEXT(Context *context, GLsizei length, const char *marker)
3494{
Jamie Madill007530e2017-12-28 14:27:04 -05003495 if (!context->getExtensions().debugMarker)
3496 {
3497 // The debug marker calls should not set error state
3498 // However, it seems reasonable to set an error state if the extension is not enabled
3499 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
3500 return false;
3501 }
Austin Kinross6ee1e782015-05-29 17:05:37 -07003502
Jamie Madill007530e2017-12-28 14:27:04 -05003503 // Note that debug marker calls must not set error state
Austin Kinross6ee1e782015-05-29 17:05:37 -07003504 if (length < 0)
3505 {
3506 return false;
3507 }
3508
3509 if (length > 0 && marker == nullptr)
3510 {
3511 return false;
3512 }
3513
3514 return true;
3515}
3516
Corentin Wallezf0e89be2017-11-08 14:00:32 -08003517bool ValidateEGLImageTargetTexture2DOES(Context *context, TextureType type, GLeglImageOES image)
Geoff Langdcab33b2015-07-21 13:03:16 -04003518{
Geoff Langa8406172015-07-21 16:53:39 -04003519 if (!context->getExtensions().eglImage && !context->getExtensions().eglImageExternal)
3520 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003521 context->handleError(InvalidOperation());
Geoff Langa8406172015-07-21 16:53:39 -04003522 return false;
3523 }
3524
Corentin Wallezf0e89be2017-11-08 14:00:32 -08003525 switch (type)
Geoff Langa8406172015-07-21 16:53:39 -04003526 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08003527 case TextureType::_2D:
Geoff Langb66a9092016-05-16 15:59:14 -04003528 if (!context->getExtensions().eglImage)
3529 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003530 context->handleError(InvalidEnum()
3531 << "GL_TEXTURE_2D texture target requires GL_OES_EGL_image.");
Geoff Langb66a9092016-05-16 15:59:14 -04003532 }
3533 break;
3534
Corentin Wallezf0e89be2017-11-08 14:00:32 -08003535 case TextureType::External:
Geoff Langb66a9092016-05-16 15:59:14 -04003536 if (!context->getExtensions().eglImageExternal)
3537 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003538 context->handleError(InvalidEnum() << "GL_TEXTURE_EXTERNAL_OES texture target "
3539 "requires GL_OES_EGL_image_external.");
Geoff Langb66a9092016-05-16 15:59:14 -04003540 }
Geoff Langa8406172015-07-21 16:53:39 -04003541 break;
3542
3543 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07003544 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTextureTarget);
Geoff Langa8406172015-07-21 16:53:39 -04003545 return false;
3546 }
3547
Rafael Cintron05a449a2018-06-20 18:08:04 -07003548 egl::Image *imageObject = static_cast<egl::Image *>(image);
Jamie Madill007530e2017-12-28 14:27:04 -05003549
Jamie Madill61e16b42017-06-19 11:13:23 -04003550 ASSERT(context->getCurrentDisplay());
Jamie Madill007530e2017-12-28 14:27:04 -05003551 if (!context->getCurrentDisplay()->isValidImage(imageObject))
Geoff Langa8406172015-07-21 16:53:39 -04003552 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003553 context->handleError(InvalidValue() << "EGL image is not valid.");
Geoff Langa8406172015-07-21 16:53:39 -04003554 return false;
3555 }
3556
Jamie Madill007530e2017-12-28 14:27:04 -05003557 if (imageObject->getSamples() > 0)
Geoff Langa8406172015-07-21 16:53:39 -04003558 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003559 context->handleError(InvalidOperation()
3560 << "cannot create a 2D texture from a multisampled EGL image.");
Geoff Langa8406172015-07-21 16:53:39 -04003561 return false;
3562 }
3563
Geoff Langca271392017-04-05 12:30:00 -04003564 const TextureCaps &textureCaps =
Jamie Madill007530e2017-12-28 14:27:04 -05003565 context->getTextureCaps().get(imageObject->getFormat().info->sizedInternalFormat);
Geoff Langa8406172015-07-21 16:53:39 -04003566 if (!textureCaps.texturable)
3567 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003568 context->handleError(InvalidOperation()
3569 << "EGL image internal format is not supported as a texture.");
Geoff Langa8406172015-07-21 16:53:39 -04003570 return false;
3571 }
3572
Geoff Langdcab33b2015-07-21 13:03:16 -04003573 return true;
3574}
3575
3576bool ValidateEGLImageTargetRenderbufferStorageOES(Context *context,
Geoff Langdcab33b2015-07-21 13:03:16 -04003577 GLenum target,
Jamie Madill007530e2017-12-28 14:27:04 -05003578 GLeglImageOES image)
Geoff Langdcab33b2015-07-21 13:03:16 -04003579{
Geoff Langa8406172015-07-21 16:53:39 -04003580 if (!context->getExtensions().eglImage)
3581 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003582 context->handleError(InvalidOperation());
Geoff Langa8406172015-07-21 16:53:39 -04003583 return false;
3584 }
3585
3586 switch (target)
3587 {
3588 case GL_RENDERBUFFER:
3589 break;
3590
3591 default:
Brandon Jonesafa75152017-07-21 13:11:29 -07003592 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidRenderbufferTarget);
Geoff Langa8406172015-07-21 16:53:39 -04003593 return false;
3594 }
3595
Rafael Cintron05a449a2018-06-20 18:08:04 -07003596 egl::Image *imageObject = static_cast<egl::Image *>(image);
Jamie Madill007530e2017-12-28 14:27:04 -05003597
Jamie Madill61e16b42017-06-19 11:13:23 -04003598 ASSERT(context->getCurrentDisplay());
Jamie Madill007530e2017-12-28 14:27:04 -05003599 if (!context->getCurrentDisplay()->isValidImage(imageObject))
Geoff Langa8406172015-07-21 16:53:39 -04003600 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003601 context->handleError(InvalidValue() << "EGL image is not valid.");
Geoff Langa8406172015-07-21 16:53:39 -04003602 return false;
3603 }
3604
Geoff Langca271392017-04-05 12:30:00 -04003605 const TextureCaps &textureCaps =
Jamie Madill007530e2017-12-28 14:27:04 -05003606 context->getTextureCaps().get(imageObject->getFormat().info->sizedInternalFormat);
Yuly Novikovf15f8862018-06-04 18:59:41 -04003607 if (!textureCaps.renderbuffer)
Geoff Langa8406172015-07-21 16:53:39 -04003608 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003609 context->handleError(InvalidOperation()
3610 << "EGL image internal format is not supported as a renderbuffer.");
Geoff Langa8406172015-07-21 16:53:39 -04003611 return false;
3612 }
3613
Geoff Langdcab33b2015-07-21 13:03:16 -04003614 return true;
3615}
Austin Kinrossbc781f32015-10-26 09:27:38 -07003616
3617bool ValidateBindVertexArrayBase(Context *context, GLuint array)
3618{
Geoff Lang36167ab2015-12-07 10:27:14 -05003619 if (!context->isVertexArrayGenerated(array))
Austin Kinrossbc781f32015-10-26 09:27:38 -07003620 {
3621 // The default VAO should always exist
3622 ASSERT(array != 0);
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003623 context->handleError(InvalidOperation());
Austin Kinrossbc781f32015-10-26 09:27:38 -07003624 return false;
3625 }
3626
3627 return true;
3628}
3629
Geoff Langc5629752015-12-07 16:29:04 -05003630bool ValidateProgramBinaryBase(Context *context,
3631 GLuint program,
3632 GLenum binaryFormat,
3633 const void *binary,
3634 GLint length)
3635{
3636 Program *programObject = GetValidProgram(context, program);
3637 if (programObject == nullptr)
3638 {
3639 return false;
3640 }
3641
3642 const std::vector<GLenum> &programBinaryFormats = context->getCaps().programBinaryFormats;
3643 if (std::find(programBinaryFormats.begin(), programBinaryFormats.end(), binaryFormat) ==
3644 programBinaryFormats.end())
3645 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003646 context->handleError(InvalidEnum() << "Program binary format is not valid.");
Geoff Langc5629752015-12-07 16:29:04 -05003647 return false;
3648 }
3649
Olli Etuahoc3e55a42016-03-09 16:29:18 +02003650 if (context->hasActiveTransformFeedback(program))
3651 {
3652 // ES 3.0.4 section 2.15 page 91
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003653 context->handleError(InvalidOperation() << "Cannot change program binary while program "
3654 "is associated with an active transform "
3655 "feedback object.");
Olli Etuahoc3e55a42016-03-09 16:29:18 +02003656 return false;
3657 }
3658
Geoff Langc5629752015-12-07 16:29:04 -05003659 return true;
3660}
3661
3662bool ValidateGetProgramBinaryBase(Context *context,
3663 GLuint program,
3664 GLsizei bufSize,
3665 GLsizei *length,
3666 GLenum *binaryFormat,
3667 void *binary)
3668{
3669 Program *programObject = GetValidProgram(context, program);
3670 if (programObject == nullptr)
3671 {
3672 return false;
3673 }
3674
3675 if (!programObject->isLinked())
3676 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003677 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ProgramNotLinked);
Geoff Langc5629752015-12-07 16:29:04 -05003678 return false;
3679 }
3680
Jamie Madilla7d12dc2016-12-13 15:08:19 -05003681 if (context->getCaps().programBinaryFormats.empty())
3682 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003683 context->handleError(InvalidOperation() << "No program binary formats supported.");
Jamie Madilla7d12dc2016-12-13 15:08:19 -05003684 return false;
3685 }
3686
Geoff Langc5629752015-12-07 16:29:04 -05003687 return true;
3688}
Jamie Madillc29968b2016-01-20 11:17:23 -05003689
Jamie Madill5b772312018-03-08 20:28:32 -05003690bool ValidateDrawBuffersBase(Context *context, GLsizei n, const GLenum *bufs)
Jamie Madillc29968b2016-01-20 11:17:23 -05003691{
3692 // INVALID_VALUE is generated if n is negative or greater than value of MAX_DRAW_BUFFERS
Brandon Jonesafa75152017-07-21 13:11:29 -07003693 if (n < 0)
Jamie Madillc29968b2016-01-20 11:17:23 -05003694 {
Brandon Jonesafa75152017-07-21 13:11:29 -07003695 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeCount);
3696 return false;
3697 }
3698 if (static_cast<GLuint>(n) > context->getCaps().maxDrawBuffers)
3699 {
3700 ANGLE_VALIDATION_ERR(context, InvalidValue(), IndexExceedsMaxDrawBuffer);
Jamie Madillc29968b2016-01-20 11:17:23 -05003701 return false;
3702 }
3703
Jamie Madilldfde6ab2016-06-09 07:07:18 -07003704 ASSERT(context->getGLState().getDrawFramebuffer());
3705 GLuint frameBufferId = context->getGLState().getDrawFramebuffer()->id();
Jamie Madillc29968b2016-01-20 11:17:23 -05003706 GLuint maxColorAttachment = GL_COLOR_ATTACHMENT0_EXT + context->getCaps().maxColorAttachments;
3707
3708 // This should come first before the check for the default frame buffer
3709 // because when we switch to ES3.1+, invalid enums will return INVALID_ENUM
3710 // rather than INVALID_OPERATION
3711 for (int colorAttachment = 0; colorAttachment < n; colorAttachment++)
3712 {
3713 const GLenum attachment = GL_COLOR_ATTACHMENT0_EXT + colorAttachment;
3714
3715 if (bufs[colorAttachment] != GL_NONE && bufs[colorAttachment] != GL_BACK &&
Olli Etuaho84c9f592016-03-09 14:37:25 +02003716 (bufs[colorAttachment] < GL_COLOR_ATTACHMENT0 ||
3717 bufs[colorAttachment] > GL_COLOR_ATTACHMENT31))
Jamie Madillc29968b2016-01-20 11:17:23 -05003718 {
3719 // Value in bufs is not NONE, BACK, or GL_COLOR_ATTACHMENTi
Olli Etuaho84c9f592016-03-09 14:37:25 +02003720 // The 3.0.4 spec says to generate GL_INVALID_OPERATION here, but this
3721 // was changed to GL_INVALID_ENUM in 3.1, which dEQP also expects.
3722 // 3.1 is still a bit ambiguous about the error, but future specs are
3723 // expected to clarify that GL_INVALID_ENUM is the correct error.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003724 context->handleError(InvalidEnum() << "Invalid buffer value");
Olli Etuaho84c9f592016-03-09 14:37:25 +02003725 return false;
3726 }
3727 else if (bufs[colorAttachment] >= maxColorAttachment)
3728 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003729 context->handleError(InvalidOperation()
3730 << "Buffer value is greater than MAX_DRAW_BUFFERS");
Jamie Madillc29968b2016-01-20 11:17:23 -05003731 return false;
3732 }
3733 else if (bufs[colorAttachment] != GL_NONE && bufs[colorAttachment] != attachment &&
3734 frameBufferId != 0)
3735 {
3736 // INVALID_OPERATION-GL is bound to buffer and ith argument
3737 // is not COLOR_ATTACHMENTi or NONE
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003738 context->handleError(InvalidOperation()
3739 << "Ith value does not match COLOR_ATTACHMENTi or NONE");
Jamie Madillc29968b2016-01-20 11:17:23 -05003740 return false;
3741 }
3742 }
3743
3744 // INVALID_OPERATION is generated if GL is bound to the default framebuffer
3745 // and n is not 1 or bufs is bound to value other than BACK and NONE
3746 if (frameBufferId == 0)
3747 {
3748 if (n != 1)
3749 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003750 context->handleError(InvalidOperation()
3751 << "n must be 1 when GL is bound to the default framebuffer");
Jamie Madillc29968b2016-01-20 11:17:23 -05003752 return false;
3753 }
3754
3755 if (bufs[0] != GL_NONE && bufs[0] != GL_BACK)
3756 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003757 context->handleError(
3758 InvalidOperation()
3759 << "Only NONE or BACK are valid values when drawing to the default framebuffer");
Jamie Madillc29968b2016-01-20 11:17:23 -05003760 return false;
3761 }
3762 }
3763
3764 return true;
3765}
3766
Geoff Lang496c02d2016-10-20 11:38:11 -07003767bool ValidateGetBufferPointervBase(Context *context,
Corentin Wallez336129f2017-10-17 15:55:40 -04003768 BufferBinding target,
Geoff Lang496c02d2016-10-20 11:38:11 -07003769 GLenum pname,
3770 GLsizei *length,
3771 void **params)
Olli Etuaho4f667482016-03-30 15:56:35 +03003772{
Geoff Lang496c02d2016-10-20 11:38:11 -07003773 if (length)
3774 {
3775 *length = 0;
3776 }
3777
3778 if (context->getClientMajorVersion() < 3 && !context->getExtensions().mapBuffer)
3779 {
3780 context->handleError(
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003781 InvalidOperation()
3782 << "Context does not support OpenGL ES 3.0 or GL_OES_mapbuffer is not enabled.");
Geoff Lang496c02d2016-10-20 11:38:11 -07003783 return false;
3784 }
3785
Corentin Walleze4477002017-12-01 14:39:58 -05003786 if (!context->isValidBufferBinding(target))
Olli Etuaho4f667482016-03-30 15:56:35 +03003787 {
Corentin Wallez336129f2017-10-17 15:55:40 -04003788 context->handleError(InvalidEnum() << "Buffer target not valid");
Olli Etuaho4f667482016-03-30 15:56:35 +03003789 return false;
3790 }
3791
Geoff Lang496c02d2016-10-20 11:38:11 -07003792 switch (pname)
Olli Etuaho4f667482016-03-30 15:56:35 +03003793 {
Geoff Lang496c02d2016-10-20 11:38:11 -07003794 case GL_BUFFER_MAP_POINTER:
3795 break;
Olli Etuaho4f667482016-03-30 15:56:35 +03003796
Geoff Lang496c02d2016-10-20 11:38:11 -07003797 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07003798 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Geoff Lang496c02d2016-10-20 11:38:11 -07003799 return false;
3800 }
Olli Etuaho4f667482016-03-30 15:56:35 +03003801
3802 // GLES 3.0 section 2.10.1: "Attempts to attempts to modify or query buffer object state for a
3803 // target bound to zero generate an INVALID_OPERATION error."
3804 // GLES 3.1 section 6.6 explicitly specifies this error.
Geoff Lang496c02d2016-10-20 11:38:11 -07003805 if (context->getGLState().getTargetBuffer(target) == nullptr)
Olli Etuaho4f667482016-03-30 15:56:35 +03003806 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003807 context->handleError(InvalidOperation()
3808 << "Can not get pointer for reserved buffer name zero.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003809 return false;
3810 }
3811
Geoff Lang496c02d2016-10-20 11:38:11 -07003812 if (length)
3813 {
3814 *length = 1;
3815 }
3816
Olli Etuaho4f667482016-03-30 15:56:35 +03003817 return true;
3818}
3819
Corentin Wallez336129f2017-10-17 15:55:40 -04003820bool ValidateUnmapBufferBase(Context *context, BufferBinding target)
Olli Etuaho4f667482016-03-30 15:56:35 +03003821{
Corentin Walleze4477002017-12-01 14:39:58 -05003822 if (!context->isValidBufferBinding(target))
Olli Etuaho4f667482016-03-30 15:56:35 +03003823 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003824 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidBufferTypes);
Olli Etuaho4f667482016-03-30 15:56:35 +03003825 return false;
3826 }
3827
Jamie Madilldfde6ab2016-06-09 07:07:18 -07003828 Buffer *buffer = context->getGLState().getTargetBuffer(target);
Olli Etuaho4f667482016-03-30 15:56:35 +03003829
3830 if (buffer == nullptr || !buffer->isMapped())
3831 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003832 context->handleError(InvalidOperation() << "Buffer not mapped.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003833 return false;
3834 }
3835
3836 return true;
3837}
3838
3839bool ValidateMapBufferRangeBase(Context *context,
Corentin Wallez336129f2017-10-17 15:55:40 -04003840 BufferBinding target,
Olli Etuaho4f667482016-03-30 15:56:35 +03003841 GLintptr offset,
3842 GLsizeiptr length,
3843 GLbitfield access)
3844{
Corentin Walleze4477002017-12-01 14:39:58 -05003845 if (!context->isValidBufferBinding(target))
Olli Etuaho4f667482016-03-30 15:56:35 +03003846 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003847 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidBufferTypes);
Olli Etuaho4f667482016-03-30 15:56:35 +03003848 return false;
3849 }
3850
Brandon Jones6cad5662017-06-14 13:25:13 -07003851 if (offset < 0)
Olli Etuaho4f667482016-03-30 15:56:35 +03003852 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003853 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeOffset);
3854 return false;
3855 }
3856
3857 if (length < 0)
3858 {
3859 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeLength);
Olli Etuaho4f667482016-03-30 15:56:35 +03003860 return false;
3861 }
3862
Jamie Madilldfde6ab2016-06-09 07:07:18 -07003863 Buffer *buffer = context->getGLState().getTargetBuffer(target);
Olli Etuaho4f667482016-03-30 15:56:35 +03003864
3865 if (!buffer)
3866 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003867 context->handleError(InvalidOperation() << "Attempted to map buffer object zero.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003868 return false;
3869 }
3870
3871 // Check for buffer overflow
Jamie Madille2e406c2016-06-02 13:04:10 -04003872 CheckedNumeric<size_t> checkedOffset(offset);
3873 auto checkedSize = checkedOffset + length;
Olli Etuaho4f667482016-03-30 15:56:35 +03003874
Jamie Madille2e406c2016-06-02 13:04:10 -04003875 if (!checkedSize.IsValid() || checkedSize.ValueOrDie() > static_cast<size_t>(buffer->getSize()))
Olli Etuaho4f667482016-03-30 15:56:35 +03003876 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003877 context->handleError(InvalidValue() << "Mapped range does not fit into buffer dimensions.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003878 return false;
3879 }
3880
3881 // Check for invalid bits in the mask
3882 GLbitfield allAccessBits = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT |
3883 GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_FLUSH_EXPLICIT_BIT |
3884 GL_MAP_UNSYNCHRONIZED_BIT;
3885
3886 if (access & ~(allAccessBits))
3887 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003888 context->handleError(InvalidValue()
3889 << "Invalid access bits: 0x" << std::hex << std::uppercase << access);
Olli Etuaho4f667482016-03-30 15:56:35 +03003890 return false;
3891 }
3892
3893 if (length == 0)
3894 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003895 context->handleError(InvalidOperation() << "Buffer mapping length is zero.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003896 return false;
3897 }
3898
3899 if (buffer->isMapped())
3900 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003901 context->handleError(InvalidOperation() << "Buffer is already mapped.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003902 return false;
3903 }
3904
3905 // Check for invalid bit combinations
3906 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0)
3907 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003908 context->handleError(InvalidOperation()
3909 << "Need to map buffer for either reading or writing.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003910 return false;
3911 }
3912
3913 GLbitfield writeOnlyBits =
3914 GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_UNSYNCHRONIZED_BIT;
3915
3916 if ((access & GL_MAP_READ_BIT) != 0 && (access & writeOnlyBits) != 0)
3917 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003918 context->handleError(InvalidOperation()
3919 << "Invalid access bits when mapping buffer for reading: 0x"
3920 << std::hex << std::uppercase << access);
Olli Etuaho4f667482016-03-30 15:56:35 +03003921 return false;
3922 }
3923
3924 if ((access & GL_MAP_WRITE_BIT) == 0 && (access & GL_MAP_FLUSH_EXPLICIT_BIT) != 0)
3925 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003926 context->handleError(
3927 InvalidOperation()
3928 << "The explicit flushing bit may only be set if the buffer is mapped for writing.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003929 return false;
3930 }
Geoff Lang79f71042017-08-14 16:43:43 -04003931
3932 return ValidateMapBufferBase(context, target);
Olli Etuaho4f667482016-03-30 15:56:35 +03003933}
3934
3935bool ValidateFlushMappedBufferRangeBase(Context *context,
Corentin Wallez336129f2017-10-17 15:55:40 -04003936 BufferBinding target,
Olli Etuaho4f667482016-03-30 15:56:35 +03003937 GLintptr offset,
3938 GLsizeiptr length)
3939{
Brandon Jones6cad5662017-06-14 13:25:13 -07003940 if (offset < 0)
Olli Etuaho4f667482016-03-30 15:56:35 +03003941 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003942 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeOffset);
3943 return false;
3944 }
3945
3946 if (length < 0)
3947 {
3948 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeLength);
Olli Etuaho4f667482016-03-30 15:56:35 +03003949 return false;
3950 }
3951
Corentin Walleze4477002017-12-01 14:39:58 -05003952 if (!context->isValidBufferBinding(target))
Olli Etuaho4f667482016-03-30 15:56:35 +03003953 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003954 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidBufferTypes);
Olli Etuaho4f667482016-03-30 15:56:35 +03003955 return false;
3956 }
3957
Jamie Madilldfde6ab2016-06-09 07:07:18 -07003958 Buffer *buffer = context->getGLState().getTargetBuffer(target);
Olli Etuaho4f667482016-03-30 15:56:35 +03003959
3960 if (buffer == nullptr)
3961 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003962 context->handleError(InvalidOperation() << "Attempted to flush buffer object zero.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003963 return false;
3964 }
3965
3966 if (!buffer->isMapped() || (buffer->getAccessFlags() & GL_MAP_FLUSH_EXPLICIT_BIT) == 0)
3967 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003968 context->handleError(InvalidOperation()
3969 << "Attempted to flush a buffer not mapped for explicit flushing.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003970 return false;
3971 }
3972
3973 // Check for buffer overflow
Jamie Madille2e406c2016-06-02 13:04:10 -04003974 CheckedNumeric<size_t> checkedOffset(offset);
3975 auto checkedSize = checkedOffset + length;
Olli Etuaho4f667482016-03-30 15:56:35 +03003976
Jamie Madille2e406c2016-06-02 13:04:10 -04003977 if (!checkedSize.IsValid() ||
3978 checkedSize.ValueOrDie() > static_cast<size_t>(buffer->getMapLength()))
Olli Etuaho4f667482016-03-30 15:56:35 +03003979 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003980 context->handleError(InvalidValue()
3981 << "Flushed range does not fit into buffer mapping dimensions.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003982 return false;
3983 }
3984
3985 return true;
3986}
3987
Olli Etuaho41997e72016-03-10 13:38:39 +02003988bool ValidateGenOrDelete(Context *context, GLint n)
3989{
3990 if (n < 0)
3991 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003992 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeCount);
Olli Etuaho41997e72016-03-10 13:38:39 +02003993 return false;
3994 }
3995 return true;
3996}
3997
Jamie Madill5b772312018-03-08 20:28:32 -05003998bool ValidateRobustEntryPoint(Context *context, GLsizei bufSize)
Geoff Langff5b2d52016-09-07 11:32:23 -04003999{
4000 if (!context->getExtensions().robustClientMemory)
4001 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004002 context->handleError(InvalidOperation()
4003 << "GL_ANGLE_robust_client_memory is not available.");
Geoff Langff5b2d52016-09-07 11:32:23 -04004004 return false;
4005 }
4006
4007 if (bufSize < 0)
4008 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004009 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeBufferSize);
Geoff Langff5b2d52016-09-07 11:32:23 -04004010 return false;
4011 }
4012
4013 return true;
4014}
4015
Jamie Madill5b772312018-03-08 20:28:32 -05004016bool ValidateRobustBufferSize(Context *context, GLsizei bufSize, GLsizei numParams)
Geoff Lang2e43dbb2016-10-14 12:27:35 -04004017{
4018 if (bufSize < numParams)
4019 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004020 context->handleError(InvalidOperation() << numParams << " parameters are required but "
4021 << bufSize << " were provided.");
Geoff Lang2e43dbb2016-10-14 12:27:35 -04004022 return false;
4023 }
4024
4025 return true;
4026}
4027
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -08004028bool ValidateGetFramebufferAttachmentParameterivBase(Context *context,
Jamie Madillbe849e42017-05-02 15:49:00 -04004029 GLenum target,
4030 GLenum attachment,
4031 GLenum pname,
4032 GLsizei *numParams)
Geoff Langff5b2d52016-09-07 11:32:23 -04004033{
Geoff Lange8afa902017-09-27 15:00:43 -04004034 if (!ValidFramebufferTarget(context, target))
Geoff Langff5b2d52016-09-07 11:32:23 -04004035 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004036 context->handleError(InvalidEnum());
Geoff Langff5b2d52016-09-07 11:32:23 -04004037 return false;
4038 }
4039
4040 int clientVersion = context->getClientMajorVersion();
4041
4042 switch (pname)
4043 {
4044 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE:
4045 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME:
4046 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL:
4047 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE:
4048 break;
4049
Martin Radeve5285d22017-07-14 16:23:53 +03004050 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_NUM_VIEWS_ANGLE:
4051 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_MULTIVIEW_LAYOUT_ANGLE:
4052 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_BASE_VIEW_INDEX_ANGLE:
4053 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_VIEWPORT_OFFSETS_ANGLE:
4054 if (clientVersion < 3 || !context->getExtensions().multiview)
4055 {
4056 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
4057 return false;
4058 }
4059 break;
4060
Geoff Langff5b2d52016-09-07 11:32:23 -04004061 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
4062 if (clientVersion < 3 && !context->getExtensions().sRGB)
4063 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004064 context->handleError(InvalidEnum());
Geoff Langff5b2d52016-09-07 11:32:23 -04004065 return false;
4066 }
4067 break;
4068
4069 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
4070 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
4071 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
4072 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
4073 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
4074 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
4075 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
4076 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LAYER:
4077 if (clientVersion < 3)
4078 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004079 context->handleError(InvalidEnum());
Geoff Langff5b2d52016-09-07 11:32:23 -04004080 return false;
4081 }
4082 break;
4083
Jiawei Shaoa8802472018-05-28 11:17:47 +08004084 case GL_FRAMEBUFFER_ATTACHMENT_LAYERED_EXT:
4085 if (!context->getExtensions().geometryShader)
4086 {
4087 ANGLE_VALIDATION_ERR(context, InvalidEnum(), GeometryShaderExtensionNotEnabled);
4088 return false;
4089 }
4090 break;
4091
Geoff Langff5b2d52016-09-07 11:32:23 -04004092 default:
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004093 context->handleError(InvalidEnum());
Geoff Langff5b2d52016-09-07 11:32:23 -04004094 return false;
4095 }
4096
4097 // Determine if the attachment is a valid enum
4098 switch (attachment)
4099 {
4100 case GL_BACK:
Geoff Langff5b2d52016-09-07 11:32:23 -04004101 case GL_DEPTH:
4102 case GL_STENCIL:
Geoff Langff5b2d52016-09-07 11:32:23 -04004103 if (clientVersion < 3)
4104 {
Geoff Langfa125c92017-10-24 13:01:46 -04004105 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004106 return false;
4107 }
4108 break;
4109
Bryan Bernhart (Intel Americas Inc)491b0d62017-11-10 12:48:22 -08004110 case GL_DEPTH_STENCIL_ATTACHMENT:
4111 if (clientVersion < 3 && !context->isWebGL1())
4112 {
4113 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidAttachment);
4114 return false;
4115 }
4116 break;
4117
Geoff Langfa125c92017-10-24 13:01:46 -04004118 case GL_COLOR_ATTACHMENT0:
Geoff Langff5b2d52016-09-07 11:32:23 -04004119 case GL_DEPTH_ATTACHMENT:
4120 case GL_STENCIL_ATTACHMENT:
4121 break;
4122
4123 default:
Geoff Langfa125c92017-10-24 13:01:46 -04004124 if ((clientVersion < 3 && !context->getExtensions().drawBuffers) ||
4125 attachment < GL_COLOR_ATTACHMENT0_EXT ||
Geoff Langff5b2d52016-09-07 11:32:23 -04004126 (attachment - GL_COLOR_ATTACHMENT0_EXT) >= context->getCaps().maxColorAttachments)
4127 {
Geoff Langfa125c92017-10-24 13:01:46 -04004128 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004129 return false;
4130 }
4131 break;
4132 }
4133
4134 const Framebuffer *framebuffer = context->getGLState().getTargetFramebuffer(target);
4135 ASSERT(framebuffer);
4136
4137 if (framebuffer->id() == 0)
4138 {
4139 if (clientVersion < 3)
4140 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004141 ANGLE_VALIDATION_ERR(context, InvalidOperation(), DefaultFramebufferTarget);
Geoff Langff5b2d52016-09-07 11:32:23 -04004142 return false;
4143 }
4144
4145 switch (attachment)
4146 {
4147 case GL_BACK:
4148 case GL_DEPTH:
4149 case GL_STENCIL:
4150 break;
4151
4152 default:
Brandon Jonesafa75152017-07-21 13:11:29 -07004153 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004154 return false;
4155 }
4156 }
4157 else
4158 {
4159 if (attachment >= GL_COLOR_ATTACHMENT0_EXT && attachment <= GL_COLOR_ATTACHMENT15_EXT)
4160 {
4161 // Valid attachment query
4162 }
4163 else
4164 {
4165 switch (attachment)
4166 {
4167 case GL_DEPTH_ATTACHMENT:
4168 case GL_STENCIL_ATTACHMENT:
4169 break;
4170
4171 case GL_DEPTH_STENCIL_ATTACHMENT:
Bryan Bernhart (Intel Americas Inc)491b0d62017-11-10 12:48:22 -08004172 if (!framebuffer->hasValidDepthStencil() && !context->isWebGL1())
Geoff Langff5b2d52016-09-07 11:32:23 -04004173 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004174 context->handleError(InvalidOperation());
Geoff Langff5b2d52016-09-07 11:32:23 -04004175 return false;
4176 }
4177 break;
4178
4179 default:
Brandon Jonesafa75152017-07-21 13:11:29 -07004180 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004181 return false;
4182 }
4183 }
4184 }
4185
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -08004186 const FramebufferAttachment *attachmentObject = framebuffer->getAttachment(context, attachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004187 if (attachmentObject)
4188 {
4189 ASSERT(attachmentObject->type() == GL_RENDERBUFFER ||
4190 attachmentObject->type() == GL_TEXTURE ||
4191 attachmentObject->type() == GL_FRAMEBUFFER_DEFAULT);
4192
4193 switch (pname)
4194 {
4195 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME:
4196 if (attachmentObject->type() != GL_RENDERBUFFER &&
4197 attachmentObject->type() != GL_TEXTURE)
4198 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004199 ANGLE_VALIDATION_ERR(context, InvalidEnum(), FramebufferIncompleteAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004200 return false;
4201 }
4202 break;
4203
4204 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL:
4205 if (attachmentObject->type() != GL_TEXTURE)
4206 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004207 ANGLE_VALIDATION_ERR(context, InvalidEnum(), FramebufferIncompleteAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004208 return false;
4209 }
4210 break;
4211
4212 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE:
4213 if (attachmentObject->type() != GL_TEXTURE)
4214 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004215 ANGLE_VALIDATION_ERR(context, InvalidEnum(), FramebufferIncompleteAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004216 return false;
4217 }
4218 break;
4219
4220 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
4221 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT)
4222 {
Brandon Jonesafa75152017-07-21 13:11:29 -07004223 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004224 return false;
4225 }
4226 break;
4227
4228 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LAYER:
4229 if (attachmentObject->type() != GL_TEXTURE)
4230 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004231 ANGLE_VALIDATION_ERR(context, InvalidEnum(), FramebufferIncompleteAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004232 return false;
4233 }
4234 break;
4235
4236 default:
4237 break;
4238 }
4239 }
4240 else
4241 {
4242 // ES 2.0.25 spec pg 127 states that if the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE
4243 // is NONE, then querying any other pname will generate INVALID_ENUM.
4244
4245 // ES 3.0.2 spec pg 235 states that if the attachment type is none,
4246 // GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME will return zero and be an
4247 // INVALID_OPERATION for all other pnames
4248
4249 switch (pname)
4250 {
4251 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE:
4252 break;
4253
4254 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME:
4255 if (clientVersion < 3)
4256 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004257 ANGLE_VALIDATION_ERR(context, InvalidEnum(),
Bryan Bernhart (Intel Americas Inc)491b0d62017-11-10 12:48:22 -08004258 InvalidFramebufferAttachmentParameter);
Geoff Langff5b2d52016-09-07 11:32:23 -04004259 return false;
4260 }
4261 break;
4262
4263 default:
4264 if (clientVersion < 3)
4265 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004266 ANGLE_VALIDATION_ERR(context, InvalidEnum(),
Bryan Bernhart (Intel Americas Inc)491b0d62017-11-10 12:48:22 -08004267 InvalidFramebufferAttachmentParameter);
Geoff Langff5b2d52016-09-07 11:32:23 -04004268 return false;
4269 }
4270 else
4271 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004272 ANGLE_VALIDATION_ERR(context, InvalidOperation(),
Bryan Bernhart (Intel Americas Inc)491b0d62017-11-10 12:48:22 -08004273 InvalidFramebufferAttachmentParameter);
Geoff Langff5b2d52016-09-07 11:32:23 -04004274 return false;
4275 }
4276 }
4277 }
4278
Martin Radeve5285d22017-07-14 16:23:53 +03004279 if (numParams)
4280 {
4281 if (pname == GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_VIEWPORT_OFFSETS_ANGLE)
4282 {
4283 // Only when the viewport offsets are queried we can have a varying number of output
4284 // parameters.
4285 const int numViews = attachmentObject ? attachmentObject->getNumViews() : 1;
4286 *numParams = numViews * 2;
4287 }
4288 else
4289 {
4290 // For all other queries we can have only one output parameter.
4291 *numParams = 1;
4292 }
4293 }
4294
Geoff Langff5b2d52016-09-07 11:32:23 -04004295 return true;
4296}
4297
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -08004298bool ValidateGetFramebufferAttachmentParameterivRobustANGLE(Context *context,
Geoff Langff5b2d52016-09-07 11:32:23 -04004299 GLenum target,
4300 GLenum attachment,
4301 GLenum pname,
4302 GLsizei bufSize,
Brandon Jonesd1049182018-03-28 10:02:20 -07004303 GLsizei *length,
4304 GLint *params)
Geoff Langff5b2d52016-09-07 11:32:23 -04004305{
4306 if (!ValidateRobustEntryPoint(context, bufSize))
4307 {
4308 return false;
4309 }
4310
Brandon Jonesd1049182018-03-28 10:02:20 -07004311 GLsizei numParams = 0;
Jamie Madillbe849e42017-05-02 15:49:00 -04004312 if (!ValidateGetFramebufferAttachmentParameterivBase(context, target, attachment, pname,
Brandon Jonesd1049182018-03-28 10:02:20 -07004313 &numParams))
Geoff Langff5b2d52016-09-07 11:32:23 -04004314 {
4315 return false;
4316 }
4317
Brandon Jonesd1049182018-03-28 10:02:20 -07004318 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Langff5b2d52016-09-07 11:32:23 -04004319 {
4320 return false;
4321 }
4322
Brandon Jonesd1049182018-03-28 10:02:20 -07004323 SetRobustLengthParam(length, numParams);
4324
Geoff Langff5b2d52016-09-07 11:32:23 -04004325 return true;
4326}
4327
Jamie Madill5b772312018-03-08 20:28:32 -05004328bool ValidateGetBufferParameterivRobustANGLE(Context *context,
Corentin Wallez336129f2017-10-17 15:55:40 -04004329 BufferBinding target,
Geoff Langff5b2d52016-09-07 11:32:23 -04004330 GLenum pname,
4331 GLsizei bufSize,
Geoff Langebebe1c2016-10-14 12:01:31 -04004332 GLsizei *length,
4333 GLint *params)
Geoff Langff5b2d52016-09-07 11:32:23 -04004334{
4335 if (!ValidateRobustEntryPoint(context, bufSize))
4336 {
4337 return false;
4338 }
4339
Brandon Jonesd1049182018-03-28 10:02:20 -07004340 GLsizei numParams = 0;
4341
4342 if (!ValidateGetBufferParameterBase(context, target, pname, false, &numParams))
Geoff Langff5b2d52016-09-07 11:32:23 -04004343 {
4344 return false;
4345 }
4346
Brandon Jonesd1049182018-03-28 10:02:20 -07004347 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Langebebe1c2016-10-14 12:01:31 -04004348 {
4349 return false;
4350 }
4351
Brandon Jonesd1049182018-03-28 10:02:20 -07004352 SetRobustLengthParam(length, numParams);
Geoff Langebebe1c2016-10-14 12:01:31 -04004353 return true;
4354}
4355
Jamie Madill5b772312018-03-08 20:28:32 -05004356bool ValidateGetBufferParameteri64vRobustANGLE(Context *context,
Corentin Wallez336129f2017-10-17 15:55:40 -04004357 BufferBinding target,
Geoff Langebebe1c2016-10-14 12:01:31 -04004358 GLenum pname,
4359 GLsizei bufSize,
4360 GLsizei *length,
4361 GLint64 *params)
4362{
Brandon Jonesd1049182018-03-28 10:02:20 -07004363 GLsizei numParams = 0;
4364
Geoff Langebebe1c2016-10-14 12:01:31 -04004365 if (!ValidateRobustEntryPoint(context, bufSize))
4366 {
4367 return false;
4368 }
4369
Brandon Jonesd1049182018-03-28 10:02:20 -07004370 if (!ValidateGetBufferParameterBase(context, target, pname, false, &numParams))
Geoff Langebebe1c2016-10-14 12:01:31 -04004371 {
4372 return false;
4373 }
4374
Brandon Jonesd1049182018-03-28 10:02:20 -07004375 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Langff5b2d52016-09-07 11:32:23 -04004376 {
4377 return false;
4378 }
4379
Brandon Jonesd1049182018-03-28 10:02:20 -07004380 SetRobustLengthParam(length, numParams);
4381
Geoff Langff5b2d52016-09-07 11:32:23 -04004382 return true;
4383}
4384
Jamie Madill5b772312018-03-08 20:28:32 -05004385bool ValidateGetProgramivBase(Context *context, GLuint program, GLenum pname, GLsizei *numParams)
Geoff Langff5b2d52016-09-07 11:32:23 -04004386{
4387 // Currently, all GetProgramiv queries return 1 parameter
Yunchao He33151a52017-04-13 09:58:17 +08004388 if (numParams)
4389 {
4390 *numParams = 1;
4391 }
Geoff Langff5b2d52016-09-07 11:32:23 -04004392
4393 Program *programObject = GetValidProgram(context, program);
4394 if (!programObject)
4395 {
4396 return false;
4397 }
4398
4399 switch (pname)
4400 {
4401 case GL_DELETE_STATUS:
4402 case GL_LINK_STATUS:
4403 case GL_VALIDATE_STATUS:
4404 case GL_INFO_LOG_LENGTH:
4405 case GL_ATTACHED_SHADERS:
4406 case GL_ACTIVE_ATTRIBUTES:
4407 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
4408 case GL_ACTIVE_UNIFORMS:
4409 case GL_ACTIVE_UNIFORM_MAX_LENGTH:
4410 break;
4411
4412 case GL_PROGRAM_BINARY_LENGTH:
4413 if (context->getClientMajorVersion() < 3 && !context->getExtensions().getProgramBinary)
4414 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004415 context->handleError(InvalidEnum() << "Querying GL_PROGRAM_BINARY_LENGTH "
4416 "requires GL_OES_get_program_binary or "
4417 "ES 3.0.");
Geoff Langff5b2d52016-09-07 11:32:23 -04004418 return false;
4419 }
4420 break;
4421
4422 case GL_ACTIVE_UNIFORM_BLOCKS:
4423 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH:
4424 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE:
4425 case GL_TRANSFORM_FEEDBACK_VARYINGS:
4426 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH:
4427 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT:
4428 if (context->getClientMajorVersion() < 3)
4429 {
Yunchao Hef0fd87d2017-09-12 04:55:05 +08004430 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ES3Required);
Geoff Langff5b2d52016-09-07 11:32:23 -04004431 return false;
4432 }
4433 break;
4434
Yunchao He61afff12017-03-14 15:34:03 +08004435 case GL_PROGRAM_SEPARABLE:
jchen1058f67be2017-10-27 08:59:27 +08004436 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS:
Yunchao He61afff12017-03-14 15:34:03 +08004437 if (context->getClientVersion() < Version(3, 1))
4438 {
Yunchao Hef0fd87d2017-09-12 04:55:05 +08004439 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ES31Required);
Yunchao He61afff12017-03-14 15:34:03 +08004440 return false;
4441 }
4442 break;
4443
Jiawei Shao6ae51612018-02-23 14:03:25 +08004444 case GL_COMPUTE_WORK_GROUP_SIZE:
4445 if (context->getClientVersion() < Version(3, 1))
4446 {
4447 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ES31Required);
4448 return false;
4449 }
4450
4451 // [OpenGL ES 3.1] Chapter 7.12 Page 122
4452 // An INVALID_OPERATION error is generated if COMPUTE_WORK_GROUP_SIZE is queried for a
4453 // program which has not been linked successfully, or which does not contain objects to
4454 // form a compute shader.
4455 if (!programObject->isLinked())
4456 {
4457 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ProgramNotLinked);
4458 return false;
4459 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08004460 if (!programObject->hasLinkedShaderStage(ShaderType::Compute))
Jiawei Shao6ae51612018-02-23 14:03:25 +08004461 {
4462 ANGLE_VALIDATION_ERR(context, InvalidOperation(), NoActiveComputeShaderStage);
4463 return false;
4464 }
4465 break;
4466
Jiawei Shao447bfac2018-03-14 14:23:40 +08004467 case GL_GEOMETRY_LINKED_INPUT_TYPE_EXT:
4468 case GL_GEOMETRY_LINKED_OUTPUT_TYPE_EXT:
4469 case GL_GEOMETRY_LINKED_VERTICES_OUT_EXT:
4470 case GL_GEOMETRY_SHADER_INVOCATIONS_EXT:
4471 if (!context->getExtensions().geometryShader)
4472 {
4473 ANGLE_VALIDATION_ERR(context, InvalidEnum(), GeometryShaderExtensionNotEnabled);
4474 return false;
4475 }
4476
4477 // [EXT_geometry_shader] Chapter 7.12
4478 // An INVALID_OPERATION error is generated if GEOMETRY_LINKED_VERTICES_OUT_EXT,
4479 // GEOMETRY_LINKED_INPUT_TYPE_EXT, GEOMETRY_LINKED_OUTPUT_TYPE_EXT, or
4480 // GEOMETRY_SHADER_INVOCATIONS_EXT are queried for a program which has not been linked
4481 // successfully, or which does not contain objects to form a geometry shader.
4482 if (!programObject->isLinked())
4483 {
4484 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ProgramNotLinked);
4485 return false;
4486 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08004487 if (!programObject->hasLinkedShaderStage(ShaderType::Geometry))
Jiawei Shao447bfac2018-03-14 14:23:40 +08004488 {
4489 ANGLE_VALIDATION_ERR(context, InvalidOperation(), NoActiveGeometryShaderStage);
4490 return false;
4491 }
4492 break;
4493
Geoff Langff5b2d52016-09-07 11:32:23 -04004494 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07004495 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Geoff Langff5b2d52016-09-07 11:32:23 -04004496 return false;
4497 }
4498
4499 return true;
4500}
4501
4502bool ValidateGetProgramivRobustANGLE(Context *context,
4503 GLuint program,
4504 GLenum pname,
4505 GLsizei bufSize,
Brandon Jonesd1049182018-03-28 10:02:20 -07004506 GLsizei *length,
4507 GLint *params)
Geoff Langff5b2d52016-09-07 11:32:23 -04004508{
4509 if (!ValidateRobustEntryPoint(context, bufSize))
4510 {
4511 return false;
4512 }
4513
Brandon Jonesd1049182018-03-28 10:02:20 -07004514 GLsizei numParams = 0;
4515
4516 if (!ValidateGetProgramivBase(context, program, pname, &numParams))
Geoff Langff5b2d52016-09-07 11:32:23 -04004517 {
4518 return false;
4519 }
4520
Brandon Jonesd1049182018-03-28 10:02:20 -07004521 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Langff5b2d52016-09-07 11:32:23 -04004522 {
4523 return false;
4524 }
4525
Brandon Jonesd1049182018-03-28 10:02:20 -07004526 SetRobustLengthParam(length, numParams);
4527
Geoff Langff5b2d52016-09-07 11:32:23 -04004528 return true;
4529}
4530
Geoff Lang740d9022016-10-07 11:20:52 -04004531bool ValidateGetRenderbufferParameterivRobustANGLE(Context *context,
4532 GLenum target,
4533 GLenum pname,
4534 GLsizei bufSize,
4535 GLsizei *length,
4536 GLint *params)
4537{
4538 if (!ValidateRobustEntryPoint(context, bufSize))
4539 {
4540 return false;
4541 }
4542
Brandon Jonesd1049182018-03-28 10:02:20 -07004543 GLsizei numParams = 0;
4544
4545 if (!ValidateGetRenderbufferParameterivBase(context, target, pname, &numParams))
Geoff Lang740d9022016-10-07 11:20:52 -04004546 {
4547 return false;
4548 }
4549
Brandon Jonesd1049182018-03-28 10:02:20 -07004550 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Lang740d9022016-10-07 11:20:52 -04004551 {
4552 return false;
4553 }
4554
Brandon Jonesd1049182018-03-28 10:02:20 -07004555 SetRobustLengthParam(length, numParams);
4556
Geoff Lang740d9022016-10-07 11:20:52 -04004557 return true;
4558}
4559
Geoff Langd7d0ed32016-10-07 11:33:51 -04004560bool ValidateGetShaderivRobustANGLE(Context *context,
4561 GLuint shader,
4562 GLenum pname,
4563 GLsizei bufSize,
4564 GLsizei *length,
4565 GLint *params)
4566{
4567 if (!ValidateRobustEntryPoint(context, bufSize))
4568 {
4569 return false;
4570 }
4571
Brandon Jonesd1049182018-03-28 10:02:20 -07004572 GLsizei numParams = 0;
4573
4574 if (!ValidateGetShaderivBase(context, shader, pname, &numParams))
Geoff Langd7d0ed32016-10-07 11:33:51 -04004575 {
4576 return false;
4577 }
4578
Brandon Jonesd1049182018-03-28 10:02:20 -07004579 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Langd7d0ed32016-10-07 11:33:51 -04004580 {
4581 return false;
4582 }
4583
Brandon Jonesd1049182018-03-28 10:02:20 -07004584 SetRobustLengthParam(length, numParams);
4585
Geoff Langd7d0ed32016-10-07 11:33:51 -04004586 return true;
4587}
4588
Geoff Langc1984ed2016-10-07 12:41:00 -04004589bool ValidateGetTexParameterfvRobustANGLE(Context *context,
Corentin Wallezf0e89be2017-11-08 14:00:32 -08004590 TextureType target,
Geoff Langc1984ed2016-10-07 12:41:00 -04004591 GLenum pname,
4592 GLsizei bufSize,
4593 GLsizei *length,
4594 GLfloat *params)
4595{
4596 if (!ValidateRobustEntryPoint(context, bufSize))
4597 {
4598 return false;
4599 }
4600
Brandon Jonesd1049182018-03-28 10:02:20 -07004601 GLsizei numParams = 0;
4602
4603 if (!ValidateGetTexParameterBase(context, target, pname, &numParams))
Geoff Langc1984ed2016-10-07 12:41:00 -04004604 {
4605 return false;
4606 }
4607
Brandon Jonesd1049182018-03-28 10:02:20 -07004608 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Langc1984ed2016-10-07 12:41:00 -04004609 {
4610 return false;
4611 }
4612
Brandon Jonesd1049182018-03-28 10:02:20 -07004613 SetRobustLengthParam(length, numParams);
4614
Geoff Langc1984ed2016-10-07 12:41:00 -04004615 return true;
4616}
4617
Geoff Langc1984ed2016-10-07 12:41:00 -04004618bool ValidateGetTexParameterivRobustANGLE(Context *context,
Corentin Wallezf0e89be2017-11-08 14:00:32 -08004619 TextureType target,
Geoff Langc1984ed2016-10-07 12:41:00 -04004620 GLenum pname,
4621 GLsizei bufSize,
4622 GLsizei *length,
4623 GLint *params)
4624{
Brandon Jonesd1049182018-03-28 10:02:20 -07004625
Geoff Langc1984ed2016-10-07 12:41:00 -04004626 if (!ValidateRobustEntryPoint(context, bufSize))
4627 {
4628 return false;
4629 }
Brandon Jonesd1049182018-03-28 10:02:20 -07004630 GLsizei numParams = 0;
4631 if (!ValidateGetTexParameterBase(context, target, pname, &numParams))
Geoff Langc1984ed2016-10-07 12:41:00 -04004632 {
4633 return false;
4634 }
4635
Brandon Jonesd1049182018-03-28 10:02:20 -07004636 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Langc1984ed2016-10-07 12:41:00 -04004637 {
4638 return false;
4639 }
4640
Brandon Jonesd1049182018-03-28 10:02:20 -07004641 SetRobustLengthParam(length, numParams);
Geoff Langc1984ed2016-10-07 12:41:00 -04004642 return true;
4643}
4644
Brandon Jonesfe4bbe62018-04-06 13:50:14 -07004645bool ValidateGetTexParameterIivRobustANGLE(Context *context,
4646 TextureType target,
4647 GLenum pname,
4648 GLsizei bufSize,
4649 GLsizei *length,
4650 GLint *params)
4651{
4652 UNIMPLEMENTED();
4653 return false;
4654}
4655
4656bool ValidateGetTexParameterIuivRobustANGLE(Context *context,
4657 TextureType target,
4658 GLenum pname,
4659 GLsizei bufSize,
4660 GLsizei *length,
4661 GLuint *params)
4662{
4663 UNIMPLEMENTED();
4664 return false;
4665}
4666
Geoff Langc1984ed2016-10-07 12:41:00 -04004667bool ValidateTexParameterfvRobustANGLE(Context *context,
Corentin Wallezf0e89be2017-11-08 14:00:32 -08004668 TextureType target,
Geoff Langc1984ed2016-10-07 12:41:00 -04004669 GLenum pname,
4670 GLsizei bufSize,
4671 const GLfloat *params)
4672{
4673 if (!ValidateRobustEntryPoint(context, bufSize))
4674 {
4675 return false;
4676 }
4677
4678 return ValidateTexParameterBase(context, target, pname, bufSize, params);
4679}
4680
Geoff Langc1984ed2016-10-07 12:41:00 -04004681bool ValidateTexParameterivRobustANGLE(Context *context,
Corentin Wallezf0e89be2017-11-08 14:00:32 -08004682 TextureType target,
Geoff Langc1984ed2016-10-07 12:41:00 -04004683 GLenum pname,
4684 GLsizei bufSize,
4685 const GLint *params)
4686{
4687 if (!ValidateRobustEntryPoint(context, bufSize))
4688 {
4689 return false;
4690 }
4691
4692 return ValidateTexParameterBase(context, target, pname, bufSize, params);
4693}
4694
Brandon Jonesfe4bbe62018-04-06 13:50:14 -07004695bool ValidateTexParameterIivRobustANGLE(Context *context,
4696 TextureType target,
4697 GLenum pname,
4698 GLsizei bufSize,
4699 const GLint *params)
4700{
4701 UNIMPLEMENTED();
4702 return false;
4703}
4704
4705bool ValidateTexParameterIuivRobustANGLE(Context *context,
4706 TextureType target,
4707 GLenum pname,
4708 GLsizei bufSize,
4709 const GLuint *params)
4710{
4711 UNIMPLEMENTED();
4712 return false;
4713}
4714
Geoff Langc1984ed2016-10-07 12:41:00 -04004715bool ValidateGetSamplerParameterfvRobustANGLE(Context *context,
4716 GLuint sampler,
4717 GLenum pname,
4718 GLuint bufSize,
4719 GLsizei *length,
4720 GLfloat *params)
4721{
4722 if (!ValidateRobustEntryPoint(context, bufSize))
4723 {
4724 return false;
4725 }
4726
Brandon Jonesd1049182018-03-28 10:02:20 -07004727 GLsizei numParams = 0;
4728
4729 if (!ValidateGetSamplerParameterBase(context, sampler, pname, &numParams))
Geoff Langc1984ed2016-10-07 12:41:00 -04004730 {
4731 return false;
4732 }
4733
Brandon Jonesd1049182018-03-28 10:02:20 -07004734 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Langc1984ed2016-10-07 12:41:00 -04004735 {
4736 return false;
4737 }
4738
Brandon Jonesd1049182018-03-28 10:02:20 -07004739 SetRobustLengthParam(length, numParams);
Geoff Langc1984ed2016-10-07 12:41:00 -04004740 return true;
4741}
4742
Geoff Langc1984ed2016-10-07 12:41:00 -04004743bool ValidateGetSamplerParameterivRobustANGLE(Context *context,
4744 GLuint sampler,
4745 GLenum pname,
Brandon Jonesd1049182018-03-28 10:02:20 -07004746 GLsizei bufSize,
Geoff Langc1984ed2016-10-07 12:41:00 -04004747 GLsizei *length,
4748 GLint *params)
4749{
4750 if (!ValidateRobustEntryPoint(context, bufSize))
4751 {
4752 return false;
4753 }
4754
Brandon Jonesd1049182018-03-28 10:02:20 -07004755 GLsizei numParams = 0;
4756
4757 if (!ValidateGetSamplerParameterBase(context, sampler, pname, &numParams))
Geoff Langc1984ed2016-10-07 12:41:00 -04004758 {
4759 return false;
4760 }
4761
Brandon Jonesd1049182018-03-28 10:02:20 -07004762 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Langc1984ed2016-10-07 12:41:00 -04004763 {
4764 return false;
4765 }
4766
Brandon Jonesd1049182018-03-28 10:02:20 -07004767 SetRobustLengthParam(length, numParams);
Geoff Langc1984ed2016-10-07 12:41:00 -04004768 return true;
4769}
4770
Brandon Jonesfe4bbe62018-04-06 13:50:14 -07004771bool ValidateGetSamplerParameterIivRobustANGLE(Context *context,
4772 GLuint sampler,
4773 GLenum pname,
4774 GLsizei bufSize,
4775 GLsizei *length,
4776 GLint *params)
4777{
4778 UNIMPLEMENTED();
4779 return false;
4780}
4781
4782bool ValidateGetSamplerParameterIuivRobustANGLE(Context *context,
4783 GLuint sampler,
4784 GLenum pname,
4785 GLsizei bufSize,
4786 GLsizei *length,
4787 GLuint *params)
4788{
4789 UNIMPLEMENTED();
4790 return false;
4791}
4792
Geoff Langc1984ed2016-10-07 12:41:00 -04004793bool ValidateSamplerParameterfvRobustANGLE(Context *context,
4794 GLuint sampler,
4795 GLenum pname,
4796 GLsizei bufSize,
4797 const GLfloat *params)
4798{
4799 if (!ValidateRobustEntryPoint(context, bufSize))
4800 {
4801 return false;
4802 }
4803
4804 return ValidateSamplerParameterBase(context, sampler, pname, bufSize, params);
4805}
4806
Geoff Langc1984ed2016-10-07 12:41:00 -04004807bool ValidateSamplerParameterivRobustANGLE(Context *context,
4808 GLuint sampler,
4809 GLenum pname,
4810 GLsizei bufSize,
4811 const GLint *params)
4812{
4813 if (!ValidateRobustEntryPoint(context, bufSize))
4814 {
4815 return false;
4816 }
4817
4818 return ValidateSamplerParameterBase(context, sampler, pname, bufSize, params);
4819}
4820
Brandon Jonesfe4bbe62018-04-06 13:50:14 -07004821bool ValidateSamplerParameterIivRobustANGLE(Context *context,
4822 GLuint sampler,
4823 GLenum pname,
4824 GLsizei bufSize,
4825 const GLint *param)
4826{
4827 UNIMPLEMENTED();
4828 return false;
4829}
4830
4831bool ValidateSamplerParameterIuivRobustANGLE(Context *context,
4832 GLuint sampler,
4833 GLenum pname,
4834 GLsizei bufSize,
4835 const GLuint *param)
4836{
4837 UNIMPLEMENTED();
4838 return false;
4839}
4840
Geoff Lang0b031062016-10-13 14:30:04 -04004841bool ValidateGetVertexAttribfvRobustANGLE(Context *context,
4842 GLuint index,
4843 GLenum pname,
4844 GLsizei bufSize,
4845 GLsizei *length,
4846 GLfloat *params)
4847{
4848 if (!ValidateRobustEntryPoint(context, bufSize))
4849 {
4850 return false;
4851 }
4852
Brandon Jonesd1049182018-03-28 10:02:20 -07004853 GLsizei writeLength = 0;
4854
4855 if (!ValidateGetVertexAttribBase(context, index, pname, &writeLength, false, false))
Geoff Lang0b031062016-10-13 14:30:04 -04004856 {
4857 return false;
4858 }
4859
Brandon Jonesd1049182018-03-28 10:02:20 -07004860 if (!ValidateRobustBufferSize(context, bufSize, writeLength))
Geoff Lang0b031062016-10-13 14:30:04 -04004861 {
4862 return false;
4863 }
4864
Brandon Jonesd1049182018-03-28 10:02:20 -07004865 SetRobustLengthParam(length, writeLength);
Geoff Lang0b031062016-10-13 14:30:04 -04004866 return true;
4867}
4868
Geoff Lang0b031062016-10-13 14:30:04 -04004869bool ValidateGetVertexAttribivRobustANGLE(Context *context,
4870 GLuint index,
4871 GLenum pname,
4872 GLsizei bufSize,
4873 GLsizei *length,
4874 GLint *params)
4875{
4876 if (!ValidateRobustEntryPoint(context, bufSize))
4877 {
4878 return false;
4879 }
4880
Brandon Jonesd1049182018-03-28 10:02:20 -07004881 GLsizei writeLength = 0;
4882
4883 if (!ValidateGetVertexAttribBase(context, index, pname, &writeLength, false, false))
Geoff Lang0b031062016-10-13 14:30:04 -04004884 {
4885 return false;
4886 }
4887
Brandon Jonesd1049182018-03-28 10:02:20 -07004888 if (!ValidateRobustBufferSize(context, bufSize, writeLength))
Geoff Lang0b031062016-10-13 14:30:04 -04004889 {
4890 return false;
4891 }
4892
Brandon Jonesd1049182018-03-28 10:02:20 -07004893 SetRobustLengthParam(length, writeLength);
4894
Geoff Lang0b031062016-10-13 14:30:04 -04004895 return true;
4896}
4897
Geoff Lang0b031062016-10-13 14:30:04 -04004898bool ValidateGetVertexAttribPointervRobustANGLE(Context *context,
4899 GLuint index,
4900 GLenum pname,
4901 GLsizei bufSize,
4902 GLsizei *length,
4903 void **pointer)
4904{
4905 if (!ValidateRobustEntryPoint(context, bufSize))
4906 {
4907 return false;
4908 }
4909
Brandon Jonesd1049182018-03-28 10:02:20 -07004910 GLsizei writeLength = 0;
4911
4912 if (!ValidateGetVertexAttribBase(context, index, pname, &writeLength, true, false))
Geoff Lang0b031062016-10-13 14:30:04 -04004913 {
4914 return false;
4915 }
4916
Brandon Jonesd1049182018-03-28 10:02:20 -07004917 if (!ValidateRobustBufferSize(context, bufSize, writeLength))
Geoff Lang0b031062016-10-13 14:30:04 -04004918 {
4919 return false;
4920 }
4921
Brandon Jonesd1049182018-03-28 10:02:20 -07004922 SetRobustLengthParam(length, writeLength);
4923
Geoff Lang0b031062016-10-13 14:30:04 -04004924 return true;
4925}
4926
Geoff Lang0b031062016-10-13 14:30:04 -04004927bool ValidateGetVertexAttribIivRobustANGLE(Context *context,
4928 GLuint index,
4929 GLenum pname,
4930 GLsizei bufSize,
4931 GLsizei *length,
4932 GLint *params)
4933{
4934 if (!ValidateRobustEntryPoint(context, bufSize))
4935 {
4936 return false;
4937 }
4938
Brandon Jonesd1049182018-03-28 10:02:20 -07004939 GLsizei writeLength = 0;
4940
4941 if (!ValidateGetVertexAttribBase(context, index, pname, &writeLength, false, true))
Geoff Lang0b031062016-10-13 14:30:04 -04004942 {
4943 return false;
4944 }
4945
Brandon Jonesd1049182018-03-28 10:02:20 -07004946 if (!ValidateRobustBufferSize(context, bufSize, writeLength))
Geoff Lang0b031062016-10-13 14:30:04 -04004947 {
4948 return false;
4949 }
4950
Brandon Jonesd1049182018-03-28 10:02:20 -07004951 SetRobustLengthParam(length, writeLength);
4952
Geoff Lang0b031062016-10-13 14:30:04 -04004953 return true;
4954}
4955
Geoff Lang0b031062016-10-13 14:30:04 -04004956bool ValidateGetVertexAttribIuivRobustANGLE(Context *context,
4957 GLuint index,
4958 GLenum pname,
4959 GLsizei bufSize,
4960 GLsizei *length,
4961 GLuint *params)
4962{
4963 if (!ValidateRobustEntryPoint(context, bufSize))
4964 {
4965 return false;
4966 }
4967
Brandon Jonesd1049182018-03-28 10:02:20 -07004968 GLsizei writeLength = 0;
4969
4970 if (!ValidateGetVertexAttribBase(context, index, pname, &writeLength, false, true))
Geoff Lang0b031062016-10-13 14:30:04 -04004971 {
4972 return false;
4973 }
4974
Brandon Jonesd1049182018-03-28 10:02:20 -07004975 if (!ValidateRobustBufferSize(context, bufSize, writeLength))
Geoff Lang0b031062016-10-13 14:30:04 -04004976 {
4977 return false;
4978 }
4979
Brandon Jonesd1049182018-03-28 10:02:20 -07004980 SetRobustLengthParam(length, writeLength);
4981
Geoff Lang0b031062016-10-13 14:30:04 -04004982 return true;
4983}
4984
Geoff Lang6899b872016-10-14 11:30:13 -04004985bool ValidateGetActiveUniformBlockivRobustANGLE(Context *context,
4986 GLuint program,
4987 GLuint uniformBlockIndex,
4988 GLenum pname,
4989 GLsizei bufSize,
4990 GLsizei *length,
4991 GLint *params)
4992{
4993 if (!ValidateRobustEntryPoint(context, bufSize))
4994 {
4995 return false;
4996 }
4997
Brandon Jonesd1049182018-03-28 10:02:20 -07004998 GLsizei writeLength = 0;
4999
5000 if (!ValidateGetActiveUniformBlockivBase(context, program, uniformBlockIndex, pname,
5001 &writeLength))
Geoff Lang6899b872016-10-14 11:30:13 -04005002 {
5003 return false;
5004 }
5005
Brandon Jonesd1049182018-03-28 10:02:20 -07005006 if (!ValidateRobustBufferSize(context, bufSize, writeLength))
Geoff Lang6899b872016-10-14 11:30:13 -04005007 {
5008 return false;
5009 }
5010
Brandon Jonesd1049182018-03-28 10:02:20 -07005011 SetRobustLengthParam(length, writeLength);
5012
Geoff Lang6899b872016-10-14 11:30:13 -04005013 return true;
5014}
5015
Brandon Jones416aaf92018-04-10 08:10:16 -07005016bool ValidateGetInternalformativRobustANGLE(Context *context,
Geoff Lang0a9661f2016-10-20 10:59:20 -07005017 GLenum target,
5018 GLenum internalformat,
5019 GLenum pname,
5020 GLsizei bufSize,
5021 GLsizei *length,
5022 GLint *params)
5023{
5024 if (!ValidateRobustEntryPoint(context, bufSize))
5025 {
5026 return false;
5027 }
5028
Brandon Jonesd1049182018-03-28 10:02:20 -07005029 GLsizei numParams = 0;
5030
5031 if (!ValidateGetInternalFormativBase(context, target, internalformat, pname, bufSize,
5032 &numParams))
Geoff Lang0a9661f2016-10-20 10:59:20 -07005033 {
5034 return false;
5035 }
5036
Brandon Jonesd1049182018-03-28 10:02:20 -07005037 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Lang0a9661f2016-10-20 10:59:20 -07005038 {
5039 return false;
5040 }
5041
Brandon Jonesd1049182018-03-28 10:02:20 -07005042 SetRobustLengthParam(length, numParams);
5043
Geoff Lang0a9661f2016-10-20 10:59:20 -07005044 return true;
5045}
5046
Jamie Madill5b772312018-03-08 20:28:32 -05005047bool ValidateVertexFormatBase(Context *context,
Shao80957d92017-02-20 21:25:59 +08005048 GLuint attribIndex,
5049 GLint size,
5050 GLenum type,
5051 GLboolean pureInteger)
5052{
5053 const Caps &caps = context->getCaps();
5054 if (attribIndex >= caps.maxVertexAttributes)
5055 {
Brandon Jonesafa75152017-07-21 13:11:29 -07005056 ANGLE_VALIDATION_ERR(context, InvalidValue(), IndexExceedsMaxVertexAttribute);
Shao80957d92017-02-20 21:25:59 +08005057 return false;
5058 }
5059
5060 if (size < 1 || size > 4)
5061 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005062 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidVertexAttrSize);
Geoff Lang8700a982017-06-13 10:15:13 -04005063 return false;
Shao80957d92017-02-20 21:25:59 +08005064 }
5065
5066 switch (type)
5067 {
5068 case GL_BYTE:
5069 case GL_UNSIGNED_BYTE:
5070 case GL_SHORT:
5071 case GL_UNSIGNED_SHORT:
5072 break;
5073
5074 case GL_INT:
5075 case GL_UNSIGNED_INT:
5076 if (context->getClientMajorVersion() < 3)
5077 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005078 context->handleError(InvalidEnum()
5079 << "Vertex type not supported before OpenGL ES 3.0.");
Shao80957d92017-02-20 21:25:59 +08005080 return false;
5081 }
5082 break;
5083
5084 case GL_FIXED:
5085 case GL_FLOAT:
5086 if (pureInteger)
5087 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005088 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTypePureInt);
Shao80957d92017-02-20 21:25:59 +08005089 return false;
5090 }
5091 break;
5092
5093 case GL_HALF_FLOAT:
5094 if (context->getClientMajorVersion() < 3)
5095 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005096 context->handleError(InvalidEnum()
5097 << "Vertex type not supported before OpenGL ES 3.0.");
Shao80957d92017-02-20 21:25:59 +08005098 return false;
5099 }
5100 if (pureInteger)
5101 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005102 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTypePureInt);
Shao80957d92017-02-20 21:25:59 +08005103 return false;
5104 }
5105 break;
5106
5107 case GL_INT_2_10_10_10_REV:
5108 case GL_UNSIGNED_INT_2_10_10_10_REV:
5109 if (context->getClientMajorVersion() < 3)
5110 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005111 context->handleError(InvalidEnum()
5112 << "Vertex type not supported before OpenGL ES 3.0.");
Shao80957d92017-02-20 21:25:59 +08005113 return false;
5114 }
5115 if (pureInteger)
5116 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005117 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTypePureInt);
Shao80957d92017-02-20 21:25:59 +08005118 return false;
5119 }
5120 if (size != 4)
5121 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005122 context->handleError(InvalidOperation() << "Type is INT_2_10_10_10_REV or "
5123 "UNSIGNED_INT_2_10_10_10_REV and "
5124 "size is not 4.");
Shao80957d92017-02-20 21:25:59 +08005125 return false;
5126 }
5127 break;
5128
5129 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005130 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidType);
Shao80957d92017-02-20 21:25:59 +08005131 return false;
5132 }
5133
5134 return true;
5135}
5136
Geoff Lang76e65652017-03-27 14:58:02 -04005137// Perform validation from WebGL 2 section 5.10 "Invalid Clears":
5138// In the WebGL 2 API, trying to perform a clear when there is a mismatch between the type of the
5139// specified clear value and the type of a buffer that is being cleared generates an
5140// INVALID_OPERATION error instead of producing undefined results
Jamie Madill5b772312018-03-08 20:28:32 -05005141bool ValidateWebGLFramebufferAttachmentClearType(Context *context,
Geoff Lang76e65652017-03-27 14:58:02 -04005142 GLint drawbuffer,
5143 const GLenum *validComponentTypes,
5144 size_t validComponentTypeCount)
5145{
5146 const FramebufferAttachment *attachment =
5147 context->getGLState().getDrawFramebuffer()->getDrawBuffer(drawbuffer);
5148 if (attachment)
5149 {
5150 GLenum componentType = attachment->getFormat().info->componentType;
5151 const GLenum *end = validComponentTypes + validComponentTypeCount;
5152 if (std::find(validComponentTypes, end, componentType) == end)
5153 {
5154 context->handleError(
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005155 InvalidOperation()
5156 << "No defined conversion between clear value and attachment format.");
Geoff Lang76e65652017-03-27 14:58:02 -04005157 return false;
5158 }
5159 }
5160
5161 return true;
5162}
5163
Jamie Madill5b772312018-03-08 20:28:32 -05005164bool ValidateRobustCompressedTexImageBase(Context *context, GLsizei imageSize, GLsizei dataSize)
Corentin Wallezb2931602017-04-11 15:58:57 -04005165{
5166 if (!ValidateRobustEntryPoint(context, dataSize))
5167 {
5168 return false;
5169 }
5170
Corentin Wallez336129f2017-10-17 15:55:40 -04005171 gl::Buffer *pixelUnpackBuffer =
5172 context->getGLState().getTargetBuffer(BufferBinding::PixelUnpack);
Corentin Wallezb2931602017-04-11 15:58:57 -04005173 if (pixelUnpackBuffer == nullptr)
5174 {
5175 if (dataSize < imageSize)
5176 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005177 context->handleError(InvalidOperation() << "dataSize must be at least " << imageSize);
Corentin Wallezb2931602017-04-11 15:58:57 -04005178 }
5179 }
5180 return true;
5181}
5182
Jamie Madill5b772312018-03-08 20:28:32 -05005183bool ValidateGetBufferParameterBase(Context *context,
Corentin Wallez336129f2017-10-17 15:55:40 -04005184 BufferBinding target,
Jamie Madillbe849e42017-05-02 15:49:00 -04005185 GLenum pname,
5186 bool pointerVersion,
5187 GLsizei *numParams)
5188{
5189 if (numParams)
5190 {
5191 *numParams = 0;
5192 }
5193
Corentin Walleze4477002017-12-01 14:39:58 -05005194 if (!context->isValidBufferBinding(target))
Jamie Madillbe849e42017-05-02 15:49:00 -04005195 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005196 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidBufferTypes);
Jamie Madillbe849e42017-05-02 15:49:00 -04005197 return false;
5198 }
5199
5200 const Buffer *buffer = context->getGLState().getTargetBuffer(target);
5201 if (!buffer)
5202 {
5203 // A null buffer means that "0" is bound to the requested buffer target
Brandon Jones6cad5662017-06-14 13:25:13 -07005204 ANGLE_VALIDATION_ERR(context, InvalidOperation(), BufferNotBound);
Jamie Madillbe849e42017-05-02 15:49:00 -04005205 return false;
5206 }
5207
5208 const Extensions &extensions = context->getExtensions();
5209
5210 switch (pname)
5211 {
5212 case GL_BUFFER_USAGE:
5213 case GL_BUFFER_SIZE:
5214 break;
5215
5216 case GL_BUFFER_ACCESS_OES:
5217 if (!extensions.mapBuffer)
5218 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005219 context->handleError(InvalidEnum()
5220 << "pname requires OpenGL ES 3.0 or GL_OES_mapbuffer.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005221 return false;
5222 }
5223 break;
5224
5225 case GL_BUFFER_MAPPED:
5226 static_assert(GL_BUFFER_MAPPED == GL_BUFFER_MAPPED_OES, "GL enums should be equal.");
5227 if (context->getClientMajorVersion() < 3 && !extensions.mapBuffer &&
5228 !extensions.mapBufferRange)
5229 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005230 context->handleError(InvalidEnum() << "pname requires OpenGL ES 3.0, "
5231 "GL_OES_mapbuffer or "
5232 "GL_EXT_map_buffer_range.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005233 return false;
5234 }
5235 break;
5236
5237 case GL_BUFFER_MAP_POINTER:
5238 if (!pointerVersion)
5239 {
5240 context->handleError(
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005241 InvalidEnum()
5242 << "GL_BUFFER_MAP_POINTER can only be queried with GetBufferPointerv.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005243 return false;
5244 }
5245 break;
5246
5247 case GL_BUFFER_ACCESS_FLAGS:
5248 case GL_BUFFER_MAP_OFFSET:
5249 case GL_BUFFER_MAP_LENGTH:
5250 if (context->getClientMajorVersion() < 3 && !extensions.mapBufferRange)
5251 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005252 context->handleError(InvalidEnum()
5253 << "pname requires OpenGL ES 3.0 or GL_EXT_map_buffer_range.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005254 return false;
5255 }
5256 break;
5257
5258 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005259 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005260 return false;
5261 }
5262
5263 // All buffer parameter queries return one value.
5264 if (numParams)
5265 {
5266 *numParams = 1;
5267 }
5268
5269 return true;
5270}
5271
5272bool ValidateGetRenderbufferParameterivBase(Context *context,
5273 GLenum target,
5274 GLenum pname,
5275 GLsizei *length)
5276{
5277 if (length)
5278 {
5279 *length = 0;
5280 }
5281
5282 if (target != GL_RENDERBUFFER)
5283 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005284 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidRenderbufferTarget);
Jamie Madillbe849e42017-05-02 15:49:00 -04005285 return false;
5286 }
5287
5288 Renderbuffer *renderbuffer = context->getGLState().getCurrentRenderbuffer();
5289 if (renderbuffer == nullptr)
5290 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005291 ANGLE_VALIDATION_ERR(context, InvalidOperation(), RenderbufferNotBound);
Jamie Madillbe849e42017-05-02 15:49:00 -04005292 return false;
5293 }
5294
5295 switch (pname)
5296 {
5297 case GL_RENDERBUFFER_WIDTH:
5298 case GL_RENDERBUFFER_HEIGHT:
5299 case GL_RENDERBUFFER_INTERNAL_FORMAT:
5300 case GL_RENDERBUFFER_RED_SIZE:
5301 case GL_RENDERBUFFER_GREEN_SIZE:
5302 case GL_RENDERBUFFER_BLUE_SIZE:
5303 case GL_RENDERBUFFER_ALPHA_SIZE:
5304 case GL_RENDERBUFFER_DEPTH_SIZE:
5305 case GL_RENDERBUFFER_STENCIL_SIZE:
5306 break;
5307
5308 case GL_RENDERBUFFER_SAMPLES_ANGLE:
5309 if (!context->getExtensions().framebufferMultisample)
5310 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005311 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ExtensionNotEnabled);
Jamie Madillbe849e42017-05-02 15:49:00 -04005312 return false;
5313 }
5314 break;
5315
5316 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005317 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005318 return false;
5319 }
5320
5321 if (length)
5322 {
5323 *length = 1;
5324 }
5325 return true;
5326}
5327
5328bool ValidateGetShaderivBase(Context *context, GLuint shader, GLenum pname, GLsizei *length)
5329{
5330 if (length)
5331 {
5332 *length = 0;
5333 }
5334
5335 if (GetValidShader(context, shader) == nullptr)
5336 {
5337 return false;
5338 }
5339
5340 switch (pname)
5341 {
5342 case GL_SHADER_TYPE:
5343 case GL_DELETE_STATUS:
5344 case GL_COMPILE_STATUS:
5345 case GL_INFO_LOG_LENGTH:
5346 case GL_SHADER_SOURCE_LENGTH:
5347 break;
5348
5349 case GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE:
5350 if (!context->getExtensions().translatedShaderSource)
5351 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005352 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ExtensionNotEnabled);
Jamie Madillbe849e42017-05-02 15:49:00 -04005353 return false;
5354 }
5355 break;
5356
5357 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005358 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005359 return false;
5360 }
5361
5362 if (length)
5363 {
5364 *length = 1;
5365 }
5366 return true;
5367}
5368
Corentin Wallezf0e89be2017-11-08 14:00:32 -08005369bool ValidateGetTexParameterBase(Context *context,
5370 TextureType target,
5371 GLenum pname,
5372 GLsizei *length)
Jamie Madillbe849e42017-05-02 15:49:00 -04005373{
5374 if (length)
5375 {
5376 *length = 0;
5377 }
5378
5379 if (!ValidTextureTarget(context, target) && !ValidTextureExternalTarget(context, target))
5380 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005381 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTextureTarget);
Jamie Madillbe849e42017-05-02 15:49:00 -04005382 return false;
5383 }
5384
5385 if (context->getTargetTexture(target) == nullptr)
5386 {
5387 // Should only be possible for external textures
Brandon Jones6cad5662017-06-14 13:25:13 -07005388 ANGLE_VALIDATION_ERR(context, InvalidEnum(), TextureNotBound);
Jamie Madillbe849e42017-05-02 15:49:00 -04005389 return false;
5390 }
5391
Lingfeng Yangf97641c2018-06-21 19:22:45 -07005392 if (context->getClientMajorVersion() == 1 && !IsValidGLES1TextureParameter(pname))
5393 {
5394 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
5395 return false;
5396 }
5397
Jamie Madillbe849e42017-05-02 15:49:00 -04005398 switch (pname)
5399 {
5400 case GL_TEXTURE_MAG_FILTER:
5401 case GL_TEXTURE_MIN_FILTER:
5402 case GL_TEXTURE_WRAP_S:
5403 case GL_TEXTURE_WRAP_T:
5404 break;
5405
5406 case GL_TEXTURE_USAGE_ANGLE:
5407 if (!context->getExtensions().textureUsage)
5408 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005409 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ExtensionNotEnabled);
Jamie Madillbe849e42017-05-02 15:49:00 -04005410 return false;
5411 }
5412 break;
5413
5414 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
Luc Ferron1b1a8642018-01-23 15:12:01 -05005415 if (!ValidateTextureMaxAnisotropyExtensionEnabled(context))
Jamie Madillbe849e42017-05-02 15:49:00 -04005416 {
Jamie Madillbe849e42017-05-02 15:49:00 -04005417 return false;
5418 }
5419 break;
5420
5421 case GL_TEXTURE_IMMUTABLE_FORMAT:
5422 if (context->getClientMajorVersion() < 3 && !context->getExtensions().textureStorage)
5423 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005424 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ExtensionNotEnabled);
Jamie Madillbe849e42017-05-02 15:49:00 -04005425 return false;
5426 }
5427 break;
5428
5429 case GL_TEXTURE_WRAP_R:
5430 case GL_TEXTURE_IMMUTABLE_LEVELS:
5431 case GL_TEXTURE_SWIZZLE_R:
5432 case GL_TEXTURE_SWIZZLE_G:
5433 case GL_TEXTURE_SWIZZLE_B:
5434 case GL_TEXTURE_SWIZZLE_A:
5435 case GL_TEXTURE_BASE_LEVEL:
5436 case GL_TEXTURE_MAX_LEVEL:
5437 case GL_TEXTURE_MIN_LOD:
5438 case GL_TEXTURE_MAX_LOD:
5439 case GL_TEXTURE_COMPARE_MODE:
5440 case GL_TEXTURE_COMPARE_FUNC:
5441 if (context->getClientMajorVersion() < 3)
5442 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005443 context->handleError(InvalidEnum() << "pname requires OpenGL ES 3.0.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005444 return false;
5445 }
5446 break;
5447
5448 case GL_TEXTURE_SRGB_DECODE_EXT:
5449 if (!context->getExtensions().textureSRGBDecode)
5450 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005451 context->handleError(InvalidEnum() << "GL_EXT_texture_sRGB_decode is not enabled.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005452 return false;
5453 }
5454 break;
5455
Yunchao Hebacaa712018-01-30 14:01:39 +08005456 case GL_DEPTH_STENCIL_TEXTURE_MODE:
5457 if (context->getClientVersion() < Version(3, 1))
5458 {
5459 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumRequiresGLES31);
5460 return false;
5461 }
5462 break;
5463
Lingfeng Yangf97641c2018-06-21 19:22:45 -07005464 case GL_GENERATE_MIPMAP:
5465 case GL_TEXTURE_CROP_RECT_OES:
5466 // TODO(lfy@google.com): Restrict to GL_OES_draw_texture
5467 // after GL_OES_draw_texture functionality implemented
5468 if (context->getClientMajorVersion() > 1)
5469 {
5470 ANGLE_VALIDATION_ERR(context, InvalidEnum(), GLES1Only);
5471 return false;
5472 }
5473 break;
Jamie Madillbe849e42017-05-02 15:49:00 -04005474 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005475 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005476 return false;
5477 }
5478
5479 if (length)
5480 {
Lingfeng Yangf97641c2018-06-21 19:22:45 -07005481 *length = GetTexParameterCount(pname);
Jamie Madillbe849e42017-05-02 15:49:00 -04005482 }
5483 return true;
5484}
5485
5486bool ValidateGetVertexAttribBase(Context *context,
5487 GLuint index,
5488 GLenum pname,
5489 GLsizei *length,
5490 bool pointer,
5491 bool pureIntegerEntryPoint)
5492{
5493 if (length)
5494 {
5495 *length = 0;
5496 }
5497
5498 if (pureIntegerEntryPoint && context->getClientMajorVersion() < 3)
5499 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005500 context->handleError(InvalidOperation() << "Context does not support OpenGL ES 3.0.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005501 return false;
5502 }
5503
5504 if (index >= context->getCaps().maxVertexAttributes)
5505 {
Brandon Jonesafa75152017-07-21 13:11:29 -07005506 ANGLE_VALIDATION_ERR(context, InvalidValue(), IndexExceedsMaxVertexAttribute);
Jamie Madillbe849e42017-05-02 15:49:00 -04005507 return false;
5508 }
5509
5510 if (pointer)
5511 {
5512 if (pname != GL_VERTEX_ATTRIB_ARRAY_POINTER)
5513 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005514 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005515 return false;
5516 }
5517 }
5518 else
5519 {
5520 switch (pname)
5521 {
5522 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
5523 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
5524 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
5525 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
5526 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
5527 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
5528 case GL_CURRENT_VERTEX_ATTRIB:
5529 break;
5530
5531 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR:
5532 static_assert(
5533 GL_VERTEX_ATTRIB_ARRAY_DIVISOR == GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE,
5534 "ANGLE extension enums not equal to GL enums.");
5535 if (context->getClientMajorVersion() < 3 &&
5536 !context->getExtensions().instancedArrays)
5537 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005538 context->handleError(InvalidEnum() << "GL_VERTEX_ATTRIB_ARRAY_DIVISOR "
5539 "requires OpenGL ES 3.0 or "
5540 "GL_ANGLE_instanced_arrays.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005541 return false;
5542 }
5543 break;
5544
5545 case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
5546 if (context->getClientMajorVersion() < 3)
5547 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005548 context->handleError(
5549 InvalidEnum() << "GL_VERTEX_ATTRIB_ARRAY_INTEGER requires OpenGL ES 3.0.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005550 return false;
5551 }
5552 break;
5553
5554 case GL_VERTEX_ATTRIB_BINDING:
5555 case GL_VERTEX_ATTRIB_RELATIVE_OFFSET:
5556 if (context->getClientVersion() < ES_3_1)
5557 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005558 context->handleError(InvalidEnum()
5559 << "Vertex Attrib Bindings require OpenGL ES 3.1.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005560 return false;
5561 }
5562 break;
5563
5564 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005565 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005566 return false;
5567 }
5568 }
5569
5570 if (length)
5571 {
5572 if (pname == GL_CURRENT_VERTEX_ATTRIB)
5573 {
5574 *length = 4;
5575 }
5576 else
5577 {
5578 *length = 1;
5579 }
5580 }
5581
5582 return true;
5583}
5584
Jamie Madill4928b7c2017-06-20 12:57:39 -04005585bool ValidateReadPixelsBase(Context *context,
Jamie Madillbe849e42017-05-02 15:49:00 -04005586 GLint x,
5587 GLint y,
5588 GLsizei width,
5589 GLsizei height,
5590 GLenum format,
5591 GLenum type,
5592 GLsizei bufSize,
5593 GLsizei *length,
5594 GLsizei *columns,
5595 GLsizei *rows,
5596 void *pixels)
5597{
5598 if (length != nullptr)
5599 {
5600 *length = 0;
5601 }
5602 if (rows != nullptr)
5603 {
5604 *rows = 0;
5605 }
5606 if (columns != nullptr)
5607 {
5608 *columns = 0;
5609 }
5610
5611 if (width < 0 || height < 0)
5612 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005613 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeSize);
Jamie Madillbe849e42017-05-02 15:49:00 -04005614 return false;
5615 }
5616
Jamie Madillacf2f3a2017-11-21 19:22:44 -05005617 Framebuffer *readFramebuffer = context->getGLState().getReadFramebuffer();
Jamie Madillbe849e42017-05-02 15:49:00 -04005618
Jamie Madill427064d2018-04-13 16:20:34 -04005619 if (!ValidateFramebufferComplete(context, readFramebuffer))
Jamie Madillbe849e42017-05-02 15:49:00 -04005620 {
Jamie Madillbe849e42017-05-02 15:49:00 -04005621 return false;
5622 }
5623
Jamie Madille98b1b52018-03-08 09:47:23 -05005624 if (readFramebuffer->id() != 0 && !ValidateFramebufferNotMultisampled(context, readFramebuffer))
Jamie Madillbe849e42017-05-02 15:49:00 -04005625 {
Jamie Madillbe849e42017-05-02 15:49:00 -04005626 return false;
5627 }
5628
Jamie Madill690c8eb2018-03-12 15:20:03 -04005629 Framebuffer *framebuffer = context->getGLState().getReadFramebuffer();
Jamie Madillbe849e42017-05-02 15:49:00 -04005630 ASSERT(framebuffer);
5631
5632 if (framebuffer->getReadBufferState() == GL_NONE)
5633 {
Brandon Jonesafa75152017-07-21 13:11:29 -07005634 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ReadBufferNone);
Jamie Madillbe849e42017-05-02 15:49:00 -04005635 return false;
5636 }
5637
5638 const FramebufferAttachment *readBuffer = framebuffer->getReadColorbuffer();
5639 // WebGL 1.0 [Section 6.26] Reading From a Missing Attachment
5640 // In OpenGL ES it is undefined what happens when an operation tries to read from a missing
5641 // attachment and WebGL defines it to be an error. We do the check unconditionnaly as the
5642 // situation is an application error that would lead to a crash in ANGLE.
5643 if (readBuffer == nullptr)
5644 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005645 ANGLE_VALIDATION_ERR(context, InvalidOperation(), MissingReadAttachment);
Jamie Madillbe849e42017-05-02 15:49:00 -04005646 return false;
5647 }
5648
Martin Radev28031682017-07-28 14:47:56 +03005649 // ANGLE_multiview, Revision 1:
5650 // ReadPixels generates an INVALID_FRAMEBUFFER_OPERATION error if the multi-view layout of the
Olli Etuaho8acb1b62018-07-30 16:20:54 +03005651 // current read framebuffer is FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE or the number of views
5652 // in the current read framebuffer is more than one.
5653 if (framebuffer->readDisallowedByMultiview())
Martin Radev28031682017-07-28 14:47:56 +03005654 {
5655 context->handleError(InvalidFramebufferOperation()
5656 << "Attempting to read from a multi-view framebuffer.");
5657 return false;
5658 }
5659
Geoff Lang280ba992017-04-18 16:30:58 -04005660 if (context->getExtensions().webglCompatibility)
5661 {
5662 // The ES 2.0 spec states that the format must be "among those defined in table 3.4,
5663 // excluding formats LUMINANCE and LUMINANCE_ALPHA.". This requires validating the format
5664 // and type before validating the combination of format and type. However, the
5665 // dEQP-GLES3.functional.negative_api.buffer.read_pixels passes GL_LUMINANCE as a format and
5666 // verifies that GL_INVALID_OPERATION is generated.
5667 // TODO(geofflang): Update this check to be done in all/no cases once this is resolved in
5668 // dEQP/WebGL.
5669 if (!ValidReadPixelsFormatEnum(context, format))
5670 {
Brandon Jonesafa75152017-07-21 13:11:29 -07005671 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidFormat);
Geoff Lang280ba992017-04-18 16:30:58 -04005672 return false;
5673 }
5674
5675 if (!ValidReadPixelsTypeEnum(context, type))
5676 {
Brandon Jonesafa75152017-07-21 13:11:29 -07005677 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidType);
Geoff Lang280ba992017-04-18 16:30:58 -04005678 return false;
5679 }
5680 }
5681
Jamie Madill690c8eb2018-03-12 15:20:03 -04005682 GLenum currentFormat = GL_NONE;
5683 ANGLE_VALIDATION_TRY(framebuffer->getImplementationColorReadFormat(context, &currentFormat));
5684
5685 GLenum currentType = GL_NONE;
5686 ANGLE_VALIDATION_TRY(framebuffer->getImplementationColorReadType(context, &currentType));
5687
Jamie Madillbe849e42017-05-02 15:49:00 -04005688 GLenum currentComponentType = readBuffer->getFormat().info->componentType;
5689
5690 bool validFormatTypeCombination =
5691 ValidReadPixelsFormatType(context, currentComponentType, format, type);
5692
5693 if (!(currentFormat == format && currentType == type) && !validFormatTypeCombination)
5694 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005695 ANGLE_VALIDATION_ERR(context, InvalidOperation(), MismatchedTypeAndFormat);
Jamie Madillbe849e42017-05-02 15:49:00 -04005696 return false;
5697 }
5698
5699 // Check for pixel pack buffer related API errors
Corentin Wallez336129f2017-10-17 15:55:40 -04005700 gl::Buffer *pixelPackBuffer = context->getGLState().getTargetBuffer(BufferBinding::PixelPack);
Jamie Madillbe849e42017-05-02 15:49:00 -04005701 if (pixelPackBuffer != nullptr && pixelPackBuffer->isMapped())
5702 {
5703 // ...the buffer object's data store is currently mapped.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005704 context->handleError(InvalidOperation() << "Pixel pack buffer is mapped.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005705 return false;
5706 }
James Darpiniane8a93c62018-01-04 18:02:24 -08005707 if (context->getExtensions().webglCompatibility && pixelPackBuffer != nullptr &&
5708 pixelPackBuffer->isBoundForTransformFeedbackAndOtherUse())
5709 {
5710 ANGLE_VALIDATION_ERR(context, InvalidOperation(), PixelPackBufferBoundForTransformFeedback);
5711 return false;
5712 }
Jamie Madillbe849e42017-05-02 15:49:00 -04005713
5714 // .. the data would be packed to the buffer object such that the memory writes required
5715 // would exceed the data store size.
5716 const InternalFormat &formatInfo = GetInternalFormatInfo(format, type);
5717 const gl::Extents size(width, height, 1);
5718 const auto &pack = context->getGLState().getPackState();
5719
Jamie Madillca2ff382018-07-11 09:01:17 -04005720 GLuint endByte = 0;
5721 if (!formatInfo.computePackUnpackEndByte(type, size, pack, false, &endByte))
Jamie Madillbe849e42017-05-02 15:49:00 -04005722 {
Jamie Madillca2ff382018-07-11 09:01:17 -04005723 ANGLE_VALIDATION_ERR(context, InvalidOperation(), IntegerOverflow);
Jamie Madillbe849e42017-05-02 15:49:00 -04005724 return false;
5725 }
5726
Jamie Madillbe849e42017-05-02 15:49:00 -04005727 if (bufSize >= 0)
5728 {
5729 if (pixelPackBuffer == nullptr && static_cast<size_t>(bufSize) < endByte)
5730 {
Brandon Jonesafa75152017-07-21 13:11:29 -07005731 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InsufficientBufferSize);
Jamie Madillbe849e42017-05-02 15:49:00 -04005732 return false;
5733 }
5734 }
5735
5736 if (pixelPackBuffer != nullptr)
5737 {
5738 CheckedNumeric<size_t> checkedEndByte(endByte);
5739 CheckedNumeric<size_t> checkedOffset(reinterpret_cast<size_t>(pixels));
5740 checkedEndByte += checkedOffset;
5741
5742 if (checkedEndByte.ValueOrDie() > static_cast<size_t>(pixelPackBuffer->getSize()))
5743 {
5744 // Overflow past the end of the buffer
Brandon Jones6cad5662017-06-14 13:25:13 -07005745 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ParamOverflow);
Jamie Madillbe849e42017-05-02 15:49:00 -04005746 return false;
5747 }
5748 }
5749
5750 if (pixelPackBuffer == nullptr && length != nullptr)
5751 {
5752 if (endByte > static_cast<size_t>(std::numeric_limits<GLsizei>::max()))
5753 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005754 ANGLE_VALIDATION_ERR(context, InvalidOperation(), IntegerOverflow);
Jamie Madillbe849e42017-05-02 15:49:00 -04005755 return false;
5756 }
5757
5758 *length = static_cast<GLsizei>(endByte);
5759 }
5760
Geoff Langa953b522018-02-21 16:56:23 -05005761 auto getClippedExtent = [](GLint start, GLsizei length, int bufferSize, GLsizei *outExtent) {
Jamie Madillbe849e42017-05-02 15:49:00 -04005762 angle::CheckedNumeric<int> clippedExtent(length);
5763 if (start < 0)
5764 {
5765 // "subtract" the area that is less than 0
5766 clippedExtent += start;
5767 }
5768
Geoff Langa953b522018-02-21 16:56:23 -05005769 angle::CheckedNumeric<int> readExtent = start;
5770 readExtent += length;
5771 if (!readExtent.IsValid())
5772 {
5773 return false;
5774 }
5775
5776 if (readExtent.ValueOrDie() > bufferSize)
Jamie Madillbe849e42017-05-02 15:49:00 -04005777 {
5778 // Subtract the region to the right of the read buffer
5779 clippedExtent -= (readExtent - bufferSize);
5780 }
5781
5782 if (!clippedExtent.IsValid())
5783 {
Geoff Langa953b522018-02-21 16:56:23 -05005784 return false;
Jamie Madillbe849e42017-05-02 15:49:00 -04005785 }
5786
Geoff Langa953b522018-02-21 16:56:23 -05005787 *outExtent = std::max(clippedExtent.ValueOrDie(), 0);
5788 return true;
Jamie Madillbe849e42017-05-02 15:49:00 -04005789 };
5790
Geoff Langa953b522018-02-21 16:56:23 -05005791 GLsizei writtenColumns = 0;
5792 if (!getClippedExtent(x, width, readBuffer->getSize().width, &writtenColumns))
5793 {
5794 ANGLE_VALIDATION_ERR(context, InvalidOperation(), IntegerOverflow);
5795 return false;
5796 }
5797
5798 GLsizei writtenRows = 0;
5799 if (!getClippedExtent(y, height, readBuffer->getSize().height, &writtenRows))
5800 {
5801 ANGLE_VALIDATION_ERR(context, InvalidOperation(), IntegerOverflow);
5802 return false;
5803 }
5804
Jamie Madillbe849e42017-05-02 15:49:00 -04005805 if (columns != nullptr)
5806 {
Geoff Langa953b522018-02-21 16:56:23 -05005807 *columns = writtenColumns;
Jamie Madillbe849e42017-05-02 15:49:00 -04005808 }
5809
5810 if (rows != nullptr)
5811 {
Geoff Langa953b522018-02-21 16:56:23 -05005812 *rows = writtenRows;
Jamie Madillbe849e42017-05-02 15:49:00 -04005813 }
5814
5815 return true;
5816}
5817
5818template <typename ParamType>
5819bool ValidateTexParameterBase(Context *context,
Corentin Wallezf0e89be2017-11-08 14:00:32 -08005820 TextureType target,
Jamie Madillbe849e42017-05-02 15:49:00 -04005821 GLenum pname,
5822 GLsizei bufSize,
5823 const ParamType *params)
5824{
5825 if (!ValidTextureTarget(context, target) && !ValidTextureExternalTarget(context, target))
5826 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005827 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTextureTarget);
Jamie Madillbe849e42017-05-02 15:49:00 -04005828 return false;
5829 }
5830
5831 if (context->getTargetTexture(target) == nullptr)
5832 {
5833 // Should only be possible for external textures
Brandon Jones6cad5662017-06-14 13:25:13 -07005834 ANGLE_VALIDATION_ERR(context, InvalidEnum(), TextureNotBound);
Jamie Madillbe849e42017-05-02 15:49:00 -04005835 return false;
5836 }
5837
Lingfeng Yangf97641c2018-06-21 19:22:45 -07005838 const GLsizei minBufSize = GetTexParameterCount(pname);
Jamie Madillbe849e42017-05-02 15:49:00 -04005839 if (bufSize >= 0 && bufSize < minBufSize)
5840 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005841 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InsufficientBufferSize);
Jamie Madillbe849e42017-05-02 15:49:00 -04005842 return false;
5843 }
5844
Lingfeng Yangf97641c2018-06-21 19:22:45 -07005845 if (context->getClientMajorVersion() == 1 && !IsValidGLES1TextureParameter(pname))
5846 {
5847 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
5848 return false;
5849 }
5850
Jamie Madillbe849e42017-05-02 15:49:00 -04005851 switch (pname)
5852 {
5853 case GL_TEXTURE_WRAP_R:
5854 case GL_TEXTURE_SWIZZLE_R:
5855 case GL_TEXTURE_SWIZZLE_G:
5856 case GL_TEXTURE_SWIZZLE_B:
5857 case GL_TEXTURE_SWIZZLE_A:
5858 case GL_TEXTURE_BASE_LEVEL:
5859 case GL_TEXTURE_MAX_LEVEL:
5860 case GL_TEXTURE_COMPARE_MODE:
5861 case GL_TEXTURE_COMPARE_FUNC:
5862 case GL_TEXTURE_MIN_LOD:
5863 case GL_TEXTURE_MAX_LOD:
5864 if (context->getClientMajorVersion() < 3)
5865 {
Brandon Jonesafa75152017-07-21 13:11:29 -07005866 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ES3Required);
Jamie Madillbe849e42017-05-02 15:49:00 -04005867 return false;
5868 }
Corentin Wallezf0e89be2017-11-08 14:00:32 -08005869 if (target == TextureType::External && !context->getExtensions().eglImageExternalEssl3)
Jamie Madillbe849e42017-05-02 15:49:00 -04005870 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005871 context->handleError(InvalidEnum() << "ES3 texture parameters are not "
5872 "available without "
5873 "GL_OES_EGL_image_external_essl3.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005874 return false;
5875 }
5876 break;
5877
Lingfeng Yangf97641c2018-06-21 19:22:45 -07005878 case GL_GENERATE_MIPMAP:
5879 case GL_TEXTURE_CROP_RECT_OES:
5880 if (context->getClientMajorVersion() > 1)
5881 {
5882 ANGLE_VALIDATION_ERR(context, InvalidEnum(), GLES1Only);
5883 return false;
5884 }
5885 break;
Jamie Madillbe849e42017-05-02 15:49:00 -04005886 default:
5887 break;
5888 }
5889
Corentin Wallezf0e89be2017-11-08 14:00:32 -08005890 if (target == TextureType::_2DMultisample)
JiangYizhou4cff8d62017-07-06 14:54:09 +08005891 {
5892 switch (pname)
5893 {
5894 case GL_TEXTURE_MIN_FILTER:
5895 case GL_TEXTURE_MAG_FILTER:
5896 case GL_TEXTURE_WRAP_S:
5897 case GL_TEXTURE_WRAP_T:
5898 case GL_TEXTURE_WRAP_R:
5899 case GL_TEXTURE_MIN_LOD:
5900 case GL_TEXTURE_MAX_LOD:
5901 case GL_TEXTURE_COMPARE_MODE:
5902 case GL_TEXTURE_COMPARE_FUNC:
5903 context->handleError(InvalidEnum()
5904 << "Invalid parameter for 2D multisampled textures.");
5905 return false;
5906 }
5907 }
5908
Jamie Madillbe849e42017-05-02 15:49:00 -04005909 switch (pname)
5910 {
5911 case GL_TEXTURE_WRAP_S:
5912 case GL_TEXTURE_WRAP_T:
5913 case GL_TEXTURE_WRAP_R:
Lingfeng Yang038dd532018-03-29 17:31:52 -07005914 {
5915 bool restrictedWrapModes =
5916 target == TextureType::External || target == TextureType::Rectangle;
5917 if (!ValidateTextureWrapModeValue(context, params, restrictedWrapModes))
Jamie Madillbe849e42017-05-02 15:49:00 -04005918 {
Lingfeng Yang038dd532018-03-29 17:31:52 -07005919 return false;
Jamie Madillbe849e42017-05-02 15:49:00 -04005920 }
Lingfeng Yang038dd532018-03-29 17:31:52 -07005921 }
5922 break;
Jamie Madillbe849e42017-05-02 15:49:00 -04005923
5924 case GL_TEXTURE_MIN_FILTER:
Lingfeng Yang038dd532018-03-29 17:31:52 -07005925 {
5926 bool restrictedMinFilter =
5927 target == TextureType::External || target == TextureType::Rectangle;
5928 if (!ValidateTextureMinFilterValue(context, params, restrictedMinFilter))
Jamie Madillbe849e42017-05-02 15:49:00 -04005929 {
Lingfeng Yang038dd532018-03-29 17:31:52 -07005930 return false;
Jamie Madillbe849e42017-05-02 15:49:00 -04005931 }
Lingfeng Yang038dd532018-03-29 17:31:52 -07005932 }
5933 break;
Jamie Madillbe849e42017-05-02 15:49:00 -04005934
5935 case GL_TEXTURE_MAG_FILTER:
5936 if (!ValidateTextureMagFilterValue(context, params))
5937 {
5938 return false;
5939 }
5940 break;
5941
5942 case GL_TEXTURE_USAGE_ANGLE:
Geoff Lang91ab54b2017-10-30 15:12:42 -04005943 if (!context->getExtensions().textureUsage)
5944 {
5945 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
5946 return false;
5947 }
5948
Jamie Madillbe849e42017-05-02 15:49:00 -04005949 switch (ConvertToGLenum(params[0]))
5950 {
5951 case GL_NONE:
5952 case GL_FRAMEBUFFER_ATTACHMENT_ANGLE:
5953 break;
5954
5955 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005956 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005957 return false;
5958 }
5959 break;
5960
5961 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
Lingfeng Yang038dd532018-03-29 17:31:52 -07005962 {
5963 GLfloat paramValue = static_cast<GLfloat>(params[0]);
5964 if (!ValidateTextureMaxAnisotropyValue(context, paramValue))
Jamie Madillbe849e42017-05-02 15:49:00 -04005965 {
Lingfeng Yang038dd532018-03-29 17:31:52 -07005966 return false;
Jamie Madillbe849e42017-05-02 15:49:00 -04005967 }
Lingfeng Yang038dd532018-03-29 17:31:52 -07005968 ASSERT(static_cast<ParamType>(paramValue) == params[0]);
5969 }
5970 break;
Jamie Madillbe849e42017-05-02 15:49:00 -04005971
5972 case GL_TEXTURE_MIN_LOD:
5973 case GL_TEXTURE_MAX_LOD:
5974 // any value is permissible
5975 break;
5976
5977 case GL_TEXTURE_COMPARE_MODE:
5978 if (!ValidateTextureCompareModeValue(context, params))
5979 {
5980 return false;
5981 }
5982 break;
5983
5984 case GL_TEXTURE_COMPARE_FUNC:
5985 if (!ValidateTextureCompareFuncValue(context, params))
5986 {
5987 return false;
5988 }
5989 break;
5990
5991 case GL_TEXTURE_SWIZZLE_R:
5992 case GL_TEXTURE_SWIZZLE_G:
5993 case GL_TEXTURE_SWIZZLE_B:
5994 case GL_TEXTURE_SWIZZLE_A:
5995 switch (ConvertToGLenum(params[0]))
5996 {
5997 case GL_RED:
5998 case GL_GREEN:
5999 case GL_BLUE:
6000 case GL_ALPHA:
6001 case GL_ZERO:
6002 case GL_ONE:
6003 break;
6004
6005 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07006006 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04006007 return false;
6008 }
6009 break;
6010
6011 case GL_TEXTURE_BASE_LEVEL:
Geoff Langfb7685f2017-11-13 11:44:11 -05006012 if (ConvertToGLint(params[0]) < 0)
Jamie Madillbe849e42017-05-02 15:49:00 -04006013 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05006014 context->handleError(InvalidValue() << "Base level must be at least 0.");
Jamie Madillbe849e42017-05-02 15:49:00 -04006015 return false;
6016 }
Corentin Wallezf0e89be2017-11-08 14:00:32 -08006017 if (target == TextureType::External && static_cast<GLuint>(params[0]) != 0)
Jamie Madillbe849e42017-05-02 15:49:00 -04006018 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05006019 context->handleError(InvalidOperation()
6020 << "Base level must be 0 for external textures.");
Jamie Madillbe849e42017-05-02 15:49:00 -04006021 return false;
6022 }
Corentin Wallezf0e89be2017-11-08 14:00:32 -08006023 if (target == TextureType::_2DMultisample && static_cast<GLuint>(params[0]) != 0)
JiangYizhou4cff8d62017-07-06 14:54:09 +08006024 {
6025 context->handleError(InvalidOperation()
6026 << "Base level must be 0 for multisampled textures.");
6027 return false;
6028 }
Corentin Wallezf0e89be2017-11-08 14:00:32 -08006029 if (target == TextureType::Rectangle && static_cast<GLuint>(params[0]) != 0)
Corentin Wallez13c0dd42017-07-04 18:27:01 -04006030 {
6031 context->handleError(InvalidOperation()
6032 << "Base level must be 0 for rectangle textures.");
6033 return false;
6034 }
Jamie Madillbe849e42017-05-02 15:49:00 -04006035 break;
6036
6037 case GL_TEXTURE_MAX_LEVEL:
Geoff Langfb7685f2017-11-13 11:44:11 -05006038 if (ConvertToGLint(params[0]) < 0)
Jamie Madillbe849e42017-05-02 15:49:00 -04006039 {
Brandon Jones6cad5662017-06-14 13:25:13 -07006040 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidMipLevel);
Jamie Madillbe849e42017-05-02 15:49:00 -04006041 return false;
6042 }
6043 break;
6044
6045 case GL_DEPTH_STENCIL_TEXTURE_MODE:
6046 if (context->getClientVersion() < Version(3, 1))
6047 {
Brandon Jones6cad5662017-06-14 13:25:13 -07006048 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumRequiresGLES31);
Jamie Madillbe849e42017-05-02 15:49:00 -04006049 return false;
6050 }
6051 switch (ConvertToGLenum(params[0]))
6052 {
6053 case GL_DEPTH_COMPONENT:
6054 case GL_STENCIL_INDEX:
6055 break;
6056
6057 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07006058 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04006059 return false;
6060 }
6061 break;
6062
6063 case GL_TEXTURE_SRGB_DECODE_EXT:
6064 if (!ValidateTextureSRGBDecodeValue(context, params))
6065 {
6066 return false;
6067 }
6068 break;
6069
Lingfeng Yangf97641c2018-06-21 19:22:45 -07006070 case GL_GENERATE_MIPMAP:
6071 case GL_TEXTURE_CROP_RECT_OES:
6072 if (context->getClientMajorVersion() > 1)
6073 {
6074 ANGLE_VALIDATION_ERR(context, InvalidEnum(), GLES1Only);
6075 return false;
6076 }
6077 break;
Jamie Madillbe849e42017-05-02 15:49:00 -04006078 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07006079 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04006080 return false;
6081 }
6082
6083 return true;
6084}
6085
Corentin Wallezf0e89be2017-11-08 14:00:32 -08006086template bool ValidateTexParameterBase(Context *, TextureType, GLenum, GLsizei, const GLfloat *);
6087template bool ValidateTexParameterBase(Context *, TextureType, GLenum, GLsizei, const GLint *);
Jamie Madillbe849e42017-05-02 15:49:00 -04006088
Jamie Madill5b772312018-03-08 20:28:32 -05006089bool ValidateVertexAttribIndex(Context *context, GLuint index)
Jamie Madill12e957f2017-08-26 21:42:26 -04006090{
6091 if (index >= MAX_VERTEX_ATTRIBS)
6092 {
6093 ANGLE_VALIDATION_ERR(context, InvalidValue(), IndexExceedsMaxVertexAttribute);
6094 return false;
6095 }
6096
6097 return true;
6098}
6099
6100bool ValidateGetActiveUniformBlockivBase(Context *context,
6101 GLuint program,
6102 GLuint uniformBlockIndex,
6103 GLenum pname,
6104 GLsizei *length)
6105{
6106 if (length)
6107 {
6108 *length = 0;
6109 }
6110
6111 if (context->getClientMajorVersion() < 3)
6112 {
6113 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ES3Required);
6114 return false;
6115 }
6116
6117 Program *programObject = GetValidProgram(context, program);
6118 if (!programObject)
6119 {
6120 return false;
6121 }
6122
6123 if (uniformBlockIndex >= programObject->getActiveUniformBlockCount())
6124 {
6125 context->handleError(InvalidValue()
6126 << "uniformBlockIndex exceeds active uniform block count.");
6127 return false;
6128 }
6129
6130 switch (pname)
6131 {
6132 case GL_UNIFORM_BLOCK_BINDING:
6133 case GL_UNIFORM_BLOCK_DATA_SIZE:
6134 case GL_UNIFORM_BLOCK_NAME_LENGTH:
6135 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
6136 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES:
6137 case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
6138 case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
6139 break;
6140
6141 default:
6142 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
6143 return false;
6144 }
6145
6146 if (length)
6147 {
6148 if (pname == GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES)
6149 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08006150 const InterfaceBlock &uniformBlock =
Jamie Madill12e957f2017-08-26 21:42:26 -04006151 programObject->getUniformBlockByIndex(uniformBlockIndex);
6152 *length = static_cast<GLsizei>(uniformBlock.memberIndexes.size());
6153 }
6154 else
6155 {
6156 *length = 1;
6157 }
6158 }
6159
6160 return true;
6161}
6162
Jamie Madill9696d072017-08-26 23:19:57 -04006163template <typename ParamType>
6164bool ValidateSamplerParameterBase(Context *context,
6165 GLuint sampler,
6166 GLenum pname,
6167 GLsizei bufSize,
6168 ParamType *params)
6169{
6170 if (context->getClientMajorVersion() < 3)
6171 {
6172 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ES3Required);
6173 return false;
6174 }
6175
6176 if (!context->isSampler(sampler))
6177 {
6178 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidSampler);
6179 return false;
6180 }
6181
6182 const GLsizei minBufSize = 1;
6183 if (bufSize >= 0 && bufSize < minBufSize)
6184 {
6185 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InsufficientBufferSize);
6186 return false;
6187 }
6188
6189 switch (pname)
6190 {
6191 case GL_TEXTURE_WRAP_S:
6192 case GL_TEXTURE_WRAP_T:
6193 case GL_TEXTURE_WRAP_R:
6194 if (!ValidateTextureWrapModeValue(context, params, false))
6195 {
6196 return false;
6197 }
6198 break;
6199
6200 case GL_TEXTURE_MIN_FILTER:
6201 if (!ValidateTextureMinFilterValue(context, params, false))
6202 {
6203 return false;
6204 }
6205 break;
6206
6207 case GL_TEXTURE_MAG_FILTER:
6208 if (!ValidateTextureMagFilterValue(context, params))
6209 {
6210 return false;
6211 }
6212 break;
6213
6214 case GL_TEXTURE_MIN_LOD:
6215 case GL_TEXTURE_MAX_LOD:
6216 // any value is permissible
6217 break;
6218
6219 case GL_TEXTURE_COMPARE_MODE:
6220 if (!ValidateTextureCompareModeValue(context, params))
6221 {
6222 return false;
6223 }
6224 break;
6225
6226 case GL_TEXTURE_COMPARE_FUNC:
6227 if (!ValidateTextureCompareFuncValue(context, params))
6228 {
6229 return false;
6230 }
6231 break;
6232
6233 case GL_TEXTURE_SRGB_DECODE_EXT:
6234 if (!ValidateTextureSRGBDecodeValue(context, params))
6235 {
6236 return false;
6237 }
6238 break;
6239
Luc Ferron1b1a8642018-01-23 15:12:01 -05006240 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
6241 {
6242 GLfloat paramValue = static_cast<GLfloat>(params[0]);
6243 if (!ValidateTextureMaxAnisotropyValue(context, paramValue))
6244 {
6245 return false;
6246 }
6247 }
6248 break;
6249
Jamie Madill9696d072017-08-26 23:19:57 -04006250 default:
6251 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
6252 return false;
6253 }
6254
6255 return true;
6256}
6257
6258template bool ValidateSamplerParameterBase(Context *, GLuint, GLenum, GLsizei, GLfloat *);
6259template bool ValidateSamplerParameterBase(Context *, GLuint, GLenum, GLsizei, GLint *);
6260
6261bool ValidateGetSamplerParameterBase(Context *context,
6262 GLuint sampler,
6263 GLenum pname,
6264 GLsizei *length)
6265{
6266 if (length)
6267 {
6268 *length = 0;
6269 }
6270
6271 if (context->getClientMajorVersion() < 3)
6272 {
6273 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ES3Required);
6274 return false;
6275 }
6276
6277 if (!context->isSampler(sampler))
6278 {
6279 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidSampler);
6280 return false;
6281 }
6282
6283 switch (pname)
6284 {
6285 case GL_TEXTURE_WRAP_S:
6286 case GL_TEXTURE_WRAP_T:
6287 case GL_TEXTURE_WRAP_R:
6288 case GL_TEXTURE_MIN_FILTER:
6289 case GL_TEXTURE_MAG_FILTER:
6290 case GL_TEXTURE_MIN_LOD:
6291 case GL_TEXTURE_MAX_LOD:
6292 case GL_TEXTURE_COMPARE_MODE:
6293 case GL_TEXTURE_COMPARE_FUNC:
6294 break;
6295
Luc Ferron1b1a8642018-01-23 15:12:01 -05006296 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
6297 if (!ValidateTextureMaxAnisotropyExtensionEnabled(context))
6298 {
6299 return false;
6300 }
6301 break;
6302
Jamie Madill9696d072017-08-26 23:19:57 -04006303 case GL_TEXTURE_SRGB_DECODE_EXT:
6304 if (!context->getExtensions().textureSRGBDecode)
6305 {
6306 context->handleError(InvalidEnum() << "GL_EXT_texture_sRGB_decode is not enabled.");
6307 return false;
6308 }
6309 break;
6310
6311 default:
6312 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
6313 return false;
6314 }
6315
6316 if (length)
6317 {
6318 *length = 1;
6319 }
6320 return true;
6321}
6322
6323bool ValidateGetInternalFormativBase(Context *context,
6324 GLenum target,
6325 GLenum internalformat,
6326 GLenum pname,
6327 GLsizei bufSize,
6328 GLsizei *numParams)
6329{
6330 if (numParams)
6331 {
6332 *numParams = 0;
6333 }
6334
6335 if (context->getClientMajorVersion() < 3)
6336 {
Yunchao Hef0fd87d2017-09-12 04:55:05 +08006337 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ES3Required);
Jamie Madill9696d072017-08-26 23:19:57 -04006338 return false;
6339 }
6340
6341 const TextureCaps &formatCaps = context->getTextureCaps().get(internalformat);
Yuly Novikovf15f8862018-06-04 18:59:41 -04006342 if (!formatCaps.renderbuffer)
Jamie Madill9696d072017-08-26 23:19:57 -04006343 {
6344 context->handleError(InvalidEnum() << "Internal format is not renderable.");
6345 return false;
6346 }
6347
6348 switch (target)
6349 {
6350 case GL_RENDERBUFFER:
6351 break;
6352
6353 case GL_TEXTURE_2D_MULTISAMPLE:
6354 if (context->getClientVersion() < ES_3_1)
6355 {
6356 context->handleError(InvalidOperation()
6357 << "Texture target requires at least OpenGL ES 3.1.");
6358 return false;
6359 }
6360 break;
6361
6362 default:
6363 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTarget);
6364 return false;
6365 }
6366
6367 if (bufSize < 0)
6368 {
6369 ANGLE_VALIDATION_ERR(context, InvalidValue(), InsufficientBufferSize);
6370 return false;
6371 }
6372
6373 GLsizei maxWriteParams = 0;
6374 switch (pname)
6375 {
6376 case GL_NUM_SAMPLE_COUNTS:
6377 maxWriteParams = 1;
6378 break;
6379
6380 case GL_SAMPLES:
6381 maxWriteParams = static_cast<GLsizei>(formatCaps.sampleCounts.size());
6382 break;
6383
6384 default:
6385 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
6386 return false;
6387 }
6388
6389 if (numParams)
6390 {
6391 // glGetInternalFormativ will not overflow bufSize
6392 *numParams = std::min(bufSize, maxWriteParams);
6393 }
6394
6395 return true;
6396}
6397
Jamie Madille98b1b52018-03-08 09:47:23 -05006398bool ValidateFramebufferNotMultisampled(Context *context, Framebuffer *framebuffer)
6399{
Jamie Madill427064d2018-04-13 16:20:34 -04006400 if (framebuffer->getSamples(context) != 0)
Jamie Madille98b1b52018-03-08 09:47:23 -05006401 {
6402 context->handleError(InvalidOperation());
6403 return false;
6404 }
6405 return true;
6406}
6407
Lingfeng Yang038dd532018-03-29 17:31:52 -07006408bool ValidateMultitextureUnit(Context *context, GLenum texture)
6409{
6410 if (texture < GL_TEXTURE0 || texture >= GL_TEXTURE0 + context->getCaps().maxMultitextureUnits)
6411 {
6412 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidMultitextureUnit);
6413 return false;
6414 }
6415 return true;
6416}
6417
Jamie Madillc29968b2016-01-20 11:17:23 -05006418} // namespace gl