Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkNormalBevelSource.h" | 8 #include "SkNormalBevelSource.h" |
| 9 | 9 |
| 10 #include "SkNormalSource.h" | 10 #include "SkNormalSource.h" |
| 11 #include "SkNormalSourcePriv.h" | 11 #include "SkNormalSourcePriv.h" |
| 12 #include "SkPoint3.h" | 12 #include "SkPoint3.h" |
| 13 #include "SkReadBuffer.h" | 13 #include "SkReadBuffer.h" |
| 14 #include "SkWriteBuffer.h" | 14 #include "SkWriteBuffer.h" |
| 15 | 15 |
| 16 #if SK_SUPPORT_GPU | 16 #if SK_SUPPORT_GPU |
| 17 #include "GrInvariantOutput.h" | 17 #include "GrInvariantOutput.h" |
| 18 #include "glsl/GrGLSLFragmentProcessor.h" | 18 #include "glsl/GrGLSLFragmentProcessor.h" |
| 19 #include "glsl/GrGLSLFragmentShaderBuilder.h" | 19 #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 20 #include "SkGr.h" | 20 #include "SkGr.h" |
| 21 | 21 |
| 22 /** \class NormalBevelFP | |
| 23 * | |
| 24 * Fragment processor for the SkNormalBevelSource. | |
| 25 * | |
| 26 * @param bevelType type of the bevel | |
| 27 * @param bevelWidth width of the bevel in device space | |
| 28 * @param bevelHeight height of the bevel in device space | |
| 29 */ | |
| 22 class NormalBevelFP : public GrFragmentProcessor { | 30 class NormalBevelFP : public GrFragmentProcessor { |
| 23 public: | 31 public: |
| 24 NormalBevelFP(SkNormalSource::BevelType type, SkScalar width, SkScalar heigh t) | 32 NormalBevelFP(SkNormalSource::BevelType bevelType, SkScalar bevelWidth, SkSc alar bevelHeight) |
| 25 : fType(type) | 33 : fBevelType(bevelType) |
| 26 , fWidth(width) | 34 , fBevelWidth(bevelWidth) |
| 27 , fHeight(height) { | 35 , fBevelHeight(bevelHeight) { |
| 28 this->initClassID<NormalBevelFP>(); | 36 this->initClassID<NormalBevelFP>(); |
| 29 | 37 |
| 30 fUsesDistanceVectorField = true; | 38 fUsesDistanceVectorField = true; |
| 31 } | 39 } |
| 32 | 40 |
| 33 class GLSLNormalBevelFP : public GLSLNormalFP { | 41 class GLSLNormalBevelFP : public GLSLNormalFP { |
| 34 public: | 42 public: |
| 35 GLSLNormalBevelFP() { | 43 GLSLNormalBevelFP() { |
| 36 fPrevWidth = SkFloatToScalar(0.0f); | 44 fPrevWidth = SkFloatToScalar(0.0f); |
| 37 fPrevHeight = SkFloatToScalar(0.0f); | 45 fPrevHeight = SkFloatToScalar(0.0f); |
| 38 } | 46 } |
| 39 | 47 |
| 40 void onEmitCode(EmitArgs& args) override { | 48 void onEmitCode(EmitArgs& args) override { |
| 41 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; | 49 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 50 const NormalBevelFP& fp = args.fFp.cast<NormalBevelFP>(); | |
| 42 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; | 51 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 43 | 52 |
| 44 const char* widthUniName = nullptr; | 53 const char* widthUniName = nullptr; |
| 45 fWidthUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kFloa t_GrSLType, | 54 fWidthUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kFloa t_GrSLType, |
| 46 kDefault_GrSLPrecision, "Width", &widthUniName); | 55 kDefault_GrSLPrecision, "Width", &widthUniName); |
| 47 | 56 |
| 48 const char* heightUniName = nullptr; | 57 const char* heightUniName = nullptr; |
| 49 fHeightUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kFlo at_GrSLType, | 58 fHeightUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kFlo at_GrSLType, |
| 50 kDefault_GrSLPrecision, "Height", &heightUniName); | 59 kDefault_GrSLPrecision, "Height", &heightUniName); |
| 51 | 60 |
| 52 fragBuilder->codeAppendf("%s = vec4(0.0, 0.0, 1.0, 0.0);", args.fOut putColor); | 61 // Here we are splitting the distance vector into length and normali zed direction |
| 62 // TODO: Output these values from the geometry processor frag code i nstead of the vector | |
| 63 fragBuilder->codeAppendf("float dv_length = length(%s);", | |
| 64 fragBuilder->distanceVectorName()); | |
| 65 fragBuilder->codeAppendf("vec2 dv_norm = normalize(%s);", | |
| 66 fragBuilder->distanceVectorName()); | |
| 67 | |
| 68 fragBuilder->codeAppend( "vec3 normal;"); | |
| 69 fragBuilder->codeAppendf("if (dv_length >= %s) {", widthUniName); | |
| 70 fragBuilder->codeAppend( " normal = vec3(0.0, 0.0, 1.0);"); | |
| 71 fragBuilder->codeAppend( "} else {"); | |
| 72 this->emitMath(fragBuilder, fp.fBevelType, widthUniName, heightUniNa me); | |
| 73 fragBuilder->codeAppend( "}"); | |
| 74 fragBuilder->codeAppendf("%s = vec4(normal, 0.0);", args.fOutputColo r); | |
| 53 } | 75 } |
| 54 | 76 |
| 55 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, | 77 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, |
| 56 GrProcessorKeyBuilder* b) { | 78 GrProcessorKeyBuilder* b) { |
| 57 const NormalBevelFP& fp = proc.cast<NormalBevelFP>(); | 79 const NormalBevelFP& fp = proc.cast<NormalBevelFP>(); |
| 58 b->add32(static_cast<int>(fp.fType)); | 80 b->add32(static_cast<int>(fp.fBevelType)); |
| 59 } | 81 } |
| 60 | 82 |
| 61 protected: | 83 protected: |
| 62 void setNormalData(const GrGLSLProgramDataManager& pdman, | 84 void setNormalData(const GrGLSLProgramDataManager& pdman, |
| 63 const GrProcessor& proc) override { | 85 const GrProcessor& proc) override { |
| 64 const NormalBevelFP& normalBevelFP = proc.cast<NormalBevelFP>(); | 86 const NormalBevelFP& normalBevelFP = proc.cast<NormalBevelFP>(); |
| 65 | 87 |
| 66 if (fPrevWidth != normalBevelFP.fWidth) { | 88 if (fPrevWidth != normalBevelFP.fBevelWidth) { |
| 67 pdman.set1f(fWidthUni, normalBevelFP.fWidth); | 89 pdman.set1f(fWidthUni, normalBevelFP.fBevelWidth); |
| 68 fPrevWidth = normalBevelFP.fWidth; | 90 fPrevWidth = normalBevelFP.fBevelWidth; |
| 69 } | 91 } |
| 70 if (fPrevHeight != normalBevelFP.fHeight) { | 92 if (fPrevHeight != normalBevelFP.fBevelHeight) { |
| 71 pdman.set1f(fHeightUni, normalBevelFP.fHeight); | 93 pdman.set1f(fHeightUni, normalBevelFP.fBevelHeight); |
| 72 fPrevHeight = normalBevelFP.fHeight; | 94 fPrevHeight = normalBevelFP.fBevelHeight; |
| 73 } | 95 } |
| 74 } | 96 } |
| 75 | 97 |
| 98 void emitMath(GrGLSLFPFragmentBuilder* fb, SkNormalSource::BevelType typ e, | |
| 99 const char* width, const char* height) { | |
| 100 const char* distanceVector = fb->distanceVectorName(); | |
| 101 | |
| 102 // Setting t to the distance from the end of the bevel as opposed to the beginning if | |
| 103 // the bevel is rounded in. | |
| 104 if ( type == SkNormalSource::BevelType::kRoundedIn ) { | |
|
egdaniel
2016/08/04 03:08:17
this should be inside roundOut/In section right?
dvonbeck
2016/08/04 17:57:25
I guess it makes more sense over there. Done.
| |
| 105 fb->codeAppendf("float t = %s - dv_length;", width); | |
| 106 } else if (type == SkNormalSource::BevelType::kRoundedOut) { | |
| 107 fb->codeAppendf("float t = dv_length;"); | |
| 108 } | |
| 109 | |
| 110 switch (type) { | |
| 111 case SkNormalSource::BevelType::kLinear: | |
| 112 fb->codeAppendf("normal = normalize(%s*vec3(%s, 0.0) + vec3( 0.0, 0.0, %s));", | |
|
egdaniel
2016/08/04 03:08:17
I don't think this is correct. Without going into
dvonbeck
2016/08/04 17:57:25
Done.
| |
| 113 height, distanceVector, width); | |
| 114 break; | |
| 115 case SkNormalSource::BevelType::kRoundedOut: | |
| 116 // Fall through | |
| 117 case SkNormalSource::BevelType::kRoundedIn: | |
| 118 fb->codeAppendf("float rootTOverW = sqrt(t/%s);", width); | |
| 119 | |
| 120 // Calculating the d- and z-components of the normal, where 'd' is the axis | |
| 121 // co-linear to the distance vector. Equation was derived fr om the formula for | |
| 122 // a bezier curve. | |
| 123 fb->codeAppendf("vec2 unnormalizedNormal_dz = vec2(%s*(1.0-r ootTOverW), " | |
|
egdaniel
2016/08/04 03:08:17
just trying to confirm in my head, since we multip
dvonbeck
2016/08/04 17:57:25
Yes. I think it's more that my math explicitly bui
| |
| 124 "%s*rootT OverW);", | |
| 125 height, width); | |
| 126 fb->codeAppendf("vec2 normal_dz = normalize(unnormalizedNorm al_dz);"); | |
| 127 | |
| 128 // Multiplying the d-component of the normal with the normal ized distance vector | |
| 129 // splitting it in x- and y-components | |
| 130 fb->codeAppendf("normal = vec3(normal_dz.x*dv_norm, normal_d z.y);"); | |
| 131 | |
| 132 break; | |
| 133 default: | |
| 134 SkDEBUGFAIL("Invalid bevel type passed to emitMath"); | |
| 135 } | |
| 136 } | |
| 137 | |
| 76 private: | 138 private: |
| 77 SkScalar fPrevWidth; | 139 SkScalar fPrevWidth; |
| 78 GrGLSLProgramDataManager::UniformHandle fWidthUni; | 140 GrGLSLProgramDataManager::UniformHandle fWidthUni; |
| 79 | 141 |
| 80 SkScalar fPrevHeight; | 142 SkScalar fPrevHeight; |
| 81 GrGLSLProgramDataManager::UniformHandle fHeightUni; | 143 GrGLSLProgramDataManager::UniformHandle fHeightUni; |
| 82 }; | 144 }; |
| 83 | 145 |
| 84 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { | 146 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { |
| 85 GLSLNormalBevelFP::GenKey(*this, caps, b); | 147 GLSLNormalBevelFP::GenKey(*this, caps, b); |
| 86 } | 148 } |
| 87 | 149 |
| 88 const char* name() const override { return "NormalBevelFP"; } | 150 const char* name() const override { return "NormalBevelFP"; } |
| 89 | 151 |
| 90 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { | 152 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
| 91 inout->setToUnknown(GrInvariantOutput::ReadInput::kWillNot_ReadInput); | 153 inout->setToUnknown(GrInvariantOutput::ReadInput::kWillNot_ReadInput); |
| 92 } | 154 } |
| 93 | 155 |
| 94 private: | 156 private: |
| 95 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLSLNormalBevelFP; } | 157 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLSLNormalBevelFP; } |
| 96 | 158 |
| 97 bool onIsEqual(const GrFragmentProcessor& proc) const override { | 159 bool onIsEqual(const GrFragmentProcessor& proc) const override { |
| 98 const NormalBevelFP& normalBevelFP = proc.cast<NormalBevelFP>(); | 160 const NormalBevelFP& normalBevelFP = proc.cast<NormalBevelFP>(); |
| 99 return fType == normalBevelFP.fType && | 161 return fBevelType == normalBevelFP.fBevelType && |
| 100 fWidth == normalBevelFP.fWidth && | 162 fBevelWidth == normalBevelFP.fBevelWidth && |
| 101 fHeight == normalBevelFP.fHeight; | 163 fBevelHeight == normalBevelFP.fBevelHeight; |
| 102 } | 164 } |
| 103 | 165 |
| 104 SkNormalSource::BevelType fType; | 166 SkNormalSource::BevelType fBevelType; |
| 105 SkScalar fWidth; | 167 SkScalar fBevelWidth; |
| 106 SkScalar fHeight; | 168 SkScalar fBevelHeight; |
| 107 }; | 169 }; |
| 108 | 170 |
| 109 sk_sp<GrFragmentProcessor> SkNormalBevelSourceImpl::asFragmentProcessor( | 171 sk_sp<GrFragmentProcessor> SkNormalBevelSourceImpl::asFragmentProcessor( |
| 110 const SkShader::AsFPArgs&) const { | 172 const SkShader::AsFPArgs& args) const { |
| 111 | 173 |
| 112 return sk_make_sp<NormalBevelFP>(fType, fWidth, fHeight); | 174 SkScalar maxScale = args.fViewMatrix->getMaxScale(); |
| 175 | |
| 176 // Providing device-space width and height | |
| 177 return sk_make_sp<NormalBevelFP>(fType, maxScale * fWidth, maxScale * fHeigh t); | |
| 113 } | 178 } |
| 114 | 179 |
| 115 #endif // SK_SUPPORT_GPU | 180 #endif // SK_SUPPORT_GPU |
| 116 | 181 |
| 117 //////////////////////////////////////////////////////////////////////////// | 182 //////////////////////////////////////////////////////////////////////////// |
| 118 | 183 |
| 119 SkNormalBevelSourceImpl::Provider::Provider() {} | 184 SkNormalBevelSourceImpl::Provider::Provider() {} |
| 120 | 185 |
| 121 SkNormalBevelSourceImpl::Provider::~Provider() {} | 186 SkNormalBevelSourceImpl::Provider::~Provider() {} |
| 122 | 187 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 160 sk_sp<SkNormalSource> SkNormalSource::MakeBevel(BevelType type, SkScalar width, SkScalar height) { | 225 sk_sp<SkNormalSource> SkNormalSource::MakeBevel(BevelType type, SkScalar width, SkScalar height) { |
| 161 /* TODO make sure this checks are tolerant enough to account for loss of con version when GPUs | 226 /* TODO make sure this checks are tolerant enough to account for loss of con version when GPUs |
| 162 use 16-bit float types. We don't want to assume stuff is non-zero on the GPU and be wrong.*/ | 227 use 16-bit float types. We don't want to assume stuff is non-zero on the GPU and be wrong.*/ |
| 163 SkASSERT(width > 0.0f && !SkScalarNearlyZero(width)); | 228 SkASSERT(width > 0.0f && !SkScalarNearlyZero(width)); |
| 164 if (SkScalarNearlyZero(height)) { | 229 if (SkScalarNearlyZero(height)) { |
| 165 return SkNormalSource::MakeFlat(); | 230 return SkNormalSource::MakeFlat(); |
| 166 } | 231 } |
| 167 | 232 |
| 168 return sk_make_sp<SkNormalBevelSourceImpl>(type, width, height); | 233 return sk_make_sp<SkNormalBevelSourceImpl>(type, width, height); |
| 169 } | 234 } |
| OLD | NEW |