joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 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 | /* |
| 9 | * This is a straightforward test of floating point textures, which are |
| 10 | * supported on some platforms. As of right now, this test only supports |
| 11 | * 32 bit floating point textures, and indeed floating point test values |
| 12 | * have been selected to require 32 bits of precision and full IEEE conformance |
| 13 | */ |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 14 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "tests/Test.h" |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 16 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/gpu/GrContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "include/private/SkHalf.h" |
| 19 | #include "src/gpu/GrContextPriv.h" |
| 20 | #include "src/gpu/GrProxyProvider.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 21 | #include "src/gpu/GrTextureProxy.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "tools/gpu/ProxyUtils.h" |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 23 | |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame] | 24 | #include <float.h> |
| 25 | |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 26 | static const int DEV_W = 100, DEV_H = 100; |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 27 | |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame] | 28 | template <typename T> |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 29 | void runFPTest(skiatest::Reporter* reporter, GrContext* context, T min, T max, T epsilon, T maxInt, |
| 30 | int arraySize, GrColorType colorType) { |
csmartdalton | 6aa0e11 | 2017-02-08 16:14:11 -0500 | [diff] [blame] | 31 | if (0 != arraySize % 4) { |
| 32 | REPORT_FAILURE(reporter, "(0 != arraySize % 4)", |
| 33 | SkString("arraySize must be divisible by 4.")); |
| 34 | return; |
| 35 | } |
| 36 | |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame] | 37 | SkTDArray<T> controlPixelData, readBuffer; |
| 38 | controlPixelData.setCount(arraySize); |
| 39 | readBuffer.setCount(arraySize); |
mtklein | cada95a | 2015-01-22 13:50:35 -0800 | [diff] [blame] | 40 | |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame] | 41 | for (int i = 0; i < arraySize; i += 4) { |
| 42 | controlPixelData[i + 0] = min; |
| 43 | controlPixelData[i + 1] = max; |
| 44 | controlPixelData[i + 2] = epsilon; |
| 45 | controlPixelData[i + 3] = maxInt; |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 48 | for (auto origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) { |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 49 | auto fpProxy = sk_gpu_test::MakeTextureProxyFromData(context, GrRenderable::kYes, DEV_W, |
| 50 | DEV_H, colorType, kPremul_SkAlphaType, |
| 51 | origin, controlPixelData.begin(), 0); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 52 | // Floating point textures are NOT supported everywhere |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 53 | if (!fpProxy) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 54 | continue; |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 55 | } |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 56 | |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 57 | sk_sp<GrSurfaceContext> sContext = context->priv().makeWrappedSurfaceContext( |
| 58 | std::move(fpProxy), colorType, kPremul_SkAlphaType); |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 59 | REPORTER_ASSERT(reporter, sContext); |
| 60 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 61 | bool result = sContext->readPixels({colorType, kPremul_SkAlphaType, nullptr, DEV_W, DEV_H}, |
| 62 | readBuffer.begin(), 0, {0, 0}, context); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 63 | REPORTER_ASSERT(reporter, result); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 64 | REPORTER_ASSERT(reporter, |
| 65 | 0 == memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.bytes())); |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame] | 69 | static const int HALF_ALPHA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 1 /*alpha-only*/; |
| 70 | static const SkHalf kMaxIntegerRepresentableInHalfFloatingPoint = 0x6800; // 2 ^ 11 |
jvanverth | 1334c21 | 2014-12-18 05:44:55 -0800 | [diff] [blame] | 71 | |
Brian Osman | 3a88725 | 2016-11-17 13:27:31 -0500 | [diff] [blame] | 72 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatAlphaTextureTest, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 73 | runFPTest<SkHalf>(reporter, ctxInfo.grContext(), SK_HalfMin, SK_HalfMax, SK_HalfEpsilon, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 74 | kMaxIntegerRepresentableInHalfFloatingPoint, HALF_ALPHA_CONTROL_ARRAY_SIZE, |
| 75 | GrColorType::kAlpha_F16); |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame] | 76 | } |
jvanverth | 1334c21 | 2014-12-18 05:44:55 -0800 | [diff] [blame] | 77 | |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame] | 78 | static const int HALF_RGBA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4 /*RGBA*/; |
jvanverth | 1334c21 | 2014-12-18 05:44:55 -0800 | [diff] [blame] | 79 | |
Brian Osman | 3a88725 | 2016-11-17 13:27:31 -0500 | [diff] [blame] | 80 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatRGBATextureTest, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 81 | runFPTest<SkHalf>(reporter, ctxInfo.grContext(), SK_HalfMin, SK_HalfMax, SK_HalfEpsilon, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 82 | kMaxIntegerRepresentableInHalfFloatingPoint, HALF_RGBA_CONTROL_ARRAY_SIZE, |
| 83 | GrColorType::kRGBA_F16); |
jvanverth | 28f9c60 | 2014-12-05 13:06:35 -0800 | [diff] [blame] | 84 | } |