2015-05-04 15:17:56 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2015 Intel Corporation
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
|
*
|
|
|
|
|
* Authors:
|
|
|
|
|
* Jason Ekstrand (jason@jlekstrand.net)
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2015-12-28 11:49:33 -08:00
|
|
|
#include "vtn_private.h"
|
|
|
|
|
#include "GLSL.std.450.h"
|
2015-05-04 15:17:56 -07:00
|
|
|
|
|
|
|
|
static nir_ssa_def*
|
|
|
|
|
build_length(nir_builder *b, nir_ssa_def *vec)
|
|
|
|
|
{
|
|
|
|
|
switch (vec->num_components) {
|
|
|
|
|
case 1: return nir_fsqrt(b, nir_fmul(b, vec, vec));
|
|
|
|
|
case 2: return nir_fsqrt(b, nir_fdot2(b, vec, vec));
|
|
|
|
|
case 3: return nir_fsqrt(b, nir_fdot3(b, vec, vec));
|
|
|
|
|
case 4: return nir_fsqrt(b, nir_fdot4(b, vec, vec));
|
|
|
|
|
default:
|
|
|
|
|
unreachable("Invalid number of components");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-29 15:15:05 -08:00
|
|
|
/**
|
|
|
|
|
* Return e^x.
|
|
|
|
|
*/
|
|
|
|
|
static nir_ssa_def *
|
|
|
|
|
build_exp(nir_builder *b, nir_ssa_def *x)
|
|
|
|
|
{
|
|
|
|
|
return nir_fexp2(b, nir_fmul(b, x, nir_imm_float(b, M_LOG2E)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return ln(x) - the natural logarithm of x.
|
|
|
|
|
*/
|
|
|
|
|
static nir_ssa_def *
|
|
|
|
|
build_log(nir_builder *b, nir_ssa_def *x)
|
|
|
|
|
{
|
|
|
|
|
return nir_fmul(b, nir_flog2(b, x), nir_imm_float(b, 1.0 / M_LOG2E));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-05-04 15:17:56 -07:00
|
|
|
static void
|
2015-09-04 18:39:57 -07:00
|
|
|
handle_glsl450_alu(struct vtn_builder *b, enum GLSLstd450 entrypoint,
|
2015-05-04 15:17:56 -07:00
|
|
|
const uint32_t *w, unsigned count)
|
|
|
|
|
{
|
2015-12-28 21:09:19 -08:00
|
|
|
struct nir_builder *nb = &b->nb;
|
2015-05-04 15:17:56 -07:00
|
|
|
struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
|
2015-08-31 12:48:04 -07:00
|
|
|
val->ssa = rzalloc(b, struct vtn_ssa_value);
|
|
|
|
|
val->ssa->type = vtn_value(b, w[1], vtn_value_type_type)->type->type;
|
2015-05-04 15:17:56 -07:00
|
|
|
|
|
|
|
|
/* Collect the various SSA sources */
|
|
|
|
|
unsigned num_inputs = count - 5;
|
|
|
|
|
nir_ssa_def *src[3];
|
|
|
|
|
for (unsigned i = 0; i < num_inputs; i++)
|
2015-08-31 12:48:04 -07:00
|
|
|
src[i] = vtn_ssa_value(b, w[i + 5])->def;
|
2015-05-04 15:17:56 -07:00
|
|
|
|
|
|
|
|
nir_op op;
|
|
|
|
|
switch (entrypoint) {
|
2015-09-04 18:39:57 -07:00
|
|
|
case GLSLstd450Round: op = nir_op_fround_even; break; /* TODO */
|
|
|
|
|
case GLSLstd450RoundEven: op = nir_op_fround_even; break;
|
|
|
|
|
case GLSLstd450Trunc: op = nir_op_ftrunc; break;
|
|
|
|
|
case GLSLstd450FAbs: op = nir_op_fabs; break;
|
|
|
|
|
case GLSLstd450FSign: op = nir_op_fsign; break;
|
|
|
|
|
case GLSLstd450Floor: op = nir_op_ffloor; break;
|
|
|
|
|
case GLSLstd450Ceil: op = nir_op_fceil; break;
|
|
|
|
|
case GLSLstd450Fract: op = nir_op_ffract; break;
|
|
|
|
|
case GLSLstd450Radians:
|
2015-12-28 21:09:19 -08:00
|
|
|
val->ssa->def = nir_fmul(nb, src[0], nir_imm_float(nb, 0.01745329251));
|
2015-05-04 15:17:56 -07:00
|
|
|
return;
|
2015-09-04 18:39:57 -07:00
|
|
|
case GLSLstd450Degrees:
|
2015-12-28 21:09:19 -08:00
|
|
|
val->ssa->def = nir_fmul(nb, src[0], nir_imm_float(nb, 57.2957795131));
|
2015-05-04 15:17:56 -07:00
|
|
|
return;
|
2015-09-04 18:39:57 -07:00
|
|
|
case GLSLstd450Sin: op = nir_op_fsin; break;
|
|
|
|
|
case GLSLstd450Cos: op = nir_op_fcos; break;
|
|
|
|
|
case GLSLstd450Tan:
|
2015-12-28 21:09:19 -08:00
|
|
|
val->ssa->def = nir_fdiv(nb, nir_fsin(nb, src[0]),
|
|
|
|
|
nir_fcos(nb, src[0]));
|
2015-05-04 15:17:56 -07:00
|
|
|
return;
|
2015-09-04 18:39:57 -07:00
|
|
|
case GLSLstd450Pow: op = nir_op_fpow; break;
|
|
|
|
|
case GLSLstd450Exp2: op = nir_op_fexp2; break;
|
|
|
|
|
case GLSLstd450Log2: op = nir_op_flog2; break;
|
|
|
|
|
case GLSLstd450Sqrt: op = nir_op_fsqrt; break;
|
|
|
|
|
case GLSLstd450InverseSqrt: op = nir_op_frsq; break;
|
|
|
|
|
|
|
|
|
|
case GLSLstd450Modf: op = nir_op_fmod; break;
|
|
|
|
|
case GLSLstd450FMin: op = nir_op_fmin; break;
|
2015-12-03 18:07:58 -08:00
|
|
|
case GLSLstd450UMin: op = nir_op_umin; break;
|
|
|
|
|
case GLSLstd450SMin: op = nir_op_imin; break;
|
2015-09-04 18:39:57 -07:00
|
|
|
case GLSLstd450FMax: op = nir_op_fmax; break;
|
2015-12-03 18:07:58 -08:00
|
|
|
case GLSLstd450UMax: op = nir_op_umax; break;
|
|
|
|
|
case GLSLstd450SMax: op = nir_op_imax; break;
|
2015-10-06 14:44:38 -07:00
|
|
|
case GLSLstd450FMix: op = nir_op_flrp; break;
|
2015-09-04 18:39:57 -07:00
|
|
|
case GLSLstd450Step:
|
2015-12-28 21:09:19 -08:00
|
|
|
val->ssa->def = nir_sge(nb, src[1], src[0]);
|
2015-05-04 15:17:56 -07:00
|
|
|
return;
|
|
|
|
|
|
2015-09-04 18:39:57 -07:00
|
|
|
case GLSLstd450Fma: op = nir_op_ffma; break;
|
|
|
|
|
case GLSLstd450Ldexp: op = nir_op_ldexp; break;
|
2015-05-04 15:17:56 -07:00
|
|
|
|
|
|
|
|
/* Packing/Unpacking functions */
|
2015-09-04 18:39:57 -07:00
|
|
|
case GLSLstd450PackSnorm4x8: op = nir_op_pack_snorm_4x8; break;
|
|
|
|
|
case GLSLstd450PackUnorm4x8: op = nir_op_pack_unorm_4x8; break;
|
|
|
|
|
case GLSLstd450PackSnorm2x16: op = nir_op_pack_snorm_2x16; break;
|
|
|
|
|
case GLSLstd450PackUnorm2x16: op = nir_op_pack_unorm_2x16; break;
|
|
|
|
|
case GLSLstd450PackHalf2x16: op = nir_op_pack_half_2x16; break;
|
|
|
|
|
case GLSLstd450UnpackSnorm4x8: op = nir_op_unpack_snorm_4x8; break;
|
|
|
|
|
case GLSLstd450UnpackUnorm4x8: op = nir_op_unpack_unorm_4x8; break;
|
|
|
|
|
case GLSLstd450UnpackSnorm2x16: op = nir_op_unpack_snorm_2x16; break;
|
|
|
|
|
case GLSLstd450UnpackUnorm2x16: op = nir_op_unpack_unorm_2x16; break;
|
|
|
|
|
case GLSLstd450UnpackHalf2x16: op = nir_op_unpack_half_2x16; break;
|
|
|
|
|
|
|
|
|
|
case GLSLstd450Length:
|
2015-12-28 21:09:19 -08:00
|
|
|
val->ssa->def = build_length(nb, src[0]);
|
2015-05-04 15:17:56 -07:00
|
|
|
return;
|
2015-09-04 18:39:57 -07:00
|
|
|
case GLSLstd450Distance:
|
2015-12-28 21:09:19 -08:00
|
|
|
val->ssa->def = build_length(nb, nir_fsub(nb, src[0], src[1]));
|
2015-05-04 15:17:56 -07:00
|
|
|
return;
|
2015-09-04 18:39:57 -07:00
|
|
|
case GLSLstd450Normalize:
|
2015-12-28 21:09:19 -08:00
|
|
|
val->ssa->def = nir_fdiv(nb, src[0], build_length(nb, src[0]));
|
2015-05-04 15:17:56 -07:00
|
|
|
return;
|
|
|
|
|
|
2015-09-04 18:39:57 -07:00
|
|
|
case GLSLstd450Exp:
|
|
|
|
|
case GLSLstd450Log:
|
|
|
|
|
case GLSLstd450FClamp:
|
|
|
|
|
case GLSLstd450UClamp:
|
|
|
|
|
case GLSLstd450SClamp:
|
|
|
|
|
case GLSLstd450Asin:
|
|
|
|
|
case GLSLstd450Acos:
|
|
|
|
|
case GLSLstd450Atan:
|
|
|
|
|
case GLSLstd450Atan2:
|
|
|
|
|
case GLSLstd450Sinh:
|
|
|
|
|
case GLSLstd450Cosh:
|
|
|
|
|
case GLSLstd450Tanh:
|
|
|
|
|
case GLSLstd450Asinh:
|
|
|
|
|
case GLSLstd450Acosh:
|
|
|
|
|
case GLSLstd450Atanh:
|
|
|
|
|
case GLSLstd450SmoothStep:
|
|
|
|
|
case GLSLstd450Frexp:
|
|
|
|
|
case GLSLstd450PackDouble2x32:
|
|
|
|
|
case GLSLstd450UnpackDouble2x32:
|
|
|
|
|
case GLSLstd450Cross:
|
|
|
|
|
case GLSLstd450FaceForward:
|
|
|
|
|
case GLSLstd450Reflect:
|
|
|
|
|
case GLSLstd450Refract:
|
2015-10-06 14:44:38 -07:00
|
|
|
case GLSLstd450IMix:
|
2015-05-04 15:17:56 -07:00
|
|
|
default:
|
|
|
|
|
unreachable("Unhandled opcode");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nir_alu_instr *instr = nir_alu_instr_create(b->shader, op);
|
|
|
|
|
nir_ssa_dest_init(&instr->instr, &instr->dest.dest,
|
2015-08-31 12:48:04 -07:00
|
|
|
glsl_get_vector_elements(val->ssa->type), val->name);
|
2015-09-04 18:47:56 -07:00
|
|
|
instr->dest.write_mask = (1 << instr->dest.dest.ssa.num_components) - 1;
|
2015-08-31 12:48:04 -07:00
|
|
|
val->ssa->def = &instr->dest.dest.ssa;
|
2015-05-04 15:17:56 -07:00
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < nir_op_infos[op].num_inputs; i++)
|
|
|
|
|
instr->src[i].src = nir_src_for_ssa(src[i]);
|
|
|
|
|
|
2015-12-28 21:09:19 -08:00
|
|
|
nir_builder_instr_insert(nb, &instr->instr);
|
2015-05-04 15:17:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
vtn_handle_glsl450_instruction(struct vtn_builder *b, uint32_t ext_opcode,
|
|
|
|
|
const uint32_t *words, unsigned count)
|
|
|
|
|
{
|
2015-09-04 18:39:57 -07:00
|
|
|
switch ((enum GLSLstd450)ext_opcode) {
|
|
|
|
|
case GLSLstd450Determinant:
|
|
|
|
|
case GLSLstd450MatrixInverse:
|
|
|
|
|
case GLSLstd450InterpolateAtCentroid:
|
|
|
|
|
case GLSLstd450InterpolateAtSample:
|
|
|
|
|
case GLSLstd450InterpolateAtOffset:
|
2015-05-04 15:17:56 -07:00
|
|
|
unreachable("Unhandled opcode");
|
|
|
|
|
|
|
|
|
|
default:
|
2015-09-04 18:39:57 -07:00
|
|
|
handle_glsl450_alu(b, (enum GLSLstd450)ext_opcode, words, count);
|
2015-05-04 15:17:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|