Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
| 8 | #include "GrTextureOp.h" |
| 9 | #include "GrAppliedClip.h" |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 10 | #include "GrCaps.h" |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 11 | #include "GrDrawOpTest.h" |
| 12 | #include "GrGeometryProcessor.h" |
| 13 | #include "GrMeshDrawOp.h" |
| 14 | #include "GrOpFlushState.h" |
| 15 | #include "GrQuad.h" |
| 16 | #include "GrResourceProvider.h" |
| 17 | #include "GrShaderCaps.h" |
| 18 | #include "GrTexture.h" |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 19 | #include "GrTexturePriv.h" |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 20 | #include "GrTextureProxy.h" |
| 21 | #include "SkGr.h" |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 22 | #include "SkMathPriv.h" |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 23 | #include "glsl/GrGLSLColorSpaceXformHelper.h" |
| 24 | #include "glsl/GrGLSLGeometryProcessor.h" |
| 25 | #include "glsl/GrGLSLVarying.h" |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | /** |
| 30 | * Geometry Processor that draws a texture modulated by a vertex color (though, this is meant to be |
| 31 | * the same value across all vertices of a quad and uses flat interpolation when available). This is |
| 32 | * used by TextureOp below. |
| 33 | */ |
| 34 | class TextureGeometryProcessor : public GrGeometryProcessor { |
| 35 | public: |
| 36 | struct Vertex { |
| 37 | SkPoint fPosition; |
| 38 | SkPoint fTextureCoords; |
| 39 | GrColor fColor; |
| 40 | }; |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 41 | struct MultiTextureVertex { |
| 42 | SkPoint fPosition; |
| 43 | int fTextureIdx; |
| 44 | SkPoint fTextureCoords; |
| 45 | GrColor fColor; |
| 46 | }; |
| 47 | |
| 48 | // Maximum number of textures supported by this op. Must also be checked against the caps |
| 49 | // limit. These numbers were based on some limited experiments on a HP Z840 and Pixel XL 2016 |
| 50 | // and could probably use more tuning. |
| 51 | #ifdef SK_BUILD_FOR_ANDROID |
| 52 | static constexpr int kMaxTextures = 4; |
| 53 | #else |
| 54 | static constexpr int kMaxTextures = 8; |
| 55 | #endif |
| 56 | |
Brian Salomon | 0b4d8aa | 2017-10-11 15:34:27 -0400 | [diff] [blame^] | 57 | static int SupportsMultitexture(const GrShaderCaps& caps) { |
| 58 | return caps.integerSupport() && !caps.disableImageMultitexturingSupport(); |
| 59 | } |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 60 | |
| 61 | static sk_sp<GrGeometryProcessor> Make(sk_sp<GrTextureProxy> proxies[], int proxyCnt, |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 62 | sk_sp<GrColorSpaceXform> csxf, |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 63 | const GrSamplerState::Filter filters[], |
| 64 | const GrShaderCaps& caps) { |
| 65 | // We use placement new to avoid always allocating space for kMaxTextures TextureSampler |
| 66 | // instances. |
| 67 | int samplerCnt = NumSamplersToUse(proxyCnt, caps); |
| 68 | size_t size = sizeof(TextureGeometryProcessor) + sizeof(TextureSampler) * (samplerCnt - 1); |
| 69 | void* mem = GrGeometryProcessor::operator new(size); |
| 70 | return sk_sp<TextureGeometryProcessor>(new (mem) TextureGeometryProcessor( |
| 71 | proxies, proxyCnt, samplerCnt, std::move(csxf), filters, caps)); |
| 72 | } |
| 73 | |
| 74 | ~TextureGeometryProcessor() override { |
| 75 | int cnt = this->numTextureSamplers(); |
| 76 | for (int i = 1; i < cnt; ++i) { |
| 77 | fSamplers[i].~TextureSampler(); |
| 78 | } |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | const char* name() const override { return "TextureGeometryProcessor"; } |
| 82 | |
| 83 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override { |
| 84 | b->add32(GrColorSpaceXform::XformKey(fColorSpaceXform.get())); |
| 85 | } |
| 86 | |
| 87 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override { |
| 88 | class GLSLProcessor : public GrGLSLGeometryProcessor { |
| 89 | public: |
| 90 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc, |
| 91 | FPCoordTransformIter&& transformIter) override { |
| 92 | const auto& textureGP = proc.cast<TextureGeometryProcessor>(); |
| 93 | this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter); |
| 94 | if (fColorSpaceXformHelper.isValid()) { |
| 95 | fColorSpaceXformHelper.setData(pdman, textureGP.fColorSpaceXform.get()); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 101 | const auto& textureGP = args.fGP.cast<TextureGeometryProcessor>(); |
| 102 | fColorSpaceXformHelper.emitCode( |
| 103 | args.fUniformHandler, textureGP.fColorSpaceXform.get()); |
| 104 | args.fVaryingHandler->setNoPerspective(); |
| 105 | args.fVaryingHandler->emitAttributes(textureGP); |
| 106 | this->writeOutputPosition(args.fVertBuilder, gpArgs, textureGP.fPositions.fName); |
| 107 | this->emitTransforms(args.fVertBuilder, |
| 108 | args.fVaryingHandler, |
| 109 | args.fUniformHandler, |
| 110 | gpArgs->fPositionVar, |
| 111 | textureGP.fTextureCoords.fName, |
| 112 | args.fFPCoordTransformHandler); |
Brian Salomon | 4127456 | 2017-09-15 09:40:03 -0700 | [diff] [blame] | 113 | if (args.fShaderCaps->preferFlatInterpolation()) { |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 114 | args.fVaryingHandler->addFlatPassThroughAttribute(&textureGP.fColors, |
| 115 | args.fOutputColor); |
| 116 | } else { |
| 117 | args.fVaryingHandler->addPassThroughAttribute(&textureGP.fColors, |
| 118 | args.fOutputColor); |
| 119 | } |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 120 | args.fFragBuilder->codeAppend("float2 texCoord;"); |
Brian Salomon | 977b54c | 2017-08-31 09:06:14 -0400 | [diff] [blame] | 121 | args.fVaryingHandler->addPassThroughAttribute(&textureGP.fTextureCoords, "texCoord", |
| 122 | kHigh_GrSLPrecision); |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 123 | if (textureGP.numTextureSamplers() > 1) { |
| 124 | SkASSERT(args.fShaderCaps->integerSupport()); |
| 125 | args.fFragBuilder->codeAppend("int texIdx;"); |
| 126 | if (args.fShaderCaps->flatInterpolationSupport()) { |
| 127 | args.fVaryingHandler->addFlatPassThroughAttribute(&textureGP.fTextureIdx, |
| 128 | "texIdx"); |
| 129 | } else { |
| 130 | args.fVaryingHandler->addPassThroughAttribute(&textureGP.fTextureIdx, |
| 131 | "texIdx"); |
| 132 | } |
| 133 | args.fFragBuilder->codeAppend("switch (texIdx) {"); |
| 134 | for (int i = 0; i < textureGP.numTextureSamplers(); ++i) { |
| 135 | args.fFragBuilder->codeAppendf("case %d: %s = ", i, args.fOutputColor); |
| 136 | args.fFragBuilder->appendTextureLookupAndModulate(args.fOutputColor, |
| 137 | args.fTexSamplers[i], |
| 138 | "texCoord", |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 139 | kFloat2_GrSLType, |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 140 | &fColorSpaceXformHelper); |
| 141 | args.fFragBuilder->codeAppend("; break;"); |
| 142 | } |
| 143 | args.fFragBuilder->codeAppend("}"); |
| 144 | } else { |
| 145 | args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor); |
| 146 | args.fFragBuilder->appendTextureLookupAndModulate(args.fOutputColor, |
| 147 | args.fTexSamplers[0], |
| 148 | "texCoord", |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 149 | kFloat2_GrSLType, |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 150 | &fColorSpaceXformHelper); |
| 151 | } |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 152 | args.fFragBuilder->codeAppend(";"); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 153 | args.fFragBuilder->codeAppendf("%s = float4(1);", args.fOutputCoverage); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 154 | } |
| 155 | GrGLSLColorSpaceXformHelper fColorSpaceXformHelper; |
| 156 | }; |
| 157 | return new GLSLProcessor; |
| 158 | } |
| 159 | |
| 160 | private: |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 161 | // This exists to reduce the number of shaders generated. It does some rounding of sampler |
| 162 | // counts. |
| 163 | static int NumSamplersToUse(int numRealProxies, const GrShaderCaps& caps) { |
| 164 | SkASSERT(numRealProxies > 0 && numRealProxies <= kMaxTextures && |
| 165 | numRealProxies <= caps.maxFragmentSamplers()); |
| 166 | if (1 == numRealProxies) { |
| 167 | return 1; |
| 168 | } |
| 169 | if (numRealProxies <= 4) { |
| 170 | return 4; |
| 171 | } |
| 172 | // Round to the next power of 2 and then clamp to kMaxTextures and the max allowed by caps. |
| 173 | return SkTMin(SkNextPow2(numRealProxies), SkTMin(kMaxTextures, caps.maxFragmentSamplers())); |
| 174 | } |
| 175 | |
| 176 | TextureGeometryProcessor(sk_sp<GrTextureProxy> proxies[], int proxyCnt, int samplerCnt, |
| 177 | sk_sp<GrColorSpaceXform> csxf, const GrSamplerState::Filter filters[], |
| 178 | const GrShaderCaps& caps) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 179 | : INHERITED(kTextureGeometryProcessor_ClassID) |
| 180 | , fColorSpaceXform(std::move(csxf)) { |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 181 | SkASSERT(proxyCnt > 0 && samplerCnt >= proxyCnt); |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 182 | fPositions = this->addVertexAttrib("position", kFloat2_GrVertexAttribType); |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 183 | fSamplers[0].reset(std::move(proxies[0]), filters[0]); |
| 184 | this->addTextureSampler(&fSamplers[0]); |
| 185 | for (int i = 1; i < proxyCnt; ++i) { |
| 186 | // This class has one sampler built in, the rest come from memory this processor was |
| 187 | // placement-newed into and so haven't been constructed. |
| 188 | new (&fSamplers[i]) TextureSampler(std::move(proxies[i]), filters[i]); |
| 189 | this->addTextureSampler(&fSamplers[i]); |
| 190 | } |
| 191 | if (samplerCnt > 1) { |
| 192 | // Here we initialize any extra samplers by repeating the last one samplerCnt - proxyCnt |
| 193 | // times. |
| 194 | GrTextureProxy* dupeProxy = fSamplers[proxyCnt - 1].proxy(); |
| 195 | for (int i = proxyCnt; i < samplerCnt; ++i) { |
| 196 | new (&fSamplers[i]) TextureSampler(sk_ref_sp(dupeProxy), filters[proxyCnt - 1]); |
| 197 | this->addTextureSampler(&fSamplers[i]); |
| 198 | } |
| 199 | SkASSERT(caps.integerSupport()); |
| 200 | fTextureIdx = this->addVertexAttrib("textureIdx", kInt_GrVertexAttribType); |
| 201 | } |
| 202 | |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 203 | fTextureCoords = this->addVertexAttrib("textureCoords", kFloat2_GrVertexAttribType); |
| 204 | fColors = this->addVertexAttrib("color", kUByte4_norm_GrVertexAttribType); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | Attribute fPositions; |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 208 | Attribute fTextureIdx; |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 209 | Attribute fTextureCoords; |
| 210 | Attribute fColors; |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 211 | sk_sp<GrColorSpaceXform> fColorSpaceXform; |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 212 | TextureSampler fSamplers[1]; |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 213 | |
| 214 | typedef GrGeometryProcessor INHERITED; |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | /** |
| 218 | * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a |
| 219 | * the texture by color. The blend with the destination is always src-over. The edges are non-AA. |
| 220 | */ |
| 221 | class TextureOp final : public GrMeshDrawOp { |
| 222 | public: |
| 223 | static std::unique_ptr<GrDrawOp> Make(sk_sp<GrTextureProxy> proxy, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 224 | GrSamplerState::Filter filter, GrColor color, |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 225 | const SkRect srcRect, const SkRect dstRect, |
| 226 | const SkMatrix& viewMatrix, sk_sp<GrColorSpaceXform> csxf, |
| 227 | bool allowSRBInputs) { |
| 228 | return std::unique_ptr<GrDrawOp>(new TextureOp(std::move(proxy), filter, color, srcRect, |
| 229 | dstRect, viewMatrix, std::move(csxf), |
| 230 | allowSRBInputs)); |
| 231 | } |
| 232 | |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 233 | ~TextureOp() override { |
| 234 | if (fFinalized) { |
| 235 | auto proxies = this->proxies(); |
| 236 | for (int i = 0; i < fProxyCnt; ++i) { |
| 237 | proxies[i]->completedRead(); |
| 238 | } |
| 239 | if (fProxyCnt > 1) { |
| 240 | delete[] reinterpret_cast<const char*>(proxies); |
| 241 | } |
| 242 | } else { |
| 243 | SkASSERT(1 == fProxyCnt); |
| 244 | fProxy0->unref(); |
| 245 | } |
| 246 | } |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 247 | |
| 248 | const char* name() const override { return "TextureOp"; } |
| 249 | |
Robert Phillips | f1748f5 | 2017-09-14 14:11:24 -0400 | [diff] [blame] | 250 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 251 | auto proxies = this->proxies(); |
| 252 | for (int i = 0; i < fProxyCnt; ++i) { |
| 253 | func(proxies[i]); |
| 254 | } |
| 255 | } |
| 256 | |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 257 | SkString dumpInfo() const override { |
| 258 | SkString str; |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 259 | str.appendf("AllowSRGBInputs: %d\n", fAllowSRGBInputs); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 260 | str.appendf("# draws: %d\n", fDraws.count()); |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 261 | auto proxies = this->proxies(); |
| 262 | for (int i = 0; i < fProxyCnt; ++i) { |
| 263 | str.appendf("Proxy ID %d: %d, Filter: %d\n", i, proxies[i]->uniqueID().asUInt(), |
| 264 | static_cast<int>(this->filters()[i])); |
| 265 | } |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 266 | for (int i = 0; i < fDraws.count(); ++i) { |
| 267 | const Draw& draw = fDraws[i]; |
| 268 | str.appendf( |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 269 | "%d: Color: 0x%08x, ProxyIdx: %d, TexRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f] " |
| 270 | "Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n", |
| 271 | i, draw.fColor, draw.fTextureIdx, draw.fSrcRect.fLeft, draw.fSrcRect.fTop, |
| 272 | draw.fSrcRect.fRight, draw.fSrcRect.fBottom, draw.fQuad.points()[0].fX, |
| 273 | draw.fQuad.points()[0].fY, draw.fQuad.points()[1].fX, draw.fQuad.points()[1].fY, |
| 274 | draw.fQuad.points()[2].fX, draw.fQuad.points()[2].fY, draw.fQuad.points()[3].fX, |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 275 | draw.fQuad.points()[3].fY); |
| 276 | } |
| 277 | str += INHERITED::dumpInfo(); |
| 278 | return str; |
| 279 | } |
| 280 | |
Brian Osman | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 281 | RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 282 | GrPixelConfigIsClamped dstIsClamped) override { |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 283 | SkASSERT(!fFinalized); |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 284 | SkASSERT(1 == fProxyCnt); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 285 | fFinalized = true; |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 286 | fProxy0->addPendingRead(); |
| 287 | fProxy0->unref(); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 288 | return RequiresDstTexture::kNo; |
| 289 | } |
| 290 | |
| 291 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
| 292 | |
| 293 | DEFINE_OP_CLASS_ID |
| 294 | |
| 295 | private: |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 296 | static constexpr int kMaxTextures = TextureGeometryProcessor::kMaxTextures; |
| 297 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 298 | TextureOp(sk_sp<GrTextureProxy> proxy, GrSamplerState::Filter filter, GrColor color, |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 299 | const SkRect& srcRect, const SkRect& dstRect, const SkMatrix& viewMatrix, |
| 300 | sk_sp<GrColorSpaceXform> csxf, bool allowSRGBInputs) |
| 301 | : INHERITED(ClassID()) |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 302 | , fColorSpaceXform(std::move(csxf)) |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 303 | , fProxy0(proxy.release()) |
| 304 | , fFilter0(filter) |
| 305 | , fProxyCnt(1) |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 306 | , fFinalized(false) |
| 307 | , fAllowSRGBInputs(allowSRGBInputs) { |
| 308 | Draw& draw = fDraws.push_back(); |
| 309 | draw.fSrcRect = srcRect; |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 310 | draw.fTextureIdx = 0; |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 311 | draw.fColor = color; |
| 312 | draw.fQuad.setFromMappedRect(dstRect, viewMatrix); |
| 313 | SkRect bounds; |
| 314 | bounds.setBounds(draw.fQuad.points(), 4); |
| 315 | this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo); |
| 316 | } |
| 317 | |
| 318 | void onPrepareDraws(Target* target) override { |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 319 | sk_sp<GrTextureProxy> proxiesSPs[kMaxTextures]; |
| 320 | auto proxies = this->proxies(); |
| 321 | auto filters = this->filters(); |
| 322 | for (int i = 0; i < fProxyCnt; ++i) { |
| 323 | if (!proxies[i]->instantiate(target->resourceProvider())) { |
| 324 | return; |
| 325 | } |
| 326 | proxiesSPs[i] = sk_ref_sp(proxies[i]); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 327 | } |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 328 | |
| 329 | sk_sp<GrGeometryProcessor> gp = |
| 330 | TextureGeometryProcessor::Make(proxiesSPs, fProxyCnt, std::move(fColorSpaceXform), |
| 331 | filters, *target->caps().shaderCaps()); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 332 | GrPipeline::InitArgs args; |
| 333 | args.fProxy = target->proxy(); |
| 334 | args.fCaps = &target->caps(); |
| 335 | args.fResourceProvider = target->resourceProvider(); |
| 336 | args.fFlags = fAllowSRGBInputs ? GrPipeline::kAllowSRGBInputs_Flag : 0; |
| 337 | const GrPipeline* pipeline = target->allocPipeline(args, GrProcessorSet::MakeEmptySet(), |
| 338 | target->detachAppliedClip()); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 339 | int vstart; |
| 340 | const GrBuffer* vbuffer; |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 341 | void* vdata = target->makeVertexSpace(gp->getVertexStride(), 4 * fDraws.count(), &vbuffer, |
| 342 | &vstart); |
| 343 | if (!vdata) { |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 344 | SkDebugf("Could not allocate vertices\n"); |
| 345 | return; |
| 346 | } |
| 347 | sk_sp<const GrBuffer> ibuffer; |
| 348 | if (fDraws.count() > 1) { |
| 349 | ibuffer.reset(target->resourceProvider()->refQuadIndexBuffer()); |
| 350 | if (!ibuffer) { |
| 351 | SkDebugf("Could not allocate quad indices\n"); |
| 352 | return; |
| 353 | } |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 354 | if (1 == fProxyCnt) { |
| 355 | SkASSERT(gp->getVertexStride() == sizeof(TextureGeometryProcessor::Vertex)); |
| 356 | for (int i = 0; i < fDraws.count(); ++i) { |
| 357 | auto vertices = static_cast<TextureGeometryProcessor::Vertex*>(vdata); |
| 358 | GrTexture* texture = proxies[0]->priv().peekTexture(); |
| 359 | float iw = 1.f / texture->width(); |
| 360 | float ih = 1.f / texture->height(); |
| 361 | float tl = iw * fDraws[i].fSrcRect.fLeft; |
| 362 | float tr = iw * fDraws[i].fSrcRect.fRight; |
| 363 | float tt = ih * fDraws[i].fSrcRect.fTop; |
| 364 | float tb = ih * fDraws[i].fSrcRect.fBottom; |
| 365 | if (proxies[0]->origin() == kBottomLeft_GrSurfaceOrigin) { |
| 366 | tt = 1.f - tt; |
| 367 | tb = 1.f - tb; |
| 368 | } |
| 369 | vertices[0 + 4 * i].fPosition = fDraws[i].fQuad.points()[0]; |
| 370 | vertices[0 + 4 * i].fTextureCoords = {tl, tt}; |
| 371 | vertices[0 + 4 * i].fColor = fDraws[i].fColor; |
| 372 | vertices[1 + 4 * i].fPosition = fDraws[i].fQuad.points()[1]; |
| 373 | vertices[1 + 4 * i].fTextureCoords = {tl, tb}; |
| 374 | vertices[1 + 4 * i].fColor = fDraws[i].fColor; |
| 375 | vertices[2 + 4 * i].fPosition = fDraws[i].fQuad.points()[2]; |
| 376 | vertices[2 + 4 * i].fTextureCoords = {tr, tb}; |
| 377 | vertices[2 + 4 * i].fColor = fDraws[i].fColor; |
| 378 | vertices[3 + 4 * i].fPosition = fDraws[i].fQuad.points()[3]; |
| 379 | vertices[3 + 4 * i].fTextureCoords = {tr, tt}; |
| 380 | vertices[3 + 4 * i].fColor = fDraws[i].fColor; |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 381 | } |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 382 | } else { |
| 383 | SkASSERT(gp->getVertexStride() == |
| 384 | sizeof(TextureGeometryProcessor::MultiTextureVertex)); |
| 385 | GrTexture* textures[kMaxTextures]; |
| 386 | float iw[kMaxTextures]; |
| 387 | float ih[kMaxTextures]; |
| 388 | for (int t = 0; t < fProxyCnt; ++t) { |
| 389 | textures[t] = proxies[t]->priv().peekTexture(); |
| 390 | iw[t] = 1.f / textures[t]->width(); |
| 391 | ih[t] = 1.f / textures[t]->height(); |
| 392 | } |
| 393 | for (int i = 0; i < fDraws.count(); ++i) { |
| 394 | int t = fDraws[i].fTextureIdx; |
| 395 | auto vertices = |
| 396 | static_cast<TextureGeometryProcessor::MultiTextureVertex*>(vdata); |
| 397 | float tl = iw[t] * fDraws[i].fSrcRect.fLeft; |
| 398 | float tr = iw[t] * fDraws[i].fSrcRect.fRight; |
| 399 | float tt = ih[t] * fDraws[i].fSrcRect.fTop; |
| 400 | float tb = ih[t] * fDraws[i].fSrcRect.fBottom; |
| 401 | if (proxies[t]->origin() == kBottomLeft_GrSurfaceOrigin) { |
| 402 | tt = 1.f - tt; |
| 403 | tb = 1.f - tb; |
| 404 | } |
| 405 | vertices[0 + 4 * i].fPosition = fDraws[i].fQuad.points()[0]; |
| 406 | vertices[0 + 4 * i].fTextureIdx = t; |
| 407 | vertices[0 + 4 * i].fTextureCoords = {tl, tt}; |
| 408 | vertices[0 + 4 * i].fColor = fDraws[i].fColor; |
| 409 | vertices[1 + 4 * i].fPosition = fDraws[i].fQuad.points()[1]; |
| 410 | vertices[1 + 4 * i].fTextureIdx = t; |
| 411 | vertices[1 + 4 * i].fTextureCoords = {tl, tb}; |
| 412 | vertices[1 + 4 * i].fColor = fDraws[i].fColor; |
| 413 | vertices[2 + 4 * i].fPosition = fDraws[i].fQuad.points()[2]; |
| 414 | vertices[2 + 4 * i].fTextureIdx = t; |
| 415 | vertices[2 + 4 * i].fTextureCoords = {tr, tb}; |
| 416 | vertices[2 + 4 * i].fColor = fDraws[i].fColor; |
| 417 | vertices[3 + 4 * i].fPosition = fDraws[i].fQuad.points()[3]; |
| 418 | vertices[3 + 4 * i].fTextureIdx = t; |
| 419 | vertices[3 + 4 * i].fTextureCoords = {tr, tt}; |
| 420 | vertices[3 + 4 * i].fColor = fDraws[i].fColor; |
| 421 | } |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 422 | } |
| 423 | GrMesh mesh(GrPrimitiveType::kTriangles); |
| 424 | mesh.setIndexedPatterned(ibuffer.get(), 6, 4, fDraws.count(), |
| 425 | GrResourceProvider::QuadCountOfQuadBuffer()); |
| 426 | mesh.setVertexData(vbuffer, vstart); |
| 427 | target->draw(gp.get(), pipeline, mesh); |
| 428 | } else { |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 429 | // If there is only one draw then there can only be one proxy. |
| 430 | SkASSERT(1 == fProxyCnt); |
| 431 | SkASSERT(gp->getVertexStride() == sizeof(TextureGeometryProcessor::Vertex)); |
| 432 | auto vertices = static_cast<TextureGeometryProcessor::Vertex*>(vdata); |
| 433 | GrTexture* texture = proxies[0]->priv().peekTexture(); |
| 434 | float iw = 1.f / texture->width(); |
| 435 | float ih = 1.f / texture->height(); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 436 | float tl = iw * fDraws[0].fSrcRect.fLeft; |
| 437 | float tr = iw * fDraws[0].fSrcRect.fRight; |
| 438 | float tt = ih * fDraws[0].fSrcRect.fTop; |
| 439 | float tb = ih * fDraws[0].fSrcRect.fBottom; |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 440 | if (proxies[0]->origin() == kBottomLeft_GrSurfaceOrigin) { |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 441 | tt = 1.f - tt; |
| 442 | tb = 1.f - tb; |
| 443 | } |
| 444 | vertices[0].fPosition = fDraws[0].fQuad.points()[0]; |
| 445 | vertices[0].fTextureCoords = {tl, tt}; |
| 446 | vertices[0].fColor = fDraws[0].fColor; |
| 447 | vertices[1].fPosition = fDraws[0].fQuad.points()[3]; |
| 448 | vertices[1].fTextureCoords = {tr, tt}; |
| 449 | vertices[1].fColor = fDraws[0].fColor; |
| 450 | vertices[2].fPosition = fDraws[0].fQuad.points()[1]; |
| 451 | vertices[2].fTextureCoords = {tl, tb}; |
| 452 | vertices[2].fColor = fDraws[0].fColor; |
| 453 | vertices[3].fPosition = fDraws[0].fQuad.points()[2]; |
| 454 | vertices[3].fTextureCoords = {tr, tb}; |
| 455 | vertices[3].fColor = fDraws[0].fColor; |
| 456 | GrMesh mesh(GrPrimitiveType::kTriangleStrip); |
| 457 | mesh.setNonIndexedNonInstanced(4); |
| 458 | mesh.setVertexData(vbuffer, vstart); |
| 459 | target->draw(gp.get(), pipeline, mesh); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
| 464 | const auto* that = t->cast<TextureOp>(); |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 465 | if (!GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) { |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 466 | return false; |
| 467 | } |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 468 | // Because of an issue where GrColorSpaceXform adds the same function every time it is used |
| 469 | // in a texture lookup, we only allow multiple textures when there is no transform. |
| 470 | if (TextureGeometryProcessor::SupportsMultitexture(*caps.shaderCaps()) && |
| 471 | !fColorSpaceXform) { |
| 472 | int map[kMaxTextures]; |
| 473 | int numNewProxies = this->mergeProxies(that, map, *caps.shaderCaps()); |
| 474 | if (numNewProxies < 0) { |
| 475 | return false; |
| 476 | } |
| 477 | if (1 == fProxyCnt && numNewProxies) { |
| 478 | void* mem = new char[(sizeof(GrSamplerState::Filter) + sizeof(GrTextureProxy*)) * |
| 479 | kMaxTextures]; |
| 480 | auto proxies = reinterpret_cast<GrTextureProxy**>(mem); |
| 481 | auto filters = reinterpret_cast<GrSamplerState::Filter*>(proxies + kMaxTextures); |
| 482 | proxies[0] = fProxy0; |
| 483 | filters[0] = fFilter0; |
| 484 | fProxyArray = proxies; |
| 485 | } |
| 486 | fProxyCnt += numNewProxies; |
| 487 | auto thisProxies = fProxyArray; |
| 488 | auto thatProxies = that->proxies(); |
| 489 | auto thatFilters = that->filters(); |
| 490 | auto thisFilters = reinterpret_cast<GrSamplerState::Filter*>(thisProxies + |
| 491 | kMaxTextures); |
| 492 | for (int i = 0; i < that->fProxyCnt; ++i) { |
| 493 | if (map[i] < 0) { |
| 494 | thatProxies[i]->addPendingRead(); |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 495 | |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 496 | thisProxies[-map[i]] = thatProxies[i]; |
| 497 | thisFilters[-map[i]] = thatFilters[i]; |
| 498 | map[i] = -map[i]; |
| 499 | } |
| 500 | } |
| 501 | int firstNewDraw = fDraws.count(); |
| 502 | fDraws.push_back_n(that->fDraws.count(), that->fDraws.begin()); |
| 503 | for (int i = firstNewDraw; i < fDraws.count(); ++i) { |
| 504 | fDraws[i].fTextureIdx = map[fDraws[i].fTextureIdx]; |
| 505 | } |
| 506 | } else { |
| 507 | if (fProxy0->uniqueID() != that->fProxy0->uniqueID() || fFilter0 != that->fFilter0) { |
| 508 | return false; |
| 509 | } |
| 510 | fDraws.push_back_n(that->fDraws.count(), that->fDraws.begin()); |
| 511 | } |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 512 | this->joinBounds(*that); |
| 513 | return true; |
| 514 | } |
| 515 | |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 516 | /** |
| 517 | * Determines a mapping of indices from that's proxy array to this's proxy array. A negative map |
| 518 | * value means that's proxy should be added to this's proxy array at the absolute value of |
| 519 | * the map entry. If it is determined that the ops shouldn't combine their proxies then a |
| 520 | * negative value is returned. Otherwise, return value indicates the number of proxies that have |
| 521 | * to be added to this op or, equivalently, the number of negative entries in map. |
| 522 | */ |
| 523 | int mergeProxies(const TextureOp* that, int map[kMaxTextures], const GrShaderCaps& caps) const { |
| 524 | std::fill_n(map, kMaxTextures, -kMaxTextures); |
| 525 | int sharedProxyCnt = 0; |
| 526 | auto thisProxies = this->proxies(); |
| 527 | auto thisFilters = this->filters(); |
| 528 | auto thatProxies = that->proxies(); |
| 529 | auto thatFilters = that->filters(); |
| 530 | for (int i = 0; i < fProxyCnt; ++i) { |
| 531 | for (int j = 0; j < that->fProxyCnt; ++j) { |
| 532 | if (thisProxies[i]->uniqueID() == thatProxies[j]->uniqueID()) { |
| 533 | if (thisFilters[i] != thatFilters[j]) { |
| 534 | // In GL we don't currently support using the same texture with different |
| 535 | // samplers. If we added support for sampler objects and a cap bit to know |
| 536 | // it's ok to use different filter modes then we could support this. |
| 537 | // Otherwise, we could also only allow a single filter mode for each op |
| 538 | // instance. |
| 539 | return -1; |
| 540 | } |
| 541 | map[j] = i; |
| 542 | ++sharedProxyCnt; |
| 543 | break; |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | int actualMaxTextures = SkTMin(caps.maxFragmentImageStorages(), kMaxTextures); |
| 548 | int newProxyCnt = that->fProxyCnt - sharedProxyCnt; |
| 549 | if (newProxyCnt + fProxyCnt > actualMaxTextures) { |
| 550 | return -1; |
| 551 | } |
| 552 | GrPixelConfig config = thisProxies[0]->config(); |
| 553 | int nextSlot = fProxyCnt; |
| 554 | for (int j = 0; j < that->fProxyCnt; ++j) { |
| 555 | // We want to avoid making many shaders because of different permutations of shader |
| 556 | // based swizzle and sampler types. The approach taken here is to require the configs to |
| 557 | // be the same and to only allow already instantiated proxies that have the most |
| 558 | // common sampler type. Otherwise we don't merge. |
| 559 | if (thatProxies[j]->config() != config) { |
| 560 | return -1; |
| 561 | } |
| 562 | if (GrTexture* tex = thatProxies[j]->priv().peekTexture()) { |
| 563 | if (tex->texturePriv().samplerType() != kTexture2DSampler_GrSLType) { |
| 564 | return -1; |
| 565 | } |
| 566 | } |
| 567 | if (map[j] < 0) { |
| 568 | map[j] = -(nextSlot++); |
| 569 | } |
| 570 | } |
| 571 | return newProxyCnt; |
| 572 | } |
| 573 | |
| 574 | GrTextureProxy* const* proxies() const { return fProxyCnt > 1 ? fProxyArray : &fProxy0; } |
| 575 | |
| 576 | const GrSamplerState::Filter* filters() const { |
| 577 | if (fProxyCnt > 1) { |
| 578 | return reinterpret_cast<const GrSamplerState::Filter*>(fProxyArray + kMaxTextures); |
| 579 | } |
| 580 | return &fFilter0; |
| 581 | } |
| 582 | |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 583 | struct Draw { |
| 584 | SkRect fSrcRect; |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 585 | int fTextureIdx; |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 586 | GrQuad fQuad; |
| 587 | GrColor fColor; |
| 588 | }; |
| 589 | SkSTArray<1, Draw, true> fDraws; |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 590 | sk_sp<GrColorSpaceXform> fColorSpaceXform; |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 591 | // Initially we store a single proxy ptr and a single filter. If we grow to have more than |
| 592 | // one proxy we instead store pointers to dynamically allocated arrays of size kMaxTextures |
| 593 | // followed by kMaxTextures filters. |
| 594 | union { |
| 595 | GrTextureProxy* fProxy0; |
| 596 | GrTextureProxy** fProxyArray; |
| 597 | }; |
| 598 | // The next four members should pack. |
| 599 | GrSamplerState::Filter fFilter0; |
| 600 | uint8_t fProxyCnt; |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 601 | // Used to track whether fProxy is ref'ed or has a pending IO after finalize() is called. |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 602 | uint8_t fFinalized; |
| 603 | uint8_t fAllowSRGBInputs; |
| 604 | |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 605 | typedef GrMeshDrawOp INHERITED; |
| 606 | }; |
| 607 | |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 608 | constexpr int TextureGeometryProcessor::kMaxTextures; |
| 609 | constexpr int TextureOp::kMaxTextures; |
| 610 | |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 611 | } // anonymous namespace |
| 612 | |
| 613 | namespace GrTextureOp { |
| 614 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 615 | std::unique_ptr<GrDrawOp> Make(sk_sp<GrTextureProxy> proxy, GrSamplerState::Filter filter, |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 616 | GrColor color, const SkRect& srcRect, const SkRect& dstRect, |
| 617 | const SkMatrix& viewMatrix, sk_sp<GrColorSpaceXform> csxf, |
| 618 | bool allowSRGBInputs) { |
| 619 | SkASSERT(!viewMatrix.hasPerspective()); |
| 620 | return TextureOp::Make(std::move(proxy), filter, color, srcRect, dstRect, viewMatrix, |
| 621 | std::move(csxf), allowSRGBInputs); |
| 622 | } |
| 623 | |
| 624 | } // namespace GrTextureOp |
| 625 | |
| 626 | #if GR_TEST_UTILS |
| 627 | #include "GrContext.h" |
| 628 | |
| 629 | GR_DRAW_OP_TEST_DEFINE(TextureOp) { |
| 630 | GrSurfaceDesc desc; |
| 631 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 632 | desc.fHeight = random->nextULessThan(90) + 10; |
| 633 | desc.fWidth = random->nextULessThan(90) + 10; |
| 634 | desc.fOrigin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin; |
| 635 | SkBackingFit fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact; |
| 636 | auto proxy = |
| 637 | GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, fit, SkBudgeted::kNo); |
| 638 | SkRect rect = GrTest::TestRect(random); |
| 639 | SkRect srcRect; |
| 640 | srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f); |
| 641 | srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f; |
| 642 | srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f); |
| 643 | srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f; |
| 644 | SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random); |
| 645 | GrColor color = SkColorToPremulGrColor(random->nextU()); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 646 | GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan( |
| 647 | static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 648 | auto csxf = GrTest::TestColorXform(random); |
| 649 | bool allowSRGBInputs = random->nextBool(); |
| 650 | return GrTextureOp::Make(std::move(proxy), filter, color, srcRect, rect, viewMatrix, |
| 651 | std::move(csxf), allowSRGBInputs); |
| 652 | } |
| 653 | |
| 654 | #endif |