예제 #1
0
        void setupDescriptorSetLayout()
        {
            var bindings = VkDescriptorSetLayoutBinding.Alloc(2);
            {
                // Binding 0 : Vertex shader uniform buffer
                bindings[0].binding         = 0;
                bindings[0].descriptorType  = VkDescriptorType.UniformBuffer;
                bindings[0].descriptorCount = 1;
                bindings[0].stageFlags      = VkShaderStageFlagBits.Vertex;
                // Binding 1 : Fragment shader image sampler
                bindings[1].binding         = 1;
                bindings[1].descriptorType  = VkDescriptorType.CombinedImageSampler;
                bindings[1].descriptorCount = 1;
                bindings[1].stageFlags      = VkShaderStageFlagBits.Fragment;
            }

            {
                var info = VkDescriptorSetLayoutCreateInfo.Alloc();
                info[0].bindings.count   = 2;
                info[0].bindings.pointer = bindings;
                VkDescriptorSetLayout layout;
                vkCreateDescriptorSetLayout(device, info, null, &layout);
                this.layout = layout;
            }

            {
                VkDescriptorSetLayout layout = this.layout;
                var info = VkPipelineLayoutCreateInfo.Alloc();
                info[0].setLayouts = layout;
                VkPipelineLayout pipelineLayout;
                vkCreatePipelineLayout(device, info, null, &pipelineLayout);
                this.pipelineLayout = pipelineLayout;
            }
        }
        void setupDescriptorSetLayout()
        {
            var bindings = new VkDescriptorSetLayoutBinding[] {
                // layout (binding = 0) uniform UboView
                new VkDescriptorSetLayoutBinding(0, VkDescriptorType.UniformBuffer, 1, VkShaderStageFlagBits.Vertex),
                // layout (binding = 1) uniform UboInstance
                new VkDescriptorSetLayoutBinding(1, VkDescriptorType.UniformBufferDynamic, 1, VkShaderStageFlagBits.Vertex),
                // no matching uniform sampler2D in shader.
                new VkDescriptorSetLayoutBinding(2, VkDescriptorType.CombinedImageSampler, 1, VkShaderStageFlagBits.Fragment),
            };

            var descriptorLayoutInfo = new VkDescriptorSetLayoutCreateInfo();

            descriptorLayoutInfo.sType    = DescriptorSetLayoutCreateInfo;
            descriptorLayoutInfo.bindings = bindings;

            VkDescriptorSetLayout layout;

            vkCreateDescriptorSetLayout(device, &descriptorLayoutInfo, null, &layout);
            this.descriptorSetLayout = layout;

            VkPipelineLayoutCreateInfo info = new VkPipelineLayoutCreateInfo();

            info.sType      = PipelineLayoutCreateInfo;
            info.setLayouts = layout;

            VkPipelineLayout pipelineLayout;

            vkCreatePipelineLayout(device, &info, null, &pipelineLayout);
            this.pipelineLayout = pipelineLayout;
        }
예제 #3
0
 public static extern void CmdPushConstants(
     VkCommandBuffer commandBuffer,
     VkPipelineLayout layout,
     VkShaderStageFlags stageFlags,
     uint offset,
     uint size,
     IntPtr pValues
     );
예제 #4
0
        /// <summary>
        /// パイプラインレイアウトを構築します。
        /// </summary>
        /// <returns></returns>
        private VkPipelineLayout CreatePipelineLayout(VkDevice device)
        {
            var createInfo          = new VkPipelineLayoutCreateInfo();
            VkPipelineLayout layout = null;

            VulkanAPI.vkCreatePipelineLayout(device, ref createInfo, out layout);
            return(layout);
        }
예제 #5
0
        public void CmdBindDescriptorSets(VkPipelineBindPoint pipelineBindPoint, VkPipelineLayout layout, int firstSet, int descriptorSetCount, VkDescriptorSet[] pDescriptorSets, int dynamicOffsetCount, int[] pDynamicOffsets)
        {
            if (m_State != CommandBufferState.Recording)
            {
                return;
            }

            m_Commands.Add(new Cmd_BindDescriptorSets(pipelineBindPoint, layout, firstSet, descriptorSetCount, pDescriptorSets, dynamicOffsetCount, pDynamicOffsets));
        }
예제 #6
0
 public Cmd_BindDescriptorSets(VkPipelineBindPoint pipelineBindPoint, VkPipelineLayout layout, int firstSet, int descriptorSetCount, VkDescriptorSet[] pDescriptorSets, int dynamicOffsetCount, int[] pDynamicOffsets)
 {
     this.pipelineBindPoint  = pipelineBindPoint;
     this.layout             = layout;
     this.firstSet           = firstSet;
     this.descriptorSetCount = descriptorSetCount;
     this.pDescriptorSets    = pDescriptorSets;
     this.dynamicOffsetCount = dynamicOffsetCount;
     this.pDynamicOffsets    = pDynamicOffsets;
 }
예제 #7
0
 public static extern void CmdBindDescriptorSets(
     VkCommandBuffer commandBuffer,
     VkPipelineBindPoint pipelineBindPoint,
     VkPipelineLayout layout,
     uint firstSet,
     uint descriptorSetCount,
     IntPtr pDescriptorSets,
     uint dynamicOffsetCount,
     IntPtr pDynamicOffsets
     );
예제 #8
0
        /// <summary>
        /// パイプラインレイアウトを構築します。
        /// </summary>
        /// <returns></returns>
        private VkPipelineLayout CreatePipelineLayout(VkDevice device)
        {
            var createInfo = new VkPipelineLayoutCreateInfo();

            createInfo.setLayouts = new[] { m_descriptorSetLayout };
            VkPipelineLayout layout = null;

            VulkanAPI.vkCreatePipelineLayout(device, ref createInfo, out layout);
            return(layout);
        }
        public VkPipelineLayout CreatePipelineLayout(VkDevice device, VkDescriptorSetLayout descSetLayout)
        {
            var createInfo = new VkPipelineLayoutCreateInfo();

            createInfo.setLayouts = new[] { descSetLayout };
            VkPipelineLayout layout = null;

            VulkanAPI.vkCreatePipelineLayout(device, ref createInfo, out layout);
            m_pipelineLayouts.Add(layout);
            return(layout);
        }
예제 #10
0
 public Pipeline(
     Device device,
     RenderPass renderPass,
     VkPipelineLayout layout,
     VkPipeline handle
     )
 {
     _device     = device;
     _renderPass = renderPass;
     _layout     = layout;
     _handle     = handle;
 }
예제 #11
0
        static VkPipelineLayout CreatePipelineLayout(VkDevice device, VkDescriptorSetLayout setLayout)
        {
            VkPipelineLayoutCreateInfo pCreateInfo = VkPipelineLayoutCreateInfo.New();

            pCreateInfo.pushConstantRangeCount = 0;
            pCreateInfo.setLayoutCount         = 1;
            pCreateInfo.pSetLayouts            = &setLayout;

            VkPipelineLayout layout = VkPipelineLayout.Null;

            Assert(vkCreatePipelineLayout(device, &pCreateInfo, null, &layout));
            return(layout);
        }
예제 #12
0
파일: Initializers.cs 프로젝트: gomson/vk
        public static VkGraphicsPipelineCreateInfo pipelineCreateInfo(
            VkPipelineLayout layout,
            VkRenderPass renderPass,
            VkPipelineCreateFlags flags = 0)
        {
            VkGraphicsPipelineCreateInfo pipelineCreateInfo = VkGraphicsPipelineCreateInfo.New();

            pipelineCreateInfo.layout             = layout;
            pipelineCreateInfo.renderPass         = renderPass;
            pipelineCreateInfo.flags              = flags;
            pipelineCreateInfo.basePipelineIndex  = -1;
            pipelineCreateInfo.basePipelineHandle = new VkPipeline();
            return(pipelineCreateInfo);
        }
예제 #13
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);
        }
예제 #14
0
 public static void vkCmdBindDescriptorSets(
     VkCommandBuffer commandBuffer,
     VkPipelineBindPoint pipelineBindPoint,
     VkPipelineLayout layout,
     uint firstSet,
     ReadOnlySpan <VkDescriptorSet> descriptorSets,
     ReadOnlySpan <uint> dynamicOffsets)
 {
     fixed(VkDescriptorSet *descriptorSetsPtr = descriptorSets)
     {
         fixed(uint *dynamicOffsetsPtr = dynamicOffsets)
         {
             vkCmdBindDescriptorSets(commandBuffer, pipelineBindPoint, layout, firstSet, (uint)descriptorSets.Length, descriptorSetsPtr, (uint)dynamicOffsets.Length, dynamicOffsetsPtr);
         }
     }
 }
예제 #15
0
        private void FlushNewResourceSets(
            VkResourceSet[] resourceSets,
            bool[] resourceSetsChanged,
            VkPipelineBindPoint bindPoint,
            VkPipelineLayout pipelineLayout)
        {
            int setCount = resourceSets.Length;
            VkDescriptorSet *descriptorSets       = stackalloc VkDescriptorSet[setCount];
            uint             currentBatchCount    = 0;
            uint             currentBatchFirstSet = 0;

            for (uint currentSlot = 0; currentSlot < resourceSets.Length; currentSlot++)
            {
                bool batchEnded = !resourceSetsChanged[currentSlot] || currentSlot == resourceSets.Length - 1;

                if (resourceSetsChanged[currentSlot])
                {
                    resourceSetsChanged[currentSlot]  = false;
                    descriptorSets[currentBatchCount] = resourceSets[currentSlot].DescriptorSet;
                    currentBatchCount += 1;
                }

                if (batchEnded)
                {
                    if (currentBatchCount != 0)
                    {
                        // Flush current batch.
                        vkCmdBindDescriptorSets(
                            _cb,
                            bindPoint,
                            pipelineLayout,
                            currentBatchFirstSet,
                            currentBatchCount,
                            descriptorSets,
                            0,
                            null);
                    }

                    currentBatchCount    = 0;
                    currentBatchFirstSet = currentSlot + 1;
                }
            }
        }
        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.
        }
예제 #17
0
        void setupDescriptorSetLayout()
        {
            var bindings = new VkDescriptorSetLayoutBinding[2];

            // Binding 0 : Vertex shader uniform buffer
            bindings[0] = new VkDescriptorSetLayoutBinding();
            bindings[0].descriptorType  = VkDescriptorType.UniformBuffer;
            bindings[0].stageFlags      = VkShaderStageFlagBits.Vertex;
            bindings[0].binding         = 0;
            bindings[0].descriptorCount = 1;
            // Binding 1 : Fragment shader combined sampler
            bindings[1] = new VkDescriptorSetLayoutBinding();
            bindings[1].descriptorType  = VkDescriptorType.CombinedImageSampler;
            bindings[1].stageFlags      = VkShaderStageFlagBits.Fragment;
            bindings[1].binding         = 1;
            bindings[1].descriptorCount = 1;

            var descriptorLayout = VkDescriptorSetLayoutCreateInfo.Alloc();

            descriptorLayout->bindings = bindings;

            VkDescriptorSetLayout dsl;

            vkCreateDescriptorSetLayout(device, descriptorLayout, null, &dsl);
            this.descriptorSetLayout = dsl;

            var info = new VkPipelineLayoutCreateInfo();

            info.sType      = PipelineLayoutCreateInfo;
            info.setLayouts = dsl;

            {
                VkPipelineLayout layout;
                vkCreatePipelineLayout(device, &info, null, &layout);
                this.pipelineLayout = layout;
            }
        }
예제 #18
0
        private void CreateGraphicsPipeline()
        {
            // Shader stages
            var vertShaderCode = System.IO.File.ReadAllBytes("Shaders/vert.spv");
            var fragShaderCode = System.IO.File.ReadAllBytes("Shaders/frag.spv");

            var vertShaderModule = CreateShaderModule(vertShaderCode);
            var fragShaderModule = CreateShaderModule(fragShaderCode);

            string name      = "main";
            int    byteCount = System.Text.Encoding.UTF8.GetByteCount(name);
            byte * utf8Ptr   = stackalloc byte[byteCount];

            fixed(char *namePtr = name)
            {
                System.Text.Encoding.UTF8.GetBytes(namePtr, name.Length, utf8Ptr, byteCount);
            }

            var vertShaderStageInfo = new VkPipelineShaderStageCreateInfo()
            {
                sType  = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
                stage  = VkShaderStageFlagBits.VK_SHADER_STAGE_VERTEX_BIT,
                module = vertShaderModule,
                pName  = utf8Ptr,
            };

            var fragShaderStageInfo = new VkPipelineShaderStageCreateInfo()
            {
                sType  = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
                stage  = VkShaderStageFlagBits.VK_SHADER_STAGE_FRAGMENT_BIT,
                module = fragShaderModule,
                pName  = utf8Ptr,
            };

            var shaderStages = stackalloc VkPipelineShaderStageCreateInfo[] { vertShaderStageInfo, fragShaderStageInfo };

            // VertexInput
            var vertexInputInfo = new VkPipelineVertexInputStateCreateInfo()
            {
                sType = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
                vertexBindingDescriptionCount   = 0,
                pVertexBindingDescriptions      = null,
                vertexAttributeDescriptionCount = 0,
                pVertexAttributeDescriptions    = null,
            };

            var inputAssembly = new VkPipelineInputAssemblyStateCreateInfo()
            {
                sType    = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
                topology = VkPrimitiveTopology.VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
                primitiveRestartEnable = false,
            };

            var viewport = new VkViewport()
            {
                x        = 0f,
                y        = 0f,
                width    = (float)vkSwapChainExtent.width,
                height   = (float)vkSwapChainExtent.height,
                minDepth = 0f,
                maxDepth = 1f,
            };

            var scissor = new VkRect2D()
            {
                offset = new VkOffset2D()
                {
                    x = 0, y = 0
                },
                extent = vkSwapChainExtent,
            };

            var viewportState = new VkPipelineViewportStateCreateInfo()
            {
                sType         = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
                viewportCount = 1,
                pViewports    = &viewport,
                scissorCount  = 1,
                pScissors     = &scissor,
            };

            var rasterizer = new VkPipelineRasterizationStateCreateInfo()
            {
                sType                   = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
                depthClampEnable        = false,
                rasterizerDiscardEnable = false,
                polygonMode             = VkPolygonMode.VK_POLYGON_MODE_FILL,
                lineWidth               = 1f,
                cullMode                = VkCullModeFlagBits.VK_CULL_MODE_BACK_BIT,
                frontFace               = VkFrontFace.VK_FRONT_FACE_CLOCKWISE,
                depthBiasEnable         = false,
                depthBiasConstantFactor = 0f,
                depthBiasClamp          = 0f,
                depthBiasSlopeFactor    = 0f,
            };

            var multisampling = new VkPipelineMultisampleStateCreateInfo()
            {
                sType = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
                sampleShadingEnable   = false,
                rasterizationSamples  = VkSampleCountFlagBits.VK_SAMPLE_COUNT_1_BIT,
                minSampleShading      = 1f,
                pSampleMask           = null,
                alphaToCoverageEnable = false,
                alphaToOneEnable      = false,
            };

            var colorBlendAttachment = new VkPipelineColorBlendAttachmentState()
            {
                colorWriteMask = VkColorComponentFlagBits.VK_COLOR_COMPONENT_R_BIT | VkColorComponentFlagBits.VK_COLOR_COMPONENT_G_BIT | VkColorComponentFlagBits.VK_COLOR_COMPONENT_B_BIT | VkColorComponentFlagBits.VK_COLOR_COMPONENT_A_BIT,
                blendEnable    = false,
            };


            var colorBlending = new VkPipelineColorBlendStateCreateInfo()
            {
                sType            = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
                logicOpEnable    = false,
                logicOp          = VkLogicOp.VK_LOGIC_OP_COPY,
                pAttachments     = &colorBlendAttachment,
                attachmentCount  = 1,
                blendConstants_0 = 0f,
                blendConstants_1 = 0f,
                blendConstants_2 = 0f,
                blendConstants_3 = 0f,
            };

            var pipelineLayoutInfo = new VkPipelineLayoutCreateInfo()
            {
                sType                  = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
                setLayoutCount         = 0,
                pushConstantRangeCount = 0,
            };

            VkPipelineLayout newPipelineLayout;
            var result = VulkanNative.vkCreatePipelineLayout(vkDevice, &pipelineLayoutInfo, null, &newPipelineLayout);

            vkPipelineLayout = newPipelineLayout;
            Helpers.CheckErrors(result);

            var pipelineInfo = new VkGraphicsPipelineCreateInfo()
            {
                sType               = VkStructureType.VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
                stageCount          = 2,
                pStages             = shaderStages,
                pVertexInputState   = &vertexInputInfo,
                pInputAssemblyState = &inputAssembly,
                pViewportState      = &viewportState,
                pRasterizationState = &rasterizer,
                pMultisampleState   = &multisampling,
                pDepthStencilState  = null,
                pColorBlendState    = &colorBlending,
                pDynamicState       = null,
                layout              = vkPipelineLayout,
                renderPass          = vkRenderPass,
                subpass             = 0,
                basePipelineHandle  = 0,
                basePipelineIndex   = -1,
            };

            VkPipeline newPipeline;

            result             = VulkanNative.vkCreateGraphicsPipelines(vkDevice, 0, 1, &pipelineInfo, null, &newPipeline);
            vkGraphicsPipeline = newPipeline;
            Helpers.CheckErrors(result);

            VulkanNative.vkDestroyShaderModule(vkDevice, vertShaderModule, null);
            VulkanNative.vkDestroyShaderModule(vkDevice, fragShaderModule, null);
        }
예제 #19
0
        void CreateGraphicsPipeline()
        {
            var vert = CreateShaderModule(File.ReadAllBytes("vert.spv"));
            var frag = CreateShaderModule(File.ReadAllBytes("frag.spv"));

            var vertInfo = new VkPipelineShaderStageCreateInfo();

            vertInfo.stage  = VkShaderStageFlags.VertexBit;
            vertInfo.module = vert;
            vertInfo.name   = "main";

            var fragInfo = new VkPipelineShaderStageCreateInfo();

            fragInfo.stage  = VkShaderStageFlags.FragmentBit;
            fragInfo.module = frag;
            fragInfo.name   = "main";

            var shaderStages = new List <VkPipelineShaderStageCreateInfo> {
                vertInfo, fragInfo
            };

            var vertexInputInfo = new VkPipelineVertexInputStateCreateInfo();

            var inputAssembly = new VkPipelineInputAssemblyStateCreateInfo();

            inputAssembly.topology = VkPrimitiveTopology.TriangleList;

            var viewport = new VkViewport();

            viewport.width    = swapchainExtent.width;
            viewport.height   = swapchainExtent.height;
            viewport.minDepth = 0f;
            viewport.maxDepth = 1f;

            var scissor = new VkRect2D();

            scissor.extent = swapchainExtent;

            var viewportState = new VkPipelineViewportStateCreateInfo();

            viewportState.viewports = new List <VkViewport> {
                viewport
            };
            viewportState.scissors = new List <VkRect2D> {
                scissor
            };

            var rasterizer = new VkPipelineRasterizationStateCreateInfo();

            rasterizer.polygonMode = VkPolygonMode.Fill;
            rasterizer.lineWidth   = 1f;
            rasterizer.cullMode    = VkCullModeFlags.BackBit;
            rasterizer.frontFace   = VkFrontFace.Clockwise;

            var multisampling = new VkPipelineMultisampleStateCreateInfo();

            multisampling.rasterizationSamples = VkSampleCountFlags._1_Bit;
            multisampling.minSampleShading     = 1f;

            var colorBlendAttachment = new VkPipelineColorBlendAttachmentState();

            colorBlendAttachment.colorWriteMask = VkColorComponentFlags.RBit
                                                  | VkColorComponentFlags.GBit
                                                  | VkColorComponentFlags.BBit
                                                  | VkColorComponentFlags.ABit;
            colorBlendAttachment.srcColorBlendFactor = VkBlendFactor.One;
            colorBlendAttachment.dstColorBlendFactor = VkBlendFactor.Zero;
            colorBlendAttachment.colorBlendOp        = VkBlendOp.Add;
            colorBlendAttachment.srcAlphaBlendFactor = VkBlendFactor.One;
            colorBlendAttachment.dstAlphaBlendFactor = VkBlendFactor.Zero;
            colorBlendAttachment.alphaBlendOp        = VkBlendOp.Add;

            var colorBlending = new VkPipelineColorBlendStateCreateInfo();

            colorBlending.logicOp     = VkLogicOp.Copy;
            colorBlending.attachments = new List <VkPipelineColorBlendAttachmentState> {
                colorBlendAttachment
            };

            var pipelineLayoutInfo = new VkPipelineLayoutCreateInfo();

            pipelineLayout?.Dispose();

            pipelineLayout = new VkPipelineLayout(device, pipelineLayoutInfo);

            var info = new VkGraphicsPipelineCreateInfo();

            info.stages             = shaderStages;
            info.vertexInputState   = vertexInputInfo;
            info.inputAssemblyState = inputAssembly;
            info.viewportState      = viewportState;
            info.rasterizationState = rasterizer;
            info.multisampleState   = multisampling;
            info.colorBlendState    = colorBlending;
            info.layout             = pipelineLayout;
            info.renderPass         = renderPass;
            info.subpass            = 0;
            info.basePipelineHandle = null;
            info.basePipelineIndex  = -1;

            pipeline?.Dispose();

            pipeline = new VkGraphicsPipeline(device, info, null);

            vert.Dispose();
            frag.Dispose();
        }
예제 #20
0
        public static void vkCmdBindDescriptorSets(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint, VkPipelineLayout layout, int firstSet, int descriptorSetCount, VkDescriptorSet[] pDescriptorSets, int dynamicOffsetCount, int[] pDynamicOffsets)
        {
            VkPreconditions.CheckNull(commandBuffer, nameof(commandBuffer));

            GetCommandBuffer(commandBuffer).CmdBindDescriptorSets(pipelineBindPoint, layout, firstSet, descriptorSetCount, pDescriptorSets, dynamicOffsetCount, pDynamicOffsets);
        }
예제 #21
0
        public static VkResult vkCreatePipelineLayout(VkDevice device, VkPipelineLayoutCreateInfo pipelineLayoutInfo, VkAllocationCallbacks pAllocator, out VkPipelineLayout pipelineLayout)
        {
            VkPreconditions.CheckNull(device, nameof(device));

            return(GetDevice(device).CreatePipelineLayout(pipelineLayoutInfo, out pipelineLayout));
        }
예제 #22
0
        public static void vkDestroyPipelineLayout(VkDevice device, VkPipelineLayout pipelineLayout, VkAllocationCallbacks pAllocator)
        {
            VkPreconditions.CheckNull(device, nameof(device));

            GetDevice(device).DestroyPipelineLayout(pipelineLayout);
        }
예제 #23
0
        private void FlushNewResourceSets(
            int newResourceSetsCount,
            VkResourceSet[] resourceSets,
            bool[] resourceSetsChanged,
            VkPipelineBindPoint bindPoint,
            VkPipelineLayout pipelineLayout)
        {
            if (newResourceSetsCount > 0)
            {
                int              totalChanged         = 0;
                uint             currentSlot          = 0;
                uint             currentBatchIndex    = 0;
                uint             currentBatchFirstSet = 0;
                VkDescriptorSet *descriptorSets       = stackalloc VkDescriptorSet[newResourceSetsCount];
                while (totalChanged < newResourceSetsCount)
                {
                    if (resourceSetsChanged[currentSlot])
                    {
                        resourceSetsChanged[currentSlot]  = false;
                        descriptorSets[currentBatchIndex] = resourceSets[currentSlot].DescriptorSet;
                        totalChanged      += 1;
                        currentBatchIndex += 1;
                        currentSlot       += 1;
                    }
                    else
                    {
                        if (currentBatchIndex != 0)
                        {
                            // Flush current batch.
                            vkCmdBindDescriptorSets(
                                _cb,
                                bindPoint,
                                pipelineLayout,
                                currentBatchFirstSet,
                                currentBatchIndex,
                                descriptorSets,
                                0,
                                null);
                            currentBatchIndex = 0;
                        }

                        currentSlot         += 1;
                        currentBatchFirstSet = currentSlot;
                    }
                }

                if (currentBatchIndex != 0)
                {
                    // Flush current batch.
                    vkCmdBindDescriptorSets(
                        _cb,
                        bindPoint,
                        pipelineLayout,
                        currentBatchFirstSet,
                        currentBatchIndex,
                        descriptorSets,
                        0,
                        null);
                }
            }
        }
예제 #24
0
 public static extern void DestroyPipelineLayout(
     VkDevice device,
     VkPipelineLayout pipelineLayout,
     IntPtr pAllocator
     );
예제 #25
0
 public static extern VkResult CreatePipelineLayout(
     VkDevice device,
     ref VkPipelineLayoutCreateInfo pCreateInfo,
     IntPtr pAllocator,
     out VkPipelineLayout pPipelineLayout
     );
예제 #26
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);
        }
예제 #27
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;
        }
예제 #28
0
 public override void DestroyPipelineLayout(VkPipelineLayout pipelineLayout)
 {
     throw new NotImplementedException();
 }
예제 #29
0
        VkPipeline CreatePipeline(VkDevice device, VkSurfaceCapabilitiesKHR surfaceCapabilities,
                                  VkRenderPass renderPass, VkPipelineLayout pipelineLayout)
        {
            //VkShaderModule vertexShaderModule = device.CreateShaderModule(LoadResource(@"Shaders\shader.vert.spv"));
            VkShaderModule vsModule;
            {
                var info = new VkShaderModuleCreateInfo {
                    sType = VkStructureType.ShaderModuleCreateInfo
                };
                byte[] bytes = LoadResource(@"Shaders\shader.vert.spv");
                info.code = bytes;
                //vkAPI.vkCreateShaderModule(device, bytes);
                vkAPI.vkCreateShaderModule(device, &info, null, &vsModule).Check();
                info.Free();
            }
            //VkShaderModule fragmentShaderModule = device.CreateShaderModule(LoadResource(@"Shaders\shader.frag.spv"));
            VkShaderModule fsModule;
            {
                var info = new VkShaderModuleCreateInfo {
                    sType = VkStructureType.ShaderModuleCreateInfo
                };
                byte[] bytes = LoadResource(@"Shaders\shader.frag.spv");
                info.code = bytes;
                //vkAPI.vkCreateShaderModule(device, bytes);
                vkAPI.vkCreateShaderModule(device, &info, null, &fsModule).Check();
                info.Free();
            }
            var shaderStages = new VkPipelineShaderStageCreateInfo[2];
            {
                shaderStages[0].sType  = VkStructureType.PipelineShaderStageCreateInfo;
                shaderStages[0].stage  = VkShaderStageFlagBits.Vertex;
                shaderStages[0].module = vsModule;
                //"main".Set(ref shaderStages[0].pName);
                shaderStages[0].pName  = "main";
                shaderStages[1].sType  = VkStructureType.PipelineShaderStageCreateInfo;
                shaderStages[1].stage  = VkShaderStageFlagBits.Fragment;
                shaderStages[1].module = fsModule;
                //"main".Set(ref shaderStages[1].pName);
                shaderStages[1].pName = "main";
            }
            var viewport = new VkPipelineViewportStateCreateInfo {
                sType = VkStructureType.PipelineViewportStateCreateInfo
            };

            viewport.viewports = new VkViewport(surfaceCapabilities.currentExtent, 0.0f, 1.0f);
            viewport.scissors  = new VkRect2D(surfaceCapabilities.currentExtent);

            var multisample = new VkPipelineMultisampleStateCreateInfo {
                sType = VkStructureType.PipelineMultisampleStateCreateInfo
            };

            multisample.rasterizationSamples = VkSampleCountFlagBits._1;

            var colorBlend = new VkPipelineColorBlendStateCreateInfo {
                sType = VkStructureType.PipelineColorBlendStateCreateInfo
            };

            colorBlend.logicOp = VkLogicOp.Copy;
            var blend = new VkPipelineColorBlendAttachmentState(
                colorWriteMask: VkColorComponentFlagBits.R | VkColorComponentFlagBits.G
                | VkColorComponentFlagBits.B | VkColorComponentFlagBits.A,
                blendEnable: false);

            colorBlend.attachments = blend;

            var rasterization = new VkPipelineRasterizationStateCreateInfo {
                sType = VkStructureType.PipelineRasterizationStateCreateInfo
            };

            rasterization.polygonMode = VkPolygonMode.Fill;
            rasterization.cullMode    = VkCullModeFlagBits.None;
            rasterization.frontFace   = VkFrontFace.Clockwise;
            rasterization.lineWidth   = 1.0f;

            var inputAssem = new VkPipelineInputAssemblyStateCreateInfo {
                sType = VkStructureType.PipelineInputAssemblyStateCreateInfo
            };

            inputAssem.topology = VkPrimitiveTopology.TriangleList;

            var input = new VkPipelineVertexInputStateCreateInfo {
                sType = VkStructureType.PipelineVertexInputStateCreateInfo
            };

            // static readonly float[] Vertices = { .. }
            input.vertexBindingDescriptions = new VkVertexInputBindingDescription(
                binding: 0,
                stride: 2 * sizeof(float),
                inputRate: VkVertexInputRate.Vertex);
            // layout(location = 0) in vec2 inPos;
            input.vertexAttributeDescriptions = new VkVertexInputAttributeDescription(
                location: 0,
                binding: 0,
                format: VkFormat.R32g32Sfloat,
                offset: 0);

            //VkPipelineCache cache = device.CreatePipelineCache(ref cacheInfo);
            VkPipelineCache cache;
            {
                var info = VkPipelineCacheCreateInfo.Alloc();
                vkAPI.vkCreatePipelineCache(device, info, null, &cache).Check();
                Marshal.FreeHGlobal((IntPtr)info);
            }
            //var infos = new VkGraphicsPipelineCreateInfo[] { pipelineCreateInfo };
            //return device.CreateGraphicsPipelines(ref cache, infos);
            VkPipeline pipeline;

            {
                var info = new VkGraphicsPipelineCreateInfo {
                    sType = VkStructureType.GraphicsPipelineCreateInfo
                };
                info.layout              = pipelineLayout;
                info.pViewportState      = &viewport;
                info.stages              = shaderStages;
                info.pMultisampleState   = &multisample;
                info.pColorBlendState    = &colorBlend;
                info.pRasterizationState = &rasterization;
                info.pInputAssemblyState = &inputAssem;
                info.pVertexInputState   = &input;
                info.renderPass          = renderPass;
                vkAPI.vkCreateGraphicsPipelines(device, cache, 1, &info, null, &pipeline).Check();
                info.Free();
            }

            shaderStages[0].Free();
            shaderStages[1].Free();
            viewport.Free();
            colorBlend.Free();
            input.Free();

            return(pipeline);
        }
예제 #30
0
 public override VkResult CreatePipelineLayout(VkPipelineLayoutCreateInfo pipelineLayoutInfo, out VkPipelineLayout pipelineLayout)
 {
     throw new NotImplementedException();
 }