blob: bb529b3f7366dda79a4ce34210ceca0bb9969fb4 [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:
23 ~GrVkBuffer() {
24 // either release or abandon should have been called by the owner of this object.
25 SkASSERT(!fResource);
26 }
27
egdaniel31bc7df2016-07-29 10:46:06 -070028 VkBuffer buffer() const { return fResource->fBuffer; }
29 const GrVkAlloc& alloc() const { return fResource->fAlloc; }
30 const GrVkRecycledResource* resource() const { return fResource; }
31 size_t size() const { return fDesc.fSizeInBytes; }
32 VkDeviceSize offset() const { return fOffset; }
Greg Daniel164a9f02016-02-22 09:56:40 -050033
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
49protected:
50 struct Desc {
51 size_t fSizeInBytes;
52 Type fType; // vertex buffer, index buffer, etc.
53 bool fDynamic;
54 };
55
jvanverth4c6e47a2016-07-22 10:34:52 -070056 class Resource : public GrVkRecycledResource {
Greg Daniel164a9f02016-02-22 09:56:40 -050057 public:
jvanverth6b6ffc42016-06-13 14:28:07 -070058 Resource(VkBuffer buf, const GrVkAlloc& alloc, Type type)
59 : INHERITED(), fBuffer(buf), fAlloc(alloc), fType(type) {}
Greg Daniel164a9f02016-02-22 09:56:40 -050060
jvanverth7ec92412016-07-06 09:24:57 -070061#ifdef SK_TRACE_VK_RESOURCES
62 void dumpInfo() const override {
63 SkDebugf("GrVkBuffer: %d (%d refs)\n", fBuffer, this->getRefCnt());
64 }
65#endif
jvanverth6b6ffc42016-06-13 14:28:07 -070066 VkBuffer fBuffer;
67 GrVkAlloc fAlloc;
68 Type fType;
jvanverth1e305ba2016-06-01 09:39:15 -070069
Greg Daniel164a9f02016-02-22 09:56:40 -050070 private:
egdaniel50ead532016-07-13 14:23:26 -070071 void freeGPUData(const GrVkGpu* gpu) const override;
Greg Daniel164a9f02016-02-22 09:56:40 -050072
jvanverth4c6e47a2016-07-22 10:34:52 -070073 void onRecycle(GrVkGpu* gpu) const override { this->unref(gpu); }
74
75 typedef GrVkRecycledResource INHERITED;
Greg Daniel164a9f02016-02-22 09:56:40 -050076 };
77
78 // convenience routine for raw buffer creation
79 static const Resource* Create(const GrVkGpu* gpu,
80 const Desc& descriptor);
81
82 GrVkBuffer(const Desc& desc, const GrVkBuffer::Resource* resource)
jvanverthdb379092016-07-07 11:18:46 -070083 : fDesc(desc), fResource(resource), fOffset(0), fMapPtr(nullptr) {
Greg Daniel164a9f02016-02-22 09:56:40 -050084 }
85
86 void* vkMap(const GrVkGpu* gpu);
jvanverth069c4642016-07-06 12:56:11 -070087 void vkUnmap(GrVkGpu* gpu);
egdaniel7cbffda2016-04-08 13:27:53 -070088 // If the caller passes in a non null createdNewBuffer, this function will set the bool to true
89 // if it creates a new VkBuffer to upload the data to.
jvanvertha584de92016-06-30 09:10:52 -070090 bool vkUpdateData(GrVkGpu* gpu, const void* src, size_t srcSizeInBytes,
egdaniel7cbffda2016-04-08 13:27:53 -070091 bool* createdNewBuffer = nullptr);
Greg Daniel164a9f02016-02-22 09:56:40 -050092
93 void vkAbandon();
94 void vkRelease(const GrVkGpu* gpu);
95
96private:
egdaniel31bc7df2016-07-29 10:46:06 -070097 virtual const Resource* createResource(GrVkGpu* gpu,
98 const Desc& descriptor) {
99 return Create(gpu, descriptor);
100 }
101
Greg Daniel164a9f02016-02-22 09:56:40 -0500102 void validate() const;
103 bool vkIsMapped() const;
104
105 Desc fDesc;
106 const Resource* fResource;
jvanverthdb379092016-07-07 11:18:46 -0700107 VkDeviceSize fOffset;
Greg Daniel164a9f02016-02-22 09:56:40 -0500108 void* fMapPtr;
109
jvanverthe50f3e72016-03-28 07:03:06 -0700110 typedef SkNoncopyable INHERITED;
Greg Daniel164a9f02016-02-22 09:56:40 -0500111};
112
113#endif