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 dstAccessMask, 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 39 void onMap() override; 40 void onUnmap() override; 41 42 void internalMap(size_t readOffset, size_t readSize); 43 void internalUnmap(size_t flushOffset, size_t flushSize); 44 isMappable()45 bool isMappable() const { return fAlloc.fFlags & skgpu::VulkanAlloc::kMappable_Flag; } 46 vulkanSharedContext()47 const VulkanSharedContext* vulkanSharedContext() const { 48 return static_cast<const VulkanSharedContext*>(this->sharedContext()); 49 } 50 51 static VkPipelineStageFlags AccessMaskToPipelineSrcStageFlags(const VkAccessFlags accessFlags); 52 53 VkBuffer fBuffer; 54 skgpu::VulkanAlloc fAlloc; 55 const VkBufferUsageFlags fBufferUsageFlags; 56 mutable VkAccessFlags fCurrentAccessMask = 0; 57 58 /** 59 * Buffers can either be mapped for: 60 * 1) Reading from the CPU (The effect of writing would be undefined) 61 * 2) Writing from the CPU (The existing contents are discarded. Even in the case where the 62 * initial contents are overwritten, CPU reads should be avoided for performance reasons as 63 * the memory may not be cached). 64 */ 65 bool fBufferUsedForCPURead = false; 66 }; 67 68 } // namespace skgpu::graphite 69 70 #endif // skgpu_graphite_VulkanBuffer_DEFINED 71 72