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