private DescriptorSet CreateGraphicsDescriptorSet() { DescriptorSet descriptorSet = _descriptorPool.AllocateSets(new DescriptorSetAllocateInfo(1, _graphicsDescriptorSetLayout))[0]; _descriptorPool.UpdateSets(new[] { // Particle diffuse map. new WriteDescriptorSet(descriptorSet, 0, 0, 1, DescriptorType.CombinedImageSampler, new[] { new DescriptorImageInfo(_sampler, _particleDiffuseMap.View, ImageLayout.ColorAttachmentOptimal) }) }); return(descriptorSet); }
public void UpdateSetsDescriptorWrite() { const int bufferSize = 256; var layoutCreateInfo = new DescriptorSetLayoutCreateInfo( new DescriptorSetLayoutBinding(0, DescriptorType.StorageBuffer, 1)); var poolCreateInfo = new DescriptorPoolCreateInfo( 1, new[] { new DescriptorPoolSize(DescriptorType.StorageBuffer, 1) }, DescriptorPoolCreateFlags.FreeDescriptorSet); using (Buffer buffer = Device.CreateBuffer(new BufferCreateInfo(bufferSize, BufferUsages.StorageBuffer))) using (DeviceMemory memory = Device.AllocateMemory(new MemoryAllocateInfo(bufferSize, 0))) using (DescriptorSetLayout layout = Device.CreateDescriptorSetLayout(layoutCreateInfo)) using (DescriptorPool pool = Device.CreateDescriptorPool(poolCreateInfo)) using (DescriptorSet set = pool.AllocateSets(new DescriptorSetAllocateInfo(1, layout))[0]) { // Required to satisfy the validation layer. buffer.GetMemoryRequirements(); buffer.BindMemory(memory); var descriptorWrite = new WriteDescriptorSet(set, 0, 0, 1, DescriptorType.StorageBuffer, bufferInfo: new[] { new DescriptorBufferInfo(buffer) }); pool.UpdateSets(new[] { descriptorWrite }); } }
private void CreateDescriptors() { int bindingCount = Inputs.Count + 1; // + 1 output. // Setup bindings. var bindings = new DescriptorSetLayoutBinding[bindingCount]; for (int i = 0; i < Inputs.Count; i++) { bindings[i] = Inputs[i].GetDescriptorSetLayoutBinding(i); } bindings[Inputs.Count] = Output.GetDescriptorSetLayoutBinding(Inputs.Count); _descriptorSetLayout = Device.Logical.CreateDescriptorSetLayout(new DescriptorSetLayoutCreateInfo(bindings)); var descriptorPoolCreateInfo = new DescriptorPoolCreateInfo( 1, new[] { new DescriptorPoolSize(DescriptorType.StorageBuffer, bindingCount) }); _descriptorPool = Device.Logical.CreateDescriptorPool(descriptorPoolCreateInfo); _descriptorSet = _descriptorPool.AllocateSets(new DescriptorSetAllocateInfo(1, _descriptorSetLayout))[0]; // Setup write descriptors. var writeDescriptorSets = new WriteDescriptorSet[bindingCount]; for (int i = 0; i < Inputs.Count; i++) { writeDescriptorSets[i] = Inputs[i].GetWriteDescriptorSet(_descriptorSet, i); } writeDescriptorSets[Inputs.Count] = Output.GetWriteDescriptorSet(_descriptorSet, Inputs.Count); _descriptorPool.UpdateSets(writeDescriptorSets); }
public static DescriptorSet CreateDescriptorSet(DescriptorPool pool, DescriptorSetLayout setLayout, DescriptorItem[] items, out Sampler[] samplers) { DescriptorSet descriptorSet = pool.AllocateSets(new DescriptorSetAllocateInfo(1, setLayout))[0]; var writeDescriptorSets = new WriteDescriptorSet[items.Length]; for (var i = 0; i < items.Length; i++) { var item = items[i]; switch (item.Type) { case DescriptorItem.DescriptorType.UniformBuffer: writeDescriptorSets[i] = new WriteDescriptorSet(descriptorSet, i, 0, item.Count, DescriptorType.UniformBuffer, bufferInfo: new[] { new DescriptorBufferInfo(item.Buffer, 0, item.Buffer.Size) }); break; case DescriptorItem.DescriptorType.CombinedImageSampler: _SamplerCollection.Add(item.Sampler); writeDescriptorSets[i] = new WriteDescriptorSet(descriptorSet, i, 0, 1, DescriptorType.CombinedImageSampler, imageInfo: new[] { new DescriptorImageInfo(item.Sampler, item.Texture.View, VulkanCore.ImageLayout.General) }); break; default: throw new NotImplementedException($"No case for {item.Type}"); } } pool.UpdateSets(writeDescriptorSets); samplers = _SamplerCollection.ToArray(); _SamplerCollection.Clear(); return(descriptorSet); }
private DescriptorSet CreateDescriptorSet() { DescriptorSet descriptorSet = _descriptorPool.AllocateSets(new DescriptorSetAllocateInfo(1, _descriptorSetLayout))[0]; // Update the descriptor set for the shader binding point. var writeDescriptorSets = new[] { new WriteDescriptorSet(descriptorSet, 0, 0, 1, DescriptorType.UniformBuffer, bufferInfo: new[] { new DescriptorBufferInfo(_uniformBuffer) }), new WriteDescriptorSet(descriptorSet, 1, 0, 1, DescriptorType.CombinedImageSampler, imageInfo: new[] { new DescriptorImageInfo(_sampler, _cubeTexture.View, ImageLayout.General) }) }; _descriptorPool.UpdateSets(writeDescriptorSets); return(descriptorSet); }
public void UpdateSetsDescriptorCopy() { var layoutCreateInfo = new DescriptorSetLayoutCreateInfo( new DescriptorSetLayoutBinding(0, DescriptorType.StorageBuffer, 1), new DescriptorSetLayoutBinding(1, DescriptorType.StorageBuffer, 1)); var poolCreateInfo = new DescriptorPoolCreateInfo( 1, new[] { new DescriptorPoolSize(DescriptorType.StorageBuffer, 2) }); using (DescriptorSetLayout layout = Device.CreateDescriptorSetLayout(layoutCreateInfo)) using (DescriptorPool pool = Device.CreateDescriptorPool(poolCreateInfo)) { DescriptorSet set = pool.AllocateSets(new DescriptorSetAllocateInfo(1, layout))[0]; // It is valid to copy from self to self (without overlapping memory boundaries). var descriptorCopy = new CopyDescriptorSet(set, 0, 0, set, 1, 0, 1); pool.UpdateSets(descriptorCopies: new[] { descriptorCopy }); } }
internal void UpdateSet(Block block, ReadOnlySpan <IShaderInput> inputs) { if (block.Container != this) { throw new ArgumentException( $"[{nameof(Chunk)}] Given block was not allocated from this pool", nameof(block)); } if (!IsCompatible(inputs)) { throw new ArgumentException( $"[{nameof(Chunk)}] Given inputs are not compatible with this block", nameof(inputs)); } var set = sets[block.Id]; var writes = new WriteDescriptorSet[inputs.Length]; for (int i = 0; i < inputs.Length; i++) { writes[i] = inputs[i].CreateDescriptorWrite(set, binding: i); } pool.UpdateSets(writes, descriptorCopies: null); }
public void CmdBindDescriptorSet() { const int bufferSize = 256; var layoutCreateInfo = new DescriptorSetLayoutCreateInfo( new DescriptorSetLayoutBinding(0, DescriptorType.StorageBuffer, 1)); var descriptorPoolCreateInfo = new DescriptorPoolCreateInfo( 1, new[] { new DescriptorPoolSize(DescriptorType.StorageBuffer, 1) }); using (DescriptorSetLayout descriptorSetLayout = Device.CreateDescriptorSetLayout(layoutCreateInfo)) using (DescriptorPool descriptorPool = Device.CreateDescriptorPool(descriptorPoolCreateInfo)) using (PipelineLayout pipelineLayout = Device.CreatePipelineLayout(new PipelineLayoutCreateInfo(new[] { descriptorSetLayout }))) using (Buffer buffer = Device.CreateBuffer(new BufferCreateInfo(bufferSize, BufferUsages.StorageBuffer))) using (DeviceMemory memory = Device.AllocateMemory(new MemoryAllocateInfo(bufferSize, 0))) { // Required to satisfy the validation layer. buffer.GetMemoryRequirements(); buffer.BindMemory(memory); DescriptorSet descriptorSet = descriptorPool.AllocateSets(new DescriptorSetAllocateInfo(1, descriptorSetLayout))[0]; var descriptorWrite = new WriteDescriptorSet(descriptorSet, 0, 0, 1, DescriptorType.StorageBuffer, bufferInfo: new[] { new DescriptorBufferInfo(buffer) }); descriptorPool.UpdateSets(new[] { descriptorWrite }); CommandBuffer.Begin(); CommandBuffer.CmdBindDescriptorSet(PipelineBindPoint.Graphics, pipelineLayout, descriptorSet); CommandBuffer.CmdBindDescriptorSets(PipelineBindPoint.Graphics, pipelineLayout, 0, new[] { descriptorSet }); CommandBuffer.End(); descriptorPool.Reset(); } }
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 RecreateDescriptorSets() { Graphics.Device.WaitIdle(); // Clean up foreach (var set in GraphicsDescriptorSets.Values) { set.Dispose(); } GraphicsDescriptorSets.Clear(); foreach (var set in ComputeDescriptorSets.Values) { set.Dispose(); } ComputeDescriptorSets.Clear(); // Create sets foreach (var keyList in InstanceLists) { var(texture, animation) = ((Texture2D, Animation))keyList.Key; var list = keyList.Value; // Graphics set var set = GraphicsDescriptorPool.AllocateSets(new DescriptorSetAllocateInfo( 1, GraphicsDescriptorSetLayout ))[0]; GraphicsDescriptorPool.UpdateSets( new WriteDescriptorSet[] { new WriteDescriptorSet( set, 0, 0, 1, DescriptorType.UniformBuffer, bufferInfo: new DescriptorBufferInfo[] { new DescriptorBufferInfo(CameraUniform.Buffer) } ), new WriteDescriptorSet( set, 1, 0, 1, DescriptorType.CombinedImageSampler, imageInfo: new DescriptorImageInfo[] { new DescriptorImageInfo( TextureSampler, texture.Image.ImageView, ImageLayout.ShaderReadOnlyOptimal ) } ), new WriteDescriptorSet( set, 2, 0, 1, DescriptorType.UniformBuffer, bufferInfo: new DescriptorBufferInfo[] { new DescriptorBufferInfo(TimeUniform.Buffer) } ) } ); GraphicsDescriptorSets.Add(list, set); // Compute set set = ComputeDescriptorPool.AllocateSets(new DescriptorSetAllocateInfo( 1, ComputeDescriptorSetLayout ))[0]; ComputeDescriptorPool.UpdateSets( new WriteDescriptorSet[] { new WriteDescriptorSet( set, 0, 0, 1, DescriptorType.StorageBuffer, bufferInfo: new DescriptorBufferInfo[] { new DescriptorBufferInfo(ComputeInstanceBuffers[list].Buffer) } ), new WriteDescriptorSet( set, 1, 0, 1, DescriptorType.StorageBuffer, bufferInfo: new DescriptorBufferInfo[] { new DescriptorBufferInfo(VertexInstanceBuffers[list].Buffer) } ), new WriteDescriptorSet( set, 2, 0, 1, DescriptorType.UniformBuffer, bufferInfo: new DescriptorBufferInfo[] { new DescriptorBufferInfo(TimeUniform.Buffer) } ), new WriteDescriptorSet( set, 3, 0, 1, DescriptorType.UniformBuffer, bufferInfo: new DescriptorBufferInfo[] { new DescriptorBufferInfo(AnimationUniform.Buffer) } ) } ); ComputeDescriptorSets.Add(list, set); } }
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); }
public override void OnStart() { // Create triangle pipeline { // Load shaders { VertexShader = Graphics.Content.LoadShaderModule("Shader.vert.spv"); FragmentShader = Graphics.Content.LoadShaderModule("Shader.frag.spv"); } // Create sampler { TextureSampler = Graphics.Device.CreateSampler(new SamplerCreateInfo { MinFilter = Filter.Linear, MagFilter = Filter.Linear }); } // Create descriptor set layout { DescriptorSetLayout = Graphics.Device.CreateDescriptorSetLayout(new DescriptorSetLayoutCreateInfo( new[] { new DescriptorSetLayoutBinding( binding: 0, descriptorType: DescriptorType.CombinedImageSampler, descriptorCount: 1, stageFlags: ShaderStages.Fragment ) } )); } // Create descriptor pool { DescriptorPool = Graphics.Device.CreateDescriptorPool(new DescriptorPoolCreateInfo( 1, new[] { new DescriptorPoolSize(DescriptorType.CombinedImageSampler, 1) } )); } // Create descriptor set { TextureDescriptorSet = DescriptorPool.AllocateSets(new DescriptorSetAllocateInfo( 1, DescriptorSetLayout ))[0]; var writes = new[] { new WriteDescriptorSet( TextureDescriptorSet, 0, 0, 1, DescriptorType.CombinedImageSampler, imageInfo: new[] { new DescriptorImageInfo( TextureSampler, Texture.Image.ImageView, ImageLayout.ShaderReadOnlyOptimal ) } ) }; DescriptorPool.UpdateSets(writes); } // Create pipeline layout { PipelineLayout = Graphics.Device.CreatePipelineLayout(new PipelineLayoutCreateInfo( setLayouts: new[] { DescriptorSetLayout } )); } // Create graphics pipeline { Pipeline = Graphics.Device.CreateGraphicsPipeline(new GraphicsPipelineCreateInfo( layout: PipelineLayout, renderPass: RenderPass.RenderPass, subpass: 0, stages: new[] { new PipelineShaderStageCreateInfo(ShaderStages.Vertex, VertexShader, "main"), new PipelineShaderStageCreateInfo(ShaderStages.Fragment, FragmentShader, "main") }, inputAssemblyState: new PipelineInputAssemblyStateCreateInfo(PrimitiveTopology.TriangleList), vertexInputState: new PipelineVertexInputStateCreateInfo(), rasterizationState: new PipelineRasterizationStateCreateInfo( polygonMode: PolygonMode.Fill, cullMode: CullModes.Front, frontFace: FrontFace.Clockwise, lineWidth: 1f ), viewportState: new PipelineViewportStateCreateInfo( new Viewport(0f, 0f, Graphics.Window.Size.X, Graphics.Window.Size.Y), new Rect2D(0, 0, (int)Graphics.Window.Size.X, (int)Graphics.Window.Size.Y) ), multisampleState: new PipelineMultisampleStateCreateInfo( rasterizationSamples: SampleCounts.Count1, minSampleShading: 1 ), colorBlendState: new PipelineColorBlendStateCreateInfo( attachments: new[] { new PipelineColorBlendAttachmentState( blendEnable: true, srcColorBlendFactor: BlendFactor.One, dstColorBlendFactor: BlendFactor.Zero, colorBlendOp: BlendOp.Add, srcAlphaBlendFactor: BlendFactor.One, dstAlphaBlendFactor: BlendFactor.Zero, alphaBlendOp: BlendOp.Add, colorWriteMask: ColorComponents.All ) } ) )); } } }