blob: 346c7379929be483060ab118f0ae2c341f285f4e [file] [log] [blame]
joshualittee5da552014-07-16 13:32:56 -07001/*
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"
jvanverth28f9c602014-12-05 13:06:35 -080020
joshualittee5da552014-07-16 13:32:56 -070021#include "SkGpuDevice.h"
jvanverth28f9c602014-12-05 13:06:35 -080022#include "SkHalf.h"
joshualittee5da552014-07-16 13:32:56 -070023
24static const int DEV_W = 100, DEV_H = 100;
jvanverth28f9c602014-12-05 13:06:35 -080025static const int FP_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4;
joshualittee5da552014-07-16 13:32:56 -070026static 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) {
bsalomonf2703d82014-10-28 14:33:06 -070044 GrSurfaceDesc desc;
45 desc.fFlags = kRenderTarget_GrSurfaceFlag;
joshualittee5da552014-07-16 13:32:56 -070046 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
bsalomon81beccc2014-10-13 12:32:55 -070073 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);
joshualittee5da552014-07-16 13:32:56 -070075 for (int j = 0; j < FP_CONTROL_ARRAY_SIZE; ++j) {
76 REPORTER_ASSERT(reporter, readBuffer[j] == controlPixelData[j]);
77 }
78 }
79 }
80}
81
jvanverth28f9c602014-12-05 13:06:35 -080082static 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 }
jvanverth1334c212014-12-18 05:44:55 -080093
jvanverth28f9c602014-12-05 13:06:35 -080094 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;
jvanverth1334c212014-12-18 05:44:55 -0800105
jvanverth28f9c602014-12-05 13:06:35 -0800106 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 }
jvanverth1334c212014-12-18 05:44:55 -0800116
jvanverth28f9c602014-12-05 13:06:35 -0800117 SkAutoTUnref<GrTexture> fpTexture(context->createUncachedTexture(desc,
118 NULL,
119 0));
jvanverth1334c212014-12-18 05:44:55 -0800120
jvanverth28f9c602014-12-05 13:06:35 -0800121 // 16-bit floating point textures are NOT supported everywhere
122 if (NULL == fpTexture) {
123 continue;
124 }
jvanverth1334c212014-12-18 05:44:55 -0800125
jvanverth28f9c602014-12-05 13:06:35 -0800126 // 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) {
jvanverth28f9c602014-12-05 13:06:35 -0800130 REPORTER_ASSERT(reporter, readBuffer[j] == controlPixelData[j]);
131 }
132 }
133 }
134}
135
joshualittee5da552014-07-16 13:32:56 -0700136#endif