Пример #1
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
                }
            });
        }