blob: 0880ef67bb52fea3c8ce82f5891f543212b0926d [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"
20#include "GrTexture.h"
jvanverth28f9c602014-12-05 13:06:35 -080021#include "SkHalf.h"
joshualittee5da552014-07-16 13:32:56 -070022
23static const int DEV_W = 100, DEV_H = 100;
joshualittee5da552014-07-16 13:32:56 -070024static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
25
jvanverthfb5df432015-05-21 08:12:27 -070026template <typename T>
kkinnunen15302832015-12-01 04:35:26 -080027void runFPTest(skiatest::Reporter* reporter, GrContext* context,
jvanverthfb5df432015-05-21 08:12:27 -070028 T min, T max, T epsilon, T maxInt, int arraySize, GrPixelConfig config) {
29 SkTDArray<T> controlPixelData, readBuffer;
30 controlPixelData.setCount(arraySize);
31 readBuffer.setCount(arraySize);
mtkleincada95a2015-01-22 13:50:35 -080032
jvanverthfb5df432015-05-21 08:12:27 -070033 for (int i = 0; i < arraySize; i += 4) {
34 controlPixelData[i + 0] = min;
35 controlPixelData[i + 1] = max;
36 controlPixelData[i + 2] = epsilon;
37 controlPixelData[i + 3] = maxInt;
joshualittee5da552014-07-16 13:32:56 -070038 }
39
40 for (int origin = 0; origin < 2; ++origin) {
kkinnunen15302832015-12-01 04:35:26 -080041 GrSurfaceDesc desc;
42 desc.fFlags = kRenderTarget_GrSurfaceFlag;
43 desc.fWidth = DEV_W;
44 desc.fHeight = DEV_H;
45 desc.fConfig = config;
46 desc.fOrigin = 0 == origin ?
jvanverthfb5df432015-05-21 08:12:27 -070047 kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
kkinnunen15302832015-12-01 04:35:26 -080048 SkAutoTUnref<GrTexture> fpTexture(context->textureProvider()->createTexture(
bsalomon5ec26ae2016-02-25 08:33:02 -080049 desc, SkBudgeted::kNo, controlPixelData.begin(), 0));
kkinnunen15302832015-12-01 04:35:26 -080050 // Floating point textures are NOT supported everywhere
51 if (nullptr == fpTexture) {
52 continue;
joshualittee5da552014-07-16 13:32:56 -070053 }
kkinnunen15302832015-12-01 04:35:26 -080054 fpTexture->readPixels(0, 0, DEV_W, DEV_H, desc.fConfig, readBuffer.begin(), 0);
55 REPORTER_ASSERT(reporter,
56 0 == memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.bytes()));
joshualittee5da552014-07-16 13:32:56 -070057 }
58}
59
jvanverthfb5df432015-05-21 08:12:27 -070060static const int FP_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4/*RGBA*/;
61static const float kMaxIntegerRepresentableInSPFloatingPoint = 16777216; // 2 ^ 24
jvanverth28f9c602014-12-05 13:06:35 -080062
bsalomon68d91342016-04-12 09:59:58 -070063DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FloatingPointTextureTest, reporter, ctxInfo) {
bsalomonf2f1c172016-04-05 12:59:06 -070064 runFPTest<float>(reporter, ctxInfo.fGrContext, FLT_MIN, FLT_MAX, FLT_EPSILON,
halcanary9d524f22016-03-29 09:03:52 -070065 kMaxIntegerRepresentableInSPFloatingPoint,
jvanverthfb5df432015-05-21 08:12:27 -070066 FP_CONTROL_ARRAY_SIZE, kRGBA_float_GrPixelConfig);
67}
mtkleincada95a2015-01-22 13:50:35 -080068
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
bsalomon68d91342016-04-12 09:59:58 -070072DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatAlphaTextureTest, reporter, ctxInfo) {
bsalomonf2f1c172016-04-05 12:59:06 -070073 runFPTest<SkHalf>(reporter, ctxInfo.fGrContext, SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
jvanverthfb5df432015-05-21 08:12:27 -070074 kMaxIntegerRepresentableInHalfFloatingPoint,
75 HALF_ALPHA_CONTROL_ARRAY_SIZE, kAlpha_half_GrPixelConfig);
76}
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
bsalomon68d91342016-04-12 09:59:58 -070080DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatRGBATextureTest, reporter, ctxInfo) {
bsalomonf2f1c172016-04-05 12:59:06 -070081 runFPTest<SkHalf>(reporter, ctxInfo.fGrContext, SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
jvanverthfb5df432015-05-21 08:12:27 -070082 kMaxIntegerRepresentableInHalfFloatingPoint,
83 HALF_RGBA_CONTROL_ARRAY_SIZE, kRGBA_half_GrPixelConfig);
jvanverth28f9c602014-12-05 13:06:35 -080084}
85
joshualittee5da552014-07-16 13:32:56 -070086#endif