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 #ifndef GrVkRenderPass_DEFINED
9 #define GrVkRenderPass_DEFINED
10 
11 #include "GrTypes.h"
12 #include "GrVkResource.h"
13 #include "vk/GrVkTypes.h"
14 
15 class GrProcessorKeyBuilder;
16 class GrVkGpu;
17 class GrVkRenderTarget;
18 
19 class GrVkRenderPass : public GrVkResource {
20 public:
GrVkRenderPass()21     GrVkRenderPass() : INHERITED(), fRenderPass(VK_NULL_HANDLE), fClearValueCount(0) {}
22 
23     // Used when importing an external render pass. In this case we have to explicitly be told the
24     // color attachment index
GrVkRenderPass(VkRenderPass renderPass,uint32_t colorAttachmentIndex)25     explicit GrVkRenderPass(VkRenderPass renderPass, uint32_t colorAttachmentIndex)
26             : INHERITED()
27             , fRenderPass(renderPass)
28             , fAttachmentFlags(kExternal_AttachmentFlag)
29             , fClearValueCount(0)
30             , fColorAttachmentIndex(colorAttachmentIndex) {}
31 
32     struct LoadStoreOps {
33         VkAttachmentLoadOp  fLoadOp;
34         VkAttachmentStoreOp fStoreOp;
35 
LoadStoreOpsLoadStoreOps36         LoadStoreOps(VkAttachmentLoadOp loadOp, VkAttachmentStoreOp storeOp)
37             : fLoadOp(loadOp)
38             , fStoreOp(storeOp) {}
39 
40         bool operator==(const LoadStoreOps& right) const {
41             return fLoadOp == right.fLoadOp && fStoreOp == right.fStoreOp;
42         }
43 
44         bool operator!=(const LoadStoreOps& right) const {
45             return !(*this == right);
46         }
47     };
48 
49     void initSimple(const GrVkGpu* gpu, const GrVkRenderTarget& target);
50     void init(const GrVkGpu* gpu,
51               const GrVkRenderTarget& target,
52               const LoadStoreOps& colorOp,
53               const LoadStoreOps& stencilOp);
54 
55     void init(const GrVkGpu* gpu,
56               const GrVkRenderPass& compatibleRenderPass,
57               const LoadStoreOps& colorOp,
58               const LoadStoreOps& stencilOp);
59 
60     struct AttachmentsDescriptor {
61         struct AttachmentDesc {
62             VkFormat fFormat;
63             int fSamples;
64             LoadStoreOps fLoadStoreOps;
65 
AttachmentDescAttachmentsDescriptor::AttachmentDesc66             AttachmentDesc()
67                 : fFormat(VK_FORMAT_UNDEFINED)
68                 , fSamples(0)
69                 , fLoadStoreOps(VK_ATTACHMENT_LOAD_OP_LOAD, VK_ATTACHMENT_STORE_OP_STORE) {}
70             bool operator==(const AttachmentDesc& right) const {
71                 return (fFormat == right.fFormat &&
72                         fSamples == right.fSamples &&
73                         fLoadStoreOps == right.fLoadStoreOps);
74             }
75             bool operator!=(const AttachmentDesc& right) const {
76                 return !(*this == right);
77             }
isCompatibleAttachmentsDescriptor::AttachmentDesc78             bool isCompatible(const AttachmentDesc& desc) const {
79                 return (fFormat == desc.fFormat && fSamples == desc.fSamples);
80             }
81         };
82         AttachmentDesc fColor;
83         AttachmentDesc fStencil;
84         uint32_t       fAttachmentCount;
85     };
86 
87     enum AttachmentFlags {
88         kColor_AttachmentFlag = 0x1,
89         kStencil_AttachmentFlag = 0x2,
90         // The external attachment flag signals that this render pass is imported from an external
91         // client. Since we don't know every attachment on the render pass we don't set any of the
92         // specific attachment flags when using external. However, the external render pass must
93         // at least have a color attachment.
94         kExternal_AttachmentFlag = 0x4,
95     };
96     GR_DECL_BITFIELD_OPS_FRIENDS(AttachmentFlags);
97 
98     // The following return the index of the render pass attachment array for the given attachment.
99     // If the render pass does not have the given attachment it will return false and not set the
100     // index value.
101     bool colorAttachmentIndex(uint32_t* index) const;
102     bool stencilAttachmentIndex(uint32_t* index) const;
103 
104     // Returns whether or not the structure of a RenderTarget matches that of the VkRenderPass in
105     // this object. Specifically this compares that the number of attachments, format of
106     // attachments, and sample counts are all the same. This function is used in the creation of
107     // basic RenderPasses that can be used when creating a VkFrameBuffer object.
108     bool isCompatible(const GrVkRenderTarget& target) const;
109 
110     bool isCompatible(const GrVkRenderPass& renderPass) const;
111 
112     bool isCompatibleExternalRP(VkRenderPass) const;
113 
114     bool equalLoadStoreOps(const LoadStoreOps& colorOps,
115                            const LoadStoreOps& stencilOps) const;
116 
vkRenderPass()117     VkRenderPass vkRenderPass() const { return fRenderPass; }
118 
granularity()119     const VkExtent2D& granularity() const { return fGranularity; }
120 
121     // Returns the number of clear colors needed to begin this render pass. Currently this will
122     // either only be 0 or 1 since we only ever clear the color attachment.
clearValueCount()123     uint32_t clearValueCount() const { return fClearValueCount; }
124 
125 
126     void genKey(GrProcessorKeyBuilder* b) const;
127 
128 #ifdef SK_TRACE_VK_RESOURCES
dumpInfo()129     void dumpInfo() const override {
130         SkDebugf("GrVkRenderPass: %d (%d refs)\n", fRenderPass, this->getRefCnt());
131     }
132 #endif
133 
134 private:
135     GrVkRenderPass(const GrVkRenderPass&);
136 
137     void init(const GrVkGpu* gpu,
138               const LoadStoreOps& colorOps,
139               const LoadStoreOps& stencilOps);
140 
141     bool isCompatible(const AttachmentsDescriptor&, const AttachmentFlags&) const;
142 
143     void freeGPUData(GrVkGpu* gpu) const override;
144 
145     VkRenderPass          fRenderPass;
146     AttachmentFlags       fAttachmentFlags;
147     AttachmentsDescriptor fAttachmentsDescriptor;
148     VkExtent2D            fGranularity;
149     uint32_t              fClearValueCount;
150     // For internally created render passes we assume the color attachment index is always 0.
151     uint32_t              fColorAttachmentIndex = 0;
152 
153     typedef GrVkResource INHERITED;
154 };
155 
156 GR_MAKE_BITFIELD_OPS(GrVkRenderPass::AttachmentFlags);
157 
158 #endif
159