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