123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- using System;
- using System.Collections.Generic;
- using System.Numerics;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- using System.Text;
- namespace Veldrid.Common.Plot
- {
- public class DataReViewPlot : BaseVeldridRender,IZoomRender
- {
- public float HScale { get; set; } = 1;
- public float VScale { get; set; } = 1;
- public float HOffset { get; set; }
- public float VOffset { get; set; }
- public bool ZoomEnbled { get; set; } = true;
- struct ProjView
- {
- public Matrix4x4 Proj;
- public Matrix4x4 View;
- }
- private ProjView projView;
- Pipeline pipeline;
- private PlotInfo plotInfo;
- DeviceBuffer plotInfoBuffer;
- DeviceBuffer projViewBuffer;
- DeviceBuffer dataBuffer;
- ResourceLayout dataLayout;
- ResourceSet dataSet;
- ResourceLayout infoLayout;
- ResourceSet infoSet;
- Shader[] shaders;
- private Info info;
- private object locker = new object();
- public DataReViewPlot(VeldridContent control) : base(control)
- {
- }
- internal override void DisposeResources()
- {
- base.DisposeResources();
- pipeline?.Dispose();
- plotInfoBuffer?.Dispose();
- projViewBuffer?.Dispose();
- dataBuffer?.Dispose();
- dataLayout?.Dispose();
- dataSet?.Dispose();
- infoLayout?.Dispose();
- infoSet?.Dispose();
- resultBuffer?.Dispose();
- cpuBuffer?.Dispose();
- }
- internal override void CreateResources()
- {
- base.CreateResources();
- plotInfoBuffer = ResourceFactory.CreateBuffer(new BufferDescription((uint)Unsafe.SizeOf<Info>(), BufferUsage.UniformBuffer));
- projViewBuffer = ResourceFactory.CreateBuffer(new BufferDescription((uint)Unsafe.SizeOf<ProjView>(), BufferUsage.UniformBuffer));
- infoLayout = ResourceFactory.CreateResourceLayout(new ResourceLayoutDescription(
- new ResourceLayoutElementDescription("InfoBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex),
- new ResourceLayoutElementDescription("ProjView", ResourceKind.UniformBuffer, ShaderStages.Vertex)));
- infoSet = ResourceFactory.CreateResourceSet(new ResourceSetDescription(infoLayout, plotInfoBuffer, projViewBuffer));
- shaders = CreateShader("DataReViewPlot");
-
- CreateBuffer(100);
- }
- public Vector2 WindowScale { get; set; } = Vector2.One;
- public void SetData(IntPtr ptr,uint bytescount)
- {
- if(dataBuffer.SizeInBytes<bytescount)
- {
- CreateBuffer(bytescount);
- }
- GraphicsDevice.UpdateBuffer(dataBuffer, 0, ptr, bytescount);
- }
- public void SetData(float[] datas)
- {
- if (datas == null || datas.Length == 0) return;
- uint bytescount = (uint)(datas.Length * Unsafe.SizeOf<float>());
- if (dataBuffer.SizeInBytes < bytescount)
- {
- CreateBuffer(bytescount);
- }
- GraphicsDevice.UpdateBuffer(dataBuffer, 0, datas);
- }
- private void CreateBuffer(uint bytescount)
- {
- dataBuffer?.Dispose();
- dataLayout?.Dispose();
- dataSet?.Dispose();
- pipeline?.Dispose();
- dataBuffer = ResourceFactory.CreateBuffer(new BufferDescription(bytescount, BufferUsage.StructuredBufferReadWrite,(uint)Unsafe.SizeOf<float>()));
- dataLayout = ResourceFactory.CreateResourceLayout(new ResourceLayoutDescription(new ResourceLayoutElementDescription("DataBuffer", ResourceKind.StructuredBufferReadOnly, ShaderStages.Vertex)));
- dataSet = ResourceFactory.CreateResourceSet(new ResourceSetDescription(dataLayout, dataBuffer));
- pipeline = CreatePipLine(PrimitiveTopology.LineStrip, new[] { dataLayout, infoLayout }, Array.Empty<VertexLayoutDescription>(), shaders);
- }
- internal override void DrawData()
- {
- base.DrawData();
- if (PlotInfos == null || PlotInfos.Length == 0 || TotalCount == 0) return;
- projView.Proj = base.OrthographicMatrix;
- 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));
- CommandList.UpdateBuffer(projViewBuffer,0, projView);
- CommandList.SetFramebuffer(MainSwapchainBuffer);
- CommandList.SetPipeline(pipeline);
- CommandList.SetGraphicsResourceSet(0, dataSet);
- CommandList.SetGraphicsResourceSet(1, infoSet);
- info.PlotCount = (uint)PlotInfos.Length;
- for (uint i=0;i<PlotInfos.Length;i++)
- {
- if (!PlotInfos[i].Visibily) continue;
- info.Color = PlotInfos[i].Color;
- info.Interval = Range.XLenght / TotalCount;
- info.PlotDataLenght = TotalCount;
- info.PlotIndex = i;
- CommandList.UpdateBuffer(plotInfoBuffer, 0, info);
- CommandList.Draw(TotalCount);
- }
- }
- DeviceBuffer resultBuffer;
- DeviceBuffer cpuBuffer;
-
- public PlotInfo[] PlotInfos { get; set; } = new PlotInfo[0];
- public uint TotalCount { get; set; }
- [StructLayout(LayoutKind.Sequential,Pack =1)]
- struct Info
- {
- public RgbaFloat Color;
- public uint PlotCount;
- public float Interval;
- public uint PlotIndex;
- public uint PlotDataLenght;
- }
- }
- public class PlotInfo
- {
- public RgbaFloat Color;
- public bool Visibily;
- public string Name = string.Empty;
- }
- }
|