CCConfiguration.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /****************************************************************************
  2. Copyright (c) 2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2016 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "base/CCConfiguration.h"
  23. #include "platform/CCFileUtils.h"
  24. #include "base/CCLog.h"
  25. #include "base/etc2.h"
  26. #include <regex>
  27. NS_CC_BEGIN
  28. //cjh extern const char* cocos2dVersion();
  29. Configuration* Configuration::s_sharedConfiguration = nullptr;
  30. const char* Configuration::CONFIG_FILE_LOADED = "config_file_loaded";
  31. Configuration::Configuration()
  32. : _maxTextureSize(0)
  33. , _maxModelviewStackDepth(0)
  34. , _supportsPVRTC(false)
  35. , _supportsETC1(false)
  36. , _supportsETC2(false)
  37. , _supportsS3TC(false)
  38. , _supportsATITC(false)
  39. , _supportsNPOT(false)
  40. , _supportsBGRA8888(false)
  41. , _supportsDiscardFramebuffer(false)
  42. , _supportsShareableVAO(false)
  43. , _supportsOESDepth24(false)
  44. , _supportsOESPackedDepthStencil(false)
  45. , _supportsOESMapBuffer(false)
  46. , _supportsFloatTexture(false)
  47. , _isOpenglES3(false)
  48. , _maxSamplesAllowed(0)
  49. , _maxTextureUnits(0)
  50. , _glExtensions(nullptr)
  51. , _maxDirLightInShader(1)
  52. , _maxPointLightInShader(1)
  53. , _maxSpotLightInShader(1)
  54. {
  55. }
  56. bool Configuration::init()
  57. {
  58. gatherGPUInfo();
  59. #if CC_ENABLE_PROFILERS
  60. _valueDict["compiled_with_profiler"] = Value(true);
  61. #else
  62. _valueDict["compiled_with_profiler"] = Value(false);
  63. #endif
  64. #if CC_ENABLE_GL_STATE_CACHE == 0
  65. _valueDict["compiled_with_gl_state_cache"] = Value(false);
  66. #else
  67. _valueDict["compiled_with_gl_state_cache"] = Value(true);
  68. #endif
  69. #if COCOS2D_DEBUG
  70. _valueDict["build_type"] = Value("DEBUG");
  71. #else
  72. _valueDict["build_type"] = Value("RELEASE");
  73. #endif
  74. return true;
  75. }
  76. Configuration::~Configuration()
  77. {
  78. }
  79. std::string Configuration::getInfo() const
  80. {
  81. // And Dump some warnings as well
  82. #if CC_ENABLE_PROFILERS
  83. CCLOG("**** WARNING **** CC_ENABLE_PROFILERS is defined. Disable it when you finish profiling (from ccConfig.h)\n");
  84. #endif
  85. #if CC_ENABLE_GL_STATE_CACHE == 0
  86. CCLOG("**** WARNING **** CC_ENABLE_GL_STATE_CACHE is disabled. To improve performance, enable it (from ccConfig.h)\n");
  87. #endif
  88. // Dump
  89. Value forDump = Value(_valueDict);
  90. return forDump.getDescription();
  91. }
  92. void Configuration::gatherGPUInfo()
  93. {
  94. _valueDict["gl.vendor"] = Value((const char*)glGetString(GL_VENDOR));
  95. _valueDict["gl.renderer"] = Value((const char*)glGetString(GL_RENDERER));
  96. const char* version = (const char*)glGetString(GL_VERSION);
  97. _valueDict["gl.version"] = Value(version);
  98. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  99. if (std::regex_match(version, std::regex("OpenGL ES 3.*"))) {
  100. _isOpenglES3 = true;
  101. }
  102. #endif
  103. _glExtensions = (char *)glGetString(GL_EXTENSIONS);
  104. _supportsETC2 = checkForETC2();
  105. _valueDict["gl.supports_ETC2"] = Value(_supportsETC2);
  106. glGetIntegerv(GL_MAX_TEXTURE_SIZE, &_maxTextureSize);
  107. _valueDict["gl.max_texture_size"] = Value((int)_maxTextureSize);
  108. glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &_maxTextureUnits);
  109. _valueDict["gl.max_texture_units"] = Value((int)_maxTextureUnits);
  110. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  111. glGetIntegerv(GL_MAX_SAMPLES_APPLE, &_maxSamplesAllowed);
  112. _valueDict["gl.max_samples_allowed"] = Value((int)_maxSamplesAllowed);
  113. #endif
  114. _supportsETC1 = checkForGLExtension("GL_OES_compressed_ETC1_RGB8_texture");
  115. _valueDict["gl.supports_ETC1"] = Value(_supportsETC1);
  116. _supportsS3TC = checkForGLExtension("GL_EXT_texture_compression_s3tc");
  117. _valueDict["gl.supports_S3TC"] = Value(_supportsS3TC);
  118. _supportsATITC = checkForGLExtension("GL_AMD_compressed_ATC_texture");
  119. _valueDict["gl.supports_ATITC"] = Value(_supportsATITC);
  120. _supportsPVRTC = checkForGLExtension("GL_IMG_texture_compression_pvrtc");
  121. _valueDict["gl.supports_PVRTC"] = Value(_supportsPVRTC);
  122. _supportsNPOT = true;
  123. _valueDict["gl.supports_NPOT"] = Value(_supportsNPOT);
  124. _supportsBGRA8888 = checkForGLExtension("GL_IMG_texture_format_BGRA888");
  125. _valueDict["gl.supports_BGRA8888"] = Value(_supportsBGRA8888);
  126. _supportsDiscardFramebuffer = checkForGLExtension("GL_EXT_discard_framebuffer");
  127. _valueDict["gl.supports_discard_framebuffer"] = Value(_supportsDiscardFramebuffer);
  128. _supportsOESMapBuffer = checkForGLExtension("GL_OES_mapbuffer");
  129. _valueDict["gl.supports_OES_map_buffer"] = Value(_supportsOESMapBuffer);
  130. _supportsOESDepth24 = checkForGLExtension("GL_OES_depth24");
  131. _valueDict["gl.supports_OES_depth24"] = Value(_supportsOESDepth24);
  132. _supportsOESPackedDepthStencil = checkForGLExtension("GL_OES_packed_depth_stencil");
  133. _valueDict["gl.supports_OES_packed_depth_stencil"] = Value(_supportsOESPackedDepthStencil);
  134. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
  135. _supportsStandardDerivatives = true;
  136. #else
  137. if (_isOpenglES3) {
  138. _supportsStandardDerivatives = true;
  139. }
  140. else {
  141. _supportsStandardDerivatives = checkForGLExtension("OES_standard_derivatives");
  142. _valueDict["gl.supports_standard_derivatives"] = Value(_supportsStandardDerivatives);
  143. }
  144. #endif
  145. if (_isOpenglES3) {
  146. _supportsFloatTexture = true;
  147. _supportsShareableVAO = true;
  148. }
  149. else {
  150. _supportsFloatTexture = checkForGLExtension("GL_ARB_texture_float");
  151. _valueDict["gl.supports_float_texture"] = Value(_supportsFloatTexture);
  152. _supportsShareableVAO = checkForGLExtension("vertex_array_object");
  153. _valueDict["gl.supports_vertex_array_object"] = Value(_supportsShareableVAO);
  154. }
  155. CHECK_GL_ERROR_DEBUG();
  156. }
  157. Configuration* Configuration::getInstance()
  158. {
  159. if (! s_sharedConfiguration)
  160. {
  161. s_sharedConfiguration = new (std::nothrow) Configuration();
  162. s_sharedConfiguration->init();
  163. }
  164. return s_sharedConfiguration;
  165. }
  166. void Configuration::destroyInstance()
  167. {
  168. CC_SAFE_RELEASE_NULL(s_sharedConfiguration);
  169. }
  170. bool Configuration::checkForGLExtension(const std::string &searchName) const
  171. {
  172. return (_glExtensions && strstr(_glExtensions, searchName.c_str() ) ) ? true : false;
  173. }
  174. //
  175. // getters for specific variables.
  176. // Maintained for backward compatibility reasons only.
  177. //
  178. int Configuration::getMaxTextureSize() const
  179. {
  180. return _maxTextureSize;
  181. }
  182. int Configuration::getMaxModelviewStackDepth() const
  183. {
  184. return _maxModelviewStackDepth;
  185. }
  186. int Configuration::getMaxTextureUnits() const
  187. {
  188. return _maxTextureUnits;
  189. }
  190. bool Configuration::supportsNPOT() const
  191. {
  192. return _supportsNPOT;
  193. }
  194. bool Configuration::supportsPVRTC() const
  195. {
  196. return _supportsPVRTC;
  197. }
  198. bool Configuration::supportsETC() const
  199. {
  200. //GL_ETC1_RGB8_OES is not defined in old opengl version
  201. #ifdef GL_ETC1_RGB8_OES
  202. return _supportsETC1;
  203. #else
  204. return false;
  205. #endif
  206. }
  207. bool Configuration::checkForETC2() const
  208. {
  209. GLint numFormats = 0;
  210. glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
  211. GLint* formats = new GLint[numFormats];
  212. glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, formats);
  213. int supportNum = 0;
  214. for (GLint i = 0; i < numFormats; ++i)
  215. {
  216. if (formats[i] == GL_COMPRESSED_RGB8_ETC2 || formats[i] == GL_COMPRESSED_RGBA8_ETC2_EAC)
  217. supportNum++;
  218. }
  219. delete [] formats;
  220. return supportNum >= 2;
  221. }
  222. bool Configuration::supportsETC2() const
  223. {
  224. return _supportsETC2;
  225. }
  226. bool Configuration::supportsS3TC() const
  227. {
  228. #ifdef GL_EXT_texture_compression_s3tc
  229. return _supportsS3TC;
  230. #else
  231. return false;
  232. #endif
  233. }
  234. bool Configuration::supportsATITC() const
  235. {
  236. return _supportsATITC;
  237. }
  238. bool Configuration::supportsBGRA8888() const
  239. {
  240. return _supportsBGRA8888;
  241. }
  242. bool Configuration::supportsDiscardFramebuffer() const
  243. {
  244. return _supportsDiscardFramebuffer;
  245. }
  246. bool Configuration::supportsShareableVAO() const
  247. {
  248. #if CC_TEXTURE_ATLAS_USE_VAO
  249. return _supportsShareableVAO;
  250. #else
  251. return false;
  252. #endif
  253. }
  254. bool Configuration::supportsMapBuffer() const
  255. {
  256. // Fixes Github issue #16123
  257. //
  258. // XXX: Fixme. Should check GL ES and not iOS or Android
  259. // For example, linux could be compiled with GL ES. Or perhaps in the future Android will
  260. // support OpenGL. This is because glMapBufferOES() is an extension of OpenGL ES. And glMapBuffer()
  261. // is always implemented in OpenGL.
  262. // XXX: Warning. On iOS this is always `true`. Avoiding the comparison.
  263. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  264. return _supportsOESMapBuffer;
  265. #else
  266. return true;
  267. #endif
  268. }
  269. bool Configuration::supportsOESDepth24() const
  270. {
  271. return _supportsOESDepth24;
  272. }
  273. bool Configuration::supportsFloatTexture() const
  274. {
  275. return _supportsFloatTexture;
  276. }
  277. bool Configuration::supportsStandardDerivatives() const
  278. {
  279. return _supportsStandardDerivatives;
  280. }
  281. bool Configuration::supportsOESPackedDepthStencil() const
  282. {
  283. return _supportsOESPackedDepthStencil;
  284. }
  285. int Configuration::getMaxSupportDirLightInShader() const
  286. {
  287. return _maxDirLightInShader;
  288. }
  289. int Configuration::getMaxSupportPointLightInShader() const
  290. {
  291. return _maxPointLightInShader;
  292. }
  293. int Configuration::getMaxSupportSpotLightInShader() const
  294. {
  295. return _maxSpotLightInShader;
  296. }
  297. const Value& Configuration::getValue(const std::string& key, const Value& defaultValue) const
  298. {
  299. auto iter = _valueDict.find(key);
  300. if (iter != _valueDict.cend())
  301. return _valueDict.at(key);
  302. return defaultValue;
  303. }
  304. void Configuration::setValue(const std::string& key, const Value& value)
  305. {
  306. _valueDict[key] = value;
  307. }
  308. void Configuration::loadConfigFile(const std::string& filename)
  309. {
  310. ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(filename);
  311. CCASSERT(!dict.empty(), "cannot create dictionary");
  312. // search for metadata
  313. bool validMetadata = false;
  314. auto metadataIter = dict.find("metadata");
  315. if (metadataIter != dict.cend() && metadataIter->second.getType() == Value::Type::MAP)
  316. {
  317. const auto& metadata = metadataIter->second.asValueMap();
  318. auto formatIter = metadata.find("format");
  319. if (formatIter != metadata.cend())
  320. {
  321. int format = formatIter->second.asInt();
  322. // Support format: 1
  323. if (format == 1)
  324. {
  325. validMetadata = true;
  326. }
  327. }
  328. }
  329. if (! validMetadata)
  330. {
  331. CCLOG("Invalid config format for file: %s", filename.c_str());
  332. return;
  333. }
  334. auto dataIter = dict.find("data");
  335. if (dataIter == dict.cend() || dataIter->second.getType() != Value::Type::MAP)
  336. {
  337. CCLOG("Expected 'data' dict, but not found. Config file: %s", filename.c_str());
  338. return;
  339. }
  340. // Add all keys in the existing dictionary
  341. const auto& dataMap = dataIter->second.asValueMap();
  342. for (auto dataMapIter = dataMap.cbegin(); dataMapIter != dataMap.cend(); ++dataMapIter)
  343. {
  344. if (_valueDict.find(dataMapIter->first) == _valueDict.cend())
  345. _valueDict[dataMapIter->first] = dataMapIter->second;
  346. else
  347. CCLOG("Key already present. Ignoring '%s'",dataMapIter->first.c_str());
  348. }
  349. //cjh Director::getInstance()->getEventDispatcher()->dispatchEvent(_loadedEvent);
  350. }
  351. NS_CC_END