project-aster/aster/image.cpp

53 lines
1.8 KiB
C++

// =============================================
// Aster: image.cpp
// Copyright (c) 2020-2024 Anish Bhobe
// =============================================
#include "image.h"
#include "device.h"
void
Image::Destroy(const Device *device)
{
if (!m_Image)
return;
vmaDestroyImage(device->m_Allocator, m_Image, m_Allocation);
m_Image = nullptr;
}
void
Texture::Init(const Device *device, const vk::Extent2D extent, const bool isMipmapped, const cstr name)
{
const u32 mipLevels = isMipmapped ? 1 + Cast<u32>(floor(log2(eastl::max(extent.width, extent.height)))) : 1;
vk::ImageCreateInfo imageCreateInfo = {
.imageType = vk::ImageType::e2D,
.format = vk::Format::eR8G8B8A8Srgb,
.extent = {.width = extent.width, .height = extent.height, .depth = 1},
.mipLevels = mipLevels,
.arrayLayers = 1,
.samples = vk::SampleCountFlagBits::e1,
.tiling = vk::ImageTiling::eOptimal,
.usage = vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eTransferDst,
.sharingMode = vk::SharingMode::eExclusive,
.initialLayout = vk::ImageLayout::eUndefined,
};
constexpr VmaAllocationCreateInfo allocationCreateInfo = {
.flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT,
.usage = VMA_MEMORY_USAGE_AUTO,
};
VkImage image;
VmaAllocation allocation;
auto result = Cast<vk::Result>(vmaCreateImage(device->m_Allocator, Recast<VkImageCreateInfo *>(&imageCreateInfo),
&allocationCreateInfo, &image, &allocation, nullptr));
ERROR_IF(Failed(result), "Could not allocate buffer. Cause: {}", result) THEN_ABORT(result);
m_Image = image;
m_Allocation = allocation;
m_Extent = {extent.width, extent.height, 1};
device->SetName(m_Image, name);
}