GrVkRenderTarget.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 GrVkRenderTarget_DEFINED
  8. #define GrVkRenderTarget_DEFINED
  9. #include "include/gpu/GrRenderTarget.h"
  10. #include "src/gpu/vk/GrVkImage.h"
  11. #include "include/gpu/vk/GrVkTypes.h"
  12. #include "src/gpu/vk/GrVkRenderPass.h"
  13. #include "src/gpu/vk/GrVkResourceProvider.h"
  14. class GrVkCommandBuffer;
  15. class GrVkFramebuffer;
  16. class GrVkGpu;
  17. class GrVkImageView;
  18. class GrVkSecondaryCommandBuffer;
  19. class GrVkStencilAttachment;
  20. struct GrVkImageInfo;
  21. #ifdef SK_BUILD_FOR_WIN
  22. // Windows gives bogus warnings about inheriting asTexture/asRenderTarget via dominance.
  23. #pragma warning(push)
  24. #pragma warning(disable: 4250)
  25. #endif
  26. class GrVkRenderTarget: public GrRenderTarget, public virtual GrVkImage {
  27. public:
  28. static sk_sp<GrVkRenderTarget> MakeWrappedRenderTarget(GrVkGpu*, const GrSurfaceDesc&,
  29. int sampleCnt, const GrVkImageInfo&,
  30. sk_sp<GrVkImageLayout>);
  31. static sk_sp<GrVkRenderTarget> MakeSecondaryCBRenderTarget(GrVkGpu*, const GrSurfaceDesc&,
  32. const GrVkDrawableInfo& vkInfo);
  33. ~GrVkRenderTarget() override;
  34. GrBackendFormat backendFormat() const override { return this->getBackendFormat(); }
  35. const GrVkFramebuffer* framebuffer() const { return fFramebuffer; }
  36. const GrVkImageView* colorAttachmentView() const { return fColorAttachmentView; }
  37. const GrVkResource* msaaImageResource() const {
  38. if (fMSAAImage) {
  39. return fMSAAImage->fResource;
  40. }
  41. return nullptr;
  42. }
  43. GrVkImage* msaaImage() { return fMSAAImage.get(); }
  44. const GrVkImageView* resolveAttachmentView() const { return fResolveAttachmentView; }
  45. const GrVkResource* stencilImageResource() const;
  46. const GrVkImageView* stencilAttachmentView() const;
  47. const GrVkRenderPass* simpleRenderPass() const { return fCachedSimpleRenderPass; }
  48. GrVkResourceProvider::CompatibleRPHandle compatibleRenderPassHandle() const {
  49. SkASSERT(!this->wrapsSecondaryCommandBuffer());
  50. return fCompatibleRPHandle;
  51. }
  52. const GrVkRenderPass* externalRenderPass() const {
  53. SkASSERT(this->wrapsSecondaryCommandBuffer());
  54. // We use the cached simple render pass to hold the external render pass.
  55. return fCachedSimpleRenderPass;
  56. }
  57. bool wrapsSecondaryCommandBuffer() const { return fSecondaryCommandBuffer != nullptr; }
  58. GrVkSecondaryCommandBuffer* getExternalSecondaryCommandBuffer() const {
  59. return fSecondaryCommandBuffer;
  60. }
  61. // override of GrRenderTarget
  62. ResolveType getResolveType() const override {
  63. if (this->numSamples() > 1) {
  64. return kCanResolve_ResolveType;
  65. }
  66. return kAutoResolves_ResolveType;
  67. }
  68. bool canAttemptStencilAttachment() const override {
  69. // We don't know the status of the stencil attachment for wrapped external secondary command
  70. // buffers so we just assume we don't have one.
  71. return !this->wrapsSecondaryCommandBuffer();
  72. }
  73. GrBackendRenderTarget getBackendRenderTarget() const override;
  74. void getAttachmentsDescriptor(GrVkRenderPass::AttachmentsDescriptor* desc,
  75. GrVkRenderPass::AttachmentFlags* flags) const;
  76. void addResources(GrVkCommandBuffer& commandBuffer) const;
  77. protected:
  78. GrVkRenderTarget(GrVkGpu* gpu,
  79. const GrSurfaceDesc& desc,
  80. int sampleCnt,
  81. const GrVkImageInfo& info,
  82. sk_sp<GrVkImageLayout> layout,
  83. const GrVkImageInfo& msaaInfo,
  84. sk_sp<GrVkImageLayout> msaaLayout,
  85. const GrVkImageView* colorAttachmentView,
  86. const GrVkImageView* resolveAttachmentView,
  87. GrBackendObjectOwnership);
  88. GrVkRenderTarget(GrVkGpu* gpu,
  89. const GrSurfaceDesc& desc,
  90. const GrVkImageInfo& info,
  91. sk_sp<GrVkImageLayout> layout,
  92. const GrVkImageView* colorAttachmentView,
  93. GrBackendObjectOwnership);
  94. GrVkGpu* getVkGpu() const;
  95. void onAbandon() override;
  96. void onRelease() override;
  97. // This accounts for the texture's memory and any MSAA renderbuffer's memory.
  98. size_t onGpuMemorySize() const override {
  99. int numColorSamples = this->numSamples();
  100. if (numColorSamples > 1) {
  101. // Add one to account for the resolved VkImage.
  102. numColorSamples += 1;
  103. }
  104. return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
  105. numColorSamples, GrMipMapped::kNo);
  106. }
  107. void createFramebuffer(GrVkGpu* gpu);
  108. const GrVkImageView* fColorAttachmentView;
  109. std::unique_ptr<GrVkImage> fMSAAImage;
  110. const GrVkImageView* fResolveAttachmentView;
  111. private:
  112. GrVkRenderTarget(GrVkGpu* gpu,
  113. const GrSurfaceDesc& desc,
  114. int sampleCnt,
  115. const GrVkImageInfo& info,
  116. sk_sp<GrVkImageLayout> layout,
  117. const GrVkImageInfo& msaaInfo,
  118. sk_sp<GrVkImageLayout> msaaLayout,
  119. const GrVkImageView* colorAttachmentView,
  120. const GrVkImageView* resolveAttachmentView);
  121. GrVkRenderTarget(GrVkGpu* gpu,
  122. const GrSurfaceDesc& desc,
  123. const GrVkImageInfo& info,
  124. sk_sp<GrVkImageLayout> layout,
  125. const GrVkImageView* colorAttachmentView);
  126. GrVkRenderTarget(GrVkGpu* gpu,
  127. const GrSurfaceDesc& desc,
  128. const GrVkImageInfo& info,
  129. sk_sp<GrVkImageLayout> layout,
  130. const GrVkRenderPass* renderPass,
  131. GrVkSecondaryCommandBuffer* secondaryCommandBuffer);
  132. bool completeStencilAttachment() override;
  133. // In Vulkan we call the release proc after we are finished with the underlying
  134. // GrVkImage::Resource object (which occurs after the GPU has finished all work on it).
  135. void onSetRelease(sk_sp<GrRefCntedCallback> releaseHelper) override {
  136. // Forward the release proc on to GrVkImage
  137. this->setResourceRelease(std::move(releaseHelper));
  138. }
  139. void releaseInternalObjects();
  140. void abandonInternalObjects();
  141. const GrVkFramebuffer* fFramebuffer;
  142. // This is a cached pointer to a simple render pass. The render target should unref it
  143. // once it is done with it.
  144. const GrVkRenderPass* fCachedSimpleRenderPass;
  145. // This is a handle to be used to quickly get compatible GrVkRenderPasses for this render target
  146. GrVkResourceProvider::CompatibleRPHandle fCompatibleRPHandle;
  147. // If this render target wraps an external VkCommandBuffer, then this pointer will be non-null
  148. // and will point to the GrVk object that, in turn, wraps the external VkCommandBuffer. In this
  149. // case the render target will not be backed by an actual VkImage and will thus be limited in
  150. // terms of what it can be used for.
  151. GrVkSecondaryCommandBuffer* fSecondaryCommandBuffer = nullptr;
  152. };
  153. #endif