blob: a2bc1e961d9c5f36d379003d21ca152cc2f2ec36 [file] [log] [blame]
joshualittee5da552014-07-16 13:32:56 -07001/*
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 */
bsalomond309e7a2015-04-30 14:18:54 -070014
joshualittee5da552014-07-16 13:32:56 -070015#include "Test.h"
kkinnunen15302832015-12-01 04:35:26 -080016
joshualittee5da552014-07-16 13:32:56 -070017#include "GrContext.h"
Robert Phillipse78b7252017-04-06 07:59:41 -040018#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050019#include "GrProxyProvider.h"
Robert Phillips009e9af2017-06-15 14:01:04 -040020#include "GrTextureProxy.h"
Brian Salomon58389b92018-03-07 13:01:25 -050021#include "ProxyUtils.h"
jvanverth28f9c602014-12-05 13:06:35 -080022#include "SkHalf.h"
joshualittee5da552014-07-16 13:32:56 -070023
Hal Canary8a001442018-09-19 11:31:27 -040024#include <float.h>
25
joshualittee5da552014-07-16 13:32:56 -070026static const int DEV_W = 100, DEV_H = 100;
joshualittee5da552014-07-16 13:32:56 -070027
jvanverthfb5df432015-05-21 08:12:27 -070028template <typename T>
Brian Salomonc320b152018-02-20 14:05:36 -050029void runFPTest(skiatest::Reporter* reporter, GrContext* context, T min, T max, T epsilon, T maxInt,
30 int arraySize, GrColorType colorType) {
csmartdalton6aa0e112017-02-08 16:14:11 -050031 if (0 != arraySize % 4) {
32 REPORT_FAILURE(reporter, "(0 != arraySize % 4)",
33 SkString("arraySize must be divisible by 4."));
34 return;
35 }
36
jvanverthfb5df432015-05-21 08:12:27 -070037 SkTDArray<T> controlPixelData, readBuffer;
38 controlPixelData.setCount(arraySize);
39 readBuffer.setCount(arraySize);
mtkleincada95a2015-01-22 13:50:35 -080040
jvanverthfb5df432015-05-21 08:12:27 -070041 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;
joshualittee5da552014-07-16 13:32:56 -070046 }
47
Brian Salomon2a4f9832018-03-03 22:43:43 -050048 for (auto origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
Brian Salomon58389b92018-03-07 13:01:25 -050049 auto fpProxy = sk_gpu_test::MakeTextureProxyFromData(context, true, DEV_W, DEV_H, colorType,
50 origin, controlPixelData.begin(), 0);
kkinnunen15302832015-12-01 04:35:26 -080051 // Floating point textures are NOT supported everywhere
Robert Phillipse78b7252017-04-06 07:59:41 -040052 if (!fpProxy) {
kkinnunen15302832015-12-01 04:35:26 -080053 continue;
joshualittee5da552014-07-16 13:32:56 -070054 }
Robert Phillipsf41c22f2017-04-18 07:48:58 -040055
56 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050057 std::move(fpProxy));
Robert Phillipsf41c22f2017-04-18 07:48:58 -040058 REPORTER_ASSERT(reporter, sContext);
59
Brian Salomonc320b152018-02-20 14:05:36 -050060 bool result = context->contextPriv().readSurfacePixels(
61 sContext.get(), 0, 0, DEV_W, DEV_H, colorType, nullptr, readBuffer.begin(), 0);
Robert Phillipse78b7252017-04-06 07:59:41 -040062 REPORTER_ASSERT(reporter, result);
kkinnunen15302832015-12-01 04:35:26 -080063 REPORTER_ASSERT(reporter,
64 0 == memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.bytes()));
joshualittee5da552014-07-16 13:32:56 -070065 }
66}
67
csmartdalton6aa0e112017-02-08 16:14:11 -050068static const int RGBA32F_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4;
jvanverthfb5df432015-05-21 08:12:27 -070069static const float kMaxIntegerRepresentableInSPFloatingPoint = 16777216; // 2 ^ 24
jvanverth28f9c602014-12-05 13:06:35 -080070
bsalomon68d91342016-04-12 09:59:58 -070071DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FloatingPointTextureTest, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070072 runFPTest<float>(reporter, ctxInfo.grContext(), FLT_MIN, FLT_MAX, FLT_EPSILON,
Brian Salomonc320b152018-02-20 14:05:36 -050073 kMaxIntegerRepresentableInSPFloatingPoint, RGBA32F_CONTROL_ARRAY_SIZE,
74 GrColorType::kRGBA_F32);
csmartdalton6aa0e112017-02-08 16:14:11 -050075}
76
77static const int RG32F_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 2;
78
79DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FloatingPointTextureTest_RG, reporter, ctxInfo) {
80 runFPTest<float>(reporter, ctxInfo.grContext(), FLT_MIN, FLT_MAX, FLT_EPSILON,
Brian Salomonc320b152018-02-20 14:05:36 -050081 kMaxIntegerRepresentableInSPFloatingPoint, RG32F_CONTROL_ARRAY_SIZE,
82 GrColorType::kRG_F32);
jvanverthfb5df432015-05-21 08:12:27 -070083}
mtkleincada95a2015-01-22 13:50:35 -080084
jvanverthfb5df432015-05-21 08:12:27 -070085static const int HALF_ALPHA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 1 /*alpha-only*/;
86static const SkHalf kMaxIntegerRepresentableInHalfFloatingPoint = 0x6800; // 2 ^ 11
jvanverth1334c212014-12-18 05:44:55 -080087
Brian Osman3a887252016-11-17 13:27:31 -050088DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatAlphaTextureTest, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070089 runFPTest<SkHalf>(reporter, ctxInfo.grContext(), SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
Brian Salomonc320b152018-02-20 14:05:36 -050090 kMaxIntegerRepresentableInHalfFloatingPoint, HALF_ALPHA_CONTROL_ARRAY_SIZE,
91 GrColorType::kAlpha_F16);
jvanverthfb5df432015-05-21 08:12:27 -070092}
jvanverth1334c212014-12-18 05:44:55 -080093
jvanverthfb5df432015-05-21 08:12:27 -070094static const int HALF_RGBA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4 /*RGBA*/;
jvanverth1334c212014-12-18 05:44:55 -080095
Brian Osman3a887252016-11-17 13:27:31 -050096DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatRGBATextureTest, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070097 runFPTest<SkHalf>(reporter, ctxInfo.grContext(), SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
Brian Salomonc320b152018-02-20 14:05:36 -050098 kMaxIntegerRepresentableInHalfFloatingPoint, HALF_RGBA_CONTROL_ARRAY_SIZE,
99 GrColorType::kRGBA_F16);
jvanverth28f9c602014-12-05 13:06:35 -0800100}