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 242 243 244 245 246 247 248 249 250 251 252 253 254 255
//
// Copyright 2016 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.
//
// ShaderVk.cpp:
// Implements the class methods for ShaderVk.
//
#include "libANGLE/renderer/vulkan/ShaderVk.h"
#include "common/debug.h"
#include "libANGLE/Context.h"
#include "libANGLE/Display.h"
#include "libANGLE/renderer/vulkan/ContextVk.h"
namespace rx
{
ShaderVk::ShaderVk(const gl::ShaderState &state) : ShaderImpl(state) {}
ShaderVk::~ShaderVk() {}
std::shared_ptr<ShaderTranslateTask> ShaderVk::compile(const gl::Context *context,
ShCompileOptions *options)
{
ContextVk *contextVk = vk::GetImpl(context);
if (context->isWebGL())
{
// Only WebGL requires initialization of local variables, others don't.
// Extra initialization in spirv shader may affect performance.
options->initializeUninitializedLocals = true;
// WebGL shaders may contain OOB array accesses which in turn cause undefined behavior,
// which may result in security issues. See https://crbug.com/1189110.
options->clampIndirectArrayBounds = true;
if (mState.getShaderType() != gl::ShaderType::Compute)
{
options->initOutputVariables = true;
}
}
if (contextVk->getFeatures().supportsSPIRV14.enabled)
{
options->emitSPIRV14 = true;
}
if (contextVk->getFeatures().retainSPIRVDebugInfo.enabled)
{
options->outputDebugInfo = true;
}
// robustBufferAccess on Vulkan doesn't support bound check on shader local variables
// but the GL_EXT_robustness does support.
// Enable the flag clampIndirectArrayBounds to ensure out of bounds local variable writes in
// shaders are protected when the context has GL_EXT_robustness enabled
if (contextVk->getShareGroup()->hasAnyContextWithRobustness())
{
options->clampIndirectArrayBounds = true;
}
if (contextVk->getFeatures().clampPointSize.enabled)
{
options->clampPointSize = true;
}
if (contextVk->getFeatures().emulateAdvancedBlendEquations.enabled)
{
options->addAdvancedBlendEquationsEmulation = true;
}
if (!contextVk->getFeatures().enablePrecisionQualifiers.enabled)
{
options->ignorePrecisionQualifiers = true;
}
if (contextVk->getFeatures().forceFragmentShaderPrecisionHighpToMediump.enabled)
{
options->forceShaderPrecisionHighpToMediump = true;
}
if (contextVk->getFeatures().clampFragDepth.enabled)
{
options->clampFragDepth = true;
}
if (!contextVk->getFeatures().supportsDepthClipControl.enabled)
{
options->addVulkanDepthCorrection = true;
}
if (contextVk->getFeatures().supportsTransformFeedbackExtension.enabled)
{
options->addVulkanXfbExtensionSupportCode = true;
}
else if (mState.getShaderType() == gl::ShaderType::Vertex &&
contextVk->getFeatures().emulateTransformFeedback.enabled)
{
options->addVulkanXfbEmulationSupportCode = true;
}
if (contextVk->getFeatures().roundOutputAfterDithering.enabled)
{
options->roundOutputAfterDithering = true;
}
if (contextVk->getFeatures().appendAliasedMemoryDecorations.enabled)
{
options->aliasedUnlessRestrict = true;
}
if (contextVk->getFeatures().explicitlyCastMediumpFloatTo16Bit.enabled)
{
options->castMediumpFloatTo16Bit = true;
}
if (contextVk->getExtensions().shaderPixelLocalStorageANGLE)
{
options->pls = contextVk->getNativePixelLocalStorageOptions();
}
if (contextVk->getFeatures().avoidOpSelectWithMismatchingRelaxedPrecision.enabled)
{
options->avoidOpSelectWithMismatchingRelaxedPrecision = true;
}
if (contextVk->getFeatures().wrapSwitchInIfTrue.enabled)
{
options->wrapSwitchInIfTrue = true;
}
if (contextVk->getFeatures().emulateR32fImageAtomicExchange.enabled)
{
options->emulateR32fImageAtomicExchange = true;
}
// https://issuetracker.google.com/406827038
// Unconditionally set this option to true for the Vulkan backend
options->preserveDenorms = true;
options->removeInactiveVariables = true;
if (contextVk->getFeatures().convertLowpAndMediumpFloatUniformsTo16Bits.enabled)
{
options->transformFloatUniformTo16Bits = true;
}
// The Vulkan backend needs no post-processing of the translated shader.
return std::shared_ptr<ShaderTranslateTask>(new ShaderTranslateTask);
}
std::shared_ptr<ShaderTranslateTask> ShaderVk::load(const gl::Context *context,
gl::BinaryInputStream *stream)
{
return std::shared_ptr<ShaderTranslateTask>(new ShaderTranslateTask);
}
std::string ShaderVk::getDebugInfo() const
{
const sh::BinaryBlob &spirv = mState.getCompiledState()->compiledBinary;
if (spirv.empty())
{
return "";
}
std::ostringstream blob;
if (!mState.getCompiledState()->inputVaryings.empty())
{
blob << "Inputs:";
for (const sh::ShaderVariable &var : mState.getCompiledState()->inputVaryings)
{
blob << " " << var.name;
}
blob << std::endl;
}
if (!mState.getCompiledState()->activeAttributes.empty())
{
blob << "Inputs:";
for (const sh::ShaderVariable &var : mState.getCompiledState()->activeAttributes)
{
blob << " " << var.name;
}
blob << std::endl;
}
if (!mState.getCompiledState()->outputVaryings.empty())
{
blob << "Outputs:";
for (const sh::ShaderVariable &var : mState.getCompiledState()->outputVaryings)
{
blob << " " << var.name;
}
blob << std::endl;
}
if (!mState.getCompiledState()->activeOutputVariables.empty())
{
blob << "Outputs:";
for (const sh::ShaderVariable &var : mState.getCompiledState()->activeOutputVariables)
{
blob << " " << var.name;
}
blob << std::endl;
}
if (!mState.getCompiledState()->uniforms.empty())
{
blob << "Uniforms:";
for (const sh::ShaderVariable &var : mState.getCompiledState()->uniforms)
{
blob << " " << var.name;
}
blob << std::endl;
}
if (!mState.getCompiledState()->uniformBlocks.empty())
{
blob << "Uniform blocks:";
for (const sh::InterfaceBlock &block : mState.getCompiledState()->uniformBlocks)
{
blob << " " << block.name;
}
blob << std::endl;
}
if (!mState.getCompiledState()->shaderStorageBlocks.empty())
{
blob << "Storage blocks:";
for (const sh::InterfaceBlock &block : mState.getCompiledState()->shaderStorageBlocks)
{
blob << " " << block.name;
}
blob << std::endl;
}
blob << R"(
Paste the following SPIR-V binary in https://www.khronos.org/spir/visualizer/ or pass to a recent build of `spirv-dis` (optionally with `--comment --nested-indent`)
Setting the environment variable ANGLE_FEATURE_OVERRIDES_ENABLED=retainSPIRVDebugInfo will retain debug info
)";
constexpr size_t kIndicesPerRow = 10;
size_t rowOffset = 0;
for (size_t index = 0; index < spirv.size(); ++index, ++rowOffset)
{
if (rowOffset == kIndicesPerRow)
{
blob << std::endl;
rowOffset = 0;
}
blob << "0x" << std::uppercase << std::setfill('0') << std::setw(8) << std::hex
<< spirv[index] << ",";
}
return blob.str();
}
} // namespace rx