Debian Codegraph @

src/gallium/drivers/nouveau/nv50/nv50_shader_state.c

raw
1 /*
2 * Copyright 2008 Ben Skeggs
3 * Copyright 2010 Christoph Bumiller
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "pipe/p_context.h"
25 #include "pipe/p_defines.h"
26 #include "pipe/p_shader_tokens.h"
27 #include "pipe/p_state.h"
28 #include "util/u_inlines.h"
29
30 #include "nv50/nv50_context.h"
31 #include "nv50/nv50_query_hw.h"
32
33 #include "nv50/nv50_compute.xml.h"
34
35 void
36 nv50_constbufs_validate(struct nv50_context *nv50)
37 {
38 struct nouveau_pushbuf *push = nv50->base.pushbuf;
39 unsigned s;
40
41 for (s = 0; s < NV50_MAX_3D_SHADER_STAGES; ++s) {
42 unsigned p;
43
44 if (s == NV50_SHADER_STAGE_FRAGMENT)
45 p = NV50_3D_SET_PROGRAM_CB_PROGRAM_FRAGMENT;
46 else
47 if (s == NV50_SHADER_STAGE_GEOMETRY)
48 p = NV50_3D_SET_PROGRAM_CB_PROGRAM_GEOMETRY;
49 else
50 p = NV50_3D_SET_PROGRAM_CB_PROGRAM_VERTEX;
51
52 while (nv50->constbuf_dirty[s]) {
53 const unsigned i = (unsigned)ffs(nv50->constbuf_dirty[s]) - 1;
54
55 assert(i < NV50_MAX_PIPE_CONSTBUFS);
56 nv50->constbuf_dirty[s] &= ~(1 << i);
57
58 if (nv50->constbuf[s][i].user) {
59 const unsigned b = NV50_CB_PVP + s;
60 unsigned start = 0;
61 unsigned words = nv50->constbuf[s][0].size / 4;
62 if (i) {
63 NOUVEAU_ERR("user constbufs only supported in slot 0\n");
64 continue;
65 }
66 if (!nv50->state.uniform_buffer_bound[s]) {
67 nv50->state.uniform_buffer_bound[s] = true;
68 BEGIN_NV04(push, NV50_3D(SET_PROGRAM_CB), 1);
69 PUSH_DATA (push, (b << 12) | (i << 8) | p | 1);
70 }
71 while (words) {
72 unsigned nr = MIN2(words, NV04_PFIFO_MAX_PACKET_LEN);
73
74 PUSH_SPACE(push, nr + 3);
75 BEGIN_NV04(push, NV50_3D(CB_ADDR), 1);
76 PUSH_DATA (push, (start << 8) | b);
77 BEGIN_NI04(push, NV50_3D(CB_DATA(0)), nr);
78 PUSH_DATAp(push, &nv50->constbuf[s][0].u.data[start * 4], nr);
79
80 start += nr;
81 words -= nr;
82 }
83 } else {
84 struct nv04_resource *res =
85 nv04_resource(nv50->constbuf[s][i].u.buf);
86 if (res) {
87 /* TODO: allocate persistent bindings */
88 const unsigned b = s * 16 + i;
89
90 assert(nouveau_resource_mapped_by_gpu(&res->base));
91
92 BEGIN_NV04(push, NV50_3D(CB_DEF_ADDRESS_HIGH), 3);
93 PUSH_DATAh(push, res->address + nv50->constbuf[s][i].offset);
94 PUSH_DATA (push, res->address + nv50->constbuf[s][i].offset);
95 PUSH_DATA (push, (b << 16) |
96 (nv50->constbuf[s][i].size & 0xffff));
97 BEGIN_NV04(push, NV50_3D(SET_PROGRAM_CB), 1);
98 PUSH_DATA (push, (b << 12) | (i << 8) | p | 1);
99
100 BCTX_REFN(nv50->bufctx_3d, 3D_CB(s, i), res, RD);
101
102 nv50->cb_dirty = 1; /* Force cache flush for UBO. */
103 res->cb_bindings[s] |= 1 << i;
104 } else {
105 BEGIN_NV04(push, NV50_3D(SET_PROGRAM_CB), 1);
106 PUSH_DATA (push, (i << 8) | p | 0);
107 }
108 if (i == 0)
109 nv50->state.uniform_buffer_bound[s] = false;
110 }
111 }
112 }
113
114 /* Invalidate all COMPUTE constbufs because they are aliased with 3D. */
115 nv50->dirty_cp |= NV50_NEW_CP_CONSTBUF;
116 nv50->constbuf_dirty[NV50_SHADER_STAGE_COMPUTE] |= nv50->constbuf_valid[NV50_SHADER_STAGE_COMPUTE];
117 nv50->state.uniform_buffer_bound[NV50_SHADER_STAGE_COMPUTE] = false;
118 }
119
120 static bool
121 nv50_program_validate(struct nv50_context *nv50, struct nv50_program *prog)
122 {
123 if (!prog->translated) {
124 prog->translated = nv50_program_translate(
125 prog, nv50->screen->base.device->chipset, &nv50->base.debug);
126 if (!prog->translated)
127 return false;
128 } else
129 if (prog->mem)
130 return true;
131
132 simple_mtx_assert_locked(&nv50->screen->state_lock);
133 return nv50_program_upload_code(nv50, prog);
134 }
135
136 static inline void
137 nv50_program_update_context_state(struct nv50_context *nv50,
138 struct nv50_program *prog, int stage)
139 {
140 const unsigned flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR;
141
142 if (prog && prog->tls_space) {
143 if (nv50->state.new_tls_space)
144 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_3D_TLS);
145 if (!nv50->state.tls_required || nv50->state.new_tls_space)
146 BCTX_REFN_bo(nv50->bufctx_3d, 3D_TLS, flags, nv50->screen->tls_bo);
147 nv50->state.new_tls_space = false;
148 nv50->state.tls_required |= 1 << stage;
149 } else {
150 if (nv50->state.tls_required == (1 << stage))
151 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_3D_TLS);
152 nv50->state.tls_required &= ~(1 << stage);
153 }
154 }
155
156 void
157 nv50_vertprog_validate(struct nv50_context *nv50)
158 {
159 struct nouveau_pushbuf *push = nv50->base.pushbuf;
160 struct nv50_program *vp = nv50->vertprog;
161
162 if (!nv50_program_validate(nv50, vp))
163 return;
164 nv50_program_update_context_state(nv50, vp, 0);
165
166 BEGIN_NV04(push, NV50_3D(VP_ATTR_EN(0)), 2);
167 PUSH_DATA (push, vp->vp.attrs[0]);
168 PUSH_DATA (push, vp->vp.attrs[1]);
169 BEGIN_NV04(push, NV50_3D(VP_REG_ALLOC_RESULT), 1);
170 PUSH_DATA (push, vp->max_out);
171 BEGIN_NV04(push, NV50_3D(VP_REG_ALLOC_TEMP), 1);
172 PUSH_DATA (push, vp->max_gpr);
173 BEGIN_NV04(push, NV50_3D(VP_START_ID), 1);
174 PUSH_DATA (push, vp->code_base);
175 }
176
177 void
178 nv50_fragprog_validate(struct nv50_context *nv50)
179 {
180 struct nouveau_pushbuf *push = nv50->base.pushbuf;
181 struct nv50_program *fp = nv50->fragprog;
182 struct pipe_rasterizer_state *rast = &nv50->rast->pipe;
183
184 if (!fp || !rast)
185 return;
186
187 if (nv50->zsa && nv50->zsa->pipe.alpha_enabled) {
188 struct pipe_framebuffer_state *fb = &nv50->framebuffer;
189 bool blendable = fb->nr_cbufs == 0 || !fb->cbufs[0].texture ||
190 nv50->screen->base.base.is_format_supported(
191 &nv50->screen->base.base,
192 fb->cbufs[0].format,
193 fb->cbufs[0].texture->target,
194 fb->cbufs[0].texture->nr_samples,
195 fb->cbufs[0].texture->nr_storage_samples,
196 PIPE_BIND_BLENDABLE);
197 /* If we already have alphatest code, we have to keep updating
198 * it. However we only have to have different code if the current RT0 is
199 * non-blendable. Otherwise we just set it to always pass and use the
200 * hardware alpha test.
201 */
202 if (fp->fp.alphatest || !blendable) {
203 uint8_t alphatest = PIPE_FUNC_ALWAYS + 1;
204 if (!blendable)
205 alphatest = nv50->zsa->pipe.alpha_func + 1;
206 if (!fp->fp.alphatest)
207 nv50_program_destroy(nv50, fp);
208 else if (fp->mem && fp->fp.alphatest != alphatest)
209 nouveau_heap_free(&fp->mem);
210
211 fp->fp.alphatest = alphatest;
212 }
213 } else if (fp->fp.alphatest && fp->fp.alphatest != PIPE_FUNC_ALWAYS + 1) {
214 /* Alpha test is disabled but we have a shader where it's filled
215 * in. Make sure to reset the function to 'always', otherwise it'll end
216 * up discarding fragments incorrectly.
217 */
218 if (fp->mem)
219 nouveau_heap_free(&fp->mem);
220
221 fp->fp.alphatest = PIPE_FUNC_ALWAYS + 1;
222 }
223
224 if (fp->fp.force_persample_interp != rast->force_persample_interp) {
225 /* Force the program to be reuploaded, which will trigger interp fixups
226 * to get applied
227 */
228 if (fp->mem)
229 nouveau_heap_free(&fp->mem);
230
231 fp->fp.force_persample_interp = rast->force_persample_interp;
232 }
233
234 if (fp->mem && !(nv50->dirty_3d & (NV50_NEW_3D_FRAGPROG | NV50_NEW_3D_MIN_SAMPLES)))
235 return;
236
237 if (!nv50_program_validate(nv50, fp))
238 return;
239 nv50_program_update_context_state(nv50, fp, 1);
240
241 BEGIN_NV04(push, NV50_3D(FP_REG_ALLOC_TEMP), 1);
242 PUSH_DATA (push, fp->max_gpr);
243 BEGIN_NV04(push, NV50_3D(FP_RESULT_COUNT), 1);
244 PUSH_DATA (push, fp->max_out);
245 BEGIN_NV04(push, NV50_3D(FP_CONTROL), 1);
246 PUSH_DATA (push, fp->fp.flags[0]);
247 BEGIN_NV04(push, NV50_3D(FP_CTRL_UNK196C), 1);
248 PUSH_DATA (push, fp->fp.flags[1]);
249 BEGIN_NV04(push, NV50_3D(FP_START_ID), 1);
250 PUSH_DATA (push, fp->code_base);
251
252 if (nv50->screen->tesla->oclass >= NVA3_3D_CLASS) {
253 BEGIN_NV04(push, SUBC_3D(NVA3_3D_FP_MULTISAMPLE), 1);
254 if (nv50->min_samples > 1 || fp->fp.has_samplemask)
255 PUSH_DATA(push,
256 NVA3_3D_FP_MULTISAMPLE_FORCE_PER_SAMPLE |
257 (NVA3_3D_FP_MULTISAMPLE_EXPORT_SAMPLE_MASK *
258 fp->fp.has_samplemask));
259 else
260 PUSH_DATA(push, 0);
261 }
262 }
263
264 void
265 nv50_gmtyprog_validate(struct nv50_context *nv50)
266 {
267 struct nouveau_pushbuf *push = nv50->base.pushbuf;
268 struct nv50_program *gp = nv50->gmtyprog;
269
270 if (gp) {
271 if (!nv50_program_validate(nv50, gp))
272 return;
273 BEGIN_NV04(push, NV50_3D(GP_REG_ALLOC_TEMP), 1);
274 PUSH_DATA (push, gp->max_gpr);
275 BEGIN_NV04(push, NV50_3D(GP_REG_ALLOC_RESULT), 1);
276 PUSH_DATA (push, gp->max_out);
277 BEGIN_NV04(push, NV50_3D(GP_OUTPUT_PRIMITIVE_TYPE), 1);
278 PUSH_DATA (push, gp->gp.prim_type);
279 BEGIN_NV04(push, NV50_3D(GP_VERTEX_OUTPUT_COUNT), 1);
280 PUSH_DATA (push, gp->gp.vert_count);
281 BEGIN_NV04(push, NV50_3D(GP_START_ID), 1);
282 PUSH_DATA (push, gp->code_base);
283
284 nv50->state.prim_size = gp->gp.prim_type; /* enum matches vertex count */
285 }
286 nv50_program_update_context_state(nv50, gp, 2);
287
288 /* GP_ENABLE is updated in linkage validation */
289 }
290
291 void
292 nv50_compprog_validate(struct nv50_context *nv50)
293 {
294 struct nouveau_pushbuf *push = nv50->base.pushbuf;
295 struct nv50_program *cp = nv50->compprog;
296
297 if (cp && !nv50_program_validate(nv50, cp))
298 return;
299
300 BEGIN_NV04(push, NV50_CP(CODE_CB_FLUSH), 1);
301 PUSH_DATA (push, 0);
302 }
303
304 static void
305 nv50_sprite_coords_validate(struct nv50_context *nv50)
306 {
307 struct nouveau_pushbuf *push = nv50->base.pushbuf;
308 uint32_t pntc[8], mode;
309 struct nv50_program *fp = nv50->fragprog;
310 unsigned i, c;
311 unsigned m = (nv50->state.interpolant_ctrl >> 8) & 0xff;
312
313 if (!nv50->rast->pipe.point_quad_rasterization) {
314 if (nv50->state.point_sprite) {
315 BEGIN_NV04(push, NV50_3D(POINT_COORD_REPLACE_MAP(0)), 8);
316 for (i = 0; i < 8; ++i)
317 PUSH_DATA(push, 0);
318
319 nv50->state.point_sprite = false;
320 }
321 return;
322 } else {
323 nv50->state.point_sprite = true;
324 }
325
326 memset(pntc, 0, sizeof(pntc));
327
328 for (i = 0; i < fp->in_nr; i++) {
329 unsigned n = util_bitcount(fp->in[i].mask);
330
331 if (fp->in[i].sn != TGSI_SEMANTIC_GENERIC) {
332 m += n;
333 continue;
334 }
335 if (!(nv50->rast->pipe.sprite_coord_enable & (1 << fp->in[i].si))) {
336 m += n;
337 continue;
338 }
339
340 for (c = 0; c < 4; ++c) {
341 if (fp->in[i].mask & (1 << c)) {
342 pntc[m / 8] |= (c + 1) << ((m % 8) * 4);
343 ++m;
344 }
345 }
346 }
347
348 if (nv50->rast->pipe.sprite_coord_mode == PIPE_SPRITE_COORD_LOWER_LEFT)
349 mode = 0x00;
350 else
351 mode = 0x10;
352
353 BEGIN_NV04(push, NV50_3D(POINT_SPRITE_CTRL), 1);
354 PUSH_DATA (push, mode);
355
356 BEGIN_NV04(push, NV50_3D(POINT_COORD_REPLACE_MAP(0)), 8);
357 PUSH_DATAp(push, pntc, 8);
358 }
359
360 /* Validate state derived from shaders and the rasterizer cso. */
361 void
362 nv50_validate_derived_rs(struct nv50_context *nv50)
363 {
364 struct nouveau_pushbuf *push = nv50->base.pushbuf;
365 uint32_t color, psize;
366
367 nv50_sprite_coords_validate(nv50);
368
369 if (nv50->state.rasterizer_discard != nv50->rast->pipe.rasterizer_discard) {
370 nv50->state.rasterizer_discard = nv50->rast->pipe.rasterizer_discard;
371 BEGIN_NV04(push, NV50_3D(RASTERIZE_ENABLE), 1);
372 PUSH_DATA (push, !nv50->rast->pipe.rasterizer_discard);
373 }
374
375 if (nv50->dirty_3d & NV50_NEW_3D_FRAGPROG)
376 return;
377 psize = nv50->state.semantic_psize & ~NV50_3D_SEMANTIC_PTSZ_PTSZ_EN__MASK;
378 color = nv50->state.semantic_color & ~NV50_3D_SEMANTIC_COLOR_CLMP_EN;
379
380 if (nv50->rast->pipe.clamp_vertex_color)
381 color |= NV50_3D_SEMANTIC_COLOR_CLMP_EN;
382
383 if (color != nv50->state.semantic_color) {
384 nv50->state.semantic_color = color;
385 BEGIN_NV04(push, NV50_3D(SEMANTIC_COLOR), 1);
386 PUSH_DATA (push, color);
387 }
388
389 if (nv50->rast->pipe.point_size_per_vertex)
390 psize |= NV50_3D_SEMANTIC_PTSZ_PTSZ_EN__MASK;
391
392 if (psize != nv50->state.semantic_psize) {
393 nv50->state.semantic_psize = psize;
394 BEGIN_NV04(push, NV50_3D(SEMANTIC_PTSZ), 1);
395 PUSH_DATA (push, psize);
396 }
397 }
398
399 static int
400 nv50_vec4_map(uint8_t *map, int mid, uint32_t lin[4],
401 struct nv50_varying *in, struct nv50_varying *out)
402 {
403 int c;
404 uint8_t mv = out->mask, mf = in->mask, oid = out->hw;
405
406 for (c = 0; c < 4; ++c) {
407 if (mf & 1) {
408 if (in->linear)
409 lin[mid / 32] |= 1 << (mid % 32);
410 if (mv & 1)
411 map[mid] = oid;
412 else
413 if (c == 3)
414 map[mid] |= 1;
415 ++mid;
416 }
417
418 oid += mv & 1;
419 mf >>= 1;
420 mv >>= 1;
421 }
422
423 return mid;
424 }
425
426 void
427 nv50_fp_linkage_validate(struct nv50_context *nv50)
428 {
429 struct nouveau_pushbuf *push = nv50->base.pushbuf;
430 struct nv50_program *vp = nv50->gmtyprog ? nv50->gmtyprog : nv50->vertprog;
431 struct nv50_program *fp = nv50->fragprog;
432 struct nv50_varying dummy;
433 int i, n, c, m;
434 uint32_t primid = 0;
435 uint32_t layerid = 0;
436 uint32_t viewportid = 0;
437 uint32_t psiz = 0x000;
438 uint32_t interp = fp->fp.interp;
439 uint32_t colors = fp->fp.colors;
440 uint32_t clpd_nr = util_last_bit(vp->vp.clip_enable | vp->vp.cull_enable);
441 uint32_t lin[4];
442 uint8_t map[64];
443 uint8_t so_map[64];
444
445 if (!(nv50->dirty_3d & (NV50_NEW_3D_VERTPROG |
446 NV50_NEW_3D_FRAGPROG |
447 NV50_NEW_3D_GMTYPROG))) {
448 uint8_t bfc, ffc;
449 ffc = (nv50->state.semantic_color & NV50_3D_SEMANTIC_COLOR_FFC0_ID__MASK);
450 bfc = (nv50->state.semantic_color & NV50_3D_SEMANTIC_COLOR_BFC0_ID__MASK)
451 >> 8;
452 if (nv50->rast->pipe.light_twoside == ((ffc == bfc) ? 0 : 1))
453 return;
454 }
455
456 memset(lin, 0x00, sizeof(lin));
457
458 /* XXX: in buggy-endian mode, is the first element of map (u32)0x000000xx
459 * or is it the first byte ?
460 */
461 memset(map, nv50->gmtyprog ? 0x80 : 0x40, sizeof(map));
462
463 dummy.mask = 0xf; /* map all components of HPOS */
464 dummy.linear = 0;
465 m = nv50_vec4_map(map, 0, lin, &dummy, &vp->out[0]);
466
467 for (c = 0; c < clpd_nr; ++c)
468 map[m++] = vp->vp.clpd[c / 4] + (c % 4);
469
470 colors |= m << 8; /* adjust BFC0 id */
471
472 dummy.mask = 0x0;
473
474 /* if light_twoside is active, FFC0_ID == BFC0_ID is invalid */
475 if (nv50->rast->pipe.light_twoside) {
476 for (i = 0; i < 2; ++i) {
477 n = vp->vp.bfc[i];
478 if (fp->vp.bfc[i] >= fp->in_nr)
479 continue;
480 m = nv50_vec4_map(map, m, lin, &fp->in[fp->vp.bfc[i]],
481 (n < vp->out_nr) ? &vp->out[n] : &dummy);
482 }
483 }
484 colors += m - 4; /* adjust FFC0 id */
485 interp |= m << 8; /* set map id where 'normal' FP inputs start */
486
487 for (i = 0; i < fp->in_nr; ++i) {
488 for (n = 0; n < vp->out_nr; ++n)
489 if (vp->out[n].sn == fp->in[i].sn &&
490 vp->out[n].si == fp->in[i].si)
491 break;
492 switch (fp->in[i].sn) {
493 case TGSI_SEMANTIC_PRIMID:
494 primid = m;
495 break;
496 case TGSI_SEMANTIC_LAYER:
497 layerid = m;
498 break;
499 case TGSI_SEMANTIC_VIEWPORT_INDEX:
500 viewportid = m;
501 break;
502 }
503 m = nv50_vec4_map(map, m, lin,
504 &fp->in[i], (n < vp->out_nr) ? &vp->out[n] : &dummy);
505 }
506
507 if (vp->gp.has_layer && !layerid) {
508 layerid = m;
509 map[m++] = vp->gp.layerid;
510 }
511
512 if (vp->gp.has_viewport && !viewportid) {
513 viewportid = m;
514 map[m++] = vp->gp.viewportid;
515 }
516
517 if (nv50->rast->pipe.point_size_per_vertex) {
518 psiz = (m << 4) | 1;
519 map[m++] = vp->vp.psiz;
520 }
521
522 if (nv50->rast->pipe.clamp_vertex_color)
523 colors |= NV50_3D_SEMANTIC_COLOR_CLMP_EN;
524
525 if (unlikely(vp->so)) {
526 /* Slot i in STRMOUT_MAP specifies the offset where slot i in RESULT_MAP
527 * gets written.
528 *
529 * TODO:
530 * Inverting vp->so->map (output -> offset) would probably speed this up.
531 */
532 memset(so_map, 0, sizeof(so_map));
533 for (i = 0; i < vp->so->map_size; ++i) {
534 if (vp->so->map[i] == 0xff)
535 continue;
536 for (c = 0; c < m; ++c)
537 if (map[c] == vp->so->map[i] && !so_map[c])
538 break;
539 if (c == m) {
540 c = m;
541 map[m++] = vp->so->map[i];
542 }
543 so_map[c] = 0x80 | i;
544 }
545 for (c = m; c & 3; ++c)
546 so_map[c] = 0;
547 }
548
549 n = (m + 3) / 4;
550 assert(m <= 64);
551
552 if (unlikely(nv50->gmtyprog)) {
553 BEGIN_NV04(push, NV50_3D(GP_RESULT_MAP_SIZE), 1);
554 PUSH_DATA (push, m);
555 BEGIN_NV04(push, NV50_3D(GP_RESULT_MAP(0)), n);
556 PUSH_DATAp(push, map, n);
557 } else {
558 BEGIN_NV04(push, NV50_3D(VP_GP_BUILTIN_ATTR_EN), 1);
559 PUSH_DATA (push, vp->vp.attrs[2] | fp->vp.attrs[2]);
560
561 BEGIN_NV04(push, NV50_3D(SEMANTIC_PRIM_ID), 1);
562 PUSH_DATA (push, primid);
563
564 assert(m > 0);
565 BEGIN_NV04(push, NV50_3D(VP_RESULT_MAP_SIZE), 1);
566 PUSH_DATA (push, m);
567 BEGIN_NV04(push, NV50_3D(VP_RESULT_MAP(0)), n);
568 PUSH_DATAp(push, map, n);
569 }
570
571 BEGIN_NV04(push, NV50_3D(GP_VIEWPORT_ID_ENABLE), 5);
572 PUSH_DATA (push, vp->gp.has_viewport);
573 PUSH_DATA (push, colors);
574 PUSH_DATA (push, (clpd_nr << 8) | 4);
575 PUSH_DATA (push, layerid);
576 PUSH_DATA (push, psiz);
577
578 BEGIN_NV04(push, NV50_3D(SEMANTIC_VIEWPORT), 1);
579 PUSH_DATA (push, viewportid);
580
581 BEGIN_NV04(push, NV50_3D(LAYER), 1);
582 PUSH_DATA (push, vp->gp.has_layer << 16);
583
584 BEGIN_NV04(push, NV50_3D(FP_INTERPOLANT_CTRL), 1);
585 PUSH_DATA (push, interp);
586
587 nv50->state.interpolant_ctrl = interp;
588
589 nv50->state.semantic_color = colors;
590 nv50->state.semantic_psize = psiz;
591
592 BEGIN_NV04(push, NV50_3D(NOPERSPECTIVE_BITMAP(0)), 4);
593 PUSH_DATAp(push, lin, 4);
594
595 BEGIN_NV04(push, NV50_3D(GP_ENABLE), 1);
596 PUSH_DATA (push, nv50->gmtyprog ? 1 : 0);
597
598 if (vp->so) {
599 BEGIN_NV04(push, NV50_3D(STRMOUT_MAP(0)), n);
600 PUSH_DATAp(push, so_map, n);
601 }
602 }
603
604 static int
605 nv50_vp_gp_mapping(uint8_t *map, int m,
606 struct nv50_program *vp, struct nv50_program *gp)
607 {
608 int i, j, c;
609
610 for (i = 0; i < gp->in_nr; ++i) {
611 uint8_t oid = 0, mv = 0, mg = gp->in[i].mask;
612
613 for (j = 0; j < vp->out_nr; ++j) {
614 if (vp->out[j].sn == gp->in[i].sn &&
615 vp->out[j].si == gp->in[i].si) {
616 mv = vp->out[j].mask;
617 oid = vp->out[j].hw;
618 break;
619 }
620 }
621
622 for (c = 0; c < 4; ++c, mv >>= 1, mg >>= 1) {
623 if (mg & mv & 1)
624 map[m++] = oid;
625 else
626 if (mg & 1)
627 map[m++] = (c == 3) ? 0x41 : 0x40;
628 oid += mv & 1;
629 }
630 }
631 if (!m)
632 map[m++] = 0;
633 return m;
634 }
635
636 void
637 nv50_gp_linkage_validate(struct nv50_context *nv50)
638 {
639 struct nouveau_pushbuf *push = nv50->base.pushbuf;
640 struct nv50_program *vp = nv50->vertprog;
641 struct nv50_program *gp = nv50->gmtyprog;
642 int m = 0;
643 int n;
644 uint8_t map[64];
645
646 if (!gp)
647 return;
648 memset(map, 0, sizeof(map));
649
650 m = nv50_vp_gp_mapping(map, m, vp, gp);
651
652 n = (m + 3) / 4;
653
654 BEGIN_NV04(push, NV50_3D(VP_GP_BUILTIN_ATTR_EN), 1);
655 PUSH_DATA (push, vp->vp.attrs[2] | gp->vp.attrs[2]);
656
657 assert(m > 0);
658 BEGIN_NV04(push, NV50_3D(VP_RESULT_MAP_SIZE), 1);
659 PUSH_DATA (push, m);
660 BEGIN_NV04(push, NV50_3D(VP_RESULT_MAP(0)), n);
661 PUSH_DATAp(push, map, n);
662 }
663
664 void
665 nv50_stream_output_validate(struct nv50_context *nv50)
666 {
667 struct nouveau_pushbuf *push = nv50->base.pushbuf;
668 struct nv50_stream_output_state *so;
669 uint32_t ctrl;
670 unsigned i;
671 unsigned prims = ~0;
672
673 so = nv50->gmtyprog ? nv50->gmtyprog->so : nv50->vertprog->so;
674
675 BEGIN_NV04(push, NV50_3D(STRMOUT_ENABLE), 1);
676 PUSH_DATA (push, 0);
677 if (!so || !nv50->num_so_targets) {
678 if (nv50->screen->base.class_3d < NVA0_3D_CLASS) {
679 BEGIN_NV04(push, NV50_3D(STRMOUT_PRIMITIVE_LIMIT), 1);
680 PUSH_DATA (push, 0);
681 }
682 BEGIN_NV04(push, NV50_3D(STRMOUT_PARAMS_LATCH), 1);
683 PUSH_DATA (push, 1);
684 return;
685 }
686
687 /* previous TFB needs to complete */
688 if (nv50->screen->base.class_3d < NVA0_3D_CLASS) {
689 BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
690 PUSH_DATA (push, 0);
691 }
692
693 ctrl = so->ctrl;
694 if (nv50->screen->base.class_3d >= NVA0_3D_CLASS)
695 ctrl |= NVA0_3D_STRMOUT_BUFFERS_CTRL_LIMIT_MODE_OFFSET;
696
697 BEGIN_NV04(push, NV50_3D(STRMOUT_BUFFERS_CTRL), 1);
698 PUSH_DATA (push, ctrl);
699
700 for (i = 0; i < nv50->num_so_targets; ++i) {
701 struct nv50_so_target *targ = nv50_so_target(nv50->so_target[i]);
702 struct nv04_resource *buf = nv04_resource(targ->pipe.buffer);
703
704 const unsigned n = nv50->screen->base.class_3d >= NVA0_3D_CLASS ? 4 : 3;
705
706 uint32_t so_used = 0;
707
708 if (!targ->clean) {
709 if (n == 4)
710 nv84_hw_query_fifo_wait(push, nv50_query(targ->pq));
711 else
712 so_used = nv50->so_used[i];
713 }
714 BEGIN_NV04(push, NV50_3D(STRMOUT_ADDRESS_HIGH(i)), n);
715 PUSH_DATAh(push, buf->address + targ->pipe.buffer_offset + so_used);
716 PUSH_DATA (push, buf->address + targ->pipe.buffer_offset + so_used);
717 PUSH_DATA (push, so->num_attribs[i]);
718 if (n == 4) {
719 PUSH_DATA(push, targ->pipe.buffer_size);
720 if (!targ->clean) {
721 assert(targ->pq);
722 nv50_hw_query_pushbuf_submit(nv50, NVA0_3D_STRMOUT_OFFSET(i),
723 nv50_query(targ->pq), 0x4);
724 } else {
725 BEGIN_NV04(push, NVA0_3D(STRMOUT_OFFSET(i)), 1);
726 PUSH_DATA(push, 0);
727 targ->clean = false;
728 }
729 } else {
730 const unsigned limit = (targ->pipe.buffer_size - so_used) /
731 (so->stride[i] * nv50->state.prim_size);
732 prims = MIN2(prims, limit);
733 targ->clean = false;
734 }
735 targ->stride = so->stride[i];
736 BCTX_REFN(nv50->bufctx_3d, 3D_SO, buf, WR);
737 }
738 if (prims != ~0) {
739 BEGIN_NV04(push, NV50_3D(STRMOUT_PRIMITIVE_LIMIT), 1);
740 PUSH_DATA (push, prims);
741 }
742 BEGIN_NV04(push, NV50_3D(STRMOUT_PARAMS_LATCH), 1);
743 PUSH_DATA (push, 1);
744 BEGIN_NV04(push, NV50_3D(STRMOUT_ENABLE), 1);
745 PUSH_DATA (push, 1);
746 }