blob: ca72a9c415b3df4d6fe1fb0bdba1989dd0e36ab3 [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
egdaniel7dc4bd02015-10-29 07:57:01 -070011#include "glsl/GrGLSLProcessorTypes.h"
egdaniel018fb622015-10-28 07:26:40 -070012#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7dc4bd02015-10-29 07:57:01 -070013#include "glsl/GrGLSLTextureSampler.h"
wangyix6af0c932015-07-22 10:21:17 -070014
egdaniel7dc4bd02015-10-29 07:57:01 -070015class GrProcessor;
16class GrProcessorKeyBuilder;
egdaniel7ea439b2015-12-03 09:20:44 -080017class GrGLSLCaps;
egdaniel8dcdedc2015-11-11 06:27:20 -080018class GrGLSLFPBuilder;
egdaniel4ca2e602015-11-18 08:01:26 -080019class GrGLSLFragmentBuilder;
egdaniel7ea439b2015-12-03 09:20:44 -080020class GrGLSLUniformHandler;
wangyix6af0c932015-07-22 10:21:17 -070021
egdaniel64c47282015-11-13 06:54:19 -080022class GrGLSLFragmentProcessor {
wangyix6af0c932015-07-22 10:21:17 -070023public:
egdaniel64c47282015-11-13 06:54:19 -080024 GrGLSLFragmentProcessor() {}
wangyix6af0c932015-07-22 10:21:17 -070025
egdaniel64c47282015-11-13 06:54:19 -080026 virtual ~GrGLSLFragmentProcessor() {
wangyixb1daa862015-08-18 11:29:31 -070027 for (int i = 0; i < fChildProcessors.count(); ++i) {
halcanary385fe4d2015-08-26 13:07:48 -070028 delete fChildProcessors[i];
wangyixb1daa862015-08-18 11:29:31 -070029 }
30 }
wangyix6af0c932015-07-22 10:21:17 -070031
egdaniel018fb622015-10-28 07:26:40 -070032 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
egdaniel7dc4bd02015-10-29 07:57:01 -070033 typedef GrGLSLTextureSampler::TextureSamplerArray TextureSamplerArray;
wangyix6af0c932015-07-22 10:21:17 -070034
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
halcanary96fcdcc2015-08-27 07:41:13 -070045 nullptr in which case the implied input is solid white (all ones).
wangyix6af0c932015-07-22 10:21:17 -070046 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.
bsalomon38ddbad2015-09-24 06:00:00 -070052 */
wangyix7c157a92015-07-22 15:08:53 -070053
54 struct EmitArgs {
egdaniel7ea439b2015-12-03 09:20:44 -080055 EmitArgs(GrGLSLFragmentBuilder* fragBuilder,
56 GrGLSLUniformHandler* uniformHandler,
egdaniela2e3e0f2015-11-19 07:23:45 -080057 const GrGLSLCaps* caps,
wangyix7c157a92015-07-22 15:08:53 -070058 const GrFragmentProcessor& fp,
59 const char* outputColor,
60 const char* inputColor,
egdaniel7dc4bd02015-10-29 07:57:01 -070061 const GrGLSLTransformedCoordsArray& coords,
wangyix7c157a92015-07-22 15:08:53 -070062 const TextureSamplerArray& samplers)
egdaniel7ea439b2015-12-03 09:20:44 -080063 : fFragBuilder(fragBuilder)
64 , fUniformHandler(uniformHandler)
egdaniela2e3e0f2015-11-19 07:23:45 -080065 , fGLSLCaps(caps)
wangyix7c157a92015-07-22 15:08:53 -070066 , fFp(fp)
67 , fOutputColor(outputColor)
68 , fInputColor(inputColor)
69 , fCoords(coords)
70 , fSamplers(samplers) {}
egdaniel4ca2e602015-11-18 08:01:26 -080071 GrGLSLFragmentBuilder* fFragBuilder;
egdaniel7ea439b2015-12-03 09:20:44 -080072 GrGLSLUniformHandler* fUniformHandler;
egdaniela2e3e0f2015-11-19 07:23:45 -080073 const GrGLSLCaps* fGLSLCaps;
wangyix7c157a92015-07-22 15:08:53 -070074 const GrFragmentProcessor& fFp;
75 const char* fOutputColor;
76 const char* fInputColor;
egdaniel7dc4bd02015-10-29 07:57:01 -070077 const GrGLSLTransformedCoordsArray& fCoords;
wangyix7c157a92015-07-22 15:08:53 -070078 const TextureSamplerArray& fSamplers;
79 };
80
81 virtual void emitCode(EmitArgs&) = 0;
wangyix6af0c932015-07-22 10:21:17 -070082
egdaniel018fb622015-10-28 07:26:40 -070083 void setData(const GrGLSLProgramDataManager& pdman, const GrFragmentProcessor& processor);
wangyix6af0c932015-07-22 10:21:17 -070084
85 static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*) {}
86
wangyixb1daa862015-08-18 11:29:31 -070087 int numChildProcessors() const { return fChildProcessors.count(); }
88
egdaniel64c47282015-11-13 06:54:19 -080089 GrGLSLFragmentProcessor* childProcessor(int index) const {
wangyixb1daa862015-08-18 11:29:31 -070090 return fChildProcessors[index];
91 }
92
wangyix54a6b1a2015-09-08 08:41:51 -070093 /** 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 -070094 * 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.
wangyix54a6b1a2015-09-08 08:41:51 -0700100 */
bsalomon38ddbad2015-09-24 06:00:00 -0700101 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);
wangyix2a378432015-08-18 12:00:12 -0700106
wangyixb1daa862015-08-18 11:29:31 -0700107protected:
egdaniel64c47282015-11-13 06:54:19 -0800108 /** A GrGLSLFragmentProcessor instance can be reused with any GrFragmentProcessor that produces
wangyixb1daa862015-08-18 11:29:31 -0700109 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
egdaniel64c47282015-11-13 06:54:19 -0800111 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. */
wangyixb1daa862015-08-18 11:29:31 -0700113 // TODO update this to pass in GrFragmentProcessor
egdaniel018fb622015-10-28 07:26:40 -0700114 virtual void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) {}
wangyixb1daa862015-08-18 11:29:31 -0700115
wangyix6af0c932015-07-22 10:21:17 -0700116private:
bsalomon38ddbad2015-09-24 06:00:00 -0700117 void internalEmitChild(int, const char*, const char*, EmitArgs&);
118
egdaniel64c47282015-11-13 06:54:19 -0800119 SkTArray<GrGLSLFragmentProcessor*, true> fChildProcessors;
wangyixb1daa862015-08-18 11:29:31 -0700120
121 friend class GrFragmentProcessor;
wangyix6af0c932015-07-22 10:21:17 -0700122};
123
124#endif