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" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 9 | #include "GrInvariantOutput.h" |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 10 | #include "gl/builders/GrGLProgramBuilder.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 11 | #include "gl/GrGLProcessor.h" |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 12 | #include "gl/GrGLSL.h" |
| 13 | #include "gl/GrGLTexture.h" |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 14 | #include "gl/GrGLGeometryProcessor.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 15 | #include "GrTBackendProcessorFactory.h" |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 16 | #include "GrTexture.h" |
| 17 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 18 | class GrGLCustomCoordsTextureEffect : public GrGLGeometryProcessor { |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 19 | public: |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame^] | 20 | GrGLCustomCoordsTextureEffect(const GrBackendProcessorFactory& factory, |
| 21 | const GrGeometryProcessor&, |
| 22 | const GrBatchTracker&) |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 23 | : INHERITED (factory) {} |
| 24 | |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 25 | virtual void emitCode(const EmitArgs& args) SK_OVERRIDE { |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 26 | const GrCustomCoordsTextureEffect& cte = |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 27 | args.fGP.cast<GrCustomCoordsTextureEffect>(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 28 | |
| 29 | GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 30 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 31 | GrGLVertToFrag v(kVec2f_GrSLType); |
| 32 | args.fPB->addVarying("TextureCoords", &v); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 33 | vsBuilder->codeAppendf("%s = %s;", v.vsOut(), cte.inTextureCoords()->fName); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 34 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 35 | if (cte.inColor()) { |
| 36 | args.fPB->addPassThroughAttribute(cte.inColor(), args.fOutputColor); |
| 37 | } |
| 38 | |
| 39 | // setup output coords |
| 40 | vsBuilder->codeAppendf("%s = %s;", vsBuilder->positionCoords(), cte.inPosition()->fName); |
| 41 | vsBuilder->codeAppendf("%s = %s;", vsBuilder->localCoords(), cte.inPosition()->fName); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 42 | |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 43 | // setup position varying |
| 44 | vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 45 | vsBuilder->uViewM(), cte.inPosition()->fName); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 46 | |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 47 | GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 48 | fsBuilder->codeAppendf("%s = ", args.fOutputCoverage); |
| 49 | fsBuilder->appendTextureLookup(args.fSamplers[0], v.fsIn(), kVec2f_GrSLType); |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 50 | fsBuilder->codeAppend(";"); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 51 | } |
| 52 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 53 | virtual void setData(const GrGLProgramDataManager&, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame^] | 54 | const GrGeometryProcessor&, |
| 55 | const GrBatchTracker&) SK_OVERRIDE {} |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 56 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame^] | 57 | static inline void GenKey(const GrGeometryProcessor& proc, |
| 58 | const GrBatchTracker&, |
| 59 | const GrGLCaps&, |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 60 | GrProcessorKeyBuilder* b) { |
| 61 | const GrCustomCoordsTextureEffect& gp = proc.cast<GrCustomCoordsTextureEffect>(); |
| 62 | |
| 63 | b->add32(SkToBool(gp.inColor())); |
| 64 | } |
| 65 | |
| 66 | |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 67 | private: |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 68 | typedef GrGLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | /////////////////////////////////////////////////////////////////////////////// |
| 72 | |
| 73 | GrCustomCoordsTextureEffect::GrCustomCoordsTextureEffect(GrTexture* texture, |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 74 | const GrTextureParams& params, |
| 75 | bool hasColor) |
| 76 | : fTextureAccess(texture, params), fInColor(NULL) { |
| 77 | fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType)); |
| 78 | if (hasColor) { |
| 79 | fInColor = &this->addVertexAttrib(GrAttribute("inColor", kVec4ub_GrVertexAttribType)); |
| 80 | this->setHasVertexColor(); |
| 81 | } |
| 82 | fInTextureCoords = &this->addVertexAttrib(GrAttribute("inTextureCoords", |
| 83 | kVec2f_GrVertexAttribType)); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 84 | this->addTextureAccess(&fTextureAccess); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 85 | } |
| 86 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 87 | bool GrCustomCoordsTextureEffect::onIsEqual(const GrGeometryProcessor& other) const { |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 88 | const GrCustomCoordsTextureEffect& gp = other.cast<GrCustomCoordsTextureEffect>(); |
| 89 | return SkToBool(this->inColor()) == SkToBool(gp.inColor()); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 90 | } |
| 91 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 92 | void GrCustomCoordsTextureEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
egdaniel | f8449ba | 2014-11-25 10:24:56 -0800 | [diff] [blame] | 93 | if (GrPixelConfigIsAlphaOnly(this->texture(0)->config())) { |
| 94 | inout->mulByUnknownAlpha(); |
| 95 | } else if (GrPixelConfigIsOpaque(this->texture(0)->config())) { |
egdaniel | ccb2e38 | 2014-10-13 12:53:46 -0700 | [diff] [blame] | 96 | inout->mulByUnknownOpaqueColor(); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 97 | } else { |
egdaniel | ccb2e38 | 2014-10-13 12:53:46 -0700 | [diff] [blame] | 98 | inout->mulByUnknownColor(); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 102 | const GrBackendGeometryProcessorFactory& GrCustomCoordsTextureEffect::getFactory() const { |
| 103 | return GrTBackendGeometryProcessorFactory<GrCustomCoordsTextureEffect>::getInstance(); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /////////////////////////////////////////////////////////////////////////////// |
| 107 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 108 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrCustomCoordsTextureEffect); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 109 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 110 | GrGeometryProcessor* GrCustomCoordsTextureEffect::TestCreate(SkRandom* random, |
| 111 | GrContext*, |
| 112 | const GrDrawTargetCaps&, |
| 113 | GrTexture* textures[]) { |
| 114 | int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 115 | GrProcessorUnitTest::kAlphaTextureIdx; |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 116 | static const SkShader::TileMode kTileModes[] = { |
| 117 | SkShader::kClamp_TileMode, |
| 118 | SkShader::kRepeat_TileMode, |
| 119 | SkShader::kMirror_TileMode, |
| 120 | }; |
| 121 | SkShader::TileMode tileModes[] = { |
| 122 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 123 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 124 | }; |
| 125 | GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode : |
| 126 | GrTextureParams::kNone_FilterMode); |
| 127 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 128 | return GrCustomCoordsTextureEffect::Create(textures[texIdx], params, random->nextBool()); |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 129 | } |