Move sampler creation from image to resource manager and cache created samplers

This commit is contained in:
Georg Hagen
2024-07-09 12:31:52 +02:00
parent d48d60441a
commit d9a22236b4
5 changed files with 49 additions and 38 deletions

View File

@@ -8,19 +8,22 @@
namespace OpenVulkano::Vulkan
{
void Image::Init(const Device* device, const vk::ImageCreateInfo& imageCreateInfo, vk::ImageViewCreateInfo imageViewCreateInfo, const vk::MemoryPropertyFlags& memoryPropertyFlags)
void Image::Init(const Device* device, const vk::ImageCreateInfo& imageCreateInfo, vk::ImageViewCreateInfo imageViewCreateInfo, bool allocateMem, const vk::MemoryPropertyFlags& memoryPropertyFlags)
{
this->device = device->device;
image = device->device.createImage(imageCreateInfo);
format = imageCreateInfo.format;
extent = imageCreateInfo.extent;
// TODO allocate from resource manager
const vk::MemoryRequirements memRequirements = device->device.getImageMemoryRequirements(image);
size = allocSize = memRequirements.size;
const vk::MemoryAllocateInfo memAllocInfo = { allocSize, device->GetMemoryType(memRequirements.memoryTypeBits, memoryPropertyFlags) };
memory = device->device.allocateMemory(memAllocInfo);
device->device.bindImageMemory(image, memory, 0);
if (allocateMem)
{
// TODO allocate from resource manager
const vk::MemoryRequirements memRequirements = device->device.getImageMemoryRequirements(image);
size = allocSize = memRequirements.size;
const vk::MemoryAllocateInfo memAllocInfo = { allocSize, device->GetMemoryType(memRequirements.memoryTypeBits, memoryPropertyFlags) };
memory = device->device.allocateMemory(memAllocInfo);
device->device.bindImageMemory(image, memory, 0);
}
imageViewCreateInfo.image = image;
view = device->device.createImageView(imageViewCreateInfo);
@@ -49,32 +52,10 @@ namespace OpenVulkano::Vulkan
imgViewCreateInfo.subresourceRange.layerCount = VK_REMAINING_ARRAY_LAYERS;
Init(device, imgCreateInfo, imgViewCreateInfo);
CreateSampler();
}
void Image::CreateSampler()
{
vk::SamplerCreateInfo samplerCreateInfo;
samplerCreateInfo.magFilter = vk::Filter::eLinear;
samplerCreateInfo.minFilter = vk::Filter::eLinear;
samplerCreateInfo.mipmapMode = vk::SamplerMipmapMode::eLinear;
samplerCreateInfo.addressModeU = vk::SamplerAddressMode::eClampToEdge;
samplerCreateInfo.addressModeV = vk::SamplerAddressMode::eClampToEdge;
samplerCreateInfo.addressModeW = vk::SamplerAddressMode::eClampToEdge;
samplerCreateInfo.borderColor = vk::BorderColor::eFloatTransparentBlack;
samplerCreateInfo.unnormalizedCoordinates = false;
samplerCreateInfo.compareEnable = false;
sampler = this->device.createSampler(samplerCreateInfo);
}
void Image::Close()
{
if(sampler)
{
device.destroySampler(sampler);
sampler = vk::Sampler();
}
if(view)
{
device.destroyImageView(view);
@@ -87,4 +68,4 @@ namespace OpenVulkano::Vulkan
}
Buffer::Close();
}
}
}