Reland "Add sk_Caps.builtinDeterminantSupport and use it in cross()."

This is a reland of 6bbf026b54

Original change's description:
> Add sk_Caps.builtinDeterminantSupport and use it in cross().
>
> This CL partially relands http://review.skia.org/321790.
>
> Change-Id: I26a1aefda8a01167783e6e7fa15a51aa35ee5d82
> Bug: skia:10819, skia:10810
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/323784
> Reviewed-by: Chris Dalton <csmartdalton@google.com>
> Commit-Queue: John Stiles <johnstiles@google.com>

Bug: skia:10819
Bug: skia:10810
Change-Id: I7731f93db07bc917707cbbe1daca2e5ce0f763d7
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/324620
Reviewed-by: John Stiles <johnstiles@google.com>
Commit-Queue: John Stiles <johnstiles@google.com>
This commit is contained in:
John Stiles 2020-10-08 14:55:36 -04:00 committed by Skia Commit-Bot
parent 9555f2934d
commit 6f3015a562
14 changed files with 463 additions and 169 deletions

View File

@ -302,6 +302,7 @@ sksl_settings_tests = [
"$_tests/sksl/inliner/InlinerWrapsEarlyReturnsWithDoWhileBlock.sksl",
"$_tests/sksl/shared/Derivatives.sksl",
"$_tests/sksl/shared/DerivativesFlipY.sksl",
"$_tests/sksl/shared/CrossIntrinsic.sksl",
"$_tests/sksl/workarounds/AbsInt.sksl",
"$_tests/sksl/workarounds/BlendGuardedDivide.sksl",
"$_tests/sksl/workarounds/BlendModesAllZeroVec.sksl",

View File

@ -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",

View File

@ -57,6 +57,7 @@ GrShaderCaps::GrShaderCaps(const GrContextOptions& options) {
fHasLowFragmentPrecision = false;
fColorSpaceMathNeedsFloat = false;
fBuiltinFMASupport = false;
fBuiltinDeterminantSupport = false;
fCanUseDoLoops = true;
fVersionDeclString = nullptr;
@ -138,6 +139,7 @@ void GrShaderCaps::dumpJSON(SkJSONWriter* writer) const {
writer->appendBool("Has poor fragment precision", fHasLowFragmentPrecision);
writer->appendBool("Color space math needs float", fColorSpaceMathNeedsFloat);
writer->appendBool("Builtin fma() support", fBuiltinFMASupport);
writer->appendBool("Builtin determinant() support", fBuiltinDeterminantSupport);
writer->appendBool("Can use do-while loops", fCanUseDoLoops);
writer->appendS32("Max FS Samplers", fMaxFragmentSamplers);

View File

@ -88,6 +88,8 @@ public:
// SkSL only.
bool builtinFMASupport() const { return fBuiltinFMASupport; }
bool builtinDeterminantSupport() const { return fBuiltinDeterminantSupport; }
AdvBlendEqInteraction advBlendEqInteraction() const { return fAdvBlendEqInteraction; }
bool mustEnableAdvBlendEqs() const {
@ -281,6 +283,7 @@ private:
// Used by SkSL to know when to generate polyfills.
bool fBuiltinFMASupport : 1;
bool fBuiltinDeterminantSupport : 1;
// Used for specific driver bug work arounds
bool fCanUseAnyFunctionInShader : 1;

View File

@ -978,6 +978,8 @@ void GrGLCaps::initGLSL(const GrGLContextInfo& ctxInfo, const GrGLInterface* gli
shaderCaps->fBuiltinFMASupport = ctxInfo.glslGeneration() >= k320es_GrGLSLGeneration;
}
shaderCaps->fBuiltinDeterminantSupport = ctxInfo.glslGeneration() >= k150_GrGLSLGeneration;
if (GR_IS_GR_WEBGL(standard)) {
// WebGL 1.0 doesn't support do-while loops.
shaderCaps->fCanUseDoLoops = version >= GR_GL_VER(2, 0);

View File

@ -300,7 +300,7 @@ void GLSLCodeGenerator::writeDeterminantHack(const Expression& mat) {
}
}
else if (type == *fContext.fFloat4x4_Type || type == *fContext.fHalf4x4_Type) {
name = "_determinant3";
name = "_determinant4";
if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
fWrittenIntrinsics.insert(name);
fExtraFunctions.writeText((
@ -536,12 +536,13 @@ void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) {
if (!fFoundDerivatives &&
fProgram.fSettings.fCaps->shaderDerivativeExtensionString()) {
SkASSERT(fProgram.fSettings.fCaps->shaderDerivativeSupport());
this->writeExtension(fProgram.fSettings.fCaps->shaderDerivativeExtensionString());
this->writeExtension(
fProgram.fSettings.fCaps->shaderDerivativeExtensionString());
fFoundDerivatives = true;
}
break;
case FunctionClass::kDeterminant:
if (fProgram.fSettings.fCaps->generation() < k150_GrGLSLGeneration) {
if (!fProgram.fSettings.fCaps->builtinDeterminantSupport()) {
SkASSERT(arguments.size() == 1);
this->writeDeterminantHack(*arguments[0]);
return;

View File

@ -159,6 +159,8 @@ static void fill_caps(const SkSL::ShaderCapsClass& caps,
CAP(canUseAnyFunctionInShader);
CAP(floatIs32Bits);
CAP(integerSupport);
CAP(builtinFMASupport);
CAP(builtinDeterminantSupport);
#undef CAP
}

View File

@ -174,6 +174,11 @@ public:
return fBuiltinFMASupport;
}
bool fBuiltinDeterminantSupport = false;
bool builtinDeterminantSupport() const {
return fBuiltinDeterminantSupport;
}
bool fCanUseDoLoops = false;
bool canUseDoLoops() const {
// we define this to false in standalone so we don't use do loops while inlining in FP files
@ -286,6 +291,7 @@ public:
ShaderCapsPointer result = MakeShaderCaps();
result->fVersionDeclString = "#version 400";
result->fShaderDerivativeSupport = true;
result->fBuiltinDeterminantSupport = true;
result->fCanUseDoLoops = true;
return result;
}

File diff suppressed because it is too large Load Diff

View File

@ -575,3 +575,15 @@ 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 sk_Caps.builtinDeterminantSupport ? determinant(float2x2(a, b))
: a.x*b.y - a.y*b.x;
}
half cross(half2 a, half2 b) {
return sk_Caps.builtinDeterminantSupport ? determinant(half2x2(a, b))
: a.x*b.y - a.y*b.x;
}

112
tests/SkSLCross.cpp Normal file
View File

@ -0,0 +1,112 @@
/*
* 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"
#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.
}
}

View File

@ -0,0 +1,12 @@
/*#pragma settings Default*/
uniform half2 h2;
uniform float2 f2;
void main() {
sk_FragColor = cross( half2(1, 2), half2(3, 4)).xxxx;
sk_FragColor = half(cross(float2(5, 6), float2(7, 8))).xxxx;
sk_FragColor = cross(h2, h2).xxxx;
sk_FragColor = half(cross(f2, f2)).xxxx;
}

View File

@ -0,0 +1,34 @@
#version 400
out vec4 sk_FragColor;
uniform vec2 h2;
uniform vec2 f2;
void main() {
float _0_cross;
{
_0_cross = determinant(mat2(vec2(1.0, 2.0), vec2(3.0, 4.0)));
}
sk_FragColor = vec4(_0_cross);
float _3_cross;
{
_3_cross = determinant(mat2(vec2(5.0, 6.0), vec2(7.0, 8.0)));
}
sk_FragColor = vec4(_3_cross);
float _6_cross;
{
_6_cross = determinant(mat2(h2, h2));
}
sk_FragColor = vec4(_6_cross);
float _7_cross;
{
_7_cross = determinant(mat2(f2, f2));
}
sk_FragColor = vec4(_7_cross);
}

View File

@ -0,0 +1,24 @@
out vec4 sk_FragColor;
uniform vec2 h2;
uniform vec2 f2;
void main() {
sk_FragColor = vec4(-2.0);
sk_FragColor = vec4(-2.0);
float _6_cross;
{
_6_cross = h2.x * h2.y - h2.y * h2.x;
}
sk_FragColor = vec4(_6_cross);
float _7_cross;
{
_7_cross = f2.x * f2.y - f2.y * f2.x;
}
sk_FragColor = vec4(_7_cross);
}