Пример #1
0
        private unsafe void AllocDescriptorSets(uint n)
        {
            if (_count + n > _capacity)
            {
                Device device = _renderer.Params.Device;
                AllocationCallbacks *allocator = (AllocationCallbacks *)_renderer.Params.AllocationCallbacks.ToPointer();
                Vk vk = _renderer.Vk;

                int cdescriptorSets = Math.Max(_count + (int)n, 16) + (_descriptorSets.Length / 2);
                Array.Resize(ref _descriptorSets, cdescriptorSets);
                _capacity = cdescriptorSets;

                vk.DestroyDescriptorPool(device, _pool, allocator);
                CreateDescriptorPool((uint)_capacity);

                DescriptorSetAllocateInfo descriptorSetAllocateInfo = new()
                {
                    SType = StructureType.DescriptorSetAllocateInfo,

                    DescriptorPool     = _pool,
                    DescriptorSetCount = 1
                };
                fixed(DescriptorSetLayout *ptr = &_layout)
                {
                    descriptorSetAllocateInfo.PSetLayouts = ptr;
                }

                for (int i = 0; i < _capacity; i++)
                {
                    _renderer.AssertVulkan(vk.AllocateDescriptorSets(device, descriptorSetAllocateInfo, out _descriptorSets[i]));
                }
            }
        }
Пример #2
0
        public void ChangeNumberOfFrames(int newFrameCount)
        {
            if (_isInitialized)
            {
                DestroyFrameResources();
            }

            var pPoolSizes = stackalloc DescriptorPoolSize[]
            {
                new DescriptorPoolSize(DescriptorType.SampledImage, (uint)(newFrameCount * _numSampledImages)),
                new DescriptorPoolSize(DescriptorType.StorageImage, (uint)(newFrameCount * _numStorageImages)),
                new DescriptorPoolSize(DescriptorType.Sampler, (uint)(newFrameCount * _numSamplers)),
            };

            _vk.CreateDescriptorPool(_device, new DescriptorPoolCreateInfo(flags: DescriptorPoolCreateFlags.DescriptorPoolCreateUpdateAfterBindBit,
                                                                           maxSets: (uint)newFrameCount, poolSizeCount: 3, pPoolSizes: pPoolSizes),
                                     null, out _descriptorPool).ThrowCode();

            _descriptorSets = new DescriptorSetInfo[newFrameCount];

            var pSetLayouts = stackalloc DescriptorSetLayout[newFrameCount];

            for (int i = 0; i < newFrameCount; i++)
            {
                pSetLayouts[i] = _descriptorSetLayout;
            }

            var descriptorSets = stackalloc DescriptorSet[newFrameCount];

            _vk.AllocateDescriptorSets(_device, new DescriptorSetAllocateInfo(descriptorPool: _descriptorPool,
                                                                              descriptorSetCount: (uint)newFrameCount, pSetLayouts: pSetLayouts), descriptorSets);

            for (int i = 0; i < newFrameCount; i++)
            {
                _descriptorSets[i] = new DescriptorSetInfo(descriptorSets[i]);
            }

            _isInitialized = true;
        }
Пример #3
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);
        }