Hash :
beae1b4a
Author :
Date :
2025-06-19T00:00:00
Metal: Remove obsolete workarounds Removed Intel-specific Metal workarounds that are not used on macOS 12 and later. Removed a redundant version check. Bug: angleproject:427600175 Change-Id: I34c7e53108f7e030512c9436ab2b9ae38ad17946 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6672312 Commit-Queue: Alexey Knyazev <lexa.knyazev@gmail.com> Reviewed-by: Geoff Lang <geofflang@chromium.org>
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 345 346 347 348 349 350 351 352 353 354
//
// Copyright 2019 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.
//
// ProgramMtl.mm:
// Implements the class methods for ProgramMtl.
//
#include "libANGLE/renderer/metal/ProgramMtl.h"
#include <TargetConditionals.h>
#include <sstream>
#include "common/WorkerThread.h"
#include "common/debug.h"
#include "common/system_utils.h"
#include "libANGLE/Context.h"
#include "libANGLE/ProgramLinkedResources.h"
#include "libANGLE/renderer/metal/CompilerMtl.h"
#include "libANGLE/renderer/metal/ContextMtl.h"
#include "libANGLE/renderer/metal/DisplayMtl.h"
#include "libANGLE/renderer/metal/blocklayoutMetal.h"
#include "libANGLE/renderer/metal/mtl_msl_utils.h"
#include "libANGLE/renderer/metal/mtl_utils.h"
#include "libANGLE/renderer/metal/renderermtl_utils.h"
#include "libANGLE/renderer/renderer_utils.h"
#include "libANGLE/trace.h"
namespace rx
{
namespace
{
inline std::map<std::string, std::string> GetDefaultSubstitutionDictionary()
{
return {};
}
class Std140BlockLayoutEncoderFactory : public gl::CustomBlockLayoutEncoderFactory
{
public:
sh::BlockLayoutEncoder *makeEncoder() override { return new sh::Std140BlockEncoder(); }
};
class CompileMslTask final : public LinkSubTask, public mtl::Context
{
public:
CompileMslTask(DisplayMtl *displayMtl,
mtl::TranslatedShaderInfo *translatedMslInfo,
const std::map<std::string, std::string> &substitutionMacros)
: mtl::Context(displayMtl),
mTranslatedMslInfo(translatedMslInfo),
mSubstitutionMacros(substitutionMacros)
{}
~CompileMslTask() override = default;
void operator()() override
{
mResult = CreateMslShaderLib(this, mInfoLog, mTranslatedMslInfo, mSubstitutionMacros);
}
angle::Result getResult(const gl::Context *context, gl::InfoLog &infoLog) override
{
if (!mInfoLog.empty())
{
infoLog << mInfoLog.str();
}
if (mErrorCode != GL_NO_ERROR)
{
mtl::GetImpl(context)->handleError(mErrorCode, mErrorMessage.c_str(), mErrorFile,
mErrorFunction, mErrorLine);
return angle::Result::Stop;
}
return mResult;
}
// override mtl::ErrorHandler
void handleError(GLenum glErrorCode,
const char *message,
const char *file,
const char *function,
unsigned int line) override
{
mErrorCode = glErrorCode;
mErrorMessage = message;
mErrorFile = file;
mErrorFunction = function;
mErrorLine = line;
}
private:
mtl::TranslatedShaderInfo *mTranslatedMslInfo;
std::map<std::string, std::string> mSubstitutionMacros;
angle::Result mResult = angle::Result::Continue;
gl::InfoLog mInfoLog;
GLenum mErrorCode = GL_NO_ERROR;
std::string mErrorMessage;
const char *mErrorFile = nullptr;
const char *mErrorFunction = nullptr;
unsigned int mErrorLine = 0;
};
} // namespace
class ProgramMtl::LinkTaskMtl final : public LinkTask, public mtl::Context
{
public:
LinkTaskMtl(DisplayMtl *displayMtl, ProgramMtl *program)
: mtl::Context(displayMtl), mProgram(program)
{}
~LinkTaskMtl() override = default;
void link(const gl::ProgramLinkedResources &resources,
const gl::ProgramMergedVaryings &mergedVaryings,
std::vector<std::shared_ptr<LinkSubTask>> *linkSubTasksOut,
std::vector<std::shared_ptr<LinkSubTask>> *postLinkSubTasksOut) override
{
ASSERT(linkSubTasksOut && linkSubTasksOut->empty());
ASSERT(postLinkSubTasksOut && postLinkSubTasksOut->empty());
mResult = mProgram->linkJobImpl(this, resources, linkSubTasksOut);
return;
}
angle::Result getResult(const gl::Context *context, gl::InfoLog &infoLog) override
{
if (mErrorCode != GL_NO_ERROR)
{
mtl::GetImpl(context)->handleError(mErrorCode, mErrorMessage.c_str(), mErrorFile,
mErrorFunction, mErrorLine);
return angle::Result::Stop;
}
return mResult;
}
// override mtl::ErrorHandler
void handleError(GLenum glErrorCode,
const char *message,
const char *file,
const char *function,
unsigned int line) override
{
mErrorCode = glErrorCode;
mErrorMessage = message;
mErrorFile = file;
mErrorFunction = function;
mErrorLine = line;
}
private:
ProgramMtl *mProgram;
angle::Result mResult = angle::Result::Continue;
GLenum mErrorCode = GL_NO_ERROR;
std::string mErrorMessage;
const char *mErrorFile = nullptr;
const char *mErrorFunction = nullptr;
unsigned int mErrorLine = 0;
};
class ProgramMtl::LoadTaskMtl final : public LinkTask
{
public:
LoadTaskMtl(std::vector<std::shared_ptr<LinkSubTask>> &&subTasks)
: mSubTasks(std::move(subTasks))
{}
~LoadTaskMtl() override = default;
void load(std::vector<std::shared_ptr<LinkSubTask>> *linkSubTasksOut,
std::vector<std::shared_ptr<LinkSubTask>> *postLinkSubTasksOut) override
{
ASSERT(linkSubTasksOut && linkSubTasksOut->empty());
ASSERT(postLinkSubTasksOut && postLinkSubTasksOut->empty());
*linkSubTasksOut = mSubTasks;
return;
}
angle::Result getResult(const gl::Context *context, gl::InfoLog &infoLog) override
{
return angle::Result::Continue;
}
private:
std::vector<std::shared_ptr<LinkSubTask>> mSubTasks;
};
// ProgramArgumentBufferEncoderMtl implementation
void ProgramArgumentBufferEncoderMtl::reset(ContextMtl *contextMtl)
{
metalArgBufferEncoder = nil;
bufferPool.destroy(contextMtl);
}
// ProgramShaderObjVariantMtl implementation
void ProgramShaderObjVariantMtl::reset(ContextMtl *contextMtl)
{
metalShader = nil;
uboArgBufferEncoder.reset(contextMtl);
translatedSrcInfo = nullptr;
}
// ProgramMtl implementation
ProgramMtl::ProgramMtl(const gl::ProgramState &state) : ProgramImpl(state) {}
ProgramMtl::~ProgramMtl() = default;
void ProgramMtl::destroy(const gl::Context *context)
{
getExecutable()->reset(mtl::GetImpl(context));
}
angle::Result ProgramMtl::load(const gl::Context *context,
gl::BinaryInputStream *stream,
std::shared_ptr<LinkTask> *loadTaskOut,
egl::CacheGetResult *resultOut)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
// NOTE(hqle): No transform feedbacks for now, since we only support ES 2.0 atm
ANGLE_TRY(getExecutable()->load(contextMtl, stream));
// TODO: parallelize the above too. http://anglebug.com/41488637
std::vector<std::shared_ptr<LinkSubTask>> subTasks;
ANGLE_TRY(compileMslShaderLibs(contextMtl, &subTasks));
*loadTaskOut = std::shared_ptr<LinkTask>(new LoadTaskMtl(std::move(subTasks)));
*resultOut = egl::CacheGetResult::Success;
return angle::Result::Continue;
}
void ProgramMtl::save(const gl::Context *context, gl::BinaryOutputStream *stream)
{
getExecutable()->save(stream);
}
void ProgramMtl::setBinaryRetrievableHint(bool retrievable) {}
void ProgramMtl::setSeparable(bool separable)
{
UNIMPLEMENTED();
}
void ProgramMtl::prepareForLink(const gl::ShaderMap<ShaderImpl *> &shaders)
{
for (gl::ShaderType shaderType : gl::AllShaderTypes())
{
mAttachedShaders[shaderType].reset();
if (shaders[shaderType] != nullptr)
{
const ShaderMtl *shaderMtl = GetAs<ShaderMtl>(shaders[shaderType]);
mAttachedShaders[shaderType] = shaderMtl->getCompiledState();
}
}
}
angle::Result ProgramMtl::link(const gl::Context *context, std::shared_ptr<LinkTask> *linkTaskOut)
{
DisplayMtl *displayMtl = mtl::GetImpl(context)->getDisplay();
*linkTaskOut = std::shared_ptr<LinkTask>(new LinkTaskMtl(displayMtl, this));
return angle::Result::Continue;
}
angle::Result ProgramMtl::linkJobImpl(mtl::Context *context,
const gl::ProgramLinkedResources &resources,
std::vector<std::shared_ptr<LinkSubTask>> *subTasksOut)
{
ProgramExecutableMtl *executableMtl = getExecutable();
// Link resources before calling GetShaderSource to make sure they are ready for the set/binding
// assignment done in that function.
linkResources(resources);
ANGLE_TRY(executableMtl->initDefaultUniformBlocks(context, mState.getAttachedShaders()));
executableMtl->linkUpdateHasFlatAttributes(mState.getAttachedShader(gl::ShaderType::Vertex));
gl::ShaderMap<std::string> shaderSources;
mtl::MSLGetShaderSource(mState, resources, &shaderSources);
ANGLE_TRY(mtl::MTLGetMSL(context->getDisplay()->getFeatures(), mState.getExecutable(),
shaderSources, mAttachedShaders,
&executableMtl->mMslShaderTranslateInfo));
executableMtl->mMslXfbOnlyVertexShaderInfo =
executableMtl->mMslShaderTranslateInfo[gl::ShaderType::Vertex];
return compileMslShaderLibs(context, subTasksOut);
}
angle::Result ProgramMtl::compileMslShaderLibs(
mtl::Context *context,
std::vector<std::shared_ptr<LinkSubTask>> *subTasksOut)
{
ANGLE_TRACE_EVENT0("gpu.angle", "ProgramMtl::compileMslShaderLibs");
gl::InfoLog &infoLog = mState.getExecutable().getInfoLog();
DisplayMtl *displayMtl = context->getDisplay();
ProgramExecutableMtl *executableMtl = getExecutable();
bool asyncCompile = displayMtl->getFeatures().enableParallelMtlLibraryCompilation.enabled;
mtl::LibraryCache &libraryCache = displayMtl->getLibraryCache();
for (gl::ShaderType shaderType : gl::kAllGLES2ShaderTypes)
{
mtl::TranslatedShaderInfo *translateInfo =
&executableMtl->mMslShaderTranslateInfo[shaderType];
std::map<std::string, std::string> macros = GetDefaultSubstitutionDictionary();
const bool disableFastMath = translateInfo->hasIsnanOrIsinf;
const bool usesInvariance = translateInfo->hasInvariant;
// Check if the shader is already in the cache and use it instead of spawning a new thread
translateInfo->metalLibrary = libraryCache.get(translateInfo->metalShaderSource, macros,
disableFastMath, usesInvariance);
if (!translateInfo->metalLibrary)
{
if (asyncCompile)
{
subTasksOut->emplace_back(new CompileMslTask(displayMtl, translateInfo, macros));
}
else
{
ANGLE_TRY(CreateMslShaderLib(context, infoLog, translateInfo, macros));
}
}
}
return angle::Result::Continue;
}
void ProgramMtl::linkResources(const gl::ProgramLinkedResources &resources)
{
Std140BlockLayoutEncoderFactory std140EncoderFactory;
gl::ProgramLinkedResourcesLinker linker(&std140EncoderFactory);
linker.linkResources(mState, resources);
}
GLboolean ProgramMtl::validate(const gl::Caps &caps)
{
// No-op. The spec is very vague about the behavior of validation.
return GL_TRUE;
}
} // namespace rx