Commit cdfa8f50 by Arun Patole Committed by Jamie Madill

Constant fold float pack/unpack functions

This change adds constant folding support for following floating point pack and unpack functions: - packSnorm2x16, unpackSnorm2x16, packUnorm2x16, unpackUnorm2x16, packHalf2x16, and unpackHalf2x16. BUG=angleproject:913 TEST=angle_unittests(new: MathUtilTest.packAndUnpack*), dEQP Tests dEQP-GLES3.functional.shaders.constant_expressions.builtin_functions.float_pack_unpack.* (all 10 tests started passing with this change) Change-Id: I2b69faebee4127e5e13900b3a9485b7145950277 Reviewed-on: https://chromium-review.googlesource.com/282683Reviewed-by: 's avatarOlli Etuaho <oetuaho@nvidia.com> Tested-by: 's avatarOlli Etuaho <oetuaho@nvidia.com> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org>
parent 846fe05f
......@@ -14,7 +14,9 @@
#include <limits>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
namespace gl
......@@ -540,6 +542,86 @@ struct Range
typedef Range<int> RangeI;
typedef Range<unsigned int> RangeUI;
// First, both normalized floating-point values are converted into 16-bit integer values.
// Then, the results are packed into the returned 32-bit unsigned integer.
// The first float value will be written to the least significant bits of the output;
// the last float value will be written to the most significant bits.
// The conversion of each value to fixed point is done as follows :
// packSnorm2x16 : round(clamp(c, -1, +1) * 32767.0)
inline uint32_t packSnorm2x16(float f1, float f2)
{
uint16_t leastSignificantBits = static_cast<uint16_t>(roundf(clamp(f1, -1.0f, 1.0f) * 32767.0f));
uint16_t mostSignificantBits = static_cast<uint16_t>(roundf(clamp(f2, -1.0f, 1.0f) * 32767.0f));
return static_cast<uint32_t>(mostSignificantBits) << 16 | static_cast<uint32_t>(leastSignificantBits);
}
// First, unpacks a single 32-bit unsigned integer u into a pair of 16-bit unsigned integers. Then, each
// component is converted to a normalized floating-point value to generate the returned two float values.
// The first float value will be extracted from the least significant bits of the input;
// the last float value will be extracted from the most-significant bits.
// The conversion for unpacked fixed-point value to floating point is done as follows:
// unpackSnorm2x16 : clamp(f / 32767.0, -1, +1)
inline void unpackSnorm2x16(uint32_t u, float *f1, float *f2)
{
int16_t leastSignificantBits = static_cast<int16_t>(u & 0xFFFF);
int16_t mostSignificantBits = static_cast<int16_t>(u >> 16);
*f1 = clamp(static_cast<float>(leastSignificantBits) / 32767.0f, -1.0f, 1.0f);
*f2 = clamp(static_cast<float>(mostSignificantBits) / 32767.0f, -1.0f, 1.0f);
}
// First, both normalized floating-point values are converted into 16-bit integer values.
// Then, the results are packed into the returned 32-bit unsigned integer.
// The first float value will be written to the least significant bits of the output;
// the last float value will be written to the most significant bits.
// The conversion of each value to fixed point is done as follows:
// packUnorm2x16 : round(clamp(c, 0, +1) * 65535.0)
inline uint32_t packUnorm2x16(float f1, float f2)
{
uint16_t leastSignificantBits = static_cast<uint16_t>(roundf(clamp(f1, 0.0f, 1.0f) * 65535.0f));
uint16_t mostSignificantBits = static_cast<uint16_t>(roundf(clamp(f2, 0.0f, 1.0f) * 65535.0f));
return static_cast<uint32_t>(mostSignificantBits) << 16 | static_cast<uint32_t>(leastSignificantBits);
}
// First, unpacks a single 32-bit unsigned integer u into a pair of 16-bit unsigned integers. Then, each
// component is converted to a normalized floating-point value to generate the returned two float values.
// The first float value will be extracted from the least significant bits of the input;
// the last float value will be extracted from the most-significant bits.
// The conversion for unpacked fixed-point value to floating point is done as follows:
// unpackUnorm2x16 : f / 65535.0
inline void unpackUnorm2x16(uint32_t u, float *f1, float *f2)
{
uint16_t leastSignificantBits = static_cast<uint16_t>(u & 0xFFFF);
uint16_t mostSignificantBits = static_cast<uint16_t>(u >> 16);
*f1 = static_cast<float>(leastSignificantBits) / 65535.0f;
*f2 = static_cast<float>(mostSignificantBits) / 65535.0f;
}
// Returns an unsigned integer obtained by converting the two floating-point values to the 16-bit
// floating-point representation found in the OpenGL ES Specification, and then packing these
// two 16-bit integers into a 32-bit unsigned integer.
// f1: The 16 least-significant bits of the result;
// f2: The 16 most-significant bits.
inline uint32_t packHalf2x16(float f1, float f2)
{
uint16_t leastSignificantBits = static_cast<uint16_t>(float32ToFloat16(f1));
uint16_t mostSignificantBits = static_cast<uint16_t>(float32ToFloat16(f2));
return static_cast<uint32_t>(mostSignificantBits) << 16 | static_cast<uint32_t>(leastSignificantBits);
}
// Returns two floating-point values obtained by unpacking a 32-bit unsigned integer into a pair of 16-bit values,
// interpreting those values as 16-bit floating-point numbers according to the OpenGL ES Specification,
// and converting them to 32-bit floating-point values.
// The first float value is obtained from the 16 least-significant bits of u;
// the second component is obtained from the 16 most-significant bits of u.
inline void unpackHalf2x16(uint32_t u, float *f1, float *f2)
{
uint16_t leastSignificantBits = static_cast<uint16_t>(u & 0xFFFF);
uint16_t mostSignificantBits = static_cast<uint16_t>(u >> 16);
*f1 = float16ToFloat32(leastSignificantBits);
*f2 = float16ToFloat32(mostSignificantBits);
}
}
namespace rx
......
//
// Copyright 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// mathutil_unittest:
// Unit tests for the utils defined in mathutil.h
//
#include "mathutil.h"
#include <gtest/gtest.h>
using namespace gl;
namespace
{
// Test the correctness of packSnorm2x16 and unpackSnorm2x16 functions.
// For floats f1 and f2, unpackSnorm2x16(packSnorm2x16(f1, f2)) should be same as f1 and f2.
TEST(MathUtilTest, packAndUnpackSnorm2x16)
{
const float input[8][2] =
{
{ 0.0f, 0.0f },
{ 1.0f, 1.0f },
{ -1.0f, 1.0f },
{ -1.0f, -1.0f },
{ 0.875f, 0.75f },
{ 0.00392f, -0.99215f },
{ -0.000675f, 0.004954f },
{ -0.6937f, -0.02146f }
};
const float floatFaultTolerance = 0.0001f;
float outputVal1, outputVal2;
for (size_t i = 0; i < 8; i++)
{
unpackSnorm2x16(packSnorm2x16(input[i][0], input[i][1]), &outputVal1, &outputVal2);
EXPECT_NEAR(input[i][0], outputVal1, floatFaultTolerance);
EXPECT_NEAR(input[i][1], outputVal2, floatFaultTolerance);
}
}
// Test the correctness of packSnorm2x16 and unpackSnorm2x16 functions with infinity values,
// result should be clamped to [-1, 1].
TEST(MathUtilTest, packAndUnpackSnorm2x16Infinity)
{
const float floatFaultTolerance = 0.0001f;
float outputVal1, outputVal2;
unpackSnorm2x16(packSnorm2x16(std::numeric_limits<float>::infinity(),
std::numeric_limits<float>::infinity()), &outputVal1, &outputVal2);
EXPECT_NEAR(1.0f, outputVal1, floatFaultTolerance);
EXPECT_NEAR(1.0f, outputVal2, floatFaultTolerance);
unpackSnorm2x16(packSnorm2x16(std::numeric_limits<float>::infinity(),
-std::numeric_limits<float>::infinity()), &outputVal1, &outputVal2);
EXPECT_NEAR(1.0f, outputVal1, floatFaultTolerance);
EXPECT_NEAR(-1.0f, outputVal2, floatFaultTolerance);
unpackSnorm2x16(packSnorm2x16(-std::numeric_limits<float>::infinity(),
-std::numeric_limits<float>::infinity()), &outputVal1, &outputVal2);
EXPECT_NEAR(-1.0f, outputVal1, floatFaultTolerance);
EXPECT_NEAR(-1.0f, outputVal2, floatFaultTolerance);
}
// Test the correctness of packUnorm2x16 and unpackUnorm2x16 functions.
// For floats f1 and f2, unpackUnorm2x16(packUnorm2x16(f1, f2)) should be same as f1 and f2.
TEST(MathUtilTest, packAndUnpackUnorm2x16)
{
const float input[8][2] =
{
{ 0.0f, 0.0f },
{ 1.0f, 1.0f },
{ -1.0f, 1.0f },
{ -1.0f, -1.0f },
{ 0.875f, 0.75f },
{ 0.00392f, -0.99215f },
{ -0.000675f, 0.004954f },
{ -0.6937f, -0.02146f }
};
const float floatFaultTolerance = 0.0001f;
float outputVal1, outputVal2;
for (size_t i = 0; i < 8; i++)
{
unpackUnorm2x16(packUnorm2x16(input[i][0], input[i][1]), &outputVal1, &outputVal2);
float expected = input[i][0] < 0.0f ? 0.0f : input[i][0];
EXPECT_NEAR(expected, outputVal1, floatFaultTolerance);
expected = input[i][1] < 0.0f ? 0.0f : input[i][1];
EXPECT_NEAR(expected, outputVal2, floatFaultTolerance);
}
}
// Test the correctness of packUnorm2x16 and unpackUnorm2x16 functions with infinity values,
// result should be clamped to [0, 1].
TEST(MathUtilTest, packAndUnpackUnorm2x16Infinity)
{
const float floatFaultTolerance = 0.0001f;
float outputVal1, outputVal2;
unpackUnorm2x16(packUnorm2x16(std::numeric_limits<float>::infinity(),
std::numeric_limits<float>::infinity()), &outputVal1, &outputVal2);
EXPECT_NEAR(1.0f, outputVal1, floatFaultTolerance);
EXPECT_NEAR(1.0f, outputVal2, floatFaultTolerance);
unpackUnorm2x16(packUnorm2x16(std::numeric_limits<float>::infinity(),
-std::numeric_limits<float>::infinity()), &outputVal1, &outputVal2);
EXPECT_NEAR(1.0f, outputVal1, floatFaultTolerance);
EXPECT_NEAR(0.0f, outputVal2, floatFaultTolerance);
unpackUnorm2x16(packUnorm2x16(-std::numeric_limits<float>::infinity(),
-std::numeric_limits<float>::infinity()), &outputVal1, &outputVal2);
EXPECT_NEAR(0.0f, outputVal1, floatFaultTolerance);
EXPECT_NEAR(0.0f, outputVal2, floatFaultTolerance);
}
// Test the correctness of packHalf2x16 and unpackHalf2x16 functions.
// For floats f1 and f2, unpackHalf2x16(packHalf2x16(f1, f2)) should be same as f1 and f2.
TEST(MathUtilTest, packAndUnpackHalf2x16)
{
const float input[8][2] =
{
{ 0.0f, 0.0f },
{ 1.0f, 1.0f },
{ -1.0f, 1.0f },
{ -1.0f, -1.0f },
{ 0.875f, 0.75f },
{ 0.00392f, -0.99215f },
{ -0.000675f, 0.004954f },
{ -0.6937f, -0.02146f },
};
const float floatFaultTolerance = 0.0005f;
float outputVal1, outputVal2;
for (size_t i = 0; i < 8; i++)
{
unpackHalf2x16(packHalf2x16(input[i][0], input[i][1]), &outputVal1, &outputVal2);
EXPECT_NEAR(input[i][0], outputVal1, floatFaultTolerance);
EXPECT_NEAR(input[i][1], outputVal2, floatFaultTolerance);
}
}
}
......@@ -1188,7 +1188,8 @@ TConstantUnion *TIntermConstantUnion::foldUnary(TOperator op, TInfoSink &infoSin
size_t objectSize = getType().getObjectSize();
if (op == EOpAny || op == EOpAll || op == EOpLength || op == EOpTranspose || op == EOpDeterminant ||
op == EOpInverse)
op == EOpInverse || op == EOpPackSnorm2x16 || op == EOpUnpackSnorm2x16 || op == EOpPackUnorm2x16 ||
op == EOpUnpackUnorm2x16 || op == EOpPackHalf2x16 || op == EOpUnpackHalf2x16)
{
// Do operations where the return type has a different number of components compared to the operand type.
TConstantUnion *resultArray = nullptr;
......@@ -1296,6 +1297,97 @@ TConstantUnion *TIntermConstantUnion::foldUnary(TOperator op, TInfoSink &infoSin
return nullptr;
}
case EOpPackSnorm2x16:
if (getType().getBasicType() == EbtFloat)
{
ASSERT(getType().getNominalSize() == 2);
resultArray = new TConstantUnion();
resultArray->setUConst(gl::packSnorm2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
break;
}
else
{
infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
return nullptr;
}
case EOpUnpackSnorm2x16:
if (getType().getBasicType() == EbtUInt)
{
resultArray = new TConstantUnion[2];
float f1, f2;
gl::unpackSnorm2x16(operandArray[0].getUConst(), &f1, &f2);
resultArray[0].setFConst(f1);
resultArray[1].setFConst(f2);
break;
}
else
{
infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
return nullptr;
}
case EOpPackUnorm2x16:
if (getType().getBasicType() == EbtFloat)
{
ASSERT(getType().getNominalSize() == 2);
resultArray = new TConstantUnion();
resultArray->setUConst(gl::packUnorm2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
break;
}
else
{
infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
return nullptr;
}
case EOpUnpackUnorm2x16:
if (getType().getBasicType() == EbtUInt)
{
resultArray = new TConstantUnion[2];
float f1, f2;
gl::unpackUnorm2x16(operandArray[0].getUConst(), &f1, &f2);
resultArray[0].setFConst(f1);
resultArray[1].setFConst(f2);
break;
}
else
{
infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
return nullptr;
}
case EOpPackHalf2x16:
if (getType().getBasicType() == EbtFloat)
{
ASSERT(getType().getNominalSize() == 2);
resultArray = new TConstantUnion();
resultArray->setUConst(gl::packHalf2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
break;
}
else
{
infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
return nullptr;
}
case EOpUnpackHalf2x16:
if (getType().getBasicType() == EbtUInt)
{
resultArray = new TConstantUnion[2];
float f1, f2;
gl::unpackHalf2x16(operandArray[0].getUConst(), &f1, &f2);
resultArray[0].setFConst(f1);
resultArray[1].setFConst(f2);
break;
}
else
{
infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
return nullptr;
}
break;
default:
break;
}
......
......@@ -16,6 +16,7 @@
'angle_unittests_sources':
[
'<(angle_path)/src/common/Optional_unittest.cpp',
'<(angle_path)/src/common/mathutil_unittest.cpp',
'<(angle_path)/src/common/matrix_utils_unittest.cpp',
'<(angle_path)/src/common/string_utils_unittest.cpp',
'<(angle_path)/src/common/utilities_unittest.cpp',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment