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