1*03ce13f7SAndroid Build Coastguard Worker // Copyright 2018 The SwiftShader Authors. All Rights Reserved.
2*03ce13f7SAndroid Build Coastguard Worker //
3*03ce13f7SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*03ce13f7SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*03ce13f7SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*03ce13f7SAndroid Build Coastguard Worker //
7*03ce13f7SAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0
8*03ce13f7SAndroid Build Coastguard Worker //
9*03ce13f7SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*03ce13f7SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*03ce13f7SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*03ce13f7SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*03ce13f7SAndroid Build Coastguard Worker // limitations under the License.
14*03ce13f7SAndroid Build Coastguard Worker
15*03ce13f7SAndroid Build Coastguard Worker #include "VkBuffer.hpp"
16*03ce13f7SAndroid Build Coastguard Worker #include "VkBufferView.hpp"
17*03ce13f7SAndroid Build Coastguard Worker #include "VkCommandBuffer.hpp"
18*03ce13f7SAndroid Build Coastguard Worker #include "VkCommandPool.hpp"
19*03ce13f7SAndroid Build Coastguard Worker #include "VkDebugUtilsMessenger.hpp"
20*03ce13f7SAndroid Build Coastguard Worker #include "VkDevice.hpp"
21*03ce13f7SAndroid Build Coastguard Worker #include "VkDeviceMemory.hpp"
22*03ce13f7SAndroid Build Coastguard Worker #include "VkEvent.hpp"
23*03ce13f7SAndroid Build Coastguard Worker #include "VkFence.hpp"
24*03ce13f7SAndroid Build Coastguard Worker #include "VkFramebuffer.hpp"
25*03ce13f7SAndroid Build Coastguard Worker #include "VkImage.hpp"
26*03ce13f7SAndroid Build Coastguard Worker #include "VkImageView.hpp"
27*03ce13f7SAndroid Build Coastguard Worker #include "VkInstance.hpp"
28*03ce13f7SAndroid Build Coastguard Worker #include "VkPhysicalDevice.hpp"
29*03ce13f7SAndroid Build Coastguard Worker #include "VkPipeline.hpp"
30*03ce13f7SAndroid Build Coastguard Worker #include "VkPipelineCache.hpp"
31*03ce13f7SAndroid Build Coastguard Worker #include "VkPipelineLayout.hpp"
32*03ce13f7SAndroid Build Coastguard Worker #include "VkPrivateData.hpp"
33*03ce13f7SAndroid Build Coastguard Worker #include "VkQueryPool.hpp"
34*03ce13f7SAndroid Build Coastguard Worker #include "VkQueue.hpp"
35*03ce13f7SAndroid Build Coastguard Worker #include "VkRenderPass.hpp"
36*03ce13f7SAndroid Build Coastguard Worker #include "VkSampler.hpp"
37*03ce13f7SAndroid Build Coastguard Worker #include "VkSemaphore.hpp"
38*03ce13f7SAndroid Build Coastguard Worker #include "VkShaderModule.hpp"
39*03ce13f7SAndroid Build Coastguard Worker #include "WSI/VkSurfaceKHR.hpp"
40*03ce13f7SAndroid Build Coastguard Worker #include "WSI/VkSwapchainKHR.hpp"
41*03ce13f7SAndroid Build Coastguard Worker
42*03ce13f7SAndroid Build Coastguard Worker #include <type_traits>
43*03ce13f7SAndroid Build Coastguard Worker
44*03ce13f7SAndroid Build Coastguard Worker namespace vk {
45*03ce13f7SAndroid Build Coastguard Worker
46*03ce13f7SAndroid Build Coastguard Worker // Because Vulkan uses optional allocation callbacks, we use them in a custom
47*03ce13f7SAndroid Build Coastguard Worker // placement new operator in the VkObjectBase class for simplicity.
48*03ce13f7SAndroid Build Coastguard Worker // Unfortunately, since we use a placement new to allocate VkObjectBase derived
49*03ce13f7SAndroid Build Coastguard Worker // classes objects, the corresponding deletion operator is a placement delete,
50*03ce13f7SAndroid Build Coastguard Worker // which does nothing. In order to properly dispose of these objects' memory,
51*03ce13f7SAndroid Build Coastguard Worker // we use this function, which calls the T:destroy() function then the T
52*03ce13f7SAndroid Build Coastguard Worker // destructor prior to releasing the object (by default,
53*03ce13f7SAndroid Build Coastguard Worker // VkObjectBase::destroy does nothing).
54*03ce13f7SAndroid Build Coastguard Worker template<typename VkT>
destroy(VkT vkObject,const VkAllocationCallbacks * pAllocator)55*03ce13f7SAndroid Build Coastguard Worker inline void destroy(VkT vkObject, const VkAllocationCallbacks *pAllocator)
56*03ce13f7SAndroid Build Coastguard Worker {
57*03ce13f7SAndroid Build Coastguard Worker auto object = Cast(vkObject);
58*03ce13f7SAndroid Build Coastguard Worker if(object)
59*03ce13f7SAndroid Build Coastguard Worker {
60*03ce13f7SAndroid Build Coastguard Worker using T = typename std::remove_pointer<decltype(object)>::type;
61*03ce13f7SAndroid Build Coastguard Worker object->destroy(pAllocator);
62*03ce13f7SAndroid Build Coastguard Worker object->~T();
63*03ce13f7SAndroid Build Coastguard Worker // object may not point to the same pointer as vkObject, for dispatchable objects,
64*03ce13f7SAndroid Build Coastguard Worker // for example, so make sure to deallocate based on the vkObject pointer, which
65*03ce13f7SAndroid Build Coastguard Worker // should always point to the beginning of the allocated memory
66*03ce13f7SAndroid Build Coastguard Worker vk::freeHostMemory(vkObject, pAllocator);
67*03ce13f7SAndroid Build Coastguard Worker }
68*03ce13f7SAndroid Build Coastguard Worker }
69*03ce13f7SAndroid Build Coastguard Worker
70*03ce13f7SAndroid Build Coastguard Worker template<typename VkT>
release(VkT vkObject,const VkAllocationCallbacks * pAllocator)71*03ce13f7SAndroid Build Coastguard Worker inline void release(VkT vkObject, const VkAllocationCallbacks *pAllocator)
72*03ce13f7SAndroid Build Coastguard Worker {
73*03ce13f7SAndroid Build Coastguard Worker auto object = Cast(vkObject);
74*03ce13f7SAndroid Build Coastguard Worker if(object)
75*03ce13f7SAndroid Build Coastguard Worker {
76*03ce13f7SAndroid Build Coastguard Worker using T = typename std::remove_pointer<decltype(object)>::type;
77*03ce13f7SAndroid Build Coastguard Worker if(object->release(pAllocator))
78*03ce13f7SAndroid Build Coastguard Worker {
79*03ce13f7SAndroid Build Coastguard Worker object->~T();
80*03ce13f7SAndroid Build Coastguard Worker // object may not point to the same pointer as vkObject, for dispatchable objects,
81*03ce13f7SAndroid Build Coastguard Worker // for example, so make sure to deallocate based on the vkObject pointer, which
82*03ce13f7SAndroid Build Coastguard Worker // should always point to the beginning of the allocated memory
83*03ce13f7SAndroid Build Coastguard Worker vk::freeHostMemory(vkObject, pAllocator);
84*03ce13f7SAndroid Build Coastguard Worker }
85*03ce13f7SAndroid Build Coastguard Worker }
86*03ce13f7SAndroid Build Coastguard Worker }
87*03ce13f7SAndroid Build Coastguard Worker
88*03ce13f7SAndroid Build Coastguard Worker } // namespace vk
89