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 "GrGammaEffect.h" | 8 #include "GrGammaEffect.h" |
9 | 9 |
10 #include "GrContext.h" | 10 #include "GrContext.h" |
11 #include "GrCoordTransform.h" | 11 #include "GrCoordTransform.h" |
12 #include "GrFragmentProcessor.h" | 12 #include "GrFragmentProcessor.h" |
13 #include "GrInvariantOutput.h" | 13 #include "GrInvariantOutput.h" |
14 #include "GrProcessor.h" | 14 #include "GrProcessor.h" |
15 #include "glsl/GrGLSLFragmentProcessor.h" | 15 #include "glsl/GrGLSLFragmentProcessor.h" |
16 #include "glsl/GrGLSLFragmentShaderBuilder.h" | 16 #include "glsl/GrGLSLFragmentShaderBuilder.h" |
17 | 17 |
18 class GrGLGammaEffect : public GrGLSLFragmentProcessor { | 18 class GrGLGammaEffect : public GrGLSLFragmentProcessor { |
19 public: | 19 public: |
20 void emitCode(EmitArgs& args) override { | 20 void emitCode(EmitArgs& args) override { |
21 const GrGammaEffect& ge = args.fFp.cast<GrGammaEffect>(); | 21 const GrGammaEffect& ge = args.fFp.cast<GrGammaEffect>(); |
22 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; | 22 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
23 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; | 23 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
24 | 24 |
25 const char* gammaUniName = nullptr; | 25 const char* gammaUniName = nullptr; |
26 if (GrGammaEffect::Mode::kExponential == ge.mode()) { | 26 if (!ge.gammaIsSRGB()) { |
27 fGammaUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kFloa
t_GrSLType, | 27 fGammaUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kFloa
t_GrSLType, |
28 kDefault_GrSLPrecision, "Gamm
a", &gammaUniName); | 28 kDefault_GrSLPrecision, "Gamm
a", &gammaUniName); |
29 } | 29 } |
30 | 30 |
| 31 GrGLSLShaderVar tmpVar("tmpColor", kVec4f_GrSLType, 0, kHigh_GrSLPrecisi
on); |
| 32 SkString tmpDecl; |
| 33 tmpVar.appendDecl(args.fGLSLCaps, &tmpDecl); |
| 34 |
31 SkString srgbFuncName; | 35 SkString srgbFuncName; |
32 static const GrGLSLShaderVar gSrgbArgs[] = { | 36 if (ge.gammaIsSRGB()) { |
33 GrGLSLShaderVar("x", kFloat_GrSLType), | 37 static const GrGLSLShaderVar gSrgbArgs[] = { |
34 }; | 38 GrGLSLShaderVar("x", kFloat_GrSLType), |
35 switch (ge.mode()) { | 39 }; |
36 case GrGammaEffect::Mode::kLinearToSRGB: | 40 |
37 fragBuilder->emitFunction(kFloat_GrSLType, | 41 fragBuilder->emitFunction(kFloat_GrSLType, |
38 "linear_to_srgb", | 42 "linear_to_srgb", |
39 SK_ARRAY_COUNT(gSrgbArgs), | 43 SK_ARRAY_COUNT(gSrgbArgs), |
40 gSrgbArgs, | 44 gSrgbArgs, |
41 "return (x <= 0.0031308) ? (x * 12.92)
" | 45 "return (x <= 0.0031308) ? (x * 12.92) " |
42 ": (1.055 * pow(x, 0.416666667) - 0.05
5);", | 46 ": (1.055 * pow(x, 0.416666667) - 0.055)
;", |
43 &srgbFuncName); | 47 &srgbFuncName); |
44 break; | |
45 case GrGammaEffect::Mode::kSRGBToLinear: | |
46 fragBuilder->emitFunction(kFloat_GrSLType, | |
47 "srgb_to_linear", | |
48 SK_ARRAY_COUNT(gSrgbArgs), | |
49 gSrgbArgs, | |
50 "return (x <= 0.04045) ? (x / 12.92) " | |
51 ": powf((x + 0.055) / 1.055, 2.4);", | |
52 &srgbFuncName); | |
53 default: | |
54 // No helper function needed | |
55 break; | |
56 } | 48 } |
57 | 49 |
58 if (nullptr == args.fInputColor) { | 50 fragBuilder->codeAppendf("%s;", tmpDecl.c_str()); |
59 args.fInputColor = "vec4(1)"; | |
60 } | |
61 | 51 |
62 if (GrGammaEffect::Mode::kExponential == ge.mode()) { | 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 { |
63 fragBuilder->codeAppendf("%s = vec4(pow(%s.rgb, vec3(%s)), %s.a);", | 65 fragBuilder->codeAppendf("%s = vec4(pow(%s.rgb, vec3(%s)), %s.a);", |
64 args.fOutputColor, args.fInputColor, gammaU
niName, | 66 args.fOutputColor, tmpVar.c_str(), gamma
UniName, |
65 args.fInputColor); | 67 tmpVar.c_str()); |
66 } else { | |
67 fragBuilder->codeAppendf("%s = vec4(%s(%s.r), %s(%s.g), %s(%s.b), %s
.a);", | |
68 args.fOutputColor, | |
69 srgbFuncName.c_str(), args.fInputColor, | |
70 srgbFuncName.c_str(), args.fInputColor, | |
71 srgbFuncName.c_str(), args.fInputColor, | |
72 args.fInputColor); | |
73 } | 68 } |
74 } | 69 } |
75 | 70 |
76 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& pro
c) override { | 71 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& pro
c) override { |
77 const GrGammaEffect& ge = proc.cast<GrGammaEffect>(); | 72 const GrGammaEffect& ge = proc.cast<GrGammaEffect>(); |
78 if (GrGammaEffect::Mode::kExponential == ge.mode()) { | 73 if (!ge.gammaIsSRGB()) { |
79 pdman.set1f(fGammaUni, ge.gamma()); | 74 pdman.set1f(fGammaUni, ge.gamma()); |
80 } | 75 } |
81 } | 76 } |
82 | 77 |
83 static inline void GenKey(const GrProcessor& processor, const GrGLSLCaps&, | 78 static inline void GenKey(const GrProcessor& processor, const GrGLSLCaps&, |
84 GrProcessorKeyBuilder* b) { | 79 GrProcessorKeyBuilder* b) { |
85 const GrGammaEffect& ge = processor.cast<GrGammaEffect>(); | 80 const GrGammaEffect& ge = processor.cast<GrGammaEffect>(); |
86 uint32_t key = static_cast<uint32_t>(ge.mode()); | 81 uint32_t key = ge.gammaIsSRGB() ? 0x1 : 0x0; |
87 b->add32(key); | 82 b->add32(key); |
88 } | 83 } |
89 | 84 |
90 private: | 85 private: |
91 GrGLSLProgramDataManager::UniformHandle fGammaUni; | 86 GrGLSLProgramDataManager::UniformHandle fGammaUni; |
92 | 87 |
93 typedef GrGLSLFragmentProcessor INHERITED; | 88 typedef GrGLSLFragmentProcessor INHERITED; |
94 }; | 89 }; |
95 | 90 |
96 /////////////////////////////////////////////////////////////////////////////// | 91 /////////////////////////////////////////////////////////////////////////////// |
97 | 92 |
98 GrGammaEffect::GrGammaEffect(Mode mode, SkScalar gamma) | 93 GrGammaEffect::GrGammaEffect(GrTexture* texture, SkScalar gamma) |
99 : fMode(mode) | 94 : INHERITED(texture, GrCoordTransform::MakeDivByTextureWHMatrix(texture)) { |
100 , fGamma(gamma) { | |
101 this->initClassID<GrGammaEffect>(); | 95 this->initClassID<GrGammaEffect>(); |
| 96 |
| 97 fGamma = gamma; |
| 98 fGammaIsSRGB = SkScalarNearlyEqual(gamma, 1.0f / 2.2f); |
102 } | 99 } |
103 | 100 |
104 bool GrGammaEffect::onIsEqual(const GrFragmentProcessor& s) const { | 101 bool GrGammaEffect::onIsEqual(const GrFragmentProcessor& s) const { |
105 const GrGammaEffect& other = s.cast<GrGammaEffect>(); | 102 const GrGammaEffect& other = s.cast<GrGammaEffect>(); |
106 return | 103 return |
107 other.fMode == fMode && | 104 other.fGammaIsSRGB == fGammaIsSRGB && |
108 (fMode != Mode::kExponential || other.fGamma == fGamma); | 105 other.fGamma == fGamma; |
109 } | 106 } |
110 | 107 |
111 void GrGammaEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { | 108 void GrGammaEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
112 inout->setToUnknown(GrInvariantOutput::kWill_ReadInput); | 109 inout->setToUnknown(GrInvariantOutput::kWill_ReadInput); |
113 } | 110 } |
114 | 111 |
115 /////////////////////////////////////////////////////////////////////////////// | 112 /////////////////////////////////////////////////////////////////////////////// |
116 | 113 |
117 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrGammaEffect); | 114 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrGammaEffect); |
118 | 115 |
119 const GrFragmentProcessor* GrGammaEffect::TestCreate(GrProcessorTestData* d) { | 116 const GrFragmentProcessor* GrGammaEffect::TestCreate(GrProcessorTestData* d) { |
120 // We want to be sure and test sRGB sometimes | 117 // We want to be sure and test sRGB sometimes |
121 Mode testMode = static_cast<Mode>(d->fRandom->nextRangeU(0, 2)); | 118 bool srgb = d->fRandom->nextBool(); |
122 SkScalar gamma = d->fRandom->nextRangeScalar(0.5f, 2.0f); | 119 return new GrGammaEffect(d->fTextures[GrProcessorUnitTest::kSkiaPMTextureIdx
], |
123 return new GrGammaEffect(testMode, gamma); | 120 srgb ? 1.0f / 2.2f : d->fRandom->nextRangeScalar(0.
5f, 2.0f)); |
124 } | 121 } |
125 | 122 |
126 /////////////////////////////////////////////////////////////////////////////// | 123 /////////////////////////////////////////////////////////////////////////////// |
127 | 124 |
128 void GrGammaEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, | 125 void GrGammaEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
129 GrProcessorKeyBuilder* b) const { | 126 GrProcessorKeyBuilder* b) const { |
130 GrGLGammaEffect::GenKey(*this, caps, b); | 127 GrGLGammaEffect::GenKey(*this, caps, b); |
131 } | 128 } |
132 | 129 |
133 GrGLSLFragmentProcessor* GrGammaEffect::onCreateGLSLInstance() const { | 130 GrGLSLFragmentProcessor* GrGammaEffect::onCreateGLSLInstance() const { |
134 return new GrGLGammaEffect(); | 131 return new GrGLGammaEffect(); |
135 } | 132 } |
136 | 133 |
137 const GrFragmentProcessor* GrGammaEffect::Create(SkScalar gamma) { | 134 const GrFragmentProcessor* GrGammaEffect::Create(GrTexture* texture, SkScalar ga
mma) { |
138 // TODO: Once our public-facing API for specifying gamma curves settles down
, expose this, | 135 return new GrGammaEffect(texture, gamma); |
139 // and allow clients to explicitly request sRGB, rather than inferring from
the exponent. | |
140 // Note that AdobeRGB (for example) is speficied as x^2.2, not the Rec.709 c
urves. | |
141 if (SkScalarNearlyEqual(gamma, 2.2f)) { | |
142 return new GrGammaEffect(Mode::kSRGBToLinear, 2.2f); | |
143 } else if (SkScalarNearlyEqual(gamma, 1.0f / 2.2f)) { | |
144 return new GrGammaEffect(Mode::kLinearToSRGB, 1.0f / 2.2f); | |
145 } else { | |
146 return new GrGammaEffect(Mode::kExponential, gamma); | |
147 } | |
148 } | 136 } |
OLD | NEW |