blob: 4a8c25da6c564238e07c43fd57cb2dfaf7e550b8 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/effects/GrBitmapTextGeoProc.h"
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrTexture.h"
11#include "src/gpu/GrCaps.h"
12#include "src/gpu/GrShaderCaps.h"
13#include "src/gpu/effects/GrAtlasedShaderHelpers.h"
14#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
15#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
16#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
17#include "src/gpu/glsl/GrGLSLUniformHandler.h"
18#include "src/gpu/glsl/GrGLSLVarying.h"
19#include "src/gpu/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:
Brian Osmancf860852018-10-31 14:04:39 -040023 GrGLBitmapTextGeoProc() : fColor(SK_PMColor4fILLEGAL), 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 "AtlasSizeInv",
39 &atlasSizeInvName);
joshualitt922c8b12015-08-07 09:55:23 -070040
Chris Dalton27372882017-12-08 13:34:21 -070041 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verth1694a862018-12-17 19:48:42 +000042 GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
43 GrGLSLVarying texIdx(texIdxType);
Brian Salomon92be2f72018-06-19 14:33:47 -040044 append_index_uv_varyings(args, btgp.inTextureCoords().name(), atlasSizeInvName, &uv,
Brian Salomon70132d02018-05-29 15:33:06 -040045 &texIdx, nullptr);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000046
Chris Dalton60283612018-02-14 13:38:14 -070047 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt9b989322014-12-15 14:16:27 -080048 // Setup pass through color
Robert Phillips8296e752017-08-25 08:45:21 -040049 if (btgp.hasVertexColor()) {
50 varyingHandler->addPassThroughAttribute(btgp.inColor(), args.fOutputColor);
Brian Salomonbfd51832017-01-04 13:22:08 -050051 } else {
52 this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor,
53 &fColorUniform);
joshualittb8c241a2015-05-19 08:23:30 -070054 }
joshualitt2dd1ae02014-12-03 06:24:10 -080055
joshualittabb52a12015-01-13 15:02:10 -080056 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -040057 gpArgs->fPositionVar = btgp.inPosition().asShaderVar();
joshualitt4973d9d2014-11-08 09:24:25 -080058
joshualittabb52a12015-01-13 15:02:10 -080059 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -080060 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -080061 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -080062 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -040063 btgp.inPosition().asShaderVar(),
Robert Phillips8296e752017-08-25 08:45:21 -040064 btgp.localMatrix(),
bsalomona624bf32016-09-20 09:12:47 -070065 args.fFPCoordTransformHandler);
joshualittabb52a12015-01-13 15:02:10 -080066
Ethan Nicholasf7b88202017-09-18 14:10:39 -040067 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -040068 append_multitexture_lookup(args, btgp.numTextureSamplers(),
69 texIdx, uv.fsIn(), "texColor");
70
Robert Phillips8296e752017-08-25 08:45:21 -040071 if (btgp.maskFormat() == kARGB_GrMaskFormat) {
Jim Van Verth6a7a7042017-09-11 11:04:10 -040072 // modulate by color
73 fragBuilder->codeAppendf("%s = %s * texColor;", args.fOutputColor, args.fOutputColor);
Ethan Nicholasf7b88202017-09-18 14:10:39 -040074 fragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
joshualitt02b05012015-02-11 06:56:30 -080075 } else {
Jim Van Verth6a7a7042017-09-11 11:04:10 -040076 fragBuilder->codeAppendf("%s = texColor;", args.fOutputCoverage);
joshualitt02b05012015-02-11 06:56:30 -080077 }
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000078 }
79
bsalomona624bf32016-09-20 09:12:47 -070080 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp,
81 FPCoordTransformIter&& transformIter) override {
joshualittb8c241a2015-05-19 08:23:30 -070082 const GrBitmapTextGeoProc& btgp = gp.cast<GrBitmapTextGeoProc>();
83 if (btgp.color() != fColor && !btgp.hasVertexColor()) {
Brian Osmancf860852018-10-31 14:04:39 -040084 pdman.set4fv(fColorUniform, 1, btgp.color().vec());
joshualittb8c241a2015-05-19 08:23:30 -070085 fColor = btgp.color();
joshualitt9b989322014-12-15 14:16:27 -080086 }
Robert Phillips8296e752017-08-25 08:45:21 -040087
Brian Salomon7eae3e02018-08-07 14:02:38 +000088 const SkISize& atlasSize = btgp.atlasSize();
89 SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
Robert Phillips8296e752017-08-25 08:45:21 -040090
Brian Salomon7eae3e02018-08-07 14:02:38 +000091 if (fAtlasSize != atlasSize) {
92 pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
93 fAtlasSize = atlasSize;
Robert Phillips8296e752017-08-25 08:45:21 -040094 }
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,
Brian Salomon94efbf52016-11-29 13:43:05 -050099 const GrShaderCaps&,
joshualitt9b989322014-12-15 14:16:27 -0800100 GrProcessorKeyBuilder* b) {
Robert Phillips8296e752017-08-25 08:45:21 -0400101 const GrBitmapTextGeoProc& btgp = proc.cast<GrBitmapTextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800102 uint32_t key = 0;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400103 key |= btgp.usesW() ? 0x1 : 0x0;
Robert Phillips8296e752017-08-25 08:45:21 -0400104 key |= btgp.maskFormat() << 1;
joshualittb8c241a2015-05-19 08:23:30 -0700105 b->add32(key);
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400106 b->add32(btgp.numTextureSamplers());
joshualitt9b989322014-12-15 14:16:27 -0800107 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800108
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000109private:
Brian Osmancf860852018-10-31 14:04:39 -0400110 SkPMColor4f fColor;
joshualitt9b989322014-12-15 14:16:27 -0800111 UniformHandle fColorUniform;
112
Robert Phillips8296e752017-08-25 08:45:21 -0400113 SkISize fAtlasSize;
114 UniformHandle fAtlasSizeInvUniform;
115
egdaniele659a582015-11-13 09:55:43 -0800116 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000117};
118
119///////////////////////////////////////////////////////////////////////////////
120
Brian Osman4a3f5c82018-09-18 16:16:38 -0400121GrBitmapTextGeoProc::GrBitmapTextGeoProc(const GrShaderCaps& caps,
Brian Osmancf860852018-10-31 14:04:39 -0400122 const SkPMColor4f& color,
Brian Osmanc906d252018-12-04 11:17:46 -0500123 bool wideColor,
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 Osmand4c29702018-09-14 16:16:55 -0400136 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Jim Van Verthb515ae72018-05-23 16:44:55 -0400137 } else {
Brian Osmand4c29702018-09-14 16:16:55 -0400138 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Jim Van Verthb515ae72018-05-23 16:44:55 -0400139 }
Brian Osman4a3f5c82018-09-18 16:16:38 -0400140
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500141 bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat ||
142 kA565_GrMaskFormat == fMaskFormat;
143 if (hasVertexColor) {
Brian Osmanc906d252018-12-04 11:17:46 -0500144 fInColor = MakeColorAttribute("inColor", wideColor);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500145 }
Robert Phillips8296e752017-08-25 08:45:21 -0400146
Jim Van Verthdfc738b2018-11-19 17:15:27 +0000147 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
148 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500149 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon92be2f72018-06-19 14:33:47 -0400150
Brian Salomon7eae3e02018-08-07 14:02:38 +0000151 if (numActiveProxies) {
152 fAtlasSize = proxies[0]->isize();
153 }
Jim Van Verthcbeae032018-05-16 14:54:41 -0400154 for (int i = 0; i < numActiveProxies; ++i) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500155 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000156 SkASSERT(proxies[i]->isize() == fAtlasSize);
Brian Salomon67529b22019-08-13 15:31:04 -0400157 fTextureSamplers[i].reset(proxies[i]->textureType(), params, proxies[i]->textureSwizzle());
Jim Van Vertha950b632017-09-12 11:54:11 -0400158 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400159 this->setTextureSamplerCnt(numActiveProxies);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500160}
161
Robert Phillips4bc70112018-03-01 10:24:02 -0500162void GrBitmapTextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
Jim Van Verthcbeae032018-05-16 14:54:41 -0400163 int numActiveProxies,
Robert Phillips4bc70112018-03-01 10:24:02 -0500164 const GrSamplerState& params) {
Jim Van Verthcbeae032018-05-16 14:54:41 -0400165 SkASSERT(numActiveProxies <= kMaxTextures);
Robert Phillips4bc70112018-03-01 10:24:02 -0500166
Brian Salomon7eae3e02018-08-07 14:02:38 +0000167 if (!fTextureSamplers[0].isInitialized()) {
168 fAtlasSize = proxies[0]->isize();
169 }
170
Jim Van Verthcbeae032018-05-16 14:54:41 -0400171 for (int i = 0; i < numActiveProxies; ++i) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500172 SkASSERT(proxies[i]);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000173 SkASSERT(proxies[i]->isize() == fAtlasSize);
Robert Phillips4bc70112018-03-01 10:24:02 -0500174
175 if (!fTextureSamplers[i].isInitialized()) {
Brian Salomon67529b22019-08-13 15:31:04 -0400176 fTextureSamplers[i].reset(proxies[i]->textureType(), params,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400177 proxies[i]->textureSwizzle());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400178 }
179 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400180 this->setTextureSamplerCnt(numActiveProxies);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400181}
182
Brian Salomon94efbf52016-11-29 13:43:05 -0500183void GrBitmapTextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800184 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700185 GrGLBitmapTextGeoProc::GenKey(*this, caps, b);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000186}
187
Brian Salomon94efbf52016-11-29 13:43:05 -0500188GrGLSLPrimitiveProcessor* GrBitmapTextGeoProc::createGLSLInstance(const GrShaderCaps& caps) const {
joshualitt465283c2015-09-11 08:19:35 -0700189 return new GrGLBitmapTextGeoProc();
joshualitteb2a6762014-12-04 11:35:33 -0800190}
joshualitt9b989322014-12-15 14:16:27 -0800191
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000192///////////////////////////////////////////////////////////////////////////////
193
egdaniel309e3462014-12-09 10:35:58 -0800194GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000195
Hal Canary6f6961e2017-01-31 13:50:44 -0500196#if GR_TEST_UTILS
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400197
bungeman06ca8ec2016-06-09 08:01:03 -0700198sk_sp<GrGeometryProcessor> GrBitmapTextGeoProc::TestCreate(GrProcessorTestData* d) {
Brian Salomon514baff2016-11-17 15:17:07 -0500199 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
200 : GrProcessorUnitTest::kAlphaTextureIdx;
Jim Van Vertha950b632017-09-12 11:54:11 -0400201 sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
202 d->textureProxy(texIdx),
203 nullptr,
204 nullptr,
205 nullptr
206 };
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500207
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400208 GrSamplerState::WrapMode wrapModes[2];
209 GrTest::TestWrapModes(d->fRandom, wrapModes);
210 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
211 ? GrSamplerState::Filter::kBilerp
212 : GrSamplerState::Filter::kNearest);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000213
robertphillipsb63c5762016-04-08 05:24:21 -0700214 GrMaskFormat format = kARGB_GrMaskFormat; // init to avoid warning
joshualitt0067ff52015-07-08 14:26:19 -0700215 switch (d->fRandom->nextULessThan(3)) {
joshualitt02b05012015-02-11 06:56:30 -0800216 case 0:
217 format = kA8_GrMaskFormat;
218 break;
219 case 1:
220 format = kA565_GrMaskFormat;
221 break;
222 case 2:
223 format = kARGB_GrMaskFormat;
224 break;
225 }
226
Brian Osman1be2b7c2018-10-29 16:07:15 -0400227 return GrBitmapTextGeoProc::Make(*d->caps()->shaderCaps(),
Brian Osmancf860852018-10-31 14:04:39 -0400228 SkPMColor4f::FromBytes_RGBA(GrRandomColor(d->fRandom)),
Brian Osmanc906d252018-12-04 11:17:46 -0500229 d->fRandom->nextBool(),
Brian Osman1be2b7c2018-10-29 16:07:15 -0400230 proxies, 1, samplerState, format,
231 GrTest::TestMatrix(d->fRandom), d->fRandom->nextBool());
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000232}
Hal Canary6f6961e2017-01-31 13:50:44 -0500233#endif