From bfcc7448920644f232f5370e56c10c2ba15e0731 Mon Sep 17 00:00:00 2001 From: Jason Ekstrand Date: Mon, 18 Jan 2016 11:35:04 -0800 Subject: [PATCH] genX/pack: Add a __gen_fixed helper and use it for TextureLODBias The __gen_fixed helper properly clamps the value and also handles negative values correctly. Eventually, we need to make the scripts generate this and use it for more things. --- src/vulkan/gen75_pack.h | 32 +++++++++++++++++++++++++++++++- src/vulkan/gen7_pack.h | 32 +++++++++++++++++++++++++++++++- src/vulkan/gen8_pack.h | 32 +++++++++++++++++++++++++++++++- src/vulkan/gen9_pack.h | 32 +++++++++++++++++++++++++++++++- 4 files changed, 124 insertions(+), 4 deletions(-) diff --git a/src/vulkan/gen75_pack.h b/src/vulkan/gen75_pack.h index 3d336848592..b012032190e 100644 --- a/src/vulkan/gen75_pack.h +++ b/src/vulkan/gen75_pack.h @@ -62,6 +62,36 @@ __gen_field(uint64_t v, uint32_t start, uint32_t end) return v << start; } +static inline uint64_t +__gen_fixed(float v, uint32_t start, uint32_t end, + bool is_signed, uint32_t fract_bits) +{ + __gen_validate_value(v); + + const float factor = (1 << fract_bits); + + float max, min; + if (is_signed) { + max = ((1 << (end - start)) - 1) / factor; + min = -(1 << (end - start)) / factor; + } else { + max = ((1 << (end - start + 1)) - 1) / factor; + min = 0.0f; + } + + if (v > max) + v = max; + else if (v < min) + v = min; + + int32_t int_val = roundf(v * factor); + + if (is_signed) + int_val &= (1 << (end - start + 1)) - 1; + + return int_val << start; +} + static inline uint64_t __gen_offset(uint64_t v, uint32_t start, uint32_t end) { @@ -8048,7 +8078,7 @@ GEN75_SAMPLER_STATE_pack(__gen_user_data *data, void * restrict dst, __gen_field(values->MipModeFilter, 20, 21) | __gen_field(values->MagModeFilter, 17, 19) | __gen_field(values->MinModeFilter, 14, 16) | - __gen_field(values->TextureLODBias * (1 << 8), 1, 13) | + __gen_fixed(values->TextureLODBias, 1, 13, true, 8) | __gen_field(values->AnisotropicAlgorithm, 0, 0) | 0; diff --git a/src/vulkan/gen7_pack.h b/src/vulkan/gen7_pack.h index 0fe13de89df..a3ba30a9745 100644 --- a/src/vulkan/gen7_pack.h +++ b/src/vulkan/gen7_pack.h @@ -62,6 +62,36 @@ __gen_field(uint64_t v, uint32_t start, uint32_t end) return v << start; } +static inline uint64_t +__gen_fixed(float v, uint32_t start, uint32_t end, + bool is_signed, uint32_t fract_bits) +{ + __gen_validate_value(v); + + const float factor = (1 << fract_bits); + + float max, min; + if (is_signed) { + max = ((1 << (end - start)) - 1) / factor; + min = -(1 << (end - start)) / factor; + } else { + max = ((1 << (end - start + 1)) - 1) / factor; + min = 0.0f; + } + + if (v > max) + v = max; + else if (v < min) + v = min; + + int32_t int_val = roundf(v * factor); + + if (is_signed) + int_val &= (1 << (end - start + 1)) - 1; + + return int_val << start; +} + static inline uint64_t __gen_offset(uint64_t v, uint32_t start, uint32_t end) { @@ -6630,7 +6660,7 @@ GEN7_SAMPLER_STATE_pack(__gen_user_data *data, void * restrict dst, __gen_field(values->MipModeFilter, 20, 21) | __gen_field(values->MagModeFilter, 17, 19) | __gen_field(values->MinModeFilter, 14, 16) | - __gen_field(values->TextureLODBias * (1 << 8), 1, 13) | + __gen_fixed(values->TextureLODBias, 1, 13, true, 8) | __gen_field(values->AnisotropicAlgorithm, 0, 0) | 0; diff --git a/src/vulkan/gen8_pack.h b/src/vulkan/gen8_pack.h index 0d77b68a9c5..042e0290a74 100644 --- a/src/vulkan/gen8_pack.h +++ b/src/vulkan/gen8_pack.h @@ -62,6 +62,36 @@ __gen_field(uint64_t v, uint32_t start, uint32_t end) return v << start; } +static inline uint64_t +__gen_fixed(float v, uint32_t start, uint32_t end, + bool is_signed, uint32_t fract_bits) +{ + __gen_validate_value(v); + + const float factor = (1 << fract_bits); + + float max, min; + if (is_signed) { + max = ((1 << (end - start)) - 1) / factor; + min = -(1 << (end - start)) / factor; + } else { + max = ((1 << (end - start + 1)) - 1) / factor; + min = 0.0f; + } + + if (v > max) + v = max; + else if (v < min) + v = min; + + int32_t int_val = roundf(v * factor); + + if (is_signed) + int_val &= (1 << (end - start + 1)) - 1; + + return int_val << start; +} + static inline uint64_t __gen_offset(uint64_t v, uint32_t start, uint32_t end) { @@ -8666,7 +8696,7 @@ GEN8_SAMPLER_STATE_pack(__gen_user_data *data, void * restrict dst, __gen_field(values->MipModeFilter, 20, 21) | __gen_field(values->MagModeFilter, 17, 19) | __gen_field(values->MinModeFilter, 14, 16) | - __gen_field(values->TextureLODBias * (1 << 8), 1, 13) | + __gen_fixed(values->TextureLODBias, 1, 13, true, 8) | __gen_field(values->AnisotropicAlgorithm, 0, 0) | 0; diff --git a/src/vulkan/gen9_pack.h b/src/vulkan/gen9_pack.h index fabcd7bb1e7..db54e9cf85b 100644 --- a/src/vulkan/gen9_pack.h +++ b/src/vulkan/gen9_pack.h @@ -62,6 +62,36 @@ __gen_field(uint64_t v, uint32_t start, uint32_t end) return v << start; } +static inline uint64_t +__gen_fixed(float v, uint32_t start, uint32_t end, + bool is_signed, uint32_t fract_bits) +{ + __gen_validate_value(v); + + const float factor = (1 << fract_bits); + + float max, min; + if (is_signed) { + max = ((1 << (end - start)) - 1) / factor; + min = -(1 << (end - start)) / factor; + } else { + max = ((1 << (end - start + 1)) - 1) / factor; + min = 0.0f; + } + + if (v > max) + v = max; + else if (v < min) + v = min; + + int32_t int_val = roundf(v * factor); + + if (is_signed) + int_val &= (1 << (end - start + 1)) - 1; + + return int_val << start; +} + static inline uint64_t __gen_offset(uint64_t v, uint32_t start, uint32_t end) { @@ -9232,7 +9262,7 @@ GEN9_SAMPLER_STATE_pack(__gen_user_data *data, void * restrict dst, __gen_field(values->MipModeFilter, 20, 21) | __gen_field(values->MagModeFilter, 17, 19) | __gen_field(values->MinModeFilter, 14, 16) | - __gen_field(values->TextureLODBias * (1 << 8), 1, 13) | + __gen_fixed(values->TextureLODBias, 1, 13, true, 8) | __gen_field(values->AnisotropicAlgorithm, 0, 0) | 0; -- 2.30.2