xref: /aosp_15_r20/external/mesa3d/src/panfrost/lib/pan_encoder.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright (C) 2019 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  * Authors (Collabora):
24  *   Alyssa Rosenzweig <[email protected]>
25  */
26 
27 #ifndef __PAN_ENCODER_H
28 #define __PAN_ENCODER_H
29 
30 #include "util/macros.h"
31 
32 #include <stdbool.h>
33 #include "genxml/gen_macros.h"
34 #include "util/format/u_format.h"
35 
36 #include "pan_pool.h"
37 
38 /* Tiler structure size computation */
39 
40 unsigned panfrost_tiler_header_size(unsigned width, unsigned height,
41                                     unsigned mask, bool hierarchy);
42 
43 unsigned panfrost_tiler_full_size(unsigned width, unsigned height,
44                                   unsigned mask, bool hierarchy);
45 
46 unsigned panfrost_choose_hierarchy_mask(unsigned width, unsigned height,
47                                         unsigned vertex_count, bool hierarchy);
48 
49 #if defined(PAN_ARCH) && PAN_ARCH <= 5
50 static inline unsigned
panfrost_tiler_get_polygon_list_size(unsigned fb_width,unsigned fb_height,unsigned vertex_count,bool hierarchy)51 panfrost_tiler_get_polygon_list_size(unsigned fb_width, unsigned fb_height,
52                                      unsigned vertex_count, bool hierarchy)
53 {
54    if (!vertex_count)
55       return MALI_MIDGARD_TILER_MINIMUM_HEADER_SIZE + 4;
56 
57    unsigned hierarchy_mask = panfrost_choose_hierarchy_mask(
58       fb_width, fb_height, vertex_count, hierarchy);
59 
60    return panfrost_tiler_full_size(fb_width, fb_height, hierarchy_mask,
61                                    hierarchy) +
62           panfrost_tiler_header_size(fb_width, fb_height, hierarchy_mask,
63                                      hierarchy);
64 }
65 #endif
66 
67 /* Stack sizes */
68 
69 unsigned panfrost_get_stack_shift(unsigned stack_size);
70 
71 unsigned panfrost_get_total_stack_size(unsigned thread_size,
72                                        unsigned threads_per_core,
73                                        unsigned core_id_range);
74 
75 /* Attributes / instancing */
76 
77 unsigned panfrost_padded_vertex_count(unsigned vertex_count);
78 
79 unsigned panfrost_compute_magic_divisor(unsigned hw_divisor, unsigned *o_shift,
80                                         unsigned *extra_flags);
81 
82 #ifdef PAN_ARCH
83 /* Records for gl_VertexID and gl_InstanceID use special encodings on Midgard */
84 
85 #if PAN_ARCH <= 5
86 static inline void
panfrost_vertex_id(unsigned padded_count,struct mali_attribute_buffer_packed * attr,bool instanced)87 panfrost_vertex_id(unsigned padded_count,
88                    struct mali_attribute_buffer_packed *attr, bool instanced)
89 {
90    pan_pack(attr, ATTRIBUTE_VERTEX_ID, cfg) {
91       if (instanced) {
92          cfg.divisor_r = __builtin_ctz(padded_count);
93          cfg.divisor_p = padded_count >> (cfg.divisor_r + 1);
94       } else {
95          /* Large values so the modulo is a no-op */
96          cfg.divisor_r = 0x1F;
97          cfg.divisor_p = 0x4;
98       }
99    }
100 }
101 
102 static inline void
panfrost_instance_id(unsigned padded_count,struct mali_attribute_buffer_packed * attr,bool instanced)103 panfrost_instance_id(unsigned padded_count,
104                      struct mali_attribute_buffer_packed *attr, bool instanced)
105 {
106    pan_pack(attr, ATTRIBUTE_INSTANCE_ID, cfg) {
107       if (!instanced || padded_count <= 1) {
108          /* Divide by large number to force to 0 */
109          cfg.divisor_p = ((1u << 31) - 1);
110          cfg.divisor_r = 0x1F;
111          cfg.divisor_e = 0x1;
112       } else if (util_is_power_of_two_or_zero(padded_count)) {
113          /* Can't underflow since padded_count >= 2 */
114          cfg.divisor_r = __builtin_ctz(padded_count) - 1;
115       } else {
116          cfg.divisor_p = panfrost_compute_magic_divisor(
117             padded_count, &cfg.divisor_r, &cfg.divisor_e);
118       }
119    }
120 }
121 #endif /* PAN_ARCH <= 5 */
122 
123 /* Sampler comparison functions are flipped in OpenGL from the hardware, so we
124  * need to be able to flip accordingly */
125 
126 static inline enum mali_func
panfrost_flip_compare_func(enum mali_func f)127 panfrost_flip_compare_func(enum mali_func f)
128 {
129    switch (f) {
130    case MALI_FUNC_LESS:
131       return MALI_FUNC_GREATER;
132    case MALI_FUNC_GREATER:
133       return MALI_FUNC_LESS;
134    case MALI_FUNC_LEQUAL:
135       return MALI_FUNC_GEQUAL;
136    case MALI_FUNC_GEQUAL:
137       return MALI_FUNC_LEQUAL;
138    default:
139       return f;
140    }
141 }
142 
143 #if PAN_ARCH <= 7
144 /* Compute shaders are invoked with a gl_NumWorkGroups X/Y/Z triplet. Vertex
145  * shaders are invoked as (1, vertex_count, instance_count). Compute shaders
146  * also have a gl_WorkGroupSize X/Y/Z triplet. These 6 values are packed
147  * together in a dynamic bitfield, packed by this routine. */
148 
149 static inline void
panfrost_pack_work_groups_compute(struct mali_invocation_packed * out,unsigned num_x,unsigned num_y,unsigned num_z,unsigned size_x,unsigned size_y,unsigned size_z,bool quirk_graphics,bool indirect_dispatch)150 panfrost_pack_work_groups_compute(struct mali_invocation_packed *out,
151                                   unsigned num_x, unsigned num_y,
152                                   unsigned num_z, unsigned size_x,
153                                   unsigned size_y, unsigned size_z,
154                                   bool quirk_graphics, bool indirect_dispatch)
155 {
156    /* The values needing packing, in order, and the corresponding shifts.
157     * Indicies into shift are off-by-one to make the logic easier */
158 
159    unsigned values[6] = {size_x, size_y, size_z, num_x, num_y, num_z};
160    unsigned shifts[7] = {0};
161    uint32_t packed = 0;
162 
163    for (unsigned i = 0; i < 6; ++i) {
164       /* Must be positive, otherwise we underflow */
165       assert(values[i] >= 1);
166 
167       /* OR it in, shifting as required */
168       packed |= ((values[i] - 1) << shifts[i]);
169 
170       /* How many bits did we use? */
171       unsigned bit_count = util_logbase2_ceil(values[i]);
172 
173       /* Set the next shift accordingly */
174       shifts[i + 1] = shifts[i] + bit_count;
175    }
176 
177    pan_pack(out, INVOCATION, cfg) {
178       cfg.invocations = packed;
179       cfg.size_y_shift = shifts[1];
180       cfg.size_z_shift = shifts[2];
181       cfg.workgroups_x_shift = shifts[3];
182 
183       if (!indirect_dispatch) {
184          /* Leave zero for the dispatch shader */
185          cfg.workgroups_y_shift = shifts[4];
186          cfg.workgroups_z_shift = shifts[5];
187       }
188 
189       /* Quirk: for non-instanced graphics, the blob sets
190        * workgroups_z_shift = 32. This doesn't appear to matter to
191        * the hardware, but it's good to be bit-identical. */
192 
193       if (quirk_graphics && (num_z <= 1))
194          cfg.workgroups_z_shift = 32;
195 
196       /* For graphics, set to the minimum efficient value. For
197        * compute, must equal the workgroup X shift for barriers to
198        * function correctly */
199 
200       cfg.thread_group_split =
201          quirk_graphics ? MALI_SPLIT_MIN_EFFICIENT : cfg.workgroups_x_shift;
202    }
203 }
204 #endif
205 
206 #if PAN_ARCH >= 5
207 /* Format conversion */
208 static inline enum mali_z_internal_format
panfrost_get_z_internal_format(enum pipe_format fmt)209 panfrost_get_z_internal_format(enum pipe_format fmt)
210 {
211    switch (fmt) {
212    case PIPE_FORMAT_Z16_UNORM:
213    case PIPE_FORMAT_Z16_UNORM_S8_UINT:
214       return MALI_Z_INTERNAL_FORMAT_D16;
215    case PIPE_FORMAT_Z24_UNORM_S8_UINT:
216    case PIPE_FORMAT_Z24X8_UNORM:
217       return MALI_Z_INTERNAL_FORMAT_D24;
218    case PIPE_FORMAT_Z32_FLOAT:
219    case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
220       return MALI_Z_INTERNAL_FORMAT_D32;
221    default:
222       unreachable("Unsupported depth/stencil format.");
223    }
224 }
225 #endif
226 
227 #endif /* PAN_ARCH */
228 
229 #if PAN_ARCH >= 9
230 static inline void
panfrost_make_resource_table(struct panfrost_ptr base,unsigned index,mali_ptr address,unsigned resource_count)231 panfrost_make_resource_table(struct panfrost_ptr base, unsigned index,
232                              mali_ptr address, unsigned resource_count)
233 {
234    if (resource_count == 0)
235       return;
236 
237    pan_pack(base.cpu + index * pan_size(RESOURCE), RESOURCE, cfg) {
238       cfg.address = address;
239       cfg.size = resource_count * pan_size(BUFFER);
240    }
241 }
242 #endif
243 
244 #endif
245