blob: 263676d353aee72e054bad65cbbca632f42eaaa0 [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);
34 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 SkScalar recipWidth = 1.0f / atlas->width();
37 SkScalar recipHeight = 1.0f / atlas->height();
38
egdaniel8dcdedc2015-11-11 06:27:20 -080039 GrGLSLVertToFrag v(kVec2f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -080040 varyingHandler->addVarying("TextureCoords", &v);
egdaniel4ca2e602015-11-18 08:01:26 -080041 vertBuilder->codeAppendf("%s = vec2(%.*f, %.*f) * %s;", v.vsOut(),
42 GR_SIGNIFICANT_POW2_DECIMAL_DIG, recipWidth,
43 GR_SIGNIFICANT_POW2_DECIMAL_DIG, recipHeight,
44 cte.inTextureCoords()->fName);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000045
cdalton85285412016-02-18 12:37:07 -080046 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt9b989322014-12-15 14:16:27 -080047 // Setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -070048 if (!cte.colorIgnored()) {
49 if (cte.hasVertexColor()) {
egdaniel0eafe792015-11-20 14:01:22 -080050 varyingHandler->addPassThroughAttribute(cte.inColor(), args.fOutputColor);
joshualittb8c241a2015-05-19 08:23:30 -070051 } else {
egdaniel7ea439b2015-12-03 09:20:44 -080052 this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor,
53 &fColorUniform);
joshualittb8c241a2015-05-19 08:23:30 -070054 }
55 }
joshualitt2dd1ae02014-12-03 06:24:10 -080056
joshualittabb52a12015-01-13 15:02:10 -080057 // Setup position
egdaniel7ea439b2015-12-03 09:20:44 -080058 this->setupPosition(vertBuilder, gpArgs, cte.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -080059
joshualittabb52a12015-01-13 15:02:10 -080060 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -080061 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -080062 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -080063 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -080064 gpArgs->fPositionVar,
65 cte.inPosition()->fName,
66 cte.localMatrix(),
67 args.fTransformsIn,
68 args.fTransformsOut);
joshualittabb52a12015-01-13 15:02:10 -080069
joshualitt02b05012015-02-11 06:56:30 -080070 if (cte.maskFormat() == kARGB_GrMaskFormat) {
egdaniel4ca2e602015-11-18 08:01:26 -080071 fragBuilder->codeAppendf("%s = ", args.fOutputColor);
72 fragBuilder->appendTextureLookupAndModulate(args.fOutputColor,
73 args.fSamplers[0],
74 v.fsIn(),
75 kVec2f_GrSLType);
76 fragBuilder->codeAppend(";");
77 fragBuilder->codeAppendf("%s = vec4(1);", args.fOutputCoverage);
joshualitt02b05012015-02-11 06:56:30 -080078 } else {
egdaniel4ca2e602015-11-18 08:01:26 -080079 fragBuilder->codeAppendf("%s = ", args.fOutputCoverage);
80 fragBuilder->appendTextureLookup(args.fSamplers[0], v.fsIn(), kVec2f_GrSLType);
81 fragBuilder->codeAppend(";");
egdaniel27b63352015-09-15 13:13:50 -070082 if (cte.maskFormat() == kA565_GrMaskFormat) {
83 // set alpha to be max of rgb coverage
egdaniel4ca2e602015-11-18 08:01:26 -080084 fragBuilder->codeAppendf("%s.a = max(max(%s.r, %s.g), %s.b);",
85 args.fOutputCoverage, args.fOutputCoverage,
86 args.fOutputCoverage, args.fOutputCoverage);
egdaniel27b63352015-09-15 13:13:50 -070087 }
joshualitt02b05012015-02-11 06:56:30 -080088 }
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000089 }
90
egdaniel018fb622015-10-28 07:26:40 -070091 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp) override {
joshualittb8c241a2015-05-19 08:23:30 -070092 const GrBitmapTextGeoProc& btgp = gp.cast<GrBitmapTextGeoProc>();
93 if (btgp.color() != fColor && !btgp.hasVertexColor()) {
egdaniel018fb622015-10-28 07:26:40 -070094 float c[4];
joshualittb8c241a2015-05-19 08:23:30 -070095 GrColorToRGBAFloat(btgp.color(), c);
joshualitt9b989322014-12-15 14:16:27 -080096 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -070097 fColor = btgp.color();
joshualitt9b989322014-12-15 14:16:27 -080098 }
joshualitt2dd1ae02014-12-03 06:24:10 -080099 }
100
joshualitte3ababe2015-05-15 07:56:07 -0700101 void setTransformData(const GrPrimitiveProcessor& primProc,
egdaniel018fb622015-10-28 07:26:40 -0700102 const GrGLSLProgramDataManager& pdman,
joshualitte3ababe2015-05-15 07:56:07 -0700103 int index,
104 const SkTArray<const GrCoordTransform*, true>& transforms) override {
105 this->setTransformDataHelper<GrBitmapTextGeoProc>(primProc, pdman, index, transforms);
106 }
107
joshualitt9b989322014-12-15 14:16:27 -0800108 static inline void GenKey(const GrGeometryProcessor& proc,
jvanverthcfc18862015-04-28 08:48:20 -0700109 const GrGLSLCaps&,
joshualitt9b989322014-12-15 14:16:27 -0800110 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800111 const GrBitmapTextGeoProc& gp = proc.cast<GrBitmapTextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800112 uint32_t key = 0;
joshualittb8c241a2015-05-19 08:23:30 -0700113 key |= gp.usesLocalCoords() && gp.localMatrix().hasPerspective() ? 0x1 : 0x0;
114 key |= gp.colorIgnored() ? 0x2 : 0x0;
115 key |= gp.maskFormat() << 3;
116 b->add32(key);
joshualitt922c8b12015-08-07 09:55:23 -0700117
118 // Currently we hardcode numbers to convert atlas coordinates to normalized floating point
119 SkASSERT(gp.numTextures() == 1);
120 GrTexture* atlas = gp.textureAccess(0).getTexture();
121 SkASSERT(atlas);
122 b->add32(atlas->width());
123 b->add32(atlas->height());
joshualitt9b989322014-12-15 14:16:27 -0800124 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800125
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000126private:
joshualitt9b989322014-12-15 14:16:27 -0800127 GrColor fColor;
128 UniformHandle fColorUniform;
129
egdaniele659a582015-11-13 09:55:43 -0800130 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000131};
132
133///////////////////////////////////////////////////////////////////////////////
134
joshualitt2e3b3e32014-12-09 13:31:14 -0800135GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color, GrTexture* texture,
joshualitt02b05012015-02-11 06:56:30 -0800136 const GrTextureParams& params, GrMaskFormat format,
joshualittb8c241a2015-05-19 08:23:30 -0700137 const SkMatrix& localMatrix, bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700138 : fColor(color)
139 , fLocalMatrix(localMatrix)
joshualittb8c241a2015-05-19 08:23:30 -0700140 , fUsesLocalCoords(usesLocalCoords)
joshualitt8fc6c2d2014-12-22 15:27:05 -0800141 , fTextureAccess(texture, params)
halcanary96fcdcc2015-08-27 07:41:13 -0700142 , fInColor(nullptr)
joshualitt02b05012015-02-11 06:56:30 -0800143 , fMaskFormat(format) {
egdaniel309e3462014-12-09 10:35:58 -0800144 this->initClassID<GrBitmapTextGeoProc>();
joshualitt71c92602015-01-14 08:12:47 -0800145 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt02b05012015-02-11 06:56:30 -0800146
joshualittd9d30f72015-12-08 10:47:55 -0800147 bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat ||
148 kA565_GrMaskFormat == fMaskFormat;
joshualitt02b05012015-02-11 06:56:30 -0800149 if (hasVertexColor) {
joshualitt71c92602015-01-14 08:12:47 -0800150 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
joshualitt2dd1ae02014-12-03 06:24:10 -0800151 }
joshualitt71c92602015-01-14 08:12:47 -0800152 fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords",
jvanverth5a105ff2015-02-18 11:36:35 -0800153 kVec2s_GrVertexAttribType));
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000154 this->addTextureAccess(&fTextureAccess);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000155}
156
egdaniel57d3b032015-11-13 11:57:27 -0800157void GrBitmapTextGeoProc::getGLSLProcessorKey(const GrGLSLCaps& caps,
158 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700159 GrGLBitmapTextGeoProc::GenKey(*this, caps, b);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000160}
161
egdaniel57d3b032015-11-13 11:57:27 -0800162GrGLSLPrimitiveProcessor* GrBitmapTextGeoProc::createGLSLInstance(const GrGLSLCaps& caps) const {
joshualitt465283c2015-09-11 08:19:35 -0700163 return new GrGLBitmapTextGeoProc();
joshualitteb2a6762014-12-04 11:35:33 -0800164}
joshualitt9b989322014-12-15 14:16:27 -0800165
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000166///////////////////////////////////////////////////////////////////////////////
167
egdaniel309e3462014-12-09 10:35:58 -0800168GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000169
bsalomonc21b09e2015-08-28 18:46:56 -0700170const GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700171 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
172 GrProcessorUnitTest::kAlphaTextureIdx;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000173 static const SkShader::TileMode kTileModes[] = {
174 SkShader::kClamp_TileMode,
175 SkShader::kRepeat_TileMode,
176 SkShader::kMirror_TileMode,
177 };
178 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700179 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
180 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000181 };
joshualitt0067ff52015-07-08 14:26:19 -0700182 GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode :
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000183 GrTextureParams::kNone_FilterMode);
184
joshualitt02b05012015-02-11 06:56:30 -0800185 GrMaskFormat format;
joshualitt0067ff52015-07-08 14:26:19 -0700186 switch (d->fRandom->nextULessThan(3)) {
joshualitt02b05012015-02-11 06:56:30 -0800187 case 0:
188 format = kA8_GrMaskFormat;
189 break;
190 case 1:
191 format = kA565_GrMaskFormat;
192 break;
193 case 2:
194 format = kARGB_GrMaskFormat;
195 break;
196 }
197
joshualitt0067ff52015-07-08 14:26:19 -0700198 return GrBitmapTextGeoProc::Create(GrRandomColor(d->fRandom), d->fTextures[texIdx], params,
199 format, GrTest::TestMatrix(d->fRandom),
200 d->fRandom->nextBool());
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000201}