blob: a852c93e60df5bfa59849d87b86b2b1887faeed9 [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
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
Brian Osman609f1592020-07-01 15:14:39 -040074 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp) override {
joshualittb8c241a2015-05-19 08:23:30 -070075 const GrBitmapTextGeoProc& btgp = gp.cast<GrBitmapTextGeoProc>();
76 if (btgp.color() != fColor && !btgp.hasVertexColor()) {
Brian Osmancf860852018-10-31 14:04:39 -040077 pdman.set4fv(fColorUniform, 1, btgp.color().vec());
joshualittb8c241a2015-05-19 08:23:30 -070078 fColor = btgp.color();
joshualitt9b989322014-12-15 14:16:27 -080079 }
Robert Phillips8296e752017-08-25 08:45:21 -040080
Brian Salomon2638f3d2019-10-22 12:20:37 -040081 const SkISize& atlasDimensions = btgp.atlasDimensions();
82 SkASSERT(SkIsPow2(atlasDimensions.fWidth) && SkIsPow2(atlasDimensions.fHeight));
Robert Phillips8296e752017-08-25 08:45:21 -040083
Brian Salomon2638f3d2019-10-22 12:20:37 -040084 if (fAtlasDimensions != atlasDimensions) {
85 pdman.set2f(fAtlasDimensionsInvUniform,
86 1.0f / atlasDimensions.fWidth,
87 1.0f / atlasDimensions.fHeight);
88 fAtlasDimensions = atlasDimensions;
Robert Phillips8296e752017-08-25 08:45:21 -040089 }
Michael Ludwig553db622020-06-19 10:47:30 -040090
91 this->setTransform(pdman, fLocalMatrixUniform, btgp.localMatrix(), &fLocalMatrix);
joshualitte3ababe2015-05-15 07:56:07 -070092 }
93
joshualitt9b989322014-12-15 14:16:27 -080094 static inline void GenKey(const GrGeometryProcessor& proc,
Brian Salomon94efbf52016-11-29 13:43:05 -050095 const GrShaderCaps&,
joshualitt9b989322014-12-15 14:16:27 -080096 GrProcessorKeyBuilder* b) {
Robert Phillips8296e752017-08-25 08:45:21 -040097 const GrBitmapTextGeoProc& btgp = proc.cast<GrBitmapTextGeoProc>();
Brian Osman48d7f7c2021-03-03 13:38:08 -050098 b->addBool(btgp.usesW(), "usesW");
99 static_assert(kLast_GrMaskFormat < (1u << 2));
100 b->addBits(2, btgp.maskFormat(), "maskFormat");
101 b->addBits(kMatrixKeyBits, ComputeMatrixKey(btgp.localMatrix()), "localMatrixType");
102 b->add32(btgp.numTextureSamplers(),"numTextures");
joshualitt9b989322014-12-15 14:16:27 -0800103 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800104
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000105private:
Brian Osmancf860852018-10-31 14:04:39 -0400106 SkPMColor4f fColor;
joshualitt9b989322014-12-15 14:16:27 -0800107 UniformHandle fColorUniform;
108
Brian Salomon2638f3d2019-10-22 12:20:37 -0400109 SkISize fAtlasDimensions;
110 UniformHandle fAtlasDimensionsInvUniform;
Robert Phillips8296e752017-08-25 08:45:21 -0400111
Michael Ludwig553db622020-06-19 10:47:30 -0400112 SkMatrix fLocalMatrix;
113 UniformHandle fLocalMatrixUniform;
114
John Stiles7571f9e2020-09-02 22:42:33 -0400115 using INHERITED = GrGLSLGeometryProcessor;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000116};
117
118///////////////////////////////////////////////////////////////////////////////
119
Brian Osman4a3f5c82018-09-18 16:16:38 -0400120GrBitmapTextGeoProc::GrBitmapTextGeoProc(const GrShaderCaps& caps,
Brian Osmancf860852018-10-31 14:04:39 -0400121 const SkPMColor4f& color,
Brian Osmanc906d252018-12-04 11:17:46 -0500122 bool wideColor,
Greg Daniel9715b6c2019-12-10 15:03:10 -0500123 const GrSurfaceProxyView* views,
124 int numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500125 GrSamplerState params,
126 GrMaskFormat format,
127 const SkMatrix& localMatrix,
128 bool usesW)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400129 : INHERITED(kGrBitmapTextGeoProc_ClassID)
130 , fColor(color)
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400131 , fLocalMatrix(localMatrix)
Jim Van Verthb515ae72018-05-23 16:44:55 -0400132 , fUsesW(usesW)
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400133 , fMaskFormat(format) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500134 SkASSERT(numActiveViews <= kMaxTextures);
Robert Phillips4bc70112018-03-01 10:24:02 -0500135
Jim Van Verthb515ae72018-05-23 16:44:55 -0400136 if (usesW) {
Brian Osmand4c29702018-09-14 16:16:55 -0400137 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Jim Van Verthb515ae72018-05-23 16:44:55 -0400138 } else {
Brian Osmand4c29702018-09-14 16:16:55 -0400139 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Jim Van Verthb515ae72018-05-23 16:44:55 -0400140 }
Brian Osman4a3f5c82018-09-18 16:16:38 -0400141
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500142 bool hasVertexColor = kA8_GrMaskFormat == fMaskFormat ||
143 kA565_GrMaskFormat == fMaskFormat;
144 if (hasVertexColor) {
Brian Osmanc906d252018-12-04 11:17:46 -0500145 fInColor = MakeColorAttribute("inColor", wideColor);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500146 }
Robert Phillips8296e752017-08-25 08:45:21 -0400147
Jim Van Verth2f2c77a2020-01-24 15:48:23 +0000148 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
149 caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500150 this->setVertexAttributes(&fInPosition, 3);
Brian Salomon92be2f72018-06-19 14:33:47 -0400151
Greg Daniel9715b6c2019-12-10 15:03:10 -0500152 if (numActiveViews) {
153 fAtlasDimensions = views[0].proxy()->dimensions();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000154 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500155 for (int i = 0; i < numActiveViews; ++i) {
156 const GrSurfaceProxy* proxy = views[i].proxy();
157 SkASSERT(proxy);
158 SkASSERT(proxy->dimensions() == fAtlasDimensions);
159 fTextureSamplers[i].reset(params, proxy->backendFormat(), views[i].swizzle());
Jim Van Vertha950b632017-09-12 11:54:11 -0400160 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500161 this->setTextureSamplerCnt(numActiveViews);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500162}
163
Greg Daniel9715b6c2019-12-10 15:03:10 -0500164void GrBitmapTextGeoProc::addNewViews(const GrSurfaceProxyView* views,
165 int numActiveViews,
Brian Salomonccb61422020-01-09 10:46:36 -0500166 GrSamplerState params) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500167 SkASSERT(numActiveViews <= kMaxTextures);
Jim Van Verth9f2516f2019-11-22 14:58:37 -0500168 // Just to make sure we don't try to add too many proxies
Brian Osman788b9162020-02-07 10:36:46 -0500169 numActiveViews = std::min(numActiveViews, kMaxTextures);
Robert Phillips4bc70112018-03-01 10:24:02 -0500170
Brian Salomon7eae3e02018-08-07 14:02:38 +0000171 if (!fTextureSamplers[0].isInitialized()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500172 fAtlasDimensions = views[0].proxy()->dimensions();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000173 }
174
Greg Daniel9715b6c2019-12-10 15:03:10 -0500175 for (int i = 0; i < numActiveViews; ++i) {
176 const GrSurfaceProxy* proxy = views[i].proxy();
177 SkASSERT(proxy);
178 SkASSERT(proxy->dimensions() == fAtlasDimensions);
Robert Phillips4bc70112018-03-01 10:24:02 -0500179
180 if (!fTextureSamplers[i].isInitialized()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500181 fTextureSamplers[i].reset(params, proxy->backendFormat(), views[i].swizzle());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400182 }
183 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500184 this->setTextureSamplerCnt(numActiveViews);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400185}
186
Brian Salomon94efbf52016-11-29 13:43:05 -0500187void GrBitmapTextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800188 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700189 GrGLBitmapTextGeoProc::GenKey(*this, caps, b);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000190}
191
Brian Salomon94efbf52016-11-29 13:43:05 -0500192GrGLSLPrimitiveProcessor* GrBitmapTextGeoProc::createGLSLInstance(const GrShaderCaps& caps) const {
joshualitt465283c2015-09-11 08:19:35 -0700193 return new GrGLBitmapTextGeoProc();
joshualitteb2a6762014-12-04 11:35:33 -0800194}
joshualitt9b989322014-12-15 14:16:27 -0800195
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000196///////////////////////////////////////////////////////////////////////////////
197
egdaniel309e3462014-12-09 10:35:58 -0800198GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrBitmapTextGeoProc);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000199
Hal Canary6f6961e2017-01-31 13:50:44 -0500200#if GR_TEST_UTILS
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400201
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500202GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(GrProcessorTestData* d) {
Greg Daniel026a60c2020-02-12 10:53:51 -0500203 auto [view, ct, at] = d->randomView();
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500204
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400205 GrSamplerState::WrapMode wrapModes[2];
206 GrTest::TestWrapModes(d->fRandom, wrapModes);
207 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
Brian Salomona3b02f52020-07-15 16:02:01 -0400208 ? GrSamplerState::Filter::kLinear
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400209 : GrSamplerState::Filter::kNearest);
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000210
Brian Salomon766098d2019-12-18 10:41:58 -0500211 GrMaskFormat format;
212 switch (ct) {
213 case GrColorType::kAlpha_8:
joshualitt02b05012015-02-11 06:56:30 -0800214 format = kA8_GrMaskFormat;
215 break;
Brian Salomon766098d2019-12-18 10:41:58 -0500216 case GrColorType::kBGR_565:
joshualitt02b05012015-02-11 06:56:30 -0800217 format = kA565_GrMaskFormat;
218 break;
Brian Salomon766098d2019-12-18 10:41:58 -0500219 case GrColorType::kRGBA_8888:
220 default: // It doesn't really matter that color type and mask format agree.
joshualitt02b05012015-02-11 06:56:30 -0800221 format = kARGB_GrMaskFormat;
222 break;
223 }
224
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500225 return GrBitmapTextGeoProc::Make(d->allocator(), *d->caps()->shaderCaps(),
Brian Osmancf860852018-10-31 14:04:39 -0400226 SkPMColor4f::FromBytes_RGBA(GrRandomColor(d->fRandom)),
Brian Osmanc906d252018-12-04 11:17:46 -0500227 d->fRandom->nextBool(),
Greg Daniel9715b6c2019-12-10 15:03:10 -0500228 &view, 1, samplerState, format,
Brian Osman1be2b7c2018-10-29 16:07:15 -0400229 GrTest::TestMatrix(d->fRandom), d->fRandom->nextBool());
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +0000230}
Hal Canary6f6961e2017-01-31 13:50:44 -0500231#endif