xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/nouveau/nvc0/nvc0_program.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright 2010 Christoph Bumiller
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 shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "nir/pipe_nir.h"
24 #include "pipe/p_defines.h"
25 
26 #include "compiler/nir/nir.h"
27 #include "compiler/nir/nir_builder.h"
28 #include "util/blob.h"
29 
30 #include "nvc0/nvc0_context.h"
31 
32 #include "nv50_ir_driver.h"
33 #include "nvc0/nve4_compute.h"
34 
35 /* NOTE: Using a[0x270] in FP may cause an error even if we're using less than
36  * 124 scalar varying values.
37  */
38 static uint32_t
nvc0_shader_input_address(unsigned sn,unsigned si)39 nvc0_shader_input_address(unsigned sn, unsigned si)
40 {
41    switch (sn) {
42    case TGSI_SEMANTIC_TESSOUTER:    return 0x000 + si * 0x4;
43    case TGSI_SEMANTIC_TESSINNER:    return 0x010 + si * 0x4;
44    case TGSI_SEMANTIC_PATCH:        return 0x020 + si * 0x10;
45    case TGSI_SEMANTIC_PRIMID:       return 0x060;
46    case TGSI_SEMANTIC_LAYER:        return 0x064;
47    case TGSI_SEMANTIC_VIEWPORT_INDEX:return 0x068;
48    case TGSI_SEMANTIC_PSIZE:        return 0x06c;
49    case TGSI_SEMANTIC_POSITION:     return 0x070;
50    case TGSI_SEMANTIC_GENERIC:      return 0x080 + si * 0x10;
51    case TGSI_SEMANTIC_FOG:          return 0x2e8;
52    case TGSI_SEMANTIC_COLOR:        return 0x280 + si * 0x10;
53    case TGSI_SEMANTIC_BCOLOR:       return 0x2a0 + si * 0x10;
54    case TGSI_SEMANTIC_CLIPDIST:     return 0x2c0 + si * 0x10;
55    case TGSI_SEMANTIC_CLIPVERTEX:   return 0x270;
56    case TGSI_SEMANTIC_PCOORD:       return 0x2e0;
57    case TGSI_SEMANTIC_TESSCOORD:    return 0x2f0;
58    case TGSI_SEMANTIC_INSTANCEID:   return 0x2f8;
59    case TGSI_SEMANTIC_VERTEXID:     return 0x2fc;
60    case TGSI_SEMANTIC_TEXCOORD:     return 0x300 + si * 0x10;
61    default:
62       assert(!"invalid TGSI input semantic");
63       return ~0;
64    }
65 }
66 
67 static uint32_t
nvc0_shader_output_address(unsigned sn,unsigned si)68 nvc0_shader_output_address(unsigned sn, unsigned si)
69 {
70    switch (sn) {
71    case TGSI_SEMANTIC_TESSOUTER:     return 0x000 + si * 0x4;
72    case TGSI_SEMANTIC_TESSINNER:     return 0x010 + si * 0x4;
73    case TGSI_SEMANTIC_PATCH:         return 0x020 + si * 0x10;
74    case TGSI_SEMANTIC_PRIMID:        return 0x060;
75    case TGSI_SEMANTIC_LAYER:         return 0x064;
76    case TGSI_SEMANTIC_VIEWPORT_INDEX:return 0x068;
77    case TGSI_SEMANTIC_PSIZE:         return 0x06c;
78    case TGSI_SEMANTIC_POSITION:      return 0x070;
79    case TGSI_SEMANTIC_GENERIC:       return 0x080 + si * 0x10;
80    case TGSI_SEMANTIC_FOG:           return 0x2e8;
81    case TGSI_SEMANTIC_COLOR:         return 0x280 + si * 0x10;
82    case TGSI_SEMANTIC_BCOLOR:        return 0x2a0 + si * 0x10;
83    case TGSI_SEMANTIC_CLIPDIST:      return 0x2c0 + si * 0x10;
84    case TGSI_SEMANTIC_CLIPVERTEX:    return 0x270;
85    case TGSI_SEMANTIC_TEXCOORD:      return 0x300 + si * 0x10;
86    case TGSI_SEMANTIC_VIEWPORT_MASK: return 0x3a0;
87    case TGSI_SEMANTIC_EDGEFLAG:      return ~0;
88    default:
89       assert(!"invalid TGSI output semantic");
90       return ~0;
91    }
92 }
93 
94 static int
nvc0_vp_assign_input_slots(struct nv50_ir_prog_info_out * info)95 nvc0_vp_assign_input_slots(struct nv50_ir_prog_info_out *info)
96 {
97    unsigned i, c, n;
98 
99    for (n = 0, i = 0; i < info->numInputs; ++i) {
100       switch (info->in[i].sn) {
101       case TGSI_SEMANTIC_INSTANCEID: /* for SM4 only, in TGSI they're SVs */
102       case TGSI_SEMANTIC_VERTEXID:
103          info->in[i].mask = 0x1;
104          info->in[i].slot[0] =
105             nvc0_shader_input_address(info->in[i].sn, 0) / 4;
106          continue;
107       default:
108          break;
109       }
110       for (c = 0; c < 4; ++c)
111          info->in[i].slot[c] = (0x80 + n * 0x10 + c * 0x4) / 4;
112       ++n;
113    }
114 
115    return 0;
116 }
117 
118 static int
nvc0_sp_assign_input_slots(struct nv50_ir_prog_info_out * info)119 nvc0_sp_assign_input_slots(struct nv50_ir_prog_info_out *info)
120 {
121    unsigned offset;
122    unsigned i, c;
123 
124    for (i = 0; i < info->numInputs; ++i) {
125       offset = nvc0_shader_input_address(info->in[i].sn, info->in[i].si);
126 
127       for (c = 0; c < 4; ++c)
128          info->in[i].slot[c] = (offset + c * 0x4) / 4;
129    }
130 
131    return 0;
132 }
133 
134 static int
nvc0_fp_assign_output_slots(struct nv50_ir_prog_info_out * info)135 nvc0_fp_assign_output_slots(struct nv50_ir_prog_info_out *info)
136 {
137    unsigned count = info->prop.fp.numColourResults * 4;
138    unsigned i, c;
139 
140    /* Compute the relative position of each color output, since skipped MRT
141     * positions will not have registers allocated to them.
142     */
143    unsigned colors[8] = {0};
144    for (i = 0; i < info->numOutputs; ++i)
145       if (info->out[i].sn == TGSI_SEMANTIC_COLOR)
146          colors[info->out[i].si] = 1;
147    for (i = 0, c = 0; i < 8; i++)
148       if (colors[i])
149          colors[i] = c++;
150    for (i = 0; i < info->numOutputs; ++i)
151       if (info->out[i].sn == TGSI_SEMANTIC_COLOR)
152          for (c = 0; c < 4; ++c)
153             info->out[i].slot[c] = colors[info->out[i].si] * 4 + c;
154 
155    if (info->io.sampleMask < PIPE_MAX_SHADER_OUTPUTS)
156       info->out[info->io.sampleMask].slot[0] = count++;
157    else
158    if (info->target >= 0xe0)
159       count++; /* on Kepler, depth is always last colour reg + 2 */
160 
161    if (info->io.fragDepth < PIPE_MAX_SHADER_OUTPUTS)
162       info->out[info->io.fragDepth].slot[2] = count;
163 
164    return 0;
165 }
166 
167 static int
nvc0_sp_assign_output_slots(struct nv50_ir_prog_info_out * info)168 nvc0_sp_assign_output_slots(struct nv50_ir_prog_info_out *info)
169 {
170    unsigned offset;
171    unsigned i, c;
172 
173    for (i = 0; i < info->numOutputs; ++i) {
174       offset = nvc0_shader_output_address(info->out[i].sn, info->out[i].si);
175 
176       for (c = 0; c < 4; ++c)
177          info->out[i].slot[c] = (offset + c * 0x4) / 4;
178    }
179 
180    return 0;
181 }
182 
183 static int
nvc0_program_assign_varying_slots(struct nv50_ir_prog_info_out * info)184 nvc0_program_assign_varying_slots(struct nv50_ir_prog_info_out *info)
185 {
186    int ret;
187 
188    if (info->type == PIPE_SHADER_VERTEX)
189       ret = nvc0_vp_assign_input_slots(info);
190    else
191       ret = nvc0_sp_assign_input_slots(info);
192    if (ret)
193       return ret;
194 
195    if (info->type == PIPE_SHADER_FRAGMENT)
196       ret = nvc0_fp_assign_output_slots(info);
197    else
198       ret = nvc0_sp_assign_output_slots(info);
199    return ret;
200 }
201 
202 static inline void
nvc0_vtgp_hdr_update_oread(struct nvc0_program * vp,uint8_t slot)203 nvc0_vtgp_hdr_update_oread(struct nvc0_program *vp, uint8_t slot)
204 {
205    uint8_t min = (vp->hdr[4] >> 12) & 0xff;
206    uint8_t max = (vp->hdr[4] >> 24);
207 
208    min = MIN2(min, slot);
209    max = MAX2(max, slot);
210 
211    vp->hdr[4] = (max << 24) | (min << 12);
212 }
213 
214 /* Common part of header generation for VP, TCP, TEP and GP. */
215 static int
nvc0_vtgp_gen_header(struct nvc0_program * vp,struct nv50_ir_prog_info_out * info)216 nvc0_vtgp_gen_header(struct nvc0_program *vp, struct nv50_ir_prog_info_out *info)
217 {
218    unsigned i, c, a;
219 
220    for (i = 0; i < info->numInputs; ++i) {
221       if (info->in[i].patch)
222          continue;
223       for (c = 0; c < 4; ++c) {
224          a = info->in[i].slot[c];
225          if (info->in[i].mask & (1 << c))
226             vp->hdr[5 + a / 32] |= 1 << (a % 32);
227       }
228    }
229 
230    for (i = 0; i < info->numOutputs; ++i) {
231       if (info->out[i].patch)
232          continue;
233       for (c = 0; c < 4; ++c) {
234          if (!(info->out[i].mask & (1 << c)))
235             continue;
236          assert(info->out[i].slot[c] >= 0x40 / 4);
237          a = info->out[i].slot[c] - 0x40 / 4;
238          vp->hdr[13 + a / 32] |= 1 << (a % 32);
239          if (info->out[i].oread)
240             nvc0_vtgp_hdr_update_oread(vp, info->out[i].slot[c]);
241       }
242    }
243 
244    for (i = 0; i < info->numSysVals; ++i) {
245       switch (info->sv[i].sn) {
246       case SYSTEM_VALUE_PRIMITIVE_ID:
247          vp->hdr[5] |= 1 << 24;
248          break;
249       case SYSTEM_VALUE_INSTANCE_ID:
250          vp->hdr[10] |= 1 << 30;
251          break;
252       case SYSTEM_VALUE_VERTEX_ID:
253          vp->hdr[10] |= 1 << 31;
254          break;
255       case SYSTEM_VALUE_TESS_COORD:
256          /* We don't have the mask, nor the slots populated. While this could
257           * be achieved, the vast majority of the time if either of the coords
258           * are read, then both will be read.
259           */
260          nvc0_vtgp_hdr_update_oread(vp, 0x2f0 / 4);
261          nvc0_vtgp_hdr_update_oread(vp, 0x2f4 / 4);
262          break;
263       default:
264          break;
265       }
266    }
267 
268    vp->vp.clip_enable = (1 << info->io.clipDistances) - 1;
269    vp->vp.cull_enable =
270       ((1 << info->io.cullDistances) - 1) << info->io.clipDistances;
271    for (i = 0; i < info->io.cullDistances; ++i)
272       vp->vp.clip_mode |= 1 << ((info->io.clipDistances + i) * 4);
273 
274    if (info->io.genUserClip < 0)
275       vp->vp.num_ucps = PIPE_MAX_CLIP_PLANES + 1; /* prevent rebuilding */
276 
277    vp->vp.layer_viewport_relative = info->io.layer_viewport_relative;
278 
279    return 0;
280 }
281 
282 static int
nvc0_vp_gen_header(struct nvc0_program * vp,struct nv50_ir_prog_info_out * info)283 nvc0_vp_gen_header(struct nvc0_program *vp, struct nv50_ir_prog_info_out *info)
284 {
285    vp->hdr[0] = 0x20061 | (1 << 10);
286    vp->hdr[4] = 0xff000;
287 
288    return nvc0_vtgp_gen_header(vp, info);
289 }
290 
291 static void
nvc0_tp_get_tess_mode(struct nvc0_program * tp,struct nv50_ir_prog_info_out * info)292 nvc0_tp_get_tess_mode(struct nvc0_program *tp, struct nv50_ir_prog_info_out *info)
293 {
294    if (info->prop.tp.outputPrim == MESA_PRIM_COUNT) {
295       tp->tp.tess_mode = ~0;
296       return;
297    }
298    switch (info->prop.tp.domain) {
299    case MESA_PRIM_LINES:
300       tp->tp.tess_mode = NVC0_3D_TESS_MODE_PRIM_ISOLINES;
301       break;
302    case MESA_PRIM_TRIANGLES:
303       tp->tp.tess_mode = NVC0_3D_TESS_MODE_PRIM_TRIANGLES;
304       break;
305    case MESA_PRIM_QUADS:
306       tp->tp.tess_mode = NVC0_3D_TESS_MODE_PRIM_QUADS;
307       break;
308    default:
309       tp->tp.tess_mode = ~0;
310       return;
311    }
312 
313    /* It seems like lines want the "CW" bit to indicate they're connected, and
314     * spit out errors in dmesg when the "CONNECTED" bit is set.
315     */
316    if (info->prop.tp.outputPrim != MESA_PRIM_POINTS) {
317       if (info->prop.tp.domain == MESA_PRIM_LINES)
318          tp->tp.tess_mode |= NVC0_3D_TESS_MODE_CW;
319       else
320          tp->tp.tess_mode |= NVC0_3D_TESS_MODE_CONNECTED;
321    }
322 
323    /* Winding only matters for triangles/quads, not lines. */
324    if (info->prop.tp.domain != MESA_PRIM_LINES &&
325        info->prop.tp.outputPrim != MESA_PRIM_POINTS &&
326        info->prop.tp.winding > 0)
327       tp->tp.tess_mode |= NVC0_3D_TESS_MODE_CW;
328 
329    switch (info->prop.tp.partitioning) {
330    case PIPE_TESS_SPACING_EQUAL:
331       tp->tp.tess_mode |= NVC0_3D_TESS_MODE_SPACING_EQUAL;
332       break;
333    case PIPE_TESS_SPACING_FRACTIONAL_ODD:
334       tp->tp.tess_mode |= NVC0_3D_TESS_MODE_SPACING_FRACTIONAL_ODD;
335       break;
336    case PIPE_TESS_SPACING_FRACTIONAL_EVEN:
337       tp->tp.tess_mode |= NVC0_3D_TESS_MODE_SPACING_FRACTIONAL_EVEN;
338       break;
339    default:
340       assert(!"invalid tessellator partitioning");
341       break;
342    }
343 }
344 
345 static int
nvc0_tcp_gen_header(struct nvc0_program * tcp,struct nv50_ir_prog_info_out * info)346 nvc0_tcp_gen_header(struct nvc0_program *tcp, struct nv50_ir_prog_info_out *info)
347 {
348    unsigned opcs = 6; /* output patch constants (at least the TessFactors) */
349 
350    if (info->numPatchConstants)
351       opcs = 8 + info->numPatchConstants * 4;
352 
353    tcp->hdr[0] = 0x20061 | (2 << 10);
354 
355    tcp->hdr[1] = opcs << 24;
356    tcp->hdr[2] = info->prop.tp.outputPatchSize << 24;
357 
358    tcp->hdr[4] = 0xff000; /* initial min/max parallel output read address */
359 
360    nvc0_vtgp_gen_header(tcp, info);
361 
362    if (info->target >= NVISA_GM107_CHIPSET) {
363       /* On GM107+, the number of output patch components has moved in the TCP
364        * header, but it seems like blob still also uses the old position.
365        * Also, the high 8-bits are located in between the min/max parallel
366        * field and has to be set after updating the outputs. */
367       tcp->hdr[3] = (opcs & 0x0f) << 28;
368       tcp->hdr[4] |= (opcs & 0xf0) << 16;
369    }
370 
371    nvc0_tp_get_tess_mode(tcp, info);
372 
373    return 0;
374 }
375 
376 static int
nvc0_tep_gen_header(struct nvc0_program * tep,struct nv50_ir_prog_info_out * info)377 nvc0_tep_gen_header(struct nvc0_program *tep, struct nv50_ir_prog_info_out *info)
378 {
379    tep->hdr[0] = 0x20061 | (3 << 10);
380    tep->hdr[4] = 0xff000;
381 
382    nvc0_vtgp_gen_header(tep, info);
383 
384    nvc0_tp_get_tess_mode(tep, info);
385 
386    tep->hdr[18] |= 0x3 << 12; /* ? */
387 
388    return 0;
389 }
390 
391 static int
nvc0_gp_gen_header(struct nvc0_program * gp,struct nv50_ir_prog_info_out * info)392 nvc0_gp_gen_header(struct nvc0_program *gp, struct nv50_ir_prog_info_out *info)
393 {
394    gp->hdr[0] = 0x20061 | (4 << 10);
395 
396    gp->hdr[2] = MIN2(info->prop.gp.instanceCount, 32) << 24;
397 
398    switch (info->prop.gp.outputPrim) {
399    case MESA_PRIM_POINTS:
400       gp->hdr[3] = 0x01000000;
401       gp->hdr[0] |= 0xf0000000;
402       break;
403    case MESA_PRIM_LINE_STRIP:
404       gp->hdr[3] = 0x06000000;
405       gp->hdr[0] |= 0x10000000;
406       break;
407    case MESA_PRIM_TRIANGLE_STRIP:
408       gp->hdr[3] = 0x07000000;
409       gp->hdr[0] |= 0x10000000;
410       break;
411    default:
412       assert(0);
413       break;
414    }
415 
416    gp->hdr[4] = CLAMP(info->prop.gp.maxVertices, 1, 1024);
417 
418    return nvc0_vtgp_gen_header(gp, info);
419 }
420 
421 #define NVC0_INTERP_FLAT          (1 << 0)
422 #define NVC0_INTERP_PERSPECTIVE   (2 << 0)
423 #define NVC0_INTERP_LINEAR        (3 << 0)
424 #define NVC0_INTERP_CENTROID      (1 << 2)
425 
426 static uint8_t
nvc0_hdr_interp_mode(const struct nv50_ir_varying * var)427 nvc0_hdr_interp_mode(const struct nv50_ir_varying *var)
428 {
429    if (var->linear)
430       return NVC0_INTERP_LINEAR;
431    if (var->flat)
432       return NVC0_INTERP_FLAT;
433    return NVC0_INTERP_PERSPECTIVE;
434 }
435 
436 static int
nvc0_fp_gen_header(struct nvc0_program * fp,struct nv50_ir_prog_info_out * info)437 nvc0_fp_gen_header(struct nvc0_program *fp, struct nv50_ir_prog_info_out *info)
438 {
439    unsigned i, c, a, m;
440 
441    /* just 00062 on Kepler */
442    fp->hdr[0] = 0x20062 | (5 << 10);
443    fp->hdr[5] = 0x80000000; /* getting a trap if FRAG_COORD_UMASK.w = 0 */
444 
445    if (info->prop.fp.usesDiscard)
446       fp->hdr[0] |= 0x8000;
447    if (!info->prop.fp.separateFragData)
448       fp->hdr[0] |= 0x4000;
449    if (info->io.sampleMask < PIPE_MAX_SHADER_OUTPUTS)
450       fp->hdr[19] |= 0x1;
451    if (info->prop.fp.writesDepth) {
452       fp->hdr[19] |= 0x2;
453       fp->flags[0] = 0x11; /* deactivate ZCULL */
454    }
455 
456    for (i = 0; i < info->numInputs; ++i) {
457       m = nvc0_hdr_interp_mode(&info->in[i]);
458       if (info->in[i].sn == TGSI_SEMANTIC_COLOR) {
459          fp->fp.colors |= 1 << info->in[i].si;
460          if (info->in[i].sc)
461             fp->fp.color_interp[info->in[i].si] = m | (info->in[i].mask << 4);
462       }
463       for (c = 0; c < 4; ++c) {
464          if (!(info->in[i].mask & (1 << c)))
465             continue;
466          a = info->in[i].slot[c];
467          if (info->in[i].slot[0] >= (0x060 / 4) &&
468              info->in[i].slot[0] <= (0x07c / 4)) {
469             fp->hdr[5] |= 1 << (24 + (a - 0x060 / 4));
470          } else
471          if (info->in[i].slot[0] >= (0x2c0 / 4) &&
472              info->in[i].slot[0] <= (0x2fc / 4)) {
473             fp->hdr[14] |= (1 << (a - 0x280 / 4)) & 0x07ff0000;
474          } else {
475             if (info->in[i].slot[c] < (0x040 / 4) ||
476                 info->in[i].slot[c] > (0x380 / 4))
477                continue;
478             a *= 2;
479             if (info->in[i].slot[0] >= (0x300 / 4))
480                a -= 32;
481             fp->hdr[4 + a / 32] |= m << (a % 32);
482          }
483       }
484    }
485    /* GM20x+ needs TGSI_SEMANTIC_POSITION to access sample locations */
486    if (info->prop.fp.readsSampleLocations && info->target >= NVISA_GM200_CHIPSET)
487       fp->hdr[5] |= 0x30000000;
488 
489    for (i = 0; i < info->numOutputs; ++i) {
490       if (info->out[i].sn == TGSI_SEMANTIC_COLOR)
491          fp->hdr[18] |= 0xf << (4 * info->out[i].si);
492    }
493 
494    /* There are no "regular" attachments, but the shader still needs to be
495     * executed. It seems like it wants to think that it has some color
496     * outputs in order to actually run.
497     */
498    if (info->prop.fp.numColourResults == 0 && !info->prop.fp.writesDepth)
499       fp->hdr[18] |= 0xf;
500 
501    fp->fp.early_z = info->prop.fp.earlyFragTests;
502    fp->fp.sample_mask_in = info->prop.fp.usesSampleMaskIn;
503    fp->fp.reads_framebuffer = info->prop.fp.readsFramebuffer;
504    fp->fp.post_depth_coverage = info->prop.fp.postDepthCoverage;
505 
506    /* Mark position xy and layer as read */
507    if (fp->fp.reads_framebuffer)
508       fp->hdr[5] |= 0x32000000;
509 
510    return 0;
511 }
512 
513 static struct nvc0_transform_feedback_state *
nvc0_program_create_tfb_state(const struct nv50_ir_prog_info_out * info,const struct pipe_stream_output_info * pso)514 nvc0_program_create_tfb_state(const struct nv50_ir_prog_info_out *info,
515                               const struct pipe_stream_output_info *pso)
516 {
517    struct nvc0_transform_feedback_state *tfb;
518    unsigned b, i, c;
519 
520    tfb = MALLOC_STRUCT(nvc0_transform_feedback_state);
521    if (!tfb)
522       return NULL;
523    for (b = 0; b < 4; ++b) {
524       tfb->stride[b] = pso->stride[b] * 4;
525       tfb->varying_count[b] = 0;
526    }
527    memset(tfb->varying_index, 0xff, sizeof(tfb->varying_index)); /* = skip */
528 
529    for (i = 0; i < pso->num_outputs; ++i) {
530       unsigned s = pso->output[i].start_component;
531       unsigned p = pso->output[i].dst_offset;
532       const unsigned r = pso->output[i].register_index;
533       b = pso->output[i].output_buffer;
534 
535       if (r >= info->numOutputs)
536          continue;
537 
538       for (c = 0; c < pso->output[i].num_components; ++c)
539          tfb->varying_index[b][p++] = info->out[r].slot[s + c];
540 
541       tfb->varying_count[b] = MAX2(tfb->varying_count[b], p);
542       tfb->stream[b] = pso->output[i].stream;
543    }
544    for (b = 0; b < 4; ++b) // zero unused indices (looks nicer)
545       for (c = tfb->varying_count[b]; c & 3; ++c)
546          tfb->varying_index[b][c] = 0;
547 
548    return tfb;
549 }
550 
551 #ifndef NDEBUG
552 static void
nvc0_program_dump(struct nvc0_program * prog)553 nvc0_program_dump(struct nvc0_program *prog)
554 {
555    unsigned pos;
556 
557    if (prog->type != PIPE_SHADER_COMPUTE) {
558       _debug_printf("dumping HDR for type %i\n", prog->type);
559       for (pos = 0; pos < ARRAY_SIZE(prog->hdr); ++pos)
560          _debug_printf("HDR[%02"PRIxPTR"] = 0x%08x\n",
561                       pos * sizeof(prog->hdr[0]), prog->hdr[pos]);
562    }
563    _debug_printf("shader binary code (0x%x bytes):", prog->code_size);
564    for (pos = 0; pos < prog->code_size / 4; ++pos) {
565       if ((pos % 8) == 0)
566          _debug_printf("\n");
567       _debug_printf("%08x ", prog->code[pos]);
568    }
569    _debug_printf("\n");
570 }
571 #endif
572 
573 bool
nvc0_program_translate(struct nvc0_program * prog,uint16_t chipset,struct disk_cache * disk_shader_cache,struct util_debug_callback * debug)574 nvc0_program_translate(struct nvc0_program *prog, uint16_t chipset,
575                        struct disk_cache *disk_shader_cache,
576                        struct util_debug_callback *debug)
577 {
578    struct blob blob;
579    size_t cache_size;
580    struct nv50_ir_prog_info *info;
581    struct nv50_ir_prog_info_out info_out = {};
582 
583    int ret = 0;
584    cache_key key;
585    bool shader_loaded = false;
586 
587    info = CALLOC_STRUCT(nv50_ir_prog_info);
588    if (!info)
589       return false;
590 
591    info->type = prog->type;
592    info->target = chipset;
593 
594    info->bin.nir = nir_shader_clone(NULL, prog->nir);
595 
596 #ifndef NDEBUG
597    info->target = debug_get_num_option("NV50_PROG_CHIPSET", chipset);
598    info->optLevel = debug_get_num_option("NV50_PROG_OPTIMIZE", 4);
599    info->dbgFlags = debug_get_num_option("NV50_PROG_DEBUG", 0);
600    info->omitLineNum = debug_get_num_option("NV50_PROG_DEBUG_OMIT_LINENUM", 0);
601 #else
602    info->optLevel = 4;
603 #endif
604 
605    info->bin.smemSize = prog->cp.smem_size;
606    info->io.genUserClip = prog->vp.num_ucps;
607    info->io.auxCBSlot = 15;
608    info->io.msInfoCBSlot = 15;
609    info->io.ucpBase = NVC0_CB_AUX_UCP_INFO;
610    info->io.drawInfoBase = NVC0_CB_AUX_DRAW_INFO;
611    info->io.msInfoBase = NVC0_CB_AUX_MS_INFO;
612    info->io.bufInfoBase = NVC0_CB_AUX_BUF_INFO(0);
613    info->io.suInfoBase = NVC0_CB_AUX_SU_INFO(0);
614    if (info->target >= NVISA_GK104_CHIPSET) {
615       info->io.texBindBase = NVC0_CB_AUX_TEX_INFO(0);
616       info->io.fbtexBindBase = NVC0_CB_AUX_FB_TEX_INFO;
617       info->io.bindlessBase = NVC0_CB_AUX_BINDLESS_INFO(0);
618    }
619 
620    if (prog->type == PIPE_SHADER_COMPUTE) {
621       if (info->target >= NVISA_GK104_CHIPSET) {
622          info->io.auxCBSlot = 7;
623          info->io.msInfoCBSlot = 7;
624          info->io.uboInfoBase = NVC0_CB_AUX_UBO_INFO(0);
625       }
626       info->prop.cp.gridInfoBase = NVC0_CB_AUX_GRID_INFO(0);
627    } else {
628       info->io.sampleInfoBase = NVC0_CB_AUX_SAMPLE_INFO;
629    }
630 
631    info->assignSlots = nvc0_program_assign_varying_slots;
632 
633    blob_init(&blob);
634 
635    if (disk_shader_cache) {
636       if (nv50_ir_prog_info_serialize(&blob, info)) {
637          void *cached_data = NULL;
638 
639          disk_cache_compute_key(disk_shader_cache, blob.data, blob.size, key);
640          cached_data = disk_cache_get(disk_shader_cache, key, &cache_size);
641 
642          if (cached_data && cache_size >= blob.size) { // blob.size is the size of serialized "info"
643             /* Blob contains only "info". In disk cache, "info_out" comes right after it */
644             size_t offset = blob.size;
645             if (nv50_ir_prog_info_out_deserialize(cached_data, cache_size, offset, &info_out))
646                shader_loaded = true;
647             else
648                debug_printf("WARNING: Couldn't deserialize shaders");
649          }
650          free(cached_data);
651       } else {
652          debug_printf("WARNING: Couldn't serialize input shaders");
653       }
654    }
655    if (!shader_loaded) {
656       cache_size = 0;
657       ret = nv50_ir_generate_code(info, &info_out);
658       if (ret) {
659          NOUVEAU_ERR("shader translation failed: %i\n", ret);
660          goto out;
661       }
662       if (disk_shader_cache) {
663          if (nv50_ir_prog_info_out_serialize(&blob, &info_out)) {
664             disk_cache_put(disk_shader_cache, key, blob.data, blob.size, NULL);
665             cache_size = blob.size;
666          } else {
667             debug_printf("WARNING: Couldn't serialize shaders");
668          }
669       }
670    }
671    blob_finish(&blob);
672 
673    prog->code = info_out.bin.code;
674    prog->code_size = info_out.bin.codeSize;
675    prog->relocs = info_out.bin.relocData;
676    prog->fixups = info_out.bin.fixupData;
677    if (info_out.target >= NVISA_GV100_CHIPSET)
678       prog->num_gprs = MAX2(4, info_out.bin.maxGPR + 3);
679    else
680       prog->num_gprs = MAX2(4, info_out.bin.maxGPR + 1);
681    prog->cp.smem_size = info_out.bin.smemSize;
682    prog->num_barriers = info_out.numBarriers;
683 
684    prog->vp.need_vertex_id = info_out.io.vertexId < PIPE_MAX_SHADER_INPUTS;
685    prog->vp.need_draw_parameters = info_out.prop.vp.usesDrawParameters;
686 
687    if (info_out.io.edgeFlagOut < PIPE_MAX_ATTRIBS)
688       info_out.out[info_out.io.edgeFlagOut].mask = 0; /* for headergen */
689    prog->vp.edgeflag = info_out.io.edgeFlagIn;
690 
691    switch (prog->type) {
692    case PIPE_SHADER_VERTEX:
693       ret = nvc0_vp_gen_header(prog, &info_out);
694       break;
695    case PIPE_SHADER_TESS_CTRL:
696       ret = nvc0_tcp_gen_header(prog, &info_out);
697       break;
698    case PIPE_SHADER_TESS_EVAL:
699       ret = nvc0_tep_gen_header(prog, &info_out);
700       break;
701    case PIPE_SHADER_GEOMETRY:
702       ret = nvc0_gp_gen_header(prog, &info_out);
703       break;
704    case PIPE_SHADER_FRAGMENT:
705       ret = nvc0_fp_gen_header(prog, &info_out);
706       break;
707    case PIPE_SHADER_COMPUTE:
708       break;
709    default:
710       ret = -1;
711       NOUVEAU_ERR("unknown program type: %u\n", prog->type);
712       break;
713    }
714    if (ret)
715       goto out;
716 
717    if (info_out.bin.tlsSpace) {
718       assert(info_out.bin.tlsSpace < (1 << 24));
719       prog->hdr[0] |= 1 << 26;
720       prog->hdr[1] |= align(info_out.bin.tlsSpace, 0x10); /* l[] size */
721       prog->need_tls = true;
722    }
723    /* TODO: factor 2 only needed where joinat/precont is used,
724     *       and we only have to count non-uniform branches
725     */
726    /*
727    if ((info->maxCFDepth * 2) > 16) {
728       prog->hdr[2] |= (((info->maxCFDepth * 2) + 47) / 48) * 0x200;
729       prog->need_tls = true;
730    }
731    */
732    if (info_out.io.globalAccess)
733       prog->hdr[0] |= 1 << 26;
734    if (info_out.io.globalAccess & 0x2)
735       prog->hdr[0] |= 1 << 16;
736    if (info_out.io.fp64)
737       prog->hdr[0] |= 1 << 27;
738 
739    if (prog->stream_output.num_outputs)
740       prog->tfb = nvc0_program_create_tfb_state(&info_out,
741                                                 &prog->stream_output);
742 
743    util_debug_message(debug, SHADER_INFO,
744                       "type: %d, local: %d, shared: %d, gpr: %d, inst: %d, bytes: %d, cached: %zd",
745                       prog->type, info_out.bin.tlsSpace, info_out.bin.smemSize,
746                       prog->num_gprs, info_out.bin.instructions,
747                       info_out.bin.codeSize, cache_size);
748 
749 #ifndef NDEBUG
750    if (debug_get_option("NV50_PROG_CHIPSET", NULL) && info->dbgFlags)
751       nvc0_program_dump(prog);
752 #endif
753 
754 out:
755    ralloc_free((void *)info->bin.nir);
756    FREE(info);
757    return !ret;
758 }
759 
760 static inline int
nvc0_program_alloc_code(struct nvc0_context * nvc0,struct nvc0_program * prog)761 nvc0_program_alloc_code(struct nvc0_context *nvc0, struct nvc0_program *prog)
762 {
763    struct nvc0_screen *screen = nvc0->screen;
764    const bool is_cp = prog->type == PIPE_SHADER_COMPUTE;
765    int ret;
766    uint32_t size = prog->code_size;
767 
768    if (!is_cp) {
769       if (screen->eng3d->oclass < TU102_3D_CLASS)
770          size += GF100_SHADER_HEADER_SIZE;
771       else
772          size += TU102_SHADER_HEADER_SIZE;
773    }
774 
775    /* On Fermi, SP_START_ID must be aligned to 0x40.
776     * On Kepler, the first instruction must be aligned to 0x80 because
777     * latency information is expected only at certain positions.
778     */
779    if (screen->base.class_3d >= NVE4_3D_CLASS)
780       size = size + (is_cp ? 0x40 : 0x70);
781    size = align(size, 0x40);
782 
783    ret = nouveau_heap_alloc(screen->text_heap, size, prog, &prog->mem);
784    if (ret)
785       return ret;
786    prog->code_base = prog->mem->start;
787 
788    if (!is_cp) {
789       if (screen->base.class_3d >= NVE4_3D_CLASS &&
790           screen->base.class_3d < TU102_3D_CLASS) {
791          switch (prog->mem->start & 0xff) {
792          case 0x40: prog->code_base += 0x70; break;
793          case 0x80: prog->code_base += 0x30; break;
794          case 0xc0: prog->code_base += 0x70; break;
795          default:
796             prog->code_base += 0x30;
797             assert((prog->mem->start & 0xff) == 0x00);
798             break;
799          }
800       }
801    } else {
802       if (screen->base.class_3d >= NVE4_3D_CLASS) {
803          if (prog->mem->start & 0x40)
804             prog->code_base += 0x40;
805          assert((prog->code_base & 0x7f) == 0x00);
806       }
807    }
808 
809    return 0;
810 }
811 
812 static inline void
nvc0_program_upload_code(struct nvc0_context * nvc0,struct nvc0_program * prog)813 nvc0_program_upload_code(struct nvc0_context *nvc0, struct nvc0_program *prog)
814 {
815    struct nvc0_screen *screen = nvc0->screen;
816    const bool is_cp = prog->type == PIPE_SHADER_COMPUTE;
817    uint32_t code_pos = prog->code_base;
818    uint32_t size_sph = 0;
819 
820    if (!is_cp) {
821       if (screen->eng3d->oclass < TU102_3D_CLASS)
822          size_sph = GF100_SHADER_HEADER_SIZE;
823       else
824          size_sph = TU102_SHADER_HEADER_SIZE;
825    }
826    code_pos += size_sph;
827 
828    if (prog->relocs)
829       nv50_ir_relocate_code(prog->relocs, prog->code, code_pos,
830                             screen->lib_code->start, 0);
831    if (prog->fixups) {
832       nv50_ir_apply_fixups(prog->fixups, prog->code,
833                            prog->fp.force_persample_interp,
834                            prog->fp.flatshade,
835                            0 /* alphatest */,
836                            prog->fp.msaa);
837       for (int i = 0; i < 2; i++) {
838          unsigned mask = prog->fp.color_interp[i] >> 4;
839          unsigned interp = prog->fp.color_interp[i] & 3;
840          if (!mask)
841             continue;
842          prog->hdr[14] &= ~(0xff << (8 * i));
843          if (prog->fp.flatshade)
844             interp = NVC0_INTERP_FLAT;
845          for (int c = 0; c < 4; c++)
846             if (mask & (1 << c))
847                prog->hdr[14] |= interp << (2 * (4 * i + c));
848       }
849    }
850 
851    if (!is_cp)
852       nvc0->base.push_data(&nvc0->base, screen->text, prog->code_base,
853                            NV_VRAM_DOMAIN(&screen->base), size_sph, prog->hdr);
854 
855    nvc0->base.push_data(&nvc0->base, screen->text, code_pos,
856                         NV_VRAM_DOMAIN(&screen->base), prog->code_size,
857                         prog->code);
858 }
859 
860 bool
nvc0_program_upload(struct nvc0_context * nvc0,struct nvc0_program * prog)861 nvc0_program_upload(struct nvc0_context *nvc0, struct nvc0_program *prog)
862 {
863    struct nvc0_screen *screen = nvc0->screen;
864    const bool is_cp = prog->type == PIPE_SHADER_COMPUTE;
865    int ret;
866    uint32_t size = prog->code_size;
867 
868    if (!is_cp) {
869       if (screen->eng3d->oclass < TU102_3D_CLASS)
870          size += GF100_SHADER_HEADER_SIZE;
871       else
872          size += TU102_SHADER_HEADER_SIZE;
873    }
874 
875    simple_mtx_assert_locked(&nvc0->screen->state_lock);
876    ret = nvc0_program_alloc_code(nvc0, prog);
877    if (ret) {
878       struct nouveau_heap *heap = screen->text_heap;
879       struct nvc0_program *progs[] = { /* Sorted accordingly to SP_START_ID */
880          nvc0->compprog, nvc0->vertprog, nvc0->tctlprog,
881          nvc0->tevlprog, nvc0->gmtyprog, nvc0->fragprog
882       };
883 
884       /* Note that the code library, which is allocated before anything else,
885        * does not have a priv pointer. We can stop once we hit it.
886        */
887       while (heap->next && heap->next->priv) {
888          struct nvc0_program *evict = heap->next->priv;
889          nouveau_heap_free(&evict->mem);
890       }
891       debug_printf("WARNING: out of code space, evicting all shaders.\n");
892 
893       /* Make sure to synchronize before deleting the code segment. */
894       IMMED_NVC0(nvc0->base.pushbuf, NVC0_3D(SERIALIZE), 0);
895 
896       if ((screen->text->size << 1) <= (1 << 23)) {
897          ret = nvc0_screen_resize_text_area(screen, nvc0->base.pushbuf, screen->text->size << 1);
898          if (ret) {
899             NOUVEAU_ERR("Error allocating TEXT area: %d\n", ret);
900             return false;
901          }
902 
903          /* Re-upload the builtin function into the new code segment. */
904          nvc0_program_library_upload(nvc0);
905       }
906 
907       ret = nvc0_program_alloc_code(nvc0, prog);
908       if (ret) {
909          NOUVEAU_ERR("shader too large (0x%x) to fit in code space ?\n", size);
910          return false;
911       }
912 
913       /* All currently bound shaders have to be reuploaded. */
914       for (int i = 0; i < ARRAY_SIZE(progs); i++) {
915          if (!progs[i] || progs[i] == prog)
916             continue;
917 
918          ret = nvc0_program_alloc_code(nvc0, progs[i]);
919          if (ret) {
920             NOUVEAU_ERR("failed to re-upload a shader after code eviction.\n");
921             return false;
922          }
923          nvc0_program_upload_code(nvc0, progs[i]);
924 
925          if (progs[i]->type == PIPE_SHADER_COMPUTE) {
926             /* Caches have to be invalidated but the CP_START_ID will be
927              * updated in the launch_grid functions. */
928             BEGIN_NVC0(nvc0->base.pushbuf, NVC0_CP(FLUSH), 1);
929             PUSH_DATA (nvc0->base.pushbuf, NVC0_COMPUTE_FLUSH_CODE);
930          } else {
931             nvc0_program_sp_start_id(nvc0, i, progs[i]);
932          }
933       }
934    }
935 
936    nvc0_program_upload_code(nvc0, prog);
937 
938 #ifndef NDEBUG
939    if (debug_get_num_option("NV50_PROG_DEBUG", 0))
940       nvc0_program_dump(prog);
941 #endif
942 
943    BEGIN_NVC0(nvc0->base.pushbuf, NVC0_3D(MEM_BARRIER), 1);
944    PUSH_DATA (nvc0->base.pushbuf, 0x1011);
945 
946    return true;
947 }
948 
949 /* Upload code for builtin functions like integer division emulation. */
950 void
nvc0_program_library_upload(struct nvc0_context * nvc0)951 nvc0_program_library_upload(struct nvc0_context *nvc0)
952 {
953    struct nvc0_screen *screen = nvc0->screen;
954    int ret;
955    uint32_t size;
956    const uint32_t *code;
957 
958    if (screen->lib_code)
959       return;
960 
961    nv50_ir_get_target_library(screen->base.device->chipset, &code, &size);
962    if (!size)
963       return;
964 
965    ret = nouveau_heap_alloc(screen->text_heap, align(size, 0x100), NULL,
966                             &screen->lib_code);
967    if (ret)
968       return;
969 
970    nvc0->base.push_data(&nvc0->base,
971                         screen->text, screen->lib_code->start, NV_VRAM_DOMAIN(&screen->base),
972                         size, code);
973    /* no need for a memory barrier, will be emitted with first program */
974 }
975 
976 void
nvc0_program_destroy(struct nvc0_context * nvc0,struct nvc0_program * prog)977 nvc0_program_destroy(struct nvc0_context *nvc0, struct nvc0_program *prog)
978 {
979    struct nir_shader *nir = prog->nir;
980    const uint8_t type = prog->type;
981 
982    if (prog->mem) {
983       if (nvc0)
984          simple_mtx_assert_locked(&nvc0->screen->state_lock);
985       nouveau_heap_free(&prog->mem);
986    }
987    FREE(prog->code); /* may be 0 for hardcoded shaders */
988    FREE(prog->relocs);
989    FREE(prog->fixups);
990    if (prog->tfb) {
991       if (nvc0->state.tfb == prog->tfb)
992          nvc0->state.tfb = NULL;
993       FREE(prog->tfb);
994    }
995 
996    memset(prog, 0, sizeof(*prog));
997 
998    prog->nir = nir;
999    prog->type = type;
1000 }
1001 
1002 void
nvc0_program_init_tcp_empty(struct nvc0_context * nvc0)1003 nvc0_program_init_tcp_empty(struct nvc0_context *nvc0)
1004 {
1005    const nir_shader_compiler_options *options =
1006       nv50_ir_nir_shader_compiler_options(nvc0->screen->base.device->chipset,
1007                                           PIPE_SHADER_TESS_CTRL);
1008 
1009    struct nir_builder b =
1010       nir_builder_init_simple_shader(MESA_SHADER_TESS_CTRL, options,
1011                                      "tcp_empty");
1012    b.shader->info.tess.tcs_vertices_out = 1;
1013 
1014    nir_validate_shader(b.shader, "in nvc0_program_init_tcp_empty");
1015 
1016    nvc0->tcp_empty = pipe_shader_from_nir(&nvc0->base.pipe, b.shader);
1017 }
1018