blob: 667f13b6891e3263951f2b773f1d6afb6fb5b932 [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"
Brian Osman4a3f5c82018-09-18 16:16:38 -040011#include "GrCaps.h"
12#include "GrShaderCaps.h"
joshualitteb2a6762014-12-04 11:35:33 -080013#include "GrTexture.h"
egdaniel2d721d32015-11-11 13:06:05 -080014#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniele659a582015-11-13 09:55:43 -080015#include "glsl/GrGLSLGeometryProcessor.h"
egdaniel018fb622015-10-28 07:26:40 -070016#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080017#include "glsl/GrGLSLUniformHandler.h"
egdaniel0eafe792015-11-20 14:01:22 -080018#include "glsl/GrGLSLVarying.h"
Chris Daltonc17bf322017-10-24 10:59:03 -060019#include "glsl/GrGLSLVertexGeoBuilder.h"
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000020
egdaniele659a582015-11-13 09:55:43 -080021class GrGLBitmapTextGeoProc : public GrGLSLGeometryProcessor {
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000022public:
Robert Phillips8296e752017-08-25 08:45:21 -040023 GrGLBitmapTextGeoProc() : fColor(GrColor_ILLEGAL), fAtlasSize({0,0}) {}
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000024
joshualitt465283c2015-09-11 08:19:35 -070025 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
Robert Phillips8296e752017-08-25 08:45:21 -040026 const GrBitmapTextGeoProc& btgp = args.fGP.cast<GrBitmapTextGeoProc>();
joshualitt2dd1ae02014-12-03 06:24:10 -080027
egdaniel4ca2e602015-11-18 08:01:26 -080028 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -080029 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -080030 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000031
joshualittabb52a12015-01-13 15:02:10 -080032 // emit attributes
Robert Phillips8296e752017-08-25 08:45:21 -040033 varyingHandler->emitAttributes(btgp);
joshualittabb52a12015-01-13 15:02:10 -080034
Robert Phillips8296e752017-08-25 08:45:21 -040035 const char* atlasSizeInvName;
36 fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040037 kFloat2_GrSLType,
Robert Phillips8296e752017-08-25 08:45:21 -040038 kHigh_GrSLPrecision,
39 "AtlasSizeInv",
40 &atlasSizeInvName);
joshualitt922c8b12015-08-07 09:55:23 -070041
Chris Dalton27372882017-12-08 13:34:21 -070042 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verthfc4f7682018-01-25 16:26:25 -050043 GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
44 GrGLSLVarying texIdx(texIdxType);
Brian Salomon92be2f72018-06-19 14:33:47 -040045 append_index_uv_varyings(args, btgp.inTextureCoords().name(), atlasSizeInvName, &uv,
Brian Salomon70132d02018-05-29 15:33:06 -040046 &texIdx, nullptr);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000047
Chris Dalton60283612018-02-14 13:38:14 -070048 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt9b989322014-12-15 14:16:27 -080049 // Setup pass through color
Robert Phillips8296e752017-08-25 08:45:21 -040050 if (btgp.hasVertexColor()) {
51 varyingHandler->addPassThroughAttribute(btgp.inColor(), args.fOutputColor);
Brian Salomonbfd51832017-01-04 13:22:08 -050052 } else {
53 this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor,
54 &fColorUniform);
joshualittb8c241a2015-05-19 08:23:30 -070055 }
joshualitt2dd1ae02014-12-03 06:24:10 -080056
joshualittabb52a12015-01-13 15:02:10 -080057 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -040058 gpArgs->fPositionVar = btgp.inPosition().asShaderVar();
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,
Brian Salomon92be2f72018-06-19 14:33:47 -040064 btgp.inPosition().asShaderVar(),
Robert Phillips8296e752017-08-25 08:45:21 -040065 btgp.localMatrix(),
bsalomona624bf32016-09-20 09:12:47 -070066 args.fFPCoordTransformHandler);
joshualittabb52a12015-01-13 15:02:10 -080067
Ethan Nicholasf7b88202017-09-18 14:10:39 -040068 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -040069 append_multitexture_lookup(args, btgp.numTextureSamplers(),
70 texIdx, uv.fsIn(), "texColor");
71
Robert Phillips8296e752017-08-25 08:45:21 -040072 if (btgp.maskFormat() == kARGB_GrMaskFormat) {
Jim Van Verth6a7a7042017-09-11 11:04:10 -040073 // modulate by color
74 fragBuilder->codeAppendf("%s = %s * texColor;", args.fOutputColor, args.fOutputColor);
Ethan Nicholasf7b88202017-09-18 14:10:39 -040075 fragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
joshualitt02b05012015-02-11 06:56:30 -080076 } else {
Jim Van Verth6a7a7042017-09-11 11:04:10 -040077 fragBuilder->codeAppendf("%s = texColor;", args.fOutputCoverage);
joshualitt02b05012015-02-11 06:56:30 -080078 }
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000079 }
80
bsalomona624bf32016-09-20 09:12:47 -070081 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp,
82 FPCoordTransformIter&& transformIter) override {
joshualittb8c241a2015-05-19 08:23:30 -070083 const GrBitmapTextGeoProc& btgp = gp.cast<GrBitmapTextGeoProc>();
84 if (btgp.color() != fColor && !btgp.hasVertexColor()) {
egdaniel018fb622015-10-28 07:26:40 -070085 float c[4];
joshualittb8c241a2015-05-19 08:23:30 -070086 GrColorToRGBAFloat(btgp.color(), c);
joshualitt9b989322014-12-15 14:16:27 -080087 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -070088 fColor = btgp.color();
joshualitt9b989322014-12-15 14:16:27 -080089 }
Robert Phillips8296e752017-08-25 08:45:21 -040090
Brian Salomon7eae3e02018-08-07 14:02:38 +000091 const SkISize& atlasSize = btgp.atlasSize();
92 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
Robert Phillips8296e752017-08-25 08:45:21 -040093
Brian Salomon7eae3e02018-08-07 14:02:38 +000094 if (fAtlasSize != atlasSize) {
95 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
96 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -040097 }
bsalomona624bf32016-09-20 09:12:47 -070098 this->setTransformDataHelper(btgp.localMatrix(), pdman, &transformIter);
joshualitte3ababe2015-05-15 07:56:07 -070099 }
100
joshualitt9b989322014-12-15 14:16:27 -0800101 static inline void GenKey(const GrGeometryProcessor& proc,
Brian Salomon94efbf52016-11-29 13:43:05 -0500102 const GrShaderCaps&,
joshualitt9b989322014-12-15 14:16:27 -0800103 GrProcessorKeyBuilder* b) {
Robert Phillips8296e752017-08-25 08:45:21 -0400104 const GrBitmapTextGeoProc& btgp = proc.cast<GrBitmapTextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800105 uint32_t key = 0;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400106 key |= btgp.usesW() ? 0x1 : 0x0;
Robert Phillips8296e752017-08-25 08:45:21 -0400107 key |= btgp.maskFormat() << 1;
joshualittb8c241a2015-05-19 08:23:30 -0700108 b->add32(key);
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400109 b->add32(btgp.numTextureSamplers());
joshualitt9b989322014-12-15 14:16:27 -0800110 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800111
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000112private:
Robert Phillips8296e752017-08-25 08:45:21 -0400113 GrColor fColor;
joshualitt9b989322014-12-15 14:16:27 -0800114 UniformHandle fColorUniform;
115
Robert Phillips8296e752017-08-25 08:45:21 -0400116 SkISize fAtlasSize;
117 UniformHandle fAtlasSizeInvUniform;
118
egdaniele659a582015-11-13 09:55:43 -0800119 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000120};
121
122///////////////////////////////////////////////////////////////////////////////
123
Brian Osman4a3f5c82018-09-18 16:16:38 -0400124GrBitmapTextGeoProc::GrBitmapTextGeoProc(const GrShaderCaps& caps,
125 GrColor color,
Robert Phillips4bc70112018-03-01 10:24:02 -0500126 const sk_sp<GrTextureProxy>* proxies,
Jim Van Verthcbeae032018-05-16 14:54:41 -0400127 int numActiveProxies,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400128 const GrSamplerState& params, GrMaskFormat format,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400129 const SkMatrix& localMatrix, bool usesW)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400130 : INHERITED(kGrBitmapTextGeoProc_ClassID)
131 , fColor(color)
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400132 , fLocalMatrix(localMatrix)
Jim Van Verthb515ae72018-05-23 16:44:55 -0400133 , fUsesW(usesW)
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400134 , fMaskFormat(format) {
Jim Van Verthcbeae032018-05-16 14:54:41 -0400135 SkASSERT(numActiveProxies <= kMaxTextures);
Robert Phillips4bc70112018-03-01 10:24:02 -0500136
Jim Van Verthb515ae72018-05-23 16:44:55 -0400137 if (usesW) {
Brian Osmand4c29702018-09-14 16:16:55 -0400138 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Jim Van Verthb515ae72018-05-23 16:44:55 -0400139 } else {
Brian Osmand4c29702018-09-14 16:16:55 -0400140 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Jim Van Verthb515ae72018-05-23 16:44:55 -0400141 }
Brian Osman4a3f5c82018-09-18 16:16:38 -0400142
143 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
144 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType };
Brian Salomon92be2f72018-06-19 14:33:47 -0400145 int cnt = 2;
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500146
147 bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat ||
148 kA565_GrMaskFormat == fMaskFormat;
149 if (hasVertexColor) {
Brian Osmand4c29702018-09-14 16:16:55 -0400150 fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
Brian Salomon92be2f72018-06-19 14:33:47 -0400151 ++cnt;
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500152 }
Robert Phillips8296e752017-08-25 08:45:21 -0400153
Brian Salomon92be2f72018-06-19 14:33:47 -0400154 this->setVertexAttributeCnt(cnt);
155
Brian Salomon7eae3e02018-08-07 14:02:38 +0000156 if (numActiveProxies) {
157 fAtlasSize = proxies[0]->isize();
158 }
Jim Van Verthcbeae032018-05-16 14:54:41 -0400159 for (int i = 0; i < numActiveProxies; ++i) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500160 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000161 SkASSERT(proxies[i]->isize() == fAtlasSize);
162 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertha950b632017-09-12 11:54:11 -0400163 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400164 this->setTextureSamplerCnt(numActiveProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500165}
166
Brian Salomon92be2f72018-06-19 14:33:47 -0400167const GrPrimitiveProcessor::Attribute& GrBitmapTextGeoProc::onVertexAttribute(int i) const {
168 return IthInitializedAttribute(i, fInPosition, fInColor, fInTextureCoords);
169}
170
Robert Phillips4bc70112018-03-01 10:24:02 -0500171void GrBitmapTextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
Jim Van Verthcbeae032018-05-16 14:54:41 -0400172 int numActiveProxies,
Robert Phillips4bc70112018-03-01 10:24:02 -0500173 const GrSamplerState& params) {
Jim Van Verthcbeae032018-05-16 14:54:41 -0400174 SkASSERT(numActiveProxies <= kMaxTextures);
Robert Phillips4bc70112018-03-01 10:24:02 -0500175
Brian Salomon7eae3e02018-08-07 14:02:38 +0000176 if (!fTextureSamplers[0].isInitialized()) {
177 fAtlasSize = proxies[0]->isize();
178 }
179
Jim Van Verthcbeae032018-05-16 14:54:41 -0400180 for (int i = 0; i < numActiveProxies; ++i) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500181 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000182 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500183
184 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000185 fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400186 }
187 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400188 this->setTextureSamplerCnt(numActiveProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400189}
190
Brian Salomon94efbf52016-11-29 13:43:05 -0500191void GrBitmapTextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800192 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700193 GrGLBitmapTextGeoProc::GenKey(*this, caps, b);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000194}
195
Brian Salomon94efbf52016-11-29 13:43:05 -0500196GrGLSLPrimitiveProcessor* GrBitmapTextGeoProc::createGLSLInstance(const GrShaderCaps& caps) const {
joshualitt465283c2015-09-11 08:19:35 -0700197 return new GrGLBitmapTextGeoProc();
joshualitteb2a6762014-12-04 11:35:33 -0800198}
joshualitt9b989322014-12-15 14:16:27 -0800199
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000200///////////////////////////////////////////////////////////////////////////////
201
egdaniel309e3462014-12-09 10:35:58 -0800202GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000203
Hal Canary6f6961e2017-01-31 13:50:44 -0500204#if GR_TEST_UTILS
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400205
bungeman06ca8ec2016-06-09 08:01:03 -0700206sk_sp<GrGeometryProcessor> GrBitmapTextGeoProc::TestCreate(GrProcessorTestData* d) {
Brian Salomon514baff2016-11-17 15:17:07 -0500207 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
208 : GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400209 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
210 d->textureProxy(texIdx),
211 nullptr,
212 nullptr,
213 nullptr
214 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500215
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400216 GrSamplerState::WrapMode wrapModes[2];
217 GrTest::TestWrapModes(d->fRandom, wrapModes);
218 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
219 ? GrSamplerState::Filter::kBilerp
220 : GrSamplerState::Filter::kNearest);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000221
robertphillipsb63c5762016-04-08 05:24:21 -0700222 GrMaskFormat format = kARGB_GrMaskFormat; // init to avoid warning
joshualitt0067ff52015-07-08 14:26:19 -0700223 switch (d->fRandom->nextULessThan(3)) {
joshualitt02b05012015-02-11 06:56:30 -0800224 case 0:
225 format = kA8_GrMaskFormat;
226 break;
227 case 1:
228 format = kA565_GrMaskFormat;
229 break;
230 case 2:
231 format = kARGB_GrMaskFormat;
232 break;
233 }
234
Brian Osman4a3f5c82018-09-18 16:16:38 -0400235 return GrBitmapTextGeoProc::Make(*d->caps()->shaderCaps(), GrRandomColor(d->fRandom), proxies,
236 1, samplerState, format, GrTest::TestMatrix(d->fRandom),
bungeman06ca8ec2016-06-09 08:01:03 -0700237 d->fRandom->nextBool());
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000238}
Hal Canary6f6961e2017-01-31 13:50:44 -0500239#endif