1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2015 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#include "nir.h"
26bf215546Sopenharmony_ci#include "nir_builder.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci/*
29bf215546Sopenharmony_ci * lowers:
30bf215546Sopenharmony_ci *
31bf215546Sopenharmony_ci * packDouble2x32(foo) -> packDouble2x32Split(foo.x, foo.y)
32bf215546Sopenharmony_ci * unpackDouble2x32(foo) -> vec2(unpackDouble2x32_x(foo), unpackDouble2x32_y(foo))
33bf215546Sopenharmony_ci * packInt2x32(foo) -> packInt2x32Split(foo.x, foo.y)
34bf215546Sopenharmony_ci * unpackInt2x32(foo) -> vec2(unpackInt2x32_x(foo), unpackInt2x32_y(foo))
35bf215546Sopenharmony_ci */
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_cistatic nir_ssa_def *
38bf215546Sopenharmony_cilower_pack_64_from_32(nir_builder *b, nir_ssa_def *src)
39bf215546Sopenharmony_ci{
40bf215546Sopenharmony_ci   return nir_pack_64_2x32_split(b, nir_channel(b, src, 0),
41bf215546Sopenharmony_ci                                    nir_channel(b, src, 1));
42bf215546Sopenharmony_ci}
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_cistatic nir_ssa_def *
45bf215546Sopenharmony_cilower_unpack_64_to_32(nir_builder *b, nir_ssa_def *src)
46bf215546Sopenharmony_ci{
47bf215546Sopenharmony_ci   return nir_vec2(b, nir_unpack_64_2x32_split_x(b, src),
48bf215546Sopenharmony_ci                      nir_unpack_64_2x32_split_y(b, src));
49bf215546Sopenharmony_ci}
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_cistatic nir_ssa_def *
52bf215546Sopenharmony_cilower_pack_32_from_16(nir_builder *b, nir_ssa_def *src)
53bf215546Sopenharmony_ci{
54bf215546Sopenharmony_ci   return nir_pack_32_2x16_split(b, nir_channel(b, src, 0),
55bf215546Sopenharmony_ci                                    nir_channel(b, src, 1));
56bf215546Sopenharmony_ci}
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_cistatic nir_ssa_def *
59bf215546Sopenharmony_cilower_unpack_32_to_16(nir_builder *b, nir_ssa_def *src)
60bf215546Sopenharmony_ci{
61bf215546Sopenharmony_ci   return nir_vec2(b, nir_unpack_32_2x16_split_x(b, src),
62bf215546Sopenharmony_ci                      nir_unpack_32_2x16_split_y(b, src));
63bf215546Sopenharmony_ci}
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_cistatic nir_ssa_def *
66bf215546Sopenharmony_cilower_pack_64_from_16(nir_builder *b, nir_ssa_def *src)
67bf215546Sopenharmony_ci{
68bf215546Sopenharmony_ci   nir_ssa_def *xy = nir_pack_32_2x16_split(b, nir_channel(b, src, 0),
69bf215546Sopenharmony_ci                                               nir_channel(b, src, 1));
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci   nir_ssa_def *zw = nir_pack_32_2x16_split(b, nir_channel(b, src, 2),
72bf215546Sopenharmony_ci                                               nir_channel(b, src, 3));
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci   return nir_pack_64_2x32_split(b, xy, zw);
75bf215546Sopenharmony_ci}
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_cistatic nir_ssa_def *
78bf215546Sopenharmony_cilower_unpack_64_to_16(nir_builder *b, nir_ssa_def *src)
79bf215546Sopenharmony_ci{
80bf215546Sopenharmony_ci   nir_ssa_def *xy = nir_unpack_64_2x32_split_x(b, src);
81bf215546Sopenharmony_ci   nir_ssa_def *zw = nir_unpack_64_2x32_split_y(b, src);
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci   return nir_vec4(b, nir_unpack_32_2x16_split_x(b, xy),
84bf215546Sopenharmony_ci                      nir_unpack_32_2x16_split_y(b, xy),
85bf215546Sopenharmony_ci                      nir_unpack_32_2x16_split_x(b, zw),
86bf215546Sopenharmony_ci                      nir_unpack_32_2x16_split_y(b, zw));
87bf215546Sopenharmony_ci}
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_cistatic nir_ssa_def *
90bf215546Sopenharmony_cilower_pack_32_from_8(nir_builder *b, nir_ssa_def *src)
91bf215546Sopenharmony_ci{
92bf215546Sopenharmony_ci   return nir_pack_32_4x8_split(b, nir_channel(b, src, 0),
93bf215546Sopenharmony_ci                                   nir_channel(b, src, 1),
94bf215546Sopenharmony_ci                                   nir_channel(b, src, 2),
95bf215546Sopenharmony_ci                                   nir_channel(b, src, 3));
96bf215546Sopenharmony_ci}
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_cistatic bool
99bf215546Sopenharmony_cilower_pack_instr(nir_builder *b, nir_instr *instr, void *data)
100bf215546Sopenharmony_ci{
101bf215546Sopenharmony_ci   if (instr->type != nir_instr_type_alu)
102bf215546Sopenharmony_ci      return false;
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci   nir_alu_instr *alu_instr = (nir_alu_instr *) instr;
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   if (alu_instr->op != nir_op_pack_64_2x32 &&
107bf215546Sopenharmony_ci       alu_instr->op != nir_op_unpack_64_2x32 &&
108bf215546Sopenharmony_ci       alu_instr->op != nir_op_pack_64_4x16 &&
109bf215546Sopenharmony_ci       alu_instr->op != nir_op_unpack_64_4x16 &&
110bf215546Sopenharmony_ci       alu_instr->op != nir_op_pack_32_2x16 &&
111bf215546Sopenharmony_ci       alu_instr->op != nir_op_unpack_32_2x16 &&
112bf215546Sopenharmony_ci       alu_instr->op != nir_op_pack_32_4x8)
113bf215546Sopenharmony_ci      return false;
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci   b->cursor = nir_before_instr(&alu_instr->instr);
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci   nir_ssa_def *src = nir_ssa_for_alu_src(b, alu_instr, 0);
118bf215546Sopenharmony_ci   nir_ssa_def *dest;
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci   switch (alu_instr->op) {
121bf215546Sopenharmony_ci   case nir_op_pack_64_2x32:
122bf215546Sopenharmony_ci      dest = lower_pack_64_from_32(b, src);
123bf215546Sopenharmony_ci      break;
124bf215546Sopenharmony_ci   case nir_op_unpack_64_2x32:
125bf215546Sopenharmony_ci      dest = lower_unpack_64_to_32(b, src);
126bf215546Sopenharmony_ci      break;
127bf215546Sopenharmony_ci   case nir_op_pack_64_4x16:
128bf215546Sopenharmony_ci      dest = lower_pack_64_from_16(b, src);
129bf215546Sopenharmony_ci      break;
130bf215546Sopenharmony_ci   case nir_op_unpack_64_4x16:
131bf215546Sopenharmony_ci      dest = lower_unpack_64_to_16(b, src);
132bf215546Sopenharmony_ci      break;
133bf215546Sopenharmony_ci   case nir_op_pack_32_2x16:
134bf215546Sopenharmony_ci      dest = lower_pack_32_from_16(b, src);
135bf215546Sopenharmony_ci      break;
136bf215546Sopenharmony_ci   case nir_op_unpack_32_2x16:
137bf215546Sopenharmony_ci      dest = lower_unpack_32_to_16(b, src);
138bf215546Sopenharmony_ci      break;
139bf215546Sopenharmony_ci   case nir_op_pack_32_4x8:
140bf215546Sopenharmony_ci      dest = lower_pack_32_from_8(b, src);
141bf215546Sopenharmony_ci      break;
142bf215546Sopenharmony_ci   default:
143bf215546Sopenharmony_ci      unreachable("Impossible opcode");
144bf215546Sopenharmony_ci   }
145bf215546Sopenharmony_ci   nir_ssa_def_rewrite_uses(&alu_instr->dest.dest.ssa, dest);
146bf215546Sopenharmony_ci   nir_instr_remove(&alu_instr->instr);
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_ci   return true;
149bf215546Sopenharmony_ci}
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_cibool
152bf215546Sopenharmony_cinir_lower_pack(nir_shader *shader)
153bf215546Sopenharmony_ci{
154bf215546Sopenharmony_ci   return nir_shader_instructions_pass(shader, lower_pack_instr,
155bf215546Sopenharmony_ci         nir_metadata_block_index | nir_metadata_dominance, NULL);
156bf215546Sopenharmony_ci}
157