blob: 6d0c1fda9a19c6e83e9435b5d5911745b5d598c2 [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
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 Daniel164a9f02016-02-22 09:56:40 -050011#include "GrVkResource.h"
jvanverthe50f3e72016-03-28 07:03:06 -070012#include "vk/GrVkDefines.h"
jvanverth1e305ba2016-06-01 09:39:15 -070013#include "vk/GrVkTypes.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050014
15class 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 */
21class GrVkBuffer : public SkNoncopyable {
22public:
egdaniel848904e2016-07-29 11:47:58 -070023 virtual ~GrVkBuffer() {
Greg Daniel164a9f02016-02-22 09:56:40 -050024 // either release or abandon should have been called by the owner of this object.
25 SkASSERT(!fResource);
egdaniel927ac9c2016-09-19 09:32:09 -070026 delete [] (unsigned char*)fMapPtr;
Greg Daniel164a9f02016-02-22 09:56:40 -050027 }
28
egdaniel31bc7df2016-07-29 10:46:06 -070029 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 Daniel164a9f02016-02-22 09:56:40 -050034
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,
Greg Danielc2dd5ed2017-05-05 13:49:11 -040046 kTexel_Type,
Greg Daniel164a9f02016-02-22 09:56:40 -050047 kCopyRead_Type,
48 kCopyWrite_Type,
49 };
50
51protected:
52 struct Desc {
53 size_t fSizeInBytes;
54 Type fType; // vertex buffer, index buffer, etc.
55 bool fDynamic;
56 };
57
jvanverth4c6e47a2016-07-22 10:34:52 -070058 class Resource : public GrVkRecycledResource {
Greg Daniel164a9f02016-02-22 09:56:40 -050059 public:
jvanverth6b6ffc42016-06-13 14:28:07 -070060 Resource(VkBuffer buf, const GrVkAlloc& alloc, Type type)
61 : INHERITED(), fBuffer(buf), fAlloc(alloc), fType(type) {}
Greg Daniel164a9f02016-02-22 09:56:40 -050062
jvanverth7ec92412016-07-06 09:24:57 -070063#ifdef SK_TRACE_VK_RESOURCES
64 void dumpInfo() const override {
65 SkDebugf("GrVkBuffer: %d (%d refs)\n", fBuffer, this->getRefCnt());
66 }
67#endif
jvanverth6b6ffc42016-06-13 14:28:07 -070068 VkBuffer fBuffer;
69 GrVkAlloc fAlloc;
70 Type fType;
jvanverth1e305ba2016-06-01 09:39:15 -070071
Greg Daniel164a9f02016-02-22 09:56:40 -050072 private:
egdaniel50ead532016-07-13 14:23:26 -070073 void freeGPUData(const GrVkGpu* gpu) const override;
Greg Daniel164a9f02016-02-22 09:56:40 -050074
jvanverth4c6e47a2016-07-22 10:34:52 -070075 void onRecycle(GrVkGpu* gpu) const override { this->unref(gpu); }
76
77 typedef GrVkRecycledResource INHERITED;
Greg Daniel164a9f02016-02-22 09:56:40 -050078 };
79
80 // convenience routine for raw buffer creation
81 static const Resource* Create(const GrVkGpu* gpu,
82 const Desc& descriptor);
83
84 GrVkBuffer(const Desc& desc, const GrVkBuffer::Resource* resource)
Greg Daniel81df0412018-05-31 13:13:33 -040085 : fDesc(desc), fResource(resource), fOffset(0), fMapPtr(nullptr) {
Greg Daniel164a9f02016-02-22 09:56:40 -050086 }
87
egdaniel927ac9c2016-09-19 09:32:09 -070088 void* vkMap(GrVkGpu* gpu) {
89 this->internalMap(gpu, fDesc.fSizeInBytes);
90 return fMapPtr;
91 }
92 void vkUnmap(GrVkGpu* gpu) { this->internalUnmap(gpu, this->size()); }
93
egdaniel7cbffda2016-04-08 13:27:53 -070094 // If the caller passes in a non null createdNewBuffer, this function will set the bool to true
95 // if it creates a new VkBuffer to upload the data to.
jvanvertha584de92016-06-30 09:10:52 -070096 bool vkUpdateData(GrVkGpu* gpu, const void* src, size_t srcSizeInBytes,
egdaniel7cbffda2016-04-08 13:27:53 -070097 bool* createdNewBuffer = nullptr);
Greg Daniel164a9f02016-02-22 09:56:40 -050098
99 void vkAbandon();
100 void vkRelease(const GrVkGpu* gpu);
101
102private:
egdaniel31bc7df2016-07-29 10:46:06 -0700103 virtual const Resource* createResource(GrVkGpu* gpu,
104 const Desc& descriptor) {
105 return Create(gpu, descriptor);
106 }
107
egdaniel927ac9c2016-09-19 09:32:09 -0700108 void internalMap(GrVkGpu* gpu, size_t size, bool* createdNewBuffer = nullptr);
109 void internalUnmap(GrVkGpu* gpu, size_t size);
110
Greg Daniel164a9f02016-02-22 09:56:40 -0500111 void validate() const;
112 bool vkIsMapped() const;
113
114 Desc fDesc;
115 const Resource* fResource;
jvanverthdb379092016-07-07 11:18:46 -0700116 VkDeviceSize fOffset;
Greg Daniel164a9f02016-02-22 09:56:40 -0500117 void* fMapPtr;
118
jvanverthe50f3e72016-03-28 07:03:06 -0700119 typedef SkNoncopyable INHERITED;
Greg Daniel164a9f02016-02-22 09:56:40 -0500120};
121
122#endif