blob: 32da45c10f246a1a20fb8e50c39ad2e627348615 [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"
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -05009
Jim Van Verth6a7a7042017-09-11 11:04:10 -040010#include "GrAtlasedShaderHelpers.h"
joshualitteb2a6762014-12-04 11:35:33 -080011#include "GrTexture.h"
egdaniel2d721d32015-11-11 13:06:05 -080012#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniele659a582015-11-13 09:55:43 -080013#include "glsl/GrGLSLGeometryProcessor.h"
egdaniel018fb622015-10-28 07:26:40 -070014#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080015#include "glsl/GrGLSLUniformHandler.h"
egdaniel0eafe792015-11-20 14:01:22 -080016#include "glsl/GrGLSLVarying.h"
Chris Daltonc17bf322017-10-24 10:59:03 -060017#include "glsl/GrGLSLVertexGeoBuilder.h"
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000018
egdaniele659a582015-11-13 09:55:43 -080019class GrGLBitmapTextGeoProc : public GrGLSLGeometryProcessor {
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000020public:
Robert Phillips8296e752017-08-25 08:45:21 -040021 GrGLBitmapTextGeoProc() : fColor(GrColor_ILLEGAL), fAtlasSize({0,0}) {}
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000022
joshualitt465283c2015-09-11 08:19:35 -070023 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
Robert Phillips8296e752017-08-25 08:45:21 -040024 const GrBitmapTextGeoProc& btgp = args.fGP.cast<GrBitmapTextGeoProc>();
joshualitt2dd1ae02014-12-03 06:24:10 -080025
egdaniel4ca2e602015-11-18 08:01:26 -080026 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -080027 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -080028 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000029
joshualittabb52a12015-01-13 15:02:10 -080030 // emit attributes
Robert Phillips8296e752017-08-25 08:45:21 -040031 varyingHandler->emitAttributes(btgp);
joshualittabb52a12015-01-13 15:02:10 -080032
Robert Phillips8296e752017-08-25 08:45:21 -040033 const char* atlasSizeInvName;
34 fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040035 kFloat2_GrSLType,
Robert Phillips8296e752017-08-25 08:45:21 -040036 kHigh_GrSLPrecision,
37 "AtlasSizeInv",
38 &atlasSizeInvName);
joshualitt922c8b12015-08-07 09:55:23 -070039
Chris Dalton27372882017-12-08 13:34:21 -070040 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verthfc4f7682018-01-25 16:26:25 -050041 GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
42 GrGLSLVarying texIdx(texIdxType);
Brian Salomon92be2f72018-06-19 14:33:47 -040043 append_index_uv_varyings(args, btgp.inTextureCoords().name(), atlasSizeInvName, &uv,
Brian Salomon70132d02018-05-29 15:33:06 -040044 &texIdx, nullptr);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000045
Chris Dalton60283612018-02-14 13:38:14 -070046 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt9b989322014-12-15 14:16:27 -080047 // Setup pass through color
Robert Phillips8296e752017-08-25 08:45:21 -040048 if (btgp.hasVertexColor()) {
49 varyingHandler->addPassThroughAttribute(btgp.inColor(), args.fOutputColor);
Brian Salomonbfd51832017-01-04 13:22:08 -050050 } else {
51 this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor,
52 &fColorUniform);
joshualittb8c241a2015-05-19 08:23:30 -070053 }
joshualitt2dd1ae02014-12-03 06:24:10 -080054
joshualittabb52a12015-01-13 15:02:10 -080055 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -040056 gpArgs->fPositionVar = btgp.inPosition().asShaderVar();
joshualitt4973d9d2014-11-08 09:24:25 -080057
joshualittabb52a12015-01-13 15:02:10 -080058 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -080059 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -080060 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -080061 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -040062 btgp.inPosition().asShaderVar(),
Robert Phillips8296e752017-08-25 08:45:21 -040063 btgp.localMatrix(),
bsalomona624bf32016-09-20 09:12:47 -070064 args.fFPCoordTransformHandler);
joshualittabb52a12015-01-13 15:02:10 -080065
Ethan Nicholasf7b88202017-09-18 14:10:39 -040066 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -040067 append_multitexture_lookup(args, btgp.numTextureSamplers(),
68 texIdx, uv.fsIn(), "texColor");
69
Robert Phillips8296e752017-08-25 08:45:21 -040070 if (btgp.maskFormat() == kARGB_GrMaskFormat) {
Jim Van Verth6a7a7042017-09-11 11:04:10 -040071 // modulate by color
72 fragBuilder->codeAppendf("%s = %s * texColor;", args.fOutputColor, args.fOutputColor);
Ethan Nicholasf7b88202017-09-18 14:10:39 -040073 fragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
joshualitt02b05012015-02-11 06:56:30 -080074 } else {
Jim Van Verth6a7a7042017-09-11 11:04:10 -040075 fragBuilder->codeAppendf("%s = texColor;", args.fOutputCoverage);
joshualitt02b05012015-02-11 06:56:30 -080076 }
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000077 }
78
bsalomona624bf32016-09-20 09:12:47 -070079 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp,
80 FPCoordTransformIter&& transformIter) override {
joshualittb8c241a2015-05-19 08:23:30 -070081 const GrBitmapTextGeoProc& btgp = gp.cast<GrBitmapTextGeoProc>();
82 if (btgp.color() != fColor && !btgp.hasVertexColor()) {
egdaniel018fb622015-10-28 07:26:40 -070083 float c[4];
joshualittb8c241a2015-05-19 08:23:30 -070084 GrColorToRGBAFloat(btgp.color(), c);
joshualitt9b989322014-12-15 14:16:27 -080085 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -070086 fColor = btgp.color();
joshualitt9b989322014-12-15 14:16:27 -080087 }
Robert Phillips8296e752017-08-25 08:45:21 -040088
Jim Van Verth6a7a7042017-09-11 11:04:10 -040089 SkASSERT(btgp.numTextureSamplers() >= 1);
Robert Phillips8296e752017-08-25 08:45:21 -040090 GrTexture* atlas = btgp.textureSampler(0).peekTexture();
91 SkASSERT(atlas && SkIsPow2(atlas->width()) && SkIsPow2(atlas->height()));
92
93 if (fAtlasSize.fWidth != atlas->width() || fAtlasSize.fHeight != atlas->height()) {
94 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlas->width(), 1.0f / atlas->height());
95 fAtlasSize.set(atlas->width(), atlas->height());
96 }
bsalomona624bf32016-09-20 09:12:47 -070097 this->setTransformDataHelper(btgp.localMatrix(), pdman, &transformIter);
joshualitte3ababe2015-05-15 07:56:07 -070098 }
99
joshualitt9b989322014-12-15 14:16:27 -0800100 static inline void GenKey(const GrGeometryProcessor& proc,
Brian Salomon94efbf52016-11-29 13:43:05 -0500101 const GrShaderCaps&,
joshualitt9b989322014-12-15 14:16:27 -0800102 GrProcessorKeyBuilder* b) {
Robert Phillips8296e752017-08-25 08:45:21 -0400103 const GrBitmapTextGeoProc& btgp = proc.cast<GrBitmapTextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800104 uint32_t key = 0;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400105 key |= btgp.usesW() ? 0x1 : 0x0;
Robert Phillips8296e752017-08-25 08:45:21 -0400106 key |= btgp.maskFormat() << 1;
joshualittb8c241a2015-05-19 08:23:30 -0700107 b->add32(key);
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400108 b->add32(btgp.numTextureSamplers());
joshualitt9b989322014-12-15 14:16:27 -0800109 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800110
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000111private:
Robert Phillips8296e752017-08-25 08:45:21 -0400112 GrColor fColor;
joshualitt9b989322014-12-15 14:16:27 -0800113 UniformHandle fColorUniform;
114
Robert Phillips8296e752017-08-25 08:45:21 -0400115 SkISize fAtlasSize;
116 UniformHandle fAtlasSizeInvUniform;
117
egdaniele659a582015-11-13 09:55:43 -0800118 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000119};
120
121///////////////////////////////////////////////////////////////////////////////
122
Jim Van Vertha950b632017-09-12 11:54:11 -0400123GrBitmapTextGeoProc::GrBitmapTextGeoProc(GrColor color,
Robert Phillips4bc70112018-03-01 10:24:02 -0500124 const sk_sp<GrTextureProxy>* proxies,
Jim Van Verthcbeae032018-05-16 14:54:41 -0400125 int numActiveProxies,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400126 const GrSamplerState& params, GrMaskFormat format,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400127 const SkMatrix& localMatrix, bool usesW)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400128 : INHERITED(kGrBitmapTextGeoProc_ClassID)
129 , fColor(color)
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400130 , fLocalMatrix(localMatrix)
Jim Van Verthb515ae72018-05-23 16:44:55 -0400131 , fUsesW(usesW)
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400132 , fMaskFormat(format) {
Jim Van Verthcbeae032018-05-16 14:54:41 -0400133 SkASSERT(numActiveProxies <= kMaxTextures);
Robert Phillips4bc70112018-03-01 10:24:02 -0500134
Jim Van Verthb515ae72018-05-23 16:44:55 -0400135 if (usesW) {
Brian Salomon92be2f72018-06-19 14:33:47 -0400136 fInPosition = {"inPosition", kFloat3_GrVertexAttribType};
Jim Van Verthb515ae72018-05-23 16:44:55 -0400137 } else {
Brian Salomon92be2f72018-06-19 14:33:47 -0400138 fInPosition = {"inPosition", kFloat2_GrVertexAttribType};
Jim Van Verthb515ae72018-05-23 16:44:55 -0400139 }
Brian Salomon92be2f72018-06-19 14:33:47 -0400140 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType};
141 int cnt = 2;
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500142
143 bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat ||
144 kA565_GrMaskFormat == fMaskFormat;
145 if (hasVertexColor) {
Brian Salomon92be2f72018-06-19 14:33:47 -0400146 fInColor = {"inColor", kUByte4_norm_GrVertexAttribType};
147 ++cnt;
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500148 }
Robert Phillips8296e752017-08-25 08:45:21 -0400149
Brian Salomon92be2f72018-06-19 14:33:47 -0400150 this->setVertexAttributeCnt(cnt);
151
Jim Van Verthcbeae032018-05-16 14:54:41 -0400152 for (int i = 0; i < numActiveProxies; ++i) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500153 SkASSERT(proxies[i]);
Robert Phillips4bc70112018-03-01 10:24:02 -0500154 fTextureSamplers[i].reset(std::move(proxies[i]), params);
Jim Van Vertha950b632017-09-12 11:54:11 -0400155 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400156 this->setTextureSamplerCnt(numActiveProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500157}
158
Brian Salomon92be2f72018-06-19 14:33:47 -0400159const GrPrimitiveProcessor::Attribute& GrBitmapTextGeoProc::onVertexAttribute(int i) const {
160 return IthInitializedAttribute(i, fInPosition, fInColor, fInTextureCoords);
161}
162
Robert Phillips4bc70112018-03-01 10:24:02 -0500163void GrBitmapTextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
Jim Van Verthcbeae032018-05-16 14:54:41 -0400164 int numActiveProxies,
Robert Phillips4bc70112018-03-01 10:24:02 -0500165 const GrSamplerState& params) {
Jim Van Verthcbeae032018-05-16 14:54:41 -0400166 SkASSERT(numActiveProxies <= kMaxTextures);
Robert Phillips4bc70112018-03-01 10:24:02 -0500167
Jim Van Verthcbeae032018-05-16 14:54:41 -0400168 for (int i = 0; i < numActiveProxies; ++i) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500169 SkASSERT(proxies[i]);
170
171 if (!fTextureSamplers[i].isInitialized()) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400172 fTextureSamplers[i].reset(std::move(proxies[i]), params);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400173 }
174 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400175 this->setTextureSamplerCnt(numActiveProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400176}
177
Brian Salomon94efbf52016-11-29 13:43:05 -0500178void GrBitmapTextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800179 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700180 GrGLBitmapTextGeoProc::GenKey(*this, caps, b);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000181}
182
Brian Salomon94efbf52016-11-29 13:43:05 -0500183GrGLSLPrimitiveProcessor* GrBitmapTextGeoProc::createGLSLInstance(const GrShaderCaps& caps) const {
joshualitt465283c2015-09-11 08:19:35 -0700184 return new GrGLBitmapTextGeoProc();
joshualitteb2a6762014-12-04 11:35:33 -0800185}
joshualitt9b989322014-12-15 14:16:27 -0800186
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000187///////////////////////////////////////////////////////////////////////////////
188
egdaniel309e3462014-12-09 10:35:58 -0800189GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000190
Hal Canary6f6961e2017-01-31 13:50:44 -0500191#if GR_TEST_UTILS
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400192
bungeman06ca8ec2016-06-09 08:01:03 -0700193sk_sp<GrGeometryProcessor> GrBitmapTextGeoProc::TestCreate(GrProcessorTestData* d) {
Brian Salomon514baff2016-11-17 15:17:07 -0500194 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
195 : GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400196 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
197 d->textureProxy(texIdx),
198 nullptr,
199 nullptr,
200 nullptr
201 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500202
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400203 GrSamplerState::WrapMode wrapModes[2];
204 GrTest::TestWrapModes(d->fRandom, wrapModes);
205 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
206 ? GrSamplerState::Filter::kBilerp
207 : GrSamplerState::Filter::kNearest);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000208
robertphillipsb63c5762016-04-08 05:24:21 -0700209 GrMaskFormat format = kARGB_GrMaskFormat; // init to avoid warning
joshualitt0067ff52015-07-08 14:26:19 -0700210 switch (d->fRandom->nextULessThan(3)) {
joshualitt02b05012015-02-11 06:56:30 -0800211 case 0:
212 format = kA8_GrMaskFormat;
213 break;
214 case 1:
215 format = kA565_GrMaskFormat;
216 break;
217 case 2:
218 format = kARGB_GrMaskFormat;
219 break;
220 }
221
Robert Phillips4bc70112018-03-01 10:24:02 -0500222 return GrBitmapTextGeoProc::Make(GrRandomColor(d->fRandom), proxies, 1, samplerState,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400223 format, GrTest::TestMatrix(d->fRandom),
bungeman06ca8ec2016-06-09 08:01:03 -0700224 d->fRandom->nextBool());
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000225}
Hal Canary6f6961e2017-01-31 13:50:44 -0500226#endif