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