blob: b819cf3b81364664865c33dc5a46bf8f23d8ed8c [file] [log] [blame]
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001/*
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"
egdaniel605dd0f2014-11-12 08:35:25 -08009#include "GrInvariantOutput.h"
egdaniel605dd0f2014-11-12 08:35:25 -080010#include "GrTexture.h"
brianosman77320db2016-09-07 08:09:10 -070011#include "glsl/GrGLSLColorSpaceXformHelper.h"
egdaniel64c47282015-11-13 06:54:19 -080012#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080013#include "glsl/GrGLSLFragmentShaderBuilder.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000014
egdaniel64c47282015-11-13 06:54:19 -080015class GrGLSimpleTextureEffect : public GrGLSLFragmentProcessor {
bsalomon@google.com68b58c92013-01-17 16:50:08 +000016public:
robertphillips9cdb9922016-02-03 12:25:40 -080017 void emitCode(EmitArgs& args) override {
brianosman77320db2016-09-07 08:09:10 -070018 const GrSimpleTextureEffect& textureEffect = args.fFp.cast<GrSimpleTextureEffect>();
19 GrGLSLColorSpaceXformHelper colorSpaceHelper(args.fUniformHandler,
20 textureEffect.colorSpaceXform(),
21 &fColorSpaceXformUni);
22
cdalton85285412016-02-18 12:37:07 -080023 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
robertphillipsbf536af2016-02-04 06:11:53 -080024 fragBuilder->codeAppendf("%s = ", args.fOutputColor);
egdaniel4ca2e602015-11-18 08:01:26 -080025 fragBuilder->appendTextureLookupAndModulate(args.fInputColor,
brianosman77320db2016-09-07 08:09:10 -070026 args.fTexSamplers[0],
27 args.fCoords[0].c_str(),
28 args.fCoords[0].getType(),
29 &colorSpaceHelper);
robertphillipsbf536af2016-02-04 06:11:53 -080030 fragBuilder->codeAppend(";");
bsalomon@google.com68b58c92013-01-17 16:50:08 +000031 }
32
brianosman77320db2016-09-07 08:09:10 -070033 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
39protected:
40 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& processor) override {
41 const GrSimpleTextureEffect& textureEffect = processor.cast<GrSimpleTextureEffect>();
42 if (SkToBool(textureEffect.colorSpaceXform())) {
brianosman51924752016-09-12 08:50:19 -070043 pdman.setSkMatrix44(fColorSpaceXformUni, textureEffect.colorSpaceXform()->srcToDst());
brianosman77320db2016-09-07 08:09:10 -070044 }
45 }
46
bsalomon@google.com68b58c92013-01-17 16:50:08 +000047private:
egdaniel64c47282015-11-13 06:54:19 -080048 typedef GrGLSLFragmentProcessor INHERITED;
brianosman77320db2016-09-07 08:09:10 -070049
50 UniformHandle fColorSpaceXformUni;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000051};
52
53///////////////////////////////////////////////////////////////////////////////
54
egdaniel605dd0f2014-11-12 08:35:25 -080055void GrSimpleTextureEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
egdaniel1a8ecdf2014-10-03 06:24:12 -070056 this->updateInvariantOutputForModulation(inout);
bsalomon@google.com68b58c92013-01-17 16:50:08 +000057}
58
egdaniel57d3b032015-11-13 11:57:27 -080059void GrSimpleTextureEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
60 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -080061 GrGLSimpleTextureEffect::GenKey(*this, caps, b);
62}
63
egdaniel57d3b032015-11-13 11:57:27 -080064GrGLSLFragmentProcessor* GrSimpleTextureEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -080065 return new GrGLSimpleTextureEffect;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000066}
67
68///////////////////////////////////////////////////////////////////////////////
69
joshualittb0a8a372014-09-23 09:50:21 -070070GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSimpleTextureEffect);
bsalomon@google.com68b58c92013-01-17 16:50:08 +000071
bungeman06ca8ec2016-06-09 08:01:03 -070072sk_sp<GrFragmentProcessor> GrSimpleTextureEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -070073 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
74 GrProcessorUnitTest::kAlphaTextureIdx;
bsalomon@google.comc7818882013-03-20 19:19:53 +000075 static const SkShader::TileMode kTileModes[] = {
76 SkShader::kClamp_TileMode,
77 SkShader::kRepeat_TileMode,
78 SkShader::kMirror_TileMode,
79 };
80 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -070081 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
82 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
bsalomon@google.comc7818882013-03-20 19:19:53 +000083 };
joshualitt0067ff52015-07-08 14:26:19 -070084 GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode :
85 GrTextureParams::kNone_FilterMode);
bsalomon@google.comc7818882013-03-20 19:19:53 +000086
bsalomon@google.com77af6802013-10-02 13:04:56 +000087 static const GrCoordSet kCoordSets[] = {
88 kLocal_GrCoordSet,
bsalomon309d4d52014-12-18 10:17:44 -080089 kDevice_GrCoordSet
bsalomon@google.comc7818882013-03-20 19:19:53 +000090 };
joshualitt0067ff52015-07-08 14:26:19 -070091 GrCoordSet coordSet = kCoordSets[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kCoordSets))];
bsalomon@google.comc7818882013-03-20 19:19:53 +000092
joshualitt0067ff52015-07-08 14:26:19 -070093 const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom);
brianosman54f30c12016-07-18 10:53:52 -070094 return GrSimpleTextureEffect::Make(d->fTextures[texIdx], nullptr, matrix, coordSet);
bsalomon@google.com68b58c92013-01-17 16:50:08 +000095}