blob: 93dd0002a7073d51df147b0b7914e9b56c94a1bb [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"
Jamie Madill9efa5812014-06-20 13:21:24 -040022#include "libGLESv2/VertexArray.h"
Geoff Lange8ebe7f2013-08-05 15:03:13 -040023
24#include "common/mathutil.h"
25#include "common/utilities.h"
26
27namespace gl
28{
29
Geoff Lang0550d032014-01-30 11:29:07 -050030bool ValidCap(const Context *context, GLenum cap)
31{
32 switch (cap)
33 {
34 case GL_CULL_FACE:
35 case GL_POLYGON_OFFSET_FILL:
36 case GL_SAMPLE_ALPHA_TO_COVERAGE:
37 case GL_SAMPLE_COVERAGE:
38 case GL_SCISSOR_TEST:
39 case GL_STENCIL_TEST:
40 case GL_DEPTH_TEST:
41 case GL_BLEND:
42 case GL_DITHER:
43 return true;
44 case GL_PRIMITIVE_RESTART_FIXED_INDEX:
45 case GL_RASTERIZER_DISCARD:
46 return (context->getClientVersion() >= 3);
47 default:
48 return false;
49 }
50}
51
Jamie Madill1fc7e2c2014-01-21 16:47:10 -050052bool ValidTextureTarget(const Context *context, GLenum target)
Jamie Madill35d15012013-10-07 10:46:37 -040053{
Jamie Madilld7460c72014-01-21 16:38:14 -050054 switch (target)
Jamie Madill35d15012013-10-07 10:46:37 -040055 {
Jamie Madilld7460c72014-01-21 16:38:14 -050056 case GL_TEXTURE_2D:
57 case GL_TEXTURE_CUBE_MAP:
58 return true;
Jamie Madill35d15012013-10-07 10:46:37 -040059
Jamie Madilld7460c72014-01-21 16:38:14 -050060 case GL_TEXTURE_3D:
61 case GL_TEXTURE_2D_ARRAY:
62 return (context->getClientVersion() >= 3);
63
64 default:
65 return false;
66 }
Jamie Madill35d15012013-10-07 10:46:37 -040067}
68
Shannon Woods4dfed832014-03-17 20:03:39 -040069// This function differs from ValidTextureTarget in that the target must be
70// usable as the destination of a 2D operation-- so a cube face is valid, but
71// GL_TEXTURE_CUBE_MAP is not.
Jamie Madill560a8d82014-05-21 13:06:20 -040072// Note: duplicate of IsInternalTextureTarget
Shannon Woods4dfed832014-03-17 20:03:39 -040073bool ValidTexture2DDestinationTarget(const Context *context, GLenum target)
74{
75 switch (target)
76 {
77 case GL_TEXTURE_2D:
78 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
79 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
80 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
81 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
82 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
83 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
84 return true;
85 case GL_TEXTURE_2D_ARRAY:
86 case GL_TEXTURE_3D:
87 return (context->getClientVersion() >= 3);
88 default:
89 return false;
90 }
91}
92
Jamie Madill1fc7e2c2014-01-21 16:47:10 -050093bool ValidFramebufferTarget(GLenum target)
94{
95 META_ASSERT(GL_DRAW_FRAMEBUFFER_ANGLE == GL_DRAW_FRAMEBUFFER && GL_READ_FRAMEBUFFER_ANGLE == GL_READ_FRAMEBUFFER);
96
97 switch (target)
98 {
99 case GL_FRAMEBUFFER: return true;
100 case GL_READ_FRAMEBUFFER: return true;
101 case GL_DRAW_FRAMEBUFFER: return true;
102 default: return false;
103 }
104}
105
Jamie Madill8c96d582014-03-05 15:01:23 -0500106bool ValidBufferTarget(const Context *context, GLenum target)
107{
108 switch (target)
109 {
110 case GL_ARRAY_BUFFER:
111 case GL_ELEMENT_ARRAY_BUFFER:
112 return true;
113
Jamie Madill8c96d582014-03-05 15:01:23 -0500114 case GL_PIXEL_PACK_BUFFER:
115 case GL_PIXEL_UNPACK_BUFFER:
Geoff Langcec35902014-04-16 10:52:36 -0400116 return context->getCaps().extensions.pixelBufferObject;
Shannon Woods158c4382014-05-06 13:00:07 -0400117
Shannon Woodsb3801742014-03-27 14:59:19 -0400118 case GL_COPY_READ_BUFFER:
119 case GL_COPY_WRITE_BUFFER:
Jamie Madill8c96d582014-03-05 15:01:23 -0500120 case GL_TRANSFORM_FEEDBACK_BUFFER:
121 case GL_UNIFORM_BUFFER:
122 return (context->getClientVersion() >= 3);
123
124 default:
125 return false;
126 }
127}
128
Jamie Madill70656a62014-03-05 15:01:26 -0500129bool ValidBufferParameter(const Context *context, GLenum pname)
130{
131 switch (pname)
132 {
133 case GL_BUFFER_USAGE:
134 case GL_BUFFER_SIZE:
135 return true;
136
137 // GL_BUFFER_MAP_POINTER is a special case, and may only be
138 // queried with GetBufferPointerv
139 case GL_BUFFER_ACCESS_FLAGS:
140 case GL_BUFFER_MAPPED:
141 case GL_BUFFER_MAP_OFFSET:
142 case GL_BUFFER_MAP_LENGTH:
143 return (context->getClientVersion() >= 3);
144
145 default:
146 return false;
147 }
148}
149
Jamie Madill8c96d582014-03-05 15:01:23 -0500150bool ValidMipLevel(const Context *context, GLenum target, GLint level)
Geoff Langce635692013-09-24 13:56:32 -0400151{
152 int maxLevel = 0;
153 switch (target)
154 {
155 case GL_TEXTURE_2D: maxLevel = context->getMaximum2DTextureLevel(); break;
156 case GL_TEXTURE_CUBE_MAP:
157 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
158 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
159 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
160 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
161 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
162 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: maxLevel = context->getMaximumCubeTextureLevel(); break;
163 case GL_TEXTURE_3D: maxLevel = context->getMaximum3DTextureLevel(); break;
164 case GL_TEXTURE_2D_ARRAY: maxLevel = context->getMaximum2DArrayTextureLevel(); break;
165 default: UNREACHABLE();
166 }
167
168 return level < maxLevel;
169}
170
Jamie Madill4fd75c12014-06-23 10:53:54 -0400171bool ValidImageSize(const gl::Context *context, GLenum target, GLint level,
172 GLsizei width, GLsizei height, GLsizei depth)
Geoff Langce635692013-09-24 13:56:32 -0400173{
174 if (level < 0 || width < 0 || height < 0 || depth < 0)
175 {
176 return false;
177 }
178
Jamie Madill4fd75c12014-06-23 10:53:54 -0400179 if (!context->getCaps().extensions.textureNPOT &&
180 (level != 0 && (!gl::isPow2(width) || !gl::isPow2(height) || !gl::isPow2(depth))))
Geoff Langce635692013-09-24 13:56:32 -0400181 {
182 return false;
183 }
184
185 if (!ValidMipLevel(context, target, level))
186 {
187 return false;
188 }
189
190 return true;
191}
192
Geoff Lang005df412013-10-16 14:12:50 -0400193bool ValidCompressedImageSize(const gl::Context *context, GLenum internalFormat, GLsizei width, GLsizei height)
Geoff Langd4f180b2013-09-24 13:57:44 -0400194{
Geoff Lange4a492b2014-06-19 14:14:41 -0400195 if (!IsFormatCompressed(internalFormat))
Geoff Langd4f180b2013-09-24 13:57:44 -0400196 {
197 return false;
198 }
199
Geoff Lange4a492b2014-06-19 14:14:41 -0400200 GLint blockWidth = GetCompressedBlockWidth(internalFormat);
201 GLint blockHeight = GetCompressedBlockHeight(internalFormat);
Geoff Langd4f180b2013-09-24 13:57:44 -0400202 if (width < 0 || (width > blockWidth && width % blockWidth != 0) ||
203 height < 0 || (height > blockHeight && height % blockHeight != 0))
204 {
205 return false;
206 }
207
208 return true;
209}
210
Geoff Lang37dde692014-01-31 16:34:54 -0500211bool ValidQueryType(const Context *context, GLenum queryType)
212{
213 META_ASSERT(GL_ANY_SAMPLES_PASSED == GL_ANY_SAMPLES_PASSED_EXT);
214 META_ASSERT(GL_ANY_SAMPLES_PASSED_CONSERVATIVE == GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT);
215
216 switch (queryType)
217 {
218 case GL_ANY_SAMPLES_PASSED:
219 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
220 return true;
221 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
222 return (context->getClientVersion() >= 3);
223 default:
224 return false;
225 }
226}
227
Geoff Lang48dcae72014-02-05 16:28:24 -0500228bool ValidProgram(const Context *context, GLuint id)
229{
230 // ES3 spec (section 2.11.1) -- "Commands that accept shader or program object names will generate the
231 // error INVALID_VALUE if the provided name is not the name of either a shader or program object and
232 // INVALID_OPERATION if the provided name identifies an object that is not the expected type."
233
234 if (context->getProgram(id) != NULL)
235 {
236 return true;
237 }
238 else if (context->getShader(id) != NULL)
239 {
240 // ID is the wrong type
241 return gl::error(GL_INVALID_OPERATION, false);
242 }
243 else
244 {
245 // No shader/program object has this ID
246 return gl::error(GL_INVALID_VALUE, false);
247 }
248}
249
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400250bool ValidateRenderbufferStorageParameters(const gl::Context *context, GLenum target, GLsizei samples,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400251 GLenum internalformat, GLsizei width, GLsizei height,
252 bool angleExtension)
253{
254 switch (target)
255 {
256 case GL_RENDERBUFFER:
257 break;
258 default:
259 return gl::error(GL_INVALID_ENUM, false);
260 }
261
262 if (width < 0 || height < 0 || samples < 0)
263 {
264 return gl::error(GL_INVALID_VALUE, false);
265 }
266
Geoff Langcec35902014-04-16 10:52:36 -0400267 const gl::Caps &caps = context->getCaps();
268 if (!gl::IsValidInternalFormat(internalformat, caps.extensions, context->getClientVersion()))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400269 {
270 return gl::error(GL_INVALID_ENUM, false);
271 }
272
273 // ANGLE_framebuffer_multisample does not explicitly state that the internal format must be
274 // sized but it does state that the format must be in the ES2.0 spec table 4.5 which contains
275 // only sized internal formats. The ES3 spec (section 4.4.2) does, however, state that the
276 // internal format must be sized and not an integer format if samples is greater than zero.
Geoff Lange4a492b2014-06-19 14:14:41 -0400277 if (!gl::IsSizedInternalFormat(internalformat))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400278 {
279 return gl::error(GL_INVALID_ENUM, false);
280 }
281
Geoff Lange4a492b2014-06-19 14:14:41 -0400282 GLenum componentType = gl::GetComponentType(internalformat);
Geoff Langb2f3d052013-08-13 12:49:27 -0400283 if ((componentType == GL_UNSIGNED_INT || componentType == GL_INT) && samples > 0)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400284 {
285 return gl::error(GL_INVALID_OPERATION, false);
286 }
287
Geoff Langcec35902014-04-16 10:52:36 -0400288 const TextureCaps &formatCaps = caps.textureCaps.get(internalformat);
289 if (!formatCaps.colorRendering && !formatCaps.depthRendering && !formatCaps.stencilRendering)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400290 {
291 return gl::error(GL_INVALID_ENUM, false);
292 }
293
294 if (std::max(width, height) > context->getMaximumRenderbufferDimension())
295 {
296 return gl::error(GL_INVALID_VALUE, false);
297 }
298
299 // ANGLE_framebuffer_multisample states that the value of samples must be less than or equal
300 // to MAX_SAMPLES_ANGLE (Context::getMaxSupportedSamples) while the ES3.0 spec (section 4.4.2)
301 // states that samples must be less than or equal to the maximum samples for the specified
302 // internal format.
303 if (angleExtension)
304 {
305 if (samples > context->getMaxSupportedSamples())
306 {
307 return gl::error(GL_INVALID_VALUE, false);
308 }
309 }
310 else
311 {
312 if (samples > context->getMaxSupportedFormatSamples(internalformat))
313 {
314 return gl::error(GL_INVALID_VALUE, false);
315 }
316 }
317
318 GLuint handle = context->getRenderbufferHandle();
319 if (handle == 0)
320 {
321 return gl::error(GL_INVALID_OPERATION, false);
322 }
323
324 return true;
325}
326
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500327bool ValidateFramebufferRenderbufferParameters(gl::Context *context, GLenum target, GLenum attachment,
328 GLenum renderbuffertarget, GLuint renderbuffer)
329{
330 gl::Framebuffer *framebuffer = context->getTargetFramebuffer(target);
331 GLuint framebufferHandle = context->getTargetFramebufferHandle(target);
332
333 if (!framebuffer || (framebufferHandle == 0 && renderbuffer != 0))
334 {
335 return gl::error(GL_INVALID_OPERATION, false);
336 }
337
338 if (attachment >= GL_COLOR_ATTACHMENT0_EXT && attachment <= GL_COLOR_ATTACHMENT15_EXT)
339 {
340 const unsigned int colorAttachment = (attachment - GL_COLOR_ATTACHMENT0_EXT);
341
342 if (colorAttachment >= context->getMaximumRenderTargets())
343 {
344 return gl::error(GL_INVALID_VALUE, false);
345 }
346 }
347 else
348 {
349 switch (attachment)
350 {
351 case GL_DEPTH_ATTACHMENT:
352 break;
353 case GL_STENCIL_ATTACHMENT:
354 break;
355 case GL_DEPTH_STENCIL_ATTACHMENT:
356 if (context->getClientVersion() < 3)
357 {
358 return gl::error(GL_INVALID_ENUM, false);
359 }
360 break;
361 default:
362 return gl::error(GL_INVALID_ENUM, false);
363 }
364 }
365
Jamie Madillab9d82c2014-01-21 16:38:14 -0500366 // [OpenGL ES 2.0.25] Section 4.4.3 page 112
367 // [OpenGL ES 3.0.2] Section 4.4.2 page 201
368 // 'renderbuffer' must be either zero or the name of an existing renderbuffer object of
369 // type 'renderbuffertarget', otherwise an INVALID_OPERATION error is generated.
370 if (renderbuffer != 0)
371 {
372 if (!context->getRenderbuffer(renderbuffer))
373 {
374 return gl::error(GL_INVALID_OPERATION, false);
375 }
376 }
377
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500378 return true;
379}
380
Jamie Madill3c7fa222014-06-05 13:08:51 -0400381static bool IsPartialBlit(gl::Context *context, gl::FramebufferAttachment *readBuffer, gl::FramebufferAttachment *writeBuffer,
Geoff Lang125deab2013-08-09 13:34:16 -0400382 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
383 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1)
384{
385 if (srcX0 != 0 || srcY0 != 0 || dstX0 != 0 || dstY0 != 0 ||
386 dstX1 != writeBuffer->getWidth() || dstY1 != writeBuffer->getHeight() ||
387 srcX1 != readBuffer->getWidth() || srcY1 != readBuffer->getHeight())
388 {
389 return true;
390 }
391 else if (context->isScissorTestEnabled())
392 {
393 int scissorX, scissorY, scissorWidth, scissorHeight;
394 context->getScissorParams(&scissorX, &scissorY, &scissorWidth, &scissorHeight);
395
396 return scissorX > 0 || scissorY > 0 ||
397 scissorWidth < writeBuffer->getWidth() ||
398 scissorHeight < writeBuffer->getHeight();
399 }
400 else
401 {
402 return false;
403 }
404}
405
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400406bool ValidateBlitFramebufferParameters(gl::Context *context, GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400407 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask,
408 GLenum filter, bool fromAngleExtension)
409{
410 switch (filter)
411 {
412 case GL_NEAREST:
413 break;
414 case GL_LINEAR:
415 if (fromAngleExtension)
416 {
417 return gl::error(GL_INVALID_ENUM, false);
418 }
419 break;
420 default:
421 return gl::error(GL_INVALID_ENUM, false);
422 }
423
424 if ((mask & ~(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)) != 0)
425 {
426 return gl::error(GL_INVALID_VALUE, false);
427 }
428
429 if (mask == 0)
430 {
431 // ES3.0 spec, section 4.3.2 specifies that a mask of zero is valid and no
432 // buffers are copied.
433 return false;
434 }
435
436 if (fromAngleExtension && (srcX1 - srcX0 != dstX1 - dstX0 || srcY1 - srcY0 != dstY1 - dstY0))
437 {
438 ERR("Scaling and flipping in BlitFramebufferANGLE not supported by this implementation.");
439 return gl::error(GL_INVALID_OPERATION, false);
440 }
441
442 // ES3.0 spec, section 4.3.2 states that linear filtering is only available for the
443 // color buffer, leaving only nearest being unfiltered from above
444 if ((mask & ~GL_COLOR_BUFFER_BIT) != 0 && filter != GL_NEAREST)
445 {
446 return gl::error(GL_INVALID_OPERATION, false);
447 }
448
449 if (context->getReadFramebufferHandle() == context->getDrawFramebufferHandle())
450 {
451 if (fromAngleExtension)
452 {
453 ERR("Blits with the same source and destination framebuffer are not supported by this "
454 "implementation.");
455 }
456 return gl::error(GL_INVALID_OPERATION, false);
457 }
458
459 gl::Framebuffer *readFramebuffer = context->getReadFramebuffer();
460 gl::Framebuffer *drawFramebuffer = context->getDrawFramebuffer();
461 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
462 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
463 {
464 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
465 }
466
467 if (drawFramebuffer->getSamples() != 0)
468 {
469 return gl::error(GL_INVALID_OPERATION, false);
470 }
471
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400472 bool sameBounds = srcX0 == dstX0 && srcY0 == dstY0 && srcX1 == dstX1 && srcY1 == dstY1;
473
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400474 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 Lange4a492b2014-06-19 14:14:41 -0400482 GLenum readComponentType = gl::GetComponentType(readInternalFormat);
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 Lange4a492b2014-06-19 14:14:41 -0400489 GLenum drawComponentType = gl::GetComponentType(drawInternalFormat);
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;
Geoff Lange4a492b2014-06-19 14:14:41 -0400852 GLuint clientVersion = context->getClientVersion();
Jamie Madill26e91952014-03-05 15:01:27 -0500853
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
Geoff Lange4a492b2014-06-19 14:14:41 -0400864 GLenum sizedInternalFormat = IsSizedInternalFormat(format) ? format
865 : GetSizedInternalFormat(format, type);
Jamie Madill26e91952014-03-05 15:01:27 -0500866
Geoff Lange4a492b2014-06-19 14:14:41 -0400867 GLsizei outputPitch = GetRowPitch(sizedInternalFormat, type, width, context->getPackAlignment());
Jamie Madill26e91952014-03-05 15:01:27 -0500868 // 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
Jamie Madillf2575982014-06-25 16:04:54 -0400992 if (VariableComponentType(uniformType) == GL_UNSIGNED_INT && context->getClientVersion() < 3)
Jamie Madillaa981bd2014-05-20 10:55:55 -0400993 {
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
Jamie Madillf2575982014-06-25 16:04:54 -04001003 GLenum targetBoolType = VariableBoolVectorType(uniformType);
Jamie Madill36398922014-05-20 14:51:53 -04001004 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 {
Geoff Lange4a492b2014-06-19 14:14:41 -04001249 GLint blockWidth = GetCompressedBlockWidth(textureInternalFormat);
1250 GLint blockHeight = GetCompressedBlockHeight(textureInternalFormat);
Jamie Madill560a8d82014-05-21 13:06:20 -04001251
1252 if (((width % blockWidth) != 0 && width != textureLevelWidth) ||
1253 ((height % blockHeight) != 0 && height != textureLevelHeight))
1254 {
1255 return gl::error(GL_INVALID_OPERATION, false);
1256 }
1257 }
1258
1259 if (isSubImage)
1260 {
1261 if (xoffset + width > textureLevelWidth ||
1262 yoffset + height > textureLevelHeight ||
1263 zoffset >= textureLevelDepth)
1264 {
1265 return gl::error(GL_INVALID_VALUE, false);
1266 }
1267 }
Jamie Madill6f38f822014-06-06 17:12:20 -04001268 else
1269 {
1270 if (IsCubemapTextureTarget(target) && width != height)
1271 {
1272 return gl::error(GL_INVALID_VALUE, false);
1273 }
1274
Geoff Langcec35902014-04-16 10:52:36 -04001275 if (!IsValidInternalFormat(internalformat, context->getCaps().extensions, context->getClientVersion()))
Jamie Madill6f38f822014-06-06 17:12:20 -04001276 {
1277 return gl::error(GL_INVALID_ENUM, false);
1278 }
1279
1280 int maxLevelDimension = (maxDimension >> level);
1281 if (static_cast<int>(width) > maxLevelDimension || static_cast<int>(height) > maxLevelDimension)
1282 {
1283 return gl::error(GL_INVALID_VALUE, false);
1284 }
1285 }
Jamie Madill560a8d82014-05-21 13:06:20 -04001286
1287 *textureFormatOut = textureInternalFormat;
1288 return true;
1289}
1290
Jamie Madill1aeb1312014-06-20 13:21:25 -04001291static bool ValidateDrawBase(const gl::Context *context, GLenum mode, GLsizei count)
Jamie Madill250d33f2014-06-06 17:09:03 -04001292{
Jamie Madill1aeb1312014-06-20 13:21:25 -04001293 switch (mode)
1294 {
1295 case GL_POINTS:
1296 case GL_LINES:
1297 case GL_LINE_LOOP:
1298 case GL_LINE_STRIP:
1299 case GL_TRIANGLES:
1300 case GL_TRIANGLE_STRIP:
1301 case GL_TRIANGLE_FAN:
1302 break;
1303 default:
1304 return gl::error(GL_INVALID_ENUM, false);
1305 }
1306
Jamie Madill250d33f2014-06-06 17:09:03 -04001307 if (count < 0)
1308 {
1309 return gl::error(GL_INVALID_VALUE, false);
1310 }
1311
Jamie Madill250d33f2014-06-06 17:09:03 -04001312 // Check for mapped buffers
Jamie Madillfd716582014-06-06 17:09:04 -04001313 if (context->hasMappedBuffer(GL_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001314 {
1315 return gl::error(GL_INVALID_OPERATION, false);
1316 }
1317
Jamie Madillac528012014-06-20 13:21:23 -04001318 const gl::DepthStencilState &depthStencilState = context->getDepthStencilState();
1319 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
1320 context->getStencilRef() != context->getStencilBackRef() ||
1321 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
1322 {
1323 // Note: these separate values are not supported in WebGL, due to D3D's limitations.
1324 // See Section 6.10 of the WebGL 1.0 spec
1325 ERR("This ANGLE implementation does not support separate front/back stencil "
1326 "writemasks, reference values, or stencil mask values.");
1327 return gl::error(GL_INVALID_OPERATION, false);
1328 }
1329
Jamie Madill9efa5812014-06-20 13:21:24 -04001330 if (!context->getCurrentProgram())
1331 {
1332 return gl::error(GL_INVALID_OPERATION, false);
1333 }
1334
1335 gl::ProgramBinary *programBinary = context->getCurrentProgramBinary();
1336 if (!programBinary->validateSamplers(NULL))
1337 {
1338 return gl::error(GL_INVALID_OPERATION, false);
1339 }
1340
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001341 const gl::Framebuffer *fbo = context->getDrawFramebuffer();
1342 if (!fbo || fbo->completeness() != GL_FRAMEBUFFER_COMPLETE)
1343 {
1344 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
1345 }
1346
Jamie Madill250d33f2014-06-06 17:09:03 -04001347 // No-op if zero count
1348 return (count > 0);
1349}
1350
Jamie Madillfd716582014-06-06 17:09:04 -04001351bool ValidateDrawArrays(const gl::Context *context, GLenum mode, GLint first, GLsizei count)
Jamie Madill250d33f2014-06-06 17:09:03 -04001352{
Jamie Madillfd716582014-06-06 17:09:04 -04001353 if (first < 0)
Jamie Madill250d33f2014-06-06 17:09:03 -04001354 {
1355 return gl::error(GL_INVALID_VALUE, false);
1356 }
1357
Jamie Madillfd716582014-06-06 17:09:04 -04001358 gl::TransformFeedback *curTransformFeedback = context->getCurrentTransformFeedback();
1359 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused() &&
1360 curTransformFeedback->getDrawMode() != mode)
1361 {
1362 // It is an invalid operation to call DrawArrays or DrawArraysInstanced with a draw mode
1363 // that does not match the current transform feedback object's draw mode (if transform feedback
1364 // is active), (3.0.2, section 2.14, pg 86)
1365 return gl::error(GL_INVALID_OPERATION, false);
1366 }
1367
Jamie Madill1aeb1312014-06-20 13:21:25 -04001368 if (!ValidateDrawBase(context, mode, count))
Jamie Madillfd716582014-06-06 17:09:04 -04001369 {
1370 return false;
1371 }
1372
1373 return true;
1374}
1375
1376bool ValidateDrawArraysInstanced(const gl::Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
1377{
1378 if (primcount < 0)
1379 {
1380 return gl::error(GL_INVALID_VALUE, false);
1381 }
1382
1383 if (!ValidateDrawArrays(context, mode, first, count))
1384 {
1385 return false;
1386 }
1387
1388 // No-op if zero primitive count
1389 return (primcount > 0);
1390}
1391
1392bool ValidateDrawElements(const gl::Context *context, GLenum mode, GLsizei count, GLenum type, const GLvoid* indices)
1393{
Jamie Madill250d33f2014-06-06 17:09:03 -04001394 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
Jamie Madillfd716582014-06-06 17:09:04 -04001418 if (context->hasMappedBuffer(GL_ELEMENT_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001419 {
1420 return gl::error(GL_INVALID_OPERATION, false);
1421 }
1422
Jamie Madill9efa5812014-06-20 13:21:24 -04001423 gl::VertexArray *vao = context->getCurrentVertexArray();
1424 if (!indices && !vao->getElementArrayBuffer())
1425 {
1426 return gl::error(GL_INVALID_OPERATION, false);
1427 }
1428
Jamie Madill1aeb1312014-06-20 13:21:25 -04001429 if (!ValidateDrawBase(context, mode, count))
Jamie Madillfd716582014-06-06 17:09:04 -04001430 {
1431 return false;
1432 }
1433
1434 return true;
1435}
1436
1437bool ValidateDrawElementsInstanced(const gl::Context *context, GLenum mode, GLsizei count, GLenum type,
1438 const GLvoid *indices, GLsizei primcount)
1439{
1440 if (primcount < 0)
1441 {
1442 return gl::error(GL_INVALID_VALUE, false);
1443 }
1444
1445 if (!ValidateDrawElements(context, mode, count, type, indices))
1446 {
1447 return false;
1448 }
1449
1450 // No-op zero primitive count
1451 return (primcount > 0);
Jamie Madill250d33f2014-06-06 17:09:03 -04001452}
1453
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001454}