• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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(Device& device, const GpuImageDesc& desc, const GpuImagePlatformData& platformData, uintptr_t hwBuffer);
50     ~GpuImageVk() override;
51 
52     const GpuImageDesc& GetDesc() const override;
53     const GpuImagePlatformData& GetBasePlatformData() const override;
54     const GpuImagePlatformDataVk& GetPlatformData() const;
55     const GpuImagePlatformDataViewsVk& GetPlatformDataViews() const;
56     const GpuImagePlatformDataConversion& GetPlaformDataConversion() const;
57 
58     AdditionalFlags GetAdditionalFlags() const override;
59 
60 private:
61     void CreateVkImage();
62     void CreateVkImageViews(
63         VkImageAspectFlags imageAspectFlags, const VkSamplerYcbcrConversionInfo* ycbcrConversionInfo);
64     void CreatePlatformHwBuffer();
65     void DestroyPlatformHwBuffer();
66 
67     Device& device_;
68 
69     GpuImagePlatformDataVk plat_;
70     GpuImagePlatformDataViewsVk platViews_;
71     GpuImagePlatformDataConversion platConversion_;
72     GpuImageDesc desc_;
73     // in normal situations owns all the vulkan resources
74     bool ownsResources_ { true };
75     // one might create a higher level view without hwbuffer
76     bool ownsImage_ { true };
77     bool ownsImageViews_ { true };
78 
79     bool destroyImageViewBase_ { false };
80 
81     struct MemoryAllocation {
82         VmaAllocation allocation;
83         VmaAllocationInfo allocationInfo;
84     };
85     MemoryAllocation mem_ {};
86 };
87 
88 namespace GpuImageUtilsVk {
89 VkImageAspectFlags GetImageAspectFlagsFromFormat(VkFormat format);
90 } // namespace GpuImageUtilsVk
91 RENDER_END_NAMESPACE()
92 
93 #endif // VULKAN_GPU_IMAGE_VK_H
94