blob: 1adc7442af610f7cc9315eb832e282724ff01fcc [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"
bsalomond309e7a2015-04-30 14:18:54 -070017#if SK_SUPPORT_GPU
joshualittee5da552014-07-16 13:32:56 -070018#include "GrContext.h"
19#include "GrTexture.h"
20#include "GrContextFactory.h"
jvanverth28f9c602014-12-05 13:06:35 -080021
joshualittee5da552014-07-16 13:32:56 -070022#include "SkGpuDevice.h"
jvanverth28f9c602014-12-05 13:06:35 -080023#include "SkHalf.h"
joshualittee5da552014-07-16 13:32:56 -070024
25static const int DEV_W = 100, DEV_H = 100;
joshualittee5da552014-07-16 13:32:56 -070026static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
27
jvanverthfb5df432015-05-21 08:12:27 -070028template <typename T>
29void runFPTest(skiatest::Reporter* reporter, GrContextFactory* factory,
30 T min, T max, T epsilon, T maxInt, int arraySize, GrPixelConfig config) {
31 SkTDArray<T> controlPixelData, readBuffer;
32 controlPixelData.setCount(arraySize);
33 readBuffer.setCount(arraySize);
mtkleincada95a2015-01-22 13:50:35 -080034
jvanverthfb5df432015-05-21 08:12:27 -070035 for (int i = 0; i < arraySize; i += 4) {
36 controlPixelData[i + 0] = min;
37 controlPixelData[i + 1] = max;
38 controlPixelData[i + 2] = epsilon;
39 controlPixelData[i + 3] = maxInt;
joshualittee5da552014-07-16 13:32:56 -070040 }
41
42 for (int origin = 0; origin < 2; ++origin) {
mtklein2f6bb6b2015-01-23 05:47:55 -080043 for (int glCtxType = 0; glCtxType < GrContextFactory::kGLContextTypeCnt; ++glCtxType) {
bsalomonf2703d82014-10-28 14:33:06 -070044 GrSurfaceDesc desc;
jvanverthfb5df432015-05-21 08:12:27 -070045 desc.fFlags = kRenderTarget_GrSurfaceFlag;
46 desc.fWidth = DEV_W;
joshualittee5da552014-07-16 13:32:56 -070047 desc.fHeight = DEV_H;
jvanverthfb5df432015-05-21 08:12:27 -070048 desc.fConfig = config;
joshualittee5da552014-07-16 13:32:56 -070049 desc.fOrigin = 0 == origin ?
jvanverthfb5df432015-05-21 08:12:27 -070050 kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
joshualittee5da552014-07-16 13:32:56 -070051
joshualittee5da552014-07-16 13:32:56 -070052 GrContextFactory::GLContextType type =
mtklein2f6bb6b2015-01-23 05:47:55 -080053 static_cast<GrContextFactory::GLContextType>(glCtxType);
joshualittee5da552014-07-16 13:32:56 -070054 if (!GrContextFactory::IsRenderingGLContext(type)) {
55 continue;
56 }
mtklein2f6bb6b2015-01-23 05:47:55 -080057 GrContext* context = factory->get(type);
jvanverthfb5df432015-05-21 08:12:27 -070058 if (NULL == context) {
joshualittee5da552014-07-16 13:32:56 -070059 continue;
60 }
61
bsalomond309e7a2015-04-30 14:18:54 -070062 SkAutoTUnref<GrTexture> fpTexture(context->textureProvider()->createTexture(
63 desc, false, controlPixelData.begin(), 0));
joshualittee5da552014-07-16 13:32:56 -070064 // Floating point textures are NOT supported everywhere
65 if (NULL == fpTexture) {
66 continue;
67 }
mtkleincada95a2015-01-22 13:50:35 -080068 fpTexture->readPixels(0, 0, DEV_W, DEV_H, desc.fConfig, readBuffer.begin(), 0);
mtklein2f6bb6b2015-01-23 05:47:55 -080069 REPORTER_ASSERT(reporter,
jvanverthfb5df432015-05-21 08:12:27 -070070 0 == memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.bytes()));
joshualittee5da552014-07-16 13:32:56 -070071 }
72 }
73}
74
jvanverthfb5df432015-05-21 08:12:27 -070075static const int FP_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4/*RGBA*/;
76static const float kMaxIntegerRepresentableInSPFloatingPoint = 16777216; // 2 ^ 24
jvanverth28f9c602014-12-05 13:06:35 -080077
jvanverthfb5df432015-05-21 08:12:27 -070078DEF_GPUTEST(FloatingPointTextureTest, reporter, factory) {
79 runFPTest<float>(reporter, factory, FLT_MIN, FLT_MAX, FLT_EPSILON,
80 kMaxIntegerRepresentableInSPFloatingPoint,
81 FP_CONTROL_ARRAY_SIZE, kRGBA_float_GrPixelConfig);
82}
mtkleincada95a2015-01-22 13:50:35 -080083
jvanverthfb5df432015-05-21 08:12:27 -070084static const int HALF_ALPHA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 1 /*alpha-only*/;
85static const SkHalf kMaxIntegerRepresentableInHalfFloatingPoint = 0x6800; // 2 ^ 11
jvanverth1334c212014-12-18 05:44:55 -080086
jvanverthfb5df432015-05-21 08:12:27 -070087DEF_GPUTEST(HalfFloatAlphaTextureTest, reporter, factory) {
88 runFPTest<SkHalf>(reporter, factory, SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
89 kMaxIntegerRepresentableInHalfFloatingPoint,
90 HALF_ALPHA_CONTROL_ARRAY_SIZE, kAlpha_half_GrPixelConfig);
91}
jvanverth1334c212014-12-18 05:44:55 -080092
jvanverthfb5df432015-05-21 08:12:27 -070093static const int HALF_RGBA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4 /*RGBA*/;
jvanverth1334c212014-12-18 05:44:55 -080094
jvanverthfb5df432015-05-21 08:12:27 -070095DEF_GPUTEST(HalfFloatRGBATextureTest, reporter, factory) {
96 runFPTest<SkHalf>(reporter, factory, SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
97 kMaxIntegerRepresentableInHalfFloatingPoint,
98 HALF_RGBA_CONTROL_ARRAY_SIZE, kRGBA_half_GrPixelConfig);
jvanverth28f9c602014-12-05 13:06:35 -080099}
100
joshualittee5da552014-07-16 13:32:56 -0700101#endif