blob: 9a5fa311699725ca266ca5346fe19f6f986e9c38 [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
27namespace gl
28{
29
Geoff Lang0550d032014-01-30 11:29:07 -050030bool ValidCap(const Context *context, GLenum cap)
31{
32 switch (cap)
33 {
34 case GL_CULL_FACE:
35 case GL_POLYGON_OFFSET_FILL:
36 case GL_SAMPLE_ALPHA_TO_COVERAGE:
37 case GL_SAMPLE_COVERAGE:
38 case GL_SCISSOR_TEST:
39 case GL_STENCIL_TEST:
40 case GL_DEPTH_TEST:
41 case GL_BLEND:
42 case GL_DITHER:
43 return true;
44 case GL_PRIMITIVE_RESTART_FIXED_INDEX:
45 case GL_RASTERIZER_DISCARD:
46 return (context->getClientVersion() >= 3);
47 default:
48 return false;
49 }
50}
51
Jamie Madill1fc7e2c2014-01-21 16:47:10 -050052bool ValidTextureTarget(const Context *context, GLenum target)
Jamie Madill35d15012013-10-07 10:46:37 -040053{
Jamie Madilld7460c72014-01-21 16:38:14 -050054 switch (target)
Jamie Madill35d15012013-10-07 10:46:37 -040055 {
Jamie Madilld7460c72014-01-21 16:38:14 -050056 case GL_TEXTURE_2D:
57 case GL_TEXTURE_CUBE_MAP:
58 return true;
Jamie Madill35d15012013-10-07 10:46:37 -040059
Jamie Madilld7460c72014-01-21 16:38:14 -050060 case GL_TEXTURE_3D:
61 case GL_TEXTURE_2D_ARRAY:
62 return (context->getClientVersion() >= 3);
63
64 default:
65 return false;
66 }
Jamie Madill35d15012013-10-07 10:46:37 -040067}
68
Shannon Woods4dfed832014-03-17 20:03:39 -040069// This function differs from ValidTextureTarget in that the target must be
70// usable as the destination of a 2D operation-- so a cube face is valid, but
71// GL_TEXTURE_CUBE_MAP is not.
Jamie Madill560a8d82014-05-21 13:06:20 -040072// Note: duplicate of IsInternalTextureTarget
Shannon Woods4dfed832014-03-17 20:03:39 -040073bool ValidTexture2DDestinationTarget(const Context *context, GLenum target)
74{
75 switch (target)
76 {
77 case GL_TEXTURE_2D:
78 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
79 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
80 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
81 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
82 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
83 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
84 return true;
85 case GL_TEXTURE_2D_ARRAY:
86 case GL_TEXTURE_3D:
87 return (context->getClientVersion() >= 3);
88 default:
89 return false;
90 }
91}
92
Jamie Madill1fc7e2c2014-01-21 16:47:10 -050093bool ValidFramebufferTarget(GLenum target)
94{
95 META_ASSERT(GL_DRAW_FRAMEBUFFER_ANGLE == GL_DRAW_FRAMEBUFFER && GL_READ_FRAMEBUFFER_ANGLE == GL_READ_FRAMEBUFFER);
96
97 switch (target)
98 {
99 case GL_FRAMEBUFFER: return true;
100 case GL_READ_FRAMEBUFFER: return true;
101 case GL_DRAW_FRAMEBUFFER: return true;
102 default: return false;
103 }
104}
105
Jamie Madill8c96d582014-03-05 15:01:23 -0500106bool ValidBufferTarget(const Context *context, GLenum target)
107{
108 switch (target)
109 {
110 case GL_ARRAY_BUFFER:
111 case GL_ELEMENT_ARRAY_BUFFER:
112 return true;
113
Jamie Madill8c96d582014-03-05 15:01:23 -0500114 case GL_PIXEL_PACK_BUFFER:
115 case GL_PIXEL_UNPACK_BUFFER:
Geoff Langc0b9ef42014-07-02 10:02:37 -0400116 return context->getExtensions().pixelBufferObject;
Shannon Woods158c4382014-05-06 13:00:07 -0400117
Shannon Woodsb3801742014-03-27 14:59:19 -0400118 case GL_COPY_READ_BUFFER:
119 case GL_COPY_WRITE_BUFFER:
Jamie Madill8c96d582014-03-05 15:01:23 -0500120 case GL_TRANSFORM_FEEDBACK_BUFFER:
121 case GL_UNIFORM_BUFFER:
122 return (context->getClientVersion() >= 3);
123
124 default:
125 return false;
126 }
127}
128
Jamie Madill70656a62014-03-05 15:01:26 -0500129bool ValidBufferParameter(const Context *context, GLenum pname)
130{
131 switch (pname)
132 {
133 case GL_BUFFER_USAGE:
134 case GL_BUFFER_SIZE:
135 return true;
136
137 // GL_BUFFER_MAP_POINTER is a special case, and may only be
138 // queried with GetBufferPointerv
139 case GL_BUFFER_ACCESS_FLAGS:
140 case GL_BUFFER_MAPPED:
141 case GL_BUFFER_MAP_OFFSET:
142 case GL_BUFFER_MAP_LENGTH:
143 return (context->getClientVersion() >= 3);
144
145 default:
146 return false;
147 }
148}
149
Jamie Madill8c96d582014-03-05 15:01:23 -0500150bool ValidMipLevel(const Context *context, GLenum target, GLint level)
Geoff Langce635692013-09-24 13:56:32 -0400151{
Geoff Langaae65a42014-05-26 12:43:44 -0400152 size_t maxDimension = 0;
Geoff Langce635692013-09-24 13:56:32 -0400153 switch (target)
154 {
Geoff Langaae65a42014-05-26 12:43:44 -0400155 case GL_TEXTURE_2D: maxDimension = context->getCaps().max2DTextureSize; break;
Geoff Langce635692013-09-24 13:56:32 -0400156 case GL_TEXTURE_CUBE_MAP:
157 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
158 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
159 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
160 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
161 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
Geoff Langaae65a42014-05-26 12:43:44 -0400162 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: maxDimension = context->getCaps().maxCubeMapTextureSize; break;
163 case GL_TEXTURE_3D: maxDimension = context->getCaps().max3DTextureSize; break;
164 case GL_TEXTURE_2D_ARRAY: maxDimension = context->getCaps().max2DTextureSize; break;
Geoff Langce635692013-09-24 13:56:32 -0400165 default: UNREACHABLE();
166 }
167
Geoff Langaae65a42014-05-26 12:43:44 -0400168 return level <= gl::log2(maxDimension);
Geoff Langce635692013-09-24 13:56:32 -0400169}
170
Geoff Langb1196682014-07-23 13:47:29 -0400171bool ValidImageSize(const Context *context, GLenum target, GLint level,
Jamie Madill4fd75c12014-06-23 10:53:54 -0400172 GLsizei width, GLsizei height, GLsizei depth)
Geoff Langce635692013-09-24 13:56:32 -0400173{
174 if (level < 0 || width < 0 || height < 0 || depth < 0)
175 {
176 return false;
177 }
178
Geoff Langc0b9ef42014-07-02 10:02:37 -0400179 if (!context->getExtensions().textureNPOT &&
Jamie Madill4fd75c12014-06-23 10:53:54 -0400180 (level != 0 && (!gl::isPow2(width) || !gl::isPow2(height) || !gl::isPow2(depth))))
Geoff Langce635692013-09-24 13:56:32 -0400181 {
182 return false;
183 }
184
185 if (!ValidMipLevel(context, target, level))
186 {
187 return false;
188 }
189
190 return true;
191}
192
Geoff Langb1196682014-07-23 13:47:29 -0400193bool ValidCompressedImageSize(const Context *context, GLenum internalFormat, GLsizei width, GLsizei height)
Geoff Langd4f180b2013-09-24 13:57:44 -0400194{
Geoff Lang5d601382014-07-22 15:14:06 -0400195 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalFormat);
196 if (!formatInfo.compressed)
Geoff Langd4f180b2013-09-24 13:57:44 -0400197 {
198 return false;
199 }
200
Geoff Lang5d601382014-07-22 15:14:06 -0400201 if (width < 0 || (static_cast<GLuint>(width) > formatInfo.compressedBlockWidth && width % formatInfo.compressedBlockWidth != 0) ||
202 height < 0 || (static_cast<GLuint>(height) > formatInfo.compressedBlockHeight && height % formatInfo.compressedBlockHeight != 0))
Geoff Langd4f180b2013-09-24 13:57:44 -0400203 {
204 return false;
205 }
206
207 return true;
208}
209
Geoff Lang37dde692014-01-31 16:34:54 -0500210bool ValidQueryType(const Context *context, GLenum queryType)
211{
212 META_ASSERT(GL_ANY_SAMPLES_PASSED == GL_ANY_SAMPLES_PASSED_EXT);
213 META_ASSERT(GL_ANY_SAMPLES_PASSED_CONSERVATIVE == GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT);
214
215 switch (queryType)
216 {
217 case GL_ANY_SAMPLES_PASSED:
218 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
219 return true;
220 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
221 return (context->getClientVersion() >= 3);
222 default:
223 return false;
224 }
225}
226
Geoff Langb1196682014-07-23 13:47:29 -0400227bool ValidProgram(Context *context, GLuint id)
Geoff Lang48dcae72014-02-05 16:28:24 -0500228{
229 // ES3 spec (section 2.11.1) -- "Commands that accept shader or program object names will generate the
230 // error INVALID_VALUE if the provided name is not the name of either a shader or program object and
231 // INVALID_OPERATION if the provided name identifies an object that is not the expected type."
232
233 if (context->getProgram(id) != NULL)
234 {
235 return true;
236 }
237 else if (context->getShader(id) != NULL)
238 {
239 // ID is the wrong type
Geoff Langb1196682014-07-23 13:47:29 -0400240 context->recordError(Error(GL_INVALID_OPERATION));
241 return false;
Geoff Lang48dcae72014-02-05 16:28:24 -0500242 }
243 else
244 {
245 // No shader/program object has this ID
Geoff Langb1196682014-07-23 13:47:29 -0400246 context->recordError(Error(GL_INVALID_VALUE));
247 return false;
Geoff Lang48dcae72014-02-05 16:28:24 -0500248 }
249}
250
Geoff Langb1196682014-07-23 13:47:29 -0400251bool ValidateAttachmentTarget(gl::Context *context, GLenum attachment)
Jamie Madillb4472272014-07-03 10:38:55 -0400252{
253 if (attachment >= GL_COLOR_ATTACHMENT0_EXT && attachment <= GL_COLOR_ATTACHMENT15_EXT)
254 {
255 const unsigned int colorAttachment = (attachment - GL_COLOR_ATTACHMENT0_EXT);
256
Geoff Langaae65a42014-05-26 12:43:44 -0400257 if (colorAttachment >= context->getCaps().maxColorAttachments)
Jamie Madillb4472272014-07-03 10:38:55 -0400258 {
Geoff Langb1196682014-07-23 13:47:29 -0400259 context->recordError(Error(GL_INVALID_VALUE));
260 return false;
Jamie Madillb4472272014-07-03 10:38:55 -0400261 }
262 }
263 else
264 {
265 switch (attachment)
266 {
267 case GL_DEPTH_ATTACHMENT:
268 case GL_STENCIL_ATTACHMENT:
269 break;
270
271 case GL_DEPTH_STENCIL_ATTACHMENT:
272 if (context->getClientVersion() < 3)
273 {
Geoff Langb1196682014-07-23 13:47:29 -0400274 context->recordError(Error(GL_INVALID_ENUM));
275 return false;
Jamie Madillb4472272014-07-03 10:38:55 -0400276 }
277 break;
278
279 default:
Geoff Langb1196682014-07-23 13:47:29 -0400280 context->recordError(Error(GL_INVALID_ENUM));
281 return false;
Jamie Madillb4472272014-07-03 10:38:55 -0400282 }
283 }
284
285 return true;
286}
287
Geoff Langb1196682014-07-23 13:47:29 -0400288bool ValidateRenderbufferStorageParameters(gl::Context *context, GLenum target, GLsizei samples,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400289 GLenum internalformat, GLsizei width, GLsizei height,
290 bool angleExtension)
291{
292 switch (target)
293 {
294 case GL_RENDERBUFFER:
295 break;
296 default:
Geoff Langb1196682014-07-23 13:47:29 -0400297 context->recordError(Error(GL_INVALID_ENUM));
298 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400299 }
300
301 if (width < 0 || height < 0 || samples < 0)
302 {
Geoff Langb1196682014-07-23 13:47:29 -0400303 context->recordError(Error(GL_INVALID_VALUE));
304 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400305 }
306
Geoff Lang5d601382014-07-22 15:14:06 -0400307 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalformat);
308 if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions()))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400309 {
Geoff Langb1196682014-07-23 13:47:29 -0400310 context->recordError(Error(GL_INVALID_ENUM));
311 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400312 }
313
314 // ANGLE_framebuffer_multisample does not explicitly state that the internal format must be
315 // sized but it does state that the format must be in the ES2.0 spec table 4.5 which contains
316 // only sized internal formats. The ES3 spec (section 4.4.2) does, however, state that the
317 // internal format must be sized and not an integer format if samples is greater than zero.
Geoff Lang5d601382014-07-22 15:14:06 -0400318 if (formatInfo.pixelBytes == 0)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400319 {
Geoff Langb1196682014-07-23 13:47:29 -0400320 context->recordError(Error(GL_INVALID_ENUM));
321 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400322 }
323
Geoff Lang5d601382014-07-22 15:14:06 -0400324 if ((formatInfo.componentType == GL_UNSIGNED_INT || formatInfo.componentType == GL_INT) && samples > 0)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400325 {
Geoff Langb1196682014-07-23 13:47:29 -0400326 context->recordError(Error(GL_INVALID_OPERATION));
327 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400328 }
329
Geoff Langc0b9ef42014-07-02 10:02:37 -0400330 const TextureCaps &formatCaps = context->getTextureCaps().get(internalformat);
Geoff Lang6cf8e1b2014-07-03 13:03:57 -0400331 if (!formatCaps.renderable)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400332 {
Geoff Langb1196682014-07-23 13:47:29 -0400333 context->recordError(Error(GL_INVALID_ENUM));
334 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400335 }
336
Geoff Langaae65a42014-05-26 12:43:44 -0400337 if (static_cast<GLuint>(std::max(width, height)) > context->getCaps().maxRenderbufferSize)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400338 {
Geoff Langb1196682014-07-23 13:47:29 -0400339 context->recordError(Error(GL_INVALID_VALUE));
340 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400341 }
342
343 // ANGLE_framebuffer_multisample states that the value of samples must be less than or equal
344 // to MAX_SAMPLES_ANGLE (Context::getMaxSupportedSamples) while the ES3.0 spec (section 4.4.2)
345 // states that samples must be less than or equal to the maximum samples for the specified
346 // internal format.
347 if (angleExtension)
348 {
Geoff Lang5f4c4632014-07-03 13:46:52 -0400349 ASSERT(context->getExtensions().framebufferMultisample);
350 if (static_cast<GLuint>(samples) > context->getExtensions().maxSamples)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400351 {
Geoff Langb1196682014-07-23 13:47:29 -0400352 context->recordError(Error(GL_INVALID_VALUE));
353 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400354 }
Geoff Lang5f4c4632014-07-03 13:46:52 -0400355
356 // Check if this specific format supports enough samples
357 if (static_cast<GLuint>(samples) > formatCaps.getMaxSamples())
358 {
Geoff Langb1196682014-07-23 13:47:29 -0400359 context->recordError(Error(GL_OUT_OF_MEMORY));
360 return false;
Geoff Lang5f4c4632014-07-03 13:46:52 -0400361 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400362 }
363 else
364 {
Geoff Lang5f4c4632014-07-03 13:46:52 -0400365 if (static_cast<GLuint>(samples) > formatCaps.getMaxSamples())
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400366 {
Geoff Langb1196682014-07-23 13:47:29 -0400367 context->recordError(Error(GL_INVALID_VALUE));
368 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400369 }
370 }
371
Shannon Woods53a94a82014-06-24 15:20:36 -0400372 GLuint handle = context->getState().getRenderbufferId();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400373 if (handle == 0)
374 {
Geoff Langb1196682014-07-23 13:47:29 -0400375 context->recordError(Error(GL_INVALID_OPERATION));
376 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400377 }
378
379 return true;
380}
381
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500382bool ValidateFramebufferRenderbufferParameters(gl::Context *context, GLenum target, GLenum attachment,
383 GLenum renderbuffertarget, GLuint renderbuffer)
384{
Shannon Woods1da3cf62014-06-27 15:32:23 -0400385 if (!ValidFramebufferTarget(target))
386 {
Geoff Langb1196682014-07-23 13:47:29 -0400387 context->recordError(Error(GL_INVALID_ENUM));
388 return false;
Shannon Woods1da3cf62014-06-27 15:32:23 -0400389 }
390
Shannon Woods53a94a82014-06-24 15:20:36 -0400391 gl::Framebuffer *framebuffer = context->getState().getTargetFramebuffer(target);
392 GLuint framebufferHandle = context->getState().getTargetFramebuffer(target)->id();
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500393
394 if (!framebuffer || (framebufferHandle == 0 && renderbuffer != 0))
395 {
Geoff Langb1196682014-07-23 13:47:29 -0400396 context->recordError(Error(GL_INVALID_OPERATION));
397 return false;
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500398 }
399
Jamie Madillb4472272014-07-03 10:38:55 -0400400 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500401 {
Jamie Madillb4472272014-07-03 10:38:55 -0400402 return false;
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500403 }
404
Jamie Madillab9d82c2014-01-21 16:38:14 -0500405 // [OpenGL ES 2.0.25] Section 4.4.3 page 112
406 // [OpenGL ES 3.0.2] Section 4.4.2 page 201
407 // 'renderbuffer' must be either zero or the name of an existing renderbuffer object of
408 // type 'renderbuffertarget', otherwise an INVALID_OPERATION error is generated.
409 if (renderbuffer != 0)
410 {
411 if (!context->getRenderbuffer(renderbuffer))
412 {
Geoff Langb1196682014-07-23 13:47:29 -0400413 context->recordError(Error(GL_INVALID_OPERATION));
414 return false;
Jamie Madillab9d82c2014-01-21 16:38:14 -0500415 }
416 }
417
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500418 return true;
419}
420
Jamie Madill3c7fa222014-06-05 13:08:51 -0400421static bool IsPartialBlit(gl::Context *context, gl::FramebufferAttachment *readBuffer, gl::FramebufferAttachment *writeBuffer,
Geoff Lang125deab2013-08-09 13:34:16 -0400422 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
423 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1)
424{
425 if (srcX0 != 0 || srcY0 != 0 || dstX0 != 0 || dstY0 != 0 ||
426 dstX1 != writeBuffer->getWidth() || dstY1 != writeBuffer->getHeight() ||
427 srcX1 != readBuffer->getWidth() || srcY1 != readBuffer->getHeight())
428 {
429 return true;
430 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400431 else if (context->getState().isScissorTestEnabled())
Geoff Lang125deab2013-08-09 13:34:16 -0400432 {
Shannon Woods53a94a82014-06-24 15:20:36 -0400433 const Rectangle &scissor = context->getState().getScissor();
Geoff Lang125deab2013-08-09 13:34:16 -0400434
Shannon Woods53a94a82014-06-24 15:20:36 -0400435 return scissor.x > 0 || scissor.y > 0 ||
436 scissor.width < writeBuffer->getWidth() ||
437 scissor.height < writeBuffer->getHeight();
Geoff Lang125deab2013-08-09 13:34:16 -0400438 }
439 else
440 {
441 return false;
442 }
443}
444
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400445bool ValidateBlitFramebufferParameters(gl::Context *context, GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400446 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask,
447 GLenum filter, bool fromAngleExtension)
448{
449 switch (filter)
450 {
451 case GL_NEAREST:
452 break;
453 case GL_LINEAR:
454 if (fromAngleExtension)
455 {
Geoff Langb1196682014-07-23 13:47:29 -0400456 context->recordError(Error(GL_INVALID_ENUM));
457 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400458 }
459 break;
460 default:
Geoff Langb1196682014-07-23 13:47:29 -0400461 context->recordError(Error(GL_INVALID_ENUM));
462 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400463 }
464
465 if ((mask & ~(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)) != 0)
466 {
Geoff Langb1196682014-07-23 13:47:29 -0400467 context->recordError(Error(GL_INVALID_VALUE));
468 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400469 }
470
471 if (mask == 0)
472 {
473 // ES3.0 spec, section 4.3.2 specifies that a mask of zero is valid and no
474 // buffers are copied.
475 return false;
476 }
477
478 if (fromAngleExtension && (srcX1 - srcX0 != dstX1 - dstX0 || srcY1 - srcY0 != dstY1 - dstY0))
479 {
480 ERR("Scaling and flipping in BlitFramebufferANGLE not supported by this implementation.");
Geoff Langb1196682014-07-23 13:47:29 -0400481 context->recordError(Error(GL_INVALID_OPERATION));
482 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400483 }
484
485 // ES3.0 spec, section 4.3.2 states that linear filtering is only available for the
486 // color buffer, leaving only nearest being unfiltered from above
487 if ((mask & ~GL_COLOR_BUFFER_BIT) != 0 && filter != GL_NEAREST)
488 {
Geoff Langb1196682014-07-23 13:47:29 -0400489 context->recordError(Error(GL_INVALID_OPERATION));
490 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400491 }
492
Shannon Woods53a94a82014-06-24 15:20:36 -0400493 if (context->getState().getReadFramebuffer()->id() == context->getState().getDrawFramebuffer()->id())
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400494 {
495 if (fromAngleExtension)
496 {
497 ERR("Blits with the same source and destination framebuffer are not supported by this "
498 "implementation.");
499 }
Geoff Langb1196682014-07-23 13:47:29 -0400500 context->recordError(Error(GL_INVALID_OPERATION));
501 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400502 }
503
Shannon Woods53a94a82014-06-24 15:20:36 -0400504 gl::Framebuffer *readFramebuffer = context->getState().getReadFramebuffer();
505 gl::Framebuffer *drawFramebuffer = context->getState().getDrawFramebuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400506 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
507 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
508 {
Geoff Langb1196682014-07-23 13:47:29 -0400509 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
510 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400511 }
512
513 if (drawFramebuffer->getSamples() != 0)
514 {
Geoff Langb1196682014-07-23 13:47:29 -0400515 context->recordError(Error(GL_INVALID_OPERATION));
516 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400517 }
518
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400519 bool sameBounds = srcX0 == dstX0 && srcY0 == dstY0 && srcX1 == dstX1 && srcY1 == dstY1;
520
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400521 if (mask & GL_COLOR_BUFFER_BIT)
522 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400523 gl::FramebufferAttachment *readColorBuffer = readFramebuffer->getReadColorbuffer();
524 gl::FramebufferAttachment *drawColorBuffer = drawFramebuffer->getFirstColorbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400525
526 if (readColorBuffer && drawColorBuffer)
527 {
Geoff Lang005df412013-10-16 14:12:50 -0400528 GLenum readInternalFormat = readColorBuffer->getActualFormat();
Geoff Lang5d601382014-07-22 15:14:06 -0400529 const InternalFormat &readFormatInfo = GetInternalFormatInfo(readInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400530
531 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; i++)
532 {
533 if (drawFramebuffer->isEnabledColorAttachment(i))
534 {
Geoff Lang005df412013-10-16 14:12:50 -0400535 GLenum drawInternalFormat = drawFramebuffer->getColorbuffer(i)->getActualFormat();
Geoff Lang5d601382014-07-22 15:14:06 -0400536 const InternalFormat &drawFormatInfo = GetInternalFormatInfo(drawInternalFormat);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400537
Geoff Langb2f3d052013-08-13 12:49:27 -0400538 // The GL ES 3.0.2 spec (pg 193) states that:
539 // 1) If the read buffer is fixed point format, the draw buffer must be as well
540 // 2) If the read buffer is an unsigned integer format, the draw buffer must be as well
541 // 3) If the read buffer is a signed integer format, the draw buffer must be as well
Geoff Lang5d601382014-07-22 15:14:06 -0400542 if ( (readFormatInfo.componentType == GL_UNSIGNED_NORMALIZED || readFormatInfo.componentType == GL_SIGNED_NORMALIZED) &&
543 !(drawFormatInfo.componentType == GL_UNSIGNED_NORMALIZED || drawFormatInfo.componentType == GL_SIGNED_NORMALIZED))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400544 {
Geoff Langb1196682014-07-23 13:47:29 -0400545 context->recordError(Error(GL_INVALID_OPERATION));
546 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400547 }
548
Geoff Lang5d601382014-07-22 15:14:06 -0400549 if (readFormatInfo.componentType == GL_UNSIGNED_INT && drawFormatInfo.componentType != GL_UNSIGNED_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400550 {
Geoff Langb1196682014-07-23 13:47:29 -0400551 context->recordError(Error(GL_INVALID_OPERATION));
552 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400553 }
554
Geoff Lang5d601382014-07-22 15:14:06 -0400555 if (readFormatInfo.componentType == GL_INT && drawFormatInfo.componentType != GL_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400556 {
Geoff Langb1196682014-07-23 13:47:29 -0400557 context->recordError(Error(GL_INVALID_OPERATION));
558 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400559 }
560
Geoff Langb2f3d052013-08-13 12:49:27 -0400561 if (readColorBuffer->getSamples() > 0 && (readInternalFormat != drawInternalFormat || !sameBounds))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400562 {
Geoff Langb1196682014-07-23 13:47:29 -0400563 context->recordError(Error(GL_INVALID_OPERATION));
564 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400565 }
566 }
567 }
568
Geoff Lang5d601382014-07-22 15:14:06 -0400569 if ((readFormatInfo.componentType == GL_INT || readFormatInfo.componentType == GL_UNSIGNED_INT) && filter == GL_LINEAR)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400570 {
Geoff Langb1196682014-07-23 13:47:29 -0400571 context->recordError(Error(GL_INVALID_OPERATION));
572 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400573 }
574
575 if (fromAngleExtension)
576 {
577 const GLenum readColorbufferType = readFramebuffer->getReadColorbufferType();
578 if (readColorbufferType != GL_TEXTURE_2D && readColorbufferType != GL_RENDERBUFFER)
579 {
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 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
585 {
586 if (drawFramebuffer->isEnabledColorAttachment(colorAttachment))
587 {
Jamie Madille92a3542014-07-03 10:38:58 -0400588 FramebufferAttachment *attachment = drawFramebuffer->getColorbuffer(colorAttachment);
589 ASSERT(attachment);
590
591 if (attachment->type() != GL_TEXTURE_2D && attachment->type() != GL_RENDERBUFFER)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400592 {
Geoff Langb1196682014-07-23 13:47:29 -0400593 context->recordError(Error(GL_INVALID_OPERATION));
594 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400595 }
596
Jamie Madille92a3542014-07-03 10:38:58 -0400597 if (attachment->getActualFormat() != readColorBuffer->getActualFormat())
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400598 {
Geoff Langb1196682014-07-23 13:47:29 -0400599 context->recordError(Error(GL_INVALID_OPERATION));
600 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400601 }
602 }
603 }
Geoff Lang125deab2013-08-09 13:34:16 -0400604 if (readFramebuffer->getSamples() != 0 && IsPartialBlit(context, readColorBuffer, drawColorBuffer,
605 srcX0, srcY0, srcX1, srcY1,
606 dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400607 {
Geoff Langb1196682014-07-23 13:47:29 -0400608 context->recordError(Error(GL_INVALID_OPERATION));
609 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400610 }
611 }
612 }
613 }
614
615 if (mask & GL_DEPTH_BUFFER_BIT)
616 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400617 gl::FramebufferAttachment *readDepthBuffer = readFramebuffer->getDepthbuffer();
618 gl::FramebufferAttachment *drawDepthBuffer = drawFramebuffer->getDepthbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400619
620 if (readDepthBuffer && drawDepthBuffer)
621 {
622 if (readDepthBuffer->getActualFormat() != drawDepthBuffer->getActualFormat())
623 {
Geoff Langb1196682014-07-23 13:47:29 -0400624 context->recordError(Error(GL_INVALID_OPERATION));
625 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400626 }
627
628 if (readDepthBuffer->getSamples() > 0 && !sameBounds)
629 {
Geoff Langb1196682014-07-23 13:47:29 -0400630 context->recordError(Error(GL_INVALID_OPERATION));
631 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400632 }
633
634 if (fromAngleExtension)
635 {
Geoff Lang125deab2013-08-09 13:34:16 -0400636 if (IsPartialBlit(context, readDepthBuffer, drawDepthBuffer,
637 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400638 {
639 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
Geoff Langb1196682014-07-23 13:47:29 -0400640 context->recordError(Error(GL_INVALID_OPERATION)); // only whole-buffer copies are permitted
641 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400642 }
643
644 if (readDepthBuffer->getSamples() != 0 || drawDepthBuffer->getSamples() != 0)
645 {
Geoff Langb1196682014-07-23 13:47:29 -0400646 context->recordError(Error(GL_INVALID_OPERATION));
647 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400648 }
649 }
650 }
651 }
652
653 if (mask & GL_STENCIL_BUFFER_BIT)
654 {
Jamie Madill3c7fa222014-06-05 13:08:51 -0400655 gl::FramebufferAttachment *readStencilBuffer = readFramebuffer->getStencilbuffer();
656 gl::FramebufferAttachment *drawStencilBuffer = drawFramebuffer->getStencilbuffer();
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400657
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400658 if (readStencilBuffer && drawStencilBuffer)
659 {
660 if (readStencilBuffer->getActualFormat() != drawStencilBuffer->getActualFormat())
661 {
Geoff Langb1196682014-07-23 13:47:29 -0400662 context->recordError(Error(GL_INVALID_OPERATION));
663 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400664 }
665
666 if (readStencilBuffer->getSamples() > 0 && !sameBounds)
667 {
Geoff Langb1196682014-07-23 13:47:29 -0400668 context->recordError(Error(GL_INVALID_OPERATION));
669 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400670 }
671
672 if (fromAngleExtension)
673 {
Geoff Lang125deab2013-08-09 13:34:16 -0400674 if (IsPartialBlit(context, readStencilBuffer, drawStencilBuffer,
675 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400676 {
677 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
Geoff Langb1196682014-07-23 13:47:29 -0400678 context->recordError(Error(GL_INVALID_OPERATION)); // only whole-buffer copies are permitted
679 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400680 }
681
682 if (readStencilBuffer->getSamples() != 0 || drawStencilBuffer->getSamples() != 0)
683 {
Geoff Langb1196682014-07-23 13:47:29 -0400684 context->recordError(Error(GL_INVALID_OPERATION));
685 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400686 }
687 }
688 }
689 }
690
691 return true;
692}
693
Geoff Langb1196682014-07-23 13:47:29 -0400694bool ValidateGetVertexAttribParameters(Context *context, GLenum pname)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400695{
696 switch (pname)
697 {
698 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
699 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
700 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
701 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
702 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
703 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
704 case GL_CURRENT_VERTEX_ATTRIB:
705 return true;
706
707 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR:
708 // Don't verify ES3 context because GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE uses
709 // the same constant.
710 META_ASSERT(GL_VERTEX_ATTRIB_ARRAY_DIVISOR == GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE);
711 return true;
712
713 case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
Geoff Langb1196682014-07-23 13:47:29 -0400714 if (context->getClientVersion() < 3)
715 {
716 context->recordError(Error(GL_INVALID_ENUM));
717 return false;
718 }
719 return true;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400720
721 default:
Geoff Langb1196682014-07-23 13:47:29 -0400722 context->recordError(Error(GL_INVALID_ENUM));
723 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400724 }
725}
726
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400727bool ValidateTexParamParameters(gl::Context *context, GLenum pname, GLint param)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400728{
729 switch (pname)
730 {
731 case GL_TEXTURE_WRAP_R:
732 case GL_TEXTURE_SWIZZLE_R:
733 case GL_TEXTURE_SWIZZLE_G:
734 case GL_TEXTURE_SWIZZLE_B:
735 case GL_TEXTURE_SWIZZLE_A:
736 case GL_TEXTURE_BASE_LEVEL:
737 case GL_TEXTURE_MAX_LEVEL:
738 case GL_TEXTURE_COMPARE_MODE:
739 case GL_TEXTURE_COMPARE_FUNC:
740 case GL_TEXTURE_MIN_LOD:
741 case GL_TEXTURE_MAX_LOD:
742 if (context->getClientVersion() < 3)
743 {
Geoff Langb1196682014-07-23 13:47:29 -0400744 context->recordError(Error(GL_INVALID_ENUM));
745 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400746 }
747 break;
748
749 default: break;
750 }
751
752 switch (pname)
753 {
754 case GL_TEXTURE_WRAP_S:
755 case GL_TEXTURE_WRAP_T:
756 case GL_TEXTURE_WRAP_R:
757 switch (param)
758 {
759 case GL_REPEAT:
760 case GL_CLAMP_TO_EDGE:
761 case GL_MIRRORED_REPEAT:
762 return true;
763 default:
Geoff Langb1196682014-07-23 13:47:29 -0400764 context->recordError(Error(GL_INVALID_ENUM));
765 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400766 }
767
768 case GL_TEXTURE_MIN_FILTER:
769 switch (param)
770 {
771 case GL_NEAREST:
772 case GL_LINEAR:
773 case GL_NEAREST_MIPMAP_NEAREST:
774 case GL_LINEAR_MIPMAP_NEAREST:
775 case GL_NEAREST_MIPMAP_LINEAR:
776 case GL_LINEAR_MIPMAP_LINEAR:
777 return true;
778 default:
Geoff Langb1196682014-07-23 13:47:29 -0400779 context->recordError(Error(GL_INVALID_ENUM));
780 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400781 }
782 break;
783
784 case GL_TEXTURE_MAG_FILTER:
785 switch (param)
786 {
787 case GL_NEAREST:
788 case GL_LINEAR:
789 return true;
790 default:
Geoff Langb1196682014-07-23 13:47:29 -0400791 context->recordError(Error(GL_INVALID_ENUM));
792 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400793 }
794 break;
795
796 case GL_TEXTURE_USAGE_ANGLE:
797 switch (param)
798 {
799 case GL_NONE:
800 case GL_FRAMEBUFFER_ATTACHMENT_ANGLE:
801 return true;
802 default:
Geoff Langb1196682014-07-23 13:47:29 -0400803 context->recordError(Error(GL_INVALID_ENUM));
804 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400805 }
806 break;
807
808 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
Geoff Langc0b9ef42014-07-02 10:02:37 -0400809 if (!context->getExtensions().textureFilterAnisotropic)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400810 {
Geoff Langb1196682014-07-23 13:47:29 -0400811 context->recordError(Error(GL_INVALID_ENUM));
812 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400813 }
814
815 // we assume the parameter passed to this validation method is truncated, not rounded
816 if (param < 1)
817 {
Geoff Langb1196682014-07-23 13:47:29 -0400818 context->recordError(Error(GL_INVALID_VALUE));
819 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400820 }
821 return true;
822
823 case GL_TEXTURE_MIN_LOD:
824 case GL_TEXTURE_MAX_LOD:
825 // any value is permissible
826 return true;
827
828 case GL_TEXTURE_COMPARE_MODE:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400829 // Acceptable mode parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400830 switch (param)
831 {
832 case GL_NONE:
833 case GL_COMPARE_REF_TO_TEXTURE:
834 return true;
835 default:
Geoff Langb1196682014-07-23 13:47:29 -0400836 context->recordError(Error(GL_INVALID_ENUM));
837 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400838 }
839 break;
840
841 case GL_TEXTURE_COMPARE_FUNC:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400842 // Acceptable function parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400843 switch (param)
844 {
845 case GL_LEQUAL:
846 case GL_GEQUAL:
847 case GL_LESS:
848 case GL_GREATER:
849 case GL_EQUAL:
850 case GL_NOTEQUAL:
851 case GL_ALWAYS:
852 case GL_NEVER:
853 return true;
854 default:
Geoff Langb1196682014-07-23 13:47:29 -0400855 context->recordError(Error(GL_INVALID_ENUM));
856 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400857 }
858 break;
859
860 case GL_TEXTURE_SWIZZLE_R:
861 case GL_TEXTURE_SWIZZLE_G:
862 case GL_TEXTURE_SWIZZLE_B:
863 case GL_TEXTURE_SWIZZLE_A:
Geoff Langbc90a482013-09-17 16:51:27 -0400864 switch (param)
865 {
866 case GL_RED:
867 case GL_GREEN:
868 case GL_BLUE:
869 case GL_ALPHA:
870 case GL_ZERO:
871 case GL_ONE:
872 return true;
873 default:
Geoff Langb1196682014-07-23 13:47:29 -0400874 context->recordError(Error(GL_INVALID_ENUM));
875 return false;
Geoff Langbc90a482013-09-17 16:51:27 -0400876 }
877 break;
878
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400879 case GL_TEXTURE_BASE_LEVEL:
880 case GL_TEXTURE_MAX_LEVEL:
Nicolas Capens8de68282014-04-04 11:10:27 -0400881 if (param < 0)
882 {
Geoff Langb1196682014-07-23 13:47:29 -0400883 context->recordError(Error(GL_INVALID_VALUE));
884 return false;
Nicolas Capens8de68282014-04-04 11:10:27 -0400885 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400886 return true;
887
888 default:
Geoff Langb1196682014-07-23 13:47:29 -0400889 context->recordError(Error(GL_INVALID_ENUM));
890 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400891 }
892}
893
Geoff Langb1196682014-07-23 13:47:29 -0400894bool ValidateSamplerObjectParameter(gl::Context *context, GLenum pname)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400895{
896 switch (pname)
897 {
898 case GL_TEXTURE_MIN_FILTER:
899 case GL_TEXTURE_MAG_FILTER:
900 case GL_TEXTURE_WRAP_S:
901 case GL_TEXTURE_WRAP_T:
902 case GL_TEXTURE_WRAP_R:
903 case GL_TEXTURE_MIN_LOD:
904 case GL_TEXTURE_MAX_LOD:
905 case GL_TEXTURE_COMPARE_MODE:
906 case GL_TEXTURE_COMPARE_FUNC:
907 return true;
908
909 default:
Geoff Langb1196682014-07-23 13:47:29 -0400910 context->recordError(Error(GL_INVALID_ENUM));
911 return false;
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400912 }
913}
914
Jamie Madill26e91952014-03-05 15:01:27 -0500915bool ValidateReadPixelsParameters(gl::Context *context, GLint x, GLint y, GLsizei width, GLsizei height,
916 GLenum format, GLenum type, GLsizei *bufSize, GLvoid *pixels)
917{
Shannon Woods53a94a82014-06-24 15:20:36 -0400918 gl::Framebuffer *framebuffer = context->getState().getReadFramebuffer();
Jamie Madill893ab082014-05-16 16:56:10 -0400919 ASSERT(framebuffer);
Jamie Madill26e91952014-03-05 15:01:27 -0500920
921 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
922 {
Geoff Langb1196682014-07-23 13:47:29 -0400923 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
924 return false;
Jamie Madill26e91952014-03-05 15:01:27 -0500925 }
926
Shannon Woods53a94a82014-06-24 15:20:36 -0400927 if (context->getState().getReadFramebuffer()->id() != 0 && framebuffer->getSamples() != 0)
Jamie Madill26e91952014-03-05 15:01:27 -0500928 {
Geoff Langb1196682014-07-23 13:47:29 -0400929 context->recordError(Error(GL_INVALID_OPERATION));
930 return false;
Jamie Madill26e91952014-03-05 15:01:27 -0500931 }
932
Jamie Madill893ab082014-05-16 16:56:10 -0400933 if (!framebuffer->getReadColorbuffer())
934 {
Geoff Langb1196682014-07-23 13:47:29 -0400935 context->recordError(Error(GL_INVALID_OPERATION));
936 return false;
Jamie Madill893ab082014-05-16 16:56:10 -0400937 }
938
Jamie Madill26e91952014-03-05 15:01:27 -0500939 GLenum currentInternalFormat, currentFormat, currentType;
Geoff Lange4a492b2014-06-19 14:14:41 -0400940 GLuint clientVersion = context->getClientVersion();
Jamie Madill26e91952014-03-05 15:01:27 -0500941
Jamie Madill893ab082014-05-16 16:56:10 -0400942 context->getCurrentReadFormatType(&currentInternalFormat, &currentFormat, &currentType);
Jamie Madill26e91952014-03-05 15:01:27 -0500943
Geoff Langbdc9b2f2014-04-16 14:41:54 -0400944 bool validReadFormat = (clientVersion < 3) ? ValidES2ReadFormatType(context, format, type) :
945 ValidES3ReadFormatType(context, currentInternalFormat, format, type);
Jamie Madill26e91952014-03-05 15:01:27 -0500946
947 if (!(currentFormat == format && currentType == type) && !validReadFormat)
948 {
Geoff Langb1196682014-07-23 13:47:29 -0400949 context->recordError(Error(GL_INVALID_OPERATION));
950 return false;
Jamie Madill26e91952014-03-05 15:01:27 -0500951 }
952
Geoff Lang5d601382014-07-22 15:14:06 -0400953 GLenum sizedInternalFormat = GetSizedInternalFormat(format, type);
954 const InternalFormat &sizedFormatInfo = GetInternalFormatInfo(sizedInternalFormat);
Jamie Madill26e91952014-03-05 15:01:27 -0500955
Geoff Lang5d601382014-07-22 15:14:06 -0400956 GLsizei outputPitch = sizedFormatInfo.computeRowPitch(type, width, context->getState().getPackAlignment());
Jamie Madill26e91952014-03-05 15:01:27 -0500957 // sized query sanity check
958 if (bufSize)
959 {
960 int requiredSize = outputPitch * height;
961 if (requiredSize > *bufSize)
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 }
967
968 return true;
969}
970
Jamie Madilldb2f14c2014-05-13 13:56:30 -0400971bool ValidateBeginQuery(gl::Context *context, GLenum target, GLuint id)
972{
973 if (!ValidQueryType(context, target))
974 {
Geoff Langb1196682014-07-23 13:47:29 -0400975 context->recordError(Error(GL_INVALID_ENUM));
976 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -0400977 }
978
979 if (id == 0)
980 {
Geoff Langb1196682014-07-23 13:47:29 -0400981 context->recordError(Error(GL_INVALID_OPERATION));
982 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -0400983 }
984
985 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
986 // of zero, if the active query object name for <target> is non-zero (for the
987 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
988 // the active query for either target is non-zero), if <id> is the name of an
989 // existing query object whose type does not match <target>, or if <id> is the
990 // active query object name for any query type, the error INVALID_OPERATION is
991 // generated.
992
993 // Ensure no other queries are active
994 // NOTE: If other queries than occlusion are supported, we will need to check
995 // separately that:
996 // a) The query ID passed is not the current active query for any target/type
997 // b) There are no active queries for the requested target (and in the case
998 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
999 // no query may be active for either if glBeginQuery targets either.
Shannon Woods53a94a82014-06-24 15:20:36 -04001000 if (context->getState().isQueryActive())
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001001 {
Geoff Langb1196682014-07-23 13:47:29 -04001002 context->recordError(Error(GL_INVALID_OPERATION));
1003 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001004 }
1005
1006 Query *queryObject = context->getQuery(id, true, target);
1007
1008 // check that name was obtained with glGenQueries
1009 if (!queryObject)
1010 {
Geoff Langb1196682014-07-23 13:47:29 -04001011 context->recordError(Error(GL_INVALID_OPERATION));
1012 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001013 }
1014
1015 // check for type mismatch
1016 if (queryObject->getType() != target)
1017 {
Geoff Langb1196682014-07-23 13:47:29 -04001018 context->recordError(Error(GL_INVALID_OPERATION));
1019 return false;
Jamie Madilldb2f14c2014-05-13 13:56:30 -04001020 }
1021
1022 return true;
1023}
1024
Jamie Madill45c785d2014-05-13 14:09:34 -04001025bool ValidateEndQuery(gl::Context *context, GLenum target)
1026{
1027 if (!ValidQueryType(context, target))
1028 {
Geoff Langb1196682014-07-23 13:47:29 -04001029 context->recordError(Error(GL_INVALID_ENUM));
1030 return false;
Jamie Madill45c785d2014-05-13 14:09:34 -04001031 }
1032
Shannon Woods53a94a82014-06-24 15:20:36 -04001033 const Query *queryObject = context->getState().getActiveQuery(target);
Jamie Madill45c785d2014-05-13 14:09:34 -04001034
1035 if (queryObject == NULL)
1036 {
Geoff Langb1196682014-07-23 13:47:29 -04001037 context->recordError(Error(GL_INVALID_OPERATION));
1038 return false;
Jamie Madill45c785d2014-05-13 14:09:34 -04001039 }
1040
Jamie Madill45c785d2014-05-13 14:09:34 -04001041 return true;
1042}
1043
Jamie Madill36398922014-05-20 14:51:53 -04001044static bool ValidateUniformCommonBase(gl::Context *context, GLenum targetUniformType,
1045 GLint location, GLsizei count, LinkedUniform **uniformOut)
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001046{
1047 if (count < 0)
1048 {
Geoff Langb1196682014-07-23 13:47:29 -04001049 context->recordError(Error(GL_INVALID_VALUE));
1050 return false;
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001051 }
1052
Shannon Woods53a94a82014-06-24 15:20:36 -04001053 gl::ProgramBinary *programBinary = context->getState().getCurrentProgramBinary();
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001054 if (!programBinary)
1055 {
Geoff Langb1196682014-07-23 13:47:29 -04001056 context->recordError(Error(GL_INVALID_OPERATION));
1057 return false;
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001058 }
1059
1060 if (location == -1)
1061 {
1062 // Silently ignore the uniform command
1063 return false;
1064 }
1065
Jamie Madill36398922014-05-20 14:51:53 -04001066 if (!programBinary->isValidUniformLocation(location))
1067 {
Geoff Langb1196682014-07-23 13:47:29 -04001068 context->recordError(Error(GL_INVALID_OPERATION));
1069 return false;
Jamie Madill36398922014-05-20 14:51:53 -04001070 }
1071
1072 LinkedUniform *uniform = programBinary->getUniformByLocation(location);
1073
1074 // attempting to write an array to a non-array uniform is an INVALID_OPERATION
1075 if (uniform->elementCount() == 1 && count > 1)
1076 {
Geoff Langb1196682014-07-23 13:47:29 -04001077 context->recordError(Error(GL_INVALID_OPERATION));
1078 return false;
Jamie Madill36398922014-05-20 14:51:53 -04001079 }
1080
1081 *uniformOut = uniform;
Jamie Madilld7c7bb22014-05-20 10:55:54 -04001082 return true;
1083}
1084
Jamie Madillaa981bd2014-05-20 10:55:55 -04001085bool ValidateUniform(gl::Context *context, GLenum uniformType, GLint location, GLsizei count)
1086{
1087 // Check for ES3 uniform entry points
Jamie Madillf2575982014-06-25 16:04:54 -04001088 if (VariableComponentType(uniformType) == GL_UNSIGNED_INT && context->getClientVersion() < 3)
Jamie Madillaa981bd2014-05-20 10:55:55 -04001089 {
Geoff Langb1196682014-07-23 13:47:29 -04001090 context->recordError(Error(GL_INVALID_OPERATION));
1091 return false;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001092 }
1093
Jamie Madill36398922014-05-20 14:51:53 -04001094 LinkedUniform *uniform = NULL;
1095 if (!ValidateUniformCommonBase(context, uniformType, location, count, &uniform))
1096 {
1097 return false;
1098 }
1099
Jamie Madillf2575982014-06-25 16:04:54 -04001100 GLenum targetBoolType = VariableBoolVectorType(uniformType);
Jamie Madill36398922014-05-20 14:51:53 -04001101 bool samplerUniformCheck = (IsSampler(uniform->type) && uniformType == GL_INT);
1102 if (!samplerUniformCheck && uniformType != uniform->type && targetBoolType != uniform->type)
1103 {
Geoff Langb1196682014-07-23 13:47:29 -04001104 context->recordError(Error(GL_INVALID_OPERATION));
1105 return false;
Jamie Madill36398922014-05-20 14:51:53 -04001106 }
1107
1108 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001109}
1110
1111bool ValidateUniformMatrix(gl::Context *context, GLenum matrixType, GLint location, GLsizei count,
1112 GLboolean transpose)
1113{
1114 // Check for ES3 uniform entry points
1115 int rows = VariableRowCount(matrixType);
1116 int cols = VariableColumnCount(matrixType);
1117 if (rows != cols && context->getClientVersion() < 3)
1118 {
Geoff Langb1196682014-07-23 13:47:29 -04001119 context->recordError(Error(GL_INVALID_OPERATION));
1120 return false;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001121 }
1122
1123 if (transpose != GL_FALSE && context->getClientVersion() < 3)
1124 {
Geoff Langb1196682014-07-23 13:47:29 -04001125 context->recordError(Error(GL_INVALID_VALUE));
1126 return false;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001127 }
1128
Jamie Madill36398922014-05-20 14:51:53 -04001129 LinkedUniform *uniform = NULL;
1130 if (!ValidateUniformCommonBase(context, matrixType, location, count, &uniform))
1131 {
1132 return false;
1133 }
1134
1135 if (uniform->type != matrixType)
1136 {
Geoff Langb1196682014-07-23 13:47:29 -04001137 context->recordError(Error(GL_INVALID_OPERATION));
1138 return false;
Jamie Madill36398922014-05-20 14:51:53 -04001139 }
1140
1141 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001142}
1143
Jamie Madill893ab082014-05-16 16:56:10 -04001144bool ValidateStateQuery(gl::Context *context, GLenum pname, GLenum *nativeType, unsigned int *numParams)
1145{
1146 if (!context->getQueryParameterInfo(pname, nativeType, numParams))
1147 {
Geoff Langb1196682014-07-23 13:47:29 -04001148 context->recordError(Error(GL_INVALID_ENUM));
1149 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001150 }
1151
1152 if (pname >= GL_DRAW_BUFFER0 && pname <= GL_DRAW_BUFFER15)
1153 {
1154 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0);
1155
Geoff Langaae65a42014-05-26 12:43:44 -04001156 if (colorAttachment >= context->getCaps().maxDrawBuffers)
Jamie Madill893ab082014-05-16 16:56:10 -04001157 {
Geoff Langb1196682014-07-23 13:47:29 -04001158 context->recordError(Error(GL_INVALID_OPERATION));
1159 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001160 }
1161 }
1162
1163 switch (pname)
1164 {
1165 case GL_TEXTURE_BINDING_2D:
1166 case GL_TEXTURE_BINDING_CUBE_MAP:
1167 case GL_TEXTURE_BINDING_3D:
1168 case GL_TEXTURE_BINDING_2D_ARRAY:
Geoff Lang3a61c322014-07-10 13:01:54 -04001169 if (context->getState().getActiveSampler() >= context->getCaps().maxCombinedTextureImageUnits)
Jamie Madill893ab082014-05-16 16:56:10 -04001170 {
Geoff Langb1196682014-07-23 13:47:29 -04001171 context->recordError(Error(GL_INVALID_OPERATION));
1172 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001173 }
1174 break;
1175
1176 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1177 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1178 {
Shannon Woods53a94a82014-06-24 15:20:36 -04001179 Framebuffer *framebuffer = context->getState().getReadFramebuffer();
Jamie Madill893ab082014-05-16 16:56:10 -04001180 ASSERT(framebuffer);
1181 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1182 {
Geoff Langb1196682014-07-23 13:47:29 -04001183 context->recordError(Error(GL_INVALID_OPERATION));
1184 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001185 }
1186
Jamie Madill3c7fa222014-06-05 13:08:51 -04001187 FramebufferAttachment *attachment = framebuffer->getReadColorbuffer();
1188 if (!attachment)
Jamie Madill893ab082014-05-16 16:56:10 -04001189 {
Geoff Langb1196682014-07-23 13:47:29 -04001190 context->recordError(Error(GL_INVALID_OPERATION));
1191 return false;
Jamie Madill893ab082014-05-16 16:56:10 -04001192 }
1193 }
1194 break;
1195
1196 default:
1197 break;
1198 }
1199
1200 // pname is valid, but there are no parameters to return
1201 if (numParams == 0)
1202 {
1203 return false;
1204 }
1205
1206 return true;
1207}
1208
Jamie Madill560a8d82014-05-21 13:06:20 -04001209bool ValidateCopyTexImageParametersBase(gl::Context* context, GLenum target, GLint level, GLenum internalformat, bool isSubImage,
1210 GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height,
1211 GLint border, GLenum *textureFormatOut)
1212{
1213
1214 if (!ValidTexture2DDestinationTarget(context, target))
1215 {
Geoff Langb1196682014-07-23 13:47:29 -04001216 context->recordError(Error(GL_INVALID_ENUM));
1217 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001218 }
1219
1220 if (level < 0 || xoffset < 0 || yoffset < 0 || zoffset < 0 || width < 0 || height < 0)
1221 {
Geoff Langb1196682014-07-23 13:47:29 -04001222 context->recordError(Error(GL_INVALID_VALUE));
1223 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001224 }
1225
1226 if (std::numeric_limits<GLsizei>::max() - xoffset < width || std::numeric_limits<GLsizei>::max() - yoffset < height)
1227 {
Geoff Langb1196682014-07-23 13:47:29 -04001228 context->recordError(Error(GL_INVALID_VALUE));
1229 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001230 }
1231
1232 if (border != 0)
1233 {
Geoff Langb1196682014-07-23 13:47:29 -04001234 context->recordError(Error(GL_INVALID_VALUE));
1235 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001236 }
1237
1238 if (!ValidMipLevel(context, target, level))
1239 {
Geoff Langb1196682014-07-23 13:47:29 -04001240 context->recordError(Error(GL_INVALID_VALUE));
1241 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001242 }
1243
Shannon Woods53a94a82014-06-24 15:20:36 -04001244 gl::Framebuffer *framebuffer = context->getState().getReadFramebuffer();
Jamie Madill560a8d82014-05-21 13:06:20 -04001245 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1246 {
Geoff Langb1196682014-07-23 13:47:29 -04001247 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
1248 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001249 }
1250
Shannon Woods53a94a82014-06-24 15:20:36 -04001251 if (context->getState().getReadFramebuffer()->id() != 0 && framebuffer->getSamples() != 0)
Jamie Madill560a8d82014-05-21 13:06:20 -04001252 {
Geoff Langb1196682014-07-23 13:47:29 -04001253 context->recordError(Error(GL_INVALID_OPERATION));
1254 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001255 }
1256
Geoff Langaae65a42014-05-26 12:43:44 -04001257 const gl::Caps &caps = context->getCaps();
1258
Jamie Madill560a8d82014-05-21 13:06:20 -04001259 gl::Texture *texture = NULL;
1260 GLenum textureInternalFormat = GL_NONE;
Jamie Madill560a8d82014-05-21 13:06:20 -04001261 GLint textureLevelWidth = 0;
1262 GLint textureLevelHeight = 0;
1263 GLint textureLevelDepth = 0;
Geoff Langaae65a42014-05-26 12:43:44 -04001264 GLuint maxDimension = 0;
Jamie Madill560a8d82014-05-21 13:06:20 -04001265
1266 switch (target)
1267 {
1268 case GL_TEXTURE_2D:
1269 {
1270 gl::Texture2D *texture2d = context->getTexture2D();
1271 if (texture2d)
1272 {
1273 textureInternalFormat = texture2d->getInternalFormat(level);
Jamie Madill560a8d82014-05-21 13:06:20 -04001274 textureLevelWidth = texture2d->getWidth(level);
1275 textureLevelHeight = texture2d->getHeight(level);
1276 textureLevelDepth = 1;
1277 texture = texture2d;
Geoff Langaae65a42014-05-26 12:43:44 -04001278 maxDimension = caps.max2DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001279 }
1280 }
1281 break;
1282
1283 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1284 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1285 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1286 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1287 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1288 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1289 {
1290 gl::TextureCubeMap *textureCube = context->getTextureCubeMap();
1291 if (textureCube)
1292 {
1293 textureInternalFormat = textureCube->getInternalFormat(target, level);
Jamie Madill560a8d82014-05-21 13:06:20 -04001294 textureLevelWidth = textureCube->getWidth(target, level);
1295 textureLevelHeight = textureCube->getHeight(target, level);
1296 textureLevelDepth = 1;
1297 texture = textureCube;
Geoff Langaae65a42014-05-26 12:43:44 -04001298 maxDimension = caps.maxCubeMapTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001299 }
1300 }
1301 break;
1302
1303 case GL_TEXTURE_2D_ARRAY:
1304 {
1305 gl::Texture2DArray *texture2dArray = context->getTexture2DArray();
1306 if (texture2dArray)
1307 {
1308 textureInternalFormat = texture2dArray->getInternalFormat(level);
Jamie Madill560a8d82014-05-21 13:06:20 -04001309 textureLevelWidth = texture2dArray->getWidth(level);
1310 textureLevelHeight = texture2dArray->getHeight(level);
1311 textureLevelDepth = texture2dArray->getLayers(level);
1312 texture = texture2dArray;
Geoff Langaae65a42014-05-26 12:43:44 -04001313 maxDimension = caps.max2DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001314 }
1315 }
1316 break;
1317
1318 case GL_TEXTURE_3D:
1319 {
1320 gl::Texture3D *texture3d = context->getTexture3D();
1321 if (texture3d)
1322 {
1323 textureInternalFormat = texture3d->getInternalFormat(level);
Jamie Madill560a8d82014-05-21 13:06:20 -04001324 textureLevelWidth = texture3d->getWidth(level);
1325 textureLevelHeight = texture3d->getHeight(level);
1326 textureLevelDepth = texture3d->getDepth(level);
1327 texture = texture3d;
Geoff Langaae65a42014-05-26 12:43:44 -04001328 maxDimension = caps.max3DTextureSize;
Jamie Madill560a8d82014-05-21 13:06:20 -04001329 }
1330 }
1331 break;
1332
1333 default:
Geoff Langb1196682014-07-23 13:47:29 -04001334 context->recordError(Error(GL_INVALID_ENUM));
1335 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001336 }
1337
1338 if (!texture)
1339 {
Geoff Langb1196682014-07-23 13:47:29 -04001340 context->recordError(Error(GL_INVALID_OPERATION));
1341 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001342 }
1343
1344 if (texture->isImmutable() && !isSubImage)
1345 {
Geoff Langb1196682014-07-23 13:47:29 -04001346 context->recordError(Error(GL_INVALID_OPERATION));
1347 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001348 }
1349
Geoff Lang5d601382014-07-22 15:14:06 -04001350 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalformat);
1351
1352 if (formatInfo.depthBits > 0)
Jamie Madill560a8d82014-05-21 13:06:20 -04001353 {
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
Geoff Lang5d601382014-07-22 15:14:06 -04001358 if (formatInfo.compressed)
Jamie Madill560a8d82014-05-21 13:06:20 -04001359 {
Geoff Lang5d601382014-07-22 15:14:06 -04001360 if (((width % formatInfo.compressedBlockWidth) != 0 && width != textureLevelWidth) ||
1361 ((height % formatInfo.compressedBlockHeight) != 0 && height != textureLevelHeight))
Jamie Madill560a8d82014-05-21 13:06:20 -04001362 {
Geoff Langb1196682014-07-23 13:47:29 -04001363 context->recordError(Error(GL_INVALID_OPERATION));
1364 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001365 }
1366 }
1367
1368 if (isSubImage)
1369 {
1370 if (xoffset + width > textureLevelWidth ||
1371 yoffset + height > textureLevelHeight ||
1372 zoffset >= textureLevelDepth)
1373 {
Geoff Langb1196682014-07-23 13:47:29 -04001374 context->recordError(Error(GL_INVALID_VALUE));
1375 return false;
Jamie Madill560a8d82014-05-21 13:06:20 -04001376 }
1377 }
Jamie Madill6f38f822014-06-06 17:12:20 -04001378 else
1379 {
1380 if (IsCubemapTextureTarget(target) && width != height)
1381 {
Geoff Langb1196682014-07-23 13:47:29 -04001382 context->recordError(Error(GL_INVALID_VALUE));
1383 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04001384 }
1385
Geoff Lang5d601382014-07-22 15:14:06 -04001386 if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions()))
Jamie Madill6f38f822014-06-06 17:12:20 -04001387 {
Geoff Langb1196682014-07-23 13:47:29 -04001388 context->recordError(Error(GL_INVALID_ENUM));
1389 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04001390 }
1391
1392 int maxLevelDimension = (maxDimension >> level);
1393 if (static_cast<int>(width) > maxLevelDimension || static_cast<int>(height) > maxLevelDimension)
1394 {
Geoff Langb1196682014-07-23 13:47:29 -04001395 context->recordError(Error(GL_INVALID_VALUE));
1396 return false;
Jamie Madill6f38f822014-06-06 17:12:20 -04001397 }
1398 }
Jamie Madill560a8d82014-05-21 13:06:20 -04001399
1400 *textureFormatOut = textureInternalFormat;
1401 return true;
1402}
1403
Geoff Langb1196682014-07-23 13:47:29 -04001404static bool ValidateDrawBase(Context *context, GLenum mode, GLsizei count, GLsizei maxVertex, GLsizei primcount)
Jamie Madill250d33f2014-06-06 17:09:03 -04001405{
Jamie Madill1aeb1312014-06-20 13:21:25 -04001406 switch (mode)
1407 {
1408 case GL_POINTS:
1409 case GL_LINES:
1410 case GL_LINE_LOOP:
1411 case GL_LINE_STRIP:
1412 case GL_TRIANGLES:
1413 case GL_TRIANGLE_STRIP:
1414 case GL_TRIANGLE_FAN:
1415 break;
1416 default:
Geoff Langb1196682014-07-23 13:47:29 -04001417 context->recordError(Error(GL_INVALID_ENUM));
1418 return false;
Jamie Madill1aeb1312014-06-20 13:21:25 -04001419 }
1420
Jamie Madill250d33f2014-06-06 17:09:03 -04001421 if (count < 0)
1422 {
Geoff Langb1196682014-07-23 13:47:29 -04001423 context->recordError(Error(GL_INVALID_VALUE));
1424 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001425 }
1426
Geoff Langb1196682014-07-23 13:47:29 -04001427 const State &state = context->getState();
1428
Jamie Madill250d33f2014-06-06 17:09:03 -04001429 // Check for mapped buffers
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001430 if (state.hasMappedBuffer(GL_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001431 {
Geoff Langb1196682014-07-23 13:47:29 -04001432 context->recordError(Error(GL_INVALID_OPERATION));
1433 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001434 }
1435
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001436 const gl::DepthStencilState &depthStencilState = state.getDepthStencilState();
Jamie Madillac528012014-06-20 13:21:23 -04001437 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001438 state.getStencilRef() != state.getStencilBackRef() ||
Jamie Madillac528012014-06-20 13:21:23 -04001439 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
1440 {
1441 // Note: these separate values are not supported in WebGL, due to D3D's limitations.
1442 // See Section 6.10 of the WebGL 1.0 spec
1443 ERR("This ANGLE implementation does not support separate front/back stencil "
1444 "writemasks, reference values, or stencil mask values.");
Geoff Langb1196682014-07-23 13:47:29 -04001445 context->recordError(Error(GL_INVALID_OPERATION));
1446 return false;
Jamie Madillac528012014-06-20 13:21:23 -04001447 }
1448
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001449 const gl::Framebuffer *fbo = state.getDrawFramebuffer();
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001450 if (!fbo || fbo->completeness() != GL_FRAMEBUFFER_COMPLETE)
1451 {
Geoff Langb1196682014-07-23 13:47:29 -04001452 context->recordError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
1453 return false;
Jamie Madill13f7d7d2014-06-20 13:21:27 -04001454 }
1455
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001456 if (state.getCurrentProgramId() == 0)
Jamie Madilld4cfa572014-07-08 10:00:32 -04001457 {
Geoff Langb1196682014-07-23 13:47:29 -04001458 context->recordError(Error(GL_INVALID_OPERATION));
1459 return false;
Jamie Madilld4cfa572014-07-08 10:00:32 -04001460 }
1461
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001462 gl::ProgramBinary *programBinary = state.getCurrentProgramBinary();
Brandon Jones43a53e22014-08-28 16:23:22 -07001463 if (!programBinary->validateSamplers(NULL, context->getCaps()))
Jamie Madilld4cfa572014-07-08 10:00:32 -04001464 {
Geoff Langb1196682014-07-23 13:47:29 -04001465 context->recordError(Error(GL_INVALID_OPERATION));
1466 return false;
Jamie Madilld4cfa572014-07-08 10:00:32 -04001467 }
1468
Jamie Madill2b976812014-08-25 15:47:49 -04001469 // Buffer validations
1470 const VertexArray *vao = state.getVertexArray();
1471 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
1472 {
1473 const VertexAttribute &attrib = vao->getVertexAttribute(attributeIndex);
1474 bool attribActive = (programBinary->getSemanticIndex(attributeIndex) != -1);
1475 if (attribActive && attrib.enabled)
1476 {
1477 gl::Buffer *buffer = attrib.buffer.get();
1478
1479 if (buffer)
1480 {
1481 GLint64 attribStride = static_cast<GLint64>(ComputeVertexAttributeStride(attrib));
1482 GLint64 maxVertexElement = 0;
1483
1484 if (attrib.divisor > 0)
1485 {
1486 maxVertexElement = static_cast<GLint64>(primcount) / static_cast<GLint64>(attrib.divisor);
1487 }
1488 else
1489 {
1490 maxVertexElement = static_cast<GLint64>(maxVertex);
1491 }
1492
1493 GLint64 attribDataSize = maxVertexElement * attribStride;
1494
1495 // [OpenGL ES 3.0.2] section 2.9.4 page 40:
1496 // We can return INVALID_OPERATION if our vertex attribute does not have
1497 // enough backing data.
1498 if (attribDataSize > buffer->getSize())
1499 {
Geoff Langb1196682014-07-23 13:47:29 -04001500 context->recordError(Error(GL_INVALID_OPERATION));
1501 return false;
Jamie Madill2b976812014-08-25 15:47:49 -04001502 }
1503 }
1504 else if (attrib.pointer == NULL)
1505 {
1506 // This is an application error that would normally result in a crash,
1507 // but we catch it and return an error
Geoff Langb1196682014-07-23 13:47:29 -04001508 context->recordError(Error(GL_INVALID_OPERATION, "An enabled vertex array has no buffer and no pointer."));
1509 return false;
Jamie Madill2b976812014-08-25 15:47:49 -04001510 }
1511 }
1512 }
1513
Jamie Madill250d33f2014-06-06 17:09:03 -04001514 // No-op if zero count
1515 return (count > 0);
1516}
1517
Geoff Langb1196682014-07-23 13:47:29 -04001518bool ValidateDrawArrays(Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
Jamie Madill250d33f2014-06-06 17:09:03 -04001519{
Jamie Madillfd716582014-06-06 17:09:04 -04001520 if (first < 0)
Jamie Madill250d33f2014-06-06 17:09:03 -04001521 {
Geoff Langb1196682014-07-23 13:47:29 -04001522 context->recordError(Error(GL_INVALID_VALUE));
1523 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001524 }
1525
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001526 const State &state = context->getState();
1527 gl::TransformFeedback *curTransformFeedback = state.getCurrentTransformFeedback();
Jamie Madillfd716582014-06-06 17:09:04 -04001528 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused() &&
1529 curTransformFeedback->getDrawMode() != mode)
1530 {
1531 // It is an invalid operation to call DrawArrays or DrawArraysInstanced with a draw mode
1532 // that does not match the current transform feedback object's draw mode (if transform feedback
1533 // is active), (3.0.2, section 2.14, pg 86)
Geoff Langb1196682014-07-23 13:47:29 -04001534 context->recordError(Error(GL_INVALID_OPERATION));
1535 return false;
Jamie Madillfd716582014-06-06 17:09:04 -04001536 }
1537
Geoff Langb1196682014-07-23 13:47:29 -04001538 if (!ValidateDrawBase(context, mode, count, count, primcount))
Jamie Madillfd716582014-06-06 17:09:04 -04001539 {
1540 return false;
1541 }
1542
1543 return true;
1544}
1545
Geoff Langb1196682014-07-23 13:47:29 -04001546bool ValidateDrawArraysInstanced(Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
Jamie Madillfd716582014-06-06 17:09:04 -04001547{
1548 if (primcount < 0)
1549 {
Geoff Langb1196682014-07-23 13:47:29 -04001550 context->recordError(Error(GL_INVALID_VALUE));
1551 return false;
Jamie Madillfd716582014-06-06 17:09:04 -04001552 }
1553
Jamie Madill2b976812014-08-25 15:47:49 -04001554 if (!ValidateDrawArrays(context, mode, first, count, primcount))
Jamie Madillfd716582014-06-06 17:09:04 -04001555 {
1556 return false;
1557 }
1558
1559 // No-op if zero primitive count
1560 return (primcount > 0);
1561}
1562
Geoff Lang87a93302014-09-16 13:29:43 -04001563static bool ValidateDrawInstancedANGLE(Context *context)
1564{
1565 // Verify there is at least one active attribute with a divisor of zero
1566 const gl::State& state = context->getState();
1567
1568 gl::ProgramBinary *programBinary = state.getCurrentProgramBinary();
1569
1570 const VertexArray *vao = state.getVertexArray();
1571 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
1572 {
1573 const VertexAttribute &attrib = vao->getVertexAttribute(attributeIndex);
1574 bool active = (programBinary->getSemanticIndex(attributeIndex) != -1);
1575 if (active && attrib.divisor == 0)
1576 {
1577 return true;
1578 }
1579 }
1580
1581 context->recordError(Error(GL_INVALID_OPERATION, "ANGLE_instanced_arrays requires that at least one active attribute"
1582 "has a divisor of zero."));
1583 return false;
1584}
1585
1586bool ValidateDrawArraysInstancedANGLE(Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
1587{
1588 if (!ValidateDrawInstancedANGLE(context))
1589 {
1590 return false;
1591 }
1592
1593 return ValidateDrawArraysInstanced(context, mode, first, count, primcount);
1594}
1595
Geoff Langb1196682014-07-23 13:47:29 -04001596bool ValidateDrawElements(Context *context, GLenum mode, GLsizei count, GLenum type,
Jamie Madill2b976812014-08-25 15:47:49 -04001597 const GLvoid* indices, GLsizei primcount, rx::RangeUI *indexRangeOut)
Jamie Madillfd716582014-06-06 17:09:04 -04001598{
Jamie Madill250d33f2014-06-06 17:09:03 -04001599 switch (type)
1600 {
1601 case GL_UNSIGNED_BYTE:
1602 case GL_UNSIGNED_SHORT:
1603 break;
1604 case GL_UNSIGNED_INT:
Geoff Langc0b9ef42014-07-02 10:02:37 -04001605 if (!context->getExtensions().elementIndexUint)
Jamie Madill250d33f2014-06-06 17:09:03 -04001606 {
Geoff Langb1196682014-07-23 13:47:29 -04001607 context->recordError(Error(GL_INVALID_ENUM));
1608 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001609 }
1610 break;
1611 default:
Geoff Langb1196682014-07-23 13:47:29 -04001612 context->recordError(Error(GL_INVALID_ENUM));
1613 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001614 }
1615
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001616 const State &state = context->getState();
1617
1618 gl::TransformFeedback *curTransformFeedback = state.getCurrentTransformFeedback();
Jamie Madill250d33f2014-06-06 17:09:03 -04001619 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused())
1620 {
1621 // It is an invalid operation to call DrawElements, DrawRangeElements or DrawElementsInstanced
1622 // while transform feedback is active, (3.0.2, section 2.14, pg 86)
Geoff Langb1196682014-07-23 13:47:29 -04001623 context->recordError(Error(GL_INVALID_OPERATION));
1624 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001625 }
1626
1627 // Check for mapped buffers
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001628 if (state.hasMappedBuffer(GL_ELEMENT_ARRAY_BUFFER))
Jamie Madill250d33f2014-06-06 17:09:03 -04001629 {
Geoff Langb1196682014-07-23 13:47:29 -04001630 context->recordError(Error(GL_INVALID_OPERATION));
1631 return false;
Jamie Madill250d33f2014-06-06 17:09:03 -04001632 }
1633
Jamie Madill2b976812014-08-25 15:47:49 -04001634 const gl::VertexArray *vao = state.getVertexArray();
1635 const gl::Buffer *elementArrayBuffer = vao->getElementArrayBuffer();
1636 if (!indices && !elementArrayBuffer)
Jamie Madilld4cfa572014-07-08 10:00:32 -04001637 {
Geoff Langb1196682014-07-23 13:47:29 -04001638 context->recordError(Error(GL_INVALID_OPERATION));
1639 return false;
Jamie Madilld4cfa572014-07-08 10:00:32 -04001640 }
1641
Jamie Madillae3000b2014-08-25 15:47:51 -04001642 if (elementArrayBuffer)
1643 {
1644 const gl::Type &typeInfo = gl::GetTypeInfo(type);
1645
1646 GLint64 offset = reinterpret_cast<GLint64>(indices);
1647 GLint64 byteCount = static_cast<GLint64>(typeInfo.bytes) * static_cast<GLint64>(count)+offset;
1648
1649 // check for integer overflows
1650 if (static_cast<GLuint>(count) > (std::numeric_limits<GLuint>::max() / typeInfo.bytes) ||
1651 byteCount > static_cast<GLint64>(std::numeric_limits<GLuint>::max()))
1652 {
Geoff Langb1196682014-07-23 13:47:29 -04001653 context->recordError(Error(GL_OUT_OF_MEMORY));
1654 return false;
Jamie Madillae3000b2014-08-25 15:47:51 -04001655 }
1656
1657 // Check for reading past the end of the bound buffer object
1658 if (byteCount > elementArrayBuffer->getSize())
1659 {
Geoff Langb1196682014-07-23 13:47:29 -04001660 context->recordError(Error(GL_INVALID_OPERATION));
1661 return false;
Jamie Madillae3000b2014-08-25 15:47:51 -04001662 }
1663 }
1664 else if (!indices)
1665 {
1666 // Catch this programming error here
Geoff Langb1196682014-07-23 13:47:29 -04001667 context->recordError(Error(GL_INVALID_OPERATION));
1668 return false;
Jamie Madillae3000b2014-08-25 15:47:51 -04001669 }
1670
Jamie Madill2b976812014-08-25 15:47:49 -04001671 // Use max index to validate if our vertex buffers are large enough for the pull.
1672 // TODO: offer fast path, with disabled index validation.
1673 // TODO: also disable index checking on back-ends that are robust to out-of-range accesses.
1674 if (elementArrayBuffer)
1675 {
1676 unsigned int offset = reinterpret_cast<unsigned int>(indices);
1677 if (!elementArrayBuffer->getIndexRangeCache()->findRange(type, offset, count, indexRangeOut, NULL))
1678 {
1679 const void *dataPointer = elementArrayBuffer->getImplementation()->getData();
1680 const uint8_t *offsetPointer = static_cast<const uint8_t *>(dataPointer) + offset;
1681 *indexRangeOut = rx::IndexRangeCache::ComputeRange(type, offsetPointer, count);
1682 }
1683 }
1684 else
1685 {
1686 *indexRangeOut = rx::IndexRangeCache::ComputeRange(type, indices, count);
1687 }
1688
Geoff Langb1196682014-07-23 13:47:29 -04001689 if (!ValidateDrawBase(context, mode, count, static_cast<GLsizei>(indexRangeOut->end), primcount))
Jamie Madillfd716582014-06-06 17:09:04 -04001690 {
1691 return false;
1692 }
1693
1694 return true;
1695}
1696
Geoff Langb1196682014-07-23 13:47:29 -04001697bool ValidateDrawElementsInstanced(Context *context,
Jamie Madill2b976812014-08-25 15:47:49 -04001698 GLenum mode, GLsizei count, GLenum type,
1699 const GLvoid *indices, GLsizei primcount,
1700 rx::RangeUI *indexRangeOut)
Jamie Madillfd716582014-06-06 17:09:04 -04001701{
1702 if (primcount < 0)
1703 {
Geoff Langb1196682014-07-23 13:47:29 -04001704 context->recordError(Error(GL_INVALID_VALUE));
1705 return false;
Jamie Madillfd716582014-06-06 17:09:04 -04001706 }
1707
Jamie Madill2b976812014-08-25 15:47:49 -04001708 if (!ValidateDrawElements(context, mode, count, type, indices, primcount, indexRangeOut))
Jamie Madillfd716582014-06-06 17:09:04 -04001709 {
1710 return false;
1711 }
1712
1713 // No-op zero primitive count
1714 return (primcount > 0);
Jamie Madill250d33f2014-06-06 17:09:03 -04001715}
1716
Geoff Lang87a93302014-09-16 13:29:43 -04001717bool ValidateDrawElementsInstancedANGLE(Context *context, GLenum mode, GLsizei count, GLenum type,
1718 const GLvoid *indices, GLsizei primcount, rx::RangeUI *indexRangeOut)
1719{
1720 if (!ValidateDrawInstancedANGLE(context))
1721 {
1722 return false;
1723 }
1724
1725 return ValidateDrawElementsInstanced(context, mode, count, type, indices, primcount, indexRangeOut);
1726}
1727
Geoff Langb1196682014-07-23 13:47:29 -04001728bool ValidateFramebufferTextureBase(Context *context, GLenum target, GLenum attachment,
Jamie Madill55ec3b12014-07-03 10:38:57 -04001729 GLuint texture, GLint level)
Jamie Madill570f7c82014-07-03 10:38:54 -04001730{
Jamie Madill55ec3b12014-07-03 10:38:57 -04001731 if (!ValidFramebufferTarget(target))
1732 {
Geoff Langb1196682014-07-23 13:47:29 -04001733 context->recordError(Error(GL_INVALID_ENUM));
1734 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001735 }
1736
1737 if (!ValidateAttachmentTarget(context, attachment))
Jamie Madill570f7c82014-07-03 10:38:54 -04001738 {
1739 return false;
1740 }
1741
Jamie Madill55ec3b12014-07-03 10:38:57 -04001742 if (texture != 0)
1743 {
1744 gl::Texture *tex = context->getTexture(texture);
1745
1746 if (tex == NULL)
1747 {
Geoff Langb1196682014-07-23 13:47:29 -04001748 context->recordError(Error(GL_INVALID_OPERATION));
1749 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001750 }
1751
1752 if (level < 0)
1753 {
Geoff Langb1196682014-07-23 13:47:29 -04001754 context->recordError(Error(GL_INVALID_VALUE));
1755 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001756 }
1757 }
1758
Shannon Woods53a94a82014-06-24 15:20:36 -04001759 const gl::Framebuffer *framebuffer = context->getState().getTargetFramebuffer(target);
1760 GLuint framebufferHandle = context->getState().getTargetFramebuffer(target)->id();
Jamie Madill55ec3b12014-07-03 10:38:57 -04001761
1762 if (framebufferHandle == 0 || !framebuffer)
1763 {
Geoff Langb1196682014-07-23 13:47:29 -04001764 context->recordError(Error(GL_INVALID_OPERATION));
1765 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001766 }
1767
1768 return true;
1769}
1770
Geoff Langb1196682014-07-23 13:47:29 -04001771bool ValidateFramebufferTexture2D(Context *context, GLenum target, GLenum attachment,
Jamie Madill55ec3b12014-07-03 10:38:57 -04001772 GLenum textarget, GLuint texture, GLint level)
1773{
1774 // Attachments are required to be bound to level 0 in ES2
1775 if (context->getClientVersion() < 3 && 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 if (!ValidateFramebufferTextureBase(context, target, attachment, texture, level))
Jamie Madill570f7c82014-07-03 10:38:54 -04001782 {
1783 return false;
1784 }
1785
Jamie Madill55ec3b12014-07-03 10:38:57 -04001786 if (texture != 0)
1787 {
1788 gl::Texture *tex = context->getTexture(texture);
1789 ASSERT(tex);
1790
Jamie Madill2a6564e2014-07-11 09:53:19 -04001791 const gl::Caps &caps = context->getCaps();
1792
Jamie Madill55ec3b12014-07-03 10:38:57 -04001793 switch (textarget)
1794 {
1795 case GL_TEXTURE_2D:
1796 {
Jamie Madill2a6564e2014-07-11 09:53:19 -04001797 if (level > gl::log2(caps.max2DTextureSize))
Jamie Madill55ec3b12014-07-03 10:38:57 -04001798 {
Geoff Langb1196682014-07-23 13:47:29 -04001799 context->recordError(Error(GL_INVALID_VALUE));
1800 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001801 }
1802 if (tex->getTarget() != GL_TEXTURE_2D)
1803 {
Geoff Langb1196682014-07-23 13:47:29 -04001804 context->recordError(Error(GL_INVALID_OPERATION));
1805 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001806 }
1807 gl::Texture2D *tex2d = static_cast<gl::Texture2D *>(tex);
1808 if (tex2d->isCompressed(level))
1809 {
Geoff Langb1196682014-07-23 13:47:29 -04001810 context->recordError(Error(GL_INVALID_OPERATION));
1811 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001812 }
1813 }
1814 break;
1815
1816 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1817 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1818 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1819 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1820 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1821 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1822 {
Jamie Madill2a6564e2014-07-11 09:53:19 -04001823 if (level > gl::log2(caps.maxCubeMapTextureSize))
Jamie Madill55ec3b12014-07-03 10:38:57 -04001824 {
Geoff Langb1196682014-07-23 13:47:29 -04001825 context->recordError(Error(GL_INVALID_VALUE));
1826 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001827 }
1828 if (tex->getTarget() != GL_TEXTURE_CUBE_MAP)
1829 {
Geoff Langb1196682014-07-23 13:47:29 -04001830 context->recordError(Error(GL_INVALID_OPERATION));
1831 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001832 }
1833 gl::TextureCubeMap *texcube = static_cast<gl::TextureCubeMap *>(tex);
1834 if (texcube->isCompressed(textarget, level))
1835 {
Geoff Langb1196682014-07-23 13:47:29 -04001836 context->recordError(Error(GL_INVALID_OPERATION));
1837 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001838 }
1839 }
1840 break;
1841
1842 default:
Geoff Langb1196682014-07-23 13:47:29 -04001843 context->recordError(Error(GL_INVALID_ENUM));
1844 return false;
Jamie Madill55ec3b12014-07-03 10:38:57 -04001845 }
1846 }
1847
Jamie Madill570f7c82014-07-03 10:38:54 -04001848 return true;
1849}
1850
Geoff Langb1196682014-07-23 13:47:29 -04001851bool ValidateGetUniformBase(Context *context, GLuint program, GLint location)
Jamie Madill0063c512014-08-25 15:47:53 -04001852{
1853 if (program == 0)
1854 {
Geoff Langb1196682014-07-23 13:47:29 -04001855 context->recordError(Error(GL_INVALID_VALUE));
1856 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001857 }
1858
1859 gl::Program *programObject = context->getProgram(program);
1860
1861 if (!programObject || !programObject->isLinked())
1862 {
Geoff Langb1196682014-07-23 13:47:29 -04001863 context->recordError(Error(GL_INVALID_OPERATION));
1864 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001865 }
1866
1867 gl::ProgramBinary *programBinary = programObject->getProgramBinary();
1868 if (!programBinary)
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
Jamie Madill549c7fd2014-08-25 15:47:56 -04001874 if (!programBinary->isValidUniformLocation(location))
1875 {
Geoff Langb1196682014-07-23 13:47:29 -04001876 context->recordError(Error(GL_INVALID_OPERATION));
1877 return false;
Jamie Madill549c7fd2014-08-25 15:47:56 -04001878 }
1879
Jamie Madill0063c512014-08-25 15:47:53 -04001880 return true;
1881}
1882
Geoff Langb1196682014-07-23 13:47:29 -04001883bool ValidateGetUniformfv(Context *context, GLuint program, GLint location, GLfloat* params)
Jamie Madill78f41802014-08-25 15:47:55 -04001884{
1885 return ValidateGetUniformBase(context, program, location);
1886}
1887
Geoff Langb1196682014-07-23 13:47:29 -04001888bool ValidateGetUniformiv(Context *context, GLuint program, GLint location, GLint* params)
Jamie Madill0063c512014-08-25 15:47:53 -04001889{
Jamie Madill78f41802014-08-25 15:47:55 -04001890 return ValidateGetUniformBase(context, program, location);
1891}
1892
Geoff Langb1196682014-07-23 13:47:29 -04001893static bool ValidateSizedGetUniform(Context *context, GLuint program, GLint location, GLsizei bufSize)
Jamie Madill78f41802014-08-25 15:47:55 -04001894{
1895 if (!ValidateGetUniformBase(context, program, location))
Jamie Madill0063c512014-08-25 15:47:53 -04001896 {
Jamie Madill78f41802014-08-25 15:47:55 -04001897 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001898 }
1899
Jamie Madilla502c742014-08-28 17:19:13 -04001900 gl::Program *programObject = context->getProgram(program);
1901 ASSERT(programObject);
1902 gl::ProgramBinary *programBinary = programObject->getProgramBinary();
Jamie Madill0063c512014-08-25 15:47:53 -04001903
Jamie Madill78f41802014-08-25 15:47:55 -04001904 // sized queries -- ensure the provided buffer is large enough
1905 LinkedUniform *uniform = programBinary->getUniformByLocation(location);
1906 size_t requiredBytes = VariableExternalSize(uniform->type);
1907 if (static_cast<size_t>(bufSize) < requiredBytes)
Jamie Madill0063c512014-08-25 15:47:53 -04001908 {
Geoff Langb1196682014-07-23 13:47:29 -04001909 context->recordError(Error(GL_INVALID_OPERATION));
1910 return false;
Jamie Madill0063c512014-08-25 15:47:53 -04001911 }
1912
1913 return true;
1914}
1915
Geoff Langb1196682014-07-23 13:47:29 -04001916bool ValidateGetnUniformfvEXT(Context *context, GLuint program, GLint location, GLsizei bufSize, GLfloat* params)
Jamie Madill0063c512014-08-25 15:47:53 -04001917{
Jamie Madill78f41802014-08-25 15:47:55 -04001918 return ValidateSizedGetUniform(context, program, location, bufSize);
Jamie Madill0063c512014-08-25 15:47:53 -04001919}
1920
Geoff Langb1196682014-07-23 13:47:29 -04001921bool ValidateGetnUniformivEXT(Context *context, GLuint program, GLint location, GLsizei bufSize, GLint* params)
Jamie Madill0063c512014-08-25 15:47:53 -04001922{
Jamie Madill78f41802014-08-25 15:47:55 -04001923 return ValidateSizedGetUniform(context, program, location, bufSize);
Jamie Madill0063c512014-08-25 15:47:53 -04001924}
1925
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001926}