| 1 |
/* |
| 2 |
* Copyright 2021 Google LLC. |
| 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 |
#include "src/gpu/ganesh/tessellate/GrTessellationShader.h" |
| 8 |
|
| 9 |
#include "src/gpu/Swizzle.h" |
| 10 |
#include "src/gpu/ganesh/GrDstProxyView.h" |
| 11 |
#include "src/gpu/ganesh/GrPipeline.h" |
| 12 |
#include "src/gpu/ganesh/GrSurfaceProxyView.h" |
| 13 |
#include "src/gpu/tessellate/WangsFormula.h" |
| 14 |
|
| 15 |
#include <utility> |
| 16 |
|
| 17 |
enum class GrAAType : unsigned int; |
| 18 |
|
| 19 |
const GrPipeline* GrTessellationShader::MakePipeline(const ProgramArgs& args, |
| 20 |
GrAAType aaType, |
| 21 |
GrAppliedClip&& appliedClip, |
| 22 |
GrProcessorSet&& processors) { |
| 23 |
GrPipeline::InitArgs pipelineArgs; |
| 24 |
|
| 25 |
pipelineArgs.fCaps = args.fCaps; |
| 26 |
pipelineArgs.fDstProxyView = *args.fDstProxyView; |
| 27 |
pipelineArgs.fWriteSwizzle = args.fWriteView.swizzle(); |
| 28 |
|
| 29 |
return args.fArena->make<GrPipeline>(pipelineArgs, |
| 30 |
std::move(processors), |
| 31 |
std::move(appliedClip)); |
| 32 |
} |
| 33 |
|
| 34 |
const char* GrTessellationShader::WangsFormulaSkSL() { |
| 35 |
static_assert(skgpu::wangs_formula::length_term<3>(1) == 0.75); |
| 36 |
static_assert(skgpu::wangs_formula::length_term_p2<3>(1) == 0.5625); |
| 37 |
|
| 38 |
return |
| 39 |
// Returns the length squared of the largest forward difference from Wang's cubic formula. |
| 40 |
"float wangs_formula_max_fdiff_p2(float2 p0, float2 p1, float2 p2, float2 p3," |
| 41 |
"float2x2 matrix) {" |
| 42 |
"float2 d0 = matrix * (fma(float2(-2), p1, p2) + p0);" |
| 43 |
"float2 d1 = matrix * (fma(float2(-2), p2, p3) + p1);" |
| 44 |
"return max(dot(d0,d0), dot(d1,d1));" |
| 45 |
"}" |
| 46 |
"float wangs_formula_cubic(float _precision_, float2 p0, float2 p1, float2 p2, float2 p3," |
| 47 |
"float2x2 matrix) {" |
| 48 |
"float m = wangs_formula_max_fdiff_p2(p0, p1, p2, p3, matrix);" |
| 49 |
"return max(ceil(sqrt(0.75 * _precision_ * sqrt(m))), 1.0);" |
| 50 |
"}" |
| 51 |
"float wangs_formula_cubic_log2(float _precision_, float2 p0, float2 p1, float2 p2, float2 p3," |
| 52 |
"float2x2 matrix) {" |
| 53 |
"float m = wangs_formula_max_fdiff_p2(p0, p1, p2, p3, matrix);" |
| 54 |
"return ceil(log2(max(0.5625 * _precision_ * _precision_ * m, 1.0)) * .25);" |
| 55 |
"}" |
| 56 |
"float wangs_formula_conic_p2(float _precision_, float2 p0, float2 p1, float2 p2, float w) {" |
| 57 |
// Translate the bounding box center to the origin. |
| 58 |
"float2 C = (min(min(p0, p1), p2) + max(max(p0, p1), p2)) * 0.5;" |
| 59 |
"p0 -= C;" |
| 60 |
"p1 -= C;" |
| 61 |
"p2 -= C;" |
| 62 |
|
| 63 |
// Compute max length. |
| 64 |
"float m = sqrt(max(max(dot(p0,p0), dot(p1,p1)), dot(p2,p2)));" |
| 65 |
|
| 66 |
// Compute forward differences. |
| 67 |
"float2 dp = fma(float2(-2.0 * w), p1, p0) + p2;" |
| 68 |
"float dw = abs(fma(-2.0, w, 2.0));" |
| 69 |
|
| 70 |
// Compute numerator and denominator for parametric step size of linearization. Here, the |
| 71 |
// epsilon referenced from the cited paper is 1/precision. |
| 72 |
"float rp_minus_1 = max(0.0, fma(m, _precision_, -1.0));" |
| 73 |
"float numer = length(dp) * _precision_ + rp_minus_1 * dw;" |
| 74 |
"float denom = 4 * min(w, 1.0);" |
| 75 |
|
| 76 |
"return numer/denom;" |
| 77 |
"}" |
| 78 |
"float wangs_formula_conic(float _precision_, float2 p0, float2 p1, float2 p2, float w) {" |
| 79 |
"float n2 = wangs_formula_conic_p2(_precision_, p0, p1, p2, w);" |
| 80 |
"return max(ceil(sqrt(n2)), 1.0);" |
| 81 |
"}" |
| 82 |
"float wangs_formula_conic_log2(float _precision_, float2 p0, float2 p1, float2 p2, float w) {" |
| 83 |
"float n2 = wangs_formula_conic_p2(_precision_, p0, p1, p2, w);" |
| 84 |
"return ceil(log2(max(n2, 1.0)) * .5);" |
| 85 |
"}" |
| 86 |
; |
| 87 |
} |