diff options
Diffstat (limited to 'src/gpu/ccpr/GrCCPRCoverageProcessor_GSImpl.cpp')
-rw-r--r-- | src/gpu/ccpr/GrCCPRCoverageProcessor_GSImpl.cpp | 284 |
1 files changed, 179 insertions, 105 deletions
diff --git a/src/gpu/ccpr/GrCCPRCoverageProcessor_GSImpl.cpp b/src/gpu/ccpr/GrCCPRCoverageProcessor_GSImpl.cpp index 60928b6afc..a832f625c2 100644 --- a/src/gpu/ccpr/GrCCPRCoverageProcessor_GSImpl.cpp +++ b/src/gpu/ccpr/GrCCPRCoverageProcessor_GSImpl.cpp @@ -10,6 +10,8 @@ #include "GrMesh.h" #include "glsl/GrGLSLVertexGeoBuilder.h" +using InputType = GrGLSLGeometryBuilder::InputType; +using OutputType = GrGLSLGeometryBuilder::OutputType; using Shader = GrCCPRCoverageProcessor::Shader; /** @@ -46,9 +48,6 @@ protected: void emitGeometryShader(const GrCCPRCoverageProcessor& proc, GrGLSLVaryingHandler* varyingHandler, GrGLSLGeometryBuilder* g, const char* rtAdjust) const { - using InputType = GrGLSLGeometryBuilder::InputType; - using OutputType = GrGLSLGeometryBuilder::OutputType; - int numInputPoints = proc.numInputPoints(); SkASSERT(3 == numInputPoints || 4 == numInputPoints); @@ -83,16 +82,11 @@ protected: #endif g->defineConstant("bloat", bloat); - Shader::GeometryVars vars; - fShader->emitSetupCode(g, "pts", "sk_InvocationID", wind.c_str(), &vars); - int maxPoints = this->onEmitGeometryShader(g, wind, emitVertexFn.c_str(), vars); - g->configure(InputType::kLines, OutputType::kTriangleStrip, maxPoints, - fShader->getNumSegments()); + this->onEmitGeometryShader(g, wind, emitVertexFn.c_str()); } - virtual int onEmitGeometryShader(GrGLSLGeometryBuilder*, const GrShaderVar& wind, - const char* emitVertexFn, - const Shader::GeometryVars&) const = 0; + virtual void onEmitGeometryShader(GrGLSLGeometryBuilder*, const GrShaderVar& wind, + const char* emitVertexFn) const = 0; virtual ~GSImpl() {} @@ -101,100 +95,166 @@ protected: typedef GrGLSLGeometryProcessor INHERITED; }; -class GSHullImpl : public GrCCPRCoverageProcessor::GSImpl { +/** + * Generates a conservative raster hull around a triangle. (See comments for RenderPass) + */ +class GSHull3Impl : public GrCCPRCoverageProcessor::GSImpl { public: - GSHullImpl(std::unique_ptr<Shader> shader) : GSImpl(std::move(shader)) {} + GSHull3Impl(std::unique_ptr<Shader> shader) : GSImpl(std::move(shader)) {} - int onEmitGeometryShader(GrGLSLGeometryBuilder* g, const GrShaderVar& wind, - const char* emitVertexFn, - const Shader::GeometryVars& vars) const override { - int numSides = fShader->getNumSegments(); - SkASSERT(numSides >= 3); + void onEmitGeometryShader(GrGLSLGeometryBuilder* g, const GrShaderVar& wind, + const char* emitVertexFn) const override { + Shader::GeometryVars vars; + fShader->emitSetupCode(g, "pts", nullptr, wind.c_str(), &vars); const char* hullPts = vars.fHullVars.fAlternatePoints; if (!hullPts) { hullPts = "pts"; } - const char* midpoint = vars.fHullVars.fAlternateMidpoint; - if (!midpoint) { - g->codeAppendf("float2 midpoint = %s * float%i(%f);", hullPts, numSides, 1.0/numSides); - midpoint = "midpoint"; - } + // Visualize the input triangle as upright and equilateral, with a flat base. Paying special + // attention to wind, we can identify the points as top, bottom-left, and bottom-right. + // + // NOTE: We generate the hull in 2 independent invocations, so each invocation designates + // the corner it will begin with as the top. + g->codeAppendf("int i = %s > 0 ? sk_InvocationID : 1 - sk_InvocationID;", wind.c_str()); + g->codeAppendf("float2 top = %s[i];", hullPts); + g->codeAppendf("float2 left = %s[%s > 0 ? (1 - i) * 2 : i + 1];", hullPts, wind.c_str()); + g->codeAppendf("float2 right = %s[%s > 0 ? i + 1 : (1 - i) * 2];", hullPts, wind.c_str()); + + // Determine how much to outset the conservative raster hull from each of the three edges. + g->codeAppend ("float2 leftbloat = float2(top.y > left.y ? +bloat : -bloat, " + "top.x > left.x ? -bloat : +bloat);"); + g->codeAppend ("float2 rightbloat = float2(right.y > top.y ? +bloat : -bloat, " + "right.x > top.x ? -bloat : +bloat);"); + g->codeAppend ("float2 downbloat = float2(left.y > right.y ? +bloat : -bloat, " + "left.x > right.x ? -bloat : +bloat);"); + + // Here we generate the conservative raster geometry. It is the convex hull of 3 pixel-size + // boxes centered on the input points, split between two invocations. This translates to a + // polygon with either one, two, or three vertices at each input point, depending on how + // sharp the corner is. For more details on conservative raster, see: + // https://developer.nvidia.com/gpugems/GPUGems2/gpugems2_chapter42.html + g->codeAppendf("bool2 left_right_notequal = notEqual(leftbloat, rightbloat);"); + g->codeAppend ("if (all(left_right_notequal)) {"); + // The top corner will have three conservative raster vertices. Emit the + // middle one first to the triangle strip. + g->codeAppendf( "%s(top + float2(-leftbloat.y, leftbloat.x), 1);", emitVertexFn); + g->codeAppend ("}"); + g->codeAppend ("if (any(left_right_notequal)) {"); + // Second conservative raster vertex for the top corner. + g->codeAppendf( "%s(top + rightbloat, 1);", emitVertexFn); + g->codeAppend ("}"); - g->codeAppendf("int previdx = (sk_InvocationID + %i) %% %i, " - "nextidx = (sk_InvocationID + 1) %% %i;", - numSides - 1, numSides, numSides); - - g->codeAppendf("float2 self = %s[sk_InvocationID];" - "int leftidx = %s > 0 ? previdx : nextidx;" - "int rightidx = %s > 0 ? nextidx : previdx;", - hullPts, wind.c_str(), wind.c_str()); - - // Which quadrant does the vector from self -> right fall into? - g->codeAppendf("float2 right = %s[rightidx];", hullPts); - if (3 == numSides) { - // TODO: evaluate perf gains. - g->codeAppend ("float2 qsr = sign(right - self);"); - } else { - SkASSERT(4 == numSides); - g->codeAppendf("float2 diag = %s[(sk_InvocationID + 2) %% 4];", hullPts); - g->codeAppend ("float2 qsr = sign((right != self ? right : diag) - self);"); - } + // Main interior body of the triangle. + g->codeAppendf("%s(top + leftbloat, 1);", emitVertexFn); + g->codeAppendf("%s(right + rightbloat, 1);", emitVertexFn); + + // Here the two invocations diverge. We can't symmetrically divide three triangle points + // between two invocations, so each does the following: + // + // sk_InvocationID=0: Finishes the main interior body of the triangle. + // sk_InvocationID=1: Remaining two conservative raster vertices for the third corner. + g->codeAppendf("bool2 right_down_notequal = notEqual(rightbloat, downbloat);"); + g->codeAppend ("if (any(right_down_notequal) || 0 == sk_InvocationID) {"); + g->codeAppendf( "%s(sk_InvocationID == 0 ? left + leftbloat : right + downbloat, 1);", + emitVertexFn); + g->codeAppend ("}"); + g->codeAppend ("if (all(right_down_notequal) && 0 != sk_InvocationID) {"); + g->codeAppendf( "%s(right + float2(-rightbloat.y, rightbloat.x), 1);", emitVertexFn); + g->codeAppend ("}"); - // Which quadrant does the vector from left -> self fall into? - g->codeAppendf("float2 qls = sign(self - %s[leftidx]);", hullPts); - - // d2 just helps us reduce triangle counts with orthogonal, axis-aligned lines. - // TODO: evaluate perf gains. - const char* dr2 = "dr"; - if (3 == numSides) { - // TODO: evaluate perf gains. - g->codeAppend ("float2 dr = float2(qsr.y != 0 ? +qsr.y : +qsr.x, " - "qsr.x != 0 ? -qsr.x : +qsr.y);"); - g->codeAppend ("float2 dr2 = float2(qsr.y != 0 ? +qsr.y : -qsr.x, " - "qsr.x != 0 ? -qsr.x : -qsr.y);"); - g->codeAppend ("float2 dl = float2(qls.y != 0 ? +qls.y : +qls.x, " - "qls.x != 0 ? -qls.x : +qls.y);"); - dr2 = "dr2"; - } else { - g->codeAppend ("float2 dr = float2(qsr.y != 0 ? +qsr.y : 1, " - "qsr.x != 0 ? -qsr.x : 1);"); - g->codeAppend ("float2 dl = (qls == float2(0)) ? dr : " - "float2(qls.y != 0 ? +qls.y : 1, qls.x != 0 ? -qls.x : 1);"); + g->configure(InputType::kLines, OutputType::kTriangleStrip, 6, 2); + } +}; + +/** + * Generates a conservative raster hull around a convex quadrilateral. (See comments for RenderPass) + */ +class GSHull4Impl : public GrCCPRCoverageProcessor::GSImpl { +public: + GSHull4Impl(std::unique_ptr<Shader> shader) : GSImpl(std::move(shader)) {} + + void onEmitGeometryShader(GrGLSLGeometryBuilder* g, const GrShaderVar& wind, + const char* emitVertexFn) const override { + Shader::GeometryVars vars; + fShader->emitSetupCode(g, "pts", nullptr, wind.c_str(), &vars); + + const char* hullPts = vars.fHullVars.fAlternatePoints; + if (!hullPts) { + hullPts = "pts"; } - g->codeAppendf("bool2 dnotequal = notEqual(%s, dl);", dr2); - - // Emit one third of what is the convex hull of pixel-size boxes centered on the vertices. - // Each invocation emits a different third. - g->codeAppendf("%s(right + bloat * dr, 1);", emitVertexFn); - g->codeAppendf("%s(%s, 1);", emitVertexFn, midpoint); - g->codeAppendf("%s(self + bloat * %s, 1);", emitVertexFn, dr2); - g->codeAppend ("if (any(dnotequal)) {"); - g->codeAppendf( "%s(self + bloat * dl, 1);", emitVertexFn); + + // Visualize the input (convex) quadrilateral as a square. Paying special attention to wind, + // we can identify the points by their corresponding corner. + // + // NOTE: We split the square down the diagonal from top-right to bottom-left, and generate + // the hull in two independent invocations. Each invocation designates the corner it will + // begin with as top-left. + g->codeAppend ("int i = sk_InvocationID * 2;"); + g->codeAppendf("float2 topleft = %s[i];", hullPts); + g->codeAppendf("float2 topright = %s[%s > 0 ? i + 1 : 3 - i];", hullPts, wind.c_str()); + g->codeAppendf("float2 bottomleft = %s[%s > 0 ? 3 - i : i + 1];", hullPts, wind.c_str()); + g->codeAppendf("float2 bottomright = %s[2 - i];", hullPts); + + // Determine how much to outset the conservative raster hull from the relevant edges. + g->codeAppend ("float2 leftbloat = float2(topleft.y > bottomleft.y ? +bloat : -bloat, " + "topleft.x > bottomleft.x ? -bloat : bloat);"); + g->codeAppend ("float2 upbloat = float2(topright.y > topleft.y ? +bloat : -bloat, " + "topright.x > topleft.x ? -bloat : +bloat);"); + g->codeAppend ("float2 rightbloat = float2(bottomright.y > topright.y ? +bloat : -bloat, " + "bottomright.x > topright.x ? -bloat : +bloat);"); + + // Here we generate the conservative raster geometry. It is the convex hull of 4 pixel-size + // boxes centered on the input points, split evenly between two invocations. This translates + // to a polygon with either one, two, or three vertices at each input point, depending on + // how sharp the corner is. For more details on conservative raster, see: + // https://developer.nvidia.com/gpugems/GPUGems2/gpugems2_chapter42.html + g->codeAppendf("bool2 left_up_notequal = notEqual(leftbloat, upbloat);"); + g->codeAppend ("if (all(left_up_notequal)) {"); + // The top-left corner will have three conservative raster vertices. + // Emit the middle one first to the triangle strip. + g->codeAppendf( "%s(topleft + float2(-leftbloat.y, leftbloat.x), 1);", + emitVertexFn); + g->codeAppend ("}"); + g->codeAppend ("if (any(left_up_notequal)) {"); + // Second conservative raster vertex for the top-left corner. + g->codeAppendf( "%s(topleft + leftbloat, 1);", emitVertexFn); + g->codeAppend ("}"); + + // Main interior body of this invocation's half of the hull. + g->codeAppendf("%s(topleft + upbloat, 1);", emitVertexFn); + g->codeAppendf("%s(bottomleft + leftbloat, 1);", emitVertexFn); + g->codeAppendf("%s(topright + upbloat, 1);", emitVertexFn); + + // Remaining two conservative raster vertices for the top-right corner. + g->codeAppendf("bool2 up_right_notequal = notEqual(upbloat, rightbloat);"); + g->codeAppend ("if (any(up_right_notequal)) {"); + g->codeAppendf( "%s(topright + rightbloat, 1);", emitVertexFn); g->codeAppend ("}"); - g->codeAppend ("if (all(dnotequal)) {"); - g->codeAppendf( "%s(self + bloat * float2(-dl.y, dl.x), 1);", emitVertexFn); + g->codeAppend ("if (all(up_right_notequal)) {"); + g->codeAppendf( "%s(topright + float2(-upbloat.y, upbloat.x), 1);", + emitVertexFn); g->codeAppend ("}"); - g->endPrimitive(); - return 5; + g->configure(InputType::kLines, OutputType::kTriangleStrip, 7, 2); } }; +/** + * Generates conservatives around each edge of a triangle. (See comments for RenderPass) + */ class GSEdgeImpl : public GrCCPRCoverageProcessor::GSImpl { public: GSEdgeImpl(std::unique_ptr<Shader> shader) : GSImpl(std::move(shader)) {} - int onEmitGeometryShader(GrGLSLGeometryBuilder* g, const GrShaderVar& wind, - const char* emitVertexFn, - const Shader::GeometryVars&) const override { - int numSides = fShader->getNumSegments(); + void onEmitGeometryShader(GrGLSLGeometryBuilder* g, const GrShaderVar& wind, + const char* emitVertexFn) const override { + fShader->emitSetupCode(g, "pts", "sk_InvocationID", wind.c_str(), nullptr); - g->codeAppendf("int nextidx = (sk_InvocationID + 1) %% %i;", numSides); - g->codeAppendf("float2 left = pts[%s > 0 ? sk_InvocationID : nextidx], " - "right = pts[%s > 0 ? nextidx : sk_InvocationID];", - wind.c_str(), wind.c_str()); + g->codeAppend ("int nextidx = 2 != sk_InvocationID ? sk_InvocationID + 1 : 0;"); + g->codeAppendf("float2 left = pts[%s > 0 ? sk_InvocationID : nextidx];", wind.c_str()); + g->codeAppendf("float2 right = pts[%s > 0 ? nextidx : sk_InvocationID];", wind.c_str()); Shader::EmitEdgeDistanceEquation(g, "left", "right", "float3 edge_distance_equation"); @@ -225,19 +285,24 @@ public: g->codeAppend ("if (!aligned) {"); g->codeAppendf( "%s(outer_pts[1], outer_coverage[1]);", emitVertexFn); g->codeAppend ("}"); - g->endPrimitive(); - return 6; + g->configure(InputType::kLines, OutputType::kTriangleStrip, 6, 3); } }; +/** + * Generates conservatives around corners. (See comments for RenderPass) + */ class GSCornerImpl : public GrCCPRCoverageProcessor::GSImpl { public: - GSCornerImpl(std::unique_ptr<Shader> shader) : GSImpl(std::move(shader)) {} + GSCornerImpl(std::unique_ptr<Shader> shader, int numCorners) + : GSImpl(std::move(shader)), fNumCorners(numCorners) {} + + void onEmitGeometryShader(GrGLSLGeometryBuilder* g, const GrShaderVar& wind, + const char* emitVertexFn) const override { + Shader::GeometryVars vars; + fShader->emitSetupCode(g, "pts", "sk_InvocationID", wind.c_str(), &vars); - int onEmitGeometryShader(GrGLSLGeometryBuilder* g, const GrShaderVar& wind, - const char* emitVertexFn, - const Shader::GeometryVars& vars) const override { const char* corner = vars.fCornerVars.fPoint; SkASSERT(corner); @@ -245,10 +310,12 @@ public: g->codeAppendf("%s(%s + float2(-bloat, +bloat), 1);", emitVertexFn, corner); g->codeAppendf("%s(%s + float2(+bloat, -bloat), 1);", emitVertexFn, corner); g->codeAppendf("%s(%s + float2(+bloat, +bloat), 1);", emitVertexFn, corner); - g->endPrimitive(); - return 4; + g->configure(InputType::kLines, OutputType::kTriangleStrip, 4, fNumCorners); } + +private: + const int fNumCorners; }; void GrCCPRCoverageProcessor::initGS() { @@ -262,21 +329,8 @@ void GrCCPRCoverageProcessor::initGS() { this->setWillUseGeoShader(); } -GrGLSLPrimitiveProcessor* GrCCPRCoverageProcessor::CreateGSImpl(std::unique_ptr<Shader> shader) { - switch (shader->getGeometryType()) { - case Shader::GeometryType::kHull: - return new GSHullImpl(std::move(shader)); - case Shader::GeometryType::kEdges: - return new GSEdgeImpl(std::move(shader)); - case Shader::GeometryType::kCorners: - return new GSCornerImpl(std::move(shader)); - } - SK_ABORT("Unexpected Shader::GeometryType."); - return nullptr; -} - void GrCCPRCoverageProcessor::appendGSMesh(GrBuffer* instanceBuffer, int instanceCount, - int baseInstance, SkTArray<GrMesh, true>* out) { + int baseInstance, SkTArray<GrMesh, true>* out) const { // GSImpl doesn't actually make instanced draw calls. Instead, we feed transposed x,y point // values to the GPU in a regular vertex array and draw kLines (see initGS). Then, each vertex // invocation receives either the shape's x or y values as inputs, which it forwards to the @@ -285,3 +339,23 @@ void GrCCPRCoverageProcessor::appendGSMesh(GrBuffer* instanceBuffer, int instanc mesh.setNonIndexedNonInstanced(instanceCount * 2); mesh.setVertexData(instanceBuffer, baseInstance * 2); } + +GrGLSLPrimitiveProcessor* +GrCCPRCoverageProcessor::createGSImpl(std::unique_ptr<Shader> shader) const { + switch (fRenderPass) { + case RenderPass::kTriangleHulls: + return new GSHull3Impl(std::move(shader)); + case RenderPass::kQuadraticHulls: + case RenderPass::kCubicHulls: + return new GSHull4Impl(std::move(shader)); + case RenderPass::kTriangleEdges: + return new GSEdgeImpl(std::move(shader)); + case RenderPass::kTriangleCorners: + return new GSCornerImpl(std::move(shader), 3); + case RenderPass::kQuadraticCorners: + case RenderPass::kCubicCorners: + return new GSCornerImpl(std::move(shader), 2); + } + SK_ABORT("Invalid RenderPass"); + return nullptr; +} |