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