Debian Codegraph @

src/gpu/ganesh/vk/GrVkBuffer.cpp

raw
1 /*
2 * Copyright 2021 Google LLC
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/ganesh/vk/GrVkBuffer.h"
9
10 #include "include/gpu/GpuTypes.h"
11 #include "include/gpu/ganesh/GrDirectContext.h"
12 #include "include/gpu/vk/VulkanMemoryAllocator.h"
13 #include "include/private/base/SkAlign.h"
14 #include "include/private/base/SkAssert.h"
15 #include "include/private/base/SkDebug.h"
16 #include "include/private/base/SkTemplates.h"
17 #include "src/gpu/ganesh/GrDirectContextPriv.h"
18 #include "src/gpu/ganesh/GrResourceProvider.h"
19 #include "src/gpu/ganesh/vk/GrVkCaps.h"
20 #include "src/gpu/ganesh/vk/GrVkDescriptorSet.h"
21 #include "src/gpu/ganesh/vk/GrVkGpu.h"
22 #include "src/gpu/ganesh/vk/GrVkResourceProvider.h"
23 #include "src/gpu/ganesh/vk/GrVkUniformHandler.h"
24 #include "src/gpu/ganesh/vk/GrVkUtil.h"
25 #include "src/gpu/vk/VulkanMemory.h"
26
27 #include <cstring>
28 #include <functional>
29 #include <utility>
30
31 #define VK_CALL(GPU, X) GR_VK_CALL(GPU->vkInterface(), X)
32
33 GrVkBuffer::GrVkBuffer(GrVkGpu* gpu,
34 size_t sizeInBytes,
35 GrGpuBufferType bufferType,
36 GrAccessPattern accessPattern,
37 VkBuffer buffer,
38 const skgpu::VulkanAlloc& alloc,
39 const GrVkDescriptorSet* uniformDescriptorSet,
40 std::string_view label)
41 : GrGpuBuffer(gpu, sizeInBytes, bufferType, accessPattern, label)
42 , fBuffer(buffer)
43 , fAlloc(alloc)
44 , fUniformDescriptorSet(uniformDescriptorSet) {
45 // We always require dynamic buffers to be mappable
46 SkASSERT(accessPattern != kDynamic_GrAccessPattern || this->isVkMappable());
47 SkASSERT(bufferType != GrGpuBufferType::kUniform || uniformDescriptorSet);
48 this->registerWithCache(skgpu::Budgeted::kYes);
49 }
50
51 static const GrVkDescriptorSet* make_uniform_desc_set(GrVkGpu* gpu, VkBuffer buffer, size_t size) {
52 const GrVkDescriptorSet* descriptorSet = gpu->resourceProvider().getUniformDescriptorSet();
53 if (!descriptorSet) {
54 return nullptr;
55 }
56
57 VkDescriptorBufferInfo bufferInfo;
58 memset(&bufferInfo, 0, sizeof(VkDescriptorBufferInfo));
59 bufferInfo.buffer = buffer;
60 bufferInfo.offset = 0;
61 bufferInfo.range = size;
62
63 VkWriteDescriptorSet descriptorWrite;
64 memset(&descriptorWrite, 0, sizeof(VkWriteDescriptorSet));
65 descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
66 descriptorWrite.pNext = nullptr;
67 descriptorWrite.dstSet = *descriptorSet->descriptorSet();
68 descriptorWrite.dstBinding = GrVkUniformHandler::kUniformBinding;
69 descriptorWrite.dstArrayElement = 0;
70 descriptorWrite.descriptorCount = 1;
71 descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
72 descriptorWrite.pImageInfo = nullptr;
73 descriptorWrite.pBufferInfo = &bufferInfo;
74 descriptorWrite.pTexelBufferView = nullptr;
75
76 GR_VK_CALL(gpu->vkInterface(),
77 UpdateDescriptorSets(gpu->device(), 1, &descriptorWrite, 0, nullptr));
78 return descriptorSet;
79 }
80
81 sk_sp<GrVkBuffer> GrVkBuffer::Make(GrVkGpu* gpu,
82 size_t size,
83 GrGpuBufferType bufferType,
84 GrAccessPattern accessPattern) {
85 VkBuffer buffer;
86 skgpu::VulkanAlloc alloc;
87
88 bool isProtected = gpu->protectedContext() &&
89 accessPattern == kStatic_GrAccessPattern &&
90 bufferType != GrGpuBufferType::kVertex &&
91 bufferType != GrGpuBufferType::kIndex &&
92 bufferType != GrGpuBufferType::kDrawIndirect;
93
94 // Protected memory _never_ uses mappable buffers.
95 // Otherwise, the only time we don't require mappable buffers is when we have a static
96 // access pattern and we're on a device where gpu only memory has faster reads on the gpu than
97 // memory that is also mappable on the cpu.
98 bool requiresMappable = !isProtected &&
99 (accessPattern == kDynamic_GrAccessPattern ||
100 accessPattern == kStream_GrAccessPattern ||
101 !gpu->vkCaps().gpuOnlyBuffersMorePerformant());
102
103 using BufferUsage = skgpu::VulkanMemoryAllocator::BufferUsage;
104 BufferUsage allocUsage;
105
106 if (bufferType == GrGpuBufferType::kXferCpuToGpu) {
107 allocUsage = BufferUsage::kTransfersFromCpuToGpu;
108 } else if (bufferType == GrGpuBufferType::kXferGpuToCpu) {
109 allocUsage = BufferUsage::kTransfersFromGpuToCpu;
110 } else {
111 allocUsage = requiresMappable ? BufferUsage::kCpuWritesGpuReads : BufferUsage::kGpuOnly;
112 }
113
114 // create the buffer object
115 VkBufferCreateInfo bufInfo;
116 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
117 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
118 bufInfo.flags = isProtected ? VK_BUFFER_CREATE_PROTECTED_BIT : 0;
119 bufInfo.size = size;
120 // To support SkMesh buffer updates we make Vertex and Index buffers capable of being transfer
121 // dsts.
122 switch (bufferType) {
123 case GrGpuBufferType::kVertex:
124 bufInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
125 break;
126 case GrGpuBufferType::kIndex:
127 bufInfo.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
128 break;
129 case GrGpuBufferType::kDrawIndirect:
130 bufInfo.usage = VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT;
131 break;
132 case GrGpuBufferType::kUniform:
133 bufInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
134 break;
135 case GrGpuBufferType::kXferCpuToGpu:
136 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
137 break;
138 case GrGpuBufferType::kXferGpuToCpu:
139 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT;
140 break;
141 }
142 // We may not always get a mappable buffer for non dynamic access buffers. Thus we set the
143 // transfer dst usage bit in case we need to do a copy to write data.
144 // TODO: It doesn't really hurt setting this extra usage flag, but maybe we can narrow the scope
145 // of buffers we set it on more than just not dynamic.
146 if (!requiresMappable) {
147 bufInfo.usage |= VK_BUFFER_USAGE_TRANSFER_DST_BIT;
148 }
149
150 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
151 bufInfo.queueFamilyIndexCount = 0;
152 bufInfo.pQueueFamilyIndices = nullptr;
153
154 VkResult err;
155 GR_VK_CALL_RESULT(gpu, err, CreateBuffer(gpu->device(), &bufInfo, nullptr, &buffer));
156 if (err) {
157 return nullptr;
158 }
159
160 bool shouldPersistentlyMapCpuToGpu = gpu->vkCaps().shouldPersistentlyMapCpuToGpuBuffers();
161 auto checkResult = [gpu, allocUsage, shouldPersistentlyMapCpuToGpu](VkResult result) {
162 GR_VK_LOG_IF_NOT_SUCCESS(gpu, result, "skgpu::VulkanMemory::AllocBufferMemory "
163 "(allocUsage:%d, shouldPersistentlyMapCpuToGpu:%d)",
164 (int)allocUsage, (int)shouldPersistentlyMapCpuToGpu);
165 return gpu->checkVkResult(result);
166 };
167 auto allocator = gpu->memoryAllocator();
168 if (!skgpu::VulkanMemory::AllocBufferMemory(allocator,
169 buffer,
170 skgpu::Protected(isProtected),
171 allocUsage,
172 shouldPersistentlyMapCpuToGpu,
173 checkResult,
174 &alloc)) {
175 VK_CALL(gpu, DestroyBuffer(gpu->device(), buffer, nullptr));
176 return nullptr;
177 }
178
179 // Bind buffer
180 GR_VK_CALL_RESULT(gpu, err, BindBufferMemory(gpu->device(),
181 buffer,
182 alloc.fMemory,
183 alloc.fOffset));
184 if (err) {
185 skgpu::VulkanMemory::FreeBufferMemory(allocator, alloc);
186 VK_CALL(gpu, DestroyBuffer(gpu->device(), buffer, nullptr));
187 return nullptr;
188 }
189
190 // If this is a uniform buffer we must setup a descriptor set
191 const GrVkDescriptorSet* uniformDescSet = nullptr;
192 if (bufferType == GrGpuBufferType::kUniform) {
193 uniformDescSet = make_uniform_desc_set(gpu, buffer, size);
194 if (!uniformDescSet) {
195 VK_CALL(gpu, DestroyBuffer(gpu->device(), buffer, nullptr));
196 skgpu::VulkanMemory::FreeBufferMemory(allocator, alloc);
197 return nullptr;
198 }
199 }
200
201 return sk_sp<GrVkBuffer>(new GrVkBuffer(
202 gpu, size, bufferType, accessPattern, buffer, alloc, uniformDescSet,
203 /*label=*/"MakeVkBuffer"));
204 }
205
206 void GrVkBuffer::vkMap(size_t readOffset, size_t readSize) {
207 SkASSERT(!fMapPtr);
208 if (this->isVkMappable()) {
209 // Not every buffer will use command buffer usage refs and instead the command buffer just
210 // holds normal refs. Systems higher up in Ganesh should be making sure not to reuse a
211 // buffer that currently has a ref held by something else. However, we do need to make sure
212 // there isn't a buffer with just a command buffer usage that is trying to be mapped.
213 SkASSERT(this->internalHasNoCommandBufferUsages());
214 SkASSERT(fAlloc.fSize > 0);
215 SkASSERT(fAlloc.fSize >= readOffset + readSize);
216
217 GrVkGpu* gpu = this->getVkGpu();
218 auto checkResult_mapAlloc = [gpu](VkResult result) {
219 GR_VK_LOG_IF_NOT_SUCCESS(gpu, result, "skgpu::VulkanMemory::MapAlloc");
220 return gpu->checkVkResult(result);
221 };
222 auto allocator = gpu->memoryAllocator();
223 fMapPtr = skgpu::VulkanMemory::MapAlloc(allocator, fAlloc, checkResult_mapAlloc);
224 if (fMapPtr && readSize != 0) {
225 auto checkResult_invalidateMapAlloc = [gpu, readOffset, readSize](VkResult result) {
226 GR_VK_LOG_IF_NOT_SUCCESS(gpu, result, "skgpu::VulkanMemory::InvalidateMappedAlloc "
227 "(readOffset:%zu, readSize:%zu)",
228 readOffset, readSize);
229 return gpu->checkVkResult(result);
230 };
231 // "Invalidate" here means make device writes visible to the host. That is, it makes
232 // sure any GPU writes are finished in the range we might read from.
233 skgpu::VulkanMemory::InvalidateMappedAlloc(allocator,
234 fAlloc,
235 readOffset,
236 readSize,
237 checkResult_invalidateMapAlloc);
238 }
239 }
240 }
241
242 void GrVkBuffer::vkUnmap(size_t flushOffset, size_t flushSize) {
243 SkASSERT(fMapPtr && this->isVkMappable());
244
245 SkASSERT(fAlloc.fSize > 0);
246 SkASSERT(fAlloc.fSize >= flushOffset + flushSize);
247
248 GrVkGpu* gpu = this->getVkGpu();
249 auto checkResult = [gpu, flushOffset, flushSize](VkResult result) {
250 GR_VK_LOG_IF_NOT_SUCCESS(gpu, result, "skgpu::VulkanMemory::FlushMappedAlloc "
251 "(flushOffset:%zu, flushSize:%zu)",
252 flushOffset, flushSize);
253 return gpu->checkVkResult(result);
254 };
255 auto allocator = this->getVkGpu()->memoryAllocator();
256 skgpu::VulkanMemory::FlushMappedAlloc(allocator, fAlloc, flushOffset, flushSize, checkResult);
257 skgpu::VulkanMemory::UnmapAlloc(allocator, fAlloc);
258 }
259
260 void GrVkBuffer::copyCpuDataToGpuBuffer(const void* src, size_t offset, size_t size) {
261 SkASSERT(src);
262
263 GrVkGpu* gpu = this->getVkGpu();
264
265 // The vulkan api restricts the use of vkCmdUpdateBuffer to updates that are less than or equal
266 // to 65536 bytes and a size and offset that are both 4 byte aligned.
267 if ((size <= 65536) && SkIsAlign4(size) && SkIsAlign4(offset) &&
268 !gpu->vkCaps().avoidUpdateBuffers()) {
269 gpu->updateBuffer(sk_ref_sp(this), src, offset, size);
270 } else {
271 GrResourceProvider* resourceProvider = gpu->getContext()->priv().resourceProvider();
272 sk_sp<GrGpuBuffer> transferBuffer = resourceProvider->createBuffer(
273 src,
274 size,
275 GrGpuBufferType::kXferCpuToGpu,
276 kDynamic_GrAccessPattern);
277 if (!transferBuffer) {
278 return;
279 }
280
281 gpu->transferFromBufferToBuffer(std::move(transferBuffer),
282 /*srcOffset=*/0,
283 sk_ref_sp(this),
284 offset,
285 size);
286 }
287 }
288
289 void GrVkBuffer::addMemoryBarrier(VkAccessFlags srcAccessMask,
290 VkAccessFlags dstAccesMask,
291 VkPipelineStageFlags srcStageMask,
292 VkPipelineStageFlags dstStageMask,
293 bool byRegion) const {
294 VkBufferMemoryBarrier bufferMemoryBarrier = {
295 VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER, // sType
296 nullptr, // pNext
297 srcAccessMask, // srcAccessMask
298 dstAccesMask, // dstAccessMask
299 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
300 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
301 fBuffer, // buffer
302 0, // offset
303 this->size(), // size
304 };
305
306 // TODO: restrict to area of buffer we're interested in
307 this->getVkGpu()->addBufferMemoryBarrier(srcStageMask, dstStageMask, byRegion,
308 &bufferMemoryBarrier);
309 }
310
311 void GrVkBuffer::vkRelease() {
312 if (this->wasDestroyed()) {
313 return;
314 }
315
316 if (fMapPtr) {
317 this->vkUnmap(0, this->size());
318 fMapPtr = nullptr;
319 }
320
321 if (fUniformDescriptorSet) {
322 fUniformDescriptorSet->recycle();
323 fUniformDescriptorSet = nullptr;
324 }
325
326 SkASSERT(fBuffer);
327 SkASSERT(fAlloc.fMemory && fAlloc.fBackendMemory);
328 VK_CALL(this->getVkGpu(), DestroyBuffer(this->getVkGpu()->device(), fBuffer, nullptr));
329 fBuffer = VK_NULL_HANDLE;
330
331 skgpu::VulkanMemory::FreeBufferMemory(this->getVkGpu()->memoryAllocator(), fAlloc);
332 fAlloc.fMemory = VK_NULL_HANDLE;
333 fAlloc.fBackendMemory = 0;
334 }
335
336 void GrVkBuffer::onRelease() {
337 this->vkRelease();
338 this->GrGpuBuffer::onRelease();
339 }
340
341 void GrVkBuffer::onAbandon() {
342 this->vkRelease();
343 this->GrGpuBuffer::onAbandon();
344 }
345
346 void GrVkBuffer::onMap(MapType type) {
347 this->vkMap(0, type == MapType::kRead ? this->size() : 0);
348 }
349
350 void GrVkBuffer::onUnmap(MapType type) {
351 this->vkUnmap(0, type == MapType::kWriteDiscard ? this->size() : 0);
352 }
353
354 bool GrVkBuffer::onClearToZero() { return this->getVkGpu()->zeroBuffer(sk_ref_sp(this)); }
355
356 bool GrVkBuffer::onUpdateData(const void* src, size_t offset, size_t size, bool /*preserve*/) {
357 if (this->isVkMappable()) {
358 // We won't be reading the mapped memory so pass an empty range.
359 this->vkMap(0, 0);
360 if (!fMapPtr) {
361 return false;
362 }
363 memcpy(SkTAddOffset<void>(fMapPtr, offset), src, size);
364 // We only need to flush the updated portion so pass the true range here.
365 this->vkUnmap(offset, size);
366 fMapPtr = nullptr;
367 } else {
368 this->copyCpuDataToGpuBuffer(src, offset, size);
369 }
370 return true;
371 }
372
373 GrVkGpu* GrVkBuffer::getVkGpu() const {
374 SkASSERT(!this->wasDestroyed());
375 return static_cast<GrVkGpu*>(this->getGpu());
376 }
377
378 const VkDescriptorSet* GrVkBuffer::uniformDescriptorSet() const {
379 SkASSERT(fUniformDescriptorSet);
380 return fUniformDescriptorSet->descriptorSet();
381 }