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 | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 23 | #include "SkMatrixPriv.h" |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 24 | #include "SkPoint.h" |
| 25 | #include "SkPoint3.h" |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 26 | #include "glsl/GrGLSLColorSpaceXformHelper.h" |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 27 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 28 | #include "glsl/GrGLSLGeometryProcessor.h" |
| 29 | #include "glsl/GrGLSLVarying.h" |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 30 | #include "glsl/GrGLSLVertexGeoBuilder.h" |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 31 | |
| 32 | namespace { |
| 33 | |
| 34 | /** |
| 35 | * Geometry Processor that draws a texture modulated by a vertex color (though, this is meant to be |
| 36 | * the same value across all vertices of a quad and uses flat interpolation when available). This is |
| 37 | * used by TextureOp below. |
| 38 | */ |
| 39 | class TextureGeometryProcessor : public GrGeometryProcessor { |
| 40 | public: |
| 41 | struct Vertex { |
| 42 | SkPoint fPosition; |
| 43 | SkPoint fTextureCoords; |
| 44 | GrColor fColor; |
| 45 | }; |
Brian Salomon | 30e1a5e | 2018-05-18 12:32:32 -0400 | [diff] [blame^] | 46 | struct AAVertex : public Vertex { |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 47 | SkPoint3 fEdges[4]; |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 48 | }; |
Brian Salomon | 30e1a5e | 2018-05-18 12:32:32 -0400 | [diff] [blame^] | 49 | struct MultiTextureVertex : Vertex { |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 50 | int fTextureIdx; |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 51 | }; |
Brian Salomon | 30e1a5e | 2018-05-18 12:32:32 -0400 | [diff] [blame^] | 52 | struct AAMultiTextureVertex : MultiTextureVertex { |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 53 | SkPoint3 fEdges[4]; |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 54 | }; |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 55 | |
| 56 | // Maximum number of textures supported by this op. Must also be checked against the caps |
| 57 | // limit. These numbers were based on some limited experiments on a HP Z840 and Pixel XL 2016 |
| 58 | // and could probably use more tuning. |
| 59 | #ifdef SK_BUILD_FOR_ANDROID |
| 60 | static constexpr int kMaxTextures = 4; |
| 61 | #else |
| 62 | static constexpr int kMaxTextures = 8; |
| 63 | #endif |
| 64 | |
Brian Salomon | 0b4d8aa | 2017-10-11 15:34:27 -0400 | [diff] [blame] | 65 | static int SupportsMultitexture(const GrShaderCaps& caps) { |
Brian Salomon | 762d5e7 | 2017-12-01 10:25:08 -0500 | [diff] [blame] | 66 | return caps.integerSupport() && caps.maxFragmentSamplers() > 1; |
Brian Salomon | 0b4d8aa | 2017-10-11 15:34:27 -0400 | [diff] [blame] | 67 | } |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 68 | |
| 69 | static sk_sp<GrGeometryProcessor> Make(sk_sp<GrTextureProxy> proxies[], int proxyCnt, |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 70 | sk_sp<GrColorSpaceXform> csxf, bool coverageAA, |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 71 | const GrSamplerState::Filter filters[], |
| 72 | const GrShaderCaps& caps) { |
| 73 | // We use placement new to avoid always allocating space for kMaxTextures TextureSampler |
| 74 | // instances. |
| 75 | int samplerCnt = NumSamplersToUse(proxyCnt, caps); |
| 76 | size_t size = sizeof(TextureGeometryProcessor) + sizeof(TextureSampler) * (samplerCnt - 1); |
| 77 | void* mem = GrGeometryProcessor::operator new(size); |
| 78 | return sk_sp<TextureGeometryProcessor>(new (mem) TextureGeometryProcessor( |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 79 | proxies, proxyCnt, samplerCnt, std::move(csxf), coverageAA, filters, caps)); |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | ~TextureGeometryProcessor() override { |
| 83 | int cnt = this->numTextureSamplers(); |
| 84 | for (int i = 1; i < cnt; ++i) { |
| 85 | fSamplers[i].~TextureSampler(); |
| 86 | } |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | const char* name() const override { return "TextureGeometryProcessor"; } |
| 90 | |
| 91 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override { |
| 92 | b->add32(GrColorSpaceXform::XformKey(fColorSpaceXform.get())); |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 93 | b->add32(static_cast<uint32_t>(this->usesCoverageEdgeAA())); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override { |
| 97 | class GLSLProcessor : public GrGLSLGeometryProcessor { |
| 98 | public: |
| 99 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc, |
| 100 | FPCoordTransformIter&& transformIter) override { |
| 101 | const auto& textureGP = proc.cast<TextureGeometryProcessor>(); |
| 102 | this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter); |
| 103 | if (fColorSpaceXformHelper.isValid()) { |
| 104 | fColorSpaceXformHelper.setData(pdman, textureGP.fColorSpaceXform.get()); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
Chris Dalton | 7b04631 | 2018-02-02 11:06:30 -0700 | [diff] [blame] | 110 | using Interpolation = GrGLSLVaryingHandler::Interpolation; |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 111 | const auto& textureGP = args.fGP.cast<TextureGeometryProcessor>(); |
| 112 | fColorSpaceXformHelper.emitCode( |
| 113 | args.fUniformHandler, textureGP.fColorSpaceXform.get()); |
| 114 | args.fVaryingHandler->setNoPerspective(); |
| 115 | args.fVaryingHandler->emitAttributes(textureGP); |
| 116 | this->writeOutputPosition(args.fVertBuilder, gpArgs, textureGP.fPositions.fName); |
| 117 | this->emitTransforms(args.fVertBuilder, |
| 118 | args.fVaryingHandler, |
| 119 | args.fUniformHandler, |
Brian Salomon | 04460cc | 2017-12-06 14:47:42 -0500 | [diff] [blame] | 120 | textureGP.fTextureCoords.asShaderVar(), |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 121 | args.fFPCoordTransformHandler); |
Chris Dalton | 7b04631 | 2018-02-02 11:06:30 -0700 | [diff] [blame] | 122 | args.fVaryingHandler->addPassThroughAttribute(&textureGP.fColors, |
| 123 | args.fOutputColor, |
| 124 | Interpolation::kCanBeFlat); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 125 | args.fFragBuilder->codeAppend("float2 texCoord;"); |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 126 | args.fVaryingHandler->addPassThroughAttribute(&textureGP.fTextureCoords, |
| 127 | "texCoord"); |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 128 | if (textureGP.numTextureSamplers() > 1) { |
Chris Dalton | 7b04631 | 2018-02-02 11:06:30 -0700 | [diff] [blame] | 129 | // If this changes to float, reconsider Interpolation::kMustBeFlat. |
| 130 | SkASSERT(kInt_GrVertexAttribType == textureGP.fTextureIdx.fType); |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 131 | SkASSERT(args.fShaderCaps->integerSupport()); |
| 132 | args.fFragBuilder->codeAppend("int texIdx;"); |
Chris Dalton | 7b04631 | 2018-02-02 11:06:30 -0700 | [diff] [blame] | 133 | args.fVaryingHandler->addPassThroughAttribute(&textureGP.fTextureIdx, "texIdx", |
| 134 | Interpolation::kMustBeFlat); |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 135 | args.fFragBuilder->codeAppend("switch (texIdx) {"); |
| 136 | for (int i = 0; i < textureGP.numTextureSamplers(); ++i) { |
| 137 | args.fFragBuilder->codeAppendf("case %d: %s = ", i, args.fOutputColor); |
| 138 | args.fFragBuilder->appendTextureLookupAndModulate(args.fOutputColor, |
| 139 | args.fTexSamplers[i], |
| 140 | "texCoord", |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 141 | kFloat2_GrSLType, |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 142 | &fColorSpaceXformHelper); |
| 143 | args.fFragBuilder->codeAppend("; break;"); |
| 144 | } |
| 145 | args.fFragBuilder->codeAppend("}"); |
| 146 | } else { |
| 147 | args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor); |
| 148 | args.fFragBuilder->appendTextureLookupAndModulate(args.fOutputColor, |
| 149 | args.fTexSamplers[0], |
| 150 | "texCoord", |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 151 | kFloat2_GrSLType, |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 152 | &fColorSpaceXformHelper); |
| 153 | } |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 154 | args.fFragBuilder->codeAppend(";"); |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 155 | if (textureGP.usesCoverageEdgeAA()) { |
Brian Salomon | dba65f9 | 2018-01-22 08:43:38 -0500 | [diff] [blame] | 156 | const char* aaDistName = nullptr; |
| 157 | // When interpolation is innacurate we perform the evaluation of the edge |
| 158 | // equations in the fragment shader rather than interpolating values computed |
| 159 | // in the vertex shader. |
| 160 | if (!args.fShaderCaps->interpolantsAreInaccurate()) { |
| 161 | GrGLSLVarying aaDistVarying(kFloat4_GrSLType, |
| 162 | GrGLSLVarying::Scope::kVertToFrag); |
| 163 | args.fVaryingHandler->addVarying("aaDists", &aaDistVarying); |
| 164 | args.fVertBuilder->codeAppendf( |
| 165 | R"(%s = float4(dot(aaEdge0.xy, %s.xy) + aaEdge0.z, |
| 166 | dot(aaEdge1.xy, %s.xy) + aaEdge1.z, |
| 167 | dot(aaEdge2.xy, %s.xy) + aaEdge2.z, |
| 168 | dot(aaEdge3.xy, %s.xy) + aaEdge3.z);)", |
| 169 | aaDistVarying.vsOut(), textureGP.fPositions.fName, |
| 170 | textureGP.fPositions.fName, textureGP.fPositions.fName, |
| 171 | textureGP.fPositions.fName); |
| 172 | aaDistName = aaDistVarying.fsIn(); |
| 173 | } else { |
| 174 | GrGLSLVarying aaEdgeVarying[4]{ |
| 175 | {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag}, |
| 176 | {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag}, |
| 177 | {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag}, |
| 178 | {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag} |
| 179 | }; |
| 180 | for (int i = 0; i < 4; ++i) { |
| 181 | SkString name; |
| 182 | name.printf("aaEdge%d", i); |
Brian Salomon | 7d982c6 | 2018-02-05 16:20:47 -0500 | [diff] [blame] | 183 | args.fVaryingHandler->addVarying(name.c_str(), &aaEdgeVarying[i], |
| 184 | Interpolation::kCanBeFlat); |
Brian Salomon | dba65f9 | 2018-01-22 08:43:38 -0500 | [diff] [blame] | 185 | args.fVertBuilder->codeAppendf( |
| 186 | "%s = aaEdge%d;", aaEdgeVarying[i].vsOut(), i); |
| 187 | } |
| 188 | args.fFragBuilder->codeAppendf( |
| 189 | R"(float4 aaDists = float4(dot(%s.xy, sk_FragCoord.xy) + %s.z, |
| 190 | dot(%s.xy, sk_FragCoord.xy) + %s.z, |
| 191 | dot(%s.xy, sk_FragCoord.xy) + %s.z, |
| 192 | dot(%s.xy, sk_FragCoord.xy) + %s.z);)", |
| 193 | aaEdgeVarying[0].fsIn(), aaEdgeVarying[0].fsIn(), |
| 194 | aaEdgeVarying[1].fsIn(), aaEdgeVarying[1].fsIn(), |
| 195 | aaEdgeVarying[2].fsIn(), aaEdgeVarying[2].fsIn(), |
| 196 | aaEdgeVarying[3].fsIn(), aaEdgeVarying[3].fsIn()); |
| 197 | aaDistName = "aaDists"; |
| 198 | } |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 199 | args.fFragBuilder->codeAppendf( |
| 200 | "float mindist = min(min(%s.x, %s.y), min(%s.z, %s.w));", |
Brian Salomon | dba65f9 | 2018-01-22 08:43:38 -0500 | [diff] [blame] | 201 | aaDistName, aaDistName, aaDistName, aaDistName); |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 202 | args.fFragBuilder->codeAppendf("%s = float4(clamp(mindist, 0, 1));", |
| 203 | args.fOutputCoverage); |
| 204 | } else { |
| 205 | args.fFragBuilder->codeAppendf("%s = float4(1);", args.fOutputCoverage); |
| 206 | } |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 207 | } |
| 208 | GrGLSLColorSpaceXformHelper fColorSpaceXformHelper; |
| 209 | }; |
| 210 | return new GLSLProcessor; |
| 211 | } |
| 212 | |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 213 | bool usesCoverageEdgeAA() const { return SkToBool(fAAEdges[0].isInitialized()); } |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 214 | |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 215 | private: |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 216 | // This exists to reduce the number of shaders generated. It does some rounding of sampler |
| 217 | // counts. |
| 218 | static int NumSamplersToUse(int numRealProxies, const GrShaderCaps& caps) { |
| 219 | SkASSERT(numRealProxies > 0 && numRealProxies <= kMaxTextures && |
| 220 | numRealProxies <= caps.maxFragmentSamplers()); |
| 221 | if (1 == numRealProxies) { |
| 222 | return 1; |
| 223 | } |
| 224 | if (numRealProxies <= 4) { |
| 225 | return 4; |
| 226 | } |
| 227 | // Round to the next power of 2 and then clamp to kMaxTextures and the max allowed by caps. |
| 228 | return SkTMin(SkNextPow2(numRealProxies), SkTMin(kMaxTextures, caps.maxFragmentSamplers())); |
| 229 | } |
| 230 | |
| 231 | TextureGeometryProcessor(sk_sp<GrTextureProxy> proxies[], int proxyCnt, int samplerCnt, |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 232 | sk_sp<GrColorSpaceXform> csxf, bool coverageAA, |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 233 | const GrSamplerState::Filter filters[], const GrShaderCaps& caps) |
| 234 | : INHERITED(kTextureGeometryProcessor_ClassID), fColorSpaceXform(std::move(csxf)) { |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 235 | SkASSERT(proxyCnt > 0 && samplerCnt >= proxyCnt); |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 236 | fSamplers[0].reset(std::move(proxies[0]), filters[0]); |
| 237 | this->addTextureSampler(&fSamplers[0]); |
| 238 | for (int i = 1; i < proxyCnt; ++i) { |
| 239 | // This class has one sampler built in, the rest come from memory this processor was |
| 240 | // placement-newed into and so haven't been constructed. |
| 241 | new (&fSamplers[i]) TextureSampler(std::move(proxies[i]), filters[i]); |
| 242 | this->addTextureSampler(&fSamplers[i]); |
| 243 | } |
Brian Salomon | 30e1a5e | 2018-05-18 12:32:32 -0400 | [diff] [blame^] | 244 | |
| 245 | fPositions = this->addVertexAttrib("position", kFloat2_GrVertexAttribType); |
| 246 | fTextureCoords = this->addVertexAttrib("textureCoords", kFloat2_GrVertexAttribType); |
| 247 | fColors = this->addVertexAttrib("color", kUByte4_norm_GrVertexAttribType); |
| 248 | |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 249 | if (samplerCnt > 1) { |
| 250 | // Here we initialize any extra samplers by repeating the last one samplerCnt - proxyCnt |
| 251 | // times. |
| 252 | GrTextureProxy* dupeProxy = fSamplers[proxyCnt - 1].proxy(); |
| 253 | for (int i = proxyCnt; i < samplerCnt; ++i) { |
| 254 | new (&fSamplers[i]) TextureSampler(sk_ref_sp(dupeProxy), filters[proxyCnt - 1]); |
| 255 | this->addTextureSampler(&fSamplers[i]); |
| 256 | } |
| 257 | SkASSERT(caps.integerSupport()); |
| 258 | fTextureIdx = this->addVertexAttrib("textureIdx", kInt_GrVertexAttribType); |
| 259 | } |
| 260 | |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 261 | if (coverageAA) { |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 262 | fAAEdges[0] = this->addVertexAttrib("aaEdge0", kFloat3_GrVertexAttribType); |
| 263 | fAAEdges[1] = this->addVertexAttrib("aaEdge1", kFloat3_GrVertexAttribType); |
| 264 | fAAEdges[2] = this->addVertexAttrib("aaEdge2", kFloat3_GrVertexAttribType); |
| 265 | fAAEdges[3] = this->addVertexAttrib("aaEdge3", kFloat3_GrVertexAttribType); |
| 266 | } |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | Attribute fPositions; |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 270 | Attribute fColors; |
Brian Salomon | 30e1a5e | 2018-05-18 12:32:32 -0400 | [diff] [blame^] | 271 | Attribute fTextureCoords; |
| 272 | Attribute fTextureIdx; |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 273 | Attribute fAAEdges[4]; |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 274 | sk_sp<GrColorSpaceXform> fColorSpaceXform; |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 275 | TextureSampler fSamplers[1]; |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 276 | |
| 277 | typedef GrGeometryProcessor INHERITED; |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 278 | }; |
| 279 | |
Brian Salomon | 6872e94 | 2018-05-18 10:29:54 -0400 | [diff] [blame] | 280 | // This computes the four edge equations for a quad, then outsets them and computes a new quad |
| 281 | // as the intersection points of the outset edges. 'x' and 'y' contain the original points as input |
| 282 | // and the outset points as output. 'a', 'b', and 'c' are the edge equation coefficients on output. |
| 283 | static void compute_quad_edges_and_outset_vertices(Sk4f* x, Sk4f* y, Sk4f* a, Sk4f* b, Sk4f* c) { |
| 284 | static constexpr auto fma = SkNx_fma<4, float>; |
| 285 | // These rotate the points/edge values either clockwise or counterclockwise assuming tri strip |
| 286 | // order. |
| 287 | auto nextCW = [](const Sk4f& v) { return SkNx_shuffle<2, 0, 3, 1>(v); }; |
| 288 | auto nextCCW = [](const Sk4f& v) { return SkNx_shuffle<1, 3, 0, 2>(v); }; |
| 289 | |
| 290 | auto xnext = nextCCW(*x); |
| 291 | auto ynext = nextCCW(*y); |
| 292 | *a = ynext - *y; |
| 293 | *b = *x - xnext; |
| 294 | *c = fma(xnext, *y, -ynext * *x); |
| 295 | Sk4f invNormLengths = (*a * *a + *b * *b).rsqrt(); |
| 296 | // Make sure the edge equations have their normals facing into the quad in device space. |
| 297 | auto test = fma(*a, nextCW(*x), fma(*b, nextCW(*y), *c)); |
| 298 | if ((test < Sk4f(0)).anyTrue()) { |
| 299 | invNormLengths = -invNormLengths; |
| 300 | } |
| 301 | *a *= invNormLengths; |
| 302 | *b *= invNormLengths; |
| 303 | *c *= invNormLengths; |
| 304 | |
| 305 | // Here is the outset. This makes our edge equations compute coverage without requiring a |
| 306 | // half pixel offset and is also used to compute the bloated quad that will cover all |
| 307 | // pixels. |
| 308 | *c += Sk4f(0.5f); |
| 309 | |
| 310 | // Reverse the process to compute the points of the bloated quad from the edge equations. |
| 311 | // This time the inputs don't have 1s as their third coord and we want to homogenize rather |
| 312 | // than normalize. |
| 313 | auto anext = nextCW(*a); |
| 314 | auto bnext = nextCW(*b); |
| 315 | auto cnext = nextCW(*c); |
| 316 | *x = fma(bnext, *c, -*b * cnext); |
| 317 | *y = fma(*a, cnext, -anext * *c); |
| 318 | auto ic = (fma(anext, *b, -bnext * *a)).invert(); |
| 319 | *x *= ic; |
| 320 | *y *= ic; |
| 321 | } |
| 322 | |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 323 | namespace { |
| 324 | // This is a class soley so it can be partially specialized (functions cannot be). |
| 325 | template<GrAA, typename Vertex> class VertexAAHandler; |
| 326 | |
| 327 | template<typename Vertex> class VertexAAHandler<GrAA::kNo, Vertex> { |
| 328 | public: |
| 329 | static void AssignPositionsAndTexCoords(Vertex* vertices, const GrQuad& quad, |
| 330 | const SkRect& texRect) { |
| 331 | vertices[0].fPosition = quad.point(0); |
| 332 | vertices[0].fTextureCoords = {texRect.fLeft, texRect.fTop}; |
| 333 | vertices[1].fPosition = quad.point(1); |
| 334 | vertices[1].fTextureCoords = {texRect.fLeft, texRect.fBottom}; |
| 335 | vertices[2].fPosition = quad.point(2); |
| 336 | vertices[2].fTextureCoords = {texRect.fRight, texRect.fTop}; |
| 337 | vertices[3].fPosition = quad.point(3); |
| 338 | vertices[3].fTextureCoords = {texRect.fRight, texRect.fBottom}; |
| 339 | } |
| 340 | }; |
| 341 | |
| 342 | template<typename Vertex> class VertexAAHandler<GrAA::kYes, Vertex> { |
| 343 | public: |
| 344 | static void AssignPositionsAndTexCoords(Vertex* vertices, const GrQuad& quad, |
| 345 | const SkRect& texRect) { |
Brian Salomon | 6872e94 | 2018-05-18 10:29:54 -0400 | [diff] [blame] | 346 | auto x = quad.x4f(); |
| 347 | auto y = quad.y4f(); |
| 348 | Sk4f a, b, c; |
| 349 | compute_quad_edges_and_outset_vertices(&x, &y, &a, &b, &c); |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 350 | |
| 351 | for (int i = 0; i < 4; ++i) { |
Brian Salomon | 6872e94 | 2018-05-18 10:29:54 -0400 | [diff] [blame] | 352 | vertices[i].fPosition = {x[i], y[i]}; |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 353 | for (int j = 0; j < 4; ++j) { |
Brian Salomon | 6872e94 | 2018-05-18 10:29:54 -0400 | [diff] [blame] | 354 | vertices[i].fEdges[j] = {a[j], b[j], c[j]}; |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 358 | AssignTexCoords(vertices, quad, texRect); |
| 359 | } |
| 360 | |
| 361 | private: |
| 362 | static void AssignTexCoords(Vertex* vertices, const GrQuad& quad, const SkRect& tex) { |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 363 | SkMatrix q = SkMatrix::MakeAll(quad.x(0), quad.x(1), quad.x(2), |
| 364 | quad.y(0), quad.y(1), quad.y(2), |
| 365 | 1.f, 1.f, 1.f); |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 366 | SkMatrix qinv; |
| 367 | if (!q.invert(&qinv)) { |
| 368 | return; |
| 369 | } |
| 370 | SkMatrix t = SkMatrix::MakeAll(tex.fLeft, tex.fLeft, tex.fRight, |
| 371 | tex.fTop, tex.fBottom, tex.fTop, |
| 372 | 1.f, 1.f, 1.f); |
| 373 | SkMatrix map; |
| 374 | map.setConcat(t, qinv); |
| 375 | SkMatrixPriv::MapPointsWithStride(map, &vertices[0].fTextureCoords, sizeof(Vertex), |
| 376 | &vertices[0].fPosition, sizeof(Vertex), 4); |
| 377 | } |
| 378 | }; |
| 379 | |
| 380 | template <typename Vertex, bool IsMultiTex> struct TexIdAssigner; |
| 381 | |
| 382 | template <typename Vertex> struct TexIdAssigner<Vertex, true> { |
| 383 | static void Assign(Vertex* vertices, int textureIdx) { |
| 384 | vertices[0].fTextureIdx = textureIdx; |
| 385 | vertices[1].fTextureIdx = textureIdx; |
| 386 | vertices[2].fTextureIdx = textureIdx; |
| 387 | vertices[3].fTextureIdx = textureIdx; |
| 388 | } |
| 389 | }; |
| 390 | |
| 391 | template <typename Vertex> struct TexIdAssigner<Vertex, false> { |
| 392 | static void Assign(Vertex* vertices, int textureIdx) {} |
| 393 | }; |
| 394 | } // anonymous namespace |
| 395 | |
| 396 | template <typename Vertex, bool IsMultiTex, GrAA AA> |
| 397 | static void tessellate_quad(const GrQuad& devQuad, const SkRect& srcRect, GrColor color, |
| 398 | GrSurfaceOrigin origin, Vertex* vertices, SkScalar iw, SkScalar ih, |
| 399 | int textureIdx) { |
| 400 | SkRect texRect = { |
| 401 | iw * srcRect.fLeft, |
| 402 | ih * srcRect.fTop, |
| 403 | iw * srcRect.fRight, |
| 404 | ih * srcRect.fBottom |
| 405 | }; |
| 406 | if (origin == kBottomLeft_GrSurfaceOrigin) { |
| 407 | texRect.fTop = 1.f - texRect.fTop; |
| 408 | texRect.fBottom = 1.f - texRect.fBottom; |
| 409 | } |
| 410 | VertexAAHandler<AA, Vertex>::AssignPositionsAndTexCoords(vertices, devQuad, texRect); |
| 411 | vertices[0].fColor = color; |
| 412 | vertices[1].fColor = color; |
| 413 | vertices[2].fColor = color; |
| 414 | vertices[3].fColor = color; |
| 415 | TexIdAssigner<Vertex, IsMultiTex>::Assign(vertices, textureIdx); |
| 416 | } |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 417 | /** |
| 418 | * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a |
| 419 | * the texture by color. The blend with the destination is always src-over. The edges are non-AA. |
| 420 | */ |
| 421 | class TextureOp final : public GrMeshDrawOp { |
| 422 | public: |
| 423 | static std::unique_ptr<GrDrawOp> Make(sk_sp<GrTextureProxy> proxy, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 424 | GrSamplerState::Filter filter, GrColor color, |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 425 | const SkRect& srcRect, const SkRect& dstRect, |
| 426 | GrAAType aaType, const SkMatrix& viewMatrix, |
| 427 | sk_sp<GrColorSpaceXform> csxf, bool allowSRBInputs) { |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 428 | return std::unique_ptr<GrDrawOp>(new TextureOp(std::move(proxy), filter, color, srcRect, |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 429 | dstRect, aaType, viewMatrix, std::move(csxf), |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 430 | allowSRBInputs)); |
| 431 | } |
| 432 | |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 433 | ~TextureOp() override { |
| 434 | if (fFinalized) { |
| 435 | auto proxies = this->proxies(); |
| 436 | for (int i = 0; i < fProxyCnt; ++i) { |
| 437 | proxies[i]->completedRead(); |
| 438 | } |
| 439 | if (fProxyCnt > 1) { |
| 440 | delete[] reinterpret_cast<const char*>(proxies); |
| 441 | } |
| 442 | } else { |
| 443 | SkASSERT(1 == fProxyCnt); |
| 444 | fProxy0->unref(); |
| 445 | } |
| 446 | } |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 447 | |
| 448 | const char* name() const override { return "TextureOp"; } |
| 449 | |
Robert Phillips | f1748f5 | 2017-09-14 14:11:24 -0400 | [diff] [blame] | 450 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 451 | auto proxies = this->proxies(); |
| 452 | for (int i = 0; i < fProxyCnt; ++i) { |
| 453 | func(proxies[i]); |
| 454 | } |
| 455 | } |
| 456 | |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 457 | SkString dumpInfo() const override { |
| 458 | SkString str; |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 459 | str.appendf("AllowSRGBInputs: %d\n", fAllowSRGBInputs); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 460 | str.appendf("# draws: %d\n", fDraws.count()); |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 461 | auto proxies = this->proxies(); |
| 462 | for (int i = 0; i < fProxyCnt; ++i) { |
| 463 | str.appendf("Proxy ID %d: %d, Filter: %d\n", i, proxies[i]->uniqueID().asUInt(), |
| 464 | static_cast<int>(this->filters()[i])); |
| 465 | } |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 466 | for (int i = 0; i < fDraws.count(); ++i) { |
| 467 | const Draw& draw = fDraws[i]; |
| 468 | str.appendf( |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 469 | "%d: Color: 0x%08x, ProxyIdx: %d, TexRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f] " |
| 470 | "Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n", |
| 471 | i, draw.fColor, draw.fTextureIdx, draw.fSrcRect.fLeft, draw.fSrcRect.fTop, |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 472 | draw.fSrcRect.fRight, draw.fSrcRect.fBottom, draw.fQuad.point(0).fX, |
| 473 | draw.fQuad.point(0).fY, draw.fQuad.point(1).fX, draw.fQuad.point(1).fY, |
| 474 | draw.fQuad.point(2).fX, draw.fQuad.point(2).fY, draw.fQuad.point(3).fX, |
| 475 | draw.fQuad.point(3).fY); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 476 | } |
| 477 | str += INHERITED::dumpInfo(); |
| 478 | return str; |
| 479 | } |
| 480 | |
Brian Osman | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 481 | RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 482 | GrPixelConfigIsClamped dstIsClamped) override { |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 483 | SkASSERT(!fFinalized); |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 484 | SkASSERT(1 == fProxyCnt); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 485 | fFinalized = true; |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 486 | fProxy0->addPendingRead(); |
| 487 | fProxy0->unref(); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 488 | return RequiresDstTexture::kNo; |
| 489 | } |
| 490 | |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 491 | FixedFunctionFlags fixedFunctionFlags() const override { |
| 492 | return this->aaType() == GrAAType::kMSAA ? FixedFunctionFlags::kUsesHWAA |
| 493 | : FixedFunctionFlags::kNone; |
| 494 | } |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 495 | |
| 496 | DEFINE_OP_CLASS_ID |
| 497 | |
| 498 | private: |
Brian Salomon | 762d5e7 | 2017-12-01 10:25:08 -0500 | [diff] [blame] | 499 | |
| 500 | // This is used in a heursitic for choosing a code path. We don't care what happens with |
| 501 | // really large rects, infs, nans, etc. |
| 502 | #if defined(__clang__) && (__clang_major__ * 1000 + __clang_minor__) >= 3007 |
| 503 | __attribute__((no_sanitize("float-cast-overflow"))) |
| 504 | #endif |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 505 | size_t RectSizeAsSizeT(const SkRect& rect) {; |
Brian Salomon | 762d5e7 | 2017-12-01 10:25:08 -0500 | [diff] [blame] | 506 | return static_cast<size_t>(SkTMax(rect.width(), 1.f) * SkTMax(rect.height(), 1.f)); |
| 507 | } |
| 508 | |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 509 | static constexpr int kMaxTextures = TextureGeometryProcessor::kMaxTextures; |
| 510 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 511 | TextureOp(sk_sp<GrTextureProxy> proxy, GrSamplerState::Filter filter, GrColor color, |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 512 | const SkRect& srcRect, const SkRect& dstRect, GrAAType aaType, |
| 513 | const SkMatrix& viewMatrix, sk_sp<GrColorSpaceXform> csxf, bool allowSRGBInputs) |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 514 | : INHERITED(ClassID()) |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 515 | , fColorSpaceXform(std::move(csxf)) |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 516 | , fProxy0(proxy.release()) |
| 517 | , fFilter0(filter) |
| 518 | , fProxyCnt(1) |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 519 | , fAAType(static_cast<unsigned>(aaType)) |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 520 | , fFinalized(0) |
| 521 | , fAllowSRGBInputs(allowSRGBInputs ? 1 : 0) { |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 522 | SkASSERT(aaType != GrAAType::kMixedSamples); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 523 | Draw& draw = fDraws.push_back(); |
| 524 | draw.fSrcRect = srcRect; |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 525 | draw.fTextureIdx = 0; |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 526 | draw.fColor = color; |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 527 | draw.fQuad = GrQuad(dstRect, viewMatrix); |
| 528 | SkRect bounds = draw.fQuad.bounds(); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 529 | this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo); |
Brian Salomon | 762d5e7 | 2017-12-01 10:25:08 -0500 | [diff] [blame] | 530 | |
| 531 | fMaxApproxDstPixelArea = RectSizeAsSizeT(bounds); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | void onPrepareDraws(Target* target) override { |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 535 | sk_sp<GrTextureProxy> proxiesSPs[kMaxTextures]; |
| 536 | auto proxies = this->proxies(); |
| 537 | auto filters = this->filters(); |
| 538 | for (int i = 0; i < fProxyCnt; ++i) { |
| 539 | if (!proxies[i]->instantiate(target->resourceProvider())) { |
| 540 | return; |
| 541 | } |
| 542 | proxiesSPs[i] = sk_ref_sp(proxies[i]); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 543 | } |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 544 | |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 545 | bool coverageAA = GrAAType::kCoverage == this->aaType(); |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 546 | sk_sp<GrGeometryProcessor> gp = |
| 547 | TextureGeometryProcessor::Make(proxiesSPs, fProxyCnt, std::move(fColorSpaceXform), |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 548 | coverageAA, filters, *target->caps().shaderCaps()); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 549 | GrPipeline::InitArgs args; |
| 550 | args.fProxy = target->proxy(); |
| 551 | args.fCaps = &target->caps(); |
| 552 | args.fResourceProvider = target->resourceProvider(); |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 553 | args.fFlags = 0; |
| 554 | if (fAllowSRGBInputs) { |
| 555 | args.fFlags |= GrPipeline::kAllowSRGBInputs_Flag; |
| 556 | } |
| 557 | if (GrAAType::kMSAA == this->aaType()) { |
| 558 | args.fFlags |= GrPipeline::kHWAntialias_Flag; |
| 559 | } |
| 560 | |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 561 | const GrPipeline* pipeline = target->allocPipeline(args, GrProcessorSet::MakeEmptySet(), |
| 562 | target->detachAppliedClip()); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 563 | int vstart; |
| 564 | const GrBuffer* vbuffer; |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 565 | void* vdata = target->makeVertexSpace(gp->getVertexStride(), 4 * fDraws.count(), &vbuffer, |
| 566 | &vstart); |
| 567 | if (!vdata) { |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 568 | SkDebugf("Could not allocate vertices\n"); |
| 569 | return; |
| 570 | } |
Brian Salomon | 57caa66 | 2017-10-18 12:21:05 +0000 | [diff] [blame] | 571 | if (1 == fProxyCnt) { |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 572 | GrSurfaceOrigin origin = proxies[0]->origin(); |
| 573 | GrTexture* texture = proxies[0]->priv().peekTexture(); |
| 574 | float iw = 1.f / texture->width(); |
| 575 | float ih = 1.f / texture->height(); |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 576 | if (coverageAA) { |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 577 | SkASSERT(gp->getVertexStride() == sizeof(TextureGeometryProcessor::AAVertex)); |
| 578 | auto vertices = static_cast<TextureGeometryProcessor::AAVertex*>(vdata); |
| 579 | for (int i = 0; i < fDraws.count(); ++i) { |
| 580 | tessellate_quad<TextureGeometryProcessor::AAVertex, false, GrAA::kYes>( |
| 581 | fDraws[i].fQuad, fDraws[i].fSrcRect, fDraws[i].fColor, origin, |
| 582 | vertices + 4 * i, iw, ih, 0); |
Brian Salomon | 57caa66 | 2017-10-18 12:21:05 +0000 | [diff] [blame] | 583 | } |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 584 | } else { |
| 585 | SkASSERT(gp->getVertexStride() == sizeof(TextureGeometryProcessor::Vertex)); |
| 586 | auto vertices = static_cast<TextureGeometryProcessor::Vertex*>(vdata); |
| 587 | for (int i = 0; i < fDraws.count(); ++i) { |
| 588 | tessellate_quad<TextureGeometryProcessor::Vertex, false, GrAA::kNo>( |
| 589 | fDraws[i].fQuad, fDraws[i].fSrcRect, fDraws[i].fColor, origin, |
| 590 | vertices + 4 * i, iw, ih, 0); |
| 591 | } |
Brian Salomon | 57caa66 | 2017-10-18 12:21:05 +0000 | [diff] [blame] | 592 | } |
| 593 | } else { |
Brian Salomon | 57caa66 | 2017-10-18 12:21:05 +0000 | [diff] [blame] | 594 | GrTexture* textures[kMaxTextures]; |
| 595 | float iw[kMaxTextures]; |
| 596 | float ih[kMaxTextures]; |
| 597 | for (int t = 0; t < fProxyCnt; ++t) { |
| 598 | textures[t] = proxies[t]->priv().peekTexture(); |
| 599 | iw[t] = 1.f / textures[t]->width(); |
| 600 | ih[t] = 1.f / textures[t]->height(); |
| 601 | } |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 602 | if (coverageAA) { |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 603 | SkASSERT(gp->getVertexStride() == |
| 604 | sizeof(TextureGeometryProcessor::AAMultiTextureVertex)); |
| 605 | auto vertices = static_cast<TextureGeometryProcessor::AAMultiTextureVertex*>(vdata); |
| 606 | for (int i = 0; i < fDraws.count(); ++i) { |
| 607 | auto tidx = fDraws[i].fTextureIdx; |
| 608 | GrSurfaceOrigin origin = proxies[tidx]->origin(); |
| 609 | tessellate_quad<TextureGeometryProcessor::AAMultiTextureVertex, true, |
| 610 | GrAA::kYes>(fDraws[i].fQuad, fDraws[i].fSrcRect, |
| 611 | fDraws[i].fColor, origin, vertices + 4 * i, |
| 612 | iw[tidx], ih[tidx], tidx); |
Brian Salomon | 57caa66 | 2017-10-18 12:21:05 +0000 | [diff] [blame] | 613 | } |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 614 | } else { |
| 615 | SkASSERT(gp->getVertexStride() == |
| 616 | sizeof(TextureGeometryProcessor::MultiTextureVertex)); |
| 617 | auto vertices = static_cast<TextureGeometryProcessor::MultiTextureVertex*>(vdata); |
| 618 | for (int i = 0; i < fDraws.count(); ++i) { |
| 619 | auto tidx = fDraws[i].fTextureIdx; |
| 620 | GrSurfaceOrigin origin = proxies[tidx]->origin(); |
| 621 | tessellate_quad<TextureGeometryProcessor::MultiTextureVertex, true, GrAA::kNo>( |
| 622 | fDraws[i].fQuad, fDraws[i].fSrcRect, fDraws[i].fColor, origin, |
| 623 | vertices + 4 * i, iw[tidx], ih[tidx], tidx); |
| 624 | } |
Brian Salomon | 57caa66 | 2017-10-18 12:21:05 +0000 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | GrPrimitiveType primitiveType = |
| 628 | fDraws.count() > 1 ? GrPrimitiveType::kTriangles : GrPrimitiveType::kTriangleStrip; |
| 629 | GrMesh mesh(primitiveType); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 630 | if (fDraws.count() > 1) { |
Brian Salomon | 57caa66 | 2017-10-18 12:21:05 +0000 | [diff] [blame] | 631 | sk_sp<const GrBuffer> ibuffer = target->resourceProvider()->refQuadIndexBuffer(); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 632 | if (!ibuffer) { |
| 633 | SkDebugf("Could not allocate quad indices\n"); |
| 634 | return; |
| 635 | } |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 636 | mesh.setIndexedPatterned(ibuffer.get(), 6, 4, fDraws.count(), |
| 637 | GrResourceProvider::QuadCountOfQuadBuffer()); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 638 | } else { |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 639 | mesh.setNonIndexedNonInstanced(4); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 640 | } |
Brian Salomon | 57caa66 | 2017-10-18 12:21:05 +0000 | [diff] [blame] | 641 | mesh.setVertexData(vbuffer, vstart); |
| 642 | target->draw(gp.get(), pipeline, mesh); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
| 646 | const auto* that = t->cast<TextureOp>(); |
Brian Salomon | 762d5e7 | 2017-12-01 10:25:08 -0500 | [diff] [blame] | 647 | const auto& shaderCaps = *caps.shaderCaps(); |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 648 | if (!GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) { |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 649 | return false; |
| 650 | } |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 651 | if (this->aaType() != that->aaType()) { |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 652 | return false; |
| 653 | } |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 654 | // Because of an issue where GrColorSpaceXform adds the same function every time it is used |
| 655 | // in a texture lookup, we only allow multiple textures when there is no transform. |
Brian Salomon | 762d5e7 | 2017-12-01 10:25:08 -0500 | [diff] [blame] | 656 | if (TextureGeometryProcessor::SupportsMultitexture(shaderCaps) && !fColorSpaceXform && |
| 657 | fMaxApproxDstPixelArea <= shaderCaps.disableImageMultitexturingDstRectAreaThreshold() && |
| 658 | that->fMaxApproxDstPixelArea <= |
| 659 | shaderCaps.disableImageMultitexturingDstRectAreaThreshold()) { |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 660 | int map[kMaxTextures]; |
Brian Salomon | 762d5e7 | 2017-12-01 10:25:08 -0500 | [diff] [blame] | 661 | int numNewProxies = this->mergeProxies(that, map, shaderCaps); |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 662 | if (numNewProxies < 0) { |
| 663 | return false; |
| 664 | } |
| 665 | if (1 == fProxyCnt && numNewProxies) { |
| 666 | void* mem = new char[(sizeof(GrSamplerState::Filter) + sizeof(GrTextureProxy*)) * |
| 667 | kMaxTextures]; |
| 668 | auto proxies = reinterpret_cast<GrTextureProxy**>(mem); |
| 669 | auto filters = reinterpret_cast<GrSamplerState::Filter*>(proxies + kMaxTextures); |
| 670 | proxies[0] = fProxy0; |
| 671 | filters[0] = fFilter0; |
| 672 | fProxyArray = proxies; |
| 673 | } |
| 674 | fProxyCnt += numNewProxies; |
| 675 | auto thisProxies = fProxyArray; |
| 676 | auto thatProxies = that->proxies(); |
| 677 | auto thatFilters = that->filters(); |
| 678 | auto thisFilters = reinterpret_cast<GrSamplerState::Filter*>(thisProxies + |
| 679 | kMaxTextures); |
| 680 | for (int i = 0; i < that->fProxyCnt; ++i) { |
| 681 | if (map[i] < 0) { |
| 682 | thatProxies[i]->addPendingRead(); |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 683 | |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 684 | thisProxies[-map[i]] = thatProxies[i]; |
| 685 | thisFilters[-map[i]] = thatFilters[i]; |
| 686 | map[i] = -map[i]; |
| 687 | } |
| 688 | } |
| 689 | int firstNewDraw = fDraws.count(); |
| 690 | fDraws.push_back_n(that->fDraws.count(), that->fDraws.begin()); |
| 691 | for (int i = firstNewDraw; i < fDraws.count(); ++i) { |
| 692 | fDraws[i].fTextureIdx = map[fDraws[i].fTextureIdx]; |
| 693 | } |
| 694 | } else { |
Brian Salomon | bbf0575 | 2017-11-30 11:30:48 -0500 | [diff] [blame] | 695 | // We can get here when one of the ops is already multitextured but the other cannot |
| 696 | // be because of the dst rect size. |
| 697 | if (fProxyCnt > 1 || that->fProxyCnt > 1) { |
| 698 | return false; |
| 699 | } |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 700 | if (fProxy0->uniqueID() != that->fProxy0->uniqueID() || fFilter0 != that->fFilter0) { |
| 701 | return false; |
| 702 | } |
| 703 | fDraws.push_back_n(that->fDraws.count(), that->fDraws.begin()); |
| 704 | } |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 705 | this->joinBounds(*that); |
Brian Salomon | 762d5e7 | 2017-12-01 10:25:08 -0500 | [diff] [blame] | 706 | fMaxApproxDstPixelArea = SkTMax(that->fMaxApproxDstPixelArea, fMaxApproxDstPixelArea); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 707 | return true; |
| 708 | } |
| 709 | |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 710 | /** |
| 711 | * Determines a mapping of indices from that's proxy array to this's proxy array. A negative map |
| 712 | * value means that's proxy should be added to this's proxy array at the absolute value of |
| 713 | * the map entry. If it is determined that the ops shouldn't combine their proxies then a |
| 714 | * negative value is returned. Otherwise, return value indicates the number of proxies that have |
| 715 | * to be added to this op or, equivalently, the number of negative entries in map. |
| 716 | */ |
| 717 | int mergeProxies(const TextureOp* that, int map[kMaxTextures], const GrShaderCaps& caps) const { |
| 718 | std::fill_n(map, kMaxTextures, -kMaxTextures); |
| 719 | int sharedProxyCnt = 0; |
| 720 | auto thisProxies = this->proxies(); |
| 721 | auto thisFilters = this->filters(); |
| 722 | auto thatProxies = that->proxies(); |
| 723 | auto thatFilters = that->filters(); |
| 724 | for (int i = 0; i < fProxyCnt; ++i) { |
| 725 | for (int j = 0; j < that->fProxyCnt; ++j) { |
| 726 | if (thisProxies[i]->uniqueID() == thatProxies[j]->uniqueID()) { |
| 727 | if (thisFilters[i] != thatFilters[j]) { |
| 728 | // In GL we don't currently support using the same texture with different |
| 729 | // samplers. If we added support for sampler objects and a cap bit to know |
| 730 | // it's ok to use different filter modes then we could support this. |
| 731 | // Otherwise, we could also only allow a single filter mode for each op |
| 732 | // instance. |
| 733 | return -1; |
| 734 | } |
| 735 | map[j] = i; |
| 736 | ++sharedProxyCnt; |
| 737 | break; |
| 738 | } |
| 739 | } |
| 740 | } |
Brian Salomon | 2b6f614 | 2017-11-13 11:49:13 -0500 | [diff] [blame] | 741 | int actualMaxTextures = SkTMin(caps.maxFragmentSamplers(), kMaxTextures); |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 742 | int newProxyCnt = that->fProxyCnt - sharedProxyCnt; |
| 743 | if (newProxyCnt + fProxyCnt > actualMaxTextures) { |
| 744 | return -1; |
| 745 | } |
| 746 | GrPixelConfig config = thisProxies[0]->config(); |
| 747 | int nextSlot = fProxyCnt; |
| 748 | for (int j = 0; j < that->fProxyCnt; ++j) { |
| 749 | // We want to avoid making many shaders because of different permutations of shader |
| 750 | // based swizzle and sampler types. The approach taken here is to require the configs to |
| 751 | // be the same and to only allow already instantiated proxies that have the most |
| 752 | // common sampler type. Otherwise we don't merge. |
| 753 | if (thatProxies[j]->config() != config) { |
| 754 | return -1; |
| 755 | } |
| 756 | if (GrTexture* tex = thatProxies[j]->priv().peekTexture()) { |
| 757 | if (tex->texturePriv().samplerType() != kTexture2DSampler_GrSLType) { |
| 758 | return -1; |
| 759 | } |
| 760 | } |
| 761 | if (map[j] < 0) { |
| 762 | map[j] = -(nextSlot++); |
| 763 | } |
| 764 | } |
| 765 | return newProxyCnt; |
| 766 | } |
| 767 | |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 768 | GrAAType aaType() const { return static_cast<GrAAType>(fAAType); } |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 769 | |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 770 | GrTextureProxy* const* proxies() const { return fProxyCnt > 1 ? fProxyArray : &fProxy0; } |
| 771 | |
| 772 | const GrSamplerState::Filter* filters() const { |
| 773 | if (fProxyCnt > 1) { |
| 774 | return reinterpret_cast<const GrSamplerState::Filter*>(fProxyArray + kMaxTextures); |
| 775 | } |
| 776 | return &fFilter0; |
| 777 | } |
| 778 | |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 779 | struct Draw { |
| 780 | SkRect fSrcRect; |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 781 | int fTextureIdx; |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 782 | GrQuad fQuad; |
| 783 | GrColor fColor; |
| 784 | }; |
| 785 | SkSTArray<1, Draw, true> fDraws; |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 786 | sk_sp<GrColorSpaceXform> fColorSpaceXform; |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 787 | // Initially we store a single proxy ptr and a single filter. If we grow to have more than |
| 788 | // one proxy we instead store pointers to dynamically allocated arrays of size kMaxTextures |
| 789 | // followed by kMaxTextures filters. |
| 790 | union { |
| 791 | GrTextureProxy* fProxy0; |
| 792 | GrTextureProxy** fProxyArray; |
| 793 | }; |
Brian Salomon | bbf0575 | 2017-11-30 11:30:48 -0500 | [diff] [blame] | 794 | size_t fMaxApproxDstPixelArea; |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 795 | GrSamplerState::Filter fFilter0; |
| 796 | uint8_t fProxyCnt; |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 797 | unsigned fAAType : 2; |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 798 | // Used to track whether fProxy is ref'ed or has a pending IO after finalize() is called. |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 799 | unsigned fFinalized : 1; |
| 800 | unsigned fAllowSRGBInputs : 1; |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 801 | |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 802 | typedef GrMeshDrawOp INHERITED; |
| 803 | }; |
| 804 | |
Brian Salomon | 336ce7b | 2017-09-08 08:23:58 -0400 | [diff] [blame] | 805 | constexpr int TextureGeometryProcessor::kMaxTextures; |
| 806 | constexpr int TextureOp::kMaxTextures; |
| 807 | |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 808 | } // anonymous namespace |
| 809 | |
| 810 | namespace GrTextureOp { |
| 811 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 812 | std::unique_ptr<GrDrawOp> Make(sk_sp<GrTextureProxy> proxy, GrSamplerState::Filter filter, |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 813 | GrColor color, const SkRect& srcRect, const SkRect& dstRect, |
| 814 | GrAAType aaType, const SkMatrix& viewMatrix, |
| 815 | sk_sp<GrColorSpaceXform> csxf, bool allowSRGBInputs) { |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 816 | SkASSERT(!viewMatrix.hasPerspective()); |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 817 | return TextureOp::Make(std::move(proxy), filter, color, srcRect, dstRect, aaType, viewMatrix, |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 818 | std::move(csxf), allowSRGBInputs); |
| 819 | } |
| 820 | |
| 821 | } // namespace GrTextureOp |
| 822 | |
| 823 | #if GR_TEST_UTILS |
| 824 | #include "GrContext.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 825 | #include "GrContextPriv.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 826 | #include "GrProxyProvider.h" |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 827 | |
| 828 | GR_DRAW_OP_TEST_DEFINE(TextureOp) { |
| 829 | GrSurfaceDesc desc; |
| 830 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 831 | desc.fHeight = random->nextULessThan(90) + 10; |
| 832 | desc.fWidth = random->nextULessThan(90) + 10; |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 833 | auto origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin; |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 834 | SkBackingFit fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact; |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 835 | |
| 836 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 837 | sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(desc, origin, fit, SkBudgeted::kNo); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 838 | |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 839 | SkRect rect = GrTest::TestRect(random); |
| 840 | SkRect srcRect; |
| 841 | srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f); |
| 842 | srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f; |
| 843 | srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f); |
| 844 | srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f; |
| 845 | SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random); |
| 846 | GrColor color = SkColorToPremulGrColor(random->nextU()); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 847 | GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan( |
| 848 | static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 849 | auto csxf = GrTest::TestColorXform(random); |
| 850 | bool allowSRGBInputs = random->nextBool(); |
Brian Salomon | 485b8c6 | 2018-01-12 15:11:06 -0500 | [diff] [blame] | 851 | GrAAType aaType = GrAAType::kNone; |
| 852 | if (random->nextBool()) { |
| 853 | aaType = (fsaaType == GrFSAAType::kUnifiedMSAA) ? GrAAType::kMSAA : GrAAType::kCoverage; |
| 854 | } |
| 855 | return GrTextureOp::Make(std::move(proxy), filter, color, srcRect, rect, aaType, viewMatrix, |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 856 | std::move(csxf), allowSRGBInputs); |
| 857 | } |
| 858 | |
| 859 | #endif |