blob: f7d43c796d592b7ca317afefe82506da71170859 [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
jvanverth1e305ba2016-06-01 09:39:15 -070028 VkBuffer buffer() const { return fResource->fBuffer; }
29 const GrVkAlloc& alloc() const { return fResource->fAlloc; }
Greg Daniel164a9f02016-02-22 09:56:40 -050030 const GrVkResource* resource() const { return fResource; }
31 size_t size() const { return fDesc.fSizeInBytes; }
32
33 void addMemoryBarrier(const GrVkGpu* gpu,
34 VkAccessFlags srcAccessMask,
35 VkAccessFlags dstAccessMask,
36 VkPipelineStageFlags srcStageMask,
37 VkPipelineStageFlags dstStageMask,
38 bool byRegion) const;
39
40 enum Type {
41 kVertex_Type,
42 kIndex_Type,
43 kUniform_Type,
44 kCopyRead_Type,
45 kCopyWrite_Type,
46 };
47
48protected:
49 struct Desc {
50 size_t fSizeInBytes;
51 Type fType; // vertex buffer, index buffer, etc.
52 bool fDynamic;
53 };
54
55 class Resource : public GrVkResource {
56 public:
jvanverth1e305ba2016-06-01 09:39:15 -070057 Resource(VkBuffer buf, const GrVkAlloc& alloc)
58 : INHERITED(), fBuffer(buf), fAlloc(alloc) {}
Greg Daniel164a9f02016-02-22 09:56:40 -050059
jvanverth1e305ba2016-06-01 09:39:15 -070060 VkBuffer fBuffer;
61 GrVkAlloc fAlloc;
62
Greg Daniel164a9f02016-02-22 09:56:40 -050063 private:
64 void freeGPUData(const GrVkGpu* gpu) const;
65
66 typedef GrVkResource INHERITED;
67 };
68
69 // convenience routine for raw buffer creation
70 static const Resource* Create(const GrVkGpu* gpu,
71 const Desc& descriptor);
72
73 GrVkBuffer(const Desc& desc, const GrVkBuffer::Resource* resource)
74 : fDesc(desc), fResource(resource), fMapPtr(nullptr) {
75 }
76
77 void* vkMap(const GrVkGpu* gpu);
78 void vkUnmap(const GrVkGpu* gpu);
egdaniel7cbffda2016-04-08 13:27:53 -070079 // If the caller passes in a non null createdNewBuffer, this function will set the bool to true
80 // if it creates a new VkBuffer to upload the data to.
81 bool vkUpdateData(const GrVkGpu* gpu, const void* src, size_t srcSizeInBytes,
82 bool* createdNewBuffer = nullptr);
Greg Daniel164a9f02016-02-22 09:56:40 -050083
84 void vkAbandon();
85 void vkRelease(const GrVkGpu* gpu);
86
87private:
88 void validate() const;
89 bool vkIsMapped() const;
90
91 Desc fDesc;
92 const Resource* fResource;
93 void* fMapPtr;
94
jvanverthe50f3e72016-03-28 07:03:06 -070095 typedef SkNoncopyable INHERITED;
Greg Daniel164a9f02016-02-22 09:56:40 -050096};
97
98#endif