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 | */ |
| 14 | #if SK_SUPPORT_GPU |
| 15 | #include <float.h> |
| 16 | #include "Test.h" |
| 17 | #include "GrContext.h" |
| 18 | #include "GrTexture.h" |
| 19 | #include "GrContextFactory.h" |
jvanverth | 28f9c60 | 2014-12-05 13:06:35 -0800 | [diff] [blame] | 20 | |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 21 | #include "SkGpuDevice.h" |
jvanverth | 28f9c60 | 2014-12-05 13:06:35 -0800 | [diff] [blame] | 22 | #include "SkHalf.h" |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 23 | |
| 24 | static const int DEV_W = 100, DEV_H = 100; |
mtklein | 2f6bb6b | 2015-01-23 05:47:55 -0800 | [diff] [blame] | 25 | static const int FP_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4/*RGBA*/; |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 26 | static const float kMaxIntegerRepresentableInSPFloatingPoint = 16777216; // 2 ^ 24 |
| 27 | |
| 28 | static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H); |
| 29 | |
| 30 | DEF_GPUTEST(FloatingPointTextureTest, reporter, factory) { |
mtklein | cada95a | 2015-01-22 13:50:35 -0800 | [diff] [blame] | 31 | SkTDArray<float> controlPixelData, readBuffer; |
| 32 | controlPixelData.setCount(FP_CONTROL_ARRAY_SIZE); |
| 33 | readBuffer.setCount(FP_CONTROL_ARRAY_SIZE); |
| 34 | |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 35 | for (int i = 0; i < FP_CONTROL_ARRAY_SIZE; i += 4) { |
mtklein | 2f6bb6b | 2015-01-23 05:47:55 -0800 | [diff] [blame] | 36 | controlPixelData[i + 0] = FLT_MIN; |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 37 | controlPixelData[i + 1] = FLT_MAX; |
| 38 | controlPixelData[i + 2] = FLT_EPSILON; |
| 39 | controlPixelData[i + 3] = kMaxIntegerRepresentableInSPFloatingPoint; |
| 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; |
mtklein | 2f6bb6b | 2015-01-23 05:47:55 -0800 | [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; |
| 48 | desc.fConfig = kRGBA_float_GrPixelConfig; |
| 49 | desc.fOrigin = 0 == origin ? |
| 50 | kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin; |
| 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); |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 58 | if (NULL == context){ |
| 59 | continue; |
| 60 | } |
| 61 | |
bsalomon | d042358 | 2015-02-06 08:49:24 -0800 | [diff] [blame] | 62 | SkAutoTUnref<GrTexture> fpTexture(context->createTexture(desc, false, |
| 63 | 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, |
| 70 | 0 == memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.bytes())); |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
mtklein | 2f6bb6b | 2015-01-23 05:47:55 -0800 | [diff] [blame] | 75 | static const int HALF_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 1 /*alpha-only*/; |
jvanverth | 28f9c60 | 2014-12-05 13:06:35 -0800 | [diff] [blame] | 76 | |
| 77 | DEF_GPUTEST(HalfFloatTextureTest, reporter, factory) { |
mtklein | cada95a | 2015-01-22 13:50:35 -0800 | [diff] [blame] | 78 | SkTDArray<SkHalf> controlPixelData, readBuffer; |
| 79 | controlPixelData.setCount(HALF_CONTROL_ARRAY_SIZE); |
| 80 | readBuffer.setCount(HALF_CONTROL_ARRAY_SIZE); |
| 81 | |
jvanverth | 28f9c60 | 2014-12-05 13:06:35 -0800 | [diff] [blame] | 82 | for (int i = 0; i < HALF_CONTROL_ARRAY_SIZE; i += 4) { |
mtklein | 2f6bb6b | 2015-01-23 05:47:55 -0800 | [diff] [blame] | 83 | controlPixelData[i + 0] = SK_HalfMin; |
jvanverth | 28f9c60 | 2014-12-05 13:06:35 -0800 | [diff] [blame] | 84 | controlPixelData[i + 1] = SK_HalfMax; |
| 85 | controlPixelData[i + 2] = SK_HalfEpsilon; |
| 86 | controlPixelData[i + 3] = 0x6800; // 2^11 |
| 87 | } |
jvanverth | 1334c21 | 2014-12-18 05:44:55 -0800 | [diff] [blame] | 88 | |
jvanverth | 28f9c60 | 2014-12-05 13:06:35 -0800 | [diff] [blame] | 89 | for (int origin = 0; origin < 2; ++origin) { |
mtklein | 2f6bb6b | 2015-01-23 05:47:55 -0800 | [diff] [blame] | 90 | for (int glCtxType = 0; glCtxType < GrContextFactory::kGLContextTypeCnt; ++glCtxType) { |
jvanverth | 28f9c60 | 2014-12-05 13:06:35 -0800 | [diff] [blame] | 91 | GrSurfaceDesc desc; |
mtklein | 2f6bb6b | 2015-01-23 05:47:55 -0800 | [diff] [blame] | 92 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 93 | desc.fWidth = DEV_W; |
jvanverth | 28f9c60 | 2014-12-05 13:06:35 -0800 | [diff] [blame] | 94 | desc.fHeight = DEV_H; |
| 95 | desc.fConfig = kAlpha_half_GrPixelConfig; |
| 96 | desc.fOrigin = 0 == origin ? |
mtklein | 2f6bb6b | 2015-01-23 05:47:55 -0800 | [diff] [blame] | 97 | kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin; |
jvanverth | 1334c21 | 2014-12-18 05:44:55 -0800 | [diff] [blame] | 98 | |
jvanverth | 28f9c60 | 2014-12-05 13:06:35 -0800 | [diff] [blame] | 99 | GrContextFactory::GLContextType type = |
mtklein | 2f6bb6b | 2015-01-23 05:47:55 -0800 | [diff] [blame] | 100 | static_cast<GrContextFactory::GLContextType>(glCtxType); |
jvanverth | 28f9c60 | 2014-12-05 13:06:35 -0800 | [diff] [blame] | 101 | if (!GrContextFactory::IsRenderingGLContext(type)) { |
| 102 | continue; |
| 103 | } |
mtklein | 2f6bb6b | 2015-01-23 05:47:55 -0800 | [diff] [blame] | 104 | GrContext* context = factory->get(type); |
jvanverth | 28f9c60 | 2014-12-05 13:06:35 -0800 | [diff] [blame] | 105 | if (NULL == context){ |
| 106 | continue; |
| 107 | } |
jvanverth | 1334c21 | 2014-12-18 05:44:55 -0800 | [diff] [blame] | 108 | |
bsalomon | d042358 | 2015-02-06 08:49:24 -0800 | [diff] [blame] | 109 | SkAutoTUnref<GrTexture> fpTexture(context->createTexture(desc, false, |
| 110 | controlPixelData.begin(), 0)); |
jvanverth | 28f9c60 | 2014-12-05 13:06:35 -0800 | [diff] [blame] | 111 | // 16-bit floating point textures are NOT supported everywhere |
| 112 | if (NULL == fpTexture) { |
| 113 | continue; |
| 114 | } |
mtklein | cada95a | 2015-01-22 13:50:35 -0800 | [diff] [blame] | 115 | fpTexture->readPixels(0, 0, DEV_W, DEV_H, desc.fConfig, readBuffer.begin(), 0); |
mtklein | 2f6bb6b | 2015-01-23 05:47:55 -0800 | [diff] [blame] | 116 | REPORTER_ASSERT(reporter, |
| 117 | 0 == memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.bytes())); |
jvanverth | 28f9c60 | 2014-12-05 13:06:35 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
joshualitt | ee5da55 | 2014-07-16 13:32:56 -0700 | [diff] [blame] | 122 | #endif |