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 | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 9 | #include "gl/builders/GrGLProgramBuilder.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 | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 22 | virtual void emitCode(GrGLGPBuilder* 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 | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 39 | GrGLVertexBuilder* 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 | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 43 | GrGLGPFragmentBuilder* 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 { |
egdaniel | ccb2e38 | 2014-10-13 12:53:46 -0700 | [diff] [blame^] | 76 | if (GrPixelConfigIsOpaque(this->texture(0)->config())) { |
| 77 | inout->mulByUnknownOpaqueColor(); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 78 | } else { |
egdaniel | ccb2e38 | 2014-10-13 12:53:46 -0700 | [diff] [blame^] | 79 | inout->mulByUnknownColor(); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 83 | const GrBackendGeometryProcessorFactory& GrCustomCoordsTextureEffect::getFactory() const { |
| 84 | return GrTBackendGeometryProcessorFactory<GrCustomCoordsTextureEffect>::getInstance(); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /////////////////////////////////////////////////////////////////////////////// |
| 88 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 89 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrCustomCoordsTextureEffect); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 90 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 91 | GrGeometryProcessor* GrCustomCoordsTextureEffect::TestCreate(SkRandom* random, |
| 92 | GrContext*, |
| 93 | const GrDrawTargetCaps&, |
| 94 | GrTexture* textures[]) { |
| 95 | int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 96 | GrProcessorUnitTest::kAlphaTextureIdx; |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 97 | static const SkShader::TileMode kTileModes[] = { |
| 98 | SkShader::kClamp_TileMode, |
| 99 | SkShader::kRepeat_TileMode, |
| 100 | SkShader::kMirror_TileMode, |
| 101 | }; |
| 102 | SkShader::TileMode tileModes[] = { |
| 103 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 104 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 105 | }; |
| 106 | GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode : |
| 107 | GrTextureParams::kNone_FilterMode); |
| 108 | |
| 109 | return GrCustomCoordsTextureEffect::Create(textures[texIdx], params); |
| 110 | } |