• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
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 skgpu_graphite_VulkanBuffer_DEFINED
9 #define skgpu_graphite_VulkanBuffer_DEFINED
10 
11 #include "include/gpu/vk/VulkanMemoryAllocator.h"
12 #include "src/gpu/graphite/Buffer.h"
13 #include "src/gpu/graphite/vk/VulkanSharedContext.h"
14 
15 namespace skgpu::graphite {
16 
17 class VulkanCommandBuffer;
18 
19 class VulkanBuffer final : public Buffer {
20 public:
21     static sk_sp<Buffer> Make(const VulkanSharedContext*, size_t, BufferType, AccessPattern);
22     void freeGpuData() override;
vkBuffer()23     VkBuffer vkBuffer() const { return fBuffer; }
bufferUsageFlags()24     VkBufferUsageFlags bufferUsageFlags() const { return fBufferUsageFlags; }
25 
26     void setBufferAccess(VulkanCommandBuffer* buffer,
27                          VkAccessFlags dstAccess,
28                          VkPipelineStageFlags dstStageMask) const;
29 
30 private:
31     VulkanBuffer(const VulkanSharedContext*,
32                  size_t,
33                  BufferType,
34                  AccessPattern,
35                  VkBuffer,
36                  const skgpu::VulkanAlloc&,
37                  VkBufferUsageFlags,
38                  Protected isProtected);
39 
40     void onMap() override;
41     void onUnmap() override;
42 
43     void internalMap(size_t readOffset, size_t readSize);
44     void internalUnmap(size_t flushOffset, size_t flushSize);
45 
isMappable()46     bool isMappable() const { return fAlloc.fFlags & skgpu::VulkanAlloc::kMappable_Flag; }
47 
vulkanSharedContext()48     const VulkanSharedContext* vulkanSharedContext() const {
49         return static_cast<const VulkanSharedContext*>(this->sharedContext());
50     }
51 
52     VkBuffer fBuffer;
53     skgpu::VulkanAlloc fAlloc;
54     const VkBufferUsageFlags fBufferUsageFlags;
55     mutable VkAccessFlags fCurrentAccess = 0;
56 
57     /**
58      * Buffers can either be mapped for:
59      * 1) Reading from the CPU (The effect of writing would be undefined)
60      * 2) Writing from the CPU (The existing contents are discarded. Even in the case where the
61      *    initial contents are overwritten, CPU reads should be avoided for performance reasons as
62      *    the memory may not be cached).
63      */
64     bool fBufferUsedForCPURead = false;
65 };
66 
67 } // namespace skgpu::graphite
68 
69 #endif // skgpu_graphite_VulkanBuffer_DEFINED
70 
71