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_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(Device& device, const BackendSpecificBufferDesc& desc); 45 46 ~GpuBufferVk() override; 47 48 const GpuBufferDesc& GetDesc() const override; 49 const GpuBufferPlatformDataVk& GetPlatformData() const; 50 const GpuAccelerationStructureDesc& GetDescAccelerationStructure() const; 51 const GpuAccelerationStructurePlatformDataVk& GetPlatformDataAccelerationStructure() const; 52 53 void* Map() override; 54 void* MapMemory() override; 55 void Unmap() const override; 56 57 private: 58 void AllocateMemory(VkMemoryPropertyFlags requiredFlags, VkMemoryPropertyFlags preferredFlags); 59 void CreateBufferImpl(); 60 void CreatePlatformHwBuffer(); 61 void DestroyPlatformHwBuffer(); 62 63 Device& device_; 64 65 GpuBufferPlatformDataVk plat_; 66 GpuBufferDesc desc_; 67 68 GpuAccelerationStructurePlatformDataVk platAccel_; 69 GpuAccelerationStructureDesc descAccel_; 70 71 bool isPersistantlyMapped_ { false }; 72 bool isMappable_ { false }; 73 bool isRingBuffer_ { false }; 74 bool isAccelerationStructure_ { false }; 75 uint32_t bufferingCount_ { 1u }; 76 77 // in normal situations owns all the vulkan resources 78 bool ownsResources_ { true }; 79 80 // debug assert usage only 81 mutable bool isMapped_ { false }; 82 83 struct MemoryAllocation { 84 VmaAllocation allocation; 85 VmaAllocationInfo allocationInfo; 86 }; 87 MemoryAllocation mem_ {}; 88 }; 89 RENDER_END_NAMESPACE() 90 91 #endif // VULKAN_GPU_BUFFER_VK_H 92