blob: ddac19d336632523d0ed13fa0727b96864033f67 [file] [log] [blame]
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001//
Geoff Langcec35902014-04-16 10:52:36 -04002// Copyright (c) 2013-2014 The ANGLE Project Authors. All rights reserved.
Geoff Lange8ebe7f2013-08-05 15:03:13 -04003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// validationES.h: Validation functions for generic OpenGL ES entry point parameters
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/validationES.h"
Jamie Madille2e406c2016-06-02 13:04:10 -040010
Geoff Lang2b5420c2014-11-19 14:20:15 -050011#include "libANGLE/Context.h"
Geoff Langa8406172015-07-21 16:53:39 -040012#include "libANGLE/Display.h"
Brandon Jones6cad5662017-06-14 13:25:13 -070013#include "libANGLE/ErrorStrings.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050014#include "libANGLE/Framebuffer.h"
15#include "libANGLE/FramebufferAttachment.h"
Geoff Langa8406172015-07-21 16:53:39 -040016#include "libANGLE/Image.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050017#include "libANGLE/Program.h"
Jamie Madill231c7f52017-04-26 13:45:37 -040018#include "libANGLE/Query.h"
19#include "libANGLE/Texture.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050020#include "libANGLE/TransformFeedback.h"
21#include "libANGLE/VertexArray.h"
James Darpinian30b604d2018-03-12 17:26:57 -070022#include "libANGLE/angletypes.h"
Jamie Madill231c7f52017-04-26 13:45:37 -040023#include "libANGLE/formatutils.h"
jchen10a99ed552017-09-22 08:10:32 +080024#include "libANGLE/queryconversions.h"
Jamie Madill231c7f52017-04-26 13:45:37 -040025#include "libANGLE/validationES2.h"
26#include "libANGLE/validationES3.h"
Geoff Lange8ebe7f2013-08-05 15:03:13 -040027
28#include "common/mathutil.h"
29#include "common/utilities.h"
30
Jamie Madille2e406c2016-06-02 13:04:10 -040031using namespace angle;
32
Geoff Lange8ebe7f2013-08-05 15:03:13 -040033namespace gl
34{
Jamie Madill1ca74672015-07-21 15:14:11 -040035namespace
36{
Luc Ferron9dbaeba2018-02-01 07:26:59 -050037bool CompressedTextureFormatRequiresExactSize(GLenum internalFormat)
38{
39 // List of compressed format that require that the texture size is smaller than or a multiple of
40 // the compressed block size.
41 switch (internalFormat)
42 {
43 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
44 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
45 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
46 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
47 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
48 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
49 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
50 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
51 case GL_ETC1_RGB8_LOSSY_DECODE_ANGLE:
52 case GL_COMPRESSED_RGB8_LOSSY_DECODE_ETC2_ANGLE:
53 case GL_COMPRESSED_SRGB8_LOSSY_DECODE_ETC2_ANGLE:
54 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_LOSSY_DECODE_ETC2_ANGLE:
55 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_LOSSY_DECODE_ETC2_ANGLE:
56 case GL_COMPRESSED_RGBA8_LOSSY_DECODE_ETC2_EAC_ANGLE:
57 case GL_COMPRESSED_SRGB8_ALPHA8_LOSSY_DECODE_ETC2_EAC_ANGLE:
58 return true;
jchen10a99ed552017-09-22 08:10:32 +080059
Luc Ferron9dbaeba2018-02-01 07:26:59 -050060 default:
61 return false;
62 }
63}
64bool CompressedSubTextureFormatRequiresExactSize(GLenum internalFormat)
65{
66 // Compressed sub textures have additional formats that requires exact size.
67 // ES 3.1, Section 8.7, Page 171
68 return CompressedTextureFormatRequiresExactSize(internalFormat) ||
69 IsETC2EACFormat(internalFormat);
70}
Olli Etuaho8d5571a2018-04-23 12:29:31 +030071
72bool DifferenceCanOverflow(GLint a, GLint b)
73{
74 CheckedNumeric<GLint> checkedA(a);
75 checkedA -= b;
76 // Use negation to make sure that the difference can't overflow regardless of the order.
77 checkedA = -checkedA;
78 return !checkedA.IsValid();
79}
80
Jamie Madill5b772312018-03-08 20:28:32 -050081bool ValidateDrawAttribs(Context *context, GLint primcount, GLint maxVertex, GLint vertexCount)
Jamie Madill1ca74672015-07-21 15:14:11 -040082{
Jamie Madilldfde6ab2016-06-09 07:07:18 -070083 const gl::State &state = context->getGLState();
Jamie Madill1ca74672015-07-21 15:14:11 -040084 const gl::Program *program = state.getProgram();
85
Corentin Wallez327411e2016-12-09 11:09:17 -050086 bool webglCompatibility = context->getExtensions().webglCompatibility;
87
Jamie Madill51af38b2018-04-15 08:50:56 -040088 const VertexArray *vao = state.getVertexArray();
89 const AttributesMask &clientAttribs = vao->getEnabledClientMemoryAttribsMask();
90
91 if (clientAttribs.any())
92 {
93 if (webglCompatibility || !state.areClientArraysEnabled())
94 {
95 // [WebGL 1.0] Section 6.5 Enabled Vertex Attributes and Range Checking
96 // If a vertex attribute is enabled as an array via enableVertexAttribArray but no
97 // buffer is bound to that attribute via bindBuffer and vertexAttribPointer, then calls
98 // to drawArrays or drawElements will generate an INVALID_OPERATION error.
99 ANGLE_VALIDATION_ERR(context, InvalidOperation(), VertexArrayNoBuffer);
100 return false;
101 }
102 else if (vao->hasEnabledNullPointerClientArray())
103 {
104 // This is an application error that would normally result in a crash, but we catch it
105 // and return an error
106 ANGLE_VALIDATION_ERR(context, InvalidOperation(), VertexArrayNoBufferPointer);
107 return false;
108 }
109 }
110
111 // If we're drawing zero vertices, we have enough data.
112 if (vertexCount <= 0 || primcount <= 0)
113 {
114 return true;
115 }
116
Jamie Madill231c7f52017-04-26 13:45:37 -0400117 const auto &vertexAttribs = vao->getVertexAttributes();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800118 const auto &vertexBindings = vao->getVertexBindings();
Jamie Madill51af38b2018-04-15 08:50:56 -0400119
Lingfeng Yang461b09a2018-04-23 09:02:09 -0700120 bool isGLES1 = context->getClientVersion() < Version(2, 0);
121
122 const AttributesMask &activeAttribs = ((isGLES1 ? context->getVertexArraysAttributeMask()
123 : program->getActiveAttribLocationsMask()) &
Jamie Madill02c9c042018-04-17 13:43:48 -0400124 vao->getEnabledAttributesMask() & ~clientAttribs);
Jamie Madill51af38b2018-04-15 08:50:56 -0400125
126 for (size_t attributeIndex : activeAttribs)
Jamie Madill1ca74672015-07-21 15:14:11 -0400127 {
128 const VertexAttribute &attrib = vertexAttribs[attributeIndex];
Jamie Madill51af38b2018-04-15 08:50:56 -0400129 ASSERT(attrib.enabled);
Corentin Wallez672f7f32017-06-15 17:42:17 -0400130
Lingfeng Yang038dd532018-03-29 17:31:52 -0700131 const VertexBinding &binding = vertexBindings[attrib.bindingIndex];
Lingfeng Yang461b09a2018-04-23 09:02:09 -0700132 ASSERT(isGLES1 || program->isAttribLocationActive(attributeIndex));
Corentin Wallezfd456442016-12-21 17:57:00 -0500133
Jamie Madill02c9c042018-04-17 13:43:48 -0400134 GLint maxVertexElement = maxVertex;
Martin Radevdd5f27e2017-06-07 10:17:09 +0300135 GLuint divisor = binding.getDivisor();
Jamie Madill02c9c042018-04-17 13:43:48 -0400136 if (divisor != 0)
Corentin Wallezfd456442016-12-21 17:57:00 -0500137 {
Martin Radevdd5f27e2017-06-07 10:17:09 +0300138 maxVertexElement = (primcount - 1) / divisor;
Corentin Wallezfd456442016-12-21 17:57:00 -0500139 }
140
141 // We do manual overflow checks here instead of using safe_math.h because it was
142 // a bottleneck. Thanks to some properties of GL we know inequalities that can
143 // help us make the overflow checks faster.
144
145 // The max possible attribSize is 16 for a vector of 4 32 bit values.
146 constexpr uint64_t kMaxAttribSize = 16;
147 constexpr uint64_t kIntMax = std::numeric_limits<int>::max();
148 constexpr uint64_t kUint64Max = std::numeric_limits<uint64_t>::max();
149
150 // We know attribStride is given as a GLsizei which is typedefed to int.
151 // We also know an upper bound for attribSize.
Jamie Madill02c9c042018-04-17 13:43:48 -0400152 static_assert(std::is_same<int, GLsizei>::value, "Unexpected type");
153 ASSERT(ComputeVertexAttributeStride(attrib, binding) == binding.getStride());
154 uint64_t attribStride = binding.getStride();
155 ASSERT(attribStride <= kIntMax && ComputeVertexAttributeTypeSize(attrib) <= kMaxAttribSize);
Corentin Wallezfd456442016-12-21 17:57:00 -0500156
Jamie Madill02c9c042018-04-17 13:43:48 -0400157 // Computing the product of two 32-bit ints will fit in 64 bits without overflow.
158 static_assert(kIntMax * kIntMax < kUint64Max, "Unexpected overflow");
159 uint64_t attribDataSizeMinusAttribSize = maxVertexElement * attribStride;
Corentin Wallezfd456442016-12-21 17:57:00 -0500160
161 // An overflow can happen when adding the offset, check for it.
Jamie Madill02c9c042018-04-17 13:43:48 -0400162 if (attribDataSizeMinusAttribSize > kUint64Max - attrib.cachedSizePlusRelativeOffset)
Corentin Wallezfd456442016-12-21 17:57:00 -0500163 {
Brandon Jonesafa75152017-07-21 13:11:29 -0700164 ANGLE_VALIDATION_ERR(context, InvalidOperation(), IntegerOverflow);
Corentin Wallezfd456442016-12-21 17:57:00 -0500165 return false;
166 }
Corentin Wallezfd456442016-12-21 17:57:00 -0500167
168 // [OpenGL ES 3.0.2] section 2.9.4 page 40:
Jamie Madill02c9c042018-04-17 13:43:48 -0400169 // We can return INVALID_OPERATION if our array buffer does not have enough backing data.
170 if (attribDataSizeMinusAttribSize + attrib.cachedSizePlusRelativeOffset >
171 binding.getCachedBufferSizeMinusOffset())
Corentin Wallezfd456442016-12-21 17:57:00 -0500172 {
Brandon Jonesafa75152017-07-21 13:11:29 -0700173 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InsufficientVertexBufferSize);
Corentin Wallezfd456442016-12-21 17:57:00 -0500174 return false;
Jamie Madill1ca74672015-07-21 15:14:11 -0400175 }
Jamie Madill02c9c042018-04-17 13:43:48 -0400176 }
James Darpiniane8a93c62018-01-04 18:02:24 -0800177
Jamie Madill7267aa62018-04-17 15:28:21 -0400178 if (webglCompatibility && vao->hasTransformFeedbackBindingConflict(activeAttribs))
Jamie Madill02c9c042018-04-17 13:43:48 -0400179 {
Jamie Madill7267aa62018-04-17 15:28:21 -0400180 ANGLE_VALIDATION_ERR(context, InvalidOperation(), VertexBufferBoundForTransformFeedback);
181 return false;
Jamie Madill1ca74672015-07-21 15:14:11 -0400182 }
183
184 return true;
185}
186
Jamie Madill5b772312018-03-08 20:28:32 -0500187bool ValidReadPixelsTypeEnum(Context *context, GLenum type)
Geoff Lang280ba992017-04-18 16:30:58 -0400188{
189 switch (type)
190 {
191 // Types referenced in Table 3.4 of the ES 2.0.25 spec
192 case GL_UNSIGNED_BYTE:
193 case GL_UNSIGNED_SHORT_4_4_4_4:
194 case GL_UNSIGNED_SHORT_5_5_5_1:
195 case GL_UNSIGNED_SHORT_5_6_5:
196 return context->getClientVersion() >= ES_2_0;
197
198 // Types referenced in Table 3.2 of the ES 3.0.5 spec (Except depth stencil)
199 case GL_BYTE:
200 case GL_INT:
201 case GL_SHORT:
202 case GL_UNSIGNED_INT:
203 case GL_UNSIGNED_INT_10F_11F_11F_REV:
204 case GL_UNSIGNED_INT_24_8:
205 case GL_UNSIGNED_INT_2_10_10_10_REV:
206 case GL_UNSIGNED_INT_5_9_9_9_REV:
207 case GL_UNSIGNED_SHORT:
208 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
209 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
210 return context->getClientVersion() >= ES_3_0;
211
212 case GL_FLOAT:
Geoff Lang7d4602f2017-09-13 10:45:09 -0400213 return context->getClientVersion() >= ES_3_0 || context->getExtensions().textureFloat ||
214 context->getExtensions().colorBufferHalfFloat;
Geoff Lang280ba992017-04-18 16:30:58 -0400215
216 case GL_HALF_FLOAT:
217 return context->getClientVersion() >= ES_3_0 ||
218 context->getExtensions().textureHalfFloat;
219
220 case GL_HALF_FLOAT_OES:
221 return context->getExtensions().colorBufferHalfFloat;
222
223 default:
224 return false;
225 }
226}
227
Jamie Madill5b772312018-03-08 20:28:32 -0500228bool ValidReadPixelsFormatEnum(Context *context, GLenum format)
Geoff Lang280ba992017-04-18 16:30:58 -0400229{
230 switch (format)
231 {
232 // Formats referenced in Table 3.4 of the ES 2.0.25 spec (Except luminance)
233 case GL_RGBA:
234 case GL_RGB:
235 case GL_ALPHA:
236 return context->getClientVersion() >= ES_2_0;
237
238 // Formats referenced in Table 3.2 of the ES 3.0.5 spec
239 case GL_RG:
240 case GL_RED:
241 case GL_RGBA_INTEGER:
242 case GL_RGB_INTEGER:
243 case GL_RG_INTEGER:
244 case GL_RED_INTEGER:
245 return context->getClientVersion() >= ES_3_0;
246
247 case GL_SRGB_ALPHA_EXT:
248 case GL_SRGB_EXT:
249 return context->getExtensions().sRGB;
250
251 case GL_BGRA_EXT:
252 return context->getExtensions().readFormatBGRA;
253
254 default:
255 return false;
256 }
257}
258
Jamie Madill5b772312018-03-08 20:28:32 -0500259bool ValidReadPixelsFormatType(Context *context,
Geoff Langf607c602016-09-21 11:46:48 -0400260 GLenum framebufferComponentType,
261 GLenum format,
262 GLenum type)
263{
264 switch (framebufferComponentType)
265 {
266 case GL_UNSIGNED_NORMALIZED:
267 // TODO(geofflang): Don't accept BGRA here. Some chrome internals appear to try to use
268 // ReadPixels with BGRA even if the extension is not present
269 return (format == GL_RGBA && type == GL_UNSIGNED_BYTE) ||
270 (context->getExtensions().readFormatBGRA && format == GL_BGRA_EXT &&
271 type == GL_UNSIGNED_BYTE);
272
273 case GL_SIGNED_NORMALIZED:
274 return (format == GL_RGBA && type == GL_UNSIGNED_BYTE);
275
276 case GL_INT:
277 return (format == GL_RGBA_INTEGER && type == GL_INT);
278
279 case GL_UNSIGNED_INT:
280 return (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_INT);
281
282 case GL_FLOAT:
283 return (format == GL_RGBA && type == GL_FLOAT);
284
285 default:
286 UNREACHABLE();
287 return false;
288 }
289}
290
Geoff Langc1984ed2016-10-07 12:41:00 -0400291template <typename ParamType>
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400292bool ValidateTextureWrapModeValue(Context *context, ParamType *params, bool restrictedWrapModes)
Geoff Langc1984ed2016-10-07 12:41:00 -0400293{
294 switch (ConvertToGLenum(params[0]))
295 {
296 case GL_CLAMP_TO_EDGE:
297 break;
298
299 case GL_REPEAT:
300 case GL_MIRRORED_REPEAT:
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400301 if (restrictedWrapModes)
Geoff Langc1984ed2016-10-07 12:41:00 -0400302 {
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400303 // OES_EGL_image_external and ANGLE_texture_rectangle specifies this error.
Brandon Jonesafa75152017-07-21 13:11:29 -0700304 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidWrapModeTexture);
Geoff Langc1984ed2016-10-07 12:41:00 -0400305 return false;
306 }
307 break;
308
309 default:
Brandon Jonesafa75152017-07-21 13:11:29 -0700310 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTextureWrap);
Geoff Langc1984ed2016-10-07 12:41:00 -0400311 return false;
312 }
313
314 return true;
315}
316
317template <typename ParamType>
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400318bool ValidateTextureMinFilterValue(Context *context, ParamType *params, bool restrictedMinFilter)
Geoff Langc1984ed2016-10-07 12:41:00 -0400319{
320 switch (ConvertToGLenum(params[0]))
321 {
322 case GL_NEAREST:
323 case GL_LINEAR:
324 break;
325
326 case GL_NEAREST_MIPMAP_NEAREST:
327 case GL_LINEAR_MIPMAP_NEAREST:
328 case GL_NEAREST_MIPMAP_LINEAR:
329 case GL_LINEAR_MIPMAP_LINEAR:
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400330 if (restrictedMinFilter)
Geoff Langc1984ed2016-10-07 12:41:00 -0400331 {
332 // OES_EGL_image_external specifies this error.
Brandon Jonesafa75152017-07-21 13:11:29 -0700333 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidFilterTexture);
Geoff Langc1984ed2016-10-07 12:41:00 -0400334 return false;
335 }
336 break;
337
338 default:
Brandon Jones6cad5662017-06-14 13:25:13 -0700339 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTextureFilterParam);
Geoff Langc1984ed2016-10-07 12:41:00 -0400340 return false;
341 }
342
343 return true;
344}
345
346template <typename ParamType>
347bool ValidateTextureMagFilterValue(Context *context, ParamType *params)
348{
349 switch (ConvertToGLenum(params[0]))
350 {
351 case GL_NEAREST:
352 case GL_LINEAR:
353 break;
354
355 default:
Brandon Jones6cad5662017-06-14 13:25:13 -0700356 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTextureFilterParam);
Geoff Langc1984ed2016-10-07 12:41:00 -0400357 return false;
358 }
359
360 return true;
361}
362
363template <typename ParamType>
364bool ValidateTextureCompareModeValue(Context *context, ParamType *params)
365{
366 // Acceptable mode parameters from GLES 3.0.2 spec, table 3.17
367 switch (ConvertToGLenum(params[0]))
368 {
369 case GL_NONE:
370 case GL_COMPARE_REF_TO_TEXTURE:
371 break;
372
373 default:
Brandon Jonesafa75152017-07-21 13:11:29 -0700374 ANGLE_VALIDATION_ERR(context, InvalidEnum(), UnknownParameter);
Geoff Langc1984ed2016-10-07 12:41:00 -0400375 return false;
376 }
377
378 return true;
379}
380
381template <typename ParamType>
382bool ValidateTextureCompareFuncValue(Context *context, ParamType *params)
383{
384 // Acceptable function parameters from GLES 3.0.2 spec, table 3.17
385 switch (ConvertToGLenum(params[0]))
386 {
387 case GL_LEQUAL:
388 case GL_GEQUAL:
389 case GL_LESS:
390 case GL_GREATER:
391 case GL_EQUAL:
392 case GL_NOTEQUAL:
393 case GL_ALWAYS:
394 case GL_NEVER:
395 break;
396
397 default:
Brandon Jonesafa75152017-07-21 13:11:29 -0700398 ANGLE_VALIDATION_ERR(context, InvalidEnum(), UnknownParameter);
Geoff Langc1984ed2016-10-07 12:41:00 -0400399 return false;
400 }
401
402 return true;
403}
404
405template <typename ParamType>
Geoff Lang81c6b572016-10-19 14:07:52 -0700406bool ValidateTextureSRGBDecodeValue(Context *context, ParamType *params)
407{
408 if (!context->getExtensions().textureSRGBDecode)
409 {
Brandon Jones6cad5662017-06-14 13:25:13 -0700410 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ExtensionNotEnabled);
Geoff Lang81c6b572016-10-19 14:07:52 -0700411 return false;
412 }
413
414 switch (ConvertToGLenum(params[0]))
415 {
416 case GL_DECODE_EXT:
417 case GL_SKIP_DECODE_EXT:
418 break;
419
420 default:
Brandon Jonesafa75152017-07-21 13:11:29 -0700421 ANGLE_VALIDATION_ERR(context, InvalidEnum(), UnknownParameter);
Geoff Lang81c6b572016-10-19 14:07:52 -0700422 return false;
423 }
424
425 return true;
426}
427
Luc Ferron1b1a8642018-01-23 15:12:01 -0500428bool ValidateTextureMaxAnisotropyExtensionEnabled(Context *context)
429{
430 if (!context->getExtensions().textureFilterAnisotropic)
431 {
432 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ExtensionNotEnabled);
433 return false;
434 }
435
436 return true;
437}
438
439bool ValidateTextureMaxAnisotropyValue(Context *context, GLfloat paramValue)
440{
441 if (!ValidateTextureMaxAnisotropyExtensionEnabled(context))
442 {
443 return false;
444 }
445
446 GLfloat largest = context->getExtensions().maxTextureAnisotropy;
447
448 if (paramValue < 1 || paramValue > largest)
449 {
450 ANGLE_VALIDATION_ERR(context, InvalidValue(), OutsideOfBounds);
451 return false;
452 }
453
454 return true;
455}
456
Jamie Madill5b772312018-03-08 20:28:32 -0500457bool ValidateFragmentShaderColorBufferTypeMatch(Context *context)
Geoff Lange0cff192017-05-30 13:04:56 -0400458{
459 const Program *program = context->getGLState().getProgram();
460 const Framebuffer *framebuffer = context->getGLState().getDrawFramebuffer();
461
Brandon Jonesc405ae72017-12-06 14:15:03 -0800462 if (!ComponentTypeMask::Validate(program->getDrawBufferTypeMask().to_ulong(),
463 framebuffer->getDrawBufferTypeMask().to_ulong(),
464 program->getActiveOutputVariables().to_ulong(),
465 framebuffer->getDrawBufferMask().to_ulong()))
Geoff Lange0cff192017-05-30 13:04:56 -0400466 {
Brandon Jones76746f92017-11-22 11:44:41 -0800467 ANGLE_VALIDATION_ERR(context, InvalidOperation(), DrawBufferTypeMismatch);
468 return false;
Geoff Lange0cff192017-05-30 13:04:56 -0400469 }
470
471 return true;
472}
473
Jamie Madill5b772312018-03-08 20:28:32 -0500474bool ValidateVertexShaderAttributeTypeMatch(Context *context)
Geoff Lang9ab5b822017-05-30 16:19:23 -0400475{
Lingfeng Yang038dd532018-03-29 17:31:52 -0700476 const auto &glState = context->getGLState();
Geoff Lang9ab5b822017-05-30 16:19:23 -0400477 const Program *program = context->getGLState().getProgram();
478 const VertexArray *vao = context->getGLState().getVertexArray();
479
Brandon Jonesc405ae72017-12-06 14:15:03 -0800480 unsigned long stateCurrentValuesTypeBits = glState.getCurrentValuesTypeMask().to_ulong();
481 unsigned long vaoAttribTypeBits = vao->getAttributesTypeMask().to_ulong();
482 unsigned long vaoAttribEnabledMask = vao->getAttributesMask().to_ulong();
483
484 vaoAttribEnabledMask |= vaoAttribEnabledMask << MAX_COMPONENT_TYPE_MASK_INDEX;
485 vaoAttribTypeBits = (vaoAttribEnabledMask & vaoAttribTypeBits);
486 vaoAttribTypeBits |= (~vaoAttribEnabledMask & stateCurrentValuesTypeBits);
487
488 if (!ComponentTypeMask::Validate(program->getAttributesTypeMask().to_ulong(), vaoAttribTypeBits,
489 program->getAttributesMask().to_ulong(), 0xFFFF))
Geoff Lang9ab5b822017-05-30 16:19:23 -0400490 {
Brandon Jonesc405ae72017-12-06 14:15:03 -0800491 ANGLE_VALIDATION_ERR(context, InvalidOperation(), VertexShaderTypeMismatch);
492 return false;
Geoff Lang9ab5b822017-05-30 16:19:23 -0400493 }
Geoff Lang9ab5b822017-05-30 16:19:23 -0400494 return true;
495}
496
Jamie Madill493f9572018-05-24 19:52:15 -0400497bool IsCompatibleDrawModeWithGeometryShader(PrimitiveMode drawMode,
498 PrimitiveMode geometryShaderInputPrimitiveType)
Jiawei Shaofccebff2018-03-08 13:51:02 +0800499{
500 // [EXT_geometry_shader] Section 11.1gs.1, Geometry Shader Input Primitives
Jamie Madill493f9572018-05-24 19:52:15 -0400501 switch (drawMode)
Jiawei Shaofccebff2018-03-08 13:51:02 +0800502 {
Jamie Madill493f9572018-05-24 19:52:15 -0400503 case PrimitiveMode::Points:
504 return geometryShaderInputPrimitiveType == PrimitiveMode::Points;
505 case PrimitiveMode::Lines:
506 case PrimitiveMode::LineStrip:
507 case PrimitiveMode::LineLoop:
508 return geometryShaderInputPrimitiveType == PrimitiveMode::Lines;
509 case PrimitiveMode::LinesAdjacency:
510 case PrimitiveMode::LineStripAdjacency:
511 return geometryShaderInputPrimitiveType == PrimitiveMode::LinesAdjacency;
512 case PrimitiveMode::Triangles:
513 case PrimitiveMode::TriangleFan:
514 case PrimitiveMode::TriangleStrip:
515 return geometryShaderInputPrimitiveType == PrimitiveMode::Triangles;
516 case PrimitiveMode::TrianglesAdjacency:
517 case PrimitiveMode::TriangleStripAdjacency:
518 return geometryShaderInputPrimitiveType == PrimitiveMode::TrianglesAdjacency;
Jiawei Shaofccebff2018-03-08 13:51:02 +0800519 default:
520 UNREACHABLE();
521 return false;
522 }
523}
524
Geoff Langf41a7152016-09-19 15:11:17 -0400525} // anonymous namespace
526
Brandon Jonesd1049182018-03-28 10:02:20 -0700527void SetRobustLengthParam(GLsizei *length, GLsizei value)
528{
529 if (length)
530 {
531 *length = value;
532 }
533}
534
Luc Ferron9dbaeba2018-02-01 07:26:59 -0500535bool IsETC2EACFormat(const GLenum format)
536{
537 // ES 3.1, Table 8.19
538 switch (format)
539 {
540 case GL_COMPRESSED_R11_EAC:
541 case GL_COMPRESSED_SIGNED_R11_EAC:
542 case GL_COMPRESSED_RG11_EAC:
543 case GL_COMPRESSED_SIGNED_RG11_EAC:
544 case GL_COMPRESSED_RGB8_ETC2:
545 case GL_COMPRESSED_SRGB8_ETC2:
546 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
547 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
548 case GL_COMPRESSED_RGBA8_ETC2_EAC:
549 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
550 return true;
551
552 default:
553 return false;
554 }
555}
556
Jamie Madill5b772312018-03-08 20:28:32 -0500557bool ValidTextureTarget(const Context *context, TextureType type)
Jamie Madill35d15012013-10-07 10:46:37 -0400558{
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800559 switch (type)
Jamie Madill35d15012013-10-07 10:46:37 -0400560 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800561 case TextureType::_2D:
562 case TextureType::CubeMap:
He Yunchaoced53ae2016-11-29 15:00:51 +0800563 return true;
Jamie Madill35d15012013-10-07 10:46:37 -0400564
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800565 case TextureType::Rectangle:
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400566 return context->getExtensions().textureRectangle;
567
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800568 case TextureType::_3D:
569 case TextureType::_2DArray:
He Yunchaoced53ae2016-11-29 15:00:51 +0800570 return (context->getClientMajorVersion() >= 3);
Jamie Madilld7460c72014-01-21 16:38:14 -0500571
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800572 case TextureType::_2DMultisample:
He Yunchaoced53ae2016-11-29 15:00:51 +0800573 return (context->getClientVersion() >= Version(3, 1));
Geoff Lang3b573612016-10-31 14:08:10 -0400574
He Yunchaoced53ae2016-11-29 15:00:51 +0800575 default:
576 return false;
Jamie Madilld7460c72014-01-21 16:38:14 -0500577 }
Jamie Madill35d15012013-10-07 10:46:37 -0400578}
579
Jamie Madill5b772312018-03-08 20:28:32 -0500580bool ValidTexture2DTarget(const Context *context, TextureType type)
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500581{
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800582 switch (type)
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500583 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800584 case TextureType::_2D:
585 case TextureType::CubeMap:
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500586 return true;
587
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800588 case TextureType::Rectangle:
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400589 return context->getExtensions().textureRectangle;
590
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500591 default:
592 return false;
593 }
594}
595
Jamie Madill5b772312018-03-08 20:28:32 -0500596bool ValidTexture3DTarget(const Context *context, TextureType target)
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500597{
598 switch (target)
599 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800600 case TextureType::_3D:
601 case TextureType::_2DArray:
Martin Radev1be913c2016-07-11 17:59:16 +0300602 return (context->getClientMajorVersion() >= 3);
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500603
604 default:
605 return false;
606 }
607}
608
Ian Ewellbda75592016-04-18 17:25:54 -0400609// Most texture GL calls are not compatible with external textures, so we have a separate validation
610// function for use in the GL calls that do
Jamie Madill5b772312018-03-08 20:28:32 -0500611bool ValidTextureExternalTarget(const Context *context, TextureType target)
Ian Ewellbda75592016-04-18 17:25:54 -0400612{
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800613 return (target == TextureType::External) &&
Ian Ewellbda75592016-04-18 17:25:54 -0400614 (context->getExtensions().eglImageExternal ||
615 context->getExtensions().eglStreamConsumerExternal);
616}
617
Shannon Woods4dfed832014-03-17 20:03:39 -0400618// This function differs from ValidTextureTarget in that the target must be
619// usable as the destination of a 2D operation-- so a cube face is valid, but
620// GL_TEXTURE_CUBE_MAP is not.
Jamie Madill560a8d82014-05-21 13:06:20 -0400621// Note: duplicate of IsInternalTextureTarget
Jamie Madill5b772312018-03-08 20:28:32 -0500622bool ValidTexture2DDestinationTarget(const Context *context, TextureTarget target)
Shannon Woods4dfed832014-03-17 20:03:39 -0400623{
624 switch (target)
625 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800626 case TextureTarget::_2D:
627 case TextureTarget::CubeMapNegativeX:
628 case TextureTarget::CubeMapNegativeY:
629 case TextureTarget::CubeMapNegativeZ:
630 case TextureTarget::CubeMapPositiveX:
631 case TextureTarget::CubeMapPositiveY:
632 case TextureTarget::CubeMapPositiveZ:
He Yunchaoced53ae2016-11-29 15:00:51 +0800633 return true;
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800634 case TextureTarget::Rectangle:
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400635 return context->getExtensions().textureRectangle;
He Yunchaoced53ae2016-11-29 15:00:51 +0800636 default:
637 return false;
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500638 }
639}
640
Jiawei Shao80c32cc2018-04-25 09:48:36 +0800641bool ValidateTransformFeedbackPrimitiveMode(const Context *context,
Jamie Madill493f9572018-05-24 19:52:15 -0400642 PrimitiveMode transformFeedbackPrimitiveMode,
643 PrimitiveMode renderPrimitiveMode)
Jiawei Shao80c32cc2018-04-25 09:48:36 +0800644{
645 ASSERT(context);
646
647 if (!context->getExtensions().geometryShader)
648 {
649 // It is an invalid operation to call DrawArrays or DrawArraysInstanced with a draw mode
650 // that does not match the current transform feedback object's draw mode (if transform
651 // feedback is active), (3.0.2, section 2.14, pg 86)
652 return transformFeedbackPrimitiveMode == renderPrimitiveMode;
653 }
654
655 // [GL_EXT_geometry_shader] Table 12.1gs
Jamie Madill493f9572018-05-24 19:52:15 -0400656 switch (renderPrimitiveMode)
Jiawei Shao80c32cc2018-04-25 09:48:36 +0800657 {
Jamie Madill493f9572018-05-24 19:52:15 -0400658 case PrimitiveMode::Points:
659 return transformFeedbackPrimitiveMode == PrimitiveMode::Points;
660 case PrimitiveMode::Lines:
661 case PrimitiveMode::LineStrip:
662 case PrimitiveMode::LineLoop:
663 return transformFeedbackPrimitiveMode == PrimitiveMode::Lines;
664 case PrimitiveMode::Triangles:
665 case PrimitiveMode::TriangleFan:
666 case PrimitiveMode::TriangleStrip:
667 return transformFeedbackPrimitiveMode == PrimitiveMode::Triangles;
Jiawei Shao80c32cc2018-04-25 09:48:36 +0800668 default:
669 UNREACHABLE();
670 return false;
671 }
672}
673
Jamie Madill5b772312018-03-08 20:28:32 -0500674bool ValidateDrawElementsInstancedBase(Context *context,
Jamie Madill493f9572018-05-24 19:52:15 -0400675 PrimitiveMode mode,
Jamie Madillbe849e42017-05-02 15:49:00 -0400676 GLsizei count,
677 GLenum type,
678 const GLvoid *indices,
679 GLsizei primcount)
680{
681 if (primcount < 0)
682 {
Brandon Jonesafa75152017-07-21 13:11:29 -0700683 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativePrimcount);
Jamie Madillbe849e42017-05-02 15:49:00 -0400684 return false;
685 }
686
687 if (!ValidateDrawElementsCommon(context, mode, count, type, indices, primcount))
688 {
689 return false;
690 }
691
Jamie Madill9fdaa492018-02-16 10:52:11 -0500692 return true;
Jamie Madillbe849e42017-05-02 15:49:00 -0400693}
694
695bool ValidateDrawArraysInstancedBase(Context *context,
Jamie Madill493f9572018-05-24 19:52:15 -0400696 PrimitiveMode mode,
Jamie Madillbe849e42017-05-02 15:49:00 -0400697 GLint first,
698 GLsizei count,
699 GLsizei primcount)
700{
701 if (primcount < 0)
702 {
Brandon Jonesafa75152017-07-21 13:11:29 -0700703 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativePrimcount);
Jamie Madillbe849e42017-05-02 15:49:00 -0400704 return false;
705 }
706
707 if (!ValidateDrawArraysCommon(context, mode, first, count, primcount))
708 {
709 return false;
710 }
711
Jamie Madill9fdaa492018-02-16 10:52:11 -0500712 return true;
Jamie Madillbe849e42017-05-02 15:49:00 -0400713}
714
Jamie Madill5b772312018-03-08 20:28:32 -0500715bool ValidateDrawInstancedANGLE(Context *context)
Jamie Madillbe849e42017-05-02 15:49:00 -0400716{
717 // Verify there is at least one active attribute with a divisor of zero
718 const State &state = context->getGLState();
719
720 Program *program = state.getProgram();
721
722 const auto &attribs = state.getVertexArray()->getVertexAttributes();
723 const auto &bindings = state.getVertexArray()->getVertexBindings();
724 for (size_t attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
725 {
726 const VertexAttribute &attrib = attribs[attributeIndex];
727 const VertexBinding &binding = bindings[attrib.bindingIndex];
Martin Radevdd5f27e2017-06-07 10:17:09 +0300728 if (program->isAttribLocationActive(attributeIndex) && binding.getDivisor() == 0)
Jamie Madillbe849e42017-05-02 15:49:00 -0400729 {
730 return true;
731 }
732 }
733
Brandon Jonesafa75152017-07-21 13:11:29 -0700734 ANGLE_VALIDATION_ERR(context, InvalidOperation(), NoZeroDivisor);
Jamie Madillbe849e42017-05-02 15:49:00 -0400735 return false;
736}
737
Jamie Madill5b772312018-03-08 20:28:32 -0500738bool ValidTexture3DDestinationTarget(const Context *context, TextureType target)
Ian Ewellfc7cf8e2016-01-20 15:57:46 -0500739{
740 switch (target)
741 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800742 case TextureType::_3D:
743 case TextureType::_2DArray:
He Yunchaoced53ae2016-11-29 15:00:51 +0800744 return true;
745 default:
746 return false;
Shannon Woods4dfed832014-03-17 20:03:39 -0400747 }
748}
749
Jamie Madill5b772312018-03-08 20:28:32 -0500750bool ValidTexLevelDestinationTarget(const Context *context, TextureType type)
He Yunchao11b038b2016-11-22 21:24:04 +0800751{
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800752 switch (type)
He Yunchao11b038b2016-11-22 21:24:04 +0800753 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800754 case TextureType::_2D:
755 case TextureType::_2DArray:
756 case TextureType::_2DMultisample:
757 case TextureType::CubeMap:
758 case TextureType::_3D:
He Yunchao11b038b2016-11-22 21:24:04 +0800759 return true;
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800760 case TextureType::Rectangle:
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400761 return context->getExtensions().textureRectangle;
He Yunchao11b038b2016-11-22 21:24:04 +0800762 default:
763 return false;
764 }
765}
766
Jamie Madill5b772312018-03-08 20:28:32 -0500767bool ValidFramebufferTarget(const Context *context, GLenum target)
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500768{
He Yunchaoced53ae2016-11-29 15:00:51 +0800769 static_assert(GL_DRAW_FRAMEBUFFER_ANGLE == GL_DRAW_FRAMEBUFFER &&
770 GL_READ_FRAMEBUFFER_ANGLE == GL_READ_FRAMEBUFFER,
Geoff Langd4475812015-03-18 10:53:05 -0400771 "ANGLE framebuffer enums must equal the ES3 framebuffer enums.");
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500772
773 switch (target)
774 {
He Yunchaoced53ae2016-11-29 15:00:51 +0800775 case GL_FRAMEBUFFER:
776 return true;
Geoff Lange8afa902017-09-27 15:00:43 -0400777
He Yunchaoced53ae2016-11-29 15:00:51 +0800778 case GL_READ_FRAMEBUFFER:
He Yunchaoced53ae2016-11-29 15:00:51 +0800779 case GL_DRAW_FRAMEBUFFER:
Geoff Lange8afa902017-09-27 15:00:43 -0400780 return (context->getExtensions().framebufferBlit ||
781 context->getClientMajorVersion() >= 3);
782
He Yunchaoced53ae2016-11-29 15:00:51 +0800783 default:
784 return false;
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500785 }
786}
787
Jamie Madill5b772312018-03-08 20:28:32 -0500788bool ValidMipLevel(const Context *context, TextureType type, GLint level)
Geoff Langce635692013-09-24 13:56:32 -0400789{
Jamie Madillc29968b2016-01-20 11:17:23 -0500790 const auto &caps = context->getCaps();
Geoff Langaae65a42014-05-26 12:43:44 -0400791 size_t maxDimension = 0;
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800792 switch (type)
Geoff Langce635692013-09-24 13:56:32 -0400793 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800794 case TextureType::_2D:
795 case TextureType::_2DArray:
796 case TextureType::_2DMultisample:
Jamie Madillc29968b2016-01-20 11:17:23 -0500797 maxDimension = caps.max2DTextureSize;
798 break;
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800799 case TextureType::CubeMap:
He Yunchaoced53ae2016-11-29 15:00:51 +0800800 maxDimension = caps.maxCubeMapTextureSize;
801 break;
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800802 case TextureType::Rectangle:
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400803 return level == 0;
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800804 case TextureType::_3D:
He Yunchaoced53ae2016-11-29 15:00:51 +0800805 maxDimension = caps.max3DTextureSize;
806 break;
He Yunchaoced53ae2016-11-29 15:00:51 +0800807 default:
808 UNREACHABLE();
Geoff Langce635692013-09-24 13:56:32 -0400809 }
810
Brandon Jones6cad5662017-06-14 13:25:13 -0700811 return level <= gl::log2(static_cast<int>(maxDimension)) && level >= 0;
Geoff Langce635692013-09-24 13:56:32 -0400812}
813
Jamie Madill5b772312018-03-08 20:28:32 -0500814bool ValidImageSizeParameters(Context *context,
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800815 TextureType target,
Austin Kinross08528e12015-10-07 16:24:40 -0700816 GLint level,
817 GLsizei width,
818 GLsizei height,
819 GLsizei depth,
820 bool isSubImage)
Geoff Langce635692013-09-24 13:56:32 -0400821{
Brandon Jones6cad5662017-06-14 13:25:13 -0700822 if (width < 0 || height < 0 || depth < 0)
Geoff Langce635692013-09-24 13:56:32 -0400823 {
Brandon Jones6cad5662017-06-14 13:25:13 -0700824 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeSize);
Geoff Langce635692013-09-24 13:56:32 -0400825 return false;
826 }
Austin Kinross08528e12015-10-07 16:24:40 -0700827 // TexSubImage parameters can be NPOT without textureNPOT extension,
828 // as long as the destination texture is POT.
Geoff Langcc507aa2016-12-12 10:09:52 -0500829 bool hasNPOTSupport =
Geoff Lang5f319a42017-01-09 16:49:19 -0500830 context->getExtensions().textureNPOT || context->getClientVersion() >= Version(3, 0);
Geoff Langcc507aa2016-12-12 10:09:52 -0500831 if (!isSubImage && !hasNPOTSupport &&
Jamie Madill4fd75c12014-06-23 10:53:54 -0400832 (level != 0 && (!gl::isPow2(width) || !gl::isPow2(height) || !gl::isPow2(depth))))
Geoff Langce635692013-09-24 13:56:32 -0400833 {
Brandon Jones6cad5662017-06-14 13:25:13 -0700834 ANGLE_VALIDATION_ERR(context, InvalidValue(), TextureNotPow2);
Geoff Langce635692013-09-24 13:56:32 -0400835 return false;
836 }
837
838 if (!ValidMipLevel(context, target, level))
839 {
Brandon Jones6cad5662017-06-14 13:25:13 -0700840 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidMipLevel);
Geoff Langce635692013-09-24 13:56:32 -0400841 return false;
842 }
843
844 return true;
845}
846
Geoff Lang966c9402017-04-18 12:38:27 -0400847bool ValidCompressedDimension(GLsizei size, GLuint blockSize, bool smallerThanBlockSizeAllowed)
848{
849 return (smallerThanBlockSizeAllowed && (size > 0) && (blockSize % size == 0)) ||
850 (size % blockSize == 0);
851}
852
Jamie Madill5b772312018-03-08 20:28:32 -0500853bool ValidCompressedImageSize(const Context *context,
Jamie Madillc29968b2016-01-20 11:17:23 -0500854 GLenum internalFormat,
Geoff Lang966c9402017-04-18 12:38:27 -0400855 GLint level,
Jamie Madillc29968b2016-01-20 11:17:23 -0500856 GLsizei width,
857 GLsizei height)
Geoff Langd4f180b2013-09-24 13:57:44 -0400858{
Geoff Langca271392017-04-05 12:30:00 -0400859 const gl::InternalFormat &formatInfo = gl::GetSizedInternalFormatInfo(internalFormat);
Geoff Lang5d601382014-07-22 15:14:06 -0400860 if (!formatInfo.compressed)
Geoff Langd4f180b2013-09-24 13:57:44 -0400861 {
862 return false;
863 }
864
Geoff Lang966c9402017-04-18 12:38:27 -0400865 if (width < 0 || height < 0)
866 {
867 return false;
868 }
869
870 if (CompressedTextureFormatRequiresExactSize(internalFormat))
871 {
872 // The ANGLE extensions allow specifying compressed textures with sizes smaller than the
873 // block size for level 0 but WebGL disallows this.
874 bool smallerThanBlockSizeAllowed =
875 level > 0 || !context->getExtensions().webglCompatibility;
876
877 if (!ValidCompressedDimension(width, formatInfo.compressedBlockWidth,
878 smallerThanBlockSizeAllowed) ||
879 !ValidCompressedDimension(height, formatInfo.compressedBlockHeight,
880 smallerThanBlockSizeAllowed))
881 {
882 return false;
883 }
884 }
885
886 return true;
887}
888
Jamie Madill5b772312018-03-08 20:28:32 -0500889bool ValidCompressedSubImageSize(const Context *context,
Geoff Lang966c9402017-04-18 12:38:27 -0400890 GLenum internalFormat,
891 GLint xoffset,
892 GLint yoffset,
893 GLsizei width,
894 GLsizei height,
895 size_t textureWidth,
896 size_t textureHeight)
897{
898 const gl::InternalFormat &formatInfo = gl::GetSizedInternalFormatInfo(internalFormat);
899 if (!formatInfo.compressed)
900 {
901 return false;
902 }
903
Geoff Lang44ff5a72017-02-03 15:15:43 -0500904 if (xoffset < 0 || yoffset < 0 || width < 0 || height < 0)
Geoff Langd4f180b2013-09-24 13:57:44 -0400905 {
906 return false;
907 }
908
Luc Ferron9dbaeba2018-02-01 07:26:59 -0500909 if (CompressedSubTextureFormatRequiresExactSize(internalFormat))
Geoff Lang0d8b7242015-09-09 14:56:53 -0400910 {
Geoff Lang44ff5a72017-02-03 15:15:43 -0500911 if (xoffset % formatInfo.compressedBlockWidth != 0 ||
Geoff Lang966c9402017-04-18 12:38:27 -0400912 yoffset % formatInfo.compressedBlockHeight != 0)
913 {
914 return false;
915 }
916
917 // Allowed to either have data that is a multiple of block size or is smaller than the block
918 // size but fills the entire mip
919 bool fillsEntireMip = xoffset == 0 && yoffset == 0 &&
920 static_cast<size_t>(width) == textureWidth &&
921 static_cast<size_t>(height) == textureHeight;
922 bool sizeMultipleOfBlockSize = (width % formatInfo.compressedBlockWidth) == 0 &&
923 (height % formatInfo.compressedBlockHeight) == 0;
924 if (!sizeMultipleOfBlockSize && !fillsEntireMip)
Geoff Lang0d8b7242015-09-09 14:56:53 -0400925 {
926 return false;
927 }
928 }
929
Geoff Langd4f180b2013-09-24 13:57:44 -0400930 return true;
931}
932
Jamie Madill5b772312018-03-08 20:28:32 -0500933bool ValidImageDataSize(Context *context,
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800934 TextureType texType,
Geoff Langff5b2d52016-09-07 11:32:23 -0400935 GLsizei width,
936 GLsizei height,
937 GLsizei depth,
Geoff Langdbcced82017-06-06 15:55:54 -0400938 GLenum format,
Geoff Langff5b2d52016-09-07 11:32:23 -0400939 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -0400940 const void *pixels,
Geoff Langff5b2d52016-09-07 11:32:23 -0400941 GLsizei imageSize)
942{
Corentin Wallez336129f2017-10-17 15:55:40 -0400943 gl::Buffer *pixelUnpackBuffer =
944 context->getGLState().getTargetBuffer(BufferBinding::PixelUnpack);
Geoff Langff5b2d52016-09-07 11:32:23 -0400945 if (pixelUnpackBuffer == nullptr && imageSize < 0)
946 {
947 // Checks are not required
948 return true;
949 }
950
951 // ...the data would be unpacked from the buffer object such that the memory reads required
952 // would exceed the data store size.
Geoff Langdbcced82017-06-06 15:55:54 -0400953 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(format, type);
954 ASSERT(formatInfo.internalFormat != GL_NONE);
Geoff Langff5b2d52016-09-07 11:32:23 -0400955 const gl::Extents size(width, height, depth);
956 const auto &unpack = context->getGLState().getUnpackState();
957
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800958 bool targetIs3D = texType == TextureType::_3D || texType == TextureType::_2DArray;
Geoff Langff5b2d52016-09-07 11:32:23 -0400959 auto endByteOrErr = formatInfo.computePackUnpackEndByte(type, size, unpack, targetIs3D);
960 if (endByteOrErr.isError())
961 {
962 context->handleError(endByteOrErr.getError());
963 return false;
964 }
965
966 GLuint endByte = endByteOrErr.getResult();
967
968 if (pixelUnpackBuffer)
969 {
970 CheckedNumeric<size_t> checkedEndByte(endByteOrErr.getResult());
971 CheckedNumeric<size_t> checkedOffset(reinterpret_cast<size_t>(pixels));
972 checkedEndByte += checkedOffset;
973
974 if (!checkedEndByte.IsValid() ||
975 (checkedEndByte.ValueOrDie() > static_cast<size_t>(pixelUnpackBuffer->getSize())))
976 {
977 // Overflow past the end of the buffer
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500978 context->handleError(InvalidOperation());
Geoff Langff5b2d52016-09-07 11:32:23 -0400979 return false;
980 }
James Darpiniane8a93c62018-01-04 18:02:24 -0800981 if (context->getExtensions().webglCompatibility &&
982 pixelUnpackBuffer->isBoundForTransformFeedbackAndOtherUse())
983 {
984 ANGLE_VALIDATION_ERR(context, InvalidOperation(),
985 PixelUnpackBufferBoundForTransformFeedback);
986 return false;
987 }
Geoff Langff5b2d52016-09-07 11:32:23 -0400988 }
989 else
990 {
991 ASSERT(imageSize >= 0);
992 if (pixels == nullptr && imageSize != 0)
993 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500994 context->handleError(InvalidOperation()
995 << "imageSize must be 0 if no texture data is provided.");
Geoff Lang3feb3ff2016-10-26 10:57:45 -0400996 return false;
Geoff Langff5b2d52016-09-07 11:32:23 -0400997 }
998
Geoff Lang3feb3ff2016-10-26 10:57:45 -0400999 if (pixels != nullptr && endByte > static_cast<GLuint>(imageSize))
Geoff Langff5b2d52016-09-07 11:32:23 -04001000 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001001 context->handleError(InvalidOperation() << "imageSize must be at least " << endByte);
Geoff Langff5b2d52016-09-07 11:32:23 -04001002 return false;
1003 }
1004 }
1005
1006 return true;
1007}
1008
Corentin Wallezad3ae902018-03-09 13:40:42 -05001009bool ValidQueryType(const Context *context, QueryType queryType)
Geoff Lang37dde692014-01-31 16:34:54 -05001010{
Geoff Lang37dde692014-01-31 16:34:54 -05001011 switch (queryType)
1012 {
Corentin Wallezad3ae902018-03-09 13:40:42 -05001013 case QueryType::AnySamples:
1014 case QueryType::AnySamplesConservative:
Geoff Lang8c5b31c2017-09-26 18:07:44 -04001015 return context->getClientMajorVersion() >= 3 ||
1016 context->getExtensions().occlusionQueryBoolean;
Corentin Wallezad3ae902018-03-09 13:40:42 -05001017 case QueryType::TransformFeedbackPrimitivesWritten:
He Yunchaoced53ae2016-11-29 15:00:51 +08001018 return (context->getClientMajorVersion() >= 3);
Corentin Wallezad3ae902018-03-09 13:40:42 -05001019 case QueryType::TimeElapsed:
He Yunchaoced53ae2016-11-29 15:00:51 +08001020 return context->getExtensions().disjointTimerQuery;
Corentin Wallezad3ae902018-03-09 13:40:42 -05001021 case QueryType::CommandsCompleted:
He Yunchaoced53ae2016-11-29 15:00:51 +08001022 return context->getExtensions().syncQuery;
Corentin Wallezad3ae902018-03-09 13:40:42 -05001023 case QueryType::PrimitivesGenerated:
Jiawei Shaod2fa07e2018-03-15 09:20:25 +08001024 return context->getExtensions().geometryShader;
He Yunchaoced53ae2016-11-29 15:00:51 +08001025 default:
1026 return false;
Geoff Lang37dde692014-01-31 16:34:54 -05001027 }
1028}
1029
Jamie Madill5b772312018-03-08 20:28:32 -05001030bool ValidateWebGLVertexAttribPointer(Context *context,
Geoff Lang2d62ab72017-03-23 16:54:40 -04001031 GLenum type,
1032 GLboolean normalized,
1033 GLsizei stride,
Jamie Madill876429b2017-04-20 15:46:24 -04001034 const void *ptr,
Geoff Lang2d62ab72017-03-23 16:54:40 -04001035 bool pureInteger)
1036{
1037 ASSERT(context->getExtensions().webglCompatibility);
Geoff Lang2d62ab72017-03-23 16:54:40 -04001038 // WebGL 1.0 [Section 6.11] Vertex Attribute Data Stride
1039 // The WebGL API supports vertex attribute data strides up to 255 bytes. A call to
1040 // vertexAttribPointer will generate an INVALID_VALUE error if the value for the stride
1041 // parameter exceeds 255.
1042 constexpr GLsizei kMaxWebGLStride = 255;
1043 if (stride > kMaxWebGLStride)
1044 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001045 context->handleError(InvalidValue()
1046 << "Stride is over the maximum stride allowed by WebGL.");
Geoff Lang2d62ab72017-03-23 16:54:40 -04001047 return false;
1048 }
1049
1050 // WebGL 1.0 [Section 6.4] Buffer Offset and Stride Requirements
1051 // The offset arguments to drawElements and vertexAttribPointer, and the stride argument to
1052 // vertexAttribPointer, must be a multiple of the size of the data type passed to the call,
1053 // or an INVALID_OPERATION error is generated.
1054 VertexFormatType internalType = GetVertexFormatType(type, normalized, 1, pureInteger);
1055 size_t typeSize = GetVertexFormatTypeSize(internalType);
1056
1057 ASSERT(isPow2(typeSize) && typeSize > 0);
1058 size_t sizeMask = (typeSize - 1);
1059 if ((reinterpret_cast<intptr_t>(ptr) & sizeMask) != 0)
1060 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001061 ANGLE_VALIDATION_ERR(context, InvalidOperation(), OffsetMustBeMultipleOfType);
Geoff Lang2d62ab72017-03-23 16:54:40 -04001062 return false;
1063 }
1064
1065 if ((stride & sizeMask) != 0)
1066 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001067 ANGLE_VALIDATION_ERR(context, InvalidOperation(), StrideMustBeMultipleOfType);
Geoff Lang2d62ab72017-03-23 16:54:40 -04001068 return false;
1069 }
1070
1071 return true;
1072}
1073
Jamie Madill5b772312018-03-08 20:28:32 -05001074Program *GetValidProgram(Context *context, GLuint id)
Geoff Lang48dcae72014-02-05 16:28:24 -05001075{
He Yunchaoced53ae2016-11-29 15:00:51 +08001076 // ES3 spec (section 2.11.1) -- "Commands that accept shader or program object names will
1077 // generate the error INVALID_VALUE if the provided name is not the name of either a shader
1078 // or program object and INVALID_OPERATION if the provided name identifies an object
1079 // that is not the expected type."
Geoff Lang48dcae72014-02-05 16:28:24 -05001080
Dian Xiang769769a2015-09-09 15:20:08 -07001081 Program *validProgram = context->getProgram(id);
1082
1083 if (!validProgram)
Geoff Lang48dcae72014-02-05 16:28:24 -05001084 {
Dian Xiang769769a2015-09-09 15:20:08 -07001085 if (context->getShader(id))
1086 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001087 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExpectedProgramName);
Dian Xiang769769a2015-09-09 15:20:08 -07001088 }
1089 else
1090 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001091 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidProgramName);
Dian Xiang769769a2015-09-09 15:20:08 -07001092 }
Geoff Lang48dcae72014-02-05 16:28:24 -05001093 }
Dian Xiang769769a2015-09-09 15:20:08 -07001094
1095 return validProgram;
1096}
1097
Jamie Madill5b772312018-03-08 20:28:32 -05001098Shader *GetValidShader(Context *context, GLuint id)
Dian Xiang769769a2015-09-09 15:20:08 -07001099{
1100 // See ValidProgram for spec details.
1101
1102 Shader *validShader = context->getShader(id);
1103
1104 if (!validShader)
Geoff Lang48dcae72014-02-05 16:28:24 -05001105 {
Dian Xiang769769a2015-09-09 15:20:08 -07001106 if (context->getProgram(id))
1107 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001108 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExpectedShaderName);
Dian Xiang769769a2015-09-09 15:20:08 -07001109 }
1110 else
1111 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001112 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidShaderName);
Dian Xiang769769a2015-09-09 15:20:08 -07001113 }
Geoff Lang48dcae72014-02-05 16:28:24 -05001114 }
Dian Xiang769769a2015-09-09 15:20:08 -07001115
1116 return validShader;
Geoff Lang48dcae72014-02-05 16:28:24 -05001117}
1118
Geoff Langb1196682014-07-23 13:47:29 -04001119bool ValidateAttachmentTarget(gl::Context *context, GLenum attachment)
Jamie Madillb4472272014-07-03 10:38:55 -04001120{
Geoff Langfa125c92017-10-24 13:01:46 -04001121 if (attachment >= GL_COLOR_ATTACHMENT1_EXT && attachment <= GL_COLOR_ATTACHMENT15_EXT)
Jamie Madillb4472272014-07-03 10:38:55 -04001122 {
Geoff Langfa125c92017-10-24 13:01:46 -04001123 if (context->getClientMajorVersion() < 3 && !context->getExtensions().drawBuffers)
1124 {
1125 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidAttachment);
1126 return false;
1127 }
Jamie Madillb4472272014-07-03 10:38:55 -04001128
Geoff Langfa125c92017-10-24 13:01:46 -04001129 // Color attachment 0 is validated below because it is always valid
1130 const unsigned int colorAttachment = (attachment - GL_COLOR_ATTACHMENT0_EXT);
Geoff Langaae65a42014-05-26 12:43:44 -04001131 if (colorAttachment >= context->getCaps().maxColorAttachments)
Jamie Madillb4472272014-07-03 10:38:55 -04001132 {
Geoff Langfa125c92017-10-24 13:01:46 -04001133 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidAttachment);
Geoff Langb1196682014-07-23 13:47:29 -04001134 return false;
Jamie Madillb4472272014-07-03 10:38:55 -04001135 }
1136 }
1137 else
1138 {
1139 switch (attachment)
1140 {
Geoff Langfa125c92017-10-24 13:01:46 -04001141 case GL_COLOR_ATTACHMENT0:
He Yunchaoced53ae2016-11-29 15:00:51 +08001142 case GL_DEPTH_ATTACHMENT:
1143 case GL_STENCIL_ATTACHMENT:
1144 break;
Jamie Madillb4472272014-07-03 10:38:55 -04001145
He Yunchaoced53ae2016-11-29 15:00:51 +08001146 case GL_DEPTH_STENCIL_ATTACHMENT:
1147 if (!context->getExtensions().webglCompatibility &&
1148 context->getClientMajorVersion() < 3)
1149 {
Geoff Langfa125c92017-10-24 13:01:46 -04001150 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidAttachment);
He Yunchaoced53ae2016-11-29 15:00:51 +08001151 return false;
1152 }
1153 break;
Jamie Madillb4472272014-07-03 10:38:55 -04001154
He Yunchaoced53ae2016-11-29 15:00:51 +08001155 default:
Geoff Langfa125c92017-10-24 13:01:46 -04001156 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidAttachment);
He Yunchaoced53ae2016-11-29 15:00:51 +08001157 return false;
Jamie Madillb4472272014-07-03 10:38:55 -04001158 }
1159 }
1160
1161 return true;
1162}
1163
Jamie Madill5b772312018-03-08 20:28:32 -05001164bool ValidateRenderbufferStorageParametersBase(Context *context,
He Yunchaoced53ae2016-11-29 15:00:51 +08001165 GLenum target,
1166 GLsizei samples,
1167 GLenum internalformat,
1168 GLsizei width,
1169 GLsizei height)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001170{
1171 switch (target)
1172 {
He Yunchaoced53ae2016-11-29 15:00:51 +08001173 case GL_RENDERBUFFER:
1174 break;
1175 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07001176 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidRenderbufferTarget);
He Yunchaoced53ae2016-11-29 15:00:51 +08001177 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001178 }
1179
1180 if (width < 0 || height < 0 || samples < 0)
1181 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001182 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidRenderbufferWidthHeight);
Geoff Langb1196682014-07-23 13:47:29 -04001183 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001184 }
1185
Jamie Madill4e0e6f82017-02-17 11:06:03 -05001186 // Hack for the special WebGL 1 "DEPTH_STENCIL" internal format.
1187 GLenum convertedInternalFormat = context->getConvertedRenderbufferFormat(internalformat);
1188
1189 const TextureCaps &formatCaps = context->getTextureCaps().get(convertedInternalFormat);
Yuly Novikovf15f8862018-06-04 18:59:41 -04001190 if (!formatCaps.renderbuffer)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001191 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001192 context->handleError(InvalidEnum());
Geoff Langb1196682014-07-23 13:47:29 -04001193 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001194 }
1195
1196 // ANGLE_framebuffer_multisample does not explicitly state that the internal format must be
1197 // 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 -08001198 // only sized internal formats.
Geoff Langca271392017-04-05 12:30:00 -04001199 const gl::InternalFormat &formatInfo = gl::GetSizedInternalFormatInfo(convertedInternalFormat);
1200 if (formatInfo.internalFormat == GL_NONE)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001201 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001202 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidRenderbufferInternalFormat);
Geoff Langb1196682014-07-23 13:47:29 -04001203 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001204 }
1205
Geoff Langaae65a42014-05-26 12:43:44 -04001206 if (static_cast<GLuint>(std::max(width, height)) > context->getCaps().maxRenderbufferSize)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001207 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001208 context->handleError(InvalidValue());
Geoff Langb1196682014-07-23 13:47:29 -04001209 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001210 }
1211
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001212 GLuint handle = context->getGLState().getRenderbufferId();
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001213 if (handle == 0)
1214 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001215 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidRenderbufferTarget);
Geoff Langb1196682014-07-23 13:47:29 -04001216 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001217 }
1218
1219 return true;
1220}
1221
He Yunchaoced53ae2016-11-29 15:00:51 +08001222bool ValidateFramebufferRenderbufferParameters(gl::Context *context,
1223 GLenum target,
1224 GLenum attachment,
1225 GLenum renderbuffertarget,
1226 GLuint renderbuffer)
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001227{
Geoff Lange8afa902017-09-27 15:00:43 -04001228 if (!ValidFramebufferTarget(context, target))
Shannon Woods1da3cf62014-06-27 15:32:23 -04001229 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001230 context->handleError(InvalidEnum());
Geoff Langb1196682014-07-23 13:47:29 -04001231 return false;
Shannon Woods1da3cf62014-06-27 15:32:23 -04001232 }
1233
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001234 gl::Framebuffer *framebuffer = context->getGLState().getTargetFramebuffer(target);
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001235
Jamie Madill84115c92015-04-23 15:00:07 -04001236 ASSERT(framebuffer);
1237 if (framebuffer->id() == 0)
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001238 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001239 ANGLE_VALIDATION_ERR(context, InvalidOperation(), DefaultFramebufferTarget);
Geoff Langb1196682014-07-23 13:47:29 -04001240 return false;
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001241 }
1242
Jamie Madillb4472272014-07-03 10:38:55 -04001243 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001244 {
Jamie Madillb4472272014-07-03 10:38:55 -04001245 return false;
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001246 }
1247
Jamie Madillab9d82c2014-01-21 16:38:14 -05001248 // [OpenGL ES 2.0.25] Section 4.4.3 page 112
1249 // [OpenGL ES 3.0.2] Section 4.4.2 page 201
1250 // 'renderbuffer' must be either zero or the name of an existing renderbuffer object of
1251 // type 'renderbuffertarget', otherwise an INVALID_OPERATION error is generated.
1252 if (renderbuffer != 0)
1253 {
1254 if (!context->getRenderbuffer(renderbuffer))
1255 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001256 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidRenderbufferTarget);
Geoff Langb1196682014-07-23 13:47:29 -04001257 return false;
Jamie Madillab9d82c2014-01-21 16:38:14 -05001258 }
1259 }
1260
Jamie Madill1fc7e2c2014-01-21 16:47:10 -05001261 return true;
1262}
1263
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -08001264bool ValidateBlitFramebufferParameters(Context *context,
Jamie Madillc29968b2016-01-20 11:17:23 -05001265 GLint srcX0,
1266 GLint srcY0,
1267 GLint srcX1,
1268 GLint srcY1,
1269 GLint dstX0,
1270 GLint dstY0,
1271 GLint dstX1,
1272 GLint dstY1,
1273 GLbitfield mask,
1274 GLenum filter)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001275{
1276 switch (filter)
1277 {
He Yunchaoced53ae2016-11-29 15:00:51 +08001278 case GL_NEAREST:
1279 break;
1280 case GL_LINEAR:
1281 break;
1282 default:
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001283 context->handleError(InvalidEnum());
He Yunchaoced53ae2016-11-29 15:00:51 +08001284 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001285 }
1286
1287 if ((mask & ~(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)) != 0)
1288 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001289 context->handleError(InvalidValue());
Geoff Langb1196682014-07-23 13:47:29 -04001290 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001291 }
1292
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001293 // ES3.0 spec, section 4.3.2 states that linear filtering is only available for the
1294 // color buffer, leaving only nearest being unfiltered from above
1295 if ((mask & ~GL_COLOR_BUFFER_BIT) != 0 && filter != GL_NEAREST)
1296 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001297 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001298 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001299 }
1300
Jamie Madill51f40ec2016-06-15 14:06:00 -04001301 const auto &glState = context->getGLState();
1302 gl::Framebuffer *readFramebuffer = glState.getReadFramebuffer();
1303 gl::Framebuffer *drawFramebuffer = glState.getDrawFramebuffer();
Jamie Madill48faf802014-11-06 15:27:22 -05001304
1305 if (!readFramebuffer || !drawFramebuffer)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001306 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001307 context->handleError(InvalidFramebufferOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001308 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001309 }
1310
Jamie Madill427064d2018-04-13 16:20:34 -04001311 if (!ValidateFramebufferComplete(context, readFramebuffer))
Jamie Madill48faf802014-11-06 15:27:22 -05001312 {
Jamie Madill48faf802014-11-06 15:27:22 -05001313 return false;
1314 }
1315
Jamie Madill427064d2018-04-13 16:20:34 -04001316 if (!ValidateFramebufferComplete(context, drawFramebuffer))
Jamie Madill48faf802014-11-06 15:27:22 -05001317 {
Jamie Madill48faf802014-11-06 15:27:22 -05001318 return false;
1319 }
1320
Qin Jiajiaaef92162018-02-27 13:51:44 +08001321 if (readFramebuffer->id() == drawFramebuffer->id())
1322 {
1323 context->handleError(InvalidOperation());
1324 return false;
1325 }
1326
Jamie Madille98b1b52018-03-08 09:47:23 -05001327 if (!ValidateFramebufferNotMultisampled(context, drawFramebuffer))
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001328 {
Geoff Langb1196682014-07-23 13:47:29 -04001329 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001330 }
1331
Olli Etuaho9aef81c2018-04-30 14:56:15 +03001332 // This validation is specified in the WebGL 2.0 spec and not in the GLES 3.0.5 spec, but we
1333 // always run it in order to avoid triggering driver bugs.
1334 if (DifferenceCanOverflow(srcX0, srcX1) || DifferenceCanOverflow(srcY0, srcY1) ||
1335 DifferenceCanOverflow(dstX0, dstX1) || DifferenceCanOverflow(dstY0, dstY1))
Olli Etuaho8d5571a2018-04-23 12:29:31 +03001336 {
Olli Etuaho9aef81c2018-04-30 14:56:15 +03001337 ANGLE_VALIDATION_ERR(context, InvalidValue(), BlitDimensionsOutOfRange);
1338 return false;
Olli Etuaho8d5571a2018-04-23 12:29:31 +03001339 }
1340
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001341 bool sameBounds = srcX0 == dstX0 && srcY0 == dstY0 && srcX1 == dstX1 && srcY1 == dstY1;
1342
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001343 if (mask & GL_COLOR_BUFFER_BIT)
1344 {
Jamie Madillb6bda4a2015-04-20 12:53:26 -04001345 const gl::FramebufferAttachment *readColorBuffer = readFramebuffer->getReadColorbuffer();
Jamie Madill6163c752015-12-07 16:32:59 -05001346 const Extensions &extensions = context->getExtensions();
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001347
He Yunchao66a41a22016-12-15 16:45:05 +08001348 if (readColorBuffer)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001349 {
Jamie Madilla3944d42016-07-22 22:13:26 -04001350 const Format &readFormat = readColorBuffer->getFormat();
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001351
Geoff Langa15472a2015-08-11 11:48:03 -04001352 for (size_t drawbufferIdx = 0;
1353 drawbufferIdx < drawFramebuffer->getDrawbufferStateCount(); ++drawbufferIdx)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001354 {
Geoff Langa15472a2015-08-11 11:48:03 -04001355 const FramebufferAttachment *attachment =
1356 drawFramebuffer->getDrawBuffer(drawbufferIdx);
1357 if (attachment)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001358 {
Jamie Madilla3944d42016-07-22 22:13:26 -04001359 const Format &drawFormat = attachment->getFormat();
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001360
Geoff Langb2f3d052013-08-13 12:49:27 -04001361 // The GL ES 3.0.2 spec (pg 193) states that:
1362 // 1) If the read buffer is fixed point format, the draw buffer must be as well
He Yunchaoced53ae2016-11-29 15:00:51 +08001363 // 2) If the read buffer is an unsigned integer format, the draw buffer must be
1364 // as well
1365 // 3) If the read buffer is a signed integer format, the draw buffer must be as
1366 // well
Jamie Madill6163c752015-12-07 16:32:59 -05001367 // Changes with EXT_color_buffer_float:
1368 // Case 1) is changed to fixed point OR floating point
Jamie Madilla3944d42016-07-22 22:13:26 -04001369 GLenum readComponentType = readFormat.info->componentType;
1370 GLenum drawComponentType = drawFormat.info->componentType;
He Yunchaoced53ae2016-11-29 15:00:51 +08001371 bool readFixedPoint = (readComponentType == GL_UNSIGNED_NORMALIZED ||
Jamie Madill6163c752015-12-07 16:32:59 -05001372 readComponentType == GL_SIGNED_NORMALIZED);
Lingfeng Yang038dd532018-03-29 17:31:52 -07001373 bool drawFixedPoint = (drawComponentType == GL_UNSIGNED_NORMALIZED ||
Jamie Madill6163c752015-12-07 16:32:59 -05001374 drawComponentType == GL_SIGNED_NORMALIZED);
1375
1376 if (extensions.colorBufferFloat)
1377 {
1378 bool readFixedOrFloat = (readFixedPoint || readComponentType == GL_FLOAT);
1379 bool drawFixedOrFloat = (drawFixedPoint || drawComponentType == GL_FLOAT);
1380
1381 if (readFixedOrFloat != drawFixedOrFloat)
1382 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001383 context->handleError(InvalidOperation()
1384 << "If the read buffer contains fixed-point or "
1385 "floating-point values, the draw buffer must "
1386 "as well.");
Jamie Madill6163c752015-12-07 16:32:59 -05001387 return false;
1388 }
1389 }
1390 else if (readFixedPoint != drawFixedPoint)
1391 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001392 context->handleError(InvalidOperation()
1393 << "If the read buffer contains fixed-point values, "
1394 "the draw buffer must as well.");
Jamie Madill6163c752015-12-07 16:32:59 -05001395 return false;
1396 }
1397
1398 if (readComponentType == GL_UNSIGNED_INT &&
1399 drawComponentType != GL_UNSIGNED_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001400 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001401 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001402 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001403 }
1404
Jamie Madill6163c752015-12-07 16:32:59 -05001405 if (readComponentType == GL_INT && drawComponentType != GL_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001406 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001407 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001408 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001409 }
1410
Jamie Madilla3944d42016-07-22 22:13:26 -04001411 if (readColorBuffer->getSamples() > 0 &&
Kenneth Russell69382852017-07-21 16:38:44 -04001412 (!Format::EquivalentForBlit(readFormat, drawFormat) || !sameBounds))
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001413 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001414 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001415 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001416 }
Geoff Lange4915782017-04-12 15:19:07 -04001417
1418 if (context->getExtensions().webglCompatibility &&
1419 *readColorBuffer == *attachment)
1420 {
1421 context->handleError(
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001422 InvalidOperation()
1423 << "Read and write color attachments cannot be the same image.");
Geoff Lange4915782017-04-12 15:19:07 -04001424 return false;
1425 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001426 }
1427 }
1428
Jamie Madilla3944d42016-07-22 22:13:26 -04001429 if ((readFormat.info->componentType == GL_INT ||
1430 readFormat.info->componentType == GL_UNSIGNED_INT) &&
1431 filter == GL_LINEAR)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001432 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001433 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001434 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001435 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001436 }
He Yunchao66a41a22016-12-15 16:45:05 +08001437 // WebGL 2.0 BlitFramebuffer when blitting from a missing attachment
1438 // In OpenGL ES it is undefined what happens when an operation tries to blit from a missing
1439 // attachment and WebGL defines it to be an error. We do the check unconditionally as the
1440 // situation is an application error that would lead to a crash in ANGLE.
1441 else if (drawFramebuffer->hasEnabledDrawBuffer())
1442 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001443 context->handleError(
1444 InvalidOperation()
1445 << "Attempt to read from a missing color attachment of a complete framebuffer.");
He Yunchao66a41a22016-12-15 16:45:05 +08001446 return false;
1447 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001448 }
1449
He Yunchaoced53ae2016-11-29 15:00:51 +08001450 GLenum masks[] = {GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT};
Dongseong Hwang44b422c2014-12-09 15:42:01 +02001451 GLenum attachments[] = {GL_DEPTH_ATTACHMENT, GL_STENCIL_ATTACHMENT};
1452 for (size_t i = 0; i < 2; i++)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001453 {
Dongseong Hwang44b422c2014-12-09 15:42:01 +02001454 if (mask & masks[i])
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001455 {
He Yunchaoced53ae2016-11-29 15:00:51 +08001456 const gl::FramebufferAttachment *readBuffer =
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -08001457 readFramebuffer->getAttachment(context, attachments[i]);
He Yunchaoced53ae2016-11-29 15:00:51 +08001458 const gl::FramebufferAttachment *drawBuffer =
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -08001459 drawFramebuffer->getAttachment(context, attachments[i]);
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001460
Dongseong Hwang44b422c2014-12-09 15:42:01 +02001461 if (readBuffer && drawBuffer)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001462 {
Kenneth Russell69382852017-07-21 16:38:44 -04001463 if (!Format::EquivalentForBlit(readBuffer->getFormat(), drawBuffer->getFormat()))
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001464 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001465 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001466 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001467 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001468
Dongseong Hwang44b422c2014-12-09 15:42:01 +02001469 if (readBuffer->getSamples() > 0 && !sameBounds)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001470 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001471 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04001472 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001473 }
Geoff Lange4915782017-04-12 15:19:07 -04001474
1475 if (context->getExtensions().webglCompatibility && *readBuffer == *drawBuffer)
1476 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001477 context->handleError(
1478 InvalidOperation()
1479 << "Read and write depth stencil attachments cannot be the same image.");
Geoff Lange4915782017-04-12 15:19:07 -04001480 return false;
1481 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001482 }
He Yunchao66a41a22016-12-15 16:45:05 +08001483 // WebGL 2.0 BlitFramebuffer when blitting from a missing attachment
1484 else if (drawBuffer)
1485 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001486 context->handleError(InvalidOperation() << "Attempt to read from a missing "
1487 "depth/stencil attachment of a "
1488 "complete framebuffer.");
He Yunchao66a41a22016-12-15 16:45:05 +08001489 return false;
1490 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001491 }
1492 }
1493
Martin Radeva3ed4572017-07-27 18:29:37 +03001494 // ANGLE_multiview, Revision 1:
1495 // Calling BlitFramebuffer will result in an INVALID_FRAMEBUFFER_OPERATION error if the
1496 // multi-view layout of the current draw framebuffer or read framebuffer is not NONE.
1497 if (readFramebuffer->getMultiviewLayout() != GL_NONE)
1498 {
1499 context->handleError(InvalidFramebufferOperation()
1500 << "Attempt to read from a multi-view framebuffer.");
1501 return false;
1502 }
1503 if (drawFramebuffer->getMultiviewLayout() != GL_NONE)
1504 {
1505 context->handleError(InvalidFramebufferOperation()
1506 << "Attempt to write to a multi-view framebuffer.");
1507 return false;
1508 }
1509
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001510 return true;
1511}
1512
Jamie Madill4928b7c2017-06-20 12:57:39 -04001513bool ValidateReadPixelsRobustANGLE(Context *context,
Geoff Lang62fce5b2016-09-30 10:46:35 -04001514 GLint x,
1515 GLint y,
1516 GLsizei width,
1517 GLsizei height,
1518 GLenum format,
1519 GLenum type,
1520 GLsizei bufSize,
1521 GLsizei *length,
Geoff Lange93daba2017-03-30 13:54:40 -04001522 GLsizei *columns,
1523 GLsizei *rows,
Jamie Madill876429b2017-04-20 15:46:24 -04001524 void *pixels)
Geoff Lang62fce5b2016-09-30 10:46:35 -04001525{
1526 if (!ValidateRobustEntryPoint(context, bufSize))
Jamie Madillc29968b2016-01-20 11:17:23 -05001527 {
Jamie Madillc29968b2016-01-20 11:17:23 -05001528 return false;
1529 }
1530
Brandon Jonesd1049182018-03-28 10:02:20 -07001531 GLsizei writeLength = 0;
1532 GLsizei writeColumns = 0;
1533 GLsizei writeRows = 0;
1534
1535 if (!ValidateReadPixelsBase(context, x, y, width, height, format, type, bufSize, &writeLength,
1536 &writeColumns, &writeRows, pixels))
Jamie Madill26e91952014-03-05 15:01:27 -05001537 {
Geoff Langb1196682014-07-23 13:47:29 -04001538 return false;
Jamie Madill26e91952014-03-05 15:01:27 -05001539 }
1540
Brandon Jonesd1049182018-03-28 10:02:20 -07001541 if (!ValidateRobustBufferSize(context, bufSize, writeLength))
Jamie Madill26e91952014-03-05 15:01:27 -05001542 {
Geoff Langb1196682014-07-23 13:47:29 -04001543 return false;
Jamie Madill26e91952014-03-05 15:01:27 -05001544 }
1545
Brandon Jonesd1049182018-03-28 10:02:20 -07001546 SetRobustLengthParam(length, writeLength);
1547 SetRobustLengthParam(columns, writeColumns);
1548 SetRobustLengthParam(rows, writeRows);
1549
Jamie Madillc29968b2016-01-20 11:17:23 -05001550 return true;
1551}
1552
1553bool ValidateReadnPixelsEXT(Context *context,
1554 GLint x,
1555 GLint y,
1556 GLsizei width,
1557 GLsizei height,
1558 GLenum format,
1559 GLenum type,
1560 GLsizei bufSize,
Jamie Madill876429b2017-04-20 15:46:24 -04001561 void *pixels)
Jamie Madillc29968b2016-01-20 11:17:23 -05001562{
1563 if (bufSize < 0)
1564 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001565 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeBufferSize);
Jamie Madillc29968b2016-01-20 11:17:23 -05001566 return false;
1567 }
1568
Geoff Lang62fce5b2016-09-30 10:46:35 -04001569 return ValidateReadPixelsBase(context, x, y, width, height, format, type, bufSize, nullptr,
Geoff Lange93daba2017-03-30 13:54:40 -04001570 nullptr, nullptr, pixels);
Geoff Lang62fce5b2016-09-30 10:46:35 -04001571}
Jamie Madill26e91952014-03-05 15:01:27 -05001572
Jamie Madill4928b7c2017-06-20 12:57:39 -04001573bool ValidateReadnPixelsRobustANGLE(Context *context,
Geoff Lang62fce5b2016-09-30 10:46:35 -04001574 GLint x,
1575 GLint y,
1576 GLsizei width,
1577 GLsizei height,
1578 GLenum format,
1579 GLenum type,
1580 GLsizei bufSize,
1581 GLsizei *length,
Geoff Lange93daba2017-03-30 13:54:40 -04001582 GLsizei *columns,
1583 GLsizei *rows,
Jamie Madill876429b2017-04-20 15:46:24 -04001584 void *data)
Geoff Lang62fce5b2016-09-30 10:46:35 -04001585{
Brandon Jonesd1049182018-03-28 10:02:20 -07001586 GLsizei writeLength = 0;
1587 GLsizei writeColumns = 0;
1588 GLsizei writeRows = 0;
1589
Geoff Lang62fce5b2016-09-30 10:46:35 -04001590 if (!ValidateRobustEntryPoint(context, bufSize))
Jamie Madille2e406c2016-06-02 13:04:10 -04001591 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001592 return false;
1593 }
1594
Brandon Jonesd1049182018-03-28 10:02:20 -07001595 if (!ValidateReadPixelsBase(context, x, y, width, height, format, type, bufSize, &writeLength,
1596 &writeColumns, &writeRows, data))
Jamie Madille2e406c2016-06-02 13:04:10 -04001597 {
Jamie Madillc29968b2016-01-20 11:17:23 -05001598 return false;
Jamie Madill26e91952014-03-05 15:01:27 -05001599 }
1600
Brandon Jonesd1049182018-03-28 10:02:20 -07001601 if (!ValidateRobustBufferSize(context, bufSize, writeLength))
Geoff Lang62fce5b2016-09-30 10:46:35 -04001602 {
1603 return false;
1604 }
1605
Brandon Jonesd1049182018-03-28 10:02:20 -07001606 SetRobustLengthParam(length, writeLength);
1607 SetRobustLengthParam(columns, writeColumns);
1608 SetRobustLengthParam(rows, writeRows);
1609
Geoff Lang62fce5b2016-09-30 10:46:35 -04001610 return true;
Jamie Madill26e91952014-03-05 15:01:27 -05001611}
1612
Jamie Madillf0e04492017-08-26 15:28:42 -04001613bool ValidateGenQueriesEXT(gl::Context *context, GLsizei n, GLuint *ids)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001614{
1615 if (!context->getExtensions().occlusionQueryBoolean &&
1616 !context->getExtensions().disjointTimerQuery)
1617 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001618 ANGLE_VALIDATION_ERR(context, InvalidOperation(), QueryExtensionNotEnabled);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001619 return false;
1620 }
1621
Olli Etuaho41997e72016-03-10 13:38:39 +02001622 return ValidateGenOrDelete(context, n);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001623}
1624
Jamie Madillf0e04492017-08-26 15:28:42 -04001625bool ValidateDeleteQueriesEXT(gl::Context *context, GLsizei n, const GLuint *ids)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001626{
1627 if (!context->getExtensions().occlusionQueryBoolean &&
1628 !context->getExtensions().disjointTimerQuery)
1629 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001630 ANGLE_VALIDATION_ERR(context, InvalidOperation(), QueryExtensionNotEnabled);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001631 return false;
1632 }
1633
Olli Etuaho41997e72016-03-10 13:38:39 +02001634 return ValidateGenOrDelete(context, n);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001635}
1636
Jamie Madillf0e04492017-08-26 15:28:42 -04001637bool ValidateIsQueryEXT(gl::Context *context, GLuint id)
1638{
1639 if (!context->getExtensions().occlusionQueryBoolean &&
1640 !context->getExtensions().disjointTimerQuery)
1641 {
1642 ANGLE_VALIDATION_ERR(context, InvalidOperation(), QueryExtensionNotEnabled);
1643 return false;
1644 }
1645
1646 return true;
1647}
1648
Corentin Wallezad3ae902018-03-09 13:40:42 -05001649bool ValidateBeginQueryBase(gl::Context *context, QueryType target, GLuint id)
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001650{
1651 if (!ValidQueryType(context, target))
1652 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001653 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidQueryType);
Geoff Langb1196682014-07-23 13:47:29 -04001654 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001655 }
1656
1657 if (id == 0)
1658 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001659 context->handleError(InvalidOperation() << "Query id is 0");
Geoff Langb1196682014-07-23 13:47:29 -04001660 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001661 }
1662
1663 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
1664 // of zero, if the active query object name for <target> is non-zero (for the
1665 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
1666 // the active query for either target is non-zero), if <id> is the name of an
1667 // existing query object whose type does not match <target>, or if <id> is the
1668 // active query object name for any query type, the error INVALID_OPERATION is
1669 // generated.
1670
1671 // Ensure no other queries are active
1672 // NOTE: If other queries than occlusion are supported, we will need to check
1673 // separately that:
1674 // a) The query ID passed is not the current active query for any target/type
1675 // b) There are no active queries for the requested target (and in the case
1676 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
1677 // no query may be active for either if glBeginQuery targets either.
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001678
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001679 if (context->getGLState().isQueryActive(target))
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001680 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001681 context->handleError(InvalidOperation() << "Other query is active");
Geoff Langb1196682014-07-23 13:47:29 -04001682 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001683 }
1684
1685 Query *queryObject = context->getQuery(id, true, target);
1686
1687 // check that name was obtained with glGenQueries
1688 if (!queryObject)
1689 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001690 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidQueryId);
Geoff Langb1196682014-07-23 13:47:29 -04001691 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001692 }
1693
1694 // check for type mismatch
1695 if (queryObject->getType() != target)
1696 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001697 context->handleError(InvalidOperation() << "Query type does not match target");
Geoff Langb1196682014-07-23 13:47:29 -04001698 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001699 }
1700
1701 return true;
1702}
1703
Corentin Wallezad3ae902018-03-09 13:40:42 -05001704bool ValidateBeginQueryEXT(gl::Context *context, QueryType target, GLuint id)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001705{
1706 if (!context->getExtensions().occlusionQueryBoolean &&
Geoff Lang2b4ce802016-04-28 13:34:50 -04001707 !context->getExtensions().disjointTimerQuery && !context->getExtensions().syncQuery)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001708 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001709 ANGLE_VALIDATION_ERR(context, InvalidOperation(), QueryExtensionNotEnabled);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001710 return false;
1711 }
1712
1713 return ValidateBeginQueryBase(context, target, id);
1714}
1715
Corentin Wallezad3ae902018-03-09 13:40:42 -05001716bool ValidateEndQueryBase(gl::Context *context, QueryType target)
Jamie Madill45c785d2014-05-13 14:09:34 -04001717{
1718 if (!ValidQueryType(context, target))
1719 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001720 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidQueryType);
Geoff Langb1196682014-07-23 13:47:29 -04001721 return false;
Jamie Madill45c785d2014-05-13 14:09:34 -04001722 }
1723
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001724 const Query *queryObject = context->getGLState().getActiveQuery(target);
Jamie Madill45c785d2014-05-13 14:09:34 -04001725
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001726 if (queryObject == nullptr)
Jamie Madill45c785d2014-05-13 14:09:34 -04001727 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001728 context->handleError(InvalidOperation() << "Query target not active");
Geoff Langb1196682014-07-23 13:47:29 -04001729 return false;
Jamie Madill45c785d2014-05-13 14:09:34 -04001730 }
1731
Jamie Madill45c785d2014-05-13 14:09:34 -04001732 return true;
1733}
1734
Corentin Wallezad3ae902018-03-09 13:40:42 -05001735bool ValidateEndQueryEXT(gl::Context *context, QueryType target)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001736{
1737 if (!context->getExtensions().occlusionQueryBoolean &&
Geoff Lang2b4ce802016-04-28 13:34:50 -04001738 !context->getExtensions().disjointTimerQuery && !context->getExtensions().syncQuery)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001739 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001740 ANGLE_VALIDATION_ERR(context, InvalidOperation(), QueryExtensionNotEnabled);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001741 return false;
1742 }
1743
1744 return ValidateEndQueryBase(context, target);
1745}
1746
Corentin Wallezad3ae902018-03-09 13:40:42 -05001747bool ValidateQueryCounterEXT(Context *context, GLuint id, QueryType target)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001748{
1749 if (!context->getExtensions().disjointTimerQuery)
1750 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001751 context->handleError(InvalidOperation() << "Disjoint timer query not enabled");
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001752 return false;
1753 }
1754
Corentin Wallezad3ae902018-03-09 13:40:42 -05001755 if (target != QueryType::Timestamp)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001756 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001757 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidQueryTarget);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001758 return false;
1759 }
1760
1761 Query *queryObject = context->getQuery(id, true, target);
1762 if (queryObject == nullptr)
1763 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001764 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidQueryId);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001765 return false;
1766 }
1767
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001768 if (context->getGLState().isQueryActive(queryObject))
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001769 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001770 ANGLE_VALIDATION_ERR(context, InvalidOperation(), QueryActive);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001771 return false;
1772 }
1773
1774 return true;
1775}
1776
Corentin Wallezad3ae902018-03-09 13:40:42 -05001777bool ValidateGetQueryivBase(Context *context, QueryType target, GLenum pname, GLsizei *numParams)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001778{
Geoff Lang2186c382016-10-14 10:54:54 -04001779 if (numParams)
1780 {
1781 *numParams = 0;
1782 }
1783
Corentin Wallezad3ae902018-03-09 13:40:42 -05001784 if (!ValidQueryType(context, target) && target != QueryType::Timestamp)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001785 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001786 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidQueryType);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001787 return false;
1788 }
1789
1790 switch (pname)
1791 {
1792 case GL_CURRENT_QUERY_EXT:
Corentin Wallezad3ae902018-03-09 13:40:42 -05001793 if (target == QueryType::Timestamp)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001794 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001795 context->handleError(InvalidEnum() << "Cannot use current query for timestamp");
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001796 return false;
1797 }
1798 break;
1799 case GL_QUERY_COUNTER_BITS_EXT:
1800 if (!context->getExtensions().disjointTimerQuery ||
Corentin Wallezad3ae902018-03-09 13:40:42 -05001801 (target != QueryType::Timestamp && target != QueryType::TimeElapsed))
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001802 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001803 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidPname);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001804 return false;
1805 }
1806 break;
1807 default:
Brandon Jonesafa75152017-07-21 13:11:29 -07001808 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidPname);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001809 return false;
1810 }
1811
Geoff Lang2186c382016-10-14 10:54:54 -04001812 if (numParams)
1813 {
1814 // All queries return only one value
1815 *numParams = 1;
1816 }
1817
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001818 return true;
1819}
1820
Corentin Wallezad3ae902018-03-09 13:40:42 -05001821bool ValidateGetQueryivEXT(Context *context, QueryType target, GLenum pname, GLint *params)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001822{
1823 if (!context->getExtensions().occlusionQueryBoolean &&
Geoff Lang2b4ce802016-04-28 13:34:50 -04001824 !context->getExtensions().disjointTimerQuery && !context->getExtensions().syncQuery)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001825 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001826 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001827 return false;
1828 }
1829
Geoff Lang2186c382016-10-14 10:54:54 -04001830 return ValidateGetQueryivBase(context, target, pname, nullptr);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001831}
1832
Geoff Lang2186c382016-10-14 10:54:54 -04001833bool ValidateGetQueryivRobustANGLE(Context *context,
Corentin Wallezad3ae902018-03-09 13:40:42 -05001834 QueryType target,
Geoff Lang2186c382016-10-14 10:54:54 -04001835 GLenum pname,
1836 GLsizei bufSize,
1837 GLsizei *length,
1838 GLint *params)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001839{
Geoff Lang2186c382016-10-14 10:54:54 -04001840 if (!ValidateRobustEntryPoint(context, bufSize))
1841 {
1842 return false;
1843 }
1844
Brandon Jonesd1049182018-03-28 10:02:20 -07001845 GLsizei numParams = 0;
1846
1847 if (!ValidateGetQueryivBase(context, target, pname, &numParams))
Geoff Lang2186c382016-10-14 10:54:54 -04001848 {
1849 return false;
1850 }
1851
Brandon Jonesd1049182018-03-28 10:02:20 -07001852 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Lang2186c382016-10-14 10:54:54 -04001853 {
1854 return false;
1855 }
1856
Brandon Jonesd1049182018-03-28 10:02:20 -07001857 SetRobustLengthParam(length, numParams);
1858
Geoff Lang2186c382016-10-14 10:54:54 -04001859 return true;
1860}
1861
1862bool ValidateGetQueryObjectValueBase(Context *context, GLuint id, GLenum pname, GLsizei *numParams)
1863{
1864 if (numParams)
1865 {
1866 *numParams = 0;
1867 }
1868
Corentin Wallezad3ae902018-03-09 13:40:42 -05001869 Query *queryObject = context->getQuery(id, false, QueryType::InvalidEnum);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001870
1871 if (!queryObject)
1872 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001873 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidQueryId);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001874 return false;
1875 }
1876
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001877 if (context->getGLState().isQueryActive(queryObject))
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001878 {
Brandon Jonesafa75152017-07-21 13:11:29 -07001879 ANGLE_VALIDATION_ERR(context, InvalidOperation(), QueryActive);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001880 return false;
1881 }
1882
1883 switch (pname)
1884 {
1885 case GL_QUERY_RESULT_EXT:
1886 case GL_QUERY_RESULT_AVAILABLE_EXT:
1887 break;
1888
1889 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07001890 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001891 return false;
1892 }
1893
Geoff Lang2186c382016-10-14 10:54:54 -04001894 if (numParams)
1895 {
1896 *numParams = 1;
1897 }
1898
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001899 return true;
1900}
1901
1902bool ValidateGetQueryObjectivEXT(Context *context, GLuint id, GLenum pname, GLint *params)
1903{
1904 if (!context->getExtensions().disjointTimerQuery)
1905 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001906 context->handleError(InvalidOperation() << "Timer query extension not enabled");
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001907 return false;
1908 }
Geoff Lang2186c382016-10-14 10:54:54 -04001909 return ValidateGetQueryObjectValueBase(context, id, pname, nullptr);
1910}
1911
1912bool ValidateGetQueryObjectivRobustANGLE(Context *context,
1913 GLuint id,
1914 GLenum pname,
1915 GLsizei bufSize,
1916 GLsizei *length,
1917 GLint *params)
1918{
1919 if (!context->getExtensions().disjointTimerQuery)
1920 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001921 context->handleError(InvalidOperation() << "Timer query extension not enabled");
Geoff Lang2186c382016-10-14 10:54:54 -04001922 return false;
1923 }
1924
1925 if (!ValidateRobustEntryPoint(context, bufSize))
1926 {
1927 return false;
1928 }
1929
Brandon Jonesd1049182018-03-28 10:02:20 -07001930 GLsizei numParams = 0;
1931
1932 if (!ValidateGetQueryObjectValueBase(context, id, pname, &numParams))
Geoff Lang2186c382016-10-14 10:54:54 -04001933 {
1934 return false;
1935 }
1936
Brandon Jonesd1049182018-03-28 10:02:20 -07001937 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Lang2186c382016-10-14 10:54:54 -04001938 {
1939 return false;
1940 }
1941
Brandon Jonesd1049182018-03-28 10:02:20 -07001942 SetRobustLengthParam(length, numParams);
1943
Geoff Lang2186c382016-10-14 10:54:54 -04001944 return true;
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001945}
1946
1947bool ValidateGetQueryObjectuivEXT(Context *context, GLuint id, GLenum pname, GLuint *params)
1948{
1949 if (!context->getExtensions().disjointTimerQuery &&
Geoff Lang2b4ce802016-04-28 13:34:50 -04001950 !context->getExtensions().occlusionQueryBoolean && !context->getExtensions().syncQuery)
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001951 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001952 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001953 return false;
1954 }
Geoff Lang2186c382016-10-14 10:54:54 -04001955 return ValidateGetQueryObjectValueBase(context, id, pname, nullptr);
1956}
1957
1958bool ValidateGetQueryObjectuivRobustANGLE(Context *context,
1959 GLuint id,
1960 GLenum pname,
1961 GLsizei bufSize,
1962 GLsizei *length,
1963 GLuint *params)
1964{
1965 if (!context->getExtensions().disjointTimerQuery &&
1966 !context->getExtensions().occlusionQueryBoolean && !context->getExtensions().syncQuery)
1967 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001968 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
Geoff Lang2186c382016-10-14 10:54:54 -04001969 return false;
1970 }
1971
1972 if (!ValidateRobustEntryPoint(context, bufSize))
1973 {
1974 return false;
1975 }
1976
Brandon Jonesd1049182018-03-28 10:02:20 -07001977 GLsizei numParams = 0;
1978
1979 if (!ValidateGetQueryObjectValueBase(context, id, pname, &numParams))
Geoff Lang2186c382016-10-14 10:54:54 -04001980 {
1981 return false;
1982 }
1983
Brandon Jonesd1049182018-03-28 10:02:20 -07001984 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Lang2186c382016-10-14 10:54:54 -04001985 {
1986 return false;
1987 }
1988
Brandon Jonesd1049182018-03-28 10:02:20 -07001989 SetRobustLengthParam(length, numParams);
1990
Geoff Lang2186c382016-10-14 10:54:54 -04001991 return true;
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001992}
1993
1994bool ValidateGetQueryObjecti64vEXT(Context *context, GLuint id, GLenum pname, GLint64 *params)
1995{
1996 if (!context->getExtensions().disjointTimerQuery)
1997 {
Brandon Jones6cad5662017-06-14 13:25:13 -07001998 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001999 return false;
2000 }
Geoff Lang2186c382016-10-14 10:54:54 -04002001 return ValidateGetQueryObjectValueBase(context, id, pname, nullptr);
2002}
2003
2004bool ValidateGetQueryObjecti64vRobustANGLE(Context *context,
2005 GLuint id,
2006 GLenum pname,
2007 GLsizei bufSize,
2008 GLsizei *length,
2009 GLint64 *params)
2010{
2011 if (!context->getExtensions().disjointTimerQuery)
2012 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002013 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
Geoff Lang2186c382016-10-14 10:54:54 -04002014 return false;
2015 }
2016
2017 if (!ValidateRobustEntryPoint(context, bufSize))
2018 {
2019 return false;
2020 }
2021
Brandon Jonesd1049182018-03-28 10:02:20 -07002022 GLsizei numParams = 0;
2023
2024 if (!ValidateGetQueryObjectValueBase(context, id, pname, &numParams))
Geoff Lang2186c382016-10-14 10:54:54 -04002025 {
2026 return false;
2027 }
2028
Brandon Jonesd1049182018-03-28 10:02:20 -07002029 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Lang2186c382016-10-14 10:54:54 -04002030 {
2031 return false;
2032 }
2033
Brandon Jonesd1049182018-03-28 10:02:20 -07002034 SetRobustLengthParam(length, numParams);
2035
Geoff Lang2186c382016-10-14 10:54:54 -04002036 return true;
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002037}
2038
2039bool ValidateGetQueryObjectui64vEXT(Context *context, GLuint id, GLenum pname, GLuint64 *params)
2040{
2041 if (!context->getExtensions().disjointTimerQuery)
2042 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002043 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002044 return false;
2045 }
Geoff Lang2186c382016-10-14 10:54:54 -04002046 return ValidateGetQueryObjectValueBase(context, id, pname, nullptr);
2047}
2048
2049bool ValidateGetQueryObjectui64vRobustANGLE(Context *context,
2050 GLuint id,
2051 GLenum pname,
2052 GLsizei bufSize,
2053 GLsizei *length,
2054 GLuint64 *params)
2055{
2056 if (!context->getExtensions().disjointTimerQuery)
2057 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002058 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
Geoff Lang2186c382016-10-14 10:54:54 -04002059 return false;
2060 }
2061
2062 if (!ValidateRobustEntryPoint(context, bufSize))
2063 {
2064 return false;
2065 }
2066
Brandon Jonesd1049182018-03-28 10:02:20 -07002067 GLsizei numParams = 0;
2068
2069 if (!ValidateGetQueryObjectValueBase(context, id, pname, &numParams))
Geoff Lang2186c382016-10-14 10:54:54 -04002070 {
2071 return false;
2072 }
2073
Brandon Jonesd1049182018-03-28 10:02:20 -07002074 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Lang2186c382016-10-14 10:54:54 -04002075 {
2076 return false;
2077 }
2078
Brandon Jonesd1049182018-03-28 10:02:20 -07002079 SetRobustLengthParam(length, numParams);
2080
Geoff Lang2186c382016-10-14 10:54:54 -04002081 return true;
Ian Ewell3ffd78b2016-01-22 16:09:42 -05002082}
2083
Jamie Madill5b772312018-03-08 20:28:32 -05002084bool ValidateUniformCommonBase(Context *context,
Jiajia Qin5451d532017-11-16 17:16:34 +08002085 gl::Program *program,
Frank Henigmana98a6472017-02-02 21:38:32 -05002086 GLint location,
2087 GLsizei count,
Jiajia Qin5451d532017-11-16 17:16:34 +08002088 const LinkedUniform **uniformOut)
Frank Henigmana98a6472017-02-02 21:38:32 -05002089{
Jiajia Qin5451d532017-11-16 17:16:34 +08002090 // TODO(Jiajia): Add image uniform check in future.
2091 if (count < 0)
Frank Henigmana98a6472017-02-02 21:38:32 -05002092 {
Jiajia Qin5451d532017-11-16 17:16:34 +08002093 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeCount);
Frank Henigmana98a6472017-02-02 21:38:32 -05002094 return false;
2095 }
2096
Jiajia Qin5451d532017-11-16 17:16:34 +08002097 if (!program)
2098 {
2099 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidProgramName);
2100 return false;
2101 }
2102
2103 if (!program->isLinked())
2104 {
2105 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ProgramNotLinked);
2106 return false;
2107 }
2108
2109 if (location == -1)
2110 {
2111 // Silently ignore the uniform command
2112 return false;
2113 }
2114
2115 const auto &uniformLocations = program->getUniformLocations();
2116 size_t castedLocation = static_cast<size_t>(location);
2117 if (castedLocation >= uniformLocations.size())
2118 {
2119 context->handleError(InvalidOperation() << "Invalid uniform location");
2120 return false;
2121 }
2122
2123 const auto &uniformLocation = uniformLocations[castedLocation];
2124 if (uniformLocation.ignored)
2125 {
2126 // Silently ignore the uniform command
2127 return false;
2128 }
2129
2130 if (!uniformLocation.used())
2131 {
2132 context->handleError(InvalidOperation());
2133 return false;
2134 }
2135
2136 const auto &uniform = program->getUniformByIndex(uniformLocation.index);
2137
2138 // attempting to write an array to a non-array uniform is an INVALID_OPERATION
Jamie Madill2fc08062018-05-10 15:10:55 -04002139 if (count > 1 && !uniform.isArray())
Jiajia Qin5451d532017-11-16 17:16:34 +08002140 {
2141 context->handleError(InvalidOperation());
2142 return false;
2143 }
2144
2145 *uniformOut = &uniform;
2146 return true;
Frank Henigmana98a6472017-02-02 21:38:32 -05002147}
2148
Jamie Madill5b772312018-03-08 20:28:32 -05002149bool ValidateUniform1ivValue(Context *context,
Jiajia Qin5451d532017-11-16 17:16:34 +08002150 GLenum uniformType,
2151 GLsizei count,
2152 const GLint *value)
Jiajia Qinee9f08c2016-11-16 10:06:10 +08002153{
Jiajia Qin5451d532017-11-16 17:16:34 +08002154 // Value type is GL_INT, because we only get here from glUniform1i{v}.
2155 // It is compatible with INT or BOOL.
2156 // Do these cheap tests first, for a little extra speed.
2157 if (GL_INT == uniformType || GL_BOOL == uniformType)
Jiajia Qinee9f08c2016-11-16 10:06:10 +08002158 {
Jiajia Qin5451d532017-11-16 17:16:34 +08002159 return true;
Jiajia Qinee9f08c2016-11-16 10:06:10 +08002160 }
2161
Jiajia Qin5451d532017-11-16 17:16:34 +08002162 if (IsSamplerType(uniformType))
2163 {
2164 // Check that the values are in range.
2165 const GLint max = context->getCaps().maxCombinedTextureImageUnits;
2166 for (GLsizei i = 0; i < count; ++i)
2167 {
2168 if (value[i] < 0 || value[i] >= max)
2169 {
2170 context->handleError(InvalidValue() << "sampler uniform value out of range");
2171 return false;
2172 }
2173 }
2174 return true;
2175 }
2176
2177 context->handleError(InvalidOperation() << "wrong type of value for uniform");
2178 return false;
2179}
2180
Jamie Madill5b772312018-03-08 20:28:32 -05002181bool ValidateUniformValue(Context *context, GLenum valueType, GLenum uniformType)
Jiajia Qin5451d532017-11-16 17:16:34 +08002182{
2183 // Check that the value type is compatible with uniform type.
2184 // Do the cheaper test first, for a little extra speed.
2185 if (valueType == uniformType || VariableBoolVectorType(valueType) == uniformType)
2186 {
2187 return true;
2188 }
2189
2190 ANGLE_VALIDATION_ERR(context, InvalidOperation(), UniformSizeMismatch);
2191 return false;
2192}
2193
Jamie Madill5b772312018-03-08 20:28:32 -05002194bool ValidateUniformMatrixValue(Context *context, GLenum valueType, GLenum uniformType)
Jiajia Qin5451d532017-11-16 17:16:34 +08002195{
2196 // Check that the value type is compatible with uniform type.
2197 if (valueType == uniformType)
2198 {
2199 return true;
2200 }
2201
2202 context->handleError(InvalidOperation() << "wrong type of value for uniform");
2203 return false;
Jiajia Qinee9f08c2016-11-16 10:06:10 +08002204}
2205
Jamie Madill5b772312018-03-08 20:28:32 -05002206bool ValidateUniform(Context *context, GLenum valueType, GLint location, GLsizei count)
Jamie Madillaa981bd2014-05-20 10:55:55 -04002207{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002208 const LinkedUniform *uniform = nullptr;
Frank Henigmanf5f74ae2017-02-02 21:14:23 -05002209 gl::Program *programObject = context->getGLState().getProgram();
2210 return ValidateUniformCommonBase(context, programObject, location, count, &uniform) &&
2211 ValidateUniformValue(context, valueType, uniform->type);
Jamie Madillaa981bd2014-05-20 10:55:55 -04002212}
2213
Jamie Madill5b772312018-03-08 20:28:32 -05002214bool ValidateUniform1iv(Context *context, GLint location, GLsizei count, const GLint *value)
Frank Henigmana98a6472017-02-02 21:38:32 -05002215{
2216 const LinkedUniform *uniform = nullptr;
2217 gl::Program *programObject = context->getGLState().getProgram();
2218 return ValidateUniformCommonBase(context, programObject, location, count, &uniform) &&
2219 ValidateUniform1ivValue(context, uniform->type, count, value);
2220}
2221
Jamie Madill5b772312018-03-08 20:28:32 -05002222bool ValidateUniformMatrix(Context *context,
Frank Henigmanf5f74ae2017-02-02 21:14:23 -05002223 GLenum valueType,
He Yunchaoced53ae2016-11-29 15:00:51 +08002224 GLint location,
2225 GLsizei count,
Jamie Madillaa981bd2014-05-20 10:55:55 -04002226 GLboolean transpose)
2227{
Geoff Lang92019432017-11-20 13:09:34 -05002228 if (ConvertToBool(transpose) && context->getClientMajorVersion() < 3)
Jamie Madillaa981bd2014-05-20 10:55:55 -04002229 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002230 context->handleError(InvalidValue());
Geoff Langb1196682014-07-23 13:47:29 -04002231 return false;
Jamie Madillaa981bd2014-05-20 10:55:55 -04002232 }
2233
Jamie Madill62d31cb2015-09-11 13:25:51 -04002234 const LinkedUniform *uniform = nullptr;
Frank Henigmanf5f74ae2017-02-02 21:14:23 -05002235 gl::Program *programObject = context->getGLState().getProgram();
2236 return ValidateUniformCommonBase(context, programObject, location, count, &uniform) &&
2237 ValidateUniformMatrixValue(context, valueType, uniform->type);
Jamie Madillaa981bd2014-05-20 10:55:55 -04002238}
2239
Jamie Madill5b772312018-03-08 20:28:32 -05002240bool ValidateStateQuery(Context *context, GLenum pname, GLenum *nativeType, unsigned int *numParams)
Jamie Madill893ab082014-05-16 16:56:10 -04002241{
2242 if (!context->getQueryParameterInfo(pname, nativeType, numParams))
2243 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002244 context->handleError(InvalidEnum());
Geoff Langb1196682014-07-23 13:47:29 -04002245 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04002246 }
2247
Jamie Madill0af26e12015-03-05 19:54:33 -05002248 const Caps &caps = context->getCaps();
2249
Jamie Madill893ab082014-05-16 16:56:10 -04002250 if (pname >= GL_DRAW_BUFFER0 && pname <= GL_DRAW_BUFFER15)
2251 {
2252 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0);
2253
Jamie Madill0af26e12015-03-05 19:54:33 -05002254 if (colorAttachment >= caps.maxDrawBuffers)
Jamie Madill893ab082014-05-16 16:56:10 -04002255 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002256 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04002257 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04002258 }
2259 }
2260
2261 switch (pname)
2262 {
He Yunchaoced53ae2016-11-29 15:00:51 +08002263 case GL_TEXTURE_BINDING_2D:
2264 case GL_TEXTURE_BINDING_CUBE_MAP:
2265 case GL_TEXTURE_BINDING_3D:
2266 case GL_TEXTURE_BINDING_2D_ARRAY:
JiangYizhou24fe74c2017-07-06 16:56:50 +08002267 case GL_TEXTURE_BINDING_2D_MULTISAMPLE:
He Yunchaoced53ae2016-11-29 15:00:51 +08002268 break;
Corentin Wallez13c0dd42017-07-04 18:27:01 -04002269 case GL_TEXTURE_BINDING_RECTANGLE_ANGLE:
2270 if (!context->getExtensions().textureRectangle)
2271 {
2272 context->handleError(InvalidEnum()
2273 << "ANGLE_texture_rectangle extension not present");
2274 return false;
2275 }
2276 break;
He Yunchaoced53ae2016-11-29 15:00:51 +08002277 case GL_TEXTURE_BINDING_EXTERNAL_OES:
2278 if (!context->getExtensions().eglStreamConsumerExternal &&
2279 !context->getExtensions().eglImageExternal)
2280 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002281 context->handleError(InvalidEnum() << "Neither NV_EGL_stream_consumer_external "
2282 "nor GL_OES_EGL_image_external "
2283 "extensions enabled");
He Yunchaoced53ae2016-11-29 15:00:51 +08002284 return false;
2285 }
2286 break;
Jamie Madill893ab082014-05-16 16:56:10 -04002287
He Yunchaoced53ae2016-11-29 15:00:51 +08002288 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
2289 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
Jamie Madill893ab082014-05-16 16:56:10 -04002290 {
Jamie Madille98b1b52018-03-08 09:47:23 -05002291 Framebuffer *readFramebuffer = context->getGLState().getReadFramebuffer();
2292 ASSERT(readFramebuffer);
2293
Jamie Madill427064d2018-04-13 16:20:34 -04002294 if (!ValidateFramebufferComplete<InvalidOperation>(context, readFramebuffer))
Jamie Madill893ab082014-05-16 16:56:10 -04002295 {
Geoff Langb1196682014-07-23 13:47:29 -04002296 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04002297 }
2298
Jamie Madille98b1b52018-03-08 09:47:23 -05002299 if (readFramebuffer->getReadBufferState() == GL_NONE)
Martin Radev138064f2016-07-15 12:03:41 +03002300 {
Brandon Jonesafa75152017-07-21 13:11:29 -07002301 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ReadBufferNone);
Martin Radev138064f2016-07-15 12:03:41 +03002302 return false;
2303 }
2304
Jamie Madille98b1b52018-03-08 09:47:23 -05002305 const FramebufferAttachment *attachment = readFramebuffer->getReadColorbuffer();
Jamie Madill3c7fa222014-06-05 13:08:51 -04002306 if (!attachment)
Jamie Madill893ab082014-05-16 16:56:10 -04002307 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002308 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04002309 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04002310 }
2311 }
2312 break;
2313
He Yunchaoced53ae2016-11-29 15:00:51 +08002314 default:
2315 break;
Jamie Madill893ab082014-05-16 16:56:10 -04002316 }
2317
2318 // pname is valid, but there are no parameters to return
Geoff Langff5b2d52016-09-07 11:32:23 -04002319 if (*numParams == 0)
2320 {
2321 return false;
2322 }
2323
2324 return true;
2325}
2326
Brandon Jonesd1049182018-03-28 10:02:20 -07002327bool ValidateGetBooleanvRobustANGLE(Context *context,
2328 GLenum pname,
2329 GLsizei bufSize,
2330 GLsizei *length,
2331 GLboolean *params)
2332{
2333 GLenum nativeType;
2334 unsigned int numParams = 0;
2335
2336 if (!ValidateRobustStateQuery(context, pname, bufSize, &nativeType, &numParams))
2337 {
2338 return false;
2339 }
2340
2341 SetRobustLengthParam(length, numParams);
2342
2343 return true;
2344}
2345
2346bool ValidateGetFloatvRobustANGLE(Context *context,
2347 GLenum pname,
2348 GLsizei bufSize,
2349 GLsizei *length,
2350 GLfloat *params)
2351{
2352 GLenum nativeType;
2353 unsigned int numParams = 0;
2354
2355 if (!ValidateRobustStateQuery(context, pname, bufSize, &nativeType, &numParams))
2356 {
2357 return false;
2358 }
2359
2360 SetRobustLengthParam(length, numParams);
2361
2362 return true;
2363}
2364
2365bool ValidateGetIntegervRobustANGLE(Context *context,
2366 GLenum pname,
2367 GLsizei bufSize,
2368 GLsizei *length,
2369 GLint *data)
2370{
2371 GLenum nativeType;
2372 unsigned int numParams = 0;
2373
2374 if (!ValidateRobustStateQuery(context, pname, bufSize, &nativeType, &numParams))
2375 {
2376 return false;
2377 }
2378
2379 SetRobustLengthParam(length, numParams);
2380
2381 return true;
2382}
2383
2384bool ValidateGetInteger64vRobustANGLE(Context *context,
2385 GLenum pname,
2386 GLsizei bufSize,
2387 GLsizei *length,
2388 GLint64 *data)
2389{
2390 GLenum nativeType;
2391 unsigned int numParams = 0;
2392
2393 if (!ValidateRobustStateQuery(context, pname, bufSize, &nativeType, &numParams))
2394 {
2395 return false;
2396 }
2397
2398 if (nativeType == GL_INT_64_ANGLEX)
2399 {
2400 CastStateValues(context, nativeType, pname, numParams, data);
2401 return false;
2402 }
2403
2404 SetRobustLengthParam(length, numParams);
2405 return true;
2406}
2407
Jamie Madill5b772312018-03-08 20:28:32 -05002408bool ValidateRobustStateQuery(Context *context,
Geoff Langff5b2d52016-09-07 11:32:23 -04002409 GLenum pname,
2410 GLsizei bufSize,
2411 GLenum *nativeType,
2412 unsigned int *numParams)
2413{
2414 if (!ValidateRobustEntryPoint(context, bufSize))
2415 {
2416 return false;
2417 }
2418
2419 if (!ValidateStateQuery(context, pname, nativeType, numParams))
2420 {
2421 return false;
2422 }
2423
2424 if (!ValidateRobustBufferSize(context, bufSize, *numParams))
Jamie Madill893ab082014-05-16 16:56:10 -04002425 {
2426 return false;
2427 }
2428
2429 return true;
2430}
2431
Jamie Madill5b772312018-03-08 20:28:32 -05002432bool ValidateCopyTexImageParametersBase(Context *context,
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002433 TextureTarget target,
Jamie Madillc29968b2016-01-20 11:17:23 -05002434 GLint level,
2435 GLenum internalformat,
2436 bool isSubImage,
2437 GLint xoffset,
2438 GLint yoffset,
2439 GLint zoffset,
2440 GLint x,
2441 GLint y,
2442 GLsizei width,
2443 GLsizei height,
2444 GLint border,
Jamie Madill0c8abca2016-07-22 20:21:26 -04002445 Format *textureFormatOut)
Jamie Madill560a8d82014-05-21 13:06:20 -04002446{
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002447 TextureType texType = TextureTargetToType(target);
2448
Brandon Jones6cad5662017-06-14 13:25:13 -07002449 if (xoffset < 0 || yoffset < 0 || zoffset < 0)
Jamie Madill560a8d82014-05-21 13:06:20 -04002450 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002451 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeOffset);
2452 return false;
2453 }
2454
2455 if (width < 0 || height < 0)
2456 {
2457 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeSize);
Geoff Langb1196682014-07-23 13:47:29 -04002458 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002459 }
2460
He Yunchaoced53ae2016-11-29 15:00:51 +08002461 if (std::numeric_limits<GLsizei>::max() - xoffset < width ||
2462 std::numeric_limits<GLsizei>::max() - yoffset < height)
Jamie Madill560a8d82014-05-21 13:06:20 -04002463 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002464 context->handleError(InvalidValue());
Geoff Langb1196682014-07-23 13:47:29 -04002465 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002466 }
2467
2468 if (border != 0)
2469 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002470 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidBorder);
Geoff Langb1196682014-07-23 13:47:29 -04002471 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002472 }
2473
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002474 if (!ValidMipLevel(context, texType, level))
Jamie Madill560a8d82014-05-21 13:06:20 -04002475 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002476 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidMipLevel);
Geoff Langb1196682014-07-23 13:47:29 -04002477 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002478 }
2479
Jamie Madille98b1b52018-03-08 09:47:23 -05002480 const gl::State &state = context->getGLState();
Jamie Madillacf2f3a2017-11-21 19:22:44 -05002481 Framebuffer *readFramebuffer = state.getReadFramebuffer();
Jamie Madill427064d2018-04-13 16:20:34 -04002482 if (!ValidateFramebufferComplete(context, readFramebuffer))
Jamie Madill560a8d82014-05-21 13:06:20 -04002483 {
Geoff Langb1196682014-07-23 13:47:29 -04002484 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002485 }
2486
Jamie Madille98b1b52018-03-08 09:47:23 -05002487 if (readFramebuffer->id() != 0 && !ValidateFramebufferNotMultisampled(context, readFramebuffer))
Jamie Madill560a8d82014-05-21 13:06:20 -04002488 {
Geoff Langb1196682014-07-23 13:47:29 -04002489 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002490 }
2491
Martin Radev138064f2016-07-15 12:03:41 +03002492 if (readFramebuffer->getReadBufferState() == GL_NONE)
2493 {
Brandon Jonesafa75152017-07-21 13:11:29 -07002494 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ReadBufferNone);
Martin Radev138064f2016-07-15 12:03:41 +03002495 return false;
2496 }
2497
Corentin Wallez3c90ed62016-12-16 16:19:28 -05002498 // WebGL 1.0 [Section 6.26] Reading From a Missing Attachment
2499 // In OpenGL ES it is undefined what happens when an operation tries to read from a missing
He Yunchao66a41a22016-12-15 16:45:05 +08002500 // attachment and WebGL defines it to be an error. We do the check unconditionally as the
Corentin Wallez3c90ed62016-12-16 16:19:28 -05002501 // situation is an application error that would lead to a crash in ANGLE.
Martin Radev04e2c3b2017-07-27 16:54:35 +03002502 const FramebufferAttachment *source = readFramebuffer->getReadColorbuffer();
2503 if (source == nullptr)
Corentin Wallez3c90ed62016-12-16 16:19:28 -05002504 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002505 ANGLE_VALIDATION_ERR(context, InvalidOperation(), MissingReadAttachment);
Corentin Wallez3c90ed62016-12-16 16:19:28 -05002506 return false;
2507 }
2508
Martin Radev04e2c3b2017-07-27 16:54:35 +03002509 // ANGLE_multiview spec, Revision 1:
2510 // Calling CopyTexSubImage3D, CopyTexImage2D, or CopyTexSubImage2D will result in an
2511 // INVALID_FRAMEBUFFER_OPERATION error if the multi-view layout of the current read framebuffer
2512 // is not NONE.
2513 if (source->getMultiviewLayout() != GL_NONE)
2514 {
2515 context->handleError(InvalidFramebufferOperation()
2516 << "The active read framebuffer object has multiview attachments.");
2517 return false;
2518 }
2519
Geoff Langaae65a42014-05-26 12:43:44 -04002520 const gl::Caps &caps = context->getCaps();
2521
Geoff Langaae65a42014-05-26 12:43:44 -04002522 GLuint maxDimension = 0;
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002523 switch (texType)
Jamie Madill560a8d82014-05-21 13:06:20 -04002524 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002525 case TextureType::_2D:
He Yunchaoced53ae2016-11-29 15:00:51 +08002526 maxDimension = caps.max2DTextureSize;
2527 break;
Jamie Madill560a8d82014-05-21 13:06:20 -04002528
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002529 case TextureType::CubeMap:
He Yunchaoced53ae2016-11-29 15:00:51 +08002530 maxDimension = caps.maxCubeMapTextureSize;
2531 break;
Jamie Madill560a8d82014-05-21 13:06:20 -04002532
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002533 case TextureType::Rectangle:
Corentin Wallez13c0dd42017-07-04 18:27:01 -04002534 maxDimension = caps.maxRectangleTextureSize;
2535 break;
2536
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002537 case TextureType::_2DArray:
He Yunchaoced53ae2016-11-29 15:00:51 +08002538 maxDimension = caps.max2DTextureSize;
2539 break;
Jamie Madill560a8d82014-05-21 13:06:20 -04002540
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002541 case TextureType::_3D:
He Yunchaoced53ae2016-11-29 15:00:51 +08002542 maxDimension = caps.max3DTextureSize;
2543 break;
Jamie Madill560a8d82014-05-21 13:06:20 -04002544
He Yunchaoced53ae2016-11-29 15:00:51 +08002545 default:
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002546 context->handleError(InvalidEnum());
He Yunchaoced53ae2016-11-29 15:00:51 +08002547 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002548 }
2549
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002550 gl::Texture *texture = state.getTargetTexture(texType);
Jamie Madill560a8d82014-05-21 13:06:20 -04002551 if (!texture)
2552 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002553 ANGLE_VALIDATION_ERR(context, InvalidOperation(), TextureNotBound);
Geoff Langb1196682014-07-23 13:47:29 -04002554 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002555 }
2556
Geoff Lang69cce582015-09-17 13:20:36 -04002557 if (texture->getImmutableFormat() && !isSubImage)
Jamie Madill560a8d82014-05-21 13:06:20 -04002558 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002559 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04002560 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002561 }
2562
Geoff Langca271392017-04-05 12:30:00 -04002563 const gl::InternalFormat &formatInfo =
Geoff Lang86f81162017-10-30 15:10:45 -04002564 isSubImage ? *texture->getFormat(target, level).info
2565 : gl::GetInternalFormatInfo(internalformat, GL_UNSIGNED_BYTE);
Geoff Lang5d601382014-07-22 15:14:06 -04002566
Geoff Lang966c9402017-04-18 12:38:27 -04002567 if (formatInfo.depthBits > 0 || formatInfo.compressed)
Jamie Madill560a8d82014-05-21 13:06:20 -04002568 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002569 context->handleError(InvalidOperation());
Geoff Langa9be0dc2014-12-17 12:34:40 -05002570 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002571 }
2572
2573 if (isSubImage)
2574 {
Geoff Langa9be0dc2014-12-17 12:34:40 -05002575 if (static_cast<size_t>(xoffset + width) > texture->getWidth(target, level) ||
2576 static_cast<size_t>(yoffset + height) > texture->getHeight(target, level) ||
2577 static_cast<size_t>(zoffset) >= texture->getDepth(target, level))
Jamie Madill560a8d82014-05-21 13:06:20 -04002578 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05002579 context->handleError(InvalidValue());
Geoff Langb1196682014-07-23 13:47:29 -04002580 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04002581 }
2582 }
Jamie Madill6f38f822014-06-06 17:12:20 -04002583 else
2584 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002585 if (texType == TextureType::CubeMap && width != height)
Jamie Madill6f38f822014-06-06 17:12:20 -04002586 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002587 ANGLE_VALIDATION_ERR(context, InvalidValue(), CubemapIncomplete);
Geoff Langb1196682014-07-23 13:47:29 -04002588 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04002589 }
2590
Geoff Langeb66a6e2016-10-31 13:06:12 -04002591 if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions()))
Jamie Madill6f38f822014-06-06 17:12:20 -04002592 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002593 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Geoff Langb1196682014-07-23 13:47:29 -04002594 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04002595 }
2596
2597 int maxLevelDimension = (maxDimension >> level);
He Yunchaoced53ae2016-11-29 15:00:51 +08002598 if (static_cast<int>(width) > maxLevelDimension ||
2599 static_cast<int>(height) > maxLevelDimension)
Jamie Madill6f38f822014-06-06 17:12:20 -04002600 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002601 ANGLE_VALIDATION_ERR(context, InvalidValue(), ResourceMaxTextureSize);
Geoff Langb1196682014-07-23 13:47:29 -04002602 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04002603 }
2604 }
Jamie Madill560a8d82014-05-21 13:06:20 -04002605
Jamie Madill0c8abca2016-07-22 20:21:26 -04002606 if (textureFormatOut)
2607 {
2608 *textureFormatOut = texture->getFormat(target, level);
2609 }
Jamie Madillf695a3a2017-01-11 17:36:35 -05002610
2611 // Detect texture copying feedback loops for WebGL.
2612 if (context->getExtensions().webglCompatibility)
2613 {
Jamie Madillfd3dd432017-02-02 19:59:59 -05002614 if (readFramebuffer->formsCopyingFeedbackLoopWith(texture->id(), level, zoffset))
Jamie Madillf695a3a2017-01-11 17:36:35 -05002615 {
Brandon Jonesafa75152017-07-21 13:11:29 -07002616 ANGLE_VALIDATION_ERR(context, InvalidOperation(), FeedbackLoop);
Jamie Madillf695a3a2017-01-11 17:36:35 -05002617 return false;
2618 }
2619 }
2620
Jamie Madill560a8d82014-05-21 13:06:20 -04002621 return true;
2622}
2623
Jamie Madill493f9572018-05-24 19:52:15 -04002624bool ValidateDrawBase(Context *context, PrimitiveMode mode, GLsizei count)
Jamie Madill250d33f2014-06-06 17:09:03 -04002625{
Jiawei Shaofccebff2018-03-08 13:51:02 +08002626 const Extensions &extensions = context->getExtensions();
2627
Jamie Madill1aeb1312014-06-20 13:21:25 -04002628 switch (mode)
2629 {
Jamie Madill493f9572018-05-24 19:52:15 -04002630 case PrimitiveMode::Points:
2631 case PrimitiveMode::Lines:
2632 case PrimitiveMode::LineLoop:
2633 case PrimitiveMode::LineStrip:
2634 case PrimitiveMode::Triangles:
2635 case PrimitiveMode::TriangleStrip:
2636 case PrimitiveMode::TriangleFan:
He Yunchaoced53ae2016-11-29 15:00:51 +08002637 break;
Jiawei Shaofccebff2018-03-08 13:51:02 +08002638
Jamie Madill493f9572018-05-24 19:52:15 -04002639 case PrimitiveMode::LinesAdjacency:
2640 case PrimitiveMode::LineStripAdjacency:
2641 case PrimitiveMode::TrianglesAdjacency:
2642 case PrimitiveMode::TriangleStripAdjacency:
Jiawei Shaofccebff2018-03-08 13:51:02 +08002643 if (!extensions.geometryShader)
2644 {
2645 ANGLE_VALIDATION_ERR(context, InvalidEnum(), GeometryShaderExtensionNotEnabled);
2646 return false;
2647 }
2648 break;
He Yunchaoced53ae2016-11-29 15:00:51 +08002649 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07002650 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidDrawMode);
He Yunchaoced53ae2016-11-29 15:00:51 +08002651 return false;
Jamie Madill1aeb1312014-06-20 13:21:25 -04002652 }
2653
Jamie Madill250d33f2014-06-06 17:09:03 -04002654 if (count < 0)
2655 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002656 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeCount);
Geoff Langb1196682014-07-23 13:47:29 -04002657 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04002658 }
2659
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002660 const State &state = context->getGLState();
Geoff Langb1196682014-07-23 13:47:29 -04002661
Jiawei Shao3ef06a92017-11-03 18:41:33 +08002662 // WebGL buffers cannot be mapped/unmapped because the MapBufferRange, FlushMappedBufferRange,
2663 // and UnmapBuffer entry points are removed from the WebGL 2.0 API.
2664 // https://www.khronos.org/registry/webgl/specs/latest/2.0/#5.14
2665 if (!extensions.webglCompatibility)
Jamie Madill250d33f2014-06-06 17:09:03 -04002666 {
Jiawei Shao3ef06a92017-11-03 18:41:33 +08002667 // Check for mapped buffers
2668 // TODO(jmadill): Optimize this check for non - WebGL contexts.
Corentin Wallez336129f2017-10-17 15:55:40 -04002669 if (state.hasMappedBuffer(BufferBinding::Array))
Jiawei Shao3ef06a92017-11-03 18:41:33 +08002670 {
2671 context->handleError(InvalidOperation());
2672 return false;
2673 }
Jamie Madill250d33f2014-06-06 17:09:03 -04002674 }
2675
Jamie Madillcbcde722017-01-06 14:50:00 -05002676 // Note: these separate values are not supported in WebGL, due to D3D's limitations. See
2677 // Section 6.10 of the WebGL 1.0 spec.
Jamie Madill51f40ec2016-06-15 14:06:00 -04002678 Framebuffer *framebuffer = state.getDrawFramebuffer();
Martin Radevffe754b2017-07-31 10:38:07 +03002679 if (context->getLimitations().noSeparateStencilRefsAndMasks || extensions.webglCompatibility)
Jamie Madillac528012014-06-20 13:21:23 -04002680 {
Ken Russellb9f92502018-01-27 19:00:26 -08002681 ASSERT(framebuffer);
Corentin Wallezb1d0a2552016-12-19 16:15:54 -05002682 const FramebufferAttachment *dsAttachment =
2683 framebuffer->getStencilOrDepthStencilAttachment();
Ken Russellb9f92502018-01-27 19:00:26 -08002684 const GLuint stencilBits = dsAttachment ? dsAttachment->getStencilSize() : 0;
2685 ASSERT(stencilBits <= 8);
2686
Jinyoung Hur85769f02015-10-20 17:08:44 -04002687 const DepthStencilState &depthStencilState = state.getDepthStencilState();
Ken Russellb9f92502018-01-27 19:00:26 -08002688 if (depthStencilState.stencilTest && stencilBits > 0)
Geoff Lang3a86ad32015-09-01 11:47:05 -04002689 {
Ken Russellb9f92502018-01-27 19:00:26 -08002690 GLuint maxStencilValue = (1 << stencilBits) - 1;
2691
2692 bool differentRefs =
2693 clamp(state.getStencilRef(), 0, static_cast<GLint>(maxStencilValue)) !=
2694 clamp(state.getStencilBackRef(), 0, static_cast<GLint>(maxStencilValue));
2695 bool differentWritemasks = (depthStencilState.stencilWritemask & maxStencilValue) !=
2696 (depthStencilState.stencilBackWritemask & maxStencilValue);
2697 bool differentMasks = (depthStencilState.stencilMask & maxStencilValue) !=
2698 (depthStencilState.stencilBackMask & maxStencilValue);
2699
2700 if (differentRefs || differentWritemasks || differentMasks)
Jamie Madillcbcde722017-01-06 14:50:00 -05002701 {
Ken Russellb9f92502018-01-27 19:00:26 -08002702 if (!extensions.webglCompatibility)
2703 {
2704 ERR() << "This ANGLE implementation does not support separate front/back "
2705 "stencil writemasks, reference values, or stencil mask values.";
2706 }
2707 ANGLE_VALIDATION_ERR(context, InvalidOperation(), StencilReferenceMaskOrMismatch);
2708 return false;
Jamie Madillcbcde722017-01-06 14:50:00 -05002709 }
Geoff Lang3a86ad32015-09-01 11:47:05 -04002710 }
Jamie Madillac528012014-06-20 13:21:23 -04002711 }
2712
Jamie Madill427064d2018-04-13 16:20:34 -04002713 if (!ValidateFramebufferComplete(context, framebuffer))
Jamie Madill13f7d7d2014-06-20 13:21:27 -04002714 {
Geoff Langb1196682014-07-23 13:47:29 -04002715 return false;
Jamie Madill13f7d7d2014-06-20 13:21:27 -04002716 }
2717
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002718 // If we are running GLES1, there is no current program.
2719 if (context->getClientVersion() >= Version(2, 0))
Jamie Madilld4cfa572014-07-08 10:00:32 -04002720 {
Jamie Madilld4cfa572014-07-08 10:00:32 -04002721
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002722 gl::Program *program = state.getProgram();
2723 if (!program)
Martin Radev7cf61662017-07-26 17:10:53 +03002724 {
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002725 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ProgramNotBound);
Martin Radev7cf61662017-07-26 17:10:53 +03002726 return false;
2727 }
Martin Radev7e69f762017-07-27 14:54:13 +03002728
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002729 // In OpenGL ES spec for UseProgram at section 7.3, trying to render without
2730 // vertex shader stage or fragment shader stage is a undefined behaviour.
2731 // But ANGLE should clearly generate an INVALID_OPERATION error instead of
2732 // produce undefined result.
2733 if (!program->hasLinkedShaderStage(ShaderType::Vertex) ||
2734 !program->hasLinkedShaderStage(ShaderType::Fragment))
Martin Radev7e69f762017-07-27 14:54:13 +03002735 {
2736 context->handleError(InvalidOperation()
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002737 << "It is a undefined behaviour to render without "
2738 "vertex shader stage or fragment shader stage.");
Martin Radev7e69f762017-07-27 14:54:13 +03002739 return false;
2740 }
Martin Radevffe754b2017-07-31 10:38:07 +03002741
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002742 if (!program->validateSamplers(nullptr, context->getCaps()))
Martin Radevffe754b2017-07-31 10:38:07 +03002743 {
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002744 context->handleError(InvalidOperation());
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002745 return false;
2746 }
2747
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002748 if (extensions.multiview)
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002749 {
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002750 const int programNumViews = program->usesMultiview() ? program->getNumViews() : 1;
2751 const int framebufferNumViews = framebuffer->getNumViews();
2752 if (framebufferNumViews != programNumViews)
2753 {
2754 context->handleError(InvalidOperation()
2755 << "The number of views in the active program "
2756 "and draw framebuffer does not match.");
2757 return false;
2758 }
2759
2760 const TransformFeedback *transformFeedbackObject = state.getCurrentTransformFeedback();
2761 if (transformFeedbackObject != nullptr && transformFeedbackObject->isActive() &&
2762 framebufferNumViews > 1)
2763 {
2764 context->handleError(InvalidOperation()
2765 << "There is an active transform feedback object "
2766 "when the number of views in the active draw "
2767 "framebuffer is greater than 1.");
2768 return false;
2769 }
2770
2771 if (extensions.disjointTimerQuery && framebufferNumViews > 1 &&
2772 state.isQueryActive(QueryType::TimeElapsed))
2773 {
2774 context->handleError(InvalidOperation()
2775 << "There is an active query for target "
2776 "GL_TIME_ELAPSED_EXT when the number of "
2777 "views in the active draw framebuffer is "
2778 "greater than 1.");
2779 return false;
2780 }
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002781 }
James Darpiniane8a93c62018-01-04 18:02:24 -08002782
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002783 // Do geometry shader specific validations
2784 if (program->hasLinkedShaderStage(ShaderType::Geometry))
James Darpiniane8a93c62018-01-04 18:02:24 -08002785 {
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002786 if (!IsCompatibleDrawModeWithGeometryShader(
2787 mode, program->getGeometryShaderInputPrimitiveType()))
2788 {
2789 ANGLE_VALIDATION_ERR(context, InvalidOperation(),
2790 IncompatibleDrawModeAgainstGeometryShader);
2791 return false;
2792 }
Jamie Madilla4595b82017-01-11 17:36:34 -05002793 }
Geoff Lange0cff192017-05-30 13:04:56 -04002794
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002795 // Uniform buffer validation
2796 for (unsigned int uniformBlockIndex = 0;
2797 uniformBlockIndex < program->getActiveUniformBlockCount(); uniformBlockIndex++)
Geoff Lang9ab5b822017-05-30 16:19:23 -04002798 {
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002799 const gl::InterfaceBlock &uniformBlock =
2800 program->getUniformBlockByIndex(uniformBlockIndex);
2801 GLuint blockBinding = program->getUniformBlockBinding(uniformBlockIndex);
2802 const OffsetBindingPointer<Buffer> &uniformBuffer =
2803 state.getIndexedUniformBuffer(blockBinding);
2804
2805 if (uniformBuffer.get() == nullptr)
2806 {
2807 // undefined behaviour
2808 context->handleError(
2809 InvalidOperation()
2810 << "It is undefined behaviour to have a used but unbound uniform buffer.");
2811 return false;
2812 }
2813
2814 size_t uniformBufferSize = GetBoundBufferAvailableSize(uniformBuffer);
2815 if (uniformBufferSize < uniformBlock.dataSize)
2816 {
2817 // undefined behaviour
2818 context->handleError(
2819 InvalidOperation()
2820 << "It is undefined behaviour to use a uniform buffer that is too small.");
2821 return false;
2822 }
2823
2824 if (extensions.webglCompatibility &&
2825 uniformBuffer->isBoundForTransformFeedbackAndOtherUse())
2826 {
2827 ANGLE_VALIDATION_ERR(context, InvalidOperation(),
2828 UniformBufferBoundForTransformFeedback);
2829 return false;
2830 }
Geoff Lang9ab5b822017-05-30 16:19:23 -04002831 }
2832
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002833 // Do some additonal WebGL-specific validation
2834 if (extensions.webglCompatibility)
Geoff Lange0cff192017-05-30 13:04:56 -04002835 {
Lingfeng Yang461b09a2018-04-23 09:02:09 -07002836 const TransformFeedback *transformFeedbackObject = state.getCurrentTransformFeedback();
2837 if (transformFeedbackObject != nullptr && transformFeedbackObject->isActive() &&
2838 transformFeedbackObject->buffersBoundForOtherUse())
2839 {
2840 ANGLE_VALIDATION_ERR(context, InvalidOperation(),
2841 TransformFeedbackBufferDoubleBound);
2842 return false;
2843 }
2844 // Detect rendering feedback loops for WebGL.
2845 if (framebuffer->formsRenderingFeedbackLoopWith(state))
2846 {
2847 ANGLE_VALIDATION_ERR(context, InvalidOperation(), FeedbackLoop);
2848 return false;
2849 }
2850
2851 // Detect that the vertex shader input types match the attribute types
2852 if (!ValidateVertexShaderAttributeTypeMatch(context))
2853 {
2854 return false;
2855 }
2856
2857 // Detect that the color buffer types match the fragment shader output types
2858 if (!ValidateFragmentShaderColorBufferTypeMatch(context))
2859 {
2860 return false;
2861 }
Geoff Lange0cff192017-05-30 13:04:56 -04002862 }
Jamie Madilla4595b82017-01-11 17:36:34 -05002863 }
2864
Jamie Madill9fdaa492018-02-16 10:52:11 -05002865 return true;
Jamie Madill250d33f2014-06-06 17:09:03 -04002866}
2867
Jamie Madill5b772312018-03-08 20:28:32 -05002868bool ValidateDrawArraysCommon(Context *context,
Jamie Madill493f9572018-05-24 19:52:15 -04002869 PrimitiveMode mode,
Jamie Madillc1d770e2017-04-13 17:31:24 -04002870 GLint first,
2871 GLsizei count,
2872 GLsizei primcount)
Jamie Madill250d33f2014-06-06 17:09:03 -04002873{
Jamie Madillfd716582014-06-06 17:09:04 -04002874 if (first < 0)
Jamie Madill250d33f2014-06-06 17:09:03 -04002875 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002876 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeStart);
Geoff Langb1196682014-07-23 13:47:29 -04002877 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04002878 }
2879
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002880 const State &state = context->getGLState();
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002881 gl::TransformFeedback *curTransformFeedback = state.getCurrentTransformFeedback();
He Yunchaoced53ae2016-11-29 15:00:51 +08002882 if (curTransformFeedback && curTransformFeedback->isActive() &&
James Darpinian30b604d2018-03-12 17:26:57 -07002883 !curTransformFeedback->isPaused())
Jamie Madillfd716582014-06-06 17:09:04 -04002884 {
Jiawei Shao80c32cc2018-04-25 09:48:36 +08002885 if (!ValidateTransformFeedbackPrimitiveMode(context,
2886 curTransformFeedback->getPrimitiveMode(), mode))
James Darpinian30b604d2018-03-12 17:26:57 -07002887 {
James Darpinian30b604d2018-03-12 17:26:57 -07002888 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidDrawModeTransformFeedback);
2889 return false;
2890 }
2891
2892 if (!curTransformFeedback->checkBufferSpaceForDraw(count, primcount))
2893 {
2894 ANGLE_VALIDATION_ERR(context, InvalidOperation(), TransformFeedbackBufferTooSmall);
2895 return false;
2896 }
Jamie Madillfd716582014-06-06 17:09:04 -04002897 }
2898
Jiajia Qind9671222016-11-29 16:30:31 +08002899 if (!ValidateDrawBase(context, mode, count))
Corentin Wallez18a2fb32015-08-10 12:58:14 -07002900 {
2901 return false;
2902 }
2903
Corentin Wallez71168a02016-12-19 15:11:18 -08002904 // Check the computation of maxVertex doesn't overflow.
Jamie Madill9fdaa492018-02-16 10:52:11 -05002905 // - first < 0 has been checked as an error condition.
2906 // - if count < 0, skip validating no-op draw calls.
Corentin Wallez71168a02016-12-19 15:11:18 -08002907 // From this we know maxVertex will be positive, and only need to check if it overflows GLint.
Jamie Madill9fdaa492018-02-16 10:52:11 -05002908 ASSERT(first >= 0);
2909 if (count > 0)
Corentin Wallez92db6942016-12-09 13:10:36 -05002910 {
Jamie Madill9fdaa492018-02-16 10:52:11 -05002911 int64_t maxVertex = static_cast<int64_t>(first) + static_cast<int64_t>(count) - 1;
2912 if (maxVertex > static_cast<int64_t>(std::numeric_limits<GLint>::max()))
2913 {
2914 ANGLE_VALIDATION_ERR(context, InvalidOperation(), IntegerOverflow);
2915 return false;
2916 }
Corentin Wallez92db6942016-12-09 13:10:36 -05002917
Jamie Madill9fdaa492018-02-16 10:52:11 -05002918 if (!ValidateDrawAttribs(context, primcount, static_cast<GLint>(maxVertex), count))
2919 {
2920 return false;
2921 }
Jamie Madillfd716582014-06-06 17:09:04 -04002922 }
2923
2924 return true;
2925}
2926
He Yunchaoced53ae2016-11-29 15:00:51 +08002927bool ValidateDrawArraysInstancedANGLE(Context *context,
Jamie Madill493f9572018-05-24 19:52:15 -04002928 PrimitiveMode mode,
He Yunchaoced53ae2016-11-29 15:00:51 +08002929 GLint first,
2930 GLsizei count,
2931 GLsizei primcount)
Geoff Lang87a93302014-09-16 13:29:43 -04002932{
Geoff Lang63c5a592017-09-27 14:08:16 -04002933 if (!context->getExtensions().instancedArrays)
2934 {
2935 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
2936 return false;
2937 }
2938
Corentin Wallez170efbf2017-05-02 13:45:01 -04002939 if (!ValidateDrawArraysInstancedBase(context, mode, first, count, primcount))
Geoff Lang87a93302014-09-16 13:29:43 -04002940 {
2941 return false;
2942 }
2943
Corentin Wallez0dc97812017-06-22 14:38:44 -04002944 return ValidateDrawInstancedANGLE(context);
Geoff Lang87a93302014-09-16 13:29:43 -04002945}
2946
Jamie Madill493f9572018-05-24 19:52:15 -04002947bool ValidateDrawElementsBase(Context *context, PrimitiveMode mode, GLenum type)
Jamie Madillfd716582014-06-06 17:09:04 -04002948{
Jamie Madill250d33f2014-06-06 17:09:03 -04002949 switch (type)
2950 {
He Yunchaoced53ae2016-11-29 15:00:51 +08002951 case GL_UNSIGNED_BYTE:
2952 case GL_UNSIGNED_SHORT:
2953 break;
2954 case GL_UNSIGNED_INT:
2955 if (context->getClientMajorVersion() < 3 && !context->getExtensions().elementIndexUint)
2956 {
Brandon Jones6cad5662017-06-14 13:25:13 -07002957 ANGLE_VALIDATION_ERR(context, InvalidEnum(), TypeNotUnsignedShortByte);
He Yunchaoced53ae2016-11-29 15:00:51 +08002958 return false;
2959 }
2960 break;
2961 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07002962 ANGLE_VALIDATION_ERR(context, InvalidEnum(), TypeNotUnsignedShortByte);
He Yunchaoced53ae2016-11-29 15:00:51 +08002963 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04002964 }
2965
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002966 const State &state = context->getGLState();
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002967
2968 gl::TransformFeedback *curTransformFeedback = state.getCurrentTransformFeedback();
He Yunchaoced53ae2016-11-29 15:00:51 +08002969 if (curTransformFeedback && curTransformFeedback->isActive() &&
2970 !curTransformFeedback->isPaused())
Jamie Madill250d33f2014-06-06 17:09:03 -04002971 {
Jiawei Shao80c32cc2018-04-25 09:48:36 +08002972 // EXT_geometry_shader allows transform feedback to work with all draw commands.
2973 // [EXT_geometry_shader] Section 12.1, "Transform Feedback"
2974 if (context->getExtensions().geometryShader)
2975 {
2976 if (!ValidateTransformFeedbackPrimitiveMode(
2977 context, curTransformFeedback->getPrimitiveMode(), mode))
2978 {
2979 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidDrawModeTransformFeedback);
2980 return false;
2981 }
2982 }
2983 else
2984 {
2985 // It is an invalid operation to call DrawElements, DrawRangeElements or
2986 // DrawElementsInstanced while transform feedback is active, (3.0.2, section 2.14, pg
2987 // 86)
2988 ANGLE_VALIDATION_ERR(context, InvalidOperation(),
2989 UnsupportedDrawModeForTransformFeedback);
2990 return false;
2991 }
Jamie Madill250d33f2014-06-06 17:09:03 -04002992 }
2993
Jiajia Qind9671222016-11-29 16:30:31 +08002994 return true;
2995}
2996
Jamie Madill5b772312018-03-08 20:28:32 -05002997bool ValidateDrawElementsCommon(Context *context,
Jamie Madill493f9572018-05-24 19:52:15 -04002998 PrimitiveMode mode,
Jamie Madill9c9b40a2017-04-26 16:31:57 -04002999 GLsizei count,
3000 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -04003001 const void *indices,
Jamie Madill9c9b40a2017-04-26 16:31:57 -04003002 GLsizei primcount)
Jiajia Qind9671222016-11-29 16:30:31 +08003003{
Jiawei Shao80c32cc2018-04-25 09:48:36 +08003004 if (!ValidateDrawElementsBase(context, mode, type))
Jiajia Qind9671222016-11-29 16:30:31 +08003005 return false;
3006
3007 const State &state = context->getGLState();
3008
Corentin Wallez170efbf2017-05-02 13:45:01 -04003009 if (!ValidateDrawBase(context, mode, count))
3010 {
3011 return false;
3012 }
3013
Jiawei Shao3ef06a92017-11-03 18:41:33 +08003014 // WebGL buffers cannot be mapped/unmapped because the MapBufferRange, FlushMappedBufferRange,
3015 // and UnmapBuffer entry points are removed from the WebGL 2.0 API.
3016 // https://www.khronos.org/registry/webgl/specs/latest/2.0/#5.14
3017 if (!context->getExtensions().webglCompatibility)
Jamie Madill250d33f2014-06-06 17:09:03 -04003018 {
Jiawei Shao3ef06a92017-11-03 18:41:33 +08003019 // Check for mapped buffers
3020 // TODO(jmadill): Optimize this check for non - WebGL contexts.
Corentin Wallez336129f2017-10-17 15:55:40 -04003021 if (state.hasMappedBuffer(gl::BufferBinding::ElementArray))
Jiawei Shao3ef06a92017-11-03 18:41:33 +08003022 {
3023 context->handleError(InvalidOperation() << "Index buffer is mapped.");
3024 return false;
3025 }
Jamie Madill250d33f2014-06-06 17:09:03 -04003026 }
3027
He Yunchaoced53ae2016-11-29 15:00:51 +08003028 const gl::VertexArray *vao = state.getVertexArray();
Jamie Madill8e344942015-07-09 14:22:07 -04003029 gl::Buffer *elementArrayBuffer = vao->getElementArrayBuffer().get();
Jamie Madilld4cfa572014-07-08 10:00:32 -04003030
Corentin Wallez3f6d4df2017-01-30 18:04:36 -05003031 GLuint typeBytes = gl::GetTypeInfo(type).bytes;
3032
3033 if (context->getExtensions().webglCompatibility)
3034 {
3035 ASSERT(isPow2(typeBytes) && typeBytes > 0);
3036 if ((reinterpret_cast<uintptr_t>(indices) & static_cast<uintptr_t>(typeBytes - 1)) != 0)
3037 {
3038 // [WebGL 1.0] Section 6.4 Buffer Offset and Stride Requirements
3039 // The offset arguments to drawElements and [...], must be a multiple of the size of the
3040 // data type passed to the call, or an INVALID_OPERATION error is generated.
Brandon Jones6cad5662017-06-14 13:25:13 -07003041 ANGLE_VALIDATION_ERR(context, InvalidOperation(), OffsetMustBeMultipleOfType);
Corentin Wallez3f6d4df2017-01-30 18:04:36 -05003042 return false;
3043 }
Corentin Wallezfe9306a2017-02-01 17:41:05 -05003044
3045 // [WebGL 1.0] Section 6.4 Buffer Offset and Stride Requirements
3046 // In addition the offset argument to drawElements must be non-negative or an INVALID_VALUE
3047 // error is generated.
3048 if (reinterpret_cast<intptr_t>(indices) < 0)
3049 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003050 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeOffset);
Corentin Wallezfe9306a2017-02-01 17:41:05 -05003051 return false;
3052 }
Geoff Langfeb8c682017-02-13 16:07:35 -05003053 }
3054
3055 if (context->getExtensions().webglCompatibility ||
3056 !context->getGLState().areClientArraysEnabled())
3057 {
Corentin Wallez3f6d4df2017-01-30 18:04:36 -05003058 if (!elementArrayBuffer && count > 0)
3059 {
3060 // [WebGL 1.0] Section 6.2 No Client Side Arrays
3061 // If drawElements is called with a count greater than zero, and no WebGLBuffer is bound
3062 // to the ELEMENT_ARRAY_BUFFER binding point, an INVALID_OPERATION error is generated.
Brandon Jones6cad5662017-06-14 13:25:13 -07003063 ANGLE_VALIDATION_ERR(context, InvalidOperation(), MustHaveElementArrayBinding);
Corentin Wallez3f6d4df2017-01-30 18:04:36 -05003064 return false;
3065 }
3066 }
3067
Jamie Madill9fdaa492018-02-16 10:52:11 -05003068 if (count > 0 && !elementArrayBuffer && !indices)
Jamie Madillae3000b2014-08-25 15:47:51 -04003069 {
Jamie Madill9fdaa492018-02-16 10:52:11 -05003070 // This is an application error that would normally result in a crash, but we catch it and
3071 // return an error
3072 context->handleError(InvalidOperation() << "No element array buffer and no pointer.");
3073 return false;
3074 }
3075
3076 if (count > 0 && elementArrayBuffer)
3077 {
3078 // The max possible type size is 8 and count is on 32 bits so doing the multiplication
3079 // in a 64 bit integer is safe. Also we are guaranteed that here count > 0.
3080 static_assert(std::is_same<int, GLsizei>::value, "GLsizei isn't the expected type");
3081 constexpr uint64_t kMaxTypeSize = 8;
3082 constexpr uint64_t kIntMax = std::numeric_limits<int>::max();
3083 constexpr uint64_t kUint64Max = std::numeric_limits<uint64_t>::max();
3084 static_assert(kIntMax < kUint64Max / kMaxTypeSize, "");
3085
3086 uint64_t typeSize = typeBytes;
3087 uint64_t elementCount = static_cast<uint64_t>(count);
3088 ASSERT(elementCount > 0 && typeSize <= kMaxTypeSize);
3089
3090 // Doing the multiplication here is overflow-safe
3091 uint64_t elementDataSizeNoOffset = typeSize * elementCount;
3092
3093 // The offset can be any value, check for overflows
3094 uint64_t offset = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(indices));
3095 if (elementDataSizeNoOffset > kUint64Max - offset)
Jamie Madillae3000b2014-08-25 15:47:51 -04003096 {
Jamie Madill9fdaa492018-02-16 10:52:11 -05003097 ANGLE_VALIDATION_ERR(context, InvalidOperation(), IntegerOverflow);
3098 return false;
Corentin Wallez0844f2d2017-01-31 17:02:59 -05003099 }
Jamie Madill9fdaa492018-02-16 10:52:11 -05003100
3101 uint64_t elementDataSizeWithOffset = elementDataSizeNoOffset + offset;
3102 if (elementDataSizeWithOffset > static_cast<uint64_t>(elementArrayBuffer->getSize()))
Corentin Wallez0844f2d2017-01-31 17:02:59 -05003103 {
Jamie Madill9fdaa492018-02-16 10:52:11 -05003104 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InsufficientBufferSize);
3105 return false;
3106 }
3107
3108 ASSERT(isPow2(typeSize) && typeSize > 0);
3109 if ((elementArrayBuffer->getSize() & (typeSize - 1)) != 0)
3110 {
3111 ANGLE_VALIDATION_ERR(context, InvalidOperation(), MismatchedByteCountType);
Geoff Langb1196682014-07-23 13:47:29 -04003112 return false;
Jamie Madillae3000b2014-08-25 15:47:51 -04003113 }
James Darpiniane8a93c62018-01-04 18:02:24 -08003114
3115 if (context->getExtensions().webglCompatibility &&
3116 elementArrayBuffer->isBoundForTransformFeedbackAndOtherUse())
3117 {
3118 ANGLE_VALIDATION_ERR(context, InvalidOperation(),
3119 ElementArrayBufferBoundForTransformFeedback);
3120 return false;
3121 }
Jamie Madillae3000b2014-08-25 15:47:51 -04003122 }
3123
Jiajia Qin8a7b3a02017-08-25 16:05:48 +08003124 if (context->getExtensions().robustBufferAccessBehavior)
Corentin Wallezc1346fb2017-08-24 16:11:26 +00003125 {
Jiajia Qin8a7b3a02017-08-25 16:05:48 +08003126 // Here we use maxVertex = 0 and vertexCount = 1 to avoid retrieving IndexRange when robust
3127 // access is enabled.
3128 if (!ValidateDrawAttribs(context, primcount, 0, 1))
3129 {
3130 return false;
3131 }
3132 }
Jamie Madill9fdaa492018-02-16 10:52:11 -05003133 else if (count == 0)
3134 {
3135 // ValidateDrawAttribs also does some extra validation that is independent of the vertex
3136 // count.
3137 if (!ValidateDrawAttribs(context, 0, 0, 0))
3138 {
3139 return false;
3140 }
3141 }
Jiajia Qin8a7b3a02017-08-25 16:05:48 +08003142 else
3143 {
3144 // Use the parameter buffer to retrieve and cache the index range.
Jamie Madill6f5444d2018-03-14 10:08:11 -04003145 const DrawCallParams &params = context->getParams<DrawCallParams>();
3146 ANGLE_VALIDATION_TRY(params.ensureIndexRangeResolved(context));
3147 const IndexRange &indexRange = params.getIndexRange();
Jiajia Qin8a7b3a02017-08-25 16:05:48 +08003148
3149 // If we use an index greater than our maximum supported index range, return an error.
3150 // The ES3 spec does not specify behaviour here, it is undefined, but ANGLE should always
3151 // return an error if possible here.
Jamie Madill6f5444d2018-03-14 10:08:11 -04003152 if (static_cast<GLuint64>(indexRange.end) >= context->getCaps().maxElementIndex)
Jiajia Qin8a7b3a02017-08-25 16:05:48 +08003153 {
3154 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExceedsMaxElement);
3155 return false;
3156 }
3157
Jamie Madill6f5444d2018-03-14 10:08:11 -04003158 if (!ValidateDrawAttribs(context, primcount, static_cast<GLint>(indexRange.end),
3159 static_cast<GLint>(indexRange.vertexCount())))
Jiajia Qin8a7b3a02017-08-25 16:05:48 +08003160 {
3161 return false;
3162 }
3163
3164 // No op if there are no real indices in the index data (all are primitive restart).
Jamie Madill6f5444d2018-03-14 10:08:11 -04003165 return (indexRange.vertexIndexCount > 0);
Corentin Wallezc1346fb2017-08-24 16:11:26 +00003166 }
3167
Jiajia Qin8a7b3a02017-08-25 16:05:48 +08003168 return true;
Jamie Madillfd716582014-06-06 17:09:04 -04003169}
3170
Jamie Madill5b772312018-03-08 20:28:32 -05003171bool ValidateDrawElementsInstancedCommon(Context *context,
Jamie Madill493f9572018-05-24 19:52:15 -04003172 PrimitiveMode mode,
Jamie Madill9c9b40a2017-04-26 16:31:57 -04003173 GLsizei count,
3174 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -04003175 const void *indices,
Jamie Madill9c9b40a2017-04-26 16:31:57 -04003176 GLsizei primcount)
Jamie Madillfd716582014-06-06 17:09:04 -04003177{
Corentin Wallez0dc97812017-06-22 14:38:44 -04003178 return ValidateDrawElementsInstancedBase(context, mode, count, type, indices, primcount);
Jamie Madill250d33f2014-06-06 17:09:03 -04003179}
3180
Geoff Lang3edfe032015-09-04 16:38:24 -04003181bool ValidateDrawElementsInstancedANGLE(Context *context,
Jamie Madill493f9572018-05-24 19:52:15 -04003182 PrimitiveMode mode,
Geoff Lang3edfe032015-09-04 16:38:24 -04003183 GLsizei count,
3184 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -04003185 const void *indices,
Jamie Madill9c9b40a2017-04-26 16:31:57 -04003186 GLsizei primcount)
Geoff Lang87a93302014-09-16 13:29:43 -04003187{
Geoff Lang63c5a592017-09-27 14:08:16 -04003188 if (!context->getExtensions().instancedArrays)
3189 {
3190 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
3191 return false;
3192 }
3193
Corentin Wallez170efbf2017-05-02 13:45:01 -04003194 if (!ValidateDrawElementsInstancedBase(context, mode, count, type, indices, primcount))
Geoff Lang87a93302014-09-16 13:29:43 -04003195 {
3196 return false;
3197 }
3198
Corentin Wallez0dc97812017-06-22 14:38:44 -04003199 return ValidateDrawInstancedANGLE(context);
Geoff Lang87a93302014-09-16 13:29:43 -04003200}
3201
He Yunchaoced53ae2016-11-29 15:00:51 +08003202bool ValidateFramebufferTextureBase(Context *context,
3203 GLenum target,
3204 GLenum attachment,
3205 GLuint texture,
3206 GLint level)
Jamie Madill570f7c82014-07-03 10:38:54 -04003207{
Geoff Lange8afa902017-09-27 15:00:43 -04003208 if (!ValidFramebufferTarget(context, target))
Jamie Madill55ec3b12014-07-03 10:38:57 -04003209 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003210 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidFramebufferTarget);
Geoff Langb1196682014-07-23 13:47:29 -04003211 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04003212 }
3213
3214 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill570f7c82014-07-03 10:38:54 -04003215 {
3216 return false;
3217 }
3218
Jamie Madill55ec3b12014-07-03 10:38:57 -04003219 if (texture != 0)
3220 {
3221 gl::Texture *tex = context->getTexture(texture);
3222
Luc Ferronadcf0ae2018-01-24 08:27:37 -05003223 if (tex == nullptr)
Jamie Madill55ec3b12014-07-03 10:38:57 -04003224 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003225 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04003226 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04003227 }
3228
3229 if (level < 0)
3230 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003231 context->handleError(InvalidValue());
Geoff Langb1196682014-07-23 13:47:29 -04003232 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04003233 }
3234 }
3235
Jamie Madilldfde6ab2016-06-09 07:07:18 -07003236 const gl::Framebuffer *framebuffer = context->getGLState().getTargetFramebuffer(target);
Jamie Madill84115c92015-04-23 15:00:07 -04003237 ASSERT(framebuffer);
Jamie Madill55ec3b12014-07-03 10:38:57 -04003238
Jamie Madill84115c92015-04-23 15:00:07 -04003239 if (framebuffer->id() == 0)
Jamie Madill55ec3b12014-07-03 10:38:57 -04003240 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003241 ANGLE_VALIDATION_ERR(context, InvalidOperation(), DefaultFramebufferTarget);
Geoff Langb1196682014-07-23 13:47:29 -04003242 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04003243 }
3244
3245 return true;
3246}
3247
Geoff Langb1196682014-07-23 13:47:29 -04003248bool ValidateGetUniformBase(Context *context, GLuint program, GLint location)
Jamie Madill0063c512014-08-25 15:47:53 -04003249{
3250 if (program == 0)
3251 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003252 context->handleError(InvalidValue());
Geoff Langb1196682014-07-23 13:47:29 -04003253 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04003254 }
3255
Dian Xiang769769a2015-09-09 15:20:08 -07003256 gl::Program *programObject = GetValidProgram(context, program);
3257 if (!programObject)
Shannon Woods4de4fd62014-11-07 16:22:02 -05003258 {
3259 return false;
3260 }
3261
Jamie Madill0063c512014-08-25 15:47:53 -04003262 if (!programObject || !programObject->isLinked())
3263 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003264 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ProgramNotLinked);
Geoff Langb1196682014-07-23 13:47:29 -04003265 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04003266 }
3267
Geoff Lang7dd2e102014-11-10 15:19:26 -05003268 if (!programObject->isValidUniformLocation(location))
Jamie Madill549c7fd2014-08-25 15:47:56 -04003269 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003270 context->handleError(InvalidOperation());
Geoff Langb1196682014-07-23 13:47:29 -04003271 return false;
Jamie Madill549c7fd2014-08-25 15:47:56 -04003272 }
3273
Jamie Madill0063c512014-08-25 15:47:53 -04003274 return true;
3275}
3276
Geoff Langf41d0ee2016-10-07 13:04:23 -04003277static bool ValidateSizedGetUniform(Context *context,
3278 GLuint program,
3279 GLint location,
3280 GLsizei bufSize,
3281 GLsizei *length)
Jamie Madill78f41802014-08-25 15:47:55 -04003282{
Geoff Langf41d0ee2016-10-07 13:04:23 -04003283 if (length)
3284 {
3285 *length = 0;
3286 }
3287
Jamie Madill78f41802014-08-25 15:47:55 -04003288 if (!ValidateGetUniformBase(context, program, location))
Jamie Madill0063c512014-08-25 15:47:53 -04003289 {
Jamie Madill78f41802014-08-25 15:47:55 -04003290 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04003291 }
3292
Geoff Langf41d0ee2016-10-07 13:04:23 -04003293 if (bufSize < 0)
3294 {
Brandon Jonesafa75152017-07-21 13:11:29 -07003295 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeBufferSize);
Geoff Langf41d0ee2016-10-07 13:04:23 -04003296 return false;
3297 }
3298
Jamie Madilla502c742014-08-28 17:19:13 -04003299 gl::Program *programObject = context->getProgram(program);
3300 ASSERT(programObject);
Jamie Madill0063c512014-08-25 15:47:53 -04003301
Jamie Madill78f41802014-08-25 15:47:55 -04003302 // sized queries -- ensure the provided buffer is large enough
Jamie Madill62d31cb2015-09-11 13:25:51 -04003303 const LinkedUniform &uniform = programObject->getUniformByLocation(location);
He Yunchaoced53ae2016-11-29 15:00:51 +08003304 size_t requiredBytes = VariableExternalSize(uniform.type);
Jamie Madill78f41802014-08-25 15:47:55 -04003305 if (static_cast<size_t>(bufSize) < requiredBytes)
Jamie Madill0063c512014-08-25 15:47:53 -04003306 {
Brandon Jonesafa75152017-07-21 13:11:29 -07003307 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InsufficientBufferSize);
Geoff Langb1196682014-07-23 13:47:29 -04003308 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04003309 }
3310
Geoff Langf41d0ee2016-10-07 13:04:23 -04003311 if (length)
3312 {
Geoff Lang94177fb2016-11-14 16:12:26 -05003313 *length = VariableComponentCount(uniform.type);
Geoff Langf41d0ee2016-10-07 13:04:23 -04003314 }
3315
Jamie Madill0063c512014-08-25 15:47:53 -04003316 return true;
3317}
3318
He Yunchaoced53ae2016-11-29 15:00:51 +08003319bool ValidateGetnUniformfvEXT(Context *context,
3320 GLuint program,
3321 GLint location,
3322 GLsizei bufSize,
3323 GLfloat *params)
Jamie Madill0063c512014-08-25 15:47:53 -04003324{
Geoff Langf41d0ee2016-10-07 13:04:23 -04003325 return ValidateSizedGetUniform(context, program, location, bufSize, nullptr);
Jamie Madill0063c512014-08-25 15:47:53 -04003326}
3327
Brandon Jonesfe4bbe62018-04-06 13:50:14 -07003328bool ValidateGetnUniformfvRobustANGLE(Context *context,
3329 GLuint program,
3330 GLint location,
3331 GLsizei bufSize,
3332 GLsizei *length,
3333 GLfloat *params)
3334{
3335 UNIMPLEMENTED();
3336 return false;
3337}
3338
He Yunchaoced53ae2016-11-29 15:00:51 +08003339bool ValidateGetnUniformivEXT(Context *context,
3340 GLuint program,
3341 GLint location,
3342 GLsizei bufSize,
3343 GLint *params)
Jamie Madill0063c512014-08-25 15:47:53 -04003344{
Geoff Langf41d0ee2016-10-07 13:04:23 -04003345 return ValidateSizedGetUniform(context, program, location, bufSize, nullptr);
3346}
3347
Brandon Jonesfe4bbe62018-04-06 13:50:14 -07003348bool ValidateGetnUniformivRobustANGLE(Context *context,
3349 GLuint program,
3350 GLint location,
3351 GLsizei bufSize,
3352 GLsizei *length,
3353 GLint *params)
3354{
3355 UNIMPLEMENTED();
3356 return false;
3357}
3358
3359bool ValidateGetnUniformuivRobustANGLE(Context *context,
3360 GLuint program,
3361 GLint location,
3362 GLsizei bufSize,
3363 GLsizei *length,
3364 GLuint *params)
3365{
3366 UNIMPLEMENTED();
3367 return false;
3368}
3369
Geoff Langf41d0ee2016-10-07 13:04:23 -04003370bool ValidateGetUniformfvRobustANGLE(Context *context,
3371 GLuint program,
3372 GLint location,
3373 GLsizei bufSize,
3374 GLsizei *length,
3375 GLfloat *params)
3376{
3377 if (!ValidateRobustEntryPoint(context, bufSize))
3378 {
3379 return false;
3380 }
3381
Brandon Jonesd1049182018-03-28 10:02:20 -07003382 GLsizei writeLength = 0;
3383
Geoff Langf41d0ee2016-10-07 13:04:23 -04003384 // bufSize is validated in ValidateSizedGetUniform
Brandon Jonesd1049182018-03-28 10:02:20 -07003385 if (!ValidateSizedGetUniform(context, program, location, bufSize, &writeLength))
3386 {
3387 return false;
3388 }
3389
3390 SetRobustLengthParam(length, writeLength);
3391
3392 return true;
Geoff Langf41d0ee2016-10-07 13:04:23 -04003393}
3394
3395bool ValidateGetUniformivRobustANGLE(Context *context,
3396 GLuint program,
3397 GLint location,
3398 GLsizei bufSize,
3399 GLsizei *length,
3400 GLint *params)
3401{
3402 if (!ValidateRobustEntryPoint(context, bufSize))
3403 {
3404 return false;
3405 }
3406
Brandon Jonesd1049182018-03-28 10:02:20 -07003407 GLsizei writeLength = 0;
3408
Geoff Langf41d0ee2016-10-07 13:04:23 -04003409 // bufSize is validated in ValidateSizedGetUniform
Brandon Jonesd1049182018-03-28 10:02:20 -07003410 if (!ValidateSizedGetUniform(context, program, location, bufSize, &writeLength))
3411 {
3412 return false;
3413 }
3414
3415 SetRobustLengthParam(length, writeLength);
3416
3417 return true;
Geoff Langf41d0ee2016-10-07 13:04:23 -04003418}
3419
3420bool ValidateGetUniformuivRobustANGLE(Context *context,
3421 GLuint program,
3422 GLint location,
3423 GLsizei bufSize,
3424 GLsizei *length,
3425 GLuint *params)
3426{
3427 if (!ValidateRobustEntryPoint(context, bufSize))
3428 {
3429 return false;
3430 }
3431
3432 if (context->getClientMajorVersion() < 3)
3433 {
Yunchao Hef0fd87d2017-09-12 04:55:05 +08003434 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ES3Required);
Geoff Langf41d0ee2016-10-07 13:04:23 -04003435 return false;
3436 }
3437
Brandon Jonesd1049182018-03-28 10:02:20 -07003438 GLsizei writeLength = 0;
3439
Geoff Langf41d0ee2016-10-07 13:04:23 -04003440 // bufSize is validated in ValidateSizedGetUniform
Brandon Jonesd1049182018-03-28 10:02:20 -07003441 if (!ValidateSizedGetUniform(context, program, location, bufSize, &writeLength))
3442 {
3443 return false;
3444 }
3445
3446 SetRobustLengthParam(length, writeLength);
3447
3448 return true;
Jamie Madill0063c512014-08-25 15:47:53 -04003449}
3450
He Yunchaoced53ae2016-11-29 15:00:51 +08003451bool ValidateDiscardFramebufferBase(Context *context,
3452 GLenum target,
3453 GLsizei numAttachments,
3454 const GLenum *attachments,
3455 bool defaultFramebuffer)
Austin Kinross08332632015-05-05 13:35:47 -07003456{
3457 if (numAttachments < 0)
3458 {
Brandon Jonesafa75152017-07-21 13:11:29 -07003459 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeAttachments);
Austin Kinross08332632015-05-05 13:35:47 -07003460 return false;
3461 }
3462
3463 for (GLsizei i = 0; i < numAttachments; ++i)
3464 {
Olli Etuaho84c9f592016-03-09 14:37:25 +02003465 if (attachments[i] >= GL_COLOR_ATTACHMENT0 && attachments[i] <= GL_COLOR_ATTACHMENT31)
Austin Kinross08332632015-05-05 13:35:47 -07003466 {
3467 if (defaultFramebuffer)
3468 {
Brandon Jonesafa75152017-07-21 13:11:29 -07003469 ANGLE_VALIDATION_ERR(context, InvalidEnum(), DefaultFramebufferInvalidAttachment);
Austin Kinross08332632015-05-05 13:35:47 -07003470 return false;
3471 }
3472
3473 if (attachments[i] >= GL_COLOR_ATTACHMENT0 + context->getCaps().maxColorAttachments)
3474 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003475 context->handleError(InvalidOperation() << "Requested color attachment is "
3476 "greater than the maximum supported "
3477 "color attachments");
Austin Kinross08332632015-05-05 13:35:47 -07003478 return false;
3479 }
3480 }
3481 else
3482 {
3483 switch (attachments[i])
3484 {
He Yunchaoced53ae2016-11-29 15:00:51 +08003485 case GL_DEPTH_ATTACHMENT:
3486 case GL_STENCIL_ATTACHMENT:
3487 case GL_DEPTH_STENCIL_ATTACHMENT:
3488 if (defaultFramebuffer)
3489 {
Brandon Jonesafa75152017-07-21 13:11:29 -07003490 ANGLE_VALIDATION_ERR(context, InvalidEnum(),
3491 DefaultFramebufferInvalidAttachment);
He Yunchaoced53ae2016-11-29 15:00:51 +08003492 return false;
3493 }
3494 break;
3495 case GL_COLOR:
3496 case GL_DEPTH:
3497 case GL_STENCIL:
3498 if (!defaultFramebuffer)
3499 {
Brandon Jonesafa75152017-07-21 13:11:29 -07003500 ANGLE_VALIDATION_ERR(context, InvalidEnum(),
3501 DefaultFramebufferInvalidAttachment);
He Yunchaoced53ae2016-11-29 15:00:51 +08003502 return false;
3503 }
3504 break;
3505 default:
Brandon Jonesafa75152017-07-21 13:11:29 -07003506 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidAttachment);
Austin Kinross08332632015-05-05 13:35:47 -07003507 return false;
Austin Kinross08332632015-05-05 13:35:47 -07003508 }
3509 }
3510 }
3511
3512 return true;
3513}
3514
Austin Kinross6ee1e782015-05-29 17:05:37 -07003515bool ValidateInsertEventMarkerEXT(Context *context, GLsizei length, const char *marker)
3516{
Jamie Madill007530e2017-12-28 14:27:04 -05003517 if (!context->getExtensions().debugMarker)
3518 {
3519 // The debug marker calls should not set error state
3520 // However, it seems reasonable to set an error state if the extension is not enabled
3521 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
3522 return false;
3523 }
Austin Kinross6ee1e782015-05-29 17:05:37 -07003524
Jamie Madill007530e2017-12-28 14:27:04 -05003525 // Note that debug marker calls must not set error state
Austin Kinross6ee1e782015-05-29 17:05:37 -07003526 if (length < 0)
3527 {
3528 return false;
3529 }
3530
3531 if (marker == nullptr)
3532 {
3533 return false;
3534 }
3535
3536 return true;
3537}
3538
3539bool ValidatePushGroupMarkerEXT(Context *context, GLsizei length, const char *marker)
3540{
Jamie Madill007530e2017-12-28 14:27:04 -05003541 if (!context->getExtensions().debugMarker)
3542 {
3543 // The debug marker calls should not set error state
3544 // However, it seems reasonable to set an error state if the extension is not enabled
3545 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ExtensionNotEnabled);
3546 return false;
3547 }
Austin Kinross6ee1e782015-05-29 17:05:37 -07003548
Jamie Madill007530e2017-12-28 14:27:04 -05003549 // Note that debug marker calls must not set error state
Austin Kinross6ee1e782015-05-29 17:05:37 -07003550 if (length < 0)
3551 {
3552 return false;
3553 }
3554
3555 if (length > 0 && marker == nullptr)
3556 {
3557 return false;
3558 }
3559
3560 return true;
3561}
3562
Corentin Wallezf0e89be2017-11-08 14:00:32 -08003563bool ValidateEGLImageTargetTexture2DOES(Context *context, TextureType type, GLeglImageOES image)
Geoff Langdcab33b2015-07-21 13:03:16 -04003564{
Geoff Langa8406172015-07-21 16:53:39 -04003565 if (!context->getExtensions().eglImage && !context->getExtensions().eglImageExternal)
3566 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003567 context->handleError(InvalidOperation());
Geoff Langa8406172015-07-21 16:53:39 -04003568 return false;
3569 }
3570
Corentin Wallezf0e89be2017-11-08 14:00:32 -08003571 switch (type)
Geoff Langa8406172015-07-21 16:53:39 -04003572 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08003573 case TextureType::_2D:
Geoff Langb66a9092016-05-16 15:59:14 -04003574 if (!context->getExtensions().eglImage)
3575 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003576 context->handleError(InvalidEnum()
3577 << "GL_TEXTURE_2D texture target requires GL_OES_EGL_image.");
Geoff Langb66a9092016-05-16 15:59:14 -04003578 }
3579 break;
3580
Corentin Wallezf0e89be2017-11-08 14:00:32 -08003581 case TextureType::External:
Geoff Langb66a9092016-05-16 15:59:14 -04003582 if (!context->getExtensions().eglImageExternal)
3583 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003584 context->handleError(InvalidEnum() << "GL_TEXTURE_EXTERNAL_OES texture target "
3585 "requires GL_OES_EGL_image_external.");
Geoff Langb66a9092016-05-16 15:59:14 -04003586 }
Geoff Langa8406172015-07-21 16:53:39 -04003587 break;
3588
3589 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07003590 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTextureTarget);
Geoff Langa8406172015-07-21 16:53:39 -04003591 return false;
3592 }
3593
Jamie Madill007530e2017-12-28 14:27:04 -05003594 egl::Image *imageObject = reinterpret_cast<egl::Image *>(image);
3595
Jamie Madill61e16b42017-06-19 11:13:23 -04003596 ASSERT(context->getCurrentDisplay());
Jamie Madill007530e2017-12-28 14:27:04 -05003597 if (!context->getCurrentDisplay()->isValidImage(imageObject))
Geoff Langa8406172015-07-21 16:53:39 -04003598 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003599 context->handleError(InvalidValue() << "EGL image is not valid.");
Geoff Langa8406172015-07-21 16:53:39 -04003600 return false;
3601 }
3602
Jamie Madill007530e2017-12-28 14:27:04 -05003603 if (imageObject->getSamples() > 0)
Geoff Langa8406172015-07-21 16:53:39 -04003604 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003605 context->handleError(InvalidOperation()
3606 << "cannot create a 2D texture from a multisampled EGL image.");
Geoff Langa8406172015-07-21 16:53:39 -04003607 return false;
3608 }
3609
Geoff Langca271392017-04-05 12:30:00 -04003610 const TextureCaps &textureCaps =
Jamie Madill007530e2017-12-28 14:27:04 -05003611 context->getTextureCaps().get(imageObject->getFormat().info->sizedInternalFormat);
Geoff Langa8406172015-07-21 16:53:39 -04003612 if (!textureCaps.texturable)
3613 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003614 context->handleError(InvalidOperation()
3615 << "EGL image internal format is not supported as a texture.");
Geoff Langa8406172015-07-21 16:53:39 -04003616 return false;
3617 }
3618
Geoff Langdcab33b2015-07-21 13:03:16 -04003619 return true;
3620}
3621
3622bool ValidateEGLImageTargetRenderbufferStorageOES(Context *context,
Geoff Langdcab33b2015-07-21 13:03:16 -04003623 GLenum target,
Jamie Madill007530e2017-12-28 14:27:04 -05003624 GLeglImageOES image)
Geoff Langdcab33b2015-07-21 13:03:16 -04003625{
Geoff Langa8406172015-07-21 16:53:39 -04003626 if (!context->getExtensions().eglImage)
3627 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003628 context->handleError(InvalidOperation());
Geoff Langa8406172015-07-21 16:53:39 -04003629 return false;
3630 }
3631
3632 switch (target)
3633 {
3634 case GL_RENDERBUFFER:
3635 break;
3636
3637 default:
Brandon Jonesafa75152017-07-21 13:11:29 -07003638 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidRenderbufferTarget);
Geoff Langa8406172015-07-21 16:53:39 -04003639 return false;
3640 }
3641
Jamie Madill007530e2017-12-28 14:27:04 -05003642 egl::Image *imageObject = reinterpret_cast<egl::Image *>(image);
3643
Jamie Madill61e16b42017-06-19 11:13:23 -04003644 ASSERT(context->getCurrentDisplay());
Jamie Madill007530e2017-12-28 14:27:04 -05003645 if (!context->getCurrentDisplay()->isValidImage(imageObject))
Geoff Langa8406172015-07-21 16:53:39 -04003646 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003647 context->handleError(InvalidValue() << "EGL image is not valid.");
Geoff Langa8406172015-07-21 16:53:39 -04003648 return false;
3649 }
3650
Geoff Langca271392017-04-05 12:30:00 -04003651 const TextureCaps &textureCaps =
Jamie Madill007530e2017-12-28 14:27:04 -05003652 context->getTextureCaps().get(imageObject->getFormat().info->sizedInternalFormat);
Yuly Novikovf15f8862018-06-04 18:59:41 -04003653 if (!textureCaps.renderbuffer)
Geoff Langa8406172015-07-21 16:53:39 -04003654 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003655 context->handleError(InvalidOperation()
3656 << "EGL image internal format is not supported as a renderbuffer.");
Geoff Langa8406172015-07-21 16:53:39 -04003657 return false;
3658 }
3659
Geoff Langdcab33b2015-07-21 13:03:16 -04003660 return true;
3661}
Austin Kinrossbc781f32015-10-26 09:27:38 -07003662
3663bool ValidateBindVertexArrayBase(Context *context, GLuint array)
3664{
Geoff Lang36167ab2015-12-07 10:27:14 -05003665 if (!context->isVertexArrayGenerated(array))
Austin Kinrossbc781f32015-10-26 09:27:38 -07003666 {
3667 // The default VAO should always exist
3668 ASSERT(array != 0);
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003669 context->handleError(InvalidOperation());
Austin Kinrossbc781f32015-10-26 09:27:38 -07003670 return false;
3671 }
3672
3673 return true;
3674}
3675
Geoff Langc5629752015-12-07 16:29:04 -05003676bool ValidateProgramBinaryBase(Context *context,
3677 GLuint program,
3678 GLenum binaryFormat,
3679 const void *binary,
3680 GLint length)
3681{
3682 Program *programObject = GetValidProgram(context, program);
3683 if (programObject == nullptr)
3684 {
3685 return false;
3686 }
3687
3688 const std::vector<GLenum> &programBinaryFormats = context->getCaps().programBinaryFormats;
3689 if (std::find(programBinaryFormats.begin(), programBinaryFormats.end(), binaryFormat) ==
3690 programBinaryFormats.end())
3691 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003692 context->handleError(InvalidEnum() << "Program binary format is not valid.");
Geoff Langc5629752015-12-07 16:29:04 -05003693 return false;
3694 }
3695
Olli Etuahoc3e55a42016-03-09 16:29:18 +02003696 if (context->hasActiveTransformFeedback(program))
3697 {
3698 // ES 3.0.4 section 2.15 page 91
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003699 context->handleError(InvalidOperation() << "Cannot change program binary while program "
3700 "is associated with an active transform "
3701 "feedback object.");
Olli Etuahoc3e55a42016-03-09 16:29:18 +02003702 return false;
3703 }
3704
Geoff Langc5629752015-12-07 16:29:04 -05003705 return true;
3706}
3707
3708bool ValidateGetProgramBinaryBase(Context *context,
3709 GLuint program,
3710 GLsizei bufSize,
3711 GLsizei *length,
3712 GLenum *binaryFormat,
3713 void *binary)
3714{
3715 Program *programObject = GetValidProgram(context, program);
3716 if (programObject == nullptr)
3717 {
3718 return false;
3719 }
3720
3721 if (!programObject->isLinked())
3722 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003723 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ProgramNotLinked);
Geoff Langc5629752015-12-07 16:29:04 -05003724 return false;
3725 }
3726
Jamie Madilla7d12dc2016-12-13 15:08:19 -05003727 if (context->getCaps().programBinaryFormats.empty())
3728 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003729 context->handleError(InvalidOperation() << "No program binary formats supported.");
Jamie Madilla7d12dc2016-12-13 15:08:19 -05003730 return false;
3731 }
3732
Geoff Langc5629752015-12-07 16:29:04 -05003733 return true;
3734}
Jamie Madillc29968b2016-01-20 11:17:23 -05003735
Jamie Madill5b772312018-03-08 20:28:32 -05003736bool ValidateDrawBuffersBase(Context *context, GLsizei n, const GLenum *bufs)
Jamie Madillc29968b2016-01-20 11:17:23 -05003737{
3738 // INVALID_VALUE is generated if n is negative or greater than value of MAX_DRAW_BUFFERS
Brandon Jonesafa75152017-07-21 13:11:29 -07003739 if (n < 0)
Jamie Madillc29968b2016-01-20 11:17:23 -05003740 {
Brandon Jonesafa75152017-07-21 13:11:29 -07003741 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeCount);
3742 return false;
3743 }
3744 if (static_cast<GLuint>(n) > context->getCaps().maxDrawBuffers)
3745 {
3746 ANGLE_VALIDATION_ERR(context, InvalidValue(), IndexExceedsMaxDrawBuffer);
Jamie Madillc29968b2016-01-20 11:17:23 -05003747 return false;
3748 }
3749
Jamie Madilldfde6ab2016-06-09 07:07:18 -07003750 ASSERT(context->getGLState().getDrawFramebuffer());
3751 GLuint frameBufferId = context->getGLState().getDrawFramebuffer()->id();
Jamie Madillc29968b2016-01-20 11:17:23 -05003752 GLuint maxColorAttachment = GL_COLOR_ATTACHMENT0_EXT + context->getCaps().maxColorAttachments;
3753
3754 // This should come first before the check for the default frame buffer
3755 // because when we switch to ES3.1+, invalid enums will return INVALID_ENUM
3756 // rather than INVALID_OPERATION
3757 for (int colorAttachment = 0; colorAttachment < n; colorAttachment++)
3758 {
3759 const GLenum attachment = GL_COLOR_ATTACHMENT0_EXT + colorAttachment;
3760
3761 if (bufs[colorAttachment] != GL_NONE && bufs[colorAttachment] != GL_BACK &&
Olli Etuaho84c9f592016-03-09 14:37:25 +02003762 (bufs[colorAttachment] < GL_COLOR_ATTACHMENT0 ||
3763 bufs[colorAttachment] > GL_COLOR_ATTACHMENT31))
Jamie Madillc29968b2016-01-20 11:17:23 -05003764 {
3765 // Value in bufs is not NONE, BACK, or GL_COLOR_ATTACHMENTi
Olli Etuaho84c9f592016-03-09 14:37:25 +02003766 // The 3.0.4 spec says to generate GL_INVALID_OPERATION here, but this
3767 // was changed to GL_INVALID_ENUM in 3.1, which dEQP also expects.
3768 // 3.1 is still a bit ambiguous about the error, but future specs are
3769 // expected to clarify that GL_INVALID_ENUM is the correct error.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003770 context->handleError(InvalidEnum() << "Invalid buffer value");
Olli Etuaho84c9f592016-03-09 14:37:25 +02003771 return false;
3772 }
3773 else if (bufs[colorAttachment] >= maxColorAttachment)
3774 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003775 context->handleError(InvalidOperation()
3776 << "Buffer value is greater than MAX_DRAW_BUFFERS");
Jamie Madillc29968b2016-01-20 11:17:23 -05003777 return false;
3778 }
3779 else if (bufs[colorAttachment] != GL_NONE && bufs[colorAttachment] != attachment &&
3780 frameBufferId != 0)
3781 {
3782 // INVALID_OPERATION-GL is bound to buffer and ith argument
3783 // is not COLOR_ATTACHMENTi or NONE
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003784 context->handleError(InvalidOperation()
3785 << "Ith value does not match COLOR_ATTACHMENTi or NONE");
Jamie Madillc29968b2016-01-20 11:17:23 -05003786 return false;
3787 }
3788 }
3789
3790 // INVALID_OPERATION is generated if GL is bound to the default framebuffer
3791 // and n is not 1 or bufs is bound to value other than BACK and NONE
3792 if (frameBufferId == 0)
3793 {
3794 if (n != 1)
3795 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003796 context->handleError(InvalidOperation()
3797 << "n must be 1 when GL is bound to the default framebuffer");
Jamie Madillc29968b2016-01-20 11:17:23 -05003798 return false;
3799 }
3800
3801 if (bufs[0] != GL_NONE && bufs[0] != GL_BACK)
3802 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003803 context->handleError(
3804 InvalidOperation()
3805 << "Only NONE or BACK are valid values when drawing to the default framebuffer");
Jamie Madillc29968b2016-01-20 11:17:23 -05003806 return false;
3807 }
3808 }
3809
3810 return true;
3811}
3812
Geoff Lang496c02d2016-10-20 11:38:11 -07003813bool ValidateGetBufferPointervBase(Context *context,
Corentin Wallez336129f2017-10-17 15:55:40 -04003814 BufferBinding target,
Geoff Lang496c02d2016-10-20 11:38:11 -07003815 GLenum pname,
3816 GLsizei *length,
3817 void **params)
Olli Etuaho4f667482016-03-30 15:56:35 +03003818{
Geoff Lang496c02d2016-10-20 11:38:11 -07003819 if (length)
3820 {
3821 *length = 0;
3822 }
3823
3824 if (context->getClientMajorVersion() < 3 && !context->getExtensions().mapBuffer)
3825 {
3826 context->handleError(
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003827 InvalidOperation()
3828 << "Context does not support OpenGL ES 3.0 or GL_OES_mapbuffer is not enabled.");
Geoff Lang496c02d2016-10-20 11:38:11 -07003829 return false;
3830 }
3831
Corentin Walleze4477002017-12-01 14:39:58 -05003832 if (!context->isValidBufferBinding(target))
Olli Etuaho4f667482016-03-30 15:56:35 +03003833 {
Corentin Wallez336129f2017-10-17 15:55:40 -04003834 context->handleError(InvalidEnum() << "Buffer target not valid");
Olli Etuaho4f667482016-03-30 15:56:35 +03003835 return false;
3836 }
3837
Geoff Lang496c02d2016-10-20 11:38:11 -07003838 switch (pname)
Olli Etuaho4f667482016-03-30 15:56:35 +03003839 {
Geoff Lang496c02d2016-10-20 11:38:11 -07003840 case GL_BUFFER_MAP_POINTER:
3841 break;
Olli Etuaho4f667482016-03-30 15:56:35 +03003842
Geoff Lang496c02d2016-10-20 11:38:11 -07003843 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07003844 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Geoff Lang496c02d2016-10-20 11:38:11 -07003845 return false;
3846 }
Olli Etuaho4f667482016-03-30 15:56:35 +03003847
3848 // GLES 3.0 section 2.10.1: "Attempts to attempts to modify or query buffer object state for a
3849 // target bound to zero generate an INVALID_OPERATION error."
3850 // GLES 3.1 section 6.6 explicitly specifies this error.
Geoff Lang496c02d2016-10-20 11:38:11 -07003851 if (context->getGLState().getTargetBuffer(target) == nullptr)
Olli Etuaho4f667482016-03-30 15:56:35 +03003852 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003853 context->handleError(InvalidOperation()
3854 << "Can not get pointer for reserved buffer name zero.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003855 return false;
3856 }
3857
Geoff Lang496c02d2016-10-20 11:38:11 -07003858 if (length)
3859 {
3860 *length = 1;
3861 }
3862
Olli Etuaho4f667482016-03-30 15:56:35 +03003863 return true;
3864}
3865
Corentin Wallez336129f2017-10-17 15:55:40 -04003866bool ValidateUnmapBufferBase(Context *context, BufferBinding target)
Olli Etuaho4f667482016-03-30 15:56:35 +03003867{
Corentin Walleze4477002017-12-01 14:39:58 -05003868 if (!context->isValidBufferBinding(target))
Olli Etuaho4f667482016-03-30 15:56:35 +03003869 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003870 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidBufferTypes);
Olli Etuaho4f667482016-03-30 15:56:35 +03003871 return false;
3872 }
3873
Jamie Madilldfde6ab2016-06-09 07:07:18 -07003874 Buffer *buffer = context->getGLState().getTargetBuffer(target);
Olli Etuaho4f667482016-03-30 15:56:35 +03003875
3876 if (buffer == nullptr || !buffer->isMapped())
3877 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003878 context->handleError(InvalidOperation() << "Buffer not mapped.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003879 return false;
3880 }
3881
3882 return true;
3883}
3884
3885bool ValidateMapBufferRangeBase(Context *context,
Corentin Wallez336129f2017-10-17 15:55:40 -04003886 BufferBinding target,
Olli Etuaho4f667482016-03-30 15:56:35 +03003887 GLintptr offset,
3888 GLsizeiptr length,
3889 GLbitfield access)
3890{
Corentin Walleze4477002017-12-01 14:39:58 -05003891 if (!context->isValidBufferBinding(target))
Olli Etuaho4f667482016-03-30 15:56:35 +03003892 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003893 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidBufferTypes);
Olli Etuaho4f667482016-03-30 15:56:35 +03003894 return false;
3895 }
3896
Brandon Jones6cad5662017-06-14 13:25:13 -07003897 if (offset < 0)
Olli Etuaho4f667482016-03-30 15:56:35 +03003898 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003899 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeOffset);
3900 return false;
3901 }
3902
3903 if (length < 0)
3904 {
3905 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeLength);
Olli Etuaho4f667482016-03-30 15:56:35 +03003906 return false;
3907 }
3908
Jamie Madilldfde6ab2016-06-09 07:07:18 -07003909 Buffer *buffer = context->getGLState().getTargetBuffer(target);
Olli Etuaho4f667482016-03-30 15:56:35 +03003910
3911 if (!buffer)
3912 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003913 context->handleError(InvalidOperation() << "Attempted to map buffer object zero.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003914 return false;
3915 }
3916
3917 // Check for buffer overflow
Jamie Madille2e406c2016-06-02 13:04:10 -04003918 CheckedNumeric<size_t> checkedOffset(offset);
3919 auto checkedSize = checkedOffset + length;
Olli Etuaho4f667482016-03-30 15:56:35 +03003920
Jamie Madille2e406c2016-06-02 13:04:10 -04003921 if (!checkedSize.IsValid() || checkedSize.ValueOrDie() > static_cast<size_t>(buffer->getSize()))
Olli Etuaho4f667482016-03-30 15:56:35 +03003922 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003923 context->handleError(InvalidValue() << "Mapped range does not fit into buffer dimensions.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003924 return false;
3925 }
3926
3927 // Check for invalid bits in the mask
3928 GLbitfield allAccessBits = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT |
3929 GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_FLUSH_EXPLICIT_BIT |
3930 GL_MAP_UNSYNCHRONIZED_BIT;
3931
3932 if (access & ~(allAccessBits))
3933 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003934 context->handleError(InvalidValue()
3935 << "Invalid access bits: 0x" << std::hex << std::uppercase << access);
Olli Etuaho4f667482016-03-30 15:56:35 +03003936 return false;
3937 }
3938
3939 if (length == 0)
3940 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003941 context->handleError(InvalidOperation() << "Buffer mapping length is zero.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003942 return false;
3943 }
3944
3945 if (buffer->isMapped())
3946 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003947 context->handleError(InvalidOperation() << "Buffer is already mapped.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003948 return false;
3949 }
3950
3951 // Check for invalid bit combinations
3952 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0)
3953 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003954 context->handleError(InvalidOperation()
3955 << "Need to map buffer for either reading or writing.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003956 return false;
3957 }
3958
3959 GLbitfield writeOnlyBits =
3960 GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_UNSYNCHRONIZED_BIT;
3961
3962 if ((access & GL_MAP_READ_BIT) != 0 && (access & writeOnlyBits) != 0)
3963 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003964 context->handleError(InvalidOperation()
3965 << "Invalid access bits when mapping buffer for reading: 0x"
3966 << std::hex << std::uppercase << access);
Olli Etuaho4f667482016-03-30 15:56:35 +03003967 return false;
3968 }
3969
3970 if ((access & GL_MAP_WRITE_BIT) == 0 && (access & GL_MAP_FLUSH_EXPLICIT_BIT) != 0)
3971 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05003972 context->handleError(
3973 InvalidOperation()
3974 << "The explicit flushing bit may only be set if the buffer is mapped for writing.");
Olli Etuaho4f667482016-03-30 15:56:35 +03003975 return false;
3976 }
Geoff Lang79f71042017-08-14 16:43:43 -04003977
3978 return ValidateMapBufferBase(context, target);
Olli Etuaho4f667482016-03-30 15:56:35 +03003979}
3980
3981bool ValidateFlushMappedBufferRangeBase(Context *context,
Corentin Wallez336129f2017-10-17 15:55:40 -04003982 BufferBinding target,
Olli Etuaho4f667482016-03-30 15:56:35 +03003983 GLintptr offset,
3984 GLsizeiptr length)
3985{
Brandon Jones6cad5662017-06-14 13:25:13 -07003986 if (offset < 0)
Olli Etuaho4f667482016-03-30 15:56:35 +03003987 {
Brandon Jones6cad5662017-06-14 13:25:13 -07003988 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeOffset);
3989 return false;
3990 }
3991
3992 if (length < 0)
3993 {
3994 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeLength);
Olli Etuaho4f667482016-03-30 15:56:35 +03003995 return false;
3996 }
3997
Corentin Walleze4477002017-12-01 14:39:58 -05003998 if (!context->isValidBufferBinding(target))
Olli Etuaho4f667482016-03-30 15:56:35 +03003999 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004000 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidBufferTypes);
Olli Etuaho4f667482016-03-30 15:56:35 +03004001 return false;
4002 }
4003
Jamie Madilldfde6ab2016-06-09 07:07:18 -07004004 Buffer *buffer = context->getGLState().getTargetBuffer(target);
Olli Etuaho4f667482016-03-30 15:56:35 +03004005
4006 if (buffer == nullptr)
4007 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004008 context->handleError(InvalidOperation() << "Attempted to flush buffer object zero.");
Olli Etuaho4f667482016-03-30 15:56:35 +03004009 return false;
4010 }
4011
4012 if (!buffer->isMapped() || (buffer->getAccessFlags() & GL_MAP_FLUSH_EXPLICIT_BIT) == 0)
4013 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004014 context->handleError(InvalidOperation()
4015 << "Attempted to flush a buffer not mapped for explicit flushing.");
Olli Etuaho4f667482016-03-30 15:56:35 +03004016 return false;
4017 }
4018
4019 // Check for buffer overflow
Jamie Madille2e406c2016-06-02 13:04:10 -04004020 CheckedNumeric<size_t> checkedOffset(offset);
4021 auto checkedSize = checkedOffset + length;
Olli Etuaho4f667482016-03-30 15:56:35 +03004022
Jamie Madille2e406c2016-06-02 13:04:10 -04004023 if (!checkedSize.IsValid() ||
4024 checkedSize.ValueOrDie() > static_cast<size_t>(buffer->getMapLength()))
Olli Etuaho4f667482016-03-30 15:56:35 +03004025 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004026 context->handleError(InvalidValue()
4027 << "Flushed range does not fit into buffer mapping dimensions.");
Olli Etuaho4f667482016-03-30 15:56:35 +03004028 return false;
4029 }
4030
4031 return true;
4032}
4033
Olli Etuaho41997e72016-03-10 13:38:39 +02004034bool ValidateGenOrDelete(Context *context, GLint n)
4035{
4036 if (n < 0)
4037 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004038 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeCount);
Olli Etuaho41997e72016-03-10 13:38:39 +02004039 return false;
4040 }
4041 return true;
4042}
4043
Jamie Madill5b772312018-03-08 20:28:32 -05004044bool ValidateRobustEntryPoint(Context *context, GLsizei bufSize)
Geoff Langff5b2d52016-09-07 11:32:23 -04004045{
4046 if (!context->getExtensions().robustClientMemory)
4047 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004048 context->handleError(InvalidOperation()
4049 << "GL_ANGLE_robust_client_memory is not available.");
Geoff Langff5b2d52016-09-07 11:32:23 -04004050 return false;
4051 }
4052
4053 if (bufSize < 0)
4054 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004055 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeBufferSize);
Geoff Langff5b2d52016-09-07 11:32:23 -04004056 return false;
4057 }
4058
4059 return true;
4060}
4061
Jamie Madill5b772312018-03-08 20:28:32 -05004062bool ValidateRobustBufferSize(Context *context, GLsizei bufSize, GLsizei numParams)
Geoff Lang2e43dbb2016-10-14 12:27:35 -04004063{
4064 if (bufSize < numParams)
4065 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004066 context->handleError(InvalidOperation() << numParams << " parameters are required but "
4067 << bufSize << " were provided.");
Geoff Lang2e43dbb2016-10-14 12:27:35 -04004068 return false;
4069 }
4070
4071 return true;
4072}
4073
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -08004074bool ValidateGetFramebufferAttachmentParameterivBase(Context *context,
Jamie Madillbe849e42017-05-02 15:49:00 -04004075 GLenum target,
4076 GLenum attachment,
4077 GLenum pname,
4078 GLsizei *numParams)
Geoff Langff5b2d52016-09-07 11:32:23 -04004079{
Geoff Lange8afa902017-09-27 15:00:43 -04004080 if (!ValidFramebufferTarget(context, target))
Geoff Langff5b2d52016-09-07 11:32:23 -04004081 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004082 context->handleError(InvalidEnum());
Geoff Langff5b2d52016-09-07 11:32:23 -04004083 return false;
4084 }
4085
4086 int clientVersion = context->getClientMajorVersion();
4087
4088 switch (pname)
4089 {
4090 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE:
4091 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME:
4092 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL:
4093 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE:
4094 break;
4095
Martin Radeve5285d22017-07-14 16:23:53 +03004096 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_NUM_VIEWS_ANGLE:
4097 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_MULTIVIEW_LAYOUT_ANGLE:
4098 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_BASE_VIEW_INDEX_ANGLE:
4099 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_VIEWPORT_OFFSETS_ANGLE:
4100 if (clientVersion < 3 || !context->getExtensions().multiview)
4101 {
4102 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
4103 return false;
4104 }
4105 break;
4106
Geoff Langff5b2d52016-09-07 11:32:23 -04004107 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
4108 if (clientVersion < 3 && !context->getExtensions().sRGB)
4109 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004110 context->handleError(InvalidEnum());
Geoff Langff5b2d52016-09-07 11:32:23 -04004111 return false;
4112 }
4113 break;
4114
4115 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
4116 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
4117 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
4118 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
4119 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
4120 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
4121 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
4122 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LAYER:
4123 if (clientVersion < 3)
4124 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004125 context->handleError(InvalidEnum());
Geoff Langff5b2d52016-09-07 11:32:23 -04004126 return false;
4127 }
4128 break;
4129
4130 default:
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004131 context->handleError(InvalidEnum());
Geoff Langff5b2d52016-09-07 11:32:23 -04004132 return false;
4133 }
4134
4135 // Determine if the attachment is a valid enum
4136 switch (attachment)
4137 {
4138 case GL_BACK:
Geoff Langff5b2d52016-09-07 11:32:23 -04004139 case GL_DEPTH:
4140 case GL_STENCIL:
Geoff Langff5b2d52016-09-07 11:32:23 -04004141 if (clientVersion < 3)
4142 {
Geoff Langfa125c92017-10-24 13:01:46 -04004143 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004144 return false;
4145 }
4146 break;
4147
Bryan Bernhart (Intel Americas Inc)491b0d62017-11-10 12:48:22 -08004148 case GL_DEPTH_STENCIL_ATTACHMENT:
4149 if (clientVersion < 3 && !context->isWebGL1())
4150 {
4151 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidAttachment);
4152 return false;
4153 }
4154 break;
4155
Geoff Langfa125c92017-10-24 13:01:46 -04004156 case GL_COLOR_ATTACHMENT0:
Geoff Langff5b2d52016-09-07 11:32:23 -04004157 case GL_DEPTH_ATTACHMENT:
4158 case GL_STENCIL_ATTACHMENT:
4159 break;
4160
4161 default:
Geoff Langfa125c92017-10-24 13:01:46 -04004162 if ((clientVersion < 3 && !context->getExtensions().drawBuffers) ||
4163 attachment < GL_COLOR_ATTACHMENT0_EXT ||
Geoff Langff5b2d52016-09-07 11:32:23 -04004164 (attachment - GL_COLOR_ATTACHMENT0_EXT) >= context->getCaps().maxColorAttachments)
4165 {
Geoff Langfa125c92017-10-24 13:01:46 -04004166 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004167 return false;
4168 }
4169 break;
4170 }
4171
4172 const Framebuffer *framebuffer = context->getGLState().getTargetFramebuffer(target);
4173 ASSERT(framebuffer);
4174
4175 if (framebuffer->id() == 0)
4176 {
4177 if (clientVersion < 3)
4178 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004179 ANGLE_VALIDATION_ERR(context, InvalidOperation(), DefaultFramebufferTarget);
Geoff Langff5b2d52016-09-07 11:32:23 -04004180 return false;
4181 }
4182
4183 switch (attachment)
4184 {
4185 case GL_BACK:
4186 case GL_DEPTH:
4187 case GL_STENCIL:
4188 break;
4189
4190 default:
Brandon Jonesafa75152017-07-21 13:11:29 -07004191 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004192 return false;
4193 }
4194 }
4195 else
4196 {
4197 if (attachment >= GL_COLOR_ATTACHMENT0_EXT && attachment <= GL_COLOR_ATTACHMENT15_EXT)
4198 {
4199 // Valid attachment query
4200 }
4201 else
4202 {
4203 switch (attachment)
4204 {
4205 case GL_DEPTH_ATTACHMENT:
4206 case GL_STENCIL_ATTACHMENT:
4207 break;
4208
4209 case GL_DEPTH_STENCIL_ATTACHMENT:
Bryan Bernhart (Intel Americas Inc)491b0d62017-11-10 12:48:22 -08004210 if (!framebuffer->hasValidDepthStencil() && !context->isWebGL1())
Geoff Langff5b2d52016-09-07 11:32:23 -04004211 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004212 context->handleError(InvalidOperation());
Geoff Langff5b2d52016-09-07 11:32:23 -04004213 return false;
4214 }
4215 break;
4216
4217 default:
Brandon Jonesafa75152017-07-21 13:11:29 -07004218 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004219 return false;
4220 }
4221 }
4222 }
4223
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -08004224 const FramebufferAttachment *attachmentObject = framebuffer->getAttachment(context, attachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004225 if (attachmentObject)
4226 {
4227 ASSERT(attachmentObject->type() == GL_RENDERBUFFER ||
4228 attachmentObject->type() == GL_TEXTURE ||
4229 attachmentObject->type() == GL_FRAMEBUFFER_DEFAULT);
4230
4231 switch (pname)
4232 {
4233 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME:
4234 if (attachmentObject->type() != GL_RENDERBUFFER &&
4235 attachmentObject->type() != GL_TEXTURE)
4236 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004237 ANGLE_VALIDATION_ERR(context, InvalidEnum(), FramebufferIncompleteAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004238 return false;
4239 }
4240 break;
4241
4242 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL:
4243 if (attachmentObject->type() != GL_TEXTURE)
4244 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004245 ANGLE_VALIDATION_ERR(context, InvalidEnum(), FramebufferIncompleteAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004246 return false;
4247 }
4248 break;
4249
4250 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE:
4251 if (attachmentObject->type() != GL_TEXTURE)
4252 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004253 ANGLE_VALIDATION_ERR(context, InvalidEnum(), FramebufferIncompleteAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004254 return false;
4255 }
4256 break;
4257
4258 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
4259 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT)
4260 {
Brandon Jonesafa75152017-07-21 13:11:29 -07004261 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004262 return false;
4263 }
4264 break;
4265
4266 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LAYER:
4267 if (attachmentObject->type() != GL_TEXTURE)
4268 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004269 ANGLE_VALIDATION_ERR(context, InvalidEnum(), FramebufferIncompleteAttachment);
Geoff Langff5b2d52016-09-07 11:32:23 -04004270 return false;
4271 }
4272 break;
4273
4274 default:
4275 break;
4276 }
4277 }
4278 else
4279 {
4280 // ES 2.0.25 spec pg 127 states that if the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE
4281 // is NONE, then querying any other pname will generate INVALID_ENUM.
4282
4283 // ES 3.0.2 spec pg 235 states that if the attachment type is none,
4284 // GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME will return zero and be an
4285 // INVALID_OPERATION for all other pnames
4286
4287 switch (pname)
4288 {
4289 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE:
4290 break;
4291
4292 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME:
4293 if (clientVersion < 3)
4294 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004295 ANGLE_VALIDATION_ERR(context, InvalidEnum(),
Bryan Bernhart (Intel Americas Inc)491b0d62017-11-10 12:48:22 -08004296 InvalidFramebufferAttachmentParameter);
Geoff Langff5b2d52016-09-07 11:32:23 -04004297 return false;
4298 }
4299 break;
4300
4301 default:
4302 if (clientVersion < 3)
4303 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004304 ANGLE_VALIDATION_ERR(context, InvalidEnum(),
Bryan Bernhart (Intel Americas Inc)491b0d62017-11-10 12:48:22 -08004305 InvalidFramebufferAttachmentParameter);
Geoff Langff5b2d52016-09-07 11:32:23 -04004306 return false;
4307 }
4308 else
4309 {
Brandon Jones6cad5662017-06-14 13:25:13 -07004310 ANGLE_VALIDATION_ERR(context, InvalidOperation(),
Bryan Bernhart (Intel Americas Inc)491b0d62017-11-10 12:48:22 -08004311 InvalidFramebufferAttachmentParameter);
Geoff Langff5b2d52016-09-07 11:32:23 -04004312 return false;
4313 }
4314 }
4315 }
4316
Martin Radeve5285d22017-07-14 16:23:53 +03004317 if (numParams)
4318 {
4319 if (pname == GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_VIEWPORT_OFFSETS_ANGLE)
4320 {
4321 // Only when the viewport offsets are queried we can have a varying number of output
4322 // parameters.
4323 const int numViews = attachmentObject ? attachmentObject->getNumViews() : 1;
4324 *numParams = numViews * 2;
4325 }
4326 else
4327 {
4328 // For all other queries we can have only one output parameter.
4329 *numParams = 1;
4330 }
4331 }
4332
Geoff Langff5b2d52016-09-07 11:32:23 -04004333 return true;
4334}
4335
Bryan Bernhart (Intel Americas Inc)2eeb1b32017-11-29 16:06:43 -08004336bool ValidateGetFramebufferAttachmentParameterivRobustANGLE(Context *context,
Geoff Langff5b2d52016-09-07 11:32:23 -04004337 GLenum target,
4338 GLenum attachment,
4339 GLenum pname,
4340 GLsizei bufSize,
Brandon Jonesd1049182018-03-28 10:02:20 -07004341 GLsizei *length,
4342 GLint *params)
Geoff Langff5b2d52016-09-07 11:32:23 -04004343{
4344 if (!ValidateRobustEntryPoint(context, bufSize))
4345 {
4346 return false;
4347 }
4348
Brandon Jonesd1049182018-03-28 10:02:20 -07004349 GLsizei numParams = 0;
Jamie Madillbe849e42017-05-02 15:49:00 -04004350 if (!ValidateGetFramebufferAttachmentParameterivBase(context, target, attachment, pname,
Brandon Jonesd1049182018-03-28 10:02:20 -07004351 &numParams))
Geoff Langff5b2d52016-09-07 11:32:23 -04004352 {
4353 return false;
4354 }
4355
Brandon Jonesd1049182018-03-28 10:02:20 -07004356 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Langff5b2d52016-09-07 11:32:23 -04004357 {
4358 return false;
4359 }
4360
Brandon Jonesd1049182018-03-28 10:02:20 -07004361 SetRobustLengthParam(length, numParams);
4362
Geoff Langff5b2d52016-09-07 11:32:23 -04004363 return true;
4364}
4365
Jamie Madill5b772312018-03-08 20:28:32 -05004366bool ValidateGetBufferParameterivRobustANGLE(Context *context,
Corentin Wallez336129f2017-10-17 15:55:40 -04004367 BufferBinding target,
Geoff Langff5b2d52016-09-07 11:32:23 -04004368 GLenum pname,
4369 GLsizei bufSize,
Geoff Langebebe1c2016-10-14 12:01:31 -04004370 GLsizei *length,
4371 GLint *params)
Geoff Langff5b2d52016-09-07 11:32:23 -04004372{
4373 if (!ValidateRobustEntryPoint(context, bufSize))
4374 {
4375 return false;
4376 }
4377
Brandon Jonesd1049182018-03-28 10:02:20 -07004378 GLsizei numParams = 0;
4379
4380 if (!ValidateGetBufferParameterBase(context, target, pname, false, &numParams))
Geoff Langff5b2d52016-09-07 11:32:23 -04004381 {
4382 return false;
4383 }
4384
Brandon Jonesd1049182018-03-28 10:02:20 -07004385 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Langebebe1c2016-10-14 12:01:31 -04004386 {
4387 return false;
4388 }
4389
Brandon Jonesd1049182018-03-28 10:02:20 -07004390 SetRobustLengthParam(length, numParams);
Geoff Langebebe1c2016-10-14 12:01:31 -04004391 return true;
4392}
4393
Jamie Madill5b772312018-03-08 20:28:32 -05004394bool ValidateGetBufferParameteri64vRobustANGLE(Context *context,
Corentin Wallez336129f2017-10-17 15:55:40 -04004395 BufferBinding target,
Geoff Langebebe1c2016-10-14 12:01:31 -04004396 GLenum pname,
4397 GLsizei bufSize,
4398 GLsizei *length,
4399 GLint64 *params)
4400{
Brandon Jonesd1049182018-03-28 10:02:20 -07004401 GLsizei numParams = 0;
4402
Geoff Langebebe1c2016-10-14 12:01:31 -04004403 if (!ValidateRobustEntryPoint(context, bufSize))
4404 {
4405 return false;
4406 }
4407
Brandon Jonesd1049182018-03-28 10:02:20 -07004408 if (!ValidateGetBufferParameterBase(context, target, pname, false, &numParams))
Geoff Langebebe1c2016-10-14 12:01:31 -04004409 {
4410 return false;
4411 }
4412
Brandon Jonesd1049182018-03-28 10:02:20 -07004413 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Langff5b2d52016-09-07 11:32:23 -04004414 {
4415 return false;
4416 }
4417
Brandon Jonesd1049182018-03-28 10:02:20 -07004418 SetRobustLengthParam(length, numParams);
4419
Geoff Langff5b2d52016-09-07 11:32:23 -04004420 return true;
4421}
4422
Jamie Madill5b772312018-03-08 20:28:32 -05004423bool ValidateGetProgramivBase(Context *context, GLuint program, GLenum pname, GLsizei *numParams)
Geoff Langff5b2d52016-09-07 11:32:23 -04004424{
4425 // Currently, all GetProgramiv queries return 1 parameter
Yunchao He33151a52017-04-13 09:58:17 +08004426 if (numParams)
4427 {
4428 *numParams = 1;
4429 }
Geoff Langff5b2d52016-09-07 11:32:23 -04004430
4431 Program *programObject = GetValidProgram(context, program);
4432 if (!programObject)
4433 {
4434 return false;
4435 }
4436
4437 switch (pname)
4438 {
4439 case GL_DELETE_STATUS:
4440 case GL_LINK_STATUS:
4441 case GL_VALIDATE_STATUS:
4442 case GL_INFO_LOG_LENGTH:
4443 case GL_ATTACHED_SHADERS:
4444 case GL_ACTIVE_ATTRIBUTES:
4445 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
4446 case GL_ACTIVE_UNIFORMS:
4447 case GL_ACTIVE_UNIFORM_MAX_LENGTH:
4448 break;
4449
4450 case GL_PROGRAM_BINARY_LENGTH:
4451 if (context->getClientMajorVersion() < 3 && !context->getExtensions().getProgramBinary)
4452 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05004453 context->handleError(InvalidEnum() << "Querying GL_PROGRAM_BINARY_LENGTH "
4454 "requires GL_OES_get_program_binary or "
4455 "ES 3.0.");
Geoff Langff5b2d52016-09-07 11:32:23 -04004456 return false;
4457 }
4458 break;
4459
4460 case GL_ACTIVE_UNIFORM_BLOCKS:
4461 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH:
4462 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE:
4463 case GL_TRANSFORM_FEEDBACK_VARYINGS:
4464 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH:
4465 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT:
4466 if (context->getClientMajorVersion() < 3)
4467 {
Yunchao Hef0fd87d2017-09-12 04:55:05 +08004468 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ES3Required);
Geoff Langff5b2d52016-09-07 11:32:23 -04004469 return false;
4470 }
4471 break;
4472
Yunchao He61afff12017-03-14 15:34:03 +08004473 case GL_PROGRAM_SEPARABLE:
jchen1058f67be2017-10-27 08:59:27 +08004474 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS:
Yunchao He61afff12017-03-14 15:34:03 +08004475 if (context->getClientVersion() < Version(3, 1))
4476 {
Yunchao Hef0fd87d2017-09-12 04:55:05 +08004477 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ES31Required);
Yunchao He61afff12017-03-14 15:34:03 +08004478 return false;
4479 }
4480 break;
4481
Jiawei Shao6ae51612018-02-23 14:03:25 +08004482 case GL_COMPUTE_WORK_GROUP_SIZE:
4483 if (context->getClientVersion() < Version(3, 1))
4484 {
4485 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ES31Required);
4486 return false;
4487 }
4488
4489 // [OpenGL ES 3.1] Chapter 7.12 Page 122
4490 // An INVALID_OPERATION error is generated if COMPUTE_WORK_GROUP_SIZE is queried for a
4491 // program which has not been linked successfully, or which does not contain objects to
4492 // form a compute shader.
4493 if (!programObject->isLinked())
4494 {
4495 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ProgramNotLinked);
4496 return false;
4497 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08004498 if (!programObject->hasLinkedShaderStage(ShaderType::Compute))
Jiawei Shao6ae51612018-02-23 14:03:25 +08004499 {
4500 ANGLE_VALIDATION_ERR(context, InvalidOperation(), NoActiveComputeShaderStage);
4501 return false;
4502 }
4503 break;
4504
Jiawei Shao447bfac2018-03-14 14:23:40 +08004505 case GL_GEOMETRY_LINKED_INPUT_TYPE_EXT:
4506 case GL_GEOMETRY_LINKED_OUTPUT_TYPE_EXT:
4507 case GL_GEOMETRY_LINKED_VERTICES_OUT_EXT:
4508 case GL_GEOMETRY_SHADER_INVOCATIONS_EXT:
4509 if (!context->getExtensions().geometryShader)
4510 {
4511 ANGLE_VALIDATION_ERR(context, InvalidEnum(), GeometryShaderExtensionNotEnabled);
4512 return false;
4513 }
4514
4515 // [EXT_geometry_shader] Chapter 7.12
4516 // An INVALID_OPERATION error is generated if GEOMETRY_LINKED_VERTICES_OUT_EXT,
4517 // GEOMETRY_LINKED_INPUT_TYPE_EXT, GEOMETRY_LINKED_OUTPUT_TYPE_EXT, or
4518 // GEOMETRY_SHADER_INVOCATIONS_EXT are queried for a program which has not been linked
4519 // successfully, or which does not contain objects to form a geometry shader.
4520 if (!programObject->isLinked())
4521 {
4522 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ProgramNotLinked);
4523 return false;
4524 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08004525 if (!programObject->hasLinkedShaderStage(ShaderType::Geometry))
Jiawei Shao447bfac2018-03-14 14:23:40 +08004526 {
4527 ANGLE_VALIDATION_ERR(context, InvalidOperation(), NoActiveGeometryShaderStage);
4528 return false;
4529 }
4530 break;
4531
Geoff Langff5b2d52016-09-07 11:32:23 -04004532 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07004533 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Geoff Langff5b2d52016-09-07 11:32:23 -04004534 return false;
4535 }
4536
4537 return true;
4538}
4539
4540bool ValidateGetProgramivRobustANGLE(Context *context,
4541 GLuint program,
4542 GLenum pname,
4543 GLsizei bufSize,
Brandon Jonesd1049182018-03-28 10:02:20 -07004544 GLsizei *length,
4545 GLint *params)
Geoff Langff5b2d52016-09-07 11:32:23 -04004546{
4547 if (!ValidateRobustEntryPoint(context, bufSize))
4548 {
4549 return false;
4550 }
4551
Brandon Jonesd1049182018-03-28 10:02:20 -07004552 GLsizei numParams = 0;
4553
4554 if (!ValidateGetProgramivBase(context, program, pname, &numParams))
Geoff Langff5b2d52016-09-07 11:32:23 -04004555 {
4556 return false;
4557 }
4558
Brandon Jonesd1049182018-03-28 10:02:20 -07004559 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Langff5b2d52016-09-07 11:32:23 -04004560 {
4561 return false;
4562 }
4563
Brandon Jonesd1049182018-03-28 10:02:20 -07004564 SetRobustLengthParam(length, numParams);
4565
Geoff Langff5b2d52016-09-07 11:32:23 -04004566 return true;
4567}
4568
Geoff Lang740d9022016-10-07 11:20:52 -04004569bool ValidateGetRenderbufferParameterivRobustANGLE(Context *context,
4570 GLenum target,
4571 GLenum pname,
4572 GLsizei bufSize,
4573 GLsizei *length,
4574 GLint *params)
4575{
4576 if (!ValidateRobustEntryPoint(context, bufSize))
4577 {
4578 return false;
4579 }
4580
Brandon Jonesd1049182018-03-28 10:02:20 -07004581 GLsizei numParams = 0;
4582
4583 if (!ValidateGetRenderbufferParameterivBase(context, target, pname, &numParams))
Geoff Lang740d9022016-10-07 11:20:52 -04004584 {
4585 return false;
4586 }
4587
Brandon Jonesd1049182018-03-28 10:02:20 -07004588 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Lang740d9022016-10-07 11:20:52 -04004589 {
4590 return false;
4591 }
4592
Brandon Jonesd1049182018-03-28 10:02:20 -07004593 SetRobustLengthParam(length, numParams);
4594
Geoff Lang740d9022016-10-07 11:20:52 -04004595 return true;
4596}
4597
Geoff Langd7d0ed32016-10-07 11:33:51 -04004598bool ValidateGetShaderivRobustANGLE(Context *context,
4599 GLuint shader,
4600 GLenum pname,
4601 GLsizei bufSize,
4602 GLsizei *length,
4603 GLint *params)
4604{
4605 if (!ValidateRobustEntryPoint(context, bufSize))
4606 {
4607 return false;
4608 }
4609
Brandon Jonesd1049182018-03-28 10:02:20 -07004610 GLsizei numParams = 0;
4611
4612 if (!ValidateGetShaderivBase(context, shader, pname, &numParams))
Geoff Langd7d0ed32016-10-07 11:33:51 -04004613 {
4614 return false;
4615 }
4616
Brandon Jonesd1049182018-03-28 10:02:20 -07004617 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Langd7d0ed32016-10-07 11:33:51 -04004618 {
4619 return false;
4620 }
4621
Brandon Jonesd1049182018-03-28 10:02:20 -07004622 SetRobustLengthParam(length, numParams);
4623
Geoff Langd7d0ed32016-10-07 11:33:51 -04004624 return true;
4625}
4626
Geoff Langc1984ed2016-10-07 12:41:00 -04004627bool ValidateGetTexParameterfvRobustANGLE(Context *context,
Corentin Wallezf0e89be2017-11-08 14:00:32 -08004628 TextureType target,
Geoff Langc1984ed2016-10-07 12:41:00 -04004629 GLenum pname,
4630 GLsizei bufSize,
4631 GLsizei *length,
4632 GLfloat *params)
4633{
4634 if (!ValidateRobustEntryPoint(context, bufSize))
4635 {
4636 return false;
4637 }
4638
Brandon Jonesd1049182018-03-28 10:02:20 -07004639 GLsizei numParams = 0;
4640
4641 if (!ValidateGetTexParameterBase(context, target, pname, &numParams))
Geoff Langc1984ed2016-10-07 12:41:00 -04004642 {
4643 return false;
4644 }
4645
Brandon Jonesd1049182018-03-28 10:02:20 -07004646 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Langc1984ed2016-10-07 12:41:00 -04004647 {
4648 return false;
4649 }
4650
Brandon Jonesd1049182018-03-28 10:02:20 -07004651 SetRobustLengthParam(length, numParams);
4652
Geoff Langc1984ed2016-10-07 12:41:00 -04004653 return true;
4654}
4655
Geoff Langc1984ed2016-10-07 12:41:00 -04004656bool ValidateGetTexParameterivRobustANGLE(Context *context,
Corentin Wallezf0e89be2017-11-08 14:00:32 -08004657 TextureType target,
Geoff Langc1984ed2016-10-07 12:41:00 -04004658 GLenum pname,
4659 GLsizei bufSize,
4660 GLsizei *length,
4661 GLint *params)
4662{
Brandon Jonesd1049182018-03-28 10:02:20 -07004663
Geoff Langc1984ed2016-10-07 12:41:00 -04004664 if (!ValidateRobustEntryPoint(context, bufSize))
4665 {
4666 return false;
4667 }
Brandon Jonesd1049182018-03-28 10:02:20 -07004668 GLsizei numParams = 0;
4669 if (!ValidateGetTexParameterBase(context, target, pname, &numParams))
Geoff Langc1984ed2016-10-07 12:41:00 -04004670 {
4671 return false;
4672 }
4673
Brandon Jonesd1049182018-03-28 10:02:20 -07004674 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Langc1984ed2016-10-07 12:41:00 -04004675 {
4676 return false;
4677 }
4678
Brandon Jonesd1049182018-03-28 10:02:20 -07004679 SetRobustLengthParam(length, numParams);
Geoff Langc1984ed2016-10-07 12:41:00 -04004680 return true;
4681}
4682
Brandon Jonesfe4bbe62018-04-06 13:50:14 -07004683bool ValidateGetTexParameterIivRobustANGLE(Context *context,
4684 TextureType target,
4685 GLenum pname,
4686 GLsizei bufSize,
4687 GLsizei *length,
4688 GLint *params)
4689{
4690 UNIMPLEMENTED();
4691 return false;
4692}
4693
4694bool ValidateGetTexParameterIuivRobustANGLE(Context *context,
4695 TextureType target,
4696 GLenum pname,
4697 GLsizei bufSize,
4698 GLsizei *length,
4699 GLuint *params)
4700{
4701 UNIMPLEMENTED();
4702 return false;
4703}
4704
Geoff Langc1984ed2016-10-07 12:41:00 -04004705bool ValidateTexParameterfvRobustANGLE(Context *context,
Corentin Wallezf0e89be2017-11-08 14:00:32 -08004706 TextureType target,
Geoff Langc1984ed2016-10-07 12:41:00 -04004707 GLenum pname,
4708 GLsizei bufSize,
4709 const GLfloat *params)
4710{
4711 if (!ValidateRobustEntryPoint(context, bufSize))
4712 {
4713 return false;
4714 }
4715
4716 return ValidateTexParameterBase(context, target, pname, bufSize, params);
4717}
4718
Geoff Langc1984ed2016-10-07 12:41:00 -04004719bool ValidateTexParameterivRobustANGLE(Context *context,
Corentin Wallezf0e89be2017-11-08 14:00:32 -08004720 TextureType target,
Geoff Langc1984ed2016-10-07 12:41:00 -04004721 GLenum pname,
4722 GLsizei bufSize,
4723 const GLint *params)
4724{
4725 if (!ValidateRobustEntryPoint(context, bufSize))
4726 {
4727 return false;
4728 }
4729
4730 return ValidateTexParameterBase(context, target, pname, bufSize, params);
4731}
4732
Brandon Jonesfe4bbe62018-04-06 13:50:14 -07004733bool ValidateTexParameterIivRobustANGLE(Context *context,
4734 TextureType target,
4735 GLenum pname,
4736 GLsizei bufSize,
4737 const GLint *params)
4738{
4739 UNIMPLEMENTED();
4740 return false;
4741}
4742
4743bool ValidateTexParameterIuivRobustANGLE(Context *context,
4744 TextureType target,
4745 GLenum pname,
4746 GLsizei bufSize,
4747 const GLuint *params)
4748{
4749 UNIMPLEMENTED();
4750 return false;
4751}
4752
Geoff Langc1984ed2016-10-07 12:41:00 -04004753bool ValidateGetSamplerParameterfvRobustANGLE(Context *context,
4754 GLuint sampler,
4755 GLenum pname,
4756 GLuint bufSize,
4757 GLsizei *length,
4758 GLfloat *params)
4759{
4760 if (!ValidateRobustEntryPoint(context, bufSize))
4761 {
4762 return false;
4763 }
4764
Brandon Jonesd1049182018-03-28 10:02:20 -07004765 GLsizei numParams = 0;
4766
4767 if (!ValidateGetSamplerParameterBase(context, sampler, pname, &numParams))
Geoff Langc1984ed2016-10-07 12:41:00 -04004768 {
4769 return false;
4770 }
4771
Brandon Jonesd1049182018-03-28 10:02:20 -07004772 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Langc1984ed2016-10-07 12:41:00 -04004773 {
4774 return false;
4775 }
4776
Brandon Jonesd1049182018-03-28 10:02:20 -07004777 SetRobustLengthParam(length, numParams);
Geoff Langc1984ed2016-10-07 12:41:00 -04004778 return true;
4779}
4780
Geoff Langc1984ed2016-10-07 12:41:00 -04004781bool ValidateGetSamplerParameterivRobustANGLE(Context *context,
4782 GLuint sampler,
4783 GLenum pname,
Brandon Jonesd1049182018-03-28 10:02:20 -07004784 GLsizei bufSize,
Geoff Langc1984ed2016-10-07 12:41:00 -04004785 GLsizei *length,
4786 GLint *params)
4787{
4788 if (!ValidateRobustEntryPoint(context, bufSize))
4789 {
4790 return false;
4791 }
4792
Brandon Jonesd1049182018-03-28 10:02:20 -07004793 GLsizei numParams = 0;
4794
4795 if (!ValidateGetSamplerParameterBase(context, sampler, pname, &numParams))
Geoff Langc1984ed2016-10-07 12:41:00 -04004796 {
4797 return false;
4798 }
4799
Brandon Jonesd1049182018-03-28 10:02:20 -07004800 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Langc1984ed2016-10-07 12:41:00 -04004801 {
4802 return false;
4803 }
4804
Brandon Jonesd1049182018-03-28 10:02:20 -07004805 SetRobustLengthParam(length, numParams);
Geoff Langc1984ed2016-10-07 12:41:00 -04004806 return true;
4807}
4808
Brandon Jonesfe4bbe62018-04-06 13:50:14 -07004809bool ValidateGetSamplerParameterIivRobustANGLE(Context *context,
4810 GLuint sampler,
4811 GLenum pname,
4812 GLsizei bufSize,
4813 GLsizei *length,
4814 GLint *params)
4815{
4816 UNIMPLEMENTED();
4817 return false;
4818}
4819
4820bool ValidateGetSamplerParameterIuivRobustANGLE(Context *context,
4821 GLuint sampler,
4822 GLenum pname,
4823 GLsizei bufSize,
4824 GLsizei *length,
4825 GLuint *params)
4826{
4827 UNIMPLEMENTED();
4828 return false;
4829}
4830
Geoff Langc1984ed2016-10-07 12:41:00 -04004831bool ValidateSamplerParameterfvRobustANGLE(Context *context,
4832 GLuint sampler,
4833 GLenum pname,
4834 GLsizei bufSize,
4835 const GLfloat *params)
4836{
4837 if (!ValidateRobustEntryPoint(context, bufSize))
4838 {
4839 return false;
4840 }
4841
4842 return ValidateSamplerParameterBase(context, sampler, pname, bufSize, params);
4843}
4844
Geoff Langc1984ed2016-10-07 12:41:00 -04004845bool ValidateSamplerParameterivRobustANGLE(Context *context,
4846 GLuint sampler,
4847 GLenum pname,
4848 GLsizei bufSize,
4849 const GLint *params)
4850{
4851 if (!ValidateRobustEntryPoint(context, bufSize))
4852 {
4853 return false;
4854 }
4855
4856 return ValidateSamplerParameterBase(context, sampler, pname, bufSize, params);
4857}
4858
Brandon Jonesfe4bbe62018-04-06 13:50:14 -07004859bool ValidateSamplerParameterIivRobustANGLE(Context *context,
4860 GLuint sampler,
4861 GLenum pname,
4862 GLsizei bufSize,
4863 const GLint *param)
4864{
4865 UNIMPLEMENTED();
4866 return false;
4867}
4868
4869bool ValidateSamplerParameterIuivRobustANGLE(Context *context,
4870 GLuint sampler,
4871 GLenum pname,
4872 GLsizei bufSize,
4873 const GLuint *param)
4874{
4875 UNIMPLEMENTED();
4876 return false;
4877}
4878
Geoff Lang0b031062016-10-13 14:30:04 -04004879bool ValidateGetVertexAttribfvRobustANGLE(Context *context,
4880 GLuint index,
4881 GLenum pname,
4882 GLsizei bufSize,
4883 GLsizei *length,
4884 GLfloat *params)
4885{
4886 if (!ValidateRobustEntryPoint(context, bufSize))
4887 {
4888 return false;
4889 }
4890
Brandon Jonesd1049182018-03-28 10:02:20 -07004891 GLsizei writeLength = 0;
4892
4893 if (!ValidateGetVertexAttribBase(context, index, pname, &writeLength, false, false))
Geoff Lang0b031062016-10-13 14:30:04 -04004894 {
4895 return false;
4896 }
4897
Brandon Jonesd1049182018-03-28 10:02:20 -07004898 if (!ValidateRobustBufferSize(context, bufSize, writeLength))
Geoff Lang0b031062016-10-13 14:30:04 -04004899 {
4900 return false;
4901 }
4902
Brandon Jonesd1049182018-03-28 10:02:20 -07004903 SetRobustLengthParam(length, writeLength);
Geoff Lang0b031062016-10-13 14:30:04 -04004904 return true;
4905}
4906
Geoff Lang0b031062016-10-13 14:30:04 -04004907bool ValidateGetVertexAttribivRobustANGLE(Context *context,
4908 GLuint index,
4909 GLenum pname,
4910 GLsizei bufSize,
4911 GLsizei *length,
4912 GLint *params)
4913{
4914 if (!ValidateRobustEntryPoint(context, bufSize))
4915 {
4916 return false;
4917 }
4918
Brandon Jonesd1049182018-03-28 10:02:20 -07004919 GLsizei writeLength = 0;
4920
4921 if (!ValidateGetVertexAttribBase(context, index, pname, &writeLength, false, false))
Geoff Lang0b031062016-10-13 14:30:04 -04004922 {
4923 return false;
4924 }
4925
Brandon Jonesd1049182018-03-28 10:02:20 -07004926 if (!ValidateRobustBufferSize(context, bufSize, writeLength))
Geoff Lang0b031062016-10-13 14:30:04 -04004927 {
4928 return false;
4929 }
4930
Brandon Jonesd1049182018-03-28 10:02:20 -07004931 SetRobustLengthParam(length, writeLength);
4932
Geoff Lang0b031062016-10-13 14:30:04 -04004933 return true;
4934}
4935
Geoff Lang0b031062016-10-13 14:30:04 -04004936bool ValidateGetVertexAttribPointervRobustANGLE(Context *context,
4937 GLuint index,
4938 GLenum pname,
4939 GLsizei bufSize,
4940 GLsizei *length,
4941 void **pointer)
4942{
4943 if (!ValidateRobustEntryPoint(context, bufSize))
4944 {
4945 return false;
4946 }
4947
Brandon Jonesd1049182018-03-28 10:02:20 -07004948 GLsizei writeLength = 0;
4949
4950 if (!ValidateGetVertexAttribBase(context, index, pname, &writeLength, true, false))
Geoff Lang0b031062016-10-13 14:30:04 -04004951 {
4952 return false;
4953 }
4954
Brandon Jonesd1049182018-03-28 10:02:20 -07004955 if (!ValidateRobustBufferSize(context, bufSize, writeLength))
Geoff Lang0b031062016-10-13 14:30:04 -04004956 {
4957 return false;
4958 }
4959
Brandon Jonesd1049182018-03-28 10:02:20 -07004960 SetRobustLengthParam(length, writeLength);
4961
Geoff Lang0b031062016-10-13 14:30:04 -04004962 return true;
4963}
4964
Geoff Lang0b031062016-10-13 14:30:04 -04004965bool ValidateGetVertexAttribIivRobustANGLE(Context *context,
4966 GLuint index,
4967 GLenum pname,
4968 GLsizei bufSize,
4969 GLsizei *length,
4970 GLint *params)
4971{
4972 if (!ValidateRobustEntryPoint(context, bufSize))
4973 {
4974 return false;
4975 }
4976
Brandon Jonesd1049182018-03-28 10:02:20 -07004977 GLsizei writeLength = 0;
4978
4979 if (!ValidateGetVertexAttribBase(context, index, pname, &writeLength, false, true))
Geoff Lang0b031062016-10-13 14:30:04 -04004980 {
4981 return false;
4982 }
4983
Brandon Jonesd1049182018-03-28 10:02:20 -07004984 if (!ValidateRobustBufferSize(context, bufSize, writeLength))
Geoff Lang0b031062016-10-13 14:30:04 -04004985 {
4986 return false;
4987 }
4988
Brandon Jonesd1049182018-03-28 10:02:20 -07004989 SetRobustLengthParam(length, writeLength);
4990
Geoff Lang0b031062016-10-13 14:30:04 -04004991 return true;
4992}
4993
Geoff Lang0b031062016-10-13 14:30:04 -04004994bool ValidateGetVertexAttribIuivRobustANGLE(Context *context,
4995 GLuint index,
4996 GLenum pname,
4997 GLsizei bufSize,
4998 GLsizei *length,
4999 GLuint *params)
5000{
5001 if (!ValidateRobustEntryPoint(context, bufSize))
5002 {
5003 return false;
5004 }
5005
Brandon Jonesd1049182018-03-28 10:02:20 -07005006 GLsizei writeLength = 0;
5007
5008 if (!ValidateGetVertexAttribBase(context, index, pname, &writeLength, false, true))
Geoff Lang0b031062016-10-13 14:30:04 -04005009 {
5010 return false;
5011 }
5012
Brandon Jonesd1049182018-03-28 10:02:20 -07005013 if (!ValidateRobustBufferSize(context, bufSize, writeLength))
Geoff Lang0b031062016-10-13 14:30:04 -04005014 {
5015 return false;
5016 }
5017
Brandon Jonesd1049182018-03-28 10:02:20 -07005018 SetRobustLengthParam(length, writeLength);
5019
Geoff Lang0b031062016-10-13 14:30:04 -04005020 return true;
5021}
5022
Geoff Lang6899b872016-10-14 11:30:13 -04005023bool ValidateGetActiveUniformBlockivRobustANGLE(Context *context,
5024 GLuint program,
5025 GLuint uniformBlockIndex,
5026 GLenum pname,
5027 GLsizei bufSize,
5028 GLsizei *length,
5029 GLint *params)
5030{
5031 if (!ValidateRobustEntryPoint(context, bufSize))
5032 {
5033 return false;
5034 }
5035
Brandon Jonesd1049182018-03-28 10:02:20 -07005036 GLsizei writeLength = 0;
5037
5038 if (!ValidateGetActiveUniformBlockivBase(context, program, uniformBlockIndex, pname,
5039 &writeLength))
Geoff Lang6899b872016-10-14 11:30:13 -04005040 {
5041 return false;
5042 }
5043
Brandon Jonesd1049182018-03-28 10:02:20 -07005044 if (!ValidateRobustBufferSize(context, bufSize, writeLength))
Geoff Lang6899b872016-10-14 11:30:13 -04005045 {
5046 return false;
5047 }
5048
Brandon Jonesd1049182018-03-28 10:02:20 -07005049 SetRobustLengthParam(length, writeLength);
5050
Geoff Lang6899b872016-10-14 11:30:13 -04005051 return true;
5052}
5053
Brandon Jones416aaf92018-04-10 08:10:16 -07005054bool ValidateGetInternalformativRobustANGLE(Context *context,
Geoff Lang0a9661f2016-10-20 10:59:20 -07005055 GLenum target,
5056 GLenum internalformat,
5057 GLenum pname,
5058 GLsizei bufSize,
5059 GLsizei *length,
5060 GLint *params)
5061{
5062 if (!ValidateRobustEntryPoint(context, bufSize))
5063 {
5064 return false;
5065 }
5066
Brandon Jonesd1049182018-03-28 10:02:20 -07005067 GLsizei numParams = 0;
5068
5069 if (!ValidateGetInternalFormativBase(context, target, internalformat, pname, bufSize,
5070 &numParams))
Geoff Lang0a9661f2016-10-20 10:59:20 -07005071 {
5072 return false;
5073 }
5074
Brandon Jonesd1049182018-03-28 10:02:20 -07005075 if (!ValidateRobustBufferSize(context, bufSize, numParams))
Geoff Lang0a9661f2016-10-20 10:59:20 -07005076 {
5077 return false;
5078 }
5079
Brandon Jonesd1049182018-03-28 10:02:20 -07005080 SetRobustLengthParam(length, numParams);
5081
Geoff Lang0a9661f2016-10-20 10:59:20 -07005082 return true;
5083}
5084
Jamie Madill5b772312018-03-08 20:28:32 -05005085bool ValidateVertexFormatBase(Context *context,
Shao80957d92017-02-20 21:25:59 +08005086 GLuint attribIndex,
5087 GLint size,
5088 GLenum type,
5089 GLboolean pureInteger)
5090{
5091 const Caps &caps = context->getCaps();
5092 if (attribIndex >= caps.maxVertexAttributes)
5093 {
Brandon Jonesafa75152017-07-21 13:11:29 -07005094 ANGLE_VALIDATION_ERR(context, InvalidValue(), IndexExceedsMaxVertexAttribute);
Shao80957d92017-02-20 21:25:59 +08005095 return false;
5096 }
5097
5098 if (size < 1 || size > 4)
5099 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005100 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidVertexAttrSize);
Geoff Lang8700a982017-06-13 10:15:13 -04005101 return false;
Shao80957d92017-02-20 21:25:59 +08005102 }
5103
5104 switch (type)
5105 {
5106 case GL_BYTE:
5107 case GL_UNSIGNED_BYTE:
5108 case GL_SHORT:
5109 case GL_UNSIGNED_SHORT:
5110 break;
5111
5112 case GL_INT:
5113 case GL_UNSIGNED_INT:
5114 if (context->getClientMajorVersion() < 3)
5115 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005116 context->handleError(InvalidEnum()
5117 << "Vertex type not supported before OpenGL ES 3.0.");
Shao80957d92017-02-20 21:25:59 +08005118 return false;
5119 }
5120 break;
5121
5122 case GL_FIXED:
5123 case GL_FLOAT:
5124 if (pureInteger)
5125 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005126 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTypePureInt);
Shao80957d92017-02-20 21:25:59 +08005127 return false;
5128 }
5129 break;
5130
5131 case GL_HALF_FLOAT:
5132 if (context->getClientMajorVersion() < 3)
5133 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005134 context->handleError(InvalidEnum()
5135 << "Vertex type not supported before OpenGL ES 3.0.");
Shao80957d92017-02-20 21:25:59 +08005136 return false;
5137 }
5138 if (pureInteger)
5139 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005140 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTypePureInt);
Shao80957d92017-02-20 21:25:59 +08005141 return false;
5142 }
5143 break;
5144
5145 case GL_INT_2_10_10_10_REV:
5146 case GL_UNSIGNED_INT_2_10_10_10_REV:
5147 if (context->getClientMajorVersion() < 3)
5148 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005149 context->handleError(InvalidEnum()
5150 << "Vertex type not supported before OpenGL ES 3.0.");
Shao80957d92017-02-20 21:25:59 +08005151 return false;
5152 }
5153 if (pureInteger)
5154 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005155 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTypePureInt);
Shao80957d92017-02-20 21:25:59 +08005156 return false;
5157 }
5158 if (size != 4)
5159 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005160 context->handleError(InvalidOperation() << "Type is INT_2_10_10_10_REV or "
5161 "UNSIGNED_INT_2_10_10_10_REV and "
5162 "size is not 4.");
Shao80957d92017-02-20 21:25:59 +08005163 return false;
5164 }
5165 break;
5166
5167 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005168 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidType);
Shao80957d92017-02-20 21:25:59 +08005169 return false;
5170 }
5171
5172 return true;
5173}
5174
Geoff Lang76e65652017-03-27 14:58:02 -04005175// Perform validation from WebGL 2 section 5.10 "Invalid Clears":
5176// In the WebGL 2 API, trying to perform a clear when there is a mismatch between the type of the
5177// specified clear value and the type of a buffer that is being cleared generates an
5178// INVALID_OPERATION error instead of producing undefined results
Jamie Madill5b772312018-03-08 20:28:32 -05005179bool ValidateWebGLFramebufferAttachmentClearType(Context *context,
Geoff Lang76e65652017-03-27 14:58:02 -04005180 GLint drawbuffer,
5181 const GLenum *validComponentTypes,
5182 size_t validComponentTypeCount)
5183{
5184 const FramebufferAttachment *attachment =
5185 context->getGLState().getDrawFramebuffer()->getDrawBuffer(drawbuffer);
5186 if (attachment)
5187 {
5188 GLenum componentType = attachment->getFormat().info->componentType;
5189 const GLenum *end = validComponentTypes + validComponentTypeCount;
5190 if (std::find(validComponentTypes, end, componentType) == end)
5191 {
5192 context->handleError(
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005193 InvalidOperation()
5194 << "No defined conversion between clear value and attachment format.");
Geoff Lang76e65652017-03-27 14:58:02 -04005195 return false;
5196 }
5197 }
5198
5199 return true;
5200}
5201
Jamie Madill5b772312018-03-08 20:28:32 -05005202bool ValidateRobustCompressedTexImageBase(Context *context, GLsizei imageSize, GLsizei dataSize)
Corentin Wallezb2931602017-04-11 15:58:57 -04005203{
5204 if (!ValidateRobustEntryPoint(context, dataSize))
5205 {
5206 return false;
5207 }
5208
Corentin Wallez336129f2017-10-17 15:55:40 -04005209 gl::Buffer *pixelUnpackBuffer =
5210 context->getGLState().getTargetBuffer(BufferBinding::PixelUnpack);
Corentin Wallezb2931602017-04-11 15:58:57 -04005211 if (pixelUnpackBuffer == nullptr)
5212 {
5213 if (dataSize < imageSize)
5214 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005215 context->handleError(InvalidOperation() << "dataSize must be at least " << imageSize);
Corentin Wallezb2931602017-04-11 15:58:57 -04005216 }
5217 }
5218 return true;
5219}
5220
Jamie Madill5b772312018-03-08 20:28:32 -05005221bool ValidateGetBufferParameterBase(Context *context,
Corentin Wallez336129f2017-10-17 15:55:40 -04005222 BufferBinding target,
Jamie Madillbe849e42017-05-02 15:49:00 -04005223 GLenum pname,
5224 bool pointerVersion,
5225 GLsizei *numParams)
5226{
5227 if (numParams)
5228 {
5229 *numParams = 0;
5230 }
5231
Corentin Walleze4477002017-12-01 14:39:58 -05005232 if (!context->isValidBufferBinding(target))
Jamie Madillbe849e42017-05-02 15:49:00 -04005233 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005234 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidBufferTypes);
Jamie Madillbe849e42017-05-02 15:49:00 -04005235 return false;
5236 }
5237
5238 const Buffer *buffer = context->getGLState().getTargetBuffer(target);
5239 if (!buffer)
5240 {
5241 // A null buffer means that "0" is bound to the requested buffer target
Brandon Jones6cad5662017-06-14 13:25:13 -07005242 ANGLE_VALIDATION_ERR(context, InvalidOperation(), BufferNotBound);
Jamie Madillbe849e42017-05-02 15:49:00 -04005243 return false;
5244 }
5245
5246 const Extensions &extensions = context->getExtensions();
5247
5248 switch (pname)
5249 {
5250 case GL_BUFFER_USAGE:
5251 case GL_BUFFER_SIZE:
5252 break;
5253
5254 case GL_BUFFER_ACCESS_OES:
5255 if (!extensions.mapBuffer)
5256 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005257 context->handleError(InvalidEnum()
5258 << "pname requires OpenGL ES 3.0 or GL_OES_mapbuffer.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005259 return false;
5260 }
5261 break;
5262
5263 case GL_BUFFER_MAPPED:
5264 static_assert(GL_BUFFER_MAPPED == GL_BUFFER_MAPPED_OES, "GL enums should be equal.");
5265 if (context->getClientMajorVersion() < 3 && !extensions.mapBuffer &&
5266 !extensions.mapBufferRange)
5267 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005268 context->handleError(InvalidEnum() << "pname requires OpenGL ES 3.0, "
5269 "GL_OES_mapbuffer or "
5270 "GL_EXT_map_buffer_range.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005271 return false;
5272 }
5273 break;
5274
5275 case GL_BUFFER_MAP_POINTER:
5276 if (!pointerVersion)
5277 {
5278 context->handleError(
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005279 InvalidEnum()
5280 << "GL_BUFFER_MAP_POINTER can only be queried with GetBufferPointerv.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005281 return false;
5282 }
5283 break;
5284
5285 case GL_BUFFER_ACCESS_FLAGS:
5286 case GL_BUFFER_MAP_OFFSET:
5287 case GL_BUFFER_MAP_LENGTH:
5288 if (context->getClientMajorVersion() < 3 && !extensions.mapBufferRange)
5289 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005290 context->handleError(InvalidEnum()
5291 << "pname requires OpenGL ES 3.0 or GL_EXT_map_buffer_range.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005292 return false;
5293 }
5294 break;
5295
5296 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005297 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005298 return false;
5299 }
5300
5301 // All buffer parameter queries return one value.
5302 if (numParams)
5303 {
5304 *numParams = 1;
5305 }
5306
5307 return true;
5308}
5309
5310bool ValidateGetRenderbufferParameterivBase(Context *context,
5311 GLenum target,
5312 GLenum pname,
5313 GLsizei *length)
5314{
5315 if (length)
5316 {
5317 *length = 0;
5318 }
5319
5320 if (target != GL_RENDERBUFFER)
5321 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005322 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidRenderbufferTarget);
Jamie Madillbe849e42017-05-02 15:49:00 -04005323 return false;
5324 }
5325
5326 Renderbuffer *renderbuffer = context->getGLState().getCurrentRenderbuffer();
5327 if (renderbuffer == nullptr)
5328 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005329 ANGLE_VALIDATION_ERR(context, InvalidOperation(), RenderbufferNotBound);
Jamie Madillbe849e42017-05-02 15:49:00 -04005330 return false;
5331 }
5332
5333 switch (pname)
5334 {
5335 case GL_RENDERBUFFER_WIDTH:
5336 case GL_RENDERBUFFER_HEIGHT:
5337 case GL_RENDERBUFFER_INTERNAL_FORMAT:
5338 case GL_RENDERBUFFER_RED_SIZE:
5339 case GL_RENDERBUFFER_GREEN_SIZE:
5340 case GL_RENDERBUFFER_BLUE_SIZE:
5341 case GL_RENDERBUFFER_ALPHA_SIZE:
5342 case GL_RENDERBUFFER_DEPTH_SIZE:
5343 case GL_RENDERBUFFER_STENCIL_SIZE:
5344 break;
5345
5346 case GL_RENDERBUFFER_SAMPLES_ANGLE:
5347 if (!context->getExtensions().framebufferMultisample)
5348 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005349 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ExtensionNotEnabled);
Jamie Madillbe849e42017-05-02 15:49:00 -04005350 return false;
5351 }
5352 break;
5353
5354 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005355 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005356 return false;
5357 }
5358
5359 if (length)
5360 {
5361 *length = 1;
5362 }
5363 return true;
5364}
5365
5366bool ValidateGetShaderivBase(Context *context, GLuint shader, GLenum pname, GLsizei *length)
5367{
5368 if (length)
5369 {
5370 *length = 0;
5371 }
5372
5373 if (GetValidShader(context, shader) == nullptr)
5374 {
5375 return false;
5376 }
5377
5378 switch (pname)
5379 {
5380 case GL_SHADER_TYPE:
5381 case GL_DELETE_STATUS:
5382 case GL_COMPILE_STATUS:
5383 case GL_INFO_LOG_LENGTH:
5384 case GL_SHADER_SOURCE_LENGTH:
5385 break;
5386
5387 case GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE:
5388 if (!context->getExtensions().translatedShaderSource)
5389 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005390 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ExtensionNotEnabled);
Jamie Madillbe849e42017-05-02 15:49:00 -04005391 return false;
5392 }
5393 break;
5394
5395 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005396 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005397 return false;
5398 }
5399
5400 if (length)
5401 {
5402 *length = 1;
5403 }
5404 return true;
5405}
5406
Corentin Wallezf0e89be2017-11-08 14:00:32 -08005407bool ValidateGetTexParameterBase(Context *context,
5408 TextureType target,
5409 GLenum pname,
5410 GLsizei *length)
Jamie Madillbe849e42017-05-02 15:49:00 -04005411{
5412 if (length)
5413 {
5414 *length = 0;
5415 }
5416
5417 if (!ValidTextureTarget(context, target) && !ValidTextureExternalTarget(context, target))
5418 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005419 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTextureTarget);
Jamie Madillbe849e42017-05-02 15:49:00 -04005420 return false;
5421 }
5422
5423 if (context->getTargetTexture(target) == nullptr)
5424 {
5425 // Should only be possible for external textures
Brandon Jones6cad5662017-06-14 13:25:13 -07005426 ANGLE_VALIDATION_ERR(context, InvalidEnum(), TextureNotBound);
Jamie Madillbe849e42017-05-02 15:49:00 -04005427 return false;
5428 }
5429
5430 switch (pname)
5431 {
5432 case GL_TEXTURE_MAG_FILTER:
5433 case GL_TEXTURE_MIN_FILTER:
5434 case GL_TEXTURE_WRAP_S:
5435 case GL_TEXTURE_WRAP_T:
5436 break;
5437
5438 case GL_TEXTURE_USAGE_ANGLE:
5439 if (!context->getExtensions().textureUsage)
5440 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005441 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ExtensionNotEnabled);
Jamie Madillbe849e42017-05-02 15:49:00 -04005442 return false;
5443 }
5444 break;
5445
5446 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
Luc Ferron1b1a8642018-01-23 15:12:01 -05005447 if (!ValidateTextureMaxAnisotropyExtensionEnabled(context))
Jamie Madillbe849e42017-05-02 15:49:00 -04005448 {
Jamie Madillbe849e42017-05-02 15:49:00 -04005449 return false;
5450 }
5451 break;
5452
5453 case GL_TEXTURE_IMMUTABLE_FORMAT:
5454 if (context->getClientMajorVersion() < 3 && !context->getExtensions().textureStorage)
5455 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005456 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ExtensionNotEnabled);
Jamie Madillbe849e42017-05-02 15:49:00 -04005457 return false;
5458 }
5459 break;
5460
5461 case GL_TEXTURE_WRAP_R:
5462 case GL_TEXTURE_IMMUTABLE_LEVELS:
5463 case GL_TEXTURE_SWIZZLE_R:
5464 case GL_TEXTURE_SWIZZLE_G:
5465 case GL_TEXTURE_SWIZZLE_B:
5466 case GL_TEXTURE_SWIZZLE_A:
5467 case GL_TEXTURE_BASE_LEVEL:
5468 case GL_TEXTURE_MAX_LEVEL:
5469 case GL_TEXTURE_MIN_LOD:
5470 case GL_TEXTURE_MAX_LOD:
5471 case GL_TEXTURE_COMPARE_MODE:
5472 case GL_TEXTURE_COMPARE_FUNC:
5473 if (context->getClientMajorVersion() < 3)
5474 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005475 context->handleError(InvalidEnum() << "pname requires OpenGL ES 3.0.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005476 return false;
5477 }
5478 break;
5479
5480 case GL_TEXTURE_SRGB_DECODE_EXT:
5481 if (!context->getExtensions().textureSRGBDecode)
5482 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005483 context->handleError(InvalidEnum() << "GL_EXT_texture_sRGB_decode is not enabled.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005484 return false;
5485 }
5486 break;
5487
Yunchao Hebacaa712018-01-30 14:01:39 +08005488 case GL_DEPTH_STENCIL_TEXTURE_MODE:
5489 if (context->getClientVersion() < Version(3, 1))
5490 {
5491 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumRequiresGLES31);
5492 return false;
5493 }
5494 break;
5495
Jamie Madillbe849e42017-05-02 15:49:00 -04005496 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005497 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005498 return false;
5499 }
5500
5501 if (length)
5502 {
5503 *length = 1;
5504 }
5505 return true;
5506}
5507
5508bool ValidateGetVertexAttribBase(Context *context,
5509 GLuint index,
5510 GLenum pname,
5511 GLsizei *length,
5512 bool pointer,
5513 bool pureIntegerEntryPoint)
5514{
5515 if (length)
5516 {
5517 *length = 0;
5518 }
5519
5520 if (pureIntegerEntryPoint && context->getClientMajorVersion() < 3)
5521 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005522 context->handleError(InvalidOperation() << "Context does not support OpenGL ES 3.0.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005523 return false;
5524 }
5525
5526 if (index >= context->getCaps().maxVertexAttributes)
5527 {
Brandon Jonesafa75152017-07-21 13:11:29 -07005528 ANGLE_VALIDATION_ERR(context, InvalidValue(), IndexExceedsMaxVertexAttribute);
Jamie Madillbe849e42017-05-02 15:49:00 -04005529 return false;
5530 }
5531
5532 if (pointer)
5533 {
5534 if (pname != GL_VERTEX_ATTRIB_ARRAY_POINTER)
5535 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005536 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005537 return false;
5538 }
5539 }
5540 else
5541 {
5542 switch (pname)
5543 {
5544 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
5545 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
5546 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
5547 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
5548 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
5549 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
5550 case GL_CURRENT_VERTEX_ATTRIB:
5551 break;
5552
5553 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR:
5554 static_assert(
5555 GL_VERTEX_ATTRIB_ARRAY_DIVISOR == GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE,
5556 "ANGLE extension enums not equal to GL enums.");
5557 if (context->getClientMajorVersion() < 3 &&
5558 !context->getExtensions().instancedArrays)
5559 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005560 context->handleError(InvalidEnum() << "GL_VERTEX_ATTRIB_ARRAY_DIVISOR "
5561 "requires OpenGL ES 3.0 or "
5562 "GL_ANGLE_instanced_arrays.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005563 return false;
5564 }
5565 break;
5566
5567 case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
5568 if (context->getClientMajorVersion() < 3)
5569 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005570 context->handleError(
5571 InvalidEnum() << "GL_VERTEX_ATTRIB_ARRAY_INTEGER requires OpenGL ES 3.0.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005572 return false;
5573 }
5574 break;
5575
5576 case GL_VERTEX_ATTRIB_BINDING:
5577 case GL_VERTEX_ATTRIB_RELATIVE_OFFSET:
5578 if (context->getClientVersion() < ES_3_1)
5579 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005580 context->handleError(InvalidEnum()
5581 << "Vertex Attrib Bindings require OpenGL ES 3.1.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005582 return false;
5583 }
5584 break;
5585
5586 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005587 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005588 return false;
5589 }
5590 }
5591
5592 if (length)
5593 {
5594 if (pname == GL_CURRENT_VERTEX_ATTRIB)
5595 {
5596 *length = 4;
5597 }
5598 else
5599 {
5600 *length = 1;
5601 }
5602 }
5603
5604 return true;
5605}
5606
Jamie Madill4928b7c2017-06-20 12:57:39 -04005607bool ValidateReadPixelsBase(Context *context,
Jamie Madillbe849e42017-05-02 15:49:00 -04005608 GLint x,
5609 GLint y,
5610 GLsizei width,
5611 GLsizei height,
5612 GLenum format,
5613 GLenum type,
5614 GLsizei bufSize,
5615 GLsizei *length,
5616 GLsizei *columns,
5617 GLsizei *rows,
5618 void *pixels)
5619{
5620 if (length != nullptr)
5621 {
5622 *length = 0;
5623 }
5624 if (rows != nullptr)
5625 {
5626 *rows = 0;
5627 }
5628 if (columns != nullptr)
5629 {
5630 *columns = 0;
5631 }
5632
5633 if (width < 0 || height < 0)
5634 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005635 ANGLE_VALIDATION_ERR(context, InvalidValue(), NegativeSize);
Jamie Madillbe849e42017-05-02 15:49:00 -04005636 return false;
5637 }
5638
Jamie Madillacf2f3a2017-11-21 19:22:44 -05005639 Framebuffer *readFramebuffer = context->getGLState().getReadFramebuffer();
Jamie Madillbe849e42017-05-02 15:49:00 -04005640
Jamie Madill427064d2018-04-13 16:20:34 -04005641 if (!ValidateFramebufferComplete(context, readFramebuffer))
Jamie Madillbe849e42017-05-02 15:49:00 -04005642 {
Jamie Madillbe849e42017-05-02 15:49:00 -04005643 return false;
5644 }
5645
Jamie Madille98b1b52018-03-08 09:47:23 -05005646 if (readFramebuffer->id() != 0 && !ValidateFramebufferNotMultisampled(context, readFramebuffer))
Jamie Madillbe849e42017-05-02 15:49:00 -04005647 {
Jamie Madillbe849e42017-05-02 15:49:00 -04005648 return false;
5649 }
5650
Jamie Madill690c8eb2018-03-12 15:20:03 -04005651 Framebuffer *framebuffer = context->getGLState().getReadFramebuffer();
Jamie Madillbe849e42017-05-02 15:49:00 -04005652 ASSERT(framebuffer);
5653
5654 if (framebuffer->getReadBufferState() == GL_NONE)
5655 {
Brandon Jonesafa75152017-07-21 13:11:29 -07005656 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ReadBufferNone);
Jamie Madillbe849e42017-05-02 15:49:00 -04005657 return false;
5658 }
5659
5660 const FramebufferAttachment *readBuffer = framebuffer->getReadColorbuffer();
5661 // WebGL 1.0 [Section 6.26] Reading From a Missing Attachment
5662 // In OpenGL ES it is undefined what happens when an operation tries to read from a missing
5663 // attachment and WebGL defines it to be an error. We do the check unconditionnaly as the
5664 // situation is an application error that would lead to a crash in ANGLE.
5665 if (readBuffer == nullptr)
5666 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005667 ANGLE_VALIDATION_ERR(context, InvalidOperation(), MissingReadAttachment);
Jamie Madillbe849e42017-05-02 15:49:00 -04005668 return false;
5669 }
5670
Martin Radev28031682017-07-28 14:47:56 +03005671 // ANGLE_multiview, Revision 1:
5672 // ReadPixels generates an INVALID_FRAMEBUFFER_OPERATION error if the multi-view layout of the
5673 // current read framebuffer is not NONE.
5674 if (readBuffer->getMultiviewLayout() != GL_NONE)
5675 {
5676 context->handleError(InvalidFramebufferOperation()
5677 << "Attempting to read from a multi-view framebuffer.");
5678 return false;
5679 }
5680
Geoff Lang280ba992017-04-18 16:30:58 -04005681 if (context->getExtensions().webglCompatibility)
5682 {
5683 // The ES 2.0 spec states that the format must be "among those defined in table 3.4,
5684 // excluding formats LUMINANCE and LUMINANCE_ALPHA.". This requires validating the format
5685 // and type before validating the combination of format and type. However, the
5686 // dEQP-GLES3.functional.negative_api.buffer.read_pixels passes GL_LUMINANCE as a format and
5687 // verifies that GL_INVALID_OPERATION is generated.
5688 // TODO(geofflang): Update this check to be done in all/no cases once this is resolved in
5689 // dEQP/WebGL.
5690 if (!ValidReadPixelsFormatEnum(context, format))
5691 {
Brandon Jonesafa75152017-07-21 13:11:29 -07005692 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidFormat);
Geoff Lang280ba992017-04-18 16:30:58 -04005693 return false;
5694 }
5695
5696 if (!ValidReadPixelsTypeEnum(context, type))
5697 {
Brandon Jonesafa75152017-07-21 13:11:29 -07005698 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidType);
Geoff Lang280ba992017-04-18 16:30:58 -04005699 return false;
5700 }
5701 }
5702
Jamie Madill690c8eb2018-03-12 15:20:03 -04005703 GLenum currentFormat = GL_NONE;
5704 ANGLE_VALIDATION_TRY(framebuffer->getImplementationColorReadFormat(context, &currentFormat));
5705
5706 GLenum currentType = GL_NONE;
5707 ANGLE_VALIDATION_TRY(framebuffer->getImplementationColorReadType(context, &currentType));
5708
Jamie Madillbe849e42017-05-02 15:49:00 -04005709 GLenum currentComponentType = readBuffer->getFormat().info->componentType;
5710
5711 bool validFormatTypeCombination =
5712 ValidReadPixelsFormatType(context, currentComponentType, format, type);
5713
5714 if (!(currentFormat == format && currentType == type) && !validFormatTypeCombination)
5715 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005716 ANGLE_VALIDATION_ERR(context, InvalidOperation(), MismatchedTypeAndFormat);
Jamie Madillbe849e42017-05-02 15:49:00 -04005717 return false;
5718 }
5719
5720 // Check for pixel pack buffer related API errors
Corentin Wallez336129f2017-10-17 15:55:40 -04005721 gl::Buffer *pixelPackBuffer = context->getGLState().getTargetBuffer(BufferBinding::PixelPack);
Jamie Madillbe849e42017-05-02 15:49:00 -04005722 if (pixelPackBuffer != nullptr && pixelPackBuffer->isMapped())
5723 {
5724 // ...the buffer object's data store is currently mapped.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005725 context->handleError(InvalidOperation() << "Pixel pack buffer is mapped.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005726 return false;
5727 }
James Darpiniane8a93c62018-01-04 18:02:24 -08005728 if (context->getExtensions().webglCompatibility && pixelPackBuffer != nullptr &&
5729 pixelPackBuffer->isBoundForTransformFeedbackAndOtherUse())
5730 {
5731 ANGLE_VALIDATION_ERR(context, InvalidOperation(), PixelPackBufferBoundForTransformFeedback);
5732 return false;
5733 }
Jamie Madillbe849e42017-05-02 15:49:00 -04005734
5735 // .. the data would be packed to the buffer object such that the memory writes required
5736 // would exceed the data store size.
5737 const InternalFormat &formatInfo = GetInternalFormatInfo(format, type);
5738 const gl::Extents size(width, height, 1);
5739 const auto &pack = context->getGLState().getPackState();
5740
5741 auto endByteOrErr = formatInfo.computePackUnpackEndByte(type, size, pack, false);
5742 if (endByteOrErr.isError())
5743 {
5744 context->handleError(endByteOrErr.getError());
5745 return false;
5746 }
5747
5748 size_t endByte = endByteOrErr.getResult();
5749 if (bufSize >= 0)
5750 {
5751 if (pixelPackBuffer == nullptr && static_cast<size_t>(bufSize) < endByte)
5752 {
Brandon Jonesafa75152017-07-21 13:11:29 -07005753 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InsufficientBufferSize);
Jamie Madillbe849e42017-05-02 15:49:00 -04005754 return false;
5755 }
5756 }
5757
5758 if (pixelPackBuffer != nullptr)
5759 {
5760 CheckedNumeric<size_t> checkedEndByte(endByte);
5761 CheckedNumeric<size_t> checkedOffset(reinterpret_cast<size_t>(pixels));
5762 checkedEndByte += checkedOffset;
5763
5764 if (checkedEndByte.ValueOrDie() > static_cast<size_t>(pixelPackBuffer->getSize()))
5765 {
5766 // Overflow past the end of the buffer
Brandon Jones6cad5662017-06-14 13:25:13 -07005767 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ParamOverflow);
Jamie Madillbe849e42017-05-02 15:49:00 -04005768 return false;
5769 }
5770 }
5771
5772 if (pixelPackBuffer == nullptr && length != nullptr)
5773 {
5774 if (endByte > static_cast<size_t>(std::numeric_limits<GLsizei>::max()))
5775 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005776 ANGLE_VALIDATION_ERR(context, InvalidOperation(), IntegerOverflow);
Jamie Madillbe849e42017-05-02 15:49:00 -04005777 return false;
5778 }
5779
5780 *length = static_cast<GLsizei>(endByte);
5781 }
5782
Geoff Langa953b522018-02-21 16:56:23 -05005783 auto getClippedExtent = [](GLint start, GLsizei length, int bufferSize, GLsizei *outExtent) {
Jamie Madillbe849e42017-05-02 15:49:00 -04005784 angle::CheckedNumeric<int> clippedExtent(length);
5785 if (start < 0)
5786 {
5787 // "subtract" the area that is less than 0
5788 clippedExtent += start;
5789 }
5790
Geoff Langa953b522018-02-21 16:56:23 -05005791 angle::CheckedNumeric<int> readExtent = start;
5792 readExtent += length;
5793 if (!readExtent.IsValid())
5794 {
5795 return false;
5796 }
5797
5798 if (readExtent.ValueOrDie() > bufferSize)
Jamie Madillbe849e42017-05-02 15:49:00 -04005799 {
5800 // Subtract the region to the right of the read buffer
5801 clippedExtent -= (readExtent - bufferSize);
5802 }
5803
5804 if (!clippedExtent.IsValid())
5805 {
Geoff Langa953b522018-02-21 16:56:23 -05005806 return false;
Jamie Madillbe849e42017-05-02 15:49:00 -04005807 }
5808
Geoff Langa953b522018-02-21 16:56:23 -05005809 *outExtent = std::max(clippedExtent.ValueOrDie(), 0);
5810 return true;
Jamie Madillbe849e42017-05-02 15:49:00 -04005811 };
5812
Geoff Langa953b522018-02-21 16:56:23 -05005813 GLsizei writtenColumns = 0;
5814 if (!getClippedExtent(x, width, readBuffer->getSize().width, &writtenColumns))
5815 {
5816 ANGLE_VALIDATION_ERR(context, InvalidOperation(), IntegerOverflow);
5817 return false;
5818 }
5819
5820 GLsizei writtenRows = 0;
5821 if (!getClippedExtent(y, height, readBuffer->getSize().height, &writtenRows))
5822 {
5823 ANGLE_VALIDATION_ERR(context, InvalidOperation(), IntegerOverflow);
5824 return false;
5825 }
5826
Jamie Madillbe849e42017-05-02 15:49:00 -04005827 if (columns != nullptr)
5828 {
Geoff Langa953b522018-02-21 16:56:23 -05005829 *columns = writtenColumns;
Jamie Madillbe849e42017-05-02 15:49:00 -04005830 }
5831
5832 if (rows != nullptr)
5833 {
Geoff Langa953b522018-02-21 16:56:23 -05005834 *rows = writtenRows;
Jamie Madillbe849e42017-05-02 15:49:00 -04005835 }
5836
5837 return true;
5838}
5839
5840template <typename ParamType>
5841bool ValidateTexParameterBase(Context *context,
Corentin Wallezf0e89be2017-11-08 14:00:32 -08005842 TextureType target,
Jamie Madillbe849e42017-05-02 15:49:00 -04005843 GLenum pname,
5844 GLsizei bufSize,
5845 const ParamType *params)
5846{
5847 if (!ValidTextureTarget(context, target) && !ValidTextureExternalTarget(context, target))
5848 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005849 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTextureTarget);
Jamie Madillbe849e42017-05-02 15:49:00 -04005850 return false;
5851 }
5852
5853 if (context->getTargetTexture(target) == nullptr)
5854 {
5855 // Should only be possible for external textures
Brandon Jones6cad5662017-06-14 13:25:13 -07005856 ANGLE_VALIDATION_ERR(context, InvalidEnum(), TextureNotBound);
Jamie Madillbe849e42017-05-02 15:49:00 -04005857 return false;
5858 }
5859
5860 const GLsizei minBufSize = 1;
5861 if (bufSize >= 0 && bufSize < minBufSize)
5862 {
Brandon Jones6cad5662017-06-14 13:25:13 -07005863 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InsufficientBufferSize);
Jamie Madillbe849e42017-05-02 15:49:00 -04005864 return false;
5865 }
5866
5867 switch (pname)
5868 {
5869 case GL_TEXTURE_WRAP_R:
5870 case GL_TEXTURE_SWIZZLE_R:
5871 case GL_TEXTURE_SWIZZLE_G:
5872 case GL_TEXTURE_SWIZZLE_B:
5873 case GL_TEXTURE_SWIZZLE_A:
5874 case GL_TEXTURE_BASE_LEVEL:
5875 case GL_TEXTURE_MAX_LEVEL:
5876 case GL_TEXTURE_COMPARE_MODE:
5877 case GL_TEXTURE_COMPARE_FUNC:
5878 case GL_TEXTURE_MIN_LOD:
5879 case GL_TEXTURE_MAX_LOD:
5880 if (context->getClientMajorVersion() < 3)
5881 {
Brandon Jonesafa75152017-07-21 13:11:29 -07005882 ANGLE_VALIDATION_ERR(context, InvalidEnum(), ES3Required);
Jamie Madillbe849e42017-05-02 15:49:00 -04005883 return false;
5884 }
Corentin Wallezf0e89be2017-11-08 14:00:32 -08005885 if (target == TextureType::External && !context->getExtensions().eglImageExternalEssl3)
Jamie Madillbe849e42017-05-02 15:49:00 -04005886 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05005887 context->handleError(InvalidEnum() << "ES3 texture parameters are not "
5888 "available without "
5889 "GL_OES_EGL_image_external_essl3.");
Jamie Madillbe849e42017-05-02 15:49:00 -04005890 return false;
5891 }
5892 break;
5893
5894 default:
5895 break;
5896 }
5897
Corentin Wallezf0e89be2017-11-08 14:00:32 -08005898 if (target == TextureType::_2DMultisample)
JiangYizhou4cff8d62017-07-06 14:54:09 +08005899 {
5900 switch (pname)
5901 {
5902 case GL_TEXTURE_MIN_FILTER:
5903 case GL_TEXTURE_MAG_FILTER:
5904 case GL_TEXTURE_WRAP_S:
5905 case GL_TEXTURE_WRAP_T:
5906 case GL_TEXTURE_WRAP_R:
5907 case GL_TEXTURE_MIN_LOD:
5908 case GL_TEXTURE_MAX_LOD:
5909 case GL_TEXTURE_COMPARE_MODE:
5910 case GL_TEXTURE_COMPARE_FUNC:
5911 context->handleError(InvalidEnum()
5912 << "Invalid parameter for 2D multisampled textures.");
5913 return false;
5914 }
5915 }
5916
Jamie Madillbe849e42017-05-02 15:49:00 -04005917 switch (pname)
5918 {
5919 case GL_TEXTURE_WRAP_S:
5920 case GL_TEXTURE_WRAP_T:
5921 case GL_TEXTURE_WRAP_R:
Lingfeng Yang038dd532018-03-29 17:31:52 -07005922 {
5923 bool restrictedWrapModes =
5924 target == TextureType::External || target == TextureType::Rectangle;
5925 if (!ValidateTextureWrapModeValue(context, params, restrictedWrapModes))
Jamie Madillbe849e42017-05-02 15:49:00 -04005926 {
Lingfeng Yang038dd532018-03-29 17:31:52 -07005927 return false;
Jamie Madillbe849e42017-05-02 15:49:00 -04005928 }
Lingfeng Yang038dd532018-03-29 17:31:52 -07005929 }
5930 break;
Jamie Madillbe849e42017-05-02 15:49:00 -04005931
5932 case GL_TEXTURE_MIN_FILTER:
Lingfeng Yang038dd532018-03-29 17:31:52 -07005933 {
5934 bool restrictedMinFilter =
5935 target == TextureType::External || target == TextureType::Rectangle;
5936 if (!ValidateTextureMinFilterValue(context, params, restrictedMinFilter))
Jamie Madillbe849e42017-05-02 15:49:00 -04005937 {
Lingfeng Yang038dd532018-03-29 17:31:52 -07005938 return false;
Jamie Madillbe849e42017-05-02 15:49:00 -04005939 }
Lingfeng Yang038dd532018-03-29 17:31:52 -07005940 }
5941 break;
Jamie Madillbe849e42017-05-02 15:49:00 -04005942
5943 case GL_TEXTURE_MAG_FILTER:
5944 if (!ValidateTextureMagFilterValue(context, params))
5945 {
5946 return false;
5947 }
5948 break;
5949
5950 case GL_TEXTURE_USAGE_ANGLE:
Geoff Lang91ab54b2017-10-30 15:12:42 -04005951 if (!context->getExtensions().textureUsage)
5952 {
5953 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
5954 return false;
5955 }
5956
Jamie Madillbe849e42017-05-02 15:49:00 -04005957 switch (ConvertToGLenum(params[0]))
5958 {
5959 case GL_NONE:
5960 case GL_FRAMEBUFFER_ATTACHMENT_ANGLE:
5961 break;
5962
5963 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07005964 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04005965 return false;
5966 }
5967 break;
5968
5969 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
Lingfeng Yang038dd532018-03-29 17:31:52 -07005970 {
5971 GLfloat paramValue = static_cast<GLfloat>(params[0]);
5972 if (!ValidateTextureMaxAnisotropyValue(context, paramValue))
Jamie Madillbe849e42017-05-02 15:49:00 -04005973 {
Lingfeng Yang038dd532018-03-29 17:31:52 -07005974 return false;
Jamie Madillbe849e42017-05-02 15:49:00 -04005975 }
Lingfeng Yang038dd532018-03-29 17:31:52 -07005976 ASSERT(static_cast<ParamType>(paramValue) == params[0]);
5977 }
5978 break;
Jamie Madillbe849e42017-05-02 15:49:00 -04005979
5980 case GL_TEXTURE_MIN_LOD:
5981 case GL_TEXTURE_MAX_LOD:
5982 // any value is permissible
5983 break;
5984
5985 case GL_TEXTURE_COMPARE_MODE:
5986 if (!ValidateTextureCompareModeValue(context, params))
5987 {
5988 return false;
5989 }
5990 break;
5991
5992 case GL_TEXTURE_COMPARE_FUNC:
5993 if (!ValidateTextureCompareFuncValue(context, params))
5994 {
5995 return false;
5996 }
5997 break;
5998
5999 case GL_TEXTURE_SWIZZLE_R:
6000 case GL_TEXTURE_SWIZZLE_G:
6001 case GL_TEXTURE_SWIZZLE_B:
6002 case GL_TEXTURE_SWIZZLE_A:
6003 switch (ConvertToGLenum(params[0]))
6004 {
6005 case GL_RED:
6006 case GL_GREEN:
6007 case GL_BLUE:
6008 case GL_ALPHA:
6009 case GL_ZERO:
6010 case GL_ONE:
6011 break;
6012
6013 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07006014 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04006015 return false;
6016 }
6017 break;
6018
6019 case GL_TEXTURE_BASE_LEVEL:
Geoff Langfb7685f2017-11-13 11:44:11 -05006020 if (ConvertToGLint(params[0]) < 0)
Jamie Madillbe849e42017-05-02 15:49:00 -04006021 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05006022 context->handleError(InvalidValue() << "Base level must be at least 0.");
Jamie Madillbe849e42017-05-02 15:49:00 -04006023 return false;
6024 }
Corentin Wallezf0e89be2017-11-08 14:00:32 -08006025 if (target == TextureType::External && static_cast<GLuint>(params[0]) != 0)
Jamie Madillbe849e42017-05-02 15:49:00 -04006026 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05006027 context->handleError(InvalidOperation()
6028 << "Base level must be 0 for external textures.");
Jamie Madillbe849e42017-05-02 15:49:00 -04006029 return false;
6030 }
Corentin Wallezf0e89be2017-11-08 14:00:32 -08006031 if (target == TextureType::_2DMultisample && static_cast<GLuint>(params[0]) != 0)
JiangYizhou4cff8d62017-07-06 14:54:09 +08006032 {
6033 context->handleError(InvalidOperation()
6034 << "Base level must be 0 for multisampled textures.");
6035 return false;
6036 }
Corentin Wallezf0e89be2017-11-08 14:00:32 -08006037 if (target == TextureType::Rectangle && static_cast<GLuint>(params[0]) != 0)
Corentin Wallez13c0dd42017-07-04 18:27:01 -04006038 {
6039 context->handleError(InvalidOperation()
6040 << "Base level must be 0 for rectangle textures.");
6041 return false;
6042 }
Jamie Madillbe849e42017-05-02 15:49:00 -04006043 break;
6044
6045 case GL_TEXTURE_MAX_LEVEL:
Geoff Langfb7685f2017-11-13 11:44:11 -05006046 if (ConvertToGLint(params[0]) < 0)
Jamie Madillbe849e42017-05-02 15:49:00 -04006047 {
Brandon Jones6cad5662017-06-14 13:25:13 -07006048 ANGLE_VALIDATION_ERR(context, InvalidValue(), InvalidMipLevel);
Jamie Madillbe849e42017-05-02 15:49:00 -04006049 return false;
6050 }
6051 break;
6052
6053 case GL_DEPTH_STENCIL_TEXTURE_MODE:
6054 if (context->getClientVersion() < Version(3, 1))
6055 {
Brandon Jones6cad5662017-06-14 13:25:13 -07006056 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumRequiresGLES31);
Jamie Madillbe849e42017-05-02 15:49:00 -04006057 return false;
6058 }
6059 switch (ConvertToGLenum(params[0]))
6060 {
6061 case GL_DEPTH_COMPONENT:
6062 case GL_STENCIL_INDEX:
6063 break;
6064
6065 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07006066 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04006067 return false;
6068 }
6069 break;
6070
6071 case GL_TEXTURE_SRGB_DECODE_EXT:
6072 if (!ValidateTextureSRGBDecodeValue(context, params))
6073 {
6074 return false;
6075 }
6076 break;
6077
6078 default:
Brandon Jones6cad5662017-06-14 13:25:13 -07006079 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
Jamie Madillbe849e42017-05-02 15:49:00 -04006080 return false;
6081 }
6082
6083 return true;
6084}
6085
Corentin Wallezf0e89be2017-11-08 14:00:32 -08006086template bool ValidateTexParameterBase(Context *, TextureType, GLenum, GLsizei, const GLfloat *);
6087template bool ValidateTexParameterBase(Context *, TextureType, GLenum, GLsizei, const GLint *);
Jamie Madillbe849e42017-05-02 15:49:00 -04006088
Jamie Madill5b772312018-03-08 20:28:32 -05006089bool ValidateVertexAttribIndex(Context *context, GLuint index)
Jamie Madill12e957f2017-08-26 21:42:26 -04006090{
6091 if (index >= MAX_VERTEX_ATTRIBS)
6092 {
6093 ANGLE_VALIDATION_ERR(context, InvalidValue(), IndexExceedsMaxVertexAttribute);
6094 return false;
6095 }
6096
6097 return true;
6098}
6099
6100bool ValidateGetActiveUniformBlockivBase(Context *context,
6101 GLuint program,
6102 GLuint uniformBlockIndex,
6103 GLenum pname,
6104 GLsizei *length)
6105{
6106 if (length)
6107 {
6108 *length = 0;
6109 }
6110
6111 if (context->getClientMajorVersion() < 3)
6112 {
6113 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ES3Required);
6114 return false;
6115 }
6116
6117 Program *programObject = GetValidProgram(context, program);
6118 if (!programObject)
6119 {
6120 return false;
6121 }
6122
6123 if (uniformBlockIndex >= programObject->getActiveUniformBlockCount())
6124 {
6125 context->handleError(InvalidValue()
6126 << "uniformBlockIndex exceeds active uniform block count.");
6127 return false;
6128 }
6129
6130 switch (pname)
6131 {
6132 case GL_UNIFORM_BLOCK_BINDING:
6133 case GL_UNIFORM_BLOCK_DATA_SIZE:
6134 case GL_UNIFORM_BLOCK_NAME_LENGTH:
6135 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
6136 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES:
6137 case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
6138 case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
6139 break;
6140
6141 default:
6142 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
6143 return false;
6144 }
6145
6146 if (length)
6147 {
6148 if (pname == GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES)
6149 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08006150 const InterfaceBlock &uniformBlock =
Jamie Madill12e957f2017-08-26 21:42:26 -04006151 programObject->getUniformBlockByIndex(uniformBlockIndex);
6152 *length = static_cast<GLsizei>(uniformBlock.memberIndexes.size());
6153 }
6154 else
6155 {
6156 *length = 1;
6157 }
6158 }
6159
6160 return true;
6161}
6162
Jamie Madill9696d072017-08-26 23:19:57 -04006163template <typename ParamType>
6164bool ValidateSamplerParameterBase(Context *context,
6165 GLuint sampler,
6166 GLenum pname,
6167 GLsizei bufSize,
6168 ParamType *params)
6169{
6170 if (context->getClientMajorVersion() < 3)
6171 {
6172 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ES3Required);
6173 return false;
6174 }
6175
6176 if (!context->isSampler(sampler))
6177 {
6178 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidSampler);
6179 return false;
6180 }
6181
6182 const GLsizei minBufSize = 1;
6183 if (bufSize >= 0 && bufSize < minBufSize)
6184 {
6185 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InsufficientBufferSize);
6186 return false;
6187 }
6188
6189 switch (pname)
6190 {
6191 case GL_TEXTURE_WRAP_S:
6192 case GL_TEXTURE_WRAP_T:
6193 case GL_TEXTURE_WRAP_R:
6194 if (!ValidateTextureWrapModeValue(context, params, false))
6195 {
6196 return false;
6197 }
6198 break;
6199
6200 case GL_TEXTURE_MIN_FILTER:
6201 if (!ValidateTextureMinFilterValue(context, params, false))
6202 {
6203 return false;
6204 }
6205 break;
6206
6207 case GL_TEXTURE_MAG_FILTER:
6208 if (!ValidateTextureMagFilterValue(context, params))
6209 {
6210 return false;
6211 }
6212 break;
6213
6214 case GL_TEXTURE_MIN_LOD:
6215 case GL_TEXTURE_MAX_LOD:
6216 // any value is permissible
6217 break;
6218
6219 case GL_TEXTURE_COMPARE_MODE:
6220 if (!ValidateTextureCompareModeValue(context, params))
6221 {
6222 return false;
6223 }
6224 break;
6225
6226 case GL_TEXTURE_COMPARE_FUNC:
6227 if (!ValidateTextureCompareFuncValue(context, params))
6228 {
6229 return false;
6230 }
6231 break;
6232
6233 case GL_TEXTURE_SRGB_DECODE_EXT:
6234 if (!ValidateTextureSRGBDecodeValue(context, params))
6235 {
6236 return false;
6237 }
6238 break;
6239
Luc Ferron1b1a8642018-01-23 15:12:01 -05006240 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
6241 {
6242 GLfloat paramValue = static_cast<GLfloat>(params[0]);
6243 if (!ValidateTextureMaxAnisotropyValue(context, paramValue))
6244 {
6245 return false;
6246 }
6247 }
6248 break;
6249
Jamie Madill9696d072017-08-26 23:19:57 -04006250 default:
6251 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
6252 return false;
6253 }
6254
6255 return true;
6256}
6257
6258template bool ValidateSamplerParameterBase(Context *, GLuint, GLenum, GLsizei, GLfloat *);
6259template bool ValidateSamplerParameterBase(Context *, GLuint, GLenum, GLsizei, GLint *);
6260
6261bool ValidateGetSamplerParameterBase(Context *context,
6262 GLuint sampler,
6263 GLenum pname,
6264 GLsizei *length)
6265{
6266 if (length)
6267 {
6268 *length = 0;
6269 }
6270
6271 if (context->getClientMajorVersion() < 3)
6272 {
6273 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ES3Required);
6274 return false;
6275 }
6276
6277 if (!context->isSampler(sampler))
6278 {
6279 ANGLE_VALIDATION_ERR(context, InvalidOperation(), InvalidSampler);
6280 return false;
6281 }
6282
6283 switch (pname)
6284 {
6285 case GL_TEXTURE_WRAP_S:
6286 case GL_TEXTURE_WRAP_T:
6287 case GL_TEXTURE_WRAP_R:
6288 case GL_TEXTURE_MIN_FILTER:
6289 case GL_TEXTURE_MAG_FILTER:
6290 case GL_TEXTURE_MIN_LOD:
6291 case GL_TEXTURE_MAX_LOD:
6292 case GL_TEXTURE_COMPARE_MODE:
6293 case GL_TEXTURE_COMPARE_FUNC:
6294 break;
6295
Luc Ferron1b1a8642018-01-23 15:12:01 -05006296 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
6297 if (!ValidateTextureMaxAnisotropyExtensionEnabled(context))
6298 {
6299 return false;
6300 }
6301 break;
6302
Jamie Madill9696d072017-08-26 23:19:57 -04006303 case GL_TEXTURE_SRGB_DECODE_EXT:
6304 if (!context->getExtensions().textureSRGBDecode)
6305 {
6306 context->handleError(InvalidEnum() << "GL_EXT_texture_sRGB_decode is not enabled.");
6307 return false;
6308 }
6309 break;
6310
6311 default:
6312 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
6313 return false;
6314 }
6315
6316 if (length)
6317 {
6318 *length = 1;
6319 }
6320 return true;
6321}
6322
6323bool ValidateGetInternalFormativBase(Context *context,
6324 GLenum target,
6325 GLenum internalformat,
6326 GLenum pname,
6327 GLsizei bufSize,
6328 GLsizei *numParams)
6329{
6330 if (numParams)
6331 {
6332 *numParams = 0;
6333 }
6334
6335 if (context->getClientMajorVersion() < 3)
6336 {
Yunchao Hef0fd87d2017-09-12 04:55:05 +08006337 ANGLE_VALIDATION_ERR(context, InvalidOperation(), ES3Required);
Jamie Madill9696d072017-08-26 23:19:57 -04006338 return false;
6339 }
6340
6341 const TextureCaps &formatCaps = context->getTextureCaps().get(internalformat);
Yuly Novikovf15f8862018-06-04 18:59:41 -04006342 if (!formatCaps.renderbuffer)
Jamie Madill9696d072017-08-26 23:19:57 -04006343 {
6344 context->handleError(InvalidEnum() << "Internal format is not renderable.");
6345 return false;
6346 }
6347
6348 switch (target)
6349 {
6350 case GL_RENDERBUFFER:
6351 break;
6352
6353 case GL_TEXTURE_2D_MULTISAMPLE:
6354 if (context->getClientVersion() < ES_3_1)
6355 {
6356 context->handleError(InvalidOperation()
6357 << "Texture target requires at least OpenGL ES 3.1.");
6358 return false;
6359 }
6360 break;
6361
6362 default:
6363 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidTarget);
6364 return false;
6365 }
6366
6367 if (bufSize < 0)
6368 {
6369 ANGLE_VALIDATION_ERR(context, InvalidValue(), InsufficientBufferSize);
6370 return false;
6371 }
6372
6373 GLsizei maxWriteParams = 0;
6374 switch (pname)
6375 {
6376 case GL_NUM_SAMPLE_COUNTS:
6377 maxWriteParams = 1;
6378 break;
6379
6380 case GL_SAMPLES:
6381 maxWriteParams = static_cast<GLsizei>(formatCaps.sampleCounts.size());
6382 break;
6383
6384 default:
6385 ANGLE_VALIDATION_ERR(context, InvalidEnum(), EnumNotSupported);
6386 return false;
6387 }
6388
6389 if (numParams)
6390 {
6391 // glGetInternalFormativ will not overflow bufSize
6392 *numParams = std::min(bufSize, maxWriteParams);
6393 }
6394
6395 return true;
6396}
6397
Jamie Madille98b1b52018-03-08 09:47:23 -05006398bool ValidateFramebufferNotMultisampled(Context *context, Framebuffer *framebuffer)
6399{
Jamie Madill427064d2018-04-13 16:20:34 -04006400 if (framebuffer->getSamples(context) != 0)
Jamie Madille98b1b52018-03-08 09:47:23 -05006401 {
6402 context->handleError(InvalidOperation());
6403 return false;
6404 }
6405 return true;
6406}
6407
Lingfeng Yang038dd532018-03-29 17:31:52 -07006408bool ValidateMultitextureUnit(Context *context, GLenum texture)
6409{
6410 if (texture < GL_TEXTURE0 || texture >= GL_TEXTURE0 + context->getCaps().maxMultitextureUnits)
6411 {
6412 ANGLE_VALIDATION_ERR(context, InvalidEnum(), InvalidMultitextureUnit);
6413 return false;
6414 }
6415 return true;
6416}
6417
Jamie Madillc29968b2016-01-20 11:17:23 -05006418} // namespace gl