Hash :
e82a2eab
Author :
Date :
2024-11-28T00:00:00
Support multisample 2D array textures on ES 3.0 contexts Supported via OES_texture_storage_multisample_2d_array enabled together with ANGLE_texture_multisample. Drive-by: Fixed exposure conditions in the OpenGL backend to match the implementation. Fixed: angleproject:382298321 Change-Id: I21b037aac7bebc35df267e9dd468088ebce35e71 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6075241 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Amirali Abdolrashidi <abdolrashidi@google.com> Commit-Queue: Amirali Abdolrashidi <abdolrashidi@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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
//
// Copyright 2002 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.
//
#include "compiler/translator/glsl/TranslatorESSL.h"
#include "angle_gl.h"
#include "common/utilities.h"
#include "compiler/translator/StaticType.h"
#include "compiler/translator/glsl/BuiltInFunctionEmulatorGLSL.h"
#include "compiler/translator/glsl/OutputESSL.h"
#include "compiler/translator/tree_ops/DeclarePerVertexBlocks.h"
#include "compiler/translator/tree_ops/RecordConstantPrecision.h"
#include "compiler/translator/tree_util/FindSymbolNode.h"
#include "compiler/translator/tree_util/ReplaceClipCullDistanceVariable.h"
#include "compiler/translator/tree_util/RunAtTheEndOfShader.h"
#include "compiler/translator/util.h"
namespace sh
{
namespace
{
bool EmulateClipOrigin(TCompiler *compiler, TIntermBlock *root, TSymbolTable *symbolTable)
{
// Skip the operation if gl_Position is not used.
const TIntermSymbol *positionSymbol = FindSymbolNode(root, ImmutableString("gl_Position"));
if (!positionSymbol)
{
return true;
}
const TType *type = StaticType::Get<EbtFloat, EbpHigh, EvqUniform, 1, 1>();
const TVariable *clipOrigin = new TVariable(symbolTable, ImmutableString("angle_ClipOrigin"),
type, SymbolType::AngleInternal);
DeclareGlobalVariable(root, clipOrigin);
// gl_Position.y *= angle_clipOrigin;
TIntermSwizzle *positionY =
new TIntermSwizzle(new TIntermSymbol(&positionSymbol->variable()), {1});
TIntermBinary *applyOrigin =
new TIntermBinary(EOpMulAssign, positionY, new TIntermSymbol(clipOrigin));
return RunAtTheEndOfShader(compiler, root, applyOrigin, symbolTable);
}
} // namespace
TranslatorESSL::TranslatorESSL(sh::GLenum type, ShShaderSpec spec)
: TCompiler(type, spec, SH_ESSL_OUTPUT)
{}
void TranslatorESSL::initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu,
const ShCompileOptions &compileOptions)
{
if (compileOptions.emulateAtan2FloatFunction)
{
InitBuiltInAtanFunctionEmulatorForGLSLWorkarounds(emu);
}
}
bool TranslatorESSL::translate(TIntermBlock *root,
const ShCompileOptions &compileOptions,
PerformanceDiagnostics * /*perfDiagnostics*/)
{
TInfoSinkBase &sink = getInfoSink().obj;
int shaderVer = getShaderVersion(); // Frontend shader version.
if (shaderVer == 300)
{
// Although ANGLE supports all these extensions with ESSL 3.00,
// some drivers may support the required functionality only
// with ESSL 3.10.
const bool hasExtensionsThatMayRequireES31 =
getResources().EXT_clip_cull_distance || getResources().ANGLE_clip_cull_distance ||
getResources().NV_shader_noperspective_interpolation ||
getResources().OES_shader_multisample_interpolation ||
getResources().ANGLE_texture_multisample ||
getResources().OES_texture_storage_multisample_2d_array;
// When PLS is implemented with shader images,
// ESSL 3.10 output is required.
const bool usesShaderImagesForPLS =
hasPixelLocalStorageUniforms() &&
compileOptions.pls.type == ShPixelLocalStorageType::ImageLoadStore;
if (hasExtensionsThatMayRequireES31 || usesShaderImagesForPLS)
{
shaderVer = 310;
}
}
if (shaderVer > 100)
{
sink << "#version " << shaderVer << " es\n";
}
// Write built-in extension behaviors.
writeExtensionBehavior(compileOptions);
// Write pragmas after extensions because some drivers consider pragmas
// like non-preprocessor tokens.
WritePragma(sink, compileOptions, getPragma());
if (!RecordConstantPrecision(this, root, &getSymbolTable()))
{
return false;
}
// Write emulated built-in functions if needed.
if (!getBuiltInFunctionEmulator().isOutputEmpty())
{
sink << "// BEGIN: Generated code for built-in function emulation\n\n";
if (getShaderType() == GL_FRAGMENT_SHADER)
{
sink << "#if defined(GL_FRAGMENT_PRECISION_HIGH)\n"
<< "#define emu_precision highp\n"
<< "#else\n"
<< "#define emu_precision mediump\n"
<< "#endif\n\n";
}
else
{
sink << "#define emu_precision highp\n";
}
getBuiltInFunctionEmulator().outputEmulatedFunctions(sink);
sink << "// END: Generated code for built-in function emulation\n\n";
}
if (getShaderType() == GL_VERTEX_SHADER)
{
// Emulate GL_CLIP_DISTANCEi_EXT state if needed
if (mMetadataFlags[MetadataFlags::HasClipDistance] &&
compileOptions.emulateClipDistanceState)
{
constexpr const ImmutableString kClipDistanceEnabledName("angle_ClipDistanceEnabled");
const TType *type = StaticType::Get<EbtUInt, EbpLow, EvqUniform, 1, 1>();
const TVariable *clipDistanceEnabled = new TVariable(
&getSymbolTable(), kClipDistanceEnabledName, type, SymbolType::AngleInternal);
const TIntermSymbol *clipDistanceEnabledSymbol = new TIntermSymbol(clipDistanceEnabled);
// AngleInternal variables don't get collected
ShaderVariable uniform;
uniform.name = kClipDistanceEnabledName.data();
uniform.mappedName = kClipDistanceEnabledName.data();
uniform.type = GLVariableType(*type);
uniform.precision = GLVariablePrecision(*type);
uniform.staticUse = true;
uniform.active = true;
uniform.binding = type->getLayoutQualifier().binding;
uniform.location = type->getLayoutQualifier().location;
uniform.offset = type->getLayoutQualifier().offset;
uniform.rasterOrdered = type->getLayoutQualifier().rasterOrdered;
uniform.readonly = type->getMemoryQualifier().readonly;
uniform.writeonly = type->getMemoryQualifier().writeonly;
mUniforms.push_back(uniform);
DeclareGlobalVariable(root, clipDistanceEnabled);
if (!ZeroDisabledClipDistanceAssignments(this, root, &getSymbolTable(), getShaderType(),
clipDistanceEnabledSymbol))
return false;
// The previous operation always redeclares gl_ClipDistance
if (!DeclarePerVertexBlocks(this, root, &getSymbolTable(), nullptr, nullptr))
return false;
}
else if (areClipDistanceOrCullDistanceUsed() &&
(IsExtensionEnabled(getExtensionBehavior(), TExtension::EXT_clip_cull_distance) ||
IsExtensionEnabled(getExtensionBehavior(), TExtension::ANGLE_clip_cull_distance)))
{
// When clip distance state emulation is not needed,
// the redeclared extension built-ins still should be moved to gl_PerVertex
if (!DeclarePerVertexBlocks(this, root, &getSymbolTable(), nullptr, nullptr))
return false;
}
if (compileOptions.emulateClipOrigin)
{
if (!EmulateClipOrigin(this, root, &getSymbolTable()))
{
return false;
}
}
}
if (getShaderType() == GL_FRAGMENT_SHADER)
{
EmitEarlyFragmentTestsGLSL(*this, sink);
WriteFragmentShaderLayoutQualifiers(sink, getAdvancedBlendEquations());
}
if (getShaderType() == GL_COMPUTE_SHADER)
{
EmitWorkGroupSizeGLSL(*this, sink);
}
if (getShaderType() == GL_GEOMETRY_SHADER_EXT)
{
WriteGeometryShaderLayoutQualifiers(
sink, getGeometryShaderInputPrimitiveType(), getGeometryShaderInvocations(),
getGeometryShaderOutputPrimitiveType(), getGeometryShaderMaxVertices());
}
// Write translated shader.
TOutputESSL outputESSL(this, sink, compileOptions);
root->traverse(&outputESSL);
return true;
}
bool TranslatorESSL::shouldFlattenPragmaStdglInvariantAll()
{
// If following the spec to the letter, we should not flatten this pragma.
// However, the spec's wording means that the pragma applies only to outputs.
// This contradicts the spirit of using the pragma,
// because if the pragma is used in a vertex shader,
// the only way to be able to link it to a fragment shader
// is to manually qualify each of fragment shader's inputs as invariant.
// Which defeats the purpose of this pragma - temporarily make all varyings
// invariant for debugging.
// Thus, we should be non-conformant to spec's letter here and flatten.
return true;
}
void TranslatorESSL::writeExtensionBehavior(const ShCompileOptions &compileOptions)
{
TInfoSinkBase &sink = getInfoSink().obj;
const TExtensionBehavior &extBehavior = getExtensionBehavior();
for (TExtensionBehavior::const_iterator iter = extBehavior.begin(); iter != extBehavior.end();
++iter)
{
if (iter->second != EBhUndefined)
{
const bool isMultiview = (iter->first == TExtension::OVR_multiview) ||
(iter->first == TExtension::OVR_multiview2);
if (getResources().NV_shader_framebuffer_fetch &&
iter->first == TExtension::EXT_shader_framebuffer_fetch)
{
sink << "#extension GL_NV_shader_framebuffer_fetch : "
<< GetBehaviorString(iter->second) << "\n";
}
else if (getResources().NV_draw_buffers && iter->first == TExtension::EXT_draw_buffers)
{
sink << "#extension GL_NV_draw_buffers : " << GetBehaviorString(iter->second)
<< "\n";
}
else if (isMultiview)
{
// Only either OVR_multiview OR OVR_multiview2 should be emitted.
if ((iter->first != TExtension::OVR_multiview) ||
!IsExtensionEnabled(extBehavior, TExtension::OVR_multiview2))
{
EmitMultiviewGLSL(*this, compileOptions, iter->first, iter->second, sink);
}
}
else if (iter->first == TExtension::EXT_geometry_shader ||
iter->first == TExtension::OES_geometry_shader)
{
sink << "#ifdef GL_EXT_geometry_shader\n"
<< "#extension GL_EXT_geometry_shader : " << GetBehaviorString(iter->second)
<< "\n"
<< "#elif defined GL_OES_geometry_shader\n"
<< "#extension GL_OES_geometry_shader : " << GetBehaviorString(iter->second)
<< "\n";
if (iter->second == EBhRequire)
{
sink << "#else\n"
<< "#error \"No geometry shader extensions available.\" // Only generate "
"this if the extension is \"required\"\n";
}
sink << "#endif\n";
}
else if (iter->first == TExtension::ANGLE_multi_draw)
{
// Don't emit anything. This extension is emulated
ASSERT(compileOptions.emulateGLDrawID);
continue;
}
else if (iter->first == TExtension::ANGLE_base_vertex_base_instance_shader_builtin)
{
// Don't emit anything. This extension is emulated
ASSERT(compileOptions.emulateGLBaseVertexBaseInstance);
continue;
}
else if (iter->first == TExtension::EXT_clip_cull_distance ||
iter->first == TExtension::ANGLE_clip_cull_distance)
{
sink << "#extension GL_EXT_clip_cull_distance : " << GetBehaviorString(iter->second)
<< "\n";
if (areClipDistanceOrCullDistanceUsed())
{
sink << "#extension GL_EXT_shader_io_blocks : "
<< GetBehaviorString(iter->second) << "\n";
}
}
else if (iter->first == TExtension::ANGLE_shader_pixel_local_storage)
{
if (compileOptions.pls.type == ShPixelLocalStorageType::FramebufferFetch)
{
// Just enable the extension. Appropriate warnings will be generated by the
// frontend compiler for GL_ANGLE_shader_pixel_local_storage, if desired.
sink << "#extension GL_EXT_shader_framebuffer_fetch : enable\n";
}
continue;
}
else if (iter->first == TExtension::EXT_shader_framebuffer_fetch)
{
sink << "#extension GL_EXT_shader_framebuffer_fetch : "
<< GetBehaviorString(iter->second) << "\n";
continue;
}
else if (iter->first == TExtension::EXT_shader_framebuffer_fetch_non_coherent)
{
sink << "#extension GL_EXT_shader_framebuffer_fetch_non_coherent : "
<< GetBehaviorString(iter->second) << "\n";
continue;
}
else if (iter->first == TExtension::ANGLE_texture_multisample)
{
// Don't emit anything. This functionality is core in ESSL 3.10.
continue;
}
else if (iter->first == TExtension::WEBGL_video_texture)
{
// Don't emit anything. This extension is emulated
// TODO(crbug.com/776222): support external image.
continue;
}
else
{
sink << "#extension " << GetExtensionNameString(iter->first) << " : "
<< GetBehaviorString(iter->second) << "\n";
}
}
}
}
} // namespace sh