| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef GrGLProgramBuilder_DEFINED | 8 #ifndef GrGLProgramBuilder_DEFINED |
| 9 #define GrGLProgramBuilder_DEFINED | 9 #define GrGLProgramBuilder_DEFINED |
| 10 | 10 |
| 11 #include "GrPipeline.h" | 11 #include "GrPipeline.h" |
| 12 #include "gl/GrGLProgramDataManager.h" | 12 #include "gl/GrGLProgramDataManager.h" |
| 13 #include "gl/GrGLUniformHandler.h" | 13 #include "gl/GrGLUniformHandler.h" |
| 14 #include "gl/GrGLVaryingHandler.h" | 14 #include "gl/GrGLVaryingHandler.h" |
| 15 #include "glsl/GrGLSLPrimitiveProcessor.h" | |
| 16 #include "glsl/GrGLSLProgramBuilder.h" | 15 #include "glsl/GrGLSLProgramBuilder.h" |
| 17 #include "glsl/GrGLSLProgramDataManager.h" | 16 #include "glsl/GrGLSLProgramDataManager.h" |
| 18 #include "glsl/GrGLSLTextureSampler.h" | |
| 19 #include "glsl/GrGLSLXferProcessor.h" | |
| 20 | 17 |
| 21 class GrFragmentProcessor; | 18 class GrFragmentProcessor; |
| 22 class GrGLContextInfo; | 19 class GrGLContextInfo; |
| 23 class GrGLSLShaderBuilder; | 20 class GrGLSLShaderBuilder; |
| 24 class GrGLSLCaps; | 21 class GrGLSLCaps; |
| 25 | 22 |
| 26 /** | |
| 27 * The below struct represent processors installed in programs. | |
| 28 */ | |
| 29 template <class Proc> | |
| 30 struct GrGLInstalledProc { | |
| 31 SkDEBUGCODE(int fSamplersIdx;) | |
| 32 SkAutoTDelete<Proc> fGLProc; | |
| 33 }; | |
| 34 | |
| 35 typedef GrGLInstalledProc<GrGLSLPrimitiveProcessor> GrGLInstalledGeoProc; | |
| 36 typedef GrGLInstalledProc<GrGLSLXferProcessor> GrGLInstalledXferProc; | |
| 37 typedef GrGLInstalledProc<GrGLSLFragmentProcessor> GrGLInstalledFragProc; | |
| 38 | |
| 39 struct GrGLInstalledFragProcs : public SkRefCnt { | |
| 40 virtual ~GrGLInstalledFragProcs(); | |
| 41 SkSTArray<8, GrGLInstalledFragProc*, true> fProcs; | |
| 42 }; | |
| 43 | |
| 44 /* | |
| 45 * Please note - no diamond problems because of virtual inheritance. Also, both
base classes | |
| 46 * are pure virtual with no data members. This is the base class for program bu
ilding. | |
| 47 * Subclasses are nearly identical but each has their own way of emitting transf
orms. State for | |
| 48 * each of the elements of the shader pipeline, ie vertex, fragment, geometry, e
tc, lives in those | |
| 49 * respective builders | |
| 50 */ | |
| 51 class GrGLProgramBuilder : public GrGLSLProgramBuilder { | 23 class GrGLProgramBuilder : public GrGLSLProgramBuilder { |
| 52 public: | 24 public: |
| 53 /** Generates a shader program. | 25 /** Generates a shader program. |
| 54 * | 26 * |
| 55 * The program implements what is specified in the stages given as input. | 27 * The program implements what is specified in the stages given as input. |
| 56 * After successful generation, the builder result objects are available | 28 * After successful generation, the builder result objects are available |
| 57 * to be used. | 29 * to be used. |
| 58 * @return true if generation was successful. | 30 * @return true if generation was successful. |
| 59 */ | 31 */ |
| 60 static GrGLProgram* CreateProgram(const DrawArgs&, GrGLGpu*); | 32 static GrGLProgram* CreateProgram(const DrawArgs&, GrGLGpu*); |
| 61 | 33 |
| 34 const GrCaps* caps() const override; |
| 62 const GrGLSLCaps* glslCaps() const override; | 35 const GrGLSLCaps* glslCaps() const override; |
| 63 | 36 |
| 64 GrGLGpu* gpu() const { return fGpu; } | 37 GrGLGpu* gpu() const { return fGpu; } |
| 65 | 38 |
| 66 private: | 39 private: |
| 67 GrGLProgramBuilder(GrGLGpu*, const DrawArgs&); | 40 GrGLProgramBuilder(GrGLGpu*, const DrawArgs&); |
| 68 | 41 |
| 69 // Generates a possibly mangled name for a stage variable and writes it to t
he fragment shader. | |
| 70 // If GrGLSLExpr4 has a valid name then it will use that instead | |
| 71 void nameExpression(GrGLSLExpr4*, const char* baseName); | |
| 72 bool emitAndInstallProcs(GrGLSLExpr4* inputColor, GrGLSLExpr4* inputCoverage
); | |
| 73 void emitAndInstallFragProcs(int procOffset, int numProcs, GrGLSLExpr4* inOu
t); | |
| 74 void emitAndInstallProc(const GrFragmentProcessor&, | |
| 75 int index, | |
| 76 const GrGLSLExpr4& input, | |
| 77 GrGLSLExpr4* output); | |
| 78 | |
| 79 void emitAndInstallProc(const GrPrimitiveProcessor&, | |
| 80 GrGLSLExpr4* outputColor, | |
| 81 GrGLSLExpr4* outputCoverage); | |
| 82 | |
| 83 // these emit functions help to keep the createAndEmitProcessors template ge
neral | |
| 84 void emitAndInstallProc(const GrFragmentProcessor&, | |
| 85 int index, | |
| 86 const char* outColor, | |
| 87 const char* inColor); | |
| 88 void emitAndInstallProc(const GrPrimitiveProcessor&, | |
| 89 const char* outColor, | |
| 90 const char* outCoverage); | |
| 91 void emitAndInstallXferProc(const GrXferProcessor&, | |
| 92 const GrGLSLExpr4& colorIn, | |
| 93 const GrGLSLExpr4& coverageIn, | |
| 94 bool ignoresCoverage); | |
| 95 void emitFSOutputSwizzle(bool hasSecondaryOutput); | |
| 96 | |
| 97 void verify(const GrPrimitiveProcessor&); | |
| 98 void verify(const GrXferProcessor&); | |
| 99 void verify(const GrFragmentProcessor&); | |
| 100 template <class Proc> | |
| 101 void emitSamplers(const GrProcessor&, | 42 void emitSamplers(const GrProcessor&, |
| 102 GrGLSLTextureSampler::TextureSamplerArray* outSamplers, | 43 GrGLSLTextureSampler::TextureSamplerArray* outSamplers) ov
erride; |
| 103 GrGLInstalledProc<Proc>*); | |
| 104 | 44 |
| 105 bool compileAndAttachShaders(GrGLSLShaderBuilder& shader, | 45 bool compileAndAttachShaders(GrGLSLShaderBuilder& shader, |
| 106 GrGLuint programId, | 46 GrGLuint programId, |
| 107 GrGLenum type, | 47 GrGLenum type, |
| 108 SkTDArray<GrGLuint>* shaderIds); | 48 SkTDArray<GrGLuint>* shaderIds); |
| 109 GrGLProgram* finalize(); | 49 GrGLProgram* finalize(); |
| 110 void bindProgramResourceLocations(GrGLuint programID); | 50 void bindProgramResourceLocations(GrGLuint programID); |
| 111 bool checkLinkStatus(GrGLuint programID); | 51 bool checkLinkStatus(GrGLuint programID); |
| 112 void resolveProgramResourceLocations(GrGLuint programID); | 52 void resolveProgramResourceLocations(GrGLuint programID); |
| 113 void cleanupProgram(GrGLuint programID, const SkTDArray<GrGLuint>& shaderIDs
); | 53 void cleanupProgram(GrGLuint programID, const SkTDArray<GrGLuint>& shaderIDs
); |
| 114 void cleanupShaders(const SkTDArray<GrGLuint>& shaderIDs); | 54 void cleanupShaders(const SkTDArray<GrGLuint>& shaderIDs); |
| 115 | 55 |
| 116 // Subclasses create different programs | 56 // Subclasses create different programs |
| 117 GrGLProgram* createProgram(GrGLuint programID); | 57 GrGLProgram* createProgram(GrGLuint programID); |
| 118 | 58 |
| 119 GrGLSLUniformHandler* uniformHandler() override { return &fUniformHandler; } | 59 GrGLSLUniformHandler* uniformHandler() override { return &fUniformHandler; } |
| 120 const GrGLSLUniformHandler* uniformHandler() const override { return &fUnifo
rmHandler; } | 60 const GrGLSLUniformHandler* uniformHandler() const override { return &fUnifo
rmHandler; } |
| 121 GrGLSLVaryingHandler* varyingHandler() override { return &fVaryingHandler; } | 61 GrGLSLVaryingHandler* varyingHandler() override { return &fVaryingHandler; } |
| 122 | 62 |
| 123 // reset is called by program creator between each processor's emit code. I
t increments the | |
| 124 // stage offset for variable name mangling, and also ensures verfication var
iables in the | |
| 125 // fragment shader are cleared. | |
| 126 void reset() { | |
| 127 this->addStage(); | |
| 128 fFS.reset(); | |
| 129 } | |
| 130 void addStage() { fStageIndex++; } | |
| 131 | |
| 132 class AutoStageAdvance { | |
| 133 public: | |
| 134 AutoStageAdvance(GrGLProgramBuilder* pb) | |
| 135 : fPB(pb) { | |
| 136 fPB->reset(); | |
| 137 // Each output to the fragment processor gets its own code section | |
| 138 fPB->fFS.nextStage(); | |
| 139 } | |
| 140 ~AutoStageAdvance() {} | |
| 141 private: | |
| 142 GrGLProgramBuilder* fPB; | |
| 143 }; | |
| 144 | |
| 145 GrGLInstalledGeoProc* fGeometryProcessor; | |
| 146 GrGLInstalledXferProc* fXferProcessor; | |
| 147 SkAutoTUnref<GrGLInstalledFragProcs> fFragmentProcessors; | |
| 148 | 63 |
| 149 GrGLGpu* fGpu; | 64 GrGLGpu* fGpu; |
| 150 GrGLSLPrimitiveProcessor::TransformsIn fCoordTransforms; | |
| 151 GrGLSLPrimitiveProcessor::TransformsOut fOutCoords; | |
| 152 typedef GrGLSLUniformHandler::UniformHandle UniformHandle; | 65 typedef GrGLSLUniformHandler::UniformHandle UniformHandle; |
| 153 SkTArray<UniformHandle> fSamplerUniforms; | 66 SkTArray<UniformHandle> fSamplerUniforms; |
| 154 | 67 |
| 155 GrGLVaryingHandler fVaryingHandler; | 68 GrGLVaryingHandler fVaryingHandler; |
| 156 GrGLUniformHandler fUniformHandler; | 69 GrGLUniformHandler fUniformHandler; |
| 157 | 70 |
| 158 typedef GrGLSLProgramBuilder INHERITED; | 71 typedef GrGLSLProgramBuilder INHERITED; |
| 159 }; | 72 }; |
| 160 #endif | 73 #endif |
| OLD | NEW |