HLSL: Additional attribute support: [[]], namespace, parameters:

- support C++11 style brackets [[...]]
- support namespaces [[vk::...]]
- support these on parameter declarations in functions
- support location, binding/set, input attachments
This commit is contained in:
John Kessenich 2017-09-30 14:34:50 -06:00
parent 3693e631f5
commit 77ea30bdc9
8 changed files with 310 additions and 16 deletions

View file

@ -40,11 +40,27 @@
namespace glslang {
// Map the given string to an attribute enum from TAttributeType,
// or EatNone if invalid.
TAttributeType TAttributeMap::attributeFromName(const TString& name)
TAttributeType TAttributeMap::attributeFromName(const TString& nameSpace, const TString& name)
{
// These are case insensitive.
TString lowername(name);
std::transform(lowername.begin(), lowername.end(), lowername.begin(), ::tolower);
TString lowernameSpace(nameSpace);
std::transform(lowernameSpace.begin(), lowernameSpace.end(), lowernameSpace.begin(), ::tolower);
// handle names within a namespace
if (lowernameSpace == "vk") {
if (lowername == "input_attachment_index")
return EatInputAttachment;
else if (lowername == "location")
return EatLocation;
else if (lowername == "binding")
return EatBinding;
} else if (lowernameSpace.size() > 0)
return EatNone;
// handle names with no namespace
if (lowername == "allow_uav_condition")
return EatAllow_uav_condition;
@ -88,12 +104,12 @@ namespace glslang {
// Look up entry, inserting if it's not there, and if name is a valid attribute name
// as known by attributeFromName.
TAttributeType TAttributeMap::setAttribute(const TString* name, TIntermAggregate* value)
TAttributeType TAttributeMap::setAttribute(const TString& nameSpace, const TString* name, TIntermAggregate* value)
{
if (name == nullptr)
return EatNone;
const TAttributeType attr = attributeFromName(*name);
const TAttributeType attr = attributeFromName(nameSpace, *name);
if (attr != EatNone)
attributes[attr] = value;

View file

@ -63,6 +63,9 @@ namespace glslang {
EatPatchSize,
EatUnroll,
EatLoop,
EatBinding,
EatLocation,
EatInputAttachment
};
}
@ -82,7 +85,7 @@ namespace glslang {
public:
// Search for and potentially add the attribute into the map. Return the
// attribute type enum for it, if found, else EatNone.
TAttributeType setAttribute(const TString* name, TIntermAggregate* value);
TAttributeType setAttribute(const TString& nameSpace, const TString* name, TIntermAggregate* value);
// Const lookup: search for (but do not modify) the attribute in the map.
const TIntermAggregate* operator[](TAttributeType) const;
@ -92,7 +95,7 @@ namespace glslang {
protected:
// Find an attribute enum given its name.
static TAttributeType attributeFromName(const TString&);
static TAttributeType attributeFromName(const TString& nameSpace, const TString& name);
std::unordered_map<TAttributeType, TIntermAggregate*> attributes;
};

View file

@ -295,13 +295,16 @@ bool HlslGrammar::acceptSamplerDeclarationDX9(TType& /*type*/)
}
// declaration
// : attributes attributed_declaration
// | NAMESPACE IDENTIFIER LEFT_BRACE declaration_list RIGHT_BRACE
//
// attributed_declaration
// : sampler_declaration_dx9 post_decls SEMICOLON
// | fully_specified_type // for cbuffer/tbuffer
// | fully_specified_type declarator_list SEMICOLON // for non cbuffer/tbuffer
// | fully_specified_type identifier function_parameters post_decls compound_statement // function definition
// | fully_specified_type identifier sampler_state post_decls compound_statement // sampler definition
// | typedef declaration
// | NAMESPACE IDENTIFIER LEFT_BRACE declaration_list RIGHT_BRACE
//
// declarator_list
// : declarator COMMA declarator COMMA declarator... // zero or more declarators
@ -376,6 +379,8 @@ bool HlslGrammar::acceptDeclaration(TIntermNode*& nodeList)
if (! acceptFullySpecifiedType(declaredType, nodeList))
return false;
parseContext.transferTypeAttributes(declarator.attributes, declaredType);
// cbuffer and tbuffer end with the closing '}'.
// No semicolon is included.
if (forbidDeclarators)
@ -2371,16 +2376,25 @@ bool HlslGrammar::acceptDefaultParameterDeclaration(const TType& type, TIntermTy
}
// parameter_declaration
// : attributes attributed_declaration
//
// attributed_declaration
// : fully_specified_type post_decls [ = default_parameter_declaration ]
// | fully_specified_type identifier array_specifier post_decls [ = default_parameter_declaration ]
//
bool HlslGrammar::acceptParameterDeclaration(TFunction& function)
{
// attributes
TAttributeMap attributes;
acceptAttributes(attributes);
// fully_specified_type
TType* type = new TType;
if (! acceptFullySpecifiedType(*type))
return false;
parseContext.transferTypeAttributes(attributes, *type);
// identifier
HlslToken idToken;
acceptIdentifier(idToken);
@ -3256,7 +3270,15 @@ bool HlslGrammar::acceptStatement(TIntermNode*& statement)
}
// attributes
// : list of zero or more of: LEFT_BRACKET attribute RIGHT_BRACKET
// : [zero or more:] bracketed-attribute
//
// bracketed-attribute:
// : LEFT_BRACKET scoped-attribute RIGHT_BRACKET
// : LEFT_BRACKET LEFT_BRACKET scoped-attribute RIGHT_BRACKET RIGHT_BRACKET
//
// scoped-attribute:
// : attribute
// | namespace COLON COLON attribute
//
// attribute:
// : UNROLL
@ -3283,18 +3305,33 @@ void HlslGrammar::acceptAttributes(TAttributeMap& attributes)
// numthreads, which is used to set the CS local size.
// TODO: subset to correct set? Pass on?
do {
HlslToken idToken;
HlslToken attributeToken;
// LEFT_BRACKET?
if (! acceptTokenClass(EHTokLeftBracket))
return;
// another LEFT_BRACKET?
bool doubleBrackets = false;
if (acceptTokenClass(EHTokLeftBracket))
doubleBrackets = true;
// attribute
if (acceptIdentifier(idToken)) {
// 'idToken.string' is the attribute
} else if (! peekTokenClass(EHTokRightBracket)) {
expected("identifier");
advanceToken();
// attribute? (could be namespace; will adjust later)
if (!acceptIdentifier(attributeToken)) {
if (!peekTokenClass(EHTokRightBracket)) {
expected("namespace or attribute identifier");
advanceToken();
}
}
TString nameSpace;
if (acceptTokenClass(EHTokColonColon)) {
// namespace COLON COLON
nameSpace = *attributeToken.string;
// attribute
if (!acceptIdentifier(attributeToken)) {
expected("attribute identifier");
return;
}
}
TIntermAggregate* expressions = nullptr;
@ -3327,10 +3364,15 @@ void HlslGrammar::acceptAttributes(TAttributeMap& attributes)
expected("]");
return;
}
// another RIGHT_BRACKET?
if (doubleBrackets && !acceptTokenClass(EHTokRightBracket)) {
expected("]]");
return;
}
// Add any values we found into the attribute map. This accepts
// (and ignores) values not mapping to a known TAttributeType;
attributes.setAttribute(idToken.string, expressions);
attributes.setAttribute(nameSpace, attributeToken.string, expressions);
} while (true);
}

View file

@ -1666,7 +1666,6 @@ TIntermAggregate* HlslParseContext::handleFunctionDefinition(const TSourceLoc& l
return paramNodes;
}
// Handle all [attrib] attribute for the shader entry point
void HlslParseContext::handleEntryPointAttributes(const TSourceLoc& loc, const TAttributeMap& attributes)
{
@ -1815,6 +1814,44 @@ void HlslParseContext::handleEntryPointAttributes(const TSourceLoc& loc, const T
}
}
// Update the given type with any type-like attribute information in the
// attributes.
void HlslParseContext::transferTypeAttributes(const TAttributeMap& attributes, TType& type)
{
// extract integers out of attribute arguments stored in attribute aggregate
const auto getInt = [&](TAttributeType attr, int argNum, int& value) -> bool {
const TIntermAggregate* attrAgg = attributes[attr];
if (attrAgg == nullptr)
return false;
if (argNum >= attrAgg->getSequence().size())
return false;
const TConstUnion& intConst = attrAgg->getSequence()[argNum]->getAsConstantUnion()->getConstArray()[0];
if (intConst == nullptr)
return false;
value = intConst.getIConst();
return true;
};
// location
int value;
if (getInt(EatLocation, 0, value))
type.getQualifier().layoutLocation = value;
// binding
if (getInt(EatBinding, 0, value)) {
type.getQualifier().layoutBinding = value;
type.getQualifier().layoutSet = 0;
}
// set
if (getInt(EatBinding, 1, value))
type.getQualifier().layoutSet = value;
// input attachment
if (getInt(EatInputAttachment, 0, value))
type.getQualifier().layoutAttachment = value;
}
//
// Do all special handling for the entry point, including wrapping
// the shader's entry point with the official entry point that will call it.

View file

@ -83,6 +83,7 @@ public:
TIntermAggregate* handleFunctionDefinition(const TSourceLoc&, TFunction&, const TAttributeMap&, TIntermNode*& entryPointTree);
TIntermNode* transformEntryPoint(const TSourceLoc&, TFunction&, const TAttributeMap&);
void handleEntryPointAttributes(const TSourceLoc&, const TAttributeMap&);
void transferTypeAttributes(const TAttributeMap&, TType&);
void handleFunctionBody(const TSourceLoc&, TFunction&, TIntermNode* functionBody, TIntermNode*& node);
void remapEntryPointIO(TFunction& function, TVariable*& returnValue, TVector<TVariable*>& inputs, TVector<TVariable*>& outputs);
void remapNonEntryPointIO(TFunction& function);