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" |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 11 | #include "GrTexture.h" |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 12 | |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 13 | // number of each input/output type in a single allocation block |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 14 | static const int kVarsPerBlock = 8; |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 15 | |
| 16 | // except FS outputs where we expect 2 at most. |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 17 | static const int kMaxFSOutputs = 2; |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 18 | |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 19 | // ES2 FS only guarantees mediump and lowp support |
| 20 | static const GrGLShaderVar::Precision kDefaultFragmentPrecision = GrGLShaderVar::kMedium_Precision; |
| 21 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 22 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
| 23 | /////////////////////////////////////////////////////////////////////////////// |
| 24 | |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 25 | namespace { |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 26 | |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 27 | inline const char* sample_function_name(GrSLType type) { |
| 28 | if (kVec2f_GrSLType == type) { |
| 29 | return "texture2D"; |
| 30 | } else { |
| 31 | GrAssert(kVec3f_GrSLType == type); |
| 32 | return "texture2DProj"; |
| 33 | } |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 34 | } |
| 35 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 36 | /** |
| 37 | * Do we need to either map r,g,b->a or a->r. |
| 38 | */ |
| 39 | inline bool swizzle_requires_alpha_remapping(const GrGLCaps& caps, |
| 40 | const GrTextureAccess& access) { |
| 41 | if (GrPixelConfigIsAlphaOnly(access.getTexture()->config())) { |
| 42 | if (caps.textureRedSupport() && (GrTextureAccess::kA_SwizzleFlag & access.swizzleMask())) { |
| 43 | return true; |
| 44 | } |
| 45 | if (GrTextureAccess::kRGB_SwizzleMask & access.swizzleMask()) { |
| 46 | return true; |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 47 | } |
| 48 | } |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 49 | return false; |
| 50 | } |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 51 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 52 | void append_swizzle(SkString* outAppend, |
| 53 | const GrTextureAccess& access, |
| 54 | const GrGLCaps& caps) { |
| 55 | const char* swizzle = access.getSwizzle(); |
| 56 | char mangledSwizzle[5]; |
| 57 | |
| 58 | // The swizzling occurs using texture params instead of shader-mangling if ARB_texture_swizzle |
| 59 | // is available. |
| 60 | if (!caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(access.getTexture()->config())) { |
| 61 | char alphaChar = caps.textureRedSupport() ? 'r' : 'a'; |
| 62 | int i; |
| 63 | for (i = 0; '\0' != swizzle[i]; ++i) { |
| 64 | mangledSwizzle[i] = alphaChar; |
| 65 | } |
| 66 | mangledSwizzle[i] ='\0'; |
| 67 | swizzle = mangledSwizzle; |
| 68 | } |
bsalomon@google.com | 73d5b2f | 2012-10-04 13:26:32 +0000 | [diff] [blame] | 69 | // For shader prettiness we omit the swizzle rather than appending ".rgba". |
| 70 | if (memcmp(swizzle, "rgba", 4)) { |
| 71 | outAppend->appendf(".%s", swizzle); |
| 72 | } |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 73 | } |
| 74 | |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 75 | } |
| 76 | |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 77 | /////////////////////////////////////////////////////////////////////////////// |
| 78 | |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 79 | // Architectural assumption: always 2-d input coords. |
| 80 | // Likely to become non-constant and non-static, perhaps even |
| 81 | // varying by stage, if we use 1D textures for gradients! |
| 82 | //const int GrGLShaderBuilder::fCoordDims = 2; |
| 83 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 84 | GrGLShaderBuilder::GrGLShaderBuilder(const GrGLContextInfo& ctx, GrGLUniformManager& uniformManager) |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 85 | : fUniforms(kVarsPerBlock) |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 86 | , fVSAttrs(kVarsPerBlock) |
| 87 | , fVSOutputs(kVarsPerBlock) |
| 88 | , fGSInputs(kVarsPerBlock) |
| 89 | , fGSOutputs(kVarsPerBlock) |
| 90 | , fFSInputs(kVarsPerBlock) |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 91 | , fFSOutputs(kMaxFSOutputs) |
tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 92 | , fUsesGS(false) |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 93 | , fContext(ctx) |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 94 | , fUniformManager(uniformManager) |
bsalomon@google.com | d9e0181 | 2012-08-29 19:35:44 +0000 | [diff] [blame] | 95 | , fCurrentStage(kNonStageIdx) |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 96 | , fSetupFragPosition(false) |
| 97 | , fRTHeightUniform(GrGLUniformManager::kInvalidUniformHandle) |
bsalomon@google.com | d9e0181 | 2012-08-29 19:35:44 +0000 | [diff] [blame] | 98 | , fTexCoordVaryingType(kVoid_GrSLType) { |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 99 | } |
| 100 | |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 101 | void GrGLShaderBuilder::setupTextureAccess(const char* varyingFSName, GrSLType varyingType) { |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 102 | // FIXME: We don't know how the effect will manipulate the coords. So we give up on using |
bsalomon@google.com | 8ea78d8 | 2012-10-24 20:11:30 +0000 | [diff] [blame] | 103 | // projective texturing and always give the stage 2D coords. This will be fixed when effects |
| 104 | // are responsible for setting up their own tex coords / tex matrices. |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 105 | switch (varyingType) { |
| 106 | case kVec2f_GrSLType: |
| 107 | fDefaultTexCoordsName = varyingFSName; |
| 108 | fTexCoordVaryingType = kVec2f_GrSLType; |
| 109 | break; |
| 110 | case kVec3f_GrSLType: { |
| 111 | fDefaultTexCoordsName = "inCoord"; |
| 112 | GrAssert(kNonStageIdx != fCurrentStage); |
| 113 | fDefaultTexCoordsName.appendS32(fCurrentStage); |
| 114 | fTexCoordVaryingType = kVec3f_GrSLType; |
| 115 | fFSCode.appendf("\t%s %s = %s.xy / %s.z;\n", |
| 116 | GrGLShaderVar::TypeString(kVec2f_GrSLType), |
| 117 | fDefaultTexCoordsName.c_str(), |
| 118 | varyingFSName, |
| 119 | varyingFSName); |
| 120 | break; |
| 121 | } |
| 122 | default: |
| 123 | GrCrash("Tex coords must either be Vec2f or Vec3f"); |
tomhudson@google.com | de78823 | 2012-08-02 20:13:12 +0000 | [diff] [blame] | 124 | } |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 125 | } |
| 126 | |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 127 | void GrGLShaderBuilder::appendTextureLookup(SkString* out, |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 128 | const GrGLShaderBuilder::TextureSampler& sampler, |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 129 | const char* coordName, |
| 130 | GrSLType varyingType) const { |
bsalomon@google.com | 2d8edaf | 2012-09-07 14:47:31 +0000 | [diff] [blame] | 131 | GrAssert(NULL != sampler.textureAccess()); |
bsalomon@google.com | 2d8edaf | 2012-09-07 14:47:31 +0000 | [diff] [blame] | 132 | |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 133 | if (NULL == coordName) { |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 134 | coordName = fDefaultTexCoordsName.c_str(); |
| 135 | varyingType = kVec2f_GrSLType; |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 136 | } |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 137 | out->appendf("%s(%s, %s)", |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 138 | sample_function_name(varyingType), |
| 139 | this->getUniformCStr(sampler.fSamplerUniform), |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 140 | coordName); |
| 141 | append_swizzle(out, *sampler.textureAccess(), fContext.caps()); |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 142 | } |
| 143 | |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 144 | void GrGLShaderBuilder::appendTextureLookupAndModulate( |
| 145 | SkString* out, |
| 146 | const char* modulation, |
| 147 | const GrGLShaderBuilder::TextureSampler& sampler, |
| 148 | const char* coordName, |
| 149 | GrSLType varyingType) const { |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 150 | GrAssert(NULL != out); |
| 151 | SkString lookup; |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 152 | this->appendTextureLookup(&lookup, sampler, coordName, varyingType); |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 153 | GrGLSLModulate4f(out, modulation, lookup.c_str()); |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 154 | } |
| 155 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 156 | GrEffect::StageKey GrGLShaderBuilder::KeyForTextureAccess(const GrTextureAccess& access, |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 157 | const GrGLCaps& caps) { |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 158 | GrEffect::StageKey key = 0; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 159 | |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 160 | // Assume that swizzle support implies that we never have to modify a shader to adjust |
| 161 | // for texture format/swizzle settings. |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 162 | if (!caps.textureSwizzleSupport() && swizzle_requires_alpha_remapping(caps, access)) { |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 163 | key = 1; |
| 164 | } |
bsalomon@google.com | 73d5b2f | 2012-10-04 13:26:32 +0000 | [diff] [blame] | 165 | #if GR_DEBUG |
| 166 | // Assert that key is set iff the swizzle will be modified. |
| 167 | SkString origString(access.getSwizzle()); |
| 168 | origString.prepend("."); |
| 169 | SkString modifiedString; |
| 170 | append_swizzle(&modifiedString, access, caps); |
| 171 | if (!modifiedString.size()) { |
| 172 | modifiedString = ".rgba"; |
| 173 | } |
| 174 | GrAssert(SkToBool(key) == (modifiedString != origString)); |
| 175 | #endif |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 176 | return key; |
| 177 | } |
| 178 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 179 | const GrGLenum* GrGLShaderBuilder::GetTexParamSwizzle(GrPixelConfig config, const GrGLCaps& caps) { |
| 180 | if (caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(config)) { |
| 181 | if (caps.textureRedSupport()) { |
| 182 | static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED, GR_GL_RED, GR_GL_RED }; |
| 183 | return gRedSmear; |
| 184 | } else { |
| 185 | static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA, |
| 186 | GR_GL_ALPHA, GR_GL_ALPHA }; |
| 187 | return gAlphaSmear; |
| 188 | } |
| 189 | } else { |
| 190 | static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN, GR_GL_BLUE, GR_GL_ALPHA }; |
| 191 | return gStraight; |
| 192 | } |
| 193 | } |
| 194 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 195 | GrGLUniformManager::UniformHandle GrGLShaderBuilder::addUniformArray(uint32_t visibility, |
| 196 | GrSLType type, |
| 197 | const char* name, |
| 198 | int count, |
| 199 | const char** outName) { |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 200 | GrAssert(name && strlen(name)); |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 201 | static const uint32_t kVisibilityMask = kVertex_ShaderType | kFragment_ShaderType; |
| 202 | GrAssert(0 == (~kVisibilityMask & visibility)); |
| 203 | GrAssert(0 != visibility); |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 204 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 205 | BuilderUniform& uni = fUniforms.push_back(); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 206 | UniformHandle h = index_to_handle(fUniforms.count() - 1); |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 207 | GR_DEBUGCODE(UniformHandle h2 =) |
| 208 | fUniformManager.appendUniform(type, count); |
| 209 | // We expect the uniform manager to initially have no uniforms and that all uniforms are added |
| 210 | // by this function. Therefore, the handles should match. |
| 211 | GrAssert(h2 == h); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 212 | uni.fVariable.setType(type); |
| 213 | uni.fVariable.setTypeModifier(GrGLShaderVar::kUniform_TypeModifier); |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 214 | SkString* uniName = uni.fVariable.accessName(); |
| 215 | if (kNonStageIdx == fCurrentStage) { |
| 216 | uniName->printf("u%s", name); |
| 217 | } else { |
| 218 | uniName->printf("u%s%d", name, fCurrentStage); |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 219 | } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 220 | uni.fVariable.setArrayCount(count); |
| 221 | uni.fVisibility = visibility; |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 222 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 223 | // If it is visible in both the VS and FS, the precision must match. |
| 224 | // We declare a default FS precision, but not a default VS. So set the var |
| 225 | // to use the default FS precision. |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 226 | if ((kVertex_ShaderType | kFragment_ShaderType) == visibility) { |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 227 | // the fragment and vertex precisions must match |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 228 | uni.fVariable.setPrecision(kDefaultFragmentPrecision); |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 229 | } |
| 230 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 231 | if (NULL != outName) { |
| 232 | *outName = uni.fVariable.c_str(); |
| 233 | } |
| 234 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 235 | return h; |
| 236 | } |
| 237 | |
| 238 | const GrGLShaderVar& GrGLShaderBuilder::getUniformVariable(UniformHandle u) const { |
| 239 | return fUniforms[handle_to_index(u)].fVariable; |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 240 | } |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 241 | |
| 242 | void GrGLShaderBuilder::addVarying(GrSLType type, |
| 243 | const char* name, |
| 244 | const char** vsOutName, |
| 245 | const char** fsInName) { |
| 246 | fVSOutputs.push_back(); |
| 247 | fVSOutputs.back().setType(type); |
| 248 | fVSOutputs.back().setTypeModifier(GrGLShaderVar::kOut_TypeModifier); |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 249 | if (kNonStageIdx == fCurrentStage) { |
| 250 | fVSOutputs.back().accessName()->printf("v%s", name); |
| 251 | } else { |
| 252 | fVSOutputs.back().accessName()->printf("v%s%d", name, fCurrentStage); |
| 253 | } |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 254 | if (vsOutName) { |
| 255 | *vsOutName = fVSOutputs.back().getName().c_str(); |
| 256 | } |
| 257 | // input to FS comes either from VS or GS |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 258 | const SkString* fsName; |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 259 | if (fUsesGS) { |
| 260 | // if we have a GS take each varying in as an array |
| 261 | // and output as non-array. |
| 262 | fGSInputs.push_back(); |
| 263 | fGSInputs.back().setType(type); |
| 264 | fGSInputs.back().setTypeModifier(GrGLShaderVar::kIn_TypeModifier); |
| 265 | fGSInputs.back().setUnsizedArray(); |
| 266 | *fGSInputs.back().accessName() = fVSOutputs.back().getName(); |
| 267 | fGSOutputs.push_back(); |
| 268 | fGSOutputs.back().setType(type); |
| 269 | fGSOutputs.back().setTypeModifier(GrGLShaderVar::kOut_TypeModifier); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 270 | if (kNonStageIdx == fCurrentStage) { |
| 271 | fGSOutputs.back().accessName()->printf("g%s", name); |
| 272 | } else { |
| 273 | fGSOutputs.back().accessName()->printf("g%s%d", name, fCurrentStage); |
| 274 | } |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 275 | fsName = fGSOutputs.back().accessName(); |
| 276 | } else { |
| 277 | fsName = fVSOutputs.back().accessName(); |
| 278 | } |
| 279 | fFSInputs.push_back(); |
| 280 | fFSInputs.back().setType(type); |
| 281 | fFSInputs.back().setTypeModifier(GrGLShaderVar::kIn_TypeModifier); |
| 282 | fFSInputs.back().setName(*fsName); |
| 283 | if (fsInName) { |
| 284 | *fsInName = fsName->c_str(); |
| 285 | } |
| 286 | } |
| 287 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 288 | const char* GrGLShaderBuilder::fragmentPosition() { |
| 289 | if (fContext.caps().fragCoordConventionsSupport()) { |
| 290 | if (!fSetupFragPosition) { |
| 291 | fFSHeader.append("#extension GL_ARB_fragment_coord_conventions: require\n"); |
bsalomon@google.com | 5fa2107 | 2012-10-25 14:57:46 +0000 | [diff] [blame^] | 292 | fFSInputs.push_back().set(kVec4f_GrSLType, |
| 293 | GrGLShaderVar::kIn_TypeModifier, |
| 294 | "gl_FragCoord", |
| 295 | GrGLShaderVar::kDefault_Precision, |
| 296 | GrGLShaderVar::kUpperLeft_Origin); |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 297 | fSetupFragPosition = true; |
| 298 | } |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 299 | return "gl_FragCoord"; |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 300 | } else { |
| 301 | static const char* kCoordName = "fragCoordYDown"; |
| 302 | if (!fSetupFragPosition) { |
| 303 | GrAssert(GrGLUniformManager::kInvalidUniformHandle == fRTHeightUniform); |
| 304 | const char* rtHeightName; |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 305 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 306 | // temporarily change the stage index because we're inserting a uniform whose name |
| 307 | // shouldn't be mangled to be stage-specific. |
| 308 | int oldStageIdx = fCurrentStage; |
| 309 | fCurrentStage = kNonStageIdx; |
| 310 | fRTHeightUniform = this->addUniform(kFragment_ShaderType, |
| 311 | kFloat_GrSLType, |
| 312 | "RTHeight", |
| 313 | &rtHeightName); |
| 314 | fCurrentStage = oldStageIdx; |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 315 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 316 | this->fFSCode.prependf("\tvec4 %s = vec4(gl_FragCoord.x, %s - gl_FragCoord.y, gl_FragCoord.zw);\n", |
| 317 | kCoordName, rtHeightName); |
| 318 | fSetupFragPosition = true; |
| 319 | } |
| 320 | GrAssert(GrGLUniformManager::kInvalidUniformHandle != fRTHeightUniform); |
| 321 | return kCoordName; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 326 | void GrGLShaderBuilder::emitFunction(ShaderType shader, |
| 327 | GrSLType returnType, |
| 328 | const char* name, |
| 329 | int argCnt, |
| 330 | const GrGLShaderVar* args, |
| 331 | const char* body, |
| 332 | SkString* outName) { |
| 333 | GrAssert(kFragment_ShaderType == shader); |
| 334 | fFSFunctions.append(GrGLShaderVar::TypeString(returnType)); |
| 335 | if (kNonStageIdx != fCurrentStage) { |
| 336 | outName->printf(" %s_%d", name, fCurrentStage); |
| 337 | } else { |
| 338 | *outName = name; |
| 339 | } |
| 340 | fFSFunctions.append(*outName); |
| 341 | fFSFunctions.append("("); |
| 342 | for (int i = 0; i < argCnt; ++i) { |
| 343 | args[i].appendDecl(fContext, &fFSFunctions); |
| 344 | if (i < argCnt - 1) { |
| 345 | fFSFunctions.append(", "); |
| 346 | } |
| 347 | } |
| 348 | fFSFunctions.append(") {\n"); |
| 349 | fFSFunctions.append(body); |
| 350 | fFSFunctions.append("}\n\n"); |
| 351 | } |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 352 | |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 353 | namespace { |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 354 | |
| 355 | inline void append_default_precision_qualifier(GrGLShaderVar::Precision p, |
| 356 | GrGLBinding binding, |
| 357 | SkString* str) { |
| 358 | // Desktop GLSL has added precision qualifiers but they don't do anything. |
| 359 | if (kES2_GrGLBinding == binding) { |
| 360 | switch (p) { |
| 361 | case GrGLShaderVar::kHigh_Precision: |
| 362 | str->append("precision highp float;\n"); |
| 363 | break; |
| 364 | case GrGLShaderVar::kMedium_Precision: |
| 365 | str->append("precision mediump float;\n"); |
| 366 | break; |
| 367 | case GrGLShaderVar::kLow_Precision: |
| 368 | str->append("precision lowp float;\n"); |
| 369 | break; |
| 370 | case GrGLShaderVar::kDefault_Precision: |
| 371 | GrCrash("Default precision now allowed."); |
| 372 | default: |
| 373 | GrCrash("Unknown precision value."); |
| 374 | } |
| 375 | } |
| 376 | } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 377 | } |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 378 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 379 | void GrGLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 380 | for (int i = 0; i < vars.count(); ++i) { |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 381 | vars[i].appendDecl(fContext, out); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 382 | out->append(";\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 383 | } |
| 384 | } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 385 | |
| 386 | void GrGLShaderBuilder::appendUniformDecls(ShaderType stype, SkString* out) const { |
| 387 | for (int i = 0; i < fUniforms.count(); ++i) { |
| 388 | if (fUniforms[i].fVisibility & stype) { |
| 389 | fUniforms[i].fVariable.appendDecl(fContext, out); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 390 | out->append(";\n"); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 391 | } |
| 392 | } |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | void GrGLShaderBuilder::getShader(ShaderType type, SkString* shaderStr) const { |
| 396 | switch (type) { |
| 397 | case kVertex_ShaderType: |
| 398 | *shaderStr = fHeader; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 399 | this->appendUniformDecls(kVertex_ShaderType, shaderStr); |
| 400 | this->appendDecls(fVSAttrs, shaderStr); |
| 401 | this->appendDecls(fVSOutputs, shaderStr); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 402 | shaderStr->append("void main() {\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 403 | shaderStr->append(fVSCode); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 404 | shaderStr->append("}\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 405 | break; |
| 406 | case kGeometry_ShaderType: |
| 407 | if (fUsesGS) { |
| 408 | *shaderStr = fHeader; |
| 409 | shaderStr->append(fGSHeader); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 410 | this->appendDecls(fGSInputs, shaderStr); |
| 411 | this->appendDecls(fGSOutputs, shaderStr); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 412 | shaderStr->append("void main() {\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 413 | shaderStr->append(fGSCode); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 414 | shaderStr->append("}\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 415 | } else { |
| 416 | shaderStr->reset(); |
| 417 | } |
| 418 | break; |
| 419 | case kFragment_ShaderType: |
| 420 | *shaderStr = fHeader; |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 421 | append_default_precision_qualifier(kDefaultFragmentPrecision, |
| 422 | fContext.binding(), |
| 423 | shaderStr); |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 424 | shaderStr->append(fFSHeader); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 425 | this->appendUniformDecls(kFragment_ShaderType, shaderStr); |
| 426 | this->appendDecls(fFSInputs, shaderStr); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 427 | // We shouldn't have declared outputs on 1.10 |
| 428 | GrAssert(k110_GrGLSLGeneration != fContext.glslGeneration() || fFSOutputs.empty()); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 429 | this->appendDecls(fFSOutputs, shaderStr); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 430 | shaderStr->append(fFSFunctions); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 431 | shaderStr->append("void main() {\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 432 | shaderStr->append(fFSCode); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 433 | shaderStr->append("}\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 434 | break; |
| 435 | } |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 436 | } |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 437 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 438 | void GrGLShaderBuilder::finished(GrGLuint programID) { |
| 439 | fUniformManager.getUniformLocations(programID, fUniforms); |
| 440 | } |