bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 "GrSimpleTextureEffect.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 9 | #include "GrInvariantOutput.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 10 | #include "GrTexture.h" |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 11 | #include "glsl/GrGLSLColorSpaceXformHelper.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 12 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 14 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 15 | class GrGLSimpleTextureEffect : public GrGLSLFragmentProcessor { |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 16 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 17 | void emitCode(EmitArgs& args) override { |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 18 | const GrSimpleTextureEffect& textureEffect = args.fFp.cast<GrSimpleTextureEffect>(); |
| 19 | GrGLSLColorSpaceXformHelper colorSpaceHelper(args.fUniformHandler, |
| 20 | textureEffect.colorSpaceXform(), |
| 21 | &fColorSpaceXformUni); |
| 22 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 23 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 24 | fragBuilder->codeAppendf("%s = ", args.fOutputColor); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 25 | fragBuilder->appendTextureLookupAndModulate(args.fInputColor, |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 26 | args.fTexSamplers[0], |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 27 | args.fTransformedCoords[0].c_str(), |
| 28 | args.fTransformedCoords[0].getType(), |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 29 | &colorSpaceHelper); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 30 | fragBuilder->codeAppend(";"); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 31 | } |
| 32 | |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 33 | static inline void GenKey(const GrProcessor& effect, const GrGLSLCaps&, |
| 34 | GrProcessorKeyBuilder* b) { |
| 35 | const GrSimpleTextureEffect& textureEffect = effect.cast<GrSimpleTextureEffect>(); |
| 36 | b->add32(GrColorSpaceXform::XformKey(textureEffect.colorSpaceXform())); |
| 37 | } |
| 38 | |
| 39 | protected: |
| 40 | void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& processor) override { |
| 41 | const GrSimpleTextureEffect& textureEffect = processor.cast<GrSimpleTextureEffect>(); |
| 42 | if (SkToBool(textureEffect.colorSpaceXform())) { |
brianosman | 5192475 | 2016-09-12 08:50:19 -0700 | [diff] [blame] | 43 | pdman.setSkMatrix44(fColorSpaceXformUni, textureEffect.colorSpaceXform()->srcToDst()); |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 47 | private: |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 48 | typedef GrGLSLFragmentProcessor INHERITED; |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 49 | |
| 50 | UniformHandle fColorSpaceXformUni; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | /////////////////////////////////////////////////////////////////////////////// |
| 54 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 55 | void GrSimpleTextureEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 56 | this->updateInvariantOutputForModulation(inout); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 57 | } |
| 58 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 59 | void GrSimpleTextureEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
| 60 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 61 | GrGLSimpleTextureEffect::GenKey(*this, caps, b); |
| 62 | } |
| 63 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 64 | GrGLSLFragmentProcessor* GrSimpleTextureEffect::onCreateGLSLInstance() const { |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 65 | return new GrGLSimpleTextureEffect; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /////////////////////////////////////////////////////////////////////////////// |
| 69 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 70 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSimpleTextureEffect); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 71 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 72 | sk_sp<GrFragmentProcessor> GrSimpleTextureEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 73 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 74 | GrProcessorUnitTest::kAlphaTextureIdx; |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 75 | static const SkShader::TileMode kTileModes[] = { |
| 76 | SkShader::kClamp_TileMode, |
| 77 | SkShader::kRepeat_TileMode, |
| 78 | SkShader::kMirror_TileMode, |
| 79 | }; |
| 80 | SkShader::TileMode tileModes[] = { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 81 | kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 82 | kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 83 | }; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 84 | GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode : |
| 85 | GrTextureParams::kNone_FilterMode); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 86 | |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 87 | const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom); |
Brian Osman | e2f732f | 2016-10-03 14:23:50 -0400 | [diff] [blame] | 88 | auto colorSpaceXform = GrTest::TestColorXform(d->fRandom); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame^] | 89 | return GrSimpleTextureEffect::Make(d->fTextures[texIdx], colorSpaceXform, matrix); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 90 | } |