Blender V4.3
vk_image_view.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include "vk_image_view.hh"
10#include "vk_backend.hh"
11#include "vk_debug.hh"
12#include "vk_device.hh"
13#include "vk_memory.hh"
14#include "vk_texture.hh"
15
16namespace blender::gpu {
17
18static VkFormat to_non_srgb_format(const VkFormat format)
19{
20 switch (format) {
21 case VK_FORMAT_R8G8B8_SRGB:
22 return VK_FORMAT_R8G8B8_UNORM;
23 case VK_FORMAT_R8G8B8A8_SRGB:
24 return VK_FORMAT_R8G8B8A8_UNORM;
25
26 default:
27 break;
28 }
29 return format;
30}
31
33 : info(info)
34{
35 const VkImageAspectFlags allowed_bits = VK_IMAGE_ASPECT_COLOR_BIT |
36 (info.use_stencil ? VK_IMAGE_ASPECT_STENCIL_BIT :
37 VK_IMAGE_ASPECT_DEPTH_BIT);
38 eGPUTextureFormat device_format = texture.device_format_get();
39 VkImageAspectFlags image_aspect = to_vk_image_aspect_flag_bits(device_format) & allowed_bits;
40
41 vk_format_ = to_vk_format(device_format);
42 if (texture.format_flag_get() & GPU_FORMAT_SRGB && !info.use_srgb) {
43 vk_format_ = to_non_srgb_format(vk_format_);
44 }
45
47 VkImageViewCreateInfo image_view_info = {};
48 image_view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
49 image_view_info.image = texture.vk_image_handle();
50 image_view_info.viewType = to_vk_image_view_type(texture.type_get(), info.usage, info.arrayed);
51 image_view_info.format = vk_format_;
52 image_view_info.components.r = to_vk_component_swizzle(info.swizzle[0]);
53 image_view_info.components.g = to_vk_component_swizzle(info.swizzle[1]);
54 image_view_info.components.b = to_vk_component_swizzle(info.swizzle[2]);
55 image_view_info.components.a = to_vk_component_swizzle(info.swizzle[3]);
56 image_view_info.subresourceRange.aspectMask = image_aspect;
57 image_view_info.subresourceRange.baseMipLevel = info.mip_range.first();
58 image_view_info.subresourceRange.levelCount = info.mip_range.size();
59 image_view_info.subresourceRange.baseArrayLayer = info.layer_range.first();
60 image_view_info.subresourceRange.layerCount = info.layer_range.size();
61
62 const VKDevice &device = VKBackend::get().device;
63 vkCreateImageView(
64 device.vk_handle(), &image_view_info, vk_allocation_callbacks, &vk_image_view_);
65 debug::object_label(vk_image_view_, name.c_str());
66}
67
68VKImageView::VKImageView(VKImageView &&other) : info(other.info)
69{
70 vk_image_view_ = other.vk_image_view_;
71 other.vk_image_view_ = VK_NULL_HANDLE;
72 vk_format_ = other.vk_format_;
73 other.vk_format_ = VK_FORMAT_UNDEFINED;
74}
75
77{
78 if (vk_image_view_ != VK_NULL_HANDLE) {
79 VKDevice &device = VKBackend::get().device;
81 vk_image_view_ = VK_NULL_HANDLE;
82 }
83 vk_format_ = VK_FORMAT_UNDEFINED;
84}
85
86} // namespace blender::gpu
eGPUTextureFormat
constexpr int64_t first() const
constexpr int64_t size() const
static VKBackend & get()
Definition vk_backend.hh:92
VkDevice vk_handle() const
Definition vk_device.hh:224
VKDiscardPool & discard_pool_for_current_thread()
Definition vk_device.cc:399
void discard_image_view(VkImageView vk_image_view)
VKImageView(VKTexture &texture, const VKImageViewInfo &info, StringRefNull name)
const VKImageViewInfo info
format
void object_label(GLenum type, GLuint object, const char *name)
Definition gl_debug.cc:344
static VkFormat to_non_srgb_format(const VkFormat format)
VkComponentSwizzle to_vk_component_swizzle(const char swizzle)
Definition vk_common.cc:774
VkFormat to_vk_format(const eGPUTextureFormat format)
Definition vk_common.cc:131
VkImageAspectFlags to_vk_image_aspect_flag_bits(const eGPUTextureFormat format)
Definition vk_common.cc:14
VkImageViewType to_vk_image_view_type(const eGPUTextureType type, const eImageViewUsage view_type, VKImageViewArrayed arrayed)
Definition vk_common.cc:711
#define VK_ALLOCATION_CALLBACKS
Definition vk_memory.hh:58