OLD | NEW |
(Empty) | |
| 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 |
| 8 #include "effects/GrColorProcessor.h" |
| 9 #include "gl/GrGLProcessor.h" |
| 10 #include "gl/GrGLSL.h" |
| 11 #include "gl/builders/GrGLProgramBuilder.h" |
| 12 |
| 13 class GrGLColorProcessor : public GrGLFragmentProcessor { |
| 14 public: |
| 15 GrGLColorProcessor() : fPrevColor(GrColor_ILLEGAL) {} |
| 16 |
| 17 void emitCode(GrGLFPBuilder* builder, |
| 18 const GrFragmentProcessor& fp, |
| 19 const char* outputColor, |
| 20 const char* inputColor, |
| 21 const TransformedCoordsArray& coords, |
| 22 const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 23 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 24 const char* colorUni; |
| 25 fColorUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibi
lity, |
| 26 kVec4f_GrSLType, kMedium_GrSLPrecisi
on, "constantColor", |
| 27 &colorUni); |
| 28 switch (fp.cast<GrColorProcessor>().inputMode()) { |
| 29 case GrColorProcessor::kIgnore_InputMode: |
| 30 fsBuilder->codeAppendf("%s = %s;", outputColor, colorUni); |
| 31 break; |
| 32 case GrColorProcessor::kModulateRGBA_InputMode: |
| 33 fsBuilder->codeAppendf("%s = %s * %s;", outputColor, inputColor,
colorUni); |
| 34 break; |
| 35 case GrColorProcessor::kModulateA_InputMode: |
| 36 fsBuilder->codeAppendf("%s = %s.a * %s;", outputColor, inputColo
r, colorUni); |
| 37 break; |
| 38 } |
| 39 } |
| 40 |
| 41 void setData(const GrGLProgramDataManager& pdm, const GrProcessor& processor
) SK_OVERRIDE { |
| 42 GrColor color = processor.cast<GrColorProcessor>().color(); |
| 43 // We use the value 0 as an uninit sentinel, so we will always upload th
at value even if it |
| 44 // were to be used repeatedly. |
| 45 if (GrColor_ILLEGAL == color || fPrevColor != color) { |
| 46 static const GrGLfloat scale = 1.f / 255.f; |
| 47 GrGLfloat floatColor[4] = { |
| 48 GrColorUnpackR(color) * scale, |
| 49 GrColorUnpackG(color) * scale, |
| 50 GrColorUnpackB(color) * scale, |
| 51 GrColorUnpackA(color) * scale, |
| 52 }; |
| 53 pdm.set4fv(fColorUniform, 1, floatColor); |
| 54 fPrevColor = color; |
| 55 } |
| 56 } |
| 57 |
| 58 private: |
| 59 GrGLProgramDataManager::UniformHandle fColorUniform; |
| 60 GrColor fPrevColor; |
| 61 |
| 62 typedef GrGLFragmentProcessor INHERITED; |
| 63 }; |
| 64 |
| 65 /////////////////////////////////////////////////////////////////////////////// |
| 66 |
| 67 void GrColorProcessor::onComputeInvariantOutput(GrInvariantOutput* inout) const
{ |
| 68 if (kIgnore_InputMode == fMode) { |
| 69 inout->setToOther(kRGBA_GrColorComponentFlags, fColor, GrInvariantOutput
::kWill_ReadInput); |
| 70 } else { |
| 71 GrColor r = GrColorUnpackR(fColor); |
| 72 bool colorIsSingleChannel = r == GrColorUnpackG(fColor) && r == GrColorU
npackB(fColor) && |
| 73 r == GrColorUnpackA(fColor); |
| 74 if (kIgnore_InputMode == fMode) { |
| 75 if (colorIsSingleChannel) { |
| 76 inout->mulByKnownSingleComponent(r); |
| 77 } else { |
| 78 inout->mulByKnownFourComponents(fColor); |
| 79 } |
| 80 } else { |
| 81 if (colorIsSingleChannel) { |
| 82 inout->mulAlphaByKnownSingleComponent(r); |
| 83 } else { |
| 84 inout->mulAlphaByKnownFourComponents(fColor); |
| 85 } |
| 86 } |
| 87 } |
| 88 } |
| 89 |
| 90 void GrColorProcessor::getGLProcessorKey(const GrGLCaps&, GrProcessorKeyBuilder*
b) const { |
| 91 b->add32(fMode); |
| 92 } |
| 93 |
| 94 GrGLFragmentProcessor* GrColorProcessor::createGLInstance() const { |
| 95 return SkNEW(GrGLColorProcessor); |
| 96 } |
| 97 |
| 98 /////////////////////////////////////////////////////////////////////////////// |
| 99 |
| 100 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrColorProcessor); |
| 101 |
| 102 GrFragmentProcessor* GrColorProcessor::TestCreate(SkRandom* random, |
| 103 GrContext*, |
| 104 const GrDrawTargetCaps&, |
| 105 GrTexture*[]) { |
| 106 GrColor color = random->nextU(); |
| 107 InputMode mode = static_cast<InputMode>(random->nextULessThan(kInputModeCnt)
); |
| 108 return GrColorProcessor::Create(color, mode); |
| 109 } |
OLD | NEW |