egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 8 | #ifndef GrOpsRenderPass_DEFINED |
| 9 | #define GrOpsRenderPass_DEFINED |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkDrawable.h" |
| 12 | #include "src/gpu/GrPipeline.h" |
| 13 | #include "src/gpu/ops/GrDrawOp.h" |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 14 | |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 15 | class GrOpFlushState; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 16 | class GrGpu; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 17 | class GrPipeline; |
| 18 | class GrPrimitiveProcessor; |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 19 | class GrProgramInfo; |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 20 | class GrRenderTarget; |
Michael Ludwig | 1e63279 | 2020-05-21 12:45:31 -0400 | [diff] [blame] | 21 | class GrScissorState; |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 22 | class GrSemaphore; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 23 | struct SkIRect; |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 24 | struct SkRect; |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 25 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 26 | /** |
| 27 | * The GrOpsRenderPass is a series of commands (draws, clears, and discards), which all target the |
| 28 | * same render target. It is possible that these commands execute immediately (GL), or get buffered |
| 29 | * up for later execution (Vulkan). GrOps execute into a GrOpsRenderPass. |
| 30 | */ |
| 31 | class GrOpsRenderPass { |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 32 | public: |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 33 | virtual ~GrOpsRenderPass() {} |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 34 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 35 | struct LoadAndStoreInfo { |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 36 | GrLoadOp fLoadOp; |
| 37 | GrStoreOp fStoreOp; |
| 38 | SkPMColor4f fClearColor; |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 41 | // Load-time clears of the stencil buffer are always to 0 so we don't store |
| 42 | // an 'fStencilClearValue' |
| 43 | struct StencilLoadAndStoreInfo { |
Robert Phillips | 6b47c7d | 2017-08-29 07:24:09 -0400 | [diff] [blame] | 44 | GrLoadOp fLoadOp; |
| 45 | GrStoreOp fStoreOp; |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 46 | }; |
| 47 | |
Chris Dalton | 2c3e169 | 2020-03-20 12:32:55 -0600 | [diff] [blame] | 48 | void begin(); |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 49 | // Signals the end of recording to the GrOpsRenderPass and that it can now be submitted. |
Chris Dalton | 2c3e169 | 2020-03-20 12:32:55 -0600 | [diff] [blame] | 50 | void end(); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 51 | |
Chris Dalton | aa0e45c | 2020-03-16 10:05:11 -0600 | [diff] [blame] | 52 | // Updates the internal pipeline state for drawing with the provided GrProgramInfo. Enters an |
| 53 | // internal "bad" state if the pipeline could not be set. |
| 54 | void bindPipeline(const GrProgramInfo&, const SkRect& drawBounds); |
Chris Dalton | 4386ad1 | 2020-02-19 16:42:06 -0700 | [diff] [blame] | 55 | |
Chris Dalton | 2e7ed26 | 2020-02-21 15:17:59 -0700 | [diff] [blame] | 56 | // The scissor rect is always dynamic state and therefore not stored on GrPipeline. If scissor |
| 57 | // test is enabled on the current pipeline, then the client must call setScissorRect() before |
| 58 | // drawing. The scissor rect may also be updated between draws without having to bind a new |
| 59 | // pipeline. |
| 60 | void setScissorRect(const SkIRect&); |
| 61 | |
Chris Dalton | db20afc | 2020-03-05 12:13:53 -0700 | [diff] [blame] | 62 | // Binds textures for the primitive processor and any FP on the GrPipeline. Texture bindings are |
| 63 | // dynamic state and therefore not set during bindPipeline(). If the current program uses |
| 64 | // textures, then the client must call bindTextures() before drawing. The primitive processor |
| 65 | // textures may also be updated between draws by calling bindTextures() again with a different |
| 66 | // array for primProcTextures. (On subsequent calls, if the backend is capable of updating the |
| 67 | // primitive processor textures independently, then it will automatically skip re-binding |
| 68 | // FP textures from GrPipeline.) |
Chris Dalton | ded4370 | 2020-03-02 11:45:27 -0700 | [diff] [blame] | 69 | // |
| 70 | // If the current program does not use textures, this is a no-op. |
Chris Dalton | db20afc | 2020-03-05 12:13:53 -0700 | [diff] [blame] | 71 | void bindTextures(const GrPrimitiveProcessor&, const GrSurfaceProxy* const primProcTextures[], |
| 72 | const GrPipeline&); |
Chris Dalton | 2e7ed26 | 2020-02-21 15:17:59 -0700 | [diff] [blame] | 73 | |
Greg Daniel | 426274b | 2020-07-20 11:37:38 -0400 | [diff] [blame] | 74 | void bindBuffers(sk_sp<const GrBuffer> indexBuffer, sk_sp<const GrBuffer> instanceBuffer, |
| 75 | sk_sp<const GrBuffer> vertexBuffer, GrPrimitiveRestart = GrPrimitiveRestart::kNo); |
Chris Dalton | ded4370 | 2020-03-02 11:45:27 -0700 | [diff] [blame] | 76 | |
Chris Dalton | 03fdf6a | 2020-04-07 12:31:59 -0600 | [diff] [blame] | 77 | // The next several draw*() methods issue draws using the current pipeline state. Before |
| 78 | // drawing, the caller must configure the pipeline and dynamic state: |
Chris Dalton | ded4370 | 2020-03-02 11:45:27 -0700 | [diff] [blame] | 79 | // |
| 80 | // - Call bindPipeline() |
| 81 | // - If the scissor test is enabled, call setScissorRect() |
| 82 | // - If the current program uses textures, call bindTextures() |
| 83 | // - Call bindBuffers() (even if all buffers are null) |
| 84 | void draw(int vertexCount, int baseVertex); |
| 85 | void drawIndexed(int indexCount, int baseIndex, uint16_t minIndexValue, uint16_t maxIndexValue, |
Chris Dalton | 33db9fc | 2020-02-25 18:52:12 -0700 | [diff] [blame] | 86 | int baseVertex); |
Chris Dalton | 03fdf6a | 2020-04-07 12:31:59 -0600 | [diff] [blame] | 87 | |
| 88 | // Requires caps.drawInstancedSupport(). |
Chris Dalton | ded4370 | 2020-03-02 11:45:27 -0700 | [diff] [blame] | 89 | void drawInstanced(int instanceCount, int baseInstance, int vertexCount, int baseVertex); |
Chris Dalton | 03fdf6a | 2020-04-07 12:31:59 -0600 | [diff] [blame] | 90 | |
| 91 | // Requires caps.drawInstancedSupport(). |
Chris Dalton | ded4370 | 2020-03-02 11:45:27 -0700 | [diff] [blame] | 92 | void drawIndexedInstanced(int indexCount, int baseIndex, int instanceCount, int baseInstance, |
| 93 | int baseVertex); |
Chris Dalton | 2e7ed26 | 2020-02-21 15:17:59 -0700 | [diff] [blame] | 94 | |
Chris Dalton | 03fdf6a | 2020-04-07 12:31:59 -0600 | [diff] [blame] | 95 | // Executes multiple draws from an array of GrDrawIndirectCommand in the provided buffer. |
| 96 | // |
| 97 | // Requires caps.drawInstancedSupport(). |
| 98 | // |
| 99 | // If caps.nativeDrawIndirectSupport() is unavailable, then 'drawIndirectBuffer' must be a |
| 100 | // GrCpuBuffer in order to polyfill. Performance may suffer in this scenario. |
| 101 | void drawIndirect(const GrBuffer* drawIndirectBuffer, size_t bufferOffset, int drawCount); |
| 102 | |
| 103 | // Executes multiple draws from an array of GrDrawIndexedIndirectCommand in the provided buffer. |
| 104 | // |
| 105 | // Requires caps.drawInstancedSupport(). |
| 106 | // |
| 107 | // If caps.nativeDrawIndirectSupport() is unavailable, then 'drawIndirectBuffer' must be a |
| 108 | // GrCpuBuffer in order to polyfill. Performance may suffer in this scenario. |
| 109 | void drawIndexedIndirect(const GrBuffer* drawIndirectBuffer, size_t bufferOffset, |
| 110 | int drawCount); |
| 111 | |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 112 | // This is a helper method for drawing a repeating pattern of vertices. The bound index buffer |
| 113 | // is understood to contain 'maxPatternRepetitionsInIndexBuffer' repetitions of the pattern. |
| 114 | // If more repetitions are required, then we loop. |
| 115 | void drawIndexPattern(int patternIndexCount, int patternRepeatCount, |
| 116 | int maxPatternRepetitionsInIndexBuffer, int patternVertexCount, |
| 117 | int baseVertex); |
| 118 | |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 119 | // Performs an upload of vertex data in the middle of a set of a set of draws |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 120 | virtual void inlineUpload(GrOpFlushState*, GrDeferredTextureUploadFn&) = 0; |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 121 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 122 | /** |
Michael Ludwig | 1e63279 | 2020-05-21 12:45:31 -0400 | [diff] [blame] | 123 | * Clear the owned render target. Clears the full target if 'scissor' is disabled, otherwise it |
| 124 | * is restricted to 'scissor'. Must check caps.performPartialClearsAsDraws() before using an |
| 125 | * enabled scissor test; must check caps.performColorClearsAsDraws() before using this at all. |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 126 | */ |
Michael Ludwig | 1e63279 | 2020-05-21 12:45:31 -0400 | [diff] [blame] | 127 | void clear(const GrScissorState& scissor, const SkPMColor4f&); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 128 | |
Michael Ludwig | 1e63279 | 2020-05-21 12:45:31 -0400 | [diff] [blame] | 129 | /** |
| 130 | * Same as clear() but modifies the stencil; check caps.performStencilClearsAsDraws() and |
| 131 | * caps.performPartialClearsAsDraws(). |
| 132 | */ |
| 133 | void clearStencilClip(const GrScissorState& scissor, bool insideStencilMask); |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 134 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 135 | /** |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 136 | * Executes the SkDrawable object for the underlying backend. |
| 137 | */ |
Chris Dalton | 4386ad1 | 2020-02-19 16:42:06 -0700 | [diff] [blame] | 138 | void executeDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler>); |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 139 | |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 140 | protected: |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 141 | GrOpsRenderPass() : fOrigin(kTopLeft_GrSurfaceOrigin), fRenderTarget(nullptr) {} |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 142 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 143 | GrOpsRenderPass(GrRenderTarget* rt, GrSurfaceOrigin origin) |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 144 | : fOrigin(origin) |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 145 | , fRenderTarget(rt) { |
| 146 | } |
| 147 | |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 148 | void set(GrRenderTarget* rt, GrSurfaceOrigin origin) { |
| 149 | SkASSERT(!fRenderTarget); |
| 150 | |
| 151 | fRenderTarget = rt; |
| 152 | fOrigin = origin; |
| 153 | } |
| 154 | |
| 155 | GrSurfaceOrigin fOrigin; |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 156 | GrRenderTarget* fRenderTarget; |
Robert Phillips | 65a88fa | 2017-08-08 08:36:22 -0400 | [diff] [blame] | 157 | |
Chris Dalton | 2c3e169 | 2020-03-20 12:32:55 -0600 | [diff] [blame] | 158 | // Backends may defer binding of certain buffers if their draw API requires a buffer, or if |
| 159 | // their bind methods don't support base values. |
| 160 | sk_sp<const GrBuffer> fActiveIndexBuffer; |
| 161 | sk_sp<const GrBuffer> fActiveVertexBuffer; |
| 162 | sk_sp<const GrBuffer> fActiveInstanceBuffer; |
| 163 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 164 | private: |
| 165 | virtual GrGpu* gpu() = 0; |
Greg Daniel | 65a0927 | 2016-10-12 09:47:22 -0400 | [diff] [blame] | 166 | |
Chris Dalton | b60c8a2 | 2020-03-31 17:24:22 -0600 | [diff] [blame] | 167 | void resetActiveBuffers() { |
| 168 | fActiveIndexBuffer.reset(); |
| 169 | fActiveInstanceBuffer.reset(); |
| 170 | fActiveVertexBuffer.reset(); |
| 171 | } |
| 172 | |
Chris Dalton | 33db9fc | 2020-02-25 18:52:12 -0700 | [diff] [blame] | 173 | bool prepareToDraw(); |
| 174 | |
Chris Dalton | 4386ad1 | 2020-02-19 16:42:06 -0700 | [diff] [blame] | 175 | // overridden by backend-specific derived class to perform the rendering command. |
Chris Dalton | 2c3e169 | 2020-03-20 12:32:55 -0600 | [diff] [blame] | 176 | virtual void onBegin() {} |
| 177 | virtual void onEnd() {} |
Chris Dalton | 4386ad1 | 2020-02-19 16:42:06 -0700 | [diff] [blame] | 178 | virtual bool onBindPipeline(const GrProgramInfo&, const SkRect& drawBounds) = 0; |
Chris Dalton | 2e7ed26 | 2020-02-21 15:17:59 -0700 | [diff] [blame] | 179 | virtual void onSetScissorRect(const SkIRect&) = 0; |
Chris Dalton | db20afc | 2020-03-05 12:13:53 -0700 | [diff] [blame] | 180 | virtual bool onBindTextures(const GrPrimitiveProcessor&, |
| 181 | const GrSurfaceProxy* const primProcTextures[], |
| 182 | const GrPipeline&) = 0; |
Greg Daniel | 426274b | 2020-07-20 11:37:38 -0400 | [diff] [blame] | 183 | virtual void onBindBuffers(sk_sp<const GrBuffer> indexBuffer, sk_sp<const GrBuffer> instanceBuffer, |
| 184 | sk_sp<const GrBuffer> vertexBuffer, GrPrimitiveRestart) = 0; |
Chris Dalton | ded4370 | 2020-03-02 11:45:27 -0700 | [diff] [blame] | 185 | virtual void onDraw(int vertexCount, int baseVertex) = 0; |
| 186 | virtual void onDrawIndexed(int indexCount, int baseIndex, uint16_t minIndexValue, |
| 187 | uint16_t maxIndexValue, int baseVertex) = 0; |
| 188 | virtual void onDrawInstanced(int instanceCount, int baseInstance, int vertexCount, |
Chris Dalton | 33db9fc | 2020-02-25 18:52:12 -0700 | [diff] [blame] | 189 | int baseVertex) = 0; |
Chris Dalton | ded4370 | 2020-03-02 11:45:27 -0700 | [diff] [blame] | 190 | virtual void onDrawIndexedInstanced(int indexCount, int baseIndex, int instanceCount, |
| 191 | int baseInstance, int baseVertex) = 0; |
Chris Dalton | 03fdf6a | 2020-04-07 12:31:59 -0600 | [diff] [blame] | 192 | virtual void onDrawIndirect(const GrBuffer*, size_t offset, int drawCount) { |
| 193 | SK_ABORT("Not implemented."); // Only called if caps.nativeDrawIndirectSupport(). |
| 194 | } |
| 195 | virtual void onDrawIndexedIndirect(const GrBuffer*, size_t offset, int drawCount) { |
| 196 | SK_ABORT("Not implemented."); // Only called if caps.nativeDrawIndirectSupport(). |
| 197 | } |
Michael Ludwig | 1e63279 | 2020-05-21 12:45:31 -0400 | [diff] [blame] | 198 | virtual void onClear(const GrScissorState&, const SkPMColor4f&) = 0; |
| 199 | virtual void onClearStencilClip(const GrScissorState&, bool insideStencilMask) = 0; |
Chris Dalton | 4386ad1 | 2020-02-19 16:42:06 -0700 | [diff] [blame] | 200 | virtual void onExecuteDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler>) {} |
| 201 | |
| 202 | enum class DrawPipelineStatus { |
| 203 | kOk = 0, |
| 204 | kNotConfigured, |
| 205 | kFailedToBind |
| 206 | }; |
| 207 | |
| 208 | DrawPipelineStatus fDrawPipelineStatus = DrawPipelineStatus::kNotConfigured; |
Chris Dalton | 2e7ed26 | 2020-02-21 15:17:59 -0700 | [diff] [blame] | 209 | GrXferBarrierType fXferBarrierType; |
| 210 | |
| 211 | #ifdef SK_DEBUG |
| 212 | enum class DynamicStateStatus { |
| 213 | kDisabled, |
| 214 | kUninitialized, |
| 215 | kConfigured |
| 216 | }; |
| 217 | |
Chris Dalton | 2c3e169 | 2020-03-20 12:32:55 -0600 | [diff] [blame] | 218 | DynamicStateStatus fScissorStatus; |
| 219 | DynamicStateStatus fTextureBindingStatus; |
| 220 | bool fHasIndexBuffer; |
| 221 | DynamicStateStatus fInstanceBufferStatus; |
| 222 | DynamicStateStatus fVertexBufferStatus; |
Chris Dalton | 2e7ed26 | 2020-02-21 15:17:59 -0700 | [diff] [blame] | 223 | #endif |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 224 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 225 | typedef GrOpsRenderPass INHERITED; |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | #endif |