1 /* 2 * Copyright 2011 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 "GrGLSL.h" 9 #include "GrGLShaderVar.h" 10 #include "SkString.h" 11 12 GrGLSLGeneration GrGetGLSLGeneration(GrGLBinding binding, const GrGLInterface* gl) { 13 GrGLSLVersion ver = GrGLGetGLSLVersion(gl); 14 switch (binding) { 15 case kDesktop_GrGLBinding: 16 GrAssert(ver >= GR_GLSL_VER(1,10)); 17 if (ver >= GR_GLSL_VER(1,50)) { 18 return k150_GrGLSLGeneration; 19 } else if (ver >= GR_GLSL_VER(1,40)) { 20 return k140_GrGLSLGeneration; 21 } else if (ver >= GR_GLSL_VER(1,30)) { 22 return k130_GrGLSLGeneration; 23 } else { 24 return k110_GrGLSLGeneration; 25 } 26 case kES2_GrGLBinding: 27 // version 1.00 of ES GLSL based on ver 1.20 of desktop GLSL 28 GrAssert(ver >= GR_GL_VER(1,00)); 29 return k110_GrGLSLGeneration; 30 default: 31 GrCrash("Unknown GL Binding"); 32 return k110_GrGLSLGeneration; // suppress warning 33 } 34 } 35 36 const char* GrGetGLSLVersionDecl(GrGLBinding binding, GrGLSLGeneration gen) { 37 switch (gen) { 38 case k110_GrGLSLGeneration: 39 if (kES2_GrGLBinding == binding) { 40 // ES2s shader language is based on version 1.20 but is version 41 // 1.00 of the ES language. 42 return "#version 100\n"; 43 } else { 44 GrAssert(kDesktop_GrGLBinding == binding); 45 return "#version 110\n"; 46 } 47 case k130_GrGLSLGeneration: 48 GrAssert(kDesktop_GrGLBinding == binding); 49 return "#version 130\n"; 50 case k140_GrGLSLGeneration: 51 GrAssert(kDesktop_GrGLBinding == binding); 52 return "#version 140\n"; 53 case k150_GrGLSLGeneration: 54 GrAssert(kDesktop_GrGLBinding == binding); 55 return "#version 150\n"; 56 default: 57 GrCrash("Unknown GL version."); 58 return ""; // suppress warning 59 } 60 } 61 62 bool GrGLSLSetupFSColorOuput(GrGLSLGeneration gen, const char* nameIfDeclared, GrGLShaderVar* var) { 63 bool declaredOutput = k110_GrGLSLGeneration != gen; 64 var->set(kVec4f_GrSLType, 65 GrGLShaderVar::kOut_TypeModifier, 66 declaredOutput ? nameIfDeclared : "gl_FragColor"); 67 return declaredOutput; 68 } 69 70 const char* GrGLSLVectorHomogCoord(int count) { 71 static const char* HOMOGS[] = {"ERROR", "", ".y", ".z", ".w"}; 72 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(HOMOGS)); 73 return HOMOGS[count]; 74 } 75 76 const char* GrGLSLVectorHomogCoord(GrSLType type) { 77 return GrGLSLVectorHomogCoord(GrSLTypeToVecLength(type)); 78 } 79 80 const char* GrGLSLVectorNonhomogCoords(int count) { 81 static const char* NONHOMOGS[] = {"ERROR", "", ".x", ".xy", ".xyz"}; 82 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(NONHOMOGS)); 83 return NONHOMOGS[count]; 84 } 85 86 const char* GrGLSLVectorNonhomogCoords(GrSLType type) { 87 return GrGLSLVectorNonhomogCoords(GrSLTypeToVecLength(type)); 88 } 89 90 namespace { 91 void append_tabs(SkString* outAppend, int tabCnt) { 92 static const char kTabs[] = "\t\t\t\t\t\t\t\t"; 93 while (tabCnt) { 94 int cnt = GrMin((int)GR_ARRAY_COUNT(kTabs), tabCnt); 95 outAppend->append(kTabs, cnt); 96 tabCnt -= cnt; 97 } 98 } 99 } 100 101 GrSLConstantVec GrGLSLMulVarBy4f(SkString* outAppend, 102 int tabCnt, 103 const char* vec4VarName, 104 const char* mulFactor, 105 GrSLConstantVec mulFactorDefault) { 106 bool haveFactor = NULL != mulFactor && '\0' != *mulFactor; 107 108 GrAssert(NULL != outAppend); 109 GrAssert(NULL != vec4VarName); 110 GrAssert(kNone_GrSLConstantVec != mulFactorDefault || haveFactor); 111 112 if (!haveFactor) { 113 if (kOnes_GrSLConstantVec == mulFactorDefault) { 114 return kNone_GrSLConstantVec; 115 } else { 116 GrAssert(kZeros_GrSLConstantVec == mulFactorDefault); 117 append_tabs(outAppend, tabCnt); 118 outAppend->appendf("%s = vec4(0, 0, 0, 0);\n", vec4VarName); 119 return kZeros_GrSLConstantVec; 120 } 121 } 122 append_tabs(outAppend, tabCnt); 123 outAppend->appendf("%s *= %s;\n", vec4VarName, mulFactor); 124 return kNone_GrSLConstantVec; 125 } 126 127 GrSLConstantVec GrGLSLGetComponent4f(SkString* outAppend, 128 const char* expr, 129 GrColorComponentFlags component, 130 GrSLConstantVec defaultExpr, 131 bool omitIfConst) { 132 if (NULL == expr || '\0' == *expr) { 133 GrAssert(defaultExpr != kNone_GrSLConstantVec); 134 if (!omitIfConst) { 135 if (kOnes_GrSLConstantVec == defaultExpr) { 136 outAppend->append("1.0"); 137 } else { 138 GrAssert(kZeros_GrSLConstantVec == defaultExpr); 139 outAppend->append("0.0"); 140 } 141 } 142 return defaultExpr; 143 } else { 144 outAppend->appendf("(%s).%c", expr, GrColorComponentFlagToChar(component)); 145 return kNone_GrSLConstantVec; 146 } 147 } 148