| 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" |
| 11 #include "GrVkUtil.h" | 11 #include "GrVkUtil.h" |
| 12 | 12 |
| 13 #define VK_CALL(GPU, X) GR_VK_CALL(GPU->vkInterface(), X) | 13 #define VK_CALL(GPU, X) GR_VK_CALL(GPU->vkInterface(), X) |
| 14 | 14 |
| 15 #ifdef SK_DEBUG | 15 #ifdef SK_DEBUG |
| 16 #define VALIDATE() this->validate() | 16 #define VALIDATE() this->validate() |
| 17 #else | 17 #else |
| 18 #define VALIDATE() do {} while(false) | 18 #define VALIDATE() do {} while(false) |
| 19 #endif | 19 #endif |
| 20 | 20 |
| 21 const GrVkBuffer::Resource* GrVkBuffer::Create(const GrVkGpu* gpu, const Desc& d
esc) { | 21 const GrVkBuffer::Resource* GrVkBuffer::Create(const GrVkGpu* gpu, const Desc& d
esc) { |
| 22 VkBuffer buffer; | 22 VkBuffer buffer; |
| 23 VkDeviceMemory alloc; | 23 VkDeviceMemory alloc; |
| 24 VkDeviceSize offset; |
| 24 | 25 |
| 25 // create the buffer object | 26 // create the buffer object |
| 26 VkBufferCreateInfo bufInfo; | 27 VkBufferCreateInfo bufInfo; |
| 27 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo)); | 28 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo)); |
| 28 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; | 29 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
| 29 bufInfo.flags = 0; | 30 bufInfo.flags = 0; |
| 30 bufInfo.size = desc.fSizeInBytes; | 31 bufInfo.size = desc.fSizeInBytes; |
| 31 switch (desc.fType) { | 32 switch (desc.fType) { |
| 32 case kVertex_Type: | 33 case kVertex_Type: |
| 33 bufInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; | 34 bufInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 56 return nullptr; | 57 return nullptr; |
| 57 } | 58 } |
| 58 | 59 |
| 59 VkMemoryPropertyFlags requiredMemProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
| | 60 VkMemoryPropertyFlags requiredMemProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
| |
| 60 VK_MEMORY_PROPERTY_HOST_COHERENT_BI
T | | 61 VK_MEMORY_PROPERTY_HOST_COHERENT_BI
T | |
| 61 VK_MEMORY_PROPERTY_HOST_CACHED_BIT; | 62 VK_MEMORY_PROPERTY_HOST_CACHED_BIT; |
| 62 | 63 |
| 63 if (!GrVkMemory::AllocAndBindBufferMemory(gpu, | 64 if (!GrVkMemory::AllocAndBindBufferMemory(gpu, |
| 64 buffer, | 65 buffer, |
| 65 requiredMemProps, | 66 requiredMemProps, |
| 66 &alloc)) { | 67 &alloc, |
| 68 &offset)) { |
| 67 // Try again without requiring host cached memory | 69 // Try again without requiring host cached memory |
| 68 requiredMemProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | | 70 requiredMemProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | |
| 69 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; | 71 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; |
| 70 if (!GrVkMemory::AllocAndBindBufferMemory(gpu, | 72 if (!GrVkMemory::AllocAndBindBufferMemory(gpu, |
| 71 buffer, | 73 buffer, |
| 72 requiredMemProps, | 74 requiredMemProps, |
| 73 &alloc)) { | 75 &alloc, |
| 76 &offset)) { |
| 74 VK_CALL(gpu, DestroyBuffer(gpu->device(), buffer, nullptr)); | 77 VK_CALL(gpu, DestroyBuffer(gpu->device(), buffer, nullptr)); |
| 75 return nullptr; | 78 return nullptr; |
| 76 } | 79 } |
| 77 } | 80 } |
| 78 | 81 |
| 79 const GrVkBuffer::Resource* resource = new GrVkBuffer::Resource(buffer, allo
c); | 82 const GrVkBuffer::Resource* resource = new GrVkBuffer::Resource(buffer, allo
c, offset); |
| 80 if (!resource) { | 83 if (!resource) { |
| 81 VK_CALL(gpu, DestroyBuffer(gpu->device(), buffer, nullptr)); | 84 VK_CALL(gpu, DestroyBuffer(gpu->device(), buffer, nullptr)); |
| 82 VK_CALL(gpu, FreeMemory(gpu->device(), alloc, nullptr)); | 85 GrVkMemory::FreeBufferMemory(gpu, alloc, offset); |
| 83 return nullptr; | 86 return nullptr; |
| 84 } | 87 } |
| 85 | 88 |
| 86 return resource; | 89 return resource; |
| 87 } | 90 } |
| 88 | 91 |
| 89 | 92 |
| 90 void GrVkBuffer::addMemoryBarrier(const GrVkGpu* gpu, | 93 void GrVkBuffer::addMemoryBarrier(const GrVkGpu* gpu, |
| 91 VkAccessFlags srcAccessMask, | 94 VkAccessFlags srcAccessMask, |
| 92 VkAccessFlags dstAccesMask, | 95 VkAccessFlags dstAccesMask, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 106 }; | 109 }; |
| 107 | 110 |
| 108 // TODO: restrict to area of buffer we're interested in | 111 // TODO: restrict to area of buffer we're interested in |
| 109 gpu->addBufferMemoryBarrier(srcStageMask, dstStageMask, byRegion, &bufferMem
oryBarrier); | 112 gpu->addBufferMemoryBarrier(srcStageMask, dstStageMask, byRegion, &bufferMem
oryBarrier); |
| 110 } | 113 } |
| 111 | 114 |
| 112 void GrVkBuffer::Resource::freeGPUData(const GrVkGpu* gpu) const { | 115 void GrVkBuffer::Resource::freeGPUData(const GrVkGpu* gpu) const { |
| 113 SkASSERT(fBuffer); | 116 SkASSERT(fBuffer); |
| 114 SkASSERT(fAlloc); | 117 SkASSERT(fAlloc); |
| 115 VK_CALL(gpu, DestroyBuffer(gpu->device(), fBuffer, nullptr)); | 118 VK_CALL(gpu, DestroyBuffer(gpu->device(), fBuffer, nullptr)); |
| 116 VK_CALL(gpu, FreeMemory(gpu->device(), fAlloc, nullptr)); | 119 GrVkMemory::FreeBufferMemory(gpu, fAlloc, fOffset); |
| 117 } | 120 } |
| 118 | 121 |
| 119 void GrVkBuffer::vkRelease(const GrVkGpu* gpu) { | 122 void GrVkBuffer::vkRelease(const GrVkGpu* gpu) { |
| 120 VALIDATE(); | 123 VALIDATE(); |
| 121 fResource->unref(gpu); | 124 fResource->unref(gpu); |
| 122 fResource = nullptr; | 125 fResource = nullptr; |
| 123 fMapPtr = nullptr; | 126 fMapPtr = nullptr; |
| 124 VALIDATE(); | 127 VALIDATE(); |
| 125 } | 128 } |
| 126 | 129 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 VK_CALL(gpu, UnmapMemory(gpu->device(), alloc())); | 196 VK_CALL(gpu, UnmapMemory(gpu->device(), alloc())); |
| 194 | 197 |
| 195 return true; | 198 return true; |
| 196 } | 199 } |
| 197 | 200 |
| 198 void GrVkBuffer::validate() const { | 201 void GrVkBuffer::validate() const { |
| 199 SkASSERT(!fResource || kVertex_Type == fDesc.fType || kIndex_Type == fDesc.f
Type | 202 SkASSERT(!fResource || kVertex_Type == fDesc.fType || kIndex_Type == fDesc.f
Type |
| 200 || kCopyRead_Type == fDesc.fType || kCopyWrite_Type == fDesc.fType | 203 || kCopyRead_Type == fDesc.fType || kCopyWrite_Type == fDesc.fType |
| 201 || kUniform_Type == fDesc.fType); | 204 || kUniform_Type == fDesc.fType); |
| 202 } | 205 } |
| OLD | NEW |