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