Signed-off-by: Francisco Casas fcasas@codeweavers.com --- Makefile.am | 2 + libs/vkd3d-shader/hlsl.c | 4 ++ libs/vkd3d-shader/hlsl.h | 4 ++ libs/vkd3d-shader/hlsl.y | 78 +++++++++++++++++++++++++ libs/vkd3d-shader/hlsl_sm4.c | 66 ++++++++++++++++++++- tests/hlsl-gather.shader_test | 106 ++++++++++++++++++++++++++++++++++ 6 files changed, 259 insertions(+), 1 deletion(-) create mode 100644 tests/hlsl-gather.shader_test
diff --git a/Makefile.am b/Makefile.am index 3e083b0a..1a80152a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -67,6 +67,7 @@ vkd3d_shader_tests = \ tests/hlsl-duplicate-modifiers.shader_test \ tests/hlsl-for.shader_test \ tests/hlsl-function-overload.shader_test \ + tests/hlsl-gather.shader_test \ tests/hlsl-invalid.shader_test \ tests/hlsl-majority-pragma.shader_test \ tests/hlsl-majority-typedef.shader_test \ @@ -292,6 +293,7 @@ XFAIL_TESTS = \ tests/hlsl-duplicate-modifiers.shader_test \ tests/hlsl-for.shader_test \ tests/hlsl-function-overload.shader_test \ + tests/hlsl-gather.shader_test \ tests/hlsl-majority-pragma.shader_test \ tests/hlsl-majority-typedef.shader_test \ tests/hlsl-nested-arrays.shader_test \ diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index 1eee4278..74b78f1b 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -1250,6 +1250,10 @@ static void dump_ir_resource_load(struct vkd3d_string_buffer *buffer, const stru { [HLSL_RESOURCE_LOAD] = "load_resource", [HLSL_RESOURCE_SAMPLE] = "sample", + [HLSL_RESOURCE_GATHER_RED] = "gather4r", + [HLSL_RESOURCE_GATHER_GREEN] = "gather4g", + [HLSL_RESOURCE_GATHER_BLUE] = "gather4b", + [HLSL_RESOURCE_GATHER_ALPHA] = "gather4a", };
vkd3d_string_buffer_printf(buffer, "%s(resource = ", type_names[load->load_type]); diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index 365ef980..891f671a 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -377,6 +377,10 @@ enum hlsl_resource_load_type { HLSL_RESOURCE_LOAD, HLSL_RESOURCE_SAMPLE, + HLSL_RESOURCE_GATHER_RED, + HLSL_RESOURCE_GATHER_GREEN, + HLSL_RESOURCE_GATHER_BLUE, + HLSL_RESOURCE_GATHER_ALPHA, };
struct hlsl_ir_resource_load diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index b7e0409d..8a2c1dd9 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -1852,6 +1852,84 @@ static bool add_method_call(struct hlsl_ctx *ctx, struct list *instrs, struct hl list_add_tail(instrs, &load->node.entry); return true; } + else if (!strcmp(name, "Gather") || !strcmp(name, "GatherRed") || !strcmp(name, "GatherBlue") + || !strcmp(name, "GatherGreen") || !strcmp(name, "GatherAlpha")) + { + const unsigned int sampler_dim = sampler_dim_count(object_type->sampler_dim); + enum hlsl_resource_load_type load_type; + const struct hlsl_type *sampler_type; + struct hlsl_ir_resource_load *load; + struct hlsl_ir_load *sampler_load; + struct hlsl_type *result_type; + struct hlsl_ir_node *coords; + int read_channel; + + if(!strcmp(name, "Gather") || !strcmp(name, "GatherRed")){ + load_type = HLSL_RESOURCE_GATHER_RED; + read_channel = 0; + } + else if(!strcmp(name, "GatherGreen")){ + load_type = HLSL_RESOURCE_GATHER_GREEN; + read_channel = 1; + } + else if(!strcmp(name, "GatherBlue")){ + load_type = HLSL_RESOURCE_GATHER_BLUE; + read_channel = 2; + } + else if(!strcmp(name, "GatherAlpha")){ + load_type = HLSL_RESOURCE_GATHER_ALPHA; + read_channel = 3; + } else { + assert(!"Unexpected Gather method."); + } + + if (params->args_count != 3 && params->args_count != 4) + { + hlsl_error(ctx, *loc, VKD3D_SHADER_ERROR_HLSL_WRONG_PARAMETER_COUNT, + "Wrong number of arguments to method '%s': expected 3 or 4, but got %u.", name, params->args_count); + return false; + } + if (params->args_count >= 3) + FIXME("Ignoring offset parameter.\n"); + if (params->args_count >= 4) + FIXME("Ignoring 'status' output parameter.\n"); + + sampler_type = params->args[0]->data_type; + if (sampler_type->type != HLSL_CLASS_OBJECT || sampler_type->base_type != HLSL_TYPE_SAMPLER + || sampler_type->sampler_dim != HLSL_SAMPLER_DIM_GENERIC) + { + struct vkd3d_string_buffer *string; + + if ((string = hlsl_type_to_string(ctx, sampler_type))) + hlsl_error(ctx, *loc, VKD3D_SHADER_ERROR_HLSL_INVALID_TYPE, + "Wrong type for argument 0 of %s(): expected 'sampler', but got '%s'.", name, string->buffer); + hlsl_release_string_buffer(ctx, string); + return false; + } + + if(read_channel >= object_type->e.resource_format->dimx){ + hlsl_error(ctx, *loc, VKD3D_SHADER_ERROR_HLSL_INVALID_TYPE, + "Method %s() requires at least %d channels.", name, read_channel+1); + return false; + } + + result_type = hlsl_get_vector_type(ctx, object_type->e.resource_format->base_type, 4); + + /* Only HLSL_IR_LOAD can return an object. */ + sampler_load = hlsl_ir_load(params->args[0]); + + if (!(coords = add_implicit_conversion(ctx, instrs, params->args[1], + hlsl_get_vector_type(ctx, HLSL_TYPE_FLOAT, sampler_dim), loc))) + coords = params->args[1]; + + if (!(load = hlsl_new_resource_load(ctx, result_type, + load_type, object_load->src.var, object_load->src.offset.node, + sampler_load->src.var, sampler_load->src.offset.node, coords, loc))) + return false; + list_add_tail(instrs, &load->node.entry); + return true; + + } else { struct vkd3d_string_buffer *string; diff --git a/libs/vkd3d-shader/hlsl_sm4.c b/libs/vkd3d-shader/hlsl_sm4.c index a95503ee..5f290129 100644 --- a/libs/vkd3d-shader/hlsl_sm4.c +++ b/libs/vkd3d-shader/hlsl_sm4.c @@ -753,7 +753,7 @@ struct sm4_instruction struct sm4_register reg; enum vkd3d_sm4_swizzle_type swizzle_type; unsigned int swizzle; - } srcs[2]; + } srcs[3]; unsigned int src_count;
uint32_t idx[2]; @@ -1506,6 +1506,61 @@ static void write_sm4_loop(struct hlsl_ctx *ctx, write_sm4_instruction(buffer, &instr); }
+ +static void write_sm4_gather(struct hlsl_ctx *ctx, struct vkd3d_bytecode_buffer *buffer, + const struct hlsl_type *resource_type, const struct hlsl_ir_node *dst, + const struct hlsl_deref *resource, const struct hlsl_deref *sampler, const struct hlsl_ir_node *coords, + enum hlsl_resource_load_type load_type) +{ + struct sm4_instruction instr; + unsigned int writemask; + + memset(&instr, 0, sizeof(instr)); + instr.opcode = VKD3D_SM4_OP_GATHER4; + + sm4_register_from_node(&instr.dsts[0].reg, &instr.dsts[0].writemask, NULL, dst); + instr.dst_count = 1; + + sm4_register_from_node(&instr.srcs[0].reg, &writemask, &instr.srcs[0].swizzle_type, coords); + instr.srcs[0].swizzle = hlsl_swizzle_from_writemask(writemask); + + sm4_register_from_deref(ctx, &instr.srcs[1].reg, &writemask, &instr.srcs[1].swizzle_type, + resource, resource_type); + instr.srcs[1].swizzle = hlsl_map_swizzle(hlsl_swizzle_from_writemask(writemask), instr.dsts[0].writemask); + + sm4_register_from_deref(ctx, &instr.srcs[2].reg, &writemask, NULL, + sampler, sampler->var->data_type); + instr.srcs[2].reg.type = VKD3D_SM4_RT_SAMPLER; + instr.srcs[2].reg.dim = 1; + instr.srcs[2].swizzle_type = VKD3D_SM4_SWIZZLE_VEC4; + + switch (load_type){ + case HLSL_RESOURCE_GATHER_RED: + instr.srcs[2].swizzle = HLSL_SWIZZLE(X, X, X, X); + break; + case HLSL_RESOURCE_GATHER_GREEN: + instr.srcs[2].swizzle = HLSL_SWIZZLE(Y, Y, Y, Y); + break; + case HLSL_RESOURCE_GATHER_BLUE: + instr.srcs[2].swizzle = HLSL_SWIZZLE(Z, Z, Z, Z); + break; + case HLSL_RESOURCE_GATHER_ALPHA: + instr.srcs[2].swizzle = HLSL_SWIZZLE(W, W, W, W); + break; + default: + assert(!"Invalid hlsl_resource_load_type."); + break; + } + instr.src_count = 3; + + write_sm4_instruction(buffer, &instr); +} + + + + + + static void write_sm4_resource_load(struct hlsl_ctx *ctx, struct vkd3d_bytecode_buffer *buffer, const struct hlsl_ir_resource_load *load) { @@ -1527,6 +1582,15 @@ static void write_sm4_resource_load(struct hlsl_ctx *ctx, case HLSL_RESOURCE_SAMPLE: hlsl_fixme(ctx, load->node.loc, "Resource sample instruction."); break; + + case HLSL_RESOURCE_GATHER_RED: + case HLSL_RESOURCE_GATHER_GREEN: + case HLSL_RESOURCE_GATHER_BLUE: + case HLSL_RESOURCE_GATHER_ALPHA: + assert(load->sampler.var); + write_sm4_gather(ctx, buffer, resource_type, &load->node, &load->resource, + &load->sampler, coords, load->load_type); + break; } }
diff --git a/tests/hlsl-gather.shader_test b/tests/hlsl-gather.shader_test new file mode 100644 index 00000000..6731b77c --- /dev/null +++ b/tests/hlsl-gather.shader_test @@ -0,0 +1,106 @@ +[sampler 0] +filter linear linear linear +address clamp clamp clamp + +[texture 0] +size (3, 3) +0.0 0.0 0.0 0.4 0.1 0.0 0.5 0.0 0.2 0.0 0.0 0.4 +0.0 0.1 0.5 0.0 0.1 0.1 0.0 0.4 0.2 0.1 0.5 0.0 +0.0 0.2 0.0 0.4 0.1 0.2 0.5 0.0 0.2 0.2 0.0 0.4 + + + +[pixel shader] +sampler s; +Texture2D t; + +float4 main() : sv_target +{ + return t.Gather(s, float2(0.2, 0.2), int2(0 , 0)); +} + +[test] +draw quad +probe all rgba (0.0, 0.1, 0.1, 0.0) + + + +[pixel shader] +SamplerState s; +Texture2D t; + +float4 main() : sv_target +{ + return t.GatherGreen(s, float2(0.2, 0.2), int2(0 , 0)); +} + +[test] +draw quad +probe all rgba (0.1, 0.1, 0.0, 0.0) + + + +[pixel shader] +SamplerState s; +Texture2D t; + +float4 main() : sv_target +{ + return t.GatherGreen(s, float2(0.8, 0.8), int2(0 , 0)); +} + +[test] +draw quad +probe all rgba (0.2, 0.2, 0.1, 0.1) + + + + +[pixel shader] +SamplerState s; +Texture2D t; + +float4 main() : sv_target +{ + return t.GatherGreen(s, float2(0.2, 0.2), int2(0 , 0)); +} + +[test] +draw quad +probe all rgba (0.1, 0.1, 0.0, 0.0) + + + + +[pixel shader] +SamplerState s; +Texture2D t; + +float4 main() : sv_target +{ + return t.GatherBlue(s, float2(0.2, 0.8), int2(0 , 0)); +} + +[test] +draw quad +probe all rgba (0.0, 0.5, 0.0, 0.5) + + + + +[pixel shader] +SamplerState s; +Texture2D t; + +float4 main() : sv_target +{ + return t.GatherAlpha(s, float2(0.2, 0.8), int2(0 , 0)); +} + +[test] +draw quad +probe all rgba (0.4, 0.0, 0.4, 0.0) + + + +
On 11/22/21 9:28 AM, Francisco Casas wrote:
diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index b7e0409d..8a2c1dd9 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -1852,6 +1852,84 @@ static bool add_method_call(struct hlsl_ctx *ctx, struct list *instrs, struct hl list_add_tail(instrs, &load->node.entry); return true; }
- else if (!strcmp(name, "Gather") || !strcmp(name, "GatherRed") || !strcmp(name, "GatherBlue")
|| !strcmp(name, "GatherGreen") || !strcmp(name, "GatherAlpha"))
- {
const unsigned int sampler_dim = sampler_dim_count(object_type->sampler_dim);
enum hlsl_resource_load_type load_type;
const struct hlsl_type *sampler_type;
struct hlsl_ir_resource_load *load;
struct hlsl_ir_load *sampler_load;
struct hlsl_type *result_type;
struct hlsl_ir_node *coords;
int read_channel;
if(!strcmp(name, "Gather") || !strcmp(name, "GatherRed")){
load_type = HLSL_RESOURCE_GATHER_RED;
read_channel = 0;
}
else if(!strcmp(name, "GatherGreen")){
load_type = HLSL_RESOURCE_GATHER_GREEN;
read_channel = 1;
}
else if(!strcmp(name, "GatherBlue")){
load_type = HLSL_RESOURCE_GATHER_BLUE;
read_channel = 2;
}
else if(!strcmp(name, "GatherAlpha")){
load_type = HLSL_RESOURCE_GATHER_ALPHA;
read_channel = 3;
} else {
assert(!"Unexpected Gather method.");
}
if (params->args_count != 3 && params->args_count != 4)
{
hlsl_error(ctx, *loc, VKD3D_SHADER_ERROR_HLSL_WRONG_PARAMETER_COUNT,
"Wrong number of arguments to method '%s': expected 3 or 4, but got %u.", name, params->args_count);
return false;
}
It seems that it's legal to omit the offset parameter as well.
It's also apparently legal to specify a list of four different offsets, one for each component, for a total of six or seven arguments. That doesn't necessarily need to be handled here, but it might be a good idea to at least print a FIXME for it.
if (params->args_count >= 3)
FIXME("Ignoring offset parameter.\n");
if (params->args_count >= 4)
FIXME("Ignoring 'status' output parameter.\n");
sampler_type = params->args[0]->data_type;
if (sampler_type->type != HLSL_CLASS_OBJECT || sampler_type->base_type != HLSL_TYPE_SAMPLER
|| sampler_type->sampler_dim != HLSL_SAMPLER_DIM_GENERIC)
{
struct vkd3d_string_buffer *string;
if ((string = hlsl_type_to_string(ctx, sampler_type)))
hlsl_error(ctx, *loc, VKD3D_SHADER_ERROR_HLSL_INVALID_TYPE,
"Wrong type for argument 0 of %s(): expected 'sampler', but got '%s'.", name, string->buffer);
hlsl_release_string_buffer(ctx, string);
return false;
}
if(read_channel >= object_type->e.resource_format->dimx){
hlsl_error(ctx, *loc, VKD3D_SHADER_ERROR_HLSL_INVALID_TYPE,
"Method %s() requires at least %d channels.", name, read_channel+1);
return false;
}
result_type = hlsl_get_vector_type(ctx, object_type->e.resource_format->base_type, 4);
/* Only HLSL_IR_LOAD can return an object. */
sampler_load = hlsl_ir_load(params->args[0]);
if (!(coords = add_implicit_conversion(ctx, instrs, params->args[1],
hlsl_get_vector_type(ctx, HLSL_TYPE_FLOAT, sampler_dim), loc)))
coords = params->args[1];
if (!(load = hlsl_new_resource_load(ctx, result_type,
load_type, object_load->src.var, object_load->src.offset.node,
sampler_load->src.var, sampler_load->src.offset.node, coords, loc)))
return false;
list_add_tail(instrs, &load->node.entry);
return true;
- } else { struct vkd3d_string_buffer *string;
diff --git a/libs/vkd3d-shader/hlsl_sm4.c b/libs/vkd3d-shader/hlsl_sm4.c index a95503ee..5f290129 100644 --- a/libs/vkd3d-shader/hlsl_sm4.c +++ b/libs/vkd3d-shader/hlsl_sm4.c @@ -753,7 +753,7 @@ struct sm4_instruction struct sm4_register reg; enum vkd3d_sm4_swizzle_type swizzle_type; unsigned int swizzle;
- } srcs[2];
} srcs[3]; unsigned int src_count;
uint32_t idx[2];
@@ -1506,6 +1506,61 @@ static void write_sm4_loop(struct hlsl_ctx *ctx, write_sm4_instruction(buffer, &instr); }
+static void write_sm4_gather(struct hlsl_ctx *ctx, struct vkd3d_bytecode_buffer *buffer,
const struct hlsl_type *resource_type, const struct hlsl_ir_node *dst,
const struct hlsl_deref *resource, const struct hlsl_deref *sampler, const struct hlsl_ir_node *coords,
enum hlsl_resource_load_type load_type)
+{
- struct sm4_instruction instr;
- unsigned int writemask;
- memset(&instr, 0, sizeof(instr));
- instr.opcode = VKD3D_SM4_OP_GATHER4;
- sm4_register_from_node(&instr.dsts[0].reg, &instr.dsts[0].writemask, NULL, dst);
- instr.dst_count = 1;
- sm4_register_from_node(&instr.srcs[0].reg, &writemask, &instr.srcs[0].swizzle_type, coords);
- instr.srcs[0].swizzle = hlsl_swizzle_from_writemask(writemask);
- sm4_register_from_deref(ctx, &instr.srcs[1].reg, &writemask, &instr.srcs[1].swizzle_type,
resource, resource_type);
- instr.srcs[1].swizzle = hlsl_map_swizzle(hlsl_swizzle_from_writemask(writemask), instr.dsts[0].writemask);
- sm4_register_from_deref(ctx, &instr.srcs[2].reg, &writemask, NULL,
sampler, sampler->var->data_type);
- instr.srcs[2].reg.type = VKD3D_SM4_RT_SAMPLER;
- instr.srcs[2].reg.dim = 1;
This should be a VKD3D_SM4_DIMENSION constant (for which I observe VEC4).
- instr.srcs[2].swizzle_type = VKD3D_SM4_SWIZZLE_VEC4;
I observe VKD3D_SM4_SWIZZLE_SCALAR from native here; do you have an example with VEC4?
- switch (load_type){
case HLSL_RESOURCE_GATHER_RED:
instr.srcs[2].swizzle = HLSL_SWIZZLE(X, X, X, X);
break;
case HLSL_RESOURCE_GATHER_GREEN:
instr.srcs[2].swizzle = HLSL_SWIZZLE(Y, Y, Y, Y);
break;
case HLSL_RESOURCE_GATHER_BLUE:
instr.srcs[2].swizzle = HLSL_SWIZZLE(Z, Z, Z, Z);
break;
case HLSL_RESOURCE_GATHER_ALPHA:
instr.srcs[2].swizzle = HLSL_SWIZZLE(W, W, W, W);
break;
default:
assert(!"Invalid hlsl_resource_load_type.");
break;
- }
It might be simpler code just to pass the swizzle (component) as an argument to this function.
- instr.src_count = 3;
- write_sm4_instruction(buffer, &instr);
+}
- static void write_sm4_resource_load(struct hlsl_ctx *ctx, struct vkd3d_bytecode_buffer *buffer, const struct hlsl_ir_resource_load *load) {
@@ -1527,6 +1582,15 @@ static void write_sm4_resource_load(struct hlsl_ctx *ctx, case HLSL_RESOURCE_SAMPLE: hlsl_fixme(ctx, load->node.loc, "Resource sample instruction."); break;
case HLSL_RESOURCE_GATHER_RED:
case HLSL_RESOURCE_GATHER_GREEN:
case HLSL_RESOURCE_GATHER_BLUE:
case HLSL_RESOURCE_GATHER_ALPHA:
assert(load->sampler.var);
write_sm4_gather(ctx, buffer, resource_type, &load->node, &load->resource,
&load->sampler, coords, load->load_type);
}break; }
diff --git a/tests/hlsl-gather.shader_test b/tests/hlsl-gather.shader_test new file mode 100644 index 00000000..6731b77c --- /dev/null +++ b/tests/hlsl-gather.shader_test @@ -0,0 +1,106 @@ +[sampler 0] +filter linear linear linear +address clamp clamp clamp
+[texture 0] +size (3, 3) +0.0 0.0 0.0 0.4 0.1 0.0 0.5 0.0 0.2 0.0 0.0 0.4 +0.0 0.1 0.5 0.0 0.1 0.1 0.0 0.4 0.2 0.1 0.5 0.0 +0.0 0.2 0.0 0.4 0.1 0.2 0.5 0.0 0.2 0.2 0.0 0.4
+[pixel shader] +sampler s; +Texture2D t;
+float4 main() : sv_target +{
- return t.Gather(s, float2(0.2, 0.2), int2(0 , 0));
+}
+[test] +draw quad +probe all rgba (0.0, 0.1, 0.1, 0.0)
Can you please add a call to GatherRed()?
+[pixel shader] +SamplerState s; +Texture2D t;
+float4 main() : sv_target +{
- return t.GatherGreen(s, float2(0.2, 0.2), int2(0 , 0));
+}
+[test] +draw quad +probe all rgba (0.1, 0.1, 0.0, 0.0)
+[pixel shader] +SamplerState s; +Texture2D t;
+float4 main() : sv_target +{
- return t.GatherGreen(s, float2(0.8, 0.8), int2(0 , 0));
+}
+[test] +draw quad +probe all rgba (0.2, 0.2, 0.1, 0.1)
+[pixel shader] +SamplerState s; +Texture2D t;
+float4 main() : sv_target +{
- return t.GatherGreen(s, float2(0.2, 0.2), int2(0 , 0));
+}
+[test] +draw quad +probe all rgba (0.1, 0.1, 0.0, 0.0)
+[pixel shader] +SamplerState s; +Texture2D t;
+float4 main() : sv_target +{
- return t.GatherBlue(s, float2(0.2, 0.8), int2(0 , 0));
+}
+[test] +draw quad +probe all rgba (0.0, 0.5, 0.0, 0.5)
+[pixel shader] +SamplerState s; +Texture2D t;
+float4 main() : sv_target +{
- return t.GatherAlpha(s, float2(0.2, 0.8), int2(0 , 0));
+}
+[test] +draw quad +probe all rgba (0.4, 0.0, 0.4, 0.0)