nir: move to compiler/
[mesa.git] / src / compiler / nir / nir_normalize_cubemap_coords.c
1 /*
2 * Copyright © 2015 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 DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Jason Ekstrand <jason@jlekstrand.net>
25 */
26
27 #include "nir.h"
28 #include "nir_builder.h"
29
30 /**
31 * This file implements a NIR lowering pass to perform the normalization of
32 * the cubemap coordinates to have the largest magnitude component be -1.0
33 * or 1.0. This is based on the old GLSL IR based pass by Eric.
34 */
35
36 struct normalize_cubemap_state {
37 nir_builder b;
38 bool progress;
39 };
40
41 static bool
42 normalize_cubemap_coords_block(nir_block *block, void *void_state)
43 {
44 struct normalize_cubemap_state *state = void_state;
45 nir_builder *b = &state->b;
46
47 nir_foreach_instr(block, instr) {
48 if (instr->type != nir_instr_type_tex)
49 continue;
50
51 nir_tex_instr *tex = nir_instr_as_tex(instr);
52 if (tex->sampler_dim != GLSL_SAMPLER_DIM_CUBE)
53 continue;
54
55 b->cursor = nir_before_instr(&tex->instr);
56
57 for (unsigned i = 0; i < tex->num_srcs; i++) {
58 if (tex->src[i].src_type != nir_tex_src_coord)
59 continue;
60
61 nir_ssa_def *orig_coord =
62 nir_ssa_for_src(b, tex->src[i].src, nir_tex_instr_src_size(tex, i));
63 assert(orig_coord->num_components >= 3);
64
65 nir_ssa_def *abs = nir_fabs(b, orig_coord);
66 nir_ssa_def *norm = nir_fmax(b, nir_channel(b, abs, 0),
67 nir_fmax(b, nir_channel(b, abs, 1),
68 nir_channel(b, abs, 2)));
69
70 nir_ssa_def *normalized = nir_fmul(b, orig_coord, nir_frcp(b, norm));
71
72 /* Array indices don't have to be normalized, so make a new vector
73 * with the coordinate's array index untouched.
74 */
75 if (tex->coord_components == 4) {
76 normalized = nir_vec4(b,
77 nir_channel(b, normalized, 0),
78 nir_channel(b, normalized, 1),
79 nir_channel(b, normalized, 2),
80 nir_channel(b, orig_coord, 3));
81 }
82
83 nir_instr_rewrite_src(&tex->instr,
84 &tex->src[i].src,
85 nir_src_for_ssa(normalized));
86
87 state->progress = true;
88 }
89 }
90
91 return true;
92 }
93
94 static bool
95 normalize_cubemap_coords_impl(nir_function_impl *impl)
96 {
97 struct normalize_cubemap_state state;
98 nir_builder_init(&state.b, impl);
99 state.progress = false;
100
101 nir_foreach_block(impl, normalize_cubemap_coords_block, &state);
102
103 nir_metadata_preserve(impl, nir_metadata_block_index |
104 nir_metadata_dominance);
105
106 return state.progress;
107 }
108
109 bool
110 nir_normalize_cubemap_coords(nir_shader *shader)
111 {
112 bool progress = false;
113
114 nir_foreach_function(shader, function) {
115 if (function->impl)
116 progress = normalize_cubemap_coords_impl(function->impl) || progress;
117 }
118
119 return progress;
120 }