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