BatchRender.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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/gColor.h"
  41. #endif
  42. //-----------------------------------------------------------------------------
  43. #define BATCHRENDER_BUFFERSIZE (65535)
  44. #define BATCHRENDER_MAXTRIANGLES (BATCHRENDER_BUFFERSIZE/3)
  45. //-----------------------------------------------------------------------------
  46. class SceneRenderRequest;
  47. //-----------------------------------------------------------------------------
  48. class BatchRender
  49. {
  50. private:
  51. struct TriangleRun
  52. {
  53. enum PrimitiveMode
  54. {
  55. TRIANGLE,
  56. QUAD,
  57. };
  58. TriangleRun( const PrimitiveMode primitive, const U32 primitiveCount, const U32 startIndex ) :
  59. mPrimitiveMode( primitive ),
  60. mPrimitiveCount( primitiveCount ),
  61. mStartIndex( startIndex )
  62. { }
  63. PrimitiveMode mPrimitiveMode;
  64. U32 mPrimitiveCount;
  65. U32 mStartIndex;
  66. };
  67. typedef Vector<TriangleRun> indexVectorType;
  68. typedef HashMap<U32, indexVectorType*> textureBatchType;
  69. VectorPtr< indexVectorType* > mIndexVectorPool;
  70. textureBatchType mTextureBatchMap;
  71. const ColorF NoColor;
  72. Vector2 mVertexBuffer[ BATCHRENDER_BUFFERSIZE ];
  73. Vector2 mTextureBuffer[ BATCHRENDER_BUFFERSIZE ];
  74. U16 mIndexBuffer[ BATCHRENDER_BUFFERSIZE ];
  75. ColorF mColorBuffer[ BATCHRENDER_BUFFERSIZE ];
  76. U32 mTriangleCount;
  77. U32 mVertexCount;
  78. U32 mTextureCoordCount;
  79. U32 mIndexCount;
  80. U32 mColorCount;
  81. bool mBlendMode;
  82. GLenum mSrcBlendFactor;
  83. GLenum mDstBlendFactor;
  84. ColorF mBlendColor;
  85. F32 mAlphaTestMode;
  86. bool mStrictOrderMode;
  87. TextureHandle mStrictOrderTextureHandle;
  88. DebugStats* mpDebugStats;
  89. bool mWireframeMode;
  90. bool mBatchEnabled;
  91. public:
  92. BatchRender();
  93. virtual ~BatchRender();
  94. static const U32 maxVertexCount = BATCHRENDER_BUFFERSIZE;
  95. /// Set the strict order mode.
  96. inline void setStrictOrderMode( const bool strictOrder, const bool forceFlush = false )
  97. {
  98. // Ignore if no change.
  99. if ( !forceFlush && strictOrder == mStrictOrderMode )
  100. return;
  101. // Flush.
  102. flushInternal();
  103. // Update strict order mode.
  104. mStrictOrderMode = strictOrder;
  105. }
  106. /// Gets the strict order mode.
  107. inline bool getStrictOrderMode( void ) const { return mStrictOrderMode; }
  108. /// Turns-on blend mode with the specified blend factors and color.
  109. inline void setBlendMode( GLenum srcFactor, GLenum dstFactor, const ColorF& blendColor = ColorF(1.0f, 1.0f, 1.0f, 1.0f))
  110. {
  111. // Ignore no change.
  112. if ( mBlendMode &&
  113. mSrcBlendFactor == srcFactor &&
  114. mDstBlendFactor == dstFactor &&
  115. mBlendColor == blendColor )
  116. return;
  117. // Flush.
  118. flush( mpDebugStats->batchBlendStateFlush );
  119. mBlendMode = true;
  120. mSrcBlendFactor = srcFactor;
  121. mDstBlendFactor = dstFactor;
  122. mBlendColor = blendColor;
  123. }
  124. /// Turns-off blend mode.
  125. inline void setBlendOff( void )
  126. {
  127. // Ignore no change,
  128. if ( !mBlendMode )
  129. return;
  130. // Flush.
  131. flush( mpDebugStats->batchBlendStateFlush );
  132. mBlendMode = false;
  133. }
  134. // Set blend mode using a specific scene render request.
  135. void setBlendMode( const SceneRenderRequest* pSceneRenderRequest );
  136. /// Set alpha-test mode.
  137. void setAlphaTestMode( const F32 alphaTestMode )
  138. {
  139. // Ignore no change.
  140. if ( mIsEqual( mAlphaTestMode, alphaTestMode ) )
  141. return;
  142. // Flush.
  143. flush( mpDebugStats->batchAlphaStateFlush );
  144. // Stats.
  145. mpDebugStats->batchAlphaStateFlush++;
  146. mAlphaTestMode = alphaTestMode;
  147. }
  148. // Set alpha test mode using a specific scene render request.
  149. void setAlphaTestMode( const SceneRenderRequest* pSceneRenderRequest );
  150. /// Sets the wireframe mode.
  151. inline void setWireframeMode( const bool enabled )
  152. {
  153. // Ignore no change.
  154. if ( mWireframeMode == enabled )
  155. return;
  156. mWireframeMode = enabled;
  157. }
  158. // Gets the wireframe mode.
  159. inline bool getWireframeMode( void ) const { return mWireframeMode; }
  160. /// Sets the batch enabled mode.
  161. inline void setBatchEnabled( const bool enabled )
  162. {
  163. // Ignore no change.
  164. if ( mBatchEnabled == enabled )
  165. return;
  166. // Flush.
  167. flushInternal();
  168. mBatchEnabled = enabled;
  169. }
  170. /// Gets the batch enabled mode.
  171. inline bool getBatchEnabled( void ) const { return mBatchEnabled; }
  172. /// Sets the debug stats to use.
  173. inline void setDebugStats( DebugStats* pDebugStats ) { mpDebugStats = pDebugStats; }
  174. /// Submit triangles for batching.
  175. /// Vertex and textures are indexed as:
  176. /// 2 5
  177. /// |\ |\
  178. /// | \ | \
  179. /// 0| _\1 3| _\4
  180. void SubmitTriangles(
  181. const U32 vertexCount,
  182. const Vector2* pVertexArray,
  183. const Vector2* pTextureArray,
  184. const ColorF* pColorArray,
  185. TextureHandle& texture);
  186. /// Submit a quad for batching.
  187. /// Vertex and textures are indexed as:
  188. /// 3 ___ 2
  189. /// |\ |
  190. /// | \ |
  191. /// 0| _\|1
  192. void SubmitQuad(
  193. const Vector2& vertexPos0,
  194. const Vector2& vertexPos1,
  195. const Vector2& vertexPos2,
  196. const Vector2& vertexPos3,
  197. const Vector2& texturePos0,
  198. const Vector2& texturePos1,
  199. const Vector2& texturePos2,
  200. const Vector2& texturePos3,
  201. TextureHandle& texture,
  202. const ColorF& color0 = ColorF(-1.0f, -1.0f, -1.0f),
  203. const ColorF& color1 = ColorF(-1.0f, -1.0f, -1.0f),
  204. const ColorF& color2 = ColorF(-1.0f, -1.0f, -1.0f),
  205. const ColorF& color3 = ColorF(-1.0f, -1.0f, -1.0f));
  206. /// Render a quad immediately without affecting current batch.
  207. /// All render state should be set beforehand directly.
  208. /// Vertex and textures are indexed as:
  209. /// 3 ___ 2
  210. /// |\ |
  211. /// | \ |
  212. /// 0| _\|1
  213. static void RenderQuad(
  214. const Vector2& vertexPos0,
  215. const Vector2& vertexPos1,
  216. const Vector2& vertexPos2,
  217. const Vector2& vertexPos3,
  218. const Vector2& texturePos0,
  219. const Vector2& texturePos1,
  220. const Vector2& texturePos2,
  221. const Vector2& texturePos3 )
  222. {
  223. glBegin( GL_TRIANGLE_STRIP );
  224. glTexCoord2f( texturePos0.x, texturePos0.y );
  225. glVertex2f( vertexPos0.x, vertexPos0.y );
  226. glTexCoord2f( texturePos1.x, texturePos1.y );
  227. glVertex2f( vertexPos1.x, vertexPos1.y );
  228. glTexCoord2f( texturePos3.x, texturePos3.y );
  229. glVertex2f( vertexPos3.x, vertexPos3.y );
  230. glTexCoord2f( texturePos2.x, texturePos2.y );
  231. glVertex2f( vertexPos2.x, vertexPos2.y );
  232. glEnd();
  233. }
  234. /// Flush (render) any pending batches with a reason metric.
  235. void flush( U32& reasonMetric );
  236. /// Flush (render) any pending batches.
  237. void flush( void );
  238. private:
  239. /// Flush (render) any pending batches.
  240. void flushInternal( void );
  241. /// Find texture batch.
  242. indexVectorType* findTextureBatch( TextureHandle& handle );
  243. };
  244. #endif