joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 "GrGLShaderBuilder.h" |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 9 | #include "GrGLProgramBuilder.h" |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame^] | 10 | #include "GrGLShaderStringBuilder.h" |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 11 | #include "../GrGLGpu.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 12 | #include "../GrGLShaderVar.h" |
| 13 | |
| 14 | namespace { |
| 15 | inline const char* sample_function_name(GrSLType type, GrGLSLGeneration glslGen) { |
| 16 | if (kVec2f_GrSLType == type) { |
| 17 | return glslGen >= k130_GrGLSLGeneration ? "texture" : "texture2D"; |
| 18 | } else { |
| 19 | SkASSERT(kVec3f_GrSLType == type); |
| 20 | return glslGen >= k130_GrGLSLGeneration ? "textureProj" : "texture2DProj"; |
| 21 | } |
| 22 | } |
| 23 | void append_texture_lookup(SkString* out, |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 24 | GrGLGpu* gpu, |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 25 | const char* samplerName, |
| 26 | const char* coordName, |
| 27 | uint32_t configComponentMask, |
| 28 | const char* swizzle, |
| 29 | GrSLType varyingType = kVec2f_GrSLType) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 30 | SkASSERT(coordName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 31 | |
| 32 | out->appendf("%s(%s, %s)", |
| 33 | sample_function_name(varyingType, gpu->glslGeneration()), |
| 34 | samplerName, |
| 35 | coordName); |
| 36 | |
| 37 | char mangledSwizzle[5]; |
| 38 | |
| 39 | // The swizzling occurs using texture params instead of shader-mangling if ARB_texture_swizzle |
| 40 | // is available. |
| 41 | if (!gpu->glCaps().textureSwizzleSupport() && |
| 42 | (kA_GrColorComponentFlag == configComponentMask)) { |
| 43 | char alphaChar = gpu->glCaps().textureRedSupport() ? 'r' : 'a'; |
| 44 | int i; |
| 45 | for (i = 0; '\0' != swizzle[i]; ++i) { |
| 46 | mangledSwizzle[i] = alphaChar; |
| 47 | } |
| 48 | mangledSwizzle[i] ='\0'; |
| 49 | swizzle = mangledSwizzle; |
| 50 | } |
| 51 | // For shader prettiness we omit the swizzle rather than appending ".rgba". |
| 52 | if (memcmp(swizzle, "rgba", 4)) { |
| 53 | out->appendf(".%s", swizzle); |
| 54 | } |
| 55 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | GrGLShaderBuilder::GrGLShaderBuilder(GrGLProgramBuilder* program) |
| 59 | : fProgramBuilder(program) |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 60 | , fInputs(GrGLProgramBuilder::kVarsPerBlock) |
| 61 | , fOutputs(GrGLProgramBuilder::kVarsPerBlock) |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame^] | 62 | , fFeaturesAddedMask(0) |
| 63 | , fCodeIndex(kCode) |
| 64 | , fFinalized(false) { |
| 65 | // We push back some dummy pointers which will later become our header |
| 66 | for (int i = 0; i <= kCode; i++) { |
| 67 | fShaderStrings.push_back(); |
| 68 | fCompilerStrings.push_back(NULL); |
| 69 | fCompilerStringLengths.push_back(0); |
| 70 | } |
| 71 | |
| 72 | this->main() = "void main() {"; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 73 | } |
| 74 | |
egdaniel | b2f94d1 | 2014-08-29 10:08:36 -0700 | [diff] [blame] | 75 | void GrGLShaderBuilder::declAppend(const GrGLShaderVar& var) { |
| 76 | SkString tempDecl; |
| 77 | var.appendDecl(fProgramBuilder->ctxInfo(), &tempDecl); |
| 78 | this->codeAppendf("%s;", tempDecl.c_str()); |
| 79 | } |
| 80 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 81 | void GrGLShaderBuilder::emitFunction(GrSLType returnType, |
| 82 | const char* name, |
| 83 | int argCnt, |
| 84 | const GrGLShaderVar* args, |
| 85 | const char* body, |
| 86 | SkString* outName) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame^] | 87 | this->functions().append(GrGLSLTypeString(returnType)); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 88 | fProgramBuilder->nameVariable(outName, '\0', name); |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame^] | 89 | this->functions().appendf(" %s", outName->c_str()); |
| 90 | this->functions().append("("); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 91 | const GrGLContextInfo& ctxInfo = fProgramBuilder->gpu()->ctxInfo(); |
| 92 | for (int i = 0; i < argCnt; ++i) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame^] | 93 | args[i].appendDecl(ctxInfo, &this->functions()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 94 | if (i < argCnt - 1) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame^] | 95 | this->functions().append(", "); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame^] | 98 | this->functions().append(") {\n"); |
| 99 | this->functions().append(body); |
| 100 | this->functions().append("}\n\n"); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void GrGLShaderBuilder::appendTextureLookup(SkString* out, |
| 104 | const TextureSampler& sampler, |
| 105 | const char* coordName, |
| 106 | GrSLType varyingType) const { |
| 107 | append_texture_lookup(out, |
| 108 | fProgramBuilder->gpu(), |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 109 | fProgramBuilder->getUniformCStr(sampler.fSamplerUniform), |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 110 | coordName, |
| 111 | sampler.configComponentMask(), |
| 112 | sampler.swizzle(), |
| 113 | varyingType); |
| 114 | } |
| 115 | |
| 116 | void GrGLShaderBuilder::appendTextureLookup(const TextureSampler& sampler, |
| 117 | const char* coordName, |
| 118 | GrSLType varyingType) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame^] | 119 | this->appendTextureLookup(&this->code(), sampler, coordName, varyingType); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void GrGLShaderBuilder::appendTextureLookupAndModulate(const char* modulation, |
| 123 | const TextureSampler& sampler, |
| 124 | const char* coordName, |
| 125 | GrSLType varyingType) { |
| 126 | SkString lookup; |
| 127 | this->appendTextureLookup(&lookup, sampler, coordName, varyingType); |
| 128 | this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str()); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | const GrGLenum* GrGLShaderBuilder::GetTexParamSwizzle(GrPixelConfig config, const GrGLCaps& caps) { |
| 133 | if (caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(config)) { |
| 134 | if (caps.textureRedSupport()) { |
| 135 | static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED, GR_GL_RED, GR_GL_RED }; |
| 136 | return gRedSmear; |
| 137 | } else { |
| 138 | static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA, |
| 139 | GR_GL_ALPHA, GR_GL_ALPHA }; |
| 140 | return gAlphaSmear; |
| 141 | } |
| 142 | } else { |
| 143 | static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN, GR_GL_BLUE, GR_GL_ALPHA }; |
| 144 | return gStraight; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void GrGLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionName) { |
| 149 | if (!(featureBit & fFeaturesAddedMask)) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame^] | 150 | this->extensions().appendf("#extension %s: require\n", extensionName); |
| 151 | fFeaturesAddedMask |= featureBit; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 155 | void GrGLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const { |
| 156 | for (int i = 0; i < vars.count(); ++i) { |
| 157 | vars[i].appendDecl(fProgramBuilder->ctxInfo(), out); |
| 158 | out->append(";\n"); |
| 159 | } |
| 160 | } |
| 161 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 162 | void GrGLShaderBuilder::appendTextureLookup(const char* samplerName, |
| 163 | const char* coordName, |
| 164 | uint32_t configComponentMask, |
| 165 | const char* swizzle) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame^] | 166 | append_texture_lookup(&this->code(), |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 167 | fProgramBuilder->gpu(), |
| 168 | samplerName, |
| 169 | coordName, |
| 170 | configComponentMask, |
| 171 | swizzle, |
| 172 | kVec2f_GrSLType); |
| 173 | } |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame^] | 174 | |
| 175 | bool |
| 176 | GrGLShaderBuilder::finalize(GrGLuint programId, GrGLenum type, SkTDArray<GrGLuint>* shaderIds) { |
| 177 | SkASSERT(!fFinalized); |
| 178 | // append the 'footer' to code |
| 179 | this->code().append("}"); |
| 180 | |
| 181 | for (int i = 0; i <= fCodeIndex; i++) { |
| 182 | fCompilerStrings[i] = fShaderStrings[i].c_str(); |
| 183 | fCompilerStringLengths[i] = (int)fShaderStrings[i].size(); |
| 184 | } |
| 185 | |
| 186 | GrGLGpu* gpu = fProgramBuilder->gpu(); |
| 187 | GrGLuint shaderId = GrGLCompileAndAttachShader(gpu->glContext(), |
| 188 | programId, |
| 189 | type, |
| 190 | fCompilerStrings.begin(), |
| 191 | fCompilerStringLengths.begin(), |
| 192 | fCompilerStrings.count(), |
| 193 | gpu->stats()); |
| 194 | |
| 195 | fFinalized = true; |
| 196 | |
| 197 | if (!shaderId) { |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | *shaderIds->append() = shaderId; |
| 202 | |
| 203 | return true; |
| 204 | } |