blob: b4ca59ffc6099a7f96c235ed4b904bed3b3ed163 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "tests/Test.h"
kkinnunen15302832015-12-01 04:35:26 -080016
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/private/SkHalf.h"
19#include "src/gpu/GrContextPriv.h"
20#include "src/gpu/GrProxyProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040021#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "tools/gpu/ProxyUtils.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 Salomone7499c72019-06-24 12:12:36 -040049 auto fpProxy = sk_gpu_test::MakeTextureProxyFromData(context, GrRenderable::kYes, DEV_W,
50 DEV_H, colorType, kPremul_SkAlphaType,
51 origin, controlPixelData.begin(), 0);
kkinnunen15302832015-12-01 04:35:26 -080052 // Floating point textures are NOT supported everywhere
Robert Phillipse78b7252017-04-06 07:59:41 -040053 if (!fpProxy) {
kkinnunen15302832015-12-01 04:35:26 -080054 continue;
joshualittee5da552014-07-16 13:32:56 -070055 }
Robert Phillipsf41c22f2017-04-18 07:48:58 -040056
Brian Salomond6287472019-06-24 15:50:07 -040057 sk_sp<GrSurfaceContext> sContext = context->priv().makeWrappedSurfaceContext(
58 std::move(fpProxy), colorType, kPremul_SkAlphaType);
Robert Phillipsf41c22f2017-04-18 07:48:58 -040059 REPORTER_ASSERT(reporter, sContext);
60
Brian Salomon1d435302019-07-01 13:05:28 -040061 bool result = sContext->readPixels({colorType, kPremul_SkAlphaType, nullptr, DEV_W, DEV_H},
62 readBuffer.begin(), 0, {0, 0}, context);
Robert Phillipse78b7252017-04-06 07:59:41 -040063 REPORTER_ASSERT(reporter, result);
kkinnunen15302832015-12-01 04:35:26 -080064 REPORTER_ASSERT(reporter,
65 0 == memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.bytes()));
joshualittee5da552014-07-16 13:32:56 -070066 }
67}
68
jvanverthfb5df432015-05-21 08:12:27 -070069static const int HALF_ALPHA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 1 /*alpha-only*/;
70static const SkHalf kMaxIntegerRepresentableInHalfFloatingPoint = 0x6800; // 2 ^ 11
jvanverth1334c212014-12-18 05:44:55 -080071
Brian Osman3a887252016-11-17 13:27:31 -050072DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatAlphaTextureTest, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070073 runFPTest<SkHalf>(reporter, ctxInfo.grContext(), SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
Brian Salomonc320b152018-02-20 14:05:36 -050074 kMaxIntegerRepresentableInHalfFloatingPoint, HALF_ALPHA_CONTROL_ARRAY_SIZE,
75 GrColorType::kAlpha_F16);
jvanverthfb5df432015-05-21 08:12:27 -070076}
jvanverth1334c212014-12-18 05:44:55 -080077
jvanverthfb5df432015-05-21 08:12:27 -070078static const int HALF_RGBA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4 /*RGBA*/;
jvanverth1334c212014-12-18 05:44:55 -080079
Brian Osman3a887252016-11-17 13:27:31 -050080DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatRGBATextureTest, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070081 runFPTest<SkHalf>(reporter, ctxInfo.grContext(), SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
Brian Salomonc320b152018-02-20 14:05:36 -050082 kMaxIntegerRepresentableInHalfFloatingPoint, HALF_RGBA_CONTROL_ARRAY_SIZE,
83 GrColorType::kRGBA_F16);
jvanverth28f9c602014-12-05 13:06:35 -080084}