blob: 178392a1528fbe9b918202d46f8612b4d950620b [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 <float.h>
16#include "Test.h"
kkinnunen15302832015-12-01 04:35:26 -080017
joshualittee5da552014-07-16 13:32:56 -070018#include "GrContext.h"
Robert Phillipse78b7252017-04-06 07:59:41 -040019#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050020#include "GrProxyProvider.h"
Robert Phillips009e9af2017-06-15 14:01:04 -040021#include "GrTextureProxy.h"
Brian Salomon58389b92018-03-07 13:01:25 -050022#include "ProxyUtils.h"
jvanverth28f9c602014-12-05 13:06:35 -080023#include "SkHalf.h"
joshualittee5da552014-07-16 13:32:56 -070024
25static const int DEV_W = 100, DEV_H = 100;
joshualittee5da552014-07-16 13:32:56 -070026
jvanverthfb5df432015-05-21 08:12:27 -070027template <typename T>
Brian Salomonc320b152018-02-20 14:05:36 -050028void runFPTest(skiatest::Reporter* reporter, GrContext* context, T min, T max, T epsilon, T maxInt,
29 int arraySize, GrColorType colorType) {
csmartdalton6aa0e112017-02-08 16:14:11 -050030 if (0 != arraySize % 4) {
31 REPORT_FAILURE(reporter, "(0 != arraySize % 4)",
32 SkString("arraySize must be divisible by 4."));
33 return;
34 }
35
jvanverthfb5df432015-05-21 08:12:27 -070036 SkTDArray<T> controlPixelData, readBuffer;
37 controlPixelData.setCount(arraySize);
38 readBuffer.setCount(arraySize);
mtkleincada95a2015-01-22 13:50:35 -080039
jvanverthfb5df432015-05-21 08:12:27 -070040 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;
joshualittee5da552014-07-16 13:32:56 -070045 }
46
Brian Salomon2a4f9832018-03-03 22:43:43 -050047 for (auto origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
Brian Salomon58389b92018-03-07 13:01:25 -050048 auto fpProxy = sk_gpu_test::MakeTextureProxyFromData(context, true, DEV_W, DEV_H, colorType,
49 origin, controlPixelData.begin(), 0);
kkinnunen15302832015-12-01 04:35:26 -080050 // Floating point textures are NOT supported everywhere
Robert Phillipse78b7252017-04-06 07:59:41 -040051 if (!fpProxy) {
kkinnunen15302832015-12-01 04:35:26 -080052 continue;
joshualittee5da552014-07-16 13:32:56 -070053 }
Robert Phillipsf41c22f2017-04-18 07:48:58 -040054
55 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050056 std::move(fpProxy));
Robert Phillipsf41c22f2017-04-18 07:48:58 -040057 REPORTER_ASSERT(reporter, sContext);
58
Brian Salomonc320b152018-02-20 14:05:36 -050059 bool result = context->contextPriv().readSurfacePixels(
60 sContext.get(), 0, 0, DEV_W, DEV_H, colorType, nullptr, readBuffer.begin(), 0);
Robert Phillipse78b7252017-04-06 07:59:41 -040061 REPORTER_ASSERT(reporter, result);
kkinnunen15302832015-12-01 04:35:26 -080062 REPORTER_ASSERT(reporter,
63 0 == memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.bytes()));
joshualittee5da552014-07-16 13:32:56 -070064 }
65}
66
csmartdalton6aa0e112017-02-08 16:14:11 -050067static const int RGBA32F_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4;
jvanverthfb5df432015-05-21 08:12:27 -070068static const float kMaxIntegerRepresentableInSPFloatingPoint = 16777216; // 2 ^ 24
jvanverth28f9c602014-12-05 13:06:35 -080069
bsalomon68d91342016-04-12 09:59:58 -070070DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FloatingPointTextureTest, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070071 runFPTest<float>(reporter, ctxInfo.grContext(), FLT_MIN, FLT_MAX, FLT_EPSILON,
Brian Salomonc320b152018-02-20 14:05:36 -050072 kMaxIntegerRepresentableInSPFloatingPoint, RGBA32F_CONTROL_ARRAY_SIZE,
73 GrColorType::kRGBA_F32);
csmartdalton6aa0e112017-02-08 16:14:11 -050074}
75
76static const int RG32F_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 2;
77
78DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FloatingPointTextureTest_RG, reporter, ctxInfo) {
79 runFPTest<float>(reporter, ctxInfo.grContext(), FLT_MIN, FLT_MAX, FLT_EPSILON,
Brian Salomonc320b152018-02-20 14:05:36 -050080 kMaxIntegerRepresentableInSPFloatingPoint, RG32F_CONTROL_ARRAY_SIZE,
81 GrColorType::kRG_F32);
jvanverthfb5df432015-05-21 08:12:27 -070082}
mtkleincada95a2015-01-22 13:50:35 -080083
jvanverthfb5df432015-05-21 08:12:27 -070084static const int HALF_ALPHA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 1 /*alpha-only*/;
85static const SkHalf kMaxIntegerRepresentableInHalfFloatingPoint = 0x6800; // 2 ^ 11
jvanverth1334c212014-12-18 05:44:55 -080086
Brian Osman3a887252016-11-17 13:27:31 -050087DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatAlphaTextureTest, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070088 runFPTest<SkHalf>(reporter, ctxInfo.grContext(), SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
Brian Salomonc320b152018-02-20 14:05:36 -050089 kMaxIntegerRepresentableInHalfFloatingPoint, HALF_ALPHA_CONTROL_ARRAY_SIZE,
90 GrColorType::kAlpha_F16);
jvanverthfb5df432015-05-21 08:12:27 -070091}
jvanverth1334c212014-12-18 05:44:55 -080092
jvanverthfb5df432015-05-21 08:12:27 -070093static const int HALF_RGBA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4 /*RGBA*/;
jvanverth1334c212014-12-18 05:44:55 -080094
Brian Osman3a887252016-11-17 13:27:31 -050095DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatRGBATextureTest, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070096 runFPTest<SkHalf>(reporter, ctxInfo.grContext(), SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
Brian Salomonc320b152018-02-20 14:05:36 -050097 kMaxIntegerRepresentableInHalfFloatingPoint, HALF_RGBA_CONTROL_ARRAY_SIZE,
98 GrColorType::kRGBA_F16);
jvanverth28f9c602014-12-05 13:06:35 -080099}