xref: /aosp_15_r20/external/mesa3d/src/panfrost/lib/pan_earlyzs.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 "panfrost/util/pan_ir.h"
26 
27 /*
28  * Return an "early" mode. If it is known that the depth/stencil tests always
29  * pass (so the shader is always executed), weak early is usually faster than
30  * force early.
31  */
32 static enum pan_earlyzs
best_early_mode(bool zs_always_passes)33 best_early_mode(bool zs_always_passes)
34 {
35    if (zs_always_passes)
36       return PAN_EARLYZS_WEAK_EARLY;
37    else
38       return PAN_EARLYZS_FORCE_EARLY;
39 }
40 
41 /*
42  * Analyze a fragment shader and provided API state to determine the early-ZS
43  * configuration. The order of arguments must match the order of states in the
44  * lookup table, synchronized with pan_earlyzs_get.
45  */
46 static struct pan_earlyzs_state
analyze(const struct pan_shader_info * s,bool writes_zs_or_oq,bool alpha_to_coverage,bool zs_always_passes)47 analyze(const struct pan_shader_info *s, bool writes_zs_or_oq,
48         bool alpha_to_coverage, bool zs_always_passes)
49 {
50    /* If the shader writes depth or stencil, all depth/stencil tests must
51     * be deferred until the value is known after the ZS_EMIT instruction,
52     * if present. ZS_EMIT must precede ATEST, so the value is known when
53     * ATEST executes, justifying the late test/update.
54     */
55    bool shader_writes_zs = (s->fs.writes_depth || s->fs.writes_stencil);
56    bool late_update = shader_writes_zs;
57    bool late_kill = shader_writes_zs;
58 
59    /* Late coverage updates are required if the coverage mask depends on
60     * the results of the shader. Discards are implemented as coverage mask
61     * updates and must be considered. Strictly, depth/stencil writes may
62     * also update the coverage mask, but these already force late updates.
63     */
64    bool late_coverage =
65       s->fs.writes_coverage || s->fs.can_discard || alpha_to_coverage;
66 
67    /* Late coverage mask updates may affect the value written to the
68     * depth/stencil buffer (if a pixel is discarded entirely). However,
69     * they do not affect depth/stencil testing. So they may only matter if
70     * depth or stencil is written.
71     *
72     * That dependency does mean late coverage mask updates require late
73     * depth/stencil updates.
74     *
75     * Similarly, occlusion queries count samples that pass the
76     * depth/stencil tests, so occlusion queries with late coverage also
77     * require a late update.
78     */
79    late_update |= (late_coverage && writes_zs_or_oq);
80 
81    /* Side effects require late depth/stencil tests to ensure the shader
82     * isn't killed before the side effects execute.
83     */
84    late_kill |= s->writes_global;
85 
86    /* Finally, the shader may override and force early fragment tests */
87    late_update &= !s->fs.early_fragment_tests;
88    late_kill &= !s->fs.early_fragment_tests;
89 
90    /* Collect results */
91    enum pan_earlyzs early_mode = best_early_mode(zs_always_passes);
92 
93    return (struct pan_earlyzs_state){
94       .update = late_update ? PAN_EARLYZS_FORCE_LATE : early_mode,
95       .kill = late_kill ? PAN_EARLYZS_FORCE_LATE : early_mode,
96    };
97 }
98 
99 /*
100  * Analyze a fragment shader to determine all possible early-ZS configurations.
101  * Returns a lookup table of configurations indexed by the API state.
102  */
103 struct pan_earlyzs_lut
pan_earlyzs_analyze(const struct pan_shader_info * s)104 pan_earlyzs_analyze(const struct pan_shader_info *s)
105 {
106    struct pan_earlyzs_lut lut;
107 
108    for (unsigned v0 = 0; v0 < 2; ++v0) {
109       for (unsigned v1 = 0; v1 < 2; ++v1) {
110          for (unsigned v2 = 0; v2 < 2; ++v2)
111             lut.states[v0][v1][v2] = analyze(s, v0, v1, v2);
112       }
113    }
114 
115    return lut;
116 }
117