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
15 #include "VkBuffer.hpp"
16 #include "VkBufferView.hpp"
17 #include "VkCommandBuffer.hpp"
18 #include "VkCommandPool.hpp"
19 #include "VkDevice.hpp"
20 #include "VkDeviceMemory.hpp"
21 #include "VkEvent.hpp"
22 #include "VkFence.hpp"
23 #include "VkFramebuffer.hpp"
24 #include "VkImage.hpp"
25 #include "VkImageView.hpp"
26 #include "VkInstance.hpp"
27 #include "VkPipeline.hpp"
28 #include "VkPipelineCache.hpp"
29 #include "VkPipelineLayout.hpp"
30 #include "VkPhysicalDevice.hpp"
31 #include "VkQueryPool.hpp"
32 #include "VkQueue.hpp"
33 #include "VkSampler.hpp"
34 #include "VkSemaphore.hpp"
35 #include "VkShaderModule.hpp"
36 #include "VkRenderPass.hpp"
37 #include "WSI/VkSurfaceKHR.hpp"
38 #include "WSI/VkSwapchainKHR.hpp"
39
40 #include <type_traits>
41
42 namespace vk {
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,
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).
52 template<typename VkT>
destroy(VkT vkObject,const VkAllocationCallbacks * pAllocator)53 inline void destroy(VkT vkObject, const VkAllocationCallbacks* pAllocator)
54 {
55 auto object = Cast(vkObject);
56 if(object)
57 {
58 using T = typename std::remove_pointer<decltype(object)>::type;
59 object->destroy(pAllocator);
60 object->~T();
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
68 } // namespace vk
69