X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fglsl%2Foptimize.h;h=33998e5e7408197b696203f1d6143e9f85f759cf;hp=92353a77ee1748c651d8d6f35ab266d6a429593c;hb=HEAD;hpb=c39bb707ff8678d0675538994dc05182e61da193 diff --git a/source/glsl/optimize.h b/source/glsl/optimize.h index 92353a77..807b7a89 100644 --- a/source/glsl/optimize.h +++ b/source/glsl/optimize.h @@ -14,11 +14,9 @@ constants. */ class ConstantSpecializer: private TraversingVisitor { private: - const std::map *values; + const std::map *values = 0; public: - ConstantSpecializer(); - void apply(Stage &, const std::map &); private: @@ -33,12 +31,10 @@ class InlineableFunctionLocator: private TraversingVisitor private: std::map refcounts; std::set inlineable; - FunctionDeclaration *current_function; - unsigned return_count; + FunctionDeclaration *current_function = 0; + unsigned return_count = 0; public: - InlineableFunctionLocator(); - std::set apply(Stage &s) { s.content.visit(*this); return inlineable; } private: @@ -62,22 +58,19 @@ private: RENAME }; - FunctionDeclaration *source_func; + FunctionDeclaration *source_func = 0; Block staging_block; - Pass pass; + Pass pass = REFERENCED; RefPtr r_inlined_statement; std::set dependencies; std::set referenced_names; std::string r_result_name; public: - InlineContentInjector(); - std::string apply(Stage &, FunctionDeclaration &, Block &, const NodeList::iterator &, FunctionCall &); private: virtual void visit(VariableReference &); - virtual void visit(InterfaceBlockReference &); virtual void visit(FunctionCall &); virtual void visit(VariableDeclaration &); virtual void visit(Return &); @@ -89,17 +82,15 @@ are inlined. */ class FunctionInliner: private TraversingVisitor { private: - Stage *stage; + Stage *stage = 0; std::set inlineable; - FunctionDeclaration *current_function; + FunctionDeclaration *current_function = 0; NodeList::iterator insert_point; RefPtr r_inline_result; - bool r_any_inlined; - bool r_inlined_here; + bool r_any_inlined = false; + bool r_inlined_here = false; public: - FunctionInliner(); - bool apply(Stage &); private: @@ -118,37 +109,32 @@ class ExpressionInliner: private TraversingVisitor private: struct ExpressionUse { - RefPtr *reference; - Block *ref_scope; - bool blocked; - - ExpressionUse(): reference(0), ref_scope(0), blocked(false) { } + RefPtr *reference = 0; + Block *ref_scope = 0; + bool blocked = false; }; struct ExpressionInfo { Assignment::Target target; RefPtr expression; - Block *assign_scope; + Block *assign_scope = 0; std::vector uses; - bool trivial; - - ExpressionInfo(): expression(0), assign_scope(0), trivial(false) { } + bool trivial = false; + bool blocked = false; }; std::list expressions; std::map assignments; - ExpressionInfo *r_ref_info; - bool r_trivial; - bool access_read; - bool access_write; - bool iteration_init; - Block *iteration_body; - const Operator *r_oper; + ExpressionInfo *r_ref_info = 0; + bool r_trivial = false; + bool access_read = true; + bool access_write = false; + bool iteration_init = false; + Block *iteration_body = 0; + const Operator *r_oper = 0; public: - ExpressionInliner(); - bool apply(Stage &); private: @@ -165,19 +151,65 @@ private: virtual void visit(Iteration &); }; +/** +Breaks aggregates up into separate variables if only the individual fields are +accessed and not the aggregate as a whole. +*/ +class AggregateDismantler: public TraversingVisitor +{ +private: + struct AggregateMember + { + const VariableDeclaration *declaration = 0; + unsigned index = 0; + RefPtr initializer; + std::vector *> references; + }; + + struct Aggregate + { + VariableDeclaration *declaration = 0; + Block *decl_scope = 0; + NodeList::iterator insert_point; + std::vector members; + bool referenced = false; + bool members_referenced = false; + }; + + NodeList::iterator insert_point; + std::map aggregates; + bool composite_reference = false; + Assignment::Target r_reference; + Aggregate *r_aggregate_ref = 0; + +public: + bool apply(Stage &); + +private: + virtual void visit(Block &); + virtual void visit(RefPtr &); + virtual void visit(VariableReference &); + void visit_composite(RefPtr &); + virtual void visit(MemberAccess &); + virtual void visit(BinaryExpression &); + virtual void visit(StructDeclaration &) { } + virtual void visit(VariableDeclaration &); + virtual void visit(FunctionDeclaration &); +}; + /** Replaces expressions consisting entirely of literals with the results of evaluating the expression.*/ class ConstantFolder: private TraversingVisitor { private: - VariableDeclaration *iteration_var; + VariableDeclaration *iteration_var = 0; Variant iter_init_value; Variant r_constant_value; - bool iteration_init; - bool r_constant; - bool r_literal; - bool r_uses_iter_var; - bool r_any_folded; + bool iteration_init = false; + bool r_constant = false; + bool r_literal = false; + bool r_uses_iter_var = false; + bool r_any_folded = false; public: bool apply(Stage &s) { s.content.visit(*this); return r_any_folded; } @@ -225,10 +257,10 @@ private: NodeList::iterator insert_point; std::set nodes_to_remove; RefPtr r_ternary_result; - bool r_external_side_effects; + bool r_external_side_effects = false; public: - void apply(Stage &); + bool apply(Stage &); private: ConstantStatus check_constant_condition(const Expression &); @@ -243,15 +275,14 @@ private: virtual void visit(Iteration &); }; +/** Removes code which is never executed due to flow control statements. */ class UnreachableCodeRemover: private TraversingVisitor { private: - bool reachable; + bool reachable = true; std::set unreachable_nodes; public: - UnreachableCodeRemover(); - virtual bool apply(Stage &); private: @@ -278,7 +309,6 @@ private: virtual void visit(ImageTypeDeclaration &); virtual void visit(StructDeclaration &); virtual void visit(VariableDeclaration &); - virtual void visit(InterfaceBlock &); virtual void visit(FunctionDeclaration &); }; @@ -289,50 +319,41 @@ class UnusedVariableRemover: private TraversingVisitor private: struct AssignmentInfo { - Node *node; + Node *node = 0; Assignment::Target target; std::vector used_by; - unsigned in_loop; - - AssignmentInfo(): node(0) { } + unsigned in_loop = 0; }; struct VariableInfo { - InterfaceBlock *interface_block; std::vector assignments; - bool initialized; - bool output; - bool referenced; - - VariableInfo(): interface_block(0), initialized(false), output(false), referenced(false) { } + bool initialized = false; + bool output = false; + bool referenced = false; }; - typedef std::map BlockVariableMap; + typedef std::map BlockVariableMap; - Stage *stage; + Stage *stage = 0; BlockVariableMap variables; std::list assignments; - InterfaceBlock *interface_block; - Assignment *r_assignment; - bool assignment_target; - bool r_side_effects; - bool in_struct; - bool composite_reference; - unsigned in_loop; + Assignment *r_assignment = 0; + bool assignment_target = false; + bool r_side_effects = false; + bool in_struct = false; + bool composite_reference = false; + unsigned in_loop = 0; std::vector loop_ext_refs; Assignment::Target r_reference; std::set unused_nodes; public: - UnusedVariableRemover(); - bool apply(Stage &); private: void referenced(const Assignment::Target &, Node &); virtual void visit(VariableReference &); - virtual void visit(InterfaceBlockReference &); void visit_composite(Expression &); virtual void visit(MemberAccess &); virtual void visit(Swizzle &); @@ -345,7 +366,6 @@ private: virtual void visit(ExpressionStatement &); virtual void visit(StructDeclaration &); virtual void visit(VariableDeclaration &); - virtual void visit(InterfaceBlock &); void merge_variables(const BlockVariableMap &); virtual void visit(FunctionDeclaration &); virtual void visit(Conditional &);