| 1 |
/* |
| 2 |
* Copyright (C) 2021 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#ifndef __PAN_SHADER_H__ |
| 7 |
#define __PAN_SHADER_H__ |
| 8 |
|
| 9 |
#include "compiler/nir/nir.h" |
| 10 |
#include "genxml/gen_macros.h" |
| 11 |
#include "panfrost/lib/pan_props.h" |
| 12 |
#include "panfrost/compiler/pan_compiler.h" |
| 13 |
|
| 14 |
#ifdef PAN_ARCH |
| 15 |
|
| 16 |
#if PAN_ARCH >= 9 |
| 17 |
static inline enum mali_shader_stage |
| 18 |
pan_shader_stage(const struct pan_shader_info *info) |
| 19 |
{ |
| 20 |
switch (info->stage) { |
| 21 |
case MESA_SHADER_VERTEX: |
| 22 |
return MALI_SHADER_STAGE_VERTEX; |
| 23 |
case MESA_SHADER_FRAGMENT: |
| 24 |
return MALI_SHADER_STAGE_FRAGMENT; |
| 25 |
default: |
| 26 |
return MALI_SHADER_STAGE_COMPUTE; |
| 27 |
} |
| 28 |
} |
| 29 |
#endif |
| 30 |
|
| 31 |
#if PAN_ARCH >= 7 |
| 32 |
static inline enum mali_shader_register_allocation |
| 33 |
pan_register_allocation(unsigned work_reg_count) |
| 34 |
{ |
| 35 |
return (work_reg_count <= 32) |
| 36 |
? MALI_SHADER_REGISTER_ALLOCATION_32_PER_THREAD |
| 37 |
: MALI_SHADER_REGISTER_ALLOCATION_64_PER_THREAD; |
| 38 |
} |
| 39 |
#endif |
| 40 |
|
| 41 |
static inline enum mali_depth_source |
| 42 |
pan_depth_source(const struct pan_shader_info *info) |
| 43 |
{ |
| 44 |
return info->fs.writes_depth ? MALI_DEPTH_SOURCE_SHADER |
| 45 |
: MALI_DEPTH_SOURCE_FIXED_FUNCTION; |
| 46 |
} |
| 47 |
|
| 48 |
#if PAN_ARCH < 9 |
| 49 |
#if PAN_ARCH <= 5 |
| 50 |
static inline void |
| 51 |
pan_shader_prepare_midgard_rsd(const struct pan_shader_info *info, |
| 52 |
struct MALI_RENDERER_STATE *rsd) |
| 53 |
{ |
| 54 |
assert((info->push.count & 3) == 0); |
| 55 |
|
| 56 |
rsd->properties.uniform_count = info->push.count / 4; |
| 57 |
rsd->properties.shader_has_side_effects = info->writes_global; |
| 58 |
rsd->properties.fp_mode = MALI_FP_MODE_GL_INF_NAN_ALLOWED; |
| 59 |
|
| 60 |
/* For fragment shaders, work register count, early-z, reads at draw-time */ |
| 61 |
|
| 62 |
if (info->stage != MESA_SHADER_FRAGMENT) { |
| 63 |
rsd->properties.work_register_count = info->work_reg_count; |
| 64 |
} else { |
| 65 |
rsd->properties.shader_reads_tilebuffer = info->fs.outputs_read; |
| 66 |
|
| 67 |
/* However, forcing early-z in the shader overrides draw-time */ |
| 68 |
rsd->properties.force_early_z = info->fs.early_fragment_tests; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
#else |
| 73 |
|
| 74 |
#define pan_preloads(reg) (preload & BITFIELD64_BIT(reg)) |
| 75 |
|
| 76 |
static void |
| 77 |
pan_make_preload(mesa_shader_stage stage, uint64_t preload, |
| 78 |
struct MALI_PRELOAD *out) |
| 79 |
{ |
| 80 |
switch (stage) { |
| 81 |
case MESA_SHADER_VERTEX: |
| 82 |
out->vertex.position_result_address_lo = pan_preloads(58); |
| 83 |
out->vertex.position_result_address_hi = pan_preloads(59); |
| 84 |
out->vertex.vertex_id = pan_preloads(61); |
| 85 |
out->vertex.instance_id = pan_preloads(62); |
| 86 |
break; |
| 87 |
|
| 88 |
case MESA_SHADER_FRAGMENT: |
| 89 |
out->fragment.primitive_id = pan_preloads(57); |
| 90 |
out->fragment.primitive_flags = pan_preloads(58); |
| 91 |
out->fragment.fragment_position = pan_preloads(59); |
| 92 |
out->fragment.sample_mask_id = pan_preloads(61); |
| 93 |
out->fragment.coverage = true; |
| 94 |
break; |
| 95 |
|
| 96 |
default: |
| 97 |
out->compute.local_invocation_xy = pan_preloads(55); |
| 98 |
out->compute.local_invocation_z = pan_preloads(56); |
| 99 |
out->compute.work_group_x = pan_preloads(57); |
| 100 |
out->compute.work_group_y = pan_preloads(58); |
| 101 |
out->compute.work_group_z = pan_preloads(59); |
| 102 |
out->compute.global_invocation_x = pan_preloads(60); |
| 103 |
out->compute.global_invocation_y = pan_preloads(61); |
| 104 |
out->compute.global_invocation_z = pan_preloads(62); |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
#if PAN_ARCH == 7 |
| 110 |
static inline void |
| 111 |
pan_pack_message_preload(struct MALI_MESSAGE_PRELOAD *cfg, |
| 112 |
const struct bifrost_message_preload *msg) |
| 113 |
{ |
| 114 |
enum mali_message_preload_register_format regfmt = |
| 115 |
msg->fp16 ? MALI_MESSAGE_PRELOAD_REGISTER_FORMAT_F16 |
| 116 |
: MALI_MESSAGE_PRELOAD_REGISTER_FORMAT_F32; |
| 117 |
|
| 118 |
if (msg->enabled && msg->texture) { |
| 119 |
cfg->type = MALI_MESSAGE_TYPE_VAR_TEX; |
| 120 |
cfg->var_tex.varying_index = msg->varying_index; |
| 121 |
cfg->var_tex.texture_index = msg->texture_index; |
| 122 |
cfg->var_tex.register_format = regfmt; |
| 123 |
cfg->var_tex.skip = msg->skip; |
| 124 |
cfg->var_tex.zero_lod = msg->zero_lod; |
| 125 |
} else if (msg->enabled) { |
| 126 |
cfg->type = MALI_MESSAGE_TYPE_LD_VAR; |
| 127 |
cfg->ld_var.varying_index = msg->varying_index; |
| 128 |
cfg->ld_var.register_format = regfmt; |
| 129 |
cfg->ld_var.num_components = msg->num_components; |
| 130 |
} else { |
| 131 |
cfg->type = MALI_MESSAGE_TYPE_DISABLED; |
| 132 |
} |
| 133 |
} |
| 134 |
#endif |
| 135 |
|
| 136 |
static inline void |
| 137 |
pan_shader_prepare_bifrost_rsd(const struct pan_shader_info *info, |
| 138 |
struct MALI_RENDERER_STATE *rsd) |
| 139 |
{ |
| 140 |
unsigned fau_count = DIV_ROUND_UP(info->push.count, 2); |
| 141 |
rsd->preload.uniform_count = fau_count; |
| 142 |
|
| 143 |
#if PAN_ARCH >= 7 |
| 144 |
rsd->properties.shader_register_allocation = |
| 145 |
pan_register_allocation(info->work_reg_count); |
| 146 |
#endif |
| 147 |
|
| 148 |
pan_make_preload(info->stage, info->preload, &rsd->preload); |
| 149 |
|
| 150 |
if (info->stage == MESA_SHADER_FRAGMENT) { |
| 151 |
rsd->properties.shader_modifies_coverage = |
| 152 |
info->fs.writes_coverage || info->fs.can_discard; |
| 153 |
|
| 154 |
rsd->properties.allow_forward_pixel_to_be_killed = !info->writes_global; |
| 155 |
|
| 156 |
#if PAN_ARCH >= 7 |
| 157 |
rsd->properties.shader_wait_dependency_6 = info->bifrost.wait_6; |
| 158 |
rsd->properties.shader_wait_dependency_7 = info->bifrost.wait_7; |
| 159 |
|
| 160 |
pan_pack_message_preload(&rsd->message_preload_1, |
| 161 |
&info->bifrost.messages[0]); |
| 162 |
pan_pack_message_preload(&rsd->message_preload_2, |
| 163 |
&info->bifrost.messages[1]); |
| 164 |
#endif |
| 165 |
} else if (info->stage == MESA_SHADER_VERTEX && info->vs.secondary_enable) { |
| 166 |
rsd->secondary_preload.uniform_count = fau_count; |
| 167 |
|
| 168 |
pan_make_preload(info->stage, info->vs.secondary_preload, |
| 169 |
&rsd->secondary_preload); |
| 170 |
|
| 171 |
rsd->secondary_shader = rsd->shader.shader + info->vs.secondary_offset; |
| 172 |
|
| 173 |
#if PAN_ARCH >= 7 |
| 174 |
rsd->properties.secondary_shader_register_allocation = |
| 175 |
pan_register_allocation(info->vs.secondary_work_reg_count); |
| 176 |
#endif |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
#endif |
| 181 |
|
| 182 |
static inline void |
| 183 |
pan_shader_prepare_rsd(const struct pan_shader_info *shader_info, |
| 184 |
uint64_t shader_ptr, struct MALI_RENDERER_STATE *rsd) |
| 185 |
{ |
| 186 |
#if PAN_ARCH <= 5 |
| 187 |
shader_ptr |= shader_info->midgard.first_tag; |
| 188 |
#endif |
| 189 |
|
| 190 |
rsd->shader.shader = shader_ptr; |
| 191 |
rsd->shader.attribute_count = shader_info->attribute_count; |
| 192 |
rsd->shader.varying_count = shader_info->varyings.formats.count; |
| 193 |
rsd->shader.texture_count = shader_info->texture_count; |
| 194 |
rsd->shader.sampler_count = shader_info->sampler_count; |
| 195 |
rsd->properties.shader_contains_barrier = shader_info->contains_barrier; |
| 196 |
rsd->properties.uniform_buffer_count = shader_info->ubo_count; |
| 197 |
|
| 198 |
if (shader_info->stage == MESA_SHADER_FRAGMENT) { |
| 199 |
rsd->properties.stencil_from_shader = shader_info->fs.writes_stencil; |
| 200 |
rsd->properties.depth_source = pan_depth_source(shader_info); |
| 201 |
|
| 202 |
/* This also needs to be set if the API forces per-sample |
| 203 |
* shading, but that'll just got ORed in */ |
| 204 |
rsd->multisample_misc.evaluate_per_sample = |
| 205 |
shader_info->fs.sample_shading; |
| 206 |
} |
| 207 |
|
| 208 |
#if PAN_ARCH >= 6 |
| 209 |
pan_shader_prepare_bifrost_rsd(shader_info, rsd); |
| 210 |
#else |
| 211 |
pan_shader_prepare_midgard_rsd(shader_info, rsd); |
| 212 |
#endif |
| 213 |
} |
| 214 |
#endif /* PAN_ARCH */ |
| 215 |
#endif |
| 216 |
|
| 217 |
#endif |