From 8bdf4b2b41d8ea8110b372d363987f0302e228a2 Mon Sep 17 00:00:00 2001 From: Karol Herbst Date: Sat, 17 Jun 2023 08:50:12 +0200 Subject: [PATCH] compiler/types: fix size of padded OpenCL Structs In C the size of a struct { uin32_t a; uint8_t b; } is 8, not 5, so we have to account for the biggest alignment across all struct members. Funny that the OpenCL CTS doesn't catch that. Fixes: 44d32e62fb8 ("glsl: add cl_size and cl_alignment") Signed-off-by: Karol Herbst Reviewed-by: Jesse Natalie Part-of: (cherry picked from commit 4431e5a222a6921ed26885d505d0b51fa391aa42) --- .pick_status.json | 2 +- src/compiler/glsl_types.cpp | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.pick_status.json b/.pick_status.json index 7aab705a7f1..d9a965d1d5b 100644 --- a/.pick_status.json +++ b/.pick_status.json @@ -211,7 +211,7 @@ "description": "compiler/types: fix size of padded OpenCL Structs", "nominated": true, "nomination_type": 1, - "resolution": 0, + "resolution": 1, "main_sha": null, "because_sha": "44d32e62fb8d1fa9bf22c136aa41114d19b2d874" }, diff --git a/src/compiler/glsl_types.cpp b/src/compiler/glsl_types.cpp index 2bc7d1d7445..df11bf08ece 100644 --- a/src/compiler/glsl_types.cpp +++ b/src/compiler/glsl_types.cpp @@ -3360,13 +3360,20 @@ glsl_type::cl_size() const return size * this->length; } else if (this->is_struct()) { unsigned size = 0; + unsigned max_alignment = 1; for (unsigned i = 0; i < this->length; ++i) { struct glsl_struct_field &field = this->fields.structure[i]; /* if a struct is packed, members don't get aligned */ - if (!this->packed) - size = align(size, field.type->cl_alignment()); + if (!this->packed) { + unsigned alignment = field.type->cl_alignment(); + max_alignment = MAX2(max_alignment, alignment); + size = align(size, alignment); + } size += field.type->cl_size(); } + + /* Size of C structs are aligned to the biggest alignment of its fields */ + size = align(size, max_alignment); return size; } return 1;