blob: db9a69b313f5e3860291cefb26ea82a425702c4a [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 "src/gpu/GrCaps.h"
11#include "src/gpu/GrShaderCaps.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000012#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#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:
Michael Ludwig553db622020-06-19 10:47:30 -040023 GrGLBitmapTextGeoProc()
24 : fColor(SK_PMColor4fILLEGAL)
25 , fAtlasDimensions{0,0}
26 , fLocalMatrix(SkMatrix::InvalidMatrix()) {}
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000027
joshualitt465283c2015-09-11 08:19:35 -070028 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
Robert Phillips8296e752017-08-25 08:45:21 -040029 const GrBitmapTextGeoProc& btgp = args.fGP.cast<GrBitmapTextGeoProc>();
joshualitt2dd1ae02014-12-03 06:24:10 -080030
egdaniel4ca2e602015-11-18 08:01:26 -080031 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -080032 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -080033 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000034
joshualittabb52a12015-01-13 15:02:10 -080035 // emit attributes
Robert Phillips8296e752017-08-25 08:45:21 -040036 varyingHandler->emitAttributes(btgp);
joshualittabb52a12015-01-13 15:02:10 -080037
Brian Salomon2638f3d2019-10-22 12:20:37 -040038 const char* atlasDimensionsInvName;
Ethan Nicholas16464c32020-04-06 13:53:05 -040039 fAtlasDimensionsInvUniform = uniformHandler->addUniform(nullptr, kVertex_GrShaderFlag,
40 kFloat2_GrSLType, "AtlasSizeInv", &atlasDimensionsInvName);
joshualitt922c8b12015-08-07 09:55:23 -070041
Chris Dalton27372882017-12-08 13:34:21 -070042 GrGLSLVarying uv(kFloat2_GrSLType);
Jim Van Verth7edb0eb2020-01-15 21:56:46 +000043 GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
44 GrGLSLVarying texIdx(texIdxType);
Jim Van Verthfb395102020-02-03 10:11:19 -050045 append_index_uv_varyings(args, btgp.numTextureSamplers(), btgp.inTextureCoords().name(),
46 atlasDimensionsInvName, &uv, &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();
Michael Ludwig553db622020-06-19 10:47:30 -040059 this->writeLocalCoord(vertBuilder, uniformHandler, gpArgs, btgp.inPosition().asShaderVar(),
60 btgp.localMatrix(), &fLocalMatrixUniform);
joshualittabb52a12015-01-13 15:02:10 -080061
Ethan Nicholasf7b88202017-09-18 14:10:39 -040062 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -040063 append_multitexture_lookup(args, btgp.numTextureSamplers(),
64 texIdx, uv.fsIn(), "texColor");
65
Robert Phillips8296e752017-08-25 08:45:21 -040066 if (btgp.maskFormat() == kARGB_GrMaskFormat) {
Jim Van Verth6a7a7042017-09-11 11:04:10 -040067 // modulate by color
68 fragBuilder->codeAppendf("%s = %s * texColor;", args.fOutputColor, args.fOutputColor);
Ethan Nicholasf7b88202017-09-18 14:10:39 -040069 fragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
joshualitt02b05012015-02-11 06:56:30 -080070 } else {
Jim Van Verth6a7a7042017-09-11 11:04:10 -040071 fragBuilder->codeAppendf("%s = texColor;", args.fOutputCoverage);
joshualitt02b05012015-02-11 06:56:30 -080072 }
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000073 }
74
Brian Osman609f1592020-07-01 15:14:39 -040075 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp) override {
joshualittb8c241a2015-05-19 08:23:30 -070076 const GrBitmapTextGeoProc& btgp = gp.cast<GrBitmapTextGeoProc>();
77 if (btgp.color() != fColor && !btgp.hasVertexColor()) {
Brian Osmancf860852018-10-31 14:04:39 -040078 pdman.set4fv(fColorUniform, 1, btgp.color().vec());
joshualittb8c241a2015-05-19 08:23:30 -070079 fColor = btgp.color();
joshualitt9b989322014-12-15 14:16:27 -080080 }
Robert Phillips8296e752017-08-25 08:45:21 -040081
Brian Salomon2638f3d2019-10-22 12:20:37 -040082 const SkISize& atlasDimensions = btgp.atlasDimensions();
83 SkASSERT(SkIsPow2(atlasDimensions.fWidth) && SkIsPow2(atlasDimensions.fHeight));
Robert Phillips8296e752017-08-25 08:45:21 -040084
Brian Salomon2638f3d2019-10-22 12:20:37 -040085 if (fAtlasDimensions != atlasDimensions) {
86 pdman.set2f(fAtlasDimensionsInvUniform,
87 1.0f / atlasDimensions.fWidth,
88 1.0f / atlasDimensions.fHeight);
89 fAtlasDimensions = atlasDimensions;
Robert Phillips8296e752017-08-25 08:45:21 -040090 }
Michael Ludwig553db622020-06-19 10:47:30 -040091
92 this->setTransform(pdman, fLocalMatrixUniform, btgp.localMatrix(), &fLocalMatrix);
joshualitte3ababe2015-05-15 07:56:07 -070093 }
94
joshualitt9b989322014-12-15 14:16:27 -080095 static inline void GenKey(const GrGeometryProcessor& proc,
Brian Salomon94efbf52016-11-29 13:43:05 -050096 const GrShaderCaps&,
joshualitt9b989322014-12-15 14:16:27 -080097 GrProcessorKeyBuilder* b) {
Robert Phillips8296e752017-08-25 08:45:21 -040098 const GrBitmapTextGeoProc& btgp = proc.cast<GrBitmapTextGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -080099 uint32_t key = 0;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400100 key |= btgp.usesW() ? 0x1 : 0x0;
Robert Phillips8296e752017-08-25 08:45:21 -0400101 key |= btgp.maskFormat() << 1;
Michael Ludwig553db622020-06-19 10:47:30 -0400102 key |= ComputeMatrixKey(btgp.localMatrix()) << 2;
joshualittb8c241a2015-05-19 08:23:30 -0700103 b->add32(key);
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400104 b->add32(btgp.numTextureSamplers());
joshualitt9b989322014-12-15 14:16:27 -0800105 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800106
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000107private:
Brian Osmancf860852018-10-31 14:04:39 -0400108 SkPMColor4f fColor;
joshualitt9b989322014-12-15 14:16:27 -0800109 UniformHandle fColorUniform;
110
Brian Salomon2638f3d2019-10-22 12:20:37 -0400111 SkISize fAtlasDimensions;
112 UniformHandle fAtlasDimensionsInvUniform;
Robert Phillips8296e752017-08-25 08:45:21 -0400113
Michael Ludwig553db622020-06-19 10:47:30 -0400114 SkMatrix fLocalMatrix;
115 UniformHandle fLocalMatrixUniform;
116
egdaniele659a582015-11-13 09:55:43 -0800117 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000118};
119
120///////////////////////////////////////////////////////////////////////////////
121
Brian Osman4a3f5c82018-09-18 16:16:38 -0400122GrBitmapTextGeoProc::GrBitmapTextGeoProc(const GrShaderCaps& caps,
Brian Osmancf860852018-10-31 14:04:39 -0400123 const SkPMColor4f& color,
Brian Osmanc906d252018-12-04 11:17:46 -0500124 bool wideColor,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500125 const GrSurfaceProxyView* views,
126 int numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500127 GrSamplerState params,
128 GrMaskFormat format,
129 const SkMatrix& localMatrix,
130 bool usesW)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400131 : INHERITED(kGrBitmapTextGeoProc_ClassID)
132 , fColor(color)
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400133 , fLocalMatrix(localMatrix)
Jim Van Verthb515ae72018-05-23 16:44:55 -0400134 , fUsesW(usesW)
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400135 , fMaskFormat(format) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500136 SkASSERT(numActiveViews <= kMaxTextures);
Robert Phillips4bc70112018-03-01 10:24:02 -0500137
Jim Van Verthb515ae72018-05-23 16:44:55 -0400138 if (usesW) {
Brian Osmand4c29702018-09-14 16:16:55 -0400139 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Jim Van Verthb515ae72018-05-23 16:44:55 -0400140 } else {
Brian Osmand4c29702018-09-14 16:16:55 -0400141 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Jim Van Verthb515ae72018-05-23 16:44:55 -0400142 }
Brian Osman4a3f5c82018-09-18 16:16:38 -0400143
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500144 bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat ||
145 kA565_GrMaskFormat == fMaskFormat;
146 if (hasVertexColor) {
Brian Osmanc906d252018-12-04 11:17:46 -0500147 fInColor = MakeColorAttribute("inColor", wideColor);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500148 }
Robert Phillips8296e752017-08-25 08:45:21 -0400149
Jim Van Verth2f2c77a2020-01-24 15:48:23 +0000150 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
151 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500152 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon92be2f72018-06-19 14:33:47 -0400153
Greg Daniel9715b6c2019-12-10 15:03:10 -0500154 if (numActiveViews) {
155 fAtlasDimensions = views[0].proxy()->dimensions();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000156 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500157 for (int i = 0; i < numActiveViews; ++i) {
158 const GrSurfaceProxy* proxy = views[i].proxy();
159 SkASSERT(proxy);
160 SkASSERT(proxy->dimensions() == fAtlasDimensions);
161 fTextureSamplers[i].reset(params, proxy->backendFormat(), views[i].swizzle());
Jim Van Vertha950b632017-09-12 11:54:11 -0400162 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500163 this->setTextureSamplerCnt(numActiveViews);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500164}
165
Greg Daniel9715b6c2019-12-10 15:03:10 -0500166void GrBitmapTextGeoProc::addNewViews(const GrSurfaceProxyView* views,
167 int numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500168 GrSamplerState params) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500169 SkASSERT(numActiveViews <= kMaxTextures);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500170 // Just to make sure we don't try to add too many proxies
Brian Osman788b9162020-02-07 10:36:46 -0500171 numActiveViews = std::min(numActiveViews, kMaxTextures);
Robert Phillips4bc70112018-03-01 10:24:02 -0500172
Brian Salomon7eae3e02018-08-07 14:02:38 +0000173 if (!fTextureSamplers[0].isInitialized()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500174 fAtlasDimensions = views[0].proxy()->dimensions();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000175 }
176
Greg Daniel9715b6c2019-12-10 15:03:10 -0500177 for (int i = 0; i < numActiveViews; ++i) {
178 const GrSurfaceProxy* proxy = views[i].proxy();
179 SkASSERT(proxy);
180 SkASSERT(proxy->dimensions() == fAtlasDimensions);
Robert Phillips4bc70112018-03-01 10:24:02 -0500181
182 if (!fTextureSamplers[i].isInitialized()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500183 fTextureSamplers[i].reset(params, proxy->backendFormat(), views[i].swizzle());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400184 }
185 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500186 this->setTextureSamplerCnt(numActiveViews);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400187}
188
Brian Salomon94efbf52016-11-29 13:43:05 -0500189void GrBitmapTextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800190 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700191 GrGLBitmapTextGeoProc::GenKey(*this, caps, b);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000192}
193
Brian Salomon94efbf52016-11-29 13:43:05 -0500194GrGLSLPrimitiveProcessor* GrBitmapTextGeoProc::createGLSLInstance(const GrShaderCaps& caps) const {
joshualitt465283c2015-09-11 08:19:35 -0700195 return new GrGLBitmapTextGeoProc();
joshualitteb2a6762014-12-04 11:35:33 -0800196}
joshualitt9b989322014-12-15 14:16:27 -0800197
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000198///////////////////////////////////////////////////////////////////////////////
199
egdaniel309e3462014-12-09 10:35:58 -0800200GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000201
Hal Canary6f6961e2017-01-31 13:50:44 -0500202#if GR_TEST_UTILS
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400203
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500204GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(GrProcessorTestData* d) {
Greg Daniel026a60c2020-02-12 10:53:51 -0500205 auto [view, ct, at] = d->randomView();
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500206
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400207 GrSamplerState::WrapMode wrapModes[2];
208 GrTest::TestWrapModes(d->fRandom, wrapModes);
209 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
Brian Salomona3b02f52020-07-15 16:02:01 -0400210 ? GrSamplerState::Filter::kLinear
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400211 : GrSamplerState::Filter::kNearest);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000212
Brian Salomon766098d2019-12-18 10:41:58 -0500213 GrMaskFormat format;
214 switch (ct) {
215 case GrColorType::kAlpha_8:
joshualitt02b05012015-02-11 06:56:30 -0800216 format = kA8_GrMaskFormat;
217 break;
Brian Salomon766098d2019-12-18 10:41:58 -0500218 case GrColorType::kBGR_565:
joshualitt02b05012015-02-11 06:56:30 -0800219 format = kA565_GrMaskFormat;
220 break;
Brian Salomon766098d2019-12-18 10:41:58 -0500221 case GrColorType::kRGBA_8888:
222 default: // It doesn't really matter that color type and mask format agree.
joshualitt02b05012015-02-11 06:56:30 -0800223 format = kARGB_GrMaskFormat;
224 break;
225 }
226
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