| 1 |
/* |
| 2 |
* Copyright 2024 Alyssa Rosenzweig |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#pragma once |
| 7 |
|
| 8 |
#include "agx_bo.h" |
| 9 |
#include "agx_compile.h" |
| 10 |
#include "agx_nir_lower_vbo.h" |
| 11 |
#include "agx_pack.h" |
| 12 |
#include "nir_lower_blend.h" |
| 13 |
#include "util/macros.h" |
| 14 |
|
| 15 |
struct agx_linked_shader { |
| 16 |
/* Mapped executable memory */ |
| 17 |
struct agx_bo *bo; |
| 18 |
|
| 19 |
/* Set if the linked SW vertex shader reads base vertex/instance. The VS |
| 20 |
* prolog can read base instance even when the API VS does not, which is why |
| 21 |
* this needs to be aggregated in the linker. |
| 22 |
*/ |
| 23 |
bool uses_base_param; |
| 24 |
|
| 25 |
/* Set if the linked shader uses txf. The epilog may even if the main shader |
| 26 |
* does not, in the case of spilled render targets. |
| 27 |
*/ |
| 28 |
bool uses_txf; |
| 29 |
|
| 30 |
/* Whether the program is a no-op and can be skipped. */ |
| 31 |
bool no_op; |
| 32 |
|
| 33 |
/* Coefficient register bindings */ |
| 34 |
struct agx_varyings_fs cf; |
| 35 |
|
| 36 |
/* Data structures packed for the linked program */ |
| 37 |
struct agx_usc_shader_packed shader; |
| 38 |
struct agx_usc_registers_packed regs; |
| 39 |
struct agx_usc_fragment_properties_packed fragment_props; |
| 40 |
struct agx_output_select_packed osel; |
| 41 |
struct agx_fragment_control_packed fragment_control; |
| 42 |
}; |
| 43 |
|
| 44 |
void agx_fast_link(struct agx_linked_shader *linked, struct agx_device *dev, |
| 45 |
bool fragment, struct agx_shader_part *main, |
| 46 |
struct agx_shader_part *prolog, |
| 47 |
struct agx_shader_part *epilog, unsigned nr_samples_shaded); |
| 48 |
|
| 49 |
/* These parts of the vertex element affect the generated code */ |
| 50 |
struct agx_velem_key { |
| 51 |
uint32_t divisor; |
| 52 |
uint16_t stride; |
| 53 |
uint8_t format; |
| 54 |
bool instanced; |
| 55 |
}; |
| 56 |
|
| 57 |
struct agx_vs_prolog_key { |
| 58 |
struct agx_velem_key attribs[AGX_MAX_VBUFS]; |
| 59 |
|
| 60 |
/* Bit mask of attribute components to load */ |
| 61 |
BITSET_DECLARE(component_mask, AGX_MAX_ATTRIBS * 4); |
| 62 |
|
| 63 |
/* Whether running as a hardware vertex shader (versus compute) */ |
| 64 |
bool hw; |
| 65 |
|
| 66 |
/* Whether the shader uses static vertex input and the prolog just fixes up |
| 67 |
* the vertex ID. |
| 68 |
*/ |
| 69 |
bool static_vi; |
| 70 |
|
| 71 |
/* If !hw and the draw call is indexed, the index size */ |
| 72 |
uint8_t sw_index_size_B; |
| 73 |
|
| 74 |
/* Adjacency primitive to emulate */ |
| 75 |
enum mesa_prim adjacency; |
| 76 |
|
| 77 |
/* Robustness settings for the vertex fetch */ |
| 78 |
struct agx_robustness robustness; |
| 79 |
}; |
| 80 |
|
| 81 |
struct agx_fs_prolog_key { |
| 82 |
/* glSampleMask() mask */ |
| 83 |
uint8_t api_sample_mask; |
| 84 |
|
| 85 |
/* Number of cull planes requiring lowering */ |
| 86 |
uint8_t cull_distance_size; |
| 87 |
|
| 88 |
/* Need to count FRAGMENT_SHADER_INVOCATIONS */ |
| 89 |
bool statistics; |
| 90 |
|
| 91 |
/* Need to lower desktop OpenGL polygon stipple */ |
| 92 |
bool polygon_stipple; |
| 93 |
|
| 94 |
/* If we discard, whether we need to run Z/S tests */ |
| 95 |
bool run_zs_tests; |
| 96 |
|
| 97 |
/* If we emulate cull distance, the base offset for our allocated coefficient |
| 98 |
* registers so we don't interfere with the main shader. |
| 99 |
*/ |
| 100 |
unsigned cf_base; |
| 101 |
}; |
| 102 |
|
| 103 |
struct agx_blend_rt_key { |
| 104 |
unsigned colormask : 4; |
| 105 |
unsigned advanced_blend : 1; |
| 106 |
unsigned pad : 1; |
| 107 |
unsigned mode : 26; |
| 108 |
}; |
| 109 |
static_assert(sizeof(struct agx_blend_rt_key) == 4, "packed"); |
| 110 |
|
| 111 |
struct agx_blend_standard { |
| 112 |
enum pipe_blend_func rgb_func : 3; |
| 113 |
enum pipe_blendfactor rgb_src_factor : 5; |
| 114 |
enum pipe_blendfactor rgb_dst_factor : 5; |
| 115 |
enum pipe_blend_func alpha_func : 3; |
| 116 |
enum pipe_blendfactor alpha_src_factor : 5; |
| 117 |
enum pipe_blendfactor alpha_dst_factor : 5; |
| 118 |
unsigned pad : 6; |
| 119 |
}; |
| 120 |
static_assert(sizeof(struct agx_blend_standard) == 4, "packed"); |
| 121 |
|
| 122 |
static inline unsigned |
| 123 |
agx_pack_blend_standard(enum pipe_blend_func rgb_func, |
| 124 |
enum pipe_blendfactor rgb_src_factor, |
| 125 |
enum pipe_blendfactor rgb_dst_factor, |
| 126 |
enum pipe_blend_func alpha_func, |
| 127 |
enum pipe_blendfactor alpha_src_factor, |
| 128 |
enum pipe_blendfactor alpha_dst_factor) |
| 129 |
{ |
| 130 |
struct agx_blend_standard blend = { |
| 131 |
.rgb_func = rgb_func, |
| 132 |
.rgb_src_factor = rgb_src_factor, |
| 133 |
.rgb_dst_factor = rgb_dst_factor, |
| 134 |
.alpha_func = alpha_func, |
| 135 |
.alpha_src_factor = alpha_src_factor, |
| 136 |
.alpha_dst_factor = alpha_dst_factor, |
| 137 |
}; |
| 138 |
unsigned val; |
| 139 |
typed_memcpy(&val, &blend, 1); |
| 140 |
return val; |
| 141 |
} |
| 142 |
|
| 143 |
static inline struct agx_blend_standard |
| 144 |
agx_unpack_blend_standard(unsigned mode) |
| 145 |
{ |
| 146 |
struct agx_blend_standard blend; |
| 147 |
typed_memcpy(&blend, &mode, 1); |
| 148 |
return blend; |
| 149 |
} |
| 150 |
|
| 151 |
struct agx_blend_advanced { |
| 152 |
enum pipe_advanced_blend_mode op : 8; |
| 153 |
enum pipe_blend_overlap_mode overlap : 2; |
| 154 |
unsigned src_premultiplied : 1; |
| 155 |
unsigned dst_premultiplied : 1; |
| 156 |
unsigned clamp_results : 1; |
| 157 |
unsigned pad : 19; |
| 158 |
}; |
| 159 |
static_assert(sizeof(struct agx_blend_advanced) == 4, "packed"); |
| 160 |
|
| 161 |
static inline unsigned |
| 162 |
agx_pack_blend_advanced(enum pipe_advanced_blend_mode op, |
| 163 |
enum pipe_blend_overlap_mode overlap, |
| 164 |
bool src_premultiplied, bool dst_premultiplied, |
| 165 |
bool clamp_results) |
| 166 |
{ |
| 167 |
struct agx_blend_advanced blend = { |
| 168 |
.op = op, |
| 169 |
.overlap = overlap, |
| 170 |
.src_premultiplied = src_premultiplied, |
| 171 |
.dst_premultiplied = dst_premultiplied, |
| 172 |
.clamp_results = clamp_results, |
| 173 |
}; |
| 174 |
unsigned val; |
| 175 |
typed_memcpy(&val, &blend, 1); |
| 176 |
return val; |
| 177 |
} |
| 178 |
|
| 179 |
static inline struct agx_blend_advanced |
| 180 |
agx_unpack_blend_advanced(unsigned mode) |
| 181 |
{ |
| 182 |
struct agx_blend_advanced blend; |
| 183 |
typed_memcpy(&blend, &mode, 1); |
| 184 |
return blend; |
| 185 |
} |
| 186 |
|
| 187 |
struct agx_blend_key { |
| 188 |
struct agx_blend_rt_key rt[8]; |
| 189 |
uint8_t logicop_func; |
| 190 |
bool alpha_to_coverage, alpha_to_one; |
| 191 |
bool logicop_enable; |
| 192 |
}; |
| 193 |
static_assert(sizeof(struct agx_blend_key) == 36, "packed"); |
| 194 |
|
| 195 |
struct agx_fs_epilog_link_info { |
| 196 |
/* Base index of spilled render targets in the binding table */ |
| 197 |
uint8_t rt_spill_base; |
| 198 |
|
| 199 |
/* Bit mask of the bit size written to each render target. Bit i set if RT i |
| 200 |
* uses 32-bit registers, else 16-bit registers. |
| 201 |
*/ |
| 202 |
uint8_t size_32; |
| 203 |
|
| 204 |
/* Mask of locations written by the main shader */ |
| 205 |
uint8_t loc_written; |
| 206 |
|
| 207 |
/* If set, the API fragment shader uses sample shading. This means the epilog |
| 208 |
* will be invoked per-sample as well. |
| 209 |
*/ |
| 210 |
unsigned sample_shading : 1; |
| 211 |
|
| 212 |
/* If set, broadcast location #0 value to all render targets. This |
| 213 |
* implements gl_FragColor semantics. This tells the driver to set remap |
| 214 |
* appropriately. |
| 215 |
*/ |
| 216 |
unsigned broadcast_rt0 : 1; |
| 217 |
|
| 218 |
/* If set, force location 0's W channel to 1.0. This optimizes blending |
| 219 |
* calculations in some applications. |
| 220 |
*/ |
| 221 |
unsigned loc0_w_1 : 1; |
| 222 |
|
| 223 |
/* If set, the API fragment shader wants to write depth/stencil respectively. |
| 224 |
* This happens in the epilog for correctness when the epilog discards. |
| 225 |
*/ |
| 226 |
unsigned write_z : 1; |
| 227 |
unsigned write_s : 1; |
| 228 |
|
| 229 |
/* Whether the fragment prolog or main fragment shader already ran tests due |
| 230 |
* to early_fragment_tests. In this case, the epilog must not run tests. |
| 231 |
*/ |
| 232 |
unsigned already_ran_zs : 1; |
| 233 |
|
| 234 |
/* Whether the main fragment shader ran tests before discards due to |
| 235 |
* early_fragment_tests. In this case, the epilog must mask the stores in |
| 236 |
* software instead. |
| 237 |
*/ |
| 238 |
bool sample_mask_after_force_early : 1; |
| 239 |
|
| 240 |
unsigned padding : 1; |
| 241 |
}; |
| 242 |
static_assert(sizeof(struct agx_fs_epilog_link_info) == 4, "packed"); |
| 243 |
|
| 244 |
struct agx_fs_epilog_key { |
| 245 |
struct agx_fs_epilog_link_info link; |
| 246 |
|
| 247 |
/* Blend state. Blending happens in the epilog. */ |
| 248 |
struct agx_blend_key blend; |
| 249 |
|
| 250 |
/* Colour attachment remapping for Vulkan. Negative values indicate that an |
| 251 |
* attachment is discarded. Positive values indicate the output location we |
| 252 |
* want to store at the indexed colour attachment. |
| 253 |
*/ |
| 254 |
int8_t remap[8]; |
| 255 |
|
| 256 |
/* Tilebuffer configuration */ |
| 257 |
enum pipe_format rt_formats[8]; |
| 258 |
uint8_t nr_samples; |
| 259 |
bool force_small_tile; |
| 260 |
}; |
| 261 |
|
| 262 |
void agx_nir_vs_prolog(struct nir_builder *b, const void *key_); |
| 263 |
void agx_nir_fs_epilog(struct nir_builder *b, const void *key_); |
| 264 |
void agx_nir_fs_prolog(struct nir_builder *b, const void *key_); |
| 265 |
|
| 266 |
bool agx_nir_gather_vs_inputs(nir_shader *s, |
| 267 |
BITSET_WORD *attrib_components_read); |
| 268 |
|
| 269 |
bool agx_nir_lower_vs_input_to_prolog(nir_shader *s); |
| 270 |
|
| 271 |
bool agx_nir_lower_fs_output_to_epilog(nir_shader *s, |
| 272 |
struct agx_fs_epilog_link_info *out); |
| 273 |
|
| 274 |
bool agx_nir_lower_fs_active_samples_to_register(nir_shader *s); |
| 275 |
|
| 276 |
bool agx_nir_lower_non_monolithic_uniforms(nir_shader *nir, unsigned nr); |