public void Dispose() { VK.DestroySemaphore(device, imageAvailableSemaphore, alloc); VK.DestroySemaphore(device, renderFinishedSemaphore, alloc); VK.DestroyCommandPool(device, commandPool, alloc); foreach (var fb in swapchainFramebuffers) { VK.DestroyFramebuffer(device, fb, alloc); } VK.DestroyPipeline(device, pipeline, alloc); VK.DestroyPipelineLayout(device, pipelineLayout, alloc); VK.DestroyRenderPass(device, renderPass, alloc); foreach (var iv in swapchainImageViews) { VK.DestroyImageView(device, iv, alloc); } destroySwapchain(device, swapchain, alloc); VK.DestroyDevice(device, alloc); destroySurface(instance, surface, alloc); VK.DestroyInstance(instance, alloc); GLFW.Terminate(); }
void CreateGraphicsPipeline() { VkShaderModule vert = CreateShaderModule(File.ReadAllBytes("vert.spv")); VkShaderModule frag = CreateShaderModule(File.ReadAllBytes("frag.spv")); InteropString entry = new InteropString("main"); var vertInfo = new VkPipelineShaderStageCreateInfo(); vertInfo.sType = CSGL.Vulkan.VkStructureType.PipelineShaderStageCreateInfo; vertInfo.stage = CSGL.Vulkan.VkShaderStageFlags.VertexBit; vertInfo.module = vert; vertInfo.pName = entry.Address; var fragInfo = new VkPipelineShaderStageCreateInfo(); fragInfo.sType = CSGL.Vulkan.VkStructureType.PipelineShaderStageCreateInfo; fragInfo.stage = CSGL.Vulkan.VkShaderStageFlags.FragmentBit; fragInfo.module = frag; fragInfo.pName = entry.Address; var shaderStages = new NativeArray <VkPipelineShaderStageCreateInfo>(2); shaderStages[0] = vertInfo; shaderStages[1] = fragInfo; var vertexInputInfo = new VkPipelineVertexInputStateCreateInfo(); vertexInputInfo.sType = CSGL.Vulkan.VkStructureType.PipelineVertexInputStateCreateInfo; var vertexInputNative = new Native <VkPipelineVertexInputStateCreateInfo>(vertexInputInfo); var inputAssembly = new VkPipelineInputAssemblyStateCreateInfo(); inputAssembly.sType = CSGL.Vulkan.VkStructureType.PipelineInputAssemblyStateCreateInfo; inputAssembly.topology = CSGL.Vulkan.VkPrimitiveTopology.TriangleList; var inputAssemblyNative = new Native <VkPipelineInputAssemblyStateCreateInfo>(inputAssembly); var viewport = new VkViewport(); viewport.width = swapchainExtent.width; viewport.height = swapchainExtent.height; viewport.minDepth = 0f; viewport.maxDepth = 1f; var viewportNative = new Native <VkViewport>(viewport); var scissor = new VkRect2D(); scissor.extent = swapchainExtent; var scissorNative = new Native <VkRect2D>(scissor); var viewportState = new VkPipelineViewportStateCreateInfo(); viewportState.sType = CSGL.Vulkan.VkStructureType.PipelineViewportStateCreateInfo; viewportState.viewportCount = 1; viewportState.pViewports = viewportNative.Address; viewportState.scissorCount = 1; viewportState.pScissors = scissorNative.Address; var viewportStateNative = new Native <VkPipelineViewportStateCreateInfo>(viewportState); var rasterizer = new VkPipelineRasterizationStateCreateInfo(); rasterizer.sType = CSGL.Vulkan.VkStructureType.PipelineRasterizationStateCreateInfo; rasterizer.polygonMode = CSGL.Vulkan.VkPolygonMode.Fill; rasterizer.lineWidth = 1f; rasterizer.cullMode = CSGL.Vulkan.VkCullModeFlags.BackBit; rasterizer.frontFace = CSGL.Vulkan.VkFrontFace.Clockwise; var rasterizerNative = new Native <VkPipelineRasterizationStateCreateInfo>(rasterizer); var multisampling = new VkPipelineMultisampleStateCreateInfo(); multisampling.sType = CSGL.Vulkan.VkStructureType.PipelineMultisampleStateCreateInfo; multisampling.rasterizationSamples = CSGL.Vulkan.VkSampleCountFlags._1_Bit; multisampling.minSampleShading = 1f; var multisamplingNative = new Native <VkPipelineMultisampleStateCreateInfo>(multisampling); var colorBlendAttachment = new VkPipelineColorBlendAttachmentState(); colorBlendAttachment.colorWriteMask = CSGL.Vulkan.VkColorComponentFlags.RBit | CSGL.Vulkan.VkColorComponentFlags.GBit | CSGL.Vulkan.VkColorComponentFlags.BBit | CSGL.Vulkan.VkColorComponentFlags.ABit; colorBlendAttachment.srcColorBlendFactor = CSGL.Vulkan.VkBlendFactor.One; colorBlendAttachment.dstColorBlendFactor = CSGL.Vulkan.VkBlendFactor.Zero; colorBlendAttachment.colorBlendOp = CSGL.Vulkan.VkBlendOp.Add; colorBlendAttachment.srcAlphaBlendFactor = CSGL.Vulkan.VkBlendFactor.One; colorBlendAttachment.dstAlphaBlendFactor = CSGL.Vulkan.VkBlendFactor.Zero; colorBlendAttachment.alphaBlendOp = CSGL.Vulkan.VkBlendOp.Add; var colorBlendAttachmentNative = new Native <VkPipelineColorBlendAttachmentState>(colorBlendAttachment); var colorBlending = new VkPipelineColorBlendStateCreateInfo(); colorBlending.sType = CSGL.Vulkan.VkStructureType.PipelineColorBlendStateCreateInfo; colorBlending.logicOp = CSGL.Vulkan.VkLogicOp.Copy; colorBlending.attachmentCount = 1; colorBlending.pAttachments = colorBlendAttachmentNative.Address; var colorBlendingNative = new Native <VkPipelineColorBlendStateCreateInfo>(colorBlending); var pipelineLayoutInfo = new VkPipelineLayoutCreateInfo(); pipelineLayoutInfo.sType = CSGL.Vulkan.VkStructureType.PipelineLayoutCreateInfo; if (pipelineLayout != VkPipelineLayout.Null) { VK.DestroyPipelineLayout(device, pipelineLayout, alloc); } var result = VK.CreatePipelineLayout(device, ref pipelineLayoutInfo, alloc, out pipelineLayout); var info = new VkGraphicsPipelineCreateInfo(); info.sType = CSGL.Vulkan.VkStructureType.GraphicsPipelineCreateInfo; info.stageCount = 2; info.pStages = shaderStages.Address; info.pVertexInputState = vertexInputNative.Address; info.pInputAssemblyState = inputAssemblyNative.Address; info.pViewportState = viewportStateNative.Address; info.pRasterizationState = rasterizerNative.Address; info.pMultisampleState = multisamplingNative.Address; info.pColorBlendState = colorBlendingNative.Address; info.layout = pipelineLayout; info.renderPass = renderPass; info.subpass = 0; info.basePipelineHandle = VkPipeline.Null; info.basePipelineIndex = -1; var infoNative = new Native <VkGraphicsPipelineCreateInfo>(info); var temp = new Native <VkPipeline>(); if (pipeline != VkPipeline.Null) { VK.DestroyPipeline(device, pipeline, alloc); } result = VK.CreateGraphicsPipelines(device, VkPipelineCache.Null, 1, infoNative.Address, alloc, temp.Address); pipeline = temp.Value; infoNative.Dispose(); temp.Dispose(); entry.Dispose(); shaderStages.Dispose(); vertexInputNative.Dispose(); inputAssemblyNative.Dispose(); viewportNative.Dispose(); scissorNative.Dispose(); viewportStateNative.Dispose(); rasterizerNative.Dispose(); multisamplingNative.Dispose(); colorBlendingNative.Dispose(); colorBlendAttachmentNative.Dispose(); VK.DestroyShaderModule(device, vert, alloc); VK.DestroyShaderModule(device, frag, alloc); }