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 | db37909 | 2016-07-07 11:18:46 -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; } |
jvanverth | db37909 | 2016-07-07 11:18:46 -0700 | [diff] [blame] | 31 | size_t size() const { return fDesc.fSizeInBytes; } |
| 32 | VkDeviceSize offset() const { return fOffset; } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 33 | |
| 34 | void addMemoryBarrier(const GrVkGpu* gpu, |
| 35 | VkAccessFlags srcAccessMask, |
| 36 | VkAccessFlags dstAccessMask, |
| 37 | VkPipelineStageFlags srcStageMask, |
| 38 | VkPipelineStageFlags dstStageMask, |
| 39 | bool byRegion) const; |
| 40 | |
| 41 | enum Type { |
| 42 | kVertex_Type, |
| 43 | kIndex_Type, |
| 44 | kUniform_Type, |
| 45 | kCopyRead_Type, |
| 46 | kCopyWrite_Type, |
| 47 | }; |
| 48 | |
| 49 | protected: |
| 50 | struct Desc { |
| 51 | size_t fSizeInBytes; |
| 52 | Type fType; // vertex buffer, index buffer, etc. |
| 53 | bool fDynamic; |
| 54 | }; |
| 55 | |
| 56 | class Resource : public GrVkResource { |
| 57 | public: |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 58 | Resource(VkBuffer buf, const GrVkAlloc& alloc, Type type) |
| 59 | : INHERITED(), fBuffer(buf), fAlloc(alloc), fType(type) {} |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 60 | |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 61 | #ifdef SK_TRACE_VK_RESOURCES |
| 62 | void dumpInfo() const override { |
| 63 | SkDebugf("GrVkBuffer: %d (%d refs)\n", fBuffer, this->getRefCnt()); |
| 64 | } |
| 65 | #endif |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 66 | VkBuffer fBuffer; |
| 67 | GrVkAlloc fAlloc; |
| 68 | Type fType; |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 69 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 70 | private: |
egdaniel | 50ead53 | 2016-07-13 14:23:26 -0700 | [diff] [blame^] | 71 | void freeGPUData(const GrVkGpu* gpu) const override; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 72 | |
| 73 | typedef GrVkResource INHERITED; |
| 74 | }; |
| 75 | |
| 76 | // convenience routine for raw buffer creation |
| 77 | static const Resource* Create(const GrVkGpu* gpu, |
| 78 | const Desc& descriptor); |
| 79 | |
| 80 | GrVkBuffer(const Desc& desc, const GrVkBuffer::Resource* resource) |
jvanverth | db37909 | 2016-07-07 11:18:46 -0700 | [diff] [blame] | 81 | : fDesc(desc), fResource(resource), fOffset(0), fMapPtr(nullptr) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void* vkMap(const GrVkGpu* gpu); |
jvanverth | 069c464 | 2016-07-06 12:56:11 -0700 | [diff] [blame] | 85 | void vkUnmap(GrVkGpu* gpu); |
egdaniel | 7cbffda | 2016-04-08 13:27:53 -0700 | [diff] [blame] | 86 | // If the caller passes in a non null createdNewBuffer, this function will set the bool to true |
| 87 | // if it creates a new VkBuffer to upload the data to. |
jvanverth | a584de9 | 2016-06-30 09:10:52 -0700 | [diff] [blame] | 88 | bool vkUpdateData(GrVkGpu* gpu, const void* src, size_t srcSizeInBytes, |
egdaniel | 7cbffda | 2016-04-08 13:27:53 -0700 | [diff] [blame] | 89 | bool* createdNewBuffer = nullptr); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 90 | |
| 91 | void vkAbandon(); |
| 92 | void vkRelease(const GrVkGpu* gpu); |
| 93 | |
| 94 | private: |
| 95 | void validate() const; |
| 96 | bool vkIsMapped() const; |
| 97 | |
| 98 | Desc fDesc; |
| 99 | const Resource* fResource; |
jvanverth | db37909 | 2016-07-07 11:18:46 -0700 | [diff] [blame] | 100 | VkDeviceSize fOffset; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 101 | void* fMapPtr; |
| 102 | |
jvanverth | e50f3e7 | 2016-03-28 07:03:06 -0700 | [diff] [blame] | 103 | typedef SkNoncopyable INHERITED; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | #endif |