OLD | NEW |
---|---|
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 "GrVkBuffer.h" | 8 #include "GrVkBuffer.h" |
9 #include "GrVkGpu.h" | 9 #include "GrVkGpu.h" |
10 #include "GrVkMemory.h" | 10 #include "GrVkMemory.h" |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
114 VALIDATE(); | 114 VALIDATE(); |
115 } | 115 } |
116 | 116 |
117 void GrVkBuffer::vkAbandon() { | 117 void GrVkBuffer::vkAbandon() { |
118 fResource->unrefAndAbandon(); | 118 fResource->unrefAndAbandon(); |
119 fResource = nullptr; | 119 fResource = nullptr; |
120 fMapPtr = nullptr; | 120 fMapPtr = nullptr; |
121 VALIDATE(); | 121 VALIDATE(); |
122 } | 122 } |
123 | 123 |
124 void* GrVkBuffer::vkMap(const GrVkGpu* gpu) { | 124 void* GrVkBuffer::vkMap(GrVkGpu* gpu) { |
125 this->innerMap(gpu, fDesc.fSizeInBytes); | |
126 return fMapPtr; | |
127 } | |
128 | |
129 VkAccessFlags buffer_type_to_access_flags(GrVkBuffer::Type type) { | |
130 switch (type) { | |
131 case GrVkBuffer::kIndex_Type: | |
132 return VK_ACCESS_INDEX_READ_BIT; | |
133 case GrVkBuffer::kVertex_Type: | |
134 return VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT; | |
135 default: | |
136 // This helper is only called for static buffers so we should only e ver see index or | |
137 // vertex buffers types | |
138 SkASSERT(false); | |
139 return 0; | |
140 } | |
141 } | |
142 | |
143 void GrVkBuffer::innerMap(GrVkGpu* gpu, size_t size, bool* createdNewBuffer) { | |
125 VALIDATE(); | 144 VALIDATE(); |
126 SkASSERT(!this->vkIsMapped()); | 145 SkASSERT(!this->vkIsMapped()); |
146 | |
127 if (!fResource->unique()) { | 147 if (!fResource->unique()) { |
128 // in use by the command buffer, so we need to create a new one | 148 if (fDesc.fDynamic) { |
129 fResource->unref(gpu); | 149 // in use by the command buffer, so we need to create a new one |
130 fResource = Create(gpu, fDesc); | 150 fResource->recycle(gpu); |
151 fResource = this->createResource(gpu, fDesc); | |
152 if (createdNewBuffer) { | |
153 *createdNewBuffer = true; | |
154 } | |
155 } else { | |
156 SkASSERT(fMapPtr); | |
egdaniel
2016/09/16 20:00:28
we now never delete the resources for static buffe
| |
157 this->addMemoryBarrier(gpu, | |
158 buffer_type_to_access_flags(fDesc.fType), | |
159 VK_ACCESS_TRANSFER_WRITE_BIT, | |
160 VK_PIPELINE_STAGE_VERTEX_INPUT_BIT, | |
161 VK_PIPELINE_STAGE_TRANSFER_BIT, | |
162 false); | |
163 delete [] (unsigned char*)fMapPtr; | |
egdaniel
2016/09/16 20:43:12
We actually should not need to ever delete and rea
| |
164 fMapPtr = nullptr; | |
165 } | |
131 } | 166 } |
132 | 167 |
133 if (fDesc.fDynamic) { | 168 if (fDesc.fDynamic) { |
134 const GrVkAlloc& alloc = this->alloc(); | 169 const GrVkAlloc& alloc = this->alloc(); |
135 VkResult err = VK_CALL(gpu, MapMemory(gpu->device(), alloc.fMemory, | 170 VkResult err = VK_CALL(gpu, MapMemory(gpu->device(), alloc.fMemory, |
136 alloc.fOffset + fOffset, | 171 alloc.fOffset + fOffset, |
137 fDesc.fSizeInBytes, 0, &fMapPtr)); | 172 size, 0, &fMapPtr)); |
138 if (err) { | 173 if (err) { |
139 fMapPtr = nullptr; | 174 fMapPtr = nullptr; |
140 } | 175 } |
141 } else { | 176 } else { |
142 fMapPtr = new unsigned char[this->size()]; | 177 if (!fMapPtr) { |
178 fMapPtr = new unsigned char[this->size()]; | |
179 } | |
143 } | 180 } |
144 | 181 |
145 VALIDATE(); | 182 VALIDATE(); |
146 return fMapPtr; | |
147 } | 183 } |
148 | 184 |
149 void GrVkBuffer::vkUnmap(GrVkGpu* gpu) { | 185 void GrVkBuffer::vkUnmap(GrVkGpu* gpu) { |
186 this->innerUnmap(gpu, this->size()); | |
187 } | |
188 | |
189 void GrVkBuffer::innerUnmap(GrVkGpu* gpu, size_t size) { | |
150 VALIDATE(); | 190 VALIDATE(); |
151 SkASSERT(this->vkIsMapped()); | 191 SkASSERT(this->vkIsMapped()); |
152 | 192 |
153 if (fDesc.fDynamic) { | 193 if (fDesc.fDynamic) { |
154 VK_CALL(gpu, UnmapMemory(gpu->device(), this->alloc().fMemory)); | 194 VK_CALL(gpu, UnmapMemory(gpu->device(), this->alloc().fMemory)); |
195 fMapPtr = nullptr; | |
155 } else { | 196 } else { |
156 gpu->updateBuffer(this, fMapPtr, this->offset(), this->size()); | 197 gpu->updateBuffer(this, fMapPtr, this->offset(), size); |
157 delete [] (unsigned char*)fMapPtr; | 198 this->addMemoryBarrier(gpu, |
199 VK_ACCESS_TRANSFER_WRITE_BIT, | |
200 buffer_type_to_access_flags(fDesc.fType), | |
201 VK_PIPELINE_STAGE_TRANSFER_BIT, | |
202 VK_PIPELINE_STAGE_VERTEX_INPUT_BIT, | |
203 false); | |
158 } | 204 } |
159 | |
160 fMapPtr = nullptr; | |
161 } | 205 } |
162 | 206 |
163 bool GrVkBuffer::vkIsMapped() const { | 207 bool GrVkBuffer::vkIsMapped() const { |
164 VALIDATE(); | 208 VALIDATE(); |
165 return SkToBool(fMapPtr); | 209 return SkToBool(fMapPtr); |
166 } | 210 } |
167 | 211 |
168 bool GrVkBuffer::vkUpdateData(GrVkGpu* gpu, const void* src, size_t srcSizeInByt es, | 212 bool GrVkBuffer::vkUpdateData(GrVkGpu* gpu, const void* src, size_t srcSizeInByt es, |
169 bool* createdNewBuffer) { | 213 bool* createdNewBuffer) { |
170 SkASSERT(!this->vkIsMapped()); | |
171 VALIDATE(); | |
172 if (srcSizeInBytes > fDesc.fSizeInBytes) { | 214 if (srcSizeInBytes > fDesc.fSizeInBytes) { |
173 return false; | 215 return false; |
174 } | 216 } |
175 | 217 |
176 // TODO: update data based on buffer offset | 218 this->innerMap(gpu, srcSizeInBytes, createdNewBuffer); |
177 if (!fDesc.fDynamic) { | 219 if (!fMapPtr) { |
178 return gpu->updateBuffer(this, src, fOffset, srcSizeInBytes); | |
179 } | |
180 | |
181 if (!fResource->unique()) { | |
182 // in use by the command buffer, so we need to create a new one | |
183 fResource->recycle(gpu); | |
184 fResource = this->createResource(gpu, fDesc); | |
185 if (createdNewBuffer) { | |
186 *createdNewBuffer = true; | |
187 } | |
188 } | |
189 | |
190 void* mapPtr; | |
191 const GrVkAlloc& alloc = this->alloc(); | |
192 VkResult err = VK_CALL(gpu, MapMemory(gpu->device(), alloc.fMemory, | |
193 alloc.fOffset + fOffset, | |
194 srcSizeInBytes, 0, &mapPtr)); | |
195 | |
196 if (VK_SUCCESS != err) { | |
197 return false; | 220 return false; |
198 } | 221 } |
199 | 222 |
200 memcpy(mapPtr, src, srcSizeInBytes); | 223 memcpy(fMapPtr, src, srcSizeInBytes); |
201 | 224 |
202 VK_CALL(gpu, UnmapMemory(gpu->device(), alloc.fMemory)); | 225 this->innerUnmap(gpu, srcSizeInBytes); |
203 | 226 |
204 return true; | 227 return true; |
205 } | 228 } |
206 | 229 |
207 void GrVkBuffer::validate() const { | 230 void GrVkBuffer::validate() const { |
208 SkASSERT(!fResource || kVertex_Type == fDesc.fType || kIndex_Type == fDesc.f Type | 231 SkASSERT(!fResource || kVertex_Type == fDesc.fType || kIndex_Type == fDesc.f Type |
209 || kCopyRead_Type == fDesc.fType || kCopyWrite_Type == fDesc.fType | 232 || kCopyRead_Type == fDesc.fType || kCopyWrite_Type == fDesc.fType |
210 || kUniform_Type == fDesc.fType); | 233 || kUniform_Type == fDesc.fType); |
211 } | 234 } |
OLD | NEW |