blob: a9e04e5b99e86e8ab561da0bd434c8d0ce21dafb [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 Madilld4cfa572014-07-08 10:00:32 -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 Langc0b9ef42014-07-02 10:02:37 -0400116 return context->getExtensions().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{
Geoff Langaae65a42014-05-26 12:43:44 -0400152 size_t maxDimension = 0;
Geoff Langce635692013-09-24 13:56:32 -0400153 switch (target)
154 {
Geoff Langaae65a42014-05-26 12:43:44 -0400155 case GL_TEXTURE_2D: maxDimension = context->getCaps().max2DTextureSize; break;
Geoff Langce635692013-09-24 13:56:32 -0400156 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:
Geoff Langaae65a42014-05-26 12:43:44 -0400162 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: maxDimension = context->getCaps().maxCubeMapTextureSize; break;
163 case GL_TEXTURE_3D: maxDimension = context->getCaps().max3DTextureSize; break;
164 case GL_TEXTURE_2D_ARRAY: maxDimension = context->getCaps().max2DTextureSize; break;
Geoff Langce635692013-09-24 13:56:32 -0400165 default: UNREACHABLE();
166 }
167
Geoff Langaae65a42014-05-26 12:43:44 -0400168 return level <= gl::log2(maxDimension);
Geoff Langce635692013-09-24 13:56:32 -0400169}
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
Geoff Langc0b9ef42014-07-02 10:02:37 -0400179 if (!context->getExtensions().textureNPOT &&
Jamie Madill4fd75c12014-06-23 10:53:54 -0400180 (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
Jamie Madillb4472272014-07-03 10:38:55 -0400250bool ValidateAttachmentTarget(const gl::Context *context, GLenum attachment)
251{
252 if (attachment >= GL_COLOR_ATTACHMENT0_EXT && attachment <= GL_COLOR_ATTACHMENT15_EXT)
253 {
254 const unsigned int colorAttachment = (attachment - GL_COLOR_ATTACHMENT0_EXT);
255
Geoff Langaae65a42014-05-26 12:43:44 -0400256 if (colorAttachment >= context->getCaps().maxColorAttachments)
Jamie Madillb4472272014-07-03 10:38:55 -0400257 {
258 return gl::error(GL_INVALID_VALUE, false);
259 }
260 }
261 else
262 {
263 switch (attachment)
264 {
265 case GL_DEPTH_ATTACHMENT:
266 case GL_STENCIL_ATTACHMENT:
267 break;
268
269 case GL_DEPTH_STENCIL_ATTACHMENT:
270 if (context->getClientVersion() < 3)
271 {
272 return gl::error(GL_INVALID_ENUM, false);
273 }
274 break;
275
276 default:
277 return gl::error(GL_INVALID_ENUM, false);
278 }
279 }
280
281 return true;
282}
283
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400284bool ValidateRenderbufferStorageParameters(const gl::Context *context, GLenum target, GLsizei samples,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400285 GLenum internalformat, GLsizei width, GLsizei height,
286 bool angleExtension)
287{
288 switch (target)
289 {
290 case GL_RENDERBUFFER:
291 break;
292 default:
293 return gl::error(GL_INVALID_ENUM, false);
294 }
295
296 if (width < 0 || height < 0 || samples < 0)
297 {
298 return gl::error(GL_INVALID_VALUE, false);
299 }
300
Geoff Langc0b9ef42014-07-02 10:02:37 -0400301 if (!gl::IsValidInternalFormat(internalformat, context->getExtensions(), context->getClientVersion()))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400302 {
303 return gl::error(GL_INVALID_ENUM, false);
304 }
305
306 // ANGLE_framebuffer_multisample does not explicitly state that the internal format must be
307 // sized but it does state that the format must be in the ES2.0 spec table 4.5 which contains
308 // only sized internal formats. The ES3 spec (section 4.4.2) does, however, state that the
309 // internal format must be sized and not an integer format if samples is greater than zero.
Geoff Lange4a492b2014-06-19 14:14:41 -0400310 if (!gl::IsSizedInternalFormat(internalformat))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400311 {
312 return gl::error(GL_INVALID_ENUM, false);
313 }
314
Geoff Lange4a492b2014-06-19 14:14:41 -0400315 GLenum componentType = gl::GetComponentType(internalformat);
Geoff Langb2f3d052013-08-13 12:49:27 -0400316 if ((componentType == GL_UNSIGNED_INT || componentType == GL_INT) && samples > 0)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400317 {
318 return gl::error(GL_INVALID_OPERATION, false);
319 }
320
Geoff Langc0b9ef42014-07-02 10:02:37 -0400321 const TextureCaps &formatCaps = context->getTextureCaps().get(internalformat);
Geoff Langcec35902014-04-16 10:52:36 -0400322 if (!formatCaps.colorRendering && !formatCaps.depthRendering && !formatCaps.stencilRendering)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400323 {
324 return gl::error(GL_INVALID_ENUM, false);
325 }
326
Geoff Langaae65a42014-05-26 12:43:44 -0400327 if (static_cast<GLuint>(std::max(width, height)) > context->getCaps().maxRenderbufferSize)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400328 {
329 return gl::error(GL_INVALID_VALUE, false);
330 }
331
332 // ANGLE_framebuffer_multisample states that the value of samples must be less than or equal
333 // to MAX_SAMPLES_ANGLE (Context::getMaxSupportedSamples) while the ES3.0 spec (section 4.4.2)
334 // states that samples must be less than or equal to the maximum samples for the specified
335 // internal format.
336 if (angleExtension)
337 {
338 if (samples > context->getMaxSupportedSamples())
339 {
340 return gl::error(GL_INVALID_VALUE, false);
341 }
342 }
343 else
344 {
345 if (samples > context->getMaxSupportedFormatSamples(internalformat))
346 {
347 return gl::error(GL_INVALID_VALUE, false);
348 }
349 }
350
351 GLuint handle = context->getRenderbufferHandle();
352 if (handle == 0)
353 {
354 return gl::error(GL_INVALID_OPERATION, false);
355 }
356
357 return true;
358}
359
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500360bool ValidateFramebufferRenderbufferParameters(gl::Context *context, GLenum target, GLenum attachment,
361 GLenum renderbuffertarget, GLuint renderbuffer)
362{
Shannon Woods1da3cf62014-06-27 15:32:23 -0400363 if (!ValidFramebufferTarget(target))
364 {
365 return gl::error(GL_INVALID_ENUM, false);
366 }
367
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500368 gl::Framebuffer *framebuffer = context->getTargetFramebuffer(target);
369 GLuint framebufferHandle = context->getTargetFramebufferHandle(target);
370
371 if (!framebuffer || (framebufferHandle == 0 && renderbuffer != 0))
372 {
373 return gl::error(GL_INVALID_OPERATION, false);
374 }
375
Jamie Madillb4472272014-07-03 10:38:55 -0400376 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500377 {
Jamie Madillb4472272014-07-03 10:38:55 -0400378 return false;
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500379 }
380
Jamie Madillab9d82c2014-01-21 16:38:14 -0500381 // [OpenGL ES 2.0.25] Section 4.4.3 page 112
382 // [OpenGL ES 3.0.2] Section 4.4.2 page 201
383 // 'renderbuffer' must be either zero or the name of an existing renderbuffer object of
384 // type 'renderbuffertarget', otherwise an INVALID_OPERATION error is generated.
385 if (renderbuffer != 0)
386 {
387 if (!context->getRenderbuffer(renderbuffer))
388 {
389 return gl::error(GL_INVALID_OPERATION, false);
390 }
391 }
392
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500393 return true;
394}
395
Jamie Madill3c7fa222014-06-05 13:08:51 -0400396static bool IsPartialBlit(gl::Context *context, gl::FramebufferAttachment *readBuffer, gl::FramebufferAttachment *writeBuffer,
Geoff Lang125deab2013-08-09 13:34:16 -0400397 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
398 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1)
399{
400 if (srcX0 != 0 || srcY0 != 0 || dstX0 != 0 || dstY0 != 0 ||
401 dstX1 != writeBuffer->getWidth() || dstY1 != writeBuffer->getHeight() ||
402 srcX1 != readBuffer->getWidth() || srcY1 != readBuffer->getHeight())
403 {
404 return true;
405 }
406 else if (context->isScissorTestEnabled())
407 {
408 int scissorX, scissorY, scissorWidth, scissorHeight;
409 context->getScissorParams(&scissorX, &scissorY, &scissorWidth, &scissorHeight);
410
411 return scissorX > 0 || scissorY > 0 ||
412 scissorWidth < writeBuffer->getWidth() ||
413 scissorHeight < writeBuffer->getHeight();
414 }
415 else
416 {
417 return false;
418 }
419}
420
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400421bool ValidateBlitFramebufferParameters(gl::Context *context, GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400422 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask,
423 GLenum filter, bool fromAngleExtension)
424{
425 switch (filter)
426 {
427 case GL_NEAREST:
428 break;
429 case GL_LINEAR:
430 if (fromAngleExtension)
431 {
432 return gl::error(GL_INVALID_ENUM, false);
433 }
434 break;
435 default:
436 return gl::error(GL_INVALID_ENUM, false);
437 }
438
439 if ((mask & ~(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)) != 0)
440 {
441 return gl::error(GL_INVALID_VALUE, false);
442 }
443
444 if (mask == 0)
445 {
446 // ES3.0 spec, section 4.3.2 specifies that a mask of zero is valid and no
447 // buffers are copied.
448 return false;
449 }
450
451 if (fromAngleExtension && (srcX1 - srcX0 != dstX1 - dstX0 || srcY1 - srcY0 != dstY1 - dstY0))
452 {
453 ERR("Scaling and flipping in BlitFramebufferANGLE not supported by this implementation.");
454 return gl::error(GL_INVALID_OPERATION, false);
455 }
456
457 // ES3.0 spec, section 4.3.2 states that linear filtering is only available for the
458 // color buffer, leaving only nearest being unfiltered from above
459 if ((mask & ~GL_COLOR_BUFFER_BIT) != 0 && filter != GL_NEAREST)
460 {
461 return gl::error(GL_INVALID_OPERATION, false);
462 }
463
464 if (context->getReadFramebufferHandle() == context->getDrawFramebufferHandle())
465 {
466 if (fromAngleExtension)
467 {
468 ERR("Blits with the same source and destination framebuffer are not supported by this "
469 "implementation.");
470 }
471 return gl::error(GL_INVALID_OPERATION, false);
472 }
473
474 gl::Framebuffer *readFramebuffer = context->getReadFramebuffer();
475 gl::Framebuffer *drawFramebuffer = context->getDrawFramebuffer();
476 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
477 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
478 {
479 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
480 }
481
482 if (drawFramebuffer->getSamples() != 0)
483 {
484 return gl::error(GL_INVALID_OPERATION, false);
485 }
486
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400487 bool sameBounds = srcX0 == dstX0 && srcY0 == dstY0 && srcX1 == dstX1 && srcY1 == dstY1;
488
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400489 if (mask & GL_COLOR_BUFFER_BIT)
490 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400491 gl::FramebufferAttachment *readColorBuffer = readFramebuffer->getReadColorbuffer();
492 gl::FramebufferAttachment *drawColorBuffer = drawFramebuffer->getFirstColorbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400493
494 if (readColorBuffer && drawColorBuffer)
495 {
Geoff Lang005df412013-10-16 14:12:50 -0400496 GLenum readInternalFormat = readColorBuffer->getActualFormat();
Geoff Lange4a492b2014-06-19 14:14:41 -0400497 GLenum readComponentType = gl::GetComponentType(readInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400498
499 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; i++)
500 {
501 if (drawFramebuffer->isEnabledColorAttachment(i))
502 {
Geoff Lang005df412013-10-16 14:12:50 -0400503 GLenum drawInternalFormat = drawFramebuffer->getColorbuffer(i)->getActualFormat();
Geoff Lange4a492b2014-06-19 14:14:41 -0400504 GLenum drawComponentType = gl::GetComponentType(drawInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400505
Geoff Langb2f3d052013-08-13 12:49:27 -0400506 // The GL ES 3.0.2 spec (pg 193) states that:
507 // 1) If the read buffer is fixed point format, the draw buffer must be as well
508 // 2) If the read buffer is an unsigned integer format, the draw buffer must be as well
509 // 3) If the read buffer is a signed integer format, the draw buffer must be as well
510 if ( (readComponentType == GL_UNSIGNED_NORMALIZED || readComponentType == GL_SIGNED_NORMALIZED) &&
511 !(drawComponentType == GL_UNSIGNED_NORMALIZED || drawComponentType == GL_SIGNED_NORMALIZED))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400512 {
513 return gl::error(GL_INVALID_OPERATION, false);
514 }
515
Geoff Langb2f3d052013-08-13 12:49:27 -0400516 if (readComponentType == GL_UNSIGNED_INT && drawComponentType != GL_UNSIGNED_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400517 {
518 return gl::error(GL_INVALID_OPERATION, false);
519 }
520
Geoff Langb2f3d052013-08-13 12:49:27 -0400521 if (readComponentType == GL_INT && drawComponentType != GL_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400522 {
523 return gl::error(GL_INVALID_OPERATION, false);
524 }
525
Geoff Langb2f3d052013-08-13 12:49:27 -0400526 if (readColorBuffer->getSamples() > 0 && (readInternalFormat != drawInternalFormat || !sameBounds))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400527 {
528 return gl::error(GL_INVALID_OPERATION, false);
529 }
530 }
531 }
532
Geoff Langb2f3d052013-08-13 12:49:27 -0400533 if ((readComponentType == GL_INT || readComponentType == GL_UNSIGNED_INT) && filter == GL_LINEAR)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400534 {
535 return gl::error(GL_INVALID_OPERATION, false);
536 }
537
538 if (fromAngleExtension)
539 {
540 const GLenum readColorbufferType = readFramebuffer->getReadColorbufferType();
541 if (readColorbufferType != GL_TEXTURE_2D && readColorbufferType != GL_RENDERBUFFER)
542 {
543 return gl::error(GL_INVALID_OPERATION, false);
544 }
545
546 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
547 {
548 if (drawFramebuffer->isEnabledColorAttachment(colorAttachment))
549 {
Jamie Madille92a3542014-07-03 10:38:58 -0400550 FramebufferAttachment *attachment = drawFramebuffer->getColorbuffer(colorAttachment);
551 ASSERT(attachment);
552
553 if (attachment->type() != GL_TEXTURE_2D && attachment->type() != GL_RENDERBUFFER)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400554 {
555 return gl::error(GL_INVALID_OPERATION, false);
556 }
557
Jamie Madille92a3542014-07-03 10:38:58 -0400558 if (attachment->getActualFormat() != readColorBuffer->getActualFormat())
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400559 {
560 return gl::error(GL_INVALID_OPERATION, false);
561 }
562 }
563 }
Geoff Lang125deab2013-08-09 13:34:16 -0400564 if (readFramebuffer->getSamples() != 0 && IsPartialBlit(context, readColorBuffer, drawColorBuffer,
565 srcX0, srcY0, srcX1, srcY1,
566 dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400567 {
568 return gl::error(GL_INVALID_OPERATION, false);
569 }
570 }
571 }
572 }
573
574 if (mask & GL_DEPTH_BUFFER_BIT)
575 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400576 gl::FramebufferAttachment *readDepthBuffer = readFramebuffer->getDepthbuffer();
577 gl::FramebufferAttachment *drawDepthBuffer = drawFramebuffer->getDepthbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400578
579 if (readDepthBuffer && drawDepthBuffer)
580 {
581 if (readDepthBuffer->getActualFormat() != drawDepthBuffer->getActualFormat())
582 {
583 return gl::error(GL_INVALID_OPERATION, false);
584 }
585
586 if (readDepthBuffer->getSamples() > 0 && !sameBounds)
587 {
588 return gl::error(GL_INVALID_OPERATION, false);
589 }
590
591 if (fromAngleExtension)
592 {
Geoff Lang125deab2013-08-09 13:34:16 -0400593 if (IsPartialBlit(context, readDepthBuffer, drawDepthBuffer,
594 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400595 {
596 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
597 return gl::error(GL_INVALID_OPERATION, false); // only whole-buffer copies are permitted
598 }
599
600 if (readDepthBuffer->getSamples() != 0 || drawDepthBuffer->getSamples() != 0)
601 {
602 return gl::error(GL_INVALID_OPERATION, false);
603 }
604 }
605 }
606 }
607
608 if (mask & GL_STENCIL_BUFFER_BIT)
609 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400610 gl::FramebufferAttachment *readStencilBuffer = readFramebuffer->getStencilbuffer();
611 gl::FramebufferAttachment *drawStencilBuffer = drawFramebuffer->getStencilbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400612
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400613 if (readStencilBuffer && drawStencilBuffer)
614 {
615 if (readStencilBuffer->getActualFormat() != drawStencilBuffer->getActualFormat())
616 {
617 return gl::error(GL_INVALID_OPERATION, false);
618 }
619
620 if (readStencilBuffer->getSamples() > 0 && !sameBounds)
621 {
622 return gl::error(GL_INVALID_OPERATION, false);
623 }
624
625 if (fromAngleExtension)
626 {
Geoff Lang125deab2013-08-09 13:34:16 -0400627 if (IsPartialBlit(context, readStencilBuffer, drawStencilBuffer,
628 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400629 {
630 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
631 return gl::error(GL_INVALID_OPERATION, false); // only whole-buffer copies are permitted
632 }
633
634 if (readStencilBuffer->getSamples() != 0 || drawStencilBuffer->getSamples() != 0)
635 {
636 return gl::error(GL_INVALID_OPERATION, false);
637 }
638 }
639 }
640 }
641
642 return true;
643}
644
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400645bool ValidateGetVertexAttribParameters(GLenum pname, int clientVersion)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400646{
647 switch (pname)
648 {
649 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
650 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
651 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
652 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
653 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
654 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
655 case GL_CURRENT_VERTEX_ATTRIB:
656 return true;
657
658 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR:
659 // Don't verify ES3 context because GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE uses
660 // the same constant.
661 META_ASSERT(GL_VERTEX_ATTRIB_ARRAY_DIVISOR == GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE);
662 return true;
663
664 case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
665 return ((clientVersion >= 3) ? true : gl::error(GL_INVALID_ENUM, false));
666
667 default:
668 return gl::error(GL_INVALID_ENUM, false);
669 }
670}
671
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400672bool ValidateTexParamParameters(gl::Context *context, GLenum pname, GLint param)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400673{
674 switch (pname)
675 {
676 case GL_TEXTURE_WRAP_R:
677 case GL_TEXTURE_SWIZZLE_R:
678 case GL_TEXTURE_SWIZZLE_G:
679 case GL_TEXTURE_SWIZZLE_B:
680 case GL_TEXTURE_SWIZZLE_A:
681 case GL_TEXTURE_BASE_LEVEL:
682 case GL_TEXTURE_MAX_LEVEL:
683 case GL_TEXTURE_COMPARE_MODE:
684 case GL_TEXTURE_COMPARE_FUNC:
685 case GL_TEXTURE_MIN_LOD:
686 case GL_TEXTURE_MAX_LOD:
687 if (context->getClientVersion() < 3)
688 {
689 return gl::error(GL_INVALID_ENUM, false);
690 }
691 break;
692
693 default: break;
694 }
695
696 switch (pname)
697 {
698 case GL_TEXTURE_WRAP_S:
699 case GL_TEXTURE_WRAP_T:
700 case GL_TEXTURE_WRAP_R:
701 switch (param)
702 {
703 case GL_REPEAT:
704 case GL_CLAMP_TO_EDGE:
705 case GL_MIRRORED_REPEAT:
706 return true;
707 default:
708 return gl::error(GL_INVALID_ENUM, false);
709 }
710
711 case GL_TEXTURE_MIN_FILTER:
712 switch (param)
713 {
714 case GL_NEAREST:
715 case GL_LINEAR:
716 case GL_NEAREST_MIPMAP_NEAREST:
717 case GL_LINEAR_MIPMAP_NEAREST:
718 case GL_NEAREST_MIPMAP_LINEAR:
719 case GL_LINEAR_MIPMAP_LINEAR:
720 return true;
721 default:
722 return gl::error(GL_INVALID_ENUM, false);
723 }
724 break;
725
726 case GL_TEXTURE_MAG_FILTER:
727 switch (param)
728 {
729 case GL_NEAREST:
730 case GL_LINEAR:
731 return true;
732 default:
733 return gl::error(GL_INVALID_ENUM, false);
734 }
735 break;
736
737 case GL_TEXTURE_USAGE_ANGLE:
738 switch (param)
739 {
740 case GL_NONE:
741 case GL_FRAMEBUFFER_ATTACHMENT_ANGLE:
742 return true;
743 default:
744 return gl::error(GL_INVALID_ENUM, false);
745 }
746 break;
747
748 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
Geoff Langc0b9ef42014-07-02 10:02:37 -0400749 if (!context->getExtensions().textureFilterAnisotropic)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400750 {
751 return gl::error(GL_INVALID_ENUM, false);
752 }
753
754 // we assume the parameter passed to this validation method is truncated, not rounded
755 if (param < 1)
756 {
757 return gl::error(GL_INVALID_VALUE, false);
758 }
759 return true;
760
761 case GL_TEXTURE_MIN_LOD:
762 case GL_TEXTURE_MAX_LOD:
763 // any value is permissible
764 return true;
765
766 case GL_TEXTURE_COMPARE_MODE:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400767 // Acceptable mode parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400768 switch (param)
769 {
770 case GL_NONE:
771 case GL_COMPARE_REF_TO_TEXTURE:
772 return true;
773 default:
774 return gl::error(GL_INVALID_ENUM, false);
775 }
776 break;
777
778 case GL_TEXTURE_COMPARE_FUNC:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400779 // Acceptable function parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400780 switch (param)
781 {
782 case GL_LEQUAL:
783 case GL_GEQUAL:
784 case GL_LESS:
785 case GL_GREATER:
786 case GL_EQUAL:
787 case GL_NOTEQUAL:
788 case GL_ALWAYS:
789 case GL_NEVER:
790 return true;
791 default:
792 return gl::error(GL_INVALID_ENUM, false);
793 }
794 break;
795
796 case GL_TEXTURE_SWIZZLE_R:
797 case GL_TEXTURE_SWIZZLE_G:
798 case GL_TEXTURE_SWIZZLE_B:
799 case GL_TEXTURE_SWIZZLE_A:
Geoff Langbc90a482013-09-17 16:51:27 -0400800 switch (param)
801 {
802 case GL_RED:
803 case GL_GREEN:
804 case GL_BLUE:
805 case GL_ALPHA:
806 case GL_ZERO:
807 case GL_ONE:
808 return true;
809 default:
810 return gl::error(GL_INVALID_ENUM, false);
811 }
812 break;
813
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400814 case GL_TEXTURE_BASE_LEVEL:
815 case GL_TEXTURE_MAX_LEVEL:
Nicolas Capens8de68282014-04-04 11:10:27 -0400816 if (param < 0)
817 {
818 return gl::error(GL_INVALID_VALUE, false);
819 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400820 return true;
821
822 default:
823 return gl::error(GL_INVALID_ENUM, false);
824 }
825}
826
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400827bool ValidateSamplerObjectParameter(GLenum pname)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400828{
829 switch (pname)
830 {
831 case GL_TEXTURE_MIN_FILTER:
832 case GL_TEXTURE_MAG_FILTER:
833 case GL_TEXTURE_WRAP_S:
834 case GL_TEXTURE_WRAP_T:
835 case GL_TEXTURE_WRAP_R:
836 case GL_TEXTURE_MIN_LOD:
837 case GL_TEXTURE_MAX_LOD:
838 case GL_TEXTURE_COMPARE_MODE:
839 case GL_TEXTURE_COMPARE_FUNC:
840 return true;
841
842 default:
843 return gl::error(GL_INVALID_ENUM, false);
844 }
845}
846
Jamie Madill26e91952014-03-05 15:01:27 -0500847bool ValidateReadPixelsParameters(gl::Context *context, GLint x, GLint y, GLsizei width, GLsizei height,
848 GLenum format, GLenum type, GLsizei *bufSize, GLvoid *pixels)
849{
850 gl::Framebuffer *framebuffer = context->getReadFramebuffer();
Jamie Madill893ab082014-05-16 16:56:10 -0400851 ASSERT(framebuffer);
Jamie Madill26e91952014-03-05 15:01:27 -0500852
853 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
854 {
855 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
856 }
857
858 if (context->getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
859 {
860 return gl::error(GL_INVALID_OPERATION, false);
861 }
862
Jamie Madill893ab082014-05-16 16:56:10 -0400863 if (!framebuffer->getReadColorbuffer())
864 {
865 return gl::error(GL_INVALID_OPERATION, false);
866 }
867
Jamie Madill26e91952014-03-05 15:01:27 -0500868 GLenum currentInternalFormat, currentFormat, currentType;
Geoff Lange4a492b2014-06-19 14:14:41 -0400869 GLuint clientVersion = context->getClientVersion();
Jamie Madill26e91952014-03-05 15:01:27 -0500870
Jamie Madill893ab082014-05-16 16:56:10 -0400871 context->getCurrentReadFormatType(&currentInternalFormat, &currentFormat, &currentType);
Jamie Madill26e91952014-03-05 15:01:27 -0500872
Geoff Langbdc9b2f2014-04-16 14:41:54 -0400873 bool validReadFormat = (clientVersion < 3) ? ValidES2ReadFormatType(context, format, type) :
874 ValidES3ReadFormatType(context, currentInternalFormat, format, type);
Jamie Madill26e91952014-03-05 15:01:27 -0500875
876 if (!(currentFormat == format && currentType == type) && !validReadFormat)
877 {
878 return gl::error(GL_INVALID_OPERATION, false);
879 }
880
Geoff Lange4a492b2014-06-19 14:14:41 -0400881 GLenum sizedInternalFormat = IsSizedInternalFormat(format) ? format
882 : GetSizedInternalFormat(format, type);
Jamie Madill26e91952014-03-05 15:01:27 -0500883
Geoff Lange4a492b2014-06-19 14:14:41 -0400884 GLsizei outputPitch = GetRowPitch(sizedInternalFormat, type, width, context->getPackAlignment());
Jamie Madill26e91952014-03-05 15:01:27 -0500885 // sized query sanity check
886 if (bufSize)
887 {
888 int requiredSize = outputPitch * height;
889 if (requiredSize > *bufSize)
890 {
891 return gl::error(GL_INVALID_OPERATION, false);
892 }
893 }
894
895 return true;
896}
897
Jamie Madilldb2f14c2014-05-13 13:56:30 -0400898bool ValidateBeginQuery(gl::Context *context, GLenum target, GLuint id)
899{
900 if (!ValidQueryType(context, target))
901 {
902 return gl::error(GL_INVALID_ENUM, false);
903 }
904
905 if (id == 0)
906 {
907 return gl::error(GL_INVALID_OPERATION, false);
908 }
909
910 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
911 // of zero, if the active query object name for <target> is non-zero (for the
912 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
913 // the active query for either target is non-zero), if <id> is the name of an
914 // existing query object whose type does not match <target>, or if <id> is the
915 // active query object name for any query type, the error INVALID_OPERATION is
916 // generated.
917
918 // Ensure no other queries are active
919 // NOTE: If other queries than occlusion are supported, we will need to check
920 // separately that:
921 // a) The query ID passed is not the current active query for any target/type
922 // b) There are no active queries for the requested target (and in the case
923 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
924 // no query may be active for either if glBeginQuery targets either.
925 if (context->isQueryActive())
926 {
927 return gl::error(GL_INVALID_OPERATION, false);
928 }
929
930 Query *queryObject = context->getQuery(id, true, target);
931
932 // check that name was obtained with glGenQueries
933 if (!queryObject)
934 {
935 return gl::error(GL_INVALID_OPERATION, false);
936 }
937
938 // check for type mismatch
939 if (queryObject->getType() != target)
940 {
941 return gl::error(GL_INVALID_OPERATION, false);
942 }
943
944 return true;
945}
946
Jamie Madill45c785d2014-05-13 14:09:34 -0400947bool ValidateEndQuery(gl::Context *context, GLenum target)
948{
949 if (!ValidQueryType(context, target))
950 {
951 return gl::error(GL_INVALID_ENUM, false);
952 }
953
954 const Query *queryObject = context->getActiveQuery(target);
955
956 if (queryObject == NULL)
957 {
958 return gl::error(GL_INVALID_OPERATION, false);
959 }
960
961 if (!queryObject->isStarted())
962 {
963 return gl::error(GL_INVALID_OPERATION, false);
964 }
965
966 return true;
967}
968
Jamie Madill36398922014-05-20 14:51:53 -0400969static bool ValidateUniformCommonBase(gl::Context *context, GLenum targetUniformType,
970 GLint location, GLsizei count, LinkedUniform **uniformOut)
Jamie Madilld7c7bb22014-05-20 10:55:54 -0400971{
972 if (count < 0)
973 {
974 return gl::error(GL_INVALID_VALUE, false);
975 }
976
Jamie Madilld7c7bb22014-05-20 10:55:54 -0400977 gl::ProgramBinary *programBinary = context->getCurrentProgramBinary();
978 if (!programBinary)
979 {
980 return gl::error(GL_INVALID_OPERATION, false);
981 }
982
983 if (location == -1)
984 {
985 // Silently ignore the uniform command
986 return false;
987 }
988
Jamie Madill36398922014-05-20 14:51:53 -0400989 if (!programBinary->isValidUniformLocation(location))
990 {
991 return gl::error(GL_INVALID_OPERATION, false);
992 }
993
994 LinkedUniform *uniform = programBinary->getUniformByLocation(location);
995
996 // attempting to write an array to a non-array uniform is an INVALID_OPERATION
997 if (uniform->elementCount() == 1 && count > 1)
998 {
999 return gl::error(GL_INVALID_OPERATION, false);
1000 }
1001
1002 *uniformOut = uniform;
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001003 return true;
1004}
1005
Jamie Madillaa981bd2014-05-20 10:55:55 -04001006bool ValidateUniform(gl::Context *context, GLenum uniformType, GLint location, GLsizei count)
1007{
1008 // Check for ES3 uniform entry points
Jamie Madillf2575982014-06-25 16:04:54 -04001009 if (VariableComponentType(uniformType) == GL_UNSIGNED_INT && context->getClientVersion() < 3)
Jamie Madillaa981bd2014-05-20 10:55:55 -04001010 {
1011 return gl::error(GL_INVALID_OPERATION, false);
1012 }
1013
Jamie Madill36398922014-05-20 14:51:53 -04001014 LinkedUniform *uniform = NULL;
1015 if (!ValidateUniformCommonBase(context, uniformType, location, count, &uniform))
1016 {
1017 return false;
1018 }
1019
Jamie Madillf2575982014-06-25 16:04:54 -04001020 GLenum targetBoolType = VariableBoolVectorType(uniformType);
Jamie Madill36398922014-05-20 14:51:53 -04001021 bool samplerUniformCheck = (IsSampler(uniform->type) && uniformType == GL_INT);
1022 if (!samplerUniformCheck && uniformType != uniform->type && targetBoolType != uniform->type)
1023 {
1024 return gl::error(GL_INVALID_OPERATION, false);
1025 }
1026
1027 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001028}
1029
1030bool ValidateUniformMatrix(gl::Context *context, GLenum matrixType, GLint location, GLsizei count,
1031 GLboolean transpose)
1032{
1033 // Check for ES3 uniform entry points
1034 int rows = VariableRowCount(matrixType);
1035 int cols = VariableColumnCount(matrixType);
1036 if (rows != cols && context->getClientVersion() < 3)
1037 {
1038 return gl::error(GL_INVALID_OPERATION, false);
1039 }
1040
1041 if (transpose != GL_FALSE && context->getClientVersion() < 3)
1042 {
1043 return gl::error(GL_INVALID_VALUE, false);
1044 }
1045
Jamie Madill36398922014-05-20 14:51:53 -04001046 LinkedUniform *uniform = NULL;
1047 if (!ValidateUniformCommonBase(context, matrixType, location, count, &uniform))
1048 {
1049 return false;
1050 }
1051
1052 if (uniform->type != matrixType)
1053 {
1054 return gl::error(GL_INVALID_OPERATION, false);
1055 }
1056
1057 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001058}
1059
Jamie Madill893ab082014-05-16 16:56:10 -04001060bool ValidateStateQuery(gl::Context *context, GLenum pname, GLenum *nativeType, unsigned int *numParams)
1061{
1062 if (!context->getQueryParameterInfo(pname, nativeType, numParams))
1063 {
1064 return gl::error(GL_INVALID_ENUM, false);
1065 }
1066
1067 if (pname >= GL_DRAW_BUFFER0 && pname <= GL_DRAW_BUFFER15)
1068 {
1069 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0);
1070
Geoff Langaae65a42014-05-26 12:43:44 -04001071 if (colorAttachment >= context->getCaps().maxDrawBuffers)
Jamie Madill893ab082014-05-16 16:56:10 -04001072 {
1073 return gl::error(GL_INVALID_OPERATION, false);
1074 }
1075 }
1076
1077 switch (pname)
1078 {
1079 case GL_TEXTURE_BINDING_2D:
1080 case GL_TEXTURE_BINDING_CUBE_MAP:
1081 case GL_TEXTURE_BINDING_3D:
1082 case GL_TEXTURE_BINDING_2D_ARRAY:
1083 if (context->getActiveSampler() >= context->getMaximumCombinedTextureImageUnits())
1084 {
1085 return gl::error(GL_INVALID_OPERATION, false);
1086 }
1087 break;
1088
1089 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1090 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1091 {
1092 Framebuffer *framebuffer = context->getReadFramebuffer();
1093 ASSERT(framebuffer);
1094 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1095 {
1096 return gl::error(GL_INVALID_OPERATION, false);
1097 }
1098
Jamie Madill3c7fa222014-06-05 13:08:51 -04001099 FramebufferAttachment *attachment = framebuffer->getReadColorbuffer();
1100 if (!attachment)
Jamie Madill893ab082014-05-16 16:56:10 -04001101 {
1102 return gl::error(GL_INVALID_OPERATION, false);
1103 }
1104 }
1105 break;
1106
1107 default:
1108 break;
1109 }
1110
1111 // pname is valid, but there are no parameters to return
1112 if (numParams == 0)
1113 {
1114 return false;
1115 }
1116
1117 return true;
1118}
1119
Jamie Madill560a8d82014-05-21 13:06:20 -04001120bool ValidateCopyTexImageParametersBase(gl::Context* context, GLenum target, GLint level, GLenum internalformat, bool isSubImage,
1121 GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height,
1122 GLint border, GLenum *textureFormatOut)
1123{
1124
1125 if (!ValidTexture2DDestinationTarget(context, target))
1126 {
1127 return gl::error(GL_INVALID_ENUM, false);
1128 }
1129
1130 if (level < 0 || xoffset < 0 || yoffset < 0 || zoffset < 0 || width < 0 || height < 0)
1131 {
1132 return gl::error(GL_INVALID_VALUE, false);
1133 }
1134
1135 if (std::numeric_limits<GLsizei>::max() - xoffset < width || std::numeric_limits<GLsizei>::max() - yoffset < height)
1136 {
1137 return gl::error(GL_INVALID_VALUE, false);
1138 }
1139
1140 if (border != 0)
1141 {
1142 return gl::error(GL_INVALID_VALUE, false);
1143 }
1144
1145 if (!ValidMipLevel(context, target, level))
1146 {
1147 return gl::error(GL_INVALID_VALUE, false);
1148 }
1149
1150 gl::Framebuffer *framebuffer = context->getReadFramebuffer();
1151 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1152 {
1153 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
1154 }
1155
1156 if (context->getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
1157 {
1158 return gl::error(GL_INVALID_OPERATION, false);
1159 }
1160
Geoff Langaae65a42014-05-26 12:43:44 -04001161 const gl::Caps &caps = context->getCaps();
1162
Jamie Madill560a8d82014-05-21 13:06:20 -04001163 gl::Texture *texture = NULL;
1164 GLenum textureInternalFormat = GL_NONE;
1165 bool textureCompressed = false;
1166 bool textureIsDepth = false;
1167 GLint textureLevelWidth = 0;
1168 GLint textureLevelHeight = 0;
1169 GLint textureLevelDepth = 0;
Geoff Langaae65a42014-05-26 12:43:44 -04001170 GLuint maxDimension = 0;
Jamie Madill560a8d82014-05-21 13:06:20 -04001171
1172 switch (target)
1173 {
1174 case GL_TEXTURE_2D:
1175 {
1176 gl::Texture2D *texture2d = context->getTexture2D();
1177 if (texture2d)
1178 {
1179 textureInternalFormat = texture2d->getInternalFormat(level);
1180 textureCompressed = texture2d->isCompressed(level);
1181 textureIsDepth = texture2d->isDepth(level);
1182 textureLevelWidth = texture2d->getWidth(level);
1183 textureLevelHeight = texture2d->getHeight(level);
1184 textureLevelDepth = 1;
1185 texture = texture2d;
Geoff Langaae65a42014-05-26 12:43:44 -04001186 maxDimension = caps.max2DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001187 }
1188 }
1189 break;
1190
1191 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1192 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1193 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1194 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1195 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1196 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1197 {
1198 gl::TextureCubeMap *textureCube = context->getTextureCubeMap();
1199 if (textureCube)
1200 {
1201 textureInternalFormat = textureCube->getInternalFormat(target, level);
1202 textureCompressed = textureCube->isCompressed(target, level);
1203 textureIsDepth = false;
1204 textureLevelWidth = textureCube->getWidth(target, level);
1205 textureLevelHeight = textureCube->getHeight(target, level);
1206 textureLevelDepth = 1;
1207 texture = textureCube;
Geoff Langaae65a42014-05-26 12:43:44 -04001208 maxDimension = caps.maxCubeMapTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001209 }
1210 }
1211 break;
1212
1213 case GL_TEXTURE_2D_ARRAY:
1214 {
1215 gl::Texture2DArray *texture2dArray = context->getTexture2DArray();
1216 if (texture2dArray)
1217 {
1218 textureInternalFormat = texture2dArray->getInternalFormat(level);
1219 textureCompressed = texture2dArray->isCompressed(level);
1220 textureIsDepth = texture2dArray->isDepth(level);
1221 textureLevelWidth = texture2dArray->getWidth(level);
1222 textureLevelHeight = texture2dArray->getHeight(level);
1223 textureLevelDepth = texture2dArray->getLayers(level);
1224 texture = texture2dArray;
Geoff Langaae65a42014-05-26 12:43:44 -04001225 maxDimension = caps.max2DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001226 }
1227 }
1228 break;
1229
1230 case GL_TEXTURE_3D:
1231 {
1232 gl::Texture3D *texture3d = context->getTexture3D();
1233 if (texture3d)
1234 {
1235 textureInternalFormat = texture3d->getInternalFormat(level);
1236 textureCompressed = texture3d->isCompressed(level);
1237 textureIsDepth = texture3d->isDepth(level);
1238 textureLevelWidth = texture3d->getWidth(level);
1239 textureLevelHeight = texture3d->getHeight(level);
1240 textureLevelDepth = texture3d->getDepth(level);
1241 texture = texture3d;
Geoff Langaae65a42014-05-26 12:43:44 -04001242 maxDimension = caps.max3DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001243 }
1244 }
1245 break;
1246
1247 default:
1248 return gl::error(GL_INVALID_ENUM, false);
1249 }
1250
1251 if (!texture)
1252 {
1253 return gl::error(GL_INVALID_OPERATION, false);
1254 }
1255
1256 if (texture->isImmutable() && !isSubImage)
1257 {
1258 return gl::error(GL_INVALID_OPERATION, false);
1259 }
1260
1261 if (textureIsDepth)
1262 {
1263 return gl::error(GL_INVALID_OPERATION, false);
1264 }
1265
1266 if (textureCompressed)
1267 {
Geoff Lange4a492b2014-06-19 14:14:41 -04001268 GLint blockWidth = GetCompressedBlockWidth(textureInternalFormat);
1269 GLint blockHeight = GetCompressedBlockHeight(textureInternalFormat);
Jamie Madill560a8d82014-05-21 13:06:20 -04001270
1271 if (((width % blockWidth) != 0 && width != textureLevelWidth) ||
1272 ((height % blockHeight) != 0 && height != textureLevelHeight))
1273 {
1274 return gl::error(GL_INVALID_OPERATION, false);
1275 }
1276 }
1277
1278 if (isSubImage)
1279 {
1280 if (xoffset + width > textureLevelWidth ||
1281 yoffset + height > textureLevelHeight ||
1282 zoffset >= textureLevelDepth)
1283 {
1284 return gl::error(GL_INVALID_VALUE, false);
1285 }
1286 }
Jamie Madill6f38f822014-06-06 17:12:20 -04001287 else
1288 {
1289 if (IsCubemapTextureTarget(target) && width != height)
1290 {
1291 return gl::error(GL_INVALID_VALUE, false);
1292 }
1293
Geoff Langc0b9ef42014-07-02 10:02:37 -04001294 if (!IsValidInternalFormat(internalformat, context->getExtensions(), context->getClientVersion()))
Jamie Madill6f38f822014-06-06 17:12:20 -04001295 {
1296 return gl::error(GL_INVALID_ENUM, false);
1297 }
1298
1299 int maxLevelDimension = (maxDimension >> level);
1300 if (static_cast<int>(width) > maxLevelDimension || static_cast<int>(height) > maxLevelDimension)
1301 {
1302 return gl::error(GL_INVALID_VALUE, false);
1303 }
1304 }
Jamie Madill560a8d82014-05-21 13:06:20 -04001305
1306 *textureFormatOut = textureInternalFormat;
1307 return true;
1308}
1309
Jamie Madill1aeb1312014-06-20 13:21:25 -04001310static bool ValidateDrawBase(const gl::Context *context, GLenum mode, GLsizei count)
Jamie Madill250d33f2014-06-06 17:09:03 -04001311{
Jamie Madill1aeb1312014-06-20 13:21:25 -04001312 switch (mode)
1313 {
1314 case GL_POINTS:
1315 case GL_LINES:
1316 case GL_LINE_LOOP:
1317 case GL_LINE_STRIP:
1318 case GL_TRIANGLES:
1319 case GL_TRIANGLE_STRIP:
1320 case GL_TRIANGLE_FAN:
1321 break;
1322 default:
1323 return gl::error(GL_INVALID_ENUM, false);
1324 }
1325
Jamie Madill250d33f2014-06-06 17:09:03 -04001326 if (count < 0)
1327 {
1328 return gl::error(GL_INVALID_VALUE, false);
1329 }
1330
Jamie Madill250d33f2014-06-06 17:09:03 -04001331 // Check for mapped buffers
Jamie Madillfd716582014-06-06 17:09:04 -04001332 if (context->hasMappedBuffer(GL_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001333 {
1334 return gl::error(GL_INVALID_OPERATION, false);
1335 }
1336
Jamie Madillac528012014-06-20 13:21:23 -04001337 const gl::DepthStencilState &depthStencilState = context->getDepthStencilState();
1338 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
1339 context->getStencilRef() != context->getStencilBackRef() ||
1340 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
1341 {
1342 // Note: these separate values are not supported in WebGL, due to D3D's limitations.
1343 // See Section 6.10 of the WebGL 1.0 spec
1344 ERR("This ANGLE implementation does not support separate front/back stencil "
1345 "writemasks, reference values, or stencil mask values.");
1346 return gl::error(GL_INVALID_OPERATION, false);
1347 }
1348
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001349 const gl::Framebuffer *fbo = context->getDrawFramebuffer();
1350 if (!fbo || fbo->completeness() != GL_FRAMEBUFFER_COMPLETE)
1351 {
1352 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
1353 }
1354
Jamie Madilld4cfa572014-07-08 10:00:32 -04001355 if (!context->getCurrentProgram())
1356 {
1357 return gl::error(GL_INVALID_OPERATION, false);
1358 }
1359
1360 gl::ProgramBinary *programBinary = context->getCurrentProgramBinary();
1361 if (!programBinary->validateSamplers(NULL))
1362 {
1363 return gl::error(GL_INVALID_OPERATION, false);
1364 }
1365
Jamie Madill250d33f2014-06-06 17:09:03 -04001366 // No-op if zero count
1367 return (count > 0);
1368}
1369
Jamie Madillfd716582014-06-06 17:09:04 -04001370bool ValidateDrawArrays(const gl::Context *context, GLenum mode, GLint first, GLsizei count)
Jamie Madill250d33f2014-06-06 17:09:03 -04001371{
Jamie Madillfd716582014-06-06 17:09:04 -04001372 if (first < 0)
Jamie Madill250d33f2014-06-06 17:09:03 -04001373 {
1374 return gl::error(GL_INVALID_VALUE, false);
1375 }
1376
Jamie Madillfd716582014-06-06 17:09:04 -04001377 gl::TransformFeedback *curTransformFeedback = context->getCurrentTransformFeedback();
1378 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused() &&
1379 curTransformFeedback->getDrawMode() != mode)
1380 {
1381 // It is an invalid operation to call DrawArrays or DrawArraysInstanced with a draw mode
1382 // that does not match the current transform feedback object's draw mode (if transform feedback
1383 // is active), (3.0.2, section 2.14, pg 86)
1384 return gl::error(GL_INVALID_OPERATION, false);
1385 }
1386
Jamie Madill1aeb1312014-06-20 13:21:25 -04001387 if (!ValidateDrawBase(context, mode, count))
Jamie Madillfd716582014-06-06 17:09:04 -04001388 {
1389 return false;
1390 }
1391
1392 return true;
1393}
1394
1395bool ValidateDrawArraysInstanced(const gl::Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
1396{
1397 if (primcount < 0)
1398 {
1399 return gl::error(GL_INVALID_VALUE, false);
1400 }
1401
1402 if (!ValidateDrawArrays(context, mode, first, count))
1403 {
1404 return false;
1405 }
1406
1407 // No-op if zero primitive count
1408 return (primcount > 0);
1409}
1410
1411bool ValidateDrawElements(const gl::Context *context, GLenum mode, GLsizei count, GLenum type, const GLvoid* indices)
1412{
Jamie Madill250d33f2014-06-06 17:09:03 -04001413 switch (type)
1414 {
1415 case GL_UNSIGNED_BYTE:
1416 case GL_UNSIGNED_SHORT:
1417 break;
1418 case GL_UNSIGNED_INT:
Geoff Langc0b9ef42014-07-02 10:02:37 -04001419 if (!context->getExtensions().elementIndexUint)
Jamie Madill250d33f2014-06-06 17:09:03 -04001420 {
1421 return gl::error(GL_INVALID_ENUM, false);
1422 }
1423 break;
1424 default:
1425 return gl::error(GL_INVALID_ENUM, false);
1426 }
1427
1428 gl::TransformFeedback *curTransformFeedback = context->getCurrentTransformFeedback();
1429 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused())
1430 {
1431 // It is an invalid operation to call DrawElements, DrawRangeElements or DrawElementsInstanced
1432 // while transform feedback is active, (3.0.2, section 2.14, pg 86)
1433 return gl::error(GL_INVALID_OPERATION, false);
1434 }
1435
1436 // Check for mapped buffers
Jamie Madillfd716582014-06-06 17:09:04 -04001437 if (context->hasMappedBuffer(GL_ELEMENT_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001438 {
1439 return gl::error(GL_INVALID_OPERATION, false);
1440 }
1441
Jamie Madilld4cfa572014-07-08 10:00:32 -04001442 gl::VertexArray *vao = context->getCurrentVertexArray();
1443 if (!indices && !vao->getElementArrayBuffer())
1444 {
1445 return gl::error(GL_INVALID_OPERATION, false);
1446 }
1447
Jamie Madill1aeb1312014-06-20 13:21:25 -04001448 if (!ValidateDrawBase(context, mode, count))
Jamie Madillfd716582014-06-06 17:09:04 -04001449 {
1450 return false;
1451 }
1452
1453 return true;
1454}
1455
1456bool ValidateDrawElementsInstanced(const gl::Context *context, GLenum mode, GLsizei count, GLenum type,
1457 const GLvoid *indices, GLsizei primcount)
1458{
1459 if (primcount < 0)
1460 {
1461 return gl::error(GL_INVALID_VALUE, false);
1462 }
1463
1464 if (!ValidateDrawElements(context, mode, count, type, indices))
1465 {
1466 return false;
1467 }
1468
1469 // No-op zero primitive count
1470 return (primcount > 0);
Jamie Madill250d33f2014-06-06 17:09:03 -04001471}
1472
Jamie Madill55ec3b12014-07-03 10:38:57 -04001473bool ValidateFramebufferTextureBase(const gl::Context *context, GLenum target, GLenum attachment,
1474 GLuint texture, GLint level)
Jamie Madill570f7c82014-07-03 10:38:54 -04001475{
Jamie Madill55ec3b12014-07-03 10:38:57 -04001476 if (!ValidFramebufferTarget(target))
1477 {
1478 return gl::error(GL_INVALID_ENUM, false);
1479 }
1480
1481 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill570f7c82014-07-03 10:38:54 -04001482 {
1483 return false;
1484 }
1485
Jamie Madill55ec3b12014-07-03 10:38:57 -04001486 if (texture != 0)
1487 {
1488 gl::Texture *tex = context->getTexture(texture);
1489
1490 if (tex == NULL)
1491 {
1492 return gl::error(GL_INVALID_OPERATION, false);
1493 }
1494
1495 if (level < 0)
1496 {
1497 return gl::error(GL_INVALID_VALUE, false);
1498 }
1499 }
1500
1501 const gl::Framebuffer *framebuffer = context->getTargetFramebuffer(target);
1502 GLuint framebufferHandle = context->getTargetFramebufferHandle(target);
1503
1504 if (framebufferHandle == 0 || !framebuffer)
1505 {
1506 return gl::error(GL_INVALID_OPERATION, false);
1507 }
1508
1509 return true;
1510}
1511
1512bool ValidateFramebufferTexture2D(const gl::Context *context, GLenum target, GLenum attachment,
1513 GLenum textarget, GLuint texture, GLint level)
1514{
1515 // Attachments are required to be bound to level 0 in ES2
1516 if (context->getClientVersion() < 3 && level != 0)
1517 {
1518 return gl::error(GL_INVALID_VALUE, false);
1519 }
1520
1521 if (!ValidateFramebufferTextureBase(context, target, attachment, texture, level))
Jamie Madill570f7c82014-07-03 10:38:54 -04001522 {
1523 return false;
1524 }
1525
Jamie Madill55ec3b12014-07-03 10:38:57 -04001526 if (texture != 0)
1527 {
1528 gl::Texture *tex = context->getTexture(texture);
1529 ASSERT(tex);
1530
Jamie Madill2a6564e2014-07-11 09:53:19 -04001531 const gl::Caps &caps = context->getCaps();
1532
Jamie Madill55ec3b12014-07-03 10:38:57 -04001533 switch (textarget)
1534 {
1535 case GL_TEXTURE_2D:
1536 {
Jamie Madill2a6564e2014-07-11 09:53:19 -04001537 if (level > gl::log2(caps.max2DTextureSize))
Jamie Madill55ec3b12014-07-03 10:38:57 -04001538 {
1539 return gl::error(GL_INVALID_VALUE, false);
1540 }
1541 if (tex->getTarget() != GL_TEXTURE_2D)
1542 {
1543 return gl::error(GL_INVALID_OPERATION, false);
1544 }
1545 gl::Texture2D *tex2d = static_cast<gl::Texture2D *>(tex);
1546 if (tex2d->isCompressed(level))
1547 {
1548 return gl::error(GL_INVALID_OPERATION, false);
1549 }
1550 }
1551 break;
1552
1553 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1554 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1555 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1556 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1557 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1558 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1559 {
Jamie Madill2a6564e2014-07-11 09:53:19 -04001560 if (level > gl::log2(caps.maxCubeMapTextureSize))
Jamie Madill55ec3b12014-07-03 10:38:57 -04001561 {
1562 return gl::error(GL_INVALID_VALUE, false);
1563 }
1564 if (tex->getTarget() != GL_TEXTURE_CUBE_MAP)
1565 {
1566 return gl::error(GL_INVALID_OPERATION, false);
1567 }
1568 gl::TextureCubeMap *texcube = static_cast<gl::TextureCubeMap *>(tex);
1569 if (texcube->isCompressed(textarget, level))
1570 {
1571 return gl::error(GL_INVALID_OPERATION, false);
1572 }
1573 }
1574 break;
1575
1576 default:
1577 return gl::error(GL_INVALID_ENUM, false);
1578 }
1579 }
1580
Jamie Madill570f7c82014-07-03 10:38:54 -04001581 return true;
1582}
1583
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001584}