]> git.tdb.fi Git - libs/gl.git/commitdiff
Don't add depth range conversion if an assignment to gl_Position.z exists
authorMikko Rasa <tdb@tdb.fi>
Sat, 9 Apr 2022 16:01:25 +0000 (19:01 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 9 Apr 2022 16:05:05 +0000 (19:05 +0300)
This is kinda crude since it triggers from any such assignment, but it
allows the idempotence test cases to work.

source/glsl/finalize.cpp
source/glsl/finalize.h

index fcf48c61cd0ae1fbce706cff7744384525be7d7c..85a6b07adf8f00591348ed63d744299e8567ab39 100644 (file)
@@ -251,9 +251,42 @@ void DepthRangeConverter::apply(Stage &stage, const Features &features)
        stage.content.visit(*this);
 }
 
+void DepthRangeConverter::visit(VariableReference &var)
+{
+       const StructDeclaration *strct = dynamic_cast<const StructDeclaration *>(var.type);
+       r_gl_pervertex = (strct && strct->block_name=="gl_PerVertex");
+}
+
+void DepthRangeConverter::visit(MemberAccess &memacc)
+{
+       r_gl_pervertex = false;
+       memacc.left->visit(*this);
+       r_gl_position = (r_gl_pervertex && memacc.member=="gl_Position");
+}
+
+void DepthRangeConverter::visit(Swizzle &swiz)
+{
+       r_gl_position = false;
+       swiz.left->visit(*this);
+       if(assignment_target && r_gl_position && swiz.count==1 && swiz.components[0]==2)
+               r_position_z_assigned = true;
+}
+
+void DepthRangeConverter::visit(Assignment &assign)
+{
+       {
+               SetFlag set_target(assignment_target);
+               assign.left->visit(*this);
+       }
+       assign.right->visit(*this);
+}
+
 void DepthRangeConverter::visit(FunctionDeclaration &func)
 {
-       if(func.definition==&func && func.name=="main")
+       r_position_z_assigned = false;
+       TraversingVisitor::visit(func);
+
+       if(func.definition==&func && func.name=="main" && !r_position_z_assigned)
        {
                VariableReference *position = new VariableReference;
                position->name = "gl_Position";
index 6e96c65eaecb46c0b7d04aaae3027cb063d7bbdf..ec5f459ae53b59f1db6371dedcc2928e6d75cd36 100644 (file)
@@ -61,10 +61,20 @@ Converts the output depth range to match expectations of the target API.
 */
 class DepthRangeConverter: private TraversingVisitor
 {
+private:
+       bool assignment_target = false;
+       bool r_gl_pervertex = false;
+       bool r_gl_position = false;
+       bool r_position_z_assigned = false;
+
 public:
        void apply(Stage &, const Features &);
 
 private:
+       virtual void visit(VariableReference &);
+       virtual void visit(MemberAccess &);
+       virtual void visit(Swizzle &);
+       virtual void visit(Assignment &);
        virtual void visit(FunctionDeclaration &);
 };