| 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); |
| 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 } |
| 131 } | 164 } |
| 132 | 165 |
| 133 if (fDesc.fDynamic) { | 166 if (fDesc.fDynamic) { |
| 134 const GrVkAlloc& alloc = this->alloc(); | 167 const GrVkAlloc& alloc = this->alloc(); |
| 135 VkResult err = VK_CALL(gpu, MapMemory(gpu->device(), alloc.fMemory, | 168 VkResult err = VK_CALL(gpu, MapMemory(gpu->device(), alloc.fMemory, |
| 136 alloc.fOffset + fOffset, | 169 alloc.fOffset + fOffset, |
| 137 fDesc.fSizeInBytes, 0, &fMapPtr)); | 170 size, 0, &fMapPtr)); |
| 138 if (err) { | 171 if (err) { |
| 139 fMapPtr = nullptr; | 172 fMapPtr = nullptr; |
| 140 } | 173 } |
| 141 } else { | 174 } else { |
| 142 fMapPtr = new unsigned char[this->size()]; | 175 if (!fMapPtr) { |
| 176 fMapPtr = new unsigned char[this->size()]; |
| 177 } |
| 143 } | 178 } |
| 144 | 179 |
| 145 VALIDATE(); | 180 VALIDATE(); |
| 146 return fMapPtr; | |
| 147 } | 181 } |
| 148 | 182 |
| 149 void GrVkBuffer::vkUnmap(GrVkGpu* gpu) { | 183 void GrVkBuffer::vkUnmap(GrVkGpu* gpu) { |
| 184 this->innerUnmap(gpu, this->size()); |
| 185 } |
| 186 |
| 187 void GrVkBuffer::innerUnmap(GrVkGpu* gpu, size_t size) { |
| 150 VALIDATE(); | 188 VALIDATE(); |
| 151 SkASSERT(this->vkIsMapped()); | 189 SkASSERT(this->vkIsMapped()); |
| 152 | 190 |
| 153 if (fDesc.fDynamic) { | 191 if (fDesc.fDynamic) { |
| 154 VK_CALL(gpu, UnmapMemory(gpu->device(), this->alloc().fMemory)); | 192 VK_CALL(gpu, UnmapMemory(gpu->device(), this->alloc().fMemory)); |
| 193 fMapPtr = nullptr; |
| 155 } else { | 194 } else { |
| 156 gpu->updateBuffer(this, fMapPtr, this->offset(), this->size()); | 195 gpu->updateBuffer(this, fMapPtr, this->offset(), size); |
| 157 delete [] (unsigned char*)fMapPtr; | 196 this->addMemoryBarrier(gpu, |
| 197 VK_ACCESS_TRANSFER_WRITE_BIT, |
| 198 buffer_type_to_access_flags(fDesc.fType), |
| 199 VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 200 VK_PIPELINE_STAGE_VERTEX_INPUT_BIT, |
| 201 false); |
| 158 } | 202 } |
| 159 | |
| 160 fMapPtr = nullptr; | |
| 161 } | 203 } |
| 162 | 204 |
| 163 bool GrVkBuffer::vkIsMapped() const { | 205 bool GrVkBuffer::vkIsMapped() const { |
| 164 VALIDATE(); | 206 VALIDATE(); |
| 165 return SkToBool(fMapPtr); | 207 return SkToBool(fMapPtr); |
| 166 } | 208 } |
| 167 | 209 |
| 168 bool GrVkBuffer::vkUpdateData(GrVkGpu* gpu, const void* src, size_t srcSizeInByt
es, | 210 bool GrVkBuffer::vkUpdateData(GrVkGpu* gpu, const void* src, size_t srcSizeInByt
es, |
| 169 bool* createdNewBuffer) { | 211 bool* createdNewBuffer) { |
| 170 SkASSERT(!this->vkIsMapped()); | |
| 171 VALIDATE(); | |
| 172 if (srcSizeInBytes > fDesc.fSizeInBytes) { | 212 if (srcSizeInBytes > fDesc.fSizeInBytes) { |
| 173 return false; | 213 return false; |
| 174 } | 214 } |
| 175 | 215 |
| 176 // TODO: update data based on buffer offset | 216 this->innerMap(gpu, srcSizeInBytes, createdNewBuffer); |
| 177 if (!fDesc.fDynamic) { | 217 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; | 218 return false; |
| 198 } | 219 } |
| 199 | 220 |
| 200 memcpy(mapPtr, src, srcSizeInBytes); | 221 memcpy(fMapPtr, src, srcSizeInBytes); |
| 201 | 222 |
| 202 VK_CALL(gpu, UnmapMemory(gpu->device(), alloc.fMemory)); | 223 this->innerUnmap(gpu, srcSizeInBytes); |
| 203 | 224 |
| 204 return true; | 225 return true; |
| 205 } | 226 } |
| 206 | 227 |
| 207 void GrVkBuffer::validate() const { | 228 void GrVkBuffer::validate() const { |
| 208 SkASSERT(!fResource || kVertex_Type == fDesc.fType || kIndex_Type == fDesc.f
Type | 229 SkASSERT(!fResource || kVertex_Type == fDesc.fType || kIndex_Type == fDesc.f
Type |
| 209 || kCopyRead_Type == fDesc.fType || kCopyWrite_Type == fDesc.fType | 230 || kCopyRead_Type == fDesc.fType || kCopyWrite_Type == fDesc.fType |
| 210 || kUniform_Type == fDesc.fType); | 231 || kUniform_Type == fDesc.fType); |
| 211 } | 232 } |
| OLD | NEW |