[Mesa-dev] [PATCH 2/4] glsl: add new IR lower pass for sqrt(abs()) and inversesqrt(abs())

Samuel Pitoiset samuel.pitoiset at gmail.com
Fri Mar 17 00:06:55 UTC 2017


Looks easier to do that at lowering time and mostly because
builtin_builder is a singleton class without access to any
states/constants.

Signed-off-by: Samuel Pitoiset <samuel.pitoiset at gmail.com>
---
 src/compiler/Makefile.sources       |  1 +
 src/compiler/glsl/ir_optimization.h |  2 +
 src/compiler/glsl/lower_sqrt.cpp    | 77 +++++++++++++++++++++++++++++++++++++
 src/compiler/glsl/test_optpass.cpp  |  2 +
 4 files changed, 82 insertions(+)
 create mode 100644 src/compiler/glsl/lower_sqrt.cpp

diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources
index 2455d4eb5a..9cc2d9be7a 100644
--- a/src/compiler/Makefile.sources
+++ b/src/compiler/Makefile.sources
@@ -112,6 +112,7 @@ LIBGLSL_FILES = \
 	glsl/lower_output_reads.cpp \
 	glsl/lower_shared_reference.cpp \
 	glsl/lower_ubo_reference.cpp \
+	glsl/lower_sqrt.cpp \
 	glsl/opt_algebraic.cpp \
 	glsl/opt_array_splitting.cpp \
 	glsl/opt_conditional_discard.cpp \
diff --git a/src/compiler/glsl/ir_optimization.h b/src/compiler/glsl/ir_optimization.h
index 67a7514c7d..073956bba5 100644
--- a/src/compiler/glsl/ir_optimization.h
+++ b/src/compiler/glsl/ir_optimization.h
@@ -173,3 +173,5 @@ compare_index_block(exec_list *instructions, ir_variable *index,
 
 bool lower_64bit_integer_instructions(exec_list *instructions,
                                       unsigned what_to_lower);
+
+bool lower_sqrt(exec_list *instructions);
diff --git a/src/compiler/glsl/lower_sqrt.cpp b/src/compiler/glsl/lower_sqrt.cpp
new file mode 100644
index 0000000000..5e827f81bb
--- /dev/null
+++ b/src/compiler/glsl/lower_sqrt.cpp
@@ -0,0 +1,77 @@
+/*
+ * Copyright © 2010 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+/**
+ * \file lower_sqrt.cpp
+ * IR lower pass to compute absolute value before sqrt() and inversesqrt() in
+ * order to follow D3D9 behaviour for (buggy) apps that request it.
+ *
+ * \author Samuel Pitoiset <samuel.pitoiset at gmail.com>
+ */
+
+#include "ir.h"
+#include "ir_rvalue_visitor.h"
+#include "ir_builder.h"
+
+using namespace ir_builder;
+
+namespace {
+
+class lower_sqrt_visitor : public ir_rvalue_visitor {
+public:
+   lower_sqrt_visitor() : progress(false)
+   {
+      /* empty */
+   }
+
+   void handle_rvalue(ir_rvalue **rvalue)
+   {
+      if (!*rvalue)
+         return;
+
+      ir_expression *ir = (*rvalue)->as_expression();
+      if (!ir)
+         return;
+
+      if (ir->operation != ir_unop_rsq && ir->operation != ir_unop_rsq)
+         return;
+
+      *rvalue = expr(ir->operation, abs(ir->operands[0]));
+
+      this->progress = true;
+   }
+
+   bool progress;
+};
+
+}
+
+bool
+lower_sqrt(exec_list *instructions)
+{
+   lower_sqrt_visitor v;
+
+   visit_list_elements(&v, instructions);
+
+   return v.progress;
+}
diff --git a/src/compiler/glsl/test_optpass.cpp b/src/compiler/glsl/test_optpass.cpp
index c6e97888f6..ea145f95f8 100644
--- a/src/compiler/glsl/test_optpass.cpp
+++ b/src/compiler/glsl/test_optpass.cpp
@@ -121,6 +121,8 @@ do_optimization(struct exec_list *ir, const char *optimization,
       return lower_instructions(ir, int_0);
    } else if (strcmp(optimization, "lower_noise") == 0) {
       return lower_noise(ir);
+   } else if (strcmp(optimization, "lower_sqrt") == 0) {
+      return lower_sqrt(ir);
    } else if (sscanf(optimization, "lower_variable_index_to_cond_assign "
                      "( %d , %d , %d , %d ) ", &int_0, &int_1, &int_2,
                      &int_3) == 4) {
-- 
2.12.0



More information about the mesa-dev mailing list