From 4f2ecd53a9daaf88bb7d075745eefb6e2e4741e0 Mon Sep 17 00:00:00 2001 From: wolfbeast Date: Wed, 11 Jul 2018 18:11:13 +0200 Subject: Roll back to ANGLE/2845 --- gfx/angle/src/libANGLE/validationES3.cpp | 1124 ++++++++++++++++++------------ 1 file changed, 665 insertions(+), 459 deletions(-) (limited to 'gfx/angle/src/libANGLE/validationES3.cpp') diff --git a/gfx/angle/src/libANGLE/validationES3.cpp b/gfx/angle/src/libANGLE/validationES3.cpp index a4cb71e6a..6f3d8002a 100755 --- a/gfx/angle/src/libANGLE/validationES3.cpp +++ b/gfx/angle/src/libANGLE/validationES3.cpp @@ -8,9 +8,6 @@ #include "libANGLE/validationES3.h" -#include "base/numerics/safe_conversions.h" -#include "common/mathutil.h" -#include "common/utilities.h" #include "libANGLE/validationES.h" #include "libANGLE/Context.h" #include "libANGLE/Texture.h" @@ -19,11 +16,209 @@ #include "libANGLE/formatutils.h" #include "libANGLE/FramebufferAttachment.h" +#include "common/mathutil.h" +#include "common/utilities.h" + using namespace angle; namespace gl { +struct ES3FormatCombination +{ + GLenum internalFormat; + GLenum format; + GLenum type; +}; + +bool operator<(const ES3FormatCombination& a, const ES3FormatCombination& b) +{ + return memcmp(&a, &b, sizeof(ES3FormatCombination)) < 0; +} + +typedef std::set ES3FormatCombinationSet; + +static inline void InsertES3FormatCombo(ES3FormatCombinationSet *set, GLenum internalFormat, GLenum format, GLenum type) +{ + ES3FormatCombination info; + info.internalFormat = internalFormat; + info.format = format; + info.type = type; + set->insert(info); +} + +ES3FormatCombinationSet BuildES3FormatSet() +{ + ES3FormatCombinationSet set; + + // Format combinations from ES 3.0.1 spec, table 3.2 + + // clang-format off + // | Internal format | Format | Type | + // | | | | + InsertES3FormatCombo(&set, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_RGBA4, GL_RGBA, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_RGBA8_SNORM, GL_RGBA, GL_BYTE ); + InsertES3FormatCombo(&set, GL_RGBA4, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4 ); + InsertES3FormatCombo(&set, GL_RGB10_A2, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV ); + InsertES3FormatCombo(&set, GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV ); + InsertES3FormatCombo(&set, GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1 ); + InsertES3FormatCombo(&set, GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT ); + InsertES3FormatCombo(&set, GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT_OES ); + InsertES3FormatCombo(&set, GL_RGBA32F, GL_RGBA, GL_FLOAT ); + InsertES3FormatCombo(&set, GL_RGBA16F, GL_RGBA, GL_FLOAT ); + InsertES3FormatCombo(&set, GL_RGBA8UI, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_RGBA8I, GL_RGBA_INTEGER, GL_BYTE ); + InsertES3FormatCombo(&set, GL_RGBA16UI, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT ); + InsertES3FormatCombo(&set, GL_RGBA16I, GL_RGBA_INTEGER, GL_SHORT ); + InsertES3FormatCombo(&set, GL_RGBA32UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT ); + InsertES3FormatCombo(&set, GL_RGBA32I, GL_RGBA_INTEGER, GL_INT ); + InsertES3FormatCombo(&set, GL_RGB10_A2UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV ); + InsertES3FormatCombo(&set, GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_RGB565, GL_RGB, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_SRGB8, GL_RGB, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_RGB8_SNORM, GL_RGB, GL_BYTE ); + InsertES3FormatCombo(&set, GL_RGB565, GL_RGB, GL_UNSIGNED_SHORT_5_6_5 ); + InsertES3FormatCombo(&set, GL_R11F_G11F_B10F, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV ); + InsertES3FormatCombo(&set, GL_RGB9_E5, GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV ); + InsertES3FormatCombo(&set, GL_RGB16F, GL_RGB, GL_HALF_FLOAT ); + InsertES3FormatCombo(&set, GL_RGB16F, GL_RGB, GL_HALF_FLOAT_OES ); + InsertES3FormatCombo(&set, GL_R11F_G11F_B10F, GL_RGB, GL_HALF_FLOAT ); + InsertES3FormatCombo(&set, GL_R11F_G11F_B10F, GL_RGB, GL_HALF_FLOAT_OES ); + InsertES3FormatCombo(&set, GL_RGB9_E5, GL_RGB, GL_HALF_FLOAT ); + InsertES3FormatCombo(&set, GL_RGB9_E5, GL_RGB, GL_HALF_FLOAT_OES ); + InsertES3FormatCombo(&set, GL_RGB32F, GL_RGB, GL_FLOAT ); + InsertES3FormatCombo(&set, GL_RGB16F, GL_RGB, GL_FLOAT ); + InsertES3FormatCombo(&set, GL_R11F_G11F_B10F, GL_RGB, GL_FLOAT ); + InsertES3FormatCombo(&set, GL_RGB9_E5, GL_RGB, GL_FLOAT ); + InsertES3FormatCombo(&set, GL_RGB8UI, GL_RGB_INTEGER, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_RGB8I, GL_RGB_INTEGER, GL_BYTE ); + InsertES3FormatCombo(&set, GL_RGB16UI, GL_RGB_INTEGER, GL_UNSIGNED_SHORT ); + InsertES3FormatCombo(&set, GL_RGB16I, GL_RGB_INTEGER, GL_SHORT ); + InsertES3FormatCombo(&set, GL_RGB32UI, GL_RGB_INTEGER, GL_UNSIGNED_INT ); + InsertES3FormatCombo(&set, GL_RGB32I, GL_RGB_INTEGER, GL_INT ); + InsertES3FormatCombo(&set, GL_RG8, GL_RG, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_RG8_SNORM, GL_RG, GL_BYTE ); + InsertES3FormatCombo(&set, GL_RG16F, GL_RG, GL_HALF_FLOAT ); + InsertES3FormatCombo(&set, GL_RG16F, GL_RG, GL_HALF_FLOAT_OES ); + InsertES3FormatCombo(&set, GL_RG32F, GL_RG, GL_FLOAT ); + InsertES3FormatCombo(&set, GL_RG16F, GL_RG, GL_FLOAT ); + InsertES3FormatCombo(&set, GL_RG8UI, GL_RG_INTEGER, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_RG8I, GL_RG_INTEGER, GL_BYTE ); + InsertES3FormatCombo(&set, GL_RG16UI, GL_RG_INTEGER, GL_UNSIGNED_SHORT ); + InsertES3FormatCombo(&set, GL_RG16I, GL_RG_INTEGER, GL_SHORT ); + InsertES3FormatCombo(&set, GL_RG32UI, GL_RG_INTEGER, GL_UNSIGNED_INT ); + InsertES3FormatCombo(&set, GL_RG32I, GL_RG_INTEGER, GL_INT ); + InsertES3FormatCombo(&set, GL_R8, GL_RED, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_R8_SNORM, GL_RED, GL_BYTE ); + InsertES3FormatCombo(&set, GL_R16F, GL_RED, GL_HALF_FLOAT ); + InsertES3FormatCombo(&set, GL_R16F, GL_RED, GL_HALF_FLOAT_OES ); + InsertES3FormatCombo(&set, GL_R32F, GL_RED, GL_FLOAT ); + InsertES3FormatCombo(&set, GL_R16F, GL_RED, GL_FLOAT ); + InsertES3FormatCombo(&set, GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_R8I, GL_RED_INTEGER, GL_BYTE ); + InsertES3FormatCombo(&set, GL_R16UI, GL_RED_INTEGER, GL_UNSIGNED_SHORT ); + InsertES3FormatCombo(&set, GL_R16I, GL_RED_INTEGER, GL_SHORT ); + InsertES3FormatCombo(&set, GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT ); + InsertES3FormatCombo(&set, GL_R32I, GL_RED_INTEGER, GL_INT ); + + // Unsized formats + InsertES3FormatCombo(&set, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4 ); + InsertES3FormatCombo(&set, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1 ); + InsertES3FormatCombo(&set, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_RGB, GL_RGB, GL_UNSIGNED_SHORT_5_6_5 ); + InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_SRGB_ALPHA_EXT, GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_SRGB_EXT, GL_SRGB_EXT, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_RG, GL_RG, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_RG, GL_RG, GL_FLOAT ); + InsertES3FormatCombo(&set, GL_RG, GL_RG, GL_HALF_FLOAT ); + InsertES3FormatCombo(&set, GL_RG, GL_RG, GL_HALF_FLOAT_OES ); + InsertES3FormatCombo(&set, GL_RED, GL_RED, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_RED, GL_RED, GL_FLOAT ); + InsertES3FormatCombo(&set, GL_RED, GL_RED, GL_HALF_FLOAT ); + InsertES3FormatCombo(&set, GL_RED, GL_RED, GL_HALF_FLOAT_OES ); + InsertES3FormatCombo(&set, GL_DEPTH_STENCIL, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8 ); + + // Depth stencil formats + InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT ); + InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT ); + InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT ); + InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT ); + InsertES3FormatCombo(&set, GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8 ); + InsertES3FormatCombo(&set, GL_DEPTH32F_STENCIL8, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV); + + // From GL_EXT_sRGB + InsertES3FormatCombo(&set, GL_SRGB8_ALPHA8_EXT, GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_SRGB8, GL_SRGB_EXT, GL_UNSIGNED_BYTE ); + + // From GL_OES_texture_float + InsertES3FormatCombo(&set, GL_RGBA, GL_RGBA, GL_FLOAT ); + InsertES3FormatCombo(&set, GL_RGB, GL_RGB, GL_FLOAT ); + InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_FLOAT ); + InsertES3FormatCombo(&set, GL_LUMINANCE, GL_LUMINANCE, GL_FLOAT ); + InsertES3FormatCombo(&set, GL_ALPHA, GL_ALPHA, GL_FLOAT ); + + // From GL_OES_texture_half_float + InsertES3FormatCombo(&set, GL_RGBA, GL_RGBA, GL_HALF_FLOAT_OES ); + InsertES3FormatCombo(&set, GL_RGB, GL_RGB, GL_HALF_FLOAT_OES ); + InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT ); + InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES ); + InsertES3FormatCombo(&set, GL_LUMINANCE, GL_LUMINANCE, GL_HALF_FLOAT ); + InsertES3FormatCombo(&set, GL_LUMINANCE, GL_LUMINANCE, GL_HALF_FLOAT_OES ); + InsertES3FormatCombo(&set, GL_ALPHA, GL_ALPHA, GL_HALF_FLOAT ); + InsertES3FormatCombo(&set, GL_ALPHA, GL_ALPHA, GL_HALF_FLOAT_OES ); + + // From GL_EXT_texture_format_BGRA8888 + InsertES3FormatCombo(&set, GL_BGRA_EXT, GL_BGRA_EXT, GL_UNSIGNED_BYTE ); + + // From GL_EXT_texture_storage + // | Internal format | Format | Type | + // | | | | + InsertES3FormatCombo(&set, GL_ALPHA8_EXT, GL_ALPHA, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_LUMINANCE8_EXT, GL_LUMINANCE, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_LUMINANCE8_ALPHA8_EXT, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_ALPHA32F_EXT, GL_ALPHA, GL_FLOAT ); + InsertES3FormatCombo(&set, GL_LUMINANCE32F_EXT, GL_LUMINANCE, GL_FLOAT ); + InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA32F_EXT, GL_LUMINANCE_ALPHA, GL_FLOAT ); + InsertES3FormatCombo(&set, GL_ALPHA16F_EXT, GL_ALPHA, GL_HALF_FLOAT ); + InsertES3FormatCombo(&set, GL_ALPHA16F_EXT, GL_ALPHA, GL_HALF_FLOAT_OES ); + InsertES3FormatCombo(&set, GL_LUMINANCE16F_EXT, GL_LUMINANCE, GL_HALF_FLOAT ); + InsertES3FormatCombo(&set, GL_LUMINANCE16F_EXT, GL_LUMINANCE, GL_HALF_FLOAT_OES ); + InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA16F_EXT, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT ); + InsertES3FormatCombo(&set, GL_LUMINANCE_ALPHA16F_EXT, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES ); + + // From GL_EXT_texture_storage and GL_EXT_texture_format_BGRA8888 + InsertES3FormatCombo(&set, GL_BGRA8_EXT, GL_BGRA_EXT, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_BGRA4_ANGLEX, GL_BGRA_EXT, GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT); + InsertES3FormatCombo(&set, GL_BGRA4_ANGLEX, GL_BGRA_EXT, GL_UNSIGNED_BYTE ); + InsertES3FormatCombo(&set, GL_BGR5_A1_ANGLEX, GL_BGRA_EXT, GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT); + InsertES3FormatCombo(&set, GL_BGR5_A1_ANGLEX, GL_BGRA_EXT, GL_UNSIGNED_BYTE ); + + // From GL_ANGLE_depth_texture and OES_depth_texture + InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT32_OES, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT_24_8_OES ); + InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT ); + InsertES3FormatCombo(&set, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT ); + + // From GL_EXT_texture_norm16 + InsertES3FormatCombo(&set, GL_R16_EXT, GL_RED, GL_UNSIGNED_SHORT); + InsertES3FormatCombo(&set, GL_RG16_EXT, GL_RG, GL_UNSIGNED_SHORT); + InsertES3FormatCombo(&set, GL_RGB16_EXT, GL_RGB, GL_UNSIGNED_SHORT); + InsertES3FormatCombo(&set, GL_RGBA16_EXT, GL_RGBA, GL_UNSIGNED_SHORT); + InsertES3FormatCombo(&set, GL_R16_SNORM_EXT, GL_RED, GL_SHORT); + InsertES3FormatCombo(&set, GL_RG16_SNORM_EXT, GL_RG, GL_SHORT); + InsertES3FormatCombo(&set, GL_RGB16_SNORM_EXT, GL_RGB, GL_SHORT); + InsertES3FormatCombo(&set, GL_RGBA16_SNORM_EXT, GL_RGBA, GL_SHORT); + // clang-format on + + return set; +} + static bool ValidateTexImageFormatCombination(gl::Context *context, GLenum internalFormat, GLenum format, GLenum type) { // For historical reasons, glTexImage2D and glTexImage3D pass in their internal format as a @@ -31,21 +226,54 @@ static bool ValidateTexImageFormatCombination(gl::Context *context, GLenum inter // error instead of a GL_INVALID_ENUM error. As this validation function is only called in // the validation codepaths for glTexImage2D/3D, we record a GL_INVALID_VALUE error. const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalFormat); - if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions())) + if (!formatInfo.textureSupport(context->getClientMajorVersion(), context->getExtensions())) { context->handleError(Error(GL_INVALID_VALUE)); return false; } // The type and format are valid if any supported internal format has that type and format - if (!ValidES3Format(format) || !ValidES3Type(type)) + bool formatSupported = false; + bool typeSupported = false; + + static const ES3FormatCombinationSet es3FormatSet = BuildES3FormatSet(); + for (ES3FormatCombinationSet::const_iterator i = es3FormatSet.begin(); i != es3FormatSet.end(); i++) + { + if (i->format == format || i->type == type) + { + const gl::InternalFormat &info = gl::GetInternalFormatInfo(i->internalFormat); + bool supported = + info.textureSupport(context->getClientMajorVersion(), context->getExtensions()); + if (supported && i->type == type) + { + typeSupported = true; + } + if (supported && i->format == format) + { + formatSupported = true; + } + + // Early-out if both type and format are supported now + if (typeSupported && formatSupported) + { + break; + } + } + } + + if (!typeSupported || !formatSupported) { context->handleError(Error(GL_INVALID_ENUM)); return false; } // Check if this is a valid format combination to load texture data - if (!ValidES3FormatCombination(format, type, internalFormat)) + ES3FormatCombination searchFormat; + searchFormat.internalFormat = internalFormat; + searchFormat.format = format; + searchFormat.type = type; + + if (es3FormatSet.find(searchFormat) == es3FormatSet.end()) { context->handleError(Error(GL_INVALID_OPERATION)); return false; @@ -69,7 +297,6 @@ bool ValidateES3TexImageParametersBase(Context *context, GLint border, GLenum format, GLenum type, - GLsizei imageSize, const GLvoid *pixels) { // Validate image size @@ -168,12 +395,6 @@ bool ValidateES3TexImageParametersBase(Context *context, // Validate texture formats GLenum actualInternalFormat = isSubImage ? texture->getFormat(target, level).asSized() : internalformat; - if (isSubImage && actualInternalFormat == GL_NONE) - { - context->handleError(Error(GL_INVALID_OPERATION, "Texture level does not exist.")); - return false; - } - const gl::InternalFormat &actualFormatInfo = gl::GetInternalFormatInfo(actualInternalFormat); if (isCompressed) { @@ -190,7 +411,8 @@ bool ValidateES3TexImageParametersBase(Context *context, return false; } - if (!actualFormatInfo.textureSupport(context->getClientVersion(), context->getExtensions())) + if (!actualFormatInfo.textureSupport(context->getClientMajorVersion(), + context->getExtensions())) { context->handleError(Error(GL_INVALID_ENUM)); return false; @@ -253,27 +475,70 @@ bool ValidateES3TexImageParametersBase(Context *context, } } - if (!ValidImageDataSize(context, target, width, height, 1, actualInternalFormat, type, pixels, - imageSize)) - { - return false; - } - // Check for pixel unpack buffer related API errors gl::Buffer *pixelUnpackBuffer = context->getGLState().getTargetBuffer(GL_PIXEL_UNPACK_BUFFER); - if (pixelUnpackBuffer != nullptr) + if (pixelUnpackBuffer != NULL) { + // ...the data would be unpacked from the buffer object such that the memory reads required + // would exceed the data store size. + GLenum sizedFormat = GetSizedInternalFormat(actualInternalFormat, type); + const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(sizedFormat); + const gl::Extents size(width, height, depth); + const auto &unpack = context->getGLState().getUnpackState(); + + auto copyBytesOrErr = formatInfo.computeUnpackSize(type, size, unpack); + if (copyBytesOrErr.isError()) + { + context->handleError(copyBytesOrErr.getError()); + return false; + } + CheckedNumeric checkedCopyBytes(copyBytesOrErr.getResult()); + CheckedNumeric checkedOffset(reinterpret_cast(pixels)); + checkedCopyBytes += checkedOffset; + + auto rowPitchOrErr = + formatInfo.computeRowPitch(type, width, unpack.alignment, unpack.rowLength); + if (rowPitchOrErr.isError()) + { + context->handleError(rowPitchOrErr.getError()); + return false; + } + auto depthPitchOrErr = formatInfo.computeDepthPitch(type, width, height, unpack.alignment, + unpack.rowLength, unpack.imageHeight); + if (depthPitchOrErr.isError()) + { + context->handleError(depthPitchOrErr.getError()); + return false; + } + + bool targetIs3D = target == GL_TEXTURE_3D || target == GL_TEXTURE_2D_ARRAY; + auto skipBytesOrErr = formatInfo.computeSkipBytes( + rowPitchOrErr.getResult(), depthPitchOrErr.getResult(), unpack.skipImages, + unpack.skipRows, unpack.skipPixels, targetIs3D); + if (skipBytesOrErr.isError()) + { + context->handleError(skipBytesOrErr.getError()); + return false; + } + checkedCopyBytes += skipBytesOrErr.getResult(); + + if (!checkedCopyBytes.IsValid() || + (checkedCopyBytes.ValueOrDie() > static_cast(pixelUnpackBuffer->getSize()))) + { + // Overflow past the end of the buffer + context->handleError(Error(GL_INVALID_OPERATION)); + return false; + } + // ...data is not evenly divisible into the number of bytes needed to store in memory a datum // indicated by type. if (!isCompressed) { - size_t offset = reinterpret_cast(pixels); size_t dataBytesPerPixel = static_cast(gl::GetTypeInfo(type).bytes); - if ((offset % dataBytesPerPixel) != 0) + if ((checkedOffset.ValueOrDie() % dataBytesPerPixel) != 0) { - context->handleError( - Error(GL_INVALID_OPERATION, "Reads would overflow the pixel unpack buffer.")); + context->handleError(Error(GL_INVALID_OPERATION)); return false; } } @@ -281,7 +546,7 @@ bool ValidateES3TexImageParametersBase(Context *context, // ...the buffer object's data store is currently mapped. if (pixelUnpackBuffer->isMapped()) { - context->handleError(Error(GL_INVALID_OPERATION, "Pixel unpack buffer is mapped.")); + context->handleError(Error(GL_INVALID_OPERATION)); return false; } } @@ -304,7 +569,6 @@ bool ValidateES3TexImage2DParameters(Context *context, GLint border, GLenum format, GLenum type, - GLsizei imageSize, const GLvoid *pixels) { if (!ValidTexture2DDestinationTarget(context, target)) @@ -315,7 +579,7 @@ bool ValidateES3TexImage2DParameters(Context *context, return ValidateES3TexImageParametersBase(context, target, level, internalformat, isCompressed, isSubImage, xoffset, yoffset, zoffset, width, height, - depth, border, format, type, imageSize, pixels); + depth, border, format, type, pixels); } bool ValidateES3TexImage3DParameters(Context *context, @@ -333,7 +597,6 @@ bool ValidateES3TexImage3DParameters(Context *context, GLint border, GLenum format, GLenum type, - GLsizei bufSize, const GLvoid *pixels) { if (!ValidTexture3DDestinationTarget(context, target)) @@ -344,115 +607,169 @@ bool ValidateES3TexImage3DParameters(Context *context, return ValidateES3TexImageParametersBase(context, target, level, internalformat, isCompressed, isSubImage, xoffset, yoffset, zoffset, width, height, - depth, border, format, type, bufSize, pixels); + depth, border, format, type, pixels); } struct EffectiveInternalFormatInfo { - GLenum effectiveFormat; - GLenum destFormat; - GLuint minRedBits; - GLuint maxRedBits; - GLuint minGreenBits; - GLuint maxGreenBits; - GLuint minBlueBits; - GLuint maxBlueBits; - GLuint minAlphaBits; - GLuint maxAlphaBits; + GLenum mEffectiveFormat; + GLenum mDestFormat; + GLuint mMinRedBits; + GLuint mMaxRedBits; + GLuint mMinGreenBits; + GLuint mMaxGreenBits; + GLuint mMinBlueBits; + GLuint mMaxBlueBits; + GLuint mMinAlphaBits; + GLuint mMaxAlphaBits; + + EffectiveInternalFormatInfo(GLenum effectiveFormat, GLenum destFormat, GLuint minRedBits, GLuint maxRedBits, + GLuint minGreenBits, GLuint maxGreenBits, GLuint minBlueBits, GLuint maxBlueBits, + GLuint minAlphaBits, GLuint maxAlphaBits) + : mEffectiveFormat(effectiveFormat), mDestFormat(destFormat), mMinRedBits(minRedBits), + mMaxRedBits(maxRedBits), mMinGreenBits(minGreenBits), mMaxGreenBits(maxGreenBits), + mMinBlueBits(minBlueBits), mMaxBlueBits(maxBlueBits), mMinAlphaBits(minAlphaBits), + mMaxAlphaBits(maxAlphaBits) {}; }; -static bool QueryEffectiveFormatList(const InternalFormat &srcFormat, - GLenum targetFormat, - const EffectiveInternalFormatInfo *list, - size_t size, - GLenum *outEffectiveFormat) -{ - for (size_t curFormat = 0; curFormat < size; ++curFormat) - { - const EffectiveInternalFormatInfo &formatInfo = list[curFormat]; - if ((formatInfo.destFormat == targetFormat) && - (formatInfo.minRedBits <= srcFormat.redBits && - formatInfo.maxRedBits >= srcFormat.redBits) && - (formatInfo.minGreenBits <= srcFormat.greenBits && - formatInfo.maxGreenBits >= srcFormat.greenBits) && - (formatInfo.minBlueBits <= srcFormat.blueBits && - formatInfo.maxBlueBits >= srcFormat.blueBits) && - (formatInfo.minAlphaBits <= srcFormat.alphaBits && - formatInfo.maxAlphaBits >= srcFormat.alphaBits)) - { - *outEffectiveFormat = formatInfo.effectiveFormat; - return true; - } - } +typedef std::vector EffectiveInternalFormatList; - *outEffectiveFormat = GL_NONE; - return false; -} - -bool GetSizedEffectiveInternalFormatInfo(const InternalFormat &srcFormat, - GLenum *outEffectiveFormat) +static EffectiveInternalFormatList BuildSizedEffectiveInternalFormatList() { - // OpenGL ES 3.0.3 Specification, Table 3.17, pg 141: - // Effective internal format coresponding to destination internal format and linear source - // buffer component sizes. - // | Source channel min/max sizes | - // Effective Internal Format | N/A | R | G | B | A | - // clang-format off - constexpr EffectiveInternalFormatInfo list[] = { - { GL_ALPHA8_EXT, GL_NONE, 0, 0, 0, 0, 0, 0, 1, 8 }, - { GL_R8, GL_NONE, 1, 8, 0, 0, 0, 0, 0, 0 }, - { GL_RG8, GL_NONE, 1, 8, 1, 8, 0, 0, 0, 0 }, - { GL_RGB565, GL_NONE, 1, 5, 1, 6, 1, 5, 0, 0 }, - { GL_RGB8, GL_NONE, 6, 8, 7, 8, 6, 8, 0, 0 }, - { GL_RGBA4, GL_NONE, 1, 4, 1, 4, 1, 4, 1, 4 }, - { GL_RGB5_A1, GL_NONE, 5, 5, 5, 5, 5, 5, 1, 1 }, - { GL_RGBA8, GL_NONE, 5, 8, 5, 8, 5, 8, 2, 8 }, - { GL_RGB10_A2, GL_NONE, 9, 10, 9, 10, 9, 10, 2, 2 }, - }; - // clang-format on + EffectiveInternalFormatList list; - return QueryEffectiveFormatList(srcFormat, GL_NONE, list, ArraySize(list), outEffectiveFormat); + // OpenGL ES 3.0.3 Specification, Table 3.17, pg 141: Effective internal format coresponding to destination internal format and + // linear source buffer component sizes. + // | Source channel min/max sizes | + // Effective Internal Format | N/A | R | G | B | A | + list.push_back(EffectiveInternalFormatInfo(GL_ALPHA8_EXT, GL_NONE, 0, 0, 0, 0, 0, 0, 1, 8)); + list.push_back(EffectiveInternalFormatInfo(GL_R8, GL_NONE, 1, 8, 0, 0, 0, 0, 0, 0)); + list.push_back(EffectiveInternalFormatInfo(GL_RG8, GL_NONE, 1, 8, 1, 8, 0, 0, 0, 0)); + list.push_back(EffectiveInternalFormatInfo(GL_RGB565, GL_NONE, 1, 5, 1, 6, 1, 5, 0, 0)); + list.push_back(EffectiveInternalFormatInfo(GL_RGB8, GL_NONE, 6, 8, 7, 8, 6, 8, 0, 0)); + list.push_back(EffectiveInternalFormatInfo(GL_RGBA4, GL_NONE, 1, 4, 1, 4, 1, 4, 1, 4)); + list.push_back(EffectiveInternalFormatInfo(GL_RGB5_A1, GL_NONE, 5, 5, 5, 5, 5, 5, 1, 1)); + list.push_back(EffectiveInternalFormatInfo(GL_RGBA8, GL_NONE, 5, 8, 5, 8, 5, 8, 2, 8)); + list.push_back(EffectiveInternalFormatInfo(GL_RGB10_A2, GL_NONE, 9, 10, 9, 10, 9, 10, 2, 2)); + + return list; } -bool GetUnsizedEffectiveInternalFormatInfo(const InternalFormat &srcFormat, - const InternalFormat &destFormat, - GLenum *outEffectiveFormat) +static EffectiveInternalFormatList BuildUnsizedEffectiveInternalFormatList() { - constexpr GLuint umax = UINT_MAX; + EffectiveInternalFormatList list; - // OpenGL ES 3.0.3 Specification, Table 3.17, pg 141: - // Effective internal format coresponding to destination internal format andlinear source buffer - // component sizes. - // | Source channel min/max sizes | - // Effective Internal Format | Dest Format | R | G | B | A | - // clang-format off - constexpr EffectiveInternalFormatInfo list[] = { - { GL_ALPHA8_EXT, GL_ALPHA, 0, umax, 0, umax, 0, umax, 1, 8 }, - { GL_LUMINANCE8_EXT, GL_LUMINANCE, 1, 8, 0, umax, 0, umax, 0, umax }, - { GL_LUMINANCE8_ALPHA8_EXT, GL_LUMINANCE_ALPHA, 1, 8, 0, umax, 0, umax, 1, 8 }, - { GL_RGB565, GL_RGB, 1, 5, 1, 6, 1, 5, 0, umax }, - { GL_RGB8, GL_RGB, 6, 8, 7, 8, 6, 8, 0, umax }, - { GL_RGBA4, GL_RGBA, 1, 4, 1, 4, 1, 4, 1, 4 }, - { GL_RGB5_A1, GL_RGBA, 5, 5, 5, 5, 5, 5, 1, 1 }, - { GL_RGBA8, GL_RGBA, 5, 8, 5, 8, 5, 8, 5, 8 }, - }; - // clang-format on + // OpenGL ES 3.0.3 Specification, Table 3.17, pg 141: Effective internal format coresponding to destination internal format and + // linear source buffer component sizes. + // | Source channel min/max sizes | + // Effective Internal Format | Dest Format | R | G | B | A | + list.push_back(EffectiveInternalFormatInfo(GL_ALPHA8_EXT, GL_ALPHA, 0, UINT_MAX, 0, UINT_MAX, 0, UINT_MAX, 1, 8)); + list.push_back(EffectiveInternalFormatInfo(GL_LUMINANCE8_EXT, GL_LUMINANCE, 1, 8, 0, UINT_MAX, 0, UINT_MAX, 0, UINT_MAX)); + list.push_back(EffectiveInternalFormatInfo(GL_LUMINANCE8_ALPHA8_EXT, GL_LUMINANCE_ALPHA, 1, 8, 0, UINT_MAX, 0, UINT_MAX, 1, 8)); + list.push_back(EffectiveInternalFormatInfo(GL_RGB565, GL_RGB, 1, 5, 1, 6, 1, 5, 0, UINT_MAX)); + list.push_back(EffectiveInternalFormatInfo(GL_RGB8, GL_RGB, 6, 8, 7, 8, 6, 8, 0, UINT_MAX)); + list.push_back(EffectiveInternalFormatInfo(GL_RGBA4, GL_RGBA, 1, 4, 1, 4, 1, 4, 1, 4)); + list.push_back(EffectiveInternalFormatInfo(GL_RGB5_A1, GL_RGBA, 5, 5, 5, 5, 5, 5, 1, 1)); + list.push_back(EffectiveInternalFormatInfo(GL_RGBA8, GL_RGBA, 5, 8, 5, 8, 5, 8, 5, 8)); - return QueryEffectiveFormatList(srcFormat, destFormat.format, list, ArraySize(list), - outEffectiveFormat); + return list; } static bool GetEffectiveInternalFormat(const InternalFormat &srcFormat, const InternalFormat &destFormat, GLenum *outEffectiveFormat) { + const EffectiveInternalFormatList *list = NULL; + GLenum targetFormat = GL_NONE; + if (destFormat.pixelBytes > 0) { - return GetSizedEffectiveInternalFormatInfo(srcFormat, outEffectiveFormat); + static const EffectiveInternalFormatList sizedList = BuildSizedEffectiveInternalFormatList(); + list = &sizedList; } else { - return GetUnsizedEffectiveInternalFormatInfo(srcFormat, destFormat, outEffectiveFormat); + static const EffectiveInternalFormatList unsizedList = BuildUnsizedEffectiveInternalFormatList(); + list = &unsizedList; + targetFormat = destFormat.format; } + + for (size_t curFormat = 0; curFormat < list->size(); ++curFormat) + { + const EffectiveInternalFormatInfo& formatInfo = list->at(curFormat); + if ((formatInfo.mDestFormat == targetFormat) && + (formatInfo.mMinRedBits <= srcFormat.redBits && formatInfo.mMaxRedBits >= srcFormat.redBits) && + (formatInfo.mMinGreenBits <= srcFormat.greenBits && formatInfo.mMaxGreenBits >= srcFormat.greenBits) && + (formatInfo.mMinBlueBits <= srcFormat.blueBits && formatInfo.mMaxBlueBits >= srcFormat.blueBits) && + (formatInfo.mMinAlphaBits <= srcFormat.alphaBits && formatInfo.mMaxAlphaBits >= srcFormat.alphaBits)) + { + *outEffectiveFormat = formatInfo.mEffectiveFormat; + return true; + } + } + + return false; +} + +struct CopyConversion +{ + GLenum mTextureFormat; + GLenum mFramebufferFormat; + + CopyConversion(GLenum textureFormat, GLenum framebufferFormat) + : mTextureFormat(textureFormat), mFramebufferFormat(framebufferFormat) { } + + bool operator<(const CopyConversion& other) const + { + return memcmp(this, &other, sizeof(CopyConversion)) < 0; + } +}; + +typedef std::set CopyConversionSet; + +static CopyConversionSet BuildValidES3CopyTexImageCombinations() +{ + CopyConversionSet set; + + // From ES 3.0.1 spec, table 3.15 + set.insert(CopyConversion(GL_ALPHA, GL_RGBA)); + set.insert(CopyConversion(GL_LUMINANCE, GL_RED)); + set.insert(CopyConversion(GL_LUMINANCE, GL_RG)); + set.insert(CopyConversion(GL_LUMINANCE, GL_RGB)); + set.insert(CopyConversion(GL_LUMINANCE, GL_RGBA)); + set.insert(CopyConversion(GL_LUMINANCE_ALPHA, GL_RGBA)); + set.insert(CopyConversion(GL_RED, GL_RED)); + set.insert(CopyConversion(GL_RED, GL_RG)); + set.insert(CopyConversion(GL_RED, GL_RGB)); + set.insert(CopyConversion(GL_RED, GL_RGBA)); + set.insert(CopyConversion(GL_RG, GL_RG)); + set.insert(CopyConversion(GL_RG, GL_RGB)); + set.insert(CopyConversion(GL_RG, GL_RGBA)); + set.insert(CopyConversion(GL_RGB, GL_RGB)); + set.insert(CopyConversion(GL_RGB, GL_RGBA)); + set.insert(CopyConversion(GL_RGBA, GL_RGBA)); + + // Necessary for ANGLE back-buffers + set.insert(CopyConversion(GL_ALPHA, GL_BGRA_EXT)); + set.insert(CopyConversion(GL_LUMINANCE, GL_BGRA_EXT)); + set.insert(CopyConversion(GL_LUMINANCE_ALPHA, GL_BGRA_EXT)); + set.insert(CopyConversion(GL_RED, GL_BGRA_EXT)); + set.insert(CopyConversion(GL_RG, GL_BGRA_EXT)); + set.insert(CopyConversion(GL_RGB, GL_BGRA_EXT)); + set.insert(CopyConversion(GL_RGBA, GL_BGRA_EXT)); + set.insert(CopyConversion(GL_BGRA_EXT, GL_BGRA_EXT)); + + set.insert(CopyConversion(GL_RED_INTEGER, GL_RED_INTEGER)); + set.insert(CopyConversion(GL_RED_INTEGER, GL_RG_INTEGER)); + set.insert(CopyConversion(GL_RED_INTEGER, GL_RGB_INTEGER)); + set.insert(CopyConversion(GL_RED_INTEGER, GL_RGBA_INTEGER)); + set.insert(CopyConversion(GL_RG_INTEGER, GL_RG_INTEGER)); + set.insert(CopyConversion(GL_RG_INTEGER, GL_RGB_INTEGER)); + set.insert(CopyConversion(GL_RG_INTEGER, GL_RGBA_INTEGER)); + set.insert(CopyConversion(GL_RGB_INTEGER, GL_RGB_INTEGER)); + set.insert(CopyConversion(GL_RGB_INTEGER, GL_RGBA_INTEGER)); + set.insert(CopyConversion(GL_RGBA_INTEGER, GL_RGBA_INTEGER)); + + return set; } static bool EqualOrFirstZero(GLuint first, GLuint second) @@ -467,129 +784,129 @@ static bool IsValidES3CopyTexImageCombination(const Format &textureFormat, const auto &textureFormatInfo = *textureFormat.info; const auto &framebufferFormatInfo = *framebufferFormat.info; - if (!ValidES3CopyConversion(textureFormatInfo.format, framebufferFormatInfo.format)) + static const CopyConversionSet conversionSet = BuildValidES3CopyTexImageCombinations(); + if (conversionSet.find(CopyConversion(textureFormatInfo.format, + framebufferFormatInfo.format)) != conversionSet.end()) { - return false; - } - - // Section 3.8.5 of the GLES 3.0.3 spec states that source and destination formats - // must both be signed, unsigned, or fixed point and both source and destinations - // must be either both SRGB or both not SRGB. EXT_color_buffer_float adds allowed - // conversion between fixed and floating point. + // Section 3.8.5 of the GLES 3.0.3 spec states that source and destination formats + // must both be signed, unsigned, or fixed point and both source and destinations + // must be either both SRGB or both not SRGB. EXT_color_buffer_float adds allowed + // conversion between fixed and floating point. - if ((textureFormatInfo.colorEncoding == GL_SRGB) != - (framebufferFormatInfo.colorEncoding == GL_SRGB)) - { - return false; - } - - if (((textureFormatInfo.componentType == GL_INT) != - (framebufferFormatInfo.componentType == GL_INT)) || - ((textureFormatInfo.componentType == GL_UNSIGNED_INT) != - (framebufferFormatInfo.componentType == GL_UNSIGNED_INT))) - { - return false; - } - - if ((textureFormatInfo.componentType == GL_UNSIGNED_NORMALIZED || - textureFormatInfo.componentType == GL_SIGNED_NORMALIZED || - textureFormatInfo.componentType == GL_FLOAT) && - !(framebufferFormatInfo.componentType == GL_UNSIGNED_NORMALIZED || - framebufferFormatInfo.componentType == GL_SIGNED_NORMALIZED || - framebufferFormatInfo.componentType == GL_FLOAT)) - { - return false; - } + if ((textureFormatInfo.colorEncoding == GL_SRGB) != + (framebufferFormatInfo.colorEncoding == GL_SRGB)) + { + return false; + } - // GLES specification 3.0.3, sec 3.8.5, pg 139-140: - // The effective internal format of the source buffer is determined with the following rules - // applied in order: - // * If the source buffer is a texture or renderbuffer that was created with a sized internal - // format then the effective internal format is the source buffer's sized internal format. - // * If the source buffer is a texture that was created with an unsized base internal format, - // then the effective internal format is the source image array's effective internal - // format, as specified by table 3.12, which is determined from the and - // that were used when the source image array was specified by TexImage*. - // * Otherwise the effective internal format is determined by the row in table 3.17 or 3.18 - // where Destination Internal Format matches internalformat and where the [source channel - // sizes] are consistent with the values of the source buffer's [channel sizes]. Table 3.17 - // is used if the FRAMEBUFFER_ATTACHMENT_ENCODING is LINEAR and table 3.18 is used if the - // FRAMEBUFFER_ATTACHMENT_ENCODING is SRGB. - const InternalFormat *sourceEffectiveFormat = NULL; - if (readBufferHandle != 0) - { - // Not the default framebuffer, therefore the read buffer must be a user-created texture or - // renderbuffer - if (framebufferFormat.sized) + if (((textureFormatInfo.componentType == GL_INT) != + (framebufferFormatInfo.componentType == GL_INT)) || + ((textureFormatInfo.componentType == GL_UNSIGNED_INT) != + (framebufferFormatInfo.componentType == GL_UNSIGNED_INT))) { - sourceEffectiveFormat = &framebufferFormatInfo; + return false; } - else + + if ((textureFormatInfo.componentType == GL_UNSIGNED_NORMALIZED || + textureFormatInfo.componentType == GL_SIGNED_NORMALIZED || + textureFormatInfo.componentType == GL_FLOAT) && + !(framebufferFormatInfo.componentType == GL_UNSIGNED_NORMALIZED || + framebufferFormatInfo.componentType == GL_SIGNED_NORMALIZED || + framebufferFormatInfo.componentType == GL_FLOAT)) { - // Renderbuffers cannot be created with an unsized internal format, so this must be an - // unsized-format texture. We can use the same table we use when creating textures to - // get its effective sized format. - GLenum sizedInternalFormat = - GetSizedInternalFormat(framebufferFormatInfo.format, framebufferFormatInfo.type); - sourceEffectiveFormat = &GetInternalFormatInfo(sizedInternalFormat); + return false; } - } - else - { - // The effective internal format must be derived from the source framebuffer's channel - // sizes. This is done in GetEffectiveInternalFormat for linear buffers (table 3.17) - if (framebufferFormatInfo.colorEncoding == GL_LINEAR) + + // GLES specification 3.0.3, sec 3.8.5, pg 139-140: + // The effective internal format of the source buffer is determined with the following rules applied in order: + // * If the source buffer is a texture or renderbuffer that was created with a sized internal format then the + // effective internal format is the source buffer's sized internal format. + // * If the source buffer is a texture that was created with an unsized base internal format, then the + // effective internal format is the source image array's effective internal format, as specified by table + // 3.12, which is determined from the and that were used when the source image array was + // specified by TexImage*. + // * Otherwise the effective internal format is determined by the row in table 3.17 or 3.18 where + // Destination Internal Format matches internalformat and where the [source channel sizes] are consistent + // with the values of the source buffer's [channel sizes]. Table 3.17 is used if the + // FRAMEBUFFER_ATTACHMENT_ENCODING is LINEAR and table 3.18 is used if the FRAMEBUFFER_ATTACHMENT_ENCODING + // is SRGB. + const InternalFormat *sourceEffectiveFormat = NULL; + if (readBufferHandle != 0) { - GLenum effectiveFormat; - if (GetEffectiveInternalFormat(framebufferFormatInfo, textureFormatInfo, - &effectiveFormat)) + // Not the default framebuffer, therefore the read buffer must be a user-created texture or renderbuffer + if (framebufferFormat.sized) { - sourceEffectiveFormat = &GetInternalFormatInfo(effectiveFormat); + sourceEffectiveFormat = &framebufferFormatInfo; } else { - return false; + // Renderbuffers cannot be created with an unsized internal format, so this must be an unsized-format + // texture. We can use the same table we use when creating textures to get its effective sized format. + GLenum sizedInternalFormat = GetSizedInternalFormat(framebufferFormatInfo.format, + framebufferFormatInfo.type); + sourceEffectiveFormat = &GetInternalFormatInfo(sizedInternalFormat); } } - else if (framebufferFormatInfo.colorEncoding == GL_SRGB) + else { - // SRGB buffers can only be copied to sized format destinations according to table 3.18 - if (textureFormat.sized && - (framebufferFormatInfo.redBits >= 1 && framebufferFormatInfo.redBits <= 8) && - (framebufferFormatInfo.greenBits >= 1 && framebufferFormatInfo.greenBits <= 8) && - (framebufferFormatInfo.blueBits >= 1 && framebufferFormatInfo.blueBits <= 8) && - (framebufferFormatInfo.alphaBits >= 1 && framebufferFormatInfo.alphaBits <= 8)) + // The effective internal format must be derived from the source framebuffer's channel sizes. + // This is done in GetEffectiveInternalFormat for linear buffers (table 3.17) + if (framebufferFormatInfo.colorEncoding == GL_LINEAR) { - sourceEffectiveFormat = &GetInternalFormatInfo(GL_SRGB8_ALPHA8); + GLenum effectiveFormat; + if (GetEffectiveInternalFormat(framebufferFormatInfo, textureFormatInfo, + &effectiveFormat)) + { + sourceEffectiveFormat = &GetInternalFormatInfo(effectiveFormat); + } + else + { + return false; + } + } + else if (framebufferFormatInfo.colorEncoding == GL_SRGB) + { + // SRGB buffers can only be copied to sized format destinations according to table 3.18 + if (textureFormat.sized && + (framebufferFormatInfo.redBits >= 1 && framebufferFormatInfo.redBits <= 8) && + (framebufferFormatInfo.greenBits >= 1 && + framebufferFormatInfo.greenBits <= 8) && + (framebufferFormatInfo.blueBits >= 1 && framebufferFormatInfo.blueBits <= 8) && + (framebufferFormatInfo.alphaBits >= 1 && framebufferFormatInfo.alphaBits <= 8)) + { + sourceEffectiveFormat = &GetInternalFormatInfo(GL_SRGB8_ALPHA8); + } + else + { + return false; + } } else { + UNREACHABLE(); return false; } } - else - { - UNREACHABLE(); - return false; - } - } - if (textureFormat.sized) - { - // Section 3.8.5 of the GLES 3.0.3 spec, pg 139, requires that, if the destination format is - // sized, component sizes of the source and destination formats must exactly match if the - // destination format exists. - if (!EqualOrFirstZero(textureFormatInfo.redBits, sourceEffectiveFormat->redBits) || - !EqualOrFirstZero(textureFormatInfo.greenBits, sourceEffectiveFormat->greenBits) || - !EqualOrFirstZero(textureFormatInfo.blueBits, sourceEffectiveFormat->blueBits) || - !EqualOrFirstZero(textureFormatInfo.alphaBits, sourceEffectiveFormat->alphaBits)) + if (textureFormat.sized) { - return false; + // Section 3.8.5 of the GLES 3.0.3 spec, pg 139, requires that, if the destination + // format is sized, component sizes of the source and destination formats must exactly + // match if the destination format exists. + if (!EqualOrFirstZero(textureFormatInfo.redBits, sourceEffectiveFormat->redBits) || + !EqualOrFirstZero(textureFormatInfo.greenBits, sourceEffectiveFormat->greenBits) || + !EqualOrFirstZero(textureFormatInfo.blueBits, sourceEffectiveFormat->blueBits) || + !EqualOrFirstZero(textureFormatInfo.alphaBits, sourceEffectiveFormat->alphaBits)) + { + return false; + } } + + return true; // A conversion function exists, and no rule in the specification has precluded conversion + // between these formats. } - return true; // A conversion function exists, and no rule in the specification has precluded - // conversion between these formats. + return false; } bool ValidateES3CopyTexImageParametersBase(ValidationContext *context, @@ -808,7 +1125,7 @@ bool ValidateES3TexStorageParametersBase(Context *context, } const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalformat); - if (!formatInfo.textureSupport(context->getClientVersion(), context->getExtensions())) + if (!formatInfo.textureSupport(context->getClientMajorVersion(), context->getExtensions())) { context->handleError(Error(GL_INVALID_ENUM)); return false; @@ -889,7 +1206,7 @@ bool ValidateGetQueryiv(Context *context, GLenum target, GLenum pname, GLint *pa return false; } - return ValidateGetQueryivBase(context, target, pname, nullptr); + return ValidateGetQueryivBase(context, target, pname); } bool ValidateGetQueryObjectuiv(Context *context, GLuint id, GLenum pname, GLuint *params) @@ -900,7 +1217,7 @@ bool ValidateGetQueryObjectuiv(Context *context, GLuint id, GLenum pname, GLuint return false; } - return ValidateGetQueryObjectValueBase(context, id, pname, nullptr); + return ValidateGetQueryObjectValueBase(context, id, pname); } bool ValidateFramebufferTextureLayer(Context *context, GLenum target, GLenum attachment, @@ -979,6 +1296,100 @@ bool ValidateFramebufferTextureLayer(Context *context, GLenum target, GLenum att return true; } +bool ValidES3ReadFormatType(ValidationContext *context, + GLenum internalFormat, + GLenum format, + GLenum type) +{ + const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(internalFormat); + + switch (format) + { + case GL_RGBA: + switch (type) + { + case GL_UNSIGNED_BYTE: + break; + case GL_UNSIGNED_SHORT: + if (internalFormatInfo.componentType != GL_UNSIGNED_NORMALIZED && + internalFormatInfo.type != GL_UNSIGNED_SHORT) + { + return false; + } + break; + case GL_UNSIGNED_INT_2_10_10_10_REV: + if (internalFormat != GL_RGB10_A2) + { + return false; + } + break; + case GL_FLOAT: + if (internalFormatInfo.componentType != GL_FLOAT) + { + return false; + } + break; + default: + return false; + } + break; + case GL_RGBA_INTEGER: + switch (type) + { + case GL_INT: + if (internalFormatInfo.componentType != GL_INT) + { + return false; + } + break; + case GL_UNSIGNED_INT: + if (internalFormatInfo.componentType != GL_UNSIGNED_INT) + { + return false; + } + break; + default: + return false; + } + break; + case GL_BGRA_EXT: + switch (type) + { + case GL_UNSIGNED_BYTE: + case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT: + case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT: + break; + default: + return false; + } + break; + case GL_RG_EXT: + case GL_RED_EXT: + if (!context->getExtensions().textureRG) + { + return false; + } + switch (type) + { + case GL_UNSIGNED_BYTE: + break; + case GL_UNSIGNED_SHORT: + if (internalFormatInfo.componentType != GL_UNSIGNED_NORMALIZED && + internalFormatInfo.type != GL_UNSIGNED_SHORT) + { + return false; + } + break; + default: + return false; + } + break; + default: + return false; + } + return true; +} + bool ValidateES3RenderbufferStorageParameters(gl::Context *context, GLenum target, GLsizei samples, GLenum internalformat, GLsizei width, GLsizei height) { @@ -1191,7 +1602,7 @@ bool ValidateCompressedTexImage3D(Context *context, } auto blockSizeOrErr = - formatInfo.computeCompressedImageSize(gl::Extents(width, height, depth)); + formatInfo.computeCompressedImageSize(GL_UNSIGNED_BYTE, gl::Extents(width, height, depth)); if (blockSizeOrErr.isError()) { context->handleError(Error(GL_INVALID_VALUE)); @@ -1213,8 +1624,7 @@ bool ValidateCompressedTexImage3D(Context *context, // validateES3TexImageFormat sets the error code if there is an error if (!ValidateES3TexImage3DParameters(context, target, level, internalformat, true, false, 0, 0, - 0, width, height, depth, border, GL_NONE, GL_NONE, -1, - data)) + 0, width, height, depth, border, GL_NONE, GL_NONE, data)) { return false; } @@ -1491,38 +1901,9 @@ bool ValidateTexImage3D(Context *context, return false; } - return ValidateES3TexImage3DParameters(context, target, level, internalformat, false, false, 0, - 0, 0, width, height, depth, border, format, type, -1, - pixels); -} - -bool ValidateTexImage3DRobustANGLE(Context *context, - GLenum target, - GLint level, - GLint internalformat, - GLsizei width, - GLsizei height, - GLsizei depth, - GLint border, - GLenum format, - GLenum type, - GLsizei bufSize, - const GLvoid *pixels) -{ - if (context->getClientMajorVersion() < 3) - { - context->handleError(Error(GL_INVALID_OPERATION)); - return false; - } - - if (!ValidateRobustEntryPoint(context, bufSize)) - { - return false; - } - return ValidateES3TexImage3DParameters(context, target, level, internalformat, false, false, 0, 0, 0, width, height, depth, border, format, type, - bufSize, pixels); + pixels); } bool ValidateTexSubImage3D(Context *context, @@ -1546,37 +1927,7 @@ bool ValidateTexSubImage3D(Context *context, return ValidateES3TexImage3DParameters(context, target, level, GL_NONE, false, true, xoffset, yoffset, zoffset, width, height, depth, 0, format, type, - -1, pixels); -} - -bool ValidateTexSubImage3DRobustANGLE(Context *context, - GLenum target, - GLint level, - GLint xoffset, - GLint yoffset, - GLint zoffset, - GLsizei width, - GLsizei height, - GLsizei depth, - GLenum format, - GLenum type, - GLsizei bufSize, - const GLvoid *pixels) -{ - if (context->getClientMajorVersion() < 3) - { - context->handleError(Error(GL_INVALID_OPERATION)); - return false; - } - - if (!ValidateRobustEntryPoint(context, bufSize)) - { - return false; - } - - return ValidateES3TexImage3DParameters(context, target, level, GL_NONE, false, true, xoffset, - yoffset, zoffset, width, height, depth, 0, format, type, - bufSize, pixels); + pixels); } bool ValidateCompressedTexSubImage3D(Context *context, @@ -1600,7 +1951,7 @@ bool ValidateCompressedTexSubImage3D(Context *context, const InternalFormat &formatInfo = GetInternalFormatInfo(format); auto blockSizeOrErr = - formatInfo.computeCompressedImageSize(gl::Extents(width, height, depth)); + formatInfo.computeCompressedImageSize(GL_UNSIGNED_BYTE, gl::Extents(width, height, depth)); if (blockSizeOrErr.isError()) { context->handleError(blockSizeOrErr.getError()); @@ -1619,7 +1970,7 @@ bool ValidateCompressedTexSubImage3D(Context *context, } return ValidateES3TexImage3DParameters(context, target, level, GL_NONE, true, true, 0, 0, 0, - width, height, depth, 0, GL_NONE, GL_NONE, -1, data); + width, height, depth, 0, GL_NONE, GL_NONE, data); } bool ValidateGenQueries(Context *context, GLint n, GLuint *) @@ -1732,36 +2083,50 @@ bool ValidateBeginTransformFeedback(Context *context, GLenum primitiveMode) return true; } -bool ValidateGetBufferPointerv(Context *context, GLenum target, GLenum pname, GLvoid **params) -{ - return ValidateGetBufferPointervBase(context, target, pname, nullptr, params); -} - -bool ValidateGetBufferPointervRobustANGLE(Context *context, - GLenum target, - GLenum pname, - GLsizei bufSize, - GLsizei *length, - GLvoid **params) +bool ValidateSamplerParameteri(Context *context, GLuint sampler, GLenum pname, GLint param) { - if (!ValidateRobustEntryPoint(context, bufSize)) + if (context->getClientMajorVersion() < 3) { + context->handleError(Error(GL_INVALID_OPERATION, "Context does not support GLES3.")); return false; } - if (!ValidateGetBufferPointervBase(context, target, pname, length, params)) + if (!context->isSampler(sampler)) { + context->handleError(Error(GL_INVALID_OPERATION)); return false; } - if (!ValidateRobustBufferSize(context, bufSize, *length)) + if (!ValidateSamplerObjectParameter(context, pname)) { return false; } + if (!ValidateTexParamParameters(context, GL_TEXTURE_2D, pname, param)) + { + return false; + } return true; } +bool ValidateSamplerParameterf(Context *context, GLuint sampler, GLenum pname, GLfloat param) +{ + // The only float parameters are MIN_LOD and MAX_LOD. For these any value is permissible, so + // ValidateSamplerParameteri can be used for validation here. + return ValidateSamplerParameteri(context, sampler, pname, static_cast(param)); +} + +bool ValidateGetBufferPointerv(Context *context, GLenum target, GLenum pname, GLvoid **params) +{ + if (context->getClientMajorVersion() < 3) + { + context->handleError(Error(GL_INVALID_OPERATION, "Context does not support GLES3.")); + return false; + } + + return ValidateGetBufferPointervBase(context, target, pname, params); +} + bool ValidateUnmapBuffer(Context *context, GLenum target) { if (context->getClientMajorVersion() < 3) @@ -1802,16 +2167,8 @@ bool ValidateFlushMappedBufferRange(Context *context, return ValidateFlushMappedBufferRangeBase(context, target, offset, length); } -bool ValidateIndexedStateQuery(ValidationContext *context, - GLenum pname, - GLuint index, - GLsizei *length) +bool ValidateIndexedStateQuery(ValidationContext *context, GLenum pname, GLuint index) { - if (length) - { - *length = 0; - } - GLenum nativeType; unsigned int numParams; if (!context->getIndexedQueryParameterInfo(pname, &nativeType, &numParams)) @@ -1855,9 +2212,10 @@ bool ValidateIndexedStateQuery(ValidationContext *context, return false; } - if (length) + // pname is valid, but there are no parameters to return + if (numParams == 0) { - *length = 1; + return false; } return true; @@ -1865,174 +2223,22 @@ bool ValidateIndexedStateQuery(ValidationContext *context, bool ValidateGetIntegeri_v(ValidationContext *context, GLenum target, GLuint index, GLint *data) { - if (context->getClientVersion() < ES_3_0) - { - context->handleError(Error(GL_INVALID_OPERATION, "Context does not support GLES3.0")); - return false; - } - return ValidateIndexedStateQuery(context, target, index, nullptr); -} - -bool ValidateGetIntegeri_vRobustANGLE(ValidationContext *context, - GLenum target, - GLuint index, - GLsizei bufSize, - GLsizei *length, - GLint *data) -{ - if (context->getClientVersion() < ES_3_0) + if (!context->getGLVersion().isES3OrGreater()) { context->handleError(Error(GL_INVALID_OPERATION, "Context does not support GLES3.0")); return false; } - - if (!ValidateRobustEntryPoint(context, bufSize)) - { - return false; - } - - if (!ValidateIndexedStateQuery(context, target, index, length)) - { - return false; - } - - if (!ValidateRobustBufferSize(context, bufSize, *length)) - { - return false; - } - - return true; + return ValidateIndexedStateQuery(context, target, index); } bool ValidateGetInteger64i_v(ValidationContext *context, GLenum target, GLuint index, GLint64 *data) { - if (context->getClientVersion() < ES_3_0) + if (!context->getGLVersion().isES3OrGreater()) { context->handleError(Error(GL_INVALID_OPERATION, "Context does not support GLES3.0")); return false; } - return ValidateIndexedStateQuery(context, target, index, nullptr); -} - -bool ValidateGetInteger64i_vRobustANGLE(ValidationContext *context, - GLenum target, - GLuint index, - GLsizei bufSize, - GLsizei *length, - GLint64 *data) -{ - if (context->getClientVersion() < ES_3_0) - { - context->handleError(Error(GL_INVALID_OPERATION, "Context does not support GLES3.0")); - return false; - } - - if (!ValidateRobustEntryPoint(context, bufSize)) - { - return false; - } - - if (!ValidateIndexedStateQuery(context, target, index, length)) - { - return false; - } - - if (!ValidateRobustBufferSize(context, bufSize, *length)) - { - return false; - } - - return true; -} - -bool ValidateCopyBufferSubData(ValidationContext *context, - GLenum readTarget, - GLenum writeTarget, - GLintptr readOffset, - GLintptr writeOffset, - GLsizeiptr size) -{ - if (context->getClientMajorVersion() < 3) - { - context->handleError( - Error(GL_INVALID_OPERATION, "CopyBufferSubData requires ES 3 or greater")); - return false; - } - - if (!ValidBufferTarget(context, readTarget) || !ValidBufferTarget(context, writeTarget)) - { - context->handleError(Error(GL_INVALID_ENUM, "Invalid buffer target")); - return false; - } - - Buffer *readBuffer = context->getGLState().getTargetBuffer(readTarget); - Buffer *writeBuffer = context->getGLState().getTargetBuffer(writeTarget); - - if (!readBuffer || !writeBuffer) - { - context->handleError(Error(GL_INVALID_OPERATION, "No buffer bound to target")); - return false; - } - - // Verify that readBuffer and writeBuffer are not currently mapped - if (readBuffer->isMapped() || writeBuffer->isMapped()) - { - context->handleError( - Error(GL_INVALID_OPERATION, "Cannot call CopyBufferSubData on a mapped buffer")); - return false; - } - - CheckedNumeric checkedReadOffset(readOffset); - CheckedNumeric checkedWriteOffset(writeOffset); - CheckedNumeric checkedSize(size); - - auto checkedReadSum = checkedReadOffset + checkedSize; - auto checkedWriteSum = checkedWriteOffset + checkedSize; - - if (!checkedReadSum.IsValid() || !checkedWriteSum.IsValid() || - !IsValueInRangeForNumericType(readBuffer->getSize()) || - !IsValueInRangeForNumericType(writeBuffer->getSize())) - { - context->handleError( - Error(GL_INVALID_VALUE, "Integer overflow when validating copy offsets.")); - return false; - } - - if (readOffset < 0 || writeOffset < 0 || size < 0) - { - context->handleError( - Error(GL_INVALID_VALUE, "readOffset, writeOffset and size must all be non-negative")); - return false; - } - - if (checkedReadSum.ValueOrDie() > readBuffer->getSize() || - checkedWriteSum.ValueOrDie() > writeBuffer->getSize()) - { - context->handleError( - Error(GL_INVALID_VALUE, "Buffer offset overflow in CopyBufferSubData")); - return false; - } - - if (readBuffer == writeBuffer) - { - auto checkedOffsetDiff = (checkedReadOffset - checkedWriteOffset).Abs(); - if (!checkedOffsetDiff.IsValid()) - { - // This shold not be possible. - UNREACHABLE(); - context->handleError( - Error(GL_INVALID_VALUE, "Integer overflow when validating same buffer copy.")); - return false; - } - - if (checkedOffsetDiff.ValueOrDie() < size) - { - context->handleError(Error(GL_INVALID_VALUE)); - return false; - } - } - - return true; + return ValidateIndexedStateQuery(context, target, index); } } // namespace gl -- cgit v1.2.3