blob: b9ea87287ea0e1ad2a68ab71fb649428222c9499 [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"
10#include "libANGLE/validationES2.h"
11#include "libANGLE/validationES3.h"
12#include "libANGLE/Context.h"
Geoff Langa8406172015-07-21 16:53:39 -040013#include "libANGLE/Display.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050014#include "libANGLE/Texture.h"
15#include "libANGLE/Framebuffer.h"
16#include "libANGLE/FramebufferAttachment.h"
17#include "libANGLE/formatutils.h"
Geoff Langa8406172015-07-21 16:53:39 -040018#include "libANGLE/Image.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050019#include "libANGLE/Query.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050020#include "libANGLE/Program.h"
21#include "libANGLE/Uniform.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050022#include "libANGLE/TransformFeedback.h"
23#include "libANGLE/VertexArray.h"
Geoff Lange8ebe7f2013-08-05 15:03:13 -040024
25#include "common/mathutil.h"
26#include "common/utilities.h"
27
28namespace gl
29{
Jamie Madill1ca74672015-07-21 15:14:11 -040030namespace
31{
32bool ValidateDrawAttribs(gl::Context *context, GLint primcount, GLint maxVertex)
33{
34 const gl::State &state = context->getState();
35 const gl::Program *program = state.getProgram();
36
37 const VertexArray *vao = state.getVertexArray();
38 const auto &vertexAttribs = vao->getVertexAttributes();
Jamie Madill1ca74672015-07-21 15:14:11 -040039 size_t maxEnabledAttrib = vao->getMaxEnabledAttribute();
40 for (size_t attributeIndex = 0; attributeIndex < maxEnabledAttrib; ++attributeIndex)
41 {
42 const VertexAttribute &attrib = vertexAttribs[attributeIndex];
Jamie Madill63805b42015-08-25 13:17:39 -040043 if (program->isAttribLocationActive(attributeIndex) && attrib.enabled)
Jamie Madill1ca74672015-07-21 15:14:11 -040044 {
45 gl::Buffer *buffer = attrib.buffer.get();
46
47 if (buffer)
48 {
49 GLint64 attribStride = static_cast<GLint64>(ComputeVertexAttributeStride(attrib));
50 GLint64 maxVertexElement = 0;
51
52 if (attrib.divisor > 0)
53 {
54 maxVertexElement =
55 static_cast<GLint64>(primcount) / static_cast<GLint64>(attrib.divisor);
56 }
57 else
58 {
59 maxVertexElement = static_cast<GLint64>(maxVertex);
60 }
61
62 // If we're drawing zero vertices, we have enough data.
63 if (maxVertexElement > 0)
64 {
65 // Note: Last vertex element does not take the full stride!
66 GLint64 attribSize =
67 static_cast<GLint64>(ComputeVertexAttributeTypeSize(attrib));
68 GLint64 attribDataSize = (maxVertexElement - 1) * attribStride + attribSize;
69
70 // [OpenGL ES 3.0.2] section 2.9.4 page 40:
71 // We can return INVALID_OPERATION if our vertex attribute does not have
72 // enough backing data.
73 if (attribDataSize > buffer->getSize())
74 {
75 context->recordError(Error(GL_INVALID_OPERATION));
76 return false;
77 }
78 }
79 }
80 else if (attrib.pointer == NULL)
81 {
82 // This is an application error that would normally result in a crash,
83 // but we catch it and return an error
84 context->recordError(Error(
85 GL_INVALID_OPERATION, "An enabled vertex array has no buffer and no pointer."));
86 return false;
87 }
88 }
89 }
90
91 return true;
92}
93
94} // anonymous namespace
Geoff Lange8ebe7f2013-08-05 15:03:13 -040095
Geoff Lang0550d032014-01-30 11:29:07 -050096bool ValidCap(const Context *context, GLenum cap)
97{
98 switch (cap)
99 {
100 case GL_CULL_FACE:
101 case GL_POLYGON_OFFSET_FILL:
102 case GL_SAMPLE_ALPHA_TO_COVERAGE:
103 case GL_SAMPLE_COVERAGE:
104 case GL_SCISSOR_TEST:
105 case GL_STENCIL_TEST:
106 case GL_DEPTH_TEST:
107 case GL_BLEND:
108 case GL_DITHER:
109 return true;
110 case GL_PRIMITIVE_RESTART_FIXED_INDEX:
111 case GL_RASTERIZER_DISCARD:
112 return (context->getClientVersion() >= 3);
113 default:
114 return false;
115 }
116}
117
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500118bool ValidTextureTarget(const Context *context, GLenum target)
Jamie Madill35d15012013-10-07 10:46:37 -0400119{
Jamie Madilld7460c72014-01-21 16:38:14 -0500120 switch (target)
Jamie Madill35d15012013-10-07 10:46:37 -0400121 {
Jamie Madilld7460c72014-01-21 16:38:14 -0500122 case GL_TEXTURE_2D:
123 case GL_TEXTURE_CUBE_MAP:
124 return true;
Jamie Madill35d15012013-10-07 10:46:37 -0400125
Jamie Madilld7460c72014-01-21 16:38:14 -0500126 case GL_TEXTURE_3D:
127 case GL_TEXTURE_2D_ARRAY:
128 return (context->getClientVersion() >= 3);
129
130 default:
131 return false;
132 }
Jamie Madill35d15012013-10-07 10:46:37 -0400133}
134
Shannon Woods4dfed832014-03-17 20:03:39 -0400135// This function differs from ValidTextureTarget in that the target must be
136// usable as the destination of a 2D operation-- so a cube face is valid, but
137// GL_TEXTURE_CUBE_MAP is not.
Jamie Madill560a8d82014-05-21 13:06:20 -0400138// Note: duplicate of IsInternalTextureTarget
Shannon Woods4dfed832014-03-17 20:03:39 -0400139bool ValidTexture2DDestinationTarget(const Context *context, GLenum target)
140{
141 switch (target)
142 {
143 case GL_TEXTURE_2D:
144 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
145 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
146 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
147 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
148 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
149 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
150 return true;
151 case GL_TEXTURE_2D_ARRAY:
152 case GL_TEXTURE_3D:
153 return (context->getClientVersion() >= 3);
154 default:
155 return false;
156 }
157}
158
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500159bool ValidFramebufferTarget(GLenum target)
160{
Geoff Langd4475812015-03-18 10:53:05 -0400161 static_assert(GL_DRAW_FRAMEBUFFER_ANGLE == GL_DRAW_FRAMEBUFFER && GL_READ_FRAMEBUFFER_ANGLE == GL_READ_FRAMEBUFFER,
162 "ANGLE framebuffer enums must equal the ES3 framebuffer enums.");
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500163
164 switch (target)
165 {
166 case GL_FRAMEBUFFER: return true;
167 case GL_READ_FRAMEBUFFER: return true;
168 case GL_DRAW_FRAMEBUFFER: return true;
169 default: return false;
170 }
171}
172
Jamie Madill8c96d582014-03-05 15:01:23 -0500173bool ValidBufferTarget(const Context *context, GLenum target)
174{
175 switch (target)
176 {
177 case GL_ARRAY_BUFFER:
178 case GL_ELEMENT_ARRAY_BUFFER:
179 return true;
180
Jamie Madill8c96d582014-03-05 15:01:23 -0500181 case GL_PIXEL_PACK_BUFFER:
182 case GL_PIXEL_UNPACK_BUFFER:
Geoff Lange4de3072015-09-02 11:01:32 -0400183 return (context->getExtensions().pixelBufferObject || context->getClientVersion() >= 3);
Shannon Woods158c4382014-05-06 13:00:07 -0400184
Shannon Woodsb3801742014-03-27 14:59:19 -0400185 case GL_COPY_READ_BUFFER:
186 case GL_COPY_WRITE_BUFFER:
Jamie Madill8c96d582014-03-05 15:01:23 -0500187 case GL_TRANSFORM_FEEDBACK_BUFFER:
188 case GL_UNIFORM_BUFFER:
189 return (context->getClientVersion() >= 3);
190
191 default:
192 return false;
193 }
194}
195
Jamie Madill70656a62014-03-05 15:01:26 -0500196bool ValidBufferParameter(const Context *context, GLenum pname)
197{
Geoff Langcc6f55d2015-03-20 13:01:02 -0400198 const Extensions &extensions = context->getExtensions();
199
Jamie Madill70656a62014-03-05 15:01:26 -0500200 switch (pname)
201 {
202 case GL_BUFFER_USAGE:
203 case GL_BUFFER_SIZE:
204 return true;
205
Geoff Langcc6f55d2015-03-20 13:01:02 -0400206 case GL_BUFFER_ACCESS_OES:
207 return extensions.mapBuffer;
208
209 case GL_BUFFER_MAPPED:
210 static_assert(GL_BUFFER_MAPPED == GL_BUFFER_MAPPED_OES, "GL enums should be equal.");
211 return (context->getClientVersion() >= 3) || extensions.mapBuffer || extensions.mapBufferRange;
212
Jamie Madill70656a62014-03-05 15:01:26 -0500213 // GL_BUFFER_MAP_POINTER is a special case, and may only be
214 // queried with GetBufferPointerv
215 case GL_BUFFER_ACCESS_FLAGS:
Jamie Madill70656a62014-03-05 15:01:26 -0500216 case GL_BUFFER_MAP_OFFSET:
217 case GL_BUFFER_MAP_LENGTH:
Geoff Langcc6f55d2015-03-20 13:01:02 -0400218 return (context->getClientVersion() >= 3) || extensions.mapBufferRange;
Jamie Madill70656a62014-03-05 15:01:26 -0500219
220 default:
221 return false;
222 }
223}
224
Jamie Madill8c96d582014-03-05 15:01:23 -0500225bool ValidMipLevel(const Context *context, GLenum target, GLint level)
Geoff Langce635692013-09-24 13:56:32 -0400226{
Geoff Langaae65a42014-05-26 12:43:44 -0400227 size_t maxDimension = 0;
Geoff Langce635692013-09-24 13:56:32 -0400228 switch (target)
229 {
Geoff Langaae65a42014-05-26 12:43:44 -0400230 case GL_TEXTURE_2D: maxDimension = context->getCaps().max2DTextureSize; break;
Geoff Langce635692013-09-24 13:56:32 -0400231 case GL_TEXTURE_CUBE_MAP:
232 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
233 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
234 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
235 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
236 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
Geoff Langaae65a42014-05-26 12:43:44 -0400237 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: maxDimension = context->getCaps().maxCubeMapTextureSize; break;
238 case GL_TEXTURE_3D: maxDimension = context->getCaps().max3DTextureSize; break;
239 case GL_TEXTURE_2D_ARRAY: maxDimension = context->getCaps().max2DTextureSize; break;
Geoff Langce635692013-09-24 13:56:32 -0400240 default: UNREACHABLE();
241 }
242
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700243 return level <= gl::log2(static_cast<int>(maxDimension));
Geoff Langce635692013-09-24 13:56:32 -0400244}
245
Austin Kinross08528e12015-10-07 16:24:40 -0700246bool ValidImageSizeParameters(const Context *context,
247 GLenum target,
248 GLint level,
249 GLsizei width,
250 GLsizei height,
251 GLsizei depth,
252 bool isSubImage)
Geoff Langce635692013-09-24 13:56:32 -0400253{
254 if (level < 0 || width < 0 || height < 0 || depth < 0)
255 {
256 return false;
257 }
258
Austin Kinross08528e12015-10-07 16:24:40 -0700259 // TexSubImage parameters can be NPOT without textureNPOT extension,
260 // as long as the destination texture is POT.
261 if (!isSubImage && !context->getExtensions().textureNPOT &&
Jamie Madill4fd75c12014-06-23 10:53:54 -0400262 (level != 0 && (!gl::isPow2(width) || !gl::isPow2(height) || !gl::isPow2(depth))))
Geoff Langce635692013-09-24 13:56:32 -0400263 {
264 return false;
265 }
266
267 if (!ValidMipLevel(context, target, level))
268 {
269 return false;
270 }
271
272 return true;
273}
274
Geoff Lang0d8b7242015-09-09 14:56:53 -0400275bool CompressedTextureFormatRequiresExactSize(GLenum internalFormat)
276{
277 // List of compressed format that require that the texture size is smaller than or a multiple of
278 // the compressed block size.
279 switch (internalFormat)
280 {
281 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
282 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
283 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
284 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
285 return true;
286
287 default:
288 return false;
289 }
290}
291
Geoff Langb1196682014-07-23 13:47:29 -0400292bool ValidCompressedImageSize(const Context *context, GLenum internalFormat, GLsizei width, GLsizei height)
Geoff Langd4f180b2013-09-24 13:57:44 -0400293{
Geoff Lang5d601382014-07-22 15:14:06 -0400294 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalFormat);
295 if (!formatInfo.compressed)
Geoff Langd4f180b2013-09-24 13:57:44 -0400296 {
297 return false;
298 }
299
Geoff Lang0d8b7242015-09-09 14:56:53 -0400300 if (width < 0 || height < 0)
Geoff Langd4f180b2013-09-24 13:57:44 -0400301 {
302 return false;
303 }
304
Geoff Lang0d8b7242015-09-09 14:56:53 -0400305 if (CompressedTextureFormatRequiresExactSize(internalFormat))
306 {
307 if ((static_cast<GLuint>(width) > formatInfo.compressedBlockWidth &&
308 width % formatInfo.compressedBlockWidth != 0) ||
309 (static_cast<GLuint>(height) > formatInfo.compressedBlockHeight &&
310 height % formatInfo.compressedBlockHeight != 0))
311 {
312 return false;
313 }
314 }
315
Geoff Langd4f180b2013-09-24 13:57:44 -0400316 return true;
317}
318
Geoff Lang37dde692014-01-31 16:34:54 -0500319bool ValidQueryType(const Context *context, GLenum queryType)
320{
Geoff Langd4475812015-03-18 10:53:05 -0400321 static_assert(GL_ANY_SAMPLES_PASSED == GL_ANY_SAMPLES_PASSED_EXT, "GL extension enums not equal.");
322 static_assert(GL_ANY_SAMPLES_PASSED_CONSERVATIVE == GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, "GL extension enums not equal.");
Geoff Lang37dde692014-01-31 16:34:54 -0500323
324 switch (queryType)
325 {
326 case GL_ANY_SAMPLES_PASSED:
327 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
328 return true;
329 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
330 return (context->getClientVersion() >= 3);
331 default:
332 return false;
333 }
334}
335
Dian Xiang769769a2015-09-09 15:20:08 -0700336Program *GetValidProgram(Context *context, GLuint id)
Geoff Lang48dcae72014-02-05 16:28:24 -0500337{
338 // ES3 spec (section 2.11.1) -- "Commands that accept shader or program object names will generate the
339 // error INVALID_VALUE if the provided name is not the name of either a shader or program object and
340 // INVALID_OPERATION if the provided name identifies an object that is not the expected type."
341
Dian Xiang769769a2015-09-09 15:20:08 -0700342 Program *validProgram = context->getProgram(id);
343
344 if (!validProgram)
Geoff Lang48dcae72014-02-05 16:28:24 -0500345 {
Dian Xiang769769a2015-09-09 15:20:08 -0700346 if (context->getShader(id))
347 {
348 context->recordError(
349 Error(GL_INVALID_OPERATION, "Expected a program name, but found a shader name"));
350 }
351 else
352 {
353 context->recordError(Error(GL_INVALID_VALUE, "Program name is not valid"));
354 }
Geoff Lang48dcae72014-02-05 16:28:24 -0500355 }
Dian Xiang769769a2015-09-09 15:20:08 -0700356
357 return validProgram;
358}
359
360Shader *GetValidShader(Context *context, GLuint id)
361{
362 // See ValidProgram for spec details.
363
364 Shader *validShader = context->getShader(id);
365
366 if (!validShader)
Geoff Lang48dcae72014-02-05 16:28:24 -0500367 {
Dian Xiang769769a2015-09-09 15:20:08 -0700368 if (context->getProgram(id))
369 {
370 context->recordError(
371 Error(GL_INVALID_OPERATION, "Expected a shader name, but found a program name"));
372 }
373 else
374 {
375 context->recordError(Error(GL_INVALID_VALUE, "Shader name is invalid"));
376 }
Geoff Lang48dcae72014-02-05 16:28:24 -0500377 }
Dian Xiang769769a2015-09-09 15:20:08 -0700378
379 return validShader;
Geoff Lang48dcae72014-02-05 16:28:24 -0500380}
381
Geoff Langb1196682014-07-23 13:47:29 -0400382bool ValidateAttachmentTarget(gl::Context *context, GLenum attachment)
Jamie Madillb4472272014-07-03 10:38:55 -0400383{
384 if (attachment >= GL_COLOR_ATTACHMENT0_EXT && attachment <= GL_COLOR_ATTACHMENT15_EXT)
385 {
386 const unsigned int colorAttachment = (attachment - GL_COLOR_ATTACHMENT0_EXT);
387
Geoff Langaae65a42014-05-26 12:43:44 -0400388 if (colorAttachment >= context->getCaps().maxColorAttachments)
Jamie Madillb4472272014-07-03 10:38:55 -0400389 {
Geoff Langb1196682014-07-23 13:47:29 -0400390 context->recordError(Error(GL_INVALID_VALUE));
391 return false;
Jamie Madillb4472272014-07-03 10:38:55 -0400392 }
393 }
394 else
395 {
396 switch (attachment)
397 {
398 case GL_DEPTH_ATTACHMENT:
399 case GL_STENCIL_ATTACHMENT:
400 break;
401
402 case GL_DEPTH_STENCIL_ATTACHMENT:
403 if (context->getClientVersion() < 3)
404 {
Geoff Langb1196682014-07-23 13:47:29 -0400405 context->recordError(Error(GL_INVALID_ENUM));
406 return false;
Jamie Madillb4472272014-07-03 10:38:55 -0400407 }
408 break;
409
410 default:
Geoff Langb1196682014-07-23 13:47:29 -0400411 context->recordError(Error(GL_INVALID_ENUM));
412 return false;
Jamie Madillb4472272014-07-03 10:38:55 -0400413 }
414 }
415
416 return true;
417}
418
Corentin Walleze0902642014-11-04 12:32:15 -0800419bool ValidateRenderbufferStorageParametersBase(gl::Context *context, GLenum target, GLsizei samples,
420 GLenum internalformat, GLsizei width, GLsizei height)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400421{
422 switch (target)
423 {
424 case GL_RENDERBUFFER:
425 break;
426 default:
Geoff Langb1196682014-07-23 13:47:29 -0400427 context->recordError(Error(GL_INVALID_ENUM));
428 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400429 }
430
431 if (width < 0 || height < 0 || samples < 0)
432 {
Geoff Langb1196682014-07-23 13:47:29 -0400433 context->recordError(Error(GL_INVALID_VALUE));
434 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400435 }
436
Geoff Langd87878e2014-09-19 15:42:59 -0400437 const TextureCaps &formatCaps = context->getTextureCaps().get(internalformat);
438 if (!formatCaps.renderable)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400439 {
Geoff Langb1196682014-07-23 13:47:29 -0400440 context->recordError(Error(GL_INVALID_ENUM));
441 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400442 }
443
444 // ANGLE_framebuffer_multisample does not explicitly state that the internal format must be
445 // 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 -0800446 // only sized internal formats.
Geoff Langd87878e2014-09-19 15:42:59 -0400447 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalformat);
Geoff Lang5d601382014-07-22 15:14:06 -0400448 if (formatInfo.pixelBytes == 0)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400449 {
Geoff Langb1196682014-07-23 13:47:29 -0400450 context->recordError(Error(GL_INVALID_ENUM));
451 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400452 }
453
Geoff Langaae65a42014-05-26 12:43:44 -0400454 if (static_cast<GLuint>(std::max(width, height)) > context->getCaps().maxRenderbufferSize)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400455 {
Geoff Langb1196682014-07-23 13:47:29 -0400456 context->recordError(Error(GL_INVALID_VALUE));
457 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400458 }
459
Shannon Woods53a94a82014-06-24 15:20:36 -0400460 GLuint handle = context->getState().getRenderbufferId();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400461 if (handle == 0)
462 {
Geoff Langb1196682014-07-23 13:47:29 -0400463 context->recordError(Error(GL_INVALID_OPERATION));
464 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400465 }
466
467 return true;
468}
469
Corentin Walleze0902642014-11-04 12:32:15 -0800470bool ValidateRenderbufferStorageParametersANGLE(gl::Context *context, GLenum target, GLsizei samples,
471 GLenum internalformat, GLsizei width, GLsizei height)
472{
Austin Kinrossd2cf3ad2015-01-07 14:00:30 -0800473 ASSERT(samples == 0 || context->getExtensions().framebufferMultisample);
Corentin Walleze0902642014-11-04 12:32:15 -0800474
475 // ANGLE_framebuffer_multisample states that the value of samples must be less than or equal
Geoff Langdef624b2015-04-13 10:46:56 -0400476 // to MAX_SAMPLES_ANGLE (Context::getCaps().maxSamples) otherwise GL_INVALID_VALUE is
Corentin Walleze0902642014-11-04 12:32:15 -0800477 // generated.
Geoff Langdef624b2015-04-13 10:46:56 -0400478 if (static_cast<GLuint>(samples) > context->getCaps().maxSamples)
Corentin Walleze0902642014-11-04 12:32:15 -0800479 {
480 context->recordError(Error(GL_INVALID_VALUE));
481 return false;
482 }
483
484 // ANGLE_framebuffer_multisample states GL_OUT_OF_MEMORY is generated on a failure to create
485 // the specified storage. This is different than ES 3.0 in which a sample number higher
486 // than the maximum sample number supported by this format generates a GL_INVALID_VALUE.
Geoff Langa4903b72015-03-02 16:02:48 -0800487 // The TextureCaps::getMaxSamples method is only guarenteed to be valid when the context is ES3.
488 if (context->getClientVersion() >= 3)
Corentin Walleze0902642014-11-04 12:32:15 -0800489 {
Geoff Langa4903b72015-03-02 16:02:48 -0800490 const TextureCaps &formatCaps = context->getTextureCaps().get(internalformat);
491 if (static_cast<GLuint>(samples) > formatCaps.getMaxSamples())
492 {
493 context->recordError(Error(GL_OUT_OF_MEMORY));
494 return false;
495 }
Corentin Walleze0902642014-11-04 12:32:15 -0800496 }
497
498 return ValidateRenderbufferStorageParametersBase(context, target, samples, internalformat, width, height);
499}
500
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500501bool ValidateFramebufferRenderbufferParameters(gl::Context *context, GLenum target, GLenum attachment,
502 GLenum renderbuffertarget, GLuint renderbuffer)
503{
Shannon Woods1da3cf62014-06-27 15:32:23 -0400504 if (!ValidFramebufferTarget(target))
505 {
Geoff Langb1196682014-07-23 13:47:29 -0400506 context->recordError(Error(GL_INVALID_ENUM));
507 return false;
Shannon Woods1da3cf62014-06-27 15:32:23 -0400508 }
509
Shannon Woods53a94a82014-06-24 15:20:36 -0400510 gl::Framebuffer *framebuffer = context->getState().getTargetFramebuffer(target);
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500511
Jamie Madill84115c92015-04-23 15:00:07 -0400512 ASSERT(framebuffer);
513 if (framebuffer->id() == 0)
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500514 {
Jamie Madill84115c92015-04-23 15:00:07 -0400515 context->recordError(Error(GL_INVALID_OPERATION, "Cannot change default FBO's attachments"));
Geoff Langb1196682014-07-23 13:47:29 -0400516 return false;
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500517 }
518
Jamie Madillb4472272014-07-03 10:38:55 -0400519 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500520 {
Jamie Madillb4472272014-07-03 10:38:55 -0400521 return false;
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500522 }
523
Jamie Madillab9d82c2014-01-21 16:38:14 -0500524 // [OpenGL ES 2.0.25] Section 4.4.3 page 112
525 // [OpenGL ES 3.0.2] Section 4.4.2 page 201
526 // 'renderbuffer' must be either zero or the name of an existing renderbuffer object of
527 // type 'renderbuffertarget', otherwise an INVALID_OPERATION error is generated.
528 if (renderbuffer != 0)
529 {
530 if (!context->getRenderbuffer(renderbuffer))
531 {
Geoff Langb1196682014-07-23 13:47:29 -0400532 context->recordError(Error(GL_INVALID_OPERATION));
533 return false;
Jamie Madillab9d82c2014-01-21 16:38:14 -0500534 }
535 }
536
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500537 return true;
538}
539
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400540static bool IsPartialBlit(gl::Context *context, const gl::FramebufferAttachment *readBuffer, const gl::FramebufferAttachment *writeBuffer,
Geoff Lang125deab2013-08-09 13:34:16 -0400541 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
542 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1)
543{
544 if (srcX0 != 0 || srcY0 != 0 || dstX0 != 0 || dstY0 != 0 ||
545 dstX1 != writeBuffer->getWidth() || dstY1 != writeBuffer->getHeight() ||
546 srcX1 != readBuffer->getWidth() || srcY1 != readBuffer->getHeight())
547 {
548 return true;
549 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400550 else if (context->getState().isScissorTestEnabled())
Geoff Lang125deab2013-08-09 13:34:16 -0400551 {
Shannon Woods53a94a82014-06-24 15:20:36 -0400552 const Rectangle &scissor = context->getState().getScissor();
Geoff Lang125deab2013-08-09 13:34:16 -0400553
Shannon Woods53a94a82014-06-24 15:20:36 -0400554 return scissor.x > 0 || scissor.y > 0 ||
555 scissor.width < writeBuffer->getWidth() ||
556 scissor.height < writeBuffer->getHeight();
Geoff Lang125deab2013-08-09 13:34:16 -0400557 }
558 else
559 {
560 return false;
561 }
562}
563
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400564bool ValidateBlitFramebufferParameters(gl::Context *context, GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400565 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask,
566 GLenum filter, bool fromAngleExtension)
567{
568 switch (filter)
569 {
570 case GL_NEAREST:
571 break;
572 case GL_LINEAR:
573 if (fromAngleExtension)
574 {
Geoff Langb1196682014-07-23 13:47:29 -0400575 context->recordError(Error(GL_INVALID_ENUM));
576 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400577 }
578 break;
579 default:
Geoff Langb1196682014-07-23 13:47:29 -0400580 context->recordError(Error(GL_INVALID_ENUM));
581 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400582 }
583
584 if ((mask & ~(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)) != 0)
585 {
Geoff Langb1196682014-07-23 13:47:29 -0400586 context->recordError(Error(GL_INVALID_VALUE));
587 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400588 }
589
590 if (mask == 0)
591 {
592 // ES3.0 spec, section 4.3.2 specifies that a mask of zero is valid and no
593 // buffers are copied.
594 return false;
595 }
596
597 if (fromAngleExtension && (srcX1 - srcX0 != dstX1 - dstX0 || srcY1 - srcY0 != dstY1 - dstY0))
598 {
599 ERR("Scaling and flipping in BlitFramebufferANGLE not supported by this implementation.");
Geoff Langb1196682014-07-23 13:47:29 -0400600 context->recordError(Error(GL_INVALID_OPERATION));
601 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400602 }
603
604 // ES3.0 spec, section 4.3.2 states that linear filtering is only available for the
605 // color buffer, leaving only nearest being unfiltered from above
606 if ((mask & ~GL_COLOR_BUFFER_BIT) != 0 && filter != GL_NEAREST)
607 {
Geoff Langb1196682014-07-23 13:47:29 -0400608 context->recordError(Error(GL_INVALID_OPERATION));
609 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400610 }
611
Shannon Woods53a94a82014-06-24 15:20:36 -0400612 if (context->getState().getReadFramebuffer()->id() == context->getState().getDrawFramebuffer()->id())
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400613 {
614 if (fromAngleExtension)
615 {
616 ERR("Blits with the same source and destination framebuffer are not supported by this "
617 "implementation.");
618 }
Geoff Langb1196682014-07-23 13:47:29 -0400619 context->recordError(Error(GL_INVALID_OPERATION));
620 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400621 }
622
Jamie Madille3ef7152015-04-28 16:55:17 +0000623 const gl::Framebuffer *readFramebuffer = context->getState().getReadFramebuffer();
624 const gl::Framebuffer *drawFramebuffer = context->getState().getDrawFramebuffer();
Jamie Madill48faf802014-11-06 15:27:22 -0500625
626 if (!readFramebuffer || !drawFramebuffer)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400627 {
Geoff Langb1196682014-07-23 13:47:29 -0400628 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
629 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400630 }
631
Geoff Lang748f74e2014-12-01 11:25:34 -0500632 if (!readFramebuffer->checkStatus(context->getData()))
Jamie Madill48faf802014-11-06 15:27:22 -0500633 {
634 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
635 return false;
636 }
637
Geoff Lang748f74e2014-12-01 11:25:34 -0500638 if (!drawFramebuffer->checkStatus(context->getData()))
Jamie Madill48faf802014-11-06 15:27:22 -0500639 {
640 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
641 return false;
642 }
643
644 if (drawFramebuffer->getSamples(context->getData()) != 0)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400645 {
Geoff Langb1196682014-07-23 13:47:29 -0400646 context->recordError(Error(GL_INVALID_OPERATION));
647 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400648 }
649
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400650 bool sameBounds = srcX0 == dstX0 && srcY0 == dstY0 && srcX1 == dstX1 && srcY1 == dstY1;
651
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400652 if (mask & GL_COLOR_BUFFER_BIT)
653 {
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400654 const gl::FramebufferAttachment *readColorBuffer = readFramebuffer->getReadColorbuffer();
655 const gl::FramebufferAttachment *drawColorBuffer = drawFramebuffer->getFirstColorbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400656
657 if (readColorBuffer && drawColorBuffer)
658 {
Geoff Langd8a22582014-12-17 15:28:23 -0500659 GLenum readInternalFormat = readColorBuffer->getInternalFormat();
Geoff Lang5d601382014-07-22 15:14:06 -0400660 const InternalFormat &readFormatInfo = GetInternalFormatInfo(readInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400661
Corentin Wallez37c39792015-08-20 14:19:46 -0400662 for (size_t i = 0; i < drawFramebuffer->getNumColorBuffers(); i++)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400663 {
664 if (drawFramebuffer->isEnabledColorAttachment(i))
665 {
Geoff Langd8a22582014-12-17 15:28:23 -0500666 GLenum drawInternalFormat = drawFramebuffer->getColorbuffer(i)->getInternalFormat();
Geoff Lang5d601382014-07-22 15:14:06 -0400667 const InternalFormat &drawFormatInfo = GetInternalFormatInfo(drawInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400668
Geoff Langb2f3d052013-08-13 12:49:27 -0400669 // The GL ES 3.0.2 spec (pg 193) states that:
670 // 1) If the read buffer is fixed point format, the draw buffer must be as well
671 // 2) If the read buffer is an unsigned integer format, the draw buffer must be as well
672 // 3) If the read buffer is a signed integer format, the draw buffer must be as well
Geoff Lang5d601382014-07-22 15:14:06 -0400673 if ( (readFormatInfo.componentType == GL_UNSIGNED_NORMALIZED || readFormatInfo.componentType == GL_SIGNED_NORMALIZED) &&
674 !(drawFormatInfo.componentType == GL_UNSIGNED_NORMALIZED || drawFormatInfo.componentType == GL_SIGNED_NORMALIZED))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400675 {
Geoff Langb1196682014-07-23 13:47:29 -0400676 context->recordError(Error(GL_INVALID_OPERATION));
677 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400678 }
679
Geoff Lang5d601382014-07-22 15:14:06 -0400680 if (readFormatInfo.componentType == GL_UNSIGNED_INT && drawFormatInfo.componentType != GL_UNSIGNED_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400681 {
Geoff Langb1196682014-07-23 13:47:29 -0400682 context->recordError(Error(GL_INVALID_OPERATION));
683 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400684 }
685
Geoff Lang5d601382014-07-22 15:14:06 -0400686 if (readFormatInfo.componentType == GL_INT && drawFormatInfo.componentType != GL_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400687 {
Geoff Langb1196682014-07-23 13:47:29 -0400688 context->recordError(Error(GL_INVALID_OPERATION));
689 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400690 }
691
Geoff Langb2f3d052013-08-13 12:49:27 -0400692 if (readColorBuffer->getSamples() > 0 && (readInternalFormat != drawInternalFormat || !sameBounds))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400693 {
Geoff Langb1196682014-07-23 13:47:29 -0400694 context->recordError(Error(GL_INVALID_OPERATION));
695 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400696 }
697 }
698 }
699
Geoff Lang5d601382014-07-22 15:14:06 -0400700 if ((readFormatInfo.componentType == GL_INT || readFormatInfo.componentType == GL_UNSIGNED_INT) && filter == GL_LINEAR)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400701 {
Geoff Langb1196682014-07-23 13:47:29 -0400702 context->recordError(Error(GL_INVALID_OPERATION));
703 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400704 }
705
706 if (fromAngleExtension)
707 {
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400708 const FramebufferAttachment *readColorAttachment = readFramebuffer->getReadColorbuffer();
Geoff Lang6a1e6b92014-11-06 10:42:45 -0500709 if (!readColorAttachment ||
Jamie Madill8cf4a392015-04-02 11:36:04 -0400710 (!(readColorAttachment->type() == GL_TEXTURE && readColorAttachment->getTextureImageIndex().type == GL_TEXTURE_2D) &&
Geoff Lang6a1e6b92014-11-06 10:42:45 -0500711 readColorAttachment->type() != GL_RENDERBUFFER &&
712 readColorAttachment->type() != GL_FRAMEBUFFER_DEFAULT))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400713 {
Geoff Langb1196682014-07-23 13:47:29 -0400714 context->recordError(Error(GL_INVALID_OPERATION));
715 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400716 }
717
Corentin Wallez37c39792015-08-20 14:19:46 -0400718 for (size_t colorAttachment = 0;
719 colorAttachment < drawFramebuffer->getNumColorBuffers(); ++colorAttachment)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400720 {
721 if (drawFramebuffer->isEnabledColorAttachment(colorAttachment))
722 {
Jamie Madille3ef7152015-04-28 16:55:17 +0000723 const FramebufferAttachment *attachment = drawFramebuffer->getColorbuffer(colorAttachment);
Jamie Madille92a3542014-07-03 10:38:58 -0400724 ASSERT(attachment);
725
Jamie Madill8cf4a392015-04-02 11:36:04 -0400726 if (!(attachment->type() == GL_TEXTURE && attachment->getTextureImageIndex().type == GL_TEXTURE_2D) &&
Geoff Lang6a1e6b92014-11-06 10:42:45 -0500727 attachment->type() != GL_RENDERBUFFER &&
728 attachment->type() != GL_FRAMEBUFFER_DEFAULT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400729 {
Geoff Langb1196682014-07-23 13:47:29 -0400730 context->recordError(Error(GL_INVALID_OPERATION));
731 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400732 }
733
Jamie Madillf8f18f02014-10-02 10:44:17 -0400734 // Return an error if the destination formats do not match
735 if (attachment->getInternalFormat() != readColorBuffer->getInternalFormat())
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400736 {
Geoff Langb1196682014-07-23 13:47:29 -0400737 context->recordError(Error(GL_INVALID_OPERATION));
738 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400739 }
740 }
741 }
Jamie Madill48faf802014-11-06 15:27:22 -0500742
743 int readSamples = readFramebuffer->getSamples(context->getData());
744
745 if (readSamples != 0 && IsPartialBlit(context, readColorBuffer, drawColorBuffer,
746 srcX0, srcY0, srcX1, srcY1,
747 dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400748 {
Geoff Langb1196682014-07-23 13:47:29 -0400749 context->recordError(Error(GL_INVALID_OPERATION));
750 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400751 }
752 }
753 }
754 }
755
Dongseong Hwang44b422c2014-12-09 15:42:01 +0200756 GLenum masks[] = {GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT};
757 GLenum attachments[] = {GL_DEPTH_ATTACHMENT, GL_STENCIL_ATTACHMENT};
758 for (size_t i = 0; i < 2; i++)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400759 {
Dongseong Hwang44b422c2014-12-09 15:42:01 +0200760 if (mask & masks[i])
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400761 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400762 const gl::FramebufferAttachment *readBuffer = readFramebuffer->getAttachment(attachments[i]);
763 const gl::FramebufferAttachment *drawBuffer = drawFramebuffer->getAttachment(attachments[i]);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400764
Dongseong Hwang44b422c2014-12-09 15:42:01 +0200765 if (readBuffer && drawBuffer)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400766 {
Geoff Langd8a22582014-12-17 15:28:23 -0500767 if (readBuffer->getInternalFormat() != drawBuffer->getInternalFormat())
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400768 {
Geoff Langb1196682014-07-23 13:47:29 -0400769 context->recordError(Error(GL_INVALID_OPERATION));
770 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400771 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400772
Dongseong Hwang44b422c2014-12-09 15:42:01 +0200773 if (readBuffer->getSamples() > 0 && !sameBounds)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400774 {
Geoff Langb1196682014-07-23 13:47:29 -0400775 context->recordError(Error(GL_INVALID_OPERATION));
776 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400777 }
Dongseong Hwang44b422c2014-12-09 15:42:01 +0200778
779 if (fromAngleExtension)
780 {
781 if (IsPartialBlit(context, readBuffer, drawBuffer,
782 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
783 {
784 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
785 context->recordError(Error(GL_INVALID_OPERATION)); // only whole-buffer copies are permitted
786 return false;
787 }
788
789 if (readBuffer->getSamples() != 0 || drawBuffer->getSamples() != 0)
790 {
791 context->recordError(Error(GL_INVALID_OPERATION));
792 return false;
793 }
794 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400795 }
796 }
797 }
798
799 return true;
800}
801
Geoff Langb1196682014-07-23 13:47:29 -0400802bool ValidateGetVertexAttribParameters(Context *context, GLenum pname)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400803{
804 switch (pname)
805 {
806 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
807 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
808 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
809 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
810 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
811 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
812 case GL_CURRENT_VERTEX_ATTRIB:
813 return true;
814
815 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR:
816 // Don't verify ES3 context because GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE uses
817 // the same constant.
Geoff Langd4475812015-03-18 10:53:05 -0400818 static_assert(GL_VERTEX_ATTRIB_ARRAY_DIVISOR == GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE,
819 "ANGLE extension enums not equal to GL enums.");
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400820 return true;
821
822 case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
Geoff Langb1196682014-07-23 13:47:29 -0400823 if (context->getClientVersion() < 3)
824 {
825 context->recordError(Error(GL_INVALID_ENUM));
826 return false;
827 }
828 return true;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400829
830 default:
Geoff Langb1196682014-07-23 13:47:29 -0400831 context->recordError(Error(GL_INVALID_ENUM));
832 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400833 }
834}
835
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400836bool ValidateTexParamParameters(gl::Context *context, GLenum pname, GLint param)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400837{
838 switch (pname)
839 {
840 case GL_TEXTURE_WRAP_R:
841 case GL_TEXTURE_SWIZZLE_R:
842 case GL_TEXTURE_SWIZZLE_G:
843 case GL_TEXTURE_SWIZZLE_B:
844 case GL_TEXTURE_SWIZZLE_A:
845 case GL_TEXTURE_BASE_LEVEL:
846 case GL_TEXTURE_MAX_LEVEL:
847 case GL_TEXTURE_COMPARE_MODE:
848 case GL_TEXTURE_COMPARE_FUNC:
849 case GL_TEXTURE_MIN_LOD:
850 case GL_TEXTURE_MAX_LOD:
851 if (context->getClientVersion() < 3)
852 {
Geoff Langb1196682014-07-23 13:47:29 -0400853 context->recordError(Error(GL_INVALID_ENUM));
854 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400855 }
856 break;
857
858 default: break;
859 }
860
861 switch (pname)
862 {
863 case GL_TEXTURE_WRAP_S:
864 case GL_TEXTURE_WRAP_T:
865 case GL_TEXTURE_WRAP_R:
866 switch (param)
867 {
868 case GL_REPEAT:
869 case GL_CLAMP_TO_EDGE:
870 case GL_MIRRORED_REPEAT:
871 return true;
872 default:
Geoff Langb1196682014-07-23 13:47:29 -0400873 context->recordError(Error(GL_INVALID_ENUM));
874 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400875 }
876
877 case GL_TEXTURE_MIN_FILTER:
878 switch (param)
879 {
880 case GL_NEAREST:
881 case GL_LINEAR:
882 case GL_NEAREST_MIPMAP_NEAREST:
883 case GL_LINEAR_MIPMAP_NEAREST:
884 case GL_NEAREST_MIPMAP_LINEAR:
885 case GL_LINEAR_MIPMAP_LINEAR:
886 return true;
887 default:
Geoff Langb1196682014-07-23 13:47:29 -0400888 context->recordError(Error(GL_INVALID_ENUM));
889 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400890 }
891 break;
892
893 case GL_TEXTURE_MAG_FILTER:
894 switch (param)
895 {
896 case GL_NEAREST:
897 case GL_LINEAR:
898 return true;
899 default:
Geoff Langb1196682014-07-23 13:47:29 -0400900 context->recordError(Error(GL_INVALID_ENUM));
901 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400902 }
903 break;
904
905 case GL_TEXTURE_USAGE_ANGLE:
906 switch (param)
907 {
908 case GL_NONE:
909 case GL_FRAMEBUFFER_ATTACHMENT_ANGLE:
910 return true;
911 default:
Geoff Langb1196682014-07-23 13:47:29 -0400912 context->recordError(Error(GL_INVALID_ENUM));
913 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400914 }
915 break;
916
917 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
Geoff Langc0b9ef42014-07-02 10:02:37 -0400918 if (!context->getExtensions().textureFilterAnisotropic)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400919 {
Geoff Langb1196682014-07-23 13:47:29 -0400920 context->recordError(Error(GL_INVALID_ENUM));
921 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400922 }
923
924 // we assume the parameter passed to this validation method is truncated, not rounded
925 if (param < 1)
926 {
Geoff Langb1196682014-07-23 13:47:29 -0400927 context->recordError(Error(GL_INVALID_VALUE));
928 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400929 }
930 return true;
931
932 case GL_TEXTURE_MIN_LOD:
933 case GL_TEXTURE_MAX_LOD:
934 // any value is permissible
935 return true;
936
937 case GL_TEXTURE_COMPARE_MODE:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400938 // Acceptable mode parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400939 switch (param)
940 {
941 case GL_NONE:
942 case GL_COMPARE_REF_TO_TEXTURE:
943 return true;
944 default:
Geoff Langb1196682014-07-23 13:47:29 -0400945 context->recordError(Error(GL_INVALID_ENUM));
946 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400947 }
948 break;
949
950 case GL_TEXTURE_COMPARE_FUNC:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400951 // Acceptable function parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400952 switch (param)
953 {
954 case GL_LEQUAL:
955 case GL_GEQUAL:
956 case GL_LESS:
957 case GL_GREATER:
958 case GL_EQUAL:
959 case GL_NOTEQUAL:
960 case GL_ALWAYS:
961 case GL_NEVER:
962 return true;
963 default:
Geoff Langb1196682014-07-23 13:47:29 -0400964 context->recordError(Error(GL_INVALID_ENUM));
965 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400966 }
967 break;
968
969 case GL_TEXTURE_SWIZZLE_R:
970 case GL_TEXTURE_SWIZZLE_G:
971 case GL_TEXTURE_SWIZZLE_B:
972 case GL_TEXTURE_SWIZZLE_A:
Geoff Langbc90a482013-09-17 16:51:27 -0400973 switch (param)
974 {
975 case GL_RED:
976 case GL_GREEN:
977 case GL_BLUE:
978 case GL_ALPHA:
979 case GL_ZERO:
980 case GL_ONE:
981 return true;
982 default:
Geoff Langb1196682014-07-23 13:47:29 -0400983 context->recordError(Error(GL_INVALID_ENUM));
984 return false;
Geoff Langbc90a482013-09-17 16:51:27 -0400985 }
986 break;
987
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400988 case GL_TEXTURE_BASE_LEVEL:
989 case GL_TEXTURE_MAX_LEVEL:
Nicolas Capens8de68282014-04-04 11:10:27 -0400990 if (param < 0)
991 {
Geoff Langb1196682014-07-23 13:47:29 -0400992 context->recordError(Error(GL_INVALID_VALUE));
993 return false;
Nicolas Capens8de68282014-04-04 11:10:27 -0400994 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400995 return true;
996
997 default:
Geoff Langb1196682014-07-23 13:47:29 -0400998 context->recordError(Error(GL_INVALID_ENUM));
999 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001000 }
1001}
1002
Geoff Langb1196682014-07-23 13:47:29 -04001003bool ValidateSamplerObjectParameter(gl::Context *context, GLenum pname)
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001004{
1005 switch (pname)
1006 {
1007 case GL_TEXTURE_MIN_FILTER:
1008 case GL_TEXTURE_MAG_FILTER:
1009 case GL_TEXTURE_WRAP_S:
1010 case GL_TEXTURE_WRAP_T:
1011 case GL_TEXTURE_WRAP_R:
1012 case GL_TEXTURE_MIN_LOD:
1013 case GL_TEXTURE_MAX_LOD:
1014 case GL_TEXTURE_COMPARE_MODE:
1015 case GL_TEXTURE_COMPARE_FUNC:
1016 return true;
1017
1018 default:
Geoff Langb1196682014-07-23 13:47:29 -04001019 context->recordError(Error(GL_INVALID_ENUM));
1020 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001021 }
1022}
1023
Jamie Madill26e91952014-03-05 15:01:27 -05001024bool ValidateReadPixelsParameters(gl::Context *context, GLint x, GLint y, GLsizei width, GLsizei height,
1025 GLenum format, GLenum type, GLsizei *bufSize, GLvoid *pixels)
1026{
Shannon Woods53a94a82014-06-24 15:20:36 -04001027 gl::Framebuffer *framebuffer = context->getState().getReadFramebuffer();
Jamie Madill893ab082014-05-16 16:56:10 -04001028 ASSERT(framebuffer);
Jamie Madill26e91952014-03-05 15:01:27 -05001029
Geoff Lang748f74e2014-12-01 11:25:34 -05001030 if (framebuffer->checkStatus(context->getData()) != GL_FRAMEBUFFER_COMPLETE)
Jamie Madill26e91952014-03-05 15:01:27 -05001031 {
Geoff Langb1196682014-07-23 13:47:29 -04001032 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
1033 return false;
Jamie Madill26e91952014-03-05 15:01:27 -05001034 }
1035
Jamie Madill48faf802014-11-06 15:27:22 -05001036 if (context->getState().getReadFramebuffer()->id() != 0 &&
1037 framebuffer->getSamples(context->getData()) != 0)
Jamie Madill26e91952014-03-05 15:01:27 -05001038 {
Geoff Langb1196682014-07-23 13:47:29 -04001039 context->recordError(Error(GL_INVALID_OPERATION));
1040 return false;
Jamie Madill26e91952014-03-05 15:01:27 -05001041 }
1042
Geoff Langbce529e2014-12-01 12:48:41 -05001043 const FramebufferAttachment *readBuffer = framebuffer->getReadColorbuffer();
1044 if (!readBuffer)
Jamie Madill893ab082014-05-16 16:56:10 -04001045 {
Geoff Langb1196682014-07-23 13:47:29 -04001046 context->recordError(Error(GL_INVALID_OPERATION));
1047 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001048 }
1049
Geoff Langbce529e2014-12-01 12:48:41 -05001050 GLenum currentFormat = framebuffer->getImplementationColorReadFormat();
1051 GLenum currentType = framebuffer->getImplementationColorReadType();
Geoff Langd8a22582014-12-17 15:28:23 -05001052 GLenum currentInternalFormat = readBuffer->getInternalFormat();
Geoff Lange4a492b2014-06-19 14:14:41 -04001053 GLuint clientVersion = context->getClientVersion();
Jamie Madill26e91952014-03-05 15:01:27 -05001054
Geoff Langbdc9b2f2014-04-16 14:41:54 -04001055 bool validReadFormat = (clientVersion < 3) ? ValidES2ReadFormatType(context, format, type) :
1056 ValidES3ReadFormatType(context, currentInternalFormat, format, type);
Jamie Madill26e91952014-03-05 15:01:27 -05001057
1058 if (!(currentFormat == format && currentType == type) && !validReadFormat)
1059 {
Geoff Langb1196682014-07-23 13:47:29 -04001060 context->recordError(Error(GL_INVALID_OPERATION));
1061 return false;
Jamie Madill26e91952014-03-05 15:01:27 -05001062 }
1063
Geoff Lang5d601382014-07-22 15:14:06 -04001064 GLenum sizedInternalFormat = GetSizedInternalFormat(format, type);
1065 const InternalFormat &sizedFormatInfo = GetInternalFormatInfo(sizedInternalFormat);
Jamie Madill26e91952014-03-05 15:01:27 -05001066
Jamie Madillc7473922015-10-14 14:33:19 +00001067 GLsizei outputPitch = sizedFormatInfo.computeRowPitch(type, width, context->getState().getPackAlignment(), 0);
Jamie Madill26e91952014-03-05 15:01:27 -05001068 // sized query sanity check
1069 if (bufSize)
1070 {
1071 int requiredSize = outputPitch * height;
1072 if (requiredSize > *bufSize)
1073 {
Geoff Langb1196682014-07-23 13:47:29 -04001074 context->recordError(Error(GL_INVALID_OPERATION));
1075 return false;
Jamie Madill26e91952014-03-05 15:01:27 -05001076 }
1077 }
1078
1079 return true;
1080}
1081
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001082bool ValidateBeginQuery(gl::Context *context, GLenum target, GLuint id)
1083{
1084 if (!ValidQueryType(context, target))
1085 {
Geoff Langb1196682014-07-23 13:47:29 -04001086 context->recordError(Error(GL_INVALID_ENUM));
1087 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001088 }
1089
1090 if (id == 0)
1091 {
Geoff Langb1196682014-07-23 13:47:29 -04001092 context->recordError(Error(GL_INVALID_OPERATION));
1093 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001094 }
1095
1096 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
1097 // of zero, if the active query object name for <target> is non-zero (for the
1098 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
1099 // the active query for either target is non-zero), if <id> is the name of an
1100 // existing query object whose type does not match <target>, or if <id> is the
1101 // active query object name for any query type, the error INVALID_OPERATION is
1102 // generated.
1103
1104 // Ensure no other queries are active
1105 // NOTE: If other queries than occlusion are supported, we will need to check
1106 // separately that:
1107 // a) The query ID passed is not the current active query for any target/type
1108 // b) There are no active queries for the requested target (and in the case
1109 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
1110 // no query may be active for either if glBeginQuery targets either.
Shannon Woods53a94a82014-06-24 15:20:36 -04001111 if (context->getState().isQueryActive())
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001112 {
Geoff Langb1196682014-07-23 13:47:29 -04001113 context->recordError(Error(GL_INVALID_OPERATION));
1114 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001115 }
1116
1117 Query *queryObject = context->getQuery(id, true, target);
1118
1119 // check that name was obtained with glGenQueries
1120 if (!queryObject)
1121 {
Geoff Langb1196682014-07-23 13:47:29 -04001122 context->recordError(Error(GL_INVALID_OPERATION));
1123 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001124 }
1125
1126 // check for type mismatch
1127 if (queryObject->getType() != target)
1128 {
Geoff Langb1196682014-07-23 13:47:29 -04001129 context->recordError(Error(GL_INVALID_OPERATION));
1130 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001131 }
1132
1133 return true;
1134}
1135
Jamie Madill45c785d2014-05-13 14:09:34 -04001136bool ValidateEndQuery(gl::Context *context, GLenum target)
1137{
1138 if (!ValidQueryType(context, target))
1139 {
Geoff Langb1196682014-07-23 13:47:29 -04001140 context->recordError(Error(GL_INVALID_ENUM));
1141 return false;
Jamie Madill45c785d2014-05-13 14:09:34 -04001142 }
1143
Shannon Woods53a94a82014-06-24 15:20:36 -04001144 const Query *queryObject = context->getState().getActiveQuery(target);
Jamie Madill45c785d2014-05-13 14:09:34 -04001145
1146 if (queryObject == NULL)
1147 {
Geoff Langb1196682014-07-23 13:47:29 -04001148 context->recordError(Error(GL_INVALID_OPERATION));
1149 return false;
Jamie Madill45c785d2014-05-13 14:09:34 -04001150 }
1151
Jamie Madill45c785d2014-05-13 14:09:34 -04001152 return true;
1153}
1154
Jamie Madill62d31cb2015-09-11 13:25:51 -04001155static bool ValidateUniformCommonBase(gl::Context *context,
1156 GLenum targetUniformType,
1157 GLint location,
1158 GLsizei count,
1159 const LinkedUniform **uniformOut)
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001160{
1161 if (count < 0)
1162 {
Geoff Langb1196682014-07-23 13:47:29 -04001163 context->recordError(Error(GL_INVALID_VALUE));
1164 return false;
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001165 }
1166
Geoff Lang7dd2e102014-11-10 15:19:26 -05001167 gl::Program *program = context->getState().getProgram();
1168 if (!program)
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001169 {
Geoff Langb1196682014-07-23 13:47:29 -04001170 context->recordError(Error(GL_INVALID_OPERATION));
1171 return false;
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001172 }
1173
1174 if (location == -1)
1175 {
1176 // Silently ignore the uniform command
1177 return false;
1178 }
1179
Geoff Lang7dd2e102014-11-10 15:19:26 -05001180 if (!program->isValidUniformLocation(location))
Jamie Madill36398922014-05-20 14:51:53 -04001181 {
Geoff Langb1196682014-07-23 13:47:29 -04001182 context->recordError(Error(GL_INVALID_OPERATION));
1183 return false;
Jamie Madill36398922014-05-20 14:51:53 -04001184 }
1185
Jamie Madill62d31cb2015-09-11 13:25:51 -04001186 const LinkedUniform &uniform = program->getUniformByLocation(location);
Jamie Madill36398922014-05-20 14:51:53 -04001187
1188 // attempting to write an array to a non-array uniform is an INVALID_OPERATION
Jamie Madill62d31cb2015-09-11 13:25:51 -04001189 if (!uniform.isArray() && count > 1)
Jamie Madill36398922014-05-20 14:51:53 -04001190 {
Geoff Langb1196682014-07-23 13:47:29 -04001191 context->recordError(Error(GL_INVALID_OPERATION));
1192 return false;
Jamie Madill36398922014-05-20 14:51:53 -04001193 }
1194
Jamie Madill62d31cb2015-09-11 13:25:51 -04001195 *uniformOut = &uniform;
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001196 return true;
1197}
1198
Jamie Madillaa981bd2014-05-20 10:55:55 -04001199bool ValidateUniform(gl::Context *context, GLenum uniformType, GLint location, GLsizei count)
1200{
1201 // Check for ES3 uniform entry points
Jamie Madillf2575982014-06-25 16:04:54 -04001202 if (VariableComponentType(uniformType) == GL_UNSIGNED_INT && context->getClientVersion() < 3)
Jamie Madillaa981bd2014-05-20 10:55:55 -04001203 {
Geoff Langb1196682014-07-23 13:47:29 -04001204 context->recordError(Error(GL_INVALID_OPERATION));
1205 return false;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001206 }
1207
Jamie Madill62d31cb2015-09-11 13:25:51 -04001208 const LinkedUniform *uniform = nullptr;
Jamie Madill36398922014-05-20 14:51:53 -04001209 if (!ValidateUniformCommonBase(context, uniformType, location, count, &uniform))
1210 {
1211 return false;
1212 }
1213
Jamie Madillf2575982014-06-25 16:04:54 -04001214 GLenum targetBoolType = VariableBoolVectorType(uniformType);
Geoff Lang2ec386b2014-12-03 14:44:38 -05001215 bool samplerUniformCheck = (IsSamplerType(uniform->type) && uniformType == GL_INT);
Jamie Madill36398922014-05-20 14:51:53 -04001216 if (!samplerUniformCheck && uniformType != uniform->type && targetBoolType != uniform->type)
1217 {
Geoff Langb1196682014-07-23 13:47:29 -04001218 context->recordError(Error(GL_INVALID_OPERATION));
1219 return false;
Jamie Madill36398922014-05-20 14:51:53 -04001220 }
1221
1222 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001223}
1224
1225bool ValidateUniformMatrix(gl::Context *context, GLenum matrixType, GLint location, GLsizei count,
1226 GLboolean transpose)
1227{
1228 // Check for ES3 uniform entry points
1229 int rows = VariableRowCount(matrixType);
1230 int cols = VariableColumnCount(matrixType);
1231 if (rows != cols && context->getClientVersion() < 3)
1232 {
Geoff Langb1196682014-07-23 13:47:29 -04001233 context->recordError(Error(GL_INVALID_OPERATION));
1234 return false;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001235 }
1236
1237 if (transpose != GL_FALSE && context->getClientVersion() < 3)
1238 {
Geoff Langb1196682014-07-23 13:47:29 -04001239 context->recordError(Error(GL_INVALID_VALUE));
1240 return false;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001241 }
1242
Jamie Madill62d31cb2015-09-11 13:25:51 -04001243 const LinkedUniform *uniform = nullptr;
Jamie Madill36398922014-05-20 14:51:53 -04001244 if (!ValidateUniformCommonBase(context, matrixType, location, count, &uniform))
1245 {
1246 return false;
1247 }
1248
1249 if (uniform->type != matrixType)
1250 {
Geoff Langb1196682014-07-23 13:47:29 -04001251 context->recordError(Error(GL_INVALID_OPERATION));
1252 return false;
Jamie Madill36398922014-05-20 14:51:53 -04001253 }
1254
1255 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001256}
1257
Jamie Madill893ab082014-05-16 16:56:10 -04001258bool ValidateStateQuery(gl::Context *context, GLenum pname, GLenum *nativeType, unsigned int *numParams)
1259{
1260 if (!context->getQueryParameterInfo(pname, nativeType, numParams))
1261 {
Geoff Langb1196682014-07-23 13:47:29 -04001262 context->recordError(Error(GL_INVALID_ENUM));
1263 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001264 }
1265
Jamie Madill0af26e12015-03-05 19:54:33 -05001266 const Caps &caps = context->getCaps();
1267
Jamie Madill893ab082014-05-16 16:56:10 -04001268 if (pname >= GL_DRAW_BUFFER0 && pname <= GL_DRAW_BUFFER15)
1269 {
1270 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0);
1271
Jamie Madill0af26e12015-03-05 19:54:33 -05001272 if (colorAttachment >= caps.maxDrawBuffers)
Jamie Madill893ab082014-05-16 16:56:10 -04001273 {
Geoff Langb1196682014-07-23 13:47:29 -04001274 context->recordError(Error(GL_INVALID_OPERATION));
1275 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001276 }
1277 }
1278
1279 switch (pname)
1280 {
1281 case GL_TEXTURE_BINDING_2D:
1282 case GL_TEXTURE_BINDING_CUBE_MAP:
1283 case GL_TEXTURE_BINDING_3D:
1284 case GL_TEXTURE_BINDING_2D_ARRAY:
Jamie Madill0af26e12015-03-05 19:54:33 -05001285 if (context->getState().getActiveSampler() >= caps.maxCombinedTextureImageUnits)
Jamie Madill893ab082014-05-16 16:56:10 -04001286 {
Geoff Langb1196682014-07-23 13:47:29 -04001287 context->recordError(Error(GL_INVALID_OPERATION));
1288 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001289 }
1290 break;
1291
1292 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1293 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1294 {
Shannon Woods53a94a82014-06-24 15:20:36 -04001295 Framebuffer *framebuffer = context->getState().getReadFramebuffer();
Jamie Madill893ab082014-05-16 16:56:10 -04001296 ASSERT(framebuffer);
Geoff Lang748f74e2014-12-01 11:25:34 -05001297 if (framebuffer->checkStatus(context->getData()) != GL_FRAMEBUFFER_COMPLETE)
Jamie Madill893ab082014-05-16 16:56:10 -04001298 {
Geoff Langb1196682014-07-23 13:47:29 -04001299 context->recordError(Error(GL_INVALID_OPERATION));
1300 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001301 }
1302
Jamie Madillb6bda4a2015-04-20 12:53:26 -04001303 const FramebufferAttachment *attachment = framebuffer->getReadColorbuffer();
Jamie Madill3c7fa222014-06-05 13:08:51 -04001304 if (!attachment)
Jamie Madill893ab082014-05-16 16:56:10 -04001305 {
Geoff Langb1196682014-07-23 13:47:29 -04001306 context->recordError(Error(GL_INVALID_OPERATION));
1307 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001308 }
1309 }
1310 break;
1311
1312 default:
1313 break;
1314 }
1315
1316 // pname is valid, but there are no parameters to return
1317 if (numParams == 0)
1318 {
1319 return false;
1320 }
1321
1322 return true;
1323}
1324
Geoff Lang831b1952015-05-05 11:02:27 -04001325bool ValidateCopyTexImageParametersBase(gl::Context *context, GLenum target, GLint level, GLenum internalformat, bool isSubImage,
Jamie Madill560a8d82014-05-21 13:06:20 -04001326 GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height,
1327 GLint border, GLenum *textureFormatOut)
1328{
1329
1330 if (!ValidTexture2DDestinationTarget(context, target))
1331 {
Geoff Langb1196682014-07-23 13:47:29 -04001332 context->recordError(Error(GL_INVALID_ENUM));
1333 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001334 }
1335
1336 if (level < 0 || xoffset < 0 || yoffset < 0 || zoffset < 0 || width < 0 || height < 0)
1337 {
Geoff Langb1196682014-07-23 13:47:29 -04001338 context->recordError(Error(GL_INVALID_VALUE));
1339 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001340 }
1341
1342 if (std::numeric_limits<GLsizei>::max() - xoffset < width || std::numeric_limits<GLsizei>::max() - yoffset < height)
1343 {
Geoff Langb1196682014-07-23 13:47:29 -04001344 context->recordError(Error(GL_INVALID_VALUE));
1345 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001346 }
1347
1348 if (border != 0)
1349 {
Geoff Langb1196682014-07-23 13:47:29 -04001350 context->recordError(Error(GL_INVALID_VALUE));
1351 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001352 }
1353
1354 if (!ValidMipLevel(context, target, level))
1355 {
Geoff Langb1196682014-07-23 13:47:29 -04001356 context->recordError(Error(GL_INVALID_VALUE));
1357 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001358 }
1359
Shannon Woods53a94a82014-06-24 15:20:36 -04001360 gl::Framebuffer *framebuffer = context->getState().getReadFramebuffer();
Geoff Lang748f74e2014-12-01 11:25:34 -05001361 if (framebuffer->checkStatus(context->getData()) != GL_FRAMEBUFFER_COMPLETE)
Jamie Madill560a8d82014-05-21 13:06:20 -04001362 {
Geoff Langb1196682014-07-23 13:47:29 -04001363 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
1364 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001365 }
1366
Jamie Madill48faf802014-11-06 15:27:22 -05001367 if (context->getState().getReadFramebuffer()->id() != 0 && framebuffer->getSamples(context->getData()) != 0)
Jamie Madill560a8d82014-05-21 13:06:20 -04001368 {
Geoff Langb1196682014-07-23 13:47:29 -04001369 context->recordError(Error(GL_INVALID_OPERATION));
1370 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001371 }
1372
Geoff Langaae65a42014-05-26 12:43:44 -04001373 const gl::Caps &caps = context->getCaps();
1374
Geoff Langaae65a42014-05-26 12:43:44 -04001375 GLuint maxDimension = 0;
Jamie Madill560a8d82014-05-21 13:06:20 -04001376 switch (target)
1377 {
1378 case GL_TEXTURE_2D:
Geoff Langa9be0dc2014-12-17 12:34:40 -05001379 maxDimension = caps.max2DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001380 break;
1381
1382 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1383 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1384 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1385 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1386 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1387 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
Geoff Langa9be0dc2014-12-17 12:34:40 -05001388 maxDimension = caps.maxCubeMapTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001389 break;
1390
1391 case GL_TEXTURE_2D_ARRAY:
Geoff Langa9be0dc2014-12-17 12:34:40 -05001392 maxDimension = caps.max2DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001393 break;
1394
1395 case GL_TEXTURE_3D:
Geoff Langa9be0dc2014-12-17 12:34:40 -05001396 maxDimension = caps.max3DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001397 break;
1398
1399 default:
Geoff Langb1196682014-07-23 13:47:29 -04001400 context->recordError(Error(GL_INVALID_ENUM));
1401 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001402 }
1403
Geoff Lang691e58c2014-12-19 17:03:25 -05001404 gl::Texture *texture = context->getTargetTexture(IsCubeMapTextureTarget(target) ? GL_TEXTURE_CUBE_MAP : target);
Jamie Madill560a8d82014-05-21 13:06:20 -04001405 if (!texture)
1406 {
Geoff Langb1196682014-07-23 13:47:29 -04001407 context->recordError(Error(GL_INVALID_OPERATION));
1408 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001409 }
1410
Geoff Lang69cce582015-09-17 13:20:36 -04001411 if (texture->getImmutableFormat() && !isSubImage)
Jamie Madill560a8d82014-05-21 13:06:20 -04001412 {
Geoff Langb1196682014-07-23 13:47:29 -04001413 context->recordError(Error(GL_INVALID_OPERATION));
1414 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001415 }
1416
Geoff Lang5d601382014-07-22 15:14:06 -04001417 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalformat);
1418
1419 if (formatInfo.depthBits > 0)
Jamie Madill560a8d82014-05-21 13:06:20 -04001420 {
Geoff Langb1196682014-07-23 13:47:29 -04001421 context->recordError(Error(GL_INVALID_OPERATION));
1422 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001423 }
1424
Geoff Langa9be0dc2014-12-17 12:34:40 -05001425 if (formatInfo.compressed && !ValidCompressedImageSize(context, internalformat, width, height))
Jamie Madill560a8d82014-05-21 13:06:20 -04001426 {
Geoff Langa9be0dc2014-12-17 12:34:40 -05001427 context->recordError(Error(GL_INVALID_OPERATION));
1428 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001429 }
1430
1431 if (isSubImage)
1432 {
Geoff Langa9be0dc2014-12-17 12:34:40 -05001433 if (static_cast<size_t>(xoffset + width) > texture->getWidth(target, level) ||
1434 static_cast<size_t>(yoffset + height) > texture->getHeight(target, level) ||
1435 static_cast<size_t>(zoffset) >= texture->getDepth(target, level))
Jamie Madill560a8d82014-05-21 13:06:20 -04001436 {
Geoff Langb1196682014-07-23 13:47:29 -04001437 context->recordError(Error(GL_INVALID_VALUE));
1438 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001439 }
1440 }
Jamie Madill6f38f822014-06-06 17:12:20 -04001441 else
1442 {
Geoff Lang691e58c2014-12-19 17:03:25 -05001443 if (IsCubeMapTextureTarget(target) && width != height)
Jamie Madill6f38f822014-06-06 17:12:20 -04001444 {
Geoff Langb1196682014-07-23 13:47:29 -04001445 context->recordError(Error(GL_INVALID_VALUE));
1446 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04001447 }
1448
Geoff Lang5d601382014-07-22 15:14:06 -04001449 if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions()))
Jamie Madill6f38f822014-06-06 17:12:20 -04001450 {
Geoff Langb1196682014-07-23 13:47:29 -04001451 context->recordError(Error(GL_INVALID_ENUM));
1452 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04001453 }
1454
1455 int maxLevelDimension = (maxDimension >> level);
1456 if (static_cast<int>(width) > maxLevelDimension || static_cast<int>(height) > maxLevelDimension)
1457 {
Geoff Langb1196682014-07-23 13:47:29 -04001458 context->recordError(Error(GL_INVALID_VALUE));
1459 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04001460 }
1461 }
Jamie Madill560a8d82014-05-21 13:06:20 -04001462
Geoff Langa9be0dc2014-12-17 12:34:40 -05001463 *textureFormatOut = texture->getInternalFormat(target, level);
Jamie Madill560a8d82014-05-21 13:06:20 -04001464 return true;
1465}
1466
Corentin Wallez18a2fb32015-08-10 12:58:14 -07001467static bool ValidateDrawBase(Context *context, GLenum mode, GLsizei count, GLsizei primcount)
Jamie Madill250d33f2014-06-06 17:09:03 -04001468{
Jamie Madill1aeb1312014-06-20 13:21:25 -04001469 switch (mode)
1470 {
1471 case GL_POINTS:
1472 case GL_LINES:
1473 case GL_LINE_LOOP:
1474 case GL_LINE_STRIP:
1475 case GL_TRIANGLES:
1476 case GL_TRIANGLE_STRIP:
1477 case GL_TRIANGLE_FAN:
1478 break;
1479 default:
Geoff Langb1196682014-07-23 13:47:29 -04001480 context->recordError(Error(GL_INVALID_ENUM));
1481 return false;
Jamie Madill1aeb1312014-06-20 13:21:25 -04001482 }
1483
Jamie Madill250d33f2014-06-06 17:09:03 -04001484 if (count < 0)
1485 {
Geoff Langb1196682014-07-23 13:47:29 -04001486 context->recordError(Error(GL_INVALID_VALUE));
1487 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001488 }
1489
Geoff Langb1196682014-07-23 13:47:29 -04001490 const State &state = context->getState();
1491
Jamie Madill250d33f2014-06-06 17:09:03 -04001492 // Check for mapped buffers
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001493 if (state.hasMappedBuffer(GL_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001494 {
Geoff Langb1196682014-07-23 13:47:29 -04001495 context->recordError(Error(GL_INVALID_OPERATION));
1496 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001497 }
1498
Geoff Lang3a86ad32015-09-01 11:47:05 -04001499 if (context->getLimitations().noSeparateStencilRefsAndMasks)
Jamie Madillac528012014-06-20 13:21:23 -04001500 {
Geoff Lang3a86ad32015-09-01 11:47:05 -04001501 const gl::DepthStencilState &depthStencilState = state.getDepthStencilState();
1502 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
1503 state.getStencilRef() != state.getStencilBackRef() ||
1504 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
1505 {
1506 // Note: these separate values are not supported in WebGL, due to D3D's limitations. See
1507 // Section 6.10 of the WebGL 1.0 spec
1508 ERR(
1509 "This ANGLE implementation does not support separate front/back stencil "
1510 "writemasks, reference values, or stencil mask values.");
1511 context->recordError(Error(GL_INVALID_OPERATION));
1512 return false;
1513 }
Jamie Madillac528012014-06-20 13:21:23 -04001514 }
1515
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001516 const gl::Framebuffer *fbo = state.getDrawFramebuffer();
Geoff Lang748f74e2014-12-01 11:25:34 -05001517 if (!fbo || fbo->checkStatus(context->getData()) != GL_FRAMEBUFFER_COMPLETE)
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001518 {
Geoff Langb1196682014-07-23 13:47:29 -04001519 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
1520 return false;
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001521 }
1522
Geoff Lang7dd2e102014-11-10 15:19:26 -05001523 gl::Program *program = state.getProgram();
1524 if (!program)
Jamie Madilld4cfa572014-07-08 10:00:32 -04001525 {
Geoff Langb1196682014-07-23 13:47:29 -04001526 context->recordError(Error(GL_INVALID_OPERATION));
1527 return false;
Jamie Madilld4cfa572014-07-08 10:00:32 -04001528 }
1529
Geoff Lang7dd2e102014-11-10 15:19:26 -05001530 if (!program->validateSamplers(NULL, context->getCaps()))
Jamie Madilld4cfa572014-07-08 10:00:32 -04001531 {
Geoff Langb1196682014-07-23 13:47:29 -04001532 context->recordError(Error(GL_INVALID_OPERATION));
1533 return false;
Jamie Madilld4cfa572014-07-08 10:00:32 -04001534 }
1535
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001536 // Uniform buffer validation
1537 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < program->getActiveUniformBlockCount(); uniformBlockIndex++)
1538 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001539 const gl::UniformBlock &uniformBlock = program->getUniformBlockByIndex(uniformBlockIndex);
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001540 GLuint blockBinding = program->getUniformBlockBinding(uniformBlockIndex);
Geoff Lang5d124a62015-09-15 13:03:27 -04001541 const OffsetBindingPointer<Buffer> &uniformBuffer =
1542 state.getIndexedUniformBuffer(blockBinding);
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001543
Geoff Lang5d124a62015-09-15 13:03:27 -04001544 if (uniformBuffer.get() == nullptr)
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001545 {
1546 // undefined behaviour
1547 context->recordError(Error(GL_INVALID_OPERATION, "It is undefined behaviour to have a used but unbound uniform buffer."));
1548 return false;
1549 }
1550
Geoff Lang5d124a62015-09-15 13:03:27 -04001551 size_t uniformBufferSize = uniformBuffer.getSize();
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001552 if (uniformBufferSize == 0)
1553 {
1554 // Bind the whole buffer.
Minmin Gong794e0002015-04-07 18:31:54 -07001555 uniformBufferSize = static_cast<size_t>(uniformBuffer->getSize());
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001556 }
1557
Jamie Madill62d31cb2015-09-11 13:25:51 -04001558 if (uniformBufferSize < uniformBlock.dataSize)
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001559 {
1560 // undefined behaviour
1561 context->recordError(Error(GL_INVALID_OPERATION, "It is undefined behaviour to use a uniform buffer that is too small."));
1562 return false;
1563 }
1564 }
1565
Jamie Madill250d33f2014-06-06 17:09:03 -04001566 // No-op if zero count
1567 return (count > 0);
1568}
1569
Geoff Langb1196682014-07-23 13:47:29 -04001570bool ValidateDrawArrays(Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
Jamie Madill250d33f2014-06-06 17:09:03 -04001571{
Jamie Madillfd716582014-06-06 17:09:04 -04001572 if (first < 0)
Jamie Madill250d33f2014-06-06 17:09:03 -04001573 {
Geoff Langb1196682014-07-23 13:47:29 -04001574 context->recordError(Error(GL_INVALID_VALUE));
1575 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001576 }
1577
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001578 const State &state = context->getState();
1579 gl::TransformFeedback *curTransformFeedback = state.getCurrentTransformFeedback();
Geoff Langbb0a0bb2015-03-27 12:16:57 -04001580 if (curTransformFeedback && curTransformFeedback->isActive() && !curTransformFeedback->isPaused() &&
1581 curTransformFeedback->getPrimitiveMode() != mode)
Jamie Madillfd716582014-06-06 17:09:04 -04001582 {
1583 // It is an invalid operation to call DrawArrays or DrawArraysInstanced with a draw mode
1584 // that does not match the current transform feedback object's draw mode (if transform feedback
1585 // is active), (3.0.2, section 2.14, pg 86)
Geoff Langb1196682014-07-23 13:47:29 -04001586 context->recordError(Error(GL_INVALID_OPERATION));
1587 return false;
Jamie Madillfd716582014-06-06 17:09:04 -04001588 }
1589
Corentin Wallez18a2fb32015-08-10 12:58:14 -07001590 if (!ValidateDrawBase(context, mode, count, primcount))
1591 {
1592 return false;
1593 }
1594
1595 if (!ValidateDrawAttribs(context, primcount, count))
Jamie Madillfd716582014-06-06 17:09:04 -04001596 {
1597 return false;
1598 }
1599
1600 return true;
1601}
1602
Geoff Langb1196682014-07-23 13:47:29 -04001603bool ValidateDrawArraysInstanced(Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
Jamie Madillfd716582014-06-06 17:09:04 -04001604{
1605 if (primcount < 0)
1606 {
Geoff Langb1196682014-07-23 13:47:29 -04001607 context->recordError(Error(GL_INVALID_VALUE));
1608 return false;
Jamie Madillfd716582014-06-06 17:09:04 -04001609 }
1610
Jamie Madill2b976812014-08-25 15:47:49 -04001611 if (!ValidateDrawArrays(context, mode, first, count, primcount))
Jamie Madillfd716582014-06-06 17:09:04 -04001612 {
1613 return false;
1614 }
1615
1616 // No-op if zero primitive count
1617 return (primcount > 0);
1618}
1619
Geoff Lang87a93302014-09-16 13:29:43 -04001620static bool ValidateDrawInstancedANGLE(Context *context)
1621{
1622 // Verify there is at least one active attribute with a divisor of zero
1623 const gl::State& state = context->getState();
1624
Geoff Lang7dd2e102014-11-10 15:19:26 -05001625 gl::Program *program = state.getProgram();
Geoff Lang87a93302014-09-16 13:29:43 -04001626
1627 const VertexArray *vao = state.getVertexArray();
Jamie Madill63805b42015-08-25 13:17:39 -04001628 for (size_t attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
Geoff Lang87a93302014-09-16 13:29:43 -04001629 {
1630 const VertexAttribute &attrib = vao->getVertexAttribute(attributeIndex);
Jamie Madill63805b42015-08-25 13:17:39 -04001631 if (program->isAttribLocationActive(attributeIndex) && attrib.divisor == 0)
Geoff Lang87a93302014-09-16 13:29:43 -04001632 {
1633 return true;
1634 }
1635 }
1636
1637 context->recordError(Error(GL_INVALID_OPERATION, "ANGLE_instanced_arrays requires that at least one active attribute"
1638 "has a divisor of zero."));
1639 return false;
1640}
1641
1642bool ValidateDrawArraysInstancedANGLE(Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
1643{
1644 if (!ValidateDrawInstancedANGLE(context))
1645 {
1646 return false;
1647 }
1648
1649 return ValidateDrawArraysInstanced(context, mode, first, count, primcount);
1650}
1651
Geoff Lang3edfe032015-09-04 16:38:24 -04001652bool ValidateDrawElements(Context *context,
1653 GLenum mode,
1654 GLsizei count,
1655 GLenum type,
1656 const GLvoid *indices,
1657 GLsizei primcount,
1658 IndexRange *indexRangeOut)
Jamie Madillfd716582014-06-06 17:09:04 -04001659{
Jamie Madill250d33f2014-06-06 17:09:03 -04001660 switch (type)
1661 {
1662 case GL_UNSIGNED_BYTE:
1663 case GL_UNSIGNED_SHORT:
1664 break;
1665 case GL_UNSIGNED_INT:
Geoff Langc0b9ef42014-07-02 10:02:37 -04001666 if (!context->getExtensions().elementIndexUint)
Jamie Madill250d33f2014-06-06 17:09:03 -04001667 {
Geoff Langb1196682014-07-23 13:47:29 -04001668 context->recordError(Error(GL_INVALID_ENUM));
1669 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001670 }
1671 break;
1672 default:
Geoff Langb1196682014-07-23 13:47:29 -04001673 context->recordError(Error(GL_INVALID_ENUM));
1674 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001675 }
1676
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001677 const State &state = context->getState();
1678
1679 gl::TransformFeedback *curTransformFeedback = state.getCurrentTransformFeedback();
Geoff Langbb0a0bb2015-03-27 12:16:57 -04001680 if (curTransformFeedback && curTransformFeedback->isActive() && !curTransformFeedback->isPaused())
Jamie Madill250d33f2014-06-06 17:09:03 -04001681 {
1682 // It is an invalid operation to call DrawElements, DrawRangeElements or DrawElementsInstanced
1683 // while transform feedback is active, (3.0.2, section 2.14, pg 86)
Geoff Langb1196682014-07-23 13:47:29 -04001684 context->recordError(Error(GL_INVALID_OPERATION));
1685 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001686 }
1687
1688 // Check for mapped buffers
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001689 if (state.hasMappedBuffer(GL_ELEMENT_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001690 {
Geoff Langb1196682014-07-23 13:47:29 -04001691 context->recordError(Error(GL_INVALID_OPERATION));
1692 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001693 }
1694
Jamie Madill2b976812014-08-25 15:47:49 -04001695 const gl::VertexArray *vao = state.getVertexArray();
Jamie Madill8e344942015-07-09 14:22:07 -04001696 gl::Buffer *elementArrayBuffer = vao->getElementArrayBuffer().get();
Jamie Madill2b976812014-08-25 15:47:49 -04001697 if (!indices && !elementArrayBuffer)
Jamie Madilld4cfa572014-07-08 10:00:32 -04001698 {
Geoff Langb1196682014-07-23 13:47:29 -04001699 context->recordError(Error(GL_INVALID_OPERATION));
1700 return false;
Jamie Madilld4cfa572014-07-08 10:00:32 -04001701 }
1702
Jamie Madillae3000b2014-08-25 15:47:51 -04001703 if (elementArrayBuffer)
1704 {
1705 const gl::Type &typeInfo = gl::GetTypeInfo(type);
1706
1707 GLint64 offset = reinterpret_cast<GLint64>(indices);
1708 GLint64 byteCount = static_cast<GLint64>(typeInfo.bytes) * static_cast<GLint64>(count)+offset;
1709
1710 // check for integer overflows
1711 if (static_cast<GLuint>(count) > (std::numeric_limits<GLuint>::max() / typeInfo.bytes) ||
1712 byteCount > static_cast<GLint64>(std::numeric_limits<GLuint>::max()))
1713 {
Geoff Langb1196682014-07-23 13:47:29 -04001714 context->recordError(Error(GL_OUT_OF_MEMORY));
1715 return false;
Jamie Madillae3000b2014-08-25 15:47:51 -04001716 }
1717
1718 // Check for reading past the end of the bound buffer object
1719 if (byteCount > elementArrayBuffer->getSize())
1720 {
Geoff Langb1196682014-07-23 13:47:29 -04001721 context->recordError(Error(GL_INVALID_OPERATION));
1722 return false;
Jamie Madillae3000b2014-08-25 15:47:51 -04001723 }
1724 }
1725 else if (!indices)
1726 {
1727 // Catch this programming error here
Geoff Langb1196682014-07-23 13:47:29 -04001728 context->recordError(Error(GL_INVALID_OPERATION));
1729 return false;
Jamie Madillae3000b2014-08-25 15:47:51 -04001730 }
1731
Corentin Wallez18a2fb32015-08-10 12:58:14 -07001732 if (!ValidateDrawBase(context, mode, count, primcount))
1733 {
1734 return false;
1735 }
1736
Jamie Madill2b976812014-08-25 15:47:49 -04001737 // Use max index to validate if our vertex buffers are large enough for the pull.
1738 // TODO: offer fast path, with disabled index validation.
1739 // TODO: also disable index checking on back-ends that are robust to out-of-range accesses.
1740 if (elementArrayBuffer)
1741 {
Jacek Cabana5521de2014-10-01 17:23:46 +02001742 uintptr_t offset = reinterpret_cast<uintptr_t>(indices);
Geoff Lang3edfe032015-09-04 16:38:24 -04001743 Error error =
1744 elementArrayBuffer->getIndexRange(type, static_cast<size_t>(offset), count,
1745 state.isPrimitiveRestartEnabled(), indexRangeOut);
Geoff Lang520c4ae2015-05-05 13:12:36 -04001746 if (error.isError())
Jamie Madill2b976812014-08-25 15:47:49 -04001747 {
Geoff Lang520c4ae2015-05-05 13:12:36 -04001748 context->recordError(error);
1749 return false;
Jamie Madill2b976812014-08-25 15:47:49 -04001750 }
1751 }
1752 else
1753 {
Geoff Lang3edfe032015-09-04 16:38:24 -04001754 *indexRangeOut = ComputeIndexRange(type, indices, count, state.isPrimitiveRestartEnabled());
Jamie Madill2b976812014-08-25 15:47:49 -04001755 }
1756
Corentin Wallez18a2fb32015-08-10 12:58:14 -07001757 if (!ValidateDrawAttribs(context, primcount, static_cast<GLsizei>(indexRangeOut->end)))
Jamie Madillfd716582014-06-06 17:09:04 -04001758 {
1759 return false;
1760 }
1761
Geoff Lang3edfe032015-09-04 16:38:24 -04001762 // No op if there are no real indices in the index data (all are primitive restart).
1763 return (indexRangeOut->vertexIndexCount > 0);
Jamie Madillfd716582014-06-06 17:09:04 -04001764}
1765
Geoff Langb1196682014-07-23 13:47:29 -04001766bool ValidateDrawElementsInstanced(Context *context,
Geoff Lang3edfe032015-09-04 16:38:24 -04001767 GLenum mode,
1768 GLsizei count,
1769 GLenum type,
1770 const GLvoid *indices,
1771 GLsizei primcount,
1772 IndexRange *indexRangeOut)
Jamie Madillfd716582014-06-06 17:09:04 -04001773{
1774 if (primcount < 0)
1775 {
Geoff Langb1196682014-07-23 13:47:29 -04001776 context->recordError(Error(GL_INVALID_VALUE));
1777 return false;
Jamie Madillfd716582014-06-06 17:09:04 -04001778 }
1779
Jamie Madill2b976812014-08-25 15:47:49 -04001780 if (!ValidateDrawElements(context, mode, count, type, indices, primcount, indexRangeOut))
Jamie Madillfd716582014-06-06 17:09:04 -04001781 {
1782 return false;
1783 }
1784
1785 // No-op zero primitive count
1786 return (primcount > 0);
Jamie Madill250d33f2014-06-06 17:09:03 -04001787}
1788
Geoff Lang3edfe032015-09-04 16:38:24 -04001789bool ValidateDrawElementsInstancedANGLE(Context *context,
1790 GLenum mode,
1791 GLsizei count,
1792 GLenum type,
1793 const GLvoid *indices,
1794 GLsizei primcount,
1795 IndexRange *indexRangeOut)
Geoff Lang87a93302014-09-16 13:29:43 -04001796{
1797 if (!ValidateDrawInstancedANGLE(context))
1798 {
1799 return false;
1800 }
1801
1802 return ValidateDrawElementsInstanced(context, mode, count, type, indices, primcount, indexRangeOut);
1803}
1804
Geoff Langb1196682014-07-23 13:47:29 -04001805bool ValidateFramebufferTextureBase(Context *context, GLenum target, GLenum attachment,
Jamie Madill55ec3b12014-07-03 10:38:57 -04001806 GLuint texture, GLint level)
Jamie Madill570f7c82014-07-03 10:38:54 -04001807{
Jamie Madill55ec3b12014-07-03 10:38:57 -04001808 if (!ValidFramebufferTarget(target))
1809 {
Geoff Langb1196682014-07-23 13:47:29 -04001810 context->recordError(Error(GL_INVALID_ENUM));
1811 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001812 }
1813
1814 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill570f7c82014-07-03 10:38:54 -04001815 {
1816 return false;
1817 }
1818
Jamie Madill55ec3b12014-07-03 10:38:57 -04001819 if (texture != 0)
1820 {
1821 gl::Texture *tex = context->getTexture(texture);
1822
1823 if (tex == NULL)
1824 {
Geoff Langb1196682014-07-23 13:47:29 -04001825 context->recordError(Error(GL_INVALID_OPERATION));
1826 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001827 }
1828
1829 if (level < 0)
1830 {
Geoff Langb1196682014-07-23 13:47:29 -04001831 context->recordError(Error(GL_INVALID_VALUE));
1832 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001833 }
1834 }
1835
Shannon Woods53a94a82014-06-24 15:20:36 -04001836 const gl::Framebuffer *framebuffer = context->getState().getTargetFramebuffer(target);
Jamie Madill84115c92015-04-23 15:00:07 -04001837 ASSERT(framebuffer);
Jamie Madill55ec3b12014-07-03 10:38:57 -04001838
Jamie Madill84115c92015-04-23 15:00:07 -04001839 if (framebuffer->id() == 0)
Jamie Madill55ec3b12014-07-03 10:38:57 -04001840 {
Jamie Madill84115c92015-04-23 15:00:07 -04001841 context->recordError(Error(GL_INVALID_OPERATION, "Cannot change default FBO's attachments"));
Geoff Langb1196682014-07-23 13:47:29 -04001842 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001843 }
1844
1845 return true;
1846}
1847
Geoff Langb1196682014-07-23 13:47:29 -04001848bool ValidateFramebufferTexture2D(Context *context, GLenum target, GLenum attachment,
Jamie Madill55ec3b12014-07-03 10:38:57 -04001849 GLenum textarget, GLuint texture, GLint level)
1850{
Geoff Lang95663912015-04-02 15:54:45 -04001851 // Attachments are required to be bound to level 0 without ES3 or the GL_OES_fbo_render_mipmap extension
1852 if (context->getClientVersion() < 3 && !context->getExtensions().fboRenderMipmap && level != 0)
Jamie Madill55ec3b12014-07-03 10:38:57 -04001853 {
Geoff Langb1196682014-07-23 13:47:29 -04001854 context->recordError(Error(GL_INVALID_VALUE));
1855 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001856 }
1857
1858 if (!ValidateFramebufferTextureBase(context, target, attachment, texture, level))
Jamie Madill570f7c82014-07-03 10:38:54 -04001859 {
1860 return false;
1861 }
1862
Jamie Madill55ec3b12014-07-03 10:38:57 -04001863 if (texture != 0)
1864 {
1865 gl::Texture *tex = context->getTexture(texture);
1866 ASSERT(tex);
1867
Jamie Madill2a6564e2014-07-11 09:53:19 -04001868 const gl::Caps &caps = context->getCaps();
1869
Jamie Madill55ec3b12014-07-03 10:38:57 -04001870 switch (textarget)
1871 {
1872 case GL_TEXTURE_2D:
1873 {
Jamie Madill2a6564e2014-07-11 09:53:19 -04001874 if (level > gl::log2(caps.max2DTextureSize))
Jamie Madill55ec3b12014-07-03 10:38:57 -04001875 {
Geoff Langb1196682014-07-23 13:47:29 -04001876 context->recordError(Error(GL_INVALID_VALUE));
1877 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001878 }
1879 if (tex->getTarget() != GL_TEXTURE_2D)
1880 {
Geoff Langb1196682014-07-23 13:47:29 -04001881 context->recordError(Error(GL_INVALID_OPERATION));
1882 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001883 }
Jamie Madill55ec3b12014-07-03 10:38:57 -04001884 }
1885 break;
1886
1887 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1888 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1889 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1890 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1891 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1892 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1893 {
Jamie Madill2a6564e2014-07-11 09:53:19 -04001894 if (level > gl::log2(caps.maxCubeMapTextureSize))
Jamie Madill55ec3b12014-07-03 10:38:57 -04001895 {
Geoff Langb1196682014-07-23 13:47:29 -04001896 context->recordError(Error(GL_INVALID_VALUE));
1897 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001898 }
1899 if (tex->getTarget() != GL_TEXTURE_CUBE_MAP)
1900 {
Geoff Langb1196682014-07-23 13:47:29 -04001901 context->recordError(Error(GL_INVALID_OPERATION));
1902 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001903 }
Jamie Madill55ec3b12014-07-03 10:38:57 -04001904 }
1905 break;
1906
1907 default:
Geoff Langb1196682014-07-23 13:47:29 -04001908 context->recordError(Error(GL_INVALID_ENUM));
1909 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001910 }
Geoff Langa9be0dc2014-12-17 12:34:40 -05001911
1912 const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(tex->getInternalFormat(textarget, level));
1913 if (internalFormatInfo.compressed)
1914 {
1915 context->recordError(Error(GL_INVALID_OPERATION));
1916 return false;
1917 }
Jamie Madill55ec3b12014-07-03 10:38:57 -04001918 }
1919
Jamie Madill570f7c82014-07-03 10:38:54 -04001920 return true;
1921}
1922
Geoff Langb1196682014-07-23 13:47:29 -04001923bool ValidateGetUniformBase(Context *context, GLuint program, GLint location)
Jamie Madill0063c512014-08-25 15:47:53 -04001924{
1925 if (program == 0)
1926 {
Geoff Langb1196682014-07-23 13:47:29 -04001927 context->recordError(Error(GL_INVALID_VALUE));
1928 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001929 }
1930
Dian Xiang769769a2015-09-09 15:20:08 -07001931 gl::Program *programObject = GetValidProgram(context, program);
1932 if (!programObject)
Shannon Woods4de4fd62014-11-07 16:22:02 -05001933 {
1934 return false;
1935 }
1936
Jamie Madill0063c512014-08-25 15:47:53 -04001937 if (!programObject || !programObject->isLinked())
1938 {
Geoff Langb1196682014-07-23 13:47:29 -04001939 context->recordError(Error(GL_INVALID_OPERATION));
1940 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001941 }
1942
Geoff Lang7dd2e102014-11-10 15:19:26 -05001943 if (!programObject->isValidUniformLocation(location))
Jamie Madill549c7fd2014-08-25 15:47:56 -04001944 {
Geoff Langb1196682014-07-23 13:47:29 -04001945 context->recordError(Error(GL_INVALID_OPERATION));
1946 return false;
Jamie Madill549c7fd2014-08-25 15:47:56 -04001947 }
1948
Jamie Madill0063c512014-08-25 15:47:53 -04001949 return true;
1950}
1951
Geoff Langb1196682014-07-23 13:47:29 -04001952bool ValidateGetUniformfv(Context *context, GLuint program, GLint location, GLfloat* params)
Jamie Madill78f41802014-08-25 15:47:55 -04001953{
1954 return ValidateGetUniformBase(context, program, location);
1955}
1956
Geoff Langb1196682014-07-23 13:47:29 -04001957bool ValidateGetUniformiv(Context *context, GLuint program, GLint location, GLint* params)
Jamie Madill0063c512014-08-25 15:47:53 -04001958{
Jamie Madill78f41802014-08-25 15:47:55 -04001959 return ValidateGetUniformBase(context, program, location);
1960}
1961
Geoff Langb1196682014-07-23 13:47:29 -04001962static bool ValidateSizedGetUniform(Context *context, GLuint program, GLint location, GLsizei bufSize)
Jamie Madill78f41802014-08-25 15:47:55 -04001963{
1964 if (!ValidateGetUniformBase(context, program, location))
Jamie Madill0063c512014-08-25 15:47:53 -04001965 {
Jamie Madill78f41802014-08-25 15:47:55 -04001966 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001967 }
1968
Jamie Madilla502c742014-08-28 17:19:13 -04001969 gl::Program *programObject = context->getProgram(program);
1970 ASSERT(programObject);
Jamie Madill0063c512014-08-25 15:47:53 -04001971
Jamie Madill78f41802014-08-25 15:47:55 -04001972 // sized queries -- ensure the provided buffer is large enough
Jamie Madill62d31cb2015-09-11 13:25:51 -04001973 const LinkedUniform &uniform = programObject->getUniformByLocation(location);
1974 size_t requiredBytes = VariableExternalSize(uniform.type);
Jamie Madill78f41802014-08-25 15:47:55 -04001975 if (static_cast<size_t>(bufSize) < requiredBytes)
Jamie Madill0063c512014-08-25 15:47:53 -04001976 {
Geoff Langb1196682014-07-23 13:47:29 -04001977 context->recordError(Error(GL_INVALID_OPERATION));
1978 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001979 }
1980
1981 return true;
1982}
1983
Geoff Langb1196682014-07-23 13:47:29 -04001984bool ValidateGetnUniformfvEXT(Context *context, GLuint program, GLint location, GLsizei bufSize, GLfloat* params)
Jamie Madill0063c512014-08-25 15:47:53 -04001985{
Jamie Madill78f41802014-08-25 15:47:55 -04001986 return ValidateSizedGetUniform(context, program, location, bufSize);
Jamie Madill0063c512014-08-25 15:47:53 -04001987}
1988
Geoff Langb1196682014-07-23 13:47:29 -04001989bool ValidateGetnUniformivEXT(Context *context, GLuint program, GLint location, GLsizei bufSize, GLint* params)
Jamie Madill0063c512014-08-25 15:47:53 -04001990{
Jamie Madill78f41802014-08-25 15:47:55 -04001991 return ValidateSizedGetUniform(context, program, location, bufSize);
Jamie Madill0063c512014-08-25 15:47:53 -04001992}
1993
Austin Kinross08332632015-05-05 13:35:47 -07001994bool ValidateDiscardFramebufferBase(Context *context, GLenum target, GLsizei numAttachments,
1995 const GLenum *attachments, bool defaultFramebuffer)
1996{
1997 if (numAttachments < 0)
1998 {
1999 context->recordError(Error(GL_INVALID_VALUE, "numAttachments must not be less than zero"));
2000 return false;
2001 }
2002
2003 for (GLsizei i = 0; i < numAttachments; ++i)
2004 {
2005 if (attachments[i] >= GL_COLOR_ATTACHMENT0 && attachments[i] <= GL_COLOR_ATTACHMENT15)
2006 {
2007 if (defaultFramebuffer)
2008 {
2009 context->recordError(Error(GL_INVALID_ENUM, "Invalid attachment when the default framebuffer is bound"));
2010 return false;
2011 }
2012
2013 if (attachments[i] >= GL_COLOR_ATTACHMENT0 + context->getCaps().maxColorAttachments)
2014 {
2015 context->recordError(Error(GL_INVALID_OPERATION,
2016 "Requested color attachment is greater than the maximum supported color attachments"));
2017 return false;
2018 }
2019 }
2020 else
2021 {
2022 switch (attachments[i])
2023 {
2024 case GL_DEPTH_ATTACHMENT:
2025 case GL_STENCIL_ATTACHMENT:
2026 case GL_DEPTH_STENCIL_ATTACHMENT:
2027 if (defaultFramebuffer)
2028 {
2029 context->recordError(Error(GL_INVALID_ENUM, "Invalid attachment when the default framebuffer is bound"));
2030 return false;
2031 }
2032 break;
2033 case GL_COLOR:
2034 case GL_DEPTH:
2035 case GL_STENCIL:
2036 if (!defaultFramebuffer)
2037 {
2038 context->recordError(Error(GL_INVALID_ENUM, "Invalid attachment when the default framebuffer is not bound"));
2039 return false;
2040 }
2041 break;
2042 default:
2043 context->recordError(Error(GL_INVALID_ENUM, "Invalid attachment"));
2044 return false;
2045 }
2046 }
2047 }
2048
2049 return true;
2050}
2051
Austin Kinross6ee1e782015-05-29 17:05:37 -07002052bool ValidateInsertEventMarkerEXT(Context *context, GLsizei length, const char *marker)
2053{
2054 // Note that debug marker calls must not set error state
2055
2056 if (length < 0)
2057 {
2058 return false;
2059 }
2060
2061 if (marker == nullptr)
2062 {
2063 return false;
2064 }
2065
2066 return true;
2067}
2068
2069bool ValidatePushGroupMarkerEXT(Context *context, GLsizei length, const char *marker)
2070{
2071 // Note that debug marker calls must not set error state
2072
2073 if (length < 0)
2074 {
2075 return false;
2076 }
2077
2078 if (length > 0 && marker == nullptr)
2079 {
2080 return false;
2081 }
2082
2083 return true;
2084}
2085
Geoff Langdcab33b2015-07-21 13:03:16 -04002086bool ValidateEGLImageTargetTexture2DOES(Context *context,
2087 egl::Display *display,
2088 GLenum target,
2089 egl::Image *image)
2090{
Geoff Langa8406172015-07-21 16:53:39 -04002091 if (!context->getExtensions().eglImage && !context->getExtensions().eglImageExternal)
2092 {
2093 context->recordError(Error(GL_INVALID_OPERATION));
2094 return false;
2095 }
2096
2097 switch (target)
2098 {
2099 case GL_TEXTURE_2D:
2100 break;
2101
2102 default:
2103 context->recordError(Error(GL_INVALID_ENUM, "invalid texture target."));
2104 return false;
2105 }
2106
2107 if (!display->isValidImage(image))
2108 {
2109 context->recordError(Error(GL_INVALID_VALUE, "EGL image is not valid."));
2110 return false;
2111 }
2112
2113 if (image->getSamples() > 0)
2114 {
2115 context->recordError(Error(GL_INVALID_OPERATION,
2116 "cannot create a 2D texture from a multisampled EGL image."));
2117 return false;
2118 }
2119
2120 const TextureCaps &textureCaps = context->getTextureCaps().get(image->getInternalFormat());
2121 if (!textureCaps.texturable)
2122 {
2123 context->recordError(Error(GL_INVALID_OPERATION,
2124 "EGL image internal format is not supported as a texture."));
2125 return false;
2126 }
2127
Geoff Langdcab33b2015-07-21 13:03:16 -04002128 return true;
2129}
2130
2131bool ValidateEGLImageTargetRenderbufferStorageOES(Context *context,
2132 egl::Display *display,
2133 GLenum target,
2134 egl::Image *image)
2135{
Geoff Langa8406172015-07-21 16:53:39 -04002136 if (!context->getExtensions().eglImage)
2137 {
2138 context->recordError(Error(GL_INVALID_OPERATION));
2139 return false;
2140 }
2141
2142 switch (target)
2143 {
2144 case GL_RENDERBUFFER:
2145 break;
2146
2147 default:
2148 context->recordError(Error(GL_INVALID_ENUM, "invalid renderbuffer target."));
2149 return false;
2150 }
2151
2152 if (!display->isValidImage(image))
2153 {
2154 context->recordError(Error(GL_INVALID_VALUE, "EGL image is not valid."));
2155 return false;
2156 }
2157
2158 const TextureCaps &textureCaps = context->getTextureCaps().get(image->getInternalFormat());
2159 if (!textureCaps.renderable)
2160 {
2161 context->recordError(Error(
2162 GL_INVALID_OPERATION, "EGL image internal format is not supported as a renderbuffer."));
2163 return false;
2164 }
2165
Geoff Langdcab33b2015-07-21 13:03:16 -04002166 return true;
2167}
Geoff Lange8ebe7f2013-08-05 15:03:13 -04002168}