Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef GrVkBuffer_DEFINED |
| 9 | #define GrVkBuffer_DEFINED |
| 10 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 11 | #include "GrVkResource.h" |
jvanverth | e50f3e7 | 2016-03-28 07:03:06 -0700 | [diff] [blame] | 12 | #include "vk/GrVkDefines.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 13 | |
| 14 | class GrVkGpu; |
| 15 | |
| 16 | /** |
| 17 | * This class serves as the base of GrVk*Buffer classes. It was written to avoid code |
| 18 | * duplication in those classes. |
| 19 | */ |
| 20 | class GrVkBuffer : public SkNoncopyable { |
| 21 | public: |
| 22 | ~GrVkBuffer() { |
| 23 | // either release or abandon should have been called by the owner of this object. |
| 24 | SkASSERT(!fResource); |
| 25 | } |
| 26 | |
| 27 | VkBuffer buffer() const { return fResource->fBuffer; } |
| 28 | VkDeviceMemory alloc() const { return fResource->fAlloc; } |
| 29 | const GrVkResource* resource() const { return fResource; } |
| 30 | size_t size() const { return fDesc.fSizeInBytes; } |
| 31 | |
| 32 | void addMemoryBarrier(const GrVkGpu* gpu, |
| 33 | VkAccessFlags srcAccessMask, |
| 34 | VkAccessFlags dstAccessMask, |
| 35 | VkPipelineStageFlags srcStageMask, |
| 36 | VkPipelineStageFlags dstStageMask, |
| 37 | bool byRegion) const; |
| 38 | |
| 39 | enum Type { |
| 40 | kVertex_Type, |
| 41 | kIndex_Type, |
| 42 | kUniform_Type, |
| 43 | kCopyRead_Type, |
| 44 | kCopyWrite_Type, |
| 45 | }; |
| 46 | |
| 47 | protected: |
| 48 | struct Desc { |
| 49 | size_t fSizeInBytes; |
| 50 | Type fType; // vertex buffer, index buffer, etc. |
| 51 | bool fDynamic; |
| 52 | }; |
| 53 | |
| 54 | class Resource : public GrVkResource { |
| 55 | public: |
| 56 | Resource(VkBuffer buf, VkDeviceMemory alloc) : INHERITED(), fBuffer(buf), fAlloc(alloc) {} |
| 57 | |
| 58 | VkBuffer fBuffer; |
| 59 | VkDeviceMemory fAlloc; |
| 60 | private: |
| 61 | void freeGPUData(const GrVkGpu* gpu) const; |
| 62 | |
| 63 | typedef GrVkResource INHERITED; |
| 64 | }; |
| 65 | |
| 66 | // convenience routine for raw buffer creation |
| 67 | static const Resource* Create(const GrVkGpu* gpu, |
| 68 | const Desc& descriptor); |
| 69 | |
| 70 | GrVkBuffer(const Desc& desc, const GrVkBuffer::Resource* resource) |
| 71 | : fDesc(desc), fResource(resource), fMapPtr(nullptr) { |
| 72 | } |
| 73 | |
| 74 | void* vkMap(const GrVkGpu* gpu); |
| 75 | void vkUnmap(const GrVkGpu* gpu); |
egdaniel | 7cbffda | 2016-04-08 13:27:53 -0700 | [diff] [blame^] | 76 | // If the caller passes in a non null createdNewBuffer, this function will set the bool to true |
| 77 | // if it creates a new VkBuffer to upload the data to. |
| 78 | bool vkUpdateData(const GrVkGpu* gpu, const void* src, size_t srcSizeInBytes, |
| 79 | bool* createdNewBuffer = nullptr); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 80 | |
| 81 | void vkAbandon(); |
| 82 | void vkRelease(const GrVkGpu* gpu); |
| 83 | |
| 84 | private: |
| 85 | void validate() const; |
| 86 | bool vkIsMapped() const; |
| 87 | |
| 88 | Desc fDesc; |
| 89 | const Resource* fResource; |
| 90 | void* fMapPtr; |
| 91 | |
jvanverth | e50f3e7 | 2016-03-28 07:03:06 -0700 | [diff] [blame] | 92 | typedef SkNoncopyable INHERITED; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | #endif |