blob: ff0a1f3d2a04204b64b206c247e66da260a6faa5 [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"
16#include "libGLESv2/Renderbuffer.h"
17#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
170bool ValidImageSize(const gl::Context *context, GLenum target, GLint level, GLsizei width, GLsizei height, GLsizei depth)
171{
172 if (level < 0 || width < 0 || height < 0 || depth < 0)
173 {
174 return false;
175 }
176
Geoff Langcec35902014-04-16 10:52:36 -0400177 if (!context->getCaps().extensions.textureNPOT && (level != 0 || !gl::isPow2(width) || !gl::isPow2(height) || !gl::isPow2(depth)))
Geoff Langce635692013-09-24 13:56:32 -0400178 {
179 return false;
180 }
181
182 if (!ValidMipLevel(context, target, level))
183 {
184 return false;
185 }
186
187 return true;
188}
189
Geoff Lang005df412013-10-16 14:12:50 -0400190bool ValidCompressedImageSize(const gl::Context *context, GLenum internalFormat, GLsizei width, GLsizei height)
Geoff Langd4f180b2013-09-24 13:57:44 -0400191{
192 GLuint clientVersion = context->getClientVersion();
193 if (!IsFormatCompressed(internalFormat, clientVersion))
194 {
195 return false;
196 }
197
198 GLint blockWidth = GetCompressedBlockWidth(internalFormat, clientVersion);
199 GLint blockHeight = GetCompressedBlockHeight(internalFormat, clientVersion);
200 if (width < 0 || (width > blockWidth && width % blockWidth != 0) ||
201 height < 0 || (height > blockHeight && height % blockHeight != 0))
202 {
203 return false;
204 }
205
206 return true;
207}
208
Geoff Lang37dde692014-01-31 16:34:54 -0500209bool ValidQueryType(const Context *context, GLenum queryType)
210{
211 META_ASSERT(GL_ANY_SAMPLES_PASSED == GL_ANY_SAMPLES_PASSED_EXT);
212 META_ASSERT(GL_ANY_SAMPLES_PASSED_CONSERVATIVE == GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT);
213
214 switch (queryType)
215 {
216 case GL_ANY_SAMPLES_PASSED:
217 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
218 return true;
219 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
220 return (context->getClientVersion() >= 3);
221 default:
222 return false;
223 }
224}
225
Geoff Lang48dcae72014-02-05 16:28:24 -0500226bool ValidProgram(const Context *context, GLuint id)
227{
228 // ES3 spec (section 2.11.1) -- "Commands that accept shader or program object names will generate the
229 // error INVALID_VALUE if the provided name is not the name of either a shader or program object and
230 // INVALID_OPERATION if the provided name identifies an object that is not the expected type."
231
232 if (context->getProgram(id) != NULL)
233 {
234 return true;
235 }
236 else if (context->getShader(id) != NULL)
237 {
238 // ID is the wrong type
239 return gl::error(GL_INVALID_OPERATION, false);
240 }
241 else
242 {
243 // No shader/program object has this ID
244 return gl::error(GL_INVALID_VALUE, false);
245 }
246}
247
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400248bool ValidateRenderbufferStorageParameters(const gl::Context *context, GLenum target, GLsizei samples,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400249 GLenum internalformat, GLsizei width, GLsizei height,
250 bool angleExtension)
251{
252 switch (target)
253 {
254 case GL_RENDERBUFFER:
255 break;
256 default:
257 return gl::error(GL_INVALID_ENUM, false);
258 }
259
260 if (width < 0 || height < 0 || samples < 0)
261 {
262 return gl::error(GL_INVALID_VALUE, false);
263 }
264
Geoff Langcec35902014-04-16 10:52:36 -0400265 const gl::Caps &caps = context->getCaps();
266 if (!gl::IsValidInternalFormat(internalformat, caps.extensions, context->getClientVersion()))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400267 {
268 return gl::error(GL_INVALID_ENUM, false);
269 }
270
271 // ANGLE_framebuffer_multisample does not explicitly state that the internal format must be
272 // sized but it does state that the format must be in the ES2.0 spec table 4.5 which contains
273 // only sized internal formats. The ES3 spec (section 4.4.2) does, however, state that the
274 // internal format must be sized and not an integer format if samples is greater than zero.
275 if (!gl::IsSizedInternalFormat(internalformat, context->getClientVersion()))
276 {
277 return gl::error(GL_INVALID_ENUM, false);
278 }
279
Geoff Langb2f3d052013-08-13 12:49:27 -0400280 GLenum componentType = gl::GetComponentType(internalformat, context->getClientVersion());
281 if ((componentType == GL_UNSIGNED_INT || componentType == GL_INT) && samples > 0)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400282 {
283 return gl::error(GL_INVALID_OPERATION, false);
284 }
285
Geoff Langcec35902014-04-16 10:52:36 -0400286 const TextureCaps &formatCaps = caps.textureCaps.get(internalformat);
287 if (!formatCaps.colorRendering && !formatCaps.depthRendering && !formatCaps.stencilRendering)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400288 {
289 return gl::error(GL_INVALID_ENUM, false);
290 }
291
292 if (std::max(width, height) > context->getMaximumRenderbufferDimension())
293 {
294 return gl::error(GL_INVALID_VALUE, false);
295 }
296
297 // ANGLE_framebuffer_multisample states that the value of samples must be less than or equal
298 // to MAX_SAMPLES_ANGLE (Context::getMaxSupportedSamples) while the ES3.0 spec (section 4.4.2)
299 // states that samples must be less than or equal to the maximum samples for the specified
300 // internal format.
301 if (angleExtension)
302 {
303 if (samples > context->getMaxSupportedSamples())
304 {
305 return gl::error(GL_INVALID_VALUE, false);
306 }
307 }
308 else
309 {
310 if (samples > context->getMaxSupportedFormatSamples(internalformat))
311 {
312 return gl::error(GL_INVALID_VALUE, false);
313 }
314 }
315
316 GLuint handle = context->getRenderbufferHandle();
317 if (handle == 0)
318 {
319 return gl::error(GL_INVALID_OPERATION, false);
320 }
321
322 return true;
323}
324
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500325bool ValidateFramebufferRenderbufferParameters(gl::Context *context, GLenum target, GLenum attachment,
326 GLenum renderbuffertarget, GLuint renderbuffer)
327{
328 gl::Framebuffer *framebuffer = context->getTargetFramebuffer(target);
329 GLuint framebufferHandle = context->getTargetFramebufferHandle(target);
330
331 if (!framebuffer || (framebufferHandle == 0 && renderbuffer != 0))
332 {
333 return gl::error(GL_INVALID_OPERATION, false);
334 }
335
336 if (attachment >= GL_COLOR_ATTACHMENT0_EXT && attachment <= GL_COLOR_ATTACHMENT15_EXT)
337 {
338 const unsigned int colorAttachment = (attachment - GL_COLOR_ATTACHMENT0_EXT);
339
340 if (colorAttachment >= context->getMaximumRenderTargets())
341 {
342 return gl::error(GL_INVALID_VALUE, false);
343 }
344 }
345 else
346 {
347 switch (attachment)
348 {
349 case GL_DEPTH_ATTACHMENT:
350 break;
351 case GL_STENCIL_ATTACHMENT:
352 break;
353 case GL_DEPTH_STENCIL_ATTACHMENT:
354 if (context->getClientVersion() < 3)
355 {
356 return gl::error(GL_INVALID_ENUM, false);
357 }
358 break;
359 default:
360 return gl::error(GL_INVALID_ENUM, false);
361 }
362 }
363
Jamie Madillab9d82c2014-01-21 16:38:14 -0500364 // [OpenGL ES 2.0.25] Section 4.4.3 page 112
365 // [OpenGL ES 3.0.2] Section 4.4.2 page 201
366 // 'renderbuffer' must be either zero or the name of an existing renderbuffer object of
367 // type 'renderbuffertarget', otherwise an INVALID_OPERATION error is generated.
368 if (renderbuffer != 0)
369 {
370 if (!context->getRenderbuffer(renderbuffer))
371 {
372 return gl::error(GL_INVALID_OPERATION, false);
373 }
374 }
375
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500376 return true;
377}
378
Jamie Madill3c7fa222014-06-05 13:08:51 -0400379static bool IsPartialBlit(gl::Context *context, gl::FramebufferAttachment *readBuffer, gl::FramebufferAttachment *writeBuffer,
Geoff Lang125deab2013-08-09 13:34:16 -0400380 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
381 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1)
382{
383 if (srcX0 != 0 || srcY0 != 0 || dstX0 != 0 || dstY0 != 0 ||
384 dstX1 != writeBuffer->getWidth() || dstY1 != writeBuffer->getHeight() ||
385 srcX1 != readBuffer->getWidth() || srcY1 != readBuffer->getHeight())
386 {
387 return true;
388 }
389 else if (context->isScissorTestEnabled())
390 {
391 int scissorX, scissorY, scissorWidth, scissorHeight;
392 context->getScissorParams(&scissorX, &scissorY, &scissorWidth, &scissorHeight);
393
394 return scissorX > 0 || scissorY > 0 ||
395 scissorWidth < writeBuffer->getWidth() ||
396 scissorHeight < writeBuffer->getHeight();
397 }
398 else
399 {
400 return false;
401 }
402}
403
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400404bool ValidateBlitFramebufferParameters(gl::Context *context, GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400405 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask,
406 GLenum filter, bool fromAngleExtension)
407{
408 switch (filter)
409 {
410 case GL_NEAREST:
411 break;
412 case GL_LINEAR:
413 if (fromAngleExtension)
414 {
415 return gl::error(GL_INVALID_ENUM, false);
416 }
417 break;
418 default:
419 return gl::error(GL_INVALID_ENUM, false);
420 }
421
422 if ((mask & ~(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)) != 0)
423 {
424 return gl::error(GL_INVALID_VALUE, false);
425 }
426
427 if (mask == 0)
428 {
429 // ES3.0 spec, section 4.3.2 specifies that a mask of zero is valid and no
430 // buffers are copied.
431 return false;
432 }
433
434 if (fromAngleExtension && (srcX1 - srcX0 != dstX1 - dstX0 || srcY1 - srcY0 != dstY1 - dstY0))
435 {
436 ERR("Scaling and flipping in BlitFramebufferANGLE not supported by this implementation.");
437 return gl::error(GL_INVALID_OPERATION, false);
438 }
439
440 // ES3.0 spec, section 4.3.2 states that linear filtering is only available for the
441 // color buffer, leaving only nearest being unfiltered from above
442 if ((mask & ~GL_COLOR_BUFFER_BIT) != 0 && filter != GL_NEAREST)
443 {
444 return gl::error(GL_INVALID_OPERATION, false);
445 }
446
447 if (context->getReadFramebufferHandle() == context->getDrawFramebufferHandle())
448 {
449 if (fromAngleExtension)
450 {
451 ERR("Blits with the same source and destination framebuffer are not supported by this "
452 "implementation.");
453 }
454 return gl::error(GL_INVALID_OPERATION, false);
455 }
456
457 gl::Framebuffer *readFramebuffer = context->getReadFramebuffer();
458 gl::Framebuffer *drawFramebuffer = context->getDrawFramebuffer();
459 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
460 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
461 {
462 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
463 }
464
465 if (drawFramebuffer->getSamples() != 0)
466 {
467 return gl::error(GL_INVALID_OPERATION, false);
468 }
469
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400470 bool sameBounds = srcX0 == dstX0 && srcY0 == dstY0 && srcX1 == dstX1 && srcY1 == dstY1;
471
472 GLuint clientVersion = context->getClientVersion();
473
474 if (mask & GL_COLOR_BUFFER_BIT)
475 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400476 gl::FramebufferAttachment *readColorBuffer = readFramebuffer->getReadColorbuffer();
477 gl::FramebufferAttachment *drawColorBuffer = drawFramebuffer->getFirstColorbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400478
479 if (readColorBuffer && drawColorBuffer)
480 {
Geoff Lang005df412013-10-16 14:12:50 -0400481 GLenum readInternalFormat = readColorBuffer->getActualFormat();
Geoff Langb2f3d052013-08-13 12:49:27 -0400482 GLenum readComponentType = gl::GetComponentType(readInternalFormat, clientVersion);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400483
484 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; i++)
485 {
486 if (drawFramebuffer->isEnabledColorAttachment(i))
487 {
Geoff Lang005df412013-10-16 14:12:50 -0400488 GLenum drawInternalFormat = drawFramebuffer->getColorbuffer(i)->getActualFormat();
Geoff Langb2f3d052013-08-13 12:49:27 -0400489 GLenum drawComponentType = gl::GetComponentType(drawInternalFormat, clientVersion);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400490
Geoff Langb2f3d052013-08-13 12:49:27 -0400491 // The GL ES 3.0.2 spec (pg 193) states that:
492 // 1) If the read buffer is fixed point format, the draw buffer must be as well
493 // 2) If the read buffer is an unsigned integer format, the draw buffer must be as well
494 // 3) If the read buffer is a signed integer format, the draw buffer must be as well
495 if ( (readComponentType == GL_UNSIGNED_NORMALIZED || readComponentType == GL_SIGNED_NORMALIZED) &&
496 !(drawComponentType == GL_UNSIGNED_NORMALIZED || drawComponentType == GL_SIGNED_NORMALIZED))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400497 {
498 return gl::error(GL_INVALID_OPERATION, false);
499 }
500
Geoff Langb2f3d052013-08-13 12:49:27 -0400501 if (readComponentType == GL_UNSIGNED_INT && drawComponentType != GL_UNSIGNED_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400502 {
503 return gl::error(GL_INVALID_OPERATION, false);
504 }
505
Geoff Langb2f3d052013-08-13 12:49:27 -0400506 if (readComponentType == GL_INT && drawComponentType != GL_INT)
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 (readColorBuffer->getSamples() > 0 && (readInternalFormat != drawInternalFormat || !sameBounds))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400512 {
513 return gl::error(GL_INVALID_OPERATION, false);
514 }
515 }
516 }
517
Geoff Langb2f3d052013-08-13 12:49:27 -0400518 if ((readComponentType == GL_INT || readComponentType == GL_UNSIGNED_INT) && filter == GL_LINEAR)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400519 {
520 return gl::error(GL_INVALID_OPERATION, false);
521 }
522
523 if (fromAngleExtension)
524 {
525 const GLenum readColorbufferType = readFramebuffer->getReadColorbufferType();
526 if (readColorbufferType != GL_TEXTURE_2D && readColorbufferType != GL_RENDERBUFFER)
527 {
528 return gl::error(GL_INVALID_OPERATION, false);
529 }
530
531 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
532 {
533 if (drawFramebuffer->isEnabledColorAttachment(colorAttachment))
534 {
535 if (drawFramebuffer->getColorbufferType(colorAttachment) != GL_TEXTURE_2D &&
536 drawFramebuffer->getColorbufferType(colorAttachment) != GL_RENDERBUFFER)
537 {
538 return gl::error(GL_INVALID_OPERATION, false);
539 }
540
541 if (drawFramebuffer->getColorbuffer(colorAttachment)->getActualFormat() != readColorBuffer->getActualFormat())
542 {
543 return gl::error(GL_INVALID_OPERATION, false);
544 }
545 }
546 }
Geoff Lang125deab2013-08-09 13:34:16 -0400547 if (readFramebuffer->getSamples() != 0 && IsPartialBlit(context, readColorBuffer, drawColorBuffer,
548 srcX0, srcY0, srcX1, srcY1,
549 dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400550 {
551 return gl::error(GL_INVALID_OPERATION, false);
552 }
553 }
554 }
555 }
556
557 if (mask & GL_DEPTH_BUFFER_BIT)
558 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400559 gl::FramebufferAttachment *readDepthBuffer = readFramebuffer->getDepthbuffer();
560 gl::FramebufferAttachment *drawDepthBuffer = drawFramebuffer->getDepthbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400561
562 if (readDepthBuffer && drawDepthBuffer)
563 {
564 if (readDepthBuffer->getActualFormat() != drawDepthBuffer->getActualFormat())
565 {
566 return gl::error(GL_INVALID_OPERATION, false);
567 }
568
569 if (readDepthBuffer->getSamples() > 0 && !sameBounds)
570 {
571 return gl::error(GL_INVALID_OPERATION, false);
572 }
573
574 if (fromAngleExtension)
575 {
Geoff Lang125deab2013-08-09 13:34:16 -0400576 if (IsPartialBlit(context, readDepthBuffer, drawDepthBuffer,
577 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400578 {
579 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
580 return gl::error(GL_INVALID_OPERATION, false); // only whole-buffer copies are permitted
581 }
582
583 if (readDepthBuffer->getSamples() != 0 || drawDepthBuffer->getSamples() != 0)
584 {
585 return gl::error(GL_INVALID_OPERATION, false);
586 }
587 }
588 }
589 }
590
591 if (mask & GL_STENCIL_BUFFER_BIT)
592 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400593 gl::FramebufferAttachment *readStencilBuffer = readFramebuffer->getStencilbuffer();
594 gl::FramebufferAttachment *drawStencilBuffer = drawFramebuffer->getStencilbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400595
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400596 if (readStencilBuffer && drawStencilBuffer)
597 {
598 if (readStencilBuffer->getActualFormat() != drawStencilBuffer->getActualFormat())
599 {
600 return gl::error(GL_INVALID_OPERATION, false);
601 }
602
603 if (readStencilBuffer->getSamples() > 0 && !sameBounds)
604 {
605 return gl::error(GL_INVALID_OPERATION, false);
606 }
607
608 if (fromAngleExtension)
609 {
Geoff Lang125deab2013-08-09 13:34:16 -0400610 if (IsPartialBlit(context, readStencilBuffer, drawStencilBuffer,
611 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400612 {
613 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
614 return gl::error(GL_INVALID_OPERATION, false); // only whole-buffer copies are permitted
615 }
616
617 if (readStencilBuffer->getSamples() != 0 || drawStencilBuffer->getSamples() != 0)
618 {
619 return gl::error(GL_INVALID_OPERATION, false);
620 }
621 }
622 }
623 }
624
625 return true;
626}
627
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400628bool ValidateGetVertexAttribParameters(GLenum pname, int clientVersion)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400629{
630 switch (pname)
631 {
632 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
633 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
634 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
635 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
636 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
637 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
638 case GL_CURRENT_VERTEX_ATTRIB:
639 return true;
640
641 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR:
642 // Don't verify ES3 context because GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE uses
643 // the same constant.
644 META_ASSERT(GL_VERTEX_ATTRIB_ARRAY_DIVISOR == GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE);
645 return true;
646
647 case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
648 return ((clientVersion >= 3) ? true : gl::error(GL_INVALID_ENUM, false));
649
650 default:
651 return gl::error(GL_INVALID_ENUM, false);
652 }
653}
654
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400655bool ValidateTexParamParameters(gl::Context *context, GLenum pname, GLint param)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400656{
657 switch (pname)
658 {
659 case GL_TEXTURE_WRAP_R:
660 case GL_TEXTURE_SWIZZLE_R:
661 case GL_TEXTURE_SWIZZLE_G:
662 case GL_TEXTURE_SWIZZLE_B:
663 case GL_TEXTURE_SWIZZLE_A:
664 case GL_TEXTURE_BASE_LEVEL:
665 case GL_TEXTURE_MAX_LEVEL:
666 case GL_TEXTURE_COMPARE_MODE:
667 case GL_TEXTURE_COMPARE_FUNC:
668 case GL_TEXTURE_MIN_LOD:
669 case GL_TEXTURE_MAX_LOD:
670 if (context->getClientVersion() < 3)
671 {
672 return gl::error(GL_INVALID_ENUM, false);
673 }
674 break;
675
676 default: break;
677 }
678
679 switch (pname)
680 {
681 case GL_TEXTURE_WRAP_S:
682 case GL_TEXTURE_WRAP_T:
683 case GL_TEXTURE_WRAP_R:
684 switch (param)
685 {
686 case GL_REPEAT:
687 case GL_CLAMP_TO_EDGE:
688 case GL_MIRRORED_REPEAT:
689 return true;
690 default:
691 return gl::error(GL_INVALID_ENUM, false);
692 }
693
694 case GL_TEXTURE_MIN_FILTER:
695 switch (param)
696 {
697 case GL_NEAREST:
698 case GL_LINEAR:
699 case GL_NEAREST_MIPMAP_NEAREST:
700 case GL_LINEAR_MIPMAP_NEAREST:
701 case GL_NEAREST_MIPMAP_LINEAR:
702 case GL_LINEAR_MIPMAP_LINEAR:
703 return true;
704 default:
705 return gl::error(GL_INVALID_ENUM, false);
706 }
707 break;
708
709 case GL_TEXTURE_MAG_FILTER:
710 switch (param)
711 {
712 case GL_NEAREST:
713 case GL_LINEAR:
714 return true;
715 default:
716 return gl::error(GL_INVALID_ENUM, false);
717 }
718 break;
719
720 case GL_TEXTURE_USAGE_ANGLE:
721 switch (param)
722 {
723 case GL_NONE:
724 case GL_FRAMEBUFFER_ATTACHMENT_ANGLE:
725 return true;
726 default:
727 return gl::error(GL_INVALID_ENUM, false);
728 }
729 break;
730
731 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
Geoff Langcec35902014-04-16 10:52:36 -0400732 if (!context->getCaps().extensions.textureFilterAnisotropic)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400733 {
734 return gl::error(GL_INVALID_ENUM, false);
735 }
736
737 // we assume the parameter passed to this validation method is truncated, not rounded
738 if (param < 1)
739 {
740 return gl::error(GL_INVALID_VALUE, false);
741 }
742 return true;
743
744 case GL_TEXTURE_MIN_LOD:
745 case GL_TEXTURE_MAX_LOD:
746 // any value is permissible
747 return true;
748
749 case GL_TEXTURE_COMPARE_MODE:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400750 // Acceptable mode parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400751 switch (param)
752 {
753 case GL_NONE:
754 case GL_COMPARE_REF_TO_TEXTURE:
755 return true;
756 default:
757 return gl::error(GL_INVALID_ENUM, false);
758 }
759 break;
760
761 case GL_TEXTURE_COMPARE_FUNC:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400762 // Acceptable function parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400763 switch (param)
764 {
765 case GL_LEQUAL:
766 case GL_GEQUAL:
767 case GL_LESS:
768 case GL_GREATER:
769 case GL_EQUAL:
770 case GL_NOTEQUAL:
771 case GL_ALWAYS:
772 case GL_NEVER:
773 return true;
774 default:
775 return gl::error(GL_INVALID_ENUM, false);
776 }
777 break;
778
779 case GL_TEXTURE_SWIZZLE_R:
780 case GL_TEXTURE_SWIZZLE_G:
781 case GL_TEXTURE_SWIZZLE_B:
782 case GL_TEXTURE_SWIZZLE_A:
Geoff Langbc90a482013-09-17 16:51:27 -0400783 switch (param)
784 {
785 case GL_RED:
786 case GL_GREEN:
787 case GL_BLUE:
788 case GL_ALPHA:
789 case GL_ZERO:
790 case GL_ONE:
791 return true;
792 default:
793 return gl::error(GL_INVALID_ENUM, false);
794 }
795 break;
796
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400797 case GL_TEXTURE_BASE_LEVEL:
798 case GL_TEXTURE_MAX_LEVEL:
Nicolas Capens8de68282014-04-04 11:10:27 -0400799 if (param < 0)
800 {
801 return gl::error(GL_INVALID_VALUE, false);
802 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400803 return true;
804
805 default:
806 return gl::error(GL_INVALID_ENUM, false);
807 }
808}
809
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400810bool ValidateSamplerObjectParameter(GLenum pname)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400811{
812 switch (pname)
813 {
814 case GL_TEXTURE_MIN_FILTER:
815 case GL_TEXTURE_MAG_FILTER:
816 case GL_TEXTURE_WRAP_S:
817 case GL_TEXTURE_WRAP_T:
818 case GL_TEXTURE_WRAP_R:
819 case GL_TEXTURE_MIN_LOD:
820 case GL_TEXTURE_MAX_LOD:
821 case GL_TEXTURE_COMPARE_MODE:
822 case GL_TEXTURE_COMPARE_FUNC:
823 return true;
824
825 default:
826 return gl::error(GL_INVALID_ENUM, false);
827 }
828}
829
Jamie Madill26e91952014-03-05 15:01:27 -0500830bool ValidateReadPixelsParameters(gl::Context *context, GLint x, GLint y, GLsizei width, GLsizei height,
831 GLenum format, GLenum type, GLsizei *bufSize, GLvoid *pixels)
832{
833 gl::Framebuffer *framebuffer = context->getReadFramebuffer();
Jamie Madill893ab082014-05-16 16:56:10 -0400834 ASSERT(framebuffer);
Jamie Madill26e91952014-03-05 15:01:27 -0500835
836 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
837 {
838 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
839 }
840
841 if (context->getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
842 {
843 return gl::error(GL_INVALID_OPERATION, false);
844 }
845
Jamie Madill893ab082014-05-16 16:56:10 -0400846 if (!framebuffer->getReadColorbuffer())
847 {
848 return gl::error(GL_INVALID_OPERATION, false);
849 }
850
Jamie Madill26e91952014-03-05 15:01:27 -0500851 GLenum currentInternalFormat, currentFormat, currentType;
852 int clientVersion = context->getClientVersion();
853
Jamie Madill893ab082014-05-16 16:56:10 -0400854 context->getCurrentReadFormatType(&currentInternalFormat, &currentFormat, &currentType);
Jamie Madill26e91952014-03-05 15:01:27 -0500855
Geoff Langbdc9b2f2014-04-16 14:41:54 -0400856 bool validReadFormat = (clientVersion < 3) ? ValidES2ReadFormatType(context, format, type) :
857 ValidES3ReadFormatType(context, currentInternalFormat, format, type);
Jamie Madill26e91952014-03-05 15:01:27 -0500858
859 if (!(currentFormat == format && currentType == type) && !validReadFormat)
860 {
861 return gl::error(GL_INVALID_OPERATION, false);
862 }
863
864 GLenum sizedInternalFormat = IsSizedInternalFormat(format, clientVersion) ? format :
865 GetSizedInternalFormat(format, type, clientVersion);
866
867 GLsizei outputPitch = GetRowPitch(sizedInternalFormat, type, clientVersion, width, context->getPackAlignment());
868 // sized query sanity check
869 if (bufSize)
870 {
871 int requiredSize = outputPitch * height;
872 if (requiredSize > *bufSize)
873 {
874 return gl::error(GL_INVALID_OPERATION, false);
875 }
876 }
877
878 return true;
879}
880
Jamie Madilldb2f14c2014-05-13 13:56:30 -0400881bool ValidateBeginQuery(gl::Context *context, GLenum target, GLuint id)
882{
883 if (!ValidQueryType(context, target))
884 {
885 return gl::error(GL_INVALID_ENUM, false);
886 }
887
888 if (id == 0)
889 {
890 return gl::error(GL_INVALID_OPERATION, false);
891 }
892
893 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
894 // of zero, if the active query object name for <target> is non-zero (for the
895 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
896 // the active query for either target is non-zero), if <id> is the name of an
897 // existing query object whose type does not match <target>, or if <id> is the
898 // active query object name for any query type, the error INVALID_OPERATION is
899 // generated.
900
901 // Ensure no other queries are active
902 // NOTE: If other queries than occlusion are supported, we will need to check
903 // separately that:
904 // a) The query ID passed is not the current active query for any target/type
905 // b) There are no active queries for the requested target (and in the case
906 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
907 // no query may be active for either if glBeginQuery targets either.
908 if (context->isQueryActive())
909 {
910 return gl::error(GL_INVALID_OPERATION, false);
911 }
912
913 Query *queryObject = context->getQuery(id, true, target);
914
915 // check that name was obtained with glGenQueries
916 if (!queryObject)
917 {
918 return gl::error(GL_INVALID_OPERATION, false);
919 }
920
921 // check for type mismatch
922 if (queryObject->getType() != target)
923 {
924 return gl::error(GL_INVALID_OPERATION, false);
925 }
926
927 return true;
928}
929
Jamie Madill45c785d2014-05-13 14:09:34 -0400930bool ValidateEndQuery(gl::Context *context, GLenum target)
931{
932 if (!ValidQueryType(context, target))
933 {
934 return gl::error(GL_INVALID_ENUM, false);
935 }
936
937 const Query *queryObject = context->getActiveQuery(target);
938
939 if (queryObject == NULL)
940 {
941 return gl::error(GL_INVALID_OPERATION, false);
942 }
943
944 if (!queryObject->isStarted())
945 {
946 return gl::error(GL_INVALID_OPERATION, false);
947 }
948
949 return true;
950}
951
Jamie Madill36398922014-05-20 14:51:53 -0400952static bool ValidateUniformCommonBase(gl::Context *context, GLenum targetUniformType,
953 GLint location, GLsizei count, LinkedUniform **uniformOut)
Jamie Madilld7c7bb22014-05-20 10:55:54 -0400954{
955 if (count < 0)
956 {
957 return gl::error(GL_INVALID_VALUE, false);
958 }
959
Jamie Madilld7c7bb22014-05-20 10:55:54 -0400960 gl::ProgramBinary *programBinary = context->getCurrentProgramBinary();
961 if (!programBinary)
962 {
963 return gl::error(GL_INVALID_OPERATION, false);
964 }
965
966 if (location == -1)
967 {
968 // Silently ignore the uniform command
969 return false;
970 }
971
Jamie Madill36398922014-05-20 14:51:53 -0400972 if (!programBinary->isValidUniformLocation(location))
973 {
974 return gl::error(GL_INVALID_OPERATION, false);
975 }
976
977 LinkedUniform *uniform = programBinary->getUniformByLocation(location);
978
979 // attempting to write an array to a non-array uniform is an INVALID_OPERATION
980 if (uniform->elementCount() == 1 && count > 1)
981 {
982 return gl::error(GL_INVALID_OPERATION, false);
983 }
984
985 *uniformOut = uniform;
Jamie Madilld7c7bb22014-05-20 10:55:54 -0400986 return true;
987}
988
Jamie Madillaa981bd2014-05-20 10:55:55 -0400989bool ValidateUniform(gl::Context *context, GLenum uniformType, GLint location, GLsizei count)
990{
991 // Check for ES3 uniform entry points
992 if (UniformComponentType(uniformType) == GL_UNSIGNED_INT && context->getClientVersion() < 3)
993 {
994 return gl::error(GL_INVALID_OPERATION, false);
995 }
996
Jamie Madill36398922014-05-20 14:51:53 -0400997 LinkedUniform *uniform = NULL;
998 if (!ValidateUniformCommonBase(context, uniformType, location, count, &uniform))
999 {
1000 return false;
1001 }
1002
1003 GLenum targetBoolType = UniformBoolVectorType(uniformType);
1004 bool samplerUniformCheck = (IsSampler(uniform->type) && uniformType == GL_INT);
1005 if (!samplerUniformCheck && uniformType != uniform->type && targetBoolType != uniform->type)
1006 {
1007 return gl::error(GL_INVALID_OPERATION, false);
1008 }
1009
1010 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001011}
1012
1013bool ValidateUniformMatrix(gl::Context *context, GLenum matrixType, GLint location, GLsizei count,
1014 GLboolean transpose)
1015{
1016 // Check for ES3 uniform entry points
1017 int rows = VariableRowCount(matrixType);
1018 int cols = VariableColumnCount(matrixType);
1019 if (rows != cols && context->getClientVersion() < 3)
1020 {
1021 return gl::error(GL_INVALID_OPERATION, false);
1022 }
1023
1024 if (transpose != GL_FALSE && context->getClientVersion() < 3)
1025 {
1026 return gl::error(GL_INVALID_VALUE, false);
1027 }
1028
Jamie Madill36398922014-05-20 14:51:53 -04001029 LinkedUniform *uniform = NULL;
1030 if (!ValidateUniformCommonBase(context, matrixType, location, count, &uniform))
1031 {
1032 return false;
1033 }
1034
1035 if (uniform->type != matrixType)
1036 {
1037 return gl::error(GL_INVALID_OPERATION, false);
1038 }
1039
1040 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001041}
1042
Jamie Madill893ab082014-05-16 16:56:10 -04001043bool ValidateStateQuery(gl::Context *context, GLenum pname, GLenum *nativeType, unsigned int *numParams)
1044{
1045 if (!context->getQueryParameterInfo(pname, nativeType, numParams))
1046 {
1047 return gl::error(GL_INVALID_ENUM, false);
1048 }
1049
1050 if (pname >= GL_DRAW_BUFFER0 && pname <= GL_DRAW_BUFFER15)
1051 {
1052 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0);
1053
1054 if (colorAttachment >= context->getMaximumRenderTargets())
1055 {
1056 return gl::error(GL_INVALID_OPERATION, false);
1057 }
1058 }
1059
1060 switch (pname)
1061 {
1062 case GL_TEXTURE_BINDING_2D:
1063 case GL_TEXTURE_BINDING_CUBE_MAP:
1064 case GL_TEXTURE_BINDING_3D:
1065 case GL_TEXTURE_BINDING_2D_ARRAY:
1066 if (context->getActiveSampler() >= context->getMaximumCombinedTextureImageUnits())
1067 {
1068 return gl::error(GL_INVALID_OPERATION, false);
1069 }
1070 break;
1071
1072 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1073 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1074 {
1075 Framebuffer *framebuffer = context->getReadFramebuffer();
1076 ASSERT(framebuffer);
1077 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1078 {
1079 return gl::error(GL_INVALID_OPERATION, false);
1080 }
1081
Jamie Madill3c7fa222014-06-05 13:08:51 -04001082 FramebufferAttachment *attachment = framebuffer->getReadColorbuffer();
1083 if (!attachment)
Jamie Madill893ab082014-05-16 16:56:10 -04001084 {
1085 return gl::error(GL_INVALID_OPERATION, false);
1086 }
1087 }
1088 break;
1089
1090 default:
1091 break;
1092 }
1093
1094 // pname is valid, but there are no parameters to return
1095 if (numParams == 0)
1096 {
1097 return false;
1098 }
1099
1100 return true;
1101}
1102
Jamie Madill560a8d82014-05-21 13:06:20 -04001103bool ValidateCopyTexImageParametersBase(gl::Context* context, GLenum target, GLint level, GLenum internalformat, bool isSubImage,
1104 GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height,
1105 GLint border, GLenum *textureFormatOut)
1106{
1107
1108 if (!ValidTexture2DDestinationTarget(context, target))
1109 {
1110 return gl::error(GL_INVALID_ENUM, false);
1111 }
1112
1113 if (level < 0 || xoffset < 0 || yoffset < 0 || zoffset < 0 || width < 0 || height < 0)
1114 {
1115 return gl::error(GL_INVALID_VALUE, false);
1116 }
1117
1118 if (std::numeric_limits<GLsizei>::max() - xoffset < width || std::numeric_limits<GLsizei>::max() - yoffset < height)
1119 {
1120 return gl::error(GL_INVALID_VALUE, false);
1121 }
1122
1123 if (border != 0)
1124 {
1125 return gl::error(GL_INVALID_VALUE, false);
1126 }
1127
1128 if (!ValidMipLevel(context, target, level))
1129 {
1130 return gl::error(GL_INVALID_VALUE, false);
1131 }
1132
1133 gl::Framebuffer *framebuffer = context->getReadFramebuffer();
1134 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1135 {
1136 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
1137 }
1138
1139 if (context->getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
1140 {
1141 return gl::error(GL_INVALID_OPERATION, false);
1142 }
1143
Jamie Madill560a8d82014-05-21 13:06:20 -04001144 gl::Texture *texture = NULL;
1145 GLenum textureInternalFormat = GL_NONE;
1146 bool textureCompressed = false;
1147 bool textureIsDepth = false;
1148 GLint textureLevelWidth = 0;
1149 GLint textureLevelHeight = 0;
1150 GLint textureLevelDepth = 0;
Jamie Madill6f38f822014-06-06 17:12:20 -04001151 int maxDimension = 0;
Jamie Madill560a8d82014-05-21 13:06:20 -04001152
1153 switch (target)
1154 {
1155 case GL_TEXTURE_2D:
1156 {
1157 gl::Texture2D *texture2d = context->getTexture2D();
1158 if (texture2d)
1159 {
1160 textureInternalFormat = texture2d->getInternalFormat(level);
1161 textureCompressed = texture2d->isCompressed(level);
1162 textureIsDepth = texture2d->isDepth(level);
1163 textureLevelWidth = texture2d->getWidth(level);
1164 textureLevelHeight = texture2d->getHeight(level);
1165 textureLevelDepth = 1;
1166 texture = texture2d;
Jamie Madill6f38f822014-06-06 17:12:20 -04001167 maxDimension = context->getMaximum2DTextureDimension();
Jamie Madill560a8d82014-05-21 13:06:20 -04001168 }
1169 }
1170 break;
1171
1172 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1173 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1174 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1175 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1176 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1177 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1178 {
1179 gl::TextureCubeMap *textureCube = context->getTextureCubeMap();
1180 if (textureCube)
1181 {
1182 textureInternalFormat = textureCube->getInternalFormat(target, level);
1183 textureCompressed = textureCube->isCompressed(target, level);
1184 textureIsDepth = false;
1185 textureLevelWidth = textureCube->getWidth(target, level);
1186 textureLevelHeight = textureCube->getHeight(target, level);
1187 textureLevelDepth = 1;
1188 texture = textureCube;
Jamie Madill6f38f822014-06-06 17:12:20 -04001189 maxDimension = context->getMaximumCubeTextureDimension();
Jamie Madill560a8d82014-05-21 13:06:20 -04001190 }
1191 }
1192 break;
1193
1194 case GL_TEXTURE_2D_ARRAY:
1195 {
1196 gl::Texture2DArray *texture2dArray = context->getTexture2DArray();
1197 if (texture2dArray)
1198 {
1199 textureInternalFormat = texture2dArray->getInternalFormat(level);
1200 textureCompressed = texture2dArray->isCompressed(level);
1201 textureIsDepth = texture2dArray->isDepth(level);
1202 textureLevelWidth = texture2dArray->getWidth(level);
1203 textureLevelHeight = texture2dArray->getHeight(level);
1204 textureLevelDepth = texture2dArray->getLayers(level);
1205 texture = texture2dArray;
Jamie Madill6f38f822014-06-06 17:12:20 -04001206 maxDimension = context->getMaximum2DTextureDimension();
Jamie Madill560a8d82014-05-21 13:06:20 -04001207 }
1208 }
1209 break;
1210
1211 case GL_TEXTURE_3D:
1212 {
1213 gl::Texture3D *texture3d = context->getTexture3D();
1214 if (texture3d)
1215 {
1216 textureInternalFormat = texture3d->getInternalFormat(level);
1217 textureCompressed = texture3d->isCompressed(level);
1218 textureIsDepth = texture3d->isDepth(level);
1219 textureLevelWidth = texture3d->getWidth(level);
1220 textureLevelHeight = texture3d->getHeight(level);
1221 textureLevelDepth = texture3d->getDepth(level);
1222 texture = texture3d;
Jamie Madill6f38f822014-06-06 17:12:20 -04001223 maxDimension = context->getMaximum3DTextureDimension();
Jamie Madill560a8d82014-05-21 13:06:20 -04001224 }
1225 }
1226 break;
1227
1228 default:
1229 return gl::error(GL_INVALID_ENUM, false);
1230 }
1231
1232 if (!texture)
1233 {
1234 return gl::error(GL_INVALID_OPERATION, false);
1235 }
1236
1237 if (texture->isImmutable() && !isSubImage)
1238 {
1239 return gl::error(GL_INVALID_OPERATION, false);
1240 }
1241
1242 if (textureIsDepth)
1243 {
1244 return gl::error(GL_INVALID_OPERATION, false);
1245 }
1246
1247 if (textureCompressed)
1248 {
1249 int clientVersion = context->getClientVersion();
1250 GLint blockWidth = GetCompressedBlockWidth(textureInternalFormat, clientVersion);
1251 GLint blockHeight = GetCompressedBlockHeight(textureInternalFormat, clientVersion);
1252
1253 if (((width % blockWidth) != 0 && width != textureLevelWidth) ||
1254 ((height % blockHeight) != 0 && height != textureLevelHeight))
1255 {
1256 return gl::error(GL_INVALID_OPERATION, false);
1257 }
1258 }
1259
1260 if (isSubImage)
1261 {
1262 if (xoffset + width > textureLevelWidth ||
1263 yoffset + height > textureLevelHeight ||
1264 zoffset >= textureLevelDepth)
1265 {
1266 return gl::error(GL_INVALID_VALUE, false);
1267 }
1268 }
Jamie Madill6f38f822014-06-06 17:12:20 -04001269 else
1270 {
1271 if (IsCubemapTextureTarget(target) && width != height)
1272 {
1273 return gl::error(GL_INVALID_VALUE, false);
1274 }
1275
Geoff Langcec35902014-04-16 10:52:36 -04001276 if (!IsValidInternalFormat(internalformat, context->getCaps().extensions, context->getClientVersion()))
Jamie Madill6f38f822014-06-06 17:12:20 -04001277 {
1278 return gl::error(GL_INVALID_ENUM, false);
1279 }
1280
1281 int maxLevelDimension = (maxDimension >> level);
1282 if (static_cast<int>(width) > maxLevelDimension || static_cast<int>(height) > maxLevelDimension)
1283 {
1284 return gl::error(GL_INVALID_VALUE, false);
1285 }
1286 }
Jamie Madill560a8d82014-05-21 13:06:20 -04001287
1288 *textureFormatOut = textureInternalFormat;
1289 return true;
1290}
1291
Jamie Madill250d33f2014-06-06 17:09:03 -04001292bool ValidateDrawArrays(const gl::Context *context, GLenum mode, GLint first, GLsizei count)
1293{
1294 if (count < 0 || first < 0)
1295 {
1296 return gl::error(GL_INVALID_VALUE, false);
1297 }
1298
1299 // Check for mapped buffers
1300 if (context->hasMappedBuffer(GL_ARRAY_BUFFER))
1301 {
1302 return gl::error(GL_INVALID_OPERATION, false);
1303 }
1304
1305 gl::TransformFeedback *curTransformFeedback = context->getCurrentTransformFeedback();
1306 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused() &&
1307 curTransformFeedback->getDrawMode() != mode)
1308 {
1309 // It is an invalid operation to call DrawArrays or DrawArraysInstanced with a draw mode
1310 // that does not match the current transform feedback object's draw mode (if transform feedback
1311 // is active), (3.0.2, section 2.14, pg 86)
1312 return gl::error(GL_INVALID_OPERATION, false);
1313 }
1314
1315 // No-op if zero count
1316 return (count > 0);
1317}
1318
1319bool ValidateDrawArraysInstanced(const gl::Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
1320{
1321 if (count < 0 || first < 0 || primcount < 0)
1322 {
1323 return gl::error(GL_INVALID_VALUE, false);
1324 }
1325
1326 // Check for mapped buffers
1327 if (context->hasMappedBuffer(GL_ARRAY_BUFFER))
1328 {
1329 return gl::error(GL_INVALID_OPERATION, false);
1330 }
1331
1332 gl::TransformFeedback *curTransformFeedback = context->getCurrentTransformFeedback();
1333 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused() &&
1334 curTransformFeedback->getDrawMode() != mode)
1335 {
1336 // It is an invalid operation to call DrawArrays or DrawArraysInstanced with a draw mode
1337 // that does not match the current transform feedback object's draw mode (if transform feedback
1338 // is active), (3.0.2, section 2.14, pg 86)
1339 return gl::error(GL_INVALID_OPERATION, false);
1340 }
1341
1342 // No-op if zero count or zero primitive count
1343 return (primcount > 0 && count > 0);
1344}
1345
1346bool ValidateDrawElements(const gl::Context *context, GLenum mode, GLsizei count, GLenum type, const GLvoid* indices)
1347{
1348 if (count < 0)
1349 {
1350 return gl::error(GL_INVALID_VALUE, false);
1351 }
1352
1353 switch (type)
1354 {
1355 case GL_UNSIGNED_BYTE:
1356 case GL_UNSIGNED_SHORT:
1357 break;
1358 case GL_UNSIGNED_INT:
1359 if (!context->getCaps().extensions.elementIndexUint)
1360 {
1361 return gl::error(GL_INVALID_ENUM, false);
1362 }
1363 break;
1364 default:
1365 return gl::error(GL_INVALID_ENUM, false);
1366 }
1367
1368 gl::TransformFeedback *curTransformFeedback = context->getCurrentTransformFeedback();
1369 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused())
1370 {
1371 // It is an invalid operation to call DrawElements, DrawRangeElements or DrawElementsInstanced
1372 // while transform feedback is active, (3.0.2, section 2.14, pg 86)
1373 return gl::error(GL_INVALID_OPERATION, false);
1374 }
1375
1376 // Check for mapped buffers
1377 if (context->hasMappedBuffer(GL_ARRAY_BUFFER) || context->hasMappedBuffer(GL_ELEMENT_ARRAY_BUFFER))
1378 {
1379 return gl::error(GL_INVALID_OPERATION, false);
1380 }
1381
1382 // No-op if zero count
1383 return (count > 0);
1384}
1385
1386bool ValidateDrawElementsInstanced(const gl::Context *context, GLenum mode, GLsizei count, GLenum type,
1387 const GLvoid *indices, GLsizei primcount)
1388{
1389 if (count < 0 || primcount < 0)
1390 {
1391 return gl::error(GL_INVALID_VALUE, false);
1392 }
1393
1394 switch (type)
1395 {
1396 case GL_UNSIGNED_BYTE:
1397 case GL_UNSIGNED_SHORT:
1398 break;
1399 case GL_UNSIGNED_INT:
1400 if (!context->getCaps().extensions.elementIndexUint)
1401 {
1402 return gl::error(GL_INVALID_ENUM, false);
1403 }
1404 break;
1405 default:
1406 return gl::error(GL_INVALID_ENUM, false);
1407 }
1408
1409 gl::TransformFeedback *curTransformFeedback = context->getCurrentTransformFeedback();
1410 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused())
1411 {
1412 // It is an invalid operation to call DrawElements, DrawRangeElements or DrawElementsInstanced
1413 // while transform feedback is active, (3.0.2, section 2.14, pg 86)
1414 return gl::error(GL_INVALID_OPERATION, false);
1415 }
1416
1417 // Check for mapped buffers
1418 if (context->hasMappedBuffer(GL_ARRAY_BUFFER) || context->hasMappedBuffer(GL_ELEMENT_ARRAY_BUFFER))
1419 {
1420 return gl::error(GL_INVALID_OPERATION, false);
1421 }
1422
1423 // No-op if zero count or zero primitive count
1424 return (primcount > 0 && count > 0);
1425}
1426
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001427}