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