blob: 7f5366363f9d568bb56206adf2aeeffbb60eb5cc [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(),
bsalomona624bf32016-09-20 09:12:47 -070063 args.fFPCoordTransformHandler);
joshualittabb52a12015-01-13 15:02:10 -080064
joshualitt02b05012015-02-11 06:56:30 -080065 if (cte.maskFormat() == kARGB_GrMaskFormat) {
egdaniel4ca2e602015-11-18 08:01:26 -080066 fragBuilder->codeAppendf("%s = ", args.fOutputColor);
67 fragBuilder->appendTextureLookupAndModulate(args.fOutputColor,
cdalton3f6f76f2016-04-11 12:18:09 -070068 args.fTexSamplers[0],
egdaniel4ca2e602015-11-18 08:01:26 -080069 v.fsIn(),
70 kVec2f_GrSLType);
71 fragBuilder->codeAppend(";");
72 fragBuilder->codeAppendf("%s = vec4(1);", args.fOutputCoverage);
joshualitt02b05012015-02-11 06:56:30 -080073 } else {
egdaniel4ca2e602015-11-18 08:01:26 -080074 fragBuilder->codeAppendf("%s = ", args.fOutputCoverage);
cdalton3f6f76f2016-04-11 12:18:09 -070075 fragBuilder->appendTextureLookup(args.fTexSamplers[0], v.fsIn(), kVec2f_GrSLType);
egdaniel4ca2e602015-11-18 08:01:26 -080076 fragBuilder->codeAppend(";");
egdaniel27b63352015-09-15 13:13:50 -070077 if (cte.maskFormat() == kA565_GrMaskFormat) {
78 // set alpha to be max of rgb coverage
egdaniel4ca2e602015-11-18 08:01:26 -080079 fragBuilder->codeAppendf("%s.a = max(max(%s.r, %s.g), %s.b);",
80 args.fOutputCoverage, args.fOutputCoverage,
81 args.fOutputCoverage, args.fOutputCoverage);
egdaniel27b63352015-09-15 13:13:50 -070082 }
joshualitt02b05012015-02-11 06:56:30 -080083 }
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000084 }
85
bsalomona624bf32016-09-20 09:12:47 -070086 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp,
87 FPCoordTransformIter&& transformIter) 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 }
bsalomona624bf32016-09-20 09:12:47 -070095 this->setTransformDataHelper(btgp.localMatrix(), pdman, &transformIter);
joshualitte3ababe2015-05-15 07:56:07 -070096 }
97
joshualitt9b989322014-12-15 14:16:27 -080098 static inline void GenKey(const GrGeometryProcessor& proc,
jvanverthcfc18862015-04-28 08:48:20 -070099 const GrGLSLCaps&,
joshualitt9b989322014-12-15 14:16:27 -0800100 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800101 const GrBitmapTextGeoProc& gp = proc.cast<GrBitmapTextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800102 uint32_t key = 0;
joshualittb8c241a2015-05-19 08:23:30 -0700103 key |= gp.usesLocalCoords() && gp.localMatrix().hasPerspective() ? 0x1 : 0x0;
104 key |= gp.colorIgnored() ? 0x2 : 0x0;
105 key |= gp.maskFormat() << 3;
106 b->add32(key);
joshualitt922c8b12015-08-07 09:55:23 -0700107
108 // Currently we hardcode numbers to convert atlas coordinates to normalized floating point
109 SkASSERT(gp.numTextures() == 1);
110 GrTexture* atlas = gp.textureAccess(0).getTexture();
111 SkASSERT(atlas);
112 b->add32(atlas->width());
113 b->add32(atlas->height());
joshualitt9b989322014-12-15 14:16:27 -0800114 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800115
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000116private:
joshualitt9b989322014-12-15 14:16:27 -0800117 GrColor fColor;
118 UniformHandle fColorUniform;
119
egdaniele659a582015-11-13 09:55:43 -0800120 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000121};
122
123///////////////////////////////////////////////////////////////////////////////
124
joshualitt2e3b3e32014-12-09 13:31:14 -0800125GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture,
joshualitt02b05012015-02-11 06:56:30 -0800126 const GrTextureParams& params, GrMaskFormat format,
joshualittb8c241a2015-05-19 08:23:30 -0700127 const SkMatrix& localMatrix, bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700128 : fColor(color)
129 , fLocalMatrix(localMatrix)
joshualittb8c241a2015-05-19 08:23:30 -0700130 , fUsesLocalCoords(usesLocalCoords)
joshualitt8fc6c2d2014-12-22 15:27:05 -0800131 , fTextureAccess(texture, params)
halcanary96fcdcc2015-08-27 07:41:13 -0700132 , fInColor(nullptr)
joshualitt02b05012015-02-11 06:56:30 -0800133 , fMaskFormat(format) {
egdaniel309e3462014-12-09 10:35:58 -0800134 this->initClassID<GrBitmapTextGeoProc>();
bsalomon6cb807b2016-08-17 11:33:39 -0700135 fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType);
joshualitt02b05012015-02-11 06:56:30 -0800136
joshualittd9d30f72015-12-08 10:47:55 -0800137 bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat ||
138 kA565_GrMaskFormat == fMaskFormat;
joshualitt02b05012015-02-11 06:56:30 -0800139 if (hasVertexColor) {
bsalomon6cb807b2016-08-17 11:33:39 -0700140 fInColor = &this->addVertexAttrib("inColor", kVec4ub_GrVertexAttribType);
joshualitt2dd1ae02014-12-03 06:24:10 -0800141 }
bsalomon6cb807b2016-08-17 11:33:39 -0700142 fInTextureCoords = &this->addVertexAttrib("inTextureCoords", kVec2us_GrVertexAttribType,
143 kHigh_GrSLPrecision);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000144 this->addTextureAccess(&fTextureAccess);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000145}
146
egdaniel57d3b032015-11-13 11:57:27 -0800147void GrBitmapTextGeoProc::getGLSLProcessorKey(const GrGLSLCaps& caps,
148 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700149 GrGLBitmapTextGeoProc::GenKey(*this, caps, b);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000150}
151
egdaniel57d3b032015-11-13 11:57:27 -0800152GrGLSLPrimitiveProcessor* GrBitmapTextGeoProc::createGLSLInstance(const GrGLSLCaps& caps) const {
joshualitt465283c2015-09-11 08:19:35 -0700153 return new GrGLBitmapTextGeoProc();
joshualitteb2a6762014-12-04 11:35:33 -0800154}
joshualitt9b989322014-12-15 14:16:27 -0800155
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000156///////////////////////////////////////////////////////////////////////////////
157
egdaniel309e3462014-12-09 10:35:58 -0800158GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000159
bungeman06ca8ec2016-06-09 08:01:03 -0700160sk_sp<GrGeometryProcessor> GrBitmapTextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700161 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
162 GrProcessorUnitTest::kAlphaTextureIdx;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000163 static const SkShader::TileMode kTileModes[] = {
164 SkShader::kClamp_TileMode,
165 SkShader::kRepeat_TileMode,
166 SkShader::kMirror_TileMode,
167 };
168 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700169 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
170 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000171 };
joshualitt0067ff52015-07-08 14:26:19 -0700172 GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode :
bungeman06ca8ec2016-06-09 08:01:03 -0700173 GrTextureParams::kNone_FilterMode);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000174
robertphillipsb63c5762016-04-08 05:24:21 -0700175 GrMaskFormat format = kARGB_GrMaskFormat; // init to avoid warning
joshualitt0067ff52015-07-08 14:26:19 -0700176 switch (d->fRandom->nextULessThan(3)) {
joshualitt02b05012015-02-11 06:56:30 -0800177 case 0:
178 format = kA8_GrMaskFormat;
179 break;
180 case 1:
181 format = kA565_GrMaskFormat;
182 break;
183 case 2:
184 format = kARGB_GrMaskFormat;
185 break;
186 }
187
bungeman06ca8ec2016-06-09 08:01:03 -0700188 return GrBitmapTextGeoProc::Make(GrRandomColor(d->fRandom), d->fTextures[texIdx], params,
189 format, GrTest::TestMatrix(d->fRandom),
190 d->fRandom->nextBool());
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000191}