| egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2015 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 GrGLSLProgramBuilder_DEFINED | 
|  | 9 | #define GrGLSLProgramBuilder_DEFINED | 
|  | 10 |  | 
| Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 11 | #include "GrCaps.h" | 
| egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 12 | #include "GrGeometryProcessor.h" | 
| Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 13 | #include "GrProgramDesc.h" | 
| robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 14 | #include "glsl/GrGLSLFragmentProcessor.h" | 
| egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 15 | #include "glsl/GrGLSLFragmentShaderBuilder.h" | 
| egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 16 | #include "glsl/GrGLSLPrimitiveProcessor.h" | 
| egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 17 | #include "glsl/GrGLSLProgramDataManager.h" | 
| egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 18 | #include "glsl/GrGLSLUniformHandler.h" | 
| Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 19 | #include "glsl/GrGLSLVertexGeoBuilder.h" | 
| egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 20 | #include "glsl/GrGLSLXferProcessor.h" | 
| egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 21 |  | 
| Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 22 | class GrShaderVar; | 
| egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 23 | class GrGLSLVaryingHandler; | 
| Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 24 | class SkString; | 
| Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 25 | class GrShaderCaps; | 
| egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 26 |  | 
| egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 27 | class GrGLSLProgramBuilder { | 
| egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 28 | public: | 
| Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 29 | using UniformHandle      = GrGLSLUniformHandler::UniformHandle; | 
|  | 30 | using SamplerHandle      = GrGLSLUniformHandler::SamplerHandle; | 
| egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 31 |  | 
|  | 32 | virtual ~GrGLSLProgramBuilder() {} | 
| egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 33 |  | 
| egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 34 | virtual const GrCaps* caps() const = 0; | 
| Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 35 | const GrShaderCaps* shaderCaps() const { return this->caps()->shaderCaps(); } | 
| egdaniel | a2e3e0f | 2015-11-19 07:23:45 -0800 | [diff] [blame] | 36 |  | 
| egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 37 | const GrPrimitiveProcessor& primitiveProcessor() const { return fPrimProc; } | 
|  | 38 | const GrPipeline& pipeline() const { return fPipeline; } | 
| Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 39 | GrProgramDesc* desc() { return fDesc; } | 
|  | 40 | const GrProgramDesc::KeyHeader& header() const { return fDesc->header(); } | 
| egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 41 |  | 
| cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 42 | void appendUniformDecls(GrShaderFlags visibility, SkString*) const; | 
| egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 43 |  | 
| Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 44 | const GrShaderVar& samplerVariable(SamplerHandle handle) const { | 
| Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 45 | return this->uniformHandler()->samplerVariable(handle); | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | GrSwizzle samplerSwizzle(SamplerHandle handle) const { | 
|  | 49 | return this->uniformHandler()->samplerSwizzle(handle); | 
|  | 50 | } | 
| egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 51 |  | 
| Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame^] | 52 | // Used to add a uniform for the RenderTarget height (used for frag position) without mangling | 
|  | 53 | // the name of the uniform inside of a stage. | 
|  | 54 | void addRTHeightUniform(const char* name); | 
| egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 55 |  | 
|  | 56 | // Generates a name for a variable. The generated string will be name prefixed by the prefix | 
|  | 57 | // char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless | 
|  | 58 | // explicitly asked not to. | 
|  | 59 | void nameVariable(SkString* out, char prefix, const char* name, bool mangle = true); | 
|  | 60 |  | 
| egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 61 | virtual GrGLSLUniformHandler* uniformHandler() = 0; | 
|  | 62 | virtual const GrGLSLUniformHandler* uniformHandler() const = 0; | 
| egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 63 | virtual GrGLSLVaryingHandler* varyingHandler() = 0; | 
|  | 64 |  | 
| egdaniel | b80ec8b | 2016-02-09 09:54:43 -0800 | [diff] [blame] | 65 | // Used for backend customization of the output color and secondary color variables from the | 
|  | 66 | // fragment processor. Only used if the outputs are explicitly declared in the shaders | 
| Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 67 | virtual void finalizeFragmentOutputColor(GrShaderVar& outputColor) {} | 
|  | 68 | virtual void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) {} | 
| egdaniel | b80ec8b | 2016-02-09 09:54:43 -0800 | [diff] [blame] | 69 |  | 
| egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 70 | // number of each input/output type in a single allocation block, used by many builders | 
|  | 71 | static const int kVarsPerBlock; | 
|  | 72 |  | 
| egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 73 | GrGLSLVertexBuilder         fVS; | 
|  | 74 | GrGLSLGeometryBuilder       fGS; | 
| egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 75 | GrGLSLFragmentShaderBuilder fFS; | 
| egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 76 |  | 
| egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 77 | int fStageIndex; | 
|  | 78 |  | 
| egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 79 | const GrPipeline&           fPipeline; | 
|  | 80 | const GrPrimitiveProcessor& fPrimProc; | 
| Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 81 | GrProgramDesc*              fDesc; | 
| egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 82 |  | 
| Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 83 | GrGLSLBuiltinUniformHandles fUniformHandles; | 
| egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 84 |  | 
| Robert Phillips | 369e8b7 | 2017-08-01 16:13:04 -0400 | [diff] [blame] | 85 | std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor; | 
|  | 86 | std::unique_ptr<GrGLSLXferProcessor> fXferProcessor; | 
| Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 87 | std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors; | 
|  | 88 | int fFragmentProcessorCnt; | 
| egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 89 |  | 
| egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 90 | protected: | 
| Brian Salomon | ff168d9 | 2018-06-23 15:17:27 -0400 | [diff] [blame] | 91 | explicit GrGLSLProgramBuilder(const GrPrimitiveProcessor&, const GrPipeline&, GrProgramDesc*); | 
| egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 92 |  | 
| cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 93 | void addFeature(GrShaderFlags shaders, uint32_t featureBit, const char* extensionName); | 
|  | 94 |  | 
| Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 95 | bool emitAndInstallProcs(); | 
| egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 96 |  | 
| egdaniel | 9f1d415 | 2016-02-10 09:50:38 -0800 | [diff] [blame] | 97 | void finalizeShaders(); | 
|  | 98 |  | 
| Brian Salomon | dc09213 | 2018-04-04 10:14:16 -0400 | [diff] [blame] | 99 | bool fragColorIsInOut() const { return fFS.primaryColorOutputIsInOut(); } | 
|  | 100 |  | 
| egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 101 | private: | 
|  | 102 | // reset is called by program creator between each processor's emit code.  It increments the | 
|  | 103 | // stage offset for variable name mangling, and also ensures verfication variables in the | 
|  | 104 | // fragment shader are cleared. | 
|  | 105 | void reset() { | 
|  | 106 | this->addStage(); | 
| cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 107 | SkDEBUGCODE(fFS.resetVerification();) | 
| egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 108 | } | 
|  | 109 | void addStage() { fStageIndex++; } | 
|  | 110 |  | 
|  | 111 | class AutoStageAdvance { | 
|  | 112 | public: | 
|  | 113 | AutoStageAdvance(GrGLSLProgramBuilder* pb) | 
|  | 114 | : fPB(pb) { | 
|  | 115 | fPB->reset(); | 
|  | 116 | // Each output to the fragment processor gets its own code section | 
|  | 117 | fPB->fFS.nextStage(); | 
|  | 118 | } | 
|  | 119 | ~AutoStageAdvance() {} | 
|  | 120 | private: | 
|  | 121 | GrGLSLProgramBuilder* fPB; | 
|  | 122 | }; | 
|  | 123 |  | 
|  | 124 | // Generates a possibly mangled name for a stage variable and writes it to the fragment shader. | 
| Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 125 | void nameExpression(SkString*, const char* baseName); | 
| egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 126 |  | 
|  | 127 | void emitAndInstallPrimProc(const GrPrimitiveProcessor&, | 
| Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 128 | SkString* outputColor, | 
|  | 129 | SkString* outputCoverage); | 
|  | 130 | void emitAndInstallFragProcs(SkString* colorInOut, SkString* coverageInOut); | 
|  | 131 | SkString emitAndInstallFragProc(const GrFragmentProcessor&, | 
|  | 132 | int index, | 
|  | 133 | int transformedCoordVarsIdx, | 
|  | 134 | const SkString& input, | 
| Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 135 | SkString output, | 
|  | 136 | SkTArray<std::unique_ptr<GrGLSLFragmentProcessor>>*); | 
| Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 137 | void emitAndInstallXferProc(const SkString& colorIn, const SkString& coverageIn); | 
| Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 138 | SamplerHandle emitSampler(GrTextureType, GrPixelConfig, const char* name, | 
| Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 139 | GrShaderFlags visibility); | 
| egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 140 | void emitFSOutputSwizzle(bool hasSecondaryOutput); | 
| Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 141 | void updateSamplerCounts(GrShaderFlags visibility); | 
| cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 142 | bool checkSamplerCounts(); | 
| egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 143 |  | 
| cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 144 | #ifdef SK_DEBUG | 
| egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 145 | void verify(const GrPrimitiveProcessor&); | 
|  | 146 | void verify(const GrXferProcessor&); | 
|  | 147 | void verify(const GrFragmentProcessor&); | 
| cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 148 | #endif | 
| egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 149 |  | 
| Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 150 | // These are used to check that we don't excede the allowable number of resources in a shader. | 
| bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 151 | int                         fNumVertexSamplers; | 
|  | 152 | int                         fNumGeometrySamplers; | 
|  | 153 | int                         fNumFragmentSamplers; | 
|  | 154 | SkSTArray<4, GrShaderVar>   fTransformedCoordVars; | 
| egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 155 | }; | 
|  | 156 |  | 
|  | 157 | #endif |