i965: Normalize cubemap coordinates like is done in the Mesa IR path.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_cubemap_normalize.cpp
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
36 class brw_cubemap_normalize_visitor : public ir_hierarchical_visitor {
37 public:
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
48 ir_visitor_status
49 brw_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 void *mem_ctx = talloc_parent(ir);
55
56 ir_variable *var = new(mem_ctx) ir_variable(ir->coordinate->type,
57 "coordinate", ir_var_auto);
58 base_ir->insert_before(var);
59 ir_dereference *deref = new(mem_ctx) ir_dereference_variable(var);
60 ir_assignment *assign = new(mem_ctx) ir_assignment(deref, ir->coordinate,
61 NULL);
62 base_ir->insert_before(assign);
63
64 deref = new(mem_ctx) ir_dereference_variable(var);
65 ir_rvalue *swiz0 = new(mem_ctx) ir_swizzle(deref, 0, 0, 0, 0, 1);
66 deref = new(mem_ctx) ir_dereference_variable(var);
67 ir_rvalue *swiz1 = new(mem_ctx) ir_swizzle(deref, 1, 0, 0, 0, 1);
68 deref = new(mem_ctx) ir_dereference_variable(var);
69 ir_rvalue *swiz2 = new(mem_ctx) ir_swizzle(deref, 2, 0, 0, 0, 1);
70
71 swiz0 = new(mem_ctx) ir_expression(ir_unop_abs, swiz0->type, swiz0, NULL);
72 swiz1 = new(mem_ctx) ir_expression(ir_unop_abs, swiz1->type, swiz1, NULL);
73 swiz2 = new(mem_ctx) ir_expression(ir_unop_abs, swiz2->type, swiz2, NULL);
74
75 ir_expression *expr;
76 expr = new(mem_ctx) ir_expression(ir_binop_max,
77 glsl_type::float_type,
78 swiz0, swiz1);
79
80 expr = new(mem_ctx) ir_expression(ir_binop_max,
81 glsl_type::float_type,
82 expr, swiz2);
83
84 expr = new(mem_ctx) ir_expression(ir_unop_rcp,
85 glsl_type::float_type,
86 expr, NULL);
87
88 deref = new(mem_ctx) ir_dereference_variable(var);
89 ir->coordinate = new(mem_ctx) ir_expression(ir_binop_mul,
90 ir->coordinate->type,
91 deref,
92 expr);
93
94 progress = true;
95 return visit_continue;
96 }
97
98 extern "C" {
99
100 GLboolean
101 brw_do_cubemap_normalize(exec_list *instructions)
102 {
103 brw_cubemap_normalize_visitor v;
104
105 visit_list_elements(&v, instructions);
106
107 return v.progress;
108 }
109
110 }