1 /*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7 
8 #include "src/gpu/vk/GrVkMemory.h"
9 
10 #include "src/gpu/vk/GrVkGpu.h"
11 #include "src/gpu/vk/GrVkUtil.h"
12 
13 using AllocationPropertyFlags = GrVkMemoryAllocator::AllocationPropertyFlags;
14 using BufferUsage = GrVkMemoryAllocator::BufferUsage;
15 
AllocAndBindBufferMemory(GrVkGpu * gpu,VkBuffer buffer,BufferUsage usage,GrVkAlloc * alloc)16 bool GrVkMemory::AllocAndBindBufferMemory(GrVkGpu* gpu,
17                                           VkBuffer buffer,
18                                           BufferUsage usage,
19                                           GrVkAlloc* alloc) {
20     GrVkMemoryAllocator* allocator = gpu->memoryAllocator();
21     GrVkBackendMemory memory = 0;
22 
23     AllocationPropertyFlags propFlags;
24     bool shouldPersistentlyMapCpuToGpu = gpu->vkCaps().shouldPersistentlyMapCpuToGpuBuffers();
25     if (usage == BufferUsage::kTransfersFromCpuToGpu ||
26         (usage == BufferUsage::kCpuWritesGpuReads && shouldPersistentlyMapCpuToGpu)) {
27         // In general it is always fine (and often better) to keep buffers always mapped that we are
28         // writing to on the cpu.
29         propFlags = AllocationPropertyFlags::kPersistentlyMapped;
30     } else {
31         propFlags = AllocationPropertyFlags::kNone;
32     }
33 
34     VkResult result = allocator->allocateBufferMemory(buffer, usage, propFlags, &memory);
35     if (!gpu->checkVkResult(result)) {
36         return false;
37     }
38     allocator->getAllocInfo(memory, alloc);
39 
40     // Bind buffer
41     VkResult err;
42     GR_VK_CALL_RESULT(gpu, err, BindBufferMemory(gpu->device(), buffer, alloc->fMemory,
43                                                  alloc->fOffset));
44     if (err) {
45         FreeBufferMemory(gpu, *alloc);
46         return false;
47     }
48 
49     return true;
50 }
51 
FreeBufferMemory(const GrVkGpu * gpu,const GrVkAlloc & alloc)52 void GrVkMemory::FreeBufferMemory(const GrVkGpu* gpu, const GrVkAlloc& alloc) {
53     SkASSERT(alloc.fBackendMemory);
54     GrVkMemoryAllocator* allocator = gpu->memoryAllocator();
55     allocator->freeMemory(alloc.fBackendMemory);
56 }
57 
AllocAndBindImageMemory(GrVkGpu * gpu,VkImage image,bool linearTiling,GrVkAlloc * alloc)58 bool GrVkMemory::AllocAndBindImageMemory(GrVkGpu* gpu,
59                                          VkImage image,
60                                          bool linearTiling,
61                                          GrVkAlloc* alloc) {
62     SkASSERT(!linearTiling);
63     GrVkMemoryAllocator* allocator = gpu->memoryAllocator();
64     GrVkBackendMemory memory = 0;
65 
66     VkMemoryRequirements memReqs;
67     GR_VK_CALL(gpu->vkInterface(), GetImageMemoryRequirements(gpu->device(), image, &memReqs));
68 
69     AllocationPropertyFlags propFlags;
70     // If we ever find that our allocator is not aggressive enough in using dedicated image
71     // memory we can add a size check here to force the use of dedicate memory. However for now,
72     // we let the allocators decide. The allocator can query the GPU for each image to see if the
73     // GPU recommends or requires the use of dedicated memory.
74     if (gpu->vkCaps().shouldAlwaysUseDedicatedImageMemory()) {
75         propFlags = AllocationPropertyFlags::kDedicatedAllocation;
76     } else {
77         propFlags = AllocationPropertyFlags::kNone;
78     }
79 
80     if (gpu->protectedContext()) {
81         propFlags |= AllocationPropertyFlags::kProtected;
82     }
83 
84     VkResult result = allocator->allocateImageMemory(image, propFlags, &memory);
85     if (!gpu->checkVkResult(result)) {
86         return false;
87     }
88 
89     allocator->getAllocInfo(memory, alloc);
90 
91     // Bind buffer
92     VkResult err;
93     GR_VK_CALL_RESULT(gpu, err, BindImageMemory(gpu->device(), image, alloc->fMemory,
94                                                 alloc->fOffset));
95     if (err) {
96         FreeImageMemory(gpu, linearTiling, *alloc);
97         return false;
98     }
99 
100     return true;
101 }
102 
FreeImageMemory(const GrVkGpu * gpu,bool linearTiling,const GrVkAlloc & alloc)103 void GrVkMemory::FreeImageMemory(const GrVkGpu* gpu, bool linearTiling,
104                                  const GrVkAlloc& alloc) {
105     SkASSERT(alloc.fBackendMemory);
106     GrVkMemoryAllocator* allocator = gpu->memoryAllocator();
107     allocator->freeMemory(alloc.fBackendMemory);
108 }
109 
MapAlloc(GrVkGpu * gpu,const GrVkAlloc & alloc)110 void* GrVkMemory::MapAlloc(GrVkGpu* gpu, const GrVkAlloc& alloc) {
111     SkASSERT(GrVkAlloc::kMappable_Flag & alloc.fFlags);
112     SkASSERT(alloc.fBackendMemory);
113     GrVkMemoryAllocator* allocator = gpu->memoryAllocator();
114     void* mapPtr;
115     VkResult result = allocator->mapMemory(alloc.fBackendMemory, &mapPtr);
116     if (!gpu->checkVkResult(result)) {
117         return nullptr;
118     }
119     return mapPtr;
120 }
121 
UnmapAlloc(const GrVkGpu * gpu,const GrVkAlloc & alloc)122 void GrVkMemory::UnmapAlloc(const GrVkGpu* gpu, const GrVkAlloc& alloc) {
123     SkASSERT(alloc.fBackendMemory);
124     GrVkMemoryAllocator* allocator = gpu->memoryAllocator();
125     allocator->unmapMemory(alloc.fBackendMemory);
126 }
127 
GetNonCoherentMappedMemoryRange(const GrVkAlloc & alloc,VkDeviceSize offset,VkDeviceSize size,VkDeviceSize alignment,VkMappedMemoryRange * range)128 void GrVkMemory::GetNonCoherentMappedMemoryRange(const GrVkAlloc& alloc, VkDeviceSize offset,
129                                                  VkDeviceSize size, VkDeviceSize alignment,
130                                                  VkMappedMemoryRange* range) {
131     SkASSERT(alloc.fFlags & GrVkAlloc::kNoncoherent_Flag);
132     offset = offset + alloc.fOffset;
133     VkDeviceSize offsetDiff = offset & (alignment -1);
134     offset = offset - offsetDiff;
135     size = (size + alignment - 1) & ~(alignment - 1);
136 #ifdef SK_DEBUG
137     SkASSERT(offset >= alloc.fOffset);
138     SkASSERT(offset + size <= alloc.fOffset + alloc.fSize);
139     SkASSERT(0 == (offset & (alignment-1)));
140     SkASSERT(size > 0);
141     SkASSERT(0 == (size & (alignment-1)));
142 #endif
143 
144     memset(range, 0, sizeof(VkMappedMemoryRange));
145     range->sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
146     range->memory = alloc.fMemory;
147     range->offset = offset;
148     range->size = size;
149 }
150 
FlushMappedAlloc(GrVkGpu * gpu,const GrVkAlloc & alloc,VkDeviceSize offset,VkDeviceSize size)151 void GrVkMemory::FlushMappedAlloc(GrVkGpu* gpu, const GrVkAlloc& alloc, VkDeviceSize offset,
152                                   VkDeviceSize size) {
153     if (alloc.fFlags & GrVkAlloc::kNoncoherent_Flag) {
154         SkASSERT(offset == 0);
155         SkASSERT(size <= alloc.fSize);
156         SkASSERT(alloc.fBackendMemory);
157         GrVkMemoryAllocator* allocator = gpu->memoryAllocator();
158         VkResult result = allocator->flushMemory(alloc.fBackendMemory, offset, size);
159         gpu->checkVkResult(result);
160     }
161 }
162 
InvalidateMappedAlloc(GrVkGpu * gpu,const GrVkAlloc & alloc,VkDeviceSize offset,VkDeviceSize size)163 void GrVkMemory::InvalidateMappedAlloc(GrVkGpu* gpu, const GrVkAlloc& alloc,
164                                        VkDeviceSize offset, VkDeviceSize size) {
165     if (alloc.fFlags & GrVkAlloc::kNoncoherent_Flag) {
166         SkASSERT(offset == 0);
167         SkASSERT(size <= alloc.fSize);
168         SkASSERT(alloc.fBackendMemory);
169         GrVkMemoryAllocator* allocator = gpu->memoryAllocator();
170         VkResult result = allocator->invalidateMemory(alloc.fBackendMemory, offset, size);
171         gpu->checkVkResult(result);
172     }
173 }
174 
175