Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: src/gpu/gl/GrGLSL_impl.h

Issue 25048002: Express (GLSL expression, possibly known value) pairs as a class (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: rebase Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef GrGLSL_impl_DEFINED 8 #ifndef GrGLSL_impl_DEFINED
9 #define GrGLSL_impl_DEFINED 9 #define GrGLSL_impl_DEFINED
10 10
11 #include "SkString.h" 11 template<>
12 inline const char* GrGLSLExpr<4>::zerosStr() {
13 return "vec4(0)";
14 }
12 15
13 namespace { 16 template<>
17 inline const char* GrGLSLExpr<4>::onesStr() {
18 return "vec4(1)";
19 }
20
21 template<>
22 inline const char* GrGLSLExpr<4>::extractAlphaStr() {
23 return "%s.a";
24 }
25
26 template<>
27 inline const char* GrGLSLExpr<4>::castStr() {
28 return "vec4(%s)";
29 }
30 template<>
31 inline const char* GrGLSLExpr<4>::castIntStr() {
32 return "vec4(%d)";
33 }
34
35 template<>
36 inline const char* GrGLSLExpr<1>::zerosStr() {
37 return "0";
38 }
39
40 template<>
41 inline const char* GrGLSLExpr<1>::onesStr() {
42 return "1";
43 }
44
45 // GrGLSLExpr<1>::extractAlphaStr() and GrGLSLExpr<1>::castStr() are
46 // unimplemented because using them is likely an error. This is now caught
47 // compile-time.
48
49 template<>
50 inline const char* GrGLSLExpr<1>::castIntStr() {
51 return "%d";
52 }
53
54 template<>
55 template<>
56 inline GrGLSLExpr<4> GrGLSLExpr<4>::VectorCast(const GrGLSLExpr<4>& expr) {
57 return expr;
58 }
59
60 template<>
61 template<>
62 inline GrGLSLExpr<1> GrGLSLExpr<1>::VectorCast(const GrGLSLExpr<1>& expr) {
63 return expr;
64 }
65
14 template<int N> 66 template<int N>
15 GrSLConstantVec return_const_vecf(GrSLConstantVec constVec, SkString* outAppend, bool omitAppend) { 67 template<int M>
16 SkASSERT(kNone_GrSLConstantVec != constVec); 68 inline GrGLSLExpr<N> GrGLSLExpr<N>::VectorCast(const GrGLSLExpr<M>& expr) {
17 if (!omitAppend) { 69 if (expr.isZeros()) {
18 if (kZeros_GrSLConstantVec == constVec) { 70 return GrGLSLExpr<N>(0);
19 outAppend->append(GrGLSLZerosVecf(N)); 71 }
20 } else { 72 if (expr.isOnes()) {
21 outAppend->append(GrGLSLOnesVecf(N)); 73 return GrGLSLExpr<N>(1);
74 }
75 return GrGLSLExpr<N>(GrGLSLExpr<N>::castStr(), expr.c_str());
76 }
77
78 template<int N>
79 template<int M>
80 inline GrGLSLExpr<N> GrGLSLExpr<N>::mul(const GrGLSLExpr<N>& in0, const GrGLSLEx pr<M>& in1) {
81 if (in0.isZeros() || in1.isZeros()) {
82 return GrGLSLExpr<N>(0);
83 }
84 if (in0.isOnes()) {
85 return VectorCast<M>(in1);
86 }
87 if (in1.isOnes()) {
88 return in0;
89 }
90 return GrGLSLExpr<N>("(%s * %s)", in0.c_str(), in1.c_str());
91 }
92
93 template<int N>
94 template<int M>
95 inline GrGLSLExpr<N> GrGLSLExpr<N>::add(const GrGLSLExpr<N>& in0, const GrGLSLEx pr<M>& in1) {
96 if (in1.isZeros()) {
97 return in0;
98 }
99 if (in0.isZeros()) {
100 return VectorCast<M>(in1);
101 }
102 if (in0.isOnes() && in1.isOnes()) {
103 return GrGLSLExpr<N>(2);
104 }
105 return GrGLSLExpr<N>("(%s + %s)", in0.c_str(), in1.c_str());
106 }
107
108 template<int N>
109 template<int M>
110 inline GrGLSLExpr<N> GrGLSLExpr<N>::sub(const GrGLSLExpr<N>& in0, const GrGLSLEx pr<M>& in1) {
111 if (in1.isZeros()) {
112 return in0;
113 }
114 if (in1.isOnes()) {
115 if (in0.isOnes()) {
116 return GrGLSLExpr<N>(0);
22 } 117 }
23 } 118 }
24 return constVec; 119
25 } 120 return GrGLSLExpr<N>("(%s - %s)", in0.c_str(), in1.c_str());
26 } 121 }
27 122
28 template <int N> 123 inline GrGLSLExpr<4> GrGLSLExprCast4(const GrGLSLExpr<1>& expr) {
29 GrSLConstantVec GrGLSLModulatef(SkString* outAppend, 124 return GrGLSLExpr<4>::VectorCast(expr);
30 const char* in0,
31 const char* in1,
32 GrSLConstantVec default0,
33 GrSLConstantVec default1,
34 bool omitIfConstVec) {
35 SkASSERT(N > 0 && N <= 4);
36 SkASSERT(NULL != outAppend);
37
38 bool has0 = NULL != in0 && '\0' != *in0;
39 bool has1 = NULL != in1 && '\0' != *in1;
40
41 SkASSERT(has0 || kNone_GrSLConstantVec != default0);
42 SkASSERT(has1 || kNone_GrSLConstantVec != default1);
43
44 if (!has0 && !has1) {
45 SkASSERT(kZeros_GrSLConstantVec == default0 || kOnes_GrSLConstantVec == default0);
46 SkASSERT(kZeros_GrSLConstantVec == default1 || kOnes_GrSLConstantVec == default1);
47 if (kZeros_GrSLConstantVec == default0 || kZeros_GrSLConstantVec == defa ult1) {
48 return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitI fConstVec);
49 } else {
50 // both inputs are ones vectors
51 return return_const_vecf<N>(kOnes_GrSLConstantVec, outAppend, omitIf ConstVec);
52 }
53 } else if (!has0) {
54 SkASSERT(kZeros_GrSLConstantVec == default0 || kOnes_GrSLConstantVec == default0);
55 if (kZeros_GrSLConstantVec == default0) {
56 return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitI fConstVec);
57 } else {
58 outAppend->appendf("%s(%s)", GrGLSLFloatVectorTypeString(N), in1);
59 return kNone_GrSLConstantVec;
60 }
61 } else if (!has1) {
62 SkASSERT(kZeros_GrSLConstantVec == default1 || kOnes_GrSLConstantVec == default1);
63 if (kZeros_GrSLConstantVec == default1) {
64 return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitI fConstVec);
65 } else {
66 outAppend->appendf("%s(%s)", GrGLSLFloatVectorTypeString(N), in0);
67 return kNone_GrSLConstantVec;
68 }
69 } else {
70 outAppend->appendf("%s((%s) * (%s))", GrGLSLFloatVectorTypeString(N), in 0, in1);
71 return kNone_GrSLConstantVec;
72 }
73 } 125 }
74 126
75 template <int N> 127 inline GrGLSLExpr<1> GrGLSLExprExtractAlpha(const GrGLSLExpr<4>& expr) {
76 GrSLConstantVec GrGLSLAddf(SkString* outAppend, 128 if (expr.isZeros()) {
77 const char* in0, 129 return GrGLSLExpr<1>(0);
78 const char* in1,
79 GrSLConstantVec default0,
80 GrSLConstantVec default1,
81 bool omitIfConstVec) {
82 SkASSERT(N > 0 && N <= 4);
83 SkASSERT(NULL != outAppend);
84
85 bool has0 = NULL != in0 && '\0' != *in0;
86 bool has1 = NULL != in1 && '\0' != *in1;
87
88 if (!has0 && !has1) {
89 SkASSERT(kNone_GrSLConstantVec != default0);
90 SkASSERT(kNone_GrSLConstantVec != default1);
91 int sum = (kOnes_GrSLConstantVec == default0) + (kOnes_GrSLConstantVec = = default1);
92 if (0 == sum) {
93 return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitI fConstVec);
94 } else if (1 == sum) {
95 outAppend->append(GrGLSLOnesVecf(N));
96 return return_const_vecf<N>(kOnes_GrSLConstantVec, outAppend, omitIf ConstVec);
97 } else {
98 SkASSERT(2 == sum);
99 outAppend->appendf("%s(2)", GrGLSLFloatVectorTypeString(N));
100 return kNone_GrSLConstantVec;
101 }
102 } else if (!has0) {
103 SkASSERT(kNone_GrSLConstantVec != default0);
104 if (kZeros_GrSLConstantVec == default0) {
105 outAppend->appendf("%s(%s)", GrGLSLFloatVectorTypeString(N), in1);
106 } else {
107 outAppend->appendf("%s(%s) + %s",
108 GrGLSLFloatVectorTypeString(N),
109 in1,
110 GrGLSLOnesVecf(N));
111 }
112 return kNone_GrSLConstantVec;
113 } else if (!has1) {
114 SkASSERT(kNone_GrSLConstantVec != default1);
115 if (kZeros_GrSLConstantVec == default1) {
116 outAppend->appendf("%s(%s)", GrGLSLFloatVectorTypeString(N), in0);
117 } else {
118 outAppend->appendf("%s(%s) + %s",
119 GrGLSLFloatVectorTypeString(N),
120 in0,
121 GrGLSLOnesVecf(N));
122 }
123 return kNone_GrSLConstantVec;
124 } else {
125 outAppend->appendf("(%s(%s) + %s(%s))",
126 GrGLSLFloatVectorTypeString(N),
127 in0,
128 GrGLSLFloatVectorTypeString(N),
129 in1);
130 return kNone_GrSLConstantVec;
131 } 130 }
132 } 131 if (expr.isOnes()) {
133 132 return GrGLSLExpr<1>(1);
134 template <int N>
135 GrSLConstantVec GrGLSLSubtractf(SkString* outAppend,
136 const char* in0,
137 const char* in1,
138 GrSLConstantVec default0,
139 GrSLConstantVec default1,
140 bool omitIfConstVec) {
141 SkASSERT(N > 0 && N <= 4);
142 SkASSERT(NULL != outAppend);
143
144 bool has0 = NULL != in0 && '\0' != *in0;
145 bool has1 = NULL != in1 && '\0' != *in1;
146
147 if (!has0 && !has1) {
148 SkASSERT(kNone_GrSLConstantVec != default0);
149 SkASSERT(kNone_GrSLConstantVec != default1);
150 int diff = (kOnes_GrSLConstantVec == default0) - (kOnes_GrSLConstantVec == default1);
151 if (-1 == diff) {
152 outAppend->appendf("%s(-1)", GrGLSLFloatVectorTypeString(N));
153 return kNone_GrSLConstantVec;
154 } else if (0 == diff) {
155 return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitI fConstVec);
156 } else {
157 SkASSERT(1 == diff);
158 return return_const_vecf<N>(kOnes_GrSLConstantVec, outAppend, omitIf ConstVec);
159 }
160 } else if (!has0) {
161 SkASSERT(kNone_GrSLConstantVec != default0);
162 if (kZeros_GrSLConstantVec == default0) {
163 outAppend->appendf("-%s(%s)", GrGLSLFloatVectorTypeString(N), in1);
164 } else {
165 outAppend->appendf("%s - %s(%s)",
166 GrGLSLOnesVecf(N),
167 GrGLSLFloatVectorTypeString(N),
168 in1);
169 }
170 return kNone_GrSLConstantVec;
171 } else if (!has1) {
172 SkASSERT(kNone_GrSLConstantVec != default1);
173 if (kZeros_GrSLConstantVec == default1) {
174 outAppend->appendf("%s(%s)", GrGLSLFloatVectorTypeString(N), in0);
175 } else {
176 outAppend->appendf("%s(%s) - %s",
177 GrGLSLFloatVectorTypeString(N),
178 in0,
179 GrGLSLOnesVecf(N));
180 }
181 return kNone_GrSLConstantVec;
182 } else {
183 outAppend->appendf("(%s(%s) - %s(%s))",
184 GrGLSLFloatVectorTypeString(N),
185 in0,
186 GrGLSLFloatVectorTypeString(N),
187 in1);
188 return kNone_GrSLConstantVec;
189 } 133 }
134 return GrGLSLExpr<1>(expr.extractAlphaStr(), expr.c_str());
190 } 135 }
191 136
192 #endif 137 #endif
OLDNEW
« src/gpu/gl/GrGLSL.h ('K') | « src/gpu/gl/GrGLSL.cpp ('k') | src/gpu/gl/GrGLShaderBuilder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698