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