Debian Codegraph @

src/gallium/drivers/nouveau/nv50/nv50_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 "pipe/p_defines.h"
24 #include "pipe/p_shader_tokens.h"
25
26 #include "compiler/nir/nir.h"
27
28 #include "nv50/nv50_context.h"
29 #include "nv50/nv50_program.h"
30
31 #include "nv50_ir_driver.h"
32
33 static inline unsigned
34 bitcount4(const uint32_t val)
35 {
36 static const uint8_t cnt[16]
37 = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
38 return cnt[val & 0xf];
39 }
40
41 static int
42 nv50_vertprog_assign_slots(struct nv50_ir_prog_info_out *info)
43 {
44 struct nv50_program *prog = (struct nv50_program *)info->driverPriv;
45 unsigned i, n, c;
46
47 n = 0;
48 for (i = 0; i < info->numInputs; ++i) {
49 prog->in[i].id = i;
50 prog->in[i].sn = info->in[i].sn;
51 prog->in[i].si = info->in[i].si;
52 prog->in[i].hw = n;
53 prog->in[i].mask = info->in[i].mask;
54
55 prog->vp.attrs[(4 * i) / 32] |= info->in[i].mask << ((4 * i) % 32);
56
57 for (c = 0; c < 4; ++c)
58 if (info->in[i].mask & (1 << c))
59 info->in[i].slot[c] = n++;
60
61 if (info->in[i].sn == TGSI_SEMANTIC_PRIMID)
62 prog->vp.attrs[2] |= NV50_3D_VP_GP_BUILTIN_ATTR_EN_PRIMITIVE_ID;
63 }
64 prog->in_nr = info->numInputs;
65
66 for (i = 0; i < info->numSysVals; ++i) {
67 switch (info->sv[i].sn) {
68 case SYSTEM_VALUE_INSTANCE_ID:
69 prog->vp.attrs[2] |= NV50_3D_VP_GP_BUILTIN_ATTR_EN_INSTANCE_ID;
70 continue;
71 case SYSTEM_VALUE_VERTEX_ID:
72 prog->vp.attrs[2] |= NV50_3D_VP_GP_BUILTIN_ATTR_EN_VERTEX_ID;
73 prog->vp.attrs[2] |= NV50_3D_VP_GP_BUILTIN_ATTR_EN_VERTEX_ID_DRAW_ARRAYS_ADD_START;
74 continue;
75 case SYSTEM_VALUE_PRIMITIVE_ID:
76 prog->vp.attrs[2] |= NV50_3D_VP_GP_BUILTIN_ATTR_EN_PRIMITIVE_ID;
77 break;
78 default:
79 break;
80 }
81 }
82
83 /*
84 * Corner case: VP has no inputs, but we will still need to submit data to
85 * draw it. HW will shout at us and won't draw anything if we don't enable
86 * any input, so let's just pretend it's the first one.
87 */
88 if (prog->vp.attrs[0] == 0 &&
89 prog->vp.attrs[1] == 0 &&
90 prog->vp.attrs[2] == 0)
91 prog->vp.attrs[0] |= 0xf;
92
93 /* VertexID before InstanceID */
94 if (info->io.vertexId < info->numSysVals)
95 info->sv[info->io.vertexId].slot[0] = n++;
96 if (info->io.instanceId < info->numSysVals)
97 info->sv[info->io.instanceId].slot[0] = n++;
98
99 n = 0;
100 for (i = 0; i < info->numOutputs; ++i) {
101 switch (info->out[i].sn) {
102 case TGSI_SEMANTIC_PSIZE:
103 prog->vp.psiz = i;
104 break;
105 case TGSI_SEMANTIC_CLIPDIST:
106 prog->vp.clpd[info->out[i].si] = n;
107 break;
108 case TGSI_SEMANTIC_EDGEFLAG:
109 prog->vp.edgeflag = i;
110 break;
111 case TGSI_SEMANTIC_BCOLOR:
112 prog->vp.bfc[info->out[i].si] = i;
113 break;
114 case TGSI_SEMANTIC_LAYER:
115 prog->gp.has_layer = true;
116 prog->gp.layerid = n;
117 break;
118 case TGSI_SEMANTIC_VIEWPORT_INDEX:
119 prog->gp.has_viewport = true;
120 prog->gp.viewportid = n;
121 break;
122 default:
123 break;
124 }
125 prog->out[i].id = i;
126 prog->out[i].sn = info->out[i].sn;
127 prog->out[i].si = info->out[i].si;
128 prog->out[i].hw = n;
129 prog->out[i].mask = info->out[i].mask;
130
131 for (c = 0; c < 4; ++c)
132 if (info->out[i].mask & (1 << c))
133 info->out[i].slot[c] = n++;
134 }
135 prog->out_nr = info->numOutputs;
136 prog->max_out = n;
137 if (!prog->max_out)
138 prog->max_out = 1;
139
140 if (prog->vp.psiz < info->numOutputs)
141 prog->vp.psiz = prog->out[prog->vp.psiz].hw;
142
143 return 0;
144 }
145
146 static int
147 nv50_fragprog_assign_slots(struct nv50_ir_prog_info_out *info)
148 {
149 struct nv50_program *prog = (struct nv50_program *)info->driverPriv;
150 unsigned i, n, m, c;
151 unsigned nvary;
152 unsigned nflat;
153 unsigned nintp = 0;
154
155 /* count recorded non-flat inputs */
156 for (m = 0, i = 0; i < info->numInputs; ++i) {
157 switch (info->in[i].sn) {
158 case TGSI_SEMANTIC_POSITION:
159 continue;
160 default:
161 m += info->in[i].flat ? 0 : 1;
162 break;
163 }
164 }
165 /* careful: id may be != i in info->in[prog->in[i].id] */
166
167 /* Fill prog->in[] so that non-flat inputs are first and
168 * kick out special inputs that don't use the RESULT_MAP.
169 */
170 for (n = 0, i = 0; i < info->numInputs; ++i) {
171 if (info->in[i].sn == TGSI_SEMANTIC_POSITION) {
172 prog->fp.interp |= info->in[i].mask << 24;
173 for (c = 0; c < 4; ++c)
174 if (info->in[i].mask & (1 << c))
175 info->in[i].slot[c] = nintp++;
176 } else {
177 unsigned j = info->in[i].flat ? m++ : n++;
178
179 if (info->in[i].sn == TGSI_SEMANTIC_COLOR)
180 prog->vp.bfc[info->in[i].si] = j;
181 else if (info->in[i].sn == TGSI_SEMANTIC_PRIMID)
182 prog->vp.attrs[2] |= NV50_3D_VP_GP_BUILTIN_ATTR_EN_PRIMITIVE_ID;
183
184 prog->in[j].id = i;
185 prog->in[j].mask = info->in[i].mask;
186 prog->in[j].sn = info->in[i].sn;
187 prog->in[j].si = info->in[i].si;
188 prog->in[j].linear = info->in[i].linear;
189
190 prog->in_nr++;
191 }
192 }
193 if (!(prog->fp.interp & (8 << 24))) {
194 ++nintp;
195 prog->fp.interp |= 8 << 24;
196 }
197
198 for (i = 0; i < prog->in_nr; ++i) {
199 int j = prog->in[i].id;
200
201 prog->in[i].hw = nintp;
202 for (c = 0; c < 4; ++c)
203 if (prog->in[i].mask & (1 << c))
204 info->in[j].slot[c] = nintp++;
205 }
206 /* (n == m) if m never increased, i.e. no flat inputs */
207 nflat = (n < m) ? (nintp - prog->in[n].hw) : 0;
208 nintp -= bitcount4(prog->fp.interp >> 24); /* subtract position inputs */
209 nvary = nintp - nflat;
210
211 prog->fp.interp |= nvary << NV50_3D_FP_INTERPOLANT_CTRL_COUNT_NONFLAT__SHIFT;
212 prog->fp.interp |= nintp << NV50_3D_FP_INTERPOLANT_CTRL_COUNT__SHIFT;
213
214 /* put front/back colors right after HPOS */
215 prog->fp.colors = 4 << NV50_3D_SEMANTIC_COLOR_FFC0_ID__SHIFT;
216 for (i = 0; i < 2; ++i)
217 if (prog->vp.bfc[i] < 0xff)
218 prog->fp.colors += bitcount4(prog->in[prog->vp.bfc[i]].mask) << 16;
219
220 /* FP outputs */
221
222 if (info->prop.fp.numColourResults > 1)
223 prog->fp.flags[0] |= NV50_3D_FP_CONTROL_MULTIPLE_RESULTS;
224
225 for (i = 0; i < info->numOutputs; ++i) {
226 prog->out[i].id = i;
227 prog->out[i].sn = info->out[i].sn;
228 prog->out[i].si = info->out[i].si;
229 prog->out[i].mask = info->out[i].mask;
230
231 if (i == info->io.fragDepth || i == info->io.sampleMask)
232 continue;
233 prog->out[i].hw = info->out[i].si * 4;
234
235 for (c = 0; c < 4; ++c)
236 info->out[i].slot[c] = prog->out[i].hw + c;
237
238 prog->max_out = MAX2(prog->max_out, prog->out[i].hw + 4);
239 }
240
241 if (info->io.sampleMask < PIPE_MAX_SHADER_OUTPUTS) {
242 info->out[info->io.sampleMask].slot[0] = prog->max_out++;
243 prog->fp.has_samplemask = 1;
244 }
245
246 if (info->io.fragDepth < PIPE_MAX_SHADER_OUTPUTS)
247 info->out[info->io.fragDepth].slot[2] = prog->max_out++;
248
249 if (!prog->max_out)
250 prog->max_out = 4;
251
252 return 0;
253 }
254
255 static int
256 nv50_program_assign_varying_slots(struct nv50_ir_prog_info_out *info)
257 {
258 switch (info->type) {
259 case MESA_SHADER_VERTEX:
260 return nv50_vertprog_assign_slots(info);
261 case MESA_SHADER_GEOMETRY:
262 return nv50_vertprog_assign_slots(info);
263 case MESA_SHADER_FRAGMENT:
264 return nv50_fragprog_assign_slots(info);
265 case MESA_SHADER_COMPUTE:
266 return 0;
267 default:
268 return -1;
269 }
270 }
271
272 static struct nv50_stream_output_state *
273 nv50_program_create_strmout_state(const struct nv50_ir_prog_info_out *info,
274 const struct pipe_stream_output_info *pso)
275 {
276 struct nv50_stream_output_state *so;
277 unsigned b, i, c;
278 unsigned base[4];
279
280 so = MALLOC_STRUCT(nv50_stream_output_state);
281 if (!so)
282 return NULL;
283 memset(so->map, 0xff, sizeof(so->map));
284
285 for (b = 0; b < 4; ++b)
286 so->num_attribs[b] = 0;
287 for (i = 0; i < pso->num_outputs; ++i) {
288 unsigned end = pso->output[i].dst_offset + pso->output[i].num_components;
289 b = pso->output[i].output_buffer;
290 assert(b < 4);
291 so->num_attribs[b] = MAX2(so->num_attribs[b], end);
292 }
293
294 so->ctrl = NV50_3D_STRMOUT_BUFFERS_CTRL_INTERLEAVED;
295
296 so->stride[0] = pso->stride[0] * 4;
297 base[0] = 0;
298 for (b = 1; b < 4; ++b) {
299 assert(!so->num_attribs[b] || so->num_attribs[b] == pso->stride[b]);
300 so->stride[b] = so->num_attribs[b] * 4;
301 if (so->num_attribs[b])
302 so->ctrl = (b + 1) << NV50_3D_STRMOUT_BUFFERS_CTRL_SEPARATE__SHIFT;
303 base[b] = align(base[b - 1] + so->num_attribs[b - 1], 4);
304 }
305 if (so->ctrl & NV50_3D_STRMOUT_BUFFERS_CTRL_INTERLEAVED) {
306 assert(so->stride[0] < NV50_3D_STRMOUT_BUFFERS_CTRL_STRIDE__MAX);
307 so->ctrl |= so->stride[0] << NV50_3D_STRMOUT_BUFFERS_CTRL_STRIDE__SHIFT;
308 }
309
310 so->map_size = base[3] + so->num_attribs[3];
311
312 for (i = 0; i < pso->num_outputs; ++i) {
313 const unsigned s = pso->output[i].start_component;
314 const unsigned p = pso->output[i].dst_offset;
315 const unsigned r = pso->output[i].register_index;
316 b = pso->output[i].output_buffer;
317
318 if (r >= info->numOutputs)
319 continue;
320
321 for (c = 0; c < pso->output[i].num_components; ++c)
322 so->map[base[b] + p + c] = info->out[r].slot[s + c];
323 }
324
325 return so;
326 }
327
328 bool
329 nv50_program_translate(struct nv50_program *prog, uint16_t chipset,
330 struct util_debug_callback *debug)
331 {
332 struct nv50_ir_prog_info *info;
333 struct nv50_ir_prog_info_out info_out = {};
334 int i, ret;
335 const uint8_t map_undef = (prog->type == MESA_SHADER_VERTEX) ? 0x40 : 0x80;
336
337 info = CALLOC_STRUCT(nv50_ir_prog_info);
338 if (!info)
339 return false;
340
341 info->type = prog->type;
342 info->target = chipset;
343
344 info->bin.nir = nir_shader_clone(NULL, prog->nir);
345
346 info->bin.smemSize = prog->cp.smem_size;
347 info->io.auxCBSlot = 15;
348 info->io.ucpBase = NV50_CB_AUX_UCP_OFFSET;
349 info->io.genUserClip = prog->vp.clpd_nr;
350 if (prog->fp.alphatest)
351 info->io.alphaRefBase = NV50_CB_AUX_ALPHATEST_OFFSET;
352
353 info->io.suInfoBase = NV50_CB_AUX_TEX_MS_OFFSET;
354 info->io.bufInfoBase = NV50_CB_AUX_BUF_INFO(0);
355 info->io.sampleInfoBase = NV50_CB_AUX_SAMPLE_OFFSET;
356 info->io.msInfoCBSlot = 15;
357 info->io.msInfoBase = NV50_CB_AUX_MS_OFFSET;
358
359 info->io.membarOffset = NV50_CB_AUX_MEMBAR_OFFSET;
360 info->io.gmemMembar = 15;
361
362 info->assignSlots = nv50_program_assign_varying_slots;
363
364 prog->vp.bfc[0] = 0xff;
365 prog->vp.bfc[1] = 0xff;
366 prog->vp.edgeflag = 0xff;
367 prog->vp.clpd[0] = map_undef;
368 prog->vp.clpd[1] = map_undef;
369 prog->vp.psiz = map_undef;
370 prog->gp.has_layer = 0;
371 prog->gp.has_viewport = 0;
372
373 if (prog->type == MESA_SHADER_COMPUTE)
374 info->prop.cp.inputOffset = 0x14;
375
376 info_out.driverPriv = prog;
377
378 #ifndef NDEBUG
379 info->optLevel = debug_get_num_option("NV50_PROG_OPTIMIZE", 4);
380 info->dbgFlags = debug_get_num_option("NV50_PROG_DEBUG", 0);
381 info->omitLineNum = debug_get_num_option("NV50_PROG_DEBUG_OMIT_LINENUM", 0);
382 #else
383 info->optLevel = 4;
384 #endif
385
386 ret = nv50_ir_generate_code(info, &info_out);
387 if (ret) {
388 NOUVEAU_ERR("shader translation failed: %i\n", ret);
389 goto out;
390 }
391
392 prog->code = info_out.bin.code;
393 prog->code_size = info_out.bin.codeSize;
394 prog->relocs = info_out.bin.relocData;
395 prog->fixups = info_out.bin.fixupData;
396 prog->max_gpr = MAX2(4, (info_out.bin.maxGPR >> 1) + 1);
397 prog->tls_space = info_out.bin.tlsSpace;
398 prog->cp.smem_size = info_out.bin.smemSize;
399 prog->mul_zero_wins = info->io.mul_zero_wins;
400 prog->vp.need_vertex_id = info_out.io.vertexId < PIPE_MAX_SHADER_INPUTS;
401
402 prog->vp.clip_enable = (1 << info_out.io.clipDistances) - 1;
403 prog->vp.cull_enable =
404 ((1 << info_out.io.cullDistances) - 1) << info_out.io.clipDistances;
405 prog->vp.clip_mode = 0;
406 for (i = 0; i < info_out.io.cullDistances; ++i)
407 prog->vp.clip_mode |= 1 << ((info_out.io.clipDistances + i) * 4);
408
409 if (prog->type == MESA_SHADER_FRAGMENT) {
410 if (info_out.prop.fp.writesDepth) {
411 prog->fp.flags[0] |= NV50_3D_FP_CONTROL_EXPORTS_Z;
412 prog->fp.flags[1] = 0x11;
413 }
414 if (info_out.prop.fp.usesDiscard)
415 prog->fp.flags[0] |= NV50_3D_FP_CONTROL_USES_KIL;
416 } else
417 if (prog->type == MESA_SHADER_GEOMETRY) {
418 switch (info_out.prop.gp.outputPrim) {
419 case MESA_PRIM_LINE_STRIP:
420 prog->gp.prim_type = NV50_3D_GP_OUTPUT_PRIMITIVE_TYPE_LINE_STRIP;
421 break;
422 case MESA_PRIM_TRIANGLE_STRIP:
423 prog->gp.prim_type = NV50_3D_GP_OUTPUT_PRIMITIVE_TYPE_TRIANGLE_STRIP;
424 break;
425 case MESA_PRIM_POINTS:
426 default:
427 assert(info_out.prop.gp.outputPrim == MESA_PRIM_POINTS);
428 prog->gp.prim_type = NV50_3D_GP_OUTPUT_PRIMITIVE_TYPE_POINTS;
429 break;
430 }
431 prog->gp.vert_count = CLAMP(info_out.prop.gp.maxVertices, 1, 1024);
432 } else
433 if (prog->type == MESA_SHADER_COMPUTE) {
434 for (i = 0; i < NV50_MAX_GLOBALS; i++) {
435 prog->cp.gmem[i] = (struct nv50_gmem_state){
436 .valid = info_out.prop.cp.gmem[i].valid,
437 .image = info_out.prop.cp.gmem[i].image,
438 .slot = info_out.prop.cp.gmem[i].slot
439 };
440 }
441 }
442
443 if (prog->stream_output.num_outputs)
444 prog->so = nv50_program_create_strmout_state(&info_out,
445 &prog->stream_output);
446
447 util_debug_message(debug, SHADER_INFO,
448 "type: %d, local: %d, shared: %d, gpr: %d, inst: %d, loops: %d, bytes: %d",
449 prog->type, info_out.bin.tlsSpace, info_out.bin.smemSize,
450 prog->max_gpr, info_out.bin.instructions, info_out.loops,
451 info_out.bin.codeSize);
452
453 out:
454 ralloc_free(info->bin.nir);
455 FREE(info);
456 return !ret;
457 }
458
459 bool
460 nv50_program_upload_code(struct nv50_context *nv50, struct nv50_program *prog)
461 {
462 struct nouveau_heap *heap;
463 int ret;
464 uint32_t size = align(prog->code_size, 0x40);
465 uint8_t prog_type;
466
467 switch (prog->type) {
468 case MESA_SHADER_VERTEX: heap = nv50->screen->vp_code_heap; break;
469 case MESA_SHADER_GEOMETRY: heap = nv50->screen->gp_code_heap; break;
470 case MESA_SHADER_FRAGMENT: heap = nv50->screen->fp_code_heap; break;
471 case MESA_SHADER_COMPUTE: heap = nv50->screen->fp_code_heap; break;
472 default:
473 assert(!"invalid program type");
474 return false;
475 }
476
477 simple_mtx_assert_locked(&nv50->screen->state_lock);
478 ret = nouveau_heap_alloc(heap, size, prog, &prog->mem);
479 if (ret) {
480 /* Out of space: evict everything to compactify the code segment, hoping
481 * the working set is much smaller and drifts slowly. Improve me !
482 */
483 while (heap->next) {
484 struct nv50_program *evict = heap->next->priv;
485 if (evict)
486 nouveau_heap_free(&evict->mem);
487 }
488 debug_printf("WARNING: out of code space, evicting all shaders.\n");
489 ret = nouveau_heap_alloc(heap, size, prog, &prog->mem);
490 if (ret) {
491 NOUVEAU_ERR("shader too large (0x%x) to fit in code space ?\n", size);
492 return false;
493 }
494 }
495
496 if (prog->type == MESA_SHADER_COMPUTE) {
497 /* CP code must be uploaded in FP code segment. */
498 prog_type = NV50_SHADER_STAGE_FRAGMENT;
499 } else {
500 prog->code_base = prog->mem->start;
501 prog_type = nv50_context_shader_stage(prog->type);
502 }
503
504 ret = nv50_tls_realloc(nv50->screen, prog->tls_space);
505 if (ret < 0) {
506 nouveau_heap_free(&prog->mem);
507 return false;
508 }
509 if (ret > 0)
510 nv50->state.new_tls_space = true;
511
512 if (prog->relocs)
513 nv50_ir_relocate_code(prog->relocs, prog->code, prog->code_base, 0, 0);
514 if (prog->fixups)
515 nv50_ir_apply_fixups(prog->fixups, prog->code,
516 prog->fp.force_persample_interp,
517 false /* flatshade */,
518 prog->fp.alphatest - 1,
519 false /* msaa */);
520
521 nv50_sifc_linear_u8(&nv50->base, nv50->screen->code,
522 (prog_type << NV50_CODE_BO_SIZE_LOG2) + prog->code_base,
523 NOUVEAU_BO_VRAM, prog->code_size, prog->code);
524
525 BEGIN_NV04(nv50->base.pushbuf, NV50_3D(CODE_CB_FLUSH), 1);
526 PUSH_DATA (nv50->base.pushbuf, 0);
527
528 return true;
529 }
530
531 void
532 nv50_program_destroy(struct nv50_context *nv50, struct nv50_program *p)
533 {
534 struct nir_shader *nir = p->nir;
535 const uint8_t type = p->type;
536
537 if (p->mem) {
538 if (nv50)
539 simple_mtx_assert_locked(&nv50->screen->state_lock);
540 nouveau_heap_free(&p->mem);
541 }
542
543 FREE(p->code);
544
545 FREE(p->relocs);
546 FREE(p->fixups);
547 FREE(p->so);
548
549 memset(p, 0, sizeof(*p));
550
551 p->nir = nir;
552 p->type = type;
553 }