Batcher2D.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. using System;
  2. using System.Text;
  3. using System.ComponentModel;
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices;
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using MonoGame.Extended.Graphics.Effects;
  10. using MonoGame.Extended.Graphics.Geometry;
  11. using MonoGame.Extended.BitmapFonts;
  12. namespace MonoGame.Extended.Graphics
  13. {
  14. /// <summary>
  15. /// A general purpose <see cref="Batcher{TDrawCallInfo}" /> for two-dimensional geometry that change
  16. /// frequently between frames such as sprites and shapes.
  17. /// </summary>
  18. /// <seealso cref="IDisposable" />
  19. public sealed class Batcher2D : Batcher<Batcher2D.DrawCallInfo>
  20. {
  21. internal const int DefaultMaximumVerticesCount = 8192;
  22. internal const int DefaultMaximumIndicesCount = 12288;
  23. private readonly VertexBuffer _vertexBuffer;
  24. private readonly IndexBuffer _indexBuffer;
  25. private readonly VertexPositionColorTexture[] _vertices;
  26. private int _vertexCount;
  27. private readonly ushort[] _indices;
  28. private int _indexCount;
  29. private readonly ushort[] _sortedIndices;
  30. private readonly GeometryBuilder2D _geometryBuilder;
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="Batcher2D" /> class.
  33. /// </summary>
  34. /// <param name="graphicsDevice">The graphics device.</param>
  35. /// <param name="maximumVerticesCount">
  36. /// The maximum number of vertices that can be enqueued before a
  37. /// <see cref="Batcher{TDrawCallInfo}.Flush" /> is required. The default value is <code>8192</code>.
  38. /// </param>
  39. /// <param name="maximumIndicesCount">
  40. /// The maximum number of indices that can be enqueued before a
  41. /// <see cref="Batcher{TDrawCallInfo}.Flush" /> is required. The default value is <code>12288</code>.
  42. /// </param>
  43. /// <param name="maximumDrawCallsCount">
  44. /// The maximum number of <see cref="DrawCallInfo" /> structs that can be enqueued before a
  45. /// <see cref="Batcher{TDrawCallInfo}.Flush" /> is required. The default value is <code>2048</code>.
  46. /// </param>
  47. /// <exception cref="ArgumentNullException"><paramref name="graphicsDevice" />.</exception>
  48. /// <exception cref="ArgumentOutOfRangeException">
  49. /// <paramref name="maximumDrawCallsCount" /> is less than or equal
  50. /// <code>0</code>, or <paramref name="maximumVerticesCount" /> is less than or equal to <code>0</code>, or,
  51. /// <paramref name="maximumVerticesCount" /> is less than or equal to <code>0</code>.
  52. /// </exception>
  53. public Batcher2D(GraphicsDevice graphicsDevice,
  54. ushort maximumVerticesCount = DefaultMaximumVerticesCount,
  55. ushort maximumIndicesCount = DefaultMaximumIndicesCount,
  56. int maximumDrawCallsCount = DefaultBatchMaximumDrawCallsCount)
  57. : base(
  58. graphicsDevice,
  59. new DefaultEffect(graphicsDevice)
  60. {
  61. TextureEnabled = true,
  62. VertexColorEnabled = true
  63. }, maximumDrawCallsCount)
  64. {
  65. _vertices = new VertexPositionColorTexture[maximumVerticesCount];
  66. _vertexBuffer = new DynamicVertexBuffer(graphicsDevice, VertexPositionColorTexture.VertexDeclaration, maximumVerticesCount, BufferUsage.WriteOnly);
  67. _indices = new ushort[maximumIndicesCount];
  68. _sortedIndices = new ushort[maximumIndicesCount];
  69. _indexBuffer = new DynamicIndexBuffer(graphicsDevice, IndexElementSize.SixteenBits, maximumIndicesCount, BufferUsage.WriteOnly);
  70. _geometryBuilder = new GeometryBuilder2D(4, 6);
  71. }
  72. protected override void SortDrawCallsAndBindBuffers()
  73. {
  74. // Upload the vertices to the GPU and then select that vertex stream for drawing
  75. _vertexBuffer.SetData(_vertices, 0, _vertexCount);
  76. GraphicsDevice.SetVertexBuffer(_vertexBuffer);
  77. Array.Sort(DrawCalls, 0, EnqueuedDrawCallCount);
  78. BuildSortedIndices();
  79. // Upload the indices to the GPU and then select that index stream for drawing
  80. _indexBuffer.SetData(_sortedIndices, 0, _indexCount);
  81. GraphicsDevice.Indices = _indexBuffer;
  82. _indexCount = 0;
  83. _vertexCount = 0;
  84. }
  85. private void BuildSortedIndices()
  86. {
  87. var newDrawCallsCount = 0;
  88. DrawCalls[0].StartIndex = 0;
  89. var currentDrawCall = DrawCalls[0];
  90. DrawCalls[newDrawCallsCount++] = DrawCalls[0];
  91. var drawCallIndexCount = currentDrawCall.PrimitiveCount * 3;
  92. Array.Copy(_indices, currentDrawCall.StartIndex, _sortedIndices, 0, drawCallIndexCount);
  93. var sortedIndexCount = drawCallIndexCount;
  94. // iterate through sorted draw calls checking if any can now be merged to reduce expensive draw calls to the graphics API
  95. // this might need to be changed for next-gen graphics API (Vulkan, Metal, DirectX 12) where the draw calls are not so expensive
  96. for (var i = 1; i < EnqueuedDrawCallCount; i++)
  97. {
  98. currentDrawCall = DrawCalls[i];
  99. drawCallIndexCount = currentDrawCall.PrimitiveCount * 3;
  100. Array.Copy(_indices, currentDrawCall.StartIndex, _sortedIndices, sortedIndexCount, drawCallIndexCount);
  101. sortedIndexCount += drawCallIndexCount;
  102. if (currentDrawCall.TryMerge(ref DrawCalls[newDrawCallsCount - 1]))
  103. continue;
  104. currentDrawCall.StartIndex = sortedIndexCount;
  105. DrawCalls[newDrawCallsCount++] = currentDrawCall;
  106. }
  107. EnqueuedDrawCallCount = newDrawCallsCount;
  108. }
  109. /// <summary>
  110. /// Submits a draw operation to the <see cref="GraphicsDevice" /> using the specified <see cref="DrawCallInfo"/>.
  111. /// </summary>
  112. /// <param name="drawCall">The draw call information.</param>
  113. protected override void InvokeDrawCall(ref DrawCallInfo drawCall)
  114. {
  115. GraphicsDevice.DrawIndexedPrimitives(drawCall.PrimitiveType, 0, drawCall.StartIndex, drawCall.PrimitiveCount);
  116. }
  117. /// <summary>
  118. /// Draws a sprite using a specified <see cref="Texture" />, transform <see cref="Matrix3x2" />, source
  119. /// <see cref="Rectangle" />, and an optional
  120. /// <see cref="Color" />, origin <see cref="Vector2" />, <see cref="FlipFlags" />, and depth <see cref="float" />.
  121. /// </summary>
  122. /// <param name="texture">The <see cref="Texture" />.</param>
  123. /// <param name="transformMatrix">The transform <see cref="Matrix3x2" />.</param>
  124. /// <param name="sourceRectangle">
  125. /// The texture region <see cref="Rectangle" /> of the <paramref name="texture" />. Use
  126. /// <code>null</code> to use the entire <see cref="Texture2D" />.
  127. /// </param>
  128. /// <param name="color">The <see cref="Color" />. Use <code>null</code> to use the default <see cref="Color.White" />.</param>
  129. /// <param name="flags">The <see cref="FlipFlags" />. The default value is <see cref="FlipFlags.None" />.</param>
  130. /// <param name="depth">The depth <see cref="float" />. The default value is <code>0</code>.</param>
  131. /// <exception cref="InvalidOperationException">The <see cref="Batcher{TDrawCallInfo}.Begin(ref Matrix, ref Matrix, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect)" /> method has not been called.</exception>
  132. /// <exception cref="ArgumentNullException"><paramref name="texture" /> is null.</exception>
  133. public void DrawSprite(Texture2D texture, ref Matrix3x2 transformMatrix, ref Rectangle sourceRectangle,
  134. Color? color = null, FlipFlags flags = FlipFlags.None, float depth = 0)
  135. {
  136. _geometryBuilder.BuildSprite(_vertexCount, ref transformMatrix, texture, ref sourceRectangle, color, flags, depth);
  137. EnqueueBuiltGeometry(texture, depth);
  138. }
  139. /// <summary>
  140. /// Draws a <see cref="Texture" /> using the specified transform <see cref="Matrix3x2" /> and an optional
  141. /// <see cref="Color" />, origin <see cref="Vector2" />, <see cref="FlipFlags" />, and depth <see cref="float" />.
  142. /// </summary>
  143. /// <param name="texture">The <see cref="Texture" />.</param>
  144. /// <param name="transformMatrix">The transform <see cref="Matrix3x2" />.</param>
  145. /// <param name="color">The <see cref="Color" />. Use <code>null</code> to use the default <see cref="Color.White" />.</param>
  146. /// <param name="flags">The <see cref="FlipFlags" />. The default value is <see cref="FlipFlags.None" />.</param>
  147. /// <param name="depth">The depth <see cref="float" />. The default value is <code>0</code>.</param>
  148. /// <exception cref="InvalidOperationException">The <see cref="Batcher{TDrawCallInfo}.Begin(ref Matrix, ref Matrix, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect)" /> method has not been called.</exception>
  149. /// <exception cref="ArgumentNullException"><paramref name="texture" /> is null.</exception>
  150. public void DrawTexture(Texture2D texture, ref Matrix3x2 transformMatrix, Color? color = null,
  151. FlipFlags flags = FlipFlags.None, float depth = 0)
  152. {
  153. var rectangle = default(Rectangle);
  154. _geometryBuilder.BuildSprite(_vertexCount, ref transformMatrix, texture, ref rectangle, color, flags, depth);
  155. EnqueueBuiltGeometry(texture, depth);
  156. }
  157. private void EnqueueBuiltGeometry(Texture2D texture, float depth)
  158. {
  159. if ((_vertexCount + _geometryBuilder.VertexCount > _vertices.Length) ||
  160. (_indexCount + _geometryBuilder.IndexCount > _indices.Length))
  161. Flush();
  162. var drawCall = new DrawCallInfo(texture, _geometryBuilder.PrimitiveType, _indexCount,
  163. _geometryBuilder.PrimitivesCount, depth);
  164. Array.Copy(_geometryBuilder.Vertices, 0, _vertices, _vertexCount, _geometryBuilder.VertexCount);
  165. _vertexCount += _geometryBuilder.VertexCount;
  166. Array.Copy(_geometryBuilder.Indices, 0, _indices, _indexCount, _geometryBuilder.IndexCount);
  167. _indexCount += _geometryBuilder.IndexCount;
  168. Enqueue(ref drawCall);
  169. }
  170. /// <summary>
  171. /// Draws unicode (UTF-16) characters as sprites using the specified <see cref="BitmapFont" />, text
  172. /// <see cref="StringBuilder" />, transform <see cref="Matrix3x2" /> and optional <see cref="Color" />, origin
  173. /// <see cref="Vector2" />, <see cref="FlipFlags" />, and depth <see cref="float" />.
  174. /// </summary>
  175. /// <param name="bitmapFont">The <see cref="BitmapFont" />.</param>
  176. /// <param name="text">The text <see cref="StringBuilder" />.</param>
  177. /// <param name="transformMatrix">The transform <see cref="Matrix3x2" />.</param>
  178. /// <param name="color">
  179. /// The <see cref="Color" />. Use <code>null</code> to use the default
  180. /// <see cref="Color.White" />.
  181. /// </param>
  182. /// <param name="flags">The <see cref="FlipFlags" />. The default value is <see cref="FlipFlags.None" />.</param>
  183. /// <param name="depth">The depth <see cref="float" />. The default value is <code>0f</code>.</param>
  184. /// <exception cref="InvalidOperationException">The <see cref="Batcher{TDrawCallInfo}.Begin(ref Matrix, ref Matrix, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect)" /> method has not been called.</exception>
  185. /// <exception cref="ArgumentNullException"><paramref name="bitmapFont" /> is null or <paramref name="text" /> is null.</exception>
  186. public void DrawString(BitmapFont bitmapFont, StringBuilder text, ref Matrix3x2 transformMatrix,
  187. Color? color = null, FlipFlags flags = FlipFlags.None, float depth = 0f)
  188. {
  189. EnsureHasBegun();
  190. if (bitmapFont == null)
  191. throw new ArgumentNullException(nameof(bitmapFont));
  192. if (text == null)
  193. throw new ArgumentNullException(nameof(text));
  194. var lineSpacing = bitmapFont.LineHeight;
  195. var offset = new Vector2(0, 0);
  196. BitmapFontCharacter lastGlyph = null;
  197. for (var i = 0; i < text.Length;)
  198. {
  199. int character;
  200. if (char.IsLowSurrogate(text[i]))
  201. {
  202. character = char.ConvertToUtf32(text[i - 1], text[i]);
  203. i += 2;
  204. }
  205. else if (char.IsHighSurrogate(text[i]))
  206. {
  207. character = char.ConvertToUtf32(text[i], text[i - 1]);
  208. i += 2;
  209. }
  210. else
  211. {
  212. character = text[i];
  213. i += 1;
  214. }
  215. // ReSharper disable once SwitchStatementMissingSomeCases
  216. switch (character)
  217. {
  218. case '\r':
  219. continue;
  220. case '\n':
  221. offset.X = 0;
  222. offset.Y += lineSpacing;
  223. lastGlyph = null;
  224. continue;
  225. }
  226. var fontRegion = bitmapFont.GetCharacter(character);
  227. if (fontRegion == null)
  228. continue;
  229. var transform1Matrix = transformMatrix;
  230. transform1Matrix.M31 += offset.X + fontRegion.XOffset;
  231. transform1Matrix.M32 += offset.Y + fontRegion.YOffset;
  232. var textureRegion = fontRegion.TextureRegion;
  233. var bounds = textureRegion.Bounds;
  234. DrawSprite(textureRegion.Texture, ref transform1Matrix, ref bounds, color, flags, depth);
  235. var advance = fontRegion.XAdvance + bitmapFont.LetterSpacing;
  236. if (bitmapFont.UseKernings && lastGlyph != null)
  237. {
  238. int amount;
  239. if (lastGlyph.Kernings.TryGetValue(character, out amount))
  240. {
  241. advance += amount;
  242. }
  243. }
  244. offset.X += i != text.Length - 1
  245. ? advance
  246. : fontRegion.XOffset + fontRegion.TextureRegion.Width;
  247. lastGlyph = fontRegion;
  248. }
  249. }
  250. /// <summary>
  251. /// Draws unicode (UTF-16) characters as sprites using the specified <see cref="BitmapFont" />, text
  252. /// <see cref="StringBuilder" />, position <see cref="Vector2" /> and optional <see cref="Color" />, rotation
  253. /// <see cref="float" />, origin <see cref="Vector2" />, scale <see cref="Vector2" /> <see cref="FlipFlags" />, and
  254. /// depth <see cref="float" />.
  255. /// </summary>
  256. /// <param name="bitmapFont">The <see cref="BitmapFont" />.</param>
  257. /// <param name="text">The text <see cref="string" />.</param>
  258. /// <param name="position">The position <see cref="Vector2" />.</param>
  259. /// <param name="color">
  260. /// The <see cref="Color" />. Use <code>null</code> to use the default
  261. /// <see cref="Color.White" />.
  262. /// </param>
  263. /// <param name="rotation">
  264. /// The angle <see cref="float" /> (in radians) to rotate each sprite about its <paramref name="origin" />. The default
  265. /// value is <code>0f</code>.
  266. /// </param>
  267. /// <param name="origin">
  268. /// The origin <see cref="Vector2" />. Use <code>null</code> to use the default
  269. /// <see cref="Vector2.Zero" />.
  270. /// </param>
  271. /// <param name="scale">
  272. /// The scale <see cref="Vector2" />. Use <code>null</code> to use the default
  273. /// <see cref="Vector2.One" />.
  274. /// </param>
  275. /// <param name="flags">The <see cref="FlipFlags" />. The default value is <see cref="FlipFlags.None" />.</param>
  276. /// <param name="depth">The depth <see cref="float" />. The default value is <code>0f</code></param>
  277. /// <exception cref="InvalidOperationException">The <see cref="Batcher{TDrawCallInfo}.Begin(ref Matrix, ref Matrix, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect)" /> method has not been called.</exception>
  278. /// <exception cref="ArgumentNullException"><paramref name="bitmapFont" /> is null or <paramref name="text" /> is null.</exception>
  279. public void DrawString(BitmapFont bitmapFont, StringBuilder text, Vector2 position, Color? color = null,
  280. float rotation = 0f, Vector2? origin = null, Vector2? scale = null,
  281. FlipFlags flags = FlipFlags.None, float depth = 0f)
  282. {
  283. Matrix3x2 transformMatrix;
  284. Matrix3x2.CreateFrom(position, rotation, scale, origin, out transformMatrix);
  285. DrawString(bitmapFont, text, ref transformMatrix, color, flags, depth);
  286. }
  287. /// <summary>
  288. /// Draws unicode (UTF-16) characters as sprites using the specified <see cref="BitmapFont" />, text
  289. /// <see cref="string" />, transform <see cref="Matrix3x2" /> and optional <see cref="Color" />, origin
  290. /// <see cref="Vector2" />, <see cref="FlipFlags" />, and depth <see cref="float" />.
  291. /// </summary>
  292. /// <param name="bitmapFont">The <see cref="BitmapFont" />.</param>
  293. /// <param name="text">The text <see cref="string" />.</param>
  294. /// <param name="transformMatrix">The transform <see cref="Matrix3x2" />.</param>
  295. /// <param name="color">
  296. /// The <see cref="Color" />. Use <code>null</code> to use the default
  297. /// <see cref="Color.White" />.
  298. /// </param>
  299. /// <param name="flags">The <see cref="FlipFlags" />. The default value is <see cref="FlipFlags.None" />.</param>
  300. /// <param name="depth">The depth <see cref="float" />. The default value is <code>0f</code></param>
  301. /// <exception cref="InvalidOperationException">The <see cref="Batcher{TDrawCallInfo}.Begin(ref Matrix, ref Matrix, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect)" /> method has not been called.</exception>
  302. /// <exception cref="ArgumentNullException"><paramref name="bitmapFont" /> is null or <paramref name="text" /> is null.</exception>
  303. public void DrawString(BitmapFont bitmapFont, string text, ref Matrix3x2 transformMatrix, Color? color = null,
  304. FlipFlags flags = FlipFlags.None, float depth = 0f)
  305. {
  306. EnsureHasBegun();
  307. if (bitmapFont == null)
  308. throw new ArgumentNullException(nameof(bitmapFont));
  309. if (text == null)
  310. throw new ArgumentNullException(nameof(text));
  311. var glyphs = bitmapFont.GetGlyphs(text);
  312. foreach (var glyph in glyphs)
  313. {
  314. var transform1Matrix = transformMatrix;
  315. transform1Matrix.M31 += glyph.Position.X;
  316. transform1Matrix.M32 += glyph.Position.Y;
  317. var texture = glyph.Character.TextureRegion.Texture;
  318. var bounds = texture.Bounds;
  319. DrawSprite(texture, ref transform1Matrix, ref bounds, color, flags, depth);
  320. }
  321. }
  322. /// <summary>
  323. /// Draws unicode (UTF-16) characters as sprites using the specified <see cref="BitmapFont" />, text
  324. /// <see cref="string" />, position <see cref="Vector2" /> and optional <see cref="Color" />, rotation
  325. /// <see cref="float" />, origin <see cref="Vector2" />, scale <see cref="Vector2" /> <see cref="FlipFlags" />, and
  326. /// depth <see cref="float" />.
  327. /// </summary>
  328. /// <param name="bitmapFont">The <see cref="BitmapFont" />.</param>
  329. /// <param name="text">The text <see cref="string" />.</param>
  330. /// <param name="position">The position <see cref="Vector2" />.</param>
  331. /// <param name="color">
  332. /// The <see cref="Color" />. Use <code>null</code> to use the default
  333. /// <see cref="Color.White" />.
  334. /// </param>
  335. /// <param name="rotation">
  336. /// The angle <see cref="float" /> (in radians) to rotate each sprite about its <paramref name="origin" />. The default
  337. /// value is <code>0f</code>.
  338. /// </param>
  339. /// <param name="origin">
  340. /// The origin <see cref="Vector2" />. Use <code>null</code> to use the default
  341. /// <see cref="Vector2.Zero" />.
  342. /// </param>
  343. /// <param name="scale">
  344. /// The scale <see cref="Vector2" />. Use <code>null</code> to use the default
  345. /// <see cref="Vector2.One" />.
  346. /// </param>
  347. /// <param name="flags">The <see cref="FlipFlags" />. The default value is <see cref="FlipFlags.None" />.</param>
  348. /// <param name="depth">The depth <see cref="float" />. The default value is <code>0f</code></param>
  349. /// <exception cref="InvalidOperationException">The <see cref="Batcher{TDrawCallInfo}.Begin(ref Matrix, ref Matrix, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect)" /> method has not been called.</exception>
  350. /// <exception cref="ArgumentNullException"><paramref name="bitmapFont" /> is null or <paramref name="text" /> is null.</exception>
  351. public void DrawString(BitmapFont bitmapFont, string text, Vector2 position, Color? color = null,
  352. float rotation = 0f, Vector2? origin = null, Vector2? scale = null,
  353. FlipFlags flags = FlipFlags.None, float depth = 0f)
  354. {
  355. Matrix3x2 matrix;
  356. Matrix3x2.CreateFrom(position, rotation, scale, origin, out matrix);
  357. DrawString(bitmapFont, text, ref matrix, color, flags, depth);
  358. }
  359. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  360. [EditorBrowsable(EditorBrowsableState.Never)]
  361. public struct DrawCallInfo : IBatchDrawCallInfo<DrawCallInfo>, IComparable<DrawCallInfo>
  362. {
  363. internal readonly PrimitiveType PrimitiveType;
  364. internal int StartIndex;
  365. internal int PrimitiveCount;
  366. internal readonly Texture2D Texture;
  367. internal readonly uint TextureKey;
  368. internal readonly uint DepthKey;
  369. internal unsafe DrawCallInfo(Texture2D texture, PrimitiveType primitiveType, int startIndex, int primitiveCount, float depth)
  370. {
  371. PrimitiveType = primitiveType;
  372. StartIndex = startIndex;
  373. PrimitiveCount = primitiveCount;
  374. Texture = texture;
  375. TextureKey = (uint)RuntimeHelpers.GetHashCode(texture);
  376. DepthKey = *(uint*)&depth;
  377. }
  378. public void SetState(Effect effect)
  379. {
  380. var textureEffect = effect as ITextureEffect;
  381. if (textureEffect != null)
  382. textureEffect.Texture = Texture;
  383. }
  384. public bool TryMerge(ref DrawCallInfo drawCall)
  385. {
  386. if (PrimitiveType != drawCall.PrimitiveType || TextureKey != drawCall.TextureKey ||
  387. DepthKey != drawCall.DepthKey)
  388. return false;
  389. drawCall.PrimitiveCount += PrimitiveCount;
  390. return true;
  391. }
  392. [SuppressMessage("ReSharper", "ImpureMethodCallOnReadonlyValueField")]
  393. public int CompareTo(DrawCallInfo other)
  394. {
  395. var result = TextureKey.CompareTo(other.TextureKey);;
  396. if (result != 0)
  397. return result;
  398. result = DepthKey.CompareTo(other.DepthKey);
  399. return result != 0 ? result : ((byte)PrimitiveType).CompareTo((byte)other.PrimitiveType);
  400. }
  401. }
  402. }
  403. }