Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5564 serge 1
/*
2
 * Copyright © 2015 Broadcom
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
 
24
/*
25
 * This lowering pass converts the coordinate division for texture projection
26
 * to be done in ALU instructions instead of asking the texture operation to
27
 * do so.
28
 */
29
 
30
#include "nir.h"
31
#include "nir_builder.h"
32
 
33
static nir_ssa_def *
34
channel(nir_builder *b, nir_ssa_def *def, int c)
35
{
36
   return nir_swizzle(b, def, (unsigned[4]){c, c, c, c}, 1, false);
37
}
38
 
39
static bool
40
nir_lower_tex_projector_block(nir_block *block, void *void_state)
41
{
42
   nir_builder *b = void_state;
43
 
44
   nir_foreach_instr_safe(block, instr) {
45
      if (instr->type != nir_instr_type_tex)
46
         continue;
47
 
48
      nir_tex_instr *tex = nir_instr_as_tex(instr);
49
      nir_builder_insert_before_instr(b, &tex->instr);
50
 
51
      /* Find the projector in the srcs list, if present. */
52
      int proj_index;
53
      for (proj_index = 0; proj_index < tex->num_srcs; proj_index++) {
54
         if (tex->src[proj_index].src_type == nir_tex_src_projector)
55
            break;
56
      }
57
      if (proj_index == tex->num_srcs)
58
         continue;
59
      nir_ssa_def *inv_proj =
60
         nir_frcp(b, nir_ssa_for_src(b, tex->src[proj_index].src, 1));
61
 
62
      /* Walk through the sources projecting the arguments. */
63
      for (int i = 0; i < tex->num_srcs; i++) {
64
         switch (tex->src[i].src_type) {
65
         case nir_tex_src_coord:
66
         case nir_tex_src_comparitor:
67
            break;
68
         default:
69
            continue;
70
         }
71
         nir_ssa_def *unprojected =
72
            nir_ssa_for_src(b, tex->src[i].src, nir_tex_instr_src_size(tex, i));
73
         nir_ssa_def *projected = nir_fmul(b, unprojected, inv_proj);
74
 
75
         /* Array indices don't get projected, so make an new vector with the
76
          * coordinate's array index untouched.
77
          */
78
         if (tex->is_array && tex->src[i].src_type == nir_tex_src_coord) {
79
            switch (tex->coord_components) {
80
            case 4:
81
               projected = nir_vec4(b,
82
                                    channel(b, projected, 0),
83
                                    channel(b, projected, 1),
84
                                    channel(b, projected, 2),
85
                                    channel(b, unprojected, 3));
86
               break;
87
            case 3:
88
               projected = nir_vec3(b,
89
                                    channel(b, projected, 0),
90
                                    channel(b, projected, 1),
91
                                    channel(b, unprojected, 2));
92
               break;
93
            case 2:
94
               projected = nir_vec2(b,
95
                                    channel(b, projected, 0),
96
                                    channel(b, unprojected, 1));
97
               break;
98
            default:
99
               unreachable("bad texture coord count for array");
100
               break;
101
            }
102
         }
103
 
104
         nir_instr_rewrite_src(&tex->instr,
105
                               &tex->src[i].src,
106
                               nir_src_for_ssa(projected));
107
      }
108
 
109
      /* Now move the later tex sources down the array so that the projector
110
       * disappears.
111
       */
112
      nir_instr_rewrite_src(&tex->instr, &tex->src[proj_index].src,
113
                            NIR_SRC_INIT);
114
      for (int i = proj_index + 1; i < tex->num_srcs; i++) {
115
         tex->src[i-1].src_type = tex->src[i].src_type;
116
         nir_instr_move_src(&tex->instr, &tex->src[i-1].src, &tex->src[i].src);
117
      }
118
      tex->num_srcs--;
119
   }
120
 
121
   return true;
122
}
123
 
124
static void
125
nir_lower_tex_projector_impl(nir_function_impl *impl)
126
{
127
   nir_builder b;
128
   nir_builder_init(&b, impl);
129
 
130
   nir_foreach_block(impl, nir_lower_tex_projector_block, &b);
131
 
132
   nir_metadata_preserve(impl, nir_metadata_block_index |
133
                               nir_metadata_dominance);
134
}
135
 
136
void
137
nir_lower_tex_projector(nir_shader *shader)
138
{
139
   nir_foreach_overload(shader, overload) {
140
      if (overload->impl)
141
         nir_lower_tex_projector_impl(overload->impl);
142
   }
143
}