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 "GrContextPriv.h"
21 #include "GrProxyProvider.h"
22 #include "GrTextureProxy.h"
23 #include "SkHalf.h"
24
25 static const int DEV_W = 100, DEV_H = 100;
26
27 template <typename T>
runFPTest(skiatest::Reporter * reporter,GrContext * context,T min,T max,T epsilon,T maxInt,int arraySize,GrPixelConfig config)28 void runFPTest(skiatest::Reporter* reporter, GrContext* context,
29 T min, T max, T epsilon, T maxInt, int arraySize, GrPixelConfig config) {
30 if (0 != arraySize % 4) {
31 REPORT_FAILURE(reporter, "(0 != arraySize % 4)",
32 SkString("arraySize must be divisible by 4."));
33 return;
34 }
35
36 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
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 (int origin = 0; origin < 2; ++origin) {
49 GrSurfaceDesc desc;
50 desc.fFlags = kRenderTarget_GrSurfaceFlag;
51 desc.fOrigin = 0 == origin ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
52 desc.fWidth = DEV_W;
53 desc.fHeight = DEV_H;
54 desc.fConfig = config;
55
56 sk_sp<GrTextureProxy> fpProxy = proxyProvider->createTextureProxy(
57 desc, SkBudgeted::kNo, controlPixelData.begin(), 0);
58 // Floating point textures are NOT supported everywhere
59 if (!fpProxy) {
60 continue;
61 }
62
63 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(
64 std::move(fpProxy));
65 REPORTER_ASSERT(reporter, sContext);
66
67 bool result = context->contextPriv().readSurfacePixels(sContext.get(),
68 0, 0, DEV_W, DEV_H,
69 desc.fConfig, nullptr,
70 readBuffer.begin(), 0);
71 REPORTER_ASSERT(reporter, result);
72 REPORTER_ASSERT(reporter,
73 0 == memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.bytes()));
74 }
75 }
76
77 static const int RGBA32F_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4;
78 static const float kMaxIntegerRepresentableInSPFloatingPoint = 16777216; // 2 ^ 24
79
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FloatingPointTextureTest,reporter,ctxInfo)80 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FloatingPointTextureTest, reporter, ctxInfo) {
81 runFPTest<float>(reporter, ctxInfo.grContext(), FLT_MIN, FLT_MAX, FLT_EPSILON,
82 kMaxIntegerRepresentableInSPFloatingPoint,
83 RGBA32F_CONTROL_ARRAY_SIZE, kRGBA_float_GrPixelConfig);
84 }
85
86 static const int RG32F_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 2;
87
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FloatingPointTextureTest_RG,reporter,ctxInfo)88 DEF_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);
92 }
93
94 static const int HALF_ALPHA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 1 /*alpha-only*/;
95 static const SkHalf kMaxIntegerRepresentableInHalfFloatingPoint = 0x6800; // 2 ^ 11
96
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatAlphaTextureTest,reporter,ctxInfo)97 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatAlphaTextureTest, reporter, ctxInfo) {
98 runFPTest<SkHalf>(reporter, ctxInfo.grContext(), SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
99 kMaxIntegerRepresentableInHalfFloatingPoint,
100 HALF_ALPHA_CONTROL_ARRAY_SIZE, kAlpha_half_GrPixelConfig);
101 }
102
103 static const int HALF_RGBA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4 /*RGBA*/;
104
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatRGBATextureTest,reporter,ctxInfo)105 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatRGBATextureTest, reporter, ctxInfo) {
106 runFPTest<SkHalf>(reporter, ctxInfo.grContext(), SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
107 kMaxIntegerRepresentableInHalfFloatingPoint,
108 HALF_RGBA_CONTROL_ARRAY_SIZE, kRGBA_half_GrPixelConfig);
109 }
110
111 #endif
112