RenderState.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  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. #define RS_STENCIL_TEST 128
  17. #define RS_STENCIL_WRITE 256
  18. #define RS_STENCIL_FUNC 512
  19. #define RS_STENCIL_OP 1024
  20. #define RS_ALL_ONES 0xFFFFFFFF
  21. namespace gameplay
  22. {
  23. RenderState::StateBlock* RenderState::StateBlock::_defaultState = NULL;
  24. std::vector<RenderState::ResolveAutoBindingCallback> RenderState::_customAutoBindingResolvers;
  25. RenderState::RenderState()
  26. : _nodeBinding(NULL), _state(NULL), _parent(NULL)
  27. {
  28. }
  29. RenderState::~RenderState()
  30. {
  31. SAFE_RELEASE(_state);
  32. // Destroy all the material parameters
  33. for (size_t i = 0, count = _parameters.size(); i < count; ++i)
  34. {
  35. SAFE_RELEASE(_parameters[i]);
  36. }
  37. }
  38. void RenderState::initialize()
  39. {
  40. if (StateBlock::_defaultState == NULL)
  41. {
  42. StateBlock::_defaultState = StateBlock::create();
  43. }
  44. }
  45. void RenderState::finalize()
  46. {
  47. SAFE_RELEASE(StateBlock::_defaultState);
  48. }
  49. void RenderState::registerAutoBindingResolver(ResolveAutoBindingCallback callback)
  50. {
  51. _customAutoBindingResolvers.push_back(callback);
  52. }
  53. MaterialParameter* RenderState::getParameter(const char* name) const
  54. {
  55. GP_ASSERT(name);
  56. // Search for an existing parameter with this name.
  57. MaterialParameter* param;
  58. for (size_t i = 0, count = _parameters.size(); i < count; ++i)
  59. {
  60. param = _parameters[i];
  61. GP_ASSERT(param);
  62. if (strcmp(param->getName(), name) == 0)
  63. {
  64. return param;
  65. }
  66. }
  67. // Create a new parameter and store it in our list.
  68. param = new MaterialParameter(name);
  69. _parameters.push_back(param);
  70. return param;
  71. }
  72. void RenderState::clearParameter(const char* name)
  73. {
  74. for (size_t i = 0, count = _parameters.size(); i < count; ++i)
  75. {
  76. MaterialParameter* p = _parameters[i];
  77. if (p->_name == name)
  78. {
  79. _parameters.erase(_parameters.begin() + i);
  80. SAFE_RELEASE(p);
  81. break;
  82. }
  83. }
  84. }
  85. /**
  86. * @script{ignore}
  87. */
  88. const char* autoBindingToString(RenderState::AutoBinding autoBinding)
  89. {
  90. // NOTE: As new AutoBinding values are added, this switch statement must be updatd.
  91. switch (autoBinding)
  92. {
  93. case RenderState::NONE:
  94. return NULL;
  95. case RenderState::VIEW_MATRIX:
  96. return "VIEW_MATRIX";
  97. case RenderState::PROJECTION_MATRIX:
  98. return "PROJECTION_MATRIX";
  99. case RenderState::WORLD_VIEW_MATRIX:
  100. return "WORLD_VIEW_MATRIX";
  101. case RenderState::VIEW_PROJECTION_MATRIX:
  102. return "VIEW_PROJECTION_MATRIX";
  103. case RenderState::WORLD_VIEW_PROJECTION_MATRIX:
  104. return "WORLD_VIEW_PROJECTION_MATRIX";
  105. case RenderState::INVERSE_TRANSPOSE_WORLD_MATRIX:
  106. return "INVERSE_TRANSPOSE_WORLD_MATRIX";
  107. case RenderState::INVERSE_TRANSPOSE_WORLD_VIEW_MATRIX:
  108. return "INVERSE_TRANSPOSE_WORLD_VIEW_MATRIX";
  109. case RenderState::CAMERA_WORLD_POSITION:
  110. return "CAMERA_WORLD_POSITION";
  111. case RenderState::CAMERA_VIEW_POSITION:
  112. return "CAMERA_VIEW_POSITION";
  113. case RenderState::MATRIX_PALETTE:
  114. return "MATRIX_PALETTE";
  115. case RenderState::SCENE_AMBIENT_COLOR:
  116. return "SCENE_AMBIENT_COLOR";
  117. case RenderState::SCENE_LIGHT_COLOR:
  118. return "SCENE_LIGHT_COLOR";
  119. case RenderState::SCENE_LIGHT_DIRECTION:
  120. return "SCENE_LIGHT_DIRECTION";
  121. default:
  122. return "";
  123. }
  124. }
  125. void RenderState::setParameterAutoBinding(const char* name, AutoBinding autoBinding)
  126. {
  127. setParameterAutoBinding(name, autoBindingToString(autoBinding));
  128. }
  129. void RenderState::setParameterAutoBinding(const char* name, const char* autoBinding)
  130. {
  131. GP_ASSERT(name);
  132. GP_ASSERT(autoBinding);
  133. if (autoBinding == NULL)
  134. {
  135. // Remove an existing auto-binding
  136. std::map<std::string, std::string>::iterator itr = _autoBindings.find(name);
  137. if (itr != _autoBindings.end())
  138. _autoBindings.erase(itr);
  139. }
  140. else
  141. {
  142. // Add/update an auto-binding
  143. _autoBindings[name] = autoBinding;
  144. }
  145. // If we already have a node binding set, pass it to our handler now
  146. if (_nodeBinding)
  147. {
  148. applyAutoBinding(name, autoBinding);
  149. }
  150. }
  151. void RenderState::setStateBlock(StateBlock* state)
  152. {
  153. if (_state != state)
  154. {
  155. SAFE_RELEASE(_state);
  156. _state = state;
  157. if (_state)
  158. {
  159. _state->addRef();
  160. }
  161. }
  162. }
  163. RenderState::StateBlock* RenderState::getStateBlock() const
  164. {
  165. if (_state == NULL)
  166. {
  167. _state = StateBlock::create();
  168. }
  169. return _state;
  170. }
  171. void RenderState::setNodeBinding(Node* node)
  172. {
  173. if (_nodeBinding != node)
  174. {
  175. _nodeBinding = node;
  176. if (_nodeBinding)
  177. {
  178. // Apply all existing auto-bindings using this node.
  179. std::map<std::string, std::string>::const_iterator itr = _autoBindings.begin();
  180. while (itr != _autoBindings.end())
  181. {
  182. applyAutoBinding(itr->first.c_str(), itr->second.c_str());
  183. ++itr;
  184. }
  185. }
  186. }
  187. }
  188. void RenderState::applyAutoBinding(const char* uniformName, const char* autoBinding)
  189. {
  190. GP_ASSERT(_nodeBinding);
  191. MaterialParameter* param = getParameter(uniformName);
  192. GP_ASSERT(param);
  193. bool bound = false;
  194. // First attempt to resolve the binding using custom registered resolvers.
  195. if (_customAutoBindingResolvers.size() > 0)
  196. {
  197. for (size_t i = 0, count = _customAutoBindingResolvers.size(); i < count; ++i)
  198. {
  199. if (_customAutoBindingResolvers[i](autoBinding, _nodeBinding, param))
  200. {
  201. // Handled by custom auto binding resolver
  202. bound = true;
  203. break;
  204. }
  205. }
  206. }
  207. // Perform built-in resolution
  208. if (!bound)
  209. {
  210. bound = true;
  211. if (strcmp(autoBinding, "WORLD_MATRIX") == 0)
  212. {
  213. param->bindValue(this, &RenderState::autoBindingGetWorldMatrix);
  214. }
  215. else if (strcmp(autoBinding, "VIEW_MATRIX") == 0)
  216. {
  217. param->bindValue(this, &RenderState::autoBindingGetViewMatrix);
  218. }
  219. else if (strcmp(autoBinding, "PROJECTION_MATRIX") == 0)
  220. {
  221. param->bindValue(this, &RenderState::autoBindingGetProjectionMatrix);
  222. }
  223. else if (strcmp(autoBinding, "WORLD_VIEW_MATRIX") == 0)
  224. {
  225. param->bindValue(this, &RenderState::autoBindingGetWorldViewMatrix);
  226. }
  227. else if (strcmp(autoBinding, "VIEW_PROJECTION_MATRIX") == 0)
  228. {
  229. param->bindValue(this, &RenderState::autoBindingGetViewProjectionMatrix);
  230. }
  231. else if (strcmp(autoBinding, "WORLD_VIEW_PROJECTION_MATRIX") == 0)
  232. {
  233. param->bindValue(this, &RenderState::autoBindingGetWorldViewProjectionMatrix);
  234. }
  235. else if (strcmp(autoBinding, "INVERSE_TRANSPOSE_WORLD_MATRIX") == 0)
  236. {
  237. param->bindValue(this, &RenderState::autoBindingGetInverseTransposeWorldMatrix);
  238. }
  239. else if (strcmp(autoBinding, "INVERSE_TRANSPOSE_WORLD_VIEW_MATRIX") == 0)
  240. {
  241. param->bindValue(this, &RenderState::autoBindingGetInverseTransposeWorldViewMatrix);
  242. }
  243. else if (strcmp(autoBinding, "CAMERA_WORLD_POSITION") == 0)
  244. {
  245. param->bindValue(this, &RenderState::autoBindingGetCameraWorldPosition);
  246. }
  247. else if (strcmp(autoBinding, "CAMERA_VIEW_POSITION") == 0)
  248. {
  249. param->bindValue(this, &RenderState::autoBindingGetCameraViewPosition);
  250. }
  251. else if (strcmp(autoBinding, "MATRIX_PALETTE") == 0)
  252. {
  253. param->bindValue(this, &RenderState::autoBindingGetMatrixPalette, &RenderState::autoBindingGetMatrixPaletteSize);
  254. }
  255. else if (strcmp(autoBinding, "SCENE_AMBIENT_COLOR") == 0)
  256. {
  257. param->bindValue(this, &RenderState::autoBindingGetAmbientColor);
  258. }
  259. else if (strcmp(autoBinding, "SCENE_LIGHT_COLOR") == 0)
  260. {
  261. param->bindValue(this, &RenderState::autoBindingGetLightColor);
  262. }
  263. else if (strcmp(autoBinding, "SCENE_LIGHT_DIRECTION") == 0)
  264. {
  265. param->bindValue(this, &RenderState::autoBindingGetLightDirection);
  266. }
  267. else
  268. {
  269. bound = false;
  270. GP_WARN("Unsupported auto binding type (%d).", autoBinding);
  271. }
  272. }
  273. if (bound)
  274. {
  275. // Mark parameter as an auto binding
  276. if (param->_type == MaterialParameter::METHOD && param->_value.method)
  277. param->_value.method->_autoBinding = true;
  278. }
  279. }
  280. const Matrix& RenderState::autoBindingGetWorldMatrix() const
  281. {
  282. return _nodeBinding ? _nodeBinding->getWorldMatrix() : Matrix::identity();
  283. }
  284. const Matrix& RenderState::autoBindingGetViewMatrix() const
  285. {
  286. return _nodeBinding ? _nodeBinding->getViewMatrix() : Matrix::identity();
  287. }
  288. const Matrix& RenderState::autoBindingGetProjectionMatrix() const
  289. {
  290. return _nodeBinding ? _nodeBinding->getProjectionMatrix() : Matrix::identity();
  291. }
  292. const Matrix& RenderState::autoBindingGetWorldViewMatrix() const
  293. {
  294. return _nodeBinding ? _nodeBinding->getWorldViewMatrix() : Matrix::identity();
  295. }
  296. const Matrix& RenderState::autoBindingGetViewProjectionMatrix() const
  297. {
  298. return _nodeBinding ? _nodeBinding->getViewProjectionMatrix() : Matrix::identity();
  299. }
  300. const Matrix& RenderState::autoBindingGetWorldViewProjectionMatrix() const
  301. {
  302. return _nodeBinding ? _nodeBinding->getWorldViewProjectionMatrix() : Matrix::identity();
  303. }
  304. const Matrix& RenderState::autoBindingGetInverseTransposeWorldMatrix() const
  305. {
  306. return _nodeBinding ? _nodeBinding->getInverseTransposeWorldMatrix() : Matrix::identity();
  307. }
  308. const Matrix& RenderState::autoBindingGetInverseTransposeWorldViewMatrix() const
  309. {
  310. return _nodeBinding ? _nodeBinding->getInverseTransposeWorldViewMatrix() : Matrix::identity();
  311. }
  312. Vector3 RenderState::autoBindingGetCameraWorldPosition() const
  313. {
  314. return _nodeBinding ? _nodeBinding->getActiveCameraTranslationWorld() : Vector3::zero();
  315. }
  316. Vector3 RenderState::autoBindingGetCameraViewPosition() const
  317. {
  318. return _nodeBinding ? _nodeBinding->getActiveCameraTranslationView() : Vector3::zero();
  319. }
  320. const Vector4* RenderState::autoBindingGetMatrixPalette() const
  321. {
  322. Model* model = _nodeBinding ? _nodeBinding->getModel() : NULL;
  323. MeshSkin* skin = model ? model->getSkin() : NULL;
  324. return skin ? skin->getMatrixPalette() : NULL;
  325. }
  326. unsigned int RenderState::autoBindingGetMatrixPaletteSize() const
  327. {
  328. Model* model = _nodeBinding ? _nodeBinding->getModel() : NULL;
  329. MeshSkin* skin = model ? model->getSkin() : NULL;
  330. return skin ? skin->getMatrixPaletteSize() : 0;
  331. }
  332. const Vector3& RenderState::autoBindingGetAmbientColor() const
  333. {
  334. Scene* scene = _nodeBinding ? _nodeBinding->getScene() : NULL;
  335. return scene ? scene->getAmbientColor() : Vector3::zero();
  336. }
  337. const Vector3& RenderState::autoBindingGetLightColor() const
  338. {
  339. Scene* scene = _nodeBinding ? _nodeBinding->getScene() : NULL;
  340. return scene ? scene->getLightColor() : Vector3::one();
  341. }
  342. const Vector3& RenderState::autoBindingGetLightDirection() const
  343. {
  344. static Vector3 down(0, -1, 0);
  345. Scene* scene = _nodeBinding ? _nodeBinding->getScene() : NULL;
  346. if (scene) {
  347. static Vector3 lightDirection;
  348. lightDirection.set(scene->getLightDirection());
  349. _nodeBinding->getViewMatrix().transformVector(&lightDirection);
  350. return lightDirection;
  351. }
  352. return down;
  353. }
  354. void RenderState::bind(Pass* pass)
  355. {
  356. GP_ASSERT(pass);
  357. // Get the combined modified state bits for our RenderState hierarchy.
  358. long stateOverrideBits = _state ? _state->_bits : 0;
  359. RenderState* rs = _parent;
  360. while (rs)
  361. {
  362. if (rs->_state)
  363. {
  364. stateOverrideBits |= rs->_state->_bits;
  365. }
  366. rs = rs->_parent;
  367. }
  368. // Restore renderer state to its default, except for explicitly specified states
  369. StateBlock::restore(stateOverrideBits);
  370. // Apply parameter bindings and renderer state for the entire hierarchy, top-down.
  371. rs = NULL;
  372. Effect* effect = pass->getEffect();
  373. while ((rs = getTopmost(rs)))
  374. {
  375. for (size_t i = 0, count = rs->_parameters.size(); i < count; ++i)
  376. {
  377. GP_ASSERT(rs->_parameters[i]);
  378. rs->_parameters[i]->bind(effect);
  379. }
  380. if (rs->_state)
  381. {
  382. rs->_state->bindNoRestore();
  383. }
  384. }
  385. }
  386. RenderState* RenderState::getTopmost(RenderState* below)
  387. {
  388. RenderState* rs = this;
  389. if (rs == below)
  390. {
  391. // Nothing below ourself.
  392. return NULL;
  393. }
  394. while (rs)
  395. {
  396. if (rs->_parent == below || rs->_parent == NULL)
  397. {
  398. // Stop traversing up here.
  399. return rs;
  400. }
  401. rs = rs->_parent;
  402. }
  403. return NULL;
  404. }
  405. void RenderState::cloneInto(RenderState* renderState, NodeCloneContext& context) const
  406. {
  407. GP_ASSERT(renderState);
  408. // Clone parameters
  409. for (std::map<std::string, std::string>::const_iterator it = _autoBindings.begin(); it != _autoBindings.end(); ++it)
  410. {
  411. renderState->setParameterAutoBinding(it->first.c_str(), it->second.c_str());
  412. }
  413. for (std::vector<MaterialParameter*>::const_iterator it = _parameters.begin(); it != _parameters.end(); ++it)
  414. {
  415. const MaterialParameter* param = *it;
  416. GP_ASSERT(param);
  417. // If this parameter is a method binding auto binding, don't clone it - it will get setup automatically
  418. // via the cloned auto bindings instead.
  419. if (param->_type == MaterialParameter::METHOD && param->_value.method && param->_value.method->_autoBinding)
  420. continue;
  421. MaterialParameter* paramCopy = new MaterialParameter(param->getName());
  422. param->cloneInto(paramCopy);
  423. renderState->_parameters.push_back(paramCopy);
  424. }
  425. // Clone our state block
  426. if (_state)
  427. {
  428. _state->cloneInto(renderState->getStateBlock());
  429. }
  430. // Notes:
  431. // 1. _nodeBinding should not be set here, it should be set by the caller.
  432. // 2. _parent should not be set here, since it's set in the constructor of Technique and Pass.
  433. }
  434. RenderState::StateBlock::StateBlock()
  435. : _cullFaceEnabled(false), _depthTestEnabled(false), _depthWriteEnabled(true), _depthFunction(RenderState::DEPTH_LESS),
  436. _blendEnabled(false), _blendSrc(RenderState::BLEND_ONE), _blendDst(RenderState::BLEND_ZERO),
  437. _stencilTestEnabled(false), _stencilWrite(RS_ALL_ONES),
  438. _stencilFunction(RenderState::STENCIL_ALWAYS), _stencilFunctionRef(0), _stencilFunctionMask(RS_ALL_ONES),
  439. _stencilOpSfail(RenderState::STENCIL_OP_KEEP), _stencilOpDpfail(RenderState::STENCIL_OP_KEEP), _stencilOpDppass(RenderState::STENCIL_OP_KEEP),
  440. _bits(0L)
  441. {
  442. }
  443. RenderState::StateBlock::StateBlock(const StateBlock& copy)
  444. {
  445. // Hidden
  446. }
  447. RenderState::StateBlock::~StateBlock()
  448. {
  449. }
  450. RenderState::StateBlock* RenderState::StateBlock::create()
  451. {
  452. return new RenderState::StateBlock();
  453. }
  454. void RenderState::StateBlock::bind()
  455. {
  456. // When the public bind() is called with no RenderState object passed in,
  457. // we assume we are being called to bind the state of a single StateBlock,
  458. // irrespective of whether it belongs to a hierarchy of RenderStates.
  459. // Therefore, we call restore() here with only this StateBlock's override
  460. // bits to restore state before applying the new state.
  461. StateBlock::restore(_bits);
  462. bindNoRestore();
  463. }
  464. void RenderState::StateBlock::bindNoRestore()
  465. {
  466. GP_ASSERT(_defaultState);
  467. // Update any state that differs from _defaultState and flip _defaultState bits
  468. if ((_bits & RS_BLEND) && (_blendEnabled != _defaultState->_blendEnabled))
  469. {
  470. if (_blendEnabled)
  471. GL_ASSERT( glEnable(GL_BLEND) );
  472. else
  473. GL_ASSERT( glDisable(GL_BLEND) );
  474. _defaultState->_blendEnabled = _blendEnabled;
  475. }
  476. if ((_bits & RS_BLEND_FUNC) && (_blendSrc != _defaultState->_blendSrc || _blendDst != _defaultState->_blendDst))
  477. {
  478. GL_ASSERT( glBlendFunc((GLenum)_blendSrc, (GLenum)_blendDst) );
  479. _defaultState->_blendSrc = _blendSrc;
  480. _defaultState->_blendDst = _blendDst;
  481. }
  482. if ((_bits & RS_CULL_FACE) && (_cullFaceEnabled != _defaultState->_cullFaceEnabled))
  483. {
  484. if (_cullFaceEnabled)
  485. GL_ASSERT( glEnable(GL_CULL_FACE) );
  486. else
  487. GL_ASSERT( glDisable(GL_CULL_FACE) );
  488. _defaultState->_cullFaceEnabled = _cullFaceEnabled;
  489. }
  490. if ((_bits & RS_CULL_FACE_SIDE) && (_cullFaceSide != _defaultState->_cullFaceSide))
  491. {
  492. GL_ASSERT( glCullFace((GLenum)_cullFaceSide) );
  493. _defaultState->_cullFaceSide = _cullFaceSide;
  494. }
  495. if ((_bits & RS_DEPTH_TEST) && (_depthTestEnabled != _defaultState->_depthTestEnabled))
  496. {
  497. if (_depthTestEnabled)
  498. GL_ASSERT( glEnable(GL_DEPTH_TEST) );
  499. else
  500. GL_ASSERT( glDisable(GL_DEPTH_TEST) );
  501. _defaultState->_depthTestEnabled = _depthTestEnabled;
  502. }
  503. if ((_bits & RS_DEPTH_WRITE) && (_depthWriteEnabled != _defaultState->_depthWriteEnabled))
  504. {
  505. GL_ASSERT( glDepthMask(_depthWriteEnabled ? GL_TRUE : GL_FALSE) );
  506. _defaultState->_depthWriteEnabled = _depthWriteEnabled;
  507. }
  508. if ((_bits & RS_DEPTH_FUNC) && (_depthFunction != _defaultState->_depthFunction))
  509. {
  510. GL_ASSERT( glDepthFunc((GLenum)_depthFunction) );
  511. _defaultState->_depthFunction = _depthFunction;
  512. }
  513. if ((_bits & RS_STENCIL_TEST) && (_stencilTestEnabled != _defaultState->_stencilTestEnabled))
  514. {
  515. if (_stencilTestEnabled)
  516. GL_ASSERT( glEnable(GL_STENCIL_TEST) );
  517. else
  518. GL_ASSERT( glDisable(GL_STENCIL_TEST) );
  519. _defaultState->_stencilTestEnabled = _stencilTestEnabled;
  520. }
  521. if ((_bits & RS_STENCIL_WRITE) && (_stencilWrite != _defaultState->_stencilWrite))
  522. {
  523. GL_ASSERT( glStencilMask(_stencilWrite) );
  524. _defaultState->_stencilWrite = _stencilWrite;
  525. }
  526. if ((_bits & RS_STENCIL_FUNC) && (_stencilFunction != _defaultState->_stencilFunction ||
  527. _stencilFunctionRef != _defaultState->_stencilFunctionRef ||
  528. _stencilFunctionMask != _defaultState->_stencilFunctionMask))
  529. {
  530. GL_ASSERT( glStencilFunc((GLenum)_stencilFunction, _stencilFunctionRef, _stencilFunctionMask) );
  531. _defaultState->_stencilFunction = _stencilFunction;
  532. _defaultState->_stencilFunctionRef = _stencilFunctionRef;
  533. _defaultState->_stencilFunctionMask = _stencilFunctionMask;
  534. }
  535. if ((_bits & RS_STENCIL_OP) && (_stencilOpSfail != _defaultState->_stencilOpSfail ||
  536. _stencilOpDpfail != _defaultState->_stencilOpDpfail ||
  537. _stencilOpDppass != _defaultState->_stencilOpDppass))
  538. {
  539. GL_ASSERT( glStencilOp((GLenum)_stencilOpSfail, (GLenum)_stencilOpDpfail, (GLenum)_stencilOpDppass) );
  540. _defaultState->_stencilOpSfail = _stencilOpSfail;
  541. _defaultState->_stencilOpDpfail = _stencilOpDpfail;
  542. _defaultState->_stencilOpDppass = _stencilOpDppass;
  543. }
  544. _defaultState->_bits |= _bits;
  545. }
  546. void RenderState::StateBlock::restore(long stateOverrideBits)
  547. {
  548. GP_ASSERT(_defaultState);
  549. // If there is no state to restore (i.e. no non-default state), do nothing.
  550. if (_defaultState->_bits == 0)
  551. {
  552. return;
  553. }
  554. // Restore any state that is not overridden and is not default
  555. if (!(stateOverrideBits & RS_BLEND) && (_defaultState->_bits & RS_BLEND))
  556. {
  557. GL_ASSERT( glDisable(GL_BLEND) );
  558. _defaultState->_bits &= ~RS_BLEND;
  559. _defaultState->_blendEnabled = false;
  560. }
  561. if (!(stateOverrideBits & RS_BLEND_FUNC) && (_defaultState->_bits & RS_BLEND_FUNC))
  562. {
  563. GL_ASSERT( glBlendFunc(GL_ONE, GL_ZERO) );
  564. _defaultState->_bits &= ~RS_BLEND_FUNC;
  565. _defaultState->_blendSrc = RenderState::BLEND_ONE;
  566. _defaultState->_blendDst = RenderState::BLEND_ZERO;
  567. }
  568. if (!(stateOverrideBits & RS_CULL_FACE) && (_defaultState->_bits & RS_CULL_FACE))
  569. {
  570. GL_ASSERT( glDisable(GL_CULL_FACE) );
  571. _defaultState->_bits &= ~RS_CULL_FACE;
  572. _defaultState->_cullFaceEnabled = false;
  573. }
  574. if (!(stateOverrideBits & RS_CULL_FACE_SIDE) && (_defaultState->_bits & RS_CULL_FACE_SIDE))
  575. {
  576. GL_ASSERT( glCullFace((GLenum)GL_BACK) );
  577. _defaultState->_bits &= ~RS_CULL_FACE_SIDE;
  578. _defaultState->_cullFaceSide = RenderState::CULL_FACE_SIDE_BACK;
  579. }
  580. if (!(stateOverrideBits & RS_DEPTH_TEST) && (_defaultState->_bits & RS_DEPTH_TEST))
  581. {
  582. GL_ASSERT( glDisable(GL_DEPTH_TEST) );
  583. _defaultState->_bits &= ~RS_DEPTH_TEST;
  584. _defaultState->_depthTestEnabled = false;
  585. }
  586. if (!(stateOverrideBits & RS_DEPTH_WRITE) && (_defaultState->_bits & RS_DEPTH_WRITE))
  587. {
  588. GL_ASSERT( glDepthMask(GL_TRUE) );
  589. _defaultState->_bits &= ~RS_DEPTH_WRITE;
  590. _defaultState->_depthWriteEnabled = true;
  591. }
  592. if (!(stateOverrideBits & RS_DEPTH_FUNC) && (_defaultState->_bits & RS_DEPTH_FUNC))
  593. {
  594. GL_ASSERT( glDepthFunc((GLenum)GL_LESS) );
  595. _defaultState->_bits &= ~RS_DEPTH_FUNC;
  596. _defaultState->_depthFunction = RenderState::DEPTH_LESS;
  597. }
  598. if (!(stateOverrideBits & RS_STENCIL_TEST) && (_defaultState->_bits & RS_STENCIL_TEST))
  599. {
  600. GL_ASSERT( glDisable(GL_STENCIL_TEST) );
  601. _defaultState->_bits &= ~RS_STENCIL_TEST;
  602. _defaultState->_stencilTestEnabled = false;
  603. }
  604. if (!(stateOverrideBits & RS_STENCIL_WRITE) && (_defaultState->_bits & RS_STENCIL_WRITE))
  605. {
  606. GL_ASSERT( glStencilMask(RS_ALL_ONES) );
  607. _defaultState->_bits &= ~RS_STENCIL_WRITE;
  608. _defaultState->_stencilTestEnabled = RS_ALL_ONES;
  609. }
  610. if (!(stateOverrideBits & RS_STENCIL_FUNC) && (_defaultState->_bits & RS_STENCIL_FUNC))
  611. {
  612. GL_ASSERT( glStencilFunc((GLenum)RenderState::STENCIL_ALWAYS, 0, RS_ALL_ONES) );
  613. _defaultState->_bits &= ~RS_STENCIL_FUNC;
  614. _defaultState->_stencilFunction = RenderState::STENCIL_ALWAYS;
  615. _defaultState->_stencilFunctionRef = 0;
  616. _defaultState->_stencilFunctionMask = RS_ALL_ONES;
  617. }
  618. if (!(stateOverrideBits & RS_STENCIL_OP) && (_defaultState->_bits & RS_STENCIL_OP))
  619. {
  620. GL_ASSERT( glStencilOp((GLenum)RenderState::STENCIL_OP_KEEP, (GLenum)RenderState::STENCIL_OP_KEEP, (GLenum)RenderState::STENCIL_OP_KEEP) );
  621. _defaultState->_bits &= ~RS_STENCIL_OP;
  622. _defaultState->_stencilOpSfail = RenderState::STENCIL_OP_KEEP;
  623. _defaultState->_stencilOpDpfail = RenderState::STENCIL_OP_KEEP;
  624. _defaultState->_stencilOpDppass = RenderState::STENCIL_OP_KEEP;
  625. }
  626. }
  627. void RenderState::StateBlock::enableDepthWrite()
  628. {
  629. GP_ASSERT(_defaultState);
  630. // Internal method used by Game::clear() to restore depth writing before a
  631. // clear operation. This is necessary if the last code to draw before the
  632. // next frame leaves depth writing disabled.
  633. if (!_defaultState->_depthWriteEnabled)
  634. {
  635. GL_ASSERT( glDepthMask(GL_TRUE) );
  636. _defaultState->_bits &= ~RS_DEPTH_WRITE;
  637. _defaultState->_depthWriteEnabled = true;
  638. }
  639. }
  640. void RenderState::StateBlock::cloneInto(StateBlock* state)
  641. {
  642. GP_ASSERT(state);
  643. state->_cullFaceEnabled = _cullFaceEnabled;
  644. state->_depthTestEnabled = _depthTestEnabled;
  645. state->_depthWriteEnabled = _depthWriteEnabled;
  646. state->_depthFunction = _depthFunction;
  647. state->_blendEnabled = _blendEnabled;
  648. state->_blendSrc = _blendSrc;
  649. state->_blendDst = _blendDst;
  650. state->_cullFaceSide = _cullFaceSide;
  651. state->_stencilTestEnabled = _stencilTestEnabled;
  652. state->_stencilWrite = _stencilWrite;
  653. state->_stencilFunction = _stencilFunction;
  654. state->_stencilFunctionRef = _stencilFunctionRef;
  655. state->_stencilFunctionMask = _stencilFunctionMask;
  656. state->_stencilOpSfail = _stencilOpSfail;
  657. state->_stencilOpDpfail = _stencilOpDpfail;
  658. state->_stencilOpDppass = _stencilOpDppass;
  659. state->_bits = _bits;
  660. }
  661. static bool parseBoolean(const char* value)
  662. {
  663. GP_ASSERT(value);
  664. if (strlen(value) == 4)
  665. {
  666. return (
  667. tolower(value[0]) == 't' &&
  668. tolower(value[1]) == 'r' &&
  669. tolower(value[2]) == 'u' &&
  670. tolower(value[3]) == 'e' );
  671. }
  672. return false;
  673. }
  674. static int parseInt(const char* value)
  675. {
  676. GP_ASSERT(value);
  677. int rValue;
  678. int scanned = sscanf(value, "%d", &rValue);
  679. if (scanned != 1)
  680. {
  681. GP_ERROR("Error attempting to parse int '%s'. (Will default to 0 if errors are treated as warnings)", value);
  682. return 0;
  683. }
  684. return rValue;
  685. }
  686. static unsigned int parseUInt(const char* value)
  687. {
  688. GP_ASSERT(value);
  689. unsigned int rValue;
  690. int scanned = sscanf(value, "%u", &rValue);
  691. if (scanned != 1)
  692. {
  693. GP_ERROR("Error attempting to parse unsigned int '%s'. (Will default to 0 if errors are treated as warnings)", value);
  694. return 0;
  695. }
  696. return rValue;
  697. }
  698. static RenderState::Blend parseBlend(const char* value)
  699. {
  700. GP_ASSERT(value);
  701. // Convert the string to uppercase for comparison.
  702. std::string upper(value);
  703. std::transform(upper.begin(), upper.end(), upper.begin(), (int(*)(int))toupper);
  704. if (upper == "ZERO")
  705. return RenderState::BLEND_ZERO;
  706. else if (upper == "ONE")
  707. return RenderState::BLEND_ONE;
  708. else if (upper == "SRC_COLOR")
  709. return RenderState::BLEND_SRC_COLOR;
  710. else if (upper == "ONE_MINUS_SRC_COLOR")
  711. return RenderState::BLEND_ONE_MINUS_SRC_COLOR;
  712. else if (upper == "DST_COLOR")
  713. return RenderState::BLEND_DST_COLOR;
  714. else if (upper == "ONE_MINUS_DST_COLOR")
  715. return RenderState::BLEND_ONE_MINUS_DST_COLOR;
  716. else if (upper == "SRC_ALPHA")
  717. return RenderState::BLEND_SRC_ALPHA;
  718. else if (upper == "ONE_MINUS_SRC_ALPHA")
  719. return RenderState::BLEND_ONE_MINUS_SRC_ALPHA;
  720. else if (upper == "DST_ALPHA")
  721. return RenderState::BLEND_DST_ALPHA;
  722. else if (upper == "ONE_MINUS_DST_ALPHA")
  723. return RenderState::BLEND_ONE_MINUS_DST_ALPHA;
  724. else if (upper == "CONSTANT_ALPHA")
  725. return RenderState::BLEND_CONSTANT_ALPHA;
  726. else if (upper == "ONE_MINUS_CONSTANT_ALPHA")
  727. return RenderState::BLEND_ONE_MINUS_CONSTANT_ALPHA;
  728. else if (upper == "SRC_ALPHA_SATURATE")
  729. return RenderState::BLEND_SRC_ALPHA_SATURATE;
  730. else
  731. {
  732. GP_ERROR("Unsupported blend value (%s). (Will default to BLEND_ONE if errors are treated as warnings)", value);
  733. return RenderState::BLEND_ONE;
  734. }
  735. }
  736. static RenderState::DepthFunction parseDepthFunc(const char* value)
  737. {
  738. GP_ASSERT(value);
  739. // Convert string to uppercase for comparison
  740. std::string upper(value);
  741. std::transform(upper.begin(), upper.end(), upper.begin(), (int(*)(int))toupper);
  742. if (upper == "NEVER")
  743. return RenderState::DEPTH_NEVER;
  744. else if (upper == "LESS")
  745. return RenderState::DEPTH_LESS;
  746. else if (upper == "EQUAL")
  747. return RenderState::DEPTH_EQUAL;
  748. else if (upper == "LEQUAL")
  749. return RenderState::DEPTH_LEQUAL;
  750. else if (upper == "GREATER")
  751. return RenderState::DEPTH_GREATER;
  752. else if (upper == "NOTEQUAL")
  753. return RenderState::DEPTH_NOTEQUAL;
  754. else if (upper == "GEQUAL")
  755. return RenderState::DEPTH_GEQUAL;
  756. else if (upper == "ALWAYS")
  757. return RenderState::DEPTH_ALWAYS;
  758. else
  759. {
  760. GP_ERROR("Unsupported depth function value (%s). Will default to DEPTH_LESS if errors are treated as warnings)", value);
  761. return RenderState::DEPTH_LESS;
  762. }
  763. }
  764. static RenderState::CullFaceSide parseCullFaceSide(const char* value)
  765. {
  766. GP_ASSERT(value);
  767. // Convert string to uppercase for comparison
  768. std::string upper(value);
  769. std::transform(upper.begin(), upper.end(), upper.begin(), (int(*)(int))toupper);
  770. if (upper == "BACK")
  771. return RenderState::CULL_FACE_SIDE_BACK;
  772. else if (upper == "FRONT")
  773. return RenderState::CULL_FACE_SIDE_FRONT;
  774. else if (upper == "FRONT_AND_BACK")
  775. return RenderState::CULL_FACE_SIDE_FRONT_AND_BACK;
  776. else
  777. {
  778. GP_ERROR("Unsupported cull face side value (%s). Will default to BACK if errors are treated as warnings)", value);
  779. return RenderState::CULL_FACE_SIDE_BACK;
  780. }
  781. }
  782. static RenderState::StencilFunction parseStencilFunc(const char* value)
  783. {
  784. GP_ASSERT(value);
  785. // Convert string to uppercase for comparison
  786. std::string upper(value);
  787. std::transform(upper.begin(), upper.end(), upper.begin(), (int(*)(int))toupper);
  788. if (upper == "NEVER")
  789. return RenderState::STENCIL_NEVER;
  790. else if (upper == "LESS")
  791. return RenderState::STENCIL_LESS;
  792. else if (upper == "EQUAL")
  793. return RenderState::STENCIL_EQUAL;
  794. else if (upper == "LEQUAL")
  795. return RenderState::STENCIL_LEQUAL;
  796. else if (upper == "GREATER")
  797. return RenderState::STENCIL_GREATER;
  798. else if (upper == "NOTEQUAL")
  799. return RenderState::STENCIL_NOTEQUAL;
  800. else if (upper == "GEQUAL")
  801. return RenderState::STENCIL_GEQUAL;
  802. else if (upper == "ALWAYS")
  803. return RenderState::STENCIL_ALWAYS;
  804. else
  805. {
  806. GP_ERROR("Unsupported stencil function value (%s). Will default to STENCIL_ALWAYS if errors are treated as warnings)", value);
  807. return RenderState::STENCIL_ALWAYS;
  808. }
  809. }
  810. static RenderState::StencilOperation parseStencilOp(const char* value)
  811. {
  812. GP_ASSERT(value);
  813. // Convert string to uppercase for comparison
  814. std::string upper(value);
  815. std::transform(upper.begin(), upper.end(), upper.begin(), (int(*)(int))toupper);
  816. if (upper == "KEEP")
  817. return RenderState::STENCIL_OP_KEEP;
  818. else if (upper == "ZERO")
  819. return RenderState::STENCIL_OP_ZERO;
  820. else if (upper == "REPLACE")
  821. return RenderState::STENCIL_OP_REPLACE;
  822. else if (upper == "INCR")
  823. return RenderState::STENCIL_OP_INCR;
  824. else if (upper == "DECR")
  825. return RenderState::STENCIL_OP_DECR;
  826. else if (upper == "INVERT")
  827. return RenderState::STENCIL_OP_INVERT;
  828. else if (upper == "INCR_WRAP")
  829. return RenderState::STENCIL_OP_INCR_WRAP;
  830. else if (upper == "DECR_WRAP")
  831. return RenderState::STENCIL_OP_DECR_WRAP;
  832. else
  833. {
  834. GP_ERROR("Unsupported stencil operation value (%s). Will default to STENCIL_OP_KEEP if errors are treated as warnings)", value);
  835. return RenderState::STENCIL_OP_KEEP;
  836. }
  837. }
  838. void RenderState::StateBlock::setState(const char* name, const char* value)
  839. {
  840. GP_ASSERT(name);
  841. if (strcmp(name, "blend") == 0)
  842. {
  843. setBlend(parseBoolean(value));
  844. }
  845. else if (strcmp(name, "blendSrc") == 0 || strcmp(name, "srcBlend") == 0 ) // Leaving srcBlend for backward compat.
  846. {
  847. setBlendSrc(parseBlend(value));
  848. }
  849. else if (strcmp(name, "blendDst") == 0 || strcmp(name, "dstBlend") == 0) // // Leaving dstBlend for backward compat.
  850. {
  851. setBlendDst(parseBlend(value));
  852. }
  853. else if (strcmp(name, "cullFace") == 0)
  854. {
  855. setCullFace(parseBoolean(value));
  856. }
  857. else if (strcmp(name, "cullFaceSide") == 0)
  858. {
  859. setCullFaceSide(parseCullFaceSide(value));
  860. }
  861. else if (strcmp(name, "depthTest") == 0)
  862. {
  863. setDepthTest(parseBoolean(value));
  864. }
  865. else if (strcmp(name, "depthWrite") == 0)
  866. {
  867. setDepthWrite(parseBoolean(value));
  868. }
  869. else if (strcmp(name, "depthFunc") == 0)
  870. {
  871. setDepthFunction(parseDepthFunc(value));
  872. }
  873. else if (strcmp(name, "stencilTest") == 0)
  874. {
  875. setStencilTest(parseBoolean(value));
  876. }
  877. else if (strcmp(name, "stencilWrite") == 0)
  878. {
  879. setStencilWrite(parseUInt(value));
  880. }
  881. else if (strcmp(name, "stencilFunc") == 0)
  882. {
  883. setStencilFunction(parseStencilFunc(value), _stencilFunctionRef, _stencilFunctionMask);
  884. }
  885. else if (strcmp(name, "stencilFuncRef") == 0)
  886. {
  887. setStencilFunction(_stencilFunction, parseInt(value), _stencilFunctionMask);
  888. }
  889. else if (strcmp(name, "stencilFuncMask") == 0)
  890. {
  891. setStencilFunction(_stencilFunction, _stencilFunctionRef, parseUInt(value));
  892. }
  893. else if (strcmp(name, "stencilOpSfail") == 0)
  894. {
  895. setStencilOperation(parseStencilOp(value), _stencilOpDpfail, _stencilOpDppass);
  896. }
  897. else if (strcmp(name, "stencilOpDpfail") == 0)
  898. {
  899. setStencilOperation(_stencilOpSfail, parseStencilOp(value), _stencilOpDppass);
  900. }
  901. else if (strcmp(name, "stencilOpDppass") == 0)
  902. {
  903. setStencilOperation(_stencilOpSfail, _stencilOpDpfail, parseStencilOp(value));
  904. }
  905. else
  906. {
  907. GP_ERROR("Unsupported render state string '%s'.", name);
  908. }
  909. }
  910. void RenderState::StateBlock::setBlend(bool enabled)
  911. {
  912. _blendEnabled = enabled;
  913. if (!enabled)
  914. {
  915. _bits &= ~RS_BLEND;
  916. }
  917. else
  918. {
  919. _bits |= RS_BLEND;
  920. }
  921. }
  922. void RenderState::StateBlock::setBlendSrc(Blend blend)
  923. {
  924. _blendSrc = blend;
  925. if (_blendSrc == BLEND_ONE && _blendDst == BLEND_ZERO)
  926. {
  927. // Default blend func
  928. _bits &= ~RS_BLEND_FUNC;
  929. }
  930. else
  931. {
  932. _bits |= RS_BLEND_FUNC;
  933. }
  934. }
  935. void RenderState::StateBlock::setBlendDst(Blend blend)
  936. {
  937. _blendDst = blend;
  938. if (_blendSrc == BLEND_ONE && _blendDst == BLEND_ZERO)
  939. {
  940. // Default blend func
  941. _bits &= ~RS_BLEND_FUNC;
  942. }
  943. else
  944. {
  945. _bits |= RS_BLEND_FUNC;
  946. }
  947. }
  948. void RenderState::StateBlock::setCullFace(bool enabled)
  949. {
  950. _cullFaceEnabled = enabled;
  951. if (!enabled)
  952. {
  953. _bits &= ~RS_CULL_FACE;
  954. }
  955. else
  956. {
  957. _bits |= RS_CULL_FACE;
  958. }
  959. }
  960. void RenderState::StateBlock::setCullFaceSide(CullFaceSide side)
  961. {
  962. _cullFaceSide = side;
  963. if (_cullFaceSide == CULL_FACE_SIDE_BACK)
  964. {
  965. // Default cull side
  966. _bits &= ~RS_CULL_FACE_SIDE;
  967. }
  968. else
  969. {
  970. _bits |= RS_CULL_FACE_SIDE;
  971. }
  972. }
  973. void RenderState::StateBlock::setDepthTest(bool enabled)
  974. {
  975. _depthTestEnabled = enabled;
  976. if (!enabled)
  977. {
  978. _bits &= ~RS_DEPTH_TEST;
  979. }
  980. else
  981. {
  982. _bits |= RS_DEPTH_TEST;
  983. }
  984. }
  985. void RenderState::StateBlock::setDepthWrite(bool enabled)
  986. {
  987. _depthWriteEnabled = enabled;
  988. if (enabled)
  989. {
  990. _bits &= ~RS_DEPTH_WRITE;
  991. }
  992. else
  993. {
  994. _bits |= RS_DEPTH_WRITE;
  995. }
  996. }
  997. void RenderState::StateBlock::setDepthFunction(DepthFunction func)
  998. {
  999. _depthFunction = func;
  1000. if (_depthFunction == DEPTH_LESS)
  1001. {
  1002. // Default depth function
  1003. _bits &= ~RS_DEPTH_FUNC;
  1004. }
  1005. else
  1006. {
  1007. _bits |= RS_DEPTH_FUNC;
  1008. }
  1009. }
  1010. void RenderState::StateBlock::setStencilTest(bool enabled)
  1011. {
  1012. _stencilTestEnabled = enabled;
  1013. if (!enabled)
  1014. {
  1015. _bits &= ~RS_STENCIL_TEST;
  1016. }
  1017. else
  1018. {
  1019. _bits |= RS_STENCIL_TEST;
  1020. }
  1021. }
  1022. void RenderState::StateBlock::setStencilWrite(unsigned int mask)
  1023. {
  1024. _stencilWrite = mask;
  1025. if (mask == RS_ALL_ONES)
  1026. {
  1027. // Default stencil write
  1028. _bits &= ~RS_STENCIL_WRITE;
  1029. }
  1030. else
  1031. {
  1032. _bits |= RS_STENCIL_WRITE;
  1033. }
  1034. }
  1035. void RenderState::StateBlock::setStencilFunction(StencilFunction func, int ref, unsigned int mask)
  1036. {
  1037. _stencilFunction = func;
  1038. _stencilFunctionRef = ref;
  1039. _stencilFunctionMask = mask;
  1040. if (func == STENCIL_ALWAYS && ref == 0 && mask == RS_ALL_ONES)
  1041. {
  1042. // Default stencil function
  1043. _bits &= ~RS_STENCIL_FUNC;
  1044. }
  1045. else
  1046. {
  1047. _bits |= RS_STENCIL_FUNC;
  1048. }
  1049. }
  1050. void RenderState::StateBlock::setStencilOperation(StencilOperation sfail, StencilOperation dpfail, StencilOperation dppass)
  1051. {
  1052. _stencilOpSfail = sfail;
  1053. _stencilOpDpfail = dpfail;
  1054. _stencilOpDppass = dppass;
  1055. if (sfail == STENCIL_OP_KEEP && dpfail == STENCIL_OP_KEEP && dppass == STENCIL_OP_KEEP)
  1056. {
  1057. // Default stencil operation
  1058. _bits &= ~RS_STENCIL_OP;
  1059. }
  1060. else
  1061. {
  1062. _bits |= RS_STENCIL_OP;
  1063. }
  1064. }
  1065. }