i965: fold offset into coord for textureOffset(gsampler2DRect)
[mesa.git] / src / mesa / drivers / dri / i965 / brw_lower_unnormalized_offset.cpp
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 brw_lower_unnormalized_offset.cpp
26 *
27 * IR lower pass to convert a texture offset into an adjusted coordinate,
28 * for use with unnormalized coordinates. At least the gather4* messages
29 * on Ivybridge and Haswell make a mess with nonzero offsets.
30 *
31 * \author Chris Forbes <chrisf@ijw.co.nz>
32 */
33
34 #include "glsl/glsl_types.h"
35 #include "glsl/ir.h"
36 #include "glsl/ir_builder.h"
37
38 using namespace ir_builder;
39
40 class brw_lower_unnormalized_offset_visitor : public ir_hierarchical_visitor {
41 public:
42 brw_lower_unnormalized_offset_visitor()
43 {
44 progress = false;
45 }
46
47 ir_visitor_status visit_leave(ir_texture *ir);
48
49 bool progress;
50 };
51
52 ir_visitor_status
53 brw_lower_unnormalized_offset_visitor::visit_leave(ir_texture *ir)
54 {
55 if (!ir->offset)
56 return visit_continue;
57
58 if (ir->op == ir_tg4 || ir->op == ir_tex) {
59 if (ir->sampler->type->sampler_dimensionality != GLSL_SAMPLER_DIM_RECT)
60 return visit_continue;
61 }
62 else if (ir->op != ir_txf) {
63 return visit_continue;
64 }
65
66 void *mem_ctx = ralloc_parent(ir);
67
68 if (ir->op == ir_txf) {
69 ir_variable *var = new(mem_ctx) ir_variable(ir->coordinate->type,
70 "coordinate",
71 ir_var_temporary);
72 base_ir->insert_before(var);
73 base_ir->insert_before(assign(var, ir->coordinate));
74 base_ir->insert_before(assign(var,
75 add(swizzle_for_size(var, ir->offset->type->vector_elements), ir->offset),
76 (1 << ir->offset->type->vector_elements) - 1));
77
78 ir->coordinate = new(mem_ctx) ir_dereference_variable(var);
79 } else {
80 ir->coordinate = add(ir->coordinate, i2f(ir->offset));
81 }
82
83 ir->offset = NULL;
84
85 progress = true;
86 return visit_continue;
87 }
88
89 extern "C" {
90
91 bool
92 brw_do_lower_unnormalized_offset(exec_list *instructions)
93 {
94 brw_lower_unnormalized_offset_visitor v;
95
96 visit_list_elements(&v, instructions);
97
98 return v.progress;
99 }
100
101 }