SkSLBench.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2019 Google LLC
  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 "bench/Benchmark.h"
  8. #include "src/sksl/SkSLCompiler.h"
  9. class SkSLBench : public Benchmark {
  10. public:
  11. SkSLBench(SkSL::String name, const char* src)
  12. : fName("sksl_" + name)
  13. , fSrc(src) {}
  14. protected:
  15. const char* onGetName() override {
  16. return fName.c_str();
  17. }
  18. bool isSuitableFor(Backend backend) override {
  19. return backend == kNonRendering_Backend;
  20. }
  21. void onDraw(int loops, SkCanvas*) override {
  22. for (int i = 0; i < loops; i++) {
  23. std::unique_ptr<SkSL::Program> program = fCompiler.convertProgram(
  24. SkSL::Program::kFragment_Kind,
  25. fSrc,
  26. fSettings);
  27. if (!fCompiler.errorCount()) {
  28. fCompiler.optimize(*program);
  29. } else {
  30. printf("%s\n", fCompiler.errorText().c_str());
  31. SK_ABORT("shader compilation failed");
  32. }
  33. }
  34. }
  35. private:
  36. SkSL::String fName;
  37. SkSL::String fSrc;
  38. SkSL::Compiler fCompiler;
  39. SkSL::Program::Settings fSettings;
  40. typedef Benchmark INHERITED;
  41. };
  42. ///////////////////////////////////////////////////////////////////////////////
  43. DEF_BENCH(return new SkSLBench("tiny", "void main() { sk_FragColor = half4(1); }"); )
  44. DEF_BENCH(return new SkSLBench("huge", R"(
  45. uniform half2 uDstTextureUpperLeft_Stage1;
  46. uniform half2 uDstTextureCoordScale_Stage1;
  47. uniform sampler2D uDstTextureSampler_Stage1;
  48. noperspective in half4 vQuadEdge_Stage0;
  49. noperspective in half4 vinColor_Stage0;
  50. out half4 sk_FragColor;
  51. half luminance_Stage1(half3 color) {
  52. return dot(half3(0.3, 0.59, 0.11), color);
  53. }
  54. half3 set_luminance_Stage1(half3 hueSat, half alpha, half3 lumColor) {
  55. half diff = luminance_Stage1(lumColor - hueSat);
  56. half3 outColor = hueSat + diff;
  57. half outLum = luminance_Stage1(outColor);
  58. half minComp = min(min(outColor.r, outColor.g), outColor.b);
  59. half maxComp = max(max(outColor.r, outColor.g), outColor.b);
  60. if (minComp < 0.0 && outLum != minComp) {
  61. outColor = outLum + ((outColor - half3(outLum, outLum, outLum)) * outLum) /
  62. (outLum - minComp);
  63. }
  64. if (maxComp > alpha && maxComp != outLum) {
  65. outColor = outLum +((outColor - half3(outLum, outLum, outLum)) * (alpha - outLum)) /
  66. (maxComp - outLum);
  67. }
  68. return outColor;
  69. }
  70. void main() {
  71. half4 outputColor_Stage0;
  72. half4 outputCoverage_Stage0;
  73. { // Stage 0, QuadEdge
  74. outputColor_Stage0 = vinColor_Stage0;
  75. half edgeAlpha;
  76. half2 duvdx = half2(dFdx(vQuadEdge_Stage0.xy));
  77. half2 duvdy = half2(dFdy(vQuadEdge_Stage0.xy));
  78. if (vQuadEdge_Stage0.z > 0.0 && vQuadEdge_Stage0.w > 0.0) {
  79. edgeAlpha = min(min(vQuadEdge_Stage0.z, vQuadEdge_Stage0.w) + 0.5, 1.0);
  80. } else {
  81. half2 gF = half2(2.0 * vQuadEdge_Stage0.x * duvdx.x - duvdx.y,
  82. 2.0 * vQuadEdge_Stage0.x * duvdy.x - duvdy.y);
  83. edgeAlpha = (vQuadEdge_Stage0.x*vQuadEdge_Stage0.x - vQuadEdge_Stage0.y);
  84. edgeAlpha = saturate(0.5 - edgeAlpha / length(gF));
  85. }
  86. outputCoverage_Stage0 = half4(edgeAlpha);
  87. }
  88. { // Xfer Processor: Custom Xfermode
  89. if (all(lessThanEqual(outputCoverage_Stage0.rgb, half3(0)))) {
  90. discard;
  91. }
  92. // Read color from copy of the destination.
  93. half2 _dstTexCoord = (half2(sk_FragCoord.xy) - uDstTextureUpperLeft_Stage1) *
  94. uDstTextureCoordScale_Stage1;
  95. _dstTexCoord.y = 1.0 - _dstTexCoord.y;
  96. half4 _dstColor = texture(uDstTextureSampler_Stage1, _dstTexCoord);
  97. sk_FragColor.a = outputColor_Stage0.a + (1.0 - outputColor_Stage0.a) * _dstColor.a;
  98. half4 srcDstAlpha = outputColor_Stage0 * _dstColor.a;
  99. sk_FragColor.rgb = set_luminance_Stage1(_dstColor.rgb * outputColor_Stage0.a,
  100. srcDstAlpha.a, srcDstAlpha.rgb);
  101. sk_FragColor.rgb += (1.0 - outputColor_Stage0.a) * _dstColor.rgb + (1.0 - _dstColor.a) *
  102. outputColor_Stage0.rgb;
  103. sk_FragColor = outputCoverage_Stage0 * sk_FragColor +
  104. (half4(1.0) - outputCoverage_Stage0) * _dstColor;
  105. }
  106. }
  107. )"); )