Add a 2d cross product intrinsic to sksl
Change-Id: Iebaf4616665547d6ca4900e1247d5b68e0f3512a Reviewed-on: https://skia-review.googlesource.com/c/skia/+/321790 Commit-Queue: Chris Dalton <csmartdalton@google.com> Reviewed-by: Brian Osman <brianosman@google.com>
This commit is contained in:
parent
439af79f26
commit
57c37ad0e4
@ -264,6 +264,7 @@ tests_sources = [
|
||||
"$_tests/SkRemoteGlyphCacheTest.cpp",
|
||||
"$_tests/SkResourceCacheTest.cpp",
|
||||
"$_tests/SkRuntimeEffectTest.cpp",
|
||||
"$_tests/SkSLCross.cpp",
|
||||
"$_tests/SkSLGLSLTestbed.cpp",
|
||||
"$_tests/SkSLInterpreterTest.cpp",
|
||||
"$_tests/SkSLMemoryLayoutTest.cpp",
|
||||
|
@ -223,12 +223,12 @@ private:
|
||||
float2x2 derivative2AtChops = transpose(float2x2(3*chopT, 1,1) * float2x2(C));
|
||||
if (chopT[0] == roots[0] || chopT[0] == roots[1]) {
|
||||
// Point midtangent in the same direction that the curve is turning at chopT[0].
|
||||
float midtangentTurn = determinant(float2x2(midtangent, derivative2AtChops[0]));
|
||||
float midtangentTurn = cross(midtangent, derivative2AtChops[0]);
|
||||
innerTan0 = (midtangentTurn * inflectSignAtInf >= 0) ? +midtangent : -midtangent;
|
||||
}
|
||||
if (chopT[1] == roots[0] || chopT[1] == roots[1]) {
|
||||
// Point midtangent in the same direction that the curve is turning at chopT[1].
|
||||
float midtangentTurn = determinant(float2x2(midtangent, derivative2AtChops[1]));
|
||||
float midtangentTurn = cross(midtangent, derivative2AtChops[1]);
|
||||
innerTan1 = (midtangentTurn * inflectSignAtInf >= 0) ? +midtangent : -midtangent;
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because one or more lines are too long
@ -575,3 +575,8 @@ half4 unpremul(half4 color) { return half4(color.rgb / max(color.a, 0.00
|
||||
float4 unpremul_float(float4 color) { return float4(color.rgb / max(color.a, 0.0001), color.a); }
|
||||
|
||||
float2 proj(float3 p) { return p.xy / p.z; }
|
||||
|
||||
// Implement cross() as a determinant to communicate our intent more clearly to the compiler.
|
||||
// NOTE: Due to precision issues, it might be the case that cross(a,a) != 0.
|
||||
float cross(float2 a, float2 b) { return determinant(float2x2(a,b)); }
|
||||
half cross(half2 a, half2 b) { return determinant(half2x2(a,b)); }
|
||||
|
112
tests/SkSLCross.cpp
Normal file
112
tests/SkSLCross.cpp
Normal file
@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright 2020 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "tests/Test.h"
|
||||
|
||||
#include "src/gpu/GrRenderTargetContext.h"
|
||||
#include "src/gpu/GrRenderTargetContextPriv.h"
|
||||
#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
|
||||
|
||||
static void run_test(skiatest::Reporter*, GrDirectContext*, GrRenderTargetContext*, SkVector a,
|
||||
SkVector b, float expectedCrossProduct);
|
||||
|
||||
// This is a GPU test that ensures the SkSL 2d cross() intrinsic returns the correct sign (negative,
|
||||
// positive, or zero).
|
||||
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkSLCross, reporter, ctxInfo) {
|
||||
GrDirectContext* directContext = ctxInfo.directContext();
|
||||
auto rtc = GrRenderTargetContext::Make(directContext, GrColorType::kRGBA_8888, nullptr,
|
||||
SkBackingFit::kExact, {1, 1});
|
||||
if (!rtc) {
|
||||
ERRORF(reporter, "could not create render target context.");
|
||||
return;
|
||||
}
|
||||
run_test(reporter, directContext, rtc.get(), {3,4}, {5,6}, -2); // Negative.
|
||||
run_test(reporter, directContext, rtc.get(), {3,4}, {-5,-6}, 2); // Positive.
|
||||
run_test(reporter, directContext, rtc.get(), {0, 2.287f}, {0, -7.741f}, 0); // Zero.
|
||||
run_test(reporter, directContext, rtc.get(), {62.17f, 0}, {-43.49f, 0}, 0); // Zero.
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
// Outputs:
|
||||
// Green if cross(a,b) > 0
|
||||
// Red if cross(a,b) < 0
|
||||
// Black if cross(a,b) == 0
|
||||
class VisualizeCrossProductSignFP : public GrFragmentProcessor {
|
||||
public:
|
||||
VisualizeCrossProductSignFP(SkVector a, SkVector b)
|
||||
: GrFragmentProcessor(kTestFP_ClassID, kPreservesOpaqueInput_OptimizationFlag)
|
||||
, fA(a), fB(b) {
|
||||
}
|
||||
|
||||
private:
|
||||
const char* name() const override { return "VisualizeCrossProductSignFP"; }
|
||||
std::unique_ptr<GrFragmentProcessor> clone() const override {
|
||||
return std::unique_ptr<GrFragmentProcessor>(new VisualizeCrossProductSignFP(fA, fB));
|
||||
}
|
||||
void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
|
||||
bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
|
||||
|
||||
class Impl : public GrGLSLFragmentProcessor {
|
||||
void emitCode(EmitArgs& args) override {
|
||||
auto& fp = args.fFp.cast<VisualizeCrossProductSignFP>();
|
||||
const char* a, *b;
|
||||
fAUniform = args.fUniformHandler->addUniform(&fp, kFragment_GrShaderFlag,
|
||||
GrSLType::kFloat2_GrSLType, "a", &a);
|
||||
fBUniform = args.fUniformHandler->addUniform(&fp, kFragment_GrShaderFlag,
|
||||
GrSLType::kFloat2_GrSLType, "b", &b);
|
||||
args.fFragBuilder->codeAppendf(R"(
|
||||
float crossProduct = cross(%s, %s);
|
||||
float2 visualization = clamp(float2(-sign(crossProduct), sign(crossProduct)),
|
||||
float2(0), float2(1));
|
||||
%s = half4(half2(visualization), 0, 1);)", a, b, args.fOutputColor);
|
||||
}
|
||||
void onSetData(const GrGLSLProgramDataManager& pdman,
|
||||
const GrFragmentProcessor& processor) override {
|
||||
const auto& fp = processor.cast<VisualizeCrossProductSignFP>();
|
||||
pdman.set2f(fAUniform, fp.fA.x(), fp.fA.y());
|
||||
pdman.set2f(fBUniform, fp.fB.x(), fp.fB.y());
|
||||
}
|
||||
GrGLSLUniformHandler::UniformHandle fAUniform;
|
||||
GrGLSLUniformHandler::UniformHandle fBUniform;
|
||||
};
|
||||
|
||||
GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new Impl; }
|
||||
const SkVector fA, fB;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
static void run_test(skiatest::Reporter* reporter, GrDirectContext* directContext,
|
||||
GrRenderTargetContext* rtc, SkVector a, SkVector b,
|
||||
float expectedCrossProduct) {
|
||||
SkASSERT(rtc->width() == 1);
|
||||
SkASSERT(rtc->height() == 1);
|
||||
|
||||
rtc->clear(SkPMColor4f::FromBytes_RGBA(0xbaaaaaad));
|
||||
|
||||
GrPaint crossPaint;
|
||||
crossPaint.setColor4f(SK_PMColor4fWHITE);
|
||||
crossPaint.setPorterDuffXPFactory(SkBlendMode::kSrcOver);
|
||||
crossPaint.setColorFragmentProcessor(std::make_unique<VisualizeCrossProductSignFP>(a, b));
|
||||
rtc->drawRect(/*clip=*/nullptr, std::move(crossPaint), GrAA::kNo, SkMatrix::I(),
|
||||
SkRect::MakeWH(1,1));
|
||||
|
||||
GrColor result;
|
||||
rtc->readPixels(directContext,
|
||||
SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType), &result,
|
||||
4, {0, 0});
|
||||
|
||||
SkASSERT(expectedCrossProduct == a.cross(b));
|
||||
if (expectedCrossProduct > 0) {
|
||||
REPORTER_ASSERT(reporter, result == GrColorPackRGBA(0, 255, 0, 255)); // Green.
|
||||
} else if (expectedCrossProduct < 0) {
|
||||
REPORTER_ASSERT(reporter, result == GrColorPackRGBA(255, 0, 0, 255)); // Red.
|
||||
} else {
|
||||
REPORTER_ASSERT(reporter, result == GrColorPackRGBA(0, 0, 0, 255)); // Black.
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user