Hash :
6746e5dd
Author :
Date :
2021-06-01T22:48:14
Vulkan: SPIR-V Gen: Basic support for compute shaders This change enables direct SPIR-V generation for compute shaders and enables a few tests. A handful of built-in functions are translated to support said tests. Bug: angleproject:4889 Change-Id: I8fd6dc50ff31559a738ba680a993fb197e29fcf9 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2939330 Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Jamie Madill <jmadill@chromium.org> Reviewed-by: Tim Van Patten <timvp@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 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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
//
// Copyright 2021 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.
//
// BuildSPIRV: Helper for OutputSPIRV to build SPIR-V.
//
#ifndef COMPILER_TRANSLATOR_BUILDSPIRV_H_
#define COMPILER_TRANSLATOR_BUILDSPIRV_H_
#include "common/hash_utils.h"
#include "common/spirv/spirv_instruction_builder_autogen.h"
#include "compiler/translator/Compiler.h"
namespace spirv = angle::spirv;
namespace sh
{
// Helper classes to map types to ids
struct SpirvType
{
// If struct or interface block, the type is identified by the pointer. Note that both
// TStructure and TInterfaceBlock inherit from TFieldListCollection, and their difference is
// irrelevant as far as SPIR-V type is concerned.
const TFieldListCollection *block = nullptr;
// If a structure is used in two interface blocks with different layouts, it would have
// to generate two SPIR-V types, as its fields' Offset decorations could be different.
// For non-block types, when used in an interface block as an array, they could generate
// different ArrayStride decorations. As such, the block storage is part of the SPIR-V type
// except for non-block non-array types.
TLayoutBlockStorage blockStorage = EbsUnspecified;
// Otherwise, it's a basic type + column, row and array dimensions, or it's an image
// declaration.
//
// Notes:
//
// - `precision` turns into a RelaxedPrecision decoration on the variable and instructions.
// - `precise` turns into a NoContraction decoration on the instructions.
// - `readonly`, `writeonly`, `coherent`, `volatile` and `restrict` only apply to memory object
// declarations
// - `invariant` only applies to variable or members of a block
// - `matrixPacking` only applies to members of a struct
TBasicType type = EbtFloat;
uint8_t primarySize = 1;
uint8_t secondarySize = 1;
TLayoutMatrixPacking matrixPacking = EmpColumnMajor;
TSpan<const unsigned int> arraySizes;
// Only useful for image types.
TLayoutImageInternalFormat imageInternalFormat = EiifUnspecified;
// For sampled images (i.e. GLSL samplers), there are two type ids; one is the OpTypeImage that
// declares the image itself, and one OpTypeSampledImage. `imageOnly` distinguishes between
// these two types. Note that for the former, the basic type is still Ebt*Sampler* to
// distinguish it from storage images (which have a basic type of Ebt*Image*).
bool isSamplerBaseImage = false;
};
struct SpirvIdAndIdList
{
spirv::IdRef id;
spirv::IdRefList idList;
bool operator==(const SpirvIdAndIdList &other) const
{
return id == other.id && idList == other.idList;
}
};
struct SpirvIdAndStorageClass
{
spirv::IdRef id;
spv::StorageClass storageClass;
bool operator==(const SpirvIdAndStorageClass &other) const
{
return id == other.id && storageClass == other.storageClass;
}
};
struct SpirvTypeHash
{
size_t operator()(const sh::SpirvType &type) const
{
// Block storage must only affect the type if it's a block type or array type (in a block).
ASSERT(type.blockStorage == sh::EbsUnspecified || type.block != nullptr ||
!type.arraySizes.empty());
size_t result = 0;
if (!type.arraySizes.empty())
{
result = angle::ComputeGenericHash(type.arraySizes.data(),
type.arraySizes.size() * sizeof(type.arraySizes[0]));
}
if (type.block != nullptr)
{
return result ^ angle::ComputeGenericHash(&type.block, sizeof(type.block)) ^
type.blockStorage;
}
static_assert(sh::EbtLast < 256, "Basic type doesn't fit in uint8_t");
static_assert(sh::EbsLast < 8, "Block storage doesn't fit in 3 bits");
static_assert(sh::EiifLast < 32, "Image format doesn't fit in 5 bits");
static_assert(sh::EmpLast < 4, "Matrix packing doesn't fit in 2 bits");
ASSERT(type.primarySize > 0 && type.primarySize <= 4);
ASSERT(type.secondarySize > 0 && type.secondarySize <= 4);
const uint8_t properties[4] = {
static_cast<uint8_t>(type.type),
static_cast<uint8_t>((type.primarySize - 1) | (type.secondarySize - 1) << 2 |
type.isSamplerBaseImage << 4 | type.matrixPacking << 5),
static_cast<uint8_t>(type.blockStorage | type.imageInternalFormat << 3),
// Padding because ComputeGenericHash expects a key size divisible by 4
};
return result ^ angle::ComputeGenericHash(properties, sizeof(properties));
}
};
struct SpirvIdAndIdListHash
{
size_t operator()(const SpirvIdAndIdList &key) const
{
return angle::ComputeGenericHash(key.idList.data(),
key.idList.size() * sizeof(key.idList[0])) ^
key.id;
}
};
struct SpirvIdAndStorageClassHash
{
size_t operator()(const SpirvIdAndStorageClass &key) const
{
ASSERT(key.storageClass < 16);
return key.storageClass | key.id << 4;
}
};
// Data tracked per SPIR-V type (keyed by SpirvType).
struct SpirvTypeData
{
// The SPIR-V id corresponding to the type.
spirv::IdRef id;
// The base alignment and size of the type based on the storage block it's used in (if
// applicable).
uint32_t baseAlignment;
uint32_t sizeInStorageBlock;
// The matrix stride, if matrix or array of matrix.
uint32_t matrixStride;
};
// A block of code. SPIR-V produces forward references to blocks, such as OpBranchConditional
// specifying the id of the if and else blocks, each of those referencing the id of the block after
// the else. Additionally, local variable declarations are accumulated at the top of the first
// block in a function. For these reasons, each block of SPIR-V is generated separately and
// assembled at the end of the function, allowing prior blocks to be modified when necessary.
struct SpirvBlock
{
// Id of the block
spirv::IdRef labelId;
// Local variable declarations. Only the first block of a function is allowed to contain any
// instructions here.
spirv::Blob localVariables;
// Everything *after* OpLabel (which itself is not generated until blocks are assembled) and
// local variables.
spirv::Blob body;
// Whether the block is terminated. Useful for functions without return, asserting that code is
// not added after return/break/continue etc (i.e. dead code, which should really be removed
// earlier by a transformation, but could also be hacked by returning a bogus block to contain
// all the "garbage" to throw away), last switch case without a break, etc.
bool isTerminated = false;
};
// Conditional code, constituting ifs, switches and loops.
struct SpirvConditional
{
// The id of blocks that make up the conditional.
//
// - For if, there are three blocks: the then, else and merge blocks
// - For loops, there are four blocks: the condition, body, continue and merge blocks
// - For switch, there are a number of blocks based on the cases.
//
// In all cases, the merge block is the last block in this list. When the conditional is done
// with, that's the block that will be made "current" and future instructions written to. The
// merge block is also the branch target of "break" instructions.
//
// For loops, the continue target block is the one before last block in this list.
std::vector<spirv::IdRef> blockIds;
// Up to which block is already generated. Used by nextConditionalBlock() to generate a block
// and give it an id pre-determined in blockIds.
size_t nextBlockToWrite = 0;
// Used to determine if continue will affect this (i.e. it's a loop).
bool isContinuable = false;
// Used to determine if break will affect this (i.e. it's a loop or switch).
bool isBreakable = false;
};
// Helper class to construct SPIR-V
class SPIRVBuilder : angle::NonCopyable
{
public:
SPIRVBuilder(TCompiler *compiler,
ShCompileOptions compileOptions,
ShHashFunction64 hashFunction,
NameMap &nameMap)
: mCompiler(compiler),
mCompileOptions(compileOptions),
mShaderType(gl::FromGLenum<gl::ShaderType>(compiler->getShaderType())),
mNextAvailableId(1),
mHashFunction(hashFunction),
mNameMap(nameMap),
mNextUnusedBinding(0),
mNextUnusedInputLocation(0),
mNextUnusedOutputLocation(0)
{}
spirv::IdRef getNewId();
SpirvType getSpirvType(const TType &type, TLayoutBlockStorage blockStorage) const;
const SpirvTypeData &getTypeData(const TType &type, TLayoutBlockStorage blockStorage);
const SpirvTypeData &getSpirvTypeData(const SpirvType &type, const char *blockName);
spirv::IdRef getTypePointerId(spirv::IdRef typeId, spv::StorageClass storageClass);
spirv::IdRef getFunctionTypeId(spirv::IdRef returnTypeId, const spirv::IdRefList ¶mTypeIds);
spirv::Blob *getSpirvDebug() { return &mSpirvDebug; }
spirv::Blob *getSpirvDecorations() { return &mSpirvDecorations; }
spirv::Blob *getSpirvTypeAndConstantDecls() { return &mSpirvTypeAndConstantDecls; }
spirv::Blob *getSpirvTypePointerDecls() { return &mSpirvTypePointerDecls; }
spirv::Blob *getSpirvFunctionTypeDecls() { return &mSpirvFunctionTypeDecls; }
spirv::Blob *getSpirvVariableDecls() { return &mSpirvVariableDecls; }
spirv::Blob *getSpirvFunctions() { return &mSpirvFunctions; }
spirv::Blob *getSpirvCurrentFunctionBlock()
{
ASSERT(!mSpirvCurrentFunctionBlocks.empty() &&
!mSpirvCurrentFunctionBlocks.back().isTerminated);
return &mSpirvCurrentFunctionBlocks.back().body;
}
bool isCurrentFunctionBlockTerminated() const
{
ASSERT(!mSpirvCurrentFunctionBlocks.empty());
return mSpirvCurrentFunctionBlocks.back().isTerminated;
}
void terminateCurrentFunctionBlock()
{
ASSERT(!mSpirvCurrentFunctionBlocks.empty());
mSpirvCurrentFunctionBlocks.back().isTerminated = true;
}
SpirvConditional *getCurrentConditional() { return &mConditionalStack.back(); }
void addCapability(spv::Capability capability);
void setEntryPointId(spirv::IdRef id);
void addEntryPointInterfaceVariableId(spirv::IdRef id);
void writePerVertexBuiltIns(const TType &type, spirv::IdRef typeId);
void writeInterfaceVariableDecorations(const TType &type, spirv::IdRef variableId);
uint32_t calculateBaseAlignmentAndSize(const SpirvType &type,
uint32_t *sizeInStorageBlockOut,
uint32_t *matrixStrideOut);
uint32_t calculateSizeAndWriteOffsetDecorations(const SpirvType &type, spirv::IdRef typeId);
spirv::IdRef getBoolConstant(bool value);
spirv::IdRef getUintConstant(uint32_t value);
spirv::IdRef getIntConstant(int32_t value);
spirv::IdRef getFloatConstant(float value);
spirv::IdRef getCompositeConstant(spirv::IdRef typeId, const spirv::IdRefList &values);
// Helpers to start and end a function.
void startNewFunction(spirv::IdRef functionId, const char *name);
void assembleSpirvFunctionBlocks();
// Helper to declare a variable. Function-local variables must be placed in the first block of
// the current function.
spirv::IdRef declareVariable(spirv::IdRef typeId,
spv::StorageClass storageClass,
spirv::IdRef *initializerId,
const char *name);
// Helpers for conditionals.
void startConditional(size_t blockCount, bool isContinuable, bool isBreakable);
void nextConditionalBlock();
void endConditional();
// TODO: remove name hashing once translation through glslang is removed. That is necessary to
// avoid name collision between ANGLE's internal symbols and user-defined ones when compiling
// the generated GLSL, but is irrelevant when generating SPIR-V directly. Currently, the SPIR-V
// transformer relies on the "mapped" names, which should also be changed when this hashing is
// removed.
ImmutableString hashName(const TSymbol *symbol);
ImmutableString hashTypeName(const TType &type);
ImmutableString hashFieldName(const TField *field);
ImmutableString hashFunctionName(const TFunction *func);
spirv::Blob getSpirv();
private:
SpirvTypeData declareType(const SpirvType &type, const char *blockName);
// Helpers for type declaration.
void getImageTypeParameters(TBasicType type,
spirv::IdRef *sampledTypeOut,
spv::Dim *dimOut,
spirv::LiteralInteger *depthOut,
spirv::LiteralInteger *arrayedOut,
spirv::LiteralInteger *multisampledOut,
spirv::LiteralInteger *sampledOut);
spv::ImageFormat getImageFormat(TLayoutImageInternalFormat imageInternalFormat);
spirv::IdRef getBasicConstantHelper(uint32_t value,
TBasicType type,
angle::HashMap<uint32_t, spirv::IdRef> *constants);
uint32_t nextUnusedBinding();
uint32_t nextUnusedInputLocation(uint32_t consumedCount);
uint32_t nextUnusedOutputLocation(uint32_t consumedCount);
void generateExecutionModes(spirv::Blob *blob);
ANGLE_MAYBE_UNUSED TCompiler *mCompiler;
ANGLE_MAYBE_UNUSED ShCompileOptions mCompileOptions;
gl::ShaderType mShaderType;
// Capabilities the shader is using. Accumulated as the instructions are generated. The Shader
// capability is unconditionally generated, so it's not tracked.
std::set<spv::Capability> mCapabilities;
// The list of interface variables and the id of main() populated as the instructions are
// generated. Used for the OpEntryPoint instruction.
spirv::IdRefList mEntryPointInterfaceList;
spirv::IdRef mEntryPointId;
// Current ID bound, used to allocate new ids.
spirv::IdRef mNextAvailableId;
// A map from the AST type to the corresponding SPIR-V ID and associated data. Note that TType
// includes a lot of information that pertains to the variable that has the type, not the type
// itself. SpirvType instead contains only information that can identify the type itself.
angle::HashMap<SpirvType, SpirvTypeData, SpirvTypeHash> mTypeMap;
// Various sections of SPIR-V. Each section grows as SPIR-V is generated, and the final result
// is obtained by stitching the sections together. This puts the instructions in the order
// required by the spec.
spirv::Blob mSpirvDebug;
spirv::Blob mSpirvDecorations;
spirv::Blob mSpirvTypeAndConstantDecls;
spirv::Blob mSpirvTypePointerDecls;
spirv::Blob mSpirvFunctionTypeDecls;
spirv::Blob mSpirvVariableDecls;
spirv::Blob mSpirvFunctions;
// A list of blocks created for the current function. These are assembled by
// assembleSpirvFunctionBlocks() when the function is entirely visited. Local variables need to
// be inserted at the beginning of the first function block, so the entire SPIR-V of the
// function cannot be obtained until it's fully visited.
//
// The last block in this list is the one currently being written to.
std::vector<SpirvBlock> mSpirvCurrentFunctionBlocks;
// List of constants that are already defined (for reuse).
spirv::IdRef mBoolConstants[2];
angle::HashMap<uint32_t, spirv::IdRef> mUintConstants;
angle::HashMap<uint32_t, spirv::IdRef> mIntConstants;
angle::HashMap<uint32_t, spirv::IdRef> mFloatConstants;
angle::HashMap<SpirvIdAndIdList, spirv::IdRef, SpirvIdAndIdListHash> mCompositeConstants;
// TODO: Use null constants as optimization for when complex types are initialized with all
// zeros. http://anglebug.com/4889
// List of type pointers that are already defined.
// TODO: if all users call getTypeData(), move to SpirvTypeData. http://anglebug.com/4889
angle::HashMap<SpirvIdAndStorageClass, spirv::IdRef, SpirvIdAndStorageClassHash>
mTypePointerIdMap;
// List of function types that are already defined.
angle::HashMap<SpirvIdAndIdList, spirv::IdRef, SpirvIdAndIdListHash> mFunctionTypeIdMap;
// Stack of conditionals. When an if, loop or switch is visited, a new conditional scope is
// added. When the conditional construct is entirely visited, it's popped. As the blocks of
// the conditional constructs are visited, ids are consumed from the top of the stack. When
// break or continue is visited, the stack is traversed backwards until a loop or switch is
// found.
std::vector<SpirvConditional> mConditionalStack;
// name hashing.
ShHashFunction64 mHashFunction;
NameMap &mNameMap;
// Every resource that requires set & binding layout qualifiers is assigned set 0 and an
// arbitrary binding. Every input/output that requires a location layout qualifier is assigned
// an arbitrary location as well.
//
// The link-time SPIR-V transformer modifies set, binding and location decorations in SPIR-V
// directly.
uint32_t mNextUnusedBinding;
uint32_t mNextUnusedInputLocation;
uint32_t mNextUnusedOutputLocation;
};
} // namespace sh
#endif // COMPILER_TRANSLATOR_BUILDSPIRV_H_