blob: 816b0d37d804a760f29991c30b5b335661584751 [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
bsalomond309e7a2015-04-30 14:18:54 -070018#if SK_SUPPORT_GPU
joshualittee5da552014-07-16 13:32:56 -070019#include "GrContext.h"
Robert Phillipse78b7252017-04-06 07:59:41 -040020#include "GrContextPriv.h"
Brian Osman32342f02017-03-04 08:12:46 -050021#include "GrResourceProvider.h"
Robert Phillips009e9af2017-06-15 14:01:04 -040022#include "GrTextureProxy.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 -070026static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
27
jvanverthfb5df432015-05-21 08:12:27 -070028template <typename T>
kkinnunen15302832015-12-01 04:35:26 -080029void runFPTest(skiatest::Reporter* reporter, GrContext* context,
jvanverthfb5df432015-05-21 08:12:27 -070030 T min, T max, T epsilon, T maxInt, int arraySize, GrPixelConfig config) {
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
48 for (int origin = 0; origin < 2; ++origin) {
kkinnunen15302832015-12-01 04:35:26 -080049 GrSurfaceDesc desc;
50 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -040051 desc.fOrigin = 0 == origin ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
kkinnunen15302832015-12-01 04:35:26 -080052 desc.fWidth = DEV_W;
53 desc.fHeight = DEV_H;
54 desc.fConfig = config;
Robert Phillipse78b7252017-04-06 07:59:41 -040055 sk_sp<GrTextureProxy> fpProxy = GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
56 desc, SkBudgeted::kNo,
57 controlPixelData.begin(), 0);
kkinnunen15302832015-12-01 04:35:26 -080058 // Floating point textures are NOT supported everywhere
Robert Phillipse78b7252017-04-06 07:59:41 -040059 if (!fpProxy) {
kkinnunen15302832015-12-01 04:35:26 -080060 continue;
joshualittee5da552014-07-16 13:32:56 -070061 }
Robert Phillipsf41c22f2017-04-18 07:48:58 -040062
63 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(
64 std::move(fpProxy), nullptr);
65 REPORTER_ASSERT(reporter, sContext);
66
67 bool result = context->contextPriv().readSurfacePixels(sContext.get(),
Robert Phillipse78b7252017-04-06 07:59:41 -040068 0, 0, DEV_W, DEV_H,
69 desc.fConfig, nullptr,
70 readBuffer.begin(), 0);
71 REPORTER_ASSERT(reporter, result);
kkinnunen15302832015-12-01 04:35:26 -080072 REPORTER_ASSERT(reporter,
73 0 == memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.bytes()));
joshualittee5da552014-07-16 13:32:56 -070074 }
75}
76
csmartdalton6aa0e112017-02-08 16:14:11 -050077static const int RGBA32F_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4;
jvanverthfb5df432015-05-21 08:12:27 -070078static const float kMaxIntegerRepresentableInSPFloatingPoint = 16777216; // 2 ^ 24
jvanverth28f9c602014-12-05 13:06:35 -080079
bsalomon68d91342016-04-12 09:59:58 -070080DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FloatingPointTextureTest, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070081 runFPTest<float>(reporter, ctxInfo.grContext(), FLT_MIN, FLT_MAX, FLT_EPSILON,
halcanary9d524f22016-03-29 09:03:52 -070082 kMaxIntegerRepresentableInSPFloatingPoint,
csmartdalton6aa0e112017-02-08 16:14:11 -050083 RGBA32F_CONTROL_ARRAY_SIZE, kRGBA_float_GrPixelConfig);
84}
85
86static const int RG32F_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 2;
87
88DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FloatingPointTextureTest_RG, reporter, ctxInfo) {
89 runFPTest<float>(reporter, ctxInfo.grContext(), FLT_MIN, FLT_MAX, FLT_EPSILON,
90 kMaxIntegerRepresentableInSPFloatingPoint,
91 RG32F_CONTROL_ARRAY_SIZE, kRG_float_GrPixelConfig);
jvanverthfb5df432015-05-21 08:12:27 -070092}
mtkleincada95a2015-01-22 13:50:35 -080093
jvanverthfb5df432015-05-21 08:12:27 -070094static const int HALF_ALPHA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 1 /*alpha-only*/;
95static const SkHalf kMaxIntegerRepresentableInHalfFloatingPoint = 0x6800; // 2 ^ 11
jvanverth1334c212014-12-18 05:44:55 -080096
Brian Osman3a887252016-11-17 13:27:31 -050097DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatAlphaTextureTest, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070098 runFPTest<SkHalf>(reporter, ctxInfo.grContext(), SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
jvanverthfb5df432015-05-21 08:12:27 -070099 kMaxIntegerRepresentableInHalfFloatingPoint,
100 HALF_ALPHA_CONTROL_ARRAY_SIZE, kAlpha_half_GrPixelConfig);
101}
jvanverth1334c212014-12-18 05:44:55 -0800102
jvanverthfb5df432015-05-21 08:12:27 -0700103static const int HALF_RGBA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4 /*RGBA*/;
jvanverth1334c212014-12-18 05:44:55 -0800104
Brian Osman3a887252016-11-17 13:27:31 -0500105DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatRGBATextureTest, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700106 runFPTest<SkHalf>(reporter, ctxInfo.grContext(), SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
jvanverthfb5df432015-05-21 08:12:27 -0700107 kMaxIntegerRepresentableInHalfFloatingPoint,
108 HALF_RGBA_CONTROL_ARRAY_SIZE, kRGBA_half_GrPixelConfig);
jvanverth28f9c602014-12-05 13:06:35 -0800109}
110
joshualittee5da552014-07-16 13:32:56 -0700111#endif