blob: cc6f91ca445458aff04df4f659bccb369a67f5d7 [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 {
Jamie Madille92a3542014-07-03 10:38:58 -0400545 FramebufferAttachment *attachment = drawFramebuffer->getColorbuffer(colorAttachment);
546 ASSERT(attachment);
547
548 if (attachment->type() != GL_TEXTURE_2D && attachment->type() != GL_RENDERBUFFER)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400549 {
550 return gl::error(GL_INVALID_OPERATION, false);
551 }
552
Jamie Madille92a3542014-07-03 10:38:58 -0400553 if (attachment->getActualFormat() != readColorBuffer->getActualFormat())
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400554 {
555 return gl::error(GL_INVALID_OPERATION, false);
556 }
557 }
558 }
Geoff Lang125deab2013-08-09 13:34:16 -0400559 if (readFramebuffer->getSamples() != 0 && IsPartialBlit(context, readColorBuffer, drawColorBuffer,
560 srcX0, srcY0, srcX1, srcY1,
561 dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400562 {
563 return gl::error(GL_INVALID_OPERATION, false);
564 }
565 }
566 }
567 }
568
569 if (mask & GL_DEPTH_BUFFER_BIT)
570 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400571 gl::FramebufferAttachment *readDepthBuffer = readFramebuffer->getDepthbuffer();
572 gl::FramebufferAttachment *drawDepthBuffer = drawFramebuffer->getDepthbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400573
574 if (readDepthBuffer && drawDepthBuffer)
575 {
576 if (readDepthBuffer->getActualFormat() != drawDepthBuffer->getActualFormat())
577 {
578 return gl::error(GL_INVALID_OPERATION, false);
579 }
580
581 if (readDepthBuffer->getSamples() > 0 && !sameBounds)
582 {
583 return gl::error(GL_INVALID_OPERATION, false);
584 }
585
586 if (fromAngleExtension)
587 {
Geoff Lang125deab2013-08-09 13:34:16 -0400588 if (IsPartialBlit(context, readDepthBuffer, drawDepthBuffer,
589 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400590 {
591 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
592 return gl::error(GL_INVALID_OPERATION, false); // only whole-buffer copies are permitted
593 }
594
595 if (readDepthBuffer->getSamples() != 0 || drawDepthBuffer->getSamples() != 0)
596 {
597 return gl::error(GL_INVALID_OPERATION, false);
598 }
599 }
600 }
601 }
602
603 if (mask & GL_STENCIL_BUFFER_BIT)
604 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400605 gl::FramebufferAttachment *readStencilBuffer = readFramebuffer->getStencilbuffer();
606 gl::FramebufferAttachment *drawStencilBuffer = drawFramebuffer->getStencilbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400607
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400608 if (readStencilBuffer && drawStencilBuffer)
609 {
610 if (readStencilBuffer->getActualFormat() != drawStencilBuffer->getActualFormat())
611 {
612 return gl::error(GL_INVALID_OPERATION, false);
613 }
614
615 if (readStencilBuffer->getSamples() > 0 && !sameBounds)
616 {
617 return gl::error(GL_INVALID_OPERATION, false);
618 }
619
620 if (fromAngleExtension)
621 {
Geoff Lang125deab2013-08-09 13:34:16 -0400622 if (IsPartialBlit(context, readStencilBuffer, drawStencilBuffer,
623 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400624 {
625 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
626 return gl::error(GL_INVALID_OPERATION, false); // only whole-buffer copies are permitted
627 }
628
629 if (readStencilBuffer->getSamples() != 0 || drawStencilBuffer->getSamples() != 0)
630 {
631 return gl::error(GL_INVALID_OPERATION, false);
632 }
633 }
634 }
635 }
636
637 return true;
638}
639
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400640bool ValidateGetVertexAttribParameters(GLenum pname, int clientVersion)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400641{
642 switch (pname)
643 {
644 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
645 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
646 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
647 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
648 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
649 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
650 case GL_CURRENT_VERTEX_ATTRIB:
651 return true;
652
653 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR:
654 // Don't verify ES3 context because GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE uses
655 // the same constant.
656 META_ASSERT(GL_VERTEX_ATTRIB_ARRAY_DIVISOR == GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE);
657 return true;
658
659 case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
660 return ((clientVersion >= 3) ? true : gl::error(GL_INVALID_ENUM, false));
661
662 default:
663 return gl::error(GL_INVALID_ENUM, false);
664 }
665}
666
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400667bool ValidateTexParamParameters(gl::Context *context, GLenum pname, GLint param)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400668{
669 switch (pname)
670 {
671 case GL_TEXTURE_WRAP_R:
672 case GL_TEXTURE_SWIZZLE_R:
673 case GL_TEXTURE_SWIZZLE_G:
674 case GL_TEXTURE_SWIZZLE_B:
675 case GL_TEXTURE_SWIZZLE_A:
676 case GL_TEXTURE_BASE_LEVEL:
677 case GL_TEXTURE_MAX_LEVEL:
678 case GL_TEXTURE_COMPARE_MODE:
679 case GL_TEXTURE_COMPARE_FUNC:
680 case GL_TEXTURE_MIN_LOD:
681 case GL_TEXTURE_MAX_LOD:
682 if (context->getClientVersion() < 3)
683 {
684 return gl::error(GL_INVALID_ENUM, false);
685 }
686 break;
687
688 default: break;
689 }
690
691 switch (pname)
692 {
693 case GL_TEXTURE_WRAP_S:
694 case GL_TEXTURE_WRAP_T:
695 case GL_TEXTURE_WRAP_R:
696 switch (param)
697 {
698 case GL_REPEAT:
699 case GL_CLAMP_TO_EDGE:
700 case GL_MIRRORED_REPEAT:
701 return true;
702 default:
703 return gl::error(GL_INVALID_ENUM, false);
704 }
705
706 case GL_TEXTURE_MIN_FILTER:
707 switch (param)
708 {
709 case GL_NEAREST:
710 case GL_LINEAR:
711 case GL_NEAREST_MIPMAP_NEAREST:
712 case GL_LINEAR_MIPMAP_NEAREST:
713 case GL_NEAREST_MIPMAP_LINEAR:
714 case GL_LINEAR_MIPMAP_LINEAR:
715 return true;
716 default:
717 return gl::error(GL_INVALID_ENUM, false);
718 }
719 break;
720
721 case GL_TEXTURE_MAG_FILTER:
722 switch (param)
723 {
724 case GL_NEAREST:
725 case GL_LINEAR:
726 return true;
727 default:
728 return gl::error(GL_INVALID_ENUM, false);
729 }
730 break;
731
732 case GL_TEXTURE_USAGE_ANGLE:
733 switch (param)
734 {
735 case GL_NONE:
736 case GL_FRAMEBUFFER_ATTACHMENT_ANGLE:
737 return true;
738 default:
739 return gl::error(GL_INVALID_ENUM, false);
740 }
741 break;
742
743 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
Geoff Langcec35902014-04-16 10:52:36 -0400744 if (!context->getCaps().extensions.textureFilterAnisotropic)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400745 {
746 return gl::error(GL_INVALID_ENUM, false);
747 }
748
749 // we assume the parameter passed to this validation method is truncated, not rounded
750 if (param < 1)
751 {
752 return gl::error(GL_INVALID_VALUE, false);
753 }
754 return true;
755
756 case GL_TEXTURE_MIN_LOD:
757 case GL_TEXTURE_MAX_LOD:
758 // any value is permissible
759 return true;
760
761 case GL_TEXTURE_COMPARE_MODE:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400762 // Acceptable mode parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400763 switch (param)
764 {
765 case GL_NONE:
766 case GL_COMPARE_REF_TO_TEXTURE:
767 return true;
768 default:
769 return gl::error(GL_INVALID_ENUM, false);
770 }
771 break;
772
773 case GL_TEXTURE_COMPARE_FUNC:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400774 // Acceptable function parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400775 switch (param)
776 {
777 case GL_LEQUAL:
778 case GL_GEQUAL:
779 case GL_LESS:
780 case GL_GREATER:
781 case GL_EQUAL:
782 case GL_NOTEQUAL:
783 case GL_ALWAYS:
784 case GL_NEVER:
785 return true;
786 default:
787 return gl::error(GL_INVALID_ENUM, false);
788 }
789 break;
790
791 case GL_TEXTURE_SWIZZLE_R:
792 case GL_TEXTURE_SWIZZLE_G:
793 case GL_TEXTURE_SWIZZLE_B:
794 case GL_TEXTURE_SWIZZLE_A:
Geoff Langbc90a482013-09-17 16:51:27 -0400795 switch (param)
796 {
797 case GL_RED:
798 case GL_GREEN:
799 case GL_BLUE:
800 case GL_ALPHA:
801 case GL_ZERO:
802 case GL_ONE:
803 return true;
804 default:
805 return gl::error(GL_INVALID_ENUM, false);
806 }
807 break;
808
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400809 case GL_TEXTURE_BASE_LEVEL:
810 case GL_TEXTURE_MAX_LEVEL:
Nicolas Capens8de68282014-04-04 11:10:27 -0400811 if (param < 0)
812 {
813 return gl::error(GL_INVALID_VALUE, false);
814 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400815 return true;
816
817 default:
818 return gl::error(GL_INVALID_ENUM, false);
819 }
820}
821
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400822bool ValidateSamplerObjectParameter(GLenum pname)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400823{
824 switch (pname)
825 {
826 case GL_TEXTURE_MIN_FILTER:
827 case GL_TEXTURE_MAG_FILTER:
828 case GL_TEXTURE_WRAP_S:
829 case GL_TEXTURE_WRAP_T:
830 case GL_TEXTURE_WRAP_R:
831 case GL_TEXTURE_MIN_LOD:
832 case GL_TEXTURE_MAX_LOD:
833 case GL_TEXTURE_COMPARE_MODE:
834 case GL_TEXTURE_COMPARE_FUNC:
835 return true;
836
837 default:
838 return gl::error(GL_INVALID_ENUM, false);
839 }
840}
841
Jamie Madill26e91952014-03-05 15:01:27 -0500842bool ValidateReadPixelsParameters(gl::Context *context, GLint x, GLint y, GLsizei width, GLsizei height,
843 GLenum format, GLenum type, GLsizei *bufSize, GLvoid *pixels)
844{
845 gl::Framebuffer *framebuffer = context->getReadFramebuffer();
Jamie Madill893ab082014-05-16 16:56:10 -0400846 ASSERT(framebuffer);
Jamie Madill26e91952014-03-05 15:01:27 -0500847
848 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
849 {
850 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
851 }
852
853 if (context->getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
854 {
855 return gl::error(GL_INVALID_OPERATION, false);
856 }
857
Jamie Madill893ab082014-05-16 16:56:10 -0400858 if (!framebuffer->getReadColorbuffer())
859 {
860 return gl::error(GL_INVALID_OPERATION, false);
861 }
862
Jamie Madill26e91952014-03-05 15:01:27 -0500863 GLenum currentInternalFormat, currentFormat, currentType;
Geoff Lange4a492b2014-06-19 14:14:41 -0400864 GLuint clientVersion = context->getClientVersion();
Jamie Madill26e91952014-03-05 15:01:27 -0500865
Jamie Madill893ab082014-05-16 16:56:10 -0400866 context->getCurrentReadFormatType(&currentInternalFormat, &currentFormat, &currentType);
Jamie Madill26e91952014-03-05 15:01:27 -0500867
Geoff Langbdc9b2f2014-04-16 14:41:54 -0400868 bool validReadFormat = (clientVersion < 3) ? ValidES2ReadFormatType(context, format, type) :
869 ValidES3ReadFormatType(context, currentInternalFormat, format, type);
Jamie Madill26e91952014-03-05 15:01:27 -0500870
871 if (!(currentFormat == format && currentType == type) && !validReadFormat)
872 {
873 return gl::error(GL_INVALID_OPERATION, false);
874 }
875
Geoff Lange4a492b2014-06-19 14:14:41 -0400876 GLenum sizedInternalFormat = IsSizedInternalFormat(format) ? format
877 : GetSizedInternalFormat(format, type);
Jamie Madill26e91952014-03-05 15:01:27 -0500878
Geoff Lange4a492b2014-06-19 14:14:41 -0400879 GLsizei outputPitch = GetRowPitch(sizedInternalFormat, type, width, context->getPackAlignment());
Jamie Madill26e91952014-03-05 15:01:27 -0500880 // sized query sanity check
881 if (bufSize)
882 {
883 int requiredSize = outputPitch * height;
884 if (requiredSize > *bufSize)
885 {
886 return gl::error(GL_INVALID_OPERATION, false);
887 }
888 }
889
890 return true;
891}
892
Jamie Madilldb2f14c2014-05-13 13:56:30 -0400893bool ValidateBeginQuery(gl::Context *context, GLenum target, GLuint id)
894{
895 if (!ValidQueryType(context, target))
896 {
897 return gl::error(GL_INVALID_ENUM, false);
898 }
899
900 if (id == 0)
901 {
902 return gl::error(GL_INVALID_OPERATION, false);
903 }
904
905 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
906 // of zero, if the active query object name for <target> is non-zero (for the
907 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
908 // the active query for either target is non-zero), if <id> is the name of an
909 // existing query object whose type does not match <target>, or if <id> is the
910 // active query object name for any query type, the error INVALID_OPERATION is
911 // generated.
912
913 // Ensure no other queries are active
914 // NOTE: If other queries than occlusion are supported, we will need to check
915 // separately that:
916 // a) The query ID passed is not the current active query for any target/type
917 // b) There are no active queries for the requested target (and in the case
918 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
919 // no query may be active for either if glBeginQuery targets either.
920 if (context->isQueryActive())
921 {
922 return gl::error(GL_INVALID_OPERATION, false);
923 }
924
925 Query *queryObject = context->getQuery(id, true, target);
926
927 // check that name was obtained with glGenQueries
928 if (!queryObject)
929 {
930 return gl::error(GL_INVALID_OPERATION, false);
931 }
932
933 // check for type mismatch
934 if (queryObject->getType() != target)
935 {
936 return gl::error(GL_INVALID_OPERATION, false);
937 }
938
939 return true;
940}
941
Jamie Madill45c785d2014-05-13 14:09:34 -0400942bool ValidateEndQuery(gl::Context *context, GLenum target)
943{
944 if (!ValidQueryType(context, target))
945 {
946 return gl::error(GL_INVALID_ENUM, false);
947 }
948
949 const Query *queryObject = context->getActiveQuery(target);
950
951 if (queryObject == NULL)
952 {
953 return gl::error(GL_INVALID_OPERATION, false);
954 }
955
956 if (!queryObject->isStarted())
957 {
958 return gl::error(GL_INVALID_OPERATION, false);
959 }
960
961 return true;
962}
963
Jamie Madill36398922014-05-20 14:51:53 -0400964static bool ValidateUniformCommonBase(gl::Context *context, GLenum targetUniformType,
965 GLint location, GLsizei count, LinkedUniform **uniformOut)
Jamie Madilld7c7bb22014-05-20 10:55:54 -0400966{
967 if (count < 0)
968 {
969 return gl::error(GL_INVALID_VALUE, false);
970 }
971
Jamie Madilld7c7bb22014-05-20 10:55:54 -0400972 gl::ProgramBinary *programBinary = context->getCurrentProgramBinary();
973 if (!programBinary)
974 {
975 return gl::error(GL_INVALID_OPERATION, false);
976 }
977
978 if (location == -1)
979 {
980 // Silently ignore the uniform command
981 return false;
982 }
983
Jamie Madill36398922014-05-20 14:51:53 -0400984 if (!programBinary->isValidUniformLocation(location))
985 {
986 return gl::error(GL_INVALID_OPERATION, false);
987 }
988
989 LinkedUniform *uniform = programBinary->getUniformByLocation(location);
990
991 // attempting to write an array to a non-array uniform is an INVALID_OPERATION
992 if (uniform->elementCount() == 1 && count > 1)
993 {
994 return gl::error(GL_INVALID_OPERATION, false);
995 }
996
997 *uniformOut = uniform;
Jamie Madilld7c7bb22014-05-20 10:55:54 -0400998 return true;
999}
1000
Jamie Madillaa981bd2014-05-20 10:55:55 -04001001bool ValidateUniform(gl::Context *context, GLenum uniformType, GLint location, GLsizei count)
1002{
1003 // Check for ES3 uniform entry points
Jamie Madillf2575982014-06-25 16:04:54 -04001004 if (VariableComponentType(uniformType) == GL_UNSIGNED_INT && context->getClientVersion() < 3)
Jamie Madillaa981bd2014-05-20 10:55:55 -04001005 {
1006 return gl::error(GL_INVALID_OPERATION, false);
1007 }
1008
Jamie Madill36398922014-05-20 14:51:53 -04001009 LinkedUniform *uniform = NULL;
1010 if (!ValidateUniformCommonBase(context, uniformType, location, count, &uniform))
1011 {
1012 return false;
1013 }
1014
Jamie Madillf2575982014-06-25 16:04:54 -04001015 GLenum targetBoolType = VariableBoolVectorType(uniformType);
Jamie Madill36398922014-05-20 14:51:53 -04001016 bool samplerUniformCheck = (IsSampler(uniform->type) && uniformType == GL_INT);
1017 if (!samplerUniformCheck && uniformType != uniform->type && targetBoolType != uniform->type)
1018 {
1019 return gl::error(GL_INVALID_OPERATION, false);
1020 }
1021
1022 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001023}
1024
1025bool ValidateUniformMatrix(gl::Context *context, GLenum matrixType, GLint location, GLsizei count,
1026 GLboolean transpose)
1027{
1028 // Check for ES3 uniform entry points
1029 int rows = VariableRowCount(matrixType);
1030 int cols = VariableColumnCount(matrixType);
1031 if (rows != cols && context->getClientVersion() < 3)
1032 {
1033 return gl::error(GL_INVALID_OPERATION, false);
1034 }
1035
1036 if (transpose != GL_FALSE && context->getClientVersion() < 3)
1037 {
1038 return gl::error(GL_INVALID_VALUE, false);
1039 }
1040
Jamie Madill36398922014-05-20 14:51:53 -04001041 LinkedUniform *uniform = NULL;
1042 if (!ValidateUniformCommonBase(context, matrixType, location, count, &uniform))
1043 {
1044 return false;
1045 }
1046
1047 if (uniform->type != matrixType)
1048 {
1049 return gl::error(GL_INVALID_OPERATION, false);
1050 }
1051
1052 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001053}
1054
Jamie Madill893ab082014-05-16 16:56:10 -04001055bool ValidateStateQuery(gl::Context *context, GLenum pname, GLenum *nativeType, unsigned int *numParams)
1056{
1057 if (!context->getQueryParameterInfo(pname, nativeType, numParams))
1058 {
1059 return gl::error(GL_INVALID_ENUM, false);
1060 }
1061
1062 if (pname >= GL_DRAW_BUFFER0 && pname <= GL_DRAW_BUFFER15)
1063 {
1064 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0);
1065
Geoff Langaae65a42014-05-26 12:43:44 -04001066 if (colorAttachment >= context->getCaps().maxDrawBuffers)
Jamie Madill893ab082014-05-16 16:56:10 -04001067 {
1068 return gl::error(GL_INVALID_OPERATION, false);
1069 }
1070 }
1071
1072 switch (pname)
1073 {
1074 case GL_TEXTURE_BINDING_2D:
1075 case GL_TEXTURE_BINDING_CUBE_MAP:
1076 case GL_TEXTURE_BINDING_3D:
1077 case GL_TEXTURE_BINDING_2D_ARRAY:
1078 if (context->getActiveSampler() >= context->getMaximumCombinedTextureImageUnits())
1079 {
1080 return gl::error(GL_INVALID_OPERATION, false);
1081 }
1082 break;
1083
1084 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1085 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1086 {
1087 Framebuffer *framebuffer = context->getReadFramebuffer();
1088 ASSERT(framebuffer);
1089 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1090 {
1091 return gl::error(GL_INVALID_OPERATION, false);
1092 }
1093
Jamie Madill3c7fa222014-06-05 13:08:51 -04001094 FramebufferAttachment *attachment = framebuffer->getReadColorbuffer();
1095 if (!attachment)
Jamie Madill893ab082014-05-16 16:56:10 -04001096 {
1097 return gl::error(GL_INVALID_OPERATION, false);
1098 }
1099 }
1100 break;
1101
1102 default:
1103 break;
1104 }
1105
1106 // pname is valid, but there are no parameters to return
1107 if (numParams == 0)
1108 {
1109 return false;
1110 }
1111
1112 return true;
1113}
1114
Jamie Madill560a8d82014-05-21 13:06:20 -04001115bool ValidateCopyTexImageParametersBase(gl::Context* context, GLenum target, GLint level, GLenum internalformat, bool isSubImage,
1116 GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height,
1117 GLint border, GLenum *textureFormatOut)
1118{
1119
1120 if (!ValidTexture2DDestinationTarget(context, target))
1121 {
1122 return gl::error(GL_INVALID_ENUM, false);
1123 }
1124
1125 if (level < 0 || xoffset < 0 || yoffset < 0 || zoffset < 0 || width < 0 || height < 0)
1126 {
1127 return gl::error(GL_INVALID_VALUE, false);
1128 }
1129
1130 if (std::numeric_limits<GLsizei>::max() - xoffset < width || std::numeric_limits<GLsizei>::max() - yoffset < height)
1131 {
1132 return gl::error(GL_INVALID_VALUE, false);
1133 }
1134
1135 if (border != 0)
1136 {
1137 return gl::error(GL_INVALID_VALUE, false);
1138 }
1139
1140 if (!ValidMipLevel(context, target, level))
1141 {
1142 return gl::error(GL_INVALID_VALUE, false);
1143 }
1144
1145 gl::Framebuffer *framebuffer = context->getReadFramebuffer();
1146 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1147 {
1148 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
1149 }
1150
1151 if (context->getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
1152 {
1153 return gl::error(GL_INVALID_OPERATION, false);
1154 }
1155
Geoff Langaae65a42014-05-26 12:43:44 -04001156 const gl::Caps &caps = context->getCaps();
1157
Jamie Madill560a8d82014-05-21 13:06:20 -04001158 gl::Texture *texture = NULL;
1159 GLenum textureInternalFormat = GL_NONE;
1160 bool textureCompressed = false;
1161 bool textureIsDepth = false;
1162 GLint textureLevelWidth = 0;
1163 GLint textureLevelHeight = 0;
1164 GLint textureLevelDepth = 0;
Geoff Langaae65a42014-05-26 12:43:44 -04001165 GLuint maxDimension = 0;
Jamie Madill560a8d82014-05-21 13:06:20 -04001166
1167 switch (target)
1168 {
1169 case GL_TEXTURE_2D:
1170 {
1171 gl::Texture2D *texture2d = context->getTexture2D();
1172 if (texture2d)
1173 {
1174 textureInternalFormat = texture2d->getInternalFormat(level);
1175 textureCompressed = texture2d->isCompressed(level);
1176 textureIsDepth = texture2d->isDepth(level);
1177 textureLevelWidth = texture2d->getWidth(level);
1178 textureLevelHeight = texture2d->getHeight(level);
1179 textureLevelDepth = 1;
1180 texture = texture2d;
Geoff Langaae65a42014-05-26 12:43:44 -04001181 maxDimension = caps.max2DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001182 }
1183 }
1184 break;
1185
1186 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1187 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1188 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1189 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1190 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1191 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1192 {
1193 gl::TextureCubeMap *textureCube = context->getTextureCubeMap();
1194 if (textureCube)
1195 {
1196 textureInternalFormat = textureCube->getInternalFormat(target, level);
1197 textureCompressed = textureCube->isCompressed(target, level);
1198 textureIsDepth = false;
1199 textureLevelWidth = textureCube->getWidth(target, level);
1200 textureLevelHeight = textureCube->getHeight(target, level);
1201 textureLevelDepth = 1;
1202 texture = textureCube;
Geoff Langaae65a42014-05-26 12:43:44 -04001203 maxDimension = caps.maxCubeMapTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001204 }
1205 }
1206 break;
1207
1208 case GL_TEXTURE_2D_ARRAY:
1209 {
1210 gl::Texture2DArray *texture2dArray = context->getTexture2DArray();
1211 if (texture2dArray)
1212 {
1213 textureInternalFormat = texture2dArray->getInternalFormat(level);
1214 textureCompressed = texture2dArray->isCompressed(level);
1215 textureIsDepth = texture2dArray->isDepth(level);
1216 textureLevelWidth = texture2dArray->getWidth(level);
1217 textureLevelHeight = texture2dArray->getHeight(level);
1218 textureLevelDepth = texture2dArray->getLayers(level);
1219 texture = texture2dArray;
Geoff Langaae65a42014-05-26 12:43:44 -04001220 maxDimension = caps.max2DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001221 }
1222 }
1223 break;
1224
1225 case GL_TEXTURE_3D:
1226 {
1227 gl::Texture3D *texture3d = context->getTexture3D();
1228 if (texture3d)
1229 {
1230 textureInternalFormat = texture3d->getInternalFormat(level);
1231 textureCompressed = texture3d->isCompressed(level);
1232 textureIsDepth = texture3d->isDepth(level);
1233 textureLevelWidth = texture3d->getWidth(level);
1234 textureLevelHeight = texture3d->getHeight(level);
1235 textureLevelDepth = texture3d->getDepth(level);
1236 texture = texture3d;
Geoff Langaae65a42014-05-26 12:43:44 -04001237 maxDimension = caps.max3DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001238 }
1239 }
1240 break;
1241
1242 default:
1243 return gl::error(GL_INVALID_ENUM, false);
1244 }
1245
1246 if (!texture)
1247 {
1248 return gl::error(GL_INVALID_OPERATION, false);
1249 }
1250
1251 if (texture->isImmutable() && !isSubImage)
1252 {
1253 return gl::error(GL_INVALID_OPERATION, false);
1254 }
1255
1256 if (textureIsDepth)
1257 {
1258 return gl::error(GL_INVALID_OPERATION, false);
1259 }
1260
1261 if (textureCompressed)
1262 {
Geoff Lange4a492b2014-06-19 14:14:41 -04001263 GLint blockWidth = GetCompressedBlockWidth(textureInternalFormat);
1264 GLint blockHeight = GetCompressedBlockHeight(textureInternalFormat);
Jamie Madill560a8d82014-05-21 13:06:20 -04001265
1266 if (((width % blockWidth) != 0 && width != textureLevelWidth) ||
1267 ((height % blockHeight) != 0 && height != textureLevelHeight))
1268 {
1269 return gl::error(GL_INVALID_OPERATION, false);
1270 }
1271 }
1272
1273 if (isSubImage)
1274 {
1275 if (xoffset + width > textureLevelWidth ||
1276 yoffset + height > textureLevelHeight ||
1277 zoffset >= textureLevelDepth)
1278 {
1279 return gl::error(GL_INVALID_VALUE, false);
1280 }
1281 }
Jamie Madill6f38f822014-06-06 17:12:20 -04001282 else
1283 {
1284 if (IsCubemapTextureTarget(target) && width != height)
1285 {
1286 return gl::error(GL_INVALID_VALUE, false);
1287 }
1288
Geoff Langcec35902014-04-16 10:52:36 -04001289 if (!IsValidInternalFormat(internalformat, context->getCaps().extensions, context->getClientVersion()))
Jamie Madill6f38f822014-06-06 17:12:20 -04001290 {
1291 return gl::error(GL_INVALID_ENUM, false);
1292 }
1293
1294 int maxLevelDimension = (maxDimension >> level);
1295 if (static_cast<int>(width) > maxLevelDimension || static_cast<int>(height) > maxLevelDimension)
1296 {
1297 return gl::error(GL_INVALID_VALUE, false);
1298 }
1299 }
Jamie Madill560a8d82014-05-21 13:06:20 -04001300
1301 *textureFormatOut = textureInternalFormat;
1302 return true;
1303}
1304
Jamie Madill1aeb1312014-06-20 13:21:25 -04001305static bool ValidateDrawBase(const gl::Context *context, GLenum mode, GLsizei count)
Jamie Madill250d33f2014-06-06 17:09:03 -04001306{
Jamie Madill1aeb1312014-06-20 13:21:25 -04001307 switch (mode)
1308 {
1309 case GL_POINTS:
1310 case GL_LINES:
1311 case GL_LINE_LOOP:
1312 case GL_LINE_STRIP:
1313 case GL_TRIANGLES:
1314 case GL_TRIANGLE_STRIP:
1315 case GL_TRIANGLE_FAN:
1316 break;
1317 default:
1318 return gl::error(GL_INVALID_ENUM, false);
1319 }
1320
Jamie Madill250d33f2014-06-06 17:09:03 -04001321 if (count < 0)
1322 {
1323 return gl::error(GL_INVALID_VALUE, false);
1324 }
1325
Jamie Madill250d33f2014-06-06 17:09:03 -04001326 // Check for mapped buffers
Jamie Madillfd716582014-06-06 17:09:04 -04001327 if (context->hasMappedBuffer(GL_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001328 {
1329 return gl::error(GL_INVALID_OPERATION, false);
1330 }
1331
Jamie Madillac528012014-06-20 13:21:23 -04001332 const gl::DepthStencilState &depthStencilState = context->getDepthStencilState();
1333 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
1334 context->getStencilRef() != context->getStencilBackRef() ||
1335 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
1336 {
1337 // Note: these separate values are not supported in WebGL, due to D3D's limitations.
1338 // See Section 6.10 of the WebGL 1.0 spec
1339 ERR("This ANGLE implementation does not support separate front/back stencil "
1340 "writemasks, reference values, or stencil mask values.");
1341 return gl::error(GL_INVALID_OPERATION, false);
1342 }
1343
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001344 const gl::Framebuffer *fbo = context->getDrawFramebuffer();
1345 if (!fbo || fbo->completeness() != GL_FRAMEBUFFER_COMPLETE)
1346 {
1347 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
1348 }
1349
Jamie Madill250d33f2014-06-06 17:09:03 -04001350 // No-op if zero count
1351 return (count > 0);
1352}
1353
Jamie Madillfd716582014-06-06 17:09:04 -04001354bool ValidateDrawArrays(const gl::Context *context, GLenum mode, GLint first, GLsizei count)
Jamie Madill250d33f2014-06-06 17:09:03 -04001355{
Jamie Madillfd716582014-06-06 17:09:04 -04001356 if (first < 0)
Jamie Madill250d33f2014-06-06 17:09:03 -04001357 {
1358 return gl::error(GL_INVALID_VALUE, false);
1359 }
1360
Jamie Madillfd716582014-06-06 17:09:04 -04001361 gl::TransformFeedback *curTransformFeedback = context->getCurrentTransformFeedback();
1362 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused() &&
1363 curTransformFeedback->getDrawMode() != mode)
1364 {
1365 // It is an invalid operation to call DrawArrays or DrawArraysInstanced with a draw mode
1366 // that does not match the current transform feedback object's draw mode (if transform feedback
1367 // is active), (3.0.2, section 2.14, pg 86)
1368 return gl::error(GL_INVALID_OPERATION, false);
1369 }
1370
Jamie Madill1aeb1312014-06-20 13:21:25 -04001371 if (!ValidateDrawBase(context, mode, count))
Jamie Madillfd716582014-06-06 17:09:04 -04001372 {
1373 return false;
1374 }
1375
1376 return true;
1377}
1378
1379bool ValidateDrawArraysInstanced(const gl::Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
1380{
1381 if (primcount < 0)
1382 {
1383 return gl::error(GL_INVALID_VALUE, false);
1384 }
1385
1386 if (!ValidateDrawArrays(context, mode, first, count))
1387 {
1388 return false;
1389 }
1390
1391 // No-op if zero primitive count
1392 return (primcount > 0);
1393}
1394
1395bool ValidateDrawElements(const gl::Context *context, GLenum mode, GLsizei count, GLenum type, const GLvoid* indices)
1396{
Jamie Madill250d33f2014-06-06 17:09:03 -04001397 switch (type)
1398 {
1399 case GL_UNSIGNED_BYTE:
1400 case GL_UNSIGNED_SHORT:
1401 break;
1402 case GL_UNSIGNED_INT:
1403 if (!context->getCaps().extensions.elementIndexUint)
1404 {
1405 return gl::error(GL_INVALID_ENUM, false);
1406 }
1407 break;
1408 default:
1409 return gl::error(GL_INVALID_ENUM, false);
1410 }
1411
1412 gl::TransformFeedback *curTransformFeedback = context->getCurrentTransformFeedback();
1413 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused())
1414 {
1415 // It is an invalid operation to call DrawElements, DrawRangeElements or DrawElementsInstanced
1416 // while transform feedback is active, (3.0.2, section 2.14, pg 86)
1417 return gl::error(GL_INVALID_OPERATION, false);
1418 }
1419
1420 // Check for mapped buffers
Jamie Madillfd716582014-06-06 17:09:04 -04001421 if (context->hasMappedBuffer(GL_ELEMENT_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001422 {
1423 return gl::error(GL_INVALID_OPERATION, false);
1424 }
1425
Jamie Madill1aeb1312014-06-20 13:21:25 -04001426 if (!ValidateDrawBase(context, mode, count))
Jamie Madillfd716582014-06-06 17:09:04 -04001427 {
1428 return false;
1429 }
1430
1431 return true;
1432}
1433
1434bool ValidateDrawElementsInstanced(const gl::Context *context, GLenum mode, GLsizei count, GLenum type,
1435 const GLvoid *indices, GLsizei primcount)
1436{
1437 if (primcount < 0)
1438 {
1439 return gl::error(GL_INVALID_VALUE, false);
1440 }
1441
1442 if (!ValidateDrawElements(context, mode, count, type, indices))
1443 {
1444 return false;
1445 }
1446
1447 // No-op zero primitive count
1448 return (primcount > 0);
Jamie Madill250d33f2014-06-06 17:09:03 -04001449}
1450
Jamie Madill55ec3b12014-07-03 10:38:57 -04001451bool ValidateFramebufferTextureBase(const gl::Context *context, GLenum target, GLenum attachment,
1452 GLuint texture, GLint level)
Jamie Madill570f7c82014-07-03 10:38:54 -04001453{
Jamie Madill55ec3b12014-07-03 10:38:57 -04001454 if (!ValidFramebufferTarget(target))
1455 {
1456 return gl::error(GL_INVALID_ENUM, false);
1457 }
1458
1459 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill570f7c82014-07-03 10:38:54 -04001460 {
1461 return false;
1462 }
1463
Jamie Madill55ec3b12014-07-03 10:38:57 -04001464 if (texture != 0)
1465 {
1466 gl::Texture *tex = context->getTexture(texture);
1467
1468 if (tex == NULL)
1469 {
1470 return gl::error(GL_INVALID_OPERATION, false);
1471 }
1472
1473 if (level < 0)
1474 {
1475 return gl::error(GL_INVALID_VALUE, false);
1476 }
1477 }
1478
1479 const gl::Framebuffer *framebuffer = context->getTargetFramebuffer(target);
1480 GLuint framebufferHandle = context->getTargetFramebufferHandle(target);
1481
1482 if (framebufferHandle == 0 || !framebuffer)
1483 {
1484 return gl::error(GL_INVALID_OPERATION, false);
1485 }
1486
1487 return true;
1488}
1489
1490bool ValidateFramebufferTexture2D(const gl::Context *context, GLenum target, GLenum attachment,
1491 GLenum textarget, GLuint texture, GLint level)
1492{
1493 // Attachments are required to be bound to level 0 in ES2
1494 if (context->getClientVersion() < 3 && level != 0)
1495 {
1496 return gl::error(GL_INVALID_VALUE, false);
1497 }
1498
1499 if (!ValidateFramebufferTextureBase(context, target, attachment, texture, level))
Jamie Madill570f7c82014-07-03 10:38:54 -04001500 {
1501 return false;
1502 }
1503
Jamie Madill55ec3b12014-07-03 10:38:57 -04001504 if (texture != 0)
1505 {
1506 gl::Texture *tex = context->getTexture(texture);
1507 ASSERT(tex);
1508
Jamie Madill2a6564e2014-07-11 09:53:19 -04001509 const gl::Caps &caps = context->getCaps();
1510
Jamie Madill55ec3b12014-07-03 10:38:57 -04001511 switch (textarget)
1512 {
1513 case GL_TEXTURE_2D:
1514 {
Jamie Madill2a6564e2014-07-11 09:53:19 -04001515 if (level > gl::log2(caps.max2DTextureSize))
Jamie Madill55ec3b12014-07-03 10:38:57 -04001516 {
1517 return gl::error(GL_INVALID_VALUE, false);
1518 }
1519 if (tex->getTarget() != GL_TEXTURE_2D)
1520 {
1521 return gl::error(GL_INVALID_OPERATION, false);
1522 }
1523 gl::Texture2D *tex2d = static_cast<gl::Texture2D *>(tex);
1524 if (tex2d->isCompressed(level))
1525 {
1526 return gl::error(GL_INVALID_OPERATION, false);
1527 }
1528 }
1529 break;
1530
1531 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1532 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1533 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1534 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1535 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1536 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1537 {
Jamie Madill2a6564e2014-07-11 09:53:19 -04001538 if (level > gl::log2(caps.maxCubeMapTextureSize))
Jamie Madill55ec3b12014-07-03 10:38:57 -04001539 {
1540 return gl::error(GL_INVALID_VALUE, false);
1541 }
1542 if (tex->getTarget() != GL_TEXTURE_CUBE_MAP)
1543 {
1544 return gl::error(GL_INVALID_OPERATION, false);
1545 }
1546 gl::TextureCubeMap *texcube = static_cast<gl::TextureCubeMap *>(tex);
1547 if (texcube->isCompressed(textarget, level))
1548 {
1549 return gl::error(GL_INVALID_OPERATION, false);
1550 }
1551 }
1552 break;
1553
1554 default:
1555 return gl::error(GL_INVALID_ENUM, false);
1556 }
1557 }
1558
Jamie Madill570f7c82014-07-03 10:38:54 -04001559 return true;
1560}
1561
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001562}