blob: 6ee4caef6529d9f75614bfef55db9b9eaa325e2e [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{
363 gl::Framebuffer *framebuffer = context->getTargetFramebuffer(target);
364 GLuint framebufferHandle = context->getTargetFramebufferHandle(target);
365
366 if (!framebuffer || (framebufferHandle == 0 && renderbuffer != 0))
367 {
368 return gl::error(GL_INVALID_OPERATION, false);
369 }
370
Jamie Madillb4472272014-07-03 10:38:55 -0400371 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500372 {
Jamie Madillb4472272014-07-03 10:38:55 -0400373 return false;
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500374 }
375
Jamie Madillab9d82c2014-01-21 16:38:14 -0500376 // [OpenGL ES 2.0.25] Section 4.4.3 page 112
377 // [OpenGL ES 3.0.2] Section 4.4.2 page 201
378 // 'renderbuffer' must be either zero or the name of an existing renderbuffer object of
379 // type 'renderbuffertarget', otherwise an INVALID_OPERATION error is generated.
380 if (renderbuffer != 0)
381 {
382 if (!context->getRenderbuffer(renderbuffer))
383 {
384 return gl::error(GL_INVALID_OPERATION, false);
385 }
386 }
387
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500388 return true;
389}
390
Jamie Madill3c7fa222014-06-05 13:08:51 -0400391static bool IsPartialBlit(gl::Context *context, gl::FramebufferAttachment *readBuffer, gl::FramebufferAttachment *writeBuffer,
Geoff Lang125deab2013-08-09 13:34:16 -0400392 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
393 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1)
394{
395 if (srcX0 != 0 || srcY0 != 0 || dstX0 != 0 || dstY0 != 0 ||
396 dstX1 != writeBuffer->getWidth() || dstY1 != writeBuffer->getHeight() ||
397 srcX1 != readBuffer->getWidth() || srcY1 != readBuffer->getHeight())
398 {
399 return true;
400 }
401 else if (context->isScissorTestEnabled())
402 {
403 int scissorX, scissorY, scissorWidth, scissorHeight;
404 context->getScissorParams(&scissorX, &scissorY, &scissorWidth, &scissorHeight);
405
406 return scissorX > 0 || scissorY > 0 ||
407 scissorWidth < writeBuffer->getWidth() ||
408 scissorHeight < writeBuffer->getHeight();
409 }
410 else
411 {
412 return false;
413 }
414}
415
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400416bool ValidateBlitFramebufferParameters(gl::Context *context, GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400417 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask,
418 GLenum filter, bool fromAngleExtension)
419{
420 switch (filter)
421 {
422 case GL_NEAREST:
423 break;
424 case GL_LINEAR:
425 if (fromAngleExtension)
426 {
427 return gl::error(GL_INVALID_ENUM, false);
428 }
429 break;
430 default:
431 return gl::error(GL_INVALID_ENUM, false);
432 }
433
434 if ((mask & ~(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)) != 0)
435 {
436 return gl::error(GL_INVALID_VALUE, false);
437 }
438
439 if (mask == 0)
440 {
441 // ES3.0 spec, section 4.3.2 specifies that a mask of zero is valid and no
442 // buffers are copied.
443 return false;
444 }
445
446 if (fromAngleExtension && (srcX1 - srcX0 != dstX1 - dstX0 || srcY1 - srcY0 != dstY1 - dstY0))
447 {
448 ERR("Scaling and flipping in BlitFramebufferANGLE not supported by this implementation.");
449 return gl::error(GL_INVALID_OPERATION, false);
450 }
451
452 // ES3.0 spec, section 4.3.2 states that linear filtering is only available for the
453 // color buffer, leaving only nearest being unfiltered from above
454 if ((mask & ~GL_COLOR_BUFFER_BIT) != 0 && filter != GL_NEAREST)
455 {
456 return gl::error(GL_INVALID_OPERATION, false);
457 }
458
459 if (context->getReadFramebufferHandle() == context->getDrawFramebufferHandle())
460 {
461 if (fromAngleExtension)
462 {
463 ERR("Blits with the same source and destination framebuffer are not supported by this "
464 "implementation.");
465 }
466 return gl::error(GL_INVALID_OPERATION, false);
467 }
468
469 gl::Framebuffer *readFramebuffer = context->getReadFramebuffer();
470 gl::Framebuffer *drawFramebuffer = context->getDrawFramebuffer();
471 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
472 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
473 {
474 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
475 }
476
477 if (drawFramebuffer->getSamples() != 0)
478 {
479 return gl::error(GL_INVALID_OPERATION, false);
480 }
481
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400482 bool sameBounds = srcX0 == dstX0 && srcY0 == dstY0 && srcX1 == dstX1 && srcY1 == dstY1;
483
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400484 if (mask & GL_COLOR_BUFFER_BIT)
485 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400486 gl::FramebufferAttachment *readColorBuffer = readFramebuffer->getReadColorbuffer();
487 gl::FramebufferAttachment *drawColorBuffer = drawFramebuffer->getFirstColorbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400488
489 if (readColorBuffer && drawColorBuffer)
490 {
Geoff Lang005df412013-10-16 14:12:50 -0400491 GLenum readInternalFormat = readColorBuffer->getActualFormat();
Geoff Lange4a492b2014-06-19 14:14:41 -0400492 GLenum readComponentType = gl::GetComponentType(readInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400493
494 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; i++)
495 {
496 if (drawFramebuffer->isEnabledColorAttachment(i))
497 {
Geoff Lang005df412013-10-16 14:12:50 -0400498 GLenum drawInternalFormat = drawFramebuffer->getColorbuffer(i)->getActualFormat();
Geoff Lange4a492b2014-06-19 14:14:41 -0400499 GLenum drawComponentType = gl::GetComponentType(drawInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400500
Geoff Langb2f3d052013-08-13 12:49:27 -0400501 // The GL ES 3.0.2 spec (pg 193) states that:
502 // 1) If the read buffer is fixed point format, the draw buffer must be as well
503 // 2) If the read buffer is an unsigned integer format, the draw buffer must be as well
504 // 3) If the read buffer is a signed integer format, the draw buffer must be as well
505 if ( (readComponentType == GL_UNSIGNED_NORMALIZED || readComponentType == GL_SIGNED_NORMALIZED) &&
506 !(drawComponentType == GL_UNSIGNED_NORMALIZED || drawComponentType == GL_SIGNED_NORMALIZED))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400507 {
508 return gl::error(GL_INVALID_OPERATION, false);
509 }
510
Geoff Langb2f3d052013-08-13 12:49:27 -0400511 if (readComponentType == GL_UNSIGNED_INT && drawComponentType != GL_UNSIGNED_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400512 {
513 return gl::error(GL_INVALID_OPERATION, false);
514 }
515
Geoff Langb2f3d052013-08-13 12:49:27 -0400516 if (readComponentType == GL_INT && drawComponentType != GL_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400517 {
518 return gl::error(GL_INVALID_OPERATION, false);
519 }
520
Geoff Langb2f3d052013-08-13 12:49:27 -0400521 if (readColorBuffer->getSamples() > 0 && (readInternalFormat != drawInternalFormat || !sameBounds))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400522 {
523 return gl::error(GL_INVALID_OPERATION, false);
524 }
525 }
526 }
527
Geoff Langb2f3d052013-08-13 12:49:27 -0400528 if ((readComponentType == GL_INT || readComponentType == GL_UNSIGNED_INT) && filter == GL_LINEAR)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400529 {
530 return gl::error(GL_INVALID_OPERATION, false);
531 }
532
533 if (fromAngleExtension)
534 {
535 const GLenum readColorbufferType = readFramebuffer->getReadColorbufferType();
536 if (readColorbufferType != GL_TEXTURE_2D && readColorbufferType != GL_RENDERBUFFER)
537 {
538 return gl::error(GL_INVALID_OPERATION, false);
539 }
540
541 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
542 {
543 if (drawFramebuffer->isEnabledColorAttachment(colorAttachment))
544 {
Jamie Madille92a3542014-07-03 10:38:58 -0400545 FramebufferAttachment *attachment = drawFramebuffer->getColorbuffer(colorAttachment);
546 ASSERT(attachment);
547
548 if (attachment->type() != GL_TEXTURE_2D && attachment->type() != GL_RENDERBUFFER)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400549 {
550 return gl::error(GL_INVALID_OPERATION, false);
551 }
552
Jamie Madille92a3542014-07-03 10:38:58 -0400553 if (attachment->getActualFormat() != readColorBuffer->getActualFormat())
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400554 {
555 return gl::error(GL_INVALID_OPERATION, false);
556 }
557 }
558 }
Geoff Lang125deab2013-08-09 13:34:16 -0400559 if (readFramebuffer->getSamples() != 0 && IsPartialBlit(context, readColorBuffer, drawColorBuffer,
560 srcX0, srcY0, srcX1, srcY1,
561 dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400562 {
563 return gl::error(GL_INVALID_OPERATION, false);
564 }
565 }
566 }
567 }
568
569 if (mask & GL_DEPTH_BUFFER_BIT)
570 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400571 gl::FramebufferAttachment *readDepthBuffer = readFramebuffer->getDepthbuffer();
572 gl::FramebufferAttachment *drawDepthBuffer = drawFramebuffer->getDepthbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400573
574 if (readDepthBuffer && drawDepthBuffer)
575 {
576 if (readDepthBuffer->getActualFormat() != drawDepthBuffer->getActualFormat())
577 {
578 return gl::error(GL_INVALID_OPERATION, false);
579 }
580
581 if (readDepthBuffer->getSamples() > 0 && !sameBounds)
582 {
583 return gl::error(GL_INVALID_OPERATION, false);
584 }
585
586 if (fromAngleExtension)
587 {
Geoff Lang125deab2013-08-09 13:34:16 -0400588 if (IsPartialBlit(context, readDepthBuffer, drawDepthBuffer,
589 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400590 {
591 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
592 return gl::error(GL_INVALID_OPERATION, false); // only whole-buffer copies are permitted
593 }
594
595 if (readDepthBuffer->getSamples() != 0 || drawDepthBuffer->getSamples() != 0)
596 {
597 return gl::error(GL_INVALID_OPERATION, false);
598 }
599 }
600 }
601 }
602
603 if (mask & GL_STENCIL_BUFFER_BIT)
604 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400605 gl::FramebufferAttachment *readStencilBuffer = readFramebuffer->getStencilbuffer();
606 gl::FramebufferAttachment *drawStencilBuffer = drawFramebuffer->getStencilbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400607
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400608 if (readStencilBuffer && drawStencilBuffer)
609 {
610 if (readStencilBuffer->getActualFormat() != drawStencilBuffer->getActualFormat())
611 {
612 return gl::error(GL_INVALID_OPERATION, false);
613 }
614
615 if (readStencilBuffer->getSamples() > 0 && !sameBounds)
616 {
617 return gl::error(GL_INVALID_OPERATION, false);
618 }
619
620 if (fromAngleExtension)
621 {
Geoff Lang125deab2013-08-09 13:34:16 -0400622 if (IsPartialBlit(context, readStencilBuffer, drawStencilBuffer,
623 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400624 {
625 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
626 return gl::error(GL_INVALID_OPERATION, false); // only whole-buffer copies are permitted
627 }
628
629 if (readStencilBuffer->getSamples() != 0 || drawStencilBuffer->getSamples() != 0)
630 {
631 return gl::error(GL_INVALID_OPERATION, false);
632 }
633 }
634 }
635 }
636
637 return true;
638}
639
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400640bool ValidateGetVertexAttribParameters(GLenum pname, int clientVersion)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400641{
642 switch (pname)
643 {
644 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
645 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
646 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
647 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
648 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
649 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
650 case GL_CURRENT_VERTEX_ATTRIB:
651 return true;
652
653 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR:
654 // Don't verify ES3 context because GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE uses
655 // the same constant.
656 META_ASSERT(GL_VERTEX_ATTRIB_ARRAY_DIVISOR == GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE);
657 return true;
658
659 case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
660 return ((clientVersion >= 3) ? true : gl::error(GL_INVALID_ENUM, false));
661
662 default:
663 return gl::error(GL_INVALID_ENUM, false);
664 }
665}
666
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400667bool ValidateTexParamParameters(gl::Context *context, GLenum pname, GLint param)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400668{
669 switch (pname)
670 {
671 case GL_TEXTURE_WRAP_R:
672 case GL_TEXTURE_SWIZZLE_R:
673 case GL_TEXTURE_SWIZZLE_G:
674 case GL_TEXTURE_SWIZZLE_B:
675 case GL_TEXTURE_SWIZZLE_A:
676 case GL_TEXTURE_BASE_LEVEL:
677 case GL_TEXTURE_MAX_LEVEL:
678 case GL_TEXTURE_COMPARE_MODE:
679 case GL_TEXTURE_COMPARE_FUNC:
680 case GL_TEXTURE_MIN_LOD:
681 case GL_TEXTURE_MAX_LOD:
682 if (context->getClientVersion() < 3)
683 {
684 return gl::error(GL_INVALID_ENUM, false);
685 }
686 break;
687
688 default: break;
689 }
690
691 switch (pname)
692 {
693 case GL_TEXTURE_WRAP_S:
694 case GL_TEXTURE_WRAP_T:
695 case GL_TEXTURE_WRAP_R:
696 switch (param)
697 {
698 case GL_REPEAT:
699 case GL_CLAMP_TO_EDGE:
700 case GL_MIRRORED_REPEAT:
701 return true;
702 default:
703 return gl::error(GL_INVALID_ENUM, false);
704 }
705
706 case GL_TEXTURE_MIN_FILTER:
707 switch (param)
708 {
709 case GL_NEAREST:
710 case GL_LINEAR:
711 case GL_NEAREST_MIPMAP_NEAREST:
712 case GL_LINEAR_MIPMAP_NEAREST:
713 case GL_NEAREST_MIPMAP_LINEAR:
714 case GL_LINEAR_MIPMAP_LINEAR:
715 return true;
716 default:
717 return gl::error(GL_INVALID_ENUM, false);
718 }
719 break;
720
721 case GL_TEXTURE_MAG_FILTER:
722 switch (param)
723 {
724 case GL_NEAREST:
725 case GL_LINEAR:
726 return true;
727 default:
728 return gl::error(GL_INVALID_ENUM, false);
729 }
730 break;
731
732 case GL_TEXTURE_USAGE_ANGLE:
733 switch (param)
734 {
735 case GL_NONE:
736 case GL_FRAMEBUFFER_ATTACHMENT_ANGLE:
737 return true;
738 default:
739 return gl::error(GL_INVALID_ENUM, false);
740 }
741 break;
742
743 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
Geoff Langc0b9ef42014-07-02 10:02:37 -0400744 if (!context->getExtensions().textureFilterAnisotropic)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400745 {
746 return gl::error(GL_INVALID_ENUM, false);
747 }
748
749 // we assume the parameter passed to this validation method is truncated, not rounded
750 if (param < 1)
751 {
752 return gl::error(GL_INVALID_VALUE, false);
753 }
754 return true;
755
756 case GL_TEXTURE_MIN_LOD:
757 case GL_TEXTURE_MAX_LOD:
758 // any value is permissible
759 return true;
760
761 case GL_TEXTURE_COMPARE_MODE:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400762 // Acceptable mode parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400763 switch (param)
764 {
765 case GL_NONE:
766 case GL_COMPARE_REF_TO_TEXTURE:
767 return true;
768 default:
769 return gl::error(GL_INVALID_ENUM, false);
770 }
771 break;
772
773 case GL_TEXTURE_COMPARE_FUNC:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400774 // Acceptable function parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400775 switch (param)
776 {
777 case GL_LEQUAL:
778 case GL_GEQUAL:
779 case GL_LESS:
780 case GL_GREATER:
781 case GL_EQUAL:
782 case GL_NOTEQUAL:
783 case GL_ALWAYS:
784 case GL_NEVER:
785 return true;
786 default:
787 return gl::error(GL_INVALID_ENUM, false);
788 }
789 break;
790
791 case GL_TEXTURE_SWIZZLE_R:
792 case GL_TEXTURE_SWIZZLE_G:
793 case GL_TEXTURE_SWIZZLE_B:
794 case GL_TEXTURE_SWIZZLE_A:
Geoff Langbc90a482013-09-17 16:51:27 -0400795 switch (param)
796 {
797 case GL_RED:
798 case GL_GREEN:
799 case GL_BLUE:
800 case GL_ALPHA:
801 case GL_ZERO:
802 case GL_ONE:
803 return true;
804 default:
805 return gl::error(GL_INVALID_ENUM, false);
806 }
807 break;
808
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400809 case GL_TEXTURE_BASE_LEVEL:
810 case GL_TEXTURE_MAX_LEVEL:
Nicolas Capens8de68282014-04-04 11:10:27 -0400811 if (param < 0)
812 {
813 return gl::error(GL_INVALID_VALUE, false);
814 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400815 return true;
816
817 default:
818 return gl::error(GL_INVALID_ENUM, false);
819 }
820}
821
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400822bool ValidateSamplerObjectParameter(GLenum pname)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400823{
824 switch (pname)
825 {
826 case GL_TEXTURE_MIN_FILTER:
827 case GL_TEXTURE_MAG_FILTER:
828 case GL_TEXTURE_WRAP_S:
829 case GL_TEXTURE_WRAP_T:
830 case GL_TEXTURE_WRAP_R:
831 case GL_TEXTURE_MIN_LOD:
832 case GL_TEXTURE_MAX_LOD:
833 case GL_TEXTURE_COMPARE_MODE:
834 case GL_TEXTURE_COMPARE_FUNC:
835 return true;
836
837 default:
838 return gl::error(GL_INVALID_ENUM, false);
839 }
840}
841
Jamie Madill26e91952014-03-05 15:01:27 -0500842bool ValidateReadPixelsParameters(gl::Context *context, GLint x, GLint y, GLsizei width, GLsizei height,
843 GLenum format, GLenum type, GLsizei *bufSize, GLvoid *pixels)
844{
845 gl::Framebuffer *framebuffer = context->getReadFramebuffer();
Jamie Madill893ab082014-05-16 16:56:10 -0400846 ASSERT(framebuffer);
Jamie Madill26e91952014-03-05 15:01:27 -0500847
848 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
849 {
850 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
851 }
852
853 if (context->getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
854 {
855 return gl::error(GL_INVALID_OPERATION, false);
856 }
857
Jamie Madill893ab082014-05-16 16:56:10 -0400858 if (!framebuffer->getReadColorbuffer())
859 {
860 return gl::error(GL_INVALID_OPERATION, false);
861 }
862
Jamie Madill26e91952014-03-05 15:01:27 -0500863 GLenum currentInternalFormat, currentFormat, currentType;
Geoff Lange4a492b2014-06-19 14:14:41 -0400864 GLuint clientVersion = context->getClientVersion();
Jamie Madill26e91952014-03-05 15:01:27 -0500865
Jamie Madill893ab082014-05-16 16:56:10 -0400866 context->getCurrentReadFormatType(&currentInternalFormat, &currentFormat, &currentType);
Jamie Madill26e91952014-03-05 15:01:27 -0500867
Geoff Langbdc9b2f2014-04-16 14:41:54 -0400868 bool validReadFormat = (clientVersion < 3) ? ValidES2ReadFormatType(context, format, type) :
869 ValidES3ReadFormatType(context, currentInternalFormat, format, type);
Jamie Madill26e91952014-03-05 15:01:27 -0500870
871 if (!(currentFormat == format && currentType == type) && !validReadFormat)
872 {
873 return gl::error(GL_INVALID_OPERATION, false);
874 }
875
Geoff Lange4a492b2014-06-19 14:14:41 -0400876 GLenum sizedInternalFormat = IsSizedInternalFormat(format) ? format
877 : GetSizedInternalFormat(format, type);
Jamie Madill26e91952014-03-05 15:01:27 -0500878
Geoff Lange4a492b2014-06-19 14:14:41 -0400879 GLsizei outputPitch = GetRowPitch(sizedInternalFormat, type, width, context->getPackAlignment());
Jamie Madill26e91952014-03-05 15:01:27 -0500880 // sized query sanity check
881 if (bufSize)
882 {
883 int requiredSize = outputPitch * height;
884 if (requiredSize > *bufSize)
885 {
886 return gl::error(GL_INVALID_OPERATION, false);
887 }
888 }
889
890 return true;
891}
892
Jamie Madilldb2f14c2014-05-13 13:56:30 -0400893bool ValidateBeginQuery(gl::Context *context, GLenum target, GLuint id)
894{
895 if (!ValidQueryType(context, target))
896 {
897 return gl::error(GL_INVALID_ENUM, false);
898 }
899
900 if (id == 0)
901 {
902 return gl::error(GL_INVALID_OPERATION, false);
903 }
904
905 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
906 // of zero, if the active query object name for <target> is non-zero (for the
907 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
908 // the active query for either target is non-zero), if <id> is the name of an
909 // existing query object whose type does not match <target>, or if <id> is the
910 // active query object name for any query type, the error INVALID_OPERATION is
911 // generated.
912
913 // Ensure no other queries are active
914 // NOTE: If other queries than occlusion are supported, we will need to check
915 // separately that:
916 // a) The query ID passed is not the current active query for any target/type
917 // b) There are no active queries for the requested target (and in the case
918 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
919 // no query may be active for either if glBeginQuery targets either.
920 if (context->isQueryActive())
921 {
922 return gl::error(GL_INVALID_OPERATION, false);
923 }
924
925 Query *queryObject = context->getQuery(id, true, target);
926
927 // check that name was obtained with glGenQueries
928 if (!queryObject)
929 {
930 return gl::error(GL_INVALID_OPERATION, false);
931 }
932
933 // check for type mismatch
934 if (queryObject->getType() != target)
935 {
936 return gl::error(GL_INVALID_OPERATION, false);
937 }
938
939 return true;
940}
941
Jamie Madill45c785d2014-05-13 14:09:34 -0400942bool ValidateEndQuery(gl::Context *context, GLenum target)
943{
944 if (!ValidQueryType(context, target))
945 {
946 return gl::error(GL_INVALID_ENUM, false);
947 }
948
949 const Query *queryObject = context->getActiveQuery(target);
950
951 if (queryObject == NULL)
952 {
953 return gl::error(GL_INVALID_OPERATION, false);
954 }
955
956 if (!queryObject->isStarted())
957 {
958 return gl::error(GL_INVALID_OPERATION, false);
959 }
960
961 return true;
962}
963
Jamie Madill36398922014-05-20 14:51:53 -0400964static bool ValidateUniformCommonBase(gl::Context *context, GLenum targetUniformType,
965 GLint location, GLsizei count, LinkedUniform **uniformOut)
Jamie Madilld7c7bb22014-05-20 10:55:54 -0400966{
967 if (count < 0)
968 {
969 return gl::error(GL_INVALID_VALUE, false);
970 }
971
Jamie Madilld7c7bb22014-05-20 10:55:54 -0400972 gl::ProgramBinary *programBinary = context->getCurrentProgramBinary();
973 if (!programBinary)
974 {
975 return gl::error(GL_INVALID_OPERATION, false);
976 }
977
978 if (location == -1)
979 {
980 // Silently ignore the uniform command
981 return false;
982 }
983
Jamie Madill36398922014-05-20 14:51:53 -0400984 if (!programBinary->isValidUniformLocation(location))
985 {
986 return gl::error(GL_INVALID_OPERATION, false);
987 }
988
989 LinkedUniform *uniform = programBinary->getUniformByLocation(location);
990
991 // attempting to write an array to a non-array uniform is an INVALID_OPERATION
992 if (uniform->elementCount() == 1 && count > 1)
993 {
994 return gl::error(GL_INVALID_OPERATION, false);
995 }
996
997 *uniformOut = uniform;
Jamie Madilld7c7bb22014-05-20 10:55:54 -0400998 return true;
999}
1000
Jamie Madillaa981bd2014-05-20 10:55:55 -04001001bool ValidateUniform(gl::Context *context, GLenum uniformType, GLint location, GLsizei count)
1002{
1003 // Check for ES3 uniform entry points
Jamie Madillf2575982014-06-25 16:04:54 -04001004 if (VariableComponentType(uniformType) == GL_UNSIGNED_INT && context->getClientVersion() < 3)
Jamie Madillaa981bd2014-05-20 10:55:55 -04001005 {
1006 return gl::error(GL_INVALID_OPERATION, false);
1007 }
1008
Jamie Madill36398922014-05-20 14:51:53 -04001009 LinkedUniform *uniform = NULL;
1010 if (!ValidateUniformCommonBase(context, uniformType, location, count, &uniform))
1011 {
1012 return false;
1013 }
1014
Jamie Madillf2575982014-06-25 16:04:54 -04001015 GLenum targetBoolType = VariableBoolVectorType(uniformType);
Jamie Madill36398922014-05-20 14:51:53 -04001016 bool samplerUniformCheck = (IsSampler(uniform->type) && uniformType == GL_INT);
1017 if (!samplerUniformCheck && uniformType != uniform->type && targetBoolType != uniform->type)
1018 {
1019 return gl::error(GL_INVALID_OPERATION, false);
1020 }
1021
1022 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001023}
1024
1025bool ValidateUniformMatrix(gl::Context *context, GLenum matrixType, GLint location, GLsizei count,
1026 GLboolean transpose)
1027{
1028 // Check for ES3 uniform entry points
1029 int rows = VariableRowCount(matrixType);
1030 int cols = VariableColumnCount(matrixType);
1031 if (rows != cols && context->getClientVersion() < 3)
1032 {
1033 return gl::error(GL_INVALID_OPERATION, false);
1034 }
1035
1036 if (transpose != GL_FALSE && context->getClientVersion() < 3)
1037 {
1038 return gl::error(GL_INVALID_VALUE, false);
1039 }
1040
Jamie Madill36398922014-05-20 14:51:53 -04001041 LinkedUniform *uniform = NULL;
1042 if (!ValidateUniformCommonBase(context, matrixType, location, count, &uniform))
1043 {
1044 return false;
1045 }
1046
1047 if (uniform->type != matrixType)
1048 {
1049 return gl::error(GL_INVALID_OPERATION, false);
1050 }
1051
1052 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001053}
1054
Jamie Madill893ab082014-05-16 16:56:10 -04001055bool ValidateStateQuery(gl::Context *context, GLenum pname, GLenum *nativeType, unsigned int *numParams)
1056{
1057 if (!context->getQueryParameterInfo(pname, nativeType, numParams))
1058 {
1059 return gl::error(GL_INVALID_ENUM, false);
1060 }
1061
1062 if (pname >= GL_DRAW_BUFFER0 && pname <= GL_DRAW_BUFFER15)
1063 {
1064 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0);
1065
Geoff Langaae65a42014-05-26 12:43:44 -04001066 if (colorAttachment >= context->getCaps().maxDrawBuffers)
Jamie Madill893ab082014-05-16 16:56:10 -04001067 {
1068 return gl::error(GL_INVALID_OPERATION, false);
1069 }
1070 }
1071
1072 switch (pname)
1073 {
1074 case GL_TEXTURE_BINDING_2D:
1075 case GL_TEXTURE_BINDING_CUBE_MAP:
1076 case GL_TEXTURE_BINDING_3D:
1077 case GL_TEXTURE_BINDING_2D_ARRAY:
1078 if (context->getActiveSampler() >= context->getMaximumCombinedTextureImageUnits())
1079 {
1080 return gl::error(GL_INVALID_OPERATION, false);
1081 }
1082 break;
1083
1084 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1085 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1086 {
1087 Framebuffer *framebuffer = context->getReadFramebuffer();
1088 ASSERT(framebuffer);
1089 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1090 {
1091 return gl::error(GL_INVALID_OPERATION, false);
1092 }
1093
Jamie Madill3c7fa222014-06-05 13:08:51 -04001094 FramebufferAttachment *attachment = framebuffer->getReadColorbuffer();
1095 if (!attachment)
Jamie Madill893ab082014-05-16 16:56:10 -04001096 {
1097 return gl::error(GL_INVALID_OPERATION, false);
1098 }
1099 }
1100 break;
1101
1102 default:
1103 break;
1104 }
1105
1106 // pname is valid, but there are no parameters to return
1107 if (numParams == 0)
1108 {
1109 return false;
1110 }
1111
1112 return true;
1113}
1114
Jamie Madill560a8d82014-05-21 13:06:20 -04001115bool ValidateCopyTexImageParametersBase(gl::Context* context, GLenum target, GLint level, GLenum internalformat, bool isSubImage,
1116 GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height,
1117 GLint border, GLenum *textureFormatOut)
1118{
1119
1120 if (!ValidTexture2DDestinationTarget(context, target))
1121 {
1122 return gl::error(GL_INVALID_ENUM, false);
1123 }
1124
1125 if (level < 0 || xoffset < 0 || yoffset < 0 || zoffset < 0 || width < 0 || height < 0)
1126 {
1127 return gl::error(GL_INVALID_VALUE, false);
1128 }
1129
1130 if (std::numeric_limits<GLsizei>::max() - xoffset < width || std::numeric_limits<GLsizei>::max() - yoffset < height)
1131 {
1132 return gl::error(GL_INVALID_VALUE, false);
1133 }
1134
1135 if (border != 0)
1136 {
1137 return gl::error(GL_INVALID_VALUE, false);
1138 }
1139
1140 if (!ValidMipLevel(context, target, level))
1141 {
1142 return gl::error(GL_INVALID_VALUE, false);
1143 }
1144
1145 gl::Framebuffer *framebuffer = context->getReadFramebuffer();
1146 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1147 {
1148 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
1149 }
1150
1151 if (context->getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
1152 {
1153 return gl::error(GL_INVALID_OPERATION, false);
1154 }
1155
Geoff Langaae65a42014-05-26 12:43:44 -04001156 const gl::Caps &caps = context->getCaps();
1157
Jamie Madill560a8d82014-05-21 13:06:20 -04001158 gl::Texture *texture = NULL;
1159 GLenum textureInternalFormat = GL_NONE;
1160 bool textureCompressed = false;
1161 bool textureIsDepth = false;
1162 GLint textureLevelWidth = 0;
1163 GLint textureLevelHeight = 0;
1164 GLint textureLevelDepth = 0;
Geoff Langaae65a42014-05-26 12:43:44 -04001165 GLuint maxDimension = 0;
Jamie Madill560a8d82014-05-21 13:06:20 -04001166
1167 switch (target)
1168 {
1169 case GL_TEXTURE_2D:
1170 {
1171 gl::Texture2D *texture2d = context->getTexture2D();
1172 if (texture2d)
1173 {
1174 textureInternalFormat = texture2d->getInternalFormat(level);
1175 textureCompressed = texture2d->isCompressed(level);
1176 textureIsDepth = texture2d->isDepth(level);
1177 textureLevelWidth = texture2d->getWidth(level);
1178 textureLevelHeight = texture2d->getHeight(level);
1179 textureLevelDepth = 1;
1180 texture = texture2d;
Geoff Langaae65a42014-05-26 12:43:44 -04001181 maxDimension = caps.max2DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001182 }
1183 }
1184 break;
1185
1186 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1187 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1188 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1189 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1190 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1191 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1192 {
1193 gl::TextureCubeMap *textureCube = context->getTextureCubeMap();
1194 if (textureCube)
1195 {
1196 textureInternalFormat = textureCube->getInternalFormat(target, level);
1197 textureCompressed = textureCube->isCompressed(target, level);
1198 textureIsDepth = false;
1199 textureLevelWidth = textureCube->getWidth(target, level);
1200 textureLevelHeight = textureCube->getHeight(target, level);
1201 textureLevelDepth = 1;
1202 texture = textureCube;
Geoff Langaae65a42014-05-26 12:43:44 -04001203 maxDimension = caps.maxCubeMapTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001204 }
1205 }
1206 break;
1207
1208 case GL_TEXTURE_2D_ARRAY:
1209 {
1210 gl::Texture2DArray *texture2dArray = context->getTexture2DArray();
1211 if (texture2dArray)
1212 {
1213 textureInternalFormat = texture2dArray->getInternalFormat(level);
1214 textureCompressed = texture2dArray->isCompressed(level);
1215 textureIsDepth = texture2dArray->isDepth(level);
1216 textureLevelWidth = texture2dArray->getWidth(level);
1217 textureLevelHeight = texture2dArray->getHeight(level);
1218 textureLevelDepth = texture2dArray->getLayers(level);
1219 texture = texture2dArray;
Geoff Langaae65a42014-05-26 12:43:44 -04001220 maxDimension = caps.max2DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001221 }
1222 }
1223 break;
1224
1225 case GL_TEXTURE_3D:
1226 {
1227 gl::Texture3D *texture3d = context->getTexture3D();
1228 if (texture3d)
1229 {
1230 textureInternalFormat = texture3d->getInternalFormat(level);
1231 textureCompressed = texture3d->isCompressed(level);
1232 textureIsDepth = texture3d->isDepth(level);
1233 textureLevelWidth = texture3d->getWidth(level);
1234 textureLevelHeight = texture3d->getHeight(level);
1235 textureLevelDepth = texture3d->getDepth(level);
1236 texture = texture3d;
Geoff Langaae65a42014-05-26 12:43:44 -04001237 maxDimension = caps.max3DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001238 }
1239 }
1240 break;
1241
1242 default:
1243 return gl::error(GL_INVALID_ENUM, false);
1244 }
1245
1246 if (!texture)
1247 {
1248 return gl::error(GL_INVALID_OPERATION, false);
1249 }
1250
1251 if (texture->isImmutable() && !isSubImage)
1252 {
1253 return gl::error(GL_INVALID_OPERATION, false);
1254 }
1255
1256 if (textureIsDepth)
1257 {
1258 return gl::error(GL_INVALID_OPERATION, false);
1259 }
1260
1261 if (textureCompressed)
1262 {
Geoff Lange4a492b2014-06-19 14:14:41 -04001263 GLint blockWidth = GetCompressedBlockWidth(textureInternalFormat);
1264 GLint blockHeight = GetCompressedBlockHeight(textureInternalFormat);
Jamie Madill560a8d82014-05-21 13:06:20 -04001265
1266 if (((width % blockWidth) != 0 && width != textureLevelWidth) ||
1267 ((height % blockHeight) != 0 && height != textureLevelHeight))
1268 {
1269 return gl::error(GL_INVALID_OPERATION, false);
1270 }
1271 }
1272
1273 if (isSubImage)
1274 {
1275 if (xoffset + width > textureLevelWidth ||
1276 yoffset + height > textureLevelHeight ||
1277 zoffset >= textureLevelDepth)
1278 {
1279 return gl::error(GL_INVALID_VALUE, false);
1280 }
1281 }
Jamie Madill6f38f822014-06-06 17:12:20 -04001282 else
1283 {
1284 if (IsCubemapTextureTarget(target) && width != height)
1285 {
1286 return gl::error(GL_INVALID_VALUE, false);
1287 }
1288
Geoff Langc0b9ef42014-07-02 10:02:37 -04001289 if (!IsValidInternalFormat(internalformat, context->getExtensions(), context->getClientVersion()))
Jamie Madill6f38f822014-06-06 17:12:20 -04001290 {
1291 return gl::error(GL_INVALID_ENUM, false);
1292 }
1293
1294 int maxLevelDimension = (maxDimension >> level);
1295 if (static_cast<int>(width) > maxLevelDimension || static_cast<int>(height) > maxLevelDimension)
1296 {
1297 return gl::error(GL_INVALID_VALUE, false);
1298 }
1299 }
Jamie Madill560a8d82014-05-21 13:06:20 -04001300
1301 *textureFormatOut = textureInternalFormat;
1302 return true;
1303}
1304
Jamie Madill1aeb1312014-06-20 13:21:25 -04001305static bool ValidateDrawBase(const gl::Context *context, GLenum mode, GLsizei count)
Jamie Madill250d33f2014-06-06 17:09:03 -04001306{
Jamie Madill1aeb1312014-06-20 13:21:25 -04001307 switch (mode)
1308 {
1309 case GL_POINTS:
1310 case GL_LINES:
1311 case GL_LINE_LOOP:
1312 case GL_LINE_STRIP:
1313 case GL_TRIANGLES:
1314 case GL_TRIANGLE_STRIP:
1315 case GL_TRIANGLE_FAN:
1316 break;
1317 default:
1318 return gl::error(GL_INVALID_ENUM, false);
1319 }
1320
Jamie Madill250d33f2014-06-06 17:09:03 -04001321 if (count < 0)
1322 {
1323 return gl::error(GL_INVALID_VALUE, false);
1324 }
1325
Jamie Madill250d33f2014-06-06 17:09:03 -04001326 // Check for mapped buffers
Jamie Madillfd716582014-06-06 17:09:04 -04001327 if (context->hasMappedBuffer(GL_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001328 {
1329 return gl::error(GL_INVALID_OPERATION, false);
1330 }
1331
Jamie Madillac528012014-06-20 13:21:23 -04001332 const gl::DepthStencilState &depthStencilState = context->getDepthStencilState();
1333 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
1334 context->getStencilRef() != context->getStencilBackRef() ||
1335 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
1336 {
1337 // Note: these separate values are not supported in WebGL, due to D3D's limitations.
1338 // See Section 6.10 of the WebGL 1.0 spec
1339 ERR("This ANGLE implementation does not support separate front/back stencil "
1340 "writemasks, reference values, or stencil mask values.");
1341 return gl::error(GL_INVALID_OPERATION, false);
1342 }
1343
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001344 const gl::Framebuffer *fbo = context->getDrawFramebuffer();
1345 if (!fbo || fbo->completeness() != GL_FRAMEBUFFER_COMPLETE)
1346 {
1347 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
1348 }
1349
Jamie Madilld4cfa572014-07-08 10:00:32 -04001350 if (!context->getCurrentProgram())
1351 {
1352 return gl::error(GL_INVALID_OPERATION, false);
1353 }
1354
1355 gl::ProgramBinary *programBinary = context->getCurrentProgramBinary();
1356 if (!programBinary->validateSamplers(NULL))
1357 {
1358 return gl::error(GL_INVALID_OPERATION, false);
1359 }
1360
Jamie Madill250d33f2014-06-06 17:09:03 -04001361 // No-op if zero count
1362 return (count > 0);
1363}
1364
Jamie Madillfd716582014-06-06 17:09:04 -04001365bool ValidateDrawArrays(const gl::Context *context, GLenum mode, GLint first, GLsizei count)
Jamie Madill250d33f2014-06-06 17:09:03 -04001366{
Jamie Madillfd716582014-06-06 17:09:04 -04001367 if (first < 0)
Jamie Madill250d33f2014-06-06 17:09:03 -04001368 {
1369 return gl::error(GL_INVALID_VALUE, false);
1370 }
1371
Jamie Madillfd716582014-06-06 17:09:04 -04001372 gl::TransformFeedback *curTransformFeedback = context->getCurrentTransformFeedback();
1373 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused() &&
1374 curTransformFeedback->getDrawMode() != mode)
1375 {
1376 // It is an invalid operation to call DrawArrays or DrawArraysInstanced with a draw mode
1377 // that does not match the current transform feedback object's draw mode (if transform feedback
1378 // is active), (3.0.2, section 2.14, pg 86)
1379 return gl::error(GL_INVALID_OPERATION, false);
1380 }
1381
Jamie Madill1aeb1312014-06-20 13:21:25 -04001382 if (!ValidateDrawBase(context, mode, count))
Jamie Madillfd716582014-06-06 17:09:04 -04001383 {
1384 return false;
1385 }
1386
1387 return true;
1388}
1389
1390bool ValidateDrawArraysInstanced(const gl::Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
1391{
1392 if (primcount < 0)
1393 {
1394 return gl::error(GL_INVALID_VALUE, false);
1395 }
1396
1397 if (!ValidateDrawArrays(context, mode, first, count))
1398 {
1399 return false;
1400 }
1401
1402 // No-op if zero primitive count
1403 return (primcount > 0);
1404}
1405
1406bool ValidateDrawElements(const gl::Context *context, GLenum mode, GLsizei count, GLenum type, const GLvoid* indices)
1407{
Jamie Madill250d33f2014-06-06 17:09:03 -04001408 switch (type)
1409 {
1410 case GL_UNSIGNED_BYTE:
1411 case GL_UNSIGNED_SHORT:
1412 break;
1413 case GL_UNSIGNED_INT:
Geoff Langc0b9ef42014-07-02 10:02:37 -04001414 if (!context->getExtensions().elementIndexUint)
Jamie Madill250d33f2014-06-06 17:09:03 -04001415 {
1416 return gl::error(GL_INVALID_ENUM, false);
1417 }
1418 break;
1419 default:
1420 return gl::error(GL_INVALID_ENUM, false);
1421 }
1422
1423 gl::TransformFeedback *curTransformFeedback = context->getCurrentTransformFeedback();
1424 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused())
1425 {
1426 // It is an invalid operation to call DrawElements, DrawRangeElements or DrawElementsInstanced
1427 // while transform feedback is active, (3.0.2, section 2.14, pg 86)
1428 return gl::error(GL_INVALID_OPERATION, false);
1429 }
1430
1431 // Check for mapped buffers
Jamie Madillfd716582014-06-06 17:09:04 -04001432 if (context->hasMappedBuffer(GL_ELEMENT_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001433 {
1434 return gl::error(GL_INVALID_OPERATION, false);
1435 }
1436
Jamie Madilld4cfa572014-07-08 10:00:32 -04001437 gl::VertexArray *vao = context->getCurrentVertexArray();
1438 if (!indices && !vao->getElementArrayBuffer())
1439 {
1440 return gl::error(GL_INVALID_OPERATION, false);
1441 }
1442
Jamie Madill1aeb1312014-06-20 13:21:25 -04001443 if (!ValidateDrawBase(context, mode, count))
Jamie Madillfd716582014-06-06 17:09:04 -04001444 {
1445 return false;
1446 }
1447
1448 return true;
1449}
1450
1451bool ValidateDrawElementsInstanced(const gl::Context *context, GLenum mode, GLsizei count, GLenum type,
1452 const GLvoid *indices, GLsizei primcount)
1453{
1454 if (primcount < 0)
1455 {
1456 return gl::error(GL_INVALID_VALUE, false);
1457 }
1458
1459 if (!ValidateDrawElements(context, mode, count, type, indices))
1460 {
1461 return false;
1462 }
1463
1464 // No-op zero primitive count
1465 return (primcount > 0);
Jamie Madill250d33f2014-06-06 17:09:03 -04001466}
1467
Jamie Madill55ec3b12014-07-03 10:38:57 -04001468bool ValidateFramebufferTextureBase(const gl::Context *context, GLenum target, GLenum attachment,
1469 GLuint texture, GLint level)
Jamie Madill570f7c82014-07-03 10:38:54 -04001470{
Jamie Madill55ec3b12014-07-03 10:38:57 -04001471 if (!ValidFramebufferTarget(target))
1472 {
1473 return gl::error(GL_INVALID_ENUM, false);
1474 }
1475
1476 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill570f7c82014-07-03 10:38:54 -04001477 {
1478 return false;
1479 }
1480
Jamie Madill55ec3b12014-07-03 10:38:57 -04001481 if (texture != 0)
1482 {
1483 gl::Texture *tex = context->getTexture(texture);
1484
1485 if (tex == NULL)
1486 {
1487 return gl::error(GL_INVALID_OPERATION, false);
1488 }
1489
1490 if (level < 0)
1491 {
1492 return gl::error(GL_INVALID_VALUE, false);
1493 }
1494 }
1495
1496 const gl::Framebuffer *framebuffer = context->getTargetFramebuffer(target);
1497 GLuint framebufferHandle = context->getTargetFramebufferHandle(target);
1498
1499 if (framebufferHandle == 0 || !framebuffer)
1500 {
1501 return gl::error(GL_INVALID_OPERATION, false);
1502 }
1503
1504 return true;
1505}
1506
1507bool ValidateFramebufferTexture2D(const gl::Context *context, GLenum target, GLenum attachment,
1508 GLenum textarget, GLuint texture, GLint level)
1509{
1510 // Attachments are required to be bound to level 0 in ES2
1511 if (context->getClientVersion() < 3 && level != 0)
1512 {
1513 return gl::error(GL_INVALID_VALUE, false);
1514 }
1515
1516 if (!ValidateFramebufferTextureBase(context, target, attachment, texture, level))
Jamie Madill570f7c82014-07-03 10:38:54 -04001517 {
1518 return false;
1519 }
1520
Jamie Madill55ec3b12014-07-03 10:38:57 -04001521 if (texture != 0)
1522 {
1523 gl::Texture *tex = context->getTexture(texture);
1524 ASSERT(tex);
1525
Jamie Madill2a6564e2014-07-11 09:53:19 -04001526 const gl::Caps &caps = context->getCaps();
1527
Jamie Madill55ec3b12014-07-03 10:38:57 -04001528 switch (textarget)
1529 {
1530 case GL_TEXTURE_2D:
1531 {
Jamie Madill2a6564e2014-07-11 09:53:19 -04001532 if (level > gl::log2(caps.max2DTextureSize))
Jamie Madill55ec3b12014-07-03 10:38:57 -04001533 {
1534 return gl::error(GL_INVALID_VALUE, false);
1535 }
1536 if (tex->getTarget() != GL_TEXTURE_2D)
1537 {
1538 return gl::error(GL_INVALID_OPERATION, false);
1539 }
1540 gl::Texture2D *tex2d = static_cast<gl::Texture2D *>(tex);
1541 if (tex2d->isCompressed(level))
1542 {
1543 return gl::error(GL_INVALID_OPERATION, false);
1544 }
1545 }
1546 break;
1547
1548 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1549 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1550 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1551 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1552 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1553 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1554 {
Jamie Madill2a6564e2014-07-11 09:53:19 -04001555 if (level > gl::log2(caps.maxCubeMapTextureSize))
Jamie Madill55ec3b12014-07-03 10:38:57 -04001556 {
1557 return gl::error(GL_INVALID_VALUE, false);
1558 }
1559 if (tex->getTarget() != GL_TEXTURE_CUBE_MAP)
1560 {
1561 return gl::error(GL_INVALID_OPERATION, false);
1562 }
1563 gl::TextureCubeMap *texcube = static_cast<gl::TextureCubeMap *>(tex);
1564 if (texcube->isCompressed(textarget, level))
1565 {
1566 return gl::error(GL_INVALID_OPERATION, false);
1567 }
1568 }
1569 break;
1570
1571 default:
1572 return gl::error(GL_INVALID_ENUM, false);
1573 }
1574 }
1575
Jamie Madill570f7c82014-07-03 10:38:54 -04001576 return true;
1577}
1578
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001579}