blob: eb6bfd2d7bb43feed0c27b46ad2f2618b7748fa3 [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"
Jamie Madill231c7f52017-04-26 13:45:37 -040021#include "libANGLE/Uniform.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050022#include "libANGLE/VertexArray.h"
Jamie Madill231c7f52017-04-26 13:45:37 -040023#include "libANGLE/formatutils.h"
24#include "libANGLE/validationES2.h"
25#include "libANGLE/validationES3.h"
Geoff Lange8ebe7f2013-08-05 15:03:13 -040026
27#include "common/mathutil.h"
28#include "common/utilities.h"
29
Jamie Madille2e406c2016-06-02 13:04:10 -040030using namespace angle;
31
Geoff Lange8ebe7f2013-08-05 15:03:13 -040032namespace gl
33{
Jamie Madill1ca74672015-07-21 15:14:11 -040034namespace
35{
Corentin Wallez92db6942016-12-09 13:10:36 -050036bool ValidateDrawAttribs(ValidationContext *context,
37 GLint primcount,
38 GLint maxVertex,
39 GLint vertexCount)
Jamie Madill1ca74672015-07-21 15:14:11 -040040{
Jamie Madilldfde6ab2016-06-09 07:07:18 -070041 const gl::State &state = context->getGLState();
Jamie Madill1ca74672015-07-21 15:14:11 -040042 const gl::Program *program = state.getProgram();
43
Corentin Wallez327411e2016-12-09 11:09:17 -050044 bool webglCompatibility = context->getExtensions().webglCompatibility;
45
Jamie Madill231c7f52017-04-26 13:45:37 -040046 const VertexArray *vao = state.getVertexArray();
47 const auto &vertexAttribs = vao->getVertexAttributes();
Jiawei-Shao2597fb62016-12-09 16:38:02 +080048 const auto &vertexBindings = vao->getVertexBindings();
Jamie Madill231c7f52017-04-26 13:45:37 -040049 size_t maxEnabledAttrib = vao->getMaxEnabledAttribute();
Jamie Madill1ca74672015-07-21 15:14:11 -040050 for (size_t attributeIndex = 0; attributeIndex < maxEnabledAttrib; ++attributeIndex)
51 {
52 const VertexAttribute &attrib = vertexAttribs[attributeIndex];
Corentin Wallez672f7f32017-06-15 17:42:17 -040053
54 // No need to range check for disabled attribs.
55 if (!attrib.enabled)
Jamie Madill1ca74672015-07-21 15:14:11 -040056 {
Corentin Wallezfd456442016-12-21 17:57:00 -050057 continue;
58 }
Jamie Madill1ca74672015-07-21 15:14:11 -040059
Jamie Madill231c7f52017-04-26 13:45:37 -040060 // If we have no buffer, then we either get an error, or there are no more checks to be
61 // done.
Corentin Wallez672f7f32017-06-15 17:42:17 -040062 const VertexBinding &binding = vertexBindings[attrib.bindingIndex];
63 gl::Buffer *buffer = binding.getBuffer().get();
Corentin Wallezfd456442016-12-21 17:57:00 -050064 if (!buffer)
65 {
Geoff Langfeb8c682017-02-13 16:07:35 -050066 if (webglCompatibility || !state.areClientArraysEnabled())
Corentin Wallez327411e2016-12-09 11:09:17 -050067 {
68 // [WebGL 1.0] Section 6.5 Enabled Vertex Attributes and Range Checking
Corentin Wallezfd456442016-12-21 17:57:00 -050069 // If a vertex attribute is enabled as an array via enableVertexAttribArray but
70 // no buffer is bound to that attribute via bindBuffer and vertexAttribPointer,
71 // then calls to drawArrays or drawElements will generate an INVALID_OPERATION
72 // error.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -050073 context->handleError(InvalidOperation()
74 << "An enabled vertex array has no buffer.");
Corentin Wallezfd456442016-12-21 17:57:00 -050075 return false;
Corentin Wallez327411e2016-12-09 11:09:17 -050076 }
Corentin Wallezfd456442016-12-21 17:57:00 -050077 else if (attrib.pointer == nullptr)
Jamie Madill1ca74672015-07-21 15:14:11 -040078 {
79 // This is an application error that would normally result in a crash,
80 // but we catch it and return an error
Yuly Novikovc4d18aa2017-03-09 18:45:02 -050081 context->handleError(InvalidOperation()
82 << "An enabled vertex array has no buffer and no pointer.");
Jamie Madill1ca74672015-07-21 15:14:11 -040083 return false;
84 }
Corentin Wallezfd456442016-12-21 17:57:00 -050085 continue;
86 }
87
Corentin Wallez672f7f32017-06-15 17:42:17 -040088 // This needs to come after the check for client arrays as even unused attributes cannot use
89 // client-side arrays
90 if (!program->isAttribLocationActive(attributeIndex))
91 {
92 continue;
93 }
94
Corentin Wallezfd456442016-12-21 17:57:00 -050095 // If we're drawing zero vertices, we have enough data.
96 if (vertexCount <= 0 || primcount <= 0)
97 {
98 continue;
99 }
100
101 GLint maxVertexElement = 0;
Martin Radevdd5f27e2017-06-07 10:17:09 +0300102 GLuint divisor = binding.getDivisor();
103 if (divisor == 0)
Corentin Wallezfd456442016-12-21 17:57:00 -0500104 {
105 maxVertexElement = maxVertex;
106 }
107 else
108 {
Martin Radevdd5f27e2017-06-07 10:17:09 +0300109 maxVertexElement = (primcount - 1) / divisor;
Corentin Wallezfd456442016-12-21 17:57:00 -0500110 }
111
112 // We do manual overflow checks here instead of using safe_math.h because it was
113 // a bottleneck. Thanks to some properties of GL we know inequalities that can
114 // help us make the overflow checks faster.
115
116 // The max possible attribSize is 16 for a vector of 4 32 bit values.
117 constexpr uint64_t kMaxAttribSize = 16;
118 constexpr uint64_t kIntMax = std::numeric_limits<int>::max();
119 constexpr uint64_t kUint64Max = std::numeric_limits<uint64_t>::max();
120
121 // We know attribStride is given as a GLsizei which is typedefed to int.
122 // We also know an upper bound for attribSize.
123 static_assert(std::is_same<int, GLsizei>::value, "");
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800124 uint64_t attribStride = ComputeVertexAttributeStride(attrib, binding);
Corentin Wallezfd456442016-12-21 17:57:00 -0500125 uint64_t attribSize = ComputeVertexAttributeTypeSize(attrib);
126 ASSERT(attribStride <= kIntMax && attribSize <= kMaxAttribSize);
127
128 // Computing the max offset using uint64_t without attrib.offset is overflow
129 // safe. Note: Last vertex element does not take the full stride!
130 static_assert(kIntMax * kIntMax < kUint64Max - kMaxAttribSize, "");
131 uint64_t attribDataSizeNoOffset = maxVertexElement * attribStride + attribSize;
132
133 // An overflow can happen when adding the offset, check for it.
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800134 uint64_t attribOffset = ComputeVertexAttributeOffset(attrib, binding);
135 if (attribDataSizeNoOffset > kUint64Max - attribOffset)
Corentin Wallezfd456442016-12-21 17:57:00 -0500136 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500137 context->handleError(InvalidOperation() << "Integer overflow.");
Corentin Wallezfd456442016-12-21 17:57:00 -0500138 return false;
139 }
140 uint64_t attribDataSizeWithOffset = attribDataSizeNoOffset + attribOffset;
141
142 // [OpenGL ES 3.0.2] section 2.9.4 page 40:
143 // We can return INVALID_OPERATION if our vertex attribute does not have
144 // enough backing data.
145 if (attribDataSizeWithOffset > static_cast<uint64_t>(buffer->getSize()))
146 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500147 context->handleError(InvalidOperation()
148 << "Vertex buffer is not big enough for the draw call");
Corentin Wallezfd456442016-12-21 17:57:00 -0500149 return false;
Jamie Madill1ca74672015-07-21 15:14:11 -0400150 }
151 }
152
153 return true;
154}
155
Geoff Lang280ba992017-04-18 16:30:58 -0400156bool ValidReadPixelsTypeEnum(ValidationContext *context, GLenum type)
157{
158 switch (type)
159 {
160 // Types referenced in Table 3.4 of the ES 2.0.25 spec
161 case GL_UNSIGNED_BYTE:
162 case GL_UNSIGNED_SHORT_4_4_4_4:
163 case GL_UNSIGNED_SHORT_5_5_5_1:
164 case GL_UNSIGNED_SHORT_5_6_5:
165 return context->getClientVersion() >= ES_2_0;
166
167 // Types referenced in Table 3.2 of the ES 3.0.5 spec (Except depth stencil)
168 case GL_BYTE:
169 case GL_INT:
170 case GL_SHORT:
171 case GL_UNSIGNED_INT:
172 case GL_UNSIGNED_INT_10F_11F_11F_REV:
173 case GL_UNSIGNED_INT_24_8:
174 case GL_UNSIGNED_INT_2_10_10_10_REV:
175 case GL_UNSIGNED_INT_5_9_9_9_REV:
176 case GL_UNSIGNED_SHORT:
177 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
178 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
179 return context->getClientVersion() >= ES_3_0;
180
181 case GL_FLOAT:
182 return context->getClientVersion() >= ES_3_0 || context->getExtensions().textureFloat;
183
184 case GL_HALF_FLOAT:
185 return context->getClientVersion() >= ES_3_0 ||
186 context->getExtensions().textureHalfFloat;
187
188 case GL_HALF_FLOAT_OES:
189 return context->getExtensions().colorBufferHalfFloat;
190
191 default:
192 return false;
193 }
194}
195
196bool ValidReadPixelsFormatEnum(ValidationContext *context, GLenum format)
197{
198 switch (format)
199 {
200 // Formats referenced in Table 3.4 of the ES 2.0.25 spec (Except luminance)
201 case GL_RGBA:
202 case GL_RGB:
203 case GL_ALPHA:
204 return context->getClientVersion() >= ES_2_0;
205
206 // Formats referenced in Table 3.2 of the ES 3.0.5 spec
207 case GL_RG:
208 case GL_RED:
209 case GL_RGBA_INTEGER:
210 case GL_RGB_INTEGER:
211 case GL_RG_INTEGER:
212 case GL_RED_INTEGER:
213 return context->getClientVersion() >= ES_3_0;
214
215 case GL_SRGB_ALPHA_EXT:
216 case GL_SRGB_EXT:
217 return context->getExtensions().sRGB;
218
219 case GL_BGRA_EXT:
220 return context->getExtensions().readFormatBGRA;
221
222 default:
223 return false;
224 }
225}
226
Geoff Langf607c602016-09-21 11:46:48 -0400227bool ValidReadPixelsFormatType(ValidationContext *context,
228 GLenum framebufferComponentType,
229 GLenum format,
230 GLenum type)
231{
232 switch (framebufferComponentType)
233 {
234 case GL_UNSIGNED_NORMALIZED:
235 // TODO(geofflang): Don't accept BGRA here. Some chrome internals appear to try to use
236 // ReadPixels with BGRA even if the extension is not present
237 return (format == GL_RGBA && type == GL_UNSIGNED_BYTE) ||
238 (context->getExtensions().readFormatBGRA && format == GL_BGRA_EXT &&
239 type == GL_UNSIGNED_BYTE);
240
241 case GL_SIGNED_NORMALIZED:
242 return (format == GL_RGBA && type == GL_UNSIGNED_BYTE);
243
244 case GL_INT:
245 return (format == GL_RGBA_INTEGER && type == GL_INT);
246
247 case GL_UNSIGNED_INT:
248 return (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_INT);
249
250 case GL_FLOAT:
251 return (format == GL_RGBA && type == GL_FLOAT);
252
253 default:
254 UNREACHABLE();
255 return false;
256 }
257}
258
Geoff Langc1984ed2016-10-07 12:41:00 -0400259template <typename ParamType>
260bool ValidateTextureWrapModeValue(Context *context, ParamType *params, bool isExternalTextureTarget)
261{
262 switch (ConvertToGLenum(params[0]))
263 {
264 case GL_CLAMP_TO_EDGE:
265 break;
266
267 case GL_REPEAT:
268 case GL_MIRRORED_REPEAT:
269 if (isExternalTextureTarget)
270 {
271 // OES_EGL_image_external specifies this error.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500272 context->handleError(InvalidEnum()
273 << "external textures only support CLAMP_TO_EDGE wrap mode");
Geoff Langc1984ed2016-10-07 12:41:00 -0400274 return false;
275 }
276 break;
277
278 default:
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500279 context->handleError(InvalidEnum() << "Unknown param value.");
Geoff Langc1984ed2016-10-07 12:41:00 -0400280 return false;
281 }
282
283 return true;
284}
285
286template <typename ParamType>
287bool ValidateTextureMinFilterValue(Context *context,
288 ParamType *params,
289 bool isExternalTextureTarget)
290{
291 switch (ConvertToGLenum(params[0]))
292 {
293 case GL_NEAREST:
294 case GL_LINEAR:
295 break;
296
297 case GL_NEAREST_MIPMAP_NEAREST:
298 case GL_LINEAR_MIPMAP_NEAREST:
299 case GL_NEAREST_MIPMAP_LINEAR:
300 case GL_LINEAR_MIPMAP_LINEAR:
301 if (isExternalTextureTarget)
302 {
303 // OES_EGL_image_external specifies this error.
304 context->handleError(
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500305 InvalidEnum() << "external textures only support NEAREST and LINEAR filtering");
Geoff Langc1984ed2016-10-07 12:41:00 -0400306 return false;
307 }
308 break;
309
310 default:
Brandon Jones6cad5662017-06-14 13:25:13 -0700311 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTextureFilterParam);
Geoff Langc1984ed2016-10-07 12:41:00 -0400312 return false;
313 }
314
315 return true;
316}
317
318template <typename ParamType>
319bool ValidateTextureMagFilterValue(Context *context, ParamType *params)
320{
321 switch (ConvertToGLenum(params[0]))
322 {
323 case GL_NEAREST:
324 case GL_LINEAR:
325 break;
326
327 default:
Brandon Jones6cad5662017-06-14 13:25:13 -0700328 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTextureFilterParam);
Geoff Langc1984ed2016-10-07 12:41:00 -0400329 return false;
330 }
331
332 return true;
333}
334
335template <typename ParamType>
336bool ValidateTextureCompareModeValue(Context *context, ParamType *params)
337{
338 // Acceptable mode parameters from GLES 3.0.2 spec, table 3.17
339 switch (ConvertToGLenum(params[0]))
340 {
341 case GL_NONE:
342 case GL_COMPARE_REF_TO_TEXTURE:
343 break;
344
345 default:
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500346 context->handleError(InvalidEnum() << "Unknown param value.");
Geoff Langc1984ed2016-10-07 12:41:00 -0400347 return false;
348 }
349
350 return true;
351}
352
353template <typename ParamType>
354bool ValidateTextureCompareFuncValue(Context *context, ParamType *params)
355{
356 // Acceptable function parameters from GLES 3.0.2 spec, table 3.17
357 switch (ConvertToGLenum(params[0]))
358 {
359 case GL_LEQUAL:
360 case GL_GEQUAL:
361 case GL_LESS:
362 case GL_GREATER:
363 case GL_EQUAL:
364 case GL_NOTEQUAL:
365 case GL_ALWAYS:
366 case GL_NEVER:
367 break;
368
369 default:
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500370 context->handleError(InvalidEnum() << "Unknown param value.");
Geoff Langc1984ed2016-10-07 12:41:00 -0400371 return false;
372 }
373
374 return true;
375}
376
377template <typename ParamType>
Geoff Lang81c6b572016-10-19 14:07:52 -0700378bool ValidateTextureSRGBDecodeValue(Context *context, ParamType *params)
379{
380 if (!context->getExtensions().textureSRGBDecode)
381 {
Brandon Jones6cad5662017-06-14 13:25:13 -0700382 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ExtensionNotEnabled);
Geoff Lang81c6b572016-10-19 14:07:52 -0700383 return false;
384 }
385
386 switch (ConvertToGLenum(params[0]))
387 {
388 case GL_DECODE_EXT:
389 case GL_SKIP_DECODE_EXT:
390 break;
391
392 default:
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500393 context->handleError(InvalidEnum() << "Unknown param value.");
Geoff Lang81c6b572016-10-19 14:07:52 -0700394 return false;
395 }
396
397 return true;
398}
399
400template <typename ParamType>
Geoff Langc1984ed2016-10-07 12:41:00 -0400401bool ValidateSamplerParameterBase(Context *context,
402 GLuint sampler,
403 GLenum pname,
404 GLsizei bufSize,
405 ParamType *params)
406{
407 if (context->getClientMajorVersion() < 3)
408 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500409 context->handleError(InvalidOperation() << "Context does not support OpenGL ES 3.0.");
Geoff Langc1984ed2016-10-07 12:41:00 -0400410 return false;
411 }
412
413 if (!context->isSampler(sampler))
414 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500415 context->handleError(InvalidOperation() << "Sampler is not valid.");
Geoff Langc1984ed2016-10-07 12:41:00 -0400416 return false;
417 }
418
419 const GLsizei minBufSize = 1;
420 if (bufSize >= 0 && bufSize < minBufSize)
421 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500422 context->handleError(InvalidOperation() << "bufSize must be at least " << minBufSize);
Geoff Langc1984ed2016-10-07 12:41:00 -0400423 return false;
424 }
425
426 switch (pname)
427 {
428 case GL_TEXTURE_WRAP_S:
429 case GL_TEXTURE_WRAP_T:
430 case GL_TEXTURE_WRAP_R:
431 if (!ValidateTextureWrapModeValue(context, params, false))
432 {
433 return false;
434 }
435 break;
436
437 case GL_TEXTURE_MIN_FILTER:
438 if (!ValidateTextureMinFilterValue(context, params, false))
439 {
440 return false;
441 }
442 break;
443
444 case GL_TEXTURE_MAG_FILTER:
445 if (!ValidateTextureMagFilterValue(context, params))
446 {
447 return false;
448 }
449 break;
450
451 case GL_TEXTURE_MIN_LOD:
452 case GL_TEXTURE_MAX_LOD:
453 // any value is permissible
454 break;
455
456 case GL_TEXTURE_COMPARE_MODE:
457 if (!ValidateTextureCompareModeValue(context, params))
458 {
459 return false;
460 }
461 break;
462
463 case GL_TEXTURE_COMPARE_FUNC:
464 if (!ValidateTextureCompareFuncValue(context, params))
465 {
466 return false;
467 }
468 break;
469
Geoff Lang81c6b572016-10-19 14:07:52 -0700470 case GL_TEXTURE_SRGB_DECODE_EXT:
471 if (!ValidateTextureSRGBDecodeValue(context, params))
472 {
473 return false;
474 }
475 break;
476
Geoff Langc1984ed2016-10-07 12:41:00 -0400477 default:
Brandon Jones6cad5662017-06-14 13:25:13 -0700478 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Geoff Langc1984ed2016-10-07 12:41:00 -0400479 return false;
480 }
481
482 return true;
483}
484
485bool ValidateGetSamplerParameterBase(Context *context,
486 GLuint sampler,
487 GLenum pname,
488 GLsizei *length)
489{
490 if (length)
491 {
492 *length = 0;
493 }
494
495 if (context->getClientMajorVersion() < 3)
496 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500497 context->handleError(InvalidOperation() << "Context does not support OpenGL ES 3.0.");
Geoff Langc1984ed2016-10-07 12:41:00 -0400498 return false;
499 }
500
501 if (!context->isSampler(sampler))
502 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500503 context->handleError(InvalidOperation() << "Sampler is not valid.");
Geoff Langc1984ed2016-10-07 12:41:00 -0400504 return false;
505 }
506
507 switch (pname)
508 {
509 case GL_TEXTURE_WRAP_S:
510 case GL_TEXTURE_WRAP_T:
511 case GL_TEXTURE_WRAP_R:
512 case GL_TEXTURE_MIN_FILTER:
513 case GL_TEXTURE_MAG_FILTER:
514 case GL_TEXTURE_MIN_LOD:
515 case GL_TEXTURE_MAX_LOD:
516 case GL_TEXTURE_COMPARE_MODE:
517 case GL_TEXTURE_COMPARE_FUNC:
518 break;
519
Geoff Lang81c6b572016-10-19 14:07:52 -0700520 case GL_TEXTURE_SRGB_DECODE_EXT:
521 if (!context->getExtensions().textureSRGBDecode)
522 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500523 context->handleError(InvalidEnum() << "GL_EXT_texture_sRGB_decode is not enabled.");
Geoff Lang81c6b572016-10-19 14:07:52 -0700524 return false;
525 }
526 break;
527
Geoff Langc1984ed2016-10-07 12:41:00 -0400528 default:
Brandon Jones6cad5662017-06-14 13:25:13 -0700529 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Geoff Langc1984ed2016-10-07 12:41:00 -0400530 return false;
531 }
532
533 if (length)
534 {
535 *length = 1;
536 }
537 return true;
538}
539
Geoff Lang6899b872016-10-14 11:30:13 -0400540bool ValidateGetActiveUniformBlockivBase(Context *context,
541 GLuint program,
542 GLuint uniformBlockIndex,
543 GLenum pname,
544 GLsizei *length)
545{
546 if (length)
547 {
548 *length = 0;
549 }
550
551 if (context->getClientMajorVersion() < 3)
552 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500553 context->handleError(InvalidOperation() << "Context does not support OpenGL ES 3.0.");
Geoff Lang6899b872016-10-14 11:30:13 -0400554 return false;
555 }
556
557 Program *programObject = GetValidProgram(context, program);
558 if (!programObject)
559 {
560 return false;
561 }
562
563 if (uniformBlockIndex >= programObject->getActiveUniformBlockCount())
564 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500565 context->handleError(InvalidValue()
566 << "uniformBlockIndex exceeds active uniform block count.");
Geoff Lang6899b872016-10-14 11:30:13 -0400567 return false;
568 }
569
570 switch (pname)
571 {
572 case GL_UNIFORM_BLOCK_BINDING:
573 case GL_UNIFORM_BLOCK_DATA_SIZE:
574 case GL_UNIFORM_BLOCK_NAME_LENGTH:
575 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
576 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES:
577 case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
578 case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
579 break;
580
581 default:
Brandon Jones6cad5662017-06-14 13:25:13 -0700582 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Geoff Lang6899b872016-10-14 11:30:13 -0400583 return false;
584 }
585
586 if (length)
587 {
588 if (pname == GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES)
589 {
590 const UniformBlock &uniformBlock =
591 programObject->getUniformBlockByIndex(uniformBlockIndex);
jchen10eaef1e52017-06-13 10:44:11 +0800592 *length = static_cast<GLsizei>(uniformBlock.memberIndexes.size());
Geoff Lang6899b872016-10-14 11:30:13 -0400593 }
594 else
595 {
596 *length = 1;
597 }
598 }
599
600 return true;
601}
602
Geoff Lang0a9661f2016-10-20 10:59:20 -0700603bool ValidateGetInternalFormativBase(Context *context,
604 GLenum target,
605 GLenum internalformat,
606 GLenum pname,
607 GLsizei bufSize,
608 GLsizei *numParams)
609{
610 if (numParams)
611 {
612 *numParams = 0;
613 }
614
615 if (context->getClientMajorVersion() < 3)
616 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500617 context->handleError(InvalidOperation() << "Context does not support OpenGL ES 3.0.");
Geoff Lang0a9661f2016-10-20 10:59:20 -0700618 return false;
619 }
620
621 const TextureCaps &formatCaps = context->getTextureCaps().get(internalformat);
622 if (!formatCaps.renderable)
623 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500624 context->handleError(InvalidEnum() << "Internal format is not renderable.");
Geoff Lang0a9661f2016-10-20 10:59:20 -0700625 return false;
626 }
627
628 switch (target)
629 {
630 case GL_RENDERBUFFER:
631 break;
632
JiangYizhoubddc46b2016-12-09 09:50:51 +0800633 case GL_TEXTURE_2D_MULTISAMPLE:
634 if (context->getClientVersion() < ES_3_1)
635 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500636 context->handleError(InvalidOperation()
637 << "Texture target requires at least OpenGL ES 3.1.");
JiangYizhoubddc46b2016-12-09 09:50:51 +0800638 return false;
639 }
640 break;
641
Geoff Lang0a9661f2016-10-20 10:59:20 -0700642 default:
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500643 context->handleError(InvalidEnum() << "Invalid target.");
Geoff Lang0a9661f2016-10-20 10:59:20 -0700644 return false;
645 }
646
647 if (bufSize < 0)
648 {
Brandon Jones6cad5662017-06-14 13:25:13 -0700649 ANGLE_VALIDATION_ERR(context, InvalidValue(), InsufficientBufferSize);
Geoff Lang0a9661f2016-10-20 10:59:20 -0700650 return false;
651 }
652
653 GLsizei maxWriteParams = 0;
654 switch (pname)
655 {
656 case GL_NUM_SAMPLE_COUNTS:
657 maxWriteParams = 1;
658 break;
659
660 case GL_SAMPLES:
661 maxWriteParams = static_cast<GLsizei>(formatCaps.sampleCounts.size());
662 break;
663
664 default:
Brandon Jones6cad5662017-06-14 13:25:13 -0700665 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Geoff Lang0a9661f2016-10-20 10:59:20 -0700666 return false;
667 }
668
669 if (numParams)
670 {
671 // glGetInternalFormativ will not overflow bufSize
672 *numParams = std::min(bufSize, maxWriteParams);
673 }
674
675 return true;
676}
677
Jamie Madillc1d770e2017-04-13 17:31:24 -0400678bool ValidateUniformCommonBase(ValidationContext *context,
Frank Henigmanf5f74ae2017-02-02 21:14:23 -0500679 gl::Program *program,
680 GLint location,
681 GLsizei count,
682 const LinkedUniform **uniformOut)
683{
684 // TODO(Jiajia): Add image uniform check in future.
685 if (count < 0)
686 {
Brandon Jones6cad5662017-06-14 13:25:13 -0700687 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeCount);
Frank Henigmanf5f74ae2017-02-02 21:14:23 -0500688 return false;
689 }
690
Brandon Jones6cad5662017-06-14 13:25:13 -0700691 if (!program)
Frank Henigmanf5f74ae2017-02-02 21:14:23 -0500692 {
Brandon Jones6cad5662017-06-14 13:25:13 -0700693 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidProgramName);
694 return false;
695 }
696
697 if (!program->isLinked())
698 {
699 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ProgramNotLinked);
Frank Henigmanf5f74ae2017-02-02 21:14:23 -0500700 return false;
701 }
702
703 if (location == -1)
704 {
705 // Silently ignore the uniform command
706 return false;
707 }
708
709 const auto &uniformLocations = program->getUniformLocations();
Jamie Madillbe849e42017-05-02 15:49:00 -0400710 size_t castedLocation = static_cast<size_t>(location);
Frank Henigmanf5f74ae2017-02-02 21:14:23 -0500711 if (castedLocation >= uniformLocations.size())
712 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500713 context->handleError(InvalidOperation() << "Invalid uniform location");
Frank Henigmanf5f74ae2017-02-02 21:14:23 -0500714 return false;
715 }
716
717 const auto &uniformLocation = uniformLocations[castedLocation];
718 if (uniformLocation.ignored)
719 {
720 // Silently ignore the uniform command
721 return false;
722 }
723
724 if (!uniformLocation.used)
725 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500726 context->handleError(InvalidOperation());
Frank Henigmanf5f74ae2017-02-02 21:14:23 -0500727 return false;
728 }
729
730 const auto &uniform = program->getUniformByIndex(uniformLocation.index);
731
732 // attempting to write an array to a non-array uniform is an INVALID_OPERATION
733 if (!uniform.isArray() && count > 1)
734 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500735 context->handleError(InvalidOperation());
Frank Henigmanf5f74ae2017-02-02 21:14:23 -0500736 return false;
737 }
738
739 *uniformOut = &uniform;
740 return true;
741}
742
Frank Henigman999b0fd2017-02-02 21:45:55 -0500743bool ValidateUniform1ivValue(ValidationContext *context,
Frank Henigmana98a6472017-02-02 21:38:32 -0500744 GLenum uniformType,
745 GLsizei count,
746 const GLint *value)
747{
748 // Value type is GL_INT, because we only get here from glUniform1i{v}.
749 // It is compatible with INT or BOOL.
750 // Do these cheap tests first, for a little extra speed.
751 if (GL_INT == uniformType || GL_BOOL == uniformType)
752 {
753 return true;
754 }
755
756 if (IsSamplerType(uniformType))
757 {
Frank Henigman999b0fd2017-02-02 21:45:55 -0500758 // Check that the values are in range.
759 const GLint max = context->getCaps().maxCombinedTextureImageUnits;
760 for (GLsizei i = 0; i < count; ++i)
761 {
762 if (value[i] < 0 || value[i] >= max)
763 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500764 context->handleError(InvalidValue() << "sampler uniform value out of range");
Frank Henigman999b0fd2017-02-02 21:45:55 -0500765 return false;
766 }
767 }
Frank Henigmana98a6472017-02-02 21:38:32 -0500768 return true;
769 }
770
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500771 context->handleError(InvalidOperation() << "wrong type of value for uniform");
Frank Henigmana98a6472017-02-02 21:38:32 -0500772 return false;
773}
774
Jamie Madillc1d770e2017-04-13 17:31:24 -0400775bool ValidateUniformValue(ValidationContext *context, GLenum valueType, GLenum uniformType)
Frank Henigmanf5f74ae2017-02-02 21:14:23 -0500776{
777 // Check that the value type is compatible with uniform type.
Frank Henigmana98a6472017-02-02 21:38:32 -0500778 // Do the cheaper test first, for a little extra speed.
779 if (valueType == uniformType || VariableBoolVectorType(valueType) == uniformType)
Frank Henigmanf5f74ae2017-02-02 21:14:23 -0500780 {
781 return true;
782 }
783
Brandon Jones6cad5662017-06-14 13:25:13 -0700784 ANGLE_VALIDATION_ERR(context, InvalidOperation(), UniformSizeDoesNotMatchMethod);
Frank Henigmanf5f74ae2017-02-02 21:14:23 -0500785 return false;
786}
787
Jamie Madillc1d770e2017-04-13 17:31:24 -0400788bool ValidateUniformMatrixValue(ValidationContext *context, GLenum valueType, GLenum uniformType)
Frank Henigmanf5f74ae2017-02-02 21:14:23 -0500789{
790 // Check that the value type is compatible with uniform type.
791 if (valueType == uniformType)
792 {
793 return true;
794 }
795
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500796 context->handleError(InvalidOperation() << "wrong type of value for uniform");
Frank Henigmanf5f74ae2017-02-02 21:14:23 -0500797 return false;
798}
799
Geoff Lange0cff192017-05-30 13:04:56 -0400800bool ValidateFragmentShaderColorBufferTypeMatch(ValidationContext *context)
801{
802 const Program *program = context->getGLState().getProgram();
803 const Framebuffer *framebuffer = context->getGLState().getDrawFramebuffer();
804
805 const auto &programOutputTypes = program->getOutputVariableTypes();
806 for (size_t drawBufferIdx = 0; drawBufferIdx < programOutputTypes.size(); drawBufferIdx++)
807 {
808 GLenum outputType = programOutputTypes[drawBufferIdx];
809 GLenum inputType = framebuffer->getDrawbufferWriteType(drawBufferIdx);
810 if (outputType != GL_NONE && inputType != GL_NONE && inputType != outputType)
811 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500812 context->handleError(InvalidOperation() << "Fragment shader output type does not "
813 "match the bound framebuffer attachment "
814 "type.");
Geoff Lange0cff192017-05-30 13:04:56 -0400815 return false;
816 }
817 }
818
819 return true;
820}
821
Geoff Lang9ab5b822017-05-30 16:19:23 -0400822bool ValidateVertexShaderAttributeTypeMatch(ValidationContext *context)
823{
824 const Program *program = context->getGLState().getProgram();
825 const VertexArray *vao = context->getGLState().getVertexArray();
826
827 for (const auto &shaderAttribute : program->getAttributes())
828 {
Geoff Lang69df2422017-07-05 12:42:31 -0400829 // gl_VertexID and gl_InstanceID are active attributes but don't have a bound attribute.
830 if (shaderAttribute.isBuiltIn())
831 {
832 continue;
833 }
834
Geoff Lang9ab5b822017-05-30 16:19:23 -0400835 GLenum shaderInputType = VariableComponentType(shaderAttribute.type);
836
837 const auto &attrib = vao->getVertexAttribute(shaderAttribute.location);
838 const auto &currentValue =
839 context->getGLState().getVertexAttribCurrentValue(shaderAttribute.location);
840 GLenum vertexType = attrib.enabled ? GetVertexAttributeBaseType(attrib) : currentValue.Type;
841
842 if (shaderInputType != GL_NONE && vertexType != GL_NONE && shaderInputType != vertexType)
843 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500844 context->handleError(InvalidOperation() << "Vertex shader input type does not "
845 "match the type of the bound vertex "
846 "attribute.");
Geoff Lang9ab5b822017-05-30 16:19:23 -0400847 return false;
848 }
849 }
850
851 return true;
852}
853
Geoff Langf41a7152016-09-19 15:11:17 -0400854} // anonymous namespace
855
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500856bool ValidTextureTarget(const ValidationContext *context, GLenum target)
Jamie Madill35d15012013-10-07 10:46:37 -0400857{
Jamie Madilld7460c72014-01-21 16:38:14 -0500858 switch (target)
Jamie Madill35d15012013-10-07 10:46:37 -0400859 {
He Yunchaoced53ae2016-11-29 15:00:51 +0800860 case GL_TEXTURE_2D:
861 case GL_TEXTURE_CUBE_MAP:
862 return true;
Jamie Madill35d15012013-10-07 10:46:37 -0400863
He Yunchaoced53ae2016-11-29 15:00:51 +0800864 case GL_TEXTURE_3D:
865 case GL_TEXTURE_2D_ARRAY:
866 return (context->getClientMajorVersion() >= 3);
Jamie Madilld7460c72014-01-21 16:38:14 -0500867
He Yunchaoced53ae2016-11-29 15:00:51 +0800868 case GL_TEXTURE_2D_MULTISAMPLE:
He Yunchaoced53ae2016-11-29 15:00:51 +0800869 return (context->getClientVersion() >= Version(3, 1));
Geoff Lang3b573612016-10-31 14:08:10 -0400870
He Yunchaoced53ae2016-11-29 15:00:51 +0800871 default:
872 return false;
Jamie Madilld7460c72014-01-21 16:38:14 -0500873 }
Jamie Madill35d15012013-10-07 10:46:37 -0400874}
875
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500876bool ValidTexture2DTarget(const ValidationContext *context, GLenum target)
877{
878 switch (target)
879 {
880 case GL_TEXTURE_2D:
881 case GL_TEXTURE_CUBE_MAP:
882 return true;
883
884 default:
885 return false;
886 }
887}
888
889bool ValidTexture3DTarget(const ValidationContext *context, GLenum target)
890{
891 switch (target)
892 {
893 case GL_TEXTURE_3D:
894 case GL_TEXTURE_2D_ARRAY:
Martin Radev1be913c2016-07-11 17:59:16 +0300895 return (context->getClientMajorVersion() >= 3);
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500896
897 default:
898 return false;
899 }
900}
901
Ian Ewellbda75592016-04-18 17:25:54 -0400902// Most texture GL calls are not compatible with external textures, so we have a separate validation
903// function for use in the GL calls that do
904bool ValidTextureExternalTarget(const ValidationContext *context, GLenum target)
905{
906 return (target == GL_TEXTURE_EXTERNAL_OES) &&
907 (context->getExtensions().eglImageExternal ||
908 context->getExtensions().eglStreamConsumerExternal);
909}
910
Shannon Woods4dfed832014-03-17 20:03:39 -0400911// This function differs from ValidTextureTarget in that the target must be
912// usable as the destination of a 2D operation-- so a cube face is valid, but
913// GL_TEXTURE_CUBE_MAP is not.
Jamie Madill560a8d82014-05-21 13:06:20 -0400914// Note: duplicate of IsInternalTextureTarget
Jamie Madillc29968b2016-01-20 11:17:23 -0500915bool ValidTexture2DDestinationTarget(const ValidationContext *context, GLenum target)
Shannon Woods4dfed832014-03-17 20:03:39 -0400916{
917 switch (target)
918 {
He Yunchaoced53ae2016-11-29 15:00:51 +0800919 case GL_TEXTURE_2D:
920 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
921 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
922 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
923 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
924 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
925 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
926 return true;
927 default:
928 return false;
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500929 }
930}
931
Jamie Madillbe849e42017-05-02 15:49:00 -0400932bool ValidateDrawElementsInstancedBase(ValidationContext *context,
933 GLenum mode,
934 GLsizei count,
935 GLenum type,
936 const GLvoid *indices,
937 GLsizei primcount)
938{
939 if (primcount < 0)
940 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500941 context->handleError(InvalidValue() << "primcount cannot be negative.");
Jamie Madillbe849e42017-05-02 15:49:00 -0400942 return false;
943 }
944
945 if (!ValidateDrawElementsCommon(context, mode, count, type, indices, primcount))
946 {
947 return false;
948 }
949
950 // No-op zero primitive count
951 return (primcount > 0);
952}
953
954bool ValidateDrawArraysInstancedBase(Context *context,
955 GLenum mode,
956 GLint first,
957 GLsizei count,
958 GLsizei primcount)
959{
960 if (primcount < 0)
961 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500962 context->handleError(InvalidValue() << "primcount cannot be negative.");
Jamie Madillbe849e42017-05-02 15:49:00 -0400963 return false;
964 }
965
966 if (!ValidateDrawArraysCommon(context, mode, first, count, primcount))
967 {
968 return false;
969 }
970
971 // No-op if zero primitive count
972 return (primcount > 0);
973}
974
Corentin Wallez0dc97812017-06-22 14:38:44 -0400975bool ValidateDrawInstancedANGLE(ValidationContext *context)
Jamie Madillbe849e42017-05-02 15:49:00 -0400976{
977 // Verify there is at least one active attribute with a divisor of zero
978 const State &state = context->getGLState();
979
980 Program *program = state.getProgram();
981
982 const auto &attribs = state.getVertexArray()->getVertexAttributes();
983 const auto &bindings = state.getVertexArray()->getVertexBindings();
984 for (size_t attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
985 {
986 const VertexAttribute &attrib = attribs[attributeIndex];
987 const VertexBinding &binding = bindings[attrib.bindingIndex];
Martin Radevdd5f27e2017-06-07 10:17:09 +0300988 if (program->isAttribLocationActive(attributeIndex) && binding.getDivisor() == 0)
Jamie Madillbe849e42017-05-02 15:49:00 -0400989 {
990 return true;
991 }
992 }
993
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500994 context->handleError(InvalidOperation()
995 << "At least one attribute must have a divisor of zero.");
Jamie Madillbe849e42017-05-02 15:49:00 -0400996 return false;
997}
998
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500999bool ValidTexture3DDestinationTarget(const ValidationContext *context, GLenum target)
1000{
1001 switch (target)
1002 {
He Yunchaoced53ae2016-11-29 15:00:51 +08001003 case GL_TEXTURE_3D:
1004 case GL_TEXTURE_2D_ARRAY:
1005 return true;
1006 default:
1007 return false;
Shannon Woods4dfed832014-03-17 20:03:39 -04001008 }
1009}
1010
He Yunchao11b038b2016-11-22 21:24:04 +08001011bool ValidTexLevelDestinationTarget(const ValidationContext *context, GLenum target)
1012{
1013 switch (target)
1014 {
1015 case GL_TEXTURE_2D:
1016 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1017 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1018 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1019 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1020 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1021 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1022 case GL_TEXTURE_3D:
1023 case GL_TEXTURE_2D_ARRAY:
1024 case GL_TEXTURE_2D_MULTISAMPLE:
1025 return true;
1026 default:
1027 return false;
1028 }
1029}
1030
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001031bool ValidFramebufferTarget(GLenum target)
1032{
He Yunchaoced53ae2016-11-29 15:00:51 +08001033 static_assert(GL_DRAW_FRAMEBUFFER_ANGLE == GL_DRAW_FRAMEBUFFER &&
1034 GL_READ_FRAMEBUFFER_ANGLE == GL_READ_FRAMEBUFFER,
Geoff Langd4475812015-03-18 10:53:05 -04001035 "ANGLE framebuffer enums must equal the ES3 framebuffer enums.");
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001036
1037 switch (target)
1038 {
He Yunchaoced53ae2016-11-29 15:00:51 +08001039 case GL_FRAMEBUFFER:
1040 return true;
1041 case GL_READ_FRAMEBUFFER:
1042 return true;
1043 case GL_DRAW_FRAMEBUFFER:
1044 return true;
1045 default:
1046 return false;
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001047 }
1048}
1049
Jamie Madill29639852016-09-02 15:00:09 -04001050bool ValidBufferTarget(const ValidationContext *context, GLenum target)
Jamie Madill8c96d582014-03-05 15:01:23 -05001051{
1052 switch (target)
1053 {
He Yunchaoced53ae2016-11-29 15:00:51 +08001054 case GL_ARRAY_BUFFER:
1055 case GL_ELEMENT_ARRAY_BUFFER:
1056 return true;
Jamie Madill8c96d582014-03-05 15:01:23 -05001057
He Yunchaoced53ae2016-11-29 15:00:51 +08001058 case GL_PIXEL_PACK_BUFFER:
1059 case GL_PIXEL_UNPACK_BUFFER:
1060 return (context->getExtensions().pixelBufferObject ||
1061 context->getClientMajorVersion() >= 3);
Shannon Woods158c4382014-05-06 13:00:07 -04001062
He Yunchaoced53ae2016-11-29 15:00:51 +08001063 case GL_COPY_READ_BUFFER:
1064 case GL_COPY_WRITE_BUFFER:
1065 case GL_TRANSFORM_FEEDBACK_BUFFER:
1066 case GL_UNIFORM_BUFFER:
1067 return (context->getClientMajorVersion() >= 3);
Jamie Madill8c96d582014-03-05 15:01:23 -05001068
He Yunchaoced53ae2016-11-29 15:00:51 +08001069 case GL_ATOMIC_COUNTER_BUFFER:
1070 case GL_SHADER_STORAGE_BUFFER:
1071 case GL_DRAW_INDIRECT_BUFFER:
1072 case GL_DISPATCH_INDIRECT_BUFFER:
He Yunchaoced53ae2016-11-29 15:00:51 +08001073 return context->getClientVersion() >= Version(3, 1);
Geoff Lang3b573612016-10-31 14:08:10 -04001074
He Yunchaoced53ae2016-11-29 15:00:51 +08001075 default:
1076 return false;
Jamie Madill8c96d582014-03-05 15:01:23 -05001077 }
1078}
1079
Jamie Madillc29968b2016-01-20 11:17:23 -05001080bool ValidMipLevel(const ValidationContext *context, GLenum target, GLint level)
Geoff Langce635692013-09-24 13:56:32 -04001081{
Jamie Madillc29968b2016-01-20 11:17:23 -05001082 const auto &caps = context->getCaps();
Geoff Langaae65a42014-05-26 12:43:44 -04001083 size_t maxDimension = 0;
Geoff Langce635692013-09-24 13:56:32 -04001084 switch (target)
1085 {
Jamie Madillc29968b2016-01-20 11:17:23 -05001086 case GL_TEXTURE_2D:
1087 maxDimension = caps.max2DTextureSize;
1088 break;
He Yunchaoced53ae2016-11-29 15:00:51 +08001089 case GL_TEXTURE_CUBE_MAP:
1090 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1091 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1092 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1093 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1094 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1095 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1096 maxDimension = caps.maxCubeMapTextureSize;
1097 break;
1098 case GL_TEXTURE_3D:
1099 maxDimension = caps.max3DTextureSize;
1100 break;
1101 case GL_TEXTURE_2D_ARRAY:
1102 maxDimension = caps.max2DTextureSize;
1103 break;
He Yunchao11b038b2016-11-22 21:24:04 +08001104 case GL_TEXTURE_2D_MULTISAMPLE:
1105 maxDimension = caps.max2DTextureSize;
1106 break;
He Yunchaoced53ae2016-11-29 15:00:51 +08001107 default:
1108 UNREACHABLE();
Geoff Langce635692013-09-24 13:56:32 -04001109 }
1110
Brandon Jones6cad5662017-06-14 13:25:13 -07001111 return level <= gl::log2(static_cast<int>(maxDimension)) && level >= 0;
Geoff Langce635692013-09-24 13:56:32 -04001112}
1113
Brandon Jones6cad5662017-06-14 13:25:13 -07001114bool ValidImageSizeParameters(ValidationContext *context,
Austin Kinross08528e12015-10-07 16:24:40 -07001115 GLenum target,
1116 GLint level,
1117 GLsizei width,
1118 GLsizei height,
1119 GLsizei depth,
1120 bool isSubImage)
Geoff Langce635692013-09-24 13:56:32 -04001121{
Brandon Jones6cad5662017-06-14 13:25:13 -07001122 if (width < 0 || height < 0 || depth < 0)
Geoff Langce635692013-09-24 13:56:32 -04001123 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001124 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeSize);
Geoff Langce635692013-09-24 13:56:32 -04001125 return false;
1126 }
Austin Kinross08528e12015-10-07 16:24:40 -07001127 // TexSubImage parameters can be NPOT without textureNPOT extension,
1128 // as long as the destination texture is POT.
Geoff Langcc507aa2016-12-12 10:09:52 -05001129 bool hasNPOTSupport =
Geoff Lang5f319a42017-01-09 16:49:19 -05001130 context->getExtensions().textureNPOT || context->getClientVersion() >= Version(3, 0);
Geoff Langcc507aa2016-12-12 10:09:52 -05001131 if (!isSubImage && !hasNPOTSupport &&
Jamie Madill4fd75c12014-06-23 10:53:54 -04001132 (level != 0 && (!gl::isPow2(width) || !gl::isPow2(height) || !gl::isPow2(depth))))
Geoff Langce635692013-09-24 13:56:32 -04001133 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001134 ANGLE_VALIDATION_ERR(context, InvalidValue(), TextureNotPow2);
Geoff Langce635692013-09-24 13:56:32 -04001135 return false;
1136 }
1137
1138 if (!ValidMipLevel(context, target, level))
1139 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001140 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidMipLevel);
Geoff Langce635692013-09-24 13:56:32 -04001141 return false;
1142 }
1143
1144 return true;
1145}
1146
Geoff Lang0d8b7242015-09-09 14:56:53 -04001147bool CompressedTextureFormatRequiresExactSize(GLenum internalFormat)
1148{
1149 // List of compressed format that require that the texture size is smaller than or a multiple of
1150 // the compressed block size.
1151 switch (internalFormat)
1152 {
1153 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
1154 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
1155 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
1156 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
Geoff Langfff7a7d2017-06-02 10:39:17 -04001157 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
1158 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
1159 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
1160 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
Minmin Gonge3939b92015-12-01 15:36:51 -08001161 case GL_ETC1_RGB8_LOSSY_DECODE_ANGLE:
Minmin Gong390208b2017-02-28 18:03:06 -08001162 case GL_COMPRESSED_RGB8_LOSSY_DECODE_ETC2_ANGLE:
1163 case GL_COMPRESSED_SRGB8_LOSSY_DECODE_ETC2_ANGLE:
1164 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_LOSSY_DECODE_ETC2_ANGLE:
1165 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_LOSSY_DECODE_ETC2_ANGLE:
1166 case GL_COMPRESSED_RGBA8_LOSSY_DECODE_ETC2_EAC_ANGLE:
1167 case GL_COMPRESSED_SRGB8_ALPHA8_LOSSY_DECODE_ETC2_EAC_ANGLE:
Geoff Lang0d8b7242015-09-09 14:56:53 -04001168 return true;
1169
1170 default:
1171 return false;
1172 }
1173}
1174
Geoff Lang966c9402017-04-18 12:38:27 -04001175bool ValidCompressedDimension(GLsizei size, GLuint blockSize, bool smallerThanBlockSizeAllowed)
1176{
1177 return (smallerThanBlockSizeAllowed && (size > 0) && (blockSize % size == 0)) ||
1178 (size % blockSize == 0);
1179}
1180
Jamie Madillc29968b2016-01-20 11:17:23 -05001181bool ValidCompressedImageSize(const ValidationContext *context,
1182 GLenum internalFormat,
Geoff Lang966c9402017-04-18 12:38:27 -04001183 GLint level,
Jamie Madillc29968b2016-01-20 11:17:23 -05001184 GLsizei width,
1185 GLsizei height)
Geoff Langd4f180b2013-09-24 13:57:44 -04001186{
Geoff Langca271392017-04-05 12:30:00 -04001187 const gl::InternalFormat &formatInfo = gl::GetSizedInternalFormatInfo(internalFormat);
Geoff Lang5d601382014-07-22 15:14:06 -04001188 if (!formatInfo.compressed)
Geoff Langd4f180b2013-09-24 13:57:44 -04001189 {
1190 return false;
1191 }
1192
Geoff Lang966c9402017-04-18 12:38:27 -04001193 if (width < 0 || height < 0)
1194 {
1195 return false;
1196 }
1197
1198 if (CompressedTextureFormatRequiresExactSize(internalFormat))
1199 {
1200 // The ANGLE extensions allow specifying compressed textures with sizes smaller than the
1201 // block size for level 0 but WebGL disallows this.
1202 bool smallerThanBlockSizeAllowed =
1203 level > 0 || !context->getExtensions().webglCompatibility;
1204
1205 if (!ValidCompressedDimension(width, formatInfo.compressedBlockWidth,
1206 smallerThanBlockSizeAllowed) ||
1207 !ValidCompressedDimension(height, formatInfo.compressedBlockHeight,
1208 smallerThanBlockSizeAllowed))
1209 {
1210 return false;
1211 }
1212 }
1213
1214 return true;
1215}
1216
1217bool ValidCompressedSubImageSize(const ValidationContext *context,
1218 GLenum internalFormat,
1219 GLint xoffset,
1220 GLint yoffset,
1221 GLsizei width,
1222 GLsizei height,
1223 size_t textureWidth,
1224 size_t textureHeight)
1225{
1226 const gl::InternalFormat &formatInfo = gl::GetSizedInternalFormatInfo(internalFormat);
1227 if (!formatInfo.compressed)
1228 {
1229 return false;
1230 }
1231
Geoff Lang44ff5a72017-02-03 15:15:43 -05001232 if (xoffset < 0 || yoffset < 0 || width < 0 || height < 0)
Geoff Langd4f180b2013-09-24 13:57:44 -04001233 {
1234 return false;
1235 }
1236
Geoff Lang0d8b7242015-09-09 14:56:53 -04001237 if (CompressedTextureFormatRequiresExactSize(internalFormat))
1238 {
Geoff Lang44ff5a72017-02-03 15:15:43 -05001239 if (xoffset % formatInfo.compressedBlockWidth != 0 ||
Geoff Lang966c9402017-04-18 12:38:27 -04001240 yoffset % formatInfo.compressedBlockHeight != 0)
1241 {
1242 return false;
1243 }
1244
1245 // Allowed to either have data that is a multiple of block size or is smaller than the block
1246 // size but fills the entire mip
1247 bool fillsEntireMip = xoffset == 0 && yoffset == 0 &&
1248 static_cast<size_t>(width) == textureWidth &&
1249 static_cast<size_t>(height) == textureHeight;
1250 bool sizeMultipleOfBlockSize = (width % formatInfo.compressedBlockWidth) == 0 &&
1251 (height % formatInfo.compressedBlockHeight) == 0;
1252 if (!sizeMultipleOfBlockSize && !fillsEntireMip)
Geoff Lang0d8b7242015-09-09 14:56:53 -04001253 {
1254 return false;
1255 }
1256 }
1257
Geoff Langd4f180b2013-09-24 13:57:44 -04001258 return true;
1259}
1260
Geoff Langff5b2d52016-09-07 11:32:23 -04001261bool ValidImageDataSize(ValidationContext *context,
1262 GLenum textureTarget,
1263 GLsizei width,
1264 GLsizei height,
1265 GLsizei depth,
Geoff Langdbcced82017-06-06 15:55:54 -04001266 GLenum format,
Geoff Langff5b2d52016-09-07 11:32:23 -04001267 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -04001268 const void *pixels,
Geoff Langff5b2d52016-09-07 11:32:23 -04001269 GLsizei imageSize)
1270{
1271 gl::Buffer *pixelUnpackBuffer = context->getGLState().getTargetBuffer(GL_PIXEL_UNPACK_BUFFER);
1272 if (pixelUnpackBuffer == nullptr && imageSize < 0)
1273 {
1274 // Checks are not required
1275 return true;
1276 }
1277
1278 // ...the data would be unpacked from the buffer object such that the memory reads required
1279 // would exceed the data store size.
Geoff Langdbcced82017-06-06 15:55:54 -04001280 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(format, type);
1281 ASSERT(formatInfo.internalFormat != GL_NONE);
Geoff Langff5b2d52016-09-07 11:32:23 -04001282 const gl::Extents size(width, height, depth);
1283 const auto &unpack = context->getGLState().getUnpackState();
1284
1285 bool targetIs3D = textureTarget == GL_TEXTURE_3D || textureTarget == GL_TEXTURE_2D_ARRAY;
1286 auto endByteOrErr = formatInfo.computePackUnpackEndByte(type, size, unpack, targetIs3D);
1287 if (endByteOrErr.isError())
1288 {
1289 context->handleError(endByteOrErr.getError());
1290 return false;
1291 }
1292
1293 GLuint endByte = endByteOrErr.getResult();
1294
1295 if (pixelUnpackBuffer)
1296 {
1297 CheckedNumeric<size_t> checkedEndByte(endByteOrErr.getResult());
1298 CheckedNumeric<size_t> checkedOffset(reinterpret_cast<size_t>(pixels));
1299 checkedEndByte += checkedOffset;
1300
1301 if (!checkedEndByte.IsValid() ||
1302 (checkedEndByte.ValueOrDie() > static_cast<size_t>(pixelUnpackBuffer->getSize())))
1303 {
1304 // Overflow past the end of the buffer
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001305 context->handleError(InvalidOperation());
Geoff Langff5b2d52016-09-07 11:32:23 -04001306 return false;
1307 }
1308 }
1309 else
1310 {
1311 ASSERT(imageSize >= 0);
1312 if (pixels == nullptr && imageSize != 0)
1313 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001314 context->handleError(InvalidOperation()
1315 << "imageSize must be 0 if no texture data is provided.");
Geoff Lang3feb3ff2016-10-26 10:57:45 -04001316 return false;
Geoff Langff5b2d52016-09-07 11:32:23 -04001317 }
1318
Geoff Lang3feb3ff2016-10-26 10:57:45 -04001319 if (pixels != nullptr && endByte > static_cast<GLuint>(imageSize))
Geoff Langff5b2d52016-09-07 11:32:23 -04001320 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001321 context->handleError(InvalidOperation() << "imageSize must be at least " << endByte);
Geoff Langff5b2d52016-09-07 11:32:23 -04001322 return false;
1323 }
1324 }
1325
1326 return true;
1327}
1328
Geoff Lang37dde692014-01-31 16:34:54 -05001329bool ValidQueryType(const Context *context, GLenum queryType)
1330{
He Yunchaoced53ae2016-11-29 15:00:51 +08001331 static_assert(GL_ANY_SAMPLES_PASSED == GL_ANY_SAMPLES_PASSED_EXT,
1332 "GL extension enums not equal.");
1333 static_assert(GL_ANY_SAMPLES_PASSED_CONSERVATIVE == GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
1334 "GL extension enums not equal.");
Geoff Lang37dde692014-01-31 16:34:54 -05001335
1336 switch (queryType)
1337 {
He Yunchaoced53ae2016-11-29 15:00:51 +08001338 case GL_ANY_SAMPLES_PASSED:
1339 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
1340 return true;
1341 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
1342 return (context->getClientMajorVersion() >= 3);
1343 case GL_TIME_ELAPSED_EXT:
1344 return context->getExtensions().disjointTimerQuery;
1345 case GL_COMMANDS_COMPLETED_CHROMIUM:
1346 return context->getExtensions().syncQuery;
1347 default:
1348 return false;
Geoff Lang37dde692014-01-31 16:34:54 -05001349 }
1350}
1351
Geoff Lang2d62ab72017-03-23 16:54:40 -04001352bool ValidateWebGLVertexAttribPointer(ValidationContext *context,
1353 GLenum type,
1354 GLboolean normalized,
1355 GLsizei stride,
Jamie Madill876429b2017-04-20 15:46:24 -04001356 const void *ptr,
Geoff Lang2d62ab72017-03-23 16:54:40 -04001357 bool pureInteger)
1358{
1359 ASSERT(context->getExtensions().webglCompatibility);
Geoff Lang2d62ab72017-03-23 16:54:40 -04001360 // WebGL 1.0 [Section 6.11] Vertex Attribute Data Stride
1361 // The WebGL API supports vertex attribute data strides up to 255 bytes. A call to
1362 // vertexAttribPointer will generate an INVALID_VALUE error if the value for the stride
1363 // parameter exceeds 255.
1364 constexpr GLsizei kMaxWebGLStride = 255;
1365 if (stride > kMaxWebGLStride)
1366 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001367 context->handleError(InvalidValue()
1368 << "Stride is over the maximum stride allowed by WebGL.");
Geoff Lang2d62ab72017-03-23 16:54:40 -04001369 return false;
1370 }
1371
1372 // WebGL 1.0 [Section 6.4] Buffer Offset and Stride Requirements
1373 // The offset arguments to drawElements and vertexAttribPointer, and the stride argument to
1374 // vertexAttribPointer, must be a multiple of the size of the data type passed to the call,
1375 // or an INVALID_OPERATION error is generated.
1376 VertexFormatType internalType = GetVertexFormatType(type, normalized, 1, pureInteger);
1377 size_t typeSize = GetVertexFormatTypeSize(internalType);
1378
1379 ASSERT(isPow2(typeSize) && typeSize > 0);
1380 size_t sizeMask = (typeSize - 1);
1381 if ((reinterpret_cast<intptr_t>(ptr) & sizeMask) != 0)
1382 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001383 ANGLE_VALIDATION_ERR(context, InvalidOperation(), OffsetMustBeMultipleOfType);
Geoff Lang2d62ab72017-03-23 16:54:40 -04001384 return false;
1385 }
1386
1387 if ((stride & sizeMask) != 0)
1388 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001389 ANGLE_VALIDATION_ERR(context, InvalidOperation(), StrideMustBeMultipleOfType);
Geoff Lang2d62ab72017-03-23 16:54:40 -04001390 return false;
1391 }
1392
1393 return true;
1394}
1395
Jamie Madillef300b12016-10-07 15:12:09 -04001396Program *GetValidProgram(ValidationContext *context, GLuint id)
Geoff Lang48dcae72014-02-05 16:28:24 -05001397{
He Yunchaoced53ae2016-11-29 15:00:51 +08001398 // ES3 spec (section 2.11.1) -- "Commands that accept shader or program object names will
1399 // generate the error INVALID_VALUE if the provided name is not the name of either a shader
1400 // or program object and INVALID_OPERATION if the provided name identifies an object
1401 // that is not the expected type."
Geoff Lang48dcae72014-02-05 16:28:24 -05001402
Dian Xiang769769a2015-09-09 15:20:08 -07001403 Program *validProgram = context->getProgram(id);
1404
1405 if (!validProgram)
Geoff Lang48dcae72014-02-05 16:28:24 -05001406 {
Dian Xiang769769a2015-09-09 15:20:08 -07001407 if (context->getShader(id))
1408 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001409 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExpectedProgramName);
Dian Xiang769769a2015-09-09 15:20:08 -07001410 }
1411 else
1412 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001413 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidProgramName);
Dian Xiang769769a2015-09-09 15:20:08 -07001414 }
Geoff Lang48dcae72014-02-05 16:28:24 -05001415 }
Dian Xiang769769a2015-09-09 15:20:08 -07001416
1417 return validProgram;
1418}
1419
Jamie Madillef300b12016-10-07 15:12:09 -04001420Shader *GetValidShader(ValidationContext *context, GLuint id)
Dian Xiang769769a2015-09-09 15:20:08 -07001421{
1422 // See ValidProgram for spec details.
1423
1424 Shader *validShader = context->getShader(id);
1425
1426 if (!validShader)
Geoff Lang48dcae72014-02-05 16:28:24 -05001427 {
Dian Xiang769769a2015-09-09 15:20:08 -07001428 if (context->getProgram(id))
1429 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001430 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExpectedShaderName);
Dian Xiang769769a2015-09-09 15:20:08 -07001431 }
1432 else
1433 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001434 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidShaderName);
Dian Xiang769769a2015-09-09 15:20:08 -07001435 }
Geoff Lang48dcae72014-02-05 16:28:24 -05001436 }
Dian Xiang769769a2015-09-09 15:20:08 -07001437
1438 return validShader;
Geoff Lang48dcae72014-02-05 16:28:24 -05001439}
1440
Geoff Langb1196682014-07-23 13:47:29 -04001441bool ValidateAttachmentTarget(gl::Context *context, GLenum attachment)
Jamie Madillb4472272014-07-03 10:38:55 -04001442{
1443 if (attachment >= GL_COLOR_ATTACHMENT0_EXT && attachment <= GL_COLOR_ATTACHMENT15_EXT)
1444 {
1445 const unsigned int colorAttachment = (attachment - GL_COLOR_ATTACHMENT0_EXT);
1446
Geoff Langaae65a42014-05-26 12:43:44 -04001447 if (colorAttachment >= context->getCaps().maxColorAttachments)
Jamie Madillb4472272014-07-03 10:38:55 -04001448 {
Martin Radevd178aa42017-07-13 14:03:22 +03001449 context->handleError(
1450 InvalidOperation()
1451 << "attachment index cannot be greater or equal to MAX_COLOR_ATTACHMENTS.");
Geoff Langb1196682014-07-23 13:47:29 -04001452 return false;
Jamie Madillb4472272014-07-03 10:38:55 -04001453 }
1454 }
1455 else
1456 {
1457 switch (attachment)
1458 {
He Yunchaoced53ae2016-11-29 15:00:51 +08001459 case GL_DEPTH_ATTACHMENT:
1460 case GL_STENCIL_ATTACHMENT:
1461 break;
Jamie Madillb4472272014-07-03 10:38:55 -04001462
He Yunchaoced53ae2016-11-29 15:00:51 +08001463 case GL_DEPTH_STENCIL_ATTACHMENT:
1464 if (!context->getExtensions().webglCompatibility &&
1465 context->getClientMajorVersion() < 3)
1466 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001467 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
He Yunchaoced53ae2016-11-29 15:00:51 +08001468 return false;
1469 }
1470 break;
Jamie Madillb4472272014-07-03 10:38:55 -04001471
He Yunchaoced53ae2016-11-29 15:00:51 +08001472 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07001473 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
He Yunchaoced53ae2016-11-29 15:00:51 +08001474 return false;
Jamie Madillb4472272014-07-03 10:38:55 -04001475 }
1476 }
1477
1478 return true;
1479}
1480
Jamie Madille8fb6402017-02-14 17:56:40 -05001481bool ValidateRenderbufferStorageParametersBase(ValidationContext *context,
He Yunchaoced53ae2016-11-29 15:00:51 +08001482 GLenum target,
1483 GLsizei samples,
1484 GLenum internalformat,
1485 GLsizei width,
1486 GLsizei height)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001487{
1488 switch (target)
1489 {
He Yunchaoced53ae2016-11-29 15:00:51 +08001490 case GL_RENDERBUFFER:
1491 break;
1492 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07001493 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidRenderbufferTarget);
He Yunchaoced53ae2016-11-29 15:00:51 +08001494 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001495 }
1496
1497 if (width < 0 || height < 0 || samples < 0)
1498 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001499 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidRenderbufferWidthHeight);
Geoff Langb1196682014-07-23 13:47:29 -04001500 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001501 }
1502
Jamie Madill4e0e6f82017-02-17 11:06:03 -05001503 // Hack for the special WebGL 1 "DEPTH_STENCIL" internal format.
1504 GLenum convertedInternalFormat = context->getConvertedRenderbufferFormat(internalformat);
1505
1506 const TextureCaps &formatCaps = context->getTextureCaps().get(convertedInternalFormat);
Geoff Langd87878e2014-09-19 15:42:59 -04001507 if (!formatCaps.renderable)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001508 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001509 context->handleError(InvalidEnum());
Geoff Langb1196682014-07-23 13:47:29 -04001510 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001511 }
1512
1513 // ANGLE_framebuffer_multisample does not explicitly state that the internal format must be
1514 // 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 -08001515 // only sized internal formats.
Geoff Langca271392017-04-05 12:30:00 -04001516 const gl::InternalFormat &formatInfo = gl::GetSizedInternalFormatInfo(convertedInternalFormat);
1517 if (formatInfo.internalFormat == GL_NONE)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001518 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001519 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidRenderbufferInternalFormat);
Geoff Langb1196682014-07-23 13:47:29 -04001520 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001521 }
1522
Geoff Langaae65a42014-05-26 12:43:44 -04001523 if (static_cast<GLuint>(std::max(width, height)) > context->getCaps().maxRenderbufferSize)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001524 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001525 context->handleError(InvalidValue());
Geoff Langb1196682014-07-23 13:47:29 -04001526 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001527 }
1528
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001529 GLuint handle = context->getGLState().getRenderbufferId();
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001530 if (handle == 0)
1531 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001532 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidRenderbufferTarget);
Geoff Langb1196682014-07-23 13:47:29 -04001533 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001534 }
1535
1536 return true;
1537}
1538
He Yunchaoced53ae2016-11-29 15:00:51 +08001539bool ValidateFramebufferRenderbufferParameters(gl::Context *context,
1540 GLenum target,
1541 GLenum attachment,
1542 GLenum renderbuffertarget,
1543 GLuint renderbuffer)
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001544{
Shannon Woods1da3cf62014-06-27 15:32:23 -04001545 if (!ValidFramebufferTarget(target))
1546 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001547 context->handleError(InvalidEnum());
Geoff Langb1196682014-07-23 13:47:29 -04001548 return false;
Shannon Woods1da3cf62014-06-27 15:32:23 -04001549 }
1550
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001551 gl::Framebuffer *framebuffer = context->getGLState().getTargetFramebuffer(target);
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001552
Jamie Madill84115c92015-04-23 15:00:07 -04001553 ASSERT(framebuffer);
1554 if (framebuffer->id() == 0)
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001555 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001556 ANGLE_VALIDATION_ERR(context, InvalidOperation(), DefaultFramebufferTarget);
Geoff Langb1196682014-07-23 13:47:29 -04001557 return false;
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001558 }
1559
Jamie Madillb4472272014-07-03 10:38:55 -04001560 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001561 {
Jamie Madillb4472272014-07-03 10:38:55 -04001562 return false;
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001563 }
1564
Jamie Madillab9d82c2014-01-21 16:38:14 -05001565 // [OpenGL ES 2.0.25] Section 4.4.3 page 112
1566 // [OpenGL ES 3.0.2] Section 4.4.2 page 201
1567 // 'renderbuffer' must be either zero or the name of an existing renderbuffer object of
1568 // type 'renderbuffertarget', otherwise an INVALID_OPERATION error is generated.
1569 if (renderbuffer != 0)
1570 {
1571 if (!context->getRenderbuffer(renderbuffer))
1572 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001573 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidRenderbufferTarget);
Geoff Langb1196682014-07-23 13:47:29 -04001574 return false;
Jamie Madillab9d82c2014-01-21 16:38:14 -05001575 }
1576 }
1577
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001578 return true;
1579}
1580
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001581bool ValidateBlitFramebufferParameters(ValidationContext *context,
Jamie Madillc29968b2016-01-20 11:17:23 -05001582 GLint srcX0,
1583 GLint srcY0,
1584 GLint srcX1,
1585 GLint srcY1,
1586 GLint dstX0,
1587 GLint dstY0,
1588 GLint dstX1,
1589 GLint dstY1,
1590 GLbitfield mask,
1591 GLenum filter)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001592{
1593 switch (filter)
1594 {
He Yunchaoced53ae2016-11-29 15:00:51 +08001595 case GL_NEAREST:
1596 break;
1597 case GL_LINEAR:
1598 break;
1599 default:
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001600 context->handleError(InvalidEnum());
He Yunchaoced53ae2016-11-29 15:00:51 +08001601 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001602 }
1603
1604 if ((mask & ~(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)) != 0)
1605 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001606 context->handleError(InvalidValue());
Geoff Langb1196682014-07-23 13:47:29 -04001607 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001608 }
1609
1610 if (mask == 0)
1611 {
1612 // ES3.0 spec, section 4.3.2 specifies that a mask of zero is valid and no
1613 // buffers are copied.
1614 return false;
1615 }
1616
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001617 // ES3.0 spec, section 4.3.2 states that linear filtering is only available for the
1618 // color buffer, leaving only nearest being unfiltered from above
1619 if ((mask & ~GL_COLOR_BUFFER_BIT) != 0 && filter != GL_NEAREST)
1620 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001621 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001622 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001623 }
1624
Jamie Madill51f40ec2016-06-15 14:06:00 -04001625 const auto &glState = context->getGLState();
1626 gl::Framebuffer *readFramebuffer = glState.getReadFramebuffer();
1627 gl::Framebuffer *drawFramebuffer = glState.getDrawFramebuffer();
Jamie Madill48faf802014-11-06 15:27:22 -05001628
1629 if (!readFramebuffer || !drawFramebuffer)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001630 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001631 context->handleError(InvalidFramebufferOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001632 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001633 }
1634
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001635 if (readFramebuffer->id() == drawFramebuffer->id())
1636 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001637 context->handleError(InvalidOperation());
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001638 return false;
1639 }
1640
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001641 if (readFramebuffer->checkStatus(context) != GL_FRAMEBUFFER_COMPLETE)
Jamie Madill48faf802014-11-06 15:27:22 -05001642 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001643 context->handleError(InvalidFramebufferOperation());
Jamie Madill48faf802014-11-06 15:27:22 -05001644 return false;
1645 }
1646
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001647 if (drawFramebuffer->checkStatus(context) != GL_FRAMEBUFFER_COMPLETE)
Jamie Madill48faf802014-11-06 15:27:22 -05001648 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001649 context->handleError(InvalidFramebufferOperation());
Jamie Madill48faf802014-11-06 15:27:22 -05001650 return false;
1651 }
1652
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001653 if (drawFramebuffer->getSamples(context) != 0)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001654 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001655 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001656 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001657 }
1658
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001659 bool sameBounds = srcX0 == dstX0 && srcY0 == dstY0 && srcX1 == dstX1 && srcY1 == dstY1;
1660
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001661 if (mask & GL_COLOR_BUFFER_BIT)
1662 {
Jamie Madillb6bda4a2015-04-20 12:53:26 -04001663 const gl::FramebufferAttachment *readColorBuffer = readFramebuffer->getReadColorbuffer();
Jamie Madill6163c752015-12-07 16:32:59 -05001664 const Extensions &extensions = context->getExtensions();
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001665
He Yunchao66a41a22016-12-15 16:45:05 +08001666 if (readColorBuffer)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001667 {
Jamie Madilla3944d42016-07-22 22:13:26 -04001668 const Format &readFormat = readColorBuffer->getFormat();
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001669
Geoff Langa15472a2015-08-11 11:48:03 -04001670 for (size_t drawbufferIdx = 0;
1671 drawbufferIdx < drawFramebuffer->getDrawbufferStateCount(); ++drawbufferIdx)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001672 {
Geoff Langa15472a2015-08-11 11:48:03 -04001673 const FramebufferAttachment *attachment =
1674 drawFramebuffer->getDrawBuffer(drawbufferIdx);
1675 if (attachment)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001676 {
Jamie Madilla3944d42016-07-22 22:13:26 -04001677 const Format &drawFormat = attachment->getFormat();
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001678
Geoff Langb2f3d052013-08-13 12:49:27 -04001679 // The GL ES 3.0.2 spec (pg 193) states that:
1680 // 1) If the read buffer is fixed point format, the draw buffer must be as well
He Yunchaoced53ae2016-11-29 15:00:51 +08001681 // 2) If the read buffer is an unsigned integer format, the draw buffer must be
1682 // as well
1683 // 3) If the read buffer is a signed integer format, the draw buffer must be as
1684 // well
Jamie Madill6163c752015-12-07 16:32:59 -05001685 // Changes with EXT_color_buffer_float:
1686 // Case 1) is changed to fixed point OR floating point
Jamie Madilla3944d42016-07-22 22:13:26 -04001687 GLenum readComponentType = readFormat.info->componentType;
1688 GLenum drawComponentType = drawFormat.info->componentType;
He Yunchaoced53ae2016-11-29 15:00:51 +08001689 bool readFixedPoint = (readComponentType == GL_UNSIGNED_NORMALIZED ||
Jamie Madill6163c752015-12-07 16:32:59 -05001690 readComponentType == GL_SIGNED_NORMALIZED);
1691 bool drawFixedPoint = (drawComponentType == GL_UNSIGNED_NORMALIZED ||
1692 drawComponentType == GL_SIGNED_NORMALIZED);
1693
1694 if (extensions.colorBufferFloat)
1695 {
1696 bool readFixedOrFloat = (readFixedPoint || readComponentType == GL_FLOAT);
1697 bool drawFixedOrFloat = (drawFixedPoint || drawComponentType == GL_FLOAT);
1698
1699 if (readFixedOrFloat != drawFixedOrFloat)
1700 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001701 context->handleError(InvalidOperation()
1702 << "If the read buffer contains fixed-point or "
1703 "floating-point values, the draw buffer must "
1704 "as well.");
Jamie Madill6163c752015-12-07 16:32:59 -05001705 return false;
1706 }
1707 }
1708 else if (readFixedPoint != drawFixedPoint)
1709 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001710 context->handleError(InvalidOperation()
1711 << "If the read buffer contains fixed-point values, "
1712 "the draw buffer must as well.");
Jamie Madill6163c752015-12-07 16:32:59 -05001713 return false;
1714 }
1715
1716 if (readComponentType == GL_UNSIGNED_INT &&
1717 drawComponentType != GL_UNSIGNED_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001718 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001719 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001720 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001721 }
1722
Jamie Madill6163c752015-12-07 16:32:59 -05001723 if (readComponentType == GL_INT && drawComponentType != GL_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001724 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001725 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001726 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001727 }
1728
Jamie Madilla3944d42016-07-22 22:13:26 -04001729 if (readColorBuffer->getSamples() > 0 &&
1730 (!Format::SameSized(readFormat, drawFormat) || !sameBounds))
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001731 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001732 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001733 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001734 }
Geoff Lange4915782017-04-12 15:19:07 -04001735
1736 if (context->getExtensions().webglCompatibility &&
1737 *readColorBuffer == *attachment)
1738 {
1739 context->handleError(
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001740 InvalidOperation()
1741 << "Read and write color attachments cannot be the same image.");
Geoff Lange4915782017-04-12 15:19:07 -04001742 return false;
1743 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001744 }
1745 }
1746
Jamie Madilla3944d42016-07-22 22:13:26 -04001747 if ((readFormat.info->componentType == GL_INT ||
1748 readFormat.info->componentType == GL_UNSIGNED_INT) &&
1749 filter == GL_LINEAR)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001750 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001751 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001752 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001753 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001754 }
He Yunchao66a41a22016-12-15 16:45:05 +08001755 // WebGL 2.0 BlitFramebuffer when blitting from a missing attachment
1756 // In OpenGL ES it is undefined what happens when an operation tries to blit from a missing
1757 // attachment and WebGL defines it to be an error. We do the check unconditionally as the
1758 // situation is an application error that would lead to a crash in ANGLE.
1759 else if (drawFramebuffer->hasEnabledDrawBuffer())
1760 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001761 context->handleError(
1762 InvalidOperation()
1763 << "Attempt to read from a missing color attachment of a complete framebuffer.");
He Yunchao66a41a22016-12-15 16:45:05 +08001764 return false;
1765 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001766 }
1767
He Yunchaoced53ae2016-11-29 15:00:51 +08001768 GLenum masks[] = {GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT};
Dongseong Hwang44b422c2014-12-09 15:42:01 +02001769 GLenum attachments[] = {GL_DEPTH_ATTACHMENT, GL_STENCIL_ATTACHMENT};
1770 for (size_t i = 0; i < 2; i++)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001771 {
Dongseong Hwang44b422c2014-12-09 15:42:01 +02001772 if (mask & masks[i])
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001773 {
He Yunchaoced53ae2016-11-29 15:00:51 +08001774 const gl::FramebufferAttachment *readBuffer =
1775 readFramebuffer->getAttachment(attachments[i]);
1776 const gl::FramebufferAttachment *drawBuffer =
1777 drawFramebuffer->getAttachment(attachments[i]);
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001778
Dongseong Hwang44b422c2014-12-09 15:42:01 +02001779 if (readBuffer && drawBuffer)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001780 {
Jamie Madilla3944d42016-07-22 22:13:26 -04001781 if (!Format::SameSized(readBuffer->getFormat(), drawBuffer->getFormat()))
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001782 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001783 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001784 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001785 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001786
Dongseong Hwang44b422c2014-12-09 15:42:01 +02001787 if (readBuffer->getSamples() > 0 && !sameBounds)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001788 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001789 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001790 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001791 }
Geoff Lange4915782017-04-12 15:19:07 -04001792
1793 if (context->getExtensions().webglCompatibility && *readBuffer == *drawBuffer)
1794 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001795 context->handleError(
1796 InvalidOperation()
1797 << "Read and write depth stencil attachments cannot be the same image.");
Geoff Lange4915782017-04-12 15:19:07 -04001798 return false;
1799 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001800 }
He Yunchao66a41a22016-12-15 16:45:05 +08001801 // WebGL 2.0 BlitFramebuffer when blitting from a missing attachment
1802 else if (drawBuffer)
1803 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001804 context->handleError(InvalidOperation() << "Attempt to read from a missing "
1805 "depth/stencil attachment of a "
1806 "complete framebuffer.");
He Yunchao66a41a22016-12-15 16:45:05 +08001807 return false;
1808 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001809 }
1810 }
1811
1812 return true;
1813}
1814
Jamie Madill4928b7c2017-06-20 12:57:39 -04001815bool ValidateReadPixelsRobustANGLE(Context *context,
Geoff Lang62fce5b2016-09-30 10:46:35 -04001816 GLint x,
1817 GLint y,
1818 GLsizei width,
1819 GLsizei height,
1820 GLenum format,
1821 GLenum type,
1822 GLsizei bufSize,
1823 GLsizei *length,
Geoff Lange93daba2017-03-30 13:54:40 -04001824 GLsizei *columns,
1825 GLsizei *rows,
Jamie Madill876429b2017-04-20 15:46:24 -04001826 void *pixels)
Geoff Lang62fce5b2016-09-30 10:46:35 -04001827{
1828 if (!ValidateRobustEntryPoint(context, bufSize))
Jamie Madillc29968b2016-01-20 11:17:23 -05001829 {
Jamie Madillc29968b2016-01-20 11:17:23 -05001830 return false;
1831 }
1832
Geoff Lang62fce5b2016-09-30 10:46:35 -04001833 if (!ValidateReadPixelsBase(context, x, y, width, height, format, type, bufSize, length,
Geoff Lange93daba2017-03-30 13:54:40 -04001834 columns, rows, pixels))
Jamie Madill26e91952014-03-05 15:01:27 -05001835 {
Geoff Langb1196682014-07-23 13:47:29 -04001836 return false;
Jamie Madill26e91952014-03-05 15:01:27 -05001837 }
1838
Geoff Lang62fce5b2016-09-30 10:46:35 -04001839 if (!ValidateRobustBufferSize(context, bufSize, *length))
Jamie Madill26e91952014-03-05 15:01:27 -05001840 {
Geoff Langb1196682014-07-23 13:47:29 -04001841 return false;
Jamie Madill26e91952014-03-05 15:01:27 -05001842 }
1843
Jamie Madillc29968b2016-01-20 11:17:23 -05001844 return true;
1845}
1846
1847bool ValidateReadnPixelsEXT(Context *context,
1848 GLint x,
1849 GLint y,
1850 GLsizei width,
1851 GLsizei height,
1852 GLenum format,
1853 GLenum type,
1854 GLsizei bufSize,
Jamie Madill876429b2017-04-20 15:46:24 -04001855 void *pixels)
Jamie Madillc29968b2016-01-20 11:17:23 -05001856{
1857 if (bufSize < 0)
1858 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001859 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeBufferSize);
Jamie Madillc29968b2016-01-20 11:17:23 -05001860 return false;
1861 }
1862
Geoff Lang62fce5b2016-09-30 10:46:35 -04001863 return ValidateReadPixelsBase(context, x, y, width, height, format, type, bufSize, nullptr,
Geoff Lange93daba2017-03-30 13:54:40 -04001864 nullptr, nullptr, pixels);
Geoff Lang62fce5b2016-09-30 10:46:35 -04001865}
Jamie Madill26e91952014-03-05 15:01:27 -05001866
Jamie Madill4928b7c2017-06-20 12:57:39 -04001867bool ValidateReadnPixelsRobustANGLE(Context *context,
Geoff Lang62fce5b2016-09-30 10:46:35 -04001868 GLint x,
1869 GLint y,
1870 GLsizei width,
1871 GLsizei height,
1872 GLenum format,
1873 GLenum type,
1874 GLsizei bufSize,
1875 GLsizei *length,
Geoff Lange93daba2017-03-30 13:54:40 -04001876 GLsizei *columns,
1877 GLsizei *rows,
Jamie Madill876429b2017-04-20 15:46:24 -04001878 void *data)
Geoff Lang62fce5b2016-09-30 10:46:35 -04001879{
1880 if (!ValidateRobustEntryPoint(context, bufSize))
Jamie Madille2e406c2016-06-02 13:04:10 -04001881 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001882 return false;
1883 }
1884
Geoff Lange93daba2017-03-30 13:54:40 -04001885 if (!ValidateReadPixelsBase(context, x, y, width, height, format, type, bufSize, length,
1886 columns, rows, data))
Jamie Madille2e406c2016-06-02 13:04:10 -04001887 {
Jamie Madillc29968b2016-01-20 11:17:23 -05001888 return false;
Jamie Madill26e91952014-03-05 15:01:27 -05001889 }
1890
Geoff Lang62fce5b2016-09-30 10:46:35 -04001891 if (!ValidateRobustBufferSize(context, bufSize, *length))
1892 {
1893 return false;
1894 }
1895
1896 return true;
Jamie Madill26e91952014-03-05 15:01:27 -05001897}
1898
Olli Etuaho41997e72016-03-10 13:38:39 +02001899bool ValidateGenQueriesEXT(gl::Context *context, GLsizei n)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001900{
1901 if (!context->getExtensions().occlusionQueryBoolean &&
1902 !context->getExtensions().disjointTimerQuery)
1903 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001904 context->handleError(InvalidOperation() << "Query extension not enabled");
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001905 return false;
1906 }
1907
Olli Etuaho41997e72016-03-10 13:38:39 +02001908 return ValidateGenOrDelete(context, n);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001909}
1910
Olli Etuaho41997e72016-03-10 13:38:39 +02001911bool ValidateDeleteQueriesEXT(gl::Context *context, GLsizei n)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001912{
1913 if (!context->getExtensions().occlusionQueryBoolean &&
1914 !context->getExtensions().disjointTimerQuery)
1915 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001916 context->handleError(InvalidOperation() << "Query extension not enabled");
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001917 return false;
1918 }
1919
Olli Etuaho41997e72016-03-10 13:38:39 +02001920 return ValidateGenOrDelete(context, n);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001921}
1922
1923bool ValidateBeginQueryBase(gl::Context *context, GLenum target, GLuint id)
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001924{
1925 if (!ValidQueryType(context, target))
1926 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001927 context->handleError(InvalidEnum() << "Invalid query target");
Geoff Langb1196682014-07-23 13:47:29 -04001928 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001929 }
1930
1931 if (id == 0)
1932 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001933 context->handleError(InvalidOperation() << "Query id is 0");
Geoff Langb1196682014-07-23 13:47:29 -04001934 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001935 }
1936
1937 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
1938 // of zero, if the active query object name for <target> is non-zero (for the
1939 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
1940 // the active query for either target is non-zero), if <id> is the name of an
1941 // existing query object whose type does not match <target>, or if <id> is the
1942 // active query object name for any query type, the error INVALID_OPERATION is
1943 // generated.
1944
1945 // Ensure no other queries are active
1946 // NOTE: If other queries than occlusion are supported, we will need to check
1947 // separately that:
1948 // a) The query ID passed is not the current active query for any target/type
1949 // b) There are no active queries for the requested target (and in the case
1950 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
1951 // no query may be active for either if glBeginQuery targets either.
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001952
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001953 if (context->getGLState().isQueryActive(target))
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001954 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001955 context->handleError(InvalidOperation() << "Other query is active");
Geoff Langb1196682014-07-23 13:47:29 -04001956 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001957 }
1958
1959 Query *queryObject = context->getQuery(id, true, target);
1960
1961 // check that name was obtained with glGenQueries
1962 if (!queryObject)
1963 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001964 context->handleError(InvalidOperation() << "Invalid query id");
Geoff Langb1196682014-07-23 13:47:29 -04001965 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001966 }
1967
1968 // check for type mismatch
1969 if (queryObject->getType() != target)
1970 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001971 context->handleError(InvalidOperation() << "Query type does not match target");
Geoff Langb1196682014-07-23 13:47:29 -04001972 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001973 }
1974
1975 return true;
1976}
1977
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001978bool ValidateBeginQueryEXT(gl::Context *context, GLenum target, GLuint id)
1979{
1980 if (!context->getExtensions().occlusionQueryBoolean &&
Geoff Lang2b4ce802016-04-28 13:34:50 -04001981 !context->getExtensions().disjointTimerQuery && !context->getExtensions().syncQuery)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001982 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001983 context->handleError(InvalidOperation() << "Query extension not enabled");
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001984 return false;
1985 }
1986
1987 return ValidateBeginQueryBase(context, target, id);
1988}
1989
1990bool ValidateEndQueryBase(gl::Context *context, GLenum target)
Jamie Madill45c785d2014-05-13 14:09:34 -04001991{
1992 if (!ValidQueryType(context, target))
1993 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001994 context->handleError(InvalidEnum() << "Invalid query target");
Geoff Langb1196682014-07-23 13:47:29 -04001995 return false;
Jamie Madill45c785d2014-05-13 14:09:34 -04001996 }
1997
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001998 const Query *queryObject = context->getGLState().getActiveQuery(target);
Jamie Madill45c785d2014-05-13 14:09:34 -04001999
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002000 if (queryObject == nullptr)
Jamie Madill45c785d2014-05-13 14:09:34 -04002001 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002002 context->handleError(InvalidOperation() << "Query target not active");
Geoff Langb1196682014-07-23 13:47:29 -04002003 return false;
Jamie Madill45c785d2014-05-13 14:09:34 -04002004 }
2005
Jamie Madill45c785d2014-05-13 14:09:34 -04002006 return true;
2007}
2008
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002009bool ValidateEndQueryEXT(gl::Context *context, GLenum target)
2010{
2011 if (!context->getExtensions().occlusionQueryBoolean &&
Geoff Lang2b4ce802016-04-28 13:34:50 -04002012 !context->getExtensions().disjointTimerQuery && !context->getExtensions().syncQuery)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002013 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002014 context->handleError(InvalidOperation() << "Query extension not enabled");
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002015 return false;
2016 }
2017
2018 return ValidateEndQueryBase(context, target);
2019}
2020
2021bool ValidateQueryCounterEXT(Context *context, GLuint id, GLenum target)
2022{
2023 if (!context->getExtensions().disjointTimerQuery)
2024 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002025 context->handleError(InvalidOperation() << "Disjoint timer query not enabled");
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002026 return false;
2027 }
2028
2029 if (target != GL_TIMESTAMP_EXT)
2030 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002031 context->handleError(InvalidEnum() << "Invalid query target");
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002032 return false;
2033 }
2034
2035 Query *queryObject = context->getQuery(id, true, target);
2036 if (queryObject == nullptr)
2037 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002038 context->handleError(InvalidOperation() << "Invalid query id");
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002039 return false;
2040 }
2041
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002042 if (context->getGLState().isQueryActive(queryObject))
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002043 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002044 context->handleError(InvalidOperation() << "Query is active");
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002045 return false;
2046 }
2047
2048 return true;
2049}
2050
Geoff Lang2186c382016-10-14 10:54:54 -04002051bool ValidateGetQueryivBase(Context *context, GLenum target, GLenum pname, GLsizei *numParams)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002052{
Geoff Lang2186c382016-10-14 10:54:54 -04002053 if (numParams)
2054 {
2055 *numParams = 0;
2056 }
2057
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002058 if (!ValidQueryType(context, target) && target != GL_TIMESTAMP_EXT)
2059 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002060 context->handleError(InvalidEnum() << "Invalid query type");
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002061 return false;
2062 }
2063
2064 switch (pname)
2065 {
2066 case GL_CURRENT_QUERY_EXT:
2067 if (target == GL_TIMESTAMP_EXT)
2068 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002069 context->handleError(InvalidEnum() << "Cannot use current query for timestamp");
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002070 return false;
2071 }
2072 break;
2073 case GL_QUERY_COUNTER_BITS_EXT:
2074 if (!context->getExtensions().disjointTimerQuery ||
2075 (target != GL_TIMESTAMP_EXT && target != GL_TIME_ELAPSED_EXT))
2076 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002077 context->handleError(InvalidEnum() << "Invalid pname");
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002078 return false;
2079 }
2080 break;
2081 default:
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002082 context->handleError(InvalidEnum() << "Invalid pname");
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002083 return false;
2084 }
2085
Geoff Lang2186c382016-10-14 10:54:54 -04002086 if (numParams)
2087 {
2088 // All queries return only one value
2089 *numParams = 1;
2090 }
2091
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002092 return true;
2093}
2094
2095bool ValidateGetQueryivEXT(Context *context, GLenum target, GLenum pname, GLint *params)
2096{
2097 if (!context->getExtensions().occlusionQueryBoolean &&
Geoff Lang2b4ce802016-04-28 13:34:50 -04002098 !context->getExtensions().disjointTimerQuery && !context->getExtensions().syncQuery)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002099 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002100 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002101 return false;
2102 }
2103
Geoff Lang2186c382016-10-14 10:54:54 -04002104 return ValidateGetQueryivBase(context, target, pname, nullptr);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002105}
2106
Geoff Lang2186c382016-10-14 10:54:54 -04002107bool ValidateGetQueryivRobustANGLE(Context *context,
2108 GLenum target,
2109 GLenum pname,
2110 GLsizei bufSize,
2111 GLsizei *length,
2112 GLint *params)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002113{
Geoff Lang2186c382016-10-14 10:54:54 -04002114 if (!ValidateRobustEntryPoint(context, bufSize))
2115 {
2116 return false;
2117 }
2118
2119 if (!ValidateGetQueryivBase(context, target, pname, length))
2120 {
2121 return false;
2122 }
2123
2124 if (!ValidateRobustBufferSize(context, bufSize, *length))
2125 {
2126 return false;
2127 }
2128
2129 return true;
2130}
2131
2132bool ValidateGetQueryObjectValueBase(Context *context, GLuint id, GLenum pname, GLsizei *numParams)
2133{
2134 if (numParams)
2135 {
2136 *numParams = 0;
2137 }
2138
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002139 Query *queryObject = context->getQuery(id, false, GL_NONE);
2140
2141 if (!queryObject)
2142 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002143 context->handleError(InvalidOperation() << "Query does not exist");
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002144 return false;
2145 }
2146
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002147 if (context->getGLState().isQueryActive(queryObject))
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002148 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002149 context->handleError(InvalidOperation() << "Query currently active");
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002150 return false;
2151 }
2152
2153 switch (pname)
2154 {
2155 case GL_QUERY_RESULT_EXT:
2156 case GL_QUERY_RESULT_AVAILABLE_EXT:
2157 break;
2158
2159 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07002160 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002161 return false;
2162 }
2163
Geoff Lang2186c382016-10-14 10:54:54 -04002164 if (numParams)
2165 {
2166 *numParams = 1;
2167 }
2168
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002169 return true;
2170}
2171
2172bool ValidateGetQueryObjectivEXT(Context *context, GLuint id, GLenum pname, GLint *params)
2173{
2174 if (!context->getExtensions().disjointTimerQuery)
2175 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002176 context->handleError(InvalidOperation() << "Timer query extension not enabled");
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002177 return false;
2178 }
Geoff Lang2186c382016-10-14 10:54:54 -04002179 return ValidateGetQueryObjectValueBase(context, id, pname, nullptr);
2180}
2181
2182bool ValidateGetQueryObjectivRobustANGLE(Context *context,
2183 GLuint id,
2184 GLenum pname,
2185 GLsizei bufSize,
2186 GLsizei *length,
2187 GLint *params)
2188{
2189 if (!context->getExtensions().disjointTimerQuery)
2190 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002191 context->handleError(InvalidOperation() << "Timer query extension not enabled");
Geoff Lang2186c382016-10-14 10:54:54 -04002192 return false;
2193 }
2194
2195 if (!ValidateRobustEntryPoint(context, bufSize))
2196 {
2197 return false;
2198 }
2199
2200 if (!ValidateGetQueryObjectValueBase(context, id, pname, length))
2201 {
2202 return false;
2203 }
2204
2205 if (!ValidateRobustBufferSize(context, bufSize, *length))
2206 {
2207 return false;
2208 }
2209
2210 return true;
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002211}
2212
2213bool ValidateGetQueryObjectuivEXT(Context *context, GLuint id, GLenum pname, GLuint *params)
2214{
2215 if (!context->getExtensions().disjointTimerQuery &&
Geoff Lang2b4ce802016-04-28 13:34:50 -04002216 !context->getExtensions().occlusionQueryBoolean && !context->getExtensions().syncQuery)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002217 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002218 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002219 return false;
2220 }
Geoff Lang2186c382016-10-14 10:54:54 -04002221 return ValidateGetQueryObjectValueBase(context, id, pname, nullptr);
2222}
2223
2224bool ValidateGetQueryObjectuivRobustANGLE(Context *context,
2225 GLuint id,
2226 GLenum pname,
2227 GLsizei bufSize,
2228 GLsizei *length,
2229 GLuint *params)
2230{
2231 if (!context->getExtensions().disjointTimerQuery &&
2232 !context->getExtensions().occlusionQueryBoolean && !context->getExtensions().syncQuery)
2233 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002234 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
Geoff Lang2186c382016-10-14 10:54:54 -04002235 return false;
2236 }
2237
2238 if (!ValidateRobustEntryPoint(context, bufSize))
2239 {
2240 return false;
2241 }
2242
2243 if (!ValidateGetQueryObjectValueBase(context, id, pname, length))
2244 {
2245 return false;
2246 }
2247
2248 if (!ValidateRobustBufferSize(context, bufSize, *length))
2249 {
2250 return false;
2251 }
2252
2253 return true;
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002254}
2255
2256bool ValidateGetQueryObjecti64vEXT(Context *context, GLuint id, GLenum pname, GLint64 *params)
2257{
2258 if (!context->getExtensions().disjointTimerQuery)
2259 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002260 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002261 return false;
2262 }
Geoff Lang2186c382016-10-14 10:54:54 -04002263 return ValidateGetQueryObjectValueBase(context, id, pname, nullptr);
2264}
2265
2266bool ValidateGetQueryObjecti64vRobustANGLE(Context *context,
2267 GLuint id,
2268 GLenum pname,
2269 GLsizei bufSize,
2270 GLsizei *length,
2271 GLint64 *params)
2272{
2273 if (!context->getExtensions().disjointTimerQuery)
2274 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002275 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
Geoff Lang2186c382016-10-14 10:54:54 -04002276 return false;
2277 }
2278
2279 if (!ValidateRobustEntryPoint(context, bufSize))
2280 {
2281 return false;
2282 }
2283
2284 if (!ValidateGetQueryObjectValueBase(context, id, pname, length))
2285 {
2286 return false;
2287 }
2288
2289 if (!ValidateRobustBufferSize(context, bufSize, *length))
2290 {
2291 return false;
2292 }
2293
2294 return true;
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002295}
2296
2297bool ValidateGetQueryObjectui64vEXT(Context *context, GLuint id, GLenum pname, GLuint64 *params)
2298{
2299 if (!context->getExtensions().disjointTimerQuery)
2300 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002301 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002302 return false;
2303 }
Geoff Lang2186c382016-10-14 10:54:54 -04002304 return ValidateGetQueryObjectValueBase(context, id, pname, nullptr);
2305}
2306
2307bool ValidateGetQueryObjectui64vRobustANGLE(Context *context,
2308 GLuint id,
2309 GLenum pname,
2310 GLsizei bufSize,
2311 GLsizei *length,
2312 GLuint64 *params)
2313{
2314 if (!context->getExtensions().disjointTimerQuery)
2315 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002316 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
Geoff Lang2186c382016-10-14 10:54:54 -04002317 return false;
2318 }
2319
2320 if (!ValidateRobustEntryPoint(context, bufSize))
2321 {
2322 return false;
2323 }
2324
2325 if (!ValidateGetQueryObjectValueBase(context, id, pname, length))
2326 {
2327 return false;
2328 }
2329
2330 if (!ValidateRobustBufferSize(context, bufSize, *length))
2331 {
2332 return false;
2333 }
2334
2335 return true;
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002336}
2337
Jiajia Qinee9f08c2016-11-16 10:06:10 +08002338bool ValidateProgramUniform(gl::Context *context,
Frank Henigmanf5f74ae2017-02-02 21:14:23 -05002339 GLenum valueType,
Jiajia Qinee9f08c2016-11-16 10:06:10 +08002340 GLuint program,
2341 GLint location,
2342 GLsizei count)
2343{
2344 // Check for ES31 program uniform entry points
2345 if (context->getClientVersion() < Version(3, 1))
2346 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002347 context->handleError(InvalidOperation());
Jiajia Qinee9f08c2016-11-16 10:06:10 +08002348 return false;
2349 }
2350
2351 const LinkedUniform *uniform = nullptr;
2352 gl::Program *programObject = GetValidProgram(context, program);
Frank Henigmanf5f74ae2017-02-02 21:14:23 -05002353 return ValidateUniformCommonBase(context, programObject, location, count, &uniform) &&
2354 ValidateUniformValue(context, valueType, uniform->type);
Jiajia Qinee9f08c2016-11-16 10:06:10 +08002355}
2356
Frank Henigmana98a6472017-02-02 21:38:32 -05002357bool ValidateProgramUniform1iv(gl::Context *context,
2358 GLuint program,
2359 GLint location,
2360 GLsizei count,
2361 const GLint *value)
2362{
2363 // Check for ES31 program uniform entry points
2364 if (context->getClientVersion() < Version(3, 1))
2365 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002366 context->handleError(InvalidOperation());
Frank Henigmana98a6472017-02-02 21:38:32 -05002367 return false;
2368 }
2369
2370 const LinkedUniform *uniform = nullptr;
2371 gl::Program *programObject = GetValidProgram(context, program);
2372 return ValidateUniformCommonBase(context, programObject, location, count, &uniform) &&
2373 ValidateUniform1ivValue(context, uniform->type, count, value);
2374}
2375
Jiajia Qinee9f08c2016-11-16 10:06:10 +08002376bool ValidateProgramUniformMatrix(gl::Context *context,
Frank Henigmanf5f74ae2017-02-02 21:14:23 -05002377 GLenum valueType,
Jiajia Qinee9f08c2016-11-16 10:06:10 +08002378 GLuint program,
2379 GLint location,
2380 GLsizei count,
2381 GLboolean transpose)
2382{
2383 // Check for ES31 program uniform entry points
2384 if (context->getClientVersion() < Version(3, 1))
2385 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002386 context->handleError(InvalidOperation());
Jiajia Qinee9f08c2016-11-16 10:06:10 +08002387 return false;
2388 }
2389
2390 const LinkedUniform *uniform = nullptr;
2391 gl::Program *programObject = GetValidProgram(context, program);
Frank Henigmanf5f74ae2017-02-02 21:14:23 -05002392 return ValidateUniformCommonBase(context, programObject, location, count, &uniform) &&
2393 ValidateUniformMatrixValue(context, valueType, uniform->type);
Jiajia Qinee9f08c2016-11-16 10:06:10 +08002394}
2395
Jamie Madillc1d770e2017-04-13 17:31:24 -04002396bool ValidateUniform(ValidationContext *context, GLenum valueType, GLint location, GLsizei count)
Jamie Madillaa981bd2014-05-20 10:55:55 -04002397{
2398 // Check for ES3 uniform entry points
Frank Henigmanf5f74ae2017-02-02 21:14:23 -05002399 if (VariableComponentType(valueType) == GL_UNSIGNED_INT && context->getClientMajorVersion() < 3)
Jamie Madillaa981bd2014-05-20 10:55:55 -04002400 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002401 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04002402 return false;
Jamie Madillaa981bd2014-05-20 10:55:55 -04002403 }
2404
Jamie Madill62d31cb2015-09-11 13:25:51 -04002405 const LinkedUniform *uniform = nullptr;
Frank Henigmanf5f74ae2017-02-02 21:14:23 -05002406 gl::Program *programObject = context->getGLState().getProgram();
2407 return ValidateUniformCommonBase(context, programObject, location, count, &uniform) &&
2408 ValidateUniformValue(context, valueType, uniform->type);
Jamie Madillaa981bd2014-05-20 10:55:55 -04002409}
2410
Jamie Madillbe849e42017-05-02 15:49:00 -04002411bool ValidateUniform1iv(ValidationContext *context,
2412 GLint location,
2413 GLsizei count,
2414 const GLint *value)
Frank Henigmana98a6472017-02-02 21:38:32 -05002415{
2416 const LinkedUniform *uniform = nullptr;
2417 gl::Program *programObject = context->getGLState().getProgram();
2418 return ValidateUniformCommonBase(context, programObject, location, count, &uniform) &&
2419 ValidateUniform1ivValue(context, uniform->type, count, value);
2420}
2421
Jamie Madillc1d770e2017-04-13 17:31:24 -04002422bool ValidateUniformMatrix(ValidationContext *context,
Frank Henigmanf5f74ae2017-02-02 21:14:23 -05002423 GLenum valueType,
He Yunchaoced53ae2016-11-29 15:00:51 +08002424 GLint location,
2425 GLsizei count,
Jamie Madillaa981bd2014-05-20 10:55:55 -04002426 GLboolean transpose)
2427{
2428 // Check for ES3 uniform entry points
Frank Henigmanf5f74ae2017-02-02 21:14:23 -05002429 int rows = VariableRowCount(valueType);
2430 int cols = VariableColumnCount(valueType);
Martin Radev1be913c2016-07-11 17:59:16 +03002431 if (rows != cols && context->getClientMajorVersion() < 3)
Jamie Madillaa981bd2014-05-20 10:55:55 -04002432 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002433 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04002434 return false;
Jamie Madillaa981bd2014-05-20 10:55:55 -04002435 }
2436
Martin Radev1be913c2016-07-11 17:59:16 +03002437 if (transpose != GL_FALSE && context->getClientMajorVersion() < 3)
Jamie Madillaa981bd2014-05-20 10:55:55 -04002438 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002439 context->handleError(InvalidValue());
Geoff Langb1196682014-07-23 13:47:29 -04002440 return false;
Jamie Madillaa981bd2014-05-20 10:55:55 -04002441 }
2442
Jamie Madill62d31cb2015-09-11 13:25:51 -04002443 const LinkedUniform *uniform = nullptr;
Frank Henigmanf5f74ae2017-02-02 21:14:23 -05002444 gl::Program *programObject = context->getGLState().getProgram();
2445 return ValidateUniformCommonBase(context, programObject, location, count, &uniform) &&
2446 ValidateUniformMatrixValue(context, valueType, uniform->type);
Jamie Madillaa981bd2014-05-20 10:55:55 -04002447}
2448
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002449bool ValidateStateQuery(ValidationContext *context,
2450 GLenum pname,
2451 GLenum *nativeType,
2452 unsigned int *numParams)
Jamie Madill893ab082014-05-16 16:56:10 -04002453{
2454 if (!context->getQueryParameterInfo(pname, nativeType, numParams))
2455 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002456 context->handleError(InvalidEnum());
Geoff Langb1196682014-07-23 13:47:29 -04002457 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04002458 }
2459
Jamie Madill0af26e12015-03-05 19:54:33 -05002460 const Caps &caps = context->getCaps();
2461
Jamie Madill893ab082014-05-16 16:56:10 -04002462 if (pname >= GL_DRAW_BUFFER0 && pname <= GL_DRAW_BUFFER15)
2463 {
2464 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0);
2465
Jamie Madill0af26e12015-03-05 19:54:33 -05002466 if (colorAttachment >= caps.maxDrawBuffers)
Jamie Madill893ab082014-05-16 16:56:10 -04002467 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002468 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04002469 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04002470 }
2471 }
2472
2473 switch (pname)
2474 {
He Yunchaoced53ae2016-11-29 15:00:51 +08002475 case GL_TEXTURE_BINDING_2D:
2476 case GL_TEXTURE_BINDING_CUBE_MAP:
2477 case GL_TEXTURE_BINDING_3D:
2478 case GL_TEXTURE_BINDING_2D_ARRAY:
2479 break;
2480 case GL_TEXTURE_BINDING_EXTERNAL_OES:
2481 if (!context->getExtensions().eglStreamConsumerExternal &&
2482 !context->getExtensions().eglImageExternal)
2483 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002484 context->handleError(InvalidEnum() << "Neither NV_EGL_stream_consumer_external "
2485 "nor GL_OES_EGL_image_external "
2486 "extensions enabled");
He Yunchaoced53ae2016-11-29 15:00:51 +08002487 return false;
2488 }
2489 break;
Jamie Madill893ab082014-05-16 16:56:10 -04002490
He Yunchaoced53ae2016-11-29 15:00:51 +08002491 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
2492 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
Jamie Madill893ab082014-05-16 16:56:10 -04002493 {
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002494 if (context->getGLState().getReadFramebuffer()->checkStatus(context) !=
2495 GL_FRAMEBUFFER_COMPLETE)
Jamie Madill893ab082014-05-16 16:56:10 -04002496 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002497 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04002498 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04002499 }
2500
Jamie Madill51f40ec2016-06-15 14:06:00 -04002501 const Framebuffer *framebuffer = context->getGLState().getReadFramebuffer();
2502 ASSERT(framebuffer);
Martin Radev138064f2016-07-15 12:03:41 +03002503
2504 if (framebuffer->getReadBufferState() == GL_NONE)
2505 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002506 context->handleError(InvalidOperation() << "Read buffer is GL_NONE");
Martin Radev138064f2016-07-15 12:03:41 +03002507 return false;
2508 }
2509
Jamie Madillb6bda4a2015-04-20 12:53:26 -04002510 const FramebufferAttachment *attachment = framebuffer->getReadColorbuffer();
Jamie Madill3c7fa222014-06-05 13:08:51 -04002511 if (!attachment)
Jamie Madill893ab082014-05-16 16:56:10 -04002512 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002513 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04002514 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04002515 }
2516 }
2517 break;
2518
He Yunchaoced53ae2016-11-29 15:00:51 +08002519 default:
2520 break;
Jamie Madill893ab082014-05-16 16:56:10 -04002521 }
2522
2523 // pname is valid, but there are no parameters to return
Geoff Langff5b2d52016-09-07 11:32:23 -04002524 if (*numParams == 0)
2525 {
2526 return false;
2527 }
2528
2529 return true;
2530}
2531
2532bool ValidateRobustStateQuery(ValidationContext *context,
2533 GLenum pname,
2534 GLsizei bufSize,
2535 GLenum *nativeType,
2536 unsigned int *numParams)
2537{
2538 if (!ValidateRobustEntryPoint(context, bufSize))
2539 {
2540 return false;
2541 }
2542
2543 if (!ValidateStateQuery(context, pname, nativeType, numParams))
2544 {
2545 return false;
2546 }
2547
2548 if (!ValidateRobustBufferSize(context, bufSize, *numParams))
Jamie Madill893ab082014-05-16 16:56:10 -04002549 {
2550 return false;
2551 }
2552
2553 return true;
2554}
2555
Jamie Madillc29968b2016-01-20 11:17:23 -05002556bool ValidateCopyTexImageParametersBase(ValidationContext *context,
2557 GLenum target,
2558 GLint level,
2559 GLenum internalformat,
2560 bool isSubImage,
2561 GLint xoffset,
2562 GLint yoffset,
2563 GLint zoffset,
2564 GLint x,
2565 GLint y,
2566 GLsizei width,
2567 GLsizei height,
2568 GLint border,
Jamie Madill0c8abca2016-07-22 20:21:26 -04002569 Format *textureFormatOut)
Jamie Madill560a8d82014-05-21 13:06:20 -04002570{
Brandon Jones6cad5662017-06-14 13:25:13 -07002571 if (xoffset < 0 || yoffset < 0 || zoffset < 0)
Jamie Madill560a8d82014-05-21 13:06:20 -04002572 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002573 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeOffset);
2574 return false;
2575 }
2576
2577 if (width < 0 || height < 0)
2578 {
2579 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeSize);
Geoff Langb1196682014-07-23 13:47:29 -04002580 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002581 }
2582
He Yunchaoced53ae2016-11-29 15:00:51 +08002583 if (std::numeric_limits<GLsizei>::max() - xoffset < width ||
2584 std::numeric_limits<GLsizei>::max() - yoffset < height)
Jamie Madill560a8d82014-05-21 13:06:20 -04002585 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002586 context->handleError(InvalidValue());
Geoff Langb1196682014-07-23 13:47:29 -04002587 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002588 }
2589
2590 if (border != 0)
2591 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002592 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidBorder);
Geoff Langb1196682014-07-23 13:47:29 -04002593 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002594 }
2595
2596 if (!ValidMipLevel(context, target, level))
2597 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002598 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidMipLevel);
Geoff Langb1196682014-07-23 13:47:29 -04002599 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002600 }
2601
Jamie Madill51f40ec2016-06-15 14:06:00 -04002602 const auto &state = context->getGLState();
2603 auto readFramebuffer = state.getReadFramebuffer();
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002604 if (readFramebuffer->checkStatus(context) != GL_FRAMEBUFFER_COMPLETE)
Jamie Madill560a8d82014-05-21 13:06:20 -04002605 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002606 context->handleError(InvalidFramebufferOperation());
Geoff Langb1196682014-07-23 13:47:29 -04002607 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002608 }
2609
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002610 if (readFramebuffer->id() != 0 && readFramebuffer->getSamples(context) != 0)
Jamie Madill560a8d82014-05-21 13:06:20 -04002611 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002612 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04002613 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002614 }
2615
Martin Radev138064f2016-07-15 12:03:41 +03002616 if (readFramebuffer->getReadBufferState() == GL_NONE)
2617 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002618 context->handleError(InvalidOperation() << "Read buffer is GL_NONE");
Martin Radev138064f2016-07-15 12:03:41 +03002619 return false;
2620 }
2621
Corentin Wallez3c90ed62016-12-16 16:19:28 -05002622 // WebGL 1.0 [Section 6.26] Reading From a Missing Attachment
2623 // In OpenGL ES it is undefined what happens when an operation tries to read from a missing
He Yunchao66a41a22016-12-15 16:45:05 +08002624 // attachment and WebGL defines it to be an error. We do the check unconditionally as the
Corentin Wallez3c90ed62016-12-16 16:19:28 -05002625 // situation is an application error that would lead to a crash in ANGLE.
2626 if (readFramebuffer->getReadColorbuffer() == nullptr)
2627 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002628 ANGLE_VALIDATION_ERR(context, InvalidOperation(), MissingReadAttachment);
Corentin Wallez3c90ed62016-12-16 16:19:28 -05002629 return false;
2630 }
2631
Geoff Langaae65a42014-05-26 12:43:44 -04002632 const gl::Caps &caps = context->getCaps();
2633
Geoff Langaae65a42014-05-26 12:43:44 -04002634 GLuint maxDimension = 0;
Jamie Madill560a8d82014-05-21 13:06:20 -04002635 switch (target)
2636 {
He Yunchaoced53ae2016-11-29 15:00:51 +08002637 case GL_TEXTURE_2D:
2638 maxDimension = caps.max2DTextureSize;
2639 break;
Jamie Madill560a8d82014-05-21 13:06:20 -04002640
He Yunchaoced53ae2016-11-29 15:00:51 +08002641 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2642 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2643 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2644 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2645 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2646 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2647 maxDimension = caps.maxCubeMapTextureSize;
2648 break;
Jamie Madill560a8d82014-05-21 13:06:20 -04002649
He Yunchaoced53ae2016-11-29 15:00:51 +08002650 case GL_TEXTURE_2D_ARRAY:
2651 maxDimension = caps.max2DTextureSize;
2652 break;
Jamie Madill560a8d82014-05-21 13:06:20 -04002653
He Yunchaoced53ae2016-11-29 15:00:51 +08002654 case GL_TEXTURE_3D:
2655 maxDimension = caps.max3DTextureSize;
2656 break;
Jamie Madill560a8d82014-05-21 13:06:20 -04002657
He Yunchaoced53ae2016-11-29 15:00:51 +08002658 default:
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002659 context->handleError(InvalidEnum());
He Yunchaoced53ae2016-11-29 15:00:51 +08002660 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002661 }
2662
Jamie Madillc29968b2016-01-20 11:17:23 -05002663 gl::Texture *texture =
2664 state.getTargetTexture(IsCubeMapTextureTarget(target) ? GL_TEXTURE_CUBE_MAP : target);
Jamie Madill560a8d82014-05-21 13:06:20 -04002665 if (!texture)
2666 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002667 ANGLE_VALIDATION_ERR(context, InvalidOperation(), TextureNotBound);
Geoff Langb1196682014-07-23 13:47:29 -04002668 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002669 }
2670
Geoff Lang69cce582015-09-17 13:20:36 -04002671 if (texture->getImmutableFormat() && !isSubImage)
Jamie Madill560a8d82014-05-21 13:06:20 -04002672 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002673 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04002674 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002675 }
2676
Geoff Langca271392017-04-05 12:30:00 -04002677 const gl::InternalFormat &formatInfo =
2678 gl::GetInternalFormatInfo(internalformat, GL_UNSIGNED_BYTE);
Geoff Lang5d601382014-07-22 15:14:06 -04002679
Geoff Lang966c9402017-04-18 12:38:27 -04002680 if (formatInfo.depthBits > 0 || formatInfo.compressed)
Jamie Madill560a8d82014-05-21 13:06:20 -04002681 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002682 context->handleError(InvalidOperation());
Geoff Langa9be0dc2014-12-17 12:34:40 -05002683 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002684 }
2685
2686 if (isSubImage)
2687 {
Geoff Langa9be0dc2014-12-17 12:34:40 -05002688 if (static_cast<size_t>(xoffset + width) > texture->getWidth(target, level) ||
2689 static_cast<size_t>(yoffset + height) > texture->getHeight(target, level) ||
2690 static_cast<size_t>(zoffset) >= texture->getDepth(target, level))
Jamie Madill560a8d82014-05-21 13:06:20 -04002691 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002692 context->handleError(InvalidValue());
Geoff Langb1196682014-07-23 13:47:29 -04002693 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002694 }
2695 }
Jamie Madill6f38f822014-06-06 17:12:20 -04002696 else
2697 {
Geoff Lang691e58c2014-12-19 17:03:25 -05002698 if (IsCubeMapTextureTarget(target) && width != height)
Jamie Madill6f38f822014-06-06 17:12:20 -04002699 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002700 ANGLE_VALIDATION_ERR(context, InvalidValue(), CubemapIncomplete);
Geoff Langb1196682014-07-23 13:47:29 -04002701 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04002702 }
2703
Geoff Langeb66a6e2016-10-31 13:06:12 -04002704 if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions()))
Jamie Madill6f38f822014-06-06 17:12:20 -04002705 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002706 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Geoff Langb1196682014-07-23 13:47:29 -04002707 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04002708 }
2709
2710 int maxLevelDimension = (maxDimension >> level);
He Yunchaoced53ae2016-11-29 15:00:51 +08002711 if (static_cast<int>(width) > maxLevelDimension ||
2712 static_cast<int>(height) > maxLevelDimension)
Jamie Madill6f38f822014-06-06 17:12:20 -04002713 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002714 ANGLE_VALIDATION_ERR(context, InvalidValue(), ResourceMaxTextureSize);
Geoff Langb1196682014-07-23 13:47:29 -04002715 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04002716 }
2717 }
Jamie Madill560a8d82014-05-21 13:06:20 -04002718
Jamie Madill0c8abca2016-07-22 20:21:26 -04002719 if (textureFormatOut)
2720 {
2721 *textureFormatOut = texture->getFormat(target, level);
2722 }
Jamie Madillf695a3a2017-01-11 17:36:35 -05002723
2724 // Detect texture copying feedback loops for WebGL.
2725 if (context->getExtensions().webglCompatibility)
2726 {
Jamie Madillfd3dd432017-02-02 19:59:59 -05002727 if (readFramebuffer->formsCopyingFeedbackLoopWith(texture->id(), level, zoffset))
Jamie Madillf695a3a2017-01-11 17:36:35 -05002728 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002729 context->handleError(InvalidOperation() << "Texture copying feedback loop formed "
2730 "between Framebuffer and specified "
2731 "Texture level.");
Jamie Madillf695a3a2017-01-11 17:36:35 -05002732 return false;
2733 }
2734 }
2735
Jamie Madill560a8d82014-05-21 13:06:20 -04002736 return true;
2737}
2738
Jiajia Qind9671222016-11-29 16:30:31 +08002739bool ValidateDrawBase(ValidationContext *context, GLenum mode, GLsizei count)
Jamie Madill250d33f2014-06-06 17:09:03 -04002740{
Jamie Madill1aeb1312014-06-20 13:21:25 -04002741 switch (mode)
2742 {
He Yunchaoced53ae2016-11-29 15:00:51 +08002743 case GL_POINTS:
2744 case GL_LINES:
2745 case GL_LINE_LOOP:
2746 case GL_LINE_STRIP:
2747 case GL_TRIANGLES:
2748 case GL_TRIANGLE_STRIP:
2749 case GL_TRIANGLE_FAN:
2750 break;
2751 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07002752 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidDrawMode);
He Yunchaoced53ae2016-11-29 15:00:51 +08002753 return false;
Jamie Madill1aeb1312014-06-20 13:21:25 -04002754 }
2755
Jamie Madill250d33f2014-06-06 17:09:03 -04002756 if (count < 0)
2757 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002758 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeCount);
Geoff Langb1196682014-07-23 13:47:29 -04002759 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04002760 }
2761
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002762 const State &state = context->getGLState();
Geoff Langb1196682014-07-23 13:47:29 -04002763
Jamie Madill250d33f2014-06-06 17:09:03 -04002764 // Check for mapped buffers
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002765 if (state.hasMappedBuffer(GL_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04002766 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002767 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04002768 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04002769 }
2770
Jamie Madillcbcde722017-01-06 14:50:00 -05002771 // Note: these separate values are not supported in WebGL, due to D3D's limitations. See
2772 // Section 6.10 of the WebGL 1.0 spec.
Jamie Madill51f40ec2016-06-15 14:06:00 -04002773 Framebuffer *framebuffer = state.getDrawFramebuffer();
Corentin Wallezb1d0a2552016-12-19 16:15:54 -05002774 if (context->getLimitations().noSeparateStencilRefsAndMasks ||
2775 context->getExtensions().webglCompatibility)
Jamie Madillac528012014-06-20 13:21:23 -04002776 {
Corentin Wallezb1d0a2552016-12-19 16:15:54 -05002777 const FramebufferAttachment *dsAttachment =
2778 framebuffer->getStencilOrDepthStencilAttachment();
2779 GLuint stencilBits = dsAttachment ? dsAttachment->getStencilSize() : 0;
He Yunchaoced53ae2016-11-29 15:00:51 +08002780 GLuint minimumRequiredStencilMask = (1 << stencilBits) - 1;
Jinyoung Hur85769f02015-10-20 17:08:44 -04002781 const DepthStencilState &depthStencilState = state.getDepthStencilState();
Corentin Wallezb1d0a2552016-12-19 16:15:54 -05002782
2783 bool differentRefs = state.getStencilRef() != state.getStencilBackRef();
2784 bool differentWritemasks =
2785 (depthStencilState.stencilWritemask & minimumRequiredStencilMask) !=
2786 (depthStencilState.stencilBackWritemask & minimumRequiredStencilMask);
2787 bool differentMasks = (depthStencilState.stencilMask & minimumRequiredStencilMask) !=
2788 (depthStencilState.stencilBackMask & minimumRequiredStencilMask);
2789
2790 if (differentRefs || differentWritemasks || differentMasks)
Geoff Lang3a86ad32015-09-01 11:47:05 -04002791 {
Jamie Madillcbcde722017-01-06 14:50:00 -05002792 if (!context->getExtensions().webglCompatibility)
2793 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05002794 ERR() << "This ANGLE implementation does not support separate front/back stencil "
2795 "writemasks, reference values, or stencil mask values.";
Jamie Madillcbcde722017-01-06 14:50:00 -05002796 }
Brandon Jones6cad5662017-06-14 13:25:13 -07002797 ANGLE_VALIDATION_ERR(context, InvalidOperation(), StencilReferenceMaskOrMismatch);
Geoff Lang3a86ad32015-09-01 11:47:05 -04002798 return false;
2799 }
Jamie Madillac528012014-06-20 13:21:23 -04002800 }
2801
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002802 if (framebuffer->checkStatus(context) != GL_FRAMEBUFFER_COMPLETE)
Jamie Madill13f7d7d2014-06-20 13:21:27 -04002803 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002804 context->handleError(InvalidFramebufferOperation());
Geoff Langb1196682014-07-23 13:47:29 -04002805 return false;
Jamie Madill13f7d7d2014-06-20 13:21:27 -04002806 }
2807
Geoff Lang7dd2e102014-11-10 15:19:26 -05002808 gl::Program *program = state.getProgram();
2809 if (!program)
Jamie Madilld4cfa572014-07-08 10:00:32 -04002810 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002811 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ProgramNotBound);
Geoff Langb1196682014-07-23 13:47:29 -04002812 return false;
Jamie Madilld4cfa572014-07-08 10:00:32 -04002813 }
2814
Yunchao Hef81ce4a2017-04-24 10:49:17 +08002815 if (!program->validateSamplers(nullptr, context->getCaps()))
Jamie Madilld4cfa572014-07-08 10:00:32 -04002816 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002817 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04002818 return false;
Jamie Madilld4cfa572014-07-08 10:00:32 -04002819 }
2820
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002821 // Uniform buffer validation
He Yunchaoced53ae2016-11-29 15:00:51 +08002822 for (unsigned int uniformBlockIndex = 0;
2823 uniformBlockIndex < program->getActiveUniformBlockCount(); uniformBlockIndex++)
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002824 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002825 const gl::UniformBlock &uniformBlock = program->getUniformBlockByIndex(uniformBlockIndex);
He Yunchaoced53ae2016-11-29 15:00:51 +08002826 GLuint blockBinding = program->getUniformBlockBinding(uniformBlockIndex);
Geoff Lang5d124a62015-09-15 13:03:27 -04002827 const OffsetBindingPointer<Buffer> &uniformBuffer =
2828 state.getIndexedUniformBuffer(blockBinding);
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002829
Geoff Lang5d124a62015-09-15 13:03:27 -04002830 if (uniformBuffer.get() == nullptr)
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002831 {
2832 // undefined behaviour
Jamie Madill437fa652016-05-03 15:13:24 -04002833 context->handleError(
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002834 InvalidOperation()
2835 << "It is undefined behaviour to have a used but unbound uniform buffer.");
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002836 return false;
2837 }
2838
Geoff Lang5d124a62015-09-15 13:03:27 -04002839 size_t uniformBufferSize = uniformBuffer.getSize();
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002840 if (uniformBufferSize == 0)
2841 {
2842 // Bind the whole buffer.
Minmin Gong794e0002015-04-07 18:31:54 -07002843 uniformBufferSize = static_cast<size_t>(uniformBuffer->getSize());
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002844 }
2845
Jamie Madill62d31cb2015-09-11 13:25:51 -04002846 if (uniformBufferSize < uniformBlock.dataSize)
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002847 {
2848 // undefined behaviour
Jamie Madill437fa652016-05-03 15:13:24 -04002849 context->handleError(
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002850 InvalidOperation()
2851 << "It is undefined behaviour to use a uniform buffer that is too small.");
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002852 return false;
2853 }
2854 }
2855
Geoff Lange0cff192017-05-30 13:04:56 -04002856 // Do some additonal WebGL-specific validation
Jamie Madilla4595b82017-01-11 17:36:34 -05002857 if (context->getExtensions().webglCompatibility)
2858 {
Geoff Lange0cff192017-05-30 13:04:56 -04002859 // Detect rendering feedback loops for WebGL.
Jamie Madilla4595b82017-01-11 17:36:34 -05002860 if (framebuffer->formsRenderingFeedbackLoopWith(state))
2861 {
2862 context->handleError(
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002863 InvalidOperation()
2864 << "Rendering feedback loop formed between Framebuffer and active Texture.");
Jamie Madilla4595b82017-01-11 17:36:34 -05002865 return false;
2866 }
Geoff Lange0cff192017-05-30 13:04:56 -04002867
Geoff Lang9ab5b822017-05-30 16:19:23 -04002868 // Detect that the vertex shader input types match the attribute types
2869 if (!ValidateVertexShaderAttributeTypeMatch(context))
2870 {
2871 return false;
2872 }
2873
Geoff Lange0cff192017-05-30 13:04:56 -04002874 // Detect that the color buffer types match the fragment shader output types
2875 if (!ValidateFragmentShaderColorBufferTypeMatch(context))
2876 {
2877 return false;
2878 }
Jamie Madilla4595b82017-01-11 17:36:34 -05002879 }
2880
Jamie Madill250d33f2014-06-06 17:09:03 -04002881 // No-op if zero count
2882 return (count > 0);
2883}
2884
Jamie Madillc1d770e2017-04-13 17:31:24 -04002885bool ValidateDrawArraysCommon(ValidationContext *context,
2886 GLenum mode,
2887 GLint first,
2888 GLsizei count,
2889 GLsizei primcount)
Jamie Madill250d33f2014-06-06 17:09:03 -04002890{
Jamie Madillfd716582014-06-06 17:09:04 -04002891 if (first < 0)
Jamie Madill250d33f2014-06-06 17:09:03 -04002892 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002893 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeStart);
Geoff Langb1196682014-07-23 13:47:29 -04002894 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04002895 }
2896
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002897 const State &state = context->getGLState();
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002898 gl::TransformFeedback *curTransformFeedback = state.getCurrentTransformFeedback();
He Yunchaoced53ae2016-11-29 15:00:51 +08002899 if (curTransformFeedback && curTransformFeedback->isActive() &&
2900 !curTransformFeedback->isPaused() && curTransformFeedback->getPrimitiveMode() != mode)
Jamie Madillfd716582014-06-06 17:09:04 -04002901 {
2902 // It is an invalid operation to call DrawArrays or DrawArraysInstanced with a draw mode
He Yunchaoced53ae2016-11-29 15:00:51 +08002903 // that does not match the current transform feedback object's draw mode (if transform
2904 // feedback
Jamie Madillfd716582014-06-06 17:09:04 -04002905 // is active), (3.0.2, section 2.14, pg 86)
Brandon Jones6cad5662017-06-14 13:25:13 -07002906 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidDrawModeTransformFeedback);
Geoff Langb1196682014-07-23 13:47:29 -04002907 return false;
Jamie Madillfd716582014-06-06 17:09:04 -04002908 }
2909
Jiajia Qind9671222016-11-29 16:30:31 +08002910 if (!ValidateDrawBase(context, mode, count))
Corentin Wallez18a2fb32015-08-10 12:58:14 -07002911 {
2912 return false;
2913 }
2914
Corentin Wallez71168a02016-12-19 15:11:18 -08002915 // Check the computation of maxVertex doesn't overflow.
2916 // - first < 0 or count < 0 have been checked as an error condition
2917 // - count > 0 has been checked in ValidateDrawBase as it makes the call a noop
2918 // From this we know maxVertex will be positive, and only need to check if it overflows GLint.
2919 ASSERT(count > 0 && first >= 0);
2920 int64_t maxVertex = static_cast<int64_t>(first) + static_cast<int64_t>(count) - 1;
2921 if (maxVertex > static_cast<int64_t>(std::numeric_limits<GLint>::max()))
Corentin Wallez92db6942016-12-09 13:10:36 -05002922 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002923 ANGLE_VALIDATION_ERR(context, InvalidOperation(), IntegerOverflow);
Corentin Wallez92db6942016-12-09 13:10:36 -05002924 return false;
2925 }
2926
Corentin Wallez71168a02016-12-19 15:11:18 -08002927 if (!ValidateDrawAttribs(context, primcount, static_cast<GLint>(maxVertex), count))
Jamie Madillfd716582014-06-06 17:09:04 -04002928 {
2929 return false;
2930 }
2931
2932 return true;
2933}
2934
He Yunchaoced53ae2016-11-29 15:00:51 +08002935bool ValidateDrawArraysInstanced(Context *context,
2936 GLenum mode,
2937 GLint first,
2938 GLsizei count,
2939 GLsizei primcount)
Jamie Madillfd716582014-06-06 17:09:04 -04002940{
Corentin Wallez0dc97812017-06-22 14:38:44 -04002941 return ValidateDrawArraysInstancedBase(context, mode, first, count, primcount);
Geoff Lang87a93302014-09-16 13:29:43 -04002942}
2943
He Yunchaoced53ae2016-11-29 15:00:51 +08002944bool ValidateDrawArraysInstancedANGLE(Context *context,
2945 GLenum mode,
2946 GLint first,
2947 GLsizei count,
2948 GLsizei primcount)
Geoff Lang87a93302014-09-16 13:29:43 -04002949{
Corentin Wallez170efbf2017-05-02 13:45:01 -04002950 if (!ValidateDrawArraysInstancedBase(context, mode, first, count, primcount))
Geoff Lang87a93302014-09-16 13:29:43 -04002951 {
2952 return false;
2953 }
2954
Corentin Wallez0dc97812017-06-22 14:38:44 -04002955 return ValidateDrawInstancedANGLE(context);
Geoff Lang87a93302014-09-16 13:29:43 -04002956}
2957
Jiajia Qind9671222016-11-29 16:30:31 +08002958bool ValidateDrawElementsBase(ValidationContext *context, GLenum type)
Jamie Madillfd716582014-06-06 17:09:04 -04002959{
Jamie Madill250d33f2014-06-06 17:09:03 -04002960 switch (type)
2961 {
He Yunchaoced53ae2016-11-29 15:00:51 +08002962 case GL_UNSIGNED_BYTE:
2963 case GL_UNSIGNED_SHORT:
2964 break;
2965 case GL_UNSIGNED_INT:
2966 if (context->getClientMajorVersion() < 3 && !context->getExtensions().elementIndexUint)
2967 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002968 ANGLE_VALIDATION_ERR(context, InvalidEnum(), TypeNotUnsignedShortByte);
He Yunchaoced53ae2016-11-29 15:00:51 +08002969 return false;
2970 }
2971 break;
2972 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07002973 ANGLE_VALIDATION_ERR(context, InvalidEnum(), TypeNotUnsignedShortByte);
He Yunchaoced53ae2016-11-29 15:00:51 +08002974 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04002975 }
2976
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002977 const State &state = context->getGLState();
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002978
2979 gl::TransformFeedback *curTransformFeedback = state.getCurrentTransformFeedback();
He Yunchaoced53ae2016-11-29 15:00:51 +08002980 if (curTransformFeedback && curTransformFeedback->isActive() &&
2981 !curTransformFeedback->isPaused())
Jamie Madill250d33f2014-06-06 17:09:03 -04002982 {
He Yunchaoced53ae2016-11-29 15:00:51 +08002983 // It is an invalid operation to call DrawElements, DrawRangeElements or
2984 // DrawElementsInstanced
Jamie Madill250d33f2014-06-06 17:09:03 -04002985 // while transform feedback is active, (3.0.2, section 2.14, pg 86)
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002986 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04002987 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04002988 }
2989
Jiajia Qind9671222016-11-29 16:30:31 +08002990 return true;
2991}
2992
Jamie Madill9c9b40a2017-04-26 16:31:57 -04002993bool ValidateDrawElementsCommon(ValidationContext *context,
2994 GLenum mode,
2995 GLsizei count,
2996 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -04002997 const void *indices,
Jamie Madill9c9b40a2017-04-26 16:31:57 -04002998 GLsizei primcount)
Jiajia Qind9671222016-11-29 16:30:31 +08002999{
3000 if (!ValidateDrawElementsBase(context, type))
3001 return false;
3002
3003 const State &state = context->getGLState();
3004
Corentin Wallez170efbf2017-05-02 13:45:01 -04003005 if (!ValidateDrawBase(context, mode, count))
3006 {
3007 return false;
3008 }
3009
Jamie Madill250d33f2014-06-06 17:09:03 -04003010 // Check for mapped buffers
Jamie Madilld9ba4f72014-08-04 10:47:59 -04003011 if (state.hasMappedBuffer(GL_ELEMENT_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04003012 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003013 context->handleError(InvalidOperation() << "Index buffer is mapped.");
Geoff Langb1196682014-07-23 13:47:29 -04003014 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04003015 }
3016
He Yunchaoced53ae2016-11-29 15:00:51 +08003017 const gl::VertexArray *vao = state.getVertexArray();
Jamie Madill8e344942015-07-09 14:22:07 -04003018 gl::Buffer *elementArrayBuffer = vao->getElementArrayBuffer().get();
Jamie Madilld4cfa572014-07-08 10:00:32 -04003019
Corentin Wallez3f6d4df2017-01-30 18:04:36 -05003020 GLuint typeBytes = gl::GetTypeInfo(type).bytes;
3021
3022 if (context->getExtensions().webglCompatibility)
3023 {
3024 ASSERT(isPow2(typeBytes) && typeBytes > 0);
3025 if ((reinterpret_cast<uintptr_t>(indices) & static_cast<uintptr_t>(typeBytes - 1)) != 0)
3026 {
3027 // [WebGL 1.0] Section 6.4 Buffer Offset and Stride Requirements
3028 // The offset arguments to drawElements and [...], must be a multiple of the size of the
3029 // data type passed to the call, or an INVALID_OPERATION error is generated.
Brandon Jones6cad5662017-06-14 13:25:13 -07003030 ANGLE_VALIDATION_ERR(context, InvalidOperation(), OffsetMustBeMultipleOfType);
Corentin Wallez3f6d4df2017-01-30 18:04:36 -05003031 return false;
3032 }
Corentin Wallezfe9306a2017-02-01 17:41:05 -05003033
3034 // [WebGL 1.0] Section 6.4 Buffer Offset and Stride Requirements
3035 // In addition the offset argument to drawElements must be non-negative or an INVALID_VALUE
3036 // error is generated.
3037 if (reinterpret_cast<intptr_t>(indices) < 0)
3038 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003039 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeOffset);
Corentin Wallezfe9306a2017-02-01 17:41:05 -05003040 return false;
3041 }
Geoff Langfeb8c682017-02-13 16:07:35 -05003042 }
3043
3044 if (context->getExtensions().webglCompatibility ||
3045 !context->getGLState().areClientArraysEnabled())
3046 {
Corentin Wallez3f6d4df2017-01-30 18:04:36 -05003047 if (!elementArrayBuffer && count > 0)
3048 {
3049 // [WebGL 1.0] Section 6.2 No Client Side Arrays
3050 // If drawElements is called with a count greater than zero, and no WebGLBuffer is bound
3051 // to the ELEMENT_ARRAY_BUFFER binding point, an INVALID_OPERATION error is generated.
Brandon Jones6cad5662017-06-14 13:25:13 -07003052 ANGLE_VALIDATION_ERR(context, InvalidOperation(), MustHaveElementArrayBinding);
Corentin Wallez3f6d4df2017-01-30 18:04:36 -05003053 return false;
3054 }
3055 }
3056
Corentin Wallez0844f2d2017-01-31 17:02:59 -05003057 if (count > 0)
Jamie Madillae3000b2014-08-25 15:47:51 -04003058 {
Corentin Wallez0844f2d2017-01-31 17:02:59 -05003059 if (elementArrayBuffer)
Jamie Madillae3000b2014-08-25 15:47:51 -04003060 {
Corentin Wallezfe9306a2017-02-01 17:41:05 -05003061 // The max possible type size is 8 and count is on 32 bits so doing the multiplication
3062 // in a 64 bit integer is safe. Also we are guaranteed that here count > 0.
3063 static_assert(std::is_same<int, GLsizei>::value, "GLsizei isn't the expected type");
3064 constexpr uint64_t kMaxTypeSize = 8;
3065 constexpr uint64_t kIntMax = std::numeric_limits<int>::max();
3066 constexpr uint64_t kUint64Max = std::numeric_limits<uint64_t>::max();
3067 static_assert(kIntMax < kUint64Max / kMaxTypeSize, "");
Corentin Wallez0844f2d2017-01-31 17:02:59 -05003068
Corentin Wallezfe9306a2017-02-01 17:41:05 -05003069 uint64_t typeSize = typeBytes;
3070 uint64_t elementCount = static_cast<uint64_t>(count);
3071 ASSERT(elementCount > 0 && typeSize <= kMaxTypeSize);
3072
3073 // Doing the multiplication here is overflow-safe
3074 uint64_t elementDataSizeNoOffset = typeSize * elementCount;
3075
3076 // The offset can be any value, check for overflows
3077 uint64_t offset = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(indices));
3078 if (elementDataSizeNoOffset > kUint64Max - offset)
Corentin Wallez0844f2d2017-01-31 17:02:59 -05003079 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003080 ANGLE_VALIDATION_ERR(context, InvalidOperation(), IntegerOverflow);
Corentin Wallez0844f2d2017-01-31 17:02:59 -05003081 return false;
3082 }
3083
Corentin Wallezfe9306a2017-02-01 17:41:05 -05003084 uint64_t elementDataSizeWithOffset = elementDataSizeNoOffset + offset;
3085 if (elementDataSizeWithOffset > static_cast<uint64_t>(elementArrayBuffer->getSize()))
Corentin Wallez0844f2d2017-01-31 17:02:59 -05003086 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003087 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InsufficientBufferSize);
Corentin Wallez0844f2d2017-01-31 17:02:59 -05003088 return false;
3089 }
3090 }
3091 else if (!indices)
3092 {
3093 // This is an application error that would normally result in a crash,
3094 // but we catch it and return an error
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003095 context->handleError(InvalidOperation() << "No element array buffer and no pointer.");
Geoff Langb1196682014-07-23 13:47:29 -04003096 return false;
Jamie Madillae3000b2014-08-25 15:47:51 -04003097 }
Jamie Madillae3000b2014-08-25 15:47:51 -04003098 }
3099
Jamie Madill9c9b40a2017-04-26 16:31:57 -04003100 // Use the parameter buffer to retrieve and cache the index range.
Jamie Madill2b976812014-08-25 15:47:49 -04003101 // TODO: offer fast path, with disabled index validation.
3102 // TODO: also disable index checking on back-ends that are robust to out-of-range accesses.
Jamie Madill9c9b40a2017-04-26 16:31:57 -04003103 const auto &params = context->getParams<HasIndexRange>();
3104 const auto &indexRangeOpt = params.getIndexRange();
3105 if (!indexRangeOpt.valid())
Jamie Madill2b976812014-08-25 15:47:49 -04003106 {
Jamie Madill9c9b40a2017-04-26 16:31:57 -04003107 // Unexpected error.
3108 return false;
Jamie Madill2b976812014-08-25 15:47:49 -04003109 }
3110
Jamie Madille79b1e12015-11-04 16:36:37 -05003111 // If we use an index greater than our maximum supported index range, return an error.
3112 // The ES3 spec does not specify behaviour here, it is undefined, but ANGLE should always
3113 // return an error if possible here.
Jamie Madill9c9b40a2017-04-26 16:31:57 -04003114 if (static_cast<GLuint64>(indexRangeOpt.value().end) >= context->getCaps().maxElementIndex)
Jamie Madille79b1e12015-11-04 16:36:37 -05003115 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003116 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExceedsMaxElement);
Jamie Madille79b1e12015-11-04 16:36:37 -05003117 return false;
3118 }
3119
Jamie Madill9c9b40a2017-04-26 16:31:57 -04003120 if (!ValidateDrawAttribs(context, primcount, static_cast<GLint>(indexRangeOpt.value().end),
3121 static_cast<GLint>(indexRangeOpt.value().vertexCount())))
Jamie Madillfd716582014-06-06 17:09:04 -04003122 {
3123 return false;
3124 }
3125
Geoff Lang3edfe032015-09-04 16:38:24 -04003126 // No op if there are no real indices in the index data (all are primitive restart).
Jamie Madill9c9b40a2017-04-26 16:31:57 -04003127 return (indexRangeOpt.value().vertexIndexCount > 0);
Jamie Madillfd716582014-06-06 17:09:04 -04003128}
3129
Jamie Madill9c9b40a2017-04-26 16:31:57 -04003130bool ValidateDrawElementsInstancedCommon(ValidationContext *context,
3131 GLenum mode,
3132 GLsizei count,
3133 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -04003134 const void *indices,
Jamie Madill9c9b40a2017-04-26 16:31:57 -04003135 GLsizei primcount)
Jamie Madillfd716582014-06-06 17:09:04 -04003136{
Corentin Wallez0dc97812017-06-22 14:38:44 -04003137 return ValidateDrawElementsInstancedBase(context, mode, count, type, indices, primcount);
Jamie Madill250d33f2014-06-06 17:09:03 -04003138}
3139
Geoff Lang3edfe032015-09-04 16:38:24 -04003140bool ValidateDrawElementsInstancedANGLE(Context *context,
3141 GLenum mode,
3142 GLsizei count,
3143 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -04003144 const void *indices,
Jamie Madill9c9b40a2017-04-26 16:31:57 -04003145 GLsizei primcount)
Geoff Lang87a93302014-09-16 13:29:43 -04003146{
Corentin Wallez170efbf2017-05-02 13:45:01 -04003147 if (!ValidateDrawElementsInstancedBase(context, mode, count, type, indices, primcount))
Geoff Lang87a93302014-09-16 13:29:43 -04003148 {
3149 return false;
3150 }
3151
Corentin Wallez0dc97812017-06-22 14:38:44 -04003152 return ValidateDrawInstancedANGLE(context);
Geoff Lang87a93302014-09-16 13:29:43 -04003153}
3154
He Yunchaoced53ae2016-11-29 15:00:51 +08003155bool ValidateFramebufferTextureBase(Context *context,
3156 GLenum target,
3157 GLenum attachment,
3158 GLuint texture,
3159 GLint level)
Jamie Madill570f7c82014-07-03 10:38:54 -04003160{
Jamie Madill55ec3b12014-07-03 10:38:57 -04003161 if (!ValidFramebufferTarget(target))
3162 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003163 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidFramebufferTarget);
Geoff Langb1196682014-07-23 13:47:29 -04003164 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04003165 }
3166
3167 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill570f7c82014-07-03 10:38:54 -04003168 {
3169 return false;
3170 }
3171
Jamie Madill55ec3b12014-07-03 10:38:57 -04003172 if (texture != 0)
3173 {
3174 gl::Texture *tex = context->getTexture(texture);
3175
Jamie Madillbe849e42017-05-02 15:49:00 -04003176 if (tex == NULL)
Jamie Madill55ec3b12014-07-03 10:38:57 -04003177 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003178 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04003179 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04003180 }
3181
3182 if (level < 0)
3183 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003184 context->handleError(InvalidValue());
Geoff Langb1196682014-07-23 13:47:29 -04003185 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04003186 }
3187 }
3188
Jamie Madilldfde6ab2016-06-09 07:07:18 -07003189 const gl::Framebuffer *framebuffer = context->getGLState().getTargetFramebuffer(target);
Jamie Madill84115c92015-04-23 15:00:07 -04003190 ASSERT(framebuffer);
Jamie Madill55ec3b12014-07-03 10:38:57 -04003191
Jamie Madill84115c92015-04-23 15:00:07 -04003192 if (framebuffer->id() == 0)
Jamie Madill55ec3b12014-07-03 10:38:57 -04003193 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003194 ANGLE_VALIDATION_ERR(context, InvalidOperation(), DefaultFramebufferTarget);
Geoff Langb1196682014-07-23 13:47:29 -04003195 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04003196 }
3197
3198 return true;
3199}
3200
Geoff Langb1196682014-07-23 13:47:29 -04003201bool ValidateGetUniformBase(Context *context, GLuint program, GLint location)
Jamie Madill0063c512014-08-25 15:47:53 -04003202{
3203 if (program == 0)
3204 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003205 context->handleError(InvalidValue());
Geoff Langb1196682014-07-23 13:47:29 -04003206 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04003207 }
3208
Dian Xiang769769a2015-09-09 15:20:08 -07003209 gl::Program *programObject = GetValidProgram(context, program);
3210 if (!programObject)
Shannon Woods4de4fd62014-11-07 16:22:02 -05003211 {
3212 return false;
3213 }
3214
Jamie Madill0063c512014-08-25 15:47:53 -04003215 if (!programObject || !programObject->isLinked())
3216 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003217 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ProgramNotLinked);
Geoff Langb1196682014-07-23 13:47:29 -04003218 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04003219 }
3220
Geoff Lang7dd2e102014-11-10 15:19:26 -05003221 if (!programObject->isValidUniformLocation(location))
Jamie Madill549c7fd2014-08-25 15:47:56 -04003222 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003223 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04003224 return false;
Jamie Madill549c7fd2014-08-25 15:47:56 -04003225 }
3226
Jamie Madill0063c512014-08-25 15:47:53 -04003227 return true;
3228}
3229
Geoff Langf41d0ee2016-10-07 13:04:23 -04003230static bool ValidateSizedGetUniform(Context *context,
3231 GLuint program,
3232 GLint location,
3233 GLsizei bufSize,
3234 GLsizei *length)
Jamie Madill78f41802014-08-25 15:47:55 -04003235{
Geoff Langf41d0ee2016-10-07 13:04:23 -04003236 if (length)
3237 {
3238 *length = 0;
3239 }
3240
Jamie Madill78f41802014-08-25 15:47:55 -04003241 if (!ValidateGetUniformBase(context, program, location))
Jamie Madill0063c512014-08-25 15:47:53 -04003242 {
Jamie Madill78f41802014-08-25 15:47:55 -04003243 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04003244 }
3245
Geoff Langf41d0ee2016-10-07 13:04:23 -04003246 if (bufSize < 0)
3247 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003248 context->handleError(InvalidValue() << "bufSize cannot be negative.");
Geoff Langf41d0ee2016-10-07 13:04:23 -04003249 return false;
3250 }
3251
Jamie Madilla502c742014-08-28 17:19:13 -04003252 gl::Program *programObject = context->getProgram(program);
3253 ASSERT(programObject);
Jamie Madill0063c512014-08-25 15:47:53 -04003254
Jamie Madill78f41802014-08-25 15:47:55 -04003255 // sized queries -- ensure the provided buffer is large enough
Jamie Madill62d31cb2015-09-11 13:25:51 -04003256 const LinkedUniform &uniform = programObject->getUniformByLocation(location);
He Yunchaoced53ae2016-11-29 15:00:51 +08003257 size_t requiredBytes = VariableExternalSize(uniform.type);
Jamie Madill78f41802014-08-25 15:47:55 -04003258 if (static_cast<size_t>(bufSize) < requiredBytes)
Jamie Madill0063c512014-08-25 15:47:53 -04003259 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003260 context->handleError(InvalidOperation()
3261 << "bufSize of at least " << requiredBytes << " is required.");
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
He Yunchaoced53ae2016-11-29 15:00:51 +08003282bool ValidateGetnUniformivEXT(Context *context,
3283 GLuint program,
3284 GLint location,
3285 GLsizei bufSize,
3286 GLint *params)
Jamie Madill0063c512014-08-25 15:47:53 -04003287{
Geoff Langf41d0ee2016-10-07 13:04:23 -04003288 return ValidateSizedGetUniform(context, program, location, bufSize, nullptr);
3289}
3290
3291bool ValidateGetUniformfvRobustANGLE(Context *context,
3292 GLuint program,
3293 GLint location,
3294 GLsizei bufSize,
3295 GLsizei *length,
3296 GLfloat *params)
3297{
3298 if (!ValidateRobustEntryPoint(context, bufSize))
3299 {
3300 return false;
3301 }
3302
3303 // bufSize is validated in ValidateSizedGetUniform
3304 return ValidateSizedGetUniform(context, program, location, bufSize, length);
3305}
3306
3307bool ValidateGetUniformivRobustANGLE(Context *context,
3308 GLuint program,
3309 GLint location,
3310 GLsizei bufSize,
3311 GLsizei *length,
3312 GLint *params)
3313{
3314 if (!ValidateRobustEntryPoint(context, bufSize))
3315 {
3316 return false;
3317 }
3318
3319 // bufSize is validated in ValidateSizedGetUniform
3320 return ValidateSizedGetUniform(context, program, location, bufSize, length);
3321}
3322
3323bool ValidateGetUniformuivRobustANGLE(Context *context,
3324 GLuint program,
3325 GLint location,
3326 GLsizei bufSize,
3327 GLsizei *length,
3328 GLuint *params)
3329{
3330 if (!ValidateRobustEntryPoint(context, bufSize))
3331 {
3332 return false;
3333 }
3334
3335 if (context->getClientMajorVersion() < 3)
3336 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003337 context->handleError(InvalidOperation() << "Entry point requires at least OpenGL ES 3.0.");
Geoff Langf41d0ee2016-10-07 13:04:23 -04003338 return false;
3339 }
3340
3341 // bufSize is validated in ValidateSizedGetUniform
3342 return ValidateSizedGetUniform(context, program, location, bufSize, length);
Jamie Madill0063c512014-08-25 15:47:53 -04003343}
3344
He Yunchaoced53ae2016-11-29 15:00:51 +08003345bool ValidateDiscardFramebufferBase(Context *context,
3346 GLenum target,
3347 GLsizei numAttachments,
3348 const GLenum *attachments,
3349 bool defaultFramebuffer)
Austin Kinross08332632015-05-05 13:35:47 -07003350{
3351 if (numAttachments < 0)
3352 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003353 context->handleError(InvalidValue() << "numAttachments must not be less than zero");
Austin Kinross08332632015-05-05 13:35:47 -07003354 return false;
3355 }
3356
3357 for (GLsizei i = 0; i < numAttachments; ++i)
3358 {
Olli Etuaho84c9f592016-03-09 14:37:25 +02003359 if (attachments[i] >= GL_COLOR_ATTACHMENT0 && attachments[i] <= GL_COLOR_ATTACHMENT31)
Austin Kinross08332632015-05-05 13:35:47 -07003360 {
3361 if (defaultFramebuffer)
3362 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003363 context->handleError(InvalidEnum()
3364 << "Invalid attachment when the default framebuffer is bound");
Austin Kinross08332632015-05-05 13:35:47 -07003365 return false;
3366 }
3367
3368 if (attachments[i] >= GL_COLOR_ATTACHMENT0 + context->getCaps().maxColorAttachments)
3369 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003370 context->handleError(InvalidOperation() << "Requested color attachment is "
3371 "greater than the maximum supported "
3372 "color attachments");
Austin Kinross08332632015-05-05 13:35:47 -07003373 return false;
3374 }
3375 }
3376 else
3377 {
3378 switch (attachments[i])
3379 {
He Yunchaoced53ae2016-11-29 15:00:51 +08003380 case GL_DEPTH_ATTACHMENT:
3381 case GL_STENCIL_ATTACHMENT:
3382 case GL_DEPTH_STENCIL_ATTACHMENT:
3383 if (defaultFramebuffer)
3384 {
3385 context->handleError(
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003386 InvalidEnum()
3387 << "Invalid attachment when the default framebuffer is bound");
He Yunchaoced53ae2016-11-29 15:00:51 +08003388 return false;
3389 }
3390 break;
3391 case GL_COLOR:
3392 case GL_DEPTH:
3393 case GL_STENCIL:
3394 if (!defaultFramebuffer)
3395 {
3396 context->handleError(
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003397 InvalidEnum()
3398 << "Invalid attachment when the default framebuffer is not bound");
He Yunchaoced53ae2016-11-29 15:00:51 +08003399 return false;
3400 }
3401 break;
3402 default:
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003403 context->handleError(InvalidEnum() << "Invalid attachment");
Austin Kinross08332632015-05-05 13:35:47 -07003404 return false;
Austin Kinross08332632015-05-05 13:35:47 -07003405 }
3406 }
3407 }
3408
3409 return true;
3410}
3411
Austin Kinross6ee1e782015-05-29 17:05:37 -07003412bool ValidateInsertEventMarkerEXT(Context *context, GLsizei length, const char *marker)
3413{
3414 // Note that debug marker calls must not set error state
3415
3416 if (length < 0)
3417 {
3418 return false;
3419 }
3420
3421 if (marker == nullptr)
3422 {
3423 return false;
3424 }
3425
3426 return true;
3427}
3428
3429bool ValidatePushGroupMarkerEXT(Context *context, GLsizei length, const char *marker)
3430{
3431 // Note that debug marker calls must not set error state
3432
3433 if (length < 0)
3434 {
3435 return false;
3436 }
3437
3438 if (length > 0 && marker == nullptr)
3439 {
3440 return false;
3441 }
3442
3443 return true;
3444}
3445
Geoff Langdcab33b2015-07-21 13:03:16 -04003446bool ValidateEGLImageTargetTexture2DOES(Context *context,
Geoff Langdcab33b2015-07-21 13:03:16 -04003447 GLenum target,
3448 egl::Image *image)
3449{
Geoff Langa8406172015-07-21 16:53:39 -04003450 if (!context->getExtensions().eglImage && !context->getExtensions().eglImageExternal)
3451 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003452 context->handleError(InvalidOperation());
Geoff Langa8406172015-07-21 16:53:39 -04003453 return false;
3454 }
3455
3456 switch (target)
3457 {
3458 case GL_TEXTURE_2D:
Geoff Langb66a9092016-05-16 15:59:14 -04003459 if (!context->getExtensions().eglImage)
3460 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003461 context->handleError(InvalidEnum()
3462 << "GL_TEXTURE_2D texture target requires GL_OES_EGL_image.");
Geoff Langb66a9092016-05-16 15:59:14 -04003463 }
3464 break;
3465
3466 case GL_TEXTURE_EXTERNAL_OES:
3467 if (!context->getExtensions().eglImageExternal)
3468 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003469 context->handleError(InvalidEnum() << "GL_TEXTURE_EXTERNAL_OES texture target "
3470 "requires GL_OES_EGL_image_external.");
Geoff Langb66a9092016-05-16 15:59:14 -04003471 }
Geoff Langa8406172015-07-21 16:53:39 -04003472 break;
3473
3474 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07003475 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTextureTarget);
Geoff Langa8406172015-07-21 16:53:39 -04003476 return false;
3477 }
3478
Jamie Madill61e16b42017-06-19 11:13:23 -04003479 ASSERT(context->getCurrentDisplay());
3480 if (!context->getCurrentDisplay()->isValidImage(image))
Geoff Langa8406172015-07-21 16:53:39 -04003481 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003482 context->handleError(InvalidValue() << "EGL image is not valid.");
Geoff Langa8406172015-07-21 16:53:39 -04003483 return false;
3484 }
3485
3486 if (image->getSamples() > 0)
3487 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003488 context->handleError(InvalidOperation()
3489 << "cannot create a 2D texture from a multisampled EGL image.");
Geoff Langa8406172015-07-21 16:53:39 -04003490 return false;
3491 }
3492
Geoff Langca271392017-04-05 12:30:00 -04003493 const TextureCaps &textureCaps =
3494 context->getTextureCaps().get(image->getFormat().info->sizedInternalFormat);
Geoff Langa8406172015-07-21 16:53:39 -04003495 if (!textureCaps.texturable)
3496 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003497 context->handleError(InvalidOperation()
3498 << "EGL image internal format is not supported as a texture.");
Geoff Langa8406172015-07-21 16:53:39 -04003499 return false;
3500 }
3501
Geoff Langdcab33b2015-07-21 13:03:16 -04003502 return true;
3503}
3504
3505bool ValidateEGLImageTargetRenderbufferStorageOES(Context *context,
Geoff Langdcab33b2015-07-21 13:03:16 -04003506 GLenum target,
3507 egl::Image *image)
3508{
Geoff Langa8406172015-07-21 16:53:39 -04003509 if (!context->getExtensions().eglImage)
3510 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003511 context->handleError(InvalidOperation());
Geoff Langa8406172015-07-21 16:53:39 -04003512 return false;
3513 }
3514
3515 switch (target)
3516 {
3517 case GL_RENDERBUFFER:
3518 break;
3519
3520 default:
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003521 context->handleError(InvalidEnum() << "invalid renderbuffer target.");
Geoff Langa8406172015-07-21 16:53:39 -04003522 return false;
3523 }
3524
Jamie Madill61e16b42017-06-19 11:13:23 -04003525 ASSERT(context->getCurrentDisplay());
3526 if (!context->getCurrentDisplay()->isValidImage(image))
Geoff Langa8406172015-07-21 16:53:39 -04003527 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003528 context->handleError(InvalidValue() << "EGL image is not valid.");
Geoff Langa8406172015-07-21 16:53:39 -04003529 return false;
3530 }
3531
Geoff Langca271392017-04-05 12:30:00 -04003532 const TextureCaps &textureCaps =
3533 context->getTextureCaps().get(image->getFormat().info->sizedInternalFormat);
Geoff Langa8406172015-07-21 16:53:39 -04003534 if (!textureCaps.renderable)
3535 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003536 context->handleError(InvalidOperation()
3537 << "EGL image internal format is not supported as a renderbuffer.");
Geoff Langa8406172015-07-21 16:53:39 -04003538 return false;
3539 }
3540
Geoff Langdcab33b2015-07-21 13:03:16 -04003541 return true;
3542}
Austin Kinrossbc781f32015-10-26 09:27:38 -07003543
3544bool ValidateBindVertexArrayBase(Context *context, GLuint array)
3545{
Geoff Lang36167ab2015-12-07 10:27:14 -05003546 if (!context->isVertexArrayGenerated(array))
Austin Kinrossbc781f32015-10-26 09:27:38 -07003547 {
3548 // The default VAO should always exist
3549 ASSERT(array != 0);
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003550 context->handleError(InvalidOperation());
Austin Kinrossbc781f32015-10-26 09:27:38 -07003551 return false;
3552 }
3553
3554 return true;
3555}
3556
Geoff Langc5629752015-12-07 16:29:04 -05003557bool ValidateProgramBinaryBase(Context *context,
3558 GLuint program,
3559 GLenum binaryFormat,
3560 const void *binary,
3561 GLint length)
3562{
3563 Program *programObject = GetValidProgram(context, program);
3564 if (programObject == nullptr)
3565 {
3566 return false;
3567 }
3568
3569 const std::vector<GLenum> &programBinaryFormats = context->getCaps().programBinaryFormats;
3570 if (std::find(programBinaryFormats.begin(), programBinaryFormats.end(), binaryFormat) ==
3571 programBinaryFormats.end())
3572 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003573 context->handleError(InvalidEnum() << "Program binary format is not valid.");
Geoff Langc5629752015-12-07 16:29:04 -05003574 return false;
3575 }
3576
Olli Etuahoc3e55a42016-03-09 16:29:18 +02003577 if (context->hasActiveTransformFeedback(program))
3578 {
3579 // ES 3.0.4 section 2.15 page 91
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003580 context->handleError(InvalidOperation() << "Cannot change program binary while program "
3581 "is associated with an active transform "
3582 "feedback object.");
Olli Etuahoc3e55a42016-03-09 16:29:18 +02003583 return false;
3584 }
3585
Geoff Langc5629752015-12-07 16:29:04 -05003586 return true;
3587}
3588
3589bool ValidateGetProgramBinaryBase(Context *context,
3590 GLuint program,
3591 GLsizei bufSize,
3592 GLsizei *length,
3593 GLenum *binaryFormat,
3594 void *binary)
3595{
3596 Program *programObject = GetValidProgram(context, program);
3597 if (programObject == nullptr)
3598 {
3599 return false;
3600 }
3601
3602 if (!programObject->isLinked())
3603 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003604 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ProgramNotLinked);
Geoff Langc5629752015-12-07 16:29:04 -05003605 return false;
3606 }
3607
Jamie Madilla7d12dc2016-12-13 15:08:19 -05003608 if (context->getCaps().programBinaryFormats.empty())
3609 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003610 context->handleError(InvalidOperation() << "No program binary formats supported.");
Jamie Madilla7d12dc2016-12-13 15:08:19 -05003611 return false;
3612 }
3613
Geoff Langc5629752015-12-07 16:29:04 -05003614 return true;
3615}
Jamie Madillc29968b2016-01-20 11:17:23 -05003616
Jamie Madillc29968b2016-01-20 11:17:23 -05003617bool ValidateDrawBuffersBase(ValidationContext *context, GLsizei n, const GLenum *bufs)
3618{
3619 // INVALID_VALUE is generated if n is negative or greater than value of MAX_DRAW_BUFFERS
3620 if (n < 0 || static_cast<GLuint>(n) > context->getCaps().maxDrawBuffers)
3621 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003622 context->handleError(InvalidValue()
3623 << "n must be non-negative and no greater than MAX_DRAW_BUFFERS");
Jamie Madillc29968b2016-01-20 11:17:23 -05003624 return false;
3625 }
3626
Jamie Madilldfde6ab2016-06-09 07:07:18 -07003627 ASSERT(context->getGLState().getDrawFramebuffer());
3628 GLuint frameBufferId = context->getGLState().getDrawFramebuffer()->id();
Jamie Madillc29968b2016-01-20 11:17:23 -05003629 GLuint maxColorAttachment = GL_COLOR_ATTACHMENT0_EXT + context->getCaps().maxColorAttachments;
3630
3631 // This should come first before the check for the default frame buffer
3632 // because when we switch to ES3.1+, invalid enums will return INVALID_ENUM
3633 // rather than INVALID_OPERATION
3634 for (int colorAttachment = 0; colorAttachment < n; colorAttachment++)
3635 {
3636 const GLenum attachment = GL_COLOR_ATTACHMENT0_EXT + colorAttachment;
3637
3638 if (bufs[colorAttachment] != GL_NONE && bufs[colorAttachment] != GL_BACK &&
Olli Etuaho84c9f592016-03-09 14:37:25 +02003639 (bufs[colorAttachment] < GL_COLOR_ATTACHMENT0 ||
3640 bufs[colorAttachment] > GL_COLOR_ATTACHMENT31))
Jamie Madillc29968b2016-01-20 11:17:23 -05003641 {
3642 // Value in bufs is not NONE, BACK, or GL_COLOR_ATTACHMENTi
Olli Etuaho84c9f592016-03-09 14:37:25 +02003643 // The 3.0.4 spec says to generate GL_INVALID_OPERATION here, but this
3644 // was changed to GL_INVALID_ENUM in 3.1, which dEQP also expects.
3645 // 3.1 is still a bit ambiguous about the error, but future specs are
3646 // expected to clarify that GL_INVALID_ENUM is the correct error.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003647 context->handleError(InvalidEnum() << "Invalid buffer value");
Olli Etuaho84c9f592016-03-09 14:37:25 +02003648 return false;
3649 }
3650 else if (bufs[colorAttachment] >= maxColorAttachment)
3651 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003652 context->handleError(InvalidOperation()
3653 << "Buffer value is greater than MAX_DRAW_BUFFERS");
Jamie Madillc29968b2016-01-20 11:17:23 -05003654 return false;
3655 }
3656 else if (bufs[colorAttachment] != GL_NONE && bufs[colorAttachment] != attachment &&
3657 frameBufferId != 0)
3658 {
3659 // INVALID_OPERATION-GL is bound to buffer and ith argument
3660 // is not COLOR_ATTACHMENTi or NONE
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003661 context->handleError(InvalidOperation()
3662 << "Ith value does not match COLOR_ATTACHMENTi or NONE");
Jamie Madillc29968b2016-01-20 11:17:23 -05003663 return false;
3664 }
3665 }
3666
3667 // INVALID_OPERATION is generated if GL is bound to the default framebuffer
3668 // and n is not 1 or bufs is bound to value other than BACK and NONE
3669 if (frameBufferId == 0)
3670 {
3671 if (n != 1)
3672 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003673 context->handleError(InvalidOperation()
3674 << "n must be 1 when GL is bound to the default framebuffer");
Jamie Madillc29968b2016-01-20 11:17:23 -05003675 return false;
3676 }
3677
3678 if (bufs[0] != GL_NONE && bufs[0] != GL_BACK)
3679 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003680 context->handleError(
3681 InvalidOperation()
3682 << "Only NONE or BACK are valid values when drawing to the default framebuffer");
Jamie Madillc29968b2016-01-20 11:17:23 -05003683 return false;
3684 }
3685 }
3686
3687 return true;
3688}
3689
Geoff Lang496c02d2016-10-20 11:38:11 -07003690bool ValidateGetBufferPointervBase(Context *context,
3691 GLenum target,
3692 GLenum pname,
3693 GLsizei *length,
3694 void **params)
Olli Etuaho4f667482016-03-30 15:56:35 +03003695{
Geoff Lang496c02d2016-10-20 11:38:11 -07003696 if (length)
3697 {
3698 *length = 0;
3699 }
3700
3701 if (context->getClientMajorVersion() < 3 && !context->getExtensions().mapBuffer)
3702 {
3703 context->handleError(
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003704 InvalidOperation()
3705 << "Context does not support OpenGL ES 3.0 or GL_OES_mapbuffer is not enabled.");
Geoff Lang496c02d2016-10-20 11:38:11 -07003706 return false;
3707 }
3708
Olli Etuaho4f667482016-03-30 15:56:35 +03003709 if (!ValidBufferTarget(context, target))
3710 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003711 context->handleError(InvalidEnum() << "Buffer target not valid: 0x" << std::hex
3712 << std::uppercase << target);
Olli Etuaho4f667482016-03-30 15:56:35 +03003713 return false;
3714 }
3715
Geoff Lang496c02d2016-10-20 11:38:11 -07003716 switch (pname)
Olli Etuaho4f667482016-03-30 15:56:35 +03003717 {
Geoff Lang496c02d2016-10-20 11:38:11 -07003718 case GL_BUFFER_MAP_POINTER:
3719 break;
Olli Etuaho4f667482016-03-30 15:56:35 +03003720
Geoff Lang496c02d2016-10-20 11:38:11 -07003721 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07003722 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Geoff Lang496c02d2016-10-20 11:38:11 -07003723 return false;
3724 }
Olli Etuaho4f667482016-03-30 15:56:35 +03003725
3726 // GLES 3.0 section 2.10.1: "Attempts to attempts to modify or query buffer object state for a
3727 // target bound to zero generate an INVALID_OPERATION error."
3728 // GLES 3.1 section 6.6 explicitly specifies this error.
Geoff Lang496c02d2016-10-20 11:38:11 -07003729 if (context->getGLState().getTargetBuffer(target) == nullptr)
Olli Etuaho4f667482016-03-30 15:56:35 +03003730 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003731 context->handleError(InvalidOperation()
3732 << "Can not get pointer for reserved buffer name zero.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003733 return false;
3734 }
3735
Geoff Lang496c02d2016-10-20 11:38:11 -07003736 if (length)
3737 {
3738 *length = 1;
3739 }
3740
Olli Etuaho4f667482016-03-30 15:56:35 +03003741 return true;
3742}
3743
3744bool ValidateUnmapBufferBase(Context *context, GLenum target)
3745{
3746 if (!ValidBufferTarget(context, target))
3747 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003748 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidBufferTypes);
Olli Etuaho4f667482016-03-30 15:56:35 +03003749 return false;
3750 }
3751
Jamie Madilldfde6ab2016-06-09 07:07:18 -07003752 Buffer *buffer = context->getGLState().getTargetBuffer(target);
Olli Etuaho4f667482016-03-30 15:56:35 +03003753
3754 if (buffer == nullptr || !buffer->isMapped())
3755 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003756 context->handleError(InvalidOperation() << "Buffer not mapped.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003757 return false;
3758 }
3759
3760 return true;
3761}
3762
3763bool ValidateMapBufferRangeBase(Context *context,
3764 GLenum target,
3765 GLintptr offset,
3766 GLsizeiptr length,
3767 GLbitfield access)
3768{
3769 if (!ValidBufferTarget(context, target))
3770 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003771 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidBufferTypes);
Olli Etuaho4f667482016-03-30 15:56:35 +03003772 return false;
3773 }
3774
Brandon Jones6cad5662017-06-14 13:25:13 -07003775 if (offset < 0)
Olli Etuaho4f667482016-03-30 15:56:35 +03003776 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003777 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeOffset);
3778 return false;
3779 }
3780
3781 if (length < 0)
3782 {
3783 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeLength);
Olli Etuaho4f667482016-03-30 15:56:35 +03003784 return false;
3785 }
3786
Jamie Madilldfde6ab2016-06-09 07:07:18 -07003787 Buffer *buffer = context->getGLState().getTargetBuffer(target);
Olli Etuaho4f667482016-03-30 15:56:35 +03003788
3789 if (!buffer)
3790 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003791 context->handleError(InvalidOperation() << "Attempted to map buffer object zero.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003792 return false;
3793 }
3794
3795 // Check for buffer overflow
Jamie Madille2e406c2016-06-02 13:04:10 -04003796 CheckedNumeric<size_t> checkedOffset(offset);
3797 auto checkedSize = checkedOffset + length;
Olli Etuaho4f667482016-03-30 15:56:35 +03003798
Jamie Madille2e406c2016-06-02 13:04:10 -04003799 if (!checkedSize.IsValid() || checkedSize.ValueOrDie() > static_cast<size_t>(buffer->getSize()))
Olli Etuaho4f667482016-03-30 15:56:35 +03003800 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003801 context->handleError(InvalidValue() << "Mapped range does not fit into buffer dimensions.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003802 return false;
3803 }
3804
3805 // Check for invalid bits in the mask
3806 GLbitfield allAccessBits = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT |
3807 GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_FLUSH_EXPLICIT_BIT |
3808 GL_MAP_UNSYNCHRONIZED_BIT;
3809
3810 if (access & ~(allAccessBits))
3811 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003812 context->handleError(InvalidValue()
3813 << "Invalid access bits: 0x" << std::hex << std::uppercase << access);
Olli Etuaho4f667482016-03-30 15:56:35 +03003814 return false;
3815 }
3816
3817 if (length == 0)
3818 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003819 context->handleError(InvalidOperation() << "Buffer mapping length is zero.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003820 return false;
3821 }
3822
3823 if (buffer->isMapped())
3824 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003825 context->handleError(InvalidOperation() << "Buffer is already mapped.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003826 return false;
3827 }
3828
3829 // Check for invalid bit combinations
3830 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0)
3831 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003832 context->handleError(InvalidOperation()
3833 << "Need to map buffer for either reading or writing.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003834 return false;
3835 }
3836
3837 GLbitfield writeOnlyBits =
3838 GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_UNSYNCHRONIZED_BIT;
3839
3840 if ((access & GL_MAP_READ_BIT) != 0 && (access & writeOnlyBits) != 0)
3841 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003842 context->handleError(InvalidOperation()
3843 << "Invalid access bits when mapping buffer for reading: 0x"
3844 << std::hex << std::uppercase << access);
Olli Etuaho4f667482016-03-30 15:56:35 +03003845 return false;
3846 }
3847
3848 if ((access & GL_MAP_WRITE_BIT) == 0 && (access & GL_MAP_FLUSH_EXPLICIT_BIT) != 0)
3849 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003850 context->handleError(
3851 InvalidOperation()
3852 << "The explicit flushing bit may only be set if the buffer is mapped for writing.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003853 return false;
3854 }
3855 return true;
3856}
3857
3858bool ValidateFlushMappedBufferRangeBase(Context *context,
3859 GLenum target,
3860 GLintptr offset,
3861 GLsizeiptr length)
3862{
Brandon Jones6cad5662017-06-14 13:25:13 -07003863 if (offset < 0)
Olli Etuaho4f667482016-03-30 15:56:35 +03003864 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003865 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeOffset);
3866 return false;
3867 }
3868
3869 if (length < 0)
3870 {
3871 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeLength);
Olli Etuaho4f667482016-03-30 15:56:35 +03003872 return false;
3873 }
3874
3875 if (!ValidBufferTarget(context, target))
3876 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003877 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidBufferTypes);
Olli Etuaho4f667482016-03-30 15:56:35 +03003878 return false;
3879 }
3880
Jamie Madilldfde6ab2016-06-09 07:07:18 -07003881 Buffer *buffer = context->getGLState().getTargetBuffer(target);
Olli Etuaho4f667482016-03-30 15:56:35 +03003882
3883 if (buffer == nullptr)
3884 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003885 context->handleError(InvalidOperation() << "Attempted to flush buffer object zero.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003886 return false;
3887 }
3888
3889 if (!buffer->isMapped() || (buffer->getAccessFlags() & GL_MAP_FLUSH_EXPLICIT_BIT) == 0)
3890 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003891 context->handleError(InvalidOperation()
3892 << "Attempted to flush a buffer not mapped for explicit flushing.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003893 return false;
3894 }
3895
3896 // Check for buffer overflow
Jamie Madille2e406c2016-06-02 13:04:10 -04003897 CheckedNumeric<size_t> checkedOffset(offset);
3898 auto checkedSize = checkedOffset + length;
Olli Etuaho4f667482016-03-30 15:56:35 +03003899
Jamie Madille2e406c2016-06-02 13:04:10 -04003900 if (!checkedSize.IsValid() ||
3901 checkedSize.ValueOrDie() > static_cast<size_t>(buffer->getMapLength()))
Olli Etuaho4f667482016-03-30 15:56:35 +03003902 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003903 context->handleError(InvalidValue()
3904 << "Flushed range does not fit into buffer mapping dimensions.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003905 return false;
3906 }
3907
3908 return true;
3909}
3910
Olli Etuaho41997e72016-03-10 13:38:39 +02003911bool ValidateGenOrDelete(Context *context, GLint n)
3912{
3913 if (n < 0)
3914 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003915 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeCount);
Olli Etuaho41997e72016-03-10 13:38:39 +02003916 return false;
3917 }
3918 return true;
3919}
3920
Geoff Langff5b2d52016-09-07 11:32:23 -04003921bool ValidateRobustEntryPoint(ValidationContext *context, GLsizei bufSize)
3922{
3923 if (!context->getExtensions().robustClientMemory)
3924 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003925 context->handleError(InvalidOperation()
3926 << "GL_ANGLE_robust_client_memory is not available.");
Geoff Langff5b2d52016-09-07 11:32:23 -04003927 return false;
3928 }
3929
3930 if (bufSize < 0)
3931 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003932 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeBufferSize);
Geoff Langff5b2d52016-09-07 11:32:23 -04003933 return false;
3934 }
3935
3936 return true;
3937}
3938
Geoff Lang2e43dbb2016-10-14 12:27:35 -04003939bool ValidateRobustBufferSize(ValidationContext *context, GLsizei bufSize, GLsizei numParams)
3940{
3941 if (bufSize < numParams)
3942 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003943 context->handleError(InvalidOperation() << numParams << " parameters are required but "
3944 << bufSize << " were provided.");
Geoff Lang2e43dbb2016-10-14 12:27:35 -04003945 return false;
3946 }
3947
3948 return true;
3949}
3950
Jamie Madillbe849e42017-05-02 15:49:00 -04003951bool ValidateGetFramebufferAttachmentParameterivBase(ValidationContext *context,
3952 GLenum target,
3953 GLenum attachment,
3954 GLenum pname,
3955 GLsizei *numParams)
Geoff Langff5b2d52016-09-07 11:32:23 -04003956{
Geoff Langff5b2d52016-09-07 11:32:23 -04003957 if (!ValidFramebufferTarget(target))
3958 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003959 context->handleError(InvalidEnum());
Geoff Langff5b2d52016-09-07 11:32:23 -04003960 return false;
3961 }
3962
3963 int clientVersion = context->getClientMajorVersion();
3964
3965 switch (pname)
3966 {
3967 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE:
3968 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME:
3969 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL:
3970 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE:
3971 break;
3972
Martin Radeve5285d22017-07-14 16:23:53 +03003973 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_NUM_VIEWS_ANGLE:
3974 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_MULTIVIEW_LAYOUT_ANGLE:
3975 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_BASE_VIEW_INDEX_ANGLE:
3976 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_VIEWPORT_OFFSETS_ANGLE:
3977 if (clientVersion < 3 || !context->getExtensions().multiview)
3978 {
3979 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
3980 return false;
3981 }
3982 break;
3983
Geoff Langff5b2d52016-09-07 11:32:23 -04003984 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
3985 if (clientVersion < 3 && !context->getExtensions().sRGB)
3986 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003987 context->handleError(InvalidEnum());
Geoff Langff5b2d52016-09-07 11:32:23 -04003988 return false;
3989 }
3990 break;
3991
3992 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
3993 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
3994 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
3995 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
3996 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
3997 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
3998 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
3999 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LAYER:
4000 if (clientVersion < 3)
4001 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004002 context->handleError(InvalidEnum());
Geoff Langff5b2d52016-09-07 11:32:23 -04004003 return false;
4004 }
4005 break;
4006
4007 default:
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004008 context->handleError(InvalidEnum());
Geoff Langff5b2d52016-09-07 11:32:23 -04004009 return false;
4010 }
4011
4012 // Determine if the attachment is a valid enum
4013 switch (attachment)
4014 {
4015 case GL_BACK:
4016 case GL_FRONT:
4017 case GL_DEPTH:
4018 case GL_STENCIL:
4019 case GL_DEPTH_STENCIL_ATTACHMENT:
4020 if (clientVersion < 3)
4021 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004022 context->handleError(InvalidEnum());
Geoff Langff5b2d52016-09-07 11:32:23 -04004023 return false;
4024 }
4025 break;
4026
4027 case GL_DEPTH_ATTACHMENT:
4028 case GL_STENCIL_ATTACHMENT:
4029 break;
4030
4031 default:
4032 if (attachment < GL_COLOR_ATTACHMENT0_EXT ||
4033 (attachment - GL_COLOR_ATTACHMENT0_EXT) >= context->getCaps().maxColorAttachments)
4034 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004035 context->handleError(InvalidEnum());
Geoff Langff5b2d52016-09-07 11:32:23 -04004036 return false;
4037 }
4038 break;
4039 }
4040
4041 const Framebuffer *framebuffer = context->getGLState().getTargetFramebuffer(target);
4042 ASSERT(framebuffer);
4043
4044 if (framebuffer->id() == 0)
4045 {
4046 if (clientVersion < 3)
4047 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004048 ANGLE_VALIDATION_ERR(context, InvalidOperation(), DefaultFramebufferTarget);
Geoff Langff5b2d52016-09-07 11:32:23 -04004049 return false;
4050 }
4051
4052 switch (attachment)
4053 {
4054 case GL_BACK:
4055 case GL_DEPTH:
4056 case GL_STENCIL:
4057 break;
4058
4059 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07004060 ANGLE_VALIDATION_ERR(context, InvalidOperation(), FramebufferInvalidAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004061 return false;
4062 }
4063 }
4064 else
4065 {
4066 if (attachment >= GL_COLOR_ATTACHMENT0_EXT && attachment <= GL_COLOR_ATTACHMENT15_EXT)
4067 {
4068 // Valid attachment query
4069 }
4070 else
4071 {
4072 switch (attachment)
4073 {
4074 case GL_DEPTH_ATTACHMENT:
4075 case GL_STENCIL_ATTACHMENT:
4076 break;
4077
4078 case GL_DEPTH_STENCIL_ATTACHMENT:
4079 if (!framebuffer->hasValidDepthStencil())
4080 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004081 context->handleError(InvalidOperation());
Geoff Langff5b2d52016-09-07 11:32:23 -04004082 return false;
4083 }
4084 break;
4085
4086 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07004087 ANGLE_VALIDATION_ERR(context, InvalidOperation(), FramebufferInvalidAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004088 return false;
4089 }
4090 }
4091 }
4092
4093 const FramebufferAttachment *attachmentObject = framebuffer->getAttachment(attachment);
4094 if (attachmentObject)
4095 {
4096 ASSERT(attachmentObject->type() == GL_RENDERBUFFER ||
4097 attachmentObject->type() == GL_TEXTURE ||
4098 attachmentObject->type() == GL_FRAMEBUFFER_DEFAULT);
4099
4100 switch (pname)
4101 {
4102 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME:
4103 if (attachmentObject->type() != GL_RENDERBUFFER &&
4104 attachmentObject->type() != GL_TEXTURE)
4105 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004106 ANGLE_VALIDATION_ERR(context, InvalidEnum(), FramebufferIncompleteAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004107 return false;
4108 }
4109 break;
4110
4111 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL:
4112 if (attachmentObject->type() != GL_TEXTURE)
4113 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004114 ANGLE_VALIDATION_ERR(context, InvalidEnum(), FramebufferIncompleteAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004115 return false;
4116 }
4117 break;
4118
4119 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE:
4120 if (attachmentObject->type() != GL_TEXTURE)
4121 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004122 ANGLE_VALIDATION_ERR(context, InvalidEnum(), FramebufferIncompleteAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004123 return false;
4124 }
4125 break;
4126
4127 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
4128 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT)
4129 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004130 ANGLE_VALIDATION_ERR(context, InvalidOperation(), FramebufferInvalidAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004131 return false;
4132 }
4133 break;
4134
4135 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LAYER:
4136 if (attachmentObject->type() != GL_TEXTURE)
4137 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004138 ANGLE_VALIDATION_ERR(context, InvalidEnum(), FramebufferIncompleteAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004139 return false;
4140 }
4141 break;
4142
4143 default:
4144 break;
4145 }
4146 }
4147 else
4148 {
4149 // ES 2.0.25 spec pg 127 states that if the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE
4150 // is NONE, then querying any other pname will generate INVALID_ENUM.
4151
4152 // ES 3.0.2 spec pg 235 states that if the attachment type is none,
4153 // GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME will return zero and be an
4154 // INVALID_OPERATION for all other pnames
4155
4156 switch (pname)
4157 {
4158 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE:
4159 break;
4160
4161 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME:
4162 if (clientVersion < 3)
4163 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004164 ANGLE_VALIDATION_ERR(context, InvalidEnum(),
4165 InvalidRenderbufferTextureParameter);
Geoff Langff5b2d52016-09-07 11:32:23 -04004166 return false;
4167 }
4168 break;
4169
4170 default:
4171 if (clientVersion < 3)
4172 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004173 ANGLE_VALIDATION_ERR(context, InvalidEnum(),
4174 InvalidRenderbufferTextureParameter);
Geoff Langff5b2d52016-09-07 11:32:23 -04004175 return false;
4176 }
4177 else
4178 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004179 ANGLE_VALIDATION_ERR(context, InvalidOperation(),
4180 InvalidRenderbufferTextureParameter);
Geoff Langff5b2d52016-09-07 11:32:23 -04004181 return false;
4182 }
4183 }
4184 }
4185
Martin Radeve5285d22017-07-14 16:23:53 +03004186 if (numParams)
4187 {
4188 if (pname == GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_VIEWPORT_OFFSETS_ANGLE)
4189 {
4190 // Only when the viewport offsets are queried we can have a varying number of output
4191 // parameters.
4192 const int numViews = attachmentObject ? attachmentObject->getNumViews() : 1;
4193 *numParams = numViews * 2;
4194 }
4195 else
4196 {
4197 // For all other queries we can have only one output parameter.
4198 *numParams = 1;
4199 }
4200 }
4201
Geoff Langff5b2d52016-09-07 11:32:23 -04004202 return true;
4203}
4204
4205bool ValidateGetFramebufferAttachmentParameterivRobustANGLE(ValidationContext *context,
4206 GLenum target,
4207 GLenum attachment,
4208 GLenum pname,
4209 GLsizei bufSize,
4210 GLsizei *numParams)
4211{
4212 if (!ValidateRobustEntryPoint(context, bufSize))
4213 {
4214 return false;
4215 }
4216
Jamie Madillbe849e42017-05-02 15:49:00 -04004217 if (!ValidateGetFramebufferAttachmentParameterivBase(context, target, attachment, pname,
4218 numParams))
Geoff Langff5b2d52016-09-07 11:32:23 -04004219 {
4220 return false;
4221 }
4222
4223 if (!ValidateRobustBufferSize(context, bufSize, *numParams))
4224 {
4225 return false;
4226 }
4227
4228 return true;
4229}
4230
Geoff Langff5b2d52016-09-07 11:32:23 -04004231bool ValidateGetBufferParameterivRobustANGLE(ValidationContext *context,
4232 GLenum target,
4233 GLenum pname,
4234 GLsizei bufSize,
Geoff Langebebe1c2016-10-14 12:01:31 -04004235 GLsizei *length,
4236 GLint *params)
Geoff Langff5b2d52016-09-07 11:32:23 -04004237{
4238 if (!ValidateRobustEntryPoint(context, bufSize))
4239 {
4240 return false;
4241 }
4242
Geoff Langebebe1c2016-10-14 12:01:31 -04004243 if (!ValidateGetBufferParameterBase(context, target, pname, false, length))
Geoff Langff5b2d52016-09-07 11:32:23 -04004244 {
4245 return false;
4246 }
4247
Geoff Langebebe1c2016-10-14 12:01:31 -04004248 if (!ValidateRobustBufferSize(context, bufSize, *length))
4249 {
4250 return false;
4251 }
4252
4253 return true;
4254}
4255
4256bool ValidateGetBufferParameteri64v(ValidationContext *context,
4257 GLenum target,
4258 GLenum pname,
4259 GLint64 *params)
4260{
4261 return ValidateGetBufferParameterBase(context, target, pname, false, nullptr);
4262}
4263
4264bool ValidateGetBufferParameteri64vRobustANGLE(ValidationContext *context,
4265 GLenum target,
4266 GLenum pname,
4267 GLsizei bufSize,
4268 GLsizei *length,
4269 GLint64 *params)
4270{
4271 if (!ValidateRobustEntryPoint(context, bufSize))
4272 {
4273 return false;
4274 }
4275
4276 if (!ValidateGetBufferParameterBase(context, target, pname, false, length))
4277 {
4278 return false;
4279 }
4280
4281 if (!ValidateRobustBufferSize(context, bufSize, *length))
Geoff Langff5b2d52016-09-07 11:32:23 -04004282 {
4283 return false;
4284 }
4285
4286 return true;
4287}
4288
Jamie Madillbe849e42017-05-02 15:49:00 -04004289bool ValidateGetProgramivBase(ValidationContext *context,
4290 GLuint program,
4291 GLenum pname,
4292 GLsizei *numParams)
Geoff Langff5b2d52016-09-07 11:32:23 -04004293{
4294 // Currently, all GetProgramiv queries return 1 parameter
Yunchao He33151a52017-04-13 09:58:17 +08004295 if (numParams)
4296 {
4297 *numParams = 1;
4298 }
Geoff Langff5b2d52016-09-07 11:32:23 -04004299
4300 Program *programObject = GetValidProgram(context, program);
4301 if (!programObject)
4302 {
4303 return false;
4304 }
4305
4306 switch (pname)
4307 {
4308 case GL_DELETE_STATUS:
4309 case GL_LINK_STATUS:
4310 case GL_VALIDATE_STATUS:
4311 case GL_INFO_LOG_LENGTH:
4312 case GL_ATTACHED_SHADERS:
4313 case GL_ACTIVE_ATTRIBUTES:
4314 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
4315 case GL_ACTIVE_UNIFORMS:
4316 case GL_ACTIVE_UNIFORM_MAX_LENGTH:
4317 break;
4318
4319 case GL_PROGRAM_BINARY_LENGTH:
4320 if (context->getClientMajorVersion() < 3 && !context->getExtensions().getProgramBinary)
4321 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004322 context->handleError(InvalidEnum() << "Querying GL_PROGRAM_BINARY_LENGTH "
4323 "requires GL_OES_get_program_binary or "
4324 "ES 3.0.");
Geoff Langff5b2d52016-09-07 11:32:23 -04004325 return false;
4326 }
4327 break;
4328
4329 case GL_ACTIVE_UNIFORM_BLOCKS:
4330 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH:
4331 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE:
4332 case GL_TRANSFORM_FEEDBACK_VARYINGS:
4333 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH:
4334 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT:
4335 if (context->getClientMajorVersion() < 3)
4336 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004337 context->handleError(InvalidEnum() << "Querying requires at least ES 3.0.");
Geoff Langff5b2d52016-09-07 11:32:23 -04004338 return false;
4339 }
4340 break;
4341
Yunchao He61afff12017-03-14 15:34:03 +08004342 case GL_PROGRAM_SEPARABLE:
4343 if (context->getClientVersion() < Version(3, 1))
4344 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004345 context->handleError(InvalidEnum() << "Querying requires at least ES 3.1.");
Yunchao He61afff12017-03-14 15:34:03 +08004346 return false;
4347 }
4348 break;
4349
Geoff Langff5b2d52016-09-07 11:32:23 -04004350 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07004351 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Geoff Langff5b2d52016-09-07 11:32:23 -04004352 return false;
4353 }
4354
4355 return true;
4356}
4357
4358bool ValidateGetProgramivRobustANGLE(Context *context,
4359 GLuint program,
4360 GLenum pname,
4361 GLsizei bufSize,
4362 GLsizei *numParams)
4363{
4364 if (!ValidateRobustEntryPoint(context, bufSize))
4365 {
4366 return false;
4367 }
4368
Jamie Madillbe849e42017-05-02 15:49:00 -04004369 if (!ValidateGetProgramivBase(context, program, pname, numParams))
Geoff Langff5b2d52016-09-07 11:32:23 -04004370 {
4371 return false;
4372 }
4373
4374 if (!ValidateRobustBufferSize(context, bufSize, *numParams))
4375 {
4376 return false;
4377 }
4378
4379 return true;
4380}
4381
Geoff Lang740d9022016-10-07 11:20:52 -04004382bool ValidateGetRenderbufferParameterivRobustANGLE(Context *context,
4383 GLenum target,
4384 GLenum pname,
4385 GLsizei bufSize,
4386 GLsizei *length,
4387 GLint *params)
4388{
4389 if (!ValidateRobustEntryPoint(context, bufSize))
4390 {
4391 return false;
4392 }
4393
4394 if (!ValidateGetRenderbufferParameterivBase(context, target, pname, length))
4395 {
4396 return false;
4397 }
4398
4399 if (!ValidateRobustBufferSize(context, bufSize, *length))
4400 {
4401 return false;
4402 }
4403
4404 return true;
4405}
4406
Geoff Langd7d0ed32016-10-07 11:33:51 -04004407bool ValidateGetShaderivRobustANGLE(Context *context,
4408 GLuint shader,
4409 GLenum pname,
4410 GLsizei bufSize,
4411 GLsizei *length,
4412 GLint *params)
4413{
4414 if (!ValidateRobustEntryPoint(context, bufSize))
4415 {
4416 return false;
4417 }
4418
4419 if (!ValidateGetShaderivBase(context, shader, pname, length))
4420 {
4421 return false;
4422 }
4423
4424 if (!ValidateRobustBufferSize(context, bufSize, *length))
4425 {
4426 return false;
4427 }
4428
4429 return true;
4430}
4431
Geoff Langc1984ed2016-10-07 12:41:00 -04004432bool ValidateGetTexParameterfvRobustANGLE(Context *context,
4433 GLenum target,
4434 GLenum pname,
4435 GLsizei bufSize,
4436 GLsizei *length,
4437 GLfloat *params)
4438{
4439 if (!ValidateRobustEntryPoint(context, bufSize))
4440 {
4441 return false;
4442 }
4443
4444 if (!ValidateGetTexParameterBase(context, target, pname, length))
4445 {
4446 return false;
4447 }
4448
4449 if (!ValidateRobustBufferSize(context, bufSize, *length))
4450 {
4451 return false;
4452 }
4453
4454 return true;
4455}
4456
Geoff Langc1984ed2016-10-07 12:41:00 -04004457bool ValidateGetTexParameterivRobustANGLE(Context *context,
4458 GLenum target,
4459 GLenum pname,
4460 GLsizei bufSize,
4461 GLsizei *length,
4462 GLint *params)
4463{
4464 if (!ValidateRobustEntryPoint(context, bufSize))
4465 {
4466 return false;
4467 }
4468
4469 if (!ValidateGetTexParameterBase(context, target, pname, length))
4470 {
4471 return false;
4472 }
4473
4474 if (!ValidateRobustBufferSize(context, bufSize, *length))
4475 {
4476 return false;
4477 }
4478
4479 return true;
4480}
4481
Geoff Langc1984ed2016-10-07 12:41:00 -04004482bool ValidateTexParameterfvRobustANGLE(Context *context,
4483 GLenum target,
4484 GLenum pname,
4485 GLsizei bufSize,
4486 const GLfloat *params)
4487{
4488 if (!ValidateRobustEntryPoint(context, bufSize))
4489 {
4490 return false;
4491 }
4492
4493 return ValidateTexParameterBase(context, target, pname, bufSize, params);
4494}
4495
Geoff Langc1984ed2016-10-07 12:41:00 -04004496bool ValidateTexParameterivRobustANGLE(Context *context,
4497 GLenum target,
4498 GLenum pname,
4499 GLsizei bufSize,
4500 const GLint *params)
4501{
4502 if (!ValidateRobustEntryPoint(context, bufSize))
4503 {
4504 return false;
4505 }
4506
4507 return ValidateTexParameterBase(context, target, pname, bufSize, params);
4508}
4509
4510bool ValidateGetSamplerParameterfv(Context *context, GLuint sampler, GLenum pname, GLfloat *params)
4511{
4512 return ValidateGetSamplerParameterBase(context, sampler, pname, nullptr);
4513}
4514
4515bool ValidateGetSamplerParameterfvRobustANGLE(Context *context,
4516 GLuint sampler,
4517 GLenum pname,
4518 GLuint bufSize,
4519 GLsizei *length,
4520 GLfloat *params)
4521{
4522 if (!ValidateRobustEntryPoint(context, bufSize))
4523 {
4524 return false;
4525 }
4526
4527 if (!ValidateGetSamplerParameterBase(context, sampler, pname, length))
4528 {
4529 return false;
4530 }
4531
4532 if (!ValidateRobustBufferSize(context, bufSize, *length))
4533 {
4534 return false;
4535 }
4536
4537 return true;
4538}
4539
4540bool ValidateGetSamplerParameteriv(Context *context, GLuint sampler, GLenum pname, GLint *params)
4541{
4542 return ValidateGetSamplerParameterBase(context, sampler, pname, nullptr);
4543}
4544
4545bool ValidateGetSamplerParameterivRobustANGLE(Context *context,
4546 GLuint sampler,
4547 GLenum pname,
4548 GLuint bufSize,
4549 GLsizei *length,
4550 GLint *params)
4551{
4552 if (!ValidateRobustEntryPoint(context, bufSize))
4553 {
4554 return false;
4555 }
4556
4557 if (!ValidateGetSamplerParameterBase(context, sampler, pname, length))
4558 {
4559 return false;
4560 }
4561
4562 if (!ValidateRobustBufferSize(context, bufSize, *length))
4563 {
4564 return false;
4565 }
4566
4567 return true;
4568}
4569
4570bool ValidateSamplerParameterf(Context *context, GLuint sampler, GLenum pname, GLfloat param)
4571{
4572 return ValidateSamplerParameterBase(context, sampler, pname, -1, &param);
4573}
4574
4575bool ValidateSamplerParameterfv(Context *context,
4576 GLuint sampler,
4577 GLenum pname,
4578 const GLfloat *params)
4579{
4580 return ValidateSamplerParameterBase(context, sampler, pname, -1, params);
4581}
4582
4583bool ValidateSamplerParameterfvRobustANGLE(Context *context,
4584 GLuint sampler,
4585 GLenum pname,
4586 GLsizei bufSize,
4587 const GLfloat *params)
4588{
4589 if (!ValidateRobustEntryPoint(context, bufSize))
4590 {
4591 return false;
4592 }
4593
4594 return ValidateSamplerParameterBase(context, sampler, pname, bufSize, params);
4595}
4596
4597bool ValidateSamplerParameteri(Context *context, GLuint sampler, GLenum pname, GLint param)
4598{
4599 return ValidateSamplerParameterBase(context, sampler, pname, -1, &param);
4600}
4601
4602bool ValidateSamplerParameteriv(Context *context, GLuint sampler, GLenum pname, const GLint *params)
4603{
4604 return ValidateSamplerParameterBase(context, sampler, pname, -1, params);
4605}
4606
4607bool ValidateSamplerParameterivRobustANGLE(Context *context,
4608 GLuint sampler,
4609 GLenum pname,
4610 GLsizei bufSize,
4611 const GLint *params)
4612{
4613 if (!ValidateRobustEntryPoint(context, bufSize))
4614 {
4615 return false;
4616 }
4617
4618 return ValidateSamplerParameterBase(context, sampler, pname, bufSize, params);
4619}
4620
Geoff Lang0b031062016-10-13 14:30:04 -04004621bool ValidateGetVertexAttribfvRobustANGLE(Context *context,
4622 GLuint index,
4623 GLenum pname,
4624 GLsizei bufSize,
4625 GLsizei *length,
4626 GLfloat *params)
4627{
4628 if (!ValidateRobustEntryPoint(context, bufSize))
4629 {
4630 return false;
4631 }
4632
4633 if (!ValidateGetVertexAttribBase(context, index, pname, length, false, false))
4634 {
4635 return false;
4636 }
4637
4638 if (!ValidateRobustBufferSize(context, bufSize, *length))
4639 {
4640 return false;
4641 }
4642
4643 return true;
4644}
4645
Geoff Lang0b031062016-10-13 14:30:04 -04004646bool ValidateGetVertexAttribivRobustANGLE(Context *context,
4647 GLuint index,
4648 GLenum pname,
4649 GLsizei bufSize,
4650 GLsizei *length,
4651 GLint *params)
4652{
4653 if (!ValidateRobustEntryPoint(context, bufSize))
4654 {
4655 return false;
4656 }
4657
4658 if (!ValidateGetVertexAttribBase(context, index, pname, length, false, false))
4659 {
4660 return false;
4661 }
4662
4663 if (!ValidateRobustBufferSize(context, bufSize, *length))
4664 {
4665 return false;
4666 }
4667
4668 return true;
4669}
4670
Geoff Lang0b031062016-10-13 14:30:04 -04004671bool ValidateGetVertexAttribPointervRobustANGLE(Context *context,
4672 GLuint index,
4673 GLenum pname,
4674 GLsizei bufSize,
4675 GLsizei *length,
4676 void **pointer)
4677{
4678 if (!ValidateRobustEntryPoint(context, bufSize))
4679 {
4680 return false;
4681 }
4682
4683 if (!ValidateGetVertexAttribBase(context, index, pname, length, true, false))
4684 {
4685 return false;
4686 }
4687
4688 if (!ValidateRobustBufferSize(context, bufSize, *length))
4689 {
4690 return false;
4691 }
4692
4693 return true;
4694}
4695
4696bool ValidateGetVertexAttribIiv(Context *context, GLuint index, GLenum pname, GLint *params)
4697{
4698 return ValidateGetVertexAttribBase(context, index, pname, nullptr, false, true);
4699}
4700
4701bool ValidateGetVertexAttribIivRobustANGLE(Context *context,
4702 GLuint index,
4703 GLenum pname,
4704 GLsizei bufSize,
4705 GLsizei *length,
4706 GLint *params)
4707{
4708 if (!ValidateRobustEntryPoint(context, bufSize))
4709 {
4710 return false;
4711 }
4712
4713 if (!ValidateGetVertexAttribBase(context, index, pname, length, false, true))
4714 {
4715 return false;
4716 }
4717
4718 if (!ValidateRobustBufferSize(context, bufSize, *length))
4719 {
4720 return false;
4721 }
4722
4723 return true;
4724}
4725
4726bool ValidateGetVertexAttribIuiv(Context *context, GLuint index, GLenum pname, GLuint *params)
4727{
4728 return ValidateGetVertexAttribBase(context, index, pname, nullptr, false, true);
4729}
4730
4731bool ValidateGetVertexAttribIuivRobustANGLE(Context *context,
4732 GLuint index,
4733 GLenum pname,
4734 GLsizei bufSize,
4735 GLsizei *length,
4736 GLuint *params)
4737{
4738 if (!ValidateRobustEntryPoint(context, bufSize))
4739 {
4740 return false;
4741 }
4742
4743 if (!ValidateGetVertexAttribBase(context, index, pname, length, false, true))
4744 {
4745 return false;
4746 }
4747
4748 if (!ValidateRobustBufferSize(context, bufSize, *length))
4749 {
4750 return false;
4751 }
4752
4753 return true;
4754}
4755
Geoff Lang6899b872016-10-14 11:30:13 -04004756bool ValidateGetActiveUniformBlockiv(Context *context,
4757 GLuint program,
4758 GLuint uniformBlockIndex,
4759 GLenum pname,
4760 GLint *params)
4761{
4762 return ValidateGetActiveUniformBlockivBase(context, program, uniformBlockIndex, pname, nullptr);
4763}
4764
4765bool ValidateGetActiveUniformBlockivRobustANGLE(Context *context,
4766 GLuint program,
4767 GLuint uniformBlockIndex,
4768 GLenum pname,
4769 GLsizei bufSize,
4770 GLsizei *length,
4771 GLint *params)
4772{
4773 if (!ValidateRobustEntryPoint(context, bufSize))
4774 {
4775 return false;
4776 }
4777
4778 if (!ValidateGetActiveUniformBlockivBase(context, program, uniformBlockIndex, pname, length))
4779 {
4780 return false;
4781 }
4782
4783 if (!ValidateRobustBufferSize(context, bufSize, *length))
4784 {
4785 return false;
4786 }
4787
4788 return true;
4789}
4790
Geoff Lang0a9661f2016-10-20 10:59:20 -07004791bool ValidateGetInternalFormativ(Context *context,
4792 GLenum target,
4793 GLenum internalformat,
4794 GLenum pname,
4795 GLsizei bufSize,
4796 GLint *params)
4797{
4798 return ValidateGetInternalFormativBase(context, target, internalformat, pname, bufSize,
4799 nullptr);
4800}
4801
4802bool ValidateGetInternalFormativRobustANGLE(Context *context,
4803 GLenum target,
4804 GLenum internalformat,
4805 GLenum pname,
4806 GLsizei bufSize,
4807 GLsizei *length,
4808 GLint *params)
4809{
4810 if (!ValidateRobustEntryPoint(context, bufSize))
4811 {
4812 return false;
4813 }
4814
4815 if (!ValidateGetInternalFormativBase(context, target, internalformat, pname, bufSize, length))
4816 {
4817 return false;
4818 }
4819
4820 if (!ValidateRobustBufferSize(context, bufSize, *length))
4821 {
4822 return false;
4823 }
4824
4825 return true;
4826}
4827
Shao80957d92017-02-20 21:25:59 +08004828bool ValidateVertexFormatBase(ValidationContext *context,
4829 GLuint attribIndex,
4830 GLint size,
4831 GLenum type,
4832 GLboolean pureInteger)
4833{
4834 const Caps &caps = context->getCaps();
4835 if (attribIndex >= caps.maxVertexAttributes)
4836 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004837 ANGLE_VALIDATION_ERR(context, InvalidValue(), IndexExceedsMax);
Shao80957d92017-02-20 21:25:59 +08004838 return false;
4839 }
4840
4841 if (size < 1 || size > 4)
4842 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004843 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidVertexAttrSize);
Geoff Lang8700a982017-06-13 10:15:13 -04004844 return false;
Shao80957d92017-02-20 21:25:59 +08004845 }
4846
4847 switch (type)
4848 {
4849 case GL_BYTE:
4850 case GL_UNSIGNED_BYTE:
4851 case GL_SHORT:
4852 case GL_UNSIGNED_SHORT:
4853 break;
4854
4855 case GL_INT:
4856 case GL_UNSIGNED_INT:
4857 if (context->getClientMajorVersion() < 3)
4858 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004859 context->handleError(InvalidEnum()
4860 << "Vertex type not supported before OpenGL ES 3.0.");
Shao80957d92017-02-20 21:25:59 +08004861 return false;
4862 }
4863 break;
4864
4865 case GL_FIXED:
4866 case GL_FLOAT:
4867 if (pureInteger)
4868 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004869 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTypePureInt);
Shao80957d92017-02-20 21:25:59 +08004870 return false;
4871 }
4872 break;
4873
4874 case GL_HALF_FLOAT:
4875 if (context->getClientMajorVersion() < 3)
4876 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004877 context->handleError(InvalidEnum()
4878 << "Vertex type not supported before OpenGL ES 3.0.");
Shao80957d92017-02-20 21:25:59 +08004879 return false;
4880 }
4881 if (pureInteger)
4882 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004883 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTypePureInt);
Shao80957d92017-02-20 21:25:59 +08004884 return false;
4885 }
4886 break;
4887
4888 case GL_INT_2_10_10_10_REV:
4889 case GL_UNSIGNED_INT_2_10_10_10_REV:
4890 if (context->getClientMajorVersion() < 3)
4891 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004892 context->handleError(InvalidEnum()
4893 << "Vertex type not supported before OpenGL ES 3.0.");
Shao80957d92017-02-20 21:25:59 +08004894 return false;
4895 }
4896 if (pureInteger)
4897 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004898 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTypePureInt);
Shao80957d92017-02-20 21:25:59 +08004899 return false;
4900 }
4901 if (size != 4)
4902 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004903 context->handleError(InvalidOperation() << "Type is INT_2_10_10_10_REV or "
4904 "UNSIGNED_INT_2_10_10_10_REV and "
4905 "size is not 4.");
Shao80957d92017-02-20 21:25:59 +08004906 return false;
4907 }
4908 break;
4909
4910 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07004911 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidType);
Shao80957d92017-02-20 21:25:59 +08004912 return false;
4913 }
4914
4915 return true;
4916}
4917
Geoff Lang76e65652017-03-27 14:58:02 -04004918// Perform validation from WebGL 2 section 5.10 "Invalid Clears":
4919// In the WebGL 2 API, trying to perform a clear when there is a mismatch between the type of the
4920// specified clear value and the type of a buffer that is being cleared generates an
4921// INVALID_OPERATION error instead of producing undefined results
4922bool ValidateWebGLFramebufferAttachmentClearType(ValidationContext *context,
4923 GLint drawbuffer,
4924 const GLenum *validComponentTypes,
4925 size_t validComponentTypeCount)
4926{
4927 const FramebufferAttachment *attachment =
4928 context->getGLState().getDrawFramebuffer()->getDrawBuffer(drawbuffer);
4929 if (attachment)
4930 {
4931 GLenum componentType = attachment->getFormat().info->componentType;
4932 const GLenum *end = validComponentTypes + validComponentTypeCount;
4933 if (std::find(validComponentTypes, end, componentType) == end)
4934 {
4935 context->handleError(
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004936 InvalidOperation()
4937 << "No defined conversion between clear value and attachment format.");
Geoff Lang76e65652017-03-27 14:58:02 -04004938 return false;
4939 }
4940 }
4941
4942 return true;
4943}
4944
Corentin Wallezb2931602017-04-11 15:58:57 -04004945bool ValidateRobustCompressedTexImageBase(ValidationContext *context,
4946 GLsizei imageSize,
4947 GLsizei dataSize)
4948{
4949 if (!ValidateRobustEntryPoint(context, dataSize))
4950 {
4951 return false;
4952 }
4953
4954 gl::Buffer *pixelUnpackBuffer = context->getGLState().getTargetBuffer(GL_PIXEL_UNPACK_BUFFER);
4955 if (pixelUnpackBuffer == nullptr)
4956 {
4957 if (dataSize < imageSize)
4958 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004959 context->handleError(InvalidOperation() << "dataSize must be at least " << imageSize);
Corentin Wallezb2931602017-04-11 15:58:57 -04004960 }
4961 }
4962 return true;
4963}
4964
Jamie Madillbe849e42017-05-02 15:49:00 -04004965bool ValidateGetBufferParameterBase(ValidationContext *context,
4966 GLenum target,
4967 GLenum pname,
4968 bool pointerVersion,
4969 GLsizei *numParams)
4970{
4971 if (numParams)
4972 {
4973 *numParams = 0;
4974 }
4975
4976 if (!ValidBufferTarget(context, target))
4977 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004978 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidBufferTypes);
Jamie Madillbe849e42017-05-02 15:49:00 -04004979 return false;
4980 }
4981
4982 const Buffer *buffer = context->getGLState().getTargetBuffer(target);
4983 if (!buffer)
4984 {
4985 // A null buffer means that "0" is bound to the requested buffer target
Brandon Jones6cad5662017-06-14 13:25:13 -07004986 ANGLE_VALIDATION_ERR(context, InvalidOperation(), BufferNotBound);
Jamie Madillbe849e42017-05-02 15:49:00 -04004987 return false;
4988 }
4989
4990 const Extensions &extensions = context->getExtensions();
4991
4992 switch (pname)
4993 {
4994 case GL_BUFFER_USAGE:
4995 case GL_BUFFER_SIZE:
4996 break;
4997
4998 case GL_BUFFER_ACCESS_OES:
4999 if (!extensions.mapBuffer)
5000 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005001 context->handleError(InvalidEnum()
5002 << "pname requires OpenGL ES 3.0 or GL_OES_mapbuffer.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005003 return false;
5004 }
5005 break;
5006
5007 case GL_BUFFER_MAPPED:
5008 static_assert(GL_BUFFER_MAPPED == GL_BUFFER_MAPPED_OES, "GL enums should be equal.");
5009 if (context->getClientMajorVersion() < 3 && !extensions.mapBuffer &&
5010 !extensions.mapBufferRange)
5011 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005012 context->handleError(InvalidEnum() << "pname requires OpenGL ES 3.0, "
5013 "GL_OES_mapbuffer or "
5014 "GL_EXT_map_buffer_range.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005015 return false;
5016 }
5017 break;
5018
5019 case GL_BUFFER_MAP_POINTER:
5020 if (!pointerVersion)
5021 {
5022 context->handleError(
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005023 InvalidEnum()
5024 << "GL_BUFFER_MAP_POINTER can only be queried with GetBufferPointerv.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005025 return false;
5026 }
5027 break;
5028
5029 case GL_BUFFER_ACCESS_FLAGS:
5030 case GL_BUFFER_MAP_OFFSET:
5031 case GL_BUFFER_MAP_LENGTH:
5032 if (context->getClientMajorVersion() < 3 && !extensions.mapBufferRange)
5033 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005034 context->handleError(InvalidEnum()
5035 << "pname requires OpenGL ES 3.0 or GL_EXT_map_buffer_range.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005036 return false;
5037 }
5038 break;
5039
5040 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005041 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005042 return false;
5043 }
5044
5045 // All buffer parameter queries return one value.
5046 if (numParams)
5047 {
5048 *numParams = 1;
5049 }
5050
5051 return true;
5052}
5053
5054bool ValidateGetRenderbufferParameterivBase(Context *context,
5055 GLenum target,
5056 GLenum pname,
5057 GLsizei *length)
5058{
5059 if (length)
5060 {
5061 *length = 0;
5062 }
5063
5064 if (target != GL_RENDERBUFFER)
5065 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005066 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidRenderbufferTarget);
Jamie Madillbe849e42017-05-02 15:49:00 -04005067 return false;
5068 }
5069
5070 Renderbuffer *renderbuffer = context->getGLState().getCurrentRenderbuffer();
5071 if (renderbuffer == nullptr)
5072 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005073 ANGLE_VALIDATION_ERR(context, InvalidOperation(), RenderbufferNotBound);
Jamie Madillbe849e42017-05-02 15:49:00 -04005074 return false;
5075 }
5076
5077 switch (pname)
5078 {
5079 case GL_RENDERBUFFER_WIDTH:
5080 case GL_RENDERBUFFER_HEIGHT:
5081 case GL_RENDERBUFFER_INTERNAL_FORMAT:
5082 case GL_RENDERBUFFER_RED_SIZE:
5083 case GL_RENDERBUFFER_GREEN_SIZE:
5084 case GL_RENDERBUFFER_BLUE_SIZE:
5085 case GL_RENDERBUFFER_ALPHA_SIZE:
5086 case GL_RENDERBUFFER_DEPTH_SIZE:
5087 case GL_RENDERBUFFER_STENCIL_SIZE:
5088 break;
5089
5090 case GL_RENDERBUFFER_SAMPLES_ANGLE:
5091 if (!context->getExtensions().framebufferMultisample)
5092 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005093 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ExtensionNotEnabled);
Jamie Madillbe849e42017-05-02 15:49:00 -04005094 return false;
5095 }
5096 break;
5097
5098 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005099 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005100 return false;
5101 }
5102
5103 if (length)
5104 {
5105 *length = 1;
5106 }
5107 return true;
5108}
5109
5110bool ValidateGetShaderivBase(Context *context, GLuint shader, GLenum pname, GLsizei *length)
5111{
5112 if (length)
5113 {
5114 *length = 0;
5115 }
5116
5117 if (GetValidShader(context, shader) == nullptr)
5118 {
5119 return false;
5120 }
5121
5122 switch (pname)
5123 {
5124 case GL_SHADER_TYPE:
5125 case GL_DELETE_STATUS:
5126 case GL_COMPILE_STATUS:
5127 case GL_INFO_LOG_LENGTH:
5128 case GL_SHADER_SOURCE_LENGTH:
5129 break;
5130
5131 case GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE:
5132 if (!context->getExtensions().translatedShaderSource)
5133 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005134 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ExtensionNotEnabled);
Jamie Madillbe849e42017-05-02 15:49:00 -04005135 return false;
5136 }
5137 break;
5138
5139 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005140 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005141 return false;
5142 }
5143
5144 if (length)
5145 {
5146 *length = 1;
5147 }
5148 return true;
5149}
5150
5151bool ValidateGetTexParameterBase(Context *context, GLenum target, GLenum pname, GLsizei *length)
5152{
5153 if (length)
5154 {
5155 *length = 0;
5156 }
5157
5158 if (!ValidTextureTarget(context, target) && !ValidTextureExternalTarget(context, target))
5159 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005160 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTextureTarget);
Jamie Madillbe849e42017-05-02 15:49:00 -04005161 return false;
5162 }
5163
5164 if (context->getTargetTexture(target) == nullptr)
5165 {
5166 // Should only be possible for external textures
Brandon Jones6cad5662017-06-14 13:25:13 -07005167 ANGLE_VALIDATION_ERR(context, InvalidEnum(), TextureNotBound);
Jamie Madillbe849e42017-05-02 15:49:00 -04005168 return false;
5169 }
5170
5171 switch (pname)
5172 {
5173 case GL_TEXTURE_MAG_FILTER:
5174 case GL_TEXTURE_MIN_FILTER:
5175 case GL_TEXTURE_WRAP_S:
5176 case GL_TEXTURE_WRAP_T:
5177 break;
5178
5179 case GL_TEXTURE_USAGE_ANGLE:
5180 if (!context->getExtensions().textureUsage)
5181 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005182 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ExtensionNotEnabled);
Jamie Madillbe849e42017-05-02 15:49:00 -04005183 return false;
5184 }
5185 break;
5186
5187 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
5188 if (!context->getExtensions().textureFilterAnisotropic)
5189 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005190 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ExtensionNotEnabled);
Jamie Madillbe849e42017-05-02 15:49:00 -04005191 return false;
5192 }
5193 break;
5194
5195 case GL_TEXTURE_IMMUTABLE_FORMAT:
5196 if (context->getClientMajorVersion() < 3 && !context->getExtensions().textureStorage)
5197 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005198 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ExtensionNotEnabled);
Jamie Madillbe849e42017-05-02 15:49:00 -04005199 return false;
5200 }
5201 break;
5202
5203 case GL_TEXTURE_WRAP_R:
5204 case GL_TEXTURE_IMMUTABLE_LEVELS:
5205 case GL_TEXTURE_SWIZZLE_R:
5206 case GL_TEXTURE_SWIZZLE_G:
5207 case GL_TEXTURE_SWIZZLE_B:
5208 case GL_TEXTURE_SWIZZLE_A:
5209 case GL_TEXTURE_BASE_LEVEL:
5210 case GL_TEXTURE_MAX_LEVEL:
5211 case GL_TEXTURE_MIN_LOD:
5212 case GL_TEXTURE_MAX_LOD:
5213 case GL_TEXTURE_COMPARE_MODE:
5214 case GL_TEXTURE_COMPARE_FUNC:
5215 if (context->getClientMajorVersion() < 3)
5216 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005217 context->handleError(InvalidEnum() << "pname requires OpenGL ES 3.0.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005218 return false;
5219 }
5220 break;
5221
5222 case GL_TEXTURE_SRGB_DECODE_EXT:
5223 if (!context->getExtensions().textureSRGBDecode)
5224 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005225 context->handleError(InvalidEnum() << "GL_EXT_texture_sRGB_decode is not enabled.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005226 return false;
5227 }
5228 break;
5229
5230 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005231 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005232 return false;
5233 }
5234
5235 if (length)
5236 {
5237 *length = 1;
5238 }
5239 return true;
5240}
5241
5242bool ValidateGetVertexAttribBase(Context *context,
5243 GLuint index,
5244 GLenum pname,
5245 GLsizei *length,
5246 bool pointer,
5247 bool pureIntegerEntryPoint)
5248{
5249 if (length)
5250 {
5251 *length = 0;
5252 }
5253
5254 if (pureIntegerEntryPoint && context->getClientMajorVersion() < 3)
5255 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005256 context->handleError(InvalidOperation() << "Context does not support OpenGL ES 3.0.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005257 return false;
5258 }
5259
5260 if (index >= context->getCaps().maxVertexAttributes)
5261 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005262 ANGLE_VALIDATION_ERR(context, InvalidValue(), IndexExceedsMax);
Jamie Madillbe849e42017-05-02 15:49:00 -04005263 return false;
5264 }
5265
5266 if (pointer)
5267 {
5268 if (pname != GL_VERTEX_ATTRIB_ARRAY_POINTER)
5269 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005270 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005271 return false;
5272 }
5273 }
5274 else
5275 {
5276 switch (pname)
5277 {
5278 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
5279 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
5280 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
5281 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
5282 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
5283 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
5284 case GL_CURRENT_VERTEX_ATTRIB:
5285 break;
5286
5287 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR:
5288 static_assert(
5289 GL_VERTEX_ATTRIB_ARRAY_DIVISOR == GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE,
5290 "ANGLE extension enums not equal to GL enums.");
5291 if (context->getClientMajorVersion() < 3 &&
5292 !context->getExtensions().instancedArrays)
5293 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005294 context->handleError(InvalidEnum() << "GL_VERTEX_ATTRIB_ARRAY_DIVISOR "
5295 "requires OpenGL ES 3.0 or "
5296 "GL_ANGLE_instanced_arrays.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005297 return false;
5298 }
5299 break;
5300
5301 case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
5302 if (context->getClientMajorVersion() < 3)
5303 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005304 context->handleError(
5305 InvalidEnum() << "GL_VERTEX_ATTRIB_ARRAY_INTEGER requires OpenGL ES 3.0.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005306 return false;
5307 }
5308 break;
5309
5310 case GL_VERTEX_ATTRIB_BINDING:
5311 case GL_VERTEX_ATTRIB_RELATIVE_OFFSET:
5312 if (context->getClientVersion() < ES_3_1)
5313 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005314 context->handleError(InvalidEnum()
5315 << "Vertex Attrib Bindings require OpenGL ES 3.1.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005316 return false;
5317 }
5318 break;
5319
5320 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005321 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005322 return false;
5323 }
5324 }
5325
5326 if (length)
5327 {
5328 if (pname == GL_CURRENT_VERTEX_ATTRIB)
5329 {
5330 *length = 4;
5331 }
5332 else
5333 {
5334 *length = 1;
5335 }
5336 }
5337
5338 return true;
5339}
5340
Jamie Madill4928b7c2017-06-20 12:57:39 -04005341bool ValidateReadPixelsBase(Context *context,
Jamie Madillbe849e42017-05-02 15:49:00 -04005342 GLint x,
5343 GLint y,
5344 GLsizei width,
5345 GLsizei height,
5346 GLenum format,
5347 GLenum type,
5348 GLsizei bufSize,
5349 GLsizei *length,
5350 GLsizei *columns,
5351 GLsizei *rows,
5352 void *pixels)
5353{
5354 if (length != nullptr)
5355 {
5356 *length = 0;
5357 }
5358 if (rows != nullptr)
5359 {
5360 *rows = 0;
5361 }
5362 if (columns != nullptr)
5363 {
5364 *columns = 0;
5365 }
5366
5367 if (width < 0 || height < 0)
5368 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005369 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeSize);
Jamie Madillbe849e42017-05-02 15:49:00 -04005370 return false;
5371 }
5372
5373 auto readFramebuffer = context->getGLState().getReadFramebuffer();
5374
5375 if (readFramebuffer->checkStatus(context) != GL_FRAMEBUFFER_COMPLETE)
5376 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005377 context->handleError(InvalidFramebufferOperation());
Jamie Madillbe849e42017-05-02 15:49:00 -04005378 return false;
5379 }
5380
5381 if (readFramebuffer->id() != 0 && readFramebuffer->getSamples(context) != 0)
5382 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005383 context->handleError(InvalidOperation());
Jamie Madillbe849e42017-05-02 15:49:00 -04005384 return false;
5385 }
5386
5387 const Framebuffer *framebuffer = context->getGLState().getReadFramebuffer();
5388 ASSERT(framebuffer);
5389
5390 if (framebuffer->getReadBufferState() == GL_NONE)
5391 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005392 context->handleError(InvalidOperation() << "Read buffer is GL_NONE");
Jamie Madillbe849e42017-05-02 15:49:00 -04005393 return false;
5394 }
5395
5396 const FramebufferAttachment *readBuffer = framebuffer->getReadColorbuffer();
5397 // WebGL 1.0 [Section 6.26] Reading From a Missing Attachment
5398 // In OpenGL ES it is undefined what happens when an operation tries to read from a missing
5399 // attachment and WebGL defines it to be an error. We do the check unconditionnaly as the
5400 // situation is an application error that would lead to a crash in ANGLE.
5401 if (readBuffer == nullptr)
5402 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005403 ANGLE_VALIDATION_ERR(context, InvalidOperation(), MissingReadAttachment);
Jamie Madillbe849e42017-05-02 15:49:00 -04005404 return false;
5405 }
5406
Geoff Lang280ba992017-04-18 16:30:58 -04005407 if (context->getExtensions().webglCompatibility)
5408 {
5409 // The ES 2.0 spec states that the format must be "among those defined in table 3.4,
5410 // excluding formats LUMINANCE and LUMINANCE_ALPHA.". This requires validating the format
5411 // and type before validating the combination of format and type. However, the
5412 // dEQP-GLES3.functional.negative_api.buffer.read_pixels passes GL_LUMINANCE as a format and
5413 // verifies that GL_INVALID_OPERATION is generated.
5414 // TODO(geofflang): Update this check to be done in all/no cases once this is resolved in
5415 // dEQP/WebGL.
5416 if (!ValidReadPixelsFormatEnum(context, format))
5417 {
5418 context->handleError(InvalidEnum() << "Invalid read format.");
5419 return false;
5420 }
5421
5422 if (!ValidReadPixelsTypeEnum(context, type))
5423 {
5424 context->handleError(InvalidEnum() << "Invalid read type.");
5425 return false;
5426 }
5427 }
5428
Jamie Madill4928b7c2017-06-20 12:57:39 -04005429 GLenum currentFormat = framebuffer->getImplementationColorReadFormat(context);
5430 GLenum currentType = framebuffer->getImplementationColorReadType(context);
Jamie Madillbe849e42017-05-02 15:49:00 -04005431 GLenum currentComponentType = readBuffer->getFormat().info->componentType;
5432
5433 bool validFormatTypeCombination =
5434 ValidReadPixelsFormatType(context, currentComponentType, format, type);
5435
5436 if (!(currentFormat == format && currentType == type) && !validFormatTypeCombination)
5437 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005438 ANGLE_VALIDATION_ERR(context, InvalidOperation(), MismatchedTypeAndFormat);
Jamie Madillbe849e42017-05-02 15:49:00 -04005439 return false;
5440 }
5441
5442 // Check for pixel pack buffer related API errors
5443 gl::Buffer *pixelPackBuffer = context->getGLState().getTargetBuffer(GL_PIXEL_PACK_BUFFER);
5444 if (pixelPackBuffer != nullptr && pixelPackBuffer->isMapped())
5445 {
5446 // ...the buffer object's data store is currently mapped.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005447 context->handleError(InvalidOperation() << "Pixel pack buffer is mapped.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005448 return false;
5449 }
5450
5451 // .. the data would be packed to the buffer object such that the memory writes required
5452 // would exceed the data store size.
5453 const InternalFormat &formatInfo = GetInternalFormatInfo(format, type);
5454 const gl::Extents size(width, height, 1);
5455 const auto &pack = context->getGLState().getPackState();
5456
5457 auto endByteOrErr = formatInfo.computePackUnpackEndByte(type, size, pack, false);
5458 if (endByteOrErr.isError())
5459 {
5460 context->handleError(endByteOrErr.getError());
5461 return false;
5462 }
5463
5464 size_t endByte = endByteOrErr.getResult();
5465 if (bufSize >= 0)
5466 {
5467 if (pixelPackBuffer == nullptr && static_cast<size_t>(bufSize) < endByte)
5468 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005469 context->handleError(InvalidOperation()
5470 << "bufSize must be at least " << endByte << " bytes.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005471 return false;
5472 }
5473 }
5474
5475 if (pixelPackBuffer != nullptr)
5476 {
5477 CheckedNumeric<size_t> checkedEndByte(endByte);
5478 CheckedNumeric<size_t> checkedOffset(reinterpret_cast<size_t>(pixels));
5479 checkedEndByte += checkedOffset;
5480
5481 if (checkedEndByte.ValueOrDie() > static_cast<size_t>(pixelPackBuffer->getSize()))
5482 {
5483 // Overflow past the end of the buffer
Brandon Jones6cad5662017-06-14 13:25:13 -07005484 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ParamOverflow);
Jamie Madillbe849e42017-05-02 15:49:00 -04005485 return false;
5486 }
5487 }
5488
5489 if (pixelPackBuffer == nullptr && length != nullptr)
5490 {
5491 if (endByte > static_cast<size_t>(std::numeric_limits<GLsizei>::max()))
5492 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005493 ANGLE_VALIDATION_ERR(context, InvalidOperation(), IntegerOverflow);
Jamie Madillbe849e42017-05-02 15:49:00 -04005494 return false;
5495 }
5496
5497 *length = static_cast<GLsizei>(endByte);
5498 }
5499
5500 auto getClippedExtent = [](GLint start, GLsizei length, int bufferSize) {
5501 angle::CheckedNumeric<int> clippedExtent(length);
5502 if (start < 0)
5503 {
5504 // "subtract" the area that is less than 0
5505 clippedExtent += start;
5506 }
5507
5508 const int readExtent = start + length;
5509 if (readExtent > bufferSize)
5510 {
5511 // Subtract the region to the right of the read buffer
5512 clippedExtent -= (readExtent - bufferSize);
5513 }
5514
5515 if (!clippedExtent.IsValid())
5516 {
5517 return 0;
5518 }
5519
5520 return std::max(clippedExtent.ValueOrDie(), 0);
5521 };
5522
5523 if (columns != nullptr)
5524 {
5525 *columns = getClippedExtent(x, width, readBuffer->getSize().width);
5526 }
5527
5528 if (rows != nullptr)
5529 {
5530 *rows = getClippedExtent(y, height, readBuffer->getSize().height);
5531 }
5532
5533 return true;
5534}
5535
5536template <typename ParamType>
5537bool ValidateTexParameterBase(Context *context,
5538 GLenum target,
5539 GLenum pname,
5540 GLsizei bufSize,
5541 const ParamType *params)
5542{
5543 if (!ValidTextureTarget(context, target) && !ValidTextureExternalTarget(context, target))
5544 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005545 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTextureTarget);
Jamie Madillbe849e42017-05-02 15:49:00 -04005546 return false;
5547 }
5548
5549 if (context->getTargetTexture(target) == nullptr)
5550 {
5551 // Should only be possible for external textures
Brandon Jones6cad5662017-06-14 13:25:13 -07005552 ANGLE_VALIDATION_ERR(context, InvalidEnum(), TextureNotBound);
Jamie Madillbe849e42017-05-02 15:49:00 -04005553 return false;
5554 }
5555
5556 const GLsizei minBufSize = 1;
5557 if (bufSize >= 0 && bufSize < minBufSize)
5558 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005559 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InsufficientBufferSize);
Jamie Madillbe849e42017-05-02 15:49:00 -04005560 return false;
5561 }
5562
5563 switch (pname)
5564 {
5565 case GL_TEXTURE_WRAP_R:
5566 case GL_TEXTURE_SWIZZLE_R:
5567 case GL_TEXTURE_SWIZZLE_G:
5568 case GL_TEXTURE_SWIZZLE_B:
5569 case GL_TEXTURE_SWIZZLE_A:
5570 case GL_TEXTURE_BASE_LEVEL:
5571 case GL_TEXTURE_MAX_LEVEL:
5572 case GL_TEXTURE_COMPARE_MODE:
5573 case GL_TEXTURE_COMPARE_FUNC:
5574 case GL_TEXTURE_MIN_LOD:
5575 case GL_TEXTURE_MAX_LOD:
5576 if (context->getClientMajorVersion() < 3)
5577 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005578 context->handleError(InvalidEnum() << "pname requires OpenGL ES 3.0.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005579 return false;
5580 }
5581 if (target == GL_TEXTURE_EXTERNAL_OES &&
5582 !context->getExtensions().eglImageExternalEssl3)
5583 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005584 context->handleError(InvalidEnum() << "ES3 texture parameters are not "
5585 "available without "
5586 "GL_OES_EGL_image_external_essl3.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005587 return false;
5588 }
5589 break;
5590
5591 default:
5592 break;
5593 }
5594
JiangYizhou4cff8d62017-07-06 14:54:09 +08005595 if (target == GL_TEXTURE_2D_MULTISAMPLE)
5596 {
5597 switch (pname)
5598 {
5599 case GL_TEXTURE_MIN_FILTER:
5600 case GL_TEXTURE_MAG_FILTER:
5601 case GL_TEXTURE_WRAP_S:
5602 case GL_TEXTURE_WRAP_T:
5603 case GL_TEXTURE_WRAP_R:
5604 case GL_TEXTURE_MIN_LOD:
5605 case GL_TEXTURE_MAX_LOD:
5606 case GL_TEXTURE_COMPARE_MODE:
5607 case GL_TEXTURE_COMPARE_FUNC:
5608 context->handleError(InvalidEnum()
5609 << "Invalid parameter for 2D multisampled textures.");
5610 return false;
5611 }
5612 }
5613
Jamie Madillbe849e42017-05-02 15:49:00 -04005614 switch (pname)
5615 {
5616 case GL_TEXTURE_WRAP_S:
5617 case GL_TEXTURE_WRAP_T:
5618 case GL_TEXTURE_WRAP_R:
5619 if (!ValidateTextureWrapModeValue(context, params, target == GL_TEXTURE_EXTERNAL_OES))
5620 {
5621 return false;
5622 }
5623 break;
5624
5625 case GL_TEXTURE_MIN_FILTER:
5626 if (!ValidateTextureMinFilterValue(context, params, target == GL_TEXTURE_EXTERNAL_OES))
5627 {
5628 return false;
5629 }
5630 break;
5631
5632 case GL_TEXTURE_MAG_FILTER:
5633 if (!ValidateTextureMagFilterValue(context, params))
5634 {
5635 return false;
5636 }
5637 break;
5638
5639 case GL_TEXTURE_USAGE_ANGLE:
5640 switch (ConvertToGLenum(params[0]))
5641 {
5642 case GL_NONE:
5643 case GL_FRAMEBUFFER_ATTACHMENT_ANGLE:
5644 break;
5645
5646 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005647 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005648 return false;
5649 }
5650 break;
5651
5652 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
5653 if (!context->getExtensions().textureFilterAnisotropic)
5654 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005655 context->handleError(InvalidEnum() << "GL_EXT_texture_anisotropic is not enabled.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005656 return false;
5657 }
5658
5659 // we assume the parameter passed to this validation method is truncated, not rounded
5660 if (params[0] < 1)
5661 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005662 context->handleError(InvalidValue() << "Max anisotropy must be at least 1.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005663 return false;
5664 }
5665 break;
5666
5667 case GL_TEXTURE_MIN_LOD:
5668 case GL_TEXTURE_MAX_LOD:
5669 // any value is permissible
5670 break;
5671
5672 case GL_TEXTURE_COMPARE_MODE:
5673 if (!ValidateTextureCompareModeValue(context, params))
5674 {
5675 return false;
5676 }
5677 break;
5678
5679 case GL_TEXTURE_COMPARE_FUNC:
5680 if (!ValidateTextureCompareFuncValue(context, params))
5681 {
5682 return false;
5683 }
5684 break;
5685
5686 case GL_TEXTURE_SWIZZLE_R:
5687 case GL_TEXTURE_SWIZZLE_G:
5688 case GL_TEXTURE_SWIZZLE_B:
5689 case GL_TEXTURE_SWIZZLE_A:
5690 switch (ConvertToGLenum(params[0]))
5691 {
5692 case GL_RED:
5693 case GL_GREEN:
5694 case GL_BLUE:
5695 case GL_ALPHA:
5696 case GL_ZERO:
5697 case GL_ONE:
5698 break;
5699
5700 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005701 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005702 return false;
5703 }
5704 break;
5705
5706 case GL_TEXTURE_BASE_LEVEL:
5707 if (params[0] < 0)
5708 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005709 context->handleError(InvalidValue() << "Base level must be at least 0.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005710 return false;
5711 }
5712 if (target == GL_TEXTURE_EXTERNAL_OES && static_cast<GLuint>(params[0]) != 0)
5713 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005714 context->handleError(InvalidOperation()
5715 << "Base level must be 0 for external textures.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005716 return false;
5717 }
JiangYizhou4cff8d62017-07-06 14:54:09 +08005718 if (target == GL_TEXTURE_2D_MULTISAMPLE && static_cast<GLuint>(params[0]) != 0)
5719 {
5720 context->handleError(InvalidOperation()
5721 << "Base level must be 0 for multisampled textures.");
5722 return false;
5723 }
Jamie Madillbe849e42017-05-02 15:49:00 -04005724 break;
5725
5726 case GL_TEXTURE_MAX_LEVEL:
5727 if (params[0] < 0)
5728 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005729 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidMipLevel);
Jamie Madillbe849e42017-05-02 15:49:00 -04005730 return false;
5731 }
5732 break;
5733
5734 case GL_DEPTH_STENCIL_TEXTURE_MODE:
5735 if (context->getClientVersion() < Version(3, 1))
5736 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005737 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumRequiresGLES31);
Jamie Madillbe849e42017-05-02 15:49:00 -04005738 return false;
5739 }
5740 switch (ConvertToGLenum(params[0]))
5741 {
5742 case GL_DEPTH_COMPONENT:
5743 case GL_STENCIL_INDEX:
5744 break;
5745
5746 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005747 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005748 return false;
5749 }
5750 break;
5751
5752 case GL_TEXTURE_SRGB_DECODE_EXT:
5753 if (!ValidateTextureSRGBDecodeValue(context, params))
5754 {
5755 return false;
5756 }
5757 break;
5758
5759 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005760 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005761 return false;
5762 }
5763
5764 return true;
5765}
5766
5767template bool ValidateTexParameterBase(Context *, GLenum, GLenum, GLsizei, const GLfloat *);
5768template bool ValidateTexParameterBase(Context *, GLenum, GLenum, GLsizei, const GLint *);
5769
Jamie Madillc29968b2016-01-20 11:17:23 -05005770} // namespace gl