FloatingPointTextureTest.cpp revision a60b2ead80c5442f35fb9109380ead98865e61be
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
21#include "SkGpuDevice.h"
22#include "SkHalf.h"
23
24static const int DEV_W = 100, DEV_H = 100;
25static const int FP_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4;
26static const float kMaxIntegerRepresentableInSPFloatingPoint = 16777216;  // 2 ^ 24
27
28static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
29
30DEF_GPUTEST(FloatingPointTextureTest, reporter, factory) {
31    float controlPixelData[FP_CONTROL_ARRAY_SIZE];
32    float readBuffer[FP_CONTROL_ARRAY_SIZE];
33    for (int i = 0; i < FP_CONTROL_ARRAY_SIZE; i += 4) {
34        controlPixelData[i] = FLT_MIN;
35        controlPixelData[i + 1] = FLT_MAX;
36        controlPixelData[i + 2] = FLT_EPSILON;
37        controlPixelData[i + 3] = kMaxIntegerRepresentableInSPFloatingPoint;
38    }
39
40    for (int origin = 0; origin < 2; ++origin) {
41        int glCtxTypeCnt = 1;
42        glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt;
43        for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) {
44            GrSurfaceDesc desc;
45            desc.fFlags = kRenderTarget_GrSurfaceFlag;
46            desc.fWidth = DEV_W;
47            desc.fHeight = DEV_H;
48            desc.fConfig = kRGBA_float_GrPixelConfig;
49            desc.fOrigin = 0 == origin ?
50                kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
51
52            GrContext* context = NULL;
53            GrContextFactory::GLContextType type =
54                    static_cast<GrContextFactory::GLContextType>(glCtxType);
55            if (!GrContextFactory::IsRenderingGLContext(type)) {
56                continue;
57            }
58            context = factory->get(type);
59            if (NULL == context){
60                continue;
61            }
62
63            SkAutoTUnref<GrTexture> fpTexture(context->createUncachedTexture(desc,
64                                                                             NULL,
65                                                                             0));
66
67            // Floating point textures are NOT supported everywhere
68            if (NULL == fpTexture) {
69                continue;
70            }
71
72            // write square
73            fpTexture->writePixels(0, 0, DEV_W, DEV_H, desc.fConfig, controlPixelData, 0);
74            fpTexture->readPixels(0, 0, DEV_W, DEV_H, desc.fConfig, readBuffer, 0);
75            for (int j = 0; j < FP_CONTROL_ARRAY_SIZE; ++j) {
76                REPORTER_ASSERT(reporter, readBuffer[j] == controlPixelData[j]);
77            }
78        }
79    }
80}
81
82static const int HALF_CONTROL_ARRAY_SIZE = DEV_W * DEV_H;
83
84DEF_GPUTEST(HalfFloatTextureTest, reporter, factory) {
85    SkHalf controlPixelData[HALF_CONTROL_ARRAY_SIZE];
86    SkHalf readBuffer[HALF_CONTROL_ARRAY_SIZE];
87    for (int i = 0; i < HALF_CONTROL_ARRAY_SIZE; i += 4) {
88        controlPixelData[i] = SK_HalfMin;
89        controlPixelData[i + 1] = SK_HalfMax;
90        controlPixelData[i + 2] = SK_HalfEpsilon;
91        controlPixelData[i + 3] = 0x6800;   // 2^11
92    }
93
94    for (int origin = 0; origin < 2; ++origin) {
95        int glCtxTypeCnt = 1;
96        glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt;
97        for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) {
98            GrSurfaceDesc desc;
99            desc.fFlags = kRenderTarget_GrSurfaceFlag;
100            desc.fWidth = DEV_W;
101            desc.fHeight = DEV_H;
102            desc.fConfig = kAlpha_half_GrPixelConfig;
103            desc.fOrigin = 0 == origin ?
104            kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
105
106            GrContext* context = NULL;
107            GrContextFactory::GLContextType type =
108            static_cast<GrContextFactory::GLContextType>(glCtxType);
109            if (!GrContextFactory::IsRenderingGLContext(type)) {
110                continue;
111            }
112            context = factory->get(type);
113            if (NULL == context){
114                continue;
115            }
116
117            SkAutoTUnref<GrTexture> fpTexture(context->createUncachedTexture(desc,
118                                                                             NULL,
119                                                                             0));
120
121            // 16-bit floating point textures are NOT supported everywhere
122            if (NULL == fpTexture) {
123                continue;
124            }
125
126            // write square
127            fpTexture->writePixels(0, 0, DEV_W, DEV_H, desc.fConfig, controlPixelData, 0);
128            fpTexture->readPixels(0, 0, DEV_W, DEV_H, desc.fConfig, readBuffer, 0);
129            for (int j = 0; j < HALF_CONTROL_ARRAY_SIZE; ++j) {
130                REPORTER_ASSERT(reporter, readBuffer[j] == controlPixelData[j]);
131            }
132        }
133    }
134}
135
136#endif
137