blob: f096921955faea689aa87cbfc5965f910b730df0 [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"
jvanverth1e305ba2016-06-01 09:39:15 -070012#include "vk/GrVkTypes.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050013
14class GrVkGpu;
15
16/**
17 * This class serves as the base of GrVk*Buffer classes. It was written to avoid code
18 * duplication in those classes.
19 */
20class GrVkBuffer : public SkNoncopyable {
21public:
egdaniel848904e2016-07-29 11:47:58 -070022 virtual ~GrVkBuffer() {
Greg Daniel164a9f02016-02-22 09:56:40 -050023 // either release or abandon should have been called by the owner of this object.
24 SkASSERT(!fResource);
egdaniel927ac9c2016-09-19 09:32:09 -070025 delete [] (unsigned char*)fMapPtr;
Greg Daniel164a9f02016-02-22 09:56:40 -050026 }
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,
Greg Danielc2dd5ed2017-05-05 13:49:11 -040045 kTexel_Type,
Greg Daniel164a9f02016-02-22 09:56:40 -050046 kCopyRead_Type,
47 kCopyWrite_Type,
48 };
49
50protected:
51 struct Desc {
52 size_t fSizeInBytes;
53 Type fType; // vertex buffer, index buffer, etc.
54 bool fDynamic;
55 };
56
jvanverth4c6e47a2016-07-22 10:34:52 -070057 class Resource : public GrVkRecycledResource {
Greg Daniel164a9f02016-02-22 09:56:40 -050058 public:
jvanverth6b6ffc42016-06-13 14:28:07 -070059 Resource(VkBuffer buf, const GrVkAlloc& alloc, Type type)
60 : INHERITED(), fBuffer(buf), fAlloc(alloc), fType(type) {}
Greg Daniel164a9f02016-02-22 09:56:40 -050061
jvanverth7ec92412016-07-06 09:24:57 -070062#ifdef SK_TRACE_VK_RESOURCES
63 void dumpInfo() const override {
64 SkDebugf("GrVkBuffer: %d (%d refs)\n", fBuffer, this->getRefCnt());
65 }
66#endif
jvanverth6b6ffc42016-06-13 14:28:07 -070067 VkBuffer fBuffer;
68 GrVkAlloc fAlloc;
69 Type fType;
jvanverth1e305ba2016-06-01 09:39:15 -070070
Greg Daniel164a9f02016-02-22 09:56:40 -050071 private:
Ethan Nicholas8e265a72018-12-12 16:22:40 -050072 void freeGPUData(GrVkGpu* gpu) const override;
Greg Daniel164a9f02016-02-22 09:56:40 -050073
jvanverth4c6e47a2016-07-22 10:34:52 -070074 void onRecycle(GrVkGpu* gpu) const override { this->unref(gpu); }
75
76 typedef GrVkRecycledResource INHERITED;
Greg Daniel164a9f02016-02-22 09:56:40 -050077 };
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)
Greg Daniel81df0412018-05-31 13:13:33 -040084 : fDesc(desc), fResource(resource), fOffset(0), fMapPtr(nullptr) {
Greg Daniel164a9f02016-02-22 09:56:40 -050085 }
86
egdaniel927ac9c2016-09-19 09:32:09 -070087 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
egdaniel7cbffda2016-04-08 13:27:53 -070093 // 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.
jvanvertha584de92016-06-30 09:10:52 -070095 bool vkUpdateData(GrVkGpu* gpu, const void* src, size_t srcSizeInBytes,
egdaniel7cbffda2016-04-08 13:27:53 -070096 bool* createdNewBuffer = nullptr);
Greg Daniel164a9f02016-02-22 09:56:40 -050097
98 void vkAbandon();
99 void vkRelease(const GrVkGpu* gpu);
100
101private:
egdaniel31bc7df2016-07-29 10:46:06 -0700102 virtual const Resource* createResource(GrVkGpu* gpu,
103 const Desc& descriptor) {
104 return Create(gpu, descriptor);
105 }
106
egdaniel927ac9c2016-09-19 09:32:09 -0700107 void internalMap(GrVkGpu* gpu, size_t size, bool* createdNewBuffer = nullptr);
108 void internalUnmap(GrVkGpu* gpu, size_t size);
109
Greg Daniel164a9f02016-02-22 09:56:40 -0500110 void validate() const;
111 bool vkIsMapped() const;
112
113 Desc fDesc;
114 const Resource* fResource;
jvanverthdb379092016-07-07 11:18:46 -0700115 VkDeviceSize fOffset;
Greg Daniel164a9f02016-02-22 09:56:40 -0500116 void* fMapPtr;
117
jvanverthe50f3e72016-03-28 07:03:06 -0700118 typedef SkNoncopyable INHERITED;
Greg Daniel164a9f02016-02-22 09:56:40 -0500119};
120
121#endif