X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fglsl%2Fgenerate.h;h=427f4de2360d977819d02ba9373b5beb7dfee7fe;hp=0c2a0ad8fce4b03aee235202f9a4eaebe0937d59;hb=041ba4b1acd55337239c5ce24cc310118c621206;hpb=0ca367ce305ae6f2025739b3b199a7f75d4132db diff --git a/source/glsl/generate.h b/source/glsl/generate.h index 0c2a0ad8..427f4de2 100644 --- a/source/glsl/generate.h +++ b/source/glsl/generate.h @@ -11,75 +11,197 @@ namespace Msp { namespace GL { namespace SL { -class DeclarationCombiner: private BlockModifier +/** Combines multiple declarations of the same identifier into one. */ +class DeclarationCombiner: private TraversingVisitor { private: - bool toplevel; std::map > functions; std::map variables; + std::set nodes_to_remove; public: - DeclarationCombiner(); + void apply(Stage &); + +private: + virtual void visit(Block &); + virtual void visit(VariableDeclaration &); + virtual void visit(FunctionDeclaration &) { } +}; + +/** Manipulates specialization constants. If values are specified, turns +specialization constants into normal constants. Without values assigns +automatic constant_ids to specialization constants. */ +class ConstantSpecializer: private TraversingVisitor +{ +private: + const std::map *values; + +public: + ConstantSpecializer(); + + void apply(Stage &, const std::map *); + +private: + virtual void visit(VariableDeclaration &); +}; + +/** Forms links between nested blocks in the syntax tree. */ +class BlockHierarchyResolver: private TraversingVisitor +{ +private: + bool r_any_resolved; + +public: + BlockHierarchyResolver(): r_any_resolved(false) { } - void apply(Stage &s) { visit(s.content); } + bool apply(Stage &s) { r_any_resolved = false; s.content.visit(*this); return r_any_resolved; } private: + virtual void enter(Block &); +}; + +/** Resolves types of variables and base types of other types. */ +class TypeResolver: private TraversingVisitor +{ +private: + Stage *stage; + std::map alias_map; + std::map array_types; + NodeList::iterator type_insert_point; + InterfaceBlock *iface_block; + bool r_any_resolved; + +public: + TypeResolver(); + + bool apply(Stage &); + +private: + TypeDeclaration *get_or_create_array_type(TypeDeclaration &); + void resolve_type(TypeDeclaration *&, const std::string &, bool); virtual void visit(Block &); - virtual void visit(FunctionDeclaration &); + virtual void visit(BasicTypeDeclaration &); + virtual void visit(ImageTypeDeclaration &); + virtual void visit(StructDeclaration &); virtual void visit(VariableDeclaration &); - using BlockModifier::visit; + virtual void visit(InterfaceBlock &); + virtual void visit(FunctionDeclaration &); }; +/** Resolves variable references. Variable references which match the name +of an interface block are turned into interface block references. */ class VariableResolver: private TraversingVisitor { private: - std::vector blocks; - StructDeclaration *type; - bool anonymous; - std::string block_interface; + Stage *stage; + RefPtr r_replacement_expr; + bool r_any_resolved; bool record_target; - VariableDeclaration *assignment_target; - bool self_referencing; + bool r_self_referencing; + VariableDeclaration *r_assignment_target; public: VariableResolver(); - void apply(Stage &); + bool apply(Stage &); private: - virtual void visit(Block &); + virtual void enter(Block &); + void visit_and_replace(RefPtr &); virtual void visit(VariableReference &); + virtual void visit(InterfaceBlockReference &); virtual void visit(MemberAccess &); + virtual void visit(UnaryExpression &); virtual void visit(BinaryExpression &); virtual void visit(Assignment &); - virtual void visit(StructDeclaration &); + virtual void visit(FunctionCall &); virtual void visit(VariableDeclaration &); virtual void visit(InterfaceBlock &); - using TraversingVisitor::visit; }; +/** Resolves types and lvalueness of expressions. */ +class ExpressionResolver: private TraversingVisitor +{ +private: + enum Compatibility + { + NOT_COMPATIBLE, + LEFT_CONVERTIBLE, + RIGHT_CONVERTIBLE, + SAME_TYPE + }; + + Stage *stage; + std::vector basic_types; + bool r_any_resolved; + +public: + ExpressionResolver(); + + bool apply(Stage &); + +private: + static bool is_scalar(BasicTypeDeclaration &); + static bool is_vector_or_matrix(BasicTypeDeclaration &); + static BasicTypeDeclaration *get_element_type(BasicTypeDeclaration &); + static bool can_convert(BasicTypeDeclaration &, BasicTypeDeclaration &); + static Compatibility get_compatibility(BasicTypeDeclaration &, BasicTypeDeclaration &); + BasicTypeDeclaration *find_type(BasicTypeDeclaration::Kind, unsigned); + BasicTypeDeclaration *find_type(BasicTypeDeclaration &, BasicTypeDeclaration::Kind, unsigned); + void convert_to(RefPtr &, BasicTypeDeclaration &); + bool convert_to_element(RefPtr &, BasicTypeDeclaration &); + void resolve(Expression &, TypeDeclaration *, bool); + + virtual void visit(Literal &); + virtual void visit(ParenthesizedExpression &); + virtual void visit(VariableReference &); + virtual void visit(InterfaceBlockReference &); + virtual void visit(MemberAccess &); + virtual void visit(UnaryExpression &); + void visit(BinaryExpression &, bool); + virtual void visit(BinaryExpression &); + virtual void visit(Assignment &); + virtual void visit(FunctionCall &); + virtual void visit(BasicTypeDeclaration &); + virtual void visit(VariableDeclaration &); +}; + +/** Resolves function declarations and calls. */ class FunctionResolver: private TraversingVisitor { private: - std::map > functions; + Stage *stage; + std::map > declarations; + bool r_any_resolved; public: - void apply(Stage &s) { visit(s.content); } + bool apply(Stage &); private: virtual void visit(FunctionCall &); virtual void visit(FunctionDeclaration &); - using TraversingVisitor::visit; }; -class InterfaceGenerator: private BlockModifier +/** Materializes implicitly declared interfaces. + +Out variable declarations inside functions are moved to the global scope. + +Passthrough statements are processed, generating out variables to match in +variables and copying values. + +Unresolved variables are looked up in the previous stage's out variables. */ +class InterfaceGenerator: private TraversingVisitor { private: Stage *stage; std::string in_prefix; std::string out_prefix; - unsigned scope_level; - std::map > iface_declarations; + bool function_scope; + bool copy_block; + Block *iface_target_block; + NodeList::iterator iface_insert_point; + NodeList::iterator assignment_insert_point; + std::set nodes_to_remove; public: InterfaceGenerator(); @@ -90,45 +212,14 @@ private: static std::string get_out_prefix(Stage::Type); std::string change_prefix(const std::string &, const std::string &) const; virtual void visit(Block &); - bool generate_interface(VariableDeclaration &, const std::string &, const std::string &); + VariableDeclaration *generate_interface(VariableDeclaration &, const std::string &, const std::string &); + InterfaceBlock *generate_interface(InterfaceBlock &); ExpressionStatement &insert_assignment(const std::string &, Expression *); virtual void visit(VariableReference &); virtual void visit(VariableDeclaration &); - virtual void visit(Passthrough &); - using BlockModifier::visit; -}; - -class DeclarationReorderer: private TraversingVisitor -{ -private: - enum DeclarationKind - { - NO_DECLARATION, - LAYOUT, - STRUCT, - VARIABLE, - FUNCTION - }; - - unsigned scope_level; - DeclarationKind kind; - std::set ordered_funcs; - std::set needed_funcs; - -public: - DeclarationReorderer(); - - void apply(Stage &s) { visit(s.content); } - -private: - virtual void visit(Block &); - virtual void visit(FunctionCall &); - virtual void visit(InterfaceLayout &) { kind = LAYOUT; } - virtual void visit(StructDeclaration &) { kind = STRUCT; } - virtual void visit(VariableDeclaration &); - virtual void visit(InterfaceBlock &) { kind = VARIABLE; } + virtual void visit(InterfaceBlock &); virtual void visit(FunctionDeclaration &); - using TraversingVisitor::visit; + virtual void visit(Passthrough &); }; } // namespace SL