joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 8 | #include "GrGLProgramBuilder.h" |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame^] | 9 | |
| 10 | #include "gl/GrGLGeometryProcessor.h" |
| 11 | #include "gl/GrGLGpu.h" |
| 12 | #include "gl/GrGLPathProcessor.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 13 | #include "gl/GrGLProgram.h" |
| 14 | #include "gl/GrGLSLPrettyPrint.h" |
| 15 | #include "gl/GrGLUniformHandle.h" |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame^] | 16 | #include "gl/GrGLXferProcessor.h" |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 17 | #include "GrCoordTransform.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 18 | #include "GrGLProgramBuilder.h" |
| 19 | #include "GrTexture.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 20 | #include "SkRTConf.h" |
| 21 | #include "SkTraceEvent.h" |
| 22 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 23 | #define GL_CALL(X) GR_GL_CALL(this->gpu()->glInterface(), X) |
| 24 | #define GL_CALL_RET(R, X) GR_GL_CALL_RET(this->gpu()->glInterface(), R, X) |
| 25 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 26 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 27 | |
| 28 | class GrGLNvprProgramBuilder : public GrGLProgramBuilder { |
| 29 | public: |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 30 | GrGLNvprProgramBuilder(GrGLGpu* gpu, const DrawArgs& args) |
| 31 | : INHERITED(gpu, args) {} |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 32 | |
| 33 | GrGLProgram* createProgram(GrGLuint programID) SK_OVERRIDE { |
| 34 | // this is just for nvpr es, which has separable varyings that are plugged in after |
| 35 | // building |
| 36 | GrGLPathProcessor* pathProc = |
| 37 | static_cast<GrGLPathProcessor*>(fGeometryProcessor->fGLProc.get()); |
| 38 | pathProc->resolveSeparableVaryings(fGpu, programID); |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 39 | return SkNEW_ARGS(GrGLNvprProgram, (fGpu, this->desc(), fUniformHandles, programID, |
| 40 | fUniforms, |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 41 | fGeometryProcessor, |
| 42 | fXferProcessor, fFragmentProcessors.get())); |
| 43 | } |
| 44 | |
| 45 | private: |
| 46 | typedef GrGLProgramBuilder INHERITED; |
| 47 | }; |
| 48 | |
| 49 | |
| 50 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 51 | ////////////////////////////////////////////////////////////////////////////// |
| 52 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 53 | const int GrGLProgramBuilder::kVarsPerBlock = 8; |
| 54 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 55 | GrGLProgram* GrGLProgramBuilder::CreateProgram(const DrawArgs& args, GrGLGpu* gpu) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 56 | // create a builder. This will be handed off to effects so they can use it to add |
| 57 | // uniforms, varyings, textures, etc |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 58 | SkAutoTDelete<GrGLProgramBuilder> builder(CreateProgramBuilder(args, gpu)); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 59 | |
| 60 | GrGLProgramBuilder* pb = builder.get(); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 61 | |
egdaniel | 37b4d86 | 2014-11-03 10:07:07 -0800 | [diff] [blame] | 62 | // TODO: Once all stages can handle taking a float or vec4 and correctly handling them we can |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 63 | // seed correctly here |
| 64 | GrGLSLExpr4 inputColor; |
| 65 | GrGLSLExpr4 inputCoverage; |
egdaniel | 37b4d86 | 2014-11-03 10:07:07 -0800 | [diff] [blame] | 66 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 67 | pb->emitAndInstallProcs(&inputColor, &inputCoverage); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 68 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 69 | return pb->finalize(); |
| 70 | } |
| 71 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 72 | GrGLProgramBuilder* GrGLProgramBuilder::CreateProgramBuilder(const DrawArgs& args, |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 73 | GrGLGpu* gpu) { |
joshualitt | 17e7314 | 2015-01-21 11:52:36 -0800 | [diff] [blame] | 74 | if (args.fPrimitiveProcessor->isPathRendering()) { |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 75 | SkASSERT(gpu->glCaps().pathRenderingSupport() && |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 76 | !args.fPrimitiveProcessor->willUseGeoShader() && |
| 77 | args.fPrimitiveProcessor->numAttribs() == 0); |
| 78 | return SkNEW_ARGS(GrGLNvprProgramBuilder, (gpu, args)); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 79 | } else { |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 80 | return SkNEW_ARGS(GrGLProgramBuilder, (gpu, args)); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | ///////////////////////////////////////////////////////////////////////////// |
| 85 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 86 | GrGLProgramBuilder::GrGLProgramBuilder(GrGLGpu* gpu, const DrawArgs& args) |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 87 | : fVS(this) |
| 88 | , fGS(this) |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 89 | , fFS(this, args.fDesc->header().fFragPosKey) |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 90 | , fOutOfStage(true) |
| 91 | , fStageIndex(-1) |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 92 | , fGeometryProcessor(NULL) |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 93 | , fXferProcessor(NULL) |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 94 | , fArgs(args) |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 95 | , fGpu(gpu) |
| 96 | , fUniforms(kVarsPerBlock) { |
| 97 | } |
| 98 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 99 | void GrGLProgramBuilder::addVarying(const char* name, |
| 100 | GrGLVarying* varying, |
bsalomon | c0bd648 | 2014-12-09 10:04:14 -0800 | [diff] [blame] | 101 | GrSLPrecision fsPrecision) { |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 102 | SkASSERT(varying); |
| 103 | if (varying->vsVarying()) { |
| 104 | fVS.addVarying(name, varying); |
| 105 | } |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 106 | if (this->primitiveProcessor().willUseGeoShader()) { |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 107 | fGS.addVarying(name, varying); |
| 108 | } |
| 109 | if (varying->fsVarying()) { |
| 110 | fFS.addVarying(varying, fsPrecision); |
| 111 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 112 | } |
| 113 | |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 114 | void GrGLProgramBuilder::addPassThroughAttribute(const GrPrimitiveProcessor::Attribute* input, |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 115 | const char* output) { |
| 116 | GrSLType type = GrVertexAttribTypeToSLType(input->fType); |
| 117 | GrGLVertToFrag v(type); |
| 118 | this->addVarying(input->fName, &v); |
| 119 | fVS.codeAppendf("%s = %s;", v.vsOut(), input->fName); |
| 120 | fFS.codeAppendf("%s = %s;", output, v.fsIn()); |
| 121 | } |
| 122 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 123 | void GrGLProgramBuilder::nameVariable(SkString* out, char prefix, const char* name) { |
| 124 | if ('\0' == prefix) { |
| 125 | *out = name; |
| 126 | } else { |
| 127 | out->printf("%c%s", prefix, name); |
| 128 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 129 | if (!fOutOfStage) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 130 | if (out->endsWith('_')) { |
| 131 | // Names containing "__" are reserved. |
| 132 | out->append("x"); |
| 133 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 134 | out->appendf("_Stage%d", fStageIndex); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 138 | GrGLProgramDataManager::UniformHandle GrGLProgramBuilder::addUniformArray( |
| 139 | uint32_t visibility, |
| 140 | GrSLType type, |
| 141 | GrSLPrecision precision, |
| 142 | const char* name, |
| 143 | int count, |
| 144 | const char** outName) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 145 | SkASSERT(name && strlen(name)); |
| 146 | SkDEBUGCODE(static const uint32_t kVisibilityMask = kVertex_Visibility | kFragment_Visibility); |
| 147 | SkASSERT(0 == (~kVisibilityMask & visibility)); |
| 148 | SkASSERT(0 != visibility); |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 149 | SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type)); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 150 | |
| 151 | UniformInfo& uni = fUniforms.push_back(); |
| 152 | uni.fVariable.setType(type); |
| 153 | uni.fVariable.setTypeModifier(GrGLShaderVar::kUniform_TypeModifier); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 154 | // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use |
| 155 | // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB |
| 156 | // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then |
| 157 | // the names will mismatch. I think the correct solution is to have all GPs which need the |
| 158 | // uniform view matrix, they should upload the view matrix in their setData along with regular |
| 159 | // uniforms. |
| 160 | char prefix = 'u'; |
| 161 | if ('u' == name[0]) { |
| 162 | prefix = '\0'; |
| 163 | } |
| 164 | this->nameVariable(uni.fVariable.accessName(), prefix, name); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 165 | uni.fVariable.setArrayCount(count); |
| 166 | uni.fVisibility = visibility; |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 167 | uni.fVariable.setPrecision(precision); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 168 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 169 | if (outName) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 170 | *outName = uni.fVariable.c_str(); |
| 171 | } |
| 172 | return GrGLProgramDataManager::UniformHandle::CreateFromUniformIndex(fUniforms.count() - 1); |
| 173 | } |
| 174 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 175 | void GrGLProgramBuilder::appendUniformDecls(ShaderVisibility visibility, |
| 176 | SkString* out) const { |
| 177 | for (int i = 0; i < fUniforms.count(); ++i) { |
| 178 | if (fUniforms[i].fVisibility & visibility) { |
| 179 | fUniforms[i].fVariable.appendDecl(this->ctxInfo(), out); |
| 180 | out->append(";\n"); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 185 | const GrGLContextInfo& GrGLProgramBuilder::ctxInfo() const { |
| 186 | return fGpu->ctxInfo(); |
| 187 | } |
| 188 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 189 | void GrGLProgramBuilder::emitAndInstallProcs(GrGLSLExpr4* inputColor, GrGLSLExpr4* inputCoverage) { |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 190 | // First we loop over all of the installed processors and collect coord transforms. These will |
| 191 | // be sent to the GrGLPrimitiveProcessor in its emitCode function |
| 192 | SkSTArray<8, GrGLProcessor::TransformedCoordsArray> outCoords; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 193 | for (int i = 0; i < this->pipeline().numFragmentStages(); i++) { |
| 194 | const GrFragmentProcessor* processor = this->pipeline().getFragmentStage(i).processor(); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 195 | SkSTArray<2, const GrCoordTransform*, true>& procCoords = fCoordTransforms.push_back(); |
| 196 | for (int t = 0; t < processor->numTransforms(); t++) { |
| 197 | procCoords.push_back(&processor->coordTransform(t)); |
| 198 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 199 | } |
| 200 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 201 | const GrPrimitiveProcessor& primProc = this->primitiveProcessor(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 202 | this->emitAndInstallProc(primProc, inputColor, inputCoverage); |
| 203 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 204 | fFragmentProcessors.reset(SkNEW(GrGLInstalledFragProcs)); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 205 | int numProcs = this->pipeline().numFragmentStages(); |
| 206 | this->emitAndInstallFragProcs(0, this->pipeline().numColorStages(), inputColor); |
| 207 | this->emitAndInstallFragProcs(this->pipeline().numColorStages(), numProcs, inputCoverage); |
| 208 | this->emitAndInstallXferProc(*this->pipeline().getXferProcessor(), *inputColor, *inputCoverage); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 209 | } |
| 210 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 211 | void GrGLProgramBuilder::emitAndInstallFragProcs(int procOffset, |
| 212 | int numProcs, |
| 213 | GrGLSLExpr4* inOut) { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 214 | for (int e = procOffset; e < numProcs; ++e) { |
| 215 | GrGLSLExpr4 output; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 216 | const GrPendingFragmentStage& stage = this->pipeline().getFragmentStage(e); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 217 | this->emitAndInstallProc(stage, e, *inOut, &output); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 218 | *inOut = output; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 219 | } |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 220 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 221 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 222 | void GrGLProgramBuilder::nameExpression(GrGLSLExpr4* output, const char* baseName) { |
| 223 | // create var to hold stage result. If we already have a valid output name, just use that |
| 224 | // otherwise create a new mangled one. This name is only valid if we are reordering stages |
| 225 | // and have to tell stage exactly where to put its output. |
| 226 | SkString outName; |
| 227 | if (output->isValid()) { |
| 228 | outName = output->c_str(); |
| 229 | } else { |
| 230 | this->nameVariable(&outName, '\0', baseName); |
| 231 | } |
| 232 | fFS.codeAppendf("vec4 %s;", outName.c_str()); |
| 233 | *output = outName; |
| 234 | } |
| 235 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 236 | // TODO Processors cannot output zeros because an empty string is all 1s |
| 237 | // the fix is to allow effects to take the GrGLSLExpr4 directly |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 238 | void GrGLProgramBuilder::emitAndInstallProc(const GrPendingFragmentStage& proc, |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 239 | int index, |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 240 | const GrGLSLExpr4& input, |
| 241 | GrGLSLExpr4* output) { |
| 242 | // Program builders have a bit of state we need to clear with each effect |
| 243 | AutoStageAdvance adv(this); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 244 | this->nameExpression(output, "output"); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 245 | |
| 246 | // Enclose custom code in a block to avoid namespace conflicts |
| 247 | SkString openBrace; |
egdaniel | ec03a46 | 2014-11-19 06:22:39 -0800 | [diff] [blame] | 248 | openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name()); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 249 | fFS.codeAppend(openBrace.c_str()); |
| 250 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 251 | this->emitAndInstallProc(proc, index, output->c_str(), input.isOnes() ? NULL : input.c_str()); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 252 | |
| 253 | fFS.codeAppend("}"); |
| 254 | } |
| 255 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 256 | void GrGLProgramBuilder::emitAndInstallProc(const GrPrimitiveProcessor& proc, |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 257 | GrGLSLExpr4* outputColor, |
| 258 | GrGLSLExpr4* outputCoverage) { |
| 259 | // Program builders have a bit of state we need to clear with each effect |
| 260 | AutoStageAdvance adv(this); |
| 261 | this->nameExpression(outputColor, "outputColor"); |
| 262 | this->nameExpression(outputCoverage, "outputCoverage"); |
| 263 | |
| 264 | // Enclose custom code in a block to avoid namespace conflicts |
| 265 | SkString openBrace; |
| 266 | openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name()); |
| 267 | fFS.codeAppend(openBrace.c_str()); |
| 268 | |
| 269 | this->emitAndInstallProc(proc, outputColor->c_str(), outputCoverage->c_str()); |
| 270 | |
| 271 | fFS.codeAppend("}"); |
| 272 | } |
| 273 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 274 | void GrGLProgramBuilder::emitAndInstallProc(const GrPendingFragmentStage& fs, |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 275 | int index, |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 276 | const char* outColor, |
| 277 | const char* inColor) { |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 278 | GrGLInstalledFragProc* ifp = SkNEW(GrGLInstalledFragProc); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 279 | |
joshualitt | 40d4bd8 | 2014-12-29 09:04:40 -0800 | [diff] [blame] | 280 | const GrFragmentProcessor& fp = *fs.processor(); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 281 | ifp->fGLProc.reset(fp.createGLInstance()); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 282 | |
| 283 | SkSTArray<4, GrGLProcessor::TextureSampler> samplers(fp.numTextures()); |
| 284 | this->emitSamplers(fp, &samplers, ifp); |
| 285 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 286 | ifp->fGLProc->emitCode(this, fp, outColor, inColor, fOutCoords[index], samplers); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 287 | |
| 288 | // We have to check that effects and the code they emit are consistent, ie if an effect |
| 289 | // asks for dst color, then the emit code needs to follow suit |
| 290 | verify(fp); |
| 291 | fFragmentProcessors->fProcs.push_back(ifp); |
| 292 | } |
| 293 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 294 | void GrGLProgramBuilder::emitAndInstallProc(const GrPrimitiveProcessor& gp, |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 295 | const char* outColor, |
| 296 | const char* outCoverage) { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 297 | SkASSERT(!fGeometryProcessor); |
| 298 | fGeometryProcessor = SkNEW(GrGLInstalledGeoProc); |
| 299 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 300 | const GrBatchTracker& bt = this->batchTracker(); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 301 | fGeometryProcessor->fGLProc.reset(gp.createGLInstance(bt, fGpu->glCaps())); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 302 | |
| 303 | SkSTArray<4, GrGLProcessor::TextureSampler> samplers(gp.numTextures()); |
| 304 | this->emitSamplers(gp, &samplers, fGeometryProcessor); |
| 305 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 306 | GrGLGeometryProcessor::EmitArgs args(this, gp, bt, outColor, outCoverage, samplers, |
| 307 | fCoordTransforms, &fOutCoords); |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 308 | fGeometryProcessor->fGLProc->emitCode(args); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 309 | |
| 310 | // We have to check that effects and the code they emit are consistent, ie if an effect |
| 311 | // asks for dst color, then the emit code needs to follow suit |
| 312 | verify(gp); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 313 | } |
| 314 | |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 315 | void GrGLProgramBuilder::emitAndInstallXferProc(const GrXferProcessor& xp, |
| 316 | const GrGLSLExpr4& colorIn, |
| 317 | const GrGLSLExpr4& coverageIn) { |
| 318 | // Program builders have a bit of state we need to clear with each effect |
| 319 | AutoStageAdvance adv(this); |
| 320 | |
| 321 | SkASSERT(!fXferProcessor); |
| 322 | fXferProcessor = SkNEW(GrGLInstalledXferProc); |
| 323 | |
| 324 | fXferProcessor->fGLProc.reset(xp.createGLInstance()); |
| 325 | |
| 326 | // Enable dual source secondary output if we have one |
| 327 | if (xp.hasSecondaryOutput()) { |
| 328 | fFS.enableSecondaryOutput(); |
| 329 | } |
| 330 | |
| 331 | // On any post 1.10 GLSL supporting GPU, we declare custom output |
| 332 | if (k110_GrGLSLGeneration != fFS.fProgramBuilder->gpu()->glslGeneration()) { |
| 333 | fFS.enableCustomOutput(); |
| 334 | } |
| 335 | |
| 336 | SkString openBrace; |
| 337 | openBrace.printf("{ // Xfer Processor: %s\n", xp.name()); |
| 338 | fFS.codeAppend(openBrace.c_str()); |
| 339 | |
| 340 | SkSTArray<4, GrGLProcessor::TextureSampler> samplers(xp.numTextures()); |
| 341 | this->emitSamplers(xp, &samplers, fXferProcessor); |
| 342 | |
| 343 | GrGLXferProcessor::EmitArgs args(this, xp, colorIn.c_str(), coverageIn.c_str(), |
| 344 | fFS.getPrimaryColorOutputName(), |
| 345 | fFS.getSecondaryColorOutputName(), samplers); |
| 346 | fXferProcessor->fGLProc->emitCode(args); |
| 347 | |
| 348 | // We have to check that effects and the code they emit are consistent, ie if an effect |
| 349 | // asks for dst color, then the emit code needs to follow suit |
| 350 | verify(xp); |
| 351 | fFS.codeAppend("}"); |
| 352 | } |
| 353 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 354 | void GrGLProgramBuilder::verify(const GrPrimitiveProcessor& gp) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 355 | SkASSERT(fFS.hasReadFragmentPosition() == gp.willReadFragmentPosition()); |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame] | 356 | } |
| 357 | |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 358 | void GrGLProgramBuilder::verify(const GrXferProcessor& xp) { |
egdaniel | 71e236c | 2015-01-20 06:34:51 -0800 | [diff] [blame] | 359 | SkASSERT(fFS.hasReadDstColor() == xp.willReadDstColor()); |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 360 | } |
| 361 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 362 | void GrGLProgramBuilder::verify(const GrFragmentProcessor& fp) { |
| 363 | SkASSERT(fFS.hasReadFragmentPosition() == fp.willReadFragmentPosition()); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 364 | } |
| 365 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 366 | template <class Proc> |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 367 | void GrGLProgramBuilder::emitSamplers(const GrProcessor& processor, |
| 368 | GrGLProcessor::TextureSamplerArray* outSamplers, |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 369 | GrGLInstalledProc<Proc>* ip) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 370 | int numTextures = processor.numTextures(); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 371 | ip->fSamplers.push_back_n(numTextures); |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 372 | SkString name; |
| 373 | for (int t = 0; t < numTextures; ++t) { |
| 374 | name.printf("Sampler%d", t); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 375 | ip->fSamplers[t].fUniform = this->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 376 | kSampler2D_GrSLType, kDefault_GrSLPrecision, |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 377 | name.c_str()); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 378 | SkNEW_APPEND_TO_TARRAY(outSamplers, GrGLProcessor::TextureSampler, |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 379 | (ip->fSamplers[t].fUniform, processor.textureAccess(t))); |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 383 | GrGLProgram* GrGLProgramBuilder::finalize() { |
| 384 | // verify we can get a program id |
| 385 | GrGLuint programID; |
| 386 | GL_CALL_RET(programID, CreateProgram()); |
| 387 | if (0 == programID) { |
| 388 | return NULL; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 389 | } |
| 390 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 391 | // compile shaders and bind attributes / uniforms |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 392 | SkTDArray<GrGLuint> shadersToDelete; |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 393 | |
| 394 | // Legacy nvpr will not compile with a vertex shader, but newer nvpr requires a dummy vertex |
| 395 | // shader |
joshualitt | 17e7314 | 2015-01-21 11:52:36 -0800 | [diff] [blame] | 396 | bool useNvpr = primitiveProcessor().isPathRendering(); |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 397 | if (!(useNvpr && fGpu->glCaps().nvprSupport() == GrGLCaps::kLegacy_NvprSupport)) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 398 | if (!fVS.compileAndAttachShaders(programID, &shadersToDelete)) { |
| 399 | this->cleanupProgram(programID, shadersToDelete); |
| 400 | return NULL; |
| 401 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 402 | |
| 403 | // Non fixed function NVPR actually requires a vertex shader to compile |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 404 | if (!useNvpr) { |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 405 | fVS.bindVertexAttributes(programID); |
| 406 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 407 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 408 | |
| 409 | if (!fFS.compileAndAttachShaders(programID, &shadersToDelete)) { |
| 410 | this->cleanupProgram(programID, shadersToDelete); |
| 411 | return NULL; |
| 412 | } |
| 413 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 414 | bool usingBindUniform = fGpu->glInterface()->fFunctions.fBindUniformLocation != NULL; |
| 415 | if (usingBindUniform) { |
| 416 | this->bindUniformLocations(programID); |
| 417 | } |
| 418 | fFS.bindFragmentShaderLocations(programID); |
| 419 | GL_CALL(LinkProgram(programID)); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 420 | |
| 421 | // Calling GetProgramiv is expensive in Chromium. Assume success in release builds. |
| 422 | bool checkLinked = !fGpu->ctxInfo().isChromium(); |
| 423 | #ifdef SK_DEBUG |
| 424 | checkLinked = true; |
| 425 | #endif |
| 426 | if (checkLinked) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 427 | checkLinkStatus(programID); |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame] | 428 | } |
joshualitt | db0d3ca | 2014-10-07 12:42:26 -0700 | [diff] [blame] | 429 | if (!usingBindUniform) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 430 | this->resolveUniformLocations(programID); |
joshualitt | db0d3ca | 2014-10-07 12:42:26 -0700 | [diff] [blame] | 431 | } |
| 432 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 433 | this->cleanupShaders(shadersToDelete); |
| 434 | |
| 435 | return this->createProgram(programID); |
| 436 | } |
| 437 | |
| 438 | void GrGLProgramBuilder::bindUniformLocations(GrGLuint programID) { |
| 439 | int count = fUniforms.count(); |
| 440 | for (int i = 0; i < count; ++i) { |
| 441 | GL_CALL(BindUniformLocation(programID, i, fUniforms[i].fVariable.c_str())); |
| 442 | fUniforms[i].fLocation = i; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | bool GrGLProgramBuilder::checkLinkStatus(GrGLuint programID) { |
| 447 | GrGLint linked = GR_GL_INIT_ZERO; |
| 448 | GL_CALL(GetProgramiv(programID, GR_GL_LINK_STATUS, &linked)); |
| 449 | if (!linked) { |
| 450 | GrGLint infoLen = GR_GL_INIT_ZERO; |
| 451 | GL_CALL(GetProgramiv(programID, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
| 452 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
| 453 | if (infoLen > 0) { |
| 454 | // retrieve length even though we don't need it to workaround |
| 455 | // bug in chrome cmd buffer param validation. |
| 456 | GrGLsizei length = GR_GL_INIT_ZERO; |
| 457 | GL_CALL(GetProgramInfoLog(programID, |
| 458 | infoLen+1, |
| 459 | &length, |
| 460 | (char*)log.get())); |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 461 | SkDebugf((char*)log.get()); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 462 | } |
| 463 | SkDEBUGFAIL("Error linking program"); |
| 464 | GL_CALL(DeleteProgram(programID)); |
| 465 | programID = 0; |
| 466 | } |
| 467 | return SkToBool(linked); |
| 468 | } |
| 469 | |
| 470 | void GrGLProgramBuilder::resolveUniformLocations(GrGLuint programID) { |
| 471 | int count = fUniforms.count(); |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 472 | for (int i = 0; i < count; ++i) { |
| 473 | GrGLint location; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 474 | GL_CALL_RET(location, GetUniformLocation(programID, fUniforms[i].fVariable.c_str())); |
| 475 | fUniforms[i].fLocation = location; |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 476 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 477 | } |
| 478 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 479 | void GrGLProgramBuilder::cleanupProgram(GrGLuint programID, const SkTDArray<GrGLuint>& shaderIDs) { |
| 480 | GL_CALL(DeleteProgram(programID)); |
| 481 | cleanupShaders(shaderIDs); |
| 482 | } |
| 483 | void GrGLProgramBuilder::cleanupShaders(const SkTDArray<GrGLuint>& shaderIDs) { |
| 484 | for (int i = 0; i < shaderIDs.count(); ++i) { |
| 485 | GL_CALL(DeleteShader(shaderIDs[i])); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | GrGLProgram* GrGLProgramBuilder::createProgram(GrGLuint programID) { |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 490 | return SkNEW_ARGS(GrGLProgram, (fGpu, this->desc(), fUniformHandles, programID, fUniforms, |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 491 | fGeometryProcessor, fXferProcessor, fFragmentProcessors.get())); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 492 | } |
| 493 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 494 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 495 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 496 | GrGLInstalledFragProcs::~GrGLInstalledFragProcs() { |
| 497 | int numProcs = fProcs.count(); |
| 498 | for (int e = 0; e < numProcs; ++e) { |
| 499 | SkDELETE(fProcs[e]); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 500 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 501 | } |