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