Blender V5.0
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
8
9#include "vk_image_view.hh"
10#include "vk_backend.hh"
11#include "vk_debug.hh"
12#include "vk_device.hh"
13#include "vk_texture.hh"
14
15namespace blender::gpu {
16
17static VkFormat to_non_srgb_format(const VkFormat format)
18{
19 switch (format) {
20 case VK_FORMAT_R8G8B8_SRGB:
21 return VK_FORMAT_R8G8B8_UNORM;
22 case VK_FORMAT_R8G8B8A8_SRGB:
23 return VK_FORMAT_R8G8B8A8_UNORM;
24
25 default:
26 break;
27 }
28 return format;
29}
30
32 : info(info)
33{
34 const VkImageAspectFlags allowed_bits = VK_IMAGE_ASPECT_COLOR_BIT |
35 (info.use_stencil ? VK_IMAGE_ASPECT_STENCIL_BIT :
36 VK_IMAGE_ASPECT_DEPTH_BIT);
37 TextureFormat device_format = texture.device_format_get();
38 VkImageAspectFlags image_aspect = to_vk_image_aspect_flag_bits(device_format) & allowed_bits;
39
40 vk_format_ = to_vk_format(device_format);
41 if (texture.format_flag_get() & GPU_FORMAT_SRGB && !info.use_srgb) {
42 vk_format_ = to_non_srgb_format(vk_format_);
43 }
44
45 VkImageViewCreateInfo image_view_info = {};
46 image_view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
47 image_view_info.image = texture.vk_image_handle();
48 image_view_info.viewType = to_vk_image_view_type(texture.type_get(), info.usage, info.arrayed);
49 image_view_info.format = vk_format_;
50 image_view_info.components.r = to_vk_component_swizzle(info.swizzle[0]);
51 image_view_info.components.g = to_vk_component_swizzle(info.swizzle[1]);
52 image_view_info.components.b = to_vk_component_swizzle(info.swizzle[2]);
53 image_view_info.components.a = to_vk_component_swizzle(info.swizzle[3]);
54 image_view_info.subresourceRange.aspectMask = image_aspect;
55 image_view_info.subresourceRange.baseMipLevel = info.mip_range.first();
56 image_view_info.subresourceRange.levelCount = info.mip_range.size();
57 image_view_info.subresourceRange.baseArrayLayer = info.layer_range.first();
58 image_view_info.subresourceRange.layerCount = info.layer_range.size();
59
60 const VKDevice &device = VKBackend::get().device;
61 vkCreateImageView(device.vk_handle(), &image_view_info, nullptr, &vk_image_view_);
62 debug::object_label(vk_image_view_, name.c_str());
63}
64
66{
67 vk_image_view_ = other.vk_image_view_;
68 other.vk_image_view_ = VK_NULL_HANDLE;
69 vk_format_ = other.vk_format_;
70 other.vk_format_ = VK_FORMAT_UNDEFINED;
71}
72
74{
75 if (vk_image_view_ != VK_NULL_HANDLE) {
77 vk_image_view_ = VK_NULL_HANDLE;
78 }
79 vk_format_ = VK_FORMAT_UNDEFINED;
80}
81
82} // namespace blender::gpu
static VKBackend & get()
Definition vk_backend.hh:91
VkDevice vk_handle() const
Definition vk_device.hh:311
void discard_image_view(VkImageView vk_image_view)
static VKDiscardPool & discard_pool_get()
VKImageView(VKTexture &texture, const VKImageViewInfo &info, StringRefNull name)
const VKImageViewInfo info
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
format
void object_label(GLenum type, GLuint object, const char *name)
Definition gl_debug.cc:329
VkImageViewType to_vk_image_view_type(const GPUTextureType type, const eImageViewUsage view_type, VKImageViewArrayed arrayed)
Definition vk_common.cc:504
VkFormat to_vk_format(const TextureFormat format)
Definition vk_common.cc:136
static VkFormat to_non_srgb_format(const VkFormat format)
VkComponentSwizzle to_vk_component_swizzle(const char swizzle)
Definition vk_common.cc:567
VkImageAspectFlags to_vk_image_aspect_flag_bits(const TextureFormat format)
Definition vk_common.cc:14
const char * name