RenderState.cpp 16 KB

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