blob: b1c4810feed6e69019a76bd726c9295a8b9ce1aa [file] [log] [blame]
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001#include "precompiled.h"
2//
Geoff Langcec35902014-04-16 10:52:36 -04003// Copyright (c) 2013-2014 The ANGLE Project Authors. All rights reserved.
Geoff Lange8ebe7f2013-08-05 15:03:13 -04004// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
8// validationES.h: Validation functions for generic OpenGL ES entry point parameters
9
10#include "libGLESv2/validationES.h"
Jamie Madill26e91952014-03-05 15:01:27 -050011#include "libGLESv2/validationES2.h"
12#include "libGLESv2/validationES3.h"
Geoff Lange8ebe7f2013-08-05 15:03:13 -040013#include "libGLESv2/Context.h"
14#include "libGLESv2/Texture.h"
15#include "libGLESv2/Framebuffer.h"
Jamie Madille261b442014-06-25 12:42:21 -040016#include "libGLESv2/FramebufferAttachment.h"
Geoff Lange8ebe7f2013-08-05 15:03:13 -040017#include "libGLESv2/formatutils.h"
18#include "libGLESv2/main.h"
Jamie Madilldb2f14c2014-05-13 13:56:30 -040019#include "libGLESv2/Query.h"
Jamie Madill36398922014-05-20 14:51:53 -040020#include "libGLESv2/ProgramBinary.h"
Jamie Madill250d33f2014-06-06 17:09:03 -040021#include "libGLESv2/TransformFeedback.h"
Geoff Lange8ebe7f2013-08-05 15:03:13 -040022
23#include "common/mathutil.h"
24#include "common/utilities.h"
25
26namespace gl
27{
28
Geoff Lang0550d032014-01-30 11:29:07 -050029bool ValidCap(const Context *context, GLenum cap)
30{
31 switch (cap)
32 {
33 case GL_CULL_FACE:
34 case GL_POLYGON_OFFSET_FILL:
35 case GL_SAMPLE_ALPHA_TO_COVERAGE:
36 case GL_SAMPLE_COVERAGE:
37 case GL_SCISSOR_TEST:
38 case GL_STENCIL_TEST:
39 case GL_DEPTH_TEST:
40 case GL_BLEND:
41 case GL_DITHER:
42 return true;
43 case GL_PRIMITIVE_RESTART_FIXED_INDEX:
44 case GL_RASTERIZER_DISCARD:
45 return (context->getClientVersion() >= 3);
46 default:
47 return false;
48 }
49}
50
Jamie Madill1fc7e2c2014-01-21 16:47:10 -050051bool ValidTextureTarget(const Context *context, GLenum target)
Jamie Madill35d15012013-10-07 10:46:37 -040052{
Jamie Madilld7460c72014-01-21 16:38:14 -050053 switch (target)
Jamie Madill35d15012013-10-07 10:46:37 -040054 {
Jamie Madilld7460c72014-01-21 16:38:14 -050055 case GL_TEXTURE_2D:
56 case GL_TEXTURE_CUBE_MAP:
57 return true;
Jamie Madill35d15012013-10-07 10:46:37 -040058
Jamie Madilld7460c72014-01-21 16:38:14 -050059 case GL_TEXTURE_3D:
60 case GL_TEXTURE_2D_ARRAY:
61 return (context->getClientVersion() >= 3);
62
63 default:
64 return false;
65 }
Jamie Madill35d15012013-10-07 10:46:37 -040066}
67
Shannon Woods4dfed832014-03-17 20:03:39 -040068// This function differs from ValidTextureTarget in that the target must be
69// usable as the destination of a 2D operation-- so a cube face is valid, but
70// GL_TEXTURE_CUBE_MAP is not.
Jamie Madill560a8d82014-05-21 13:06:20 -040071// Note: duplicate of IsInternalTextureTarget
Shannon Woods4dfed832014-03-17 20:03:39 -040072bool ValidTexture2DDestinationTarget(const Context *context, GLenum target)
73{
74 switch (target)
75 {
76 case GL_TEXTURE_2D:
77 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
78 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
79 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
80 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
81 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
82 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
83 return true;
84 case GL_TEXTURE_2D_ARRAY:
85 case GL_TEXTURE_3D:
86 return (context->getClientVersion() >= 3);
87 default:
88 return false;
89 }
90}
91
Jamie Madill1fc7e2c2014-01-21 16:47:10 -050092bool ValidFramebufferTarget(GLenum target)
93{
94 META_ASSERT(GL_DRAW_FRAMEBUFFER_ANGLE == GL_DRAW_FRAMEBUFFER && GL_READ_FRAMEBUFFER_ANGLE == GL_READ_FRAMEBUFFER);
95
96 switch (target)
97 {
98 case GL_FRAMEBUFFER: return true;
99 case GL_READ_FRAMEBUFFER: return true;
100 case GL_DRAW_FRAMEBUFFER: return true;
101 default: return false;
102 }
103}
104
Jamie Madill8c96d582014-03-05 15:01:23 -0500105bool ValidBufferTarget(const Context *context, GLenum target)
106{
107 switch (target)
108 {
109 case GL_ARRAY_BUFFER:
110 case GL_ELEMENT_ARRAY_BUFFER:
111 return true;
112
Jamie Madill8c96d582014-03-05 15:01:23 -0500113 case GL_PIXEL_PACK_BUFFER:
114 case GL_PIXEL_UNPACK_BUFFER:
Geoff Langcec35902014-04-16 10:52:36 -0400115 return context->getCaps().extensions.pixelBufferObject;
Shannon Woods158c4382014-05-06 13:00:07 -0400116
Shannon Woodsb3801742014-03-27 14:59:19 -0400117 case GL_COPY_READ_BUFFER:
118 case GL_COPY_WRITE_BUFFER:
Jamie Madill8c96d582014-03-05 15:01:23 -0500119 case GL_TRANSFORM_FEEDBACK_BUFFER:
120 case GL_UNIFORM_BUFFER:
121 return (context->getClientVersion() >= 3);
122
123 default:
124 return false;
125 }
126}
127
Jamie Madill70656a62014-03-05 15:01:26 -0500128bool ValidBufferParameter(const Context *context, GLenum pname)
129{
130 switch (pname)
131 {
132 case GL_BUFFER_USAGE:
133 case GL_BUFFER_SIZE:
134 return true;
135
136 // GL_BUFFER_MAP_POINTER is a special case, and may only be
137 // queried with GetBufferPointerv
138 case GL_BUFFER_ACCESS_FLAGS:
139 case GL_BUFFER_MAPPED:
140 case GL_BUFFER_MAP_OFFSET:
141 case GL_BUFFER_MAP_LENGTH:
142 return (context->getClientVersion() >= 3);
143
144 default:
145 return false;
146 }
147}
148
Jamie Madill8c96d582014-03-05 15:01:23 -0500149bool ValidMipLevel(const Context *context, GLenum target, GLint level)
Geoff Langce635692013-09-24 13:56:32 -0400150{
Geoff Langaae65a42014-05-26 12:43:44 -0400151 size_t maxDimension = 0;
Geoff Langce635692013-09-24 13:56:32 -0400152 switch (target)
153 {
Geoff Langaae65a42014-05-26 12:43:44 -0400154 case GL_TEXTURE_2D: maxDimension = context->getCaps().max2DTextureSize; break;
Geoff Langce635692013-09-24 13:56:32 -0400155 case GL_TEXTURE_CUBE_MAP:
156 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
157 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
158 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
159 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
160 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
Geoff Langaae65a42014-05-26 12:43:44 -0400161 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: maxDimension = context->getCaps().maxCubeMapTextureSize; break;
162 case GL_TEXTURE_3D: maxDimension = context->getCaps().max3DTextureSize; break;
163 case GL_TEXTURE_2D_ARRAY: maxDimension = context->getCaps().max2DTextureSize; break;
Geoff Langce635692013-09-24 13:56:32 -0400164 default: UNREACHABLE();
165 }
166
Geoff Langaae65a42014-05-26 12:43:44 -0400167 return level <= gl::log2(maxDimension);
Geoff Langce635692013-09-24 13:56:32 -0400168}
169
Jamie Madill4fd75c12014-06-23 10:53:54 -0400170bool ValidImageSize(const gl::Context *context, GLenum target, GLint level,
171 GLsizei width, GLsizei height, GLsizei depth)
Geoff Langce635692013-09-24 13:56:32 -0400172{
173 if (level < 0 || width < 0 || height < 0 || depth < 0)
174 {
175 return false;
176 }
177
Jamie Madill4fd75c12014-06-23 10:53:54 -0400178 if (!context->getCaps().extensions.textureNPOT &&
179 (level != 0 && (!gl::isPow2(width) || !gl::isPow2(height) || !gl::isPow2(depth))))
Geoff Langce635692013-09-24 13:56:32 -0400180 {
181 return false;
182 }
183
184 if (!ValidMipLevel(context, target, level))
185 {
186 return false;
187 }
188
189 return true;
190}
191
Geoff Lang005df412013-10-16 14:12:50 -0400192bool ValidCompressedImageSize(const gl::Context *context, GLenum internalFormat, GLsizei width, GLsizei height)
Geoff Langd4f180b2013-09-24 13:57:44 -0400193{
Geoff Lange4a492b2014-06-19 14:14:41 -0400194 if (!IsFormatCompressed(internalFormat))
Geoff Langd4f180b2013-09-24 13:57:44 -0400195 {
196 return false;
197 }
198
Geoff Lange4a492b2014-06-19 14:14:41 -0400199 GLint blockWidth = GetCompressedBlockWidth(internalFormat);
200 GLint blockHeight = GetCompressedBlockHeight(internalFormat);
Geoff Langd4f180b2013-09-24 13:57:44 -0400201 if (width < 0 || (width > blockWidth && width % blockWidth != 0) ||
202 height < 0 || (height > blockHeight && height % blockHeight != 0))
203 {
204 return false;
205 }
206
207 return true;
208}
209
Geoff Lang37dde692014-01-31 16:34:54 -0500210bool ValidQueryType(const Context *context, GLenum queryType)
211{
212 META_ASSERT(GL_ANY_SAMPLES_PASSED == GL_ANY_SAMPLES_PASSED_EXT);
213 META_ASSERT(GL_ANY_SAMPLES_PASSED_CONSERVATIVE == GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT);
214
215 switch (queryType)
216 {
217 case GL_ANY_SAMPLES_PASSED:
218 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
219 return true;
220 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
221 return (context->getClientVersion() >= 3);
222 default:
223 return false;
224 }
225}
226
Geoff Lang48dcae72014-02-05 16:28:24 -0500227bool ValidProgram(const Context *context, GLuint id)
228{
229 // ES3 spec (section 2.11.1) -- "Commands that accept shader or program object names will generate the
230 // error INVALID_VALUE if the provided name is not the name of either a shader or program object and
231 // INVALID_OPERATION if the provided name identifies an object that is not the expected type."
232
233 if (context->getProgram(id) != NULL)
234 {
235 return true;
236 }
237 else if (context->getShader(id) != NULL)
238 {
239 // ID is the wrong type
240 return gl::error(GL_INVALID_OPERATION, false);
241 }
242 else
243 {
244 // No shader/program object has this ID
245 return gl::error(GL_INVALID_VALUE, false);
246 }
247}
248
Jamie Madillb4472272014-07-03 10:38:55 -0400249bool ValidateAttachmentTarget(const gl::Context *context, GLenum attachment)
250{
251 if (attachment >= GL_COLOR_ATTACHMENT0_EXT && attachment <= GL_COLOR_ATTACHMENT15_EXT)
252 {
253 const unsigned int colorAttachment = (attachment - GL_COLOR_ATTACHMENT0_EXT);
254
Geoff Langaae65a42014-05-26 12:43:44 -0400255 if (colorAttachment >= context->getCaps().maxColorAttachments)
Jamie Madillb4472272014-07-03 10:38:55 -0400256 {
257 return gl::error(GL_INVALID_VALUE, false);
258 }
259 }
260 else
261 {
262 switch (attachment)
263 {
264 case GL_DEPTH_ATTACHMENT:
265 case GL_STENCIL_ATTACHMENT:
266 break;
267
268 case GL_DEPTH_STENCIL_ATTACHMENT:
269 if (context->getClientVersion() < 3)
270 {
271 return gl::error(GL_INVALID_ENUM, false);
272 }
273 break;
274
275 default:
276 return gl::error(GL_INVALID_ENUM, false);
277 }
278 }
279
280 return true;
281}
282
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400283bool ValidateRenderbufferStorageParameters(const gl::Context *context, GLenum target, GLsizei samples,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400284 GLenum internalformat, GLsizei width, GLsizei height,
285 bool angleExtension)
286{
287 switch (target)
288 {
289 case GL_RENDERBUFFER:
290 break;
291 default:
292 return gl::error(GL_INVALID_ENUM, false);
293 }
294
295 if (width < 0 || height < 0 || samples < 0)
296 {
297 return gl::error(GL_INVALID_VALUE, false);
298 }
299
Geoff Langcec35902014-04-16 10:52:36 -0400300 const gl::Caps &caps = context->getCaps();
301 if (!gl::IsValidInternalFormat(internalformat, caps.extensions, context->getClientVersion()))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400302 {
303 return gl::error(GL_INVALID_ENUM, false);
304 }
305
306 // ANGLE_framebuffer_multisample does not explicitly state that the internal format must be
307 // sized but it does state that the format must be in the ES2.0 spec table 4.5 which contains
308 // only sized internal formats. The ES3 spec (section 4.4.2) does, however, state that the
309 // internal format must be sized and not an integer format if samples is greater than zero.
Geoff Lange4a492b2014-06-19 14:14:41 -0400310 if (!gl::IsSizedInternalFormat(internalformat))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400311 {
312 return gl::error(GL_INVALID_ENUM, false);
313 }
314
Geoff Lange4a492b2014-06-19 14:14:41 -0400315 GLenum componentType = gl::GetComponentType(internalformat);
Geoff Langb2f3d052013-08-13 12:49:27 -0400316 if ((componentType == GL_UNSIGNED_INT || componentType == GL_INT) && samples > 0)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400317 {
318 return gl::error(GL_INVALID_OPERATION, false);
319 }
320
Geoff Langcec35902014-04-16 10:52:36 -0400321 const TextureCaps &formatCaps = caps.textureCaps.get(internalformat);
322 if (!formatCaps.colorRendering && !formatCaps.depthRendering && !formatCaps.stencilRendering)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400323 {
324 return gl::error(GL_INVALID_ENUM, false);
325 }
326
Geoff Langaae65a42014-05-26 12:43:44 -0400327 if (static_cast<GLuint>(std::max(width, height)) > context->getCaps().maxRenderbufferSize)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400328 {
329 return gl::error(GL_INVALID_VALUE, false);
330 }
331
332 // ANGLE_framebuffer_multisample states that the value of samples must be less than or equal
333 // to MAX_SAMPLES_ANGLE (Context::getMaxSupportedSamples) while the ES3.0 spec (section 4.4.2)
334 // states that samples must be less than or equal to the maximum samples for the specified
335 // internal format.
336 if (angleExtension)
337 {
338 if (samples > context->getMaxSupportedSamples())
339 {
340 return gl::error(GL_INVALID_VALUE, false);
341 }
342 }
343 else
344 {
345 if (samples > context->getMaxSupportedFormatSamples(internalformat))
346 {
347 return gl::error(GL_INVALID_VALUE, false);
348 }
349 }
350
351 GLuint handle = context->getRenderbufferHandle();
352 if (handle == 0)
353 {
354 return gl::error(GL_INVALID_OPERATION, false);
355 }
356
357 return true;
358}
359
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500360bool ValidateFramebufferRenderbufferParameters(gl::Context *context, GLenum target, GLenum attachment,
361 GLenum renderbuffertarget, GLuint renderbuffer)
362{
363 gl::Framebuffer *framebuffer = context->getTargetFramebuffer(target);
364 GLuint framebufferHandle = context->getTargetFramebufferHandle(target);
365
366 if (!framebuffer || (framebufferHandle == 0 && renderbuffer != 0))
367 {
368 return gl::error(GL_INVALID_OPERATION, false);
369 }
370
Jamie Madillb4472272014-07-03 10:38:55 -0400371 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500372 {
Jamie Madillb4472272014-07-03 10:38:55 -0400373 return false;
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500374 }
375
Jamie Madillab9d82c2014-01-21 16:38:14 -0500376 // [OpenGL ES 2.0.25] Section 4.4.3 page 112
377 // [OpenGL ES 3.0.2] Section 4.4.2 page 201
378 // 'renderbuffer' must be either zero or the name of an existing renderbuffer object of
379 // type 'renderbuffertarget', otherwise an INVALID_OPERATION error is generated.
380 if (renderbuffer != 0)
381 {
382 if (!context->getRenderbuffer(renderbuffer))
383 {
384 return gl::error(GL_INVALID_OPERATION, false);
385 }
386 }
387
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500388 return true;
389}
390
Jamie Madill3c7fa222014-06-05 13:08:51 -0400391static bool IsPartialBlit(gl::Context *context, gl::FramebufferAttachment *readBuffer, gl::FramebufferAttachment *writeBuffer,
Geoff Lang125deab2013-08-09 13:34:16 -0400392 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
393 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1)
394{
395 if (srcX0 != 0 || srcY0 != 0 || dstX0 != 0 || dstY0 != 0 ||
396 dstX1 != writeBuffer->getWidth() || dstY1 != writeBuffer->getHeight() ||
397 srcX1 != readBuffer->getWidth() || srcY1 != readBuffer->getHeight())
398 {
399 return true;
400 }
401 else if (context->isScissorTestEnabled())
402 {
403 int scissorX, scissorY, scissorWidth, scissorHeight;
404 context->getScissorParams(&scissorX, &scissorY, &scissorWidth, &scissorHeight);
405
406 return scissorX > 0 || scissorY > 0 ||
407 scissorWidth < writeBuffer->getWidth() ||
408 scissorHeight < writeBuffer->getHeight();
409 }
410 else
411 {
412 return false;
413 }
414}
415
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400416bool ValidateBlitFramebufferParameters(gl::Context *context, GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400417 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask,
418 GLenum filter, bool fromAngleExtension)
419{
420 switch (filter)
421 {
422 case GL_NEAREST:
423 break;
424 case GL_LINEAR:
425 if (fromAngleExtension)
426 {
427 return gl::error(GL_INVALID_ENUM, false);
428 }
429 break;
430 default:
431 return gl::error(GL_INVALID_ENUM, false);
432 }
433
434 if ((mask & ~(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)) != 0)
435 {
436 return gl::error(GL_INVALID_VALUE, false);
437 }
438
439 if (mask == 0)
440 {
441 // ES3.0 spec, section 4.3.2 specifies that a mask of zero is valid and no
442 // buffers are copied.
443 return false;
444 }
445
446 if (fromAngleExtension && (srcX1 - srcX0 != dstX1 - dstX0 || srcY1 - srcY0 != dstY1 - dstY0))
447 {
448 ERR("Scaling and flipping in BlitFramebufferANGLE not supported by this implementation.");
449 return gl::error(GL_INVALID_OPERATION, false);
450 }
451
452 // ES3.0 spec, section 4.3.2 states that linear filtering is only available for the
453 // color buffer, leaving only nearest being unfiltered from above
454 if ((mask & ~GL_COLOR_BUFFER_BIT) != 0 && filter != GL_NEAREST)
455 {
456 return gl::error(GL_INVALID_OPERATION, false);
457 }
458
459 if (context->getReadFramebufferHandle() == context->getDrawFramebufferHandle())
460 {
461 if (fromAngleExtension)
462 {
463 ERR("Blits with the same source and destination framebuffer are not supported by this "
464 "implementation.");
465 }
466 return gl::error(GL_INVALID_OPERATION, false);
467 }
468
469 gl::Framebuffer *readFramebuffer = context->getReadFramebuffer();
470 gl::Framebuffer *drawFramebuffer = context->getDrawFramebuffer();
471 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
472 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
473 {
474 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
475 }
476
477 if (drawFramebuffer->getSamples() != 0)
478 {
479 return gl::error(GL_INVALID_OPERATION, false);
480 }
481
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400482 bool sameBounds = srcX0 == dstX0 && srcY0 == dstY0 && srcX1 == dstX1 && srcY1 == dstY1;
483
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400484 if (mask & GL_COLOR_BUFFER_BIT)
485 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400486 gl::FramebufferAttachment *readColorBuffer = readFramebuffer->getReadColorbuffer();
487 gl::FramebufferAttachment *drawColorBuffer = drawFramebuffer->getFirstColorbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400488
489 if (readColorBuffer && drawColorBuffer)
490 {
Geoff Lang005df412013-10-16 14:12:50 -0400491 GLenum readInternalFormat = readColorBuffer->getActualFormat();
Geoff Lange4a492b2014-06-19 14:14:41 -0400492 GLenum readComponentType = gl::GetComponentType(readInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400493
494 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; i++)
495 {
496 if (drawFramebuffer->isEnabledColorAttachment(i))
497 {
Geoff Lang005df412013-10-16 14:12:50 -0400498 GLenum drawInternalFormat = drawFramebuffer->getColorbuffer(i)->getActualFormat();
Geoff Lange4a492b2014-06-19 14:14:41 -0400499 GLenum drawComponentType = gl::GetComponentType(drawInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400500
Geoff Langb2f3d052013-08-13 12:49:27 -0400501 // The GL ES 3.0.2 spec (pg 193) states that:
502 // 1) If the read buffer is fixed point format, the draw buffer must be as well
503 // 2) If the read buffer is an unsigned integer format, the draw buffer must be as well
504 // 3) If the read buffer is a signed integer format, the draw buffer must be as well
505 if ( (readComponentType == GL_UNSIGNED_NORMALIZED || readComponentType == GL_SIGNED_NORMALIZED) &&
506 !(drawComponentType == GL_UNSIGNED_NORMALIZED || drawComponentType == GL_SIGNED_NORMALIZED))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400507 {
508 return gl::error(GL_INVALID_OPERATION, false);
509 }
510
Geoff Langb2f3d052013-08-13 12:49:27 -0400511 if (readComponentType == GL_UNSIGNED_INT && drawComponentType != GL_UNSIGNED_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400512 {
513 return gl::error(GL_INVALID_OPERATION, false);
514 }
515
Geoff Langb2f3d052013-08-13 12:49:27 -0400516 if (readComponentType == GL_INT && drawComponentType != GL_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400517 {
518 return gl::error(GL_INVALID_OPERATION, false);
519 }
520
Geoff Langb2f3d052013-08-13 12:49:27 -0400521 if (readColorBuffer->getSamples() > 0 && (readInternalFormat != drawInternalFormat || !sameBounds))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400522 {
523 return gl::error(GL_INVALID_OPERATION, false);
524 }
525 }
526 }
527
Geoff Langb2f3d052013-08-13 12:49:27 -0400528 if ((readComponentType == GL_INT || readComponentType == GL_UNSIGNED_INT) && filter == GL_LINEAR)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400529 {
530 return gl::error(GL_INVALID_OPERATION, false);
531 }
532
533 if (fromAngleExtension)
534 {
535 const GLenum readColorbufferType = readFramebuffer->getReadColorbufferType();
536 if (readColorbufferType != GL_TEXTURE_2D && readColorbufferType != GL_RENDERBUFFER)
537 {
538 return gl::error(GL_INVALID_OPERATION, false);
539 }
540
541 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
542 {
543 if (drawFramebuffer->isEnabledColorAttachment(colorAttachment))
544 {
545 if (drawFramebuffer->getColorbufferType(colorAttachment) != GL_TEXTURE_2D &&
546 drawFramebuffer->getColorbufferType(colorAttachment) != GL_RENDERBUFFER)
547 {
548 return gl::error(GL_INVALID_OPERATION, false);
549 }
550
551 if (drawFramebuffer->getColorbuffer(colorAttachment)->getActualFormat() != readColorBuffer->getActualFormat())
552 {
553 return gl::error(GL_INVALID_OPERATION, false);
554 }
555 }
556 }
Geoff Lang125deab2013-08-09 13:34:16 -0400557 if (readFramebuffer->getSamples() != 0 && IsPartialBlit(context, readColorBuffer, drawColorBuffer,
558 srcX0, srcY0, srcX1, srcY1,
559 dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400560 {
561 return gl::error(GL_INVALID_OPERATION, false);
562 }
563 }
564 }
565 }
566
567 if (mask & GL_DEPTH_BUFFER_BIT)
568 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400569 gl::FramebufferAttachment *readDepthBuffer = readFramebuffer->getDepthbuffer();
570 gl::FramebufferAttachment *drawDepthBuffer = drawFramebuffer->getDepthbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400571
572 if (readDepthBuffer && drawDepthBuffer)
573 {
574 if (readDepthBuffer->getActualFormat() != drawDepthBuffer->getActualFormat())
575 {
576 return gl::error(GL_INVALID_OPERATION, false);
577 }
578
579 if (readDepthBuffer->getSamples() > 0 && !sameBounds)
580 {
581 return gl::error(GL_INVALID_OPERATION, false);
582 }
583
584 if (fromAngleExtension)
585 {
Geoff Lang125deab2013-08-09 13:34:16 -0400586 if (IsPartialBlit(context, readDepthBuffer, drawDepthBuffer,
587 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400588 {
589 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
590 return gl::error(GL_INVALID_OPERATION, false); // only whole-buffer copies are permitted
591 }
592
593 if (readDepthBuffer->getSamples() != 0 || drawDepthBuffer->getSamples() != 0)
594 {
595 return gl::error(GL_INVALID_OPERATION, false);
596 }
597 }
598 }
599 }
600
601 if (mask & GL_STENCIL_BUFFER_BIT)
602 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400603 gl::FramebufferAttachment *readStencilBuffer = readFramebuffer->getStencilbuffer();
604 gl::FramebufferAttachment *drawStencilBuffer = drawFramebuffer->getStencilbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400605
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400606 if (readStencilBuffer && drawStencilBuffer)
607 {
608 if (readStencilBuffer->getActualFormat() != drawStencilBuffer->getActualFormat())
609 {
610 return gl::error(GL_INVALID_OPERATION, false);
611 }
612
613 if (readStencilBuffer->getSamples() > 0 && !sameBounds)
614 {
615 return gl::error(GL_INVALID_OPERATION, false);
616 }
617
618 if (fromAngleExtension)
619 {
Geoff Lang125deab2013-08-09 13:34:16 -0400620 if (IsPartialBlit(context, readStencilBuffer, drawStencilBuffer,
621 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400622 {
623 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
624 return gl::error(GL_INVALID_OPERATION, false); // only whole-buffer copies are permitted
625 }
626
627 if (readStencilBuffer->getSamples() != 0 || drawStencilBuffer->getSamples() != 0)
628 {
629 return gl::error(GL_INVALID_OPERATION, false);
630 }
631 }
632 }
633 }
634
635 return true;
636}
637
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400638bool ValidateGetVertexAttribParameters(GLenum pname, int clientVersion)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400639{
640 switch (pname)
641 {
642 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
643 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
644 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
645 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
646 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
647 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
648 case GL_CURRENT_VERTEX_ATTRIB:
649 return true;
650
651 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR:
652 // Don't verify ES3 context because GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE uses
653 // the same constant.
654 META_ASSERT(GL_VERTEX_ATTRIB_ARRAY_DIVISOR == GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE);
655 return true;
656
657 case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
658 return ((clientVersion >= 3) ? true : gl::error(GL_INVALID_ENUM, false));
659
660 default:
661 return gl::error(GL_INVALID_ENUM, false);
662 }
663}
664
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400665bool ValidateTexParamParameters(gl::Context *context, GLenum pname, GLint param)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400666{
667 switch (pname)
668 {
669 case GL_TEXTURE_WRAP_R:
670 case GL_TEXTURE_SWIZZLE_R:
671 case GL_TEXTURE_SWIZZLE_G:
672 case GL_TEXTURE_SWIZZLE_B:
673 case GL_TEXTURE_SWIZZLE_A:
674 case GL_TEXTURE_BASE_LEVEL:
675 case GL_TEXTURE_MAX_LEVEL:
676 case GL_TEXTURE_COMPARE_MODE:
677 case GL_TEXTURE_COMPARE_FUNC:
678 case GL_TEXTURE_MIN_LOD:
679 case GL_TEXTURE_MAX_LOD:
680 if (context->getClientVersion() < 3)
681 {
682 return gl::error(GL_INVALID_ENUM, false);
683 }
684 break;
685
686 default: break;
687 }
688
689 switch (pname)
690 {
691 case GL_TEXTURE_WRAP_S:
692 case GL_TEXTURE_WRAP_T:
693 case GL_TEXTURE_WRAP_R:
694 switch (param)
695 {
696 case GL_REPEAT:
697 case GL_CLAMP_TO_EDGE:
698 case GL_MIRRORED_REPEAT:
699 return true;
700 default:
701 return gl::error(GL_INVALID_ENUM, false);
702 }
703
704 case GL_TEXTURE_MIN_FILTER:
705 switch (param)
706 {
707 case GL_NEAREST:
708 case GL_LINEAR:
709 case GL_NEAREST_MIPMAP_NEAREST:
710 case GL_LINEAR_MIPMAP_NEAREST:
711 case GL_NEAREST_MIPMAP_LINEAR:
712 case GL_LINEAR_MIPMAP_LINEAR:
713 return true;
714 default:
715 return gl::error(GL_INVALID_ENUM, false);
716 }
717 break;
718
719 case GL_TEXTURE_MAG_FILTER:
720 switch (param)
721 {
722 case GL_NEAREST:
723 case GL_LINEAR:
724 return true;
725 default:
726 return gl::error(GL_INVALID_ENUM, false);
727 }
728 break;
729
730 case GL_TEXTURE_USAGE_ANGLE:
731 switch (param)
732 {
733 case GL_NONE:
734 case GL_FRAMEBUFFER_ATTACHMENT_ANGLE:
735 return true;
736 default:
737 return gl::error(GL_INVALID_ENUM, false);
738 }
739 break;
740
741 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
Geoff Langcec35902014-04-16 10:52:36 -0400742 if (!context->getCaps().extensions.textureFilterAnisotropic)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400743 {
744 return gl::error(GL_INVALID_ENUM, false);
745 }
746
747 // we assume the parameter passed to this validation method is truncated, not rounded
748 if (param < 1)
749 {
750 return gl::error(GL_INVALID_VALUE, false);
751 }
752 return true;
753
754 case GL_TEXTURE_MIN_LOD:
755 case GL_TEXTURE_MAX_LOD:
756 // any value is permissible
757 return true;
758
759 case GL_TEXTURE_COMPARE_MODE:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400760 // Acceptable mode parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400761 switch (param)
762 {
763 case GL_NONE:
764 case GL_COMPARE_REF_TO_TEXTURE:
765 return true;
766 default:
767 return gl::error(GL_INVALID_ENUM, false);
768 }
769 break;
770
771 case GL_TEXTURE_COMPARE_FUNC:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400772 // Acceptable function parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400773 switch (param)
774 {
775 case GL_LEQUAL:
776 case GL_GEQUAL:
777 case GL_LESS:
778 case GL_GREATER:
779 case GL_EQUAL:
780 case GL_NOTEQUAL:
781 case GL_ALWAYS:
782 case GL_NEVER:
783 return true;
784 default:
785 return gl::error(GL_INVALID_ENUM, false);
786 }
787 break;
788
789 case GL_TEXTURE_SWIZZLE_R:
790 case GL_TEXTURE_SWIZZLE_G:
791 case GL_TEXTURE_SWIZZLE_B:
792 case GL_TEXTURE_SWIZZLE_A:
Geoff Langbc90a482013-09-17 16:51:27 -0400793 switch (param)
794 {
795 case GL_RED:
796 case GL_GREEN:
797 case GL_BLUE:
798 case GL_ALPHA:
799 case GL_ZERO:
800 case GL_ONE:
801 return true;
802 default:
803 return gl::error(GL_INVALID_ENUM, false);
804 }
805 break;
806
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400807 case GL_TEXTURE_BASE_LEVEL:
808 case GL_TEXTURE_MAX_LEVEL:
Nicolas Capens8de68282014-04-04 11:10:27 -0400809 if (param < 0)
810 {
811 return gl::error(GL_INVALID_VALUE, false);
812 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400813 return true;
814
815 default:
816 return gl::error(GL_INVALID_ENUM, false);
817 }
818}
819
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400820bool ValidateSamplerObjectParameter(GLenum pname)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400821{
822 switch (pname)
823 {
824 case GL_TEXTURE_MIN_FILTER:
825 case GL_TEXTURE_MAG_FILTER:
826 case GL_TEXTURE_WRAP_S:
827 case GL_TEXTURE_WRAP_T:
828 case GL_TEXTURE_WRAP_R:
829 case GL_TEXTURE_MIN_LOD:
830 case GL_TEXTURE_MAX_LOD:
831 case GL_TEXTURE_COMPARE_MODE:
832 case GL_TEXTURE_COMPARE_FUNC:
833 return true;
834
835 default:
836 return gl::error(GL_INVALID_ENUM, false);
837 }
838}
839
Jamie Madill26e91952014-03-05 15:01:27 -0500840bool ValidateReadPixelsParameters(gl::Context *context, GLint x, GLint y, GLsizei width, GLsizei height,
841 GLenum format, GLenum type, GLsizei *bufSize, GLvoid *pixels)
842{
843 gl::Framebuffer *framebuffer = context->getReadFramebuffer();
Jamie Madill893ab082014-05-16 16:56:10 -0400844 ASSERT(framebuffer);
Jamie Madill26e91952014-03-05 15:01:27 -0500845
846 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
847 {
848 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
849 }
850
851 if (context->getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
852 {
853 return gl::error(GL_INVALID_OPERATION, false);
854 }
855
Jamie Madill893ab082014-05-16 16:56:10 -0400856 if (!framebuffer->getReadColorbuffer())
857 {
858 return gl::error(GL_INVALID_OPERATION, false);
859 }
860
Jamie Madill26e91952014-03-05 15:01:27 -0500861 GLenum currentInternalFormat, currentFormat, currentType;
Geoff Lange4a492b2014-06-19 14:14:41 -0400862 GLuint clientVersion = context->getClientVersion();
Jamie Madill26e91952014-03-05 15:01:27 -0500863
Jamie Madill893ab082014-05-16 16:56:10 -0400864 context->getCurrentReadFormatType(&currentInternalFormat, &currentFormat, &currentType);
Jamie Madill26e91952014-03-05 15:01:27 -0500865
Geoff Langbdc9b2f2014-04-16 14:41:54 -0400866 bool validReadFormat = (clientVersion < 3) ? ValidES2ReadFormatType(context, format, type) :
867 ValidES3ReadFormatType(context, currentInternalFormat, format, type);
Jamie Madill26e91952014-03-05 15:01:27 -0500868
869 if (!(currentFormat == format && currentType == type) && !validReadFormat)
870 {
871 return gl::error(GL_INVALID_OPERATION, false);
872 }
873
Geoff Lange4a492b2014-06-19 14:14:41 -0400874 GLenum sizedInternalFormat = IsSizedInternalFormat(format) ? format
875 : GetSizedInternalFormat(format, type);
Jamie Madill26e91952014-03-05 15:01:27 -0500876
Geoff Lange4a492b2014-06-19 14:14:41 -0400877 GLsizei outputPitch = GetRowPitch(sizedInternalFormat, type, width, context->getPackAlignment());
Jamie Madill26e91952014-03-05 15:01:27 -0500878 // sized query sanity check
879 if (bufSize)
880 {
881 int requiredSize = outputPitch * height;
882 if (requiredSize > *bufSize)
883 {
884 return gl::error(GL_INVALID_OPERATION, false);
885 }
886 }
887
888 return true;
889}
890
Jamie Madilldb2f14c2014-05-13 13:56:30 -0400891bool ValidateBeginQuery(gl::Context *context, GLenum target, GLuint id)
892{
893 if (!ValidQueryType(context, target))
894 {
895 return gl::error(GL_INVALID_ENUM, false);
896 }
897
898 if (id == 0)
899 {
900 return gl::error(GL_INVALID_OPERATION, false);
901 }
902
903 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
904 // of zero, if the active query object name for <target> is non-zero (for the
905 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
906 // the active query for either target is non-zero), if <id> is the name of an
907 // existing query object whose type does not match <target>, or if <id> is the
908 // active query object name for any query type, the error INVALID_OPERATION is
909 // generated.
910
911 // Ensure no other queries are active
912 // NOTE: If other queries than occlusion are supported, we will need to check
913 // separately that:
914 // a) The query ID passed is not the current active query for any target/type
915 // b) There are no active queries for the requested target (and in the case
916 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
917 // no query may be active for either if glBeginQuery targets either.
918 if (context->isQueryActive())
919 {
920 return gl::error(GL_INVALID_OPERATION, false);
921 }
922
923 Query *queryObject = context->getQuery(id, true, target);
924
925 // check that name was obtained with glGenQueries
926 if (!queryObject)
927 {
928 return gl::error(GL_INVALID_OPERATION, false);
929 }
930
931 // check for type mismatch
932 if (queryObject->getType() != target)
933 {
934 return gl::error(GL_INVALID_OPERATION, false);
935 }
936
937 return true;
938}
939
Jamie Madill45c785d2014-05-13 14:09:34 -0400940bool ValidateEndQuery(gl::Context *context, GLenum target)
941{
942 if (!ValidQueryType(context, target))
943 {
944 return gl::error(GL_INVALID_ENUM, false);
945 }
946
947 const Query *queryObject = context->getActiveQuery(target);
948
949 if (queryObject == NULL)
950 {
951 return gl::error(GL_INVALID_OPERATION, false);
952 }
953
954 if (!queryObject->isStarted())
955 {
956 return gl::error(GL_INVALID_OPERATION, false);
957 }
958
959 return true;
960}
961
Jamie Madill36398922014-05-20 14:51:53 -0400962static bool ValidateUniformCommonBase(gl::Context *context, GLenum targetUniformType,
963 GLint location, GLsizei count, LinkedUniform **uniformOut)
Jamie Madilld7c7bb22014-05-20 10:55:54 -0400964{
965 if (count < 0)
966 {
967 return gl::error(GL_INVALID_VALUE, false);
968 }
969
Jamie Madilld7c7bb22014-05-20 10:55:54 -0400970 gl::ProgramBinary *programBinary = context->getCurrentProgramBinary();
971 if (!programBinary)
972 {
973 return gl::error(GL_INVALID_OPERATION, false);
974 }
975
976 if (location == -1)
977 {
978 // Silently ignore the uniform command
979 return false;
980 }
981
Jamie Madill36398922014-05-20 14:51:53 -0400982 if (!programBinary->isValidUniformLocation(location))
983 {
984 return gl::error(GL_INVALID_OPERATION, false);
985 }
986
987 LinkedUniform *uniform = programBinary->getUniformByLocation(location);
988
989 // attempting to write an array to a non-array uniform is an INVALID_OPERATION
990 if (uniform->elementCount() == 1 && count > 1)
991 {
992 return gl::error(GL_INVALID_OPERATION, false);
993 }
994
995 *uniformOut = uniform;
Jamie Madilld7c7bb22014-05-20 10:55:54 -0400996 return true;
997}
998
Jamie Madillaa981bd2014-05-20 10:55:55 -0400999bool ValidateUniform(gl::Context *context, GLenum uniformType, GLint location, GLsizei count)
1000{
1001 // Check for ES3 uniform entry points
Jamie Madillf2575982014-06-25 16:04:54 -04001002 if (VariableComponentType(uniformType) == GL_UNSIGNED_INT && context->getClientVersion() < 3)
Jamie Madillaa981bd2014-05-20 10:55:55 -04001003 {
1004 return gl::error(GL_INVALID_OPERATION, false);
1005 }
1006
Jamie Madill36398922014-05-20 14:51:53 -04001007 LinkedUniform *uniform = NULL;
1008 if (!ValidateUniformCommonBase(context, uniformType, location, count, &uniform))
1009 {
1010 return false;
1011 }
1012
Jamie Madillf2575982014-06-25 16:04:54 -04001013 GLenum targetBoolType = VariableBoolVectorType(uniformType);
Jamie Madill36398922014-05-20 14:51:53 -04001014 bool samplerUniformCheck = (IsSampler(uniform->type) && uniformType == GL_INT);
1015 if (!samplerUniformCheck && uniformType != uniform->type && targetBoolType != uniform->type)
1016 {
1017 return gl::error(GL_INVALID_OPERATION, false);
1018 }
1019
1020 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001021}
1022
1023bool ValidateUniformMatrix(gl::Context *context, GLenum matrixType, GLint location, GLsizei count,
1024 GLboolean transpose)
1025{
1026 // Check for ES3 uniform entry points
1027 int rows = VariableRowCount(matrixType);
1028 int cols = VariableColumnCount(matrixType);
1029 if (rows != cols && context->getClientVersion() < 3)
1030 {
1031 return gl::error(GL_INVALID_OPERATION, false);
1032 }
1033
1034 if (transpose != GL_FALSE && context->getClientVersion() < 3)
1035 {
1036 return gl::error(GL_INVALID_VALUE, false);
1037 }
1038
Jamie Madill36398922014-05-20 14:51:53 -04001039 LinkedUniform *uniform = NULL;
1040 if (!ValidateUniformCommonBase(context, matrixType, location, count, &uniform))
1041 {
1042 return false;
1043 }
1044
1045 if (uniform->type != matrixType)
1046 {
1047 return gl::error(GL_INVALID_OPERATION, false);
1048 }
1049
1050 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001051}
1052
Jamie Madill893ab082014-05-16 16:56:10 -04001053bool ValidateStateQuery(gl::Context *context, GLenum pname, GLenum *nativeType, unsigned int *numParams)
1054{
1055 if (!context->getQueryParameterInfo(pname, nativeType, numParams))
1056 {
1057 return gl::error(GL_INVALID_ENUM, false);
1058 }
1059
1060 if (pname >= GL_DRAW_BUFFER0 && pname <= GL_DRAW_BUFFER15)
1061 {
1062 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0);
1063
Geoff Langaae65a42014-05-26 12:43:44 -04001064 if (colorAttachment >= context->getCaps().maxDrawBuffers)
Jamie Madill893ab082014-05-16 16:56:10 -04001065 {
1066 return gl::error(GL_INVALID_OPERATION, false);
1067 }
1068 }
1069
1070 switch (pname)
1071 {
1072 case GL_TEXTURE_BINDING_2D:
1073 case GL_TEXTURE_BINDING_CUBE_MAP:
1074 case GL_TEXTURE_BINDING_3D:
1075 case GL_TEXTURE_BINDING_2D_ARRAY:
1076 if (context->getActiveSampler() >= context->getMaximumCombinedTextureImageUnits())
1077 {
1078 return gl::error(GL_INVALID_OPERATION, false);
1079 }
1080 break;
1081
1082 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1083 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1084 {
1085 Framebuffer *framebuffer = context->getReadFramebuffer();
1086 ASSERT(framebuffer);
1087 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1088 {
1089 return gl::error(GL_INVALID_OPERATION, false);
1090 }
1091
Jamie Madill3c7fa222014-06-05 13:08:51 -04001092 FramebufferAttachment *attachment = framebuffer->getReadColorbuffer();
1093 if (!attachment)
Jamie Madill893ab082014-05-16 16:56:10 -04001094 {
1095 return gl::error(GL_INVALID_OPERATION, false);
1096 }
1097 }
1098 break;
1099
1100 default:
1101 break;
1102 }
1103
1104 // pname is valid, but there are no parameters to return
1105 if (numParams == 0)
1106 {
1107 return false;
1108 }
1109
1110 return true;
1111}
1112
Jamie Madill560a8d82014-05-21 13:06:20 -04001113bool ValidateCopyTexImageParametersBase(gl::Context* context, GLenum target, GLint level, GLenum internalformat, bool isSubImage,
1114 GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height,
1115 GLint border, GLenum *textureFormatOut)
1116{
1117
1118 if (!ValidTexture2DDestinationTarget(context, target))
1119 {
1120 return gl::error(GL_INVALID_ENUM, false);
1121 }
1122
1123 if (level < 0 || xoffset < 0 || yoffset < 0 || zoffset < 0 || width < 0 || height < 0)
1124 {
1125 return gl::error(GL_INVALID_VALUE, false);
1126 }
1127
1128 if (std::numeric_limits<GLsizei>::max() - xoffset < width || std::numeric_limits<GLsizei>::max() - yoffset < height)
1129 {
1130 return gl::error(GL_INVALID_VALUE, false);
1131 }
1132
1133 if (border != 0)
1134 {
1135 return gl::error(GL_INVALID_VALUE, false);
1136 }
1137
1138 if (!ValidMipLevel(context, target, level))
1139 {
1140 return gl::error(GL_INVALID_VALUE, false);
1141 }
1142
1143 gl::Framebuffer *framebuffer = context->getReadFramebuffer();
1144 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1145 {
1146 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
1147 }
1148
1149 if (context->getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
1150 {
1151 return gl::error(GL_INVALID_OPERATION, false);
1152 }
1153
Geoff Langaae65a42014-05-26 12:43:44 -04001154 const gl::Caps &caps = context->getCaps();
1155
Jamie Madill560a8d82014-05-21 13:06:20 -04001156 gl::Texture *texture = NULL;
1157 GLenum textureInternalFormat = GL_NONE;
1158 bool textureCompressed = false;
1159 bool textureIsDepth = false;
1160 GLint textureLevelWidth = 0;
1161 GLint textureLevelHeight = 0;
1162 GLint textureLevelDepth = 0;
Geoff Langaae65a42014-05-26 12:43:44 -04001163 GLuint maxDimension = 0;
Jamie Madill560a8d82014-05-21 13:06:20 -04001164
1165 switch (target)
1166 {
1167 case GL_TEXTURE_2D:
1168 {
1169 gl::Texture2D *texture2d = context->getTexture2D();
1170 if (texture2d)
1171 {
1172 textureInternalFormat = texture2d->getInternalFormat(level);
1173 textureCompressed = texture2d->isCompressed(level);
1174 textureIsDepth = texture2d->isDepth(level);
1175 textureLevelWidth = texture2d->getWidth(level);
1176 textureLevelHeight = texture2d->getHeight(level);
1177 textureLevelDepth = 1;
1178 texture = texture2d;
Geoff Langaae65a42014-05-26 12:43:44 -04001179 maxDimension = caps.max2DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001180 }
1181 }
1182 break;
1183
1184 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1185 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1186 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1187 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1188 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1189 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1190 {
1191 gl::TextureCubeMap *textureCube = context->getTextureCubeMap();
1192 if (textureCube)
1193 {
1194 textureInternalFormat = textureCube->getInternalFormat(target, level);
1195 textureCompressed = textureCube->isCompressed(target, level);
1196 textureIsDepth = false;
1197 textureLevelWidth = textureCube->getWidth(target, level);
1198 textureLevelHeight = textureCube->getHeight(target, level);
1199 textureLevelDepth = 1;
1200 texture = textureCube;
Geoff Langaae65a42014-05-26 12:43:44 -04001201 maxDimension = caps.maxCubeMapTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001202 }
1203 }
1204 break;
1205
1206 case GL_TEXTURE_2D_ARRAY:
1207 {
1208 gl::Texture2DArray *texture2dArray = context->getTexture2DArray();
1209 if (texture2dArray)
1210 {
1211 textureInternalFormat = texture2dArray->getInternalFormat(level);
1212 textureCompressed = texture2dArray->isCompressed(level);
1213 textureIsDepth = texture2dArray->isDepth(level);
1214 textureLevelWidth = texture2dArray->getWidth(level);
1215 textureLevelHeight = texture2dArray->getHeight(level);
1216 textureLevelDepth = texture2dArray->getLayers(level);
1217 texture = texture2dArray;
Geoff Langaae65a42014-05-26 12:43:44 -04001218 maxDimension = caps.max2DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001219 }
1220 }
1221 break;
1222
1223 case GL_TEXTURE_3D:
1224 {
1225 gl::Texture3D *texture3d = context->getTexture3D();
1226 if (texture3d)
1227 {
1228 textureInternalFormat = texture3d->getInternalFormat(level);
1229 textureCompressed = texture3d->isCompressed(level);
1230 textureIsDepth = texture3d->isDepth(level);
1231 textureLevelWidth = texture3d->getWidth(level);
1232 textureLevelHeight = texture3d->getHeight(level);
1233 textureLevelDepth = texture3d->getDepth(level);
1234 texture = texture3d;
Geoff Langaae65a42014-05-26 12:43:44 -04001235 maxDimension = caps.max3DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001236 }
1237 }
1238 break;
1239
1240 default:
1241 return gl::error(GL_INVALID_ENUM, false);
1242 }
1243
1244 if (!texture)
1245 {
1246 return gl::error(GL_INVALID_OPERATION, false);
1247 }
1248
1249 if (texture->isImmutable() && !isSubImage)
1250 {
1251 return gl::error(GL_INVALID_OPERATION, false);
1252 }
1253
1254 if (textureIsDepth)
1255 {
1256 return gl::error(GL_INVALID_OPERATION, false);
1257 }
1258
1259 if (textureCompressed)
1260 {
Geoff Lange4a492b2014-06-19 14:14:41 -04001261 GLint blockWidth = GetCompressedBlockWidth(textureInternalFormat);
1262 GLint blockHeight = GetCompressedBlockHeight(textureInternalFormat);
Jamie Madill560a8d82014-05-21 13:06:20 -04001263
1264 if (((width % blockWidth) != 0 && width != textureLevelWidth) ||
1265 ((height % blockHeight) != 0 && height != textureLevelHeight))
1266 {
1267 return gl::error(GL_INVALID_OPERATION, false);
1268 }
1269 }
1270
1271 if (isSubImage)
1272 {
1273 if (xoffset + width > textureLevelWidth ||
1274 yoffset + height > textureLevelHeight ||
1275 zoffset >= textureLevelDepth)
1276 {
1277 return gl::error(GL_INVALID_VALUE, false);
1278 }
1279 }
Jamie Madill6f38f822014-06-06 17:12:20 -04001280 else
1281 {
1282 if (IsCubemapTextureTarget(target) && width != height)
1283 {
1284 return gl::error(GL_INVALID_VALUE, false);
1285 }
1286
Geoff Langcec35902014-04-16 10:52:36 -04001287 if (!IsValidInternalFormat(internalformat, context->getCaps().extensions, context->getClientVersion()))
Jamie Madill6f38f822014-06-06 17:12:20 -04001288 {
1289 return gl::error(GL_INVALID_ENUM, false);
1290 }
1291
1292 int maxLevelDimension = (maxDimension >> level);
1293 if (static_cast<int>(width) > maxLevelDimension || static_cast<int>(height) > maxLevelDimension)
1294 {
1295 return gl::error(GL_INVALID_VALUE, false);
1296 }
1297 }
Jamie Madill560a8d82014-05-21 13:06:20 -04001298
1299 *textureFormatOut = textureInternalFormat;
1300 return true;
1301}
1302
Jamie Madill1aeb1312014-06-20 13:21:25 -04001303static bool ValidateDrawBase(const gl::Context *context, GLenum mode, GLsizei count)
Jamie Madill250d33f2014-06-06 17:09:03 -04001304{
Jamie Madill1aeb1312014-06-20 13:21:25 -04001305 switch (mode)
1306 {
1307 case GL_POINTS:
1308 case GL_LINES:
1309 case GL_LINE_LOOP:
1310 case GL_LINE_STRIP:
1311 case GL_TRIANGLES:
1312 case GL_TRIANGLE_STRIP:
1313 case GL_TRIANGLE_FAN:
1314 break;
1315 default:
1316 return gl::error(GL_INVALID_ENUM, false);
1317 }
1318
Jamie Madill250d33f2014-06-06 17:09:03 -04001319 if (count < 0)
1320 {
1321 return gl::error(GL_INVALID_VALUE, false);
1322 }
1323
Jamie Madill250d33f2014-06-06 17:09:03 -04001324 // Check for mapped buffers
Jamie Madillfd716582014-06-06 17:09:04 -04001325 if (context->hasMappedBuffer(GL_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001326 {
1327 return gl::error(GL_INVALID_OPERATION, false);
1328 }
1329
Jamie Madillac528012014-06-20 13:21:23 -04001330 const gl::DepthStencilState &depthStencilState = context->getDepthStencilState();
1331 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
1332 context->getStencilRef() != context->getStencilBackRef() ||
1333 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
1334 {
1335 // Note: these separate values are not supported in WebGL, due to D3D's limitations.
1336 // See Section 6.10 of the WebGL 1.0 spec
1337 ERR("This ANGLE implementation does not support separate front/back stencil "
1338 "writemasks, reference values, or stencil mask values.");
1339 return gl::error(GL_INVALID_OPERATION, false);
1340 }
1341
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001342 const gl::Framebuffer *fbo = context->getDrawFramebuffer();
1343 if (!fbo || fbo->completeness() != GL_FRAMEBUFFER_COMPLETE)
1344 {
1345 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
1346 }
1347
Jamie Madill250d33f2014-06-06 17:09:03 -04001348 // No-op if zero count
1349 return (count > 0);
1350}
1351
Jamie Madillfd716582014-06-06 17:09:04 -04001352bool ValidateDrawArrays(const gl::Context *context, GLenum mode, GLint first, GLsizei count)
Jamie Madill250d33f2014-06-06 17:09:03 -04001353{
Jamie Madillfd716582014-06-06 17:09:04 -04001354 if (first < 0)
Jamie Madill250d33f2014-06-06 17:09:03 -04001355 {
1356 return gl::error(GL_INVALID_VALUE, false);
1357 }
1358
Jamie Madillfd716582014-06-06 17:09:04 -04001359 gl::TransformFeedback *curTransformFeedback = context->getCurrentTransformFeedback();
1360 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused() &&
1361 curTransformFeedback->getDrawMode() != mode)
1362 {
1363 // It is an invalid operation to call DrawArrays or DrawArraysInstanced with a draw mode
1364 // that does not match the current transform feedback object's draw mode (if transform feedback
1365 // is active), (3.0.2, section 2.14, pg 86)
1366 return gl::error(GL_INVALID_OPERATION, false);
1367 }
1368
Jamie Madill1aeb1312014-06-20 13:21:25 -04001369 if (!ValidateDrawBase(context, mode, count))
Jamie Madillfd716582014-06-06 17:09:04 -04001370 {
1371 return false;
1372 }
1373
1374 return true;
1375}
1376
1377bool ValidateDrawArraysInstanced(const gl::Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
1378{
1379 if (primcount < 0)
1380 {
1381 return gl::error(GL_INVALID_VALUE, false);
1382 }
1383
1384 if (!ValidateDrawArrays(context, mode, first, count))
1385 {
1386 return false;
1387 }
1388
1389 // No-op if zero primitive count
1390 return (primcount > 0);
1391}
1392
1393bool ValidateDrawElements(const gl::Context *context, GLenum mode, GLsizei count, GLenum type, const GLvoid* indices)
1394{
Jamie Madill250d33f2014-06-06 17:09:03 -04001395 switch (type)
1396 {
1397 case GL_UNSIGNED_BYTE:
1398 case GL_UNSIGNED_SHORT:
1399 break;
1400 case GL_UNSIGNED_INT:
1401 if (!context->getCaps().extensions.elementIndexUint)
1402 {
1403 return gl::error(GL_INVALID_ENUM, false);
1404 }
1405 break;
1406 default:
1407 return gl::error(GL_INVALID_ENUM, false);
1408 }
1409
1410 gl::TransformFeedback *curTransformFeedback = context->getCurrentTransformFeedback();
1411 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused())
1412 {
1413 // It is an invalid operation to call DrawElements, DrawRangeElements or DrawElementsInstanced
1414 // while transform feedback is active, (3.0.2, section 2.14, pg 86)
1415 return gl::error(GL_INVALID_OPERATION, false);
1416 }
1417
1418 // Check for mapped buffers
Jamie Madillfd716582014-06-06 17:09:04 -04001419 if (context->hasMappedBuffer(GL_ELEMENT_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001420 {
1421 return gl::error(GL_INVALID_OPERATION, false);
1422 }
1423
Jamie Madill1aeb1312014-06-20 13:21:25 -04001424 if (!ValidateDrawBase(context, mode, count))
Jamie Madillfd716582014-06-06 17:09:04 -04001425 {
1426 return false;
1427 }
1428
1429 return true;
1430}
1431
1432bool ValidateDrawElementsInstanced(const gl::Context *context, GLenum mode, GLsizei count, GLenum type,
1433 const GLvoid *indices, GLsizei primcount)
1434{
1435 if (primcount < 0)
1436 {
1437 return gl::error(GL_INVALID_VALUE, false);
1438 }
1439
1440 if (!ValidateDrawElements(context, mode, count, type, indices))
1441 {
1442 return false;
1443 }
1444
1445 // No-op zero primitive count
1446 return (primcount > 0);
Jamie Madill250d33f2014-06-06 17:09:03 -04001447}
1448
Jamie Madill570f7c82014-07-03 10:38:54 -04001449bool ValidateFramebufferTexture2D(const gl::Context *context, GLenum target, GLenum attachment,
1450 GLenum textarget, GLuint texture, GLint level)
1451{
1452 if (context->getClientVersion() < 3 &&
1453 !ValidateES2FramebufferTextureParameters(context, target, attachment, textarget, texture, level))
1454 {
1455 return false;
1456 }
1457
1458 if (context->getClientVersion() >= 3 &&
1459 !ValidateES3FramebufferTextureParameters(context, target, attachment, textarget, texture, level, 0, false))
1460 {
1461 return false;
1462 }
1463
1464 return true;
1465}
1466
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001467}