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_BUFFER_VK_H
17 #define VULKAN_GPU_BUFFER_VK_H
18 
19 #include <vulkan/vulkan_core.h>
20 
21 #include <render/device/gpu_resource_desc.h>
22 #include <render/namespace.h>
23 #include <render/vulkan/intf_device_vk.h>
24 
25 #include "device/gpu_buffer.h"
26 #include "vulkan/gpu_memory_allocator_vk.h"
27 
28 RENDER_BEGIN_NAMESPACE()
29 class Device;
30 
31 struct GpuAccelerationStructurePlatformDataVk final {
32     VkBuffer buffer { VK_NULL_HANDLE };
33     uint32_t byteSize { 0u };
34     uint64_t deviceAddress { 0 };
35 #if (RENDER_VULKAN_RT_ENABLED == 1)
36     VkAccelerationStructureKHR accelerationStructure { VK_NULL_HANDLE };
37 #endif
38 };
39 
40 class GpuBufferVk final : public GpuBuffer {
41 public:
42     GpuBufferVk(Device& device, const GpuBufferDesc& desc);
43     GpuBufferVk(Device& device, const GpuAccelerationStructureDesc& desc);
44     ~GpuBufferVk();
45 
46     const GpuBufferDesc& GetDesc() const override;
47     const GpuBufferPlatformDataVk& GetPlatformData() const;
48     const GpuAccelerationStructureDesc& GetDescAccelerationStructure() const;
49     const GpuAccelerationStructurePlatformDataVk& GetPlatformDataAccelerationStructure() const;
50 
51     void* Map() override;
52     void* MapMemory() override;
53     void Unmap() const override;
54 
55 private:
56     void AllocateMemory(const VkMemoryPropertyFlags requiredFlags, const VkMemoryPropertyFlags preferredFlags);
57     void CreateBufferImpl();
58 
59     Device& device_;
60 
61     GpuBufferPlatformDataVk plat_;
62     GpuBufferDesc desc_;
63 
64     GpuAccelerationStructurePlatformDataVk platAccel_;
65     GpuAccelerationStructureDesc descAccel_;
66 
67     bool isPersistantlyMapped_ { false };
68     bool isMappable_ { false };
69     bool isRingBuffer_ { false };
70     bool isAccelerationStructure_ { false };
71     uint32_t bufferingCount_ { 1u };
72 
73     // debug assert usage only
74     mutable bool isMapped_ { false };
75 
76     struct MemoryAllocation {
77         VmaAllocation allocation;
78         VmaAllocationInfo allocationInfo;
79     };
80     MemoryAllocation mem_ {};
81 };
82 RENDER_END_NAMESPACE()
83 
84 #endif // VULKAN_GPU_BUFFER_VK_H
85