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