fwidth_squircle.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #include "gm/gm.h"
  8. #include "include/core/SkBlendMode.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkMatrix.h"
  12. #include "include/core/SkPoint.h"
  13. #include "include/core/SkRect.h"
  14. #include "include/core/SkRefCnt.h"
  15. #include "include/core/SkString.h"
  16. #include "include/gpu/GrContext.h"
  17. #include "include/private/GrRecordingContext.h"
  18. #include "include/private/GrTypesPriv.h"
  19. #include "src/gpu/GrBuffer.h"
  20. #include "src/gpu/GrCaps.h"
  21. #include "src/gpu/GrContextPriv.h"
  22. #include "src/gpu/GrGeometryProcessor.h"
  23. #include "src/gpu/GrGpuBuffer.h"
  24. #include "src/gpu/GrGpuCommandBuffer.h"
  25. #include "src/gpu/GrMemoryPool.h"
  26. #include "src/gpu/GrMesh.h"
  27. #include "src/gpu/GrOpFlushState.h"
  28. #include "src/gpu/GrPipeline.h"
  29. #include "src/gpu/GrPrimitiveProcessor.h"
  30. #include "src/gpu/GrProcessor.h"
  31. #include "src/gpu/GrProcessorSet.h"
  32. #include "src/gpu/GrRecordingContextPriv.h"
  33. #include "src/gpu/GrRenderTargetContext.h"
  34. #include "src/gpu/GrRenderTargetContextPriv.h"
  35. #include "src/gpu/GrResourceProvider.h"
  36. #include "src/gpu/GrShaderCaps.h"
  37. #include "src/gpu/GrShaderVar.h"
  38. #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
  39. #include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
  40. #include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h"
  41. #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
  42. #include "src/gpu/glsl/GrGLSLUniformHandler.h"
  43. #include "src/gpu/glsl/GrGLSLVarying.h"
  44. #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
  45. #include "src/gpu/ops/GrDrawOp.h"
  46. #include "src/gpu/ops/GrOp.h"
  47. #include <memory>
  48. #include <utility>
  49. class GrAppliedClip;
  50. /**
  51. * This test ensures that fwidth() works properly on GPU configs by drawing a squircle.
  52. */
  53. namespace skiagm {
  54. static constexpr GrGeometryProcessor::Attribute gVertex =
  55. {"bboxcoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
  56. ////////////////////////////////////////////////////////////////////////////////////////////////////
  57. // SkSL code.
  58. class FwidthSquircleTestProcessor : public GrGeometryProcessor {
  59. public:
  60. FwidthSquircleTestProcessor(const SkMatrix& viewMatrix)
  61. : GrGeometryProcessor(kFwidthSquircleTestProcessor_ClassID)
  62. , fViewMatrix(viewMatrix) {
  63. this->setVertexAttributes(&gVertex, 1);
  64. }
  65. const char* name() const override { return "FwidthSquircleTestProcessor"; }
  66. void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {}
  67. GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
  68. private:
  69. const SkMatrix fViewMatrix;
  70. class Impl;
  71. };
  72. class FwidthSquircleTestProcessor::Impl : public GrGLSLGeometryProcessor {
  73. void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
  74. const auto& proc = args.fGP.cast<FwidthSquircleTestProcessor>();
  75. auto* uniforms = args.fUniformHandler;
  76. fViewMatrixHandle =
  77. uniforms->addUniform(kVertex_GrShaderFlag, kFloat3x3_GrSLType, "viewmatrix");
  78. auto* varyings = args.fVaryingHandler;
  79. varyings->emitAttributes(proc);
  80. GrGLSLVarying squircleCoord(kFloat2_GrSLType);
  81. varyings->addVarying("bboxcoord", &squircleCoord);
  82. auto* v = args.fVertBuilder;
  83. v->codeAppendf("float2x2 R = float2x2(cos(.05), sin(.05), -sin(.05), cos(.05));");
  84. v->codeAppendf("%s = bboxcoord * 1.25;", squircleCoord.vsOut());
  85. v->codeAppendf("float3 vertexpos = float3(bboxcoord * 100 * R + 100, 1);");
  86. v->codeAppendf("vertexpos = %s * vertexpos;", uniforms->getUniformCStr(fViewMatrixHandle));
  87. gpArgs->fPositionVar.set(kFloat3_GrSLType, "vertexpos");
  88. auto* f = args.fFragBuilder;
  89. f->codeAppendf("float golden_ratio = 1.61803398875;");
  90. f->codeAppendf("float pi = 3.141592653589793;");
  91. f->codeAppendf("float x = abs(%s.x), y = abs(%s.y);",
  92. squircleCoord.fsIn(), squircleCoord.fsIn());
  93. // Squircle function!
  94. f->codeAppendf("float fn = half(pow(x, golden_ratio*pi) + pow(y, golden_ratio*pi) - 1);");
  95. f->codeAppendf("float fnwidth = fwidth(fn);");
  96. f->codeAppendf("fnwidth += 1e-10;"); // Guard against divide-by-zero.
  97. f->codeAppendf("half coverage = clamp(half(.5 - fn/fnwidth), 0, 1);");
  98. f->codeAppendf("%s = half4(.51, .42, .71, 1) * .89;", args.fOutputColor);
  99. f->codeAppendf("%s = half4(coverage);", args.fOutputCoverage);
  100. }
  101. void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc,
  102. FPCoordTransformIter&& transformIter) override {
  103. const auto& proc = primProc.cast<FwidthSquircleTestProcessor>();
  104. pdman.setSkMatrix(fViewMatrixHandle, proc.fViewMatrix);
  105. }
  106. UniformHandle fViewMatrixHandle;
  107. };
  108. GrGLSLPrimitiveProcessor* FwidthSquircleTestProcessor::createGLSLInstance(
  109. const GrShaderCaps&) const {
  110. return new Impl();
  111. }
  112. ////////////////////////////////////////////////////////////////////////////////////////////////////
  113. // Draw Op.
  114. class FwidthSquircleTestOp : public GrDrawOp {
  115. public:
  116. DEFINE_OP_CLASS_ID
  117. static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* ctx, const SkMatrix& viewMatrix) {
  118. GrOpMemoryPool* pool = ctx->priv().opMemoryPool();
  119. return pool->allocate<FwidthSquircleTestOp>(viewMatrix);
  120. }
  121. private:
  122. FwidthSquircleTestOp(const SkMatrix& viewMatrix)
  123. : GrDrawOp(ClassID())
  124. , fViewMatrix(viewMatrix) {
  125. this->setBounds(SkRect::MakeIWH(200, 200), HasAABloat::kNo, IsZeroArea::kNo);
  126. }
  127. const char* name() const override { return "ClockwiseTestOp"; }
  128. FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
  129. GrProcessorSet::Analysis finalize(
  130. const GrCaps&, const GrAppliedClip*, bool hasMixedSampledCoverage, GrClampType) override {
  131. return GrProcessorSet::EmptySetAnalysis();
  132. }
  133. void onPrepare(GrOpFlushState*) override {}
  134. void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
  135. SkPoint vertices[4] = {
  136. {-1, -1},
  137. {+1, -1},
  138. {-1, +1},
  139. {+1, +1},
  140. };
  141. sk_sp<const GrBuffer> vertexBuffer(flushState->resourceProvider()->createBuffer(
  142. sizeof(vertices), GrGpuBufferType::kVertex, kStatic_GrAccessPattern, vertices));
  143. if (!vertexBuffer) {
  144. return;
  145. }
  146. GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kSrcOver,
  147. flushState->drawOpArgs().fOutputSwizzle);
  148. GrMesh mesh(GrPrimitiveType::kTriangleStrip);
  149. mesh.setNonIndexedNonInstanced(4);
  150. mesh.setVertexData(std::move(vertexBuffer));
  151. flushState->rtCommandBuffer()->draw(FwidthSquircleTestProcessor(fViewMatrix), pipeline,
  152. nullptr, nullptr, &mesh, 1, SkRect::MakeIWH(100, 100));
  153. }
  154. const SkMatrix fViewMatrix;
  155. friend class ::GrOpMemoryPool; // for ctor
  156. };
  157. ////////////////////////////////////////////////////////////////////////////////////////////////////
  158. // Test.
  159. DEF_SIMPLE_GPU_GM_CAN_FAIL(fwidth_squircle, ctx, rtc, canvas, errorMsg, 200, 200) {
  160. if (!ctx->priv().caps()->shaderCaps()->shaderDerivativeSupport()) {
  161. *errorMsg = "Shader derivatives not supported.";
  162. return DrawResult::kSkip;
  163. }
  164. // Draw the test directly to the frame buffer.
  165. canvas->clear(SK_ColorWHITE);
  166. rtc->priv().testingOnly_addDrawOp(FwidthSquircleTestOp::Make(ctx, canvas->getTotalMatrix()));
  167. return skiagm::DrawResult::kOk;
  168. }
  169. }