Skip to content

Commit d591b16

Browse files
Improved texture views support
1 parent 06bc885 commit d591b16

File tree

2 files changed

+37
-43
lines changed

2 files changed

+37
-43
lines changed

lvk/vulkan/VulkanClasses.cpp

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,7 @@ lvk::VulkanSwapchain::VulkanSwapchain(VulkanContext& ctx, uint32_t width, uint32
11861186
.vkType_ = VK_IMAGE_TYPE_2D,
11871187
.vkImageFormat_ = surfaceFormat_.format,
11881188
.isSwapchainImage_ = true,
1189+
.isOwningVkImage_ = false,
11891190
.isDepthFormat_ = VulkanImage::isDepthFormat(surfaceFormat_.format),
11901191
.isStencilFormat_ = VulkanImage::isStencilFormat(surfaceFormat_.format),
11911192
};
@@ -3892,25 +3893,24 @@ lvk::Holder<lvk::TextureHandle> lvk::VulkanContext::createTextureView(lvk::Textu
38923893
const TextureViewDesc& desc,
38933894
const char* debugName,
38943895
Result* outResult) {
3895-
LVK_ASSERT(texture.valid());
3896-
3897-
// TODO: Texture views are still work-in-progress. Beware!
3898-
3899-
const VulkanImage* baseImage = texturesPool_.get(texture);
3900-
3901-
VulkanImage newImage = *baseImage;
3896+
if (!texture) {
3897+
LVK_ASSERT(texture.valid());
3898+
return {};
3899+
}
39023900

3903-
// drop old existing views - the baseImage owns them
3904-
memset(&newImage.imageViewStorage_, 0, sizeof(newImage.imageViewStorage_));
3905-
memset(&newImage.imageViewForFramebuffer_, 0, sizeof(newImage.imageViewForFramebuffer_));
3901+
// make a copy and make it non-owning
3902+
VulkanImage image = *texturesPool_.get(texture);
3903+
image.isOwningVkImage_ = false;
39063904

3907-
newImage.isSwapchainImage_ = true; // TODO: rename this to `isExternallyManaged` etc
3905+
// drop all existing image views - they belong to the base image
3906+
memset(&image.imageViewStorage_, 0, sizeof(image.imageViewStorage_));
3907+
memset(&image.imageViewForFramebuffer_, 0, sizeof(image.imageViewForFramebuffer_));
39083908

39093909
VkImageAspectFlags aspect = 0;
3910-
if (newImage.isDepthFormat_ || newImage.isStencilFormat_) {
3911-
if (newImage.isDepthFormat_) {
3910+
if (image.isDepthFormat_ || image.isStencilFormat_) {
3911+
if (image.isDepthFormat_) {
39123912
aspect |= VK_IMAGE_ASPECT_DEPTH_BIT;
3913-
} else if (newImage.isStencilFormat_) {
3913+
} else if (image.isStencilFormat_) {
39143914
aspect |= VK_IMAGE_ASPECT_STENCIL_BIT;
39153915
}
39163916
} else {
@@ -3941,42 +3941,35 @@ lvk::Holder<lvk::TextureHandle> lvk::VulkanContext::createTextureView(lvk::Textu
39413941
.a = VkComponentSwizzle(desc.swizzle.a),
39423942
};
39433943

3944-
newImage.imageView_ = newImage.createImageView(vkDevice_,
3945-
vkImageViewType,
3946-
newImage.vkImageFormat_,
3947-
aspect,
3948-
desc.mipLevel,
3949-
desc.numMipLevels,
3950-
desc.layer,
3951-
desc.numLayers,
3952-
mapping,
3953-
nullptr,
3954-
debugName);
3955-
3956-
if (!LVK_VERIFY(newImage.imageView_ != VK_NULL_HANDLE)) {
3944+
LVK_ASSERT_MSG(lvk::getNumImagePlanes(image.vkImageFormat_) == 1, "Unsupported multiplanar image");
3945+
3946+
image.imageView_ = image.createImageView(vkDevice_,
3947+
vkImageViewType,
3948+
image.vkImageFormat_,
3949+
aspect,
3950+
desc.mipLevel,
3951+
desc.numMipLevels,
3952+
desc.layer,
3953+
desc.numLayers,
3954+
mapping,
3955+
nullptr,
3956+
debugName);
3957+
3958+
if (!LVK_VERIFY(image.imageView_ != VK_NULL_HANDLE)) {
39573959
Result::setResult(outResult, Result::Code::RuntimeError, "Cannot create VkImageView");
39583960
return {};
39593961
}
39603962

3961-
if (newImage.vkUsageFlags_ & VK_IMAGE_USAGE_STORAGE_BIT) {
3963+
if (image.vkUsageFlags_ & VK_IMAGE_USAGE_STORAGE_BIT) {
39623964
if (!desc.swizzle.identity()) {
39633965
// use identity swizzle for storage images
3964-
newImage.imageViewStorage_ = newImage.createImageView(vkDevice_,
3965-
vkImageViewType,
3966-
newImage.vkImageFormat_,
3967-
aspect,
3968-
0,
3969-
VK_REMAINING_MIP_LEVELS,
3970-
0,
3971-
desc.numLayers,
3972-
{},
3973-
nullptr,
3974-
debugName);
3975-
LVK_ASSERT(newImage.imageViewStorage_ != VK_NULL_HANDLE);
3966+
image.imageViewStorage_ = image.createImageView(
3967+
vkDevice_, vkImageViewType, image.vkImageFormat_, aspect, 0, VK_REMAINING_MIP_LEVELS, 0, desc.numLayers, {}, nullptr, debugName);
3968+
LVK_ASSERT(image.imageViewStorage_ != VK_NULL_HANDLE);
39763969
}
39773970
}
39783971

3979-
TextureHandle handle = texturesPool_.create(std::move(newImage));
3972+
TextureHandle handle = texturesPool_.create(std::move(image));
39803973

39813974
awaitingCreation_ = true;
39823975

@@ -5034,7 +5027,7 @@ void lvk::VulkanContext::destroy(lvk::TextureHandle handle) {
50345027
}
50355028
}
50365029

5037-
if (tex->isSwapchainImage_) {
5030+
if (!tex->isOwningVkImage_) {
50385031
return;
50395032
}
50405033

@@ -5093,7 +5086,7 @@ void lvk::VulkanContext::destroy(Framebuffer& fb) {
50935086
if (handle.empty())
50945087
return;
50955088
lvk::VulkanImage* tex = texturesPool_.get(handle);
5096-
if (!tex || tex->isSwapchainImage_)
5089+
if (!tex || !tex->isOwningVkImage_)
50975090
return;
50985091
destroy(handle);
50995092
handle = {};

lvk/vulkan/VulkanClasses.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ struct VulkanImage final {
105105
VkSampleCountFlagBits vkSamples_ = VK_SAMPLE_COUNT_1_BIT;
106106
void* mappedPtr_ = nullptr;
107107
bool isSwapchainImage_ = false;
108+
bool isOwningVkImage_ = true;
108109
uint32_t numLevels_ = 1u;
109110
uint32_t numLayers_ = 1u;
110111
bool isDepthFormat_ = false;

0 commit comments

Comments
 (0)