Hash :
c229ccfe
Author :
Date :
2021-05-18T15:51:12
Vulkan: SPIR-V Gen: Handle gl_PerVertex If not declared by the shader, default gl_PerVertex is now explicitly declared in AST prior to SPIR-V generation. The SPIR-V generation code then doesn't need to declare them magically. Bug: angleproject:4889 Change-Id: Icc593bc1ccc3162487bdbae7f66bc775d098778d Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2905952 Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Tim Van Patten <timvp@google.com> Reviewed-by: Jamie Madill <jmadill@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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
//
// 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.
//
// OutputSPIRV: Generate SPIR-V from the AST.
//
#include "compiler/translator/OutputSPIRV.h"
#include "angle_gl.h"
#include "common/debug.h"
#include "common/mathutil.h"
#include "common/spirv/spirv_instruction_builder_autogen.h"
#include "compiler/translator/BuildSPIRV.h"
#include "compiler/translator/Compiler.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
#include <cfloat>
// SPIR-V tools include for disassembly
#include <spirv-tools/libspirv.hpp>
// Enable this for debug logging of pre-transform SPIR-V:
#if !defined(ANGLE_DEBUG_SPIRV_TRANSFORMER)
# define ANGLE_DEBUG_SPIRV_TRANSFORMER 0
#endif // !defined(ANGLE_DEBUG_SPIRV_TRANSFORMER)
namespace sh
{
namespace
{
class OutputSPIRVTraverser : public TIntermTraverser
{
public:
OutputSPIRVTraverser(TCompiler *compiler, ShCompileOptions compileOptions);
spirv::Blob getSpirv();
protected:
void visitSymbol(TIntermSymbol *node) override;
void visitConstantUnion(TIntermConstantUnion *node) override;
bool visitSwizzle(Visit visit, TIntermSwizzle *node) override;
bool visitBinary(Visit visit, TIntermBinary *node) override;
bool visitUnary(Visit visit, TIntermUnary *node) override;
bool visitTernary(Visit visit, TIntermTernary *node) override;
bool visitIfElse(Visit visit, TIntermIfElse *node) override;
bool visitSwitch(Visit visit, TIntermSwitch *node) override;
bool visitCase(Visit visit, TIntermCase *node) override;
void visitFunctionPrototype(TIntermFunctionPrototype *node) override;
bool visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node) override;
bool visitAggregate(Visit visit, TIntermAggregate *node) override;
bool visitBlock(Visit visit, TIntermBlock *node) override;
bool visitGlobalQualifierDeclaration(Visit visit,
TIntermGlobalQualifierDeclaration *node) override;
bool visitDeclaration(Visit visit, TIntermDeclaration *node) override;
bool visitLoop(Visit visit, TIntermLoop *node) override;
bool visitBranch(Visit visit, TIntermBranch *node) override;
void visitPreprocessorDirective(TIntermPreprocessorDirective *node) override;
private:
ANGLE_MAYBE_UNUSED TCompiler *mCompiler;
ANGLE_MAYBE_UNUSED ShCompileOptions mCompileOptions;
SPIRVBuilder mBuilder;
};
spv::StorageClass GetStorageClass(const TType &type)
{
// Opaque uniforms (samplers and images) have the UniformConstant storage class
if (type.isSampler() || type.isImage())
{
return spv::StorageClassUniformConstant;
}
// Input varying and IO blocks have the Input storage class
if (IsShaderIn(type.getQualifier()))
{
return spv::StorageClassInput;
}
// Output varying and IO blocks have the Input storage class
if (IsShaderOut(type.getQualifier()))
{
return spv::StorageClassOutput;
}
// Uniform and storage buffers have the Uniform storage class
if (type.isInterfaceBlock())
{
// I/O blocks must have already been classified as input or output above.
ASSERT(!IsShaderIoBlock(type.getQualifier()));
return spv::StorageClassUniform;
}
// Compute shader shared memory has the Workgroup storage class
if (type.getQualifier() == EvqShared)
{
return spv::StorageClassWorkgroup;
}
// All other variables are either Private or Function, based on whether they are global or
// function-local.
if (type.getQualifier() == EvqGlobal)
{
return spv::StorageClassPrivate;
}
ASSERT(type.getQualifier() == EvqTemporary);
return spv::StorageClassFunction;
}
OutputSPIRVTraverser::OutputSPIRVTraverser(TCompiler *compiler, ShCompileOptions compileOptions)
: TIntermTraverser(true, true, true, &compiler->getSymbolTable()),
mCompiler(compiler),
mCompileOptions(compileOptions),
mBuilder(gl::FromGLenum<gl::ShaderType>(compiler->getShaderType()),
compiler->getHashFunction(),
compiler->getNameMap())
{}
void OutputSPIRVTraverser::visitSymbol(TIntermSymbol *node)
{
// TODO: http://anglebug.com/4889
UNIMPLEMENTED();
}
void OutputSPIRVTraverser::visitConstantUnion(TIntermConstantUnion *node)
{
// TODO: http://anglebug.com/4889
UNIMPLEMENTED();
}
bool OutputSPIRVTraverser::visitSwizzle(Visit visit, TIntermSwizzle *node)
{
// TODO: http://anglebug.com/4889
UNIMPLEMENTED();
return true;
}
bool OutputSPIRVTraverser::visitBinary(Visit visit, TIntermBinary *node)
{
// TODO: http://anglebug.com/4889
UNIMPLEMENTED();
return true;
}
bool OutputSPIRVTraverser::visitUnary(Visit visit, TIntermUnary *node)
{
// TODO: http://anglebug.com/4889
UNIMPLEMENTED();
return true;
}
bool OutputSPIRVTraverser::visitTernary(Visit visit, TIntermTernary *node)
{
// TODO: http://anglebug.com/4889
UNIMPLEMENTED();
return true;
}
bool OutputSPIRVTraverser::visitIfElse(Visit visit, TIntermIfElse *node)
{
// TODO: http://anglebug.com/4889
UNIMPLEMENTED();
return true;
}
bool OutputSPIRVTraverser::visitSwitch(Visit visit, TIntermSwitch *node)
{
// TODO: http://anglebug.com/4889
UNIMPLEMENTED();
return true;
}
bool OutputSPIRVTraverser::visitCase(Visit visit, TIntermCase *node)
{
// TODO: http://anglebug.com/4889
UNIMPLEMENTED();
return false;
}
bool OutputSPIRVTraverser::visitBlock(Visit visit, TIntermBlock *node)
{
// If global block, nothing to generate.
if (getCurrentTraversalDepth() == 0)
{
return true;
}
if (visit == PreVisit)
{
const spirv::IdRef blockLabelId = mBuilder.getNewId();
spirv::WriteLabel(mBuilder.getSpirvFunctions(), blockLabelId);
}
// TODO: http://anglebug.com/4889
UNIMPLEMENTED();
return false;
}
bool OutputSPIRVTraverser::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
{
if (visit == PreVisit)
{
const TFunction *function = node->getFunction();
// Declare the function type
const spirv::IdRef returnTypeId =
mBuilder.getTypeData(function->getReturnType(), EbsUnspecified).id;
spirv::IdRefList paramTypeIds;
for (size_t paramIndex = 0; paramIndex < function->getParamCount(); ++paramIndex)
{
paramTypeIds.push_back(
mBuilder.getTypeData(function->getParam(paramIndex)->getType(), EbsUnspecified).id);
}
const spirv::IdRef functionTypeId = mBuilder.getFunctionTypeId(returnTypeId, paramTypeIds);
// Declare the function itself
const spirv::IdRef functionId = mBuilder.getNewId();
spirv::WriteFunction(mBuilder.getSpirvFunctions(), returnTypeId, functionId,
spv::FunctionControlMaskNone, functionTypeId);
for (size_t paramIndex = 0; paramIndex < function->getParamCount(); ++paramIndex)
{
const spirv::IdRef paramId = mBuilder.getNewId();
spirv::WriteFunctionParameter(mBuilder.getSpirvFunctions(), paramTypeIds[paramIndex],
paramId);
// TODO: Add to TVariable to variableId map so references to this variable can discover
// the ID. http://anglebug.com/4889
}
// Remember the ID of main() for the sake of OpEntryPoint.
if (function->isMain())
{
mBuilder.setEntryPointId(functionId);
}
return true;
}
if (visit == PostVisit)
{
// TODO: if the function returns void, the AST may not have an explicit OpReturn node, so
// generate one at the end if not already. For testing, unconditionally add it.
// http://anglebug.com/4889
if (node->getFunction()->getReturnType().getBasicType() == EbtVoid)
{
spirv::WriteReturn(mBuilder.getSpirvFunctions());
}
// End the function
spirv::WriteFunctionEnd(mBuilder.getSpirvFunctions());
}
return true;
}
bool OutputSPIRVTraverser::visitGlobalQualifierDeclaration(Visit visit,
TIntermGlobalQualifierDeclaration *node)
{
// TODO: http://anglebug.com/4889
UNIMPLEMENTED();
return true;
}
void OutputSPIRVTraverser::visitFunctionPrototype(TIntermFunctionPrototype *node)
{
// Nothing to do. The function type is declared together with its definition.
}
bool OutputSPIRVTraverser::visitAggregate(Visit visit, TIntermAggregate *node)
{
// TODO: http://anglebug.com/4889
UNIMPLEMENTED();
return false;
}
bool OutputSPIRVTraverser::visitDeclaration(Visit visit, TIntermDeclaration *node)
{
if (visit != PreVisit)
{
return true;
}
const TIntermSequence &sequence = *node->getSequence();
// Enforced by ValidateASTOptions::validateMultiDeclarations.
ASSERT(sequence.size() == 1);
TIntermTyped *declVariable = sequence.front()->getAsTyped();
const TType &type = declVariable->getType();
TIntermSymbol *symbol = declVariable->getAsSymbolNode();
ASSERT(symbol != nullptr);
// If this is just a struct declaration (and not a variable declaration), don't declare the
// struct up-front and let it be lazily defined. If the struct is only used inside an interface
// block for example, this avoids it being doubly defined (once with the unspecified block
// storage and once with interface block's).
if (type.isStructSpecifier() && symbol->variable().symbolType() == SymbolType::Empty)
{
return false;
}
const spirv::IdRef typeId = mBuilder.getTypeData(type, EbsUnspecified).id;
// TODO: handle constant declarations. http://anglebug.com/4889
spv::StorageClass storageClass = GetStorageClass(type);
const spirv::IdRef typePointerId = mBuilder.getTypePointerId(typeId, storageClass);
spirv::Blob *spirvSection = storageClass == spv::StorageClassFunction
? mBuilder.getSpirvFunctions()
: mBuilder.getSpirvVariableDecls();
const spirv::IdRef variableId = mBuilder.getNewId();
// TODO: handle initializers. http://anglebug.com/4889
spirv::WriteVariable(spirvSection, typePointerId, variableId, storageClass, nullptr);
// TODO: create a TVariable to variableId map so references to this variable can discover the
// ID. http://anglebug.com/4889
if (IsShaderIn(type.getQualifier()) || IsShaderOut(type.getQualifier()))
{
// Add in and out variables to the list of interface variables.
mBuilder.addEntryPointInterfaceVariableId(variableId);
if (IsShaderIoBlock(type.getQualifier()) && type.isInterfaceBlock())
{
// For gl_PerVertex in particular, write the necessary BuiltIn decorations
if (type.getQualifier() == EvqPerVertexIn || type.getQualifier() == EvqPerVertexOut)
{
mBuilder.writePerVertexBuiltIns(type, typeId);
}
// I/O blocks are decorated with Block
spirv::WriteDecorate(mBuilder.getSpirvDecorations(), typeId, spv::DecorationBlock, {});
}
}
else if (type.getBasicType() == EbtInterfaceBlock)
{
// For uniform and buffer variables, add Block and BufferBlock decorations respectively.
const spv::Decoration decoration =
type.getQualifier() == EvqUniform ? spv::DecorationBlock : spv::DecorationBufferBlock;
spirv::WriteDecorate(mBuilder.getSpirvDecorations(), typeId, decoration, {});
}
// Write DescriptorSet, Binding, Location etc decorations if necessary.
mBuilder.writeInterfaceVariableDecorations(type, variableId);
// Output debug information.
spirv::WriteName(mBuilder.getSpirvDebug(), variableId,
mBuilder.hashName(&symbol->variable()).data());
return false;
}
bool OutputSPIRVTraverser::visitLoop(Visit visit, TIntermLoop *node)
{
// TODO: http://anglebug.com/4889
UNIMPLEMENTED();
return true;
}
bool OutputSPIRVTraverser::visitBranch(Visit visit, TIntermBranch *node)
{
// TODO: http://anglebug.com/4889
UNIMPLEMENTED();
return true;
}
void OutputSPIRVTraverser::visitPreprocessorDirective(TIntermPreprocessorDirective *node)
{
// No preprocessor directives expected at this point.
UNREACHABLE();
}
spirv::Blob OutputSPIRVTraverser::getSpirv()
{
spirv::Blob result = mBuilder.getSpirv();
// Validate that correct SPIR-V was generated
ASSERT(spirv::Validate(result));
#if ANGLE_DEBUG_SPIRV_TRANSFORMER
// Disassemble and log the generated SPIR-V for debugging.
spvtools::SpirvTools spirvTools(SPV_ENV_VULKAN_1_1);
std::string readableSpirv;
spirvTools.Disassemble(result, &readableSpirv, 0);
fprintf(stderr, "%s\n", readableSpirv.c_str());
#endif // ANGLE_DEBUG_SPIRV_TRANSFORMER
return result;
}
} // anonymous namespace
bool OutputSPIRV(TCompiler *compiler, TIntermBlock *root, ShCompileOptions compileOptions)
{
// Traverse the tree and generate SPIR-V instructions
OutputSPIRVTraverser traverser(compiler, compileOptions);
root->traverse(&traverser);
// Generate the final SPIR-V and store in the sink
spirv::Blob spirvBlob = traverser.getSpirv();
compiler->getInfoSink().obj.setBinary(std::move(spirvBlob));
return true;
}
} // namespace sh