vkd3d-shader/hlsl: Implement the firstbitlow() intrinsic.

This commit is contained in:
Petrichor Park
2024-08-13 11:34:23 -05:00
committed by Henri Verbeet
parent e6d840170d
commit e35604dbf0
Notes: Henri Verbeet 2025-09-22 11:46:20 +02:00
Approved-by: Francisco Casas (@fcasas)
Approved-by: Elizabeth Figura (@zfigura)
Approved-by: Henri Verbeet (@hverbeet)
Merge-Request: https://gitlab.winehq.org/wine/vkd3d/-/merge_requests/965
9 changed files with 135 additions and 4 deletions

View File

@@ -11077,6 +11077,12 @@ static bool sm4_generate_vsir_instr_expr(struct hlsl_ctx *ctx,
generate_vsir_instr_expr_single_instr_op(ctx, program, expr, VSIR_OP_COUNTBITS, 0, 0, true);
return true;
case HLSL_OP1_CTZ:
VKD3D_ASSERT(hlsl_type_is_integer(dst_type));
VKD3D_ASSERT(hlsl_version_ge(ctx, 5, 0));
generate_vsir_instr_expr_single_instr_op(ctx, program, expr, VSIR_OP_FIRSTBIT_LO, 0, 0, true);
return true;
case HLSL_OP1_DSX:
VKD3D_ASSERT(type_is_float(dst_type));
generate_vsir_instr_expr_single_instr_op(ctx, program, expr, VSIR_OP_DSX, 0, 0, true);
@@ -14157,6 +14163,57 @@ static bool lower_countbits(struct hlsl_ctx *ctx, struct hlsl_ir_node *node, str
return true;
}
static bool lower_ctz(struct hlsl_ctx *ctx, struct hlsl_ir_node *node, struct hlsl_block *block)
{
struct hlsl_ir_function_decl *func;
struct hlsl_ir_node *call, *rhs;
struct hlsl_ir_expr *expr;
struct hlsl_ir_var *lhs;
char *body;
/* ctz() returns the bit number of the least significant 1-bit.
* Bit numbers count from the least significant bit. */
static const char template[] =
"typedef uint%u uintX;\n"
"uintX ctz(uintX v)\n"
"{\n"
" uintX c = 31;\n"
" v &= -v;\n"
" c = (v & 0x0000ffff) ? c - 16 : c;\n"
" c = (v & 0x00ff00ff) ? c - 8 : c;\n"
" c = (v & 0x0f0f0f0f) ? c - 4 : c;\n"
" c = (v & 0x33333333) ? c - 2 : c;\n"
" c = (v & 0x55555555) ? c - 1 : c;\n"
" return v ? c : -1;\n"
"}\n";
if (node->type != HLSL_IR_EXPR)
return false;
expr = hlsl_ir_expr(node);
if (expr->op != HLSL_OP1_CTZ)
return false;
rhs = expr->operands[0].node;
if (!(body = hlsl_sprintf_alloc(ctx, template, hlsl_type_component_count(rhs->data_type))))
return false;
func = hlsl_compile_internal_function(ctx, "ctz", body);
vkd3d_free(body);
if (!func)
return false;
lhs = func->parameters.vars[0];
hlsl_block_add_simple_store(ctx, block, lhs, rhs);
if (!(call = hlsl_new_call(ctx, func, &node->loc)))
return false;
hlsl_block_add_instr(block, call);
hlsl_block_add_simple_load(ctx, block, func->return_var, &node->loc);
return true;
}
static bool lower_f16tof32(struct hlsl_ctx *ctx, struct hlsl_ir_node *node, struct hlsl_block *block)
{
struct hlsl_ir_function_decl *func;
@@ -14479,6 +14536,7 @@ static void process_entry_function(struct hlsl_ctx *ctx, struct list *semantic_v
if (hlsl_version_ge(ctx, 4, 0) && hlsl_version_lt(ctx, 5, 0))
{
lower_ir(ctx, lower_countbits, body);
lower_ir(ctx, lower_ctz, body);
lower_ir(ctx, lower_f16tof32, body);
lower_ir(ctx, lower_f32tof16, body);
lower_ir(ctx, lower_find_msb, body);