blob: ec2a6b682ae2617a849be8adfe86b39620738a10 [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
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400249bool ValidateRenderbufferStorageParameters(const gl::Context *context, GLenum target, GLsizei samples,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400250 GLenum internalformat, GLsizei width, GLsizei height,
251 bool angleExtension)
252{
253 switch (target)
254 {
255 case GL_RENDERBUFFER:
256 break;
257 default:
258 return gl::error(GL_INVALID_ENUM, false);
259 }
260
261 if (width < 0 || height < 0 || samples < 0)
262 {
263 return gl::error(GL_INVALID_VALUE, false);
264 }
265
Geoff Langcec35902014-04-16 10:52:36 -0400266 const gl::Caps &caps = context->getCaps();
267 if (!gl::IsValidInternalFormat(internalformat, caps.extensions, context->getClientVersion()))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400268 {
269 return gl::error(GL_INVALID_ENUM, false);
270 }
271
272 // ANGLE_framebuffer_multisample does not explicitly state that the internal format must be
273 // sized but it does state that the format must be in the ES2.0 spec table 4.5 which contains
274 // only sized internal formats. The ES3 spec (section 4.4.2) does, however, state that the
275 // internal format must be sized and not an integer format if samples is greater than zero.
Geoff Lange4a492b2014-06-19 14:14:41 -0400276 if (!gl::IsSizedInternalFormat(internalformat))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400277 {
278 return gl::error(GL_INVALID_ENUM, false);
279 }
280
Geoff Lange4a492b2014-06-19 14:14:41 -0400281 GLenum componentType = gl::GetComponentType(internalformat);
Geoff Langb2f3d052013-08-13 12:49:27 -0400282 if ((componentType == GL_UNSIGNED_INT || componentType == GL_INT) && samples > 0)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400283 {
284 return gl::error(GL_INVALID_OPERATION, false);
285 }
286
Geoff Langcec35902014-04-16 10:52:36 -0400287 const TextureCaps &formatCaps = caps.textureCaps.get(internalformat);
288 if (!formatCaps.colorRendering && !formatCaps.depthRendering && !formatCaps.stencilRendering)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400289 {
290 return gl::error(GL_INVALID_ENUM, false);
291 }
292
293 if (std::max(width, height) > context->getMaximumRenderbufferDimension())
294 {
295 return gl::error(GL_INVALID_VALUE, false);
296 }
297
298 // ANGLE_framebuffer_multisample states that the value of samples must be less than or equal
299 // to MAX_SAMPLES_ANGLE (Context::getMaxSupportedSamples) while the ES3.0 spec (section 4.4.2)
300 // states that samples must be less than or equal to the maximum samples for the specified
301 // internal format.
302 if (angleExtension)
303 {
304 if (samples > context->getMaxSupportedSamples())
305 {
306 return gl::error(GL_INVALID_VALUE, false);
307 }
308 }
309 else
310 {
311 if (samples > context->getMaxSupportedFormatSamples(internalformat))
312 {
313 return gl::error(GL_INVALID_VALUE, false);
314 }
315 }
316
317 GLuint handle = context->getRenderbufferHandle();
318 if (handle == 0)
319 {
320 return gl::error(GL_INVALID_OPERATION, false);
321 }
322
323 return true;
324}
325
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500326bool ValidateFramebufferRenderbufferParameters(gl::Context *context, GLenum target, GLenum attachment,
327 GLenum renderbuffertarget, GLuint renderbuffer)
328{
329 gl::Framebuffer *framebuffer = context->getTargetFramebuffer(target);
330 GLuint framebufferHandle = context->getTargetFramebufferHandle(target);
331
332 if (!framebuffer || (framebufferHandle == 0 && renderbuffer != 0))
333 {
334 return gl::error(GL_INVALID_OPERATION, false);
335 }
336
337 if (attachment >= GL_COLOR_ATTACHMENT0_EXT && attachment <= GL_COLOR_ATTACHMENT15_EXT)
338 {
339 const unsigned int colorAttachment = (attachment - GL_COLOR_ATTACHMENT0_EXT);
340
341 if (colorAttachment >= context->getMaximumRenderTargets())
342 {
343 return gl::error(GL_INVALID_VALUE, false);
344 }
345 }
346 else
347 {
348 switch (attachment)
349 {
350 case GL_DEPTH_ATTACHMENT:
351 break;
352 case GL_STENCIL_ATTACHMENT:
353 break;
354 case GL_DEPTH_STENCIL_ATTACHMENT:
355 if (context->getClientVersion() < 3)
356 {
357 return gl::error(GL_INVALID_ENUM, false);
358 }
359 break;
360 default:
361 return gl::error(GL_INVALID_ENUM, false);
362 }
363 }
364
Jamie Madillab9d82c2014-01-21 16:38:14 -0500365 // [OpenGL ES 2.0.25] Section 4.4.3 page 112
366 // [OpenGL ES 3.0.2] Section 4.4.2 page 201
367 // 'renderbuffer' must be either zero or the name of an existing renderbuffer object of
368 // type 'renderbuffertarget', otherwise an INVALID_OPERATION error is generated.
369 if (renderbuffer != 0)
370 {
371 if (!context->getRenderbuffer(renderbuffer))
372 {
373 return gl::error(GL_INVALID_OPERATION, false);
374 }
375 }
376
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500377 return true;
378}
379
Jamie Madill3c7fa222014-06-05 13:08:51 -0400380static bool IsPartialBlit(gl::Context *context, gl::FramebufferAttachment *readBuffer, gl::FramebufferAttachment *writeBuffer,
Geoff Lang125deab2013-08-09 13:34:16 -0400381 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
382 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1)
383{
384 if (srcX0 != 0 || srcY0 != 0 || dstX0 != 0 || dstY0 != 0 ||
385 dstX1 != writeBuffer->getWidth() || dstY1 != writeBuffer->getHeight() ||
386 srcX1 != readBuffer->getWidth() || srcY1 != readBuffer->getHeight())
387 {
388 return true;
389 }
390 else if (context->isScissorTestEnabled())
391 {
392 int scissorX, scissorY, scissorWidth, scissorHeight;
393 context->getScissorParams(&scissorX, &scissorY, &scissorWidth, &scissorHeight);
394
395 return scissorX > 0 || scissorY > 0 ||
396 scissorWidth < writeBuffer->getWidth() ||
397 scissorHeight < writeBuffer->getHeight();
398 }
399 else
400 {
401 return false;
402 }
403}
404
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400405bool ValidateBlitFramebufferParameters(gl::Context *context, GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400406 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask,
407 GLenum filter, bool fromAngleExtension)
408{
409 switch (filter)
410 {
411 case GL_NEAREST:
412 break;
413 case GL_LINEAR:
414 if (fromAngleExtension)
415 {
416 return gl::error(GL_INVALID_ENUM, false);
417 }
418 break;
419 default:
420 return gl::error(GL_INVALID_ENUM, false);
421 }
422
423 if ((mask & ~(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)) != 0)
424 {
425 return gl::error(GL_INVALID_VALUE, false);
426 }
427
428 if (mask == 0)
429 {
430 // ES3.0 spec, section 4.3.2 specifies that a mask of zero is valid and no
431 // buffers are copied.
432 return false;
433 }
434
435 if (fromAngleExtension && (srcX1 - srcX0 != dstX1 - dstX0 || srcY1 - srcY0 != dstY1 - dstY0))
436 {
437 ERR("Scaling and flipping in BlitFramebufferANGLE not supported by this implementation.");
438 return gl::error(GL_INVALID_OPERATION, false);
439 }
440
441 // ES3.0 spec, section 4.3.2 states that linear filtering is only available for the
442 // color buffer, leaving only nearest being unfiltered from above
443 if ((mask & ~GL_COLOR_BUFFER_BIT) != 0 && filter != GL_NEAREST)
444 {
445 return gl::error(GL_INVALID_OPERATION, false);
446 }
447
448 if (context->getReadFramebufferHandle() == context->getDrawFramebufferHandle())
449 {
450 if (fromAngleExtension)
451 {
452 ERR("Blits with the same source and destination framebuffer are not supported by this "
453 "implementation.");
454 }
455 return gl::error(GL_INVALID_OPERATION, false);
456 }
457
458 gl::Framebuffer *readFramebuffer = context->getReadFramebuffer();
459 gl::Framebuffer *drawFramebuffer = context->getDrawFramebuffer();
460 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
461 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
462 {
463 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
464 }
465
466 if (drawFramebuffer->getSamples() != 0)
467 {
468 return gl::error(GL_INVALID_OPERATION, false);
469 }
470
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400471 bool sameBounds = srcX0 == dstX0 && srcY0 == dstY0 && srcX1 == dstX1 && srcY1 == dstY1;
472
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400473 if (mask & GL_COLOR_BUFFER_BIT)
474 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400475 gl::FramebufferAttachment *readColorBuffer = readFramebuffer->getReadColorbuffer();
476 gl::FramebufferAttachment *drawColorBuffer = drawFramebuffer->getFirstColorbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400477
478 if (readColorBuffer && drawColorBuffer)
479 {
Geoff Lang005df412013-10-16 14:12:50 -0400480 GLenum readInternalFormat = readColorBuffer->getActualFormat();
Geoff Lange4a492b2014-06-19 14:14:41 -0400481 GLenum readComponentType = gl::GetComponentType(readInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400482
483 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; i++)
484 {
485 if (drawFramebuffer->isEnabledColorAttachment(i))
486 {
Geoff Lang005df412013-10-16 14:12:50 -0400487 GLenum drawInternalFormat = drawFramebuffer->getColorbuffer(i)->getActualFormat();
Geoff Lange4a492b2014-06-19 14:14:41 -0400488 GLenum drawComponentType = gl::GetComponentType(drawInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400489
Geoff Langb2f3d052013-08-13 12:49:27 -0400490 // The GL ES 3.0.2 spec (pg 193) states that:
491 // 1) If the read buffer is fixed point format, the draw buffer must be as well
492 // 2) If the read buffer is an unsigned integer format, the draw buffer must be as well
493 // 3) If the read buffer is a signed integer format, the draw buffer must be as well
494 if ( (readComponentType == GL_UNSIGNED_NORMALIZED || readComponentType == GL_SIGNED_NORMALIZED) &&
495 !(drawComponentType == GL_UNSIGNED_NORMALIZED || drawComponentType == GL_SIGNED_NORMALIZED))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400496 {
497 return gl::error(GL_INVALID_OPERATION, false);
498 }
499
Geoff Langb2f3d052013-08-13 12:49:27 -0400500 if (readComponentType == GL_UNSIGNED_INT && drawComponentType != GL_UNSIGNED_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400501 {
502 return gl::error(GL_INVALID_OPERATION, false);
503 }
504
Geoff Langb2f3d052013-08-13 12:49:27 -0400505 if (readComponentType == GL_INT && drawComponentType != GL_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400506 {
507 return gl::error(GL_INVALID_OPERATION, false);
508 }
509
Geoff Langb2f3d052013-08-13 12:49:27 -0400510 if (readColorBuffer->getSamples() > 0 && (readInternalFormat != drawInternalFormat || !sameBounds))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400511 {
512 return gl::error(GL_INVALID_OPERATION, false);
513 }
514 }
515 }
516
Geoff Langb2f3d052013-08-13 12:49:27 -0400517 if ((readComponentType == GL_INT || readComponentType == GL_UNSIGNED_INT) && filter == GL_LINEAR)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400518 {
519 return gl::error(GL_INVALID_OPERATION, false);
520 }
521
522 if (fromAngleExtension)
523 {
524 const GLenum readColorbufferType = readFramebuffer->getReadColorbufferType();
525 if (readColorbufferType != GL_TEXTURE_2D && readColorbufferType != GL_RENDERBUFFER)
526 {
527 return gl::error(GL_INVALID_OPERATION, false);
528 }
529
530 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
531 {
532 if (drawFramebuffer->isEnabledColorAttachment(colorAttachment))
533 {
534 if (drawFramebuffer->getColorbufferType(colorAttachment) != GL_TEXTURE_2D &&
535 drawFramebuffer->getColorbufferType(colorAttachment) != GL_RENDERBUFFER)
536 {
537 return gl::error(GL_INVALID_OPERATION, false);
538 }
539
540 if (drawFramebuffer->getColorbuffer(colorAttachment)->getActualFormat() != readColorBuffer->getActualFormat())
541 {
542 return gl::error(GL_INVALID_OPERATION, false);
543 }
544 }
545 }
Geoff Lang125deab2013-08-09 13:34:16 -0400546 if (readFramebuffer->getSamples() != 0 && IsPartialBlit(context, readColorBuffer, drawColorBuffer,
547 srcX0, srcY0, srcX1, srcY1,
548 dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400549 {
550 return gl::error(GL_INVALID_OPERATION, false);
551 }
552 }
553 }
554 }
555
556 if (mask & GL_DEPTH_BUFFER_BIT)
557 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400558 gl::FramebufferAttachment *readDepthBuffer = readFramebuffer->getDepthbuffer();
559 gl::FramebufferAttachment *drawDepthBuffer = drawFramebuffer->getDepthbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400560
561 if (readDepthBuffer && drawDepthBuffer)
562 {
563 if (readDepthBuffer->getActualFormat() != drawDepthBuffer->getActualFormat())
564 {
565 return gl::error(GL_INVALID_OPERATION, false);
566 }
567
568 if (readDepthBuffer->getSamples() > 0 && !sameBounds)
569 {
570 return gl::error(GL_INVALID_OPERATION, false);
571 }
572
573 if (fromAngleExtension)
574 {
Geoff Lang125deab2013-08-09 13:34:16 -0400575 if (IsPartialBlit(context, readDepthBuffer, drawDepthBuffer,
576 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400577 {
578 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
579 return gl::error(GL_INVALID_OPERATION, false); // only whole-buffer copies are permitted
580 }
581
582 if (readDepthBuffer->getSamples() != 0 || drawDepthBuffer->getSamples() != 0)
583 {
584 return gl::error(GL_INVALID_OPERATION, false);
585 }
586 }
587 }
588 }
589
590 if (mask & GL_STENCIL_BUFFER_BIT)
591 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400592 gl::FramebufferAttachment *readStencilBuffer = readFramebuffer->getStencilbuffer();
593 gl::FramebufferAttachment *drawStencilBuffer = drawFramebuffer->getStencilbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400594
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400595 if (readStencilBuffer && drawStencilBuffer)
596 {
597 if (readStencilBuffer->getActualFormat() != drawStencilBuffer->getActualFormat())
598 {
599 return gl::error(GL_INVALID_OPERATION, false);
600 }
601
602 if (readStencilBuffer->getSamples() > 0 && !sameBounds)
603 {
604 return gl::error(GL_INVALID_OPERATION, false);
605 }
606
607 if (fromAngleExtension)
608 {
Geoff Lang125deab2013-08-09 13:34:16 -0400609 if (IsPartialBlit(context, readStencilBuffer, drawStencilBuffer,
610 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400611 {
612 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
613 return gl::error(GL_INVALID_OPERATION, false); // only whole-buffer copies are permitted
614 }
615
616 if (readStencilBuffer->getSamples() != 0 || drawStencilBuffer->getSamples() != 0)
617 {
618 return gl::error(GL_INVALID_OPERATION, false);
619 }
620 }
621 }
622 }
623
624 return true;
625}
626
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400627bool ValidateGetVertexAttribParameters(GLenum pname, int clientVersion)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400628{
629 switch (pname)
630 {
631 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
632 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
633 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
634 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
635 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
636 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
637 case GL_CURRENT_VERTEX_ATTRIB:
638 return true;
639
640 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR:
641 // Don't verify ES3 context because GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE uses
642 // the same constant.
643 META_ASSERT(GL_VERTEX_ATTRIB_ARRAY_DIVISOR == GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE);
644 return true;
645
646 case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
647 return ((clientVersion >= 3) ? true : gl::error(GL_INVALID_ENUM, false));
648
649 default:
650 return gl::error(GL_INVALID_ENUM, false);
651 }
652}
653
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400654bool ValidateTexParamParameters(gl::Context *context, GLenum pname, GLint param)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400655{
656 switch (pname)
657 {
658 case GL_TEXTURE_WRAP_R:
659 case GL_TEXTURE_SWIZZLE_R:
660 case GL_TEXTURE_SWIZZLE_G:
661 case GL_TEXTURE_SWIZZLE_B:
662 case GL_TEXTURE_SWIZZLE_A:
663 case GL_TEXTURE_BASE_LEVEL:
664 case GL_TEXTURE_MAX_LEVEL:
665 case GL_TEXTURE_COMPARE_MODE:
666 case GL_TEXTURE_COMPARE_FUNC:
667 case GL_TEXTURE_MIN_LOD:
668 case GL_TEXTURE_MAX_LOD:
669 if (context->getClientVersion() < 3)
670 {
671 return gl::error(GL_INVALID_ENUM, false);
672 }
673 break;
674
675 default: break;
676 }
677
678 switch (pname)
679 {
680 case GL_TEXTURE_WRAP_S:
681 case GL_TEXTURE_WRAP_T:
682 case GL_TEXTURE_WRAP_R:
683 switch (param)
684 {
685 case GL_REPEAT:
686 case GL_CLAMP_TO_EDGE:
687 case GL_MIRRORED_REPEAT:
688 return true;
689 default:
690 return gl::error(GL_INVALID_ENUM, false);
691 }
692
693 case GL_TEXTURE_MIN_FILTER:
694 switch (param)
695 {
696 case GL_NEAREST:
697 case GL_LINEAR:
698 case GL_NEAREST_MIPMAP_NEAREST:
699 case GL_LINEAR_MIPMAP_NEAREST:
700 case GL_NEAREST_MIPMAP_LINEAR:
701 case GL_LINEAR_MIPMAP_LINEAR:
702 return true;
703 default:
704 return gl::error(GL_INVALID_ENUM, false);
705 }
706 break;
707
708 case GL_TEXTURE_MAG_FILTER:
709 switch (param)
710 {
711 case GL_NEAREST:
712 case GL_LINEAR:
713 return true;
714 default:
715 return gl::error(GL_INVALID_ENUM, false);
716 }
717 break;
718
719 case GL_TEXTURE_USAGE_ANGLE:
720 switch (param)
721 {
722 case GL_NONE:
723 case GL_FRAMEBUFFER_ATTACHMENT_ANGLE:
724 return true;
725 default:
726 return gl::error(GL_INVALID_ENUM, false);
727 }
728 break;
729
730 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
Geoff Langcec35902014-04-16 10:52:36 -0400731 if (!context->getCaps().extensions.textureFilterAnisotropic)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400732 {
733 return gl::error(GL_INVALID_ENUM, false);
734 }
735
736 // we assume the parameter passed to this validation method is truncated, not rounded
737 if (param < 1)
738 {
739 return gl::error(GL_INVALID_VALUE, false);
740 }
741 return true;
742
743 case GL_TEXTURE_MIN_LOD:
744 case GL_TEXTURE_MAX_LOD:
745 // any value is permissible
746 return true;
747
748 case GL_TEXTURE_COMPARE_MODE:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400749 // Acceptable mode parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400750 switch (param)
751 {
752 case GL_NONE:
753 case GL_COMPARE_REF_TO_TEXTURE:
754 return true;
755 default:
756 return gl::error(GL_INVALID_ENUM, false);
757 }
758 break;
759
760 case GL_TEXTURE_COMPARE_FUNC:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400761 // Acceptable function parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400762 switch (param)
763 {
764 case GL_LEQUAL:
765 case GL_GEQUAL:
766 case GL_LESS:
767 case GL_GREATER:
768 case GL_EQUAL:
769 case GL_NOTEQUAL:
770 case GL_ALWAYS:
771 case GL_NEVER:
772 return true;
773 default:
774 return gl::error(GL_INVALID_ENUM, false);
775 }
776 break;
777
778 case GL_TEXTURE_SWIZZLE_R:
779 case GL_TEXTURE_SWIZZLE_G:
780 case GL_TEXTURE_SWIZZLE_B:
781 case GL_TEXTURE_SWIZZLE_A:
Geoff Langbc90a482013-09-17 16:51:27 -0400782 switch (param)
783 {
784 case GL_RED:
785 case GL_GREEN:
786 case GL_BLUE:
787 case GL_ALPHA:
788 case GL_ZERO:
789 case GL_ONE:
790 return true;
791 default:
792 return gl::error(GL_INVALID_ENUM, false);
793 }
794 break;
795
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400796 case GL_TEXTURE_BASE_LEVEL:
797 case GL_TEXTURE_MAX_LEVEL:
Nicolas Capens8de68282014-04-04 11:10:27 -0400798 if (param < 0)
799 {
800 return gl::error(GL_INVALID_VALUE, false);
801 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400802 return true;
803
804 default:
805 return gl::error(GL_INVALID_ENUM, false);
806 }
807}
808
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400809bool ValidateSamplerObjectParameter(GLenum pname)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400810{
811 switch (pname)
812 {
813 case GL_TEXTURE_MIN_FILTER:
814 case GL_TEXTURE_MAG_FILTER:
815 case GL_TEXTURE_WRAP_S:
816 case GL_TEXTURE_WRAP_T:
817 case GL_TEXTURE_WRAP_R:
818 case GL_TEXTURE_MIN_LOD:
819 case GL_TEXTURE_MAX_LOD:
820 case GL_TEXTURE_COMPARE_MODE:
821 case GL_TEXTURE_COMPARE_FUNC:
822 return true;
823
824 default:
825 return gl::error(GL_INVALID_ENUM, false);
826 }
827}
828
Jamie Madill26e91952014-03-05 15:01:27 -0500829bool ValidateReadPixelsParameters(gl::Context *context, GLint x, GLint y, GLsizei width, GLsizei height,
830 GLenum format, GLenum type, GLsizei *bufSize, GLvoid *pixels)
831{
832 gl::Framebuffer *framebuffer = context->getReadFramebuffer();
Jamie Madill893ab082014-05-16 16:56:10 -0400833 ASSERT(framebuffer);
Jamie Madill26e91952014-03-05 15:01:27 -0500834
835 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
836 {
837 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
838 }
839
840 if (context->getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
841 {
842 return gl::error(GL_INVALID_OPERATION, false);
843 }
844
Jamie Madill893ab082014-05-16 16:56:10 -0400845 if (!framebuffer->getReadColorbuffer())
846 {
847 return gl::error(GL_INVALID_OPERATION, false);
848 }
849
Jamie Madill26e91952014-03-05 15:01:27 -0500850 GLenum currentInternalFormat, currentFormat, currentType;
Geoff Lange4a492b2014-06-19 14:14:41 -0400851 GLuint clientVersion = context->getClientVersion();
Jamie Madill26e91952014-03-05 15:01:27 -0500852
Jamie Madill893ab082014-05-16 16:56:10 -0400853 context->getCurrentReadFormatType(&currentInternalFormat, &currentFormat, &currentType);
Jamie Madill26e91952014-03-05 15:01:27 -0500854
Geoff Langbdc9b2f2014-04-16 14:41:54 -0400855 bool validReadFormat = (clientVersion < 3) ? ValidES2ReadFormatType(context, format, type) :
856 ValidES3ReadFormatType(context, currentInternalFormat, format, type);
Jamie Madill26e91952014-03-05 15:01:27 -0500857
858 if (!(currentFormat == format && currentType == type) && !validReadFormat)
859 {
860 return gl::error(GL_INVALID_OPERATION, false);
861 }
862
Geoff Lange4a492b2014-06-19 14:14:41 -0400863 GLenum sizedInternalFormat = IsSizedInternalFormat(format) ? format
864 : GetSizedInternalFormat(format, type);
Jamie Madill26e91952014-03-05 15:01:27 -0500865
Geoff Lange4a492b2014-06-19 14:14:41 -0400866 GLsizei outputPitch = GetRowPitch(sizedInternalFormat, type, width, context->getPackAlignment());
Jamie Madill26e91952014-03-05 15:01:27 -0500867 // sized query sanity check
868 if (bufSize)
869 {
870 int requiredSize = outputPitch * height;
871 if (requiredSize > *bufSize)
872 {
873 return gl::error(GL_INVALID_OPERATION, false);
874 }
875 }
876
877 return true;
878}
879
Jamie Madilldb2f14c2014-05-13 13:56:30 -0400880bool ValidateBeginQuery(gl::Context *context, GLenum target, GLuint id)
881{
882 if (!ValidQueryType(context, target))
883 {
884 return gl::error(GL_INVALID_ENUM, false);
885 }
886
887 if (id == 0)
888 {
889 return gl::error(GL_INVALID_OPERATION, false);
890 }
891
892 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
893 // of zero, if the active query object name for <target> is non-zero (for the
894 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
895 // the active query for either target is non-zero), if <id> is the name of an
896 // existing query object whose type does not match <target>, or if <id> is the
897 // active query object name for any query type, the error INVALID_OPERATION is
898 // generated.
899
900 // Ensure no other queries are active
901 // NOTE: If other queries than occlusion are supported, we will need to check
902 // separately that:
903 // a) The query ID passed is not the current active query for any target/type
904 // b) There are no active queries for the requested target (and in the case
905 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
906 // no query may be active for either if glBeginQuery targets either.
907 if (context->isQueryActive())
908 {
909 return gl::error(GL_INVALID_OPERATION, false);
910 }
911
912 Query *queryObject = context->getQuery(id, true, target);
913
914 // check that name was obtained with glGenQueries
915 if (!queryObject)
916 {
917 return gl::error(GL_INVALID_OPERATION, false);
918 }
919
920 // check for type mismatch
921 if (queryObject->getType() != target)
922 {
923 return gl::error(GL_INVALID_OPERATION, false);
924 }
925
926 return true;
927}
928
Jamie Madill45c785d2014-05-13 14:09:34 -0400929bool ValidateEndQuery(gl::Context *context, GLenum target)
930{
931 if (!ValidQueryType(context, target))
932 {
933 return gl::error(GL_INVALID_ENUM, false);
934 }
935
936 const Query *queryObject = context->getActiveQuery(target);
937
938 if (queryObject == NULL)
939 {
940 return gl::error(GL_INVALID_OPERATION, false);
941 }
942
943 if (!queryObject->isStarted())
944 {
945 return gl::error(GL_INVALID_OPERATION, false);
946 }
947
948 return true;
949}
950
Jamie Madill36398922014-05-20 14:51:53 -0400951static bool ValidateUniformCommonBase(gl::Context *context, GLenum targetUniformType,
952 GLint location, GLsizei count, LinkedUniform **uniformOut)
Jamie Madilld7c7bb22014-05-20 10:55:54 -0400953{
954 if (count < 0)
955 {
956 return gl::error(GL_INVALID_VALUE, false);
957 }
958
Jamie Madilld7c7bb22014-05-20 10:55:54 -0400959 gl::ProgramBinary *programBinary = context->getCurrentProgramBinary();
960 if (!programBinary)
961 {
962 return gl::error(GL_INVALID_OPERATION, false);
963 }
964
965 if (location == -1)
966 {
967 // Silently ignore the uniform command
968 return false;
969 }
970
Jamie Madill36398922014-05-20 14:51:53 -0400971 if (!programBinary->isValidUniformLocation(location))
972 {
973 return gl::error(GL_INVALID_OPERATION, false);
974 }
975
976 LinkedUniform *uniform = programBinary->getUniformByLocation(location);
977
978 // attempting to write an array to a non-array uniform is an INVALID_OPERATION
979 if (uniform->elementCount() == 1 && count > 1)
980 {
981 return gl::error(GL_INVALID_OPERATION, false);
982 }
983
984 *uniformOut = uniform;
Jamie Madilld7c7bb22014-05-20 10:55:54 -0400985 return true;
986}
987
Jamie Madillaa981bd2014-05-20 10:55:55 -0400988bool ValidateUniform(gl::Context *context, GLenum uniformType, GLint location, GLsizei count)
989{
990 // Check for ES3 uniform entry points
Jamie Madillf2575982014-06-25 16:04:54 -0400991 if (VariableComponentType(uniformType) == GL_UNSIGNED_INT && context->getClientVersion() < 3)
Jamie Madillaa981bd2014-05-20 10:55:55 -0400992 {
993 return gl::error(GL_INVALID_OPERATION, false);
994 }
995
Jamie Madill36398922014-05-20 14:51:53 -0400996 LinkedUniform *uniform = NULL;
997 if (!ValidateUniformCommonBase(context, uniformType, location, count, &uniform))
998 {
999 return false;
1000 }
1001
Jamie Madillf2575982014-06-25 16:04:54 -04001002 GLenum targetBoolType = VariableBoolVectorType(uniformType);
Jamie Madill36398922014-05-20 14:51:53 -04001003 bool samplerUniformCheck = (IsSampler(uniform->type) && uniformType == GL_INT);
1004 if (!samplerUniformCheck && uniformType != uniform->type && targetBoolType != uniform->type)
1005 {
1006 return gl::error(GL_INVALID_OPERATION, false);
1007 }
1008
1009 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001010}
1011
1012bool ValidateUniformMatrix(gl::Context *context, GLenum matrixType, GLint location, GLsizei count,
1013 GLboolean transpose)
1014{
1015 // Check for ES3 uniform entry points
1016 int rows = VariableRowCount(matrixType);
1017 int cols = VariableColumnCount(matrixType);
1018 if (rows != cols && context->getClientVersion() < 3)
1019 {
1020 return gl::error(GL_INVALID_OPERATION, false);
1021 }
1022
1023 if (transpose != GL_FALSE && context->getClientVersion() < 3)
1024 {
1025 return gl::error(GL_INVALID_VALUE, false);
1026 }
1027
Jamie Madill36398922014-05-20 14:51:53 -04001028 LinkedUniform *uniform = NULL;
1029 if (!ValidateUniformCommonBase(context, matrixType, location, count, &uniform))
1030 {
1031 return false;
1032 }
1033
1034 if (uniform->type != matrixType)
1035 {
1036 return gl::error(GL_INVALID_OPERATION, false);
1037 }
1038
1039 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001040}
1041
Jamie Madill893ab082014-05-16 16:56:10 -04001042bool ValidateStateQuery(gl::Context *context, GLenum pname, GLenum *nativeType, unsigned int *numParams)
1043{
1044 if (!context->getQueryParameterInfo(pname, nativeType, numParams))
1045 {
1046 return gl::error(GL_INVALID_ENUM, false);
1047 }
1048
1049 if (pname >= GL_DRAW_BUFFER0 && pname <= GL_DRAW_BUFFER15)
1050 {
1051 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0);
1052
1053 if (colorAttachment >= context->getMaximumRenderTargets())
1054 {
1055 return gl::error(GL_INVALID_OPERATION, false);
1056 }
1057 }
1058
1059 switch (pname)
1060 {
1061 case GL_TEXTURE_BINDING_2D:
1062 case GL_TEXTURE_BINDING_CUBE_MAP:
1063 case GL_TEXTURE_BINDING_3D:
1064 case GL_TEXTURE_BINDING_2D_ARRAY:
1065 if (context->getActiveSampler() >= context->getMaximumCombinedTextureImageUnits())
1066 {
1067 return gl::error(GL_INVALID_OPERATION, false);
1068 }
1069 break;
1070
1071 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1072 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1073 {
1074 Framebuffer *framebuffer = context->getReadFramebuffer();
1075 ASSERT(framebuffer);
1076 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1077 {
1078 return gl::error(GL_INVALID_OPERATION, false);
1079 }
1080
Jamie Madill3c7fa222014-06-05 13:08:51 -04001081 FramebufferAttachment *attachment = framebuffer->getReadColorbuffer();
1082 if (!attachment)
Jamie Madill893ab082014-05-16 16:56:10 -04001083 {
1084 return gl::error(GL_INVALID_OPERATION, false);
1085 }
1086 }
1087 break;
1088
1089 default:
1090 break;
1091 }
1092
1093 // pname is valid, but there are no parameters to return
1094 if (numParams == 0)
1095 {
1096 return false;
1097 }
1098
1099 return true;
1100}
1101
Jamie Madill560a8d82014-05-21 13:06:20 -04001102bool ValidateCopyTexImageParametersBase(gl::Context* context, GLenum target, GLint level, GLenum internalformat, bool isSubImage,
1103 GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height,
1104 GLint border, GLenum *textureFormatOut)
1105{
1106
1107 if (!ValidTexture2DDestinationTarget(context, target))
1108 {
1109 return gl::error(GL_INVALID_ENUM, false);
1110 }
1111
1112 if (level < 0 || xoffset < 0 || yoffset < 0 || zoffset < 0 || width < 0 || height < 0)
1113 {
1114 return gl::error(GL_INVALID_VALUE, false);
1115 }
1116
1117 if (std::numeric_limits<GLsizei>::max() - xoffset < width || std::numeric_limits<GLsizei>::max() - yoffset < height)
1118 {
1119 return gl::error(GL_INVALID_VALUE, false);
1120 }
1121
1122 if (border != 0)
1123 {
1124 return gl::error(GL_INVALID_VALUE, false);
1125 }
1126
1127 if (!ValidMipLevel(context, target, level))
1128 {
1129 return gl::error(GL_INVALID_VALUE, false);
1130 }
1131
1132 gl::Framebuffer *framebuffer = context->getReadFramebuffer();
1133 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1134 {
1135 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
1136 }
1137
1138 if (context->getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
1139 {
1140 return gl::error(GL_INVALID_OPERATION, false);
1141 }
1142
Jamie Madill560a8d82014-05-21 13:06:20 -04001143 gl::Texture *texture = NULL;
1144 GLenum textureInternalFormat = GL_NONE;
1145 bool textureCompressed = false;
1146 bool textureIsDepth = false;
1147 GLint textureLevelWidth = 0;
1148 GLint textureLevelHeight = 0;
1149 GLint textureLevelDepth = 0;
Jamie Madill6f38f822014-06-06 17:12:20 -04001150 int maxDimension = 0;
Jamie Madill560a8d82014-05-21 13:06:20 -04001151
1152 switch (target)
1153 {
1154 case GL_TEXTURE_2D:
1155 {
1156 gl::Texture2D *texture2d = context->getTexture2D();
1157 if (texture2d)
1158 {
1159 textureInternalFormat = texture2d->getInternalFormat(level);
1160 textureCompressed = texture2d->isCompressed(level);
1161 textureIsDepth = texture2d->isDepth(level);
1162 textureLevelWidth = texture2d->getWidth(level);
1163 textureLevelHeight = texture2d->getHeight(level);
1164 textureLevelDepth = 1;
1165 texture = texture2d;
Jamie Madill6f38f822014-06-06 17:12:20 -04001166 maxDimension = context->getMaximum2DTextureDimension();
Jamie Madill560a8d82014-05-21 13:06:20 -04001167 }
1168 }
1169 break;
1170
1171 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1172 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1173 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1174 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1175 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1176 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1177 {
1178 gl::TextureCubeMap *textureCube = context->getTextureCubeMap();
1179 if (textureCube)
1180 {
1181 textureInternalFormat = textureCube->getInternalFormat(target, level);
1182 textureCompressed = textureCube->isCompressed(target, level);
1183 textureIsDepth = false;
1184 textureLevelWidth = textureCube->getWidth(target, level);
1185 textureLevelHeight = textureCube->getHeight(target, level);
1186 textureLevelDepth = 1;
1187 texture = textureCube;
Jamie Madill6f38f822014-06-06 17:12:20 -04001188 maxDimension = context->getMaximumCubeTextureDimension();
Jamie Madill560a8d82014-05-21 13:06:20 -04001189 }
1190 }
1191 break;
1192
1193 case GL_TEXTURE_2D_ARRAY:
1194 {
1195 gl::Texture2DArray *texture2dArray = context->getTexture2DArray();
1196 if (texture2dArray)
1197 {
1198 textureInternalFormat = texture2dArray->getInternalFormat(level);
1199 textureCompressed = texture2dArray->isCompressed(level);
1200 textureIsDepth = texture2dArray->isDepth(level);
1201 textureLevelWidth = texture2dArray->getWidth(level);
1202 textureLevelHeight = texture2dArray->getHeight(level);
1203 textureLevelDepth = texture2dArray->getLayers(level);
1204 texture = texture2dArray;
Jamie Madill6f38f822014-06-06 17:12:20 -04001205 maxDimension = context->getMaximum2DTextureDimension();
Jamie Madill560a8d82014-05-21 13:06:20 -04001206 }
1207 }
1208 break;
1209
1210 case GL_TEXTURE_3D:
1211 {
1212 gl::Texture3D *texture3d = context->getTexture3D();
1213 if (texture3d)
1214 {
1215 textureInternalFormat = texture3d->getInternalFormat(level);
1216 textureCompressed = texture3d->isCompressed(level);
1217 textureIsDepth = texture3d->isDepth(level);
1218 textureLevelWidth = texture3d->getWidth(level);
1219 textureLevelHeight = texture3d->getHeight(level);
1220 textureLevelDepth = texture3d->getDepth(level);
1221 texture = texture3d;
Jamie Madill6f38f822014-06-06 17:12:20 -04001222 maxDimension = context->getMaximum3DTextureDimension();
Jamie Madill560a8d82014-05-21 13:06:20 -04001223 }
1224 }
1225 break;
1226
1227 default:
1228 return gl::error(GL_INVALID_ENUM, false);
1229 }
1230
1231 if (!texture)
1232 {
1233 return gl::error(GL_INVALID_OPERATION, false);
1234 }
1235
1236 if (texture->isImmutable() && !isSubImage)
1237 {
1238 return gl::error(GL_INVALID_OPERATION, false);
1239 }
1240
1241 if (textureIsDepth)
1242 {
1243 return gl::error(GL_INVALID_OPERATION, false);
1244 }
1245
1246 if (textureCompressed)
1247 {
Geoff Lange4a492b2014-06-19 14:14:41 -04001248 GLint blockWidth = GetCompressedBlockWidth(textureInternalFormat);
1249 GLint blockHeight = GetCompressedBlockHeight(textureInternalFormat);
Jamie Madill560a8d82014-05-21 13:06:20 -04001250
1251 if (((width % blockWidth) != 0 && width != textureLevelWidth) ||
1252 ((height % blockHeight) != 0 && height != textureLevelHeight))
1253 {
1254 return gl::error(GL_INVALID_OPERATION, false);
1255 }
1256 }
1257
1258 if (isSubImage)
1259 {
1260 if (xoffset + width > textureLevelWidth ||
1261 yoffset + height > textureLevelHeight ||
1262 zoffset >= textureLevelDepth)
1263 {
1264 return gl::error(GL_INVALID_VALUE, false);
1265 }
1266 }
Jamie Madill6f38f822014-06-06 17:12:20 -04001267 else
1268 {
1269 if (IsCubemapTextureTarget(target) && width != height)
1270 {
1271 return gl::error(GL_INVALID_VALUE, false);
1272 }
1273
Geoff Langcec35902014-04-16 10:52:36 -04001274 if (!IsValidInternalFormat(internalformat, context->getCaps().extensions, context->getClientVersion()))
Jamie Madill6f38f822014-06-06 17:12:20 -04001275 {
1276 return gl::error(GL_INVALID_ENUM, false);
1277 }
1278
1279 int maxLevelDimension = (maxDimension >> level);
1280 if (static_cast<int>(width) > maxLevelDimension || static_cast<int>(height) > maxLevelDimension)
1281 {
1282 return gl::error(GL_INVALID_VALUE, false);
1283 }
1284 }
Jamie Madill560a8d82014-05-21 13:06:20 -04001285
1286 *textureFormatOut = textureInternalFormat;
1287 return true;
1288}
1289
Jamie Madill1aeb1312014-06-20 13:21:25 -04001290static bool ValidateDrawBase(const gl::Context *context, GLenum mode, GLsizei count)
Jamie Madill250d33f2014-06-06 17:09:03 -04001291{
Jamie Madill1aeb1312014-06-20 13:21:25 -04001292 switch (mode)
1293 {
1294 case GL_POINTS:
1295 case GL_LINES:
1296 case GL_LINE_LOOP:
1297 case GL_LINE_STRIP:
1298 case GL_TRIANGLES:
1299 case GL_TRIANGLE_STRIP:
1300 case GL_TRIANGLE_FAN:
1301 break;
1302 default:
1303 return gl::error(GL_INVALID_ENUM, false);
1304 }
1305
Jamie Madill250d33f2014-06-06 17:09:03 -04001306 if (count < 0)
1307 {
1308 return gl::error(GL_INVALID_VALUE, false);
1309 }
1310
Jamie Madill250d33f2014-06-06 17:09:03 -04001311 // Check for mapped buffers
Jamie Madillfd716582014-06-06 17:09:04 -04001312 if (context->hasMappedBuffer(GL_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001313 {
1314 return gl::error(GL_INVALID_OPERATION, false);
1315 }
1316
Jamie Madillac528012014-06-20 13:21:23 -04001317 const gl::DepthStencilState &depthStencilState = context->getDepthStencilState();
1318 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
1319 context->getStencilRef() != context->getStencilBackRef() ||
1320 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
1321 {
1322 // Note: these separate values are not supported in WebGL, due to D3D's limitations.
1323 // See Section 6.10 of the WebGL 1.0 spec
1324 ERR("This ANGLE implementation does not support separate front/back stencil "
1325 "writemasks, reference values, or stencil mask values.");
1326 return gl::error(GL_INVALID_OPERATION, false);
1327 }
1328
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001329 const gl::Framebuffer *fbo = context->getDrawFramebuffer();
1330 if (!fbo || fbo->completeness() != GL_FRAMEBUFFER_COMPLETE)
1331 {
1332 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
1333 }
1334
Jamie Madill250d33f2014-06-06 17:09:03 -04001335 // No-op if zero count
1336 return (count > 0);
1337}
1338
Jamie Madillfd716582014-06-06 17:09:04 -04001339bool ValidateDrawArrays(const gl::Context *context, GLenum mode, GLint first, GLsizei count)
Jamie Madill250d33f2014-06-06 17:09:03 -04001340{
Jamie Madillfd716582014-06-06 17:09:04 -04001341 if (first < 0)
Jamie Madill250d33f2014-06-06 17:09:03 -04001342 {
1343 return gl::error(GL_INVALID_VALUE, false);
1344 }
1345
Jamie Madillfd716582014-06-06 17:09:04 -04001346 gl::TransformFeedback *curTransformFeedback = context->getCurrentTransformFeedback();
1347 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused() &&
1348 curTransformFeedback->getDrawMode() != mode)
1349 {
1350 // It is an invalid operation to call DrawArrays or DrawArraysInstanced with a draw mode
1351 // that does not match the current transform feedback object's draw mode (if transform feedback
1352 // is active), (3.0.2, section 2.14, pg 86)
1353 return gl::error(GL_INVALID_OPERATION, false);
1354 }
1355
Jamie Madill1aeb1312014-06-20 13:21:25 -04001356 if (!ValidateDrawBase(context, mode, count))
Jamie Madillfd716582014-06-06 17:09:04 -04001357 {
1358 return false;
1359 }
1360
1361 return true;
1362}
1363
1364bool ValidateDrawArraysInstanced(const gl::Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
1365{
1366 if (primcount < 0)
1367 {
1368 return gl::error(GL_INVALID_VALUE, false);
1369 }
1370
1371 if (!ValidateDrawArrays(context, mode, first, count))
1372 {
1373 return false;
1374 }
1375
1376 // No-op if zero primitive count
1377 return (primcount > 0);
1378}
1379
1380bool ValidateDrawElements(const gl::Context *context, GLenum mode, GLsizei count, GLenum type, const GLvoid* indices)
1381{
Jamie Madill250d33f2014-06-06 17:09:03 -04001382 switch (type)
1383 {
1384 case GL_UNSIGNED_BYTE:
1385 case GL_UNSIGNED_SHORT:
1386 break;
1387 case GL_UNSIGNED_INT:
1388 if (!context->getCaps().extensions.elementIndexUint)
1389 {
1390 return gl::error(GL_INVALID_ENUM, false);
1391 }
1392 break;
1393 default:
1394 return gl::error(GL_INVALID_ENUM, false);
1395 }
1396
1397 gl::TransformFeedback *curTransformFeedback = context->getCurrentTransformFeedback();
1398 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused())
1399 {
1400 // It is an invalid operation to call DrawElements, DrawRangeElements or DrawElementsInstanced
1401 // while transform feedback is active, (3.0.2, section 2.14, pg 86)
1402 return gl::error(GL_INVALID_OPERATION, false);
1403 }
1404
1405 // Check for mapped buffers
Jamie Madillfd716582014-06-06 17:09:04 -04001406 if (context->hasMappedBuffer(GL_ELEMENT_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001407 {
1408 return gl::error(GL_INVALID_OPERATION, false);
1409 }
1410
Jamie Madill1aeb1312014-06-20 13:21:25 -04001411 if (!ValidateDrawBase(context, mode, count))
Jamie Madillfd716582014-06-06 17:09:04 -04001412 {
1413 return false;
1414 }
1415
1416 return true;
1417}
1418
1419bool ValidateDrawElementsInstanced(const gl::Context *context, GLenum mode, GLsizei count, GLenum type,
1420 const GLvoid *indices, GLsizei primcount)
1421{
1422 if (primcount < 0)
1423 {
1424 return gl::error(GL_INVALID_VALUE, false);
1425 }
1426
1427 if (!ValidateDrawElements(context, mode, count, type, indices))
1428 {
1429 return false;
1430 }
1431
1432 // No-op zero primitive count
1433 return (primcount > 0);
Jamie Madill250d33f2014-06-06 17:09:03 -04001434}
1435
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001436}