blob: 355d49022597bc2c3c73d1d99df26bf1af6cb30d [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"
Brian Salomonf9f45122016-11-29 11:59:17 -050014#include "glsl/GrGLSLUniformHandler.h"
wangyix6af0c932015-07-22 10:21:17 -070015
egdaniel7dc4bd02015-10-29 07:57:01 -070016class GrProcessor;
17class GrProcessorKeyBuilder;
egdaniel8dcdedc2015-11-11 06:27:20 -080018class GrGLSLFPBuilder;
cdalton85285412016-02-18 12:37:07 -080019class GrGLSLFPFragmentBuilder;
wangyix6af0c932015-07-22 10:21:17 -070020
egdaniel64c47282015-11-13 06:54:19 -080021class GrGLSLFragmentProcessor {
wangyix6af0c932015-07-22 10:21:17 -070022public:
egdaniel64c47282015-11-13 06:54:19 -080023 GrGLSLFragmentProcessor() {}
wangyix6af0c932015-07-22 10:21:17 -070024
egdaniel64c47282015-11-13 06:54:19 -080025 virtual ~GrGLSLFragmentProcessor() {
wangyixb1daa862015-08-18 11:29:31 -070026 for (int i = 0; i < fChildProcessors.count(); ++i) {
halcanary385fe4d2015-08-26 13:07:48 -070027 delete fChildProcessors[i];
wangyixb1daa862015-08-18 11:29:31 -070028 }
29 }
wangyix6af0c932015-07-22 10:21:17 -070030
Brian Salomonf9f45122016-11-29 11:59:17 -050031 using UniformHandle = GrGLSLUniformHandler::UniformHandle;
32 using SamplerHandle = GrGLSLUniformHandler::SamplerHandle;
Greg Danielbc5d4d72017-05-05 10:28:42 -040033 using TexelBufferHandle = GrGLSLUniformHandler::TexelBufferHandle;
Brian Salomonf9f45122016-11-29 11:59:17 -050034 using ImageStorageHandle = GrGLSLUniformHandler::ImageStorageHandle;
wangyix6af0c932015-07-22 10:21:17 -070035
bsalomonb58a2b42016-09-26 06:55:02 -070036private:
bsalomona624bf32016-09-20 09:12:47 -070037 /**
bsalomonb58a2b42016-09-26 06:55:02 -070038 * 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.
bsalomona624bf32016-09-20 09:12:47 -070042 */
bsalomonb58a2b42016-09-26 06:55:02 -070043 template <typename T, typename FPBASE, int (FPBASE::*COUNT)() const>
44 class BuilderInputProvider {
bsalomona624bf32016-09-20 09:12:47 -070045 public:
bsalomonb58a2b42016-09-26 06:55:02 -070046 BuilderInputProvider(const GrFragmentProcessor* fp, const T* ts) : fFP(fp) , fTs(ts) {}
bsalomona624bf32016-09-20 09:12:47 -070047
bsalomonb58a2b42016-09-26 06:55:02 -070048 const T& operator[] (int i) const {
49 SkASSERT(i >= 0 && i < (fFP->*COUNT)());
50 return fTs[i];
bsalomona624bf32016-09-20 09:12:47 -070051 }
52
bsalomonb58a2b42016-09-26 06:55:02 -070053 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 }
bsalomona624bf32016-09-20 09:12:47 -070065
66 private:
67 const GrFragmentProcessor* fFP;
bsalomonb58a2b42016-09-26 06:55:02 -070068 const T* fTs;
bsalomona624bf32016-09-20 09:12:47 -070069 };
70
bsalomonb58a2b42016-09-26 06:55:02 -070071public:
72 using TransformedCoordVars = BuilderInputProvider<GrShaderVar, GrFragmentProcessor,
73 &GrFragmentProcessor::numCoordTransforms>;
Brian Salomonab015ef2017-04-04 10:15:51 -040074 using TextureSamplers = BuilderInputProvider<SamplerHandle, GrResourceIOProcessor,
75 &GrResourceIOProcessor::numTextureSamplers>;
Greg Danielbc5d4d72017-05-05 10:28:42 -040076 using TexelBuffers = BuilderInputProvider<TexelBufferHandle, GrResourceIOProcessor,
Brian Salomonab015ef2017-04-04 10:15:51 -040077 &GrResourceIOProcessor::numBuffers>;
78 using ImageStorages = BuilderInputProvider<ImageStorageHandle, GrResourceIOProcessor,
79 &GrResourceIOProcessor::numImageStorages>;
bsalomonb58a2b42016-09-26 06:55:02 -070080
wangyix6af0c932015-07-22 10:21:17 -070081 /** 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
bsalomon1a1aa932016-09-12 09:30:36 -070085 @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
bsalomona624bf32016-09-20 09:12:47 -070098 each of the GrFragmentProcessor's GrCoordTransforms.
Brian Salomon0bbecb22016-11-17 11:38:22 -050099 @param texSamplers Contains one entry for each TextureSampler of the GrProcessor.
bsalomon1a1aa932016-09-12 09:30:36 -0700100 These can be passed to the builder to emit texture reads in the
101 generated code.
Brian Salomonb014cca2016-11-18 11:39:15 -0500102 @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 Salomonf9f45122016-11-29 11:59:17 -0500105 @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.
bsalomon38ddbad2015-09-24 06:00:00 -0700108 */
wangyix7c157a92015-07-22 15:08:53 -0700109 struct EmitArgs {
cdalton85285412016-02-18 12:37:07 -0800110 EmitArgs(GrGLSLFPFragmentBuilder* fragBuilder,
egdaniel7ea439b2015-12-03 09:20:44 -0800111 GrGLSLUniformHandler* uniformHandler,
Brian Salomon94efbf52016-11-29 13:43:05 -0500112 const GrShaderCaps* caps,
wangyix7c157a92015-07-22 15:08:53 -0700113 const GrFragmentProcessor& fp,
114 const char* outputColor,
115 const char* inputColor,
bsalomona624bf32016-09-20 09:12:47 -0700116 const TransformedCoordVars& transformedCoordVars,
bsalomonb58a2b42016-09-26 06:55:02 -0700117 const TextureSamplers& textureSamplers,
Greg Danielbc5d4d72017-05-05 10:28:42 -0400118 const TexelBuffers& texelBuffers,
Brian Salomone23bffd2017-06-02 11:01:10 -0400119 const ImageStorages& imageStorages)
120 : fFragBuilder(fragBuilder)
121 , fUniformHandler(uniformHandler)
122 , fShaderCaps(caps)
123 , fFp(fp)
124 , fOutputColor(outputColor)
125 , fInputColor(inputColor)
126 , fTransformedCoords(transformedCoordVars)
127 , fTexSamplers(textureSamplers)
128 , fTexelBuffers(texelBuffers)
129 , fImageStorages(imageStorages) {}
cdalton85285412016-02-18 12:37:07 -0800130 GrGLSLFPFragmentBuilder* fFragBuilder;
egdaniel7ea439b2015-12-03 09:20:44 -0800131 GrGLSLUniformHandler* fUniformHandler;
Brian Salomon1edc5b92016-11-29 13:43:46 -0500132 const GrShaderCaps* fShaderCaps;
wangyix7c157a92015-07-22 15:08:53 -0700133 const GrFragmentProcessor& fFp;
134 const char* fOutputColor;
135 const char* fInputColor;
bsalomona624bf32016-09-20 09:12:47 -0700136 const TransformedCoordVars& fTransformedCoords;
bsalomonb58a2b42016-09-26 06:55:02 -0700137 const TextureSamplers& fTexSamplers;
Greg Danielbc5d4d72017-05-05 10:28:42 -0400138 const TexelBuffers& fTexelBuffers;
Brian Salomonf9f45122016-11-29 11:59:17 -0500139 const ImageStorages& fImageStorages;
wangyix7c157a92015-07-22 15:08:53 -0700140 };
141
142 virtual void emitCode(EmitArgs&) = 0;
wangyix6af0c932015-07-22 10:21:17 -0700143
egdaniel018fb622015-10-28 07:26:40 -0700144 void setData(const GrGLSLProgramDataManager& pdman, const GrFragmentProcessor& processor);
wangyix6af0c932015-07-22 10:21:17 -0700145
Brian Salomon94efbf52016-11-29 13:43:05 -0500146 static void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*) {}
wangyix6af0c932015-07-22 10:21:17 -0700147
wangyixb1daa862015-08-18 11:29:31 -0700148 int numChildProcessors() const { return fChildProcessors.count(); }
149
bsalomonb58a2b42016-09-26 06:55:02 -0700150 GrGLSLFragmentProcessor* childProcessor(int index) {
wangyixb1daa862015-08-18 11:29:31 -0700151 return fChildProcessors[index];
152 }
153
Ethan Nicholas2983f402017-05-08 09:36:08 -0400154 inline void emitChild(int childIndex, SkString* outputColor, EmitArgs& parentArgs) {
155 this->emitChild(childIndex, "vec4(1.0)", outputColor, parentArgs);
156 }
157
wangyix54a6b1a2015-09-08 08:41:51 -0700158 /** 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 -0700159 * emitChild will automatically extract the coords and samplers of that child and pass them
160 * on to the child's emitCode(). Also, any uniforms or functions emitted by the child will
161 * have their names mangled to prevent redefinitions. The output color name is also mangled
162 * therefore in an in/out param. It will be declared in mangled form by emitChild(). It is
163 * legal to pass nullptr as inputColor, since all fragment processors are required to work
164 * without an input color.
wangyix54a6b1a2015-09-08 08:41:51 -0700165 */
bsalomon38ddbad2015-09-24 06:00:00 -0700166 void emitChild(int childIndex, const char* inputColor, SkString* outputColor,
167 EmitArgs& parentArgs);
168
Ethan Nicholas2983f402017-05-08 09:36:08 -0400169 inline void emitChild(int childIndex, EmitArgs& args) {
170 this->emitChild(childIndex, "vec4(1.0)", args);
171 }
172
bsalomon38ddbad2015-09-24 06:00:00 -0700173 /** Variation that uses the parent's output color variable to hold the child's output.*/
174 void emitChild(int childIndex, const char* inputColor, EmitArgs& parentArgs);
wangyix2a378432015-08-18 12:00:12 -0700175
bsalomonb58a2b42016-09-26 06:55:02 -0700176 /**
177 * Pre-order traversal of a GLSLFP hierarchy, or of multiple trees with roots in an array of
178 * GLSLFPS. This agrees with the traversal order of GrFragmentProcessor::Iter
179 */
180 class Iter : public SkNoncopyable {
181 public:
182 explicit Iter(GrGLSLFragmentProcessor* fp) { fFPStack.push_back(fp); }
183 explicit Iter(GrGLSLFragmentProcessor* fps[], int cnt) {
184 for (int i = cnt - 1; i >= 0; --i) {
185 fFPStack.push_back(fps[i]);
186 }
187 }
188 GrGLSLFragmentProcessor* next();
189
190 private:
191 SkSTArray<4, GrGLSLFragmentProcessor*, true> fFPStack;
192 };
193
wangyixb1daa862015-08-18 11:29:31 -0700194protected:
egdaniel64c47282015-11-13 06:54:19 -0800195 /** A GrGLSLFragmentProcessor instance can be reused with any GrFragmentProcessor that produces
wangyixb1daa862015-08-18 11:29:31 -0700196 the same stage key; this function reads data from a GrFragmentProcessor and uploads any
197 uniform variables required by the shaders created in emitCode(). The GrFragmentProcessor
egdaniel64c47282015-11-13 06:54:19 -0800198 parameter is guaranteed to be of the same type that created this GrGLSLFragmentProcessor and
199 to have an identical processor key as the one that created this GrGLSLFragmentProcessor. */
Brian Salomonab015ef2017-04-04 10:15:51 -0400200 virtual void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) {}
wangyixb1daa862015-08-18 11:29:31 -0700201
wangyix6af0c932015-07-22 10:21:17 -0700202private:
bsalomon38ddbad2015-09-24 06:00:00 -0700203 void internalEmitChild(int, const char*, const char*, EmitArgs&);
204
egdaniel64c47282015-11-13 06:54:19 -0800205 SkTArray<GrGLSLFragmentProcessor*, true> fChildProcessors;
wangyixb1daa862015-08-18 11:29:31 -0700206
207 friend class GrFragmentProcessor;
wangyix6af0c932015-07-22 10:21:17 -0700208};
209
210#endif