Example #1
0
        public VkResourceSet(VkGraphicsDevice gd, ref ResourceSetDescription description)
        {
            _gd = gd;
            VkResourceLayout vkLayout = Util.AssertSubtype <ResourceLayout, VkResourceLayout>(description.Layout);

            VkDescriptorSetAllocateInfo dsAI = VkDescriptorSetAllocateInfo.New();

            dsAI.descriptorSetCount = 1;
            VkDescriptorSetLayout dsl = vkLayout.DescriptorSetLayout;

            dsAI.pSetLayouts    = &dsl;
            dsAI.descriptorPool = _gd.SharedDescriptorPool;
            VkResult result = vkAllocateDescriptorSets(_gd.Device, ref dsAI, out _descriptorSet);

            CheckResult(result);

            BindableResource[] boundResources        = description.BoundResources;
            uint descriptorWriteCount                = (uint)boundResources.Length;
            VkWriteDescriptorSet *  descriptorWrites = stackalloc VkWriteDescriptorSet[(int)descriptorWriteCount];
            VkDescriptorBufferInfo *bufferInfos      = stackalloc VkDescriptorBufferInfo[(int)descriptorWriteCount];
            VkDescriptorImageInfo * imageInfos       = stackalloc VkDescriptorImageInfo[(int)descriptorWriteCount];

            for (int i = 0; i < descriptorWriteCount; i++)
            {
                VkDescriptorType type = vkLayout.DescriptorTypes[i];

                descriptorWrites[i].sType           = VkStructureType.WriteDescriptorSet;
                descriptorWrites[i].descriptorCount = 1;
                descriptorWrites[i].descriptorType  = type;
                descriptorWrites[i].dstBinding      = (uint)i;
                descriptorWrites[i].dstSet          = _descriptorSet;

                if (type == VkDescriptorType.UniformBuffer)
                {
                    VkUniformBuffer uniformBuffer = Util.AssertSubtype <BindableResource, VkUniformBuffer>(boundResources[i]);
                    bufferInfos[i].buffer           = uniformBuffer.DeviceBuffer;
                    bufferInfos[i].range            = uniformBuffer.SizeInBytes;
                    descriptorWrites[i].pBufferInfo = &bufferInfos[i];
                }
                else if (type == VkDescriptorType.SampledImage)
                {
                    VkTextureView textureView = Util.AssertSubtype <BindableResource, VkTextureView>(boundResources[i]);
                    imageInfos[i].imageView        = textureView.ImageView;
                    imageInfos[i].imageLayout      = VkImageLayout.ShaderReadOnlyOptimal;
                    descriptorWrites[i].pImageInfo = &imageInfos[i];
                }
                else if (type == VkDescriptorType.Sampler)
                {
                    VkSampler sampler = Util.AssertSubtype <BindableResource, VkSampler>(boundResources[i]);
                    imageInfos[i].sampler          = sampler.DeviceSampler;
                    descriptorWrites[i].pImageInfo = &imageInfos[i];
                }
            }

            vkUpdateDescriptorSets(_gd.Device, descriptorWriteCount, descriptorWrites, 0, null);
        }
Example #2
0
        public static VkDescriptorSetAllocateInfo descriptorSetAllocateInfo(
            VkDescriptorPool descriptorPool,
            VkDescriptorSetLayout *pSetLayouts,
            uint descriptorSetCount)
        {
            VkDescriptorSetAllocateInfo descriptorSetAllocateInfo = VkDescriptorSetAllocateInfo.New();

            descriptorSetAllocateInfo.descriptorPool     = descriptorPool;
            descriptorSetAllocateInfo.pSetLayouts        = pSetLayouts;
            descriptorSetAllocateInfo.descriptorSetCount = descriptorSetCount;
            return(descriptorSetAllocateInfo);
        }
Example #3
0
        public void Allocate(DescriptorSet descriptorSet)
        {
            VkDescriptorSetAllocateInfo allocInfo = VkDescriptorSetAllocateInfo.New();

            allocInfo.descriptorPool     = handle;
            allocInfo.descriptorSetCount = (uint)descriptorSet.descriptorSetLayouts.Count;
            allocInfo.pSetLayouts        = descriptorSet.descriptorSetLayouts.Pin();

            Utils.CheckResult(vkAllocateDescriptorSets(Dev.VkDev, ref allocInfo, out descriptorSet.handle));

            descriptorSet.descriptorSetLayouts.Unpin();
        }
        public unsafe DescriptorAllocationToken Allocate(DescriptorResourceCounts counts, VkDescriptorSetLayout setLayout)
        {
            VkDescriptorPool            pool = GetPool(counts);
            VkDescriptorSetAllocateInfo dsAI = VkDescriptorSetAllocateInfo.New();

            dsAI.descriptorSetCount = 1;
            dsAI.pSetLayouts        = &setLayout;
            dsAI.descriptorPool     = pool;
            VkResult result = vkAllocateDescriptorSets(_gd.Device, ref dsAI, out VkDescriptorSet set);

            VulkanUtil.CheckResult(result);

            return(new DescriptorAllocationToken(set, pool));
        }
Example #5
0
        void setupDescriptorSet()
        {
            VkDescriptorSetAllocateInfo allocInfo = VkDescriptorSetAllocateInfo.New();

            allocInfo.descriptorPool     = descriptorPool;
            allocInfo.pSetLayouts        = (VkDescriptorSetLayout *)descriptorSetLayout.Data.ToPointer();
            allocInfo.descriptorSetCount = 1;
            Util.CheckResult(vkAllocateDescriptorSets(Device, &allocInfo, out descriptorSet));

            var descriptor = uniformBuffer.descriptor;
            VkWriteDescriptorSet writeDescriptorSets = Initializers.writeDescriptorSet(
                descriptorSet,
                VkDescriptorType.UniformBuffer,
                0,
                &descriptor);

            vkUpdateDescriptorSets(Device, 1, ref writeDescriptorSets, 0, null);
        }
Example #6
0
        private static VkDescriptorSet[] AllocateDescriptorSets(VkDevice device, VkDescriptorSetLayout layout, VkDescriptorPool pool, uint swapchainImageCount)
        {
            VkDescriptorSetLayout[] localLayouts = new VkDescriptorSetLayout[swapchainImageCount];
            for (int i = 0; i < localLayouts.Length; i++)
            {
                localLayouts[i] = layout;
            }
            VkDescriptorSetAllocateInfo allocateInfo = VkDescriptorSetAllocateInfo.New();

            allocateInfo.descriptorPool     = pool;
            allocateInfo.descriptorSetCount = swapchainImageCount;

            fixed(VkDescriptorSetLayout *ptr = localLayouts)
            allocateInfo.pSetLayouts = ptr;

            VkDescriptorSet[] sets = new VkDescriptorSet[swapchainImageCount];

            fixed(VkDescriptorSet *ptr = sets)
            Assert(vkAllocateDescriptorSets(device, &allocateInfo, ptr));

            return(sets);
        }
        private VkDescriptorSet CreateNewDescriptorSet(ref VkDescriptorSetCacheKey cacheKey)
        {
            {
                VkDescriptorSetAllocateInfo descriptorSetAI = VkDescriptorSetAllocateInfo.New();
                descriptorSetAI.descriptorPool     = _descriptorPool;
                descriptorSetAI.descriptorSetCount = 1;
                VkDescriptorSetLayout layout = cacheKey.ShaderResourceBindingSlots.DescriptorSetLayout;
                descriptorSetAI.pSetLayouts = &layout;
                VkResult result = vkAllocateDescriptorSets(_device, ref descriptorSetAI, out VkDescriptorSet descriptorSet);
                CheckResult(result);

                int resourceCount = cacheKey.ShaderResourceBindingSlots.Resources.Length;
                VkWriteDescriptorSet[]  descriptorWrites = new VkWriteDescriptorSet[resourceCount];
                VkDescriptorBufferInfo *bufferInfos      = stackalloc VkDescriptorBufferInfo[resourceCount]; // TODO: Fix this.
                VkDescriptorImageInfo * imageInfos       = stackalloc VkDescriptorImageInfo[resourceCount];  // TODO: Fix this.

                for (uint binding = 0; binding < resourceCount; binding++)
                {
                    descriptorWrites[binding].sType           = VkStructureType.WriteDescriptorSet;
                    descriptorWrites[binding].descriptorCount = 1;
                    descriptorWrites[binding].dstBinding      = binding;
                    descriptorWrites[binding].dstSet          = descriptorSet;

                    ShaderResourceDescription resource = cacheKey.ShaderResourceBindingSlots.Resources[binding];
                    switch (resource.Type)
                    {
                    case ShaderResourceType.ConstantBuffer:
                    {
                        descriptorWrites[binding].descriptorType = VkDescriptorType.UniformBuffer;
                        VkConstantBuffer cb = cacheKey.ConstantBuffers[binding];
                        if (cb == null)
                        {
                            throw new VeldridException($"No constant buffer bound to required binding slot {binding}.");
                        }
                        VkDescriptorBufferInfo *cbInfo = &bufferInfos[binding];
                        cbInfo->buffer = cb.DeviceBuffer;
                        cbInfo->offset = 0;
                        cbInfo->range  = (ulong)resource.DataSizeInBytes;
                        descriptorWrites[binding].pBufferInfo = cbInfo;
                        break;
                    }

                    case ShaderResourceType.Texture:
                    {
                        descriptorWrites[binding].descriptorType = VkDescriptorType.SampledImage;
                        VkShaderTextureBinding textureBinding = cacheKey.TextureBindings[binding];
                        if (textureBinding == null)
                        {
                            throw new VeldridException($"No texture bound to required binding slot {binding}.");
                        }
                        VkDescriptorImageInfo *imageInfo = &imageInfos[binding];
                        imageInfo->imageLayout = textureBinding.ImageLayout;
                        imageInfo->imageView   = textureBinding.ImageView;
                        descriptorWrites[binding].pImageInfo = imageInfo;
                    }
                    break;

                    case ShaderResourceType.Sampler:
                    {
                        descriptorWrites[binding].descriptorType = VkDescriptorType.Sampler;
                        VkSamplerState         samplerState = cacheKey.SamplerStates[binding] ?? (VkSamplerState)_defaultSamplerState;
                        VkDescriptorImageInfo *imageInfo    = &imageInfos[binding];
                        imageInfo->sampler = samplerState.Sampler;
                        descriptorWrites[binding].pImageInfo = imageInfo;
                    }
                    break;

                    default:
                        throw Illegal.Value <ShaderResourceType>();
                    }
                }

                vkUpdateDescriptorSets(_device, (uint)resourceCount, ref descriptorWrites[0], 0, null);

                return(descriptorSet);
            }
        }