1 /* 2 * Copyright 2018 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 "GrCCConicShader.h" 9 10 #include "glsl/GrGLSLFragmentShaderBuilder.h" 11 #include "glsl/GrGLSLVertexGeoBuilder.h" 12 13 void GrCCConicShader::emitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts, const char* wind, 14 const char** outHull4) const { 15 // K is distance from the line P2 -> P0. L is distance from the line P0 -> P1, scaled by 2w. 16 // M is distance from the line P1 -> P2, scaled by 2w. We do this in a space where P1=0. 17 s->declareGlobal(fKLMMatrix); 18 s->codeAppendf("float x0 = %s[0].x - %s[1].x, x2 = %s[2].x - %s[1].x;", pts, pts, pts, pts); 19 s->codeAppendf("float y0 = %s[0].y - %s[1].y, y2 = %s[2].y - %s[1].y;", pts, pts, pts, pts); 20 s->codeAppendf("float w = %s[3].x;", pts); 21 s->codeAppendf("%s = float3x3(y2 - y0, x0 - x2, x2*y0 - x0*y2, " 22 "2*w * float2(+y0, -x0), 0, " 23 "2*w * float2(-y2, +x2), 0);", fKLMMatrix.c_str()); 24 25 s->declareGlobal(fControlPoint); 26 s->codeAppendf("%s = %s[1];", fControlPoint.c_str(), pts); 27 28 // Scale KLM by the inverse Manhattan width of K. This allows K to double as the flat opposite 29 // edge AA. kwidth will not be 0 because we cull degenerate conics on the CPU. 30 s->codeAppendf("float kwidth = 2*bloat * %s * (abs(%s[0].x) + abs(%s[0].y));", 31 wind, fKLMMatrix.c_str(), fKLMMatrix.c_str()); 32 s->codeAppendf("%s *= 1/kwidth;", fKLMMatrix.c_str()); 33 34 if (outHull4) { 35 // Clip the conic triangle by the tangent line at maximum height. Conics have the nice 36 // property that maximum height always occurs at T=.5. This is a simple application for 37 // De Casteljau's algorithm. 38 s->codeAppendf("float2 p1w = %s[1]*w;", pts); 39 s->codeAppend ("float r = 1 / (1 + w);"); 40 s->codeAppend ("float2 conic_hull[4];"); 41 s->codeAppendf("conic_hull[0] = %s[0];", pts); 42 s->codeAppendf("conic_hull[1] = (%s[0] + p1w) * r;", pts); 43 s->codeAppendf("conic_hull[2] = (p1w + %s[2]) * r;", pts); 44 s->codeAppendf("conic_hull[3] = %s[2];", pts); 45 *outHull4 = "conic_hull"; 46 } 47 } 48 49 void GrCCConicShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, 50 GrGLSLVarying::Scope scope, SkString* code, 51 const char* position, const char* coverage, 52 const char* cornerCoverage) { 53 fKLM_fWind.reset(kFloat4_GrSLType, scope); 54 varyingHandler->addVarying("klm_and_wind", &fKLM_fWind); 55 code->appendf("float3 klm = float3(%s - %s, 1) * %s;", 56 position, fControlPoint.c_str(), fKLMMatrix.c_str()); 57 code->appendf("%s.xyz = klm;", OutName(fKLM_fWind)); 58 code->appendf("%s.w = %s;", OutName(fKLM_fWind), coverage); // coverage == wind. 59 60 fGrad_fCorner.reset(cornerCoverage ? kFloat4_GrSLType : kFloat2_GrSLType, scope); 61 varyingHandler->addVarying(cornerCoverage ? "grad_and_corner" : "grad", &fGrad_fCorner); 62 code->appendf("%s.xy = 2*bloat * (float3x2(%s) * float3(2*klm[0], -klm[2], -klm[1]));", 63 OutName(fGrad_fCorner), fKLMMatrix.c_str()); 64 65 if (cornerCoverage) { 66 code->appendf("half hull_coverage;"); 67 this->calcHullCoverage(code, "klm", OutName(fGrad_fCorner), "hull_coverage"); 68 code->appendf("%s.zw = half2(hull_coverage, 1) * %s;", 69 OutName(fGrad_fCorner), cornerCoverage); 70 } 71 } 72 73 void GrCCConicShader::onEmitFragmentCode(GrGLSLFPFragmentBuilder* f, 74 const char* outputCoverage) const { 75 this->calcHullCoverage(&AccessCodeString(f), fKLM_fWind.fsIn(), fGrad_fCorner.fsIn(), 76 outputCoverage); 77 f->codeAppendf("%s *= %s.w;", outputCoverage, fKLM_fWind.fsIn()); // Wind. 78 79 if (kFloat4_GrSLType == fGrad_fCorner.type()) { 80 f->codeAppendf("%s = %s.z * %s.w + %s;", // Attenuated corner coverage. 81 outputCoverage, fGrad_fCorner.fsIn(), fGrad_fCorner.fsIn(), 82 outputCoverage); 83 } 84 } 85 86 void GrCCConicShader::calcHullCoverage(SkString* code, const char* klm, const char* grad, 87 const char* outputCoverage) const { 88 code->appendf("float k = %s.x, l = %s.y, m = %s.z;", klm, klm, klm); 89 code->append ("float f = k*k - l*m;"); 90 code->appendf("float fwidth = abs(%s.x) + abs(%s.y);", grad, grad); 91 code->appendf("%s = min(0.5 - f/fwidth, 1);", outputCoverage); // Curve coverage. 92 code->append ("half d = min(k - 0.5, 0);"); // K doubles as the flat opposite edge's AA. 93 code->appendf("%s = max(%s + d, 0);", outputCoverage, outputCoverage); // Total hull coverage. 94 } 95