blob: 8225df9ecefab2a34ccfaae105373c57b2ee46e4 [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"
Jamie Madill2b976812014-08-25 15:47:49 -040023#include "libGLESv2/renderer/BufferImpl.h"
Geoff Lange8ebe7f2013-08-05 15:03:13 -040024
25#include "common/mathutil.h"
26#include "common/utilities.h"
27
28namespace gl
29{
30
Geoff Lang0550d032014-01-30 11:29:07 -050031bool ValidCap(const Context *context, GLenum cap)
32{
33 switch (cap)
34 {
35 case GL_CULL_FACE:
36 case GL_POLYGON_OFFSET_FILL:
37 case GL_SAMPLE_ALPHA_TO_COVERAGE:
38 case GL_SAMPLE_COVERAGE:
39 case GL_SCISSOR_TEST:
40 case GL_STENCIL_TEST:
41 case GL_DEPTH_TEST:
42 case GL_BLEND:
43 case GL_DITHER:
44 return true;
45 case GL_PRIMITIVE_RESTART_FIXED_INDEX:
46 case GL_RASTERIZER_DISCARD:
47 return (context->getClientVersion() >= 3);
48 default:
49 return false;
50 }
51}
52
Jamie Madill1fc7e2c2014-01-21 16:47:10 -050053bool ValidTextureTarget(const Context *context, GLenum target)
Jamie Madill35d15012013-10-07 10:46:37 -040054{
Jamie Madilld7460c72014-01-21 16:38:14 -050055 switch (target)
Jamie Madill35d15012013-10-07 10:46:37 -040056 {
Jamie Madilld7460c72014-01-21 16:38:14 -050057 case GL_TEXTURE_2D:
58 case GL_TEXTURE_CUBE_MAP:
59 return true;
Jamie Madill35d15012013-10-07 10:46:37 -040060
Jamie Madilld7460c72014-01-21 16:38:14 -050061 case GL_TEXTURE_3D:
62 case GL_TEXTURE_2D_ARRAY:
63 return (context->getClientVersion() >= 3);
64
65 default:
66 return false;
67 }
Jamie Madill35d15012013-10-07 10:46:37 -040068}
69
Shannon Woods4dfed832014-03-17 20:03:39 -040070// This function differs from ValidTextureTarget in that the target must be
71// usable as the destination of a 2D operation-- so a cube face is valid, but
72// GL_TEXTURE_CUBE_MAP is not.
Jamie Madill560a8d82014-05-21 13:06:20 -040073// Note: duplicate of IsInternalTextureTarget
Shannon Woods4dfed832014-03-17 20:03:39 -040074bool ValidTexture2DDestinationTarget(const Context *context, GLenum target)
75{
76 switch (target)
77 {
78 case GL_TEXTURE_2D:
79 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
80 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
81 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
82 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
83 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
84 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
85 return true;
86 case GL_TEXTURE_2D_ARRAY:
87 case GL_TEXTURE_3D:
88 return (context->getClientVersion() >= 3);
89 default:
90 return false;
91 }
92}
93
Jamie Madill1fc7e2c2014-01-21 16:47:10 -050094bool ValidFramebufferTarget(GLenum target)
95{
96 META_ASSERT(GL_DRAW_FRAMEBUFFER_ANGLE == GL_DRAW_FRAMEBUFFER && GL_READ_FRAMEBUFFER_ANGLE == GL_READ_FRAMEBUFFER);
97
98 switch (target)
99 {
100 case GL_FRAMEBUFFER: return true;
101 case GL_READ_FRAMEBUFFER: return true;
102 case GL_DRAW_FRAMEBUFFER: return true;
103 default: return false;
104 }
105}
106
Jamie Madill8c96d582014-03-05 15:01:23 -0500107bool ValidBufferTarget(const Context *context, GLenum target)
108{
109 switch (target)
110 {
111 case GL_ARRAY_BUFFER:
112 case GL_ELEMENT_ARRAY_BUFFER:
113 return true;
114
Jamie Madill8c96d582014-03-05 15:01:23 -0500115 case GL_PIXEL_PACK_BUFFER:
116 case GL_PIXEL_UNPACK_BUFFER:
Geoff Langc0b9ef42014-07-02 10:02:37 -0400117 return context->getExtensions().pixelBufferObject;
Shannon Woods158c4382014-05-06 13:00:07 -0400118
Shannon Woodsb3801742014-03-27 14:59:19 -0400119 case GL_COPY_READ_BUFFER:
120 case GL_COPY_WRITE_BUFFER:
Jamie Madill8c96d582014-03-05 15:01:23 -0500121 case GL_TRANSFORM_FEEDBACK_BUFFER:
122 case GL_UNIFORM_BUFFER:
123 return (context->getClientVersion() >= 3);
124
125 default:
126 return false;
127 }
128}
129
Jamie Madill70656a62014-03-05 15:01:26 -0500130bool ValidBufferParameter(const Context *context, GLenum pname)
131{
132 switch (pname)
133 {
134 case GL_BUFFER_USAGE:
135 case GL_BUFFER_SIZE:
136 return true;
137
138 // GL_BUFFER_MAP_POINTER is a special case, and may only be
139 // queried with GetBufferPointerv
140 case GL_BUFFER_ACCESS_FLAGS:
141 case GL_BUFFER_MAPPED:
142 case GL_BUFFER_MAP_OFFSET:
143 case GL_BUFFER_MAP_LENGTH:
144 return (context->getClientVersion() >= 3);
145
146 default:
147 return false;
148 }
149}
150
Jamie Madill8c96d582014-03-05 15:01:23 -0500151bool ValidMipLevel(const Context *context, GLenum target, GLint level)
Geoff Langce635692013-09-24 13:56:32 -0400152{
Geoff Langaae65a42014-05-26 12:43:44 -0400153 size_t maxDimension = 0;
Geoff Langce635692013-09-24 13:56:32 -0400154 switch (target)
155 {
Geoff Langaae65a42014-05-26 12:43:44 -0400156 case GL_TEXTURE_2D: maxDimension = context->getCaps().max2DTextureSize; break;
Geoff Langce635692013-09-24 13:56:32 -0400157 case GL_TEXTURE_CUBE_MAP:
158 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
159 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
160 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
161 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
162 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
Geoff Langaae65a42014-05-26 12:43:44 -0400163 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: maxDimension = context->getCaps().maxCubeMapTextureSize; break;
164 case GL_TEXTURE_3D: maxDimension = context->getCaps().max3DTextureSize; break;
165 case GL_TEXTURE_2D_ARRAY: maxDimension = context->getCaps().max2DTextureSize; break;
Geoff Langce635692013-09-24 13:56:32 -0400166 default: UNREACHABLE();
167 }
168
Geoff Langaae65a42014-05-26 12:43:44 -0400169 return level <= gl::log2(maxDimension);
Geoff Langce635692013-09-24 13:56:32 -0400170}
171
Jamie Madill4fd75c12014-06-23 10:53:54 -0400172bool ValidImageSize(const gl::Context *context, GLenum target, GLint level,
173 GLsizei width, GLsizei height, GLsizei depth)
Geoff Langce635692013-09-24 13:56:32 -0400174{
175 if (level < 0 || width < 0 || height < 0 || depth < 0)
176 {
177 return false;
178 }
179
Geoff Langc0b9ef42014-07-02 10:02:37 -0400180 if (!context->getExtensions().textureNPOT &&
Jamie Madill4fd75c12014-06-23 10:53:54 -0400181 (level != 0 && (!gl::isPow2(width) || !gl::isPow2(height) || !gl::isPow2(depth))))
Geoff Langce635692013-09-24 13:56:32 -0400182 {
183 return false;
184 }
185
186 if (!ValidMipLevel(context, target, level))
187 {
188 return false;
189 }
190
191 return true;
192}
193
Geoff Lang005df412013-10-16 14:12:50 -0400194bool ValidCompressedImageSize(const gl::Context *context, GLenum internalFormat, GLsizei width, GLsizei height)
Geoff Langd4f180b2013-09-24 13:57:44 -0400195{
Geoff Lang5d601382014-07-22 15:14:06 -0400196 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalFormat);
197 if (!formatInfo.compressed)
Geoff Langd4f180b2013-09-24 13:57:44 -0400198 {
199 return false;
200 }
201
Geoff Lang5d601382014-07-22 15:14:06 -0400202 if (width < 0 || (static_cast<GLuint>(width) > formatInfo.compressedBlockWidth && width % formatInfo.compressedBlockWidth != 0) ||
203 height < 0 || (static_cast<GLuint>(height) > formatInfo.compressedBlockHeight && height % formatInfo.compressedBlockHeight != 0))
Geoff Langd4f180b2013-09-24 13:57:44 -0400204 {
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 Lang5d601382014-07-22 15:14:06 -0400301 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalformat);
302 if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions()))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400303 {
304 return gl::error(GL_INVALID_ENUM, false);
305 }
306
307 // ANGLE_framebuffer_multisample does not explicitly state that the internal format must be
308 // sized but it does state that the format must be in the ES2.0 spec table 4.5 which contains
309 // only sized internal formats. The ES3 spec (section 4.4.2) does, however, state that the
310 // internal format must be sized and not an integer format if samples is greater than zero.
Geoff Lang5d601382014-07-22 15:14:06 -0400311 if (formatInfo.pixelBytes == 0)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400312 {
313 return gl::error(GL_INVALID_ENUM, false);
314 }
315
Geoff Lang5d601382014-07-22 15:14:06 -0400316 if ((formatInfo.componentType == GL_UNSIGNED_INT || formatInfo.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 Lang6cf8e1b2014-07-03 13:03:57 -0400322 if (!formatCaps.renderable)
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 {
Geoff Lang5f4c4632014-07-03 13:46:52 -0400338 ASSERT(context->getExtensions().framebufferMultisample);
339 if (static_cast<GLuint>(samples) > context->getExtensions().maxSamples)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400340 {
341 return gl::error(GL_INVALID_VALUE, false);
342 }
Geoff Lang5f4c4632014-07-03 13:46:52 -0400343
344 // Check if this specific format supports enough samples
345 if (static_cast<GLuint>(samples) > formatCaps.getMaxSamples())
346 {
347 return gl::error(GL_OUT_OF_MEMORY, false);
348 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400349 }
350 else
351 {
Geoff Lang5f4c4632014-07-03 13:46:52 -0400352 if (static_cast<GLuint>(samples) > formatCaps.getMaxSamples())
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400353 {
354 return gl::error(GL_INVALID_VALUE, false);
355 }
356 }
357
Shannon Woods53a94a82014-06-24 15:20:36 -0400358 GLuint handle = context->getState().getRenderbufferId();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400359 if (handle == 0)
360 {
361 return gl::error(GL_INVALID_OPERATION, false);
362 }
363
364 return true;
365}
366
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500367bool ValidateFramebufferRenderbufferParameters(gl::Context *context, GLenum target, GLenum attachment,
368 GLenum renderbuffertarget, GLuint renderbuffer)
369{
Shannon Woods1da3cf62014-06-27 15:32:23 -0400370 if (!ValidFramebufferTarget(target))
371 {
372 return gl::error(GL_INVALID_ENUM, false);
373 }
374
Shannon Woods53a94a82014-06-24 15:20:36 -0400375 gl::Framebuffer *framebuffer = context->getState().getTargetFramebuffer(target);
376 GLuint framebufferHandle = context->getState().getTargetFramebuffer(target)->id();
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500377
378 if (!framebuffer || (framebufferHandle == 0 && renderbuffer != 0))
379 {
380 return gl::error(GL_INVALID_OPERATION, false);
381 }
382
Jamie Madillb4472272014-07-03 10:38:55 -0400383 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500384 {
Jamie Madillb4472272014-07-03 10:38:55 -0400385 return false;
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500386 }
387
Jamie Madillab9d82c2014-01-21 16:38:14 -0500388 // [OpenGL ES 2.0.25] Section 4.4.3 page 112
389 // [OpenGL ES 3.0.2] Section 4.4.2 page 201
390 // 'renderbuffer' must be either zero or the name of an existing renderbuffer object of
391 // type 'renderbuffertarget', otherwise an INVALID_OPERATION error is generated.
392 if (renderbuffer != 0)
393 {
394 if (!context->getRenderbuffer(renderbuffer))
395 {
396 return gl::error(GL_INVALID_OPERATION, false);
397 }
398 }
399
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500400 return true;
401}
402
Jamie Madill3c7fa222014-06-05 13:08:51 -0400403static bool IsPartialBlit(gl::Context *context, gl::FramebufferAttachment *readBuffer, gl::FramebufferAttachment *writeBuffer,
Geoff Lang125deab2013-08-09 13:34:16 -0400404 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
405 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1)
406{
407 if (srcX0 != 0 || srcY0 != 0 || dstX0 != 0 || dstY0 != 0 ||
408 dstX1 != writeBuffer->getWidth() || dstY1 != writeBuffer->getHeight() ||
409 srcX1 != readBuffer->getWidth() || srcY1 != readBuffer->getHeight())
410 {
411 return true;
412 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400413 else if (context->getState().isScissorTestEnabled())
Geoff Lang125deab2013-08-09 13:34:16 -0400414 {
Shannon Woods53a94a82014-06-24 15:20:36 -0400415 const Rectangle &scissor = context->getState().getScissor();
Geoff Lang125deab2013-08-09 13:34:16 -0400416
Shannon Woods53a94a82014-06-24 15:20:36 -0400417 return scissor.x > 0 || scissor.y > 0 ||
418 scissor.width < writeBuffer->getWidth() ||
419 scissor.height < writeBuffer->getHeight();
Geoff Lang125deab2013-08-09 13:34:16 -0400420 }
421 else
422 {
423 return false;
424 }
425}
426
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400427bool ValidateBlitFramebufferParameters(gl::Context *context, GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400428 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask,
429 GLenum filter, bool fromAngleExtension)
430{
431 switch (filter)
432 {
433 case GL_NEAREST:
434 break;
435 case GL_LINEAR:
436 if (fromAngleExtension)
437 {
438 return gl::error(GL_INVALID_ENUM, false);
439 }
440 break;
441 default:
442 return gl::error(GL_INVALID_ENUM, false);
443 }
444
445 if ((mask & ~(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)) != 0)
446 {
447 return gl::error(GL_INVALID_VALUE, false);
448 }
449
450 if (mask == 0)
451 {
452 // ES3.0 spec, section 4.3.2 specifies that a mask of zero is valid and no
453 // buffers are copied.
454 return false;
455 }
456
457 if (fromAngleExtension && (srcX1 - srcX0 != dstX1 - dstX0 || srcY1 - srcY0 != dstY1 - dstY0))
458 {
459 ERR("Scaling and flipping in BlitFramebufferANGLE not supported by this implementation.");
460 return gl::error(GL_INVALID_OPERATION, false);
461 }
462
463 // ES3.0 spec, section 4.3.2 states that linear filtering is only available for the
464 // color buffer, leaving only nearest being unfiltered from above
465 if ((mask & ~GL_COLOR_BUFFER_BIT) != 0 && filter != GL_NEAREST)
466 {
467 return gl::error(GL_INVALID_OPERATION, false);
468 }
469
Shannon Woods53a94a82014-06-24 15:20:36 -0400470 if (context->getState().getReadFramebuffer()->id() == context->getState().getDrawFramebuffer()->id())
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400471 {
472 if (fromAngleExtension)
473 {
474 ERR("Blits with the same source and destination framebuffer are not supported by this "
475 "implementation.");
476 }
477 return gl::error(GL_INVALID_OPERATION, false);
478 }
479
Shannon Woods53a94a82014-06-24 15:20:36 -0400480 gl::Framebuffer *readFramebuffer = context->getState().getReadFramebuffer();
481 gl::Framebuffer *drawFramebuffer = context->getState().getDrawFramebuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400482 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
483 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
484 {
485 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
486 }
487
488 if (drawFramebuffer->getSamples() != 0)
489 {
490 return gl::error(GL_INVALID_OPERATION, false);
491 }
492
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400493 bool sameBounds = srcX0 == dstX0 && srcY0 == dstY0 && srcX1 == dstX1 && srcY1 == dstY1;
494
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400495 if (mask & GL_COLOR_BUFFER_BIT)
496 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400497 gl::FramebufferAttachment *readColorBuffer = readFramebuffer->getReadColorbuffer();
498 gl::FramebufferAttachment *drawColorBuffer = drawFramebuffer->getFirstColorbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400499
500 if (readColorBuffer && drawColorBuffer)
501 {
Geoff Lang005df412013-10-16 14:12:50 -0400502 GLenum readInternalFormat = readColorBuffer->getActualFormat();
Geoff Lang5d601382014-07-22 15:14:06 -0400503 const InternalFormat &readFormatInfo = GetInternalFormatInfo(readInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400504
505 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; i++)
506 {
507 if (drawFramebuffer->isEnabledColorAttachment(i))
508 {
Geoff Lang005df412013-10-16 14:12:50 -0400509 GLenum drawInternalFormat = drawFramebuffer->getColorbuffer(i)->getActualFormat();
Geoff Lang5d601382014-07-22 15:14:06 -0400510 const InternalFormat &drawFormatInfo = GetInternalFormatInfo(drawInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400511
Geoff Langb2f3d052013-08-13 12:49:27 -0400512 // The GL ES 3.0.2 spec (pg 193) states that:
513 // 1) If the read buffer is fixed point format, the draw buffer must be as well
514 // 2) If the read buffer is an unsigned integer format, the draw buffer must be as well
515 // 3) If the read buffer is a signed integer format, the draw buffer must be as well
Geoff Lang5d601382014-07-22 15:14:06 -0400516 if ( (readFormatInfo.componentType == GL_UNSIGNED_NORMALIZED || readFormatInfo.componentType == GL_SIGNED_NORMALIZED) &&
517 !(drawFormatInfo.componentType == GL_UNSIGNED_NORMALIZED || drawFormatInfo.componentType == GL_SIGNED_NORMALIZED))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400518 {
519 return gl::error(GL_INVALID_OPERATION, false);
520 }
521
Geoff Lang5d601382014-07-22 15:14:06 -0400522 if (readFormatInfo.componentType == GL_UNSIGNED_INT && drawFormatInfo.componentType != GL_UNSIGNED_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400523 {
524 return gl::error(GL_INVALID_OPERATION, false);
525 }
526
Geoff Lang5d601382014-07-22 15:14:06 -0400527 if (readFormatInfo.componentType == GL_INT && drawFormatInfo.componentType != GL_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400528 {
529 return gl::error(GL_INVALID_OPERATION, false);
530 }
531
Geoff Langb2f3d052013-08-13 12:49:27 -0400532 if (readColorBuffer->getSamples() > 0 && (readInternalFormat != drawInternalFormat || !sameBounds))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400533 {
534 return gl::error(GL_INVALID_OPERATION, false);
535 }
536 }
537 }
538
Geoff Lang5d601382014-07-22 15:14:06 -0400539 if ((readFormatInfo.componentType == GL_INT || readFormatInfo.componentType == GL_UNSIGNED_INT) && filter == GL_LINEAR)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400540 {
541 return gl::error(GL_INVALID_OPERATION, false);
542 }
543
544 if (fromAngleExtension)
545 {
546 const GLenum readColorbufferType = readFramebuffer->getReadColorbufferType();
547 if (readColorbufferType != GL_TEXTURE_2D && readColorbufferType != GL_RENDERBUFFER)
548 {
549 return gl::error(GL_INVALID_OPERATION, false);
550 }
551
552 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
553 {
554 if (drawFramebuffer->isEnabledColorAttachment(colorAttachment))
555 {
Jamie Madille92a3542014-07-03 10:38:58 -0400556 FramebufferAttachment *attachment = drawFramebuffer->getColorbuffer(colorAttachment);
557 ASSERT(attachment);
558
559 if (attachment->type() != GL_TEXTURE_2D && attachment->type() != GL_RENDERBUFFER)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400560 {
561 return gl::error(GL_INVALID_OPERATION, false);
562 }
563
Jamie Madille92a3542014-07-03 10:38:58 -0400564 if (attachment->getActualFormat() != readColorBuffer->getActualFormat())
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400565 {
566 return gl::error(GL_INVALID_OPERATION, false);
567 }
568 }
569 }
Geoff Lang125deab2013-08-09 13:34:16 -0400570 if (readFramebuffer->getSamples() != 0 && IsPartialBlit(context, readColorBuffer, drawColorBuffer,
571 srcX0, srcY0, srcX1, srcY1,
572 dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400573 {
574 return gl::error(GL_INVALID_OPERATION, false);
575 }
576 }
577 }
578 }
579
580 if (mask & GL_DEPTH_BUFFER_BIT)
581 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400582 gl::FramebufferAttachment *readDepthBuffer = readFramebuffer->getDepthbuffer();
583 gl::FramebufferAttachment *drawDepthBuffer = drawFramebuffer->getDepthbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400584
585 if (readDepthBuffer && drawDepthBuffer)
586 {
587 if (readDepthBuffer->getActualFormat() != drawDepthBuffer->getActualFormat())
588 {
589 return gl::error(GL_INVALID_OPERATION, false);
590 }
591
592 if (readDepthBuffer->getSamples() > 0 && !sameBounds)
593 {
594 return gl::error(GL_INVALID_OPERATION, false);
595 }
596
597 if (fromAngleExtension)
598 {
Geoff Lang125deab2013-08-09 13:34:16 -0400599 if (IsPartialBlit(context, readDepthBuffer, drawDepthBuffer,
600 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400601 {
602 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
603 return gl::error(GL_INVALID_OPERATION, false); // only whole-buffer copies are permitted
604 }
605
606 if (readDepthBuffer->getSamples() != 0 || drawDepthBuffer->getSamples() != 0)
607 {
608 return gl::error(GL_INVALID_OPERATION, false);
609 }
610 }
611 }
612 }
613
614 if (mask & GL_STENCIL_BUFFER_BIT)
615 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400616 gl::FramebufferAttachment *readStencilBuffer = readFramebuffer->getStencilbuffer();
617 gl::FramebufferAttachment *drawStencilBuffer = drawFramebuffer->getStencilbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400618
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400619 if (readStencilBuffer && drawStencilBuffer)
620 {
621 if (readStencilBuffer->getActualFormat() != drawStencilBuffer->getActualFormat())
622 {
623 return gl::error(GL_INVALID_OPERATION, false);
624 }
625
626 if (readStencilBuffer->getSamples() > 0 && !sameBounds)
627 {
628 return gl::error(GL_INVALID_OPERATION, false);
629 }
630
631 if (fromAngleExtension)
632 {
Geoff Lang125deab2013-08-09 13:34:16 -0400633 if (IsPartialBlit(context, readStencilBuffer, drawStencilBuffer,
634 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400635 {
636 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
637 return gl::error(GL_INVALID_OPERATION, false); // only whole-buffer copies are permitted
638 }
639
640 if (readStencilBuffer->getSamples() != 0 || drawStencilBuffer->getSamples() != 0)
641 {
642 return gl::error(GL_INVALID_OPERATION, false);
643 }
644 }
645 }
646 }
647
648 return true;
649}
650
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400651bool ValidateGetVertexAttribParameters(GLenum pname, int clientVersion)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400652{
653 switch (pname)
654 {
655 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
656 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
657 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
658 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
659 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
660 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
661 case GL_CURRENT_VERTEX_ATTRIB:
662 return true;
663
664 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR:
665 // Don't verify ES3 context because GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE uses
666 // the same constant.
667 META_ASSERT(GL_VERTEX_ATTRIB_ARRAY_DIVISOR == GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE);
668 return true;
669
670 case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
671 return ((clientVersion >= 3) ? true : gl::error(GL_INVALID_ENUM, false));
672
673 default:
674 return gl::error(GL_INVALID_ENUM, false);
675 }
676}
677
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400678bool ValidateTexParamParameters(gl::Context *context, GLenum pname, GLint param)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400679{
680 switch (pname)
681 {
682 case GL_TEXTURE_WRAP_R:
683 case GL_TEXTURE_SWIZZLE_R:
684 case GL_TEXTURE_SWIZZLE_G:
685 case GL_TEXTURE_SWIZZLE_B:
686 case GL_TEXTURE_SWIZZLE_A:
687 case GL_TEXTURE_BASE_LEVEL:
688 case GL_TEXTURE_MAX_LEVEL:
689 case GL_TEXTURE_COMPARE_MODE:
690 case GL_TEXTURE_COMPARE_FUNC:
691 case GL_TEXTURE_MIN_LOD:
692 case GL_TEXTURE_MAX_LOD:
693 if (context->getClientVersion() < 3)
694 {
695 return gl::error(GL_INVALID_ENUM, false);
696 }
697 break;
698
699 default: break;
700 }
701
702 switch (pname)
703 {
704 case GL_TEXTURE_WRAP_S:
705 case GL_TEXTURE_WRAP_T:
706 case GL_TEXTURE_WRAP_R:
707 switch (param)
708 {
709 case GL_REPEAT:
710 case GL_CLAMP_TO_EDGE:
711 case GL_MIRRORED_REPEAT:
712 return true;
713 default:
714 return gl::error(GL_INVALID_ENUM, false);
715 }
716
717 case GL_TEXTURE_MIN_FILTER:
718 switch (param)
719 {
720 case GL_NEAREST:
721 case GL_LINEAR:
722 case GL_NEAREST_MIPMAP_NEAREST:
723 case GL_LINEAR_MIPMAP_NEAREST:
724 case GL_NEAREST_MIPMAP_LINEAR:
725 case GL_LINEAR_MIPMAP_LINEAR:
726 return true;
727 default:
728 return gl::error(GL_INVALID_ENUM, false);
729 }
730 break;
731
732 case GL_TEXTURE_MAG_FILTER:
733 switch (param)
734 {
735 case GL_NEAREST:
736 case GL_LINEAR:
737 return true;
738 default:
739 return gl::error(GL_INVALID_ENUM, false);
740 }
741 break;
742
743 case GL_TEXTURE_USAGE_ANGLE:
744 switch (param)
745 {
746 case GL_NONE:
747 case GL_FRAMEBUFFER_ATTACHMENT_ANGLE:
748 return true;
749 default:
750 return gl::error(GL_INVALID_ENUM, false);
751 }
752 break;
753
754 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
Geoff Langc0b9ef42014-07-02 10:02:37 -0400755 if (!context->getExtensions().textureFilterAnisotropic)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400756 {
757 return gl::error(GL_INVALID_ENUM, false);
758 }
759
760 // we assume the parameter passed to this validation method is truncated, not rounded
761 if (param < 1)
762 {
763 return gl::error(GL_INVALID_VALUE, false);
764 }
765 return true;
766
767 case GL_TEXTURE_MIN_LOD:
768 case GL_TEXTURE_MAX_LOD:
769 // any value is permissible
770 return true;
771
772 case GL_TEXTURE_COMPARE_MODE:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400773 // Acceptable mode parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400774 switch (param)
775 {
776 case GL_NONE:
777 case GL_COMPARE_REF_TO_TEXTURE:
778 return true;
779 default:
780 return gl::error(GL_INVALID_ENUM, false);
781 }
782 break;
783
784 case GL_TEXTURE_COMPARE_FUNC:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400785 // Acceptable function parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400786 switch (param)
787 {
788 case GL_LEQUAL:
789 case GL_GEQUAL:
790 case GL_LESS:
791 case GL_GREATER:
792 case GL_EQUAL:
793 case GL_NOTEQUAL:
794 case GL_ALWAYS:
795 case GL_NEVER:
796 return true;
797 default:
798 return gl::error(GL_INVALID_ENUM, false);
799 }
800 break;
801
802 case GL_TEXTURE_SWIZZLE_R:
803 case GL_TEXTURE_SWIZZLE_G:
804 case GL_TEXTURE_SWIZZLE_B:
805 case GL_TEXTURE_SWIZZLE_A:
Geoff Langbc90a482013-09-17 16:51:27 -0400806 switch (param)
807 {
808 case GL_RED:
809 case GL_GREEN:
810 case GL_BLUE:
811 case GL_ALPHA:
812 case GL_ZERO:
813 case GL_ONE:
814 return true;
815 default:
816 return gl::error(GL_INVALID_ENUM, false);
817 }
818 break;
819
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400820 case GL_TEXTURE_BASE_LEVEL:
821 case GL_TEXTURE_MAX_LEVEL:
Nicolas Capens8de68282014-04-04 11:10:27 -0400822 if (param < 0)
823 {
824 return gl::error(GL_INVALID_VALUE, false);
825 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400826 return true;
827
828 default:
829 return gl::error(GL_INVALID_ENUM, false);
830 }
831}
832
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400833bool ValidateSamplerObjectParameter(GLenum pname)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400834{
835 switch (pname)
836 {
837 case GL_TEXTURE_MIN_FILTER:
838 case GL_TEXTURE_MAG_FILTER:
839 case GL_TEXTURE_WRAP_S:
840 case GL_TEXTURE_WRAP_T:
841 case GL_TEXTURE_WRAP_R:
842 case GL_TEXTURE_MIN_LOD:
843 case GL_TEXTURE_MAX_LOD:
844 case GL_TEXTURE_COMPARE_MODE:
845 case GL_TEXTURE_COMPARE_FUNC:
846 return true;
847
848 default:
849 return gl::error(GL_INVALID_ENUM, false);
850 }
851}
852
Jamie Madill26e91952014-03-05 15:01:27 -0500853bool ValidateReadPixelsParameters(gl::Context *context, GLint x, GLint y, GLsizei width, GLsizei height,
854 GLenum format, GLenum type, GLsizei *bufSize, GLvoid *pixels)
855{
Shannon Woods53a94a82014-06-24 15:20:36 -0400856 gl::Framebuffer *framebuffer = context->getState().getReadFramebuffer();
Jamie Madill893ab082014-05-16 16:56:10 -0400857 ASSERT(framebuffer);
Jamie Madill26e91952014-03-05 15:01:27 -0500858
859 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
860 {
861 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
862 }
863
Shannon Woods53a94a82014-06-24 15:20:36 -0400864 if (context->getState().getReadFramebuffer()->id() != 0 && framebuffer->getSamples() != 0)
Jamie Madill26e91952014-03-05 15:01:27 -0500865 {
866 return gl::error(GL_INVALID_OPERATION, false);
867 }
868
Jamie Madill893ab082014-05-16 16:56:10 -0400869 if (!framebuffer->getReadColorbuffer())
870 {
871 return gl::error(GL_INVALID_OPERATION, false);
872 }
873
Jamie Madill26e91952014-03-05 15:01:27 -0500874 GLenum currentInternalFormat, currentFormat, currentType;
Geoff Lange4a492b2014-06-19 14:14:41 -0400875 GLuint clientVersion = context->getClientVersion();
Jamie Madill26e91952014-03-05 15:01:27 -0500876
Jamie Madill893ab082014-05-16 16:56:10 -0400877 context->getCurrentReadFormatType(&currentInternalFormat, &currentFormat, &currentType);
Jamie Madill26e91952014-03-05 15:01:27 -0500878
Geoff Langbdc9b2f2014-04-16 14:41:54 -0400879 bool validReadFormat = (clientVersion < 3) ? ValidES2ReadFormatType(context, format, type) :
880 ValidES3ReadFormatType(context, currentInternalFormat, format, type);
Jamie Madill26e91952014-03-05 15:01:27 -0500881
882 if (!(currentFormat == format && currentType == type) && !validReadFormat)
883 {
884 return gl::error(GL_INVALID_OPERATION, false);
885 }
886
Geoff Lang5d601382014-07-22 15:14:06 -0400887 GLenum sizedInternalFormat = GetSizedInternalFormat(format, type);
888 const InternalFormat &sizedFormatInfo = GetInternalFormatInfo(sizedInternalFormat);
Jamie Madill26e91952014-03-05 15:01:27 -0500889
Geoff Lang5d601382014-07-22 15:14:06 -0400890 GLsizei outputPitch = sizedFormatInfo.computeRowPitch(type, width, context->getState().getPackAlignment());
Jamie Madill26e91952014-03-05 15:01:27 -0500891 // sized query sanity check
892 if (bufSize)
893 {
894 int requiredSize = outputPitch * height;
895 if (requiredSize > *bufSize)
896 {
897 return gl::error(GL_INVALID_OPERATION, false);
898 }
899 }
900
901 return true;
902}
903
Jamie Madilldb2f14c2014-05-13 13:56:30 -0400904bool ValidateBeginQuery(gl::Context *context, GLenum target, GLuint id)
905{
906 if (!ValidQueryType(context, target))
907 {
908 return gl::error(GL_INVALID_ENUM, false);
909 }
910
911 if (id == 0)
912 {
913 return gl::error(GL_INVALID_OPERATION, false);
914 }
915
916 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
917 // of zero, if the active query object name for <target> is non-zero (for the
918 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
919 // the active query for either target is non-zero), if <id> is the name of an
920 // existing query object whose type does not match <target>, or if <id> is the
921 // active query object name for any query type, the error INVALID_OPERATION is
922 // generated.
923
924 // Ensure no other queries are active
925 // NOTE: If other queries than occlusion are supported, we will need to check
926 // separately that:
927 // a) The query ID passed is not the current active query for any target/type
928 // b) There are no active queries for the requested target (and in the case
929 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
930 // no query may be active for either if glBeginQuery targets either.
Shannon Woods53a94a82014-06-24 15:20:36 -0400931 if (context->getState().isQueryActive())
Jamie Madilldb2f14c2014-05-13 13:56:30 -0400932 {
933 return gl::error(GL_INVALID_OPERATION, false);
934 }
935
936 Query *queryObject = context->getQuery(id, true, target);
937
938 // check that name was obtained with glGenQueries
939 if (!queryObject)
940 {
941 return gl::error(GL_INVALID_OPERATION, false);
942 }
943
944 // check for type mismatch
945 if (queryObject->getType() != target)
946 {
947 return gl::error(GL_INVALID_OPERATION, false);
948 }
949
950 return true;
951}
952
Jamie Madill45c785d2014-05-13 14:09:34 -0400953bool ValidateEndQuery(gl::Context *context, GLenum target)
954{
955 if (!ValidQueryType(context, target))
956 {
957 return gl::error(GL_INVALID_ENUM, false);
958 }
959
Shannon Woods53a94a82014-06-24 15:20:36 -0400960 const Query *queryObject = context->getState().getActiveQuery(target);
Jamie Madill45c785d2014-05-13 14:09:34 -0400961
962 if (queryObject == NULL)
963 {
964 return gl::error(GL_INVALID_OPERATION, false);
965 }
966
967 if (!queryObject->isStarted())
968 {
969 return gl::error(GL_INVALID_OPERATION, false);
970 }
971
972 return true;
973}
974
Jamie Madill36398922014-05-20 14:51:53 -0400975static bool ValidateUniformCommonBase(gl::Context *context, GLenum targetUniformType,
976 GLint location, GLsizei count, LinkedUniform **uniformOut)
Jamie Madilld7c7bb22014-05-20 10:55:54 -0400977{
978 if (count < 0)
979 {
980 return gl::error(GL_INVALID_VALUE, false);
981 }
982
Shannon Woods53a94a82014-06-24 15:20:36 -0400983 gl::ProgramBinary *programBinary = context->getState().getCurrentProgramBinary();
Jamie Madilld7c7bb22014-05-20 10:55:54 -0400984 if (!programBinary)
985 {
986 return gl::error(GL_INVALID_OPERATION, false);
987 }
988
989 if (location == -1)
990 {
991 // Silently ignore the uniform command
992 return false;
993 }
994
Jamie Madill36398922014-05-20 14:51:53 -0400995 if (!programBinary->isValidUniformLocation(location))
996 {
997 return gl::error(GL_INVALID_OPERATION, false);
998 }
999
1000 LinkedUniform *uniform = programBinary->getUniformByLocation(location);
1001
1002 // attempting to write an array to a non-array uniform is an INVALID_OPERATION
1003 if (uniform->elementCount() == 1 && count > 1)
1004 {
1005 return gl::error(GL_INVALID_OPERATION, false);
1006 }
1007
1008 *uniformOut = uniform;
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001009 return true;
1010}
1011
Jamie Madillaa981bd2014-05-20 10:55:55 -04001012bool ValidateUniform(gl::Context *context, GLenum uniformType, GLint location, GLsizei count)
1013{
1014 // Check for ES3 uniform entry points
Jamie Madillf2575982014-06-25 16:04:54 -04001015 if (VariableComponentType(uniformType) == GL_UNSIGNED_INT && context->getClientVersion() < 3)
Jamie Madillaa981bd2014-05-20 10:55:55 -04001016 {
1017 return gl::error(GL_INVALID_OPERATION, false);
1018 }
1019
Jamie Madill36398922014-05-20 14:51:53 -04001020 LinkedUniform *uniform = NULL;
1021 if (!ValidateUniformCommonBase(context, uniformType, location, count, &uniform))
1022 {
1023 return false;
1024 }
1025
Jamie Madillf2575982014-06-25 16:04:54 -04001026 GLenum targetBoolType = VariableBoolVectorType(uniformType);
Jamie Madill36398922014-05-20 14:51:53 -04001027 bool samplerUniformCheck = (IsSampler(uniform->type) && uniformType == GL_INT);
1028 if (!samplerUniformCheck && uniformType != uniform->type && targetBoolType != uniform->type)
1029 {
1030 return gl::error(GL_INVALID_OPERATION, false);
1031 }
1032
1033 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001034}
1035
1036bool ValidateUniformMatrix(gl::Context *context, GLenum matrixType, GLint location, GLsizei count,
1037 GLboolean transpose)
1038{
1039 // Check for ES3 uniform entry points
1040 int rows = VariableRowCount(matrixType);
1041 int cols = VariableColumnCount(matrixType);
1042 if (rows != cols && context->getClientVersion() < 3)
1043 {
1044 return gl::error(GL_INVALID_OPERATION, false);
1045 }
1046
1047 if (transpose != GL_FALSE && context->getClientVersion() < 3)
1048 {
1049 return gl::error(GL_INVALID_VALUE, false);
1050 }
1051
Jamie Madill36398922014-05-20 14:51:53 -04001052 LinkedUniform *uniform = NULL;
1053 if (!ValidateUniformCommonBase(context, matrixType, location, count, &uniform))
1054 {
1055 return false;
1056 }
1057
1058 if (uniform->type != matrixType)
1059 {
1060 return gl::error(GL_INVALID_OPERATION, false);
1061 }
1062
1063 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001064}
1065
Jamie Madill893ab082014-05-16 16:56:10 -04001066bool ValidateStateQuery(gl::Context *context, GLenum pname, GLenum *nativeType, unsigned int *numParams)
1067{
1068 if (!context->getQueryParameterInfo(pname, nativeType, numParams))
1069 {
1070 return gl::error(GL_INVALID_ENUM, false);
1071 }
1072
1073 if (pname >= GL_DRAW_BUFFER0 && pname <= GL_DRAW_BUFFER15)
1074 {
1075 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0);
1076
Geoff Langaae65a42014-05-26 12:43:44 -04001077 if (colorAttachment >= context->getCaps().maxDrawBuffers)
Jamie Madill893ab082014-05-16 16:56:10 -04001078 {
1079 return gl::error(GL_INVALID_OPERATION, false);
1080 }
1081 }
1082
1083 switch (pname)
1084 {
1085 case GL_TEXTURE_BINDING_2D:
1086 case GL_TEXTURE_BINDING_CUBE_MAP:
1087 case GL_TEXTURE_BINDING_3D:
1088 case GL_TEXTURE_BINDING_2D_ARRAY:
Shannon Woods53a94a82014-06-24 15:20:36 -04001089 if (context->getState().getActiveSampler() >= context->getMaximumCombinedTextureImageUnits())
Jamie Madill893ab082014-05-16 16:56:10 -04001090 {
1091 return gl::error(GL_INVALID_OPERATION, false);
1092 }
1093 break;
1094
1095 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1096 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1097 {
Shannon Woods53a94a82014-06-24 15:20:36 -04001098 Framebuffer *framebuffer = context->getState().getReadFramebuffer();
Jamie Madill893ab082014-05-16 16:56:10 -04001099 ASSERT(framebuffer);
1100 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1101 {
1102 return gl::error(GL_INVALID_OPERATION, false);
1103 }
1104
Jamie Madill3c7fa222014-06-05 13:08:51 -04001105 FramebufferAttachment *attachment = framebuffer->getReadColorbuffer();
1106 if (!attachment)
Jamie Madill893ab082014-05-16 16:56:10 -04001107 {
1108 return gl::error(GL_INVALID_OPERATION, false);
1109 }
1110 }
1111 break;
1112
1113 default:
1114 break;
1115 }
1116
1117 // pname is valid, but there are no parameters to return
1118 if (numParams == 0)
1119 {
1120 return false;
1121 }
1122
1123 return true;
1124}
1125
Jamie Madill560a8d82014-05-21 13:06:20 -04001126bool ValidateCopyTexImageParametersBase(gl::Context* context, GLenum target, GLint level, GLenum internalformat, bool isSubImage,
1127 GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height,
1128 GLint border, GLenum *textureFormatOut)
1129{
1130
1131 if (!ValidTexture2DDestinationTarget(context, target))
1132 {
1133 return gl::error(GL_INVALID_ENUM, false);
1134 }
1135
1136 if (level < 0 || xoffset < 0 || yoffset < 0 || zoffset < 0 || width < 0 || height < 0)
1137 {
1138 return gl::error(GL_INVALID_VALUE, false);
1139 }
1140
1141 if (std::numeric_limits<GLsizei>::max() - xoffset < width || std::numeric_limits<GLsizei>::max() - yoffset < height)
1142 {
1143 return gl::error(GL_INVALID_VALUE, false);
1144 }
1145
1146 if (border != 0)
1147 {
1148 return gl::error(GL_INVALID_VALUE, false);
1149 }
1150
1151 if (!ValidMipLevel(context, target, level))
1152 {
1153 return gl::error(GL_INVALID_VALUE, false);
1154 }
1155
Shannon Woods53a94a82014-06-24 15:20:36 -04001156 gl::Framebuffer *framebuffer = context->getState().getReadFramebuffer();
Jamie Madill560a8d82014-05-21 13:06:20 -04001157 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1158 {
1159 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
1160 }
1161
Shannon Woods53a94a82014-06-24 15:20:36 -04001162 if (context->getState().getReadFramebuffer()->id() != 0 && framebuffer->getSamples() != 0)
Jamie Madill560a8d82014-05-21 13:06:20 -04001163 {
1164 return gl::error(GL_INVALID_OPERATION, false);
1165 }
1166
Geoff Langaae65a42014-05-26 12:43:44 -04001167 const gl::Caps &caps = context->getCaps();
1168
Jamie Madill560a8d82014-05-21 13:06:20 -04001169 gl::Texture *texture = NULL;
1170 GLenum textureInternalFormat = GL_NONE;
Jamie Madill560a8d82014-05-21 13:06:20 -04001171 GLint textureLevelWidth = 0;
1172 GLint textureLevelHeight = 0;
1173 GLint textureLevelDepth = 0;
Geoff Langaae65a42014-05-26 12:43:44 -04001174 GLuint maxDimension = 0;
Jamie Madill560a8d82014-05-21 13:06:20 -04001175
1176 switch (target)
1177 {
1178 case GL_TEXTURE_2D:
1179 {
1180 gl::Texture2D *texture2d = context->getTexture2D();
1181 if (texture2d)
1182 {
1183 textureInternalFormat = texture2d->getInternalFormat(level);
Jamie Madill560a8d82014-05-21 13:06:20 -04001184 textureLevelWidth = texture2d->getWidth(level);
1185 textureLevelHeight = texture2d->getHeight(level);
1186 textureLevelDepth = 1;
1187 texture = texture2d;
Geoff Langaae65a42014-05-26 12:43:44 -04001188 maxDimension = caps.max2DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001189 }
1190 }
1191 break;
1192
1193 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1194 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1195 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1196 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1197 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1198 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1199 {
1200 gl::TextureCubeMap *textureCube = context->getTextureCubeMap();
1201 if (textureCube)
1202 {
1203 textureInternalFormat = textureCube->getInternalFormat(target, level);
Jamie Madill560a8d82014-05-21 13:06:20 -04001204 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);
Jamie Madill560a8d82014-05-21 13:06:20 -04001219 textureLevelWidth = texture2dArray->getWidth(level);
1220 textureLevelHeight = texture2dArray->getHeight(level);
1221 textureLevelDepth = texture2dArray->getLayers(level);
1222 texture = texture2dArray;
Geoff Langaae65a42014-05-26 12:43:44 -04001223 maxDimension = caps.max2DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001224 }
1225 }
1226 break;
1227
1228 case GL_TEXTURE_3D:
1229 {
1230 gl::Texture3D *texture3d = context->getTexture3D();
1231 if (texture3d)
1232 {
1233 textureInternalFormat = texture3d->getInternalFormat(level);
Jamie Madill560a8d82014-05-21 13:06:20 -04001234 textureLevelWidth = texture3d->getWidth(level);
1235 textureLevelHeight = texture3d->getHeight(level);
1236 textureLevelDepth = texture3d->getDepth(level);
1237 texture = texture3d;
Geoff Langaae65a42014-05-26 12:43:44 -04001238 maxDimension = caps.max3DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001239 }
1240 }
1241 break;
1242
1243 default:
1244 return gl::error(GL_INVALID_ENUM, false);
1245 }
1246
1247 if (!texture)
1248 {
1249 return gl::error(GL_INVALID_OPERATION, false);
1250 }
1251
1252 if (texture->isImmutable() && !isSubImage)
1253 {
1254 return gl::error(GL_INVALID_OPERATION, false);
1255 }
1256
Geoff Lang5d601382014-07-22 15:14:06 -04001257 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalformat);
1258
1259 if (formatInfo.depthBits > 0)
Jamie Madill560a8d82014-05-21 13:06:20 -04001260 {
1261 return gl::error(GL_INVALID_OPERATION, false);
1262 }
1263
Geoff Lang5d601382014-07-22 15:14:06 -04001264 if (formatInfo.compressed)
Jamie Madill560a8d82014-05-21 13:06:20 -04001265 {
Geoff Lang5d601382014-07-22 15:14:06 -04001266 if (((width % formatInfo.compressedBlockWidth) != 0 && width != textureLevelWidth) ||
1267 ((height % formatInfo.compressedBlockHeight) != 0 && height != textureLevelHeight))
Jamie Madill560a8d82014-05-21 13:06:20 -04001268 {
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 Lang5d601382014-07-22 15:14:06 -04001289 if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions()))
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 Madill2b976812014-08-25 15:47:49 -04001305static bool ValidateDrawBase(const gl::State &state, GLenum mode, GLsizei count, GLsizei maxVertex, GLsizei primcount)
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 Madilld9ba4f72014-08-04 10:47:59 -04001327 if (state.hasMappedBuffer(GL_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001328 {
1329 return gl::error(GL_INVALID_OPERATION, false);
1330 }
1331
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001332 const gl::DepthStencilState &depthStencilState = state.getDepthStencilState();
Jamie Madillac528012014-06-20 13:21:23 -04001333 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001334 state.getStencilRef() != state.getStencilBackRef() ||
Jamie Madillac528012014-06-20 13:21:23 -04001335 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 Madilld9ba4f72014-08-04 10:47:59 -04001344 const gl::Framebuffer *fbo = state.getDrawFramebuffer();
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001345 if (!fbo || fbo->completeness() != GL_FRAMEBUFFER_COMPLETE)
1346 {
1347 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
1348 }
1349
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001350 if (state.getCurrentProgramId() == 0)
Jamie Madilld4cfa572014-07-08 10:00:32 -04001351 {
1352 return gl::error(GL_INVALID_OPERATION, false);
1353 }
1354
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001355 gl::ProgramBinary *programBinary = state.getCurrentProgramBinary();
Jamie Madilld4cfa572014-07-08 10:00:32 -04001356 if (!programBinary->validateSamplers(NULL))
1357 {
1358 return gl::error(GL_INVALID_OPERATION, false);
1359 }
1360
Jamie Madill2b976812014-08-25 15:47:49 -04001361 // Buffer validations
1362 const VertexArray *vao = state.getVertexArray();
1363 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
1364 {
1365 const VertexAttribute &attrib = vao->getVertexAttribute(attributeIndex);
1366 bool attribActive = (programBinary->getSemanticIndex(attributeIndex) != -1);
1367 if (attribActive && attrib.enabled)
1368 {
1369 gl::Buffer *buffer = attrib.buffer.get();
1370
1371 if (buffer)
1372 {
1373 GLint64 attribStride = static_cast<GLint64>(ComputeVertexAttributeStride(attrib));
1374 GLint64 maxVertexElement = 0;
1375
1376 if (attrib.divisor > 0)
1377 {
1378 maxVertexElement = static_cast<GLint64>(primcount) / static_cast<GLint64>(attrib.divisor);
1379 }
1380 else
1381 {
1382 maxVertexElement = static_cast<GLint64>(maxVertex);
1383 }
1384
1385 GLint64 attribDataSize = maxVertexElement * attribStride;
1386
1387 // [OpenGL ES 3.0.2] section 2.9.4 page 40:
1388 // We can return INVALID_OPERATION if our vertex attribute does not have
1389 // enough backing data.
1390 if (attribDataSize > buffer->getSize())
1391 {
1392 return gl::error(GL_INVALID_OPERATION, false);
1393 }
1394 }
1395 else if (attrib.pointer == NULL)
1396 {
1397 // This is an application error that would normally result in a crash,
1398 // but we catch it and return an error
1399 ERR("An enabled vertex array has no buffer and no pointer.");
1400 return gl::error(GL_INVALID_OPERATION, false);
1401 }
1402 }
1403 }
1404
Jamie Madill250d33f2014-06-06 17:09:03 -04001405 // No-op if zero count
1406 return (count > 0);
1407}
1408
Jamie Madill2b976812014-08-25 15:47:49 -04001409bool ValidateDrawArrays(const gl::Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
Jamie Madill250d33f2014-06-06 17:09:03 -04001410{
Jamie Madillfd716582014-06-06 17:09:04 -04001411 if (first < 0)
Jamie Madill250d33f2014-06-06 17:09:03 -04001412 {
1413 return gl::error(GL_INVALID_VALUE, false);
1414 }
1415
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001416 const State &state = context->getState();
1417 gl::TransformFeedback *curTransformFeedback = state.getCurrentTransformFeedback();
Jamie Madillfd716582014-06-06 17:09:04 -04001418 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused() &&
1419 curTransformFeedback->getDrawMode() != mode)
1420 {
1421 // It is an invalid operation to call DrawArrays or DrawArraysInstanced with a draw mode
1422 // that does not match the current transform feedback object's draw mode (if transform feedback
1423 // is active), (3.0.2, section 2.14, pg 86)
1424 return gl::error(GL_INVALID_OPERATION, false);
1425 }
1426
Jamie Madill2b976812014-08-25 15:47:49 -04001427 if (!ValidateDrawBase(state, mode, count, count, primcount))
Jamie Madillfd716582014-06-06 17:09:04 -04001428 {
1429 return false;
1430 }
1431
1432 return true;
1433}
1434
1435bool ValidateDrawArraysInstanced(const gl::Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
1436{
1437 if (primcount < 0)
1438 {
1439 return gl::error(GL_INVALID_VALUE, false);
1440 }
1441
Jamie Madill2b976812014-08-25 15:47:49 -04001442 if (!ValidateDrawArrays(context, mode, first, count, primcount))
Jamie Madillfd716582014-06-06 17:09:04 -04001443 {
1444 return false;
1445 }
1446
1447 // No-op if zero primitive count
1448 return (primcount > 0);
1449}
1450
Jamie Madill2b976812014-08-25 15:47:49 -04001451bool ValidateDrawElements(const gl::Context *context, GLenum mode, GLsizei count, GLenum type,
1452 const GLvoid* indices, GLsizei primcount, rx::RangeUI *indexRangeOut)
Jamie Madillfd716582014-06-06 17:09:04 -04001453{
Jamie Madill250d33f2014-06-06 17:09:03 -04001454 switch (type)
1455 {
1456 case GL_UNSIGNED_BYTE:
1457 case GL_UNSIGNED_SHORT:
1458 break;
1459 case GL_UNSIGNED_INT:
Geoff Langc0b9ef42014-07-02 10:02:37 -04001460 if (!context->getExtensions().elementIndexUint)
Jamie Madill250d33f2014-06-06 17:09:03 -04001461 {
1462 return gl::error(GL_INVALID_ENUM, false);
1463 }
1464 break;
1465 default:
1466 return gl::error(GL_INVALID_ENUM, false);
1467 }
1468
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001469 const State &state = context->getState();
1470
1471 gl::TransformFeedback *curTransformFeedback = state.getCurrentTransformFeedback();
Jamie Madill250d33f2014-06-06 17:09:03 -04001472 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused())
1473 {
1474 // It is an invalid operation to call DrawElements, DrawRangeElements or DrawElementsInstanced
1475 // while transform feedback is active, (3.0.2, section 2.14, pg 86)
1476 return gl::error(GL_INVALID_OPERATION, false);
1477 }
1478
1479 // Check for mapped buffers
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001480 if (state.hasMappedBuffer(GL_ELEMENT_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001481 {
1482 return gl::error(GL_INVALID_OPERATION, false);
1483 }
1484
Jamie Madill2b976812014-08-25 15:47:49 -04001485 const gl::VertexArray *vao = state.getVertexArray();
1486 const gl::Buffer *elementArrayBuffer = vao->getElementArrayBuffer();
1487 if (!indices && !elementArrayBuffer)
Jamie Madilld4cfa572014-07-08 10:00:32 -04001488 {
1489 return gl::error(GL_INVALID_OPERATION, false);
1490 }
1491
Jamie Madillae3000b2014-08-25 15:47:51 -04001492 if (elementArrayBuffer)
1493 {
1494 const gl::Type &typeInfo = gl::GetTypeInfo(type);
1495
1496 GLint64 offset = reinterpret_cast<GLint64>(indices);
1497 GLint64 byteCount = static_cast<GLint64>(typeInfo.bytes) * static_cast<GLint64>(count)+offset;
1498
1499 // check for integer overflows
1500 if (static_cast<GLuint>(count) > (std::numeric_limits<GLuint>::max() / typeInfo.bytes) ||
1501 byteCount > static_cast<GLint64>(std::numeric_limits<GLuint>::max()))
1502 {
1503 return gl::error(GL_OUT_OF_MEMORY, false);
1504 }
1505
1506 // Check for reading past the end of the bound buffer object
1507 if (byteCount > elementArrayBuffer->getSize())
1508 {
1509 return gl::error(GL_INVALID_OPERATION, false);
1510 }
1511 }
1512 else if (!indices)
1513 {
1514 // Catch this programming error here
1515 return gl::error(GL_INVALID_OPERATION, false);
1516 }
1517
Jamie Madill2b976812014-08-25 15:47:49 -04001518 // Use max index to validate if our vertex buffers are large enough for the pull.
1519 // TODO: offer fast path, with disabled index validation.
1520 // TODO: also disable index checking on back-ends that are robust to out-of-range accesses.
1521 if (elementArrayBuffer)
1522 {
1523 unsigned int offset = reinterpret_cast<unsigned int>(indices);
1524 if (!elementArrayBuffer->getIndexRangeCache()->findRange(type, offset, count, indexRangeOut, NULL))
1525 {
1526 const void *dataPointer = elementArrayBuffer->getImplementation()->getData();
1527 const uint8_t *offsetPointer = static_cast<const uint8_t *>(dataPointer) + offset;
1528 *indexRangeOut = rx::IndexRangeCache::ComputeRange(type, offsetPointer, count);
1529 }
1530 }
1531 else
1532 {
1533 *indexRangeOut = rx::IndexRangeCache::ComputeRange(type, indices, count);
1534 }
1535
1536 if (!ValidateDrawBase(state, mode, count, static_cast<GLsizei>(indexRangeOut->end), primcount))
Jamie Madillfd716582014-06-06 17:09:04 -04001537 {
1538 return false;
1539 }
1540
1541 return true;
1542}
1543
Jamie Madill2b976812014-08-25 15:47:49 -04001544bool ValidateDrawElementsInstanced(const gl::Context *context,
1545 GLenum mode, GLsizei count, GLenum type,
1546 const GLvoid *indices, GLsizei primcount,
1547 rx::RangeUI *indexRangeOut)
Jamie Madillfd716582014-06-06 17:09:04 -04001548{
1549 if (primcount < 0)
1550 {
1551 return gl::error(GL_INVALID_VALUE, false);
1552 }
1553
Jamie Madill2b976812014-08-25 15:47:49 -04001554 if (!ValidateDrawElements(context, mode, count, type, indices, primcount, indexRangeOut))
Jamie Madillfd716582014-06-06 17:09:04 -04001555 {
1556 return false;
1557 }
1558
1559 // No-op zero primitive count
1560 return (primcount > 0);
Jamie Madill250d33f2014-06-06 17:09:03 -04001561}
1562
Jamie Madill55ec3b12014-07-03 10:38:57 -04001563bool ValidateFramebufferTextureBase(const gl::Context *context, GLenum target, GLenum attachment,
1564 GLuint texture, GLint level)
Jamie Madill570f7c82014-07-03 10:38:54 -04001565{
Jamie Madill55ec3b12014-07-03 10:38:57 -04001566 if (!ValidFramebufferTarget(target))
1567 {
1568 return gl::error(GL_INVALID_ENUM, false);
1569 }
1570
1571 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill570f7c82014-07-03 10:38:54 -04001572 {
1573 return false;
1574 }
1575
Jamie Madill55ec3b12014-07-03 10:38:57 -04001576 if (texture != 0)
1577 {
1578 gl::Texture *tex = context->getTexture(texture);
1579
1580 if (tex == NULL)
1581 {
1582 return gl::error(GL_INVALID_OPERATION, false);
1583 }
1584
1585 if (level < 0)
1586 {
1587 return gl::error(GL_INVALID_VALUE, false);
1588 }
1589 }
1590
Shannon Woods53a94a82014-06-24 15:20:36 -04001591 const gl::Framebuffer *framebuffer = context->getState().getTargetFramebuffer(target);
1592 GLuint framebufferHandle = context->getState().getTargetFramebuffer(target)->id();
Jamie Madill55ec3b12014-07-03 10:38:57 -04001593
1594 if (framebufferHandle == 0 || !framebuffer)
1595 {
1596 return gl::error(GL_INVALID_OPERATION, false);
1597 }
1598
1599 return true;
1600}
1601
1602bool ValidateFramebufferTexture2D(const gl::Context *context, GLenum target, GLenum attachment,
1603 GLenum textarget, GLuint texture, GLint level)
1604{
1605 // Attachments are required to be bound to level 0 in ES2
1606 if (context->getClientVersion() < 3 && level != 0)
1607 {
1608 return gl::error(GL_INVALID_VALUE, false);
1609 }
1610
1611 if (!ValidateFramebufferTextureBase(context, target, attachment, texture, level))
Jamie Madill570f7c82014-07-03 10:38:54 -04001612 {
1613 return false;
1614 }
1615
Jamie Madill55ec3b12014-07-03 10:38:57 -04001616 if (texture != 0)
1617 {
1618 gl::Texture *tex = context->getTexture(texture);
1619 ASSERT(tex);
1620
Jamie Madill2a6564e2014-07-11 09:53:19 -04001621 const gl::Caps &caps = context->getCaps();
1622
Jamie Madill55ec3b12014-07-03 10:38:57 -04001623 switch (textarget)
1624 {
1625 case GL_TEXTURE_2D:
1626 {
Jamie Madill2a6564e2014-07-11 09:53:19 -04001627 if (level > gl::log2(caps.max2DTextureSize))
Jamie Madill55ec3b12014-07-03 10:38:57 -04001628 {
1629 return gl::error(GL_INVALID_VALUE, false);
1630 }
1631 if (tex->getTarget() != GL_TEXTURE_2D)
1632 {
1633 return gl::error(GL_INVALID_OPERATION, false);
1634 }
1635 gl::Texture2D *tex2d = static_cast<gl::Texture2D *>(tex);
1636 if (tex2d->isCompressed(level))
1637 {
1638 return gl::error(GL_INVALID_OPERATION, false);
1639 }
1640 }
1641 break;
1642
1643 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1644 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1645 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1646 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1647 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1648 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1649 {
Jamie Madill2a6564e2014-07-11 09:53:19 -04001650 if (level > gl::log2(caps.maxCubeMapTextureSize))
Jamie Madill55ec3b12014-07-03 10:38:57 -04001651 {
1652 return gl::error(GL_INVALID_VALUE, false);
1653 }
1654 if (tex->getTarget() != GL_TEXTURE_CUBE_MAP)
1655 {
1656 return gl::error(GL_INVALID_OPERATION, false);
1657 }
1658 gl::TextureCubeMap *texcube = static_cast<gl::TextureCubeMap *>(tex);
1659 if (texcube->isCompressed(textarget, level))
1660 {
1661 return gl::error(GL_INVALID_OPERATION, false);
1662 }
1663 }
1664 break;
1665
1666 default:
1667 return gl::error(GL_INVALID_ENUM, false);
1668 }
1669 }
1670
Jamie Madill570f7c82014-07-03 10:38:54 -04001671 return true;
1672}
1673
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001674}