RenderState.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. #ifndef RENDERSTATE_H_
  2. #define RENDERSTATE_H_
  3. #include "Ref.h"
  4. #include "Vector3.h"
  5. #include "Vector4.h"
  6. namespace gameplay
  7. {
  8. class MaterialParameter;
  9. class Node;
  10. class NodeCloneContext;
  11. class Pass;
  12. /**
  13. * Defines the render state of the graphics device.
  14. */
  15. class RenderState : public Ref
  16. {
  17. friend class Game;
  18. friend class Material;
  19. friend class Technique;
  20. friend class Pass;
  21. friend class Model;
  22. public:
  23. /**
  24. * Built-in auto-bind targets for material parameters.
  25. */
  26. enum AutoBinding
  27. {
  28. NONE,
  29. /**
  30. * Binds a node's World matrix.
  31. */
  32. WORLD_MATRIX,
  33. /**
  34. * Binds the View matrix of the active camera for the node's scene.
  35. */
  36. VIEW_MATRIX,
  37. /**
  38. * Binds the Projection matrix of the active camera for the node's scene.
  39. */
  40. PROJECTION_MATRIX,
  41. /**
  42. * Binds a node's WorldView matrix.
  43. */
  44. WORLD_VIEW_MATRIX,
  45. /**
  46. * Binds the ViewProjection matrix of the active camera for the node's scene.
  47. */
  48. VIEW_PROJECTION_MATRIX,
  49. /**
  50. * Binds a node's WorldViewProjection matrix.
  51. */
  52. WORLD_VIEW_PROJECTION_MATRIX,
  53. /**
  54. * Binds a node's InverseTransposeWorl matrix.
  55. */
  56. INVERSE_TRANSPOSE_WORLD_MATRIX,
  57. /**
  58. * Binds a node's InverseTransposeWorldView matrix.
  59. */
  60. INVERSE_TRANSPOSE_WORLD_VIEW_MATRIX,
  61. /**
  62. * Binds the position (Vector3) of the active camera for the node's scene.
  63. */
  64. CAMERA_WORLD_POSITION,
  65. /**
  66. * Binds the view-space position (Vector3) of the active camera for the node's scene.
  67. */
  68. CAMERA_VIEW_POSITION,
  69. /**
  70. * Binds the matrix palette of MeshSkin attached to a node's model.
  71. */
  72. MATRIX_PALETTE,
  73. /**
  74. * Binds the current scene's ambient color (Vector3).
  75. */
  76. SCENE_AMBIENT_COLOR,
  77. /**
  78. * Binds the current scene's light color (Vector3).
  79. *
  80. * This is typically used for the main directional light in a scene, such as the Sun.
  81. */
  82. SCENE_LIGHT_COLOR,
  83. /**
  84. * Binds the current scene's light direction (Vector3).
  85. *
  86. * This is typically used for the main directional light in a scene, such as the Sun.
  87. */
  88. SCENE_LIGHT_DIRECTION
  89. };
  90. /**
  91. * Callback function prototype for resolving material parameter auto bindings.
  92. *
  93. * Functions matching this callback signature can be registered via the
  94. * RenderState::registerAutoBindingResolver method to extend or override the set
  95. * of built-in material parameter auto bindings.
  96. *
  97. * @param autoBinding Name of the auto binding to resolve.
  98. * @param node Node that is bound to the material of the specified parameter.
  99. * @param parameter Material parameter to set the binding on.
  100. *
  101. * @return True ONLY if the implementations explicitly handles the auto binding, false otherwise.
  102. * Returning true here will prevent any further code (including built-in resolving code) from
  103. * handling the auto binding.
  104. *
  105. * @see RenderState::registerAutoBindingResolver(const char*, RenderState::AutoBindingResolver)
  106. * @script{ignore}
  107. */
  108. typedef bool (*ResolveAutoBindingCallback) (const char* autoBinding, Node* node, MaterialParameter* parameter);
  109. /**
  110. * Defines blend constants supported by the blend function.
  111. */
  112. enum Blend
  113. {
  114. BLEND_ZERO = GL_ZERO,
  115. BLEND_ONE = GL_ONE,
  116. BLEND_SRC_COLOR = GL_SRC_COLOR,
  117. BLEND_ONE_MINUS_SRC_COLOR = GL_ONE_MINUS_SRC_COLOR,
  118. BLEND_DST_COLOR = GL_DST_COLOR,
  119. BLEND_ONE_MINUS_DST_COLOR = GL_ONE_MINUS_DST_COLOR,
  120. BLEND_SRC_ALPHA = GL_SRC_ALPHA,
  121. BLEND_ONE_MINUS_SRC_ALPHA = GL_ONE_MINUS_SRC_ALPHA,
  122. BLEND_DST_ALPHA = GL_DST_ALPHA,
  123. BLEND_ONE_MINUS_DST_ALPHA = GL_ONE_MINUS_DST_ALPHA,
  124. BLEND_CONSTANT_ALPHA = GL_CONSTANT_ALPHA,
  125. BLEND_ONE_MINUS_CONSTANT_ALPHA = GL_ONE_MINUS_CONSTANT_ALPHA,
  126. BLEND_SRC_ALPHA_SATURATE = GL_SRC_ALPHA_SATURATE
  127. };
  128. /**
  129. * Defines the supported depth compare functions.
  130. *
  131. * Depth compare functions specify the comparison that takes place between the
  132. * incoming pixel's depth value and the depth value already in the depth buffer.
  133. * If the compare function passes, the new pixel will be drawn.
  134. *
  135. * The intial depth compare function is DEPTH_LESS.
  136. */
  137. enum DepthFunction
  138. {
  139. DEPTH_NEVER = GL_NEVER,
  140. DEPTH_LESS = GL_LESS,
  141. DEPTH_EQUAL = GL_EQUAL,
  142. DEPTH_LEQUAL = GL_LEQUAL,
  143. DEPTH_GREATER = GL_GREATER,
  144. DEPTH_NOTEQUAL = GL_NOTEQUAL,
  145. DEPTH_GEQUAL = GL_GEQUAL,
  146. DEPTH_ALWAYS = GL_ALWAYS
  147. };
  148. /**
  149. * Defines culling criteria for front-facing, back-facing and both-side
  150. * facets.
  151. */
  152. enum CullFaceSide
  153. {
  154. CULL_FACE_SIDE_BACK = GL_BACK,
  155. CULL_FACE_SIDE_FRONT = GL_FRONT,
  156. CULL_FACE_SIDE_FRONT_AND_BACK = GL_FRONT_AND_BACK
  157. };
  158. /**
  159. * Defines the supported stencil compare functions.
  160. *
  161. * Stencil compare functions determine if a new pixel will be drawn.
  162. *
  163. * The initial stencil compare function is STENCIL_ALWAYS.
  164. */
  165. enum StencilFunction
  166. {
  167. STENCIL_NEVER = GL_NEVER,
  168. STENCIL_ALWAYS = GL_ALWAYS,
  169. STENCIL_LESS = GL_LESS,
  170. STENCIL_LEQUAL = GL_LEQUAL,
  171. STENCIL_EQUAL = GL_EQUAL,
  172. STENCIL_GREATER = GL_GREATER,
  173. STENCIL_GEQUAL = GL_GEQUAL,
  174. STENCIL_NOTEQUAL = GL_NOTEQUAL
  175. };
  176. /**
  177. * Defines the supported stencil operations to perform.
  178. *
  179. * Stencil operations determine what should happen to the pixel if the
  180. * stencil test fails, passes, or passes but fails the depth test.
  181. *
  182. * The initial stencil operation is STENCIL_OP_KEEP.
  183. */
  184. enum StencilOperation
  185. {
  186. STENCIL_OP_KEEP = GL_KEEP,
  187. STENCIL_OP_ZERO = GL_ZERO,
  188. STENCIL_OP_REPLACE = GL_REPLACE,
  189. STENCIL_OP_INCR = GL_INCR,
  190. STENCIL_OP_DECR = GL_DECR,
  191. STENCIL_OP_INVERT = GL_INVERT,
  192. STENCIL_OP_INCR_WRAP = GL_INCR_WRAP,
  193. STENCIL_OP_DECR_WRAP = GL_DECR_WRAP
  194. };
  195. /**
  196. * Defines a block of fixed-function render states that can be applied to a
  197. * RenderState object.
  198. */
  199. class StateBlock : public Ref
  200. {
  201. friend class RenderState;
  202. friend class Game;
  203. public:
  204. /**
  205. * Creates a new StateBlock with default render state settings.
  206. * @script{create}
  207. */
  208. static StateBlock* create();
  209. /**
  210. * Binds the state in this StateBlock to the renderer.
  211. *
  212. * This method handles both setting and restoring of render states to ensure that
  213. * only the state explicitly defined by this StateBlock is applied to the renderer.
  214. */
  215. void bind();
  216. /**
  217. * Toggles blending.
  218. *
  219. * @param enabled true to enable, false to disable.
  220. */
  221. void setBlend(bool enabled);
  222. /**
  223. * Explicitly sets the source used in the blend function for this render state.
  224. *
  225. * Note that the blend function is only applied when blending is enabled.
  226. *
  227. * @param blend Specifies how the source blending factors are computed.
  228. */
  229. void setBlendSrc(Blend blend);
  230. /**
  231. * Explicitly sets the source used in the blend function for this render state.
  232. *
  233. * Note that the blend function is only applied when blending is enabled.
  234. *
  235. * @param blend Specifies how the destination blending factors are computed.
  236. */
  237. void setBlendDst(Blend blend);
  238. /**
  239. * Explicitly enables or disables backface culling.
  240. *
  241. * @param enabled true to enable, false to disable.
  242. */
  243. void setCullFace(bool enabled);
  244. /**
  245. * Sets the side of the facets to cull.
  246. *
  247. * When not explicitly set, the default is to cull back-facing facets.
  248. * @param side The side to cull.
  249. */
  250. void setCullFaceSide(CullFaceSide side);
  251. /**
  252. * Toggles depth testing.
  253. *
  254. * By default, depth testing is disabled.
  255. *
  256. * @param enabled true to enable, false to disable.
  257. */
  258. void setDepthTest(bool enabled);
  259. /**
  260. * Toggles depth writing.
  261. *
  262. * @param enabled true to enable, false to disable.
  263. */
  264. void setDepthWrite(bool enabled);
  265. /**
  266. * Sets the depth function to use when depth testing is enabled.
  267. *
  268. * When not explicitly set and when depth testing is enabled, the default
  269. * depth function is DEPTH_LESS.
  270. *
  271. * @param func The depth function.
  272. */
  273. void setDepthFunction(DepthFunction func);
  274. /**
  275. * Toggles stencil testing.
  276. *
  277. * By default, stencil testing is disabled.
  278. *
  279. * @param enabled true to enable, false to disable.
  280. */
  281. void setStencilTest(bool enabled);
  282. /**
  283. * Sets the stencil writing mask.
  284. *
  285. * By default, the stencil writing mask is all 1's.
  286. *
  287. * @param mask Bit mask controlling writing to individual stencil planes.
  288. */
  289. void setStencilWrite(unsigned int mask);
  290. /**
  291. * Sets the stencil function.
  292. *
  293. * By default, the function is set to STENCIL_ALWAYS, the reference value is 0, and the mask is all 1's.
  294. *
  295. * @param func The stencil function.
  296. * @param ref The stencil reference value.
  297. * @param mask The stencil mask.
  298. */
  299. void setStencilFunction(StencilFunction func, int ref, unsigned int mask);
  300. /**
  301. * Sets the stencil operation.
  302. *
  303. * By default, stencil fail, stencil pass/depth fail, and stencil and depth pass are set to STENCIL_OP_KEEP.
  304. *
  305. * @param sfail The stencil operation if the stencil test fails.
  306. * @param dpfail The stencil operation if the stencil test passes, but the depth test fails.
  307. * @param dppass The stencil operation if both the stencil test and depth test pass.
  308. */
  309. void setStencilOperation(StencilOperation sfail, StencilOperation dpfail, StencilOperation dppass);
  310. /**
  311. * Sets a render state from the given name and value strings.
  312. *
  313. * This method attempts to interpret the passed in strings as render state
  314. * name and value. This is normally used when loading render states from
  315. * material files.
  316. *
  317. * @param name Name of the render state to set.
  318. * @param value Value of the specified render state.
  319. */
  320. void setState(const char* name, const char* value);
  321. private:
  322. /**
  323. * Constructor.
  324. */
  325. StateBlock();
  326. /**
  327. * Copy constructor.
  328. */
  329. StateBlock(const StateBlock& copy);
  330. /**
  331. * Destructor.
  332. */
  333. ~StateBlock();
  334. void bindNoRestore();
  335. static void restore(long stateOverrideBits);
  336. static void enableDepthWrite();
  337. void cloneInto(StateBlock* state);
  338. // States
  339. bool _cullFaceEnabled;
  340. bool _depthTestEnabled;
  341. bool _depthWriteEnabled;
  342. DepthFunction _depthFunction;
  343. bool _blendEnabled;
  344. Blend _blendSrc;
  345. Blend _blendDst;
  346. CullFaceSide _cullFaceSide;
  347. bool _stencilTestEnabled;
  348. unsigned int _stencilWrite;
  349. StencilFunction _stencilFunction;
  350. int _stencilFunctionRef;
  351. unsigned int _stencilFunctionMask;
  352. StencilOperation _stencilOpSfail;
  353. StencilOperation _stencilOpDpfail;
  354. StencilOperation _stencilOpDppass;
  355. long _bits;
  356. static StateBlock* _defaultState;
  357. };
  358. /**
  359. * Returns a MaterialParameter for the specified name.
  360. *
  361. * The returned MaterialParameter can be used to set values for the specified
  362. * parameter name.
  363. *
  364. * Note that this method causes a new MaterialParameter to be created if one
  365. * does not already exist for the given parameter name.
  366. *
  367. * @param name Material parameter (uniform) name.
  368. *
  369. * @return A MaterialParameter for the specified name.
  370. */
  371. MaterialParameter* getParameter(const char* name) const;
  372. /**
  373. * Clears the MaterialParameter with the given name.
  374. *
  375. * If a material parameter exists for the given name, it is destroyed and
  376. * removed from this RenderState.
  377. *
  378. * @param name Material parameter (uniform) name.
  379. */
  380. void clearParameter(const char* name);
  381. /**
  382. * Sets a material parameter auto-binding.
  383. *
  384. * @param name The name of the material parameter to store an auto-binding for.
  385. * @param autoBinding A valid AutoBinding value.
  386. */
  387. void setParameterAutoBinding(const char* name, AutoBinding autoBinding);
  388. /**
  389. * Sets a material parameter auto-binding.
  390. *
  391. * This method parses the passed in autoBinding string and attempts to convert it
  392. * to an AutoBinding enumeration value, which is then stored in this render state.
  393. *
  394. * @param name The name of the material parameter to store an auto-binding for.
  395. * @param autoBinding A string matching one of the built-in AutoBinding enum constants.
  396. */
  397. void setParameterAutoBinding(const char* name, const char* autoBinding);
  398. /**
  399. * Sets the fixed-function render state of this object to the state contained
  400. * in the specified StateBlock.
  401. *
  402. * The passed in StateBlock is stored in this RenderState object with an
  403. * increased reference count and released when either a different StateBlock
  404. * is assigned, or when this RenderState object is destroyed.
  405. *
  406. * @param state The state block to set.
  407. */
  408. void setStateBlock(StateBlock* state);
  409. /**
  410. * Gets the fixed-function StateBlock for this RenderState object.
  411. *
  412. * The returned StateBlock is referenced by this RenderState and therefore
  413. * should not be released by the user. To release a StateBlock for a
  414. * RenderState, the setState(StateBlock*) method should be called, passing
  415. * NULL. This removes the StateBlock and resets the fixed-function render
  416. * state to the default state.
  417. *
  418. * It is legal to pass the returned StateBlock to another RenderState object.
  419. * In this case, the StateBlock will be referenced by both RenderState objects
  420. * and any changes to the StateBlock will be reflected in all objects
  421. * that reference it.
  422. *
  423. * @return The StateBlock for this RenderState.
  424. */
  425. StateBlock* getStateBlock() const;
  426. /**
  427. * Registers a custom auto binding resolver.
  428. *
  429. * Implementing a custom auto binding resolver allows the set of built-in parameter auto
  430. * bindings to be extended or overridden. Any parameter auto binding that is set on a
  431. * material will be forwarded to any custom auto binding resolvers, in the order in which
  432. * they are registered. If a registered resolver returns true (specifying that it handles
  433. * the specified autoBinding), no further code will be executed for that autoBinding.
  434. * This allows auto binding resolvers to not only implement new/custom binding strings,
  435. * but it also lets them override existing/built-in ones. For this reason, you should
  436. * ensure that you ONLY return true if you explicitly handle a custom auto binding; return
  437. * false otherwise.
  438. *
  439. * Note that the custom resolver is called only once for a RenderState object when its
  440. * node binding is initially set. This occurs when a material is initially bound to a
  441. * Model that belongs to a Node. The resolver is NOT called each frame or each time
  442. * the RenderState is bound. Therefore, when implementing custom auto bindings for values
  443. * that change over time, the you should bind a method pointer onto the passed in
  444. * MaterialParaemter using the MaterialParameter::bindValue method. This way, the bound
  445. * method will be called each frame to set an updated value into the MaterialParameter.
  446. *
  447. * If no registered resolvers explicitly handle an auto binding, the binding will attempt
  448. * to be resolved using the internal/built-in resolver, which is able to handle any
  449. * auto bindings found in the RenderState::AutoBinding enumeration.
  450. *
  451. * @param callback Callback function for resolving parameter auto bindings.
  452. * @script{ignore}
  453. */
  454. static void registerAutoBindingResolver(ResolveAutoBindingCallback callback);
  455. protected:
  456. /**
  457. * Constructor.
  458. */
  459. RenderState();
  460. /**
  461. * Destructor.
  462. */
  463. virtual ~RenderState();
  464. /**
  465. * Static initializer that is called during game startup.
  466. */
  467. static void initialize();
  468. /**
  469. * Static finalizer that is called during game shutdown.
  470. */
  471. static void finalize();
  472. /**
  473. * Sets the node that this render state is bound to.
  474. *
  475. * The specified node is used to apply auto-bindings for the render state.
  476. * This is typically set to the node of the model that a material is
  477. * applied to.
  478. *
  479. * @param node The node to use for applying auto-bindings.
  480. */
  481. void setNodeBinding(Node* node);
  482. /**
  483. * Applies the specified custom auto-binding.
  484. *
  485. * @param uniformName Name of the shader uniform.
  486. * @param autoBinding Name of the auto binding.s
  487. */
  488. void applyAutoBinding(const char* uniformName, const char* autoBinding);
  489. /**
  490. * Binds the render state for this RenderState and any of its parents, top-down,
  491. * for the given pass.
  492. */
  493. void bind(Pass* pass);
  494. /**
  495. * Returns the topmost RenderState in the hierarchy below the given RenderState.
  496. */
  497. RenderState* getTopmost(RenderState* below);
  498. /**
  499. * Copies the data from this RenderState into the given RenderState.
  500. *
  501. * @param renderState The RenderState to copy the data to.
  502. * @param context The clone context.
  503. */
  504. void cloneInto(RenderState* renderState, NodeCloneContext& context) const;
  505. private:
  506. /**
  507. * Hidden copy constructor.
  508. */
  509. RenderState(const RenderState& copy);
  510. /**
  511. * Hidden copy assignment operator.
  512. */
  513. RenderState& operator=(const RenderState&);
  514. // Internal auto binding handler methods.
  515. const Matrix& autoBindingGetWorldMatrix() const;
  516. const Matrix& autoBindingGetViewMatrix() const;
  517. const Matrix& autoBindingGetProjectionMatrix() const;
  518. const Matrix& autoBindingGetWorldViewMatrix() const;
  519. const Matrix& autoBindingGetViewProjectionMatrix() const;
  520. const Matrix& autoBindingGetWorldViewProjectionMatrix() const;
  521. const Matrix& autoBindingGetInverseTransposeWorldMatrix() const;
  522. const Matrix& autoBindingGetInverseTransposeWorldViewMatrix() const;
  523. Vector3 autoBindingGetCameraWorldPosition() const;
  524. Vector3 autoBindingGetCameraViewPosition() const;
  525. const Vector4* autoBindingGetMatrixPalette() const;
  526. unsigned int autoBindingGetMatrixPaletteSize() const;
  527. const Vector3& autoBindingGetAmbientColor() const;
  528. const Vector3& autoBindingGetLightColor() const;
  529. const Vector3& autoBindingGetLightDirection() const;
  530. protected:
  531. /**
  532. * Collection of MaterialParameter's to be applied to the gameplay::Effect.
  533. */
  534. mutable std::vector<MaterialParameter*> _parameters;
  535. /**
  536. * Map of parameter names to auto binding strings.
  537. */
  538. std::map<std::string, std::string> _autoBindings;
  539. /**
  540. * The Node bound to the RenderState.
  541. */
  542. Node* _nodeBinding;
  543. /**
  544. * The StateBlock of fixed-function render states that can be applied to the RenderState.
  545. */
  546. mutable StateBlock* _state;
  547. /**
  548. * The RenderState's parent.
  549. */
  550. RenderState* _parent;
  551. /**
  552. * Map of custom auto binding resolvers.
  553. */
  554. static std::vector<ResolveAutoBindingCallback> _customAutoBindingResolvers;
  555. };
  556. }
  557. // Include MaterialParameter after the Pass class declaration
  558. // to avoid an erroneous circular dependency during compilation.
  559. #include "MaterialParameter.h"
  560. #endif