axmol/core/renderer/backend/opengl/DeviceInfoGL.cpp

208 lines
6.8 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
2022-04-25 19:15:46 +08:00
Copyright (c) 2020 C4games Ltd
2019-11-23 20:27:39 +08:00
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
2019-11-23 20:27:39 +08:00
#include "DeviceInfoGL.h"
#include "platform/GL.h"
2019-11-23 20:27:39 +08:00
2020-08-31 21:04:47 +08:00
#if !defined(GL_COMPRESSED_RGBA8_ETC2_EAC)
2021-12-25 10:04:45 +08:00
# define GL_COMPRESSED_RGBA8_ETC2_EAC 0x9278
2023-08-31 18:14:26 +08:00
// #define GL_COMPRESSED_RGB8_ETC2 0x9274
2020-08-31 21:04:47 +08:00
#endif
#if !defined(GL_COMPRESSED_RGBA_ASTC_4x4)
2021-12-25 10:04:45 +08:00
# define GL_COMPRESSED_RGBA_ASTC_4x4 0x93B0
2023-08-31 18:14:26 +08:00
// #define GL_COMPRESSED_RGBA_ASTC_8x8 0x93B7
2020-08-31 21:04:47 +08:00
#endif
NS_AX_BACKEND_BEGIN
2019-11-23 20:27:39 +08:00
/// <summary>
2023-08-31 18:14:26 +08:00
/// 1px method to detect whether GPU support astc compressed texture really
/// </summary>
/// <returns>true: support, false: not support</returns>
2023-08-31 18:14:26 +08:00
static bool checkReallySupportsASTC()
2021-12-25 10:04:45 +08:00
{
2023-08-31 18:14:26 +08:00
const GLsizei TEXTURE_DIM = 1;
// 1px/2px astc 4x4 compressed texels srgb
2021-12-25 10:04:45 +08:00
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);
2023-08-31 18:14:26 +08:00
2023-08-15 18:02:02 +08:00
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2023-08-31 18:14:26 +08:00
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,
2021-12-25 10:04:45 +08:00
0, // level
GL_COMPRESSED_RGBA_ASTC_4x4_KHR, // format
2023-08-31 18:14:26 +08:00
TEXTURE_DIM, TEXTURE_DIM,
0, // border
sizeof(astctexels), // dataLen,
2021-12-25 10:04:45 +08:00
astctexels);
2023-08-31 18:14:26 +08:00
auto error = glGetError();
#if !defined(AX_USE_GLES)
if (!error && glGetTexImage)
2021-12-25 10:04:45 +08:00
{
2023-08-31 18:14:26 +08:00
// 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;
}
}
2023-08-31 18:14:26 +08:00
#endif
2023-08-31 18:14:26 +08:00
glBindTexture(GL_TEXTURE_2D, 0); // unbind texture
glDeleteTextures(1, &texID);
2023-08-31 18:14:26 +08:00
return !error;
}
2019-11-23 20:27:39 +08:00
bool DeviceInfoGL::init()
{
glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &_maxAttributes);
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &_maxTextureSize);
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &_maxTextureUnits);
2023-08-31 18:14:26 +08:00
_glExtensions = (const char*)glGetString(GL_EXTENSIONS);
2019-11-23 20:27:39 +08:00
return true;
}
const char* DeviceInfoGL::getVendor() const
{
2023-08-31 18:14:26 +08:00
return (const char*)glGetString(GL_VENDOR);
2019-11-23 20:27:39 +08:00
}
const char* DeviceInfoGL::getRenderer() const
{
2023-08-31 18:14:26 +08:00
return (const char*)glGetString(GL_RENDERER);
2019-11-23 20:27:39 +08:00
}
const char* DeviceInfoGL::getVersion() const
{
2023-08-31 18:14:26 +08:00
return (const char*)glGetString(GL_VERSION);
2023-08-13 23:58:18 +08:00
}
2023-08-31 18:14:26 +08:00
const char* DeviceInfoGL::getExtension() const
2023-08-13 23:58:18 +08:00
{
2023-08-31 18:14:26 +08:00
return _glExtensions.c_str();
2019-11-23 20:27:39 +08:00
}
bool DeviceInfoGL::checkForFeatureSupported(FeatureType feature)
{
bool featureSupported = false;
switch (feature)
{
case FeatureType::ETC1:
2023-08-31 18:14:26 +08:00
featureSupported = checkForGLExtension("GL_OES_compressed_ETC1_RGB8_texture");
2019-11-23 20:27:39 +08:00
break;
2020-08-31 21:04:47 +08:00
case FeatureType::ETC2:
2023-08-31 18:14:26 +08:00
featureSupported = checkSupportsCompressedFormat(GL_COMPRESSED_RGBA8_ETC2_EAC);
2020-08-31 21:04:47 +08:00
break;
2019-11-23 20:27:39 +08:00
case FeatureType::S3TC:
#ifdef GL_EXT_texture_compression_s3tc
2023-08-31 18:14:26 +08:00
featureSupported = checkForGLExtension("GL_EXT_texture_compression_s3tc");
2019-11-23 20:27:39 +08:00
#endif
break;
case FeatureType::AMD_COMPRESSED_ATC:
2023-08-31 18:14:26 +08:00
featureSupported = checkForGLExtension("GL_AMD_compressed_ATC_texture");
2019-11-23 20:27:39 +08:00
break;
case FeatureType::PVRTC:
2023-08-31 18:14:26 +08:00
featureSupported = checkForGLExtension("GL_IMG_texture_compression_pvrtc");
2019-11-23 20:27:39 +08:00
break;
case FeatureType::IMG_FORMAT_BGRA8888:
2023-08-31 18:14:26 +08:00
featureSupported = checkForGLExtension("GL_IMG_texture_format_BGRA8888");
2019-11-23 20:27:39 +08:00
break;
case FeatureType::DISCARD_FRAMEBUFFER:
2023-08-31 18:14:26 +08:00
featureSupported = checkForGLExtension("GL_EXT_discard_framebuffer");
2019-11-23 20:27:39 +08:00
break;
case FeatureType::PACKED_DEPTH_STENCIL:
2023-08-31 18:14:26 +08:00
featureSupported = checkForGLExtension("GL_OES_packed_depth_stencil");
2019-11-23 20:27:39 +08:00
break;
case FeatureType::VAO:
2022-07-16 10:43:05 +08:00
#ifdef AX_PLATFORM_PC
2023-08-31 18:14:26 +08:00
featureSupported = checkForGLExtension("vertex_array_object");
2019-11-23 20:27:39 +08:00
#else
2023-08-31 18:14:26 +08:00
featureSupported = checkForGLExtension("GL_OES_vertex_array_object");
2019-11-23 20:27:39 +08:00
#endif
break;
case FeatureType::MAPBUFFER:
2023-08-31 18:14:26 +08:00
featureSupported = checkForGLExtension("GL_OES_mapbuffer");
2019-11-23 20:27:39 +08:00
break;
case FeatureType::DEPTH24:
2023-08-31 18:14:26 +08:00
featureSupported = checkForGLExtension("GL_OES_depth24");
2019-11-23 20:27:39 +08:00
break;
case FeatureType::ASTC:
2023-08-31 18:14:26 +08:00
featureSupported = checkReallySupportsASTC();
break;
2019-11-23 20:27:39 +08:00
default:
break;
}
return featureSupported;
}
2023-08-31 18:14:26 +08:00
bool DeviceInfoGL::checkForGLExtension(std::string_view searchName) const
2019-11-23 20:27:39 +08:00
{
2023-08-31 18:14:26 +08:00
return _glExtensions.find(searchName) != std::string::npos;
}
2023-08-31 18:14:26 +08:00
bool DeviceInfoGL::checkSupportsCompressedFormat(int compressedFormat)
{
2023-08-31 18:14:26 +08:00
const int MAX_ALLOCA_SIZE = 512;
2019-11-23 20:27:39 +08:00
2023-08-31 18:14:26 +08:00
GLint numFormats = 0;
glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
GLint* formats = nullptr;
int buffersize = numFormats * sizeof(GLint);
if (buffersize <= MAX_ALLOCA_SIZE)
formats = (GLint*)alloca(buffersize);
else
formats = (GLint*)malloc(buffersize);
glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, formats);
2020-08-31 21:04:47 +08:00
2023-08-31 18:14:26 +08:00
bool supported = false;
for (GLint i = 0; i < numFormats; ++i)
{
if (formats[i] == compressedFormat)
{
supported = true;
break;
}
}
if (buffersize > MAX_ALLOCA_SIZE)
free(formats);
return supported;
2020-08-31 21:04:47 +08:00
}
NS_AX_BACKEND_END