parent
85ebf53ee1
commit
7c85f8efaa
11 changed files with 199 additions and 70 deletions
@ -0,0 +1,63 @@ |
||||
#include "image.hpp" |
||||
#include "instance.hpp" |
||||
#include "utils.h" |
||||
|
||||
|
||||
Image::Image(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, |
||||
VkImageUsageFlags usage, VkMemoryPropertyFlags properties) { |
||||
|
||||
VkImageCreateInfo imageCreateInfo {}; |
||||
imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; |
||||
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D; |
||||
imageCreateInfo.extent.width = width; |
||||
imageCreateInfo.extent.height = height; |
||||
imageCreateInfo.extent.depth = 1; |
||||
imageCreateInfo.mipLevels = 1; |
||||
imageCreateInfo.arrayLayers = 1; |
||||
imageCreateInfo.format = format; |
||||
imageCreateInfo.tiling = tiling; |
||||
imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
||||
imageCreateInfo.usage = usage; |
||||
imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT; |
||||
imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
||||
|
||||
vkCreateImage(Instance::instance->device, &imageCreateInfo, nullptr, &handle); |
||||
|
||||
VkMemoryRequirements memoryRequirements; |
||||
vkGetImageMemoryRequirements(Instance::instance->device, handle, &memoryRequirements); |
||||
|
||||
VkMemoryAllocateInfo allocateInfo {}; |
||||
allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; |
||||
allocateInfo.allocationSize = memoryRequirements.size; |
||||
allocateInfo.memoryTypeIndex = findMemoryType(memoryRequirements.memoryTypeBits, properties); |
||||
|
||||
vkAllocateMemory(Instance::instance->device, &allocateInfo, nullptr, &memory); |
||||
vkBindImageMemory(Instance::instance->device, handle, memory, 0); |
||||
} |
||||
|
||||
Image::~Image() { |
||||
vkFreeMemory(Instance::instance->device, memory, nullptr); |
||||
if (view) |
||||
vkDestroyImageView(Instance::instance->device, view, nullptr); |
||||
vkDestroyImage(Instance::instance->device, handle, nullptr); |
||||
} |
||||
|
||||
VkImageView Image::createView(VkFormat format, VkImageAspectFlags aspectFlags) { |
||||
VkImageSubresourceRange subresourceRange {}; |
||||
subresourceRange.aspectMask = aspectFlags; |
||||
subresourceRange.baseMipLevel = 0; |
||||
subresourceRange.levelCount = 1; |
||||
subresourceRange.baseArrayLayer = 0; |
||||
subresourceRange.layerCount = 1; |
||||
|
||||
VkImageViewCreateInfo createInfo {}; |
||||
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; |
||||
createInfo.format = format; |
||||
createInfo.image = handle; |
||||
createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; |
||||
createInfo.subresourceRange = subresourceRange; |
||||
|
||||
vkCreateImageView(Instance::instance->device, &createInfo, nullptr, &view); |
||||
|
||||
return view; |
||||
} |
@ -0,0 +1,19 @@ |
||||
#pragma once |
||||
|
||||
#include <vulkan/vulkan.h> |
||||
|
||||
class Instance; |
||||
|
||||
class Image { |
||||
public: |
||||
explicit Image(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, |
||||
VkImageUsageFlags usage, VkMemoryPropertyFlags properties); |
||||
~Image(); |
||||
|
||||
VkImageView createView(VkFormat format, VkImageAspectFlags aspectFlags); |
||||
|
||||
VkImage handle = VK_NULL_HANDLE; |
||||
VkImageView view = VK_NULL_HANDLE; |
||||
private: |
||||
VkDeviceMemory memory = VK_NULL_HANDLE; |
||||
}; |
@ -0,0 +1,17 @@ |
||||
#include <stdexcept> |
||||
#include "utils.h" |
||||
|
||||
#include "instance.hpp" |
||||
|
||||
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags propertyFlags) { |
||||
VkPhysicalDeviceMemoryProperties memoryProperties; |
||||
vkGetPhysicalDeviceMemoryProperties(Instance::instance->physicalDevice, &memoryProperties); |
||||
|
||||
for (uint32_t type = 0; type < memoryProperties.memoryTypeCount; type++){ |
||||
if ((typeFilter & (1 << type)) && (memoryProperties.memoryTypes[type].propertyFlags & propertyFlags)){ |
||||
return type; |
||||
} |
||||
} |
||||
|
||||
throw std::runtime_error("failed to find suitable memory type!"); |
||||
} |
@ -0,0 +1,3 @@ |
||||
#include <vulkan/vulkan_core.h> |
||||
|
||||
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags propertyFlags); |
Loading…
Reference in new issue