RenderState.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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. #include "Scene.h"
  8. // Render state override bits
  9. #define RS_BLEND 1
  10. #define RS_BLEND_FUNC 2
  11. #define RS_CULL_FACE 4
  12. #define RS_DEPTH_TEST 8
  13. #define RS_DEPTH_WRITE 16
  14. #define RS_DEPTH_FUNC 32
  15. #define RS_CULL_FACE_SIDE 64
  16. namespace gameplay
  17. {
  18. RenderState::StateBlock* RenderState::StateBlock::_defaultState = NULL;
  19. std::vector<RenderState::ResolveAutoBindingCallback> RenderState::_customAutoBindingResolvers;
  20. RenderState::RenderState()
  21. : _nodeBinding(NULL), _state(NULL), _parent(NULL)
  22. {
  23. }
  24. RenderState::~RenderState()
  25. {
  26. SAFE_RELEASE(_state);
  27. // Destroy all the material parameters
  28. for (size_t i = 0, count = _parameters.size(); i < count; ++i)
  29. {
  30. SAFE_RELEASE(_parameters[i]);
  31. }
  32. }
  33. void RenderState::initialize()
  34. {
  35. if (StateBlock::_defaultState == NULL)
  36. {
  37. StateBlock::_defaultState = StateBlock::create();
  38. }
  39. }
  40. void RenderState::finalize()
  41. {
  42. SAFE_RELEASE(StateBlock::_defaultState);
  43. }
  44. void RenderState::registerAutoBindingResolver(ResolveAutoBindingCallback callback)
  45. {
  46. _customAutoBindingResolvers.push_back(callback);
  47. }
  48. MaterialParameter* RenderState::getParameter(const char* name) const
  49. {
  50. GP_ASSERT(name);
  51. // Search for an existing parameter with this name.
  52. MaterialParameter* param;
  53. for (size_t i = 0, count = _parameters.size(); i < count; ++i)
  54. {
  55. param = _parameters[i];
  56. GP_ASSERT(param);
  57. if (strcmp(param->getName(), name) == 0)
  58. {
  59. return param;
  60. }
  61. }
  62. // Create a new parameter and store it in our list.
  63. param = new MaterialParameter(name);
  64. _parameters.push_back(param);
  65. return param;
  66. }
  67. void RenderState::clearParameter(const char* name)
  68. {
  69. for (size_t i = 0, count = _parameters.size(); i < count; ++i)
  70. {
  71. MaterialParameter* p = _parameters[i];
  72. if (p->_name == name)
  73. {
  74. _parameters.erase(_parameters.begin() + i);
  75. SAFE_RELEASE(p);
  76. break;
  77. }
  78. }
  79. }
  80. /**
  81. * @script{ignore}
  82. */
  83. const char* autoBindingToString(RenderState::AutoBinding autoBinding)
  84. {
  85. // NOTE: As new AutoBinding values are added, this switch statement must be updatd.
  86. switch (autoBinding)
  87. {
  88. case RenderState::NONE:
  89. return NULL;
  90. case RenderState::VIEW_MATRIX:
  91. return "VIEW_MATRIX";
  92. case RenderState::PROJECTION_MATRIX:
  93. return "PROJECTION_MATRIX";
  94. case RenderState::WORLD_VIEW_MATRIX:
  95. return "WORLD_VIEW_MATRIX";
  96. case RenderState::VIEW_PROJECTION_MATRIX:
  97. return "VIEW_PROJECTION_MATRIX";
  98. case RenderState::WORLD_VIEW_PROJECTION_MATRIX:
  99. return "WORLD_VIEW_PROJECTION_MATRIX";
  100. case RenderState::INVERSE_TRANSPOSE_WORLD_MATRIX:
  101. return "INVERSE_TRANSPOSE_WORLD_MATRIX";
  102. case RenderState::INVERSE_TRANSPOSE_WORLD_VIEW_MATRIX:
  103. return "INVERSE_TRANSPOSE_WORLD_VIEW_MATRIX";
  104. case RenderState::CAMERA_WORLD_POSITION:
  105. return "CAMERA_WORLD_POSITION";
  106. case RenderState::CAMERA_VIEW_POSITION:
  107. return "CAMERA_VIEW_POSITION";
  108. case RenderState::MATRIX_PALETTE:
  109. return "MATRIX_PALETTE";
  110. case RenderState::SCENE_AMBIENT_COLOR:
  111. return "SCENE_AMBIENT_COLOR";
  112. case RenderState::SCENE_LIGHT_COLOR:
  113. return "SCENE_LIGHT_COLOR";
  114. case RenderState::SCENE_LIGHT_DIRECTION:
  115. return "SCENE_LIGHT_DIRECTION";
  116. default:
  117. return "";
  118. }
  119. }
  120. void RenderState::setParameterAutoBinding(const char* name, AutoBinding autoBinding)
  121. {
  122. setParameterAutoBinding(name, autoBindingToString(autoBinding));
  123. }
  124. void RenderState::setParameterAutoBinding(const char* name, const char* autoBinding)
  125. {
  126. GP_ASSERT(name);
  127. GP_ASSERT(autoBinding);
  128. if (autoBinding == NULL)
  129. {
  130. // Remove an existing auto-binding
  131. std::map<std::string, std::string>::iterator itr = _autoBindings.find(name);
  132. if (itr != _autoBindings.end())
  133. _autoBindings.erase(itr);
  134. }
  135. else
  136. {
  137. // Add/update an auto-binding
  138. _autoBindings[name] = autoBinding;
  139. }
  140. // If we already have a node binding set, pass it to our handler now
  141. if (_nodeBinding)
  142. {
  143. applyAutoBinding(name, autoBinding);
  144. }
  145. }
  146. void RenderState::setStateBlock(StateBlock* state)
  147. {
  148. if (_state != state)
  149. {
  150. SAFE_RELEASE(_state);
  151. _state = state;
  152. if (_state)
  153. {
  154. _state->addRef();
  155. }
  156. }
  157. }
  158. RenderState::StateBlock* RenderState::getStateBlock() const
  159. {
  160. if (_state == NULL)
  161. {
  162. _state = StateBlock::create();
  163. }
  164. return _state;
  165. }
  166. void RenderState::setNodeBinding(Node* node)
  167. {
  168. if (_nodeBinding != node)
  169. {
  170. _nodeBinding = node;
  171. if (_nodeBinding)
  172. {
  173. // Apply all existing auto-bindings using this node.
  174. std::map<std::string, std::string>::const_iterator itr = _autoBindings.begin();
  175. while (itr != _autoBindings.end())
  176. {
  177. applyAutoBinding(itr->first.c_str(), itr->second.c_str());
  178. ++itr;
  179. }
  180. }
  181. }
  182. }
  183. void RenderState::applyAutoBinding(const char* uniformName, const char* autoBinding)
  184. {
  185. GP_ASSERT(_nodeBinding);
  186. MaterialParameter* param = getParameter(uniformName);
  187. GP_ASSERT(param);
  188. bool bound = false;
  189. // First attempt to resolve the binding using custom registered resolvers.
  190. if (_customAutoBindingResolvers.size() > 0)
  191. {
  192. for (size_t i = 0, count = _customAutoBindingResolvers.size(); i < count; ++i)
  193. {
  194. if (_customAutoBindingResolvers[i](autoBinding, _nodeBinding, param))
  195. {
  196. // Handled by custom auto binding resolver
  197. bound = true;
  198. break;
  199. }
  200. }
  201. }
  202. // Perform built-in resolution
  203. if (!bound)
  204. {
  205. bound = true;
  206. if (strcmp(autoBinding, "WORLD_MATRIX") == 0)
  207. {
  208. param->bindValue(this, &RenderState::autoBindingGetWorldMatrix);
  209. }
  210. else if (strcmp(autoBinding, "VIEW_MATRIX") == 0)
  211. {
  212. param->bindValue(this, &RenderState::autoBindingGetViewMatrix);
  213. }
  214. else if (strcmp(autoBinding, "PROJECTION_MATRIX") == 0)
  215. {
  216. param->bindValue(this, &RenderState::autoBindingGetProjectionMatrix);
  217. }
  218. else if (strcmp(autoBinding, "WORLD_VIEW_MATRIX") == 0)
  219. {
  220. param->bindValue(this, &RenderState::autoBindingGetWorldViewMatrix);
  221. }
  222. else if (strcmp(autoBinding, "VIEW_PROJECTION_MATRIX") == 0)
  223. {
  224. param->bindValue(this, &RenderState::autoBindingGetViewProjectionMatrix);
  225. }
  226. else if (strcmp(autoBinding, "WORLD_VIEW_PROJECTION_MATRIX") == 0)
  227. {
  228. param->bindValue(this, &RenderState::autoBindingGetWorldViewProjectionMatrix);
  229. }
  230. else if (strcmp(autoBinding, "INVERSE_TRANSPOSE_WORLD_MATRIX") == 0)
  231. {
  232. param->bindValue(this, &RenderState::autoBindingGetInverseTransposeWorldMatrix);
  233. }
  234. else if (strcmp(autoBinding, "INVERSE_TRANSPOSE_WORLD_VIEW_MATRIX") == 0)
  235. {
  236. param->bindValue(this, &RenderState::autoBindingGetInverseTransposeWorldViewMatrix);
  237. }
  238. else if (strcmp(autoBinding, "CAMERA_WORLD_POSITION") == 0)
  239. {
  240. param->bindValue(this, &RenderState::autoBindingGetCameraWorldPosition);
  241. }
  242. else if (strcmp(autoBinding, "CAMERA_VIEW_POSITION") == 0)
  243. {
  244. param->bindValue(this, &RenderState::autoBindingGetCameraViewPosition);
  245. }
  246. else if (strcmp(autoBinding, "MATRIX_PALETTE") == 0)
  247. {
  248. param->bindValue(this, &RenderState::autoBindingGetMatrixPalette, &RenderState::autoBindingGetMatrixPaletteSize);
  249. }
  250. else if (strcmp(autoBinding, "SCENE_AMBIENT_COLOR") == 0)
  251. {
  252. param->bindValue(this, &RenderState::autoBindingGetAmbientColor);
  253. }
  254. else if (strcmp(autoBinding, "SCENE_LIGHT_COLOR") == 0)
  255. {
  256. param->bindValue(this, &RenderState::autoBindingGetLightColor);
  257. }
  258. else if (strcmp(autoBinding, "SCENE_LIGHT_DIRECTION") == 0)
  259. {
  260. param->bindValue(this, &RenderState::autoBindingGetLightDirection);
  261. }
  262. else
  263. {
  264. bound = false;
  265. GP_WARN("Unsupported auto binding type (%d).", autoBinding);
  266. }
  267. }
  268. if (bound)
  269. {
  270. // Mark parameter as an auto binding
  271. if (param->_type == MaterialParameter::METHOD && param->_value.method)
  272. param->_value.method->_autoBinding = true;
  273. }
  274. }
  275. const Matrix& RenderState::autoBindingGetWorldMatrix() const
  276. {
  277. return _nodeBinding ? _nodeBinding->getWorldMatrix() : Matrix::identity();
  278. }
  279. const Matrix& RenderState::autoBindingGetViewMatrix() const
  280. {
  281. return _nodeBinding ? _nodeBinding->getViewMatrix() : Matrix::identity();
  282. }
  283. const Matrix& RenderState::autoBindingGetProjectionMatrix() const
  284. {
  285. return _nodeBinding ? _nodeBinding->getProjectionMatrix() : Matrix::identity();
  286. }
  287. const Matrix& RenderState::autoBindingGetWorldViewMatrix() const
  288. {
  289. return _nodeBinding ? _nodeBinding->getWorldViewMatrix() : Matrix::identity();
  290. }
  291. const Matrix& RenderState::autoBindingGetViewProjectionMatrix() const
  292. {
  293. return _nodeBinding ? _nodeBinding->getViewProjectionMatrix() : Matrix::identity();
  294. }
  295. const Matrix& RenderState::autoBindingGetWorldViewProjectionMatrix() const
  296. {
  297. return _nodeBinding ? _nodeBinding->getWorldViewProjectionMatrix() : Matrix::identity();
  298. }
  299. const Matrix& RenderState::autoBindingGetInverseTransposeWorldMatrix() const
  300. {
  301. return _nodeBinding ? _nodeBinding->getInverseTransposeWorldMatrix() : Matrix::identity();
  302. }
  303. const Matrix& RenderState::autoBindingGetInverseTransposeWorldViewMatrix() const
  304. {
  305. return _nodeBinding ? _nodeBinding->getInverseTransposeWorldViewMatrix() : Matrix::identity();
  306. }
  307. Vector3 RenderState::autoBindingGetCameraWorldPosition() const
  308. {
  309. return _nodeBinding ? _nodeBinding->getActiveCameraTranslationWorld() : Vector3::zero();
  310. }
  311. Vector3 RenderState::autoBindingGetCameraViewPosition() const
  312. {
  313. return _nodeBinding ? _nodeBinding->getActiveCameraTranslationView() : Vector3::zero();
  314. }
  315. const Vector4* RenderState::autoBindingGetMatrixPalette() const
  316. {
  317. Model* model = _nodeBinding ? _nodeBinding->getModel() : NULL;
  318. MeshSkin* skin = model ? model->getSkin() : NULL;
  319. return skin ? skin->getMatrixPalette() : NULL;
  320. }
  321. unsigned int RenderState::autoBindingGetMatrixPaletteSize() const
  322. {
  323. Model* model = _nodeBinding ? _nodeBinding->getModel() : NULL;
  324. MeshSkin* skin = model ? model->getSkin() : NULL;
  325. return skin ? skin->getMatrixPaletteSize() : 0;
  326. }
  327. const Vector3& RenderState::autoBindingGetAmbientColor() const
  328. {
  329. Scene* scene = _nodeBinding ? _nodeBinding->getScene() : NULL;
  330. return scene ? scene->getAmbientColor() : Vector3::zero();
  331. }
  332. const Vector3& RenderState::autoBindingGetLightColor() const
  333. {
  334. Scene* scene = _nodeBinding ? _nodeBinding->getScene() : NULL;
  335. return scene ? scene->getLightColor() : Vector3::one();
  336. }
  337. const Vector3& RenderState::autoBindingGetLightDirection() const
  338. {
  339. static Vector3 down(0, -1, 0);
  340. Scene* scene = _nodeBinding ? _nodeBinding->getScene() : NULL;
  341. return scene ? scene->getLightDirection() : down;
  342. }
  343. void RenderState::bind(Pass* pass)
  344. {
  345. GP_ASSERT(pass);
  346. // Get the combined modified state bits for our RenderState hierarchy.
  347. long stateOverrideBits = _state ? _state->_bits : 0;
  348. RenderState* rs = _parent;
  349. while (rs)
  350. {
  351. if (rs->_state)
  352. {
  353. stateOverrideBits |= rs->_state->_bits;
  354. }
  355. rs = rs->_parent;
  356. }
  357. // Restore renderer state to its default, except for explicitly specified states
  358. StateBlock::restore(stateOverrideBits);
  359. // Apply parameter bindings and renderer state for the entire hierarchy, top-down.
  360. rs = NULL;
  361. Effect* effect = pass->getEffect();
  362. while ((rs = getTopmost(rs)))
  363. {
  364. for (size_t i = 0, count = rs->_parameters.size(); i < count; ++i)
  365. {
  366. GP_ASSERT(rs->_parameters[i]);
  367. rs->_parameters[i]->bind(effect);
  368. }
  369. if (rs->_state)
  370. {
  371. rs->_state->bindNoRestore();
  372. }
  373. }
  374. }
  375. RenderState* RenderState::getTopmost(RenderState* below)
  376. {
  377. RenderState* rs = this;
  378. if (rs == below)
  379. {
  380. // Nothing below ourself.
  381. return NULL;
  382. }
  383. while (rs)
  384. {
  385. if (rs->_parent == below || rs->_parent == NULL)
  386. {
  387. // Stop traversing up here.
  388. return rs;
  389. }
  390. rs = rs->_parent;
  391. }
  392. return NULL;
  393. }
  394. void RenderState::cloneInto(RenderState* renderState, NodeCloneContext& context) const
  395. {
  396. GP_ASSERT(renderState);
  397. // Clone parameters
  398. for (std::map<std::string, std::string>::const_iterator it = _autoBindings.begin(); it != _autoBindings.end(); ++it)
  399. {
  400. renderState->setParameterAutoBinding(it->first.c_str(), it->second.c_str());
  401. }
  402. for (std::vector<MaterialParameter*>::const_iterator it = _parameters.begin(); it != _parameters.end(); ++it)
  403. {
  404. const MaterialParameter* param = *it;
  405. GP_ASSERT(param);
  406. // If this parameter is a method binding auto binding, don't clone it - it will get setup automatically
  407. // via the cloned auto bindings instead.
  408. if (param->_type == MaterialParameter::METHOD && param->_value.method && param->_value.method->_autoBinding)
  409. continue;
  410. MaterialParameter* paramCopy = new MaterialParameter(param->getName());
  411. param->cloneInto(paramCopy);
  412. renderState->_parameters.push_back(paramCopy);
  413. }
  414. // Clone our state block
  415. if (_state)
  416. {
  417. _state->cloneInto(renderState->getStateBlock());
  418. }
  419. // Notes:
  420. // 1. _nodeBinding should not be set here, it should be set by the caller.
  421. // 2. _parent should not be set here, since it's set in the constructor of Technique and Pass.
  422. }
  423. RenderState::StateBlock::StateBlock()
  424. : _cullFaceEnabled(false), _depthTestEnabled(false), _depthWriteEnabled(true), _depthFunction(RenderState::DEPTH_LESS),
  425. _blendEnabled(false), _blendSrc(RenderState::BLEND_ONE), _blendDst(RenderState::BLEND_ZERO),
  426. _bits(0L)
  427. {
  428. }
  429. RenderState::StateBlock::StateBlock(const StateBlock& copy)
  430. {
  431. // Hidden
  432. }
  433. RenderState::StateBlock::~StateBlock()
  434. {
  435. }
  436. RenderState::StateBlock* RenderState::StateBlock::create()
  437. {
  438. return new RenderState::StateBlock();
  439. }
  440. void RenderState::StateBlock::bind()
  441. {
  442. // When the public bind() is called with no RenderState object passed in,
  443. // we assume we are being called to bind the state of a single StateBlock,
  444. // irrespective of whether it belongs to a hierarchy of RenderStates.
  445. // Therefore, we call restore() here with only this StateBlock's override
  446. // bits to restore state before applying the new state.
  447. StateBlock::restore(_bits);
  448. bindNoRestore();
  449. }
  450. void RenderState::StateBlock::bindNoRestore()
  451. {
  452. GP_ASSERT(_defaultState);
  453. // Update any state that differs from _defaultState and flip _defaultState bits
  454. if ((_bits & RS_BLEND) && (_blendEnabled != _defaultState->_blendEnabled))
  455. {
  456. if (_blendEnabled)
  457. GL_ASSERT( glEnable(GL_BLEND) );
  458. else
  459. GL_ASSERT( glDisable(GL_BLEND) );
  460. _defaultState->_blendEnabled = _blendEnabled;
  461. }
  462. if ((_bits & RS_BLEND_FUNC) && (_blendSrc != _defaultState->_blendSrc || _blendDst != _defaultState->_blendDst))
  463. {
  464. GL_ASSERT( glBlendFunc((GLenum)_blendSrc, (GLenum)_blendDst) );
  465. _defaultState->_blendSrc = _blendSrc;
  466. _defaultState->_blendDst = _blendDst;
  467. }
  468. if ((_bits & RS_CULL_FACE) && (_cullFaceEnabled != _defaultState->_cullFaceEnabled))
  469. {
  470. if (_cullFaceEnabled)
  471. GL_ASSERT( glEnable(GL_CULL_FACE) );
  472. else
  473. GL_ASSERT( glDisable(GL_CULL_FACE) );
  474. _defaultState->_cullFaceEnabled = _cullFaceEnabled;
  475. }
  476. if ((_bits & RS_CULL_FACE_SIDE) && (_cullFaceSide != _defaultState->_cullFaceSide))
  477. {
  478. GL_ASSERT( glCullFace((GLenum)_cullFaceSide) );
  479. _defaultState->_cullFaceSide = _cullFaceSide;
  480. }
  481. if ((_bits & RS_DEPTH_TEST) && (_depthTestEnabled != _defaultState->_depthTestEnabled))
  482. {
  483. if (_depthTestEnabled)
  484. GL_ASSERT( glEnable(GL_DEPTH_TEST) );
  485. else
  486. GL_ASSERT( glDisable(GL_DEPTH_TEST) );
  487. _defaultState->_depthTestEnabled = _depthTestEnabled;
  488. }
  489. if ((_bits & RS_DEPTH_WRITE) && (_depthWriteEnabled != _defaultState->_depthWriteEnabled))
  490. {
  491. GL_ASSERT( glDepthMask(_depthWriteEnabled ? GL_TRUE : GL_FALSE) );
  492. _defaultState->_depthWriteEnabled = _depthWriteEnabled;
  493. }
  494. if ((_bits & RS_DEPTH_FUNC) && (_depthFunction != _defaultState->_depthFunction))
  495. {
  496. GL_ASSERT( glDepthFunc((GLenum)_depthFunction) );
  497. _defaultState->_depthFunction = _depthFunction;
  498. }
  499. _defaultState->_bits |= _bits;
  500. }
  501. void RenderState::StateBlock::restore(long stateOverrideBits)
  502. {
  503. GP_ASSERT(_defaultState);
  504. // If there is no state to restore (i.e. no non-default state), do nothing.
  505. if (_defaultState->_bits == 0)
  506. {
  507. return;
  508. }
  509. // Restore any state that is not overridden and is not default
  510. if (!(stateOverrideBits & RS_BLEND) && (_defaultState->_bits & RS_BLEND))
  511. {
  512. GL_ASSERT( glDisable(GL_BLEND) );
  513. _defaultState->_bits &= ~RS_BLEND;
  514. _defaultState->_blendEnabled = false;
  515. }
  516. if (!(stateOverrideBits & RS_BLEND_FUNC) && (_defaultState->_bits & RS_BLEND_FUNC))
  517. {
  518. GL_ASSERT( glBlendFunc(GL_ONE, GL_ZERO) );
  519. _defaultState->_bits &= ~RS_BLEND_FUNC;
  520. _defaultState->_blendSrc = RenderState::BLEND_ONE;
  521. _defaultState->_blendDst = RenderState::BLEND_ZERO;
  522. }
  523. if (!(stateOverrideBits & RS_CULL_FACE) && (_defaultState->_bits & RS_CULL_FACE))
  524. {
  525. GL_ASSERT( glDisable(GL_CULL_FACE) );
  526. _defaultState->_bits &= ~RS_CULL_FACE;
  527. _defaultState->_cullFaceEnabled = false;
  528. }
  529. if (!(stateOverrideBits & RS_CULL_FACE_SIDE) && (_defaultState->_bits & RS_CULL_FACE_SIDE))
  530. {
  531. GL_ASSERT( glCullFace((GLenum)GL_BACK) );
  532. _defaultState->_bits &= ~RS_CULL_FACE_SIDE;
  533. _defaultState->_cullFaceSide = RenderState::CULL_FACE_SIDE_BACK;
  534. }
  535. if (!(stateOverrideBits & RS_DEPTH_TEST) && (_defaultState->_bits & RS_DEPTH_TEST))
  536. {
  537. GL_ASSERT( glDisable(GL_DEPTH_TEST) );
  538. _defaultState->_bits &= ~RS_DEPTH_TEST;
  539. _defaultState->_depthTestEnabled = false;
  540. }
  541. if (!(stateOverrideBits & RS_DEPTH_WRITE) && (_defaultState->_bits & RS_DEPTH_WRITE))
  542. {
  543. GL_ASSERT( glDepthMask(GL_TRUE) );
  544. _defaultState->_bits &= ~RS_DEPTH_WRITE;
  545. _defaultState->_depthWriteEnabled = true;
  546. }
  547. if (!(stateOverrideBits & RS_DEPTH_FUNC) && (_defaultState->_bits & RS_DEPTH_FUNC))
  548. {
  549. GL_ASSERT( glDepthFunc((GLenum)GL_LESS) );
  550. _defaultState->_bits &= ~RS_DEPTH_FUNC;
  551. _defaultState->_depthFunction = RenderState::DEPTH_LESS;
  552. }
  553. }
  554. void RenderState::StateBlock::enableDepthWrite()
  555. {
  556. GP_ASSERT(_defaultState);
  557. // Internal method used by Game::clear() to restore depth writing before a
  558. // clear operation. This is necessary if the last code to draw before the
  559. // next frame leaves depth writing disabled.
  560. if (!_defaultState->_depthWriteEnabled)
  561. {
  562. GL_ASSERT( glDepthMask(GL_TRUE) );
  563. _defaultState->_bits &= ~RS_DEPTH_WRITE;
  564. _defaultState->_depthWriteEnabled = true;
  565. }
  566. }
  567. void RenderState::StateBlock::cloneInto(StateBlock* state)
  568. {
  569. GP_ASSERT(state);
  570. state->_cullFaceEnabled = _cullFaceEnabled;
  571. state->_depthTestEnabled = _depthTestEnabled;
  572. state->_depthWriteEnabled = _depthWriteEnabled;
  573. state->_depthFunction = _depthFunction;
  574. state->_blendEnabled = _blendEnabled;
  575. state->_blendSrc = _blendSrc;
  576. state->_blendDst = _blendDst;
  577. state->_cullFaceSide = _cullFaceSide;
  578. state->_bits = _bits;
  579. }
  580. static bool parseBoolean(const char* value)
  581. {
  582. GP_ASSERT(value);
  583. if (strlen(value) == 4)
  584. {
  585. return (
  586. tolower(value[0]) == 't' &&
  587. tolower(value[1]) == 'r' &&
  588. tolower(value[2]) == 'u' &&
  589. tolower(value[3]) == 'e' );
  590. }
  591. return false;
  592. }
  593. static RenderState::Blend parseBlend(const char* value)
  594. {
  595. GP_ASSERT(value);
  596. // Convert the string to uppercase for comparison.
  597. std::string upper(value);
  598. std::transform(upper.begin(), upper.end(), upper.begin(), (int(*)(int))toupper);
  599. if (upper == "ZERO")
  600. return RenderState::BLEND_ZERO;
  601. else if (upper == "ONE")
  602. return RenderState::BLEND_ONE;
  603. else if (upper == "SRC_COLOR")
  604. return RenderState::BLEND_SRC_COLOR;
  605. else if (upper == "ONE_MINUS_SRC_COLOR")
  606. return RenderState::BLEND_ONE_MINUS_SRC_COLOR;
  607. else if (upper == "DST_COLOR")
  608. return RenderState::BLEND_DST_COLOR;
  609. else if (upper == "ONE_MINUS_DST_COLOR")
  610. return RenderState::BLEND_ONE_MINUS_DST_COLOR;
  611. else if (upper == "SRC_ALPHA")
  612. return RenderState::BLEND_SRC_ALPHA;
  613. else if (upper == "ONE_MINUS_SRC_ALPHA")
  614. return RenderState::BLEND_ONE_MINUS_SRC_ALPHA;
  615. else if (upper == "DST_ALPHA")
  616. return RenderState::BLEND_DST_ALPHA;
  617. else if (upper == "ONE_MINUS_DST_ALPHA")
  618. return RenderState::BLEND_ONE_MINUS_DST_ALPHA;
  619. else if (upper == "CONSTANT_ALPHA")
  620. return RenderState::BLEND_CONSTANT_ALPHA;
  621. else if (upper == "ONE_MINUS_CONSTANT_ALPHA")
  622. return RenderState::BLEND_ONE_MINUS_CONSTANT_ALPHA;
  623. else if (upper == "SRC_ALPHA_SATURATE")
  624. return RenderState::BLEND_SRC_ALPHA_SATURATE;
  625. else
  626. {
  627. GP_ERROR("Unsupported blend value (%s). (Will default to BLEND_ONE if errors are treated as warnings)", value);
  628. return RenderState::BLEND_ONE;
  629. }
  630. }
  631. static RenderState::DepthFunction parseDepthFunc(const char* value)
  632. {
  633. GP_ASSERT(value);
  634. // Convert string to uppercase for comparison
  635. std::string upper(value);
  636. std::transform(upper.begin(), upper.end(), upper.begin(), (int(*)(int))toupper);
  637. if (upper == "NEVER")
  638. return RenderState::DEPTH_NEVER;
  639. else if (upper == "LESS")
  640. return RenderState::DEPTH_LESS;
  641. else if (upper == "EQUAL")
  642. return RenderState::DEPTH_EQUAL;
  643. else if (upper == "LEQUAL")
  644. return RenderState::DEPTH_LEQUAL;
  645. else if (upper == "GREATER")
  646. return RenderState::DEPTH_GREATER;
  647. else if (upper == "NOTEQUAL")
  648. return RenderState::DEPTH_NOTEQUAL;
  649. else if (upper == "GEQUAL")
  650. return RenderState::DEPTH_GEQUAL;
  651. else if (upper == "ALWAYS")
  652. return RenderState::DEPTH_ALWAYS;
  653. else
  654. {
  655. GP_ERROR("Unsupported depth function value (%s). Will default to DEPTH_LESS if errors are treated as warnings)", value);
  656. return RenderState::DEPTH_LESS;
  657. }
  658. }
  659. static RenderState::CullFaceSide parseCullFaceSide(const char* value)
  660. {
  661. GP_ASSERT(value);
  662. // Convert string to uppercase for comparison
  663. std::string upper(value);
  664. std::transform(upper.begin(), upper.end(), upper.begin(), (int(*)(int))toupper);
  665. if (upper == "BACK")
  666. return RenderState::CULL_FACE_SIDE_BACK;
  667. else if (upper == "FRONT")
  668. return RenderState::CULL_FACE_SIDE_FRONT;
  669. else if (upper == "FRONT_AND_BACK")
  670. return RenderState::CULL_FACE_SIDE_FRONT_AND_BACK;
  671. else
  672. {
  673. GP_ERROR("Unsupported cull face side value (%s). Will default to BACK if errors are treated as warnings)", value);
  674. return RenderState::CULL_FACE_SIDE_BACK;
  675. }
  676. }
  677. void RenderState::StateBlock::setState(const char* name, const char* value)
  678. {
  679. GP_ASSERT(name);
  680. if (strcmp(name, "blend") == 0)
  681. {
  682. setBlend(parseBoolean(value));
  683. }
  684. else if (strcmp(name, "blendSrc") == 0 || strcmp(name, "srcBlend") == 0 ) // Leaving srcBlend for backward compat.
  685. {
  686. setBlendSrc(parseBlend(value));
  687. }
  688. else if (strcmp(name, "blendDst") == 0 || strcmp(name, "dstBlend") == 0) // // Leaving dstBlend for backward compat.
  689. {
  690. setBlendDst(parseBlend(value));
  691. }
  692. else if (strcmp(name, "cullFace") == 0)
  693. {
  694. setCullFace(parseBoolean(value));
  695. }
  696. else if (strcmp(name, "cullFaceSide") == 0)
  697. {
  698. setCullFaceSide(parseCullFaceSide(value));
  699. }
  700. else if (strcmp(name, "depthTest") == 0)
  701. {
  702. setDepthTest(parseBoolean(value));
  703. }
  704. else if (strcmp(name, "depthWrite") == 0)
  705. {
  706. setDepthWrite(parseBoolean(value));
  707. }
  708. else if (strcmp(name, "depthFunc") == 0)
  709. {
  710. setDepthFunction(parseDepthFunc(value));
  711. }
  712. else
  713. {
  714. GP_ERROR("Unsupported render state string '%s'.", name);
  715. }
  716. }
  717. void RenderState::StateBlock::setBlend(bool enabled)
  718. {
  719. _blendEnabled = enabled;
  720. if (!enabled)
  721. {
  722. _bits &= ~RS_BLEND;
  723. }
  724. else
  725. {
  726. _bits |= RS_BLEND;
  727. }
  728. }
  729. void RenderState::StateBlock::setBlendSrc(Blend blend)
  730. {
  731. _blendSrc = blend;
  732. if (_blendSrc == BLEND_ONE && _blendDst == BLEND_ZERO)
  733. {
  734. // Default blend func
  735. _bits &= ~RS_BLEND_FUNC;
  736. }
  737. else
  738. {
  739. _bits |= RS_BLEND_FUNC;
  740. }
  741. }
  742. void RenderState::StateBlock::setBlendDst(Blend blend)
  743. {
  744. _blendDst = blend;
  745. if (_blendSrc == BLEND_ONE && _blendDst == BLEND_ZERO)
  746. {
  747. // Default blend func
  748. _bits &= ~RS_BLEND_FUNC;
  749. }
  750. else
  751. {
  752. _bits |= RS_BLEND_FUNC;
  753. }
  754. }
  755. void RenderState::StateBlock::setCullFace(bool enabled)
  756. {
  757. _cullFaceEnabled = enabled;
  758. if (!enabled)
  759. {
  760. _bits &= ~RS_CULL_FACE;
  761. }
  762. else
  763. {
  764. _bits |= RS_CULL_FACE;
  765. }
  766. }
  767. void RenderState::StateBlock::setCullFaceSide(CullFaceSide side)
  768. {
  769. _cullFaceSide = side;
  770. if (_cullFaceSide == CULL_FACE_SIDE_BACK)
  771. {
  772. // Default cull side
  773. _bits &= ~RS_CULL_FACE_SIDE;
  774. }
  775. else
  776. {
  777. _bits |= RS_CULL_FACE_SIDE;
  778. }
  779. }
  780. void RenderState::StateBlock::setDepthTest(bool enabled)
  781. {
  782. _depthTestEnabled = enabled;
  783. if (!enabled)
  784. {
  785. _bits &= ~RS_DEPTH_TEST;
  786. }
  787. else
  788. {
  789. _bits |= RS_DEPTH_TEST;
  790. }
  791. }
  792. void RenderState::StateBlock::setDepthWrite(bool enabled)
  793. {
  794. _depthWriteEnabled = enabled;
  795. if (enabled)
  796. {
  797. _bits &= ~RS_DEPTH_WRITE;
  798. }
  799. else
  800. {
  801. _bits |= RS_DEPTH_WRITE;
  802. }
  803. }
  804. void RenderState::StateBlock::setDepthFunction(DepthFunction func)
  805. {
  806. _depthFunction = func;
  807. if (_depthFunction == DEPTH_LESS)
  808. {
  809. // Default depth function
  810. _bits &= ~RS_DEPTH_FUNC;
  811. }
  812. else
  813. {
  814. _bits |= RS_DEPTH_FUNC;
  815. }
  816. }
  817. }