|
@@ -5,6 +5,7 @@ using System.Linq;
|
|
|
using System.Net.Http.Headers;
|
|
|
using System.Numerics;
|
|
|
using System.Runtime.CompilerServices;
|
|
|
+using System.Runtime.InteropServices;
|
|
|
using System.Security.Cryptography;
|
|
|
using System.Text;
|
|
|
|
|
@@ -18,6 +19,7 @@ namespace Veldrid.Common.Plot
|
|
|
public float HOffset { get; set; }
|
|
|
public float VOffset { get; set; }
|
|
|
public bool ZoomEnbled { get; set; }
|
|
|
+ private const uint ThreadGroupSizeX = 1000;
|
|
|
private ProjView projView;
|
|
|
Pipeline pipeline;
|
|
|
private PlotInfo plotInfo;
|
|
@@ -31,7 +33,15 @@ namespace Veldrid.Common.Plot
|
|
|
Shader[] shaders;
|
|
|
|
|
|
|
|
|
-
|
|
|
+ DeviceBuffer resultBuffer;
|
|
|
+ DeviceBuffer cpuBuffer;
|
|
|
+ ResourceLayout computeLayout;
|
|
|
+ ResourceSet computeSet;
|
|
|
+ Pipeline computePipline;
|
|
|
+ ResourceLayout computeLineInfoLayout;
|
|
|
+ ResourceSet computeLineInfoSet;
|
|
|
+ Shader computeShader;
|
|
|
+ CommandList computeCommandList;
|
|
|
private object locker = new object();
|
|
|
public LineSeries(VeldridContent content, uint datalen=1000,uint plotcount=4):base(content)
|
|
|
{
|
|
@@ -39,7 +49,6 @@ namespace Veldrid.Common.Plot
|
|
|
PlotInfos = new SeriesInfo[plotcount];
|
|
|
Margin = new Padding(40, 10, 10, 20);
|
|
|
}
|
|
|
- List<float>[] temp = new List<float>[0];
|
|
|
internal override void DisposeResources()
|
|
|
{
|
|
|
base.DisposeResources();
|
|
@@ -52,6 +61,16 @@ namespace Veldrid.Common.Plot
|
|
|
infoLayout?.Dispose();
|
|
|
infoSet?.Dispose();
|
|
|
|
|
|
+
|
|
|
+ resultBuffer?.Dispose();
|
|
|
+ cpuBuffer?.Dispose();
|
|
|
+ computeLayout?.Dispose();
|
|
|
+ computeSet?.Dispose();
|
|
|
+ computePipline?.Dispose();
|
|
|
+ computeCommandList?.Dispose();
|
|
|
+ computeLineInfoLayout?.Dispose();
|
|
|
+ computeLineInfoSet?.Dispose();
|
|
|
+ computeShader?.Dispose();
|
|
|
}
|
|
|
private void CreateDataBuffer()
|
|
|
{
|
|
@@ -66,12 +85,13 @@ namespace Veldrid.Common.Plot
|
|
|
Usage = BufferUsage.StructuredBufferReadWrite,
|
|
|
StructureByteStride = 4,
|
|
|
});
|
|
|
- temp = Enumerable.Range(0, PlotInfos.Length).Select(x=> new List<float>()).ToArray();
|
|
|
dataSet = ResourceFactory.CreateResourceSet(new ResourceSetDescription(dataLayout, dataBuffer));
|
|
|
pipeline = CreatePipLine(PrimitiveTopology.LineStrip, new ResourceLayout[] { dataLayout, infoLayout }, new VertexLayoutDescription[0],new[] { shaders[0], shaders[1] });
|
|
|
datastartindex = 0;
|
|
|
datatotallen = 0;
|
|
|
TotalCount = 0;
|
|
|
+ MaxAndMin = new Vector2[PlotInfos.Length];
|
|
|
+ CreateComputeBuffer();
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -113,12 +133,34 @@ namespace Veldrid.Common.Plot
|
|
|
}));
|
|
|
infoSet = ResourceFactory.CreateResourceSet(new ResourceSetDescription(infoLayout, plotInfoBuffer, projViewBuffer));
|
|
|
|
|
|
+ resultBuffer = ResourceFactory.CreateBuffer(new BufferDescription(20 * 4, BufferUsage.StructuredBufferReadWrite, 4));
|
|
|
+ cpuBuffer = ResourceFactory.CreateBuffer(new BufferDescription(resultBuffer.SizeInBytes, BufferUsage.Staging));
|
|
|
+ computeLayout = ResourceFactory.CreateResourceLayout(new ResourceLayoutDescription(
|
|
|
+ new ResourceLayoutElementDescription("DataBuffer", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
|
|
|
+ new ResourceLayoutElementDescription("ResultDataBuffer", ResourceKind.StructuredBufferReadWrite, ShaderStages.Compute)));
|
|
|
+ computeLineInfoLayout = ResourceFactory.CreateResourceLayout(new ResourceLayoutDescription(
|
|
|
+ new ResourceLayoutElementDescription("InfoBuffer", ResourceKind.UniformBuffer, ShaderStages.Compute)));
|
|
|
+ computeLineInfoSet = ResourceFactory.CreateResourceSet(new ResourceSetDescription(computeLineInfoLayout, plotInfoBuffer));
|
|
|
+ ShaderDescription shaderDescription = new ShaderDescription(ShaderStages.Compute, GLSLManger.LoadShader("LineSeries.hlsl"), "main")
|
|
|
+ {
|
|
|
+ Debug = true,
|
|
|
+ };
|
|
|
+ computeShader = ResourceFactory.CreateShader(shaderDescription);
|
|
|
+ computeCommandList = ResourceFactory.CreateCommandList() ;
|
|
|
CreateDataBuffer();
|
|
|
}
|
|
|
+
|
|
|
+ private void CreateComputeBuffer()
|
|
|
+ {
|
|
|
+ computeSet?.Dispose();
|
|
|
+ computePipline?.Dispose();
|
|
|
+ computeSet = ResourceFactory.CreateResourceSet(new ResourceSetDescription(computeLayout, dataBuffer, resultBuffer));
|
|
|
+ computePipline = ResourceFactory.CreateComputePipeline(new ComputePipelineDescription(computeShader, new[] { computeLayout, computeLineInfoLayout }, ThreadGroupSizeX, 1, 1));
|
|
|
+ }
|
|
|
struct PlotInfo
|
|
|
{
|
|
|
public RgbaFloat Color;
|
|
|
- public float Start;
|
|
|
+ public uint PlotIndex;
|
|
|
public float Interval;
|
|
|
public uint DataStartIndex;
|
|
|
public uint PlotDataLenght;
|
|
@@ -136,7 +178,7 @@ namespace Veldrid.Common.Plot
|
|
|
}
|
|
|
public ulong TotalCount { get; private set; }
|
|
|
public Vector2[] MaxAndMin { get; private set; } = new Vector2[0];
|
|
|
- public void AddData(float[] data)
|
|
|
+ public unsafe void AddData(float[] data)
|
|
|
{
|
|
|
if (DataLenght == 0 || PlotInfos == null || PlotInfos.Length == 0) return;
|
|
|
if (data == null|| data.Length!=PlotInfos.Length) return;
|
|
@@ -144,26 +186,50 @@ namespace Veldrid.Common.Plot
|
|
|
for(int i=0;i<data.Length;i++)
|
|
|
{
|
|
|
GraphicsDevice.UpdateBuffer(dataBuffer,(uint)((datastartindex+i*DataLenght)*Unsafe.SizeOf<float>()),ref data[i],(uint)Unsafe.SizeOf<float>());
|
|
|
- temp[i].Add(data[i]);
|
|
|
- if (temp[i].Count > DataLenght) temp[i].RemoveAt(0);
|
|
|
+
|
|
|
}
|
|
|
datastartindex++;
|
|
|
datatotallen++;
|
|
|
if(datatotallen>DataLenght)datatotallen = DataLenght;
|
|
|
if (datastartindex >= DataLenght) datastartindex = 0;
|
|
|
MaxAndMin = new Vector2[PlotInfos.Length];
|
|
|
- for (int i = 0; i < MaxAndMin.Length; i++)
|
|
|
+ //return;
|
|
|
+ if (datatotallen > 0)
|
|
|
+ {
|
|
|
+ computeCommandList.Begin();
|
|
|
+ CalcMaxMin(computeCommandList);
|
|
|
+ computeCommandList.End();
|
|
|
+ GraphicsDevice.SubmitCommands(computeCommandList);
|
|
|
+ GraphicsDevice.WaitForIdle();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ private unsafe void CalcMaxMin(CommandList command)
|
|
|
+ {
|
|
|
+ MaxAndMin = new Vector2[PlotInfos.Length];
|
|
|
+ plotInfo.DataStartIndex = datatotallen;
|
|
|
+ plotInfo.PlotDataLenght = DataLenght;
|
|
|
+ command.SetPipeline(computePipline);
|
|
|
+ command.SetComputeResourceSet(0, computeSet);
|
|
|
+ command.SetComputeResourceSet(1, computeLineInfoSet);
|
|
|
+ for (uint i = 0; i < PlotInfos.Length; i++)
|
|
|
{
|
|
|
- MaxAndMin[i] = new Vector2(temp[i].Max(), temp[i].Min());
|
|
|
+ plotInfo.PlotIndex = i;
|
|
|
+ command.UpdateBuffer(plotInfoBuffer, 0, plotInfo);
|
|
|
+ command.Dispatch((uint)MathF.Ceiling(((float)datatotallen) / ThreadGroupSizeX), 1, 1);
|
|
|
}
|
|
|
+ command.CopyBuffer(resultBuffer, 0, cpuBuffer, 0, (uint)(MaxAndMin.Length * Unsafe.SizeOf<Vector2>()));
|
|
|
+ var mapped = GraphicsDevice.Map(cpuBuffer, MapMode.ReadWrite);
|
|
|
+ Unsafe.CopyBlock(ref Unsafe.As<Vector2, byte>(ref MaxAndMin[0]), ref Unsafe.AsRef<byte>(mapped.Data.ToPointer()), (uint)(MaxAndMin.Length * Unsafe.SizeOf<Vector2>()));
|
|
|
+ GraphicsDevice.Unmap(cpuBuffer);
|
|
|
}
|
|
|
- internal override void DrawData()
|
|
|
+ internal unsafe override void DrawData()
|
|
|
{
|
|
|
if (DataLenght == 0 || PlotInfos == null || PlotInfos.Length == 0) return;
|
|
|
lock (locker)
|
|
|
{
|
|
|
base.DrawData();
|
|
|
if (datatotallen == 0) return;
|
|
|
+ //CalcMaxMin(CommandList);
|
|
|
projView.Proj = OrthographicMatrix;
|
|
|
projView.View = GetLineMatrix(HScale,VScale,0,VOffset);
|
|
|
CommandList.SetFramebuffer(MainSwapchainBuffer);
|
|
@@ -178,8 +244,8 @@ namespace Veldrid.Common.Plot
|
|
|
|
|
|
plotInfo.Color = PlotInfos[i].Color;
|
|
|
plotInfo.PlotDataLenght = DataLenght;
|
|
|
- plotInfo.DataStartIndex = datatotallen == DataLenght ? i * DataLenght + datastartindex : i * DataLenght;
|
|
|
- plotInfo.Start = 0;
|
|
|
+ plotInfo.DataStartIndex = datatotallen == DataLenght ? datastartindex : 0;
|
|
|
+ plotInfo.PlotIndex = i;
|
|
|
CommandList.UpdateBuffer(plotInfoBuffer, 0, plotInfo);
|
|
|
CommandList.Draw(datatotallen);
|
|
|
}
|