Hash :
90a58622
Author :
Date :
2019-09-04T15:39:58
Refactor ShaderVariable to Remove Specializations The following structs are being refactored and moved into the parent struct ShaderVariable: VariableWithLocation Uniform Attribute OutputVariable InterfaceBlockField Varying Bug: angleproject:3899 Test: CQ Change-Id: I389eb3ab4ed44a360e09fca75ecc78d64a277f83 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/1785877 Reviewed-by: Jamie Madill <jmadill@chromium.org> Reviewed-by: Courtney Goeltzenleuchter <courtneygo@google.com> Commit-Queue: 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
//
// 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.
//
#ifndef COMPILER_TRANSLATOR_COMPILER_H_
#define COMPILER_TRANSLATOR_COMPILER_H_
//
// Machine independent part of the compiler private objects
// sent as ShHandle to the driver.
//
// This should not be included by driver code.
//
#include <GLSLANG/ShaderVars.h>
#include "compiler/translator/BuiltInFunctionEmulator.h"
#include "compiler/translator/CallDAG.h"
#include "compiler/translator/Diagnostics.h"
#include "compiler/translator/ExtensionBehavior.h"
#include "compiler/translator/HashNames.h"
#include "compiler/translator/InfoSink.h"
#include "compiler/translator/Pragma.h"
#include "compiler/translator/SymbolTable.h"
#include "compiler/translator/ValidateAST.h"
#include "third_party/compiler/ArrayBoundsClamper.h"
namespace sh
{
class TCompiler;
class TParseContext;
#ifdef ANGLE_ENABLE_HLSL
class TranslatorHLSL;
#endif // ANGLE_ENABLE_HLSL
//
// Helper function to check if the shader type is GLSL.
//
bool IsGLSL130OrNewer(ShShaderOutput output);
bool IsGLSL420OrNewer(ShShaderOutput output);
bool IsGLSL410OrOlder(ShShaderOutput output);
//
// Helper function to check if the invariant qualifier can be removed.
//
bool RemoveInvariant(sh::GLenum shaderType,
int shaderVersion,
ShShaderOutput outputType,
ShCompileOptions compileOptions);
//
// The base class used to back handles returned to the driver.
//
class TShHandleBase
{
public:
TShHandleBase();
virtual ~TShHandleBase();
virtual TCompiler *getAsCompiler() { return 0; }
#ifdef ANGLE_ENABLE_HLSL
virtual TranslatorHLSL *getAsTranslatorHLSL() { return 0; }
#endif // ANGLE_ENABLE_HLSL
protected:
// Memory allocator. Allocates and tracks memory required by the compiler.
// Deallocates all memory when compiler is destructed.
angle::PoolAllocator allocator;
};
//
// The base class for the machine dependent compiler to derive from
// for managing object code from the compile.
//
class TCompiler : public TShHandleBase
{
public:
TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
~TCompiler() override;
TCompiler *getAsCompiler() override { return this; }
bool Init(const ShBuiltInResources &resources);
// compileTreeForTesting should be used only when tests require access to
// the AST. Users of this function need to manually manage the global pool
// allocator. Returns nullptr whenever there are compilation errors.
TIntermBlock *compileTreeForTesting(const char *const shaderStrings[],
size_t numStrings,
ShCompileOptions compileOptions);
bool compile(const char *const shaderStrings[],
size_t numStrings,
ShCompileOptions compileOptions);
// Get results of the last compilation.
int getShaderVersion() const { return mShaderVersion; }
TInfoSink &getInfoSink() { return mInfoSink; }
bool isComputeShaderLocalSizeDeclared() const { return mComputeShaderLocalSizeDeclared; }
const sh::WorkGroupSize &getComputeShaderLocalSize() const { return mComputeShaderLocalSize; }
int getNumViews() const { return mNumViews; }
// Clears the results from the previous compilation.
void clearResults();
const std::vector<sh::ShaderVariable> &getAttributes() const { return mAttributes; }
const std::vector<sh::ShaderVariable> &getOutputVariables() const { return mOutputVariables; }
const std::vector<sh::ShaderVariable> &getUniforms() const { return mUniforms; }
const std::vector<sh::ShaderVariable> &getInputVaryings() const { return mInputVaryings; }
const std::vector<sh::ShaderVariable> &getOutputVaryings() const { return mOutputVaryings; }
const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return mInterfaceBlocks; }
const std::vector<sh::InterfaceBlock> &getUniformBlocks() const { return mUniformBlocks; }
const std::vector<sh::InterfaceBlock> &getShaderStorageBlocks() const
{
return mShaderStorageBlocks;
}
const std::vector<sh::InterfaceBlock> &getInBlocks() const { return mInBlocks; }
ShHashFunction64 getHashFunction() const { return mResources.HashFunction; }
NameMap &getNameMap() { return mNameMap; }
TSymbolTable &getSymbolTable() { return mSymbolTable; }
ShShaderSpec getShaderSpec() const { return mShaderSpec; }
ShShaderOutput getOutputType() const { return mOutputType; }
const std::string &getBuiltInResourcesString() const { return mBuiltInResourcesString; }
bool shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const;
// Get the resources set by InitBuiltInSymbolTable
const ShBuiltInResources &getResources() const;
int getGeometryShaderMaxVertices() const { return mGeometryShaderMaxVertices; }
int getGeometryShaderInvocations() const { return mGeometryShaderInvocations; }
TLayoutPrimitiveType getGeometryShaderInputPrimitiveType() const
{
return mGeometryShaderInputPrimitiveType;
}
TLayoutPrimitiveType getGeometryShaderOutputPrimitiveType() const
{
return mGeometryShaderOutputPrimitiveType;
}
sh::GLenum getShaderType() const { return mShaderType; }
bool validateAST(TIntermNode *root);
protected:
// Add emulated functions to the built-in function emulator.
virtual void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu,
ShCompileOptions compileOptions)
{}
// Translate to object code. May generate performance warnings through the diagnostics.
ANGLE_NO_DISCARD virtual bool translate(TIntermBlock *root,
ShCompileOptions compileOptions,
PerformanceDiagnostics *perfDiagnostics) = 0;
// Get built-in extensions with default behavior.
const TExtensionBehavior &getExtensionBehavior() const;
const char *getSourcePath() const;
const TPragma &getPragma() const { return mPragma; }
void writePragma(ShCompileOptions compileOptions);
// Relies on collectVariables having been called.
bool isVaryingDefined(const char *varyingName);
const ArrayBoundsClamper &getArrayBoundsClamper() const;
ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const;
const BuiltInFunctionEmulator &getBuiltInFunctionEmulator() const;
virtual bool shouldFlattenPragmaStdglInvariantAll() = 0;
virtual bool shouldCollectVariables(ShCompileOptions compileOptions);
bool wereVariablesCollected() const;
std::vector<sh::ShaderVariable> mAttributes;
std::vector<sh::ShaderVariable> mOutputVariables;
std::vector<sh::ShaderVariable> mUniforms;
std::vector<sh::ShaderVariable> mInputVaryings;
std::vector<sh::ShaderVariable> mOutputVaryings;
std::vector<sh::InterfaceBlock> mInterfaceBlocks;
std::vector<sh::InterfaceBlock> mUniformBlocks;
std::vector<sh::InterfaceBlock> mShaderStorageBlocks;
std::vector<sh::InterfaceBlock> mInBlocks;
private:
// Initialize symbol-table with built-in symbols.
bool initBuiltInSymbolTable(const ShBuiltInResources &resources);
// Compute the string representation of the built-in resources
void setResourceString();
// Return false if the call depth is exceeded.
bool checkCallDepth();
// Insert statements to reference all members in unused uniform blocks with standard and shared
// layout. This is to work around a Mac driver that treats unused standard/shared
// uniform blocks as inactive.
ANGLE_NO_DISCARD bool useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root);
// Insert statements to initialize output variables in the beginning of main().
// This is to avoid undefined behaviors.
ANGLE_NO_DISCARD bool initializeOutputVariables(TIntermBlock *root);
// Insert gl_Position = vec4(0,0,0,0) to the beginning of main().
// It is to work around a Linux driver bug where missing this causes compile failure
// while spec says it is allowed.
// This function should only be applied to vertex shaders.
ANGLE_NO_DISCARD bool initializeGLPosition(TIntermBlock *root);
// Return true if the maximum expression complexity is below the limit.
bool limitExpressionComplexity(TIntermBlock *root);
// Creates the function call DAG for further analysis, returning false if there is a recursion
bool initCallDag(TIntermNode *root);
// Return false if "main" doesn't exist
bool tagUsedFunctions();
void internalTagUsedFunction(size_t index);
void collectInterfaceBlocks();
bool mVariablesCollected;
bool mGLPositionInitialized;
// Removes unused function declarations and prototypes from the AST
class UnusedPredicate;
void pruneUnusedFunctions(TIntermBlock *root);
TIntermBlock *compileTreeImpl(const char *const shaderStrings[],
size_t numStrings,
const ShCompileOptions compileOptions);
// Fetches and stores shader metadata that is not stored within the AST itself, such as shader
// version.
void setASTMetadata(const TParseContext &parseContext);
// Check if shader version meets the requirement.
bool checkShaderVersion(TParseContext *parseContext);
// Does checks that need to be run after parsing is complete and returns true if they pass.
bool checkAndSimplifyAST(TIntermBlock *root,
const TParseContext &parseContext,
ShCompileOptions compileOptions);
sh::GLenum mShaderType;
ShShaderSpec mShaderSpec;
ShShaderOutput mOutputType;
struct FunctionMetadata
{
FunctionMetadata() : used(false) {}
bool used;
};
CallDAG mCallDag;
std::vector<FunctionMetadata> mFunctionMetadata;
ShBuiltInResources mResources;
std::string mBuiltInResourcesString;
// Built-in symbol table for the given language, spec, and resources.
// It is preserved from compile-to-compile.
TSymbolTable mSymbolTable;
// Built-in extensions with default behavior.
TExtensionBehavior mExtensionBehavior;
ArrayBoundsClamper mArrayBoundsClamper;
BuiltInFunctionEmulator mBuiltInFunctionEmulator;
// Results of compilation.
int mShaderVersion;
TInfoSink mInfoSink; // Output sink.
TDiagnostics mDiagnostics;
const char *mSourcePath; // Path of source file or NULL
// compute shader local group size
bool mComputeShaderLocalSizeDeclared;
sh::WorkGroupSize mComputeShaderLocalSize;
// GL_OVR_multiview num_views.
int mNumViews;
// geometry shader parameters.
int mGeometryShaderMaxVertices;
int mGeometryShaderInvocations;
TLayoutPrimitiveType mGeometryShaderInputPrimitiveType;
TLayoutPrimitiveType mGeometryShaderOutputPrimitiveType;
// name hashing.
NameMap mNameMap;
TPragma mPragma;
// Track what should be validated given passes currently applied.
ValidateASTOptions mValidateASTOptions;
ShCompileOptions mCompileOptions;
};
//
// This is the interface between the machine independent code
// and the machine dependent code.
//
// The machine dependent code should derive from the classes
// above. Then Construct*() and Delete*() will create and
// destroy the machine dependent objects, which contain the
// above machine independent information.
//
TCompiler *ConstructCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
void DeleteCompiler(TCompiler *);
void EmitWorkGroupSizeGLSL(const TCompiler &, TInfoSinkBase &sink);
void EmitMultiviewGLSL(const TCompiler &, const ShCompileOptions &, TBehavior, TInfoSinkBase &sink);
} // namespace sh
#endif // COMPILER_TRANSLATOR_COMPILER_H_