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