blob: b5bae466fd0e9287ec60ebf387ebdb2e95a72be9 [file] [log] [blame]
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +00001/*
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
egdaniel309e3462014-12-09 10:35:58 -08008#include "GrBitmapTextGeoProc.h"
egdaniel605dd0f2014-11-12 08:35:25 -08009#include "GrInvariantOutput.h"
joshualitteb2a6762014-12-04 11:35:33 -080010#include "GrTexture.h"
egdaniel2d721d32015-11-11 13:06:05 -080011#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniele659a582015-11-13 09:55:43 -080012#include "glsl/GrGLSLGeometryProcessor.h"
egdaniel018fb622015-10-28 07:26:40 -070013#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080014#include "glsl/GrGLSLUniformHandler.h"
egdaniel0eafe792015-11-20 14:01:22 -080015#include "glsl/GrGLSLVarying.h"
egdaniel2d721d32015-11-11 13:06:05 -080016#include "glsl/GrGLSLVertexShaderBuilder.h"
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000017
egdaniele659a582015-11-13 09:55:43 -080018class GrGLBitmapTextGeoProc : public GrGLSLGeometryProcessor {
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000019public:
joshualitt465283c2015-09-11 08:19:35 -070020 GrGLBitmapTextGeoProc() : fColor(GrColor_ILLEGAL) {}
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000021
joshualitt465283c2015-09-11 08:19:35 -070022 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
egdaniel309e3462014-12-09 10:35:58 -080023 const GrBitmapTextGeoProc& cte = args.fGP.cast<GrBitmapTextGeoProc>();
joshualitt2dd1ae02014-12-03 06:24:10 -080024
egdaniel4ca2e602015-11-18 08:01:26 -080025 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -080026 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -080027 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000028
joshualittabb52a12015-01-13 15:02:10 -080029 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -080030 varyingHandler->emitAttributes(cte);
joshualittabb52a12015-01-13 15:02:10 -080031
joshualitt922c8b12015-08-07 09:55:23 -070032 // compute numbers to be hardcoded to convert texture coordinates from int to float
33 SkASSERT(cte.numTextures() == 1);
jvanverth7023a002016-02-22 11:25:32 -080034 SkDEBUGCODE(GrTexture* atlas = cte.textureAccess(0).getTexture());
joshualitt7375d6b2015-08-07 13:36:44 -070035 SkASSERT(atlas && SkIsPow2(atlas->width()) && SkIsPow2(atlas->height()));
joshualitt922c8b12015-08-07 09:55:23 -070036
egdaniel8dcdedc2015-11-11 06:27:20 -080037 GrGLSLVertToFrag v(kVec2f_GrSLType);
jvanverth7023a002016-02-22 11:25:32 -080038 varyingHandler->addVarying("TextureCoords", &v, kHigh_GrSLPrecision);
39 vertBuilder->codeAppendf("%s = %s;", v.vsOut(),
egdaniel4ca2e602015-11-18 08:01:26 -080040 cte.inTextureCoords()->fName);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000041
cdalton85285412016-02-18 12:37:07 -080042 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt9b989322014-12-15 14:16:27 -080043 // Setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -070044 if (!cte.colorIgnored()) {
45 if (cte.hasVertexColor()) {
egdaniel0eafe792015-11-20 14:01:22 -080046 varyingHandler->addPassThroughAttribute(cte.inColor(), args.fOutputColor);
joshualittb8c241a2015-05-19 08:23:30 -070047 } else {
egdaniel7ea439b2015-12-03 09:20:44 -080048 this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor,
49 &fColorUniform);
joshualittb8c241a2015-05-19 08:23:30 -070050 }
51 }
joshualitt2dd1ae02014-12-03 06:24:10 -080052
joshualittabb52a12015-01-13 15:02:10 -080053 // Setup position
egdaniel7ea439b2015-12-03 09:20:44 -080054 this->setupPosition(vertBuilder, gpArgs, cte.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -080055
joshualittabb52a12015-01-13 15:02:10 -080056 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -080057 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -080058 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -080059 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -080060 gpArgs->fPositionVar,
61 cte.inPosition()->fName,
62 cte.localMatrix(),
63 args.fTransformsIn,
64 args.fTransformsOut);
joshualittabb52a12015-01-13 15:02:10 -080065
joshualitt02b05012015-02-11 06:56:30 -080066 if (cte.maskFormat() == kARGB_GrMaskFormat) {
egdaniel4ca2e602015-11-18 08:01:26 -080067 fragBuilder->codeAppendf("%s = ", args.fOutputColor);
68 fragBuilder->appendTextureLookupAndModulate(args.fOutputColor,
69 args.fSamplers[0],
70 v.fsIn(),
71 kVec2f_GrSLType);
72 fragBuilder->codeAppend(";");
73 fragBuilder->codeAppendf("%s = vec4(1);", args.fOutputCoverage);
joshualitt02b05012015-02-11 06:56:30 -080074 } else {
egdaniel4ca2e602015-11-18 08:01:26 -080075 fragBuilder->codeAppendf("%s = ", args.fOutputCoverage);
76 fragBuilder->appendTextureLookup(args.fSamplers[0], v.fsIn(), kVec2f_GrSLType);
77 fragBuilder->codeAppend(";");
egdaniel27b63352015-09-15 13:13:50 -070078 if (cte.maskFormat() == kA565_GrMaskFormat) {
79 // set alpha to be max of rgb coverage
egdaniel4ca2e602015-11-18 08:01:26 -080080 fragBuilder->codeAppendf("%s.a = max(max(%s.r, %s.g), %s.b);",
81 args.fOutputCoverage, args.fOutputCoverage,
82 args.fOutputCoverage, args.fOutputCoverage);
egdaniel27b63352015-09-15 13:13:50 -070083 }
joshualitt02b05012015-02-11 06:56:30 -080084 }
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000085 }
86
egdaniel018fb622015-10-28 07:26:40 -070087 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp) override {
joshualittb8c241a2015-05-19 08:23:30 -070088 const GrBitmapTextGeoProc& btgp = gp.cast<GrBitmapTextGeoProc>();
89 if (btgp.color() != fColor && !btgp.hasVertexColor()) {
egdaniel018fb622015-10-28 07:26:40 -070090 float c[4];
joshualittb8c241a2015-05-19 08:23:30 -070091 GrColorToRGBAFloat(btgp.color(), c);
joshualitt9b989322014-12-15 14:16:27 -080092 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -070093 fColor = btgp.color();
joshualitt9b989322014-12-15 14:16:27 -080094 }
joshualitt2dd1ae02014-12-03 06:24:10 -080095 }
96
joshualitte3ababe2015-05-15 07:56:07 -070097 void setTransformData(const GrPrimitiveProcessor& primProc,
egdaniel018fb622015-10-28 07:26:40 -070098 const GrGLSLProgramDataManager& pdman,
joshualitte3ababe2015-05-15 07:56:07 -070099 int index,
100 const SkTArray<const GrCoordTransform*, true>& transforms) override {
101 this->setTransformDataHelper<GrBitmapTextGeoProc>(primProc, pdman, index, transforms);
102 }
103
joshualitt9b989322014-12-15 14:16:27 -0800104 static inline void GenKey(const GrGeometryProcessor& proc,
jvanverthcfc18862015-04-28 08:48:20 -0700105 const GrGLSLCaps&,
joshualitt9b989322014-12-15 14:16:27 -0800106 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800107 const GrBitmapTextGeoProc& gp = proc.cast<GrBitmapTextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800108 uint32_t key = 0;
joshualittb8c241a2015-05-19 08:23:30 -0700109 key |= gp.usesLocalCoords() && gp.localMatrix().hasPerspective() ? 0x1 : 0x0;
110 key |= gp.colorIgnored() ? 0x2 : 0x0;
111 key |= gp.maskFormat() << 3;
112 b->add32(key);
joshualitt922c8b12015-08-07 09:55:23 -0700113
114 // Currently we hardcode numbers to convert atlas coordinates to normalized floating point
115 SkASSERT(gp.numTextures() == 1);
116 GrTexture* atlas = gp.textureAccess(0).getTexture();
117 SkASSERT(atlas);
118 b->add32(atlas->width());
119 b->add32(atlas->height());
joshualitt9b989322014-12-15 14:16:27 -0800120 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800121
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000122private:
joshualitt9b989322014-12-15 14:16:27 -0800123 GrColor fColor;
124 UniformHandle fColorUniform;
125
egdaniele659a582015-11-13 09:55:43 -0800126 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000127};
128
129///////////////////////////////////////////////////////////////////////////////
130
joshualitt2e3b3e32014-12-09 13:31:14 -0800131GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture,
joshualitt02b05012015-02-11 06:56:30 -0800132 const GrTextureParams& params, GrMaskFormat format,
joshualittb8c241a2015-05-19 08:23:30 -0700133 const SkMatrix& localMatrix, bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700134 : fColor(color)
135 , fLocalMatrix(localMatrix)
joshualittb8c241a2015-05-19 08:23:30 -0700136 , fUsesLocalCoords(usesLocalCoords)
joshualitt8fc6c2d2014-12-22 15:27:05 -0800137 , fTextureAccess(texture, params)
halcanary96fcdcc2015-08-27 07:41:13 -0700138 , fInColor(nullptr)
joshualitt02b05012015-02-11 06:56:30 -0800139 , fMaskFormat(format) {
egdaniel309e3462014-12-09 10:35:58 -0800140 this->initClassID<GrBitmapTextGeoProc>();
joshualitt71c92602015-01-14 08:12:47 -0800141 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt02b05012015-02-11 06:56:30 -0800142
joshualittd9d30f72015-12-08 10:47:55 -0800143 bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat ||
144 kA565_GrMaskFormat == fMaskFormat;
joshualitt02b05012015-02-11 06:56:30 -0800145 if (hasVertexColor) {
joshualitt71c92602015-01-14 08:12:47 -0800146 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
joshualitt2dd1ae02014-12-03 06:24:10 -0800147 }
joshualitt71c92602015-01-14 08:12:47 -0800148 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords",
jvanverth7023a002016-02-22 11:25:32 -0800149 kVec2us_GrVertexAttribType,
150 kHigh_GrSLPrecision));
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000151 this->addTextureAccess(&fTextureAccess);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000152}
153
egdaniel57d3b032015-11-13 11:57:27 -0800154void GrBitmapTextGeoProc::getGLSLProcessorKey(const GrGLSLCaps& caps,
155 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700156 GrGLBitmapTextGeoProc::GenKey(*this, caps, b);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000157}
158
egdaniel57d3b032015-11-13 11:57:27 -0800159GrGLSLPrimitiveProcessor* GrBitmapTextGeoProc::createGLSLInstance(const GrGLSLCaps& caps) const {
joshualitt465283c2015-09-11 08:19:35 -0700160 return new GrGLBitmapTextGeoProc();
joshualitteb2a6762014-12-04 11:35:33 -0800161}
joshualitt9b989322014-12-15 14:16:27 -0800162
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000163///////////////////////////////////////////////////////////////////////////////
164
egdaniel309e3462014-12-09 10:35:58 -0800165GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000166
bsalomonc21b09e2015-08-28 18:46:56 -0700167const GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700168 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
169 GrProcessorUnitTest::kAlphaTextureIdx;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000170 static const SkShader::TileMode kTileModes[] = {
171 SkShader::kClamp_TileMode,
172 SkShader::kRepeat_TileMode,
173 SkShader::kMirror_TileMode,
174 };
175 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700176 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
177 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000178 };
joshualitt0067ff52015-07-08 14:26:19 -0700179 GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode :
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000180 GrTextureParams::kNone_FilterMode);
181
robertphillipsb63c5762016-04-08 05:24:21 -0700182 GrMaskFormat format = kARGB_GrMaskFormat; // init to avoid warning
joshualitt0067ff52015-07-08 14:26:19 -0700183 switch (d->fRandom->nextULessThan(3)) {
joshualitt02b05012015-02-11 06:56:30 -0800184 case 0:
185 format = kA8_GrMaskFormat;
186 break;
187 case 1:
188 format = kA565_GrMaskFormat;
189 break;
190 case 2:
191 format = kARGB_GrMaskFormat;
192 break;
193 }
194
joshualitt0067ff52015-07-08 14:26:19 -0700195 return GrBitmapTextGeoProc::Create(GrRandomColor(d->fRandom), d->fTextures[texIdx], params,
196 format, GrTest::TestMatrix(d->fRandom),
197 d->fRandom->nextBool());
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000198}