| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "gl/GrGLShaderBuilder.h" |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 9 | #include "gl/GrGLProgram.h" |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 10 | #include "gl/GrGLUniformHandle.h" |
| bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 11 | #include "GrCoordTransform.h" |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 12 | #include "GrDrawEffect.h" |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 13 | #include "GrGpuGL.h" |
| twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 14 | #include "GrTexture.h" |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 15 | #include "SkRTConf.h" |
| commit-bot@chromium.org | 933e65d | 2014-03-20 20:00:24 +0000 | [diff] [blame] | 16 | #include "SkTraceEvent.h" |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 17 | |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 18 | #define GL_CALL(X) GR_GL_CALL(this->gpu()->glInterface(), X) |
| 19 | #define GL_CALL_RET(R, X) GR_GL_CALL_RET(this->gpu()->glInterface(), R, X) |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 20 | |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 21 | // number of each input/output type in a single allocation block |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 22 | static const int kVarsPerBlock = 8; |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 23 | |
| 24 | // except FS outputs where we expect 2 at most. |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 25 | static const int kMaxFSOutputs = 2; |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 26 | |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 27 | // ES2 FS only guarantees mediump and lowp support |
| 28 | static const GrGLShaderVar::Precision kDefaultFragmentPrecision = GrGLShaderVar::kMedium_Precision; |
| 29 | |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 30 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 31 | |
| 32 | SK_CONF_DECLARE(bool, c_PrintShaders, "gpu.printShaders", false, |
| 33 | "Print the source code for all shaders generated."); |
| 34 | |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | |
| bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 37 | namespace { |
| twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 38 | |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 39 | inline const char* color_attribute_name() { return "aColor"; } |
| 40 | inline const char* coverage_attribute_name() { return "aCoverage"; } |
| 41 | inline const char* declared_color_output_name() { return "fsColorOut"; } |
| 42 | inline const char* dual_source_output_name() { return "dualSourceOut"; } |
| bsalomon@google.com | 2b1b8c0 | 2013-02-28 22:06:02 +0000 | [diff] [blame] | 43 | inline const char* sample_function_name(GrSLType type, GrGLSLGeneration glslGen) { |
| bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 44 | if (kVec2f_GrSLType == type) { |
| bsalomon@google.com | 2b1b8c0 | 2013-02-28 22:06:02 +0000 | [diff] [blame] | 45 | return glslGen >= k130_GrGLSLGeneration ? "texture" : "texture2D"; |
| bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 46 | } else { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 47 | SkASSERT(kVec3f_GrSLType == type); |
| bsalomon@google.com | 2b1b8c0 | 2013-02-28 22:06:02 +0000 | [diff] [blame] | 48 | return glslGen >= k130_GrGLSLGeneration ? "textureProj" : "texture2DProj"; |
| bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 49 | } |
| twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 52 | void append_texture_lookup(SkString* out, |
| 53 | GrGpuGL* gpu, |
| 54 | const char* samplerName, |
| 55 | const char* coordName, |
| 56 | uint32_t configComponentMask, |
| 57 | const char* swizzle, |
| 58 | GrSLType varyingType = kVec2f_GrSLType) { |
| 59 | SkASSERT(NULL != coordName); |
| twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 60 | |
| commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 61 | out->appendf("%s(%s, %s)", |
| 62 | sample_function_name(varyingType, gpu->glslGeneration()), |
| 63 | samplerName, |
| 64 | coordName); |
| 65 | |
| bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 66 | char mangledSwizzle[5]; |
| 67 | |
| 68 | // The swizzling occurs using texture params instead of shader-mangling if ARB_texture_swizzle |
| 69 | // is available. |
| commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 70 | if (!gpu->glCaps().textureSwizzleSupport() && |
| 71 | (kA_GrColorComponentFlag == configComponentMask)) { |
| 72 | char alphaChar = gpu->glCaps().textureRedSupport() ? 'r' : 'a'; |
| bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 73 | int i; |
| 74 | for (i = 0; '\0' != swizzle[i]; ++i) { |
| 75 | mangledSwizzle[i] = alphaChar; |
| 76 | } |
| 77 | mangledSwizzle[i] ='\0'; |
| 78 | swizzle = mangledSwizzle; |
| 79 | } |
| bsalomon@google.com | 73d5b2f | 2012-10-04 13:26:32 +0000 | [diff] [blame] | 80 | // For shader prettiness we omit the swizzle rather than appending ".rgba". |
| 81 | if (memcmp(swizzle, "rgba", 4)) { |
| commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 82 | out->appendf(".%s", swizzle); |
| bsalomon@google.com | 73d5b2f | 2012-10-04 13:26:32 +0000 | [diff] [blame] | 83 | } |
| twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 88 | static const char kDstCopyColorName[] = "_dstColor"; |
| twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 89 | |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 90 | /////////////////////////////////////////////////////////////////////////////// |
| tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 91 | |
| commit-bot@chromium.org | 0365261 | 2014-05-29 19:09:52 +0000 | [diff] [blame] | 92 | bool GrGLShaderBuilder::GenProgram(GrGpuGL* gpu, |
| commit-bot@chromium.org | 6eac42e | 2014-05-29 21:29:51 +0000 | [diff] [blame^] | 93 | GrGLUniformManager* uman, |
| commit-bot@chromium.org | 0365261 | 2014-05-29 19:09:52 +0000 | [diff] [blame] | 94 | const GrGLProgramDesc& desc, |
| 95 | const GrEffectStage* inColorStages[], |
| 96 | const GrEffectStage* inCoverageStages[], |
| 97 | GenProgramOutput* output) { |
| 98 | if (desc.getHeader().fHasVertexCode ||!gpu->shouldUseFixedFunctionTexturing()) { |
| 99 | GrGLFullShaderBuilder fullBuilder(gpu, uman, desc); |
| 100 | if (fullBuilder.genProgram(inColorStages, inCoverageStages, output)) { |
| 101 | output->fHasVS = true; |
| 102 | output->fUniformHandles.fViewMatrixUni = fullBuilder.getViewMatrixUniform(); |
| 103 | output->fUniformHandles.fRTAdjustmentUni = fullBuilder.getRTAdjustmentVecUniform(); |
| 104 | return true; |
| 105 | } |
| 106 | } else { |
| 107 | GrGLFragmentOnlyShaderBuilder fragmentOnlyBuilder(gpu, uman, desc); |
| 108 | if (fragmentOnlyBuilder.genProgram(inColorStages, inCoverageStages, output)) { |
| 109 | output->fHasVS = false; |
| 110 | output->fNumTexCoordSets = fragmentOnlyBuilder.getNumTexCoordSets(); |
| 111 | return true; |
| 112 | } |
| 113 | } |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | bool GrGLShaderBuilder::genProgram(const GrEffectStage* colorStages[], |
| 118 | const GrEffectStage* coverageStages[], |
| 119 | GenProgramOutput* output) { |
| 120 | const GrGLProgramDesc::KeyHeader& header = this->desc().getHeader(); |
| 121 | |
| 122 | // incoming color to current stage being processed. |
| 123 | GrGLSLExpr4 inColor = this->getInputColor(); |
| 124 | |
| 125 | output->fColorEffects = |
| 126 | this->createAndEmitEffects(colorStages, |
| 127 | this->desc().getEffectKeys(), |
| 128 | this->desc().numColorEffects(), |
| 129 | &inColor); |
| 130 | |
| 131 | /////////////////////////////////////////////////////////////////////////// |
| 132 | // compute the partial coverage |
| 133 | GrGLSLExpr4 inCoverage = this->getInputCoverage(); |
| 134 | |
| 135 | output->fCoverageEffects = |
| 136 | this->createAndEmitEffects(coverageStages, |
| 137 | this->desc().getEffectKeys() + this->desc().numColorEffects(), |
| 138 | this->desc().numCoverageEffects(), |
| 139 | &inCoverage); |
| 140 | |
| 141 | if (GrGLProgramDesc::CoverageOutputUsesSecondaryOutput(header.fCoverageOutput)) { |
| 142 | const char* secondaryOutputName = this->enableSecondaryOutput(); |
| 143 | |
| 144 | // default coeff to ones for kCoverage_DualSrcOutput |
| 145 | GrGLSLExpr4 coeff(1); |
| 146 | if (GrGLProgramDesc::kSecondaryCoverageISA_CoverageOutput == header.fCoverageOutput) { |
| 147 | // Get (1-A) into coeff |
| 148 | coeff = GrGLSLExpr4::VectorCast(GrGLSLExpr1(1) - inColor.a()); |
| 149 | } else if (GrGLProgramDesc::kSecondaryCoverageISC_CoverageOutput == |
| 150 | header.fCoverageOutput){ |
| 151 | // Get (1-RGBA) into coeff |
| 152 | coeff = GrGLSLExpr4(1) - inColor; |
| 153 | } |
| 154 | // Get coeff * coverage into modulate and then write that to the dual source output. |
| 155 | this->fsCodeAppendf("\t%s = %s;\n", secondaryOutputName, (coeff * inCoverage).c_str()); |
| 156 | } |
| 157 | |
| 158 | /////////////////////////////////////////////////////////////////////////// |
| 159 | // combine color and coverage as frag color |
| 160 | |
| 161 | // Get "color * coverage" into fragColor |
| 162 | GrGLSLExpr4 fragColor = inColor * inCoverage; |
| 163 | // Now tack on "+(1-coverage)dst onto the frag color if we were asked to do so. |
| 164 | if (GrGLProgramDesc::kCombineWithDst_CoverageOutput == header.fCoverageOutput) { |
| 165 | GrGLSLExpr4 dstCoeff = GrGLSLExpr4(1) - inCoverage; |
| 166 | |
| 167 | GrGLSLExpr4 dstContribution = dstCoeff * GrGLSLExpr4(this->dstColor()); |
| 168 | |
| 169 | fragColor = fragColor + dstContribution; |
| 170 | } |
| 171 | this->fsCodeAppendf("\t%s = %s;\n", this->getColorOutputName(), fragColor.c_str()); |
| 172 | |
| 173 | if (!this->finish(&output->fProgramID)) { |
| 174 | return false; |
| 175 | } |
| 176 | |
| commit-bot@chromium.org | 6eac42e | 2014-05-29 21:29:51 +0000 | [diff] [blame^] | 177 | output->fUniformHandles.fRTHeightUni = fRTHeightUniform; |
| 178 | output->fUniformHandles.fColorUni = fColorUniform; |
| 179 | output->fUniformHandles.fCoverageUni = fCoverageUniform; |
| 180 | output->fUniformHandles.fDstCopyTopLeftUni = fDstCopyTopLeftUniform; |
| 181 | output->fUniformHandles.fDstCopyScaleUni = fDstCopyScaleUniform; |
| 182 | output->fUniformHandles.fDstCopySamplerUni = fDstCopySamplerUniform; |
| commit-bot@chromium.org | 0365261 | 2014-05-29 19:09:52 +0000 | [diff] [blame] | 183 | |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | ////////////////////////////////////////////////////////////////////////////// |
| 188 | |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 189 | GrGLShaderBuilder::GrGLShaderBuilder(GrGpuGL* gpu, |
| commit-bot@chromium.org | 6eac42e | 2014-05-29 21:29:51 +0000 | [diff] [blame^] | 190 | GrGLUniformManager* uniformManager, |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 191 | const GrGLProgramDesc& desc) |
| commit-bot@chromium.org | 0365261 | 2014-05-29 19:09:52 +0000 | [diff] [blame] | 192 | : fDesc(desc) |
| 193 | , fGpu(gpu) |
| commit-bot@chromium.org | 6eac42e | 2014-05-29 21:29:51 +0000 | [diff] [blame^] | 194 | , fUniformManager(SkRef(uniformManager)) |
| bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 195 | , fFSFeaturesAddedMask(0) |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 196 | , fFSInputs(kVarsPerBlock) |
| 197 | , fFSOutputs(kMaxFSOutputs) |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 198 | , fUniforms(kVarsPerBlock) |
| bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 199 | , fSetupFragPosition(false) |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 200 | , fHasCustomColorOutput(false) |
| 201 | , fHasSecondaryOutput(false) |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 202 | , fTopLeftFragPosRead(kTopLeftFragPosRead_FragPosKey == desc.getHeader().fFragPosKey) { |
| 203 | |
| 204 | const GrGLProgramDesc::KeyHeader& header = desc.getHeader(); |
| skia.committer@gmail.com | e862d16 | 2012-10-31 02:01:18 +0000 | [diff] [blame] | 205 | |
| bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 206 | // Emit code to read the dst copy textue if necessary. |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 207 | if (kNoDstRead_DstReadKey != header.fDstReadKey && |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 208 | GrGLCaps::kNone_FBFetchType == fGpu->glCaps().fbFetchType()) { |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 209 | bool topDown = SkToBool(kTopLeftOrigin_DstReadKeyBit & header.fDstReadKey); |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 210 | const char* dstCopyTopLeftName; |
| 211 | const char* dstCopyCoordScaleName; |
| 212 | uint32_t configMask; |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 213 | if (SkToBool(kUseAlphaConfig_DstReadKeyBit & header.fDstReadKey)) { |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 214 | configMask = kA_GrColorComponentFlag; |
| 215 | } else { |
| 216 | configMask = kRGBA_GrColorComponentFlags; |
| 217 | } |
| commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 218 | fDstCopySamplerUniform = this->addUniform(kFragment_Visibility, |
| 219 | kSampler2D_GrSLType, |
| 220 | "DstCopySampler"); |
| commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 221 | fDstCopyTopLeftUniform = this->addUniform(kFragment_Visibility, |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 222 | kVec2f_GrSLType, |
| 223 | "DstCopyUpperLeft", |
| 224 | &dstCopyTopLeftName); |
| commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 225 | fDstCopyScaleUniform = this->addUniform(kFragment_Visibility, |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 226 | kVec2f_GrSLType, |
| 227 | "DstCopyCoordScale", |
| 228 | &dstCopyCoordScaleName); |
| 229 | const char* fragPos = this->fragmentPosition(); |
| 230 | this->fsCodeAppend("\t// Read color from copy of the destination.\n"); |
| 231 | this->fsCodeAppendf("\tvec2 _dstTexCoord = (%s.xy - %s) * %s;\n", |
| 232 | fragPos, dstCopyTopLeftName, dstCopyCoordScaleName); |
| 233 | if (!topDown) { |
| 234 | this->fsCodeAppend("\t_dstTexCoord.y = 1.0 - _dstTexCoord.y;\n"); |
| 235 | } |
| bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 236 | this->fsCodeAppendf("\tvec4 %s = ", kDstCopyColorName); |
| commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 237 | append_texture_lookup(&fFSCode, |
| 238 | fGpu, |
| 239 | this->getUniformCStr(fDstCopySamplerUniform), |
| 240 | "_dstTexCoord", |
| 241 | configMask, |
| 242 | "rgba"); |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 243 | this->fsCodeAppend(";\n\n"); |
| 244 | } |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 245 | |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 246 | if (GrGLProgramDesc::kUniform_ColorInput == header.fColorInput) { |
| 247 | const char* name; |
| 248 | fColorUniform = this->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 249 | kVec4f_GrSLType, "Color", &name); |
| commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 250 | fInputColor = GrGLSLExpr4(name); |
| commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 251 | } else if (GrGLProgramDesc::kSolidWhite_ColorInput == header.fColorInput) { |
| commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 252 | fInputColor = GrGLSLExpr4(1); |
| commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 253 | } else if (GrGLProgramDesc::kTransBlack_ColorInput == header.fColorInput) { |
| commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 254 | fInputColor = GrGLSLExpr4(0); |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 257 | if (GrGLProgramDesc::kUniform_ColorInput == header.fCoverageInput) { |
| 258 | const char* name; |
| 259 | fCoverageUniform = this->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 260 | kVec4f_GrSLType, "Coverage", &name); |
| commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 261 | fInputCoverage = GrGLSLExpr4(name); |
| commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 262 | } else if (GrGLProgramDesc::kSolidWhite_ColorInput == header.fCoverageInput) { |
| commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 263 | fInputCoverage = GrGLSLExpr4(1); |
| commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 264 | } else if (GrGLProgramDesc::kTransBlack_ColorInput == header.fCoverageInput) { |
| commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 265 | fInputCoverage = GrGLSLExpr4(0); |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | if (k110_GrGLSLGeneration != fGpu->glslGeneration()) { |
| 269 | fFSOutputs.push_back().set(kVec4f_GrSLType, |
| 270 | GrGLShaderVar::kOut_TypeModifier, |
| 271 | declared_color_output_name()); |
| 272 | fHasCustomColorOutput = true; |
| 273 | } |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 276 | bool GrGLShaderBuilder::enableFeature(GLSLFeature feature) { |
| 277 | switch (feature) { |
| 278 | case kStandardDerivatives_GLSLFeature: |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 279 | if (!fGpu->glCaps().shaderDerivativeSupport()) { |
| bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 280 | return false; |
| 281 | } |
| commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 282 | if (kGLES_GrGLStandard == fGpu->glStandard()) { |
| bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 283 | this->addFSFeature(1 << kStandardDerivatives_GLSLFeature, |
| 284 | "GL_OES_standard_derivatives"); |
| 285 | } |
| 286 | return true; |
| 287 | default: |
| commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 288 | SkFAIL("Unexpected GLSLFeature requested."); |
| bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 289 | return false; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | bool GrGLShaderBuilder::enablePrivateFeature(GLSLPrivateFeature feature) { |
| 294 | switch (feature) { |
| 295 | case kFragCoordConventions_GLSLPrivateFeature: |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 296 | if (!fGpu->glCaps().fragCoordConventionsSupport()) { |
| bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 297 | return false; |
| 298 | } |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 299 | if (fGpu->glslGeneration() < k150_GrGLSLGeneration) { |
| bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 300 | this->addFSFeature(1 << kFragCoordConventions_GLSLPrivateFeature, |
| 301 | "GL_ARB_fragment_coord_conventions"); |
| 302 | } |
| 303 | return true; |
| bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 304 | case kEXTShaderFramebufferFetch_GLSLPrivateFeature: |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 305 | if (GrGLCaps::kEXT_FBFetchType != fGpu->glCaps().fbFetchType()) { |
| bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 306 | return false; |
| 307 | } |
| 308 | this->addFSFeature(1 << kEXTShaderFramebufferFetch_GLSLPrivateFeature, |
| 309 | "GL_EXT_shader_framebuffer_fetch"); |
| 310 | return true; |
| 311 | case kNVShaderFramebufferFetch_GLSLPrivateFeature: |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 312 | if (GrGLCaps::kNV_FBFetchType != fGpu->glCaps().fbFetchType()) { |
| bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 313 | return false; |
| 314 | } |
| 315 | this->addFSFeature(1 << kNVShaderFramebufferFetch_GLSLPrivateFeature, |
| 316 | "GL_NV_shader_framebuffer_fetch"); |
| 317 | return true; |
| bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 318 | default: |
| commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 319 | SkFAIL("Unexpected GLSLPrivateFeature requested."); |
| bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 320 | return false; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | void GrGLShaderBuilder::addFSFeature(uint32_t featureBit, const char* extensionName) { |
| 325 | if (!(featureBit & fFSFeaturesAddedMask)) { |
| 326 | fFSExtensions.appendf("#extension %s: require\n", extensionName); |
| 327 | fFSFeaturesAddedMask |= featureBit; |
| 328 | } |
| 329 | } |
| 330 | |
| bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 331 | void GrGLShaderBuilder::nameVariable(SkString* out, char prefix, const char* name) { |
| 332 | if ('\0' == prefix) { |
| 333 | *out = name; |
| 334 | } else { |
| 335 | out->printf("%c%s", prefix, name); |
| 336 | } |
| 337 | if (fCodeStage.inStageCode()) { |
| 338 | if (out->endsWith('_')) { |
| 339 | // Names containing "__" are reserved. |
| 340 | out->append("x"); |
| 341 | } |
| 342 | out->appendf("_Stage%d", fCodeStage.stageIndex()); |
| 343 | } |
| 344 | } |
| 345 | |
| bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 346 | const char* GrGLShaderBuilder::dstColor() { |
| commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 347 | if (fCodeStage.inStageCode()) { |
| commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 348 | const GrEffectRef& effect = *fCodeStage.effectStage()->getEffect(); |
| commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 349 | if (!effect->willReadDstColor()) { |
| commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 350 | SkDEBUGFAIL("GrGLEffect asked for dst color but its generating GrEffect " |
| commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 351 | "did not request access."); |
| 352 | return ""; |
| 353 | } |
| 354 | } |
| bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 355 | static const char kFBFetchColorName[] = "gl_LastFragData[0]"; |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 356 | GrGLCaps::FBFetchType fetchType = fGpu->glCaps().fbFetchType(); |
| bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 357 | if (GrGLCaps::kEXT_FBFetchType == fetchType) { |
| 358 | SkAssertResult(this->enablePrivateFeature(kEXTShaderFramebufferFetch_GLSLPrivateFeature)); |
| 359 | return kFBFetchColorName; |
| 360 | } else if (GrGLCaps::kNV_FBFetchType == fetchType) { |
| 361 | SkAssertResult(this->enablePrivateFeature(kNVShaderFramebufferFetch_GLSLPrivateFeature)); |
| 362 | return kFBFetchColorName; |
| commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 363 | } else if (fDstCopySamplerUniform.isValid()) { |
| bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 364 | return kDstCopyColorName; |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 365 | } else { |
| commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 366 | return ""; |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 367 | } |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 370 | void GrGLShaderBuilder::appendTextureLookup(SkString* out, |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 371 | const GrGLShaderBuilder::TextureSampler& sampler, |
| bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 372 | const char* coordName, |
| 373 | GrSLType varyingType) const { |
| commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 374 | append_texture_lookup(out, |
| 375 | fGpu, |
| 376 | this->getUniformCStr(sampler.samplerUniform()), |
| 377 | coordName, |
| 378 | sampler.configComponentMask(), |
| 379 | sampler.swizzle(), |
| 380 | varyingType); |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 383 | void GrGLShaderBuilder::fsAppendTextureLookup(const GrGLShaderBuilder::TextureSampler& sampler, |
| 384 | const char* coordName, |
| 385 | GrSLType varyingType) { |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 386 | this->appendTextureLookup(&fFSCode, sampler, coordName, varyingType); |
| 387 | } |
| 388 | |
| commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 389 | void GrGLShaderBuilder::fsAppendTextureLookupAndModulate( |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 390 | const char* modulation, |
| 391 | const GrGLShaderBuilder::TextureSampler& sampler, |
| 392 | const char* coordName, |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 393 | GrSLType varyingType) { |
| bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 394 | SkString lookup; |
| bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 395 | this->appendTextureLookup(&lookup, sampler, coordName, varyingType); |
| commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 396 | fFSCode.append((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str()); |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 399 | GrGLShaderBuilder::DstReadKey GrGLShaderBuilder::KeyForDstRead(const GrTexture* dstCopy, |
| 400 | const GrGLCaps& caps) { |
| 401 | uint32_t key = kYesDstRead_DstReadKeyBit; |
| bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 402 | if (GrGLCaps::kNone_FBFetchType != caps.fbFetchType()) { |
| 403 | return key; |
| 404 | } |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 405 | SkASSERT(NULL != dstCopy); |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 406 | if (!caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(dstCopy->config())) { |
| 407 | // The fact that the config is alpha-only must be considered when generating code. |
| 408 | key |= kUseAlphaConfig_DstReadKeyBit; |
| 409 | } |
| 410 | if (kTopLeft_GrSurfaceOrigin == dstCopy->origin()) { |
| 411 | key |= kTopLeftOrigin_DstReadKeyBit; |
| 412 | } |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 413 | SkASSERT(static_cast<DstReadKey>(key) == key); |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 414 | return static_cast<DstReadKey>(key); |
| twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 417 | GrGLShaderBuilder::FragPosKey GrGLShaderBuilder::KeyForFragmentPosition(const GrRenderTarget* dst, |
| 418 | const GrGLCaps&) { |
| 419 | if (kTopLeft_GrSurfaceOrigin == dst->origin()) { |
| 420 | return kTopLeftFragPosRead_FragPosKey; |
| 421 | } else { |
| 422 | return kBottomLeftFragPosRead_FragPosKey; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | |
| bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 427 | const GrGLenum* GrGLShaderBuilder::GetTexParamSwizzle(GrPixelConfig config, const GrGLCaps& caps) { |
| 428 | if (caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(config)) { |
| 429 | if (caps.textureRedSupport()) { |
| 430 | static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED, GR_GL_RED, GR_GL_RED }; |
| 431 | return gRedSmear; |
| 432 | } else { |
| 433 | static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA, |
| 434 | GR_GL_ALPHA, GR_GL_ALPHA }; |
| 435 | return gAlphaSmear; |
| 436 | } |
| 437 | } else { |
| 438 | static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN, GR_GL_BLUE, GR_GL_ALPHA }; |
| 439 | return gStraight; |
| 440 | } |
| 441 | } |
| 442 | |
| bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 443 | GrGLUniformManager::UniformHandle GrGLShaderBuilder::addUniformArray(uint32_t visibility, |
| 444 | GrSLType type, |
| 445 | const char* name, |
| 446 | int count, |
| 447 | const char** outName) { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 448 | SkASSERT(name && strlen(name)); |
| commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 449 | SkDEBUGCODE(static const uint32_t kVisibilityMask = kVertex_Visibility | kFragment_Visibility); |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 450 | SkASSERT(0 == (~kVisibilityMask & visibility)); |
| 451 | SkASSERT(0 != visibility); |
| tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 452 | |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 453 | BuilderUniform& uni = fUniforms.push_back(); |
| commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 454 | UniformHandle h = GrGLUniformManager::UniformHandle::CreateFromUniformIndex(fUniforms.count() - 1); |
| commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 455 | SkDEBUGCODE(UniformHandle h2 =) |
| commit-bot@chromium.org | 6eac42e | 2014-05-29 21:29:51 +0000 | [diff] [blame^] | 456 | fUniformManager->appendUniform(type, count); |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 457 | // We expect the uniform manager to initially have no uniforms and that all uniforms are added |
| 458 | // by this function. Therefore, the handles should match. |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 459 | SkASSERT(h2 == h); |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 460 | uni.fVariable.setType(type); |
| 461 | uni.fVariable.setTypeModifier(GrGLShaderVar::kUniform_TypeModifier); |
| bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 462 | this->nameVariable(uni.fVariable.accessName(), 'u', name); |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 463 | uni.fVariable.setArrayCount(count); |
| 464 | uni.fVisibility = visibility; |
| tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 465 | |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 466 | // If it is visible in both the VS and FS, the precision must match. |
| 467 | // We declare a default FS precision, but not a default VS. So set the var |
| 468 | // to use the default FS precision. |
| commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 469 | if ((kVertex_Visibility | kFragment_Visibility) == visibility) { |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 470 | // the fragment and vertex precisions must match |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 471 | uni.fVariable.setPrecision(kDefaultFragmentPrecision); |
| tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 474 | if (NULL != outName) { |
| 475 | *outName = uni.fVariable.c_str(); |
| 476 | } |
| 477 | |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 478 | return h; |
| 479 | } |
| 480 | |
| bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 481 | SkString GrGLShaderBuilder::ensureFSCoords2D(const TransformedCoordsArray& coords, int index) { |
| 482 | if (kVec3f_GrSLType != coords[index].type()) { |
| 483 | SkASSERT(kVec2f_GrSLType == coords[index].type()); |
| 484 | return coords[index].getName(); |
| 485 | } |
| 486 | |
| 487 | SkString coords2D("coords2D"); |
| 488 | if (0 != index) { |
| 489 | coords2D.appendf("_%i", index); |
| 490 | } |
| 491 | this->fsCodeAppendf("\tvec2 %s = %s.xy / %s.z;", |
| 492 | coords2D.c_str(), coords[index].c_str(), coords[index].c_str()); |
| 493 | return coords2D; |
| 494 | } |
| 495 | |
| bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 496 | const char* GrGLShaderBuilder::fragmentPosition() { |
| commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 497 | if (fCodeStage.inStageCode()) { |
| commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 498 | const GrEffectRef& effect = *fCodeStage.effectStage()->getEffect(); |
| commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 499 | if (!effect->willReadFragmentPosition()) { |
| commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 500 | SkDEBUGFAIL("GrGLEffect asked for frag position but its generating GrEffect " |
| commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 501 | "did not request access."); |
| 502 | return ""; |
| 503 | } |
| 504 | } |
| commit-bot@chromium.org | 5f6ac03 | 2014-03-03 23:25:26 +0000 | [diff] [blame] | 505 | // We only declare "gl_FragCoord" when we're in the case where we want to use layout qualifiers |
| 506 | // to reverse y. Otherwise it isn't necessary and whether the "in" qualifier appears in the |
| 507 | // declaration varies in earlier GLSL specs. So it is simpler to omit it. |
| bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 508 | if (fTopLeftFragPosRead) { |
| commit-bot@chromium.org | 5f6ac03 | 2014-03-03 23:25:26 +0000 | [diff] [blame] | 509 | fSetupFragPosition = true; |
| bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 510 | return "gl_FragCoord"; |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 511 | } else if (fGpu->glCaps().fragCoordConventionsSupport()) { |
| bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 512 | if (!fSetupFragPosition) { |
| bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 513 | SkAssertResult(this->enablePrivateFeature(kFragCoordConventions_GLSLPrivateFeature)); |
| bsalomon@google.com | 5fa2107 | 2012-10-25 14:57:46 +0000 | [diff] [blame] | 514 | fFSInputs.push_back().set(kVec4f_GrSLType, |
| 515 | GrGLShaderVar::kIn_TypeModifier, |
| 516 | "gl_FragCoord", |
| 517 | GrGLShaderVar::kDefault_Precision, |
| 518 | GrGLShaderVar::kUpperLeft_Origin); |
| bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 519 | fSetupFragPosition = true; |
| 520 | } |
| skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 521 | return "gl_FragCoord"; |
| bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 522 | } else { |
| 523 | static const char* kCoordName = "fragCoordYDown"; |
| 524 | if (!fSetupFragPosition) { |
| bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 525 | // temporarily change the stage index because we're inserting non-stage code. |
| 526 | CodeStage::AutoStageRestore csar(&fCodeStage, NULL); |
| 527 | |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 528 | SkASSERT(!fRTHeightUniform.isValid()); |
| bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 529 | const char* rtHeightName; |
| skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 530 | |
| commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 531 | fRTHeightUniform = this->addUniform(kFragment_Visibility, |
| bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 532 | kFloat_GrSLType, |
| 533 | "RTHeight", |
| 534 | &rtHeightName); |
| skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 535 | |
| bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 536 | this->fFSCode.prependf("\tvec4 %s = vec4(gl_FragCoord.x, %s - gl_FragCoord.y, gl_FragCoord.zw);\n", |
| 537 | kCoordName, rtHeightName); |
| 538 | fSetupFragPosition = true; |
| 539 | } |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 540 | SkASSERT(fRTHeightUniform.isValid()); |
| bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 541 | return kCoordName; |
| 542 | } |
| 543 | } |
| 544 | |
| commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 545 | void GrGLShaderBuilder::fsEmitFunction(GrSLType returnType, |
| 546 | const char* name, |
| 547 | int argCnt, |
| 548 | const GrGLShaderVar* args, |
| 549 | const char* body, |
| 550 | SkString* outName) { |
| bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 551 | fFSFunctions.append(GrGLSLTypeString(returnType)); |
| bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 552 | this->nameVariable(outName, '\0', name); |
| bsalomon@google.com | 77cf460 | 2013-04-22 21:05:48 +0000 | [diff] [blame] | 553 | fFSFunctions.appendf(" %s", outName->c_str()); |
| bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 554 | fFSFunctions.append("("); |
| 555 | for (int i = 0; i < argCnt; ++i) { |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 556 | args[i].appendDecl(this->ctxInfo(), &fFSFunctions); |
| bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 557 | if (i < argCnt - 1) { |
| 558 | fFSFunctions.append(", "); |
| 559 | } |
| 560 | } |
| 561 | fFSFunctions.append(") {\n"); |
| 562 | fFSFunctions.append(body); |
| 563 | fFSFunctions.append("}\n\n"); |
| 564 | } |
| tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 565 | |
| bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 566 | namespace { |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 567 | |
| 568 | inline void append_default_precision_qualifier(GrGLShaderVar::Precision p, |
| commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 569 | GrGLStandard standard, |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 570 | SkString* str) { |
| 571 | // Desktop GLSL has added precision qualifiers but they don't do anything. |
| commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 572 | if (kGLES_GrGLStandard == standard) { |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 573 | switch (p) { |
| 574 | case GrGLShaderVar::kHigh_Precision: |
| 575 | str->append("precision highp float;\n"); |
| 576 | break; |
| 577 | case GrGLShaderVar::kMedium_Precision: |
| 578 | str->append("precision mediump float;\n"); |
| 579 | break; |
| 580 | case GrGLShaderVar::kLow_Precision: |
| 581 | str->append("precision lowp float;\n"); |
| 582 | break; |
| 583 | case GrGLShaderVar::kDefault_Precision: |
| commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 584 | SkFAIL("Default precision now allowed."); |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 585 | default: |
| commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 586 | SkFAIL("Unknown precision value."); |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | } |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 590 | } |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 591 | |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 592 | void GrGLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const { |
| bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 593 | for (int i = 0; i < vars.count(); ++i) { |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 594 | vars[i].appendDecl(this->ctxInfo(), out); |
| bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 595 | out->append(";\n"); |
| bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 596 | } |
| 597 | } |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 598 | |
| commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 599 | void GrGLShaderBuilder::appendUniformDecls(ShaderVisibility visibility, |
| 600 | SkString* out) const { |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 601 | for (int i = 0; i < fUniforms.count(); ++i) { |
| commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 602 | if (fUniforms[i].fVisibility & visibility) { |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 603 | fUniforms[i].fVariable.appendDecl(this->ctxInfo(), out); |
| bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 604 | out->append(";\n"); |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 605 | } |
| 606 | } |
| bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 609 | void GrGLShaderBuilder::createAndEmitEffects(GrGLProgramEffectsBuilder* programEffectsBuilder, |
| 610 | const GrEffectStage* effectStages[], |
| 611 | const EffectKey effectKeys[], |
| 612 | int effectCnt, |
| commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 613 | GrGLSLExpr4* fsInOutColor) { |
| bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 614 | bool effectEmitted = false; |
| bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 615 | |
| commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 616 | GrGLSLExpr4 inColor = *fsInOutColor; |
| 617 | GrGLSLExpr4 outColor; |
| bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 618 | |
| bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 619 | for (int e = 0; e < effectCnt; ++e) { |
| commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 620 | SkASSERT(NULL != effectStages[e] && NULL != effectStages[e]->getEffect()); |
| 621 | const GrEffectStage& stage = *effectStages[e]; |
| bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 622 | |
| commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 623 | CodeStage::AutoStageRestore csar(&fCodeStage, &stage); |
| bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 624 | |
| commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 625 | if (inColor.isZeros()) { |
| 626 | SkString inColorName; |
| 627 | |
| bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 628 | // Effects have no way to communicate zeros, they treat an empty string as ones. |
| commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 629 | this->nameVariable(&inColorName, '\0', "input"); |
| 630 | this->fsCodeAppendf("\tvec4 %s = %s;\n", inColorName.c_str(), inColor.c_str()); |
| 631 | inColor = inColorName; |
| bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | // create var to hold stage result |
| commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 635 | SkString outColorName; |
| 636 | this->nameVariable(&outColorName, '\0', "output"); |
| 637 | this->fsCodeAppendf("\tvec4 %s;\n", outColorName.c_str()); |
| 638 | outColor = outColorName; |
| 639 | |
| bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 640 | |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 641 | programEffectsBuilder->emitEffect(stage, |
| 642 | effectKeys[e], |
| 643 | outColor.c_str(), |
| commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 644 | inColor.isOnes() ? NULL : inColor.c_str(), |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 645 | fCodeStage.stageIndex()); |
| bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 646 | |
| 647 | inColor = outColor; |
| bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 648 | effectEmitted = true; |
| commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 651 | if (effectEmitted) { |
| 652 | *fsInOutColor = outColor; |
| 653 | } |
| bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 654 | } |
| commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 655 | |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 656 | const char* GrGLShaderBuilder::getColorOutputName() const { |
| 657 | return fHasCustomColorOutput ? declared_color_output_name() : "gl_FragColor"; |
| 658 | } |
| 659 | |
| 660 | const char* GrGLShaderBuilder::enableSecondaryOutput() { |
| 661 | if (!fHasSecondaryOutput) { |
| 662 | fFSOutputs.push_back().set(kVec4f_GrSLType, |
| 663 | GrGLShaderVar::kOut_TypeModifier, |
| 664 | dual_source_output_name()); |
| 665 | fHasSecondaryOutput = true; |
| 666 | } |
| 667 | return dual_source_output_name(); |
| 668 | } |
| 669 | |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 670 | bool GrGLShaderBuilder::finish(GrGLuint* outProgramId) { |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 671 | GrGLuint programId = 0; |
| 672 | GL_CALL_RET(programId, CreateProgram()); |
| 673 | if (!programId) { |
| 674 | return false; |
| 675 | } |
| 676 | |
| commit-bot@chromium.org | d328fb6 | 2014-02-14 00:03:38 +0000 | [diff] [blame] | 677 | SkTDArray<GrGLuint> shadersToDelete; |
| 678 | |
| 679 | if (!this->compileAndAttachShaders(programId, &shadersToDelete)) { |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 680 | GL_CALL(DeleteProgram(programId)); |
| 681 | return false; |
| 682 | } |
| 683 | |
| 684 | this->bindProgramLocations(programId); |
| commit-bot@chromium.org | 6eac42e | 2014-05-29 21:29:51 +0000 | [diff] [blame^] | 685 | if (fUniformManager->isUsingBindUniform()) { |
| 686 | fUniformManager->getUniformLocations(programId, fUniforms); |
| commit-bot@chromium.org | d3baf20 | 2013-11-07 22:06:08 +0000 | [diff] [blame] | 687 | } |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 688 | |
| 689 | GL_CALL(LinkProgram(programId)); |
| commit-bot@chromium.org | c9424b8 | 2013-10-30 20:03:16 +0000 | [diff] [blame] | 690 | |
| 691 | // Calling GetProgramiv is expensive in Chromium. Assume success in release builds. |
| 692 | bool checkLinked = !fGpu->ctxInfo().isChromium(); |
| 693 | #ifdef SK_DEBUG |
| 694 | checkLinked = true; |
| 695 | #endif |
| 696 | if (checkLinked) { |
| 697 | GrGLint linked = GR_GL_INIT_ZERO; |
| 698 | GL_CALL(GetProgramiv(programId, GR_GL_LINK_STATUS, &linked)); |
| 699 | if (!linked) { |
| 700 | GrGLint infoLen = GR_GL_INIT_ZERO; |
| 701 | GL_CALL(GetProgramiv(programId, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
| 702 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
| 703 | if (infoLen > 0) { |
| 704 | // retrieve length even though we don't need it to workaround |
| 705 | // bug in chrome cmd buffer param validation. |
| 706 | GrGLsizei length = GR_GL_INIT_ZERO; |
| 707 | GL_CALL(GetProgramInfoLog(programId, |
| 708 | infoLen+1, |
| 709 | &length, |
| 710 | (char*)log.get())); |
| 711 | GrPrintf((char*)log.get()); |
| 712 | } |
| 713 | SkDEBUGFAIL("Error linking program"); |
| 714 | GL_CALL(DeleteProgram(programId)); |
| 715 | return false; |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 716 | } |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| commit-bot@chromium.org | 6eac42e | 2014-05-29 21:29:51 +0000 | [diff] [blame^] | 719 | if (!fUniformManager->isUsingBindUniform()) { |
| 720 | fUniformManager->getUniformLocations(programId, fUniforms); |
| commit-bot@chromium.org | d3baf20 | 2013-11-07 22:06:08 +0000 | [diff] [blame] | 721 | } |
| commit-bot@chromium.org | d328fb6 | 2014-02-14 00:03:38 +0000 | [diff] [blame] | 722 | |
| 723 | for (int i = 0; i < shadersToDelete.count(); ++i) { |
| 724 | GL_CALL(DeleteShader(shadersToDelete[i])); |
| 725 | } |
| 726 | |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 727 | *outProgramId = programId; |
| 728 | return true; |
| 729 | } |
| 730 | |
| commit-bot@chromium.org | d328fb6 | 2014-02-14 00:03:38 +0000 | [diff] [blame] | 731 | // Compiles a GL shader and attaches it to a program. Returns the shader ID if |
| 732 | // successful, or 0 if not. |
| 733 | static GrGLuint attach_shader(const GrGLContext& glCtx, |
| 734 | GrGLuint programId, |
| 735 | GrGLenum type, |
| 736 | const SkString& shaderSrc) { |
| commit-bot@chromium.org | c9424b8 | 2013-10-30 20:03:16 +0000 | [diff] [blame] | 737 | const GrGLInterface* gli = glCtx.interface(); |
| 738 | |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 739 | GrGLuint shaderId; |
| 740 | GR_GL_CALL_RET(gli, shaderId, CreateShader(type)); |
| 741 | if (0 == shaderId) { |
| commit-bot@chromium.org | d328fb6 | 2014-02-14 00:03:38 +0000 | [diff] [blame] | 742 | return 0; |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | const GrGLchar* sourceStr = shaderSrc.c_str(); |
| robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 746 | GrGLint sourceLength = static_cast<GrGLint>(shaderSrc.size()); |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 747 | GR_GL_CALL(gli, ShaderSource(shaderId, 1, &sourceStr, &sourceLength)); |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 748 | GR_GL_CALL(gli, CompileShader(shaderId)); |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 749 | |
| commit-bot@chromium.org | c9424b8 | 2013-10-30 20:03:16 +0000 | [diff] [blame] | 750 | // Calling GetShaderiv in Chromium is quite expensive. Assume success in release builds. |
| commit-bot@chromium.org | b1854a8 | 2014-01-16 18:39:04 +0000 | [diff] [blame] | 751 | bool checkCompiled = !glCtx.isChromium(); |
| commit-bot@chromium.org | c9424b8 | 2013-10-30 20:03:16 +0000 | [diff] [blame] | 752 | #ifdef SK_DEBUG |
| 753 | checkCompiled = true; |
| 754 | #endif |
| 755 | if (checkCompiled) { |
| 756 | GrGLint compiled = GR_GL_INIT_ZERO; |
| 757 | GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_COMPILE_STATUS, &compiled)); |
| 758 | |
| 759 | if (!compiled) { |
| 760 | GrGLint infoLen = GR_GL_INIT_ZERO; |
| 761 | GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
| 762 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
| 763 | if (infoLen > 0) { |
| 764 | // retrieve length even though we don't need it to workaround bug in Chromium cmd |
| 765 | // buffer param validation. |
| 766 | GrGLsizei length = GR_GL_INIT_ZERO; |
| 767 | GR_GL_CALL(gli, GetShaderInfoLog(shaderId, infoLen+1, |
| 768 | &length, (char*)log.get())); |
| 769 | GrPrintf(shaderSrc.c_str()); |
| 770 | GrPrintf("\n%s", log.get()); |
| 771 | } |
| 772 | SkDEBUGFAIL("Shader compilation failed!"); |
| 773 | GR_GL_CALL(gli, DeleteShader(shaderId)); |
| commit-bot@chromium.org | d328fb6 | 2014-02-14 00:03:38 +0000 | [diff] [blame] | 774 | return 0; |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 775 | } |
| commit-bot@chromium.org | c9424b8 | 2013-10-30 20:03:16 +0000 | [diff] [blame] | 776 | } |
| 777 | if (c_PrintShaders) { |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 778 | GrPrintf(shaderSrc.c_str()); |
| 779 | GrPrintf("\n"); |
| 780 | } |
| 781 | |
| commit-bot@chromium.org | d328fb6 | 2014-02-14 00:03:38 +0000 | [diff] [blame] | 782 | // Attach the shader, but defer deletion until after we have linked the program. |
| 783 | // This works around a bug in the Android emulator's GLES2 wrapper which |
| 784 | // will immediately delete the shader object and free its memory even though it's |
| 785 | // attached to a program, which then causes glLinkProgram to fail. |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 786 | GR_GL_CALL(gli, AttachShader(programId, shaderId)); |
| commit-bot@chromium.org | d328fb6 | 2014-02-14 00:03:38 +0000 | [diff] [blame] | 787 | |
| 788 | return shaderId; |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| commit-bot@chromium.org | d328fb6 | 2014-02-14 00:03:38 +0000 | [diff] [blame] | 791 | bool GrGLShaderBuilder::compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>* shaderIds) const { |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 792 | SkString fragShaderSrc(GrGetGLSLVersionDecl(this->ctxInfo())); |
| 793 | fragShaderSrc.append(fFSExtensions); |
| 794 | append_default_precision_qualifier(kDefaultFragmentPrecision, |
| commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 795 | fGpu->glStandard(), |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 796 | &fragShaderSrc); |
| 797 | this->appendUniformDecls(kFragment_Visibility, &fragShaderSrc); |
| 798 | this->appendDecls(fFSInputs, &fragShaderSrc); |
| 799 | // We shouldn't have declared outputs on 1.10 |
| 800 | SkASSERT(k110_GrGLSLGeneration != fGpu->glslGeneration() || fFSOutputs.empty()); |
| 801 | this->appendDecls(fFSOutputs, &fragShaderSrc); |
| 802 | fragShaderSrc.append(fFSFunctions); |
| 803 | fragShaderSrc.append("void main() {\n"); |
| 804 | fragShaderSrc.append(fFSCode); |
| 805 | fragShaderSrc.append("}\n"); |
| commit-bot@chromium.org | d328fb6 | 2014-02-14 00:03:38 +0000 | [diff] [blame] | 806 | |
| 807 | GrGLuint fragShaderId = attach_shader(fGpu->glContext(), programId, GR_GL_FRAGMENT_SHADER, fragShaderSrc); |
| 808 | if (!fragShaderId) { |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 809 | return false; |
| 810 | } |
| 811 | |
| commit-bot@chromium.org | d328fb6 | 2014-02-14 00:03:38 +0000 | [diff] [blame] | 812 | *shaderIds->append() = fragShaderId; |
| 813 | |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 814 | return true; |
| 815 | } |
| 816 | |
| 817 | void GrGLShaderBuilder::bindProgramLocations(GrGLuint programId) const { |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 818 | if (fHasCustomColorOutput) { |
| 819 | GL_CALL(BindFragDataLocation(programId, 0, declared_color_output_name())); |
| 820 | } |
| 821 | if (fHasSecondaryOutput) { |
| 822 | GL_CALL(BindFragDataLocationIndexed(programId, 0, 1, dual_source_output_name())); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | const GrGLContextInfo& GrGLShaderBuilder::ctxInfo() const { |
| 827 | return fGpu->ctxInfo(); |
| 828 | } |
| 829 | |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 830 | //////////////////////////////////////////////////////////////////////////////// |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 831 | |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 832 | GrGLFullShaderBuilder::GrGLFullShaderBuilder(GrGpuGL* gpu, |
| commit-bot@chromium.org | 6eac42e | 2014-05-29 21:29:51 +0000 | [diff] [blame^] | 833 | GrGLUniformManager* uniformManager, |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 834 | const GrGLProgramDesc& desc) |
| 835 | : INHERITED(gpu, uniformManager, desc) |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 836 | , fVSAttrs(kVarsPerBlock) |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 837 | , fVSOutputs(kVarsPerBlock) |
| 838 | , fGSInputs(kVarsPerBlock) |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 839 | , fGSOutputs(kVarsPerBlock) { |
| 840 | |
| commit-bot@chromium.org | 0365261 | 2014-05-29 19:09:52 +0000 | [diff] [blame] | 841 | const GrGLProgramDesc::KeyHeader& header = this->desc().getHeader(); |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 842 | |
| 843 | fPositionVar = &fVSAttrs.push_back(); |
| 844 | fPositionVar->set(kVec2f_GrSLType, GrGLShaderVar::kAttribute_TypeModifier, "aPosition"); |
| 845 | if (-1 != header.fLocalCoordAttributeIndex) { |
| 846 | fLocalCoordsVar = &fVSAttrs.push_back(); |
| 847 | fLocalCoordsVar->set(kVec2f_GrSLType, |
| 848 | GrGLShaderVar::kAttribute_TypeModifier, |
| 849 | "aLocalCoords"); |
| 850 | } else { |
| 851 | fLocalCoordsVar = fPositionVar; |
| 852 | } |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 853 | |
| 854 | const char* viewMName; |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 855 | fViewMatrixUniform = this->addUniform(GrGLShaderBuilder::kVertex_Visibility, |
| 856 | kMat33f_GrSLType, "ViewM", &viewMName); |
| commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 857 | const char* rtAdjustName; |
| 858 | fRTAdustmentVecUniform = this->addUniform(GrGLShaderBuilder::kVertex_Visibility, |
| 859 | kVec4f_GrSLType, "rtAdjustment", &rtAdjustName); |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 860 | |
| commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 861 | // Transform the position into Skia's device coords. |
| 862 | this->vsCodeAppendf("\tvec3 pos3 = %s * vec3(%s, 1);\n", |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 863 | viewMName, fPositionVar->c_str()); |
| 864 | |
| commit-bot@chromium.org | 47c66dd | 2014-05-29 01:12:10 +0000 | [diff] [blame] | 865 | // Transform from Skia's device coords to GL's normalized device coords. |
| 866 | this->vsCodeAppendf( |
| 867 | "\tgl_Position = vec4(dot(pos3.xz, %s.xy), dot(pos3.yz, %s.zw), 0, pos3.z);\n", |
| 868 | rtAdjustName, rtAdjustName); |
| 869 | |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 870 | // we output point size in the GS if present |
| 871 | if (header.fEmitsPointSize |
| 872 | #if GR_GL_EXPERIMENTAL_GS |
| 873 | && !header.fExperimentalGS |
| 874 | #endif |
| 875 | ) { |
| 876 | this->vsCodeAppend("\tgl_PointSize = 1.0;\n"); |
| 877 | } |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 878 | |
| 879 | if (GrGLProgramDesc::kAttribute_ColorInput == header.fColorInput) { |
| 880 | this->addAttribute(kVec4f_GrSLType, color_attribute_name()); |
| 881 | const char *vsName, *fsName; |
| 882 | this->addVarying(kVec4f_GrSLType, "Color", &vsName, &fsName); |
| 883 | this->vsCodeAppendf("\t%s = %s;\n", vsName, color_attribute_name()); |
| 884 | this->setInputColor(fsName); |
| 885 | } |
| 886 | |
| 887 | if (GrGLProgramDesc::kAttribute_ColorInput == header.fCoverageInput) { |
| 888 | this->addAttribute(kVec4f_GrSLType, coverage_attribute_name()); |
| 889 | const char *vsName, *fsName; |
| 890 | this->addVarying(kVec4f_GrSLType, "Coverage", &vsName, &fsName); |
| 891 | this->vsCodeAppendf("\t%s = %s;\n", vsName, coverage_attribute_name()); |
| 892 | this->setInputCoverage(fsName); |
| 893 | } |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 894 | } |
| 895 | |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 896 | bool GrGLFullShaderBuilder::addAttribute(GrSLType type, const char* name) { |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 897 | for (int i = 0; i < fVSAttrs.count(); ++i) { |
| 898 | const GrGLShaderVar& attr = fVSAttrs[i]; |
| 899 | // if attribute already added, don't add it again |
| 900 | if (attr.getName().equals(name)) { |
| 901 | SkASSERT(attr.getType() == type); |
| 902 | return false; |
| 903 | } |
| 904 | } |
| 905 | fVSAttrs.push_back().set(type, |
| 906 | GrGLShaderVar::kAttribute_TypeModifier, |
| 907 | name); |
| 908 | return true; |
| 909 | } |
| 910 | |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 911 | bool GrGLFullShaderBuilder::addEffectAttribute(int attributeIndex, |
| 912 | GrSLType type, |
| 913 | const SkString& name) { |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 914 | if (!this->addAttribute(type, name.c_str())) { |
| 915 | return false; |
| 916 | } |
| 917 | |
| 918 | fEffectAttributes.push_back().set(attributeIndex, name); |
| 919 | return true; |
| 920 | } |
| 921 | |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 922 | void GrGLFullShaderBuilder::addVarying(GrSLType type, |
| 923 | const char* name, |
| 924 | const char** vsOutName, |
| 925 | const char** fsInName) { |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 926 | fVSOutputs.push_back(); |
| 927 | fVSOutputs.back().setType(type); |
| 928 | fVSOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 929 | this->nameVariable(fVSOutputs.back().accessName(), 'v', name); |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 930 | |
| 931 | if (vsOutName) { |
| 932 | *vsOutName = fVSOutputs.back().getName().c_str(); |
| 933 | } |
| 934 | // input to FS comes either from VS or GS |
| 935 | const SkString* fsName; |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 936 | #if GR_GL_EXPERIMENTAL_GS |
| commit-bot@chromium.org | 0365261 | 2014-05-29 19:09:52 +0000 | [diff] [blame] | 937 | if (this->desc().getHeader().fExperimentalGS) { |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 938 | // if we have a GS take each varying in as an array |
| 939 | // and output as non-array. |
| 940 | fGSInputs.push_back(); |
| 941 | fGSInputs.back().setType(type); |
| 942 | fGSInputs.back().setTypeModifier(GrGLShaderVar::kVaryingIn_TypeModifier); |
| 943 | fGSInputs.back().setUnsizedArray(); |
| 944 | *fGSInputs.back().accessName() = fVSOutputs.back().getName(); |
| 945 | fGSOutputs.push_back(); |
| 946 | fGSOutputs.back().setType(type); |
| 947 | fGSOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 948 | this->nameVariable(fGSOutputs.back().accessName(), 'g', name); |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 949 | fsName = fGSOutputs.back().accessName(); |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 950 | } else |
| 951 | #endif |
| 952 | { |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 953 | fsName = fVSOutputs.back().accessName(); |
| 954 | } |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 955 | this->fsInputAppend().set(type, GrGLShaderVar::kVaryingIn_TypeModifier, *fsName); |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 956 | if (fsInName) { |
| 957 | *fsInName = fsName->c_str(); |
| 958 | } |
| 959 | } |
| 960 | |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 961 | const SkString* GrGLFullShaderBuilder::getEffectAttributeName(int attributeIndex) const { |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 962 | const AttributePair* attribEnd = fEffectAttributes.end(); |
| 963 | for (const AttributePair* attrib = fEffectAttributes.begin(); attrib != attribEnd; ++attrib) { |
| commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 964 | if (attrib->fIndex == attributeIndex) { |
| 965 | return &attrib->fName; |
| 966 | } |
| skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 967 | } |
| commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 968 | |
| 969 | return NULL; |
| 970 | } |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 971 | |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 972 | GrGLProgramEffects* GrGLFullShaderBuilder::createAndEmitEffects( |
| 973 | const GrEffectStage* effectStages[], |
| 974 | const EffectKey effectKeys[], |
| 975 | int effectCnt, |
| commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 976 | GrGLSLExpr4* inOutFSColor) { |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 977 | |
| 978 | GrGLVertexProgramEffectsBuilder programEffectsBuilder(this, effectCnt); |
| 979 | this->INHERITED::createAndEmitEffects(&programEffectsBuilder, |
| 980 | effectStages, |
| 981 | effectKeys, |
| 982 | effectCnt, |
| commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 983 | inOutFSColor); |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 984 | return programEffectsBuilder.finish(); |
| 985 | } |
| 986 | |
| commit-bot@chromium.org | 0365261 | 2014-05-29 19:09:52 +0000 | [diff] [blame] | 987 | bool GrGLFullShaderBuilder::compileAndAttachShaders(GrGLuint programId, |
| 988 | SkTDArray<GrGLuint>* shaderIds) const { |
| commit-bot@chromium.org | c9424b8 | 2013-10-30 20:03:16 +0000 | [diff] [blame] | 989 | const GrGLContext& glCtx = this->gpu()->glContext(); |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 990 | SkString vertShaderSrc(GrGetGLSLVersionDecl(this->ctxInfo())); |
| 991 | this->appendUniformDecls(kVertex_Visibility, &vertShaderSrc); |
| 992 | this->appendDecls(fVSAttrs, &vertShaderSrc); |
| 993 | this->appendDecls(fVSOutputs, &vertShaderSrc); |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 994 | vertShaderSrc.append("void main() {\n"); |
| 995 | vertShaderSrc.append(fVSCode); |
| 996 | vertShaderSrc.append("}\n"); |
| commit-bot@chromium.org | d328fb6 | 2014-02-14 00:03:38 +0000 | [diff] [blame] | 997 | GrGLuint vertShaderId = attach_shader(glCtx, programId, GR_GL_VERTEX_SHADER, vertShaderSrc); |
| 998 | if (!vertShaderId) { |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 999 | return false; |
| 1000 | } |
| commit-bot@chromium.org | d328fb6 | 2014-02-14 00:03:38 +0000 | [diff] [blame] | 1001 | *shaderIds->append() = vertShaderId; |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 1002 | |
| 1003 | #if GR_GL_EXPERIMENTAL_GS |
| commit-bot@chromium.org | 0365261 | 2014-05-29 19:09:52 +0000 | [diff] [blame] | 1004 | if (this->desc().getHeader().fExperimentalGS) { |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 1005 | SkASSERT(this->ctxInfo().glslGeneration() >= k150_GrGLSLGeneration); |
| 1006 | SkString geomShaderSrc(GrGetGLSLVersionDecl(this->ctxInfo())); |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 1007 | geomShaderSrc.append("layout(triangles) in;\n" |
| 1008 | "layout(triangle_strip, max_vertices = 6) out;\n"); |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 1009 | this->appendDecls(fGSInputs, &geomShaderSrc); |
| 1010 | this->appendDecls(fGSOutputs, &geomShaderSrc); |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 1011 | geomShaderSrc.append("void main() {\n"); |
| 1012 | geomShaderSrc.append("\tfor (int i = 0; i < 3; ++i) {\n" |
| 1013 | "\t\tgl_Position = gl_in[i].gl_Position;\n"); |
| commit-bot@chromium.org | 0365261 | 2014-05-29 19:09:52 +0000 | [diff] [blame] | 1014 | if (this->desc().getHeader().fEmitsPointSize) { |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 1015 | geomShaderSrc.append("\t\tgl_PointSize = 1.0;\n"); |
| 1016 | } |
| 1017 | SkASSERT(fGSInputs.count() == fGSOutputs.count()); |
| 1018 | for (int i = 0; i < fGSInputs.count(); ++i) { |
| 1019 | geomShaderSrc.appendf("\t\t%s = %s[i];\n", |
| 1020 | fGSOutputs[i].getName().c_str(), |
| 1021 | fGSInputs[i].getName().c_str()); |
| 1022 | } |
| 1023 | geomShaderSrc.append("\t\tEmitVertex();\n" |
| 1024 | "\t}\n" |
| 1025 | "\tEndPrimitive();\n"); |
| 1026 | geomShaderSrc.append("}\n"); |
| commit-bot@chromium.org | d328fb6 | 2014-02-14 00:03:38 +0000 | [diff] [blame] | 1027 | GrGLuint geomShaderId = attach_shader(glCtx, programId, GR_GL_GEOMETRY_SHADER, geomShaderSrc); |
| 1028 | if (!geomShaderId) { |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 1029 | return false; |
| 1030 | } |
| commit-bot@chromium.org | d328fb6 | 2014-02-14 00:03:38 +0000 | [diff] [blame] | 1031 | *shaderIds->append() = geomShaderId; |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 1032 | } |
| 1033 | #endif |
| 1034 | |
| commit-bot@chromium.org | d328fb6 | 2014-02-14 00:03:38 +0000 | [diff] [blame] | 1035 | return this->INHERITED::compileAndAttachShaders(programId, shaderIds); |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
| commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 1038 | void GrGLFullShaderBuilder::bindProgramLocations(GrGLuint programId) const { |
| 1039 | this->INHERITED::bindProgramLocations(programId); |
| 1040 | |
| commit-bot@chromium.org | 0365261 | 2014-05-29 19:09:52 +0000 | [diff] [blame] | 1041 | const GrGLProgramDesc::KeyHeader& header = this->desc().getHeader(); |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 1042 | |
| 1043 | // Bind the attrib locations to same values for all shaders |
| 1044 | SkASSERT(-1 != header.fPositionAttributeIndex); |
| 1045 | GL_CALL(BindAttribLocation(programId, |
| 1046 | header.fPositionAttributeIndex, |
| 1047 | fPositionVar->c_str())); |
| 1048 | if (-1 != header.fLocalCoordAttributeIndex) { |
| 1049 | GL_CALL(BindAttribLocation(programId, |
| 1050 | header.fLocalCoordAttributeIndex, |
| 1051 | fLocalCoordsVar->c_str())); |
| 1052 | } |
| 1053 | if (-1 != header.fColorAttributeIndex) { |
| 1054 | GL_CALL(BindAttribLocation(programId, |
| 1055 | header.fColorAttributeIndex, |
| 1056 | color_attribute_name())); |
| 1057 | } |
| 1058 | if (-1 != header.fCoverageAttributeIndex) { |
| 1059 | GL_CALL(BindAttribLocation(programId, |
| 1060 | header.fCoverageAttributeIndex, |
| 1061 | coverage_attribute_name())); |
| 1062 | } |
| 1063 | |
| 1064 | const AttributePair* attribEnd = fEffectAttributes.end(); |
| 1065 | for (const AttributePair* attrib = fEffectAttributes.begin(); attrib != attribEnd; ++attrib) { |
| 1066 | GL_CALL(BindAttribLocation(programId, attrib->fIndex, attrib->fName.c_str())); |
| 1067 | } |
| 1068 | } |
| commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 1069 | |
| 1070 | //////////////////////////////////////////////////////////////////////////////// |
| 1071 | |
| 1072 | GrGLFragmentOnlyShaderBuilder::GrGLFragmentOnlyShaderBuilder(GrGpuGL* gpu, |
| commit-bot@chromium.org | 6eac42e | 2014-05-29 21:29:51 +0000 | [diff] [blame^] | 1073 | GrGLUniformManager* uniformManager, |
| commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 1074 | const GrGLProgramDesc& desc) |
| 1075 | : INHERITED(gpu, uniformManager, desc) |
| 1076 | , fNumTexCoordSets(0) { |
| 1077 | |
| 1078 | SkASSERT(!desc.getHeader().fHasVertexCode); |
| commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 1079 | SkASSERT(gpu->glCaps().pathRenderingSupport()); |
| commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 1080 | SkASSERT(GrGLProgramDesc::kAttribute_ColorInput != desc.getHeader().fColorInput); |
| 1081 | SkASSERT(GrGLProgramDesc::kAttribute_ColorInput != desc.getHeader().fCoverageInput); |
| 1082 | } |
| 1083 | |
| commit-bot@chromium.org | 8e919ad | 2013-10-21 14:48:23 +0000 | [diff] [blame] | 1084 | int GrGLFragmentOnlyShaderBuilder::addTexCoordSets(int count) { |
| 1085 | int firstFreeCoordSet = fNumTexCoordSets; |
| 1086 | fNumTexCoordSets += count; |
| 1087 | SkASSERT(gpu()->glCaps().maxFixedFunctionTextureCoords() >= fNumTexCoordSets); |
| 1088 | return firstFreeCoordSet; |
| 1089 | } |
| 1090 | |
| commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 1091 | GrGLProgramEffects* GrGLFragmentOnlyShaderBuilder::createAndEmitEffects( |
| 1092 | const GrEffectStage* effectStages[], |
| 1093 | const EffectKey effectKeys[], |
| 1094 | int effectCnt, |
| commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 1095 | GrGLSLExpr4* inOutFSColor) { |
| commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 1096 | |
| commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 1097 | GrGLPathTexGenProgramEffectsBuilder pathTexGenEffectsBuilder(this, |
| 1098 | effectCnt); |
| 1099 | this->INHERITED::createAndEmitEffects(&pathTexGenEffectsBuilder, |
| commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 1100 | effectStages, |
| 1101 | effectKeys, |
| 1102 | effectCnt, |
| commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 1103 | inOutFSColor); |
| commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 1104 | return pathTexGenEffectsBuilder.finish(); |
| commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 1105 | } |