public VkResourceSet(VkGraphicsDevice gd, ref ResourceSetDescription description)
            : base(ref description)
        {
            _gd = gd;
            VkResourceLayout vkLayout = Util.AssertSubtype <ResourceLayout, VkResourceLayout>(description.Layout);

            VkDescriptorSetLayout dsl = vkLayout.DescriptorSetLayout;

            _descriptorCounts          = vkLayout.DescriptorResourceCounts;
            _descriptorAllocationToken = _gd.DescriptorPoolManager.Allocate(_descriptorCounts, dsl);

            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          = _descriptorAllocationToken.Set;

                if (type == VkDescriptorType.UniformBuffer || type == VkDescriptorType.StorageBuffer)
                {
                    VkBuffer vkBuffer = Util.AssertSubtype <BindableResource, VkBuffer>(boundResources[i]);
                    bufferInfos[i].buffer           = vkBuffer.DeviceBuffer;
                    bufferInfos[i].range            = vkBuffer.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.StorageImage)
                {
                    VkTextureView textureView = Util.AssertSubtype <BindableResource, VkTextureView>(boundResources[i]);
                    imageInfos[i].imageView        = textureView.ImageView;
                    imageInfos[i].imageLayout      = VkImageLayout.General;
                    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);
        }
Пример #2
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);
        }
Пример #3
0
        public VkResourceSet(VkGraphicsDevice gd, ref ResourceSetDescription description)
            : base(ref description)
        {
            _gd      = gd;
            RefCount = new ResourceRefCount(DisposeCore);
            VkResourceLayout vkLayout = Util.AssertSubtype <ResourceLayout, VkResourceLayout>(description.Layout);

            VkDescriptorSetLayout dsl = vkLayout.DescriptorSetLayout;

            _descriptorCounts          = vkLayout.DescriptorResourceCounts;
            _descriptorAllocationToken = _gd.DescriptorPoolManager.Allocate(_descriptorCounts, dsl);

            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          = _descriptorAllocationToken.Set;

                if (type == VkDescriptorType.UniformBuffer || type == VkDescriptorType.UniformBufferDynamic ||
                    type == VkDescriptorType.StorageBuffer || type == VkDescriptorType.StorageBufferDynamic)
                {
                    DeviceBufferRange range          = Util.GetBufferRange(boundResources[i], 0);
                    VkBuffer          rangedVkBuffer = Util.AssertSubtype <DeviceBuffer, VkBuffer>(range.Buffer);
                    bufferInfos[i].buffer           = rangedVkBuffer.DeviceBuffer;
                    bufferInfos[i].offset           = range.Offset;
                    bufferInfos[i].range            = range.SizeInBytes;
                    descriptorWrites[i].pBufferInfo = &bufferInfos[i];
                    _refCounts.Add(rangedVkBuffer.RefCount);
                }
                else if (type == VkDescriptorType.SampledImage)
                {
                    TextureView   texView   = Util.GetTextureView(_gd, boundResources[i]);
                    VkTextureView vkTexView = Util.AssertSubtype <TextureView, VkTextureView>(texView);
                    imageInfos[i].imageView        = vkTexView.ImageView;
                    imageInfos[i].imageLayout      = VkImageLayout.ShaderReadOnlyOptimal;
                    descriptorWrites[i].pImageInfo = &imageInfos[i];
                    _sampledTextures.Add(Util.AssertSubtype <Texture, VkTexture>(texView.Target));
                    _refCounts.Add(vkTexView.RefCount);
                }
                else if (type == VkDescriptorType.StorageImage)
                {
                    TextureView   texView   = Util.GetTextureView(_gd, boundResources[i]);
                    VkTextureView vkTexView = Util.AssertSubtype <TextureView, VkTextureView>(texView);
                    imageInfos[i].imageView        = vkTexView.ImageView;
                    imageInfos[i].imageLayout      = VkImageLayout.General;
                    descriptorWrites[i].pImageInfo = &imageInfos[i];
                    _storageImages.Add(Util.AssertSubtype <Texture, VkTexture>(texView.Target));
                    _refCounts.Add(vkTexView.RefCount);
                }
                else if (type == VkDescriptorType.Sampler)
                {
                    VkSampler sampler = Util.AssertSubtype <BindableResource, VkSampler>(boundResources[i]);
                    imageInfos[i].sampler          = sampler.DeviceSampler;
                    descriptorWrites[i].pImageInfo = &imageInfos[i];
                    _refCounts.Add(sampler.RefCount);
                }
            }

            vkUpdateDescriptorSets(_gd.Device, descriptorWriteCount, descriptorWrites, 0, null);
        }