From bc4034b243975089c06c4415d4e26edaaaec7a46 Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Thu, 5 Aug 2010 15:22:05 -0700 Subject: [PATCH] glsl2: Add a pass to convert exp and log to exp2 and log2. Fixes ir_to_mesa handling of unop_log, which used the weird ARB_vp LOG opcode that doesn't do what we want. This also lets the multiplication coefficients in there get constant-folded, possibly. Fixes: glsl-fs-log --- src/glsl/Makefile | 1 + src/glsl/ir.h | 4 +- src/glsl/ir_explog_to_explog2.cpp | 85 +++++++++++++++++++++++++++++++ src/glsl/ir_optimization.h | 1 + src/glsl/ir_validate.cpp | 4 ++ src/glsl/linker.cpp | 1 + src/mesa/program/ir_to_mesa.cpp | 7 +-- 7 files changed, 96 insertions(+), 7 deletions(-) create mode 100644 src/glsl/ir_explog_to_explog2.cpp diff --git a/src/glsl/Makefile b/src/glsl/Makefile index 53567508a01..752e60a79f6 100644 --- a/src/glsl/Makefile +++ b/src/glsl/Makefile @@ -41,6 +41,7 @@ CXX_SOURCES = \ ir_dead_code_local.cpp \ ir_dead_functions.cpp \ ir_div_to_mul_rcp.cpp \ + ir_explog_to_explog2.cpp \ ir_expression_flattening.cpp \ ir_function_can_inline.cpp \ ir_function.cpp \ diff --git a/src/glsl/ir.h b/src/glsl/ir.h index ef8339ce199..5dc3c6b9186 100644 --- a/src/glsl/ir.h +++ b/src/glsl/ir.h @@ -593,8 +593,8 @@ enum ir_expression_operation { ir_unop_rcp, ir_unop_rsq, ir_unop_sqrt, - ir_unop_exp, - ir_unop_log, + ir_unop_exp, /**< Log base e on gentype */ + ir_unop_log, /**< Natural log on gentype */ ir_unop_exp2, ir_unop_log2, ir_unop_f2i, /**< Float-to-integer conversion. */ diff --git a/src/glsl/ir_explog_to_explog2.cpp b/src/glsl/ir_explog_to_explog2.cpp new file mode 100644 index 00000000000..4fe1daaee91 --- /dev/null +++ b/src/glsl/ir_explog_to_explog2.cpp @@ -0,0 +1,85 @@ +/* + * 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 ir_explog_to_explog2.cpp + * + * Many GPUs don't have a base e log or exponent instruction, but they + * do have base 2 versions, so this pass converts exp and log to exp2 + * and log2 operations. + */ + +#include +#include "ir.h" +#include "glsl_types.h" + +class ir_explog_to_explog2_visitor : public ir_hierarchical_visitor { +public: + ir_explog_to_explog2_visitor() + { + this->progress = false; + } + + ir_visitor_status visit_leave(ir_expression *); + + bool progress; +}; + +bool +do_explog_to_explog2(exec_list *instructions) +{ + ir_explog_to_explog2_visitor v; + + visit_list_elements(&v, instructions); + return v.progress; +} + +ir_visitor_status +ir_explog_to_explog2_visitor::visit_leave(ir_expression *ir) +{ + if (ir->operation == ir_unop_exp) { + void *mem_ctx = talloc_parent(ir); + ir_constant *log2_e = new(mem_ctx) ir_constant(log2f(M_E)); + + ir->operation = ir_unop_exp2; + ir->operands[0] = new(mem_ctx) ir_expression(ir_binop_mul, + ir->operands[0]->type, + ir->operands[0], + log2_e); + this->progress = true; + } + + if (ir->operation == ir_unop_log) { + void *mem_ctx = talloc_parent(ir); + + ir->operation = ir_binop_mul; + ir->operands[0] = new(mem_ctx) ir_expression(ir_unop_log2, + ir->operands[0]->type, + ir->operands[0], + NULL); + ir->operands[1] = new(mem_ctx) ir_constant(1.0f / log2f(M_E)); + this->progress = true; + } + + return visit_continue; +} diff --git a/src/glsl/ir_optimization.h b/src/glsl/ir_optimization.h index eac28dc64cb..c6e7beb447f 100644 --- a/src/glsl/ir_optimization.h +++ b/src/glsl/ir_optimization.h @@ -38,6 +38,7 @@ bool do_dead_code_local(exec_list *instructions); bool do_dead_code_unlinked(exec_list *instructions); bool do_dead_functions(exec_list *instructions); bool do_div_to_mul_rcp(exec_list *instructions); +bool do_explog_to_explog2(exec_list *instructions); bool do_function_inlining(exec_list *instructions); bool do_if_return(exec_list *instructions); bool do_if_simplification(exec_list *instructions); diff --git a/src/glsl/ir_validate.cpp b/src/glsl/ir_validate.cpp index 701bf21ea61..545fe2799f3 100644 --- a/src/glsl/ir_validate.cpp +++ b/src/glsl/ir_validate.cpp @@ -183,10 +183,14 @@ ir_validate::visit_leave(ir_expression *ir) case ir_unop_rcp: case ir_unop_rsq: case ir_unop_sqrt: + assert(ir->type == ir->operands[0]->type); + break; + case ir_unop_exp: case ir_unop_log: case ir_unop_exp2: case ir_unop_log2: + assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT); assert(ir->type == ir->operands[0]->type); break; diff --git a/src/glsl/linker.cpp b/src/glsl/linker.cpp index 050116954a1..9d6de242f5f 100644 --- a/src/glsl/linker.cpp +++ b/src/glsl/linker.cpp @@ -1281,6 +1281,7 @@ link_shaders(struct gl_shader_program *prog) do_mat_op_to_vec(ir); do_mod_to_fract(ir); do_div_to_mul_rcp(ir); + do_explog_to_explog2(ir); do { progress = false; diff --git a/src/mesa/program/ir_to_mesa.cpp b/src/mesa/program/ir_to_mesa.cpp index 299b11d2741..26fbc4349ab 100644 --- a/src/mesa/program/ir_to_mesa.cpp +++ b/src/mesa/program/ir_to_mesa.cpp @@ -752,15 +752,12 @@ ir_to_mesa_visitor::visit(ir_expression *ir) ir_to_mesa_emit_scalar_op1(ir, OPCODE_RCP, result_dst, op[0]); break; - case ir_unop_exp: - ir_to_mesa_emit_scalar_op2(ir, OPCODE_POW, result_dst, - src_reg_for_float(M_E), op[0]); - break; case ir_unop_exp2: ir_to_mesa_emit_scalar_op1(ir, OPCODE_EX2, result_dst, op[0]); break; + case ir_unop_exp: case ir_unop_log: - ir_to_mesa_emit_scalar_op1(ir, OPCODE_LOG, result_dst, op[0]); + assert(!"not reached: should be handled by ir_explog_to_explog2"); break; case ir_unop_log2: ir_to_mesa_emit_scalar_op1(ir, OPCODE_LG2, result_dst, op[0]); -- 2.30.2