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 | |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 15 | #include <float.h> |
| 16 | #include "Test.h" |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 17 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 18 | #if SK_SUPPORT_GPU |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 19 | #include "GrContext.h" |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 20 | #include "GrContextPriv.h" |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 21 | #include "GrResourceProvider.h" |
Robert Phillips | 009e9af | 2017-06-15 14:01:04 -0400 | [diff] [blame] | 22 | #include "GrTextureProxy.h" |
jvanverth | 28f9c60 | 2014-12-05 13:06:35 -0800 | [diff] [blame] | 23 | #include "SkHalf.h" |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 24 | |
| 25 | static const int DEV_W = 100, DEV_H = 100; |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 26 | static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H); |
| 27 | |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame] | 28 | template <typename T> |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 29 | void runFPTest(skiatest::Reporter* reporter, GrContext* context, |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame] | 30 | T min, T max, T epsilon, T maxInt, int arraySize, GrPixelConfig config) { |
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 | |
| 48 | for (int origin = 0; origin < 2; ++origin) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 49 | GrSurfaceDesc desc; |
| 50 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 51 | desc.fOrigin = 0 == origin ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin; |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 52 | desc.fWidth = DEV_W; |
| 53 | desc.fHeight = DEV_H; |
| 54 | desc.fConfig = config; |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 55 | sk_sp<GrTextureProxy> fpProxy = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), |
| 56 | desc, SkBudgeted::kNo, |
| 57 | controlPixelData.begin(), 0); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 58 | // Floating point textures are NOT supported everywhere |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 59 | if (!fpProxy) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 60 | continue; |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 61 | } |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 62 | |
| 63 | sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext( |
| 64 | std::move(fpProxy), nullptr); |
| 65 | REPORTER_ASSERT(reporter, sContext); |
| 66 | |
| 67 | bool result = context->contextPriv().readSurfacePixels(sContext.get(), |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 68 | 0, 0, DEV_W, DEV_H, |
| 69 | desc.fConfig, nullptr, |
| 70 | readBuffer.begin(), 0); |
| 71 | REPORTER_ASSERT(reporter, result); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 72 | REPORTER_ASSERT(reporter, |
| 73 | 0 == memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.bytes())); |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
csmartdalton | 6aa0e11 | 2017-02-08 16:14:11 -0500 | [diff] [blame] | 77 | static const int RGBA32F_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4; |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame] | 78 | static const float kMaxIntegerRepresentableInSPFloatingPoint = 16777216; // 2 ^ 24 |
jvanverth | 28f9c60 | 2014-12-05 13:06:35 -0800 | [diff] [blame] | 79 | |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 80 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FloatingPointTextureTest, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 81 | runFPTest<float>(reporter, ctxInfo.grContext(), FLT_MIN, FLT_MAX, FLT_EPSILON, |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 82 | kMaxIntegerRepresentableInSPFloatingPoint, |
csmartdalton | 6aa0e11 | 2017-02-08 16:14:11 -0500 | [diff] [blame] | 83 | RGBA32F_CONTROL_ARRAY_SIZE, kRGBA_float_GrPixelConfig); |
| 84 | } |
| 85 | |
| 86 | static const int RG32F_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 2; |
| 87 | |
| 88 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FloatingPointTextureTest_RG, reporter, ctxInfo) { |
| 89 | runFPTest<float>(reporter, ctxInfo.grContext(), FLT_MIN, FLT_MAX, FLT_EPSILON, |
| 90 | kMaxIntegerRepresentableInSPFloatingPoint, |
| 91 | RG32F_CONTROL_ARRAY_SIZE, kRG_float_GrPixelConfig); |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame] | 92 | } |
mtklein | cada95a | 2015-01-22 13:50:35 -0800 | [diff] [blame] | 93 | |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame] | 94 | static const int HALF_ALPHA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 1 /*alpha-only*/; |
| 95 | static const SkHalf kMaxIntegerRepresentableInHalfFloatingPoint = 0x6800; // 2 ^ 11 |
jvanverth | 1334c21 | 2014-12-18 05:44:55 -0800 | [diff] [blame] | 96 | |
Brian Osman | 3a88725 | 2016-11-17 13:27:31 -0500 | [diff] [blame] | 97 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatAlphaTextureTest, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 98 | runFPTest<SkHalf>(reporter, ctxInfo.grContext(), SK_HalfMin, SK_HalfMax, SK_HalfEpsilon, |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame] | 99 | kMaxIntegerRepresentableInHalfFloatingPoint, |
| 100 | HALF_ALPHA_CONTROL_ARRAY_SIZE, kAlpha_half_GrPixelConfig); |
| 101 | } |
jvanverth | 1334c21 | 2014-12-18 05:44:55 -0800 | [diff] [blame] | 102 | |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame] | 103 | 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] | 104 | |
Brian Osman | 3a88725 | 2016-11-17 13:27:31 -0500 | [diff] [blame] | 105 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatRGBATextureTest, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 106 | runFPTest<SkHalf>(reporter, ctxInfo.grContext(), SK_HalfMin, SK_HalfMax, SK_HalfEpsilon, |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame] | 107 | kMaxIntegerRepresentableInHalfFloatingPoint, |
| 108 | HALF_RGBA_CONTROL_ARRAY_SIZE, kRGBA_half_GrPixelConfig); |
jvanverth | 28f9c60 | 2014-12-05 13:06:35 -0800 | [diff] [blame] | 109 | } |
| 110 | |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 111 | #endif |