Blender V5.3
mipmap_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "gpu_testing.hh"
6
7#include <array>
8
9#include "MEM_guardedalloc.h"
10
11#include "BLI_array.hh"
12#include "BLI_bit_vector.hh"
13#include "BLI_math_vector.hh"
15#include "BLI_rect.hh"
16#include "BLI_span.hh"
17
18#include "GPU_context.hh"
19#include "GPU_shader.hh"
20#include "GPU_shader_builtin.hh"
21#include "GPU_state.hh"
22#include "GPU_texture.hh"
23
24namespace blender::gpu::tests {
25
27 int layer;
29};
30
31static BitVector<> modified_chunks_bitmask(const int2 chunks_size,
32 const Span<ChangedRegion> changed,
33 const int layer)
34{
35 constexpr int chunk_size = GPU_TEXTURE_MIPMAP_UPDATE_CHUNK_SIZE;
36 BitVector<> chunks(chunks_size.x * chunks_size.y, false);
37 for (const ChangedRegion &c : changed) {
38 if (c.layer != layer) {
39 continue;
40 }
41 const rcti &r = c.rect;
42 for (int ty = r.ymin / chunk_size; ty <= (r.ymax - 1) / chunk_size; ty++) {
43 for (int tx = r.xmin / chunk_size; tx <= (r.xmax - 1) / chunk_size; tx++) {
44 chunks[int64_t(ty) * chunks_size.x + tx].set();
45 }
46 }
47 }
48 return chunks;
49}
50
51static float4 to_float4(const float4 &v)
52{
53 return v;
54}
55static float4 to_float4(const uchar4 &v)
56{
57 return float4(v.x, v.y, v.z, v.w) * (1.0f / 255.0f);
58}
59
60template<typename T>
62 const eGPUDataFormat data_format,
63 const eGPUTextureUsage extra_usage,
64 const GPUBuiltinShader mipmap_shader,
65 const Span<T> layer_backgrounds,
66 const T changed_value,
67 const int size,
68 const Span<ChangedRegion> changed,
69 const float background_threshold)
70{
72
73 EXPECT_NE(GPU_shader_get_builtin_shader(mipmap_shader), nullptr)
74 << "mipmap compute shader failed to compile";
75
76 /* Create texture. */
77 int mip_len = 1;
78 while ((size >> mip_len) > 0) {
79 mip_len++;
80 }
81 const int layers = int(layer_backgrounds.size());
83 GPU_TEXTURE_USAGE_HOST_READ | extra_usage;
85 (layers == 1) ?
86 GPU_texture_create_2d("test_mipmap", size, size, mip_len, format, usage, nullptr) :
88 "test_mipmap", size, size, layers, mip_len, format, usage, nullptr);
89
90 /* Fill mip 0 with background color. */
91 const int64_t layer_len = size * size;
92 Array<T> mip0(layer_len * layers);
93 for (int layer = 0; layer < layers; layer++) {
94 mip0.as_mutable_span().slice(layer * layer_len, layer_len).fill(layer_backgrounds[layer]);
95 }
96
97 /* Generate full mip chain. */
98 GPU_texture_update(texture, data_format, mip0.data());
101
102 /* Verify full update preserves color. */
103 for (int mip = 1; mip < mip_len; mip++) {
104 const int mip_size = size >> mip;
105 T *data = static_cast<T *>(GPU_texture_read(texture, data_format, mip));
106 float max_diff = 0.0f;
107 for (int layer = 0; layer < layers; layer++) {
108 const float4 expected = to_float4(layer_backgrounds[layer]);
109 for (int64_t i = 0; i < mip_size * mip_size; i++) {
110 const float4 value = to_float4(data[layer * mip_size * mip_size + i]);
111 max_diff = math::max(max_diff, math::reduce_max(math::abs(value - expected)));
112 }
113 }
114 EXPECT_LT(max_diff, 0.02f) << "mip level " << mip
115 << " of a constant image deviates from the background";
116 MEM_delete(data);
117 }
118
119 /* Upload the changed regions. */
120 for (const ChangedRegion &c : changed) {
121 const int w = BLI_rcti_size_x(&c.rect);
122 const int h = BLI_rcti_size_y(&c.rect);
123 Array<T> patch(w * h);
124 patch.fill(changed_value);
126 texture, data_format, patch.data(), c.rect.xmin, c.rect.ymin, c.layer, w, h, 1);
127 }
129
130 /* Partial update of the changed regions. */
131 const int chunks_len = (size + GPU_TEXTURE_MIPMAP_UPDATE_CHUNK_SIZE - 1) /
133 const int2 chunks_size(chunks_len, chunks_len);
134 for (int layer = 0; layer < layers; layer++) {
135 const BitVector<> modified_chunks = modified_chunks_bitmask(chunks_size, changed, layer);
137 }
139
140 /* Read back mip levels. */
141 Array<Array<T>> partial(mip_len);
142 for (int mip = 1; mip < mip_len; mip++) {
143 const int mip_size = size >> mip;
144 T *data = static_cast<T *>(GPU_texture_read(texture, data_format, mip));
145 partial[mip] = Array<T>(Span<T>(data, mip_size * mip_size * layers));
146 MEM_delete(data);
147 }
148
149 /* Regenerate the whole chain and compare. */
152 for (int mip = 1; mip < mip_len; mip++) {
153 const int mip_size = size >> mip;
154 T *data = static_cast<T *>(GPU_texture_read(texture, data_format, mip));
155 int mismatches = 0;
156 for (int64_t i = 0; i < mip_size * mip_size * layers; i++) {
157 if (partial[mip][i] != data[i]) {
158 mismatches++;
159 }
160 }
161 EXPECT_EQ(mismatches, 0) << "mip level " << mip << " differs between partial and full update";
162 MEM_delete(data);
163 }
164
165 /* Check the 1x1 mip of each layer is close to the background color. */
166 {
167 T *data = static_cast<T *>(GPU_texture_read(texture, data_format, mip_len - 1));
168 for (int layer = 0; layer < layers; layer++) {
169 const float diff = math::reduce_max(
170 math::abs(to_float4(data[layer]) - to_float4(layer_backgrounds[layer])));
171 EXPECT_LT(diff, background_threshold)
172 << "coarsest mip of layer " << layer << " is not close to the background";
173 }
174 MEM_delete(data);
175 }
176
179}
180
181static const std::array<ChangedRegion, 3> test_changed_regions = {
182 {{0, {37, 87, 41, 87}}, {0, {300, 344, 320, 359}}, {0, {256, 281, 256, 281}}}};
183
185{
186 test_mipmap_partial_matches_full<float4>(TextureFormat::SFLOAT_32_32_32_32,
190 {float4(0.1f, 0.4f, 0.7f, 1.0f)},
191 float4(0.9f, 0.2f, 0.5f, 1.0f),
194 0.4f);
195}
196GPU_TEST(mipmap_partial_float_pow2)
197
199{
200 test_mipmap_partial_matches_full<float4>(TextureFormat::SFLOAT_32_32_32_32,
204 {float4(0.1f, 0.4f, 0.7f, 1.0f)},
205 float4(0.9f, 0.2f, 0.5f, 1.0f),
206 511,
208 0.4f);
209}
210GPU_TEST(mipmap_partial_npot)
211
213{
214 test_mipmap_partial_matches_full<uchar4>(TextureFormat::SRGBA_8_8_8_8,
218 {uchar4(30, 110, 200, 255)},
219 uchar4(230, 50, 130, 255),
222 0.4f);
223}
224GPU_TEST(mipmap_partial_srgb_pow2)
225
227{
228 test_mipmap_partial_matches_full<float4>(TextureFormat::SFLOAT_32_32_32_32,
232 {float4(0.1f, 0.3f, 0.6f, 1.0f),
233 float4(0.2f, 0.3f, 0.6f, 1.0f),
234 float4(0.3f, 0.3f, 0.6f, 1.0f)},
235 float4(0.9f, 0.2f, 0.5f, 1.0f),
237 {{0, {37, 87, 41, 87}}, {2, {300, 344, 320, 359}}},
238 0.05f);
239}
240GPU_TEST(mipmap_partial_layered)
241
242} // namespace blender::gpu::tests
Read Blender memory allocation and freeing.
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
Array(Allocator allocator={}) noexcept
Definition BLI_array.hh:78
const T * data() const
Definition BLI_array.hh:323
void fill(const T &value) const
Definition BLI_array.hh:283
constexpr int64_t size() const
Definition BLI_span.hh:256
IMETHOD Vector diff(const Vector &a, const Vector &b, double dt)
Definition frames.inl:1166
#define layer
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
#define EXPECT_LT(result, expect)
#define EXPECT_NE(result, expect)
#define GPU_TEST(test_name)
#define T
static void test_mipmap_partial_float_pow2()
static void test_mipmap_partial_npot()
static void test_mipmap_partial_srgb_pow2()
static void test_mipmap_partial_layered()
static BitVector modified_chunks_bitmask(const int2 chunks_size, const Span< ChangedRegion > changed, const int layer)
static const std::array< ChangedRegion, 3 > test_changed_regions
static float4 to_float4(const float4 &v)
static void test_mipmap_partial_matches_full(const TextureFormat format, const eGPUDataFormat data_format, const eGPUTextureUsage extra_usage, const GPUBuiltinShader mipmap_shader, const Span< T > layer_backgrounds, const T changed_value, const int size, const Span< ChangedRegion > changed, const float background_threshold)
T reduce_max(const VecBase< T, Size > &a)
T max(const T &a, const T &b)
T abs(const T &a)
VecBase< uint8_t, 4 > uchar4
__int64 int64_t
Definition BKE_text.h:90
void GPU_texture_update(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
void GPU_texture_update_mipmap_chain(gpu::Texture *texture)
constexpr int GPU_TEXTURE_MIPMAP_UPDATE_CHUNK_SIZE
gpu::Texture * GPU_texture_create_2d_array(const char *name, int width, int height, int layer_len, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
void GPU_render_begin()
ATTR_WARN_UNUSED_RESULT const BMVert * v
void GPU_texture_update_sub(gpu::Texture *texture, eGPUDataFormat data_format, const void *pixels, int offset_x, int offset_y, int offset_z, int width, int height, int depth, uint unpack_row_length=0)
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
@ GPU_TEXTURE_USAGE_FORMAT_VIEW
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_SHADER_WRITE
@ GPU_TEXTURE_USAGE_HOST_READ
void GPU_render_end()
void GPU_memory_barrier(GPUBarrier barrier)
Definition gpu_state.cc:305
BMesh const char void * data
@ GPU_SHADER_2D_UPDATE_MIPMAPS_SFLOAT_32_32_32_32_LAYERED
@ GPU_SHADER_2D_UPDATE_MIPMAPS_SRGBA_8_8_8_8
@ GPU_SHADER_2D_UPDATE_MIPMAPS_SFLOAT_32_32_32_32
void GPU_texture_update_mipmap_chain_partial(gpu::Texture *texture, int layer, BitSpan modified_chunks)
@ GPU_BARRIER_TEXTURE_UPDATE
Definition GPU_state.hh:42
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
gpu::Shader * GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
void GPU_texture_free(gpu::Texture *texture)
void * GPU_texture_read(gpu::Texture *texture, eGPUDataFormat data_format, int mip_level)
gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)