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 8 #ifndef GrGLSLFragmentProcessor_DEFINED 9 #define GrGLSLFragmentProcessor_DEFINED 10 11 #include "glsl/GrGLSLProcessorTypes.h" 12 #include "glsl/GrGLSLProgramDataManager.h" 13 #include "glsl/GrGLSLTextureSampler.h" 14 15 class GrProcessor; 16 class GrProcessorKeyBuilder; 17 class GrGLSLCaps; 18 class GrGLSLFPBuilder; 19 class GrGLSLFPFragmentBuilder; 20 class GrGLSLUniformHandler; 21 22 class GrGLSLFragmentProcessor { 23 public: GrGLSLFragmentProcessor()24 GrGLSLFragmentProcessor() {} 25 ~GrGLSLFragmentProcessor()26 virtual ~GrGLSLFragmentProcessor() { 27 for (int i = 0; i < fChildProcessors.count(); ++i) { 28 delete fChildProcessors[i]; 29 } 30 } 31 32 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; 33 typedef GrGLSLTextureSampler::TextureSamplerArray TextureSamplerArray; 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 45 nullptr in which case the implied input is solid white (all ones). 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. 52 */ 53 54 struct EmitArgs { EmitArgsEmitArgs55 EmitArgs(GrGLSLFPFragmentBuilder* fragBuilder, 56 GrGLSLUniformHandler* uniformHandler, 57 const GrGLSLCaps* caps, 58 const GrFragmentProcessor& fp, 59 const char* outputColor, 60 const char* inputColor, 61 const GrGLSLTransformedCoordsArray& coords, 62 const TextureSamplerArray& samplers) 63 : fFragBuilder(fragBuilder) 64 , fUniformHandler(uniformHandler) 65 , fGLSLCaps(caps) 66 , fFp(fp) 67 , fOutputColor(outputColor) 68 , fInputColor(inputColor) 69 , fCoords(coords) 70 , fSamplers(samplers) {} 71 GrGLSLFPFragmentBuilder* fFragBuilder; 72 GrGLSLUniformHandler* fUniformHandler; 73 const GrGLSLCaps* fGLSLCaps; 74 const GrFragmentProcessor& fFp; 75 const char* fOutputColor; 76 const char* fInputColor; 77 const GrGLSLTransformedCoordsArray& fCoords; 78 const TextureSamplerArray& fSamplers; 79 }; 80 81 virtual void emitCode(EmitArgs&) = 0; 82 83 void setData(const GrGLSLProgramDataManager& pdman, const GrFragmentProcessor& processor); 84 GenKey(const GrProcessor &,const GrGLSLCaps &,GrProcessorKeyBuilder *)85 static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*) {} 86 numChildProcessors()87 int numChildProcessors() const { return fChildProcessors.count(); } 88 childProcessor(int index)89 GrGLSLFragmentProcessor* childProcessor(int index) const { 90 return fChildProcessors[index]; 91 } 92 93 /** Will emit the code of a child proc in its own scope. Pass in the parent's EmitArgs and 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. 100 */ 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); 106 107 protected: 108 /** A GrGLSLFragmentProcessor instance can be reused with any GrFragmentProcessor that produces 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 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. */ 113 // TODO update this to pass in GrFragmentProcessor onSetData(const GrGLSLProgramDataManager &,const GrProcessor &)114 virtual void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) {} 115 116 private: 117 void internalEmitChild(int, const char*, const char*, EmitArgs&); 118 119 SkTArray<GrGLSLFragmentProcessor*, true> fChildProcessors; 120 121 friend class GrFragmentProcessor; 122 }; 123 124 #endif 125