| 1 |
/* |
| 2 |
* Copyright © 2012 Rob Clark <[email protected]> |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
* |
| 5 |
* Authors: |
| 6 |
* Rob Clark <[email protected]> |
| 7 |
* Jonathan Marek <[email protected]> |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "nir/tgsi_to_nir.h" |
| 11 |
#include "pipe/p_state.h" |
| 12 |
#include "tgsi/tgsi_dump.h" |
| 13 |
#include "util/format/u_format.h" |
| 14 |
#include "util/u_inlines.h" |
| 15 |
#include "util/u_memory.h" |
| 16 |
#include "util/u_string.h" |
| 17 |
|
| 18 |
#include "freedreno_program.h" |
| 19 |
|
| 20 |
#include "ir2/instr-a2xx.h" |
| 21 |
#include "fd2_program.h" |
| 22 |
#include "fd2_texture.h" |
| 23 |
#include "fd2_util.h" |
| 24 |
#include "ir2.h" |
| 25 |
|
| 26 |
static void |
| 27 |
fd2_shader_state_delete(struct pipe_context *pctx, void *hwcso) |
| 28 |
{ |
| 29 |
struct fd2_shader_stateobj *so = hwcso; |
| 30 |
if (!so) |
| 31 |
return; |
| 32 |
ralloc_free(so->nir); |
| 33 |
for (int i = 0; i < ARRAY_SIZE(so->variant); i++) |
| 34 |
free(so->variant[i].info.dwords); |
| 35 |
free(so); |
| 36 |
} |
| 37 |
|
| 38 |
static void |
| 39 |
emit(struct fd_ringbuffer *ring, mesa_shader_stage type, |
| 40 |
struct ir2_shader_info *info, struct util_dynarray *patches) |
| 41 |
{ |
| 42 |
unsigned i; |
| 43 |
|
| 44 |
assert(info->sizedwords); |
| 45 |
|
| 46 |
OUT_PKT3(ring, CP_IM_LOAD_IMMEDIATE, 2 + info->sizedwords); |
| 47 |
OUT_RING(ring, type == MESA_SHADER_FRAGMENT); |
| 48 |
OUT_RING(ring, info->sizedwords); |
| 49 |
|
| 50 |
if (patches) { |
| 51 |
util_dynarray_append(patches, &ring->cur[info->mem_export_ptr]); |
| 52 |
} |
| 53 |
|
| 54 |
for (i = 0; i < info->sizedwords; i++) |
| 55 |
OUT_RING(ring, info->dwords[i]); |
| 56 |
} |
| 57 |
|
| 58 |
static int |
| 59 |
ir2_glsl_type_size(const struct glsl_type *type, bool bindless) |
| 60 |
{ |
| 61 |
return glsl_count_attribute_slots(type, false); |
| 62 |
} |
| 63 |
|
| 64 |
static void * |
| 65 |
fd2_shader_state_create(struct pipe_context *pctx, |
| 66 |
const struct pipe_shader_state *cso) |
| 67 |
{ |
| 68 |
struct fd2_shader_stateobj *so = CALLOC_STRUCT(fd2_shader_stateobj); |
| 69 |
if (!so) |
| 70 |
return NULL; |
| 71 |
|
| 72 |
so->nir = (cso->type == PIPE_SHADER_IR_NIR) |
| 73 |
? cso->ir.nir |
| 74 |
: tgsi_to_nir(cso->tokens, pctx->screen, false); |
| 75 |
so->type = so->nir->info.stage; |
| 76 |
so->is_a20x = is_a20x(fd_context(pctx)->screen); |
| 77 |
|
| 78 |
NIR_PASS(_, so->nir, nir_lower_io, nir_var_shader_in | nir_var_shader_out, |
| 79 |
ir2_glsl_type_size, 0); |
| 80 |
|
| 81 |
if (ir2_optimize_nir(so->nir, true)) |
| 82 |
goto fail; |
| 83 |
|
| 84 |
so->first_immediate = so->nir->num_uniforms; |
| 85 |
|
| 86 |
ir2_compile(so, 0, NULL); |
| 87 |
|
| 88 |
/* Free FS NIR now. VS NIR will need to stick around for the draw variant |
| 89 |
* later. |
| 90 |
*/ |
| 91 |
if (so->nir->info.stage == MESA_SHADER_FRAGMENT) { |
| 92 |
ralloc_free(so->nir); |
| 93 |
so->nir = NULL; |
| 94 |
} |
| 95 |
|
| 96 |
return so; |
| 97 |
|
| 98 |
fail: |
| 99 |
fd2_shader_state_delete(pctx, so); |
| 100 |
return NULL; |
| 101 |
} |
| 102 |
|
| 103 |
static void |
| 104 |
patch_vtx_fetch(struct fd_context *ctx, struct pipe_vertex_element *elem, |
| 105 |
instr_fetch_vtx_t *instr, uint16_t dst_swiz) assert_dt |
| 106 |
{ |
| 107 |
struct surface_format fmt = fd2_pipe2surface(elem->src_format); |
| 108 |
|
| 109 |
instr->dst_swiz = fd2_vtx_swiz(elem->src_format, dst_swiz); |
| 110 |
instr->format_comp_all = fmt.sign == SQ_TEX_SIGN_SIGNED; |
| 111 |
instr->num_format_all = fmt.num_format; |
| 112 |
instr->format = fmt.format; |
| 113 |
instr->exp_adjust_all = fmt.exp_adjust; |
| 114 |
instr->stride = elem->src_stride; |
| 115 |
instr->offset = elem->src_offset; |
| 116 |
} |
| 117 |
|
| 118 |
static void |
| 119 |
patch_fetches(struct fd_context *ctx, struct ir2_shader_info *info, |
| 120 |
struct fd_vertex_stateobj *vtx, |
| 121 |
struct fd_texture_stateobj *tex) assert_dt |
| 122 |
{ |
| 123 |
for (int i = 0; i < info->num_fetch_instrs; i++) { |
| 124 |
struct ir2_fetch_info *fi = &info->fetch_info[i]; |
| 125 |
|
| 126 |
instr_fetch_t *instr = (instr_fetch_t *)&info->dwords[fi->offset]; |
| 127 |
if (instr->opc == VTX_FETCH) { |
| 128 |
unsigned idx = |
| 129 |
(instr->vtx.const_index - 20) * 3 + instr->vtx.const_index_sel; |
| 130 |
patch_vtx_fetch(ctx, &vtx->pipe[idx], &instr->vtx, fi->vtx.dst_swiz); |
| 131 |
continue; |
| 132 |
} |
| 133 |
|
| 134 |
assert(instr->opc == TEX_FETCH); |
| 135 |
instr->tex.const_idx = fd2_get_const_idx(ctx, tex, fi->tex.samp_id); |
| 136 |
instr->tex.src_swiz = fi->tex.src_swiz; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
void |
| 141 |
fd2_program_emit(struct fd_context *ctx, struct fd_ringbuffer *ring, |
| 142 |
struct fd_program_stateobj *prog) |
| 143 |
{ |
| 144 |
struct fd2_shader_stateobj *fp = NULL, *vp; |
| 145 |
struct ir2_shader_info *fpi, *vpi; |
| 146 |
struct ir2_frag_linkage *f; |
| 147 |
uint8_t vs_gprs, fs_gprs = 0, vs_export = 0; |
| 148 |
enum a2xx_sq_ps_vtx_mode mode = POSITION_1_VECTOR; |
| 149 |
bool binning = (ctx->batch && ring == ctx->batch->binning); |
| 150 |
unsigned variant = 0; |
| 151 |
|
| 152 |
vp = prog->vs; |
| 153 |
|
| 154 |
/* find variant matching the linked fragment shader */ |
| 155 |
if (!binning) { |
| 156 |
fp = prog->fs; |
| 157 |
for (variant = 1; variant < ARRAY_SIZE(vp->variant); variant++) { |
| 158 |
/* if checked all variants, compile a new variant */ |
| 159 |
if (!vp->variant[variant].info.sizedwords) { |
| 160 |
ir2_compile(vp, variant, fp); |
| 161 |
break; |
| 162 |
} |
| 163 |
|
| 164 |
/* check if fragment shader linkage matches */ |
| 165 |
if (!memcmp(&vp->variant[variant].f, &fp->variant[0].f, |
| 166 |
sizeof(struct ir2_frag_linkage))) |
| 167 |
break; |
| 168 |
} |
| 169 |
assert(variant < ARRAY_SIZE(vp->variant)); |
| 170 |
} |
| 171 |
|
| 172 |
vpi = &vp->variant[variant].info; |
| 173 |
fpi = &fp->variant[0].info; |
| 174 |
f = &fp->variant[0].f; |
| 175 |
|
| 176 |
/* clear/gmem2mem/mem2gmem need to be changed to remove this condition */ |
| 177 |
if (prog != &ctx->solid_prog && prog != &ctx->blit_prog[0]) { |
| 178 |
patch_fetches(ctx, vpi, ctx->vtx.vtx, &ctx->tex[MESA_SHADER_VERTEX]); |
| 179 |
if (fp) |
| 180 |
patch_fetches(ctx, fpi, NULL, &ctx->tex[MESA_SHADER_FRAGMENT]); |
| 181 |
} |
| 182 |
|
| 183 |
emit(ring, MESA_SHADER_VERTEX, vpi, |
| 184 |
binning ? &ctx->batch->shader_patches : NULL); |
| 185 |
|
| 186 |
if (fp) { |
| 187 |
emit(ring, MESA_SHADER_FRAGMENT, fpi, NULL); |
| 188 |
fs_gprs = (fpi->max_reg < 0) ? 0x80 : fpi->max_reg; |
| 189 |
vs_export = MAX2(1, f->inputs_count) - 1; |
| 190 |
} |
| 191 |
|
| 192 |
vs_gprs = (vpi->max_reg < 0) ? 0x80 : vpi->max_reg; |
| 193 |
|
| 194 |
if (vp->writes_psize && !binning) |
| 195 |
mode = POSITION_2_VECTORS_SPRITE; |
| 196 |
|
| 197 |
/* set register to use for param (fragcoord/pointcoord/frontfacing) */ |
| 198 |
OUT_PKT3(ring, CP_SET_CONSTANT, 2); |
| 199 |
OUT_RING(ring, CP_REG(REG_A2XX_SQ_CONTEXT_MISC)); |
| 200 |
OUT_RING(ring, |
| 201 |
A2XX_SQ_CONTEXT_MISC_SC_SAMPLE_CNTL(CENTERS_ONLY) | |
| 202 |
COND(fp, A2XX_SQ_CONTEXT_MISC_PARAM_GEN_POS(f->inputs_count)) | |
| 203 |
/* we need SCREEN_XY for both fragcoord and frontfacing */ |
| 204 |
A2XX_SQ_CONTEXT_MISC_SC_OUTPUT_SCREEN_XY); |
| 205 |
|
| 206 |
OUT_PKT3(ring, CP_SET_CONSTANT, 2); |
| 207 |
OUT_RING(ring, CP_REG(REG_A2XX_SQ_PROGRAM_CNTL)); |
| 208 |
OUT_RING(ring, |
| 209 |
A2XX_SQ_PROGRAM_CNTL_PS_EXPORT_MODE(2) | |
| 210 |
A2XX_SQ_PROGRAM_CNTL_VS_EXPORT_MODE(mode) | |
| 211 |
A2XX_SQ_PROGRAM_CNTL_VS_RESOURCE | |
| 212 |
A2XX_SQ_PROGRAM_CNTL_PS_RESOURCE | |
| 213 |
A2XX_SQ_PROGRAM_CNTL_VS_EXPORT_COUNT(vs_export) | |
| 214 |
A2XX_SQ_PROGRAM_CNTL_PS_REGS(fs_gprs) | |
| 215 |
A2XX_SQ_PROGRAM_CNTL_VS_REGS(vs_gprs) | |
| 216 |
COND(fp && fp->need_param, A2XX_SQ_PROGRAM_CNTL_PARAM_GEN) | |
| 217 |
COND(!fp, A2XX_SQ_PROGRAM_CNTL_GEN_INDEX_VTX)); |
| 218 |
} |
| 219 |
|
| 220 |
void |
| 221 |
fd2_prog_init(struct pipe_context *pctx) |
| 222 |
{ |
| 223 |
struct fd_context *ctx = fd_context(pctx); |
| 224 |
struct fd_program_stateobj *prog; |
| 225 |
struct fd2_shader_stateobj *so; |
| 226 |
struct ir2_shader_info *info; |
| 227 |
instr_fetch_vtx_t *instr; |
| 228 |
|
| 229 |
pctx->create_fs_state = fd2_shader_state_create; |
| 230 |
pctx->delete_fs_state = fd2_shader_state_delete; |
| 231 |
|
| 232 |
pctx->create_vs_state = fd2_shader_state_create; |
| 233 |
pctx->delete_vs_state = fd2_shader_state_delete; |
| 234 |
|
| 235 |
fd_prog_init(pctx); |
| 236 |
|
| 237 |
/* XXX maybe its possible to reuse patch_vtx_fetch somehow? */ |
| 238 |
|
| 239 |
prog = &ctx->solid_prog; |
| 240 |
so = prog->vs; |
| 241 |
ir2_compile(prog->vs, 1, prog->fs); |
| 242 |
|
| 243 |
#define IR2_FETCH_SWIZ_XY01 0xb08 |
| 244 |
#define IR2_FETCH_SWIZ_XYZ1 0xa88 |
| 245 |
|
| 246 |
info = &so->variant[1].info; |
| 247 |
|
| 248 |
instr = (instr_fetch_vtx_t *)&info->dwords[info->fetch_info[0].offset]; |
| 249 |
instr->const_index = 26; |
| 250 |
instr->const_index_sel = 0; |
| 251 |
instr->format = FMT_32_32_32_FLOAT; |
| 252 |
instr->format_comp_all = false; |
| 253 |
instr->stride = 12; |
| 254 |
instr->num_format_all = true; |
| 255 |
instr->dst_swiz = IR2_FETCH_SWIZ_XYZ1; |
| 256 |
|
| 257 |
prog = &ctx->blit_prog[0]; |
| 258 |
so = prog->vs; |
| 259 |
ir2_compile(prog->vs, 1, prog->fs); |
| 260 |
|
| 261 |
info = &so->variant[1].info; |
| 262 |
|
| 263 |
instr = (instr_fetch_vtx_t *)&info->dwords[info->fetch_info[0].offset]; |
| 264 |
instr->const_index = 26; |
| 265 |
instr->const_index_sel = 1; |
| 266 |
instr->format = FMT_32_32_FLOAT; |
| 267 |
instr->format_comp_all = false; |
| 268 |
instr->stride = 8; |
| 269 |
instr->num_format_all = false; |
| 270 |
instr->dst_swiz = IR2_FETCH_SWIZ_XY01; |
| 271 |
|
| 272 |
instr = (instr_fetch_vtx_t *)&info->dwords[info->fetch_info[1].offset]; |
| 273 |
instr->const_index = 26; |
| 274 |
instr->const_index_sel = 0; |
| 275 |
instr->format = FMT_32_32_32_FLOAT; |
| 276 |
instr->format_comp_all = false; |
| 277 |
instr->stride = 12; |
| 278 |
instr->num_format_all = false; |
| 279 |
instr->dst_swiz = IR2_FETCH_SWIZ_XYZ1; |
| 280 |
} |