BatchRender.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _BATCH_RENDER_H_
  23. #define _BATCH_RENDER_H_
  24. #ifndef _VECTOR2_H_
  25. #include "2d/core/Vector2.h"
  26. #endif
  27. #ifndef _UTILITY_H_
  28. #include "2d/core/Utility.h"
  29. #endif
  30. #ifndef _DEBUG_STATS_H_
  31. #include "2d/scene/DebugStats.h"
  32. #endif
  33. #ifndef _TEXTURE_MANAGER_H_
  34. #include "graphics/TextureManager.h"
  35. #endif
  36. #ifndef _HASHTABLE_H
  37. #include "collection/hashTable.h"
  38. #endif
  39. #ifndef _COLOR_H_
  40. #include "graphics/color.h"
  41. #endif
  42. //-----------------------------------------------------------------------------
  43. #define BATCHRENDER_BUFFERSIZE (65535)
  44. #define BATCHRENDER_MAXQUADS (BATCHRENDER_BUFFERSIZE/6)
  45. //-----------------------------------------------------------------------------
  46. class SceneRenderRequest;
  47. //-----------------------------------------------------------------------------
  48. class BatchRender
  49. {
  50. public:
  51. BatchRender();
  52. virtual ~BatchRender();
  53. /// Set the strict order mode.
  54. inline void setStrictOrderMode( const bool strictOrder, const bool forceFlush = false )
  55. {
  56. // Ignore if no change.
  57. if ( !forceFlush && strictOrder == mStrictOrderMode )
  58. return;
  59. // Flush.
  60. flushInternal();
  61. // Update strict order mode.
  62. mStrictOrderMode = strictOrder;
  63. }
  64. /// Gets the strict order mode.
  65. inline bool getStrictOrderMode( void ) const { return mStrictOrderMode; }
  66. /// Turns-on blend mode with the specified blend factors and color.
  67. inline void setBlendMode( GLenum srcFactor, GLenum dstFactor, const ColorF& blendColor = ColorF(1.0f, 1.0f, 1.0f, 1.0f))
  68. {
  69. // Ignore no change.
  70. if ( mBlendMode &&
  71. mSrcBlendFactor == srcFactor &&
  72. mDstBlendFactor == dstFactor &&
  73. mBlendColor == blendColor )
  74. return;
  75. // Flush.
  76. flush( mpDebugStats->batchBlendStateFlush );
  77. mBlendMode = true;
  78. mSrcBlendFactor = srcFactor;
  79. mDstBlendFactor = dstFactor;
  80. mBlendColor = blendColor;
  81. }
  82. /// Turns-off blend mode.
  83. inline void setBlendOff( void )
  84. {
  85. // Ignore no change,
  86. if ( !mBlendMode )
  87. return;
  88. // Flush.
  89. flush( mpDebugStats->batchBlendStateFlush );
  90. mBlendMode = false;
  91. }
  92. // Set blend mode using a specific scene render request.
  93. void setBlendMode( const SceneRenderRequest* pSceneRenderRequest );
  94. /// Set alpha-test mode.
  95. inline void setAlphaTestMode( const F32 alphaTestMode )
  96. {
  97. // Ignore no change.
  98. if ( mIsEqual( mAlphaTestMode, alphaTestMode ) )
  99. return;
  100. // Flush.
  101. flush( mpDebugStats->batchAlphaStateFlush );
  102. // Stats.
  103. mpDebugStats->batchAlphaStateFlush++;
  104. mAlphaTestMode = alphaTestMode;
  105. }
  106. // Set alpha test mode using a specific scene render request.
  107. void setAlphaTestMode( const SceneRenderRequest* pSceneRenderRequest );
  108. /// Sets the wireframe mode.
  109. inline void setWireframeMode( const bool enabled )
  110. {
  111. // Ignore no change.
  112. if ( mWireframeMode == enabled )
  113. return;
  114. mWireframeMode = enabled;
  115. }
  116. // Gets the wireframe mode.
  117. inline bool getWireframeMode( void ) const { return mWireframeMode; }
  118. /// Sets the batch enabled mode.
  119. inline void setBatchEnabled( const bool enabled )
  120. {
  121. // Ignore no change.
  122. if ( mBatchEnabled == enabled )
  123. return;
  124. // Flush.
  125. flushInternal();
  126. mBatchEnabled = enabled;
  127. }
  128. /// Gets the batch enabled mode.
  129. inline bool getBatchEnabled( void ) const { return mBatchEnabled; }
  130. /// Sets the debug stats to use.
  131. inline void setDebugStats( DebugStats* pDebugStats ) { mpDebugStats = pDebugStats; }
  132. /// Submit a quad for batching.
  133. /// Vertex and textures are indexed as:
  134. /// 3 ___ 2
  135. /// |\ |
  136. /// | \ |
  137. /// 0| _\|1
  138. void SubmitQuad(
  139. const Vector2& vertexPos0,
  140. const Vector2& vertexPos1,
  141. const Vector2& vertexPos2,
  142. const Vector2& vertexPos3,
  143. const Vector2& texturePos0,
  144. const Vector2& texturePos1,
  145. const Vector2& texturePos2,
  146. const Vector2& texturePos3,
  147. TextureHandle& texture,
  148. const ColorF& color = ColorF(-1.0f, -1.0f, -1.0f) );
  149. /// Render a quad immediately without affecting current batch.
  150. /// All render state should be set beforehand directly.
  151. /// Vertex and textures are indexed as:
  152. /// 3 ___ 2
  153. /// |\ |
  154. /// | \ |
  155. /// 0| _\|1
  156. static void RenderQuad(
  157. const Vector2& vertexPos0,
  158. const Vector2& vertexPos1,
  159. const Vector2& vertexPos2,
  160. const Vector2& vertexPos3,
  161. const Vector2& texturePos0,
  162. const Vector2& texturePos1,
  163. const Vector2& texturePos2,
  164. const Vector2& texturePos3 );
  165. /// Flush (render) any pending batches with a reason metric.
  166. void flush( U32& reasonMetric );
  167. /// Flush (render) any pending batches.
  168. void flush( void );
  169. private:
  170. /// Flush (render) any pending batches.
  171. void flushInternal( void );
  172. private:
  173. typedef Vector<U32> indexVectorType;
  174. typedef HashMap<U32, indexVectorType*> textureBatchType;
  175. VectorPtr< indexVectorType* > mIndexVectorPool;
  176. textureBatchType mTextureBatchMap;
  177. const ColorF NoColor;
  178. Vector2 mVertexBuffer[ BATCHRENDER_BUFFERSIZE ];
  179. Vector2 mTextureBuffer[ BATCHRENDER_BUFFERSIZE ];
  180. U16 mIndexBuffer[ BATCHRENDER_BUFFERSIZE ];
  181. ColorF mColorBuffer[ BATCHRENDER_BUFFERSIZE ];
  182. U32 mQuadCount;
  183. U32 mVertexCount;
  184. U32 mTextureResidentCount;
  185. U32 mIndexCount;
  186. U32 mColorCount;
  187. bool mBlendMode;
  188. GLenum mSrcBlendFactor;
  189. GLenum mDstBlendFactor;
  190. ColorF mBlendColor;
  191. F32 mAlphaTestMode;
  192. bool mStrictOrderMode;
  193. TextureHandle mStrictOrderTextureHandle;
  194. DebugStats* mpDebugStats;
  195. bool mWireframeMode;
  196. bool mBatchEnabled;
  197. };
  198. #endif