michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "GrGLSL.h" michael@0: #include "GrGLShaderVar.h" michael@0: #include "SkString.h" michael@0: michael@0: GrGLSLGeneration GrGetGLSLGeneration(const GrGLInterface* gl) { michael@0: GrGLSLVersion ver = GrGLGetGLSLVersion(gl); michael@0: switch (gl->fStandard) { michael@0: case kGL_GrGLStandard: michael@0: SkASSERT(ver >= GR_GLSL_VER(1,10)); michael@0: if (ver >= GR_GLSL_VER(1,50)) { michael@0: return k150_GrGLSLGeneration; michael@0: } else if (ver >= GR_GLSL_VER(1,40)) { michael@0: return k140_GrGLSLGeneration; michael@0: } else if (ver >= GR_GLSL_VER(1,30)) { michael@0: return k130_GrGLSLGeneration; michael@0: } else { michael@0: return k110_GrGLSLGeneration; michael@0: } michael@0: case kGLES_GrGLStandard: michael@0: // version 1.00 of ES GLSL based on ver 1.20 of desktop GLSL michael@0: SkASSERT(ver >= GR_GL_VER(1,00)); michael@0: return k110_GrGLSLGeneration; michael@0: default: michael@0: GrCrash("Unknown GL Standard"); michael@0: return k110_GrGLSLGeneration; // suppress warning michael@0: } michael@0: } michael@0: michael@0: const char* GrGetGLSLVersionDecl(const GrGLContextInfo& info) { michael@0: switch (info.glslGeneration()) { michael@0: case k110_GrGLSLGeneration: michael@0: if (kGLES_GrGLStandard == info.standard()) { michael@0: // ES2s shader language is based on version 1.20 but is version michael@0: // 1.00 of the ES language. michael@0: return "#version 100\n"; michael@0: } else { michael@0: SkASSERT(kGL_GrGLStandard == info.standard()); michael@0: return "#version 110\n"; michael@0: } michael@0: case k130_GrGLSLGeneration: michael@0: SkASSERT(kGL_GrGLStandard == info.standard()); michael@0: return "#version 130\n"; michael@0: case k140_GrGLSLGeneration: michael@0: SkASSERT(kGL_GrGLStandard == info.standard()); michael@0: return "#version 140\n"; michael@0: case k150_GrGLSLGeneration: michael@0: SkASSERT(kGL_GrGLStandard == info.standard()); michael@0: if (info.caps()->isCoreProfile()) { michael@0: return "#version 150\n"; michael@0: } else { michael@0: return "#version 150 compatibility\n"; michael@0: } michael@0: default: michael@0: GrCrash("Unknown GL version."); michael@0: return ""; // suppress warning michael@0: } michael@0: } michael@0: michael@0: namespace { michael@0: void append_tabs(SkString* outAppend, int tabCnt) { michael@0: static const char kTabs[] = "\t\t\t\t\t\t\t\t"; michael@0: while (tabCnt) { michael@0: int cnt = GrMin((int)GR_ARRAY_COUNT(kTabs), tabCnt); michael@0: outAppend->append(kTabs, cnt); michael@0: tabCnt -= cnt; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void GrGLSLMulVarBy4f(SkString* outAppend, michael@0: unsigned tabCnt, michael@0: const char* vec4VarName, michael@0: const GrGLSLExpr4& mulFactor) { michael@0: if (mulFactor.isOnes()) { michael@0: *outAppend = SkString(); michael@0: } michael@0: michael@0: append_tabs(outAppend, tabCnt); michael@0: michael@0: if (mulFactor.isZeros()) { michael@0: outAppend->appendf("%s = vec4(0);\n", vec4VarName); michael@0: } else { michael@0: outAppend->appendf("%s *= %s;\n", vec4VarName, mulFactor.c_str()); michael@0: } michael@0: }