blob: 032d177c3bd328d27a428ba556d69e0733cfc2b1 [file] [log] [blame]
Chris Dalton1a325d22017-07-14 15:17:41 -06001/*
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
Chris Dalton383a2ef2018-01-08 17:21:41 -05008#include "GrCCCoverageProcessor.h"
Chris Dalton1a325d22017-07-14 15:17:41 -06009
Chris Dalton6a3dbee2017-10-16 10:44:41 -060010#include "SkMakeUnique.h"
Chris Dalton383a2ef2018-01-08 17:21:41 -050011#include "ccpr/GrCCCubicShader.h"
12#include "ccpr/GrCCQuadraticShader.h"
13#include "ccpr/GrCCTriangleShader.h"
Chris Dalton90e8fb12017-12-22 02:24:53 -070014#include "glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060015#include "glsl/GrGLSLFragmentShaderBuilder.h"
Chris Dalton1fbdb612017-12-12 12:48:47 -070016#include "glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060017
Chris Dalton383a2ef2018-01-08 17:21:41 -050018void GrCCCoverageProcessor::Shader::emitVaryings(GrGLSLVaryingHandler* varyingHandler,
19 GrGLSLVarying::Scope scope, SkString* code,
20 const char* position, const char* coverage,
21 const char* wind) {
Chris Dalton90e8fb12017-12-22 02:24:53 -070022 SkASSERT(GrGLSLVarying::Scope::kVertToGeo != scope);
23 WindHandling windHandling = this->onEmitVaryings(varyingHandler, scope, code, position,
24 coverage, wind);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060025 if (WindHandling::kNotHandled == windHandling) {
Chris Dalton90e8fb12017-12-22 02:24:53 -070026 fWind.reset(kHalf_GrSLType, scope);
Chris Daltonfdde34e2017-10-16 14:15:26 -060027 varyingHandler->addFlatVarying("wind", &fWind);
Chris Dalton90e8fb12017-12-22 02:24:53 -070028 code->appendf("%s = %s;", OutName(fWind), wind);
Chris Dalton1a325d22017-07-14 15:17:41 -060029 }
Eric Borend6365e52017-10-16 12:31:14 +000030}
31
Chris Dalton383a2ef2018-01-08 17:21:41 -050032void GrCCCoverageProcessor::Shader::emitFragmentCode(const GrCCCoverageProcessor& proc,
Robert Phillips7f861922018-01-30 13:13:42 +000033 GrGLSLPPFragmentBuilder* f,
Chris Dalton383a2ef2018-01-08 17:21:41 -050034 const char* skOutputColor,
35 const char* skOutputCoverage) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -060036 f->codeAppendf("half coverage = 0;");
37 this->onEmitFragmentCode(f, "coverage");
38 if (fWind.fsIn()) {
39 f->codeAppendf("%s.a = coverage * %s;", skOutputColor, fWind.fsIn());
40 } else {
41 f->codeAppendf("%s.a = coverage;", skOutputColor);
Eric Borend6365e52017-10-16 12:31:14 +000042 }
Chris Dalton6a3dbee2017-10-16 10:44:41 -060043 f->codeAppendf("%s = half4(1);", skOutputCoverage);
Chris Dalton1a325d22017-07-14 15:17:41 -060044#ifdef SK_DEBUG
Chris Daltona640c492017-09-11 22:04:03 -070045 if (proc.debugVisualizationsEnabled()) {
Chris Dalton6a3dbee2017-10-16 10:44:41 -060046 f->codeAppendf("%s = half4(-%s.a, %s.a, 0, 1);",
47 skOutputColor, skOutputColor, skOutputColor);
Eric Borend6365e52017-10-16 12:31:14 +000048 }
49#endif
Eric Borend6365e52017-10-16 12:31:14 +000050}
51
Chris Dalton383a2ef2018-01-08 17:21:41 -050052void GrCCCoverageProcessor::Shader::EmitEdgeDistanceEquation(GrGLSLVertexGeoBuilder* s,
53 const char* leftPt,
54 const char* rightPt,
55 const char* outputDistanceEquation) {
Chris Dalton6a3dbee2017-10-16 10:44:41 -060056 s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);",
Eric Borend6365e52017-10-16 12:31:14 +000057 rightPt, leftPt, leftPt, rightPt);
Chris Daltoncc0ab7e2017-10-24 14:16:52 -060058 s->codeAppend ("float nwidth = (abs(n.x) + abs(n.y)) * (bloat * 2);");
59 // When nwidth=0, wind must also be 0 (and coverage * wind = 0). So it doesn't matter what we
60 // come up with here as long as it isn't NaN or Inf.
61 s->codeAppend ("n /= (0 != nwidth) ? nwidth : 1;");
62 s->codeAppendf("%s = float3(-n, dot(n, %s) - .5);", outputDistanceEquation, leftPt);
Eric Borend6365e52017-10-16 12:31:14 +000063}
64
Robert Phillips7f861922018-01-30 13:13:42 +000065int GrCCCoverageProcessor::Shader::DefineSoftSampleLocations(GrGLSLPPFragmentBuilder* f,
Chris Dalton383a2ef2018-01-08 17:21:41 -050066 const char* samplesName) {
Chris Dalton1a325d22017-07-14 15:17:41 -060067 // Standard DX11 sample locations.
68#if defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS)
Ethan Nicholas8aa45692017-09-20 11:24:15 -040069 f->defineConstant("float2[8]", samplesName, "float2[8]("
70 "float2(+1, -3)/16, float2(-1, +3)/16, float2(+5, +1)/16, float2(-3, -5)/16, "
71 "float2(-5, +5)/16, float2(-7, -1)/16, float2(+3, +7)/16, float2(+7, -7)/16."
Chris Dalton1a325d22017-07-14 15:17:41 -060072 ")");
73 return 8;
74#else
Ethan Nicholas8aa45692017-09-20 11:24:15 -040075 f->defineConstant("float2[16]", samplesName, "float2[16]("
76 "float2(+1, +1)/16, float2(-1, -3)/16, float2(-3, +2)/16, float2(+4, -1)/16, "
77 "float2(-5, -2)/16, float2(+2, +5)/16, float2(+5, +3)/16, float2(+3, -5)/16, "
78 "float2(-2, +6)/16, float2( 0, -7)/16, float2(-4, -6)/16, float2(-6, +4)/16, "
79 "float2(-8, 0)/16, float2(+7, -4)/16, float2(+6, +7)/16, float2(-7, -8)/16."
Chris Dalton1a325d22017-07-14 15:17:41 -060080 ")");
81 return 16;
82#endif
83}
84
Chris Dalton383a2ef2018-01-08 17:21:41 -050085void GrCCCoverageProcessor::getGLSLProcessorKey(const GrShaderCaps&,
86 GrProcessorKeyBuilder* b) const {
Chris Dalton90e8fb12017-12-22 02:24:53 -070087 int key = (int)fRenderPass << 1;
88 if (Impl::kGeometryShader == fImpl) {
89 key |= 1;
90 }
91#ifdef SK_DEBUG
92 uint32_t bloatBits;
93 memcpy(&bloatBits, &fDebugBloat, 4);
94 b->add32(bloatBits);
95#endif
96 b->add32(key);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060097}
98
Chris Dalton383a2ef2018-01-08 17:21:41 -050099GrGLSLPrimitiveProcessor* GrCCCoverageProcessor::createGLSLInstance(const GrShaderCaps&) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600100 std::unique_ptr<Shader> shader;
101 switch (fRenderPass) {
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600102 case RenderPass::kTriangleHulls:
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600103 case RenderPass::kTriangleEdges:
Chris Dalton383a2ef2018-01-08 17:21:41 -0500104 shader = skstd::make_unique<GrCCTriangleShader>();
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600105 break;
106 case RenderPass::kTriangleCorners:
Chris Dalton383a2ef2018-01-08 17:21:41 -0500107 shader = skstd::make_unique<GrCCTriangleCornerShader>();
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600108 break;
109 case RenderPass::kQuadraticHulls:
Chris Dalton383a2ef2018-01-08 17:21:41 -0500110 shader = skstd::make_unique<GrCCQuadraticHullShader>();
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600111 break;
112 case RenderPass::kQuadraticCorners:
Chris Dalton383a2ef2018-01-08 17:21:41 -0500113 shader = skstd::make_unique<GrCCQuadraticCornerShader>();
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600114 break;
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700115 case RenderPass::kCubicHulls:
Chris Dalton383a2ef2018-01-08 17:21:41 -0500116 shader = skstd::make_unique<GrCCCubicHullShader>();
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600117 break;
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700118 case RenderPass::kCubicCorners:
Chris Dalton383a2ef2018-01-08 17:21:41 -0500119 shader = skstd::make_unique<GrCCCubicCornerShader>();
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600120 break;
121 }
Chris Dalton90e8fb12017-12-22 02:24:53 -0700122 return Impl::kGeometryShader == fImpl ? this->createGSImpl(std::move(shader))
123 : this->createVSImpl(std::move(shader));
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600124}