1  /*
2   * Copyright (c) 2024 Huawei Device Co., Ltd.
3   * Licensed under the Apache License, Version 2.0 (the "License");
4   * you may not use this file except in compliance with the License.
5   * You may obtain a copy of the License at
6   *
7   *     http://www.apache.org/licenses/LICENSE-2.0
8   *
9   * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  #ifndef VULKAN_GPU_IMAGE_VK_H
17  #define VULKAN_GPU_IMAGE_VK_H
18  
19  #include <vulkan/vulkan_core.h>
20  
21  #include <base/containers/vector.h>
22  #include <render/device/gpu_resource_desc.h>
23  #include <render/namespace.h>
24  #include <render/vulkan/intf_device_vk.h>
25  
26  #include "device/gpu_image.h"
27  #include "vulkan/gpu_memory_allocator_vk.h"
28  
29  RENDER_BEGIN_NAMESPACE()
30  struct GpuImagePlatformDataViewsVk final {
31      // generated only for color attachments
32      BASE_NS::vector<VkImageView> mipImageViews;
33      BASE_NS::vector<VkImageView> mipImageAllLayerViews;
34      BASE_NS::vector<VkImageView> layerImageViews;
35  };
36  
37  struct GpuImagePlatformDataConversion final {
38      // Sampler conversion object, used only with immutable samplers and created sampler conversion
39      VkSampler sampler { VK_NULL_HANDLE };
40      // Sampler conversion object, used only with immutable samplers and views
41      VkSamplerYcbcrConversion samplerConversion { VK_NULL_HANDLE };
42  };
43  
44  class Device;
45  
46  class GpuImageVk final : public GpuImage {
47  public:
48      GpuImageVk(Device& device, const GpuImageDesc& desc);
49      GpuImageVk(
50          Device& device, const GpuImageDesc& desc, const GpuImagePlatformData& platformData, const uintptr_t hwBuffer);
51      ~GpuImageVk();
52  
53      const GpuImageDesc& GetDesc() const override;
54      const GpuImagePlatformData& GetBasePlatformData() const override;
55      const GpuImagePlatformDataVk& GetPlatformData() const;
56      const GpuImagePlatformDataViewsVk& GetPlatformDataViews() const;
57      const GpuImagePlatformDataConversion& GetPlaformDataConversion() const;
58  
59      AdditionalFlags GetAdditionalFlags() const override;
60  
61  private:
62      void CreateVkImage();
63      void CreateVkImageViews(
64          VkImageAspectFlags imageAspectFlags, const VkSamplerYcbcrConversionInfo* ycbcrConversionInfo);
65      void CreatePlatformHwBuffer();
66      void DestroyPlatformHwBuffer();
67  
68      Device& device_;
69  
70      GpuImagePlatformDataVk plat_;
71      GpuImagePlatformDataViewsVk platViews_;
72      GpuImagePlatformDataConversion platConversion_;
73      GpuImageDesc desc_;
74      uintptr_t hwBuffer_ { 0 };
75      // in normal situations owns all the vulkan resources
76      bool ownsResources_ { true };
77      bool destroyImageViewBase_ { false };
78  
79      struct MemoryAllocation {
80          VmaAllocation allocation;
81          VmaAllocationInfo allocationInfo;
82      };
83      MemoryAllocation mem_ {};
84  };
85  
86  namespace GpuImageUtilsVk {
87  VkImageAspectFlags GetImageAspectFlagsFromFormat(const VkFormat format);
88  } // namespace GpuImageUtilsVk
89  RENDER_END_NAMESPACE()
90  
91  #endif // VULKAN_GPU_IMAGE_VK_H
92