2018-11-13 20:07:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBlendMode.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/gpu/GrContext.h"
|
|
|
|
#include "include/private/GrRecordingContext.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/private/GrTypesPriv.h"
|
|
|
|
#include "src/gpu/GrBuffer.h"
|
|
|
|
#include "src/gpu/GrCaps.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrContextPriv.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "src/gpu/GrGeometryProcessor.h"
|
|
|
|
#include "src/gpu/GrGpuBuffer.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrMemoryPool.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "src/gpu/GrMesh.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrOpFlushState.h"
|
2019-08-26 15:08:51 +00:00
|
|
|
#include "src/gpu/GrOpsRenderPass.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "src/gpu/GrPipeline.h"
|
|
|
|
#include "src/gpu/GrPrimitiveProcessor.h"
|
|
|
|
#include "src/gpu/GrProcessor.h"
|
|
|
|
#include "src/gpu/GrProcessorSet.h"
|
2019-10-08 16:32:56 +00:00
|
|
|
#include "src/gpu/GrProgramInfo.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrRecordingContextPriv.h"
|
|
|
|
#include "src/gpu/GrRenderTargetContext.h"
|
|
|
|
#include "src/gpu/GrRenderTargetContextPriv.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "src/gpu/GrResourceProvider.h"
|
|
|
|
#include "src/gpu/GrShaderCaps.h"
|
|
|
|
#include "src/gpu/GrShaderVar.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
|
|
|
|
#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h"
|
|
|
|
#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
|
|
|
|
#include "src/gpu/glsl/GrGLSLUniformHandler.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/glsl/GrGLSLVarying.h"
|
|
|
|
#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "src/gpu/ops/GrDrawOp.h"
|
|
|
|
#include "src/gpu/ops/GrOp.h"
|
2019-11-21 21:02:34 +00:00
|
|
|
#include "tools/gpu/ProxyUtils.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
class GrAppliedClip;
|
2018-11-13 20:07:24 +00:00
|
|
|
|
2019-02-07 22:23:36 +00:00
|
|
|
/**
|
|
|
|
* This test ensures that fwidth() works properly on GPU configs by drawing a squircle.
|
|
|
|
*/
|
2018-11-13 20:07:24 +00:00
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
static constexpr GrGeometryProcessor::Attribute gVertex =
|
|
|
|
{"bboxcoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// SkSL code.
|
|
|
|
|
|
|
|
class FwidthSquircleTestProcessor : public GrGeometryProcessor {
|
|
|
|
public:
|
2019-11-18 16:19:51 +00:00
|
|
|
static GrGeometryProcessor* Make(SkArenaAlloc* arena, const SkMatrix& viewMatrix) {
|
|
|
|
return arena->make<FwidthSquircleTestProcessor>(viewMatrix);
|
2018-11-13 20:07:24 +00:00
|
|
|
}
|
2019-11-18 16:19:51 +00:00
|
|
|
|
2018-11-13 20:07:24 +00:00
|
|
|
const char* name() const override { return "FwidthSquircleTestProcessor"; }
|
2019-11-18 16:19:51 +00:00
|
|
|
|
2018-11-13 20:07:24 +00:00
|
|
|
void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {}
|
2019-11-18 16:19:51 +00:00
|
|
|
|
2018-11-13 20:07:24 +00:00
|
|
|
GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
|
|
|
|
|
|
|
|
private:
|
2019-11-18 16:19:51 +00:00
|
|
|
friend class ::SkArenaAlloc; // for access to ctor
|
|
|
|
|
|
|
|
FwidthSquircleTestProcessor(const SkMatrix& viewMatrix)
|
|
|
|
: GrGeometryProcessor(kFwidthSquircleTestProcessor_ClassID)
|
|
|
|
, fViewMatrix(viewMatrix) {
|
|
|
|
this->setVertexAttributes(&gVertex, 1);
|
|
|
|
}
|
|
|
|
|
2018-11-13 20:07:24 +00:00
|
|
|
const SkMatrix fViewMatrix;
|
|
|
|
|
|
|
|
class Impl;
|
2019-11-18 16:19:51 +00:00
|
|
|
|
|
|
|
typedef GrGeometryProcessor INHERITED;
|
2018-11-13 20:07:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class FwidthSquircleTestProcessor::Impl : public GrGLSLGeometryProcessor {
|
|
|
|
void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
|
|
|
|
const auto& proc = args.fGP.cast<FwidthSquircleTestProcessor>();
|
|
|
|
|
|
|
|
auto* uniforms = args.fUniformHandler;
|
|
|
|
fViewMatrixHandle =
|
|
|
|
uniforms->addUniform(kVertex_GrShaderFlag, kFloat3x3_GrSLType, "viewmatrix");
|
|
|
|
|
|
|
|
auto* varyings = args.fVaryingHandler;
|
|
|
|
varyings->emitAttributes(proc);
|
|
|
|
|
|
|
|
GrGLSLVarying squircleCoord(kFloat2_GrSLType);
|
|
|
|
varyings->addVarying("bboxcoord", &squircleCoord);
|
|
|
|
|
|
|
|
auto* v = args.fVertBuilder;
|
|
|
|
v->codeAppendf("float2x2 R = float2x2(cos(.05), sin(.05), -sin(.05), cos(.05));");
|
|
|
|
|
|
|
|
v->codeAppendf("%s = bboxcoord * 1.25;", squircleCoord.vsOut());
|
|
|
|
v->codeAppendf("float3 vertexpos = float3(bboxcoord * 100 * R + 100, 1);");
|
|
|
|
v->codeAppendf("vertexpos = %s * vertexpos;", uniforms->getUniformCStr(fViewMatrixHandle));
|
|
|
|
gpArgs->fPositionVar.set(kFloat3_GrSLType, "vertexpos");
|
|
|
|
|
|
|
|
auto* f = args.fFragBuilder;
|
|
|
|
f->codeAppendf("float golden_ratio = 1.61803398875;");
|
|
|
|
f->codeAppendf("float pi = 3.141592653589793;");
|
|
|
|
f->codeAppendf("float x = abs(%s.x), y = abs(%s.y);",
|
|
|
|
squircleCoord.fsIn(), squircleCoord.fsIn());
|
|
|
|
|
|
|
|
// Squircle function!
|
2019-02-05 22:17:40 +00:00
|
|
|
f->codeAppendf("float fn = half(pow(x, golden_ratio*pi) + pow(y, golden_ratio*pi) - 1);");
|
2018-11-13 20:07:24 +00:00
|
|
|
f->codeAppendf("float fnwidth = fwidth(fn);");
|
|
|
|
f->codeAppendf("fnwidth += 1e-10;"); // Guard against divide-by-zero.
|
2019-02-05 22:17:40 +00:00
|
|
|
f->codeAppendf("half coverage = clamp(half(.5 - fn/fnwidth), 0, 1);");
|
2018-11-13 20:07:24 +00:00
|
|
|
|
|
|
|
f->codeAppendf("%s = half4(.51, .42, .71, 1) * .89;", args.fOutputColor);
|
|
|
|
f->codeAppendf("%s = half4(coverage);", args.fOutputCoverage);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc,
|
2019-11-27 13:57:17 +00:00
|
|
|
const CoordTransformRange&) override {
|
2018-11-13 20:07:24 +00:00
|
|
|
const auto& proc = primProc.cast<FwidthSquircleTestProcessor>();
|
|
|
|
pdman.setSkMatrix(fViewMatrixHandle, proc.fViewMatrix);
|
|
|
|
}
|
|
|
|
|
|
|
|
UniformHandle fViewMatrixHandle;
|
|
|
|
};
|
|
|
|
|
|
|
|
GrGLSLPrimitiveProcessor* FwidthSquircleTestProcessor::createGLSLInstance(
|
|
|
|
const GrShaderCaps&) const {
|
|
|
|
return new Impl();
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Draw Op.
|
|
|
|
|
|
|
|
class FwidthSquircleTestOp : public GrDrawOp {
|
|
|
|
public:
|
|
|
|
DEFINE_OP_CLASS_ID
|
|
|
|
|
2019-02-15 16:33:22 +00:00
|
|
|
static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* ctx, const SkMatrix& viewMatrix) {
|
2019-02-04 18:26:26 +00:00
|
|
|
GrOpMemoryPool* pool = ctx->priv().opMemoryPool();
|
2018-11-13 20:07:24 +00:00
|
|
|
return pool->allocate<FwidthSquircleTestOp>(viewMatrix);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
FwidthSquircleTestOp(const SkMatrix& viewMatrix)
|
|
|
|
: GrDrawOp(ClassID())
|
|
|
|
, fViewMatrix(viewMatrix) {
|
2019-10-01 19:14:44 +00:00
|
|
|
this->setBounds(SkRect::MakeIWH(kWidth, kHeight), HasAABloat::kNo, IsHairline::kNo);
|
2018-11-13 20:07:24 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 16:32:56 +00:00
|
|
|
const char* name() const override { return "FwidthSquircleTestOp"; }
|
2018-11-13 20:07:24 +00:00
|
|
|
FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
|
2019-11-18 16:19:51 +00:00
|
|
|
GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
|
|
|
|
bool hasMixedSampledCoverage, GrClampType) override {
|
2019-01-15 18:53:00 +00:00
|
|
|
return GrProcessorSet::EmptySetAnalysis();
|
2018-11-13 20:07:24 +00:00
|
|
|
}
|
2019-11-18 16:19:51 +00:00
|
|
|
|
|
|
|
void onPrePrepare(GrRecordingContext* context,
|
|
|
|
const GrSurfaceProxyView* dstView,
|
2019-11-21 21:02:34 +00:00
|
|
|
GrAppliedClip* clip,
|
2019-11-21 15:44:53 +00:00
|
|
|
const GrXferProcessor::DstProxyView& dstProxyView) final {
|
2019-11-18 16:19:51 +00:00
|
|
|
SkArenaAlloc* arena = context->priv().recordTimeAllocator();
|
|
|
|
|
2019-11-21 21:02:34 +00:00
|
|
|
// This is equivalent to a GrOpFlushState::detachAppliedClip
|
|
|
|
GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip();
|
|
|
|
|
|
|
|
GrGeometryProcessor* geomProc = FwidthSquircleTestProcessor::Make(arena, fViewMatrix);
|
|
|
|
|
|
|
|
// TODO: need to also give this to the recording context
|
|
|
|
fProgramInfo = sk_gpu_test::CreateProgramInfo(context->priv().caps(), arena, dstView,
|
|
|
|
std::move(appliedClip), dstProxyView,
|
|
|
|
geomProc, SkBlendMode::kSrcOver,
|
|
|
|
GrPrimitiveType::kTriangleStrip);
|
2019-11-18 16:19:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onPrepare(GrOpFlushState* flushState) final {
|
2018-11-13 20:07:24 +00:00
|
|
|
SkPoint vertices[4] = {
|
|
|
|
{-1, -1},
|
|
|
|
{+1, -1},
|
|
|
|
{-1, +1},
|
|
|
|
{+1, +1},
|
|
|
|
};
|
2019-09-05 17:23:23 +00:00
|
|
|
fVertexBuffer = flushState->resourceProvider()->createBuffer(
|
|
|
|
sizeof(vertices), GrGpuBufferType::kVertex, kStatic_GrAccessPattern, vertices);
|
|
|
|
}
|
2019-11-18 16:19:51 +00:00
|
|
|
|
|
|
|
void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) final {
|
2019-09-05 17:23:23 +00:00
|
|
|
if (!fVertexBuffer) {
|
2018-11-13 20:07:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-10-08 16:32:56 +00:00
|
|
|
|
2019-11-21 21:02:34 +00:00
|
|
|
if (!fProgramInfo) {
|
|
|
|
auto geomProc = FwidthSquircleTestProcessor::Make(flushState->allocator(),
|
|
|
|
fViewMatrix);
|
|
|
|
|
|
|
|
fProgramInfo = sk_gpu_test::CreateProgramInfo(&flushState->caps(),
|
|
|
|
flushState->allocator(),
|
|
|
|
flushState->view(),
|
|
|
|
flushState->detachAppliedClip(),
|
|
|
|
flushState->dstProxyView(),
|
|
|
|
geomProc, SkBlendMode::kSrcOver,
|
|
|
|
GrPrimitiveType::kTriangleStrip);
|
|
|
|
}
|
|
|
|
|
2018-11-13 20:07:24 +00:00
|
|
|
GrMesh mesh(GrPrimitiveType::kTriangleStrip);
|
|
|
|
mesh.setNonIndexedNonInstanced(4);
|
2019-09-05 17:23:23 +00:00
|
|
|
mesh.setVertexData(std::move(fVertexBuffer));
|
2019-11-18 16:19:51 +00:00
|
|
|
|
2019-11-21 21:02:34 +00:00
|
|
|
flushState->opsRenderPass()->draw(*fProgramInfo, &mesh, 1,
|
|
|
|
SkRect::MakeIWH(kWidth, kHeight));
|
|
|
|
|
2018-11-13 20:07:24 +00:00
|
|
|
}
|
|
|
|
|
2019-09-30 16:23:26 +00:00
|
|
|
static const int kWidth = 200;
|
|
|
|
static const int kHeight = 200;
|
2018-11-13 20:07:24 +00:00
|
|
|
|
2019-11-18 16:19:51 +00:00
|
|
|
sk_sp<GrBuffer> fVertexBuffer;
|
|
|
|
const SkMatrix fViewMatrix;
|
|
|
|
|
|
|
|
// The program info (and both the GrPipeline and GrPrimitiveProcessor it relies on), when
|
2019-11-21 21:02:34 +00:00
|
|
|
// allocated, are allocated in either the ddl-record-time or flush-time arena. It is the
|
|
|
|
// arena's job to free up their memory so we just have a bare programInfo pointer here. We
|
|
|
|
// don't even store the GrPipeline and GrPrimitiveProcessor pointers here bc they are
|
|
|
|
// guaranteed to have the same lifetime as the program info.
|
2019-11-18 16:19:51 +00:00
|
|
|
GrProgramInfo* fProgramInfo = nullptr;
|
|
|
|
|
2018-11-13 20:07:24 +00:00
|
|
|
friend class ::GrOpMemoryPool; // for ctor
|
2019-11-18 16:19:51 +00:00
|
|
|
|
|
|
|
typedef GrDrawOp INHERITED;
|
2018-11-13 20:07:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Test.
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
DEF_SIMPLE_GPU_GM_CAN_FAIL(fwidth_squircle, ctx, rtc, canvas, errorMsg, 200, 200) {
|
2019-02-04 18:26:26 +00:00
|
|
|
if (!ctx->priv().caps()->shaderCaps()->shaderDerivativeSupport()) {
|
2019-02-07 23:20:09 +00:00
|
|
|
*errorMsg = "Shader derivatives not supported.";
|
|
|
|
return DrawResult::kSkip;
|
2018-11-13 20:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the test directly to the frame buffer.
|
2019-02-07 22:23:36 +00:00
|
|
|
canvas->clear(SK_ColorWHITE);
|
2018-11-13 20:07:24 +00:00
|
|
|
rtc->priv().testingOnly_addDrawOp(FwidthSquircleTestOp::Make(ctx, canvas->getTotalMatrix()));
|
2019-02-07 23:20:09 +00:00
|
|
|
return skiagm::DrawResult::kOk;
|
2018-11-13 20:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|