blob: 9889bcc9c64546498323714b09bcd53ff1718237 [file] [log] [blame]
wangyix6af0c932015-07-22 10:21:17 -07001/*
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
egdaniel64c47282015-11-13 06:54:19 -08008#ifndef GrGLSLFragmentProcessor_DEFINED
9#define GrGLSLFragmentProcessor_DEFINED
wangyix6af0c932015-07-22 10:21:17 -070010
cdalton3f6f76f2016-04-11 12:18:09 -070011#include "GrFragmentProcessor.h"
bsalomon1a1aa932016-09-12 09:30:36 -070012#include "GrShaderVar.h"
egdaniel018fb622015-10-28 07:26:40 -070013#include "glsl/GrGLSLProgramDataManager.h"
cdalton3f6f76f2016-04-11 12:18:09 -070014#include "glsl/GrGLSLSampler.h"
wangyix6af0c932015-07-22 10:21:17 -070015
egdaniel7dc4bd02015-10-29 07:57:01 -070016class GrProcessor;
17class GrProcessorKeyBuilder;
egdaniel7ea439b2015-12-03 09:20:44 -080018class GrGLSLCaps;
egdaniel8dcdedc2015-11-11 06:27:20 -080019class GrGLSLFPBuilder;
cdalton85285412016-02-18 12:37:07 -080020class GrGLSLFPFragmentBuilder;
egdaniel7ea439b2015-12-03 09:20:44 -080021class GrGLSLUniformHandler;
wangyix6af0c932015-07-22 10:21:17 -070022
egdaniel64c47282015-11-13 06:54:19 -080023class GrGLSLFragmentProcessor {
wangyix6af0c932015-07-22 10:21:17 -070024public:
egdaniel64c47282015-11-13 06:54:19 -080025 GrGLSLFragmentProcessor() {}
wangyix6af0c932015-07-22 10:21:17 -070026
egdaniel64c47282015-11-13 06:54:19 -080027 virtual ~GrGLSLFragmentProcessor() {
wangyixb1daa862015-08-18 11:29:31 -070028 for (int i = 0; i < fChildProcessors.count(); ++i) {
halcanary385fe4d2015-08-26 13:07:48 -070029 delete fChildProcessors[i];
wangyixb1daa862015-08-18 11:29:31 -070030 }
31 }
wangyix6af0c932015-07-22 10:21:17 -070032
egdaniel018fb622015-10-28 07:26:40 -070033 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
egdaniel09aa1fc2016-04-20 07:09:46 -070034 typedef GrGLSLProgramDataManager::UniformHandle SamplerHandle;
wangyix6af0c932015-07-22 10:21:17 -070035
bsalomona624bf32016-09-20 09:12:47 -070036 /**
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
wangyix6af0c932015-07-22 10:21:17 -070060 /** 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
bsalomon1a1aa932016-09-12 09:30:36 -070064 @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
bsalomona624bf32016-09-20 09:12:47 -070077 each of the GrFragmentProcessor's GrCoordTransforms.
bsalomon1a1aa932016-09-12 09:30:36 -070078 @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.
bsalomon38ddbad2015-09-24 06:00:00 -070084 */
wangyix7c157a92015-07-22 15:08:53 -070085 struct EmitArgs {
cdalton85285412016-02-18 12:37:07 -080086 EmitArgs(GrGLSLFPFragmentBuilder* fragBuilder,
egdaniel7ea439b2015-12-03 09:20:44 -080087 GrGLSLUniformHandler* uniformHandler,
egdaniela2e3e0f2015-11-19 07:23:45 -080088 const GrGLSLCaps* caps,
wangyix7c157a92015-07-22 15:08:53 -070089 const GrFragmentProcessor& fp,
90 const char* outputColor,
91 const char* inputColor,
bsalomona624bf32016-09-20 09:12:47 -070092 const TransformedCoordVars& transformedCoordVars,
egdaniel09aa1fc2016-04-20 07:09:46 -070093 const SamplerHandle* texSamplers,
dvonbeck9b03e7b2016-08-01 11:01:56 -070094 const SamplerHandle* bufferSamplers,
95 bool gpImplementsDistanceVector)
egdaniel7ea439b2015-12-03 09:20:44 -080096 : fFragBuilder(fragBuilder)
97 , fUniformHandler(uniformHandler)
egdaniela2e3e0f2015-11-19 07:23:45 -080098 , fGLSLCaps(caps)
wangyix7c157a92015-07-22 15:08:53 -070099 , fFp(fp)
100 , fOutputColor(outputColor)
101 , fInputColor(inputColor)
bsalomona624bf32016-09-20 09:12:47 -0700102 , fTransformedCoords(transformedCoordVars)
cdalton74b8d322016-04-11 14:47:28 -0700103 , fTexSamplers(texSamplers)
dvonbeck9b03e7b2016-08-01 11:01:56 -0700104 , fBufferSamplers(bufferSamplers)
bsalomon1a1aa932016-09-12 09:30:36 -0700105 , fGpImplementsDistanceVector(gpImplementsDistanceVector) {}
cdalton85285412016-02-18 12:37:07 -0800106 GrGLSLFPFragmentBuilder* fFragBuilder;
egdaniel7ea439b2015-12-03 09:20:44 -0800107 GrGLSLUniformHandler* fUniformHandler;
egdaniela2e3e0f2015-11-19 07:23:45 -0800108 const GrGLSLCaps* fGLSLCaps;
wangyix7c157a92015-07-22 15:08:53 -0700109 const GrFragmentProcessor& fFp;
110 const char* fOutputColor;
111 const char* fInputColor;
bsalomona624bf32016-09-20 09:12:47 -0700112 const TransformedCoordVars& fTransformedCoords;
egdaniel09aa1fc2016-04-20 07:09:46 -0700113 const SamplerHandle* fTexSamplers;
114 const SamplerHandle* fBufferSamplers;
dvonbeck9b03e7b2016-08-01 11:01:56 -0700115 bool fGpImplementsDistanceVector;
wangyix7c157a92015-07-22 15:08:53 -0700116 };
117
118 virtual void emitCode(EmitArgs&) = 0;
wangyix6af0c932015-07-22 10:21:17 -0700119
egdaniel018fb622015-10-28 07:26:40 -0700120 void setData(const GrGLSLProgramDataManager& pdman, const GrFragmentProcessor& processor);
wangyix6af0c932015-07-22 10:21:17 -0700121
122 static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*) {}
123
wangyixb1daa862015-08-18 11:29:31 -0700124 int numChildProcessors() const { return fChildProcessors.count(); }
125
egdaniel64c47282015-11-13 06:54:19 -0800126 GrGLSLFragmentProcessor* childProcessor(int index) const {
wangyixb1daa862015-08-18 11:29:31 -0700127 return fChildProcessors[index];
128 }
129
wangyix54a6b1a2015-09-08 08:41:51 -0700130 /** Will emit the code of a child proc in its own scope. Pass in the parent's EmitArgs and
bsalomon38ddbad2015-09-24 06:00:00 -0700131 * 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.
wangyix54a6b1a2015-09-08 08:41:51 -0700137 */
bsalomon38ddbad2015-09-24 06:00:00 -0700138 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);
wangyix2a378432015-08-18 12:00:12 -0700143
wangyixb1daa862015-08-18 11:29:31 -0700144protected:
egdaniel64c47282015-11-13 06:54:19 -0800145 /** A GrGLSLFragmentProcessor instance can be reused with any GrFragmentProcessor that produces
wangyixb1daa862015-08-18 11:29:31 -0700146 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
egdaniel64c47282015-11-13 06:54:19 -0800148 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. */
wangyixb1daa862015-08-18 11:29:31 -0700150 // TODO update this to pass in GrFragmentProcessor
egdaniel018fb622015-10-28 07:26:40 -0700151 virtual void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) {}
wangyixb1daa862015-08-18 11:29:31 -0700152
wangyix6af0c932015-07-22 10:21:17 -0700153private:
bsalomon38ddbad2015-09-24 06:00:00 -0700154 void internalEmitChild(int, const char*, const char*, EmitArgs&);
155
egdaniel64c47282015-11-13 06:54:19 -0800156 SkTArray<GrGLSLFragmentProcessor*, true> fChildProcessors;
wangyixb1daa862015-08-18 11:29:31 -0700157
158 friend class GrFragmentProcessor;
wangyix6af0c932015-07-22 10:21:17 -0700159};
160
161#endif