Esempio n. 1
0
        private unsafe void SetupComputeResources()
        {
            ResourceFactory factory = _gd.ResourceFactory;

            if (_gd.Features.ComputeShader is false)
            {
                Logging.RecordError("Error: Compute shaders are unavailable"); return;
            }

            _velocityParamsBuffer = VeldridGraphBuffers.TrackedVRAMAlloc(_gd, (uint)Unsafe.SizeOf <VelocityShaderParams>(), BufferUsage.UniformBuffer, name: "VelocityShaderParams");

            byte[]? velocityShaderBytes = ImGuiNET.ImGuiController.LoadEmbeddedShaderCode(factory, "sim-nodeVelocity", ShaderStages.Compute);
            _velocityShader             = factory.CreateShader(new ShaderDescription(ShaderStages.Compute, velocityShaderBytes, "main"));

            _velocityShaderRsrcLayout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                                         new ResourceLayoutElementDescription("Params", ResourceKind.UniformBuffer, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("positions", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("velocities", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("edgeIndices", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("edgeData", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("edgeStrengths", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("resultData", ResourceKind.StructuredBufferReadWrite, ShaderStages.Compute)));

            ComputePipelineDescription VelocityCPD = new ComputePipelineDescription(_velocityShader, _velocityShaderRsrcLayout, 16, 16, 1); //todo: i dont understand this. experiment with group sizes.

            _velocityComputePipeline = factory.CreateComputePipeline(VelocityCPD);


            _positionShaderRsrcLayout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                                         new ResourceLayoutElementDescription("Params", ResourceKind.UniformBuffer, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("positions", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("velocities", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("resultData", ResourceKind.StructuredBufferReadWrite, ShaderStages.Compute)
                                                                         ));


            byte[]? positionShaderBytes;

            /*
             * This is how we would compile the compute shaders instead of loading pre-compiled
             * 177ms~ ish for this simple shader - multiplied by the different pipelines and stages this would add a lot to startup time
             *   so for now going to keep embedding them in resources. This at least illustrates how to do startup-time compilation.
             */
            /*
             * if (velocityShaderBytes is null)
             * {
             *  _timer.Restart();
             *  positionShaderBytes = Veldrid.SPIRV.SpirvCompilation.CompileGlslToSpirv(positionShaderSource, null, ShaderStages.Compute, Veldrid.SPIRV.GlslCompileOptions.Default).SpirvBytes;
             *  _timer.Stop();
             *  Logging.RecordLogEvent($"Compilation for ForceDirectedNode Position shader took {_timer.Elapsed.TotalMilliseconds:F1}ms");
             * }
             */
            positionShaderBytes = ImGuiNET.ImGuiController.LoadEmbeddedShaderCode(factory, "sim-nodePosition", ShaderStages.Compute);

            _positionShader = factory.CreateShader(new ShaderDescription(ShaderStages.Compute, positionShaderBytes, "main"));
            ComputePipelineDescription PositionCPD = new ComputePipelineDescription(_positionShader, _positionShaderRsrcLayout, 16, 16, 1);

            _positionComputePipeline = factory.CreateComputePipeline(PositionCPD);
            _positionParamsBuffer    = VeldridGraphBuffers.TrackedVRAMAlloc(_gd, (uint)Unsafe.SizeOf <PositionShaderParams>(), BufferUsage.UniformBuffer, name: "PositionShaderParams");
        }
Esempio n. 2
0
        /// Creates an array of metadata for basic blocks used for basic-block-centric graph layout
        public static unsafe void CreateBlockMetadataBuffer(PlottedGraph plot, GraphicsDevice gdevice)
        {
            GraphLayoutState layout = plot.LayoutState;

            if (GlobalConfig.Settings.Logs.BulkLogging)
            {
                Logging.RecordLogEvent($"CreateBlockDataBuffer  {plot.TID}", Logging.LogFilterType.BulkDebugLogFile);
            }

            GraphLayoutState.GPUBuffers VRAMBuffers = layout._VRAMBuffers;
            VeldridGraphBuffers.VRAMDispose(VRAMBuffers.BlockMetadata);
            VeldridGraphBuffers.VRAMDispose(VRAMBuffers.BlockMiddles);

            var textureSize = plot.EdgeTextureWidth();

            if (textureSize > 0)
            {
                CreateBlockMetadataBuf(plot, out NODE_BLOCK_METADATA_COMPUTEBUFFER[] blockdats, out int[] blockMiddles);

                VRAMBuffers.BlockMetadata = VeldridGraphBuffers.TrackedVRAMAlloc(gdevice,
                                                                                 (uint)blockdats.Length * NODE_BLOCK_METADATA_COMPUTEBUFFER.SizeInBytes,
                                                                                 BufferUsage.StructuredBufferReadOnly, sizeof(int), $"BlockMetadata_T{plot.TID}");

                VRAMBuffers.BlockMiddles = VeldridGraphBuffers.TrackedVRAMAlloc(gdevice,
                                                                                (uint)blockMiddles.Length * sizeof(int), BufferUsage.StructuredBufferReadOnly, sizeof(int), $"BlockMiddles_T{plot.TID}");

                VRAMBuffers.BlockCount = blockMiddles.Length;

                if (blockdats.Length == 0)
                {
                    return;
                }

                fixed(NODE_BLOCK_METADATA_COMPUTEBUFFER *datsPtr = blockdats)
                {
                    fixed(int *middlesPtr = blockMiddles)
                    {
                        CommandList cl = gdevice.ResourceFactory.CreateCommandList();

                        cl.Begin();
                        cl.UpdateBuffer(VRAMBuffers.BlockMetadata, 0, (IntPtr)datsPtr, (uint)blockdats.Length * NODE_BLOCK_METADATA_COMPUTEBUFFER.SizeInBytes);
                        cl.UpdateBuffer(VRAMBuffers.BlockMiddles, 0, (IntPtr)middlesPtr, (uint)blockMiddles.Length * sizeof(int));
                        cl.End();
                        gdevice.SubmitCommands(cl);
                        gdevice.WaitForIdle();
                        cl.Dispose();
                    }
                }
            }

            //Debug.Assert(!VeldridGraphBuffers.DetectNaN(_gd, newBuffer));

            if (GlobalConfig.Settings.Logs.BulkLogging)
            {
                Logging.RecordLogEvent($"CreateBlockDataBuffer  {plot.TID} complete", Logging.LogFilterType.BulkDebugLogFile);
            }
            //PrintBufferArray(textureArray, "Created data texture:");
        }
Esempio n. 3
0
        private unsafe void SetupComputeResources()
        {
            Debug.Assert(_gd is not null, "Init not called");
            ResourceFactory factory = _gd.ResourceFactory;

            if (_gd.Features.ComputeShader is false)
            {
                Logging.RecordError("Error: Compute shaders are unavailable"); return;
            }

            _velocityParamsBuffer = VeldridGraphBuffers.TrackedVRAMAlloc(_gd, (uint)Unsafe.SizeOf <VelocityShaderParams>(), BufferUsage.UniformBuffer, name: "VelocityShaderParams");


            byte[]? velocityBlockShaderBytes = ImGuiNET.ImGuiController.LoadEmbeddedShaderCode(factory, "sim-blockVelocity", ShaderStages.Fragment);
            _velocityShader = factory.CreateShader(new ShaderDescription(ShaderStages.Compute, velocityBlockShaderBytes, "FS"));

            _velocityShaderRsrcLayout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                                         new ResourceLayoutElementDescription("Params", ResourceKind.UniformBuffer, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("positions", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("velocities", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("edgeIndices", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("edgeData", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("edgeStrengths", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("blockData", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("blockMiddles", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("resultData", ResourceKind.StructuredBufferReadWrite, ShaderStages.Compute)));

            ComputePipelineDescription VelocityBlockCPD = new ComputePipelineDescription(_velocityShader, _velocityShaderRsrcLayout, 16, 16, 1);

            _velocityComputePipeline = factory.CreateComputePipeline(VelocityBlockCPD);

            _positionShaderRsrcLayout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                                         new ResourceLayoutElementDescription("Params", ResourceKind.UniformBuffer, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("positions", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("velocities", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("blockData", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("resultData", ResourceKind.StructuredBufferReadWrite, ShaderStages.Compute)
                                                                         ));


            byte[]? positionShaderBytes = ImGuiNET.ImGuiController.LoadEmbeddedShaderCode(factory, "sim-blockPosition", ShaderStages.Vertex);
            _positionShader             = factory.CreateShader(new ShaderDescription(ShaderStages.Fragment, positionShaderBytes, "FS")); //todo ... not fragment

            ComputePipelineDescription PositionCPD = new ComputePipelineDescription(_positionShader, _positionShaderRsrcLayout, 16, 16, 1);

            _positionComputePipeline = factory.CreateComputePipeline(PositionCPD);
            _positionParamsBuffer    = VeldridGraphBuffers.TrackedVRAMAlloc(_gd, (uint)Unsafe.SizeOf <PositionShaderParams>(), BufferUsage.UniformBuffer, name: "PositionShaderParams");
        }
Esempio n. 4
0
        /*
         *
         * Picking node points for mousing over instruction verts
         *
         */


        public static ShaderSetDescription CreateNodePickingShaders(GraphicsDevice gd, out DeviceBuffer vertBuffer)
        {
            VertexElementDescription VEDpos       = new VertexElementDescription("PositionBufIndex", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Int1);
            VertexElementDescription VEDcol       = new VertexElementDescription("Color", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float4);
            VertexLayoutDescription  vertexLayout = new VertexLayoutDescription(VEDpos, VEDcol);

            byte[]            vertShaderBytes    = Encoding.UTF8.GetBytes(Shaders.SPIR_V.SPIRVShaders.vspickingglsl);
            byte[]            fragShaderBytes    = Encoding.UTF8.GetBytes(Shaders.SPIR_V.SPIRVShaders.fspickingglsl);
            ShaderDescription vertexShaderDesc   = new ShaderDescription(ShaderStages.Vertex, vertShaderBytes, "main");
            ShaderDescription fragmentShaderDesc = new ShaderDescription(ShaderStages.Fragment, fragShaderBytes, "main");

            ShaderSetDescription shaderSetDesc = new ShaderSetDescription(
                vertexLayouts: new VertexLayoutDescription[] { vertexLayout },
                shaders: gd.ResourceFactory.CreateFromSpirv(vertexShaderDesc, fragmentShaderDesc));

            vertBuffer = VeldridGraphBuffers.TrackedVRAMAlloc(gd, 1, BufferUsage.VertexBuffer, name: "NodePickingShaderVertexBufInitial");
            return(shaderSetDesc);
        }
Esempio n. 5
0
        private unsafe void SetupComputeResources()
        {
            ResourceFactory factory = _gd.ResourceFactory;

            if (_gd.Features.ComputeShader is false)
            {
                Logging.RecordError("Error: Compute shaders are unavailable"); return;
            }

            _velocityParamsBuffer = VeldridGraphBuffers.TrackedVRAMAlloc(_gd, (uint)Unsafe.SizeOf <VelocityShaderParams>(), BufferUsage.UniformBuffer, name: "VelocityShaderParams");

            byte[]? velocityShaderBytes = ImGuiNET.ImGuiController.LoadEmbeddedShaderCode(factory, "sim-presetVelocity", ShaderStages.Compute);
            _velocityShader             = factory.CreateShader(new ShaderDescription(ShaderStages.Compute, velocityShaderBytes, "main"));

            _velocityShaderRsrcLayout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                                         new ResourceLayoutElementDescription("Params", ResourceKind.UniformBuffer, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("positions", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("presetPositions", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("velocities", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("resultData", ResourceKind.StructuredBufferReadWrite, ShaderStages.Compute)));

            ComputePipelineDescription VelocityCPD = new ComputePipelineDescription(_velocityShader, _velocityShaderRsrcLayout, 16, 16, 1); //todo: i dont understand this. experiment with group sizes.

            _velocityComputePipeline = factory.CreateComputePipeline(VelocityCPD);


            _positionShaderRsrcLayout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                                         new ResourceLayoutElementDescription("Params", ResourceKind.UniformBuffer, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("positions", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("velocities", ResourceKind.StructuredBufferReadOnly, ShaderStages.Compute),
                                                                         new ResourceLayoutElementDescription("resultData", ResourceKind.StructuredBufferReadWrite, ShaderStages.Compute)
                                                                         ));

            byte[]? positionShaderBytes;

            positionShaderBytes = ImGuiNET.ImGuiController.LoadEmbeddedShaderCode(factory, "sim-nodePosition", ShaderStages.Compute);

            _positionShader = factory.CreateShader(new ShaderDescription(ShaderStages.Compute, positionShaderBytes, "main"));
            ComputePipelineDescription PositionCPD = new ComputePipelineDescription(_positionShader, _positionShaderRsrcLayout, 16, 16, 1);

            _positionComputePipeline = factory.CreateComputePipeline(PositionCPD);
            _positionParamsBuffer    = VeldridGraphBuffers.TrackedVRAMAlloc(_gd, (uint)Unsafe.SizeOf <PositionShaderParams>(), BufferUsage.UniformBuffer, name: "PositionShaderParams");
        }
Esempio n. 6
0
        /*
         *
         * Font triangles
         *
         */
        public static ShaderSetDescription CreateFontShaders(GraphicsDevice gd, out DeviceBuffer vertBuffer)
        {
            VertexElementDescription nodeIdx = new VertexElementDescription("nodeIdx", VertexElementSemantic.TextureCoordinate, VertexElementFormat.UInt1);
            VertexElementDescription VEDpos  = new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3);
            VertexElementDescription Charpos = new VertexElementDescription("CharCoord", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2);
            VertexElementDescription yoff    = new VertexElementDescription("YOffset", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float1);
            VertexElementDescription fcol    = new VertexElementDescription("FontColour", VertexElementSemantic.Color, VertexElementFormat.Float4);

            VertexLayoutDescription vertexLayout = new VertexLayoutDescription(nodeIdx, VEDpos, Charpos, yoff, fcol);

            byte[]            vertShaderBytes    = Encoding.UTF8.GetBytes(Shaders.SPIR_V.SPIRVShaders.vsfontglsl);
            byte[]            fragShaderBytes    = Encoding.UTF8.GetBytes(Shaders.SPIR_V.SPIRVShaders.fsfontglsl);
            ShaderDescription vertexShaderDesc   = new ShaderDescription(ShaderStages.Vertex, vertShaderBytes, "main");
            ShaderDescription fragmentShaderDesc = new ShaderDescription(ShaderStages.Fragment, fragShaderBytes, "main");

            ShaderSetDescription shaderSetDesc = new ShaderSetDescription(
                vertexLayouts: new VertexLayoutDescription[] { vertexLayout },
                shaders: gd.ResourceFactory.CreateFromSpirv(vertexShaderDesc, fragmentShaderDesc));

            vertBuffer = VeldridGraphBuffers.TrackedVRAMAlloc(gd, 1, BufferUsage.VertexBuffer, name: "FontShaderVertexBufInitial");
            return(shaderSetDesc);
        }