blob: 6c8f88360c0a49e2a071ff01a84b03b12192a9da [file] [log] [blame]
tomhudson@google.com086e5352011-12-08 14:44:10 +00001/*
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.come55fd0f2012-02-10 15:56:06 +00009#include "GrGLShaderVar.h"
bsalomon@google.com4af0af62012-08-29 12:59:57 +000010#include "SkString.h"
tomhudson@google.com086e5352011-12-08 14:44:10 +000011
commit-bot@chromium.orgf4e67e32014-04-30 01:26:04 +000012bool GrGetGLSLGeneration(const GrGLInterface* gl, GrGLSLGeneration* generation) {
bsalomon49f085d2014-09-05 13:34:00 -070013 SkASSERT(generation);
tomhudson@google.com086e5352011-12-08 14:44:10 +000014 GrGLSLVersion ver = GrGLGetGLSLVersion(gl);
commit-bot@chromium.orgf4e67e32014-04-30 01:26:04 +000015 if (GR_GLSL_INVALID_VER == ver) {
16 return false;
17 }
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +000018 switch (gl->fStandard) {
19 case kGL_GrGLStandard:
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000020 SkASSERT(ver >= GR_GLSL_VER(1,10));
tomhudson@google.com086e5352011-12-08 14:44:10 +000021 if (ver >= GR_GLSL_VER(1,50)) {
commit-bot@chromium.orgf4e67e32014-04-30 01:26:04 +000022 *generation = k150_GrGLSLGeneration;
bsalomon@google.com281c7262012-10-23 14:31:30 +000023 } else if (ver >= GR_GLSL_VER(1,40)) {
commit-bot@chromium.orgf4e67e32014-04-30 01:26:04 +000024 *generation = k140_GrGLSLGeneration;
tomhudson@google.com086e5352011-12-08 14:44:10 +000025 } else if (ver >= GR_GLSL_VER(1,30)) {
commit-bot@chromium.orgf4e67e32014-04-30 01:26:04 +000026 *generation = k130_GrGLSLGeneration;
tomhudson@google.com086e5352011-12-08 14:44:10 +000027 } else {
commit-bot@chromium.orgf4e67e32014-04-30 01:26:04 +000028 *generation = k110_GrGLSLGeneration;
tomhudson@google.com086e5352011-12-08 14:44:10 +000029 }
commit-bot@chromium.orgf4e67e32014-04-30 01:26:04 +000030 return true;
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +000031 case kGLES_GrGLStandard:
tomhudson@google.com086e5352011-12-08 14:44:10 +000032 // version 1.00 of ES GLSL based on ver 1.20 of desktop GLSL
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000033 SkASSERT(ver >= GR_GL_VER(1,00));
commit-bot@chromium.orgf4e67e32014-04-30 01:26:04 +000034 *generation = k110_GrGLSLGeneration;
35 return true;
tomhudson@google.com086e5352011-12-08 14:44:10 +000036 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +000037 SkFAIL("Unknown GL Standard");
commit-bot@chromium.orgf4e67e32014-04-30 01:26:04 +000038 return false;
tomhudson@google.com086e5352011-12-08 14:44:10 +000039 }
40}
41
commit-bot@chromium.org06f05982013-08-30 15:52:36 +000042const char* GrGetGLSLVersionDecl(const GrGLContextInfo& info) {
43 switch (info.glslGeneration()) {
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000044 case k110_GrGLSLGeneration:
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +000045 if (kGLES_GrGLStandard == info.standard()) {
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000046 // 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.org9e90aed2014-01-16 16:35:09 +000050 SkASSERT(kGL_GrGLStandard == info.standard());
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000051 return "#version 110\n";
52 }
53 case k130_GrGLSLGeneration:
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +000054 SkASSERT(kGL_GrGLStandard == info.standard());
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000055 return "#version 130\n";
bsalomon@google.com281c7262012-10-23 14:31:30 +000056 case k140_GrGLSLGeneration:
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +000057 SkASSERT(kGL_GrGLStandard == info.standard());
bsalomon@google.com281c7262012-10-23 14:31:30 +000058 return "#version 140\n";
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000059 case k150_GrGLSLGeneration:
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +000060 SkASSERT(kGL_GrGLStandard == info.standard());
commit-bot@chromium.org06f05982013-08-30 15:52:36 +000061 if (info.caps()->isCoreProfile()) {
62 return "#version 150\n";
63 } else {
64 return "#version 150 compatibility\n";
65 }
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000066 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +000067 SkFAIL("Unknown GL version.");
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000068 return ""; // suppress warning
69 }
70}
71
bsalomon@google.com868a8e72012-08-30 19:11:34 +000072namespace {
bsalomon@google.com018f1792013-04-18 19:36:09 +000073 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.org972f9cd2014-03-28 17:58:28 +000076 int cnt = SkTMin((int)SK_ARRAY_COUNT(kTabs), tabCnt);
bsalomon@google.com018f1792013-04-18 19:36:09 +000077 outAppend->append(kTabs, cnt);
78 tabCnt -= cnt;
79 }
bsalomon@google.com868a8e72012-08-30 19:11:34 +000080 }
81}
bsalomon@google.com868a8e72012-08-30 19:11:34 +000082
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000083void GrGLSLMulVarBy4f(SkString* outAppend,
84 unsigned tabCnt,
85 const char* vec4VarName,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000086 const GrGLSLExpr4& mulFactor) {
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000087 if (mulFactor.isOnes()) {
88 *outAppend = SkString();
bsalomon@google.com868a8e72012-08-30 19:11:34 +000089 }
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000090
bsalomon@google.com868a8e72012-08-30 19:11:34 +000091 append_tabs(outAppend, tabCnt);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000092
93 if (mulFactor.isZeros()) {
94 outAppend->appendf("%s = vec4(0);\n", vec4VarName);
commit-bot@chromium.org86607832013-11-21 15:57:40 +000095 } else {
96 outAppend->appendf("%s *= %s;\n", vec4VarName, mulFactor.c_str());
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000097 }
bsalomon@google.com868a8e72012-08-30 19:11:34 +000098}