Squash-merge branch 'gallium-clip-state'
[mesa.git] / src / gallium / drivers / nv50 / nv50_shader_state.c
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 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
20 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "pipe/p_context.h"
25 #include "pipe/p_defines.h"
26 #include "pipe/p_state.h"
27 #include "util/u_inlines.h"
28
29 #include "nv50_context.h"
30
31 void
32 nv50_constbufs_validate(struct nv50_context *nv50)
33 {
34 struct nouveau_channel *chan = nv50->screen->base.channel;
35 unsigned s;
36
37 for (s = 0; s < 3; ++s) {
38 struct nv04_resource *res;
39 int i;
40 unsigned p, b;
41
42 if (s == PIPE_SHADER_FRAGMENT)
43 p = NV50_3D_SET_PROGRAM_CB_PROGRAM_FRAGMENT;
44 else
45 if (s == PIPE_SHADER_GEOMETRY)
46 p = NV50_3D_SET_PROGRAM_CB_PROGRAM_GEOMETRY;
47 else
48 p = NV50_3D_SET_PROGRAM_CB_PROGRAM_VERTEX;
49
50 while (nv50->constbuf_dirty[s]) {
51 struct nouveau_bo *bo;
52 unsigned start = 0;
53 unsigned words = 0;
54
55 i = ffs(nv50->constbuf_dirty[s]) - 1;
56 nv50->constbuf_dirty[s] &= ~(1 << i);
57
58 res = nv04_resource(nv50->constbuf[s][i]);
59 if (!res) {
60 if (i != 0) {
61 BEGIN_RING(chan, RING_3D(SET_PROGRAM_CB), 1);
62 OUT_RING (chan, (i << 8) | p | 0);
63 }
64 continue;
65 }
66
67 if (i == 0) {
68 b = NV50_CB_PVP + s;
69
70 /* always upload GL uniforms through CB DATA */
71 bo = nv50->screen->uniforms;
72 words = res->base.width0 / 4;
73 } else {
74 b = s * 16 + i;
75
76 assert(0);
77
78 if (!nouveau_resource_mapped_by_gpu(&res->base)) {
79 nouveau_buffer_migrate(&nv50->base, res, NOUVEAU_BO_VRAM);
80
81 BEGIN_RING(chan, RING_3D(CODE_CB_FLUSH), 1);
82 OUT_RING (chan, 0);
83 }
84 MARK_RING (chan, 6, 2);
85 BEGIN_RING(chan, RING_3D(CB_DEF_ADDRESS_HIGH), 3);
86 OUT_RESRCh(chan, res, 0, NOUVEAU_BO_RD);
87 OUT_RESRCl(chan, res, 0, NOUVEAU_BO_RD);
88 OUT_RING (chan, (b << 16) | (res->base.width0 & 0xffff));
89 BEGIN_RING(chan, RING_3D(SET_PROGRAM_CB), 1);
90 OUT_RING (chan, (b << 12) | (i << 8) | p | 1);
91
92 bo = res->bo;
93
94 nv50_bufctx_add_resident(nv50, NV50_BUFCTX_CONSTANT, res,
95 res->domain | NOUVEAU_BO_RD);
96 }
97
98 if (words) {
99 MARK_RING(chan, 8, 1);
100
101 nouveau_bo_validate(chan, bo, res->domain | NOUVEAU_BO_WR);
102 }
103
104 while (words) {
105 unsigned nr = AVAIL_RING(chan);
106
107 if (nr < 16) {
108 FIRE_RING(chan);
109 nouveau_bo_validate(chan, bo, res->domain | NOUVEAU_BO_WR);
110 continue;
111 }
112 nr = MIN2(MIN2(nr - 3, words), NV04_PFIFO_MAX_PACKET_LEN);
113
114 BEGIN_RING(chan, RING_3D(CB_ADDR), 1);
115 OUT_RING (chan, (start << 8) | b);
116 BEGIN_RING_NI(chan, RING_3D(CB_DATA(0)), nr);
117 OUT_RINGp (chan, &res->data[start * 4], nr);
118
119 start += nr;
120 words -= nr;
121 }
122 }
123 }
124 }
125
126 static boolean
127 nv50_program_validate(struct nv50_context *nv50, struct nv50_program *prog)
128 {
129 struct nouveau_resource *heap;
130 int ret;
131 unsigned size;
132
133 if (!prog->translated) {
134 prog->translated = nv50_program_translate(prog);
135 if (!prog->translated)
136 return FALSE;
137 } else
138 if (prog->res)
139 return TRUE;
140
141 if (prog->type == PIPE_SHADER_FRAGMENT) heap = nv50->screen->fp_code_heap;
142 else
143 if (prog->type == PIPE_SHADER_GEOMETRY) heap = nv50->screen->gp_code_heap;
144 else
145 heap = nv50->screen->vp_code_heap;
146
147 size = align(prog->code_size, 0x100);
148
149 ret = nouveau_resource_alloc(heap, size, prog, &prog->res);
150 if (ret) {
151 NOUVEAU_ERR("out of code space for shader type %i\n", prog->type);
152 return FALSE;
153 }
154 prog->code_base = prog->res->start;
155
156 nv50_relocate_program(prog, prog->code_base, 0);
157
158 nv50_sifc_linear_u8(&nv50->base, nv50->screen->code,
159 (prog->type << NV50_CODE_BO_SIZE_LOG2) + prog->code_base,
160 NOUVEAU_BO_VRAM, prog->code_size, prog->code);
161
162 BEGIN_RING(nv50->screen->base.channel, RING_3D(CODE_CB_FLUSH), 1);
163 OUT_RING (nv50->screen->base.channel, 0);
164
165 return TRUE;
166 }
167
168 void
169 nv50_vertprog_validate(struct nv50_context *nv50)
170 {
171 struct nouveau_channel *chan = nv50->screen->base.channel;
172 struct nv50_program *vp = nv50->vertprog;
173
174 if (!nv50_program_validate(nv50, vp))
175 return;
176
177 BEGIN_RING(chan, RING_3D(VP_ATTR_EN(0)), 2);
178 OUT_RING (chan, vp->vp.attrs[0]);
179 OUT_RING (chan, vp->vp.attrs[1]);
180 BEGIN_RING(chan, RING_3D(VP_REG_ALLOC_RESULT), 1);
181 OUT_RING (chan, vp->max_out);
182 BEGIN_RING(chan, RING_3D(VP_REG_ALLOC_TEMP), 1);
183 OUT_RING (chan, vp->max_gpr);
184 BEGIN_RING(chan, RING_3D(VP_START_ID), 1);
185 OUT_RING (chan, vp->code_base);
186 }
187
188 void
189 nv50_fragprog_validate(struct nv50_context *nv50)
190 {
191 struct nouveau_channel *chan = nv50->screen->base.channel;
192 struct nv50_program *fp = nv50->fragprog;
193
194 if (!nv50_program_validate(nv50, fp))
195 return;
196
197 BEGIN_RING(chan, RING_3D(FP_REG_ALLOC_TEMP), 1);
198 OUT_RING (chan, fp->max_gpr);
199 BEGIN_RING(chan, RING_3D(FP_RESULT_COUNT), 1);
200 OUT_RING (chan, fp->max_out);
201 BEGIN_RING(chan, RING_3D(FP_CONTROL), 1);
202 OUT_RING (chan, fp->fp.flags[0]);
203 BEGIN_RING(chan, RING_3D(FP_CTRL_UNK196C), 1);
204 OUT_RING (chan, fp->fp.flags[1]);
205 BEGIN_RING(chan, RING_3D(FP_START_ID), 1);
206 OUT_RING (chan, fp->code_base);
207 }
208
209 void
210 nv50_gmtyprog_validate(struct nv50_context *nv50)
211 {
212 struct nouveau_channel *chan = nv50->screen->base.channel;
213 struct nv50_program *gp = nv50->gmtyprog;
214
215 if (!gp) /* GP_ENABLE is updated in linkage validation */
216 return;
217 if (!nv50_program_validate(nv50, gp))
218 return;
219
220 BEGIN_RING(chan, RING_3D(GP_REG_ALLOC_TEMP), 1);
221 OUT_RING (chan, gp->max_gpr);
222 BEGIN_RING(chan, RING_3D(GP_REG_ALLOC_RESULT), 1);
223 OUT_RING (chan, gp->max_out);
224 BEGIN_RING(chan, RING_3D(GP_OUTPUT_PRIMITIVE_TYPE), 1);
225 OUT_RING (chan, gp->gp.prim_type);
226 BEGIN_RING(chan, RING_3D(GP_VERTEX_OUTPUT_COUNT), 1);
227 OUT_RING (chan, gp->gp.vert_count);
228 BEGIN_RING(chan, RING_3D(GP_START_ID), 1);
229 OUT_RING (chan, gp->code_base);
230 }
231
232 static void
233 nv50_sprite_coords_validate(struct nv50_context *nv50)
234 {
235 struct nouveau_channel *chan = nv50->screen->base.channel;
236 uint32_t pntc[8], mode;
237 struct nv50_program *fp = nv50->fragprog;
238 unsigned i, c;
239 unsigned m = (nv50->state.interpolant_ctrl >> 8) & 0xff;
240
241 if (!nv50->rast->pipe.point_quad_rasterization) {
242 if (nv50->state.point_sprite) {
243 BEGIN_RING(chan, RING_3D(POINT_COORD_REPLACE_MAP(0)), 8);
244 for (i = 0; i < 8; ++i)
245 OUT_RING(chan, 0);
246
247 nv50->state.point_sprite = FALSE;
248 }
249 return;
250 } else {
251 nv50->state.point_sprite = TRUE;
252 }
253
254 memset(pntc, 0, sizeof(pntc));
255
256 for (i = 0; i < fp->in_nr; i++) {
257 unsigned n = util_bitcount(fp->in[i].mask);
258
259 if (fp->in[i].sn != TGSI_SEMANTIC_GENERIC) {
260 m += n;
261 continue;
262 }
263 if (!(nv50->rast->pipe.sprite_coord_enable & (1 << fp->in[i].si))) {
264 m += n;
265 continue;
266 }
267
268 for (c = 0; c < 4; ++c) {
269 if (fp->in[i].mask & (1 << c)) {
270 pntc[m / 8] |= (c + 1) << ((m % 8) * 4);
271 ++m;
272 }
273 }
274 }
275
276 if (nv50->rast->pipe.sprite_coord_mode == PIPE_SPRITE_COORD_LOWER_LEFT)
277 mode = 0x00;
278 else
279 mode = 0x10;
280
281 BEGIN_RING(chan, RING_3D(POINT_SPRITE_CTRL), 1);
282 OUT_RING (chan, mode);
283
284 BEGIN_RING(chan, RING_3D(POINT_COORD_REPLACE_MAP(0)), 8);
285 OUT_RINGp (chan, pntc, 8);
286 }
287
288 /* Validate state derived from shaders and the rasterizer cso. */
289 void
290 nv50_validate_derived_rs(struct nv50_context *nv50)
291 {
292 struct nouveau_channel *chan = nv50->screen->base.channel;
293 uint32_t color, psize;
294
295 nv50_sprite_coords_validate(nv50);
296
297 if (nv50->dirty & NV50_NEW_FRAGPROG)
298 return;
299 psize = nv50->state.semantic_psize & ~NV50_3D_MAP_SEMANTIC_3_PTSZ_EN__MASK;
300 color = nv50->state.semantic_color & ~NV50_3D_MAP_SEMANTIC_0_CLMP_EN;
301
302 if (nv50->rast->pipe.clamp_vertex_color)
303 color |= NV50_3D_MAP_SEMANTIC_0_CLMP_EN;
304
305 if (color != nv50->state.semantic_color) {
306 nv50->state.semantic_color = color;
307 BEGIN_RING(chan, RING_3D(MAP_SEMANTIC_0), 1);
308 OUT_RING (chan, color);
309 }
310
311 if (nv50->rast->pipe.point_size_per_vertex)
312 psize |= NV50_3D_MAP_SEMANTIC_3_PTSZ_EN__MASK;
313
314 if (psize != nv50->state.semantic_psize) {
315 nv50->state.semantic_psize = psize;
316 BEGIN_RING(chan, RING_3D(MAP_SEMANTIC_3), 1);
317 OUT_RING (chan, psize);
318 }
319 }
320
321 static int
322 nv50_vec4_map(uint8_t *map, int mid, uint32_t lin[4],
323 struct nv50_varying *in, struct nv50_varying *out)
324 {
325 int c;
326 uint8_t mv = out->mask, mf = in->mask, oid = out->hw;
327
328 for (c = 0; c < 4; ++c) {
329 if (mf & 1) {
330 if (in->linear)
331 lin[mid / 32] |= 1 << (mid % 32);
332 if (mv & 1)
333 map[mid] = oid;
334 else
335 if (c == 3)
336 map[mid] |= 1;
337 ++mid;
338 }
339
340 oid += mv & 1;
341 mf >>= 1;
342 mv >>= 1;
343 }
344
345 return mid;
346 }
347
348 void
349 nv50_fp_linkage_validate(struct nv50_context *nv50)
350 {
351 struct nouveau_channel *chan = nv50->screen->base.channel;
352 struct nv50_program *vp = nv50->gmtyprog ? nv50->gmtyprog : nv50->vertprog;
353 struct nv50_program *fp = nv50->fragprog;
354 struct nv50_varying dummy;
355 int i, n, c, m;
356 uint32_t primid = 0;
357 uint32_t psiz = 0x000;
358 uint32_t interp = fp->fp.interp;
359 uint32_t colors = fp->fp.colors;
360 uint32_t lin[4];
361 uint8_t map[64];
362
363 memset(lin, 0x00, sizeof(lin));
364
365 /* XXX: in buggy-endian mode, is the first element of map (u32)0x000000xx
366 * or is it the first byte ?
367 */
368 memset(map, nv50->gmtyprog ? 0x80 : 0x40, sizeof(map));
369
370 dummy.mask = 0xf; /* map all components of HPOS */
371 dummy.linear = 0;
372 m = nv50_vec4_map(map, 0, lin, &dummy, &vp->out[0]);
373
374 for (c = 0; c < vp->vp.clpd_nr; ++c)
375 map[m++] = vp->vp.clpd + c;
376
377 colors |= m << 8; /* adjust BFC0 id */
378
379 /* if light_twoside is active, FFC0_ID == BFC0_ID is invalid */
380 if (nv50->rast->pipe.light_twoside) {
381 for (i = 0; i < 2; ++i)
382 m = nv50_vec4_map(map, m, lin,
383 &fp->in[fp->vp.bfc[i]], &vp->out[vp->vp.bfc[i]]);
384 }
385 colors += m - 4; /* adjust FFC0 id */
386 interp |= m << 8; /* set map id where 'normal' FP inputs start */
387
388 dummy.mask = 0x0;
389 for (i = 0; i < fp->in_nr; ++i) {
390 for (n = 0; n < vp->out_nr; ++n)
391 if (vp->out[n].sn == fp->in[i].sn &&
392 vp->out[n].si == fp->in[i].si)
393 break;
394 m = nv50_vec4_map(map, m, lin,
395 &fp->in[i], (n < vp->out_nr) ? &vp->out[n] : &dummy);
396 }
397
398 /* PrimitiveID either is replaced by the system value, or
399 * written by the geometry shader into an output register
400 */
401 if (fp->gp.primid < 0x40) {
402 primid = m;
403 map[m++] = vp->gp.primid;
404 }
405
406 if (nv50->rast->pipe.point_size_per_vertex) {
407 psiz = (m << 4) | 1;
408 map[m++] = vp->vp.psiz;
409 }
410
411 if (nv50->rast->pipe.clamp_vertex_color)
412 colors |= NV50_3D_MAP_SEMANTIC_0_CLMP_EN;
413
414 n = (m + 3) / 4;
415 assert(m <= 64);
416
417 if (unlikely(nv50->gmtyprog)) {
418 BEGIN_RING(chan, RING_3D(GP_RESULT_MAP_SIZE), 1);
419 OUT_RING (chan, m);
420 BEGIN_RING(chan, RING_3D(GP_RESULT_MAP(0)), n);
421 OUT_RINGp (chan, map, n);
422 } else {
423 BEGIN_RING(chan, RING_3D(VP_GP_BUILTIN_ATTR_EN), 1);
424 OUT_RING (chan, vp->vp.attrs[2]);
425
426 BEGIN_RING(chan, RING_3D(MAP_SEMANTIC_4), 1);
427 OUT_RING (chan, primid);
428
429 BEGIN_RING(chan, RING_3D(VP_RESULT_MAP_SIZE), 1);
430 OUT_RING (chan, m);
431 BEGIN_RING(chan, RING_3D(VP_RESULT_MAP(0)), n);
432 OUT_RINGp (chan, map, n);
433 }
434
435 BEGIN_RING(chan, RING_3D(MAP_SEMANTIC_0), 4);
436 OUT_RING (chan, colors);
437 OUT_RING (chan, (vp->vp.clpd_nr << 8) | 4);
438 OUT_RING (chan, 0);
439 OUT_RING (chan, psiz);
440
441 BEGIN_RING(chan, RING_3D(FP_INTERPOLANT_CTRL), 1);
442 OUT_RING (chan, interp);
443
444 nv50->state.interpolant_ctrl = interp;
445
446 nv50->state.semantic_color = colors;
447 nv50->state.semantic_psize = psiz;
448
449 BEGIN_RING(chan, RING_3D(NOPERSPECTIVE_BITMAP(0)), 4);
450 OUT_RINGp (chan, lin, 4);
451
452 BEGIN_RING(chan, RING_3D(GP_ENABLE), 1);
453 OUT_RING (chan, nv50->gmtyprog ? 1 : 0);
454 }
455
456 static int
457 nv50_vp_gp_mapping(uint8_t *map, int m,
458 struct nv50_program *vp, struct nv50_program *gp)
459 {
460 int i, j, c;
461
462 for (i = 0; i < gp->in_nr; ++i) {
463 uint8_t oid = 0, mv = 0, mg = gp->in[i].mask;
464
465 for (j = 0; j < vp->out_nr; ++j) {
466 if (vp->out[j].sn == gp->in[i].sn &&
467 vp->out[j].si == gp->in[i].si) {
468 mv = vp->out[j].mask;
469 oid = vp->out[j].hw;
470 break;
471 }
472 }
473
474 for (c = 0; c < 4; ++c, mv >>= 1, mg >>= 1) {
475 if (mg & mv & 1)
476 map[m++] = oid;
477 else
478 if (mg & 1)
479 map[m++] = (c == 3) ? 0x41 : 0x40;
480 oid += mv & 1;
481 }
482 }
483 return m;
484 }
485
486 void
487 nv50_gp_linkage_validate(struct nv50_context *nv50)
488 {
489 struct nouveau_channel *chan = nv50->screen->base.channel;
490 struct nv50_program *vp = nv50->vertprog;
491 struct nv50_program *gp = nv50->gmtyprog;
492 int m = 0;
493 int n;
494 uint8_t map[64];
495
496 if (!gp)
497 return;
498 memset(map, 0, sizeof(map));
499
500 m = nv50_vp_gp_mapping(map, m, vp, gp);
501
502 n = (m + 3) / 4;
503
504 BEGIN_RING(chan, RING_3D(VP_GP_BUILTIN_ATTR_EN), 1);
505 OUT_RING (chan, vp->vp.attrs[2] | gp->vp.attrs[2]);
506
507 BEGIN_RING(chan, RING_3D(VP_RESULT_MAP_SIZE), 1);
508 OUT_RING (chan, m);
509 BEGIN_RING(chan, RING_3D(VP_RESULT_MAP(0)), n);
510 OUT_RINGp (chan, map, n);
511 }