OLD | NEW |
(Empty) | |
| 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" |
| 9 #include "GrGLProgramBuilder.h" |
| 10 #include "../GrGpuGL.h" |
| 11 #include "../GrGLShaderVar.h" |
| 12 |
| 13 namespace { |
| 14 inline const char* sample_function_name(GrSLType type, GrGLSLGeneration glslGen)
{ |
| 15 if (kVec2f_GrSLType == type) { |
| 16 return glslGen >= k130_GrGLSLGeneration ? "texture" : "texture2D"; |
| 17 } else { |
| 18 SkASSERT(kVec3f_GrSLType == type); |
| 19 return glslGen >= k130_GrGLSLGeneration ? "textureProj" : "texture2DProj
"; |
| 20 } |
| 21 } |
| 22 void append_texture_lookup(SkString* out, |
| 23 GrGpuGL* gpu, |
| 24 const char* samplerName, |
| 25 const char* coordName, |
| 26 uint32_t configComponentMask, |
| 27 const char* swizzle, |
| 28 GrSLType varyingType = kVec2f_GrSLType) { |
| 29 SkASSERT(NULL != coordName); |
| 30 |
| 31 out->appendf("%s(%s, %s)", |
| 32 sample_function_name(varyingType, gpu->glslGeneration()), |
| 33 samplerName, |
| 34 coordName); |
| 35 |
| 36 char mangledSwizzle[5]; |
| 37 |
| 38 // The swizzling occurs using texture params instead of shader-mangling if A
RB_texture_swizzle |
| 39 // is available. |
| 40 if (!gpu->glCaps().textureSwizzleSupport() && |
| 41 (kA_GrColorComponentFlag == configComponentMask)) { |
| 42 char alphaChar = gpu->glCaps().textureRedSupport() ? 'r' : 'a'; |
| 43 int i; |
| 44 for (i = 0; '\0' != swizzle[i]; ++i) { |
| 45 mangledSwizzle[i] = alphaChar; |
| 46 } |
| 47 mangledSwizzle[i] ='\0'; |
| 48 swizzle = mangledSwizzle; |
| 49 } |
| 50 // For shader prettiness we omit the swizzle rather than appending ".rgba". |
| 51 if (memcmp(swizzle, "rgba", 4)) { |
| 52 out->appendf(".%s", swizzle); |
| 53 } |
| 54 } |
| 55 static const int kVarsPerBlock = 8; |
| 56 } |
| 57 |
| 58 GrGLShaderBuilder::GrGLShaderBuilder(GrGLProgramBuilder* program) |
| 59 : fProgramBuilder(program) |
| 60 , fInputs(kVarsPerBlock) |
| 61 , fOutputs(kVarsPerBlock) |
| 62 , fFeaturesAddedMask(0) { |
| 63 } |
| 64 |
| 65 void GrGLShaderBuilder::emitFunction(GrSLType returnType, |
| 66 const char* name, |
| 67 int argCnt, |
| 68 const GrGLShaderVar* args, |
| 69 const char* body, |
| 70 SkString* outName) { |
| 71 fFunctions.append(GrGLSLTypeString(returnType)); |
| 72 fProgramBuilder->nameVariable(outName, '\0', name); |
| 73 fFunctions.appendf(" %s", outName->c_str()); |
| 74 fFunctions.append("("); |
| 75 const GrGLContextInfo& ctxInfo = fProgramBuilder->gpu()->ctxInfo(); |
| 76 for (int i = 0; i < argCnt; ++i) { |
| 77 args[i].appendDecl(ctxInfo, &fFunctions); |
| 78 if (i < argCnt - 1) { |
| 79 fFunctions.append(", "); |
| 80 } |
| 81 } |
| 82 fFunctions.append(") {\n"); |
| 83 fFunctions.append(body); |
| 84 fFunctions.append("}\n\n"); |
| 85 } |
| 86 |
| 87 void GrGLShaderBuilder::appendTextureLookup(SkString* out, |
| 88 const TextureSampler& sampler, |
| 89 const char* coordName, |
| 90 GrSLType varyingType) const { |
| 91 append_texture_lookup(out, |
| 92 fProgramBuilder->gpu(), |
| 93 fProgramBuilder->getUniformCStr(sampler.samplerUniform
()), |
| 94 coordName, |
| 95 sampler.configComponentMask(), |
| 96 sampler.swizzle(), |
| 97 varyingType); |
| 98 } |
| 99 |
| 100 void GrGLShaderBuilder::appendTextureLookup(const TextureSampler& sampler, |
| 101 const char* coordName, |
| 102 GrSLType varyingType) { |
| 103 this->appendTextureLookup(&fCode, sampler, coordName, varyingType); |
| 104 } |
| 105 |
| 106 void GrGLShaderBuilder::appendTextureLookupAndModulate(const char* modulation, |
| 107 const TextureSampler& sam
pler, |
| 108 const char* coordName, |
| 109 GrSLType varyingType) { |
| 110 SkString lookup; |
| 111 this->appendTextureLookup(&lookup, sampler, coordName, varyingType); |
| 112 this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str()); |
| 113 } |
| 114 |
| 115 |
| 116 const GrGLenum* GrGLShaderBuilder::GetTexParamSwizzle(GrPixelConfig config, cons
t GrGLCaps& caps) { |
| 117 if (caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(config)) { |
| 118 if (caps.textureRedSupport()) { |
| 119 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED, GR_GL_RE
D, GR_GL_RED }; |
| 120 return gRedSmear; |
| 121 } else { |
| 122 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA, |
| 123 GR_GL_ALPHA, GR_GL_ALPHA }; |
| 124 return gAlphaSmear; |
| 125 } |
| 126 } else { |
| 127 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN, GR_GL_BLUE
, GR_GL_ALPHA }; |
| 128 return gStraight; |
| 129 } |
| 130 } |
| 131 |
| 132 void GrGLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionNam
e) { |
| 133 if (!(featureBit & fFeaturesAddedMask)) { |
| 134 fExtensions.appendf("#extension %s: require\n", extensionName); |
| 135 fFeaturesAddedMask |= featureBit; |
| 136 } |
| 137 } |
| 138 |
| 139 void GrGLShaderBuilder::appendTextureLookup(const char* samplerName, |
| 140 const char* coordName, |
| 141 uint32_t configComponentMask, |
| 142 const char* swizzle) { |
| 143 append_texture_lookup(&fCode, |
| 144 fProgramBuilder->gpu(), |
| 145 samplerName, |
| 146 coordName, |
| 147 configComponentMask, |
| 148 swizzle, |
| 149 kVec2f_GrSLType); |
| 150 } |
| 151 |
| 152 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 153 GrGLFullShaderBuilder::GrGLFullShaderBuilder(GrGLFullProgramBuilder* program) |
| 154 : INHERITED(program) |
| 155 , fFullProgramBuilder(program) {} |
OLD | NEW |