diff --git a/client/render/image_loader.cpp b/client/render/image_loader.cpp index a23fa121..b88515d3 100644 --- a/client/render/image_loader.cpp +++ b/client/render/image_loader.cpp @@ -46,6 +46,7 @@ namespace { image_allocation allocation; vk::Image image; + vk::raii::ImageView image_view = nullptr; }; struct ktx_image_resources @@ -53,6 +54,7 @@ namespace ktxVulkanTexture ktx_texture; VkDevice device; vk::Image image; + vk::raii::ImageView image_view = nullptr; ktx_image_resources() = default; ktx_image_resources(const ktx_image_resources&) = delete; @@ -155,7 +157,6 @@ int bytes_per_pixel(vk::Format format) } // namespace image_loader::image_loader(vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, vk::raii::Queue& queue, vk::raii::CommandPool & cb_pool) : - physical_device(physical_device), device(device), queue(queue), cb_pool(cb_pool) @@ -341,7 +342,20 @@ void image_loader::do_load_raw(const void * pixels, vk::Extent3D extent, vk::For queue.submit(info, *fence); device.waitForFences(*fence, true, 1'000'000'000); - image = std::shared_ptr(r, &r->image); + r->image_view = vk::raii::ImageView{device, vk::ImageViewCreateInfo{ + .image = r->image, + .viewType = image_view_type, + .format = format, + .subresourceRange = { + .aspectMask = vk::ImageAspectFlagBits::eColor, + .baseMipLevel = 0, + .levelCount = num_mipmaps, + .baseArrayLayer = 0, + .layerCount = 1, + }, + }}; + + image_view = std::shared_ptr(r, &r->image_view); } void image_loader::do_load_ktx(std::span bytes) @@ -386,14 +400,8 @@ void image_loader::do_load_ktx(std::span bytes) r->ktx_texture = vk_texture; r->device = *device; r->image = vk::Image(vk_texture.image); - - image = std::shared_ptr(r, &r->image); -} - -void image_loader::create_image_view() -{ - image_view = vk::raii::ImageView{device, vk::ImageViewCreateInfo{ - .image = *image, + r->image_view = vk::raii::ImageView{device, vk::ImageViewCreateInfo{ + .image = r->image, .viewType = image_view_type, .format = format, .subresourceRange = { @@ -404,6 +412,8 @@ void image_loader::create_image_view() .layerCount = 1, }, }}; + + image_view = std::shared_ptr(r, &r->image_view); } static constexpr bool starts_with(std::span data, std::span prefix) @@ -459,8 +469,6 @@ void image_loader::load(std::span bytes, bool srgb) do_load_raw(pixels.get(), extent, format); } - - create_image_view(); } // Load raw pixel data @@ -474,6 +482,4 @@ void image_loader::load(const void * pixels, size_t size, vk::Extent3D extent_, throw std::invalid_argument("size"); do_load_raw(pixels, extent_, format_); - - create_image_view(); } diff --git a/client/render/image_loader.h b/client/render/image_loader.h index 5601b888..17939c3d 100644 --- a/client/render/image_loader.h +++ b/client/render/image_loader.h @@ -27,20 +27,20 @@ #include #include #include -#include +#include +#include "vk/fwd.h" struct ktxVulkanDeviceInfo; -struct ktxVulkanTexture; struct image_loader { - std::shared_ptr image; - vk::raii::ImageView image_view = nullptr; - + vk::Image image; vk::Format format; vk::Extent3D extent; vk::ImageViewType image_view_type; + std::shared_ptr image_view; + uint32_t num_mipmaps; image_loader(vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, vk::raii::Queue & queue, vk::raii::CommandPool & cb_pool); @@ -75,7 +75,6 @@ struct image_loader private: ktxVulkanDeviceInfo * vdi = nullptr; - vk::raii::PhysicalDevice physical_device; vk::raii::Device & device; vk::raii::Queue & queue; vk::raii::CommandPool & cb_pool; diff --git a/client/render/imgui_impl.cpp b/client/render/imgui_impl.cpp index 1b4db955..c1cdad7c 100644 --- a/client/render/imgui_impl.cpp +++ b/client/render/imgui_impl.cpp @@ -553,7 +553,7 @@ ImTextureID imgui_context::load_texture(const std::string& filename, vk::raii::S vk::DescriptorImageInfo image_info{ .sampler = *sampler, - .imageView = *loader.image_view, + .imageView = **loader.image_view, .imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal, }; @@ -570,7 +570,7 @@ ImTextureID imgui_context::load_texture(const std::string& filename, vk::raii::S textures.emplace(id, texture_data{ .sampler = std::move(sampler), - .image = std::move(loader.image), + // .image = std::move(loader.image), .image_view = std::move(loader.image_view), .descriptor_set = std::move(ds), }); diff --git a/client/render/imgui_impl.h b/client/render/imgui_impl.h index fd22b0fc..0e3aef97 100644 --- a/client/render/imgui_impl.h +++ b/client/render/imgui_impl.h @@ -42,8 +42,7 @@ class imgui_context struct texture_data { vk::raii::Sampler sampler; - std::shared_ptr image; - vk::raii::ImageView image_view; + std::shared_ptr image_view; vk::raii::DescriptorSet descriptor_set; }; diff --git a/client/render/scene_data.cpp b/client/render/scene_data.cpp index 5c9f99fc..849850de 100644 --- a/client/render/scene_data.cpp +++ b/client/render/scene_data.cpp @@ -342,7 +342,7 @@ static fastgltf::MimeType guess_mime_type(std::span image_data) return fastgltf::MimeType::None; } -static std::shared_ptr do_load_image( +static std::shared_ptr do_load_image( vk::raii::PhysicalDevice & physical_device, vk::raii::Device & device, vk::raii::Queue & queue, @@ -362,7 +362,7 @@ static std::shared_ptr do_load_image( loader.load(image_data, srgb); spdlog::debug("Loaded image {}x{}, format {}, {} mipmaps", loader.extent.width, loader.extent.height, vk::to_string(loader.format), loader.num_mipmaps); - return std::make_shared(std::move(loader.image), std::move(loader.image_view)); + return loader.image_view; } catch(std::exception& e) { @@ -401,7 +401,6 @@ public: vk::raii::Queue & queue, vk::raii::CommandPool & cb_pool) : base_directory(base_directory), - // loader(physical_device, device, queue, cb_pool), gltf(gltf), physical_device(physical_device), device(device), @@ -480,8 +479,8 @@ public: source); } - std::unordered_map> images; - std::shared_ptr load_image(int index, bool srgb) + std::unordered_map> images; + std::shared_ptr load_image(int index, bool srgb) { auto it = images.find(index); if (it != images.end()) @@ -523,13 +522,9 @@ public: if (gltf_texture.basisuImageIndex) { - std::shared_ptr image = load_image(*gltf_texture.basisuImageIndex, srgb); - if (image) - { - // Use the aliasing constructor so that the image_view has the same lifetime as the image - texture_ref.image_view = std::shared_ptr(image, &image->image_view); + texture_ref.image_view = load_image(*gltf_texture.basisuImageIndex, srgb); + if (texture_ref.image_view) continue; - } } // if (gltf_texture.ddsImageIndex) @@ -544,12 +539,9 @@ public: if (gltf_texture.imageIndex) { - std::shared_ptr image = load_image(*gltf_texture.imageIndex, srgb); - if (image) - { - texture_ref.image_view = std::shared_ptr(image, &image->image_view); + texture_ref.image_view = load_image(*gltf_texture.imageIndex, srgb); + if (texture_ref.image_view) continue; - } } throw std::runtime_error("Unsupported image type"); diff --git a/client/render/scene_data.h b/client/render/scene_data.h index a3d623c4..817ce870 100644 --- a/client/render/scene_data.h +++ b/client/render/scene_data.h @@ -59,12 +59,6 @@ class node_handle; struct scene_data { - struct image - { - std::shared_ptr image_; - vk::raii::ImageView image_view = nullptr; - }; - struct texture { std::shared_ptr image_view; diff --git a/client/render/scene_renderer.cpp b/client/render/scene_renderer.cpp index 06c72d7e..905b4d71 100644 --- a/client/render/scene_renderer.cpp +++ b/client/render/scene_renderer.cpp @@ -97,9 +97,7 @@ std::shared_ptr scene_renderer::create_default_texture(vk:: image_loader loader(physical_device, device, queue, cb_pool); loader.load(pixel, vk::Extent3D{1, 1, 1}, format); - auto image = std::make_shared(std::move(loader.image), std::move(loader.image_view)); - - std::shared_ptr image_view(image, &image->image_view); + std::shared_ptr image_view = loader.image_view; return std::make_shared(image_view, sampler_info{}); } diff --git a/client/vk/fwd.h b/client/vk/fwd.h new file mode 100644 index 00000000..61c0a246 --- /dev/null +++ b/client/vk/fwd.h @@ -0,0 +1,29 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2022 Guillaume Meunier + * Copyright (C) 2022 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +namespace vk::raii +{ + class ImageView; + class PhysicalDevice; + class Device; + class Queue; + class CommandPool; +}