2020-10-08 18:55:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2020 Google LLC
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tests/Test.h"
|
|
|
|
|
2021-08-11 17:01:06 +00:00
|
|
|
#include "src/gpu/GrFragmentProcessor.h"
|
2021-07-06 20:28:52 +00:00
|
|
|
#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
|
2021-07-28 19:13:20 +00:00
|
|
|
#include "src/gpu/v1/SurfaceDrawContext_v1.h"
|
2020-10-08 18:55:36 +00:00
|
|
|
|
2020-12-09 21:37:04 +00:00
|
|
|
static void run_test(skiatest::Reporter*, GrDirectContext*,
|
2021-07-28 19:13:20 +00:00
|
|
|
skgpu::v1::SurfaceDrawContext*, SkVector a,
|
2020-10-08 18:55:36 +00:00
|
|
|
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) {
|
2021-07-28 19:13:20 +00:00
|
|
|
GrDirectContext* dContext = ctxInfo.directContext();
|
|
|
|
auto sdc = skgpu::v1::SurfaceDrawContext::Make(dContext, GrColorType::kRGBA_8888, nullptr,
|
|
|
|
SkBackingFit::kExact, {1, 1}, SkSurfaceProps());
|
|
|
|
if (!sdc) {
|
2020-10-08 18:55:36 +00:00
|
|
|
ERRORF(reporter, "could not create render target context.");
|
|
|
|
return;
|
|
|
|
}
|
2021-07-28 19:13:20 +00:00
|
|
|
run_test(reporter, dContext, sdc.get(), {3,4}, {5,6}, -2); // Negative.
|
|
|
|
run_test(reporter, dContext, sdc.get(), {3,4}, {-5,-6}, 2); // Positive.
|
|
|
|
run_test(reporter, dContext, sdc.get(), {0, 2.287f}, {0, -7.741f}, 0); // Zero.
|
|
|
|
run_test(reporter, dContext, sdc.get(), {62.17f, 0}, {-43.49f, 0}, 0); // Zero.
|
2020-10-08 18:55:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* name() const override { return "VisualizeCrossProductSignFP"; }
|
2021-08-10 17:56:13 +00:00
|
|
|
|
2020-10-08 18:55:36 +00:00
|
|
|
std::unique_ptr<GrFragmentProcessor> clone() const override {
|
|
|
|
return std::unique_ptr<GrFragmentProcessor>(new VisualizeCrossProductSignFP(fA, fB));
|
|
|
|
}
|
2021-08-10 17:56:13 +00:00
|
|
|
|
|
|
|
private:
|
2021-12-20 17:37:56 +00:00
|
|
|
void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {}
|
2020-10-08 18:55:36 +00:00
|
|
|
bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
|
|
|
|
|
2021-08-10 17:56:13 +00:00
|
|
|
std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override {
|
|
|
|
class Impl : public ProgramImpl {
|
|
|
|
public:
|
|
|
|
void emitCode(EmitArgs& args) override {
|
|
|
|
auto& fp = args.fFp.cast<VisualizeCrossProductSignFP>();
|
|
|
|
const char *a, *b;
|
|
|
|
fAUniform = args.fUniformHandler->addUniform(&fp, kFragment_GrShaderFlag,
|
2022-01-21 19:59:22 +00:00
|
|
|
SkSLType::kFloat2, "a", &a);
|
2021-08-10 17:56:13 +00:00
|
|
|
fBUniform = args.fUniformHandler->addUniform(&fp, kFragment_GrShaderFlag,
|
2022-01-21 19:59:22 +00:00
|
|
|
SkSLType::kFloat2, "b", &b);
|
2021-08-10 17:56:13 +00:00
|
|
|
args.fFragBuilder->codeAppendf(R"(
|
2021-12-14 16:25:26 +00:00
|
|
|
float crossProduct = cross_length_2d(%s, %s);
|
2020-10-08 18:55:36 +00:00
|
|
|
float2 visualization = clamp(float2(-sign(crossProduct), sign(crossProduct)),
|
|
|
|
float2(0), float2(1));
|
2021-08-10 17:56:13 +00:00
|
|
|
return half2(visualization).xy01;)", a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
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;
|
|
|
|
};
|
2020-10-08 18:55:36 +00:00
|
|
|
|
2021-02-23 15:07:05 +00:00
|
|
|
return std::make_unique<Impl>();
|
|
|
|
}
|
2020-10-08 18:55:36 +00:00
|
|
|
const SkVector fA, fB;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
static void run_test(skiatest::Reporter* reporter, GrDirectContext* directContext,
|
2021-07-28 19:13:20 +00:00
|
|
|
skgpu::v1::SurfaceDrawContext* sdc, SkVector a, SkVector b,
|
2020-10-08 18:55:36 +00:00
|
|
|
float expectedCrossProduct) {
|
2021-07-28 19:13:20 +00:00
|
|
|
SkASSERT(sdc->width() == 1);
|
|
|
|
SkASSERT(sdc->height() == 1);
|
2020-10-08 18:55:36 +00:00
|
|
|
|
2021-07-28 19:13:20 +00:00
|
|
|
sdc->clear(SkPMColor4f::FromBytes_RGBA(0xbaaaaaad));
|
2020-10-08 18:55:36 +00:00
|
|
|
|
|
|
|
GrPaint crossPaint;
|
|
|
|
crossPaint.setColor4f(SK_PMColor4fWHITE);
|
|
|
|
crossPaint.setPorterDuffXPFactory(SkBlendMode::kSrcOver);
|
|
|
|
crossPaint.setColorFragmentProcessor(std::make_unique<VisualizeCrossProductSignFP>(a, b));
|
2021-07-28 19:13:20 +00:00
|
|
|
sdc->drawRect(/*clip=*/nullptr, std::move(crossPaint), GrAA::kNo, SkMatrix::I(),
|
2020-10-08 18:55:36 +00:00
|
|
|
SkRect::MakeWH(1,1));
|
|
|
|
|
|
|
|
GrColor result;
|
2020-12-24 01:36:44 +00:00
|
|
|
GrPixmap resultPM(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
|
|
|
|
&result,
|
|
|
|
sizeof(GrColor));
|
2021-07-28 19:13:20 +00:00
|
|
|
sdc->readPixels(directContext, resultPM, {0, 0});
|
2020-10-08 18:55:36 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
}
|
|
|
|
}
|