| 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 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 VALIDATE(); | 125 VALIDATE(); |
| 126 SkASSERT(!this->vkIsMapped()); | 126 SkASSERT(!this->vkIsMapped()); |
| 127 if (!fResource->unique()) { | 127 if (!fResource->unique()) { |
| 128 // in use by the command buffer, so we need to create a new one | 128 // in use by the command buffer, so we need to create a new one |
| 129 fResource->unref(gpu); | 129 fResource->unref(gpu); |
| 130 fResource = Create(gpu, fDesc); | 130 fResource = Create(gpu, fDesc); |
| 131 } | 131 } |
| 132 | 132 |
| 133 if (fDesc.fDynamic) { | 133 if (fDesc.fDynamic) { |
| 134 const GrVkAlloc& alloc = this->alloc(); | 134 const GrVkAlloc& alloc = this->alloc(); |
| 135 VkResult err = VK_CALL(gpu, MapMemory(gpu->device(), alloc.fMemory, allo
c.fOffset, | 135 VkResult err = VK_CALL(gpu, MapMemory(gpu->device(), alloc.fMemory, |
| 136 VK_WHOLE_SIZE, 0, &fMapPtr)); | 136 alloc.fOffset + fOffset, |
| 137 fDesc.fSizeInBytes, 0, &fMapPtr)); |
| 137 if (err) { | 138 if (err) { |
| 138 fMapPtr = nullptr; | 139 fMapPtr = nullptr; |
| 139 } | 140 } |
| 140 } else { | 141 } else { |
| 141 fMapPtr = new unsigned char[this->size()]; | 142 fMapPtr = new unsigned char[this->size()]; |
| 142 } | 143 } |
| 143 | 144 |
| 144 VALIDATE(); | 145 VALIDATE(); |
| 145 return fMapPtr; | 146 return fMapPtr; |
| 146 } | 147 } |
| 147 | 148 |
| 148 void GrVkBuffer::vkUnmap(GrVkGpu* gpu) { | 149 void GrVkBuffer::vkUnmap(GrVkGpu* gpu) { |
| 149 VALIDATE(); | 150 VALIDATE(); |
| 150 SkASSERT(this->vkIsMapped()); | 151 SkASSERT(this->vkIsMapped()); |
| 151 | 152 |
| 152 if (fDesc.fDynamic) { | 153 if (fDesc.fDynamic) { |
| 153 VK_CALL(gpu, UnmapMemory(gpu->device(), this->alloc().fMemory)); | 154 VK_CALL(gpu, UnmapMemory(gpu->device(), this->alloc().fMemory)); |
| 154 } else { | 155 } else { |
| 155 gpu->updateBuffer(this, fMapPtr, this->size()); | 156 gpu->updateBuffer(this, fMapPtr, this->offset(), this->size()); |
| 156 delete [] (unsigned char*)fMapPtr; | 157 delete [] (unsigned char*)fMapPtr; |
| 157 } | 158 } |
| 158 | 159 |
| 159 fMapPtr = nullptr; | 160 fMapPtr = nullptr; |
| 160 } | 161 } |
| 161 | 162 |
| 162 bool GrVkBuffer::vkIsMapped() const { | 163 bool GrVkBuffer::vkIsMapped() const { |
| 163 VALIDATE(); | 164 VALIDATE(); |
| 164 return SkToBool(fMapPtr); | 165 return SkToBool(fMapPtr); |
| 165 } | 166 } |
| 166 | 167 |
| 167 bool GrVkBuffer::vkUpdateData(GrVkGpu* gpu, const void* src, size_t srcSizeInByt
es, | 168 bool GrVkBuffer::vkUpdateData(GrVkGpu* gpu, const void* src, size_t srcSizeInByt
es, |
| 168 bool* createdNewBuffer) { | 169 bool* createdNewBuffer) { |
| 169 SkASSERT(!this->vkIsMapped()); | 170 SkASSERT(!this->vkIsMapped()); |
| 170 VALIDATE(); | 171 VALIDATE(); |
| 171 if (srcSizeInBytes > fDesc.fSizeInBytes) { | 172 if (srcSizeInBytes > fDesc.fSizeInBytes) { |
| 172 return false; | 173 return false; |
| 173 } | 174 } |
| 174 | 175 |
| 176 // TODO: update data based on buffer offset |
| 175 if (!fDesc.fDynamic) { | 177 if (!fDesc.fDynamic) { |
| 176 return gpu->updateBuffer(this, src, srcSizeInBytes); | 178 return gpu->updateBuffer(this, src, fOffset, srcSizeInBytes); |
| 177 } | 179 } |
| 178 | 180 |
| 179 if (!fResource->unique()) { | 181 if (!fResource->unique()) { |
| 180 // in use by the command buffer, so we need to create a new one | 182 // in use by the command buffer, so we need to create a new one |
| 181 fResource->unref(gpu); | 183 fResource->unref(gpu); |
| 182 fResource = Create(gpu, fDesc); | 184 fResource = Create(gpu, fDesc); |
| 183 if (createdNewBuffer) { | 185 if (createdNewBuffer) { |
| 184 *createdNewBuffer = true; | 186 *createdNewBuffer = true; |
| 185 } | 187 } |
| 186 } | 188 } |
| 187 | 189 |
| 188 void* mapPtr; | 190 void* mapPtr; |
| 189 const GrVkAlloc& alloc = this->alloc(); | 191 const GrVkAlloc& alloc = this->alloc(); |
| 190 VkResult err = VK_CALL(gpu, MapMemory(gpu->device(), alloc.fMemory, alloc.fO
ffset, | 192 VkResult err = VK_CALL(gpu, MapMemory(gpu->device(), alloc.fMemory, |
| 193 alloc.fOffset + fOffset, |
| 191 srcSizeInBytes, 0, &mapPtr)); | 194 srcSizeInBytes, 0, &mapPtr)); |
| 192 | 195 |
| 193 if (VK_SUCCESS != err) { | 196 if (VK_SUCCESS != err) { |
| 194 return false; | 197 return false; |
| 195 } | 198 } |
| 196 | 199 |
| 197 memcpy(mapPtr, src, srcSizeInBytes); | 200 memcpy(mapPtr, src, srcSizeInBytes); |
| 198 | 201 |
| 199 VK_CALL(gpu, UnmapMemory(gpu->device(), alloc.fMemory)); | 202 VK_CALL(gpu, UnmapMemory(gpu->device(), alloc.fMemory)); |
| 200 | 203 |
| 201 return true; | 204 return true; |
| 202 } | 205 } |
| 203 | 206 |
| 204 void GrVkBuffer::validate() const { | 207 void GrVkBuffer::validate() const { |
| 205 SkASSERT(!fResource || kVertex_Type == fDesc.fType || kIndex_Type == fDesc.f
Type | 208 SkASSERT(!fResource || kVertex_Type == fDesc.fType || kIndex_Type == fDesc.f
Type |
| 206 || kCopyRead_Type == fDesc.fType || kCopyWrite_Type == fDesc.fType | 209 || kCopyRead_Type == fDesc.fType || kCopyWrite_Type == fDesc.fType |
| 207 || kUniform_Type == fDesc.fType); | 210 || kUniform_Type == fDesc.fType); |
| 208 } | 211 } |
| OLD | NEW |