1aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt/*
2aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * Copyright © 2010 Intel Corporation
3aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt *
4aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * Permission is hereby granted, free of charge, to any person obtaining a
5aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * copy of this software and associated documentation files (the "Software"),
6aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * to deal in the Software without restriction, including without limitation
7aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * and/or sell copies of the Software, and to permit persons to whom the
9aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * Software is furnished to do so, subject to the following conditions:
10aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt *
11aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * The above copyright notice and this permission notice (including the next
12aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * paragraph) shall be included in all copies or substantial portions of the
13aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * Software.
14aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt *
15aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * DEALINGS IN THE SOFTWARE.
22aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt */
23aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt
24aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt/**
25aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * \file lower_texture_projection.cpp
26aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt *
27aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * IR lower pass to perform the division of texture coordinates by the texture
28aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * projector if present.
29aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt *
30aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * Many GPUs have a texture sampling opcode that takes the projector
31aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * and does the divide internally, thus the presence of the projector
32aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * in the IR.  For GPUs that don't, this saves the driver needing the
33aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * logic for handling the divide.
34aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt *
35aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt * \author Eric Anholt <eric@anholt.net>
36aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt */
37aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt
38aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt#include "ir.h"
39aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt
40aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholtclass lower_texture_projection_visitor : public ir_hierarchical_visitor {
41aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholtpublic:
42aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt   lower_texture_projection_visitor()
43aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt   {
44aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt      progress = false;
45aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt   }
46aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt
47aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt   ir_visitor_status visit_leave(ir_texture *ir);
48aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt
49aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt   bool progress;
50aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt};
51aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt
52aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholtir_visitor_status
53aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholtlower_texture_projection_visitor::visit_leave(ir_texture *ir)
54aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt{
55aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt   if (!ir->projector)
56aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt      return visit_continue;
57aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt
58d3073f58c17d8675a2ecdd5dfa83e5520c78e1a8Kenneth Graunke   void *mem_ctx = ralloc_parent(ir);
59aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt
60aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt   ir_variable *var = new(mem_ctx) ir_variable(ir->projector->type,
61aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt					       "projector", ir_var_auto);
62aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt   base_ir->insert_before(var);
63aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt   ir_dereference *deref = new(mem_ctx) ir_dereference_variable(var);
64aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt   ir_expression *expr = new(mem_ctx) ir_expression(ir_unop_rcp,
65aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt						    ir->projector->type,
66aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt						    ir->projector,
67aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt						    NULL);
68aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt   ir_assignment *assign = new(mem_ctx) ir_assignment(deref, expr, NULL);
69aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt   base_ir->insert_before(assign);
70aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt
71aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt   deref = new(mem_ctx) ir_dereference_variable(var);
72aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt   ir->coordinate = new(mem_ctx) ir_expression(ir_binop_mul,
73aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt					       ir->coordinate->type,
74aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt					       ir->coordinate,
75aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt					       deref);
76aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt
77aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt   if (ir->shadow_comparitor) {
78aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt      deref = new(mem_ctx) ir_dereference_variable(var);
79aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt      ir->shadow_comparitor = new(mem_ctx) ir_expression(ir_binop_mul,
80aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt						  ir->shadow_comparitor->type,
81aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt						  ir->shadow_comparitor,
82aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt						  deref);
83aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt   }
84aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt
85aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt   ir->projector = NULL;
86aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt
87aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt   progress = true;
88aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt   return visit_continue;
89aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt}
90aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt
91aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholtbool
92aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholtdo_lower_texture_projection(exec_list *instructions)
93aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt{
94aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt   lower_texture_projection_visitor v;
95aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt
96aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt   visit_list_elements(&v, instructions);
97aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt
98aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt   return v.progress;
99aae338104fa6022b8b1d6b22c7ad1115b252b9b6Eric Anholt}
100