Hash :
d3d81498
Author :
Date :
2023-09-11T16:11:33
Add metrics for shader compilation time and shader blob size Log the time it takes for the system compiler to compile Metal and D3D shaders. Log the D3D shader blob size to get a sense of storage size needed. Bug: chromium:1481238 Change-Id: I300102dcb035f42e91d7819cd9465ff18436abf3 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4851196 Reviewed-by: Peng Huang <penghuang@chromium.org> Commit-Queue: 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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
//
// Copyright 2014 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 "libANGLE/renderer/d3d/HLSLCompiler.h"
#include <sstream>
#include "common/system_utils.h"
#include "common/utilities.h"
#include "libANGLE/Context.h"
#include "libANGLE/Program.h"
#include "libANGLE/features.h"
#include "libANGLE/histogram_macros.h"
#include "libANGLE/renderer/d3d/ContextD3D.h"
#include "libANGLE/trace.h"
namespace
{
#if ANGLE_APPEND_ASSEMBLY_TO_SHADER_DEBUG_INFO
# ifdef CREATE_COMPILER_FLAG_INFO
# undef CREATE_COMPILER_FLAG_INFO
# endif
# define CREATE_COMPILER_FLAG_INFO(flag) \
{ \
flag, #flag \
}
struct CompilerFlagInfo
{
UINT mFlag;
const char *mName;
};
CompilerFlagInfo CompilerFlagInfos[] = {
// NOTE: The data below is copied from d3dcompiler.h
// If something changes there it should be changed here as well
CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_DEBUG), // (1 << 0)
CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_SKIP_VALIDATION), // (1 << 1)
CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_SKIP_OPTIMIZATION), // (1 << 2)
CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_PACK_MATRIX_ROW_MAJOR), // (1 << 3)
CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR), // (1 << 4)
CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_PARTIAL_PRECISION), // (1 << 5)
CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_FORCE_VS_SOFTWARE_NO_OPT), // (1 << 6)
CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_FORCE_PS_SOFTWARE_NO_OPT), // (1 << 7)
CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_NO_PRESHADER), // (1 << 8)
CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_AVOID_FLOW_CONTROL), // (1 << 9)
CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_PREFER_FLOW_CONTROL), // (1 << 10)
CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_ENABLE_STRICTNESS), // (1 << 11)
CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY), // (1 << 12)
CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_IEEE_STRICTNESS), // (1 << 13)
CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_OPTIMIZATION_LEVEL0), // (1 << 14)
CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_OPTIMIZATION_LEVEL1), // 0
CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_OPTIMIZATION_LEVEL2), // ((1 << 14) | (1 << 15))
CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_OPTIMIZATION_LEVEL3), // (1 << 15)
CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_RESERVED16), // (1 << 16)
CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_RESERVED17), // (1 << 17)
CREATE_COMPILER_FLAG_INFO(D3DCOMPILE_WARNINGS_ARE_ERRORS) // (1 << 18)
};
# undef CREATE_COMPILER_FLAG_INFO
bool IsCompilerFlagSet(UINT mask, UINT flag)
{
bool isFlagSet = IsMaskFlagSet(mask, flag);
switch (flag)
{
case D3DCOMPILE_OPTIMIZATION_LEVEL0:
return isFlagSet && !IsMaskFlagSet(mask, UINT(D3DCOMPILE_OPTIMIZATION_LEVEL3));
case D3DCOMPILE_OPTIMIZATION_LEVEL1:
return (mask & D3DCOMPILE_OPTIMIZATION_LEVEL2) == UINT(0);
case D3DCOMPILE_OPTIMIZATION_LEVEL3:
return isFlagSet && !IsMaskFlagSet(mask, UINT(D3DCOMPILE_OPTIMIZATION_LEVEL0));
default:
return isFlagSet;
}
}
#endif // ANGLE_APPEND_ASSEMBLY_TO_SHADER_DEBUG_INFO
enum D3DCompilerLoadLibraryResult
{
D3DCompilerDefaultLibrarySuccess,
D3DCompilerFailure,
D3DCompilerEnumBoundary,
};
} // anonymous namespace
namespace rx
{
CompileConfig::CompileConfig() : flags(0), name() {}
CompileConfig::CompileConfig(UINT flags, const std::string &name) : flags(flags), name(name) {}
HLSLCompiler::HLSLCompiler()
: mInitialized(false),
mD3DCompilerModule(nullptr),
mD3DCompileFunc(nullptr),
mD3DDisassembleFunc(nullptr)
{}
HLSLCompiler::~HLSLCompiler()
{
release();
}
angle::Result HLSLCompiler::ensureInitialized(d3d::Context *context)
{
if (mInitialized)
{
return angle::Result::Continue;
}
ANGLE_TRACE_EVENT0("gpu.angle", "HLSLCompiler::initialize");
#if !defined(ANGLE_ENABLE_WINDOWS_UWP)
# if defined(ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES)
// Find a D3DCompiler module that had already been loaded based on a predefined list of
// versions.
static const char *d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES;
for (size_t i = 0; i < ArraySize(d3dCompilerNames); ++i)
{
if (GetModuleHandleExA(0, d3dCompilerNames[i], &mD3DCompilerModule))
{
break;
}
}
# endif // ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES
if (!mD3DCompilerModule)
{
// Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was
// built with.
mD3DCompilerModule = LoadLibraryA(D3DCOMPILER_DLL_A);
if (mD3DCompilerModule)
{
ANGLE_HISTOGRAM_ENUMERATION("GPU.ANGLE.D3DCompilerLoadLibraryResult",
D3DCompilerDefaultLibrarySuccess, D3DCompilerEnumBoundary);
}
}
if (!mD3DCompilerModule)
{
DWORD lastError = GetLastError();
ERR() << "D3D Compiler LoadLibrary failed. GetLastError=" << lastError;
ANGLE_HISTOGRAM_ENUMERATION("GPU.ANGLE.D3DCompilerLoadLibraryResult", D3DCompilerFailure,
D3DCompilerEnumBoundary);
ANGLE_TRY_HR(context, E_OUTOFMEMORY, "LoadLibrary failed to load D3D Compiler DLL.");
}
mD3DCompileFunc =
reinterpret_cast<pD3DCompile>(GetProcAddress(mD3DCompilerModule, "D3DCompile"));
ASSERT(mD3DCompileFunc);
mD3DDisassembleFunc =
reinterpret_cast<pD3DDisassemble>(GetProcAddress(mD3DCompilerModule, "D3DDisassemble"));
ASSERT(mD3DDisassembleFunc);
#else
// D3D Shader compiler is linked already into this module, so the export
// can be directly assigned.
mD3DCompilerModule = nullptr;
mD3DCompileFunc = reinterpret_cast<pD3DCompile>(D3DCompile);
mD3DDisassembleFunc = reinterpret_cast<pD3DDisassemble>(D3DDisassemble);
#endif
ANGLE_CHECK_HR(context, mD3DCompileFunc, "Error finding D3DCompile entry point.",
E_OUTOFMEMORY);
mInitialized = true;
return angle::Result::Continue;
}
void HLSLCompiler::release()
{
if (mInitialized)
{
FreeLibrary(mD3DCompilerModule);
mD3DCompilerModule = nullptr;
mD3DCompileFunc = nullptr;
mD3DDisassembleFunc = nullptr;
mInitialized = false;
}
}
angle::Result HLSLCompiler::compileToBinary(d3d::Context *context,
gl::InfoLog &infoLog,
const std::string &hlsl,
const std::string &profile,
const std::vector<CompileConfig> &configs,
const D3D_SHADER_MACRO *overrideMacros,
ID3DBlob **outCompiledBlob,
std::string *outDebugInfo)
{
ASSERT(mInitialized);
#if !defined(ANGLE_ENABLE_WINDOWS_UWP)
ASSERT(mD3DCompilerModule);
#endif
ASSERT(mD3DCompileFunc);
#if !defined(ANGLE_ENABLE_WINDOWS_UWP) && defined(ANGLE_ENABLE_DEBUG_TRACE)
std::string sourcePath = angle::CreateTemporaryFile().value();
std::ostringstream stream;
stream << "#line 2 \"" << sourcePath << "\"\n\n" << hlsl;
std::string sourceText = stream.str();
writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
#endif
auto *platform = ANGLEPlatformCurrent();
const D3D_SHADER_MACRO *macros = overrideMacros ? overrideMacros : nullptr;
for (size_t i = 0; i < configs.size(); ++i)
{
ID3DBlob *errorMessage = nullptr;
ID3DBlob *binary = nullptr;
HRESULT result = S_OK;
double startTime = platform->currentTime(platform);
{
ANGLE_TRACE_EVENT1("gpu.angle", "D3DCompile", "source", hlsl);
result = mD3DCompileFunc(hlsl.c_str(), hlsl.length(), gl::g_fakepath, macros, nullptr,
"main", profile.c_str(), configs[i].flags, 0, &binary,
&errorMessage);
}
double compileTime = platform->currentTime(platform) - startTime;
if (errorMessage)
{
std::string message = static_cast<const char *>(errorMessage->GetBufferPointer());
SafeRelease(errorMessage);
ANGLE_TRACE_EVENT1("gpu.angle", "D3DCompile::Error", "error", errorMessage);
infoLog.appendSanitized(message.c_str());
// This produces unbelievable amounts of spam in about:gpu.
// WARN() << std::endl << hlsl;
WARN() << std::endl << message;
if (macros != nullptr)
{
constexpr const char *kLoopRelatedErrors[] = {
// "can't unroll loops marked with loop attribute"
"error X3531:",
// "cannot have gradient operations inside loops with divergent flow control",
// even though it is counter-intuitive to disable unrolling for this error, some
// very long shaders have trouble deciding which loops to unroll and turning off
// forced unrolls allows them to compile properly.
"error X4014:",
// "array index out of bounds", loop unrolling can result in invalid array
// access if the indices become constant, causing loops that may never be
// executed to generate compilation errors
"error X3504:",
};
bool hasLoopRelatedError = false;
for (const char *errorType : kLoopRelatedErrors)
{
if (message.find(errorType) != std::string::npos)
{
hasLoopRelatedError = true;
break;
}
}
if (hasLoopRelatedError)
{
// Disable [loop] and [flatten]
macros = nullptr;
// Retry without changing compiler flags
i--;
continue;
}
}
}
if (SUCCEEDED(result))
{
int compileUs = static_cast<int>(compileTime * 1000'000.0);
ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.D3DShaderCompilationTimeUs", compileUs);
ANGLE_HISTOGRAM_MEMORY_KB("GPU.ANGLE.D3DShaderBlobSizeKB",
static_cast<int>(binary->GetBufferSize() / 1024));
*outCompiledBlob = binary;
(*outDebugInfo) +=
"// COMPILER INPUT HLSL BEGIN\n\n" + hlsl + "\n// COMPILER INPUT HLSL END\n";
#if ANGLE_APPEND_ASSEMBLY_TO_SHADER_DEBUG_INFO
(*outDebugInfo) += "\n\n// ASSEMBLY BEGIN\n\n";
(*outDebugInfo) += "// Compiler configuration: " + configs[i].name + "\n// Flags:\n";
for (size_t fIx = 0; fIx < ArraySize(CompilerFlagInfos); ++fIx)
{
if (IsCompilerFlagSet(configs[i].flags, CompilerFlagInfos[fIx].mFlag))
{
(*outDebugInfo) += std::string("// ") + CompilerFlagInfos[fIx].mName + "\n";
}
}
(*outDebugInfo) += "// Macros:\n";
if (macros == nullptr)
{
(*outDebugInfo) += "// - : -\n";
}
else
{
for (const D3D_SHADER_MACRO *mIt = macros; mIt->Name != nullptr; ++mIt)
{
(*outDebugInfo) +=
std::string("// ") + mIt->Name + " : " + mIt->Definition + "\n";
}
}
std::string disassembly;
ANGLE_TRY(disassembleBinary(context, binary, &disassembly));
(*outDebugInfo) += "\n" + disassembly + "\n// ASSEMBLY END\n";
#endif // ANGLE_APPEND_ASSEMBLY_TO_SHADER_DEBUG_INFO
return angle::Result::Continue;
}
if (result == E_OUTOFMEMORY)
{
*outCompiledBlob = nullptr;
ANGLE_TRY_HR(context, result, "HLSL compiler had an unexpected failure");
}
infoLog << "Warning: D3D shader compilation failed with " << configs[i].name << " flags. ("
<< profile << ")";
if (i + 1 < configs.size())
{
infoLog << " Retrying with " << configs[i + 1].name;
}
}
// None of the configurations succeeded in compiling this shader but the compiler is still
// intact
*outCompiledBlob = nullptr;
return angle::Result::Continue;
}
angle::Result HLSLCompiler::disassembleBinary(d3d::Context *context,
ID3DBlob *shaderBinary,
std::string *disassemblyOut)
{
ANGLE_TRY(ensureInitialized(context));
// Retrieve disassembly
UINT flags = D3D_DISASM_ENABLE_DEFAULT_VALUE_PRINTS | D3D_DISASM_ENABLE_INSTRUCTION_NUMBERING;
ID3DBlob *disassembly = nullptr;
pD3DDisassemble disassembleFunc = reinterpret_cast<pD3DDisassemble>(mD3DDisassembleFunc);
LPCVOID buffer = shaderBinary->GetBufferPointer();
SIZE_T bufSize = shaderBinary->GetBufferSize();
HRESULT result = disassembleFunc(buffer, bufSize, flags, "", &disassembly);
if (SUCCEEDED(result))
{
*disassemblyOut = std::string(static_cast<const char *>(disassembly->GetBufferPointer()));
}
else
{
*disassemblyOut = "";
}
SafeRelease(disassembly);
return angle::Result::Continue;
}
} // namespace rx