| 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; |
| 23 |
VkBuffer vkBuffer() const { return fBuffer; } |
| 24 |
VkBufferUsageFlags bufferUsageFlags() const { return fBufferUsageFlags; } |
| 25 |
|
| 26 |
void setBufferAccess(VulkanCommandBuffer* buffer, |
| 27 |
VkAccessFlags dstAccess, |
| 28 |
VkPipelineStageFlags dstStageMask) const; |
| 29 |
|
| 30 |
bool bufferUsedForCpuRead() const { return fBufferUsedForCPURead; } |
| 31 |
|
| 32 |
private: |
| 33 |
VulkanBuffer(const VulkanSharedContext*, |
| 34 |
size_t, |
| 35 |
BufferType, |
| 36 |
AccessPattern, |
| 37 |
VkBuffer, |
| 38 |
const skgpu::VulkanAlloc&, |
| 39 |
VkBufferUsageFlags, |
| 40 |
Protected isProtected); |
| 41 |
|
| 42 |
void onMap() override; |
| 43 |
void onUnmap() override; |
| 44 |
|
| 45 |
void internalMap(size_t readOffset, size_t readSize); |
| 46 |
void internalUnmap(size_t flushOffset, size_t flushSize); |
| 47 |
|
| 48 |
bool isMappable() const { return fAlloc.fFlags & skgpu::VulkanAlloc::kMappable_Flag; } |
| 49 |
|
| 50 |
const VulkanSharedContext* vulkanSharedContext() const { |
| 51 |
return static_cast<const VulkanSharedContext*>(this->sharedContext()); |
| 52 |
} |
| 53 |
|
| 54 |
VkBuffer fBuffer; |
| 55 |
skgpu::VulkanAlloc fAlloc; |
| 56 |
const VkBufferUsageFlags fBufferUsageFlags; |
| 57 |
mutable VkAccessFlags fCurrentAccess = 0; |
| 58 |
|
| 59 |
/** |
| 60 |
* Buffers can either be mapped for: |
| 61 |
* 1) Reading from the CPU (The effect of writing would be undefined) |
| 62 |
* 2) Writing from the CPU (The existing contents are discarded. Even in the case where the |
| 63 |
* initial contents are overwritten, CPU reads should be avoided for performance reasons as |
| 64 |
* the memory may not be cached). |
| 65 |
*/ |
| 66 |
bool fBufferUsedForCPURead = false; |
| 67 |
}; |
| 68 |
|
| 69 |
} // namespace skgpu::graphite |
| 70 |
|
| 71 |
#endif // skgpu_graphite_VulkanBuffer_DEFINED |
| 72 |
|