blob: 22cd6800df52810ae49fb891ada117c1659fb32b [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
9#include "libGLESv2/validationES.h"
Jamie Madill26e91952014-03-05 15:01:27 -050010#include "libGLESv2/validationES2.h"
11#include "libGLESv2/validationES3.h"
Geoff Lange8ebe7f2013-08-05 15:03:13 -040012#include "libGLESv2/Context.h"
13#include "libGLESv2/Texture.h"
14#include "libGLESv2/Framebuffer.h"
Jamie Madille261b442014-06-25 12:42:21 -040015#include "libGLESv2/FramebufferAttachment.h"
Geoff Lange8ebe7f2013-08-05 15:03:13 -040016#include "libGLESv2/formatutils.h"
17#include "libGLESv2/main.h"
Jamie Madilldb2f14c2014-05-13 13:56:30 -040018#include "libGLESv2/Query.h"
Jamie Madill36398922014-05-20 14:51:53 -040019#include "libGLESv2/ProgramBinary.h"
Jamie Madill250d33f2014-06-06 17:09:03 -040020#include "libGLESv2/TransformFeedback.h"
Jamie Madilld4cfa572014-07-08 10:00:32 -040021#include "libGLESv2/VertexArray.h"
Jamie Madill2b976812014-08-25 15:47:49 -040022#include "libGLESv2/renderer/BufferImpl.h"
Geoff Lange8ebe7f2013-08-05 15:03:13 -040023
24#include "common/mathutil.h"
25#include "common/utilities.h"
26
Jamie Madill9ae396b2014-10-21 17:46:30 -040027// FIXME(jmadill): remove this when we support buffer data caching
28#include "libGLESv2/renderer/d3d/BufferD3D.h"
29
Geoff Lange8ebe7f2013-08-05 15:03:13 -040030namespace gl
31{
32
Geoff Lang0550d032014-01-30 11:29:07 -050033bool ValidCap(const Context *context, GLenum cap)
34{
35 switch (cap)
36 {
37 case GL_CULL_FACE:
38 case GL_POLYGON_OFFSET_FILL:
39 case GL_SAMPLE_ALPHA_TO_COVERAGE:
40 case GL_SAMPLE_COVERAGE:
41 case GL_SCISSOR_TEST:
42 case GL_STENCIL_TEST:
43 case GL_DEPTH_TEST:
44 case GL_BLEND:
45 case GL_DITHER:
46 return true;
47 case GL_PRIMITIVE_RESTART_FIXED_INDEX:
48 case GL_RASTERIZER_DISCARD:
49 return (context->getClientVersion() >= 3);
50 default:
51 return false;
52 }
53}
54
Jamie Madill1fc7e2c2014-01-21 16:47:10 -050055bool ValidTextureTarget(const Context *context, GLenum target)
Jamie Madill35d15012013-10-07 10:46:37 -040056{
Jamie Madilld7460c72014-01-21 16:38:14 -050057 switch (target)
Jamie Madill35d15012013-10-07 10:46:37 -040058 {
Jamie Madilld7460c72014-01-21 16:38:14 -050059 case GL_TEXTURE_2D:
60 case GL_TEXTURE_CUBE_MAP:
61 return true;
Jamie Madill35d15012013-10-07 10:46:37 -040062
Jamie Madilld7460c72014-01-21 16:38:14 -050063 case GL_TEXTURE_3D:
64 case GL_TEXTURE_2D_ARRAY:
65 return (context->getClientVersion() >= 3);
66
67 default:
68 return false;
69 }
Jamie Madill35d15012013-10-07 10:46:37 -040070}
71
Shannon Woods4dfed832014-03-17 20:03:39 -040072// This function differs from ValidTextureTarget in that the target must be
73// usable as the destination of a 2D operation-- so a cube face is valid, but
74// GL_TEXTURE_CUBE_MAP is not.
Jamie Madill560a8d82014-05-21 13:06:20 -040075// Note: duplicate of IsInternalTextureTarget
Shannon Woods4dfed832014-03-17 20:03:39 -040076bool ValidTexture2DDestinationTarget(const Context *context, GLenum target)
77{
78 switch (target)
79 {
80 case GL_TEXTURE_2D:
81 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
82 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
83 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
84 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
85 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
86 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
87 return true;
88 case GL_TEXTURE_2D_ARRAY:
89 case GL_TEXTURE_3D:
90 return (context->getClientVersion() >= 3);
91 default:
92 return false;
93 }
94}
95
Jamie Madill1fc7e2c2014-01-21 16:47:10 -050096bool ValidFramebufferTarget(GLenum target)
97{
98 META_ASSERT(GL_DRAW_FRAMEBUFFER_ANGLE == GL_DRAW_FRAMEBUFFER && GL_READ_FRAMEBUFFER_ANGLE == GL_READ_FRAMEBUFFER);
99
100 switch (target)
101 {
102 case GL_FRAMEBUFFER: return true;
103 case GL_READ_FRAMEBUFFER: return true;
104 case GL_DRAW_FRAMEBUFFER: return true;
105 default: return false;
106 }
107}
108
Jamie Madill8c96d582014-03-05 15:01:23 -0500109bool ValidBufferTarget(const Context *context, GLenum target)
110{
111 switch (target)
112 {
113 case GL_ARRAY_BUFFER:
114 case GL_ELEMENT_ARRAY_BUFFER:
115 return true;
116
Jamie Madill8c96d582014-03-05 15:01:23 -0500117 case GL_PIXEL_PACK_BUFFER:
118 case GL_PIXEL_UNPACK_BUFFER:
Geoff Langc0b9ef42014-07-02 10:02:37 -0400119 return context->getExtensions().pixelBufferObject;
Shannon Woods158c4382014-05-06 13:00:07 -0400120
Shannon Woodsb3801742014-03-27 14:59:19 -0400121 case GL_COPY_READ_BUFFER:
122 case GL_COPY_WRITE_BUFFER:
Jamie Madill8c96d582014-03-05 15:01:23 -0500123 case GL_TRANSFORM_FEEDBACK_BUFFER:
124 case GL_UNIFORM_BUFFER:
125 return (context->getClientVersion() >= 3);
126
127 default:
128 return false;
129 }
130}
131
Jamie Madill70656a62014-03-05 15:01:26 -0500132bool ValidBufferParameter(const Context *context, GLenum pname)
133{
134 switch (pname)
135 {
136 case GL_BUFFER_USAGE:
137 case GL_BUFFER_SIZE:
138 return true;
139
140 // GL_BUFFER_MAP_POINTER is a special case, and may only be
141 // queried with GetBufferPointerv
142 case GL_BUFFER_ACCESS_FLAGS:
143 case GL_BUFFER_MAPPED:
144 case GL_BUFFER_MAP_OFFSET:
145 case GL_BUFFER_MAP_LENGTH:
146 return (context->getClientVersion() >= 3);
147
148 default:
149 return false;
150 }
151}
152
Jamie Madill8c96d582014-03-05 15:01:23 -0500153bool ValidMipLevel(const Context *context, GLenum target, GLint level)
Geoff Langce635692013-09-24 13:56:32 -0400154{
Geoff Langaae65a42014-05-26 12:43:44 -0400155 size_t maxDimension = 0;
Geoff Langce635692013-09-24 13:56:32 -0400156 switch (target)
157 {
Geoff Langaae65a42014-05-26 12:43:44 -0400158 case GL_TEXTURE_2D: maxDimension = context->getCaps().max2DTextureSize; break;
Geoff Langce635692013-09-24 13:56:32 -0400159 case GL_TEXTURE_CUBE_MAP:
160 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
161 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
162 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
163 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
164 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
Geoff Langaae65a42014-05-26 12:43:44 -0400165 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: maxDimension = context->getCaps().maxCubeMapTextureSize; break;
166 case GL_TEXTURE_3D: maxDimension = context->getCaps().max3DTextureSize; break;
167 case GL_TEXTURE_2D_ARRAY: maxDimension = context->getCaps().max2DTextureSize; break;
Geoff Langce635692013-09-24 13:56:32 -0400168 default: UNREACHABLE();
169 }
170
Geoff Langaae65a42014-05-26 12:43:44 -0400171 return level <= gl::log2(maxDimension);
Geoff Langce635692013-09-24 13:56:32 -0400172}
173
Geoff Langb1196682014-07-23 13:47:29 -0400174bool ValidImageSize(const Context *context, GLenum target, GLint level,
Jamie Madill4fd75c12014-06-23 10:53:54 -0400175 GLsizei width, GLsizei height, GLsizei depth)
Geoff Langce635692013-09-24 13:56:32 -0400176{
177 if (level < 0 || width < 0 || height < 0 || depth < 0)
178 {
179 return false;
180 }
181
Geoff Langc0b9ef42014-07-02 10:02:37 -0400182 if (!context->getExtensions().textureNPOT &&
Jamie Madill4fd75c12014-06-23 10:53:54 -0400183 (level != 0 && (!gl::isPow2(width) || !gl::isPow2(height) || !gl::isPow2(depth))))
Geoff Langce635692013-09-24 13:56:32 -0400184 {
185 return false;
186 }
187
188 if (!ValidMipLevel(context, target, level))
189 {
190 return false;
191 }
192
193 return true;
194}
195
Geoff Langb1196682014-07-23 13:47:29 -0400196bool ValidCompressedImageSize(const Context *context, GLenum internalFormat, GLsizei width, GLsizei height)
Geoff Langd4f180b2013-09-24 13:57:44 -0400197{
Geoff Lang5d601382014-07-22 15:14:06 -0400198 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalFormat);
199 if (!formatInfo.compressed)
Geoff Langd4f180b2013-09-24 13:57:44 -0400200 {
201 return false;
202 }
203
Geoff Lang5d601382014-07-22 15:14:06 -0400204 if (width < 0 || (static_cast<GLuint>(width) > formatInfo.compressedBlockWidth && width % formatInfo.compressedBlockWidth != 0) ||
205 height < 0 || (static_cast<GLuint>(height) > formatInfo.compressedBlockHeight && height % formatInfo.compressedBlockHeight != 0))
Geoff Langd4f180b2013-09-24 13:57:44 -0400206 {
207 return false;
208 }
209
210 return true;
211}
212
Geoff Lang37dde692014-01-31 16:34:54 -0500213bool ValidQueryType(const Context *context, GLenum queryType)
214{
215 META_ASSERT(GL_ANY_SAMPLES_PASSED == GL_ANY_SAMPLES_PASSED_EXT);
216 META_ASSERT(GL_ANY_SAMPLES_PASSED_CONSERVATIVE == GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT);
217
218 switch (queryType)
219 {
220 case GL_ANY_SAMPLES_PASSED:
221 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
222 return true;
223 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
224 return (context->getClientVersion() >= 3);
225 default:
226 return false;
227 }
228}
229
Geoff Langb1196682014-07-23 13:47:29 -0400230bool ValidProgram(Context *context, GLuint id)
Geoff Lang48dcae72014-02-05 16:28:24 -0500231{
232 // ES3 spec (section 2.11.1) -- "Commands that accept shader or program object names will generate the
233 // error INVALID_VALUE if the provided name is not the name of either a shader or program object and
234 // INVALID_OPERATION if the provided name identifies an object that is not the expected type."
235
236 if (context->getProgram(id) != NULL)
237 {
238 return true;
239 }
240 else if (context->getShader(id) != NULL)
241 {
242 // ID is the wrong type
Geoff Langb1196682014-07-23 13:47:29 -0400243 context->recordError(Error(GL_INVALID_OPERATION));
244 return false;
Geoff Lang48dcae72014-02-05 16:28:24 -0500245 }
246 else
247 {
248 // No shader/program object has this ID
Geoff Langb1196682014-07-23 13:47:29 -0400249 context->recordError(Error(GL_INVALID_VALUE));
250 return false;
Geoff Lang48dcae72014-02-05 16:28:24 -0500251 }
252}
253
Geoff Langb1196682014-07-23 13:47:29 -0400254bool ValidateAttachmentTarget(gl::Context *context, GLenum attachment)
Jamie Madillb4472272014-07-03 10:38:55 -0400255{
256 if (attachment >= GL_COLOR_ATTACHMENT0_EXT && attachment <= GL_COLOR_ATTACHMENT15_EXT)
257 {
258 const unsigned int colorAttachment = (attachment - GL_COLOR_ATTACHMENT0_EXT);
259
Geoff Langaae65a42014-05-26 12:43:44 -0400260 if (colorAttachment >= context->getCaps().maxColorAttachments)
Jamie Madillb4472272014-07-03 10:38:55 -0400261 {
Geoff Langb1196682014-07-23 13:47:29 -0400262 context->recordError(Error(GL_INVALID_VALUE));
263 return false;
Jamie Madillb4472272014-07-03 10:38:55 -0400264 }
265 }
266 else
267 {
268 switch (attachment)
269 {
270 case GL_DEPTH_ATTACHMENT:
271 case GL_STENCIL_ATTACHMENT:
272 break;
273
274 case GL_DEPTH_STENCIL_ATTACHMENT:
275 if (context->getClientVersion() < 3)
276 {
Geoff Langb1196682014-07-23 13:47:29 -0400277 context->recordError(Error(GL_INVALID_ENUM));
278 return false;
Jamie Madillb4472272014-07-03 10:38:55 -0400279 }
280 break;
281
282 default:
Geoff Langb1196682014-07-23 13:47:29 -0400283 context->recordError(Error(GL_INVALID_ENUM));
284 return false;
Jamie Madillb4472272014-07-03 10:38:55 -0400285 }
286 }
287
288 return true;
289}
290
Geoff Langb1196682014-07-23 13:47:29 -0400291bool ValidateRenderbufferStorageParameters(gl::Context *context, GLenum target, GLsizei samples,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400292 GLenum internalformat, GLsizei width, GLsizei height,
293 bool angleExtension)
294{
295 switch (target)
296 {
297 case GL_RENDERBUFFER:
298 break;
299 default:
Geoff Langb1196682014-07-23 13:47:29 -0400300 context->recordError(Error(GL_INVALID_ENUM));
301 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400302 }
303
304 if (width < 0 || height < 0 || samples < 0)
305 {
Geoff Langb1196682014-07-23 13:47:29 -0400306 context->recordError(Error(GL_INVALID_VALUE));
307 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400308 }
309
Geoff Langd87878e2014-09-19 15:42:59 -0400310 const TextureCaps &formatCaps = context->getTextureCaps().get(internalformat);
311 if (!formatCaps.renderable)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400312 {
Geoff Langb1196682014-07-23 13:47:29 -0400313 context->recordError(Error(GL_INVALID_ENUM));
314 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400315 }
316
317 // ANGLE_framebuffer_multisample does not explicitly state that the internal format must be
318 // sized but it does state that the format must be in the ES2.0 spec table 4.5 which contains
319 // only sized internal formats. The ES3 spec (section 4.4.2) does, however, state that the
320 // internal format must be sized and not an integer format if samples is greater than zero.
Geoff Langd87878e2014-09-19 15:42:59 -0400321 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalformat);
Geoff Lang5d601382014-07-22 15:14:06 -0400322 if (formatInfo.pixelBytes == 0)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400323 {
Geoff Langb1196682014-07-23 13:47:29 -0400324 context->recordError(Error(GL_INVALID_ENUM));
325 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400326 }
327
Geoff Lang5d601382014-07-22 15:14:06 -0400328 if ((formatInfo.componentType == GL_UNSIGNED_INT || formatInfo.componentType == GL_INT) && samples > 0)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400329 {
Geoff Langb1196682014-07-23 13:47:29 -0400330 context->recordError(Error(GL_INVALID_OPERATION));
331 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400332 }
333
Geoff Langaae65a42014-05-26 12:43:44 -0400334 if (static_cast<GLuint>(std::max(width, height)) > context->getCaps().maxRenderbufferSize)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400335 {
Geoff Langb1196682014-07-23 13:47:29 -0400336 context->recordError(Error(GL_INVALID_VALUE));
337 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400338 }
339
340 // ANGLE_framebuffer_multisample states that the value of samples must be less than or equal
341 // to MAX_SAMPLES_ANGLE (Context::getMaxSupportedSamples) while the ES3.0 spec (section 4.4.2)
342 // states that samples must be less than or equal to the maximum samples for the specified
343 // internal format.
344 if (angleExtension)
345 {
Geoff Lang5f4c4632014-07-03 13:46:52 -0400346 ASSERT(context->getExtensions().framebufferMultisample);
347 if (static_cast<GLuint>(samples) > context->getExtensions().maxSamples)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400348 {
Geoff Langb1196682014-07-23 13:47:29 -0400349 context->recordError(Error(GL_INVALID_VALUE));
350 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400351 }
Geoff Lang5f4c4632014-07-03 13:46:52 -0400352
353 // Check if this specific format supports enough samples
354 if (static_cast<GLuint>(samples) > formatCaps.getMaxSamples())
355 {
Geoff Langb1196682014-07-23 13:47:29 -0400356 context->recordError(Error(GL_OUT_OF_MEMORY));
357 return false;
Geoff Lang5f4c4632014-07-03 13:46:52 -0400358 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400359 }
360 else
361 {
Geoff Lang5f4c4632014-07-03 13:46:52 -0400362 if (static_cast<GLuint>(samples) > formatCaps.getMaxSamples())
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400363 {
Geoff Langb1196682014-07-23 13:47:29 -0400364 context->recordError(Error(GL_INVALID_VALUE));
365 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400366 }
367 }
368
Shannon Woods53a94a82014-06-24 15:20:36 -0400369 GLuint handle = context->getState().getRenderbufferId();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400370 if (handle == 0)
371 {
Geoff Langb1196682014-07-23 13:47:29 -0400372 context->recordError(Error(GL_INVALID_OPERATION));
373 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400374 }
375
376 return true;
377}
378
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500379bool ValidateFramebufferRenderbufferParameters(gl::Context *context, GLenum target, GLenum attachment,
380 GLenum renderbuffertarget, GLuint renderbuffer)
381{
Shannon Woods1da3cf62014-06-27 15:32:23 -0400382 if (!ValidFramebufferTarget(target))
383 {
Geoff Langb1196682014-07-23 13:47:29 -0400384 context->recordError(Error(GL_INVALID_ENUM));
385 return false;
Shannon Woods1da3cf62014-06-27 15:32:23 -0400386 }
387
Shannon Woods53a94a82014-06-24 15:20:36 -0400388 gl::Framebuffer *framebuffer = context->getState().getTargetFramebuffer(target);
389 GLuint framebufferHandle = context->getState().getTargetFramebuffer(target)->id();
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500390
391 if (!framebuffer || (framebufferHandle == 0 && renderbuffer != 0))
392 {
Geoff Langb1196682014-07-23 13:47:29 -0400393 context->recordError(Error(GL_INVALID_OPERATION));
394 return false;
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500395 }
396
Jamie Madillb4472272014-07-03 10:38:55 -0400397 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500398 {
Jamie Madillb4472272014-07-03 10:38:55 -0400399 return false;
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500400 }
401
Jamie Madillab9d82c2014-01-21 16:38:14 -0500402 // [OpenGL ES 2.0.25] Section 4.4.3 page 112
403 // [OpenGL ES 3.0.2] Section 4.4.2 page 201
404 // 'renderbuffer' must be either zero or the name of an existing renderbuffer object of
405 // type 'renderbuffertarget', otherwise an INVALID_OPERATION error is generated.
406 if (renderbuffer != 0)
407 {
408 if (!context->getRenderbuffer(renderbuffer))
409 {
Geoff Langb1196682014-07-23 13:47:29 -0400410 context->recordError(Error(GL_INVALID_OPERATION));
411 return false;
Jamie Madillab9d82c2014-01-21 16:38:14 -0500412 }
413 }
414
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500415 return true;
416}
417
Jamie Madill3c7fa222014-06-05 13:08:51 -0400418static bool IsPartialBlit(gl::Context *context, gl::FramebufferAttachment *readBuffer, gl::FramebufferAttachment *writeBuffer,
Geoff Lang125deab2013-08-09 13:34:16 -0400419 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
420 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1)
421{
422 if (srcX0 != 0 || srcY0 != 0 || dstX0 != 0 || dstY0 != 0 ||
423 dstX1 != writeBuffer->getWidth() || dstY1 != writeBuffer->getHeight() ||
424 srcX1 != readBuffer->getWidth() || srcY1 != readBuffer->getHeight())
425 {
426 return true;
427 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400428 else if (context->getState().isScissorTestEnabled())
Geoff Lang125deab2013-08-09 13:34:16 -0400429 {
Shannon Woods53a94a82014-06-24 15:20:36 -0400430 const Rectangle &scissor = context->getState().getScissor();
Geoff Lang125deab2013-08-09 13:34:16 -0400431
Shannon Woods53a94a82014-06-24 15:20:36 -0400432 return scissor.x > 0 || scissor.y > 0 ||
433 scissor.width < writeBuffer->getWidth() ||
434 scissor.height < writeBuffer->getHeight();
Geoff Lang125deab2013-08-09 13:34:16 -0400435 }
436 else
437 {
438 return false;
439 }
440}
441
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400442bool ValidateBlitFramebufferParameters(gl::Context *context, GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400443 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask,
444 GLenum filter, bool fromAngleExtension)
445{
446 switch (filter)
447 {
448 case GL_NEAREST:
449 break;
450 case GL_LINEAR:
451 if (fromAngleExtension)
452 {
Geoff Langb1196682014-07-23 13:47:29 -0400453 context->recordError(Error(GL_INVALID_ENUM));
454 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400455 }
456 break;
457 default:
Geoff Langb1196682014-07-23 13:47:29 -0400458 context->recordError(Error(GL_INVALID_ENUM));
459 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400460 }
461
462 if ((mask & ~(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)) != 0)
463 {
Geoff Langb1196682014-07-23 13:47:29 -0400464 context->recordError(Error(GL_INVALID_VALUE));
465 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400466 }
467
468 if (mask == 0)
469 {
470 // ES3.0 spec, section 4.3.2 specifies that a mask of zero is valid and no
471 // buffers are copied.
472 return false;
473 }
474
475 if (fromAngleExtension && (srcX1 - srcX0 != dstX1 - dstX0 || srcY1 - srcY0 != dstY1 - dstY0))
476 {
477 ERR("Scaling and flipping in BlitFramebufferANGLE not supported by this implementation.");
Geoff Langb1196682014-07-23 13:47:29 -0400478 context->recordError(Error(GL_INVALID_OPERATION));
479 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400480 }
481
482 // ES3.0 spec, section 4.3.2 states that linear filtering is only available for the
483 // color buffer, leaving only nearest being unfiltered from above
484 if ((mask & ~GL_COLOR_BUFFER_BIT) != 0 && filter != GL_NEAREST)
485 {
Geoff Langb1196682014-07-23 13:47:29 -0400486 context->recordError(Error(GL_INVALID_OPERATION));
487 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400488 }
489
Shannon Woods53a94a82014-06-24 15:20:36 -0400490 if (context->getState().getReadFramebuffer()->id() == context->getState().getDrawFramebuffer()->id())
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400491 {
492 if (fromAngleExtension)
493 {
494 ERR("Blits with the same source and destination framebuffer are not supported by this "
495 "implementation.");
496 }
Geoff Langb1196682014-07-23 13:47:29 -0400497 context->recordError(Error(GL_INVALID_OPERATION));
498 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400499 }
500
Shannon Woods53a94a82014-06-24 15:20:36 -0400501 gl::Framebuffer *readFramebuffer = context->getState().getReadFramebuffer();
502 gl::Framebuffer *drawFramebuffer = context->getState().getDrawFramebuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400503 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
504 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
505 {
Geoff Langb1196682014-07-23 13:47:29 -0400506 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
507 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400508 }
509
510 if (drawFramebuffer->getSamples() != 0)
511 {
Geoff Langb1196682014-07-23 13:47:29 -0400512 context->recordError(Error(GL_INVALID_OPERATION));
513 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400514 }
515
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400516 bool sameBounds = srcX0 == dstX0 && srcY0 == dstY0 && srcX1 == dstX1 && srcY1 == dstY1;
517
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400518 if (mask & GL_COLOR_BUFFER_BIT)
519 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400520 gl::FramebufferAttachment *readColorBuffer = readFramebuffer->getReadColorbuffer();
521 gl::FramebufferAttachment *drawColorBuffer = drawFramebuffer->getFirstColorbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400522
523 if (readColorBuffer && drawColorBuffer)
524 {
Geoff Lang005df412013-10-16 14:12:50 -0400525 GLenum readInternalFormat = readColorBuffer->getActualFormat();
Geoff Lang5d601382014-07-22 15:14:06 -0400526 const InternalFormat &readFormatInfo = GetInternalFormatInfo(readInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400527
528 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; i++)
529 {
530 if (drawFramebuffer->isEnabledColorAttachment(i))
531 {
Geoff Lang005df412013-10-16 14:12:50 -0400532 GLenum drawInternalFormat = drawFramebuffer->getColorbuffer(i)->getActualFormat();
Geoff Lang5d601382014-07-22 15:14:06 -0400533 const InternalFormat &drawFormatInfo = GetInternalFormatInfo(drawInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400534
Geoff Langb2f3d052013-08-13 12:49:27 -0400535 // The GL ES 3.0.2 spec (pg 193) states that:
536 // 1) If the read buffer is fixed point format, the draw buffer must be as well
537 // 2) If the read buffer is an unsigned integer format, the draw buffer must be as well
538 // 3) If the read buffer is a signed integer format, the draw buffer must be as well
Geoff Lang5d601382014-07-22 15:14:06 -0400539 if ( (readFormatInfo.componentType == GL_UNSIGNED_NORMALIZED || readFormatInfo.componentType == GL_SIGNED_NORMALIZED) &&
540 !(drawFormatInfo.componentType == GL_UNSIGNED_NORMALIZED || drawFormatInfo.componentType == GL_SIGNED_NORMALIZED))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400541 {
Geoff Langb1196682014-07-23 13:47:29 -0400542 context->recordError(Error(GL_INVALID_OPERATION));
543 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400544 }
545
Geoff Lang5d601382014-07-22 15:14:06 -0400546 if (readFormatInfo.componentType == GL_UNSIGNED_INT && drawFormatInfo.componentType != GL_UNSIGNED_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400547 {
Geoff Langb1196682014-07-23 13:47:29 -0400548 context->recordError(Error(GL_INVALID_OPERATION));
549 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400550 }
551
Geoff Lang5d601382014-07-22 15:14:06 -0400552 if (readFormatInfo.componentType == GL_INT && drawFormatInfo.componentType != GL_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400553 {
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
Geoff Langb2f3d052013-08-13 12:49:27 -0400558 if (readColorBuffer->getSamples() > 0 && (readInternalFormat != drawInternalFormat || !sameBounds))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400559 {
Geoff Langb1196682014-07-23 13:47:29 -0400560 context->recordError(Error(GL_INVALID_OPERATION));
561 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400562 }
563 }
564 }
565
Geoff Lang5d601382014-07-22 15:14:06 -0400566 if ((readFormatInfo.componentType == GL_INT || readFormatInfo.componentType == GL_UNSIGNED_INT) && filter == GL_LINEAR)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400567 {
Geoff Langb1196682014-07-23 13:47:29 -0400568 context->recordError(Error(GL_INVALID_OPERATION));
569 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400570 }
571
572 if (fromAngleExtension)
573 {
574 const GLenum readColorbufferType = readFramebuffer->getReadColorbufferType();
575 if (readColorbufferType != GL_TEXTURE_2D && readColorbufferType != GL_RENDERBUFFER)
576 {
Geoff Langb1196682014-07-23 13:47:29 -0400577 context->recordError(Error(GL_INVALID_OPERATION));
578 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400579 }
580
581 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
582 {
583 if (drawFramebuffer->isEnabledColorAttachment(colorAttachment))
584 {
Jamie Madille92a3542014-07-03 10:38:58 -0400585 FramebufferAttachment *attachment = drawFramebuffer->getColorbuffer(colorAttachment);
586 ASSERT(attachment);
587
588 if (attachment->type() != GL_TEXTURE_2D && attachment->type() != GL_RENDERBUFFER)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400589 {
Geoff Langb1196682014-07-23 13:47:29 -0400590 context->recordError(Error(GL_INVALID_OPERATION));
591 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400592 }
593
Jamie Madillf8f18f02014-10-02 10:44:17 -0400594 // Return an error if the destination formats do not match
595 if (attachment->getInternalFormat() != readColorBuffer->getInternalFormat())
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400596 {
Geoff Langb1196682014-07-23 13:47:29 -0400597 context->recordError(Error(GL_INVALID_OPERATION));
598 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400599 }
600 }
601 }
Geoff Lang125deab2013-08-09 13:34:16 -0400602 if (readFramebuffer->getSamples() != 0 && IsPartialBlit(context, readColorBuffer, drawColorBuffer,
603 srcX0, srcY0, srcX1, srcY1,
604 dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400605 {
Geoff Langb1196682014-07-23 13:47:29 -0400606 context->recordError(Error(GL_INVALID_OPERATION));
607 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400608 }
609 }
610 }
611 }
612
613 if (mask & GL_DEPTH_BUFFER_BIT)
614 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400615 gl::FramebufferAttachment *readDepthBuffer = readFramebuffer->getDepthbuffer();
616 gl::FramebufferAttachment *drawDepthBuffer = drawFramebuffer->getDepthbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400617
618 if (readDepthBuffer && drawDepthBuffer)
619 {
620 if (readDepthBuffer->getActualFormat() != drawDepthBuffer->getActualFormat())
621 {
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
626 if (readDepthBuffer->getSamples() > 0 && !sameBounds)
627 {
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
632 if (fromAngleExtension)
633 {
Geoff Lang125deab2013-08-09 13:34:16 -0400634 if (IsPartialBlit(context, readDepthBuffer, drawDepthBuffer,
635 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400636 {
637 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
Geoff Langb1196682014-07-23 13:47:29 -0400638 context->recordError(Error(GL_INVALID_OPERATION)); // only whole-buffer copies are permitted
639 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400640 }
641
642 if (readDepthBuffer->getSamples() != 0 || drawDepthBuffer->getSamples() != 0)
643 {
Geoff Langb1196682014-07-23 13:47:29 -0400644 context->recordError(Error(GL_INVALID_OPERATION));
645 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400646 }
647 }
648 }
649 }
650
651 if (mask & GL_STENCIL_BUFFER_BIT)
652 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400653 gl::FramebufferAttachment *readStencilBuffer = readFramebuffer->getStencilbuffer();
654 gl::FramebufferAttachment *drawStencilBuffer = drawFramebuffer->getStencilbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400655
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400656 if (readStencilBuffer && drawStencilBuffer)
657 {
658 if (readStencilBuffer->getActualFormat() != drawStencilBuffer->getActualFormat())
659 {
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
664 if (readStencilBuffer->getSamples() > 0 && !sameBounds)
665 {
Geoff Langb1196682014-07-23 13:47:29 -0400666 context->recordError(Error(GL_INVALID_OPERATION));
667 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400668 }
669
670 if (fromAngleExtension)
671 {
Geoff Lang125deab2013-08-09 13:34:16 -0400672 if (IsPartialBlit(context, readStencilBuffer, drawStencilBuffer,
673 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400674 {
675 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
Geoff Langb1196682014-07-23 13:47:29 -0400676 context->recordError(Error(GL_INVALID_OPERATION)); // only whole-buffer copies are permitted
677 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400678 }
679
680 if (readStencilBuffer->getSamples() != 0 || drawStencilBuffer->getSamples() != 0)
681 {
Geoff Langb1196682014-07-23 13:47:29 -0400682 context->recordError(Error(GL_INVALID_OPERATION));
683 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400684 }
685 }
686 }
687 }
688
689 return true;
690}
691
Geoff Langb1196682014-07-23 13:47:29 -0400692bool ValidateGetVertexAttribParameters(Context *context, GLenum pname)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400693{
694 switch (pname)
695 {
696 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
697 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
698 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
699 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
700 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
701 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
702 case GL_CURRENT_VERTEX_ATTRIB:
703 return true;
704
705 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR:
706 // Don't verify ES3 context because GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE uses
707 // the same constant.
708 META_ASSERT(GL_VERTEX_ATTRIB_ARRAY_DIVISOR == GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE);
709 return true;
710
711 case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
Geoff Langb1196682014-07-23 13:47:29 -0400712 if (context->getClientVersion() < 3)
713 {
714 context->recordError(Error(GL_INVALID_ENUM));
715 return false;
716 }
717 return true;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400718
719 default:
Geoff Langb1196682014-07-23 13:47:29 -0400720 context->recordError(Error(GL_INVALID_ENUM));
721 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400722 }
723}
724
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400725bool ValidateTexParamParameters(gl::Context *context, GLenum pname, GLint param)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400726{
727 switch (pname)
728 {
729 case GL_TEXTURE_WRAP_R:
730 case GL_TEXTURE_SWIZZLE_R:
731 case GL_TEXTURE_SWIZZLE_G:
732 case GL_TEXTURE_SWIZZLE_B:
733 case GL_TEXTURE_SWIZZLE_A:
734 case GL_TEXTURE_BASE_LEVEL:
735 case GL_TEXTURE_MAX_LEVEL:
736 case GL_TEXTURE_COMPARE_MODE:
737 case GL_TEXTURE_COMPARE_FUNC:
738 case GL_TEXTURE_MIN_LOD:
739 case GL_TEXTURE_MAX_LOD:
740 if (context->getClientVersion() < 3)
741 {
Geoff Langb1196682014-07-23 13:47:29 -0400742 context->recordError(Error(GL_INVALID_ENUM));
743 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400744 }
745 break;
746
747 default: break;
748 }
749
750 switch (pname)
751 {
752 case GL_TEXTURE_WRAP_S:
753 case GL_TEXTURE_WRAP_T:
754 case GL_TEXTURE_WRAP_R:
755 switch (param)
756 {
757 case GL_REPEAT:
758 case GL_CLAMP_TO_EDGE:
759 case GL_MIRRORED_REPEAT:
760 return true;
761 default:
Geoff Langb1196682014-07-23 13:47:29 -0400762 context->recordError(Error(GL_INVALID_ENUM));
763 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400764 }
765
766 case GL_TEXTURE_MIN_FILTER:
767 switch (param)
768 {
769 case GL_NEAREST:
770 case GL_LINEAR:
771 case GL_NEAREST_MIPMAP_NEAREST:
772 case GL_LINEAR_MIPMAP_NEAREST:
773 case GL_NEAREST_MIPMAP_LINEAR:
774 case GL_LINEAR_MIPMAP_LINEAR:
775 return true;
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 break;
781
782 case GL_TEXTURE_MAG_FILTER:
783 switch (param)
784 {
785 case GL_NEAREST:
786 case GL_LINEAR:
787 return true;
788 default:
Geoff Langb1196682014-07-23 13:47:29 -0400789 context->recordError(Error(GL_INVALID_ENUM));
790 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400791 }
792 break;
793
794 case GL_TEXTURE_USAGE_ANGLE:
795 switch (param)
796 {
797 case GL_NONE:
798 case GL_FRAMEBUFFER_ATTACHMENT_ANGLE:
799 return true;
800 default:
Geoff Langb1196682014-07-23 13:47:29 -0400801 context->recordError(Error(GL_INVALID_ENUM));
802 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400803 }
804 break;
805
806 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
Geoff Langc0b9ef42014-07-02 10:02:37 -0400807 if (!context->getExtensions().textureFilterAnisotropic)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400808 {
Geoff Langb1196682014-07-23 13:47:29 -0400809 context->recordError(Error(GL_INVALID_ENUM));
810 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400811 }
812
813 // we assume the parameter passed to this validation method is truncated, not rounded
814 if (param < 1)
815 {
Geoff Langb1196682014-07-23 13:47:29 -0400816 context->recordError(Error(GL_INVALID_VALUE));
817 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400818 }
819 return true;
820
821 case GL_TEXTURE_MIN_LOD:
822 case GL_TEXTURE_MAX_LOD:
823 // any value is permissible
824 return true;
825
826 case GL_TEXTURE_COMPARE_MODE:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400827 // Acceptable mode parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400828 switch (param)
829 {
830 case GL_NONE:
831 case GL_COMPARE_REF_TO_TEXTURE:
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_COMPARE_FUNC:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400840 // Acceptable function parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400841 switch (param)
842 {
843 case GL_LEQUAL:
844 case GL_GEQUAL:
845 case GL_LESS:
846 case GL_GREATER:
847 case GL_EQUAL:
848 case GL_NOTEQUAL:
849 case GL_ALWAYS:
850 case GL_NEVER:
851 return true;
852 default:
Geoff Langb1196682014-07-23 13:47:29 -0400853 context->recordError(Error(GL_INVALID_ENUM));
854 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400855 }
856 break;
857
858 case GL_TEXTURE_SWIZZLE_R:
859 case GL_TEXTURE_SWIZZLE_G:
860 case GL_TEXTURE_SWIZZLE_B:
861 case GL_TEXTURE_SWIZZLE_A:
Geoff Langbc90a482013-09-17 16:51:27 -0400862 switch (param)
863 {
864 case GL_RED:
865 case GL_GREEN:
866 case GL_BLUE:
867 case GL_ALPHA:
868 case GL_ZERO:
869 case GL_ONE:
870 return true;
871 default:
Geoff Langb1196682014-07-23 13:47:29 -0400872 context->recordError(Error(GL_INVALID_ENUM));
873 return false;
Geoff Langbc90a482013-09-17 16:51:27 -0400874 }
875 break;
876
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400877 case GL_TEXTURE_BASE_LEVEL:
878 case GL_TEXTURE_MAX_LEVEL:
Nicolas Capens8de68282014-04-04 11:10:27 -0400879 if (param < 0)
880 {
Geoff Langb1196682014-07-23 13:47:29 -0400881 context->recordError(Error(GL_INVALID_VALUE));
882 return false;
Nicolas Capens8de68282014-04-04 11:10:27 -0400883 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400884 return true;
885
886 default:
Geoff Langb1196682014-07-23 13:47:29 -0400887 context->recordError(Error(GL_INVALID_ENUM));
888 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400889 }
890}
891
Geoff Langb1196682014-07-23 13:47:29 -0400892bool ValidateSamplerObjectParameter(gl::Context *context, GLenum pname)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400893{
894 switch (pname)
895 {
896 case GL_TEXTURE_MIN_FILTER:
897 case GL_TEXTURE_MAG_FILTER:
898 case GL_TEXTURE_WRAP_S:
899 case GL_TEXTURE_WRAP_T:
900 case GL_TEXTURE_WRAP_R:
901 case GL_TEXTURE_MIN_LOD:
902 case GL_TEXTURE_MAX_LOD:
903 case GL_TEXTURE_COMPARE_MODE:
904 case GL_TEXTURE_COMPARE_FUNC:
905 return true;
906
907 default:
Geoff Langb1196682014-07-23 13:47:29 -0400908 context->recordError(Error(GL_INVALID_ENUM));
909 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400910 }
911}
912
Jamie Madill26e91952014-03-05 15:01:27 -0500913bool ValidateReadPixelsParameters(gl::Context *context, GLint x, GLint y, GLsizei width, GLsizei height,
914 GLenum format, GLenum type, GLsizei *bufSize, GLvoid *pixels)
915{
Shannon Woods53a94a82014-06-24 15:20:36 -0400916 gl::Framebuffer *framebuffer = context->getState().getReadFramebuffer();
Jamie Madill893ab082014-05-16 16:56:10 -0400917 ASSERT(framebuffer);
Jamie Madill26e91952014-03-05 15:01:27 -0500918
919 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
920 {
Geoff Langb1196682014-07-23 13:47:29 -0400921 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
922 return false;
Jamie Madill26e91952014-03-05 15:01:27 -0500923 }
924
Shannon Woods53a94a82014-06-24 15:20:36 -0400925 if (context->getState().getReadFramebuffer()->id() != 0 && framebuffer->getSamples() != 0)
Jamie Madill26e91952014-03-05 15:01:27 -0500926 {
Geoff Langb1196682014-07-23 13:47:29 -0400927 context->recordError(Error(GL_INVALID_OPERATION));
928 return false;
Jamie Madill26e91952014-03-05 15:01:27 -0500929 }
930
Jamie Madill893ab082014-05-16 16:56:10 -0400931 if (!framebuffer->getReadColorbuffer())
932 {
Geoff Langb1196682014-07-23 13:47:29 -0400933 context->recordError(Error(GL_INVALID_OPERATION));
934 return false;
Jamie Madill893ab082014-05-16 16:56:10 -0400935 }
936
Jamie Madill26e91952014-03-05 15:01:27 -0500937 GLenum currentInternalFormat, currentFormat, currentType;
Geoff Lange4a492b2014-06-19 14:14:41 -0400938 GLuint clientVersion = context->getClientVersion();
Jamie Madill26e91952014-03-05 15:01:27 -0500939
Jamie Madill893ab082014-05-16 16:56:10 -0400940 context->getCurrentReadFormatType(&currentInternalFormat, &currentFormat, &currentType);
Jamie Madill26e91952014-03-05 15:01:27 -0500941
Geoff Langbdc9b2f2014-04-16 14:41:54 -0400942 bool validReadFormat = (clientVersion < 3) ? ValidES2ReadFormatType(context, format, type) :
943 ValidES3ReadFormatType(context, currentInternalFormat, format, type);
Jamie Madill26e91952014-03-05 15:01:27 -0500944
945 if (!(currentFormat == format && currentType == type) && !validReadFormat)
946 {
Geoff Langb1196682014-07-23 13:47:29 -0400947 context->recordError(Error(GL_INVALID_OPERATION));
948 return false;
Jamie Madill26e91952014-03-05 15:01:27 -0500949 }
950
Geoff Lang5d601382014-07-22 15:14:06 -0400951 GLenum sizedInternalFormat = GetSizedInternalFormat(format, type);
952 const InternalFormat &sizedFormatInfo = GetInternalFormatInfo(sizedInternalFormat);
Jamie Madill26e91952014-03-05 15:01:27 -0500953
Geoff Lang5d601382014-07-22 15:14:06 -0400954 GLsizei outputPitch = sizedFormatInfo.computeRowPitch(type, width, context->getState().getPackAlignment());
Jamie Madill26e91952014-03-05 15:01:27 -0500955 // sized query sanity check
956 if (bufSize)
957 {
958 int requiredSize = outputPitch * height;
959 if (requiredSize > *bufSize)
960 {
Geoff Langb1196682014-07-23 13:47:29 -0400961 context->recordError(Error(GL_INVALID_OPERATION));
962 return false;
Jamie Madill26e91952014-03-05 15:01:27 -0500963 }
964 }
965
966 return true;
967}
968
Jamie Madilldb2f14c2014-05-13 13:56:30 -0400969bool ValidateBeginQuery(gl::Context *context, GLenum target, GLuint id)
970{
971 if (!ValidQueryType(context, target))
972 {
Geoff Langb1196682014-07-23 13:47:29 -0400973 context->recordError(Error(GL_INVALID_ENUM));
974 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -0400975 }
976
977 if (id == 0)
978 {
Geoff Langb1196682014-07-23 13:47:29 -0400979 context->recordError(Error(GL_INVALID_OPERATION));
980 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -0400981 }
982
983 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
984 // of zero, if the active query object name for <target> is non-zero (for the
985 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
986 // the active query for either target is non-zero), if <id> is the name of an
987 // existing query object whose type does not match <target>, or if <id> is the
988 // active query object name for any query type, the error INVALID_OPERATION is
989 // generated.
990
991 // Ensure no other queries are active
992 // NOTE: If other queries than occlusion are supported, we will need to check
993 // separately that:
994 // a) The query ID passed is not the current active query for any target/type
995 // b) There are no active queries for the requested target (and in the case
996 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
997 // no query may be active for either if glBeginQuery targets either.
Shannon Woods53a94a82014-06-24 15:20:36 -0400998 if (context->getState().isQueryActive())
Jamie Madilldb2f14c2014-05-13 13:56:30 -0400999 {
Geoff Langb1196682014-07-23 13:47:29 -04001000 context->recordError(Error(GL_INVALID_OPERATION));
1001 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001002 }
1003
1004 Query *queryObject = context->getQuery(id, true, target);
1005
1006 // check that name was obtained with glGenQueries
1007 if (!queryObject)
1008 {
Geoff Langb1196682014-07-23 13:47:29 -04001009 context->recordError(Error(GL_INVALID_OPERATION));
1010 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001011 }
1012
1013 // check for type mismatch
1014 if (queryObject->getType() != target)
1015 {
Geoff Langb1196682014-07-23 13:47:29 -04001016 context->recordError(Error(GL_INVALID_OPERATION));
1017 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001018 }
1019
1020 return true;
1021}
1022
Jamie Madill45c785d2014-05-13 14:09:34 -04001023bool ValidateEndQuery(gl::Context *context, GLenum target)
1024{
1025 if (!ValidQueryType(context, target))
1026 {
Geoff Langb1196682014-07-23 13:47:29 -04001027 context->recordError(Error(GL_INVALID_ENUM));
1028 return false;
Jamie Madill45c785d2014-05-13 14:09:34 -04001029 }
1030
Shannon Woods53a94a82014-06-24 15:20:36 -04001031 const Query *queryObject = context->getState().getActiveQuery(target);
Jamie Madill45c785d2014-05-13 14:09:34 -04001032
1033 if (queryObject == NULL)
1034 {
Geoff Langb1196682014-07-23 13:47:29 -04001035 context->recordError(Error(GL_INVALID_OPERATION));
1036 return false;
Jamie Madill45c785d2014-05-13 14:09:34 -04001037 }
1038
Jamie Madill45c785d2014-05-13 14:09:34 -04001039 return true;
1040}
1041
Jamie Madill36398922014-05-20 14:51:53 -04001042static bool ValidateUniformCommonBase(gl::Context *context, GLenum targetUniformType,
1043 GLint location, GLsizei count, LinkedUniform **uniformOut)
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001044{
1045 if (count < 0)
1046 {
Geoff Langb1196682014-07-23 13:47:29 -04001047 context->recordError(Error(GL_INVALID_VALUE));
1048 return false;
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001049 }
1050
Shannon Woods53a94a82014-06-24 15:20:36 -04001051 gl::ProgramBinary *programBinary = context->getState().getCurrentProgramBinary();
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001052 if (!programBinary)
1053 {
Geoff Langb1196682014-07-23 13:47:29 -04001054 context->recordError(Error(GL_INVALID_OPERATION));
1055 return false;
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001056 }
1057
1058 if (location == -1)
1059 {
1060 // Silently ignore the uniform command
1061 return false;
1062 }
1063
Jamie Madill36398922014-05-20 14:51:53 -04001064 if (!programBinary->isValidUniformLocation(location))
1065 {
Geoff Langb1196682014-07-23 13:47:29 -04001066 context->recordError(Error(GL_INVALID_OPERATION));
1067 return false;
Jamie Madill36398922014-05-20 14:51:53 -04001068 }
1069
1070 LinkedUniform *uniform = programBinary->getUniformByLocation(location);
1071
1072 // attempting to write an array to a non-array uniform is an INVALID_OPERATION
1073 if (uniform->elementCount() == 1 && count > 1)
1074 {
Geoff Langb1196682014-07-23 13:47:29 -04001075 context->recordError(Error(GL_INVALID_OPERATION));
1076 return false;
Jamie Madill36398922014-05-20 14:51:53 -04001077 }
1078
1079 *uniformOut = uniform;
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001080 return true;
1081}
1082
Jamie Madillaa981bd2014-05-20 10:55:55 -04001083bool ValidateUniform(gl::Context *context, GLenum uniformType, GLint location, GLsizei count)
1084{
1085 // Check for ES3 uniform entry points
Jamie Madillf2575982014-06-25 16:04:54 -04001086 if (VariableComponentType(uniformType) == GL_UNSIGNED_INT && context->getClientVersion() < 3)
Jamie Madillaa981bd2014-05-20 10:55:55 -04001087 {
Geoff Langb1196682014-07-23 13:47:29 -04001088 context->recordError(Error(GL_INVALID_OPERATION));
1089 return false;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001090 }
1091
Jamie Madill36398922014-05-20 14:51:53 -04001092 LinkedUniform *uniform = NULL;
1093 if (!ValidateUniformCommonBase(context, uniformType, location, count, &uniform))
1094 {
1095 return false;
1096 }
1097
Jamie Madillf2575982014-06-25 16:04:54 -04001098 GLenum targetBoolType = VariableBoolVectorType(uniformType);
Jamie Madill36398922014-05-20 14:51:53 -04001099 bool samplerUniformCheck = (IsSampler(uniform->type) && uniformType == GL_INT);
1100 if (!samplerUniformCheck && uniformType != uniform->type && targetBoolType != uniform->type)
1101 {
Geoff Langb1196682014-07-23 13:47:29 -04001102 context->recordError(Error(GL_INVALID_OPERATION));
1103 return false;
Jamie Madill36398922014-05-20 14:51:53 -04001104 }
1105
1106 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001107}
1108
1109bool ValidateUniformMatrix(gl::Context *context, GLenum matrixType, GLint location, GLsizei count,
1110 GLboolean transpose)
1111{
1112 // Check for ES3 uniform entry points
1113 int rows = VariableRowCount(matrixType);
1114 int cols = VariableColumnCount(matrixType);
1115 if (rows != cols && context->getClientVersion() < 3)
1116 {
Geoff Langb1196682014-07-23 13:47:29 -04001117 context->recordError(Error(GL_INVALID_OPERATION));
1118 return false;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001119 }
1120
1121 if (transpose != GL_FALSE && context->getClientVersion() < 3)
1122 {
Geoff Langb1196682014-07-23 13:47:29 -04001123 context->recordError(Error(GL_INVALID_VALUE));
1124 return false;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001125 }
1126
Jamie Madill36398922014-05-20 14:51:53 -04001127 LinkedUniform *uniform = NULL;
1128 if (!ValidateUniformCommonBase(context, matrixType, location, count, &uniform))
1129 {
1130 return false;
1131 }
1132
1133 if (uniform->type != matrixType)
1134 {
Geoff Langb1196682014-07-23 13:47:29 -04001135 context->recordError(Error(GL_INVALID_OPERATION));
1136 return false;
Jamie Madill36398922014-05-20 14:51:53 -04001137 }
1138
1139 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001140}
1141
Jamie Madill893ab082014-05-16 16:56:10 -04001142bool ValidateStateQuery(gl::Context *context, GLenum pname, GLenum *nativeType, unsigned int *numParams)
1143{
1144 if (!context->getQueryParameterInfo(pname, nativeType, numParams))
1145 {
Geoff Langb1196682014-07-23 13:47:29 -04001146 context->recordError(Error(GL_INVALID_ENUM));
1147 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001148 }
1149
1150 if (pname >= GL_DRAW_BUFFER0 && pname <= GL_DRAW_BUFFER15)
1151 {
1152 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0);
1153
Geoff Langaae65a42014-05-26 12:43:44 -04001154 if (colorAttachment >= context->getCaps().maxDrawBuffers)
Jamie Madill893ab082014-05-16 16:56:10 -04001155 {
Geoff Langb1196682014-07-23 13:47:29 -04001156 context->recordError(Error(GL_INVALID_OPERATION));
1157 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001158 }
1159 }
1160
1161 switch (pname)
1162 {
1163 case GL_TEXTURE_BINDING_2D:
1164 case GL_TEXTURE_BINDING_CUBE_MAP:
1165 case GL_TEXTURE_BINDING_3D:
1166 case GL_TEXTURE_BINDING_2D_ARRAY:
Geoff Lang3a61c322014-07-10 13:01:54 -04001167 if (context->getState().getActiveSampler() >= context->getCaps().maxCombinedTextureImageUnits)
Jamie Madill893ab082014-05-16 16:56:10 -04001168 {
Geoff Langb1196682014-07-23 13:47:29 -04001169 context->recordError(Error(GL_INVALID_OPERATION));
1170 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001171 }
1172 break;
1173
1174 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1175 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1176 {
Shannon Woods53a94a82014-06-24 15:20:36 -04001177 Framebuffer *framebuffer = context->getState().getReadFramebuffer();
Jamie Madill893ab082014-05-16 16:56:10 -04001178 ASSERT(framebuffer);
1179 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1180 {
Geoff Langb1196682014-07-23 13:47:29 -04001181 context->recordError(Error(GL_INVALID_OPERATION));
1182 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001183 }
1184
Jamie Madill3c7fa222014-06-05 13:08:51 -04001185 FramebufferAttachment *attachment = framebuffer->getReadColorbuffer();
1186 if (!attachment)
Jamie Madill893ab082014-05-16 16:56:10 -04001187 {
Geoff Langb1196682014-07-23 13:47:29 -04001188 context->recordError(Error(GL_INVALID_OPERATION));
1189 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001190 }
1191 }
1192 break;
1193
1194 default:
1195 break;
1196 }
1197
1198 // pname is valid, but there are no parameters to return
1199 if (numParams == 0)
1200 {
1201 return false;
1202 }
1203
1204 return true;
1205}
1206
Jamie Madill560a8d82014-05-21 13:06:20 -04001207bool ValidateCopyTexImageParametersBase(gl::Context* context, GLenum target, GLint level, GLenum internalformat, bool isSubImage,
1208 GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height,
1209 GLint border, GLenum *textureFormatOut)
1210{
1211
1212 if (!ValidTexture2DDestinationTarget(context, target))
1213 {
Geoff Langb1196682014-07-23 13:47:29 -04001214 context->recordError(Error(GL_INVALID_ENUM));
1215 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001216 }
1217
1218 if (level < 0 || xoffset < 0 || yoffset < 0 || zoffset < 0 || width < 0 || height < 0)
1219 {
Geoff Langb1196682014-07-23 13:47:29 -04001220 context->recordError(Error(GL_INVALID_VALUE));
1221 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001222 }
1223
1224 if (std::numeric_limits<GLsizei>::max() - xoffset < width || std::numeric_limits<GLsizei>::max() - yoffset < height)
1225 {
Geoff Langb1196682014-07-23 13:47:29 -04001226 context->recordError(Error(GL_INVALID_VALUE));
1227 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001228 }
1229
1230 if (border != 0)
1231 {
Geoff Langb1196682014-07-23 13:47:29 -04001232 context->recordError(Error(GL_INVALID_VALUE));
1233 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001234 }
1235
1236 if (!ValidMipLevel(context, target, level))
1237 {
Geoff Langb1196682014-07-23 13:47:29 -04001238 context->recordError(Error(GL_INVALID_VALUE));
1239 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001240 }
1241
Shannon Woods53a94a82014-06-24 15:20:36 -04001242 gl::Framebuffer *framebuffer = context->getState().getReadFramebuffer();
Jamie Madill560a8d82014-05-21 13:06:20 -04001243 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1244 {
Geoff Langb1196682014-07-23 13:47:29 -04001245 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
1246 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001247 }
1248
Shannon Woods53a94a82014-06-24 15:20:36 -04001249 if (context->getState().getReadFramebuffer()->id() != 0 && framebuffer->getSamples() != 0)
Jamie Madill560a8d82014-05-21 13:06:20 -04001250 {
Geoff Langb1196682014-07-23 13:47:29 -04001251 context->recordError(Error(GL_INVALID_OPERATION));
1252 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001253 }
1254
Geoff Langaae65a42014-05-26 12:43:44 -04001255 const gl::Caps &caps = context->getCaps();
1256
Jamie Madill560a8d82014-05-21 13:06:20 -04001257 gl::Texture *texture = NULL;
1258 GLenum textureInternalFormat = GL_NONE;
Jamie Madill560a8d82014-05-21 13:06:20 -04001259 GLint textureLevelWidth = 0;
1260 GLint textureLevelHeight = 0;
1261 GLint textureLevelDepth = 0;
Geoff Langaae65a42014-05-26 12:43:44 -04001262 GLuint maxDimension = 0;
Jamie Madill560a8d82014-05-21 13:06:20 -04001263
1264 switch (target)
1265 {
1266 case GL_TEXTURE_2D:
1267 {
1268 gl::Texture2D *texture2d = context->getTexture2D();
1269 if (texture2d)
1270 {
1271 textureInternalFormat = texture2d->getInternalFormat(level);
Jamie Madill560a8d82014-05-21 13:06:20 -04001272 textureLevelWidth = texture2d->getWidth(level);
1273 textureLevelHeight = texture2d->getHeight(level);
1274 textureLevelDepth = 1;
1275 texture = texture2d;
Geoff Langaae65a42014-05-26 12:43:44 -04001276 maxDimension = caps.max2DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001277 }
1278 }
1279 break;
1280
1281 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1282 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1283 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1284 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1285 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1286 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1287 {
1288 gl::TextureCubeMap *textureCube = context->getTextureCubeMap();
1289 if (textureCube)
1290 {
1291 textureInternalFormat = textureCube->getInternalFormat(target, level);
Jamie Madill560a8d82014-05-21 13:06:20 -04001292 textureLevelWidth = textureCube->getWidth(target, level);
1293 textureLevelHeight = textureCube->getHeight(target, level);
1294 textureLevelDepth = 1;
1295 texture = textureCube;
Geoff Langaae65a42014-05-26 12:43:44 -04001296 maxDimension = caps.maxCubeMapTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001297 }
1298 }
1299 break;
1300
1301 case GL_TEXTURE_2D_ARRAY:
1302 {
1303 gl::Texture2DArray *texture2dArray = context->getTexture2DArray();
1304 if (texture2dArray)
1305 {
1306 textureInternalFormat = texture2dArray->getInternalFormat(level);
Jamie Madill560a8d82014-05-21 13:06:20 -04001307 textureLevelWidth = texture2dArray->getWidth(level);
1308 textureLevelHeight = texture2dArray->getHeight(level);
1309 textureLevelDepth = texture2dArray->getLayers(level);
1310 texture = texture2dArray;
Geoff Langaae65a42014-05-26 12:43:44 -04001311 maxDimension = caps.max2DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001312 }
1313 }
1314 break;
1315
1316 case GL_TEXTURE_3D:
1317 {
1318 gl::Texture3D *texture3d = context->getTexture3D();
1319 if (texture3d)
1320 {
1321 textureInternalFormat = texture3d->getInternalFormat(level);
Jamie Madill560a8d82014-05-21 13:06:20 -04001322 textureLevelWidth = texture3d->getWidth(level);
1323 textureLevelHeight = texture3d->getHeight(level);
1324 textureLevelDepth = texture3d->getDepth(level);
1325 texture = texture3d;
Geoff Langaae65a42014-05-26 12:43:44 -04001326 maxDimension = caps.max3DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001327 }
1328 }
1329 break;
1330
1331 default:
Geoff Langb1196682014-07-23 13:47:29 -04001332 context->recordError(Error(GL_INVALID_ENUM));
1333 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001334 }
1335
1336 if (!texture)
1337 {
Geoff Langb1196682014-07-23 13:47:29 -04001338 context->recordError(Error(GL_INVALID_OPERATION));
1339 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001340 }
1341
1342 if (texture->isImmutable() && !isSubImage)
1343 {
Geoff Langb1196682014-07-23 13:47:29 -04001344 context->recordError(Error(GL_INVALID_OPERATION));
1345 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001346 }
1347
Geoff Lang5d601382014-07-22 15:14:06 -04001348 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalformat);
1349
1350 if (formatInfo.depthBits > 0)
Jamie Madill560a8d82014-05-21 13:06:20 -04001351 {
Geoff Langb1196682014-07-23 13:47:29 -04001352 context->recordError(Error(GL_INVALID_OPERATION));
1353 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001354 }
1355
Geoff Lang5d601382014-07-22 15:14:06 -04001356 if (formatInfo.compressed)
Jamie Madill560a8d82014-05-21 13:06:20 -04001357 {
Geoff Lang5d601382014-07-22 15:14:06 -04001358 if (((width % formatInfo.compressedBlockWidth) != 0 && width != textureLevelWidth) ||
1359 ((height % formatInfo.compressedBlockHeight) != 0 && height != textureLevelHeight))
Jamie Madill560a8d82014-05-21 13:06:20 -04001360 {
Geoff Langb1196682014-07-23 13:47:29 -04001361 context->recordError(Error(GL_INVALID_OPERATION));
1362 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001363 }
1364 }
1365
1366 if (isSubImage)
1367 {
1368 if (xoffset + width > textureLevelWidth ||
1369 yoffset + height > textureLevelHeight ||
1370 zoffset >= textureLevelDepth)
1371 {
Geoff Langb1196682014-07-23 13:47:29 -04001372 context->recordError(Error(GL_INVALID_VALUE));
1373 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001374 }
1375 }
Jamie Madill6f38f822014-06-06 17:12:20 -04001376 else
1377 {
1378 if (IsCubemapTextureTarget(target) && width != height)
1379 {
Geoff Langb1196682014-07-23 13:47:29 -04001380 context->recordError(Error(GL_INVALID_VALUE));
1381 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04001382 }
1383
Geoff Lang5d601382014-07-22 15:14:06 -04001384 if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions()))
Jamie Madill6f38f822014-06-06 17:12:20 -04001385 {
Geoff Langb1196682014-07-23 13:47:29 -04001386 context->recordError(Error(GL_INVALID_ENUM));
1387 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04001388 }
1389
1390 int maxLevelDimension = (maxDimension >> level);
1391 if (static_cast<int>(width) > maxLevelDimension || static_cast<int>(height) > maxLevelDimension)
1392 {
Geoff Langb1196682014-07-23 13:47:29 -04001393 context->recordError(Error(GL_INVALID_VALUE));
1394 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04001395 }
1396 }
Jamie Madill560a8d82014-05-21 13:06:20 -04001397
1398 *textureFormatOut = textureInternalFormat;
1399 return true;
1400}
1401
Geoff Langb1196682014-07-23 13:47:29 -04001402static bool ValidateDrawBase(Context *context, GLenum mode, GLsizei count, GLsizei maxVertex, GLsizei primcount)
Jamie Madill250d33f2014-06-06 17:09:03 -04001403{
Jamie Madill1aeb1312014-06-20 13:21:25 -04001404 switch (mode)
1405 {
1406 case GL_POINTS:
1407 case GL_LINES:
1408 case GL_LINE_LOOP:
1409 case GL_LINE_STRIP:
1410 case GL_TRIANGLES:
1411 case GL_TRIANGLE_STRIP:
1412 case GL_TRIANGLE_FAN:
1413 break;
1414 default:
Geoff Langb1196682014-07-23 13:47:29 -04001415 context->recordError(Error(GL_INVALID_ENUM));
1416 return false;
Jamie Madill1aeb1312014-06-20 13:21:25 -04001417 }
1418
Jamie Madill250d33f2014-06-06 17:09:03 -04001419 if (count < 0)
1420 {
Geoff Langb1196682014-07-23 13:47:29 -04001421 context->recordError(Error(GL_INVALID_VALUE));
1422 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001423 }
1424
Geoff Langb1196682014-07-23 13:47:29 -04001425 const State &state = context->getState();
1426
Jamie Madill250d33f2014-06-06 17:09:03 -04001427 // Check for mapped buffers
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001428 if (state.hasMappedBuffer(GL_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001429 {
Geoff Langb1196682014-07-23 13:47:29 -04001430 context->recordError(Error(GL_INVALID_OPERATION));
1431 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001432 }
1433
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001434 const gl::DepthStencilState &depthStencilState = state.getDepthStencilState();
Jamie Madillac528012014-06-20 13:21:23 -04001435 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001436 state.getStencilRef() != state.getStencilBackRef() ||
Jamie Madillac528012014-06-20 13:21:23 -04001437 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
1438 {
1439 // Note: these separate values are not supported in WebGL, due to D3D's limitations.
1440 // See Section 6.10 of the WebGL 1.0 spec
1441 ERR("This ANGLE implementation does not support separate front/back stencil "
1442 "writemasks, reference values, or stencil mask values.");
Geoff Langb1196682014-07-23 13:47:29 -04001443 context->recordError(Error(GL_INVALID_OPERATION));
1444 return false;
Jamie Madillac528012014-06-20 13:21:23 -04001445 }
1446
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001447 const gl::Framebuffer *fbo = state.getDrawFramebuffer();
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001448 if (!fbo || fbo->completeness() != GL_FRAMEBUFFER_COMPLETE)
1449 {
Geoff Langb1196682014-07-23 13:47:29 -04001450 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
1451 return false;
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001452 }
1453
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001454 if (state.getCurrentProgramId() == 0)
Jamie Madilld4cfa572014-07-08 10:00:32 -04001455 {
Geoff Langb1196682014-07-23 13:47:29 -04001456 context->recordError(Error(GL_INVALID_OPERATION));
1457 return false;
Jamie Madilld4cfa572014-07-08 10:00:32 -04001458 }
1459
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001460 gl::ProgramBinary *programBinary = state.getCurrentProgramBinary();
Brandon Jones43a53e22014-08-28 16:23:22 -07001461 if (!programBinary->validateSamplers(NULL, context->getCaps()))
Jamie Madilld4cfa572014-07-08 10:00:32 -04001462 {
Geoff Langb1196682014-07-23 13:47:29 -04001463 context->recordError(Error(GL_INVALID_OPERATION));
1464 return false;
Jamie Madilld4cfa572014-07-08 10:00:32 -04001465 }
1466
Jamie Madill2b976812014-08-25 15:47:49 -04001467 // Buffer validations
1468 const VertexArray *vao = state.getVertexArray();
1469 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
1470 {
1471 const VertexAttribute &attrib = vao->getVertexAttribute(attributeIndex);
1472 bool attribActive = (programBinary->getSemanticIndex(attributeIndex) != -1);
1473 if (attribActive && attrib.enabled)
1474 {
1475 gl::Buffer *buffer = attrib.buffer.get();
1476
1477 if (buffer)
1478 {
1479 GLint64 attribStride = static_cast<GLint64>(ComputeVertexAttributeStride(attrib));
1480 GLint64 maxVertexElement = 0;
1481
1482 if (attrib.divisor > 0)
1483 {
1484 maxVertexElement = static_cast<GLint64>(primcount) / static_cast<GLint64>(attrib.divisor);
1485 }
1486 else
1487 {
1488 maxVertexElement = static_cast<GLint64>(maxVertex);
1489 }
1490
1491 GLint64 attribDataSize = maxVertexElement * attribStride;
1492
1493 // [OpenGL ES 3.0.2] section 2.9.4 page 40:
1494 // We can return INVALID_OPERATION if our vertex attribute does not have
1495 // enough backing data.
1496 if (attribDataSize > buffer->getSize())
1497 {
Geoff Langb1196682014-07-23 13:47:29 -04001498 context->recordError(Error(GL_INVALID_OPERATION));
1499 return false;
Jamie Madill2b976812014-08-25 15:47:49 -04001500 }
1501 }
1502 else if (attrib.pointer == NULL)
1503 {
1504 // This is an application error that would normally result in a crash,
1505 // but we catch it and return an error
Geoff Langb1196682014-07-23 13:47:29 -04001506 context->recordError(Error(GL_INVALID_OPERATION, "An enabled vertex array has no buffer and no pointer."));
1507 return false;
Jamie Madill2b976812014-08-25 15:47:49 -04001508 }
1509 }
1510 }
1511
Jamie Madill250d33f2014-06-06 17:09:03 -04001512 // No-op if zero count
1513 return (count > 0);
1514}
1515
Geoff Langb1196682014-07-23 13:47:29 -04001516bool ValidateDrawArrays(Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
Jamie Madill250d33f2014-06-06 17:09:03 -04001517{
Jamie Madillfd716582014-06-06 17:09:04 -04001518 if (first < 0)
Jamie Madill250d33f2014-06-06 17:09:03 -04001519 {
Geoff Langb1196682014-07-23 13:47:29 -04001520 context->recordError(Error(GL_INVALID_VALUE));
1521 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001522 }
1523
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001524 const State &state = context->getState();
1525 gl::TransformFeedback *curTransformFeedback = state.getCurrentTransformFeedback();
Jamie Madillfd716582014-06-06 17:09:04 -04001526 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused() &&
1527 curTransformFeedback->getDrawMode() != mode)
1528 {
1529 // It is an invalid operation to call DrawArrays or DrawArraysInstanced with a draw mode
1530 // that does not match the current transform feedback object's draw mode (if transform feedback
1531 // is active), (3.0.2, section 2.14, pg 86)
Geoff Langb1196682014-07-23 13:47:29 -04001532 context->recordError(Error(GL_INVALID_OPERATION));
1533 return false;
Jamie Madillfd716582014-06-06 17:09:04 -04001534 }
1535
Geoff Langb1196682014-07-23 13:47:29 -04001536 if (!ValidateDrawBase(context, mode, count, count, primcount))
Jamie Madillfd716582014-06-06 17:09:04 -04001537 {
1538 return false;
1539 }
1540
1541 return true;
1542}
1543
Geoff Langb1196682014-07-23 13:47:29 -04001544bool ValidateDrawArraysInstanced(Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
Jamie Madillfd716582014-06-06 17:09:04 -04001545{
1546 if (primcount < 0)
1547 {
Geoff Langb1196682014-07-23 13:47:29 -04001548 context->recordError(Error(GL_INVALID_VALUE));
1549 return false;
Jamie Madillfd716582014-06-06 17:09:04 -04001550 }
1551
Jamie Madill2b976812014-08-25 15:47:49 -04001552 if (!ValidateDrawArrays(context, mode, first, count, primcount))
Jamie Madillfd716582014-06-06 17:09:04 -04001553 {
1554 return false;
1555 }
1556
1557 // No-op if zero primitive count
1558 return (primcount > 0);
1559}
1560
Geoff Lang87a93302014-09-16 13:29:43 -04001561static bool ValidateDrawInstancedANGLE(Context *context)
1562{
1563 // Verify there is at least one active attribute with a divisor of zero
1564 const gl::State& state = context->getState();
1565
1566 gl::ProgramBinary *programBinary = state.getCurrentProgramBinary();
1567
1568 const VertexArray *vao = state.getVertexArray();
1569 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
1570 {
1571 const VertexAttribute &attrib = vao->getVertexAttribute(attributeIndex);
1572 bool active = (programBinary->getSemanticIndex(attributeIndex) != -1);
1573 if (active && attrib.divisor == 0)
1574 {
1575 return true;
1576 }
1577 }
1578
1579 context->recordError(Error(GL_INVALID_OPERATION, "ANGLE_instanced_arrays requires that at least one active attribute"
1580 "has a divisor of zero."));
1581 return false;
1582}
1583
1584bool ValidateDrawArraysInstancedANGLE(Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
1585{
1586 if (!ValidateDrawInstancedANGLE(context))
1587 {
1588 return false;
1589 }
1590
1591 return ValidateDrawArraysInstanced(context, mode, first, count, primcount);
1592}
1593
Geoff Langb1196682014-07-23 13:47:29 -04001594bool ValidateDrawElements(Context *context, GLenum mode, GLsizei count, GLenum type,
Jamie Madill2b976812014-08-25 15:47:49 -04001595 const GLvoid* indices, GLsizei primcount, rx::RangeUI *indexRangeOut)
Jamie Madillfd716582014-06-06 17:09:04 -04001596{
Jamie Madill250d33f2014-06-06 17:09:03 -04001597 switch (type)
1598 {
1599 case GL_UNSIGNED_BYTE:
1600 case GL_UNSIGNED_SHORT:
1601 break;
1602 case GL_UNSIGNED_INT:
Geoff Langc0b9ef42014-07-02 10:02:37 -04001603 if (!context->getExtensions().elementIndexUint)
Jamie Madill250d33f2014-06-06 17:09:03 -04001604 {
Geoff Langb1196682014-07-23 13:47:29 -04001605 context->recordError(Error(GL_INVALID_ENUM));
1606 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001607 }
1608 break;
1609 default:
Geoff Langb1196682014-07-23 13:47:29 -04001610 context->recordError(Error(GL_INVALID_ENUM));
1611 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001612 }
1613
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001614 const State &state = context->getState();
1615
1616 gl::TransformFeedback *curTransformFeedback = state.getCurrentTransformFeedback();
Jamie Madill250d33f2014-06-06 17:09:03 -04001617 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused())
1618 {
1619 // It is an invalid operation to call DrawElements, DrawRangeElements or DrawElementsInstanced
1620 // while transform feedback is active, (3.0.2, section 2.14, pg 86)
Geoff Langb1196682014-07-23 13:47:29 -04001621 context->recordError(Error(GL_INVALID_OPERATION));
1622 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001623 }
1624
1625 // Check for mapped buffers
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001626 if (state.hasMappedBuffer(GL_ELEMENT_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001627 {
Geoff Langb1196682014-07-23 13:47:29 -04001628 context->recordError(Error(GL_INVALID_OPERATION));
1629 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001630 }
1631
Jamie Madill2b976812014-08-25 15:47:49 -04001632 const gl::VertexArray *vao = state.getVertexArray();
1633 const gl::Buffer *elementArrayBuffer = vao->getElementArrayBuffer();
1634 if (!indices && !elementArrayBuffer)
Jamie Madilld4cfa572014-07-08 10:00:32 -04001635 {
Geoff Langb1196682014-07-23 13:47:29 -04001636 context->recordError(Error(GL_INVALID_OPERATION));
1637 return false;
Jamie Madilld4cfa572014-07-08 10:00:32 -04001638 }
1639
Jamie Madillae3000b2014-08-25 15:47:51 -04001640 if (elementArrayBuffer)
1641 {
1642 const gl::Type &typeInfo = gl::GetTypeInfo(type);
1643
1644 GLint64 offset = reinterpret_cast<GLint64>(indices);
1645 GLint64 byteCount = static_cast<GLint64>(typeInfo.bytes) * static_cast<GLint64>(count)+offset;
1646
1647 // check for integer overflows
1648 if (static_cast<GLuint>(count) > (std::numeric_limits<GLuint>::max() / typeInfo.bytes) ||
1649 byteCount > static_cast<GLint64>(std::numeric_limits<GLuint>::max()))
1650 {
Geoff Langb1196682014-07-23 13:47:29 -04001651 context->recordError(Error(GL_OUT_OF_MEMORY));
1652 return false;
Jamie Madillae3000b2014-08-25 15:47:51 -04001653 }
1654
1655 // Check for reading past the end of the bound buffer object
1656 if (byteCount > elementArrayBuffer->getSize())
1657 {
Geoff Langb1196682014-07-23 13:47:29 -04001658 context->recordError(Error(GL_INVALID_OPERATION));
1659 return false;
Jamie Madillae3000b2014-08-25 15:47:51 -04001660 }
1661 }
1662 else if (!indices)
1663 {
1664 // Catch this programming error here
Geoff Langb1196682014-07-23 13:47:29 -04001665 context->recordError(Error(GL_INVALID_OPERATION));
1666 return false;
Jamie Madillae3000b2014-08-25 15:47:51 -04001667 }
1668
Jamie Madill2b976812014-08-25 15:47:49 -04001669 // Use max index to validate if our vertex buffers are large enough for the pull.
1670 // TODO: offer fast path, with disabled index validation.
1671 // TODO: also disable index checking on back-ends that are robust to out-of-range accesses.
1672 if (elementArrayBuffer)
1673 {
Jacek Cabana5521de2014-10-01 17:23:46 +02001674 uintptr_t offset = reinterpret_cast<uintptr_t>(indices);
Jamie Madill2b976812014-08-25 15:47:49 -04001675 if (!elementArrayBuffer->getIndexRangeCache()->findRange(type, offset, count, indexRangeOut, NULL))
1676 {
Jamie Madill9ae396b2014-10-21 17:46:30 -04001677 // FIXME(jmadill): Use buffer data caching instead of the D3D back-end
1678 rx::BufferD3D *bufferD3D = rx::BufferD3D::makeBufferD3D(elementArrayBuffer->getImplementation());
Geoff Langc8d297a2014-09-19 11:09:08 -04001679 const uint8_t *dataPointer = NULL;
Jamie Madill9ae396b2014-10-21 17:46:30 -04001680 Error error = bufferD3D->getData(&dataPointer);
Geoff Langc8d297a2014-09-19 11:09:08 -04001681 if (error.isError())
1682 {
1683 context->recordError(error);
1684 return false;
1685 }
1686
1687 const uint8_t *offsetPointer = dataPointer + offset;
Jamie Madill2b976812014-08-25 15:47:49 -04001688 *indexRangeOut = rx::IndexRangeCache::ComputeRange(type, offsetPointer, count);
1689 }
1690 }
1691 else
1692 {
1693 *indexRangeOut = rx::IndexRangeCache::ComputeRange(type, indices, count);
1694 }
1695
Geoff Langb1196682014-07-23 13:47:29 -04001696 if (!ValidateDrawBase(context, mode, count, static_cast<GLsizei>(indexRangeOut->end), primcount))
Jamie Madillfd716582014-06-06 17:09:04 -04001697 {
1698 return false;
1699 }
1700
1701 return true;
1702}
1703
Geoff Langb1196682014-07-23 13:47:29 -04001704bool ValidateDrawElementsInstanced(Context *context,
Jamie Madill2b976812014-08-25 15:47:49 -04001705 GLenum mode, GLsizei count, GLenum type,
1706 const GLvoid *indices, GLsizei primcount,
1707 rx::RangeUI *indexRangeOut)
Jamie Madillfd716582014-06-06 17:09:04 -04001708{
1709 if (primcount < 0)
1710 {
Geoff Langb1196682014-07-23 13:47:29 -04001711 context->recordError(Error(GL_INVALID_VALUE));
1712 return false;
Jamie Madillfd716582014-06-06 17:09:04 -04001713 }
1714
Jamie Madill2b976812014-08-25 15:47:49 -04001715 if (!ValidateDrawElements(context, mode, count, type, indices, primcount, indexRangeOut))
Jamie Madillfd716582014-06-06 17:09:04 -04001716 {
1717 return false;
1718 }
1719
1720 // No-op zero primitive count
1721 return (primcount > 0);
Jamie Madill250d33f2014-06-06 17:09:03 -04001722}
1723
Geoff Lang87a93302014-09-16 13:29:43 -04001724bool ValidateDrawElementsInstancedANGLE(Context *context, GLenum mode, GLsizei count, GLenum type,
1725 const GLvoid *indices, GLsizei primcount, rx::RangeUI *indexRangeOut)
1726{
1727 if (!ValidateDrawInstancedANGLE(context))
1728 {
1729 return false;
1730 }
1731
1732 return ValidateDrawElementsInstanced(context, mode, count, type, indices, primcount, indexRangeOut);
1733}
1734
Geoff Langb1196682014-07-23 13:47:29 -04001735bool ValidateFramebufferTextureBase(Context *context, GLenum target, GLenum attachment,
Jamie Madill55ec3b12014-07-03 10:38:57 -04001736 GLuint texture, GLint level)
Jamie Madill570f7c82014-07-03 10:38:54 -04001737{
Jamie Madill55ec3b12014-07-03 10:38:57 -04001738 if (!ValidFramebufferTarget(target))
1739 {
Geoff Langb1196682014-07-23 13:47:29 -04001740 context->recordError(Error(GL_INVALID_ENUM));
1741 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001742 }
1743
1744 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill570f7c82014-07-03 10:38:54 -04001745 {
1746 return false;
1747 }
1748
Jamie Madill55ec3b12014-07-03 10:38:57 -04001749 if (texture != 0)
1750 {
1751 gl::Texture *tex = context->getTexture(texture);
1752
1753 if (tex == NULL)
1754 {
Geoff Langb1196682014-07-23 13:47:29 -04001755 context->recordError(Error(GL_INVALID_OPERATION));
1756 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001757 }
1758
1759 if (level < 0)
1760 {
Geoff Langb1196682014-07-23 13:47:29 -04001761 context->recordError(Error(GL_INVALID_VALUE));
1762 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001763 }
1764 }
1765
Shannon Woods53a94a82014-06-24 15:20:36 -04001766 const gl::Framebuffer *framebuffer = context->getState().getTargetFramebuffer(target);
1767 GLuint framebufferHandle = context->getState().getTargetFramebuffer(target)->id();
Jamie Madill55ec3b12014-07-03 10:38:57 -04001768
1769 if (framebufferHandle == 0 || !framebuffer)
1770 {
Geoff Langb1196682014-07-23 13:47:29 -04001771 context->recordError(Error(GL_INVALID_OPERATION));
1772 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001773 }
1774
1775 return true;
1776}
1777
Geoff Langb1196682014-07-23 13:47:29 -04001778bool ValidateFramebufferTexture2D(Context *context, GLenum target, GLenum attachment,
Jamie Madill55ec3b12014-07-03 10:38:57 -04001779 GLenum textarget, GLuint texture, GLint level)
1780{
1781 // Attachments are required to be bound to level 0 in ES2
1782 if (context->getClientVersion() < 3 && level != 0)
1783 {
Geoff Langb1196682014-07-23 13:47:29 -04001784 context->recordError(Error(GL_INVALID_VALUE));
1785 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001786 }
1787
1788 if (!ValidateFramebufferTextureBase(context, target, attachment, texture, level))
Jamie Madill570f7c82014-07-03 10:38:54 -04001789 {
1790 return false;
1791 }
1792
Jamie Madill55ec3b12014-07-03 10:38:57 -04001793 if (texture != 0)
1794 {
1795 gl::Texture *tex = context->getTexture(texture);
1796 ASSERT(tex);
1797
Jamie Madill2a6564e2014-07-11 09:53:19 -04001798 const gl::Caps &caps = context->getCaps();
1799
Jamie Madill55ec3b12014-07-03 10:38:57 -04001800 switch (textarget)
1801 {
1802 case GL_TEXTURE_2D:
1803 {
Jamie Madill2a6564e2014-07-11 09:53:19 -04001804 if (level > gl::log2(caps.max2DTextureSize))
Jamie Madill55ec3b12014-07-03 10:38:57 -04001805 {
Geoff Langb1196682014-07-23 13:47:29 -04001806 context->recordError(Error(GL_INVALID_VALUE));
1807 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001808 }
1809 if (tex->getTarget() != GL_TEXTURE_2D)
1810 {
Geoff Langb1196682014-07-23 13:47:29 -04001811 context->recordError(Error(GL_INVALID_OPERATION));
1812 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001813 }
1814 gl::Texture2D *tex2d = static_cast<gl::Texture2D *>(tex);
1815 if (tex2d->isCompressed(level))
1816 {
Geoff Langb1196682014-07-23 13:47:29 -04001817 context->recordError(Error(GL_INVALID_OPERATION));
1818 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001819 }
1820 }
1821 break;
1822
1823 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1824 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1825 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1826 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1827 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1828 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1829 {
Jamie Madill2a6564e2014-07-11 09:53:19 -04001830 if (level > gl::log2(caps.maxCubeMapTextureSize))
Jamie Madill55ec3b12014-07-03 10:38:57 -04001831 {
Geoff Langb1196682014-07-23 13:47:29 -04001832 context->recordError(Error(GL_INVALID_VALUE));
1833 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001834 }
1835 if (tex->getTarget() != GL_TEXTURE_CUBE_MAP)
1836 {
Geoff Langb1196682014-07-23 13:47:29 -04001837 context->recordError(Error(GL_INVALID_OPERATION));
1838 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001839 }
1840 gl::TextureCubeMap *texcube = static_cast<gl::TextureCubeMap *>(tex);
1841 if (texcube->isCompressed(textarget, level))
1842 {
Geoff Langb1196682014-07-23 13:47:29 -04001843 context->recordError(Error(GL_INVALID_OPERATION));
1844 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001845 }
1846 }
1847 break;
1848
1849 default:
Geoff Langb1196682014-07-23 13:47:29 -04001850 context->recordError(Error(GL_INVALID_ENUM));
1851 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001852 }
1853 }
1854
Jamie Madill570f7c82014-07-03 10:38:54 -04001855 return true;
1856}
1857
Geoff Langb1196682014-07-23 13:47:29 -04001858bool ValidateGetUniformBase(Context *context, GLuint program, GLint location)
Jamie Madill0063c512014-08-25 15:47:53 -04001859{
1860 if (program == 0)
1861 {
Geoff Langb1196682014-07-23 13:47:29 -04001862 context->recordError(Error(GL_INVALID_VALUE));
1863 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001864 }
1865
1866 gl::Program *programObject = context->getProgram(program);
1867
1868 if (!programObject || !programObject->isLinked())
1869 {
Geoff Langb1196682014-07-23 13:47:29 -04001870 context->recordError(Error(GL_INVALID_OPERATION));
1871 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001872 }
1873
1874 gl::ProgramBinary *programBinary = programObject->getProgramBinary();
1875 if (!programBinary)
1876 {
Geoff Langb1196682014-07-23 13:47:29 -04001877 context->recordError(Error(GL_INVALID_OPERATION));
1878 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001879 }
1880
Jamie Madill549c7fd2014-08-25 15:47:56 -04001881 if (!programBinary->isValidUniformLocation(location))
1882 {
Geoff Langb1196682014-07-23 13:47:29 -04001883 context->recordError(Error(GL_INVALID_OPERATION));
1884 return false;
Jamie Madill549c7fd2014-08-25 15:47:56 -04001885 }
1886
Jamie Madill0063c512014-08-25 15:47:53 -04001887 return true;
1888}
1889
Geoff Langb1196682014-07-23 13:47:29 -04001890bool ValidateGetUniformfv(Context *context, GLuint program, GLint location, GLfloat* params)
Jamie Madill78f41802014-08-25 15:47:55 -04001891{
1892 return ValidateGetUniformBase(context, program, location);
1893}
1894
Geoff Langb1196682014-07-23 13:47:29 -04001895bool ValidateGetUniformiv(Context *context, GLuint program, GLint location, GLint* params)
Jamie Madill0063c512014-08-25 15:47:53 -04001896{
Jamie Madill78f41802014-08-25 15:47:55 -04001897 return ValidateGetUniformBase(context, program, location);
1898}
1899
Geoff Langb1196682014-07-23 13:47:29 -04001900static bool ValidateSizedGetUniform(Context *context, GLuint program, GLint location, GLsizei bufSize)
Jamie Madill78f41802014-08-25 15:47:55 -04001901{
1902 if (!ValidateGetUniformBase(context, program, location))
Jamie Madill0063c512014-08-25 15:47:53 -04001903 {
Jamie Madill78f41802014-08-25 15:47:55 -04001904 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001905 }
1906
Jamie Madilla502c742014-08-28 17:19:13 -04001907 gl::Program *programObject = context->getProgram(program);
1908 ASSERT(programObject);
1909 gl::ProgramBinary *programBinary = programObject->getProgramBinary();
Jamie Madill0063c512014-08-25 15:47:53 -04001910
Jamie Madill78f41802014-08-25 15:47:55 -04001911 // sized queries -- ensure the provided buffer is large enough
1912 LinkedUniform *uniform = programBinary->getUniformByLocation(location);
1913 size_t requiredBytes = VariableExternalSize(uniform->type);
1914 if (static_cast<size_t>(bufSize) < requiredBytes)
Jamie Madill0063c512014-08-25 15:47:53 -04001915 {
Geoff Langb1196682014-07-23 13:47:29 -04001916 context->recordError(Error(GL_INVALID_OPERATION));
1917 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001918 }
1919
1920 return true;
1921}
1922
Geoff Langb1196682014-07-23 13:47:29 -04001923bool ValidateGetnUniformfvEXT(Context *context, GLuint program, GLint location, GLsizei bufSize, GLfloat* params)
Jamie Madill0063c512014-08-25 15:47:53 -04001924{
Jamie Madill78f41802014-08-25 15:47:55 -04001925 return ValidateSizedGetUniform(context, program, location, bufSize);
Jamie Madill0063c512014-08-25 15:47:53 -04001926}
1927
Geoff Langb1196682014-07-23 13:47:29 -04001928bool ValidateGetnUniformivEXT(Context *context, GLuint program, GLint location, GLsizei bufSize, GLint* params)
Jamie Madill0063c512014-08-25 15:47:53 -04001929{
Jamie Madill78f41802014-08-25 15:47:55 -04001930 return ValidateSizedGetUniform(context, program, location, bufSize);
Jamie Madill0063c512014-08-25 15:47:53 -04001931}
1932
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001933}