FloatingPointTextureTest.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright 2014 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. * This is a straightforward test of floating point textures, which are
  9. * supported on some platforms. As of right now, this test only supports
  10. * 32 bit floating point textures, and indeed floating point test values
  11. * have been selected to require 32 bits of precision and full IEEE conformance
  12. */
  13. #include "tests/Test.h"
  14. #include "include/gpu/GrContext.h"
  15. #include "include/private/SkHalf.h"
  16. #include "src/gpu/GrContextPriv.h"
  17. #include "src/gpu/GrProxyProvider.h"
  18. #include "src/gpu/GrTextureProxy.h"
  19. #include "tools/gpu/ProxyUtils.h"
  20. #include <float.h>
  21. static const int DEV_W = 100, DEV_H = 100;
  22. template <typename T>
  23. void runFPTest(skiatest::Reporter* reporter, GrContext* context, T min, T max, T epsilon, T maxInt,
  24. int arraySize, GrColorType colorType) {
  25. if (0 != arraySize % 4) {
  26. REPORT_FAILURE(reporter, "(0 != arraySize % 4)",
  27. SkString("arraySize must be divisible by 4."));
  28. return;
  29. }
  30. SkTDArray<T> controlPixelData, readBuffer;
  31. controlPixelData.setCount(arraySize);
  32. readBuffer.setCount(arraySize);
  33. for (int i = 0; i < arraySize; i += 4) {
  34. controlPixelData[i + 0] = min;
  35. controlPixelData[i + 1] = max;
  36. controlPixelData[i + 2] = epsilon;
  37. controlPixelData[i + 3] = maxInt;
  38. }
  39. for (auto origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
  40. auto fpProxy = sk_gpu_test::MakeTextureProxyFromData(context, GrRenderable::kYes, DEV_W,
  41. DEV_H, colorType, kPremul_SkAlphaType,
  42. origin, controlPixelData.begin(), 0);
  43. // Floating point textures are NOT supported everywhere
  44. if (!fpProxy) {
  45. continue;
  46. }
  47. sk_sp<GrSurfaceContext> sContext = context->priv().makeWrappedSurfaceContext(
  48. std::move(fpProxy), colorType, kPremul_SkAlphaType);
  49. REPORTER_ASSERT(reporter, sContext);
  50. bool result = sContext->readPixels({colorType, kPremul_SkAlphaType, nullptr, DEV_W, DEV_H},
  51. readBuffer.begin(), 0, {0, 0}, context);
  52. REPORTER_ASSERT(reporter, result);
  53. REPORTER_ASSERT(reporter,
  54. 0 == memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.bytes()));
  55. }
  56. }
  57. static const int HALF_ALPHA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 1 /*alpha-only*/;
  58. static const SkHalf kMaxIntegerRepresentableInHalfFloatingPoint = 0x6800; // 2 ^ 11
  59. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatAlphaTextureTest, reporter, ctxInfo) {
  60. runFPTest<SkHalf>(reporter, ctxInfo.grContext(), SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
  61. kMaxIntegerRepresentableInHalfFloatingPoint, HALF_ALPHA_CONTROL_ARRAY_SIZE,
  62. GrColorType::kAlpha_F16);
  63. }
  64. static const int HALF_RGBA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4 /*RGBA*/;
  65. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatRGBATextureTest, reporter, ctxInfo) {
  66. runFPTest<SkHalf>(reporter, ctxInfo.grContext(), SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
  67. kMaxIntegerRepresentableInHalfFloatingPoint, HALF_RGBA_CONTROL_ARRAY_SIZE,
  68. GrColorType::kRGBA_F16);
  69. }