From 5f6ee612cb83029de9559bae644c9a3b4e579259 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 5 Mar 2021 13:28:22 +0200 Subject: [PATCH] Follow type aliases in TypeResolver --- source/glsl/generate.cpp | 29 +++++++++++++++++++---------- source/glsl/generate.h | 2 ++ tests/glsl/alias_types.glsl | 20 ++++++++++++++++++++ 3 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 tests/glsl/alias_types.glsl diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index 433d506e..f5b6b7ad 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -125,10 +125,21 @@ void TypeResolver::apply(Stage &s) s.content.visit(*this); } +TypeDeclaration *TypeResolver::resolve_type(const string &name) +{ + map::iterator i = stage->types.find(name); + if(i!=stage->types.end()) + { + map::iterator j = alias_map.find(i->second); + return (j!=alias_map.end() ? j->second : i->second); + } + else + return 0; +} + void TypeResolver::visit(BasicTypeDeclaration &type) { - map::iterator i = stage->types.find(type.base); - type.base_type = (i!=stage->types.end() ? i->second : 0); + type.base_type = resolve_type(type.base); if(type.kind==BasicTypeDeclaration::VECTOR && type.base_type) if(BasicTypeDeclaration *basic_base = dynamic_cast(type.base_type)) @@ -138,14 +149,15 @@ void TypeResolver::visit(BasicTypeDeclaration &type) type.size |= basic_base->size<<16; } + if(type.kind==BasicTypeDeclaration::ALIAS && type.base_type) + alias_map[&type] = type.base_type; + stage->types.insert(make_pair(type.name, &type)); } void TypeResolver::visit(ImageTypeDeclaration &type) { - map::iterator i = stage->types.find(type.base); - type.base_type = (i!=stage->types.end() ? i->second : 0); - + type.base_type = resolve_type(type.base); stage->types.insert(make_pair(type.name, &type)); } @@ -157,15 +169,12 @@ void TypeResolver::visit(StructDeclaration &strct) void TypeResolver::visit(VariableDeclaration &var) { - map::iterator i = stage->types.find(var.type); - if(i!=stage->types.end()) - var.type_declaration = i->second; + var.type_declaration = resolve_type(var.type); } void TypeResolver::visit(FunctionDeclaration &func) { - map::iterator i = stage->types.find(func.return_type); - func.return_type_declaration = (i!=stage->types.end() ? i->second : 0); + func.return_type_declaration = resolve_type(func.return_type); TraversingVisitor::visit(func); } diff --git a/source/glsl/generate.h b/source/glsl/generate.h index d4e570ab..b660a095 100644 --- a/source/glsl/generate.h +++ b/source/glsl/generate.h @@ -60,6 +60,7 @@ class TypeResolver: private TraversingVisitor { private: Stage *stage; + std::map alias_map; public: TypeResolver(); @@ -67,6 +68,7 @@ public: void apply(Stage &); private: + TypeDeclaration *resolve_type(const std::string &); virtual void visit(BasicTypeDeclaration &); virtual void visit(ImageTypeDeclaration &); virtual void visit(StructDeclaration &); diff --git a/tests/glsl/alias_types.glsl b/tests/glsl/alias_types.glsl new file mode 100644 index 00000000..8e6e9a91 --- /dev/null +++ b/tests/glsl/alias_types.glsl @@ -0,0 +1,20 @@ +typedef vec4 myvec4; +typedef mat4x4 mymat4; + +uniform mymat4 mvp; + +#pragma MSP stage(vertex) +layout(location=0) in myvec4 position; +void main() +{ + gl_Position = mvp*position; +} + +/* Expected output: vertex +uniform mat4 mvp; +layout(location=0) in vec4 position; +void main() +{ + gl_Position = mvp*position; +} +*/ -- 2.43.0