Debian Codegraph @

src/gallium/drivers/nouveau/nvc0/nvc0_program.c

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