OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 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 |
| 8 #include "GrGammaEffect.h" |
| 9 |
| 10 #include "GrContext.h" |
| 11 #include "GrCoordTransform.h" |
| 12 #include "GrFragmentProcessor.h" |
| 13 #include "GrInvariantOutput.h" |
| 14 #include "GrProcessor.h" |
| 15 #include "glsl/GrGLSLFragmentProcessor.h" |
| 16 #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 17 |
| 18 class GrGLGammaEffect : public GrGLSLFragmentProcessor { |
| 19 public: |
| 20 void emitCode(EmitArgs& args) override { |
| 21 const GrGammaEffect& ge = args.fFp.cast<GrGammaEffect>(); |
| 22 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 23 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 24 |
| 25 const char* gammaUniName = nullptr; |
| 26 if (!ge.gammaIsSRGB()) { |
| 27 fGammaUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kFloa
t_GrSLType, |
| 28 kDefault_GrSLPrecision, "Gamm
a", &gammaUniName); |
| 29 } |
| 30 |
| 31 GrGLSLShaderVar tmpVar("tmpColor", kVec4f_GrSLType, 0, kHigh_GrSLPrecisi
on); |
| 32 SkString tmpDecl; |
| 33 tmpVar.appendDecl(args.fGLSLCaps, &tmpDecl); |
| 34 |
| 35 SkString srgbFuncName; |
| 36 if (ge.gammaIsSRGB()) { |
| 37 static const GrGLSLShaderVar gSrgbArgs[] = { |
| 38 GrGLSLShaderVar("x", kFloat_GrSLType), |
| 39 }; |
| 40 |
| 41 fragBuilder->emitFunction(kFloat_GrSLType, |
| 42 "linear_to_srgb", |
| 43 SK_ARRAY_COUNT(gSrgbArgs), |
| 44 gSrgbArgs, |
| 45 "return (x <= 0.0031308) ? (x * 12.92) " |
| 46 ": (1.055 * pow(x, 0.416666667) - 0.055)
;", |
| 47 &srgbFuncName); |
| 48 } |
| 49 |
| 50 fragBuilder->codeAppendf("%s;", tmpDecl.c_str()); |
| 51 |
| 52 fragBuilder->codeAppendf("%s = ", tmpVar.c_str()); |
| 53 fragBuilder->appendTextureLookup(args.fTexSamplers[0], args.fCoords[0].c
_str(), |
| 54 args.fCoords[0].getType()); |
| 55 fragBuilder->codeAppend(";"); |
| 56 |
| 57 if (ge.gammaIsSRGB()) { |
| 58 fragBuilder->codeAppendf("%s = vec4(%s(%s.r), %s(%s.g), %s(%s.b), %s
.a);", |
| 59 args.fOutputColor, |
| 60 srgbFuncName.c_str(), tmpVar.c_str(), |
| 61 srgbFuncName.c_str(), tmpVar.c_str(), |
| 62 srgbFuncName.c_str(), tmpVar.c_str(), |
| 63 tmpVar.c_str()); |
| 64 } else { |
| 65 fragBuilder->codeAppendf("%s = vec4(pow(%s.rgb, vec3(%s)), %s.a);", |
| 66 args.fOutputColor, tmpVar.c_str(), gamma
UniName, |
| 67 tmpVar.c_str()); |
| 68 } |
| 69 } |
| 70 |
| 71 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& pro
c) override { |
| 72 const GrGammaEffect& ge = proc.cast<GrGammaEffect>(); |
| 73 if (!ge.gammaIsSRGB()) { |
| 74 pdman.set1f(fGammaUni, ge.gamma()); |
| 75 } |
| 76 } |
| 77 |
| 78 static inline void GenKey(const GrProcessor& processor, const GrGLSLCaps&, |
| 79 GrProcessorKeyBuilder* b) { |
| 80 const GrGammaEffect& ge = processor.cast<GrGammaEffect>(); |
| 81 uint32_t key = ge.gammaIsSRGB() ? 0x1 : 0x0; |
| 82 b->add32(key); |
| 83 } |
| 84 |
| 85 private: |
| 86 GrGLSLProgramDataManager::UniformHandle fGammaUni; |
| 87 |
| 88 typedef GrGLSLFragmentProcessor INHERITED; |
| 89 }; |
| 90 |
| 91 /////////////////////////////////////////////////////////////////////////////// |
| 92 |
| 93 GrGammaEffect::GrGammaEffect(GrTexture* texture, SkScalar gamma) |
| 94 : INHERITED(texture, GrCoordTransform::MakeDivByTextureWHMatrix(texture)) { |
| 95 this->initClassID<GrGammaEffect>(); |
| 96 |
| 97 fGamma = gamma; |
| 98 fGammaIsSRGB = SkScalarNearlyEqual(gamma, 1.0f / 2.2f); |
| 99 } |
| 100 |
| 101 bool GrGammaEffect::onIsEqual(const GrFragmentProcessor& s) const { |
| 102 const GrGammaEffect& other = s.cast<GrGammaEffect>(); |
| 103 return |
| 104 other.fGammaIsSRGB == fGammaIsSRGB && |
| 105 other.fGamma == fGamma; |
| 106 } |
| 107 |
| 108 void GrGammaEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
| 109 inout->setToUnknown(GrInvariantOutput::kWill_ReadInput); |
| 110 } |
| 111 |
| 112 /////////////////////////////////////////////////////////////////////////////// |
| 113 |
| 114 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrGammaEffect); |
| 115 |
| 116 const GrFragmentProcessor* GrGammaEffect::TestCreate(GrProcessorTestData* d) { |
| 117 // We want to be sure and test sRGB sometimes |
| 118 bool srgb = d->fRandom->nextBool(); |
| 119 return new GrGammaEffect(d->fTextures[GrProcessorUnitTest::kSkiaPMTextureIdx
], |
| 120 srgb ? 1.0f / 2.2f : d->fRandom->nextRangeScalar(0.
5f, 2.0f)); |
| 121 } |
| 122 |
| 123 /////////////////////////////////////////////////////////////////////////////// |
| 124 |
| 125 void GrGammaEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
| 126 GrProcessorKeyBuilder* b) const { |
| 127 GrGLGammaEffect::GenKey(*this, caps, b); |
| 128 } |
| 129 |
| 130 GrGLSLFragmentProcessor* GrGammaEffect::onCreateGLSLInstance() const { |
| 131 return new GrGLGammaEffect(); |
| 132 } |
| 133 |
| 134 const GrFragmentProcessor* GrGammaEffect::Create(GrTexture* texture, SkScalar ga
mma) { |
| 135 return new GrGammaEffect(texture, gamma); |
| 136 } |
OLD | NEW |