FloatingPointTextureTest.cpp revision 13d7f5d7c2872ed4298330758e173ae605578cb2
1/*
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 */
14
15#include <float.h>
16#include "Test.h"
17
18#if SK_SUPPORT_GPU
19#include "GrContext.h"
20#include "GrTexture.h"
21#include "SkHalf.h"
22
23static const int DEV_W = 100, DEV_H = 100;
24static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
25
26template <typename T>
27void runFPTest(skiatest::Reporter* reporter, GrContext* context,
28               T min, T max, T epsilon, T maxInt, int arraySize, GrPixelConfig config) {
29    if (0 != arraySize % 4) {
30        REPORT_FAILURE(reporter, "(0 != arraySize % 4)",
31                       SkString("arraySize must be divisible by 4."));
32        return;
33    }
34
35    SkTDArray<T> controlPixelData, readBuffer;
36    controlPixelData.setCount(arraySize);
37    readBuffer.setCount(arraySize);
38
39    for (int i = 0; i < arraySize; i += 4) {
40        controlPixelData[i + 0] = min;
41        controlPixelData[i + 1] = max;
42        controlPixelData[i + 2] = epsilon;
43        controlPixelData[i + 3] = maxInt;
44    }
45
46    for (int origin = 0; origin < 2; ++origin) {
47        GrSurfaceDesc desc;
48        desc.fFlags = kRenderTarget_GrSurfaceFlag;
49        desc.fWidth = DEV_W;
50        desc.fHeight = DEV_H;
51        desc.fConfig = config;
52        desc.fOrigin = 0 == origin ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
53        sk_sp<GrTexture> fpTexture(context->textureProvider()->createTexture(
54            desc, SkBudgeted::kNo, controlPixelData.begin(), 0));
55        // Floating point textures are NOT supported everywhere
56        if (nullptr == fpTexture) {
57            continue;
58        }
59        REPORTER_ASSERT(reporter,
60                        fpTexture->readPixels(0, 0, DEV_W, DEV_H, desc.fConfig, readBuffer.begin(), 0));
61        REPORTER_ASSERT(reporter,
62                        0 == memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.bytes()));
63    }
64}
65
66static const int RGBA32F_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4;
67static const float kMaxIntegerRepresentableInSPFloatingPoint = 16777216;  // 2 ^ 24
68
69DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FloatingPointTextureTest, reporter, ctxInfo) {
70    runFPTest<float>(reporter, ctxInfo.grContext(), FLT_MIN, FLT_MAX, FLT_EPSILON,
71                     kMaxIntegerRepresentableInSPFloatingPoint,
72                     RGBA32F_CONTROL_ARRAY_SIZE, kRGBA_float_GrPixelConfig);
73}
74
75static const int RG32F_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 2;
76
77DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FloatingPointTextureTest_RG, reporter, ctxInfo) {
78    runFPTest<float>(reporter, ctxInfo.grContext(), FLT_MIN, FLT_MAX, FLT_EPSILON,
79                     kMaxIntegerRepresentableInSPFloatingPoint,
80                     RG32F_CONTROL_ARRAY_SIZE, kRG_float_GrPixelConfig);
81}
82
83static const int HALF_ALPHA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 1 /*alpha-only*/;
84static const SkHalf kMaxIntegerRepresentableInHalfFloatingPoint = 0x6800;  // 2 ^ 11
85
86DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatAlphaTextureTest, reporter, ctxInfo) {
87    runFPTest<SkHalf>(reporter, ctxInfo.grContext(), SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
88        kMaxIntegerRepresentableInHalfFloatingPoint,
89        HALF_ALPHA_CONTROL_ARRAY_SIZE, kAlpha_half_GrPixelConfig);
90}
91
92static const int HALF_RGBA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4 /*RGBA*/;
93
94DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatRGBATextureTest, reporter, ctxInfo) {
95    runFPTest<SkHalf>(reporter, ctxInfo.grContext(), SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
96        kMaxIntegerRepresentableInHalfFloatingPoint,
97        HALF_RGBA_CONTROL_ARRAY_SIZE, kRGBA_half_GrPixelConfig);
98}
99
100#endif
101