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" |
| 16 | #include "SkTrace.h" |
| 17 | |
| 18 | #define GL_CALL(X) GR_GL_CALL(fGpu->glInterface(), X) |
| 19 | #define GL_CALL_RET(R, X) GR_GL_CALL_RET(fGpu->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 | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 92 | GrGLShaderBuilder::GrGLShaderBuilder(GrGpuGL* gpu, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 93 | GrGLUniformManager& uniformManager, |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 94 | const GrGLProgramDesc& desc, |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 95 | bool needsVertexShader) |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 96 | : fUniforms(kVarsPerBlock) |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 97 | , fGpu(gpu) |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 98 | , fUniformManager(uniformManager) |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 99 | , fFSFeaturesAddedMask(0) |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 100 | , fFSInputs(kVarsPerBlock) |
| 101 | , fFSOutputs(kMaxFSOutputs) |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 102 | , fSetupFragPosition(false) |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 103 | , fKnownColorValue(kNone_GrSLConstantVec) |
| 104 | , fKnownCoverageValue(kNone_GrSLConstantVec) |
| 105 | , fHasCustomColorOutput(false) |
| 106 | , fHasSecondaryOutput(false) |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 107 | , fTopLeftFragPosRead(kTopLeftFragPosRead_FragPosKey == desc.getHeader().fFragPosKey) { |
| 108 | |
| 109 | const GrGLProgramDesc::KeyHeader& header = desc.getHeader(); |
skia.committer@gmail.com | e862d16 | 2012-10-31 02:01:18 +0000 | [diff] [blame] | 110 | |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 111 | if (needsVertexShader) { |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 112 | fVertexBuilder.reset(SkNEW_ARGS(VertexBuilder, (this, fGpu, desc))); |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 113 | } |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 114 | |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 115 | // Emit code to read the dst copy textue if necessary. |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 116 | if (kNoDstRead_DstReadKey != header.fDstReadKey && |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 117 | GrGLCaps::kNone_FBFetchType == fGpu->glCaps().fbFetchType()) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 118 | bool topDown = SkToBool(kTopLeftOrigin_DstReadKeyBit & header.fDstReadKey); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 119 | const char* dstCopyTopLeftName; |
| 120 | const char* dstCopyCoordScaleName; |
| 121 | uint32_t configMask; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 122 | if (SkToBool(kUseAlphaConfig_DstReadKeyBit & header.fDstReadKey)) { |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 123 | configMask = kA_GrColorComponentFlag; |
| 124 | } else { |
| 125 | configMask = kRGBA_GrColorComponentFlags; |
| 126 | } |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame^] | 127 | fDstCopySamplerUniform = this->addUniform(kFragment_Visibility, |
| 128 | kSampler2D_GrSLType, |
| 129 | "DstCopySampler"); |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 130 | fDstCopyTopLeftUniform = this->addUniform(kFragment_Visibility, |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 131 | kVec2f_GrSLType, |
| 132 | "DstCopyUpperLeft", |
| 133 | &dstCopyTopLeftName); |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 134 | fDstCopyScaleUniform = this->addUniform(kFragment_Visibility, |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 135 | kVec2f_GrSLType, |
| 136 | "DstCopyCoordScale", |
| 137 | &dstCopyCoordScaleName); |
| 138 | const char* fragPos = this->fragmentPosition(); |
| 139 | this->fsCodeAppend("\t// Read color from copy of the destination.\n"); |
| 140 | this->fsCodeAppendf("\tvec2 _dstTexCoord = (%s.xy - %s) * %s;\n", |
| 141 | fragPos, dstCopyTopLeftName, dstCopyCoordScaleName); |
| 142 | if (!topDown) { |
| 143 | this->fsCodeAppend("\t_dstTexCoord.y = 1.0 - _dstTexCoord.y;\n"); |
| 144 | } |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 145 | this->fsCodeAppendf("\tvec4 %s = ", kDstCopyColorName); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame^] | 146 | append_texture_lookup(&fFSCode, |
| 147 | fGpu, |
| 148 | this->getUniformCStr(fDstCopySamplerUniform), |
| 149 | "_dstTexCoord", |
| 150 | configMask, |
| 151 | "rgba"); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 152 | this->fsCodeAppend(";\n\n"); |
| 153 | } |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 154 | |
| 155 | switch (header.fColorInput) { |
| 156 | case GrGLProgramDesc::kAttribute_ColorInput: { |
| 157 | SkASSERT(NULL != fVertexBuilder.get()); |
| 158 | fVertexBuilder->addAttribute(kVec4f_GrSLType, color_attribute_name()); |
| 159 | const char *vsName, *fsName; |
| 160 | fVertexBuilder->addVarying(kVec4f_GrSLType, "Color", &vsName, &fsName); |
| 161 | fVertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsName, color_attribute_name()); |
| 162 | fInputColor = fsName; |
| 163 | break; |
| 164 | } |
| 165 | case GrGLProgramDesc::kUniform_ColorInput: { |
| 166 | const char* name; |
| 167 | fColorUniform = this->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 168 | kVec4f_GrSLType, "Color", &name); |
| 169 | fInputColor = name; |
| 170 | break; |
| 171 | } |
| 172 | case GrGLProgramDesc::kTransBlack_ColorInput: |
| 173 | fKnownColorValue = kZeros_GrSLConstantVec; |
| 174 | break; |
| 175 | case GrGLProgramDesc::kSolidWhite_ColorInput: |
| 176 | fKnownColorValue = kOnes_GrSLConstantVec; |
| 177 | break; |
| 178 | default: |
| 179 | GrCrash("Unknown color type."); |
| 180 | } |
| 181 | |
| 182 | switch (header.fCoverageInput) { |
| 183 | case GrGLProgramDesc::kAttribute_ColorInput: { |
| 184 | SkASSERT(NULL != fVertexBuilder.get()); |
| 185 | fVertexBuilder->addAttribute(kVec4f_GrSLType, coverage_attribute_name()); |
| 186 | const char *vsName, *fsName; |
| 187 | fVertexBuilder->addVarying(kVec4f_GrSLType, "Coverage", &vsName, &fsName); |
| 188 | fVertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsName, coverage_attribute_name()); |
| 189 | fInputCoverage = fsName; |
| 190 | break; |
| 191 | } |
| 192 | case GrGLProgramDesc::kUniform_ColorInput: { |
| 193 | const char* name; |
| 194 | fCoverageUniform = this->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 195 | kVec4f_GrSLType, "Coverage", &name); |
| 196 | fInputCoverage = name; |
| 197 | break; |
| 198 | } |
| 199 | case GrGLProgramDesc::kTransBlack_ColorInput: |
| 200 | fKnownCoverageValue = kZeros_GrSLConstantVec; |
| 201 | break; |
| 202 | case GrGLProgramDesc::kSolidWhite_ColorInput: |
| 203 | fKnownCoverageValue = kOnes_GrSLConstantVec; |
| 204 | break; |
| 205 | default: |
| 206 | GrCrash("Unknown coverage type."); |
| 207 | } |
| 208 | |
| 209 | if (k110_GrGLSLGeneration != fGpu->glslGeneration()) { |
| 210 | fFSOutputs.push_back().set(kVec4f_GrSLType, |
| 211 | GrGLShaderVar::kOut_TypeModifier, |
| 212 | declared_color_output_name()); |
| 213 | fHasCustomColorOutput = true; |
| 214 | } |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 215 | } |
| 216 | |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 217 | bool GrGLShaderBuilder::enableFeature(GLSLFeature feature) { |
| 218 | switch (feature) { |
| 219 | case kStandardDerivatives_GLSLFeature: |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 220 | if (!fGpu->glCaps().shaderDerivativeSupport()) { |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 221 | return false; |
| 222 | } |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 223 | if (kES_GrGLBinding == fGpu->glBinding()) { |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 224 | this->addFSFeature(1 << kStandardDerivatives_GLSLFeature, |
| 225 | "GL_OES_standard_derivatives"); |
| 226 | } |
| 227 | return true; |
| 228 | default: |
| 229 | GrCrash("Unexpected GLSLFeature requested."); |
| 230 | return false; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | bool GrGLShaderBuilder::enablePrivateFeature(GLSLPrivateFeature feature) { |
| 235 | switch (feature) { |
| 236 | case kFragCoordConventions_GLSLPrivateFeature: |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 237 | if (!fGpu->glCaps().fragCoordConventionsSupport()) { |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 238 | return false; |
| 239 | } |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 240 | if (fGpu->glslGeneration() < k150_GrGLSLGeneration) { |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 241 | this->addFSFeature(1 << kFragCoordConventions_GLSLPrivateFeature, |
| 242 | "GL_ARB_fragment_coord_conventions"); |
| 243 | } |
| 244 | return true; |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 245 | case kEXTShaderFramebufferFetch_GLSLPrivateFeature: |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 246 | if (GrGLCaps::kEXT_FBFetchType != fGpu->glCaps().fbFetchType()) { |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 247 | return false; |
| 248 | } |
| 249 | this->addFSFeature(1 << kEXTShaderFramebufferFetch_GLSLPrivateFeature, |
| 250 | "GL_EXT_shader_framebuffer_fetch"); |
| 251 | return true; |
| 252 | case kNVShaderFramebufferFetch_GLSLPrivateFeature: |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 253 | if (GrGLCaps::kNV_FBFetchType != fGpu->glCaps().fbFetchType()) { |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 254 | return false; |
| 255 | } |
| 256 | this->addFSFeature(1 << kNVShaderFramebufferFetch_GLSLPrivateFeature, |
| 257 | "GL_NV_shader_framebuffer_fetch"); |
| 258 | return true; |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 259 | default: |
| 260 | GrCrash("Unexpected GLSLPrivateFeature requested."); |
| 261 | return false; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | void GrGLShaderBuilder::addFSFeature(uint32_t featureBit, const char* extensionName) { |
| 266 | if (!(featureBit & fFSFeaturesAddedMask)) { |
| 267 | fFSExtensions.appendf("#extension %s: require\n", extensionName); |
| 268 | fFSFeaturesAddedMask |= featureBit; |
| 269 | } |
| 270 | } |
| 271 | |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 272 | void GrGLShaderBuilder::nameVariable(SkString* out, char prefix, const char* name) { |
| 273 | if ('\0' == prefix) { |
| 274 | *out = name; |
| 275 | } else { |
| 276 | out->printf("%c%s", prefix, name); |
| 277 | } |
| 278 | if (fCodeStage.inStageCode()) { |
| 279 | if (out->endsWith('_')) { |
| 280 | // Names containing "__" are reserved. |
| 281 | out->append("x"); |
| 282 | } |
| 283 | out->appendf("_Stage%d", fCodeStage.stageIndex()); |
| 284 | } |
| 285 | } |
| 286 | |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 287 | const char* GrGLShaderBuilder::dstColor() { |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 288 | if (fCodeStage.inStageCode()) { |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 289 | const GrEffectRef& effect = *fCodeStage.effectStage()->getEffect(); |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 290 | if (!effect->willReadDstColor()) { |
| 291 | GrDebugCrash("GrGLEffect asked for dst color but its generating GrEffect " |
| 292 | "did not request access."); |
| 293 | return ""; |
| 294 | } |
| 295 | } |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 296 | static const char kFBFetchColorName[] = "gl_LastFragData[0]"; |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 297 | GrGLCaps::FBFetchType fetchType = fGpu->glCaps().fbFetchType(); |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 298 | if (GrGLCaps::kEXT_FBFetchType == fetchType) { |
| 299 | SkAssertResult(this->enablePrivateFeature(kEXTShaderFramebufferFetch_GLSLPrivateFeature)); |
| 300 | return kFBFetchColorName; |
| 301 | } else if (GrGLCaps::kNV_FBFetchType == fetchType) { |
| 302 | SkAssertResult(this->enablePrivateFeature(kNVShaderFramebufferFetch_GLSLPrivateFeature)); |
| 303 | return kFBFetchColorName; |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame^] | 304 | } else if (fDstCopySamplerUniform.isValid()) { |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 305 | return kDstCopyColorName; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 306 | } else { |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 307 | return ""; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 308 | } |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 309 | } |
| 310 | |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 311 | void GrGLShaderBuilder::appendTextureLookup(SkString* out, |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 312 | const GrGLShaderBuilder::TextureSampler& sampler, |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 313 | const char* coordName, |
| 314 | GrSLType varyingType) const { |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame^] | 315 | append_texture_lookup(out, |
| 316 | fGpu, |
| 317 | this->getUniformCStr(sampler.samplerUniform()), |
| 318 | coordName, |
| 319 | sampler.configComponentMask(), |
| 320 | sampler.swizzle(), |
| 321 | varyingType); |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 322 | } |
| 323 | |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 324 | void GrGLShaderBuilder::fsAppendTextureLookup(const GrGLShaderBuilder::TextureSampler& sampler, |
| 325 | const char* coordName, |
| 326 | GrSLType varyingType) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 327 | this->appendTextureLookup(&fFSCode, sampler, coordName, varyingType); |
| 328 | } |
| 329 | |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 330 | void GrGLShaderBuilder::fsAppendTextureLookupAndModulate( |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 331 | const char* modulation, |
| 332 | const GrGLShaderBuilder::TextureSampler& sampler, |
| 333 | const char* coordName, |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 334 | GrSLType varyingType) { |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 335 | SkString lookup; |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 336 | this->appendTextureLookup(&lookup, sampler, coordName, varyingType); |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 337 | GrGLSLModulatef<4>(&fFSCode, modulation, lookup.c_str()); |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 338 | } |
| 339 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 340 | GrGLShaderBuilder::DstReadKey GrGLShaderBuilder::KeyForDstRead(const GrTexture* dstCopy, |
| 341 | const GrGLCaps& caps) { |
| 342 | uint32_t key = kYesDstRead_DstReadKeyBit; |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 343 | if (GrGLCaps::kNone_FBFetchType != caps.fbFetchType()) { |
| 344 | return key; |
| 345 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 346 | SkASSERT(NULL != dstCopy); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 347 | if (!caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(dstCopy->config())) { |
| 348 | // The fact that the config is alpha-only must be considered when generating code. |
| 349 | key |= kUseAlphaConfig_DstReadKeyBit; |
| 350 | } |
| 351 | if (kTopLeft_GrSurfaceOrigin == dstCopy->origin()) { |
| 352 | key |= kTopLeftOrigin_DstReadKeyBit; |
| 353 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 354 | SkASSERT(static_cast<DstReadKey>(key) == key); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 355 | return static_cast<DstReadKey>(key); |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 356 | } |
| 357 | |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 358 | GrGLShaderBuilder::FragPosKey GrGLShaderBuilder::KeyForFragmentPosition(const GrRenderTarget* dst, |
| 359 | const GrGLCaps&) { |
| 360 | if (kTopLeft_GrSurfaceOrigin == dst->origin()) { |
| 361 | return kTopLeftFragPosRead_FragPosKey; |
| 362 | } else { |
| 363 | return kBottomLeftFragPosRead_FragPosKey; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 368 | const GrGLenum* GrGLShaderBuilder::GetTexParamSwizzle(GrPixelConfig config, const GrGLCaps& caps) { |
| 369 | if (caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(config)) { |
| 370 | if (caps.textureRedSupport()) { |
| 371 | static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED, GR_GL_RED, GR_GL_RED }; |
| 372 | return gRedSmear; |
| 373 | } else { |
| 374 | static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA, |
| 375 | GR_GL_ALPHA, GR_GL_ALPHA }; |
| 376 | return gAlphaSmear; |
| 377 | } |
| 378 | } else { |
| 379 | static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN, GR_GL_BLUE, GR_GL_ALPHA }; |
| 380 | return gStraight; |
| 381 | } |
| 382 | } |
| 383 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 384 | GrGLUniformManager::UniformHandle GrGLShaderBuilder::addUniformArray(uint32_t visibility, |
| 385 | GrSLType type, |
| 386 | const char* name, |
| 387 | int count, |
| 388 | const char** outName) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 389 | SkASSERT(name && strlen(name)); |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 390 | SkDEBUGCODE(static const uint32_t kVisibilityMask = kVertex_Visibility | kFragment_Visibility); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 391 | SkASSERT(0 == (~kVisibilityMask & visibility)); |
| 392 | SkASSERT(0 != visibility); |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 393 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 394 | BuilderUniform& uni = fUniforms.push_back(); |
commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 395 | UniformHandle h = GrGLUniformManager::UniformHandle::CreateFromUniformIndex(fUniforms.count() - 1); |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 396 | SkDEBUGCODE(UniformHandle h2 =) |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 397 | fUniformManager.appendUniform(type, count); |
| 398 | // We expect the uniform manager to initially have no uniforms and that all uniforms are added |
| 399 | // by this function. Therefore, the handles should match. |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 400 | SkASSERT(h2 == h); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 401 | uni.fVariable.setType(type); |
| 402 | uni.fVariable.setTypeModifier(GrGLShaderVar::kUniform_TypeModifier); |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 403 | this->nameVariable(uni.fVariable.accessName(), 'u', name); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 404 | uni.fVariable.setArrayCount(count); |
| 405 | uni.fVisibility = visibility; |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 406 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 407 | // If it is visible in both the VS and FS, the precision must match. |
| 408 | // We declare a default FS precision, but not a default VS. So set the var |
| 409 | // to use the default FS precision. |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 410 | if ((kVertex_Visibility | kFragment_Visibility) == visibility) { |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 411 | // the fragment and vertex precisions must match |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 412 | uni.fVariable.setPrecision(kDefaultFragmentPrecision); |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 413 | } |
| 414 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 415 | if (NULL != outName) { |
| 416 | *outName = uni.fVariable.c_str(); |
| 417 | } |
| 418 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 419 | return h; |
| 420 | } |
| 421 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 422 | SkString GrGLShaderBuilder::ensureFSCoords2D(const TransformedCoordsArray& coords, int index) { |
| 423 | if (kVec3f_GrSLType != coords[index].type()) { |
| 424 | SkASSERT(kVec2f_GrSLType == coords[index].type()); |
| 425 | return coords[index].getName(); |
| 426 | } |
| 427 | |
| 428 | SkString coords2D("coords2D"); |
| 429 | if (0 != index) { |
| 430 | coords2D.appendf("_%i", index); |
| 431 | } |
| 432 | this->fsCodeAppendf("\tvec2 %s = %s.xy / %s.z;", |
| 433 | coords2D.c_str(), coords[index].c_str(), coords[index].c_str()); |
| 434 | return coords2D; |
| 435 | } |
| 436 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 437 | const char* GrGLShaderBuilder::fragmentPosition() { |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 438 | if (fCodeStage.inStageCode()) { |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 439 | const GrEffectRef& effect = *fCodeStage.effectStage()->getEffect(); |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 440 | if (!effect->willReadFragmentPosition()) { |
| 441 | GrDebugCrash("GrGLEffect asked for frag position but its generating GrEffect " |
| 442 | "did not request access."); |
| 443 | return ""; |
| 444 | } |
| 445 | } |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 446 | if (fTopLeftFragPosRead) { |
| 447 | if (!fSetupFragPosition) { |
| 448 | fFSInputs.push_back().set(kVec4f_GrSLType, |
| 449 | GrGLShaderVar::kIn_TypeModifier, |
| 450 | "gl_FragCoord", |
| 451 | GrGLShaderVar::kDefault_Precision); |
| 452 | fSetupFragPosition = true; |
| 453 | } |
| 454 | return "gl_FragCoord"; |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 455 | } else if (fGpu->glCaps().fragCoordConventionsSupport()) { |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 456 | if (!fSetupFragPosition) { |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 457 | SkAssertResult(this->enablePrivateFeature(kFragCoordConventions_GLSLPrivateFeature)); |
bsalomon@google.com | 5fa2107 | 2012-10-25 14:57:46 +0000 | [diff] [blame] | 458 | fFSInputs.push_back().set(kVec4f_GrSLType, |
| 459 | GrGLShaderVar::kIn_TypeModifier, |
| 460 | "gl_FragCoord", |
| 461 | GrGLShaderVar::kDefault_Precision, |
| 462 | GrGLShaderVar::kUpperLeft_Origin); |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 463 | fSetupFragPosition = true; |
| 464 | } |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 465 | return "gl_FragCoord"; |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 466 | } else { |
| 467 | static const char* kCoordName = "fragCoordYDown"; |
| 468 | if (!fSetupFragPosition) { |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 469 | // temporarily change the stage index because we're inserting non-stage code. |
| 470 | CodeStage::AutoStageRestore csar(&fCodeStage, NULL); |
| 471 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 472 | SkASSERT(!fRTHeightUniform.isValid()); |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 473 | const char* rtHeightName; |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 474 | |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 475 | fRTHeightUniform = this->addUniform(kFragment_Visibility, |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 476 | kFloat_GrSLType, |
| 477 | "RTHeight", |
| 478 | &rtHeightName); |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 479 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 480 | this->fFSCode.prependf("\tvec4 %s = vec4(gl_FragCoord.x, %s - gl_FragCoord.y, gl_FragCoord.zw);\n", |
| 481 | kCoordName, rtHeightName); |
| 482 | fSetupFragPosition = true; |
| 483 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 484 | SkASSERT(fRTHeightUniform.isValid()); |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 485 | return kCoordName; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 490 | void GrGLShaderBuilder::fsEmitFunction(GrSLType returnType, |
| 491 | const char* name, |
| 492 | int argCnt, |
| 493 | const GrGLShaderVar* args, |
| 494 | const char* body, |
| 495 | SkString* outName) { |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 496 | fFSFunctions.append(GrGLSLTypeString(returnType)); |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 497 | this->nameVariable(outName, '\0', name); |
bsalomon@google.com | 77cf460 | 2013-04-22 21:05:48 +0000 | [diff] [blame] | 498 | fFSFunctions.appendf(" %s", outName->c_str()); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 499 | fFSFunctions.append("("); |
| 500 | for (int i = 0; i < argCnt; ++i) { |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 501 | args[i].appendDecl(this->ctxInfo(), &fFSFunctions); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 502 | if (i < argCnt - 1) { |
| 503 | fFSFunctions.append(", "); |
| 504 | } |
| 505 | } |
| 506 | fFSFunctions.append(") {\n"); |
| 507 | fFSFunctions.append(body); |
| 508 | fFSFunctions.append("}\n\n"); |
| 509 | } |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 510 | |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 511 | namespace { |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 512 | |
| 513 | inline void append_default_precision_qualifier(GrGLShaderVar::Precision p, |
| 514 | GrGLBinding binding, |
| 515 | SkString* str) { |
| 516 | // Desktop GLSL has added precision qualifiers but they don't do anything. |
bsalomon@google.com | 791816a | 2013-08-15 18:54:39 +0000 | [diff] [blame] | 517 | if (kES_GrGLBinding == binding) { |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 518 | switch (p) { |
| 519 | case GrGLShaderVar::kHigh_Precision: |
| 520 | str->append("precision highp float;\n"); |
| 521 | break; |
| 522 | case GrGLShaderVar::kMedium_Precision: |
| 523 | str->append("precision mediump float;\n"); |
| 524 | break; |
| 525 | case GrGLShaderVar::kLow_Precision: |
| 526 | str->append("precision lowp float;\n"); |
| 527 | break; |
| 528 | case GrGLShaderVar::kDefault_Precision: |
| 529 | GrCrash("Default precision now allowed."); |
| 530 | default: |
| 531 | GrCrash("Unknown precision value."); |
| 532 | } |
| 533 | } |
| 534 | } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 535 | } |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 536 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 537 | void GrGLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 538 | for (int i = 0; i < vars.count(); ++i) { |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 539 | vars[i].appendDecl(this->ctxInfo(), out); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 540 | out->append(";\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 543 | |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 544 | void GrGLShaderBuilder::appendUniformDecls(ShaderVisibility visibility, |
| 545 | SkString* out) const { |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 546 | for (int i = 0; i < fUniforms.count(); ++i) { |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 547 | if (fUniforms[i].fVisibility & visibility) { |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 548 | fUniforms[i].fVariable.appendDecl(this->ctxInfo(), out); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 549 | out->append(";\n"); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 550 | } |
| 551 | } |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 552 | } |
| 553 | |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame^] | 554 | GrGLProgramEffects* GrGLShaderBuilder::createAndEmitEffects( |
| 555 | const GrEffectStage* effectStages[], |
| 556 | const EffectKey effectKeys[], |
| 557 | int effectCnt, |
| 558 | SkString* fsInOutColor, |
| 559 | GrSLConstantVec* fsInOutColorKnownValue) { |
| 560 | |
| 561 | GrGLProgramEffectsBuilder programEffectsBuilder(this, effectCnt); |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 562 | bool effectEmitted = false; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 563 | |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 564 | SkString inColor = *fsInOutColor; |
| 565 | SkString outColor; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 566 | |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 567 | for (int e = 0; e < effectCnt; ++e) { |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 568 | SkASSERT(NULL != effectStages[e] && NULL != effectStages[e]->getEffect()); |
| 569 | const GrEffectStage& stage = *effectStages[e]; |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 570 | |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 571 | CodeStage::AutoStageRestore csar(&fCodeStage, &stage); |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 572 | |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 573 | if (kZeros_GrSLConstantVec == *fsInOutColorKnownValue) { |
| 574 | // Effects have no way to communicate zeros, they treat an empty string as ones. |
| 575 | this->nameVariable(&inColor, '\0', "input"); |
| 576 | this->fsCodeAppendf("\tvec4 %s = %s;\n", inColor.c_str(), GrGLSLZerosVecf(4)); |
| 577 | } |
| 578 | |
| 579 | // create var to hold stage result |
| 580 | this->nameVariable(&outColor, '\0', "output"); |
| 581 | this->fsCodeAppendf("\tvec4 %s;\n", outColor.c_str()); |
| 582 | |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame^] | 583 | programEffectsBuilder.emitEffect(stage, |
| 584 | effectKeys[e], |
| 585 | outColor.c_str(), |
| 586 | inColor.isEmpty() ? NULL : inColor.c_str(), |
| 587 | fCodeStage.stageIndex()); |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 588 | |
| 589 | inColor = outColor; |
| 590 | *fsInOutColorKnownValue = kNone_GrSLConstantVec; |
| 591 | effectEmitted = true; |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 592 | } |
| 593 | |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 594 | if (effectEmitted) { |
| 595 | *fsInOutColor = outColor; |
| 596 | } |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame^] | 597 | |
| 598 | return programEffectsBuilder.finish(); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 599 | } |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 600 | |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 601 | const char* GrGLShaderBuilder::getColorOutputName() const { |
| 602 | return fHasCustomColorOutput ? declared_color_output_name() : "gl_FragColor"; |
| 603 | } |
| 604 | |
| 605 | const char* GrGLShaderBuilder::enableSecondaryOutput() { |
| 606 | if (!fHasSecondaryOutput) { |
| 607 | fFSOutputs.push_back().set(kVec4f_GrSLType, |
| 608 | GrGLShaderVar::kOut_TypeModifier, |
| 609 | dual_source_output_name()); |
| 610 | fHasSecondaryOutput = true; |
| 611 | } |
| 612 | return dual_source_output_name(); |
| 613 | } |
| 614 | |
| 615 | |
| 616 | bool GrGLShaderBuilder::finish(GrGLuint* outProgramId) { |
| 617 | SK_TRACE_EVENT0("GrGLShaderBuilder::finish"); |
| 618 | |
| 619 | GrGLuint programId = 0; |
| 620 | GL_CALL_RET(programId, CreateProgram()); |
| 621 | if (!programId) { |
| 622 | return false; |
| 623 | } |
| 624 | |
| 625 | if (!this->compileAndAttachShaders(programId)) { |
| 626 | GL_CALL(DeleteProgram(programId)); |
| 627 | return false; |
| 628 | } |
| 629 | |
| 630 | this->bindProgramLocations(programId); |
| 631 | |
| 632 | GL_CALL(LinkProgram(programId)); |
| 633 | GrGLint linked = GR_GL_INIT_ZERO; |
| 634 | GL_CALL(GetProgramiv(programId, GR_GL_LINK_STATUS, &linked)); |
| 635 | if (!linked) { |
| 636 | GrGLint infoLen = GR_GL_INIT_ZERO; |
| 637 | GL_CALL(GetProgramiv(programId, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
| 638 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
| 639 | if (infoLen > 0) { |
| 640 | // retrieve length even though we don't need it to workaround |
| 641 | // bug in chrome cmd buffer param validation. |
| 642 | GrGLsizei length = GR_GL_INIT_ZERO; |
| 643 | GL_CALL(GetProgramInfoLog(programId, |
| 644 | infoLen+1, |
| 645 | &length, |
| 646 | (char*)log.get())); |
| 647 | GrPrintf((char*)log.get()); |
| 648 | } |
| 649 | SkDEBUGFAIL("Error linking program"); |
| 650 | GL_CALL(DeleteProgram(programId)); |
| 651 | return false; |
| 652 | } |
| 653 | |
| 654 | fUniformManager.getUniformLocations(programId, fUniforms); |
| 655 | *outProgramId = programId; |
| 656 | return true; |
| 657 | } |
| 658 | |
| 659 | namespace { |
| 660 | // Compiles a GL shader, attaches it to a program, and releases the shader's reference. |
| 661 | // (That way there's no need to hang on to the GL shader id and delete it later.) |
| 662 | bool attach_shader(const GrGLInterface* gli, |
| 663 | GrGLuint programId, |
| 664 | GrGLenum type, |
| 665 | const SkString& shaderSrc) { |
| 666 | GrGLuint shaderId; |
| 667 | GR_GL_CALL_RET(gli, shaderId, CreateShader(type)); |
| 668 | if (0 == shaderId) { |
| 669 | return false; |
| 670 | } |
| 671 | |
| 672 | const GrGLchar* sourceStr = shaderSrc.c_str(); |
| 673 | int sourceLength = shaderSrc.size(); |
| 674 | GR_GL_CALL(gli, ShaderSource(shaderId, 1, &sourceStr, &sourceLength)); |
| 675 | |
| 676 | GrGLint compiled = GR_GL_INIT_ZERO; |
| 677 | GR_GL_CALL(gli, CompileShader(shaderId)); |
| 678 | GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_COMPILE_STATUS, &compiled)); |
| 679 | |
| 680 | if (!compiled) { |
| 681 | GrGLint infoLen = GR_GL_INIT_ZERO; |
| 682 | GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
| 683 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
| 684 | if (infoLen > 0) { |
| 685 | // retrieve length even though we don't need it to workaround bug in chrome cmd buffer |
| 686 | // param validation. |
| 687 | GrGLsizei length = GR_GL_INIT_ZERO; |
| 688 | GR_GL_CALL(gli, GetShaderInfoLog(shaderId, infoLen+1, |
| 689 | &length, (char*)log.get())); |
| 690 | GrPrintf(shaderSrc.c_str()); |
| 691 | GrPrintf("\n%s", log.get()); |
| 692 | } |
| 693 | SkDEBUGFAIL("Shader compilation failed!"); |
| 694 | GR_GL_CALL(gli, DeleteShader(shaderId)); |
| 695 | return false; |
| 696 | } else if (c_PrintShaders) { |
| 697 | GrPrintf(shaderSrc.c_str()); |
| 698 | GrPrintf("\n"); |
| 699 | } |
| 700 | |
| 701 | GR_GL_CALL(gli, AttachShader(programId, shaderId)); |
| 702 | GR_GL_CALL(gli, DeleteShader(shaderId)); |
| 703 | return true; |
| 704 | } |
| 705 | |
| 706 | } |
| 707 | |
| 708 | bool GrGLShaderBuilder::compileAndAttachShaders(GrGLuint programId) const { |
| 709 | if (NULL != fVertexBuilder.get() && !fVertexBuilder->compileAndAttachShaders(programId)) { |
| 710 | return false; |
| 711 | } |
| 712 | |
| 713 | SkString fragShaderSrc(GrGetGLSLVersionDecl(this->ctxInfo())); |
| 714 | fragShaderSrc.append(fFSExtensions); |
| 715 | append_default_precision_qualifier(kDefaultFragmentPrecision, |
| 716 | fGpu->glBinding(), |
| 717 | &fragShaderSrc); |
| 718 | this->appendUniformDecls(kFragment_Visibility, &fragShaderSrc); |
| 719 | this->appendDecls(fFSInputs, &fragShaderSrc); |
| 720 | // We shouldn't have declared outputs on 1.10 |
| 721 | SkASSERT(k110_GrGLSLGeneration != fGpu->glslGeneration() || fFSOutputs.empty()); |
| 722 | this->appendDecls(fFSOutputs, &fragShaderSrc); |
| 723 | fragShaderSrc.append(fFSFunctions); |
| 724 | fragShaderSrc.append("void main() {\n"); |
| 725 | fragShaderSrc.append(fFSCode); |
| 726 | fragShaderSrc.append("}\n"); |
| 727 | if (!attach_shader(fGpu->glInterface(), programId, GR_GL_FRAGMENT_SHADER, fragShaderSrc)) { |
| 728 | return false; |
| 729 | } |
| 730 | |
| 731 | return true; |
| 732 | } |
| 733 | |
| 734 | void GrGLShaderBuilder::bindProgramLocations(GrGLuint programId) const { |
| 735 | if (NULL != fVertexBuilder.get()) { |
| 736 | fVertexBuilder->bindProgramLocations(programId); |
| 737 | } |
| 738 | |
| 739 | if (fHasCustomColorOutput) { |
| 740 | GL_CALL(BindFragDataLocation(programId, 0, declared_color_output_name())); |
| 741 | } |
| 742 | if (fHasSecondaryOutput) { |
| 743 | GL_CALL(BindFragDataLocationIndexed(programId, 0, 1, dual_source_output_name())); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | const GrGLContextInfo& GrGLShaderBuilder::ctxInfo() const { |
| 748 | return fGpu->ctxInfo(); |
| 749 | } |
| 750 | |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 751 | //////////////////////////////////////////////////////////////////////////// |
| 752 | |
| 753 | GrGLShaderBuilder::VertexBuilder::VertexBuilder(GrGLShaderBuilder* parent, |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 754 | GrGpuGL* gpu, |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 755 | const GrGLProgramDesc& desc) |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 756 | : fParent(parent) |
| 757 | , fGpu(gpu) |
| 758 | , fDesc(desc) |
| 759 | , fVSAttrs(kVarsPerBlock) |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 760 | , fVSOutputs(kVarsPerBlock) |
| 761 | , fGSInputs(kVarsPerBlock) |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 762 | , fGSOutputs(kVarsPerBlock) { |
| 763 | |
| 764 | const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 765 | |
| 766 | fPositionVar = &fVSAttrs.push_back(); |
| 767 | fPositionVar->set(kVec2f_GrSLType, GrGLShaderVar::kAttribute_TypeModifier, "aPosition"); |
| 768 | if (-1 != header.fLocalCoordAttributeIndex) { |
| 769 | fLocalCoordsVar = &fVSAttrs.push_back(); |
| 770 | fLocalCoordsVar->set(kVec2f_GrSLType, |
| 771 | GrGLShaderVar::kAttribute_TypeModifier, |
| 772 | "aLocalCoords"); |
| 773 | } else { |
| 774 | fLocalCoordsVar = fPositionVar; |
| 775 | } |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 776 | |
| 777 | const char* viewMName; |
| 778 | fViewMatrixUniform = fParent->addUniform(GrGLShaderBuilder::kVertex_Visibility, |
| 779 | kMat33f_GrSLType, "ViewM", &viewMName); |
| 780 | |
| 781 | this->vsCodeAppendf("\tvec3 pos3 = %s * vec3(%s, 1);\n" |
| 782 | "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n", |
| 783 | viewMName, fPositionVar->c_str()); |
| 784 | |
| 785 | // we output point size in the GS if present |
| 786 | if (header.fEmitsPointSize |
| 787 | #if GR_GL_EXPERIMENTAL_GS |
| 788 | && !header.fExperimentalGS |
| 789 | #endif |
| 790 | ) { |
| 791 | this->vsCodeAppend("\tgl_PointSize = 1.0;\n"); |
| 792 | } |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | bool GrGLShaderBuilder::VertexBuilder::addAttribute(GrSLType type, |
| 796 | const char* name) { |
| 797 | for (int i = 0; i < fVSAttrs.count(); ++i) { |
| 798 | const GrGLShaderVar& attr = fVSAttrs[i]; |
| 799 | // if attribute already added, don't add it again |
| 800 | if (attr.getName().equals(name)) { |
| 801 | SkASSERT(attr.getType() == type); |
| 802 | return false; |
| 803 | } |
| 804 | } |
| 805 | fVSAttrs.push_back().set(type, |
| 806 | GrGLShaderVar::kAttribute_TypeModifier, |
| 807 | name); |
| 808 | return true; |
| 809 | } |
| 810 | |
| 811 | bool GrGLShaderBuilder::VertexBuilder::addEffectAttribute(int attributeIndex, |
| 812 | GrSLType type, |
| 813 | const SkString& name) { |
| 814 | if (!this->addAttribute(type, name.c_str())) { |
| 815 | return false; |
| 816 | } |
| 817 | |
| 818 | fEffectAttributes.push_back().set(attributeIndex, name); |
| 819 | return true; |
| 820 | } |
| 821 | |
| 822 | void GrGLShaderBuilder::VertexBuilder::addVarying(GrSLType type, |
| 823 | const char* name, |
| 824 | const char** vsOutName, |
| 825 | const char** fsInName) { |
| 826 | fVSOutputs.push_back(); |
| 827 | fVSOutputs.back().setType(type); |
| 828 | fVSOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); |
| 829 | fParent->nameVariable(fVSOutputs.back().accessName(), 'v', name); |
| 830 | |
| 831 | if (vsOutName) { |
| 832 | *vsOutName = fVSOutputs.back().getName().c_str(); |
| 833 | } |
| 834 | // input to FS comes either from VS or GS |
| 835 | const SkString* fsName; |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 836 | #if GR_GL_EXPERIMENTAL_GS |
| 837 | if (fDesc.getHeader().fExperimentalGS) { |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 838 | // if we have a GS take each varying in as an array |
| 839 | // and output as non-array. |
| 840 | fGSInputs.push_back(); |
| 841 | fGSInputs.back().setType(type); |
| 842 | fGSInputs.back().setTypeModifier(GrGLShaderVar::kVaryingIn_TypeModifier); |
| 843 | fGSInputs.back().setUnsizedArray(); |
| 844 | *fGSInputs.back().accessName() = fVSOutputs.back().getName(); |
| 845 | fGSOutputs.push_back(); |
| 846 | fGSOutputs.back().setType(type); |
| 847 | fGSOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); |
| 848 | fParent->nameVariable(fGSOutputs.back().accessName(), 'g', name); |
| 849 | fsName = fGSOutputs.back().accessName(); |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 850 | } else |
| 851 | #endif |
| 852 | { |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 853 | fsName = fVSOutputs.back().accessName(); |
| 854 | } |
| 855 | fParent->fsInputAppend().set(type, |
| 856 | GrGLShaderVar::kVaryingIn_TypeModifier, |
| 857 | *fsName); |
| 858 | if (fsInName) { |
| 859 | *fsInName = fsName->c_str(); |
| 860 | } |
| 861 | } |
| 862 | |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 863 | const SkString* GrGLShaderBuilder::VertexBuilder::getEffectAttributeName(int attributeIndex) const { |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 864 | const AttributePair* attribEnd = fEffectAttributes.end(); |
| 865 | for (const AttributePair* attrib = fEffectAttributes.begin(); attrib != attribEnd; ++attrib) { |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 866 | if (attrib->fIndex == attributeIndex) { |
| 867 | return &attrib->fName; |
| 868 | } |
skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 869 | } |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 870 | |
| 871 | return NULL; |
| 872 | } |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 873 | |
| 874 | bool GrGLShaderBuilder::VertexBuilder::compileAndAttachShaders(GrGLuint programId) const { |
| 875 | SkString vertShaderSrc(GrGetGLSLVersionDecl(fParent->ctxInfo())); |
| 876 | fParent->appendUniformDecls(kVertex_Visibility, &vertShaderSrc); |
| 877 | fParent->appendDecls(fVSAttrs, &vertShaderSrc); |
| 878 | fParent->appendDecls(fVSOutputs, &vertShaderSrc); |
| 879 | vertShaderSrc.append("void main() {\n"); |
| 880 | vertShaderSrc.append(fVSCode); |
| 881 | vertShaderSrc.append("}\n"); |
| 882 | if (!attach_shader(fGpu->glInterface(), programId, GR_GL_VERTEX_SHADER, vertShaderSrc)) { |
| 883 | return false; |
| 884 | } |
| 885 | |
| 886 | #if GR_GL_EXPERIMENTAL_GS |
| 887 | if (fDesc.getHeader().fExperimentalGS) { |
| 888 | SkASSERT(fGpu->glslGeneration() >= k150_GrGLSLGeneration); |
| 889 | SkString geomShaderSrc(GrGetGLSLVersionDecl(fParent->ctxInfo())); |
| 890 | geomShaderSrc.append("layout(triangles) in;\n" |
| 891 | "layout(triangle_strip, max_vertices = 6) out;\n"); |
| 892 | fParent->appendDecls(fGSInputs, &geomShaderSrc); |
| 893 | fParent->appendDecls(fGSOutputs, &geomShaderSrc); |
| 894 | geomShaderSrc.append("void main() {\n"); |
| 895 | geomShaderSrc.append("\tfor (int i = 0; i < 3; ++i) {\n" |
| 896 | "\t\tgl_Position = gl_in[i].gl_Position;\n"); |
| 897 | if (fDesc.getHeader().fEmitsPointSize) { |
| 898 | geomShaderSrc.append("\t\tgl_PointSize = 1.0;\n"); |
| 899 | } |
| 900 | SkASSERT(fGSInputs.count() == fGSOutputs.count()); |
| 901 | for (int i = 0; i < fGSInputs.count(); ++i) { |
| 902 | geomShaderSrc.appendf("\t\t%s = %s[i];\n", |
| 903 | fGSOutputs[i].getName().c_str(), |
| 904 | fGSInputs[i].getName().c_str()); |
| 905 | } |
| 906 | geomShaderSrc.append("\t\tEmitVertex();\n" |
| 907 | "\t}\n" |
| 908 | "\tEndPrimitive();\n"); |
| 909 | geomShaderSrc.append("}\n"); |
| 910 | if (!attach_shader(fGpu->glInterface(), programId, GR_GL_GEOMETRY_SHADER, geomShaderSrc)) { |
| 911 | return false; |
| 912 | } |
| 913 | } |
| 914 | #endif |
| 915 | |
| 916 | return true; |
| 917 | } |
| 918 | |
| 919 | void GrGLShaderBuilder::VertexBuilder::bindProgramLocations(GrGLuint programId) const { |
| 920 | const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); |
| 921 | |
| 922 | // Bind the attrib locations to same values for all shaders |
| 923 | SkASSERT(-1 != header.fPositionAttributeIndex); |
| 924 | GL_CALL(BindAttribLocation(programId, |
| 925 | header.fPositionAttributeIndex, |
| 926 | fPositionVar->c_str())); |
| 927 | if (-1 != header.fLocalCoordAttributeIndex) { |
| 928 | GL_CALL(BindAttribLocation(programId, |
| 929 | header.fLocalCoordAttributeIndex, |
| 930 | fLocalCoordsVar->c_str())); |
| 931 | } |
| 932 | if (-1 != header.fColorAttributeIndex) { |
| 933 | GL_CALL(BindAttribLocation(programId, |
| 934 | header.fColorAttributeIndex, |
| 935 | color_attribute_name())); |
| 936 | } |
| 937 | if (-1 != header.fCoverageAttributeIndex) { |
| 938 | GL_CALL(BindAttribLocation(programId, |
| 939 | header.fCoverageAttributeIndex, |
| 940 | coverage_attribute_name())); |
| 941 | } |
| 942 | |
| 943 | const AttributePair* attribEnd = fEffectAttributes.end(); |
| 944 | for (const AttributePair* attrib = fEffectAttributes.begin(); attrib != attribEnd; ++attrib) { |
| 945 | GL_CALL(BindAttribLocation(programId, attrib->fIndex, attrib->fName.c_str())); |
| 946 | } |
| 947 | } |