vkd3d-shader/hlsl: Define lit() in HLSL.

This commit is contained in:
Zebediah Figura 2023-08-07 17:51:35 -05:00 committed by Alexandre Julliard
parent 9ab77658f2
commit a597dc8755
Notes: Alexandre Julliard 2023-08-30 23:19:43 +02:00
Approved-by: Giovanni Mascellani (@giomasce)
Approved-by: Henri Verbeet (@hverbeet)
Approved-by: Alexandre Julliard (@julliard)
Merge-Request: https://gitlab.winehq.org/wine/vkd3d/-/merge_requests/310
2 changed files with 24 additions and 70 deletions

View File

@ -3034,14 +3034,17 @@ static struct hlsl_ir_node * add_pow_expr(struct hlsl_ctx *ctx,
static bool intrinsic_lit(struct hlsl_ctx *ctx, static bool intrinsic_lit(struct hlsl_ctx *ctx,
const struct parse_initializer *params, const struct vkd3d_shader_location *loc) const struct parse_initializer *params, const struct vkd3d_shader_location *loc)
{ {
struct hlsl_ir_node *n_l_neg, *n_h_neg, *specular_or, *specular_pow, *load; struct hlsl_ir_function_decl *func;
struct hlsl_ir_node *n_l, *n_h, *m, *diffuse, *zero, *store, *init;
struct hlsl_constant_value init_value; static const char body[] =
struct hlsl_ir_load *var_load; "float4 lit(float n_l, float n_h, float m)\n"
struct hlsl_deref var_deref; "{\n"
struct hlsl_type *ret_type; " float4 ret;\n"
struct hlsl_ir_var *var; " ret.xw = 1.0;\n"
struct hlsl_block block; " ret.y = max(n_l, 0);\n"
" ret.z = (n_l < 0 || n_h < 0) ? 0 : pow(n_h, m);\n"
" return ret;\n"
"}";
if (params->args[0]->data_type->class != HLSL_CLASS_SCALAR if (params->args[0]->data_type->class != HLSL_CLASS_SCALAR
|| params->args[1]->data_type->class != HLSL_CLASS_SCALAR || params->args[1]->data_type->class != HLSL_CLASS_SCALAR
@ -3051,70 +3054,10 @@ static bool intrinsic_lit(struct hlsl_ctx *ctx,
return false; return false;
} }
if (!(n_l = intrinsic_float_convert_arg(ctx, params, params->args[0], loc))) if (!(func = hlsl_compile_internal_function(ctx, "lit", body)))
return false; return false;
if (!(n_h = intrinsic_float_convert_arg(ctx, params, params->args[1], loc))) return add_user_call(ctx, func, params, loc);
return false;
if (!(m = intrinsic_float_convert_arg(ctx, params, params->args[2], loc)))
return false;
ret_type = hlsl_get_vector_type(ctx, HLSL_TYPE_FLOAT, 4);
if (!(var = hlsl_new_synthetic_var(ctx, "lit", ret_type, loc)))
return false;
hlsl_init_simple_deref_from_var(&var_deref, var);
init_value.u[0].f = 1.0f;
init_value.u[1].f = 0.0f;
init_value.u[2].f = 0.0f;
init_value.u[3].f = 1.0f;
if (!(init = hlsl_new_constant(ctx, ret_type, &init_value, loc)))
return false;
hlsl_block_add_instr(params->instrs, init);
if (!(store = hlsl_new_simple_store(ctx, var, init)))
return false;
hlsl_block_add_instr(params->instrs, store);
if (!(zero = hlsl_new_float_constant(ctx, 0.0f, loc)))
return false;
hlsl_block_add_instr(params->instrs, zero);
/* Diffuse component. */
if (!(diffuse = add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_MAX, n_l, zero, loc)))
return false;
if (!hlsl_new_store_component(ctx, &block, &var_deref, 1, diffuse))
return false;
hlsl_block_add_block(params->instrs, &block);
/* Specular component. */
if (!(n_h_neg = add_binary_comparison_expr(ctx, params->instrs, HLSL_OP2_LESS, n_h, zero, loc)))
return false;
if (!(n_l_neg = add_binary_comparison_expr(ctx, params->instrs, HLSL_OP2_LESS, n_l, zero, loc)))
return false;
if (!(specular_or = add_binary_logical_expr(ctx, params->instrs, HLSL_OP2_LOGIC_OR, n_l_neg, n_h_neg, loc)))
return false;
if (!(specular_pow = add_pow_expr(ctx, params->instrs, n_h, m, loc)))
return false;
if (!(load = hlsl_add_conditional(ctx, params->instrs, specular_or, zero, specular_pow)))
return false;
if (!hlsl_new_store_component(ctx, &block, &var_deref, 2, load))
return false;
hlsl_block_add_block(params->instrs, &block);
if (!(var_load = hlsl_new_var_load(ctx, var, loc)))
return false;
hlsl_block_add_instr(params->instrs, &var_load->node);
return true;
} }
static bool intrinsic_log(struct hlsl_ctx *ctx, static bool intrinsic_log(struct hlsl_ctx *ctx,

View File

@ -19,6 +19,17 @@ uniform 0 float4 1.2 2.0 3.0 0.0
draw quad draw quad
probe all rgba (1.0, 1.2, 8.0, 1.0) probe all rgba (1.0, 1.2, 8.0, 1.0)
[pixel shader]
float4 main(uniform float4 u) : sv_target
{
return lit(u.x, u.y, u.z) + lit(u.x, u.y, u.z);
}
[test]
uniform 0 float4 1.2 2.0 3.0 0.0
draw quad
probe all rgba (2.0, 2.4, 16.0, 2.0)
[pixel shader fail] [pixel shader fail]
float4 main(uniform float4 u) : sv_target float4 main(uniform float4 u) : sv_target
{ {