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

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

Issue 2232803003: Check allignment of sub heap allocation in vulkan (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 4 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 } 75 }
76 76
77 GrVkHeap* heap = gpu->getHeap(buffer_type_to_heap(type)); 77 GrVkHeap* heap = gpu->getHeap(buffer_type_to_heap(type));
78 78
79 if (!heap->alloc(memReqs.size, memReqs.alignment, typeIndex, alloc)) { 79 if (!heap->alloc(memReqs.size, memReqs.alignment, typeIndex, alloc)) {
80 SkDebugf("Failed to alloc buffer\n"); 80 SkDebugf("Failed to alloc buffer\n");
81 return false; 81 return false;
82 } 82 }
83 83
84 // Bind Memory to device 84 // Bind Memory to device
85 VkResult err = GR_VK_CALL(iface, BindBufferMemory(device, buffer, 85 VkResult err = GR_VK_CALL(iface, BindBufferMemory(device, buffer,
86 alloc->fMemory, alloc->fOf fset)); 86 alloc->fMemory, alloc->fOf fset));
87 if (err) { 87 if (err) {
88 SkASSERT_RELEASE(heap->free(*alloc)); 88 SkASSERT_RELEASE(heap->free(*alloc));
89 return false; 89 return false;
90 } 90 }
91 91
92 return true; 92 return true;
93 } 93 }
94 94
95 void GrVkMemory::FreeBufferMemory(const GrVkGpu* gpu, GrVkBuffer::Type type, 95 void GrVkMemory::FreeBufferMemory(const GrVkGpu* gpu, GrVkBuffer::Type type,
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 alloc->fMemory = fAlloc; 422 alloc->fMemory = fAlloc;
423 return INHERITED::alloc(size, &alloc->fOffset, &alloc->fSize); 423 return INHERITED::alloc(size, &alloc->fOffset, &alloc->fSize);
424 } 424 }
425 425
426 void GrVkSubHeap::free(const GrVkAlloc& alloc) { 426 void GrVkSubHeap::free(const GrVkAlloc& alloc) {
427 SkASSERT(alloc.fMemory == fAlloc); 427 SkASSERT(alloc.fMemory == fAlloc);
428 428
429 INHERITED::free(alloc.fOffset, alloc.fSize); 429 INHERITED::free(alloc.fOffset, alloc.fSize);
430 } 430 }
431 431
432 bool GrVkHeap::subAlloc(VkDeviceSize size, VkDeviceSize alignment, 432 bool GrVkHeap::subAlloc(VkDeviceSize size, VkDeviceSize alignment,
433 uint32_t memoryTypeIndex, GrVkAlloc* alloc) { 433 uint32_t memoryTypeIndex, GrVkAlloc* alloc) {
434 VkDeviceSize alignedSize = align_size(size, alignment); 434 VkDeviceSize alignedSize = align_size(size, alignment);
435 435
436 // if requested is larger than our subheap allocation, just alloc directly 436 // if requested is larger than our subheap allocation, just alloc directly
437 if (alignedSize > fSubHeapSize) { 437 if (alignedSize > fSubHeapSize) {
438 VkMemoryAllocateInfo allocInfo = { 438 VkMemoryAllocateInfo allocInfo = {
439 VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, // sType 439 VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, // sType
440 NULL, // pNext 440 NULL, // pNext
441 size, // allocationSize 441 size, // allocationSize
442 memoryTypeIndex, // memoryTypeIndex 442 memoryTypeIndex, // memoryTypeIndex
443 }; 443 };
444 444
445 VkResult err = GR_VK_CALL(fGpu->vkInterface(), AllocateMemory(fGpu->devi ce(), 445 VkResult err = GR_VK_CALL(fGpu->vkInterface(), AllocateMemory(fGpu->devi ce(),
446 &allocInfo , 446 &allocInfo ,
447 nullptr, 447 nullptr,
448 &alloc->fM emory)); 448 &alloc->fM emory));
449 if (VK_SUCCESS != err) { 449 if (VK_SUCCESS != err) {
450 return false; 450 return false;
451 } 451 }
452 alloc->fOffset = 0; 452 alloc->fOffset = 0;
453 alloc->fSize = 0; // hint that this is not a subheap allocation 453 alloc->fSize = 0; // hint that this is not a subheap allocation
454 454
455 return true; 455 return true;
456 } 456 }
457 457
458 // first try to find a subheap that fits our allocation request 458 // first try to find a subheap that fits our allocation request
459 int bestFitIndex = -1; 459 int bestFitIndex = -1;
460 VkDeviceSize bestFitSize = 0x7FFFFFFF; 460 VkDeviceSize bestFitSize = 0x7FFFFFFF;
461 for (auto i = 0; i < fSubHeaps.count(); ++i) { 461 for (auto i = 0; i < fSubHeaps.count(); ++i) {
462 if (fSubHeaps[i]->memoryTypeIndex() == memoryTypeIndex) { 462 if (fSubHeaps[i]->memoryTypeIndex() == memoryTypeIndex &&
463 fSubHeaps[i]->alignment() == alignment) {
463 VkDeviceSize heapSize = fSubHeaps[i]->largestBlockSize(); 464 VkDeviceSize heapSize = fSubHeaps[i]->largestBlockSize();
464 if (heapSize >= alignedSize && heapSize < bestFitSize) { 465 if (heapSize >= alignedSize && heapSize < bestFitSize) {
465 bestFitIndex = i; 466 bestFitIndex = i;
466 bestFitSize = heapSize; 467 bestFitSize = heapSize;
467 } 468 }
468 } 469 }
469 } 470 }
470 471
471 if (bestFitIndex >= 0) { 472 if (bestFitIndex >= 0) {
472 SkASSERT(fSubHeaps[bestFitIndex]->alignment() == alignment); 473 SkASSERT(fSubHeaps[bestFitIndex]->alignment() == alignment);
egdaniel 2016/08/10 14:46:04 This assert fires without the change
473 if (fSubHeaps[bestFitIndex]->alloc(size, alloc)) { 474 if (fSubHeaps[bestFitIndex]->alloc(size, alloc)) {
474 fUsedSize += alloc->fSize; 475 fUsedSize += alloc->fSize;
475 return true; 476 return true;
476 } 477 }
477 return false; 478 return false;
478 } 479 }
479 480
480 // need to allocate a new subheap 481 // need to allocate a new subheap
481 SkAutoTDelete<GrVkSubHeap>& subHeap = fSubHeaps.push_back(); 482 SkAutoTDelete<GrVkSubHeap>& subHeap = fSubHeaps.push_back();
482 subHeap.reset(new GrVkSubHeap(fGpu, memoryTypeIndex, fSubHeapSize, alignment )); 483 subHeap.reset(new GrVkSubHeap(fGpu, memoryTypeIndex, fSubHeapSize, alignment ));
483 // try to recover from failed allocation by only allocating what we need 484 // try to recover from failed allocation by only allocating what we need
484 if (subHeap->size() == 0) { 485 if (subHeap->size() == 0) {
485 VkDeviceSize alignedSize = align_size(size, alignment); 486 VkDeviceSize alignedSize = align_size(size, alignment);
486 subHeap.reset(new GrVkSubHeap(fGpu, memoryTypeIndex, alignedSize, alignm ent)); 487 subHeap.reset(new GrVkSubHeap(fGpu, memoryTypeIndex, alignedSize, alignm ent));
487 if (subHeap->size() == 0) { 488 if (subHeap->size() == 0) {
488 return false; 489 return false;
489 } 490 }
490 } 491 }
491 fAllocSize += fSubHeapSize; 492 fAllocSize += fSubHeapSize;
492 if (subHeap->alloc(size, alloc)) { 493 if (subHeap->alloc(size, alloc)) {
493 fUsedSize += alloc->fSize; 494 fUsedSize += alloc->fSize;
494 return true; 495 return true;
495 } 496 }
496 497
497 return false; 498 return false;
498 } 499 }
499 500
500 bool GrVkHeap::singleAlloc(VkDeviceSize size, VkDeviceSize alignment, 501 bool GrVkHeap::singleAlloc(VkDeviceSize size, VkDeviceSize alignment,
501 uint32_t memoryTypeIndex, GrVkAlloc* alloc) { 502 uint32_t memoryTypeIndex, GrVkAlloc* alloc) {
502 VkDeviceSize alignedSize = align_size(size, alignment); 503 VkDeviceSize alignedSize = align_size(size, alignment);
503 504
504 // first try to find an unallocated subheap that fits our allocation request 505 // first try to find an unallocated subheap that fits our allocation request
505 int bestFitIndex = -1; 506 int bestFitIndex = -1;
506 VkDeviceSize bestFitSize = 0x7FFFFFFF; 507 VkDeviceSize bestFitSize = 0x7FFFFFFF;
507 for (auto i = 0; i < fSubHeaps.count(); ++i) { 508 for (auto i = 0; i < fSubHeaps.count(); ++i) {
508 if (fSubHeaps[i]->memoryTypeIndex() == memoryTypeIndex && fSubHeaps[i]-> unallocated()) { 509 if (fSubHeaps[i]->memoryTypeIndex() == memoryTypeIndex &&
510 fSubHeaps[i]->alignment() == alignment &&
511 fSubHeaps[i]->unallocated()) {
509 VkDeviceSize heapSize = fSubHeaps[i]->size(); 512 VkDeviceSize heapSize = fSubHeaps[i]->size();
510 if (heapSize >= alignedSize && heapSize < bestFitSize) { 513 if (heapSize >= alignedSize && heapSize < bestFitSize) {
511 bestFitIndex = i; 514 bestFitIndex = i;
512 bestFitSize = heapSize; 515 bestFitSize = heapSize;
513 } 516 }
514 } 517 }
515 } 518 }
516 519
517 if (bestFitIndex >= 0) { 520 if (bestFitIndex >= 0) {
518 SkASSERT(fSubHeaps[bestFitIndex]->alignment() == alignment); 521 SkASSERT(fSubHeaps[bestFitIndex]->alignment() == alignment);
(...skipping 29 matching lines...) Expand all
548 fSubHeaps[i]->free(alloc); 551 fSubHeaps[i]->free(alloc);
549 fUsedSize -= alloc.fSize; 552 fUsedSize -= alloc.fSize;
550 return true; 553 return true;
551 } 554 }
552 } 555 }
553 556
554 return false; 557 return false;
555 } 558 }
556 559
557 560
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698