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