DataReViewPlot.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Numerics;
  4. using System.Runtime.CompilerServices;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. namespace Veldrid.Common.Plot
  8. {
  9. public class DataReViewPlot : BaseVeldridRender,IZoomRender
  10. {
  11. public float HScale { get; set; } = 1;
  12. public float VScale { get; set; } = 1;
  13. public float HOffset { get; set; }
  14. public float VOffset { get; set; }
  15. public bool ZoomEnbled { get; set; } = true;
  16. struct ProjView
  17. {
  18. public Matrix4x4 Proj;
  19. public Matrix4x4 View;
  20. }
  21. private ProjView projView;
  22. Pipeline pipeline;
  23. private PlotInfo plotInfo;
  24. DeviceBuffer plotInfoBuffer;
  25. DeviceBuffer projViewBuffer;
  26. DeviceBuffer dataBuffer;
  27. ResourceLayout dataLayout;
  28. ResourceSet dataSet;
  29. ResourceLayout infoLayout;
  30. ResourceSet infoSet;
  31. Shader[] shaders;
  32. private Info info;
  33. private object locker = new object();
  34. public DataReViewPlot(VeldridContent control) : base(control)
  35. {
  36. }
  37. internal override void DisposeResources()
  38. {
  39. base.DisposeResources();
  40. pipeline?.Dispose();
  41. plotInfoBuffer?.Dispose();
  42. projViewBuffer?.Dispose();
  43. dataBuffer?.Dispose();
  44. dataLayout?.Dispose();
  45. dataSet?.Dispose();
  46. infoLayout?.Dispose();
  47. infoSet?.Dispose();
  48. resultBuffer?.Dispose();
  49. cpuBuffer?.Dispose();
  50. }
  51. internal override void CreateResources()
  52. {
  53. base.CreateResources();
  54. plotInfoBuffer = ResourceFactory.CreateBuffer(new BufferDescription((uint)Unsafe.SizeOf<Info>(), BufferUsage.UniformBuffer));
  55. projViewBuffer = ResourceFactory.CreateBuffer(new BufferDescription((uint)Unsafe.SizeOf<ProjView>(), BufferUsage.UniformBuffer));
  56. infoLayout = ResourceFactory.CreateResourceLayout(new ResourceLayoutDescription(
  57. new ResourceLayoutElementDescription("InfoBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex),
  58. new ResourceLayoutElementDescription("ProjView", ResourceKind.UniformBuffer, ShaderStages.Vertex)));
  59. infoSet = ResourceFactory.CreateResourceSet(new ResourceSetDescription(infoLayout, plotInfoBuffer, projViewBuffer));
  60. shaders = CreateShader("DataReViewPlot");
  61. CreateBuffer(100);
  62. }
  63. public Vector2 WindowScale { get; set; } = Vector2.One;
  64. public void SetData(IntPtr ptr,uint bytescount)
  65. {
  66. if(dataBuffer.SizeInBytes<bytescount)
  67. {
  68. CreateBuffer(bytescount);
  69. }
  70. GraphicsDevice.UpdateBuffer(dataBuffer, 0, ptr, bytescount);
  71. }
  72. public void SetData(float[] datas)
  73. {
  74. if (datas == null || datas.Length == 0) return;
  75. uint bytescount = (uint)(datas.Length * Unsafe.SizeOf<float>());
  76. if (dataBuffer.SizeInBytes < bytescount)
  77. {
  78. CreateBuffer(bytescount);
  79. }
  80. GraphicsDevice.UpdateBuffer(dataBuffer, 0, datas);
  81. }
  82. private void CreateBuffer(uint bytescount)
  83. {
  84. dataBuffer?.Dispose();
  85. dataLayout?.Dispose();
  86. dataSet?.Dispose();
  87. pipeline?.Dispose();
  88. dataBuffer = ResourceFactory.CreateBuffer(new BufferDescription(bytescount, BufferUsage.StructuredBufferReadWrite,(uint)Unsafe.SizeOf<float>()));
  89. dataLayout = ResourceFactory.CreateResourceLayout(new ResourceLayoutDescription(new ResourceLayoutElementDescription("DataBuffer", ResourceKind.StructuredBufferReadOnly, ShaderStages.Vertex)));
  90. dataSet = ResourceFactory.CreateResourceSet(new ResourceSetDescription(dataLayout, dataBuffer));
  91. pipeline = CreatePipLine(PrimitiveTopology.LineStrip, new[] { dataLayout, infoLayout }, Array.Empty<VertexLayoutDescription>(), shaders);
  92. }
  93. internal override void DrawData()
  94. {
  95. base.DrawData();
  96. if (PlotInfos == null || PlotInfos.Length == 0 || TotalCount == 0) return;
  97. projView.Proj = base.OrthographicMatrix;
  98. projView.View = GetLineMatrix(HScale,VScale,HOffset/(MainSwapchainBuffer.Width/ WindowScale.X)*2 * (Rectangle.Width/MainSwapchainBuffer.Width),VOffset/(MainSwapchainBuffer.Height/ WindowScale.Y)*2*(Rectangle.Width/MainSwapchainBuffer.Width));
  99. CommandList.UpdateBuffer(projViewBuffer,0, projView);
  100. CommandList.SetFramebuffer(MainSwapchainBuffer);
  101. CommandList.SetPipeline(pipeline);
  102. CommandList.SetGraphicsResourceSet(0, dataSet);
  103. CommandList.SetGraphicsResourceSet(1, infoSet);
  104. info.PlotCount = (uint)PlotInfos.Length;
  105. for (uint i=0;i<PlotInfos.Length;i++)
  106. {
  107. if (!PlotInfos[i].Visibily) continue;
  108. info.Color = PlotInfos[i].Color;
  109. info.Interval = Range.XLenght / TotalCount;
  110. info.PlotDataLenght = TotalCount;
  111. info.PlotIndex = i;
  112. CommandList.UpdateBuffer(plotInfoBuffer, 0, info);
  113. CommandList.Draw(TotalCount);
  114. }
  115. }
  116. DeviceBuffer resultBuffer;
  117. DeviceBuffer cpuBuffer;
  118. public PlotInfo[] PlotInfos { get; set; } = new PlotInfo[0];
  119. public uint TotalCount { get; set; }
  120. [StructLayout(LayoutKind.Sequential,Pack =1)]
  121. struct Info
  122. {
  123. public RgbaFloat Color;
  124. public uint PlotCount;
  125. public float Interval;
  126. public uint PlotIndex;
  127. public uint PlotDataLenght;
  128. }
  129. }
  130. public class PlotInfo
  131. {
  132. public RgbaFloat Color;
  133. public bool Visibily;
  134. public string Name = string.Empty;
  135. }
  136. }