Alexis Hetu | e70c351 | 2018-10-17 13:18:04 -0400 | [diff] [blame] | 1 | // Copyright 2018 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 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 | |
Alexis Hetu | 38ff830 | 2018-10-18 15:08:13 -0400 | [diff] [blame] | 15 | #include "VkBuffer.hpp" |
| 16 | #include "VkBufferView.hpp" |
Alexis Hetu | e70c351 | 2018-10-17 13:18:04 -0400 | [diff] [blame] | 17 | #include "VkCommandBuffer.hpp" |
Alexis Hetu | 9c4ecae | 2018-11-20 16:26:10 -0500 | [diff] [blame] | 18 | #include "VkCommandPool.hpp" |
Alexis Hetu | e70c351 | 2018-10-17 13:18:04 -0400 | [diff] [blame] | 19 | #include "VkDevice.hpp" |
Alexis Hetu | 38ff830 | 2018-10-18 15:08:13 -0400 | [diff] [blame] | 20 | #include "VkDeviceMemory.hpp" |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 21 | #include "VkEvent.hpp" |
| 22 | #include "VkFence.hpp" |
Alexis Hetu | 8f631c8 | 2018-11-15 15:11:36 -0500 | [diff] [blame] | 23 | #include "VkFramebuffer.hpp" |
Alexis Hetu | f62f375 | 2018-11-15 14:51:15 -0500 | [diff] [blame] | 24 | #include "VkImage.hpp" |
Alexis Hetu | 9fbaf69 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 25 | #include "VkImageView.hpp" |
Alexis Hetu | e70c351 | 2018-10-17 13:18:04 -0400 | [diff] [blame] | 26 | #include "VkInstance.hpp" |
Antonio Maiorano | 71c49f8 | 2020-04-27 11:33:30 -0400 | [diff] [blame] | 27 | #include "VkPhysicalDevice.hpp" |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 28 | #include "VkPipeline.hpp" |
Alexis Hetu | 18a8425 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 29 | #include "VkPipelineCache.hpp" |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 30 | #include "VkPipelineLayout.hpp" |
Alexis Hetu | 86f8bdb | 2019-01-22 12:07:24 -0500 | [diff] [blame] | 31 | #include "VkQueryPool.hpp" |
Alexis Hetu | e70c351 | 2018-10-17 13:18:04 -0400 | [diff] [blame] | 32 | #include "VkQueue.hpp" |
Antonio Maiorano | 71c49f8 | 2020-04-27 11:33:30 -0400 | [diff] [blame] | 33 | #include "VkRenderPass.hpp" |
Alexis Hetu | 5174c57 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 34 | #include "VkSampler.hpp" |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 35 | #include "VkSemaphore.hpp" |
Alexis Hetu | 259ad3d | 2018-11-15 13:44:31 -0500 | [diff] [blame] | 36 | #include "VkShaderModule.hpp" |
Hernan Liatis | c6eb41b | 2019-02-22 11:12:59 -0800 | [diff] [blame] | 37 | #include "WSI/VkSurfaceKHR.hpp" |
Hernan Liatis | c7943e9 | 2019-02-25 19:29:54 -0800 | [diff] [blame] | 38 | #include "WSI/VkSwapchainKHR.hpp" |
Alexis Hetu | e70c351 | 2018-10-17 13:18:04 -0400 | [diff] [blame] | 39 | |
Ben Clayton | caf6031 | 2019-05-21 15:31:12 +0100 | [diff] [blame] | 40 | #include <type_traits> |
| 41 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 42 | namespace vk { |
Alexis Hetu | e70c351 | 2018-10-17 13:18:04 -0400 | [diff] [blame] | 43 | |
| 44 | // Because Vulkan uses optional allocation callbacks, we use them in a custom |
| 45 | // placement new operator in the VkObjectBase class for simplicity. |
| 46 | // Unfortunately, since we use a placement new to allocate VkObjectBase derived |
| 47 | // classes objects, the corresponding deletion operator is a placement delete, |
| 48 | // which does nothing. In order to properly dispose of these objects' memory, |
Ben Clayton | caf6031 | 2019-05-21 15:31:12 +0100 | [diff] [blame] | 49 | // we use this function, which calls the T:destroy() function then the T |
| 50 | // destructor prior to releasing the object (by default, |
| 51 | // VkObjectBase::destroy does nothing). |
Alexis Hetu | e70c351 | 2018-10-17 13:18:04 -0400 | [diff] [blame] | 52 | template<typename VkT> |
Antonio Maiorano | 71c49f8 | 2020-04-27 11:33:30 -0400 | [diff] [blame] | 53 | inline void destroy(VkT vkObject, const VkAllocationCallbacks *pAllocator) |
Alexis Hetu | e70c351 | 2018-10-17 13:18:04 -0400 | [diff] [blame] | 54 | { |
| 55 | auto object = Cast(vkObject); |
| 56 | if(object) |
| 57 | { |
Ben Clayton | caf6031 | 2019-05-21 15:31:12 +0100 | [diff] [blame] | 58 | using T = typename std::remove_pointer<decltype(object)>::type; |
Alexis Hetu | e70c351 | 2018-10-17 13:18:04 -0400 | [diff] [blame] | 59 | object->destroy(pAllocator); |
Ben Clayton | caf6031 | 2019-05-21 15:31:12 +0100 | [diff] [blame] | 60 | object->~T(); |
Alexis Hetu | e70c351 | 2018-10-17 13:18:04 -0400 | [diff] [blame] | 61 | // object may not point to the same pointer as vkObject, for dispatchable objects, |
| 62 | // for example, so make sure to deallocate based on the vkObject pointer, which |
| 63 | // should always point to the beginning of the allocated memory |
| 64 | vk::deallocate(vkObject, pAllocator); |
| 65 | } |
| 66 | } |
| 67 | |
Trevor David Black | 3ad285a | 2020-05-26 19:28:05 +0000 | [diff] [blame] | 68 | template<typename VkT> |
| 69 | inline void release(VkT vkObject, const VkAllocationCallbacks *pAllocator) |
| 70 | { |
| 71 | auto object = Cast(vkObject); |
| 72 | if(object) |
| 73 | { |
| 74 | using T = typename std::remove_pointer<decltype(object)>::type; |
| 75 | if(object->release(pAllocator)) |
| 76 | { |
| 77 | object->~T(); |
| 78 | // object may not point to the same pointer as vkObject, for dispatchable objects, |
| 79 | // for example, so make sure to deallocate based on the vkObject pointer, which |
| 80 | // should always point to the beginning of the allocated memory |
| 81 | vk::deallocate(vkObject, pAllocator); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 86 | } // namespace vk |