| 1 |
/* |
| 2 |
* Copyright 2018 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkBlendMode.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkMatrix.h" |
| 13 |
#include "include/core/SkPoint.h" |
| 14 |
#include "include/core/SkRect.h" |
| 15 |
#include "include/core/SkRefCnt.h" |
| 16 |
#include "include/core/SkString.h" |
| 17 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 18 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 19 |
#include "src/core/SkCanvasPriv.h" |
| 20 |
#include "src/gpu/ganesh/GrBuffer.h" |
| 21 |
#include "src/gpu/ganesh/GrCanvas.h" |
| 22 |
#include "src/gpu/ganesh/GrCaps.h" |
| 23 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 24 |
#include "src/gpu/ganesh/GrGeometryProcessor.h" |
| 25 |
#include "src/gpu/ganesh/GrGpuBuffer.h" |
| 26 |
#include "src/gpu/ganesh/GrMemoryPool.h" |
| 27 |
#include "src/gpu/ganesh/GrOpFlushState.h" |
| 28 |
#include "src/gpu/ganesh/GrOpsRenderPass.h" |
| 29 |
#include "src/gpu/ganesh/GrPipeline.h" |
| 30 |
#include "src/gpu/ganesh/GrProcessor.h" |
| 31 |
#include "src/gpu/ganesh/GrProcessorSet.h" |
| 32 |
#include "src/gpu/ganesh/GrProgramInfo.h" |
| 33 |
#include "src/gpu/ganesh/GrRecordingContextPriv.h" |
| 34 |
#include "src/gpu/ganesh/GrResourceProvider.h" |
| 35 |
#include "src/gpu/ganesh/GrShaderCaps.h" |
| 36 |
#include "src/gpu/ganesh/GrShaderVar.h" |
| 37 |
#include "src/gpu/ganesh/SurfaceDrawContext.h" |
| 38 |
#include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h" |
| 39 |
#include "src/gpu/ganesh/glsl/GrGLSLProgramDataManager.h" |
| 40 |
#include "src/gpu/ganesh/glsl/GrGLSLUniformHandler.h" |
| 41 |
#include "src/gpu/ganesh/glsl/GrGLSLVarying.h" |
| 42 |
#include "src/gpu/ganesh/glsl/GrGLSLVertexGeoBuilder.h" |
| 43 |
#include "src/gpu/ganesh/ops/GrDrawOp.h" |
| 44 |
#include "src/gpu/ganesh/ops/GrOp.h" |
| 45 |
#include "tools/ganesh/ProxyUtils.h" |
| 46 |
|
| 47 |
#include <memory> |
| 48 |
#include <utility> |
| 49 |
|
| 50 |
class GrAppliedClip; |
| 51 |
|
| 52 |
/** |
| 53 |
* This test ensures that fwidth() works properly on GPU configs by drawing a squircle. |
| 54 |
*/ |
| 55 |
namespace { |
| 56 |
|
| 57 |
static constexpr GrGeometryProcessor::Attribute gVertex = |
| 58 |
{"bboxcoord", kFloat2_GrVertexAttribType, SkSLType::kFloat2}; |
| 59 |
|
| 60 |
//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 61 |
// SkSL code. |
| 62 |
|
| 63 |
class FwidthSquircleTestProcessor : public GrGeometryProcessor { |
| 64 |
public: |
| 65 |
static GrGeometryProcessor* Make(SkArenaAlloc* arena, const SkMatrix& viewMatrix) { |
| 66 |
return arena->make([&](void* ptr) { |
| 67 |
return new (ptr) FwidthSquircleTestProcessor(viewMatrix); |
| 68 |
}); |
| 69 |
} |
| 70 |
|
| 71 |
const char* name() const override { return "FwidthSquircleTestProcessor"; } |
| 72 |
|
| 73 |
void addToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const final {} |
| 74 |
|
| 75 |
std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const final; |
| 76 |
|
| 77 |
private: |
| 78 |
FwidthSquircleTestProcessor(const SkMatrix& viewMatrix) |
| 79 |
: GrGeometryProcessor(kFwidthSquircleTestProcessor_ClassID) |
| 80 |
, fViewMatrix(viewMatrix) { |
| 81 |
this->setVertexAttributesWithImplicitOffsets(&gVertex, 1); |
| 82 |
} |
| 83 |
|
| 84 |
const SkMatrix fViewMatrix; |
| 85 |
|
| 86 |
using INHERITED = GrGeometryProcessor; |
| 87 |
}; |
| 88 |
|
| 89 |
std::unique_ptr<GrGeometryProcessor::ProgramImpl> FwidthSquircleTestProcessor::makeProgramImpl( |
| 90 |
const GrShaderCaps&) const { |
| 91 |
class Impl : public ProgramImpl { |
| 92 |
public: |
| 93 |
void setData(const GrGLSLProgramDataManager& pdman, |
| 94 |
const GrShaderCaps&, |
| 95 |
const GrGeometryProcessor& geomProc) override { |
| 96 |
const auto& proc = geomProc.cast<FwidthSquircleTestProcessor>(); |
| 97 |
pdman.setSkMatrix(fViewMatrixHandle, proc.fViewMatrix); |
| 98 |
} |
| 99 |
|
| 100 |
private: |
| 101 |
void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 102 |
const auto& proc = args.fGeomProc.cast<FwidthSquircleTestProcessor>(); |
| 103 |
|
| 104 |
auto* uniforms = args.fUniformHandler; |
| 105 |
fViewMatrixHandle = uniforms->addUniform(nullptr, |
| 106 |
kVertex_GrShaderFlag, |
| 107 |
SkSLType::kFloat3x3, |
| 108 |
"viewmatrix"); |
| 109 |
|
| 110 |
auto* varyings = args.fVaryingHandler; |
| 111 |
varyings->emitAttributes(proc); |
| 112 |
|
| 113 |
GrGLSLVarying squircleCoord(SkSLType::kFloat2); |
| 114 |
varyings->addVarying("bboxcoord", &squircleCoord); |
| 115 |
|
| 116 |
auto* v = args.fVertBuilder; |
| 117 |
v->codeAppendf("float2x2 R = float2x2(cos(.05), sin(.05), -sin(.05), cos(.05));"); |
| 118 |
|
| 119 |
v->codeAppendf("%s = bboxcoord * 1.25;", squircleCoord.vsOut()); |
| 120 |
v->codeAppendf("float3 vertexpos = float3(bboxcoord * 100 * R + 100, 1);"); |
| 121 |
v->codeAppendf("vertexpos = %s * vertexpos;", |
| 122 |
uniforms->getUniformCStr(fViewMatrixHandle)); |
| 123 |
gpArgs->fPositionVar.set(SkSLType::kFloat3, "vertexpos"); |
| 124 |
|
| 125 |
auto* f = args.fFragBuilder; |
| 126 |
f->codeAppendf("float golden_ratio = 1.61803398875;"); |
| 127 |
f->codeAppendf("float pi = 3.141592653589793;"); |
| 128 |
f->codeAppendf("float x = abs(%s.x), y = abs(%s.y);", |
| 129 |
squircleCoord.fsIn(), squircleCoord.fsIn()); |
| 130 |
|
| 131 |
// Squircle function! |
| 132 |
f->codeAppendf("float fn = half(pow(x, golden_ratio*pi) + " |
| 133 |
"pow(y, golden_ratio*pi) - 1);"); |
| 134 |
f->codeAppendf("float fnwidth = fwidth(fn);"); |
| 135 |
f->codeAppendf("fnwidth += 1e-10;"); // Guard against divide-by-zero. |
| 136 |
f->codeAppendf("half coverage = clamp(half(.5 - fn/fnwidth), 0, 1);"); |
| 137 |
|
| 138 |
f->codeAppendf("half4 %s = half4(.51, .42, .71, 1) * .89;", args.fOutputColor); |
| 139 |
f->codeAppendf("half4 %s = half4(coverage);", args.fOutputCoverage); |
| 140 |
} |
| 141 |
|
| 142 |
UniformHandle fViewMatrixHandle; |
| 143 |
}; |
| 144 |
|
| 145 |
return std::make_unique<Impl>(); |
| 146 |
} |
| 147 |
|
| 148 |
//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 149 |
// Draw Op. |
| 150 |
|
| 151 |
class FwidthSquircleTestOp : public GrDrawOp { |
| 152 |
public: |
| 153 |
DEFINE_OP_CLASS_ID |
| 154 |
|
| 155 |
static GrOp::Owner Make(GrRecordingContext* ctx, const SkMatrix& viewMatrix) { |
| 156 |
return GrOp::Make<FwidthSquircleTestOp>(ctx, viewMatrix); |
| 157 |
} |
| 158 |
|
| 159 |
private: |
| 160 |
FwidthSquircleTestOp(const SkMatrix& viewMatrix) |
| 161 |
: GrDrawOp(ClassID()) |
| 162 |
, fViewMatrix(viewMatrix) { |
| 163 |
this->setBounds(SkRect::MakeIWH(kWidth, kHeight), HasAABloat::kNo, IsHairline::kNo); |
| 164 |
} |
| 165 |
|
| 166 |
const char* name() const override { return "FwidthSquircleTestOp"; } |
| 167 |
FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
| 168 |
GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, GrClampType) override { |
| 169 |
return GrProcessorSet::EmptySetAnalysis(); |
| 170 |
} |
| 171 |
|
| 172 |
GrProgramInfo* createProgramInfo(const GrCaps* caps, |
| 173 |
SkArenaAlloc* arena, |
| 174 |
const GrSurfaceProxyView& writeView, |
| 175 |
bool usesMSAASurface, |
| 176 |
GrAppliedClip&& appliedClip, |
| 177 |
const GrDstProxyView& dstProxyView, |
| 178 |
GrXferBarrierFlags renderPassXferBarriers, |
| 179 |
GrLoadOp colorLoadOp) const { |
| 180 |
GrGeometryProcessor* geomProc = FwidthSquircleTestProcessor::Make(arena, fViewMatrix); |
| 181 |
|
| 182 |
return sk_gpu_test::CreateProgramInfo(caps, arena, writeView, usesMSAASurface, |
| 183 |
std::move(appliedClip), dstProxyView, |
| 184 |
geomProc, SkBlendMode::kSrcOver, |
| 185 |
GrPrimitiveType::kTriangleStrip, |
| 186 |
renderPassXferBarriers, colorLoadOp); |
| 187 |
} |
| 188 |
|
| 189 |
GrProgramInfo* createProgramInfo(GrOpFlushState* flushState) const { |
| 190 |
return this->createProgramInfo(&flushState->caps(), |
| 191 |
flushState->allocator(), |
| 192 |
flushState->writeView(), |
| 193 |
flushState->usesMSAASurface(), |
| 194 |
flushState->detachAppliedClip(), |
| 195 |
flushState->dstProxyView(), |
| 196 |
flushState->renderPassBarriers(), |
| 197 |
flushState->colorLoadOp()); |
| 198 |
} |
| 199 |
|
| 200 |
void onPrePrepare(GrRecordingContext* context, |
| 201 |
const GrSurfaceProxyView& writeView, |
| 202 |
GrAppliedClip* clip, |
| 203 |
const GrDstProxyView& dstProxyView, |
| 204 |
GrXferBarrierFlags renderPassXferBarriers, |
| 205 |
GrLoadOp colorLoadOp) final { |
| 206 |
SkArenaAlloc* arena = context->priv().recordTimeAllocator(); |
| 207 |
|
| 208 |
// DMSAA is not supported on DDL. |
| 209 |
bool usesMSAASurface = writeView.asRenderTargetProxy()->numSamples() > 1; |
| 210 |
|
| 211 |
// This is equivalent to a GrOpFlushState::detachAppliedClip |
| 212 |
GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip::Disabled(); |
| 213 |
|
| 214 |
fProgramInfo = this->createProgramInfo(context->priv().caps(), arena, writeView, |
| 215 |
usesMSAASurface, std::move(appliedClip), |
| 216 |
dstProxyView, renderPassXferBarriers, colorLoadOp); |
| 217 |
|
| 218 |
context->priv().recordProgramInfo(fProgramInfo); |
| 219 |
} |
| 220 |
|
| 221 |
void onPrepare(GrOpFlushState* flushState) final { |
| 222 |
SkPoint vertices[4] = { |
| 223 |
{-1, -1}, |
| 224 |
{+1, -1}, |
| 225 |
{-1, +1}, |
| 226 |
{+1, +1}, |
| 227 |
}; |
| 228 |
fVertexBuffer = flushState->resourceProvider()->createBuffer(vertices, |
| 229 |
sizeof(vertices), |
| 230 |
GrGpuBufferType::kVertex, |
| 231 |
kStatic_GrAccessPattern); |
| 232 |
} |
| 233 |
|
| 234 |
void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) final { |
| 235 |
if (!fVertexBuffer) { |
| 236 |
return; |
| 237 |
} |
| 238 |
|
| 239 |
if (!fProgramInfo) { |
| 240 |
fProgramInfo = this->createProgramInfo(flushState); |
| 241 |
} |
| 242 |
|
| 243 |
flushState->bindPipeline(*fProgramInfo, SkRect::MakeIWH(kWidth, kHeight)); |
| 244 |
flushState->bindBuffers(nullptr, nullptr, std::move(fVertexBuffer)); |
| 245 |
flushState->draw(4, 0); |
| 246 |
|
| 247 |
} |
| 248 |
|
| 249 |
static const int kWidth = 200; |
| 250 |
static const int kHeight = 200; |
| 251 |
|
| 252 |
sk_sp<GrBuffer> fVertexBuffer; |
| 253 |
const SkMatrix fViewMatrix; |
| 254 |
|
| 255 |
// The program info (and both the GrPipeline and GrGeometryProcessor it relies on), when |
| 256 |
// allocated, are allocated in either the ddl-record-time or flush-time arena. It is the |
| 257 |
// arena's job to free up their memory so we just have a bare programInfo pointer here. We |
| 258 |
// don't even store the GrPipeline and GrGeometryProcessor pointers here bc they are |
| 259 |
// guaranteed to have the same lifetime as the program info. |
| 260 |
GrProgramInfo* fProgramInfo = nullptr; |
| 261 |
|
| 262 |
friend class ::GrOp; // for ctor |
| 263 |
|
| 264 |
using INHERITED = GrDrawOp; |
| 265 |
}; |
| 266 |
|
| 267 |
} // namespace |
| 268 |
|
| 269 |
//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 270 |
// Test. |
| 271 |
|
| 272 |
namespace skiagm { |
| 273 |
|
| 274 |
DEF_SIMPLE_GPU_GM_CAN_FAIL(fwidth_squircle, rContext, canvas, errorMsg, 200, 200) { |
| 275 |
if (!rContext->priv().caps()->shaderCaps()->fShaderDerivativeSupport) { |
| 276 |
*errorMsg = "Shader derivatives not supported."; |
| 277 |
return DrawResult::kSkip; |
| 278 |
} |
| 279 |
|
| 280 |
auto sdc = skgpu::ganesh::TopDeviceSurfaceDrawContext(canvas); |
| 281 |
if (!sdc) { |
| 282 |
*errorMsg = GM::kErrorMsg_DrawSkippedGpuOnly; |
| 283 |
return DrawResult::kSkip; |
| 284 |
} |
| 285 |
|
| 286 |
// Draw the test directly to the frame buffer. |
| 287 |
canvas->clear(SK_ColorWHITE); |
| 288 |
sdc->addDrawOp(FwidthSquircleTestOp::Make(rContext, canvas->getTotalMatrix())); |
| 289 |
return skiagm::DrawResult::kOk; |
| 290 |
} |
| 291 |
|
| 292 |
} // namespace skiagm |