VeldridContent.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Numerics;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Vulkan;
  8. using Veldrid.SPIRV;
  9. using System.Diagnostics;
  10. using System.Drawing;
  11. namespace Veldrid.Common
  12. {
  13. public sealed partial class VeldridContent: IDisposable
  14. {
  15. public Axis.LineAxis AddLineAxis()
  16. {
  17. Axis.LineAxis lineAxis = new Axis.LineAxis(this);
  18. lineAxis.CreateResources();
  19. renders.Add(lineAxis);
  20. return lineAxis;
  21. }
  22. public Vector2 WindowScale { get; set; } = Vector2.One;
  23. public ImagesRender AddImages(BitmapData bitmapData, SizeF cursorsize, SizeF gridsize)
  24. {
  25. ImagesRender imagesRender = new ImagesRender(this,bitmapData,cursorsize,gridsize);
  26. imagesRender.CreateResources();
  27. renders.Add(imagesRender);
  28. return imagesRender;
  29. }
  30. public VeldridText AddText(Boolean supportbackColor = false, Boolean supportbackTransparent = true)
  31. {
  32. VeldridText text = new VeldridText(this,supportbackColor,supportbackTransparent);
  33. text.CreateResources();
  34. renders.Add(text);
  35. return text;
  36. }
  37. public MutiText AddMutiText(Boolean supportbackColor = false, Boolean supportbackTransparent = true)
  38. {
  39. MutiText text = new MutiText(this, supportbackColor, supportbackTransparent);
  40. text.CreateResources();
  41. renders.Add(text);
  42. return text;
  43. }
  44. public Plot.LineSeries AddLineSeries()
  45. {
  46. Plot.LineSeries series = new Plot.LineSeries(this);
  47. series.CreateResources();
  48. renders.Add(series);
  49. return series;
  50. }
  51. public Plot.DataReViewPlot AddReviewPlot()
  52. {
  53. Plot.DataReViewPlot plot= new Plot.DataReViewPlot(this);
  54. plot.CreateResources();
  55. renders.Add(plot);
  56. return plot;
  57. }
  58. public Legend.Legend AddLegend()
  59. {
  60. Legend.Legend legend = new Legend.Legend(this);
  61. legend.CreateResources();
  62. renders.Add(legend);
  63. return legend;
  64. }
  65. private List<BaseVeldridRender> renders = new List<BaseVeldridRender>();
  66. private VeldridText text;
  67. internal GraphicsManger GraphicsManger { get; }
  68. private bool disposedValue;
  69. public VeldridContent(GraphicsDevice device)
  70. {
  71. GraphicsManger = new GraphicsManger(device);
  72. text = new VeldridText(this,true,false);
  73. text.Margin = new Padding(40, 10, 10, 20);
  74. text.CreateResources();
  75. text.FontSize = 14;
  76. text.Color = System.Drawing.Color.White;
  77. text.Local = new System.Drawing.PointF(text.Range.MinX+20, text.Range.MinY+40);
  78. text.VerticalAlignment = VerticalAlignment.Bottom;
  79. text.HorizontalAlignment = HorizontalAlignment.Left;
  80. }
  81. public RgbaFloat BackColor = RgbaFloat.Black;
  82. private object locker= new object();
  83. public void Resize(uint width,uint height)
  84. {
  85. GraphicsManger.Device.ResizeMainWindow(width,height);
  86. renders.ForEach(x => x.Resize());
  87. text?.Resize();
  88. }
  89. public uint Width => GraphicsManger.Device.SwapchainFramebuffer.Width;
  90. public uint Height=>GraphicsManger.Device.SwapchainFramebuffer.Height;
  91. public void Render(bool runtimeplot = true)
  92. {
  93. lock (locker)
  94. {
  95. Stopwatch stopwatch = new Stopwatch();
  96. stopwatch.Start();
  97. GraphicsManger.CommandList.Begin();
  98. GraphicsManger.CommandList.SetFramebuffer(GraphicsManger.Device.MainSwapchain.Framebuffer);
  99. GraphicsManger.CommandList.ClearColorTarget(0, BackColor);
  100. renders.ForEach(x => x?.Draw());
  101. text?.Draw();
  102. GraphicsManger.CommandList.End();
  103. GraphicsManger.Device.SubmitCommands(GraphicsManger.CommandList);
  104. GraphicsManger.Device.SwapBuffers();
  105. GraphicsManger.Device.WaitForIdle();
  106. stopwatch.Stop();
  107. if (text != null) text.Text = string.Format("{0:f2}FPS", 1000 / stopwatch.Elapsed.TotalMilliseconds);
  108. }
  109. }
  110. private void Dispose(bool disposing)
  111. {
  112. if (!disposedValue)
  113. {
  114. if (disposing)
  115. {
  116. renders.ForEach(x => x?.DisposeResources());
  117. GraphicsManger?.Dispose();
  118. // TODO: 释放托管状态(托管对象)
  119. }
  120. // TODO: 释放未托管的资源(未托管的对象)并重写终结器
  121. // TODO: 将大型字段设置为 null
  122. disposedValue = true;
  123. }
  124. }
  125. // // TODO: 仅当“Dispose(bool disposing)”拥有用于释放未托管资源的代码时才替代终结器
  126. // ~VeldridContent()
  127. // {
  128. // // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
  129. // Dispose(disposing: false);
  130. // }
  131. public void Dispose()
  132. {
  133. // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
  134. Dispose(disposing: true);
  135. GC.SuppressFinalize(this);
  136. }
  137. }
  138. }