summaryrefslogtreecommitdiff
path: root/src/compiler/glsl/lower_arithmetic_width.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/compiler/glsl/lower_arithmetic_width.cpp')
-rw-r--r--src/compiler/glsl/lower_arithmetic_width.cpp100
1 files changed, 100 insertions, 0 deletions
diff --git a/src/compiler/glsl/lower_arithmetic_width.cpp b/src/compiler/glsl/lower_arithmetic_width.cpp
new file mode 100644
index 00000000000..5d2028d0ba0
--- /dev/null
+++ b/src/compiler/glsl/lower_arithmetic_width.cpp
@@ -0,0 +1,100 @@
+/*
+ * Copyright © 2017 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.
+ */
+
+/**
+ * \name lower_arithmetic_width.cpp
+ *
+ * GLSL IR allows mixing of scalars and either vectors or matrices for some
+ * arithmetic operations. Vector architectures implement these operations by
+ * swizzling the scalar to a vector of the correct size. In addition, SPIR-V
+ * does not allow mixed scalar and vector operations for anything except
+ * multiplication.
+ *
+ * This pass replaces mixed scalar-vector operations with vector-vector
+ * operations by applying a swizzle to the scalar operand to create a vector
+ * of matching size.
+ *
+ * If the reduce_multiply option is specified, multiplications with a vector
+ * operand that is effectively a scalar will be converted to a scalar (e.g.,
+ * foo.yyyy becomes foo.y). If the operand is a scalar, the swizzle is
+ * removed (e.g., scalar_foo.xxx becomes scalar_foo).
+ */
+
+#include "ir.h"
+#include "ir_hierarchical_visitor.h"
+
+class arithmetic_width_visitor : public ir_hierarchical_visitor {
+public:
+ arithmetic_width_visitor()
+ {
+ /* empty */
+ }
+
+ ir_visitor_status visit_leave(ir_expression *);
+};
+
+ir_visitor_status
+arithmetic_width_visitor::visit_leave(ir_expression *ir)
+{
+ /* If there's only one operand, there's nothing to mix. */
+ if (ir->num_operands == 1)
+ return visit_continue;
+
+ /* All operations where GLSL IR allows mixing are operations that result in
+ * a vector.
+ */
+ if (!ir->type->is_vector())
+ return visit_continue;
+
+ if (ir->operation == ir_binop_mul &&
+ (ir->type->is_float() || ir->type->is_double())) {
+ return visit_continue;
+ }
+
+ const unsigned vector_size = ir->type->vector_elements;
+ const struct ir_swizzle_mask swiz_mask = { 0, 0, 0, 0, vector_size, true };
+ void *const mem_ctx = ralloc_parent(ir);
+
+ for (unsigned i = 0; i < ir->num_operands; ++i) {
+ ir_rvalue *const src = ir->operands[i];
+
+ assert(!src->type->is_vector() ||
+ vector_size == src->type->vector_elements);
+
+ if (src->type->is_scalar()) {
+ ir_swizzle *const swiz = new(mem_ctx) ir_swizzle(src, swiz_mask);
+
+ ir->operands[i] = swiz;
+ }
+ }
+
+ return visit_continue;
+}
+
+void
+lower_arithmetic_width(exec_list *instructions)
+{
+ arithmetic_width_visitor v;
+
+ v.run(instructions);
+}