RenderState.h 21 KB

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