robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 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 | |
dvonbeck | 8811e40 | 2016-06-16 12:39:25 -0700 | [diff] [blame] | 8 | #include "SkBitmapProcShader.h" |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 9 | #include "SkBitmapProcState.h" |
| 10 | #include "SkColor.h" |
| 11 | #include "SkEmptyShader.h" |
| 12 | #include "SkErrorInternals.h" |
| 13 | #include "SkLightingShader.h" |
| 14 | #include "SkMathPriv.h" |
dvonbeck | 12c4fc2 | 2016-06-27 11:40:45 -0700 | [diff] [blame] | 15 | #include "SkNormalSource.h" |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 16 | #include "SkPoint3.h" |
| 17 | #include "SkReadBuffer.h" |
| 18 | #include "SkWriteBuffer.h" |
| 19 | |
| 20 | //////////////////////////////////////////////////////////////////////////// |
| 21 | |
| 22 | /* |
| 23 | SkLightingShader TODOs: |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 24 | support different light types |
| 25 | support multiple lights |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 26 | fix non-opaque diffuse textures |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 27 | |
| 28 | To Test: |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 29 | A8 diffuse textures |
| 30 | down & upsampled draws |
| 31 | */ |
| 32 | |
| 33 | |
| 34 | |
| 35 | /** \class SkLightingShaderImpl |
| 36 | This subclass of shader applies lighting. |
| 37 | */ |
dvonbeck | 8811e40 | 2016-06-16 12:39:25 -0700 | [diff] [blame] | 38 | class SkLightingShaderImpl : public SkShader { |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 39 | public: |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 40 | /** Create a new lighting shader that uses the provided normal map and |
| 41 | lights to light the diffuse bitmap. |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 42 | @param diffuseShader the shader that provides the diffuse colors |
dvonbeck | 5b794fa | 2016-07-06 13:58:36 -0700 | [diff] [blame] | 43 | @param normalSource the source of normals for lighting computation |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 44 | @param lights the lights applied to the geometry |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 45 | */ |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 46 | SkLightingShaderImpl(sk_sp<SkShader> diffuseShader, |
| 47 | sk_sp<SkNormalSource> normalSource, |
vjiaoblack | 95302da | 2016-07-21 10:25:54 -0700 | [diff] [blame] | 48 | sk_sp<SkLights> lights) |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 49 | : fDiffuseShader(std::move(diffuseShader)) |
| 50 | , fNormalSource(std::move(normalSource)) |
| 51 | , fLights(std::move(lights)) {} |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 52 | |
| 53 | bool isOpaque() const override; |
| 54 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 55 | #if SK_SUPPORT_GPU |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 56 | sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const override; |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 57 | #endif |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 58 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 59 | class LightingShaderContext : public SkShader::Context { |
| 60 | public: |
| 61 | // The context takes ownership of the states. It will call their destructors |
| 62 | // but will NOT free the memory. |
| 63 | LightingShaderContext(const SkLightingShaderImpl&, const ContextRec&, |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 64 | SkShader::Context* diffuseContext, SkNormalSource::Provider*, |
dvonbeck | 12c4fc2 | 2016-06-27 11:40:45 -0700 | [diff] [blame] | 65 | void* heapAllocated); |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 66 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 67 | ~LightingShaderContext() override; |
| 68 | |
| 69 | void shadeSpan(int x, int y, SkPMColor[], int count) override; |
| 70 | |
| 71 | uint32_t getFlags() const override { return fFlags; } |
| 72 | |
| 73 | private: |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 74 | SkShader::Context* fDiffuseContext; |
dvonbeck | 12c4fc2 | 2016-06-27 11:40:45 -0700 | [diff] [blame] | 75 | SkNormalSource::Provider* fNormalProvider; |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 76 | SkColor fPaintColor; |
dvonbeck | 12c4fc2 | 2016-06-27 11:40:45 -0700 | [diff] [blame] | 77 | uint32_t fFlags; |
| 78 | |
| 79 | void* fHeapAllocated; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 80 | |
| 81 | typedef SkShader::Context INHERITED; |
| 82 | }; |
| 83 | |
| 84 | SK_TO_STRING_OVERRIDE() |
| 85 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLightingShaderImpl) |
| 86 | |
| 87 | protected: |
| 88 | void flatten(SkWriteBuffer&) const override; |
reed | 773ceda | 2016-03-03 18:18:25 -0800 | [diff] [blame] | 89 | size_t onContextSize(const ContextRec&) const override; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 90 | Context* onCreateContext(const ContextRec&, void*) const override; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 91 | |
| 92 | private: |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 93 | sk_sp<SkShader> fDiffuseShader; |
rmistry | 0e56972 | 2016-07-08 06:23:35 -0700 | [diff] [blame] | 94 | sk_sp<SkNormalSource> fNormalSource; |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 95 | sk_sp<SkLights> fLights; |
rmistry | 0e56972 | 2016-07-08 06:23:35 -0700 | [diff] [blame] | 96 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 97 | friend class SkLightingShader; |
| 98 | |
| 99 | typedef SkShader INHERITED; |
| 100 | }; |
| 101 | |
| 102 | //////////////////////////////////////////////////////////////////////////// |
| 103 | |
| 104 | #if SK_SUPPORT_GPU |
| 105 | |
| 106 | #include "GrCoordTransform.h" |
| 107 | #include "GrFragmentProcessor.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 108 | #include "GrInvariantOutput.h" |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 109 | #include "GrTextureAccess.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 110 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 111 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 112 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 113 | #include "glsl/GrGLSLUniformHandler.h" |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 114 | #include "SkGr.h" |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 115 | #include "SkGrPriv.h" |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 116 | |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 117 | // This FP expects a premul'd color input for its diffuse color. Premul'ing of the paint's color is |
| 118 | // handled by the asFragmentProcessor() factory, but shaders providing diffuse color must output it |
| 119 | // premul'd. |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 120 | class LightingFP : public GrFragmentProcessor { |
| 121 | public: |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 122 | LightingFP(sk_sp<GrFragmentProcessor> normalFP, sk_sp<SkLights> lights) { |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 123 | |
| 124 | // fuse all ambient lights into a single one |
| 125 | fAmbientColor.set(0.0f, 0.0f, 0.0f); |
| 126 | for (int i = 0; i < lights->numLights(); ++i) { |
robertphillips | 71e0552 | 2016-05-31 12:08:25 -0700 | [diff] [blame] | 127 | if (SkLights::Light::kAmbient_LightType == lights->light(i).type()) { |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 128 | fAmbientColor += lights->light(i).color(); |
| 129 | } else { |
| 130 | // TODO: handle more than one of these |
| 131 | fLightColor = lights->light(i).color(); |
| 132 | fLightDir = lights->light(i).dir(); |
vjiaoblack | 95302da | 2016-07-21 10:25:54 -0700 | [diff] [blame] | 133 | // TODO get the handle to the shadow map if there is one |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
dvonbeck | 8811e40 | 2016-06-16 12:39:25 -0700 | [diff] [blame] | 137 | this->registerChildProcessor(std::move(normalFP)); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 138 | this->initClassID<LightingFP>(); |
| 139 | } |
| 140 | |
dvonbeck | 8811e40 | 2016-06-16 12:39:25 -0700 | [diff] [blame] | 141 | class GLSLLightingFP : public GrGLSLFragmentProcessor { |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 142 | public: |
dvonbeck | 8811e40 | 2016-06-16 12:39:25 -0700 | [diff] [blame] | 143 | GLSLLightingFP() { |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 144 | fLightDir.fX = 10000.0f; |
| 145 | fLightColor.fX = 0.0f; |
| 146 | fAmbientColor.fX = 0.0f; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void emitCode(EmitArgs& args) override { |
| 150 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 151 | GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 152 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 153 | |
| 154 | // add uniforms |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 155 | const char* lightDirUniName = nullptr; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 156 | fLightDirUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 157 | kVec3f_GrSLType, kDefault_GrSLPrecision, |
| 158 | "LightDir", &lightDirUniName); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 159 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 160 | const char* lightColorUniName = nullptr; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 161 | fLightColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 162 | kVec3f_GrSLType, kDefault_GrSLPrecision, |
| 163 | "LightColor", &lightColorUniName); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 164 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 165 | const char* ambientColorUniName = nullptr; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 166 | fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 167 | kVec3f_GrSLType, kDefault_GrSLPrecision, |
| 168 | "AmbientColor", &ambientColorUniName); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 169 | |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 170 | fragBuilder->codeAppendf("vec4 diffuseColor = %s;", args.fInputColor); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 171 | |
dvonbeck | 8811e40 | 2016-06-16 12:39:25 -0700 | [diff] [blame] | 172 | SkString dstNormalName("dstNormal"); |
| 173 | this->emitChild(0, nullptr, &dstNormalName, args); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 174 | |
dvonbeck | 8811e40 | 2016-06-16 12:39:25 -0700 | [diff] [blame] | 175 | fragBuilder->codeAppendf("vec3 normal = %s.xyz;", dstNormalName.c_str()); |
vjiaoblack | 95302da | 2016-07-21 10:25:54 -0700 | [diff] [blame] | 176 | |
| 177 | // TODO: make this a loop and modulate the contribution from each light |
| 178 | // based on the shadow map |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 179 | fragBuilder->codeAppendf("float NdotL = clamp(dot(normal, %s), 0.0, 1.0);", |
| 180 | lightDirUniName); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 181 | // diffuse light |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 182 | fragBuilder->codeAppendf("vec3 result = %s*diffuseColor.rgb*NdotL;", lightColorUniName); |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 183 | // ambient light (multiplied by input color's alpha because we're working in premul'd |
| 184 | // space) |
| 185 | fragBuilder->codeAppendf("result += diffuseColor.a * %s;", ambientColorUniName); |
| 186 | |
| 187 | // Clamping to alpha (equivalent to an unpremul'd clamp to 1.0) |
| 188 | fragBuilder->codeAppendf("%s = vec4(clamp(result.rgb, 0.0, diffuseColor.a), " |
| 189 | "diffuseColor.a);", args.fOutputColor); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, |
| 193 | GrProcessorKeyBuilder* b) { |
| 194 | // const LightingFP& lightingFP = proc.cast<LightingFP>(); |
| 195 | // only one shader generated currently |
| 196 | b->add32(0x0); |
| 197 | } |
| 198 | |
| 199 | protected: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 200 | void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override { |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 201 | const LightingFP& lightingFP = proc.cast<LightingFP>(); |
| 202 | |
| 203 | const SkVector3& lightDir = lightingFP.lightDir(); |
| 204 | if (lightDir != fLightDir) { |
| 205 | pdman.set3fv(fLightDirUni, 1, &lightDir.fX); |
| 206 | fLightDir = lightDir; |
| 207 | } |
| 208 | |
| 209 | const SkColor3f& lightColor = lightingFP.lightColor(); |
| 210 | if (lightColor != fLightColor) { |
| 211 | pdman.set3fv(fLightColorUni, 1, &lightColor.fX); |
| 212 | fLightColor = lightColor; |
| 213 | } |
| 214 | |
| 215 | const SkColor3f& ambientColor = lightingFP.ambientColor(); |
| 216 | if (ambientColor != fAmbientColor) { |
| 217 | pdman.set3fv(fAmbientColorUni, 1, &ambientColor.fX); |
| 218 | fAmbientColor = ambientColor; |
| 219 | } |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | private: |
| 223 | SkVector3 fLightDir; |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 224 | GrGLSLProgramDataManager::UniformHandle fLightDirUni; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 225 | |
| 226 | SkColor3f fLightColor; |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 227 | GrGLSLProgramDataManager::UniformHandle fLightColorUni; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 228 | |
| 229 | SkColor3f fAmbientColor; |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 230 | GrGLSLProgramDataManager::UniformHandle fAmbientColorUni; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 231 | }; |
| 232 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 233 | void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { |
dvonbeck | 8811e40 | 2016-06-16 12:39:25 -0700 | [diff] [blame] | 234 | GLSLLightingFP::GenKey(*this, caps, b); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | const char* name() const override { return "LightingFP"; } |
| 238 | |
| 239 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
| 240 | inout->mulByUnknownFourComponents(); |
| 241 | } |
| 242 | |
| 243 | const SkVector3& lightDir() const { return fLightDir; } |
| 244 | const SkColor3f& lightColor() const { return fLightColor; } |
| 245 | const SkColor3f& ambientColor() const { return fAmbientColor; } |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 246 | |
| 247 | private: |
dvonbeck | 8811e40 | 2016-06-16 12:39:25 -0700 | [diff] [blame] | 248 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLSLLightingFP; } |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 249 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 250 | bool onIsEqual(const GrFragmentProcessor& proc) const override { |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 251 | const LightingFP& lightingFP = proc.cast<LightingFP>(); |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 252 | return fLightDir == lightingFP.fLightDir && |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 253 | fLightColor == lightingFP.fLightColor && |
dvonbeck | 8811e40 | 2016-06-16 12:39:25 -0700 | [diff] [blame] | 254 | fAmbientColor == lightingFP.fAmbientColor; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 255 | } |
| 256 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 257 | SkVector3 fLightDir; |
| 258 | SkColor3f fLightColor; |
| 259 | SkColor3f fAmbientColor; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 260 | }; |
| 261 | |
| 262 | //////////////////////////////////////////////////////////////////////////// |
| 263 | |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 264 | sk_sp<GrFragmentProcessor> SkLightingShaderImpl::asFragmentProcessor(const AsFPArgs& args) const { |
| 265 | sk_sp<GrFragmentProcessor> normalFP(fNormalSource->asFragmentProcessor(args)); |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 266 | if (!normalFP) { |
| 267 | return nullptr; |
| 268 | } |
| 269 | |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 270 | if (fDiffuseShader) { |
| 271 | sk_sp<GrFragmentProcessor> fpPipeline[] = { |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 272 | fDiffuseShader->asFragmentProcessor(args), |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 273 | sk_make_sp<LightingFP>(std::move(normalFP), fLights) |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 274 | }; |
| 275 | if(!fpPipeline[0]) { |
| 276 | return nullptr; |
| 277 | } |
| 278 | |
| 279 | sk_sp<GrFragmentProcessor> innerLightFP = GrFragmentProcessor::RunInSeries(fpPipeline, 2); |
| 280 | // FP is wrapped because paint's alpha needs to be applied to output |
| 281 | return GrFragmentProcessor::MulOutputByInputAlpha(std::move(innerLightFP)); |
| 282 | } else { |
| 283 | // FP is wrapped because paint comes in unpremul'd to fragment shader, but LightingFP |
| 284 | // expects premul'd color. |
| 285 | return GrFragmentProcessor::PremulInput(sk_make_sp<LightingFP>(std::move(normalFP), |
| 286 | fLights)); |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 287 | } |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | #endif |
| 291 | |
| 292 | //////////////////////////////////////////////////////////////////////////// |
| 293 | |
| 294 | bool SkLightingShaderImpl::isOpaque() const { |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 295 | return (fDiffuseShader ? fDiffuseShader->isOpaque() : false); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 296 | } |
| 297 | |
dvonbeck | 8811e40 | 2016-06-16 12:39:25 -0700 | [diff] [blame] | 298 | SkLightingShaderImpl::LightingShaderContext::LightingShaderContext( |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 299 | const SkLightingShaderImpl& shader, const ContextRec& rec, |
| 300 | SkShader::Context* diffuseContext, SkNormalSource::Provider* normalProvider, |
| 301 | void* heapAllocated) |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 302 | : INHERITED(shader, rec) |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 303 | , fDiffuseContext(diffuseContext) |
dvonbeck | 12c4fc2 | 2016-06-27 11:40:45 -0700 | [diff] [blame] | 304 | , fNormalProvider(normalProvider) |
| 305 | , fHeapAllocated(heapAllocated) { |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 306 | bool isOpaque = shader.isOpaque(); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 307 | |
| 308 | // update fFlags |
| 309 | uint32_t flags = 0; |
| 310 | if (isOpaque && (255 == this->getPaintAlpha())) { |
| 311 | flags |= kOpaqueAlpha_Flag; |
| 312 | } |
| 313 | |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 314 | fPaintColor = rec.fPaint->getColor(); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 315 | fFlags = flags; |
| 316 | } |
| 317 | |
| 318 | SkLightingShaderImpl::LightingShaderContext::~LightingShaderContext() { |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 319 | // The dependencies have been created outside of the context on memory that was allocated by |
| 320 | // the onCreateContext() method. Call the destructors and free the memory. |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 321 | if (fDiffuseContext) { |
| 322 | fDiffuseContext->~Context(); |
| 323 | } |
dvonbeck | 12c4fc2 | 2016-06-27 11:40:45 -0700 | [diff] [blame] | 324 | fNormalProvider->~Provider(); |
| 325 | |
| 326 | sk_free(fHeapAllocated); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | static inline SkPMColor convert(SkColor3f color, U8CPU a) { |
| 330 | if (color.fX <= 0.0f) { |
| 331 | color.fX = 0.0f; |
| 332 | } else if (color.fX >= 255.0f) { |
| 333 | color.fX = 255.0f; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 334 | } |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 335 | |
| 336 | if (color.fY <= 0.0f) { |
| 337 | color.fY = 0.0f; |
| 338 | } else if (color.fY >= 255.0f) { |
| 339 | color.fY = 255.0f; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 340 | } |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 341 | |
| 342 | if (color.fZ <= 0.0f) { |
| 343 | color.fZ = 0.0f; |
| 344 | } else if (color.fZ >= 255.0f) { |
| 345 | color.fZ = 255.0f; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 346 | } |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 347 | |
| 348 | return SkPreMultiplyARGB(a, (int) color.fX, (int) color.fY, (int) color.fZ); |
| 349 | } |
| 350 | |
| 351 | // larger is better (fewer times we have to loop), but we shouldn't |
| 352 | // take up too much stack-space (each one here costs 16 bytes) |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 353 | #define BUFFER_MAX 16 |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 354 | void SkLightingShaderImpl::LightingShaderContext::shadeSpan(int x, int y, |
| 355 | SkPMColor result[], int count) { |
| 356 | const SkLightingShaderImpl& lightShader = static_cast<const SkLightingShaderImpl&>(fShader); |
| 357 | |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 358 | SkPMColor diffuse[BUFFER_MAX]; |
dvonbeck | 12c4fc2 | 2016-06-27 11:40:45 -0700 | [diff] [blame] | 359 | SkPoint3 normals[BUFFER_MAX]; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 360 | |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 361 | SkColor diffColor = fPaintColor; |
| 362 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 363 | do { |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 364 | int n = SkTMin(count, BUFFER_MAX); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 365 | |
dvonbeck | 12c4fc2 | 2016-06-27 11:40:45 -0700 | [diff] [blame] | 366 | fNormalProvider->fillScanLine(x, y, normals, n); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 367 | |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 368 | if (fDiffuseContext) { |
| 369 | fDiffuseContext->shadeSpan(x, y, diffuse, n); |
| 370 | } |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 371 | |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 372 | for (int i = 0; i < n; ++i) { |
| 373 | if (fDiffuseContext) { |
| 374 | diffColor = SkUnPreMultiply::PMColorToColor(diffuse[i]); |
| 375 | } |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 376 | |
| 377 | SkColor3f accum = SkColor3f::Make(0.0f, 0.0f, 0.0f); |
| 378 | // This is all done in linear unpremul color space (each component 0..255.0f though) |
| 379 | for (int l = 0; l < lightShader.fLights->numLights(); ++l) { |
robertphillips | 71e0552 | 2016-05-31 12:08:25 -0700 | [diff] [blame] | 380 | const SkLights::Light& light = lightShader.fLights->light(l); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 381 | |
robertphillips | 71e0552 | 2016-05-31 12:08:25 -0700 | [diff] [blame] | 382 | if (SkLights::Light::kAmbient_LightType == light.type()) { |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 383 | accum += light.color().makeScale(255.0f); |
| 384 | } else { |
dvonbeck | 12c4fc2 | 2016-06-27 11:40:45 -0700 | [diff] [blame] | 385 | SkScalar NdotL = normals[i].dot(light.dir()); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 386 | if (NdotL < 0.0f) { |
| 387 | NdotL = 0.0f; |
| 388 | } |
| 389 | |
| 390 | accum.fX += light.color().fX * SkColorGetR(diffColor) * NdotL; |
| 391 | accum.fY += light.color().fY * SkColorGetG(diffColor) * NdotL; |
| 392 | accum.fZ += light.color().fZ * SkColorGetB(diffColor) * NdotL; |
| 393 | } |
| 394 | } |
| 395 | |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 396 | // convert() premultiplies the accumulate color with alpha |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 397 | result[i] = convert(accum, SkColorGetA(diffColor)); |
| 398 | } |
| 399 | |
| 400 | result += n; |
| 401 | x += n; |
| 402 | count -= n; |
| 403 | } while (count > 0); |
| 404 | } |
| 405 | |
| 406 | //////////////////////////////////////////////////////////////////////////// |
| 407 | |
| 408 | #ifndef SK_IGNORE_TO_STRING |
| 409 | void SkLightingShaderImpl::toString(SkString* str) const { |
| 410 | str->appendf("LightingShader: ()"); |
| 411 | } |
| 412 | #endif |
| 413 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 414 | sk_sp<SkFlattenable> SkLightingShaderImpl::CreateProc(SkReadBuffer& buf) { |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 415 | |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 416 | // Discarding SkShader flattenable params |
| 417 | bool hasLocalMatrix = buf.readBool(); |
| 418 | SkAssertResult(!hasLocalMatrix); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 419 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 420 | int numLights = buf.readInt(); |
| 421 | |
robertphillips | 71e0552 | 2016-05-31 12:08:25 -0700 | [diff] [blame] | 422 | SkLights::Builder builder; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 423 | |
| 424 | for (int l = 0; l < numLights; ++l) { |
| 425 | bool isAmbient = buf.readBool(); |
| 426 | |
| 427 | SkColor3f color; |
| 428 | if (!buf.readScalarArray(&color.fX, 3)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 429 | return nullptr; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | if (isAmbient) { |
robertphillips | 71e0552 | 2016-05-31 12:08:25 -0700 | [diff] [blame] | 433 | builder.add(SkLights::Light(color)); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 434 | } else { |
| 435 | SkVector3 dir; |
| 436 | if (!buf.readScalarArray(&dir.fX, 3)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 437 | return nullptr; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 438 | } |
robertphillips | 71e0552 | 2016-05-31 12:08:25 -0700 | [diff] [blame] | 439 | builder.add(SkLights::Light(color, dir)); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
robertphillips | 71e0552 | 2016-05-31 12:08:25 -0700 | [diff] [blame] | 443 | sk_sp<SkLights> lights(builder.finish()); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 444 | |
dvonbeck | 12c4fc2 | 2016-06-27 11:40:45 -0700 | [diff] [blame] | 445 | sk_sp<SkNormalSource> normalSource(buf.readFlattenable<SkNormalSource>()); |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 446 | |
| 447 | bool hasDiffuse = buf.readBool(); |
| 448 | sk_sp<SkShader> diffuseShader = nullptr; |
| 449 | if (hasDiffuse) { |
| 450 | diffuseShader = buf.readFlattenable<SkShader>(); |
| 451 | } |
dvonbeck | 8811e40 | 2016-06-16 12:39:25 -0700 | [diff] [blame] | 452 | |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 453 | return sk_make_sp<SkLightingShaderImpl>(std::move(diffuseShader), std::move(normalSource), |
| 454 | std::move(lights)); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | void SkLightingShaderImpl::flatten(SkWriteBuffer& buf) const { |
| 458 | this->INHERITED::flatten(buf); |
| 459 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 460 | buf.writeInt(fLights->numLights()); |
| 461 | for (int l = 0; l < fLights->numLights(); ++l) { |
robertphillips | 71e0552 | 2016-05-31 12:08:25 -0700 | [diff] [blame] | 462 | const SkLights::Light& light = fLights->light(l); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 463 | |
robertphillips | 71e0552 | 2016-05-31 12:08:25 -0700 | [diff] [blame] | 464 | bool isAmbient = SkLights::Light::kAmbient_LightType == light.type(); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 465 | |
| 466 | buf.writeBool(isAmbient); |
| 467 | buf.writeScalarArray(&light.color().fX, 3); |
| 468 | if (!isAmbient) { |
| 469 | buf.writeScalarArray(&light.dir().fX, 3); |
| 470 | } |
| 471 | } |
dvonbeck | 8811e40 | 2016-06-16 12:39:25 -0700 | [diff] [blame] | 472 | |
| 473 | buf.writeFlattenable(fNormalSource.get()); |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 474 | buf.writeBool(fDiffuseShader); |
| 475 | if (fDiffuseShader) { |
| 476 | buf.writeFlattenable(fDiffuseShader.get()); |
| 477 | } |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 478 | } |
| 479 | |
dvonbeck | 12c4fc2 | 2016-06-27 11:40:45 -0700 | [diff] [blame] | 480 | size_t SkLightingShaderImpl::onContextSize(const ContextRec& rec) const { |
| 481 | return sizeof(LightingShaderContext); |
reed | 773ceda | 2016-03-03 18:18:25 -0800 | [diff] [blame] | 482 | } |
| 483 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 484 | SkShader::Context* SkLightingShaderImpl::onCreateContext(const ContextRec& rec, |
| 485 | void* storage) const { |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 486 | size_t heapRequired = (fDiffuseShader ? fDiffuseShader->contextSize(rec) : 0) + |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 487 | fNormalSource->providerSize(rec); |
dvonbeck | 12c4fc2 | 2016-06-27 11:40:45 -0700 | [diff] [blame] | 488 | void* heapAllocated = sk_malloc_throw(heapRequired); |
dvonbeck | 2c80711 | 2016-06-27 09:30:19 -0700 | [diff] [blame] | 489 | |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 490 | void* diffuseContextStorage = heapAllocated; |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 491 | void* normalProviderStorage = (char*) diffuseContextStorage + |
| 492 | (fDiffuseShader ? fDiffuseShader->contextSize(rec) : 0); |
| 493 | |
| 494 | SkShader::Context *diffuseContext = nullptr; |
| 495 | if (fDiffuseShader) { |
| 496 | diffuseContext = fDiffuseShader->createContext(rec, diffuseContextStorage); |
| 497 | if (!diffuseContext) { |
| 498 | sk_free(heapAllocated); |
| 499 | return nullptr; |
| 500 | } |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 501 | } |
| 502 | |
dvonbeck | 12c4fc2 | 2016-06-27 11:40:45 -0700 | [diff] [blame] | 503 | SkNormalSource::Provider* normalProvider = fNormalSource->asProvider(rec, |
| 504 | normalProviderStorage); |
| 505 | if (!normalProvider) { |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 506 | diffuseContext->~Context(); |
dvonbeck | 12c4fc2 | 2016-06-27 11:40:45 -0700 | [diff] [blame] | 507 | sk_free(heapAllocated); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 508 | return nullptr; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 509 | } |
| 510 | |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 511 | return new (storage) LightingShaderContext(*this, rec, diffuseContext, normalProvider, |
dvonbeck | 12c4fc2 | 2016-06-27 11:40:45 -0700 | [diff] [blame] | 512 | heapAllocated); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /////////////////////////////////////////////////////////////////////////////// |
| 516 | |
dvonbeck | 6af677f | 2016-07-10 18:38:33 -0700 | [diff] [blame] | 517 | sk_sp<SkShader> SkLightingShader::Make(sk_sp<SkShader> diffuseShader, |
| 518 | sk_sp<SkNormalSource> normalSource, |
| 519 | sk_sp<SkLights> lights) { |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 520 | if (!normalSource) { |
| 521 | normalSource = SkNormalSource::MakeFlat(); |
dvonbeck | 5b794fa | 2016-07-06 13:58:36 -0700 | [diff] [blame] | 522 | } |
dvonbeck | 12c4fc2 | 2016-06-27 11:40:45 -0700 | [diff] [blame] | 523 | |
dvonbeck | e6347ad | 2016-07-09 17:08:15 -0700 | [diff] [blame] | 524 | return sk_make_sp<SkLightingShaderImpl>(std::move(diffuseShader), std::move(normalSource), |
| 525 | std::move(lights)); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | /////////////////////////////////////////////////////////////////////////////// |
| 529 | |
| 530 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader) |
| 531 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl) |
| 532 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| 533 | |
| 534 | /////////////////////////////////////////////////////////////////////////////// |