blob: 64fa7614f725c5de8c240a058f0d543d18c11941 [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 Phillips787fd9d2021-03-22 14:48:09 -040029 const GrBitmapTextGeoProc& btgp = args.fGeomProc.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
Brian Salomonde3d4412020-09-10 16:00:16 -040042 GrGLSLVarying uv, texIdx;
Jim Van Verthfb395102020-02-03 10:11:19 -050043 append_index_uv_varyings(args, btgp.numTextureSamplers(), btgp.inTextureCoords().name(),
44 atlasDimensionsInvName, &uv, &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
John Stiles4d7ac492021-03-09 20:16:43 -050048 fragBuilder->codeAppendf("half4 %s;", args.fOutputColor);
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();
Michael Ludwig553db622020-06-19 10:47:30 -040058 this->writeLocalCoord(vertBuilder, uniformHandler, gpArgs, btgp.inPosition().asShaderVar(),
59 btgp.localMatrix(), &fLocalMatrixUniform);
joshualittabb52a12015-01-13 15:02:10 -080060
Ethan Nicholasf7b88202017-09-18 14:10:39 -040061 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -040062 append_multitexture_lookup(args, btgp.numTextureSamplers(),
63 texIdx, uv.fsIn(), "texColor");
64
Robert Phillips8296e752017-08-25 08:45:21 -040065 if (btgp.maskFormat() == kARGB_GrMaskFormat) {
Jim Van Verth6a7a7042017-09-11 11:04:10 -040066 // modulate by color
67 fragBuilder->codeAppendf("%s = %s * texColor;", args.fOutputColor, args.fOutputColor);
John Stiles4d7ac492021-03-09 20:16:43 -050068 fragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputCoverage);
joshualitt02b05012015-02-11 06:56:30 -080069 } else {
John Stiles4d7ac492021-03-09 20:16:43 -050070 fragBuilder->codeAppendf("half4 %s = texColor;", args.fOutputCoverage);
joshualitt02b05012015-02-11 06:56:30 -080071 }
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000072 }
73
Robert Phillips787fd9d2021-03-22 14:48:09 -040074 void setData(const GrGLSLProgramDataManager& pdman,
75 const GrGeometryProcessor& geomProc) override {
76 const GrBitmapTextGeoProc& btgp = geomProc.cast<GrBitmapTextGeoProc>();
joshualittb8c241a2015-05-19 08:23:30 -070077 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>();
Brian Osman48d7f7c2021-03-03 13:38:08 -050099 b->addBool(btgp.usesW(), "usesW");
100 static_assert(kLast_GrMaskFormat < (1u << 2));
101 b->addBits(2, btgp.maskFormat(), "maskFormat");
102 b->addBits(kMatrixKeyBits, ComputeMatrixKey(btgp.localMatrix()), "localMatrixType");
103 b->add32(btgp.numTextureSamplers(),"numTextures");
joshualitt9b989322014-12-15 14:16:27 -0800104 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800105
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000106private:
Brian Osmancf860852018-10-31 14:04:39 -0400107 SkPMColor4f fColor;
joshualitt9b989322014-12-15 14:16:27 -0800108 UniformHandle fColorUniform;
109
Brian Salomon2638f3d2019-10-22 12:20:37 -0400110 SkISize fAtlasDimensions;
111 UniformHandle fAtlasDimensionsInvUniform;
Robert Phillips8296e752017-08-25 08:45:21 -0400112
Michael Ludwig553db622020-06-19 10:47:30 -0400113 SkMatrix fLocalMatrix;
114 UniformHandle fLocalMatrixUniform;
115
John Stiles7571f9e2020-09-02 22:42:33 -0400116 using INHERITED = GrGLSLGeometryProcessor;
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 Salomonccb61422020-01-09 10:46:36 -0500126 GrSamplerState params,
127 GrMaskFormat format,
128 const SkMatrix& localMatrix,
129 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) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500135 SkASSERT(numActiveViews <= 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
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500143 bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat ||
144 kA565_GrMaskFormat == fMaskFormat;
145 if (hasVertexColor) {
Brian Osmanc906d252018-12-04 11:17:46 -0500146 fInColor = MakeColorAttribute("inColor", wideColor);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500147 }
Robert Phillips8296e752017-08-25 08:45:21 -0400148
Jim Van Verth2f2c77a2020-01-24 15:48:23 +0000149 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
150 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500151 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon92be2f72018-06-19 14:33:47 -0400152
Greg Daniel9715b6c2019-12-10 15:03:10 -0500153 if (numActiveViews) {
154 fAtlasDimensions = views[0].proxy()->dimensions();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000155 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500156 for (int i = 0; i < numActiveViews; ++i) {
157 const GrSurfaceProxy* proxy = views[i].proxy();
158 SkASSERT(proxy);
159 SkASSERT(proxy->dimensions() == fAtlasDimensions);
160 fTextureSamplers[i].reset(params, proxy->backendFormat(), views[i].swizzle());
Jim Van Vertha950b632017-09-12 11:54:11 -0400161 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500162 this->setTextureSamplerCnt(numActiveViews);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500163}
164
Greg Daniel9715b6c2019-12-10 15:03:10 -0500165void GrBitmapTextGeoProc::addNewViews(const GrSurfaceProxyView* views,
166 int numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500167 GrSamplerState params) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500168 SkASSERT(numActiveViews <= kMaxTextures);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500169 // Just to make sure we don't try to add too many proxies
Brian Osman788b9162020-02-07 10:36:46 -0500170 numActiveViews = std::min(numActiveViews, kMaxTextures);
Robert Phillips4bc70112018-03-01 10:24:02 -0500171
Brian Salomon7eae3e02018-08-07 14:02:38 +0000172 if (!fTextureSamplers[0].isInitialized()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500173 fAtlasDimensions = views[0].proxy()->dimensions();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000174 }
175
Greg Daniel9715b6c2019-12-10 15:03:10 -0500176 for (int i = 0; i < numActiveViews; ++i) {
177 const GrSurfaceProxy* proxy = views[i].proxy();
178 SkASSERT(proxy);
179 SkASSERT(proxy->dimensions() == fAtlasDimensions);
Robert Phillips4bc70112018-03-01 10:24:02 -0500180
181 if (!fTextureSamplers[i].isInitialized()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500182 fTextureSamplers[i].reset(params, proxy->backendFormat(), views[i].swizzle());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400183 }
184 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500185 this->setTextureSamplerCnt(numActiveViews);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400186}
187
Brian Salomon94efbf52016-11-29 13:43:05 -0500188void GrBitmapTextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800189 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700190 GrGLBitmapTextGeoProc::GenKey(*this, caps, b);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000191}
192
Brian Salomon94efbf52016-11-29 13:43:05 -0500193GrGLSLPrimitiveProcessor* GrBitmapTextGeoProc::createGLSLInstance(const GrShaderCaps& caps) const {
joshualitt465283c2015-09-11 08:19:35 -0700194 return new GrGLBitmapTextGeoProc();
joshualitteb2a6762014-12-04 11:35:33 -0800195}
joshualitt9b989322014-12-15 14:16:27 -0800196
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000197///////////////////////////////////////////////////////////////////////////////
198
egdaniel309e3462014-12-09 10:35:58 -0800199GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000200
Hal Canary6f6961e2017-01-31 13:50:44 -0500201#if GR_TEST_UTILS
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400202
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500203GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(GrProcessorTestData* d) {
Greg Daniel026a60c2020-02-12 10:53:51 -0500204 auto [view, ct, at] = d->randomView();
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500205
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400206 GrSamplerState::WrapMode wrapModes[2];
207 GrTest::TestWrapModes(d->fRandom, wrapModes);
208 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
Brian Salomona3b02f52020-07-15 16:02:01 -0400209 ? GrSamplerState::Filter::kLinear
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400210 : GrSamplerState::Filter::kNearest);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000211
Brian Salomon766098d2019-12-18 10:41:58 -0500212 GrMaskFormat format;
213 switch (ct) {
214 case GrColorType::kAlpha_8:
joshualitt02b05012015-02-11 06:56:30 -0800215 format = kA8_GrMaskFormat;
216 break;
Brian Salomon766098d2019-12-18 10:41:58 -0500217 case GrColorType::kBGR_565:
joshualitt02b05012015-02-11 06:56:30 -0800218 format = kA565_GrMaskFormat;
219 break;
Brian Salomon766098d2019-12-18 10:41:58 -0500220 case GrColorType::kRGBA_8888:
221 default: // It doesn't really matter that color type and mask format agree.
joshualitt02b05012015-02-11 06:56:30 -0800222 format = kARGB_GrMaskFormat;
223 break;
224 }
225
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500226 return GrBitmapTextGeoProc::Make(d->allocator(), *d->caps()->shaderCaps(),
Brian Osmancf860852018-10-31 14:04:39 -0400227 SkPMColor4f::FromBytes_RGBA(GrRandomColor(d->fRandom)),
Brian Osmanc906d252018-12-04 11:17:46 -0500228 d->fRandom->nextBool(),
Greg Daniel9715b6c2019-12-10 15:03:10 -0500229 &view, 1, samplerState, format,
Brian Osman1be2b7c2018-10-29 16:07:15 -0400230 GrTest::TestMatrix(d->fRandom), d->fRandom->nextBool());
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000231}
Hal Canary6f6961e2017-01-31 13:50:44 -0500232#endif