Kale
Loading...
Searching...
No Matches
FrameBuffer.cpp
Go to the documentation of this file.
1/*
2 Copyright 2022 Rishi Challa
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#ifdef KALE_VULKAN
18
19#include "FrameBuffer.hpp"
20
23
24#ifdef KALE_DEBUG
25
26#include <cassert>
27
28#endif
29
30using namespace Kale;
31using namespace Kale::Vulkan;
32
37 // Empty Body
38}
39
44FrameBuffer::FrameBuffer(Renderer& renderer, const Vector2ui32& size) : ChildResource(renderer) {
45
46 // Create the image
47 const vk::ImageCreateInfo imageCreateInfo(vk::ImageCreateFlags(), vk::ImageType::e2D, Core::swapchain.format,
48 {size.x, size.y, 1}, 1, 1, vk::SampleCountFlagBits::e1, vk::ImageTiling::eOptimal,
49 vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eSampled);
50 image = Core::device.logicalDevice.get().createImageUnique(imageCreateInfo);
51
52 // Allocate memory for the image and bind it
53 imageMemory.init(Core::device, Core::device.logicalDevice.get().getImageMemoryRequirements(image.get()),
54 vk::MemoryPropertyFlagBits::eDeviceLocal);
55 Core::device.logicalDevice.get().bindImageMemory(image.get(), imageMemory.deviceMemory.get(), imageMemory.memoryInfo.alignment);
56
57 // Create the image view
58 const vk::ImageSubresourceRange range(vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1);
59 const vk::ImageViewCreateInfo imageViewCreateInfo(vk::ImageViewCreateFlags(), image.get(), vk::ImageViewType::e2D,
60 Core::swapchain.format, vk::ComponentMapping(), range);
61 imageView = Core::device.logicalDevice.get().createImageViewUnique(imageViewCreateInfo);
62
63 // Sets up the frame buffer
64 createFrameBuffer(imageView, {size.x, size.y}, renderer.renderPass);
65}
66
71void FrameBuffer::init(Renderer& renderer, const Vector2ui32& size) {
72
73 ChildResource::init(renderer);
74
75 // Create the image
76 const vk::Extent3D extent(size.x, size.y, 1);
77 const vk::ImageCreateInfo imageCreateInfo(vk::ImageCreateFlags(), vk::ImageType::e2D, Core::swapchain.format, extent);
78 image = Core::device.logicalDevice.get().createImageUnique(imageCreateInfo);
79
80 // Create the image view
81 const vk::ImageSubresourceRange range(vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1);
82 const vk::ImageViewCreateInfo imageViewCreateInfo(vk::ImageViewCreateFlags(), image.get(), vk::ImageViewType::e2D,
83 Core::swapchain.format, vk::ComponentMapping(), range);
84 imageView = Core::device.logicalDevice.get().createImageViewUnique(imageViewCreateInfo);
85
86 // Sets up the frame buffer
87 createFrameBuffer(imageView, {size.x, size.y}, renderer.renderPass);
88}
89
96void FrameBuffer::createFrameBuffer(const vk::UniqueImageView& imageView, const vk::Extent2D& extent,
97 const vk::UniqueRenderPass& renderPass) {
98
99 vk::FramebufferCreateInfo createInfo(vk::FramebufferCreateFlags(), renderPass.get(), 1, &imageView.get(),
100 extent.width, extent.height, 1);
101 framebuffer = Core::device.logicalDevice->createFramebufferUnique(createInfo);
102}
103
112
125
126#endif
virtual void freeResources(bool remove=true)
virtual void init(Parent &parent)
static Device device
Definition Core.hpp:99
static SwapChain swapchain
Definition Core.hpp:104
vk::UniqueDevice logicalDevice
Definition Device.hpp:59
void init(Device &device, const vk::MemoryRequirements &requirements, vk::MemoryPropertyFlags properties)
vk::MemoryRequirements memoryInfo
vk::UniqueDeviceMemory deviceMemory
void freeResources(bool remove=true) override
void init(Renderer &renderer, const Vector2ui32 &size)
vk::UniqueImageView imageView
vk::UniqueFramebuffer framebuffer
void createFrameBuffer(const vk::UniqueImageView &imageView, const vk::Extent2D &extent, const vk::UniqueRenderPass &renderPass)
vk::UniqueRenderPass renderPass
Definition Renderer.hpp:59