1/*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24/**
25 * \file brw_cubemap_normalize.cpp
26 *
27 * IR lower pass to perform the normalization of the cubemap coordinates to
28 * have the largest magnitude component be -1.0 or 1.0.
29 *
30 * \author Eric Anholt <eric@anholt.net>
31 */
32
33#include "glsl/glsl_types.h"
34#include "glsl/ir.h"
35
36class brw_cubemap_normalize_visitor : public ir_hierarchical_visitor {
37public:
38   brw_cubemap_normalize_visitor()
39   {
40      progress = false;
41   }
42
43   ir_visitor_status visit_leave(ir_texture *ir);
44
45   bool progress;
46};
47
48ir_visitor_status
49brw_cubemap_normalize_visitor::visit_leave(ir_texture *ir)
50{
51   if (ir->sampler->type->sampler_dimensionality != GLSL_SAMPLER_DIM_CUBE)
52      return visit_continue;
53
54   if (ir->op == ir_txs)
55      return visit_continue;
56
57   void *mem_ctx = ralloc_parent(ir);
58
59   ir_variable *var = new(mem_ctx) ir_variable(ir->coordinate->type,
60					       "coordinate", ir_var_auto);
61   base_ir->insert_before(var);
62   ir_dereference *deref = new(mem_ctx) ir_dereference_variable(var);
63   ir_assignment *assign = new(mem_ctx) ir_assignment(deref, ir->coordinate,
64						      NULL);
65   base_ir->insert_before(assign);
66
67   deref = new(mem_ctx) ir_dereference_variable(var);
68   ir_rvalue *swiz0 = new(mem_ctx) ir_swizzle(deref, 0, 0, 0, 0, 1);
69   deref = new(mem_ctx) ir_dereference_variable(var);
70   ir_rvalue *swiz1 = new(mem_ctx) ir_swizzle(deref, 1, 0, 0, 0, 1);
71   deref = new(mem_ctx) ir_dereference_variable(var);
72   ir_rvalue *swiz2 = new(mem_ctx) ir_swizzle(deref, 2, 0, 0, 0, 1);
73
74   swiz0 = new(mem_ctx) ir_expression(ir_unop_abs, swiz0->type, swiz0, NULL);
75   swiz1 = new(mem_ctx) ir_expression(ir_unop_abs, swiz1->type, swiz1, NULL);
76   swiz2 = new(mem_ctx) ir_expression(ir_unop_abs, swiz2->type, swiz2, NULL);
77
78   ir_expression *expr;
79   expr = new(mem_ctx) ir_expression(ir_binop_max,
80				     glsl_type::float_type,
81				     swiz0, swiz1);
82
83   expr = new(mem_ctx) ir_expression(ir_binop_max,
84				     glsl_type::float_type,
85				     expr, swiz2);
86
87   expr = new(mem_ctx) ir_expression(ir_unop_rcp,
88				     glsl_type::float_type,
89				     expr, NULL);
90
91   deref = new(mem_ctx) ir_dereference_variable(var);
92   ir->coordinate = new(mem_ctx) ir_expression(ir_binop_mul,
93					       ir->coordinate->type,
94					       deref,
95					       expr);
96
97   progress = true;
98   return visit_continue;
99}
100
101extern "C" {
102
103bool
104brw_do_cubemap_normalize(exec_list *instructions)
105{
106   brw_cubemap_normalize_visitor v;
107
108   visit_list_elements(&v, instructions);
109
110   return v.progress;
111}
112
113}
114