From 98fda4ae1e0d87137e80070b7e288c8f51dade62 Mon Sep 17 00:00:00 2001 From: halx99 Date: Thu, 10 Jun 2021 12:44:34 +0800 Subject: [PATCH] Use 1px detect method for checking whether GPU support astc [ci build] --- .../renderer/backend/opengl/DeviceInfoGL.cpp | 53 +++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/cocos/renderer/backend/opengl/DeviceInfoGL.cpp b/cocos/renderer/backend/opengl/DeviceInfoGL.cpp index 4c976ebe57..64373c4f0e 100644 --- a/cocos/renderer/backend/opengl/DeviceInfoGL.cpp +++ b/cocos/renderer/backend/opengl/DeviceInfoGL.cpp @@ -22,13 +22,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ - + #include "DeviceInfoGL.h" #include "platform/CCGL.h" #if !defined(GL_COMPRESSED_RGBA8_ETC2_EAC) #define GL_COMPRESSED_RGBA8_ETC2_EAC 0x9278 -// #define GL_COMPRESSED_RGB8_ETC2 0x9274 + // #define GL_COMPRESSED_RGB8_ETC2 0x9274 #endif #if !defined(GL_COMPRESSED_RGBA_ASTC_4x4) @@ -38,6 +38,53 @@ CC_BACKEND_BEGIN +/// +/// 1px method to detect whether GPU support astc compressed texture really +/// +/// true: support, false: not support +static bool checkReallySupportsASTC() { + const GLsizei TEXTURE_DIM = 1; + // 1px/2px astc 4x4 compressed texels srgb + uint8_t astctexels[] = { + 0xfc, 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x80, 0x00, 0x00, 0xff, 0xff}; + + GLuint texID = 0; + glGenTextures(1, &texID); + glBindTexture(GL_TEXTURE_2D, texID); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glCompressedTexImage2D(GL_TEXTURE_2D, + 0, // level + GL_COMPRESSED_RGBA_ASTC_4x4_KHR, // format + TEXTURE_DIM, TEXTURE_DIM, + 0, // border + sizeof(astctexels), // dataLen, + astctexels); + + auto error = glGetError(); + if (!error) { + // read pixel RGB: should be: 255, 128, 0 + uint8_t pixels[TEXTURE_DIM * TEXTURE_DIM * 4] = {0}; + glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + error = glGetError(); + if (error || pixels[0] != 255 || pixels[1] != 128) { + error = GL_INVALID_VALUE; + } + } + + glBindTexture(GL_TEXTURE_2D, 0); // ubind texture + glDeleteTextures(1, &texID); + + CHECK_GL_ERROR_DEBUG(); + + return !error; +} + bool DeviceInfoGL::init() { glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &_maxAttributes); @@ -111,7 +158,7 @@ bool DeviceInfoGL::checkForFeatureSupported(FeatureType feature) featureSupported = checkForGLExtension("GL_OES_depth24"); break; case FeatureType::ASTC: - featureSupported = checkForGLExtension("GL_OES_texture_compression_astc") || checkSupportsCompressedFormat(GL_COMPRESSED_RGBA_ASTC_4x4); + featureSupported = checkReallySupportsASTC(); break; default: break;