commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 "GrCustomCoordsTextureEffect.h" |
joshualitt | 408d612 | 2014-09-17 07:00:35 -0700 | [diff] [blame] | 9 | #include "gl/builders/GrGLFullProgramBuilder.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 10 | #include "gl/GrGLProcessor.h" |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 11 | #include "gl/GrGLSL.h" |
| 12 | #include "gl/GrGLTexture.h" |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 13 | #include "gl/GrGLGeometryProcessor.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 14 | #include "GrTBackendProcessorFactory.h" |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 15 | #include "GrTexture.h" |
| 16 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 17 | class GrGLCustomCoordsTextureEffect : public GrGLGeometryProcessor { |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 18 | public: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 19 | GrGLCustomCoordsTextureEffect(const GrBackendProcessorFactory& factory, const GrProcessor&) |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 20 | : INHERITED (factory) {} |
| 21 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 22 | virtual void emitCode(GrGLFullProgramBuilder* builder, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 23 | const GrGeometryProcessor& geometryProcessor, |
| 24 | const GrProcessorKey& key, |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 25 | const char* outputColor, |
| 26 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 27 | const TransformedCoordsArray&, |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 28 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 29 | const GrCustomCoordsTextureEffect& customCoordsTextureEffect = |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 30 | geometryProcessor.cast<GrCustomCoordsTextureEffect>(); |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 31 | SkASSERT(1 == customCoordsTextureEffect.getVertexAttribs().count()); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 32 | |
| 33 | SkString fsCoordName; |
| 34 | const char* vsVaryingName; |
| 35 | const char* fsVaryingNamePtr; |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 36 | builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsVaryingName, &fsVaryingNamePtr); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 37 | fsCoordName = fsVaryingNamePtr; |
| 38 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 39 | GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder(); |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 40 | const GrShaderVar& inTextureCoords = customCoordsTextureEffect.inTextureCoords(); |
| 41 | vsBuilder->codeAppendf("\t%s = %s;\n", vsVaryingName, inTextureCoords.c_str()); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 42 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 43 | GrGLProcessorFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 44 | fsBuilder->codeAppendf("\t%s = ", outputColor); |
| 45 | fsBuilder->appendTextureLookupAndModulate(inputColor, |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 46 | samplers[0], |
| 47 | fsCoordName.c_str(), |
| 48 | kVec2f_GrSLType); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 49 | fsBuilder->codeAppend(";\n"); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 50 | } |
| 51 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 52 | virtual void setData(const GrGLProgramDataManager&, |
| 53 | const GrProcessor&) SK_OVERRIDE {} |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 54 | |
| 55 | private: |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 56 | typedef GrGLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | /////////////////////////////////////////////////////////////////////////////// |
| 60 | |
| 61 | GrCustomCoordsTextureEffect::GrCustomCoordsTextureEffect(GrTexture* texture, |
| 62 | const GrTextureParams& params) |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 63 | : fTextureAccess(texture, params) |
| 64 | , fInTextureCoords(this->addVertexAttrib(GrShaderVar("inTextureCoords", |
| 65 | kVec2f_GrSLType, |
| 66 | GrShaderVar::kAttribute_TypeModifier))) { |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 67 | this->addTextureAccess(&fTextureAccess); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 68 | } |
| 69 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 70 | bool GrCustomCoordsTextureEffect::onIsEqual(const GrProcessor& other) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 71 | const GrCustomCoordsTextureEffect& cte = other.cast<GrCustomCoordsTextureEffect>(); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 72 | return fTextureAccess == cte.fTextureAccess; |
| 73 | } |
| 74 | |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame^] | 75 | void GrCustomCoordsTextureEffect::onComputeInvariantOutput(InvariantOutput* inout) const { |
| 76 | if (inout->isOpaque() && GrPixelConfigIsOpaque(this->texture(0)->config())) { |
| 77 | inout->fValidFlags = kA_GrColorComponentFlag; |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 78 | } else { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame^] | 79 | inout->fValidFlags = 0; |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 80 | } |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame^] | 81 | inout->fIsSingleComponent = false; |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 82 | } |
| 83 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 84 | const GrBackendGeometryProcessorFactory& GrCustomCoordsTextureEffect::getFactory() const { |
| 85 | return GrTBackendGeometryProcessorFactory<GrCustomCoordsTextureEffect>::getInstance(); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /////////////////////////////////////////////////////////////////////////////// |
| 89 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 90 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrCustomCoordsTextureEffect); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 91 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 92 | GrGeometryProcessor* GrCustomCoordsTextureEffect::TestCreate(SkRandom* random, |
| 93 | GrContext*, |
| 94 | const GrDrawTargetCaps&, |
| 95 | GrTexture* textures[]) { |
| 96 | int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 97 | GrProcessorUnitTest::kAlphaTextureIdx; |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 98 | static const SkShader::TileMode kTileModes[] = { |
| 99 | SkShader::kClamp_TileMode, |
| 100 | SkShader::kRepeat_TileMode, |
| 101 | SkShader::kMirror_TileMode, |
| 102 | }; |
| 103 | SkShader::TileMode tileModes[] = { |
| 104 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 105 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 106 | }; |
| 107 | GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode : |
| 108 | GrTextureParams::kNone_FilterMode); |
| 109 | |
| 110 | return GrCustomCoordsTextureEffect::Create(textures[texIdx], params); |
| 111 | } |