private VulkanBuffer CreateStorageBuffer()
        {
            var random = new Random();

            int numParticles = Host.Platform == Platform.Android
                ? 256 * 1024
                : 256 * 2048; // ~500k particles.

            var particles = new VertexParticle[numParticles];

            for (int i = 0; i < numParticles; i++)
            {
                particles[i] = new VertexParticle
                {
                    Position = new Vector2(
                        ((float)random.NextDouble() - 0.5f) * 2.0f,
                        ((float)random.NextDouble() - 0.5f) * 2.0f),
                    Color = new Vector4(
                        0.5f + (float)random.NextDouble() / 2.0f,
                        (float)random.NextDouble() / 2.0f,
                        (float)random.NextDouble() / 2.0f,
                        1.0f)
                };
            }

            return(VulkanBuffer.Storage(Context, particles));
        }
Beispiel #2
0
        protected override void InitializePermanent()
        {
            var cube = GeometricPrimitive.Box(1.0f, 1.0f, 1.0f);

            _cubeTexture         = Content.Load <VulkanImage>("IndustryForgedDark512.ktx");
            _cubeVertices        = ToDispose(VulkanBuffer.Vertex(Context, cube.Vertices));
            _cubeIndices         = ToDispose(VulkanBuffer.Index(Context, cube.Indices));
            _sampler             = ToDispose(CreateSampler());
            _uniformBuffer       = ToDispose(VulkanBuffer.DynamicUniform <WorldViewProjection>(Context, 1));
            _descriptorSetLayout = ToDispose(CreateDescriptorSetLayout());
            _pipelineLayout      = ToDispose(CreatePipelineLayout());
            _descriptorPool      = ToDispose(CreateDescriptorPool());
            _descriptorSet       = CreateDescriptorSet(); // Will be freed when pool is destroyed.
        }
        protected override void InitializePermanent()
        {
            _descriptorPool = ToDispose(CreateDescriptorPool());

            _sampler                     = ToDispose(CreateSampler());
            _particleDiffuseMap          = Content.Load <VulkanImage>("ParticleDiffuse.ktx");
            _graphicsDescriptorSetLayout = ToDispose(CreateGraphicsDescriptorSetLayout());
            _graphicsPipelineLayout      = ToDispose(CreateGraphicsPipelineLayout());
            _graphicsDescriptorSet       = CreateGraphicsDescriptorSet();

            _storageBuffer = ToDispose(CreateStorageBuffer());
            _uniformBuffer = ToDispose(VulkanBuffer.DynamicUniform <UniformBufferObject>(Context, 1));
            _computeDescriptorSetLayout = ToDispose(CreateComputeDescriptorSetLayout());
            _computePipelineLayout      = ToDispose(CreateComputePipelineLayout());
            _computeDescriptorSet       = CreateComputeDescriptorSet();
            _computeCmdBuffer           = Context.ComputeCommandPool.AllocateBuffers(new CommandBufferAllocateInfo(CommandBufferLevel.Primary, 1))[0];
            _computeFence = ToDispose(Context.Device.CreateFence());
        }
        protected override Expression VisitConstant(ConstantExpression node)
        {
            object value = node.Value;

            switch (value)
            {
            case IQueryable query:
            {
                if (!_existingBuffersMap.TryGetValue(query, out GlslVariable variable))
                {
                    VulkanBuffer computeBuffer = QueryToComputeBuffer(value);
                    _ctx.Inputs.Add(computeBuffer);
                    variable = _shader.AddBuffer(computeBuffer.ElementType, true, _isBufferStartOfChain);
                    _existingBuffersMap.Add(query, variable);
                }
                _bufferVariables.Push(variable);
                break;
            }

            case Array array:
            {
                if (!_existingBuffersMap.TryGetValue(array, out GlslVariable variable))
                {
                    var computeBuffer = new VulkanBuffer(_device, array, TypeSystem.GetElementType(node.Type), ResourceDirection.CpuToGpu);
                    _ctx.Inputs.Add(computeBuffer);
                    variable = _shader.AddBuffer(computeBuffer.ElementType, true, _isBufferStartOfChain);
                    _existingBuffersMap.Add(array, variable);
                }
                _bufferVariables.Push(variable);
                break;
            }

            default:
            {
                if (value == null)
                {
                    throw new NotImplementedException();
                }
                _shader.Main.Append(value.GlslLiteral());
                break;
            }
            }
            return(node);
        }
        protected override void InitializePermanent()
        {
            var cube = GeometricPrimitive.Box(1.0f, 1.0f, 1.0f);

            _cubeTexture  = Content.LoadVulkanImage("IndustryForgedDark512.ktx");
            _cubeVertices = ToDispose(VulkanBuffer.Vertex(Context, cube.Vertices));
            _cubeIndices  = ToDispose(VulkanBuffer.Index(Context, cube.Indices));
            var sampler = CreateSampler();

            _sampler = sampler;
            ToDispose(new ActionDisposable(() =>
            {
                vkDestroySampler(Context.Device, sampler, null);
            }));
            _uniformBuffer = ToDispose(VulkanBuffer.DynamicUniform <WorldViewProjection>(Context, 1));
            var descriptorSetLayout = CreateDescriptorSetLayout();

            _descriptorSetLayout = descriptorSetLayout;
            ToDispose(new ActionDisposable(() =>
            {
                vkDestroyDescriptorSetLayout(Context.Device, descriptorSetLayout, null);
            }));
            var pipelineLayout = CreatePipelineLayout();

            _pipelineLayout = pipelineLayout;
            ToDispose(new ActionDisposable(() =>
            {
                vkDestroyPipelineLayout(Context.Device, pipelineLayout, null);
            }));
            var descriptorPool = CreateDescriptorPool();

            _descriptorPool = descriptorPool;
            ToDispose(new ActionDisposable(() =>
            {
                vkDestroyDescriptorPool(Context.Device, descriptorPool, null);
            }));
            _descriptorSet = CreateDescriptorSet(_descriptorPool); // Will be freed when pool is destroyed.
        }
Beispiel #6
0
        public override void Initialise(Device device, BufferManager bufferManager)
        {
            var vertShaderData = LoadShaderData(".\\Shaders\\BasicShader.vert.spv", out int vertCodeSize);

            this.vertexShader = device.CreateShaderModule(new ShaderModuleCreateInfo
            {
                Code     = vertShaderData,
                CodeSize = vertCodeSize
            });

            var fragShaderData = LoadShaderData(".\\Shaders\\BasicShader.frag.spv", out int fragCodeSize);

            this.fragmentShader = device.CreateShaderModule(new ShaderModuleCreateInfo
            {
                Code     = fragShaderData,
                CodeSize = fragCodeSize
            });

            SphereData.Get(6, out var vertices, out var indices);

            var vertexData = vertices.Select(x => new Vertex(x.Item1, x.Item2, x.Item3, x.Item4)).ToArray();

            indexCount = indices.Count();

            this.vertexBuffer = bufferManager.CreateBuffer(MemUtil.SizeOf <Vertex>() * (uint)vertexData.Length, BufferUsageFlags.TransferDestination | BufferUsageFlags.VertexBuffer, MemoryPropertyFlags.DeviceLocal);

            this.vertexBuffer.Update(vertexData);

            this.indexBuffer = bufferManager.CreateBuffer(MemUtil.SizeOf <uint>() * (uint)indices.Length, BufferUsageFlags.TransferDestination | BufferUsageFlags.IndexBuffer, MemoryPropertyFlags.DeviceLocal);

            this.indexBuffer.Update(indices);

            this.uniformBuffer = bufferManager.CreateBuffer(MemUtil.SizeOf <UniformBufferObject>(), BufferUsageFlags.TransferDestination | BufferUsageFlags.UniformBuffer, MemoryPropertyFlags.DeviceLocal);

            this.descriptorPool = device.CreateDescriptorPool(new DescriptorPoolCreateInfo
            {
                PoolSizes = new[]
                {
                    new DescriptorPoolSize
                    {
                        DescriptorCount = 1,
                        Type            = DescriptorType.UniformBuffer
                    }
                },
                MaxSets = 1
            });

            this.descriptorSetLayout = device.CreateDescriptorSetLayout(new DescriptorSetLayoutCreateInfo
            {
                Bindings = new[]
                {
                    new DescriptorSetLayoutBinding
                    {
                        Binding         = 0,
                        DescriptorType  = DescriptorType.UniformBuffer,
                        StageFlags      = ShaderStageFlags.AllGraphics,
                        DescriptorCount = 1
                    }
                }
            });

            this.descriptorSet = device.AllocateDescriptorSets(new DescriptorSetAllocateInfo
            {
                DescriptorPool = descriptorPool,
                SetLayouts     = new[]
                {
                    this.descriptorSetLayout
                }
            }).Single();

            device.UpdateDescriptorSets(
                new WriteDescriptorSet
            {
                BufferInfo = new[]
                {
                    new DescriptorBufferInfo
                    {
                        Buffer = this.uniformBuffer.Buffer,
                        Offset = 0,
                        Range  = MemUtil.SizeOf <UniformBufferObject>()
                    }
                },
                DestinationSet          = descriptorSet,
                DestinationBinding      = 0,
                DestinationArrayElement = 0,
                DescriptorType          = DescriptorType.UniformBuffer
            }, null);

            this.pipelineLayout = device.CreatePipelineLayout(new PipelineLayoutCreateInfo()
            {
                SetLayouts = new[]
                {
                    this.descriptorSetLayout
                }
            });
        }