blob: f0bc6c66c856683b1ecfff1a69dac20cbe4b5a70 [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
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 Salomon201cdbb2019-08-14 17:00:30 -040012#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/vk/GrVkImage.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050014
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/gpu/vk/GrVkTypes.h"
16#include "src/gpu/vk/GrVkRenderPass.h"
17#include "src/gpu/vk/GrVkResourceProvider.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050018
Greg Daniel164a9f02016-02-22 09:56:40 -050019class GrVkFramebuffer;
20class GrVkGpu;
21class GrVkImageView;
Greg Daniel164a9f02016-02-22 09:56:40 -050022
egdanielb2df0c22016-05-13 11:30:37 -070023struct GrVkImageInfo;
jvanverthfd359ca2016-03-18 11:57:24 -070024
Greg Danielde4bbdb2021-04-13 14:23:23 -040025class GrVkRenderTarget : public GrRenderTarget {
Greg Daniel164a9f02016-02-22 09:56:40 -050026public:
Greg Danielde4bbdb2021-04-13 14:23:23 -040027 static sk_sp<GrVkRenderTarget> MakeWrappedRenderTarget(GrVkGpu*,
28 SkISize,
29 int sampleCnt,
Brian Salomona56a7462020-02-07 14:17:25 -050030 const GrVkImageInfo&,
Greg Daniel6c6caf42020-05-29 12:11:05 -040031 sk_sp<GrBackendSurfaceMutableStateImpl>);
Greg Daniel164a9f02016-02-22 09:56:40 -050032
Greg Danielde4bbdb2021-04-13 14:23:23 -040033 static sk_sp<GrVkRenderTarget> MakeSecondaryCBRenderTarget(GrVkGpu*,
34 SkISize,
Greg Danielb46add82019-01-02 14:51:29 -050035 const GrVkDrawableInfo& vkInfo);
36
Greg Daniel164a9f02016-02-22 09:56:40 -050037 ~GrVkRenderTarget() override;
38
Greg Danielde4bbdb2021-04-13 14:23:23 -040039 GrBackendFormat backendFormat() const override;
Greg Daniel4065d452018-11-16 15:43:41 -050040
Greg Daniel21774362020-09-14 10:36:43 -040041 using SelfDependencyFlags = GrVkRenderPass::SelfDependencyFlags;
Greg Daniel7acddf52020-12-16 15:15:51 -050042 using LoadFromResolve = GrVkRenderPass::LoadFromResolve;
Greg Daniel21774362020-09-14 10:36:43 -040043
Greg Daniel7acddf52020-12-16 15:15:51 -050044 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 Daniele2893f22020-10-13 10:20:06 -040054
Greg Daniel2bc96d62021-09-13 13:08:02 -040055 GrVkImage* colorAttachment() const {
Greg Danielde4bbdb2021-04-13 14:23:23 -040056 SkASSERT(!this->wrapsSecondaryCommandBuffer());
57 return fColorAttachment.get();
58 }
59 const GrVkImageView* colorAttachmentView() const {
60 SkASSERT(!this->wrapsSecondaryCommandBuffer());
Greg Daniel60ec6172021-04-16 11:31:58 -040061 return this->colorAttachment()->framebufferView();
Greg Danielde4bbdb2021-04-13 14:23:23 -040062 }
Greg Daniele2893f22020-10-13 10:20:06 -040063
Greg Daniel2bc96d62021-09-13 13:08:02 -040064 GrVkImage* resolveAttachment() const {
Greg Danielde4bbdb2021-04-13 14:23:23 -040065 SkASSERT(!this->wrapsSecondaryCommandBuffer());
66 return fResolveAttachment.get();
67 }
Greg Daniel00d6cf42021-03-05 22:50:08 +000068 const GrVkImageView* resolveAttachmentView() const {
Greg Danielde4bbdb2021-04-13 14:23:23 -040069 SkASSERT(!this->wrapsSecondaryCommandBuffer());
Greg Daniel00d6cf42021-03-05 22:50:08 +000070 return fResolveAttachment->framebufferView();
71 }
Greg Daniele2893f22020-10-13 10:20:06 -040072
Greg Daniel2bc96d62021-09-13 13:08:02 -040073 // Returns the GrVkImage of the non-msaa attachment. If the color attachment has 1 sample,
Greg Daniel00d6cf42021-03-05 22:50:08 +000074 // 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 Daniel2bc96d62021-09-13 13:08:02 -040077 GrVkImage* nonMSAAAttachment() const;
Greg Daniel00d6cf42021-03-05 22:50:08 +000078
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 Daniel2bc96d62021-09-13 13:08:02 -040081 GrVkImage* externalAttachment() const {
Greg Daniel00d6cf42021-03-05 22:50:08 +000082 return fResolveAttachment ? fResolveAttachment.get() : fColorAttachment.get();
83 }
Greg Daniele2893f22020-10-13 10:20:06 -040084
Greg Daniel805c6222021-04-20 12:44:48 -040085 const GrVkRenderPass* getSimpleRenderPass(
Greg Daniela92d7872021-04-07 11:08:36 -040086 bool withResolve,
87 bool withStencil,
88 SelfDependencyFlags selfDepFlags,
89 LoadFromResolve);
Greg Daniel7acddf52020-12-16 15:15:51 -050090 GrVkResourceProvider::CompatibleRPHandle compatibleRenderPassHandle(
91 bool withResolve,
92 bool withStencil,
93 SelfDependencyFlags selfDepFlags,
94 LoadFromResolve);
Greg Danielb46add82019-01-02 14:51:29 -050095
Greg Danielde4bbdb2021-04-13 14:23:23 -040096 bool wrapsSecondaryCommandBuffer() const { return SkToBool(fExternalFramebuffer); }
Greg Daniel60ec6172021-04-16 11:31:58 -040097 sk_sp<GrVkFramebuffer> externalFramebuffer() const;
Greg Daniel164a9f02016-02-22 09:56:40 -050098
Greg Daniel1e169372021-08-24 15:44:15 -040099 bool canAttemptStencilAttachment(bool useMSAASurface) const override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500100
Robert Phillipsb67821d2017-12-13 15:00:45 -0500101 GrBackendRenderTarget getBackendRenderTarget() const override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500102
Greg Daniel164a9f02016-02-22 09:56:40 -0500103 void getAttachmentsDescriptor(GrVkRenderPass::AttachmentsDescriptor* desc,
Robert Phillips96f22372020-05-20 12:31:18 -0400104 GrVkRenderPass::AttachmentFlags* flags,
Greg Daniel7acddf52020-12-16 15:15:51 -0500105 bool withResolve,
Greg Daniel60ec6172021-04-16 11:31:58 -0400106 bool withStencil);
halcanary9d524f22016-03-29 09:03:52 -0700107
Robert Phillips24e2f6e2020-06-26 08:30:07 -0400108 // 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 Daniel164a9f02016-02-22 09:56:40 -0500116protected:
Greg Daniel00d6cf42021-03-05 22:50:08 +0000117 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 Daniel164a9f02016-02-22 09:56:40 -0500121
122 GrVkRenderTarget(GrVkGpu* gpu,
Brian Salomona56a7462020-02-07 14:17:25 -0500123 SkISize dimensions,
Greg Daniel2bc96d62021-09-13 13:08:02 -0400124 sk_sp<GrVkImage> colorAttachment,
125 sk_sp<GrVkImage> resolveImage,
Greg Daniel00d6cf42021-03-05 22:50:08 +0000126 CreateType createType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500127
Greg Daniel164a9f02016-02-22 09:56:40 -0500128 void onAbandon() override;
129 void onRelease() override;
130
Greg Daniel00d6cf42021-03-05 22:50:08 +0000131 // This returns zero since the memory should all be handled by the attachments
132 size_t onGpuMemorySize() const override { return 0; }
Greg Daniel164a9f02016-02-22 09:56:40 -0500133
134private:
Greg Danielde4bbdb2021-04-13 14:23:23 -0400135 // For external framebuffers that wrap a secondary command buffer
egdanielb2df0c22016-05-13 11:30:37 -0700136 GrVkRenderTarget(GrVkGpu* gpu,
Brian Salomona56a7462020-02-07 14:17:25 -0500137 SkISize dimensions,
Greg Danielde4bbdb2021-04-13 14:23:23 -0400138 sk_sp<GrVkFramebuffer> externalFramebuffer);
Greg Danielb46add82019-01-02 14:51:29 -0500139
Greg Daniel00d6cf42021-03-05 22:50:08 +0000140 void setFlags();
Greg Daniel638b2e82020-08-27 14:29:00 -0400141
Greg Danielfa3adf72019-11-07 09:53:41 -0500142 GrVkGpu* getVkGpu() const;
143
Greg Daniel2bc96d62021-09-13 13:08:02 -0400144 GrVkImage* dynamicMSAAAttachment();
145 GrVkImage* msaaAttachment();
Greg Daniel60ec6172021-04-16 11:31:58 -0400146
Greg Daniel805c6222021-04-20 12:44:48 -0400147 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 Danielfa3adf72019-11-07 09:53:41 -0500156
Chris Daltone0fe23a2021-04-23 13:11:44 -0600157 bool completeStencilAttachment(GrAttachment* stencil, bool useMSAASurface) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500158
Greg Daniel2d35a1c2019-02-01 14:48:10 -0500159 // 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 Salomonb2c5dae2019-03-04 10:25:17 -0500161 void onSetRelease(sk_sp<GrRefCntedCallback> releaseHelper) override {
Greg Daniel00d6cf42021-03-05 22:50:08 +0000162 // Forward the release proc on to the GrVkImage of the release attachment if we have one,
163 // otherwise the color attachment.
Greg Daniel2bc96d62021-09-13 13:08:02 -0400164 GrVkImage* attachment =
Greg Daniel00d6cf42021-03-05 22:50:08 +0000165 fResolveAttachment ? fResolveAttachment.get() : fColorAttachment.get();
166 attachment->setResourceRelease(std::move(releaseHelper));
Greg Daniel2d35a1c2019-02-01 14:48:10 -0500167 }
168
Greg Daniel164a9f02016-02-22 09:56:40 -0500169 void releaseInternalObjects();
Greg Daniel164a9f02016-02-22 09:56:40 -0500170
Greg Daniel2bc96d62021-09-13 13:08:02 -0400171 sk_sp<GrVkImage> fColorAttachment;
172 sk_sp<GrVkImage> fResolveAttachment;
173 sk_sp<GrVkImage> fDynamicMSAAAttachment;
Greg Danielfa3adf72019-11-07 09:53:41 -0500174
Greg Daniel7acddf52020-12-16 15:15:51 -0500175 // 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 Daniel805c6222021-04-20 12:44:48 -0400177 // 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;
halcanary9d524f22016-03-29 09:03:52 -0700180
Greg Daniel805c6222021-04-20 12:44:48 -0400181 sk_sp<const GrVkFramebuffer> fCachedFramebuffers[kNumCachedFramebuffers];
Greg Danielb46add82019-01-02 14:51:29 -0500182
Greg Daniel37fd6582020-09-14 12:36:09 -0400183 const GrVkDescriptorSet* fCachedInputDescriptorSet = nullptr;
184
Greg Danielde4bbdb2021-04-13 14:23:23 -0400185 sk_sp<GrVkFramebuffer> fExternalFramebuffer;
Greg Daniel164a9f02016-02-22 09:56:40 -0500186};
187
188#endif