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 "Test.h"
16
17 #include "GrContext.h"
18 #include "GrContextPriv.h"
19 #include "GrProxyProvider.h"
20 #include "GrTextureProxy.h"
21 #include "ProxyUtils.h"
22 #include "SkHalf.h"
23
24 #include <float.h>
25
26 static const int DEV_W = 100, DEV_H = 100;
27
28 template <typename T>
runFPTest(skiatest::Reporter * reporter,GrContext * context,T min,T max,T epsilon,T maxInt,int arraySize,GrColorType colorType)29 void runFPTest(skiatest::Reporter* reporter, GrContext* context, T min, T max, T epsilon, T maxInt,
30 int arraySize, GrColorType colorType) {
31 if (0 != arraySize % 4) {
32 REPORT_FAILURE(reporter, "(0 != arraySize % 4)",
33 SkString("arraySize must be divisible by 4."));
34 return;
35 }
36
37 SkTDArray<T> controlPixelData, readBuffer;
38 controlPixelData.setCount(arraySize);
39 readBuffer.setCount(arraySize);
40
41 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;
46 }
47
48 for (auto origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
49 auto fpProxy = sk_gpu_test::MakeTextureProxyFromData(context, true, DEV_W, DEV_H, colorType,
50 origin, controlPixelData.begin(), 0);
51 // Floating point textures are NOT supported everywhere
52 if (!fpProxy) {
53 continue;
54 }
55
56 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(
57 std::move(fpProxy));
58 REPORTER_ASSERT(reporter, sContext);
59
60 bool result = context->contextPriv().readSurfacePixels(
61 sContext.get(), 0, 0, DEV_W, DEV_H, colorType, nullptr, readBuffer.begin(), 0);
62 REPORTER_ASSERT(reporter, result);
63 REPORTER_ASSERT(reporter,
64 0 == memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.bytes()));
65 }
66 }
67
68 static const int RGBA32F_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4;
69 static const float kMaxIntegerRepresentableInSPFloatingPoint = 16777216; // 2 ^ 24
70
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FloatingPointTextureTest,reporter,ctxInfo)71 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FloatingPointTextureTest, reporter, ctxInfo) {
72 runFPTest<float>(reporter, ctxInfo.grContext(), FLT_MIN, FLT_MAX, FLT_EPSILON,
73 kMaxIntegerRepresentableInSPFloatingPoint, RGBA32F_CONTROL_ARRAY_SIZE,
74 GrColorType::kRGBA_F32);
75 }
76
77 static const int RG32F_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 2;
78
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FloatingPointTextureTest_RG,reporter,ctxInfo)79 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FloatingPointTextureTest_RG, reporter, ctxInfo) {
80 runFPTest<float>(reporter, ctxInfo.grContext(), FLT_MIN, FLT_MAX, FLT_EPSILON,
81 kMaxIntegerRepresentableInSPFloatingPoint, RG32F_CONTROL_ARRAY_SIZE,
82 GrColorType::kRG_F32);
83 }
84
85 static const int HALF_ALPHA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 1 /*alpha-only*/;
86 static const SkHalf kMaxIntegerRepresentableInHalfFloatingPoint = 0x6800; // 2 ^ 11
87
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatAlphaTextureTest,reporter,ctxInfo)88 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatAlphaTextureTest, reporter, ctxInfo) {
89 runFPTest<SkHalf>(reporter, ctxInfo.grContext(), SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
90 kMaxIntegerRepresentableInHalfFloatingPoint, HALF_ALPHA_CONTROL_ARRAY_SIZE,
91 GrColorType::kAlpha_F16);
92 }
93
94 static const int HALF_RGBA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4 /*RGBA*/;
95
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatRGBATextureTest,reporter,ctxInfo)96 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatRGBATextureTest, reporter, ctxInfo) {
97 runFPTest<SkHalf>(reporter, ctxInfo.grContext(), SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
98 kMaxIntegerRepresentableInHalfFloatingPoint, HALF_RGBA_CONTROL_ARRAY_SIZE,
99 GrColorType::kRGBA_F16);
100 }
101