private void createCommandBuffers(VkContext context) { var allocInfo = new Vk.CommandBufferAllocateInfo(); allocInfo.CommandPool = context.GraphicsCommandPool; allocInfo.Level = Vk.CommandBufferLevel.Primary; allocInfo.CommandBufferCount = this.wrapper.ImageCapacity; try { this.wrapper.CommandBuffers = context.Device.AllocateCommandBuffers(allocInfo); } catch (Vk.ResultException result) { throw new VkException("An error occurred while creating the command buffers.", result); } for (int i = 0; i < this.wrapper.ImageCapacity; i++) { Vk.CommandBuffer buffer = this.wrapper.CommandBuffers[i]; var beginInfo = new Vk.CommandBufferBeginInfo(); beginInfo.Flags = Vk.CommandBufferUsageFlags.SimultaneousUse; try { buffer.Begin(beginInfo); } catch (Vk.ResultException result) { throw new VkException($"An error occurred while beginning recording for command buffer {i}.", result); } var renderPassInfo = new Vk.RenderPassBeginInfo(); renderPassInfo.RenderPass = this.wrapper.RenderPass; renderPassInfo.Framebuffer = this.wrapper.Framebuffers[i]; var clearColour = new Vk.ClearValue(); var renderArea = new Vk.Rect2D(); clearColour.Color = new Vk.ClearColorValue(new float[] { 0.0F, 0.0F, 0.0F, 1.0F }); renderArea.Extent.Width = this.wrapper.Extent.Width; renderArea.Extent.Height = this.wrapper.Extent.Height; renderArea.Offset.X = 0; renderArea.Offset.Y = 0; var clearDepth = new Vk.ClearValue(); var depth = new Vk.ClearDepthStencilValue(); depth.Depth = 1; clearDepth.DepthStencil = depth; renderPassInfo.RenderArea = renderArea; renderPassInfo.ClearValueCount = 2; renderPassInfo.ClearValues = new Vk.ClearValue[] { clearColour, clearDepth }; buffer.CmdBeginRenderPass(renderPassInfo, Vk.SubpassContents.Inline); buffer.CmdBindPipeline(Vk.PipelineBindPoint.Graphics, this.wrapper.Pipeline); buffer.CmdBindDescriptorSet(Vk.PipelineBindPoint.Graphics, this.wrapper.PipelineLayout, 0, this.wrapper.DescriptorSets[i], null); this.renderPassCallback.Invoke(buffer); buffer.CmdEndRenderPass(); try { buffer.End(); } catch (Vk.ResultException result) { throw new VkException($"An error occurred while recording for command buffer {i}.", result); } } }
private void createGraphicsPipeline(VkContext context) { var vertShaderStageInfo = new Vk.PipelineShaderStageCreateInfo(); vertShaderStageInfo.Stage = Vk.ShaderStageFlags.Vertex; vertShaderStageInfo.Module = this.vertexShader; vertShaderStageInfo.Name = "main"; var fragShaderStageInfo = new Vk.PipelineShaderStageCreateInfo(); fragShaderStageInfo.Stage = Vk.ShaderStageFlags.Fragment; fragShaderStageInfo.Module = this.fragmentShader; fragShaderStageInfo.Name = "main"; var shaderStageInfos = new Vk.PipelineShaderStageCreateInfo[] { vertShaderStageInfo, fragShaderStageInfo }; var attributes = Vertex.AttributeDescriptions; var bindings = new Vk.VertexInputBindingDescription[] { Vertex.BindingDescription }; var vertexInputInfo = new Vk.PipelineVertexInputStateCreateInfo(); vertexInputInfo.VertexBindingDescriptionCount = (uint)bindings.Length; vertexInputInfo.VertexBindingDescriptions = bindings; vertexInputInfo.VertexAttributeDescriptionCount = (uint)attributes.Length; vertexInputInfo.VertexAttributeDescriptions = attributes; var inputAssemblyInfo = new Vk.PipelineInputAssemblyStateCreateInfo(); inputAssemblyInfo.Topology = Vk.PrimitiveTopology.TriangleList; inputAssemblyInfo.PrimitiveRestartEnable = false; Vk.Viewport viewport = new Vk.Viewport(); viewport.X = 0.0F; viewport.Y = 0.0F; viewport.Width = (float)this.wrapper.Extent.Width; viewport.Height = (float)this.wrapper.Extent.Height; viewport.MinDepth = 0.0F; viewport.MaxDepth = 1.0F; Vk.Rect2D scissorRect = new Vk.Rect2D(); scissorRect.Offset.X = 0; scissorRect.Offset.Y = 0; scissorRect.Extent = this.wrapper.Extent; var viewportStateInfo = new Vk.PipelineViewportStateCreateInfo(); viewportStateInfo.ViewportCount = 1; viewportStateInfo.Viewports = new Vk.Viewport[] { viewport }; viewportStateInfo.ScissorCount = 1; viewportStateInfo.Scissors = new Vk.Rect2D[] { scissorRect }; var rasteriserInfo = new Vk.PipelineRasterizationStateCreateInfo(); rasteriserInfo.DepthClampEnable = false; rasteriserInfo.RasterizerDiscardEnable = false; rasteriserInfo.PolygonMode = Vk.PolygonMode.Fill; rasteriserInfo.LineWidth = 1.0F; rasteriserInfo.CullMode = Vk.CullModeFlags.Back; rasteriserInfo.FrontFace = Vk.FrontFace.Clockwise; rasteriserInfo.DepthBiasEnable = false; var multisamplingInfo = new Vk.PipelineMultisampleStateCreateInfo(); multisamplingInfo.SampleShadingEnable = false; multisamplingInfo.RasterizationSamples = Vk.SampleCountFlags.Count1; var colourBlendAttachmentInfo = new Vk.PipelineColorBlendAttachmentState(); colourBlendAttachmentInfo.ColorWriteMask = Vk.ColorComponentFlags.R | Vk.ColorComponentFlags.G | Vk.ColorComponentFlags.B | Vk.ColorComponentFlags.A; colourBlendAttachmentInfo.BlendEnable = true; colourBlendAttachmentInfo.SrcColorBlendFactor = Vk.BlendFactor.SrcAlpha; colourBlendAttachmentInfo.DstColorBlendFactor = Vk.BlendFactor.OneMinusSrcAlpha; colourBlendAttachmentInfo.ColorBlendOp = Vk.BlendOp.Add; colourBlendAttachmentInfo.SrcAlphaBlendFactor = Vk.BlendFactor.One; colourBlendAttachmentInfo.DstAlphaBlendFactor = Vk.BlendFactor.Zero; colourBlendAttachmentInfo.AlphaBlendOp = Vk.BlendOp.Add; var colourBlendAttachmentInfos = new Vk.PipelineColorBlendAttachmentState[] { colourBlendAttachmentInfo }; var colourBlendStateInfo = new Vk.PipelineColorBlendStateCreateInfo(); colourBlendStateInfo.LogicOpEnable = false; colourBlendStateInfo.LogicOp = Vk.LogicOp.Copy; colourBlendStateInfo.AttachmentCount = 1; colourBlendStateInfo.Attachments = colourBlendAttachmentInfos; var pipelineLayoutInfo = new Vk.PipelineLayoutCreateInfo(); pipelineLayoutInfo.SetLayoutCount = 1; pipelineLayoutInfo.SetLayouts = new Vk.DescriptorSetLayout[] { this.descriptorSetLayout }; try { this.wrapper.PipelineLayout = context.Device.CreatePipelineLayout(pipelineLayoutInfo); } catch (Vk.ResultException result) { throw new VkException("An error occurred while creating the pipeline layout.", result); } var depthStencil = new Vk.PipelineDepthStencilStateCreateInfo(); depthStencil.DepthTestEnable = true; depthStencil.DepthWriteEnable = true; depthStencil.DepthCompareOp = Vk.CompareOp.Less; depthStencil.DepthBoundsTestEnable = false; depthStencil.StencilTestEnable = false; var pipelineInfo = new Vk.GraphicsPipelineCreateInfo(); pipelineInfo.StageCount = 2; pipelineInfo.Stages = shaderStageInfos; pipelineInfo.VertexInputState = vertexInputInfo; pipelineInfo.InputAssemblyState = inputAssemblyInfo; pipelineInfo.ViewportState = viewportStateInfo; pipelineInfo.RasterizationState = rasteriserInfo; pipelineInfo.MultisampleState = multisamplingInfo; pipelineInfo.DepthStencilState = depthStencil; pipelineInfo.ColorBlendState = colourBlendStateInfo; pipelineInfo.DynamicState = null; pipelineInfo.Layout = this.wrapper.PipelineLayout; pipelineInfo.RenderPass = this.wrapper.RenderPass; pipelineInfo.Subpass = 0; pipelineInfo.BasePipelineHandle = null; pipelineInfo.BasePipelineIndex = -1; var pipelineInfos = new Vk.GraphicsPipelineCreateInfo[] { pipelineInfo }; try { this.wrapper.Pipeline = context.Device.CreateGraphicsPipelines(null, pipelineInfos)[0]; } catch (Vk.ResultException result) { throw new VkException("An error occurred while creating the graphics pipeline.", result); } }