From e5ba79b4f1fb3829a35677d2a97084d141e98ffb Mon Sep 17 00:00:00 2001 From: Nikolay Sivov Date: Mon, 4 Nov 2024 16:57:06 +0100 Subject: [PATCH] vkd3d-shader/hlsl: Implement the ByteAddressBuffer.Load*() methods. Signed-off-by: Nikolay Sivov --- Makefile.am | 1 + libs/vkd3d-shader/hlsl.y | 57 +++++++++++++++++++- libs/vkd3d-shader/tpf.c | 38 +++++++++++-- tests/hlsl/srv-byteaddressbuffer.shader_test | 56 +++++++++++++++++++ tests/shader_runner_d3d11.c | 16 ++++-- tests/shader_runner_d3d12.c | 1 + 6 files changed, 161 insertions(+), 8 deletions(-) create mode 100644 tests/hlsl/srv-byteaddressbuffer.shader_test diff --git a/Makefile.am b/Makefile.am index 13ced7ba..1bf8cac1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -232,6 +232,7 @@ vkd3d_shader_tests = \ tests/hlsl/smoothstep.shader_test \ tests/hlsl/sqrt.shader_test \ tests/hlsl/srv-buffers.shader_test \ + tests/hlsl/srv-byteaddressbuffer.shader_test \ tests/hlsl/state-block-function-syntax.shader_test \ tests/hlsl/state-block-syntax.shader_test \ tests/hlsl/static-initializer.shader_test \ diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index 6dfcc25a..60aade73 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -5603,6 +5603,55 @@ static bool raise_invalid_method_object_type(struct hlsl_ctx *ctx, const struct return false; } +static bool add_raw_load_method_call(struct hlsl_ctx *ctx, struct hlsl_block *block, struct hlsl_ir_node *object, + const char *name, const struct parse_initializer *params, const struct vkd3d_shader_location *loc) +{ + struct hlsl_resource_load_params load_params = {.type = HLSL_RESOURCE_LOAD}; + struct hlsl_ir_node *load; + unsigned int value_dim; + + if (params->args_count != 1 && params->args_count != 2) + { + hlsl_error(ctx, loc, VKD3D_SHADER_ERROR_HLSL_WRONG_PARAMETER_COUNT, + "Wrong number of arguments to method 'Load': expected between 1 and 2, but got %u.", + params->args_count); + return false; + } + + if (params->args_count == 2) + { + hlsl_fixme(ctx, loc, "Tiled resource status argument."); + return false; + } + + if (params->args[0]->data_type->class != HLSL_CLASS_SCALAR) + { + hlsl_error(ctx, loc, VKD3D_SHADER_ERROR_HLSL_INVALID_TYPE, "Scalar address argument expected for '%s'.", name); + return false; + } + + if (!strcmp(name, "Load")) + value_dim = 1; + else if (!strcmp(name, "Load2")) + value_dim = 2; + else if (!strcmp(name, "Load3")) + value_dim = 3; + else + value_dim = 4; + + if (!(load_params.coords = add_implicit_conversion(ctx, block, params->args[0], + hlsl_get_scalar_type(ctx, HLSL_TYPE_UINT), loc))) + return false; + + load_params.format = hlsl_get_vector_type(ctx, HLSL_TYPE_UINT, value_dim); + load_params.resource = object; + + if (!(load = hlsl_new_resource_load(ctx, &load_params, loc))) + return false; + hlsl_block_add_instr(block, load); + return true; +} + static bool add_load_method_call(struct hlsl_ctx *ctx, struct hlsl_block *block, struct hlsl_ir_node *object, const char *name, const struct parse_initializer *params, const struct vkd3d_shader_location *loc) { @@ -5612,6 +5661,9 @@ static bool add_load_method_call(struct hlsl_ctx *ctx, struct hlsl_block *block, struct hlsl_ir_node *load; bool multisampled; + if (object_type->sampler_dim == HLSL_SAMPLER_DIM_RAW_BUFFER) + return add_raw_load_method_call(ctx, block, object, name, params, loc); + if (object_type->sampler_dim == HLSL_SAMPLER_DIM_STRUCTURED_BUFFER) { hlsl_fixme(ctx, loc, "Method '%s' for structured buffers.", name); @@ -6264,7 +6316,10 @@ texture_methods[] = { "GetDimensions", add_getdimensions_method_call, "00111111111110" }, - { "Load", add_load_method_call, "00111011110110" }, + { "Load", add_load_method_call, "00111011110111" }, + { "Load2", add_raw_load_method_call, "00000000000001" }, + { "Load3", add_raw_load_method_call, "00000000000001" }, + { "Load4", add_raw_load_method_call, "00000000000001" }, { "Sample", add_sample_method_call, "00111111001000" }, { "SampleBias", add_sample_lod_method_call, "00111111001000" }, diff --git a/libs/vkd3d-shader/tpf.c b/libs/vkd3d-shader/tpf.c index 76865751..d705965e 100644 --- a/libs/vkd3d-shader/tpf.c +++ b/libs/vkd3d-shader/tpf.c @@ -4831,7 +4831,15 @@ static void write_sm4_dcl_textures(const struct tpf_compiler *tpf, const struct } else { - instr.opcode = VKD3D_SM4_OP_DCL_RESOURCE; + switch (component_type->sampler_dim) + { + case HLSL_SAMPLER_DIM_RAW_BUFFER: + instr.opcode = VKD3D_SM5_OP_DCL_RESOURCE_RAW; + break; + default: + instr.opcode = VKD3D_SM4_OP_DCL_RESOURCE; + break; + } } instr.extra_bits |= (sm4_resource_dimension(component_type) << VKD3D_SM4_RESOURCE_TYPE_SHIFT); @@ -5133,12 +5141,15 @@ static void write_sm4_ld(const struct tpf_compiler *tpf, const struct hlsl_ir_no && (resource_type->sampler_dim == HLSL_SAMPLER_DIM_2DMS || resource_type->sampler_dim == HLSL_SAMPLER_DIM_2DMSARRAY); bool uav = (hlsl_deref_get_regset(tpf->ctx, resource) == HLSL_REGSET_UAVS); const struct vkd3d_shader_version *version = &tpf->program->shader_version; + bool raw = resource_type->sampler_dim == HLSL_SAMPLER_DIM_RAW_BUFFER; unsigned int coords_writemask = VKD3DSP_WRITEMASK_ALL; struct sm4_instruction instr; memset(&instr, 0, sizeof(instr)); if (uav) instr.opcode = VKD3D_SM5_OP_LD_UAV_TYPED; + else if (raw) + instr.opcode = VKD3D_SM5_OP_LD_RAW; else instr.opcode = multisampled ? VKD3D_SM4_OP_LD2DMS : VKD3D_SM4_OP_LD; @@ -6227,6 +6238,7 @@ static void tpf_write_shdr(struct tpf_compiler *tpf, struct hlsl_ir_function_dec const struct hlsl_buffer *cbuffer; struct hlsl_ctx *ctx = tpf->ctx; size_t token_count_position; + uint32_t global_flags = 0; static const uint16_t shader_types[VKD3D_SHADER_TYPE_COUNT] = { @@ -6248,6 +6260,27 @@ static void tpf_write_shdr(struct tpf_compiler *tpf, struct hlsl_ir_function_dec put_u32(&buffer, vkd3d_make_u32((version->major << 4) | version->minor, shader_types[version->type])); token_count_position = put_u32(&buffer, 0); + if (version->major == 4) + { + for (i = 0; i < extern_resources_count; ++i) + { + const struct extern_resource *resource = &extern_resources[i]; + const struct hlsl_type *type = resource->component_type; + + if (type && type->class == HLSL_CLASS_TEXTURE && type->sampler_dim == HLSL_SAMPLER_DIM_RAW_BUFFER) + { + global_flags |= VKD3DSGF_ENABLE_RAW_AND_STRUCTURED_BUFFERS; + break; + } + } + } + + if (entry_func->early_depth_test && vkd3d_shader_ver_ge(version, 5, 0)) + global_flags |= VKD3DSGF_FORCE_EARLY_DEPTH_STENCIL; + + if (global_flags) + write_sm4_dcl_global_flags(tpf, global_flags); + if (version->type == VKD3D_SHADER_TYPE_HULL) { tpf_write_hs_decls(tpf); @@ -6282,9 +6315,6 @@ static void tpf_write_shdr(struct tpf_compiler *tpf, struct hlsl_ir_function_dec write_sm4_dcl_textures(tpf, resource, true); } - if (entry_func->early_depth_test && vkd3d_shader_ver_ge(version, 5, 0)) - write_sm4_dcl_global_flags(tpf, VKD3DSGF_FORCE_EARLY_DEPTH_STENCIL); - if (version->type == VKD3D_SHADER_TYPE_HULL) tpf_write_hs_control_point_phase(tpf); diff --git a/tests/hlsl/srv-byteaddressbuffer.shader_test b/tests/hlsl/srv-byteaddressbuffer.shader_test new file mode 100644 index 00000000..a738e925 --- /dev/null +++ b/tests/hlsl/srv-byteaddressbuffer.shader_test @@ -0,0 +1,56 @@ +[require] +shader model >= 4.0 + +[rtv 0] +format r32g32b32a32-uint +size (2d, 32, 32) + +[srv 0] +format r32-typeless +size (raw_buffer, 4) + +1 2 3 4 + +[pixel shader] +ByteAddressBuffer t; +uint offset; +uint count; + +uint4 main() : sv_target +{ + if (count == 1) + return uint4(t.Load(offset), 0xa, 0xb, 0xc); + if (count == 2) + return uint4(t.Load2(offset), 0xd, 0xe); + if (count == 3) + return uint4(t.Load3(offset), 0xf); + if (count == 4) + return t.Load4(offset); + return 0; +} + +[test] +uniform 0 uint4 0 1 0 0 +todo(msl | glsl) draw quad +probe (0, 0) rgbaui(0x1, 0xa, 0xb, 0xc) +uniform 0 uint4 4 1 0 0 +todo(msl | glsl) draw quad +probe (0, 0) rgbaui(0x2, 0xa, 0xb, 0xc) +uniform 0 uint4 0 2 0 0 +todo(msl | glsl) draw quad +probe (0, 0) rgbaui(0x1, 0x2, 0xd, 0xe) +uniform 0 uint4 4 2 0 0 +todo(msl | glsl) draw quad +probe (0, 0) rgbaui(0x2, 0x3, 0xd, 0xe) +uniform 0 uint4 0 3 0 0 +todo(msl | glsl) draw quad +probe (0, 0) rgbaui(0x1, 0x2, 0x3, 0xf) +uniform 0 uint4 4 3 0 0 +todo(msl | glsl) draw quad +probe (0, 0) rgbaui(0x2, 0x3, 0x4, 0xf) +uniform 0 uint4 0 4 0 0 +todo(msl | glsl) draw quad +probe (0, 0) rgbaui(0x1, 0x2, 0x3, 0x4) +uniform 0 uint4 4 4 0 0 +todo(msl | glsl) draw quad +probe (0, 0) rgbaui(0x2, 0x3, 0x4, 0x0) diff --git a/tests/shader_runner_d3d11.c b/tests/shader_runner_d3d11.c index 375417aa..a0769fe9 100644 --- a/tests/shader_runner_d3d11.c +++ b/tests/shader_runner_d3d11.c @@ -478,9 +478,19 @@ static void init_resource_srv_buffer(struct d3d11_shader_runner *runner, struct resource->resource = (ID3D11Resource *)resource->buffer; srv_desc.Format = params->desc.format; - srv_desc.ViewDimension = D3D11_SRV_DIMENSION_BUFFER; - srv_desc.Buffer.FirstElement = 0; - srv_desc.Buffer.NumElements = params->data_size / params->desc.texel_size; + if (params->is_raw) + { + srv_desc.ViewDimension = D3D11_SRV_DIMENSION_BUFFEREX; + srv_desc.BufferEx.FirstElement = 0; + srv_desc.BufferEx.NumElements = params->data_size / params->desc.texel_size; + srv_desc.BufferEx.Flags = D3D11_BUFFEREX_SRV_FLAG_RAW; + } + else + { + srv_desc.ViewDimension = D3D11_SRV_DIMENSION_BUFFER; + srv_desc.Buffer.FirstElement = 0; + srv_desc.Buffer.NumElements = params->data_size / params->desc.texel_size; + } hr = ID3D11Device_CreateShaderResourceView(device, resource->resource, &srv_desc, &resource->srv); ok(hr == S_OK, "Failed to create view, hr %#lx.\n", hr); } diff --git a/tests/shader_runner_d3d12.c b/tests/shader_runner_d3d12.c index 0bff37b3..543d2225 100644 --- a/tests/shader_runner_d3d12.c +++ b/tests/shader_runner_d3d12.c @@ -159,6 +159,7 @@ static struct resource *d3d12_runner_create_resource(struct shader_runner *r, co srv_desc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER; srv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; srv_desc.Buffer.NumElements = params->desc.width * params->desc.height; + srv_desc.Buffer.Flags = params->is_raw ? D3D12_BUFFER_SRV_FLAG_RAW : 0; ID3D12Device_CreateShaderResourceView(device, resource->resource, &srv_desc, get_cpu_descriptor_handle(test_context, runner->heap, resource->r.desc.slot));