Hash :
c1cdc2b5
Author :
Date :
2025-08-22T15:23:58
Transform SPIRV to use 16-bit float for lower precision uniforms This change adds a ShCompileOption flag transformFloatUniformTo16Bits. The flag is turned on in vulkan backends where VK_KHR_16bit_storage extension is supported, and uniformAndStorageBuffer16BitAccess feature is supported. When the compiler flag is turned on, in the generated SPIRV, float data types in mediump and lowp uniforms are transformed from 32-bit to 16-bit. The 16-bit float uniform data is converted to 32-bit with OpFConvert instruction upon loading in SPIRV, this is to minimize the changes in OutputSPIRV.cpp. The converted variable is decorated with RelaxedPrecision, so that SPIRV compiler should be able to treat the converted variable as 16 bits, and the hardware can still benefit from reduced precision floats. The frontend is also notified such SPIRV shader changes by setting the isFloat16 bit in CollectVariables() step, and the frontend will transform float uniform data from 32-bit to 16-bit before storing the data into memory. That way, the uniform data that SPIRV shader reads matches with the uniform data type transformed in the SPIRV shader. This change also updates some test code to allow relative 2^-10 precision wiggle room for mediump uniform floats. This is valid according to spec: https://developer.arm.com/documentation/102502/0101/Shader-precision Bug: angleproject:405795981 Bug: angleproject:440941211 Change-Id: I05db7f5ef744df513fbad87cfed8aa173890ec26 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6851560 Reviewed-by: Amirali Abdolrashidi <abdolrashidi@google.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Yuxin Hu <yuxinhu@google.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
//
// 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.
//
// PackUnpackTest:
// Tests the corrrectness of opengl 4.1 emulation of pack/unpack built-in functions.
//
#include "test_utils/ANGLETest.h"
using namespace angle;
namespace
{
class PackUnpackTest : public ANGLETest<>
{
protected:
PackUnpackTest()
{
setWindowWidth(16);
setWindowHeight(16);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
}
void testSetUp() override
{
// Fragment Shader source
constexpr char kSNormFS[] = R"(#version 300 es
precision mediump float;
uniform mediump vec2 v;
layout(location = 0) out mediump vec4 fragColor;
void main()
{
highp uint u = packSnorm2x16(v);
vec2 r = unpackSnorm2x16(u);
fragColor = vec4(r, 0.0, 1.0);
})";
// Fragment Shader source
constexpr char kUNormFS[] = R"(#version 300 es
precision mediump float;
uniform mediump vec2 v;
layout(location = 0) out mediump vec4 fragColor;
void main()
{
highp uint u = packUnorm2x16(v);
vec2 r = unpackUnorm2x16(u);
fragColor = vec4(r, 0.0, 1.0);
})";
// Fragment Shader source
constexpr char kHalfFS[] = R"(#version 300 es
precision mediump float;
uniform mediump vec2 v;
layout(location = 0) out mediump vec4 fragColor;
void main()
{
highp uint u = packHalf2x16(v);
vec2 r = unpackHalf2x16(u);
fragColor = vec4(r, 0.0, 1.0);
})";
mSNormProgram = CompileProgram(essl3_shaders::vs::Simple(), kSNormFS);
mUNormProgram = CompileProgram(essl3_shaders::vs::Simple(), kUNormFS);
mHalfProgram = CompileProgram(essl3_shaders::vs::Simple(), kHalfFS);
if (mSNormProgram == 0 || mUNormProgram == 0 || mHalfProgram == 0)
{
FAIL() << "shader compilation failed.";
}
glGenTextures(1, &mOffscreenTexture2D);
glBindTexture(GL_TEXTURE_2D, mOffscreenTexture2D);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG32F, getWindowWidth(), getWindowHeight());
glGenFramebuffers(1, &mOffscreenFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, mOffscreenFramebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
mOffscreenTexture2D, 0);
glViewport(0, 0, 16, 16);
const GLfloat color[] = {1.0f, 1.0f, 0.0f, 1.0f};
glClearBufferfv(GL_COLOR, 0, color);
}
void testTearDown() override
{
glDeleteTextures(1, &mOffscreenTexture2D);
glDeleteFramebuffers(1, &mOffscreenFramebuffer);
glDeleteProgram(mSNormProgram);
glDeleteProgram(mUNormProgram);
glDeleteProgram(mHalfProgram);
}
void compareBeforeAfter(GLuint program, float input1, float input2)
{
compareBeforeAfter(program, input1, input2, input1, input2);
}
void compareBeforeAfter(GLuint program,
float input1,
float input2,
float expect1,
float expect2)
{
GLint vec2Location = glGetUniformLocation(program, "v");
glUseProgram(program);
glUniform2f(vec2Location, input1, input2);
drawQuad(program, essl3_shaders::PositionAttrib(), 0.5f);
ASSERT_GL_NO_ERROR();
GLfloat p[2] = {0};
glReadPixels(8, 8, 1, 1, GL_RG, GL_FLOAT, p);
ASSERT_GL_NO_ERROR();
static const double epsilon = 0.0005;
EXPECT_NEAR(p[0], expect1, epsilon);
EXPECT_NEAR(p[1], expect2, epsilon);
}
GLuint mSNormProgram;
GLuint mUNormProgram;
GLuint mHalfProgram;
GLuint mOffscreenFramebuffer;
GLuint mOffscreenTexture2D;
};
// Test the correctness of packSnorm2x16 and unpackSnorm2x16 functions calculating normal floating
// numbers.
TEST_P(PackUnpackTest, PackUnpackSnormNormal)
{
// Expect the shader to output the same value as the input
compareBeforeAfter(mSNormProgram, 0.5f, -0.2f);
compareBeforeAfter(mSNormProgram, -0.35f, 0.75f);
compareBeforeAfter(mSNormProgram, 0.00392f, -0.99215f);
compareBeforeAfter(mSNormProgram, 1.0f, -0.00392f);
}
// Test the correctness of packSnorm2x16 and unpackSnorm2x16 functions calculating normal floating
// numbers.
TEST_P(PackUnpackTest, PackUnpackUnormNormal)
{
// Expect the shader to output the same value as the input
compareBeforeAfter(mUNormProgram, 0.5f, 0.2f, 0.5f, 0.2f);
compareBeforeAfter(mUNormProgram, 0.35f, 0.75f, 0.35f, 0.75f);
compareBeforeAfter(mUNormProgram, 0.00392f, 0.99215f, 0.00392f, 0.99215f);
compareBeforeAfter(mUNormProgram, 1.0f, 0.00392f, 1.0f, 0.00392f);
}
// Test the correctness of packHalf2x16 and unpackHalf2x16 functions calculating normal floating
// numbers.
TEST_P(PackUnpackTest, PackUnpackHalfNormal)
{
// Expect the shader to output the same value as the input
compareBeforeAfter(mHalfProgram, 0.5f, -0.2f);
compareBeforeAfter(mHalfProgram, -0.35f, 0.75f);
compareBeforeAfter(mHalfProgram, 0.00392f, -0.99215f);
compareBeforeAfter(mHalfProgram, 1.0f, -0.00392f);
}
// Test the correctness of packSnorm2x16 and unpackSnorm2x16 functions calculating subnormal
// floating numbers.
TEST_P(PackUnpackTest, PackUnpackSnormSubnormal)
{
// Expect the shader to output the same value as the input
compareBeforeAfter(mSNormProgram, 0.00001f, -0.00001f);
}
// Test the correctness of packUnorm2x16 and unpackUnorm2x16 functions calculating subnormal
// floating numbers.
TEST_P(PackUnpackTest, PackUnpackUnormSubnormal)
{
// Expect the shader to output the same value as the input for positive numbers and clamp
// to [0, 1]
compareBeforeAfter(mUNormProgram, 0.00001f, -0.00001f, 0.00001f, 0.0f);
}
// Test the correctness of packHalf2x16 and unpackHalf2x16 functions calculating subnormal floating
// numbers.
TEST_P(PackUnpackTest, PackUnpackHalfSubnormal)
{
// Expect the shader to output the same value as the input
compareBeforeAfter(mHalfProgram, 0.00001f, -0.00001f);
}
// Test the correctness of packSnorm2x16 and unpackSnorm2x16 functions calculating zero floating
// numbers.
TEST_P(PackUnpackTest, PackUnpackSnormZero)
{
// Expect the shader to output the same value as the input
compareBeforeAfter(mSNormProgram, 0.00000f, -0.00000f);
}
// Test the correctness of packUnorm2x16 and unpackUnorm2x16 functions calculating zero floating
// numbers.
TEST_P(PackUnpackTest, PackUnpackUnormZero)
{
compareBeforeAfter(mUNormProgram, 0.00000f, -0.00000f, 0.00000f, 0.00000f);
}
// Test the correctness of packHalf2x16 and unpackHalf2x16 functions calculating zero floating
// numbers.
TEST_P(PackUnpackTest, PackUnpackHalfZero)
{
// Expect the shader to output the same value as the input
compareBeforeAfter(mHalfProgram, 0.00000f, -0.00000f);
}
// Test the correctness of packUnorm2x16 and unpackUnorm2x16 functions calculating overflow floating
// numbers.
TEST_P(PackUnpackTest, PackUnpackUnormOverflow)
{
// Expect the shader to clamp the input to [0, 1]
// mediump float data range is [2^-14, 2^14]
compareBeforeAfter(mUNormProgram, 16384.0f, -16384.0f, 1.0f, 0.0f);
}
// Test the correctness of packSnorm2x16 and unpackSnorm2x16 functions calculating overflow floating
// numbers.
TEST_P(PackUnpackTest, PackUnpackSnormOverflow)
{
// Expect the shader to clamp the input to [-1, 1]
// mediump float data range is [2^-14, 2^14]
compareBeforeAfter(mSNormProgram, 16384.0f, -16384.0f, 1.0f, -1.0f);
}
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(PackUnpackTest);
ANGLE_INSTANTIATE_TEST_ES3(PackUnpackTest);
} // namespace