wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 8 | #ifndef GrGLSLFragmentProcessor_DEFINED |
| 9 | #define GrGLSLFragmentProcessor_DEFINED |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 10 | |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 11 | #include "GrFragmentProcessor.h" |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 12 | #include "GrShaderVar.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 13 | #include "glsl/GrGLSLProgramDataManager.h" |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame^] | 14 | #include "glsl/GrGLSLUniformHandler.h" |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 15 | |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 16 | class GrProcessor; |
| 17 | class GrProcessorKeyBuilder; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 18 | class GrGLSLCaps; |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 19 | class GrGLSLFPBuilder; |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 20 | class GrGLSLFPFragmentBuilder; |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 21 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 22 | class GrGLSLFragmentProcessor { |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 23 | public: |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 24 | GrGLSLFragmentProcessor() {} |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 25 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 26 | virtual ~GrGLSLFragmentProcessor() { |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 27 | for (int i = 0; i < fChildProcessors.count(); ++i) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 28 | delete fChildProcessors[i]; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 29 | } |
| 30 | } |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 31 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame^] | 32 | using UniformHandle = GrGLSLUniformHandler::UniformHandle; |
| 33 | using SamplerHandle = GrGLSLUniformHandler::SamplerHandle; |
| 34 | using ImageStorageHandle = GrGLSLUniformHandler::ImageStorageHandle; |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 35 | |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 36 | private: |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 37 | /** |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 38 | * This class allows the shader builder to provide each GrGLSLFragmentProcesor with an array of |
| 39 | * generated variables where each generated variable corresponds to an element of an array on |
| 40 | * the GrFragmentProcessor that generated the GLSLFP. For example, this is used to provide a |
| 41 | * variable holding transformed coords for each GrCoordTransform owned by the FP. |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 42 | */ |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 43 | template <typename T, typename FPBASE, int (FPBASE::*COUNT)() const> |
| 44 | class BuilderInputProvider { |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 45 | public: |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 46 | BuilderInputProvider(const GrFragmentProcessor* fp, const T* ts) : fFP(fp) , fTs(ts) {} |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 47 | |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 48 | const T& operator[] (int i) const { |
| 49 | SkASSERT(i >= 0 && i < (fFP->*COUNT)()); |
| 50 | return fTs[i]; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 51 | } |
| 52 | |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 53 | BuilderInputProvider childInputs(int childIdx) const { |
| 54 | const GrFragmentProcessor* child = &fFP->childProcessor(childIdx); |
| 55 | GrFragmentProcessor::Iter iter(fFP); |
| 56 | int numToSkip = 0; |
| 57 | while (true) { |
| 58 | const GrFragmentProcessor* fp = iter.next(); |
| 59 | if (fp == child) { |
| 60 | return BuilderInputProvider(child, fTs + numToSkip); |
| 61 | } |
| 62 | numToSkip += (fp->*COUNT)(); |
| 63 | } |
| 64 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 65 | |
| 66 | private: |
| 67 | const GrFragmentProcessor* fFP; |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 68 | const T* fTs; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 71 | public: |
| 72 | using TransformedCoordVars = BuilderInputProvider<GrShaderVar, GrFragmentProcessor, |
| 73 | &GrFragmentProcessor::numCoordTransforms>; |
| 74 | using TextureSamplers = BuilderInputProvider<SamplerHandle, GrProcessor, |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 75 | &GrProcessor::numTextureSamplers>; |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 76 | using BufferSamplers = BuilderInputProvider<SamplerHandle, GrProcessor, |
| 77 | &GrProcessor::numBuffers>; |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame^] | 78 | using ImageStorages = BuilderInputProvider<ImageStorageHandle, GrProcessor, |
| 79 | &GrProcessor::numImageStorages>; |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 80 | |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 81 | /** Called when the program stage should insert its code into the shaders. The code in each |
| 82 | shader will be in its own block ({}) and so locally scoped names will not collide across |
| 83 | stages. |
| 84 | |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 85 | @param fragBuilder Interface used to emit code in the shaders. |
| 86 | @param fp The processor that generated this program stage. |
| 87 | @param key The key that was computed by GenKey() from the generating |
| 88 | GrProcessor. |
| 89 | @param outputColor A predefined vec4 in the FS in which the stage should place its |
| 90 | output color (or coverage). |
| 91 | @param inputColor A vec4 that holds the input color to the stage in the FS. This may |
| 92 | be nullptr in which case the implied input is solid white (all |
| 93 | ones). TODO: Better system for communicating optimization info |
| 94 | (e.g. input color is solid white, trans black, known to be opaque, |
| 95 | etc.) that allows the processor to communicate back similar known |
| 96 | info about its output. |
| 97 | @param transformedCoords Fragment shader variables containing the coords computed using |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 98 | each of the GrFragmentProcessor's GrCoordTransforms. |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 99 | @param texSamplers Contains one entry for each TextureSampler of the GrProcessor. |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 100 | These can be passed to the builder to emit texture reads in the |
| 101 | generated code. |
Brian Salomon | b014cca | 2016-11-18 11:39:15 -0500 | [diff] [blame] | 102 | @param bufferSamplers Contains one entry for each BufferAccess of the GrProcessor. These |
| 103 | can be passed to the builder to emit buffer reads in the generated |
| 104 | code. |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame^] | 105 | @param imageStorages Contains one entry for each ImageStorageAccess of the GrProcessor. |
| 106 | These can be passed to the builder to emit image loads and stores |
| 107 | in the generated code. |
| 108 | @param gpImplementsDistanceVector |
| 109 | Does the GrGeometryProcessor implement the feature where it |
| 110 | provides a vector to the nearest edge of the shape being rendered. |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 111 | */ |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 112 | struct EmitArgs { |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 113 | EmitArgs(GrGLSLFPFragmentBuilder* fragBuilder, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 114 | GrGLSLUniformHandler* uniformHandler, |
egdaniel | a2e3e0f | 2015-11-19 07:23:45 -0800 | [diff] [blame] | 115 | const GrGLSLCaps* caps, |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 116 | const GrFragmentProcessor& fp, |
| 117 | const char* outputColor, |
| 118 | const char* inputColor, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 119 | const TransformedCoordVars& transformedCoordVars, |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 120 | const TextureSamplers& textureSamplers, |
| 121 | const BufferSamplers& bufferSamplers, |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame^] | 122 | const ImageStorages& imageStorages, |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 123 | bool gpImplementsDistanceVector) |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 124 | : fFragBuilder(fragBuilder) |
| 125 | , fUniformHandler(uniformHandler) |
egdaniel | a2e3e0f | 2015-11-19 07:23:45 -0800 | [diff] [blame] | 126 | , fGLSLCaps(caps) |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 127 | , fFp(fp) |
| 128 | , fOutputColor(outputColor) |
| 129 | , fInputColor(inputColor) |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 130 | , fTransformedCoords(transformedCoordVars) |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 131 | , fTexSamplers(textureSamplers) |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 132 | , fBufferSamplers(bufferSamplers) |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame^] | 133 | , fImageStorages(imageStorages) |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 134 | , fGpImplementsDistanceVector(gpImplementsDistanceVector) {} |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 135 | GrGLSLFPFragmentBuilder* fFragBuilder; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 136 | GrGLSLUniformHandler* fUniformHandler; |
egdaniel | a2e3e0f | 2015-11-19 07:23:45 -0800 | [diff] [blame] | 137 | const GrGLSLCaps* fGLSLCaps; |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 138 | const GrFragmentProcessor& fFp; |
| 139 | const char* fOutputColor; |
| 140 | const char* fInputColor; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 141 | const TransformedCoordVars& fTransformedCoords; |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 142 | const TextureSamplers& fTexSamplers; |
| 143 | const BufferSamplers& fBufferSamplers; |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame^] | 144 | const ImageStorages& fImageStorages; |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 145 | bool fGpImplementsDistanceVector; |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | virtual void emitCode(EmitArgs&) = 0; |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 149 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 150 | void setData(const GrGLSLProgramDataManager& pdman, const GrFragmentProcessor& processor); |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 151 | |
| 152 | static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*) {} |
| 153 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 154 | int numChildProcessors() const { return fChildProcessors.count(); } |
| 155 | |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 156 | GrGLSLFragmentProcessor* childProcessor(int index) { |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 157 | return fChildProcessors[index]; |
| 158 | } |
| 159 | |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 160 | /** Will emit the code of a child proc in its own scope. Pass in the parent's EmitArgs and |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 161 | * emitChild will automatically extract the coords and samplers of that child and pass them |
| 162 | * on to the child's emitCode(). Also, any uniforms or functions emitted by the child will |
| 163 | * have their names mangled to prevent redefinitions. The output color name is also mangled |
| 164 | * therefore in an in/out param. It will be declared in mangled form by emitChild(). It is |
| 165 | * legal to pass nullptr as inputColor, since all fragment processors are required to work |
| 166 | * without an input color. |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 167 | */ |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 168 | void emitChild(int childIndex, const char* inputColor, SkString* outputColor, |
| 169 | EmitArgs& parentArgs); |
| 170 | |
| 171 | /** Variation that uses the parent's output color variable to hold the child's output.*/ |
| 172 | void emitChild(int childIndex, const char* inputColor, EmitArgs& parentArgs); |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 173 | |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 174 | /** |
| 175 | * Pre-order traversal of a GLSLFP hierarchy, or of multiple trees with roots in an array of |
| 176 | * GLSLFPS. This agrees with the traversal order of GrFragmentProcessor::Iter |
| 177 | */ |
| 178 | class Iter : public SkNoncopyable { |
| 179 | public: |
| 180 | explicit Iter(GrGLSLFragmentProcessor* fp) { fFPStack.push_back(fp); } |
| 181 | explicit Iter(GrGLSLFragmentProcessor* fps[], int cnt) { |
| 182 | for (int i = cnt - 1; i >= 0; --i) { |
| 183 | fFPStack.push_back(fps[i]); |
| 184 | } |
| 185 | } |
| 186 | GrGLSLFragmentProcessor* next(); |
| 187 | |
| 188 | private: |
| 189 | SkSTArray<4, GrGLSLFragmentProcessor*, true> fFPStack; |
| 190 | }; |
| 191 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 192 | protected: |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 193 | /** A GrGLSLFragmentProcessor instance can be reused with any GrFragmentProcessor that produces |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 194 | the same stage key; this function reads data from a GrFragmentProcessor and uploads any |
| 195 | uniform variables required by the shaders created in emitCode(). The GrFragmentProcessor |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 196 | parameter is guaranteed to be of the same type that created this GrGLSLFragmentProcessor and |
| 197 | to have an identical processor key as the one that created this GrGLSLFragmentProcessor. */ |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 198 | // TODO update this to pass in GrFragmentProcessor |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 199 | virtual void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) {} |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 200 | |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 201 | private: |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 202 | void internalEmitChild(int, const char*, const char*, EmitArgs&); |
| 203 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 204 | SkTArray<GrGLSLFragmentProcessor*, true> fChildProcessors; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 205 | |
| 206 | friend class GrFragmentProcessor; |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | #endif |