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" |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 14 | #include "glsl/GrGLSLSampler.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; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 21 | class GrGLSLUniformHandler; |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 22 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 23 | class GrGLSLFragmentProcessor { |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 24 | public: |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 25 | GrGLSLFragmentProcessor() {} |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 26 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 27 | virtual ~GrGLSLFragmentProcessor() { |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 28 | for (int i = 0; i < fChildProcessors.count(); ++i) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 29 | delete fChildProcessors[i]; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 30 | } |
| 31 | } |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 32 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 33 | typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 34 | typedef GrGLSLProgramDataManager::UniformHandle SamplerHandle; |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 35 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame^] | 36 | /** |
| 37 | * When building a program from a GrPipeline this is used to provide the GrShaderVars that |
| 38 | * contain the resulting transformed coords from each of a GrFragmentProcessor's |
| 39 | * GrCoordTransforms. This allows the GrFragmentProcessor subclasses to refer to the transformed |
| 40 | * coords in fragment code. |
| 41 | */ |
| 42 | class TransformedCoordVars { |
| 43 | public: |
| 44 | TransformedCoordVars(const GrFragmentProcessor* fp, const GrShaderVar* vars) |
| 45 | : fFP(fp) |
| 46 | , fTransformedVars(vars) {} |
| 47 | |
| 48 | const GrShaderVar& operator[] (int i) const { |
| 49 | SkASSERT(i >= 0 && i < fFP->numCoordTransforms()); |
| 50 | return fTransformedVars[i]; |
| 51 | } |
| 52 | |
| 53 | TransformedCoordVars childTransforms(int childIdx) const; |
| 54 | |
| 55 | private: |
| 56 | const GrFragmentProcessor* fFP; |
| 57 | const GrShaderVar* fTransformedVars; |
| 58 | }; |
| 59 | |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 60 | /** Called when the program stage should insert its code into the shaders. The code in each |
| 61 | shader will be in its own block ({}) and so locally scoped names will not collide across |
| 62 | stages. |
| 63 | |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 64 | @param fragBuilder Interface used to emit code in the shaders. |
| 65 | @param fp The processor that generated this program stage. |
| 66 | @param key The key that was computed by GenKey() from the generating |
| 67 | GrProcessor. |
| 68 | @param outputColor A predefined vec4 in the FS in which the stage should place its |
| 69 | output color (or coverage). |
| 70 | @param inputColor A vec4 that holds the input color to the stage in the FS. This may |
| 71 | be nullptr in which case the implied input is solid white (all |
| 72 | ones). TODO: Better system for communicating optimization info |
| 73 | (e.g. input color is solid white, trans black, known to be opaque, |
| 74 | etc.) that allows the processor to communicate back similar known |
| 75 | info about its output. |
| 76 | @param transformedCoords Fragment shader variables containing the coords computed using |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame^] | 77 | each of the GrFragmentProcessor's GrCoordTransforms. |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 78 | @param texSamplers Contains one entry for each GrTextureAccess of the GrProcessor. |
| 79 | These can be passed to the builder to emit texture reads in the |
| 80 | generated code. |
| 81 | @param bufferSamplers Contains one entry for each GrBufferAccess of the GrProcessor. |
| 82 | These can be passed to the builder to emit buffer reads in the |
| 83 | generated code. |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 84 | */ |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 85 | struct EmitArgs { |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 86 | EmitArgs(GrGLSLFPFragmentBuilder* fragBuilder, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 87 | GrGLSLUniformHandler* uniformHandler, |
egdaniel | a2e3e0f | 2015-11-19 07:23:45 -0800 | [diff] [blame] | 88 | const GrGLSLCaps* caps, |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 89 | const GrFragmentProcessor& fp, |
| 90 | const char* outputColor, |
| 91 | const char* inputColor, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame^] | 92 | const TransformedCoordVars& transformedCoordVars, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 93 | const SamplerHandle* texSamplers, |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 94 | const SamplerHandle* bufferSamplers, |
| 95 | bool gpImplementsDistanceVector) |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 96 | : fFragBuilder(fragBuilder) |
| 97 | , fUniformHandler(uniformHandler) |
egdaniel | a2e3e0f | 2015-11-19 07:23:45 -0800 | [diff] [blame] | 98 | , fGLSLCaps(caps) |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 99 | , fFp(fp) |
| 100 | , fOutputColor(outputColor) |
| 101 | , fInputColor(inputColor) |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame^] | 102 | , fTransformedCoords(transformedCoordVars) |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 103 | , fTexSamplers(texSamplers) |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 104 | , fBufferSamplers(bufferSamplers) |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 105 | , fGpImplementsDistanceVector(gpImplementsDistanceVector) {} |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 106 | GrGLSLFPFragmentBuilder* fFragBuilder; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 107 | GrGLSLUniformHandler* fUniformHandler; |
egdaniel | a2e3e0f | 2015-11-19 07:23:45 -0800 | [diff] [blame] | 108 | const GrGLSLCaps* fGLSLCaps; |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 109 | const GrFragmentProcessor& fFp; |
| 110 | const char* fOutputColor; |
| 111 | const char* fInputColor; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame^] | 112 | const TransformedCoordVars& fTransformedCoords; |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 113 | const SamplerHandle* fTexSamplers; |
| 114 | const SamplerHandle* fBufferSamplers; |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 115 | bool fGpImplementsDistanceVector; |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | virtual void emitCode(EmitArgs&) = 0; |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 119 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 120 | void setData(const GrGLSLProgramDataManager& pdman, const GrFragmentProcessor& processor); |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 121 | |
| 122 | static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*) {} |
| 123 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 124 | int numChildProcessors() const { return fChildProcessors.count(); } |
| 125 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 126 | GrGLSLFragmentProcessor* childProcessor(int index) const { |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 127 | return fChildProcessors[index]; |
| 128 | } |
| 129 | |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 130 | /** 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] | 131 | * emitChild will automatically extract the coords and samplers of that child and pass them |
| 132 | * on to the child's emitCode(). Also, any uniforms or functions emitted by the child will |
| 133 | * have their names mangled to prevent redefinitions. The output color name is also mangled |
| 134 | * therefore in an in/out param. It will be declared in mangled form by emitChild(). It is |
| 135 | * legal to pass nullptr as inputColor, since all fragment processors are required to work |
| 136 | * without an input color. |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 137 | */ |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 138 | void emitChild(int childIndex, const char* inputColor, SkString* outputColor, |
| 139 | EmitArgs& parentArgs); |
| 140 | |
| 141 | /** Variation that uses the parent's output color variable to hold the child's output.*/ |
| 142 | void emitChild(int childIndex, const char* inputColor, EmitArgs& parentArgs); |
wangyix | 2a37843 | 2015-08-18 12:00:12 -0700 | [diff] [blame] | 143 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 144 | protected: |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 145 | /** A GrGLSLFragmentProcessor instance can be reused with any GrFragmentProcessor that produces |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 146 | the same stage key; this function reads data from a GrFragmentProcessor and uploads any |
| 147 | uniform variables required by the shaders created in emitCode(). The GrFragmentProcessor |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 148 | parameter is guaranteed to be of the same type that created this GrGLSLFragmentProcessor and |
| 149 | to have an identical processor key as the one that created this GrGLSLFragmentProcessor. */ |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 150 | // TODO update this to pass in GrFragmentProcessor |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 151 | virtual void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) {} |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 152 | |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 153 | private: |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 154 | void internalEmitChild(int, const char*, const char*, EmitArgs&); |
| 155 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 156 | SkTArray<GrGLSLFragmentProcessor*, true> fChildProcessors; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 157 | |
| 158 | friend class GrFragmentProcessor; |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | #endif |