| 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 VkAccessFlags buffer_type_to_access_flags(GrVkBuffer::Type type) { |
| 125 switch (type) { |
| 126 case GrVkBuffer::kIndex_Type: |
| 127 return VK_ACCESS_INDEX_READ_BIT; |
| 128 case GrVkBuffer::kVertex_Type: |
| 129 return VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT; |
| 130 default: |
| 131 // This helper is only called for static buffers so we should only e
ver see index or |
| 132 // vertex buffers types |
| 133 SkASSERT(false); |
| 134 return 0; |
| 135 } |
| 136 } |
| 137 |
| 138 void GrVkBuffer::internalMap(GrVkGpu* gpu, size_t size, bool* createdNewBuffer)
{ |
| 125 VALIDATE(); | 139 VALIDATE(); |
| 126 SkASSERT(!this->vkIsMapped()); | 140 SkASSERT(!this->vkIsMapped()); |
| 141 |
| 127 if (!fResource->unique()) { | 142 if (!fResource->unique()) { |
| 128 // in use by the command buffer, so we need to create a new one | 143 if (fDesc.fDynamic) { |
| 129 fResource->unref(gpu); | 144 // in use by the command buffer, so we need to create a new one |
| 130 fResource = Create(gpu, fDesc); | 145 fResource->recycle(gpu); |
| 146 fResource = this->createResource(gpu, fDesc); |
| 147 if (createdNewBuffer) { |
| 148 *createdNewBuffer = true; |
| 149 } |
| 150 } else { |
| 151 SkASSERT(fMapPtr); |
| 152 this->addMemoryBarrier(gpu, |
| 153 buffer_type_to_access_flags(fDesc.fType), |
| 154 VK_ACCESS_TRANSFER_WRITE_BIT, |
| 155 VK_PIPELINE_STAGE_VERTEX_INPUT_BIT, |
| 156 VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 157 false); |
| 158 } |
| 131 } | 159 } |
| 132 | 160 |
| 133 if (fDesc.fDynamic) { | 161 if (fDesc.fDynamic) { |
| 134 const GrVkAlloc& alloc = this->alloc(); | 162 const GrVkAlloc& alloc = this->alloc(); |
| 135 VkResult err = VK_CALL(gpu, MapMemory(gpu->device(), alloc.fMemory, | 163 VkResult err = VK_CALL(gpu, MapMemory(gpu->device(), alloc.fMemory, |
| 136 alloc.fOffset + fOffset, | 164 alloc.fOffset + fOffset, |
| 137 fDesc.fSizeInBytes, 0, &fMapPtr)); | 165 size, 0, &fMapPtr)); |
| 138 if (err) { | 166 if (err) { |
| 139 fMapPtr = nullptr; | 167 fMapPtr = nullptr; |
| 140 } | 168 } |
| 141 } else { | 169 } else { |
| 142 fMapPtr = new unsigned char[this->size()]; | 170 if (!fMapPtr) { |
| 171 fMapPtr = new unsigned char[this->size()]; |
| 172 } |
| 143 } | 173 } |
| 144 | 174 |
| 145 VALIDATE(); | 175 VALIDATE(); |
| 146 return fMapPtr; | |
| 147 } | 176 } |
| 148 | 177 |
| 149 void GrVkBuffer::vkUnmap(GrVkGpu* gpu) { | 178 void GrVkBuffer::internalUnmap(GrVkGpu* gpu, size_t size) { |
| 150 VALIDATE(); | 179 VALIDATE(); |
| 151 SkASSERT(this->vkIsMapped()); | 180 SkASSERT(this->vkIsMapped()); |
| 152 | 181 |
| 153 if (fDesc.fDynamic) { | 182 if (fDesc.fDynamic) { |
| 154 VK_CALL(gpu, UnmapMemory(gpu->device(), this->alloc().fMemory)); | 183 VK_CALL(gpu, UnmapMemory(gpu->device(), this->alloc().fMemory)); |
| 184 fMapPtr = nullptr; |
| 155 } else { | 185 } else { |
| 156 gpu->updateBuffer(this, fMapPtr, this->offset(), this->size()); | 186 gpu->updateBuffer(this, fMapPtr, this->offset(), size); |
| 157 delete [] (unsigned char*)fMapPtr; | 187 this->addMemoryBarrier(gpu, |
| 188 VK_ACCESS_TRANSFER_WRITE_BIT, |
| 189 buffer_type_to_access_flags(fDesc.fType), |
| 190 VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 191 VK_PIPELINE_STAGE_VERTEX_INPUT_BIT, |
| 192 false); |
| 158 } | 193 } |
| 159 | |
| 160 fMapPtr = nullptr; | |
| 161 } | 194 } |
| 162 | 195 |
| 163 bool GrVkBuffer::vkIsMapped() const { | 196 bool GrVkBuffer::vkIsMapped() const { |
| 164 VALIDATE(); | 197 VALIDATE(); |
| 165 return SkToBool(fMapPtr); | 198 return SkToBool(fMapPtr); |
| 166 } | 199 } |
| 167 | 200 |
| 168 bool GrVkBuffer::vkUpdateData(GrVkGpu* gpu, const void* src, size_t srcSizeInByt
es, | 201 bool GrVkBuffer::vkUpdateData(GrVkGpu* gpu, const void* src, size_t srcSizeInByt
es, |
| 169 bool* createdNewBuffer) { | 202 bool* createdNewBuffer) { |
| 170 SkASSERT(!this->vkIsMapped()); | |
| 171 VALIDATE(); | |
| 172 if (srcSizeInBytes > fDesc.fSizeInBytes) { | 203 if (srcSizeInBytes > fDesc.fSizeInBytes) { |
| 173 return false; | 204 return false; |
| 174 } | 205 } |
| 175 | 206 |
| 176 // TODO: update data based on buffer offset | 207 this->internalMap(gpu, srcSizeInBytes, createdNewBuffer); |
| 177 if (!fDesc.fDynamic) { | 208 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; | 209 return false; |
| 198 } | 210 } |
| 199 | 211 |
| 200 memcpy(mapPtr, src, srcSizeInBytes); | 212 memcpy(fMapPtr, src, srcSizeInBytes); |
| 201 | 213 |
| 202 VK_CALL(gpu, UnmapMemory(gpu->device(), alloc.fMemory)); | 214 this->internalUnmap(gpu, srcSizeInBytes); |
| 203 | 215 |
| 204 return true; | 216 return true; |
| 205 } | 217 } |
| 206 | 218 |
| 207 void GrVkBuffer::validate() const { | 219 void GrVkBuffer::validate() const { |
| 208 SkASSERT(!fResource || kVertex_Type == fDesc.fType || kIndex_Type == fDesc.f
Type | 220 SkASSERT(!fResource || kVertex_Type == fDesc.fType || kIndex_Type == fDesc.f
Type |
| 209 || kCopyRead_Type == fDesc.fType || kCopyWrite_Type == fDesc.fType | 221 || kCopyRead_Type == fDesc.fType || kCopyWrite_Type == fDesc.fType |
| 210 || kUniform_Type == fDesc.fType); | 222 || kUniform_Type == fDesc.fType); |
| 211 } | 223 } |
| OLD | NEW |