Hash :
9bc837f6
Author :
Date :
2021-04-07T15:24:12
Vulkan: Generate SPIR-V directly from the translator; Part 1 This is the first change in a series to generate SPIR-V directly from the translator's AST, instead of the generating text GLSL and feeding it to glslang. This change implements the majority of the work needed to map AST types to SPIR-V types, and declare types and interface variables in SPIR-V. Additionally, it lays the infrastructure to conditionally enabling this path in end2end tests. No tests are currently enabled as the change doesn't actually generate code for function bodies yet. Bug: angleproject:4889 Change-Id: Iacb28b6907fd48c50e4cc5a0e7ad72f6eed241d4 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2889603 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
//
// Copyright 2002 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.
//
// The ValidateVaryingLocations function checks if there exists location conflicts on shader
// varyings.
//
#include "ValidateVaryingLocations.h"
#include "compiler/translator/Diagnostics.h"
#include "compiler/translator/SymbolTable.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
#include "compiler/translator/util.h"
namespace sh
{
namespace
{
void error(const TIntermSymbol &symbol, const char *reason, TDiagnostics *diagnostics)
{
diagnostics->error(symbol.getLine(), reason, symbol.getName().data());
}
int GetStructLocationCount(const TStructure *structure);
int GetFieldLocationCount(const TField *field)
{
int field_size = 0;
const TType *fieldType = field->type();
if (fieldType->getStruct() != nullptr)
{
field_size = GetStructLocationCount(fieldType->getStruct());
}
else if (fieldType->isMatrix())
{
field_size = fieldType->getNominalSize();
}
else
{
ASSERT(fieldType->getSecondarySize() == 1);
field_size = 1;
}
if (fieldType->isArray())
{
field_size *= fieldType->getArraySizeProduct();
}
return field_size;
}
int GetStructLocationCount(const TStructure *structure)
{
int totalLocation = 0;
for (const TField *field : structure->fields())
{
totalLocation += GetFieldLocationCount(field);
}
return totalLocation;
}
int GetInterfaceBlockLocationCount(const TType &varyingType, bool ignoreVaryingArraySize)
{
int totalLocation = 0;
for (const TField *field : varyingType.getInterfaceBlock()->fields())
{
totalLocation += GetFieldLocationCount(field);
}
if (!ignoreVaryingArraySize && varyingType.isArray())
{
totalLocation *= varyingType.getArraySizeProduct();
}
return totalLocation;
}
int GetLocationCount(const TType &varyingType, bool ignoreVaryingArraySize)
{
ASSERT(!varyingType.isInterfaceBlock());
if (varyingType.getStruct() != nullptr)
{
int totalLocation = 0;
for (const TField *field : varyingType.getStruct()->fields())
{
const TType *fieldType = field->type();
ASSERT(fieldType->getStruct() == nullptr && !fieldType->isArray());
totalLocation += GetFieldLocationCount(field);
}
return totalLocation;
}
ASSERT(varyingType.isMatrix() || varyingType.getSecondarySize() == 1);
int elementLocationCount = varyingType.isMatrix() ? varyingType.getNominalSize() : 1;
// [GL_EXT_shader_io_blocks SPEC Chapter 4.4.1]
// Geometry shader inputs, tessellation control shader inputs and outputs, and tessellation
// evaluation inputs all have an additional level of arrayness relative to other shader inputs
// and outputs. This outer array level is removed from the type before considering how many
// locations the type consumes.
if (ignoreVaryingArraySize)
{
// Array-of-arrays cannot be inputs or outputs of a geometry shader.
// (GL_EXT_geometry_shader SPEC issues(5))
ASSERT(!varyingType.isArrayOfArrays());
return elementLocationCount;
}
return elementLocationCount * varyingType.getArraySizeProduct();
}
bool ShouldIgnoreVaryingArraySize(TQualifier qualifier, GLenum shaderType)
{
bool isVaryingIn = IsShaderIn(qualifier) && qualifier != EvqPatchIn;
switch (shaderType)
{
case GL_GEOMETRY_SHADER:
case GL_TESS_EVALUATION_SHADER:
return isVaryingIn;
case GL_TESS_CONTROL_SHADER:
return (IsShaderOut(qualifier) && qualifier != EvqPatchOut) || isVaryingIn;
default:
return false;
}
}
struct SymbolAndField
{
const TIntermSymbol *symbol;
const TField *field;
};
using LocationMap = std::map<int, SymbolAndField>;
void MarkVaryingLocations(TDiagnostics *diagnostics,
const TIntermSymbol *varying,
const TField *field,
int location,
int elementCount,
LocationMap *locationMap)
{
for (int elementIndex = 0; elementIndex < elementCount; ++elementIndex)
{
const int offsetLocation = location + elementIndex;
auto conflict = locationMap->find(offsetLocation);
if (conflict != locationMap->end())
{
std::stringstream strstr = sh::InitializeStream<std::stringstream>();
strstr << "'" << varying->getName();
if (field)
{
strstr << "." << field->name();
}
strstr << "' conflicting location with '" << conflict->second.symbol->getName();
if (conflict->second.field)
{
strstr << "." << conflict->second.field->name();
}
strstr << "'";
error(*varying, strstr.str().c_str(), diagnostics);
}
else
{
(*locationMap)[offsetLocation] = {varying, field};
}
}
}
using VaryingVector = std::vector<const TIntermSymbol *>;
void ValidateShaderInterfaceAndAssignLocations(TDiagnostics *diagnostics,
const VaryingVector &varyingVector,
GLenum shaderType)
{
// Location conflicts can only happen when there are two or more varyings in varyingVector.
if (varyingVector.size() <= 1)
{
return;
}
LocationMap locationMap;
for (const TIntermSymbol *varying : varyingVector)
{
const TType &varyingType = varying->getType();
const int location = varyingType.getLayoutQualifier().location;
ASSERT(location >= 0);
bool ignoreVaryingArraySize =
ShouldIgnoreVaryingArraySize(varying->getQualifier(), shaderType);
// A varying is either:
//
// - A vector or matrix, which can take a number of contiguous locations
// - A struct, which also takes a number of contiguous locations
// - An interface block.
//
// Interface blocks can assign arbitrary locations to their fields, for example:
//
// layout(location = 4) in block {
// vec4 a; // gets location 4
// vec4 b; // gets location 5
// layout(location = 7) vec4 c; // gets location 7
// vec4 d; // gets location 8
// layout (location = 1) vec4 e; // gets location 1
// vec4 f; // gets location 2
// };
//
// The following code therefore takes two paths. For non-interface-block types, the number
// of locations for the varying is calculated (elementCount), and all locations in
// [location, location + elementCount) are marked as occupied.
//
// For interface blocks, a similar algorithm is implemented except each field is
// individually marked with the location either advancing automatically or taking its value
// from the field's layout qualifier.
if (varyingType.isInterfaceBlock())
{
int currentLocation = location;
bool anyFieldWithLocation = false;
for (const TField *field : varyingType.getInterfaceBlock()->fields())
{
const int fieldLocation = field->type()->getLayoutQualifier().location;
if (fieldLocation >= 0)
{
currentLocation = fieldLocation;
anyFieldWithLocation = true;
}
const int fieldLocationCount = GetFieldLocationCount(field);
MarkVaryingLocations(diagnostics, varying, field, currentLocation,
fieldLocationCount, &locationMap);
currentLocation += fieldLocationCount;
}
// Array interface blocks can't have location qualifiers on fields.
ASSERT(ignoreVaryingArraySize || !anyFieldWithLocation || !varyingType.isArray());
if (!ignoreVaryingArraySize && varyingType.isArray())
{
// This is only reached if the varying is an array of interface blocks, with only a
// layout qualifier on the block itself, for example:
//
// layout(location = 4) in block {
// vec4 a;
// vec4 b;
// vec4 c;
// vec4 d;
// } instance[N];
//
// The locations for instance[0] are already marked by the above code, so we need to
// further mark locations occupied by instances [1, N). |currentLocation| is
// already just past the end of instance[0], which is the beginning of instance[1].
//
int remainingLocations = currentLocation * (varyingType.getArraySizeProduct() - 1);
MarkVaryingLocations(diagnostics, varying, nullptr, currentLocation,
remainingLocations, &locationMap);
}
}
else
{
const int elementCount = GetLocationCount(varying->getType(), ignoreVaryingArraySize);
MarkVaryingLocations(diagnostics, varying, nullptr, location, elementCount,
&locationMap);
}
}
}
class ValidateVaryingLocationsTraverser : public TIntermTraverser
{
public:
ValidateVaryingLocationsTraverser(GLenum shaderType);
void validate(TDiagnostics *diagnostics);
private:
bool visitDeclaration(Visit visit, TIntermDeclaration *node) override;
bool visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node) override;
VaryingVector mInputVaryingsWithLocation;
VaryingVector mOutputVaryingsWithLocation;
GLenum mShaderType;
};
ValidateVaryingLocationsTraverser::ValidateVaryingLocationsTraverser(GLenum shaderType)
: TIntermTraverser(true, false, false), mShaderType(shaderType)
{}
bool ValidateVaryingLocationsTraverser::visitDeclaration(Visit visit, TIntermDeclaration *node)
{
const TIntermSequence &sequence = *(node->getSequence());
ASSERT(!sequence.empty());
const TIntermSymbol *symbol = sequence.front()->getAsSymbolNode();
if (symbol == nullptr)
{
return false;
}
if (symbol->variable().symbolType() == SymbolType::Empty)
{
return false;
}
// Collect varyings that have explicit 'location' qualifiers.
const TQualifier qualifier = symbol->getQualifier();
if (symbol->getType().getLayoutQualifier().location != -1)
{
if (IsVaryingIn(qualifier))
{
mInputVaryingsWithLocation.push_back(symbol);
}
else if (IsVaryingOut(qualifier))
{
mOutputVaryingsWithLocation.push_back(symbol);
}
}
return false;
}
bool ValidateVaryingLocationsTraverser::visitFunctionDefinition(Visit visit,
TIntermFunctionDefinition *node)
{
// We stop traversing function definitions because varyings cannot be defined in a function.
return false;
}
void ValidateVaryingLocationsTraverser::validate(TDiagnostics *diagnostics)
{
ASSERT(diagnostics);
ValidateShaderInterfaceAndAssignLocations(diagnostics, mInputVaryingsWithLocation, mShaderType);
ValidateShaderInterfaceAndAssignLocations(diagnostics, mOutputVaryingsWithLocation,
mShaderType);
}
} // anonymous namespace
unsigned int CalculateVaryingLocationCount(const TType &varyingType, GLenum shaderType)
{
const TQualifier qualifier = varyingType.getQualifier();
const bool ignoreVaryingArraySize = ShouldIgnoreVaryingArraySize(qualifier, shaderType);
if (varyingType.isInterfaceBlock())
{
return GetInterfaceBlockLocationCount(varyingType, ignoreVaryingArraySize);
}
return GetLocationCount(varyingType, ignoreVaryingArraySize);
}
bool ValidateVaryingLocations(TIntermBlock *root, TDiagnostics *diagnostics, GLenum shaderType)
{
ValidateVaryingLocationsTraverser varyingValidator(shaderType);
root->traverse(&varyingValidator);
int numErrorsBefore = diagnostics->numErrors();
varyingValidator.validate(diagnostics);
return (diagnostics->numErrors() == numErrorsBefore);
}
} // namespace sh