blob: 69495b4bd27bce863ee3dfb4a89f56ae9720dae2 [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 */
bsalomond309e7a2015-04-30 14:18:54 -070014
joshualittee5da552014-07-16 13:32:56 -070015#include <float.h>
16#include "Test.h"
bsalomond309e7a2015-04-30 14:18:54 -070017#if SK_SUPPORT_GPU
joshualittee5da552014-07-16 13:32:56 -070018#include "GrContext.h"
19#include "GrTexture.h"
20#include "GrContextFactory.h"
jvanverth28f9c602014-12-05 13:06:35 -080021
joshualittee5da552014-07-16 13:32:56 -070022#include "SkGpuDevice.h"
jvanverth28f9c602014-12-05 13:06:35 -080023#include "SkHalf.h"
joshualittee5da552014-07-16 13:32:56 -070024
25static const int DEV_W = 100, DEV_H = 100;
mtklein2f6bb6b2015-01-23 05:47:55 -080026static const int FP_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4/*RGBA*/;
joshualittee5da552014-07-16 13:32:56 -070027static const float kMaxIntegerRepresentableInSPFloatingPoint = 16777216; // 2 ^ 24
28
29static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
30
31DEF_GPUTEST(FloatingPointTextureTest, reporter, factory) {
mtkleincada95a2015-01-22 13:50:35 -080032 SkTDArray<float> controlPixelData, readBuffer;
33 controlPixelData.setCount(FP_CONTROL_ARRAY_SIZE);
34 readBuffer.setCount(FP_CONTROL_ARRAY_SIZE);
35
joshualittee5da552014-07-16 13:32:56 -070036 for (int i = 0; i < FP_CONTROL_ARRAY_SIZE; i += 4) {
mtklein2f6bb6b2015-01-23 05:47:55 -080037 controlPixelData[i + 0] = FLT_MIN;
joshualittee5da552014-07-16 13:32:56 -070038 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) {
mtklein2f6bb6b2015-01-23 05:47:55 -080044 for (int glCtxType = 0; glCtxType < GrContextFactory::kGLContextTypeCnt; ++glCtxType) {
bsalomonf2703d82014-10-28 14:33:06 -070045 GrSurfaceDesc desc;
mtklein2f6bb6b2015-01-23 05:47:55 -080046 desc.fFlags = kRenderTarget_GrSurfaceFlag;
47 desc.fWidth = DEV_W;
joshualittee5da552014-07-16 13:32:56 -070048 desc.fHeight = DEV_H;
49 desc.fConfig = kRGBA_float_GrPixelConfig;
50 desc.fOrigin = 0 == origin ?
51 kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
52
joshualittee5da552014-07-16 13:32:56 -070053 GrContextFactory::GLContextType type =
mtklein2f6bb6b2015-01-23 05:47:55 -080054 static_cast<GrContextFactory::GLContextType>(glCtxType);
joshualittee5da552014-07-16 13:32:56 -070055 if (!GrContextFactory::IsRenderingGLContext(type)) {
56 continue;
57 }
mtklein2f6bb6b2015-01-23 05:47:55 -080058 GrContext* context = factory->get(type);
joshualittee5da552014-07-16 13:32:56 -070059 if (NULL == context){
60 continue;
61 }
62
bsalomond309e7a2015-04-30 14:18:54 -070063 SkAutoTUnref<GrTexture> fpTexture(context->textureProvider()->createTexture(
64 desc, false, controlPixelData.begin(), 0));
joshualittee5da552014-07-16 13:32:56 -070065 // Floating point textures are NOT supported everywhere
66 if (NULL == fpTexture) {
67 continue;
68 }
mtkleincada95a2015-01-22 13:50:35 -080069 fpTexture->readPixels(0, 0, DEV_W, DEV_H, desc.fConfig, readBuffer.begin(), 0);
mtklein2f6bb6b2015-01-23 05:47:55 -080070 REPORTER_ASSERT(reporter,
71 0 == memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.bytes()));
joshualittee5da552014-07-16 13:32:56 -070072 }
73 }
74}
75
mtklein2f6bb6b2015-01-23 05:47:55 -080076static const int HALF_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 1 /*alpha-only*/;
jvanverth28f9c602014-12-05 13:06:35 -080077
78DEF_GPUTEST(HalfFloatTextureTest, reporter, factory) {
mtkleincada95a2015-01-22 13:50:35 -080079 SkTDArray<SkHalf> controlPixelData, readBuffer;
80 controlPixelData.setCount(HALF_CONTROL_ARRAY_SIZE);
81 readBuffer.setCount(HALF_CONTROL_ARRAY_SIZE);
82
jvanverth28f9c602014-12-05 13:06:35 -080083 for (int i = 0; i < HALF_CONTROL_ARRAY_SIZE; i += 4) {
mtklein2f6bb6b2015-01-23 05:47:55 -080084 controlPixelData[i + 0] = SK_HalfMin;
jvanverth28f9c602014-12-05 13:06:35 -080085 controlPixelData[i + 1] = SK_HalfMax;
86 controlPixelData[i + 2] = SK_HalfEpsilon;
87 controlPixelData[i + 3] = 0x6800; // 2^11
88 }
jvanverth1334c212014-12-18 05:44:55 -080089
jvanverth28f9c602014-12-05 13:06:35 -080090 for (int origin = 0; origin < 2; ++origin) {
mtklein2f6bb6b2015-01-23 05:47:55 -080091 for (int glCtxType = 0; glCtxType < GrContextFactory::kGLContextTypeCnt; ++glCtxType) {
jvanverth28f9c602014-12-05 13:06:35 -080092 GrSurfaceDesc desc;
mtklein2f6bb6b2015-01-23 05:47:55 -080093 desc.fFlags = kRenderTarget_GrSurfaceFlag;
94 desc.fWidth = DEV_W;
jvanverth28f9c602014-12-05 13:06:35 -080095 desc.fHeight = DEV_H;
96 desc.fConfig = kAlpha_half_GrPixelConfig;
97 desc.fOrigin = 0 == origin ?
mtklein2f6bb6b2015-01-23 05:47:55 -080098 kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
jvanverth1334c212014-12-18 05:44:55 -080099
jvanverth28f9c602014-12-05 13:06:35 -0800100 GrContextFactory::GLContextType type =
mtklein2f6bb6b2015-01-23 05:47:55 -0800101 static_cast<GrContextFactory::GLContextType>(glCtxType);
jvanverth28f9c602014-12-05 13:06:35 -0800102 if (!GrContextFactory::IsRenderingGLContext(type)) {
103 continue;
104 }
mtklein2f6bb6b2015-01-23 05:47:55 -0800105 GrContext* context = factory->get(type);
jvanverth28f9c602014-12-05 13:06:35 -0800106 if (NULL == context){
107 continue;
108 }
jvanverth1334c212014-12-18 05:44:55 -0800109
bsalomond309e7a2015-04-30 14:18:54 -0700110 SkAutoTUnref<GrTexture> fpTexture(context->textureProvider()->createTexture(
111 desc, false, controlPixelData.begin(), 0));
jvanverth28f9c602014-12-05 13:06:35 -0800112 // 16-bit floating point textures are NOT supported everywhere
113 if (NULL == fpTexture) {
114 continue;
115 }
mtkleincada95a2015-01-22 13:50:35 -0800116 fpTexture->readPixels(0, 0, DEV_W, DEV_H, desc.fConfig, readBuffer.begin(), 0);
mtklein2f6bb6b2015-01-23 05:47:55 -0800117 REPORTER_ASSERT(reporter,
118 0 == memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.bytes()));
jvanverth28f9c602014-12-05 13:06:35 -0800119 }
120 }
121}
122
joshualittee5da552014-07-16 13:32:56 -0700123#endif