tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 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" |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 9 | #include "GrGLShaderVar.h" |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 10 | #include "SkString.h" |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 11 | |
commit-bot@chromium.org | f4e67e3 | 2014-04-30 01:26:04 +0000 | [diff] [blame] | 12 | bool GrGetGLSLGeneration(const GrGLInterface* gl, GrGLSLGeneration* generation) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 13 | SkASSERT(generation); |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 14 | GrGLSLVersion ver = GrGLGetGLSLVersion(gl); |
commit-bot@chromium.org | f4e67e3 | 2014-04-30 01:26:04 +0000 | [diff] [blame] | 15 | if (GR_GLSL_INVALID_VER == ver) { |
| 16 | return false; |
| 17 | } |
commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 18 | switch (gl->fStandard) { |
| 19 | case kGL_GrGLStandard: |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 20 | SkASSERT(ver >= GR_GLSL_VER(1,10)); |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 21 | if (ver >= GR_GLSL_VER(1,50)) { |
commit-bot@chromium.org | f4e67e3 | 2014-04-30 01:26:04 +0000 | [diff] [blame] | 22 | *generation = k150_GrGLSLGeneration; |
bsalomon@google.com | 281c726 | 2012-10-23 14:31:30 +0000 | [diff] [blame] | 23 | } else if (ver >= GR_GLSL_VER(1,40)) { |
commit-bot@chromium.org | f4e67e3 | 2014-04-30 01:26:04 +0000 | [diff] [blame] | 24 | *generation = k140_GrGLSLGeneration; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 25 | } else if (ver >= GR_GLSL_VER(1,30)) { |
commit-bot@chromium.org | f4e67e3 | 2014-04-30 01:26:04 +0000 | [diff] [blame] | 26 | *generation = k130_GrGLSLGeneration; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 27 | } else { |
commit-bot@chromium.org | f4e67e3 | 2014-04-30 01:26:04 +0000 | [diff] [blame] | 28 | *generation = k110_GrGLSLGeneration; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 29 | } |
commit-bot@chromium.org | f4e67e3 | 2014-04-30 01:26:04 +0000 | [diff] [blame] | 30 | return true; |
commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 31 | case kGLES_GrGLStandard: |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 32 | // version 1.00 of ES GLSL based on ver 1.20 of desktop GLSL |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 33 | SkASSERT(ver >= GR_GL_VER(1,00)); |
commit-bot@chromium.org | f4e67e3 | 2014-04-30 01:26:04 +0000 | [diff] [blame] | 34 | *generation = k110_GrGLSLGeneration; |
| 35 | return true; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 36 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 37 | SkFAIL("Unknown GL Standard"); |
commit-bot@chromium.org | f4e67e3 | 2014-04-30 01:26:04 +0000 | [diff] [blame] | 38 | return false; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
commit-bot@chromium.org | 06f0598 | 2013-08-30 15:52:36 +0000 | [diff] [blame] | 42 | const char* GrGetGLSLVersionDecl(const GrGLContextInfo& info) { |
| 43 | switch (info.glslGeneration()) { |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 44 | case k110_GrGLSLGeneration: |
commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 45 | if (kGLES_GrGLStandard == info.standard()) { |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 46 | // ES2s shader language is based on version 1.20 but is version |
| 47 | // 1.00 of the ES language. |
| 48 | return "#version 100\n"; |
| 49 | } else { |
commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 50 | SkASSERT(kGL_GrGLStandard == info.standard()); |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 51 | return "#version 110\n"; |
| 52 | } |
| 53 | case k130_GrGLSLGeneration: |
commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 54 | SkASSERT(kGL_GrGLStandard == info.standard()); |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 55 | return "#version 130\n"; |
bsalomon@google.com | 281c726 | 2012-10-23 14:31:30 +0000 | [diff] [blame] | 56 | case k140_GrGLSLGeneration: |
commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 57 | SkASSERT(kGL_GrGLStandard == info.standard()); |
bsalomon@google.com | 281c726 | 2012-10-23 14:31:30 +0000 | [diff] [blame] | 58 | return "#version 140\n"; |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 59 | case k150_GrGLSLGeneration: |
commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 60 | SkASSERT(kGL_GrGLStandard == info.standard()); |
commit-bot@chromium.org | 06f0598 | 2013-08-30 15:52:36 +0000 | [diff] [blame] | 61 | if (info.caps()->isCoreProfile()) { |
| 62 | return "#version 150\n"; |
| 63 | } else { |
| 64 | return "#version 150 compatibility\n"; |
| 65 | } |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 66 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 67 | SkFAIL("Unknown GL version."); |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 68 | return ""; // suppress warning |
| 69 | } |
| 70 | } |
| 71 | |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 72 | namespace { |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 73 | void append_tabs(SkString* outAppend, int tabCnt) { |
| 74 | static const char kTabs[] = "\t\t\t\t\t\t\t\t"; |
| 75 | while (tabCnt) { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 76 | int cnt = SkTMin((int)SK_ARRAY_COUNT(kTabs), tabCnt); |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 77 | outAppend->append(kTabs, cnt); |
| 78 | tabCnt -= cnt; |
| 79 | } |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 82 | |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 83 | void GrGLSLMulVarBy4f(SkString* outAppend, |
| 84 | unsigned tabCnt, |
| 85 | const char* vec4VarName, |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 86 | const GrGLSLExpr4& mulFactor) { |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 87 | if (mulFactor.isOnes()) { |
| 88 | *outAppend = SkString(); |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 89 | } |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 90 | |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 91 | append_tabs(outAppend, tabCnt); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 92 | |
| 93 | if (mulFactor.isZeros()) { |
| 94 | outAppend->appendf("%s = vec4(0);\n", vec4VarName); |
commit-bot@chromium.org | 8660783 | 2013-11-21 15:57:40 +0000 | [diff] [blame] | 95 | } else { |
| 96 | outAppend->appendf("%s *= %s;\n", vec4VarName, mulFactor.c_str()); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 97 | } |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 98 | } |