Blender V5.3
texture_view_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/*
6 * Set of tests to identify issues with glTextureView and glGetTexImage. Note; these tests
7 * rely on device-to-host data conversion (f16 -> f32) for texture readback, which is only
8 * currently supported on OpenGL. Hence, they are only enabled on OpenGL.
9 */
10
11#include "gpu_testing.hh"
12
13#include "MEM_guardedalloc.h"
14
16#include "BLI_vector.hh"
17
18#include "GPU_context.hh"
19#include "GPU_framebuffer.hh"
20#include "GPU_state.hh"
21#include "GPU_texture.hh"
23
24namespace blender::gpu::tests {
25
26/* Repeat the first `components` components of a float4 `n` times, into a vector. */
27template<typename T> static Vector<T> repeat_data(VecBase<T, 4> data, size_t n, size_t components)
28{
29 Vector<T> out(n * components);
30 for (uint i = 0; i < out.size(); ++i) {
31 out[i] = data[i % components];
32 }
33 return out;
34}
35
36/* Create a base texture of the specified format and clear it to black. */
38 uint2 size,
39 int mip_len = 1,
40 int layer_len = 0,
41 const float *data = nullptr)
42{
45
46 gpu::Texture *base = nullptr;
47 if (layer_len == 0) {
48 base = GPU_texture_create_2d("base", size.x, size.y, mip_len, format, usage, data);
49 }
50 else {
52 "base", size.x, size.y, layer_len, mip_len, format, usage, data);
53 }
54
55 GPU_texture_mipmap_mode(base, false, false);
57
58 if (!data) {
59 /* Bind the texture as attachment to a temporary framebuffer, and clear to black. */
60 gpu::FrameBuffer *fbo = nullptr;
63 GPU_framebuffer_clear(fbo, GPUFrameBufferBits::GPU_COLOR_BIT, {0.0, 0.0, 0.0, 0.0}, 0.0f, 0u);
66 }
67
68 return base;
69}
70
71/* Create a view texture of compatible aliasing format. */
73 gpu::Texture *base,
74 int mip = 0,
75 int layer = 0)
76{
78 "view", base, format, mip, 1, layer, 1, false, false);
79 GPU_texture_mipmap_mode(view, false, false);
81 return view;
82}
83
84/* Read back a texture into a contiguous vector of T. */
85template<typename T>
86static Vector<T> read_texture(gpu::Texture *texture, eGPUDataFormat data_format, int mip_level = 0)
87{
88 void *src = GPU_texture_read(texture, data_format, mip_level);
89 Vector<T> dst;
90 dst.resize(GPU_texture_read_size_get(texture, data_format, mip_level) / sizeof(T));
91 std::memcpy(static_cast<void *>(dst.data()), src, sizeof(T) * dst.size());
92
93 MEM_delete_void(src);
94
95 return dst;
96}
97
98/* Given a pair of TextureFormat values, create base and view textures and
99 * attempt to perform a framebuffer color clear over the view texture. */
100template<TextureFormat FormatA, TextureFormat FormatB>
102{
104
105 /* Test operates on a 4x4 texture patch. */
106 const uint2 texture_size = uint2(4);
107 const uint pixel_count = texture_size.x * texture_size.y;
108
109 /* Float comparator threshold; half-to-full conversion has significant precision loss. */
110 constexpr float tolerance = 1e5f;
111
112 gpu::Texture *base = create_base_texture(FormatA, texture_size);
113 gpu::Texture *view = create_view_texture(FormatB, base);
114
115 /* First check; the view texture should be all zeroes. */
116 float4 zero(0.0f, 0.0f, 0.0f, 0.0f);
118 uint4 uzero(zero);
119 auto zero_expected = repeat_data(uzero, pixel_count, to_component_len(FormatB));
120 auto zero_readback = read_texture<uint>(view, GPU_DATA_UINT);
121 EXPECT_EQ(zero_expected, zero_readback);
122 }
123 else if (to_texture_data_format(FormatB) == GPU_DATA_INT) {
124 int4 izero(zero);
125 auto zero_expected = repeat_data(izero, pixel_count, to_component_len(FormatB));
126 auto zero_readback = read_texture<int>(view, GPU_DATA_INT);
127 EXPECT_EQ(zero_expected, zero_readback);
128 }
130 auto zero_expected = repeat_data(zero, pixel_count, to_component_len(FormatB));
131 auto zero_readback = read_texture<float>(view, GPU_DATA_FLOAT);
132 EXPECT_NEAR_SPAN(zero_expected.as_span(), zero_readback.as_span(), tolerance);
133 }
134 else {
136 }
137
138 /* Create FBO with view as color attachment 0. */
139 gpu::FrameBuffer *fbo = nullptr;
142
143 /* Clear FBO to specific color with a different value on each channel. */
145 float4(0.75f, 0.5f, 0.25f, 0.0f) :
146 float4(127.0f, 31.0f, 14.0f, 15.0f);
149
150 /* Second check; the view texture should read back this color. */
152 uint4 ucolr(colr);
153 auto colr_expected = repeat_data(ucolr, pixel_count, to_component_len(FormatB));
154 auto colr_readback = read_texture<uint>(view, GPU_DATA_UINT);
155 EXPECT_EQ(colr_expected, colr_readback);
156 }
157 else if (to_texture_data_format(FormatB) == GPU_DATA_INT) {
158 int4 icolr(colr);
159 auto colr_expected = repeat_data(icolr, pixel_count, to_component_len(FormatB));
160 auto colr_readback = read_texture<int>(view, GPU_DATA_INT);
161 EXPECT_EQ(colr_expected, colr_readback);
162 }
164 auto colr_expected = repeat_data(colr, pixel_count, to_component_len(FormatB));
165 auto colr_readback = read_texture<float>(view, GPU_DATA_FLOAT);
166 EXPECT_NEAR_SPAN(colr_expected.as_span(), colr_readback.as_span(), tolerance);
167 }
168 else {
170 }
171
173 GPU_texture_free(view);
174 GPU_texture_free(base);
175
177}
178
180{
181 texture_view_create_format_test<TextureFormat::SFLOAT_32_32_32_32,
182 TextureFormat::SFLOAT_32_32_32_32>();
183 texture_view_create_format_test<TextureFormat::SFLOAT_32_32_32_32,
184 TextureFormat::UINT_32_32_32_32>();
185 texture_view_create_format_test<TextureFormat::SFLOAT_32_32_32_32,
186 TextureFormat::SINT_32_32_32_32>();
187}
188GPU_TEST(texture_view_SFLOAT_32_32_32_32);
189
206GPU_TEST(texture_view_SFLOAT_32_32);
207
209{
211 GTEST_SKIP() << "Broken on AMD.";
212 }
213
226
227 /* Note the special formats. */
230
231 /* Skipped: readback is not handled as we store these in reverse order. */
232 // texture_view_create_format_test<TextureFormat::SFLOAT_32, TextureFormat::UINT_10_10_10_2>();
233 // texture_view_create_format_test<TextureFormat::SFLOAT_32, TextureFormat::UNORM_10_10_10_2>();
234}
235GPU_TEST(texture_view_SFLOAT_32);
236
253GPU_TEST(texture_view_SFLOAT_16);
254
262GPU_TEST(texture_view_UINT_8);
263
265{
267
268 /* Test operates on a 4x4 texture patch. */
269 const uint2 texture_size = uint2(4);
270 const TextureFormat format = TextureFormat::UINT_8_8;
271
272 auto mip_size = [&](int mip) { return int3(texture_size.x >> mip, texture_size.y >> mip, 1); };
273
274 auto layer_mip_data = [&](int layer, int mip) {
275 int3 size = mip_size(mip);
276 return repeat_data(uint4(layer, layer == 0 ? 0 : mip, 0, 0), size.x * size.y, 2);
277 };
278
279 gpu::Texture *base = create_base_texture(format, texture_size, 3, 3);
280
281 /* Clear everything. Layer 0 will be kept like this. */
282 base->clear(double4(0, 0, 0, 0));
283
284 for (int mip : IndexRange(3)) {
285 /* Upload to layer 1 through base texture */
286 if (GPU_backend_get_type() == GPU_BACKEND_METAL && mip != 0) {
287 /* The Metal API doesn't support this.
288 * > Updating texture layers other than mip=0 when data is mismatched is not possible in
289 * > METAL on macOS using texture->write.
290 * Use a clear instead. :/ */
291 gpu::Texture *layer_1_view = create_view_texture(format, base, mip, 1);
292 layer_1_view->clear(double4(1, mip, 0, 0));
293 GPU_texture_free(layer_1_view);
294 }
295 else {
296 base->update_sub(mip,
297 int3(0, 0, 1),
298 mip_size(mip),
300 layer_mip_data(1, mip).data());
301 }
302
303 /* Clear layer 2 using a layer and mip view. */
304 gpu::Texture *layer_2_view = create_view_texture(format, base, mip, 2);
305 layer_2_view->clear(double4(2, mip, 0, 0));
306 GPU_texture_free(layer_2_view);
307 }
308
309 gpu::Texture *copy = create_base_texture(format, texture_size, 3, 4);
310
311 for (int layer : IndexRange(3)) {
312 for (int mip : IndexRange(3)) {
313 auto expected_data = layer_mip_data(layer, mip);
314
315 gpu::Texture *base_view = create_view_texture(format, base, mip, layer);
316 auto base_readback = read_texture<uint>(base_view, GPU_DATA_UINT, 0);
317
318 EXPECT_EQ(expected_data, base_readback);
319
320 gpu::Texture *copy_view = create_view_texture(format, copy, mip, 3 - layer);
321 GPU_texture_copy(copy_view, base_view);
322 auto copy_readback = read_texture<uint>(copy_view, GPU_DATA_UINT, 0);
323
324 EXPECT_EQ(expected_data, copy_readback);
325
326 GPU_texture_free(base_view);
327 GPU_texture_free(copy_view);
328 }
329 }
330
331 GPU_texture_free(base);
332 GPU_texture_free(copy);
333
335}
336GPU_TEST(texture_view_mip_layer_test);
337
338} // namespace blender::gpu::tests
#define GPU_ATTACHMENT_TEXTURE(_texture)
#define GPU_ATTACHMENT_NONE
#define GPU_framebuffer_ensure_config(_fb,...)
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define ELEM(...)
constexpr IndexRange()=default
virtual void clear(const double4 data)=0
virtual void update_sub(int mip, int offset[3], int extent[3], eGPUDataFormat format, const void *data, uint unpack_row_length=0)=0
#define layer
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
#define T
static Vector< T > repeat_data(VecBase< T, 4 > data, size_t n, size_t components)
static void test_texture_view_SFLOAT_16()
static gpu::Texture * create_base_texture(TextureFormat format, uint2 size, int mip_len=1, int layer_len=0, const float *data=nullptr)
static void test_texture_view_SFLOAT_32()
static Vector< T > read_texture(gpu::Texture *texture, eGPUDataFormat data_format, int mip_level=0)
static gpu::Texture * create_view_texture(TextureFormat format, gpu::Texture *base, int mip=0, int layer=0)
static void test_texture_view_SFLOAT_32_32()
static void test_texture_view_UINT_8()
static void test_texture_view_SFLOAT_32_32_32_32()
static void test_texture_view_mip_layer_test()
GPU_TEST(compute_indirect)
static void texture_view_create_format_test()
eGPUDataFormat to_texture_data_format(TextureFormat tex_format)
int to_component_len(TextureFormat format)
VecBase< uint32_t, 2 > uint2
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
size_t GPU_texture_read_size_get(const gpu::Texture *texture, eGPUDataFormat data_format, int mip_level)
bool GPU_type_matches_ex(GPUDeviceType device, GPUOSType os, GPUDriverType driver, GPUBackendType backend)
void GPU_framebuffer_clear(gpu::FrameBuffer *fb, GPUFrameBufferBits buffers, const double4 clear_col, float clear_depth, unsigned int clear_stencil)
unsigned int uint
Definition BKE_text.h:55
void GPU_framebuffer_free(gpu::FrameBuffer *fb)
GPUBackendType GPU_backend_get_type()
@ GPU_DATA_2_10_10_10_REV
@ GPU_DATA_10_11_11_REV
VecBase< uint32_t, 4 > uint4
void GPU_texture_copy(gpu::Texture *dst, gpu::Texture *src)
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()
void MEM_delete_void(void *vmemh)
void GPU_texture_mipmap_mode(gpu::Texture *texture, bool use_mipmap, bool use_filter)
@ GPU_TEXTURE_USAGE_FORMAT_VIEW
@ GPU_TEXTURE_USAGE_GENERAL
@ 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_BARRIER_FRAMEBUFFER
Definition GPU_state.hh:36
@ GPU_BARRIER_TEXTURE_UPDATE
Definition GPU_state.hh:42
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
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_view(const char *name, gpu::Texture *source_texture, gpu::TextureFormat view_format, int mip_start, int mip_len, int layer_start, int layer_len, bool cube_as_array, bool use_stencil)
VecBase< double, 4 > double4
gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
int size() const
Definition vector.hh:130
void resize(int new_size)
Definition vector.hh:94