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