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" |
joshualitt | db0d3ca | 2014-10-07 12:42:26 -0700 | [diff] [blame] | 13 | #include "../GrGpuGL.h" |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 14 | #include "GrCoordTransform.h" |
| 15 | #include "GrGLLegacyNvprProgramBuilder.h" |
| 16 | #include "GrGLNvprProgramBuilder.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 17 | #include "GrGLProgramBuilder.h" |
| 18 | #include "GrTexture.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 19 | #include "SkRTConf.h" |
| 20 | #include "SkTraceEvent.h" |
| 21 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 22 | #define GL_CALL(X) GR_GL_CALL(this->gpu()->glInterface(), X) |
| 23 | #define GL_CALL_RET(R, X) GR_GL_CALL_RET(this->gpu()->glInterface(), R, X) |
| 24 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 25 | ////////////////////////////////////////////////////////////////////////////// |
| 26 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 27 | const int GrGLProgramBuilder::kVarsPerBlock = 8; |
| 28 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame^] | 29 | GrGLProgram* GrGLProgramBuilder::CreateProgram(const GrOptDrawState& optState, GrGLGpu* gpu) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 30 | // create a builder. This will be handed off to effects so they can use it to add |
| 31 | // uniforms, varyings, textures, etc |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 32 | SkAutoTDelete<GrGLProgramBuilder> builder(CreateProgramBuilder(optState, |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 33 | optState.hasGeometryProcessor(), |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 34 | gpu)); |
| 35 | |
| 36 | GrGLProgramBuilder* pb = builder.get(); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 37 | const GrGLProgramDescBuilder::GLKeyHeader& header = GrGLProgramDescBuilder::GetHeader(pb->desc()); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 38 | |
| 39 | // emit code to read the dst copy texture, if necessary |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 40 | if (GrGLFragmentShaderBuilder::kNoDstRead_DstReadKey != header.fDstReadKey && |
| 41 | !gpu->glCaps().fbFetchSupport()) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 42 | pb->fFS.emitCodeToReadDstTexture(); |
| 43 | } |
| 44 | |
egdaniel | 37b4d86 | 2014-11-03 10:07:07 -0800 | [diff] [blame] | 45 | // 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] | 46 | // seed correctly here |
| 47 | GrGLSLExpr4 inputColor; |
| 48 | GrGLSLExpr4 inputCoverage; |
egdaniel | 37b4d86 | 2014-11-03 10:07:07 -0800 | [diff] [blame] | 49 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 50 | pb->emitAndInstallProcs(&inputColor, &inputCoverage); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 51 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 52 | return pb->finalize(); |
| 53 | } |
| 54 | |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 55 | GrGLProgramBuilder* GrGLProgramBuilder::CreateProgramBuilder(const GrOptDrawState& optState, |
| 56 | bool hasGeometryProcessor, |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame^] | 57 | GrGLGpu* gpu) { |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 58 | const GrProgramDesc& desc = optState.programDesc(); |
| 59 | if (GrGLProgramDescBuilder::GetHeader(desc).fUseNvpr) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 60 | SkASSERT(gpu->glCaps().pathRenderingSupport()); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 61 | SkASSERT(!hasGeometryProcessor); |
joshualitt | 0e60282 | 2014-10-28 10:27:44 -0700 | [diff] [blame] | 62 | if (gpu->glPathRendering()->texturingMode() == |
| 63 | GrGLPathRendering::FixedFunction_TexturingMode) { |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 64 | return SkNEW_ARGS(GrGLLegacyNvprProgramBuilder, (gpu, optState)); |
joshualitt | 0e60282 | 2014-10-28 10:27:44 -0700 | [diff] [blame] | 65 | } else { |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 66 | return SkNEW_ARGS(GrGLNvprProgramBuilder, (gpu, optState)); |
joshualitt | 0e60282 | 2014-10-28 10:27:44 -0700 | [diff] [blame] | 67 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 68 | } else { |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 69 | return SkNEW_ARGS(GrGLProgramBuilder, (gpu, optState)); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | ///////////////////////////////////////////////////////////////////////////// |
| 74 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame^] | 75 | GrGLProgramBuilder::GrGLProgramBuilder(GrGLGpu* gpu, const GrOptDrawState& optState) |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 76 | : fVS(this) |
| 77 | , fGS(this) |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 78 | , fFS(this, optState.programDesc().header().fFragPosKey) |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 79 | , fOutOfStage(true) |
| 80 | , fStageIndex(-1) |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 81 | , fGeometryProcessor(NULL) |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 82 | , fXferProcessor(NULL) |
egdaniel | 307796b | 2014-10-06 12:13:54 -0700 | [diff] [blame] | 83 | , fOptState(optState) |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 84 | , fDesc(optState.programDesc()) |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 85 | , fGpu(gpu) |
| 86 | , fUniforms(kVarsPerBlock) { |
| 87 | } |
| 88 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 89 | void GrGLProgramBuilder::addVarying(const char* name, |
| 90 | GrGLVarying* varying, |
bsalomon | c0bd648 | 2014-12-09 10:04:14 -0800 | [diff] [blame] | 91 | GrSLPrecision fsPrecision) { |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 92 | SkASSERT(varying); |
| 93 | if (varying->vsVarying()) { |
| 94 | fVS.addVarying(name, varying); |
| 95 | } |
| 96 | if (fOptState.hasGeometryProcessor() && fOptState.getGeometryProcessor()->willUseGeoShader()) { |
| 97 | fGS.addVarying(name, varying); |
| 98 | } |
| 99 | if (varying->fsVarying()) { |
| 100 | fFS.addVarying(varying, fsPrecision); |
| 101 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 102 | } |
| 103 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 104 | void GrGLProgramBuilder::addPassThroughAttribute(const GrGeometryProcessor::GrAttribute* input, |
| 105 | const char* output) { |
| 106 | GrSLType type = GrVertexAttribTypeToSLType(input->fType); |
| 107 | GrGLVertToFrag v(type); |
| 108 | this->addVarying(input->fName, &v); |
| 109 | fVS.codeAppendf("%s = %s;", v.vsOut(), input->fName); |
| 110 | fFS.codeAppendf("%s = %s;", output, v.fsIn()); |
| 111 | } |
| 112 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 113 | void GrGLProgramBuilder::nameVariable(SkString* out, char prefix, const char* name) { |
| 114 | if ('\0' == prefix) { |
| 115 | *out = name; |
| 116 | } else { |
| 117 | out->printf("%c%s", prefix, name); |
| 118 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 119 | if (!fOutOfStage) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 120 | if (out->endsWith('_')) { |
| 121 | // Names containing "__" are reserved. |
| 122 | out->append("x"); |
| 123 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 124 | out->appendf("_Stage%d", fStageIndex); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 128 | GrGLProgramDataManager::UniformHandle GrGLProgramBuilder::addUniformArray( |
| 129 | uint32_t visibility, |
| 130 | GrSLType type, |
| 131 | GrSLPrecision precision, |
| 132 | const char* name, |
| 133 | int count, |
| 134 | const char** outName) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 135 | SkASSERT(name && strlen(name)); |
| 136 | SkDEBUGCODE(static const uint32_t kVisibilityMask = kVertex_Visibility | kFragment_Visibility); |
| 137 | SkASSERT(0 == (~kVisibilityMask & visibility)); |
| 138 | SkASSERT(0 != visibility); |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 139 | SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type)); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 140 | |
| 141 | UniformInfo& uni = fUniforms.push_back(); |
| 142 | uni.fVariable.setType(type); |
| 143 | uni.fVariable.setTypeModifier(GrGLShaderVar::kUniform_TypeModifier); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 144 | // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use |
| 145 | // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB |
| 146 | // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then |
| 147 | // the names will mismatch. I think the correct solution is to have all GPs which need the |
| 148 | // uniform view matrix, they should upload the view matrix in their setData along with regular |
| 149 | // uniforms. |
| 150 | char prefix = 'u'; |
| 151 | if ('u' == name[0]) { |
| 152 | prefix = '\0'; |
| 153 | } |
| 154 | this->nameVariable(uni.fVariable.accessName(), prefix, name); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 155 | uni.fVariable.setArrayCount(count); |
| 156 | uni.fVisibility = visibility; |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 157 | uni.fVariable.setPrecision(precision); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 158 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 159 | if (outName) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 160 | *outName = uni.fVariable.c_str(); |
| 161 | } |
| 162 | return GrGLProgramDataManager::UniformHandle::CreateFromUniformIndex(fUniforms.count() - 1); |
| 163 | } |
| 164 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 165 | void GrGLProgramBuilder::appendUniformDecls(ShaderVisibility visibility, |
| 166 | SkString* out) const { |
| 167 | for (int i = 0; i < fUniforms.count(); ++i) { |
| 168 | if (fUniforms[i].fVisibility & visibility) { |
| 169 | fUniforms[i].fVariable.appendDecl(this->ctxInfo(), out); |
| 170 | out->append(";\n"); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 175 | const GrGLContextInfo& GrGLProgramBuilder::ctxInfo() const { |
| 176 | return fGpu->ctxInfo(); |
| 177 | } |
| 178 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 179 | void GrGLProgramBuilder::emitAndInstallProcs(GrGLSLExpr4* inputColor, GrGLSLExpr4* inputCoverage) { |
| 180 | if (fOptState.hasGeometryProcessor()) { |
| 181 | fVS.setupUniformViewMatrix(); |
| 182 | |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 183 | fVS.codeAppend("gl_PointSize = 1.0;"); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 184 | |
| 185 | // Setup position |
| 186 | // TODO it'd be possible to remove these from the vertexshader builder and have them |
| 187 | // be outputs from the emit call. We don't do this because emitargs is constant. It would |
| 188 | // be easy to change this though |
| 189 | fVS.codeAppendf("vec3 %s;", fVS.glPosition()); |
| 190 | fVS.codeAppendf("vec2 %s;", fVS.positionCoords()); |
| 191 | fVS.codeAppendf("vec2 %s;", fVS.localCoords()); |
| 192 | |
| 193 | const GrGeometryProcessor& gp = *fOptState.getGeometryProcessor(); |
| 194 | fVS.emitAttributes(gp); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 195 | } |
| 196 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 197 | const GrPrimitiveProcessor& primProc = *fOptState.getPrimitiveProcessor(); |
| 198 | this->emitAndInstallProc(primProc, inputColor, inputCoverage); |
| 199 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 200 | fFragmentProcessors.reset(SkNEW(GrGLInstalledFragProcs)); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 201 | int numProcs = fOptState.numFragmentStages(); |
| 202 | this->emitAndInstallFragProcs(0, fOptState.numColorStages(), inputColor); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 203 | this->emitAndInstallFragProcs(fOptState.numColorStages(), numProcs, inputCoverage); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 204 | |
| 205 | if (fOptState.hasGeometryProcessor()) { |
| 206 | fVS.transformToNormalizedDeviceSpace(); |
| 207 | } |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 208 | |
| 209 | this->emitAndInstallXferProc(*fOptState.getXferProcessor(), *inputColor, *inputCoverage); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 210 | } |
| 211 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 212 | void GrGLProgramBuilder::emitAndInstallFragProcs(int procOffset, |
| 213 | int numProcs, |
| 214 | GrGLSLExpr4* inOut) { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 215 | for (int e = procOffset; e < numProcs; ++e) { |
| 216 | GrGLSLExpr4 output; |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 217 | const GrPendingFragmentStage& stage = fOptState.getFragmentStage(e); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 218 | this->emitAndInstallProc(stage, e, *inOut, &output); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 219 | *inOut = output; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 220 | } |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 221 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 222 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 223 | void GrGLProgramBuilder::nameExpression(GrGLSLExpr4* output, const char* baseName) { |
| 224 | // create var to hold stage result. If we already have a valid output name, just use that |
| 225 | // otherwise create a new mangled one. This name is only valid if we are reordering stages |
| 226 | // and have to tell stage exactly where to put its output. |
| 227 | SkString outName; |
| 228 | if (output->isValid()) { |
| 229 | outName = output->c_str(); |
| 230 | } else { |
| 231 | this->nameVariable(&outName, '\0', baseName); |
| 232 | } |
| 233 | fFS.codeAppendf("vec4 %s;", outName.c_str()); |
| 234 | *output = outName; |
| 235 | } |
| 236 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 237 | // TODO Processors cannot output zeros because an empty string is all 1s |
| 238 | // the fix is to allow effects to take the GrGLSLExpr4 directly |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 239 | void GrGLProgramBuilder::emitAndInstallProc(const GrPendingFragmentStage& proc, |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 240 | int index, |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 241 | const GrGLSLExpr4& input, |
| 242 | GrGLSLExpr4* output) { |
| 243 | // Program builders have a bit of state we need to clear with each effect |
| 244 | AutoStageAdvance adv(this); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 245 | this->nameExpression(output, "output"); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 246 | |
| 247 | // Enclose custom code in a block to avoid namespace conflicts |
| 248 | SkString openBrace; |
egdaniel | ec03a46 | 2014-11-19 06:22:39 -0800 | [diff] [blame] | 249 | openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name()); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 250 | fFS.codeAppend(openBrace.c_str()); |
| 251 | |
joshualitt | 267ce48 | 2014-11-25 14:52:21 -0800 | [diff] [blame] | 252 | this->emitAndInstallProc(proc, output->c_str(), input.isOnes() ? NULL : input.c_str()); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 253 | |
| 254 | fFS.codeAppend("}"); |
| 255 | } |
| 256 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 257 | void GrGLProgramBuilder::emitAndInstallProc(const GrPrimitiveProcessor& proc, |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 258 | GrGLSLExpr4* outputColor, |
| 259 | GrGLSLExpr4* outputCoverage) { |
| 260 | // Program builders have a bit of state we need to clear with each effect |
| 261 | AutoStageAdvance adv(this); |
| 262 | this->nameExpression(outputColor, "outputColor"); |
| 263 | this->nameExpression(outputCoverage, "outputCoverage"); |
| 264 | |
| 265 | // Enclose custom code in a block to avoid namespace conflicts |
| 266 | SkString openBrace; |
| 267 | openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name()); |
| 268 | fFS.codeAppend(openBrace.c_str()); |
| 269 | |
| 270 | this->emitAndInstallProc(proc, outputColor->c_str(), outputCoverage->c_str()); |
| 271 | |
| 272 | fFS.codeAppend("}"); |
| 273 | } |
| 274 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 275 | void GrGLProgramBuilder::emitAndInstallProc(const GrPendingFragmentStage& fs, |
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 | |
| 280 | const GrFragmentProcessor& fp = *fs.getProcessor(); |
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 | |
| 286 | // Fragment processors can have coord transforms |
| 287 | SkSTArray<2, GrGLProcessor::TransformedCoords> coords(fp.numTransforms()); |
| 288 | this->emitTransforms(fs, &coords, ifp); |
| 289 | |
joshualitt | 267ce48 | 2014-11-25 14:52:21 -0800 | [diff] [blame] | 290 | ifp->fGLProc->emitCode(this, fp, outColor, inColor, coords, samplers); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 291 | |
| 292 | // We have to check that effects and the code they emit are consistent, ie if an effect |
| 293 | // asks for dst color, then the emit code needs to follow suit |
| 294 | verify(fp); |
| 295 | fFragmentProcessors->fProcs.push_back(ifp); |
| 296 | } |
| 297 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 298 | void GrGLProgramBuilder::emitAndInstallProc(const GrPrimitiveProcessor& gp, |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 299 | const char* outColor, |
| 300 | const char* outCoverage) { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 301 | SkASSERT(!fGeometryProcessor); |
| 302 | fGeometryProcessor = SkNEW(GrGLInstalledGeoProc); |
| 303 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 304 | const GrBatchTracker& bt = fOptState.getBatchTracker(); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 305 | fGeometryProcessor->fGLProc.reset(gp.createGLInstance(bt)); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 306 | |
| 307 | SkSTArray<4, GrGLProcessor::TextureSampler> samplers(gp.numTextures()); |
| 308 | this->emitSamplers(gp, &samplers, fGeometryProcessor); |
| 309 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 310 | GrGLGeometryProcessor::EmitArgs args(this, gp, bt, outColor, outCoverage, samplers); |
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) { |
| 362 | // TODO: Once will readDst is only xp enable this assert and remove it from the |
| 363 | // FragmentProcessor verify() |
| 364 | //SkASSERT(fFS.hasReadDstColor() == xp.willReadDstColor()); |
| 365 | } |
| 366 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 367 | void GrGLProgramBuilder::verify(const GrFragmentProcessor& fp) { |
| 368 | SkASSERT(fFS.hasReadFragmentPosition() == fp.willReadFragmentPosition()); |
| 369 | SkASSERT(fFS.hasReadDstColor() == fp.willReadDstColor()); |
| 370 | } |
| 371 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 372 | void GrGLProgramBuilder::emitTransforms(const GrPendingFragmentStage& stage, |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 373 | GrGLProcessor::TransformedCoordsArray* outCoords, |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 374 | GrGLInstalledFragProc* ifp) { |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 375 | const GrFragmentProcessor* processor = stage.getProcessor(); |
| 376 | int numTransforms = processor->numTransforms(); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 377 | ifp->fTransforms.push_back_n(numTransforms); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 378 | |
| 379 | for (int t = 0; t < numTransforms; t++) { |
| 380 | const char* uniName = "StageMatrix"; |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 381 | GrSLType varyingType = stage.isPerspectiveCoordTransform(t) ? kVec3f_GrSLType : |
| 382 | kVec2f_GrSLType; |
bsalomon | 54a57f5 | 2014-12-09 10:38:57 -0800 | [diff] [blame] | 383 | GrSLPrecision precision = processor->coordTransform(t).precision(); |
| 384 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 385 | SkString suffixedUniName; |
| 386 | if (0 != t) { |
| 387 | suffixedUniName.append(uniName); |
| 388 | suffixedUniName.appendf("_%i", t); |
| 389 | uniName = suffixedUniName.c_str(); |
| 390 | } |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 391 | ifp->fTransforms[t].fHandle = this->addUniform(GrGLProgramBuilder::kVertex_Visibility, |
bsalomon | 54a57f5 | 2014-12-09 10:38:57 -0800 | [diff] [blame] | 392 | kMat33f_GrSLType, precision, |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 393 | uniName, |
| 394 | &uniName).toShaderBuilderIndex(); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 395 | |
| 396 | const char* varyingName = "MatrixCoord"; |
| 397 | SkString suffixedVaryingName; |
| 398 | if (0 != t) { |
| 399 | suffixedVaryingName.append(varyingName); |
| 400 | suffixedVaryingName.appendf("_%i", t); |
| 401 | varyingName = suffixedVaryingName.c_str(); |
| 402 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 403 | |
| 404 | bool useLocalCoords = kLocal_GrCoordSet == processor->coordTransform(t).sourceCoords(); |
| 405 | const char* coords = useLocalCoords ? fVS.localCoords() : fVS.positionCoords(); |
| 406 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 407 | GrGLVertToFrag v(varyingType); |
bsalomon | 54a57f5 | 2014-12-09 10:38:57 -0800 | [diff] [blame] | 408 | this->addVarying(varyingName, &v, precision); |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 409 | fCoordVaryings.push_back(TransformVarying(v, uniName, coords)); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 410 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 411 | SkASSERT(kVec2f_GrSLType == varyingType || kVec3f_GrSLType == varyingType); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 412 | SkNEW_APPEND_TO_TARRAY(outCoords, GrGLProcessor::TransformedCoords, |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 413 | (SkString(v.fsIn()), varyingType)); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
| 417 | void GrGLProgramBuilder::emitSamplers(const GrProcessor& processor, |
| 418 | GrGLProcessor::TextureSamplerArray* outSamplers, |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 419 | GrGLInstalledProc* ip) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 420 | int numTextures = processor.numTextures(); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 421 | ip->fSamplers.push_back_n(numTextures); |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 422 | SkString name; |
| 423 | for (int t = 0; t < numTextures; ++t) { |
| 424 | name.printf("Sampler%d", t); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 425 | ip->fSamplers[t].fUniform = this->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 426 | kSampler2D_GrSLType, kDefault_GrSLPrecision, |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 427 | name.c_str()); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 428 | SkNEW_APPEND_TO_TARRAY(outSamplers, GrGLProcessor::TextureSampler, |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 429 | (ip->fSamplers[t].fUniform, processor.textureAccess(t))); |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 433 | GrGLProgram* GrGLProgramBuilder::finalize() { |
| 434 | // verify we can get a program id |
| 435 | GrGLuint programID; |
| 436 | GL_CALL_RET(programID, CreateProgram()); |
| 437 | if (0 == programID) { |
| 438 | return NULL; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 439 | } |
| 440 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 441 | // compile shaders and bind attributes / uniforms |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 442 | SkTDArray<GrGLuint> shadersToDelete; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 443 | if (!(GrGLProgramDescBuilder::GetHeader(fDesc).fUseNvpr && |
joshualitt | 0e60282 | 2014-10-28 10:27:44 -0700 | [diff] [blame] | 444 | fGpu->glPathRendering()->texturingMode() == |
| 445 | GrGLPathRendering::FixedFunction_TexturingMode)) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 446 | if (!fVS.compileAndAttachShaders(programID, &shadersToDelete)) { |
| 447 | this->cleanupProgram(programID, shadersToDelete); |
| 448 | return NULL; |
| 449 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 450 | |
| 451 | // Non fixed function NVPR actually requires a vertex shader to compile |
| 452 | if (fOptState.hasGeometryProcessor()) { |
| 453 | fVS.bindVertexAttributes(programID); |
| 454 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 455 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 456 | |
| 457 | if (!fFS.compileAndAttachShaders(programID, &shadersToDelete)) { |
| 458 | this->cleanupProgram(programID, shadersToDelete); |
| 459 | return NULL; |
| 460 | } |
| 461 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 462 | bool usingBindUniform = fGpu->glInterface()->fFunctions.fBindUniformLocation != NULL; |
| 463 | if (usingBindUniform) { |
| 464 | this->bindUniformLocations(programID); |
| 465 | } |
| 466 | fFS.bindFragmentShaderLocations(programID); |
| 467 | GL_CALL(LinkProgram(programID)); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 468 | |
| 469 | // Calling GetProgramiv is expensive in Chromium. Assume success in release builds. |
| 470 | bool checkLinked = !fGpu->ctxInfo().isChromium(); |
| 471 | #ifdef SK_DEBUG |
| 472 | checkLinked = true; |
| 473 | #endif |
| 474 | if (checkLinked) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 475 | checkLinkStatus(programID); |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame] | 476 | } |
joshualitt | db0d3ca | 2014-10-07 12:42:26 -0700 | [diff] [blame] | 477 | if (!usingBindUniform) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 478 | this->resolveUniformLocations(programID); |
joshualitt | db0d3ca | 2014-10-07 12:42:26 -0700 | [diff] [blame] | 479 | } |
| 480 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 481 | this->cleanupShaders(shadersToDelete); |
| 482 | |
| 483 | return this->createProgram(programID); |
| 484 | } |
| 485 | |
| 486 | void GrGLProgramBuilder::bindUniformLocations(GrGLuint programID) { |
| 487 | int count = fUniforms.count(); |
| 488 | for (int i = 0; i < count; ++i) { |
| 489 | GL_CALL(BindUniformLocation(programID, i, fUniforms[i].fVariable.c_str())); |
| 490 | fUniforms[i].fLocation = i; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | bool GrGLProgramBuilder::checkLinkStatus(GrGLuint programID) { |
| 495 | GrGLint linked = GR_GL_INIT_ZERO; |
| 496 | GL_CALL(GetProgramiv(programID, GR_GL_LINK_STATUS, &linked)); |
| 497 | if (!linked) { |
| 498 | GrGLint infoLen = GR_GL_INIT_ZERO; |
| 499 | GL_CALL(GetProgramiv(programID, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
| 500 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
| 501 | if (infoLen > 0) { |
| 502 | // retrieve length even though we don't need it to workaround |
| 503 | // bug in chrome cmd buffer param validation. |
| 504 | GrGLsizei length = GR_GL_INIT_ZERO; |
| 505 | GL_CALL(GetProgramInfoLog(programID, |
| 506 | infoLen+1, |
| 507 | &length, |
| 508 | (char*)log.get())); |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 509 | SkDebugf((char*)log.get()); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 510 | } |
| 511 | SkDEBUGFAIL("Error linking program"); |
| 512 | GL_CALL(DeleteProgram(programID)); |
| 513 | programID = 0; |
| 514 | } |
| 515 | return SkToBool(linked); |
| 516 | } |
| 517 | |
| 518 | void GrGLProgramBuilder::resolveUniformLocations(GrGLuint programID) { |
| 519 | int count = fUniforms.count(); |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 520 | for (int i = 0; i < count; ++i) { |
| 521 | GrGLint location; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 522 | GL_CALL_RET(location, GetUniformLocation(programID, fUniforms[i].fVariable.c_str())); |
| 523 | fUniforms[i].fLocation = location; |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 524 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 525 | } |
| 526 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 527 | void GrGLProgramBuilder::cleanupProgram(GrGLuint programID, const SkTDArray<GrGLuint>& shaderIDs) { |
| 528 | GL_CALL(DeleteProgram(programID)); |
| 529 | cleanupShaders(shaderIDs); |
| 530 | } |
| 531 | void GrGLProgramBuilder::cleanupShaders(const SkTDArray<GrGLuint>& shaderIDs) { |
| 532 | for (int i = 0; i < shaderIDs.count(); ++i) { |
| 533 | GL_CALL(DeleteShader(shaderIDs[i])); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | GrGLProgram* GrGLProgramBuilder::createProgram(GrGLuint programID) { |
| 538 | return SkNEW_ARGS(GrGLProgram, (fGpu, fDesc, fUniformHandles, programID, fUniforms, |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 539 | fGeometryProcessor, fXferProcessor, fFragmentProcessors.get())); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 540 | } |
| 541 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 542 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 543 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 544 | GrGLInstalledFragProcs::~GrGLInstalledFragProcs() { |
| 545 | int numProcs = fProcs.count(); |
| 546 | for (int e = 0; e < numProcs; ++e) { |
| 547 | SkDELETE(fProcs[e]); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 548 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 549 | } |