blob: cfb205d92c27f5e71cd053c98df912847f660042 [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 Salomon2638f3d2019-10-22 12:20:37 -040023 GrGLBitmapTextGeoProc() : fColor(SK_PMColor4fILLEGAL), fAtlasDimensions{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
Brian Salomon2638f3d2019-10-22 12:20:37 -040035 const char* atlasDimensionsInvName;
36 fAtlasDimensionsInvUniform = uniformHandler->addUniform(
37 kVertex_GrShaderFlag, kFloat2_GrSLType, "AtlasSizeInv", &atlasDimensionsInvName);
joshualitt922c8b12015-08-07 09:55:23 -070038
Chris Dalton27372882017-12-08 13:34:21 -070039 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verth1694a862018-12-17 19:48:42 +000040 GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
41 GrGLSLVarying texIdx(texIdxType);
Brian Salomon2638f3d2019-10-22 12:20:37 -040042 append_index_uv_varyings(args, btgp.inTextureCoords().name(), atlasDimensionsInvName, &uv,
Brian Salomon70132d02018-05-29 15:33:06 -040043 &texIdx, nullptr);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000044
Chris Dalton60283612018-02-14 13:38:14 -070045 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt9b989322014-12-15 14:16:27 -080046 // Setup pass through color
Robert Phillips8296e752017-08-25 08:45:21 -040047 if (btgp.hasVertexColor()) {
48 varyingHandler->addPassThroughAttribute(btgp.inColor(), args.fOutputColor);
Brian Salomonbfd51832017-01-04 13:22:08 -050049 } else {
50 this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor,
51 &fColorUniform);
joshualittb8c241a2015-05-19 08:23:30 -070052 }
joshualitt2dd1ae02014-12-03 06:24:10 -080053
joshualittabb52a12015-01-13 15:02:10 -080054 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -040055 gpArgs->fPositionVar = btgp.inPosition().asShaderVar();
joshualitt4973d9d2014-11-08 09:24:25 -080056
joshualittabb52a12015-01-13 15:02:10 -080057 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -080058 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -080059 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -080060 uniformHandler,
Brian Salomon92be2f72018-06-19 14:33:47 -040061 btgp.inPosition().asShaderVar(),
Robert Phillips8296e752017-08-25 08:45:21 -040062 btgp.localMatrix(),
bsalomona624bf32016-09-20 09:12:47 -070063 args.fFPCoordTransformHandler);
joshualittabb52a12015-01-13 15:02:10 -080064
Ethan Nicholasf7b88202017-09-18 14:10:39 -040065 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -040066 append_multitexture_lookup(args, btgp.numTextureSamplers(),
67 texIdx, uv.fsIn(), "texColor");
68
Robert Phillips8296e752017-08-25 08:45:21 -040069 if (btgp.maskFormat() == kARGB_GrMaskFormat) {
Jim Van Verth6a7a7042017-09-11 11:04:10 -040070 // modulate by color
71 fragBuilder->codeAppendf("%s = %s * texColor;", args.fOutputColor, args.fOutputColor);
Ethan Nicholasf7b88202017-09-18 14:10:39 -040072 fragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
joshualitt02b05012015-02-11 06:56:30 -080073 } else {
Jim Van Verth6a7a7042017-09-11 11:04:10 -040074 fragBuilder->codeAppendf("%s = texColor;", args.fOutputCoverage);
joshualitt02b05012015-02-11 06:56:30 -080075 }
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000076 }
77
bsalomona624bf32016-09-20 09:12:47 -070078 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp,
Brian Salomonc241b582019-11-27 08:57:17 -050079 const CoordTransformRange& transformRange) override {
joshualittb8c241a2015-05-19 08:23:30 -070080 const GrBitmapTextGeoProc& btgp = gp.cast<GrBitmapTextGeoProc>();
81 if (btgp.color() != fColor && !btgp.hasVertexColor()) {
Brian Osmancf860852018-10-31 14:04:39 -040082 pdman.set4fv(fColorUniform, 1, btgp.color().vec());
joshualittb8c241a2015-05-19 08:23:30 -070083 fColor = btgp.color();
joshualitt9b989322014-12-15 14:16:27 -080084 }
Robert Phillips8296e752017-08-25 08:45:21 -040085
Brian Salomon2638f3d2019-10-22 12:20:37 -040086 const SkISize& atlasDimensions = btgp.atlasDimensions();
87 SkASSERT(SkIsPow2(atlasDimensions.fWidth) && SkIsPow2(atlasDimensions.fHeight));
Robert Phillips8296e752017-08-25 08:45:21 -040088
Brian Salomon2638f3d2019-10-22 12:20:37 -040089 if (fAtlasDimensions != atlasDimensions) {
90 pdman.set2f(fAtlasDimensionsInvUniform,
91 1.0f / atlasDimensions.fWidth,
92 1.0f / atlasDimensions.fHeight);
93 fAtlasDimensions = atlasDimensions;
Robert Phillips8296e752017-08-25 08:45:21 -040094 }
Brian Salomonc241b582019-11-27 08:57:17 -050095 this->setTransformDataHelper(btgp.localMatrix(), pdman, transformRange);
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
Brian Salomon2638f3d2019-10-22 12:20:37 -0400113 SkISize fAtlasDimensions;
114 UniformHandle fAtlasDimensionsInvUniform;
Robert Phillips8296e752017-08-25 08:45:21 -0400115
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,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500124 const GrSurfaceProxyView* views,
125 int numActiveViews,
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) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500133 SkASSERT(numActiveViews <= 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
Greg Daniel9715b6c2019-12-10 15:03:10 -0500151 if (numActiveViews) {
152 fAtlasDimensions = views[0].proxy()->dimensions();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000153 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500154 for (int i = 0; i < numActiveViews; ++i) {
155 const GrSurfaceProxy* proxy = views[i].proxy();
156 SkASSERT(proxy);
157 SkASSERT(proxy->dimensions() == fAtlasDimensions);
158 fTextureSamplers[i].reset(params, proxy->backendFormat(), views[i].swizzle());
Jim Van Vertha950b632017-09-12 11:54:11 -0400159 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500160 this->setTextureSamplerCnt(numActiveViews);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500161}
162
Greg Daniel9715b6c2019-12-10 15:03:10 -0500163void GrBitmapTextGeoProc::addNewViews(const GrSurfaceProxyView* views,
164 int numActiveViews,
165 const GrSamplerState& params) {
166 SkASSERT(numActiveViews <= kMaxTextures);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500167 // Just to make sure we don't try to add too many proxies
Greg Daniel9715b6c2019-12-10 15:03:10 -0500168 numActiveViews = SkTMin(numActiveViews, kMaxTextures);
Robert Phillips4bc70112018-03-01 10:24:02 -0500169
Brian Salomon7eae3e02018-08-07 14:02:38 +0000170 if (!fTextureSamplers[0].isInitialized()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500171 fAtlasDimensions = views[0].proxy()->dimensions();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000172 }
173
Greg Daniel9715b6c2019-12-10 15:03:10 -0500174 for (int i = 0; i < numActiveViews; ++i) {
175 const GrSurfaceProxy* proxy = views[i].proxy();
176 SkASSERT(proxy);
177 SkASSERT(proxy->dimensions() == fAtlasDimensions);
Robert Phillips4bc70112018-03-01 10:24:02 -0500178
179 if (!fTextureSamplers[i].isInitialized()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500180 fTextureSamplers[i].reset(params, proxy->backendFormat(), views[i].swizzle());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400181 }
182 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500183 this->setTextureSamplerCnt(numActiveViews);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400184}
185
Brian Salomon94efbf52016-11-29 13:43:05 -0500186void GrBitmapTextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800187 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700188 GrGLBitmapTextGeoProc::GenKey(*this, caps, b);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000189}
190
Brian Salomon94efbf52016-11-29 13:43:05 -0500191GrGLSLPrimitiveProcessor* GrBitmapTextGeoProc::createGLSLInstance(const GrShaderCaps& caps) const {
joshualitt465283c2015-09-11 08:19:35 -0700192 return new GrGLBitmapTextGeoProc();
joshualitteb2a6762014-12-04 11:35:33 -0800193}
joshualitt9b989322014-12-15 14:16:27 -0800194
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000195///////////////////////////////////////////////////////////////////////////////
196
egdaniel309e3462014-12-09 10:35:58 -0800197GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000198
Hal Canary6f6961e2017-01-31 13:50:44 -0500199#if GR_TEST_UTILS
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400200
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500201GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(GrProcessorTestData* d) {
Brian Salomon766098d2019-12-18 10:41:58 -0500202 auto [proxy, ct, at] = d->randomProxy();
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500203
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400204 GrSamplerState::WrapMode wrapModes[2];
205 GrTest::TestWrapModes(d->fRandom, wrapModes);
206 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
207 ? GrSamplerState::Filter::kBilerp
208 : GrSamplerState::Filter::kNearest);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000209
Brian Salomon766098d2019-12-18 10:41:58 -0500210 GrMaskFormat format;
211 switch (ct) {
212 case GrColorType::kAlpha_8:
joshualitt02b05012015-02-11 06:56:30 -0800213 format = kA8_GrMaskFormat;
214 break;
Brian Salomon766098d2019-12-18 10:41:58 -0500215 case GrColorType::kBGR_565:
joshualitt02b05012015-02-11 06:56:30 -0800216 format = kA565_GrMaskFormat;
217 break;
Brian Salomon766098d2019-12-18 10:41:58 -0500218 case GrColorType::kRGBA_8888:
219 default: // It doesn't really matter that color type and mask format agree.
joshualitt02b05012015-02-11 06:56:30 -0800220 format = kARGB_GrMaskFormat;
221 break;
222 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500223 GrSurfaceOrigin origin = proxy->origin();
224 const GrSwizzle& swizzle = proxy->textureSwizzle();
225 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
joshualitt02b05012015-02-11 06:56:30 -0800226
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500227 return GrBitmapTextGeoProc::Make(d->allocator(), *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(),
Greg Daniel9715b6c2019-12-10 15:03:10 -0500230 &view, 1, samplerState, format,
Brian Osman1be2b7c2018-10-29 16:07:15 -0400231 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