blob: a58a2b208968256f1c0cb630482bd328652c2766 [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
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
Robert Phillips8296e752017-08-25 08:45:21 -040048 if (btgp.hasVertexColor()) {
49 varyingHandler->addPassThroughAttribute(btgp.inColor(), args.fOutputColor);
Brian Salomonbfd51832017-01-04 13:22:08 -050050 } else {
51 this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor,
52 &fColorUniform);
joshualittb8c241a2015-05-19 08:23:30 -070053 }
joshualitt2dd1ae02014-12-03 06:24:10 -080054
joshualittabb52a12015-01-13 15:02:10 -080055 // Setup position
Brian Salomon92be2f72018-06-19 14:33:47 -040056 gpArgs->fPositionVar = btgp.inPosition().asShaderVar();
Michael Ludwig553db622020-06-19 10:47:30 -040057 this->writeLocalCoord(vertBuilder, uniformHandler, gpArgs, btgp.inPosition().asShaderVar(),
58 btgp.localMatrix(), &fLocalMatrixUniform);
joshualittabb52a12015-01-13 15:02:10 -080059
Ethan Nicholasf7b88202017-09-18 14:10:39 -040060 fragBuilder->codeAppend("half4 texColor;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -040061 append_multitexture_lookup(args, btgp.numTextureSamplers(),
62 texIdx, uv.fsIn(), "texColor");
63
Robert Phillips8296e752017-08-25 08:45:21 -040064 if (btgp.maskFormat() == kARGB_GrMaskFormat) {
Jim Van Verth6a7a7042017-09-11 11:04:10 -040065 // modulate by color
66 fragBuilder->codeAppendf("%s = %s * texColor;", args.fOutputColor, args.fOutputColor);
Ethan Nicholasf7b88202017-09-18 14:10:39 -040067 fragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
joshualitt02b05012015-02-11 06:56:30 -080068 } else {
Jim Van Verth6a7a7042017-09-11 11:04:10 -040069 fragBuilder->codeAppendf("%s = texColor;", args.fOutputCoverage);
joshualitt02b05012015-02-11 06:56:30 -080070 }
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000071 }
72
Brian Osman609f1592020-07-01 15:14:39 -040073 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp) override {
joshualittb8c241a2015-05-19 08:23:30 -070074 const GrBitmapTextGeoProc& btgp = gp.cast<GrBitmapTextGeoProc>();
75 if (btgp.color() != fColor && !btgp.hasVertexColor()) {
Brian Osmancf860852018-10-31 14:04:39 -040076 pdman.set4fv(fColorUniform, 1, btgp.color().vec());
joshualittb8c241a2015-05-19 08:23:30 -070077 fColor = btgp.color();
joshualitt9b989322014-12-15 14:16:27 -080078 }
Robert Phillips8296e752017-08-25 08:45:21 -040079
Brian Salomon2638f3d2019-10-22 12:20:37 -040080 const SkISize& atlasDimensions = btgp.atlasDimensions();
81 SkASSERT(SkIsPow2(atlasDimensions.fWidth) && SkIsPow2(atlasDimensions.fHeight));
Robert Phillips8296e752017-08-25 08:45:21 -040082
Brian Salomon2638f3d2019-10-22 12:20:37 -040083 if (fAtlasDimensions != atlasDimensions) {
84 pdman.set2f(fAtlasDimensionsInvUniform,
85 1.0f / atlasDimensions.fWidth,
86 1.0f / atlasDimensions.fHeight);
87 fAtlasDimensions = atlasDimensions;
Robert Phillips8296e752017-08-25 08:45:21 -040088 }
Michael Ludwig553db622020-06-19 10:47:30 -040089
90 this->setTransform(pdman, fLocalMatrixUniform, btgp.localMatrix(), &fLocalMatrix);
joshualitte3ababe2015-05-15 07:56:07 -070091 }
92
joshualitt9b989322014-12-15 14:16:27 -080093 static inline void GenKey(const GrGeometryProcessor& proc,
Brian Salomon94efbf52016-11-29 13:43:05 -050094 const GrShaderCaps&,
joshualitt9b989322014-12-15 14:16:27 -080095 GrProcessorKeyBuilder* b) {
Robert Phillips8296e752017-08-25 08:45:21 -040096 const GrBitmapTextGeoProc& btgp = proc.cast<GrBitmapTextGeoProc>();
Brian Osman48d7f7c2021-03-03 13:38:08 -050097 b->addBool(btgp.usesW(), "usesW");
98 static_assert(kLast_GrMaskFormat < (1u << 2));
99 b->addBits(2, btgp.maskFormat(), "maskFormat");
100 b->addBits(kMatrixKeyBits, ComputeMatrixKey(btgp.localMatrix()), "localMatrixType");
101 b->add32(btgp.numTextureSamplers(),"numTextures");
joshualitt9b989322014-12-15 14:16:27 -0800102 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800103
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000104private:
Brian Osmancf860852018-10-31 14:04:39 -0400105 SkPMColor4f fColor;
joshualitt9b989322014-12-15 14:16:27 -0800106 UniformHandle fColorUniform;
107
Brian Salomon2638f3d2019-10-22 12:20:37 -0400108 SkISize fAtlasDimensions;
109 UniformHandle fAtlasDimensionsInvUniform;
Robert Phillips8296e752017-08-25 08:45:21 -0400110
Michael Ludwig553db622020-06-19 10:47:30 -0400111 SkMatrix fLocalMatrix;
112 UniformHandle fLocalMatrixUniform;
113
John Stiles7571f9e2020-09-02 22:42:33 -0400114 using INHERITED = GrGLSLGeometryProcessor;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000115};
116
117///////////////////////////////////////////////////////////////////////////////
118
Brian Osman4a3f5c82018-09-18 16:16:38 -0400119GrBitmapTextGeoProc::GrBitmapTextGeoProc(const GrShaderCaps& caps,
Brian Osmancf860852018-10-31 14:04:39 -0400120 const SkPMColor4f& color,
Brian Osmanc906d252018-12-04 11:17:46 -0500121 bool wideColor,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500122 const GrSurfaceProxyView* views,
123 int numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500124 GrSamplerState params,
125 GrMaskFormat format,
126 const SkMatrix& localMatrix,
127 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 Verth2f2c77a2020-01-24 15:48:23 +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,
Brian Salomonccb61422020-01-09 10:46:36 -0500165 GrSamplerState params) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500166 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
Brian Osman788b9162020-02-07 10:36:46 -0500168 numActiveViews = std::min(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) {
Greg Daniel026a60c2020-02-12 10:53:51 -0500202 auto [view, ct, at] = d->randomView();
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()
Brian Salomona3b02f52020-07-15 16:02:01 -0400207 ? GrSamplerState::Filter::kLinear
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400208 : 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 }
223
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500224 return GrBitmapTextGeoProc::Make(d->allocator(), *d->caps()->shaderCaps(),
Brian Osmancf860852018-10-31 14:04:39 -0400225 SkPMColor4f::FromBytes_RGBA(GrRandomColor(d->fRandom)),
Brian Osmanc906d252018-12-04 11:17:46 -0500226 d->fRandom->nextBool(),
Greg Daniel9715b6c2019-12-10 15:03:10 -0500227 &view, 1, samplerState, format,
Brian Osman1be2b7c2018-10-29 16:07:15 -0400228 GrTest::TestMatrix(d->fRandom), d->fRandom->nextBool());
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000229}
Hal Canary6f6961e2017-01-31 13:50:44 -0500230#endif