2022-02-10 19:48:19 -08:00
|
|
|
/*
|
|
|
|
* HLSL constant value operations for constant folding
|
|
|
|
*
|
|
|
|
* Copyright 2022 Francisco Casas for CodeWeavers
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2022-04-22 03:24:57 -07:00
|
|
|
#include <math.h>
|
|
|
|
|
2022-02-10 19:48:19 -08:00
|
|
|
#include "hlsl.h"
|
|
|
|
|
2023-05-23 08:07:58 -07:00
|
|
|
static bool fold_abs(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst,
|
|
|
|
const struct hlsl_type *dst_type, const struct hlsl_ir_constant *src)
|
2023-05-08 10:21:37 -07:00
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2023-05-08 10:21:37 -07:00
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src->node.data_type->e.numeric.type);
|
2023-05-08 10:21:37 -07:00
|
|
|
|
2023-05-23 08:07:58 -07:00
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
2023-05-08 10:21:37 -07:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
2023-05-23 08:07:58 -07:00
|
|
|
dst->u[k].f = fabsf(src->value.u[k].f);
|
2023-05-08 10:21:37 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_DOUBLE:
|
2023-05-23 08:07:58 -07:00
|
|
|
dst->u[k].d = fabs(src->value.u[k].d);
|
2023-05-08 10:21:37 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_INT:
|
|
|
|
/* C's abs(INT_MIN) is undefined, but HLSL evaluates this to INT_MIN */
|
|
|
|
if (src->value.u[k].i == INT_MIN)
|
2023-05-23 08:07:58 -07:00
|
|
|
dst->u[k].i = INT_MIN;
|
2023-05-08 10:21:37 -07:00
|
|
|
else
|
2023-05-23 08:07:58 -07:00
|
|
|
dst->u[k].i = abs(src->value.u[k].i);
|
2023-05-08 10:21:37 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_UINT:
|
2023-05-23 08:07:58 -07:00
|
|
|
dst->u[k].u = src->value.u[k].u;
|
2023-05-08 10:21:37 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-05-23 08:07:58 -07:00
|
|
|
FIXME("Fold abs() for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
2023-05-08 10:21:37 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-09-10 13:50:01 -07:00
|
|
|
static uint32_t float_to_uint(float x)
|
|
|
|
{
|
|
|
|
if (isnan(x) || x <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (x >= 4294967296.0f)
|
|
|
|
return UINT32_MAX;
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t float_to_int(float x)
|
|
|
|
{
|
|
|
|
if (isnan(x))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (x <= -2147483648.0f)
|
|
|
|
return INT32_MIN;
|
|
|
|
|
|
|
|
if (x >= 2147483648.0f)
|
|
|
|
return INT32_MAX;
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2023-09-12 05:03:33 -07:00
|
|
|
static uint32_t double_to_uint(double x)
|
|
|
|
{
|
|
|
|
if (isnan(x) || x <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (x >= 4294967296.0)
|
|
|
|
return UINT32_MAX;
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t double_to_int(double x)
|
|
|
|
{
|
|
|
|
if (isnan(x))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (x <= -2147483648.0)
|
|
|
|
return INT32_MIN;
|
|
|
|
|
|
|
|
if (x >= 2147483648.0)
|
|
|
|
return INT32_MAX;
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2023-09-23 10:21:42 -07:00
|
|
|
static bool fold_bit_not(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst,
|
|
|
|
const struct hlsl_type *dst_type, const struct hlsl_ir_constant *src)
|
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2023-09-23 10:21:42 -07:00
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src->node.data_type->e.numeric.type);
|
2023-09-23 10:21:42 -07:00
|
|
|
|
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case HLSL_TYPE_INT:
|
|
|
|
case HLSL_TYPE_UINT:
|
|
|
|
case HLSL_TYPE_BOOL:
|
|
|
|
dst->u[k].u = ~src->value.u[k].u;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
vkd3d_unreachable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-11 16:43:57 -08:00
|
|
|
static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst,
|
|
|
|
const struct hlsl_type *dst_type, const struct hlsl_ir_constant *src)
|
2022-02-16 13:00:47 -08:00
|
|
|
{
|
|
|
|
unsigned int k;
|
|
|
|
uint32_t u;
|
|
|
|
int32_t i;
|
|
|
|
double d;
|
|
|
|
float f;
|
|
|
|
|
2022-11-11 16:43:57 -08:00
|
|
|
if (dst_type->dimx != src->node.data_type->dimx
|
|
|
|
|| dst_type->dimy != src->node.data_type->dimy)
|
2022-02-16 13:00:47 -08:00
|
|
|
{
|
|
|
|
FIXME("Cast from %s to %s.\n", debug_hlsl_type(ctx, src->node.data_type),
|
2022-11-11 16:43:57 -08:00
|
|
|
debug_hlsl_type(ctx, dst_type));
|
2022-02-16 13:00:47 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-28 14:14:55 -07:00
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
2022-02-16 13:00:47 -08:00
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
switch (src->node.data_type->e.numeric.type)
|
2022-02-16 13:00:47 -08:00
|
|
|
{
|
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
2023-09-10 13:50:01 -07:00
|
|
|
u = float_to_uint(src->value.u[k].f);
|
|
|
|
i = float_to_int(src->value.u[k].f);
|
2022-11-11 16:39:55 -08:00
|
|
|
f = src->value.u[k].f;
|
|
|
|
d = src->value.u[k].f;
|
2022-02-16 13:00:47 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_DOUBLE:
|
2023-09-12 05:03:33 -07:00
|
|
|
u = double_to_uint(src->value.u[k].d);
|
|
|
|
i = double_to_int(src->value.u[k].d);
|
2022-11-11 16:39:55 -08:00
|
|
|
f = src->value.u[k].d;
|
|
|
|
d = src->value.u[k].d;
|
2022-02-16 13:00:47 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_INT:
|
2022-11-11 16:39:55 -08:00
|
|
|
u = src->value.u[k].i;
|
|
|
|
i = src->value.u[k].i;
|
|
|
|
f = src->value.u[k].i;
|
|
|
|
d = src->value.u[k].i;
|
2022-02-16 13:00:47 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_UINT:
|
2022-11-11 16:39:55 -08:00
|
|
|
u = src->value.u[k].u;
|
|
|
|
i = src->value.u[k].u;
|
|
|
|
f = src->value.u[k].u;
|
|
|
|
d = src->value.u[k].u;
|
2022-02-16 13:00:47 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_BOOL:
|
2022-11-11 16:39:55 -08:00
|
|
|
u = !!src->value.u[k].u;
|
|
|
|
i = !!src->value.u[k].u;
|
|
|
|
f = !!src->value.u[k].u;
|
|
|
|
d = !!src->value.u[k].u;
|
2022-02-16 13:00:47 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-08-31 04:25:24 -07:00
|
|
|
vkd3d_unreachable();
|
2022-02-16 13:00:47 -08:00
|
|
|
}
|
|
|
|
|
2024-02-27 15:30:51 -08:00
|
|
|
switch (dst_type->e.numeric.type)
|
2022-02-16 13:00:47 -08:00
|
|
|
{
|
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
2022-11-11 16:43:57 -08:00
|
|
|
dst->u[k].f = f;
|
2022-02-16 13:00:47 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_DOUBLE:
|
2022-11-11 16:43:57 -08:00
|
|
|
dst->u[k].d = d;
|
2022-02-16 13:00:47 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_INT:
|
2022-11-11 16:43:57 -08:00
|
|
|
dst->u[k].i = i;
|
2022-02-16 13:00:47 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_UINT:
|
2022-11-11 16:43:57 -08:00
|
|
|
dst->u[k].u = u;
|
2022-02-16 13:00:47 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_BOOL:
|
2022-04-06 12:24:42 -07:00
|
|
|
/* Casts to bool should have already been lowered. */
|
2022-02-16 13:00:47 -08:00
|
|
|
default:
|
2022-08-31 04:25:24 -07:00
|
|
|
vkd3d_unreachable();
|
2022-02-16 13:00:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-17 14:57:46 -07:00
|
|
|
static bool fold_ceil(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst,
|
|
|
|
const struct hlsl_type *dst_type, const struct hlsl_ir_constant *src)
|
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2023-10-17 14:57:46 -07:00
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src->node.data_type->e.numeric.type);
|
2023-10-17 14:57:46 -07:00
|
|
|
|
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
|
|
|
dst->u[k].f = ceilf(src->value.u[k].f);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
FIXME("Fold 'ceil' for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-09-12 09:34:27 -07:00
|
|
|
static bool fold_exp2(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst,
|
|
|
|
const struct hlsl_type *dst_type, const struct hlsl_ir_constant *src)
|
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2023-09-12 09:34:27 -07:00
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src->node.data_type->e.numeric.type);
|
2023-09-12 09:34:27 -07:00
|
|
|
|
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
|
|
|
dst->u[k].f = exp2f(src->value.u[k].f);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
FIXME("Fold 'exp2' for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-17 15:08:02 -07:00
|
|
|
static bool fold_floor(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst,
|
|
|
|
const struct hlsl_type *dst_type, const struct hlsl_ir_constant *src)
|
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2023-10-17 15:08:02 -07:00
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src->node.data_type->e.numeric.type);
|
2023-10-17 15:08:02 -07:00
|
|
|
|
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
|
|
|
dst->u[k].f = floorf(src->value.u[k].f);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
FIXME("Fold 'floor' for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-09-12 09:31:03 -07:00
|
|
|
static bool fold_fract(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst,
|
|
|
|
const struct hlsl_type *dst_type, const struct hlsl_ir_constant *src)
|
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2023-09-12 09:31:03 -07:00
|
|
|
unsigned int k;
|
|
|
|
float i;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src->node.data_type->e.numeric.type);
|
2023-09-12 09:31:03 -07:00
|
|
|
|
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
|
|
|
dst->u[k].f = modff(src->value.u[k].f, &i);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
FIXME("Fold 'fract' for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-07-05 02:09:21 -07:00
|
|
|
static bool fold_log2(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
|
|
|
const struct hlsl_ir_constant *src, const struct vkd3d_shader_location *loc)
|
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2023-07-05 02:09:21 -07:00
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src->node.data_type->e.numeric.type);
|
2023-07-05 02:09:21 -07:00
|
|
|
|
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
|
|
|
if (ctx->profile->major_version >= 4 && src->value.u[k].f < 0.0f)
|
|
|
|
{
|
|
|
|
hlsl_warning(ctx, loc, VKD3D_SHADER_WARNING_HLSL_NON_FINITE_RESULT,
|
|
|
|
"Indefinite logarithm result.");
|
|
|
|
}
|
|
|
|
dst->u[k].f = log2f(src->value.u[k].f);
|
|
|
|
if (ctx->profile->major_version < 4 && !isfinite(dst->u[k].f))
|
|
|
|
{
|
|
|
|
hlsl_error(ctx, loc, VKD3D_SHADER_ERROR_HLSL_NON_FINITE_RESULT,
|
|
|
|
"Infinities and NaNs are not allowed by the shader model.");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_DOUBLE:
|
|
|
|
if (src->value.u[k].d < 0.0)
|
|
|
|
{
|
|
|
|
hlsl_warning(ctx, loc, VKD3D_SHADER_WARNING_HLSL_NON_FINITE_RESULT,
|
|
|
|
"Indefinite logarithm result.");
|
|
|
|
}
|
|
|
|
dst->u[k].d = log2(src->value.u[k].d);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
FIXME("Fold 'log2' for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-11 16:46:10 -08:00
|
|
|
static bool fold_neg(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst,
|
|
|
|
const struct hlsl_type *dst_type, const struct hlsl_ir_constant *src)
|
2022-02-16 13:00:47 -08:00
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2022-02-16 13:00:47 -08:00
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src->node.data_type->e.numeric.type);
|
2022-02-16 13:00:47 -08:00
|
|
|
|
2023-06-28 14:14:55 -07:00
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
2022-02-16 13:00:47 -08:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
2022-11-11 16:46:10 -08:00
|
|
|
dst->u[k].f = -src->value.u[k].f;
|
2022-02-16 13:00:47 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_DOUBLE:
|
2022-11-11 16:46:10 -08:00
|
|
|
dst->u[k].d = -src->value.u[k].d;
|
2022-02-16 13:00:47 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_INT:
|
|
|
|
case HLSL_TYPE_UINT:
|
2022-11-11 16:46:10 -08:00
|
|
|
dst->u[k].u = -src->value.u[k].u;
|
2022-02-16 13:00:47 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-11-11 16:46:10 -08:00
|
|
|
FIXME("Fold negation for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
2022-02-16 13:00:47 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-09-23 09:18:07 -07:00
|
|
|
static bool fold_not(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst,
|
|
|
|
const struct hlsl_type *dst_type, const struct hlsl_ir_constant *src)
|
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2023-09-23 09:18:07 -07:00
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src->node.data_type->e.numeric.type);
|
2023-09-23 09:18:07 -07:00
|
|
|
|
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case HLSL_TYPE_BOOL:
|
|
|
|
dst->u[k].u = ~src->value.u[k].u;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
FIXME("Fold logic 'not' for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-27 12:18:11 -07:00
|
|
|
static bool fold_rcp(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
|
|
|
const struct hlsl_ir_constant *src, const struct vkd3d_shader_location *loc)
|
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2023-06-27 12:18:11 -07:00
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src->node.data_type->e.numeric.type);
|
2023-06-27 12:18:11 -07:00
|
|
|
|
2023-07-05 11:47:00 -07:00
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
2023-06-27 12:18:11 -07:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
|
|
|
if (ctx->profile->major_version >= 4 && src->value.u[k].f == 0.0f)
|
|
|
|
{
|
|
|
|
hlsl_warning(ctx, loc, VKD3D_SHADER_WARNING_HLSL_DIVISION_BY_ZERO,
|
|
|
|
"Floating point division by zero.");
|
|
|
|
}
|
|
|
|
dst->u[k].f = 1.0f / src->value.u[k].f;
|
|
|
|
if (ctx->profile->major_version < 4 && !isfinite(dst->u[k].f))
|
|
|
|
{
|
|
|
|
hlsl_error(ctx, loc, VKD3D_SHADER_ERROR_HLSL_DIVISION_BY_ZERO,
|
|
|
|
"Infinities and NaNs are not allowed by the shader model.");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_DOUBLE:
|
|
|
|
if (src->value.u[k].d == 0.0)
|
|
|
|
{
|
|
|
|
hlsl_warning(ctx, loc, VKD3D_SHADER_WARNING_HLSL_DIVISION_BY_ZERO,
|
|
|
|
"Floating point division by zero.");
|
|
|
|
}
|
|
|
|
dst->u[k].d = 1.0 / src->value.u[k].d;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
FIXME("Fold 'rcp' for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-09-26 05:32:24 -07:00
|
|
|
static bool fold_rsq(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
|
|
|
const struct hlsl_ir_constant *src, const struct vkd3d_shader_location *loc)
|
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2023-09-26 05:32:24 -07:00
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src->node.data_type->e.numeric.type);
|
2023-09-26 05:32:24 -07:00
|
|
|
|
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
|
|
|
if (ctx->profile->major_version >= 4)
|
|
|
|
{
|
|
|
|
if (src->value.u[k].f < 0.0f)
|
|
|
|
hlsl_warning(ctx, loc, VKD3D_SHADER_WARNING_HLSL_IMAGINARY_NUMERIC_RESULT,
|
|
|
|
"Imaginary square root result.");
|
|
|
|
else if (src->value.u[k].f == 0.0f)
|
|
|
|
hlsl_warning(ctx, loc, VKD3D_SHADER_WARNING_HLSL_IMAGINARY_NUMERIC_RESULT,
|
|
|
|
"Floating point division by zero.");
|
|
|
|
}
|
|
|
|
dst->u[k].f = 1.0f / sqrtf(src->value.u[k].f);
|
|
|
|
if (ctx->profile->major_version < 4 && !isfinite(dst->u[k].f))
|
|
|
|
{
|
|
|
|
hlsl_error(ctx, loc, VKD3D_SHADER_ERROR_HLSL_NON_FINITE_RESULT,
|
|
|
|
"Infinities and NaNs are not allowed by the shader model.");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
FIXME("Fold 'rsq' for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-09-12 09:38:00 -07:00
|
|
|
static bool fold_sat(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
|
|
|
const struct hlsl_ir_constant *src)
|
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2023-09-12 09:38:00 -07:00
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src->node.data_type->e.numeric.type);
|
2023-09-12 09:38:00 -07:00
|
|
|
|
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
|
|
|
dst->u[k].f = min(max(0.0f, src->value.u[k].f), 1.0f);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
FIXME("Fold 'sat' for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-07-05 01:39:04 -07:00
|
|
|
static bool fold_sqrt(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
|
|
|
const struct hlsl_ir_constant *src, const struct vkd3d_shader_location *loc)
|
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2023-07-05 01:39:04 -07:00
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src->node.data_type->e.numeric.type);
|
2023-07-05 01:39:04 -07:00
|
|
|
|
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
|
|
|
if (ctx->profile->major_version >= 4 && src->value.u[k].f < 0.0f)
|
|
|
|
{
|
|
|
|
hlsl_warning(ctx, loc, VKD3D_SHADER_WARNING_HLSL_IMAGINARY_NUMERIC_RESULT,
|
|
|
|
"Imaginary square root result.");
|
|
|
|
}
|
|
|
|
dst->u[k].f = sqrtf(src->value.u[k].f);
|
|
|
|
if (ctx->profile->major_version < 4 && !isfinite(dst->u[k].f))
|
|
|
|
{
|
|
|
|
hlsl_error(ctx, loc, VKD3D_SHADER_ERROR_HLSL_NON_FINITE_RESULT,
|
|
|
|
"Infinities and NaNs are not allowed by the shader model.");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_DOUBLE:
|
|
|
|
if (src->value.u[k].d < 0.0)
|
|
|
|
{
|
|
|
|
hlsl_warning(ctx, loc, VKD3D_SHADER_WARNING_HLSL_IMAGINARY_NUMERIC_RESULT,
|
|
|
|
"Imaginary square root result.");
|
|
|
|
}
|
|
|
|
dst->u[k].d = sqrt(src->value.u[k].d);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
FIXME("Fold 'sqrt' for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-11 16:47:28 -08:00
|
|
|
static bool fold_add(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
|
|
|
const struct hlsl_ir_constant *src1, const struct hlsl_ir_constant *src2)
|
2022-02-16 13:00:47 -08:00
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2022-02-16 13:00:47 -08:00
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src1->node.data_type->e.numeric.type);
|
|
|
|
VKD3D_ASSERT(type == src2->node.data_type->e.numeric.type);
|
2022-02-16 13:00:47 -08:00
|
|
|
|
2023-06-28 14:14:55 -07:00
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
2022-02-16 13:00:47 -08:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
2022-11-11 16:47:28 -08:00
|
|
|
dst->u[k].f = src1->value.u[k].f + src2->value.u[k].f;
|
2022-02-16 13:00:47 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_DOUBLE:
|
2022-11-11 16:47:28 -08:00
|
|
|
dst->u[k].d = src1->value.u[k].d + src2->value.u[k].d;
|
2022-02-16 13:00:47 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Handling HLSL_TYPE_INT through the unsigned field to avoid
|
|
|
|
* undefined behavior with signed integers in C. */
|
|
|
|
case HLSL_TYPE_INT:
|
|
|
|
case HLSL_TYPE_UINT:
|
2022-11-11 16:47:28 -08:00
|
|
|
dst->u[k].u = src1->value.u[k].u + src2->value.u[k].u;
|
2022-02-16 13:00:47 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-11-11 16:47:28 -08:00
|
|
|
FIXME("Fold addition for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
2022-02-16 13:00:47 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-09 13:39:25 -07:00
|
|
|
static bool fold_and(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
2022-11-11 16:48:13 -08:00
|
|
|
const struct hlsl_ir_constant *src1, const struct hlsl_ir_constant *src2)
|
2022-02-16 13:00:47 -08:00
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2022-04-05 03:33:06 -07:00
|
|
|
unsigned int k;
|
2022-02-16 13:00:47 -08:00
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src1->node.data_type->e.numeric.type);
|
|
|
|
VKD3D_ASSERT(type == src2->node.data_type->e.numeric.type);
|
2022-02-16 13:00:47 -08:00
|
|
|
|
2023-06-09 13:21:01 -07:00
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
2022-02-16 13:00:47 -08:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case HLSL_TYPE_INT:
|
|
|
|
case HLSL_TYPE_UINT:
|
2023-06-09 13:39:25 -07:00
|
|
|
case HLSL_TYPE_BOOL:
|
2023-06-09 13:21:01 -07:00
|
|
|
dst->u[k].u = src1->value.u[k].u & src2->value.u[k].u;
|
2022-02-16 13:00:47 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-06-09 13:39:25 -07:00
|
|
|
FIXME("Fold bit/logic and for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
2022-02-16 13:00:47 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-09 13:39:25 -07:00
|
|
|
static bool fold_or(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
2022-11-11 16:49:14 -08:00
|
|
|
const struct hlsl_ir_constant *src1, const struct hlsl_ir_constant *src2)
|
2022-04-05 03:33:13 -07:00
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2022-04-05 03:33:13 -07:00
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src1->node.data_type->e.numeric.type);
|
|
|
|
VKD3D_ASSERT(type == src2->node.data_type->e.numeric.type);
|
2022-04-05 03:33:13 -07:00
|
|
|
|
2023-06-09 13:21:01 -07:00
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
2022-04-05 03:33:13 -07:00
|
|
|
{
|
2023-06-09 13:21:01 -07:00
|
|
|
switch (type)
|
2022-04-05 03:33:13 -07:00
|
|
|
{
|
2023-06-09 13:21:01 -07:00
|
|
|
case HLSL_TYPE_INT:
|
|
|
|
case HLSL_TYPE_UINT:
|
2023-06-09 13:39:25 -07:00
|
|
|
case HLSL_TYPE_BOOL:
|
2023-06-09 13:21:01 -07:00
|
|
|
dst->u[k].u = src1->value.u[k].u | src2->value.u[k].u;
|
2022-04-05 03:33:13 -07:00
|
|
|
break;
|
|
|
|
|
2023-06-09 13:21:01 -07:00
|
|
|
default:
|
2023-06-09 13:39:25 -07:00
|
|
|
FIXME("Fold bit/logic or for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
2023-06-09 13:21:01 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2022-04-05 03:33:13 -07:00
|
|
|
|
2023-06-09 13:21:01 -07:00
|
|
|
static bool fold_bit_xor(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
|
|
|
const struct hlsl_ir_constant *src1, const struct hlsl_ir_constant *src2)
|
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2023-06-09 13:21:01 -07:00
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src1->node.data_type->e.numeric.type);
|
|
|
|
VKD3D_ASSERT(type == src2->node.data_type->e.numeric.type);
|
2023-06-09 13:21:01 -07:00
|
|
|
|
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2022-04-05 03:33:13 -07:00
|
|
|
case HLSL_TYPE_INT:
|
|
|
|
case HLSL_TYPE_UINT:
|
2023-06-09 13:21:01 -07:00
|
|
|
dst->u[k].u = src1->value.u[k].u ^ src2->value.u[k].u;
|
2022-04-05 03:33:13 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-06-09 13:21:01 -07:00
|
|
|
FIXME("Fold bit xor for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
|
|
|
return false;
|
2022-04-05 03:33:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-18 11:46:51 -07:00
|
|
|
static bool fold_dot(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
|
|
|
const struct hlsl_ir_constant *src1, const struct hlsl_ir_constant *src2)
|
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2023-06-18 11:46:51 -07:00
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src1->node.data_type->e.numeric.type);
|
|
|
|
VKD3D_ASSERT(type == src2->node.data_type->e.numeric.type);
|
|
|
|
VKD3D_ASSERT(src1->node.data_type->dimx == src2->node.data_type->dimx);
|
2023-06-18 11:46:51 -07:00
|
|
|
|
|
|
|
dst->u[0].f = 0.0f;
|
|
|
|
for (k = 0; k < src1->node.data_type->dimx; ++k)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
|
|
|
dst->u[0].f += src1->value.u[k].f * src2->value.u[k].f;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FIXME("Fold 'dot' for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-18 14:19:02 -07:00
|
|
|
static bool fold_dp2add(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
|
|
|
const struct hlsl_ir_constant *src1, const struct hlsl_ir_constant *src2, const struct hlsl_ir_constant *src3)
|
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2023-06-18 14:19:02 -07:00
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src1->node.data_type->e.numeric.type);
|
|
|
|
VKD3D_ASSERT(type == src2->node.data_type->e.numeric.type);
|
|
|
|
VKD3D_ASSERT(type == src3->node.data_type->e.numeric.type);
|
|
|
|
VKD3D_ASSERT(src1->node.data_type->dimx == src2->node.data_type->dimx);
|
|
|
|
VKD3D_ASSERT(src3->node.data_type->dimx == 1);
|
2023-06-18 14:19:02 -07:00
|
|
|
|
|
|
|
dst->u[0].f = src3->value.u[0].f;
|
|
|
|
for (k = 0; k < src1->node.data_type->dimx; ++k)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
|
|
|
dst->u[0].f += src1->value.u[k].f * src2->value.u[k].f;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FIXME("Fold 'dp2add' for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-11 16:52:16 -08:00
|
|
|
static bool fold_div(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
|
|
|
const struct hlsl_ir_constant *src1, const struct hlsl_ir_constant *src2,
|
|
|
|
const struct vkd3d_shader_location *loc)
|
2022-04-14 03:52:39 -07:00
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2022-04-14 03:52:39 -07:00
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src1->node.data_type->e.numeric.type);
|
|
|
|
VKD3D_ASSERT(type == src2->node.data_type->e.numeric.type);
|
2022-04-14 03:52:39 -07:00
|
|
|
|
2022-11-11 16:52:16 -08:00
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
2022-04-14 03:52:39 -07:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
2022-11-11 16:39:55 -08:00
|
|
|
if (ctx->profile->major_version >= 4 && src2->value.u[k].f == 0)
|
2022-04-14 03:52:39 -07:00
|
|
|
{
|
2022-11-11 16:52:16 -08:00
|
|
|
hlsl_warning(ctx, loc, VKD3D_SHADER_WARNING_HLSL_DIVISION_BY_ZERO,
|
2022-04-22 03:24:58 -07:00
|
|
|
"Floating point division by zero.");
|
2022-04-14 03:52:39 -07:00
|
|
|
}
|
2022-11-11 16:52:16 -08:00
|
|
|
dst->u[k].f = src1->value.u[k].f / src2->value.u[k].f;
|
|
|
|
if (ctx->profile->major_version < 4 && !isfinite(dst->u[k].f))
|
2022-04-22 03:24:57 -07:00
|
|
|
{
|
2022-11-11 16:52:16 -08:00
|
|
|
hlsl_error(ctx, loc, VKD3D_SHADER_ERROR_HLSL_DIVISION_BY_ZERO,
|
2022-04-27 01:56:20 -07:00
|
|
|
"Infinities and NaNs are not allowed by the shader model.");
|
2022-04-22 03:24:57 -07:00
|
|
|
}
|
2022-04-14 03:52:39 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_DOUBLE:
|
2022-11-11 16:39:55 -08:00
|
|
|
if (src2->value.u[k].d == 0)
|
2022-04-14 03:52:39 -07:00
|
|
|
{
|
2022-11-11 16:52:16 -08:00
|
|
|
hlsl_warning(ctx, loc, VKD3D_SHADER_WARNING_HLSL_DIVISION_BY_ZERO,
|
2022-04-22 03:24:58 -07:00
|
|
|
"Floating point division by zero.");
|
2022-04-14 03:52:39 -07:00
|
|
|
}
|
2022-11-11 16:52:16 -08:00
|
|
|
dst->u[k].d = src1->value.u[k].d / src2->value.u[k].d;
|
2022-04-14 03:52:39 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_INT:
|
2022-11-11 16:39:55 -08:00
|
|
|
if (src2->value.u[k].i == 0)
|
2022-04-14 03:52:39 -07:00
|
|
|
{
|
2022-11-11 16:52:16 -08:00
|
|
|
hlsl_error(ctx, loc, VKD3D_SHADER_ERROR_HLSL_DIVISION_BY_ZERO,
|
2022-04-14 03:52:39 -07:00
|
|
|
"Division by zero.");
|
|
|
|
return false;
|
|
|
|
}
|
2022-11-11 16:39:55 -08:00
|
|
|
if (src1->value.u[k].i == INT_MIN && src2->value.u[k].i == -1)
|
2022-11-11 16:52:16 -08:00
|
|
|
dst->u[k].i = INT_MIN;
|
2022-04-14 03:52:39 -07:00
|
|
|
else
|
2022-11-11 16:52:16 -08:00
|
|
|
dst->u[k].i = src1->value.u[k].i / src2->value.u[k].i;
|
2022-04-14 03:52:39 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_UINT:
|
2022-11-11 16:39:55 -08:00
|
|
|
if (src2->value.u[k].u == 0)
|
2022-04-14 03:52:39 -07:00
|
|
|
{
|
2022-11-11 16:52:16 -08:00
|
|
|
hlsl_error(ctx, loc, VKD3D_SHADER_ERROR_HLSL_DIVISION_BY_ZERO,
|
2022-04-14 03:52:39 -07:00
|
|
|
"Division by zero.");
|
|
|
|
return false;
|
|
|
|
}
|
2022-11-11 16:52:16 -08:00
|
|
|
dst->u[k].u = src1->value.u[k].u / src2->value.u[k].u;
|
2022-04-14 03:52:39 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-11-11 16:52:16 -08:00
|
|
|
FIXME("Fold division for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
2022-04-14 03:52:39 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-09 13:23:40 -07:00
|
|
|
static bool fold_equal(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
|
|
|
const struct hlsl_ir_constant *src1, const struct hlsl_ir_constant *src2)
|
|
|
|
{
|
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(dst_type->e.numeric.type == HLSL_TYPE_BOOL);
|
|
|
|
VKD3D_ASSERT(src1->node.data_type->e.numeric.type == src2->node.data_type->e.numeric.type);
|
2023-06-09 13:23:40 -07:00
|
|
|
|
2023-06-28 14:14:55 -07:00
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
2023-06-09 13:23:40 -07:00
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
switch (src1->node.data_type->e.numeric.type)
|
2023-06-09 13:23:40 -07:00
|
|
|
{
|
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
|
|
|
dst->u[k].u = src1->value.u[k].f == src2->value.u[k].f;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_DOUBLE:
|
|
|
|
dst->u[k].u = src1->value.u[k].d == src2->value.u[k].d;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_INT:
|
|
|
|
case HLSL_TYPE_UINT:
|
|
|
|
case HLSL_TYPE_BOOL:
|
|
|
|
dst->u[k].u = src1->value.u[k].u == src2->value.u[k].u;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
vkd3d_unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
dst->u[k].u *= ~0u;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-09 13:27:03 -07:00
|
|
|
static bool fold_gequal(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
|
|
|
const struct hlsl_ir_constant *src1, const struct hlsl_ir_constant *src2)
|
|
|
|
{
|
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(dst_type->e.numeric.type == HLSL_TYPE_BOOL);
|
|
|
|
VKD3D_ASSERT(src1->node.data_type->e.numeric.type == src2->node.data_type->e.numeric.type);
|
2023-06-09 13:27:03 -07:00
|
|
|
|
2023-06-28 14:14:55 -07:00
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
2023-06-09 13:27:03 -07:00
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
switch (src1->node.data_type->e.numeric.type)
|
2023-06-09 13:27:03 -07:00
|
|
|
{
|
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
|
|
|
dst->u[k].u = src1->value.u[k].f >= src2->value.u[k].f;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_DOUBLE:
|
|
|
|
dst->u[k].u = src1->value.u[k].d >= src2->value.u[k].d;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_INT:
|
|
|
|
dst->u[k].u = src1->value.u[k].i >= src2->value.u[k].i;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_UINT:
|
|
|
|
case HLSL_TYPE_BOOL:
|
|
|
|
dst->u[k].u = src1->value.u[k].u >= src2->value.u[k].u;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
vkd3d_unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
dst->u[k].u *= ~0u;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-09 13:25:51 -07:00
|
|
|
static bool fold_less(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
|
|
|
const struct hlsl_ir_constant *src1, const struct hlsl_ir_constant *src2)
|
|
|
|
{
|
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(dst_type->e.numeric.type == HLSL_TYPE_BOOL);
|
|
|
|
VKD3D_ASSERT(src1->node.data_type->e.numeric.type == src2->node.data_type->e.numeric.type);
|
2023-06-09 13:25:51 -07:00
|
|
|
|
2023-06-28 14:14:55 -07:00
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
2023-06-09 13:25:51 -07:00
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
switch (src1->node.data_type->e.numeric.type)
|
2023-06-09 13:25:51 -07:00
|
|
|
{
|
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
|
|
|
dst->u[k].u = src1->value.u[k].f < src2->value.u[k].f;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_DOUBLE:
|
|
|
|
dst->u[k].u = src1->value.u[k].d < src2->value.u[k].d;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_INT:
|
|
|
|
dst->u[k].u = src1->value.u[k].i < src2->value.u[k].i;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_UINT:
|
|
|
|
case HLSL_TYPE_BOOL:
|
|
|
|
dst->u[k].u = src1->value.u[k].u < src2->value.u[k].u;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
vkd3d_unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
dst->u[k].u *= ~0u;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-09-12 22:29:10 -07:00
|
|
|
static bool fold_lshift(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
|
|
|
const struct hlsl_ir_constant *src1, const struct hlsl_ir_constant *src2)
|
|
|
|
{
|
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(dst_type->e.numeric.type == src1->node.data_type->e.numeric.type);
|
|
|
|
VKD3D_ASSERT(src2->node.data_type->e.numeric.type == HLSL_TYPE_INT);
|
2023-09-12 22:29:10 -07:00
|
|
|
|
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
|
|
|
{
|
|
|
|
unsigned int shift = src2->value.u[k].u % 32;
|
|
|
|
|
2024-02-27 15:30:51 -08:00
|
|
|
switch (src1->node.data_type->e.numeric.type)
|
2023-09-12 22:29:10 -07:00
|
|
|
{
|
|
|
|
case HLSL_TYPE_INT:
|
|
|
|
dst->u[k].i = src1->value.u[k].i << shift;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_UINT:
|
|
|
|
dst->u[k].u = src1->value.u[k].u << shift;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
vkd3d_unreachable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-11 16:54:25 -08:00
|
|
|
static bool fold_max(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
|
|
|
const struct hlsl_ir_constant *src1, const struct hlsl_ir_constant *src2)
|
2022-06-27 08:22:39 -07:00
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2022-06-27 08:22:39 -07:00
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src1->node.data_type->e.numeric.type);
|
|
|
|
VKD3D_ASSERT(type == src2->node.data_type->e.numeric.type);
|
2022-06-27 08:22:39 -07:00
|
|
|
|
2022-11-11 16:54:25 -08:00
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
2022-06-27 08:22:39 -07:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2023-06-09 13:30:12 -07:00
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
|
|
|
dst->u[k].f = fmaxf(src1->value.u[k].f, src2->value.u[k].f);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_DOUBLE:
|
|
|
|
dst->u[k].d = fmax(src1->value.u[k].d, src2->value.u[k].d);
|
|
|
|
break;
|
|
|
|
|
2022-06-27 08:22:39 -07:00
|
|
|
case HLSL_TYPE_INT:
|
2022-11-11 16:54:25 -08:00
|
|
|
dst->u[k].i = max(src1->value.u[k].i, src2->value.u[k].i);
|
2022-06-27 08:22:39 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_UINT:
|
2022-11-11 16:54:25 -08:00
|
|
|
dst->u[k].u = max(src1->value.u[k].u, src2->value.u[k].u);
|
2022-06-27 08:22:39 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-11-11 16:54:25 -08:00
|
|
|
FIXME("Fold max for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
2022-06-27 08:22:39 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-11 16:55:55 -08:00
|
|
|
static bool fold_min(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
|
|
|
const struct hlsl_ir_constant *src1, const struct hlsl_ir_constant *src2)
|
2022-06-27 08:22:40 -07:00
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2022-06-27 08:22:40 -07:00
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src1->node.data_type->e.numeric.type);
|
|
|
|
VKD3D_ASSERT(type == src2->node.data_type->e.numeric.type);
|
2022-06-27 08:22:40 -07:00
|
|
|
|
2022-11-11 16:55:55 -08:00
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
2022-06-27 08:22:40 -07:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2023-06-09 13:30:56 -07:00
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
|
|
|
dst->u[k].f = fminf(src1->value.u[k].f, src2->value.u[k].f);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_DOUBLE:
|
|
|
|
dst->u[k].d = fmin(src1->value.u[k].d, src2->value.u[k].d);
|
|
|
|
break;
|
|
|
|
|
2022-06-27 08:22:40 -07:00
|
|
|
case HLSL_TYPE_INT:
|
2022-11-11 16:55:55 -08:00
|
|
|
dst->u[k].i = min(src1->value.u[k].i, src2->value.u[k].i);
|
2022-06-27 08:22:40 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_UINT:
|
2022-11-11 16:55:55 -08:00
|
|
|
dst->u[k].u = min(src1->value.u[k].u, src2->value.u[k].u);
|
2022-06-27 08:22:40 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-11-11 16:55:55 -08:00
|
|
|
FIXME("Fold min for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
2022-06-27 08:22:40 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-09 13:21:01 -07:00
|
|
|
static bool fold_mod(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
|
|
|
const struct hlsl_ir_constant *src1, const struct hlsl_ir_constant *src2,
|
|
|
|
const struct vkd3d_shader_location *loc)
|
2022-06-27 08:22:41 -07:00
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2022-06-27 08:22:41 -07:00
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src1->node.data_type->e.numeric.type);
|
|
|
|
VKD3D_ASSERT(type == src2->node.data_type->e.numeric.type);
|
2022-06-27 08:22:41 -07:00
|
|
|
|
2022-11-11 16:56:40 -08:00
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
2022-06-27 08:22:41 -07:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case HLSL_TYPE_INT:
|
2023-06-09 13:21:01 -07:00
|
|
|
if (src2->value.u[k].i == 0)
|
|
|
|
{
|
|
|
|
hlsl_error(ctx, loc, VKD3D_SHADER_ERROR_HLSL_DIVISION_BY_ZERO, "Division by zero.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (src1->value.u[k].i == INT_MIN && src2->value.u[k].i == -1)
|
|
|
|
dst->u[k].i = 0;
|
|
|
|
else
|
|
|
|
dst->u[k].i = src1->value.u[k].i % src2->value.u[k].i;
|
|
|
|
break;
|
|
|
|
|
2022-06-27 08:22:41 -07:00
|
|
|
case HLSL_TYPE_UINT:
|
2023-06-09 13:21:01 -07:00
|
|
|
if (src2->value.u[k].u == 0)
|
|
|
|
{
|
|
|
|
hlsl_error(ctx, loc, VKD3D_SHADER_ERROR_HLSL_DIVISION_BY_ZERO, "Division by zero.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
dst->u[k].u = src1->value.u[k].u % src2->value.u[k].u;
|
2022-06-27 08:22:41 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-06-09 13:21:01 -07:00
|
|
|
FIXME("Fold modulus for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
2022-06-27 08:22:41 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-09 13:21:01 -07:00
|
|
|
static bool fold_mul(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
2022-11-11 16:57:25 -08:00
|
|
|
const struct hlsl_ir_constant *src1, const struct hlsl_ir_constant *src2)
|
2022-06-27 08:22:42 -07:00
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
enum hlsl_base_type type = dst_type->e.numeric.type;
|
2022-06-27 08:22:42 -07:00
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(type == src1->node.data_type->e.numeric.type);
|
|
|
|
VKD3D_ASSERT(type == src2->node.data_type->e.numeric.type);
|
2022-06-27 08:22:42 -07:00
|
|
|
|
2023-06-28 14:14:55 -07:00
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
2022-06-27 08:22:42 -07:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2023-06-09 13:21:01 -07:00
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
|
|
|
dst->u[k].f = src1->value.u[k].f * src2->value.u[k].f;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_DOUBLE:
|
|
|
|
dst->u[k].d = src1->value.u[k].d * src2->value.u[k].d;
|
|
|
|
break;
|
|
|
|
|
2022-06-27 08:22:42 -07:00
|
|
|
case HLSL_TYPE_INT:
|
|
|
|
case HLSL_TYPE_UINT:
|
2023-06-09 13:21:01 -07:00
|
|
|
dst->u[k].u = src1->value.u[k].u * src2->value.u[k].u;
|
2022-06-27 08:22:42 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-06-09 13:21:01 -07:00
|
|
|
FIXME("Fold multiplication for type %s.\n", debug_hlsl_type(ctx, dst_type));
|
2022-06-27 08:22:42 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-09 13:21:01 -07:00
|
|
|
static bool fold_nequal(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
2022-11-11 16:58:02 -08:00
|
|
|
const struct hlsl_ir_constant *src1, const struct hlsl_ir_constant *src2)
|
2022-06-27 08:22:43 -07:00
|
|
|
{
|
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(dst_type->e.numeric.type == HLSL_TYPE_BOOL);
|
|
|
|
VKD3D_ASSERT(src1->node.data_type->e.numeric.type == src2->node.data_type->e.numeric.type);
|
2022-06-27 08:22:43 -07:00
|
|
|
|
2023-06-28 14:14:55 -07:00
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
2022-06-27 08:22:43 -07:00
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
switch (src1->node.data_type->e.numeric.type)
|
2022-06-27 08:22:43 -07:00
|
|
|
{
|
2023-06-09 13:21:01 -07:00
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
|
|
|
dst->u[k].u = src1->value.u[k].f != src2->value.u[k].f;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_DOUBLE:
|
|
|
|
dst->u[k].u = src1->value.u[k].d != src2->value.u[k].d;
|
|
|
|
break;
|
|
|
|
|
2022-06-27 08:22:43 -07:00
|
|
|
case HLSL_TYPE_INT:
|
|
|
|
case HLSL_TYPE_UINT:
|
2023-06-09 13:21:01 -07:00
|
|
|
case HLSL_TYPE_BOOL:
|
|
|
|
dst->u[k].u = src1->value.u[k].u != src2->value.u[k].u;
|
2022-06-27 08:22:43 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-06-09 13:21:01 -07:00
|
|
|
vkd3d_unreachable();
|
2022-06-27 08:22:43 -07:00
|
|
|
}
|
2023-06-09 13:21:01 -07:00
|
|
|
|
|
|
|
dst->u[k].u *= ~0u;
|
2022-06-27 08:22:43 -07:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-09-12 08:17:29 -07:00
|
|
|
static bool fold_ternary(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
|
|
|
const struct hlsl_ir_constant *src1, const struct hlsl_ir_constant *src2, const struct hlsl_ir_constant *src3)
|
|
|
|
{
|
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(dst_type->e.numeric.type == src2->node.data_type->e.numeric.type);
|
|
|
|
VKD3D_ASSERT(dst_type->e.numeric.type == src3->node.data_type->e.numeric.type);
|
|
|
|
VKD3D_ASSERT(src1->node.data_type->e.numeric.type == HLSL_TYPE_BOOL);
|
2023-09-12 08:17:29 -07:00
|
|
|
|
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
2024-03-01 11:01:03 -08:00
|
|
|
dst->u[k] = src1->value.u[k].u ? src2->value.u[k] : src3->value.u[k];
|
2023-09-12 08:17:29 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-09-23 09:31:55 -07:00
|
|
|
static bool fold_rshift(struct hlsl_ctx *ctx, struct hlsl_constant_value *dst, const struct hlsl_type *dst_type,
|
|
|
|
const struct hlsl_ir_constant *src1, const struct hlsl_ir_constant *src2)
|
|
|
|
{
|
|
|
|
unsigned int k;
|
|
|
|
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(dst_type->e.numeric.type == src1->node.data_type->e.numeric.type);
|
|
|
|
VKD3D_ASSERT(src2->node.data_type->e.numeric.type == HLSL_TYPE_INT);
|
2023-09-23 09:31:55 -07:00
|
|
|
|
|
|
|
for (k = 0; k < dst_type->dimx; ++k)
|
|
|
|
{
|
|
|
|
unsigned int shift = src2->value.u[k].u % 32;
|
|
|
|
|
2024-02-27 15:30:51 -08:00
|
|
|
switch (src1->node.data_type->e.numeric.type)
|
2023-09-23 09:31:55 -07:00
|
|
|
{
|
|
|
|
case HLSL_TYPE_INT:
|
|
|
|
dst->u[k].i = src1->value.u[k].i >> shift;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_UINT:
|
|
|
|
dst->u[k].u = src1->value.u[k].u >> shift;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
vkd3d_unreachable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-27 08:22:38 -07:00
|
|
|
bool hlsl_fold_constant_exprs(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context)
|
2022-02-10 19:48:19 -08:00
|
|
|
{
|
2023-06-18 14:19:02 -07:00
|
|
|
struct hlsl_ir_constant *arg1, *arg2 = NULL, *arg3 = NULL;
|
2022-11-11 17:10:14 -08:00
|
|
|
struct hlsl_constant_value res = {0};
|
2022-11-11 17:13:26 -08:00
|
|
|
struct hlsl_ir_node *res_node;
|
2022-02-10 19:48:19 -08:00
|
|
|
struct hlsl_ir_expr *expr;
|
2022-02-16 13:00:47 -08:00
|
|
|
unsigned int i;
|
|
|
|
bool success;
|
2022-02-10 19:48:19 -08:00
|
|
|
|
|
|
|
if (instr->type != HLSL_IR_EXPR)
|
|
|
|
return false;
|
|
|
|
expr = hlsl_ir_expr(instr);
|
2021-09-10 14:35:54 -07:00
|
|
|
if (!expr->operands[0].node)
|
|
|
|
return false;
|
2022-02-10 19:48:19 -08:00
|
|
|
|
2022-11-11 17:31:55 -08:00
|
|
|
if (instr->data_type->class > HLSL_CLASS_VECTOR)
|
2022-04-05 03:33:07 -07:00
|
|
|
return false;
|
|
|
|
|
2022-02-10 19:48:19 -08:00
|
|
|
for (i = 0; i < ARRAY_SIZE(expr->operands); ++i)
|
|
|
|
{
|
2022-04-05 03:33:07 -07:00
|
|
|
if (expr->operands[i].node)
|
|
|
|
{
|
|
|
|
if (expr->operands[i].node->type != HLSL_IR_CONSTANT)
|
|
|
|
return false;
|
2024-08-01 01:49:10 -07:00
|
|
|
VKD3D_ASSERT(expr->operands[i].node->data_type->class <= HLSL_CLASS_VECTOR);
|
2022-04-05 03:33:07 -07:00
|
|
|
}
|
2022-02-10 19:48:19 -08:00
|
|
|
}
|
|
|
|
arg1 = hlsl_ir_constant(expr->operands[0].node);
|
|
|
|
if (expr->operands[1].node)
|
|
|
|
arg2 = hlsl_ir_constant(expr->operands[1].node);
|
2023-06-18 14:19:02 -07:00
|
|
|
if (expr->operands[2].node)
|
|
|
|
arg3 = hlsl_ir_constant(expr->operands[2].node);
|
2022-02-10 19:48:19 -08:00
|
|
|
|
2022-02-16 13:00:47 -08:00
|
|
|
switch (expr->op)
|
2022-02-10 19:48:19 -08:00
|
|
|
{
|
2023-05-08 10:21:37 -07:00
|
|
|
case HLSL_OP1_ABS:
|
2022-11-11 17:10:14 -08:00
|
|
|
success = fold_abs(ctx, &res, instr->data_type, arg1);
|
2023-05-08 10:21:37 -07:00
|
|
|
break;
|
|
|
|
|
2023-09-23 10:21:42 -07:00
|
|
|
case HLSL_OP1_BIT_NOT:
|
|
|
|
success = fold_bit_not(ctx, &res, instr->data_type, arg1);
|
|
|
|
break;
|
|
|
|
|
2022-02-16 13:00:47 -08:00
|
|
|
case HLSL_OP1_CAST:
|
2022-11-11 17:10:14 -08:00
|
|
|
success = fold_cast(ctx, &res, instr->data_type, arg1);
|
2022-02-10 19:48:19 -08:00
|
|
|
break;
|
|
|
|
|
2023-10-17 14:57:46 -07:00
|
|
|
case HLSL_OP1_CEIL:
|
|
|
|
success = fold_ceil(ctx, &res, instr->data_type, arg1);
|
|
|
|
break;
|
|
|
|
|
2023-09-12 09:34:27 -07:00
|
|
|
case HLSL_OP1_EXP2:
|
|
|
|
success = fold_exp2(ctx, &res, instr->data_type, arg1);
|
|
|
|
break;
|
|
|
|
|
2023-10-17 15:08:02 -07:00
|
|
|
case HLSL_OP1_FLOOR:
|
|
|
|
success = fold_floor(ctx, &res, instr->data_type, arg1);
|
|
|
|
break;
|
|
|
|
|
2023-09-12 09:31:03 -07:00
|
|
|
case HLSL_OP1_FRACT:
|
|
|
|
success = fold_fract(ctx, &res, instr->data_type, arg1);
|
|
|
|
break;
|
|
|
|
|
2023-07-05 02:09:21 -07:00
|
|
|
case HLSL_OP1_LOG2:
|
|
|
|
success = fold_log2(ctx, &res, instr->data_type, arg1, &instr->loc);
|
|
|
|
break;
|
|
|
|
|
2022-02-16 13:00:47 -08:00
|
|
|
case HLSL_OP1_NEG:
|
2022-11-11 17:10:14 -08:00
|
|
|
success = fold_neg(ctx, &res, instr->data_type, arg1);
|
2022-02-16 13:00:47 -08:00
|
|
|
break;
|
|
|
|
|
2023-09-23 09:18:07 -07:00
|
|
|
case HLSL_OP1_LOGIC_NOT:
|
|
|
|
success = fold_not(ctx, &res, instr->data_type, arg1);
|
|
|
|
break;
|
|
|
|
|
2023-06-27 12:18:11 -07:00
|
|
|
case HLSL_OP1_RCP:
|
|
|
|
success = fold_rcp(ctx, &res, instr->data_type, arg1, &instr->loc);
|
|
|
|
break;
|
|
|
|
|
2023-09-26 05:32:24 -07:00
|
|
|
case HLSL_OP1_RSQ:
|
|
|
|
success = fold_rsq(ctx, &res, instr->data_type, arg1, &instr->loc);
|
|
|
|
break;
|
|
|
|
|
2023-09-12 09:38:00 -07:00
|
|
|
case HLSL_OP1_SAT:
|
|
|
|
success = fold_sat(ctx, &res, instr->data_type, arg1);
|
|
|
|
break;
|
|
|
|
|
2023-07-05 01:39:04 -07:00
|
|
|
case HLSL_OP1_SQRT:
|
|
|
|
success = fold_sqrt(ctx, &res, instr->data_type, arg1, &instr->loc);
|
|
|
|
break;
|
|
|
|
|
2022-02-16 13:00:47 -08:00
|
|
|
case HLSL_OP2_ADD:
|
2022-11-11 17:10:14 -08:00
|
|
|
success = fold_add(ctx, &res, instr->data_type, arg1, arg2);
|
2022-02-16 13:00:47 -08:00
|
|
|
break;
|
|
|
|
|
2023-06-09 13:21:01 -07:00
|
|
|
case HLSL_OP2_BIT_AND:
|
2023-06-09 13:39:25 -07:00
|
|
|
case HLSL_OP2_LOGIC_AND:
|
|
|
|
success = fold_and(ctx, &res, instr->data_type, arg1, arg2);
|
2022-02-10 19:48:19 -08:00
|
|
|
break;
|
|
|
|
|
2023-06-09 13:21:01 -07:00
|
|
|
case HLSL_OP2_BIT_OR:
|
2023-06-09 13:39:25 -07:00
|
|
|
case HLSL_OP2_LOGIC_OR:
|
|
|
|
success = fold_or(ctx, &res, instr->data_type, arg1, arg2);
|
2022-04-05 03:33:13 -07:00
|
|
|
break;
|
|
|
|
|
2023-06-09 13:21:01 -07:00
|
|
|
case HLSL_OP2_BIT_XOR:
|
|
|
|
success = fold_bit_xor(ctx, &res, instr->data_type, arg1, arg2);
|
2022-04-14 03:52:39 -07:00
|
|
|
break;
|
|
|
|
|
2023-06-18 11:46:51 -07:00
|
|
|
case HLSL_OP2_DOT:
|
|
|
|
success = fold_dot(ctx, &res, instr->data_type, arg1, arg2);
|
|
|
|
break;
|
|
|
|
|
2023-06-09 13:21:01 -07:00
|
|
|
case HLSL_OP2_DIV:
|
|
|
|
success = fold_div(ctx, &res, instr->data_type, arg1, arg2, &instr->loc);
|
2022-04-14 03:52:40 -07:00
|
|
|
break;
|
|
|
|
|
2023-06-09 13:23:40 -07:00
|
|
|
case HLSL_OP2_EQUAL:
|
|
|
|
success = fold_equal(ctx, &res, instr->data_type, arg1, arg2);
|
2023-06-09 13:27:03 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_OP2_GEQUAL:
|
|
|
|
success = fold_gequal(ctx, &res, instr->data_type, arg1, arg2);
|
2023-06-09 13:23:40 -07:00
|
|
|
break;
|
|
|
|
|
2023-06-09 13:25:51 -07:00
|
|
|
case HLSL_OP2_LESS:
|
|
|
|
success = fold_less(ctx, &res, instr->data_type, arg1, arg2);
|
|
|
|
break;
|
|
|
|
|
2023-09-12 22:29:10 -07:00
|
|
|
case HLSL_OP2_LSHIFT:
|
|
|
|
success = fold_lshift(ctx, &res, instr->data_type, arg1, arg2);
|
|
|
|
break;
|
|
|
|
|
2022-06-27 08:22:39 -07:00
|
|
|
case HLSL_OP2_MAX:
|
2022-11-11 17:10:14 -08:00
|
|
|
success = fold_max(ctx, &res, instr->data_type, arg1, arg2);
|
2022-06-27 08:22:39 -07:00
|
|
|
break;
|
|
|
|
|
2022-06-27 08:22:40 -07:00
|
|
|
case HLSL_OP2_MIN:
|
2022-11-11 17:10:14 -08:00
|
|
|
success = fold_min(ctx, &res, instr->data_type, arg1, arg2);
|
2022-06-27 08:22:40 -07:00
|
|
|
break;
|
|
|
|
|
2023-06-09 13:21:01 -07:00
|
|
|
case HLSL_OP2_MOD:
|
|
|
|
success = fold_mod(ctx, &res, instr->data_type, arg1, arg2, &instr->loc);
|
2022-06-27 08:22:41 -07:00
|
|
|
break;
|
|
|
|
|
2023-06-09 13:21:01 -07:00
|
|
|
case HLSL_OP2_MUL:
|
|
|
|
success = fold_mul(ctx, &res, instr->data_type, arg1, arg2);
|
2022-06-27 08:22:42 -07:00
|
|
|
break;
|
|
|
|
|
2023-06-09 13:21:01 -07:00
|
|
|
case HLSL_OP2_NEQUAL:
|
|
|
|
success = fold_nequal(ctx, &res, instr->data_type, arg1, arg2);
|
2022-06-27 08:22:43 -07:00
|
|
|
break;
|
|
|
|
|
2023-09-23 09:31:55 -07:00
|
|
|
case HLSL_OP2_RSHIFT:
|
|
|
|
success = fold_rshift(ctx, &res, instr->data_type, arg1, arg2);
|
|
|
|
break;
|
|
|
|
|
2023-06-18 14:19:02 -07:00
|
|
|
case HLSL_OP3_DP2ADD:
|
|
|
|
success = fold_dp2add(ctx, &res, instr->data_type, arg1, arg2, arg3);
|
|
|
|
break;
|
|
|
|
|
2023-09-12 08:17:29 -07:00
|
|
|
case HLSL_OP3_TERNARY:
|
|
|
|
success = fold_ternary(ctx, &res, instr->data_type, arg1, arg2, arg3);
|
|
|
|
break;
|
|
|
|
|
2022-02-10 19:48:19 -08:00
|
|
|
default:
|
2022-02-16 13:00:47 -08:00
|
|
|
FIXME("Fold \"%s\" expression.\n", debug_hlsl_expr_op(expr->op));
|
|
|
|
success = false;
|
|
|
|
break;
|
2022-02-10 19:48:19 -08:00
|
|
|
}
|
|
|
|
|
2022-02-16 13:00:47 -08:00
|
|
|
if (success)
|
|
|
|
{
|
2022-11-11 17:10:14 -08:00
|
|
|
if (!(res_node = hlsl_new_constant(ctx, instr->data_type, &res, &instr->loc)))
|
|
|
|
return false;
|
2022-11-11 17:13:26 -08:00
|
|
|
list_add_before(&expr->node.entry, &res_node->entry);
|
|
|
|
hlsl_replace_node(&expr->node, res_node);
|
2022-02-16 13:00:47 -08:00
|
|
|
}
|
|
|
|
return success;
|
2022-02-10 19:48:19 -08:00
|
|
|
}
|
2022-06-27 08:22:38 -07:00
|
|
|
|
2024-04-19 09:57:38 -07:00
|
|
|
static bool constant_is_zero(struct hlsl_ir_constant *const_arg)
|
|
|
|
{
|
|
|
|
struct hlsl_type *data_type = const_arg->node.data_type;
|
|
|
|
unsigned int k;
|
|
|
|
|
|
|
|
for (k = 0; k < data_type->dimx; ++k)
|
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
switch (data_type->e.numeric.type)
|
2024-04-19 09:57:38 -07:00
|
|
|
{
|
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
|
|
|
if (const_arg->value.u[k].f != 0.0f)
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_DOUBLE:
|
|
|
|
if (const_arg->value.u[k].d != 0.0)
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_UINT:
|
|
|
|
case HLSL_TYPE_INT:
|
|
|
|
case HLSL_TYPE_BOOL:
|
|
|
|
if (const_arg->value.u[k].u != 0)
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-04-19 10:02:37 -07:00
|
|
|
static bool constant_is_one(struct hlsl_ir_constant *const_arg)
|
|
|
|
{
|
|
|
|
struct hlsl_type *data_type = const_arg->node.data_type;
|
|
|
|
unsigned int k;
|
|
|
|
|
|
|
|
for (k = 0; k < data_type->dimx; ++k)
|
|
|
|
{
|
2024-02-27 15:30:51 -08:00
|
|
|
switch (data_type->e.numeric.type)
|
2024-04-19 10:02:37 -07:00
|
|
|
{
|
|
|
|
case HLSL_TYPE_FLOAT:
|
|
|
|
case HLSL_TYPE_HALF:
|
|
|
|
if (const_arg->value.u[k].f != 1.0f)
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_DOUBLE:
|
|
|
|
if (const_arg->value.u[k].d != 1.0)
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HLSL_TYPE_UINT:
|
|
|
|
case HLSL_TYPE_INT:
|
|
|
|
case HLSL_TYPE_BOOL:
|
|
|
|
if (const_arg->value.u[k].u != 1)
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-04-19 09:57:38 -07:00
|
|
|
bool hlsl_fold_constant_identities(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context)
|
|
|
|
{
|
|
|
|
struct hlsl_ir_constant *const_arg = NULL;
|
|
|
|
struct hlsl_ir_node *mut_arg = NULL;
|
|
|
|
struct hlsl_ir_node *res_node;
|
|
|
|
struct hlsl_ir_expr *expr;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (instr->type != HLSL_IR_EXPR)
|
|
|
|
return false;
|
|
|
|
expr = hlsl_ir_expr(instr);
|
|
|
|
|
|
|
|
if (instr->data_type->class > HLSL_CLASS_VECTOR)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Verify that the expression has two operands. */
|
|
|
|
for (i = 0; i < ARRAY_SIZE(expr->operands); ++i)
|
|
|
|
{
|
|
|
|
if (!!expr->operands[i].node != (i < 2))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expr->operands[0].node->type == HLSL_IR_CONSTANT)
|
|
|
|
{
|
|
|
|
const_arg = hlsl_ir_constant(expr->operands[0].node);
|
|
|
|
mut_arg = expr->operands[1].node;
|
|
|
|
}
|
|
|
|
else if (expr->operands[1].node->type == HLSL_IR_CONSTANT)
|
|
|
|
{
|
|
|
|
mut_arg = expr->operands[0].node;
|
|
|
|
const_arg = hlsl_ir_constant(expr->operands[1].node);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
res_node = NULL;
|
|
|
|
switch (expr->op)
|
|
|
|
{
|
|
|
|
case HLSL_OP2_ADD:
|
|
|
|
if (constant_is_zero(const_arg))
|
|
|
|
res_node = mut_arg;
|
|
|
|
break;
|
2024-04-19 10:02:37 -07:00
|
|
|
|
|
|
|
case HLSL_OP2_MUL:
|
|
|
|
if (constant_is_one(const_arg))
|
|
|
|
res_node = mut_arg;
|
|
|
|
break;
|
2024-04-19 09:57:38 -07:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res_node)
|
|
|
|
{
|
|
|
|
hlsl_replace_node(&expr->node, res_node);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-27 08:22:38 -07:00
|
|
|
bool hlsl_fold_constant_swizzles(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context)
|
|
|
|
{
|
2022-11-11 17:10:14 -08:00
|
|
|
struct hlsl_constant_value value;
|
2022-06-27 08:22:38 -07:00
|
|
|
struct hlsl_ir_swizzle *swizzle;
|
2022-11-11 17:13:26 -08:00
|
|
|
struct hlsl_ir_constant *src;
|
|
|
|
struct hlsl_ir_node *dst;
|
2023-01-12 13:52:49 -08:00
|
|
|
unsigned int i;
|
2022-06-27 08:22:38 -07:00
|
|
|
|
|
|
|
if (instr->type != HLSL_IR_SWIZZLE)
|
|
|
|
return false;
|
|
|
|
swizzle = hlsl_ir_swizzle(instr);
|
|
|
|
if (swizzle->val.node->type != HLSL_IR_CONSTANT)
|
|
|
|
return false;
|
2022-11-11 17:10:14 -08:00
|
|
|
src = hlsl_ir_constant(swizzle->val.node);
|
2022-06-27 08:22:38 -07:00
|
|
|
|
|
|
|
for (i = 0; i < swizzle->node.data_type->dimx; ++i)
|
2022-11-11 17:10:14 -08:00
|
|
|
value.u[i] = src->value.u[hlsl_swizzle_get_component(swizzle->swizzle, i)];
|
|
|
|
|
|
|
|
if (!(dst = hlsl_new_constant(ctx, instr->data_type, &value, &instr->loc)))
|
|
|
|
return false;
|
2022-06-27 08:22:38 -07:00
|
|
|
|
2022-11-11 17:13:26 -08:00
|
|
|
list_add_before(&swizzle->node.entry, &dst->entry);
|
|
|
|
hlsl_replace_node(&swizzle->node, dst);
|
2022-06-27 08:22:38 -07:00
|
|
|
return true;
|
|
|
|
}
|