1
0

CCMaterial.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /****************************************************************************
  2. Copyright (c) 2015-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. Ideas taken from:
  20. - GamePlay3D: http://gameplay3d.org/
  21. - OGRE3D: http://www.ogre3d.org/
  22. - Qt3D: http://qt-project.org/
  23. ****************************************************************************/
  24. #include "renderer/CCMaterial.h"
  25. #include "renderer/CCTechnique.h"
  26. #include "renderer/CCPass.h"
  27. #include "renderer/CCTextureCache.h"
  28. #include "renderer/CCTexture2D.h"
  29. #include "renderer/CCGLProgram.h"
  30. #include "renderer/CCGLProgramState.h"
  31. #include "base/CCProperties.h"
  32. #include "base/CCDirector.h"
  33. #include "platform/CCFileUtils.h"
  34. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  35. #define strcasecmp _stricmp
  36. #endif
  37. NS_CC_BEGIN
  38. // Helpers declaration
  39. static const char* getOptionalString(Properties* properties, const char* key, const char* defaultValue);
  40. static bool isValidUniform(const char* name);
  41. Material* Material::createWithFilename(const std::string& filepath)
  42. {
  43. auto validfilename = FileUtils::getInstance()->fullPathForFilename(filepath);
  44. if (validfilename.size() > 0) {
  45. auto mat = new (std::nothrow) Material();
  46. if (mat && mat->initWithFile(validfilename))
  47. {
  48. mat->autorelease();
  49. return mat;
  50. }
  51. }
  52. return nullptr;
  53. }
  54. Material* Material::createWithProperties(Properties* materialProperties)
  55. {
  56. auto mat = new (std::nothrow) Material();
  57. if (mat && mat->initWithProperties(materialProperties))
  58. {
  59. mat->autorelease();
  60. return mat;
  61. }
  62. return nullptr;
  63. }
  64. Material* Material::createWithGLStateProgram(GLProgramState* programState)
  65. {
  66. CCASSERT(programState, "Invalid GL Program State");
  67. auto mat = new (std::nothrow) Material();
  68. if (mat && mat->initWithGLProgramState(programState))
  69. {
  70. mat->autorelease();
  71. return mat;
  72. }
  73. return nullptr;
  74. }
  75. bool Material::initWithGLProgramState(cocos2d::GLProgramState *state)
  76. {
  77. auto technique = Technique::createWithGLProgramState(this, state);
  78. if (technique) {
  79. _techniques.pushBack(technique);
  80. // weak pointer
  81. _currentTechnique = technique;
  82. return true;
  83. }
  84. return false;
  85. }
  86. bool Material::initWithFile(const std::string& validfilename)
  87. {
  88. // Warning: properties is not a "Ref" object, must be manually deleted
  89. Properties* properties = Properties::createNonRefCounted(validfilename);
  90. // get the first material
  91. parseProperties((strlen(properties->getNamespace()) > 0) ? properties : properties->getNextNamespace());
  92. CC_SAFE_DELETE(properties);
  93. return true;
  94. }
  95. bool Material::initWithProperties(Properties* materialProperties)
  96. {
  97. return parseProperties(materialProperties);
  98. }
  99. void Material::setTarget(cocos2d::Node *target)
  100. {
  101. _target = target;
  102. }
  103. bool Material::parseProperties(Properties* materialProperties)
  104. {
  105. setName(materialProperties->getId());
  106. auto space = materialProperties->getNextNamespace();
  107. while (space)
  108. {
  109. const char* name = space->getNamespace();
  110. if (strcmp(name, "technique") == 0)
  111. {
  112. parseTechnique(space);
  113. }
  114. else if (strcmp(name, "renderState") == 0)
  115. {
  116. parseRenderState(this, space);
  117. }
  118. space = materialProperties->getNextNamespace();
  119. }
  120. return true;
  121. }
  122. bool Material::parseTechnique(Properties* techniqueProperties)
  123. {
  124. auto technique = Technique::create(this);
  125. _techniques.pushBack(technique);
  126. // first one is the default one
  127. if (!_currentTechnique)
  128. _currentTechnique = technique;
  129. // name
  130. technique->setName(techniqueProperties->getId());
  131. // passes
  132. auto space = techniqueProperties->getNextNamespace();
  133. while (space)
  134. {
  135. const char* name = space->getNamespace();
  136. if (strcmp(name, "pass") == 0)
  137. {
  138. parsePass(technique, space);
  139. }
  140. else if (strcmp(name, "renderState") == 0)
  141. {
  142. parseRenderState(this, space);
  143. }
  144. space = techniqueProperties->getNextNamespace();
  145. }
  146. return true;
  147. }
  148. bool Material::parsePass(Technique* technique, Properties* passProperties)
  149. {
  150. auto pass = Pass::create(technique);
  151. technique->addPass(pass);
  152. // Pass can have 3 different namespaces:
  153. // - one or more "sampler"
  154. // - one "renderState"
  155. // - one "shader"
  156. auto space = passProperties->getNextNamespace();
  157. while (space)
  158. {
  159. const char* name = space->getNamespace();
  160. if (strcmp(name, "shader") == 0)
  161. parseShader(pass, space);
  162. else if (strcmp(name, "renderState") == 0)
  163. parseRenderState(pass, space);
  164. else {
  165. CCASSERT(false, "Invalid namespace");
  166. return false;
  167. }
  168. space = passProperties->getNextNamespace();
  169. }
  170. return true;
  171. }
  172. // cocos2d-x doesn't support Samplers yet. But will be added soon
  173. bool Material::parseSampler(GLProgramState* glProgramState, Properties* samplerProperties)
  174. {
  175. CCASSERT(samplerProperties->getId(), "Sampler must have an id. The id is the uniform name");
  176. // required
  177. auto filename = samplerProperties->getString("path");
  178. auto texture = Director::getInstance()->getTextureCache()->addImage(filename);
  179. if (!texture) {
  180. CCLOG("Invalid filepath");
  181. return false;
  182. }
  183. // optionals
  184. {
  185. Texture2D::TexParams texParams;
  186. // mipmap
  187. bool usemipmap = false;
  188. const char* mipmap = getOptionalString(samplerProperties, "mipmap", "false");
  189. if (mipmap && strcasecmp(mipmap, "true")==0) {
  190. texture->generateMipmap();
  191. usemipmap = true;
  192. }
  193. // valid options: REPEAT, CLAMP
  194. const char* wrapS = getOptionalString(samplerProperties, "wrapS", "CLAMP_TO_EDGE");
  195. if (strcasecmp(wrapS, "REPEAT")==0)
  196. texParams.wrapS = GL_REPEAT;
  197. else if(strcasecmp(wrapS, "CLAMP_TO_EDGE")==0)
  198. texParams.wrapS = GL_CLAMP_TO_EDGE;
  199. else
  200. CCLOG("Invalid wrapS: %s", wrapS);
  201. // valid options: REPEAT, CLAMP
  202. const char* wrapT = getOptionalString(samplerProperties, "wrapT", "CLAMP_TO_EDGE");
  203. if (strcasecmp(wrapT, "REPEAT")==0)
  204. texParams.wrapT = GL_REPEAT;
  205. else if(strcasecmp(wrapT, "CLAMP_TO_EDGE")==0)
  206. texParams.wrapT = GL_CLAMP_TO_EDGE;
  207. else
  208. CCLOG("Invalid wrapT: %s", wrapT);
  209. // valid options: NEAREST, LINEAR, NEAREST_MIPMAP_NEAREST, LINEAR_MIPMAP_NEAREST, NEAREST_MIPMAP_LINEAR, LINEAR_MIPMAP_LINEAR
  210. const char* minFilter = getOptionalString(samplerProperties, "minFilter", usemipmap ? "LINEAR_MIPMAP_NEAREST" : "LINEAR");
  211. if (strcasecmp(minFilter, "NEAREST")==0)
  212. texParams.minFilter = GL_NEAREST;
  213. else if(strcasecmp(minFilter, "LINEAR")==0)
  214. texParams.minFilter = GL_LINEAR;
  215. else if(strcasecmp(minFilter, "NEAREST_MIPMAP_NEAREST")==0)
  216. texParams.minFilter = GL_NEAREST_MIPMAP_NEAREST;
  217. else if(strcasecmp(minFilter, "LINEAR_MIPMAP_NEAREST")==0)
  218. texParams.minFilter = GL_LINEAR_MIPMAP_NEAREST;
  219. else if(strcasecmp(minFilter, "NEAREST_MIPMAP_LINEAR")==0)
  220. texParams.minFilter = GL_NEAREST_MIPMAP_LINEAR;
  221. else if(strcasecmp(minFilter, "LINEAR_MIPMAP_LINEAR")==0)
  222. texParams.minFilter = GL_LINEAR_MIPMAP_LINEAR;
  223. else
  224. CCLOG("Invalid minFilter: %s", minFilter);
  225. // valid options: NEAREST, LINEAR
  226. const char* magFilter = getOptionalString(samplerProperties, "magFilter", "LINEAR");
  227. if (strcasecmp(magFilter, "NEAREST")==0)
  228. texParams.magFilter = GL_NEAREST;
  229. else if(strcasecmp(magFilter, "LINEAR")==0)
  230. texParams.magFilter = GL_LINEAR;
  231. else
  232. CCLOG("Invalid magFilter: %s", magFilter);
  233. texture->setTexParameters(texParams);
  234. }
  235. glProgramState->setUniformTexture(samplerProperties->getId(), texture);
  236. return true;
  237. }
  238. bool Material::parseShader(Pass* pass, Properties* shaderProperties)
  239. {
  240. // vertexShader
  241. const char* vertShader = getOptionalString(shaderProperties, "vertexShader", nullptr);
  242. // fragmentShader
  243. const char* fragShader = getOptionalString(shaderProperties, "fragmentShader", nullptr);
  244. // compileTimeDefines
  245. const char* compileTimeDefines = getOptionalString(shaderProperties, "defines", "");
  246. if (vertShader && fragShader)
  247. {
  248. auto glProgramState = GLProgramState::getOrCreateWithShaders(vertShader, fragShader, compileTimeDefines);
  249. pass->setGLProgramState(glProgramState);
  250. // Parse uniforms only if the GLProgramState was created
  251. auto property = shaderProperties->getNextProperty();
  252. while (property)
  253. {
  254. if (isValidUniform(property))
  255. {
  256. parseUniform(glProgramState, shaderProperties, property);
  257. }
  258. property = shaderProperties->getNextProperty();
  259. }
  260. auto space = shaderProperties->getNextNamespace();
  261. while (space)
  262. {
  263. const char* name = space->getNamespace();
  264. if (strcmp(name, "sampler") == 0)
  265. {
  266. parseSampler(glProgramState, space);
  267. }
  268. space = shaderProperties->getNextNamespace();
  269. }
  270. }
  271. return true;
  272. }
  273. bool Material::parseUniform(GLProgramState* programState, Properties* properties, const char* uniformName)
  274. {
  275. bool ret = true;
  276. auto type = properties->getType(uniformName);
  277. switch (type) {
  278. case Properties::Type::NUMBER:
  279. {
  280. auto f = properties->getFloat(uniformName);
  281. programState->setUniformFloat(uniformName, f);
  282. break;
  283. }
  284. case Properties::Type::VECTOR2:
  285. {
  286. Vec2 v2;
  287. properties->getVec2(uniformName, &v2);
  288. programState->setUniformVec2(uniformName, v2);
  289. break;
  290. }
  291. case Properties::Type::VECTOR3:
  292. {
  293. Vec3 v3;
  294. properties->getVec3(uniformName, &v3);
  295. programState->setUniformVec3(uniformName, v3);
  296. break;
  297. }
  298. case Properties::Type::VECTOR4:
  299. {
  300. Vec4 v4;
  301. properties->getVec4(uniformName, &v4);
  302. programState->setUniformVec4(uniformName, v4);
  303. break;
  304. }
  305. case Properties::Type::MATRIX:
  306. {
  307. Mat4 m4;
  308. properties->getMat4(uniformName, &m4);
  309. programState->setUniformMat4(uniformName, m4);
  310. break;
  311. }
  312. case Properties::Type::STRING:
  313. default:
  314. {
  315. // Assume this is a parameter auto-binding.
  316. programState->setParameterAutoBinding(uniformName, properties->getString());
  317. break;
  318. }
  319. }
  320. return ret;
  321. }
  322. bool Material::parseRenderState(RenderState* renderState, Properties* properties)
  323. {
  324. auto state = renderState->getStateBlock();
  325. auto property = properties->getNextProperty();
  326. while (property)
  327. {
  328. // Parse uniforms only if the GLProgramState was created
  329. // Render state only can have "strings" or numbers as values. No new namespaces
  330. state->setState(property, properties->getString(property));
  331. property = properties->getNextProperty();
  332. }
  333. return true;
  334. }
  335. void Material::setName(const std::string&name)
  336. {
  337. _name = name;
  338. }
  339. std::string Material::getName() const
  340. {
  341. return _name;
  342. }
  343. Material::Material()
  344. : _name("")
  345. , _currentTechnique(nullptr)
  346. , _target(nullptr)
  347. {
  348. }
  349. Material::~Material()
  350. {
  351. }
  352. Material* Material::clone() const
  353. {
  354. auto material = new (std::nothrow) Material();
  355. if (material)
  356. {
  357. RenderState::cloneInto(material);
  358. for (const auto& technique: _techniques)
  359. {
  360. auto t = technique->clone();
  361. t->_parent = material;
  362. material->_techniques.pushBack(t);
  363. }
  364. // current technique
  365. auto name = _currentTechnique->getName();
  366. material->_currentTechnique = material->getTechniqueByName(name);
  367. material->autorelease();
  368. }
  369. return material;
  370. }
  371. Technique* Material::getTechnique() const
  372. {
  373. return _currentTechnique;
  374. }
  375. const Vector<Technique*>& Material::getTechniques() const
  376. {
  377. return _techniques;
  378. }
  379. Technique* Material::getTechniqueByName(const std::string& name)
  380. {
  381. for(const auto& technique : _techniques) {
  382. if (technique->getName().compare(name)==0)
  383. return technique;
  384. }
  385. return nullptr;
  386. }
  387. Technique* Material::getTechniqueByIndex(ssize_t index)
  388. {
  389. CC_ASSERT(index>=0 && index<_techniques.size() && "Invalid size");
  390. return _techniques.at(index);
  391. }
  392. void Material::addTechnique(Technique* technique)
  393. {
  394. _techniques.pushBack(technique);
  395. }
  396. void Material::setTechnique(const std::string& techniqueName)
  397. {
  398. auto technique = getTechniqueByName(techniqueName);
  399. if (technique)
  400. _currentTechnique = technique;
  401. }
  402. ssize_t Material::getTechniqueCount() const
  403. {
  404. return _techniques.size();
  405. }
  406. // Helpers implementation
  407. static bool isValidUniform(const char* name)
  408. {
  409. return !(strcmp(name, "defines")==0 ||
  410. strcmp(name, "vertexShader")==0 ||
  411. strcmp(name, "fragmentShader")==0);
  412. }
  413. static const char* getOptionalString(Properties* properties, const char* key, const char* defaultValue)
  414. {
  415. const char* ret = properties->getString(key);
  416. if (!ret)
  417. ret = defaultValue;
  418. return ret;
  419. }
  420. NS_CC_END