mirror of
https://gitlab.winehq.org/wine/vkd3d.git
synced 2025-09-12 18:50:22 -07:00
vkd3d-shader: Allow controlling clip planes through vkd3d-shader parameters.
This commit is contained in:
committed by
Henri Verbeet
parent
85b8503995
commit
4400315c4b
Notes:
Henri Verbeet
2024-10-02 22:36:40 +02:00
Approved-by: Elizabeth Figura (@zfigura) Approved-by: Henri Verbeet (@hverbeet) Merge-Request: https://gitlab.winehq.org/wine/vkd3d/-/merge_requests/946
@@ -215,6 +215,14 @@ static void src_param_init_temp_float(struct vkd3d_shader_src_param *src, unsign
|
||||
src->reg.idx[0].offset = idx;
|
||||
}
|
||||
|
||||
static void src_param_init_temp_float4(struct vkd3d_shader_src_param *src, unsigned int idx)
|
||||
{
|
||||
vsir_src_param_init(src, VKD3DSPR_TEMP, VKD3D_DATA_FLOAT, 1);
|
||||
src->reg.dimension = VSIR_DIMENSION_VEC4;
|
||||
src->swizzle = VKD3D_SHADER_NO_SWIZZLE;
|
||||
src->reg.idx[0].offset = idx;
|
||||
}
|
||||
|
||||
static void src_param_init_temp_uint(struct vkd3d_shader_src_param *src, unsigned int idx)
|
||||
{
|
||||
vsir_src_param_init(src, VKD3DSPR_TEMP, VKD3D_DATA_UINT, 1);
|
||||
@@ -5539,9 +5547,11 @@ static bool find_colour_signature_idx(const struct shader_signature *signature,
|
||||
|
||||
static enum vkd3d_result insert_alpha_test_before_ret(struct vsir_program *program,
|
||||
const struct vkd3d_shader_instruction *ret, enum vkd3d_shader_comparison_func compare_func,
|
||||
const struct vkd3d_shader_parameter1 *ref, uint32_t colour_signature_idx, uint32_t colour_temp, size_t *ret_pos)
|
||||
const struct vkd3d_shader_parameter1 *ref, uint32_t colour_signature_idx,
|
||||
uint32_t colour_temp, size_t *ret_pos, struct vkd3d_shader_message_context *message_context)
|
||||
{
|
||||
struct vkd3d_shader_instruction_array *instructions = &program->instructions;
|
||||
static const struct vkd3d_shader_location no_loc;
|
||||
size_t pos = ret - instructions->elements;
|
||||
struct vkd3d_shader_instruction *ins;
|
||||
|
||||
@@ -5596,6 +5606,11 @@ static enum vkd3d_result insert_alpha_test_before_ret(struct vsir_program *progr
|
||||
VKD3D_SHADER_PARAMETER_NAME_ALPHA_TEST_REF, VKD3D_DATA_UINT);
|
||||
break;
|
||||
|
||||
case VKD3D_SHADER_PARAMETER_DATA_TYPE_FLOAT32_VEC4:
|
||||
vkd3d_shader_error(message_context, &no_loc, VKD3D_SHADER_ERROR_VSIR_INVALID_PARAMETER,
|
||||
"Alpha test reference data type must be a single component.");
|
||||
return VKD3D_ERROR_INVALID_ARGUMENT;
|
||||
|
||||
default:
|
||||
FIXME("Unhandled parameter data type %#x.\n", ref->data_type);
|
||||
return VKD3D_ERROR_NOT_IMPLEMENTED;
|
||||
@@ -5682,7 +5697,7 @@ static enum vkd3d_result vsir_program_insert_alpha_test(struct vsir_program *pro
|
||||
if (ins->opcode == VKD3DSIH_RET)
|
||||
{
|
||||
if ((ret = insert_alpha_test_before_ret(program, ins, compare_func,
|
||||
ref, colour_signature_idx, colour_temp, &new_pos)) < 0)
|
||||
ref, colour_signature_idx, colour_temp, &new_pos, message_context)) < 0)
|
||||
return ret;
|
||||
i = new_pos;
|
||||
continue;
|
||||
@@ -5709,6 +5724,202 @@ static enum vkd3d_result vsir_program_insert_alpha_test(struct vsir_program *pro
|
||||
return VKD3D_OK;
|
||||
}
|
||||
|
||||
static enum vkd3d_result insert_clip_planes_before_ret(struct vsir_program *program,
|
||||
const struct vkd3d_shader_instruction *ret, uint32_t mask, uint32_t position_signature_idx,
|
||||
uint32_t position_temp, uint32_t low_signature_idx, uint32_t high_signature_idx, size_t *ret_pos)
|
||||
{
|
||||
struct vkd3d_shader_instruction_array *instructions = &program->instructions;
|
||||
size_t pos = ret - instructions->elements;
|
||||
struct vkd3d_shader_instruction *ins;
|
||||
unsigned int output_idx = 0;
|
||||
|
||||
if (!shader_instruction_array_insert_at(&program->instructions, pos, vkd3d_popcount(mask) + 1))
|
||||
return VKD3D_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
ins = &program->instructions.elements[pos];
|
||||
|
||||
for (unsigned int i = 0; i < 8; ++i)
|
||||
{
|
||||
if (!(mask & (1u << i)))
|
||||
continue;
|
||||
|
||||
vsir_instruction_init_with_params(program, ins, &ret->location, VKD3DSIH_DP4, 1, 2);
|
||||
src_param_init_temp_float4(&ins->src[0], position_temp);
|
||||
src_param_init_parameter(&ins->src[1], VKD3D_SHADER_PARAMETER_NAME_CLIP_PLANE_0 + i, VKD3D_DATA_FLOAT);
|
||||
ins->src[1].swizzle = VKD3D_SHADER_NO_SWIZZLE;
|
||||
ins->src[1].reg.dimension = VSIR_DIMENSION_VEC4;
|
||||
|
||||
vsir_dst_param_init(&ins->dst[0], VKD3DSPR_OUTPUT, VKD3D_DATA_FLOAT, 1);
|
||||
if (output_idx < 4)
|
||||
ins->dst[0].reg.idx[0].offset = low_signature_idx;
|
||||
else
|
||||
ins->dst[0].reg.idx[0].offset = high_signature_idx;
|
||||
ins->dst[0].reg.dimension = VSIR_DIMENSION_VEC4;
|
||||
ins->dst[0].write_mask = (1u << (output_idx % 4));
|
||||
++output_idx;
|
||||
|
||||
++ins;
|
||||
}
|
||||
|
||||
vsir_instruction_init_with_params(program, ins, &ret->location, VKD3DSIH_MOV, 1, 1);
|
||||
vsir_dst_param_init(&ins->dst[0], VKD3DSPR_OUTPUT, VKD3D_DATA_FLOAT, 1);
|
||||
ins->dst[0].reg.idx[0].offset = position_signature_idx;
|
||||
ins->dst[0].reg.dimension = VSIR_DIMENSION_VEC4;
|
||||
ins->dst[0].write_mask = program->output_signature.elements[position_signature_idx].mask;
|
||||
src_param_init_temp_float(&ins->src[0], position_temp);
|
||||
ins->src[0].reg.dimension = VSIR_DIMENSION_VEC4;
|
||||
ins->src[0].swizzle = VKD3D_SHADER_NO_SWIZZLE;
|
||||
|
||||
*ret_pos = pos + vkd3d_popcount(mask) + 1;
|
||||
return VKD3D_OK;
|
||||
}
|
||||
|
||||
static bool find_position_signature_idx(const struct shader_signature *signature, uint32_t *idx)
|
||||
{
|
||||
for (unsigned int i = 0; i < signature->element_count; ++i)
|
||||
{
|
||||
if (signature->elements[i].sysval_semantic == VKD3D_SHADER_SV_POSITION)
|
||||
{
|
||||
*idx = i;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static enum vkd3d_result vsir_program_insert_clip_planes(struct vsir_program *program,
|
||||
struct vsir_transformation_context *ctx)
|
||||
{
|
||||
struct shader_signature *signature = &program->output_signature;
|
||||
unsigned int low_signature_idx = ~0u, high_signature_idx = ~0u;
|
||||
const struct vkd3d_shader_parameter1 *mask_parameter = NULL;
|
||||
struct signature_element *new_elements, *clip_element;
|
||||
uint32_t position_signature_idx, position_temp, mask;
|
||||
static const struct vkd3d_shader_location no_loc;
|
||||
struct vkd3d_shader_instruction *ins;
|
||||
unsigned int plane_count;
|
||||
size_t new_pos;
|
||||
int ret;
|
||||
|
||||
if (program->shader_version.type != VKD3D_SHADER_TYPE_VERTEX)
|
||||
return VKD3D_OK;
|
||||
|
||||
for (unsigned int i = 0; i < program->parameter_count; ++i)
|
||||
{
|
||||
const struct vkd3d_shader_parameter1 *parameter = &program->parameters[i];
|
||||
|
||||
if (parameter->name == VKD3D_SHADER_PARAMETER_NAME_CLIP_PLANE_MASK)
|
||||
mask_parameter = parameter;
|
||||
}
|
||||
|
||||
if (!mask_parameter)
|
||||
return VKD3D_OK;
|
||||
|
||||
if (mask_parameter->type != VKD3D_SHADER_PARAMETER_TYPE_IMMEDIATE_CONSTANT)
|
||||
{
|
||||
vkd3d_shader_error(ctx->message_context, &no_loc, VKD3D_SHADER_ERROR_VSIR_NOT_IMPLEMENTED,
|
||||
"Unsupported clip plane mask parameter type %#x.", mask_parameter->type);
|
||||
return VKD3D_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
if (mask_parameter->data_type != VKD3D_SHADER_PARAMETER_DATA_TYPE_UINT32)
|
||||
{
|
||||
vkd3d_shader_error(ctx->message_context, &no_loc, VKD3D_SHADER_ERROR_VSIR_INVALID_DATA_TYPE,
|
||||
"Invalid clip plane mask parameter data type %#x.", mask_parameter->data_type);
|
||||
return VKD3D_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
mask = mask_parameter->u.immediate_constant.u.u32;
|
||||
|
||||
if (!mask)
|
||||
return VKD3D_OK;
|
||||
|
||||
for (unsigned int i = 0; i < signature->element_count; ++i)
|
||||
{
|
||||
if (signature->elements[i].sysval_semantic == VKD3D_SHADER_SV_CLIP_DISTANCE)
|
||||
{
|
||||
vkd3d_shader_error(ctx->message_context, &no_loc, VKD3D_SHADER_ERROR_VSIR_INVALID_PARAMETER,
|
||||
"Clip planes cannot be used if the shader writes clip distance.");
|
||||
return VKD3D_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
}
|
||||
|
||||
if (!find_position_signature_idx(signature, &position_signature_idx))
|
||||
{
|
||||
vkd3d_shader_error(ctx->message_context, &no_loc, VKD3D_SHADER_ERROR_VSIR_MISSING_SEMANTIC,
|
||||
"Shader does not write position.");
|
||||
return VKD3D_ERROR_INVALID_SHADER;
|
||||
}
|
||||
|
||||
/* Append the clip plane signature indices. */
|
||||
|
||||
plane_count = vkd3d_popcount(mask);
|
||||
|
||||
if (!(new_elements = vkd3d_realloc(signature->elements,
|
||||
(signature->element_count + 2) * sizeof(*signature->elements))))
|
||||
return VKD3D_ERROR_OUT_OF_MEMORY;
|
||||
signature->elements = new_elements;
|
||||
|
||||
low_signature_idx = signature->element_count;
|
||||
clip_element = &signature->elements[signature->element_count++];
|
||||
memset(clip_element, 0, sizeof(*clip_element));
|
||||
clip_element->sysval_semantic = VKD3D_SHADER_SV_CLIP_DISTANCE;
|
||||
clip_element->component_type = VKD3D_SHADER_COMPONENT_FLOAT;
|
||||
clip_element->register_count = 1;
|
||||
clip_element->mask = vkd3d_write_mask_from_component_count(min(plane_count, 4));
|
||||
clip_element->used_mask = clip_element->mask;
|
||||
clip_element->min_precision = VKD3D_SHADER_MINIMUM_PRECISION_NONE;
|
||||
|
||||
if (plane_count > 4)
|
||||
{
|
||||
high_signature_idx = signature->element_count;
|
||||
clip_element = &signature->elements[signature->element_count++];
|
||||
memset(clip_element, 0, sizeof(*clip_element));
|
||||
clip_element->sysval_semantic = VKD3D_SHADER_SV_CLIP_DISTANCE;
|
||||
clip_element->semantic_index = 1;
|
||||
clip_element->component_type = VKD3D_SHADER_COMPONENT_FLOAT;
|
||||
clip_element->register_count = 1;
|
||||
clip_element->mask = vkd3d_write_mask_from_component_count(plane_count - 4);
|
||||
clip_element->used_mask = clip_element->mask;
|
||||
clip_element->min_precision = VKD3D_SHADER_MINIMUM_PRECISION_NONE;
|
||||
}
|
||||
|
||||
/* We're going to be reading from the output position, so we need to go
|
||||
* through the whole shader and convert it to a temp. */
|
||||
|
||||
position_temp = program->temp_count++;
|
||||
|
||||
for (size_t i = 0; i < program->instructions.count; ++i)
|
||||
{
|
||||
ins = &program->instructions.elements[i];
|
||||
|
||||
if (vsir_instruction_is_dcl(ins))
|
||||
continue;
|
||||
|
||||
if (ins->opcode == VKD3DSIH_RET)
|
||||
{
|
||||
if ((ret = insert_clip_planes_before_ret(program, ins, mask, position_signature_idx,
|
||||
position_temp, low_signature_idx, high_signature_idx, &new_pos)) < 0)
|
||||
return ret;
|
||||
i = new_pos;
|
||||
continue;
|
||||
}
|
||||
|
||||
for (size_t j = 0; j < ins->dst_count; ++j)
|
||||
{
|
||||
struct vkd3d_shader_dst_param *dst = &ins->dst[j];
|
||||
|
||||
/* Note we run after I/O normalization. */
|
||||
if (dst->reg.type == VKD3DSPR_OUTPUT && dst->reg.idx[0].offset == position_signature_idx)
|
||||
{
|
||||
dst->reg.type = VKD3DSPR_TEMP;
|
||||
dst->reg.idx[0].offset = position_temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return VKD3D_OK;
|
||||
}
|
||||
|
||||
struct validation_context
|
||||
{
|
||||
struct vkd3d_shader_message_context *message_context;
|
||||
@@ -6842,6 +7053,7 @@ enum vkd3d_result vsir_program_transform(struct vsir_program *program, uint64_t
|
||||
}
|
||||
|
||||
vsir_transform(&ctx, vsir_program_insert_alpha_test);
|
||||
vsir_transform(&ctx, vsir_program_insert_clip_planes);
|
||||
|
||||
if (TRACE_ON())
|
||||
vkd3d_shader_trace(program);
|
||||
|
Reference in New Issue
Block a user