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" |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 12 | #include "GrTexture.h" |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 13 | |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 14 | // number of each input/output type in a single allocation block |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 15 | static const int kVarsPerBlock = 8; |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 16 | |
| 17 | // except FS outputs where we expect 2 at most. |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 18 | static const int kMaxFSOutputs = 2; |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 19 | |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 20 | // ES2 FS only guarantees mediump and lowp support |
| 21 | static const GrGLShaderVar::Precision kDefaultFragmentPrecision = GrGLShaderVar::kMedium_Precision; |
| 22 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 23 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
| 24 | /////////////////////////////////////////////////////////////////////////////// |
| 25 | |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 26 | namespace { |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 27 | |
bsalomon@google.com | 2b1b8c0 | 2013-02-28 22:06:02 +0000 | [diff] [blame] | 28 | inline const char* sample_function_name(GrSLType type, GrGLSLGeneration glslGen) { |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 29 | if (kVec2f_GrSLType == type) { |
bsalomon@google.com | 2b1b8c0 | 2013-02-28 22:06:02 +0000 | [diff] [blame] | 30 | return glslGen >= k130_GrGLSLGeneration ? "texture" : "texture2D"; |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 31 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame^] | 32 | SkASSERT(kVec3f_GrSLType == type); |
bsalomon@google.com | 2b1b8c0 | 2013-02-28 22:06:02 +0000 | [diff] [blame] | 33 | return glslGen >= k130_GrGLSLGeneration ? "textureProj" : "texture2DProj"; |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 34 | } |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 35 | } |
| 36 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 37 | /** |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 38 | * Do we need to either map r,g,b->a or a->r. configComponentMask indicates which channels are |
| 39 | * present in the texture's config. swizzleComponentMask indicates the channels present in the |
| 40 | * shader swizzle. |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 41 | */ |
| 42 | inline bool swizzle_requires_alpha_remapping(const GrGLCaps& caps, |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 43 | uint32_t configComponentMask, |
| 44 | uint32_t swizzleComponentMask) { |
| 45 | if (caps.textureSwizzleSupport()) { |
| 46 | // Any remapping is handled using texture swizzling not shader modifications. |
| 47 | return false; |
| 48 | } |
| 49 | // check if the texture is alpha-only |
| 50 | if (kA_GrColorComponentFlag == configComponentMask) { |
| 51 | if (caps.textureRedSupport() && (kA_GrColorComponentFlag & swizzleComponentMask)) { |
| 52 | // we must map the swizzle 'a's to 'r'. |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 53 | return true; |
| 54 | } |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 55 | if (kRGB_GrColorComponentFlags & swizzleComponentMask) { |
| 56 | // The 'r', 'g', and/or 'b's must be mapped to 'a' according to our semantics that |
| 57 | // alpha-only textures smear alpha across all four channels when read. |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 58 | return true; |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 61 | return false; |
| 62 | } |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 63 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 64 | void append_swizzle(SkString* outAppend, |
bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 65 | const GrGLShaderBuilder::TextureSampler& texSampler, |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 66 | const GrGLCaps& caps) { |
bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 67 | const char* swizzle = texSampler.swizzle(); |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 68 | char mangledSwizzle[5]; |
| 69 | |
| 70 | // The swizzling occurs using texture params instead of shader-mangling if ARB_texture_swizzle |
| 71 | // is available. |
bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 72 | if (!caps.textureSwizzleSupport() && |
| 73 | (kA_GrColorComponentFlag == texSampler.configComponentMask())) { |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 74 | char alphaChar = caps.textureRedSupport() ? 'r' : 'a'; |
| 75 | int i; |
| 76 | for (i = 0; '\0' != swizzle[i]; ++i) { |
| 77 | mangledSwizzle[i] = alphaChar; |
| 78 | } |
| 79 | mangledSwizzle[i] ='\0'; |
| 80 | swizzle = mangledSwizzle; |
| 81 | } |
bsalomon@google.com | 73d5b2f | 2012-10-04 13:26:32 +0000 | [diff] [blame] | 82 | // For shader prettiness we omit the swizzle rather than appending ".rgba". |
| 83 | if (memcmp(swizzle, "rgba", 4)) { |
| 84 | outAppend->appendf(".%s", swizzle); |
| 85 | } |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 86 | } |
| 87 | |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 88 | } |
| 89 | |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 90 | static const char kDstCopyColorName[] = "_dstColor"; |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 91 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 92 | /////////////////////////////////////////////////////////////////////////////// |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 93 | |
skia.committer@gmail.com | 631cdcb | 2013-03-01 12:12:55 +0000 | [diff] [blame] | 94 | GrGLShaderBuilder::GrGLShaderBuilder(const GrGLContextInfo& ctxInfo, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 95 | GrGLUniformManager& uniformManager, |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 96 | const GrGLProgramDesc& desc) |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 97 | : fUniforms(kVarsPerBlock) |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 98 | , fVSAttrs(kVarsPerBlock) |
| 99 | , fVSOutputs(kVarsPerBlock) |
| 100 | , fGSInputs(kVarsPerBlock) |
| 101 | , fGSOutputs(kVarsPerBlock) |
| 102 | , fFSInputs(kVarsPerBlock) |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 103 | , fFSOutputs(kMaxFSOutputs) |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 104 | , fCtxInfo(ctxInfo) |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 105 | , fUniformManager(uniformManager) |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 106 | , fFSFeaturesAddedMask(0) |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 107 | #if GR_GL_EXPERIMENTAL_GS |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 108 | , fUsesGS(SkToBool(desc.getHeader().fExperimentalGS)) |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 109 | #else |
| 110 | , fUsesGS(false) |
| 111 | #endif |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 112 | , fSetupFragPosition(false) |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 113 | , fTopLeftFragPosRead(kTopLeftFragPosRead_FragPosKey == desc.getHeader().fFragPosKey) { |
| 114 | |
| 115 | const GrGLProgramDesc::KeyHeader& header = desc.getHeader(); |
skia.committer@gmail.com | e862d16 | 2012-10-31 02:01:18 +0000 | [diff] [blame] | 116 | |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 117 | fPositionVar = &fVSAttrs.push_back(); |
| 118 | fPositionVar->set(kVec2f_GrSLType, GrGLShaderVar::kAttribute_TypeModifier, "aPosition"); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 119 | if (-1 != header.fLocalCoordAttributeIndex) { |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 120 | fLocalCoordsVar = &fVSAttrs.push_back(); |
| 121 | fLocalCoordsVar->set(kVec2f_GrSLType, |
| 122 | GrGLShaderVar::kAttribute_TypeModifier, |
| 123 | "aLocalCoords"); |
| 124 | } else { |
| 125 | fLocalCoordsVar = fPositionVar; |
| 126 | } |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 127 | // Emit code to read the dst copy textue if necessary. |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 128 | if (kNoDstRead_DstReadKey != header.fDstReadKey && |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 129 | GrGLCaps::kNone_FBFetchType == ctxInfo.caps()->fbFetchType()) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 130 | bool topDown = SkToBool(kTopLeftOrigin_DstReadKeyBit & header.fDstReadKey); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 131 | const char* dstCopyTopLeftName; |
| 132 | const char* dstCopyCoordScaleName; |
| 133 | uint32_t configMask; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 134 | if (SkToBool(kUseAlphaConfig_DstReadKeyBit & header.fDstReadKey)) { |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 135 | configMask = kA_GrColorComponentFlag; |
| 136 | } else { |
| 137 | configMask = kRGBA_GrColorComponentFlags; |
| 138 | } |
| 139 | fDstCopySampler.init(this, configMask, "rgba", 0); |
| 140 | |
| 141 | fDstCopyTopLeftUniform = this->addUniform(kFragment_ShaderType, |
| 142 | kVec2f_GrSLType, |
| 143 | "DstCopyUpperLeft", |
| 144 | &dstCopyTopLeftName); |
| 145 | fDstCopyScaleUniform = this->addUniform(kFragment_ShaderType, |
| 146 | kVec2f_GrSLType, |
| 147 | "DstCopyCoordScale", |
| 148 | &dstCopyCoordScaleName); |
| 149 | const char* fragPos = this->fragmentPosition(); |
| 150 | this->fsCodeAppend("\t// Read color from copy of the destination.\n"); |
| 151 | this->fsCodeAppendf("\tvec2 _dstTexCoord = (%s.xy - %s) * %s;\n", |
| 152 | fragPos, dstCopyTopLeftName, dstCopyCoordScaleName); |
| 153 | if (!topDown) { |
| 154 | this->fsCodeAppend("\t_dstTexCoord.y = 1.0 - _dstTexCoord.y;\n"); |
| 155 | } |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 156 | this->fsCodeAppendf("\tvec4 %s = ", kDstCopyColorName); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 157 | this->appendTextureLookup(kFragment_ShaderType, fDstCopySampler, "_dstTexCoord"); |
| 158 | this->fsCodeAppend(";\n\n"); |
| 159 | } |
| 160 | } |
| 161 | |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 162 | bool GrGLShaderBuilder::enableFeature(GLSLFeature feature) { |
| 163 | switch (feature) { |
| 164 | case kStandardDerivatives_GLSLFeature: |
| 165 | if (!fCtxInfo.caps()->shaderDerivativeSupport()) { |
| 166 | return false; |
| 167 | } |
bsalomon@google.com | 791816a | 2013-08-15 18:54:39 +0000 | [diff] [blame] | 168 | if (kES_GrGLBinding == fCtxInfo.binding()) { |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 169 | this->addFSFeature(1 << kStandardDerivatives_GLSLFeature, |
| 170 | "GL_OES_standard_derivatives"); |
| 171 | } |
| 172 | return true; |
| 173 | default: |
| 174 | GrCrash("Unexpected GLSLFeature requested."); |
| 175 | return false; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | bool GrGLShaderBuilder::enablePrivateFeature(GLSLPrivateFeature feature) { |
| 180 | switch (feature) { |
| 181 | case kFragCoordConventions_GLSLPrivateFeature: |
| 182 | if (!fCtxInfo.caps()->fragCoordConventionsSupport()) { |
| 183 | return false; |
| 184 | } |
| 185 | if (fCtxInfo.glslGeneration() < k150_GrGLSLGeneration) { |
| 186 | this->addFSFeature(1 << kFragCoordConventions_GLSLPrivateFeature, |
| 187 | "GL_ARB_fragment_coord_conventions"); |
| 188 | } |
| 189 | return true; |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 190 | case kEXTShaderFramebufferFetch_GLSLPrivateFeature: |
| 191 | if (GrGLCaps::kEXT_FBFetchType != fCtxInfo.caps()->fbFetchType()) { |
| 192 | return false; |
| 193 | } |
| 194 | this->addFSFeature(1 << kEXTShaderFramebufferFetch_GLSLPrivateFeature, |
| 195 | "GL_EXT_shader_framebuffer_fetch"); |
| 196 | return true; |
| 197 | case kNVShaderFramebufferFetch_GLSLPrivateFeature: |
| 198 | if (GrGLCaps::kNV_FBFetchType != fCtxInfo.caps()->fbFetchType()) { |
| 199 | return false; |
| 200 | } |
| 201 | this->addFSFeature(1 << kNVShaderFramebufferFetch_GLSLPrivateFeature, |
| 202 | "GL_NV_shader_framebuffer_fetch"); |
| 203 | return true; |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 204 | default: |
| 205 | GrCrash("Unexpected GLSLPrivateFeature requested."); |
| 206 | return false; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | void GrGLShaderBuilder::addFSFeature(uint32_t featureBit, const char* extensionName) { |
| 211 | if (!(featureBit & fFSFeaturesAddedMask)) { |
| 212 | fFSExtensions.appendf("#extension %s: require\n", extensionName); |
| 213 | fFSFeaturesAddedMask |= featureBit; |
| 214 | } |
| 215 | } |
| 216 | |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 217 | void GrGLShaderBuilder::nameVariable(SkString* out, char prefix, const char* name) { |
| 218 | if ('\0' == prefix) { |
| 219 | *out = name; |
| 220 | } else { |
| 221 | out->printf("%c%s", prefix, name); |
| 222 | } |
| 223 | if (fCodeStage.inStageCode()) { |
| 224 | if (out->endsWith('_')) { |
| 225 | // Names containing "__" are reserved. |
| 226 | out->append("x"); |
| 227 | } |
| 228 | out->appendf("_Stage%d", fCodeStage.stageIndex()); |
| 229 | } |
| 230 | } |
| 231 | |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 232 | const char* GrGLShaderBuilder::dstColor() { |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 233 | if (fCodeStage.inStageCode()) { |
| 234 | const GrEffectRef& effect = *fCodeStage.effectStage()->getEffect(); |
| 235 | if (!effect->willReadDstColor()) { |
| 236 | GrDebugCrash("GrGLEffect asked for dst color but its generating GrEffect " |
| 237 | "did not request access."); |
| 238 | return ""; |
| 239 | } |
| 240 | } |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 241 | static const char kFBFetchColorName[] = "gl_LastFragData[0]"; |
| 242 | GrGLCaps::FBFetchType fetchType = fCtxInfo.caps()->fbFetchType(); |
| 243 | if (GrGLCaps::kEXT_FBFetchType == fetchType) { |
| 244 | SkAssertResult(this->enablePrivateFeature(kEXTShaderFramebufferFetch_GLSLPrivateFeature)); |
| 245 | return kFBFetchColorName; |
| 246 | } else if (GrGLCaps::kNV_FBFetchType == fetchType) { |
| 247 | SkAssertResult(this->enablePrivateFeature(kNVShaderFramebufferFetch_GLSLPrivateFeature)); |
| 248 | return kFBFetchColorName; |
| 249 | } else if (fDstCopySampler.isInitialized()) { |
| 250 | return kDstCopyColorName; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 251 | } else { |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 252 | return ""; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 253 | } |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 254 | } |
| 255 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 256 | void GrGLShaderBuilder::codeAppendf(ShaderType type, const char format[], va_list args) { |
sugoi@google.com | 9c55f80 | 2013-03-07 20:52:59 +0000 | [diff] [blame] | 257 | SkString* string = NULL; |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 258 | switch (type) { |
| 259 | case kVertex_ShaderType: |
| 260 | string = &fVSCode; |
| 261 | break; |
| 262 | case kGeometry_ShaderType: |
| 263 | string = &fGSCode; |
| 264 | break; |
| 265 | case kFragment_ShaderType: |
| 266 | string = &fFSCode; |
| 267 | break; |
| 268 | default: |
| 269 | GrCrash("Invalid shader type"); |
| 270 | } |
| 271 | string->appendf(format, args); |
| 272 | } |
| 273 | |
| 274 | void GrGLShaderBuilder::codeAppend(ShaderType type, const char* str) { |
sugoi@google.com | 9c55f80 | 2013-03-07 20:52:59 +0000 | [diff] [blame] | 275 | SkString* string = NULL; |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 276 | switch (type) { |
| 277 | case kVertex_ShaderType: |
| 278 | string = &fVSCode; |
| 279 | break; |
| 280 | case kGeometry_ShaderType: |
| 281 | string = &fGSCode; |
| 282 | break; |
| 283 | case kFragment_ShaderType: |
| 284 | string = &fFSCode; |
| 285 | break; |
| 286 | default: |
| 287 | GrCrash("Invalid shader type"); |
| 288 | } |
| 289 | string->append(str); |
| 290 | } |
| 291 | |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 292 | void GrGLShaderBuilder::appendTextureLookup(SkString* out, |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 293 | const GrGLShaderBuilder::TextureSampler& sampler, |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 294 | const char* coordName, |
| 295 | GrSLType varyingType) const { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame^] | 296 | SkASSERT(NULL != coordName); |
bsalomon@google.com | 2d8edaf | 2012-09-07 14:47:31 +0000 | [diff] [blame] | 297 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 298 | out->appendf("%s(%s, %s)", |
bsalomon@google.com | 2b1b8c0 | 2013-02-28 22:06:02 +0000 | [diff] [blame] | 299 | sample_function_name(varyingType, fCtxInfo.glslGeneration()), |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 300 | this->getUniformCStr(sampler.fSamplerUniform), |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 301 | coordName); |
bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 302 | append_swizzle(out, sampler, *fCtxInfo.caps()); |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 303 | } |
| 304 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 305 | void GrGLShaderBuilder::appendTextureLookup(ShaderType type, |
| 306 | const GrGLShaderBuilder::TextureSampler& sampler, |
| 307 | const char* coordName, |
| 308 | GrSLType varyingType) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame^] | 309 | SkASSERT(kFragment_ShaderType == type); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 310 | this->appendTextureLookup(&fFSCode, sampler, coordName, varyingType); |
| 311 | } |
| 312 | |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 313 | void GrGLShaderBuilder::appendTextureLookupAndModulate( |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 314 | ShaderType type, |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 315 | const char* modulation, |
| 316 | const GrGLShaderBuilder::TextureSampler& sampler, |
| 317 | const char* coordName, |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 318 | GrSLType varyingType) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame^] | 319 | SkASSERT(kFragment_ShaderType == type); |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 320 | SkString lookup; |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 321 | this->appendTextureLookup(&lookup, sampler, coordName, varyingType); |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 322 | GrGLSLModulatef<4>(&fFSCode, modulation, lookup.c_str()); |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 323 | } |
| 324 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 325 | GrBackendEffectFactory::EffectKey GrGLShaderBuilder::KeyForTextureAccess( |
| 326 | const GrTextureAccess& access, |
| 327 | const GrGLCaps& caps) { |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 328 | uint32_t configComponentMask = GrPixelConfigComponentMask(access.getTexture()->config()); |
| 329 | if (swizzle_requires_alpha_remapping(caps, configComponentMask, access.swizzleMask())) { |
| 330 | return 1; |
| 331 | } else { |
| 332 | return 0; |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 333 | } |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | GrGLShaderBuilder::DstReadKey GrGLShaderBuilder::KeyForDstRead(const GrTexture* dstCopy, |
| 337 | const GrGLCaps& caps) { |
| 338 | uint32_t key = kYesDstRead_DstReadKeyBit; |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 339 | if (GrGLCaps::kNone_FBFetchType != caps.fbFetchType()) { |
| 340 | return key; |
| 341 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame^] | 342 | SkASSERT(NULL != dstCopy); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 343 | if (!caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(dstCopy->config())) { |
| 344 | // The fact that the config is alpha-only must be considered when generating code. |
| 345 | key |= kUseAlphaConfig_DstReadKeyBit; |
| 346 | } |
| 347 | if (kTopLeft_GrSurfaceOrigin == dstCopy->origin()) { |
| 348 | key |= kTopLeftOrigin_DstReadKeyBit; |
| 349 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame^] | 350 | SkASSERT(static_cast<DstReadKey>(key) == key); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 351 | return static_cast<DstReadKey>(key); |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 352 | } |
| 353 | |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 354 | GrGLShaderBuilder::FragPosKey GrGLShaderBuilder::KeyForFragmentPosition(const GrRenderTarget* dst, |
| 355 | const GrGLCaps&) { |
| 356 | if (kTopLeft_GrSurfaceOrigin == dst->origin()) { |
| 357 | return kTopLeftFragPosRead_FragPosKey; |
| 358 | } else { |
| 359 | return kBottomLeftFragPosRead_FragPosKey; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 364 | const GrGLenum* GrGLShaderBuilder::GetTexParamSwizzle(GrPixelConfig config, const GrGLCaps& caps) { |
| 365 | if (caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(config)) { |
| 366 | if (caps.textureRedSupport()) { |
| 367 | static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED, GR_GL_RED, GR_GL_RED }; |
| 368 | return gRedSmear; |
| 369 | } else { |
| 370 | static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA, |
| 371 | GR_GL_ALPHA, GR_GL_ALPHA }; |
| 372 | return gAlphaSmear; |
| 373 | } |
| 374 | } else { |
| 375 | static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN, GR_GL_BLUE, GR_GL_ALPHA }; |
| 376 | return gStraight; |
| 377 | } |
| 378 | } |
| 379 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 380 | GrGLUniformManager::UniformHandle GrGLShaderBuilder::addUniformArray(uint32_t visibility, |
| 381 | GrSLType type, |
| 382 | const char* name, |
| 383 | int count, |
| 384 | const char** outName) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame^] | 385 | SkASSERT(name && strlen(name)); |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 386 | SkDEBUGCODE(static const uint32_t kVisibilityMask = kVertex_ShaderType | kFragment_ShaderType); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame^] | 387 | SkASSERT(0 == (~kVisibilityMask & visibility)); |
| 388 | SkASSERT(0 != visibility); |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 389 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 390 | BuilderUniform& uni = fUniforms.push_back(); |
commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 391 | UniformHandle h = GrGLUniformManager::UniformHandle::CreateFromUniformIndex(fUniforms.count() - 1); |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 392 | GR_DEBUGCODE(UniformHandle h2 =) |
| 393 | fUniformManager.appendUniform(type, count); |
| 394 | // We expect the uniform manager to initially have no uniforms and that all uniforms are added |
| 395 | // by this function. Therefore, the handles should match. |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame^] | 396 | SkASSERT(h2 == h); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 397 | uni.fVariable.setType(type); |
| 398 | uni.fVariable.setTypeModifier(GrGLShaderVar::kUniform_TypeModifier); |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 399 | this->nameVariable(uni.fVariable.accessName(), 'u', name); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 400 | uni.fVariable.setArrayCount(count); |
| 401 | uni.fVisibility = visibility; |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 402 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 403 | // If it is visible in both the VS and FS, the precision must match. |
| 404 | // We declare a default FS precision, but not a default VS. So set the var |
| 405 | // to use the default FS precision. |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 406 | if ((kVertex_ShaderType | kFragment_ShaderType) == visibility) { |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 407 | // the fragment and vertex precisions must match |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 408 | uni.fVariable.setPrecision(kDefaultFragmentPrecision); |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 409 | } |
| 410 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 411 | if (NULL != outName) { |
| 412 | *outName = uni.fVariable.c_str(); |
| 413 | } |
| 414 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 415 | return h; |
| 416 | } |
| 417 | |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 418 | bool GrGLShaderBuilder::addAttribute(GrSLType type, |
| 419 | const char* name) { |
| 420 | for (int i = 0; i < fVSAttrs.count(); ++i) { |
| 421 | const GrGLShaderVar& attr = fVSAttrs[i]; |
| 422 | // if attribute already added, don't add it again |
| 423 | if (attr.getName().equals(name)) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame^] | 424 | SkASSERT(attr.getType() == type); |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 425 | return false; |
| 426 | } |
| 427 | } |
| 428 | fVSAttrs.push_back().set(type, |
| 429 | GrGLShaderVar::kAttribute_TypeModifier, |
| 430 | name); |
| 431 | return true; |
| 432 | } |
| 433 | |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 434 | void GrGLShaderBuilder::addVarying(GrSLType type, |
| 435 | const char* name, |
| 436 | const char** vsOutName, |
| 437 | const char** fsInName) { |
| 438 | fVSOutputs.push_back(); |
| 439 | fVSOutputs.back().setType(type); |
bsalomon@google.com | 77cf460 | 2013-04-22 21:05:48 +0000 | [diff] [blame] | 440 | fVSOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 441 | this->nameVariable(fVSOutputs.back().accessName(), 'v', name); |
| 442 | |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 443 | if (vsOutName) { |
| 444 | *vsOutName = fVSOutputs.back().getName().c_str(); |
| 445 | } |
| 446 | // input to FS comes either from VS or GS |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 447 | const SkString* fsName; |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 448 | if (fUsesGS) { |
| 449 | // if we have a GS take each varying in as an array |
| 450 | // and output as non-array. |
| 451 | fGSInputs.push_back(); |
| 452 | fGSInputs.back().setType(type); |
bsalomon@google.com | 77cf460 | 2013-04-22 21:05:48 +0000 | [diff] [blame] | 453 | fGSInputs.back().setTypeModifier(GrGLShaderVar::kVaryingIn_TypeModifier); |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 454 | fGSInputs.back().setUnsizedArray(); |
| 455 | *fGSInputs.back().accessName() = fVSOutputs.back().getName(); |
| 456 | fGSOutputs.push_back(); |
| 457 | fGSOutputs.back().setType(type); |
bsalomon@google.com | 77cf460 | 2013-04-22 21:05:48 +0000 | [diff] [blame] | 458 | fGSOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 459 | this->nameVariable(fGSOutputs.back().accessName(), 'g', name); |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 460 | fsName = fGSOutputs.back().accessName(); |
| 461 | } else { |
| 462 | fsName = fVSOutputs.back().accessName(); |
| 463 | } |
| 464 | fFSInputs.push_back(); |
| 465 | fFSInputs.back().setType(type); |
bsalomon@google.com | 77cf460 | 2013-04-22 21:05:48 +0000 | [diff] [blame] | 466 | fFSInputs.back().setTypeModifier(GrGLShaderVar::kVaryingIn_TypeModifier); |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 467 | fFSInputs.back().setName(*fsName); |
| 468 | if (fsInName) { |
| 469 | *fsInName = fsName->c_str(); |
| 470 | } |
| 471 | } |
| 472 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 473 | const char* GrGLShaderBuilder::fragmentPosition() { |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 474 | if (fCodeStage.inStageCode()) { |
| 475 | const GrEffectRef& effect = *fCodeStage.effectStage()->getEffect(); |
| 476 | if (!effect->willReadFragmentPosition()) { |
| 477 | GrDebugCrash("GrGLEffect asked for frag position but its generating GrEffect " |
| 478 | "did not request access."); |
| 479 | return ""; |
| 480 | } |
| 481 | } |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 482 | if (fTopLeftFragPosRead) { |
| 483 | if (!fSetupFragPosition) { |
| 484 | fFSInputs.push_back().set(kVec4f_GrSLType, |
| 485 | GrGLShaderVar::kIn_TypeModifier, |
| 486 | "gl_FragCoord", |
| 487 | GrGLShaderVar::kDefault_Precision); |
| 488 | fSetupFragPosition = true; |
| 489 | } |
| 490 | return "gl_FragCoord"; |
| 491 | } else if (fCtxInfo.caps()->fragCoordConventionsSupport()) { |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 492 | if (!fSetupFragPosition) { |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 493 | SkAssertResult(this->enablePrivateFeature(kFragCoordConventions_GLSLPrivateFeature)); |
bsalomon@google.com | 5fa2107 | 2012-10-25 14:57:46 +0000 | [diff] [blame] | 494 | fFSInputs.push_back().set(kVec4f_GrSLType, |
| 495 | GrGLShaderVar::kIn_TypeModifier, |
| 496 | "gl_FragCoord", |
| 497 | GrGLShaderVar::kDefault_Precision, |
| 498 | GrGLShaderVar::kUpperLeft_Origin); |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 499 | fSetupFragPosition = true; |
| 500 | } |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 501 | return "gl_FragCoord"; |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 502 | } else { |
| 503 | static const char* kCoordName = "fragCoordYDown"; |
| 504 | if (!fSetupFragPosition) { |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 505 | // temporarily change the stage index because we're inserting non-stage code. |
| 506 | CodeStage::AutoStageRestore csar(&fCodeStage, NULL); |
| 507 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame^] | 508 | SkASSERT(!fRTHeightUniform.isValid()); |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 509 | const char* rtHeightName; |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 510 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 511 | fRTHeightUniform = this->addUniform(kFragment_ShaderType, |
| 512 | kFloat_GrSLType, |
| 513 | "RTHeight", |
| 514 | &rtHeightName); |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 515 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 516 | this->fFSCode.prependf("\tvec4 %s = vec4(gl_FragCoord.x, %s - gl_FragCoord.y, gl_FragCoord.zw);\n", |
| 517 | kCoordName, rtHeightName); |
| 518 | fSetupFragPosition = true; |
| 519 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame^] | 520 | SkASSERT(fRTHeightUniform.isValid()); |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 521 | return kCoordName; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 526 | void GrGLShaderBuilder::emitFunction(ShaderType shader, |
| 527 | GrSLType returnType, |
| 528 | const char* name, |
| 529 | int argCnt, |
| 530 | const GrGLShaderVar* args, |
| 531 | const char* body, |
| 532 | SkString* outName) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame^] | 533 | SkASSERT(kFragment_ShaderType == shader); |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 534 | fFSFunctions.append(GrGLSLTypeString(returnType)); |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 535 | this->nameVariable(outName, '\0', name); |
bsalomon@google.com | 77cf460 | 2013-04-22 21:05:48 +0000 | [diff] [blame] | 536 | fFSFunctions.appendf(" %s", outName->c_str()); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 537 | fFSFunctions.append("("); |
| 538 | for (int i = 0; i < argCnt; ++i) { |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 539 | args[i].appendDecl(fCtxInfo, &fFSFunctions); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 540 | if (i < argCnt - 1) { |
| 541 | fFSFunctions.append(", "); |
| 542 | } |
| 543 | } |
| 544 | fFSFunctions.append(") {\n"); |
| 545 | fFSFunctions.append(body); |
| 546 | fFSFunctions.append("}\n\n"); |
| 547 | } |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 548 | |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 549 | namespace { |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 550 | |
| 551 | inline void append_default_precision_qualifier(GrGLShaderVar::Precision p, |
| 552 | GrGLBinding binding, |
| 553 | SkString* str) { |
| 554 | // 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] | 555 | if (kES_GrGLBinding == binding) { |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 556 | switch (p) { |
| 557 | case GrGLShaderVar::kHigh_Precision: |
| 558 | str->append("precision highp float;\n"); |
| 559 | break; |
| 560 | case GrGLShaderVar::kMedium_Precision: |
| 561 | str->append("precision mediump float;\n"); |
| 562 | break; |
| 563 | case GrGLShaderVar::kLow_Precision: |
| 564 | str->append("precision lowp float;\n"); |
| 565 | break; |
| 566 | case GrGLShaderVar::kDefault_Precision: |
| 567 | GrCrash("Default precision now allowed."); |
| 568 | default: |
| 569 | GrCrash("Unknown precision value."); |
| 570 | } |
| 571 | } |
| 572 | } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 573 | } |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 574 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 575 | void GrGLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 576 | for (int i = 0; i < vars.count(); ++i) { |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 577 | vars[i].appendDecl(fCtxInfo, out); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 578 | out->append(";\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 579 | } |
| 580 | } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 581 | |
| 582 | void GrGLShaderBuilder::appendUniformDecls(ShaderType stype, SkString* out) const { |
| 583 | for (int i = 0; i < fUniforms.count(); ++i) { |
| 584 | if (fUniforms[i].fVisibility & stype) { |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 585 | fUniforms[i].fVariable.appendDecl(fCtxInfo, out); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 586 | out->append(";\n"); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 587 | } |
| 588 | } |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | void GrGLShaderBuilder::getShader(ShaderType type, SkString* shaderStr) const { |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 592 | const char* version = GrGetGLSLVersionDecl(fCtxInfo.binding(), fCtxInfo.glslGeneration()); |
| 593 | |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 594 | switch (type) { |
| 595 | case kVertex_ShaderType: |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 596 | *shaderStr = version; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 597 | this->appendUniformDecls(kVertex_ShaderType, shaderStr); |
| 598 | this->appendDecls(fVSAttrs, shaderStr); |
| 599 | this->appendDecls(fVSOutputs, shaderStr); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 600 | shaderStr->append("void main() {\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 601 | shaderStr->append(fVSCode); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 602 | shaderStr->append("}\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 603 | break; |
| 604 | case kGeometry_ShaderType: |
| 605 | if (fUsesGS) { |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 606 | *shaderStr = version; |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 607 | shaderStr->append(fGSHeader); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 608 | this->appendDecls(fGSInputs, shaderStr); |
| 609 | this->appendDecls(fGSOutputs, shaderStr); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 610 | shaderStr->append("void main() {\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 611 | shaderStr->append(fGSCode); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 612 | shaderStr->append("}\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 613 | } else { |
| 614 | shaderStr->reset(); |
| 615 | } |
| 616 | break; |
| 617 | case kFragment_ShaderType: |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 618 | *shaderStr = version; |
| 619 | shaderStr->append(fFSExtensions); |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 620 | append_default_precision_qualifier(kDefaultFragmentPrecision, |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 621 | fCtxInfo.binding(), |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 622 | shaderStr); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 623 | this->appendUniformDecls(kFragment_ShaderType, shaderStr); |
| 624 | this->appendDecls(fFSInputs, shaderStr); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 625 | // We shouldn't have declared outputs on 1.10 |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame^] | 626 | SkASSERT(k110_GrGLSLGeneration != fCtxInfo.glslGeneration() || fFSOutputs.empty()); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 627 | this->appendDecls(fFSOutputs, shaderStr); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 628 | shaderStr->append(fFSFunctions); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 629 | shaderStr->append("void main() {\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 630 | shaderStr->append(fFSCode); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 631 | shaderStr->append("}\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 632 | break; |
| 633 | } |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 634 | } |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 635 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 636 | void GrGLShaderBuilder::finished(GrGLuint programID) { |
| 637 | fUniformManager.getUniformLocations(programID, fUniforms); |
| 638 | } |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 639 | |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 640 | void GrGLShaderBuilder::emitEffects( |
| 641 | const GrEffectStage* effectStages[], |
| 642 | const GrBackendEffectFactory::EffectKey effectKeys[], |
| 643 | int effectCnt, |
| 644 | SkString* fsInOutColor, |
| 645 | GrSLConstantVec* fsInOutColorKnownValue, |
| 646 | SkTArray<GrGLUniformManager::UniformHandle, true>* effectSamplerHandles[], |
| 647 | GrGLEffect* glEffects[]) { |
| 648 | bool effectEmitted = false; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 649 | |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 650 | SkString inColor = *fsInOutColor; |
| 651 | SkString outColor; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 652 | |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 653 | for (int e = 0; e < effectCnt; ++e) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame^] | 654 | SkASSERT(NULL != effectStages[e] && NULL != effectStages[e]->getEffect()); |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 655 | const GrEffectStage& stage = *effectStages[e]; |
| 656 | const GrEffectRef& effect = *stage.getEffect(); |
| 657 | |
| 658 | CodeStage::AutoStageRestore csar(&fCodeStage, &stage); |
| 659 | |
| 660 | int numTextures = effect->numTextures(); |
| 661 | SkSTArray<8, GrGLShaderBuilder::TextureSampler> textureSamplers; |
| 662 | textureSamplers.push_back_n(numTextures); |
| 663 | for (int t = 0; t < numTextures; ++t) { |
| 664 | textureSamplers[t].init(this, &effect->textureAccess(t), t); |
| 665 | effectSamplerHandles[e]->push_back(textureSamplers[t].fSamplerUniform); |
| 666 | } |
| 667 | GrDrawEffect drawEffect(stage, this->hasExplicitLocalCoords()); |
| 668 | |
| 669 | int numAttributes = stage.getVertexAttribIndexCount(); |
| 670 | const int* attributeIndices = stage.getVertexAttribIndices(); |
| 671 | SkSTArray<GrEffect::kMaxVertexAttribs, SkString> attributeNames; |
| 672 | for (int a = 0; a < numAttributes; ++a) { |
| 673 | // TODO: Make addAttribute mangle the name. |
| 674 | SkString attributeName("aAttr"); |
| 675 | attributeName.appendS32(attributeIndices[a]); |
| 676 | if (this->addAttribute(effect->vertexAttribType(a), attributeName.c_str())) { |
| 677 | fEffectAttributes.push_back().set(attributeIndices[a], attributeName); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | glEffects[e] = effect->getFactory().createGLInstance(drawEffect); |
| 682 | |
| 683 | if (kZeros_GrSLConstantVec == *fsInOutColorKnownValue) { |
| 684 | // Effects have no way to communicate zeros, they treat an empty string as ones. |
| 685 | this->nameVariable(&inColor, '\0', "input"); |
| 686 | this->fsCodeAppendf("\tvec4 %s = %s;\n", inColor.c_str(), GrGLSLZerosVecf(4)); |
| 687 | } |
| 688 | |
| 689 | // create var to hold stage result |
| 690 | this->nameVariable(&outColor, '\0', "output"); |
| 691 | this->fsCodeAppendf("\tvec4 %s;\n", outColor.c_str()); |
| 692 | |
| 693 | // Enclose custom code in a block to avoid namespace conflicts |
| 694 | SkString openBrace; |
| 695 | openBrace.printf("\t{ // Stage %d: %s\n", fCodeStage.stageIndex(), glEffects[e]->name()); |
| 696 | this->fVSCode.append(openBrace); |
| 697 | this->fFSCode.append(openBrace); |
| 698 | |
| 699 | glEffects[e]->emitCode(this, |
| 700 | drawEffect, |
| 701 | effectKeys[e], |
| 702 | outColor.c_str(), |
| 703 | inColor.isEmpty() ? NULL : inColor.c_str(), |
| 704 | textureSamplers); |
| 705 | this->fVSCode.append("\t}\n"); |
| 706 | this->fFSCode.append("\t}\n"); |
| 707 | |
| 708 | inColor = outColor; |
| 709 | *fsInOutColorKnownValue = kNone_GrSLConstantVec; |
| 710 | effectEmitted = true; |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 711 | } |
| 712 | |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 713 | if (effectEmitted) { |
| 714 | *fsInOutColor = outColor; |
| 715 | } |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 716 | } |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 717 | |
| 718 | const SkString* GrGLShaderBuilder::getEffectAttributeName(int attributeIndex) const { |
| 719 | const AttributePair* attribEnd = this->getEffectAttributes().end(); |
skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 720 | for (const AttributePair* attrib = this->getEffectAttributes().begin(); |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 721 | attrib != attribEnd; |
| 722 | ++attrib) { |
| 723 | if (attrib->fIndex == attributeIndex) { |
| 724 | return &attrib->fName; |
| 725 | } |
skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 726 | } |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 727 | |
| 728 | return NULL; |
| 729 | } |