internal unsafe SharpVulkan.DescriptorSet AllocateDescriptorSet(DescriptorSetLayout descriptorSetLayout) { // Keep track of descriptor pool usage bool isPoolExhausted = ++allocatedSetCount > GraphicsDevice.MaxDescriptorSetCount; for (int i = 0; i < DescriptorSetLayout.DescriptorTypeCount; i++) { allocatedTypeCounts[i] += descriptorSetLayout.TypeCounts[i]; if (allocatedTypeCounts[i] > GraphicsDevice.MaxDescriptorTypeCounts[i]) { isPoolExhausted = true; break; } } if (isPoolExhausted) { return SharpVulkan.DescriptorSet.Null; } // Allocate new descriptor set var nativeLayoutCopy = descriptorSetLayout.NativeLayout; var allocateInfo = new DescriptorSetAllocateInfo { StructureType = StructureType.DescriptorSetAllocateInfo, DescriptorPool = NativeDescriptorPool, DescriptorSetCount = 1, SetLayouts = new IntPtr(&nativeLayoutCopy) }; SharpVulkan.DescriptorSet descriptorSet; GraphicsDevice.NativeDevice.AllocateDescriptorSets(ref allocateInfo, &descriptorSet); return descriptorSet; }
public unsafe DescriptorSets(Api api, DescriptorPool descriptorPool, DescriptorSetLayout layout, Dictionary <uint, DescriptorType> bindingTypes, ulong size) { (_api, _descriptorPool, _bindingTypes) = (api, descriptorPool, bindingTypes); Span <VkDescriptorSetLayout> layouts = stackalloc VkDescriptorSetLayout[(int)size]; for (int i = 0; i < layouts.Length; i++) { layouts[i] = layout.VkDescriptorSetLayout; } var allocInfo = new DescriptorSetAllocateInfo(); allocInfo.SType = StructureType.DescriptorSetAllocateInfo; allocInfo.DescriptorPool = descriptorPool.VkDescriptorPool; allocInfo.DescriptorSetCount = (uint)size; allocInfo.PSetLayouts = (VkDescriptorSetLayout *)Unsafe.AsPointer(ref layouts[0]); _vkDescriptorSets = new DescriptorSet[(int)size]; fixed(DescriptorSet *pDescriptorSets = &_vkDescriptorSets[0]) { Util.Verify( _api.Vk.AllocateDescriptorSets(_api.Device.VkDevice, allocInfo, pDescriptorSets), $"{nameof(descriptorPool)}: Cannot allocate descriptor sets"); } }
public unsafe DescriptorSetCollection AllocateDescriptorSets(ReadOnlySpan <DescriptorSetLayout> layouts) { Debug.Assert(!_done); _totalSets += layouts.Length; _setsInUse += layouts.Length; DescriptorSet[] descriptorSets = new DescriptorSet[layouts.Length]; fixed(DescriptorSet *pDescriptorSets = descriptorSets) { fixed(DescriptorSetLayout *pLayouts = layouts) { var descriptorSetAllocateInfo = new DescriptorSetAllocateInfo() { SType = StructureType.DescriptorSetAllocateInfo, DescriptorPool = _pool, DescriptorSetCount = (uint)layouts.Length, PSetLayouts = pLayouts }; Api.AllocateDescriptorSets(Device, &descriptorSetAllocateInfo, pDescriptorSets).ThrowOnError(); } } return(new DescriptorSetCollection(this, descriptorSets)); }
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); }
private DescriptorSet[] AllocateDescriptorSets() { fixed(DescriptorSetLayout *pLayouts = this.DescriptorSetLayouts) { DescriptorSetAllocateInfo allocInfo = new DescriptorSetAllocateInfo { SType = StructureType.DescriptorSetAllocateInfo, DescriptorPool = this.DescriptorPool, DescriptorSetCount = (uint)this.DescriptorSetLayouts.Length, PSetLayouts = pLayouts }; var arr = new DescriptorSet[this.DescriptorSetLayouts.Length]; fixed(DescriptorSet *pSets = arr) { var res = VkApi.AllocateDescriptorSets(this.Device, &allocInfo, pSets); if (res != Result.Success) { throw new VMASharp.VulkanResultException("Failed to allocate Descriptor Sets!", res); } return(arr); } } }
internal unsafe SharpVulkan.DescriptorSet AllocateDescriptorSet(DescriptorSetLayout descriptorSetLayout) { // Keep track of descriptor pool usage bool isPoolExhausted = ++allocatedSetCount > GraphicsDevice.MaxDescriptorSetCount; for (int i = 0; i < DescriptorSetLayout.DescriptorTypeCount; i++) { allocatedTypeCounts[i] += descriptorSetLayout.TypeCounts[i]; if (allocatedTypeCounts[i] > GraphicsDevice.MaxDescriptorTypeCounts[i]) { isPoolExhausted = true; break; } } if (isPoolExhausted) { return(SharpVulkan.DescriptorSet.Null); } // Allocate new descriptor set var nativeLayoutCopy = descriptorSetLayout.NativeLayout; var allocateInfo = new DescriptorSetAllocateInfo { StructureType = StructureType.DescriptorSetAllocateInfo, DescriptorPool = NativeDescriptorPool, DescriptorSetCount = 1, SetLayouts = new IntPtr(&nativeLayoutCopy) }; SharpVulkan.DescriptorSet descriptorSet; GraphicsDevice.NativeDevice.AllocateDescriptorSets(ref allocateInfo, &descriptorSet); return(descriptorSet); }
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 void Write(string name, DescriptorImageInfo myInfo) * internal void WriteUniform<T>(string v, T mYBuffer) where T : struct*/ internal void AllocateFrom(DescriptorPool myNewPoolObject) { DescriptorSetLayout myBindings = GetDescriptorSetLayout(); DescriptorSetLayoutCreateInfo myInfo = new DescriptorSetLayoutCreateInfo(); DescriptorSetAllocateInfo mySetAlloc = new DescriptorSetAllocateInfo(); //TODO: Garbage and memory stuff mySetAlloc.SetLayouts = new DescriptorSetLayout[] { myBindings }; mySetAlloc.DescriptorPool = myNewPoolObject; myDSet = VulkanRenderer.SelectedLogicalDevice.AllocateDescriptorSets(mySetAlloc); // Debug.Assert(myDSet.Count() == myBindings.Count); }
DescriptorSet CreateDescriptorSet(DescriptorPool pool, DescriptorSetLayout layout, ImageData imageData, UniformData uniformData) { var allocInfo = new DescriptorSetAllocateInfo(pool, new[] { layout }); var sets = device.AllocateDescriptorSets(allocInfo); var descriptorSet = sets.First(); var texDescriptor = new DescriptorImageInfo(imageData.Sampler, imageData.View, ImageLayout.General); var writeDescriptorSets = new[] { new WriteDescriptorSet(descriptorSet, 0, 0, DescriptorType.UniformBuffer, null, null, null), new WriteDescriptorSet(descriptorSet, 1, 0, DescriptorType.CombinedImageSampler, null, null, null), }; writeDescriptorSets[0].BufferInfo = new[] { uniformData.Descriptor }; writeDescriptorSets[1].ImageInfo = new[] { texDescriptor }; device.UpdateDescriptorSets(writeDescriptorSets, null); return(descriptorSet); }
DescriptorSet[] CreateDescriptorSets() { var typeCount = new DescriptorPoolSize { Type = DescriptorType.UniformBuffer, DescriptorCount = 1 }; var descriptorPoolCreateInfo = new DescriptorPoolCreateInfo { PoolSizes = new DescriptorPoolSize [] { typeCount }, MaxSets = 1 }; var descriptorPool = device.CreateDescriptorPool(descriptorPoolCreateInfo); var descriptorSetAllocateInfo = new DescriptorSetAllocateInfo { SetLayouts = new DescriptorSetLayout [] { descriptorSetLayout }, DescriptorPool = descriptorPool }; return(device.AllocateDescriptorSets(descriptorSetAllocateInfo)); }
private unsafe bool TryAllocateDescriptorSets(ReadOnlySpan <DescriptorSetLayout> layouts, bool isTry, out DescriptorSetCollection dsc) { Debug.Assert(!_done); DescriptorSet[] descriptorSets = new DescriptorSet[layouts.Length]; fixed(DescriptorSet *pDescriptorSets = descriptorSets) { fixed(DescriptorSetLayout *pLayouts = layouts) { var descriptorSetAllocateInfo = new DescriptorSetAllocateInfo() { SType = StructureType.DescriptorSetAllocateInfo, DescriptorPool = _pool, DescriptorSetCount = (uint)layouts.Length, PSetLayouts = pLayouts }; var result = Api.AllocateDescriptorSets(Device, &descriptorSetAllocateInfo, pDescriptorSets); if (isTry && result == Result.ErrorOutOfPoolMemory) { _totalSets = (int)_capacity; _done = true; DestroyIfDone(); dsc = default; return(false); } result.ThrowOnError(); } } _totalSets += layouts.Length; _setsInUse += layouts.Length; dsc = new DescriptorSetCollection(this, descriptorSets); return(true); }
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 }); } }
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, ©); // 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); }
internal static unsafe extern Result vkAllocateDescriptorSets(Device device, DescriptorSetAllocateInfo* allocateInfo, DescriptorSet* descriptorSets);
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); }
public unsafe void AllocateDescriptorSets(ref DescriptorSetAllocateInfo allocateInfo, DescriptorSet* descriptorSets) { fixed (DescriptorSetAllocateInfo* __allocateInfo__ = &allocateInfo) { vkAllocateDescriptorSets(this, __allocateInfo__, descriptorSets).CheckError(); } }
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); }