senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 The Android Open Source Project |
| 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 "SkLightingImageFilter.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkColorPriv.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 11 | #include "SkReadBuffer.h" |
| 12 | #include "SkWriteBuffer.h" |
| 13 | #include "SkReadBuffer.h" |
| 14 | #include "SkWriteBuffer.h" |
tomhudson@google.com | 300f562 | 2012-07-20 14:15:22 +0000 | [diff] [blame] | 15 | #include "SkTypes.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 16 | |
| 17 | #if SK_SUPPORT_GPU |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 18 | #include "effects/GrSingleTextureEffect.h" |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 19 | #include "gl/GrGLEffect.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 20 | #include "gl/builders/GrGLProgramBuilder.h" |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 21 | #include "GrEffect.h" |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 22 | #include "GrTBackendEffectFactory.h" |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 23 | |
| 24 | class GrGLDiffuseLightingEffect; |
| 25 | class GrGLSpecularLightingEffect; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 26 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 27 | // For brevity |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 28 | typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 29 | #endif |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 30 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | const SkScalar gOneThird = SkScalarInvert(SkIntToScalar(3)); |
| 34 | const SkScalar gTwoThirds = SkScalarDiv(SkIntToScalar(2), SkIntToScalar(3)); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 35 | const SkScalar gOneHalf = 0.5f; |
| 36 | const SkScalar gOneQuarter = 0.25f; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 37 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 38 | #if SK_SUPPORT_GPU |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 39 | void setUniformPoint3(const GrGLProgramDataManager& pdman, UniformHandle uni, const SkPoint3& point) { |
bsalomon@google.com | b9119a6 | 2012-07-25 17:55:26 +0000 | [diff] [blame] | 40 | GR_STATIC_ASSERT(sizeof(SkPoint3) == 3 * sizeof(GrGLfloat)); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 41 | pdman.set3fv(uni, 1, &point.fX); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 42 | } |
| 43 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 44 | void setUniformNormal3(const GrGLProgramDataManager& pdman, UniformHandle uni, const SkPoint3& point) { |
| 45 | setUniformPoint3(pdman, uni, SkPoint3(point.fX, point.fY, point.fZ)); |
senorblanco@chromium.org | f4770d7 | 2012-07-13 18:25:06 +0000 | [diff] [blame] | 46 | } |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 47 | #endif |
senorblanco@chromium.org | f4770d7 | 2012-07-13 18:25:06 +0000 | [diff] [blame] | 48 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 49 | // Shift matrix components to the left, as we advance pixels to the right. |
| 50 | inline void shiftMatrixLeft(int m[9]) { |
| 51 | m[0] = m[1]; |
| 52 | m[3] = m[4]; |
| 53 | m[6] = m[7]; |
| 54 | m[1] = m[2]; |
| 55 | m[4] = m[5]; |
| 56 | m[7] = m[8]; |
| 57 | } |
| 58 | |
| 59 | class DiffuseLightingType { |
| 60 | public: |
| 61 | DiffuseLightingType(SkScalar kd) |
| 62 | : fKD(kd) {} |
| 63 | SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight, const SkPoint3& lightColor) const { |
| 64 | SkScalar colorScale = SkScalarMul(fKD, normal.dot(surfaceTolight)); |
| 65 | colorScale = SkScalarClampMax(colorScale, SK_Scalar1); |
| 66 | SkPoint3 color(lightColor * colorScale); |
| 67 | return SkPackARGB32(255, |
senorblanco@chromium.org | b9c9597 | 2014-03-19 19:44:41 +0000 | [diff] [blame] | 68 | SkClampMax(SkScalarRoundToInt(color.fX), 255), |
| 69 | SkClampMax(SkScalarRoundToInt(color.fY), 255), |
| 70 | SkClampMax(SkScalarRoundToInt(color.fZ), 255)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 71 | } |
| 72 | private: |
| 73 | SkScalar fKD; |
| 74 | }; |
| 75 | |
| 76 | class SpecularLightingType { |
| 77 | public: |
| 78 | SpecularLightingType(SkScalar ks, SkScalar shininess) |
| 79 | : fKS(ks), fShininess(shininess) {} |
| 80 | SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight, const SkPoint3& lightColor) const { |
| 81 | SkPoint3 halfDir(surfaceTolight); |
| 82 | halfDir.fZ += SK_Scalar1; // eye position is always (0, 0, 1) |
| 83 | halfDir.normalize(); |
| 84 | SkScalar colorScale = SkScalarMul(fKS, |
| 85 | SkScalarPow(normal.dot(halfDir), fShininess)); |
| 86 | colorScale = SkScalarClampMax(colorScale, SK_Scalar1); |
| 87 | SkPoint3 color(lightColor * colorScale); |
senorblanco@chromium.org | b9c9597 | 2014-03-19 19:44:41 +0000 | [diff] [blame] | 88 | return SkPackARGB32(SkClampMax(SkScalarRoundToInt(color.maxComponent()), 255), |
| 89 | SkClampMax(SkScalarRoundToInt(color.fX), 255), |
| 90 | SkClampMax(SkScalarRoundToInt(color.fY), 255), |
| 91 | SkClampMax(SkScalarRoundToInt(color.fZ), 255)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 92 | } |
| 93 | private: |
| 94 | SkScalar fKS; |
| 95 | SkScalar fShininess; |
| 96 | }; |
| 97 | |
| 98 | inline SkScalar sobel(int a, int b, int c, int d, int e, int f, SkScalar scale) { |
| 99 | return SkScalarMul(SkIntToScalar(-a + b - 2 * c + 2 * d -e + f), scale); |
| 100 | } |
| 101 | |
| 102 | inline SkPoint3 pointToNormal(SkScalar x, SkScalar y, SkScalar surfaceScale) { |
| 103 | SkPoint3 vector(SkScalarMul(-x, surfaceScale), |
| 104 | SkScalarMul(-y, surfaceScale), |
| 105 | SK_Scalar1); |
| 106 | vector.normalize(); |
| 107 | return vector; |
| 108 | } |
| 109 | |
| 110 | inline SkPoint3 topLeftNormal(int m[9], SkScalar surfaceScale) { |
| 111 | return pointToNormal(sobel(0, 0, m[4], m[5], m[7], m[8], gTwoThirds), |
| 112 | sobel(0, 0, m[4], m[7], m[5], m[8], gTwoThirds), |
| 113 | surfaceScale); |
| 114 | } |
| 115 | |
| 116 | inline SkPoint3 topNormal(int m[9], SkScalar surfaceScale) { |
| 117 | return pointToNormal(sobel( 0, 0, m[3], m[5], m[6], m[8], gOneThird), |
| 118 | sobel(m[3], m[6], m[4], m[7], m[5], m[8], gOneHalf), |
| 119 | surfaceScale); |
| 120 | } |
| 121 | |
| 122 | inline SkPoint3 topRightNormal(int m[9], SkScalar surfaceScale) { |
| 123 | return pointToNormal(sobel( 0, 0, m[3], m[4], m[6], m[7], gTwoThirds), |
| 124 | sobel(m[3], m[6], m[4], m[7], 0, 0, gTwoThirds), |
| 125 | surfaceScale); |
| 126 | } |
| 127 | |
| 128 | inline SkPoint3 leftNormal(int m[9], SkScalar surfaceScale) { |
| 129 | return pointToNormal(sobel(m[1], m[2], m[4], m[5], m[7], m[8], gOneHalf), |
| 130 | sobel( 0, 0, m[1], m[7], m[2], m[8], gOneThird), |
| 131 | surfaceScale); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | inline SkPoint3 interiorNormal(int m[9], SkScalar surfaceScale) { |
| 136 | return pointToNormal(sobel(m[0], m[2], m[3], m[5], m[6], m[8], gOneQuarter), |
| 137 | sobel(m[0], m[6], m[1], m[7], m[2], m[8], gOneQuarter), |
| 138 | surfaceScale); |
| 139 | } |
| 140 | |
| 141 | inline SkPoint3 rightNormal(int m[9], SkScalar surfaceScale) { |
| 142 | return pointToNormal(sobel(m[0], m[1], m[3], m[4], m[6], m[7], gOneHalf), |
| 143 | sobel(m[0], m[6], m[1], m[7], 0, 0, gOneThird), |
| 144 | surfaceScale); |
| 145 | } |
| 146 | |
| 147 | inline SkPoint3 bottomLeftNormal(int m[9], SkScalar surfaceScale) { |
| 148 | return pointToNormal(sobel(m[1], m[2], m[4], m[5], 0, 0, gTwoThirds), |
| 149 | sobel( 0, 0, m[1], m[4], m[2], m[5], gTwoThirds), |
| 150 | surfaceScale); |
| 151 | } |
| 152 | |
| 153 | inline SkPoint3 bottomNormal(int m[9], SkScalar surfaceScale) { |
| 154 | return pointToNormal(sobel(m[0], m[2], m[3], m[5], 0, 0, gOneThird), |
| 155 | sobel(m[0], m[3], m[1], m[4], m[2], m[5], gOneHalf), |
| 156 | surfaceScale); |
| 157 | } |
| 158 | |
| 159 | inline SkPoint3 bottomRightNormal(int m[9], SkScalar surfaceScale) { |
| 160 | return pointToNormal(sobel(m[0], m[1], m[3], m[4], 0, 0, gTwoThirds), |
| 161 | sobel(m[0], m[3], m[1], m[4], 0, 0, gTwoThirds), |
| 162 | surfaceScale); |
| 163 | } |
| 164 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 165 | template <class LightingType, class LightType> void lightBitmap(const LightingType& lightingType, const SkLight* light, const SkBitmap& src, SkBitmap* dst, SkScalar surfaceScale, const SkIRect& bounds) { |
| 166 | SkASSERT(dst->width() == bounds.width() && dst->height() == bounds.height()); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 167 | const LightType* l = static_cast<const LightType*>(light); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 168 | int left = bounds.left(), right = bounds.right(); |
| 169 | int bottom = bounds.bottom(); |
| 170 | int y = bounds.top(); |
| 171 | SkPMColor* dptr = dst->getAddr32(0, 0); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 172 | { |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 173 | int x = left; |
| 174 | const SkPMColor* row1 = src.getAddr32(x, y); |
| 175 | const SkPMColor* row2 = src.getAddr32(x, y + 1); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 176 | int m[9]; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 177 | m[4] = SkGetPackedA32(*row1++); |
| 178 | m[5] = SkGetPackedA32(*row1++); |
| 179 | m[7] = SkGetPackedA32(*row2++); |
| 180 | m[8] = SkGetPackedA32(*row2++); |
| 181 | SkPoint3 surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale); |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 182 | *dptr++ = lightingType.light(topLeftNormal(m, surfaceScale), surfaceToLight, l->lightColor(surfaceToLight)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 183 | for (++x; x < right - 1; ++x) |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 184 | { |
| 185 | shiftMatrixLeft(m); |
| 186 | m[5] = SkGetPackedA32(*row1++); |
| 187 | m[8] = SkGetPackedA32(*row2++); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 188 | surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale); |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 189 | *dptr++ = lightingType.light(topNormal(m, surfaceScale), surfaceToLight, l->lightColor(surfaceToLight)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 190 | } |
| 191 | shiftMatrixLeft(m); |
| 192 | surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale); |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 193 | *dptr++ = lightingType.light(topRightNormal(m, surfaceScale), surfaceToLight, l->lightColor(surfaceToLight)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 194 | } |
| 195 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 196 | for (++y; y < bottom - 1; ++y) { |
| 197 | int x = left; |
| 198 | const SkPMColor* row0 = src.getAddr32(x, y - 1); |
| 199 | const SkPMColor* row1 = src.getAddr32(x, y); |
| 200 | const SkPMColor* row2 = src.getAddr32(x, y + 1); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 201 | int m[9]; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 202 | m[1] = SkGetPackedA32(*row0++); |
| 203 | m[2] = SkGetPackedA32(*row0++); |
| 204 | m[4] = SkGetPackedA32(*row1++); |
| 205 | m[5] = SkGetPackedA32(*row1++); |
| 206 | m[7] = SkGetPackedA32(*row2++); |
| 207 | m[8] = SkGetPackedA32(*row2++); |
| 208 | SkPoint3 surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale); |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 209 | *dptr++ = lightingType.light(leftNormal(m, surfaceScale), surfaceToLight, l->lightColor(surfaceToLight)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 210 | for (++x; x < right - 1; ++x) { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 211 | shiftMatrixLeft(m); |
| 212 | m[2] = SkGetPackedA32(*row0++); |
| 213 | m[5] = SkGetPackedA32(*row1++); |
| 214 | m[8] = SkGetPackedA32(*row2++); |
| 215 | surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale); |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 216 | *dptr++ = lightingType.light(interiorNormal(m, surfaceScale), surfaceToLight, l->lightColor(surfaceToLight)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 217 | } |
| 218 | shiftMatrixLeft(m); |
| 219 | surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale); |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 220 | *dptr++ = lightingType.light(rightNormal(m, surfaceScale), surfaceToLight, l->lightColor(surfaceToLight)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | { |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 224 | int x = left; |
| 225 | const SkPMColor* row0 = src.getAddr32(x, bottom - 2); |
| 226 | const SkPMColor* row1 = src.getAddr32(x, bottom - 1); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 227 | int m[9]; |
| 228 | m[1] = SkGetPackedA32(*row0++); |
| 229 | m[2] = SkGetPackedA32(*row0++); |
| 230 | m[4] = SkGetPackedA32(*row1++); |
| 231 | m[5] = SkGetPackedA32(*row1++); |
| 232 | SkPoint3 surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale); |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 233 | *dptr++ = lightingType.light(bottomLeftNormal(m, surfaceScale), surfaceToLight, l->lightColor(surfaceToLight)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 234 | for (++x; x < right - 1; ++x) |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 235 | { |
| 236 | shiftMatrixLeft(m); |
| 237 | m[2] = SkGetPackedA32(*row0++); |
| 238 | m[5] = SkGetPackedA32(*row1++); |
| 239 | surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale); |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 240 | *dptr++ = lightingType.light(bottomNormal(m, surfaceScale), surfaceToLight, l->lightColor(surfaceToLight)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 241 | } |
| 242 | shiftMatrixLeft(m); |
| 243 | surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale); |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 244 | *dptr++ = lightingType.light(bottomRightNormal(m, surfaceScale), surfaceToLight, l->lightColor(surfaceToLight)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 248 | SkPoint3 readPoint3(SkReadBuffer& buffer) { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 249 | SkPoint3 point; |
| 250 | point.fX = buffer.readScalar(); |
| 251 | point.fY = buffer.readScalar(); |
| 252 | point.fZ = buffer.readScalar(); |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 253 | buffer.validate(SkScalarIsFinite(point.fX) && |
| 254 | SkScalarIsFinite(point.fY) && |
| 255 | SkScalarIsFinite(point.fZ)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 256 | return point; |
| 257 | }; |
| 258 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 259 | void writePoint3(const SkPoint3& point, SkWriteBuffer& buffer) { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 260 | buffer.writeScalar(point.fX); |
| 261 | buffer.writeScalar(point.fY); |
| 262 | buffer.writeScalar(point.fZ); |
| 263 | }; |
| 264 | |
| 265 | class SkDiffuseLightingImageFilter : public SkLightingImageFilter { |
| 266 | public: |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 267 | static SkImageFilter* Create(SkLight* light, SkScalar surfaceScale, SkScalar kd, SkImageFilter*, |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 268 | const CropRect*, uint32_t uniqueID = 0); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 269 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 270 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDiffuseLightingImageFilter) |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 271 | SkScalar kd() const { return fKD; } |
| 272 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 273 | protected: |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 274 | SkDiffuseLightingImageFilter(SkLight* light, SkScalar surfaceScale, |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 275 | SkScalar kd, SkImageFilter* input, const CropRect* cropRect, |
| 276 | uint32_t uniqueID); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 277 | #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 278 | explicit SkDiffuseLightingImageFilter(SkReadBuffer& buffer); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 279 | #endif |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 280 | virtual void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE; |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 281 | virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 282 | SkBitmap* result, SkIPoint* offset) const SK_OVERRIDE; |
senorblanco@chromium.org | 1aa6872 | 2013-10-17 19:35:09 +0000 | [diff] [blame] | 283 | #if SK_SUPPORT_GPU |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 284 | virtual bool asNewEffect(GrEffect** effect, GrTexture*, const SkMatrix& matrix, |
| 285 | const SkIRect& bounds) const SK_OVERRIDE; |
senorblanco@chromium.org | 1aa6872 | 2013-10-17 19:35:09 +0000 | [diff] [blame] | 286 | #endif |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 287 | |
| 288 | private: |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 289 | friend class SkLightingImageFilter; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 290 | typedef SkLightingImageFilter INHERITED; |
| 291 | SkScalar fKD; |
| 292 | }; |
| 293 | |
| 294 | class SkSpecularLightingImageFilter : public SkLightingImageFilter { |
| 295 | public: |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 296 | static SkImageFilter* Create(SkLight* light, SkScalar surfaceScale, |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 297 | SkScalar ks, SkScalar shininess, SkImageFilter*, const CropRect*, uint32_t uniqueID = 0); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 298 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 299 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSpecularLightingImageFilter) |
| 300 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 301 | SkScalar ks() const { return fKS; } |
| 302 | SkScalar shininess() const { return fShininess; } |
| 303 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 304 | protected: |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 305 | SkSpecularLightingImageFilter(SkLight* light, SkScalar surfaceScale, SkScalar ks, |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 306 | SkScalar shininess, SkImageFilter* input, const CropRect*, |
| 307 | uint32_t uniqueID); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 308 | #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 309 | explicit SkSpecularLightingImageFilter(SkReadBuffer& buffer); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 310 | #endif |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 311 | virtual void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE; |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 312 | virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 313 | SkBitmap* result, SkIPoint* offset) const SK_OVERRIDE; |
senorblanco@chromium.org | 1aa6872 | 2013-10-17 19:35:09 +0000 | [diff] [blame] | 314 | #if SK_SUPPORT_GPU |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 315 | virtual bool asNewEffect(GrEffect** effect, GrTexture*, const SkMatrix& matrix, |
| 316 | const SkIRect& bounds) const SK_OVERRIDE; |
senorblanco@chromium.org | 1aa6872 | 2013-10-17 19:35:09 +0000 | [diff] [blame] | 317 | #endif |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 318 | |
| 319 | private: |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 320 | SkScalar fKS; |
| 321 | SkScalar fShininess; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 322 | friend class SkLightingImageFilter; |
| 323 | typedef SkLightingImageFilter INHERITED; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 324 | }; |
| 325 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 326 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 327 | |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 328 | class GrLightingEffect : public GrSingleTextureEffect { |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 329 | public: |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 330 | GrLightingEffect(GrTexture* texture, const SkLight* light, SkScalar surfaceScale, const SkMatrix& matrix); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 331 | virtual ~GrLightingEffect(); |
| 332 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 333 | const SkLight* light() const { return fLight; } |
| 334 | SkScalar surfaceScale() const { return fSurfaceScale; } |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 335 | const SkMatrix& filterMatrix() const { return fFilterMatrix; } |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 336 | |
| 337 | virtual void getConstantColorComponents(GrColor* color, |
| 338 | uint32_t* validFlags) const SK_OVERRIDE { |
| 339 | // lighting shaders are complicated. We just throw up our hands. |
| 340 | *validFlags = 0; |
| 341 | } |
| 342 | |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 343 | protected: |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 344 | virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 345 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 346 | private: |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 347 | typedef GrSingleTextureEffect INHERITED; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 348 | const SkLight* fLight; |
| 349 | SkScalar fSurfaceScale; |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 350 | SkMatrix fFilterMatrix; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 351 | }; |
| 352 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 353 | class GrDiffuseLightingEffect : public GrLightingEffect { |
| 354 | public: |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 355 | static GrEffect* Create(GrTexture* texture, |
| 356 | const SkLight* light, |
| 357 | SkScalar surfaceScale, |
| 358 | const SkMatrix& matrix, |
| 359 | SkScalar kd) { |
bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 360 | return SkNEW_ARGS(GrDiffuseLightingEffect, (texture, |
| 361 | light, |
| 362 | surfaceScale, |
| 363 | matrix, |
| 364 | kd)); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 365 | } |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 366 | |
| 367 | static const char* Name() { return "DiffuseLighting"; } |
| 368 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 369 | typedef GrGLDiffuseLightingEffect GLEffect; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 370 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 371 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 372 | SkScalar kd() const { return fKD; } |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 373 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 374 | private: |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 375 | virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 376 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 377 | GrDiffuseLightingEffect(GrTexture* texture, |
| 378 | const SkLight* light, |
| 379 | SkScalar surfaceScale, |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 380 | const SkMatrix& matrix, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 381 | SkScalar kd); |
| 382 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 383 | GR_DECLARE_EFFECT_TEST; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 384 | typedef GrLightingEffect INHERITED; |
| 385 | SkScalar fKD; |
| 386 | }; |
| 387 | |
| 388 | class GrSpecularLightingEffect : public GrLightingEffect { |
| 389 | public: |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 390 | static GrEffect* Create(GrTexture* texture, |
| 391 | const SkLight* light, |
| 392 | SkScalar surfaceScale, |
| 393 | const SkMatrix& matrix, |
| 394 | SkScalar ks, |
| 395 | SkScalar shininess) { |
bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 396 | return SkNEW_ARGS(GrSpecularLightingEffect, (texture, |
| 397 | light, |
| 398 | surfaceScale, |
| 399 | matrix, |
| 400 | ks, |
| 401 | shininess)); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 402 | } |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 403 | static const char* Name() { return "SpecularLighting"; } |
| 404 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 405 | typedef GrGLSpecularLightingEffect GLEffect; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 406 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 407 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 408 | SkScalar ks() const { return fKS; } |
| 409 | SkScalar shininess() const { return fShininess; } |
| 410 | |
| 411 | private: |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 412 | virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 413 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 414 | GrSpecularLightingEffect(GrTexture* texture, |
| 415 | const SkLight* light, |
| 416 | SkScalar surfaceScale, |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 417 | const SkMatrix& matrix, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 418 | SkScalar ks, |
| 419 | SkScalar shininess); |
| 420 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 421 | GR_DECLARE_EFFECT_TEST; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 422 | typedef GrLightingEffect INHERITED; |
| 423 | SkScalar fKS; |
| 424 | SkScalar fShininess; |
| 425 | }; |
| 426 | |
| 427 | /////////////////////////////////////////////////////////////////////////////// |
| 428 | |
| 429 | class GrGLLight { |
| 430 | public: |
senorblanco@chromium.org | 6730cbb | 2012-07-11 16:35:00 +0000 | [diff] [blame] | 431 | virtual ~GrGLLight() {} |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 432 | |
| 433 | /** |
| 434 | * This is called by GrGLLightingEffect::emitCode() before either of the two virtual functions |
| 435 | * below. It adds a vec3f uniform visible in the FS that represents the constant light color. |
| 436 | */ |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 437 | void emitLightColorUniform(GrGLProgramBuilder*); |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 438 | |
skia.committer@gmail.com | e862d16 | 2012-10-31 02:01:18 +0000 | [diff] [blame] | 439 | /** |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 440 | * These two functions are called from GrGLLightingEffect's emitCode() function. |
| 441 | * emitSurfaceToLight places an expression in param out that is the vector from the surface to |
| 442 | * the light. The expression will be used in the FS. emitLightColor writes an expression into |
| 443 | * the FS that is the color of the light. Either function may add functions and/or uniforms to |
| 444 | * the FS. The default of emitLightColor appends the name of the constant light color uniform |
| 445 | * and so this function only needs to be overridden if the light color varies spatially. |
| 446 | */ |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 447 | virtual void emitSurfaceToLight(GrGLProgramBuilder*, const char* z) = 0; |
| 448 | virtual void emitLightColor(GrGLProgramBuilder*, const char *surfaceToLight); |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 449 | |
| 450 | // This is called from GrGLLightingEffect's setData(). Subclasses of GrGLLight must call |
| 451 | // INHERITED::setData(). |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 452 | virtual void setData(const GrGLProgramDataManager&, |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 453 | const SkLight* light) const; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 454 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 455 | protected: |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 456 | /** |
| 457 | * Gets the constant light color uniform. Subclasses can use this in their emitLightColor |
| 458 | * function. |
| 459 | */ |
| 460 | UniformHandle lightColorUni() const { return fColorUni; } |
| 461 | |
| 462 | private: |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 463 | UniformHandle fColorUni; |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 464 | |
| 465 | typedef SkRefCnt INHERITED; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 466 | }; |
| 467 | |
| 468 | /////////////////////////////////////////////////////////////////////////////// |
| 469 | |
| 470 | class GrGLDistantLight : public GrGLLight { |
| 471 | public: |
senorblanco@chromium.org | 6730cbb | 2012-07-11 16:35:00 +0000 | [diff] [blame] | 472 | virtual ~GrGLDistantLight() {} |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 473 | virtual void setData(const GrGLProgramDataManager&, |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 474 | const SkLight* light) const SK_OVERRIDE; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 475 | virtual void emitSurfaceToLight(GrGLProgramBuilder*, const char* z) SK_OVERRIDE; |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 476 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 477 | private: |
| 478 | typedef GrGLLight INHERITED; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 479 | UniformHandle fDirectionUni; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 480 | }; |
| 481 | |
| 482 | /////////////////////////////////////////////////////////////////////////////// |
| 483 | |
| 484 | class GrGLPointLight : public GrGLLight { |
| 485 | public: |
senorblanco@chromium.org | 6730cbb | 2012-07-11 16:35:00 +0000 | [diff] [blame] | 486 | virtual ~GrGLPointLight() {} |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 487 | virtual void setData(const GrGLProgramDataManager&, |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 488 | const SkLight* light) const SK_OVERRIDE; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 489 | virtual void emitSurfaceToLight(GrGLProgramBuilder*, const char* z) SK_OVERRIDE; |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 490 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 491 | private: |
| 492 | typedef GrGLLight INHERITED; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 493 | UniformHandle fLocationUni; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 494 | }; |
| 495 | |
| 496 | /////////////////////////////////////////////////////////////////////////////// |
| 497 | |
| 498 | class GrGLSpotLight : public GrGLLight { |
| 499 | public: |
senorblanco@chromium.org | 6730cbb | 2012-07-11 16:35:00 +0000 | [diff] [blame] | 500 | virtual ~GrGLSpotLight() {} |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 501 | virtual void setData(const GrGLProgramDataManager&, |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 502 | const SkLight* light) const SK_OVERRIDE; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 503 | virtual void emitSurfaceToLight(GrGLProgramBuilder*, const char* z) SK_OVERRIDE; |
| 504 | virtual void emitLightColor(GrGLProgramBuilder*, const char *surfaceToLight) SK_OVERRIDE; |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 505 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 506 | private: |
| 507 | typedef GrGLLight INHERITED; |
| 508 | |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 509 | SkString fLightColorFunc; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 510 | UniformHandle fLocationUni; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 511 | UniformHandle fExponentUni; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 512 | UniformHandle fCosOuterConeAngleUni; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 513 | UniformHandle fCosInnerConeAngleUni; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 514 | UniformHandle fConeScaleUni; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 515 | UniformHandle fSUni; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 516 | }; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 517 | #else |
| 518 | |
| 519 | class GrGLLight; |
| 520 | |
| 521 | #endif |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 522 | |
| 523 | }; |
| 524 | |
| 525 | /////////////////////////////////////////////////////////////////////////////// |
| 526 | |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 527 | class SkLight : public SkRefCnt { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 528 | public: |
robertphillips@google.com | 0456e0b | 2012-06-27 14:03:26 +0000 | [diff] [blame] | 529 | SK_DECLARE_INST_COUNT(SkLight) |
| 530 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 531 | enum LightType { |
| 532 | kDistant_LightType, |
| 533 | kPoint_LightType, |
| 534 | kSpot_LightType, |
| 535 | }; |
| 536 | virtual LightType type() const = 0; |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 537 | const SkPoint3& color() const { return fColor; } |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 538 | virtual GrGLLight* createGLLight() const = 0; |
| 539 | virtual bool isEqual(const SkLight& other) const { |
| 540 | return fColor == other.fColor; |
| 541 | } |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 542 | // Called to know whether the generated GrGLLight will require access to the fragment position. |
| 543 | virtual bool requiresFragmentPosition() const = 0; |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 544 | virtual SkLight* transform(const SkMatrix& matrix) const = 0; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 545 | |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 546 | // Defined below SkLight's subclasses. |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 547 | void flattenLight(SkWriteBuffer& buffer) const; |
| 548 | static SkLight* UnflattenLight(SkReadBuffer& buffer); |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 549 | |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 550 | protected: |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 551 | SkLight(SkColor color) |
| 552 | : fColor(SkIntToScalar(SkColorGetR(color)), |
| 553 | SkIntToScalar(SkColorGetG(color)), |
| 554 | SkIntToScalar(SkColorGetB(color))) {} |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 555 | SkLight(const SkPoint3& color) |
| 556 | : fColor(color) {} |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 557 | SkLight(SkReadBuffer& buffer) { |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 558 | fColor = readPoint3(buffer); |
| 559 | } |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 560 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 561 | virtual void onFlattenLight(SkWriteBuffer& buffer) const = 0; |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 562 | |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 563 | |
| 564 | private: |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 565 | typedef SkRefCnt INHERITED; |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 566 | SkPoint3 fColor; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 567 | }; |
| 568 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 569 | /////////////////////////////////////////////////////////////////////////////// |
| 570 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 571 | class SkDistantLight : public SkLight { |
| 572 | public: |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 573 | SkDistantLight(const SkPoint3& direction, SkColor color) |
| 574 | : INHERITED(color), fDirection(direction) { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 575 | } |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 576 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 577 | SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const { |
| 578 | return fDirection; |
| 579 | }; |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 580 | SkPoint3 lightColor(const SkPoint3&) const { return color(); } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 581 | virtual LightType type() const { return kDistant_LightType; } |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 582 | const SkPoint3& direction() const { return fDirection; } |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 583 | virtual GrGLLight* createGLLight() const SK_OVERRIDE { |
| 584 | #if SK_SUPPORT_GPU |
| 585 | return SkNEW(GrGLDistantLight); |
| 586 | #else |
| 587 | SkDEBUGFAIL("Should not call in GPU-less build"); |
| 588 | return NULL; |
| 589 | #endif |
| 590 | } |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 591 | virtual bool requiresFragmentPosition() const SK_OVERRIDE { return false; } |
| 592 | |
senorblanco@chromium.org | 6730cbb | 2012-07-11 16:35:00 +0000 | [diff] [blame] | 593 | virtual bool isEqual(const SkLight& other) const SK_OVERRIDE { |
| 594 | if (other.type() != kDistant_LightType) { |
| 595 | return false; |
| 596 | } |
| 597 | |
| 598 | const SkDistantLight& o = static_cast<const SkDistantLight&>(other); |
| 599 | return INHERITED::isEqual(other) && |
| 600 | fDirection == o.fDirection; |
| 601 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 602 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 603 | SkDistantLight(SkReadBuffer& buffer) : INHERITED(buffer) { |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 604 | fDirection = readPoint3(buffer); |
| 605 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 606 | |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 607 | protected: |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 608 | SkDistantLight(const SkPoint3& direction, const SkPoint3& color) |
| 609 | : INHERITED(color), fDirection(direction) { |
| 610 | } |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 611 | virtual SkLight* transform(const SkMatrix& matrix) const { |
| 612 | return new SkDistantLight(direction(), color()); |
| 613 | } |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 614 | virtual void onFlattenLight(SkWriteBuffer& buffer) const SK_OVERRIDE { |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 615 | writePoint3(fDirection, buffer); |
| 616 | } |
| 617 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 618 | private: |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 619 | typedef SkLight INHERITED; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 620 | SkPoint3 fDirection; |
| 621 | }; |
| 622 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 623 | /////////////////////////////////////////////////////////////////////////////// |
| 624 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 625 | class SkPointLight : public SkLight { |
| 626 | public: |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 627 | SkPointLight(const SkPoint3& location, SkColor color) |
| 628 | : INHERITED(color), fLocation(location) {} |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 629 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 630 | SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const { |
| 631 | SkPoint3 direction(fLocation.fX - SkIntToScalar(x), |
| 632 | fLocation.fY - SkIntToScalar(y), |
| 633 | fLocation.fZ - SkScalarMul(SkIntToScalar(z), surfaceScale)); |
| 634 | direction.normalize(); |
| 635 | return direction; |
| 636 | }; |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 637 | SkPoint3 lightColor(const SkPoint3&) const { return color(); } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 638 | virtual LightType type() const { return kPoint_LightType; } |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 639 | const SkPoint3& location() const { return fLocation; } |
| 640 | virtual GrGLLight* createGLLight() const SK_OVERRIDE { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 641 | #if SK_SUPPORT_GPU |
tomhudson@google.com | 300f562 | 2012-07-20 14:15:22 +0000 | [diff] [blame] | 642 | return SkNEW(GrGLPointLight); |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 643 | #else |
| 644 | SkDEBUGFAIL("Should not call in GPU-less build"); |
| 645 | return NULL; |
| 646 | #endif |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 647 | } |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 648 | virtual bool requiresFragmentPosition() const SK_OVERRIDE { return true; } |
senorblanco@chromium.org | 6730cbb | 2012-07-11 16:35:00 +0000 | [diff] [blame] | 649 | virtual bool isEqual(const SkLight& other) const SK_OVERRIDE { |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 650 | if (other.type() != kPoint_LightType) { |
| 651 | return false; |
| 652 | } |
| 653 | const SkPointLight& o = static_cast<const SkPointLight&>(other); |
| 654 | return INHERITED::isEqual(other) && |
| 655 | fLocation == o.fLocation; |
| 656 | } |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 657 | virtual SkLight* transform(const SkMatrix& matrix) const { |
| 658 | SkPoint location2 = SkPoint::Make(fLocation.fX, fLocation.fY); |
| 659 | matrix.mapPoints(&location2, 1); |
commit-bot@chromium.org | 1037d92 | 2014-03-13 19:45:41 +0000 | [diff] [blame] | 660 | // Use X scale and Y scale on Z and average the result |
| 661 | SkPoint locationZ = SkPoint::Make(fLocation.fZ, fLocation.fZ); |
| 662 | matrix.mapVectors(&locationZ, 1); |
| 663 | SkPoint3 location(location2.fX, location2.fY, SkScalarAve(locationZ.fX, locationZ.fY)); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 664 | return new SkPointLight(location, color()); |
| 665 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 666 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 667 | SkPointLight(SkReadBuffer& buffer) : INHERITED(buffer) { |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 668 | fLocation = readPoint3(buffer); |
| 669 | } |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 670 | |
| 671 | protected: |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 672 | SkPointLight(const SkPoint3& location, const SkPoint3& color) |
| 673 | : INHERITED(color), fLocation(location) {} |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 674 | virtual void onFlattenLight(SkWriteBuffer& buffer) const SK_OVERRIDE { |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 675 | writePoint3(fLocation, buffer); |
| 676 | } |
| 677 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 678 | private: |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 679 | typedef SkLight INHERITED; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 680 | SkPoint3 fLocation; |
| 681 | }; |
| 682 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 683 | /////////////////////////////////////////////////////////////////////////////// |
| 684 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 685 | class SkSpotLight : public SkLight { |
| 686 | public: |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 687 | SkSpotLight(const SkPoint3& location, const SkPoint3& target, SkScalar specularExponent, SkScalar cutoffAngle, SkColor color) |
| 688 | : INHERITED(color), |
| 689 | fLocation(location), |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 690 | fTarget(target), |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 691 | fSpecularExponent(SkScalarPin(specularExponent, kSpecularExponentMin, kSpecularExponentMax)) |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 692 | { |
| 693 | fS = target - location; |
| 694 | fS.normalize(); |
| 695 | fCosOuterConeAngle = SkScalarCos(SkDegreesToRadians(cutoffAngle)); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 696 | const SkScalar antiAliasThreshold = 0.016f; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 697 | fCosInnerConeAngle = fCosOuterConeAngle + antiAliasThreshold; |
| 698 | fConeScale = SkScalarInvert(antiAliasThreshold); |
| 699 | } |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 700 | |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 701 | virtual SkLight* transform(const SkMatrix& matrix) const { |
| 702 | SkPoint location2 = SkPoint::Make(fLocation.fX, fLocation.fY); |
| 703 | matrix.mapPoints(&location2, 1); |
commit-bot@chromium.org | 1037d92 | 2014-03-13 19:45:41 +0000 | [diff] [blame] | 704 | // Use X scale and Y scale on Z and average the result |
| 705 | SkPoint locationZ = SkPoint::Make(fLocation.fZ, fLocation.fZ); |
| 706 | matrix.mapVectors(&locationZ, 1); |
| 707 | SkPoint3 location(location2.fX, location2.fY, SkScalarAve(locationZ.fX, locationZ.fY)); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 708 | SkPoint target2 = SkPoint::Make(fTarget.fX, fTarget.fY); |
| 709 | matrix.mapPoints(&target2, 1); |
commit-bot@chromium.org | 1037d92 | 2014-03-13 19:45:41 +0000 | [diff] [blame] | 710 | SkPoint targetZ = SkPoint::Make(fTarget.fZ, fTarget.fZ); |
| 711 | matrix.mapVectors(&targetZ, 1); |
| 712 | SkPoint3 target(target2.fX, target2.fY, SkScalarAve(targetZ.fX, targetZ.fY)); |
| 713 | SkPoint3 s = target - location; |
| 714 | s.normalize(); |
| 715 | return new SkSpotLight(location, target, fSpecularExponent, fCosOuterConeAngle, fCosInnerConeAngle, fConeScale, s, color()); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 716 | } |
| 717 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 718 | SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const { |
| 719 | SkPoint3 direction(fLocation.fX - SkIntToScalar(x), |
| 720 | fLocation.fY - SkIntToScalar(y), |
| 721 | fLocation.fZ - SkScalarMul(SkIntToScalar(z), surfaceScale)); |
| 722 | direction.normalize(); |
| 723 | return direction; |
| 724 | }; |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 725 | SkPoint3 lightColor(const SkPoint3& surfaceToLight) const { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 726 | SkScalar cosAngle = -surfaceToLight.dot(fS); |
| 727 | if (cosAngle < fCosOuterConeAngle) { |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 728 | return SkPoint3(0, 0, 0); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 729 | } |
| 730 | SkScalar scale = SkScalarPow(cosAngle, fSpecularExponent); |
| 731 | if (cosAngle < fCosInnerConeAngle) { |
| 732 | scale = SkScalarMul(scale, cosAngle - fCosOuterConeAngle); |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 733 | return color() * SkScalarMul(scale, fConeScale); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 734 | } |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 735 | return color() * scale; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 736 | } |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 737 | virtual GrGLLight* createGLLight() const SK_OVERRIDE { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 738 | #if SK_SUPPORT_GPU |
tomhudson@google.com | 300f562 | 2012-07-20 14:15:22 +0000 | [diff] [blame] | 739 | return SkNEW(GrGLSpotLight); |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 740 | #else |
| 741 | SkDEBUGFAIL("Should not call in GPU-less build"); |
| 742 | return NULL; |
| 743 | #endif |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 744 | } |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 745 | virtual bool requiresFragmentPosition() const SK_OVERRIDE { return true; } |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 746 | virtual LightType type() const { return kSpot_LightType; } |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 747 | const SkPoint3& location() const { return fLocation; } |
| 748 | const SkPoint3& target() const { return fTarget; } |
| 749 | SkScalar specularExponent() const { return fSpecularExponent; } |
| 750 | SkScalar cosInnerConeAngle() const { return fCosInnerConeAngle; } |
| 751 | SkScalar cosOuterConeAngle() const { return fCosOuterConeAngle; } |
| 752 | SkScalar coneScale() const { return fConeScale; } |
senorblanco@chromium.org | eb31184 | 2012-07-11 20:49:26 +0000 | [diff] [blame] | 753 | const SkPoint3& s() const { return fS; } |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 754 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 755 | SkSpotLight(SkReadBuffer& buffer) : INHERITED(buffer) { |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 756 | fLocation = readPoint3(buffer); |
| 757 | fTarget = readPoint3(buffer); |
| 758 | fSpecularExponent = buffer.readScalar(); |
| 759 | fCosOuterConeAngle = buffer.readScalar(); |
| 760 | fCosInnerConeAngle = buffer.readScalar(); |
| 761 | fConeScale = buffer.readScalar(); |
| 762 | fS = readPoint3(buffer); |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 763 | buffer.validate(SkScalarIsFinite(fSpecularExponent) && |
| 764 | SkScalarIsFinite(fCosOuterConeAngle) && |
| 765 | SkScalarIsFinite(fCosInnerConeAngle) && |
| 766 | SkScalarIsFinite(fConeScale)); |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 767 | } |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 768 | protected: |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 769 | SkSpotLight(const SkPoint3& location, const SkPoint3& target, SkScalar specularExponent, SkScalar cosOuterConeAngle, SkScalar cosInnerConeAngle, SkScalar coneScale, const SkPoint3& s, const SkPoint3& color) |
| 770 | : INHERITED(color), |
| 771 | fLocation(location), |
| 772 | fTarget(target), |
| 773 | fSpecularExponent(specularExponent), |
| 774 | fCosOuterConeAngle(cosOuterConeAngle), |
| 775 | fCosInnerConeAngle(cosInnerConeAngle), |
| 776 | fConeScale(coneScale), |
| 777 | fS(s) |
| 778 | { |
| 779 | } |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 780 | virtual void onFlattenLight(SkWriteBuffer& buffer) const SK_OVERRIDE { |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 781 | writePoint3(fLocation, buffer); |
| 782 | writePoint3(fTarget, buffer); |
| 783 | buffer.writeScalar(fSpecularExponent); |
| 784 | buffer.writeScalar(fCosOuterConeAngle); |
| 785 | buffer.writeScalar(fCosInnerConeAngle); |
| 786 | buffer.writeScalar(fConeScale); |
| 787 | writePoint3(fS, buffer); |
| 788 | } |
| 789 | |
senorblanco@chromium.org | bd9fad6 | 2012-07-11 16:25:37 +0000 | [diff] [blame] | 790 | virtual bool isEqual(const SkLight& other) const SK_OVERRIDE { |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 791 | if (other.type() != kSpot_LightType) { |
| 792 | return false; |
| 793 | } |
| 794 | |
| 795 | const SkSpotLight& o = static_cast<const SkSpotLight&>(other); |
| 796 | return INHERITED::isEqual(other) && |
| 797 | fLocation == o.fLocation && |
| 798 | fTarget == o.fTarget && |
| 799 | fSpecularExponent == o.fSpecularExponent && |
| 800 | fCosOuterConeAngle == o.fCosOuterConeAngle; |
| 801 | } |
| 802 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 803 | private: |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 804 | static const SkScalar kSpecularExponentMin; |
| 805 | static const SkScalar kSpecularExponentMax; |
| 806 | |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 807 | typedef SkLight INHERITED; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 808 | SkPoint3 fLocation; |
| 809 | SkPoint3 fTarget; |
| 810 | SkScalar fSpecularExponent; |
| 811 | SkScalar fCosOuterConeAngle; |
| 812 | SkScalar fCosInnerConeAngle; |
| 813 | SkScalar fConeScale; |
| 814 | SkPoint3 fS; |
| 815 | }; |
| 816 | |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 817 | // According to the spec, the specular term should be in the range [1, 128] : |
| 818 | // http://www.w3.org/TR/SVG/filters.html#feSpecularLightingSpecularExponentAttribute |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 819 | const SkScalar SkSpotLight::kSpecularExponentMin = 1.0f; |
| 820 | const SkScalar SkSpotLight::kSpecularExponentMax = 128.0f; |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 821 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 822 | /////////////////////////////////////////////////////////////////////////////// |
| 823 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 824 | void SkLight::flattenLight(SkWriteBuffer& buffer) const { |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 825 | // Write type first, then baseclass, then subclass. |
| 826 | buffer.writeInt(this->type()); |
| 827 | writePoint3(fColor, buffer); |
| 828 | this->onFlattenLight(buffer); |
| 829 | } |
| 830 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 831 | /*static*/ SkLight* SkLight::UnflattenLight(SkReadBuffer& buffer) { |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 832 | // Read type first. |
| 833 | const SkLight::LightType type = (SkLight::LightType)buffer.readInt(); |
| 834 | switch (type) { |
| 835 | // Each of these constructors must first call SkLight's, so we'll read the baseclass |
| 836 | // then subclass, same order as flattenLight. |
| 837 | case SkLight::kDistant_LightType: return SkNEW_ARGS(SkDistantLight, (buffer)); |
| 838 | case SkLight::kPoint_LightType: return SkNEW_ARGS(SkPointLight, (buffer)); |
| 839 | case SkLight::kSpot_LightType: return SkNEW_ARGS(SkSpotLight, (buffer)); |
| 840 | default: |
| 841 | SkDEBUGFAIL("Unknown LightType."); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 842 | buffer.validate(false); |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 843 | return NULL; |
| 844 | } |
| 845 | } |
| 846 | /////////////////////////////////////////////////////////////////////////////// |
| 847 | |
senorblanco | 9ea3d57 | 2014-07-08 09:16:22 -0700 | [diff] [blame] | 848 | SkLightingImageFilter::SkLightingImageFilter(SkLight* light, SkScalar surfaceScale, |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 849 | SkImageFilter* input, const CropRect* cropRect, |
| 850 | uint32_t uniqueID) |
| 851 | : INHERITED(1, &input, cropRect, uniqueID) |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 852 | , fLight(SkRef(light)) |
| 853 | , fSurfaceScale(surfaceScale / 255) |
| 854 | {} |
| 855 | |
| 856 | SkImageFilter* SkLightingImageFilter::CreateDistantLitDiffuse(const SkPoint3& direction, |
| 857 | SkColor lightColor, |
| 858 | SkScalar surfaceScale, |
| 859 | SkScalar kd, |
| 860 | SkImageFilter* input, |
| 861 | const CropRect* cropRect) { |
| 862 | SkAutoTUnref<SkLight> light(SkNEW_ARGS(SkDistantLight, (direction, lightColor))); |
| 863 | return SkDiffuseLightingImageFilter::Create(light, surfaceScale, kd, input, cropRect); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 864 | } |
| 865 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 866 | SkImageFilter* SkLightingImageFilter::CreatePointLitDiffuse(const SkPoint3& location, |
| 867 | SkColor lightColor, |
| 868 | SkScalar surfaceScale, |
| 869 | SkScalar kd, |
| 870 | SkImageFilter* input, |
| 871 | const CropRect* cropRect) { |
| 872 | SkAutoTUnref<SkLight> light(SkNEW_ARGS(SkPointLight, (location, lightColor))); |
| 873 | return SkDiffuseLightingImageFilter::Create(light, surfaceScale, kd, input, cropRect); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 874 | } |
| 875 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 876 | SkImageFilter* SkLightingImageFilter::CreateSpotLitDiffuse(const SkPoint3& location, |
| 877 | const SkPoint3& target, |
| 878 | SkScalar specularExponent, |
| 879 | SkScalar cutoffAngle, |
| 880 | SkColor lightColor, |
| 881 | SkScalar surfaceScale, |
| 882 | SkScalar kd, |
| 883 | SkImageFilter* input, |
| 884 | const CropRect* cropRect) { |
| 885 | SkAutoTUnref<SkLight> light(SkNEW_ARGS(SkSpotLight, (location, target, specularExponent, |
| 886 | cutoffAngle, lightColor))); |
| 887 | return SkDiffuseLightingImageFilter::Create(light, surfaceScale, kd, input, cropRect); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 888 | } |
| 889 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 890 | SkImageFilter* SkLightingImageFilter::CreateDistantLitSpecular(const SkPoint3& direction, |
| 891 | SkColor lightColor, |
| 892 | SkScalar surfaceScale, |
| 893 | SkScalar ks, |
| 894 | SkScalar shine, |
| 895 | SkImageFilter* input, |
| 896 | const CropRect* cropRect) { |
| 897 | SkAutoTUnref<SkLight> light(SkNEW_ARGS(SkDistantLight, (direction, lightColor))); |
| 898 | return SkSpecularLightingImageFilter::Create(light, surfaceScale, ks, shine, input, cropRect); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 899 | } |
| 900 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 901 | SkImageFilter* SkLightingImageFilter::CreatePointLitSpecular(const SkPoint3& location, |
| 902 | SkColor lightColor, |
| 903 | SkScalar surfaceScale, |
| 904 | SkScalar ks, |
| 905 | SkScalar shine, |
| 906 | SkImageFilter* input, |
| 907 | const CropRect* cropRect) { |
| 908 | SkAutoTUnref<SkLight> light(SkNEW_ARGS(SkPointLight, (location, lightColor))); |
| 909 | return SkSpecularLightingImageFilter::Create(light, surfaceScale, ks, shine, input, cropRect); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 910 | } |
| 911 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 912 | SkImageFilter* SkLightingImageFilter::CreateSpotLitSpecular(const SkPoint3& location, |
| 913 | const SkPoint3& target, |
| 914 | SkScalar specularExponent, |
| 915 | SkScalar cutoffAngle, |
| 916 | SkColor lightColor, |
| 917 | SkScalar surfaceScale, |
| 918 | SkScalar ks, |
| 919 | SkScalar shine, |
| 920 | SkImageFilter* input, |
| 921 | const CropRect* cropRect) { |
| 922 | SkAutoTUnref<SkLight> light(SkNEW_ARGS(SkSpotLight, (location, target, specularExponent, |
| 923 | cutoffAngle, lightColor))); |
| 924 | return SkSpecularLightingImageFilter::Create(light, surfaceScale, ks, shine, input, cropRect); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 925 | } |
| 926 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 927 | SkLightingImageFilter::~SkLightingImageFilter() {} |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 928 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 929 | #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 930 | SkLightingImageFilter::SkLightingImageFilter(SkReadBuffer& buffer) |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 931 | : INHERITED(1, buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 932 | fLight.reset(SkLight::UnflattenLight(buffer)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 933 | fSurfaceScale = buffer.readScalar(); |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 934 | buffer.validate(SkScalarIsFinite(fSurfaceScale)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 935 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 936 | #endif |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 937 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 938 | void SkLightingImageFilter::flatten(SkWriteBuffer& buffer) const { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 939 | this->INHERITED::flatten(buffer); |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 940 | fLight->flattenLight(buffer); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 941 | buffer.writeScalar(fSurfaceScale * 255); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 942 | } |
| 943 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 944 | /////////////////////////////////////////////////////////////////////////////// |
| 945 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 946 | SkImageFilter* SkDiffuseLightingImageFilter::Create(SkLight* light, SkScalar surfaceScale, |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 947 | SkScalar kd, SkImageFilter* input, const CropRect* cropRect, uint32_t uniqueID) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 948 | if (NULL == light) { |
| 949 | return NULL; |
| 950 | } |
| 951 | if (!SkScalarIsFinite(surfaceScale) || !SkScalarIsFinite(kd)) { |
| 952 | return NULL; |
| 953 | } |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 954 | // According to the spec, kd can be any non-negative number : |
| 955 | // http://www.w3.org/TR/SVG/filters.html#feDiffuseLightingElement |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 956 | if (kd < 0) { |
| 957 | return NULL; |
| 958 | } |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 959 | return SkNEW_ARGS(SkDiffuseLightingImageFilter, (light, surfaceScale, kd, input, cropRect, uniqueID)); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 960 | } |
| 961 | |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 962 | SkDiffuseLightingImageFilter::SkDiffuseLightingImageFilter(SkLight* light, SkScalar surfaceScale, SkScalar kd, SkImageFilter* input, const CropRect* cropRect, uint32_t uniqueID) |
| 963 | : SkLightingImageFilter(light, surfaceScale, input, cropRect, uniqueID), |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 964 | fKD(kd) |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 965 | { |
| 966 | } |
| 967 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 968 | #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 969 | SkDiffuseLightingImageFilter::SkDiffuseLightingImageFilter(SkReadBuffer& buffer) |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 970 | : INHERITED(buffer) |
| 971 | { |
| 972 | fKD = buffer.readScalar(); |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 973 | buffer.validate(SkScalarIsFinite(fKD) && (fKD >= 0)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 974 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 975 | #endif |
| 976 | |
| 977 | SkFlattenable* SkDiffuseLightingImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 978 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 979 | SkAutoTUnref<SkLight> light(SkLight::UnflattenLight(buffer)); |
| 980 | SkScalar surfaceScale = buffer.readScalar(); |
| 981 | SkScalar kd = buffer.readScalar(); |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 982 | return Create(light, surfaceScale, kd, common.getInput(0), &common.cropRect(), common.uniqueID()); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 983 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 984 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 985 | void SkDiffuseLightingImageFilter::flatten(SkWriteBuffer& buffer) const { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 986 | this->INHERITED::flatten(buffer); |
| 987 | buffer.writeScalar(fKD); |
| 988 | } |
| 989 | |
commit-bot@chromium.org | a8aef8b | 2013-10-23 15:33:44 +0000 | [diff] [blame] | 990 | bool SkDiffuseLightingImageFilter::onFilterImage(Proxy* proxy, |
| 991 | const SkBitmap& source, |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 992 | const Context& ctx, |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 993 | SkBitmap* dst, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 994 | SkIPoint* offset) const { |
commit-bot@chromium.org | a8aef8b | 2013-10-23 15:33:44 +0000 | [diff] [blame] | 995 | SkImageFilter* input = getInput(0); |
| 996 | SkBitmap src = source; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 997 | SkIPoint srcOffset = SkIPoint::Make(0, 0); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 998 | if (input && !input->filterImage(proxy, source, ctx, &src, &srcOffset)) { |
commit-bot@chromium.org | a8aef8b | 2013-10-23 15:33:44 +0000 | [diff] [blame] | 999 | return false; |
| 1000 | } |
| 1001 | |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 1002 | if (src.colorType() != kN32_SkColorType) { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1003 | return false; |
| 1004 | } |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 1005 | SkIRect bounds; |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 1006 | if (!this->applyCropRect(ctx, proxy, src, &srcOffset, &bounds, &src)) { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1007 | return false; |
| 1008 | } |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 1009 | |
| 1010 | if (bounds.width() < 2 || bounds.height() < 2) { |
| 1011 | return false; |
| 1012 | } |
| 1013 | |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 1014 | SkAutoLockPixels alp(src); |
| 1015 | if (!src.getPixels()) { |
| 1016 | return false; |
| 1017 | } |
| 1018 | |
reed | c77392e | 2014-06-02 13:07:26 -0700 | [diff] [blame] | 1019 | if (!dst->allocPixels(src.info().makeWH(bounds.width(), bounds.height()))) { |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 1020 | return false; |
| 1021 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1022 | |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1023 | SkAutoTUnref<SkLight> transformedLight(light()->transform(ctx.ctm())); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1024 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1025 | DiffuseLightingType lightingType(fKD); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1026 | offset->fX = bounds.left(); |
| 1027 | offset->fY = bounds.top(); |
| 1028 | bounds.offset(-srcOffset); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1029 | switch (transformedLight->type()) { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1030 | case SkLight::kDistant_LightType: |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1031 | lightBitmap<DiffuseLightingType, SkDistantLight>(lightingType, transformedLight, src, dst, surfaceScale(), bounds); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1032 | break; |
| 1033 | case SkLight::kPoint_LightType: |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1034 | lightBitmap<DiffuseLightingType, SkPointLight>(lightingType, transformedLight, src, dst, surfaceScale(), bounds); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1035 | break; |
| 1036 | case SkLight::kSpot_LightType: |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1037 | lightBitmap<DiffuseLightingType, SkSpotLight>(lightingType, transformedLight, src, dst, surfaceScale(), bounds); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1038 | break; |
| 1039 | } |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 1040 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1041 | return true; |
| 1042 | } |
| 1043 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 1044 | #if SK_SUPPORT_GPU |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 1045 | bool SkDiffuseLightingImageFilter::asNewEffect(GrEffect** effect, GrTexture* texture, |
| 1046 | const SkMatrix& matrix, const SkIRect&) const { |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 1047 | if (effect) { |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1048 | SkScalar scale = SkScalarMul(surfaceScale(), SkIntToScalar(255)); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1049 | *effect = GrDiffuseLightingEffect::Create(texture, light(), scale, matrix, kd()); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1050 | } |
| 1051 | return true; |
| 1052 | } |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 1053 | #endif |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1054 | |
| 1055 | /////////////////////////////////////////////////////////////////////////////// |
| 1056 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1057 | SkImageFilter* SkSpecularLightingImageFilter::Create(SkLight* light, SkScalar surfaceScale, |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 1058 | SkScalar ks, SkScalar shininess, SkImageFilter* input, const CropRect* cropRect, uint32_t uniqueID) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1059 | if (NULL == light) { |
| 1060 | return NULL; |
| 1061 | } |
| 1062 | if (!SkScalarIsFinite(surfaceScale) || !SkScalarIsFinite(ks) || !SkScalarIsFinite(shininess)) { |
| 1063 | return NULL; |
| 1064 | } |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 1065 | // According to the spec, ks can be any non-negative number : |
| 1066 | // http://www.w3.org/TR/SVG/filters.html#feSpecularLightingElement |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1067 | if (ks < 0) { |
| 1068 | return NULL; |
| 1069 | } |
| 1070 | return SkNEW_ARGS(SkSpecularLightingImageFilter, |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 1071 | (light, surfaceScale, ks, shininess, input, cropRect, uniqueID)); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 1074 | SkSpecularLightingImageFilter::SkSpecularLightingImageFilter(SkLight* light, SkScalar surfaceScale, SkScalar ks, SkScalar shininess, SkImageFilter* input, const CropRect* cropRect, uint32_t uniqueID) |
| 1075 | : SkLightingImageFilter(light, surfaceScale, input, cropRect, uniqueID), |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1076 | fKS(ks), |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1077 | fShininess(shininess) |
| 1078 | { |
| 1079 | } |
| 1080 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1081 | #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 1082 | SkSpecularLightingImageFilter::SkSpecularLightingImageFilter(SkReadBuffer& buffer) |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1083 | : INHERITED(buffer) |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1084 | { |
| 1085 | fKS = buffer.readScalar(); |
| 1086 | fShininess = buffer.readScalar(); |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 1087 | buffer.validate(SkScalarIsFinite(fKS) && (fKS >= 0) && |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 1088 | SkScalarIsFinite(fShininess)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1089 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1090 | #endif |
| 1091 | |
| 1092 | SkFlattenable* SkSpecularLightingImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 1093 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 1094 | SkAutoTUnref<SkLight> light(SkLight::UnflattenLight(buffer)); |
| 1095 | SkScalar surfaceScale = buffer.readScalar(); |
| 1096 | SkScalar ks = buffer.readScalar(); |
| 1097 | SkScalar shine = buffer.readScalar(); |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 1098 | return Create(light, surfaceScale, ks, shine, common.getInput(0), &common.cropRect(), common.uniqueID()); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1099 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1100 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 1101 | void SkSpecularLightingImageFilter::flatten(SkWriteBuffer& buffer) const { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1102 | this->INHERITED::flatten(buffer); |
| 1103 | buffer.writeScalar(fKS); |
| 1104 | buffer.writeScalar(fShininess); |
| 1105 | } |
| 1106 | |
commit-bot@chromium.org | a8aef8b | 2013-10-23 15:33:44 +0000 | [diff] [blame] | 1107 | bool SkSpecularLightingImageFilter::onFilterImage(Proxy* proxy, |
| 1108 | const SkBitmap& source, |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1109 | const Context& ctx, |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1110 | SkBitmap* dst, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 1111 | SkIPoint* offset) const { |
commit-bot@chromium.org | a8aef8b | 2013-10-23 15:33:44 +0000 | [diff] [blame] | 1112 | SkImageFilter* input = getInput(0); |
| 1113 | SkBitmap src = source; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1114 | SkIPoint srcOffset = SkIPoint::Make(0, 0); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1115 | if (input && !input->filterImage(proxy, source, ctx, &src, &srcOffset)) { |
commit-bot@chromium.org | a8aef8b | 2013-10-23 15:33:44 +0000 | [diff] [blame] | 1116 | return false; |
| 1117 | } |
| 1118 | |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 1119 | if (src.colorType() != kN32_SkColorType) { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1120 | return false; |
| 1121 | } |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 1122 | |
| 1123 | SkIRect bounds; |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 1124 | if (!this->applyCropRect(ctx, proxy, src, &srcOffset, &bounds, &src)) { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1125 | return false; |
| 1126 | } |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 1127 | |
| 1128 | if (bounds.width() < 2 || bounds.height() < 2) { |
| 1129 | return false; |
| 1130 | } |
| 1131 | |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 1132 | SkAutoLockPixels alp(src); |
| 1133 | if (!src.getPixels()) { |
| 1134 | return false; |
| 1135 | } |
| 1136 | |
reed | c77392e | 2014-06-02 13:07:26 -0700 | [diff] [blame] | 1137 | if (!dst->allocPixels(src.info().makeWH(bounds.width(), bounds.height()))) { |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 1138 | return false; |
| 1139 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1140 | |
| 1141 | SpecularLightingType lightingType(fKS, fShininess); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1142 | offset->fX = bounds.left(); |
| 1143 | offset->fY = bounds.top(); |
| 1144 | bounds.offset(-srcOffset); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1145 | SkAutoTUnref<SkLight> transformedLight(light()->transform(ctx.ctm())); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1146 | switch (transformedLight->type()) { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1147 | case SkLight::kDistant_LightType: |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1148 | lightBitmap<SpecularLightingType, SkDistantLight>(lightingType, transformedLight, src, dst, surfaceScale(), bounds); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1149 | break; |
| 1150 | case SkLight::kPoint_LightType: |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1151 | lightBitmap<SpecularLightingType, SkPointLight>(lightingType, transformedLight, src, dst, surfaceScale(), bounds); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1152 | break; |
| 1153 | case SkLight::kSpot_LightType: |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1154 | lightBitmap<SpecularLightingType, SkSpotLight>(lightingType, transformedLight, src, dst, surfaceScale(), bounds); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1155 | break; |
| 1156 | } |
| 1157 | return true; |
| 1158 | } |
| 1159 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 1160 | #if SK_SUPPORT_GPU |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 1161 | bool SkSpecularLightingImageFilter::asNewEffect(GrEffect** effect, GrTexture* texture, |
| 1162 | const SkMatrix& matrix, const SkIRect&) const { |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 1163 | if (effect) { |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1164 | SkScalar scale = SkScalarMul(surfaceScale(), SkIntToScalar(255)); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1165 | *effect = GrSpecularLightingEffect::Create(texture, light(), scale, matrix, ks(), shininess()); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1166 | } |
| 1167 | return true; |
| 1168 | } |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 1169 | #endif |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1170 | |
| 1171 | /////////////////////////////////////////////////////////////////////////////// |
| 1172 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 1173 | #if SK_SUPPORT_GPU |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1174 | |
| 1175 | namespace { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 1176 | SkPoint3 random_point3(SkRandom* random) { |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1177 | return SkPoint3(SkScalarToFloat(random->nextSScalar1()), |
| 1178 | SkScalarToFloat(random->nextSScalar1()), |
| 1179 | SkScalarToFloat(random->nextSScalar1())); |
| 1180 | } |
| 1181 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 1182 | SkLight* create_random_light(SkRandom* random) { |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1183 | int type = random->nextULessThan(3); |
| 1184 | switch (type) { |
| 1185 | case 0: { |
| 1186 | return SkNEW_ARGS(SkDistantLight, (random_point3(random), random->nextU())); |
| 1187 | } |
| 1188 | case 1: { |
| 1189 | return SkNEW_ARGS(SkPointLight, (random_point3(random), random->nextU())); |
| 1190 | } |
| 1191 | case 2: { |
| 1192 | return SkNEW_ARGS(SkSpotLight, (random_point3(random), |
| 1193 | random_point3(random), |
| 1194 | random->nextUScalar1(), |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1195 | random->nextUScalar1(), |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1196 | random->nextU())); |
| 1197 | } |
| 1198 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 1199 | SkFAIL("Unexpected value."); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1200 | return NULL; |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | } |
| 1205 | |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1206 | class GrGLLightingEffect : public GrGLEffect { |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1207 | public: |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 1208 | GrGLLightingEffect(const GrBackendEffectFactory& factory, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 1209 | const GrDrawEffect& effect); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1210 | virtual ~GrGLLightingEffect(); |
| 1211 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1212 | virtual void emitCode(GrGLProgramBuilder*, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 1213 | const GrDrawEffect&, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 1214 | const GrEffectKey&, |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1215 | const char* outputColor, |
| 1216 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 1217 | const TransformedCoordsArray&, |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1218 | const TextureSamplerArray&) SK_OVERRIDE; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1219 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 1220 | static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuilder* b); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1221 | |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1222 | /** |
| 1223 | * Subclasses of GrGLLightingEffect must call INHERITED::setData(); |
| 1224 | */ |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1225 | virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1226 | |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1227 | protected: |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1228 | virtual void emitLightFunc(GrGLProgramBuilder*, SkString* funcName) = 0; |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1229 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1230 | private: |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1231 | typedef GrGLEffect INHERITED; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1232 | |
bsalomon@google.com | 17fc651 | 2012-11-02 21:45:01 +0000 | [diff] [blame] | 1233 | UniformHandle fImageIncrementUni; |
| 1234 | UniformHandle fSurfaceScaleUni; |
| 1235 | GrGLLight* fLight; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1236 | }; |
| 1237 | |
| 1238 | /////////////////////////////////////////////////////////////////////////////// |
| 1239 | |
| 1240 | class GrGLDiffuseLightingEffect : public GrGLLightingEffect { |
| 1241 | public: |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 1242 | GrGLDiffuseLightingEffect(const GrBackendEffectFactory& factory, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 1243 | const GrDrawEffect& drawEffect); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1244 | virtual void emitLightFunc(GrGLProgramBuilder*, SkString* funcName) SK_OVERRIDE; |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1245 | virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1246 | |
| 1247 | private: |
| 1248 | typedef GrGLLightingEffect INHERITED; |
| 1249 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 1250 | UniformHandle fKDUni; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1251 | }; |
| 1252 | |
| 1253 | /////////////////////////////////////////////////////////////////////////////// |
| 1254 | |
| 1255 | class GrGLSpecularLightingEffect : public GrGLLightingEffect { |
| 1256 | public: |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 1257 | GrGLSpecularLightingEffect(const GrBackendEffectFactory& factory, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 1258 | const GrDrawEffect& effect); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1259 | virtual void emitLightFunc(GrGLProgramBuilder*, SkString* funcName) SK_OVERRIDE; |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1260 | virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1261 | |
| 1262 | private: |
| 1263 | typedef GrGLLightingEffect INHERITED; |
| 1264 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 1265 | UniformHandle fKSUni; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 1266 | UniformHandle fShininessUni; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1267 | }; |
| 1268 | |
| 1269 | /////////////////////////////////////////////////////////////////////////////// |
| 1270 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 1271 | GrLightingEffect::GrLightingEffect(GrTexture* texture, |
| 1272 | const SkLight* light, |
| 1273 | SkScalar surfaceScale, |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1274 | const SkMatrix& matrix) |
bsalomon@google.com | 17fc651 | 2012-11-02 21:45:01 +0000 | [diff] [blame] | 1275 | : INHERITED(texture, MakeDivByTextureWHMatrix(texture)) |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 1276 | , fLight(light) |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 1277 | , fSurfaceScale(surfaceScale) |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1278 | , fFilterMatrix(matrix) { |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1279 | fLight->ref(); |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 1280 | if (light->requiresFragmentPosition()) { |
| 1281 | this->setWillReadFragmentPosition(); |
| 1282 | } |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | GrLightingEffect::~GrLightingEffect() { |
| 1286 | fLight->unref(); |
| 1287 | } |
| 1288 | |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 1289 | bool GrLightingEffect::onIsEqual(const GrEffect& sBase) const { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 1290 | const GrLightingEffect& s = CastEffect<GrLightingEffect>(sBase); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 1291 | return this->texture(0) == s.texture(0) && |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 1292 | fLight->isEqual(*s.fLight) && |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1293 | fSurfaceScale == s.fSurfaceScale; |
| 1294 | } |
| 1295 | |
| 1296 | /////////////////////////////////////////////////////////////////////////////// |
| 1297 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 1298 | GrDiffuseLightingEffect::GrDiffuseLightingEffect(GrTexture* texture, |
| 1299 | const SkLight* light, |
| 1300 | SkScalar surfaceScale, |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1301 | const SkMatrix& matrix, |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 1302 | SkScalar kd) |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1303 | : INHERITED(texture, light, surfaceScale, matrix), fKD(kd) { |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 1306 | const GrBackendEffectFactory& GrDiffuseLightingEffect::getFactory() const { |
| 1307 | return GrTBackendEffectFactory<GrDiffuseLightingEffect>::getInstance(); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 1310 | bool GrDiffuseLightingEffect::onIsEqual(const GrEffect& sBase) const { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 1311 | const GrDiffuseLightingEffect& s = CastEffect<GrDiffuseLightingEffect>(sBase); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 1312 | return INHERITED::onIsEqual(sBase) && |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1313 | this->kd() == s.kd(); |
| 1314 | } |
| 1315 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 1316 | GR_DEFINE_EFFECT_TEST(GrDiffuseLightingEffect); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1317 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 1318 | GrEffect* GrDiffuseLightingEffect::TestCreate(SkRandom* random, |
| 1319 | GrContext* context, |
| 1320 | const GrDrawTargetCaps&, |
| 1321 | GrTexture* textures[]) { |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1322 | SkScalar surfaceScale = random->nextSScalar1(); |
| 1323 | SkScalar kd = random->nextUScalar1(); |
| 1324 | SkAutoTUnref<SkLight> light(create_random_light(random)); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1325 | SkMatrix matrix; |
| 1326 | for (int i = 0; i < 9; i++) { |
| 1327 | matrix[i] = random->nextUScalar1(); |
| 1328 | } |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 1329 | return GrDiffuseLightingEffect::Create(textures[GrEffectUnitTest::kAlphaTextureIdx], |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1330 | light, surfaceScale, matrix, kd); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1334 | /////////////////////////////////////////////////////////////////////////////// |
| 1335 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 1336 | GrGLLightingEffect::GrGLLightingEffect(const GrBackendEffectFactory& factory, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 1337 | const GrDrawEffect& drawEffect) |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 1338 | : INHERITED(factory) { |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 1339 | const GrLightingEffect& m = drawEffect.castEffect<GrLightingEffect>(); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1340 | fLight = m.light()->createGLLight(); |
| 1341 | } |
| 1342 | |
| 1343 | GrGLLightingEffect::~GrGLLightingEffect() { |
| 1344 | delete fLight; |
| 1345 | } |
| 1346 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1347 | void GrGLLightingEffect::emitCode(GrGLProgramBuilder* builder, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 1348 | const GrDrawEffect&, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 1349 | const GrEffectKey& key, |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1350 | const char* outputColor, |
| 1351 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 1352 | const TransformedCoordsArray& coords, |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1353 | const TextureSamplerArray& samplers) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1354 | fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 1355 | kVec2f_GrSLType, |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 1356 | "ImageIncrement"); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1357 | fSurfaceScaleUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 1358 | kFloat_GrSLType, |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 1359 | "SurfaceScale"); |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1360 | fLight->emitLightColorUniform(builder); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1361 | SkString lightFunc; |
| 1362 | this->emitLightFunc(builder, &lightFunc); |
| 1363 | static const GrGLShaderVar gSobelArgs[] = { |
| 1364 | GrGLShaderVar("a", kFloat_GrSLType), |
| 1365 | GrGLShaderVar("b", kFloat_GrSLType), |
| 1366 | GrGLShaderVar("c", kFloat_GrSLType), |
| 1367 | GrGLShaderVar("d", kFloat_GrSLType), |
| 1368 | GrGLShaderVar("e", kFloat_GrSLType), |
| 1369 | GrGLShaderVar("f", kFloat_GrSLType), |
| 1370 | GrGLShaderVar("scale", kFloat_GrSLType), |
| 1371 | }; |
| 1372 | SkString sobelFuncName; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1373 | GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 1374 | SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0); |
| 1375 | |
| 1376 | fsBuilder->emitFunction(kFloat_GrSLType, |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 1377 | "sobel", |
| 1378 | SK_ARRAY_COUNT(gSobelArgs), |
| 1379 | gSobelArgs, |
| 1380 | "\treturn (-a + b - 2.0 * c + 2.0 * d -e + f) * scale;\n", |
| 1381 | &sobelFuncName); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1382 | static const GrGLShaderVar gPointToNormalArgs[] = { |
| 1383 | GrGLShaderVar("x", kFloat_GrSLType), |
| 1384 | GrGLShaderVar("y", kFloat_GrSLType), |
| 1385 | GrGLShaderVar("scale", kFloat_GrSLType), |
| 1386 | }; |
| 1387 | SkString pointToNormalName; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1388 | fsBuilder->emitFunction(kVec3f_GrSLType, |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 1389 | "pointToNormal", |
| 1390 | SK_ARRAY_COUNT(gPointToNormalArgs), |
| 1391 | gPointToNormalArgs, |
| 1392 | "\treturn normalize(vec3(-x * scale, y * scale, 1));\n", |
| 1393 | &pointToNormalName); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1394 | |
| 1395 | static const GrGLShaderVar gInteriorNormalArgs[] = { |
| 1396 | GrGLShaderVar("m", kFloat_GrSLType, 9), |
| 1397 | GrGLShaderVar("surfaceScale", kFloat_GrSLType), |
| 1398 | }; |
| 1399 | SkString interiorNormalBody; |
| 1400 | interiorNormalBody.appendf("\treturn %s(%s(m[0], m[2], m[3], m[5], m[6], m[8], 0.25),\n" |
| 1401 | "\t %s(m[0], m[6], m[1], m[7], m[2], m[8], 0.25),\n" |
| 1402 | "\t surfaceScale);\n", |
| 1403 | pointToNormalName.c_str(), |
| 1404 | sobelFuncName.c_str(), |
| 1405 | sobelFuncName.c_str()); |
| 1406 | SkString interiorNormalName; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1407 | fsBuilder->emitFunction(kVec3f_GrSLType, |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 1408 | "interiorNormal", |
| 1409 | SK_ARRAY_COUNT(gInteriorNormalArgs), |
| 1410 | gInteriorNormalArgs, |
| 1411 | interiorNormalBody.c_str(), |
| 1412 | &interiorNormalName); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1413 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1414 | fsBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str()); |
| 1415 | fsBuilder->codeAppend("\t\tfloat m[9];\n"); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 1416 | |
| 1417 | const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
| 1418 | const char* surfScale = builder->getUniformCStr(fSurfaceScaleUni); |
| 1419 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1420 | int index = 0; |
| 1421 | for (int dy = -1; dy <= 1; dy++) { |
| 1422 | for (int dx = -1; dx <= 1; dx++) { |
| 1423 | SkString texCoords; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 1424 | texCoords.appendf("coord + vec2(%d, %d) * %s", dx, dy, imgInc); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1425 | fsBuilder->codeAppendf("\t\tm[%d] = ", index++); |
| 1426 | fsBuilder->appendTextureLookup(samplers[0], texCoords.c_str()); |
| 1427 | fsBuilder->codeAppend(".a;\n"); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1428 | } |
| 1429 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1430 | fsBuilder->codeAppend("\t\tvec3 surfaceToLight = "); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1431 | SkString arg; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 1432 | arg.appendf("%s * m[4]", surfScale); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 1433 | fLight->emitSurfaceToLight(builder, arg.c_str()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1434 | fsBuilder->codeAppend(";\n"); |
| 1435 | fsBuilder->codeAppendf("\t\t%s = %s(%s(m, %s), surfaceToLight, ", |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 1436 | outputColor, lightFunc.c_str(), interiorNormalName.c_str(), surfScale); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1437 | fLight->emitLightColor(builder, "surfaceToLight"); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1438 | fsBuilder->codeAppend(");\n"); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 1439 | SkString modulate; |
| 1440 | GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1441 | fsBuilder->codeAppend(modulate.c_str()); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 1444 | void GrGLLightingEffect::GenKey(const GrDrawEffect& drawEffect, |
| 1445 | const GrGLCaps& caps, GrEffectKeyBuilder* b) { |
| 1446 | b->add32(drawEffect.castEffect<GrLightingEffect>().light()->type()); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1449 | void GrGLLightingEffect::setData(const GrGLProgramDataManager& pdman, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 1450 | const GrDrawEffect& drawEffect) { |
| 1451 | const GrLightingEffect& lighting = drawEffect.castEffect<GrLightingEffect>(); |
| 1452 | GrTexture* texture = lighting.texture(0); |
senorblanco@chromium.org | ef5dbe1 | 2013-01-28 16:42:38 +0000 | [diff] [blame] | 1453 | float ySign = texture->origin() == kTopLeft_GrSurfaceOrigin ? -1.0f : 1.0f; |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1454 | pdman.set2f(fImageIncrementUni, 1.0f / texture->width(), ySign / texture->height()); |
| 1455 | pdman.set1f(fSurfaceScaleUni, lighting.surfaceScale()); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1456 | SkAutoTUnref<SkLight> transformedLight(lighting.light()->transform(lighting.filterMatrix())); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1457 | fLight->setData(pdman, transformedLight); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | /////////////////////////////////////////////////////////////////////////////// |
| 1461 | |
| 1462 | /////////////////////////////////////////////////////////////////////////////// |
| 1463 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 1464 | GrGLDiffuseLightingEffect::GrGLDiffuseLightingEffect(const GrBackendEffectFactory& factory, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 1465 | const GrDrawEffect& drawEffect) |
commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 1466 | : INHERITED(factory, drawEffect) { |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1469 | void GrGLDiffuseLightingEffect::emitLightFunc(GrGLProgramBuilder* builder, SkString* funcName) { |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1470 | const char* kd; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1471 | fKDUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1472 | kFloat_GrSLType, |
| 1473 | "KD", |
| 1474 | &kd); |
| 1475 | |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1476 | static const GrGLShaderVar gLightArgs[] = { |
| 1477 | GrGLShaderVar("normal", kVec3f_GrSLType), |
| 1478 | GrGLShaderVar("surfaceToLight", kVec3f_GrSLType), |
| 1479 | GrGLShaderVar("lightColor", kVec3f_GrSLType) |
| 1480 | }; |
| 1481 | SkString lightBody; |
| 1482 | lightBody.appendf("\tfloat colorScale = %s * dot(normal, surfaceToLight);\n", kd); |
| 1483 | lightBody.appendf("\treturn vec4(lightColor * clamp(colorScale, 0.0, 1.0), 1.0);\n"); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1484 | builder->getFragmentShaderBuilder()->emitFunction(kVec4f_GrSLType, |
| 1485 | "light", |
| 1486 | SK_ARRAY_COUNT(gLightArgs), |
| 1487 | gLightArgs, |
| 1488 | lightBody.c_str(), |
| 1489 | funcName); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1492 | void GrGLDiffuseLightingEffect::setData(const GrGLProgramDataManager& pdman, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 1493 | const GrDrawEffect& drawEffect) { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1494 | INHERITED::setData(pdman, drawEffect); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 1495 | const GrDiffuseLightingEffect& diffuse = drawEffect.castEffect<GrDiffuseLightingEffect>(); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1496 | pdman.set1f(fKDUni, diffuse.kd()); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | /////////////////////////////////////////////////////////////////////////////// |
| 1500 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 1501 | GrSpecularLightingEffect::GrSpecularLightingEffect(GrTexture* texture, |
| 1502 | const SkLight* light, |
| 1503 | SkScalar surfaceScale, |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1504 | const SkMatrix& matrix, |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 1505 | SkScalar ks, |
| 1506 | SkScalar shininess) |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1507 | : INHERITED(texture, light, surfaceScale, matrix), |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1508 | fKS(ks), |
| 1509 | fShininess(shininess) { |
| 1510 | } |
| 1511 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 1512 | const GrBackendEffectFactory& GrSpecularLightingEffect::getFactory() const { |
| 1513 | return GrTBackendEffectFactory<GrSpecularLightingEffect>::getInstance(); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 1516 | bool GrSpecularLightingEffect::onIsEqual(const GrEffect& sBase) const { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 1517 | const GrSpecularLightingEffect& s = CastEffect<GrSpecularLightingEffect>(sBase); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 1518 | return INHERITED::onIsEqual(sBase) && |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1519 | this->ks() == s.ks() && |
| 1520 | this->shininess() == s.shininess(); |
| 1521 | } |
| 1522 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 1523 | GR_DEFINE_EFFECT_TEST(GrSpecularLightingEffect); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1524 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 1525 | GrEffect* GrSpecularLightingEffect::TestCreate(SkRandom* random, |
| 1526 | GrContext* context, |
| 1527 | const GrDrawTargetCaps&, |
| 1528 | GrTexture* textures[]) { |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1529 | SkScalar surfaceScale = random->nextSScalar1(); |
| 1530 | SkScalar ks = random->nextUScalar1(); |
| 1531 | SkScalar shininess = random->nextUScalar1(); |
| 1532 | SkAutoTUnref<SkLight> light(create_random_light(random)); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1533 | SkMatrix matrix; |
| 1534 | for (int i = 0; i < 9; i++) { |
| 1535 | matrix[i] = random->nextUScalar1(); |
| 1536 | } |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 1537 | return GrSpecularLightingEffect::Create(textures[GrEffectUnitTest::kAlphaTextureIdx], |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1538 | light, surfaceScale, matrix, ks, shininess); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1541 | /////////////////////////////////////////////////////////////////////////////// |
| 1542 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 1543 | GrGLSpecularLightingEffect::GrGLSpecularLightingEffect(const GrBackendEffectFactory& factory, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 1544 | const GrDrawEffect& drawEffect) |
commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 1545 | : INHERITED(factory, drawEffect) { |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1548 | void GrGLSpecularLightingEffect::emitLightFunc(GrGLProgramBuilder* builder, SkString* funcName) { |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1549 | const char* ks; |
| 1550 | const char* shininess; |
| 1551 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1552 | fKSUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1553 | kFloat_GrSLType, "KS", &ks); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1554 | fShininessUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1555 | kFloat_GrSLType, "Shininess", &shininess); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1556 | |
| 1557 | static const GrGLShaderVar gLightArgs[] = { |
| 1558 | GrGLShaderVar("normal", kVec3f_GrSLType), |
| 1559 | GrGLShaderVar("surfaceToLight", kVec3f_GrSLType), |
| 1560 | GrGLShaderVar("lightColor", kVec3f_GrSLType) |
| 1561 | }; |
| 1562 | SkString lightBody; |
| 1563 | lightBody.appendf("\tvec3 halfDir = vec3(normalize(surfaceToLight + vec3(0, 0, 1)));\n"); |
| 1564 | lightBody.appendf("\tfloat colorScale = %s * pow(dot(normal, halfDir), %s);\n", ks, shininess); |
senorblanco@chromium.org | 7b734e0 | 2012-10-29 19:47:06 +0000 | [diff] [blame] | 1565 | lightBody.appendf("\tvec3 color = lightColor * clamp(colorScale, 0.0, 1.0);\n"); |
| 1566 | lightBody.appendf("\treturn vec4(color, max(max(color.r, color.g), color.b));\n"); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1567 | builder->getFragmentShaderBuilder()->emitFunction(kVec4f_GrSLType, |
| 1568 | "light", |
| 1569 | SK_ARRAY_COUNT(gLightArgs), |
| 1570 | gLightArgs, |
| 1571 | lightBody.c_str(), |
| 1572 | funcName); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1575 | void GrGLSpecularLightingEffect::setData(const GrGLProgramDataManager& pdman, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 1576 | const GrDrawEffect& drawEffect) { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1577 | INHERITED::setData(pdman, drawEffect); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 1578 | const GrSpecularLightingEffect& spec = drawEffect.castEffect<GrSpecularLightingEffect>(); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1579 | pdman.set1f(fKSUni, spec.ks()); |
| 1580 | pdman.set1f(fShininessUni, spec.shininess()); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | /////////////////////////////////////////////////////////////////////////////// |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1584 | void GrGLLight::emitLightColorUniform(GrGLProgramBuilder* builder) { |
| 1585 | fColorUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 1586 | kVec3f_GrSLType, "LightColor"); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1589 | void GrGLLight::emitLightColor(GrGLProgramBuilder* builder, |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1590 | const char *surfaceToLight) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1591 | builder->getFragmentShaderBuilder()-> |
| 1592 | codeAppend(builder->getUniformCStr(this->lightColorUni())); |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1595 | void GrGLLight::setData(const GrGLProgramDataManager& pdman, |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1596 | const SkLight* light) const { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1597 | setUniformPoint3(pdman, fColorUni, light->color() * SkScalarInvert(SkIntToScalar(255))); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1600 | /////////////////////////////////////////////////////////////////////////////// |
| 1601 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1602 | void GrGLDistantLight::setData(const GrGLProgramDataManager& pdman, |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1603 | const SkLight* light) const { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1604 | INHERITED::setData(pdman, light); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1605 | SkASSERT(light->type() == SkLight::kDistant_LightType); |
| 1606 | const SkDistantLight* distantLight = static_cast<const SkDistantLight*>(light); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1607 | setUniformNormal3(pdman, fDirectionUni, distantLight->direction()); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1610 | void GrGLDistantLight::emitSurfaceToLight(GrGLProgramBuilder* builder, const char* z) { |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1611 | const char* dir; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1612 | fDirectionUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, kVec3f_GrSLType, |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1613 | "LightDirection", &dir); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1614 | builder->getFragmentShaderBuilder()->codeAppend(dir); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | /////////////////////////////////////////////////////////////////////////////// |
| 1618 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1619 | void GrGLPointLight::setData(const GrGLProgramDataManager& pdman, |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1620 | const SkLight* light) const { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1621 | INHERITED::setData(pdman, light); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1622 | SkASSERT(light->type() == SkLight::kPoint_LightType); |
| 1623 | const SkPointLight* pointLight = static_cast<const SkPointLight*>(light); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1624 | setUniformPoint3(pdman, fLocationUni, pointLight->location()); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1627 | void GrGLPointLight::emitSurfaceToLight(GrGLProgramBuilder* builder, const char* z) { |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1628 | const char* loc; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1629 | fLocationUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, kVec3f_GrSLType, |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1630 | "LightLocation", &loc); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1631 | GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 1632 | fsBuilder->codeAppendf("normalize(%s - vec3(%s.xy, %s))", |
| 1633 | loc, fsBuilder->fragmentPosition(), z); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | /////////////////////////////////////////////////////////////////////////////// |
| 1637 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1638 | void GrGLSpotLight::setData(const GrGLProgramDataManager& pdman, |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1639 | const SkLight* light) const { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1640 | INHERITED::setData(pdman, light); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1641 | SkASSERT(light->type() == SkLight::kSpot_LightType); |
| 1642 | const SkSpotLight* spotLight = static_cast<const SkSpotLight *>(light); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1643 | setUniformPoint3(pdman, fLocationUni, spotLight->location()); |
| 1644 | pdman.set1f(fExponentUni, spotLight->specularExponent()); |
| 1645 | pdman.set1f(fCosInnerConeAngleUni, spotLight->cosInnerConeAngle()); |
| 1646 | pdman.set1f(fCosOuterConeAngleUni, spotLight->cosOuterConeAngle()); |
| 1647 | pdman.set1f(fConeScaleUni, spotLight->coneScale()); |
| 1648 | setUniformNormal3(pdman, fSUni, spotLight->s()); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1651 | void GrGLSpotLight::emitSurfaceToLight(GrGLProgramBuilder* builder, const char* z) { |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1652 | const char* location; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1653 | fLocationUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1654 | kVec3f_GrSLType, "LightLocation", &location); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1655 | |
| 1656 | GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 1657 | fsBuilder->codeAppendf("normalize(%s - vec3(%s.xy, %s))", |
| 1658 | location, fsBuilder->fragmentPosition(), z); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1661 | void GrGLSpotLight::emitLightColor(GrGLProgramBuilder* builder, |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1662 | const char *surfaceToLight) { |
| 1663 | |
| 1664 | const char* color = builder->getUniformCStr(this->lightColorUni()); // created by parent class. |
| 1665 | |
| 1666 | const char* exponent; |
| 1667 | const char* cosInner; |
| 1668 | const char* cosOuter; |
| 1669 | const char* coneScale; |
| 1670 | const char* s; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1671 | fExponentUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1672 | kFloat_GrSLType, "Exponent", &exponent); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1673 | fCosInnerConeAngleUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1674 | kFloat_GrSLType, "CosInnerConeAngle", &cosInner); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1675 | fCosOuterConeAngleUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1676 | kFloat_GrSLType, "CosOuterConeAngle", &cosOuter); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1677 | fConeScaleUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1678 | kFloat_GrSLType, "ConeScale", &coneScale); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1679 | fSUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1680 | kVec3f_GrSLType, "S", &s); |
| 1681 | |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1682 | static const GrGLShaderVar gLightColorArgs[] = { |
| 1683 | GrGLShaderVar("surfaceToLight", kVec3f_GrSLType) |
| 1684 | }; |
| 1685 | SkString lightColorBody; |
| 1686 | lightColorBody.appendf("\tfloat cosAngle = -dot(surfaceToLight, %s);\n", s); |
| 1687 | lightColorBody.appendf("\tif (cosAngle < %s) {\n", cosOuter); |
| 1688 | lightColorBody.appendf("\t\treturn vec3(0);\n"); |
| 1689 | lightColorBody.appendf("\t}\n"); |
| 1690 | lightColorBody.appendf("\tfloat scale = pow(cosAngle, %s);\n", exponent); |
| 1691 | lightColorBody.appendf("\tif (cosAngle < %s) {\n", cosInner); |
| 1692 | lightColorBody.appendf("\t\treturn %s * scale * (cosAngle - %s) * %s;\n", |
| 1693 | color, cosOuter, coneScale); |
| 1694 | lightColorBody.appendf("\t}\n"); |
| 1695 | lightColorBody.appendf("\treturn %s;\n", color); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1696 | GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 1697 | fsBuilder->emitFunction(kVec3f_GrSLType, |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 1698 | "lightColor", |
| 1699 | SK_ARRAY_COUNT(gLightColorArgs), |
| 1700 | gLightColorArgs, |
| 1701 | lightColorBody.c_str(), |
| 1702 | &fLightColorFunc); |
skia.committer@gmail.com | e862d16 | 2012-10-31 02:01:18 +0000 | [diff] [blame] | 1703 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1704 | fsBuilder->codeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 1707 | #endif |
| 1708 | |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 1709 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) |
| 1710 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) |
| 1711 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 1712 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |