blob: e0ec49c271373ed38cbd2b0e0dcae01a10df1451 [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,
cdalton3f6f76f2016-04-11 12:18:09 -070069 args.fTexSamplers[0],
egdaniel4ca2e602015-11-18 08:01:26 -080070 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);
cdalton3f6f76f2016-04-11 12:18:09 -070076 fragBuilder->appendTextureLookup(args.fTexSamplers[0], v.fsIn(), kVec2f_GrSLType);
egdaniel4ca2e602015-11-18 08:01:26 -080077 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 {
bsalomone4f24612016-08-17 10:30:17 -0700101 this->setTransformDataHelper(primProc.cast<GrBitmapTextGeoProc>().localMatrix(), pdman,
102 index, transforms);
joshualitte3ababe2015-05-15 07:56:07 -0700103 }
104
joshualitt9b989322014-12-15 14:16:27 -0800105 static inline void GenKey(const GrGeometryProcessor& proc,
jvanverthcfc18862015-04-28 08:48:20 -0700106 const GrGLSLCaps&,
joshualitt9b989322014-12-15 14:16:27 -0800107 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800108 const GrBitmapTextGeoProc& gp = proc.cast<GrBitmapTextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800109 uint32_t key = 0;
joshualittb8c241a2015-05-19 08:23:30 -0700110 key |= gp.usesLocalCoords() && gp.localMatrix().hasPerspective() ? 0x1 : 0x0;
111 key |= gp.colorIgnored() ? 0x2 : 0x0;
112 key |= gp.maskFormat() << 3;
113 b->add32(key);
joshualitt922c8b12015-08-07 09:55:23 -0700114
115 // Currently we hardcode numbers to convert atlas coordinates to normalized floating point
116 SkASSERT(gp.numTextures() == 1);
117 GrTexture* atlas = gp.textureAccess(0).getTexture();
118 SkASSERT(atlas);
119 b->add32(atlas->width());
120 b->add32(atlas->height());
joshualitt9b989322014-12-15 14:16:27 -0800121 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800122
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000123private:
joshualitt9b989322014-12-15 14:16:27 -0800124 GrColor fColor;
125 UniformHandle fColorUniform;
126
egdaniele659a582015-11-13 09:55:43 -0800127 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000128};
129
130///////////////////////////////////////////////////////////////////////////////
131
joshualitt2e3b3e32014-12-09 13:31:14 -0800132GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture,
joshualitt02b05012015-02-11 06:56:30 -0800133 const GrTextureParams& params, GrMaskFormat format,
joshualittb8c241a2015-05-19 08:23:30 -0700134 const SkMatrix& localMatrix, bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700135 : fColor(color)
136 , fLocalMatrix(localMatrix)
joshualittb8c241a2015-05-19 08:23:30 -0700137 , fUsesLocalCoords(usesLocalCoords)
joshualitt8fc6c2d2014-12-22 15:27:05 -0800138 , fTextureAccess(texture, params)
halcanary96fcdcc2015-08-27 07:41:13 -0700139 , fInColor(nullptr)
joshualitt02b05012015-02-11 06:56:30 -0800140 , fMaskFormat(format) {
egdaniel309e3462014-12-09 10:35:58 -0800141 this->initClassID<GrBitmapTextGeoProc>();
joshualitt71c92602015-01-14 08:12:47 -0800142 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt02b05012015-02-11 06:56:30 -0800143
joshualittd9d30f72015-12-08 10:47:55 -0800144 bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat ||
145 kA565_GrMaskFormat == fMaskFormat;
joshualitt02b05012015-02-11 06:56:30 -0800146 if (hasVertexColor) {
joshualitt71c92602015-01-14 08:12:47 -0800147 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
joshualitt2dd1ae02014-12-03 06:24:10 -0800148 }
joshualitt71c92602015-01-14 08:12:47 -0800149 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords",
jvanverth7023a002016-02-22 11:25:32 -0800150 kVec2us_GrVertexAttribType,
151 kHigh_GrSLPrecision));
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000152 this->addTextureAccess(&fTextureAccess);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000153}
154
egdaniel57d3b032015-11-13 11:57:27 -0800155void GrBitmapTextGeoProc::getGLSLProcessorKey(const GrGLSLCaps& caps,
156 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700157 GrGLBitmapTextGeoProc::GenKey(*this, caps, b);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000158}
159
egdaniel57d3b032015-11-13 11:57:27 -0800160GrGLSLPrimitiveProcessor* GrBitmapTextGeoProc::createGLSLInstance(const GrGLSLCaps& caps) const {
joshualitt465283c2015-09-11 08:19:35 -0700161 return new GrGLBitmapTextGeoProc();
joshualitteb2a6762014-12-04 11:35:33 -0800162}
joshualitt9b989322014-12-15 14:16:27 -0800163
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000164///////////////////////////////////////////////////////////////////////////////
165
egdaniel309e3462014-12-09 10:35:58 -0800166GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000167
bungeman06ca8ec2016-06-09 08:01:03 -0700168sk_sp<GrGeometryProcessor> GrBitmapTextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700169 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
170 GrProcessorUnitTest::kAlphaTextureIdx;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000171 static const SkShader::TileMode kTileModes[] = {
172 SkShader::kClamp_TileMode,
173 SkShader::kRepeat_TileMode,
174 SkShader::kMirror_TileMode,
175 };
176 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700177 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
178 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000179 };
joshualitt0067ff52015-07-08 14:26:19 -0700180 GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode :
bungeman06ca8ec2016-06-09 08:01:03 -0700181 GrTextureParams::kNone_FilterMode);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000182
robertphillipsb63c5762016-04-08 05:24:21 -0700183 GrMaskFormat format = kARGB_GrMaskFormat; // init to avoid warning
joshualitt0067ff52015-07-08 14:26:19 -0700184 switch (d->fRandom->nextULessThan(3)) {
joshualitt02b05012015-02-11 06:56:30 -0800185 case 0:
186 format = kA8_GrMaskFormat;
187 break;
188 case 1:
189 format = kA565_GrMaskFormat;
190 break;
191 case 2:
192 format = kARGB_GrMaskFormat;
193 break;
194 }
195
bungeman06ca8ec2016-06-09 08:01:03 -0700196 return GrBitmapTextGeoProc::Make(GrRandomColor(d->fRandom), d->fTextures[texIdx], params,
197 format, GrTest::TestMatrix(d->fRandom),
198 d->fRandom->nextBool());
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000199}