1 /* SPDX-License-Identifier: MIT */
2
3 #include "util/box.h"
4 #include "u_surface.h"
5 #include <gtest/gtest.h>
6
7 struct pipe_resource
test_resource_with_format(pipe_format f)8 test_resource_with_format(pipe_format f)
9 {
10 struct pipe_resource rsc;
11 memset(&rsc, 0, sizeof(rsc));
12
13 rsc.width0 = 1;
14 rsc.height0 = 1;
15 rsc.depth0 = 1;
16 rsc.format = f;
17
18 return rsc;
19 }
20
21 static struct pipe_blit_info
test_blit_with_formats(struct pipe_resource * src,pipe_format src_format,struct pipe_resource * dst,pipe_format dst_format)22 test_blit_with_formats(struct pipe_resource *src, pipe_format src_format,
23 struct pipe_resource *dst, pipe_format dst_format)
24 {
25 struct pipe_blit_info info;
26 memset(&info, 0, sizeof(info));
27
28 info.dst.resource = dst;
29 info.dst.format = dst_format;
30
31 info.src.resource = src;
32 info.src.format = src_format;
33
34 info.mask = PIPE_MASK_RGBA;
35 info.filter = PIPE_TEX_FILTER_NEAREST;
36 info.scissor_enable = false;
37 info.alpha_blend = false,
38
39 u_box_2d(0, 0, src->width0, src->height0, &info.src.box);
40 u_box_2d(0, 0, dst->width0, dst->height0, &info.dst.box);
41
42 return info;
43 }
44
TEST(util_can_blit_via_copy_region,formats)45 TEST(util_can_blit_via_copy_region, formats)
46 {
47 struct pipe_resource src_rgba8_unorm = test_resource_with_format(PIPE_FORMAT_R8G8B8A8_UNORM);
48 struct pipe_resource dst_rgba8_unorm = test_resource_with_format(PIPE_FORMAT_R8G8B8A8_UNORM);
49 struct pipe_resource src_rgbx8_unorm = test_resource_with_format(PIPE_FORMAT_R8G8B8X8_UNORM);
50 struct pipe_resource dst_rgbx8_unorm = test_resource_with_format(PIPE_FORMAT_R8G8B8X8_UNORM);
51 struct pipe_resource dst_rgba8_srgb = test_resource_with_format(PIPE_FORMAT_R8G8B8A8_SRGB);
52
53 /* Same-format blit should pass */
54 struct pipe_blit_info rgba8_unorm_rgba8_unorm_blit = test_blit_with_formats(&src_rgba8_unorm, PIPE_FORMAT_R8G8B8A8_UNORM,
55 &dst_rgba8_unorm, PIPE_FORMAT_R8G8B8A8_UNORM);
56 ASSERT_TRUE(util_can_blit_via_copy_region(&rgba8_unorm_rgba8_unorm_blit, false, false));
57
58 /* Blit that should do sRGB encoding should fail. */
59 struct pipe_blit_info rgba8_unorm_rgba8_srgb_blit = test_blit_with_formats(&src_rgba8_unorm, PIPE_FORMAT_R8G8B8A8_UNORM,
60 &dst_rgba8_unorm, PIPE_FORMAT_R8G8B8A8_SRGB);
61 ASSERT_FALSE(util_can_blit_via_copy_region(&rgba8_unorm_rgba8_srgb_blit, false, false));
62
63 /* RGBA->RGBX is fine, since A is ignored. */
64 struct pipe_blit_info rgba8_unorm_rgbx8_unorm_blit = test_blit_with_formats(&src_rgba8_unorm, PIPE_FORMAT_R8G8B8A8_UNORM,
65 &dst_rgbx8_unorm, PIPE_FORMAT_R8G8B8X8_UNORM);
66 ASSERT_TRUE(util_can_blit_via_copy_region(&rgba8_unorm_rgbx8_unorm_blit, false, false));
67
68 /* RGBX->RGBA is invalid, since src A is undefined. */
69 struct pipe_blit_info rgbx8_unorm_rgba8_unorm_blit = test_blit_with_formats(&src_rgbx8_unorm, PIPE_FORMAT_R8G8B8X8_UNORM,
70 &dst_rgba8_unorm, PIPE_FORMAT_R8G8B8A8_UNORM);
71 ASSERT_FALSE(util_can_blit_via_copy_region(&rgbx8_unorm_rgba8_unorm_blit, false, false));
72
73 /* If the RGBA8_UNORM resources are both viewed as sRGB, it's still a memcpy. */
74 struct pipe_blit_info rgba8_srgb_rgba8_srgb_blit = test_blit_with_formats(&src_rgba8_unorm, PIPE_FORMAT_R8G8B8A8_SRGB,
75 &dst_rgba8_unorm, PIPE_FORMAT_R8G8B8A8_SRGB);
76 ASSERT_TRUE(util_can_blit_via_copy_region(&rgba8_srgb_rgba8_srgb_blit, false, false));
77
78 /* A memcpy blit can't be lowered to copy_region if copy_region would have mismatched resource formats. */
79 struct pipe_blit_info non_memcpy_copy_region = test_blit_with_formats(&src_rgba8_unorm, PIPE_FORMAT_R8G8B8A8_UNORM,
80 &dst_rgba8_srgb, PIPE_FORMAT_R8G8B8A8_UNORM);
81 ASSERT_FALSE(util_can_blit_via_copy_region(&non_memcpy_copy_region, false, false));
82 }
83