RenderState.cpp 16 KB

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