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" |
| 20 | #include "SkGpuDevice.h" |
| 21 | |
| 22 | static const int DEV_W = 100, DEV_H = 100; |
| 23 | static const int FP_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * sizeof(float); |
| 24 | static const float kMaxIntegerRepresentableInSPFloatingPoint = 16777216; // 2 ^ 24 |
| 25 | |
| 26 | static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H); |
| 27 | |
| 28 | DEF_GPUTEST(FloatingPointTextureTest, reporter, factory) { |
| 29 | float controlPixelData[FP_CONTROL_ARRAY_SIZE]; |
| 30 | float readBuffer[FP_CONTROL_ARRAY_SIZE]; |
| 31 | for (int i = 0; i < FP_CONTROL_ARRAY_SIZE; i += 4) { |
| 32 | controlPixelData[i] = FLT_MIN; |
| 33 | controlPixelData[i + 1] = FLT_MAX; |
| 34 | controlPixelData[i + 2] = FLT_EPSILON; |
| 35 | controlPixelData[i + 3] = kMaxIntegerRepresentableInSPFloatingPoint; |
| 36 | } |
| 37 | |
| 38 | for (int origin = 0; origin < 2; ++origin) { |
| 39 | int glCtxTypeCnt = 1; |
| 40 | glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt; |
| 41 | for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) { |
| 42 | GrTextureDesc desc; |
| 43 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 44 | desc.fWidth = DEV_W; |
| 45 | desc.fHeight = DEV_H; |
| 46 | desc.fConfig = kRGBA_float_GrPixelConfig; |
| 47 | desc.fOrigin = 0 == origin ? |
| 48 | kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin; |
| 49 | |
| 50 | GrContext* context = NULL; |
| 51 | GrContextFactory::GLContextType type = |
| 52 | static_cast<GrContextFactory::GLContextType>(glCtxType); |
| 53 | if (!GrContextFactory::IsRenderingGLContext(type)) { |
| 54 | continue; |
| 55 | } |
| 56 | context = factory->get(type); |
| 57 | if (NULL == context){ |
| 58 | continue; |
| 59 | } |
| 60 | |
| 61 | SkAutoTUnref<GrTexture> fpTexture(context->createUncachedTexture(desc, |
| 62 | NULL, |
| 63 | 0)); |
| 64 | |
| 65 | // Floating point textures are NOT supported everywhere |
| 66 | if (NULL == fpTexture) { |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | // write square |
| 71 | context->writeTexturePixels(fpTexture, 0, 0, DEV_W, DEV_H, desc.fConfig, |
| 72 | controlPixelData, 0); |
| 73 | context->readTexturePixels(fpTexture, 0, 0, DEV_W, DEV_H, desc.fConfig, readBuffer, 0); |
| 74 | for (int j = 0; j < FP_CONTROL_ARRAY_SIZE; ++j) { |
| 75 | REPORTER_ASSERT(reporter, readBuffer[j] == controlPixelData[j]); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | #endif |