Geoff Lang | e8ebe7f | 2013-08-05 15:03:13 -0400 | [diff] [blame^] | 1 | #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" |
| 11 | #include "libGLESv2/Context.h" |
| 12 | #include "libGLESv2/Texture.h" |
| 13 | #include "libGLESv2/Framebuffer.h" |
| 14 | #include "libGLESv2/Renderbuffer.h" |
| 15 | #include "libGLESv2/formatutils.h" |
| 16 | #include "libGLESv2/main.h" |
| 17 | |
| 18 | #include "common/mathutil.h" |
| 19 | #include "common/utilities.h" |
| 20 | |
| 21 | namespace gl |
| 22 | { |
| 23 | |
| 24 | bool validateRenderbufferStorageParameters(const gl::Context *context, GLenum target, GLsizei samples, |
| 25 | GLenum internalformat, GLsizei width, GLsizei height, |
| 26 | bool angleExtension) |
| 27 | { |
| 28 | switch (target) |
| 29 | { |
| 30 | case GL_RENDERBUFFER: |
| 31 | break; |
| 32 | default: |
| 33 | return gl::error(GL_INVALID_ENUM, false); |
| 34 | } |
| 35 | |
| 36 | if (width < 0 || height < 0 || samples < 0) |
| 37 | { |
| 38 | return gl::error(GL_INVALID_VALUE, false); |
| 39 | } |
| 40 | |
| 41 | if (!gl::IsValidInternalFormat(internalformat, context)) |
| 42 | { |
| 43 | return gl::error(GL_INVALID_ENUM, false); |
| 44 | } |
| 45 | |
| 46 | // ANGLE_framebuffer_multisample does not explicitly state that the internal format must be |
| 47 | // sized but it does state that the format must be in the ES2.0 spec table 4.5 which contains |
| 48 | // only sized internal formats. The ES3 spec (section 4.4.2) does, however, state that the |
| 49 | // internal format must be sized and not an integer format if samples is greater than zero. |
| 50 | if (!gl::IsSizedInternalFormat(internalformat, context->getClientVersion())) |
| 51 | { |
| 52 | return gl::error(GL_INVALID_ENUM, false); |
| 53 | } |
| 54 | |
| 55 | if (gl::IsIntegerFormat(internalformat, context->getClientVersion()) && samples > 0) |
| 56 | { |
| 57 | return gl::error(GL_INVALID_OPERATION, false); |
| 58 | } |
| 59 | |
| 60 | if (!gl::IsColorRenderingSupported(internalformat, context) && |
| 61 | !gl::IsDepthRenderingSupported(internalformat, context) && |
| 62 | !gl::IsStencilRenderingSupported(internalformat, context)) |
| 63 | { |
| 64 | return gl::error(GL_INVALID_ENUM, false); |
| 65 | } |
| 66 | |
| 67 | if (std::max(width, height) > context->getMaximumRenderbufferDimension()) |
| 68 | { |
| 69 | return gl::error(GL_INVALID_VALUE, false); |
| 70 | } |
| 71 | |
| 72 | // ANGLE_framebuffer_multisample states that the value of samples must be less than or equal |
| 73 | // to MAX_SAMPLES_ANGLE (Context::getMaxSupportedSamples) while the ES3.0 spec (section 4.4.2) |
| 74 | // states that samples must be less than or equal to the maximum samples for the specified |
| 75 | // internal format. |
| 76 | if (angleExtension) |
| 77 | { |
| 78 | if (samples > context->getMaxSupportedSamples()) |
| 79 | { |
| 80 | return gl::error(GL_INVALID_VALUE, false); |
| 81 | } |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | if (samples > context->getMaxSupportedFormatSamples(internalformat)) |
| 86 | { |
| 87 | return gl::error(GL_INVALID_VALUE, false); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | GLuint handle = context->getRenderbufferHandle(); |
| 92 | if (handle == 0) |
| 93 | { |
| 94 | return gl::error(GL_INVALID_OPERATION, false); |
| 95 | } |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | bool validateBlitFramebufferParameters(gl::Context *context, GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, |
| 101 | GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask, |
| 102 | GLenum filter, bool fromAngleExtension) |
| 103 | { |
| 104 | switch (filter) |
| 105 | { |
| 106 | case GL_NEAREST: |
| 107 | break; |
| 108 | case GL_LINEAR: |
| 109 | if (fromAngleExtension) |
| 110 | { |
| 111 | return gl::error(GL_INVALID_ENUM, false); |
| 112 | } |
| 113 | break; |
| 114 | default: |
| 115 | return gl::error(GL_INVALID_ENUM, false); |
| 116 | } |
| 117 | |
| 118 | if ((mask & ~(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)) != 0) |
| 119 | { |
| 120 | return gl::error(GL_INVALID_VALUE, false); |
| 121 | } |
| 122 | |
| 123 | if (mask == 0) |
| 124 | { |
| 125 | // ES3.0 spec, section 4.3.2 specifies that a mask of zero is valid and no |
| 126 | // buffers are copied. |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | if (fromAngleExtension && (srcX1 - srcX0 != dstX1 - dstX0 || srcY1 - srcY0 != dstY1 - dstY0)) |
| 131 | { |
| 132 | ERR("Scaling and flipping in BlitFramebufferANGLE not supported by this implementation."); |
| 133 | return gl::error(GL_INVALID_OPERATION, false); |
| 134 | } |
| 135 | |
| 136 | // ES3.0 spec, section 4.3.2 states that linear filtering is only available for the |
| 137 | // color buffer, leaving only nearest being unfiltered from above |
| 138 | if ((mask & ~GL_COLOR_BUFFER_BIT) != 0 && filter != GL_NEAREST) |
| 139 | { |
| 140 | return gl::error(GL_INVALID_OPERATION, false); |
| 141 | } |
| 142 | |
| 143 | if (context->getReadFramebufferHandle() == context->getDrawFramebufferHandle()) |
| 144 | { |
| 145 | if (fromAngleExtension) |
| 146 | { |
| 147 | ERR("Blits with the same source and destination framebuffer are not supported by this " |
| 148 | "implementation."); |
| 149 | } |
| 150 | return gl::error(GL_INVALID_OPERATION, false); |
| 151 | } |
| 152 | |
| 153 | gl::Framebuffer *readFramebuffer = context->getReadFramebuffer(); |
| 154 | gl::Framebuffer *drawFramebuffer = context->getDrawFramebuffer(); |
| 155 | if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE || |
| 156 | !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE) |
| 157 | { |
| 158 | return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false); |
| 159 | } |
| 160 | |
| 161 | if (drawFramebuffer->getSamples() != 0) |
| 162 | { |
| 163 | return gl::error(GL_INVALID_OPERATION, false); |
| 164 | } |
| 165 | |
| 166 | gl::Rectangle sourceClippedRect, destClippedRect; |
| 167 | bool partialCopy; |
| 168 | if (!context->clipBlitFramebufferCoordinates(srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, |
| 169 | &sourceClippedRect, &destClippedRect, &partialCopy)) |
| 170 | { |
| 171 | return gl::error(GL_INVALID_OPERATION, false); |
| 172 | } |
| 173 | |
| 174 | bool sameBounds = srcX0 == dstX0 && srcY0 == dstY0 && srcX1 == dstX1 && srcY1 == dstY1; |
| 175 | |
| 176 | GLuint clientVersion = context->getClientVersion(); |
| 177 | |
| 178 | if (mask & GL_COLOR_BUFFER_BIT) |
| 179 | { |
| 180 | gl::Renderbuffer *readColorBuffer = readFramebuffer->getReadColorbuffer(); |
| 181 | gl::Renderbuffer *drawColorBuffer = drawFramebuffer->getFirstColorbuffer(); |
| 182 | |
| 183 | if (readColorBuffer && drawColorBuffer) |
| 184 | { |
| 185 | GLint readInternalFormat = readColorBuffer->getActualFormat(); |
| 186 | GLint drawInternalFormat = drawColorBuffer->getActualFormat(); |
| 187 | |
| 188 | for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; i++) |
| 189 | { |
| 190 | if (drawFramebuffer->isEnabledColorAttachment(i)) |
| 191 | { |
| 192 | GLint drawbufferAttachmentFormat = drawFramebuffer->getColorbuffer(i)->getActualFormat(); |
| 193 | |
| 194 | if (gl::IsNormalizedFixedPointFormat(readInternalFormat, clientVersion) && |
| 195 | !gl::IsNormalizedFixedPointFormat(drawbufferAttachmentFormat, clientVersion)) |
| 196 | { |
| 197 | return gl::error(GL_INVALID_OPERATION, false); |
| 198 | } |
| 199 | |
| 200 | if (gl::IsUnsignedIntegerFormat(readInternalFormat, clientVersion) && |
| 201 | !gl::IsUnsignedIntegerFormat(drawbufferAttachmentFormat, clientVersion)) |
| 202 | { |
| 203 | return gl::error(GL_INVALID_OPERATION, false); |
| 204 | } |
| 205 | |
| 206 | if (gl::IsSignedIntegerFormat(readInternalFormat, clientVersion) && |
| 207 | !gl::IsSignedIntegerFormat(drawbufferAttachmentFormat, clientVersion)) |
| 208 | { |
| 209 | return gl::error(GL_INVALID_OPERATION, false); |
| 210 | } |
| 211 | |
| 212 | if (readColorBuffer->getSamples() > 0 && (readInternalFormat != drawbufferAttachmentFormat || !sameBounds)) |
| 213 | { |
| 214 | return gl::error(GL_INVALID_OPERATION, false); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | if (gl::IsIntegerFormat(readInternalFormat, clientVersion) && filter == GL_LINEAR) |
| 220 | { |
| 221 | return gl::error(GL_INVALID_OPERATION, false); |
| 222 | } |
| 223 | |
| 224 | if (fromAngleExtension) |
| 225 | { |
| 226 | const GLenum readColorbufferType = readFramebuffer->getReadColorbufferType(); |
| 227 | if (readColorbufferType != GL_TEXTURE_2D && readColorbufferType != GL_RENDERBUFFER) |
| 228 | { |
| 229 | return gl::error(GL_INVALID_OPERATION, false); |
| 230 | } |
| 231 | |
| 232 | for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++) |
| 233 | { |
| 234 | if (drawFramebuffer->isEnabledColorAttachment(colorAttachment)) |
| 235 | { |
| 236 | if (drawFramebuffer->getColorbufferType(colorAttachment) != GL_TEXTURE_2D && |
| 237 | drawFramebuffer->getColorbufferType(colorAttachment) != GL_RENDERBUFFER) |
| 238 | { |
| 239 | return gl::error(GL_INVALID_OPERATION, false); |
| 240 | } |
| 241 | |
| 242 | if (drawFramebuffer->getColorbuffer(colorAttachment)->getActualFormat() != readColorBuffer->getActualFormat()) |
| 243 | { |
| 244 | return gl::error(GL_INVALID_OPERATION, false); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | if (partialCopy && readFramebuffer->getSamples() != 0) |
| 250 | { |
| 251 | return gl::error(GL_INVALID_OPERATION, false); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (mask & GL_DEPTH_BUFFER_BIT) |
| 258 | { |
| 259 | gl::Renderbuffer *readDepthBuffer = readFramebuffer->getDepthbuffer(); |
| 260 | gl::Renderbuffer *drawDepthBuffer = drawFramebuffer->getDepthbuffer(); |
| 261 | |
| 262 | if (readDepthBuffer && drawDepthBuffer) |
| 263 | { |
| 264 | if (readDepthBuffer->getActualFormat() != drawDepthBuffer->getActualFormat()) |
| 265 | { |
| 266 | return gl::error(GL_INVALID_OPERATION, false); |
| 267 | } |
| 268 | |
| 269 | if (readDepthBuffer->getSamples() > 0 && !sameBounds) |
| 270 | { |
| 271 | return gl::error(GL_INVALID_OPERATION, false); |
| 272 | } |
| 273 | |
| 274 | if (fromAngleExtension) |
| 275 | { |
| 276 | if (partialCopy) |
| 277 | { |
| 278 | ERR("Only whole-buffer depth and stencil blits are supported by this implementation."); |
| 279 | return gl::error(GL_INVALID_OPERATION, false); // only whole-buffer copies are permitted |
| 280 | } |
| 281 | |
| 282 | if (readDepthBuffer->getSamples() != 0 || drawDepthBuffer->getSamples() != 0) |
| 283 | { |
| 284 | return gl::error(GL_INVALID_OPERATION, false); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | if (mask & GL_STENCIL_BUFFER_BIT) |
| 291 | { |
| 292 | gl::Renderbuffer *readStencilBuffer = readFramebuffer->getStencilbuffer(); |
| 293 | gl::Renderbuffer *drawStencilBuffer = drawFramebuffer->getStencilbuffer(); |
| 294 | |
| 295 | if (fromAngleExtension && partialCopy) |
| 296 | { |
| 297 | ERR("Only whole-buffer depth and stencil blits are supported by this implementation."); |
| 298 | return gl::error(GL_INVALID_OPERATION, false); // only whole-buffer copies are permitted |
| 299 | } |
| 300 | |
| 301 | if (readStencilBuffer && drawStencilBuffer) |
| 302 | { |
| 303 | if (readStencilBuffer->getActualFormat() != drawStencilBuffer->getActualFormat()) |
| 304 | { |
| 305 | return gl::error(GL_INVALID_OPERATION, false); |
| 306 | } |
| 307 | |
| 308 | if (readStencilBuffer->getSamples() > 0 && !sameBounds) |
| 309 | { |
| 310 | return gl::error(GL_INVALID_OPERATION, false); |
| 311 | } |
| 312 | |
| 313 | if (fromAngleExtension) |
| 314 | { |
| 315 | if (partialCopy) |
| 316 | { |
| 317 | ERR("Only whole-buffer depth and stencil blits are supported by this implementation."); |
| 318 | return gl::error(GL_INVALID_OPERATION, false); // only whole-buffer copies are permitted |
| 319 | } |
| 320 | |
| 321 | if (readStencilBuffer->getSamples() != 0 || drawStencilBuffer->getSamples() != 0) |
| 322 | { |
| 323 | return gl::error(GL_INVALID_OPERATION, false); |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | return true; |
| 330 | } |
| 331 | |
| 332 | bool validateGetVertexAttribParameters(GLenum pname, int clientVersion) |
| 333 | { |
| 334 | switch (pname) |
| 335 | { |
| 336 | case GL_VERTEX_ATTRIB_ARRAY_ENABLED: |
| 337 | case GL_VERTEX_ATTRIB_ARRAY_SIZE: |
| 338 | case GL_VERTEX_ATTRIB_ARRAY_STRIDE: |
| 339 | case GL_VERTEX_ATTRIB_ARRAY_TYPE: |
| 340 | case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED: |
| 341 | case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING: |
| 342 | case GL_CURRENT_VERTEX_ATTRIB: |
| 343 | return true; |
| 344 | |
| 345 | case GL_VERTEX_ATTRIB_ARRAY_DIVISOR: |
| 346 | // Don't verify ES3 context because GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE uses |
| 347 | // the same constant. |
| 348 | META_ASSERT(GL_VERTEX_ATTRIB_ARRAY_DIVISOR == GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE); |
| 349 | return true; |
| 350 | |
| 351 | case GL_VERTEX_ATTRIB_ARRAY_INTEGER: |
| 352 | return ((clientVersion >= 3) ? true : gl::error(GL_INVALID_ENUM, false)); |
| 353 | |
| 354 | default: |
| 355 | return gl::error(GL_INVALID_ENUM, false); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | bool validateTexParamParameters(gl::Context *context, GLenum pname, GLint param) |
| 360 | { |
| 361 | switch (pname) |
| 362 | { |
| 363 | case GL_TEXTURE_WRAP_R: |
| 364 | case GL_TEXTURE_SWIZZLE_R: |
| 365 | case GL_TEXTURE_SWIZZLE_G: |
| 366 | case GL_TEXTURE_SWIZZLE_B: |
| 367 | case GL_TEXTURE_SWIZZLE_A: |
| 368 | case GL_TEXTURE_BASE_LEVEL: |
| 369 | case GL_TEXTURE_MAX_LEVEL: |
| 370 | case GL_TEXTURE_COMPARE_MODE: |
| 371 | case GL_TEXTURE_COMPARE_FUNC: |
| 372 | case GL_TEXTURE_MIN_LOD: |
| 373 | case GL_TEXTURE_MAX_LOD: |
| 374 | if (context->getClientVersion() < 3) |
| 375 | { |
| 376 | return gl::error(GL_INVALID_ENUM, false); |
| 377 | } |
| 378 | break; |
| 379 | |
| 380 | default: break; |
| 381 | } |
| 382 | |
| 383 | switch (pname) |
| 384 | { |
| 385 | case GL_TEXTURE_WRAP_S: |
| 386 | case GL_TEXTURE_WRAP_T: |
| 387 | case GL_TEXTURE_WRAP_R: |
| 388 | switch (param) |
| 389 | { |
| 390 | case GL_REPEAT: |
| 391 | case GL_CLAMP_TO_EDGE: |
| 392 | case GL_MIRRORED_REPEAT: |
| 393 | return true; |
| 394 | default: |
| 395 | return gl::error(GL_INVALID_ENUM, false); |
| 396 | } |
| 397 | |
| 398 | case GL_TEXTURE_MIN_FILTER: |
| 399 | switch (param) |
| 400 | { |
| 401 | case GL_NEAREST: |
| 402 | case GL_LINEAR: |
| 403 | case GL_NEAREST_MIPMAP_NEAREST: |
| 404 | case GL_LINEAR_MIPMAP_NEAREST: |
| 405 | case GL_NEAREST_MIPMAP_LINEAR: |
| 406 | case GL_LINEAR_MIPMAP_LINEAR: |
| 407 | return true; |
| 408 | default: |
| 409 | return gl::error(GL_INVALID_ENUM, false); |
| 410 | } |
| 411 | break; |
| 412 | |
| 413 | case GL_TEXTURE_MAG_FILTER: |
| 414 | switch (param) |
| 415 | { |
| 416 | case GL_NEAREST: |
| 417 | case GL_LINEAR: |
| 418 | return true; |
| 419 | default: |
| 420 | return gl::error(GL_INVALID_ENUM, false); |
| 421 | } |
| 422 | break; |
| 423 | |
| 424 | case GL_TEXTURE_USAGE_ANGLE: |
| 425 | switch (param) |
| 426 | { |
| 427 | case GL_NONE: |
| 428 | case GL_FRAMEBUFFER_ATTACHMENT_ANGLE: |
| 429 | return true; |
| 430 | default: |
| 431 | return gl::error(GL_INVALID_ENUM, false); |
| 432 | } |
| 433 | break; |
| 434 | |
| 435 | case GL_TEXTURE_MAX_ANISOTROPY_EXT: |
| 436 | if (!context->supportsTextureFilterAnisotropy()) |
| 437 | { |
| 438 | return gl::error(GL_INVALID_ENUM, false); |
| 439 | } |
| 440 | |
| 441 | // we assume the parameter passed to this validation method is truncated, not rounded |
| 442 | if (param < 1) |
| 443 | { |
| 444 | return gl::error(GL_INVALID_VALUE, false); |
| 445 | } |
| 446 | return true; |
| 447 | |
| 448 | case GL_TEXTURE_MIN_LOD: |
| 449 | case GL_TEXTURE_MAX_LOD: |
| 450 | // any value is permissible |
| 451 | return true; |
| 452 | |
| 453 | case GL_TEXTURE_COMPARE_MODE: |
| 454 | switch (param) |
| 455 | { |
| 456 | case GL_NONE: |
| 457 | case GL_COMPARE_REF_TO_TEXTURE: |
| 458 | return true; |
| 459 | default: |
| 460 | return gl::error(GL_INVALID_ENUM, false); |
| 461 | } |
| 462 | break; |
| 463 | |
| 464 | case GL_TEXTURE_COMPARE_FUNC: |
| 465 | switch (param) |
| 466 | { |
| 467 | case GL_LEQUAL: |
| 468 | case GL_GEQUAL: |
| 469 | case GL_LESS: |
| 470 | case GL_GREATER: |
| 471 | case GL_EQUAL: |
| 472 | case GL_NOTEQUAL: |
| 473 | case GL_ALWAYS: |
| 474 | case GL_NEVER: |
| 475 | return true; |
| 476 | default: |
| 477 | return gl::error(GL_INVALID_ENUM, false); |
| 478 | } |
| 479 | break; |
| 480 | |
| 481 | case GL_TEXTURE_SWIZZLE_R: |
| 482 | case GL_TEXTURE_SWIZZLE_G: |
| 483 | case GL_TEXTURE_SWIZZLE_B: |
| 484 | case GL_TEXTURE_SWIZZLE_A: |
| 485 | case GL_TEXTURE_BASE_LEVEL: |
| 486 | case GL_TEXTURE_MAX_LEVEL: |
| 487 | UNIMPLEMENTED(); |
| 488 | return true; |
| 489 | |
| 490 | default: |
| 491 | return gl::error(GL_INVALID_ENUM, false); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | bool validateSamplerObjectParameter(GLenum pname) |
| 496 | { |
| 497 | switch (pname) |
| 498 | { |
| 499 | case GL_TEXTURE_MIN_FILTER: |
| 500 | case GL_TEXTURE_MAG_FILTER: |
| 501 | case GL_TEXTURE_WRAP_S: |
| 502 | case GL_TEXTURE_WRAP_T: |
| 503 | case GL_TEXTURE_WRAP_R: |
| 504 | case GL_TEXTURE_MIN_LOD: |
| 505 | case GL_TEXTURE_MAX_LOD: |
| 506 | case GL_TEXTURE_COMPARE_MODE: |
| 507 | case GL_TEXTURE_COMPARE_FUNC: |
| 508 | return true; |
| 509 | |
| 510 | default: |
| 511 | return gl::error(GL_INVALID_ENUM, false); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | } |