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 | 4fe9b1b | 2012-10-22 17:40:14 +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) { |
| 102 | // FIXME: We don't know how the custom stage will manipulate the coords. So we give up on using |
| 103 | // projective texturing and always give the stage 2D coords. This will be fixed when custom |
bsalomon@google.com | 4fe9b1b | 2012-10-22 17:40:14 +0000 | [diff] [blame^] | 104 | // stages 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 | |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 156 | GrCustomStage::StageKey GrGLShaderBuilder::KeyForTextureAccess(const GrTextureAccess& access, |
| 157 | const GrGLCaps& caps) { |
| 158 | GrCustomStage::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 | 4fe9b1b | 2012-10-22 17:40:14 +0000 | [diff] [blame^] | 288 | const char* GrGLShaderBuilder::fragmentPosition() { |
| 289 | if (fContext.caps().fragCoordConventionsSupport()) { |
| 290 | if (!fSetupFragPosition) { |
| 291 | this->fFSHeader.append("layout(origin_upper_left) in vec4 gl_FragCoord;\n"); |
| 292 | fSetupFragPosition = true; |
| 293 | } |
| 294 | return "gl_FragCoord"; |
| 295 | } else { |
| 296 | static const char* kCoordName = "fragCoordYDown"; |
| 297 | if (!fSetupFragPosition) { |
| 298 | GrAssert(GrGLUniformManager::kInvalidUniformHandle == fRTHeightUniform); |
| 299 | const char* rtHeightName; |
| 300 | |
| 301 | // temporarily change the stage index because we're inserting a uniform whose name |
| 302 | // shouldn't be mangled to be stage-specific. |
| 303 | int oldStageIdx = fCurrentStage; |
| 304 | fCurrentStage = kNonStageIdx; |
| 305 | fRTHeightUniform = this->addUniform(kFragment_ShaderType, |
| 306 | kFloat_GrSLType, |
| 307 | "RTHeight", |
| 308 | &rtHeightName); |
| 309 | fCurrentStage = oldStageIdx; |
| 310 | |
| 311 | this->fFSCode.prependf("\tvec4 %s = vec4(gl_FragCoord.x, %s - gl_FragCoord.y, gl_FragCoord.zw);\n", |
| 312 | kCoordName, rtHeightName); |
| 313 | fSetupFragPosition = true; |
| 314 | } |
| 315 | GrAssert(GrGLUniformManager::kInvalidUniformHandle != fRTHeightUniform); |
| 316 | return kCoordName; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 321 | void GrGLShaderBuilder::emitFunction(ShaderType shader, |
| 322 | GrSLType returnType, |
| 323 | const char* name, |
| 324 | int argCnt, |
| 325 | const GrGLShaderVar* args, |
| 326 | const char* body, |
| 327 | SkString* outName) { |
| 328 | GrAssert(kFragment_ShaderType == shader); |
| 329 | fFSFunctions.append(GrGLShaderVar::TypeString(returnType)); |
| 330 | if (kNonStageIdx != fCurrentStage) { |
| 331 | outName->printf(" %s_%d", name, fCurrentStage); |
| 332 | } else { |
| 333 | *outName = name; |
| 334 | } |
| 335 | fFSFunctions.append(*outName); |
| 336 | fFSFunctions.append("("); |
| 337 | for (int i = 0; i < argCnt; ++i) { |
| 338 | args[i].appendDecl(fContext, &fFSFunctions); |
| 339 | if (i < argCnt - 1) { |
| 340 | fFSFunctions.append(", "); |
| 341 | } |
| 342 | } |
| 343 | fFSFunctions.append(") {\n"); |
| 344 | fFSFunctions.append(body); |
| 345 | fFSFunctions.append("}\n\n"); |
| 346 | } |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 347 | |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 348 | namespace { |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 349 | |
| 350 | inline void append_default_precision_qualifier(GrGLShaderVar::Precision p, |
| 351 | GrGLBinding binding, |
| 352 | SkString* str) { |
| 353 | // Desktop GLSL has added precision qualifiers but they don't do anything. |
| 354 | if (kES2_GrGLBinding == binding) { |
| 355 | switch (p) { |
| 356 | case GrGLShaderVar::kHigh_Precision: |
| 357 | str->append("precision highp float;\n"); |
| 358 | break; |
| 359 | case GrGLShaderVar::kMedium_Precision: |
| 360 | str->append("precision mediump float;\n"); |
| 361 | break; |
| 362 | case GrGLShaderVar::kLow_Precision: |
| 363 | str->append("precision lowp float;\n"); |
| 364 | break; |
| 365 | case GrGLShaderVar::kDefault_Precision: |
| 366 | GrCrash("Default precision now allowed."); |
| 367 | default: |
| 368 | GrCrash("Unknown precision value."); |
| 369 | } |
| 370 | } |
| 371 | } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 372 | } |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 373 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 374 | void GrGLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 375 | for (int i = 0; i < vars.count(); ++i) { |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 376 | vars[i].appendDecl(fContext, out); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 377 | out->append(";\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 378 | } |
| 379 | } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 380 | |
| 381 | void GrGLShaderBuilder::appendUniformDecls(ShaderType stype, SkString* out) const { |
| 382 | for (int i = 0; i < fUniforms.count(); ++i) { |
| 383 | if (fUniforms[i].fVisibility & stype) { |
| 384 | fUniforms[i].fVariable.appendDecl(fContext, out); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 385 | out->append(";\n"); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 386 | } |
| 387 | } |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | void GrGLShaderBuilder::getShader(ShaderType type, SkString* shaderStr) const { |
| 391 | switch (type) { |
| 392 | case kVertex_ShaderType: |
| 393 | *shaderStr = fHeader; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 394 | this->appendUniformDecls(kVertex_ShaderType, shaderStr); |
| 395 | this->appendDecls(fVSAttrs, shaderStr); |
| 396 | this->appendDecls(fVSOutputs, shaderStr); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 397 | shaderStr->append("void main() {\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 398 | shaderStr->append(fVSCode); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 399 | shaderStr->append("}\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 400 | break; |
| 401 | case kGeometry_ShaderType: |
| 402 | if (fUsesGS) { |
| 403 | *shaderStr = fHeader; |
| 404 | shaderStr->append(fGSHeader); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 405 | this->appendDecls(fGSInputs, shaderStr); |
| 406 | this->appendDecls(fGSOutputs, shaderStr); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 407 | shaderStr->append("void main() {\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 408 | shaderStr->append(fGSCode); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 409 | shaderStr->append("}\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 410 | } else { |
| 411 | shaderStr->reset(); |
| 412 | } |
| 413 | break; |
| 414 | case kFragment_ShaderType: |
| 415 | *shaderStr = fHeader; |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 416 | append_default_precision_qualifier(kDefaultFragmentPrecision, |
| 417 | fContext.binding(), |
| 418 | shaderStr); |
bsalomon@google.com | 4fe9b1b | 2012-10-22 17:40:14 +0000 | [diff] [blame^] | 419 | shaderStr->append(fFSHeader); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 420 | this->appendUniformDecls(kFragment_ShaderType, shaderStr); |
| 421 | this->appendDecls(fFSInputs, shaderStr); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 422 | // We shouldn't have declared outputs on 1.10 |
| 423 | GrAssert(k110_GrGLSLGeneration != fContext.glslGeneration() || fFSOutputs.empty()); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 424 | this->appendDecls(fFSOutputs, shaderStr); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 425 | shaderStr->append(fFSFunctions); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 426 | shaderStr->append("void main() {\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 427 | shaderStr->append(fFSCode); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 428 | shaderStr->append("}\n"); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 429 | break; |
| 430 | } |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 431 | } |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 432 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 433 | void GrGLShaderBuilder::finished(GrGLuint programID) { |
| 434 | fUniformManager.getUniformLocations(programID, fUniforms); |
| 435 | } |