axmol/core/base/Configuration.cpp

420 lines
12 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-04-25 19:15:46 +08:00
Copyright (c) 2020-2021 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.
****************************************************************************/
#include "base/Configuration.h"
#include "platform/FileUtils.h"
#include "base/EventCustom.h"
#include "base/Director.h"
#include "base/EventDispatcher.h"
2019-11-23 20:27:39 +08:00
#include "renderer/backend/Device.h"
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
2022-10-01 16:24:52 +08:00
extern const char* axmolVersion();
2019-11-23 20:27:39 +08:00
Configuration* Configuration::s_sharedConfiguration = nullptr;
const char* Configuration::CONFIG_FILE_LOADED = "config_file_loaded";
Configuration::Configuration()
2021-12-25 10:04:45 +08:00
: _maxModelviewStackDepth(0)
, _supportsPVRTC(false)
, _supportsETC1(false)
, _supportsETC2(false)
, _supportsS3TC(false)
, _supportsATITC(false)
, _supportsASTC(false)
, _supportsNPOT(false)
, _supportsBGRA8888(false)
, _supportsDiscardFramebuffer(false)
, _supportsShareableVAO(false)
, _supportsOESMapBuffer(false)
, _supportsOESDepth24(false)
, _supportsOESPackedDepthStencil(false)
, _maxDirLightInShader(1)
, _maxPointLightInShader(1)
, _maxSpotLightInShader(1)
, _animate3DQuality(Animate3DQuality::QUALITY_LOW)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
_loadedEvent = new EventCustom(CONFIG_FILE_LOADED);
2019-11-23 20:27:39 +08:00
}
bool Configuration::init()
{
2022-10-01 16:24:52 +08:00
_valueDict["axmol.version"] = Value(axmolVersion());
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_PROFILERS
2022-10-01 16:24:52 +08:00
_valueDict["axmol.compiled_with_profiler"] = Value(true);
2019-11-23 20:27:39 +08:00
#else
2022-10-01 16:24:52 +08:00
_valueDict["axmol.compiled_with_profiler"] = Value(false);
2019-11-23 20:27:39 +08:00
#endif
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_GL_STATE_CACHE == 0
2022-10-01 16:24:52 +08:00
_valueDict["axmol.compiled_with_gl_state_cache"] = Value(false);
2019-11-23 20:27:39 +08:00
#else
2022-10-01 16:24:52 +08:00
_valueDict["axmol.compiled_with_gl_state_cache"] = Value(true);
2019-11-23 20:27:39 +08:00
#endif
2022-08-08 18:02:17 +08:00
#if _AX_DEBUG
2022-10-01 16:24:52 +08:00
_valueDict["axmol.build_type"] = Value("DEBUG");
2019-11-23 20:27:39 +08:00
#else
2022-10-01 16:24:52 +08:00
_valueDict["axmol.build_type"] = Value("RELEASE");
2019-11-23 20:27:39 +08:00
#endif
2021-12-25 10:04:45 +08:00
return true;
2019-11-23 20:27:39 +08:00
}
Configuration::~Configuration()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(_loadedEvent);
2019-11-23 20:27:39 +08:00
}
std::string Configuration::getInfo() const
{
2021-12-25 10:04:45 +08:00
// And Dump some warnings as well
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_PROFILERS
AXLOG(
"axmol: **** WARNING **** AX_ENABLE_PROFILERS is defined. Disable it when you finish profiling (from "
2021-12-25 10:04:45 +08:00
"ccConfig.h)\n");
2019-11-23 20:27:39 +08:00
#endif
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_GL_STATE_CACHE == 0
AXLOG(
"axmol: **** WARNING **** AX_ENABLE_GL_STATE_CACHE is disabled. To improve performance, enable it (from "
2021-12-25 10:04:45 +08:00
"ccConfig.h)\n");
2019-11-23 20:27:39 +08:00
#endif
// Dump
Value forDump = Value(_valueDict);
return forDump.getDescription();
}
void Configuration::gatherGPUInfo()
{
auto _deviceInfo = backend::Device::getInstance()->getDeviceInfo();
2023-08-10 18:36:45 +08:00
AXLOG("Supported extensions: %s", _deviceInfo->getExtension());
2021-12-25 10:04:45 +08:00
_valueDict["vendor"] = Value(_deviceInfo->getVendor());
2019-11-23 20:27:39 +08:00
_valueDict["renderer"] = Value(_deviceInfo->getRenderer());
2021-12-25 10:04:45 +08:00
_valueDict["version"] = Value(_deviceInfo->getVersion());
_valueDict["max_texture_size"] = Value(_deviceInfo->getMaxTextureSize());
2019-11-23 20:27:39 +08:00
_valueDict["max_vertex_attributes"] = Value(_deviceInfo->getMaxAttributes());
2021-12-25 10:04:45 +08:00
_valueDict["max_texture_units"] = Value(_deviceInfo->getMaxTextureUnits());
_valueDict["max_samples_allowed"] = Value(_deviceInfo->getMaxSamplesAllowed());
_supportsNPOT = true;
2019-11-23 20:27:39 +08:00
_valueDict["supports_NPOT"] = Value(_supportsNPOT);
2021-12-25 10:04:45 +08:00
_supportsETC1 = _deviceInfo->checkForFeatureSupported(backend::FeatureType::ETC1);
2019-11-23 20:27:39 +08:00
_valueDict["supports_ETC1"] = Value(_supportsETC1);
2020-08-31 21:04:47 +08:00
2021-12-25 10:04:45 +08:00
_supportsETC2 = _deviceInfo->checkForFeatureSupported(backend::FeatureType::ETC2);
2020-08-31 21:04:47 +08:00
_valueDict["supports_ETC2"] = Value(_supportsETC2);
2021-12-25 10:04:45 +08:00
_supportsS3TC = _deviceInfo->checkForFeatureSupported(backend::FeatureType::S3TC);
2019-11-23 20:27:39 +08:00
_valueDict["supports_S3TC"] = Value(_supportsS3TC);
2021-12-25 10:04:45 +08:00
_supportsATITC = _deviceInfo->checkForFeatureSupported(backend::FeatureType::AMD_COMPRESSED_ATC);
2019-11-23 20:27:39 +08:00
_valueDict["supports_ATITC"] = Value(_supportsATITC);
2021-12-25 10:04:45 +08:00
_supportsASTC = _deviceInfo->checkForFeatureSupported(backend::FeatureType::ASTC);
2021-06-10 16:51:30 +08:00
_valueDict["supports_ASTC"] = Value(_supportsASTC);
2021-12-25 10:04:45 +08:00
_supportsPVRTC = _deviceInfo->checkForFeatureSupported(backend::FeatureType::PVRTC);
2019-11-23 20:27:39 +08:00
_valueDict["supports_PVRTC"] = Value(_supportsPVRTC);
2021-12-25 10:04:45 +08:00
_supportsBGRA8888 = _deviceInfo->checkForFeatureSupported(backend::FeatureType::IMG_FORMAT_BGRA8888);
2019-11-23 20:27:39 +08:00
_valueDict["supports_BGRA8888"] = Value(_supportsBGRA8888);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_supportsDiscardFramebuffer = _deviceInfo->checkForFeatureSupported(backend::FeatureType::DISCARD_FRAMEBUFFER);
_valueDict["supports_discard_framebuffer"] = Value(_supportsDiscardFramebuffer);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_supportsOESPackedDepthStencil = _deviceInfo->checkForFeatureSupported(backend::FeatureType::PACKED_DEPTH_STENCIL);
_valueDict["supports_OES_packed_depth_stencil"] = Value(_supportsOESPackedDepthStencil);
2021-12-25 10:04:45 +08:00
_supportsShareableVAO = _deviceInfo->checkForFeatureSupported(backend::FeatureType::VAO);
2019-11-23 20:27:39 +08:00
_valueDict["supports_vertex_array_object"] = Value(_supportsShareableVAO);
2021-12-25 10:04:45 +08:00
_supportsOESMapBuffer = _deviceInfo->checkForFeatureSupported(backend::FeatureType::MAPBUFFER);
2019-11-23 20:27:39 +08:00
_valueDict["supports_OES_map_buffer"] = Value(_supportsOESMapBuffer);
2021-12-25 10:04:45 +08:00
_supportsOESDepth24 = _deviceInfo->checkForFeatureSupported(backend::FeatureType::DEPTH24);
2019-11-23 20:27:39 +08:00
_valueDict["supports_OES_depth24"] = Value(_supportsOESDepth24);
2021-12-25 10:04:45 +08:00
2023-08-10 18:36:45 +08:00
_glExtensions = _deviceInfo->getExtension();
2019-11-23 20:27:39 +08:00
}
Configuration* Configuration::getInstance()
{
2021-12-25 10:04:45 +08:00
if (!s_sharedConfiguration)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
s_sharedConfiguration = new Configuration();
2019-11-23 20:27:39 +08:00
s_sharedConfiguration->init();
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return s_sharedConfiguration;
}
void Configuration::destroyInstance()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE_NULL(s_sharedConfiguration);
2019-11-23 20:27:39 +08:00
}
2023-08-10 18:36:45 +08:00
bool Configuration::checkForGLExtension(std::string_view searchName) const
{
return _glExtensions.find(searchName) != std::string::npos;
}
2019-11-23 20:27:39 +08:00
//
// getters for specific variables.
// Maintained for backward compatibility reasons only.
//
int Configuration::getMaxTextureSize() const
{
auto _deviceInfo = backend::Device::getInstance()->getDeviceInfo();
return _deviceInfo->getMaxTextureSize();
}
int Configuration::getMaxModelviewStackDepth() const
{
2021-12-25 10:04:45 +08:00
return _maxModelviewStackDepth;
2019-11-23 20:27:39 +08:00
}
int Configuration::getMaxTextureUnits() const
{
auto _deviceInfo = backend::Device::getInstance()->getDeviceInfo();
return _deviceInfo->getMaxTextureUnits();
}
bool Configuration::supportsNPOT() const
{
2021-12-25 10:04:45 +08:00
return _supportsNPOT;
2019-11-23 20:27:39 +08:00
}
bool Configuration::supportsPVRTC() const
{
2021-12-25 10:04:45 +08:00
return _supportsPVRTC;
2019-11-23 20:27:39 +08:00
}
bool Configuration::supportsETC1() const
2019-11-23 20:27:39 +08:00
{
return _supportsETC1;
}
2020-08-31 21:04:47 +08:00
bool Configuration::supportsETC2() const
{
return _supportsETC2;
}
2019-11-23 20:27:39 +08:00
bool Configuration::supportsS3TC() const
{
return _supportsS3TC;
}
bool Configuration::supportsATITC() const
{
return _supportsATITC;
}
bool Configuration::supportsASTC() const
{
return _supportsASTC;
}
2019-11-23 20:27:39 +08:00
bool Configuration::supportsBGRA8888() const
{
2021-12-25 10:04:45 +08:00
return _supportsBGRA8888;
2019-11-23 20:27:39 +08:00
}
bool Configuration::supportsDiscardFramebuffer() const
{
2021-12-25 10:04:45 +08:00
return _supportsDiscardFramebuffer;
2019-11-23 20:27:39 +08:00
}
bool Configuration::supportsShareableVAO() const
{
2022-07-16 10:43:05 +08:00
#if AX_TEXTURE_ATLAS_USE_VAO
2019-11-23 20:27:39 +08:00
return _supportsShareableVAO;
#else
return false;
#endif
}
bool Configuration::supportsMapBuffer() const
{
// Fixes Github issue #16123
//
// XXX: Fixme. Should check GL ES and not iOS or Android
// For example, linux could be compiled with GL ES. Or perhaps in the future Android will
// support OpenGL. This is because glMapBufferOES() is an extension of OpenGL ES. And glMapBuffer()
// is always implemented in OpenGL.
// XXX: Warning. On iOS this is always `true`. Avoiding the comparison.
2022-07-16 10:43:05 +08:00
#if defined(AX_USE_GLES) && !defined(__APPLE__)
2019-11-23 20:27:39 +08:00
return _supportsOESMapBuffer;
#else
return true;
#endif
}
bool Configuration::supportsOESDepth24() const
{
return _supportsOESDepth24;
}
bool Configuration::supportsOESPackedDepthStencil() const
{
return _supportsOESPackedDepthStencil;
}
int Configuration::getMaxSupportDirLightInShader() const
{
return _maxDirLightInShader;
}
int Configuration::getMaxSupportPointLightInShader() const
{
return _maxPointLightInShader;
}
int Configuration::getMaxSupportSpotLightInShader() const
{
return _maxSpotLightInShader;
}
Animate3DQuality Configuration::getAnimate3DQuality() const
{
return _animate3DQuality;
}
//
// generic getters for properties
//
const Value& Configuration::getValue(std::string_view key, const Value& defaultValue) const
2019-11-23 20:27:39 +08:00
{
auto iter = _valueDict.find(key);
if (iter != _valueDict.cend())
return iter->second;
return defaultValue;
}
void Configuration::setValue(std::string_view key, const Value& value)
2019-11-23 20:27:39 +08:00
{
hlookup::set_item(_valueDict, key, value); // _valueDict[key] = value;
2019-11-23 20:27:39 +08:00
}
//
// load file
//
void Configuration::loadConfigFile(std::string_view filename)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(filename);
2022-07-16 10:43:05 +08:00
AXASSERT(!dict.empty(), "cannot create dictionary");
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
// search for metadata
bool validMetadata = false;
auto metadataIter = dict.find("metadata");
if (metadataIter != dict.cend() && metadataIter->second.getType() == Value::Type::MAP)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
const auto& metadata = metadataIter->second.asValueMap();
auto formatIter = metadata.find("format");
if (formatIter != metadata.cend())
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
int format = formatIter->second.asInt();
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
// Support format: 1
if (format == 1)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
validMetadata = true;
}
}
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
if (!validMetadata)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXLOG("Invalid config format for file: %s", filename.data());
2021-12-25 10:04:45 +08:00
return;
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
auto dataIter = dict.find("data");
if (dataIter == dict.cend() || dataIter->second.getType() != Value::Type::MAP)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXLOG("Expected 'data' dict, but not found. Config file: %s", filename.data());
2021-12-25 10:04:45 +08:00
return;
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
// Add all keys in the existing dictionary
const auto& dataMap = dataIter->second.asValueMap();
2019-11-23 20:27:39 +08:00
for (const auto& dataMapIter : dataMap)
{
if (_valueDict.find(dataMapIter.first) == _valueDict.cend())
_valueDict[dataMapIter.first] = dataMapIter.second;
else
2022-07-16 10:43:05 +08:00
AXLOG("Key already present. Ignoring '%s'", dataMapIter.first.c_str());
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
// light info
2022-10-01 16:24:52 +08:00
std::string name = "axmol.3d.max_dir_light_in_shader";
2021-12-25 10:04:45 +08:00
if (_valueDict.find(name) != _valueDict.end())
2020-08-18 11:33:18 +08:00
_maxDirLightInShader = _valueDict[name].asInt();
2019-11-23 20:27:39 +08:00
else
_valueDict[name] = Value(_maxDirLightInShader);
2021-12-25 10:04:45 +08:00
2022-10-01 16:24:52 +08:00
name = "axmol.3d.max_point_light_in_shader";
2021-12-25 10:04:45 +08:00
if (_valueDict.find(name) != _valueDict.end())
2020-08-18 11:33:18 +08:00
_maxPointLightInShader = _valueDict[name].asInt();
2019-11-23 20:27:39 +08:00
else
_valueDict[name] = Value(_maxPointLightInShader);
2021-12-25 10:04:45 +08:00
2022-10-01 16:24:52 +08:00
name = "axmol.3d.max_spot_light_in_shader";
2021-12-25 10:04:45 +08:00
if (_valueDict.find(name) != _valueDict.end())
2020-08-18 11:33:18 +08:00
_maxSpotLightInShader = _valueDict[name].asInt();
2019-11-23 20:27:39 +08:00
else
_valueDict[name] = Value(_maxSpotLightInShader);
2021-12-25 10:04:45 +08:00
2022-10-01 16:24:52 +08:00
name = "axmol.3d.animate_quality";
2019-11-23 20:27:39 +08:00
if (_valueDict.find(name) != _valueDict.end())
2020-08-18 11:33:18 +08:00
_animate3DQuality = (Animate3DQuality)_valueDict[name].asInt();
2019-11-23 20:27:39 +08:00
else
_valueDict[name] = Value((int)_animate3DQuality);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Director::getInstance()->getEventDispatcher()->dispatchEvent(_loadedEvent);
}
int Configuration::getMaxAttributes() const
{
auto _deviceInfo = backend::Device::getInstance()->getDeviceInfo();
return _deviceInfo->getMaxAttributes();
}
NS_AX_END