blob: 7ac27cb6a4805fbe60caaa10ec8d1f855d7db887 [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;
mtklein2f6bb6b2015-01-23 05:47:55 -080025static const int FP_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4/*RGBA*/;
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) {
mtkleincada95a2015-01-22 13:50:35 -080031 SkTDArray<float> controlPixelData, readBuffer;
32 controlPixelData.setCount(FP_CONTROL_ARRAY_SIZE);
33 readBuffer.setCount(FP_CONTROL_ARRAY_SIZE);
34
joshualittee5da552014-07-16 13:32:56 -070035 for (int i = 0; i < FP_CONTROL_ARRAY_SIZE; i += 4) {
mtklein2f6bb6b2015-01-23 05:47:55 -080036 controlPixelData[i + 0] = FLT_MIN;
joshualittee5da552014-07-16 13:32:56 -070037 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) {
mtklein2f6bb6b2015-01-23 05:47:55 -080043 for (int glCtxType = 0; glCtxType < GrContextFactory::kGLContextTypeCnt; ++glCtxType) {
bsalomonf2703d82014-10-28 14:33:06 -070044 GrSurfaceDesc desc;
mtklein2f6bb6b2015-01-23 05:47:55 -080045 desc.fFlags = kRenderTarget_GrSurfaceFlag;
46 desc.fWidth = DEV_W;
joshualittee5da552014-07-16 13:32:56 -070047 desc.fHeight = DEV_H;
48 desc.fConfig = kRGBA_float_GrPixelConfig;
49 desc.fOrigin = 0 == origin ?
50 kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
51
joshualittee5da552014-07-16 13:32:56 -070052 GrContextFactory::GLContextType type =
mtklein2f6bb6b2015-01-23 05:47:55 -080053 static_cast<GrContextFactory::GLContextType>(glCtxType);
joshualittee5da552014-07-16 13:32:56 -070054 if (!GrContextFactory::IsRenderingGLContext(type)) {
55 continue;
56 }
mtklein2f6bb6b2015-01-23 05:47:55 -080057 GrContext* context = factory->get(type);
joshualittee5da552014-07-16 13:32:56 -070058 if (NULL == context){
59 continue;
60 }
61
bsalomond0423582015-02-06 08:49:24 -080062 SkAutoTUnref<GrTexture> fpTexture(context->createTexture(desc, false,
63 controlPixelData.begin(), 0));
joshualittee5da552014-07-16 13:32:56 -070064 // Floating point textures are NOT supported everywhere
65 if (NULL == fpTexture) {
66 continue;
67 }
mtkleincada95a2015-01-22 13:50:35 -080068 fpTexture->readPixels(0, 0, DEV_W, DEV_H, desc.fConfig, readBuffer.begin(), 0);
mtklein2f6bb6b2015-01-23 05:47:55 -080069 REPORTER_ASSERT(reporter,
70 0 == memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.bytes()));
joshualittee5da552014-07-16 13:32:56 -070071 }
72 }
73}
74
mtklein2f6bb6b2015-01-23 05:47:55 -080075static const int HALF_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 1 /*alpha-only*/;
jvanverth28f9c602014-12-05 13:06:35 -080076
77DEF_GPUTEST(HalfFloatTextureTest, reporter, factory) {
mtkleincada95a2015-01-22 13:50:35 -080078 SkTDArray<SkHalf> controlPixelData, readBuffer;
79 controlPixelData.setCount(HALF_CONTROL_ARRAY_SIZE);
80 readBuffer.setCount(HALF_CONTROL_ARRAY_SIZE);
81
jvanverth28f9c602014-12-05 13:06:35 -080082 for (int i = 0; i < HALF_CONTROL_ARRAY_SIZE; i += 4) {
mtklein2f6bb6b2015-01-23 05:47:55 -080083 controlPixelData[i + 0] = SK_HalfMin;
jvanverth28f9c602014-12-05 13:06:35 -080084 controlPixelData[i + 1] = SK_HalfMax;
85 controlPixelData[i + 2] = SK_HalfEpsilon;
86 controlPixelData[i + 3] = 0x6800; // 2^11
87 }
jvanverth1334c212014-12-18 05:44:55 -080088
jvanverth28f9c602014-12-05 13:06:35 -080089 for (int origin = 0; origin < 2; ++origin) {
mtklein2f6bb6b2015-01-23 05:47:55 -080090 for (int glCtxType = 0; glCtxType < GrContextFactory::kGLContextTypeCnt; ++glCtxType) {
jvanverth28f9c602014-12-05 13:06:35 -080091 GrSurfaceDesc desc;
mtklein2f6bb6b2015-01-23 05:47:55 -080092 desc.fFlags = kRenderTarget_GrSurfaceFlag;
93 desc.fWidth = DEV_W;
jvanverth28f9c602014-12-05 13:06:35 -080094 desc.fHeight = DEV_H;
95 desc.fConfig = kAlpha_half_GrPixelConfig;
96 desc.fOrigin = 0 == origin ?
mtklein2f6bb6b2015-01-23 05:47:55 -080097 kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
jvanverth1334c212014-12-18 05:44:55 -080098
jvanverth28f9c602014-12-05 13:06:35 -080099 GrContextFactory::GLContextType type =
mtklein2f6bb6b2015-01-23 05:47:55 -0800100 static_cast<GrContextFactory::GLContextType>(glCtxType);
jvanverth28f9c602014-12-05 13:06:35 -0800101 if (!GrContextFactory::IsRenderingGLContext(type)) {
102 continue;
103 }
mtklein2f6bb6b2015-01-23 05:47:55 -0800104 GrContext* context = factory->get(type);
jvanverth28f9c602014-12-05 13:06:35 -0800105 if (NULL == context){
106 continue;
107 }
jvanverth1334c212014-12-18 05:44:55 -0800108
bsalomond0423582015-02-06 08:49:24 -0800109 SkAutoTUnref<GrTexture> fpTexture(context->createTexture(desc, false,
110 controlPixelData.begin(), 0));
jvanverth28f9c602014-12-05 13:06:35 -0800111 // 16-bit floating point textures are NOT supported everywhere
112 if (NULL == fpTexture) {
113 continue;
114 }
mtkleincada95a2015-01-22 13:50:35 -0800115 fpTexture->readPixels(0, 0, DEV_W, DEV_H, desc.fConfig, readBuffer.begin(), 0);
mtklein2f6bb6b2015-01-23 05:47:55 -0800116 REPORTER_ASSERT(reporter,
117 0 == memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.bytes()));
jvanverth28f9c602014-12-05 13:06:35 -0800118 }
119 }
120}
121
joshualittee5da552014-07-16 13:32:56 -0700122#endif