blob: 3dc6ea3a2c1cc4f6959ccd1d248970cdcb9e6362 [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"
Robert Phillips40fd7c92017-01-30 08:06:27 -050010#include "GrProxyMove.h"
egdaniel605dd0f2014-11-12 08:35:25 -080011#include "GrTexture.h"
brianosman77320db2016-09-07 08:09:10 -070012#include "glsl/GrGLSLColorSpaceXformHelper.h"
egdaniel64c47282015-11-13 06:54:19 -080013#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080014#include "glsl/GrGLSLFragmentShaderBuilder.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000015
Robert Phillips40fd7c92017-01-30 08:06:27 -050016GrSimpleTextureEffect::GrSimpleTextureEffect(GrContext* ctx, sk_sp<GrTextureProxy> proxy,
17 sk_sp<GrColorSpaceXform> colorSpaceXform,
18 const SkMatrix& matrix,
19 GrSamplerParams::FilterMode filterMode)
20 : INHERITED{ctx,
21 ModulationFlags(proxy->config()),
22 GR_PROXY_MOVE(proxy),
23 std::move(colorSpaceXform),
24 matrix,
25 filterMode} {
26 this->initClassID<GrSimpleTextureEffect>();
27}
28
29GrSimpleTextureEffect::GrSimpleTextureEffect(GrContext* ctx, sk_sp<GrTextureProxy> proxy,
30 sk_sp<GrColorSpaceXform> colorSpaceXform,
31 const SkMatrix& matrix,
32 const GrSamplerParams& params)
33 : INHERITED{ctx,
34 ModulationFlags(proxy->config()),
35 GR_PROXY_MOVE(proxy),
36 std::move(colorSpaceXform),
37 matrix,
38 params} {
39 this->initClassID<GrSimpleTextureEffect>();
40}
41
egdaniel64c47282015-11-13 06:54:19 -080042class GrGLSimpleTextureEffect : public GrGLSLFragmentProcessor {
bsalomon@google.com68b58c92013-01-17 16:50:08 +000043public:
robertphillips9cdb9922016-02-03 12:25:40 -080044 void emitCode(EmitArgs& args) override {
brianosman77320db2016-09-07 08:09:10 -070045 const GrSimpleTextureEffect& textureEffect = args.fFp.cast<GrSimpleTextureEffect>();
46 GrGLSLColorSpaceXformHelper colorSpaceHelper(args.fUniformHandler,
47 textureEffect.colorSpaceXform(),
48 &fColorSpaceXformUni);
49
cdalton85285412016-02-18 12:37:07 -080050 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
robertphillipsbf536af2016-02-04 06:11:53 -080051 fragBuilder->codeAppendf("%s = ", args.fOutputColor);
egdaniel4ca2e602015-11-18 08:01:26 -080052 fragBuilder->appendTextureLookupAndModulate(args.fInputColor,
brianosman77320db2016-09-07 08:09:10 -070053 args.fTexSamplers[0],
bsalomon1a1aa932016-09-12 09:30:36 -070054 args.fTransformedCoords[0].c_str(),
55 args.fTransformedCoords[0].getType(),
brianosman77320db2016-09-07 08:09:10 -070056 &colorSpaceHelper);
robertphillipsbf536af2016-02-04 06:11:53 -080057 fragBuilder->codeAppend(";");
bsalomon@google.com68b58c92013-01-17 16:50:08 +000058 }
59
Brian Salomon94efbf52016-11-29 13:43:05 -050060 static inline void GenKey(const GrProcessor& effect, const GrShaderCaps&,
brianosman77320db2016-09-07 08:09:10 -070061 GrProcessorKeyBuilder* b) {
62 const GrSimpleTextureEffect& textureEffect = effect.cast<GrSimpleTextureEffect>();
63 b->add32(GrColorSpaceXform::XformKey(textureEffect.colorSpaceXform()));
64 }
65
66protected:
67 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& processor) override {
68 const GrSimpleTextureEffect& textureEffect = processor.cast<GrSimpleTextureEffect>();
69 if (SkToBool(textureEffect.colorSpaceXform())) {
brianosman51924752016-09-12 08:50:19 -070070 pdman.setSkMatrix44(fColorSpaceXformUni, textureEffect.colorSpaceXform()->srcToDst());
brianosman77320db2016-09-07 08:09:10 -070071 }
72 }
73
bsalomon@google.com68b58c92013-01-17 16:50:08 +000074private:
egdaniel64c47282015-11-13 06:54:19 -080075 typedef GrGLSLFragmentProcessor INHERITED;
brianosman77320db2016-09-07 08:09:10 -070076
77 UniformHandle fColorSpaceXformUni;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000078};
79
80///////////////////////////////////////////////////////////////////////////////
81
egdaniel605dd0f2014-11-12 08:35:25 -080082void GrSimpleTextureEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
egdaniel1a8ecdf2014-10-03 06:24:12 -070083 this->updateInvariantOutputForModulation(inout);
bsalomon@google.com68b58c92013-01-17 16:50:08 +000084}
85
Brian Salomon94efbf52016-11-29 13:43:05 -050086void GrSimpleTextureEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -080087 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -080088 GrGLSimpleTextureEffect::GenKey(*this, caps, b);
89}
90
egdaniel57d3b032015-11-13 11:57:27 -080091GrGLSLFragmentProcessor* GrSimpleTextureEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -080092 return new GrGLSimpleTextureEffect;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000093}
94
95///////////////////////////////////////////////////////////////////////////////
96
joshualittb0a8a372014-09-23 09:50:21 -070097GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSimpleTextureEffect);
bsalomon@google.com68b58c92013-01-17 16:50:08 +000098
bungeman06ca8ec2016-06-09 08:01:03 -070099sk_sp<GrFragmentProcessor> GrSimpleTextureEffect::TestCreate(GrProcessorTestData* d) {
Robert Phillips40fd7c92017-01-30 08:06:27 -0500100 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
101 : GrProcessorUnitTest::kAlphaTextureIdx;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000102 static const SkShader::TileMode kTileModes[] = {
103 SkShader::kClamp_TileMode,
104 SkShader::kRepeat_TileMode,
105 SkShader::kMirror_TileMode,
106 };
107 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700108 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
109 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
bsalomon@google.comc7818882013-03-20 19:19:53 +0000110 };
Robert Phillips40fd7c92017-01-30 08:06:27 -0500111 GrSamplerParams params(tileModes, d->fRandom->nextBool() ? GrSamplerParams::kBilerp_FilterMode
112 : GrSamplerParams::kNone_FilterMode);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000113
joshualitt0067ff52015-07-08 14:26:19 -0700114 const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom);
Robert Phillips40fd7c92017-01-30 08:06:27 -0500115 sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(d->fRandom);
116 return GrSimpleTextureEffect::Make(d->context(), d->textureProxy(texIdx),
117 std::move(colorSpaceXform), matrix);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000118}