summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/common/bit_util.h44
-rw-r--r--src/video_core/texture_cache/surface_params.cpp19
2 files changed, 50 insertions, 13 deletions
diff --git a/src/common/bit_util.h b/src/common/bit_util.h
index d032df413..6f7d5a947 100644
--- a/src/common/bit_util.h
+++ b/src/common/bit_util.h
@@ -97,4 +97,48 @@ inline u32 CountTrailingZeroes64(u64 value) {
97} 97}
98#endif 98#endif
99 99
100#ifdef _MSC_VER
101
102inline u32 MostSignificantBit32(const u32 value) {
103 unsigned long result;
104 _BitScanReverse(&result, value);
105 return static_cast<u32>(result);
106}
107
108inline u32 MostSignificantBit64(const u64 value) {
109 unsigned long result;
110 _BitScanReverse64(&result, value);
111 return static_cast<u32>(result);
112}
113
114#else
115
116inline u32 MostSignificantBit32(const u32 value) {
117 return 31U - static_cast<u32>(__builtin_clz(value));
118}
119
120inline u32 MostSignificantBit64(const u64 value) {
121 return 63U - static_cast<u32>(__builtin_clzll(value));
122}
123
124#endif
125
126inline u32 Log2Floor32(const u32 value) {
127 return MostSignificantBit32(value);
128}
129
130inline u32 Log2Ceil32(const u32 value) {
131 const u32 log2_f = Log2Floor32(value);
132 return log2_f + ((value ^ (1U << log2_f)) != 0U);
133}
134
135inline u32 Log2Floor64(const u64 value) {
136 return MostSignificantBit64(value);
137}
138
139inline u32 Log2Ceil64(const u64 value) {
140 const u64 log2_f = static_cast<u64>(Log2Floor64(value));
141 return static_cast<u32>(log2_f + ((value ^ (1ULL << log2_f)) != 0ULL));
142}
143
100} // namespace Common 144} // namespace Common
diff --git a/src/video_core/texture_cache/surface_params.cpp b/src/video_core/texture_cache/surface_params.cpp
index 3a47f404d..e7e671d8c 100644
--- a/src/video_core/texture_cache/surface_params.cpp
+++ b/src/video_core/texture_cache/surface_params.cpp
@@ -5,6 +5,7 @@
5#include <map> 5#include <map>
6 6
7#include "common/alignment.h" 7#include "common/alignment.h"
8#include "common/bit_util.h"
8#include "common/cityhash.h" 9#include "common/cityhash.h"
9#include "core/core.h" 10#include "core/core.h"
10#include "video_core/engines/shader_bytecode.h" 11#include "video_core/engines/shader_bytecode.h"
@@ -190,11 +191,8 @@ u32 SurfaceParams::GetMipBlockHeight(u32 level) const {
190 const u32 height{GetMipHeight(level)}; 191 const u32 height{GetMipHeight(level)};
191 const u32 default_block_height{GetDefaultBlockHeight()}; 192 const u32 default_block_height{GetDefaultBlockHeight()};
192 const u32 blocks_in_y{(height + default_block_height - 1) / default_block_height}; 193 const u32 blocks_in_y{(height + default_block_height - 1) / default_block_height};
193 u32 block_height = 4; 194 const u32 block_height = Common::Log2Ceil32(blocks_in_y);
194 while (block_height > 0 && blocks_in_y <= (1U << block_height) * 4) { 195 return std::clamp(block_height, 3U, 8U) - 3U;
195 --block_height;
196 }
197 return block_height;
198} 196}
199 197
200u32 SurfaceParams::GetMipBlockDepth(u32 level) const { 198u32 SurfaceParams::GetMipBlockDepth(u32 level) const {
@@ -206,15 +204,10 @@ u32 SurfaceParams::GetMipBlockDepth(u32 level) const {
206 } 204 }
207 205
208 const u32 depth{GetMipDepth(level)}; 206 const u32 depth{GetMipDepth(level)};
209 u32 block_depth = 5; 207 const u32 block_depth = Common::Log2Ceil32(depth);
210 while (block_depth > 0 && depth * 2 <= (1U << block_depth)) { 208 if (block_depth > 4) {
211 --block_depth; 209 return 5 - (GetMipBlockHeight(level) >= 2);
212 }
213
214 if (block_depth == 5 && GetMipBlockHeight(level) >= 2) {
215 return 4;
216 } 210 }
217
218 return block_depth; 211 return block_depth;
219} 212}
220 213