blob: cda2bc8e10a31add79f99eec7f28c9928f9c3e36 [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{
151 int maxLevel = 0;
152 switch (target)
153 {
154 case GL_TEXTURE_2D: maxLevel = context->getMaximum2DTextureLevel(); break;
155 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:
161 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: maxLevel = context->getMaximumCubeTextureLevel(); break;
162 case GL_TEXTURE_3D: maxLevel = context->getMaximum3DTextureLevel(); break;
163 case GL_TEXTURE_2D_ARRAY: maxLevel = context->getMaximum2DArrayTextureLevel(); break;
164 default: UNREACHABLE();
165 }
166
167 return level < maxLevel;
168}
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
255 if (colorAttachment >= context->getMaximumRenderTargets())
256 {
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
327 if (std::max(width, height) > context->getMaximumRenderbufferDimension())
328 {
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
1064 if (colorAttachment >= context->getMaximumRenderTargets())
1065 {
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
Jamie Madill560a8d82014-05-21 13:06:20 -04001154 gl::Texture *texture = NULL;
1155 GLenum textureInternalFormat = GL_NONE;
1156 bool textureCompressed = false;
1157 bool textureIsDepth = false;
1158 GLint textureLevelWidth = 0;
1159 GLint textureLevelHeight = 0;
1160 GLint textureLevelDepth = 0;
Jamie Madill6f38f822014-06-06 17:12:20 -04001161 int maxDimension = 0;
Jamie Madill560a8d82014-05-21 13:06:20 -04001162
1163 switch (target)
1164 {
1165 case GL_TEXTURE_2D:
1166 {
1167 gl::Texture2D *texture2d = context->getTexture2D();
1168 if (texture2d)
1169 {
1170 textureInternalFormat = texture2d->getInternalFormat(level);
1171 textureCompressed = texture2d->isCompressed(level);
1172 textureIsDepth = texture2d->isDepth(level);
1173 textureLevelWidth = texture2d->getWidth(level);
1174 textureLevelHeight = texture2d->getHeight(level);
1175 textureLevelDepth = 1;
1176 texture = texture2d;
Jamie Madill6f38f822014-06-06 17:12:20 -04001177 maxDimension = context->getMaximum2DTextureDimension();
Jamie Madill560a8d82014-05-21 13:06:20 -04001178 }
1179 }
1180 break;
1181
1182 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1183 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1184 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1185 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1186 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1187 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1188 {
1189 gl::TextureCubeMap *textureCube = context->getTextureCubeMap();
1190 if (textureCube)
1191 {
1192 textureInternalFormat = textureCube->getInternalFormat(target, level);
1193 textureCompressed = textureCube->isCompressed(target, level);
1194 textureIsDepth = false;
1195 textureLevelWidth = textureCube->getWidth(target, level);
1196 textureLevelHeight = textureCube->getHeight(target, level);
1197 textureLevelDepth = 1;
1198 texture = textureCube;
Jamie Madill6f38f822014-06-06 17:12:20 -04001199 maxDimension = context->getMaximumCubeTextureDimension();
Jamie Madill560a8d82014-05-21 13:06:20 -04001200 }
1201 }
1202 break;
1203
1204 case GL_TEXTURE_2D_ARRAY:
1205 {
1206 gl::Texture2DArray *texture2dArray = context->getTexture2DArray();
1207 if (texture2dArray)
1208 {
1209 textureInternalFormat = texture2dArray->getInternalFormat(level);
1210 textureCompressed = texture2dArray->isCompressed(level);
1211 textureIsDepth = texture2dArray->isDepth(level);
1212 textureLevelWidth = texture2dArray->getWidth(level);
1213 textureLevelHeight = texture2dArray->getHeight(level);
1214 textureLevelDepth = texture2dArray->getLayers(level);
1215 texture = texture2dArray;
Jamie Madill6f38f822014-06-06 17:12:20 -04001216 maxDimension = context->getMaximum2DTextureDimension();
Jamie Madill560a8d82014-05-21 13:06:20 -04001217 }
1218 }
1219 break;
1220
1221 case GL_TEXTURE_3D:
1222 {
1223 gl::Texture3D *texture3d = context->getTexture3D();
1224 if (texture3d)
1225 {
1226 textureInternalFormat = texture3d->getInternalFormat(level);
1227 textureCompressed = texture3d->isCompressed(level);
1228 textureIsDepth = texture3d->isDepth(level);
1229 textureLevelWidth = texture3d->getWidth(level);
1230 textureLevelHeight = texture3d->getHeight(level);
1231 textureLevelDepth = texture3d->getDepth(level);
1232 texture = texture3d;
Jamie Madill6f38f822014-06-06 17:12:20 -04001233 maxDimension = context->getMaximum3DTextureDimension();
Jamie Madill560a8d82014-05-21 13:06:20 -04001234 }
1235 }
1236 break;
1237
1238 default:
1239 return gl::error(GL_INVALID_ENUM, false);
1240 }
1241
1242 if (!texture)
1243 {
1244 return gl::error(GL_INVALID_OPERATION, false);
1245 }
1246
1247 if (texture->isImmutable() && !isSubImage)
1248 {
1249 return gl::error(GL_INVALID_OPERATION, false);
1250 }
1251
1252 if (textureIsDepth)
1253 {
1254 return gl::error(GL_INVALID_OPERATION, false);
1255 }
1256
1257 if (textureCompressed)
1258 {
Geoff Lange4a492b2014-06-19 14:14:41 -04001259 GLint blockWidth = GetCompressedBlockWidth(textureInternalFormat);
1260 GLint blockHeight = GetCompressedBlockHeight(textureInternalFormat);
Jamie Madill560a8d82014-05-21 13:06:20 -04001261
1262 if (((width % blockWidth) != 0 && width != textureLevelWidth) ||
1263 ((height % blockHeight) != 0 && height != textureLevelHeight))
1264 {
1265 return gl::error(GL_INVALID_OPERATION, false);
1266 }
1267 }
1268
1269 if (isSubImage)
1270 {
1271 if (xoffset + width > textureLevelWidth ||
1272 yoffset + height > textureLevelHeight ||
1273 zoffset >= textureLevelDepth)
1274 {
1275 return gl::error(GL_INVALID_VALUE, false);
1276 }
1277 }
Jamie Madill6f38f822014-06-06 17:12:20 -04001278 else
1279 {
1280 if (IsCubemapTextureTarget(target) && width != height)
1281 {
1282 return gl::error(GL_INVALID_VALUE, false);
1283 }
1284
Geoff Langcec35902014-04-16 10:52:36 -04001285 if (!IsValidInternalFormat(internalformat, context->getCaps().extensions, context->getClientVersion()))
Jamie Madill6f38f822014-06-06 17:12:20 -04001286 {
1287 return gl::error(GL_INVALID_ENUM, false);
1288 }
1289
1290 int maxLevelDimension = (maxDimension >> level);
1291 if (static_cast<int>(width) > maxLevelDimension || static_cast<int>(height) > maxLevelDimension)
1292 {
1293 return gl::error(GL_INVALID_VALUE, false);
1294 }
1295 }
Jamie Madill560a8d82014-05-21 13:06:20 -04001296
1297 *textureFormatOut = textureInternalFormat;
1298 return true;
1299}
1300
Jamie Madill1aeb1312014-06-20 13:21:25 -04001301static bool ValidateDrawBase(const gl::Context *context, GLenum mode, GLsizei count)
Jamie Madill250d33f2014-06-06 17:09:03 -04001302{
Jamie Madill1aeb1312014-06-20 13:21:25 -04001303 switch (mode)
1304 {
1305 case GL_POINTS:
1306 case GL_LINES:
1307 case GL_LINE_LOOP:
1308 case GL_LINE_STRIP:
1309 case GL_TRIANGLES:
1310 case GL_TRIANGLE_STRIP:
1311 case GL_TRIANGLE_FAN:
1312 break;
1313 default:
1314 return gl::error(GL_INVALID_ENUM, false);
1315 }
1316
Jamie Madill250d33f2014-06-06 17:09:03 -04001317 if (count < 0)
1318 {
1319 return gl::error(GL_INVALID_VALUE, false);
1320 }
1321
Jamie Madill250d33f2014-06-06 17:09:03 -04001322 // Check for mapped buffers
Jamie Madillfd716582014-06-06 17:09:04 -04001323 if (context->hasMappedBuffer(GL_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001324 {
1325 return gl::error(GL_INVALID_OPERATION, false);
1326 }
1327
Jamie Madillac528012014-06-20 13:21:23 -04001328 const gl::DepthStencilState &depthStencilState = context->getDepthStencilState();
1329 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
1330 context->getStencilRef() != context->getStencilBackRef() ||
1331 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
1332 {
1333 // Note: these separate values are not supported in WebGL, due to D3D's limitations.
1334 // See Section 6.10 of the WebGL 1.0 spec
1335 ERR("This ANGLE implementation does not support separate front/back stencil "
1336 "writemasks, reference values, or stencil mask values.");
1337 return gl::error(GL_INVALID_OPERATION, false);
1338 }
1339
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001340 const gl::Framebuffer *fbo = context->getDrawFramebuffer();
1341 if (!fbo || fbo->completeness() != GL_FRAMEBUFFER_COMPLETE)
1342 {
1343 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
1344 }
1345
Jamie Madill250d33f2014-06-06 17:09:03 -04001346 // No-op if zero count
1347 return (count > 0);
1348}
1349
Jamie Madillfd716582014-06-06 17:09:04 -04001350bool ValidateDrawArrays(const gl::Context *context, GLenum mode, GLint first, GLsizei count)
Jamie Madill250d33f2014-06-06 17:09:03 -04001351{
Jamie Madillfd716582014-06-06 17:09:04 -04001352 if (first < 0)
Jamie Madill250d33f2014-06-06 17:09:03 -04001353 {
1354 return gl::error(GL_INVALID_VALUE, false);
1355 }
1356
Jamie Madillfd716582014-06-06 17:09:04 -04001357 gl::TransformFeedback *curTransformFeedback = context->getCurrentTransformFeedback();
1358 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused() &&
1359 curTransformFeedback->getDrawMode() != mode)
1360 {
1361 // It is an invalid operation to call DrawArrays or DrawArraysInstanced with a draw mode
1362 // that does not match the current transform feedback object's draw mode (if transform feedback
1363 // is active), (3.0.2, section 2.14, pg 86)
1364 return gl::error(GL_INVALID_OPERATION, false);
1365 }
1366
Jamie Madill1aeb1312014-06-20 13:21:25 -04001367 if (!ValidateDrawBase(context, mode, count))
Jamie Madillfd716582014-06-06 17:09:04 -04001368 {
1369 return false;
1370 }
1371
1372 return true;
1373}
1374
1375bool ValidateDrawArraysInstanced(const gl::Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
1376{
1377 if (primcount < 0)
1378 {
1379 return gl::error(GL_INVALID_VALUE, false);
1380 }
1381
1382 if (!ValidateDrawArrays(context, mode, first, count))
1383 {
1384 return false;
1385 }
1386
1387 // No-op if zero primitive count
1388 return (primcount > 0);
1389}
1390
1391bool ValidateDrawElements(const gl::Context *context, GLenum mode, GLsizei count, GLenum type, const GLvoid* indices)
1392{
Jamie Madill250d33f2014-06-06 17:09:03 -04001393 switch (type)
1394 {
1395 case GL_UNSIGNED_BYTE:
1396 case GL_UNSIGNED_SHORT:
1397 break;
1398 case GL_UNSIGNED_INT:
1399 if (!context->getCaps().extensions.elementIndexUint)
1400 {
1401 return gl::error(GL_INVALID_ENUM, false);
1402 }
1403 break;
1404 default:
1405 return gl::error(GL_INVALID_ENUM, false);
1406 }
1407
1408 gl::TransformFeedback *curTransformFeedback = context->getCurrentTransformFeedback();
1409 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused())
1410 {
1411 // It is an invalid operation to call DrawElements, DrawRangeElements or DrawElementsInstanced
1412 // while transform feedback is active, (3.0.2, section 2.14, pg 86)
1413 return gl::error(GL_INVALID_OPERATION, false);
1414 }
1415
1416 // Check for mapped buffers
Jamie Madillfd716582014-06-06 17:09:04 -04001417 if (context->hasMappedBuffer(GL_ELEMENT_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001418 {
1419 return gl::error(GL_INVALID_OPERATION, false);
1420 }
1421
Jamie Madill1aeb1312014-06-20 13:21:25 -04001422 if (!ValidateDrawBase(context, mode, count))
Jamie Madillfd716582014-06-06 17:09:04 -04001423 {
1424 return false;
1425 }
1426
1427 return true;
1428}
1429
1430bool ValidateDrawElementsInstanced(const gl::Context *context, GLenum mode, GLsizei count, GLenum type,
1431 const GLvoid *indices, GLsizei primcount)
1432{
1433 if (primcount < 0)
1434 {
1435 return gl::error(GL_INVALID_VALUE, false);
1436 }
1437
1438 if (!ValidateDrawElements(context, mode, count, type, indices))
1439 {
1440 return false;
1441 }
1442
1443 // No-op zero primitive count
1444 return (primcount > 0);
Jamie Madill250d33f2014-06-06 17:09:03 -04001445}
1446
Jamie Madill570f7c82014-07-03 10:38:54 -04001447bool ValidateFramebufferTexture2D(const gl::Context *context, GLenum target, GLenum attachment,
1448 GLenum textarget, GLuint texture, GLint level)
1449{
1450 if (context->getClientVersion() < 3 &&
1451 !ValidateES2FramebufferTextureParameters(context, target, attachment, textarget, texture, level))
1452 {
1453 return false;
1454 }
1455
1456 if (context->getClientVersion() >= 3 &&
1457 !ValidateES3FramebufferTextureParameters(context, target, attachment, textarget, texture, level, 0, false))
1458 {
1459 return false;
1460 }
1461
1462 return true;
1463}
1464
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001465}