OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 #include "GrGLSLBlend.h" | 7 #include "GrGLSLBlend.h" |
8 #include "gl/builders/GrGLFragmentShaderBuilder.h" | 8 #include "glsl/GrGLSLFragmentShaderBuilder.h" |
9 | 9 |
10 ////////////////////////////////////////////////////////////////////////////// | 10 ////////////////////////////////////////////////////////////////////////////// |
11 // Advanced (non-coeff) blend helpers | 11 // Advanced (non-coeff) blend helpers |
12 ////////////////////////////////////////////////////////////////////////////// | 12 ////////////////////////////////////////////////////////////////////////////// |
13 | 13 |
14 static void hard_light(GrGLFragmentBuilder* fsBuilder, | 14 static void hard_light(GrGLSLFragmentBuilder* fsBuilder, |
15 const char* final, | 15 const char* final, |
16 const char* src, | 16 const char* src, |
17 const char* dst) { | 17 const char* dst) { |
18 static const char kComponents[] = { 'r', 'g', 'b' }; | 18 static const char kComponents[] = { 'r', 'g', 'b' }; |
19 for (size_t i = 0; i < SK_ARRAY_COUNT(kComponents); ++i) { | 19 for (size_t i = 0; i < SK_ARRAY_COUNT(kComponents); ++i) { |
20 char component = kComponents[i]; | 20 char component = kComponents[i]; |
21 fsBuilder->codeAppendf("if (2.0 * %s.%c <= %s.a) {", src, component, src
); | 21 fsBuilder->codeAppendf("if (2.0 * %s.%c <= %s.a) {", src, component, src
); |
22 fsBuilder->codeAppendf("%s.%c = 2.0 * %s.%c * %s.%c;", | 22 fsBuilder->codeAppendf("%s.%c = 2.0 * %s.%c * %s.%c;", |
23 final, component, src, component, dst, component)
; | 23 final, component, src, component, dst, component)
; |
24 fsBuilder->codeAppend("} else {"); | 24 fsBuilder->codeAppend("} else {"); |
25 fsBuilder->codeAppendf("%s.%c = %s.a * %s.a - 2.0 * (%s.a - %s.%c) * (%s
.a - %s.%c);", | 25 fsBuilder->codeAppendf("%s.%c = %s.a * %s.a - 2.0 * (%s.a - %s.%c) * (%s
.a - %s.%c);", |
26 final, component, src, dst, dst, dst, component,
src, src, | 26 final, component, src, dst, dst, dst, component,
src, src, |
27 component); | 27 component); |
28 fsBuilder->codeAppend("}"); | 28 fsBuilder->codeAppend("}"); |
29 } | 29 } |
30 fsBuilder->codeAppendf("%s.rgb += %s.rgb * (1.0 - %s.a) + %s.rgb * (1.0 - %s
.a);", | 30 fsBuilder->codeAppendf("%s.rgb += %s.rgb * (1.0 - %s.a) + %s.rgb * (1.0 - %s
.a);", |
31 final, src, dst, dst, src); | 31 final, src, dst, dst, src); |
32 } | 32 } |
33 | 33 |
34 // Does one component of color-dodge | 34 // Does one component of color-dodge |
35 static void color_dodge_component(GrGLFragmentBuilder* fsBuilder, | 35 static void color_dodge_component(GrGLSLFragmentBuilder* fsBuilder, |
36 const char* final, | 36 const char* final, |
37 const char* src, | 37 const char* src, |
38 const char* dst, | 38 const char* dst, |
39 const char component) { | 39 const char component) { |
40 fsBuilder->codeAppendf("if (0.0 == %s.%c) {", dst, component); | 40 fsBuilder->codeAppendf("if (0.0 == %s.%c) {", dst, component); |
41 fsBuilder->codeAppendf("%s.%c = %s.%c * (1.0 - %s.a);", | 41 fsBuilder->codeAppendf("%s.%c = %s.%c * (1.0 - %s.a);", |
42 final, component, src, component, dst); | 42 final, component, src, component, dst); |
43 fsBuilder->codeAppend("} else {"); | 43 fsBuilder->codeAppend("} else {"); |
44 fsBuilder->codeAppendf("float d = %s.a - %s.%c;", src, src, component); | 44 fsBuilder->codeAppendf("float d = %s.a - %s.%c;", src, src, component); |
45 fsBuilder->codeAppend("if (0.0 == d) {"); | 45 fsBuilder->codeAppend("if (0.0 == d) {"); |
46 fsBuilder->codeAppendf("%s.%c = %s.a * %s.a + %s.%c * (1.0 - %s.a) + %s.%c *
(1.0 - %s.a);", | 46 fsBuilder->codeAppendf("%s.%c = %s.a * %s.a + %s.%c * (1.0 - %s.a) + %s.%c *
(1.0 - %s.a);", |
47 final, component, src, dst, src, component, dst, dst,
component, | 47 final, component, src, dst, src, component, dst, dst,
component, |
48 src); | 48 src); |
49 fsBuilder->codeAppend("} else {"); | 49 fsBuilder->codeAppend("} else {"); |
50 fsBuilder->codeAppendf("d = min(%s.a, %s.%c * %s.a / d);", | 50 fsBuilder->codeAppendf("d = min(%s.a, %s.%c * %s.a / d);", |
51 dst, dst, component, src); | 51 dst, dst, component, src); |
52 fsBuilder->codeAppendf("%s.%c = d * %s.a + %s.%c * (1.0 - %s.a) + %s.%c * (1
.0 - %s.a);", | 52 fsBuilder->codeAppendf("%s.%c = d * %s.a + %s.%c * (1.0 - %s.a) + %s.%c * (1
.0 - %s.a);", |
53 final, component, src, src, component, dst, dst, comp
onent, src); | 53 final, component, src, src, component, dst, dst, comp
onent, src); |
54 fsBuilder->codeAppend("}"); | 54 fsBuilder->codeAppend("}"); |
55 fsBuilder->codeAppend("}"); | 55 fsBuilder->codeAppend("}"); |
56 } | 56 } |
57 | 57 |
58 // Does one component of color-burn | 58 // Does one component of color-burn |
59 static void color_burn_component(GrGLFragmentBuilder* fsBuilder, | 59 static void color_burn_component(GrGLSLFragmentBuilder* fsBuilder, |
60 const char* final, | 60 const char* final, |
61 const char* src, | 61 const char* src, |
62 const char* dst, | 62 const char* dst, |
63 const char component) { | 63 const char component) { |
64 fsBuilder->codeAppendf("if (%s.a == %s.%c) {", dst, dst, component); | 64 fsBuilder->codeAppendf("if (%s.a == %s.%c) {", dst, dst, component); |
65 fsBuilder->codeAppendf("%s.%c = %s.a * %s.a + %s.%c * (1.0 - %s.a) + %s.%c *
(1.0 - %s.a);", | 65 fsBuilder->codeAppendf("%s.%c = %s.a * %s.a + %s.%c * (1.0 - %s.a) + %s.%c *
(1.0 - %s.a);", |
66 final, component, src, dst, src, component, dst, dst,
component, | 66 final, component, src, dst, src, component, dst, dst,
component, |
67 src); | 67 src); |
68 fsBuilder->codeAppendf("} else if (0.0 == %s.%c) {", src, component); | 68 fsBuilder->codeAppendf("} else if (0.0 == %s.%c) {", src, component); |
69 fsBuilder->codeAppendf("%s.%c = %s.%c * (1.0 - %s.a);", | 69 fsBuilder->codeAppendf("%s.%c = %s.%c * (1.0 - %s.a);", |
70 final, component, dst, component, src); | 70 final, component, dst, component, src); |
71 fsBuilder->codeAppend("} else {"); | 71 fsBuilder->codeAppend("} else {"); |
72 fsBuilder->codeAppendf("float d = max(0.0, %s.a - (%s.a - %s.%c) * %s.a / %s
.%c);", | 72 fsBuilder->codeAppendf("float d = max(0.0, %s.a - (%s.a - %s.%c) * %s.a / %s
.%c);", |
73 dst, dst, dst, component, src, src, component); | 73 dst, dst, dst, component, src, src, component); |
74 fsBuilder->codeAppendf("%s.%c = %s.a * d + %s.%c * (1.0 - %s.a) + %s.%c * (1
.0 - %s.a);", | 74 fsBuilder->codeAppendf("%s.%c = %s.a * d + %s.%c * (1.0 - %s.a) + %s.%c * (1
.0 - %s.a);", |
75 final, component, src, src, component, dst, dst, comp
onent, src); | 75 final, component, src, src, component, dst, dst, comp
onent, src); |
76 fsBuilder->codeAppend("}"); | 76 fsBuilder->codeAppend("}"); |
77 } | 77 } |
78 | 78 |
79 // Does one component of soft-light. Caller should have already checked that dst
alpha > 0. | 79 // Does one component of soft-light. Caller should have already checked that dst
alpha > 0. |
80 static void soft_light_component_pos_dst_alpha(GrGLFragmentBuilder* fsBuilder, | 80 static void soft_light_component_pos_dst_alpha(GrGLSLFragmentBuilder* fsBuilder, |
81 const char* final, | 81 const char* final, |
82 const char* src, | 82 const char* src, |
83 const char* dst, | 83 const char* dst, |
84 const char component) { | 84 const char component) { |
85 // if (2S < Sa) | 85 // if (2S < Sa) |
86 fsBuilder->codeAppendf("if (2.0 * %s.%c <= %s.a) {", src, component, src); | 86 fsBuilder->codeAppendf("if (2.0 * %s.%c <= %s.a) {", src, component, src); |
87 // (D^2 (Sa-2 S))/Da+(1-Da) S+D (-Sa+2 S+1) | 87 // (D^2 (Sa-2 S))/Da+(1-Da) S+D (-Sa+2 S+1) |
88 fsBuilder->codeAppendf("%s.%c = (%s.%c*%s.%c*(%s.a - 2.0*%s.%c)) / %s.a +" | 88 fsBuilder->codeAppendf("%s.%c = (%s.%c*%s.%c*(%s.a - 2.0*%s.%c)) / %s.a +" |
89 "(1.0 - %s.a) * %s.%c + %s.%c*(-%s.a + 2.0*%s.%c + 1.
0);", | 89 "(1.0 - %s.a) * %s.%c + %s.%c*(-%s.a + 2.0*%s.%c + 1.
0);", |
90 final, component, dst, component, dst, component, src
, src, | 90 final, component, dst, component, dst, component, src
, src, |
(...skipping 21 matching lines...) Expand all Loading... |
112 " sqrt(%s.a*%s.%c)*(%s.a - 2.0*%s.%c) - %s.a*%s.%c;", | 112 " sqrt(%s.a*%s.%c)*(%s.a - 2.0*%s.%c) - %s.a*%s.%c;", |
113 final, component, dst, component, src, src, component
, src, component, | 113 final, component, dst, component, src, src, component
, src, component, |
114 dst, dst, component, src, src, component, dst, src, c
omponent); | 114 dst, dst, component, src, src, component, dst, src, c
omponent); |
115 fsBuilder->codeAppendf("}"); | 115 fsBuilder->codeAppendf("}"); |
116 } | 116 } |
117 | 117 |
118 // Adds a function that takes two colors and an alpha as input. It produces a co
lor with the | 118 // Adds a function that takes two colors and an alpha as input. It produces a co
lor with the |
119 // hue and saturation of the first color, the luminosity of the second color, an
d the input | 119 // hue and saturation of the first color, the luminosity of the second color, an
d the input |
120 // alpha. It has this signature: | 120 // alpha. It has this signature: |
121 // vec3 set_luminance(vec3 hueSatColor, float alpha, vec3 lumColor). | 121 // vec3 set_luminance(vec3 hueSatColor, float alpha, vec3 lumColor). |
122 static void add_lum_function(GrGLFragmentBuilder* fsBuilder, SkString* setLumFun
ction) { | 122 static void add_lum_function(GrGLSLFragmentBuilder* fsBuilder, SkString* setLumF
unction) { |
123 // Emit a helper that gets the luminance of a color. | 123 // Emit a helper that gets the luminance of a color. |
124 SkString getFunction; | 124 SkString getFunction; |
125 GrGLSLShaderVar getLumArgs[] = { | 125 GrGLSLShaderVar getLumArgs[] = { |
126 GrGLSLShaderVar("color", kVec3f_GrSLType), | 126 GrGLSLShaderVar("color", kVec3f_GrSLType), |
127 }; | 127 }; |
128 SkString getLumBody("return dot(vec3(0.3, 0.59, 0.11), color);"); | 128 SkString getLumBody("return dot(vec3(0.3, 0.59, 0.11), color);"); |
129 fsBuilder->emitFunction(kFloat_GrSLType, | 129 fsBuilder->emitFunction(kFloat_GrSLType, |
130 "luminance", | 130 "luminance", |
131 SK_ARRAY_COUNT(getLumArgs), getLumArgs, | 131 SK_ARRAY_COUNT(getLumArgs), getLumArgs, |
132 getLumBody.c_str(), | 132 getLumBody.c_str(), |
(...skipping 24 matching lines...) Expand all Loading... |
157 fsBuilder->emitFunction(kVec3f_GrSLType, | 157 fsBuilder->emitFunction(kVec3f_GrSLType, |
158 "set_luminance", | 158 "set_luminance", |
159 SK_ARRAY_COUNT(setLumArgs), setLumArgs, | 159 SK_ARRAY_COUNT(setLumArgs), setLumArgs, |
160 setLumBody.c_str(), | 160 setLumBody.c_str(), |
161 setLumFunction); | 161 setLumFunction); |
162 } | 162 } |
163 | 163 |
164 // Adds a function that creates a color with the hue and luminosity of one input
color and | 164 // Adds a function that creates a color with the hue and luminosity of one input
color and |
165 // the saturation of another color. It will have this signature: | 165 // the saturation of another color. It will have this signature: |
166 // float set_saturation(vec3 hueLumColor, vec3 satColor) | 166 // float set_saturation(vec3 hueLumColor, vec3 satColor) |
167 static void add_sat_function(GrGLFragmentBuilder* fsBuilder, SkString* setSatFun
ction) { | 167 static void add_sat_function(GrGLSLFragmentBuilder* fsBuilder, SkString* setSatF
unction) { |
168 // Emit a helper that gets the saturation of a color | 168 // Emit a helper that gets the saturation of a color |
169 SkString getFunction; | 169 SkString getFunction; |
170 GrGLSLShaderVar getSatArgs[] = { GrGLSLShaderVar("color", kVec3f_GrSLType) }
; | 170 GrGLSLShaderVar getSatArgs[] = { GrGLSLShaderVar("color", kVec3f_GrSLType) }
; |
171 SkString getSatBody; | 171 SkString getSatBody; |
172 getSatBody.printf("return max(max(color.r, color.g), color.b) - " | 172 getSatBody.printf("return max(max(color.r, color.g), color.b) - " |
173 "min(min(color.r, color.g), color.b);"); | 173 "min(min(color.r, color.g), color.b);"); |
174 fsBuilder->emitFunction(kFloat_GrSLType, | 174 fsBuilder->emitFunction(kFloat_GrSLType, |
175 "saturation", | 175 "saturation", |
176 SK_ARRAY_COUNT(getSatArgs), getSatArgs, | 176 SK_ARRAY_COUNT(getSatArgs), getSatArgs, |
177 getSatBody.c_str(), | 177 getSatBody.c_str(), |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 "return hueLumColor;", | 228 "return hueLumColor;", |
229 getFunction.c_str(), helpFunc, helpFunc, helpFunc, helpFu
nc, | 229 getFunction.c_str(), helpFunc, helpFunc, helpFunc, helpFu
nc, |
230 helpFunc, helpFunc); | 230 helpFunc, helpFunc); |
231 fsBuilder->emitFunction(kVec3f_GrSLType, | 231 fsBuilder->emitFunction(kVec3f_GrSLType, |
232 "set_saturation", | 232 "set_saturation", |
233 SK_ARRAY_COUNT(setSatArgs), setSatArgs, | 233 SK_ARRAY_COUNT(setSatArgs), setSatArgs, |
234 setSatBody.c_str(), | 234 setSatBody.c_str(), |
235 setSatFunction); | 235 setSatFunction); |
236 } | 236 } |
237 | 237 |
238 static void emit_advanced_xfermode_code(GrGLFragmentBuilder* fsBuilder, const ch
ar* srcColor, | 238 static void emit_advanced_xfermode_code(GrGLSLFragmentBuilder* fsBuilder, const
char* srcColor, |
239 const char* dstColor, const char* output
Color, | 239 const char* dstColor, const char* output
Color, |
240 SkXfermode::Mode mode) { | 240 SkXfermode::Mode mode) { |
241 SkASSERT(srcColor); | 241 SkASSERT(srcColor); |
242 SkASSERT(dstColor); | 242 SkASSERT(dstColor); |
243 SkASSERT(outputColor); | 243 SkASSERT(outputColor); |
244 // These all perform src-over on the alpha channel. | 244 // These all perform src-over on the alpha channel. |
245 fsBuilder->codeAppendf("%s.a = %s.a + (1.0 - %s.a) * %s.a;", | 245 fsBuilder->codeAppendf("%s.a = %s.a + (1.0 - %s.a) * %s.a;", |
246 outputColor, srcColor, srcColor, dstColor); | 246 outputColor, srcColor, srcColor, dstColor); |
247 | 247 |
248 switch (mode) { | 248 switch (mode) { |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 default: | 361 default: |
362 SkFAIL("Unknown Custom Xfer mode."); | 362 SkFAIL("Unknown Custom Xfer mode."); |
363 break; | 363 break; |
364 } | 364 } |
365 } | 365 } |
366 | 366 |
367 ////////////////////////////////////////////////////////////////////////////// | 367 ////////////////////////////////////////////////////////////////////////////// |
368 // Porter-Duff blend helper | 368 // Porter-Duff blend helper |
369 ////////////////////////////////////////////////////////////////////////////// | 369 ////////////////////////////////////////////////////////////////////////////// |
370 | 370 |
371 static bool append_porterduff_term(GrGLFragmentBuilder* fsBuilder, SkXfermode::C
oeff coeff, | 371 static bool append_porterduff_term(GrGLSLFragmentBuilder* fsBuilder, SkXfermode:
:Coeff coeff, |
372 const char* colorName, const char* srcColorNa
me, | 372 const char* colorName, const char* srcColorNa
me, |
373 const char* dstColorName, bool hasPrevious) { | 373 const char* dstColorName, bool hasPrevious) { |
374 if (SkXfermode::kZero_Coeff == coeff) { | 374 if (SkXfermode::kZero_Coeff == coeff) { |
375 return hasPrevious; | 375 return hasPrevious; |
376 } else { | 376 } else { |
377 if (hasPrevious) { | 377 if (hasPrevious) { |
378 fsBuilder->codeAppend(" + "); | 378 fsBuilder->codeAppend(" + "); |
379 } | 379 } |
380 fsBuilder->codeAppendf("%s", colorName); | 380 fsBuilder->codeAppendf("%s", colorName); |
381 switch (coeff) { | 381 switch (coeff) { |
(...skipping 25 matching lines...) Expand all Loading... |
407 break; | 407 break; |
408 default: | 408 default: |
409 SkFAIL("Unsupported Blend Coeff"); | 409 SkFAIL("Unsupported Blend Coeff"); |
410 } | 410 } |
411 return true; | 411 return true; |
412 } | 412 } |
413 } | 413 } |
414 | 414 |
415 ////////////////////////////////////////////////////////////////////////////// | 415 ////////////////////////////////////////////////////////////////////////////// |
416 | 416 |
417 void GrGLSLBlend::AppendMode(GrGLFragmentBuilder* fsBuilder, const char* srcColo
r, | 417 void GrGLSLBlend::AppendMode(GrGLSLFragmentBuilder* fsBuilder, const char* srcCo
lor, |
418 const char* dstColor, const char* outColor, | 418 const char* dstColor, const char* outColor, |
419 SkXfermode::Mode mode) { | 419 SkXfermode::Mode mode) { |
420 | 420 |
421 SkXfermode::Coeff srcCoeff, dstCoeff; | 421 SkXfermode::Coeff srcCoeff, dstCoeff; |
422 if (SkXfermode::ModeAsCoeff(mode, &srcCoeff, &dstCoeff)) { | 422 if (SkXfermode::ModeAsCoeff(mode, &srcCoeff, &dstCoeff)) { |
423 fsBuilder->codeAppendf("%s = ", outColor); | 423 fsBuilder->codeAppendf("%s = ", outColor); |
424 // append src blend | 424 // append src blend |
425 bool didAppend = append_porterduff_term(fsBuilder, srcCoeff, srcColor, s
rcColor, dstColor, | 425 bool didAppend = append_porterduff_term(fsBuilder, srcCoeff, srcColor, s
rcColor, dstColor, |
426 false); | 426 false); |
427 // append dst blend | 427 // append dst blend |
428 if(!append_porterduff_term(fsBuilder, dstCoeff, dstColor, srcColor, dstC
olor, didAppend)) { | 428 if(!append_porterduff_term(fsBuilder, dstCoeff, dstColor, srcColor, dstC
olor, didAppend)) { |
429 fsBuilder->codeAppend("vec4(0, 0, 0, 0)"); | 429 fsBuilder->codeAppend("vec4(0, 0, 0, 0)"); |
430 } | 430 } |
431 fsBuilder->codeAppend(";"); | 431 fsBuilder->codeAppend(";"); |
432 } else { | 432 } else { |
433 emit_advanced_xfermode_code(fsBuilder, srcColor, dstColor, outColor, mod
e); | 433 emit_advanced_xfermode_code(fsBuilder, srcColor, dstColor, outColor, mod
e); |
434 } | 434 } |
435 } | 435 } |
OLD | NEW |