blob: 4b057a1680a9e0f36b19aed245a97b30aacf1436 [file] [log] [blame]
Alexis Hetue70c3512018-10-17 13:18:04 -04001// 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 Hetu38ff8302018-10-18 15:08:13 -040015#include "VkBuffer.hpp"
16#include "VkBufferView.hpp"
Alexis Hetue70c3512018-10-17 13:18:04 -040017#include "VkCommandBuffer.hpp"
Alexis Hetu9c4ecae2018-11-20 16:26:10 -050018#include "VkCommandPool.hpp"
Alexis Hetue70c3512018-10-17 13:18:04 -040019#include "VkDevice.hpp"
Alexis Hetu38ff8302018-10-18 15:08:13 -040020#include "VkDeviceMemory.hpp"
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040021#include "VkEvent.hpp"
22#include "VkFence.hpp"
Alexis Hetu8f631c82018-11-15 15:11:36 -050023#include "VkFramebuffer.hpp"
Alexis Hetuf62f3752018-11-15 14:51:15 -050024#include "VkImage.hpp"
Alexis Hetu9fbaf692018-11-19 11:30:43 -050025#include "VkImageView.hpp"
Alexis Hetue70c3512018-10-17 13:18:04 -040026#include "VkInstance.hpp"
Antonio Maiorano71c49f82020-04-27 11:33:30 -040027#include "VkPhysicalDevice.hpp"
Alexis Hetu000df8b2018-10-24 15:22:41 -040028#include "VkPipeline.hpp"
Alexis Hetu18a84252018-11-19 11:30:43 -050029#include "VkPipelineCache.hpp"
Alexis Hetu000df8b2018-10-24 15:22:41 -040030#include "VkPipelineLayout.hpp"
Alexis Hetu86f8bdb2019-01-22 12:07:24 -050031#include "VkQueryPool.hpp"
Alexis Hetue70c3512018-10-17 13:18:04 -040032#include "VkQueue.hpp"
Antonio Maiorano71c49f82020-04-27 11:33:30 -040033#include "VkRenderPass.hpp"
Alexis Hetu5174c572018-11-19 11:30:43 -050034#include "VkSampler.hpp"
Alexis Hetu1f23d8c2018-10-16 14:40:19 -040035#include "VkSemaphore.hpp"
Alexis Hetu259ad3d2018-11-15 13:44:31 -050036#include "VkShaderModule.hpp"
Hernan Liatisc6eb41b2019-02-22 11:12:59 -080037#include "WSI/VkSurfaceKHR.hpp"
Hernan Liatisc7943e92019-02-25 19:29:54 -080038#include "WSI/VkSwapchainKHR.hpp"
Alexis Hetue70c3512018-10-17 13:18:04 -040039
Ben Claytoncaf60312019-05-21 15:31:12 +010040#include <type_traits>
41
Nicolas Capens157ba262019-12-10 17:49:14 -050042namespace vk {
Alexis Hetue70c3512018-10-17 13:18:04 -040043
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 Claytoncaf60312019-05-21 15:31:12 +010049// 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 Hetue70c3512018-10-17 13:18:04 -040052template<typename VkT>
Antonio Maiorano71c49f82020-04-27 11:33:30 -040053inline void destroy(VkT vkObject, const VkAllocationCallbacks *pAllocator)
Alexis Hetue70c3512018-10-17 13:18:04 -040054{
55 auto object = Cast(vkObject);
56 if(object)
57 {
Ben Claytoncaf60312019-05-21 15:31:12 +010058 using T = typename std::remove_pointer<decltype(object)>::type;
Alexis Hetue70c3512018-10-17 13:18:04 -040059 object->destroy(pAllocator);
Ben Claytoncaf60312019-05-21 15:31:12 +010060 object->~T();
Alexis Hetue70c3512018-10-17 13:18:04 -040061 // 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 Black3ad285a2020-05-26 19:28:05 +000068template<typename VkT>
69inline 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 Capens157ba262019-12-10 17:49:14 -050086} // namespace vk