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 "spirv_to_nir_private.h"
30 enum GLSL450Entrypoint
{
67 Modf
= 32, // second argument needs the OpVariable = , not an OpLoad
95 UnpackDouble2x32
= 57,
110 BitfieldExtract
= 70,
112 BitfieldReverse
= 72,
117 InterpolateAtCentroid
= 76,
118 InterpolateAtSample
= 77,
119 InterpolateAtOffset
= 78,
125 build_length(nir_builder
*b
, nir_ssa_def
*vec
)
127 switch (vec
->num_components
) {
128 case 1: return nir_fsqrt(b
, nir_fmul(b
, vec
, vec
));
129 case 2: return nir_fsqrt(b
, nir_fdot2(b
, vec
, vec
));
130 case 3: return nir_fsqrt(b
, nir_fdot3(b
, vec
, vec
));
131 case 4: return nir_fsqrt(b
, nir_fdot4(b
, vec
, vec
));
133 unreachable("Invalid number of components");
138 handle_glsl450_alu(struct vtn_builder
*b
, enum GLSL450Entrypoint entrypoint
,
139 const uint32_t *w
, unsigned count
)
141 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
142 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
143 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
144 val
->ssa
->type
= val
->type
;
146 /* Collect the various SSA sources */
147 unsigned num_inputs
= count
- 5;
149 for (unsigned i
= 0; i
< num_inputs
; i
++)
150 src
[i
] = vtn_ssa_value(b
, w
[i
+ 5])->def
;
153 switch (entrypoint
) {
154 case Round
: op
= nir_op_fround_even
; break; /* TODO */
155 case RoundEven
: op
= nir_op_fround_even
; break;
156 case Trunc
: op
= nir_op_ftrunc
; break;
157 case Abs
: op
= nir_op_fabs
; break;
158 case Sign
: op
= nir_op_fsign
; break;
159 case Floor
: op
= nir_op_ffloor
; break;
160 case Ceil
: op
= nir_op_fceil
; break;
161 case Fract
: op
= nir_op_ffract
; break;
163 val
->ssa
->def
= nir_fmul(&b
->nb
, src
[0], nir_imm_float(&b
->nb
, 0.01745329251));
166 val
->ssa
->def
= nir_fmul(&b
->nb
, src
[0], nir_imm_float(&b
->nb
, 57.2957795131));
168 case Sin
: op
= nir_op_fsin
; break;
169 case Cos
: op
= nir_op_fcos
; break;
171 val
->ssa
->def
= nir_fdiv(&b
->nb
, nir_fsin(&b
->nb
, src
[0]),
172 nir_fcos(&b
->nb
, src
[0]));
174 case Pow
: op
= nir_op_fpow
; break;
175 case Exp2
: op
= nir_op_fexp2
; break;
176 case Log2
: op
= nir_op_flog2
; break;
177 case Sqrt
: op
= nir_op_fsqrt
; break;
178 case InverseSqrt
: op
= nir_op_frsq
; break;
180 case Modf
: op
= nir_op_fmod
; break;
181 case Min
: op
= nir_op_fmin
; break;
182 case Max
: op
= nir_op_fmax
; break;
183 case Mix
: op
= nir_op_flrp
; break;
185 val
->ssa
->def
= nir_sge(&b
->nb
, src
[1], src
[0]);
189 case FloatBitsToUint
:
191 case UintBitsToFloat
:
192 /* Probably going to be removed from the final version of the spec. */
193 val
->ssa
->def
= src
[0];
196 case Fma
: op
= nir_op_ffma
; break;
197 case Ldexp
: op
= nir_op_ldexp
; break;
199 /* Packing/Unpacking functions */
200 case PackSnorm4x8
: op
= nir_op_pack_snorm_4x8
; break;
201 case PackUnorm4x8
: op
= nir_op_pack_unorm_4x8
; break;
202 case PackSnorm2x16
: op
= nir_op_pack_snorm_2x16
; break;
203 case PackUnorm2x16
: op
= nir_op_pack_unorm_2x16
; break;
204 case PackHalf2x16
: op
= nir_op_pack_half_2x16
; break;
205 case UnpackSnorm4x8
: op
= nir_op_unpack_snorm_4x8
; break;
206 case UnpackUnorm4x8
: op
= nir_op_unpack_unorm_4x8
; break;
207 case UnpackSnorm2x16
: op
= nir_op_unpack_snorm_2x16
; break;
208 case UnpackUnorm2x16
: op
= nir_op_unpack_unorm_2x16
; break;
209 case UnpackHalf2x16
: op
= nir_op_unpack_half_2x16
; break;
212 val
->ssa
->def
= build_length(&b
->nb
, src
[0]);
215 val
->ssa
->def
= build_length(&b
->nb
, nir_fsub(&b
->nb
, src
[0], src
[1]));
218 val
->ssa
->def
= nir_fdiv(&b
->nb
, src
[0], build_length(&b
->nb
, src
[0]));
221 case UaddCarry
: op
= nir_op_uadd_carry
; break;
222 case UsubBorrow
: op
= nir_op_usub_borrow
; break;
223 case BitfieldExtract
: op
= nir_op_ubitfield_extract
; break; /* TODO */
224 case BitfieldInsert
: op
= nir_op_bitfield_insert
; break;
225 case BitfieldReverse
: op
= nir_op_bitfield_reverse
; break;
226 case BitCount
: op
= nir_op_bit_count
; break;
227 case FindLSB
: op
= nir_op_find_lsb
; break;
228 case FindMSB
: op
= nir_op_ufind_msb
; break; /* TODO */
246 case UnpackDouble2x32
:
255 unreachable("Unhandled opcode");
258 nir_alu_instr
*instr
= nir_alu_instr_create(b
->shader
, op
);
259 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
.dest
,
260 glsl_get_vector_elements(val
->type
), val
->name
);
261 val
->ssa
->def
= &instr
->dest
.dest
.ssa
;
263 for (unsigned i
= 0; i
< nir_op_infos
[op
].num_inputs
; i
++)
264 instr
->src
[i
].src
= nir_src_for_ssa(src
[i
]);
266 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
270 vtn_handle_glsl450_instruction(struct vtn_builder
*b
, uint32_t ext_opcode
,
271 const uint32_t *words
, unsigned count
)
273 switch ((enum GLSL450Entrypoint
)ext_opcode
) {
276 case InterpolateAtCentroid
:
277 case InterpolateAtSample
:
278 case InterpolateAtOffset
:
279 unreachable("Unhandled opcode");
282 handle_glsl450_alu(b
, (enum GLSL450Entrypoint
)ext_opcode
, words
, count
);