diff options
author | Sebastián Aedo <saedo@codeweavers.com> | 2021-11-11 18:57:57 +0300 |
---|---|---|
committer | Sebastián Aedo <saedo@codeweavers.com> | 2021-12-07 22:00:06 +0300 |
commit | 905b8244e7756d8657c3a3ef97693af00f9e38bb (patch) | |
tree | 077adf80d8786c870d9a644ba04c18037277e21e /spirv_glsl.cpp | |
parent | e9cc6403341baf0edd430a4027b074d0a06b782f (diff) |
Clamp vector element access to vector size.
In cases where we know the size of the vector and the index at compile
time, we can check if it's accessing in bounds and rely in undefined
behavior otherwise.
Signed-off-by: Sebastián Aedo <saedo@codeweavers.com>
Diffstat (limited to 'spirv_glsl.cpp')
-rw-r--r-- | spirv_glsl.cpp | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/spirv_glsl.cpp b/spirv_glsl.cpp index cdc1c6b6..f06ed2c0 100644 --- a/spirv_glsl.cpp +++ b/spirv_glsl.cpp @@ -8894,30 +8894,37 @@ string CompilerGLSL::access_chain_internal(uint32_t base, const uint32_t *indice is_packed); } - if (is_literal && !is_packed && !row_major_matrix_needs_conversion) + if (is_literal) { - expr += "."; - expr += index_to_swizzle(index); + bool out_of_bounds = (index >= type->vecsize); + + if (!is_packed && !row_major_matrix_needs_conversion) + { + expr += "."; + expr += index_to_swizzle(out_of_bounds ? 0 : index); + } + else + { + // For packed vectors, we can only access them as an array, not by swizzle. + expr += join("[", out_of_bounds ? 0 : index, "]"); + } } else if (ir.ids[index].get_type() == TypeConstant && !is_packed && !row_major_matrix_needs_conversion) { auto &c = get<SPIRConstant>(index); + bool out_of_bounds = (c.scalar() >= type->vecsize); + if (c.specialization) { // If the index is a spec constant, we cannot turn extract into a swizzle. - expr += join("[", to_expression(index), "]"); + expr += join("[", out_of_bounds ? "0" : to_expression(index), "]"); } else { expr += "."; - expr += index_to_swizzle(c.scalar()); + expr += index_to_swizzle(out_of_bounds ? 0 : c.scalar()); } } - else if (is_literal) - { - // For packed vectors, we can only access them as an array, not by swizzle. - expr += join("[", index, "]"); - } else { expr += "["; |