blob: 682539ff3bab0a17bb29d43eb6dc98e6c354b9b0 [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],
bsalomon1a1aa932016-09-12 09:30:36 -070027 args.fTransformedCoords[0].c_str(),
28 args.fTransformedCoords[0].getType(),
brianosman77320db2016-09-07 08:09:10 -070029 &colorSpaceHelper);
robertphillipsbf536af2016-02-04 06:11:53 -080030 fragBuilder->codeAppend(";");
bsalomon@google.com68b58c92013-01-17 16:50:08 +000031 }
32
Brian Salomon94efbf52016-11-29 13:43:05 -050033 static inline void GenKey(const GrProcessor& effect, const GrShaderCaps&,
brianosman77320db2016-09-07 08:09:10 -070034 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
Brian Salomon94efbf52016-11-29 13:43:05 -050059void GrSimpleTextureEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -080060 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 };
Brian Salomon514baff2016-11-17 15:17:07 -050084 GrSamplerParams params(tileModes, d->fRandom->nextBool() ? GrSamplerParams::kBilerp_FilterMode :
85 GrSamplerParams::kNone_FilterMode);
bsalomon@google.comc7818882013-03-20 19:19:53 +000086
joshualitt0067ff52015-07-08 14:26:19 -070087 const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom);
Brian Osmane2f732f2016-10-03 14:23:50 -040088 auto colorSpaceXform = GrTest::TestColorXform(d->fRandom);
Brian Salomon2ebd0c82016-10-03 17:15:28 -040089 return GrSimpleTextureEffect::Make(d->fTextures[texIdx], colorSpaceXform, matrix);
bsalomon@google.com68b58c92013-01-17 16:50:08 +000090}