private void CreateRenderPass() { var colorAttachment = new AttachmentDescription() { Format = vkSwapChainImageFormat, Samples = SampleCountFlags.Count1, LoadOp = AttachmentLoadOp.Clear, StoreOp = AttachmentStoreOp.Store, StencilLoadOp = AttachmentLoadOp.DontCare, StencilStoreOp = AttachmentStoreOp.DontCare, InitialLayout = ImageLayout.Undefined, FinalLayout = ImageLayout.PresentSrcKhr, }; var colorAttachmentRef = new AttachmentReference() { Attachment = 0, Layout = Vulkan.ImageLayout.ColorAttachmentOptimal, }; var subpass = new SubpassDescription() { PipelineBindPoint = PipelineBindPoint.Graphics, ColorAttachmentCount = 1, ColorAttachments = new AttachmentReference[] { colorAttachmentRef }, }; var dependency = new SubpassDependency() { SrcSubpass = VK_SUBPASS_EXTERNAL, SrcStageMask = PipelineStageFlags.ColorAttachmentOutput, SrcAccessMask = 0, DstSubpass = 0, DstStageMask = PipelineStageFlags.ColorAttachmentOutput, DstAccessMask = AccessFlags.ColorAttachmentRead | AccessFlags.ColorAttachmentWrite, }; var renderPassInfo = new RenderPassCreateInfo() { AttachmentCount = 1, Attachments = new AttachmentDescription[] { colorAttachment }, SubpassCount = 1, Subpasses = new SubpassDescription[] { subpass }, DependencyCount = 1, Dependencies = new SubpassDependency[] { dependency }, }; vkRenderPass = vkDevice.CreateRenderPass(renderPassInfo); }
protected void CreateRenderPass() { var colorAttachment = new AttachmentDescription { Format = format.Format, Samples = SampleCountFlags.Count1, LoadOp = AttachmentLoadOp.Clear, StoreOp = AttachmentStoreOp.Store, StencilLoadOp = AttachmentLoadOp.DontCare, StencilStoreOp = AttachmentStoreOp.DontCare, InitialLayout = ImageLayout.Undefined, FinalLayout = ImageLayout.PresentSrcKhr }; var colorAttachmentRef = new AttachmentReference { Attachment = 0, Layout = ImageLayout.ColorAttachmentOptimal }; var subpass = new SubpassDescription { PipelineBindPoint = PipelineBindPoint.Graphics, ColorAttachmentCount = 1, ColorAttachments = new AttachmentReference[] { colorAttachmentRef }, }; var dependency = new SubpassDependency { SrcSubpass = VK_SUBPASS_INTERNAL, DstSubpass = 0, SrcStageMask = PipelineStageFlags.ColorAttachmentOutput, SrcAccessMask = 0, DstStageMask = PipelineStageFlags.ColorAttachmentOutput, DstAccessMask = AccessFlags.ColorAttachmentRead | AccessFlags.ColorAttachmentWrite }; var renderPassInfo = new RenderPassCreateInfo { AttachmentCount = 1, Attachments = new AttachmentDescription[] { colorAttachment }, SubpassCount = 1, Subpasses = new SubpassDescription[] { subpass }, DependencyCount = 1, Dependencies = new SubpassDependency[] { dependency } }; renderPass = device.CreateRenderPass(renderPassInfo); }
private static RenderPass CreateRenderPass( Device logicalDevice, ReadOnlySpan <DeviceTexture> targets) { ResizeArray <AttachmentReference> colorReferences = new ResizeArray <AttachmentReference>(); AttachmentReference?depthReference = null; var attachmentDescriptions = new AttachmentDescription[targets.Length]; for (int i = 0; i < targets.Length; i++) { //Create the description attachmentDescriptions[i] = new AttachmentDescription( flags: AttachmentDescriptions.MayAlias, format: targets[i].Format, samples: SampleCounts.Count1, loadOp: AttachmentLoadOp.Clear, storeOp: AttachmentStoreOp.Store, stencilLoadOp: AttachmentLoadOp.DontCare, stencilStoreOp: AttachmentStoreOp.DontCare, initialLayout: ImageLayout.Undefined, finalLayout: targets[i].DesiredLayout); //Add the reference (either color or depth) if (targets[i].DepthTexture) { if (depthReference != null) { throw new Exception( $"[{nameof(Renderer)}] Only 1 depth target can be used at a time"); } else { depthReference = new AttachmentReference(i, ImageLayout.DepthStencilAttachmentOptimal); } } else { colorReferences.Add(new AttachmentReference(i, ImageLayout.ColorAttachmentOptimal)); } } //Dependency at the beginning to transition the attachments to the proper layout var beginTransitionDependency = new SubpassDependency( srcSubpass: Constant.SubpassExternal, dstSubpass: 0, //Our subpass srcStageMask: PipelineStages.BottomOfPipe, srcAccessMask: Accesses.MemoryRead, dstStageMask: PipelineStages.ColorAttachmentOutput, dstAccessMask: Accesses.ColorAttachmentWrite, dependencyFlags: Dependencies.None ); //Dependency at the end to transition the attachments to the final layout var endTransitionDependency = new SubpassDependency( srcSubpass: 0, //Our subpass dstSubpass: Constant.SubpassExternal, srcStageMask: PipelineStages.ColorAttachmentOutput, srcAccessMask: Accesses.ColorAttachmentWrite, dstStageMask: PipelineStages.BottomOfPipe, dstAccessMask: Accesses.MemoryRead, dependencyFlags: Dependencies.None ); return(logicalDevice.CreateRenderPass(new RenderPassCreateInfo( subpasses: new [] { new SubpassDescription ( flags: SubpassDescriptionFlags.None, colorAttachments: colorReferences.ToArray(), depthStencilAttachment: depthReference ) }, attachments: attachmentDescriptions, dependencies: new [] { beginTransitionDependency, endTransitionDependency } ))); }
public bool Create(string vsName, string fsName, VertexInputBindingDescription [] vbind, VertexInputAttributeDescription [] vatts) { Device dv = mDevices.GetLogicalDevice(); PipelineShaderStageCreateInfo plssciv = new PipelineShaderStageCreateInfo( ShaderStages.Vertex, mShaders[vsName], "main", null); PipelineShaderStageCreateInfo plsscif = new PipelineShaderStageCreateInfo( ShaderStages.Fragment, mShaders[fsName], "main", null); PipelineVertexInputStateCreateInfo plvisci = new PipelineVertexInputStateCreateInfo( vbind, vatts); PipelineInputAssemblyStateCreateInfo pliasci = new PipelineInputAssemblyStateCreateInfo( PrimitiveTopology.TriangleList); Viewport vp = new Viewport(0, 0, 1280, 720, 0f, 1f); Rect2D scissor = new Rect2D(Offset2D.Zero, mDevices.GetChainExtent()); PipelineViewportStateCreateInfo plvpsci = new PipelineViewportStateCreateInfo( new Viewport[1] { vp }, new Rect2D[1] { scissor }); PipelineRasterizationStateCreateInfo plrsci = new PipelineRasterizationStateCreateInfo(); plrsci.LineWidth = 1; PipelineMultisampleStateCreateInfo plmssci = new PipelineMultisampleStateCreateInfo(); plmssci.RasterizationSamples = SampleCounts.Count1; PipelineColorBlendAttachmentState plcbas = new PipelineColorBlendAttachmentState(); plcbas.ColorWriteMask = ColorComponents.All; plcbas.BlendEnable = false; PipelineColorBlendStateCreateInfo plcbsci = new PipelineColorBlendStateCreateInfo(); plcbsci.LogicOpEnable = false; plcbsci.LogicOp = LogicOp.Copy; plcbsci.Attachments = new PipelineColorBlendAttachmentState[1] { plcbas }; plcbsci.BlendConstants = ColorF4.Zero; PipelineLayoutCreateInfo pllci = new PipelineLayoutCreateInfo( mDevices.GetDSLs()); mPipeLayout = dv.CreatePipelineLayout(pllci); AttachmentDescription ad = new AttachmentDescription(); ad.Format = Format.B8G8R8A8UNorm; ad.Samples = SampleCounts.Count1; ad.LoadOp = AttachmentLoadOp.Clear; ad.StoreOp = AttachmentStoreOp.Store; ad.StencilLoadOp = AttachmentLoadOp.DontCare; ad.StencilStoreOp = AttachmentStoreOp.DontCare; ad.InitialLayout = ImageLayout.Undefined; ad.FinalLayout = ImageLayout.PresentSrcKhr; AttachmentReference ar = new AttachmentReference(0, ImageLayout.ColorAttachmentOptimal); SubpassDescription spd = new SubpassDescription(); spd.ColorAttachments = new AttachmentReference[1] { ar }; SubpassDependency spdc = new SubpassDependency(); spdc.SrcSubpass = Constant.SubpassExternal; spdc.DstSubpass = 0; spdc.SrcStageMask = PipelineStages.ColorAttachmentOutput; spdc.SrcAccessMask = 0; spdc.DstStageMask = PipelineStages.ColorAttachmentOutput; spdc.DstAccessMask = Accesses.ColorAttachmentRead | Accesses.ColorAttachmentWrite; RenderPassCreateInfo rpci = new RenderPassCreateInfo( new SubpassDescription[1] { spd }, new AttachmentDescription[1] { ad }, new SubpassDependency[1] { spdc }); mRenderPass = dv.CreateRenderPass(rpci); GraphicsPipelineCreateInfo gplci = new GraphicsPipelineCreateInfo(); gplci.Stages = new PipelineShaderStageCreateInfo[2] { plssciv, plsscif }; gplci.VertexInputState = plvisci; gplci.InputAssemblyState = pliasci; gplci.ViewportState = plvpsci; gplci.RasterizationState = plrsci; gplci.MultisampleState = plmssci; gplci.DepthStencilState = null; gplci.ColorBlendState = plcbsci; gplci.DynamicState = null; gplci.Layout = mPipeLayout; gplci.RenderPass = mRenderPass; gplci.Subpass = 0; gplci.BasePipelineHandle = null; gplci.BasePipelineIndex = -1; mPipe = dv.CreateGraphicsPipeline(gplci); CreateSyncObjects(); return(true); }
private RenderPass CreateRenderpass() { AttachmentDescription *pAttachments = stackalloc AttachmentDescription[2] { //Color Attachment new AttachmentDescription { Format = SwapchainImageFormat, Samples = SampleCountFlags.SampleCount1Bit, LoadOp = AttachmentLoadOp.Clear, StoreOp = AttachmentStoreOp.Store, StencilLoadOp = AttachmentLoadOp.DontCare, StencilStoreOp = AttachmentStoreOp.DontCare, InitialLayout = ImageLayout.Undefined, FinalLayout = ImageLayout.PresentSrcKhr }, //Depth Attachment new AttachmentDescription { Format = DepthFormat, Samples = SampleCountFlags.SampleCount1Bit, LoadOp = AttachmentLoadOp.Clear, StoreOp = AttachmentStoreOp.DontCare, StencilLoadOp = AttachmentLoadOp.DontCare, StencilStoreOp = AttachmentStoreOp.DontCare, InitialLayout = ImageLayout.Undefined, FinalLayout = ImageLayout.DepthStencilAttachmentOptimal } }; var colorAttachmentRef = new AttachmentReference(0, ImageLayout.ColorAttachmentOptimal); var depthAttachmentRef = new AttachmentReference(1, ImageLayout.DepthStencilAttachmentOptimal); var subpass = new SubpassDescription { PipelineBindPoint = PipelineBindPoint.Graphics, ColorAttachmentCount = 1, PColorAttachments = &colorAttachmentRef, PDepthStencilAttachment = &depthAttachmentRef }; var dependency = new SubpassDependency { SrcSubpass = Vk.SubpassExternal, DstSubpass = 0, SrcStageMask = PipelineStageFlags.PipelineStageColorAttachmentOutputBit, SrcAccessMask = 0, DstStageMask = PipelineStageFlags.PipelineStageColorAttachmentOutputBit, DstAccessMask = AccessFlags.AccessColorAttachmentReadBit | AccessFlags.AccessColorAttachmentWriteBit }; var renderPassInfo = new RenderPassCreateInfo { SType = StructureType.RenderPassCreateInfo, AttachmentCount = 2, PAttachments = pAttachments, SubpassCount = 1, PSubpasses = &subpass, DependencyCount = 1, PDependencies = &dependency }; RenderPass rPass; var res = VkApi.CreateRenderPass(Device, &renderPassInfo, null, &rPass); if (res != Result.Success) { throw new VMASharp.VulkanResultException("Failed to create RenderPass!", res); } return(rPass); } }
public unsafe RenderPass(Api api, SwapChain swapChain, DepthBuffer depthBuffer, AttachmentLoadOp colorBufferLoadOp, AttachmentLoadOp depthBufferLoadOp) { _api = api; var colorAttachment = new AttachmentDescription(); colorAttachment.Format = swapChain.Format; colorAttachment.Samples = SampleCountFlags.SampleCount1Bit; colorAttachment.LoadOp = colorBufferLoadOp; colorAttachment.StoreOp = AttachmentStoreOp.Store; colorAttachment.StencilLoadOp = AttachmentLoadOp.DontCare; colorAttachment.StencilStoreOp = AttachmentStoreOp.DontCare; colorAttachment.InitialLayout = colorBufferLoadOp == AttachmentLoadOp.Clear ? ImageLayout.Undefined : ImageLayout.PresentSrcKhr; colorAttachment.FinalLayout = ImageLayout.PresentSrcKhr; var depthAttachment = new AttachmentDescription(); depthAttachment.Format = depthBuffer.Format; depthAttachment.Samples = SampleCountFlags.SampleCount1Bit; depthAttachment.LoadOp = depthBufferLoadOp; depthAttachment.StoreOp = AttachmentStoreOp.DontCare; depthAttachment.StencilLoadOp = AttachmentLoadOp.DontCare; depthAttachment.StencilStoreOp = AttachmentStoreOp.DontCare; depthAttachment.InitialLayout = depthBufferLoadOp == AttachmentLoadOp.Clear ? ImageLayout.Undefined : ImageLayout.DepthStencilAttachmentOptimal; depthAttachment.FinalLayout = ImageLayout.DepthStencilAttachmentOptimal; var colorAttachmentRef = new AttachmentReference(); colorAttachmentRef.Attachment = 0; colorAttachmentRef.Layout = ImageLayout.ColorAttachmentOptimal; var depthAttachmentRef = new AttachmentReference(); depthAttachmentRef.Attachment = 1; depthAttachmentRef.Layout = ImageLayout.DepthStencilAttachmentOptimal; var subpass = new SubpassDescription(); subpass.PipelineBindPoint = PipelineBindPoint.Graphics; subpass.ColorAttachmentCount = 1; subpass.PColorAttachments = (AttachmentReference *)Unsafe.AsPointer(ref colorAttachmentRef); subpass.PDepthStencilAttachment = (AttachmentReference *)Unsafe.AsPointer(ref depthAttachmentRef); var dependency = new SubpassDependency(); dependency.SrcSubpass = Vk.SubpassExternal; dependency.DstSubpass = 0; dependency.SrcStageMask = PipelineStageFlags.PipelineStageColorAttachmentOutputBit; dependency.SrcAccessMask = 0; dependency.DstStageMask = PipelineStageFlags.PipelineStageColorAttachmentOutputBit; dependency.DstAccessMask = AccessFlags.AccessColorAttachmentReadBit | AccessFlags.AccessColorAttachmentWriteBit; Span <AttachmentDescription> attachments = stackalloc AttachmentDescription[] { colorAttachment, depthAttachment }; var renderPassInfo = new RenderPassCreateInfo(); renderPassInfo.SType = StructureType.RenderPassCreateInfo; renderPassInfo.AttachmentCount = (uint)attachments.Length; renderPassInfo.PAttachments = (AttachmentDescription *)Unsafe.AsPointer(ref attachments[0]); renderPassInfo.SubpassCount = 1; renderPassInfo.PSubpasses = (SubpassDescription *)Unsafe.AsPointer(ref subpass); renderPassInfo.DependencyCount = 1; renderPassInfo.PDependencies = (SubpassDependency *)Unsafe.AsPointer(ref dependency); Util.Verify( _api.Vk.CreateRenderPass(_api.Vk.CurrentDevice.Value, renderPassInfo, default, out _vkRenderPass),
public static unsafe RenderPassCreateInfo RenderPassCreateInfo(Span <AttachmentDescription> attachments, SubpassDependency dependency, SubpassDescription subpass) { RenderPassCreateInfo renderPassCreateInfo = new() { SType = StructureType.RenderPassCreateInfo, AttachmentCount = (uint)attachments.Length, DependencyCount = 1, PDependencies = &dependency, SubpassCount = 1, PSubpasses = &subpass }; fixed(AttachmentDescription *ptr = &attachments[0]) { renderPassCreateInfo.PAttachments = ptr; } return(renderPassCreateInfo); }
public static unsafe DisposableRenderPass ToRenderPass(this ProgramPipelineState state, VulkanRenderer gd, Device device) { const int MaxAttachments = Constants.MaxRenderTargets + 1; AttachmentDescription[] attachmentDescs = null; var subpass = new SubpassDescription() { PipelineBindPoint = PipelineBindPoint.Graphics }; AttachmentReference *attachmentReferences = stackalloc AttachmentReference[MaxAttachments]; Span <int> attachmentIndices = stackalloc int[MaxAttachments]; Span <Silk.NET.Vulkan.Format> attachmentFormats = stackalloc Silk.NET.Vulkan.Format[MaxAttachments]; int attachmentCount = 0; int colorCount = 0; int maxColorAttachmentIndex = 0; for (int i = 0; i < state.AttachmentEnable.Length; i++) { if (state.AttachmentEnable[i]) { maxColorAttachmentIndex = i; attachmentFormats[attachmentCount] = gd.FormatCapabilities.ConvertToVkFormat(state.AttachmentFormats[i]); attachmentIndices[attachmentCount++] = i; colorCount++; } } if (state.DepthStencilEnable) { attachmentFormats[attachmentCount++] = gd.FormatCapabilities.ConvertToVkFormat(state.DepthStencilFormat); } if (attachmentCount != 0) { attachmentDescs = new AttachmentDescription[attachmentCount]; for (int i = 0; i < attachmentCount; i++) { int bindIndex = attachmentIndices[i]; attachmentDescs[i] = new AttachmentDescription( 0, attachmentFormats[i], TextureStorage.ConvertToSampleCountFlags((uint)state.SamplesCount), AttachmentLoadOp.Load, AttachmentStoreOp.Store, AttachmentLoadOp.Load, AttachmentStoreOp.Store, ImageLayout.General, ImageLayout.General); } int colorAttachmentsCount = colorCount; if (colorAttachmentsCount > MaxAttachments - 1) { colorAttachmentsCount = MaxAttachments - 1; } if (colorAttachmentsCount != 0) { int maxAttachmentIndex = Constants.MaxRenderTargets - 1; subpass.ColorAttachmentCount = (uint)maxAttachmentIndex + 1; subpass.PColorAttachments = &attachmentReferences[0]; // Fill with VK_ATTACHMENT_UNUSED to cover any gaps. for (int i = 0; i <= maxAttachmentIndex; i++) { subpass.PColorAttachments[i] = new AttachmentReference(Vk.AttachmentUnused, ImageLayout.Undefined); } for (int i = 0; i < colorAttachmentsCount; i++) { int bindIndex = attachmentIndices[i]; subpass.PColorAttachments[bindIndex] = new AttachmentReference((uint)i, ImageLayout.General); } } if (state.DepthStencilEnable) { uint dsIndex = (uint)attachmentCount - 1; subpass.PDepthStencilAttachment = &attachmentReferences[MaxAttachments - 1]; *subpass.PDepthStencilAttachment = new AttachmentReference(dsIndex, ImageLayout.General); } } var subpassDependency = new SubpassDependency( 0, 0, PipelineStageFlags.PipelineStageAllGraphicsBit, PipelineStageFlags.PipelineStageAllGraphicsBit, AccessFlags.AccessMemoryReadBit | AccessFlags.AccessMemoryWriteBit, AccessFlags.AccessMemoryReadBit | AccessFlags.AccessMemoryWriteBit, 0); fixed(AttachmentDescription *pAttachmentDescs = attachmentDescs) { var renderPassCreateInfo = new RenderPassCreateInfo() { SType = StructureType.RenderPassCreateInfo, PAttachments = pAttachmentDescs, AttachmentCount = attachmentDescs != null ? (uint)attachmentDescs.Length : 0, PSubpasses = &subpass, SubpassCount = 1, PDependencies = &subpassDependency, DependencyCount = 1 }; gd.Api.CreateRenderPass(device, renderPassCreateInfo, null, out var renderPass).ThrowOnError(); return(new DisposableRenderPass(gd.Api, device, renderPass)); } }