Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
VulkanMemory.cpp
Go to the documentation of this file.
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
9
11
12namespace skgpu {
13
15
17 VkBuffer buffer,
19 bool shouldPersistentlyMapCpuToGpu,
20 const std::function<CheckResult>& checkResult,
21 VulkanAlloc* alloc) {
22 VulkanBackendMemory memory = 0;
23 uint32_t propFlags;
24 if (usage == BufferUsage::kTransfersFromCpuToGpu ||
25 (usage == BufferUsage::kCpuWritesGpuReads && shouldPersistentlyMapCpuToGpu)) {
26 // In general it is always fine (and often better) to keep buffers always mapped that we are
27 // writing to on the cpu.
29 } else {
31 }
32
33 VkResult result = allocator->allocateBufferMemory(buffer, usage, propFlags, &memory);
34 if (!checkResult(result)) {
35 return false;
36 }
37 allocator->getAllocInfo(memory, alloc);
38 return true;
39}
40
43 allocator->freeMemory(alloc.fBackendMemory);
44}
45
47 VkImage image,
48 Protected isProtected,
49 bool forceDedicatedMemory,
50 bool useLazyAllocation,
51 const std::function<CheckResult>& checkResult,
52 VulkanAlloc* alloc) {
53 VulkanBackendMemory memory = 0;
54
55 uint32_t propFlags;
56 // If we ever find that our allocator is not aggressive enough in using dedicated image
57 // memory we can add a size check here to force the use of dedicate memory. However for now,
58 // we let the allocators decide. The allocator can query the GPU for each image to see if the
59 // GPU recommends or requires the use of dedicated memory.
60 if (forceDedicatedMemory) {
62 } else {
64 }
65
66 if (isProtected == Protected::kYes) {
68 }
69
70 if (useLazyAllocation) {
72 }
73
74 VkResult result = allocator->allocateImageMemory(image, propFlags, &memory);
75 if (!checkResult(result)) {
76 return false;
77 }
78
79 allocator->getAllocInfo(memory, alloc);
80 return true;
81}
82
84 const VulkanAlloc& alloc) {
86 allocator->freeMemory(alloc.fBackendMemory);
87}
88
90 const VulkanAlloc& alloc,
91 const std::function<CheckResult>& checkResult) {
94 void* mapPtr;
95 VkResult result = allocator->mapMemory(alloc.fBackendMemory, &mapPtr);
96 if (!checkResult(result)) {
97 return nullptr;
98 }
99 return mapPtr;
100}
101
103 const VulkanAlloc& alloc) {
105 allocator->unmapMemory(alloc.fBackendMemory);
106}
107
110 VkDeviceSize size,
111 VkDeviceSize alignment,
112 VkMappedMemoryRange* range) {
114 offset = offset + alloc.fOffset;
115 VkDeviceSize offsetDiff = offset & (alignment -1);
116 offset = offset - offsetDiff;
117 size = (size + alignment - 1) & ~(alignment - 1);
118#ifdef SK_DEBUG
119 SkASSERT(offset >= alloc.fOffset);
120 SkASSERT(offset + size <= alloc.fOffset + alloc.fSize);
121 SkASSERT(0 == (offset & (alignment-1)));
122 SkASSERT(size > 0);
123 SkASSERT(0 == (size & (alignment-1)));
124#endif
125
126 memset(range, 0, sizeof(VkMappedMemoryRange));
128 range->memory = alloc.fMemory;
129 range->offset = offset;
130 range->size = size;
131}
132
134 const VulkanAlloc& alloc,
136 VkDeviceSize size,
137 const std::function<CheckResult>& checkResult) {
139 SkASSERT(offset == 0);
140 SkASSERT(size <= alloc.fSize);
142 VkResult result = allocator->flushMemory(alloc.fBackendMemory, offset, size);
143 checkResult(result);
144 }
145}
146
148 const VulkanAlloc& alloc,
150 VkDeviceSize size,
151 const std::function<CheckResult>& checkResult) {
153 SkASSERT(offset == 0);
154 SkASSERT(size <= alloc.fSize);
156 VkResult result = allocator->invalidateMemory(alloc.fBackendMemory, offset, size);
157 checkResult(result);
158 }
159}
160
161} // namespace skgpu
162
#define SkASSERT(cond)
Definition SkAssert.h:116
virtual void unmapMemory(const skgpu::VulkanBackendMemory &)=0
virtual VkResult flushMemory(const skgpu::VulkanBackendMemory &memory, VkDeviceSize offset, VkDeviceSize size)
virtual void * mapMemory(const skgpu::VulkanBackendMemory &)
virtual void freeMemory(const skgpu::VulkanBackendMemory &)=0
virtual VkResult allocateImageMemory(VkImage image, uint32_t allocationPropertyFlags, skgpu::VulkanBackendMemory *memory)=0
virtual VkResult allocateBufferMemory(VkBuffer buffer, BufferUsage usage, uint32_t allocationPropertyFlags, skgpu::VulkanBackendMemory *memory)=0
virtual void getAllocInfo(const skgpu::VulkanBackendMemory &, skgpu::VulkanAlloc *) const =0
virtual VkResult invalidateMemory(const skgpu::VulkanBackendMemory &memory, VkDeviceSize offset, VkDeviceSize size)
sk_sp< SkImage > image
Definition examples.cpp:29
static const uint8_t buffer[]
GAsyncResult * result
void FreeBufferMemory(VulkanMemoryAllocator *, const VulkanAlloc &alloc)
bool AllocBufferMemory(VulkanMemoryAllocator *, VkBuffer buffer, skgpu::VulkanMemoryAllocator::BufferUsage, bool shouldPersistentlyMapCpuToGpu, const std::function< CheckResult > &, VulkanAlloc *alloc)
void GetNonCoherentMappedMemoryRange(const VulkanAlloc &, VkDeviceSize offset, VkDeviceSize size, VkDeviceSize alignment, VkMappedMemoryRange *)
void FreeImageMemory(VulkanMemoryAllocator *, const VulkanAlloc &alloc)
void * MapAlloc(VulkanMemoryAllocator *, const VulkanAlloc &, const std::function< CheckResult > &)
void FlushMappedAlloc(VulkanMemoryAllocator *, const skgpu::VulkanAlloc &, VkDeviceSize offset, VkDeviceSize size, const std::function< CheckResult > &)
bool AllocImageMemory(VulkanMemoryAllocator *, VkImage image, skgpu::Protected isProtected, bool forceDedicatedMemory, bool useLazyAllocation, const std::function< CheckResult > &, VulkanAlloc *alloc)
void UnmapAlloc(VulkanMemoryAllocator *, const VulkanAlloc &alloc)
void InvalidateMappedAlloc(VulkanMemoryAllocator *, const VulkanAlloc &alloc, VkDeviceSize offset, VkDeviceSize size, const std::function< CheckResult > &)
intptr_t VulkanBackendMemory
Definition VulkanTypes.h:31
Protected
Definition GpuTypes.h:61
static void usage(char *argv0)
Point offset
VkDeviceSize offset
VkDeviceMemory memory
VkStructureType sType
VkDeviceSize fSize
Definition VulkanTypes.h:40
VulkanBackendMemory fBackendMemory
Definition VulkanTypes.h:43
VkDeviceMemory fMemory
Definition VulkanTypes.h:38
VkDeviceSize fOffset
Definition VulkanTypes.h:39
uint64_t VkDeviceSize
Definition vulkan_core.h:96
VkResult
@ VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE