RenderState.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. #include "Base.h"
  2. #include "RenderState.h"
  3. #include "Node.h"
  4. #include "Pass.h"
  5. #include "Technique.h"
  6. #include "Node.h"
  7. // Render state override bits
  8. #define RS_BLEND 1
  9. #define RS_BLEND_FUNC 2
  10. #define RS_CULL_FACE 4
  11. #define RS_DEPTH_TEST 8
  12. #define RS_DEPTH_WRITE 16
  13. namespace gameplay
  14. {
  15. RenderState::StateBlock* RenderState::StateBlock::_defaultState = NULL;
  16. RenderState::RenderState()
  17. : _nodeBinding(NULL), _state(NULL), _parent(NULL)
  18. {
  19. }
  20. RenderState::~RenderState()
  21. {
  22. SAFE_RELEASE(_state);
  23. // Destroy all the material parameters
  24. for (unsigned int i = 0, count = _parameters.size(); i < count; ++i)
  25. {
  26. SAFE_RELEASE(_parameters[i]);
  27. }
  28. }
  29. void RenderState::initialize()
  30. {
  31. if (StateBlock::_defaultState == NULL)
  32. {
  33. StateBlock::_defaultState = StateBlock::create();
  34. }
  35. }
  36. void RenderState::finalize()
  37. {
  38. SAFE_RELEASE(StateBlock::_defaultState);
  39. }
  40. MaterialParameter* RenderState::getParameter(const char* name) const
  41. {
  42. GP_ASSERT(name);
  43. // Search for an existing parameter with this name.
  44. MaterialParameter* param;
  45. for (unsigned int i = 0, count = _parameters.size(); i < count; ++i)
  46. {
  47. param = _parameters[i];
  48. GP_ASSERT(param);
  49. if (strcmp(param->getName(), name) == 0)
  50. {
  51. return param;
  52. }
  53. }
  54. // Create a new parameter and store it in our list.
  55. param = new MaterialParameter(name);
  56. _parameters.push_back(param);
  57. return param;
  58. }
  59. void RenderState::setParameterAutoBinding(const char* name, AutoBinding autoBinding)
  60. {
  61. GP_ASSERT(name);
  62. // Store the auto-binding.
  63. if (autoBinding == NONE)
  64. {
  65. // Clear current auto binding.
  66. std::map<std::string, AutoBinding>::iterator itr = _autoBindings.find(name);
  67. if (itr != _autoBindings.end())
  68. {
  69. _autoBindings.erase(itr);
  70. }
  71. }
  72. else
  73. {
  74. // Set new auto binding.
  75. _autoBindings[name] = autoBinding;
  76. }
  77. // If we have a currently set node binding, apply the auto binding immediately.
  78. if (_nodeBinding)
  79. {
  80. applyAutoBinding(name, autoBinding);
  81. }
  82. }
  83. void RenderState::setParameterAutoBinding(const char* name, const char* autoBinding)
  84. {
  85. GP_ASSERT(autoBinding);
  86. AutoBinding value = NONE;
  87. // Parse the passed in autoBinding string.
  88. if (strcmp(autoBinding, "WORLD_MATRIX") == 0)
  89. {
  90. value = WORLD_MATRIX;
  91. }
  92. else if (strcmp(autoBinding, "VIEW_MATRIX") == 0)
  93. {
  94. value = VIEW_MATRIX;
  95. }
  96. else if (strcmp(autoBinding, "PROJECTION_MATRIX") == 0)
  97. {
  98. value = PROJECTION_MATRIX;
  99. }
  100. else if (strcmp(autoBinding, "WORLD_VIEW_MATRIX") == 0)
  101. {
  102. value = WORLD_VIEW_MATRIX;
  103. }
  104. else if (strcmp(autoBinding, "VIEW_PROJECTION_MATRIX") == 0)
  105. {
  106. value = VIEW_PROJECTION_MATRIX;
  107. }
  108. else if (strcmp(autoBinding, "WORLD_VIEW_PROJECTION_MATRIX") == 0)
  109. {
  110. value = WORLD_VIEW_PROJECTION_MATRIX;
  111. }
  112. else if (strcmp(autoBinding, "INVERSE_TRANSPOSE_WORLD_MATRIX") == 0)
  113. {
  114. value = INVERSE_TRANSPOSE_WORLD_MATRIX;
  115. }
  116. else if (strcmp(autoBinding, "INVERSE_TRANSPOSE_WORLD_VIEW_MATRIX") == 0)
  117. {
  118. value = INVERSE_TRANSPOSE_WORLD_VIEW_MATRIX;
  119. }
  120. else if (strcmp(autoBinding, "CAMERA_WORLD_POSITION") == 0)
  121. {
  122. value = CAMERA_WORLD_POSITION;
  123. }
  124. else if (strcmp(autoBinding, "CAMERA_VIEW_POSITION") == 0)
  125. {
  126. value = CAMERA_VIEW_POSITION;
  127. }
  128. else if (strcmp(autoBinding, "MATRIX_PALETTE") == 0)
  129. {
  130. value = MATRIX_PALETTE;
  131. }
  132. else
  133. {
  134. // Ignore all other cases (the value was previously set to the default of NONE).
  135. }
  136. if (value != NONE)
  137. {
  138. setParameterAutoBinding(name, value);
  139. }
  140. }
  141. void RenderState::setStateBlock(StateBlock* state)
  142. {
  143. if (_state != state)
  144. {
  145. SAFE_RELEASE(_state);
  146. _state = state;
  147. if (_state)
  148. {
  149. _state->addRef();
  150. }
  151. }
  152. }
  153. RenderState::StateBlock* RenderState::getStateBlock() const
  154. {
  155. if (_state == NULL)
  156. {
  157. _state = StateBlock::create();
  158. }
  159. return _state;
  160. }
  161. void RenderState::setNodeBinding(Node* node)
  162. {
  163. _nodeBinding = node;
  164. if (_nodeBinding)
  165. {
  166. // Apply all existing auto-bindings using this node.
  167. std::map<std::string, AutoBinding>::const_iterator itr = _autoBindings.begin();
  168. while (itr != _autoBindings.end())
  169. {
  170. applyAutoBinding(itr->first.c_str(), itr->second);
  171. itr++;
  172. }
  173. }
  174. }
  175. void RenderState::applyAutoBinding(const char* uniformName, AutoBinding autoBinding)
  176. {
  177. MaterialParameter* param = getParameter(uniformName);
  178. switch (autoBinding)
  179. {
  180. case WORLD_MATRIX:
  181. GP_ASSERT(param);
  182. param->bindValue(_nodeBinding, &Node::getWorldMatrix);
  183. break;
  184. case VIEW_MATRIX:
  185. GP_ASSERT(param);
  186. param->bindValue(_nodeBinding, &Node::getViewMatrix);
  187. break;
  188. case PROJECTION_MATRIX:
  189. GP_ASSERT(param);
  190. param->bindValue(_nodeBinding, &Node::getProjectionMatrix);
  191. break;
  192. case WORLD_VIEW_MATRIX:
  193. GP_ASSERT(param);
  194. param->bindValue(_nodeBinding, &Node::getWorldViewMatrix);
  195. break;
  196. case VIEW_PROJECTION_MATRIX:
  197. GP_ASSERT(param);
  198. param->bindValue(_nodeBinding, &Node::getViewProjectionMatrix);
  199. break;
  200. case WORLD_VIEW_PROJECTION_MATRIX:
  201. GP_ASSERT(param);
  202. param->bindValue(_nodeBinding, &Node::getWorldViewProjectionMatrix);
  203. break;
  204. case INVERSE_TRANSPOSE_WORLD_MATRIX:
  205. GP_ASSERT(param);
  206. param->bindValue(_nodeBinding, &Node::getInverseTransposeWorldMatrix);
  207. break;
  208. case INVERSE_TRANSPOSE_WORLD_VIEW_MATRIX:
  209. GP_ASSERT(param);
  210. param->bindValue(_nodeBinding, &Node::getInverseTransposeWorldViewMatrix);
  211. break;
  212. case CAMERA_WORLD_POSITION:
  213. GP_ASSERT(param);
  214. param->bindValue(_nodeBinding, &Node::getActiveCameraTranslationWorld);
  215. break;
  216. case CAMERA_VIEW_POSITION:
  217. GP_ASSERT(param);
  218. param->bindValue(_nodeBinding, &Node::getActiveCameraTranslationView);
  219. break;
  220. case MATRIX_PALETTE:
  221. {
  222. Model* model = _nodeBinding->getModel();
  223. MeshSkin* skin = model ? model->getSkin() : NULL;
  224. if (skin)
  225. {
  226. GP_ASSERT(param);
  227. param->bindValue(skin, &MeshSkin::getMatrixPalette, &MeshSkin::getMatrixPaletteSize);
  228. }
  229. }
  230. break;
  231. default:
  232. GP_ERROR("Unsupported auto binding type (%d).", autoBinding);
  233. break;
  234. }
  235. }
  236. void RenderState::bind(Pass* pass)
  237. {
  238. GP_ASSERT(pass);
  239. // Get the combined modified state bits for our RenderState hierarchy.
  240. long stateOverrideBits = _state ? _state->_bits : 0;
  241. RenderState* rs = _parent;
  242. while (rs)
  243. {
  244. if (rs->_state)
  245. {
  246. stateOverrideBits |= rs->_state->_bits;
  247. }
  248. rs = rs->_parent;
  249. }
  250. // Restore renderer state to its default, except for explicitly specified states
  251. StateBlock::restore(stateOverrideBits);
  252. // Apply parameter bindings and renderer state for the entire hierarchy, top-down.
  253. rs = NULL;
  254. Effect* effect = pass->getEffect();
  255. while (rs = getTopmost(rs))
  256. {
  257. for (unsigned int i = 0, count = rs->_parameters.size(); i < count; ++i)
  258. {
  259. GP_ASSERT(rs->_parameters[i]);
  260. rs->_parameters[i]->bind(effect);
  261. }
  262. if (rs->_state)
  263. {
  264. rs->_state->bindNoRestore();
  265. }
  266. }
  267. }
  268. RenderState* RenderState::getTopmost(RenderState* below)
  269. {
  270. RenderState* rs = this;
  271. if (rs == below)
  272. {
  273. // Nothing below ourself.
  274. return NULL;
  275. }
  276. while (rs)
  277. {
  278. if (rs->_parent == below || rs->_parent == NULL)
  279. {
  280. // Stop traversing up here.
  281. return rs;
  282. }
  283. rs = rs->_parent;
  284. }
  285. return NULL;
  286. }
  287. void RenderState::cloneInto(RenderState* renderState, NodeCloneContext& context) const
  288. {
  289. GP_ASSERT(renderState);
  290. for (std::map<std::string, AutoBinding>::const_iterator it = _autoBindings.begin(); it != _autoBindings.end(); ++it)
  291. {
  292. renderState->setParameterAutoBinding(it->first.c_str(), it->second);
  293. }
  294. for (std::vector<MaterialParameter*>::const_iterator it = _parameters.begin(); it != _parameters.end(); ++it)
  295. {
  296. const MaterialParameter* param = *it;
  297. GP_ASSERT(param);
  298. MaterialParameter* paramCopy = new MaterialParameter(param->getName());
  299. param->cloneInto(paramCopy);
  300. renderState->_parameters.push_back(paramCopy);
  301. }
  302. renderState->_parent = _parent;
  303. if (Node* node = context.findClonedNode(_nodeBinding))
  304. {
  305. renderState->setNodeBinding(node);
  306. }
  307. if (_state)
  308. {
  309. renderState->setStateBlock(_state);
  310. }
  311. }
  312. RenderState::StateBlock::StateBlock()
  313. : _blendEnabled(false), _cullFaceEnabled(false), _depthTestEnabled(false), _depthWriteEnabled(false),
  314. _srcBlend(RenderState::BLEND_ONE), _dstBlend(RenderState::BLEND_ONE), _bits(0L)
  315. {
  316. }
  317. RenderState::StateBlock::StateBlock(const StateBlock& copy)
  318. {
  319. // Hidden
  320. }
  321. RenderState::StateBlock::~StateBlock()
  322. {
  323. }
  324. RenderState::StateBlock* RenderState::StateBlock::create()
  325. {
  326. return new RenderState::StateBlock();
  327. }
  328. void RenderState::StateBlock::bind()
  329. {
  330. // When the public bind() is called with no RenderState object passed in,
  331. // we assume we are being called to bind the state of a single StateBlock,
  332. // irrespective of whether it belongs to a hierarchy of RenderStates.
  333. // Therefore, we call restore() here with only this StateBlock's override
  334. // bits to restore state before applying the new state.
  335. StateBlock::restore(_bits);
  336. bindNoRestore();
  337. }
  338. void RenderState::StateBlock::bindNoRestore()
  339. {
  340. GP_ASSERT(_defaultState);
  341. // Update any state that differs from _defaultState and flip _defaultState bits
  342. if ((_bits & RS_BLEND) && (_blendEnabled != _defaultState->_blendEnabled))
  343. {
  344. if (_blendEnabled)
  345. GL_ASSERT( glEnable(GL_BLEND) );
  346. else
  347. GL_ASSERT( glDisable(GL_BLEND) );
  348. _defaultState->_blendEnabled = _blendEnabled;
  349. }
  350. if ((_bits & RS_BLEND_FUNC) && (_srcBlend != _defaultState->_srcBlend || _dstBlend != _defaultState->_dstBlend))
  351. {
  352. GL_ASSERT( glBlendFunc((GLenum)_srcBlend, (GLenum)_dstBlend) );
  353. _defaultState->_srcBlend = _srcBlend;
  354. _defaultState->_dstBlend = _dstBlend;
  355. }
  356. if ((_bits & RS_CULL_FACE) && (_cullFaceEnabled != _defaultState->_cullFaceEnabled))
  357. {
  358. if (_cullFaceEnabled)
  359. GL_ASSERT( glEnable(GL_CULL_FACE) );
  360. else
  361. GL_ASSERT( glDisable(GL_CULL_FACE) );
  362. _defaultState->_cullFaceEnabled = _cullFaceEnabled;
  363. }
  364. if ((_bits & RS_DEPTH_TEST) && (_depthTestEnabled != _defaultState->_depthTestEnabled))
  365. {
  366. if (_depthTestEnabled)
  367. GL_ASSERT( glEnable(GL_DEPTH_TEST) );
  368. else
  369. GL_ASSERT( glDisable(GL_DEPTH_TEST) );
  370. _defaultState->_depthTestEnabled = _depthTestEnabled;
  371. }
  372. if ((_bits & RS_DEPTH_WRITE) && (_depthWriteEnabled != _defaultState->_depthWriteEnabled))
  373. {
  374. GL_ASSERT( glDepthMask(_depthWriteEnabled ? GL_TRUE : GL_FALSE) );
  375. _defaultState->_depthWriteEnabled = _depthWriteEnabled;
  376. }
  377. _defaultState->_bits |= _bits;
  378. }
  379. void RenderState::StateBlock::restore(long stateOverrideBits)
  380. {
  381. GP_ASSERT(_defaultState);
  382. // If there is no state to restore (i.e. no non-default state), do nothing.
  383. if (_defaultState->_bits == 0)
  384. {
  385. return;
  386. }
  387. // Restore any state that is not overridden and is not default
  388. if (!(stateOverrideBits & RS_BLEND) && (_defaultState->_bits & RS_BLEND))
  389. {
  390. GL_ASSERT( glDisable(GL_BLEND) );
  391. _defaultState->_bits &= ~RS_BLEND;
  392. _defaultState->_blendEnabled = false;
  393. }
  394. if (!(stateOverrideBits & RS_BLEND_FUNC) && (_defaultState->_bits & RS_BLEND_FUNC))
  395. {
  396. GL_ASSERT( glBlendFunc(GL_ONE, GL_ONE) );
  397. _defaultState->_bits &= ~RS_BLEND_FUNC;
  398. _defaultState->_srcBlend = RenderState::BLEND_ONE;
  399. _defaultState->_dstBlend = RenderState::BLEND_ONE;
  400. }
  401. if (!(stateOverrideBits & RS_CULL_FACE) && (_defaultState->_bits & RS_CULL_FACE))
  402. {
  403. GL_ASSERT( glDisable(GL_CULL_FACE) );
  404. _defaultState->_bits &= ~RS_CULL_FACE;
  405. _defaultState->_cullFaceEnabled = false;
  406. }
  407. if (!(stateOverrideBits & RS_DEPTH_TEST) && (_defaultState->_bits & RS_DEPTH_TEST))
  408. {
  409. GL_ASSERT( glDisable(GL_DEPTH_TEST) );
  410. _defaultState->_bits &= ~RS_DEPTH_TEST;
  411. _defaultState->_depthTestEnabled = false;
  412. }
  413. if (!(stateOverrideBits & RS_DEPTH_WRITE) && (_defaultState->_bits & RS_DEPTH_WRITE))
  414. {
  415. GL_ASSERT( glDepthMask(GL_TRUE) );
  416. _defaultState->_bits &= ~RS_DEPTH_WRITE;
  417. _defaultState->_depthWriteEnabled = true;
  418. }
  419. }
  420. void RenderState::StateBlock::enableDepthWrite()
  421. {
  422. GP_ASSERT(_defaultState);
  423. // Internal method used by Game::clear() to restore depth writing before a
  424. // clear operation. This is neccessary if the last code to draw before the
  425. // next frame leaves depth writing disabled.
  426. if (!_defaultState->_depthWriteEnabled)
  427. {
  428. GL_ASSERT( glDepthMask(GL_TRUE) );
  429. _defaultState->_bits &= ~RS_DEPTH_WRITE;
  430. _defaultState->_depthWriteEnabled = true;
  431. }
  432. }
  433. bool parseBoolean(const char* value)
  434. {
  435. GP_ASSERT(value);
  436. if (strlen(value) == 4)
  437. {
  438. return (
  439. tolower(value[0]) == 't' &&
  440. tolower(value[1]) == 'r' &&
  441. tolower(value[2]) == 'u' &&
  442. tolower(value[3]) == 'e' );
  443. }
  444. return false;
  445. }
  446. RenderState::Blend parseBlend(const char* value)
  447. {
  448. GP_ASSERT(value);
  449. // Convert the string to uppercase for comparison.
  450. std::string upper(value);
  451. std::transform(upper.begin(), upper.end(), upper.begin(), (int(*)(int))toupper);
  452. if (upper == "ZERO")
  453. return RenderState::BLEND_ZERO;
  454. else if (upper == "ONE")
  455. return RenderState::BLEND_ONE;
  456. else if (upper == "SRC_ALPHA")
  457. return RenderState::BLEND_SRC_ALPHA;
  458. else if (upper == "ONE_MINUS_SRC_ALPHA")
  459. return RenderState::BLEND_ONE_MINUS_SRC_ALPHA;
  460. else if (upper == "DST_ALPHA")
  461. return RenderState::BLEND_DST_ALPHA;
  462. else if (upper == "ONE_MINUS_DST_ALPHA")
  463. return RenderState::BLEND_ONE_MINUS_DST_ALPHA;
  464. else if (upper == "CONSTANT_ALPHA")
  465. return RenderState::BLEND_CONSTANT_ALPHA;
  466. else if (upper == "ONE_MINUS_CONSTANT_ALPHA")
  467. return RenderState::BLEND_ONE_MINUS_CONSTANT_ALPHA;
  468. else if (upper == "SRC_ALPHA_SATURATE")
  469. return RenderState::BLEND_SRC_ALPHA_SATURATE;
  470. else
  471. {
  472. GP_ERROR("Unsupported blend value (%s). (Will default to BLEND_ONE if errors are treated as warnings)", value);
  473. return RenderState::BLEND_ONE;
  474. }
  475. }
  476. void RenderState::StateBlock::setState(const char* name, const char* value)
  477. {
  478. GP_ASSERT(name);
  479. if (strcmp(name, "blend") == 0)
  480. {
  481. setBlend(parseBoolean(value));
  482. }
  483. else if (strcmp(name, "srcBlend") == 0)
  484. {
  485. setBlendSrc(parseBlend(value));
  486. }
  487. else if (strcmp(name, "dstBlend") == 0)
  488. {
  489. setBlendDst(parseBlend(value));
  490. }
  491. else if (strcmp(name, "cullFace") == 0)
  492. {
  493. setCullFace(parseBoolean(value));
  494. }
  495. else if (strcmp(name, "depthTest") == 0)
  496. {
  497. setDepthTest(parseBoolean(value));
  498. }
  499. else if (strcmp(name, "depthWrite") == 0)
  500. {
  501. setDepthWrite(parseBoolean(value));
  502. }
  503. else
  504. {
  505. GP_ERROR("Unsupported render state string '%s'.", name);
  506. }
  507. }
  508. void RenderState::StateBlock::setBlend(bool enabled)
  509. {
  510. _blendEnabled = enabled;
  511. if (!enabled)
  512. {
  513. _bits &= ~RS_BLEND;
  514. }
  515. else
  516. {
  517. _bits |= RS_BLEND;
  518. }
  519. }
  520. void RenderState::StateBlock::setBlendSrc(Blend blend)
  521. {
  522. _srcBlend = blend;
  523. if (_srcBlend == BLEND_ONE && _dstBlend == BLEND_ONE)
  524. {
  525. _bits &= ~RS_BLEND_FUNC;
  526. }
  527. else
  528. {
  529. _bits |= RS_BLEND_FUNC;
  530. }
  531. }
  532. void RenderState::StateBlock::setBlendDst(Blend blend)
  533. {
  534. _dstBlend = blend;
  535. if (_srcBlend == BLEND_ONE && _dstBlend == BLEND_ONE)
  536. {
  537. _bits &= ~RS_BLEND_FUNC;
  538. }
  539. else
  540. {
  541. _bits |= RS_BLEND_FUNC;
  542. }
  543. }
  544. void RenderState::StateBlock::setCullFace(bool enabled)
  545. {
  546. _cullFaceEnabled = enabled;
  547. if (!enabled)
  548. {
  549. _bits &= ~RS_CULL_FACE;
  550. }
  551. else
  552. {
  553. _bits |= RS_CULL_FACE;
  554. }
  555. }
  556. void RenderState::StateBlock::setDepthTest(bool enabled)
  557. {
  558. _depthTestEnabled = enabled;
  559. if (!enabled)
  560. {
  561. _bits &= ~RS_DEPTH_TEST;
  562. }
  563. else
  564. {
  565. _bits |= RS_DEPTH_TEST;
  566. }
  567. }
  568. void RenderState::StateBlock::setDepthWrite(bool enabled)
  569. {
  570. _depthWriteEnabled = enabled;
  571. if (enabled)
  572. {
  573. _bits &= ~RS_DEPTH_WRITE;
  574. }
  575. else
  576. {
  577. _bits |= RS_DEPTH_WRITE;
  578. }
  579. }
  580. }