Batcher2D.cs 24 KB

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