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