RenderState.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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_VIEW_MATRIX") == 0)
  116. {
  117. value = INVERSE_TRANSPOSE_WORLD_VIEW_MATRIX;
  118. }
  119. else if (strcmp(autoBinding, "CAMERA_WORLD_POSITION") == 0)
  120. {
  121. value = CAMERA_WORLD_POSITION;
  122. }
  123. else if (strcmp(autoBinding, "MATRIX_PALETTE") == 0)
  124. {
  125. value = MATRIX_PALETTE;
  126. }
  127. if (value != NONE)
  128. {
  129. setParameterAutoBinding(name, value);
  130. }
  131. }
  132. void RenderState::setStateBlock(StateBlock* state)
  133. {
  134. if (_state != state)
  135. {
  136. SAFE_RELEASE(_state);
  137. _state = state;
  138. if (_state)
  139. {
  140. _state->addRef();
  141. }
  142. }
  143. }
  144. RenderState::StateBlock* RenderState::getStateBlock() const
  145. {
  146. if (_state == NULL)
  147. {
  148. _state = StateBlock::create();
  149. }
  150. return _state;
  151. }
  152. void RenderState::setNodeBinding(Node* node)
  153. {
  154. _nodeBinding = node;
  155. if (_nodeBinding)
  156. {
  157. // Apply all existing auto-bindings using this node
  158. std::map<std::string, AutoBinding>::const_iterator itr = _autoBindings.begin();
  159. while (itr != _autoBindings.end())
  160. {
  161. applyAutoBinding(itr->first.c_str(), itr->second);
  162. itr++;
  163. }
  164. }
  165. }
  166. void RenderState::applyAutoBinding(const char* uniformName, AutoBinding autoBinding)
  167. {
  168. switch (autoBinding)
  169. {
  170. case WORLD_MATRIX:
  171. getParameter(uniformName)->bindValue(_nodeBinding, &Node::getWorldMatrix);
  172. break;
  173. case VIEW_MATRIX:
  174. getParameter(uniformName)->bindValue(_nodeBinding, &Node::getViewMatrix);
  175. break;
  176. case PROJECTION_MATRIX:
  177. getParameter(uniformName)->bindValue(_nodeBinding, &Node::getProjectionMatrix);
  178. break;
  179. case WORLD_VIEW_MATRIX:
  180. getParameter(uniformName)->bindValue(_nodeBinding, &Node::getWorldViewMatrix);
  181. break;
  182. case VIEW_PROJECTION_MATRIX:
  183. getParameter(uniformName)->bindValue(_nodeBinding, &Node::getViewProjectionMatrix);
  184. break;
  185. case WORLD_VIEW_PROJECTION_MATRIX:
  186. getParameter(uniformName)->bindValue(_nodeBinding, &Node::getWorldViewProjectionMatrix);
  187. break;
  188. case INVERSE_TRANSPOSE_WORLD_VIEW_MATRIX:
  189. getParameter(uniformName)->bindValue(_nodeBinding, &Node::getInverseTransposeWorldViewMatrix);
  190. break;
  191. case CAMERA_WORLD_POSITION:
  192. getParameter(uniformName)->bindValue(_nodeBinding, &Node::getActiveCameraTranslationWorld);
  193. break;
  194. case MATRIX_PALETTE:
  195. {
  196. Model* model = _nodeBinding->getModel();
  197. MeshSkin* skin = model ? model->getSkin() : NULL;
  198. if (skin)
  199. {
  200. getParameter(uniformName)->bindValue(skin, &MeshSkin::getMatrixPalette, &MeshSkin::getMatrixPaletteSize);
  201. }
  202. }
  203. break;
  204. }
  205. }
  206. void RenderState::bind(Pass* pass)
  207. {
  208. // Get the combined modified state bits for our RenderState hierarchy.
  209. long stateOverrideBits = _state ? _state->_bits : 0;
  210. RenderState* rs = _parent;
  211. while (rs)
  212. {
  213. if (rs->_state)
  214. {
  215. stateOverrideBits |= rs->_state->_bits;
  216. }
  217. rs = rs->_parent;
  218. }
  219. // Restore renderer state to its default, except for explicitly specified states
  220. StateBlock::restore(stateOverrideBits);
  221. // Apply parameter bindings and renderer state for the entire hierarchy, top-down.
  222. rs = NULL;
  223. Effect* effect = pass->getEffect();
  224. while (rs = getTopmost(rs))
  225. {
  226. for (unsigned int i = 0, count = rs->_parameters.size(); i < count; ++i)
  227. {
  228. rs->_parameters[i]->bind(effect);
  229. }
  230. if (rs->_state)
  231. {
  232. rs->_state->bindNoRestore();
  233. }
  234. }
  235. }
  236. RenderState* RenderState::getTopmost(RenderState* below)
  237. {
  238. RenderState* rs = this;
  239. if (rs == below)
  240. {
  241. // Nothing below ourself
  242. return NULL;
  243. }
  244. while (rs)
  245. {
  246. if (rs->_parent == below || rs->_parent == NULL)
  247. {
  248. // Stop traversing up here
  249. return rs;
  250. }
  251. rs = rs->_parent;
  252. }
  253. return NULL;
  254. }
  255. RenderState::StateBlock::StateBlock()
  256. : _blendEnabled(false), _cullFaceEnabled(false), _depthTestEnabled(false), _depthWriteEnabled(false),
  257. _srcBlend(RenderState::BLEND_ONE), _dstBlend(RenderState::BLEND_ONE), _bits(0L)
  258. {
  259. }
  260. RenderState::StateBlock::StateBlock(const StateBlock& copy)
  261. {
  262. // Hidden
  263. }
  264. RenderState::StateBlock::~StateBlock()
  265. {
  266. }
  267. RenderState::StateBlock* RenderState::StateBlock::create()
  268. {
  269. return new RenderState::StateBlock();
  270. }
  271. void RenderState::StateBlock::bind()
  272. {
  273. // When the public bind() is called with no RenderState object passed in,
  274. // we assume we are being called to bind the state of a single StateBlock,
  275. // irrespective of whether it belongs to a hierarchy of RenderStates.
  276. // Therefore, we call restore() here with only this StateBlock's override
  277. // bits to restore state before applying the new state.
  278. StateBlock::restore(_bits);
  279. bindNoRestore();
  280. }
  281. void RenderState::StateBlock::bindNoRestore()
  282. {
  283. // Update any state that differs from _defaultState and flip _defaultState bits
  284. if ((_bits & RS_BLEND) && (_blendEnabled != _defaultState->_blendEnabled))
  285. {
  286. _blendEnabled ? glEnable(GL_BLEND) : glDisable(GL_BLEND);
  287. _defaultState->_blendEnabled = _blendEnabled;
  288. }
  289. if ((_bits & RS_BLEND_FUNC) && (_srcBlend != _defaultState->_srcBlend || _dstBlend != _defaultState->_dstBlend))
  290. {
  291. glBlendFunc((GLenum)_srcBlend, (GLenum)_dstBlend);
  292. _defaultState->_srcBlend = _srcBlend;
  293. _defaultState->_dstBlend = _dstBlend;
  294. }
  295. if ((_bits & RS_CULL_FACE) && (_cullFaceEnabled != _defaultState->_cullFaceEnabled))
  296. {
  297. _cullFaceEnabled ? glEnable(GL_CULL_FACE) : glDisable(GL_CULL_FACE);
  298. _defaultState->_cullFaceEnabled = _cullFaceEnabled;
  299. }
  300. if ((_bits & RS_DEPTH_TEST) && (_depthTestEnabled != _defaultState->_depthTestEnabled))
  301. {
  302. _depthTestEnabled ? glEnable(GL_DEPTH_TEST) : glDisable(GL_DEPTH_TEST);
  303. _defaultState->_depthTestEnabled = _depthTestEnabled;
  304. }
  305. if ((_bits & RS_DEPTH_WRITE) && (_depthWriteEnabled != _defaultState->_depthWriteEnabled))
  306. {
  307. glDepthMask(_depthWriteEnabled);
  308. _defaultState->_depthWriteEnabled = _depthWriteEnabled;
  309. }
  310. _defaultState->_bits |= _bits;
  311. }
  312. void RenderState::StateBlock::restore(long stateOverrideBits)
  313. {
  314. // If there is no state to restore (i.e. no non-default state), do nothing
  315. if (_defaultState->_bits == 0)
  316. {
  317. return;
  318. }
  319. // Restore any state that is not overridden and is not default
  320. if (!(stateOverrideBits & RS_BLEND) && (_defaultState->_bits & RS_BLEND))
  321. {
  322. glDisable(GL_BLEND);
  323. _defaultState->_bits &= ~RS_BLEND;
  324. _defaultState->_blendEnabled = false;
  325. }
  326. if (!(stateOverrideBits & RS_BLEND_FUNC) && (_defaultState->_bits & RS_BLEND_FUNC))
  327. {
  328. glBlendFunc(GL_ONE, GL_ONE);
  329. _defaultState->_bits &= ~RS_BLEND_FUNC;
  330. _defaultState->_srcBlend = RenderState::BLEND_ONE;
  331. _defaultState->_dstBlend = RenderState::BLEND_ONE;
  332. }
  333. if (!(stateOverrideBits & RS_CULL_FACE) && (_defaultState->_bits & RS_CULL_FACE))
  334. {
  335. glDisable(GL_CULL_FACE);
  336. _defaultState->_bits &= ~RS_CULL_FACE;
  337. _defaultState->_cullFaceEnabled = false;
  338. }
  339. if (!(stateOverrideBits & RS_DEPTH_TEST) && (_defaultState->_bits & RS_DEPTH_TEST))
  340. {
  341. glDisable(GL_DEPTH_TEST);
  342. _defaultState->_bits &= ~RS_DEPTH_TEST;
  343. _defaultState->_depthTestEnabled = false;
  344. }
  345. if (!(stateOverrideBits & RS_DEPTH_WRITE) && (_defaultState->_bits & RS_DEPTH_WRITE))
  346. {
  347. glDepthMask(GL_TRUE);
  348. _defaultState->_bits &= ~RS_DEPTH_WRITE;
  349. _defaultState->_depthWriteEnabled = true;
  350. }
  351. }
  352. bool parseBoolean(const char* value)
  353. {
  354. if (strlen(value) == 4)
  355. {
  356. return (
  357. tolower(value[0]) == 't' &&
  358. tolower(value[1]) == 'r' &&
  359. tolower(value[2]) == 'u' &&
  360. tolower(value[3]) == 'e' );
  361. }
  362. return false;
  363. }
  364. RenderState::Blend parseBlend(const char* value)
  365. {
  366. // Conver the string to uppercase for comparison
  367. std::string upper(value);
  368. std::transform(upper.begin(), upper.end(), upper.begin(), (int(*)(int))toupper);
  369. if (upper == "ZERO")
  370. return RenderState::BLEND_ZERO;
  371. if (upper == "ONE")
  372. return RenderState::BLEND_ONE;
  373. if (upper == "SRC_ALPHA")
  374. return RenderState::BLEND_SRC_ALPHA;
  375. if (upper == "ONE_MINUS_SRC_ALPHA")
  376. return RenderState::BLEND_ONE_MINUS_SRC_ALPHA;
  377. if (upper == "DST_ALPHA")
  378. return RenderState::BLEND_DST_ALPHA;
  379. if (upper == "ONE_MINUS_DST_ALPHA")
  380. return RenderState::BLEND_ONE_MINUS_DST_ALPHA;
  381. if (upper == "CONSTANT_ALPHA")
  382. return RenderState::BLEND_CONSTANT_ALPHA;
  383. if (upper == "ONE_MINUS_CONSTANT_ALPHA")
  384. return RenderState::BLEND_ONE_MINUS_CONSTANT_ALPHA;
  385. if (upper == "SRC_ALPHA_SATURATE")
  386. return RenderState::BLEND_SRC_ALPHA_SATURATE;
  387. WARN_VARG("Warning: Unrecognized blend value (%s), defaulting to BLEND_ONE.", value);
  388. return RenderState::BLEND_ONE;
  389. }
  390. void RenderState::StateBlock::setState(const char* name, const char* value)
  391. {
  392. assert(name && value);
  393. if (strcmp(name, "blend") == 0)
  394. {
  395. setBlend(parseBoolean(value));
  396. }
  397. else if (strcmp(name, "srcBlend") == 0)
  398. {
  399. setBlendSrc(parseBlend(value));
  400. }
  401. else if (strcmp(name, "dstBlend") == 0)
  402. {
  403. setBlendDst(parseBlend(value));
  404. }
  405. else if (strcmp(name, "cullFace") == 0)
  406. {
  407. setCullFace(parseBoolean(value));
  408. }
  409. else if (strcmp(name, "depthTest") == 0)
  410. {
  411. setDepthTest(parseBoolean(value));
  412. }
  413. else if (strcmp(name, "depthWrite") == 0)
  414. {
  415. setDepthWrite(parseBoolean(value));
  416. }
  417. else
  418. {
  419. WARN_VARG("Warning: Invalid render state: %s", name);
  420. }
  421. }
  422. void RenderState::StateBlock::setBlend(bool enabled)
  423. {
  424. _blendEnabled = enabled;
  425. if (!enabled)
  426. {
  427. _bits &= ~RS_BLEND;
  428. }
  429. else
  430. {
  431. _bits |= RS_BLEND;
  432. }
  433. }
  434. void RenderState::StateBlock::setBlendSrc(Blend blend)
  435. {
  436. _srcBlend = blend;
  437. if (_srcBlend == BLEND_ONE && _dstBlend == BLEND_ONE)
  438. {
  439. _bits &= ~RS_BLEND_FUNC;
  440. }
  441. else
  442. {
  443. _bits |= RS_BLEND_FUNC;
  444. }
  445. }
  446. void RenderState::StateBlock::setBlendDst(Blend blend)
  447. {
  448. _dstBlend = blend;
  449. if (_srcBlend == BLEND_ONE && _dstBlend == BLEND_ONE)
  450. {
  451. _bits &= ~RS_BLEND_FUNC;
  452. }
  453. else
  454. {
  455. _bits |= RS_BLEND_FUNC;
  456. }
  457. }
  458. void RenderState::StateBlock::setCullFace(bool enabled)
  459. {
  460. _cullFaceEnabled = enabled;
  461. if (!enabled)
  462. {
  463. _bits &= ~RS_CULL_FACE;
  464. }
  465. else
  466. {
  467. _bits |= RS_CULL_FACE;
  468. }
  469. }
  470. void RenderState::StateBlock::setDepthTest(bool enabled)
  471. {
  472. _depthTestEnabled = enabled;
  473. if (!enabled)
  474. {
  475. _bits &= ~RS_DEPTH_TEST;
  476. }
  477. else
  478. {
  479. _bits |= RS_DEPTH_TEST;
  480. }
  481. }
  482. void RenderState::StateBlock::setDepthWrite(bool enabled)
  483. {
  484. _depthWriteEnabled = enabled;
  485. if (enabled)
  486. {
  487. _bits &= ~RS_DEPTH_WRITE;
  488. }
  489. else
  490. {
  491. _bits |= RS_DEPTH_WRITE;
  492. }
  493. }
  494. }