CustomRenderer.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // Custom Renderer
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using AtomicEngine;
  6. namespace AtomicBlaster
  7. {
  8. [StructLayout(LayoutKind.Sequential)]
  9. struct PositionColorUVVertex
  10. {
  11. public float X, Y, Z;
  12. public uint Color;
  13. public float U, V;
  14. };
  15. class Batch
  16. {
  17. public Texture2D Texture;
  18. public uint VertexCount = 0;
  19. public PositionColorUVVertex[] Vertices = new PositionColorUVVertex[256];
  20. }
  21. struct DrawItem
  22. {
  23. public Texture2D Texture;
  24. public uint StartVertex;
  25. public uint VertexCount;
  26. }
  27. static class CustomRenderer
  28. {
  29. // 250k max vertices
  30. const uint maxVertices = 256 * 1024;
  31. static uint totalVertex = 0;
  32. static VertexBuffer vertexBuffer;
  33. static ShaderVariation pixelShader;
  34. static ShaderVariation vertexShader;
  35. static SortedDictionary<float, Dictionary<Texture2D, Batch>> layerBatches = new SortedDictionary<float, Dictionary<Texture2D, Batch>>();
  36. public static void Initialize()
  37. {
  38. var graphics = AtomicNET.GetSubsystem<Graphics>();
  39. pixelShader = graphics.GetShader(ShaderType.PS, "Atomic2D");
  40. vertexShader = graphics.GetShader(ShaderType.VS, "Atomic2D");
  41. vertexBuffer = new VertexBuffer();
  42. vertexBuffer.SetSize(maxVertices, Constants.MASK_POSITION | Constants.MASK_COLOR | Constants.MASK_TEXCOORD1, true);
  43. }
  44. public static void Begin()
  45. {
  46. totalVertex = 0;
  47. // reset batches
  48. foreach (var layer in layerBatches)
  49. {
  50. foreach (var batch in layer.Value.Values)
  51. {
  52. batch.VertexCount = 0;
  53. }
  54. }
  55. }
  56. unsafe public static void End()
  57. {
  58. List<DrawItem> drawList = new List<DrawItem>();
  59. if (totalVertex == 0)
  60. return;
  61. IntPtr vertexData = vertexBuffer.Lock(0, totalVertex, true);
  62. if (vertexData == IntPtr.Zero)
  63. return;
  64. uint startVertex = 0;
  65. PositionColorUVVertex* vout = (PositionColorUVVertex*)vertexData;
  66. foreach (var layer in layerBatches)
  67. {
  68. foreach (var batch in layer.Value.Values)
  69. {
  70. if (totalVertex + batch.VertexCount >= maxVertices)
  71. {
  72. throw new System.InvalidOperationException("Ran out of vertices");
  73. }
  74. if (batch.VertexCount == 0)
  75. continue;
  76. // faster blit possible?
  77. for (uint i = 0; i < batch.VertexCount; i++, vout++)
  78. {
  79. *vout = batch.Vertices[i];
  80. }
  81. var item = new DrawItem();
  82. item.Texture = batch.Texture;
  83. item.StartVertex = startVertex;
  84. item.VertexCount = batch.VertexCount;
  85. startVertex += batch.VertexCount;
  86. drawList.Add(item);
  87. }
  88. }
  89. vertexBuffer.Unlock();
  90. var renderer = AtomicNET.GetSubsystem<Renderer>();
  91. var graphics = AtomicNET.GetSubsystem<Graphics>();
  92. var view = renderer.GetViewport(0).View;
  93. var camera = renderer.GetViewport(0).Camera;
  94. if (view == null || camera == null)
  95. return;
  96. graphics.SetBlendMode(BlendMode.BLEND_ADDALPHA);
  97. graphics.SetCullMode(CullMode.CULL_NONE);
  98. graphics.SetFillMode(FillMode.FILL_SOLID);
  99. graphics.SetDepthTest(CompareMode.CMP_ALWAYS);
  100. graphics.SetShaders(vertexShader, pixelShader);
  101. view.SetCameraShaderParameters(camera);
  102. graphics.SetShaderParameter(ShaderParams.VSP_MODEL, Matrix3x4.IDENTITY);
  103. graphics.SetShaderParameter(ShaderParams.PSP_MATDIFFCOLOR, Color.White);
  104. graphics.SetVertexBuffer(vertexBuffer);
  105. foreach (var item in drawList)
  106. {
  107. graphics.SetTexture((int) TextureUnit.TU_DIFFUSE, item.Texture);
  108. graphics.Draw(PrimitiveType.TRIANGLE_LIST, item.StartVertex, item.VertexCount);
  109. }
  110. graphics.SetTexture(0, null);
  111. }
  112. public static void Draw(CustomSprite texture, Vector2 position, Color color, float rotation, Vector2 origin, float scale, float layerDepth)
  113. {
  114. var w = texture.Width * scale;
  115. var h = texture.Height * scale;
  116. DrawInternal(texture,
  117. new Vector4(position.X, position.Y, w, h),
  118. color,
  119. rotation,
  120. origin * scale,
  121. layerDepth);
  122. }
  123. public static void Draw(CustomSprite texture, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, float layerDepth)
  124. {
  125. var w = texture.Width * scale.X;
  126. var h = texture.Height * scale.Y;
  127. DrawInternal(texture,
  128. new Vector4(position.X, position.Y, w, h),
  129. color,
  130. rotation,
  131. origin * scale,
  132. layerDepth);
  133. }
  134. public static void DrawLine(Vector2 start, Vector2 end, Color color, float thickness = 2f)
  135. {
  136. Vector2 delta = end - start;
  137. Draw(Art.Pixel, start, color, delta.ToAngle(), new Vector2(0, 0.5f), new Vector2(delta.Length, thickness), 0f);
  138. }
  139. static void DrawInternal(CustomSprite sprite, Vector4 destinationRectangle, Color color, float rotation, Vector2 origin, float depth)
  140. {
  141. Dictionary<Texture2D, Batch> batches;
  142. if (!layerBatches.TryGetValue(depth, out batches))
  143. {
  144. batches = new Dictionary<Texture2D, Batch>();
  145. layerBatches[depth] = batches;
  146. }
  147. Batch batch;
  148. var texture = sprite.Texture;
  149. if (!batches.TryGetValue(texture, out batch))
  150. {
  151. batch = new Batch();
  152. batch.Texture = texture;
  153. batches[texture] = batch;
  154. }
  155. if (totalVertex + 6 >= maxVertices)
  156. {
  157. throw new System.InvalidOperationException("Ran out of vertices");
  158. }
  159. totalVertex += 6;
  160. if (batch.VertexCount + 6 >= batch.Vertices.Length)
  161. {
  162. Array.Resize(ref batch.Vertices, batch.Vertices.Length * 2);
  163. }
  164. if (rotation == 0f)
  165. {
  166. Set(batch.Vertices, ref batch.VertexCount, destinationRectangle.X - origin.X,
  167. destinationRectangle.Y - origin.Y,
  168. destinationRectangle.Z,
  169. destinationRectangle.W,
  170. color,
  171. sprite.TexCoordTL,
  172. sprite.TexCoordBR,
  173. depth);
  174. }
  175. else
  176. {
  177. Set(batch.Vertices, ref batch.VertexCount, destinationRectangle.X,
  178. destinationRectangle.Y,
  179. -origin.X,
  180. -origin.Y,
  181. destinationRectangle.Z,
  182. destinationRectangle.W,
  183. (float)Math.Sin(rotation),
  184. (float)Math.Cos(rotation),
  185. color,
  186. sprite.TexCoordTL,
  187. sprite.TexCoordBR,
  188. depth);
  189. }
  190. }
  191. static Vector2 _texCoordTL = new Vector2(0, 0);
  192. static Vector2 _texCoordBR = new Vector2(1, 1);
  193. static PositionColorUVVertex vertexTL = new PositionColorUVVertex();
  194. static PositionColorUVVertex vertexTR = new PositionColorUVVertex();
  195. static PositionColorUVVertex vertexBL = new PositionColorUVVertex();
  196. static PositionColorUVVertex vertexBR = new PositionColorUVVertex();
  197. // Portions Copyright (C) The MonoGame Team
  198. static public void Set(PositionColorUVVertex[] vertices, ref uint vertexCount, float x, float y, float dx, float dy, float w, float h, float sin, float cos, Color color, Vector2 texCoordTL, Vector2 texCoordBR, float depth)
  199. {
  200. uint ucolor = color.ToUInt();
  201. vertexTL.X = x + dx * cos - dy * sin;
  202. vertexTL.Y = y + dx * sin + dy * cos;
  203. vertexTL.Z = depth;
  204. vertexTL.Color = ucolor;
  205. vertexTL.U = texCoordTL.X;
  206. vertexTL.V = texCoordTL.Y;
  207. vertexTR.X = x + (dx + w) * cos - dy * sin;
  208. vertexTR.Y = y + (dx + w) * sin + dy * cos;
  209. vertexTR.Z = depth;
  210. vertexTR.Color = ucolor;
  211. vertexTR.U = texCoordBR.X;
  212. vertexTR.V = texCoordTL.Y;
  213. vertexBL.X = x + dx * cos - (dy + h) * sin;
  214. vertexBL.Y = y + dx * sin + (dy + h) * cos;
  215. vertexBL.Z = depth;
  216. vertexBL.Color = ucolor;
  217. vertexBL.U = texCoordTL.X;
  218. vertexBL.V = texCoordBR.Y;
  219. vertexBR.X = x + (dx + w) * cos - (dy + h) * sin;
  220. vertexBR.Y = y + (dx + w) * sin + (dy + h) * cos;
  221. vertexBR.Z = depth;
  222. vertexBR.Color = ucolor;
  223. vertexBR.U = texCoordBR.X;
  224. vertexBR.V = texCoordBR.Y;
  225. vertices[vertexCount++] = vertexTL;
  226. vertices[vertexCount++] = vertexTR;
  227. vertices[vertexCount++] = vertexBL;
  228. vertices[vertexCount++] = vertexTR;
  229. vertices[vertexCount++] = vertexBR;
  230. vertices[vertexCount++] = vertexBL;
  231. }
  232. static public void Set(PositionColorUVVertex[] vertices, ref uint vertexCount, float x, float y, float w, float h, Color color, Vector2 texCoordTL, Vector2 texCoordBR, float depth)
  233. {
  234. uint ucolor = color.ToUInt();
  235. vertexTL.X = x;
  236. vertexTL.Y = y;
  237. vertexTL.Z = depth;
  238. vertexTL.Color = ucolor;
  239. vertexTL.U = texCoordTL.X;
  240. vertexTL.V = texCoordTL.Y;
  241. vertexTR.X = x + w;
  242. vertexTR.Y = y;
  243. vertexTR.Z = depth;
  244. vertexTR.Color = ucolor;
  245. vertexTR.U = texCoordBR.X;
  246. vertexTR.V = texCoordTL.Y;
  247. vertexBL.X = x;
  248. vertexBL.Y = y + h;
  249. vertexBL.Z = depth;
  250. vertexBL.Color = ucolor;
  251. vertexBL.U = texCoordTL.X;
  252. vertexBL.V = texCoordBR.Y;
  253. vertexBR.X = x + w;
  254. vertexBR.Y = y + h;
  255. vertexBR.Z = depth;
  256. vertexBR.Color = ucolor;
  257. vertexBR.U = texCoordBR.X;
  258. vertexBR.V = texCoordBR.Y;
  259. vertices[vertexCount++] = vertexTL;
  260. vertices[vertexCount++] = vertexTR;
  261. vertices[vertexCount++] = vertexBL;
  262. vertices[vertexCount++] = vertexTR;
  263. vertices[vertexCount++] = vertexBR;
  264. vertices[vertexCount++] = vertexBL;
  265. }
  266. }
  267. }