blob: 91df2bea2ffce551324055c2e7995db80bb59662 [file] [log] [blame]
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001#include "precompiled.h"
2//
3// Copyright (c) 2013 The ANGLE Project Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
8// validationES.h: Validation functions for generic OpenGL ES entry point parameters
9
10#include "libGLESv2/validationES.h"
Jamie Madill26e91952014-03-05 15:01:27 -050011#include "libGLESv2/validationES2.h"
12#include "libGLESv2/validationES3.h"
Geoff Lange8ebe7f2013-08-05 15:03:13 -040013#include "libGLESv2/Context.h"
14#include "libGLESv2/Texture.h"
15#include "libGLESv2/Framebuffer.h"
16#include "libGLESv2/Renderbuffer.h"
17#include "libGLESv2/formatutils.h"
18#include "libGLESv2/main.h"
Jamie Madilldb2f14c2014-05-13 13:56:30 -040019#include "libGLESv2/Query.h"
Jamie Madill36398922014-05-20 14:51:53 -040020#include "libGLESv2/ProgramBinary.h"
Geoff Lange8ebe7f2013-08-05 15:03:13 -040021
22#include "common/mathutil.h"
23#include "common/utilities.h"
24
25namespace gl
26{
27
Geoff Lang0550d032014-01-30 11:29:07 -050028bool ValidCap(const Context *context, GLenum cap)
29{
30 switch (cap)
31 {
32 case GL_CULL_FACE:
33 case GL_POLYGON_OFFSET_FILL:
34 case GL_SAMPLE_ALPHA_TO_COVERAGE:
35 case GL_SAMPLE_COVERAGE:
36 case GL_SCISSOR_TEST:
37 case GL_STENCIL_TEST:
38 case GL_DEPTH_TEST:
39 case GL_BLEND:
40 case GL_DITHER:
41 return true;
42 case GL_PRIMITIVE_RESTART_FIXED_INDEX:
43 case GL_RASTERIZER_DISCARD:
44 return (context->getClientVersion() >= 3);
45 default:
46 return false;
47 }
48}
49
Jamie Madill1fc7e2c2014-01-21 16:47:10 -050050bool ValidTextureTarget(const Context *context, GLenum target)
Jamie Madill35d15012013-10-07 10:46:37 -040051{
Jamie Madilld7460c72014-01-21 16:38:14 -050052 switch (target)
Jamie Madill35d15012013-10-07 10:46:37 -040053 {
Jamie Madilld7460c72014-01-21 16:38:14 -050054 case GL_TEXTURE_2D:
55 case GL_TEXTURE_CUBE_MAP:
56 return true;
Jamie Madill35d15012013-10-07 10:46:37 -040057
Jamie Madilld7460c72014-01-21 16:38:14 -050058 case GL_TEXTURE_3D:
59 case GL_TEXTURE_2D_ARRAY:
60 return (context->getClientVersion() >= 3);
61
62 default:
63 return false;
64 }
Jamie Madill35d15012013-10-07 10:46:37 -040065}
66
Shannon Woods4dfed832014-03-17 20:03:39 -040067// This function differs from ValidTextureTarget in that the target must be
68// usable as the destination of a 2D operation-- so a cube face is valid, but
69// GL_TEXTURE_CUBE_MAP is not.
Jamie Madill560a8d82014-05-21 13:06:20 -040070// Note: duplicate of IsInternalTextureTarget
Shannon Woods4dfed832014-03-17 20:03:39 -040071bool ValidTexture2DDestinationTarget(const Context *context, GLenum target)
72{
73 switch (target)
74 {
75 case GL_TEXTURE_2D:
76 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
77 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
78 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
79 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
80 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
81 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
82 return true;
83 case GL_TEXTURE_2D_ARRAY:
84 case GL_TEXTURE_3D:
85 return (context->getClientVersion() >= 3);
86 default:
87 return false;
88 }
89}
90
Jamie Madill1fc7e2c2014-01-21 16:47:10 -050091bool ValidFramebufferTarget(GLenum target)
92{
93 META_ASSERT(GL_DRAW_FRAMEBUFFER_ANGLE == GL_DRAW_FRAMEBUFFER && GL_READ_FRAMEBUFFER_ANGLE == GL_READ_FRAMEBUFFER);
94
95 switch (target)
96 {
97 case GL_FRAMEBUFFER: return true;
98 case GL_READ_FRAMEBUFFER: return true;
99 case GL_DRAW_FRAMEBUFFER: return true;
100 default: return false;
101 }
102}
103
Jamie Madill8c96d582014-03-05 15:01:23 -0500104bool ValidBufferTarget(const Context *context, GLenum target)
105{
106 switch (target)
107 {
108 case GL_ARRAY_BUFFER:
109 case GL_ELEMENT_ARRAY_BUFFER:
110 return true;
111
Jamie Madill8c96d582014-03-05 15:01:23 -0500112 case GL_PIXEL_PACK_BUFFER:
113 case GL_PIXEL_UNPACK_BUFFER:
Shannon Woods158c4382014-05-06 13:00:07 -0400114 return context->supportsPBOs();
115
Shannon Woodsb3801742014-03-27 14:59:19 -0400116 case GL_COPY_READ_BUFFER:
117 case GL_COPY_WRITE_BUFFER:
Jamie Madill8c96d582014-03-05 15:01:23 -0500118 case GL_TRANSFORM_FEEDBACK_BUFFER:
119 case GL_UNIFORM_BUFFER:
120 return (context->getClientVersion() >= 3);
121
122 default:
123 return false;
124 }
125}
126
Jamie Madill70656a62014-03-05 15:01:26 -0500127bool ValidBufferParameter(const Context *context, GLenum pname)
128{
129 switch (pname)
130 {
131 case GL_BUFFER_USAGE:
132 case GL_BUFFER_SIZE:
133 return true;
134
135 // GL_BUFFER_MAP_POINTER is a special case, and may only be
136 // queried with GetBufferPointerv
137 case GL_BUFFER_ACCESS_FLAGS:
138 case GL_BUFFER_MAPPED:
139 case GL_BUFFER_MAP_OFFSET:
140 case GL_BUFFER_MAP_LENGTH:
141 return (context->getClientVersion() >= 3);
142
143 default:
144 return false;
145 }
146}
147
Jamie Madill8c96d582014-03-05 15:01:23 -0500148bool ValidMipLevel(const Context *context, GLenum target, GLint level)
Geoff Langce635692013-09-24 13:56:32 -0400149{
150 int maxLevel = 0;
151 switch (target)
152 {
153 case GL_TEXTURE_2D: maxLevel = context->getMaximum2DTextureLevel(); break;
154 case GL_TEXTURE_CUBE_MAP:
155 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
156 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
157 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
158 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
159 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
160 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: maxLevel = context->getMaximumCubeTextureLevel(); break;
161 case GL_TEXTURE_3D: maxLevel = context->getMaximum3DTextureLevel(); break;
162 case GL_TEXTURE_2D_ARRAY: maxLevel = context->getMaximum2DArrayTextureLevel(); break;
163 default: UNREACHABLE();
164 }
165
166 return level < maxLevel;
167}
168
169bool ValidImageSize(const gl::Context *context, GLenum target, GLint level, GLsizei width, GLsizei height, GLsizei depth)
170{
171 if (level < 0 || width < 0 || height < 0 || depth < 0)
172 {
173 return false;
174 }
175
176 if (!context->supportsNonPower2Texture() && (level != 0 || !gl::isPow2(width) || !gl::isPow2(height) || !gl::isPow2(depth)))
177 {
178 return false;
179 }
180
181 if (!ValidMipLevel(context, target, level))
182 {
183 return false;
184 }
185
186 return true;
187}
188
Geoff Lang005df412013-10-16 14:12:50 -0400189bool ValidCompressedImageSize(const gl::Context *context, GLenum internalFormat, GLsizei width, GLsizei height)
Geoff Langd4f180b2013-09-24 13:57:44 -0400190{
191 GLuint clientVersion = context->getClientVersion();
192 if (!IsFormatCompressed(internalFormat, clientVersion))
193 {
194 return false;
195 }
196
197 GLint blockWidth = GetCompressedBlockWidth(internalFormat, clientVersion);
198 GLint blockHeight = GetCompressedBlockHeight(internalFormat, clientVersion);
199 if (width < 0 || (width > blockWidth && width % blockWidth != 0) ||
200 height < 0 || (height > blockHeight && height % blockHeight != 0))
201 {
202 return false;
203 }
204
205 return true;
206}
207
Geoff Lang37dde692014-01-31 16:34:54 -0500208bool ValidQueryType(const Context *context, GLenum queryType)
209{
210 META_ASSERT(GL_ANY_SAMPLES_PASSED == GL_ANY_SAMPLES_PASSED_EXT);
211 META_ASSERT(GL_ANY_SAMPLES_PASSED_CONSERVATIVE == GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT);
212
213 switch (queryType)
214 {
215 case GL_ANY_SAMPLES_PASSED:
216 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
217 return true;
218 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
219 return (context->getClientVersion() >= 3);
220 default:
221 return false;
222 }
223}
224
Geoff Lang48dcae72014-02-05 16:28:24 -0500225bool ValidProgram(const Context *context, GLuint id)
226{
227 // ES3 spec (section 2.11.1) -- "Commands that accept shader or program object names will generate the
228 // error INVALID_VALUE if the provided name is not the name of either a shader or program object and
229 // INVALID_OPERATION if the provided name identifies an object that is not the expected type."
230
231 if (context->getProgram(id) != NULL)
232 {
233 return true;
234 }
235 else if (context->getShader(id) != NULL)
236 {
237 // ID is the wrong type
238 return gl::error(GL_INVALID_OPERATION, false);
239 }
240 else
241 {
242 // No shader/program object has this ID
243 return gl::error(GL_INVALID_VALUE, false);
244 }
245}
246
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400247bool ValidateRenderbufferStorageParameters(const gl::Context *context, GLenum target, GLsizei samples,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400248 GLenum internalformat, GLsizei width, GLsizei height,
249 bool angleExtension)
250{
251 switch (target)
252 {
253 case GL_RENDERBUFFER:
254 break;
255 default:
256 return gl::error(GL_INVALID_ENUM, false);
257 }
258
259 if (width < 0 || height < 0 || samples < 0)
260 {
261 return gl::error(GL_INVALID_VALUE, false);
262 }
263
264 if (!gl::IsValidInternalFormat(internalformat, context))
265 {
266 return gl::error(GL_INVALID_ENUM, false);
267 }
268
269 // ANGLE_framebuffer_multisample does not explicitly state that the internal format must be
270 // sized but it does state that the format must be in the ES2.0 spec table 4.5 which contains
271 // only sized internal formats. The ES3 spec (section 4.4.2) does, however, state that the
272 // internal format must be sized and not an integer format if samples is greater than zero.
273 if (!gl::IsSizedInternalFormat(internalformat, context->getClientVersion()))
274 {
275 return gl::error(GL_INVALID_ENUM, false);
276 }
277
Geoff Langb2f3d052013-08-13 12:49:27 -0400278 GLenum componentType = gl::GetComponentType(internalformat, context->getClientVersion());
279 if ((componentType == GL_UNSIGNED_INT || componentType == GL_INT) && samples > 0)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400280 {
281 return gl::error(GL_INVALID_OPERATION, false);
282 }
283
284 if (!gl::IsColorRenderingSupported(internalformat, context) &&
285 !gl::IsDepthRenderingSupported(internalformat, context) &&
286 !gl::IsStencilRenderingSupported(internalformat, context))
287 {
288 return gl::error(GL_INVALID_ENUM, false);
289 }
290
291 if (std::max(width, height) > context->getMaximumRenderbufferDimension())
292 {
293 return gl::error(GL_INVALID_VALUE, false);
294 }
295
296 // ANGLE_framebuffer_multisample states that the value of samples must be less than or equal
297 // to MAX_SAMPLES_ANGLE (Context::getMaxSupportedSamples) while the ES3.0 spec (section 4.4.2)
298 // states that samples must be less than or equal to the maximum samples for the specified
299 // internal format.
300 if (angleExtension)
301 {
302 if (samples > context->getMaxSupportedSamples())
303 {
304 return gl::error(GL_INVALID_VALUE, false);
305 }
306 }
307 else
308 {
309 if (samples > context->getMaxSupportedFormatSamples(internalformat))
310 {
311 return gl::error(GL_INVALID_VALUE, false);
312 }
313 }
314
315 GLuint handle = context->getRenderbufferHandle();
316 if (handle == 0)
317 {
318 return gl::error(GL_INVALID_OPERATION, false);
319 }
320
321 return true;
322}
323
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500324bool ValidateFramebufferRenderbufferParameters(gl::Context *context, GLenum target, GLenum attachment,
325 GLenum renderbuffertarget, GLuint renderbuffer)
326{
327 gl::Framebuffer *framebuffer = context->getTargetFramebuffer(target);
328 GLuint framebufferHandle = context->getTargetFramebufferHandle(target);
329
330 if (!framebuffer || (framebufferHandle == 0 && renderbuffer != 0))
331 {
332 return gl::error(GL_INVALID_OPERATION, false);
333 }
334
335 if (attachment >= GL_COLOR_ATTACHMENT0_EXT && attachment <= GL_COLOR_ATTACHMENT15_EXT)
336 {
337 const unsigned int colorAttachment = (attachment - GL_COLOR_ATTACHMENT0_EXT);
338
339 if (colorAttachment >= context->getMaximumRenderTargets())
340 {
341 return gl::error(GL_INVALID_VALUE, false);
342 }
343 }
344 else
345 {
346 switch (attachment)
347 {
348 case GL_DEPTH_ATTACHMENT:
349 break;
350 case GL_STENCIL_ATTACHMENT:
351 break;
352 case GL_DEPTH_STENCIL_ATTACHMENT:
353 if (context->getClientVersion() < 3)
354 {
355 return gl::error(GL_INVALID_ENUM, false);
356 }
357 break;
358 default:
359 return gl::error(GL_INVALID_ENUM, false);
360 }
361 }
362
Jamie Madillab9d82c2014-01-21 16:38:14 -0500363 // [OpenGL ES 2.0.25] Section 4.4.3 page 112
364 // [OpenGL ES 3.0.2] Section 4.4.2 page 201
365 // 'renderbuffer' must be either zero or the name of an existing renderbuffer object of
366 // type 'renderbuffertarget', otherwise an INVALID_OPERATION error is generated.
367 if (renderbuffer != 0)
368 {
369 if (!context->getRenderbuffer(renderbuffer))
370 {
371 return gl::error(GL_INVALID_OPERATION, false);
372 }
373 }
374
Jamie Madill1fc7e2c2014-01-21 16:47:10 -0500375 return true;
376}
377
Geoff Lang125deab2013-08-09 13:34:16 -0400378static bool IsPartialBlit(gl::Context *context, gl::Renderbuffer *readBuffer, gl::Renderbuffer *writeBuffer,
379 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
380 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1)
381{
382 if (srcX0 != 0 || srcY0 != 0 || dstX0 != 0 || dstY0 != 0 ||
383 dstX1 != writeBuffer->getWidth() || dstY1 != writeBuffer->getHeight() ||
384 srcX1 != readBuffer->getWidth() || srcY1 != readBuffer->getHeight())
385 {
386 return true;
387 }
388 else if (context->isScissorTestEnabled())
389 {
390 int scissorX, scissorY, scissorWidth, scissorHeight;
391 context->getScissorParams(&scissorX, &scissorY, &scissorWidth, &scissorHeight);
392
393 return scissorX > 0 || scissorY > 0 ||
394 scissorWidth < writeBuffer->getWidth() ||
395 scissorHeight < writeBuffer->getHeight();
396 }
397 else
398 {
399 return false;
400 }
401}
402
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400403bool ValidateBlitFramebufferParameters(gl::Context *context, GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400404 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask,
405 GLenum filter, bool fromAngleExtension)
406{
407 switch (filter)
408 {
409 case GL_NEAREST:
410 break;
411 case GL_LINEAR:
412 if (fromAngleExtension)
413 {
414 return gl::error(GL_INVALID_ENUM, false);
415 }
416 break;
417 default:
418 return gl::error(GL_INVALID_ENUM, false);
419 }
420
421 if ((mask & ~(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)) != 0)
422 {
423 return gl::error(GL_INVALID_VALUE, false);
424 }
425
426 if (mask == 0)
427 {
428 // ES3.0 spec, section 4.3.2 specifies that a mask of zero is valid and no
429 // buffers are copied.
430 return false;
431 }
432
433 if (fromAngleExtension && (srcX1 - srcX0 != dstX1 - dstX0 || srcY1 - srcY0 != dstY1 - dstY0))
434 {
435 ERR("Scaling and flipping in BlitFramebufferANGLE not supported by this implementation.");
436 return gl::error(GL_INVALID_OPERATION, false);
437 }
438
439 // ES3.0 spec, section 4.3.2 states that linear filtering is only available for the
440 // color buffer, leaving only nearest being unfiltered from above
441 if ((mask & ~GL_COLOR_BUFFER_BIT) != 0 && filter != GL_NEAREST)
442 {
443 return gl::error(GL_INVALID_OPERATION, false);
444 }
445
446 if (context->getReadFramebufferHandle() == context->getDrawFramebufferHandle())
447 {
448 if (fromAngleExtension)
449 {
450 ERR("Blits with the same source and destination framebuffer are not supported by this "
451 "implementation.");
452 }
453 return gl::error(GL_INVALID_OPERATION, false);
454 }
455
456 gl::Framebuffer *readFramebuffer = context->getReadFramebuffer();
457 gl::Framebuffer *drawFramebuffer = context->getDrawFramebuffer();
458 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
459 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
460 {
461 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
462 }
463
464 if (drawFramebuffer->getSamples() != 0)
465 {
466 return gl::error(GL_INVALID_OPERATION, false);
467 }
468
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400469 bool sameBounds = srcX0 == dstX0 && srcY0 == dstY0 && srcX1 == dstX1 && srcY1 == dstY1;
470
471 GLuint clientVersion = context->getClientVersion();
472
473 if (mask & GL_COLOR_BUFFER_BIT)
474 {
475 gl::Renderbuffer *readColorBuffer = readFramebuffer->getReadColorbuffer();
476 gl::Renderbuffer *drawColorBuffer = drawFramebuffer->getFirstColorbuffer();
477
478 if (readColorBuffer && drawColorBuffer)
479 {
Geoff Lang005df412013-10-16 14:12:50 -0400480 GLenum readInternalFormat = readColorBuffer->getActualFormat();
Geoff Langb2f3d052013-08-13 12:49:27 -0400481 GLenum readComponentType = gl::GetComponentType(readInternalFormat, clientVersion);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400482
483 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; i++)
484 {
485 if (drawFramebuffer->isEnabledColorAttachment(i))
486 {
Geoff Lang005df412013-10-16 14:12:50 -0400487 GLenum drawInternalFormat = drawFramebuffer->getColorbuffer(i)->getActualFormat();
Geoff Langb2f3d052013-08-13 12:49:27 -0400488 GLenum drawComponentType = gl::GetComponentType(drawInternalFormat, clientVersion);
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400489
Geoff Langb2f3d052013-08-13 12:49:27 -0400490 // The GL ES 3.0.2 spec (pg 193) states that:
491 // 1) If the read buffer is fixed point format, the draw buffer must be as well
492 // 2) If the read buffer is an unsigned integer format, the draw buffer must be as well
493 // 3) If the read buffer is a signed integer format, the draw buffer must be as well
494 if ( (readComponentType == GL_UNSIGNED_NORMALIZED || readComponentType == GL_SIGNED_NORMALIZED) &&
495 !(drawComponentType == GL_UNSIGNED_NORMALIZED || drawComponentType == GL_SIGNED_NORMALIZED))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400496 {
497 return gl::error(GL_INVALID_OPERATION, false);
498 }
499
Geoff Langb2f3d052013-08-13 12:49:27 -0400500 if (readComponentType == GL_UNSIGNED_INT && drawComponentType != GL_UNSIGNED_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400501 {
502 return gl::error(GL_INVALID_OPERATION, false);
503 }
504
Geoff Langb2f3d052013-08-13 12:49:27 -0400505 if (readComponentType == GL_INT && drawComponentType != GL_INT)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400506 {
507 return gl::error(GL_INVALID_OPERATION, false);
508 }
509
Geoff Langb2f3d052013-08-13 12:49:27 -0400510 if (readColorBuffer->getSamples() > 0 && (readInternalFormat != drawInternalFormat || !sameBounds))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400511 {
512 return gl::error(GL_INVALID_OPERATION, false);
513 }
514 }
515 }
516
Geoff Langb2f3d052013-08-13 12:49:27 -0400517 if ((readComponentType == GL_INT || readComponentType == GL_UNSIGNED_INT) && filter == GL_LINEAR)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400518 {
519 return gl::error(GL_INVALID_OPERATION, false);
520 }
521
522 if (fromAngleExtension)
523 {
524 const GLenum readColorbufferType = readFramebuffer->getReadColorbufferType();
525 if (readColorbufferType != GL_TEXTURE_2D && readColorbufferType != GL_RENDERBUFFER)
526 {
527 return gl::error(GL_INVALID_OPERATION, false);
528 }
529
530 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
531 {
532 if (drawFramebuffer->isEnabledColorAttachment(colorAttachment))
533 {
534 if (drawFramebuffer->getColorbufferType(colorAttachment) != GL_TEXTURE_2D &&
535 drawFramebuffer->getColorbufferType(colorAttachment) != GL_RENDERBUFFER)
536 {
537 return gl::error(GL_INVALID_OPERATION, false);
538 }
539
540 if (drawFramebuffer->getColorbuffer(colorAttachment)->getActualFormat() != readColorBuffer->getActualFormat())
541 {
542 return gl::error(GL_INVALID_OPERATION, false);
543 }
544 }
545 }
Geoff Lang125deab2013-08-09 13:34:16 -0400546 if (readFramebuffer->getSamples() != 0 && IsPartialBlit(context, readColorBuffer, drawColorBuffer,
547 srcX0, srcY0, srcX1, srcY1,
548 dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400549 {
550 return gl::error(GL_INVALID_OPERATION, false);
551 }
552 }
553 }
554 }
555
556 if (mask & GL_DEPTH_BUFFER_BIT)
557 {
558 gl::Renderbuffer *readDepthBuffer = readFramebuffer->getDepthbuffer();
559 gl::Renderbuffer *drawDepthBuffer = drawFramebuffer->getDepthbuffer();
560
561 if (readDepthBuffer && drawDepthBuffer)
562 {
563 if (readDepthBuffer->getActualFormat() != drawDepthBuffer->getActualFormat())
564 {
565 return gl::error(GL_INVALID_OPERATION, false);
566 }
567
568 if (readDepthBuffer->getSamples() > 0 && !sameBounds)
569 {
570 return gl::error(GL_INVALID_OPERATION, false);
571 }
572
573 if (fromAngleExtension)
574 {
Geoff Lang125deab2013-08-09 13:34:16 -0400575 if (IsPartialBlit(context, readDepthBuffer, drawDepthBuffer,
576 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400577 {
578 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
579 return gl::error(GL_INVALID_OPERATION, false); // only whole-buffer copies are permitted
580 }
581
582 if (readDepthBuffer->getSamples() != 0 || drawDepthBuffer->getSamples() != 0)
583 {
584 return gl::error(GL_INVALID_OPERATION, false);
585 }
586 }
587 }
588 }
589
590 if (mask & GL_STENCIL_BUFFER_BIT)
591 {
592 gl::Renderbuffer *readStencilBuffer = readFramebuffer->getStencilbuffer();
593 gl::Renderbuffer *drawStencilBuffer = drawFramebuffer->getStencilbuffer();
594
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400595 if (readStencilBuffer && drawStencilBuffer)
596 {
597 if (readStencilBuffer->getActualFormat() != drawStencilBuffer->getActualFormat())
598 {
599 return gl::error(GL_INVALID_OPERATION, false);
600 }
601
602 if (readStencilBuffer->getSamples() > 0 && !sameBounds)
603 {
604 return gl::error(GL_INVALID_OPERATION, false);
605 }
606
607 if (fromAngleExtension)
608 {
Geoff Lang125deab2013-08-09 13:34:16 -0400609 if (IsPartialBlit(context, readStencilBuffer, drawStencilBuffer,
610 srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400611 {
612 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
613 return gl::error(GL_INVALID_OPERATION, false); // only whole-buffer copies are permitted
614 }
615
616 if (readStencilBuffer->getSamples() != 0 || drawStencilBuffer->getSamples() != 0)
617 {
618 return gl::error(GL_INVALID_OPERATION, false);
619 }
620 }
621 }
622 }
623
624 return true;
625}
626
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400627bool ValidateGetVertexAttribParameters(GLenum pname, int clientVersion)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400628{
629 switch (pname)
630 {
631 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
632 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
633 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
634 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
635 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
636 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
637 case GL_CURRENT_VERTEX_ATTRIB:
638 return true;
639
640 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR:
641 // Don't verify ES3 context because GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE uses
642 // the same constant.
643 META_ASSERT(GL_VERTEX_ATTRIB_ARRAY_DIVISOR == GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE);
644 return true;
645
646 case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
647 return ((clientVersion >= 3) ? true : gl::error(GL_INVALID_ENUM, false));
648
649 default:
650 return gl::error(GL_INVALID_ENUM, false);
651 }
652}
653
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400654bool ValidateTexParamParameters(gl::Context *context, GLenum pname, GLint param)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400655{
656 switch (pname)
657 {
658 case GL_TEXTURE_WRAP_R:
659 case GL_TEXTURE_SWIZZLE_R:
660 case GL_TEXTURE_SWIZZLE_G:
661 case GL_TEXTURE_SWIZZLE_B:
662 case GL_TEXTURE_SWIZZLE_A:
663 case GL_TEXTURE_BASE_LEVEL:
664 case GL_TEXTURE_MAX_LEVEL:
665 case GL_TEXTURE_COMPARE_MODE:
666 case GL_TEXTURE_COMPARE_FUNC:
667 case GL_TEXTURE_MIN_LOD:
668 case GL_TEXTURE_MAX_LOD:
669 if (context->getClientVersion() < 3)
670 {
671 return gl::error(GL_INVALID_ENUM, false);
672 }
673 break;
674
675 default: break;
676 }
677
678 switch (pname)
679 {
680 case GL_TEXTURE_WRAP_S:
681 case GL_TEXTURE_WRAP_T:
682 case GL_TEXTURE_WRAP_R:
683 switch (param)
684 {
685 case GL_REPEAT:
686 case GL_CLAMP_TO_EDGE:
687 case GL_MIRRORED_REPEAT:
688 return true;
689 default:
690 return gl::error(GL_INVALID_ENUM, false);
691 }
692
693 case GL_TEXTURE_MIN_FILTER:
694 switch (param)
695 {
696 case GL_NEAREST:
697 case GL_LINEAR:
698 case GL_NEAREST_MIPMAP_NEAREST:
699 case GL_LINEAR_MIPMAP_NEAREST:
700 case GL_NEAREST_MIPMAP_LINEAR:
701 case GL_LINEAR_MIPMAP_LINEAR:
702 return true;
703 default:
704 return gl::error(GL_INVALID_ENUM, false);
705 }
706 break;
707
708 case GL_TEXTURE_MAG_FILTER:
709 switch (param)
710 {
711 case GL_NEAREST:
712 case GL_LINEAR:
713 return true;
714 default:
715 return gl::error(GL_INVALID_ENUM, false);
716 }
717 break;
718
719 case GL_TEXTURE_USAGE_ANGLE:
720 switch (param)
721 {
722 case GL_NONE:
723 case GL_FRAMEBUFFER_ATTACHMENT_ANGLE:
724 return true;
725 default:
726 return gl::error(GL_INVALID_ENUM, false);
727 }
728 break;
729
730 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
731 if (!context->supportsTextureFilterAnisotropy())
732 {
733 return gl::error(GL_INVALID_ENUM, false);
734 }
735
736 // we assume the parameter passed to this validation method is truncated, not rounded
737 if (param < 1)
738 {
739 return gl::error(GL_INVALID_VALUE, false);
740 }
741 return true;
742
743 case GL_TEXTURE_MIN_LOD:
744 case GL_TEXTURE_MAX_LOD:
745 // any value is permissible
746 return true;
747
748 case GL_TEXTURE_COMPARE_MODE:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400749 // Acceptable mode parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400750 switch (param)
751 {
752 case GL_NONE:
753 case GL_COMPARE_REF_TO_TEXTURE:
754 return true;
755 default:
756 return gl::error(GL_INVALID_ENUM, false);
757 }
758 break;
759
760 case GL_TEXTURE_COMPARE_FUNC:
Geoff Lang63b5f1f2013-09-23 14:52:14 -0400761 // Acceptable function parameters from GLES 3.0.2 spec, table 3.17
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400762 switch (param)
763 {
764 case GL_LEQUAL:
765 case GL_GEQUAL:
766 case GL_LESS:
767 case GL_GREATER:
768 case GL_EQUAL:
769 case GL_NOTEQUAL:
770 case GL_ALWAYS:
771 case GL_NEVER:
772 return true;
773 default:
774 return gl::error(GL_INVALID_ENUM, false);
775 }
776 break;
777
778 case GL_TEXTURE_SWIZZLE_R:
779 case GL_TEXTURE_SWIZZLE_G:
780 case GL_TEXTURE_SWIZZLE_B:
781 case GL_TEXTURE_SWIZZLE_A:
Geoff Langbc90a482013-09-17 16:51:27 -0400782 switch (param)
783 {
784 case GL_RED:
785 case GL_GREEN:
786 case GL_BLUE:
787 case GL_ALPHA:
788 case GL_ZERO:
789 case GL_ONE:
790 return true;
791 default:
792 return gl::error(GL_INVALID_ENUM, false);
793 }
794 break;
795
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400796 case GL_TEXTURE_BASE_LEVEL:
797 case GL_TEXTURE_MAX_LEVEL:
Nicolas Capens8de68282014-04-04 11:10:27 -0400798 if (param < 0)
799 {
800 return gl::error(GL_INVALID_VALUE, false);
801 }
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400802 return true;
803
804 default:
805 return gl::error(GL_INVALID_ENUM, false);
806 }
807}
808
Geoff Lang34dbb6f2013-08-05 15:05:47 -0400809bool ValidateSamplerObjectParameter(GLenum pname)
Geoff Lange8ebe7f2013-08-05 15:03:13 -0400810{
811 switch (pname)
812 {
813 case GL_TEXTURE_MIN_FILTER:
814 case GL_TEXTURE_MAG_FILTER:
815 case GL_TEXTURE_WRAP_S:
816 case GL_TEXTURE_WRAP_T:
817 case GL_TEXTURE_WRAP_R:
818 case GL_TEXTURE_MIN_LOD:
819 case GL_TEXTURE_MAX_LOD:
820 case GL_TEXTURE_COMPARE_MODE:
821 case GL_TEXTURE_COMPARE_FUNC:
822 return true;
823
824 default:
825 return gl::error(GL_INVALID_ENUM, false);
826 }
827}
828
Jamie Madill26e91952014-03-05 15:01:27 -0500829bool ValidateReadPixelsParameters(gl::Context *context, GLint x, GLint y, GLsizei width, GLsizei height,
830 GLenum format, GLenum type, GLsizei *bufSize, GLvoid *pixels)
831{
832 gl::Framebuffer *framebuffer = context->getReadFramebuffer();
Jamie Madill893ab082014-05-16 16:56:10 -0400833 ASSERT(framebuffer);
Jamie Madill26e91952014-03-05 15:01:27 -0500834
835 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
836 {
837 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
838 }
839
840 if (context->getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
841 {
842 return gl::error(GL_INVALID_OPERATION, false);
843 }
844
Jamie Madill893ab082014-05-16 16:56:10 -0400845 if (!framebuffer->getReadColorbuffer())
846 {
847 return gl::error(GL_INVALID_OPERATION, false);
848 }
849
Jamie Madill26e91952014-03-05 15:01:27 -0500850 GLenum currentInternalFormat, currentFormat, currentType;
851 int clientVersion = context->getClientVersion();
852
Jamie Madill893ab082014-05-16 16:56:10 -0400853 context->getCurrentReadFormatType(&currentInternalFormat, &currentFormat, &currentType);
Jamie Madill26e91952014-03-05 15:01:27 -0500854
Geoff Langbdc9b2f2014-04-16 14:41:54 -0400855 bool validReadFormat = (clientVersion < 3) ? ValidES2ReadFormatType(context, format, type) :
856 ValidES3ReadFormatType(context, currentInternalFormat, format, type);
Jamie Madill26e91952014-03-05 15:01:27 -0500857
858 if (!(currentFormat == format && currentType == type) && !validReadFormat)
859 {
860 return gl::error(GL_INVALID_OPERATION, false);
861 }
862
863 GLenum sizedInternalFormat = IsSizedInternalFormat(format, clientVersion) ? format :
864 GetSizedInternalFormat(format, type, clientVersion);
865
866 GLsizei outputPitch = GetRowPitch(sizedInternalFormat, type, clientVersion, width, context->getPackAlignment());
867 // sized query sanity check
868 if (bufSize)
869 {
870 int requiredSize = outputPitch * height;
871 if (requiredSize > *bufSize)
872 {
873 return gl::error(GL_INVALID_OPERATION, false);
874 }
875 }
876
877 return true;
878}
879
Jamie Madilldb2f14c2014-05-13 13:56:30 -0400880bool ValidateBeginQuery(gl::Context *context, GLenum target, GLuint id)
881{
882 if (!ValidQueryType(context, target))
883 {
884 return gl::error(GL_INVALID_ENUM, false);
885 }
886
887 if (id == 0)
888 {
889 return gl::error(GL_INVALID_OPERATION, false);
890 }
891
892 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
893 // of zero, if the active query object name for <target> is non-zero (for the
894 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
895 // the active query for either target is non-zero), if <id> is the name of an
896 // existing query object whose type does not match <target>, or if <id> is the
897 // active query object name for any query type, the error INVALID_OPERATION is
898 // generated.
899
900 // Ensure no other queries are active
901 // NOTE: If other queries than occlusion are supported, we will need to check
902 // separately that:
903 // a) The query ID passed is not the current active query for any target/type
904 // b) There are no active queries for the requested target (and in the case
905 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
906 // no query may be active for either if glBeginQuery targets either.
907 if (context->isQueryActive())
908 {
909 return gl::error(GL_INVALID_OPERATION, false);
910 }
911
912 Query *queryObject = context->getQuery(id, true, target);
913
914 // check that name was obtained with glGenQueries
915 if (!queryObject)
916 {
917 return gl::error(GL_INVALID_OPERATION, false);
918 }
919
920 // check for type mismatch
921 if (queryObject->getType() != target)
922 {
923 return gl::error(GL_INVALID_OPERATION, false);
924 }
925
926 return true;
927}
928
Jamie Madill45c785d2014-05-13 14:09:34 -0400929bool ValidateEndQuery(gl::Context *context, GLenum target)
930{
931 if (!ValidQueryType(context, target))
932 {
933 return gl::error(GL_INVALID_ENUM, false);
934 }
935
936 const Query *queryObject = context->getActiveQuery(target);
937
938 if (queryObject == NULL)
939 {
940 return gl::error(GL_INVALID_OPERATION, false);
941 }
942
943 if (!queryObject->isStarted())
944 {
945 return gl::error(GL_INVALID_OPERATION, false);
946 }
947
948 return true;
949}
950
Jamie Madill36398922014-05-20 14:51:53 -0400951static bool ValidateUniformCommonBase(gl::Context *context, GLenum targetUniformType,
952 GLint location, GLsizei count, LinkedUniform **uniformOut)
Jamie Madilld7c7bb22014-05-20 10:55:54 -0400953{
954 if (count < 0)
955 {
956 return gl::error(GL_INVALID_VALUE, false);
957 }
958
Jamie Madilld7c7bb22014-05-20 10:55:54 -0400959 gl::ProgramBinary *programBinary = context->getCurrentProgramBinary();
960 if (!programBinary)
961 {
962 return gl::error(GL_INVALID_OPERATION, false);
963 }
964
965 if (location == -1)
966 {
967 // Silently ignore the uniform command
968 return false;
969 }
970
Jamie Madill36398922014-05-20 14:51:53 -0400971 if (!programBinary->isValidUniformLocation(location))
972 {
973 return gl::error(GL_INVALID_OPERATION, false);
974 }
975
976 LinkedUniform *uniform = programBinary->getUniformByLocation(location);
977
978 // attempting to write an array to a non-array uniform is an INVALID_OPERATION
979 if (uniform->elementCount() == 1 && count > 1)
980 {
981 return gl::error(GL_INVALID_OPERATION, false);
982 }
983
984 *uniformOut = uniform;
Jamie Madilld7c7bb22014-05-20 10:55:54 -0400985 return true;
986}
987
Jamie Madillaa981bd2014-05-20 10:55:55 -0400988bool ValidateUniform(gl::Context *context, GLenum uniformType, GLint location, GLsizei count)
989{
990 // Check for ES3 uniform entry points
991 if (UniformComponentType(uniformType) == GL_UNSIGNED_INT && context->getClientVersion() < 3)
992 {
993 return gl::error(GL_INVALID_OPERATION, false);
994 }
995
Jamie Madill36398922014-05-20 14:51:53 -0400996 LinkedUniform *uniform = NULL;
997 if (!ValidateUniformCommonBase(context, uniformType, location, count, &uniform))
998 {
999 return false;
1000 }
1001
1002 GLenum targetBoolType = UniformBoolVectorType(uniformType);
1003 bool samplerUniformCheck = (IsSampler(uniform->type) && uniformType == GL_INT);
1004 if (!samplerUniformCheck && uniformType != uniform->type && targetBoolType != uniform->type)
1005 {
1006 return gl::error(GL_INVALID_OPERATION, false);
1007 }
1008
1009 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001010}
1011
1012bool ValidateUniformMatrix(gl::Context *context, GLenum matrixType, GLint location, GLsizei count,
1013 GLboolean transpose)
1014{
1015 // Check for ES3 uniform entry points
1016 int rows = VariableRowCount(matrixType);
1017 int cols = VariableColumnCount(matrixType);
1018 if (rows != cols && context->getClientVersion() < 3)
1019 {
1020 return gl::error(GL_INVALID_OPERATION, false);
1021 }
1022
1023 if (transpose != GL_FALSE && context->getClientVersion() < 3)
1024 {
1025 return gl::error(GL_INVALID_VALUE, false);
1026 }
1027
Jamie Madill36398922014-05-20 14:51:53 -04001028 LinkedUniform *uniform = NULL;
1029 if (!ValidateUniformCommonBase(context, matrixType, location, count, &uniform))
1030 {
1031 return false;
1032 }
1033
1034 if (uniform->type != matrixType)
1035 {
1036 return gl::error(GL_INVALID_OPERATION, false);
1037 }
1038
1039 return true;
Jamie Madillaa981bd2014-05-20 10:55:55 -04001040}
1041
Jamie Madill893ab082014-05-16 16:56:10 -04001042bool ValidateStateQuery(gl::Context *context, GLenum pname, GLenum *nativeType, unsigned int *numParams)
1043{
1044 if (!context->getQueryParameterInfo(pname, nativeType, numParams))
1045 {
1046 return gl::error(GL_INVALID_ENUM, false);
1047 }
1048
1049 if (pname >= GL_DRAW_BUFFER0 && pname <= GL_DRAW_BUFFER15)
1050 {
1051 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0);
1052
1053 if (colorAttachment >= context->getMaximumRenderTargets())
1054 {
1055 return gl::error(GL_INVALID_OPERATION, false);
1056 }
1057 }
1058
1059 switch (pname)
1060 {
1061 case GL_TEXTURE_BINDING_2D:
1062 case GL_TEXTURE_BINDING_CUBE_MAP:
1063 case GL_TEXTURE_BINDING_3D:
1064 case GL_TEXTURE_BINDING_2D_ARRAY:
1065 if (context->getActiveSampler() >= context->getMaximumCombinedTextureImageUnits())
1066 {
1067 return gl::error(GL_INVALID_OPERATION, false);
1068 }
1069 break;
1070
1071 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1072 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1073 {
1074 Framebuffer *framebuffer = context->getReadFramebuffer();
1075 ASSERT(framebuffer);
1076 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1077 {
1078 return gl::error(GL_INVALID_OPERATION, false);
1079 }
1080
1081 Renderbuffer *renderbuffer = framebuffer->getReadColorbuffer();
1082 if (!renderbuffer)
1083 {
1084 return gl::error(GL_INVALID_OPERATION, false);
1085 }
1086 }
1087 break;
1088
1089 default:
1090 break;
1091 }
1092
1093 // pname is valid, but there are no parameters to return
1094 if (numParams == 0)
1095 {
1096 return false;
1097 }
1098
1099 return true;
1100}
1101
Jamie Madill560a8d82014-05-21 13:06:20 -04001102bool ValidateCopyTexImageParametersBase(gl::Context* context, GLenum target, GLint level, GLenum internalformat, bool isSubImage,
1103 GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height,
1104 GLint border, GLenum *textureFormatOut)
1105{
1106
1107 if (!ValidTexture2DDestinationTarget(context, target))
1108 {
1109 return gl::error(GL_INVALID_ENUM, false);
1110 }
1111
1112 if (level < 0 || xoffset < 0 || yoffset < 0 || zoffset < 0 || width < 0 || height < 0)
1113 {
1114 return gl::error(GL_INVALID_VALUE, false);
1115 }
1116
1117 if (std::numeric_limits<GLsizei>::max() - xoffset < width || std::numeric_limits<GLsizei>::max() - yoffset < height)
1118 {
1119 return gl::error(GL_INVALID_VALUE, false);
1120 }
1121
1122 if (border != 0)
1123 {
1124 return gl::error(GL_INVALID_VALUE, false);
1125 }
1126
1127 if (!ValidMipLevel(context, target, level))
1128 {
1129 return gl::error(GL_INVALID_VALUE, false);
1130 }
1131
1132 gl::Framebuffer *framebuffer = context->getReadFramebuffer();
1133 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1134 {
1135 return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
1136 }
1137
1138 if (context->getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
1139 {
1140 return gl::error(GL_INVALID_OPERATION, false);
1141 }
1142
1143 gl::Renderbuffer *source = framebuffer->getReadColorbuffer();
1144 GLenum colorbufferInternalFormat = source->getInternalFormat();
1145 gl::Texture *texture = NULL;
1146 GLenum textureInternalFormat = GL_NONE;
1147 bool textureCompressed = false;
1148 bool textureIsDepth = false;
1149 GLint textureLevelWidth = 0;
1150 GLint textureLevelHeight = 0;
1151 GLint textureLevelDepth = 0;
1152
1153 switch (target)
1154 {
1155 case GL_TEXTURE_2D:
1156 {
1157 gl::Texture2D *texture2d = context->getTexture2D();
1158 if (texture2d)
1159 {
1160 textureInternalFormat = texture2d->getInternalFormat(level);
1161 textureCompressed = texture2d->isCompressed(level);
1162 textureIsDepth = texture2d->isDepth(level);
1163 textureLevelWidth = texture2d->getWidth(level);
1164 textureLevelHeight = texture2d->getHeight(level);
1165 textureLevelDepth = 1;
1166 texture = texture2d;
1167 }
1168 }
1169 break;
1170
1171 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1172 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1173 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1174 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1175 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1176 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1177 {
1178 gl::TextureCubeMap *textureCube = context->getTextureCubeMap();
1179 if (textureCube)
1180 {
1181 textureInternalFormat = textureCube->getInternalFormat(target, level);
1182 textureCompressed = textureCube->isCompressed(target, level);
1183 textureIsDepth = false;
1184 textureLevelWidth = textureCube->getWidth(target, level);
1185 textureLevelHeight = textureCube->getHeight(target, level);
1186 textureLevelDepth = 1;
1187 texture = textureCube;
1188 }
1189 }
1190 break;
1191
1192 case GL_TEXTURE_2D_ARRAY:
1193 {
1194 gl::Texture2DArray *texture2dArray = context->getTexture2DArray();
1195 if (texture2dArray)
1196 {
1197 textureInternalFormat = texture2dArray->getInternalFormat(level);
1198 textureCompressed = texture2dArray->isCompressed(level);
1199 textureIsDepth = texture2dArray->isDepth(level);
1200 textureLevelWidth = texture2dArray->getWidth(level);
1201 textureLevelHeight = texture2dArray->getHeight(level);
1202 textureLevelDepth = texture2dArray->getLayers(level);
1203 texture = texture2dArray;
1204 }
1205 }
1206 break;
1207
1208 case GL_TEXTURE_3D:
1209 {
1210 gl::Texture3D *texture3d = context->getTexture3D();
1211 if (texture3d)
1212 {
1213 textureInternalFormat = texture3d->getInternalFormat(level);
1214 textureCompressed = texture3d->isCompressed(level);
1215 textureIsDepth = texture3d->isDepth(level);
1216 textureLevelWidth = texture3d->getWidth(level);
1217 textureLevelHeight = texture3d->getHeight(level);
1218 textureLevelDepth = texture3d->getDepth(level);
1219 texture = texture3d;
1220 }
1221 }
1222 break;
1223
1224 default:
1225 return gl::error(GL_INVALID_ENUM, false);
1226 }
1227
1228 if (!texture)
1229 {
1230 return gl::error(GL_INVALID_OPERATION, false);
1231 }
1232
1233 if (texture->isImmutable() && !isSubImage)
1234 {
1235 return gl::error(GL_INVALID_OPERATION, false);
1236 }
1237
1238 if (textureIsDepth)
1239 {
1240 return gl::error(GL_INVALID_OPERATION, false);
1241 }
1242
1243 if (textureCompressed)
1244 {
1245 int clientVersion = context->getClientVersion();
1246 GLint blockWidth = GetCompressedBlockWidth(textureInternalFormat, clientVersion);
1247 GLint blockHeight = GetCompressedBlockHeight(textureInternalFormat, clientVersion);
1248
1249 if (((width % blockWidth) != 0 && width != textureLevelWidth) ||
1250 ((height % blockHeight) != 0 && height != textureLevelHeight))
1251 {
1252 return gl::error(GL_INVALID_OPERATION, false);
1253 }
1254 }
1255
1256 if (isSubImage)
1257 {
1258 if (xoffset + width > textureLevelWidth ||
1259 yoffset + height > textureLevelHeight ||
1260 zoffset >= textureLevelDepth)
1261 {
1262 return gl::error(GL_INVALID_VALUE, false);
1263 }
1264 }
1265
1266 *textureFormatOut = textureInternalFormat;
1267 return true;
1268}
1269
Geoff Lange8ebe7f2013-08-05 15:03:13 -04001270}