private VkFramebuffer[] CreateFramebuffers() { var framebuffers = new VkFramebuffer[SwapchainImages.Length]; for (int i = 0; i < SwapchainImages.Length; i++) { VkImageView *attachments = stackalloc VkImageView[2] { _imageViews[i], _depthStencilBuffer.View }; var createInfo = new VkFramebufferCreateInfo { sType = VkStructureType.FramebufferCreateInfo, pNext = null, attachmentCount = 2, pAttachments = attachments, width = (uint)Host.Width, height = (uint)Host.Height, renderPass = _renderPass, layers = 1 }; VkFramebuffer frameBuffer; vkCreateFramebuffer(Context.Device, &createInfo, null, out frameBuffer).CheckResult(); framebuffers[i] = frameBuffer; } return(framebuffers); }
public static VkResult vkCreateFramebuffer( VkDevice device, VkRenderPass renderPass, ReadOnlySpan <VkImageView> attachments, uint width, uint height, uint layers, out VkFramebuffer framebuffer) { fixed(VkImageView *attachmentsPtr = attachments) { VkFramebufferCreateInfo createInfo = new VkFramebufferCreateInfo { sType = VkStructureType.FramebufferCreateInfo, renderPass = renderPass, attachmentCount = (uint)attachments.Length, pAttachments = attachmentsPtr, width = width, height = height, layers = layers }; return(vkCreateFramebuffer(device, &createInfo, null, out framebuffer)); } }
private ulong[] CreateFrameBuffers() { var frameBuffers = new ulong[(uint)_graphicsSurface.BufferCount]; var attachments = stackalloc ulong[1]; var frameBufferCreateInfo = new VkFramebufferCreateInfo { sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, pNext = null, flags = 0, renderPass = RenderPass, attachmentCount = 1, pAttachments = attachments, width = (uint)_graphicsSurface.Width, height = (uint)_graphicsSurface.Height, layers = 1, }; for (var i = 0; i < frameBuffers.Length; i++) { attachments[0] = SwapChainImageViews[i]; ulong frameBuffer; var result = vkCreateFramebuffer(Device, &frameBufferCreateInfo, pAllocator: null, &frameBuffer); if (result != VK_SUCCESS) { ThrowExternalException(nameof(vkCreateFramebuffer), (int)result); } frameBuffers[i] = frameBuffer; } return(frameBuffers); }
void CreateFramebuffers() { if (swapchainFramebuffers != null) { foreach (var fb in swapchainFramebuffers) { VK.DestroyFramebuffer(device, fb, alloc); } } swapchainFramebuffers = new List <VkFramebuffer>(swapchainImageViews.Count); for (int i = 0; i < swapchainImageViews.Count; i++) { var attachments = new Native <VkImageView>(swapchainImageViews[i]); var info = new VkFramebufferCreateInfo(); info.sType = CSGL.Vulkan.VkStructureType.FramebufferCreateInfo; info.renderPass = renderPass; info.attachmentCount = 1; info.pAttachments = attachments.Address; info.width = swapchainExtent.width; info.height = swapchainExtent.height; info.layers = 1; VkFramebuffer temp; var result = VK.CreateFramebuffer(device, ref info, alloc, out temp); swapchainFramebuffers.Add(temp); attachments.Dispose(); } }
protected virtual void SetupFrameBuffer() { var attachments = new VkImageView[2]; // Depth/Stencil attachment is the same for all frame buffers attachments[1] = DepthStencil.View; VkFramebufferCreateInfo frameBufferCreateInfo = new VkFramebufferCreateInfo(); frameBufferCreateInfo.sType = FramebufferCreateInfo; frameBufferCreateInfo.renderPass = renderPass; frameBufferCreateInfo.attachments = attachments; frameBufferCreateInfo.width = width; frameBufferCreateInfo.height = height; frameBufferCreateInfo.layers = 1; // Create frame buffers for every swap chain image frameBuffers = new VkFramebuffer[Swapchain.ImageCount]; for (uint i = 0; i < frameBuffers.Length; i++) { attachments[0] = Swapchain.Buffers[i].View; frameBufferCreateInfo.attachments = attachments; VkFramebuffer framebuffer; vkCreateFramebuffer(device, &frameBufferCreateInfo, null, &framebuffer); frameBuffers[i] = framebuffer; } }
public void CreateFrameBuffers() { VkImageView[] SwapChainImageViews = SwapChain.swapChain_image_views; framebuffers = new VkFramebuffer[SwapChainImageViews.Length]; for (uint i = 0; i < SwapChainImageViews.Length; i++) { #pragma warning disable CA2014 VkImageView *attachments = stackalloc VkImageView[2] { SwapChainImageViews[i], SwapChain.DepthStencil.image_view, }; #pragma warning restore CA2014 VkFramebufferCreateInfo frameBufferInfo = new VkFramebufferCreateInfo() { sType = VkStructureType.FramebufferCreateInfo, pNext = null, flags = VkFramebufferCreateFlags.None, renderPass = renderPass, attachmentCount = 2, pAttachments = attachments, width = (uint)SwapChain.Width, height = (uint)SwapChain.Height, layers = 1, }; vkCreateFramebuffer(NativeDevice.handle, &frameBufferInfo, null, out framebuffers[i]); } }
protected virtual void SetupFrameBuffer() { using (NativeList <VkImageView> attachments = new NativeList <VkImageView>(2)) { attachments.Count = 2; // Depth/Stencil attachment is the same for all frame buffers attachments[1] = DepthStencil.View; VkFramebufferCreateInfo frameBufferCreateInfo = VkFramebufferCreateInfo.New(); frameBufferCreateInfo.renderPass = renderPass; frameBufferCreateInfo.attachmentCount = 2; frameBufferCreateInfo.pAttachments = (VkImageView *)attachments.Data; frameBufferCreateInfo.width = width; frameBufferCreateInfo.height = height; frameBufferCreateInfo.layers = 1; // Create frame buffers for every swap chain image frameBuffers.Count = (Swapchain.ImageCount); for (uint i = 0; i < frameBuffers.Count; i++) { attachments[0] = Swapchain.Buffers[i].View; Util.CheckResult(vkCreateFramebuffer(device, ref frameBufferCreateInfo, null, (VkFramebuffer *)Unsafe.AsPointer(ref frameBuffers[i]))); } } }
void CreateFramebuffer(FramebufferCreateInfo mInfo) { VkFramebufferCreateInfo info = new VkFramebufferCreateInfo(); info.sType = VkStructureType.FramebufferCreateInfo; info.renderPass = mInfo.renderPass.Native; var attachmentsMarshalled = new NativeArray <VkImageView>(mInfo.attachments.Count); for (int i = 0; i < mInfo.attachments.Count; i++) { attachmentsMarshalled[i] = mInfo.attachments[i].Native; } info.attachmentCount = (uint)mInfo.attachments.Count; info.pAttachments = attachmentsMarshalled.Address; info.width = mInfo.width; info.height = mInfo.height; info.layers = mInfo.layers; using (attachmentsMarshalled) { var result = Device.Commands.createFramebuffer(Device.Native, ref info, Device.Instance.AllocationCallbacks, out framebuffer); if (result != VkResult.Success) { throw new FramebufferException(string.Format("Error creating framebuffer: {0}", result)); } } RenderPass = mInfo.renderPass; Width = mInfo.width; Height = mInfo.height; Layers = mInfo.layers; }
void CreateFramebuffers() { if (swapchainFramebuffers != null) { foreach (var fb in swapchainFramebuffers) { fb.Dispose(); } } swapchainFramebuffers = new List <VkFramebuffer>(swapchainImageViews.Count); for (int i = 0; i < swapchainImageViews.Count; i++) { var attachments = new List <VkImageView> { swapchainImageViews[i] }; var info = new VkFramebufferCreateInfo(); info.renderPass = renderPass; info.attachments = attachments; info.width = swapchainExtent.width; info.height = swapchainExtent.height; info.layers = 1; swapchainFramebuffers.Add(new VkFramebuffer(device, info)); } }
public Framebuffer(RenderPass pass, VkExtent2D size, uint layers, IEnumerable <ImageView> views) { Size = size; Device = pass.Device; var imageArray = views.Select(x => { x.AssertValid(); return(x.Handle); }).ToArray(); unsafe { fixed(VkImageView *view = imageArray) { var info = new VkFramebufferCreateInfo() { SType = VkStructureType.FramebufferCreateInfo, Flags = 0, PNext = IntPtr.Zero, RenderPass = pass.Handle, Width = size.Width, Height = size.Height, Layers = layers, AttachmentCount = (uint)imageArray.Length, PAttachments = view }; Handle = Device.Handle.CreateFramebuffer(&info, Instance.AllocationCallbacks); } } }
private void CreateFrameBuffers() { vkSwapChainFramebuffers = new VkFramebuffer[vkSwapChainImageViews.Length]; for (int i = 0; i < vkSwapChainImageViews.Length; i++) { var attachments = stackalloc VkImageView[] { vkSwapChainImageViews[i] }; var frameBufferInfo = new VkFramebufferCreateInfo() { sType = VkStructureType.VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, renderPass = vkRenderPass, attachmentCount = 1, pAttachments = attachments, width = vkSwapChainExtent.width, height = vkSwapChainExtent.height, layers = 1, }; VkFramebuffer newFrameBuffer; var result = VulkanNative.vkCreateFramebuffer(vkDevice, &frameBufferInfo, null, &newFrameBuffer); vkSwapChainFramebuffers[i] = newFrameBuffer; Helpers.CheckErrors(result); } }
private void CreateFramebuffers() { this.swapChainFramebuffers = new VkFramebuffer[this.swapChainImageViews.Length]; for (int i = 0; i < this.swapChainImageViews.Length; i++) { VkFramebufferCreateInfo framebufferInfo = new VkFramebufferCreateInfo(); framebufferInfo.sType = VkStructureType.VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; framebufferInfo.renderPass = renderPass; framebufferInfo.attachmentCount = 1; fixed(VkImageView *attachments = &swapChainImageViews[i]) { framebufferInfo.pAttachments = attachments; } framebufferInfo.width = swapChainExtent.width; framebufferInfo.height = swapChainExtent.height; framebufferInfo.layers = 1; fixed(VkFramebuffer *swapChainFramebufferPtr = &this.swapChainFramebuffers[i]) { Helpers.CheckErrors(VulkanNative.vkCreateFramebuffer(device, &framebufferInfo, null, swapChainFramebufferPtr)); } } }
public unsafe Framebuffer( RenderPass renderPass, uint width, uint height, uint layers = 1 ) { _width = width; _height = height; _device = renderPass.Device; _renderPass = renderPass; _images = new List <Image>(); _imageViews = new List <ImageView>(); foreach (var attachment in renderPass.Attachments) { var img = new Image( _device, width, height, attachment.Format, attachment.ImageUsageFlags, 1 ); _images.Add(img); _imageViews.Add(new ImageView( img, attachment.ImageAspectFlags )); } var attachments = new NativeList <VkImageView>(); foreach (var view in _imageViews) { attachments.Add(view.Handle); } var framebufferCreateInfo = new VkFramebufferCreateInfo { sType = VkStructureType.FramebufferCreateInfo, renderPass = renderPass.Handle, attachmentCount = attachments.Count, pAttachments = (VkImageView *)attachments.Data.ToPointer(), width = width, height = height, layers = layers }; VkFramebuffer framebuffer; if (VulkanNative.vkCreateFramebuffer( _device.Handle, &framebufferCreateInfo, null, &framebuffer ) != VkResult.Success) { throw new Exception("failed to create framebuffer"); } _handle = framebuffer; }
public Framebuffer(Device dev, VkFramebufferCreateInfo info) { Device = dev; Size = new VkExtent2D() { Width = info.Width, Height = info.Height }; unsafe { Handle = dev.Handle.CreateFramebuffer(&info, Instance.AllocationCallbacks); } }
protected VkFramebuffer[] CreateFramebuffers(VkDevice device, VkImage[] images, VkSurfaceFormatKHR surfaceFormat, VkRenderPass renderPass, VkSurfaceCapabilitiesKHR surfaceCapabilities) { var displayViews = new VkImageView[images.Length]; var viewInfo = new VkImageViewCreateInfo { sType = VkStructureType.ImageViewCreateInfo }; viewInfo.viewType = VkImageViewType._2d; viewInfo.format = surfaceFormat.format; viewInfo.components = new VkComponentMapping { r = VkComponentSwizzle.R, g = VkComponentSwizzle.G, b = VkComponentSwizzle.B, a = VkComponentSwizzle.A }; viewInfo.subresourceRange = new VkImageSubresourceRange { aspectMask = VkImageAspectFlagBits.Color, levelCount = 1, layerCount = 1 }; for (int i = 0; i < images.Length; i++) { viewInfo.image = images[i]; //displayViews[i] = device.CreateImageView(ref info); VkImageView view; vkAPI.vkCreateImageView(device, &viewInfo, null, &view).Check(); displayViews[i] = view; } var framebuffers = new VkFramebuffer[images.Length]; var fbInfo = new VkFramebufferCreateInfo { sType = VkStructureType.FramebufferCreateInfo }; fbInfo.layers = 1; fbInfo.renderPass = renderPass; fbInfo.width = surfaceCapabilities.currentExtent.width; fbInfo.height = surfaceCapabilities.currentExtent.height; for (int i = 0; i < images.Length; i++) { fbInfo.attachments = displayViews[i]; //framebuffers[i] = device.CreateFramebuffer(ref info); VkFramebuffer framebuffer; vkAPI.vkCreateFramebuffer(device, &fbInfo, null, &framebuffer).Check(); framebuffers[i] = framebuffer; } fbInfo.Free(); return(framebuffers); }
private void CreateFramebuffers() { swapChainFramebuffers = swapChainImageViews.Select((v, i) => { var createInfo = new VkFramebufferCreateInfo { RenderPass = renderPass, Attachments = new[] { v }, Width = swapChainExtent.Width, Height = swapChainExtent.Height, Layers = 1 }; return(device.CreateFramebuffer(createInfo, null).Object); }).ToArray(); }
unsafe void Create(RenderPass renderPass, uint width, uint height, uint layers, ReadOnlySpan <VkImageView> attachments, VkFramebufferCreateFlags flags = 0) { fixed(VkImageView *attachmentsPtr = attachments) { var framebufferCreateInfo = new VkFramebufferCreateInfo { sType = VkStructureType.FramebufferCreateInfo, flags = flags, renderPass = renderPass, attachmentCount = (uint)attachments.Length, pAttachments = attachmentsPtr, width = width, height = height, layers = layers }; handle = Device.CreateFramebuffer(ref framebufferCreateInfo); }; }
private void Setup() { using (NativeList <VkImageView> attachments = new NativeList <VkImageView>(2)) { attachments.Count = 2; // Depth/Stencil attachment is the same for all frame buffers attachments[1] = depthStencil.vkView; VkFramebufferCreateInfo frameBufferCreateInfo = VkFramebufferCreateInfo.New(); frameBufferCreateInfo.renderPass = renderPass.vkRenderPass; frameBufferCreateInfo.attachmentCount = 2; frameBufferCreateInfo.pAttachments = (VkImageView *)attachments.Data; frameBufferCreateInfo.width = swapchain.width; frameBufferCreateInfo.height = swapchain.height; frameBufferCreateInfo.layers = 1; attachments[0] = swapchain.vkSwapchain.Buffers[swapchainImageIndex].View; Util.CheckResult(vkCreateFramebuffer(device.device, ref frameBufferCreateInfo, null, out vkFrameBuffer)); } disposed = false; }
private VkFramebuffer CreateVulkanFramebuffer() { VkFramebuffer vulkanFramebuffer; var graphicsDevice = VulkanGraphicsDevice; var graphicsSurface = graphicsDevice.GraphicsSurface; var swapChainImageView = VulkanSwapChainImageView; var frameBufferCreateInfo = new VkFramebufferCreateInfo { sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, renderPass = graphicsDevice.VulkanRenderPass, attachmentCount = 1, pAttachments = (ulong *)&swapChainImageView, width = (uint)graphicsSurface.Width, height = (uint)graphicsSurface.Height, layers = 1, }; ThrowExternalExceptionIfNotSuccess(nameof(vkCreateFramebuffer), vkCreateFramebuffer(graphicsDevice.VulkanDevice, &frameBufferCreateInfo, pAllocator: null, (ulong *)&vulkanFramebuffer)); return(vulkanFramebuffer); }
internal void CreateFrameBuffers() { var SwapChainImageViews = NativeDevice.NativeSwapChain.SwapChainImageViews; SwapChainFramebuffers = new VkFramebuffer[SwapChainImageViews.Length]; for (uint i = 0; i < SwapChainImageViews.Length; i++) { VkFramebufferCreateInfo frameBufferInfo = new VkFramebufferCreateInfo() { sType = VkStructureType.FramebufferCreateInfo, renderPass = NativeRenderPass, attachmentCount = 1, pAttachments = Interop.AllocToPointer(ref SwapChainImageViews[i]), width = (uint)NativeDevice.NativeParameters.BackBufferWidth, height = (uint)NativeDevice.NativeParameters.BackBufferWidth, layers = 1, }; vkCreateFramebuffer(NativeDevice.Device, ref frameBufferInfo, null, out SwapChainFramebuffers[i]); } }
private void createFramebuffers() { swapChainFramebuffers = new VkFramebuffer[swapChainImageViews.Length]; for (int i = 0; i < swapChainImageViews.Length; i++) { VkFramebufferCreateInfo framebufferInfo = new VkFramebufferCreateInfo(); framebufferInfo.sType = VkStructureType.VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; framebufferInfo.renderPass = renderPass; framebufferInfo.attachmentCount = 1; framebufferInfo.pAttachments = new VkImageView[] { swapChainImageViews[i] }; framebufferInfo.width = swapChainExtent.width; framebufferInfo.height = swapChainExtent.height; framebufferInfo.layers = 1; VkFramebuffer frameBuffer = null; VkResult result = Vulkan.vkCreateFramebuffer(device, framebufferInfo, null, out frameBuffer); if (result != VkResult.VK_SUCCESS) { throw Program.Throw("failed to create framebuffer!", result); } swapChainFramebuffers[i] = frameBuffer; } }
static VkFramebuffer CreateFramebuffer(VkDevice device, VkRenderPass pass, VkImageView imageView, VkImageView depthImageView) { VkImageView[] imageViews = new VkImageView[] { imageView, depthImageView }; VkFramebufferCreateInfo createInfo = VkFramebufferCreateInfo.New(); createInfo.renderPass = pass; createInfo.attachmentCount = (uint)imageViews.Length; fixed(VkImageView *ptr = imageViews) createInfo.pAttachments = ptr; createInfo.width = (uint)width; createInfo.height = (uint)height; createInfo.layers = 1; VkFramebuffer framebuffer = VkFramebuffer.Null; Assert(vkCreateFramebuffer(device, &createInfo, null, &framebuffer)); return(framebuffer); }
public VkFramebuffer(VkGraphicsDevice gd, ref FramebufferDescription description, bool isPresented) : base(description.DepthTarget, description.ColorTargets) { _gd = gd; VkRenderPassCreateInfo renderPassCI = VkRenderPassCreateInfo.New(); StackList <VkAttachmentDescription> attachments = new StackList <VkAttachmentDescription>(); uint colorAttachmentCount = (uint)ColorTargets.Count; StackList <VkAttachmentReference> colorAttachmentRefs = new StackList <VkAttachmentReference>(); for (int i = 0; i < colorAttachmentCount; i++) { VkTexture vkColorTex = Util.AssertSubtype <Texture, VkTexture>(ColorTargets[i].Target); VkAttachmentDescription colorAttachmentDesc = new VkAttachmentDescription(); colorAttachmentDesc.format = vkColorTex.VkFormat; colorAttachmentDesc.samples = vkColorTex.VkSampleCount; colorAttachmentDesc.loadOp = VkAttachmentLoadOp.DontCare; colorAttachmentDesc.storeOp = VkAttachmentStoreOp.Store; colorAttachmentDesc.stencilLoadOp = VkAttachmentLoadOp.DontCare; colorAttachmentDesc.stencilStoreOp = VkAttachmentStoreOp.DontCare; colorAttachmentDesc.initialLayout = VkImageLayout.Undefined; colorAttachmentDesc.finalLayout = VkImageLayout.ColorAttachmentOptimal; attachments.Add(colorAttachmentDesc); VkAttachmentReference colorAttachmentRef = new VkAttachmentReference(); colorAttachmentRef.attachment = (uint)i; colorAttachmentRef.layout = VkImageLayout.ColorAttachmentOptimal; colorAttachmentRefs.Add(colorAttachmentRef); } VkAttachmentDescription depthAttachmentDesc = new VkAttachmentDescription(); VkAttachmentReference depthAttachmentRef = new VkAttachmentReference(); if (DepthTarget != null) { VkTexture vkDepthTex = Util.AssertSubtype <Texture, VkTexture>(DepthTarget.Value.Target); bool hasStencil = FormatHelpers.IsStencilFormat(vkDepthTex.Format); depthAttachmentDesc.format = vkDepthTex.VkFormat; depthAttachmentDesc.samples = vkDepthTex.VkSampleCount; depthAttachmentDesc.loadOp = VkAttachmentLoadOp.DontCare; depthAttachmentDesc.storeOp = VkAttachmentStoreOp.Store; depthAttachmentDesc.stencilLoadOp = VkAttachmentLoadOp.DontCare; depthAttachmentDesc.stencilStoreOp = hasStencil ? VkAttachmentStoreOp.Store : VkAttachmentStoreOp.DontCare; depthAttachmentDesc.initialLayout = VkImageLayout.Undefined; depthAttachmentDesc.finalLayout = VkImageLayout.DepthStencilAttachmentOptimal; depthAttachmentRef.attachment = (uint)description.ColorTargets.Length; depthAttachmentRef.layout = VkImageLayout.DepthStencilAttachmentOptimal; } VkSubpassDescription subpass = new VkSubpassDescription(); subpass.pipelineBindPoint = VkPipelineBindPoint.Graphics; if (ColorTargets.Count > 0) { subpass.colorAttachmentCount = colorAttachmentCount; subpass.pColorAttachments = (VkAttachmentReference *)colorAttachmentRefs.Data; } if (DepthTarget != null) { subpass.pDepthStencilAttachment = &depthAttachmentRef; attachments.Add(depthAttachmentDesc); } VkSubpassDependency subpassDependency = new VkSubpassDependency(); subpassDependency.srcSubpass = SubpassExternal; subpassDependency.srcStageMask = VkPipelineStageFlags.ColorAttachmentOutput; subpassDependency.dstStageMask = VkPipelineStageFlags.ColorAttachmentOutput; subpassDependency.dstAccessMask = VkAccessFlags.ColorAttachmentRead | VkAccessFlags.ColorAttachmentWrite; if (DepthTarget != null) { subpassDependency.dstAccessMask |= VkAccessFlags.DepthStencilAttachmentRead | VkAccessFlags.DepthStencilAttachmentWrite; } renderPassCI.attachmentCount = attachments.Count; renderPassCI.pAttachments = (VkAttachmentDescription *)attachments.Data; renderPassCI.subpassCount = 1; renderPassCI.pSubpasses = &subpass; renderPassCI.dependencyCount = 1; renderPassCI.pDependencies = &subpassDependency; VkResult creationResult = vkCreateRenderPass(_gd.Device, ref renderPassCI, null, out _renderPassNoClear); CheckResult(creationResult); for (int i = 0; i < colorAttachmentCount; i++) { attachments[i].loadOp = VkAttachmentLoadOp.Load; attachments[i].initialLayout = VkImageLayout.ColorAttachmentOptimal; } if (DepthTarget != null) { attachments[attachments.Count - 1].loadOp = VkAttachmentLoadOp.Load; attachments[attachments.Count - 1].initialLayout = VkImageLayout.DepthStencilAttachmentOptimal; bool hasStencil = FormatHelpers.IsStencilFormat(DepthTarget.Value.Target.Format); if (hasStencil) { attachments[attachments.Count - 1].stencilLoadOp = VkAttachmentLoadOp.Load; } } creationResult = vkCreateRenderPass(_gd.Device, ref renderPassCI, null, out _renderPassNoClearLoad); CheckResult(creationResult); // Load version if (DepthTarget != null) { attachments[attachments.Count - 1].loadOp = VkAttachmentLoadOp.Clear; attachments[attachments.Count - 1].initialLayout = VkImageLayout.Undefined; bool hasStencil = FormatHelpers.IsStencilFormat(DepthTarget.Value.Target.Format); if (hasStencil) { attachments[attachments.Count - 1].stencilLoadOp = VkAttachmentLoadOp.Clear; } } for (int i = 0; i < colorAttachmentCount; i++) { attachments[i].loadOp = VkAttachmentLoadOp.Clear; attachments[i].initialLayout = VkImageLayout.Undefined; } creationResult = vkCreateRenderPass(_gd.Device, ref renderPassCI, null, out _renderPassClear); CheckResult(creationResult); VkFramebufferCreateInfo fbCI = VkFramebufferCreateInfo.New(); uint fbAttachmentsCount = (uint)description.ColorTargets.Length; if (description.DepthTarget != null) { fbAttachmentsCount += 1; } VkImageView *fbAttachments = stackalloc VkImageView[(int)fbAttachmentsCount]; for (int i = 0; i < colorAttachmentCount; i++) { VkTexture vkColorTarget = Util.AssertSubtype <Texture, VkTexture>(description.ColorTargets[i].Target); VkImageViewCreateInfo imageViewCI = VkImageViewCreateInfo.New(); imageViewCI.image = vkColorTarget.OptimalDeviceImage; imageViewCI.format = vkColorTarget.VkFormat; imageViewCI.viewType = VkImageViewType.Image2D; imageViewCI.subresourceRange = new VkImageSubresourceRange( VkImageAspectFlags.Color, description.ColorTargets[i].MipLevel, 1, description.ColorTargets[i].ArrayLayer, 1); VkImageView *dest = (fbAttachments + i); VkResult result = vkCreateImageView(_gd.Device, ref imageViewCI, null, dest); CheckResult(result); _attachmentViews.Add(*dest); } // Depth if (description.DepthTarget != null) { VkTexture vkDepthTarget = Util.AssertSubtype <Texture, VkTexture>(description.DepthTarget.Value.Target); bool hasStencil = FormatHelpers.IsStencilFormat(vkDepthTarget.Format); VkImageViewCreateInfo depthViewCI = VkImageViewCreateInfo.New(); depthViewCI.image = vkDepthTarget.OptimalDeviceImage; depthViewCI.format = vkDepthTarget.VkFormat; depthViewCI.viewType = description.DepthTarget.Value.Target.ArrayLayers == 1 ? VkImageViewType.Image2D : VkImageViewType.Image2DArray; depthViewCI.subresourceRange = new VkImageSubresourceRange( hasStencil ? VkImageAspectFlags.Depth | VkImageAspectFlags.Stencil : VkImageAspectFlags.Depth, description.DepthTarget.Value.MipLevel, 1, description.DepthTarget.Value.ArrayLayer, 1); VkImageView *dest = (fbAttachments + (fbAttachmentsCount - 1)); VkResult result = vkCreateImageView(_gd.Device, ref depthViewCI, null, dest); CheckResult(result); _attachmentViews.Add(*dest); } Texture dimTex; uint mipLevel; if (ColorTargets.Count > 0) { dimTex = ColorTargets[0].Target; mipLevel = ColorTargets[0].MipLevel; } else { Debug.Assert(DepthTarget != null); dimTex = DepthTarget.Value.Target; mipLevel = DepthTarget.Value.MipLevel; } Util.GetMipDimensions( dimTex, mipLevel, out uint mipWidth, out uint mipHeight, out _); fbCI.width = mipWidth; fbCI.height = mipHeight; fbCI.attachmentCount = fbAttachmentsCount; fbCI.pAttachments = fbAttachments; fbCI.layers = 1; fbCI.renderPass = _renderPassNoClear; creationResult = vkCreateFramebuffer(_gd.Device, ref fbCI, null, out _deviceFramebuffer); CheckResult(creationResult); if (DepthTarget != null) { AttachmentCount += 1; } AttachmentCount += (uint)ColorTargets.Count; }
public static VkFramebuffer CreateFramebuffer(ref VkFramebufferCreateInfo framebufferCreateInfo) { VulkanUtil.CheckResult(vkCreateFramebuffer(device, Utilities.AsPtr(ref framebufferCreateInfo), null, out VkFramebuffer framebuffer)); return(framebuffer); }
public static VkResult vkCreateFramebuffer(VkDevice device, VkFramebufferCreateInfo pCreateInfo, VkAllocationCallbacks pAllocator, out VkFramebuffer pFrameBuffer) { VkPreconditions.CheckNull(device, nameof(device)); return(GetDevice(device).CreateFrameBuffer(pCreateInfo, out pFrameBuffer)); }
// Setup the offscreen framebuffer for rendering the blurred scene // The color attachment of this framebuffer will then be used to sample frame in the fragment shader of the final pass void prepareOffscreen() { offscreenPass.width = FB_DIM; offscreenPass.height = FB_DIM; // Find a suitable depth format VkFormat fbDepthFormat; VkBool32 validDepthFormat = Tools.getSupportedDepthFormat(physicalDevice, &fbDepthFormat); Debug.Assert(validDepthFormat); // Color attachment VkImageCreateInfo image = Initializers.imageCreateInfo(); image.imageType = VK_IMAGE_TYPE_2D; image.format = FB_COLOR_FORMAT; image.extent.width = (uint)offscreenPass.width; image.extent.height = (uint)offscreenPass.height; image.extent.depth = 1; image.mipLevels = 1; image.arrayLayers = 1; image.samples = VK_SAMPLE_COUNT_1_BIT; image.tiling = VK_IMAGE_TILING_OPTIMAL; // We will sample directly from the color attachment image.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; VkMemoryAllocateInfo memAlloc = Initializers.memoryAllocateInfo(); VkMemoryRequirements memReqs; Util.CheckResult(vkCreateImage(device, &image, null, out offscreenPass.color.image)); vkGetImageMemoryRequirements(device, offscreenPass.color.image, &memReqs); memAlloc.allocationSize = memReqs.size; memAlloc.memoryTypeIndex = vulkanDevice.getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); Util.CheckResult(vkAllocateMemory(device, &memAlloc, null, out offscreenPass.color.mem)); Util.CheckResult(vkBindImageMemory(device, offscreenPass.color.image, offscreenPass.color.mem, 0)); VkImageViewCreateInfo colorImageView = Initializers.imageViewCreateInfo(); colorImageView.viewType = VK_IMAGE_VIEW_TYPE_2D; colorImageView.format = FB_COLOR_FORMAT; colorImageView.subresourceRange = new VkImageSubresourceRange(); colorImageView.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; colorImageView.subresourceRange.baseMipLevel = 0; colorImageView.subresourceRange.levelCount = 1; colorImageView.subresourceRange.baseArrayLayer = 0; colorImageView.subresourceRange.layerCount = 1; colorImageView.image = offscreenPass.color.image; Util.CheckResult(vkCreateImageView(device, &colorImageView, null, out offscreenPass.color.view)); // Create sampler to sample from the attachment in the fragment shader VkSamplerCreateInfo samplerInfo = Initializers.samplerCreateInfo(); samplerInfo.magFilter = VK_FILTER_LINEAR; samplerInfo.minFilter = VK_FILTER_LINEAR; samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR; samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; samplerInfo.addressModeV = samplerInfo.addressModeU; samplerInfo.addressModeW = samplerInfo.addressModeU; samplerInfo.mipLodBias = 0.0f; samplerInfo.maxAnisotropy = 0; samplerInfo.minLod = 0.0f; samplerInfo.maxLod = 1.0f; samplerInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE; Util.CheckResult(vkCreateSampler(device, &samplerInfo, null, out offscreenPass.sampler)); // Depth stencil attachment image.format = fbDepthFormat; image.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; Util.CheckResult(vkCreateImage(device, &image, null, out offscreenPass.depth.image)); vkGetImageMemoryRequirements(device, offscreenPass.depth.image, &memReqs); memAlloc.allocationSize = memReqs.size; memAlloc.memoryTypeIndex = vulkanDevice.getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); Util.CheckResult(vkAllocateMemory(device, &memAlloc, null, out offscreenPass.depth.mem)); Util.CheckResult(vkBindImageMemory(device, offscreenPass.depth.image, offscreenPass.depth.mem, 0)); VkImageViewCreateInfo depthStencilView = Initializers.imageViewCreateInfo(); depthStencilView.viewType = VK_IMAGE_VIEW_TYPE_2D; depthStencilView.format = fbDepthFormat; depthStencilView.flags = 0; depthStencilView.subresourceRange = new VkImageSubresourceRange(); depthStencilView.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT; depthStencilView.subresourceRange.baseMipLevel = 0; depthStencilView.subresourceRange.levelCount = 1; depthStencilView.subresourceRange.baseArrayLayer = 0; depthStencilView.subresourceRange.layerCount = 1; depthStencilView.image = offscreenPass.depth.image; Util.CheckResult(vkCreateImageView(device, &depthStencilView, null, out offscreenPass.depth.view)); // Create a separate render pass for the offscreen rendering as it may differ from the one used for scene rendering FixedArray2 <VkAttachmentDescription> attchmentDescriptions = new FixedArray2 <VkAttachmentDescription>(); // Color attachment attchmentDescriptions.First.format = FB_COLOR_FORMAT; attchmentDescriptions.First.samples = VK_SAMPLE_COUNT_1_BIT; attchmentDescriptions.First.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; attchmentDescriptions.First.storeOp = VK_ATTACHMENT_STORE_OP_STORE; attchmentDescriptions.First.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; attchmentDescriptions.First.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; attchmentDescriptions.First.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; attchmentDescriptions.First.finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; // Depth attachment attchmentDescriptions.Second.format = fbDepthFormat; attchmentDescriptions.Second.samples = VK_SAMPLE_COUNT_1_BIT; attchmentDescriptions.Second.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; attchmentDescriptions.Second.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; attchmentDescriptions.Second.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; attchmentDescriptions.Second.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; attchmentDescriptions.Second.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; attchmentDescriptions.Second.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; VkAttachmentReference colorReference = new VkAttachmentReference { attachment = 0, layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL }; VkAttachmentReference depthReference = new VkAttachmentReference { attachment = 1, layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL }; VkSubpassDescription subpassDescription = new VkSubpassDescription(); subpassDescription.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; subpassDescription.colorAttachmentCount = 1; subpassDescription.pColorAttachments = &colorReference; subpassDescription.pDepthStencilAttachment = &depthReference; // Use subpass dependencies for layout transitions FixedArray2 <VkSubpassDependency> dependencies = new FixedArray2 <VkSubpassDependency>(); dependencies.First.srcSubpass = VK_SUBPASS_EXTERNAL; dependencies.First.dstSubpass = 0; dependencies.First.srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; dependencies.First.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; dependencies.First.srcAccessMask = VK_ACCESS_MEMORY_READ_BIT; dependencies.First.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; dependencies.First.dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; dependencies.Second.srcSubpass = 0; dependencies.Second.dstSubpass = VK_SUBPASS_EXTERNAL; dependencies.Second.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; dependencies.Second.dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; dependencies.Second.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; dependencies.Second.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT; dependencies.Second.dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; // Create the actual renderpass VkRenderPassCreateInfo renderPassInfo = VkRenderPassCreateInfo.New(); renderPassInfo.attachmentCount = attchmentDescriptions.Count; renderPassInfo.pAttachments = &attchmentDescriptions.First; renderPassInfo.subpassCount = 1; renderPassInfo.pSubpasses = &subpassDescription; renderPassInfo.dependencyCount = dependencies.Count; renderPassInfo.pDependencies = &dependencies.First; Util.CheckResult(vkCreateRenderPass(device, &renderPassInfo, null, out offscreenPass.renderPass)); FixedArray2 <VkImageView> attachments = new FixedArray2 <VkImageView>(offscreenPass.color.view, offscreenPass.depth.view); VkFramebufferCreateInfo fbufCreateInfo = Initializers.framebufferCreateInfo(); fbufCreateInfo.renderPass = offscreenPass.renderPass; fbufCreateInfo.attachmentCount = 2; fbufCreateInfo.pAttachments = &attachments.First; fbufCreateInfo.width = (uint)offscreenPass.width; fbufCreateInfo.height = (uint)offscreenPass.height; fbufCreateInfo.layers = 1; Util.CheckResult(vkCreateFramebuffer(device, &fbufCreateInfo, null, out offscreenPass.frameBuffer)); // Fill a descriptor for later use in a descriptor set offscreenPass.descriptor.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; offscreenPass.descriptor.imageView = offscreenPass.color.view; offscreenPass.descriptor.sampler = offscreenPass.sampler; }
public static extern VkResult CreateFramebuffer( VkDevice device, ref VkFramebufferCreateInfo pCreateInfo, IntPtr pAllocator, out VkFramebuffer pFramebuffer );
public override VkResult CreateFrameBuffer(VkFramebufferCreateInfo frameBufferCreateInfo, out VkFramebuffer frameBuffer) { frameBuffer = new DummyFramebuffer(this, frameBufferCreateInfo); return(VkResult.VK_SUCCESS); }
public abstract VkResult CreateFrameBuffer(VkFramebufferCreateInfo frameBufferCreateInfo, out VkFramebuffer frameBuffer);
public void Build(int deviceIndex) { if (!locked) { unsafe { //Setup framebuffer var attachmentCnt = (ColorAttachments == null ? 0 : ColorAttachments.Length) + (DepthAttachment == null ? 0 : 1); var attachments = stackalloc IntPtr[attachmentCnt]; if (ColorAttachments != null) { for (int i = 0; i < ColorAttachments.Length; i++) { attachments[i] = ColorAttachments[i].hndl; if (ColorAttachments[i].Width != Width) { throw new Exception(); } if (ColorAttachments[i].Height != Height) { throw new Exception(); } } } if (DepthAttachment != null) { attachments[attachmentCnt - 1] = DepthAttachment.hndl; } var framebufferInfo = new VkFramebufferCreateInfo() { sType = VkStructureType.StructureTypeFramebufferCreateInfo, attachmentCount = (uint)attachmentCnt, pAttachments = attachments, renderPass = RenderPass.hndl, width = Width, height = Height, layers = 1 }; IntPtr framebuffer_l = IntPtr.Zero; if (vkCreateFramebuffer(GraphicsDevice.GetDeviceInfo(deviceIndex).Device, framebufferInfo.Pointer(), null, &framebuffer_l) != VkResult.Success) { throw new Exception("Failed to create framebuffer"); } hndl = framebuffer_l; if (GraphicsDevice.EnableValidation) { var objName = new VkDebugUtilsObjectNameInfoEXT() { sType = VkStructureType.StructureTypeDebugUtilsObjectNameInfoExt, pObjectName = Name, objectType = VkObjectType.ObjectTypeFramebuffer, objectHandle = (ulong)hndl }; GraphicsDevice.SetDebugUtilsObjectNameEXT(GraphicsDevice.GetDeviceInfo(deviceIndex).Device, objName.Pointer()); } locked = true; } } else { throw new Exception("Framebuffer is locked."); } }