Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/gpu/vk/GrVkMemory.cpp

Issue 2348523002: Support use of non-coherent memory allocations in Vulkan. (Closed)
Patch Set: Some more clean up Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2015 Google Inc. 2 * Copyright 2015 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "GrVkMemory.h" 8 #include "GrVkMemory.h"
9 9
10 #include "GrVkGpu.h" 10 #include "GrVkGpu.h"
11 #include "GrVkUtil.h" 11 #include "GrVkUtil.h"
12 12
13 static bool get_valid_memory_type_index(VkPhysicalDeviceMemoryProperties physDev MemProps, 13 static bool get_valid_memory_type_index(const VkPhysicalDeviceMemoryProperties& physDevMemProps,
14 uint32_t typeBits, 14 uint32_t typeBits,
15 VkMemoryPropertyFlags requestedMemFlags, 15 VkMemoryPropertyFlags requestedMemFlags,
16 uint32_t* typeIndex) { 16 uint32_t* typeIndex) {
17 uint32_t checkBit = 1; 17 for (uint32_t i = 0; i < physDevMemProps.memoryTypeCount; ++i) {
18 for (uint32_t i = 0; i < 32; ++i) { 18 if (typeBits & (1 << i)) {
19 if (typeBits & checkBit) {
20 uint32_t supportedFlags = physDevMemProps.memoryTypes[i].propertyFla gs & 19 uint32_t supportedFlags = physDevMemProps.memoryTypes[i].propertyFla gs &
21 requestedMemFlags; 20 requestedMemFlags;
22 if (supportedFlags == requestedMemFlags) { 21 if (supportedFlags == requestedMemFlags) {
23 *typeIndex = i; 22 *typeIndex = i;
24 return true; 23 return true;
25 } 24 }
26 } 25 }
27 checkBit <<= 1;
28 } 26 }
29 return false; 27 return false;
30 } 28 }
31 29
32 static GrVkGpu::Heap buffer_type_to_heap(GrVkBuffer::Type type) { 30 static GrVkGpu::Heap buffer_type_to_heap(GrVkBuffer::Type type) {
33 const GrVkGpu::Heap kBufferToHeap[]{ 31 const GrVkGpu::Heap kBufferToHeap[]{
34 GrVkGpu::kVertexBuffer_Heap, 32 GrVkGpu::kVertexBuffer_Heap,
35 GrVkGpu::kIndexBuffer_Heap, 33 GrVkGpu::kIndexBuffer_Heap,
36 GrVkGpu::kUniformBuffer_Heap, 34 GrVkGpu::kUniformBuffer_Heap,
37 GrVkGpu::kCopyReadBuffer_Heap, 35 GrVkGpu::kCopyReadBuffer_Heap,
(...skipping 12 matching lines...) Expand all
50 VkBuffer buffer, 48 VkBuffer buffer,
51 GrVkBuffer::Type type, 49 GrVkBuffer::Type type,
52 bool dynamic, 50 bool dynamic,
53 GrVkAlloc* alloc) { 51 GrVkAlloc* alloc) {
54 const GrVkInterface* iface = gpu->vkInterface(); 52 const GrVkInterface* iface = gpu->vkInterface();
55 VkDevice device = gpu->device(); 53 VkDevice device = gpu->device();
56 54
57 VkMemoryRequirements memReqs; 55 VkMemoryRequirements memReqs;
58 GR_VK_CALL(iface, GetBufferMemoryRequirements(device, buffer, &memReqs)); 56 GR_VK_CALL(iface, GetBufferMemoryRequirements(device, buffer, &memReqs));
59 57
60 VkMemoryPropertyFlags desiredMemProps = dynamic ? VK_MEMORY_PROPERTY_HOST_VI SIBLE_BIT |
61 VK_MEMORY_PROPERTY_HOST_CO HERENT_BIT |
62 VK_MEMORY_PROPERTY_HOST_CA CHED_BIT
63 : VK_MEMORY_PROPERTY_DEVICE_ LOCAL_BIT;
64 uint32_t typeIndex = 0; 58 uint32_t typeIndex = 0;
65 if (!get_valid_memory_type_index(gpu->physicalDeviceMemoryProperties(), 59 const VkPhysicalDeviceMemoryProperties& phDevMemProps = gpu->physicalDeviceM emoryProperties();
66 memReqs.memoryTypeBits, 60 if (dynamic) {
67 desiredMemProps, 61 // try to get cached and ideally non-coherent memory first
68 &typeIndex)) { 62 if (!get_valid_memory_type_index(phDevMemProps,
69 // this memory type should always be available 63 memReqs.memoryTypeBits,
70 SkASSERT_RELEASE(get_valid_memory_type_index(gpu->physicalDeviceMemoryPr operties(), 64 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
65 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
66 &typeIndex)) {
67 // some sort of host-visible memory type should always be available for dynamic buffers
68 SkASSERT_RELEASE(get_valid_memory_type_index(phDevMemProps,
69 memReqs.memoryTypeBits,
70 VK_MEMORY_PROPERTY_HOST _VISIBLE_BIT,
71 &typeIndex));
72 }
73
74 VkMemoryPropertyFlags mpf = phDevMemProps.memoryTypes[typeIndex].propert yFlags;
75 alloc->fFlags = mpf & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT ? 0x0
76 : GrVkAlloc:: kNoncoherent_Flag;
77 } else {
78 // device-local memory should always be available for static buffers
79 SkASSERT_RELEASE(get_valid_memory_type_index(phDevMemProps,
71 memReqs.memoryTypeBits, 80 memReqs.memoryTypeBits,
72 VK_MEMORY_PROPERTY_HOST_VIS IBLE_BIT | 81 VK_MEMORY_PROPERTY_DEVICE_L OCAL_BIT,
73 VK_MEMORY_PROPERTY_HOST_COH ERENT_BIT,
74 &typeIndex)); 82 &typeIndex));
83 alloc->fFlags = 0x0;
75 } 84 }
76 85
77 GrVkHeap* heap = gpu->getHeap(buffer_type_to_heap(type)); 86 GrVkHeap* heap = gpu->getHeap(buffer_type_to_heap(type));
78 87
79 if (!heap->alloc(memReqs.size, memReqs.alignment, typeIndex, alloc)) { 88 if (!heap->alloc(memReqs.size, memReqs.alignment, typeIndex, alloc)) {
80 SkDebugf("Failed to alloc buffer\n"); 89 SkDebugf("Failed to alloc buffer\n");
81 return false; 90 return false;
82 } 91 }
83 92
84 // Bind Memory to device 93 // Bind buffer
85 VkResult err = GR_VK_CALL(iface, BindBufferMemory(device, buffer, 94 VkResult err = GR_VK_CALL(iface, BindBufferMemory(device, buffer,
86 alloc->fMemory, alloc->fOf fset)); 95 alloc->fMemory, alloc->fOf fset));
87 if (err) { 96 if (err) {
88 SkASSERT_RELEASE(heap->free(*alloc)); 97 SkASSERT_RELEASE(heap->free(*alloc));
89 return false; 98 return false;
90 } 99 }
91 100
92 return true; 101 return true;
93 } 102 }
94 103
(...skipping 20 matching lines...) Expand all
115 bool linearTiling, 124 bool linearTiling,
116 GrVkAlloc* alloc) { 125 GrVkAlloc* alloc) {
117 const GrVkInterface* iface = gpu->vkInterface(); 126 const GrVkInterface* iface = gpu->vkInterface();
118 VkDevice device = gpu->device(); 127 VkDevice device = gpu->device();
119 128
120 VkMemoryRequirements memReqs; 129 VkMemoryRequirements memReqs;
121 GR_VK_CALL(iface, GetImageMemoryRequirements(device, image, &memReqs)); 130 GR_VK_CALL(iface, GetImageMemoryRequirements(device, image, &memReqs));
122 131
123 uint32_t typeIndex = 0; 132 uint32_t typeIndex = 0;
124 GrVkHeap* heap; 133 GrVkHeap* heap;
134 const VkPhysicalDeviceMemoryProperties& phDevMemProps = gpu->physicalDeviceM emoryProperties();
125 if (linearTiling) { 135 if (linearTiling) {
126 VkMemoryPropertyFlags desiredMemProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_ BIT | 136 VkMemoryPropertyFlags desiredMemProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_ BIT |
127 VK_MEMORY_PROPERTY_HOST_COHERENT _BIT |
128 VK_MEMORY_PROPERTY_HOST_CACHED_B IT; 137 VK_MEMORY_PROPERTY_HOST_CACHED_B IT;
129 if (!get_valid_memory_type_index(gpu->physicalDeviceMemoryProperties(), 138 if (!get_valid_memory_type_index(phDevMemProps,
130 memReqs.memoryTypeBits, 139 memReqs.memoryTypeBits,
131 desiredMemProps, 140 desiredMemProps,
132 &typeIndex)) { 141 &typeIndex)) {
133 // this memory type should always be available 142 // some sort of host-visible memory type should always be available
134 SkASSERT_RELEASE(get_valid_memory_type_index(gpu->physicalDeviceMemo ryProperties(), 143 SkASSERT_RELEASE(get_valid_memory_type_index(phDevMemProps,
135 memReqs.memoryTypeBits, 144 memReqs.memoryTypeBits,
136 VK_MEMORY_PROPERTY_HOST _VISIBLE_BIT | 145 VK_MEMORY_PROPERTY_HOST _VISIBLE_BIT,
137 VK_MEMORY_PROPERTY_HOST _COHERENT_BIT,
138 &typeIndex)); 146 &typeIndex));
139 } 147 }
140 heap = gpu->getHeap(GrVkGpu::kLinearImage_Heap); 148 heap = gpu->getHeap(GrVkGpu::kLinearImage_Heap);
149 VkMemoryPropertyFlags mpf = phDevMemProps.memoryTypes[typeIndex].propert yFlags;
150 alloc->fFlags = mpf & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT ? 0x0
151 : GrVkAlloc:: kNoncoherent_Flag;
141 } else { 152 } else {
142 // this memory type should always be available 153 // this memory type should always be available
143 SkASSERT_RELEASE(get_valid_memory_type_index(gpu->physicalDeviceMemoryPr operties(), 154 SkASSERT_RELEASE(get_valid_memory_type_index(phDevMemProps,
144 memReqs.memoryTypeBits, 155 memReqs.memoryTypeBits,
145 VK_MEMORY_PROPERTY_DEVICE_L OCAL_BIT, 156 VK_MEMORY_PROPERTY_DEVICE_L OCAL_BIT,
146 &typeIndex)); 157 &typeIndex));
147 if (memReqs.size <= kMaxSmallImageSize) { 158 if (memReqs.size <= kMaxSmallImageSize) {
148 heap = gpu->getHeap(GrVkGpu::kSmallOptimalImage_Heap); 159 heap = gpu->getHeap(GrVkGpu::kSmallOptimalImage_Heap);
149 } else { 160 } else {
150 heap = gpu->getHeap(GrVkGpu::kOptimalImage_Heap); 161 heap = gpu->getHeap(GrVkGpu::kOptimalImage_Heap);
151 } 162 }
163 alloc->fFlags = 0x0;
152 } 164 }
153 165
154 if (!heap->alloc(memReqs.size, memReqs.alignment, typeIndex, alloc)) { 166 if (!heap->alloc(memReqs.size, memReqs.alignment, typeIndex, alloc)) {
155 SkDebugf("Failed to alloc image\n"); 167 SkDebugf("Failed to alloc image\n");
156 return false; 168 return false;
157 } 169 }
158 170
159 // Bind Memory to device 171 // Bind image
160 VkResult err = GR_VK_CALL(iface, BindImageMemory(device, image, 172 VkResult err = GR_VK_CALL(iface, BindImageMemory(device, image,
161 alloc->fMemory, alloc->fOffset)); 173 alloc->fMemory, alloc->fOffset));
162 if (err) { 174 if (err) {
163 SkASSERT_RELEASE(heap->free(*alloc)); 175 SkASSERT_RELEASE(heap->free(*alloc));
164 return false; 176 return false;
165 } 177 }
166 178
167 gTotalImageMemory += alloc->fSize; 179 gTotalImageMemory += alloc->fSize;
168 180
169 VkDeviceSize pageAlignedSize = align_size(alloc->fSize, kMinVulkanPageSize); 181 VkDeviceSize pageAlignedSize = align_size(alloc->fSize, kMinVulkanPageSize);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 } else if (VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL == layout) { 249 } else if (VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL == layout) {
238 flags = VK_ACCESS_TRANSFER_WRITE_BIT; 250 flags = VK_ACCESS_TRANSFER_WRITE_BIT;
239 } else if (VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == layout) { 251 } else if (VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == layout) {
240 flags = VK_ACCESS_TRANSFER_READ_BIT; 252 flags = VK_ACCESS_TRANSFER_READ_BIT;
241 } else if (VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == layout) { 253 } else if (VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == layout) {
242 flags = VK_ACCESS_SHADER_READ_BIT; 254 flags = VK_ACCESS_SHADER_READ_BIT;
243 } 255 }
244 return flags; 256 return flags;
245 } 257 }
246 258
259 void GrVkMemory::FlushMappedAlloc(const GrVkGpu* gpu, const GrVkAlloc& alloc) {
260 if (alloc.fFlags & GrVkAlloc::kNoncoherent_Flag) {
261 VkMappedMemoryRange mappedMemoryRange;
262 memset(&mappedMemoryRange, 0, sizeof(VkMappedMemoryRange));
263 mappedMemoryRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
264 mappedMemoryRange.memory = alloc.fMemory;
265 mappedMemoryRange.offset = alloc.fOffset;
266 mappedMemoryRange.size = alloc.fSize;
267 GR_VK_CALL(gpu->vkInterface(), FlushMappedMemoryRanges(gpu->device(),
268 1, &mappedMemoryR ange));
269 }
270 }
271
272 void GrVkMemory::InvalidateMappedAlloc(const GrVkGpu* gpu, const GrVkAlloc& allo c) {
273 if (alloc.fFlags & GrVkAlloc::kNoncoherent_Flag) {
274 VkMappedMemoryRange mappedMemoryRange;
275 memset(&mappedMemoryRange, 0, sizeof(VkMappedMemoryRange));
276 mappedMemoryRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
277 mappedMemoryRange.memory = alloc.fMemory;
278 mappedMemoryRange.offset = alloc.fOffset;
279 mappedMemoryRange.size = alloc.fSize;
280 GR_VK_CALL(gpu->vkInterface(), InvalidateMappedMemoryRanges(gpu->device( ),
281 1, &mappedMemoryR ange));
282 }
283 }
284
247 bool GrVkFreeListAlloc::alloc(VkDeviceSize requestedSize, 285 bool GrVkFreeListAlloc::alloc(VkDeviceSize requestedSize,
248 VkDeviceSize* allocOffset, VkDeviceSize* allocSize ) { 286 VkDeviceSize* allocOffset, VkDeviceSize* allocSize ) {
249 VkDeviceSize alignedSize = align_size(requestedSize, fAlignment); 287 VkDeviceSize alignedSize = align_size(requestedSize, fAlignment);
250 288
251 // find the smallest block big enough for our allocation 289 // find the smallest block big enough for our allocation
252 FreeList::Iter iter = fFreeList.headIter(); 290 FreeList::Iter iter = fFreeList.headIter();
253 FreeList::Iter bestFitIter; 291 FreeList::Iter bestFitIter;
254 VkDeviceSize bestFitSize = fSize + 1; 292 VkDeviceSize bestFitSize = fSize + 1;
255 VkDeviceSize secondLargestSize = 0; 293 VkDeviceSize secondLargestSize = 0;
256 VkDeviceSize secondLargestOffset = 0; 294 VkDeviceSize secondLargestOffset = 0;
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 fSubHeaps[i]->free(alloc); 591 fSubHeaps[i]->free(alloc);
554 fUsedSize -= alloc.fSize; 592 fUsedSize -= alloc.fSize;
555 return true; 593 return true;
556 } 594 }
557 } 595 }
558 596
559 return false; 597 return false;
560 } 598 }
561 599
562 600
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698