blob: f4a93ebe71c35b41b49a62466e22d204ca45ab58 [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
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
bsalomon1a1aa932016-09-12 09:30:36 -070040 @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.
bsalomon38ddbad2015-09-24 06:00:00 -070060 */
wangyix7c157a92015-07-22 15:08:53 -070061 struct EmitArgs {
cdalton85285412016-02-18 12:37:07 -080062 EmitArgs(GrGLSLFPFragmentBuilder* fragBuilder,
egdaniel7ea439b2015-12-03 09:20:44 -080063 GrGLSLUniformHandler* uniformHandler,
egdaniela2e3e0f2015-11-19 07:23:45 -080064 const GrGLSLCaps* caps,
wangyix7c157a92015-07-22 15:08:53 -070065 const GrFragmentProcessor& fp,
66 const char* outputColor,
67 const char* inputColor,
bsalomon1a1aa932016-09-12 09:30:36 -070068 const SkTArray<GrShaderVar>& transformedCoords,
egdaniel09aa1fc2016-04-20 07:09:46 -070069 const SamplerHandle* texSamplers,
dvonbeck9b03e7b2016-08-01 11:01:56 -070070 const SamplerHandle* bufferSamplers,
71 bool gpImplementsDistanceVector)
egdaniel7ea439b2015-12-03 09:20:44 -080072 : fFragBuilder(fragBuilder)
73 , fUniformHandler(uniformHandler)
egdaniela2e3e0f2015-11-19 07:23:45 -080074 , fGLSLCaps(caps)
wangyix7c157a92015-07-22 15:08:53 -070075 , fFp(fp)
76 , fOutputColor(outputColor)
77 , fInputColor(inputColor)
bsalomon1a1aa932016-09-12 09:30:36 -070078 , fTransformedCoords(transformedCoords)
cdalton74b8d322016-04-11 14:47:28 -070079 , fTexSamplers(texSamplers)
dvonbeck9b03e7b2016-08-01 11:01:56 -070080 , fBufferSamplers(bufferSamplers)
bsalomon1a1aa932016-09-12 09:30:36 -070081 , fGpImplementsDistanceVector(gpImplementsDistanceVector) {}
cdalton85285412016-02-18 12:37:07 -080082 GrGLSLFPFragmentBuilder* fFragBuilder;
egdaniel7ea439b2015-12-03 09:20:44 -080083 GrGLSLUniformHandler* fUniformHandler;
egdaniela2e3e0f2015-11-19 07:23:45 -080084 const GrGLSLCaps* fGLSLCaps;
wangyix7c157a92015-07-22 15:08:53 -070085 const GrFragmentProcessor& fFp;
86 const char* fOutputColor;
87 const char* fInputColor;
bsalomon1a1aa932016-09-12 09:30:36 -070088 const SkTArray<GrShaderVar>& fTransformedCoords;
egdaniel09aa1fc2016-04-20 07:09:46 -070089 const SamplerHandle* fTexSamplers;
90 const SamplerHandle* fBufferSamplers;
dvonbeck9b03e7b2016-08-01 11:01:56 -070091 bool fGpImplementsDistanceVector;
wangyix7c157a92015-07-22 15:08:53 -070092 };
93
94 virtual void emitCode(EmitArgs&) = 0;
wangyix6af0c932015-07-22 10:21:17 -070095
egdaniel018fb622015-10-28 07:26:40 -070096 void setData(const GrGLSLProgramDataManager& pdman, const GrFragmentProcessor& processor);
wangyix6af0c932015-07-22 10:21:17 -070097
98 static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*) {}
99
wangyixb1daa862015-08-18 11:29:31 -0700100 int numChildProcessors() const { return fChildProcessors.count(); }
101
egdaniel64c47282015-11-13 06:54:19 -0800102 GrGLSLFragmentProcessor* childProcessor(int index) const {
wangyixb1daa862015-08-18 11:29:31 -0700103 return fChildProcessors[index];
104 }
105
wangyix54a6b1a2015-09-08 08:41:51 -0700106 /** 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 -0700107 * 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.
wangyix54a6b1a2015-09-08 08:41:51 -0700113 */
bsalomon38ddbad2015-09-24 06:00:00 -0700114 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);
wangyix2a378432015-08-18 12:00:12 -0700119
wangyixb1daa862015-08-18 11:29:31 -0700120protected:
egdaniel64c47282015-11-13 06:54:19 -0800121 /** A GrGLSLFragmentProcessor instance can be reused with any GrFragmentProcessor that produces
wangyixb1daa862015-08-18 11:29:31 -0700122 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
egdaniel64c47282015-11-13 06:54:19 -0800124 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. */
wangyixb1daa862015-08-18 11:29:31 -0700126 // TODO update this to pass in GrFragmentProcessor
egdaniel018fb622015-10-28 07:26:40 -0700127 virtual void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) {}
wangyixb1daa862015-08-18 11:29:31 -0700128
wangyix6af0c932015-07-22 10:21:17 -0700129private:
bsalomon38ddbad2015-09-24 06:00:00 -0700130 void internalEmitChild(int, const char*, const char*, EmitArgs&);
131
egdaniel64c47282015-11-13 06:54:19 -0800132 SkTArray<GrGLSLFragmentProcessor*, true> fChildProcessors;
wangyixb1daa862015-08-18 11:29:31 -0700133
134 friend class GrFragmentProcessor;
wangyix6af0c932015-07-22 10:21:17 -0700135};
136
137#endif