1b8e80941Smrg/*
2b8e80941Smrg * Copyright © 2018 Valve Corporation
3b8e80941Smrg * Copyright © 2017 Red Hat
4b8e80941Smrg *
5b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
6b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
7b8e80941Smrg * to deal in the Software without restriction, including without limitation
8b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
10b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
11b8e80941Smrg *
12b8e80941Smrg * The above copyright notice and this permission notice (including the next
13b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
14b8e80941Smrg * Software.
15b8e80941Smrg *
16b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22b8e80941Smrg * IN THE SOFTWARE.
23b8e80941Smrg *
24b8e80941Smrg */
25b8e80941Smrg
26b8e80941Smrg#include "vtn_private.h"
27b8e80941Smrg#include "GLSL.ext.AMD.h"
28b8e80941Smrg
29b8e80941Smrgbool
30b8e80941Smrgvtn_handle_amd_gcn_shader_instruction(struct vtn_builder *b, SpvOp ext_opcode,
31b8e80941Smrg                                      const uint32_t *w, unsigned count)
32b8e80941Smrg{
33b8e80941Smrg   const struct glsl_type *dest_type =
34b8e80941Smrg                           vtn_value(b, w[1], vtn_value_type_type)->type->type;
35b8e80941Smrg   struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
36b8e80941Smrg   val->ssa = vtn_create_ssa_value(b, dest_type);
37b8e80941Smrg
38b8e80941Smrg   switch ((enum GcnShaderAMD)ext_opcode) {
39b8e80941Smrg   case CubeFaceIndexAMD:
40b8e80941Smrg      val->ssa->def = nir_cube_face_index(&b->nb, vtn_ssa_value(b, w[5])->def);
41b8e80941Smrg	  break;
42b8e80941Smrg   case CubeFaceCoordAMD:
43b8e80941Smrg      val->ssa->def = nir_cube_face_coord(&b->nb, vtn_ssa_value(b, w[5])->def);
44b8e80941Smrg      break;
45b8e80941Smrg   case TimeAMD: {
46b8e80941Smrg      nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b->nb.shader,
47b8e80941Smrg                                    nir_intrinsic_shader_clock);
48b8e80941Smrg      nir_ssa_dest_init(&intrin->instr, &intrin->dest, 2, 32, NULL);
49b8e80941Smrg      nir_builder_instr_insert(&b->nb, &intrin->instr);
50b8e80941Smrg      val->ssa->def = nir_pack_64_2x32(&b->nb, &intrin->dest.ssa);
51b8e80941Smrg      break;
52b8e80941Smrg   }
53b8e80941Smrg   default:
54b8e80941Smrg      unreachable("Invalid opcode");
55b8e80941Smrg   }
56b8e80941Smrg   return true;
57b8e80941Smrg}
58b8e80941Smrg
59b8e80941Smrgbool
60b8e80941Smrgvtn_handle_amd_shader_trinary_minmax_instruction(struct vtn_builder *b, SpvOp ext_opcode,
61b8e80941Smrg                                                 const uint32_t *w, unsigned count)
62b8e80941Smrg{
63b8e80941Smrg   struct nir_builder *nb = &b->nb;
64b8e80941Smrg   const struct glsl_type *dest_type =
65b8e80941Smrg      vtn_value(b, w[1], vtn_value_type_type)->type->type;
66b8e80941Smrg   struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
67b8e80941Smrg   val->ssa = vtn_create_ssa_value(b, dest_type);
68b8e80941Smrg
69b8e80941Smrg   unsigned num_inputs = count - 5;
70b8e80941Smrg   assert(num_inputs == 3);
71b8e80941Smrg   nir_ssa_def *src[3] = { NULL, };
72b8e80941Smrg   for (unsigned i = 0; i < num_inputs; i++)
73b8e80941Smrg      src[i] = vtn_ssa_value(b, w[i + 5])->def;
74b8e80941Smrg
75b8e80941Smrg   switch ((enum ShaderTrinaryMinMaxAMD)ext_opcode) {
76b8e80941Smrg   case FMin3AMD:
77b8e80941Smrg      val->ssa->def = nir_fmin3(nb, src[0], src[1], src[2]);
78b8e80941Smrg      break;
79b8e80941Smrg   case UMin3AMD:
80b8e80941Smrg      val->ssa->def = nir_umin3(nb, src[0], src[1], src[2]);
81b8e80941Smrg      break;
82b8e80941Smrg   case SMin3AMD:
83b8e80941Smrg      val->ssa->def = nir_imin3(nb, src[0], src[1], src[2]);
84b8e80941Smrg      break;
85b8e80941Smrg   case FMax3AMD:
86b8e80941Smrg      val->ssa->def = nir_fmax3(nb, src[0], src[1], src[2]);
87b8e80941Smrg      break;
88b8e80941Smrg   case UMax3AMD:
89b8e80941Smrg      val->ssa->def = nir_umax3(nb, src[0], src[1], src[2]);
90b8e80941Smrg      break;
91b8e80941Smrg   case SMax3AMD:
92b8e80941Smrg      val->ssa->def = nir_imax3(nb, src[0], src[1], src[2]);
93b8e80941Smrg      break;
94b8e80941Smrg   case FMid3AMD:
95b8e80941Smrg      val->ssa->def = nir_fmed3(nb, src[0], src[1], src[2]);
96b8e80941Smrg      break;
97b8e80941Smrg   case UMid3AMD:
98b8e80941Smrg      val->ssa->def = nir_umed3(nb, src[0], src[1], src[2]);
99b8e80941Smrg      break;
100b8e80941Smrg   case SMid3AMD:
101b8e80941Smrg      val->ssa->def = nir_imed3(nb, src[0], src[1], src[2]);
102b8e80941Smrg      break;
103b8e80941Smrg   default:
104b8e80941Smrg      unreachable("unknown opcode\n");
105b8e80941Smrg      break;
106b8e80941Smrg   }
107b8e80941Smrg
108b8e80941Smrg   return true;
109b8e80941Smrg}
110