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