Exemple #1
0
        /// <summary>
        /// Free unmanaged memory and reset all members to 0.
        /// </summary>
        public void Reset()
        {
            if (this.imageInfo != null)
            {
                UInt32 count = this.count;
                IntPtr ptr   = (IntPtr)this.imageInfo;
                Helper.Set <VkDescriptorImageInfo>(null, ref ptr, ref count);
                this.imageInfo = null;
            }

            if (this.bufferInfo != null)
            {
                UInt32 count = this.count;
                IntPtr ptr   = (IntPtr)this.bufferInfo;
                Helper.Set <VkDescriptorBufferInfo>(null, ref ptr, ref count);
                this.bufferInfo = null;
            }

            if (this.texelBufferView != null)
            {
                UInt32 count = this.count;
                IntPtr ptr   = (IntPtr)this.texelBufferView;
                Helper.Set <VkBufferView>(null, ref ptr, ref count);
                this.texelBufferView = null;
            }

            {
                this.count = 0;
            }
        }
Exemple #2
0
        public void Set(params VkDescriptorImageInfo[] value)
        {
            IntPtr ptr = (IntPtr)this.imageInfo;

            value.Set(ref ptr, ref this.count);
            this.imageInfo = (VkDescriptorImageInfo *)ptr;
        }
        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);
        }
Exemple #4
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);
        }
Exemple #5
0
        public static VkWriteDescriptorSet writeDescriptorSet(
            VkDescriptorSet dstSet,
            VkDescriptorType type,
            uint binding,
            VkDescriptorImageInfo *imageInfo,
            uint descriptorCount = 1)
        {
            VkWriteDescriptorSet writeDescriptorSet = VkWriteDescriptorSet.New();

            writeDescriptorSet.dstSet          = dstSet;
            writeDescriptorSet.descriptorType  = type;
            writeDescriptorSet.dstBinding      = binding;
            writeDescriptorSet.pImageInfo      = imageInfo;
            writeDescriptorSet.descriptorCount = descriptorCount;
            return(writeDescriptorSet);
        }
Exemple #6
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);
        }
        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);
            }
        }