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 GrGLSLPrimitiveProcessor_DEFINED 9 #define GrGLSLPrimitiveProcessor_DEFINED 10 11 #include "GrFragmentProcessor.h" 12 #include "GrPrimitiveProcessor.h" 13 #include "glsl/GrGLSLProgramDataManager.h" 14 #include "glsl/GrGLSLUniformHandler.h" 15 16 class GrPrimitiveProcessor; 17 class GrGLSLPPFragmentBuilder; 18 class GrGLSLGeometryBuilder; 19 class GrGLSLGPBuilder; 20 class GrGLSLVaryingHandler; 21 class GrGLSLVertexBuilder; 22 class GrShaderCaps; 23 24 class GrGLSLPrimitiveProcessor { 25 public: 26 using FPCoordTransformIter = GrFragmentProcessor::CoordTransformIter; 27 ~GrGLSLPrimitiveProcessor()28 virtual ~GrGLSLPrimitiveProcessor() {} 29 30 using UniformHandle = GrGLSLProgramDataManager::UniformHandle; 31 using SamplerHandle = GrGLSLUniformHandler::SamplerHandle; 32 using ImageStorageHandle = GrGLSLUniformHandler::ImageStorageHandle; 33 34 /** 35 * This class provides access to the GrCoordTransforms across all GrFragmentProcessors in a 36 * GrPipeline. It is also used by the primitive processor to specify the fragment shader 37 * variable that will hold the transformed coords for each GrCoordTransform. It is required that 38 * the primitive processor iterate over each coord transform and insert a shader var result for 39 * each. The GrGLSLFragmentProcessors will reference these variables in their fragment code. 40 */ 41 class FPCoordTransformHandler : public SkNoncopyable { 42 public: FPCoordTransformHandler(const GrPipeline & pipeline,SkTArray<GrShaderVar> * transformedCoordVars)43 FPCoordTransformHandler(const GrPipeline& pipeline, 44 SkTArray<GrShaderVar>* transformedCoordVars) 45 : fIter(pipeline) 46 , fTransformedCoordVars(transformedCoordVars) {} 47 ~FPCoordTransformHandler()48 ~FPCoordTransformHandler() { SkASSERT(!this->nextCoordTransform());} 49 50 const GrCoordTransform* nextCoordTransform(); 51 52 // 'args' are constructor params to GrShaderVar. 53 template<typename... Args> specifyCoordsForCurrCoordTransform(Args &&...args)54 void specifyCoordsForCurrCoordTransform(Args&&... args) { 55 SkASSERT(!fAddedCoord); 56 fTransformedCoordVars->emplace_back(std::forward<Args>(args)...); 57 SkDEBUGCODE(fAddedCoord = true;) 58 } 59 60 private: 61 GrFragmentProcessor::CoordTransformIter fIter; 62 SkDEBUGCODE(bool fAddedCoord = false;) 63 SkDEBUGCODE(const GrCoordTransform* fCurr = nullptr;) 64 SkTArray<GrShaderVar>* fTransformedCoordVars; 65 }; 66 67 struct EmitArgs { EmitArgsEmitArgs68 EmitArgs(GrGLSLVertexBuilder* vertBuilder, 69 GrGLSLGeometryBuilder* geomBuilder, 70 GrGLSLPPFragmentBuilder* fragBuilder, 71 GrGLSLVaryingHandler* varyingHandler, 72 GrGLSLUniformHandler* uniformHandler, 73 const GrShaderCaps* caps, 74 const GrPrimitiveProcessor& gp, 75 const char* outputColor, 76 const char* outputCoverage, 77 const char* distanceVectorName, 78 const char* rtAdjustName, 79 const SamplerHandle* texSamplers, 80 const SamplerHandle* bufferSamplers, 81 const ImageStorageHandle* imageStorages, 82 FPCoordTransformHandler* transformHandler) 83 : fVertBuilder(vertBuilder) 84 , fGeomBuilder(geomBuilder) 85 , fFragBuilder(fragBuilder) 86 , fVaryingHandler(varyingHandler) 87 , fUniformHandler(uniformHandler) 88 , fShaderCaps(caps) 89 , fGP(gp) 90 , fOutputColor(outputColor) 91 , fOutputCoverage(outputCoverage) 92 , fDistanceVectorName(distanceVectorName) 93 , fRTAdjustName(rtAdjustName) 94 , fTexSamplers(texSamplers) 95 , fBufferSamplers(bufferSamplers) 96 , fImageStorages(imageStorages) 97 , fFPCoordTransformHandler(transformHandler) {} 98 GrGLSLVertexBuilder* fVertBuilder; 99 GrGLSLGeometryBuilder* fGeomBuilder; 100 GrGLSLPPFragmentBuilder* fFragBuilder; 101 GrGLSLVaryingHandler* fVaryingHandler; 102 GrGLSLUniformHandler* fUniformHandler; 103 const GrShaderCaps* fShaderCaps; 104 const GrPrimitiveProcessor& fGP; 105 const char* fOutputColor; 106 const char* fOutputCoverage; 107 const char* fDistanceVectorName; 108 const char* fRTAdjustName; 109 const SamplerHandle* fTexSamplers; 110 const SamplerHandle* fBufferSamplers; 111 const ImageStorageHandle* fImageStorages; 112 FPCoordTransformHandler* fFPCoordTransformHandler; 113 }; 114 115 /** 116 * This is similar to emitCode() in the base class, except it takes a full shader builder. 117 * This allows the effect subclass to emit vertex code. 118 */ 119 virtual void emitCode(EmitArgs&) = 0; 120 121 /** 122 * A GrGLSLPrimitiveProcessor instance can be reused with any GrGLSLPrimitiveProcessor that 123 * produces the same stage key; this function reads data from a GrGLSLPrimitiveProcessor and 124 * uploads any uniform variables required by the shaders created in emitCode(). The 125 * GrPrimitiveProcessor parameter is guaranteed to be of the same type and to have an 126 * identical processor key as the GrPrimitiveProcessor that created this 127 * GrGLSLPrimitiveProcessor. 128 * The subclass may use the transform iterator to perform any setup required for the particular 129 * set of fp transform matrices, such as uploading via uniforms. The iterator will iterate over 130 * the transforms in the same order as the TransformHandler passed to emitCode. 131 */ 132 virtual void setData(const GrGLSLProgramDataManager&, const GrPrimitiveProcessor&, 133 FPCoordTransformIter&&) = 0; 134 135 static SkMatrix GetTransformMatrix(const SkMatrix& localMatrix, const GrCoordTransform&); 136 137 protected: 138 void setupUniformColor(GrGLSLPPFragmentBuilder* fragBuilder, 139 GrGLSLUniformHandler* uniformHandler, 140 const char* outputName, 141 UniformHandle* colorUniform); 142 }; 143 144 #endif 145