blob: d28a8ccbf1e1cfaf542dd0293a47e13a21ed9ee2 [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
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 "GrVkBuffer.h"
9#include "GrVkGpu.h"
10#include "GrVkMemory.h"
11#include "GrVkUtil.h"
12
13#define VK_CALL(GPU, X) GR_VK_CALL(GPU->vkInterface(), X)
14
15#ifdef SK_DEBUG
16#define VALIDATE() this->validate()
17#else
18#define VALIDATE() do {} while(false)
19#endif
20
21const GrVkBuffer::Resource* GrVkBuffer::Create(const GrVkGpu* gpu, const Desc& desc) {
22 VkBuffer buffer;
jvanverth1e305ba2016-06-01 09:39:15 -070023 GrVkAlloc alloc;
Greg Daniel164a9f02016-02-22 09:56:40 -050024
25 // create the buffer object
26 VkBufferCreateInfo bufInfo;
27 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
28 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
29 bufInfo.flags = 0;
30 bufInfo.size = desc.fSizeInBytes;
31 switch (desc.fType) {
jvanvertha584de92016-06-30 09:10:52 -070032 case kVertex_Type:
33 bufInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
34 break;
35 case kIndex_Type:
36 bufInfo.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
37 break;
38 case kUniform_Type:
39 bufInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
40 break;
41 case kCopyRead_Type:
42 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
43 break;
44 case kCopyWrite_Type:
45 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT;
46 break;
Greg Daniel164a9f02016-02-22 09:56:40 -050047 }
jvanvertha584de92016-06-30 09:10:52 -070048 if (!desc.fDynamic) {
49 bufInfo.usage |= VK_BUFFER_USAGE_TRANSFER_DST_BIT;
50 }
51
Greg Daniel164a9f02016-02-22 09:56:40 -050052 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
53 bufInfo.queueFamilyIndexCount = 0;
54 bufInfo.pQueueFamilyIndices = nullptr;
55
56 VkResult err;
57 err = VK_CALL(gpu, CreateBuffer(gpu->device(), &bufInfo, nullptr, &buffer));
58 if (err) {
59 return nullptr;
60 }
61
Greg Daniel164a9f02016-02-22 09:56:40 -050062 if (!GrVkMemory::AllocAndBindBufferMemory(gpu,
63 buffer,
jvanverth6b6ffc42016-06-13 14:28:07 -070064 desc.fType,
jvanvertha584de92016-06-30 09:10:52 -070065 desc.fDynamic,
Greg Daniel164a9f02016-02-22 09:56:40 -050066 &alloc)) {
jvanverth6b6ffc42016-06-13 14:28:07 -070067 return nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -050068 }
69
jvanverth6b6ffc42016-06-13 14:28:07 -070070 const GrVkBuffer::Resource* resource = new GrVkBuffer::Resource(buffer, alloc, desc.fType);
Greg Daniel164a9f02016-02-22 09:56:40 -050071 if (!resource) {
72 VK_CALL(gpu, DestroyBuffer(gpu->device(), buffer, nullptr));
jvanverth6b6ffc42016-06-13 14:28:07 -070073 GrVkMemory::FreeBufferMemory(gpu, desc.fType, alloc);
Greg Daniel164a9f02016-02-22 09:56:40 -050074 return nullptr;
75 }
76
77 return resource;
78}
79
Greg Daniel164a9f02016-02-22 09:56:40 -050080void GrVkBuffer::addMemoryBarrier(const GrVkGpu* gpu,
81 VkAccessFlags srcAccessMask,
82 VkAccessFlags dstAccesMask,
83 VkPipelineStageFlags srcStageMask,
84 VkPipelineStageFlags dstStageMask,
85 bool byRegion) const {
86 VkBufferMemoryBarrier bufferMemoryBarrier = {
87 VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER, // sType
88 NULL, // pNext
89 srcAccessMask, // srcAccessMask
90 dstAccesMask, // dstAccessMask
91 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
92 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
93 this->buffer(), // buffer
94 0, // offset
95 fDesc.fSizeInBytes, // size
96 };
97
98 // TODO: restrict to area of buffer we're interested in
99 gpu->addBufferMemoryBarrier(srcStageMask, dstStageMask, byRegion, &bufferMemoryBarrier);
100}
101
102void GrVkBuffer::Resource::freeGPUData(const GrVkGpu* gpu) const {
103 SkASSERT(fBuffer);
jvanverth1e305ba2016-06-01 09:39:15 -0700104 SkASSERT(fAlloc.fMemory);
Greg Daniel164a9f02016-02-22 09:56:40 -0500105 VK_CALL(gpu, DestroyBuffer(gpu->device(), fBuffer, nullptr));
jvanverth6b6ffc42016-06-13 14:28:07 -0700106 GrVkMemory::FreeBufferMemory(gpu, fType, fAlloc);
Greg Daniel164a9f02016-02-22 09:56:40 -0500107}
108
109void GrVkBuffer::vkRelease(const GrVkGpu* gpu) {
110 VALIDATE();
111 fResource->unref(gpu);
112 fResource = nullptr;
113 fMapPtr = nullptr;
114 VALIDATE();
115}
116
117void GrVkBuffer::vkAbandon() {
118 fResource->unrefAndAbandon();
jvanverthaf236b52016-05-20 06:01:06 -0700119 fResource = nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -0500120 fMapPtr = nullptr;
121 VALIDATE();
122}
123
124void* GrVkBuffer::vkMap(const GrVkGpu* gpu) {
125 VALIDATE();
126 SkASSERT(!this->vkIsMapped());
jvanvertha584de92016-06-30 09:10:52 -0700127 if (!fDesc.fDynamic) {
128 return nullptr;
129 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500130
jvanverthdbb429a2016-03-16 06:47:39 -0700131 if (!fResource->unique()) {
132 // in use by the command buffer, so we need to create a new one
133 fResource->unref(gpu);
134 fResource = Create(gpu, fDesc);
135 }
jvanverth910114a2016-03-08 12:09:27 -0800136
jvanverth1e305ba2016-06-01 09:39:15 -0700137 const GrVkAlloc& alloc = this->alloc();
138 VkResult err = VK_CALL(gpu, MapMemory(gpu->device(), alloc.fMemory, alloc.fOffset,
139 VK_WHOLE_SIZE, 0, &fMapPtr));
Greg Daniel164a9f02016-02-22 09:56:40 -0500140 if (err) {
141 fMapPtr = nullptr;
142 }
143
144 VALIDATE();
145 return fMapPtr;
146}
147
148void GrVkBuffer::vkUnmap(const GrVkGpu* gpu) {
149 VALIDATE();
150 SkASSERT(this->vkIsMapped());
jvanvertha584de92016-06-30 09:10:52 -0700151 SkASSERT(fDesc.fDynamic);
Greg Daniel164a9f02016-02-22 09:56:40 -0500152
jvanverth1e305ba2016-06-01 09:39:15 -0700153 VK_CALL(gpu, UnmapMemory(gpu->device(), this->alloc().fMemory));
Greg Daniel164a9f02016-02-22 09:56:40 -0500154
155 fMapPtr = nullptr;
156}
157
158bool GrVkBuffer::vkIsMapped() const {
159 VALIDATE();
160 return SkToBool(fMapPtr);
161}
162
jvanvertha584de92016-06-30 09:10:52 -0700163bool GrVkBuffer::vkUpdateData(GrVkGpu* gpu, const void* src, size_t srcSizeInBytes,
egdaniel7cbffda2016-04-08 13:27:53 -0700164 bool* createdNewBuffer) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500165 SkASSERT(!this->vkIsMapped());
166 VALIDATE();
167 if (srcSizeInBytes > fDesc.fSizeInBytes) {
168 return false;
169 }
170
jvanvertha584de92016-06-30 09:10:52 -0700171 if (!fDesc.fDynamic) {
172 return gpu->updateBuffer(this, src, srcSizeInBytes);
173 }
174
jvanverth910114a2016-03-08 12:09:27 -0800175 if (!fResource->unique()) {
176 // in use by the command buffer, so we need to create a new one
177 fResource->unref(gpu);
178 fResource = Create(gpu, fDesc);
egdaniel7cbffda2016-04-08 13:27:53 -0700179 if (createdNewBuffer) {
180 *createdNewBuffer = true;
181 }
jvanverth910114a2016-03-08 12:09:27 -0800182 }
183
Greg Daniel164a9f02016-02-22 09:56:40 -0500184 void* mapPtr;
jvanverth1e305ba2016-06-01 09:39:15 -0700185 const GrVkAlloc& alloc = this->alloc();
186 VkResult err = VK_CALL(gpu, MapMemory(gpu->device(), alloc.fMemory, alloc.fOffset,
187 srcSizeInBytes, 0, &mapPtr));
Greg Daniel164a9f02016-02-22 09:56:40 -0500188
189 if (VK_SUCCESS != err) {
190 return false;
191 }
192
193 memcpy(mapPtr, src, srcSizeInBytes);
194
jvanverth1e305ba2016-06-01 09:39:15 -0700195 VK_CALL(gpu, UnmapMemory(gpu->device(), alloc.fMemory));
Greg Daniel164a9f02016-02-22 09:56:40 -0500196
197 return true;
198}
199
200void GrVkBuffer::validate() const {
201 SkASSERT(!fResource || kVertex_Type == fDesc.fType || kIndex_Type == fDesc.fType
202 || kCopyRead_Type == fDesc.fType || kCopyWrite_Type == fDesc.fType
203 || kUniform_Type == fDesc.fType);
204}