From 220362cbadfff0d28cef8190565110d29ce3a662 Mon Sep 17 00:00:00 2001 From: Zebediah Figura Date: Mon, 5 Feb 2024 20:13:17 -0600 Subject: [PATCH] vkd3d-shader/hlsl: Make HLSL_TYPE_SAMPLER into a separate class. --- libs/vkd3d-shader/d3dbc.c | 38 +++++++------- libs/vkd3d-shader/fx.c | 29 +++++++---- libs/vkd3d-shader/hlsl.c | 87 +++++++++++++++++++------------- libs/vkd3d-shader/hlsl.h | 5 +- libs/vkd3d-shader/hlsl.y | 17 +++---- libs/vkd3d-shader/hlsl_codegen.c | 3 +- libs/vkd3d-shader/tpf.c | 29 +++++++---- 7 files changed, 120 insertions(+), 88 deletions(-) diff --git a/libs/vkd3d-shader/d3dbc.c b/libs/vkd3d-shader/d3dbc.c index 61f0f5cb..e6cce48a 100644 --- a/libs/vkd3d-shader/d3dbc.c +++ b/libs/vkd3d-shader/d3dbc.c @@ -1513,6 +1513,7 @@ D3DXPARAMETER_CLASS hlsl_sm1_class(const struct hlsl_type *type) case HLSL_CLASS_VECTOR: return D3DXPC_VECTOR; case HLSL_CLASS_OBJECT: + case HLSL_CLASS_SAMPLER: case HLSL_CLASS_STRING: return D3DXPC_OBJECT; case HLSL_CLASS_VOID: @@ -1552,29 +1553,30 @@ D3DXPARAMETER_TYPE hlsl_sm1_base_type(const struct hlsl_type *type) vkd3d_unreachable(); } + case HLSL_CLASS_SAMPLER: + switch (type->sampler_dim) + { + case HLSL_SAMPLER_DIM_1D: + return D3DXPT_SAMPLER1D; + case HLSL_SAMPLER_DIM_2D: + return D3DXPT_SAMPLER2D; + case HLSL_SAMPLER_DIM_3D: + return D3DXPT_SAMPLER3D; + case HLSL_SAMPLER_DIM_CUBE: + return D3DXPT_SAMPLERCUBE; + case HLSL_SAMPLER_DIM_GENERIC: + return D3DXPT_SAMPLER; + default: + ERR("Invalid dimension %#x.\n", type->sampler_dim); + vkd3d_unreachable(); + } + break; + case HLSL_CLASS_OBJECT: switch (type->base_type) { case HLSL_TYPE_PIXELSHADER: return D3DXPT_PIXELSHADER; - case HLSL_TYPE_SAMPLER: - switch (type->sampler_dim) - { - case HLSL_SAMPLER_DIM_1D: - return D3DXPT_SAMPLER1D; - case HLSL_SAMPLER_DIM_2D: - return D3DXPT_SAMPLER2D; - case HLSL_SAMPLER_DIM_3D: - return D3DXPT_SAMPLER3D; - case HLSL_SAMPLER_DIM_CUBE: - return D3DXPT_SAMPLERCUBE; - case HLSL_SAMPLER_DIM_GENERIC: - return D3DXPT_SAMPLER; - default: - ERR("Invalid dimension %#x.\n", type->sampler_dim); - vkd3d_unreachable(); - } - break; case HLSL_TYPE_TEXTURE: switch (type->sampler_dim) { diff --git a/libs/vkd3d-shader/fx.c b/libs/vkd3d-shader/fx.c index e5770f10..c5caeadf 100644 --- a/libs/vkd3d-shader/fx.c +++ b/libs/vkd3d-shader/fx.c @@ -437,6 +437,7 @@ static uint32_t write_fx_4_type(const struct hlsl_type *type, struct fx_write_co case HLSL_CLASS_ARRAY: vkd3d_unreachable(); + case HLSL_CLASS_SAMPLER: case HLSL_CLASS_STRING: case HLSL_CLASS_VOID: FIXME("Writing type class %u is not implemented.\n", type->class); @@ -810,7 +811,6 @@ static bool is_type_supported_fx_2(struct hlsl_ctx *ctx, const struct hlsl_type } break; - case HLSL_TYPE_SAMPLER: case HLSL_TYPE_PIXELSHADER: case HLSL_TYPE_VERTEXSHADER: hlsl_fixme(ctx, loc, "Write fx 2.0 parameter object type %#x.", type->base_type); @@ -820,6 +820,7 @@ static bool is_type_supported_fx_2(struct hlsl_ctx *ctx, const struct hlsl_type return false; } + case HLSL_CLASS_SAMPLER: case HLSL_CLASS_STRING: hlsl_fixme(ctx, loc, "Write fx 2.0 parameter class %#x.", type->class); return false; @@ -1097,18 +1098,24 @@ static bool is_object_variable(const struct hlsl_ir_var *var) { const struct hlsl_type *type = hlsl_get_multiarray_element_type(var->data_type); - if (type->class != HLSL_CLASS_OBJECT) - return false; - - switch (type->base_type) + switch (type->class) { - case HLSL_TYPE_SAMPLER: - case HLSL_TYPE_TEXTURE: - case HLSL_TYPE_UAV: - case HLSL_TYPE_PIXELSHADER: - case HLSL_TYPE_VERTEXSHADER: - case HLSL_TYPE_RENDERTARGETVIEW: + case HLSL_CLASS_SAMPLER: return true; + + case HLSL_CLASS_OBJECT: + switch (type->base_type) + { + case HLSL_TYPE_TEXTURE: + case HLSL_TYPE_UAV: + case HLSL_TYPE_PIXELSHADER: + case HLSL_TYPE_VERTEXSHADER: + case HLSL_TYPE_RENDERTARGETVIEW: + return true; + default: + return false; + } + default: return false; } diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index c3170ff7..d1353ead 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -226,50 +226,61 @@ unsigned int hlsl_get_multiarray_size(const struct hlsl_type *type) bool hlsl_type_is_resource(const struct hlsl_type *type) { - if (type->class == HLSL_CLASS_ARRAY) - return hlsl_type_is_resource(type->e.array.type); - - if (type->class == HLSL_CLASS_OBJECT) + switch (type->class) { - switch (type->base_type) - { - case HLSL_TYPE_TEXTURE: - case HLSL_TYPE_SAMPLER: - case HLSL_TYPE_UAV: - return true; - default: - return false; - } + case HLSL_CLASS_ARRAY: + return hlsl_type_is_resource(type->e.array.type); + + case HLSL_CLASS_SAMPLER: + return true; + + case HLSL_CLASS_OBJECT: + switch (type->base_type) + { + case HLSL_TYPE_TEXTURE: + case HLSL_TYPE_UAV: + return true; + default: + return false; + } + + default: + return false; } - return false; } /* Only intended to be used for derefs (after copies have been lowered to components or vectors) or * resources, since for both their data types span across a single regset. */ static enum hlsl_regset type_get_regset(const struct hlsl_type *type) { - if (hlsl_is_numeric_type(type)) - return HLSL_REGSET_NUMERIC; - - if (type->class == HLSL_CLASS_ARRAY) - return type_get_regset(type->e.array.type); - - if (type->class == HLSL_CLASS_OBJECT) + switch (type->class) { - switch (type->base_type) - { - case HLSL_TYPE_TEXTURE: - return HLSL_REGSET_TEXTURES; + case HLSL_CLASS_SCALAR: + case HLSL_CLASS_VECTOR: + case HLSL_CLASS_MATRIX: + return HLSL_REGSET_NUMERIC; - case HLSL_TYPE_SAMPLER: - return HLSL_REGSET_SAMPLERS; + case HLSL_CLASS_ARRAY: + return type_get_regset(type->e.array.type); - case HLSL_TYPE_UAV: - return HLSL_REGSET_UAVS; + case HLSL_CLASS_SAMPLER: + return HLSL_REGSET_SAMPLERS; - default: - vkd3d_unreachable(); - } + case HLSL_CLASS_OBJECT: + switch (type->base_type) + { + case HLSL_TYPE_TEXTURE: + return HLSL_REGSET_TEXTURES; + + case HLSL_TYPE_UAV: + return HLSL_REGSET_UAVS; + + default: + vkd3d_unreachable(); + } + + default: + break; } vkd3d_unreachable(); @@ -366,6 +377,10 @@ static void hlsl_type_calculate_reg_size(struct hlsl_ctx *ctx, struct hlsl_type break; } + case HLSL_CLASS_SAMPLER: + type->reg_size[HLSL_REGSET_SAMPLERS] = 1; + break; + case HLSL_CLASS_STRING: case HLSL_CLASS_VOID: break; @@ -429,6 +444,7 @@ static bool type_is_single_component(const struct hlsl_type *type) { case HLSL_CLASS_SCALAR: case HLSL_CLASS_OBJECT: + case HLSL_CLASS_SAMPLER: case HLSL_CLASS_STRING: return true; @@ -561,6 +577,7 @@ unsigned int hlsl_type_get_component_offset(struct hlsl_ctx *ctx, struct hlsl_ty break; case HLSL_CLASS_OBJECT: + case HLSL_CLASS_SAMPLER: case HLSL_CLASS_STRING: assert(idx == 0); break; @@ -933,6 +950,7 @@ unsigned int hlsl_type_component_count(const struct hlsl_type *type) return hlsl_type_component_count(type->e.array.type) * type->e.array.elements_count; case HLSL_CLASS_OBJECT: + case HLSL_CLASS_SAMPLER: case HLSL_CLASS_STRING: return 1; @@ -952,7 +970,7 @@ bool hlsl_types_are_equal(const struct hlsl_type *t1, const struct hlsl_type *t2 return false; if (t1->base_type != t2->base_type) return false; - if (t1->base_type == HLSL_TYPE_SAMPLER || t1->base_type == HLSL_TYPE_TEXTURE + if (t1->class == HLSL_CLASS_SAMPLER || t1->base_type == HLSL_TYPE_TEXTURE || t1->base_type == HLSL_TYPE_UAV) { if (t1->sampler_dim != t2->sampler_dim) @@ -2359,6 +2377,7 @@ struct vkd3d_string_buffer *hlsl_type_to_string(struct hlsl_ctx *ctx, const stru } } + case HLSL_CLASS_SAMPLER: case HLSL_CLASS_STRING: case HLSL_CLASS_VOID: break; @@ -3647,7 +3666,7 @@ static void declare_predefined_types(struct hlsl_ctx *ctx) for (bt = 0; bt <= HLSL_SAMPLER_DIM_LAST_SAMPLER; ++bt) { - type = hlsl_new_type(ctx, sampler_names[bt], HLSL_CLASS_OBJECT, HLSL_TYPE_SAMPLER, 1, 1); + type = hlsl_new_simple_type(ctx, sampler_names[bt], HLSL_CLASS_SAMPLER); type->sampler_dim = bt; ctx->builtin_types.sampler[bt] = type; } diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index 83679767..110a0f8e 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -79,6 +79,7 @@ enum hlsl_type_class HLSL_CLASS_STRUCT, HLSL_CLASS_ARRAY, HLSL_CLASS_OBJECT, + HLSL_CLASS_SAMPLER, HLSL_CLASS_STRING, HLSL_CLASS_VOID, }; @@ -92,7 +93,6 @@ enum hlsl_base_type HLSL_TYPE_UINT, HLSL_TYPE_BOOL, HLSL_TYPE_LAST_SCALAR = HLSL_TYPE_BOOL, - HLSL_TYPE_SAMPLER, HLSL_TYPE_TEXTURE, HLSL_TYPE_UAV, HLSL_TYPE_PIXELSHADER, @@ -150,7 +150,7 @@ struct hlsl_type * Otherwise, base_type is not used. */ enum hlsl_base_type base_type; - /* If base_type is HLSL_TYPE_SAMPLER, then sampler_dim is <= HLSL_SAMPLER_DIM_LAST_SAMPLER. + /* If class is HLSL_CLASS_SAMPLER, then sampler_dim is <= HLSL_SAMPLER_DIM_LAST_SAMPLER. * If base_type is HLSL_TYPE_TEXTURE, then sampler_dim can be any value of the enum except * HLSL_SAMPLER_DIM_GENERIC and HLSL_SAMPLER_DIM_COMPARISON. * If base_type is HLSL_TYPE_UAV, then sampler_dim must be one of HLSL_SAMPLER_DIM_1D, @@ -172,7 +172,6 @@ struct hlsl_type * If type is HLSL_CLASS_ARRAY, then dimx and dimy have the same value as in the type of the array elements. * If type is HLSL_CLASS_STRUCT, then dimx is the sum of (dimx * dimy) of every component, and dimy = 1. * If type is HLSL_CLASS_OBJECT, dimx and dimy depend on the base_type: - * If base_type is HLSL_TYPE_SAMPLER, then both dimx = 1 and dimy = 1. * If base_type is HLSL_TYPE_TEXTURE, then dimx = 4 and dimy = 1. * If base_type is HLSL_TYPE_UAV, then dimx is the dimx of e.resource_format, and dimy = 1. */ diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index 8f715567..5461b792 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -4044,7 +4044,7 @@ static bool intrinsic_tex(struct hlsl_ctx *ctx, const struct parse_initializer * } sampler_type = params->args[0]->data_type; - if (sampler_type->class != HLSL_CLASS_OBJECT || sampler_type->base_type != HLSL_TYPE_SAMPLER + if (sampler_type->class != HLSL_CLASS_SAMPLER || (sampler_type->sampler_dim != dim && sampler_type->sampler_dim != HLSL_SAMPLER_DIM_GENERIC)) { struct vkd3d_string_buffer *string; @@ -4721,8 +4721,7 @@ static bool add_sample_method_call(struct hlsl_ctx *ctx, struct hlsl_block *bloc } sampler_type = params->args[0]->data_type; - if (sampler_type->class != HLSL_CLASS_OBJECT || sampler_type->base_type != HLSL_TYPE_SAMPLER - || sampler_type->sampler_dim != HLSL_SAMPLER_DIM_GENERIC) + if (sampler_type->class != HLSL_CLASS_SAMPLER || sampler_type->sampler_dim != HLSL_SAMPLER_DIM_GENERIC) { struct vkd3d_string_buffer *string; @@ -4786,8 +4785,7 @@ static bool add_sample_cmp_method_call(struct hlsl_ctx *ctx, struct hlsl_block * } sampler_type = params->args[0]->data_type; - if (sampler_type->class != HLSL_CLASS_OBJECT || sampler_type->base_type != HLSL_TYPE_SAMPLER - || sampler_type->sampler_dim != HLSL_SAMPLER_DIM_COMPARISON) + if (sampler_type->class != HLSL_CLASS_SAMPLER || sampler_type->sampler_dim != HLSL_SAMPLER_DIM_COMPARISON) { struct vkd3d_string_buffer *string; @@ -4897,8 +4895,7 @@ static bool add_gather_method_call(struct hlsl_ctx *ctx, struct hlsl_block *bloc } sampler_type = params->args[0]->data_type; - if (sampler_type->class != HLSL_CLASS_OBJECT || sampler_type->base_type != HLSL_TYPE_SAMPLER - || sampler_type->sampler_dim != HLSL_SAMPLER_DIM_GENERIC) + if (sampler_type->class != HLSL_CLASS_SAMPLER || sampler_type->sampler_dim != HLSL_SAMPLER_DIM_GENERIC) { struct vkd3d_string_buffer *string; @@ -5134,8 +5131,7 @@ static bool add_sample_lod_method_call(struct hlsl_ctx *ctx, struct hlsl_block * } sampler_type = params->args[0]->data_type; - if (sampler_type->class != HLSL_CLASS_OBJECT || sampler_type->base_type != HLSL_TYPE_SAMPLER - || sampler_type->sampler_dim != HLSL_SAMPLER_DIM_GENERIC) + if (sampler_type->class != HLSL_CLASS_SAMPLER || sampler_type->sampler_dim != HLSL_SAMPLER_DIM_GENERIC) { struct vkd3d_string_buffer *string; @@ -5197,8 +5193,7 @@ static bool add_sample_grad_method_call(struct hlsl_ctx *ctx, struct hlsl_block } sampler_type = params->args[0]->data_type; - if (sampler_type->class != HLSL_CLASS_OBJECT || sampler_type->base_type != HLSL_TYPE_SAMPLER - || sampler_type->sampler_dim != HLSL_SAMPLER_DIM_GENERIC) + if (sampler_type->class != HLSL_CLASS_SAMPLER || sampler_type->sampler_dim != HLSL_SAMPLER_DIM_GENERIC) { struct vkd3d_string_buffer *string; diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index e1064b99..092eb7f7 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -1625,6 +1625,7 @@ static bool copy_propagation_transform_load(struct hlsl_ctx *ctx, { case HLSL_CLASS_SCALAR: case HLSL_CLASS_VECTOR: + case HLSL_CLASS_SAMPLER: case HLSL_CLASS_OBJECT: break; @@ -2611,7 +2612,7 @@ static bool lower_combined_samples(struct hlsl_ctx *ctx, struct hlsl_ir_node *in hlsl_copy_deref(ctx, &load->sampler, &load->resource); load->resource.var = var; assert(hlsl_deref_get_type(ctx, &load->resource)->base_type == HLSL_TYPE_TEXTURE); - assert(hlsl_deref_get_type(ctx, &load->sampler)->base_type == HLSL_TYPE_SAMPLER); + assert(hlsl_deref_get_type(ctx, &load->sampler)->class == HLSL_CLASS_SAMPLER); return true; } diff --git a/libs/vkd3d-shader/tpf.c b/libs/vkd3d-shader/tpf.c index 4bacbefd..e9f9ba40 100644 --- a/libs/vkd3d-shader/tpf.c +++ b/libs/vkd3d-shader/tpf.c @@ -3008,6 +3008,7 @@ static D3D_SHADER_VARIABLE_CLASS sm4_class(const struct hlsl_type *type) case HLSL_CLASS_ARRAY: case HLSL_CLASS_STRUCT: case HLSL_CLASS_OBJECT: + case HLSL_CLASS_SAMPLER: case HLSL_CLASS_STRING: case HLSL_CLASS_VOID: break; @@ -3109,20 +3110,28 @@ static void write_sm4_type(struct hlsl_ctx *ctx, struct vkd3d_bytecode_buffer *b static D3D_SHADER_INPUT_TYPE sm4_resource_type(const struct hlsl_type *type) { - if (type->class == HLSL_CLASS_ARRAY) - return sm4_resource_type(type->e.array.type); - - switch (type->base_type) + switch (type->class) { - case HLSL_TYPE_SAMPLER: + case HLSL_CLASS_ARRAY: + return sm4_resource_type(type->e.array.type); + case HLSL_CLASS_SAMPLER: return D3D_SIT_SAMPLER; - case HLSL_TYPE_TEXTURE: - return D3D_SIT_TEXTURE; - case HLSL_TYPE_UAV: - return D3D_SIT_UAV_RWTYPED; + case HLSL_CLASS_OBJECT: + switch (type->base_type) + { + case HLSL_TYPE_TEXTURE: + return D3D_SIT_TEXTURE; + case HLSL_TYPE_UAV: + return D3D_SIT_UAV_RWTYPED; + default: + break; + } + default: - vkd3d_unreachable(); + break; } + + vkd3d_unreachable(); } static D3D_RESOURCE_RETURN_TYPE sm4_resource_format(const struct hlsl_type *type)