| 1 |
/* |
| 2 |
* Copyright (C) 2019 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#ifndef __PAN_ENCODER_H |
| 7 |
#define __PAN_ENCODER_H |
| 8 |
|
| 9 |
#ifndef __OPENCL_VERSION__ |
| 10 |
#include "util/macros.h" |
| 11 |
|
| 12 |
#include <stdbool.h> |
| 13 |
#include "util/format/u_format.h" |
| 14 |
#include "pan_pool.h" |
| 15 |
#else |
| 16 |
#include "compiler/libcl/libcl.h" |
| 17 |
#endif |
| 18 |
|
| 19 |
#include "genxml/gen_macros.h" |
| 20 |
|
| 21 |
/* Tiler structure size computation */ |
| 22 |
|
| 23 |
unsigned pan_tiler_header_size(unsigned width, unsigned height, unsigned mask, |
| 24 |
bool hierarchy); |
| 25 |
|
| 26 |
unsigned pan_tiler_full_size(unsigned width, unsigned height, unsigned mask, |
| 27 |
bool hierarchy); |
| 28 |
|
| 29 |
unsigned pan_choose_hierarchy_mask(unsigned width, unsigned height, |
| 30 |
unsigned vertex_count, bool hierarchy); |
| 31 |
|
| 32 |
#if defined(PAN_ARCH) && PAN_ARCH <= 5 |
| 33 |
static inline unsigned |
| 34 |
pan_tiler_get_polygon_list_size(unsigned fb_width, unsigned fb_height, |
| 35 |
unsigned vertex_count, bool hierarchy) |
| 36 |
{ |
| 37 |
if (!vertex_count) |
| 38 |
return MALI_MIDGARD_TILER_MINIMUM_HEADER_SIZE + 4; |
| 39 |
|
| 40 |
unsigned hierarchy_mask = |
| 41 |
pan_choose_hierarchy_mask(fb_width, fb_height, vertex_count, hierarchy); |
| 42 |
|
| 43 |
return pan_tiler_full_size(fb_width, fb_height, hierarchy_mask, hierarchy) + |
| 44 |
pan_tiler_header_size(fb_width, fb_height, hierarchy_mask, hierarchy); |
| 45 |
} |
| 46 |
#endif |
| 47 |
|
| 48 |
/* Stack sizes */ |
| 49 |
|
| 50 |
unsigned pan_get_stack_shift(unsigned stack_size); |
| 51 |
|
| 52 |
unsigned pan_get_total_stack_size(unsigned thread_size, |
| 53 |
unsigned threads_per_core, |
| 54 |
unsigned core_id_range); |
| 55 |
|
| 56 |
/* Attributes / instancing */ |
| 57 |
|
| 58 |
static inline unsigned |
| 59 |
pan_padded_vertex_count(unsigned vertex_count) |
| 60 |
{ |
| 61 |
if (vertex_count < 10) |
| 62 |
return vertex_count; |
| 63 |
|
| 64 |
if (vertex_count < 20) |
| 65 |
return (vertex_count + 1) & ~1; |
| 66 |
|
| 67 |
/* First, we have to find the highest set one */ |
| 68 |
unsigned highest = 32 - __builtin_clz(vertex_count); |
| 69 |
|
| 70 |
/* Using that, we mask out the highest 4-bits */ |
| 71 |
unsigned n = highest - 4; |
| 72 |
unsigned nibble = (vertex_count >> n) & 0xF; |
| 73 |
|
| 74 |
/* Great, we have the nibble. Now we can just try possibilities. Note |
| 75 |
* that we don't care about the bottom most bit in most cases, and we |
| 76 |
* know the top bit must be 1 */ |
| 77 |
|
| 78 |
unsigned middle_two = (nibble >> 1) & 0x3; |
| 79 |
|
| 80 |
switch (middle_two) { |
| 81 |
case 0b00: |
| 82 |
if (!(nibble & 1)) |
| 83 |
return (1 << n) * 9; |
| 84 |
else |
| 85 |
return (1 << (n + 1)) * 5; |
| 86 |
case 0b01: |
| 87 |
return (1 << (n + 2)) * 3; |
| 88 |
case 0b10: |
| 89 |
return (1 << (n + 1)) * 7; |
| 90 |
case 0b11: |
| 91 |
return (1 << (n + 4)); |
| 92 |
default: |
| 93 |
return 0; /* unreachable */ |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
static inline unsigned |
| 98 |
pan_compute_npot_divisor(unsigned hw_divisor, unsigned *divisor_r, |
| 99 |
unsigned *divisor_e) |
| 100 |
{ |
| 101 |
unsigned r = util_logbase2(hw_divisor); |
| 102 |
|
| 103 |
uint64_t shift_hi = 32 + r; |
| 104 |
uint64_t t = (uint64_t)1 << shift_hi; |
| 105 |
uint64_t f0 = t + hw_divisor / 2; |
| 106 |
uint64_t fi = f0 / hw_divisor; |
| 107 |
uint64_t ff = f0 - fi * hw_divisor; |
| 108 |
|
| 109 |
uint64_t d = fi - (1ul << 31); |
| 110 |
*divisor_r = r; |
| 111 |
*divisor_e = ff > hw_divisor / 2 ? 1 : 0; |
| 112 |
|
| 113 |
return d; |
| 114 |
} |
| 115 |
|
| 116 |
#ifdef PAN_ARCH |
| 117 |
/* Records for gl_VertexID and gl_InstanceID use special encodings on Midgard */ |
| 118 |
|
| 119 |
#if PAN_ARCH <= 5 |
| 120 |
static inline void |
| 121 |
pan_vertex_id(unsigned padded_count, |
| 122 |
struct mali_attribute_vertex_id_packed *attr, bool instanced) |
| 123 |
{ |
| 124 |
pan_pack(attr, ATTRIBUTE_VERTEX_ID, cfg) { |
| 125 |
if (instanced) { |
| 126 |
cfg.divisor_r = __builtin_ctz(padded_count); |
| 127 |
cfg.divisor_p = padded_count >> (cfg.divisor_r + 1); |
| 128 |
} else { |
| 129 |
/* Large values so the modulo is a no-op */ |
| 130 |
cfg.divisor_r = 0x1F; |
| 131 |
cfg.divisor_p = 0x4; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
static inline void |
| 137 |
pan_instance_id(unsigned padded_count, |
| 138 |
struct mali_attribute_instance_id_packed *attr, bool instanced) |
| 139 |
{ |
| 140 |
pan_pack(attr, ATTRIBUTE_INSTANCE_ID, cfg) { |
| 141 |
if (!instanced || padded_count <= 1) { |
| 142 |
/* Divide by large number to force to 0 */ |
| 143 |
cfg.divisor_p = ((1u << 31) - 1); |
| 144 |
cfg.divisor_r = 0x1F; |
| 145 |
cfg.divisor_e = 0x1; |
| 146 |
} else if (util_is_power_of_two_or_zero(padded_count)) { |
| 147 |
/* Can't underflow since padded_count >= 2 */ |
| 148 |
cfg.divisor_r = __builtin_ctz(padded_count) - 1; |
| 149 |
} else { |
| 150 |
cfg.divisor_p = pan_compute_npot_divisor(padded_count, &cfg.divisor_r, |
| 151 |
&cfg.divisor_e); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
#endif /* PAN_ARCH <= 5 */ |
| 156 |
|
| 157 |
/* Sampler comparison functions are flipped in OpenGL from the hardware, so we |
| 158 |
* need to be able to flip accordingly */ |
| 159 |
|
| 160 |
static inline enum mali_func |
| 161 |
pan_flip_compare_func(enum mali_func f) |
| 162 |
{ |
| 163 |
switch (f) { |
| 164 |
case MALI_FUNC_LESS: |
| 165 |
return MALI_FUNC_GREATER; |
| 166 |
case MALI_FUNC_GREATER: |
| 167 |
return MALI_FUNC_LESS; |
| 168 |
case MALI_FUNC_LEQUAL: |
| 169 |
return MALI_FUNC_GEQUAL; |
| 170 |
case MALI_FUNC_GEQUAL: |
| 171 |
return MALI_FUNC_LEQUAL; |
| 172 |
default: |
| 173 |
return f; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
#if PAN_ARCH < 9 |
| 178 |
/* Compute shaders are invoked with a gl_NumWorkGroups X/Y/Z triplet. Vertex |
| 179 |
* shaders are invoked as (1, vertex_count, instance_count). Compute shaders |
| 180 |
* also have a gl_WorkGroupSize X/Y/Z triplet. These 6 values are packed |
| 181 |
* together in a dynamic bitfield, packed by this routine. */ |
| 182 |
|
| 183 |
static inline void |
| 184 |
pan_pack_work_groups_compute(struct mali_invocation_packed *out, unsigned num_x, |
| 185 |
unsigned num_y, unsigned num_z, unsigned size_x, |
| 186 |
unsigned size_y, unsigned size_z, |
| 187 |
bool quirk_graphics, bool indirect_dispatch) |
| 188 |
{ |
| 189 |
/* The values needing packing, in order, and the corresponding shifts. |
| 190 |
* Indices into shift are off-by-one to make the logic easier */ |
| 191 |
|
| 192 |
unsigned values[6] = {size_x, size_y, size_z, num_x, num_y, num_z}; |
| 193 |
unsigned shifts[7] = {0}; |
| 194 |
uint32_t packed = 0; |
| 195 |
|
| 196 |
for (unsigned i = 0; i < 6; ++i) { |
| 197 |
/* Must be positive, otherwise we underflow */ |
| 198 |
assert(values[i] >= 1); |
| 199 |
|
| 200 |
/* OR it in, shifting as required */ |
| 201 |
packed |= ((values[i] - 1) << shifts[i]); |
| 202 |
|
| 203 |
/* How many bits did we use? */ |
| 204 |
unsigned bit_count = util_logbase2_ceil(values[i]); |
| 205 |
|
| 206 |
/* Set the next shift accordingly */ |
| 207 |
shifts[i + 1] = shifts[i] + bit_count; |
| 208 |
} |
| 209 |
|
| 210 |
pan_pack(out, INVOCATION, cfg) { |
| 211 |
cfg.invocations = packed; |
| 212 |
cfg.size_y_shift = shifts[1]; |
| 213 |
cfg.size_z_shift = shifts[2]; |
| 214 |
cfg.workgroups_x_shift = shifts[3]; |
| 215 |
|
| 216 |
if (!indirect_dispatch) { |
| 217 |
/* Leave zero for the dispatch shader */ |
| 218 |
cfg.workgroups_y_shift = shifts[4]; |
| 219 |
cfg.workgroups_z_shift = shifts[5]; |
| 220 |
} |
| 221 |
|
| 222 |
/* Quirk: for non-instanced graphics, the blob sets |
| 223 |
* workgroups_z_shift = 32. This doesn't appear to matter to |
| 224 |
* the hardware, but it's good to be bit-identical. */ |
| 225 |
|
| 226 |
if (quirk_graphics && (num_z <= 1)) |
| 227 |
cfg.workgroups_z_shift = 32; |
| 228 |
|
| 229 |
/* For graphics, set to the minimum efficient value. For |
| 230 |
* compute, must equal the workgroup X shift for barriers to |
| 231 |
* function correctly */ |
| 232 |
|
| 233 |
cfg.thread_group_split = |
| 234 |
quirk_graphics ? MALI_SPLIT_MIN_EFFICIENT : cfg.workgroups_x_shift; |
| 235 |
} |
| 236 |
} |
| 237 |
#endif |
| 238 |
|
| 239 |
#ifndef __OPENCL_VERSION__ |
| 240 |
#if PAN_ARCH >= 5 |
| 241 |
/* Format conversion */ |
| 242 |
static inline enum mali_z_internal_format |
| 243 |
pan_get_z_internal_format(enum pipe_format fmt) |
| 244 |
{ |
| 245 |
switch (fmt) { |
| 246 |
case PIPE_FORMAT_Z16_UNORM: |
| 247 |
case PIPE_FORMAT_Z16_UNORM_S8_UINT: |
| 248 |
return MALI_Z_INTERNAL_FORMAT_D16; |
| 249 |
case PIPE_FORMAT_Z24_UNORM_S8_UINT: |
| 250 |
case PIPE_FORMAT_Z24X8_UNORM: |
| 251 |
case PIPE_FORMAT_Z24_UNORM_PACKED: |
| 252 |
return MALI_Z_INTERNAL_FORMAT_D24; |
| 253 |
case PIPE_FORMAT_Z32_FLOAT: |
| 254 |
case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT: |
| 255 |
return MALI_Z_INTERNAL_FORMAT_D32; |
| 256 |
default: |
| 257 |
UNREACHABLE("Unsupported depth/stencil format."); |
| 258 |
} |
| 259 |
} |
| 260 |
#endif |
| 261 |
#endif |
| 262 |
|
| 263 |
#endif /* PAN_ARCH */ |
| 264 |
|
| 265 |
#ifndef __OPENCL_VERSION__ |
| 266 |
#if PAN_ARCH >= 9 |
| 267 |
static inline void |
| 268 |
pan_make_resource_table(struct pan_ptr base, unsigned index, uint64_t address, |
| 269 |
unsigned resource_count) |
| 270 |
{ |
| 271 |
if (resource_count == 0) |
| 272 |
return; |
| 273 |
|
| 274 |
struct mali_resource_packed *res = base.cpu; |
| 275 |
pan_pack(&res[index], RESOURCE, cfg) { |
| 276 |
cfg.address = address; |
| 277 |
cfg.size = resource_count * pan_size(BUFFER); |
| 278 |
} |
| 279 |
} |
| 280 |
#endif |
| 281 |
#endif |
| 282 |
|
| 283 |
#endif |