GrVkBuffer.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  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. #ifndef GrVkBuffer_DEFINED
  8. #define GrVkBuffer_DEFINED
  9. #include "include/gpu/vk/GrVkTypes.h"
  10. #include "src/gpu/vk/GrVkResource.h"
  11. class GrVkGpu;
  12. /**
  13. * This class serves as the base of GrVk*Buffer classes. It was written to avoid code
  14. * duplication in those classes.
  15. */
  16. class GrVkBuffer : public SkNoncopyable {
  17. public:
  18. virtual ~GrVkBuffer() {
  19. // either release or abandon should have been called by the owner of this object.
  20. SkASSERT(!fResource);
  21. delete [] (unsigned char*)fMapPtr;
  22. }
  23. VkBuffer buffer() const { return fResource->fBuffer; }
  24. const GrVkAlloc& alloc() const { return fResource->fAlloc; }
  25. const GrVkRecycledResource* resource() const { return fResource; }
  26. size_t size() const { return fDesc.fSizeInBytes; }
  27. VkDeviceSize offset() const { return fOffset; }
  28. void addMemoryBarrier(const GrVkGpu* gpu,
  29. VkAccessFlags srcAccessMask,
  30. VkAccessFlags dstAccessMask,
  31. VkPipelineStageFlags srcStageMask,
  32. VkPipelineStageFlags dstStageMask,
  33. bool byRegion) const;
  34. enum Type {
  35. kVertex_Type,
  36. kIndex_Type,
  37. kUniform_Type,
  38. kTexel_Type,
  39. kCopyRead_Type,
  40. kCopyWrite_Type,
  41. };
  42. protected:
  43. struct Desc {
  44. size_t fSizeInBytes;
  45. Type fType; // vertex buffer, index buffer, etc.
  46. bool fDynamic;
  47. };
  48. class Resource : public GrVkRecycledResource {
  49. public:
  50. Resource(VkBuffer buf, const GrVkAlloc& alloc, Type type)
  51. : INHERITED(), fBuffer(buf), fAlloc(alloc), fType(type) {}
  52. #ifdef SK_TRACE_VK_RESOURCES
  53. void dumpInfo() const override {
  54. SkDebugf("GrVkBuffer: %d (%d refs)\n", fBuffer, this->getRefCnt());
  55. }
  56. #endif
  57. VkBuffer fBuffer;
  58. GrVkAlloc fAlloc;
  59. Type fType;
  60. private:
  61. void freeGPUData(GrVkGpu* gpu) const override;
  62. void onRecycle(GrVkGpu* gpu) const override { this->unref(gpu); }
  63. typedef GrVkRecycledResource INHERITED;
  64. };
  65. // convenience routine for raw buffer creation
  66. static const Resource* Create(const GrVkGpu* gpu,
  67. const Desc& descriptor);
  68. GrVkBuffer(const Desc& desc, const GrVkBuffer::Resource* resource)
  69. : fDesc(desc), fResource(resource), fOffset(0), fMapPtr(nullptr) {
  70. }
  71. void* vkMap(GrVkGpu* gpu) {
  72. this->internalMap(gpu, fDesc.fSizeInBytes);
  73. return fMapPtr;
  74. }
  75. void vkUnmap(GrVkGpu* gpu) { this->internalUnmap(gpu, this->size()); }
  76. // If the caller passes in a non null createdNewBuffer, this function will set the bool to true
  77. // if it creates a new VkBuffer to upload the data to.
  78. bool vkUpdateData(GrVkGpu* gpu, const void* src, size_t srcSizeInBytes,
  79. bool* createdNewBuffer = nullptr);
  80. void vkAbandon();
  81. void vkRelease(const GrVkGpu* gpu);
  82. private:
  83. virtual const Resource* createResource(GrVkGpu* gpu,
  84. const Desc& descriptor) {
  85. return Create(gpu, descriptor);
  86. }
  87. void internalMap(GrVkGpu* gpu, size_t size, bool* createdNewBuffer = nullptr);
  88. void internalUnmap(GrVkGpu* gpu, size_t size);
  89. void copyCpuDataToGpuBuffer(GrVkGpu* gpu, const void* srcData, size_t size);
  90. void validate() const;
  91. bool vkIsMapped() const;
  92. Desc fDesc;
  93. const Resource* fResource;
  94. VkDeviceSize fOffset;
  95. void* fMapPtr;
  96. typedef SkNoncopyable INHERITED;
  97. };
  98. #endif