blob: 9146be36109b29eae3bc5badf0ba6f32d9cdcd9d [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();
Jamie Madill48faf802014-11-06 15:27:22 -0500503
504 if (!readFramebuffer || !drawFramebuffer)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400505 {
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
Jamie Madill48faf802014-11-06 15:27:22 -0500510 if (!readFramebuffer->completeness(context->getData()))
511 {
512 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
513 return false;
514 }
515
516 if (!drawFramebuffer->completeness(context->getData()))
517 {
518 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
519 return false;
520 }
521
522 if (drawFramebuffer->getSamples(context->getData()) != 0)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400523 {
Geoff Langb1196682014-07-23 13:47:29 -0400524 context->recordError(Error(GL_INVALID_OPERATION));
525 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400526 }
527
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400528 bool sameBounds = srcX0 == dstX0 && srcY0 == dstY0 && srcX1 == dstX1 && srcY1 == dstY1;
529
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400530 if (mask & GL_COLOR_BUFFER_BIT)
531 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400532 gl::FramebufferAttachment *readColorBuffer = readFramebuffer->getReadColorbuffer();
533 gl::FramebufferAttachment *drawColorBuffer = drawFramebuffer->getFirstColorbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400534
535 if (readColorBuffer && drawColorBuffer)
536 {
Geoff Lang005df412013-10-16 14:12:50 -0400537 GLenum readInternalFormat = readColorBuffer->getActualFormat();
Geoff Lang5d601382014-07-22 15:14:06 -0400538 const InternalFormat &readFormatInfo = GetInternalFormatInfo(readInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400539
540 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; i++)
541 {
542 if (drawFramebuffer->isEnabledColorAttachment(i))
543 {
Geoff Lang005df412013-10-16 14:12:50 -0400544 GLenum drawInternalFormat = drawFramebuffer->getColorbuffer(i)->getActualFormat();
Geoff Lang5d601382014-07-22 15:14:06 -0400545 const InternalFormat &drawFormatInfo = GetInternalFormatInfo(drawInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400546
Geoff Langb2f3d052013-08-13 12:49:27 -0400547 // The GL ES 3.0.2 spec (pg 193) states that:
548 // 1) If the read buffer is fixed point format, the draw buffer must be as well
549 // 2) If the read buffer is an unsigned integer format, the draw buffer must be as well
550 // 3) If the read buffer is a signed integer format, the draw buffer must be as well
Geoff Lang5d601382014-07-22 15:14:06 -0400551 if ( (readFormatInfo.componentType == GL_UNSIGNED_NORMALIZED || readFormatInfo.componentType == GL_SIGNED_NORMALIZED) &&
552 !(drawFormatInfo.componentType == GL_UNSIGNED_NORMALIZED || drawFormatInfo.componentType == GL_SIGNED_NORMALIZED))
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 Lang5d601382014-07-22 15:14:06 -0400558 if (readFormatInfo.componentType == GL_UNSIGNED_INT && drawFormatInfo.componentType != GL_UNSIGNED_INT)
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
Geoff Lang5d601382014-07-22 15:14:06 -0400564 if (readFormatInfo.componentType == GL_INT && drawFormatInfo.componentType != GL_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400565 {
Geoff Langb1196682014-07-23 13:47:29 -0400566 context->recordError(Error(GL_INVALID_OPERATION));
567 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400568 }
569
Geoff Langb2f3d052013-08-13 12:49:27 -0400570 if (readColorBuffer->getSamples() > 0 && (readInternalFormat != drawInternalFormat || !sameBounds))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400571 {
Geoff Langb1196682014-07-23 13:47:29 -0400572 context->recordError(Error(GL_INVALID_OPERATION));
573 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400574 }
575 }
576 }
577
Geoff Lang5d601382014-07-22 15:14:06 -0400578 if ((readFormatInfo.componentType == GL_INT || readFormatInfo.componentType == GL_UNSIGNED_INT) && filter == GL_LINEAR)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400579 {
Geoff Langb1196682014-07-23 13:47:29 -0400580 context->recordError(Error(GL_INVALID_OPERATION));
581 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400582 }
583
584 if (fromAngleExtension)
585 {
586 const GLenum readColorbufferType = readFramebuffer->getReadColorbufferType();
587 if (readColorbufferType != GL_TEXTURE_2D && readColorbufferType != GL_RENDERBUFFER)
588 {
Geoff Langb1196682014-07-23 13:47:29 -0400589 context->recordError(Error(GL_INVALID_OPERATION));
590 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400591 }
592
593 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
594 {
595 if (drawFramebuffer->isEnabledColorAttachment(colorAttachment))
596 {
Jamie Madille92a3542014-07-03 10:38:58 -0400597 FramebufferAttachment *attachment = drawFramebuffer->getColorbuffer(colorAttachment);
598 ASSERT(attachment);
599
600 if (attachment->type() != GL_TEXTURE_2D && attachment->type() != GL_RENDERBUFFER)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400601 {
Geoff Langb1196682014-07-23 13:47:29 -0400602 context->recordError(Error(GL_INVALID_OPERATION));
603 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400604 }
605
Jamie Madillf8f18f02014-10-02 10:44:17 -0400606 // Return an error if the destination formats do not match
607 if (attachment->getInternalFormat() != readColorBuffer->getInternalFormat())
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400608 {
Geoff Langb1196682014-07-23 13:47:29 -0400609 context->recordError(Error(GL_INVALID_OPERATION));
610 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400611 }
612 }
613 }
Jamie Madill48faf802014-11-06 15:27:22 -0500614
615 int readSamples = readFramebuffer->getSamples(context->getData());
616
617 if (readSamples != 0 && IsPartialBlit(context, readColorBuffer, drawColorBuffer,
618 srcX0, srcY0, srcX1, srcY1,
619 dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400620 {
Geoff Langb1196682014-07-23 13:47:29 -0400621 context->recordError(Error(GL_INVALID_OPERATION));
622 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400623 }
624 }
625 }
626 }
627
628 if (mask & GL_DEPTH_BUFFER_BIT)
629 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400630 gl::FramebufferAttachment *readDepthBuffer = readFramebuffer->getDepthbuffer();
631 gl::FramebufferAttachment *drawDepthBuffer = drawFramebuffer->getDepthbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400632
633 if (readDepthBuffer && drawDepthBuffer)
634 {
635 if (readDepthBuffer->getActualFormat() != drawDepthBuffer->getActualFormat())
636 {
Geoff Langb1196682014-07-23 13:47:29 -0400637 context->recordError(Error(GL_INVALID_OPERATION));
638 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400639 }
640
641 if (readDepthBuffer->getSamples() > 0 && !sameBounds)
642 {
Geoff Langb1196682014-07-23 13:47:29 -0400643 context->recordError(Error(GL_INVALID_OPERATION));
644 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400645 }
646
647 if (fromAngleExtension)
648 {
Geoff Lang125deab2013-08-09 13:34:16 -0400649 if (IsPartialBlit(context, readDepthBuffer, drawDepthBuffer,
650 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400651 {
652 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
Geoff Langb1196682014-07-23 13:47:29 -0400653 context->recordError(Error(GL_INVALID_OPERATION)); // only whole-buffer copies are permitted
654 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400655 }
656
657 if (readDepthBuffer->getSamples() != 0 || drawDepthBuffer->getSamples() != 0)
658 {
Geoff Langb1196682014-07-23 13:47:29 -0400659 context->recordError(Error(GL_INVALID_OPERATION));
660 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400661 }
662 }
663 }
664 }
665
666 if (mask & GL_STENCIL_BUFFER_BIT)
667 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400668 gl::FramebufferAttachment *readStencilBuffer = readFramebuffer->getStencilbuffer();
669 gl::FramebufferAttachment *drawStencilBuffer = drawFramebuffer->getStencilbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400670
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400671 if (readStencilBuffer && drawStencilBuffer)
672 {
673 if (readStencilBuffer->getActualFormat() != drawStencilBuffer->getActualFormat())
674 {
Geoff Langb1196682014-07-23 13:47:29 -0400675 context->recordError(Error(GL_INVALID_OPERATION));
676 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400677 }
678
679 if (readStencilBuffer->getSamples() > 0 && !sameBounds)
680 {
Geoff Langb1196682014-07-23 13:47:29 -0400681 context->recordError(Error(GL_INVALID_OPERATION));
682 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400683 }
684
685 if (fromAngleExtension)
686 {
Geoff Lang125deab2013-08-09 13:34:16 -0400687 if (IsPartialBlit(context, readStencilBuffer, drawStencilBuffer,
688 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400689 {
690 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
Geoff Langb1196682014-07-23 13:47:29 -0400691 context->recordError(Error(GL_INVALID_OPERATION)); // only whole-buffer copies are permitted
692 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400693 }
694
695 if (readStencilBuffer->getSamples() != 0 || drawStencilBuffer->getSamples() != 0)
696 {
Geoff Langb1196682014-07-23 13:47:29 -0400697 context->recordError(Error(GL_INVALID_OPERATION));
698 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400699 }
700 }
701 }
702 }
703
704 return true;
705}
706
Geoff Langb1196682014-07-23 13:47:29 -0400707bool ValidateGetVertexAttribParameters(Context *context, GLenum pname)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400708{
709 switch (pname)
710 {
711 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
712 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
713 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
714 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
715 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
716 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
717 case GL_CURRENT_VERTEX_ATTRIB:
718 return true;
719
720 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR:
721 // Don't verify ES3 context because GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE uses
722 // the same constant.
723 META_ASSERT(GL_VERTEX_ATTRIB_ARRAY_DIVISOR == GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE);
724 return true;
725
726 case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
Geoff Langb1196682014-07-23 13:47:29 -0400727 if (context->getClientVersion() < 3)
728 {
729 context->recordError(Error(GL_INVALID_ENUM));
730 return false;
731 }
732 return true;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400733
734 default:
Geoff Langb1196682014-07-23 13:47:29 -0400735 context->recordError(Error(GL_INVALID_ENUM));
736 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400737 }
738}
739
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400740bool ValidateTexParamParameters(gl::Context *context, GLenum pname, GLint param)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400741{
742 switch (pname)
743 {
744 case GL_TEXTURE_WRAP_R:
745 case GL_TEXTURE_SWIZZLE_R:
746 case GL_TEXTURE_SWIZZLE_G:
747 case GL_TEXTURE_SWIZZLE_B:
748 case GL_TEXTURE_SWIZZLE_A:
749 case GL_TEXTURE_BASE_LEVEL:
750 case GL_TEXTURE_MAX_LEVEL:
751 case GL_TEXTURE_COMPARE_MODE:
752 case GL_TEXTURE_COMPARE_FUNC:
753 case GL_TEXTURE_MIN_LOD:
754 case GL_TEXTURE_MAX_LOD:
755 if (context->getClientVersion() < 3)
756 {
Geoff Langb1196682014-07-23 13:47:29 -0400757 context->recordError(Error(GL_INVALID_ENUM));
758 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400759 }
760 break;
761
762 default: break;
763 }
764
765 switch (pname)
766 {
767 case GL_TEXTURE_WRAP_S:
768 case GL_TEXTURE_WRAP_T:
769 case GL_TEXTURE_WRAP_R:
770 switch (param)
771 {
772 case GL_REPEAT:
773 case GL_CLAMP_TO_EDGE:
774 case GL_MIRRORED_REPEAT:
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
781 case GL_TEXTURE_MIN_FILTER:
782 switch (param)
783 {
784 case GL_NEAREST:
785 case GL_LINEAR:
786 case GL_NEAREST_MIPMAP_NEAREST:
787 case GL_LINEAR_MIPMAP_NEAREST:
788 case GL_NEAREST_MIPMAP_LINEAR:
789 case GL_LINEAR_MIPMAP_LINEAR:
790 return true;
791 default:
Geoff Langb1196682014-07-23 13:47:29 -0400792 context->recordError(Error(GL_INVALID_ENUM));
793 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400794 }
795 break;
796
797 case GL_TEXTURE_MAG_FILTER:
798 switch (param)
799 {
800 case GL_NEAREST:
801 case GL_LINEAR:
802 return true;
803 default:
Geoff Langb1196682014-07-23 13:47:29 -0400804 context->recordError(Error(GL_INVALID_ENUM));
805 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400806 }
807 break;
808
809 case GL_TEXTURE_USAGE_ANGLE:
810 switch (param)
811 {
812 case GL_NONE:
813 case GL_FRAMEBUFFER_ATTACHMENT_ANGLE:
814 return true;
815 default:
Geoff Langb1196682014-07-23 13:47:29 -0400816 context->recordError(Error(GL_INVALID_ENUM));
817 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400818 }
819 break;
820
821 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
Geoff Langc0b9ef42014-07-02 10:02:37 -0400822 if (!context->getExtensions().textureFilterAnisotropic)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400823 {
Geoff Langb1196682014-07-23 13:47:29 -0400824 context->recordError(Error(GL_INVALID_ENUM));
825 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400826 }
827
828 // we assume the parameter passed to this validation method is truncated, not rounded
829 if (param < 1)
830 {
Geoff Langb1196682014-07-23 13:47:29 -0400831 context->recordError(Error(GL_INVALID_VALUE));
832 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400833 }
834 return true;
835
836 case GL_TEXTURE_MIN_LOD:
837 case GL_TEXTURE_MAX_LOD:
838 // any value is permissible
839 return true;
840
841 case GL_TEXTURE_COMPARE_MODE:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400842 // Acceptable mode parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400843 switch (param)
844 {
845 case GL_NONE:
846 case GL_COMPARE_REF_TO_TEXTURE:
847 return true;
848 default:
Geoff Langb1196682014-07-23 13:47:29 -0400849 context->recordError(Error(GL_INVALID_ENUM));
850 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400851 }
852 break;
853
854 case GL_TEXTURE_COMPARE_FUNC:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400855 // Acceptable function parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400856 switch (param)
857 {
858 case GL_LEQUAL:
859 case GL_GEQUAL:
860 case GL_LESS:
861 case GL_GREATER:
862 case GL_EQUAL:
863 case GL_NOTEQUAL:
864 case GL_ALWAYS:
865 case GL_NEVER:
866 return true;
867 default:
Geoff Langb1196682014-07-23 13:47:29 -0400868 context->recordError(Error(GL_INVALID_ENUM));
869 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400870 }
871 break;
872
873 case GL_TEXTURE_SWIZZLE_R:
874 case GL_TEXTURE_SWIZZLE_G:
875 case GL_TEXTURE_SWIZZLE_B:
876 case GL_TEXTURE_SWIZZLE_A:
Geoff Langbc90a482013-09-17 16:51:27 -0400877 switch (param)
878 {
879 case GL_RED:
880 case GL_GREEN:
881 case GL_BLUE:
882 case GL_ALPHA:
883 case GL_ZERO:
884 case GL_ONE:
885 return true;
886 default:
Geoff Langb1196682014-07-23 13:47:29 -0400887 context->recordError(Error(GL_INVALID_ENUM));
888 return false;
Geoff Langbc90a482013-09-17 16:51:27 -0400889 }
890 break;
891
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400892 case GL_TEXTURE_BASE_LEVEL:
893 case GL_TEXTURE_MAX_LEVEL:
Nicolas Capens8de68282014-04-04 11:10:27 -0400894 if (param < 0)
895 {
Geoff Langb1196682014-07-23 13:47:29 -0400896 context->recordError(Error(GL_INVALID_VALUE));
897 return false;
Nicolas Capens8de68282014-04-04 11:10:27 -0400898 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400899 return true;
900
901 default:
Geoff Langb1196682014-07-23 13:47:29 -0400902 context->recordError(Error(GL_INVALID_ENUM));
903 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400904 }
905}
906
Geoff Langb1196682014-07-23 13:47:29 -0400907bool ValidateSamplerObjectParameter(gl::Context *context, GLenum pname)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400908{
909 switch (pname)
910 {
911 case GL_TEXTURE_MIN_FILTER:
912 case GL_TEXTURE_MAG_FILTER:
913 case GL_TEXTURE_WRAP_S:
914 case GL_TEXTURE_WRAP_T:
915 case GL_TEXTURE_WRAP_R:
916 case GL_TEXTURE_MIN_LOD:
917 case GL_TEXTURE_MAX_LOD:
918 case GL_TEXTURE_COMPARE_MODE:
919 case GL_TEXTURE_COMPARE_FUNC:
920 return true;
921
922 default:
Geoff Langb1196682014-07-23 13:47:29 -0400923 context->recordError(Error(GL_INVALID_ENUM));
924 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400925 }
926}
927
Jamie Madill26e91952014-03-05 15:01:27 -0500928bool ValidateReadPixelsParameters(gl::Context *context, GLint x, GLint y, GLsizei width, GLsizei height,
929 GLenum format, GLenum type, GLsizei *bufSize, GLvoid *pixels)
930{
Shannon Woods53a94a82014-06-24 15:20:36 -0400931 gl::Framebuffer *framebuffer = context->getState().getReadFramebuffer();
Jamie Madill893ab082014-05-16 16:56:10 -0400932 ASSERT(framebuffer);
Jamie Madill26e91952014-03-05 15:01:27 -0500933
Jamie Madill48faf802014-11-06 15:27:22 -0500934 if (framebuffer->completeness(context->getData()) != GL_FRAMEBUFFER_COMPLETE)
Jamie Madill26e91952014-03-05 15:01:27 -0500935 {
Geoff Langb1196682014-07-23 13:47:29 -0400936 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
937 return false;
Jamie Madill26e91952014-03-05 15:01:27 -0500938 }
939
Jamie Madill48faf802014-11-06 15:27:22 -0500940 if (context->getState().getReadFramebuffer()->id() != 0 &&
941 framebuffer->getSamples(context->getData()) != 0)
Jamie Madill26e91952014-03-05 15:01:27 -0500942 {
Geoff Langb1196682014-07-23 13:47:29 -0400943 context->recordError(Error(GL_INVALID_OPERATION));
944 return false;
Jamie Madill26e91952014-03-05 15:01:27 -0500945 }
946
Jamie Madill893ab082014-05-16 16:56:10 -0400947 if (!framebuffer->getReadColorbuffer())
948 {
Geoff Langb1196682014-07-23 13:47:29 -0400949 context->recordError(Error(GL_INVALID_OPERATION));
950 return false;
Jamie Madill893ab082014-05-16 16:56:10 -0400951 }
952
Jamie Madill26e91952014-03-05 15:01:27 -0500953 GLenum currentInternalFormat, currentFormat, currentType;
Geoff Lange4a492b2014-06-19 14:14:41 -0400954 GLuint clientVersion = context->getClientVersion();
Jamie Madill26e91952014-03-05 15:01:27 -0500955
Jamie Madill893ab082014-05-16 16:56:10 -0400956 context->getCurrentReadFormatType(&currentInternalFormat, &currentFormat, &currentType);
Jamie Madill26e91952014-03-05 15:01:27 -0500957
Geoff Langbdc9b2f2014-04-16 14:41:54 -0400958 bool validReadFormat = (clientVersion < 3) ? ValidES2ReadFormatType(context, format, type) :
959 ValidES3ReadFormatType(context, currentInternalFormat, format, type);
Jamie Madill26e91952014-03-05 15:01:27 -0500960
961 if (!(currentFormat == format && currentType == type) && !validReadFormat)
962 {
Geoff Langb1196682014-07-23 13:47:29 -0400963 context->recordError(Error(GL_INVALID_OPERATION));
964 return false;
Jamie Madill26e91952014-03-05 15:01:27 -0500965 }
966
Geoff Lang5d601382014-07-22 15:14:06 -0400967 GLenum sizedInternalFormat = GetSizedInternalFormat(format, type);
968 const InternalFormat &sizedFormatInfo = GetInternalFormatInfo(sizedInternalFormat);
Jamie Madill26e91952014-03-05 15:01:27 -0500969
Geoff Lang5d601382014-07-22 15:14:06 -0400970 GLsizei outputPitch = sizedFormatInfo.computeRowPitch(type, width, context->getState().getPackAlignment());
Jamie Madill26e91952014-03-05 15:01:27 -0500971 // sized query sanity check
972 if (bufSize)
973 {
974 int requiredSize = outputPitch * height;
975 if (requiredSize > *bufSize)
976 {
Geoff Langb1196682014-07-23 13:47:29 -0400977 context->recordError(Error(GL_INVALID_OPERATION));
978 return false;
Jamie Madill26e91952014-03-05 15:01:27 -0500979 }
980 }
981
982 return true;
983}
984
Jamie Madilldb2f14c2014-05-13 13:56:30 -0400985bool ValidateBeginQuery(gl::Context *context, GLenum target, GLuint id)
986{
987 if (!ValidQueryType(context, target))
988 {
Geoff Langb1196682014-07-23 13:47:29 -0400989 context->recordError(Error(GL_INVALID_ENUM));
990 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -0400991 }
992
993 if (id == 0)
994 {
Geoff Langb1196682014-07-23 13:47:29 -0400995 context->recordError(Error(GL_INVALID_OPERATION));
996 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -0400997 }
998
999 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
1000 // of zero, if the active query object name for <target> is non-zero (for the
1001 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
1002 // the active query for either target is non-zero), if <id> is the name of an
1003 // existing query object whose type does not match <target>, or if <id> is the
1004 // active query object name for any query type, the error INVALID_OPERATION is
1005 // generated.
1006
1007 // Ensure no other queries are active
1008 // NOTE: If other queries than occlusion are supported, we will need to check
1009 // separately that:
1010 // a) The query ID passed is not the current active query for any target/type
1011 // b) There are no active queries for the requested target (and in the case
1012 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
1013 // no query may be active for either if glBeginQuery targets either.
Shannon Woods53a94a82014-06-24 15:20:36 -04001014 if (context->getState().isQueryActive())
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001015 {
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 Query *queryObject = context->getQuery(id, true, target);
1021
1022 // check that name was obtained with glGenQueries
1023 if (!queryObject)
1024 {
Geoff Langb1196682014-07-23 13:47:29 -04001025 context->recordError(Error(GL_INVALID_OPERATION));
1026 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001027 }
1028
1029 // check for type mismatch
1030 if (queryObject->getType() != target)
1031 {
Geoff Langb1196682014-07-23 13:47:29 -04001032 context->recordError(Error(GL_INVALID_OPERATION));
1033 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001034 }
1035
1036 return true;
1037}
1038
Jamie Madill45c785d2014-05-13 14:09:34 -04001039bool ValidateEndQuery(gl::Context *context, GLenum target)
1040{
1041 if (!ValidQueryType(context, target))
1042 {
Geoff Langb1196682014-07-23 13:47:29 -04001043 context->recordError(Error(GL_INVALID_ENUM));
1044 return false;
Jamie Madill45c785d2014-05-13 14:09:34 -04001045 }
1046
Shannon Woods53a94a82014-06-24 15:20:36 -04001047 const Query *queryObject = context->getState().getActiveQuery(target);
Jamie Madill45c785d2014-05-13 14:09:34 -04001048
1049 if (queryObject == NULL)
1050 {
Geoff Langb1196682014-07-23 13:47:29 -04001051 context->recordError(Error(GL_INVALID_OPERATION));
1052 return false;
Jamie Madill45c785d2014-05-13 14:09:34 -04001053 }
1054
Jamie Madill45c785d2014-05-13 14:09:34 -04001055 return true;
1056}
1057
Jamie Madill36398922014-05-20 14:51:53 -04001058static bool ValidateUniformCommonBase(gl::Context *context, GLenum targetUniformType,
1059 GLint location, GLsizei count, LinkedUniform **uniformOut)
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001060{
1061 if (count < 0)
1062 {
Geoff Langb1196682014-07-23 13:47:29 -04001063 context->recordError(Error(GL_INVALID_VALUE));
1064 return false;
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001065 }
1066
Shannon Woods53a94a82014-06-24 15:20:36 -04001067 gl::ProgramBinary *programBinary = context->getState().getCurrentProgramBinary();
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001068 if (!programBinary)
1069 {
Geoff Langb1196682014-07-23 13:47:29 -04001070 context->recordError(Error(GL_INVALID_OPERATION));
1071 return false;
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001072 }
1073
1074 if (location == -1)
1075 {
1076 // Silently ignore the uniform command
1077 return false;
1078 }
1079
Jamie Madill36398922014-05-20 14:51:53 -04001080 if (!programBinary->isValidUniformLocation(location))
1081 {
Geoff Langb1196682014-07-23 13:47:29 -04001082 context->recordError(Error(GL_INVALID_OPERATION));
1083 return false;
Jamie Madill36398922014-05-20 14:51:53 -04001084 }
1085
1086 LinkedUniform *uniform = programBinary->getUniformByLocation(location);
1087
1088 // attempting to write an array to a non-array uniform is an INVALID_OPERATION
1089 if (uniform->elementCount() == 1 && count > 1)
1090 {
Geoff Langb1196682014-07-23 13:47:29 -04001091 context->recordError(Error(GL_INVALID_OPERATION));
1092 return false;
Jamie Madill36398922014-05-20 14:51:53 -04001093 }
1094
1095 *uniformOut = uniform;
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001096 return true;
1097}
1098
Jamie Madillaa981bd2014-05-20 10:55:55 -04001099bool ValidateUniform(gl::Context *context, GLenum uniformType, GLint location, GLsizei count)
1100{
1101 // Check for ES3 uniform entry points
Jamie Madillf2575982014-06-25 16:04:54 -04001102 if (VariableComponentType(uniformType) == GL_UNSIGNED_INT && context->getClientVersion() < 3)
Jamie Madillaa981bd2014-05-20 10:55:55 -04001103 {
Geoff Langb1196682014-07-23 13:47:29 -04001104 context->recordError(Error(GL_INVALID_OPERATION));
1105 return false;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001106 }
1107
Jamie Madill36398922014-05-20 14:51:53 -04001108 LinkedUniform *uniform = NULL;
1109 if (!ValidateUniformCommonBase(context, uniformType, location, count, &uniform))
1110 {
1111 return false;
1112 }
1113
Jamie Madillf2575982014-06-25 16:04:54 -04001114 GLenum targetBoolType = VariableBoolVectorType(uniformType);
Jamie Madill36398922014-05-20 14:51:53 -04001115 bool samplerUniformCheck = (IsSampler(uniform->type) && uniformType == GL_INT);
1116 if (!samplerUniformCheck && uniformType != uniform->type && targetBoolType != uniform->type)
1117 {
Geoff Langb1196682014-07-23 13:47:29 -04001118 context->recordError(Error(GL_INVALID_OPERATION));
1119 return false;
Jamie Madill36398922014-05-20 14:51:53 -04001120 }
1121
1122 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001123}
1124
1125bool ValidateUniformMatrix(gl::Context *context, GLenum matrixType, GLint location, GLsizei count,
1126 GLboolean transpose)
1127{
1128 // Check for ES3 uniform entry points
1129 int rows = VariableRowCount(matrixType);
1130 int cols = VariableColumnCount(matrixType);
1131 if (rows != cols && context->getClientVersion() < 3)
1132 {
Geoff Langb1196682014-07-23 13:47:29 -04001133 context->recordError(Error(GL_INVALID_OPERATION));
1134 return false;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001135 }
1136
1137 if (transpose != GL_FALSE && context->getClientVersion() < 3)
1138 {
Geoff Langb1196682014-07-23 13:47:29 -04001139 context->recordError(Error(GL_INVALID_VALUE));
1140 return false;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001141 }
1142
Jamie Madill36398922014-05-20 14:51:53 -04001143 LinkedUniform *uniform = NULL;
1144 if (!ValidateUniformCommonBase(context, matrixType, location, count, &uniform))
1145 {
1146 return false;
1147 }
1148
1149 if (uniform->type != matrixType)
1150 {
Geoff Langb1196682014-07-23 13:47:29 -04001151 context->recordError(Error(GL_INVALID_OPERATION));
1152 return false;
Jamie Madill36398922014-05-20 14:51:53 -04001153 }
1154
1155 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001156}
1157
Jamie Madill893ab082014-05-16 16:56:10 -04001158bool ValidateStateQuery(gl::Context *context, GLenum pname, GLenum *nativeType, unsigned int *numParams)
1159{
1160 if (!context->getQueryParameterInfo(pname, nativeType, numParams))
1161 {
Geoff Langb1196682014-07-23 13:47:29 -04001162 context->recordError(Error(GL_INVALID_ENUM));
1163 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001164 }
1165
1166 if (pname >= GL_DRAW_BUFFER0 && pname <= GL_DRAW_BUFFER15)
1167 {
1168 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0);
1169
Geoff Langaae65a42014-05-26 12:43:44 -04001170 if (colorAttachment >= context->getCaps().maxDrawBuffers)
Jamie Madill893ab082014-05-16 16:56:10 -04001171 {
Geoff Langb1196682014-07-23 13:47:29 -04001172 context->recordError(Error(GL_INVALID_OPERATION));
1173 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001174 }
1175 }
1176
1177 switch (pname)
1178 {
1179 case GL_TEXTURE_BINDING_2D:
1180 case GL_TEXTURE_BINDING_CUBE_MAP:
1181 case GL_TEXTURE_BINDING_3D:
1182 case GL_TEXTURE_BINDING_2D_ARRAY:
Geoff Lang3a61c322014-07-10 13:01:54 -04001183 if (context->getState().getActiveSampler() >= context->getCaps().maxCombinedTextureImageUnits)
Jamie Madill893ab082014-05-16 16:56:10 -04001184 {
Geoff Langb1196682014-07-23 13:47:29 -04001185 context->recordError(Error(GL_INVALID_OPERATION));
1186 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001187 }
1188 break;
1189
1190 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1191 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1192 {
Shannon Woods53a94a82014-06-24 15:20:36 -04001193 Framebuffer *framebuffer = context->getState().getReadFramebuffer();
Jamie Madill893ab082014-05-16 16:56:10 -04001194 ASSERT(framebuffer);
Jamie Madill48faf802014-11-06 15:27:22 -05001195 if (framebuffer->completeness(context->getData()) != GL_FRAMEBUFFER_COMPLETE)
Jamie Madill893ab082014-05-16 16:56:10 -04001196 {
Geoff Langb1196682014-07-23 13:47:29 -04001197 context->recordError(Error(GL_INVALID_OPERATION));
1198 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001199 }
1200
Jamie Madill3c7fa222014-06-05 13:08:51 -04001201 FramebufferAttachment *attachment = framebuffer->getReadColorbuffer();
1202 if (!attachment)
Jamie Madill893ab082014-05-16 16:56:10 -04001203 {
Geoff Langb1196682014-07-23 13:47:29 -04001204 context->recordError(Error(GL_INVALID_OPERATION));
1205 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001206 }
1207 }
1208 break;
1209
1210 default:
1211 break;
1212 }
1213
1214 // pname is valid, but there are no parameters to return
1215 if (numParams == 0)
1216 {
1217 return false;
1218 }
1219
1220 return true;
1221}
1222
Jamie Madill560a8d82014-05-21 13:06:20 -04001223bool ValidateCopyTexImageParametersBase(gl::Context* context, GLenum target, GLint level, GLenum internalformat, bool isSubImage,
1224 GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height,
1225 GLint border, GLenum *textureFormatOut)
1226{
1227
1228 if (!ValidTexture2DDestinationTarget(context, target))
1229 {
Geoff Langb1196682014-07-23 13:47:29 -04001230 context->recordError(Error(GL_INVALID_ENUM));
1231 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001232 }
1233
1234 if (level < 0 || xoffset < 0 || yoffset < 0 || zoffset < 0 || width < 0 || height < 0)
1235 {
Geoff Langb1196682014-07-23 13:47:29 -04001236 context->recordError(Error(GL_INVALID_VALUE));
1237 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001238 }
1239
1240 if (std::numeric_limits<GLsizei>::max() - xoffset < width || std::numeric_limits<GLsizei>::max() - yoffset < height)
1241 {
Geoff Langb1196682014-07-23 13:47:29 -04001242 context->recordError(Error(GL_INVALID_VALUE));
1243 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001244 }
1245
1246 if (border != 0)
1247 {
Geoff Langb1196682014-07-23 13:47:29 -04001248 context->recordError(Error(GL_INVALID_VALUE));
1249 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001250 }
1251
1252 if (!ValidMipLevel(context, target, level))
1253 {
Geoff Langb1196682014-07-23 13:47:29 -04001254 context->recordError(Error(GL_INVALID_VALUE));
1255 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001256 }
1257
Shannon Woods53a94a82014-06-24 15:20:36 -04001258 gl::Framebuffer *framebuffer = context->getState().getReadFramebuffer();
Jamie Madill48faf802014-11-06 15:27:22 -05001259 if (framebuffer->completeness(context->getData()) != GL_FRAMEBUFFER_COMPLETE)
Jamie Madill560a8d82014-05-21 13:06:20 -04001260 {
Geoff Langb1196682014-07-23 13:47:29 -04001261 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
1262 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001263 }
1264
Jamie Madill48faf802014-11-06 15:27:22 -05001265 if (context->getState().getReadFramebuffer()->id() != 0 && framebuffer->getSamples(context->getData()) != 0)
Jamie Madill560a8d82014-05-21 13:06:20 -04001266 {
Geoff Langb1196682014-07-23 13:47:29 -04001267 context->recordError(Error(GL_INVALID_OPERATION));
1268 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001269 }
1270
Geoff Langaae65a42014-05-26 12:43:44 -04001271 const gl::Caps &caps = context->getCaps();
1272
Jamie Madill560a8d82014-05-21 13:06:20 -04001273 gl::Texture *texture = NULL;
1274 GLenum textureInternalFormat = GL_NONE;
Jamie Madill560a8d82014-05-21 13:06:20 -04001275 GLint textureLevelWidth = 0;
1276 GLint textureLevelHeight = 0;
1277 GLint textureLevelDepth = 0;
Geoff Langaae65a42014-05-26 12:43:44 -04001278 GLuint maxDimension = 0;
Jamie Madill560a8d82014-05-21 13:06:20 -04001279
1280 switch (target)
1281 {
1282 case GL_TEXTURE_2D:
1283 {
1284 gl::Texture2D *texture2d = context->getTexture2D();
1285 if (texture2d)
1286 {
1287 textureInternalFormat = texture2d->getInternalFormat(level);
Jamie Madill560a8d82014-05-21 13:06:20 -04001288 textureLevelWidth = texture2d->getWidth(level);
1289 textureLevelHeight = texture2d->getHeight(level);
1290 textureLevelDepth = 1;
1291 texture = texture2d;
Geoff Langaae65a42014-05-26 12:43:44 -04001292 maxDimension = caps.max2DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001293 }
1294 }
1295 break;
1296
1297 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1298 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1299 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1300 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1301 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1302 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1303 {
1304 gl::TextureCubeMap *textureCube = context->getTextureCubeMap();
1305 if (textureCube)
1306 {
1307 textureInternalFormat = textureCube->getInternalFormat(target, level);
Jamie Madill560a8d82014-05-21 13:06:20 -04001308 textureLevelWidth = textureCube->getWidth(target, level);
1309 textureLevelHeight = textureCube->getHeight(target, level);
1310 textureLevelDepth = 1;
1311 texture = textureCube;
Geoff Langaae65a42014-05-26 12:43:44 -04001312 maxDimension = caps.maxCubeMapTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001313 }
1314 }
1315 break;
1316
1317 case GL_TEXTURE_2D_ARRAY:
1318 {
1319 gl::Texture2DArray *texture2dArray = context->getTexture2DArray();
1320 if (texture2dArray)
1321 {
1322 textureInternalFormat = texture2dArray->getInternalFormat(level);
Jamie Madill560a8d82014-05-21 13:06:20 -04001323 textureLevelWidth = texture2dArray->getWidth(level);
1324 textureLevelHeight = texture2dArray->getHeight(level);
1325 textureLevelDepth = texture2dArray->getLayers(level);
1326 texture = texture2dArray;
Geoff Langaae65a42014-05-26 12:43:44 -04001327 maxDimension = caps.max2DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001328 }
1329 }
1330 break;
1331
1332 case GL_TEXTURE_3D:
1333 {
1334 gl::Texture3D *texture3d = context->getTexture3D();
1335 if (texture3d)
1336 {
1337 textureInternalFormat = texture3d->getInternalFormat(level);
Jamie Madill560a8d82014-05-21 13:06:20 -04001338 textureLevelWidth = texture3d->getWidth(level);
1339 textureLevelHeight = texture3d->getHeight(level);
1340 textureLevelDepth = texture3d->getDepth(level);
1341 texture = texture3d;
Geoff Langaae65a42014-05-26 12:43:44 -04001342 maxDimension = caps.max3DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001343 }
1344 }
1345 break;
1346
1347 default:
Geoff Langb1196682014-07-23 13:47:29 -04001348 context->recordError(Error(GL_INVALID_ENUM));
1349 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001350 }
1351
1352 if (!texture)
1353 {
Geoff Langb1196682014-07-23 13:47:29 -04001354 context->recordError(Error(GL_INVALID_OPERATION));
1355 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001356 }
1357
1358 if (texture->isImmutable() && !isSubImage)
1359 {
Geoff Langb1196682014-07-23 13:47:29 -04001360 context->recordError(Error(GL_INVALID_OPERATION));
1361 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001362 }
1363
Geoff Lang5d601382014-07-22 15:14:06 -04001364 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalformat);
1365
1366 if (formatInfo.depthBits > 0)
Jamie Madill560a8d82014-05-21 13:06:20 -04001367 {
Geoff Langb1196682014-07-23 13:47:29 -04001368 context->recordError(Error(GL_INVALID_OPERATION));
1369 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001370 }
1371
Geoff Lang5d601382014-07-22 15:14:06 -04001372 if (formatInfo.compressed)
Jamie Madill560a8d82014-05-21 13:06:20 -04001373 {
Geoff Lang5d601382014-07-22 15:14:06 -04001374 if (((width % formatInfo.compressedBlockWidth) != 0 && width != textureLevelWidth) ||
1375 ((height % formatInfo.compressedBlockHeight) != 0 && height != textureLevelHeight))
Jamie Madill560a8d82014-05-21 13:06:20 -04001376 {
Geoff Langb1196682014-07-23 13:47:29 -04001377 context->recordError(Error(GL_INVALID_OPERATION));
1378 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001379 }
1380 }
1381
1382 if (isSubImage)
1383 {
1384 if (xoffset + width > textureLevelWidth ||
1385 yoffset + height > textureLevelHeight ||
1386 zoffset >= textureLevelDepth)
1387 {
Geoff Langb1196682014-07-23 13:47:29 -04001388 context->recordError(Error(GL_INVALID_VALUE));
1389 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001390 }
1391 }
Jamie Madill6f38f822014-06-06 17:12:20 -04001392 else
1393 {
1394 if (IsCubemapTextureTarget(target) && width != height)
1395 {
Geoff Langb1196682014-07-23 13:47:29 -04001396 context->recordError(Error(GL_INVALID_VALUE));
1397 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04001398 }
1399
Geoff Lang5d601382014-07-22 15:14:06 -04001400 if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions()))
Jamie Madill6f38f822014-06-06 17:12:20 -04001401 {
Geoff Langb1196682014-07-23 13:47:29 -04001402 context->recordError(Error(GL_INVALID_ENUM));
1403 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04001404 }
1405
1406 int maxLevelDimension = (maxDimension >> level);
1407 if (static_cast<int>(width) > maxLevelDimension || static_cast<int>(height) > maxLevelDimension)
1408 {
Geoff Langb1196682014-07-23 13:47:29 -04001409 context->recordError(Error(GL_INVALID_VALUE));
1410 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04001411 }
1412 }
Jamie Madill560a8d82014-05-21 13:06:20 -04001413
1414 *textureFormatOut = textureInternalFormat;
1415 return true;
1416}
1417
Geoff Langb1196682014-07-23 13:47:29 -04001418static bool ValidateDrawBase(Context *context, GLenum mode, GLsizei count, GLsizei maxVertex, GLsizei primcount)
Jamie Madill250d33f2014-06-06 17:09:03 -04001419{
Jamie Madill1aeb1312014-06-20 13:21:25 -04001420 switch (mode)
1421 {
1422 case GL_POINTS:
1423 case GL_LINES:
1424 case GL_LINE_LOOP:
1425 case GL_LINE_STRIP:
1426 case GL_TRIANGLES:
1427 case GL_TRIANGLE_STRIP:
1428 case GL_TRIANGLE_FAN:
1429 break;
1430 default:
Geoff Langb1196682014-07-23 13:47:29 -04001431 context->recordError(Error(GL_INVALID_ENUM));
1432 return false;
Jamie Madill1aeb1312014-06-20 13:21:25 -04001433 }
1434
Jamie Madill250d33f2014-06-06 17:09:03 -04001435 if (count < 0)
1436 {
Geoff Langb1196682014-07-23 13:47:29 -04001437 context->recordError(Error(GL_INVALID_VALUE));
1438 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001439 }
1440
Geoff Langb1196682014-07-23 13:47:29 -04001441 const State &state = context->getState();
1442
Jamie Madill250d33f2014-06-06 17:09:03 -04001443 // Check for mapped buffers
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001444 if (state.hasMappedBuffer(GL_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001445 {
Geoff Langb1196682014-07-23 13:47:29 -04001446 context->recordError(Error(GL_INVALID_OPERATION));
1447 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001448 }
1449
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001450 const gl::DepthStencilState &depthStencilState = state.getDepthStencilState();
Jamie Madillac528012014-06-20 13:21:23 -04001451 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001452 state.getStencilRef() != state.getStencilBackRef() ||
Jamie Madillac528012014-06-20 13:21:23 -04001453 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
1454 {
1455 // Note: these separate values are not supported in WebGL, due to D3D's limitations.
1456 // See Section 6.10 of the WebGL 1.0 spec
1457 ERR("This ANGLE implementation does not support separate front/back stencil "
1458 "writemasks, reference values, or stencil mask values.");
Geoff Langb1196682014-07-23 13:47:29 -04001459 context->recordError(Error(GL_INVALID_OPERATION));
1460 return false;
Jamie Madillac528012014-06-20 13:21:23 -04001461 }
1462
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001463 const gl::Framebuffer *fbo = state.getDrawFramebuffer();
Jamie Madill48faf802014-11-06 15:27:22 -05001464 if (!fbo || fbo->completeness(context->getData()) != GL_FRAMEBUFFER_COMPLETE)
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001465 {
Geoff Langb1196682014-07-23 13:47:29 -04001466 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
1467 return false;
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001468 }
1469
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001470 if (state.getCurrentProgramId() == 0)
Jamie Madilld4cfa572014-07-08 10:00:32 -04001471 {
Geoff Langb1196682014-07-23 13:47:29 -04001472 context->recordError(Error(GL_INVALID_OPERATION));
1473 return false;
Jamie Madilld4cfa572014-07-08 10:00:32 -04001474 }
1475
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001476 gl::ProgramBinary *programBinary = state.getCurrentProgramBinary();
Brandon Jones43a53e22014-08-28 16:23:22 -07001477 if (!programBinary->validateSamplers(NULL, context->getCaps()))
Jamie Madilld4cfa572014-07-08 10:00:32 -04001478 {
Geoff Langb1196682014-07-23 13:47:29 -04001479 context->recordError(Error(GL_INVALID_OPERATION));
1480 return false;
Jamie Madilld4cfa572014-07-08 10:00:32 -04001481 }
1482
Jamie Madill2b976812014-08-25 15:47:49 -04001483 // Buffer validations
1484 const VertexArray *vao = state.getVertexArray();
1485 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
1486 {
1487 const VertexAttribute &attrib = vao->getVertexAttribute(attributeIndex);
1488 bool attribActive = (programBinary->getSemanticIndex(attributeIndex) != -1);
1489 if (attribActive && attrib.enabled)
1490 {
1491 gl::Buffer *buffer = attrib.buffer.get();
1492
1493 if (buffer)
1494 {
1495 GLint64 attribStride = static_cast<GLint64>(ComputeVertexAttributeStride(attrib));
1496 GLint64 maxVertexElement = 0;
1497
1498 if (attrib.divisor > 0)
1499 {
1500 maxVertexElement = static_cast<GLint64>(primcount) / static_cast<GLint64>(attrib.divisor);
1501 }
1502 else
1503 {
1504 maxVertexElement = static_cast<GLint64>(maxVertex);
1505 }
1506
1507 GLint64 attribDataSize = maxVertexElement * attribStride;
1508
1509 // [OpenGL ES 3.0.2] section 2.9.4 page 40:
1510 // We can return INVALID_OPERATION if our vertex attribute does not have
1511 // enough backing data.
1512 if (attribDataSize > buffer->getSize())
1513 {
Geoff Langb1196682014-07-23 13:47:29 -04001514 context->recordError(Error(GL_INVALID_OPERATION));
1515 return false;
Jamie Madill2b976812014-08-25 15:47:49 -04001516 }
1517 }
1518 else if (attrib.pointer == NULL)
1519 {
1520 // This is an application error that would normally result in a crash,
1521 // but we catch it and return an error
Geoff Langb1196682014-07-23 13:47:29 -04001522 context->recordError(Error(GL_INVALID_OPERATION, "An enabled vertex array has no buffer and no pointer."));
1523 return false;
Jamie Madill2b976812014-08-25 15:47:49 -04001524 }
1525 }
1526 }
1527
Jamie Madill250d33f2014-06-06 17:09:03 -04001528 // No-op if zero count
1529 return (count > 0);
1530}
1531
Geoff Langb1196682014-07-23 13:47:29 -04001532bool ValidateDrawArrays(Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
Jamie Madill250d33f2014-06-06 17:09:03 -04001533{
Jamie Madillfd716582014-06-06 17:09:04 -04001534 if (first < 0)
Jamie Madill250d33f2014-06-06 17:09:03 -04001535 {
Geoff Langb1196682014-07-23 13:47:29 -04001536 context->recordError(Error(GL_INVALID_VALUE));
1537 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001538 }
1539
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001540 const State &state = context->getState();
1541 gl::TransformFeedback *curTransformFeedback = state.getCurrentTransformFeedback();
Jamie Madillfd716582014-06-06 17:09:04 -04001542 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused() &&
1543 curTransformFeedback->getDrawMode() != mode)
1544 {
1545 // It is an invalid operation to call DrawArrays or DrawArraysInstanced with a draw mode
1546 // that does not match the current transform feedback object's draw mode (if transform feedback
1547 // is active), (3.0.2, section 2.14, pg 86)
Geoff Langb1196682014-07-23 13:47:29 -04001548 context->recordError(Error(GL_INVALID_OPERATION));
1549 return false;
Jamie Madillfd716582014-06-06 17:09:04 -04001550 }
1551
Geoff Langb1196682014-07-23 13:47:29 -04001552 if (!ValidateDrawBase(context, mode, count, count, primcount))
Jamie Madillfd716582014-06-06 17:09:04 -04001553 {
1554 return false;
1555 }
1556
1557 return true;
1558}
1559
Geoff Langb1196682014-07-23 13:47:29 -04001560bool ValidateDrawArraysInstanced(Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
Jamie Madillfd716582014-06-06 17:09:04 -04001561{
1562 if (primcount < 0)
1563 {
Geoff Langb1196682014-07-23 13:47:29 -04001564 context->recordError(Error(GL_INVALID_VALUE));
1565 return false;
Jamie Madillfd716582014-06-06 17:09:04 -04001566 }
1567
Jamie Madill2b976812014-08-25 15:47:49 -04001568 if (!ValidateDrawArrays(context, mode, first, count, primcount))
Jamie Madillfd716582014-06-06 17:09:04 -04001569 {
1570 return false;
1571 }
1572
1573 // No-op if zero primitive count
1574 return (primcount > 0);
1575}
1576
Geoff Lang87a93302014-09-16 13:29:43 -04001577static bool ValidateDrawInstancedANGLE(Context *context)
1578{
1579 // Verify there is at least one active attribute with a divisor of zero
1580 const gl::State& state = context->getState();
1581
1582 gl::ProgramBinary *programBinary = state.getCurrentProgramBinary();
1583
1584 const VertexArray *vao = state.getVertexArray();
1585 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
1586 {
1587 const VertexAttribute &attrib = vao->getVertexAttribute(attributeIndex);
1588 bool active = (programBinary->getSemanticIndex(attributeIndex) != -1);
1589 if (active && attrib.divisor == 0)
1590 {
1591 return true;
1592 }
1593 }
1594
1595 context->recordError(Error(GL_INVALID_OPERATION, "ANGLE_instanced_arrays requires that at least one active attribute"
1596 "has a divisor of zero."));
1597 return false;
1598}
1599
1600bool ValidateDrawArraysInstancedANGLE(Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
1601{
1602 if (!ValidateDrawInstancedANGLE(context))
1603 {
1604 return false;
1605 }
1606
1607 return ValidateDrawArraysInstanced(context, mode, first, count, primcount);
1608}
1609
Geoff Langb1196682014-07-23 13:47:29 -04001610bool ValidateDrawElements(Context *context, GLenum mode, GLsizei count, GLenum type,
Jamie Madill2b976812014-08-25 15:47:49 -04001611 const GLvoid* indices, GLsizei primcount, rx::RangeUI *indexRangeOut)
Jamie Madillfd716582014-06-06 17:09:04 -04001612{
Jamie Madill250d33f2014-06-06 17:09:03 -04001613 switch (type)
1614 {
1615 case GL_UNSIGNED_BYTE:
1616 case GL_UNSIGNED_SHORT:
1617 break;
1618 case GL_UNSIGNED_INT:
Geoff Langc0b9ef42014-07-02 10:02:37 -04001619 if (!context->getExtensions().elementIndexUint)
Jamie Madill250d33f2014-06-06 17:09:03 -04001620 {
Geoff Langb1196682014-07-23 13:47:29 -04001621 context->recordError(Error(GL_INVALID_ENUM));
1622 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001623 }
1624 break;
1625 default:
Geoff Langb1196682014-07-23 13:47:29 -04001626 context->recordError(Error(GL_INVALID_ENUM));
1627 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001628 }
1629
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001630 const State &state = context->getState();
1631
1632 gl::TransformFeedback *curTransformFeedback = state.getCurrentTransformFeedback();
Jamie Madill250d33f2014-06-06 17:09:03 -04001633 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused())
1634 {
1635 // It is an invalid operation to call DrawElements, DrawRangeElements or DrawElementsInstanced
1636 // while transform feedback is active, (3.0.2, section 2.14, pg 86)
Geoff Langb1196682014-07-23 13:47:29 -04001637 context->recordError(Error(GL_INVALID_OPERATION));
1638 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001639 }
1640
1641 // Check for mapped buffers
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001642 if (state.hasMappedBuffer(GL_ELEMENT_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001643 {
Geoff Langb1196682014-07-23 13:47:29 -04001644 context->recordError(Error(GL_INVALID_OPERATION));
1645 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001646 }
1647
Jamie Madill2b976812014-08-25 15:47:49 -04001648 const gl::VertexArray *vao = state.getVertexArray();
1649 const gl::Buffer *elementArrayBuffer = vao->getElementArrayBuffer();
1650 if (!indices && !elementArrayBuffer)
Jamie Madilld4cfa572014-07-08 10:00:32 -04001651 {
Geoff Langb1196682014-07-23 13:47:29 -04001652 context->recordError(Error(GL_INVALID_OPERATION));
1653 return false;
Jamie Madilld4cfa572014-07-08 10:00:32 -04001654 }
1655
Jamie Madillae3000b2014-08-25 15:47:51 -04001656 if (elementArrayBuffer)
1657 {
1658 const gl::Type &typeInfo = gl::GetTypeInfo(type);
1659
1660 GLint64 offset = reinterpret_cast<GLint64>(indices);
1661 GLint64 byteCount = static_cast<GLint64>(typeInfo.bytes) * static_cast<GLint64>(count)+offset;
1662
1663 // check for integer overflows
1664 if (static_cast<GLuint>(count) > (std::numeric_limits<GLuint>::max() / typeInfo.bytes) ||
1665 byteCount > static_cast<GLint64>(std::numeric_limits<GLuint>::max()))
1666 {
Geoff Langb1196682014-07-23 13:47:29 -04001667 context->recordError(Error(GL_OUT_OF_MEMORY));
1668 return false;
Jamie Madillae3000b2014-08-25 15:47:51 -04001669 }
1670
1671 // Check for reading past the end of the bound buffer object
1672 if (byteCount > elementArrayBuffer->getSize())
1673 {
Geoff Langb1196682014-07-23 13:47:29 -04001674 context->recordError(Error(GL_INVALID_OPERATION));
1675 return false;
Jamie Madillae3000b2014-08-25 15:47:51 -04001676 }
1677 }
1678 else if (!indices)
1679 {
1680 // Catch this programming error here
Geoff Langb1196682014-07-23 13:47:29 -04001681 context->recordError(Error(GL_INVALID_OPERATION));
1682 return false;
Jamie Madillae3000b2014-08-25 15:47:51 -04001683 }
1684
Jamie Madill2b976812014-08-25 15:47:49 -04001685 // Use max index to validate if our vertex buffers are large enough for the pull.
1686 // TODO: offer fast path, with disabled index validation.
1687 // TODO: also disable index checking on back-ends that are robust to out-of-range accesses.
1688 if (elementArrayBuffer)
1689 {
Jacek Cabana5521de2014-10-01 17:23:46 +02001690 uintptr_t offset = reinterpret_cast<uintptr_t>(indices);
Jamie Madill2b976812014-08-25 15:47:49 -04001691 if (!elementArrayBuffer->getIndexRangeCache()->findRange(type, offset, count, indexRangeOut, NULL))
1692 {
Jamie Madill9ae396b2014-10-21 17:46:30 -04001693 // FIXME(jmadill): Use buffer data caching instead of the D3D back-end
1694 rx::BufferD3D *bufferD3D = rx::BufferD3D::makeBufferD3D(elementArrayBuffer->getImplementation());
Geoff Langc8d297a2014-09-19 11:09:08 -04001695 const uint8_t *dataPointer = NULL;
Jamie Madill9ae396b2014-10-21 17:46:30 -04001696 Error error = bufferD3D->getData(&dataPointer);
Geoff Langc8d297a2014-09-19 11:09:08 -04001697 if (error.isError())
1698 {
1699 context->recordError(error);
1700 return false;
1701 }
1702
1703 const uint8_t *offsetPointer = dataPointer + offset;
Jamie Madill2b976812014-08-25 15:47:49 -04001704 *indexRangeOut = rx::IndexRangeCache::ComputeRange(type, offsetPointer, count);
1705 }
1706 }
1707 else
1708 {
1709 *indexRangeOut = rx::IndexRangeCache::ComputeRange(type, indices, count);
1710 }
1711
Geoff Langb1196682014-07-23 13:47:29 -04001712 if (!ValidateDrawBase(context, mode, count, static_cast<GLsizei>(indexRangeOut->end), primcount))
Jamie Madillfd716582014-06-06 17:09:04 -04001713 {
1714 return false;
1715 }
1716
1717 return true;
1718}
1719
Geoff Langb1196682014-07-23 13:47:29 -04001720bool ValidateDrawElementsInstanced(Context *context,
Jamie Madill2b976812014-08-25 15:47:49 -04001721 GLenum mode, GLsizei count, GLenum type,
1722 const GLvoid *indices, GLsizei primcount,
1723 rx::RangeUI *indexRangeOut)
Jamie Madillfd716582014-06-06 17:09:04 -04001724{
1725 if (primcount < 0)
1726 {
Geoff Langb1196682014-07-23 13:47:29 -04001727 context->recordError(Error(GL_INVALID_VALUE));
1728 return false;
Jamie Madillfd716582014-06-06 17:09:04 -04001729 }
1730
Jamie Madill2b976812014-08-25 15:47:49 -04001731 if (!ValidateDrawElements(context, mode, count, type, indices, primcount, indexRangeOut))
Jamie Madillfd716582014-06-06 17:09:04 -04001732 {
1733 return false;
1734 }
1735
1736 // No-op zero primitive count
1737 return (primcount > 0);
Jamie Madill250d33f2014-06-06 17:09:03 -04001738}
1739
Geoff Lang87a93302014-09-16 13:29:43 -04001740bool ValidateDrawElementsInstancedANGLE(Context *context, GLenum mode, GLsizei count, GLenum type,
1741 const GLvoid *indices, GLsizei primcount, rx::RangeUI *indexRangeOut)
1742{
1743 if (!ValidateDrawInstancedANGLE(context))
1744 {
1745 return false;
1746 }
1747
1748 return ValidateDrawElementsInstanced(context, mode, count, type, indices, primcount, indexRangeOut);
1749}
1750
Geoff Langb1196682014-07-23 13:47:29 -04001751bool ValidateFramebufferTextureBase(Context *context, GLenum target, GLenum attachment,
Jamie Madill55ec3b12014-07-03 10:38:57 -04001752 GLuint texture, GLint level)
Jamie Madill570f7c82014-07-03 10:38:54 -04001753{
Jamie Madill55ec3b12014-07-03 10:38:57 -04001754 if (!ValidFramebufferTarget(target))
1755 {
Geoff Langb1196682014-07-23 13:47:29 -04001756 context->recordError(Error(GL_INVALID_ENUM));
1757 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001758 }
1759
1760 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill570f7c82014-07-03 10:38:54 -04001761 {
1762 return false;
1763 }
1764
Jamie Madill55ec3b12014-07-03 10:38:57 -04001765 if (texture != 0)
1766 {
1767 gl::Texture *tex = context->getTexture(texture);
1768
1769 if (tex == NULL)
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 if (level < 0)
1776 {
Geoff Langb1196682014-07-23 13:47:29 -04001777 context->recordError(Error(GL_INVALID_VALUE));
1778 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001779 }
1780 }
1781
Shannon Woods53a94a82014-06-24 15:20:36 -04001782 const gl::Framebuffer *framebuffer = context->getState().getTargetFramebuffer(target);
1783 GLuint framebufferHandle = context->getState().getTargetFramebuffer(target)->id();
Jamie Madill55ec3b12014-07-03 10:38:57 -04001784
1785 if (framebufferHandle == 0 || !framebuffer)
1786 {
Geoff Langb1196682014-07-23 13:47:29 -04001787 context->recordError(Error(GL_INVALID_OPERATION));
1788 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001789 }
1790
1791 return true;
1792}
1793
Geoff Langb1196682014-07-23 13:47:29 -04001794bool ValidateFramebufferTexture2D(Context *context, GLenum target, GLenum attachment,
Jamie Madill55ec3b12014-07-03 10:38:57 -04001795 GLenum textarget, GLuint texture, GLint level)
1796{
1797 // Attachments are required to be bound to level 0 in ES2
1798 if (context->getClientVersion() < 3 && level != 0)
1799 {
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
1804 if (!ValidateFramebufferTextureBase(context, target, attachment, texture, level))
Jamie Madill570f7c82014-07-03 10:38:54 -04001805 {
1806 return false;
1807 }
1808
Jamie Madill55ec3b12014-07-03 10:38:57 -04001809 if (texture != 0)
1810 {
1811 gl::Texture *tex = context->getTexture(texture);
1812 ASSERT(tex);
1813
Jamie Madill2a6564e2014-07-11 09:53:19 -04001814 const gl::Caps &caps = context->getCaps();
1815
Jamie Madill55ec3b12014-07-03 10:38:57 -04001816 switch (textarget)
1817 {
1818 case GL_TEXTURE_2D:
1819 {
Jamie Madill2a6564e2014-07-11 09:53:19 -04001820 if (level > gl::log2(caps.max2DTextureSize))
Jamie Madill55ec3b12014-07-03 10:38:57 -04001821 {
Geoff Langb1196682014-07-23 13:47:29 -04001822 context->recordError(Error(GL_INVALID_VALUE));
1823 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001824 }
1825 if (tex->getTarget() != GL_TEXTURE_2D)
1826 {
Geoff Langb1196682014-07-23 13:47:29 -04001827 context->recordError(Error(GL_INVALID_OPERATION));
1828 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001829 }
1830 gl::Texture2D *tex2d = static_cast<gl::Texture2D *>(tex);
1831 if (tex2d->isCompressed(level))
1832 {
Geoff Langb1196682014-07-23 13:47:29 -04001833 context->recordError(Error(GL_INVALID_OPERATION));
1834 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001835 }
1836 }
1837 break;
1838
1839 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1840 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1841 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1842 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1843 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1844 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1845 {
Jamie Madill2a6564e2014-07-11 09:53:19 -04001846 if (level > gl::log2(caps.maxCubeMapTextureSize))
Jamie Madill55ec3b12014-07-03 10:38:57 -04001847 {
Geoff Langb1196682014-07-23 13:47:29 -04001848 context->recordError(Error(GL_INVALID_VALUE));
1849 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001850 }
1851 if (tex->getTarget() != GL_TEXTURE_CUBE_MAP)
1852 {
Geoff Langb1196682014-07-23 13:47:29 -04001853 context->recordError(Error(GL_INVALID_OPERATION));
1854 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001855 }
1856 gl::TextureCubeMap *texcube = static_cast<gl::TextureCubeMap *>(tex);
1857 if (texcube->isCompressed(textarget, level))
1858 {
Geoff Langb1196682014-07-23 13:47:29 -04001859 context->recordError(Error(GL_INVALID_OPERATION));
1860 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001861 }
1862 }
1863 break;
1864
1865 default:
Geoff Langb1196682014-07-23 13:47:29 -04001866 context->recordError(Error(GL_INVALID_ENUM));
1867 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001868 }
1869 }
1870
Jamie Madill570f7c82014-07-03 10:38:54 -04001871 return true;
1872}
1873
Geoff Langb1196682014-07-23 13:47:29 -04001874bool ValidateGetUniformBase(Context *context, GLuint program, GLint location)
Jamie Madill0063c512014-08-25 15:47:53 -04001875{
1876 if (program == 0)
1877 {
Geoff Langb1196682014-07-23 13:47:29 -04001878 context->recordError(Error(GL_INVALID_VALUE));
1879 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001880 }
1881
1882 gl::Program *programObject = context->getProgram(program);
1883
1884 if (!programObject || !programObject->isLinked())
1885 {
Geoff Langb1196682014-07-23 13:47:29 -04001886 context->recordError(Error(GL_INVALID_OPERATION));
1887 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001888 }
1889
1890 gl::ProgramBinary *programBinary = programObject->getProgramBinary();
1891 if (!programBinary)
1892 {
Geoff Langb1196682014-07-23 13:47:29 -04001893 context->recordError(Error(GL_INVALID_OPERATION));
1894 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001895 }
1896
Jamie Madill549c7fd2014-08-25 15:47:56 -04001897 if (!programBinary->isValidUniformLocation(location))
1898 {
Geoff Langb1196682014-07-23 13:47:29 -04001899 context->recordError(Error(GL_INVALID_OPERATION));
1900 return false;
Jamie Madill549c7fd2014-08-25 15:47:56 -04001901 }
1902
Jamie Madill0063c512014-08-25 15:47:53 -04001903 return true;
1904}
1905
Geoff Langb1196682014-07-23 13:47:29 -04001906bool ValidateGetUniformfv(Context *context, GLuint program, GLint location, GLfloat* params)
Jamie Madill78f41802014-08-25 15:47:55 -04001907{
1908 return ValidateGetUniformBase(context, program, location);
1909}
1910
Geoff Langb1196682014-07-23 13:47:29 -04001911bool ValidateGetUniformiv(Context *context, GLuint program, GLint location, GLint* params)
Jamie Madill0063c512014-08-25 15:47:53 -04001912{
Jamie Madill78f41802014-08-25 15:47:55 -04001913 return ValidateGetUniformBase(context, program, location);
1914}
1915
Geoff Langb1196682014-07-23 13:47:29 -04001916static bool ValidateSizedGetUniform(Context *context, GLuint program, GLint location, GLsizei bufSize)
Jamie Madill78f41802014-08-25 15:47:55 -04001917{
1918 if (!ValidateGetUniformBase(context, program, location))
Jamie Madill0063c512014-08-25 15:47:53 -04001919 {
Jamie Madill78f41802014-08-25 15:47:55 -04001920 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001921 }
1922
Jamie Madilla502c742014-08-28 17:19:13 -04001923 gl::Program *programObject = context->getProgram(program);
1924 ASSERT(programObject);
1925 gl::ProgramBinary *programBinary = programObject->getProgramBinary();
Jamie Madill0063c512014-08-25 15:47:53 -04001926
Jamie Madill78f41802014-08-25 15:47:55 -04001927 // sized queries -- ensure the provided buffer is large enough
1928 LinkedUniform *uniform = programBinary->getUniformByLocation(location);
1929 size_t requiredBytes = VariableExternalSize(uniform->type);
1930 if (static_cast<size_t>(bufSize) < requiredBytes)
Jamie Madill0063c512014-08-25 15:47:53 -04001931 {
Geoff Langb1196682014-07-23 13:47:29 -04001932 context->recordError(Error(GL_INVALID_OPERATION));
1933 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001934 }
1935
1936 return true;
1937}
1938
Geoff Langb1196682014-07-23 13:47:29 -04001939bool ValidateGetnUniformfvEXT(Context *context, GLuint program, GLint location, GLsizei bufSize, GLfloat* params)
Jamie Madill0063c512014-08-25 15:47:53 -04001940{
Jamie Madill78f41802014-08-25 15:47:55 -04001941 return ValidateSizedGetUniform(context, program, location, bufSize);
Jamie Madill0063c512014-08-25 15:47:53 -04001942}
1943
Geoff Langb1196682014-07-23 13:47:29 -04001944bool ValidateGetnUniformivEXT(Context *context, GLuint program, GLint location, GLsizei bufSize, GLint* params)
Jamie Madill0063c512014-08-25 15:47:53 -04001945{
Jamie Madill78f41802014-08-25 15:47:55 -04001946 return ValidateSizedGetUniform(context, program, location, bufSize);
Jamie Madill0063c512014-08-25 15:47:53 -04001947}
1948
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001949}