RenderState.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. : _cullFaceEnabled(false), _depthTestEnabled(false), _depthWriteEnabled(false),
  314. _blendEnabled(false), _blendSrc(RenderState::BLEND_ONE), _blendDst(RenderState::BLEND_ZERO),
  315. _bits(0L)
  316. {
  317. }
  318. RenderState::StateBlock::StateBlock(const StateBlock& copy)
  319. {
  320. // Hidden
  321. }
  322. RenderState::StateBlock::~StateBlock()
  323. {
  324. }
  325. RenderState::StateBlock* RenderState::StateBlock::create()
  326. {
  327. return new RenderState::StateBlock();
  328. }
  329. void RenderState::StateBlock::bind()
  330. {
  331. // When the public bind() is called with no RenderState object passed in,
  332. // we assume we are being called to bind the state of a single StateBlock,
  333. // irrespective of whether it belongs to a hierarchy of RenderStates.
  334. // Therefore, we call restore() here with only this StateBlock's override
  335. // bits to restore state before applying the new state.
  336. StateBlock::restore(_bits);
  337. bindNoRestore();
  338. }
  339. void RenderState::StateBlock::bindNoRestore()
  340. {
  341. GP_ASSERT(_defaultState);
  342. // Update any state that differs from _defaultState and flip _defaultState bits
  343. if ((_bits & RS_BLEND) && (_blendEnabled != _defaultState->_blendEnabled))
  344. {
  345. if (_blendEnabled)
  346. GL_ASSERT( glEnable(GL_BLEND) );
  347. else
  348. GL_ASSERT( glDisable(GL_BLEND) );
  349. _defaultState->_blendEnabled = _blendEnabled;
  350. }
  351. if ((_bits & RS_BLEND_FUNC) && (_blendSrc != _defaultState->_blendSrc || _blendDst != _defaultState->_blendDst))
  352. {
  353. GL_ASSERT( glBlendFunc((GLenum)_blendSrc, (GLenum)_blendDst) );
  354. _defaultState->_blendSrc = _blendSrc;
  355. _defaultState->_blendDst = _blendDst;
  356. }
  357. if ((_bits & RS_CULL_FACE) && (_cullFaceEnabled != _defaultState->_cullFaceEnabled))
  358. {
  359. if (_cullFaceEnabled)
  360. GL_ASSERT( glEnable(GL_CULL_FACE) );
  361. else
  362. GL_ASSERT( glDisable(GL_CULL_FACE) );
  363. _defaultState->_cullFaceEnabled = _cullFaceEnabled;
  364. }
  365. if ((_bits & RS_DEPTH_TEST) && (_depthTestEnabled != _defaultState->_depthTestEnabled))
  366. {
  367. if (_depthTestEnabled)
  368. GL_ASSERT( glEnable(GL_DEPTH_TEST) );
  369. else
  370. GL_ASSERT( glDisable(GL_DEPTH_TEST) );
  371. _defaultState->_depthTestEnabled = _depthTestEnabled;
  372. }
  373. if ((_bits & RS_DEPTH_WRITE) && (_depthWriteEnabled != _defaultState->_depthWriteEnabled))
  374. {
  375. GL_ASSERT( glDepthMask(_depthWriteEnabled ? GL_TRUE : GL_FALSE) );
  376. _defaultState->_depthWriteEnabled = _depthWriteEnabled;
  377. }
  378. _defaultState->_bits |= _bits;
  379. }
  380. void RenderState::StateBlock::restore(long stateOverrideBits)
  381. {
  382. GP_ASSERT(_defaultState);
  383. // If there is no state to restore (i.e. no non-default state), do nothing.
  384. if (_defaultState->_bits == 0)
  385. {
  386. return;
  387. }
  388. // Restore any state that is not overridden and is not default
  389. if (!(stateOverrideBits & RS_BLEND) && (_defaultState->_bits & RS_BLEND))
  390. {
  391. GL_ASSERT( glDisable(GL_BLEND) );
  392. _defaultState->_bits &= ~RS_BLEND;
  393. _defaultState->_blendEnabled = false;
  394. }
  395. if (!(stateOverrideBits & RS_BLEND_FUNC) && (_defaultState->_bits & RS_BLEND_FUNC))
  396. {
  397. GL_ASSERT( glBlendFunc(GL_ONE, GL_ZERO) );
  398. _defaultState->_bits &= ~RS_BLEND_FUNC;
  399. _defaultState->_blendSrc = RenderState::BLEND_ONE;
  400. _defaultState->_blendDst = RenderState::BLEND_ZERO;
  401. }
  402. if (!(stateOverrideBits & RS_CULL_FACE) && (_defaultState->_bits & RS_CULL_FACE))
  403. {
  404. GL_ASSERT( glDisable(GL_CULL_FACE) );
  405. _defaultState->_bits &= ~RS_CULL_FACE;
  406. _defaultState->_cullFaceEnabled = false;
  407. }
  408. if (!(stateOverrideBits & RS_DEPTH_TEST) && (_defaultState->_bits & RS_DEPTH_TEST))
  409. {
  410. GL_ASSERT( glDisable(GL_DEPTH_TEST) );
  411. _defaultState->_bits &= ~RS_DEPTH_TEST;
  412. _defaultState->_depthTestEnabled = false;
  413. }
  414. if (!(stateOverrideBits & RS_DEPTH_WRITE) && (_defaultState->_bits & RS_DEPTH_WRITE))
  415. {
  416. GL_ASSERT( glDepthMask(GL_TRUE) );
  417. _defaultState->_bits &= ~RS_DEPTH_WRITE;
  418. _defaultState->_depthWriteEnabled = true;
  419. }
  420. }
  421. void RenderState::StateBlock::enableDepthWrite()
  422. {
  423. GP_ASSERT(_defaultState);
  424. // Internal method used by Game::clear() to restore depth writing before a
  425. // clear operation. This is neccessary if the last code to draw before the
  426. // next frame leaves depth writing disabled.
  427. if (!_defaultState->_depthWriteEnabled)
  428. {
  429. GL_ASSERT( glDepthMask(GL_TRUE) );
  430. _defaultState->_bits &= ~RS_DEPTH_WRITE;
  431. _defaultState->_depthWriteEnabled = true;
  432. }
  433. }
  434. static bool parseBoolean(const char* value)
  435. {
  436. GP_ASSERT(value);
  437. if (strlen(value) == 4)
  438. {
  439. return (
  440. tolower(value[0]) == 't' &&
  441. tolower(value[1]) == 'r' &&
  442. tolower(value[2]) == 'u' &&
  443. tolower(value[3]) == 'e' );
  444. }
  445. return false;
  446. }
  447. static RenderState::Blend parseBlend(const char* value)
  448. {
  449. GP_ASSERT(value);
  450. // Convert the string to uppercase for comparison.
  451. std::string upper(value);
  452. std::transform(upper.begin(), upper.end(), upper.begin(), (int(*)(int))toupper);
  453. if (upper == "ZERO")
  454. return RenderState::BLEND_ZERO;
  455. else if (upper == "ONE")
  456. return RenderState::BLEND_ONE;
  457. else if (upper == "SRC_ALPHA")
  458. return RenderState::BLEND_SRC_ALPHA;
  459. else if (upper == "ONE_MINUS_SRC_ALPHA")
  460. return RenderState::BLEND_ONE_MINUS_SRC_ALPHA;
  461. else if (upper == "DST_ALPHA")
  462. return RenderState::BLEND_DST_ALPHA;
  463. else if (upper == "ONE_MINUS_DST_ALPHA")
  464. return RenderState::BLEND_ONE_MINUS_DST_ALPHA;
  465. else if (upper == "CONSTANT_ALPHA")
  466. return RenderState::BLEND_CONSTANT_ALPHA;
  467. else if (upper == "ONE_MINUS_CONSTANT_ALPHA")
  468. return RenderState::BLEND_ONE_MINUS_CONSTANT_ALPHA;
  469. else if (upper == "SRC_ALPHA_SATURATE")
  470. return RenderState::BLEND_SRC_ALPHA_SATURATE;
  471. else
  472. {
  473. GP_ERROR("Unsupported blend value (%s). (Will default to BLEND_ONE if errors are treated as warnings)", value);
  474. return RenderState::BLEND_ONE;
  475. }
  476. }
  477. void RenderState::StateBlock::setState(const char* name, const char* value)
  478. {
  479. GP_ASSERT(name);
  480. if (strcmp(name, "blend") == 0)
  481. {
  482. setBlend(parseBoolean(value));
  483. }
  484. else if (strcmp(name, "blendSrc") == 0 || strcmp(name, "srcBlend") == 0 ) // Leaving srcBlend for backward compat.
  485. {
  486. setBlendSrc(parseBlend(value));
  487. }
  488. else if (strcmp(name, "blendDst") == 0 || strcmp(name, "dstBlend") == 0) // // Leaving dstBlend for backward compat.
  489. {
  490. setBlendDst(parseBlend(value));
  491. }
  492. else if (strcmp(name, "cullFace") == 0)
  493. {
  494. setCullFace(parseBoolean(value));
  495. }
  496. else if (strcmp(name, "depthTest") == 0)
  497. {
  498. setDepthTest(parseBoolean(value));
  499. }
  500. else if (strcmp(name, "depthWrite") == 0)
  501. {
  502. setDepthWrite(parseBoolean(value));
  503. }
  504. else
  505. {
  506. GP_ERROR("Unsupported render state string '%s'.", name);
  507. }
  508. }
  509. void RenderState::StateBlock::setBlend(bool enabled)
  510. {
  511. _blendEnabled = enabled;
  512. if (!enabled)
  513. {
  514. _bits &= ~RS_BLEND;
  515. }
  516. else
  517. {
  518. _bits |= RS_BLEND;
  519. }
  520. }
  521. void RenderState::StateBlock::setBlendSrc(Blend blend)
  522. {
  523. _blendSrc = blend;
  524. if (_blendSrc == BLEND_ONE && _blendDst == BLEND_ZERO)
  525. {
  526. // Default blend func
  527. _bits &= ~RS_BLEND_FUNC;
  528. }
  529. else
  530. {
  531. _bits |= RS_BLEND_FUNC;
  532. }
  533. }
  534. void RenderState::StateBlock::setBlendDst(Blend blend)
  535. {
  536. _blendDst = blend;
  537. if (_blendSrc == BLEND_ONE && _blendDst == BLEND_ZERO)
  538. {
  539. // Default blend func
  540. _bits &= ~RS_BLEND_FUNC;
  541. }
  542. else
  543. {
  544. _bits |= RS_BLEND_FUNC;
  545. }
  546. }
  547. void RenderState::StateBlock::setCullFace(bool enabled)
  548. {
  549. _cullFaceEnabled = enabled;
  550. if (!enabled)
  551. {
  552. _bits &= ~RS_CULL_FACE;
  553. }
  554. else
  555. {
  556. _bits |= RS_CULL_FACE;
  557. }
  558. }
  559. void RenderState::StateBlock::setDepthTest(bool enabled)
  560. {
  561. _depthTestEnabled = enabled;
  562. if (!enabled)
  563. {
  564. _bits &= ~RS_DEPTH_TEST;
  565. }
  566. else
  567. {
  568. _bits |= RS_DEPTH_TEST;
  569. }
  570. }
  571. void RenderState::StateBlock::setDepthWrite(bool enabled)
  572. {
  573. _depthWriteEnabled = enabled;
  574. if (enabled)
  575. {
  576. _bits &= ~RS_DEPTH_WRITE;
  577. }
  578. else
  579. {
  580. _bits |= RS_DEPTH_WRITE;
  581. }
  582. }
  583. }