public void BindDescriptorSets(VkPipelineBindPoint pipelineBindPoint, PipelineLayout layout, uint firstSet, DescriptorSet descriptorSet)
 {
     unsafe {
         VkDescriptorSet set = descriptorSet.Native;
         Device.Commands.cmdBindDescriptorSets(commandBuffer, pipelineBindPoint, layout.Native, firstSet, 1, (IntPtr)(&set), 0, IntPtr.Zero);
     }
 }
        void setupDescriptorSet()
        {
            // Scene rendering
            {
                VkDescriptorSetLayout dsl = setLayoutScene;
                var info = VkDescriptorSetAllocateInfo.Alloc();
                info->setLayouts     = dsl;
                info->descriptorPool = descriptorPool;
                VkDescriptorSet set;
                vkAllocateDescriptorSets(device, info, &set);
                this.setScene = set;
            }
            {
                var descriptor0 = uniformBufferScene.descriptor;
                var descriptor1 = textures_gradient.descriptor;
                var writes      = VkWriteDescriptorSet.Alloc(2);
                {
                    // Binding 0: Vertex shader uniform buffer
                    writes[0].dstSet = setScene;
                    writes[0].data.descriptorType = VkDescriptorType.UniformBuffer;
                    writes[0].dstBinding          = 0;
                    writes[0].data.Set(descriptor0);
                    // Binding 1: Color gradient sampler
                    writes[1].dstSet = setScene;
                    writes[1].data.descriptorType = VkDescriptorType.CombinedImageSampler;
                    writes[1].dstBinding          = 1;
                    writes[1].data.Set(descriptor1);
                }
                vkUpdateDescriptorSets(device, 2, writes, 0, null);
            }

            // Fullscreen radial blur
            {
                VkDescriptorSetLayout dsl = setLayoutRadialBlur;
                var info = VkDescriptorSetAllocateInfo.Alloc();
                info->setLayouts     = dsl;
                info->descriptorPool = descriptorPool;
                VkDescriptorSet set;
                vkAllocateDescriptorSets(device, info, &set);
                this.setRadialBlur = set;
            }
            {
                VkDescriptorBufferInfo descriptor0 = uniformBufferBlurParams.descriptor;
                VkDescriptorImageInfo  descriptor1 = offscreenPass.descriptorImage;
                var writes = VkWriteDescriptorSet.Alloc(2);
                {
                    // Binding 0: Vertex shader uniform buffer
                    writes[0].dstSet = setRadialBlur;
                    writes[0].data.descriptorType = VkDescriptorType.UniformBuffer;
                    writes[0].dstBinding          = 0;
                    writes[0].data.Set(descriptor0);
                    // Binding 0: Fragment shader texture sampler
                    writes[1].dstSet = setRadialBlur;
                    writes[1].data.descriptorType = VkDescriptorType.CombinedImageSampler;
                    writes[1].dstBinding          = 1;
                    writes[1].data.Set(descriptor1);
                }
                vkUpdateDescriptorSets(device, 2, writes, 0, null);
            }
        }
Example #3
0
        public void Allocate(VkDescriptorSetLayout setLayout)
        {
            uint *variable_desc_counts = stackalloc uint[1]
            {
                (uint)DeviceLimits.BindingsBindless
            };


            VkDescriptorSetVariableDescriptorCountAllocateInfo variable_descriptor_count_alloc_info = new()
            {
                sType = VkStructureType.DescriptorSetVariableDescriptorCountAllocateInfo,
                descriptorSetCount = 2,
                pDescriptorCounts  = variable_desc_counts,
            };


            bool descriptor_indexing = NativeDevice.supports_descriptor_indexing();

            VkDescriptorSetAllocateInfo descriptor_set_allocate_info = new()
            {
                sType = VkStructureType.DescriptorSetAllocateInfo,
                pNext = descriptor_indexing ? &variable_descriptor_count_alloc_info : null,
                descriptorSetCount = 1,
                pSetLayouts        = &setLayout,
                descriptorPool     = pool,
            };


            VkDescriptorSet descriptor_set;

            vkAllocateDescriptorSets(NativeDevice.handle, &descriptor_set_allocate_info, &descriptor_set).CheckResult();
            handle = descriptor_set;
        }
        protected override void buildCommandBuffers()
        {
            var cmdBufInfo = new VkCommandBufferBeginInfo();

            cmdBufInfo.sType = CommandBufferBeginInfo;

            var clearValues = new VkClearValue[2];

            clearValues[0].color        = defaultClearColor;
            clearValues[1].depthStencil = new VkClearDepthStencilValue(1.0f, 0);

            var info = new VkRenderPassBeginInfo();

            info.sType                    = RenderPassBeginInfo;
            info.renderPass               = renderPass;
            info.renderArea.offset.x      = 0;
            info.renderArea.offset.y      = 0;
            info.renderArea.extent.width  = width;
            info.renderArea.extent.height = height;
            info.clearValues              = clearValues;

            for (int i = 0; i < drawCmdBuffers.Length; ++i)
            {
                info.framebuffer = frameBuffers[i];

                vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo);

                vkCmdBeginRenderPass(drawCmdBuffers[i], &info, VkSubpassContents.Inline);

                var viewport = new VkViewport(0, 0, width, height, 0.0f, 1.0f);
                vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);

                var scissor = new VkRect2D(0, 0, width, height);
                vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);

                vkCmdBindPipeline(drawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipeline);

                VkDeviceSize offsets = 0;
                VkBuffer     buffer  = vertexBuffer.buffer;
                vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &buffer, &offsets);
                vkCmdBindIndexBuffer(drawCmdBuffers[i], indexBuffer.buffer, 0, VkIndexType.Uint32);

                // Render multiple objects using different model matrices by dynamically offsetting into one uniform buffer
                for (uint j = 0; j < OBJECT_INSTANCES; j++)
                {
                    // One dynamic offset per dynamic descriptor to offset into the ubo containing all model matrices
                    uint dynamicOffset = j * (uint)(dynamicAlignment);
                    // Bind the descriptor set for rendering a mesh using the dynamic offset
                    VkDescriptorSet set = descriptorSet;
                    vkCmdBindDescriptorSets(drawCmdBuffers[i], VkPipelineBindPoint.Graphics,
                                            pipelineLayout, 0, 1, &set, 1, &dynamicOffset);

                    vkCmdDrawIndexed(drawCmdBuffers[i], indexCount, 1, 0, 0, 0);
                }

                vkCmdEndRenderPass(drawCmdBuffers[i]);

                vkEndCommandBuffer(drawCmdBuffers[i]);
            }
        }
        public unsafe DescriptorSet(DescriptorPool descriptorPool, uint setCount = 1)
        {
            _device         = descriptorPool.Device;
            _descriptorPool = descriptorPool;

            var layouts = new NativeList <VkDescriptorSetLayout>(setCount);

            for (int i = 0; i < setCount; i++)
            {
                layouts.Add(descriptorPool.Layout.Handle);
            }

            var allocateInfo = new VkDescriptorSetAllocateInfo
            {
                sType              = VkStructureType.DescriptorSetAllocateInfo,
                descriptorPool     = descriptorPool.Handle,
                descriptorSetCount = setCount,
                pSetLayouts        = (VkDescriptorSetLayout *)layouts.Data.ToPointer()
            };

            VkDescriptorSet descriptorSet;

            if (VulkanNative.vkAllocateDescriptorSets(
                    _device.Handle,
                    &allocateInfo,
                    &descriptorSet
                    ) != VkResult.Success)
            {
                throw new Exception("failed to allocate descriptor sets");
            }
            _handle = descriptorSet;
        }
Example #6
0
        void CreateDescriptorSet()
        {
            var layouts = new List <VkDescriptorSetLayout> {
                descriptorSetLayout
            };
            var info = new VkDescriptorSetAllocateInfo();

            info.setLayouts = layouts;

            descriptorSet = descriptorPool.Allocate(info)[0];

            var bufferInfo = new VkDescriptorBufferInfo();

            bufferInfo.buffer = uniformBuffer;
            bufferInfo.offset = 0;
            bufferInfo.range  = Interop.SizeOf <UniformBufferObject>();

            var descriptorWrites = new List <VkWriteDescriptorSet>();

            descriptorWrites.Add(new VkWriteDescriptorSet());
            descriptorWrites[0].dstSet          = descriptorSet;
            descriptorWrites[0].dstBinding      = 0;
            descriptorWrites[0].dstArrayElement = 0;
            descriptorWrites[0].descriptorType  = VkDescriptorType.UniformBuffer;
            descriptorWrites[0].bufferInfo      = new List <VkDescriptorBufferInfo> {
                bufferInfo
            };

            descriptorSet.Update(descriptorWrites, null);
        }
Example #7
0
 public void BindDescriptorSet(Pipeline pipeline, uint set, VkDescriptorSet descriptorSets)
 {
     unsafe
     {
         BindDescriptorSets(pipeline, set, new Span <VkDescriptorSet>(&descriptorSets, 1),
                            new Span <uint>((void *)0, 0));
     }
 }
Example #8
0
        protected override void buildCommandBuffers()
        {
            var cmdInfo = VkCommandBufferBeginInfo.Alloc();

            var clearValues = VkClearValue.Alloc(2);

            clearValues[0].color        = defaultClearColor;
            clearValues[1].depthStencil = new VkClearDepthStencilValue()
            {
                depth = 1.0f, stencil = 0
            };

            var info = VkRenderPassBeginInfo.Alloc();

            info[0].renderPass               = renderPass;
            info[0].renderArea.offset.x      = 0;
            info[0].renderArea.offset.y      = 0;
            info[0].renderArea.extent.width  = width;
            info[0].renderArea.extent.height = height;
            info[0].clearValues.count        = 2;
            info[0].clearValues.pointer      = clearValues;

            for (int i = 0; i < drawCmdBuffers.Length; ++i)
            {
                // Set target frame buffer
                info[0].framebuffer = frameBuffers[i];

                vkBeginCommandBuffer(drawCmdBuffers[i], cmdInfo);

                vkCmdBeginRenderPass(drawCmdBuffers[i], info, VkSubpassContents.Inline);

                VkViewport viewport = new VkViewport((float)width, (float)height, 0.0f, 1.0f);
                vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);

                VkRect2D scissor = new VkRect2D(0, 0, width, height);
                vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
                {
                    VkDescriptorSet set = descriptorSet;
                    vkCmdBindDescriptorSets(drawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipelineLayout,
                                            0, 1, &set, 0, null);
                }
                vkCmdBindPipeline(drawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipeline);

                {
                    VkDeviceSize offsets = 0;
                    VkBuffer     buffer  = vertexBuffer.buffer;
                    vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1,
                                           &buffer, &offsets);
                }
                vkCmdBindIndexBuffer(drawCmdBuffers[i], indexBuffer.buffer, 0, VkIndexType.Uint32);

                vkCmdDrawIndexed(drawCmdBuffers[i], indexCount, 1, 0, 0, 0);

                vkCmdEndRenderPass(drawCmdBuffers[i]);

                vkEndCommandBuffer(drawCmdBuffers[i]);
            }
        }
Example #9
0
 public void BindDescriptorSet(Pipeline pipeline, uint set, VkDescriptorSet descriptorSets,
                               Span <uint> pDynamicOffsets)
 {
     unsafe
     {
         BindDescriptorSets(pipeline, set, new Span <VkDescriptorSet>(&descriptorSets, 1),
                            pDynamicOffsets);
     }
 }
Example #10
0
 public unsafe VkWriteDescriptorSet(uint binding, VkDescriptorSet dstSet, VkDescriptorType type,
                                    ref VkDescriptorBufferInfo bufferInfo, uint descriptorCount = 1)
 {
     this.sType            = VkStructureType.WriteDescriptorSet;
     this.pNext            = null;
     this.dstSet           = dstSet;
     this.descriptorType   = type;
     this.dstBinding       = binding;
     this.pBufferInfo      = (VkDescriptorBufferInfo *)Unsafe.AsPointer(ref bufferInfo);
     this.descriptorCount  = descriptorCount;
     this.pImageInfo       = null;
     this.pTexelBufferView = null;
     this.dstArrayElement  = 0;
 }
            internal void Free(VkDevice device, DescriptorAllocationToken token, DescriptorResourceCounts counts)
            {
                VkDescriptorSet set = token.Set;

                vkFreeDescriptorSets(device, Pool, 1, ref set);

                RemainingSets += 1;

                UniformBufferCount += counts.UniformBufferCount;
                SampledImageCount  += counts.SampledImageCount;
                SamplerCount       += counts.SamplerCount;
                StorageBufferCount += counts.StorageBufferCount;
                StorageImageCount  += counts.StorageImageCount;
            }
Example #12
0
 public unsafe VkWriteDescriptorSet(uint binding,
                                    VkDescriptorSet dstSet,
                                    VkDescriptorType type, VkWriteDescriptorSetInlineUniformBlockEXT *inlineUniformBlockEXT)
 {
     this.sType            = VkStructureType.WriteDescriptorSet;
     this.pNext            = inlineUniformBlockEXT;
     this.dstSet           = dstSet;
     this.descriptorType   = type;
     this.dstBinding       = binding;
     this.descriptorCount  = inlineUniformBlockEXT->dataSize;
     this.pBufferInfo      = null;
     this.pTexelBufferView = null;
     this.pImageInfo       = null;
     this.dstArrayElement  = 0;
 }
Example #13
0
        public static void SetDebugMarkerName(this VkDescriptorSet obj, Device dev, string name)
        {
            if (!dev.DebugMarkersEnabled)
            {
                return;
            }
            VkDebugMarkerObjectNameInfoEXT dmo = new VkDebugMarkerObjectNameInfoEXT(VkDebugReportObjectTypeEXT.DescriptorSetEXT,
                                                                                    obj.Handle)
            {
                pObjectName = name.Pin()
            };

            Utils.CheckResult(vkDebugMarkerSetObjectNameEXT(dev.VkDev, ref dmo));
            name.Unpin();
        }
Example #14
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);
        }
Example #15
0
        public void BindDescriptorSets(DescriptorSet descriptor, uint dynamicOffsetCount = 0, uint dynamicOffsets = 0)
        {
            // Bind descriptor sets describing shader binding points
            VkDescriptorSet  descriptor_set  = descriptor._descriptorSet;
            VkPipelineLayout pipeline_layout = descriptor._pipelineLayout;

            for (int i = 0; i < descriptor.DescriptorData.Data.Count; i++)
            {
                ResourceData data = descriptor.DescriptorData.Data[i];
                if (data.DescriptorType == VkDescriptorType.StorageImage)
                {
                }
            }

            vkCmdBindDescriptorSets(handle, descriptor.BindPoint, pipeline_layout, 0, 1, &descriptor_set, dynamicOffsetCount, &dynamicOffsets);
        }
Example #16
0
        void UpdateDescriptorSets(VkDevice device, VkBuffer uniformBuffer, VkDescriptorSet descriptorSet)
        {
            var write = new VkWriteDescriptorSet {
                sType = VkStructureType.WriteDescriptorSet
            };

            write.dstSet = descriptorSet;
            write.data.descriptorType = VkDescriptorType.UniformBuffer;
            var info = new VkDescriptorBufferInfo(uniformBuffer, 0, 2 * sizeof(float));

            write.data.Set(info);

            //device.UpdateDescriptorSets(new VkWriteDescriptorSet[] { write }, null);
            vkAPI.vkUpdateDescriptorSets(device, 1, &write, 0, null);

            write.Free();
        }
Example #17
0
        void CreateDescriptorSet()
        {
            var layouts = new List <VkDescriptorSetLayout> {
                descriptorSetLayout
            };
            var info = new VkDescriptorSetAllocateInfo();

            info.setLayouts = layouts;

            descriptorSet = descriptorPool.Allocate(info)[0];

            var bufferInfo = new VkDescriptorBufferInfo();

            bufferInfo.buffer = uniformBuffer;
            bufferInfo.offset = 0;
            bufferInfo.range  = Interop.SizeOf <UniformBufferObject>();

            var imageInfo = new VkDescriptorImageInfo();

            imageInfo.imageLayout = VkImageLayout.ShaderReadOnlyOptimal;
            imageInfo.imageView   = textureImageView;
            imageInfo.sampler     = textureSampler;

            var descriptorWrites = new List <VkWriteDescriptorSet>();

            descriptorWrites.Add(new VkWriteDescriptorSet());
            descriptorWrites[0].dstSet          = descriptorSet;
            descriptorWrites[0].dstBinding      = 0;
            descriptorWrites[0].dstArrayElement = 0;
            descriptorWrites[0].descriptorType  = VkDescriptorType.UniformBuffer;
            descriptorWrites[0].bufferInfo      = new List <VkDescriptorBufferInfo> {
                bufferInfo
            };

            descriptorWrites.Add(new VkWriteDescriptorSet());
            descriptorWrites[1].dstSet          = descriptorSet;
            descriptorWrites[1].dstBinding      = 1;
            descriptorWrites[1].dstArrayElement = 0;
            descriptorWrites[1].descriptorType  = VkDescriptorType.CombinedImageSampler;
            descriptorWrites[1].imageInfo       = new List <VkDescriptorImageInfo> {
                imageInfo
            };

            descriptorSet.Update(descriptorWrites, null);
        }
        void setupDescriptorSet()
        {
            var allocInfo = new VkDescriptorSetAllocateInfo();

            allocInfo.sType          = DescriptorSetAllocateInfo;
            allocInfo.descriptorPool = descriptorPool;
            allocInfo.setLayouts     = descriptorSetLayout;

            VkDescriptorSet set;

            vkAllocateDescriptorSets(device, &allocInfo, &set);
            this.descriptorSet = set;

            VkDescriptorBufferInfo descriptor0 = uniformBuffers_view.descriptor;
            VkDescriptorBufferInfo descriptor1 = uniformBuffers_dynamic.descriptor;
            var writeDescriptorSets            = new VkWriteDescriptorSet[2];

            {
                // Binding 0 : Projection/View matrix uniform buffer
                var a = new VkWriteDescriptorSet()
                {
                    sType      = WriteDescriptorSet,
                    dstSet     = descriptorSet,
                    dstBinding = 0,
                };
                a.data.descriptorType = VkDescriptorType.UniformBuffer;
                a.data.Set(descriptor0);
                writeDescriptorSets[0] = a;
                // Binding 1 : Instance matrix as dynamic uniform buffer
                var b = new VkWriteDescriptorSet()
                {
                    sType      = WriteDescriptorSet,
                    dstSet     = descriptorSet,
                    dstBinding = 1,
                };
                b.data.descriptorType = VkDescriptorType.UniformBufferDynamic;
                b.data.Set(descriptor1);
                writeDescriptorSets[1] = b;
            };

            fixed(VkWriteDescriptorSet *pointer = writeDescriptorSets)
            {
                vkUpdateDescriptorSets(device, (UInt32)writeDescriptorSets.Length, pointer, 0, null);
            }
        }
Example #19
0
        void setupDescriptorSet()
        {
            VkDescriptorSetLayout       dsl       = descriptorSetLayout;
            VkDescriptorSetAllocateInfo allocInfo = new VkDescriptorSetAllocateInfo();

            allocInfo.sType          = DescriptorSetAllocateInfo;
            allocInfo.descriptorPool = descriptorPool;
            allocInfo.setLayouts     = dsl;

            {
                VkDescriptorSet set;
                vkAllocateDescriptorSets(device, &allocInfo, &set);
                this.descriptorSet = set;
            }

            VkDescriptorImageInfo texDescriptor = new VkDescriptorImageInfo();

            texDescriptor.sampler     = textures_colorMap.sampler;
            texDescriptor.imageView   = textures_colorMap.view;
            texDescriptor.imageLayout = VkImageLayout.General;

            VkDescriptorBufferInfo temp = uniformBuffers_scene.descriptor;
            var writes = new VkWriteDescriptorSet[2];

            // Binding 0 : Vertex shader uniform buffer
            writes[0]        = new VkWriteDescriptorSet();
            writes[0].sType  = WriteDescriptorSet;
            writes[0].dstSet = descriptorSet;
            writes[0].data.descriptorType = VkDescriptorType.UniformBuffer;
            writes[0].dstBinding          = 0;
            writes[0].data.Set(temp);
            // Binding 1 : Color map
            writes[1]        = new VkWriteDescriptorSet();
            writes[1].sType  = WriteDescriptorSet;
            writes[1].dstSet = descriptorSet;
            writes[1].data.descriptorType = VkDescriptorType.CombinedImageSampler;
            writes[1].dstBinding          = 1;
            writes[1].data.Set(texDescriptor);

            fixed(VkWriteDescriptorSet *pointer = writes)
            {
                vkUpdateDescriptorSets(device, (UInt32)writes.Length, pointer, 0, null);
            }
        }
        protected override void RecordCommandBuffer(VkCommandBuffer cmdBuffer, int imageIndex)
        {
            VkClearValue *clearValues = stackalloc VkClearValue[2];

            clearValues[0] =
                new VkClearValue
            {
                color = new VkClearColorValue(0.39f, 0.58f, 0.93f, 1.0f)
            };
            clearValues[1]
                = new VkClearValue
                {
                depthStencil = new VkClearDepthStencilValue(1.0f, 0)
                };
            VkRenderPassBeginInfo renderPassBeginInfo = new VkRenderPassBeginInfo
            {
                sType           = VkStructureType.RenderPassBeginInfo,
                pNext           = null,
                framebuffer     = _framebuffers[imageIndex],
                renderArea      = new Vortice.Mathematics.Rectangle(0, 0, Host.Width, Host.Height),
                clearValueCount = 2,
                pClearValues    = clearValues,
                renderPass      = _renderPass
            };

            vkCmdBeginRenderPass(cmdBuffer, &renderPassBeginInfo, VkSubpassContents.Inline);

            VkDescriptorSet descriptorSet = _descriptorSet;

            vkCmdBindDescriptorSets(cmdBuffer, VkPipelineBindPoint.Graphics, _pipelineLayout, 0, 1, &descriptorSet);

            ulong *offsets = stackalloc ulong[1]
            {
                0
            };

            vkCmdBindPipeline(cmdBuffer, VkPipelineBindPoint.Graphics, _pipeline);
            VkBuffer buffer = _cubeVertices.Buffer;

            vkCmdBindVertexBuffers(cmdBuffer, 0, 1, &buffer, offsets);
            vkCmdBindIndexBuffer(cmdBuffer, _cubeIndices.Buffer, 0, VkIndexType.Uint32);
            vkCmdDrawIndexed(cmdBuffer, (uint)_cubeIndices.Count, 1, 0, 0, 0);
            vkCmdEndRenderPass(cmdBuffer);
        }
        public void BindDescriptorSets(VkPipelineBindPoint pipelineBindPoint, PipelineLayout layout, uint firstSet, DescriptorSet descriptorSet, List <uint> dynamicOffsets)
        {
            unsafe
            {
                int dynamicOffsetCount = 0;
                if (dynamicOffsets != null)
                {
                    dynamicOffsetCount = dynamicOffsets.Count;
                }

                var offsets = stackalloc uint[dynamicOffsetCount];

                VkDescriptorSet setNative = descriptorSet.Native;
                Interop.Copy(dynamicOffsets, (IntPtr)offsets);

                Device.Commands.cmdBindDescriptorSets(commandBuffer, VkPipelineBindPoint.Graphics, layout.Native,
                                                      firstSet, 1, (IntPtr)(&setNative),
                                                      (uint)dynamicOffsetCount, (IntPtr)offsets);
            }
        }
        private VkDescriptorSet CreateVulkanDescriptorSet()
        {
            VkDescriptorSet vulkanDescriptorSet  = VK_NULL_HANDLE;
            var             vulkanDescriptorPool = VulkanDescriptorPool;

            if (vulkanDescriptorPool != VK_NULL_HANDLE)
            {
                var vulkanDescriptorSetLayout = VulkanDescriptorSetLayout;

                var descriptorSetAllocateInfo = new VkDescriptorSetAllocateInfo {
                    sType              = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
                    descriptorPool     = vulkanDescriptorPool,
                    descriptorSetCount = 1,
                    pSetLayouts        = (ulong *)&vulkanDescriptorSetLayout,
                };
                ThrowExternalExceptionIfNotSuccess(vkAllocateDescriptorSets(Device.VulkanDevice, &descriptorSetAllocateInfo, (ulong *)&vulkanDescriptorSet), nameof(vkAllocateDescriptorSets));
            }

            return(vulkanDescriptorSet);
        }
Example #23
0
        private static VkDescriptorSet[] AllocateDescriptorSets(VkDevice device, VkDescriptorSetLayout layout, VkDescriptorPool pool, uint swapchainImageCount)
        {
            VkDescriptorSetLayout[] localLayouts = new VkDescriptorSetLayout[swapchainImageCount];
            for (int i = 0; i < localLayouts.Length; i++)
            {
                localLayouts[i] = layout;
            }
            VkDescriptorSetAllocateInfo allocateInfo = VkDescriptorSetAllocateInfo.New();

            allocateInfo.descriptorPool     = pool;
            allocateInfo.descriptorSetCount = swapchainImageCount;

            fixed(VkDescriptorSetLayout *ptr = localLayouts)
            allocateInfo.pSetLayouts = ptr;

            VkDescriptorSet[] sets = new VkDescriptorSet[swapchainImageCount];

            fixed(VkDescriptorSet *ptr = sets)
            Assert(vkAllocateDescriptorSets(device, &allocateInfo, ptr));

            return(sets);
        }
        protected override void InitializePermanent()
        {
            var cube = GeometricPrimitive.Box(1.0f, 1.0f, 1.0f);

            _cubeTexture  = Content.LoadVulkanImage("IndustryForgedDark512.ktx");
            _cubeVertices = ToDispose(VulkanBuffer.Vertex(Context, cube.Vertices));
            _cubeIndices  = ToDispose(VulkanBuffer.Index(Context, cube.Indices));
            var sampler = CreateSampler();

            _sampler = sampler;
            ToDispose(new ActionDisposable(() =>
            {
                vkDestroySampler(Context.Device, sampler, null);
            }));
            _uniformBuffer = ToDispose(VulkanBuffer.DynamicUniform <WorldViewProjection>(Context, 1));
            var descriptorSetLayout = CreateDescriptorSetLayout();

            _descriptorSetLayout = descriptorSetLayout;
            ToDispose(new ActionDisposable(() =>
            {
                vkDestroyDescriptorSetLayout(Context.Device, descriptorSetLayout, null);
            }));
            var pipelineLayout = CreatePipelineLayout();

            _pipelineLayout = pipelineLayout;
            ToDispose(new ActionDisposable(() =>
            {
                vkDestroyPipelineLayout(Context.Device, pipelineLayout, null);
            }));
            var descriptorPool = CreateDescriptorPool();

            _descriptorPool = descriptorPool;
            ToDispose(new ActionDisposable(() =>
            {
                vkDestroyDescriptorPool(Context.Device, descriptorPool, null);
            }));
            _descriptorSet = CreateDescriptorSet(_descriptorPool); // Will be freed when pool is destroyed.
        }
Example #25
0
        void setupDescriptorSet()
        {
            VkDescriptorSetLayout layout = this.layout;
            var allocInfo = VkDescriptorSetAllocateInfo.Alloc();

            allocInfo[0].descriptorPool = descriptorPool;
            allocInfo[0].setLayouts     = layout;
            {
                VkDescriptorSet set;
                vkAllocateDescriptorSets(device, allocInfo, &set);
                this.descriptorSet = set;
            }

            // Setup a descriptor image info for the current texture to be used as a combined image sampler
            VkDescriptorImageInfo imageInfo;

            imageInfo.imageView   = texture.view;           // The image's view (images are never directly accessed by the shader, but rather through views defining subresources)
            imageInfo.sampler     = texture.sampler;        //  The sampler (Telling the pipeline how to sample the texture, including repeat, border, etc.)
            imageInfo.imageLayout = texture.imageLayout;    //  The current layout of the image (Note: Should always fit the actual use, e.g. shader read)

            VkDescriptorBufferInfo bufferInfo = uniformBufferVS.descriptor;
            var writes = VkWriteDescriptorSet.Alloc(2);

            {
                // Binding 0 : Vertex shader uniform buffer
                writes[0].dstSet              = descriptorSet;
                writes[0].dstBinding          = 0;
                writes[0].data.descriptorType = VkDescriptorType.UniformBuffer;
                writes[0].data.Set(bufferInfo);
                // Binding 1 : Fragment shader texture sampler
                //  Fragment shader: layout (binding = 1) uniform sampler2D samplerColor;
                writes[1].dstSet              = descriptorSet;
                writes[1].dstBinding          = 1;
                writes[1].data.descriptorType = VkDescriptorType.CombinedImageSampler;
                writes[1].data.Set(imageInfo);
            }
            vkUpdateDescriptorSets(device, 2, writes, 0, null);
        }
Example #26
0
        public override void Draw(uint indexCount, uint instanceCount, uint indexStart, int vertexOffset, uint instanceStart)
        {
            // TODO: This should only call vkCmdBindDescriptorSets once, or rather, only the ones that changed.
            for (int i = 0; i < _resourceSetsChanged.Length; i++)
            {
                if (_resourceSetsChanged[i])
                {
                    _resourceSetsChanged[i] = false;
                    VkDescriptorSet ds = _currentResourceSets[i].DescriptorSet;
                    vkCmdBindDescriptorSets(
                        _cb,
                        VkPipelineBindPoint.Graphics,
                        _currentPipeline.PipelineLayout,
                        (uint)i,
                        1,
                        ref ds,
                        0,
                        null);
                }
            }

            vkCmdDrawIndexed(_cb, indexCount, instanceCount, indexStart, vertexOffset, instanceStart);
        }
Example #27
0
        VkCommandBuffer[] CreateCommandBuffers(
            VkDevice device, VkRenderPass renderPass, VkSurfaceCapabilitiesKHR surfaceCapabilities,
            VkImage[] images, VkFramebuffer[] framebuffers, VkPipeline pipeline,
            VkBuffer vertexBuffer, VkBuffer indexBuffer, uint indexLength,
            VkPipelineLayout pipelineLayout, VkDescriptorSet descriptorSet)
        {
            VkCommandBuffer[] buffers;
            {
                VkCommandPool pool;
                {
                    var info = new VkCommandPoolCreateInfo {
                        sType = VkStructureType.CommandPoolCreateInfo
                    };
                    info.flags = VkCommandPoolCreateFlagBits.ResetCommandBuffer;
                    //var commandPool = device.CreateCommandPool(ref poolInfo);
                    vkCreateCommandPool(device, &info, null, &pool).Check();
                }
                {
                    var info = new VkCommandBufferAllocateInfo {
                        sType = VkStructureType.CommandBufferAllocateInfo
                    };
                    info.level              = VkCommandBufferLevel.Primary;
                    info.commandPool        = pool;
                    info.commandBufferCount = (uint)images.Length;
                    //buffers = device.AllocateCommandBuffers(ref info);
                    buffers = new VkCommandBuffer[info.commandBufferCount];
                    fixed(VkCommandBuffer *pointer = buffers)
                    {
                        vkAPI.vkAllocateCommandBuffers(device, &info, pointer).Check();
                    }
                }
            }

            var cmdBeginInfo = new VkCommandBufferBeginInfo {
                sType = VkStructureType.CommandBufferBeginInfo
            };
            var clearValue = new VkClearValue {
                color = new VkClearColorValue(0.9f, 0.87f, 0.75f, 1.0f)
            };
            var begin = new VkRenderPassBeginInfo {
                sType = VkStructureType.RenderPassBeginInfo
            };

            begin.renderPass  = renderPass;
            begin.clearValues = clearValue;
            begin.renderArea  = new VkRect2D {
                extent = surfaceCapabilities.currentExtent
            };
            for (int i = 0; i < images.Length; i++)
            {
                VkCommandBuffer cmds = buffers[i];
                //cmds.Begin(ref cmdBeginInfo);
                vkAPI.vkBeginCommandBuffer(cmds, &cmdBeginInfo).Check();
                begin.framebuffer = framebuffers[i];
                vkAPI.vkCmdBeginRenderPass(cmds, &begin, VkSubpassContents.Inline);
                vkAPI.vkCmdBindDescriptorSets(cmds, VkPipelineBindPoint.Graphics, pipelineLayout,
                                              0, 1, &descriptorSet,
                                              0, null);
                vkAPI.vkCmdBindPipeline(cmds, VkPipelineBindPoint.Graphics, pipeline);
                VkDeviceSize offset = 0;
                vkAPI.vkCmdBindVertexBuffers(cmds, 0, 1, &vertexBuffer, &offset);
                vkAPI.vkCmdBindIndexBuffer(cmds, indexBuffer, offset, VkIndexType.Uint16);
                vkAPI.vkCmdDrawIndexed(cmds, indexLength, 1, 0, 0, 0);
                vkAPI.vkCmdEndRenderPass(cmds);
                vkAPI.vkEndCommandBuffer(cmds).Check();
            }

            begin.Free();

            return(buffers);
        }
Example #28
0
        public void Init(IntPtr hwnd, IntPtr processHandle)
        {
            if (this.isInitialized)
            {
                return;
            }

            this.instance = InitInstance();
            InitDebugCallback(this.instance);
            this.surface          = InitSurface(this.instance, hwnd, processHandle);
            this.vkPhysicalDevice = InitPhysicalDevice(this.instance);
            VkSurfaceFormatKHR surfaceFormat = SelectFormat(this.vkPhysicalDevice, this.surface);

            this.device = CreateDevice(this.vkPhysicalDevice, this.surface);

            this.vkQueue = this.device.GetQueue(0, 0);

            VkSurfaceCapabilitiesKHR surfaceCapabilities;

            //this.vkPhysicalDevice.GetSurfaceCapabilitiesKhr(this.vkSurface, out surfaceCapabilities);
            vkAPI.vkGetPhysicalDeviceSurfaceCapabilitiesKHR(this.vkPhysicalDevice, this.surface, &surfaceCapabilities).Check();

            this.swapchain = CreateSwapchain(this.device, this.surface, surfaceFormat, surfaceCapabilities);

            this.vkImages = this.device.GetSwapchainImages(this.swapchain);

            this.renderPass = CreateRenderPass(this.device, surfaceFormat);

            this.framebuffers = CreateFramebuffers(this.device, this.vkImages, surfaceFormat, this.renderPass, surfaceCapabilities);

            this.vkFence     = this.device.CreateFence();
            this.vkSemaphore = this.device.CreateSemaphore();

            // buffers for vertex data.
            VkBuffer vertexBuffer = CreateBuffer(this.vkPhysicalDevice, this.device, Vertices, VkBufferUsageFlagBits.VertexBuffer, typeof(float));

            VkBuffer indexBuffer = CreateBuffer(this.vkPhysicalDevice, this.device, Indexes, VkBufferUsageFlagBits.IndexBuffer, typeof(short));

            var uniformBufferData = new AreaUniformBuffer(1, 1);

            this.originalWidth  = 1; this.width = this.originalWidth;
            this.originalHeight = 1; this.height = this.originalHeight;

            this.uniformBuffer = CreateBuffer(this.vkPhysicalDevice, this.device, uniformBufferData, VkBufferUsageFlagBits.UniformBuffer, typeof(AreaUniformBuffer));

            this.descriptorSetLayout = CreateDescriptorSetLayout(this.device);

            this.vkPipelineLayout = CreatePipelineLayout(this.device, this.descriptorSetLayout);

            VkPipeline pipeline = CreatePipeline(this.device, surfaceCapabilities, this.renderPass, this.vkPipelineLayout);

            this.descriptorSet = CreateDescriptorSet(this.device, this.descriptorSetLayout);

            UpdateDescriptorSets(this.device, this.uniformBuffer, this.descriptorSet);

            this.commandBuffers = CreateCommandBuffers(
                this.device, this.renderPass, surfaceCapabilities,
                this.vkImages, this.framebuffers, pipeline,
                vertexBuffer, indexBuffer, (uint)Indexes.Length,
                this.vkPipelineLayout, this.descriptorSet);

            this.isInitialized = true;
        }
 public DescriptorAllocationToken(VkDescriptorSet set, VkDescriptorPool pool)
 {
     Set  = set;
     Pool = pool;
 }
Example #30
0
 public static VkResult vkFreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, VkDescriptorSet descriptorSet)
 {
     return(vkFreeDescriptorSets(device, descriptorPool, 1, &descriptorSet));
 }