1 /*
2  * Copyright © 2013 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 lower_offset_array.cpp
26  *
27  * IR lower pass to decompose ir_texture ir_tg4 with an array of offsets
28  * into four ir_tg4s with a single ivec2 offset, select the .w component of each,
29  * and return those four values packed into a gvec4.
30  *
31  * \author Chris Forbes <chrisf@ijw.co.nz>
32  */
33 
34 #include "compiler/glsl_types.h"
35 #include "ir.h"
36 #include "ir_builder.h"
37 #include "ir_optimization.h"
38 #include "ir_rvalue_visitor.h"
39 
40 using namespace ir_builder;
41 
42 class lower_offset_array_visitor : public ir_rvalue_visitor {
43 public:
lower_offset_array_visitor()44    lower_offset_array_visitor()
45    {
46       progress = false;
47    }
48 
49    void handle_rvalue(ir_rvalue **rv);
50 
51    bool progress;
52 };
53 
54 void
handle_rvalue(ir_rvalue ** rv)55 lower_offset_array_visitor::handle_rvalue(ir_rvalue **rv)
56 {
57    if (*rv == NULL || (*rv)->ir_type != ir_type_texture)
58       return;
59 
60    ir_texture *ir = (ir_texture *) *rv;
61    if (ir->op != ir_tg4 || !ir->offset || !ir->offset->type->is_array())
62       return;
63 
64    void *mem_ctx = ralloc_parent(ir);
65 
66    ir_variable *var =
67       new (mem_ctx) ir_variable(ir->type, "result", ir_var_temporary);
68    base_ir->insert_before(var);
69 
70    for (int i = 0; i < 4; i++) {
71       ir_texture *tex = ir->clone(mem_ctx, NULL);
72       tex->offset = new (mem_ctx) ir_dereference_array(tex->offset,
73             new (mem_ctx) ir_constant(i));
74 
75       base_ir->insert_before(assign(var, swizzle_w(tex), 1 << i));
76    }
77 
78    *rv = new (mem_ctx) ir_dereference_variable(var);
79 
80    progress = true;
81 }
82 
83 bool
lower_offset_arrays(exec_list * instructions)84 lower_offset_arrays(exec_list *instructions)
85 {
86    lower_offset_array_visitor v;
87 
88    visit_list_elements(&v, instructions);
89 
90    return v.progress;
91 }
92