Ejemplo n.º 1
0
        private DescriptorSet CreateGraphicsDescriptorSet()
        {
            DescriptorSet descriptorSet = _descriptorPool.AllocateSets(new DescriptorSetAllocateInfo(1, _graphicsDescriptorSetLayout))[0];

            _descriptorPool.UpdateSets(new[]
            {
                // Particle diffuse map.
                new WriteDescriptorSet(descriptorSet, 0, 0, 1, DescriptorType.CombinedImageSampler,
                                       new[] { new DescriptorImageInfo(_sampler, _particleDiffuseMap.View, ImageLayout.ColorAttachmentOptimal) })
            });
            return(descriptorSet);
        }
Ejemplo n.º 2
0
        public void UpdateSetsDescriptorWrite()
        {
            const int bufferSize = 256;

            var layoutCreateInfo = new DescriptorSetLayoutCreateInfo(
                new DescriptorSetLayoutBinding(0, DescriptorType.StorageBuffer, 1));
            var poolCreateInfo = new DescriptorPoolCreateInfo(
                1,
                new[] { new DescriptorPoolSize(DescriptorType.StorageBuffer, 1) },
                DescriptorPoolCreateFlags.FreeDescriptorSet);

            using (Buffer buffer = Device.CreateBuffer(new BufferCreateInfo(bufferSize, BufferUsages.StorageBuffer)))
                using (DeviceMemory memory = Device.AllocateMemory(new MemoryAllocateInfo(bufferSize, 0)))
                    using (DescriptorSetLayout layout = Device.CreateDescriptorSetLayout(layoutCreateInfo))
                        using (DescriptorPool pool = Device.CreateDescriptorPool(poolCreateInfo))
                            using (DescriptorSet set = pool.AllocateSets(new DescriptorSetAllocateInfo(1, layout))[0])
                            {
                                // Required to satisfy the validation layer.
                                buffer.GetMemoryRequirements();

                                buffer.BindMemory(memory);

                                var descriptorWrite = new WriteDescriptorSet(set, 0, 0, 1, DescriptorType.StorageBuffer,
                                                                             bufferInfo: new[] { new DescriptorBufferInfo(buffer) });
                                pool.UpdateSets(new[] { descriptorWrite });
                            }
        }
Ejemplo n.º 3
0
        private void CreateDescriptors()
        {
            int bindingCount = Inputs.Count + 1; // + 1 output.

            // Setup bindings.
            var bindings = new DescriptorSetLayoutBinding[bindingCount];

            for (int i = 0; i < Inputs.Count; i++)
            {
                bindings[i] = Inputs[i].GetDescriptorSetLayoutBinding(i);
            }
            bindings[Inputs.Count] = Output.GetDescriptorSetLayoutBinding(Inputs.Count);

            _descriptorSetLayout = Device.Logical.CreateDescriptorSetLayout(new DescriptorSetLayoutCreateInfo(bindings));
            var descriptorPoolCreateInfo = new DescriptorPoolCreateInfo(
                1,
                new[] { new DescriptorPoolSize(DescriptorType.StorageBuffer, bindingCount) });

            _descriptorPool = Device.Logical.CreateDescriptorPool(descriptorPoolCreateInfo);
            _descriptorSet  = _descriptorPool.AllocateSets(new DescriptorSetAllocateInfo(1, _descriptorSetLayout))[0];

            // Setup write descriptors.
            var writeDescriptorSets = new WriteDescriptorSet[bindingCount];

            for (int i = 0; i < Inputs.Count; i++)
            {
                writeDescriptorSets[i] = Inputs[i].GetWriteDescriptorSet(_descriptorSet, i);
            }
            writeDescriptorSets[Inputs.Count] = Output.GetWriteDescriptorSet(_descriptorSet, Inputs.Count);

            _descriptorPool.UpdateSets(writeDescriptorSets);
        }
Ejemplo n.º 4
0
        public static DescriptorSet CreateDescriptorSet(DescriptorPool pool, DescriptorSetLayout setLayout, DescriptorItem[] items, out Sampler[] samplers)
        {
            DescriptorSet descriptorSet = pool.AllocateSets(new DescriptorSetAllocateInfo(1, setLayout))[0];

            var writeDescriptorSets = new WriteDescriptorSet[items.Length];

            for (var i = 0; i < items.Length; i++)
            {
                var item = items[i];
                switch (item.Type)
                {
                case DescriptorItem.DescriptorType.UniformBuffer:
                    writeDescriptorSets[i] = new WriteDescriptorSet(descriptorSet, i, 0, item.Count, DescriptorType.UniformBuffer,
                                                                    bufferInfo: new[] { new DescriptorBufferInfo(item.Buffer, 0, item.Buffer.Size) });
                    break;

                case DescriptorItem.DescriptorType.CombinedImageSampler:
                    _SamplerCollection.Add(item.Sampler);
                    writeDescriptorSets[i] = new WriteDescriptorSet(descriptorSet, i, 0, 1, DescriptorType.CombinedImageSampler,
                                                                    imageInfo: new[] { new DescriptorImageInfo(item.Sampler, item.Texture.View, VulkanCore.ImageLayout.General) });
                    break;

                default:
                    throw new NotImplementedException($"No case for {item.Type}");
                }
            }

            pool.UpdateSets(writeDescriptorSets);

            samplers = _SamplerCollection.ToArray();
            _SamplerCollection.Clear();
            return(descriptorSet);
        }
Ejemplo n.º 5
0
        public void AllocateSetsAndFreeSets()
        {
            var layoutCreateInfo = new DescriptorSetLayoutCreateInfo(
                new DescriptorSetLayoutBinding(0, DescriptorType.StorageBuffer, 1));
            var poolCreateInfo = new DescriptorPoolCreateInfo(
                1,
                new[] { new DescriptorPoolSize(DescriptorType.StorageBuffer, 1) },
                DescriptorPoolCreateFlags.FreeDescriptorSet);

            using (DescriptorSetLayout layout = Device.CreateDescriptorSetLayout(layoutCreateInfo))
                using (DescriptorPool pool = Device.CreateDescriptorPool(poolCreateInfo))
                {
                    using (pool.AllocateSets(new DescriptorSetAllocateInfo(1, layout))[0]) { }
                    DescriptorSet set = pool.AllocateSets(new DescriptorSetAllocateInfo(1, layout))[0];
                    pool.FreeSets(set);
                }
        }
Ejemplo n.º 6
0
            internal Chunk(Device logicalDevice, ReadOnlySpan <IShaderInput> inputs, int size = 5)
            {
                types = new DescriptorType[inputs.Length];
                for (int i = 0; i < types.Length; i++)
                {
                    types[i] = inputs[i].DescriptorType;
                }

                //Gather how many inputs of each type we have
                var poolSizes = new ResizeArray <DescriptorPoolSize>();

                for (int i = 0; i < types.Length; i++)
                {
                    for (int j = 0; j < poolSizes.Count; j++)
                    {
                        if (poolSizes.Data[j].Type == types[i])
                        {
                            poolSizes.Data[j].DescriptorCount += size;
                            continue;
                        }
                    }
                    poolSizes.Add(new DescriptorPoolSize(types[i], size));
                }

                //Create a pool for 'size' types the amount of resources of one set
                pool = logicalDevice.CreateDescriptorPool(new DescriptorPoolCreateInfo(
                                                              maxSets: size,
                                                              poolSizes: poolSizes.ToArray(),
                                                              flags: DescriptorPoolCreateFlags.None));

                //Create a layout that matches the inputs
                layout = CreateLayout(logicalDevice, inputs);

                //Even tho we use the same layout for the entire pool the 'VkDescriptorSetAllocateInfo'
                //expects a layout per allocation so we create a array of layouts all pointing to the
                //same layout
                var layouts = new DescriptorSetLayout[size];

                for (int i = 0; i < layouts.Length; i++)
                {
                    layouts[i] = layout;
                }

                //Pre-allocate all the sets from the pool
                sets = pool.AllocateSets(new DescriptorSetAllocateInfo(
                                             descriptorSetCount: size,
                                             setLayouts: layouts));

                //Mark all the sets as being free
                isFree = new bool[size];
                for (int i = 0; i < isFree.Length; i++)
                {
                    isFree[i] = true;
                }
            }
Ejemplo n.º 7
0
        private DescriptorSet CreateDescriptorSet()
        {
            DescriptorSet descriptorSet = _descriptorPool.AllocateSets(new DescriptorSetAllocateInfo(1, _descriptorSetLayout))[0];
            // Update the descriptor set for the shader binding point.
            var writeDescriptorSets = new[]
            {
                new WriteDescriptorSet(descriptorSet, 0, 0, 1, DescriptorType.UniformBuffer,
                                       bufferInfo: new[] { new DescriptorBufferInfo(_uniformBuffer) }),
                new WriteDescriptorSet(descriptorSet, 1, 0, 1, DescriptorType.CombinedImageSampler,
                                       imageInfo: new[] { new DescriptorImageInfo(_sampler, _cubeTexture.View, ImageLayout.General) })
            };

            _descriptorPool.UpdateSets(writeDescriptorSets);
            return(descriptorSet);
        }
Ejemplo n.º 8
0
        public void ResetPool()
        {
            var layoutCreateInfo = new DescriptorSetLayoutCreateInfo(
                new DescriptorSetLayoutBinding(0, DescriptorType.StorageBuffer, 1));
            var poolCreateInfo = new DescriptorPoolCreateInfo(
                1,
                new[] { new DescriptorPoolSize(DescriptorType.StorageBuffer, 1) });

            using (DescriptorSetLayout layout = Device.CreateDescriptorSetLayout(layoutCreateInfo))
                using (DescriptorPool pool = Device.CreateDescriptorPool(poolCreateInfo))
                {
                    pool.AllocateSets(new DescriptorSetAllocateInfo(1, layout));
                    pool.Reset();
                }
        }
Ejemplo n.º 9
0
        public void UpdateSetsDescriptorCopy()
        {
            var layoutCreateInfo = new DescriptorSetLayoutCreateInfo(
                new DescriptorSetLayoutBinding(0, DescriptorType.StorageBuffer, 1),
                new DescriptorSetLayoutBinding(1, DescriptorType.StorageBuffer, 1));
            var poolCreateInfo = new DescriptorPoolCreateInfo(
                1,
                new[] { new DescriptorPoolSize(DescriptorType.StorageBuffer, 2) });

            using (DescriptorSetLayout layout = Device.CreateDescriptorSetLayout(layoutCreateInfo))
                using (DescriptorPool pool = Device.CreateDescriptorPool(poolCreateInfo))
                {
                    DescriptorSet set = pool.AllocateSets(new DescriptorSetAllocateInfo(1, layout))[0];
                    // It is valid to copy from self to self (without overlapping memory boundaries).
                    var descriptorCopy = new CopyDescriptorSet(set, 0, 0, set, 1, 0, 1);
                    pool.UpdateSets(descriptorCopies: new[] { descriptorCopy });
                }
        }
Ejemplo n.º 10
0
        public void CmdBindDescriptorSet()
        {
            const int bufferSize = 256;

            var layoutCreateInfo = new DescriptorSetLayoutCreateInfo(
                new DescriptorSetLayoutBinding(0, DescriptorType.StorageBuffer, 1));
            var descriptorPoolCreateInfo = new DescriptorPoolCreateInfo(
                1,
                new[] { new DescriptorPoolSize(DescriptorType.StorageBuffer, 1) });

            using (DescriptorSetLayout descriptorSetLayout = Device.CreateDescriptorSetLayout(layoutCreateInfo))
                using (DescriptorPool descriptorPool = Device.CreateDescriptorPool(descriptorPoolCreateInfo))
                    using (PipelineLayout pipelineLayout = Device.CreatePipelineLayout(new PipelineLayoutCreateInfo(new[] { descriptorSetLayout })))
                        using (Buffer buffer = Device.CreateBuffer(new BufferCreateInfo(bufferSize, BufferUsages.StorageBuffer)))
                            using (DeviceMemory memory = Device.AllocateMemory(new MemoryAllocateInfo(bufferSize, 0)))
                            {
                                // Required to satisfy the validation layer.
                                buffer.GetMemoryRequirements();

                                buffer.BindMemory(memory);

                                DescriptorSet descriptorSet =
                                    descriptorPool.AllocateSets(new DescriptorSetAllocateInfo(1, descriptorSetLayout))[0];

                                var descriptorWrite = new WriteDescriptorSet(descriptorSet, 0, 0, 1, DescriptorType.StorageBuffer,
                                                                             bufferInfo: new[] { new DescriptorBufferInfo(buffer) });

                                descriptorPool.UpdateSets(new[] { descriptorWrite });

                                CommandBuffer.Begin();
                                CommandBuffer.CmdBindDescriptorSet(PipelineBindPoint.Graphics, pipelineLayout, descriptorSet);
                                CommandBuffer.CmdBindDescriptorSets(PipelineBindPoint.Graphics, pipelineLayout, 0, new[] { descriptorSet });
                                CommandBuffer.End();

                                descriptorPool.Reset();
                            }
        }
Ejemplo n.º 11
0
        public void CreateDesriptorSets(string bufName, int size)
        {
            DescriptorSetAllocateInfo dsai = new DescriptorSetAllocateInfo(
                mChainImageViews.Length, mDSLs);

            mDSets = mDPool.AllocateSets(dsai);

            for (int i = 0; i < mChainImageViews.Length; i++)
            {
                DescriptorBufferInfo dbi = new DescriptorBufferInfo();

                dbi.Buffer = mBuffers[bufName + i];
                dbi.Range  = size;

                WriteDescriptorSet wds = new WriteDescriptorSet();

                wds.DstSet          = mDSets[i];
                wds.DescriptorType  = DescriptorType.UniformBuffer;
                wds.DescriptorCount = 1;
                wds.BufferInfo      = new DescriptorBufferInfo[] { dbi };

                mDPool.UpdateSets(new WriteDescriptorSet[] { wds });
            }
        }
Ejemplo n.º 12
0
 private void RecreateDescriptorSets()
 {
     Graphics.Device.WaitIdle();
     // Clean up
     foreach (var set in GraphicsDescriptorSets.Values)
     {
         set.Dispose();
     }
     GraphicsDescriptorSets.Clear();
     foreach (var set in ComputeDescriptorSets.Values)
     {
         set.Dispose();
     }
     ComputeDescriptorSets.Clear();
     // Create sets
     foreach (var keyList in InstanceLists)
     {
         var(texture, animation) = ((Texture2D, Animation))keyList.Key;
         var list = keyList.Value;
         // Graphics set
         var set = GraphicsDescriptorPool.AllocateSets(new DescriptorSetAllocateInfo(
                                                           1, GraphicsDescriptorSetLayout
                                                           ))[0];
         GraphicsDescriptorPool.UpdateSets(
             new WriteDescriptorSet[]
         {
             new WriteDescriptorSet(
                 set, 0, 0, 1, DescriptorType.UniformBuffer,
                 bufferInfo: new DescriptorBufferInfo[]
             {
                 new DescriptorBufferInfo(CameraUniform.Buffer)
             }
                 ),
             new WriteDescriptorSet(
                 set, 1, 0, 1, DescriptorType.CombinedImageSampler,
                 imageInfo: new DescriptorImageInfo[]
             {
                 new DescriptorImageInfo(
                     TextureSampler,
                     texture.Image.ImageView,
                     ImageLayout.ShaderReadOnlyOptimal
                     )
             }
                 ),
             new WriteDescriptorSet(
                 set, 2, 0, 1, DescriptorType.UniformBuffer,
                 bufferInfo: new DescriptorBufferInfo[]
             {
                 new DescriptorBufferInfo(TimeUniform.Buffer)
             }
                 )
         }
             );
         GraphicsDescriptorSets.Add(list, set);
         // Compute set
         set = ComputeDescriptorPool.AllocateSets(new DescriptorSetAllocateInfo(
                                                      1, ComputeDescriptorSetLayout
                                                      ))[0];
         ComputeDescriptorPool.UpdateSets(
             new WriteDescriptorSet[]
         {
             new WriteDescriptorSet(
                 set, 0, 0, 1, DescriptorType.StorageBuffer,
                 bufferInfo: new DescriptorBufferInfo[]
             {
                 new DescriptorBufferInfo(ComputeInstanceBuffers[list].Buffer)
             }
                 ),
             new WriteDescriptorSet(
                 set, 1, 0, 1, DescriptorType.StorageBuffer,
                 bufferInfo: new DescriptorBufferInfo[]
             {
                 new DescriptorBufferInfo(VertexInstanceBuffers[list].Buffer)
             }
                 ),
             new WriteDescriptorSet(
                 set, 2, 0, 1, DescriptorType.UniformBuffer,
                 bufferInfo: new DescriptorBufferInfo[]
             {
                 new DescriptorBufferInfo(TimeUniform.Buffer)
             }
                 ),
             new WriteDescriptorSet(
                 set, 3, 0, 1, DescriptorType.UniformBuffer,
                 bufferInfo: new DescriptorBufferInfo[]
             {
                 new DescriptorBufferInfo(AnimationUniform.Buffer)
             }
                 )
         }
             );
         ComputeDescriptorSets.Add(list, set);
     }
 }
Ejemplo n.º 13
0
        void CreateDescriptorSet()
        {
            /*
             * So we will allocate a descriptor set here.
             * But we need to first create a descriptor pool to do that.
             */

            /*
             * Our descriptor pool can only allocate a single storage buffer.
             */
            DescriptorPoolSize descriptorPoolSize = new DescriptorPoolSize()
            {
                Type            = DescriptorType.StorageBuffer,// VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
                DescriptorCount = 1
            };
            DescriptorPoolCreateInfo descriptorPoolCreateInfo = new DescriptorPoolCreateInfo()
            {
                MaxSets   = 1, // we only need to allocate one descriptor set from the pool.
                Flags     = DescriptorPoolCreateFlags.FreeDescriptorSet,
                PoolSizes = new[] { descriptorPoolSize }
            };

            // create descriptor pool.
            descriptorPool = device.CreateDescriptorPool(descriptorPoolCreateInfo);

            /*
             * With the pool allocated, we can now allocate the descriptor set.
             */
            DescriptorSetAllocateInfo descriptorSetAllocateInfo = new DescriptorSetAllocateInfo(1, descriptorSetLayout);

            // allocate a single descriptor set.

            // allocate descriptor set.
            descriptorSet = descriptorPool.AllocateSets(descriptorSetAllocateInfo)[0];

            /*
             * Next, we need to connect our actual storage buffer with the descrptor.
             * We use vkUpdateDescriptorSets() to update the descriptor set.
             */

            // Specify the buffer to bind to the descriptor.
            DescriptorBufferInfo descriptorBufferInfo = new DescriptorBufferInfo()
            {
                Buffer = buffer,
                Offset = 0,
                Range  = bufferSize
            };
            WriteDescriptorSet writeDescriptorSet = new WriteDescriptorSet()
            {
                DstSet          = descriptorSet,                // write to this descriptor set.
                DstBinding      = 0,                            // write to the first, and only binding.
                DstArrayElement = 0,
                DescriptorCount = 1,                            // update a single descriptor.
                DescriptorType  = DescriptorType.StorageBuffer, // VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; // storage buffer.
                BufferInfo      = new[] { descriptorBufferInfo }
            };

            // perform the update of the descriptor set.
            WriteDescriptorSet[] k = { writeDescriptorSet };
            descriptorPool.UpdateSets(k);
        }
Ejemplo n.º 14
0
 public override void OnStart()
 {
     // Create triangle pipeline
     {
         // Load shaders
         {
             VertexShader   = Graphics.Content.LoadShaderModule("Shader.vert.spv");
             FragmentShader = Graphics.Content.LoadShaderModule("Shader.frag.spv");
         }
         // Create sampler
         {
             TextureSampler = Graphics.Device.CreateSampler(new SamplerCreateInfo
             {
                 MinFilter = Filter.Linear,
                 MagFilter = Filter.Linear
             });
         }
         // Create descriptor set layout
         {
             DescriptorSetLayout = Graphics.Device.CreateDescriptorSetLayout(new DescriptorSetLayoutCreateInfo(
                                                                                 new[]
             {
                 new DescriptorSetLayoutBinding(
                     binding: 0,
                     descriptorType: DescriptorType.CombinedImageSampler,
                     descriptorCount: 1,
                     stageFlags: ShaderStages.Fragment
                     )
             }
                                                                                 ));
         }
         // Create descriptor pool
         {
             DescriptorPool = Graphics.Device.CreateDescriptorPool(new DescriptorPoolCreateInfo(
                                                                       1,
                                                                       new[]
             {
                 new DescriptorPoolSize(DescriptorType.CombinedImageSampler, 1)
             }
                                                                       ));
         }
         // Create descriptor set
         {
             TextureDescriptorSet = DescriptorPool.AllocateSets(new DescriptorSetAllocateInfo(
                                                                    1, DescriptorSetLayout
                                                                    ))[0];
             var writes = new[]
             {
                 new WriteDescriptorSet(
                     TextureDescriptorSet, 0, 0, 1, DescriptorType.CombinedImageSampler,
                     imageInfo: new[]
                 {
                     new DescriptorImageInfo(
                         TextureSampler,
                         Texture.Image.ImageView,
                         ImageLayout.ShaderReadOnlyOptimal
                         )
                 }
                     )
             };
             DescriptorPool.UpdateSets(writes);
         }
         // Create pipeline layout
         {
             PipelineLayout = Graphics.Device.CreatePipelineLayout(new PipelineLayoutCreateInfo(
                                                                       setLayouts: new[] { DescriptorSetLayout }
                                                                       ));
         }
         // Create graphics pipeline
         {
             Pipeline = Graphics.Device.CreateGraphicsPipeline(new GraphicsPipelineCreateInfo(
                                                                   layout: PipelineLayout,
                                                                   renderPass: RenderPass.RenderPass,
                                                                   subpass: 0,
                                                                   stages: new[]
             {
                 new PipelineShaderStageCreateInfo(ShaderStages.Vertex, VertexShader, "main"),
                 new PipelineShaderStageCreateInfo(ShaderStages.Fragment, FragmentShader, "main")
             },
                                                                   inputAssemblyState: new PipelineInputAssemblyStateCreateInfo(PrimitiveTopology.TriangleList),
                                                                   vertexInputState: new PipelineVertexInputStateCreateInfo(),
                                                                   rasterizationState: new PipelineRasterizationStateCreateInfo(
                                                                       polygonMode: PolygonMode.Fill,
                                                                       cullMode: CullModes.Front,
                                                                       frontFace: FrontFace.Clockwise,
                                                                       lineWidth: 1f
                                                                       ),
                                                                   viewportState: new PipelineViewportStateCreateInfo(
                                                                       new Viewport(0f, 0f, Graphics.Window.Size.X, Graphics.Window.Size.Y),
                                                                       new Rect2D(0, 0, (int)Graphics.Window.Size.X, (int)Graphics.Window.Size.Y)
                                                                       ),
                                                                   multisampleState: new PipelineMultisampleStateCreateInfo(
                                                                       rasterizationSamples: SampleCounts.Count1,
                                                                       minSampleShading: 1
                                                                       ),
                                                                   colorBlendState: new PipelineColorBlendStateCreateInfo(
                                                                       attachments: new[]
             {
                 new PipelineColorBlendAttachmentState(
                     blendEnable: true,
                     srcColorBlendFactor: BlendFactor.One,
                     dstColorBlendFactor: BlendFactor.Zero,
                     colorBlendOp: BlendOp.Add,
                     srcAlphaBlendFactor: BlendFactor.One,
                     dstAlphaBlendFactor: BlendFactor.Zero,
                     alphaBlendOp: BlendOp.Add,
                     colorWriteMask: ColorComponents.All
                     )
             }
                                                                       )
                                                                   ));
         }
     }
 }