Hash :
eb0d5997
Author :
Date :
2023-09-15T16:41:13
Move set/get uniform machinery to ProgramExecutable This is done because some uniforms are internally added by the compiler (draw ID, base vertex, and base instance) and are automatically set **on the installed executable**. This change fixes scenarios where a draw is done after a program has failed a relink, and therefore is unable to correctly set the uniforms (as it does not have access to the executable that is installed). It also fixes draws that use those uniforms in a PPO. Bug: angleproject:8297 Change-Id: Id74b4984b88aa09b5b81be1c91412d6c91711136 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4864693 Reviewed-by: Charlie Lao <cclao@google.com> Reviewed-by: Yuxin Hu <yuxinhu@google.com> Commit-Queue: Shahbaz Youssefi <syoussefi@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
//
// 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 {};
}
bool DisableFastMathForShaderCompilation(mtl::Context *context)
{
return context->getDisplay()->getFeatures().intelDisableFastMath.enabled;
}
bool UsesInvariance(const mtl::TranslatedShaderInfo *translatedMslInfo)
{
return translatedMslInfo->hasInvariant;
}
class Std140BlockLayoutEncoderFactory : public gl::CustomBlockLayoutEncoderFactory
{
public:
sh::BlockLayoutEncoder *makeEncoder() override { return new sh::Std140BlockEncoder(); }
};
class CompileMslTask final : public LinkSubTask
{
public:
CompileMslTask(ContextMtl *context,
mtl::TranslatedShaderInfo *translatedMslInfo,
const std::map<std::string, std::string> &substitutionMacros)
: mContext(context),
mTranslatedMslInfo(translatedMslInfo),
mSubstitutionMacros(substitutionMacros)
{}
~CompileMslTask() override = default;
void operator()() override
{
mResult = CreateMslShaderLib(mContext, mInfoLog, mTranslatedMslInfo, mSubstitutionMacros);
}
angle::Result getResult(const gl::Context *context, gl::InfoLog &infoLog) override
{
if (!mInfoLog.empty())
{
infoLog << mInfoLog.str();
}
return mResult;
}
private:
// TODO: remove this, inherit from mtl::Context and ensure thread-safety.
// http://anglebug.com/8297
ContextMtl *mContext;
gl::InfoLog mInfoLog;
mtl::TranslatedShaderInfo *mTranslatedMslInfo;
std::map<std::string, std::string> mSubstitutionMacros;
angle::Result mResult = angle::Result::Continue;
};
} // namespace
class ProgramMtl::LinkTaskMtl final : public LinkTask
{
public:
LinkTaskMtl(const gl::Context *context, ProgramMtl *program)
: mContext(context), mProgram(program)
{}
~LinkTaskMtl() override = default;
std::vector<std::shared_ptr<LinkSubTask>> link(
const gl::ProgramLinkedResources &resources,
const gl::ProgramMergedVaryings &mergedVaryings) override
{
std::vector<std::shared_ptr<LinkSubTask>> subTasks;
mResult = mProgram->linkJobImpl(mContext, resources, &subTasks);
return subTasks;
}
angle::Result getResult(const gl::Context *context, gl::InfoLog &infoLog) override
{
return mResult;
}
private:
// TODO: remove this, inherit from mtl::Context and ensure thread-safety.
// http://anglebug.com/8297
const gl::Context *mContext;
ProgramMtl *mProgram;
angle::Result mResult = angle::Result::Continue;
};
class ProgramMtl::LoadTaskMtl final : public LinkTask
{
public:
LoadTaskMtl(std::vector<std::shared_ptr<LinkSubTask>> &&subTasks)
: mSubTasks(std::move(subTasks))
{}
~LoadTaskMtl() override = default;
std::vector<std::shared_ptr<LinkSubTask>> load() override { return mSubTasks; }
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)
{
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/8297
std::vector<std::shared_ptr<LinkSubTask>> subTasks;
ANGLE_TRY(compileMslShaderLibs(context, &subTasks));
*loadTaskOut = std::shared_ptr<LinkTask>(new LoadTaskMtl(std::move(subTasks)));
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)
{
*linkTaskOut = std::shared_ptr<LinkTask>(new LinkTaskMtl(context, this));
return angle::Result::Continue;
}
angle::Result ProgramMtl::linkJobImpl(const gl::Context *context,
const gl::ProgramLinkedResources &resources,
std::vector<std::shared_ptr<LinkSubTask>> *subTasksOut)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
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(contextMtl, mState.getAttachedShaders()));
executableMtl->linkUpdateHasFlatAttributes(mState.getAttachedShader(gl::ShaderType::Vertex));
gl::ShaderMap<std::string> shaderSources;
mtl::MSLGetShaderSource(mState, resources, &shaderSources);
ANGLE_TRY(mtl::MTLGetMSL(contextMtl, mState.getExecutable(), contextMtl->getCaps(),
shaderSources, mAttachedShaders,
&executableMtl->mMslShaderTranslateInfo));
executableMtl->mMslXfbOnlyVertexShaderInfo =
executableMtl->mMslShaderTranslateInfo[gl::ShaderType::Vertex];
return compileMslShaderLibs(context, subTasksOut);
}
angle::Result ProgramMtl::compileMslShaderLibs(
const gl::Context *context,
std::vector<std::shared_ptr<LinkSubTask>> *subTasksOut)
{
ANGLE_TRACE_EVENT0("gpu.angle", "ProgramMtl::compileMslShaderLibs");
gl::InfoLog &infoLog = mState.getExecutable().getInfoLog();
ContextMtl *contextMtl = mtl::GetImpl(context);
ProgramExecutableMtl *executableMtl = getExecutable();
bool asyncCompile =
contextMtl->getDisplay()->getFeatures().enableParallelMtlLibraryCompilation.enabled;
mtl::LibraryCache &libraryCache = contextMtl->getDisplay()->getLibraryCache();
for (gl::ShaderType shaderType : gl::kAllGLES2ShaderTypes)
{
mtl::TranslatedShaderInfo *translateInfo =
&executableMtl->mMslShaderTranslateInfo[shaderType];
std::map<std::string, std::string> macros = GetDefaultSubstitutionDictionary();
bool disableFastMath = DisableFastMathForShaderCompilation(contextMtl);
bool usesInvariance = UsesInvariance(translateInfo);
// 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(contextMtl, translateInfo, macros));
}
else
{
ANGLE_TRY(CreateMslShaderLib(contextMtl, 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