protected DescriptorSetExample() : base()
        {
            this.DescriptorPool = CreateDescriptorPool();

            this.DescriptorSets = AllocateDescriptorSets();

            DescriptorBufferInfo info = new DescriptorBufferInfo
            {
                Buffer = UniformBuffer,
                Offset = 0,
                Range  = this.UniformBufferSize
            };

            WriteDescriptorSet write = new WriteDescriptorSet
            {
                SType           = StructureType.WriteDescriptorSet,
                DstSet          = this.DescriptorSets[0],
                DescriptorCount = 1,
                DescriptorType  = DescriptorType.UniformBuffer,
                PBufferInfo     = &info,
                DstArrayElement = 0,
                DstBinding      = 0
            };

            VkApi.UpdateDescriptorSets(this.Device, 1, &write, 0, null);
        }
        private void CreateDescriptorSet()
        {
            var allocInfo = new DescriptorSetAllocateInfo()
            {
                DescriptorPool = vkDescriptorPool,
                SetLayouts     = new DescriptorSetLayout[] { vkDescriptorSetLayout },
            };

            vkDescriptorSet = vkDevice.AllocateDescriptorSets(allocInfo)[0];

            var bufferInfo = new DescriptorBufferInfo()
            {
                Buffer = vkUniformBuffer,
                Offset = 0,
                Range  = Marshal.SizeOf <UniformBufferObject>(),
            };

            var descriptorWrite = new WriteDescriptorSet()
            {
                DstSet          = vkDescriptorSet,
                DstBinding      = 0,
                DstArrayElement = 0,
                DescriptorType  = DescriptorType.UniformBuffer,
                DescriptorCount = 1,
                BufferInfo      = new DescriptorBufferInfo[] { bufferInfo },
            };

            vkDevice.UpdateDescriptorSet(descriptorWrite, null);
        }
Example #3
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);
        }
        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 });
                            }
        }
Example #5
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);
        }
Example #6
0
        protected override void CreateDescriptorSets()
        {
            var layouts = new DescriptorSetLayout[images.Length];

            for (int i = 0; i < layouts.Length; i++)
            {
                layouts[i] = descriptorSetLayout;
            }
            var allocInfo = new DescriptorSetAllocateInfo
            {
                DescriptorPool     = descriptorPool,
                DescriptorSetCount = (uint)images.Length,
                SetLayouts         = layouts
            };

            descriptorSets = device.AllocateDescriptorSets(allocInfo);
            for (int a = 0; a < descriptorSets.Length; a++)
            {
                var bufferInfo = new DescriptorBufferInfo
                {
                    Buffer = uniformBuffers[a],
                    Offset = 0,
                    Range  = (sizeof(float) * 16) * 3
                };

                var imageInfo = new DescriptorImageInfo
                {
                    ImageLayout = ImageLayout.ShaderReadOnlyOptimal,
                    ImageView   = textureImageView,
                    Sampler     = textureSampler
                };

                //TODO: Dispose this
                var descriptorWrites = new WriteDescriptorSet[]
                {
                    new WriteDescriptorSet
                    {
                        DstSet          = descriptorSets[a],
                        DstBinding      = 0,
                        DstArrayElement = 0,
                        DescriptorType  = DescriptorType.UniformBuffer,
                        DescriptorCount = 1,
                        BufferInfo      = new DescriptorBufferInfo[] { bufferInfo }
                    },
                    new WriteDescriptorSet
                    {
                        DstSet          = descriptorSets[a],
                        DstBinding      = 1,
                        DstArrayElement = 0,
                        DescriptorType  = DescriptorType.CombinedImageSampler,
                        DescriptorCount = 1,
                        ImageInfo       = new DescriptorImageInfo[] { imageInfo }
                    }
                };
                //device.UpdateDescriptorSet(descriptorWrites[0], null);
                //device.UpdateDescriptorSet(descriptorWrites[1], null);
                device.UpdateDescriptorSets(descriptorWrites, null);
            }
        }
 public unsafe void Bind(int index, uint binding, WriteDescriptorSetAccelerationStructureKHR *structureInfo, out WriteDescriptorSet descriptorWrite, uint count = 1)
 {
     descriptorWrite                 = new WriteDescriptorSet();
     descriptorWrite.SType           = StructureType.WriteDescriptorSet;
     descriptorWrite.DstSet          = _vkDescriptorSets[index];
     descriptorWrite.DstBinding      = binding;
     descriptorWrite.DstArrayElement = 0;
     descriptorWrite.DescriptorType  = GetBindingType(binding);
     descriptorWrite.DescriptorCount = count;
     descriptorWrite.PNext           = structureInfo;
 }
 public unsafe void Bind(int index, uint binding, DescriptorImageInfo *imageInfo, out WriteDescriptorSet descriptorWrite, uint count = 1)
 {
     descriptorWrite                 = new WriteDescriptorSet();
     descriptorWrite.SType           = StructureType.WriteDescriptorSet;
     descriptorWrite.DstSet          = _vkDescriptorSets[index];
     descriptorWrite.DstBinding      = binding;
     descriptorWrite.DstArrayElement = 0;
     descriptorWrite.DescriptorType  = GetBindingType(binding);
     descriptorWrite.DescriptorCount = count;
     descriptorWrite.PImageInfo      = imageInfo;
 }
Example #9
0
        void UpdateDescriptorSets()
        {
            var uniformBufferInfo = new DescriptorBufferInfo {
                Buffer = uniformBuffer,
                Offset = 0,
                Range  = 2 * sizeof(float)
            };
            var writeDescriptorSet = new WriteDescriptorSet {
                DstSet         = descriptorSets [0],
                DescriptorType = DescriptorType.UniformBuffer,
                BufferInfo     = new DescriptorBufferInfo [] { uniformBufferInfo }
            };

            device.UpdateDescriptorSets(new WriteDescriptorSet [] { writeDescriptorSet }, null);
        }
        public unsafe void UpdateImage(int setIndex, int bindingIndex, DescriptorImageInfo imageInfo, DescriptorType type)
        {
            if (imageInfo.ImageView.Handle != 0UL)
            {
                var writeDescriptorSet = new WriteDescriptorSet
                {
                    SType           = StructureType.WriteDescriptorSet,
                    DstSet          = _descriptorSets[setIndex],
                    DstBinding      = (uint)bindingIndex,
                    DescriptorType  = type,
                    DescriptorCount = 1,
                    PImageInfo      = &imageInfo
                };

                _holder.Api.UpdateDescriptorSets(_holder.Device, 1, writeDescriptorSet, 0, null);
            }
        }
        public unsafe void UpdateBufferImage(int setIndex, int bindingIndex, BufferView texelBufferView, DescriptorType type)
        {
            if (texelBufferView.Handle != 0UL)
            {
                var writeDescriptorSet = new WriteDescriptorSet
                {
                    SType            = StructureType.WriteDescriptorSet,
                    DstSet           = _descriptorSets[setIndex],
                    DstBinding       = (uint)bindingIndex,
                    DescriptorType   = type,
                    DescriptorCount  = 1,
                    PTexelBufferView = &texelBufferView
                };

                _holder.Api.UpdateDescriptorSets(_holder.Device, 1, writeDescriptorSet, 0, null);
            }
        }
        public unsafe void UpdateBuffer(int setIndex, int bindingIndex, DescriptorBufferInfo bufferInfo, DescriptorType type)
        {
            if (bufferInfo.Buffer.Handle != 0UL)
            {
                var writeDescriptorSet = new WriteDescriptorSet
                {
                    SType           = StructureType.WriteDescriptorSet,
                    DstSet          = _descriptorSets[setIndex],
                    DstBinding      = (uint)bindingIndex,
                    DescriptorType  = type,
                    DescriptorCount = 1,
                    PBufferInfo     = &bufferInfo
                };

                _holder.Api.UpdateDescriptorSets(_holder.Device, 1, writeDescriptorSet, 0, null);
            }
        }
Example #13
0
        public void Write(string name, DescriptorImageInfo myInfo)
        {
            var Value = myIDsToItems[name];

            WriteDescriptorSet mySet = new WriteDescriptorSet();

            mySet.DescriptorType  = DescriptorType.UniformBuffer;
            mySet.DescriptorCount = Value.DescriptorCount;
            mySet.DstBinding      = Value.Binding;
            mySet.DstSet          = myDSet[Value.Binding];
            unsafe
            {
                mySet.ImageInfo = new DescriptorImageInfo[] { myInfo };
            }
            VulkanRenderer.SelectedLogicalDevice.UpdateDescriptorSet(mySet, null);
            mySet.Dispose();
        }
        /// <summary>
        /// Sets a sampler state descriptor.
        /// </summary>
        /// <param name="slot">The slot.</param>
        /// <param name="samplerState">The sampler state.</param>
        public unsafe void SetSamplerState(int slot, SamplerState samplerState)
        {
            var imageInfo = new DescriptorImageInfo {
                Sampler = samplerState.NativeSampler
            };

            var write = new WriteDescriptorSet
            {
                sType                   = VkStructureType.WriteDescriptorSet,
                DescriptorCount         = 1,
                DestinationSet          = NativeDescriptorSet,
                DestinationBinding      = (uint)slot,
                DestinationArrayElement = 0,
                VkDescriptorType        = VkDescriptorType.Sampler,
                ImageInfo               = new IntPtr(&imageInfo),
            };

            GraphicsDevice.NativeDevice.UpdateDescriptorSets(1, &write, 0, null);
        }
        /// <summary>
        /// Sets a constant buffer view descriptor.
        /// </summary>
        /// <param name="slot">The slot.</param>
        /// <param name="buffer">The constant buffer.</param>
        /// <param name="offset">The constant buffer view start offset.</param>
        /// <param name="size">The constant buffer view size.</param>
        public unsafe void SetConstantBuffer(int slot, Buffer buffer, int offset, int size)
        {
            var bufferInfo = new DescriptorBufferInfo {
                Buffer = buffer.NativeBuffer, Offset = (ulong)offset, Range = (ulong)size
            };

            var write = new WriteDescriptorSet
            {
                sType                   = VkStructureType.WriteDescriptorSet,
                DescriptorCount         = 1,
                DestinationSet          = NativeDescriptorSet,
                DestinationBinding      = (uint)slot,
                DestinationArrayElement = 0,
                VkDescriptorType        = VkDescriptorType.UniformBuffer,
                BufferInfo              = new IntPtr(&bufferInfo)
            };

            GraphicsDevice.NativeDevice.UpdateDescriptorSets(1, &write, 0, null);
        }
        /// <summary>
        /// Sets a shader resource view descriptor.
        /// </summary>
        /// <param name="slot">The slot.</param>
        /// <param name="shaderResourceView">The shader resource view.</param>
        public unsafe void SetShaderResourceView(int slot, GraphicsResource shaderResourceView)
        {
            var write = new WriteDescriptorSet
            {
                sType                   = VkStructureType.WriteDescriptorSet,
                DescriptorCount         = 1,
                DestinationSet          = NativeDescriptorSet,
                DestinationBinding      = (uint)slot,
                DestinationArrayElement = 0,
            };

            var texture = shaderResourceView as Texture;

            if (texture != null)
            {
                var imageInfo = new DescriptorImageInfo {
                    VkImageView = texture.NativeImageView, ImageLayout = VkImageLayout.ShaderReadOnlyOptimal
                };

                write.VkDescriptorType = VkDescriptorType.SampledImage;
                write.ImageInfo        = new IntPtr(&imageInfo);

                GraphicsDevice.NativeDevice.UpdateDescriptorSets(1, &write, 0, null);
            }
            else
            {
                var buffer = shaderResourceView as Buffer;
                if (buffer != null)
                {
                    var bufferViewCopy = buffer.NativeBufferView;

                    write.VkDescriptorType = VkDescriptorType.UniformTexelBuffer;
                    write.TexelBufferView  = new IntPtr(&bufferViewCopy);

                    GraphicsDevice.NativeDevice.UpdateDescriptorSets(1, &write, 0, null);
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
        }
Example #17
0
        internal void Write <T>(string v, T mYBuffer) where T : struct
        {
            var Value                = myIDsToItems[v];
            var MVPBuffer            = new UniformBuffer <T>(mYBuffer);
            WriteDescriptorSet mySet = new WriteDescriptorSet();

            mySet.DescriptorType  = DescriptorType.UniformBuffer;
            mySet.DescriptorCount = Value.DescriptorCount;
            mySet.DstBinding      = Value.Binding;
            mySet.DstSet          = myDSet[Value.Binding];
            unsafe
            {
                mySet.BufferInfo = new DescriptorBufferInfo[] { new DescriptorBufferInfo {
                                                                    Buffer = MVPBuffer, Offset = 0, Range = MVPBuffer.SizeOfBuffer
                                                                } };
            }
            VulkanRenderer.SelectedLogicalDevice.UpdateDescriptorSet(mySet, null);
            MVPBuffer.Dispose();
            mySet.Dispose();
        }
        public unsafe void UpdateImagesCombined(int setIndex, int baseBinding, ReadOnlySpan <DescriptorImageInfo> imageInfo, DescriptorType type)
        {
            if (imageInfo.Length == 0)
            {
                return;
            }

            fixed(DescriptorImageInfo *pImageInfo = imageInfo)
            {
                for (int i = 0; i < imageInfo.Length; i++)
                {
                    bool nonNull = imageInfo[i].ImageView.Handle != 0 && imageInfo[i].Sampler.Handle != 0;
                    if (nonNull)
                    {
                        int count = 1;

                        while (i + count < imageInfo.Length &&
                               imageInfo[i + count].ImageView.Handle != 0 &&
                               imageInfo[i + count].Sampler.Handle != 0)
                        {
                            count++;
                        }

                        var writeDescriptorSet = new WriteDescriptorSet
                        {
                            SType           = StructureType.WriteDescriptorSet,
                            DstSet          = _descriptorSets[setIndex],
                            DstBinding      = (uint)(baseBinding + i),
                            DescriptorType  = DescriptorType.CombinedImageSampler,
                            DescriptorCount = (uint)count,
                            PImageInfo      = pImageInfo
                        };

                        _holder.Api.UpdateDescriptorSets(_holder.Device, 1, writeDescriptorSet, 0, null);

                        i += count - 1;
                    }
                }
            }
        }
        public unsafe void UpdateBuffers(int setIndex, int baseBinding, ReadOnlySpan <DescriptorBufferInfo> bufferInfo, DescriptorType type)
        {
            if (bufferInfo.Length == 0)
            {
                return;
            }

            fixed(DescriptorBufferInfo *pBufferInfo = bufferInfo)
            {
                var writeDescriptorSet = new WriteDescriptorSet
                {
                    SType           = StructureType.WriteDescriptorSet,
                    DstSet          = _descriptorSets[setIndex],
                    DstBinding      = (uint)baseBinding,
                    DescriptorType  = type,
                    DescriptorCount = (uint)bufferInfo.Length,
                    PBufferInfo     = pBufferInfo
                };

                _holder.Api.UpdateDescriptorSets(_holder.Device, 1, writeDescriptorSet, 0, null);
            }
        }
Example #20
0
            internal void UpdateSet(Block block, ReadOnlySpan <IShaderInput> inputs)
            {
                if (block.Container != this)
                {
                    throw new ArgumentException(
                              $"[{nameof(Chunk)}] Given block was not allocated from this pool", nameof(block));
                }
                if (!IsCompatible(inputs))
                {
                    throw new ArgumentException(
                              $"[{nameof(Chunk)}] Given inputs are not compatible with this block", nameof(inputs));
                }

                var set    = sets[block.Id];
                var writes = new WriteDescriptorSet[inputs.Length];

                for (int i = 0; i < inputs.Length; i++)
                {
                    writes[i] = inputs[i].CreateDescriptorWrite(set, binding: i);
                }
                pool.UpdateSets(writes, descriptorCopies: null);
            }
        public unsafe void UpdateStorageBuffers(int setIndex, int baseBinding, ReadOnlySpan <DescriptorBufferInfo> bufferInfo)
        {
            if (bufferInfo.Length == 0)
            {
                return;
            }

            fixed(DescriptorBufferInfo *pBufferInfo = bufferInfo)
            {
                var writeDescriptorSet = new WriteDescriptorSet
                {
                    SType           = StructureType.WriteDescriptorSet,
                    DstSet          = _descriptorSets[setIndex],
                    DstBinding      = (uint)(baseBinding & ~(Constants.MaxStorageBuffersPerStage - 1)),
                    DstArrayElement = (uint)(baseBinding & (Constants.MaxStorageBuffersPerStage - 1)),
                    DescriptorType  = DescriptorType.StorageBuffer,
                    DescriptorCount = (uint)bufferInfo.Length,
                    PBufferInfo     = pBufferInfo
                };

                _holder.Api.UpdateDescriptorSets(_holder.Device, 1, writeDescriptorSet, 0, null);
            }
        }
        public unsafe void UpdateBufferImages(int setIndex, int baseBinding, ReadOnlySpan <BufferView> texelBufferView, DescriptorType type)
        {
            if (texelBufferView.Length == 0)
            {
                return;
            }

            fixed(BufferView *pTexelBufferView = texelBufferView)
            {
                for (uint i = 0; i < texelBufferView.Length;)
                {
                    uint count = 1;

                    if (texelBufferView[(int)i].Handle != 0UL)
                    {
                        while (i + count < texelBufferView.Length && texelBufferView[(int)(i + count)].Handle != 0UL)
                        {
                            count++;
                        }

                        var writeDescriptorSet = new WriteDescriptorSet
                        {
                            SType            = StructureType.WriteDescriptorSet,
                            DstSet           = _descriptorSets[setIndex],
                            DstBinding       = (uint)baseBinding + i,
                            DescriptorType   = type,
                            DescriptorCount  = count,
                            PTexelBufferView = pTexelBufferView + i
                        };

                        _holder.Api.UpdateDescriptorSets(_holder.Device, 1, writeDescriptorSet, 0, null);
                    }

                    i += count;
                }
            }
        }
Example #23
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();
                            }
        }
Example #24
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 });
            }
        }
Example #25
0
 public void CmdPushDescriptorSetKHR(PipelineBindPoint pipelineBindPoint, PipelineLayout layout, UInt32 set, WriteDescriptorSet pDescriptorWrite)
 {
     unsafe {
         Interop.NativeMethods.vkCmdPushDescriptorSetKHR(this.m, pipelineBindPoint, layout != null ? layout.m : default(UInt64), set, (UInt32)(pDescriptorWrite != null ? 1 : 0), pDescriptorWrite != null ? pDescriptorWrite.m : (Interop.WriteDescriptorSet *) default(IntPtr));
     }
 }
        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);
        }
Example #27
0
 public abstract void CmdPushDescriptorSet([Count(Count = 0)] CommandBuffer commandBuffer, [Count(Count = 0)] PipelineBindPoint pipelineBindPoint, [Count(Count = 0)] PipelineLayout layout, [Count(Count = 0)] uint set, [Count(Count = 0)] uint descriptorWriteCount, [Count(Computed = "descriptorWriteCount"), Flow(FlowDirection.In)] ref WriteDescriptorSet pDescriptorWrites);
Example #28
0
        private void DrawInternal()
        {
            // Update descriptors
            var descriptorSets = stackalloc DescriptorSet[2];
            var setLayouts = stackalloc DescriptorSetLayout[2];
            setLayouts[0] = setLayouts[1] = descriptorSetLayout;

            var allocateInfo = new DescriptorSetAllocateInfo
            {
                StructureType = StructureType.DescriptorSetAllocateInfo,
                DescriptorPool = descriptorPool,
                DescriptorSetCount = 2,
                SetLayouts = new IntPtr(setLayouts),
            };
            device.AllocateDescriptorSets(ref allocateInfo, descriptorSets);

            var bufferInfo = new DescriptorBufferInfo
            {
                Buffer = uniformBuffer,
                Range = Vulkan.WholeSize
            };

            var write = new WriteDescriptorSet
            {
                StructureType = StructureType.WriteDescriptorSet,
                DescriptorCount = 1,
                DestinationSet = descriptorSets[0],
                DestinationBinding = 0,
                DescriptorType = DescriptorType.UniformBuffer,
                BufferInfo = new IntPtr(&bufferInfo)
            };

            var copy = new CopyDescriptorSet
            {
                StructureType = StructureType.CopyDescriptorSet,
                DescriptorCount = 1,
                SourceBinding = 0,
                DestinationBinding = 0,
                SourceSet = descriptorSets[0],
                DestinationSet = descriptorSets[1]
            };

            device.UpdateDescriptorSets(1, &write, 0, null);
            device.UpdateDescriptorSets(0, null, 1, &copy);

            // Post-present transition
            var memoryBarrier = new ImageMemoryBarrier
            {
                StructureType = StructureType.ImageMemoryBarrier,
                Image = backBuffers[currentBackBufferIndex],
                SubresourceRange = new ImageSubresourceRange(ImageAspectFlags.Color, 0, 1, 0, 1),
                OldLayout = ImageLayout.PresentSource,
                NewLayout = ImageLayout.ColorAttachmentOptimal,
                SourceAccessMask = AccessFlags.MemoryRead,
                DestinationAccessMask = AccessFlags.ColorAttachmentWrite
            };
            commandBuffer.PipelineBarrier(PipelineStageFlags.TopOfPipe, PipelineStageFlags.TopOfPipe, DependencyFlags.None, 0, null, 0, null, 1, &memoryBarrier);

            // Clear render target
            var clearRange = new ImageSubresourceRange(ImageAspectFlags.Color, 0, 1, 0, 1);
            commandBuffer.ClearColorImage(backBuffers[currentBackBufferIndex], ImageLayout.TransferDestinationOptimal, new RawColor4(0, 0, 0, 1), 1, &clearRange);

            // Begin render pass
            var renderPassBeginInfo = new RenderPassBeginInfo
            {
                StructureType = StructureType.RenderPassBeginInfo,
                RenderPass = renderPass,
                Framebuffer = framebuffers[currentBackBufferIndex],
                RenderArea = new Rect2D(0, 0, (uint)form.ClientSize.Width, (uint)form.ClientSize.Height),
            };
            commandBuffer.BeginRenderPass(ref renderPassBeginInfo, SubpassContents.Inline);

            // Bind pipeline
            commandBuffer.BindPipeline(PipelineBindPoint.Graphics, pipeline);

            // Bind descriptor sets
            commandBuffer.BindDescriptorSets(PipelineBindPoint.Graphics, pipelineLayout, 0, 1, descriptorSets + 1, 0, null);

            // Set viewport and scissor
            var viewport = new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height);
            commandBuffer.SetViewport(0, 1, &viewport);

            var scissor = new Rect2D(0, 0, (uint)form.ClientSize.Width, (uint)form.ClientSize.Height);
            commandBuffer.SetScissor(0, 1, &scissor);

            // Bind vertex buffer
            var vertexBufferCopy = vertexBuffer;
            ulong offset = 0;
            commandBuffer.BindVertexBuffers(0, 1, &vertexBufferCopy, &offset);

            // Draw vertices
            commandBuffer.Draw(3, 1, 0, 0);

            // End render pass
            commandBuffer.EndRenderPass();

            // Pre-present transition
            memoryBarrier = new ImageMemoryBarrier
            {
                StructureType = StructureType.ImageMemoryBarrier,
                Image = backBuffers[currentBackBufferIndex],
                SubresourceRange = new ImageSubresourceRange(ImageAspectFlags.Color, 0, 1, 0, 1),
                OldLayout = ImageLayout.ColorAttachmentOptimal,
                NewLayout = ImageLayout.PresentSource,
                SourceAccessMask = AccessFlags.ColorAttachmentWrite,
                DestinationAccessMask = AccessFlags.MemoryRead
            };
            commandBuffer.PipelineBarrier(PipelineStageFlags.AllCommands, PipelineStageFlags.BottomOfPipe, DependencyFlags.None, 0, null, 0, null, 1, &memoryBarrier);
        }
Example #29
0
 public unsafe void UpdateDescriptorSets(uint descriptorWriteCount, WriteDescriptorSet* descriptorWrites, uint descriptorCopyCount, CopyDescriptorSet* descriptorCopies)
 {
     vkUpdateDescriptorSets(this, descriptorWriteCount, descriptorWrites, descriptorCopyCount, descriptorCopies);
 }
Example #30
0
        private void DrawInternal()
        {
            // Update descriptors
            var descriptorSets = stackalloc DescriptorSet[2];
            var setLayouts     = stackalloc DescriptorSetLayout[2];

            setLayouts[0] = setLayouts[1] = descriptorSetLayout;

            var allocateInfo = new DescriptorSetAllocateInfo
            {
                StructureType      = StructureType.DescriptorSetAllocateInfo,
                DescriptorPool     = descriptorPool,
                DescriptorSetCount = 2,
                SetLayouts         = new IntPtr(setLayouts),
            };

            device.AllocateDescriptorSets(ref allocateInfo, descriptorSets);

            var bufferInfo = new DescriptorBufferInfo
            {
                Buffer = uniformBuffer,
                Range  = Vulkan.WholeSize
            };

            var write = new WriteDescriptorSet
            {
                StructureType      = StructureType.WriteDescriptorSet,
                DescriptorCount    = 1,
                DestinationSet     = descriptorSets[0],
                DestinationBinding = 0,
                DescriptorType     = DescriptorType.UniformBuffer,
                BufferInfo         = new IntPtr(&bufferInfo)
            };

            var copy = new CopyDescriptorSet
            {
                StructureType      = StructureType.CopyDescriptorSet,
                DescriptorCount    = 1,
                SourceBinding      = 0,
                DestinationBinding = 0,
                SourceSet          = descriptorSets[0],
                DestinationSet     = descriptorSets[1]
            };

            device.UpdateDescriptorSets(1, &write, 0, null);
            device.UpdateDescriptorSets(0, null, 1, &copy);

            // Post-present transition
            var memoryBarrier = new ImageMemoryBarrier
            {
                StructureType         = StructureType.ImageMemoryBarrier,
                Image                 = backBuffers[currentBackBufferIndex],
                SubresourceRange      = new ImageSubresourceRange(ImageAspectFlags.Color, 0, 1, 0, 1),
                OldLayout             = ImageLayout.PresentSource,
                NewLayout             = ImageLayout.ColorAttachmentOptimal,
                SourceAccessMask      = AccessFlags.MemoryRead,
                DestinationAccessMask = AccessFlags.ColorAttachmentWrite
            };

            commandBuffer.PipelineBarrier(PipelineStageFlags.TopOfPipe, PipelineStageFlags.TopOfPipe, DependencyFlags.None, 0, null, 0, null, 1, &memoryBarrier);

            // Clear render target
            var clearRange = new ImageSubresourceRange(ImageAspectFlags.Color, 0, 1, 0, 1);

            commandBuffer.ClearColorImage(backBuffers[currentBackBufferIndex], ImageLayout.TransferDestinationOptimal, new RawColor4(0, 0, 0, 1), 1, &clearRange);

            // Begin render pass
            var renderPassBeginInfo = new RenderPassBeginInfo
            {
                StructureType = StructureType.RenderPassBeginInfo,
                RenderPass    = renderPass,
                Framebuffer   = framebuffers[currentBackBufferIndex],
                RenderArea    = new Rect2D(0, 0, (uint)form.ClientSize.Width, (uint)form.ClientSize.Height),
            };

            commandBuffer.BeginRenderPass(ref renderPassBeginInfo, SubpassContents.Inline);

            // Bind pipeline
            commandBuffer.BindPipeline(PipelineBindPoint.Graphics, pipeline);

            // Bind descriptor sets
            commandBuffer.BindDescriptorSets(PipelineBindPoint.Graphics, pipelineLayout, 0, 1, descriptorSets + 1, 0, null);

            // Set viewport and scissor
            var viewport = new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height);

            commandBuffer.SetViewport(0, 1, &viewport);

            var scissor = new Rect2D(0, 0, (uint)form.ClientSize.Width, (uint)form.ClientSize.Height);

            commandBuffer.SetScissor(0, 1, &scissor);

            // Bind vertex buffer
            var   vertexBufferCopy = vertexBuffer;
            ulong offset           = 0;

            commandBuffer.BindVertexBuffers(0, 1, &vertexBufferCopy, &offset);

            // Draw vertices
            commandBuffer.Draw(3, 1, 0, 0);

            // End render pass
            commandBuffer.EndRenderPass();

            // Pre-present transition
            memoryBarrier = new ImageMemoryBarrier
            {
                StructureType         = StructureType.ImageMemoryBarrier,
                Image                 = backBuffers[currentBackBufferIndex],
                SubresourceRange      = new ImageSubresourceRange(ImageAspectFlags.Color, 0, 1, 0, 1),
                OldLayout             = ImageLayout.ColorAttachmentOptimal,
                NewLayout             = ImageLayout.PresentSource,
                SourceAccessMask      = AccessFlags.ColorAttachmentWrite,
                DestinationAccessMask = AccessFlags.MemoryRead
            };
            commandBuffer.PipelineBarrier(PipelineStageFlags.AllCommands, PipelineStageFlags.BottomOfPipe, DependencyFlags.None, 0, null, 0, null, 1, &memoryBarrier);
        }
Example #31
0
        public unsafe void ChangeTargetImages(
            Vector2D <uint> targetSize,
            ReadOnlyMemory <Image> targetImages,
            Format format,
            ColorSpaceKHR colorSpace)
        {
            _commandAllocs           = new Allocation[targetImages.Length];
            _commandBuffers          = new Buffer[targetImages.Length];
            _commandBufferCapacities = new int[targetImages.Length];
            _commandBufferCapacities.AsSpan().Fill(-1);

            if (_descriptorPool.Handle != 0)
            {
                _vk.DestroyDescriptorPool(_device, _descriptorPool, null);
            }

            _targetSize       = targetSize;
            _targetImages     = targetImages;
            _targetImageViews = new ImageView[targetImages.Length];

            var poolSizes = stackalloc  DescriptorPoolSize[]
            {
                new DescriptorPoolSize(DescriptorType.StorageImage, (uint)targetImages.Length),
                new DescriptorPoolSize(DescriptorType.StorageBuffer, (uint)targetImages.Length),
            };

            _vk.CreateDescriptorPool(_device,
                                     new DescriptorPoolCreateInfo(maxSets: (uint)targetImages.Length, poolSizeCount: 2,
                                                                  pPoolSizes: poolSizes), null, out _descriptorPool).ThrowCode();
            Name(ObjectType.DescriptorPool, _descriptorPool.Handle, $"Target Descriptor Pool");

            _targetDescriptorSets = new DescriptorSet[targetImages.Length];
            var setLayouts = stackalloc DescriptorSetLayout[targetImages.Length];

            new Span <DescriptorSetLayout>(setLayouts, targetImages.Length).Fill(_setLayout);
            var info = new DescriptorSetAllocateInfo(descriptorPool: _descriptorPool,
                                                     descriptorSetCount: (uint)targetImages.Length, pSetLayouts: setLayouts);

            _vk.AllocateDescriptorSets(_device, &info, _targetDescriptorSets.AsSpan()).ThrowCode();

            var imageInfos = stackalloc DescriptorImageInfo[targetImages.Length];
            Span <WriteDescriptorSet> writes = stackalloc WriteDescriptorSet[targetImages.Length];

            for (int i = 0; i < targetImages.Length; i++)
            {
                Name(ObjectType.Image, _targetImages.Span[i].Handle, $"Target Image {i}");

                // _vk.CreateImage(_device,
                //     new ImageCreateInfo(imageType: ImageType.ImageType2D, format: Format.R8G8B8A8Srgb,
                //         extent: new Extent3D(_targetSize.X, _targetSize.Y, 1), mipLevels: 1, arrayLayers: 1,
                //         samples: SampleCountFlags.SampleCount1Bit, tiling: ImageTiling.Optimal,
                //         initialLayout: ImageLayout.Undefined, usage: ImageUsageFlags.ImageUsageStorageBit,
                //         sharingMode: SharingMode.Exclusive, queueFamilyIndexCount: 0, pQueueFamilyIndices: null), null,
                //     out _ownImages[i]).ThrowCode();

                _vk.CreateImageView(_device,
                                    new ImageViewCreateInfo(image: _targetImages.Span[i], viewType: ImageViewType.ImageViewType2D,
                                                            format: format,
                                                            components: new ComponentMapping(ComponentSwizzle.Identity, ComponentSwizzle.Identity,
                                                                                             ComponentSwizzle.Identity, ComponentSwizzle.Identity),
                                                            subresourceRange: new ImageSubresourceRange(ImageAspectFlags.ImageAspectColorBit, 0, 1, 0, 1)),
                                    null, out _targetImageViews[i]).ThrowCode();
                Name(ObjectType.ImageView, _targetImageViews[i].Handle, $"Target Image View {i}");

                imageInfos[i] = new DescriptorImageInfo(imageView: _targetImageViews[i], imageLayout: ImageLayout.General);
                writes[i]     = new WriteDescriptorSet(dstSet: _targetDescriptorSets[i], dstBinding: 0, dstArrayElement: 0,
                                                       descriptorCount: 1, descriptorType: DescriptorType.StorageImage, pImageInfo: (imageInfos + i));

                Name(ObjectType.DescriptorSet, _targetDescriptorSets[i].Handle, $"Target Descriptor {i}");
            }

            _vk.UpdateDescriptorSets(_device, (uint)targetImages.Length, writes, 0, (CopyDescriptorSet *)null);
        }
Example #32
0
 internal static unsafe extern void vkUpdateDescriptorSets(Device device, uint descriptorWriteCount, WriteDescriptorSet* descriptorWrites, uint descriptorCopyCount, CopyDescriptorSet* descriptorCopies);