skia2/tests/SkSLCross.cpp

150 lines
6.1 KiB
C++
Raw Normal View History

/*
* Copyright 2020 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
[includes] Enforce IWYU on sksl code PS1 regenerates the Bazel files. Use it as the base change when comparing patchsets. IWYU seems to do a good job of working with MyFile.cpp and MyFile.h, but if there is just a MyHeader.h, it doesn't always seem to throw errors if the includes aren't correct. This was observed with include/sksl/DSL.h This might be due to the fact that headers are not compiled on their own, so they are never sent directly to the IWYU binary. This change sets enforce_iwyu_on_package() on the all sksl packages and then fixes the includes until all those checks are happy. There were a few files that needed fixes outside of the sksl folder. Examples include: - src/gpu/effects/GrConvexPolyEffect.cpp - tests/SkSLDSLTest.cpp To really enforce this, we need to add a CI/CQ job that runs bazel build //example:hello_world_gl --config=clang \ --sandbox_base=/dev/shm --features skia_enforce_iwyu If that failed, a dev could make the changes described in the logs and/or run the command locally to see those prescribed fixes. I had to add several entries to toolchain/IWYU_mapping.imp in order to fix some private includes and other atypical choices. I tried adding a rule there to allow inclusion of SkTypes.h to make sure defines like SK_SUPPORT_GPU, but could not get it to work for all cases, so I deferred to using the IWYU pragma: keep (e.g. SkSLPipelineStageCodeGenerator.h) Change-Id: I4c3e536d8e69ff7ff2d26fe61a525a6c2e80db06 Bug: skia:13052 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/522256 Reviewed-by: John Stiles <johnstiles@google.com> Reviewed-by: Greg Daniel <egdaniel@google.com>
2022-03-18 20:40:58 +00:00
#include "include/core/SkAlphaType.h"
#include "include/core/SkBlendMode.h"
#include "include/core/SkColorSpace.h"
#include "include/core/SkColorType.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkMatrix.h"
#include "include/core/SkPoint.h"
#include "include/core/SkRect.h"
#include "include/core/SkSurfaceProps.h"
#include "include/core/SkTypes.h"
#include "include/gpu/GrDirectContext.h"
#include "include/private/SkColorData.h"
#include "include/private/gpu/ganesh/GrTypesPriv.h"
[includes] Enforce IWYU on sksl code PS1 regenerates the Bazel files. Use it as the base change when comparing patchsets. IWYU seems to do a good job of working with MyFile.cpp and MyFile.h, but if there is just a MyHeader.h, it doesn't always seem to throw errors if the includes aren't correct. This was observed with include/sksl/DSL.h This might be due to the fact that headers are not compiled on their own, so they are never sent directly to the IWYU binary. This change sets enforce_iwyu_on_package() on the all sksl packages and then fixes the includes until all those checks are happy. There were a few files that needed fixes outside of the sksl folder. Examples include: - src/gpu/effects/GrConvexPolyEffect.cpp - tests/SkSLDSLTest.cpp To really enforce this, we need to add a CI/CQ job that runs bazel build //example:hello_world_gl --config=clang \ --sandbox_base=/dev/shm --features skia_enforce_iwyu If that failed, a dev could make the changes described in the logs and/or run the command locally to see those prescribed fixes. I had to add several entries to toolchain/IWYU_mapping.imp in order to fix some private includes and other atypical choices. I tried adding a rule there to allow inclusion of SkTypes.h to make sure defines like SK_SUPPORT_GPU, but could not get it to work for all cases, so I deferred to using the IWYU pragma: keep (e.g. SkSLPipelineStageCodeGenerator.h) Change-Id: I4c3e536d8e69ff7ff2d26fe61a525a6c2e80db06 Bug: skia:13052 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/522256 Reviewed-by: John Stiles <johnstiles@google.com> Reviewed-by: Greg Daniel <egdaniel@google.com>
2022-03-18 20:40:58 +00:00
#include "src/core/SkSLTypeShared.h"
#include "src/gpu/ganesh/GrColor.h"
#include "src/gpu/ganesh/GrFragmentProcessor.h"
#include "src/gpu/ganesh/GrImageInfo.h"
#include "src/gpu/ganesh/GrPaint.h"
#include "src/gpu/ganesh/GrPixmap.h"
#include "src/gpu/ganesh/GrProcessor.h"
#include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h"
#include "src/gpu/ganesh/glsl/GrGLSLProgramDataManager.h"
#include "src/gpu/ganesh/glsl/GrGLSLUniformHandler.h"
#include "src/gpu/ganesh/v1/SurfaceDrawContext_v1.h"
[includes] Enforce IWYU on sksl code PS1 regenerates the Bazel files. Use it as the base change when comparing patchsets. IWYU seems to do a good job of working with MyFile.cpp and MyFile.h, but if there is just a MyHeader.h, it doesn't always seem to throw errors if the includes aren't correct. This was observed with include/sksl/DSL.h This might be due to the fact that headers are not compiled on their own, so they are never sent directly to the IWYU binary. This change sets enforce_iwyu_on_package() on the all sksl packages and then fixes the includes until all those checks are happy. There were a few files that needed fixes outside of the sksl folder. Examples include: - src/gpu/effects/GrConvexPolyEffect.cpp - tests/SkSLDSLTest.cpp To really enforce this, we need to add a CI/CQ job that runs bazel build //example:hello_world_gl --config=clang \ --sandbox_base=/dev/shm --features skia_enforce_iwyu If that failed, a dev could make the changes described in the logs and/or run the command locally to see those prescribed fixes. I had to add several entries to toolchain/IWYU_mapping.imp in order to fix some private includes and other atypical choices. I tried adding a rule there to allow inclusion of SkTypes.h to make sure defines like SK_SUPPORT_GPU, but could not get it to work for all cases, so I deferred to using the IWYU pragma: keep (e.g. SkSLPipelineStageCodeGenerator.h) Change-Id: I4c3e536d8e69ff7ff2d26fe61a525a6c2e80db06 Bug: skia:13052 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/522256 Reviewed-by: John Stiles <johnstiles@google.com> Reviewed-by: Greg Daniel <egdaniel@google.com>
2022-03-18 20:40:58 +00:00
#include "tests/Test.h"
#include "tools/gpu/GrContextFactory.h"
#include <memory>
#include <utility>
namespace skgpu { class KeyBuilder; }
struct GrShaderCaps;
static void run_test(skiatest::Reporter*, GrDirectContext*,
skgpu::v1::SurfaceDrawContext*, 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* dContext = ctxInfo.directContext();
auto sdc = skgpu::v1::SurfaceDrawContext::Make(dContext, GrColorType::kRGBA_8888, nullptr,
SkBackingFit::kExact, {1, 1}, SkSurfaceProps(),
/*label=*/"SkSLCross_Test");
if (!sdc) {
ERRORF(reporter, "could not create render target context.");
return;
}
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.
}
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"; }
std::unique_ptr<GrFragmentProcessor> clone() const override {
return std::unique_ptr<GrFragmentProcessor>(new VisualizeCrossProductSignFP(fA, fB));
}
private:
void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {}
bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
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,
SkSLType::kFloat2, "a", &a);
fBUniform = args.fUniformHandler->addUniform(&fp, kFragment_GrShaderFlag,
SkSLType::kFloat2, "b", &b);
args.fFragBuilder->codeAppendf(R"(
float crossProduct = cross_length_2d(%s, %s);
float2 visualization = clamp(float2(-sign(crossProduct), sign(crossProduct)),
float2(0), float2(1));
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;
};
return std::make_unique<Impl>();
}
const SkVector fA, fB;
};
} // namespace
static void run_test(skiatest::Reporter* reporter, GrDirectContext* directContext,
skgpu::v1::SurfaceDrawContext* sdc, SkVector a, SkVector b,
float expectedCrossProduct) {
SkASSERT(sdc->width() == 1);
SkASSERT(sdc->height() == 1);
sdc->clear(SkPMColor4f::FromBytes_RGBA(0xbaaaaaad));
GrPaint crossPaint;
crossPaint.setColor4f(SK_PMColor4fWHITE);
crossPaint.setPorterDuffXPFactory(SkBlendMode::kSrcOver);
crossPaint.setColorFragmentProcessor(std::make_unique<VisualizeCrossProductSignFP>(a, b));
sdc->drawRect(/*clip=*/nullptr, std::move(crossPaint), GrAA::kNo, SkMatrix::I(),
SkRect::MakeWH(1,1));
GrColor result;
GrPixmap resultPM(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
&result,
sizeof(GrColor));
sdc->readPixels(directContext, resultPM, {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.
}
}