blob: 73aafab685dd5e20977f3e230f048563ecf6eb01 [file] [log] [blame]
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001//
Geoff Langcec35902014-04-16 10:52:36 -04002// Copyright (c) 2013-2014 The ANGLE Project Authors. All rights reserved.
Geoff Lange8ebe7f2013-08-05 15:03:13 -04003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// validationES.h: Validation functions for generic OpenGL ES entry point parameters
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/validationES.h"
10#include "libANGLE/validationES2.h"
11#include "libANGLE/validationES3.h"
12#include "libANGLE/Context.h"
Geoff Langa8406172015-07-21 16:53:39 -040013#include "libANGLE/Display.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050014#include "libANGLE/Texture.h"
15#include "libANGLE/Framebuffer.h"
16#include "libANGLE/FramebufferAttachment.h"
17#include "libANGLE/formatutils.h"
Geoff Langa8406172015-07-21 16:53:39 -040018#include "libANGLE/Image.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050019#include "libANGLE/Query.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050020#include "libANGLE/Program.h"
21#include "libANGLE/Uniform.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050022#include "libANGLE/TransformFeedback.h"
23#include "libANGLE/VertexArray.h"
Geoff Lange8ebe7f2013-08-05 15:03:13 -040024
25#include "common/mathutil.h"
26#include "common/utilities.h"
27
28namespace gl
29{
Jamie Madill1ca74672015-07-21 15:14:11 -040030namespace
31{
32bool ValidateDrawAttribs(gl::Context *context, GLint primcount, GLint maxVertex)
33{
34 const gl::State &state = context->getState();
35 const gl::Program *program = state.getProgram();
36
37 const VertexArray *vao = state.getVertexArray();
38 const auto &vertexAttribs = vao->getVertexAttributes();
39 const int *semanticIndexes = program->getSemanticIndexes();
40 size_t maxEnabledAttrib = vao->getMaxEnabledAttribute();
41 for (size_t attributeIndex = 0; attributeIndex < maxEnabledAttrib; ++attributeIndex)
42 {
43 const VertexAttribute &attrib = vertexAttribs[attributeIndex];
44 bool attribActive = (semanticIndexes[attributeIndex] != -1);
45 if (attribActive && attrib.enabled)
46 {
47 gl::Buffer *buffer = attrib.buffer.get();
48
49 if (buffer)
50 {
51 GLint64 attribStride = static_cast<GLint64>(ComputeVertexAttributeStride(attrib));
52 GLint64 maxVertexElement = 0;
53
54 if (attrib.divisor > 0)
55 {
56 maxVertexElement =
57 static_cast<GLint64>(primcount) / static_cast<GLint64>(attrib.divisor);
58 }
59 else
60 {
61 maxVertexElement = static_cast<GLint64>(maxVertex);
62 }
63
64 // If we're drawing zero vertices, we have enough data.
65 if (maxVertexElement > 0)
66 {
67 // Note: Last vertex element does not take the full stride!
68 GLint64 attribSize =
69 static_cast<GLint64>(ComputeVertexAttributeTypeSize(attrib));
70 GLint64 attribDataSize = (maxVertexElement - 1) * attribStride + attribSize;
71
72 // [OpenGL ES 3.0.2] section 2.9.4 page 40:
73 // We can return INVALID_OPERATION if our vertex attribute does not have
74 // enough backing data.
75 if (attribDataSize > buffer->getSize())
76 {
77 context->recordError(Error(GL_INVALID_OPERATION));
78 return false;
79 }
80 }
81 }
82 else if (attrib.pointer == NULL)
83 {
84 // This is an application error that would normally result in a crash,
85 // but we catch it and return an error
86 context->recordError(Error(
87 GL_INVALID_OPERATION, "An enabled vertex array has no buffer and no pointer."));
88 return false;
89 }
90 }
91 }
92
93 return true;
94}
95
96} // anonymous namespace
Geoff Lange8ebe7f2013-08-05 15:03:13 -040097
Geoff Lang0550d032014-01-30 11:29:07 -050098bool ValidCap(const Context *context, GLenum cap)
99{
100 switch (cap)
101 {
102 case GL_CULL_FACE:
103 case GL_POLYGON_OFFSET_FILL:
104 case GL_SAMPLE_ALPHA_TO_COVERAGE:
105 case GL_SAMPLE_COVERAGE:
106 case GL_SCISSOR_TEST:
107 case GL_STENCIL_TEST:
108 case GL_DEPTH_TEST:
109 case GL_BLEND:
110 case GL_DITHER:
111 return true;
112 case GL_PRIMITIVE_RESTART_FIXED_INDEX:
113 case GL_RASTERIZER_DISCARD:
114 return (context->getClientVersion() >= 3);
115 default:
116 return false;
117 }
118}
119
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500120bool ValidTextureTarget(const Context *context, GLenum target)
Jamie Madill35d15012013-10-07 10:46:37 -0400121{
Jamie Madilld7460c72014-01-21 16:38:14 -0500122 switch (target)
Jamie Madill35d15012013-10-07 10:46:37 -0400123 {
Jamie Madilld7460c72014-01-21 16:38:14 -0500124 case GL_TEXTURE_2D:
125 case GL_TEXTURE_CUBE_MAP:
126 return true;
Jamie Madill35d15012013-10-07 10:46:37 -0400127
Jamie Madilld7460c72014-01-21 16:38:14 -0500128 case GL_TEXTURE_3D:
129 case GL_TEXTURE_2D_ARRAY:
130 return (context->getClientVersion() >= 3);
131
132 default:
133 return false;
134 }
Jamie Madill35d15012013-10-07 10:46:37 -0400135}
136
Shannon Woods4dfed832014-03-17 20:03:39 -0400137// This function differs from ValidTextureTarget in that the target must be
138// usable as the destination of a 2D operation-- so a cube face is valid, but
139// GL_TEXTURE_CUBE_MAP is not.
Jamie Madill560a8d82014-05-21 13:06:20 -0400140// Note: duplicate of IsInternalTextureTarget
Shannon Woods4dfed832014-03-17 20:03:39 -0400141bool ValidTexture2DDestinationTarget(const Context *context, GLenum target)
142{
143 switch (target)
144 {
145 case GL_TEXTURE_2D:
146 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
147 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
148 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
149 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
150 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
151 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
152 return true;
153 case GL_TEXTURE_2D_ARRAY:
154 case GL_TEXTURE_3D:
155 return (context->getClientVersion() >= 3);
156 default:
157 return false;
158 }
159}
160
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500161bool ValidFramebufferTarget(GLenum target)
162{
Geoff Langd4475812015-03-18 10:53:05 -0400163 static_assert(GL_DRAW_FRAMEBUFFER_ANGLE == GL_DRAW_FRAMEBUFFER && GL_READ_FRAMEBUFFER_ANGLE == GL_READ_FRAMEBUFFER,
164 "ANGLE framebuffer enums must equal the ES3 framebuffer enums.");
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500165
166 switch (target)
167 {
168 case GL_FRAMEBUFFER: return true;
169 case GL_READ_FRAMEBUFFER: return true;
170 case GL_DRAW_FRAMEBUFFER: return true;
171 default: return false;
172 }
173}
174
Jamie Madill8c96d582014-03-05 15:01:23 -0500175bool ValidBufferTarget(const Context *context, GLenum target)
176{
177 switch (target)
178 {
179 case GL_ARRAY_BUFFER:
180 case GL_ELEMENT_ARRAY_BUFFER:
181 return true;
182
Jamie Madill8c96d582014-03-05 15:01:23 -0500183 case GL_PIXEL_PACK_BUFFER:
184 case GL_PIXEL_UNPACK_BUFFER:
Geoff Langc0b9ef42014-07-02 10:02:37 -0400185 return context->getExtensions().pixelBufferObject;
Shannon Woods158c4382014-05-06 13:00:07 -0400186
Shannon Woodsb3801742014-03-27 14:59:19 -0400187 case GL_COPY_READ_BUFFER:
188 case GL_COPY_WRITE_BUFFER:
Jamie Madill8c96d582014-03-05 15:01:23 -0500189 case GL_TRANSFORM_FEEDBACK_BUFFER:
190 case GL_UNIFORM_BUFFER:
191 return (context->getClientVersion() >= 3);
192
193 default:
194 return false;
195 }
196}
197
Jamie Madill70656a62014-03-05 15:01:26 -0500198bool ValidBufferParameter(const Context *context, GLenum pname)
199{
Geoff Langcc6f55d2015-03-20 13:01:02 -0400200 const Extensions &extensions = context->getExtensions();
201
Jamie Madill70656a62014-03-05 15:01:26 -0500202 switch (pname)
203 {
204 case GL_BUFFER_USAGE:
205 case GL_BUFFER_SIZE:
206 return true;
207
Geoff Langcc6f55d2015-03-20 13:01:02 -0400208 case GL_BUFFER_ACCESS_OES:
209 return extensions.mapBuffer;
210
211 case GL_BUFFER_MAPPED:
212 static_assert(GL_BUFFER_MAPPED == GL_BUFFER_MAPPED_OES, "GL enums should be equal.");
213 return (context->getClientVersion() >= 3) || extensions.mapBuffer || extensions.mapBufferRange;
214
Jamie Madill70656a62014-03-05 15:01:26 -0500215 // GL_BUFFER_MAP_POINTER is a special case, and may only be
216 // queried with GetBufferPointerv
217 case GL_BUFFER_ACCESS_FLAGS:
Jamie Madill70656a62014-03-05 15:01:26 -0500218 case GL_BUFFER_MAP_OFFSET:
219 case GL_BUFFER_MAP_LENGTH:
Geoff Langcc6f55d2015-03-20 13:01:02 -0400220 return (context->getClientVersion() >= 3) || extensions.mapBufferRange;
Jamie Madill70656a62014-03-05 15:01:26 -0500221
222 default:
223 return false;
224 }
225}
226
Jamie Madill8c96d582014-03-05 15:01:23 -0500227bool ValidMipLevel(const Context *context, GLenum target, GLint level)
Geoff Langce635692013-09-24 13:56:32 -0400228{
Geoff Langaae65a42014-05-26 12:43:44 -0400229 size_t maxDimension = 0;
Geoff Langce635692013-09-24 13:56:32 -0400230 switch (target)
231 {
Geoff Langaae65a42014-05-26 12:43:44 -0400232 case GL_TEXTURE_2D: maxDimension = context->getCaps().max2DTextureSize; break;
Geoff Langce635692013-09-24 13:56:32 -0400233 case GL_TEXTURE_CUBE_MAP:
234 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
235 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
236 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
237 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
238 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
Geoff Langaae65a42014-05-26 12:43:44 -0400239 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: maxDimension = context->getCaps().maxCubeMapTextureSize; break;
240 case GL_TEXTURE_3D: maxDimension = context->getCaps().max3DTextureSize; break;
241 case GL_TEXTURE_2D_ARRAY: maxDimension = context->getCaps().max2DTextureSize; break;
Geoff Langce635692013-09-24 13:56:32 -0400242 default: UNREACHABLE();
243 }
244
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700245 return level <= gl::log2(static_cast<int>(maxDimension));
Geoff Langce635692013-09-24 13:56:32 -0400246}
247
Geoff Langb1196682014-07-23 13:47:29 -0400248bool ValidImageSize(const Context *context, GLenum target, GLint level,
Jamie Madill4fd75c12014-06-23 10:53:54 -0400249 GLsizei width, GLsizei height, GLsizei depth)
Geoff Langce635692013-09-24 13:56:32 -0400250{
251 if (level < 0 || width < 0 || height < 0 || depth < 0)
252 {
253 return false;
254 }
255
Geoff Langc0b9ef42014-07-02 10:02:37 -0400256 if (!context->getExtensions().textureNPOT &&
Jamie Madill4fd75c12014-06-23 10:53:54 -0400257 (level != 0 && (!gl::isPow2(width) || !gl::isPow2(height) || !gl::isPow2(depth))))
Geoff Langce635692013-09-24 13:56:32 -0400258 {
259 return false;
260 }
261
262 if (!ValidMipLevel(context, target, level))
263 {
264 return false;
265 }
266
267 return true;
268}
269
Geoff Langb1196682014-07-23 13:47:29 -0400270bool ValidCompressedImageSize(const Context *context, GLenum internalFormat, GLsizei width, GLsizei height)
Geoff Langd4f180b2013-09-24 13:57:44 -0400271{
Geoff Lang5d601382014-07-22 15:14:06 -0400272 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalFormat);
273 if (!formatInfo.compressed)
Geoff Langd4f180b2013-09-24 13:57:44 -0400274 {
275 return false;
276 }
277
Geoff Lang5d601382014-07-22 15:14:06 -0400278 if (width < 0 || (static_cast<GLuint>(width) > formatInfo.compressedBlockWidth && width % formatInfo.compressedBlockWidth != 0) ||
279 height < 0 || (static_cast<GLuint>(height) > formatInfo.compressedBlockHeight && height % formatInfo.compressedBlockHeight != 0))
Geoff Langd4f180b2013-09-24 13:57:44 -0400280 {
281 return false;
282 }
283
284 return true;
285}
286
Geoff Lang37dde692014-01-31 16:34:54 -0500287bool ValidQueryType(const Context *context, GLenum queryType)
288{
Geoff Langd4475812015-03-18 10:53:05 -0400289 static_assert(GL_ANY_SAMPLES_PASSED == GL_ANY_SAMPLES_PASSED_EXT, "GL extension enums not equal.");
290 static_assert(GL_ANY_SAMPLES_PASSED_CONSERVATIVE == GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, "GL extension enums not equal.");
Geoff Lang37dde692014-01-31 16:34:54 -0500291
292 switch (queryType)
293 {
294 case GL_ANY_SAMPLES_PASSED:
295 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
296 return true;
297 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
298 return (context->getClientVersion() >= 3);
299 default:
300 return false;
301 }
302}
303
Geoff Langb1196682014-07-23 13:47:29 -0400304bool ValidProgram(Context *context, GLuint id)
Geoff Lang48dcae72014-02-05 16:28:24 -0500305{
306 // ES3 spec (section 2.11.1) -- "Commands that accept shader or program object names will generate the
307 // error INVALID_VALUE if the provided name is not the name of either a shader or program object and
308 // INVALID_OPERATION if the provided name identifies an object that is not the expected type."
309
310 if (context->getProgram(id) != NULL)
311 {
312 return true;
313 }
314 else if (context->getShader(id) != NULL)
315 {
316 // ID is the wrong type
Geoff Langb1196682014-07-23 13:47:29 -0400317 context->recordError(Error(GL_INVALID_OPERATION));
318 return false;
Geoff Lang48dcae72014-02-05 16:28:24 -0500319 }
320 else
321 {
322 // No shader/program object has this ID
Geoff Langb1196682014-07-23 13:47:29 -0400323 context->recordError(Error(GL_INVALID_VALUE));
324 return false;
Geoff Lang48dcae72014-02-05 16:28:24 -0500325 }
326}
327
Geoff Langb1196682014-07-23 13:47:29 -0400328bool ValidateAttachmentTarget(gl::Context *context, GLenum attachment)
Jamie Madillb4472272014-07-03 10:38:55 -0400329{
330 if (attachment >= GL_COLOR_ATTACHMENT0_EXT && attachment <= GL_COLOR_ATTACHMENT15_EXT)
331 {
332 const unsigned int colorAttachment = (attachment - GL_COLOR_ATTACHMENT0_EXT);
333
Geoff Langaae65a42014-05-26 12:43:44 -0400334 if (colorAttachment >= context->getCaps().maxColorAttachments)
Jamie Madillb4472272014-07-03 10:38:55 -0400335 {
Geoff Langb1196682014-07-23 13:47:29 -0400336 context->recordError(Error(GL_INVALID_VALUE));
337 return false;
Jamie Madillb4472272014-07-03 10:38:55 -0400338 }
339 }
340 else
341 {
342 switch (attachment)
343 {
344 case GL_DEPTH_ATTACHMENT:
345 case GL_STENCIL_ATTACHMENT:
346 break;
347
348 case GL_DEPTH_STENCIL_ATTACHMENT:
349 if (context->getClientVersion() < 3)
350 {
Geoff Langb1196682014-07-23 13:47:29 -0400351 context->recordError(Error(GL_INVALID_ENUM));
352 return false;
Jamie Madillb4472272014-07-03 10:38:55 -0400353 }
354 break;
355
356 default:
Geoff Langb1196682014-07-23 13:47:29 -0400357 context->recordError(Error(GL_INVALID_ENUM));
358 return false;
Jamie Madillb4472272014-07-03 10:38:55 -0400359 }
360 }
361
362 return true;
363}
364
Corentin Walleze0902642014-11-04 12:32:15 -0800365bool ValidateRenderbufferStorageParametersBase(gl::Context *context, GLenum target, GLsizei samples,
366 GLenum internalformat, GLsizei width, GLsizei height)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400367{
368 switch (target)
369 {
370 case GL_RENDERBUFFER:
371 break;
372 default:
Geoff Langb1196682014-07-23 13:47:29 -0400373 context->recordError(Error(GL_INVALID_ENUM));
374 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400375 }
376
377 if (width < 0 || height < 0 || samples < 0)
378 {
Geoff Langb1196682014-07-23 13:47:29 -0400379 context->recordError(Error(GL_INVALID_VALUE));
380 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400381 }
382
Geoff Langd87878e2014-09-19 15:42:59 -0400383 const TextureCaps &formatCaps = context->getTextureCaps().get(internalformat);
384 if (!formatCaps.renderable)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400385 {
Geoff Langb1196682014-07-23 13:47:29 -0400386 context->recordError(Error(GL_INVALID_ENUM));
387 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400388 }
389
390 // ANGLE_framebuffer_multisample does not explicitly state that the internal format must be
391 // sized but it does state that the format must be in the ES2.0 spec table 4.5 which contains
Corentin Walleze0902642014-11-04 12:32:15 -0800392 // only sized internal formats.
Geoff Langd87878e2014-09-19 15:42:59 -0400393 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalformat);
Geoff Lang5d601382014-07-22 15:14:06 -0400394 if (formatInfo.pixelBytes == 0)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400395 {
Geoff Langb1196682014-07-23 13:47:29 -0400396 context->recordError(Error(GL_INVALID_ENUM));
397 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400398 }
399
Geoff Langaae65a42014-05-26 12:43:44 -0400400 if (static_cast<GLuint>(std::max(width, height)) > context->getCaps().maxRenderbufferSize)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400401 {
Geoff Langb1196682014-07-23 13:47:29 -0400402 context->recordError(Error(GL_INVALID_VALUE));
403 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400404 }
405
Shannon Woods53a94a82014-06-24 15:20:36 -0400406 GLuint handle = context->getState().getRenderbufferId();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400407 if (handle == 0)
408 {
Geoff Langb1196682014-07-23 13:47:29 -0400409 context->recordError(Error(GL_INVALID_OPERATION));
410 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400411 }
412
413 return true;
414}
415
Corentin Walleze0902642014-11-04 12:32:15 -0800416bool ValidateRenderbufferStorageParametersANGLE(gl::Context *context, GLenum target, GLsizei samples,
417 GLenum internalformat, GLsizei width, GLsizei height)
418{
Austin Kinrossd2cf3ad2015-01-07 14:00:30 -0800419 ASSERT(samples == 0 || context->getExtensions().framebufferMultisample);
Corentin Walleze0902642014-11-04 12:32:15 -0800420
421 // ANGLE_framebuffer_multisample states that the value of samples must be less than or equal
Geoff Langdef624b2015-04-13 10:46:56 -0400422 // to MAX_SAMPLES_ANGLE (Context::getCaps().maxSamples) otherwise GL_INVALID_VALUE is
Corentin Walleze0902642014-11-04 12:32:15 -0800423 // generated.
Geoff Langdef624b2015-04-13 10:46:56 -0400424 if (static_cast<GLuint>(samples) > context->getCaps().maxSamples)
Corentin Walleze0902642014-11-04 12:32:15 -0800425 {
426 context->recordError(Error(GL_INVALID_VALUE));
427 return false;
428 }
429
430 // ANGLE_framebuffer_multisample states GL_OUT_OF_MEMORY is generated on a failure to create
431 // the specified storage. This is different than ES 3.0 in which a sample number higher
432 // than the maximum sample number supported by this format generates a GL_INVALID_VALUE.
Geoff Langa4903b72015-03-02 16:02:48 -0800433 // The TextureCaps::getMaxSamples method is only guarenteed to be valid when the context is ES3.
434 if (context->getClientVersion() >= 3)
Corentin Walleze0902642014-11-04 12:32:15 -0800435 {
Geoff Langa4903b72015-03-02 16:02:48 -0800436 const TextureCaps &formatCaps = context->getTextureCaps().get(internalformat);
437 if (static_cast<GLuint>(samples) > formatCaps.getMaxSamples())
438 {
439 context->recordError(Error(GL_OUT_OF_MEMORY));
440 return false;
441 }
Corentin Walleze0902642014-11-04 12:32:15 -0800442 }
443
444 return ValidateRenderbufferStorageParametersBase(context, target, samples, internalformat, width, height);
445}
446
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500447bool ValidateFramebufferRenderbufferParameters(gl::Context *context, GLenum target, GLenum attachment,
448 GLenum renderbuffertarget, GLuint renderbuffer)
449{
Shannon Woods1da3cf62014-06-27 15:32:23 -0400450 if (!ValidFramebufferTarget(target))
451 {
Geoff Langb1196682014-07-23 13:47:29 -0400452 context->recordError(Error(GL_INVALID_ENUM));
453 return false;
Shannon Woods1da3cf62014-06-27 15:32:23 -0400454 }
455
Shannon Woods53a94a82014-06-24 15:20:36 -0400456 gl::Framebuffer *framebuffer = context->getState().getTargetFramebuffer(target);
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500457
Jamie Madill84115c92015-04-23 15:00:07 -0400458 ASSERT(framebuffer);
459 if (framebuffer->id() == 0)
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500460 {
Jamie Madill84115c92015-04-23 15:00:07 -0400461 context->recordError(Error(GL_INVALID_OPERATION, "Cannot change default FBO's attachments"));
Geoff Langb1196682014-07-23 13:47:29 -0400462 return false;
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500463 }
464
Jamie Madillb4472272014-07-03 10:38:55 -0400465 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500466 {
Jamie Madillb4472272014-07-03 10:38:55 -0400467 return false;
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500468 }
469
Jamie Madillab9d82c2014-01-21 16:38:14 -0500470 // [OpenGL ES 2.0.25] Section 4.4.3 page 112
471 // [OpenGL ES 3.0.2] Section 4.4.2 page 201
472 // 'renderbuffer' must be either zero or the name of an existing renderbuffer object of
473 // type 'renderbuffertarget', otherwise an INVALID_OPERATION error is generated.
474 if (renderbuffer != 0)
475 {
476 if (!context->getRenderbuffer(renderbuffer))
477 {
Geoff Langb1196682014-07-23 13:47:29 -0400478 context->recordError(Error(GL_INVALID_OPERATION));
479 return false;
Jamie Madillab9d82c2014-01-21 16:38:14 -0500480 }
481 }
482
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500483 return true;
484}
485
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400486static bool IsPartialBlit(gl::Context *context, const gl::FramebufferAttachment *readBuffer, const gl::FramebufferAttachment *writeBuffer,
Geoff Lang125deab2013-08-09 13:34:16 -0400487 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
488 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1)
489{
490 if (srcX0 != 0 || srcY0 != 0 || dstX0 != 0 || dstY0 != 0 ||
491 dstX1 != writeBuffer->getWidth() || dstY1 != writeBuffer->getHeight() ||
492 srcX1 != readBuffer->getWidth() || srcY1 != readBuffer->getHeight())
493 {
494 return true;
495 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400496 else if (context->getState().isScissorTestEnabled())
Geoff Lang125deab2013-08-09 13:34:16 -0400497 {
Shannon Woods53a94a82014-06-24 15:20:36 -0400498 const Rectangle &scissor = context->getState().getScissor();
Geoff Lang125deab2013-08-09 13:34:16 -0400499
Shannon Woods53a94a82014-06-24 15:20:36 -0400500 return scissor.x > 0 || scissor.y > 0 ||
501 scissor.width < writeBuffer->getWidth() ||
502 scissor.height < writeBuffer->getHeight();
Geoff Lang125deab2013-08-09 13:34:16 -0400503 }
504 else
505 {
506 return false;
507 }
508}
509
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400510bool ValidateBlitFramebufferParameters(gl::Context *context, GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400511 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask,
512 GLenum filter, bool fromAngleExtension)
513{
514 switch (filter)
515 {
516 case GL_NEAREST:
517 break;
518 case GL_LINEAR:
519 if (fromAngleExtension)
520 {
Geoff Langb1196682014-07-23 13:47:29 -0400521 context->recordError(Error(GL_INVALID_ENUM));
522 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400523 }
524 break;
525 default:
Geoff Langb1196682014-07-23 13:47:29 -0400526 context->recordError(Error(GL_INVALID_ENUM));
527 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400528 }
529
530 if ((mask & ~(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)) != 0)
531 {
Geoff Langb1196682014-07-23 13:47:29 -0400532 context->recordError(Error(GL_INVALID_VALUE));
533 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400534 }
535
536 if (mask == 0)
537 {
538 // ES3.0 spec, section 4.3.2 specifies that a mask of zero is valid and no
539 // buffers are copied.
540 return false;
541 }
542
543 if (fromAngleExtension && (srcX1 - srcX0 != dstX1 - dstX0 || srcY1 - srcY0 != dstY1 - dstY0))
544 {
545 ERR("Scaling and flipping in BlitFramebufferANGLE not supported by this implementation.");
Geoff Langb1196682014-07-23 13:47:29 -0400546 context->recordError(Error(GL_INVALID_OPERATION));
547 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400548 }
549
550 // ES3.0 spec, section 4.3.2 states that linear filtering is only available for the
551 // color buffer, leaving only nearest being unfiltered from above
552 if ((mask & ~GL_COLOR_BUFFER_BIT) != 0 && filter != GL_NEAREST)
553 {
Geoff Langb1196682014-07-23 13:47:29 -0400554 context->recordError(Error(GL_INVALID_OPERATION));
555 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400556 }
557
Shannon Woods53a94a82014-06-24 15:20:36 -0400558 if (context->getState().getReadFramebuffer()->id() == context->getState().getDrawFramebuffer()->id())
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400559 {
560 if (fromAngleExtension)
561 {
562 ERR("Blits with the same source and destination framebuffer are not supported by this "
563 "implementation.");
564 }
Geoff Langb1196682014-07-23 13:47:29 -0400565 context->recordError(Error(GL_INVALID_OPERATION));
566 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400567 }
568
Jamie Madille3ef7152015-04-28 16:55:17 +0000569 const gl::Framebuffer *readFramebuffer = context->getState().getReadFramebuffer();
570 const gl::Framebuffer *drawFramebuffer = context->getState().getDrawFramebuffer();
Jamie Madill48faf802014-11-06 15:27:22 -0500571
572 if (!readFramebuffer || !drawFramebuffer)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400573 {
Geoff Langb1196682014-07-23 13:47:29 -0400574 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
575 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400576 }
577
Geoff Lang748f74e2014-12-01 11:25:34 -0500578 if (!readFramebuffer->checkStatus(context->getData()))
Jamie Madill48faf802014-11-06 15:27:22 -0500579 {
580 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
581 return false;
582 }
583
Geoff Lang748f74e2014-12-01 11:25:34 -0500584 if (!drawFramebuffer->checkStatus(context->getData()))
Jamie Madill48faf802014-11-06 15:27:22 -0500585 {
586 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
587 return false;
588 }
589
590 if (drawFramebuffer->getSamples(context->getData()) != 0)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400591 {
Geoff Langb1196682014-07-23 13:47:29 -0400592 context->recordError(Error(GL_INVALID_OPERATION));
593 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400594 }
595
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400596 bool sameBounds = srcX0 == dstX0 && srcY0 == dstY0 && srcX1 == dstX1 && srcY1 == dstY1;
597
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400598 if (mask & GL_COLOR_BUFFER_BIT)
599 {
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400600 const gl::FramebufferAttachment *readColorBuffer = readFramebuffer->getReadColorbuffer();
601 const gl::FramebufferAttachment *drawColorBuffer = drawFramebuffer->getFirstColorbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400602
603 if (readColorBuffer && drawColorBuffer)
604 {
Geoff Langd8a22582014-12-17 15:28:23 -0500605 GLenum readInternalFormat = readColorBuffer->getInternalFormat();
Geoff Lang5d601382014-07-22 15:14:06 -0400606 const InternalFormat &readFormatInfo = GetInternalFormatInfo(readInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400607
Corentin Wallez37c39792015-08-20 14:19:46 -0400608 for (size_t i = 0; i < drawFramebuffer->getNumColorBuffers(); i++)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400609 {
610 if (drawFramebuffer->isEnabledColorAttachment(i))
611 {
Geoff Langd8a22582014-12-17 15:28:23 -0500612 GLenum drawInternalFormat = drawFramebuffer->getColorbuffer(i)->getInternalFormat();
Geoff Lang5d601382014-07-22 15:14:06 -0400613 const InternalFormat &drawFormatInfo = GetInternalFormatInfo(drawInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400614
Geoff Langb2f3d052013-08-13 12:49:27 -0400615 // The GL ES 3.0.2 spec (pg 193) states that:
616 // 1) If the read buffer is fixed point format, the draw buffer must be as well
617 // 2) If the read buffer is an unsigned integer format, the draw buffer must be as well
618 // 3) If the read buffer is a signed integer format, the draw buffer must be as well
Geoff Lang5d601382014-07-22 15:14:06 -0400619 if ( (readFormatInfo.componentType == GL_UNSIGNED_NORMALIZED || readFormatInfo.componentType == GL_SIGNED_NORMALIZED) &&
620 !(drawFormatInfo.componentType == GL_UNSIGNED_NORMALIZED || drawFormatInfo.componentType == GL_SIGNED_NORMALIZED))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400621 {
Geoff Langb1196682014-07-23 13:47:29 -0400622 context->recordError(Error(GL_INVALID_OPERATION));
623 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400624 }
625
Geoff Lang5d601382014-07-22 15:14:06 -0400626 if (readFormatInfo.componentType == GL_UNSIGNED_INT && drawFormatInfo.componentType != GL_UNSIGNED_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400627 {
Geoff Langb1196682014-07-23 13:47:29 -0400628 context->recordError(Error(GL_INVALID_OPERATION));
629 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400630 }
631
Geoff Lang5d601382014-07-22 15:14:06 -0400632 if (readFormatInfo.componentType == GL_INT && drawFormatInfo.componentType != GL_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400633 {
Geoff Langb1196682014-07-23 13:47:29 -0400634 context->recordError(Error(GL_INVALID_OPERATION));
635 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400636 }
637
Geoff Langb2f3d052013-08-13 12:49:27 -0400638 if (readColorBuffer->getSamples() > 0 && (readInternalFormat != drawInternalFormat || !sameBounds))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400639 {
Geoff Langb1196682014-07-23 13:47:29 -0400640 context->recordError(Error(GL_INVALID_OPERATION));
641 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400642 }
643 }
644 }
645
Geoff Lang5d601382014-07-22 15:14:06 -0400646 if ((readFormatInfo.componentType == GL_INT || readFormatInfo.componentType == GL_UNSIGNED_INT) && filter == GL_LINEAR)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400647 {
Geoff Langb1196682014-07-23 13:47:29 -0400648 context->recordError(Error(GL_INVALID_OPERATION));
649 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400650 }
651
652 if (fromAngleExtension)
653 {
Jamie Madillb6bda4a2015-04-20 12:53:26 -0400654 const FramebufferAttachment *readColorAttachment = readFramebuffer->getReadColorbuffer();
Geoff Lang6a1e6b92014-11-06 10:42:45 -0500655 if (!readColorAttachment ||
Jamie Madill8cf4a392015-04-02 11:36:04 -0400656 (!(readColorAttachment->type() == GL_TEXTURE && readColorAttachment->getTextureImageIndex().type == GL_TEXTURE_2D) &&
Geoff Lang6a1e6b92014-11-06 10:42:45 -0500657 readColorAttachment->type() != GL_RENDERBUFFER &&
658 readColorAttachment->type() != GL_FRAMEBUFFER_DEFAULT))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400659 {
Geoff Langb1196682014-07-23 13:47:29 -0400660 context->recordError(Error(GL_INVALID_OPERATION));
661 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400662 }
663
Corentin Wallez37c39792015-08-20 14:19:46 -0400664 for (size_t colorAttachment = 0;
665 colorAttachment < drawFramebuffer->getNumColorBuffers(); ++colorAttachment)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400666 {
667 if (drawFramebuffer->isEnabledColorAttachment(colorAttachment))
668 {
Jamie Madille3ef7152015-04-28 16:55:17 +0000669 const FramebufferAttachment *attachment = drawFramebuffer->getColorbuffer(colorAttachment);
Jamie Madille92a3542014-07-03 10:38:58 -0400670 ASSERT(attachment);
671
Jamie Madill8cf4a392015-04-02 11:36:04 -0400672 if (!(attachment->type() == GL_TEXTURE && attachment->getTextureImageIndex().type == GL_TEXTURE_2D) &&
Geoff Lang6a1e6b92014-11-06 10:42:45 -0500673 attachment->type() != GL_RENDERBUFFER &&
674 attachment->type() != GL_FRAMEBUFFER_DEFAULT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400675 {
Geoff Langb1196682014-07-23 13:47:29 -0400676 context->recordError(Error(GL_INVALID_OPERATION));
677 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400678 }
679
Jamie Madillf8f18f02014-10-02 10:44:17 -0400680 // Return an error if the destination formats do not match
681 if (attachment->getInternalFormat() != readColorBuffer->getInternalFormat())
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400682 {
Geoff Langb1196682014-07-23 13:47:29 -0400683 context->recordError(Error(GL_INVALID_OPERATION));
684 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400685 }
686 }
687 }
Jamie Madill48faf802014-11-06 15:27:22 -0500688
689 int readSamples = readFramebuffer->getSamples(context->getData());
690
691 if (readSamples != 0 && IsPartialBlit(context, readColorBuffer, drawColorBuffer,
692 srcX0, srcY0, srcX1, srcY1,
693 dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400694 {
Geoff Langb1196682014-07-23 13:47:29 -0400695 context->recordError(Error(GL_INVALID_OPERATION));
696 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400697 }
698 }
699 }
700 }
701
Dongseong Hwang44b422c2014-12-09 15:42:01 +0200702 GLenum masks[] = {GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT};
703 GLenum attachments[] = {GL_DEPTH_ATTACHMENT, GL_STENCIL_ATTACHMENT};
704 for (size_t i = 0; i < 2; i++)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400705 {
Dongseong Hwang44b422c2014-12-09 15:42:01 +0200706 if (mask & masks[i])
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400707 {
Jamie Madill2d06b732015-04-20 12:53:28 -0400708 const gl::FramebufferAttachment *readBuffer = readFramebuffer->getAttachment(attachments[i]);
709 const gl::FramebufferAttachment *drawBuffer = drawFramebuffer->getAttachment(attachments[i]);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400710
Dongseong Hwang44b422c2014-12-09 15:42:01 +0200711 if (readBuffer && drawBuffer)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400712 {
Geoff Langd8a22582014-12-17 15:28:23 -0500713 if (readBuffer->getInternalFormat() != drawBuffer->getInternalFormat())
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400714 {
Geoff Langb1196682014-07-23 13:47:29 -0400715 context->recordError(Error(GL_INVALID_OPERATION));
716 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400717 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400718
Dongseong Hwang44b422c2014-12-09 15:42:01 +0200719 if (readBuffer->getSamples() > 0 && !sameBounds)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400720 {
Geoff Langb1196682014-07-23 13:47:29 -0400721 context->recordError(Error(GL_INVALID_OPERATION));
722 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400723 }
Dongseong Hwang44b422c2014-12-09 15:42:01 +0200724
725 if (fromAngleExtension)
726 {
727 if (IsPartialBlit(context, readBuffer, drawBuffer,
728 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
729 {
730 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
731 context->recordError(Error(GL_INVALID_OPERATION)); // only whole-buffer copies are permitted
732 return false;
733 }
734
735 if (readBuffer->getSamples() != 0 || drawBuffer->getSamples() != 0)
736 {
737 context->recordError(Error(GL_INVALID_OPERATION));
738 return false;
739 }
740 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400741 }
742 }
743 }
744
745 return true;
746}
747
Geoff Langb1196682014-07-23 13:47:29 -0400748bool ValidateGetVertexAttribParameters(Context *context, GLenum pname)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400749{
750 switch (pname)
751 {
752 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
753 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
754 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
755 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
756 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
757 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
758 case GL_CURRENT_VERTEX_ATTRIB:
759 return true;
760
761 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR:
762 // Don't verify ES3 context because GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE uses
763 // the same constant.
Geoff Langd4475812015-03-18 10:53:05 -0400764 static_assert(GL_VERTEX_ATTRIB_ARRAY_DIVISOR == GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE,
765 "ANGLE extension enums not equal to GL enums.");
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400766 return true;
767
768 case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
Geoff Langb1196682014-07-23 13:47:29 -0400769 if (context->getClientVersion() < 3)
770 {
771 context->recordError(Error(GL_INVALID_ENUM));
772 return false;
773 }
774 return true;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400775
776 default:
Geoff Langb1196682014-07-23 13:47:29 -0400777 context->recordError(Error(GL_INVALID_ENUM));
778 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400779 }
780}
781
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400782bool ValidateTexParamParameters(gl::Context *context, GLenum pname, GLint param)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400783{
784 switch (pname)
785 {
786 case GL_TEXTURE_WRAP_R:
787 case GL_TEXTURE_SWIZZLE_R:
788 case GL_TEXTURE_SWIZZLE_G:
789 case GL_TEXTURE_SWIZZLE_B:
790 case GL_TEXTURE_SWIZZLE_A:
791 case GL_TEXTURE_BASE_LEVEL:
792 case GL_TEXTURE_MAX_LEVEL:
793 case GL_TEXTURE_COMPARE_MODE:
794 case GL_TEXTURE_COMPARE_FUNC:
795 case GL_TEXTURE_MIN_LOD:
796 case GL_TEXTURE_MAX_LOD:
797 if (context->getClientVersion() < 3)
798 {
Geoff Langb1196682014-07-23 13:47:29 -0400799 context->recordError(Error(GL_INVALID_ENUM));
800 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400801 }
802 break;
803
804 default: break;
805 }
806
807 switch (pname)
808 {
809 case GL_TEXTURE_WRAP_S:
810 case GL_TEXTURE_WRAP_T:
811 case GL_TEXTURE_WRAP_R:
812 switch (param)
813 {
814 case GL_REPEAT:
815 case GL_CLAMP_TO_EDGE:
816 case GL_MIRRORED_REPEAT:
817 return true;
818 default:
Geoff Langb1196682014-07-23 13:47:29 -0400819 context->recordError(Error(GL_INVALID_ENUM));
820 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400821 }
822
823 case GL_TEXTURE_MIN_FILTER:
824 switch (param)
825 {
826 case GL_NEAREST:
827 case GL_LINEAR:
828 case GL_NEAREST_MIPMAP_NEAREST:
829 case GL_LINEAR_MIPMAP_NEAREST:
830 case GL_NEAREST_MIPMAP_LINEAR:
831 case GL_LINEAR_MIPMAP_LINEAR:
832 return true;
833 default:
Geoff Langb1196682014-07-23 13:47:29 -0400834 context->recordError(Error(GL_INVALID_ENUM));
835 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400836 }
837 break;
838
839 case GL_TEXTURE_MAG_FILTER:
840 switch (param)
841 {
842 case GL_NEAREST:
843 case GL_LINEAR:
844 return true;
845 default:
Geoff Langb1196682014-07-23 13:47:29 -0400846 context->recordError(Error(GL_INVALID_ENUM));
847 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400848 }
849 break;
850
851 case GL_TEXTURE_USAGE_ANGLE:
852 switch (param)
853 {
854 case GL_NONE:
855 case GL_FRAMEBUFFER_ATTACHMENT_ANGLE:
856 return true;
857 default:
Geoff Langb1196682014-07-23 13:47:29 -0400858 context->recordError(Error(GL_INVALID_ENUM));
859 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400860 }
861 break;
862
863 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
Geoff Langc0b9ef42014-07-02 10:02:37 -0400864 if (!context->getExtensions().textureFilterAnisotropic)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400865 {
Geoff Langb1196682014-07-23 13:47:29 -0400866 context->recordError(Error(GL_INVALID_ENUM));
867 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400868 }
869
870 // we assume the parameter passed to this validation method is truncated, not rounded
871 if (param < 1)
872 {
Geoff Langb1196682014-07-23 13:47:29 -0400873 context->recordError(Error(GL_INVALID_VALUE));
874 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400875 }
876 return true;
877
878 case GL_TEXTURE_MIN_LOD:
879 case GL_TEXTURE_MAX_LOD:
880 // any value is permissible
881 return true;
882
883 case GL_TEXTURE_COMPARE_MODE:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400884 // Acceptable mode parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400885 switch (param)
886 {
887 case GL_NONE:
888 case GL_COMPARE_REF_TO_TEXTURE:
889 return true;
890 default:
Geoff Langb1196682014-07-23 13:47:29 -0400891 context->recordError(Error(GL_INVALID_ENUM));
892 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400893 }
894 break;
895
896 case GL_TEXTURE_COMPARE_FUNC:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400897 // Acceptable function parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400898 switch (param)
899 {
900 case GL_LEQUAL:
901 case GL_GEQUAL:
902 case GL_LESS:
903 case GL_GREATER:
904 case GL_EQUAL:
905 case GL_NOTEQUAL:
906 case GL_ALWAYS:
907 case GL_NEVER:
908 return true;
909 default:
Geoff Langb1196682014-07-23 13:47:29 -0400910 context->recordError(Error(GL_INVALID_ENUM));
911 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400912 }
913 break;
914
915 case GL_TEXTURE_SWIZZLE_R:
916 case GL_TEXTURE_SWIZZLE_G:
917 case GL_TEXTURE_SWIZZLE_B:
918 case GL_TEXTURE_SWIZZLE_A:
Geoff Langbc90a482013-09-17 16:51:27 -0400919 switch (param)
920 {
921 case GL_RED:
922 case GL_GREEN:
923 case GL_BLUE:
924 case GL_ALPHA:
925 case GL_ZERO:
926 case GL_ONE:
927 return true;
928 default:
Geoff Langb1196682014-07-23 13:47:29 -0400929 context->recordError(Error(GL_INVALID_ENUM));
930 return false;
Geoff Langbc90a482013-09-17 16:51:27 -0400931 }
932 break;
933
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400934 case GL_TEXTURE_BASE_LEVEL:
935 case GL_TEXTURE_MAX_LEVEL:
Nicolas Capens8de68282014-04-04 11:10:27 -0400936 if (param < 0)
937 {
Geoff Langb1196682014-07-23 13:47:29 -0400938 context->recordError(Error(GL_INVALID_VALUE));
939 return false;
Nicolas Capens8de68282014-04-04 11:10:27 -0400940 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400941 return true;
942
943 default:
Geoff Langb1196682014-07-23 13:47:29 -0400944 context->recordError(Error(GL_INVALID_ENUM));
945 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400946 }
947}
948
Geoff Langb1196682014-07-23 13:47:29 -0400949bool ValidateSamplerObjectParameter(gl::Context *context, GLenum pname)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400950{
951 switch (pname)
952 {
953 case GL_TEXTURE_MIN_FILTER:
954 case GL_TEXTURE_MAG_FILTER:
955 case GL_TEXTURE_WRAP_S:
956 case GL_TEXTURE_WRAP_T:
957 case GL_TEXTURE_WRAP_R:
958 case GL_TEXTURE_MIN_LOD:
959 case GL_TEXTURE_MAX_LOD:
960 case GL_TEXTURE_COMPARE_MODE:
961 case GL_TEXTURE_COMPARE_FUNC:
962 return true;
963
964 default:
Geoff Langb1196682014-07-23 13:47:29 -0400965 context->recordError(Error(GL_INVALID_ENUM));
966 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400967 }
968}
969
Jamie Madill26e91952014-03-05 15:01:27 -0500970bool ValidateReadPixelsParameters(gl::Context *context, GLint x, GLint y, GLsizei width, GLsizei height,
971 GLenum format, GLenum type, GLsizei *bufSize, GLvoid *pixels)
972{
Shannon Woods53a94a82014-06-24 15:20:36 -0400973 gl::Framebuffer *framebuffer = context->getState().getReadFramebuffer();
Jamie Madill893ab082014-05-16 16:56:10 -0400974 ASSERT(framebuffer);
Jamie Madill26e91952014-03-05 15:01:27 -0500975
Geoff Lang748f74e2014-12-01 11:25:34 -0500976 if (framebuffer->checkStatus(context->getData()) != GL_FRAMEBUFFER_COMPLETE)
Jamie Madill26e91952014-03-05 15:01:27 -0500977 {
Geoff Langb1196682014-07-23 13:47:29 -0400978 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
979 return false;
Jamie Madill26e91952014-03-05 15:01:27 -0500980 }
981
Jamie Madill48faf802014-11-06 15:27:22 -0500982 if (context->getState().getReadFramebuffer()->id() != 0 &&
983 framebuffer->getSamples(context->getData()) != 0)
Jamie Madill26e91952014-03-05 15:01:27 -0500984 {
Geoff Langb1196682014-07-23 13:47:29 -0400985 context->recordError(Error(GL_INVALID_OPERATION));
986 return false;
Jamie Madill26e91952014-03-05 15:01:27 -0500987 }
988
Geoff Langbce529e2014-12-01 12:48:41 -0500989 const FramebufferAttachment *readBuffer = framebuffer->getReadColorbuffer();
990 if (!readBuffer)
Jamie Madill893ab082014-05-16 16:56:10 -0400991 {
Geoff Langb1196682014-07-23 13:47:29 -0400992 context->recordError(Error(GL_INVALID_OPERATION));
993 return false;
Jamie Madill893ab082014-05-16 16:56:10 -0400994 }
995
Geoff Langbce529e2014-12-01 12:48:41 -0500996 GLenum currentFormat = framebuffer->getImplementationColorReadFormat();
997 GLenum currentType = framebuffer->getImplementationColorReadType();
Geoff Langd8a22582014-12-17 15:28:23 -0500998 GLenum currentInternalFormat = readBuffer->getInternalFormat();
Geoff Lange4a492b2014-06-19 14:14:41 -0400999 GLuint clientVersion = context->getClientVersion();
Jamie Madill26e91952014-03-05 15:01:27 -05001000
Geoff Langbdc9b2f2014-04-16 14:41:54 -04001001 bool validReadFormat = (clientVersion < 3) ? ValidES2ReadFormatType(context, format, type) :
1002 ValidES3ReadFormatType(context, currentInternalFormat, format, type);
Jamie Madill26e91952014-03-05 15:01:27 -05001003
1004 if (!(currentFormat == format && currentType == type) && !validReadFormat)
1005 {
Geoff Langb1196682014-07-23 13:47:29 -04001006 context->recordError(Error(GL_INVALID_OPERATION));
1007 return false;
Jamie Madill26e91952014-03-05 15:01:27 -05001008 }
1009
Geoff Lang5d601382014-07-22 15:14:06 -04001010 GLenum sizedInternalFormat = GetSizedInternalFormat(format, type);
1011 const InternalFormat &sizedFormatInfo = GetInternalFormatInfo(sizedInternalFormat);
Jamie Madill26e91952014-03-05 15:01:27 -05001012
Minmin Gongb8aee3b2015-01-27 14:42:36 -08001013 GLsizei outputPitch = sizedFormatInfo.computeRowPitch(type, width, context->getState().getPackAlignment(), 0);
Jamie Madill26e91952014-03-05 15:01:27 -05001014 // sized query sanity check
1015 if (bufSize)
1016 {
1017 int requiredSize = outputPitch * height;
1018 if (requiredSize > *bufSize)
1019 {
Geoff Langb1196682014-07-23 13:47:29 -04001020 context->recordError(Error(GL_INVALID_OPERATION));
1021 return false;
Jamie Madill26e91952014-03-05 15:01:27 -05001022 }
1023 }
1024
1025 return true;
1026}
1027
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001028bool ValidateBeginQuery(gl::Context *context, GLenum target, GLuint id)
1029{
1030 if (!ValidQueryType(context, target))
1031 {
Geoff Langb1196682014-07-23 13:47:29 -04001032 context->recordError(Error(GL_INVALID_ENUM));
1033 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001034 }
1035
1036 if (id == 0)
1037 {
Geoff Langb1196682014-07-23 13:47:29 -04001038 context->recordError(Error(GL_INVALID_OPERATION));
1039 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001040 }
1041
1042 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
1043 // of zero, if the active query object name for <target> is non-zero (for the
1044 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
1045 // the active query for either target is non-zero), if <id> is the name of an
1046 // existing query object whose type does not match <target>, or if <id> is the
1047 // active query object name for any query type, the error INVALID_OPERATION is
1048 // generated.
1049
1050 // Ensure no other queries are active
1051 // NOTE: If other queries than occlusion are supported, we will need to check
1052 // separately that:
1053 // a) The query ID passed is not the current active query for any target/type
1054 // b) There are no active queries for the requested target (and in the case
1055 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
1056 // no query may be active for either if glBeginQuery targets either.
Shannon Woods53a94a82014-06-24 15:20:36 -04001057 if (context->getState().isQueryActive())
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001058 {
Geoff Langb1196682014-07-23 13:47:29 -04001059 context->recordError(Error(GL_INVALID_OPERATION));
1060 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001061 }
1062
1063 Query *queryObject = context->getQuery(id, true, target);
1064
1065 // check that name was obtained with glGenQueries
1066 if (!queryObject)
1067 {
Geoff Langb1196682014-07-23 13:47:29 -04001068 context->recordError(Error(GL_INVALID_OPERATION));
1069 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001070 }
1071
1072 // check for type mismatch
1073 if (queryObject->getType() != target)
1074 {
Geoff Langb1196682014-07-23 13:47:29 -04001075 context->recordError(Error(GL_INVALID_OPERATION));
1076 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001077 }
1078
1079 return true;
1080}
1081
Jamie Madill45c785d2014-05-13 14:09:34 -04001082bool ValidateEndQuery(gl::Context *context, GLenum target)
1083{
1084 if (!ValidQueryType(context, target))
1085 {
Geoff Langb1196682014-07-23 13:47:29 -04001086 context->recordError(Error(GL_INVALID_ENUM));
1087 return false;
Jamie Madill45c785d2014-05-13 14:09:34 -04001088 }
1089
Shannon Woods53a94a82014-06-24 15:20:36 -04001090 const Query *queryObject = context->getState().getActiveQuery(target);
Jamie Madill45c785d2014-05-13 14:09:34 -04001091
1092 if (queryObject == NULL)
1093 {
Geoff Langb1196682014-07-23 13:47:29 -04001094 context->recordError(Error(GL_INVALID_OPERATION));
1095 return false;
Jamie Madill45c785d2014-05-13 14:09:34 -04001096 }
1097
Jamie Madill45c785d2014-05-13 14:09:34 -04001098 return true;
1099}
1100
Jamie Madill36398922014-05-20 14:51:53 -04001101static bool ValidateUniformCommonBase(gl::Context *context, GLenum targetUniformType,
1102 GLint location, GLsizei count, LinkedUniform **uniformOut)
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001103{
1104 if (count < 0)
1105 {
Geoff Langb1196682014-07-23 13:47:29 -04001106 context->recordError(Error(GL_INVALID_VALUE));
1107 return false;
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001108 }
1109
Geoff Lang7dd2e102014-11-10 15:19:26 -05001110 gl::Program *program = context->getState().getProgram();
1111 if (!program)
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001112 {
Geoff Langb1196682014-07-23 13:47:29 -04001113 context->recordError(Error(GL_INVALID_OPERATION));
1114 return false;
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001115 }
1116
1117 if (location == -1)
1118 {
1119 // Silently ignore the uniform command
1120 return false;
1121 }
1122
Geoff Lang7dd2e102014-11-10 15:19:26 -05001123 if (!program->isValidUniformLocation(location))
Jamie Madill36398922014-05-20 14:51:53 -04001124 {
Geoff Langb1196682014-07-23 13:47:29 -04001125 context->recordError(Error(GL_INVALID_OPERATION));
1126 return false;
Jamie Madill36398922014-05-20 14:51:53 -04001127 }
1128
Geoff Lang7dd2e102014-11-10 15:19:26 -05001129 LinkedUniform *uniform = program->getUniformByLocation(location);
Jamie Madill36398922014-05-20 14:51:53 -04001130
1131 // attempting to write an array to a non-array uniform is an INVALID_OPERATION
Geoff Lang79d059f2015-07-28 15:03:28 -04001132 if (!uniform->isArray() && count > 1)
Jamie Madill36398922014-05-20 14:51:53 -04001133 {
Geoff Langb1196682014-07-23 13:47:29 -04001134 context->recordError(Error(GL_INVALID_OPERATION));
1135 return false;
Jamie Madill36398922014-05-20 14:51:53 -04001136 }
1137
1138 *uniformOut = uniform;
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001139 return true;
1140}
1141
Jamie Madillaa981bd2014-05-20 10:55:55 -04001142bool ValidateUniform(gl::Context *context, GLenum uniformType, GLint location, GLsizei count)
1143{
1144 // Check for ES3 uniform entry points
Jamie Madillf2575982014-06-25 16:04:54 -04001145 if (VariableComponentType(uniformType) == GL_UNSIGNED_INT && context->getClientVersion() < 3)
Jamie Madillaa981bd2014-05-20 10:55:55 -04001146 {
Geoff Langb1196682014-07-23 13:47:29 -04001147 context->recordError(Error(GL_INVALID_OPERATION));
1148 return false;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001149 }
1150
Jamie Madill36398922014-05-20 14:51:53 -04001151 LinkedUniform *uniform = NULL;
1152 if (!ValidateUniformCommonBase(context, uniformType, location, count, &uniform))
1153 {
1154 return false;
1155 }
1156
Jamie Madillf2575982014-06-25 16:04:54 -04001157 GLenum targetBoolType = VariableBoolVectorType(uniformType);
Geoff Lang2ec386b2014-12-03 14:44:38 -05001158 bool samplerUniformCheck = (IsSamplerType(uniform->type) && uniformType == GL_INT);
Jamie Madill36398922014-05-20 14:51:53 -04001159 if (!samplerUniformCheck && uniformType != uniform->type && targetBoolType != uniform->type)
1160 {
Geoff Langb1196682014-07-23 13:47:29 -04001161 context->recordError(Error(GL_INVALID_OPERATION));
1162 return false;
Jamie Madill36398922014-05-20 14:51:53 -04001163 }
1164
1165 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001166}
1167
1168bool ValidateUniformMatrix(gl::Context *context, GLenum matrixType, GLint location, GLsizei count,
1169 GLboolean transpose)
1170{
1171 // Check for ES3 uniform entry points
1172 int rows = VariableRowCount(matrixType);
1173 int cols = VariableColumnCount(matrixType);
1174 if (rows != cols && context->getClientVersion() < 3)
1175 {
Geoff Langb1196682014-07-23 13:47:29 -04001176 context->recordError(Error(GL_INVALID_OPERATION));
1177 return false;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001178 }
1179
1180 if (transpose != GL_FALSE && context->getClientVersion() < 3)
1181 {
Geoff Langb1196682014-07-23 13:47:29 -04001182 context->recordError(Error(GL_INVALID_VALUE));
1183 return false;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001184 }
1185
Jamie Madill36398922014-05-20 14:51:53 -04001186 LinkedUniform *uniform = NULL;
1187 if (!ValidateUniformCommonBase(context, matrixType, location, count, &uniform))
1188 {
1189 return false;
1190 }
1191
1192 if (uniform->type != matrixType)
1193 {
Geoff Langb1196682014-07-23 13:47:29 -04001194 context->recordError(Error(GL_INVALID_OPERATION));
1195 return false;
Jamie Madill36398922014-05-20 14:51:53 -04001196 }
1197
1198 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001199}
1200
Jamie Madill893ab082014-05-16 16:56:10 -04001201bool ValidateStateQuery(gl::Context *context, GLenum pname, GLenum *nativeType, unsigned int *numParams)
1202{
1203 if (!context->getQueryParameterInfo(pname, nativeType, numParams))
1204 {
Geoff Langb1196682014-07-23 13:47:29 -04001205 context->recordError(Error(GL_INVALID_ENUM));
1206 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001207 }
1208
Jamie Madill0af26e12015-03-05 19:54:33 -05001209 const Caps &caps = context->getCaps();
1210
Jamie Madill893ab082014-05-16 16:56:10 -04001211 if (pname >= GL_DRAW_BUFFER0 && pname <= GL_DRAW_BUFFER15)
1212 {
1213 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0);
1214
Jamie Madill0af26e12015-03-05 19:54:33 -05001215 if (colorAttachment >= caps.maxDrawBuffers)
Jamie Madill893ab082014-05-16 16:56:10 -04001216 {
Geoff Langb1196682014-07-23 13:47:29 -04001217 context->recordError(Error(GL_INVALID_OPERATION));
1218 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001219 }
1220 }
1221
1222 switch (pname)
1223 {
1224 case GL_TEXTURE_BINDING_2D:
1225 case GL_TEXTURE_BINDING_CUBE_MAP:
1226 case GL_TEXTURE_BINDING_3D:
1227 case GL_TEXTURE_BINDING_2D_ARRAY:
Jamie Madill0af26e12015-03-05 19:54:33 -05001228 if (context->getState().getActiveSampler() >= caps.maxCombinedTextureImageUnits)
Jamie Madill893ab082014-05-16 16:56:10 -04001229 {
Geoff Langb1196682014-07-23 13:47:29 -04001230 context->recordError(Error(GL_INVALID_OPERATION));
1231 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001232 }
1233 break;
1234
1235 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1236 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1237 {
Shannon Woods53a94a82014-06-24 15:20:36 -04001238 Framebuffer *framebuffer = context->getState().getReadFramebuffer();
Jamie Madill893ab082014-05-16 16:56:10 -04001239 ASSERT(framebuffer);
Geoff Lang748f74e2014-12-01 11:25:34 -05001240 if (framebuffer->checkStatus(context->getData()) != GL_FRAMEBUFFER_COMPLETE)
Jamie Madill893ab082014-05-16 16:56:10 -04001241 {
Geoff Langb1196682014-07-23 13:47:29 -04001242 context->recordError(Error(GL_INVALID_OPERATION));
1243 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001244 }
1245
Jamie Madillb6bda4a2015-04-20 12:53:26 -04001246 const FramebufferAttachment *attachment = framebuffer->getReadColorbuffer();
Jamie Madill3c7fa222014-06-05 13:08:51 -04001247 if (!attachment)
Jamie Madill893ab082014-05-16 16:56:10 -04001248 {
Geoff Langb1196682014-07-23 13:47:29 -04001249 context->recordError(Error(GL_INVALID_OPERATION));
1250 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001251 }
1252 }
1253 break;
1254
1255 default:
1256 break;
1257 }
1258
1259 // pname is valid, but there are no parameters to return
1260 if (numParams == 0)
1261 {
1262 return false;
1263 }
1264
1265 return true;
1266}
1267
Geoff Lang831b1952015-05-05 11:02:27 -04001268bool ValidateCopyTexImageParametersBase(gl::Context *context, GLenum target, GLint level, GLenum internalformat, bool isSubImage,
Jamie Madill560a8d82014-05-21 13:06:20 -04001269 GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height,
1270 GLint border, GLenum *textureFormatOut)
1271{
1272
1273 if (!ValidTexture2DDestinationTarget(context, target))
1274 {
Geoff Langb1196682014-07-23 13:47:29 -04001275 context->recordError(Error(GL_INVALID_ENUM));
1276 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001277 }
1278
1279 if (level < 0 || xoffset < 0 || yoffset < 0 || zoffset < 0 || width < 0 || height < 0)
1280 {
Geoff Langb1196682014-07-23 13:47:29 -04001281 context->recordError(Error(GL_INVALID_VALUE));
1282 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001283 }
1284
1285 if (std::numeric_limits<GLsizei>::max() - xoffset < width || std::numeric_limits<GLsizei>::max() - yoffset < height)
1286 {
Geoff Langb1196682014-07-23 13:47:29 -04001287 context->recordError(Error(GL_INVALID_VALUE));
1288 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001289 }
1290
1291 if (border != 0)
1292 {
Geoff Langb1196682014-07-23 13:47:29 -04001293 context->recordError(Error(GL_INVALID_VALUE));
1294 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001295 }
1296
1297 if (!ValidMipLevel(context, target, level))
1298 {
Geoff Langb1196682014-07-23 13:47:29 -04001299 context->recordError(Error(GL_INVALID_VALUE));
1300 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001301 }
1302
Shannon Woods53a94a82014-06-24 15:20:36 -04001303 gl::Framebuffer *framebuffer = context->getState().getReadFramebuffer();
Geoff Lang748f74e2014-12-01 11:25:34 -05001304 if (framebuffer->checkStatus(context->getData()) != GL_FRAMEBUFFER_COMPLETE)
Jamie Madill560a8d82014-05-21 13:06:20 -04001305 {
Geoff Langb1196682014-07-23 13:47:29 -04001306 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
1307 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001308 }
1309
Jamie Madill48faf802014-11-06 15:27:22 -05001310 if (context->getState().getReadFramebuffer()->id() != 0 && framebuffer->getSamples(context->getData()) != 0)
Jamie Madill560a8d82014-05-21 13:06:20 -04001311 {
Geoff Langb1196682014-07-23 13:47:29 -04001312 context->recordError(Error(GL_INVALID_OPERATION));
1313 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001314 }
1315
Geoff Langaae65a42014-05-26 12:43:44 -04001316 const gl::Caps &caps = context->getCaps();
1317
Geoff Langaae65a42014-05-26 12:43:44 -04001318 GLuint maxDimension = 0;
Jamie Madill560a8d82014-05-21 13:06:20 -04001319 switch (target)
1320 {
1321 case GL_TEXTURE_2D:
Geoff Langa9be0dc2014-12-17 12:34:40 -05001322 maxDimension = caps.max2DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001323 break;
1324
1325 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1326 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1327 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1328 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1329 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1330 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
Geoff Langa9be0dc2014-12-17 12:34:40 -05001331 maxDimension = caps.maxCubeMapTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001332 break;
1333
1334 case GL_TEXTURE_2D_ARRAY:
Geoff Langa9be0dc2014-12-17 12:34:40 -05001335 maxDimension = caps.max2DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001336 break;
1337
1338 case GL_TEXTURE_3D:
Geoff Langa9be0dc2014-12-17 12:34:40 -05001339 maxDimension = caps.max3DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001340 break;
1341
1342 default:
Geoff Langb1196682014-07-23 13:47:29 -04001343 context->recordError(Error(GL_INVALID_ENUM));
1344 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001345 }
1346
Geoff Lang691e58c2014-12-19 17:03:25 -05001347 gl::Texture *texture = context->getTargetTexture(IsCubeMapTextureTarget(target) ? GL_TEXTURE_CUBE_MAP : target);
Jamie Madill560a8d82014-05-21 13:06:20 -04001348 if (!texture)
1349 {
Geoff Langb1196682014-07-23 13:47:29 -04001350 context->recordError(Error(GL_INVALID_OPERATION));
1351 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001352 }
1353
1354 if (texture->isImmutable() && !isSubImage)
1355 {
Geoff Langb1196682014-07-23 13:47:29 -04001356 context->recordError(Error(GL_INVALID_OPERATION));
1357 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001358 }
1359
Geoff Lang5d601382014-07-22 15:14:06 -04001360 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalformat);
1361
1362 if (formatInfo.depthBits > 0)
Jamie Madill560a8d82014-05-21 13:06:20 -04001363 {
Geoff Langb1196682014-07-23 13:47:29 -04001364 context->recordError(Error(GL_INVALID_OPERATION));
1365 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001366 }
1367
Geoff Langa9be0dc2014-12-17 12:34:40 -05001368 if (formatInfo.compressed && !ValidCompressedImageSize(context, internalformat, width, height))
Jamie Madill560a8d82014-05-21 13:06:20 -04001369 {
Geoff Langa9be0dc2014-12-17 12:34:40 -05001370 context->recordError(Error(GL_INVALID_OPERATION));
1371 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001372 }
1373
1374 if (isSubImage)
1375 {
Geoff Langa9be0dc2014-12-17 12:34:40 -05001376 if (static_cast<size_t>(xoffset + width) > texture->getWidth(target, level) ||
1377 static_cast<size_t>(yoffset + height) > texture->getHeight(target, level) ||
1378 static_cast<size_t>(zoffset) >= texture->getDepth(target, level))
Jamie Madill560a8d82014-05-21 13:06:20 -04001379 {
Geoff Langb1196682014-07-23 13:47:29 -04001380 context->recordError(Error(GL_INVALID_VALUE));
1381 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001382 }
1383 }
Jamie Madill6f38f822014-06-06 17:12:20 -04001384 else
1385 {
Geoff Lang691e58c2014-12-19 17:03:25 -05001386 if (IsCubeMapTextureTarget(target) && width != height)
Jamie Madill6f38f822014-06-06 17:12:20 -04001387 {
Geoff Langb1196682014-07-23 13:47:29 -04001388 context->recordError(Error(GL_INVALID_VALUE));
1389 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04001390 }
1391
Geoff Lang5d601382014-07-22 15:14:06 -04001392 if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions()))
Jamie Madill6f38f822014-06-06 17:12:20 -04001393 {
Geoff Langb1196682014-07-23 13:47:29 -04001394 context->recordError(Error(GL_INVALID_ENUM));
1395 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04001396 }
1397
1398 int maxLevelDimension = (maxDimension >> level);
1399 if (static_cast<int>(width) > maxLevelDimension || static_cast<int>(height) > maxLevelDimension)
1400 {
Geoff Langb1196682014-07-23 13:47:29 -04001401 context->recordError(Error(GL_INVALID_VALUE));
1402 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04001403 }
1404 }
Jamie Madill560a8d82014-05-21 13:06:20 -04001405
Geoff Langa9be0dc2014-12-17 12:34:40 -05001406 *textureFormatOut = texture->getInternalFormat(target, level);
Jamie Madill560a8d82014-05-21 13:06:20 -04001407 return true;
1408}
1409
Corentin Wallez18a2fb32015-08-10 12:58:14 -07001410static bool ValidateDrawBase(Context *context, GLenum mode, GLsizei count, GLsizei primcount)
Jamie Madill250d33f2014-06-06 17:09:03 -04001411{
Jamie Madill1aeb1312014-06-20 13:21:25 -04001412 switch (mode)
1413 {
1414 case GL_POINTS:
1415 case GL_LINES:
1416 case GL_LINE_LOOP:
1417 case GL_LINE_STRIP:
1418 case GL_TRIANGLES:
1419 case GL_TRIANGLE_STRIP:
1420 case GL_TRIANGLE_FAN:
1421 break;
1422 default:
Geoff Langb1196682014-07-23 13:47:29 -04001423 context->recordError(Error(GL_INVALID_ENUM));
1424 return false;
Jamie Madill1aeb1312014-06-20 13:21:25 -04001425 }
1426
Jamie Madill250d33f2014-06-06 17:09:03 -04001427 if (count < 0)
1428 {
Geoff Langb1196682014-07-23 13:47:29 -04001429 context->recordError(Error(GL_INVALID_VALUE));
1430 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001431 }
1432
Geoff Langb1196682014-07-23 13:47:29 -04001433 const State &state = context->getState();
1434
Jamie Madill250d33f2014-06-06 17:09:03 -04001435 // Check for mapped buffers
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001436 if (state.hasMappedBuffer(GL_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001437 {
Geoff Langb1196682014-07-23 13:47:29 -04001438 context->recordError(Error(GL_INVALID_OPERATION));
1439 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001440 }
1441
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001442 const gl::DepthStencilState &depthStencilState = state.getDepthStencilState();
Jamie Madillac528012014-06-20 13:21:23 -04001443 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001444 state.getStencilRef() != state.getStencilBackRef() ||
Jamie Madillac528012014-06-20 13:21:23 -04001445 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
1446 {
1447 // Note: these separate values are not supported in WebGL, due to D3D's limitations.
1448 // See Section 6.10 of the WebGL 1.0 spec
1449 ERR("This ANGLE implementation does not support separate front/back stencil "
1450 "writemasks, reference values, or stencil mask values.");
Geoff Langb1196682014-07-23 13:47:29 -04001451 context->recordError(Error(GL_INVALID_OPERATION));
1452 return false;
Jamie Madillac528012014-06-20 13:21:23 -04001453 }
1454
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001455 const gl::Framebuffer *fbo = state.getDrawFramebuffer();
Geoff Lang748f74e2014-12-01 11:25:34 -05001456 if (!fbo || fbo->checkStatus(context->getData()) != GL_FRAMEBUFFER_COMPLETE)
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001457 {
Geoff Langb1196682014-07-23 13:47:29 -04001458 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
1459 return false;
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001460 }
1461
Geoff Lang7dd2e102014-11-10 15:19:26 -05001462 gl::Program *program = state.getProgram();
1463 if (!program)
Jamie Madilld4cfa572014-07-08 10:00:32 -04001464 {
Geoff Langb1196682014-07-23 13:47:29 -04001465 context->recordError(Error(GL_INVALID_OPERATION));
1466 return false;
Jamie Madilld4cfa572014-07-08 10:00:32 -04001467 }
1468
Geoff Lang7dd2e102014-11-10 15:19:26 -05001469 if (!program->validateSamplers(NULL, context->getCaps()))
Jamie Madilld4cfa572014-07-08 10:00:32 -04001470 {
Geoff Langb1196682014-07-23 13:47:29 -04001471 context->recordError(Error(GL_INVALID_OPERATION));
1472 return false;
Jamie Madilld4cfa572014-07-08 10:00:32 -04001473 }
1474
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001475 // Uniform buffer validation
1476 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < program->getActiveUniformBlockCount(); uniformBlockIndex++)
1477 {
1478 const gl::UniformBlock *uniformBlock = program->getUniformBlockByIndex(uniformBlockIndex);
1479 GLuint blockBinding = program->getUniformBlockBinding(uniformBlockIndex);
1480 const gl::Buffer *uniformBuffer = state.getIndexedUniformBuffer(blockBinding);
1481
1482 if (!uniformBuffer)
1483 {
1484 // undefined behaviour
1485 context->recordError(Error(GL_INVALID_OPERATION, "It is undefined behaviour to have a used but unbound uniform buffer."));
1486 return false;
1487 }
1488
1489 size_t uniformBufferSize = state.getIndexedUniformBufferSize(blockBinding);
1490
1491 if (uniformBufferSize == 0)
1492 {
1493 // Bind the whole buffer.
Minmin Gong794e0002015-04-07 18:31:54 -07001494 uniformBufferSize = static_cast<size_t>(uniformBuffer->getSize());
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001495 }
1496
1497 if (uniformBufferSize < uniformBlock->dataSize)
1498 {
1499 // undefined behaviour
1500 context->recordError(Error(GL_INVALID_OPERATION, "It is undefined behaviour to use a uniform buffer that is too small."));
1501 return false;
1502 }
1503 }
1504
Jamie Madill250d33f2014-06-06 17:09:03 -04001505 // No-op if zero count
1506 return (count > 0);
1507}
1508
Geoff Langb1196682014-07-23 13:47:29 -04001509bool ValidateDrawArrays(Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
Jamie Madill250d33f2014-06-06 17:09:03 -04001510{
Jamie Madillfd716582014-06-06 17:09:04 -04001511 if (first < 0)
Jamie Madill250d33f2014-06-06 17:09:03 -04001512 {
Geoff Langb1196682014-07-23 13:47:29 -04001513 context->recordError(Error(GL_INVALID_VALUE));
1514 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001515 }
1516
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001517 const State &state = context->getState();
1518 gl::TransformFeedback *curTransformFeedback = state.getCurrentTransformFeedback();
Geoff Langbb0a0bb2015-03-27 12:16:57 -04001519 if (curTransformFeedback && curTransformFeedback->isActive() && !curTransformFeedback->isPaused() &&
1520 curTransformFeedback->getPrimitiveMode() != mode)
Jamie Madillfd716582014-06-06 17:09:04 -04001521 {
1522 // It is an invalid operation to call DrawArrays or DrawArraysInstanced with a draw mode
1523 // that does not match the current transform feedback object's draw mode (if transform feedback
1524 // is active), (3.0.2, section 2.14, pg 86)
Geoff Langb1196682014-07-23 13:47:29 -04001525 context->recordError(Error(GL_INVALID_OPERATION));
1526 return false;
Jamie Madillfd716582014-06-06 17:09:04 -04001527 }
1528
Corentin Wallez18a2fb32015-08-10 12:58:14 -07001529 if (!ValidateDrawBase(context, mode, count, primcount))
1530 {
1531 return false;
1532 }
1533
1534 if (!ValidateDrawAttribs(context, primcount, count))
Jamie Madillfd716582014-06-06 17:09:04 -04001535 {
1536 return false;
1537 }
1538
1539 return true;
1540}
1541
Geoff Langb1196682014-07-23 13:47:29 -04001542bool ValidateDrawArraysInstanced(Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
Jamie Madillfd716582014-06-06 17:09:04 -04001543{
1544 if (primcount < 0)
1545 {
Geoff Langb1196682014-07-23 13:47:29 -04001546 context->recordError(Error(GL_INVALID_VALUE));
1547 return false;
Jamie Madillfd716582014-06-06 17:09:04 -04001548 }
1549
Jamie Madill2b976812014-08-25 15:47:49 -04001550 if (!ValidateDrawArrays(context, mode, first, count, primcount))
Jamie Madillfd716582014-06-06 17:09:04 -04001551 {
1552 return false;
1553 }
1554
1555 // No-op if zero primitive count
1556 return (primcount > 0);
1557}
1558
Geoff Lang87a93302014-09-16 13:29:43 -04001559static bool ValidateDrawInstancedANGLE(Context *context)
1560{
1561 // Verify there is at least one active attribute with a divisor of zero
1562 const gl::State& state = context->getState();
1563
Geoff Lang7dd2e102014-11-10 15:19:26 -05001564 gl::Program *program = state.getProgram();
Geoff Lang87a93302014-09-16 13:29:43 -04001565
1566 const VertexArray *vao = state.getVertexArray();
1567 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
1568 {
1569 const VertexAttribute &attrib = vao->getVertexAttribute(attributeIndex);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001570 bool active = (program->getSemanticIndex(attributeIndex) != -1);
Geoff Lang87a93302014-09-16 13:29:43 -04001571 if (active && attrib.divisor == 0)
1572 {
1573 return true;
1574 }
1575 }
1576
1577 context->recordError(Error(GL_INVALID_OPERATION, "ANGLE_instanced_arrays requires that at least one active attribute"
1578 "has a divisor of zero."));
1579 return false;
1580}
1581
1582bool ValidateDrawArraysInstancedANGLE(Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
1583{
1584 if (!ValidateDrawInstancedANGLE(context))
1585 {
1586 return false;
1587 }
1588
1589 return ValidateDrawArraysInstanced(context, mode, first, count, primcount);
1590}
1591
Geoff Langb1196682014-07-23 13:47:29 -04001592bool ValidateDrawElements(Context *context, GLenum mode, GLsizei count, GLenum type,
Geoff Lang831b1952015-05-05 11:02:27 -04001593 const GLvoid *indices, GLsizei primcount, RangeUI *indexRangeOut)
Jamie Madillfd716582014-06-06 17:09:04 -04001594{
Jamie Madill250d33f2014-06-06 17:09:03 -04001595 switch (type)
1596 {
1597 case GL_UNSIGNED_BYTE:
1598 case GL_UNSIGNED_SHORT:
1599 break;
1600 case GL_UNSIGNED_INT:
Geoff Langc0b9ef42014-07-02 10:02:37 -04001601 if (!context->getExtensions().elementIndexUint)
Jamie Madill250d33f2014-06-06 17:09:03 -04001602 {
Geoff Langb1196682014-07-23 13:47:29 -04001603 context->recordError(Error(GL_INVALID_ENUM));
1604 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001605 }
1606 break;
1607 default:
Geoff Langb1196682014-07-23 13:47:29 -04001608 context->recordError(Error(GL_INVALID_ENUM));
1609 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001610 }
1611
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001612 const State &state = context->getState();
1613
1614 gl::TransformFeedback *curTransformFeedback = state.getCurrentTransformFeedback();
Geoff Langbb0a0bb2015-03-27 12:16:57 -04001615 if (curTransformFeedback && curTransformFeedback->isActive() && !curTransformFeedback->isPaused())
Jamie Madill250d33f2014-06-06 17:09:03 -04001616 {
1617 // It is an invalid operation to call DrawElements, DrawRangeElements or DrawElementsInstanced
1618 // while transform feedback is active, (3.0.2, section 2.14, pg 86)
Geoff Langb1196682014-07-23 13:47:29 -04001619 context->recordError(Error(GL_INVALID_OPERATION));
1620 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001621 }
1622
1623 // Check for mapped buffers
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001624 if (state.hasMappedBuffer(GL_ELEMENT_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001625 {
Geoff Langb1196682014-07-23 13:47:29 -04001626 context->recordError(Error(GL_INVALID_OPERATION));
1627 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001628 }
1629
Jamie Madill2b976812014-08-25 15:47:49 -04001630 const gl::VertexArray *vao = state.getVertexArray();
Jamie Madill8e344942015-07-09 14:22:07 -04001631 gl::Buffer *elementArrayBuffer = vao->getElementArrayBuffer().get();
Jamie Madill2b976812014-08-25 15:47:49 -04001632 if (!indices && !elementArrayBuffer)
Jamie Madilld4cfa572014-07-08 10:00:32 -04001633 {
Geoff Langb1196682014-07-23 13:47:29 -04001634 context->recordError(Error(GL_INVALID_OPERATION));
1635 return false;
Jamie Madilld4cfa572014-07-08 10:00:32 -04001636 }
1637
Jamie Madillae3000b2014-08-25 15:47:51 -04001638 if (elementArrayBuffer)
1639 {
1640 const gl::Type &typeInfo = gl::GetTypeInfo(type);
1641
1642 GLint64 offset = reinterpret_cast<GLint64>(indices);
1643 GLint64 byteCount = static_cast<GLint64>(typeInfo.bytes) * static_cast<GLint64>(count)+offset;
1644
1645 // check for integer overflows
1646 if (static_cast<GLuint>(count) > (std::numeric_limits<GLuint>::max() / typeInfo.bytes) ||
1647 byteCount > static_cast<GLint64>(std::numeric_limits<GLuint>::max()))
1648 {
Geoff Langb1196682014-07-23 13:47:29 -04001649 context->recordError(Error(GL_OUT_OF_MEMORY));
1650 return false;
Jamie Madillae3000b2014-08-25 15:47:51 -04001651 }
1652
1653 // Check for reading past the end of the bound buffer object
1654 if (byteCount > elementArrayBuffer->getSize())
1655 {
Geoff Langb1196682014-07-23 13:47:29 -04001656 context->recordError(Error(GL_INVALID_OPERATION));
1657 return false;
Jamie Madillae3000b2014-08-25 15:47:51 -04001658 }
1659 }
1660 else if (!indices)
1661 {
1662 // Catch this programming error here
Geoff Langb1196682014-07-23 13:47:29 -04001663 context->recordError(Error(GL_INVALID_OPERATION));
1664 return false;
Jamie Madillae3000b2014-08-25 15:47:51 -04001665 }
1666
Corentin Wallez18a2fb32015-08-10 12:58:14 -07001667 if (!ValidateDrawBase(context, mode, count, primcount))
1668 {
1669 return false;
1670 }
1671
Jamie Madill2b976812014-08-25 15:47:49 -04001672 // Use max index to validate if our vertex buffers are large enough for the pull.
1673 // TODO: offer fast path, with disabled index validation.
1674 // TODO: also disable index checking on back-ends that are robust to out-of-range accesses.
1675 if (elementArrayBuffer)
1676 {
Jacek Cabana5521de2014-10-01 17:23:46 +02001677 uintptr_t offset = reinterpret_cast<uintptr_t>(indices);
Geoff Lang520c4ae2015-05-05 13:12:36 -04001678 Error error = elementArrayBuffer->getIndexRange(type, static_cast<size_t>(offset), count, indexRangeOut);
1679 if (error.isError())
Jamie Madill2b976812014-08-25 15:47:49 -04001680 {
Geoff Lang520c4ae2015-05-05 13:12:36 -04001681 context->recordError(error);
1682 return false;
Jamie Madill2b976812014-08-25 15:47:49 -04001683 }
1684 }
1685 else
1686 {
Geoff Lang831b1952015-05-05 11:02:27 -04001687 *indexRangeOut = ComputeIndexRange(type, indices, count);
Jamie Madill2b976812014-08-25 15:47:49 -04001688 }
1689
Corentin Wallez18a2fb32015-08-10 12:58:14 -07001690 if (!ValidateDrawAttribs(context, primcount, static_cast<GLsizei>(indexRangeOut->end)))
Jamie Madillfd716582014-06-06 17:09:04 -04001691 {
1692 return false;
1693 }
1694
1695 return true;
1696}
1697
Geoff Langb1196682014-07-23 13:47:29 -04001698bool ValidateDrawElementsInstanced(Context *context,
Jamie Madill2b976812014-08-25 15:47:49 -04001699 GLenum mode, GLsizei count, GLenum type,
1700 const GLvoid *indices, GLsizei primcount,
Geoff Lang831b1952015-05-05 11:02:27 -04001701 RangeUI *indexRangeOut)
Jamie Madillfd716582014-06-06 17:09:04 -04001702{
1703 if (primcount < 0)
1704 {
Geoff Langb1196682014-07-23 13:47:29 -04001705 context->recordError(Error(GL_INVALID_VALUE));
1706 return false;
Jamie Madillfd716582014-06-06 17:09:04 -04001707 }
1708
Jamie Madill2b976812014-08-25 15:47:49 -04001709 if (!ValidateDrawElements(context, mode, count, type, indices, primcount, indexRangeOut))
Jamie Madillfd716582014-06-06 17:09:04 -04001710 {
1711 return false;
1712 }
1713
1714 // No-op zero primitive count
1715 return (primcount > 0);
Jamie Madill250d33f2014-06-06 17:09:03 -04001716}
1717
Geoff Lang87a93302014-09-16 13:29:43 -04001718bool ValidateDrawElementsInstancedANGLE(Context *context, GLenum mode, GLsizei count, GLenum type,
Geoff Lang831b1952015-05-05 11:02:27 -04001719 const GLvoid *indices, GLsizei primcount, RangeUI *indexRangeOut)
Geoff Lang87a93302014-09-16 13:29:43 -04001720{
1721 if (!ValidateDrawInstancedANGLE(context))
1722 {
1723 return false;
1724 }
1725
1726 return ValidateDrawElementsInstanced(context, mode, count, type, indices, primcount, indexRangeOut);
1727}
1728
Geoff Langb1196682014-07-23 13:47:29 -04001729bool ValidateFramebufferTextureBase(Context *context, GLenum target, GLenum attachment,
Jamie Madill55ec3b12014-07-03 10:38:57 -04001730 GLuint texture, GLint level)
Jamie Madill570f7c82014-07-03 10:38:54 -04001731{
Jamie Madill55ec3b12014-07-03 10:38:57 -04001732 if (!ValidFramebufferTarget(target))
1733 {
Geoff Langb1196682014-07-23 13:47:29 -04001734 context->recordError(Error(GL_INVALID_ENUM));
1735 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001736 }
1737
1738 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill570f7c82014-07-03 10:38:54 -04001739 {
1740 return false;
1741 }
1742
Jamie Madill55ec3b12014-07-03 10:38:57 -04001743 if (texture != 0)
1744 {
1745 gl::Texture *tex = context->getTexture(texture);
1746
1747 if (tex == NULL)
1748 {
Geoff Langb1196682014-07-23 13:47:29 -04001749 context->recordError(Error(GL_INVALID_OPERATION));
1750 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001751 }
1752
1753 if (level < 0)
1754 {
Geoff Langb1196682014-07-23 13:47:29 -04001755 context->recordError(Error(GL_INVALID_VALUE));
1756 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001757 }
1758 }
1759
Shannon Woods53a94a82014-06-24 15:20:36 -04001760 const gl::Framebuffer *framebuffer = context->getState().getTargetFramebuffer(target);
Jamie Madill84115c92015-04-23 15:00:07 -04001761 ASSERT(framebuffer);
Jamie Madill55ec3b12014-07-03 10:38:57 -04001762
Jamie Madill84115c92015-04-23 15:00:07 -04001763 if (framebuffer->id() == 0)
Jamie Madill55ec3b12014-07-03 10:38:57 -04001764 {
Jamie Madill84115c92015-04-23 15:00:07 -04001765 context->recordError(Error(GL_INVALID_OPERATION, "Cannot change default FBO's attachments"));
Geoff Langb1196682014-07-23 13:47:29 -04001766 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001767 }
1768
1769 return true;
1770}
1771
Geoff Langb1196682014-07-23 13:47:29 -04001772bool ValidateFramebufferTexture2D(Context *context, GLenum target, GLenum attachment,
Jamie Madill55ec3b12014-07-03 10:38:57 -04001773 GLenum textarget, GLuint texture, GLint level)
1774{
Geoff Lang95663912015-04-02 15:54:45 -04001775 // Attachments are required to be bound to level 0 without ES3 or the GL_OES_fbo_render_mipmap extension
1776 if (context->getClientVersion() < 3 && !context->getExtensions().fboRenderMipmap && level != 0)
Jamie Madill55ec3b12014-07-03 10:38:57 -04001777 {
Geoff Langb1196682014-07-23 13:47:29 -04001778 context->recordError(Error(GL_INVALID_VALUE));
1779 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001780 }
1781
1782 if (!ValidateFramebufferTextureBase(context, target, attachment, texture, level))
Jamie Madill570f7c82014-07-03 10:38:54 -04001783 {
1784 return false;
1785 }
1786
Jamie Madill55ec3b12014-07-03 10:38:57 -04001787 if (texture != 0)
1788 {
1789 gl::Texture *tex = context->getTexture(texture);
1790 ASSERT(tex);
1791
Jamie Madill2a6564e2014-07-11 09:53:19 -04001792 const gl::Caps &caps = context->getCaps();
1793
Jamie Madill55ec3b12014-07-03 10:38:57 -04001794 switch (textarget)
1795 {
1796 case GL_TEXTURE_2D:
1797 {
Jamie Madill2a6564e2014-07-11 09:53:19 -04001798 if (level > gl::log2(caps.max2DTextureSize))
Jamie Madill55ec3b12014-07-03 10:38:57 -04001799 {
Geoff Langb1196682014-07-23 13:47:29 -04001800 context->recordError(Error(GL_INVALID_VALUE));
1801 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001802 }
1803 if (tex->getTarget() != GL_TEXTURE_2D)
1804 {
Geoff Langb1196682014-07-23 13:47:29 -04001805 context->recordError(Error(GL_INVALID_OPERATION));
1806 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001807 }
Jamie Madill55ec3b12014-07-03 10:38:57 -04001808 }
1809 break;
1810
1811 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1812 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1813 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1814 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1815 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1816 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1817 {
Jamie Madill2a6564e2014-07-11 09:53:19 -04001818 if (level > gl::log2(caps.maxCubeMapTextureSize))
Jamie Madill55ec3b12014-07-03 10:38:57 -04001819 {
Geoff Langb1196682014-07-23 13:47:29 -04001820 context->recordError(Error(GL_INVALID_VALUE));
1821 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001822 }
1823 if (tex->getTarget() != GL_TEXTURE_CUBE_MAP)
1824 {
Geoff Langb1196682014-07-23 13:47:29 -04001825 context->recordError(Error(GL_INVALID_OPERATION));
1826 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001827 }
Jamie Madill55ec3b12014-07-03 10:38:57 -04001828 }
1829 break;
1830
1831 default:
Geoff Langb1196682014-07-23 13:47:29 -04001832 context->recordError(Error(GL_INVALID_ENUM));
1833 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001834 }
Geoff Langa9be0dc2014-12-17 12:34:40 -05001835
1836 const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(tex->getInternalFormat(textarget, level));
1837 if (internalFormatInfo.compressed)
1838 {
1839 context->recordError(Error(GL_INVALID_OPERATION));
1840 return false;
1841 }
Jamie Madill55ec3b12014-07-03 10:38:57 -04001842 }
1843
Jamie Madill570f7c82014-07-03 10:38:54 -04001844 return true;
1845}
1846
Geoff Langb1196682014-07-23 13:47:29 -04001847bool ValidateGetUniformBase(Context *context, GLuint program, GLint location)
Jamie Madill0063c512014-08-25 15:47:53 -04001848{
1849 if (program == 0)
1850 {
Geoff Langb1196682014-07-23 13:47:29 -04001851 context->recordError(Error(GL_INVALID_VALUE));
1852 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001853 }
1854
Shannon Woods4de4fd62014-11-07 16:22:02 -05001855 if (!ValidProgram(context, program))
1856 {
1857 return false;
1858 }
1859
Jamie Madill0063c512014-08-25 15:47:53 -04001860 gl::Program *programObject = context->getProgram(program);
1861
1862 if (!programObject || !programObject->isLinked())
1863 {
Geoff Langb1196682014-07-23 13:47:29 -04001864 context->recordError(Error(GL_INVALID_OPERATION));
1865 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001866 }
1867
Geoff Lang7dd2e102014-11-10 15:19:26 -05001868 if (!programObject->isValidUniformLocation(location))
Jamie Madill549c7fd2014-08-25 15:47:56 -04001869 {
Geoff Langb1196682014-07-23 13:47:29 -04001870 context->recordError(Error(GL_INVALID_OPERATION));
1871 return false;
Jamie Madill549c7fd2014-08-25 15:47:56 -04001872 }
1873
Jamie Madill0063c512014-08-25 15:47:53 -04001874 return true;
1875}
1876
Geoff Langb1196682014-07-23 13:47:29 -04001877bool ValidateGetUniformfv(Context *context, GLuint program, GLint location, GLfloat* params)
Jamie Madill78f41802014-08-25 15:47:55 -04001878{
1879 return ValidateGetUniformBase(context, program, location);
1880}
1881
Geoff Langb1196682014-07-23 13:47:29 -04001882bool ValidateGetUniformiv(Context *context, GLuint program, GLint location, GLint* params)
Jamie Madill0063c512014-08-25 15:47:53 -04001883{
Jamie Madill78f41802014-08-25 15:47:55 -04001884 return ValidateGetUniformBase(context, program, location);
1885}
1886
Geoff Langb1196682014-07-23 13:47:29 -04001887static bool ValidateSizedGetUniform(Context *context, GLuint program, GLint location, GLsizei bufSize)
Jamie Madill78f41802014-08-25 15:47:55 -04001888{
1889 if (!ValidateGetUniformBase(context, program, location))
Jamie Madill0063c512014-08-25 15:47:53 -04001890 {
Jamie Madill78f41802014-08-25 15:47:55 -04001891 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001892 }
1893
Jamie Madilla502c742014-08-28 17:19:13 -04001894 gl::Program *programObject = context->getProgram(program);
1895 ASSERT(programObject);
Jamie Madill0063c512014-08-25 15:47:53 -04001896
Jamie Madill78f41802014-08-25 15:47:55 -04001897 // sized queries -- ensure the provided buffer is large enough
Geoff Lang7dd2e102014-11-10 15:19:26 -05001898 LinkedUniform *uniform = programObject->getUniformByLocation(location);
Jamie Madill78f41802014-08-25 15:47:55 -04001899 size_t requiredBytes = VariableExternalSize(uniform->type);
1900 if (static_cast<size_t>(bufSize) < requiredBytes)
Jamie Madill0063c512014-08-25 15:47:53 -04001901 {
Geoff Langb1196682014-07-23 13:47:29 -04001902 context->recordError(Error(GL_INVALID_OPERATION));
1903 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001904 }
1905
1906 return true;
1907}
1908
Geoff Langb1196682014-07-23 13:47:29 -04001909bool ValidateGetnUniformfvEXT(Context *context, GLuint program, GLint location, GLsizei bufSize, GLfloat* params)
Jamie Madill0063c512014-08-25 15:47:53 -04001910{
Jamie Madill78f41802014-08-25 15:47:55 -04001911 return ValidateSizedGetUniform(context, program, location, bufSize);
Jamie Madill0063c512014-08-25 15:47:53 -04001912}
1913
Geoff Langb1196682014-07-23 13:47:29 -04001914bool ValidateGetnUniformivEXT(Context *context, GLuint program, GLint location, GLsizei bufSize, GLint* params)
Jamie Madill0063c512014-08-25 15:47:53 -04001915{
Jamie Madill78f41802014-08-25 15:47:55 -04001916 return ValidateSizedGetUniform(context, program, location, bufSize);
Jamie Madill0063c512014-08-25 15:47:53 -04001917}
1918
Austin Kinross08332632015-05-05 13:35:47 -07001919bool ValidateDiscardFramebufferBase(Context *context, GLenum target, GLsizei numAttachments,
1920 const GLenum *attachments, bool defaultFramebuffer)
1921{
1922 if (numAttachments < 0)
1923 {
1924 context->recordError(Error(GL_INVALID_VALUE, "numAttachments must not be less than zero"));
1925 return false;
1926 }
1927
1928 for (GLsizei i = 0; i < numAttachments; ++i)
1929 {
1930 if (attachments[i] >= GL_COLOR_ATTACHMENT0 && attachments[i] <= GL_COLOR_ATTACHMENT15)
1931 {
1932 if (defaultFramebuffer)
1933 {
1934 context->recordError(Error(GL_INVALID_ENUM, "Invalid attachment when the default framebuffer is bound"));
1935 return false;
1936 }
1937
1938 if (attachments[i] >= GL_COLOR_ATTACHMENT0 + context->getCaps().maxColorAttachments)
1939 {
1940 context->recordError(Error(GL_INVALID_OPERATION,
1941 "Requested color attachment is greater than the maximum supported color attachments"));
1942 return false;
1943 }
1944 }
1945 else
1946 {
1947 switch (attachments[i])
1948 {
1949 case GL_DEPTH_ATTACHMENT:
1950 case GL_STENCIL_ATTACHMENT:
1951 case GL_DEPTH_STENCIL_ATTACHMENT:
1952 if (defaultFramebuffer)
1953 {
1954 context->recordError(Error(GL_INVALID_ENUM, "Invalid attachment when the default framebuffer is bound"));
1955 return false;
1956 }
1957 break;
1958 case GL_COLOR:
1959 case GL_DEPTH:
1960 case GL_STENCIL:
1961 if (!defaultFramebuffer)
1962 {
1963 context->recordError(Error(GL_INVALID_ENUM, "Invalid attachment when the default framebuffer is not bound"));
1964 return false;
1965 }
1966 break;
1967 default:
1968 context->recordError(Error(GL_INVALID_ENUM, "Invalid attachment"));
1969 return false;
1970 }
1971 }
1972 }
1973
1974 return true;
1975}
1976
Austin Kinross6ee1e782015-05-29 17:05:37 -07001977bool ValidateInsertEventMarkerEXT(Context *context, GLsizei length, const char *marker)
1978{
1979 // Note that debug marker calls must not set error state
1980
1981 if (length < 0)
1982 {
1983 return false;
1984 }
1985
1986 if (marker == nullptr)
1987 {
1988 return false;
1989 }
1990
1991 return true;
1992}
1993
1994bool ValidatePushGroupMarkerEXT(Context *context, GLsizei length, const char *marker)
1995{
1996 // Note that debug marker calls must not set error state
1997
1998 if (length < 0)
1999 {
2000 return false;
2001 }
2002
2003 if (length > 0 && marker == nullptr)
2004 {
2005 return false;
2006 }
2007
2008 return true;
2009}
2010
Geoff Langdcab33b2015-07-21 13:03:16 -04002011bool ValidateEGLImageTargetTexture2DOES(Context *context,
2012 egl::Display *display,
2013 GLenum target,
2014 egl::Image *image)
2015{
Geoff Langa8406172015-07-21 16:53:39 -04002016 if (!context->getExtensions().eglImage && !context->getExtensions().eglImageExternal)
2017 {
2018 context->recordError(Error(GL_INVALID_OPERATION));
2019 return false;
2020 }
2021
2022 switch (target)
2023 {
2024 case GL_TEXTURE_2D:
2025 break;
2026
2027 default:
2028 context->recordError(Error(GL_INVALID_ENUM, "invalid texture target."));
2029 return false;
2030 }
2031
2032 if (!display->isValidImage(image))
2033 {
2034 context->recordError(Error(GL_INVALID_VALUE, "EGL image is not valid."));
2035 return false;
2036 }
2037
2038 if (image->getSamples() > 0)
2039 {
2040 context->recordError(Error(GL_INVALID_OPERATION,
2041 "cannot create a 2D texture from a multisampled EGL image."));
2042 return false;
2043 }
2044
2045 const TextureCaps &textureCaps = context->getTextureCaps().get(image->getInternalFormat());
2046 if (!textureCaps.texturable)
2047 {
2048 context->recordError(Error(GL_INVALID_OPERATION,
2049 "EGL image internal format is not supported as a texture."));
2050 return false;
2051 }
2052
Geoff Langdcab33b2015-07-21 13:03:16 -04002053 return true;
2054}
2055
2056bool ValidateEGLImageTargetRenderbufferStorageOES(Context *context,
2057 egl::Display *display,
2058 GLenum target,
2059 egl::Image *image)
2060{
Geoff Langa8406172015-07-21 16:53:39 -04002061 if (!context->getExtensions().eglImage)
2062 {
2063 context->recordError(Error(GL_INVALID_OPERATION));
2064 return false;
2065 }
2066
2067 switch (target)
2068 {
2069 case GL_RENDERBUFFER:
2070 break;
2071
2072 default:
2073 context->recordError(Error(GL_INVALID_ENUM, "invalid renderbuffer target."));
2074 return false;
2075 }
2076
2077 if (!display->isValidImage(image))
2078 {
2079 context->recordError(Error(GL_INVALID_VALUE, "EGL image is not valid."));
2080 return false;
2081 }
2082
2083 const TextureCaps &textureCaps = context->getTextureCaps().get(image->getInternalFormat());
2084 if (!textureCaps.renderable)
2085 {
2086 context->recordError(Error(
2087 GL_INVALID_OPERATION, "EGL image internal format is not supported as a renderbuffer."));
2088 return false;
2089 }
2090
Geoff Langdcab33b2015-07-21 13:03:16 -04002091 return true;
2092}
Geoff Lange8ebe7f2013-08-05 15:03:13 -04002093}