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 "VkMemory.hpp"
16*03ce13f7SAndroid Build Coastguard Worker
17*03ce13f7SAndroid Build Coastguard Worker #include "VkConfig.hpp"
18*03ce13f7SAndroid Build Coastguard Worker #include "System/Debug.hpp"
19*03ce13f7SAndroid Build Coastguard Worker #include "System/Memory.hpp"
20*03ce13f7SAndroid Build Coastguard Worker
21*03ce13f7SAndroid Build Coastguard Worker namespace vk {
22*03ce13f7SAndroid Build Coastguard Worker
allocateDeviceMemory(size_t bytes,size_t alignment)23*03ce13f7SAndroid Build Coastguard Worker void *allocateDeviceMemory(size_t bytes, size_t alignment)
24*03ce13f7SAndroid Build Coastguard Worker {
25*03ce13f7SAndroid Build Coastguard Worker ASSERT(bytes <= vk::MAX_MEMORY_ALLOCATION_SIZE);
26*03ce13f7SAndroid Build Coastguard Worker
27*03ce13f7SAndroid Build Coastguard Worker #if defined(SWIFTSHADER_ZERO_INITIALIZE_DEVICE_MEMORY)
28*03ce13f7SAndroid Build Coastguard Worker return sw::allocateZeroOrPoison(bytes, alignment);
29*03ce13f7SAndroid Build Coastguard Worker #else
30*03ce13f7SAndroid Build Coastguard Worker return sw::allocate(bytes, alignment);
31*03ce13f7SAndroid Build Coastguard Worker #endif
32*03ce13f7SAndroid Build Coastguard Worker }
33*03ce13f7SAndroid Build Coastguard Worker
freeDeviceMemory(void * ptr)34*03ce13f7SAndroid Build Coastguard Worker void freeDeviceMemory(void *ptr)
35*03ce13f7SAndroid Build Coastguard Worker {
36*03ce13f7SAndroid Build Coastguard Worker sw::freeMemory(ptr);
37*03ce13f7SAndroid Build Coastguard Worker }
38*03ce13f7SAndroid Build Coastguard Worker
allocateHostMemory(size_t bytes,size_t alignment,const VkAllocationCallbacks * pAllocator,VkSystemAllocationScope allocationScope)39*03ce13f7SAndroid Build Coastguard Worker void *allocateHostMemory(size_t bytes, size_t alignment, const VkAllocationCallbacks *pAllocator, VkSystemAllocationScope allocationScope)
40*03ce13f7SAndroid Build Coastguard Worker {
41*03ce13f7SAndroid Build Coastguard Worker ASSERT(bytes <= vk::MAX_MEMORY_ALLOCATION_SIZE);
42*03ce13f7SAndroid Build Coastguard Worker
43*03ce13f7SAndroid Build Coastguard Worker if(pAllocator)
44*03ce13f7SAndroid Build Coastguard Worker {
45*03ce13f7SAndroid Build Coastguard Worker return pAllocator->pfnAllocation(pAllocator->pUserData, bytes, alignment, allocationScope);
46*03ce13f7SAndroid Build Coastguard Worker }
47*03ce13f7SAndroid Build Coastguard Worker else
48*03ce13f7SAndroid Build Coastguard Worker {
49*03ce13f7SAndroid Build Coastguard Worker // TODO(b/140991626): Use allocate() instead of allocateZeroOrPoison() to improve startup performance.
50*03ce13f7SAndroid Build Coastguard Worker return sw::allocateZeroOrPoison(bytes, alignment);
51*03ce13f7SAndroid Build Coastguard Worker }
52*03ce13f7SAndroid Build Coastguard Worker }
53*03ce13f7SAndroid Build Coastguard Worker
freeHostMemory(void * ptr,const VkAllocationCallbacks * pAllocator)54*03ce13f7SAndroid Build Coastguard Worker void freeHostMemory(void *ptr, const VkAllocationCallbacks *pAllocator)
55*03ce13f7SAndroid Build Coastguard Worker {
56*03ce13f7SAndroid Build Coastguard Worker if(pAllocator)
57*03ce13f7SAndroid Build Coastguard Worker {
58*03ce13f7SAndroid Build Coastguard Worker pAllocator->pfnFree(pAllocator->pUserData, ptr);
59*03ce13f7SAndroid Build Coastguard Worker }
60*03ce13f7SAndroid Build Coastguard Worker else
61*03ce13f7SAndroid Build Coastguard Worker {
62*03ce13f7SAndroid Build Coastguard Worker sw::freeMemory(ptr);
63*03ce13f7SAndroid Build Coastguard Worker }
64*03ce13f7SAndroid Build Coastguard Worker }
65*03ce13f7SAndroid Build Coastguard Worker
66*03ce13f7SAndroid Build Coastguard Worker } // namespace vk
67