| 1 |
/* |
| 2 |
* Copyright (C) 2021 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "pan_blend.h" |
| 7 |
|
| 8 |
/* A test consists of a given blend mode and its translated form */ |
| 9 |
struct test { |
| 10 |
const char *label; |
| 11 |
struct pan_blend_equation eq; |
| 12 |
unsigned constant_mask; |
| 13 |
bool reads_dest; |
| 14 |
bool opaque; |
| 15 |
bool fixed_function; |
| 16 |
bool alpha_zero_nop; |
| 17 |
bool alpha_one_store; |
| 18 |
uint32_t hardware; |
| 19 |
}; |
| 20 |
|
| 21 |
/* clang-format off */ |
| 22 |
#define RGBA(key, value) \ |
| 23 |
.rgb_ ## key = value, \ |
| 24 |
.alpha_ ## key = value |
| 25 |
|
| 26 |
static const struct test blend_tests[] = { |
| 27 |
{ |
| 28 |
"Replace", |
| 29 |
{ |
| 30 |
.blend_enable = false, |
| 31 |
.color_mask = 0xF, |
| 32 |
}, |
| 33 |
.constant_mask = 0x0, |
| 34 |
.reads_dest = false, |
| 35 |
.opaque = true, |
| 36 |
.fixed_function = true, |
| 37 |
.alpha_zero_nop = false, |
| 38 |
.alpha_one_store = false, |
| 39 |
.hardware = 0xF0122122 |
| 40 |
}, |
| 41 |
{ |
| 42 |
"Alpha", |
| 43 |
{ |
| 44 |
.blend_enable = true, |
| 45 |
.color_mask = 0xF, |
| 46 |
|
| 47 |
RGBA(func, PIPE_BLEND_ADD), |
| 48 |
RGBA(src_factor, PIPE_BLENDFACTOR_SRC_ALPHA), |
| 49 |
RGBA(dst_factor, PIPE_BLENDFACTOR_INV_SRC_ALPHA), |
| 50 |
}, |
| 51 |
.constant_mask = 0x0, |
| 52 |
.reads_dest = true, |
| 53 |
.opaque = false, |
| 54 |
.fixed_function = true, |
| 55 |
.alpha_zero_nop = true, |
| 56 |
.alpha_one_store = true, |
| 57 |
.hardware = 0xF0503503 |
| 58 |
}, |
| 59 |
{ |
| 60 |
"Additive", |
| 61 |
{ |
| 62 |
.blend_enable = true, |
| 63 |
.color_mask = 0xF, |
| 64 |
|
| 65 |
RGBA(func, PIPE_BLEND_ADD), |
| 66 |
RGBA(src_factor, PIPE_BLENDFACTOR_ONE), |
| 67 |
RGBA(dst_factor, PIPE_BLENDFACTOR_ONE), |
| 68 |
}, |
| 69 |
.constant_mask = 0x0, |
| 70 |
.reads_dest = true, |
| 71 |
.opaque = false, |
| 72 |
.fixed_function = true, |
| 73 |
.alpha_zero_nop = false, |
| 74 |
.alpha_one_store = false, |
| 75 |
.hardware = 0xF0932932 /* equivalently 0xF0923923 */ |
| 76 |
}, |
| 77 |
{ |
| 78 |
"Additive-Alpha", |
| 79 |
{ |
| 80 |
.blend_enable = true, |
| 81 |
.color_mask = 0xF, |
| 82 |
|
| 83 |
RGBA(func, PIPE_BLEND_ADD), |
| 84 |
RGBA(src_factor, PIPE_BLENDFACTOR_SRC_ALPHA), |
| 85 |
RGBA(dst_factor, PIPE_BLENDFACTOR_ONE), |
| 86 |
}, |
| 87 |
.constant_mask = 0x0, |
| 88 |
.reads_dest = true, |
| 89 |
.opaque = false, |
| 90 |
.fixed_function = true, |
| 91 |
.alpha_zero_nop = true, |
| 92 |
.alpha_one_store = false, |
| 93 |
.hardware = 0xF0523523 |
| 94 |
}, |
| 95 |
{ |
| 96 |
"Subtractive", |
| 97 |
{ |
| 98 |
.blend_enable = true, |
| 99 |
.color_mask = 0xF, |
| 100 |
|
| 101 |
RGBA(func, PIPE_BLEND_SUBTRACT), |
| 102 |
RGBA(src_factor, PIPE_BLENDFACTOR_ONE), |
| 103 |
RGBA(dst_factor, PIPE_BLENDFACTOR_ONE), |
| 104 |
}, |
| 105 |
.constant_mask = 0x0, |
| 106 |
.reads_dest = true, |
| 107 |
.opaque = false, |
| 108 |
.fixed_function = true, |
| 109 |
.alpha_zero_nop = false, |
| 110 |
.alpha_one_store = false, |
| 111 |
.hardware = 0xF09B29B2 /* equivalently 0xF09A39A3 */ |
| 112 |
}, |
| 113 |
{ |
| 114 |
"Subtractive-Alpha", |
| 115 |
{ |
| 116 |
.blend_enable = true, |
| 117 |
.color_mask = 0xF, |
| 118 |
|
| 119 |
RGBA(func, PIPE_BLEND_SUBTRACT), |
| 120 |
RGBA(src_factor, PIPE_BLENDFACTOR_SRC_ALPHA), |
| 121 |
RGBA(dst_factor, PIPE_BLENDFACTOR_ONE), |
| 122 |
}, |
| 123 |
.constant_mask = 0x0, |
| 124 |
.reads_dest = true, |
| 125 |
.opaque = false, |
| 126 |
.fixed_function = true, |
| 127 |
.alpha_zero_nop = false, |
| 128 |
.alpha_one_store = false, |
| 129 |
.hardware = 0xF052B52b /* equivalently 0xF05A35A3 */ |
| 130 |
}, |
| 131 |
{ |
| 132 |
"Modulate", |
| 133 |
{ |
| 134 |
.blend_enable = true, |
| 135 |
.color_mask = 0xF, |
| 136 |
|
| 137 |
RGBA(func, PIPE_BLEND_ADD), |
| 138 |
RGBA(src_factor, PIPE_BLENDFACTOR_ZERO), |
| 139 |
RGBA(dst_factor, PIPE_BLENDFACTOR_SRC_COLOR), |
| 140 |
}, |
| 141 |
.constant_mask = 0x0, |
| 142 |
.reads_dest = true, |
| 143 |
.opaque = false, |
| 144 |
.fixed_function = true, |
| 145 |
.alpha_zero_nop = false, |
| 146 |
.alpha_one_store = false, |
| 147 |
.hardware = 0xF0231231 /* equivalently 0xF0321321 */ |
| 148 |
}, |
| 149 |
{ |
| 150 |
"Replace masked", |
| 151 |
{ |
| 152 |
.blend_enable = false, |
| 153 |
.color_mask = 0x3, |
| 154 |
}, |
| 155 |
.constant_mask = 0x0, |
| 156 |
.reads_dest = true, |
| 157 |
.opaque = false, |
| 158 |
.fixed_function = true, |
| 159 |
.alpha_zero_nop = false, |
| 160 |
.alpha_one_store = false, |
| 161 |
.hardware = 0x30122122 |
| 162 |
}, |
| 163 |
{ |
| 164 |
"Modulate masked", |
| 165 |
{ |
| 166 |
.blend_enable = true, |
| 167 |
.color_mask = 0xA, |
| 168 |
|
| 169 |
RGBA(func, PIPE_BLEND_ADD), |
| 170 |
RGBA(src_factor, PIPE_BLENDFACTOR_ZERO), |
| 171 |
RGBA(dst_factor, PIPE_BLENDFACTOR_SRC_COLOR), |
| 172 |
}, |
| 173 |
.constant_mask = 0x0, |
| 174 |
.reads_dest = true, |
| 175 |
.opaque = false, |
| 176 |
.fixed_function = true, |
| 177 |
.alpha_zero_nop = false, |
| 178 |
.alpha_one_store = false, |
| 179 |
.hardware = 0xA0231231 /* equivalently 0xA0321321 */ |
| 180 |
}, |
| 181 |
{ |
| 182 |
"src*dst + dst*src", |
| 183 |
{ |
| 184 |
.blend_enable = true, |
| 185 |
.color_mask = 0xF, |
| 186 |
|
| 187 |
RGBA(func, PIPE_BLEND_ADD), |
| 188 |
RGBA(src_factor, PIPE_BLENDFACTOR_DST_COLOR), |
| 189 |
RGBA(dst_factor, PIPE_BLENDFACTOR_SRC_COLOR), |
| 190 |
}, |
| 191 |
.constant_mask = 0x0, |
| 192 |
.reads_dest = true, |
| 193 |
.opaque = false, |
| 194 |
.fixed_function = true, |
| 195 |
.alpha_zero_nop = false, |
| 196 |
.alpha_one_store = false, |
| 197 |
.hardware = 0xF0431431 /* 0 + dest * (2*src) */ |
| 198 |
}, |
| 199 |
{ |
| 200 |
"Mixed src*dst + dst*src masked I", |
| 201 |
{ |
| 202 |
.blend_enable = true, |
| 203 |
.color_mask = 0xC, |
| 204 |
|
| 205 |
.rgb_func = PIPE_BLEND_ADD, |
| 206 |
.rgb_src_factor = PIPE_BLENDFACTOR_ONE, |
| 207 |
.rgb_dst_factor= PIPE_BLENDFACTOR_ZERO, |
| 208 |
|
| 209 |
.alpha_func = PIPE_BLEND_ADD, |
| 210 |
.alpha_src_factor = PIPE_BLENDFACTOR_DST_COLOR, |
| 211 |
.alpha_dst_factor= PIPE_BLENDFACTOR_SRC_COLOR, |
| 212 |
}, |
| 213 |
.constant_mask = 0x0, |
| 214 |
.reads_dest = true, |
| 215 |
.opaque = false, |
| 216 |
.fixed_function = true, |
| 217 |
.alpha_zero_nop = false, |
| 218 |
.alpha_one_store = false, |
| 219 |
.hardware = 0xC0431132 /* 0 + dest * (2*src); equivalent 0xC0431122 */ |
| 220 |
}, |
| 221 |
{ |
| 222 |
"Mixed src*dst + dst*src masked II", |
| 223 |
{ |
| 224 |
.blend_enable = true, |
| 225 |
.color_mask = 0xC, |
| 226 |
|
| 227 |
.rgb_func = PIPE_BLEND_ADD, |
| 228 |
.rgb_src_factor = PIPE_BLENDFACTOR_ONE, |
| 229 |
.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO, |
| 230 |
|
| 231 |
.alpha_func = PIPE_BLEND_ADD, |
| 232 |
.alpha_src_factor = PIPE_BLENDFACTOR_DST_ALPHA, |
| 233 |
.alpha_dst_factor= PIPE_BLENDFACTOR_SRC_COLOR, |
| 234 |
}, |
| 235 |
.constant_mask = 0x0, |
| 236 |
.reads_dest = true, |
| 237 |
.opaque = false, |
| 238 |
.fixed_function = true, |
| 239 |
.alpha_zero_nop = false, |
| 240 |
.alpha_one_store = false, |
| 241 |
.hardware = 0xC0431132 /* 0 + dest * (2*src); equivalent 0xC0431122 */ |
| 242 |
}, |
| 243 |
{ |
| 244 |
"Mixed src*dst + dst*src masked III", |
| 245 |
{ |
| 246 |
.blend_enable = true, |
| 247 |
.color_mask = 0xC, |
| 248 |
|
| 249 |
.rgb_func = PIPE_BLEND_ADD, |
| 250 |
.rgb_src_factor = PIPE_BLENDFACTOR_ONE, |
| 251 |
.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO, |
| 252 |
|
| 253 |
.alpha_func = PIPE_BLEND_ADD, |
| 254 |
.alpha_src_factor = PIPE_BLENDFACTOR_DST_ALPHA, |
| 255 |
.alpha_dst_factor= PIPE_BLENDFACTOR_SRC_ALPHA, |
| 256 |
}, |
| 257 |
.constant_mask = 0x0, |
| 258 |
.reads_dest = true, |
| 259 |
.opaque = false, |
| 260 |
.fixed_function = true, |
| 261 |
.alpha_zero_nop = false, |
| 262 |
.alpha_one_store = false, |
| 263 |
.hardware = 0xC0431132 /* 0 + dest * (2*src); equivalent 0xC0431122 */ |
| 264 |
} |
| 265 |
}; |
| 266 |
/* clang-format on */ |
| 267 |
|
| 268 |
#define ASSERT_EQ(x, y) \ |
| 269 |
do { \ |
| 270 |
if (x == y) { \ |
| 271 |
nr_pass++; \ |
| 272 |
} else { \ |
| 273 |
nr_fail++; \ |
| 274 |
fprintf(stderr, "%s: Assertion failed %s (%x) != %s (%x)\n", T.label, \ |
| 275 |
#x, x, #y, y); \ |
| 276 |
} \ |
| 277 |
} while (0) |
| 278 |
|
| 279 |
int |
| 280 |
main(int argc, const char **argv) |
| 281 |
{ |
| 282 |
unsigned nr_pass = 0, nr_fail = 0; |
| 283 |
|
| 284 |
/* The architecture we test for is arbitrary and used only for checking |
| 285 |
* whether we can use fixed function. If we really wanted to be paranoid |
| 286 |
* we could add a loop checking all architectures, but in practice there's |
| 287 |
* not much difference and we're only checking for internal consistency |
| 288 |
* anyway |
| 289 |
*/ |
| 290 |
unsigned arch = 10; |
| 291 |
|
| 292 |
for (unsigned i = 0; i < ARRAY_SIZE(blend_tests); ++i) { |
| 293 |
struct test T = blend_tests[i]; |
| 294 |
ASSERT_EQ(T.constant_mask, pan_blend_constant_mask(T.eq)); |
| 295 |
ASSERT_EQ(T.reads_dest, pan_blend_reads_dest(T.eq)); |
| 296 |
ASSERT_EQ(T.opaque, pan_blend_is_opaque(T.eq)); |
| 297 |
ASSERT_EQ(T.fixed_function, pan_blend_can_fixed_function(arch, T.eq, true)); |
| 298 |
ASSERT_EQ(T.alpha_zero_nop, pan_blend_alpha_zero_nop(T.eq)); |
| 299 |
ASSERT_EQ(T.alpha_one_store, pan_blend_alpha_one_store(T.eq)); |
| 300 |
|
| 301 |
if (pan_blend_can_fixed_function(arch, T.eq, true)) { |
| 302 |
ASSERT_EQ(T.hardware, pan_pack_blend(T.eq)); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
printf("Passed %u/%u\n", nr_pass, nr_pass + nr_fail); |
| 307 |
return nr_fail ? 1 : 0; |
| 308 |
} |