1 /*
2 * Copyright © 2020 Google, Inc.
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 #define FD_BO_NO_HARDPIN 1
25
26 #include "pipe/p_state.h"
27
28 #include "freedreno_batch.h"
29 #include "freedreno_gmem.h"
30
31 #include "fd6_vsc.h"
32
33 /*
34 * Helper util to update expected vsc draw and primitive stream sizes, see
35 * https://gitlab.freedesktop.org/freedreno/freedreno/-/wikis/Visibility-Stream-Format
36 */
37
38 enum bits_per {
39 byte = 8,
40 dword = 4 * byte,
41 };
42
43 /**
44 * Determine # of bits required to store a given number, see
45 * https://gitlab.freedesktop.org/freedreno/freedreno/-/wikis/Visibility-Stream-Format#numbers
46 */
47 static unsigned
number_size_bits(unsigned nr)48 number_size_bits(unsigned nr)
49 {
50 unsigned n = util_last_bit(nr);
51 assert(n); /* encoding 0 is not possible */
52 return n + (n - 1);
53 }
54
55 /**
56 * Determine # of bits requred to store a given bitfield, see
57 * https://gitlab.freedesktop.org/freedreno/freedreno/-/wikis/Visibility-Stream-Format#bitfields
58 */
59 static unsigned
bitfield_size_bits(unsigned n)60 bitfield_size_bits(unsigned n)
61 {
62 return n + 1; /* worst case is always 1 + nr of bits */
63 }
64
65 static unsigned
prim_count(const struct pipe_draw_info * info,const struct pipe_draw_start_count_bias * draw)66 prim_count(const struct pipe_draw_info *info,
67 const struct pipe_draw_start_count_bias *draw)
68 {
69 /* MESA_PRIM_COUNT used internally for RECTLIST blits on 3d pipe: */
70 unsigned vtx_per_prim =
71 (info->mode == MESA_PRIM_COUNT) ? 2 : mesa_vertices_per_prim(info->mode);
72 return MAX2(1, (draw->count * info->instance_count) / vtx_per_prim);
73 }
74
75 /**
76 * The primitive stream uses a run-length encoding, where each packet contains a
77 * bitfield of bins covered and then the number of primitives which have the
78 * same bitfield. Each packet consists of the following, in order:
79 *
80 * - The (compressed) bitfield of bins covered
81 * - The number of primitives with this bitset
82 * - Checksum
83 *
84 * The worst case would be that each primitive has a different bitmask. In
85 * practice, assuming ever other primitive has a different bitmask still gets us
86 * conservatively large primitive stream sizes. (Ie. 10x what is needed, vs.
87 * 20x)
88 *
89 * https://gitlab.freedesktop.org/freedreno/freedreno/-/wikis/Visibility-Stream-Format#primitive-streams
90 */
91 static unsigned
primitive_stream_size_bits(const struct pipe_draw_info * info,const struct pipe_draw_start_count_bias * draw,unsigned num_bins)92 primitive_stream_size_bits(const struct pipe_draw_info *info,
93 const struct pipe_draw_start_count_bias *draw,
94 unsigned num_bins)
95 {
96 unsigned num_prims = prim_count(info, draw);
97 unsigned nbits =
98 (bitfield_size_bits(num_bins) /* bitfield of bins covered */
99 + number_size_bits(1) /* number of primitives with this bitset */
100 + 1 /* checksum */
101 ) *
102 DIV_ROUND_UP(num_prims, 2);
103 return align(nbits, dword);
104 }
105
106 /**
107 * Each draw stream packet contains the following:
108 *
109 * - Bin bitfield
110 * - Last instance bit
111 * - If bitfield is empty, the number of draws it is empty for, otherwise
112 * the size of the corresponding primitive stream in DWORD's.
113 * - Checksum
114 *
115 * https://gitlab.freedesktop.org/freedreno/freedreno/-/wikis/Visibility-Stream-Format#draw-streams
116 */
117 static unsigned
draw_stream_size_bits(const struct pipe_draw_info * info,unsigned num_bins,unsigned prim_strm_bits)118 draw_stream_size_bits(const struct pipe_draw_info *info, unsigned num_bins,
119 unsigned prim_strm_bits)
120 {
121 unsigned ndwords = prim_strm_bits / dword;
122 return (bitfield_size_bits(num_bins) /* bitfield of bins */
123 + 1 /* last-instance-bit */
124 + number_size_bits(ndwords) /* size of corresponding prim strm */
125 + 1 /* checksum */
126 ) *
127 MAX2(1, info->instance_count);
128 }
129
130 void
fd6_vsc_update_sizes(struct fd_batch * batch,const struct pipe_draw_info * info,const struct pipe_draw_start_count_bias * draw)131 fd6_vsc_update_sizes(struct fd_batch *batch, const struct pipe_draw_info *info,
132 const struct pipe_draw_start_count_bias *draw)
133 {
134 if (!batch->num_bins_per_pipe) {
135 batch->num_bins_per_pipe = fd_gmem_estimate_bins_per_pipe(batch);
136
137 /* This is a convenient spot to add the size of the final draw-
138 * stream packet:
139 *
140 * If there are N bins, the final packet, after all the draws are
141 * done, consists of a 1 followed by N + 17 0's, plus a final 1.
142 * This uses the otherwise-unused pattern of a non-empty bitfield
143 * (initial 1) that is nontheless empty (has all 0's)
144 */
145 unsigned final_pkt_sz = 1 + batch->num_bins_per_pipe + 17 + 1;
146 batch->prim_strm_bits = align(final_pkt_sz, dword);
147 }
148
149 unsigned prim_strm_bits =
150 primitive_stream_size_bits(info, draw, batch->num_bins_per_pipe);
151 unsigned draw_strm_bits =
152 draw_stream_size_bits(info, batch->num_bins_per_pipe, prim_strm_bits);
153
154 #if 0
155 mesa_logd("vsc: prim_strm_bits=%d, draw_strm_bits=%d, nb=%u, ic=%u, c=%u, pc=%u (%s)",
156 prim_strm_bits, draw_strm_bits, batch->num_bins_per_pipe,
157 info->instance_count, info->count,
158 (info->count * info->instance_count) /
159 mesa_vertices_per_prim(info->mode),
160 u_prim_name(info->mode));
161 #endif
162
163 batch->prim_strm_bits += prim_strm_bits;
164 batch->draw_strm_bits += draw_strm_bits;
165 }
166