2 * Copyright © 2010 Intel Corporation
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:
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
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.
25 * \file brw_cubemap_normalize.cpp
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.
30 * \author Eric Anholt <eric@anholt.net>
33 #include "glsl/glsl_types.h"
35 #include "program/prog_instruction.h" /* For WRITEMASK_* */
37 class brw_cubemap_normalize_visitor : public ir_hierarchical_visitor {
39 brw_cubemap_normalize_visitor()
44 ir_visitor_status visit_leave(ir_texture *ir);
50 brw_cubemap_normalize_visitor::visit_leave(ir_texture *ir)
52 if (ir->sampler->type->sampler_dimensionality != GLSL_SAMPLER_DIM_CUBE)
53 return visit_continue;
56 return visit_continue;
58 void *mem_ctx = ralloc_parent(ir);
60 ir_variable *var = new(mem_ctx) ir_variable(ir->coordinate->type,
61 "coordinate", ir_var_auto);
62 base_ir->insert_before(var);
63 ir_dereference *deref = new(mem_ctx) ir_dereference_variable(var);
64 ir_assignment *assign = new(mem_ctx) ir_assignment(deref, ir->coordinate,
66 base_ir->insert_before(assign);
68 deref = new(mem_ctx) ir_dereference_variable(var);
69 ir_rvalue *swiz0 = new(mem_ctx) ir_swizzle(deref, 0, 0, 0, 0, 1);
70 deref = new(mem_ctx) ir_dereference_variable(var);
71 ir_rvalue *swiz1 = new(mem_ctx) ir_swizzle(deref, 1, 0, 0, 0, 1);
72 deref = new(mem_ctx) ir_dereference_variable(var);
73 ir_rvalue *swiz2 = new(mem_ctx) ir_swizzle(deref, 2, 0, 0, 0, 1);
75 swiz0 = new(mem_ctx) ir_expression(ir_unop_abs, swiz0->type, swiz0, NULL);
76 swiz1 = new(mem_ctx) ir_expression(ir_unop_abs, swiz1->type, swiz1, NULL);
77 swiz2 = new(mem_ctx) ir_expression(ir_unop_abs, swiz2->type, swiz2, NULL);
80 expr = new(mem_ctx) ir_expression(ir_binop_max,
81 glsl_type::float_type,
84 expr = new(mem_ctx) ir_expression(ir_binop_max,
85 glsl_type::float_type,
88 expr = new(mem_ctx) ir_expression(ir_unop_rcp,
89 glsl_type::float_type,
92 /* coordinate.xyz *= expr */
93 assign = new(mem_ctx) ir_assignment(
94 new(mem_ctx) ir_dereference_variable(var),
95 new(mem_ctx) ir_swizzle(
96 new(mem_ctx) ir_expression(ir_binop_mul,
98 new(mem_ctx) ir_dereference_variable(var),
101 assign->write_mask = WRITEMASK_XYZ;
102 base_ir->insert_before(assign);
103 ir->coordinate = new(mem_ctx) ir_dereference_variable(var);
106 return visit_continue;
112 brw_do_cubemap_normalize(exec_list *instructions)
114 brw_cubemap_normalize_visitor v;
116 visit_list_elements(&v, instructions);