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