joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 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 | */ |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 14 | |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 15 | #include <float.h> |
| 16 | #include "Test.h" |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 17 | #if SK_SUPPORT_GPU |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 18 | #include "GrContext.h" |
| 19 | #include "GrTexture.h" |
| 20 | #include "GrContextFactory.h" |
jvanverth | 28f9c60 | 2014-12-05 13:06:35 -0800 | [diff] [blame] | 21 | |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 22 | #include "SkGpuDevice.h" |
jvanverth | 28f9c60 | 2014-12-05 13:06:35 -0800 | [diff] [blame] | 23 | #include "SkHalf.h" |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 24 | |
| 25 | static const int DEV_W = 100, DEV_H = 100; |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 26 | static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H); |
| 27 | |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame^] | 28 | template <typename T> |
| 29 | void 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); |
mtklein | cada95a | 2015-01-22 13:50:35 -0800 | [diff] [blame] | 34 | |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame^] | 35 | 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; |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | for (int origin = 0; origin < 2; ++origin) { |
mtklein | 2f6bb6b | 2015-01-23 05:47:55 -0800 | [diff] [blame] | 43 | for (int glCtxType = 0; glCtxType < GrContextFactory::kGLContextTypeCnt; ++glCtxType) { |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 44 | GrSurfaceDesc desc; |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame^] | 45 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 46 | desc.fWidth = DEV_W; |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 47 | desc.fHeight = DEV_H; |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame^] | 48 | desc.fConfig = config; |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 49 | desc.fOrigin = 0 == origin ? |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame^] | 50 | kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin; |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 51 | |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 52 | GrContextFactory::GLContextType type = |
mtklein | 2f6bb6b | 2015-01-23 05:47:55 -0800 | [diff] [blame] | 53 | static_cast<GrContextFactory::GLContextType>(glCtxType); |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 54 | if (!GrContextFactory::IsRenderingGLContext(type)) { |
| 55 | continue; |
| 56 | } |
mtklein | 2f6bb6b | 2015-01-23 05:47:55 -0800 | [diff] [blame] | 57 | GrContext* context = factory->get(type); |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame^] | 58 | if (NULL == context) { |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 59 | continue; |
| 60 | } |
| 61 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 62 | SkAutoTUnref<GrTexture> fpTexture(context->textureProvider()->createTexture( |
| 63 | desc, false, controlPixelData.begin(), 0)); |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 64 | // Floating point textures are NOT supported everywhere |
| 65 | if (NULL == fpTexture) { |
| 66 | continue; |
| 67 | } |
mtklein | cada95a | 2015-01-22 13:50:35 -0800 | [diff] [blame] | 68 | fpTexture->readPixels(0, 0, DEV_W, DEV_H, desc.fConfig, readBuffer.begin(), 0); |
mtklein | 2f6bb6b | 2015-01-23 05:47:55 -0800 | [diff] [blame] | 69 | REPORTER_ASSERT(reporter, |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame^] | 70 | 0 == memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.bytes())); |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame^] | 75 | static const int FP_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4/*RGBA*/; |
| 76 | static const float kMaxIntegerRepresentableInSPFloatingPoint = 16777216; // 2 ^ 24 |
jvanverth | 28f9c60 | 2014-12-05 13:06:35 -0800 | [diff] [blame] | 77 | |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame^] | 78 | DEF_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 | } |
mtklein | cada95a | 2015-01-22 13:50:35 -0800 | [diff] [blame] | 83 | |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame^] | 84 | static const int HALF_ALPHA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 1 /*alpha-only*/; |
| 85 | static const SkHalf kMaxIntegerRepresentableInHalfFloatingPoint = 0x6800; // 2 ^ 11 |
jvanverth | 1334c21 | 2014-12-18 05:44:55 -0800 | [diff] [blame] | 86 | |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame^] | 87 | DEF_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 | } |
jvanverth | 1334c21 | 2014-12-18 05:44:55 -0800 | [diff] [blame] | 92 | |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame^] | 93 | static const int HALF_RGBA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4 /*RGBA*/; |
jvanverth | 1334c21 | 2014-12-18 05:44:55 -0800 | [diff] [blame] | 94 | |
jvanverth | fb5df43 | 2015-05-21 08:12:27 -0700 | [diff] [blame^] | 95 | DEF_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); |
jvanverth | 28f9c60 | 2014-12-05 13:06:35 -0800 | [diff] [blame] | 99 | } |
| 100 | |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 101 | #endif |