1 /*
2 * Copyright (C) 2022 Collabora, Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "pan_earlyzs.h"
25 #include "util/pan_ir.h"
26
27 #include <gtest/gtest.h>
28
29 /*
30 * Test the early-ZS helpers used on Bifrost and Valhall. Early-ZS state depends
31 * on both shader state and draw-time API state. As such, there are two helpers
32 * -- analzye and get -- that separate the link-time analysis of a fragment
33 * shader from the draw-time classification. The internal data structure is not
34 * under test, only the external API. So we test only the composition.
35 */
36
37 #define ZS_WRITEMASK BITFIELD_BIT(0)
38 #define ALPHA2COV BITFIELD_BIT(1)
39 #define ZS_ALWAYS_PASSES BITFIELD_BIT(2)
40 #define DISCARD BITFIELD_BIT(3)
41 #define WRITES_Z BITFIELD_BIT(4)
42 #define WRITES_S BITFIELD_BIT(5)
43 #define WRITES_COV BITFIELD_BIT(6)
44 #define SIDEFX BITFIELD_BIT(7)
45 #define API_EARLY BITFIELD_BIT(8)
46
47 static void
test(enum pan_earlyzs expected_update,enum pan_earlyzs expected_kill,uint32_t flags)48 test(enum pan_earlyzs expected_update, enum pan_earlyzs expected_kill, uint32_t flags)
49 {
50 struct pan_shader_info info = {
51 .fs = {
52 .can_discard = !!(flags & DISCARD),
53 .writes_depth = !!(flags & WRITES_Z),
54 .writes_stencil = !!(flags & WRITES_S),
55 .writes_coverage = !!(flags & WRITES_COV),
56 .early_fragment_tests = !!(flags & API_EARLY),
57 },
58 .writes_global = !!(flags & SIDEFX),
59 };
60
61 struct pan_earlyzs_state result =
62 pan_earlyzs_get(pan_earlyzs_analyze(&info),
63 !!(flags & ZS_WRITEMASK),
64 !!(flags & ALPHA2COV),
65 !!(flags & ZS_ALWAYS_PASSES));
66
67 ASSERT_EQ(result.update, expected_update);
68 ASSERT_EQ(result.kill, expected_kill);
69 }
70
71
72 #define CASE(expected_update, expected_kill, flags) \
73 test(PAN_EARLYZS_ ## expected_update, PAN_EARLYZS_ ## expected_kill, flags)
74
TEST(EarlyZS,APIForceEarly)75 TEST(EarlyZS, APIForceEarly)
76 {
77 CASE(FORCE_EARLY, FORCE_EARLY, API_EARLY);
78 CASE(FORCE_EARLY, FORCE_EARLY, API_EARLY | WRITES_Z | WRITES_S);
79 CASE(FORCE_EARLY, FORCE_EARLY, API_EARLY | ALPHA2COV | DISCARD);
80 }
81
TEST(EarlyZS,ShaderCalculatesZS)82 TEST(EarlyZS, ShaderCalculatesZS)
83 {
84 CASE(FORCE_LATE, FORCE_LATE, WRITES_Z);
85 CASE(FORCE_LATE, FORCE_LATE, WRITES_S);
86 CASE(FORCE_LATE, FORCE_LATE, WRITES_Z | WRITES_S);
87 CASE(FORCE_LATE, FORCE_LATE, WRITES_Z | WRITES_S | SIDEFX);
88 CASE(FORCE_LATE, FORCE_LATE, WRITES_Z | WRITES_S | ZS_ALWAYS_PASSES);
89 CASE(FORCE_LATE, FORCE_LATE, WRITES_Z | ZS_ALWAYS_PASSES | ALPHA2COV);
90 }
91
TEST(EarlyZS,ModifiesCoverageWritesZSNoSideFX)92 TEST(EarlyZS, ModifiesCoverageWritesZSNoSideFX)
93 {
94 CASE(FORCE_LATE, FORCE_EARLY, ZS_WRITEMASK | WRITES_COV);
95 CASE(FORCE_LATE, FORCE_EARLY, ZS_WRITEMASK | DISCARD);
96 CASE(FORCE_LATE, FORCE_EARLY, ZS_WRITEMASK | ALPHA2COV);
97 CASE(FORCE_LATE, FORCE_EARLY, ZS_WRITEMASK | WRITES_COV | DISCARD | ALPHA2COV);
98 }
99
TEST(EarlyZS,ModifiesCoverageWritesZSNoSideFXAlt)100 TEST(EarlyZS, ModifiesCoverageWritesZSNoSideFXAlt)
101 {
102 CASE(FORCE_LATE, WEAK_EARLY, ZS_ALWAYS_PASSES | ZS_WRITEMASK | WRITES_COV);
103 CASE(FORCE_LATE, WEAK_EARLY, ZS_ALWAYS_PASSES | ZS_WRITEMASK | DISCARD);
104 CASE(FORCE_LATE, WEAK_EARLY, ZS_ALWAYS_PASSES | ZS_WRITEMASK | ALPHA2COV);
105 CASE(FORCE_LATE, WEAK_EARLY, ZS_ALWAYS_PASSES | ZS_WRITEMASK | WRITES_COV | DISCARD | ALPHA2COV);
106 }
107
TEST(EarlyZS,ModifiesCoverageWritesZSSideFX)108 TEST(EarlyZS, ModifiesCoverageWritesZSSideFX)
109 {
110 CASE(FORCE_LATE, FORCE_LATE, ZS_WRITEMASK | SIDEFX | WRITES_COV);
111 CASE(FORCE_LATE, FORCE_LATE, ZS_WRITEMASK | SIDEFX | DISCARD);
112 CASE(FORCE_LATE, FORCE_LATE, ZS_WRITEMASK | SIDEFX | ALPHA2COV);
113 CASE(FORCE_LATE, FORCE_LATE, ZS_WRITEMASK | SIDEFX | WRITES_COV | DISCARD | ALPHA2COV);
114 }
115
TEST(EarlyZS,SideFXNoShaderZS)116 TEST(EarlyZS, SideFXNoShaderZS)
117 {
118 CASE(FORCE_EARLY, FORCE_LATE, SIDEFX);
119 CASE(FORCE_EARLY, FORCE_LATE, SIDEFX | DISCARD);
120 CASE(FORCE_EARLY, FORCE_LATE, SIDEFX | WRITES_COV);
121 CASE(FORCE_EARLY, FORCE_LATE, SIDEFX | ALPHA2COV);
122 }
123
TEST(EarlyZS,SideFXNoShaderZSAlt)124 TEST(EarlyZS, SideFXNoShaderZSAlt)
125 {
126 CASE(WEAK_EARLY, FORCE_LATE, ZS_ALWAYS_PASSES | SIDEFX);
127 CASE(WEAK_EARLY, FORCE_LATE, ZS_ALWAYS_PASSES | SIDEFX | DISCARD);
128 CASE(WEAK_EARLY, FORCE_LATE, ZS_ALWAYS_PASSES | SIDEFX | WRITES_COV);
129 CASE(WEAK_EARLY, FORCE_LATE, ZS_ALWAYS_PASSES | SIDEFX | ALPHA2COV);
130 }
131
TEST(EarlyZS,NoSideFXNoShaderZS)132 TEST(EarlyZS, NoSideFXNoShaderZS)
133 {
134 CASE(FORCE_EARLY, FORCE_EARLY, 0);
135 CASE(FORCE_EARLY, FORCE_EARLY, ALPHA2COV | DISCARD | WRITES_COV);
136 CASE(FORCE_EARLY, FORCE_EARLY, ZS_WRITEMASK);
137 }
138
TEST(EarlyZS,NoSideFXNoShaderZSAlt)139 TEST(EarlyZS, NoSideFXNoShaderZSAlt)
140 {
141 CASE(WEAK_EARLY, WEAK_EARLY, ZS_ALWAYS_PASSES);
142 CASE(WEAK_EARLY, WEAK_EARLY, ZS_ALWAYS_PASSES | ALPHA2COV | DISCARD | WRITES_COV);
143 CASE(WEAK_EARLY, WEAK_EARLY, ZS_ALWAYS_PASSES | ZS_WRITEMASK);
144 }
145