2 * Copyright © 2015 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 DEALINGS
24 * Jason Ekstrand <jason@jlekstrand.net>
28 #include "nir_builder.h"
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.
37 normalize_cubemap_coords_block(nir_block
*block
, nir_builder
*b
)
39 bool progress
= false;
41 nir_foreach_instr(instr
, block
) {
42 if (instr
->type
!= nir_instr_type_tex
)
45 nir_tex_instr
*tex
= nir_instr_as_tex(instr
);
46 if (tex
->sampler_dim
!= GLSL_SAMPLER_DIM_CUBE
)
49 b
->cursor
= nir_before_instr(&tex
->instr
);
51 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
52 if (tex
->src
[i
].src_type
!= nir_tex_src_coord
)
55 nir_ssa_def
*orig_coord
=
56 nir_ssa_for_src(b
, tex
->src
[i
].src
, nir_tex_instr_src_size(tex
, i
));
57 assert(orig_coord
->num_components
>= 3);
59 nir_ssa_def
*abs
= nir_fabs(b
, orig_coord
);
60 nir_ssa_def
*norm
= nir_fmax(b
, nir_channel(b
, abs
, 0),
61 nir_fmax(b
, nir_channel(b
, abs
, 1),
62 nir_channel(b
, abs
, 2)));
64 nir_ssa_def
*normalized
= nir_fmul(b
, orig_coord
, nir_frcp(b
, norm
));
66 /* Array indices don't have to be normalized, so make a new vector
67 * with the coordinate's array index untouched.
69 if (tex
->coord_components
== 4) {
70 normalized
= nir_vec4(b
,
71 nir_channel(b
, normalized
, 0),
72 nir_channel(b
, normalized
, 1),
73 nir_channel(b
, normalized
, 2),
74 nir_channel(b
, orig_coord
, 3));
77 nir_instr_rewrite_src(&tex
->instr
,
79 nir_src_for_ssa(normalized
));
89 normalize_cubemap_coords_impl(nir_function_impl
*impl
)
92 nir_builder_init(&b
, impl
);
93 bool progress
= false;
95 nir_foreach_block(block
, impl
) {
96 progress
|= normalize_cubemap_coords_block(block
, &b
);
99 nir_metadata_preserve(impl
, nir_metadata_block_index
|
100 nir_metadata_dominance
);
106 nir_normalize_cubemap_coords(nir_shader
*shader
)
108 bool progress
= false;
110 nir_foreach_function(function
, shader
) {
112 progress
= normalize_cubemap_coords_impl(function
->impl
) || progress
;