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 "gl/builders/GrGLFragmentShaderBuilder.h" |
9 | 9 |
10 ////////////////////////////////////////////////////////////////////////////// | 10 ////////////////////////////////////////////////////////////////////////////// |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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(GrGLFragmentBuilder* fsBuilder, SkString* setLumFun
ction) { |
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 GrGLShaderVar getLumArgs[] = { | 125 GrGLSLShaderVar getLumArgs[] = { |
126 GrGLShaderVar("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(), |
133 &getFunction); | 133 &getFunction); |
134 | 134 |
135 // Emit the set luminance function. | 135 // Emit the set luminance function. |
136 GrGLShaderVar setLumArgs[] = { | 136 GrGLSLShaderVar setLumArgs[] = { |
137 GrGLShaderVar("hueSat", kVec3f_GrSLType), | 137 GrGLSLShaderVar("hueSat", kVec3f_GrSLType), |
138 GrGLShaderVar("alpha", kFloat_GrSLType), | 138 GrGLSLShaderVar("alpha", kFloat_GrSLType), |
139 GrGLShaderVar("lumColor", kVec3f_GrSLType), | 139 GrGLSLShaderVar("lumColor", kVec3f_GrSLType), |
140 }; | 140 }; |
141 SkString setLumBody; | 141 SkString setLumBody; |
142 setLumBody.printf("float diff = %s(lumColor - hueSat);", getFunction.c_str()
); | 142 setLumBody.printf("float diff = %s(lumColor - hueSat);", getFunction.c_str()
); |
143 setLumBody.append("vec3 outColor = hueSat + diff;"); | 143 setLumBody.append("vec3 outColor = hueSat + diff;"); |
144 setLumBody.appendf("float outLum = %s(outColor);", getFunction.c_str()); | 144 setLumBody.appendf("float outLum = %s(outColor);", getFunction.c_str()); |
145 setLumBody.append("float minComp = min(min(outColor.r, outColor.g), outColor
.b);" | 145 setLumBody.append("float minComp = min(min(outColor.r, outColor.g), outColor
.b);" |
146 "float maxComp = max(max(outColor.r, outColor.g), outColor
.b);" | 146 "float maxComp = max(max(outColor.r, outColor.g), outColor
.b);" |
147 "if (minComp < 0.0 && outLum != minComp) {" | 147 "if (minComp < 0.0 && outLum != minComp) {" |
148 "outColor = outLum + ((outColor - vec3(outLum, outLum, out
Lum)) * outLum) /" | 148 "outColor = outLum + ((outColor - vec3(outLum, outLum, out
Lum)) * outLum) /" |
149 "(outLum - minComp);" | 149 "(outLum - minComp);" |
(...skipping 10 matching lines...) Expand all Loading... |
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(GrGLFragmentBuilder* fsBuilder, SkString* setSatFun
ction) { |
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 GrGLShaderVar getSatArgs[] = { GrGLShaderVar("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(), |
178 &getFunction); | 178 &getFunction); |
179 | 179 |
180 // Emit a helper that sets the saturation given sorted input channels. This
used | 180 // Emit a helper that sets the saturation given sorted input channels. This
used |
181 // to use inout params for min, mid, and max components but that seems to ca
use | 181 // to use inout params for min, mid, and max components but that seems to ca
use |
182 // problems on PowerVR drivers. So instead it returns a vec3 where r, g ,b a
re the | 182 // problems on PowerVR drivers. So instead it returns a vec3 where r, g ,b a
re the |
183 // adjusted min, mid, and max inputs, respectively. | 183 // adjusted min, mid, and max inputs, respectively. |
184 SkString helperFunction; | 184 SkString helperFunction; |
185 GrGLShaderVar helperArgs[] = { | 185 GrGLSLShaderVar helperArgs[] = { |
186 GrGLShaderVar("minComp", kFloat_GrSLType), | 186 GrGLSLShaderVar("minComp", kFloat_GrSLType), |
187 GrGLShaderVar("midComp", kFloat_GrSLType), | 187 GrGLSLShaderVar("midComp", kFloat_GrSLType), |
188 GrGLShaderVar("maxComp", kFloat_GrSLType), | 188 GrGLSLShaderVar("maxComp", kFloat_GrSLType), |
189 GrGLShaderVar("sat", kFloat_GrSLType), | 189 GrGLSLShaderVar("sat", kFloat_GrSLType), |
190 }; | 190 }; |
191 static const char kHelperBody[] = "if (minComp < maxComp) {" | 191 static const char kHelperBody[] = "if (minComp < maxComp) {" |
192 "vec3 result;" | 192 "vec3 result;" |
193 "result.r = 0.0;" | 193 "result.r = 0.0;" |
194 "result.g = sat * (midComp - minComp) / (maxComp - minComp);" | 194 "result.g = sat * (midComp - minComp) / (maxComp - minComp);" |
195 "result.b = sat;" | 195 "result.b = sat;" |
196 "return result;" | 196 "return result;" |
197 "} else {" | 197 "} else {" |
198 "return vec3(0, 0, 0);" | 198 "return vec3(0, 0, 0);" |
199 "}"; | 199 "}"; |
200 fsBuilder->emitFunction(kVec3f_GrSLType, | 200 fsBuilder->emitFunction(kVec3f_GrSLType, |
201 "set_saturation_helper", | 201 "set_saturation_helper", |
202 SK_ARRAY_COUNT(helperArgs), helperArgs, | 202 SK_ARRAY_COUNT(helperArgs), helperArgs, |
203 kHelperBody, | 203 kHelperBody, |
204 &helperFunction); | 204 &helperFunction); |
205 | 205 |
206 GrGLShaderVar setSatArgs[] = { | 206 GrGLSLShaderVar setSatArgs[] = { |
207 GrGLShaderVar("hueLumColor", kVec3f_GrSLType), | 207 GrGLSLShaderVar("hueLumColor", kVec3f_GrSLType), |
208 GrGLShaderVar("satColor", kVec3f_GrSLType), | 208 GrGLSLShaderVar("satColor", kVec3f_GrSLType), |
209 }; | 209 }; |
210 const char* helpFunc = helperFunction.c_str(); | 210 const char* helpFunc = helperFunction.c_str(); |
211 SkString setSatBody; | 211 SkString setSatBody; |
212 setSatBody.appendf("float sat = %s(satColor);" | 212 setSatBody.appendf("float sat = %s(satColor);" |
213 "if (hueLumColor.r <= hueLumColor.g) {" | 213 "if (hueLumColor.r <= hueLumColor.g) {" |
214 "if (hueLumColor.g <= hueLumColor.b) {" | 214 "if (hueLumColor.g <= hueLumColor.b) {" |
215 "hueLumColor.rgb = %s(hueLumColor.r, hueLumColor.g, hueLu
mColor.b, sat);" | 215 "hueLumColor.rgb = %s(hueLumColor.r, hueLumColor.g, hueLu
mColor.b, sat);" |
216 "} else if (hueLumColor.r <= hueLumColor.b) {" | 216 "} else if (hueLumColor.r <= hueLumColor.b) {" |
217 "hueLumColor.rbg = %s(hueLumColor.r, hueLumColor.b, hueLu
mColor.g, sat);" | 217 "hueLumColor.rbg = %s(hueLumColor.r, hueLumColor.b, hueLu
mColor.g, sat);" |
218 "} else {" | 218 "} else {" |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |