Path: blob/main_old/include/GLSLANG/ShaderVars.h
1693 views
//1// Copyright 2013 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//5// ShaderVars.h:6// Types to represent GL variables (varyings, uniforms, etc)7//89#ifndef GLSLANG_SHADERVARS_H_10#define GLSLANG_SHADERVARS_H_1112#include <algorithm>13#include <array>14#include <string>15#include <vector>1617// This type is defined here to simplify ANGLE's integration with glslang for SPIRv.18using ShCompileOptions = uint64_t;1920namespace sh21{22// GLenum alias23typedef unsigned int GLenum;2425// Varying interpolation qualifier, see section 4.3.9 of the ESSL 3.00.4 spec26enum InterpolationType27{28INTERPOLATION_SMOOTH,29INTERPOLATION_CENTROID,30INTERPOLATION_SAMPLE,31INTERPOLATION_FLAT,32INTERPOLATION_NOPERSPECTIVE33};3435const char *InterpolationTypeToString(InterpolationType type);3637// Validate link & SSO consistency of interpolation qualifiers38bool InterpolationTypesMatch(InterpolationType a, InterpolationType b);3940// Uniform block layout qualifier, see section 4.3.8.3 of the ESSL 3.00.4 spec41enum BlockLayoutType42{43BLOCKLAYOUT_STANDARD,44BLOCKLAYOUT_STD140 = BLOCKLAYOUT_STANDARD,45BLOCKLAYOUT_STD430, // Shader storage block layout qualifier46BLOCKLAYOUT_PACKED,47BLOCKLAYOUT_SHARED48};4950const char *BlockLayoutTypeToString(BlockLayoutType type);5152// Interface Blocks, see section 4.3.9 of the ESSL 3.10 spec53enum class BlockType54{55BLOCK_UNIFORM,56BLOCK_BUFFER,57};5859const char *BlockTypeToString(BlockType type);6061// Base class for all variables defined in shaders, including Varyings, Uniforms, etc62// Note: we must override the copy constructor and assignment operator so we can63// work around excessive GCC binary bloating:64// See https://code.google.com/p/angleproject/issues/detail?id=69765struct ShaderVariable66{67ShaderVariable();68ShaderVariable(GLenum typeIn);69ShaderVariable(GLenum typeIn, unsigned int arraySizeIn);70~ShaderVariable();71ShaderVariable(const ShaderVariable &other);72ShaderVariable &operator=(const ShaderVariable &other);73bool operator==(const ShaderVariable &other) const;74bool operator!=(const ShaderVariable &other) const { return !operator==(other); }7576bool isArrayOfArrays() const { return arraySizes.size() >= 2u; }77bool isArray() const { return !arraySizes.empty(); }78unsigned int getArraySizeProduct() const;79// Return the inner array size product.80// For example, if there's a variable declared as size 3 array of size 4 array of size 5 array81// of int:82// int a[3][4][5];83// then getInnerArraySizeProduct of a would be 4*5.84unsigned int getInnerArraySizeProduct() const;8586// Array size 0 means not an array when passed to or returned from these functions.87// Note that setArraySize() is deprecated and should not be used inside ANGLE.88unsigned int getOutermostArraySize() const { return isArray() ? arraySizes.back() : 0; }89void setArraySize(unsigned int size);9091// Turn this ShaderVariable from an array into a specific element in that array. Will update92// flattenedOffsetInParentArrays.93void indexIntoArray(unsigned int arrayIndex);9495// Get the nth nested array size from the top. Caller is responsible for range checking96// arrayNestingIndex.97unsigned int getNestedArraySize(unsigned int arrayNestingIndex) const;9899// This function should only be used with variables that are of a basic type or an array of a100// basic type. Shader interface variables that are enumerated according to rules in GLES 3.1101// spec section 7.3.1.1 page 77 are fine. For those variables the return value should match the102// ARRAY_SIZE value that can be queried through the API.103unsigned int getBasicTypeElementCount() const;104105unsigned int getExternalSize() const;106107bool isStruct() const { return !fields.empty(); }108const std::string &getStructName() const { return structOrBlockName; }109void setStructName(const std::string &newName) { structOrBlockName = newName; }110111// All of the shader's variables are described using nested data112// structures. This is needed in order to disambiguate similar looking113// types, such as two structs containing the same fields, but in114// different orders. "findInfoByMappedName" provides an easy query for115// users to dive into the data structure and fetch the unique variable116// instance corresponding to a dereferencing chain of the top-level117// variable.118// Given a mapped name like 'a[0].b.c[0]', return the ShaderVariable119// that defines 'c' in |leafVar|, and the original name 'A[0].B.C[0]'120// in |originalName|, based on the assumption that |this| defines 'a'.121// If no match is found, return false.122bool findInfoByMappedName(const std::string &mappedFullName,123const ShaderVariable **leafVar,124std::string *originalFullName) const;125126// Find the child field which matches 'fullName' == var.name + "." + field.name.127// Return nullptr if not found.128const sh::ShaderVariable *findField(const std::string &fullName, uint32_t *fieldIndexOut) const;129130bool isBuiltIn() const;131bool isEmulatedBuiltIn() const;132133// Offset of this variable in parent arrays. In case the parent is an array of arrays, the134// offset is outerArrayElement * innerArraySize + innerArrayElement.135// For example, if there's a variable declared as size 3 array of size 4 array of int:136// int a[3][4];137// then the flattenedOffsetInParentArrays of a[2] would be 2.138// and flattenedOffsetInParentArrays of a[2][1] would be 2*4 + 1 = 9.139int parentArrayIndex() const140{141return hasParentArrayIndex() ? flattenedOffsetInParentArrays : 0;142}143144int getFlattenedOffsetInParentArrays() const { return flattenedOffsetInParentArrays; }145void setParentArrayIndex(int indexIn) { flattenedOffsetInParentArrays = indexIn; }146147bool hasParentArrayIndex() const { return flattenedOffsetInParentArrays != -1; }148149void resetEffectiveLocation();150void updateEffectiveLocation(const sh::ShaderVariable &parent);151152// Decide whether two uniforms are the same at shader link time,153// assuming they are from consecutive shader stages.154// GLSL ES Spec 3.00.3, section 4.3.5.155// GLSL ES Spec 3.10.4, section 4.4.5156bool isSameUniformAtLinkTime(const ShaderVariable &other) const;157158// InterfaceBlockField159// Decide whether two InterfaceBlock fields are the same at shader160// link time, assuming they are from consecutive shader stages.161// See GLSL ES Spec 3.00.3, sec 4.3.7.162bool isSameInterfaceBlockFieldAtLinkTime(const ShaderVariable &other) const;163164// Decide whether two varyings are the same at shader link time,165// assuming they are from consecutive shader stages.166// Invariance needs to match only in ESSL1. Relevant spec sections:167// GLSL ES 3.00.4, sections 4.6.1 and 4.3.9.168// GLSL ES 1.00.17, section 4.6.4.169bool isSameVaryingAtLinkTime(const ShaderVariable &other, int shaderVersion) const;170// Deprecated version of isSameVaryingAtLinkTime, which assumes ESSL1.171bool isSameVaryingAtLinkTime(const ShaderVariable &other) const;172173// Shader I/O blocks may match by block name or instance, based on whether both stages have an174// instance name or not.175bool isSameNameAtLinkTime(const ShaderVariable &other) const;176177// NOTE: When adding new members, the following functions also need to be updated:178// gl::WriteShaderVar(BinaryOutputStream *stream, const sh::ShaderVariable &var)179// gl::LoadShaderVar(BinaryInputStream *stream, sh::ShaderVariable *var)180181GLenum type;182GLenum precision;183std::string name;184std::string mappedName;185186// Used to make an array type. Outermost array size is stored at the end of the vector.187std::vector<unsigned int> arraySizes;188189// Static use means that the variable is accessed somewhere in the shader source.190bool staticUse;191// A variable is active unless the compiler determined that it is not accessed by the shader.192// All active variables are statically used, but not all statically used variables are193// necessarily active. GLES 3.0.5 section 2.12.6. GLES 3.1 section 7.3.1.194bool active;195std::vector<ShaderVariable> fields;196// structOrBlockName is used for:197// - varyings of struct type, in which case it contains the struct name.198// - shader I/O blocks, in which case it contains the block name.199std::string structOrBlockName;200std::string mappedStructOrBlockName;201202// Only applies to interface block fields. Kept here for simplicity.203bool isRowMajorLayout;204205// VariableWithLocation206int location;207208// The location of inputs or outputs without location layout quailifer will be updated to '-1'.209// GLES Spec 3.1, Section 7.3. PROGRAM OBJECTS210// Not all active variables are assigned valid locations;211// the following variables will have an effective location of -1:212bool hasImplicitLocation;213214// Uniform215int binding;216GLenum imageUnitFormat;217int offset;218bool readonly;219bool writeonly;220221// From EXT_shader_framebuffer_fetch222bool isFragmentInOut;223224// OutputVariable225// From EXT_blend_func_extended.226int index;227228// From EXT_YUV_target229bool yuv;230231// Varying232InterpolationType interpolation;233bool isInvariant;234bool isShaderIOBlock;235bool isPatch;236237// If the variable is a sampler that has ever been statically used with texelFetch238bool texelFetchStaticUse;239240protected:241bool isSameVariableAtLinkTime(const ShaderVariable &other,242bool matchPrecision,243bool matchName) const;244245// NOTE: When adding new members, the following functions also need to be updated:246// gl::WriteShaderVar(BinaryOutputStream *stream, const sh::ShaderVariable &var)247// gl::LoadShaderVar(BinaryInputStream *stream, sh::ShaderVariable *var)248249int flattenedOffsetInParentArrays;250};251252// TODO: anglebug.com/3899253// For backwards compatibility for other codebases (e.g., chromium/src/gpu/command_buffer/service)254using Uniform = ShaderVariable;255using Attribute = ShaderVariable;256using OutputVariable = ShaderVariable;257using InterfaceBlockField = ShaderVariable;258using Varying = ShaderVariable;259260struct InterfaceBlock261{262InterfaceBlock();263~InterfaceBlock();264InterfaceBlock(const InterfaceBlock &other);265InterfaceBlock &operator=(const InterfaceBlock &other);266267// Fields from blocks with non-empty instance names are prefixed with the block name.268std::string fieldPrefix() const;269std::string fieldMappedPrefix() const;270271// Decide whether two interface blocks are the same at shader link time.272bool isSameInterfaceBlockAtLinkTime(const InterfaceBlock &other) const;273274bool isBuiltIn() const;275276bool isArray() const { return arraySize > 0; }277unsigned int elementCount() const { return std::max(1u, arraySize); }278279std::string name;280std::string mappedName;281std::string instanceName;282unsigned int arraySize;283BlockLayoutType layout;284285// Deprecated. Matrix packing should only be queried from individual fields of the block.286// TODO(oetuaho): Remove this once it is no longer used in Chromium.287bool isRowMajorLayout;288289int binding;290bool staticUse;291bool active;292BlockType blockType;293std::vector<ShaderVariable> fields;294};295296struct WorkGroupSize297{298// Must have a trivial default constructor since it is used in YYSTYPE.299inline WorkGroupSize() = default;300inline explicit constexpr WorkGroupSize(int initialSize);301302void fill(int fillValue);303void setLocalSize(int localSizeX, int localSizeY, int localSizeZ);304305int &operator[](size_t index);306int operator[](size_t index) const;307size_t size() const;308309// Checks whether two work group size declarations match.310// Two work group size declarations are the same if the explicitly specified elements are the311// same or if one of them is specified as one and the other one is not specified312bool isWorkGroupSizeMatching(const WorkGroupSize &right) const;313314// Checks whether any of the values are set.315bool isAnyValueSet() const;316317// Checks whether all of the values are set.318bool isDeclared() const;319320// Checks whether either all of the values are set, or none of them are.321bool isLocalSizeValid() const;322323int localSizeQualifiers[3];324};325326inline constexpr WorkGroupSize::WorkGroupSize(int initialSize)327: localSizeQualifiers{initialSize, initialSize, initialSize}328{}329330} // namespace sh331332#endif // GLSLANG_SHADERVARS_H_333334335