blob: 59b041843c9fcf40009b267de5f56bd218b50d82 [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
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040017#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/private/SkHalf.h"
Adlai Hollera0693042020-10-14 11:23:11 -040019#include "src/gpu/GrDirectContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040020#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrProxyProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040022#include "src/gpu/GrTextureProxy.h"
Robert Phillips53eaa642021-08-10 13:49:51 -040023#include "src/gpu/SurfaceContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "tools/gpu/ProxyUtils.h"
joshualittee5da552014-07-16 13:32:56 -070025
Hal Canary8a001442018-09-19 11:31:27 -040026#include <float.h>
27
joshualittee5da552014-07-16 13:32:56 -070028static const int DEV_W = 100, DEV_H = 100;
joshualittee5da552014-07-16 13:32:56 -070029
jvanverthfb5df432015-05-21 08:12:27 -070030template <typename T>
Adlai Hollerc95b5892020-08-11 12:02:22 -040031void runFPTest(skiatest::Reporter* reporter, GrDirectContext* dContext,
Robert Phillips44333c52020-06-30 13:28:00 -040032 T min, T max, T epsilon, T maxInt,
Brian Salomonc320b152018-02-20 14:05:36 -050033 int arraySize, GrColorType colorType) {
csmartdalton6aa0e112017-02-08 16:14:11 -050034 if (0 != arraySize % 4) {
35 REPORT_FAILURE(reporter, "(0 != arraySize % 4)",
36 SkString("arraySize must be divisible by 4."));
37 return;
38 }
39
jvanverthfb5df432015-05-21 08:12:27 -070040 SkTDArray<T> controlPixelData, readBuffer;
41 controlPixelData.setCount(arraySize);
42 readBuffer.setCount(arraySize);
mtkleincada95a2015-01-22 13:50:35 -080043
jvanverthfb5df432015-05-21 08:12:27 -070044 for (int i = 0; i < arraySize; i += 4) {
45 controlPixelData[i + 0] = min;
46 controlPixelData[i + 1] = max;
47 controlPixelData[i + 2] = epsilon;
48 controlPixelData[i + 3] = maxInt;
joshualittee5da552014-07-16 13:32:56 -070049 }
50
Brian Salomon2a4f9832018-03-03 22:43:43 -050051 for (auto origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
Brian Salomon14f99fc2020-12-07 12:19:47 -050052 GrImageInfo info(colorType, kPremul_SkAlphaType, nullptr, {DEV_W, DEV_H});
Brian Salomon5392c942021-03-30 16:14:37 -040053 GrCPixmap controlPixmap(info, controlPixelData.begin(), info.minRowBytes());
Brian Salomondd4087d2020-12-23 20:36:44 -050054 auto fpView = sk_gpu_test::MakeTextureProxyViewFromData(dContext,
55 GrRenderable::kYes,
56 origin,
57 controlPixmap);
kkinnunen15302832015-12-01 04:35:26 -080058 // Floating point textures are NOT supported everywhere
Brian Salomon652124c2020-08-12 14:06:50 -040059 if (!fpView) {
kkinnunen15302832015-12-01 04:35:26 -080060 continue;
joshualittee5da552014-07-16 13:32:56 -070061 }
Robert Phillipsf41c22f2017-04-18 07:48:58 -040062
Robert Phillips643f4812021-08-11 09:31:00 -040063 auto sc = dContext->priv().makeSC(std::move(fpView), info.colorInfo());
64 REPORTER_ASSERT(reporter, sc);
Robert Phillipsf41c22f2017-04-18 07:48:58 -040065
Brian Salomondd4087d2020-12-23 20:36:44 -050066 GrPixmap readPixmap(info, readBuffer.begin(), info.minRowBytes());
Robert Phillips643f4812021-08-11 09:31:00 -040067 bool result = sc->readPixels(dContext, readPixmap, {0, 0});
Robert Phillipse78b7252017-04-06 07:59:41 -040068 REPORTER_ASSERT(reporter, result);
kkinnunen15302832015-12-01 04:35:26 -080069 REPORTER_ASSERT(reporter,
Brian Salomon652124c2020-08-12 14:06:50 -040070 !memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.bytes()));
joshualittee5da552014-07-16 13:32:56 -070071 }
72}
73
jvanverthfb5df432015-05-21 08:12:27 -070074static const int HALF_ALPHA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 1 /*alpha-only*/;
75static const SkHalf kMaxIntegerRepresentableInHalfFloatingPoint = 0x6800; // 2 ^ 11
jvanverth1334c212014-12-18 05:44:55 -080076
Brian Osman3a887252016-11-17 13:27:31 -050077DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatAlphaTextureTest, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040078 auto direct = ctxInfo.directContext();
Robert Phillips44333c52020-06-30 13:28:00 -040079
80 runFPTest<SkHalf>(reporter, direct, SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
Brian Salomonc320b152018-02-20 14:05:36 -050081 kMaxIntegerRepresentableInHalfFloatingPoint, HALF_ALPHA_CONTROL_ARRAY_SIZE,
82 GrColorType::kAlpha_F16);
jvanverthfb5df432015-05-21 08:12:27 -070083}
jvanverth1334c212014-12-18 05:44:55 -080084
jvanverthfb5df432015-05-21 08:12:27 -070085static const int HALF_RGBA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4 /*RGBA*/;
jvanverth1334c212014-12-18 05:44:55 -080086
Brian Osman3a887252016-11-17 13:27:31 -050087DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatRGBATextureTest, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040088 auto direct = ctxInfo.directContext();
Robert Phillips44333c52020-06-30 13:28:00 -040089
90 runFPTest<SkHalf>(reporter, direct, SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
Brian Salomonc320b152018-02-20 14:05:36 -050091 kMaxIntegerRepresentableInHalfFloatingPoint, HALF_RGBA_CONTROL_ARRAY_SIZE,
92 GrColorType::kRGBA_F16);
jvanverth28f9c602014-12-05 13:06:35 -080093}