Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 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 | |
| 8 | |
| 9 | #ifndef GrVkRenderTarget_DEFINED |
| 10 | #define GrVkRenderTarget_DEFINED |
| 11 | |
Brian Salomon | 201cdbb | 2019-08-14 17:00:30 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrRenderTarget.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/vk/GrVkImage.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 14 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/gpu/vk/GrVkTypes.h" |
| 16 | #include "src/gpu/vk/GrVkRenderPass.h" |
| 17 | #include "src/gpu/vk/GrVkResourceProvider.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 18 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 19 | class GrVkFramebuffer; |
| 20 | class GrVkGpu; |
| 21 | class GrVkImageView; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 22 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 23 | struct GrVkImageInfo; |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 24 | |
Greg Daniel | de4bbdb | 2021-04-13 14:23:23 -0400 | [diff] [blame] | 25 | class GrVkRenderTarget : public GrRenderTarget { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 26 | public: |
Greg Daniel | de4bbdb | 2021-04-13 14:23:23 -0400 | [diff] [blame] | 27 | static sk_sp<GrVkRenderTarget> MakeWrappedRenderTarget(GrVkGpu*, |
| 28 | SkISize, |
| 29 | int sampleCnt, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 30 | const GrVkImageInfo&, |
Greg Daniel | 6c6caf4 | 2020-05-29 12:11:05 -0400 | [diff] [blame] | 31 | sk_sp<GrBackendSurfaceMutableStateImpl>); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 32 | |
Greg Daniel | de4bbdb | 2021-04-13 14:23:23 -0400 | [diff] [blame] | 33 | static sk_sp<GrVkRenderTarget> MakeSecondaryCBRenderTarget(GrVkGpu*, |
| 34 | SkISize, |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 35 | const GrVkDrawableInfo& vkInfo); |
| 36 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 37 | ~GrVkRenderTarget() override; |
| 38 | |
Greg Daniel | de4bbdb | 2021-04-13 14:23:23 -0400 | [diff] [blame] | 39 | GrBackendFormat backendFormat() const override; |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 40 | |
Greg Daniel | 2177436 | 2020-09-14 10:36:43 -0400 | [diff] [blame] | 41 | using SelfDependencyFlags = GrVkRenderPass::SelfDependencyFlags; |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 42 | using LoadFromResolve = GrVkRenderPass::LoadFromResolve; |
Greg Daniel | 2177436 | 2020-09-14 10:36:43 -0400 | [diff] [blame] | 43 | |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 44 | const GrVkFramebuffer* getFramebuffer(bool withResolve, |
| 45 | bool withStencil, |
| 46 | SelfDependencyFlags selfDepFlags, |
| 47 | LoadFromResolve); |
| 48 | const GrVkFramebuffer* getFramebuffer(const GrVkRenderPass& renderPass) { |
| 49 | return this->getFramebuffer(renderPass.hasResolveAttachment(), |
| 50 | renderPass.hasStencilAttachment(), |
| 51 | renderPass.selfDependencyFlags(), |
| 52 | renderPass.loadFromResolve()); |
| 53 | } |
Greg Daniel | e2893f2 | 2020-10-13 10:20:06 -0400 | [diff] [blame] | 54 | |
Greg Daniel | 2bc96d6 | 2021-09-13 13:08:02 -0400 | [diff] [blame^] | 55 | GrVkImage* colorAttachment() const { |
Greg Daniel | de4bbdb | 2021-04-13 14:23:23 -0400 | [diff] [blame] | 56 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
| 57 | return fColorAttachment.get(); |
| 58 | } |
| 59 | const GrVkImageView* colorAttachmentView() const { |
| 60 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
Greg Daniel | 60ec617 | 2021-04-16 11:31:58 -0400 | [diff] [blame] | 61 | return this->colorAttachment()->framebufferView(); |
Greg Daniel | de4bbdb | 2021-04-13 14:23:23 -0400 | [diff] [blame] | 62 | } |
Greg Daniel | e2893f2 | 2020-10-13 10:20:06 -0400 | [diff] [blame] | 63 | |
Greg Daniel | 2bc96d6 | 2021-09-13 13:08:02 -0400 | [diff] [blame^] | 64 | GrVkImage* resolveAttachment() const { |
Greg Daniel | de4bbdb | 2021-04-13 14:23:23 -0400 | [diff] [blame] | 65 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
| 66 | return fResolveAttachment.get(); |
| 67 | } |
Greg Daniel | 00d6cf4 | 2021-03-05 22:50:08 +0000 | [diff] [blame] | 68 | const GrVkImageView* resolveAttachmentView() const { |
Greg Daniel | de4bbdb | 2021-04-13 14:23:23 -0400 | [diff] [blame] | 69 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
Greg Daniel | 00d6cf4 | 2021-03-05 22:50:08 +0000 | [diff] [blame] | 70 | return fResolveAttachment->framebufferView(); |
| 71 | } |
Greg Daniel | e2893f2 | 2020-10-13 10:20:06 -0400 | [diff] [blame] | 72 | |
Greg Daniel | 2bc96d6 | 2021-09-13 13:08:02 -0400 | [diff] [blame^] | 73 | // Returns the GrVkImage of the non-msaa attachment. If the color attachment has 1 sample, |
Greg Daniel | 00d6cf4 | 2021-03-05 22:50:08 +0000 | [diff] [blame] | 74 | // then the color attachment will be returned. Otherwise, the resolve attachment is returned. |
| 75 | // Note that in this second case the resolve attachment may be null if this was created by |
| 76 | // wrapping an msaa VkImage. |
Greg Daniel | 2bc96d6 | 2021-09-13 13:08:02 -0400 | [diff] [blame^] | 77 | GrVkImage* nonMSAAAttachment() const; |
Greg Daniel | 00d6cf4 | 2021-03-05 22:50:08 +0000 | [diff] [blame] | 78 | |
| 79 | // Returns the attachment that is used for all external client facing operations. This will be |
| 80 | // either a wrapped color attachment or the resolve attachment for created VkImages. |
Greg Daniel | 2bc96d6 | 2021-09-13 13:08:02 -0400 | [diff] [blame^] | 81 | GrVkImage* externalAttachment() const { |
Greg Daniel | 00d6cf4 | 2021-03-05 22:50:08 +0000 | [diff] [blame] | 82 | return fResolveAttachment ? fResolveAttachment.get() : fColorAttachment.get(); |
| 83 | } |
Greg Daniel | e2893f2 | 2020-10-13 10:20:06 -0400 | [diff] [blame] | 84 | |
Greg Daniel | 805c622 | 2021-04-20 12:44:48 -0400 | [diff] [blame] | 85 | const GrVkRenderPass* getSimpleRenderPass( |
Greg Daniel | a92d787 | 2021-04-07 11:08:36 -0400 | [diff] [blame] | 86 | bool withResolve, |
| 87 | bool withStencil, |
| 88 | SelfDependencyFlags selfDepFlags, |
| 89 | LoadFromResolve); |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 90 | GrVkResourceProvider::CompatibleRPHandle compatibleRenderPassHandle( |
| 91 | bool withResolve, |
| 92 | bool withStencil, |
| 93 | SelfDependencyFlags selfDepFlags, |
| 94 | LoadFromResolve); |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 95 | |
Greg Daniel | de4bbdb | 2021-04-13 14:23:23 -0400 | [diff] [blame] | 96 | bool wrapsSecondaryCommandBuffer() const { return SkToBool(fExternalFramebuffer); } |
Greg Daniel | 60ec617 | 2021-04-16 11:31:58 -0400 | [diff] [blame] | 97 | sk_sp<GrVkFramebuffer> externalFramebuffer() const; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 98 | |
Greg Daniel | 1e16937 | 2021-08-24 15:44:15 -0400 | [diff] [blame] | 99 | bool canAttemptStencilAttachment(bool useMSAASurface) const override; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 100 | |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 101 | GrBackendRenderTarget getBackendRenderTarget() const override; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 102 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 103 | void getAttachmentsDescriptor(GrVkRenderPass::AttachmentsDescriptor* desc, |
Robert Phillips | 96f2237 | 2020-05-20 12:31:18 -0400 | [diff] [blame] | 104 | GrVkRenderPass::AttachmentFlags* flags, |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 105 | bool withResolve, |
Greg Daniel | 60ec617 | 2021-04-16 11:31:58 -0400 | [diff] [blame] | 106 | bool withStencil); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 107 | |
Robert Phillips | 24e2f6e | 2020-06-26 08:30:07 -0400 | [diff] [blame] | 108 | // Reconstruct the render target attachment information from the programInfo. This includes |
| 109 | // which attachments the render target will have (color, stencil) and the attachments' formats |
| 110 | // and sample counts - cf. getAttachmentsDescriptor. |
| 111 | static void ReconstructAttachmentsDescriptor(const GrVkCaps& vkCaps, |
| 112 | const GrProgramInfo& programInfo, |
| 113 | GrVkRenderPass::AttachmentsDescriptor* desc, |
| 114 | GrVkRenderPass::AttachmentFlags* flags); |
| 115 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 116 | protected: |
Greg Daniel | 00d6cf4 | 2021-03-05 22:50:08 +0000 | [diff] [blame] | 117 | enum class CreateType { |
| 118 | kDirectlyWrapped, // We need to register this in the ctor |
| 119 | kFromTextureRT, // Skip registering this to cache since TexRT will handle it |
| 120 | }; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 121 | |
| 122 | GrVkRenderTarget(GrVkGpu* gpu, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 123 | SkISize dimensions, |
Greg Daniel | 2bc96d6 | 2021-09-13 13:08:02 -0400 | [diff] [blame^] | 124 | sk_sp<GrVkImage> colorAttachment, |
| 125 | sk_sp<GrVkImage> resolveImage, |
Greg Daniel | 00d6cf4 | 2021-03-05 22:50:08 +0000 | [diff] [blame] | 126 | CreateType createType); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 127 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 128 | void onAbandon() override; |
| 129 | void onRelease() override; |
| 130 | |
Greg Daniel | 00d6cf4 | 2021-03-05 22:50:08 +0000 | [diff] [blame] | 131 | // This returns zero since the memory should all be handled by the attachments |
| 132 | size_t onGpuMemorySize() const override { return 0; } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 133 | |
| 134 | private: |
Greg Daniel | de4bbdb | 2021-04-13 14:23:23 -0400 | [diff] [blame] | 135 | // For external framebuffers that wrap a secondary command buffer |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 136 | GrVkRenderTarget(GrVkGpu* gpu, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 137 | SkISize dimensions, |
Greg Daniel | de4bbdb | 2021-04-13 14:23:23 -0400 | [diff] [blame] | 138 | sk_sp<GrVkFramebuffer> externalFramebuffer); |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 139 | |
Greg Daniel | 00d6cf4 | 2021-03-05 22:50:08 +0000 | [diff] [blame] | 140 | void setFlags(); |
Greg Daniel | 638b2e8 | 2020-08-27 14:29:00 -0400 | [diff] [blame] | 141 | |
Greg Daniel | fa3adf7 | 2019-11-07 09:53:41 -0500 | [diff] [blame] | 142 | GrVkGpu* getVkGpu() const; |
| 143 | |
Greg Daniel | 2bc96d6 | 2021-09-13 13:08:02 -0400 | [diff] [blame^] | 144 | GrVkImage* dynamicMSAAAttachment(); |
| 145 | GrVkImage* msaaAttachment(); |
Greg Daniel | 60ec617 | 2021-04-16 11:31:58 -0400 | [diff] [blame] | 146 | |
Greg Daniel | 805c622 | 2021-04-20 12:44:48 -0400 | [diff] [blame] | 147 | std::pair<const GrVkRenderPass*, GrVkResourceProvider::CompatibleRPHandle> |
| 148 | createSimpleRenderPass(bool withResolve, |
| 149 | bool withStencil, |
| 150 | SelfDependencyFlags selfDepFlags, |
| 151 | LoadFromResolve); |
| 152 | void createFramebuffer(bool withResolve, |
| 153 | bool withStencil, |
| 154 | SelfDependencyFlags selfDepFlags, |
| 155 | LoadFromResolve); |
Greg Daniel | fa3adf7 | 2019-11-07 09:53:41 -0500 | [diff] [blame] | 156 | |
Chris Dalton | e0fe23a | 2021-04-23 13:11:44 -0600 | [diff] [blame] | 157 | bool completeStencilAttachment(GrAttachment* stencil, bool useMSAASurface) override; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 158 | |
Greg Daniel | 2d35a1c | 2019-02-01 14:48:10 -0500 | [diff] [blame] | 159 | // In Vulkan we call the release proc after we are finished with the underlying |
| 160 | // GrVkImage::Resource object (which occurs after the GPU has finished all work on it). |
Brian Salomon | b2c5dae | 2019-03-04 10:25:17 -0500 | [diff] [blame] | 161 | void onSetRelease(sk_sp<GrRefCntedCallback> releaseHelper) override { |
Greg Daniel | 00d6cf4 | 2021-03-05 22:50:08 +0000 | [diff] [blame] | 162 | // Forward the release proc on to the GrVkImage of the release attachment if we have one, |
| 163 | // otherwise the color attachment. |
Greg Daniel | 2bc96d6 | 2021-09-13 13:08:02 -0400 | [diff] [blame^] | 164 | GrVkImage* attachment = |
Greg Daniel | 00d6cf4 | 2021-03-05 22:50:08 +0000 | [diff] [blame] | 165 | fResolveAttachment ? fResolveAttachment.get() : fColorAttachment.get(); |
| 166 | attachment->setResourceRelease(std::move(releaseHelper)); |
Greg Daniel | 2d35a1c | 2019-02-01 14:48:10 -0500 | [diff] [blame] | 167 | } |
| 168 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 169 | void releaseInternalObjects(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 170 | |
Greg Daniel | 2bc96d6 | 2021-09-13 13:08:02 -0400 | [diff] [blame^] | 171 | sk_sp<GrVkImage> fColorAttachment; |
| 172 | sk_sp<GrVkImage> fResolveAttachment; |
| 173 | sk_sp<GrVkImage> fDynamicMSAAAttachment; |
Greg Daniel | fa3adf7 | 2019-11-07 09:53:41 -0500 | [diff] [blame] | 174 | |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 175 | // We can have a renderpass with and without resolve attachment, stencil attachment, |
| 176 | // input attachment dependency, advanced blend dependency, and loading from resolve. All 5 of |
Greg Daniel | 805c622 | 2021-04-20 12:44:48 -0400 | [diff] [blame] | 177 | // these being completely orthogonal. Thus we have a total of 32 types of render passes. We then |
| 178 | // cache a framebuffer for each type of these render passes. |
| 179 | static constexpr int kNumCachedFramebuffers = 32; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 180 | |
Greg Daniel | 805c622 | 2021-04-20 12:44:48 -0400 | [diff] [blame] | 181 | sk_sp<const GrVkFramebuffer> fCachedFramebuffers[kNumCachedFramebuffers]; |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 182 | |
Greg Daniel | 37fd658 | 2020-09-14 12:36:09 -0400 | [diff] [blame] | 183 | const GrVkDescriptorSet* fCachedInputDescriptorSet = nullptr; |
| 184 | |
Greg Daniel | de4bbdb | 2021-04-13 14:23:23 -0400 | [diff] [blame] | 185 | sk_sp<GrVkFramebuffer> fExternalFramebuffer; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | #endif |