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 | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 167 | } else if (GrGLProgramDesc::kSolidWhite_ColorInput == header.fColorInput) { |
commit-bot@chromium.org | f7f9aa0 | 2014-05-30 15:14:56 +0000 | [diff] [blame] | 168 | inputColor = GrGLSLExpr4(1); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 169 | } else if (GrGLProgramDesc::kTransBlack_ColorInput == header.fColorInput) { |
commit-bot@chromium.org | f7f9aa0 | 2014-05-30 15:14:56 +0000 | [diff] [blame] | 170 | inputColor = GrGLSLExpr4(0); |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 171 | } |
| 172 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 173 | if (GrGLProgramDesc::kUniform_ColorInput == header.fCoverageInput) { |
| 174 | const char* name; |
commit-bot@chromium.org | facad13 | 2014-05-29 22:20:03 +0000 | [diff] [blame] | 175 | fOutput.fUniformHandles.fCoverageUni = |
| 176 | this->addUniform(GrGLShaderBuilder::kFragment_Visibility, kVec4f_GrSLType, "Coverage", |
| 177 | &name); |
commit-bot@chromium.org | f7f9aa0 | 2014-05-30 15:14:56 +0000 | [diff] [blame] | 178 | inputCoverage = GrGLSLExpr4(name); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 179 | } else if (GrGLProgramDesc::kSolidWhite_ColorInput == header.fCoverageInput) { |
commit-bot@chromium.org | f7f9aa0 | 2014-05-30 15:14:56 +0000 | [diff] [blame] | 180 | inputCoverage = GrGLSLExpr4(1); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 181 | } else if (GrGLProgramDesc::kTransBlack_ColorInput == header.fCoverageInput) { |
commit-bot@chromium.org | f7f9aa0 | 2014-05-30 15:14:56 +0000 | [diff] [blame] | 182 | inputCoverage = GrGLSLExpr4(0); |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | if (k110_GrGLSLGeneration != fGpu->glslGeneration()) { |
| 186 | fFSOutputs.push_back().set(kVec4f_GrSLType, |
| 187 | GrGLShaderVar::kOut_TypeModifier, |
| 188 | declared_color_output_name()); |
| 189 | fHasCustomColorOutput = true; |
| 190 | } |
commit-bot@chromium.org | f7f9aa0 | 2014-05-30 15:14:56 +0000 | [diff] [blame] | 191 | |
| 192 | this->emitCodeBeforeEffects(&inputColor, &inputCoverage); |
| 193 | |
| 194 | /////////////////////////////////////////////////////////////////////////// |
| 195 | // emit the per-effect code for both color and coverage effects |
| 196 | |
commit-bot@chromium.org | a05fa06 | 2014-05-30 18:55:03 +0000 | [diff] [blame] | 197 | fOutput.fColorEffects.reset(this->createAndEmitEffects(colorStages, |
| 198 | this->desc().getEffectKeys(), |
| 199 | this->desc().numColorEffects(), |
| 200 | &inputColor)); |
commit-bot@chromium.org | f7f9aa0 | 2014-05-30 15:14:56 +0000 | [diff] [blame] | 201 | |
commit-bot@chromium.org | a05fa06 | 2014-05-30 18:55:03 +0000 | [diff] [blame] | 202 | fOutput.fCoverageEffects.reset(this->createAndEmitEffects(coverageStages, |
commit-bot@chromium.org | f7f9aa0 | 2014-05-30 15:14:56 +0000 | [diff] [blame] | 203 | this->desc().getEffectKeys() + this->desc().numColorEffects(), |
| 204 | this->desc().numCoverageEffects(), |
commit-bot@chromium.org | a05fa06 | 2014-05-30 18:55:03 +0000 | [diff] [blame] | 205 | &inputCoverage)); |
commit-bot@chromium.org | f7f9aa0 | 2014-05-30 15:14:56 +0000 | [diff] [blame] | 206 | |
| 207 | this->emitCodeAfterEffects(); |
| 208 | |
| 209 | /////////////////////////////////////////////////////////////////////////// |
| 210 | // write the secondary color output if necessary |
| 211 | if (GrGLProgramDesc::CoverageOutputUsesSecondaryOutput(header.fCoverageOutput)) { |
| 212 | const char* secondaryOutputName = this->enableSecondaryOutput(); |
| 213 | |
| 214 | // default coeff to ones for kCoverage_DualSrcOutput |
| 215 | GrGLSLExpr4 coeff(1); |
| 216 | if (GrGLProgramDesc::kSecondaryCoverageISA_CoverageOutput == header.fCoverageOutput) { |
| 217 | // Get (1-A) into coeff |
| 218 | coeff = GrGLSLExpr4::VectorCast(GrGLSLExpr1(1) - inputColor.a()); |
humper | 4a24cd8 | 2014-06-17 13:39:29 -0700 | [diff] [blame^] | 219 | } else if (GrGLProgramDesc::kSecondaryCoverageISC_CoverageOutput == |
commit-bot@chromium.org | f7f9aa0 | 2014-05-30 15:14:56 +0000 | [diff] [blame] | 220 | header.fCoverageOutput){ |
| 221 | // Get (1-RGBA) into coeff |
| 222 | coeff = GrGLSLExpr4(1) - inputColor; |
| 223 | } |
| 224 | // Get coeff * coverage into modulate and then write that to the dual source output. |
| 225 | this->fsCodeAppendf("\t%s = %s;\n", secondaryOutputName, (coeff * inputCoverage).c_str()); |
| 226 | } |
| 227 | |
| 228 | /////////////////////////////////////////////////////////////////////////// |
| 229 | // combine color and coverage as frag color |
| 230 | |
| 231 | // Get "color * coverage" into fragColor |
| 232 | GrGLSLExpr4 fragColor = inputColor * inputCoverage; |
| 233 | // Now tack on "+(1-coverage)dst onto the frag color if we were asked to do so. |
| 234 | if (GrGLProgramDesc::kCombineWithDst_CoverageOutput == header.fCoverageOutput) { |
| 235 | GrGLSLExpr4 dstCoeff = GrGLSLExpr4(1) - inputCoverage; |
| 236 | |
| 237 | GrGLSLExpr4 dstContribution = dstCoeff * GrGLSLExpr4(this->dstColor()); |
| 238 | |
| 239 | fragColor = fragColor + dstContribution; |
| 240 | } |
| 241 | this->fsCodeAppendf("\t%s = %s;\n", this->getColorOutputName(), fragColor.c_str()); |
| 242 | |
| 243 | if (!this->finish()) { |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | ////////////////////////////////////////////////////////////////////////////// |
| 251 | |
| 252 | GrGLShaderBuilder::GrGLShaderBuilder(GrGpuGL* gpu, |
| 253 | GrGLUniformManager* uniformManager, |
| 254 | const GrGLProgramDesc& desc) |
| 255 | : fDesc(desc) |
| 256 | , fGpu(gpu) |
| 257 | , fUniformManager(SkRef(uniformManager)) |
| 258 | , fFSFeaturesAddedMask(0) |
| 259 | , fFSInputs(kVarsPerBlock) |
| 260 | , fFSOutputs(kMaxFSOutputs) |
| 261 | , fUniforms(kVarsPerBlock) |
| 262 | , fSetupFragPosition(false) |
| 263 | , fTopLeftFragPosRead(kTopLeftFragPosRead_FragPosKey == desc.getHeader().fFragPosKey) |
| 264 | , fHasCustomColorOutput(false) |
| 265 | , fHasSecondaryOutput(false) { |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 266 | } |
| 267 | |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 268 | bool GrGLShaderBuilder::enableFeature(GLSLFeature feature) { |
| 269 | switch (feature) { |
| 270 | case kStandardDerivatives_GLSLFeature: |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 271 | if (!fGpu->glCaps().shaderDerivativeSupport()) { |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 272 | return false; |
| 273 | } |
commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 274 | if (kGLES_GrGLStandard == fGpu->glStandard()) { |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 275 | this->addFSFeature(1 << kStandardDerivatives_GLSLFeature, |
| 276 | "GL_OES_standard_derivatives"); |
| 277 | } |
| 278 | return true; |
| 279 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 280 | SkFAIL("Unexpected GLSLFeature requested."); |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 281 | return false; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | bool GrGLShaderBuilder::enablePrivateFeature(GLSLPrivateFeature feature) { |
| 286 | switch (feature) { |
| 287 | case kFragCoordConventions_GLSLPrivateFeature: |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 288 | if (!fGpu->glCaps().fragCoordConventionsSupport()) { |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 289 | return false; |
| 290 | } |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 291 | if (fGpu->glslGeneration() < k150_GrGLSLGeneration) { |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 292 | this->addFSFeature(1 << kFragCoordConventions_GLSLPrivateFeature, |
| 293 | "GL_ARB_fragment_coord_conventions"); |
| 294 | } |
| 295 | return true; |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 296 | case kEXTShaderFramebufferFetch_GLSLPrivateFeature: |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 297 | if (GrGLCaps::kEXT_FBFetchType != fGpu->glCaps().fbFetchType()) { |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 298 | return false; |
| 299 | } |
| 300 | this->addFSFeature(1 << kEXTShaderFramebufferFetch_GLSLPrivateFeature, |
| 301 | "GL_EXT_shader_framebuffer_fetch"); |
| 302 | return true; |
| 303 | case kNVShaderFramebufferFetch_GLSLPrivateFeature: |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 304 | if (GrGLCaps::kNV_FBFetchType != fGpu->glCaps().fbFetchType()) { |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 305 | return false; |
| 306 | } |
| 307 | this->addFSFeature(1 << kNVShaderFramebufferFetch_GLSLPrivateFeature, |
| 308 | "GL_NV_shader_framebuffer_fetch"); |
| 309 | return true; |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 310 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 311 | SkFAIL("Unexpected GLSLPrivateFeature requested."); |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 312 | return false; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | void GrGLShaderBuilder::addFSFeature(uint32_t featureBit, const char* extensionName) { |
| 317 | if (!(featureBit & fFSFeaturesAddedMask)) { |
| 318 | fFSExtensions.appendf("#extension %s: require\n", extensionName); |
| 319 | fFSFeaturesAddedMask |= featureBit; |
| 320 | } |
| 321 | } |
| 322 | |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 323 | void GrGLShaderBuilder::nameVariable(SkString* out, char prefix, const char* name) { |
| 324 | if ('\0' == prefix) { |
| 325 | *out = name; |
| 326 | } else { |
| 327 | out->printf("%c%s", prefix, name); |
| 328 | } |
| 329 | if (fCodeStage.inStageCode()) { |
| 330 | if (out->endsWith('_')) { |
| 331 | // Names containing "__" are reserved. |
| 332 | out->append("x"); |
| 333 | } |
| 334 | out->appendf("_Stage%d", fCodeStage.stageIndex()); |
| 335 | } |
| 336 | } |
| 337 | |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 338 | const char* GrGLShaderBuilder::dstColor() { |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 339 | if (fCodeStage.inStageCode()) { |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 340 | const GrEffectRef& effect = *fCodeStage.effectStage()->getEffect(); |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 341 | if (!effect->willReadDstColor()) { |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 342 | SkDEBUGFAIL("GrGLEffect asked for dst color but its generating GrEffect " |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 343 | "did not request access."); |
| 344 | return ""; |
| 345 | } |
| 346 | } |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 347 | static const char kFBFetchColorName[] = "gl_LastFragData[0]"; |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 348 | GrGLCaps::FBFetchType fetchType = fGpu->glCaps().fbFetchType(); |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 349 | if (GrGLCaps::kEXT_FBFetchType == fetchType) { |
| 350 | SkAssertResult(this->enablePrivateFeature(kEXTShaderFramebufferFetch_GLSLPrivateFeature)); |
| 351 | return kFBFetchColorName; |
| 352 | } else if (GrGLCaps::kNV_FBFetchType == fetchType) { |
| 353 | SkAssertResult(this->enablePrivateFeature(kNVShaderFramebufferFetch_GLSLPrivateFeature)); |
| 354 | return kFBFetchColorName; |
commit-bot@chromium.org | facad13 | 2014-05-29 22:20:03 +0000 | [diff] [blame] | 355 | } else if (fOutput.fUniformHandles.fDstCopySamplerUni.isValid()) { |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 356 | return kDstCopyColorName; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 357 | } else { |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 358 | return ""; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 359 | } |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 360 | } |
| 361 | |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 362 | void GrGLShaderBuilder::appendTextureLookup(SkString* out, |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 363 | const GrGLShaderBuilder::TextureSampler& sampler, |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 364 | const char* coordName, |
| 365 | GrSLType varyingType) const { |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 366 | append_texture_lookup(out, |
| 367 | fGpu, |
| 368 | this->getUniformCStr(sampler.samplerUniform()), |
| 369 | coordName, |
| 370 | sampler.configComponentMask(), |
| 371 | sampler.swizzle(), |
| 372 | varyingType); |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 373 | } |
| 374 | |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 375 | void GrGLShaderBuilder::fsAppendTextureLookup(const GrGLShaderBuilder::TextureSampler& sampler, |
| 376 | const char* coordName, |
| 377 | GrSLType varyingType) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 378 | this->appendTextureLookup(&fFSCode, sampler, coordName, varyingType); |
| 379 | } |
| 380 | |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 381 | void GrGLShaderBuilder::fsAppendTextureLookupAndModulate( |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 382 | const char* modulation, |
| 383 | const GrGLShaderBuilder::TextureSampler& sampler, |
| 384 | const char* coordName, |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 385 | GrSLType varyingType) { |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 386 | SkString lookup; |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 387 | this->appendTextureLookup(&lookup, sampler, coordName, varyingType); |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 388 | fFSCode.append((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str()); |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 389 | } |
| 390 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 391 | GrGLShaderBuilder::DstReadKey GrGLShaderBuilder::KeyForDstRead(const GrTexture* dstCopy, |
| 392 | const GrGLCaps& caps) { |
| 393 | uint32_t key = kYesDstRead_DstReadKeyBit; |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 394 | if (GrGLCaps::kNone_FBFetchType != caps.fbFetchType()) { |
| 395 | return key; |
| 396 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 397 | SkASSERT(NULL != dstCopy); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 398 | if (!caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(dstCopy->config())) { |
| 399 | // The fact that the config is alpha-only must be considered when generating code. |
| 400 | key |= kUseAlphaConfig_DstReadKeyBit; |
| 401 | } |
| 402 | if (kTopLeft_GrSurfaceOrigin == dstCopy->origin()) { |
| 403 | key |= kTopLeftOrigin_DstReadKeyBit; |
| 404 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 405 | SkASSERT(static_cast<DstReadKey>(key) == key); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 406 | return static_cast<DstReadKey>(key); |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 407 | } |
| 408 | |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 409 | GrGLShaderBuilder::FragPosKey GrGLShaderBuilder::KeyForFragmentPosition(const GrRenderTarget* dst, |
| 410 | const GrGLCaps&) { |
| 411 | if (kTopLeft_GrSurfaceOrigin == dst->origin()) { |
| 412 | return kTopLeftFragPosRead_FragPosKey; |
| 413 | } else { |
| 414 | return kBottomLeftFragPosRead_FragPosKey; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 419 | const GrGLenum* GrGLShaderBuilder::GetTexParamSwizzle(GrPixelConfig config, const GrGLCaps& caps) { |
| 420 | if (caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(config)) { |
| 421 | if (caps.textureRedSupport()) { |
| 422 | static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED, GR_GL_RED, GR_GL_RED }; |
| 423 | return gRedSmear; |
| 424 | } else { |
| 425 | static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA, |
| 426 | GR_GL_ALPHA, GR_GL_ALPHA }; |
| 427 | return gAlphaSmear; |
| 428 | } |
| 429 | } else { |
| 430 | static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN, GR_GL_BLUE, GR_GL_ALPHA }; |
| 431 | return gStraight; |
| 432 | } |
| 433 | } |
| 434 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 435 | GrGLUniformManager::UniformHandle GrGLShaderBuilder::addUniformArray(uint32_t visibility, |
| 436 | GrSLType type, |
| 437 | const char* name, |
| 438 | int count, |
| 439 | const char** outName) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 440 | SkASSERT(name && strlen(name)); |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 441 | SkDEBUGCODE(static const uint32_t kVisibilityMask = kVertex_Visibility | kFragment_Visibility); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 442 | SkASSERT(0 == (~kVisibilityMask & visibility)); |
| 443 | SkASSERT(0 != visibility); |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 444 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 445 | BuilderUniform& uni = fUniforms.push_back(); |
commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 446 | UniformHandle h = GrGLUniformManager::UniformHandle::CreateFromUniformIndex(fUniforms.count() - 1); |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 447 | SkDEBUGCODE(UniformHandle h2 =) |
commit-bot@chromium.org | 6eac42e | 2014-05-29 21:29:51 +0000 | [diff] [blame] | 448 | fUniformManager->appendUniform(type, count); |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 449 | // We expect the uniform manager to initially have no uniforms and that all uniforms are added |
| 450 | // by this function. Therefore, the handles should match. |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 451 | SkASSERT(h2 == h); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 452 | uni.fVariable.setType(type); |
| 453 | uni.fVariable.setTypeModifier(GrGLShaderVar::kUniform_TypeModifier); |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 454 | this->nameVariable(uni.fVariable.accessName(), 'u', name); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 455 | uni.fVariable.setArrayCount(count); |
| 456 | uni.fVisibility = visibility; |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 457 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 458 | // If it is visible in both the VS and FS, the precision must match. |
| 459 | // We declare a default FS precision, but not a default VS. So set the var |
| 460 | // to use the default FS precision. |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 461 | if ((kVertex_Visibility | kFragment_Visibility) == visibility) { |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 462 | // the fragment and vertex precisions must match |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 463 | uni.fVariable.setPrecision(kDefaultFragmentPrecision); |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 464 | } |
| 465 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 466 | if (NULL != outName) { |
| 467 | *outName = uni.fVariable.c_str(); |
| 468 | } |
| 469 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 470 | return h; |
| 471 | } |
| 472 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 473 | SkString GrGLShaderBuilder::ensureFSCoords2D(const TransformedCoordsArray& coords, int index) { |
| 474 | if (kVec3f_GrSLType != coords[index].type()) { |
| 475 | SkASSERT(kVec2f_GrSLType == coords[index].type()); |
| 476 | return coords[index].getName(); |
| 477 | } |
| 478 | |
| 479 | SkString coords2D("coords2D"); |
| 480 | if (0 != index) { |
| 481 | coords2D.appendf("_%i", index); |
| 482 | } |
| 483 | this->fsCodeAppendf("\tvec2 %s = %s.xy / %s.z;", |
| 484 | coords2D.c_str(), coords[index].c_str(), coords[index].c_str()); |
| 485 | return coords2D; |
| 486 | } |
| 487 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 488 | const char* GrGLShaderBuilder::fragmentPosition() { |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 489 | if (fCodeStage.inStageCode()) { |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 490 | const GrEffectRef& effect = *fCodeStage.effectStage()->getEffect(); |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 491 | if (!effect->willReadFragmentPosition()) { |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 492 | SkDEBUGFAIL("GrGLEffect asked for frag position but its generating GrEffect " |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 493 | "did not request access."); |
| 494 | return ""; |
| 495 | } |
| 496 | } |
commit-bot@chromium.org | 5f6ac03 | 2014-03-03 23:25:26 +0000 | [diff] [blame] | 497 | // We only declare "gl_FragCoord" when we're in the case where we want to use layout qualifiers |
| 498 | // to reverse y. Otherwise it isn't necessary and whether the "in" qualifier appears in the |
| 499 | // 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] | 500 | if (fTopLeftFragPosRead) { |
commit-bot@chromium.org | 5f6ac03 | 2014-03-03 23:25:26 +0000 | [diff] [blame] | 501 | fSetupFragPosition = true; |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 502 | return "gl_FragCoord"; |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 503 | } else if (fGpu->glCaps().fragCoordConventionsSupport()) { |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 504 | if (!fSetupFragPosition) { |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 505 | SkAssertResult(this->enablePrivateFeature(kFragCoordConventions_GLSLPrivateFeature)); |
bsalomon@google.com | 5fa2107 | 2012-10-25 14:57:46 +0000 | [diff] [blame] | 506 | fFSInputs.push_back().set(kVec4f_GrSLType, |
| 507 | GrGLShaderVar::kIn_TypeModifier, |
| 508 | "gl_FragCoord", |
| 509 | GrGLShaderVar::kDefault_Precision, |
| 510 | GrGLShaderVar::kUpperLeft_Origin); |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 511 | fSetupFragPosition = true; |
| 512 | } |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 513 | return "gl_FragCoord"; |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 514 | } else { |
| 515 | static const char* kCoordName = "fragCoordYDown"; |
| 516 | if (!fSetupFragPosition) { |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 517 | // temporarily change the stage index because we're inserting non-stage code. |
| 518 | CodeStage::AutoStageRestore csar(&fCodeStage, NULL); |
| 519 | |
commit-bot@chromium.org | facad13 | 2014-05-29 22:20:03 +0000 | [diff] [blame] | 520 | SkASSERT(!fOutput.fUniformHandles.fRTHeightUni.isValid()); |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 521 | const char* rtHeightName; |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 522 | |
commit-bot@chromium.org | facad13 | 2014-05-29 22:20:03 +0000 | [diff] [blame] | 523 | fOutput.fUniformHandles.fRTHeightUni = |
| 524 | this->addUniform(kFragment_Visibility, kFloat_GrSLType, "RTHeight", &rtHeightName); |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 525 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 526 | this->fFSCode.prependf("\tvec4 %s = vec4(gl_FragCoord.x, %s - gl_FragCoord.y, gl_FragCoord.zw);\n", |
| 527 | kCoordName, rtHeightName); |
| 528 | fSetupFragPosition = true; |
| 529 | } |
commit-bot@chromium.org | facad13 | 2014-05-29 22:20:03 +0000 | [diff] [blame] | 530 | SkASSERT(fOutput.fUniformHandles.fRTHeightUni.isValid()); |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 531 | return kCoordName; |
| 532 | } |
| 533 | } |
| 534 | |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 535 | void GrGLShaderBuilder::fsEmitFunction(GrSLType returnType, |
| 536 | const char* name, |
| 537 | int argCnt, |
| 538 | const GrGLShaderVar* args, |
| 539 | const char* body, |
| 540 | SkString* outName) { |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 541 | fFSFunctions.append(GrGLSLTypeString(returnType)); |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 542 | this->nameVariable(outName, '\0', name); |
bsalomon@google.com | 77cf460 | 2013-04-22 21:05:48 +0000 | [diff] [blame] | 543 | fFSFunctions.appendf(" %s", outName->c_str()); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 544 | fFSFunctions.append("("); |
| 545 | for (int i = 0; i < argCnt; ++i) { |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 546 | args[i].appendDecl(this->ctxInfo(), &fFSFunctions); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 547 | if (i < argCnt - 1) { |
| 548 | fFSFunctions.append(", "); |
| 549 | } |
| 550 | } |
| 551 | fFSFunctions.append(") {\n"); |
| 552 | fFSFunctions.append(body); |
| 553 | fFSFunctions.append("}\n\n"); |
| 554 | } |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 555 | |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 556 | namespace { |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 557 | |
| 558 | inline void append_default_precision_qualifier(GrGLShaderVar::Precision p, |
commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 559 | GrGLStandard standard, |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 560 | SkString* str) { |
| 561 | // 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] | 562 | if (kGLES_GrGLStandard == standard) { |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 563 | switch (p) { |
| 564 | case GrGLShaderVar::kHigh_Precision: |
| 565 | str->append("precision highp float;\n"); |
| 566 | break; |
| 567 | case GrGLShaderVar::kMedium_Precision: |
| 568 | str->append("precision mediump float;\n"); |
| 569 | break; |
| 570 | case GrGLShaderVar::kLow_Precision: |
| 571 | str->append("precision lowp float;\n"); |
| 572 | break; |
| 573 | case GrGLShaderVar::kDefault_Precision: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 574 | SkFAIL("Default precision now allowed."); |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 575 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 576 | SkFAIL("Unknown precision value."); |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 580 | } |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 581 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 582 | void GrGLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 583 | for (int i = 0; i < vars.count(); ++i) { |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 584 | vars[i].appendDecl(this->ctxInfo(), out); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 585 | out->append(";\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 586 | } |
| 587 | } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 588 | |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 589 | void GrGLShaderBuilder::appendUniformDecls(ShaderVisibility visibility, |
| 590 | SkString* out) const { |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 591 | for (int i = 0; i < fUniforms.count(); ++i) { |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 592 | if (fUniforms[i].fVisibility & visibility) { |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 593 | fUniforms[i].fVariable.appendDecl(this->ctxInfo(), out); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 594 | out->append(";\n"); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 597 | } |
| 598 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 599 | void GrGLShaderBuilder::createAndEmitEffects(GrGLProgramEffectsBuilder* programEffectsBuilder, |
| 600 | const GrEffectStage* effectStages[], |
| 601 | const EffectKey effectKeys[], |
| 602 | int effectCnt, |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 603 | GrGLSLExpr4* fsInOutColor) { |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 604 | bool effectEmitted = false; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 605 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 606 | GrGLSLExpr4 inColor = *fsInOutColor; |
| 607 | GrGLSLExpr4 outColor; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 608 | |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 609 | for (int e = 0; e < effectCnt; ++e) { |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 610 | SkASSERT(NULL != effectStages[e] && NULL != effectStages[e]->getEffect()); |
| 611 | const GrEffectStage& stage = *effectStages[e]; |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 612 | |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 613 | CodeStage::AutoStageRestore csar(&fCodeStage, &stage); |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 614 | |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 615 | if (inColor.isZeros()) { |
| 616 | SkString inColorName; |
| 617 | |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 618 | // 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] | 619 | this->nameVariable(&inColorName, '\0', "input"); |
| 620 | this->fsCodeAppendf("\tvec4 %s = %s;\n", inColorName.c_str(), inColor.c_str()); |
| 621 | inColor = inColorName; |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | // create var to hold stage result |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 625 | SkString outColorName; |
| 626 | this->nameVariable(&outColorName, '\0', "output"); |
| 627 | this->fsCodeAppendf("\tvec4 %s;\n", outColorName.c_str()); |
| 628 | outColor = outColorName; |
| 629 | |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 630 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 631 | programEffectsBuilder->emitEffect(stage, |
| 632 | effectKeys[e], |
| 633 | outColor.c_str(), |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 634 | inColor.isOnes() ? NULL : inColor.c_str(), |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 635 | fCodeStage.stageIndex()); |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 636 | |
| 637 | inColor = outColor; |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 638 | effectEmitted = true; |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 639 | } |
| 640 | |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 641 | if (effectEmitted) { |
| 642 | *fsInOutColor = outColor; |
| 643 | } |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 644 | } |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 645 | |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 646 | const char* GrGLShaderBuilder::getColorOutputName() const { |
| 647 | return fHasCustomColorOutput ? declared_color_output_name() : "gl_FragColor"; |
| 648 | } |
| 649 | |
| 650 | const char* GrGLShaderBuilder::enableSecondaryOutput() { |
| 651 | if (!fHasSecondaryOutput) { |
| 652 | fFSOutputs.push_back().set(kVec4f_GrSLType, |
| 653 | GrGLShaderVar::kOut_TypeModifier, |
| 654 | dual_source_output_name()); |
| 655 | fHasSecondaryOutput = true; |
| 656 | } |
| 657 | return dual_source_output_name(); |
| 658 | } |
| 659 | |
commit-bot@chromium.org | facad13 | 2014-05-29 22:20:03 +0000 | [diff] [blame] | 660 | bool GrGLShaderBuilder::finish() { |
| 661 | SkASSERT(0 == fOutput.fProgramID); |
| 662 | GL_CALL_RET(fOutput.fProgramID, CreateProgram()); |
| 663 | if (!fOutput.fProgramID) { |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 664 | return false; |
| 665 | } |
| 666 | |
commit-bot@chromium.org | d328fb6 | 2014-02-14 00:03:38 +0000 | [diff] [blame] | 667 | SkTDArray<GrGLuint> shadersToDelete; |
| 668 | |
commit-bot@chromium.org | facad13 | 2014-05-29 22:20:03 +0000 | [diff] [blame] | 669 | if (!this->compileAndAttachShaders(fOutput.fProgramID, &shadersToDelete)) { |
| 670 | GL_CALL(DeleteProgram(fOutput.fProgramID)); |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 671 | return false; |
| 672 | } |
| 673 | |
commit-bot@chromium.org | facad13 | 2014-05-29 22:20:03 +0000 | [diff] [blame] | 674 | this->bindProgramLocations(fOutput.fProgramID); |
commit-bot@chromium.org | 6eac42e | 2014-05-29 21:29:51 +0000 | [diff] [blame] | 675 | if (fUniformManager->isUsingBindUniform()) { |
commit-bot@chromium.org | facad13 | 2014-05-29 22:20:03 +0000 | [diff] [blame] | 676 | fUniformManager->getUniformLocations(fOutput.fProgramID, fUniforms); |
commit-bot@chromium.org | d3baf20 | 2013-11-07 22:06:08 +0000 | [diff] [blame] | 677 | } |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 678 | |
commit-bot@chromium.org | facad13 | 2014-05-29 22:20:03 +0000 | [diff] [blame] | 679 | GL_CALL(LinkProgram(fOutput.fProgramID)); |
commit-bot@chromium.org | c9424b8 | 2013-10-30 20:03:16 +0000 | [diff] [blame] | 680 | |
| 681 | // Calling GetProgramiv is expensive in Chromium. Assume success in release builds. |
| 682 | bool checkLinked = !fGpu->ctxInfo().isChromium(); |
| 683 | #ifdef SK_DEBUG |
| 684 | checkLinked = true; |
| 685 | #endif |
| 686 | if (checkLinked) { |
| 687 | GrGLint linked = 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_LINK_STATUS, &linked)); |
commit-bot@chromium.org | c9424b8 | 2013-10-30 20:03:16 +0000 | [diff] [blame] | 689 | if (!linked) { |
| 690 | GrGLint infoLen = GR_GL_INIT_ZERO; |
commit-bot@chromium.org | facad13 | 2014-05-29 22:20:03 +0000 | [diff] [blame] | 691 | 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] | 692 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
| 693 | if (infoLen > 0) { |
| 694 | // retrieve length even though we don't need it to workaround |
| 695 | // bug in chrome cmd buffer param validation. |
| 696 | GrGLsizei length = GR_GL_INIT_ZERO; |
commit-bot@chromium.org | facad13 | 2014-05-29 22:20:03 +0000 | [diff] [blame] | 697 | GL_CALL(GetProgramInfoLog(fOutput.fProgramID, |
commit-bot@chromium.org | c9424b8 | 2013-10-30 20:03:16 +0000 | [diff] [blame] | 698 | infoLen+1, |
| 699 | &length, |
| 700 | (char*)log.get())); |
| 701 | GrPrintf((char*)log.get()); |
| 702 | } |
| 703 | SkDEBUGFAIL("Error linking program"); |
commit-bot@chromium.org | facad13 | 2014-05-29 22:20:03 +0000 | [diff] [blame] | 704 | GL_CALL(DeleteProgram(fOutput.fProgramID)); |
| 705 | fOutput.fProgramID = 0; |
commit-bot@chromium.org | c9424b8 | 2013-10-30 20:03:16 +0000 | [diff] [blame] | 706 | return false; |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 707 | } |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 708 | } |
| 709 | |
commit-bot@chromium.org | 6eac42e | 2014-05-29 21:29:51 +0000 | [diff] [blame] | 710 | if (!fUniformManager->isUsingBindUniform()) { |
commit-bot@chromium.org | facad13 | 2014-05-29 22:20:03 +0000 | [diff] [blame] | 711 | fUniformManager->getUniformLocations(fOutput.fProgramID, fUniforms); |
commit-bot@chromium.org | d3baf20 | 2013-11-07 22:06:08 +0000 | [diff] [blame] | 712 | } |
commit-bot@chromium.org | d328fb6 | 2014-02-14 00:03:38 +0000 | [diff] [blame] | 713 | |
| 714 | for (int i = 0; i < shadersToDelete.count(); ++i) { |
| 715 | GL_CALL(DeleteShader(shadersToDelete[i])); |
| 716 | } |
| 717 | |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 718 | return true; |
| 719 | } |
| 720 | |
commit-bot@chromium.org | d328fb6 | 2014-02-14 00:03:38 +0000 | [diff] [blame] | 721 | // Compiles a GL shader and attaches it to a program. Returns the shader ID if |
| 722 | // successful, or 0 if not. |
| 723 | static GrGLuint attach_shader(const GrGLContext& glCtx, |
| 724 | GrGLuint programId, |
| 725 | GrGLenum type, |
| 726 | const SkString& shaderSrc) { |
commit-bot@chromium.org | c9424b8 | 2013-10-30 20:03:16 +0000 | [diff] [blame] | 727 | const GrGLInterface* gli = glCtx.interface(); |
| 728 | |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 729 | GrGLuint shaderId; |
| 730 | GR_GL_CALL_RET(gli, shaderId, CreateShader(type)); |
| 731 | if (0 == shaderId) { |
commit-bot@chromium.org | d328fb6 | 2014-02-14 00:03:38 +0000 | [diff] [blame] | 732 | return 0; |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | const GrGLchar* sourceStr = shaderSrc.c_str(); |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 736 | GrGLint sourceLength = static_cast<GrGLint>(shaderSrc.size()); |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 737 | GR_GL_CALL(gli, ShaderSource(shaderId, 1, &sourceStr, &sourceLength)); |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 738 | GR_GL_CALL(gli, CompileShader(shaderId)); |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 739 | |
commit-bot@chromium.org | c9424b8 | 2013-10-30 20:03:16 +0000 | [diff] [blame] | 740 | // 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] | 741 | bool checkCompiled = !glCtx.isChromium(); |
commit-bot@chromium.org | c9424b8 | 2013-10-30 20:03:16 +0000 | [diff] [blame] | 742 | #ifdef SK_DEBUG |
| 743 | checkCompiled = true; |
| 744 | #endif |
| 745 | if (checkCompiled) { |
| 746 | GrGLint compiled = GR_GL_INIT_ZERO; |
| 747 | GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_COMPILE_STATUS, &compiled)); |
| 748 | |
| 749 | if (!compiled) { |
| 750 | GrGLint infoLen = GR_GL_INIT_ZERO; |
| 751 | GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
| 752 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
| 753 | if (infoLen > 0) { |
| 754 | // retrieve length even though we don't need it to workaround bug in Chromium cmd |
| 755 | // buffer param validation. |
| 756 | GrGLsizei length = GR_GL_INIT_ZERO; |
| 757 | GR_GL_CALL(gli, GetShaderInfoLog(shaderId, infoLen+1, |
| 758 | &length, (char*)log.get())); |
| 759 | GrPrintf(shaderSrc.c_str()); |
| 760 | GrPrintf("\n%s", log.get()); |
| 761 | } |
| 762 | SkDEBUGFAIL("Shader compilation failed!"); |
| 763 | GR_GL_CALL(gli, DeleteShader(shaderId)); |
commit-bot@chromium.org | d328fb6 | 2014-02-14 00:03:38 +0000 | [diff] [blame] | 764 | return 0; |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 765 | } |
commit-bot@chromium.org | c9424b8 | 2013-10-30 20:03:16 +0000 | [diff] [blame] | 766 | } |
| 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 | } |