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