8d662d8f60e4ec794ae7c935703e5a12b8aa76f3
[mesa.git] / src / gallium / drivers / nv50 / nv50_state_validate.c
1 /*
2 * Copyright 2008 Ben Skeggs
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 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include "util/u_format.h"
24
25 #include "nv50_context.h"
26 #include "nv50_resource.h"
27 #include "nouveau/nouveau_stateobj.h"
28
29 static struct nouveau_stateobj *
30 validate_fb(struct nv50_context *nv50)
31 {
32 struct nouveau_grobj *tesla = nv50->screen->tesla;
33 struct nouveau_stateobj *so = so_new(32, 79, 18);
34 struct pipe_framebuffer_state *fb = &nv50->framebuffer;
35 unsigned i, w = 0, h = 0, gw = 0;
36
37 /* Set nr of active RTs and select RT for each colour output.
38 * FP result 0 always goes to RT[0], bits 4 - 6 are ignored.
39 * Ambiguous assignment results in no rendering (no DATA_ERROR).
40 */
41 so_method(so, tesla, NV50TCL_RT_CONTROL, 1);
42 so_data (so, fb->nr_cbufs |
43 (0 << 4) | (1 << 7) | (2 << 10) | (3 << 13) |
44 (4 << 16) | (5 << 19) | (6 << 22) | (7 << 25));
45
46 for (i = 0; i < fb->nr_cbufs; i++) {
47 struct pipe_resource *pt = fb->cbufs[i]->texture;
48 struct nouveau_bo *bo = nv50_miptree(pt)->base.bo;
49
50 if (!gw) {
51 w = fb->cbufs[i]->width;
52 h = fb->cbufs[i]->height;
53 gw = 1;
54 } else {
55 assert(w == fb->cbufs[i]->width);
56 assert(h == fb->cbufs[i]->height);
57 }
58
59 so_method(so, tesla, NV50TCL_RT_HORIZ(i), 2);
60 so_data (so, fb->cbufs[i]->width);
61 so_data (so, fb->cbufs[i]->height);
62
63 so_method(so, tesla, NV50TCL_RT_ADDRESS_HIGH(i), 5);
64 so_reloc (so, bo, fb->cbufs[i]->offset, NOUVEAU_BO_VRAM |
65 NOUVEAU_BO_HIGH | NOUVEAU_BO_RDWR, 0, 0);
66 so_reloc (so, bo, fb->cbufs[i]->offset, NOUVEAU_BO_VRAM |
67 NOUVEAU_BO_LOW | NOUVEAU_BO_RDWR, 0, 0);
68 switch (fb->cbufs[i]->format) {
69 case PIPE_FORMAT_B8G8R8A8_UNORM:
70 so_data(so, NV50TCL_RT_FORMAT_A8R8G8B8_UNORM);
71 break;
72 case PIPE_FORMAT_B8G8R8X8_UNORM:
73 so_data(so, NV50TCL_RT_FORMAT_X8R8G8B8_UNORM);
74 break;
75 case PIPE_FORMAT_B5G6R5_UNORM:
76 so_data(so, NV50TCL_RT_FORMAT_R5G6B5_UNORM);
77 break;
78 case PIPE_FORMAT_R16G16B16A16_SNORM:
79 so_data(so, NV50TCL_RT_FORMAT_R16G16B16A16_SNORM);
80 break;
81 case PIPE_FORMAT_R16G16B16A16_UNORM:
82 so_data(so, NV50TCL_RT_FORMAT_R16G16B16A16_UNORM);
83 break;
84 case PIPE_FORMAT_R16G16B16A16_FLOAT:
85 so_data(so, NV50TCL_RT_FORMAT_R16G16B16A16_FLOAT);
86 break;
87 case PIPE_FORMAT_R32G32B32A32_FLOAT:
88 so_data(so, NV50TCL_RT_FORMAT_R32G32B32A32_FLOAT);
89 break;
90 case PIPE_FORMAT_R16G16_SNORM:
91 so_data(so, NV50TCL_RT_FORMAT_R16G16_SNORM);
92 break;
93 case PIPE_FORMAT_R16G16_UNORM:
94 so_data(so, NV50TCL_RT_FORMAT_R16G16_UNORM);
95 break;
96 default:
97 NOUVEAU_ERR("AIIII unknown format %s\n",
98 util_format_name(fb->cbufs[i]->format));
99 so_data(so, NV50TCL_RT_FORMAT_X8R8G8B8_UNORM);
100 break;
101 }
102 so_data(so, nv50_miptree(pt)->
103 level[fb->cbufs[i]->level].tile_mode << 4);
104 so_data(so, 0x00000000);
105
106 so_method(so, tesla, NV50TCL_RT_ARRAY_MODE, 1);
107 so_data (so, 1);
108 }
109
110 if (fb->zsbuf) {
111 struct pipe_resource *pt = fb->zsbuf->texture;
112 struct nouveau_bo *bo = nv50_miptree(pt)->base.bo;
113
114 if (!gw) {
115 w = fb->zsbuf->width;
116 h = fb->zsbuf->height;
117 gw = 1;
118 } else {
119 assert(w == fb->zsbuf->width);
120 assert(h == fb->zsbuf->height);
121 }
122
123 so_method(so, tesla, NV50TCL_ZETA_ADDRESS_HIGH, 5);
124 so_reloc (so, bo, fb->zsbuf->offset, NOUVEAU_BO_VRAM |
125 NOUVEAU_BO_HIGH | NOUVEAU_BO_RDWR, 0, 0);
126 so_reloc (so, bo, fb->zsbuf->offset, NOUVEAU_BO_VRAM |
127 NOUVEAU_BO_LOW | NOUVEAU_BO_RDWR, 0, 0);
128 switch (fb->zsbuf->format) {
129 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
130 so_data(so, NV50TCL_ZETA_FORMAT_S8Z24_UNORM);
131 break;
132 case PIPE_FORMAT_Z24X8_UNORM:
133 so_data(so, NV50TCL_ZETA_FORMAT_X8Z24_UNORM);
134 break;
135 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
136 so_data(so, NV50TCL_ZETA_FORMAT_Z24S8_UNORM);
137 break;
138 case PIPE_FORMAT_Z32_FLOAT:
139 so_data(so, NV50TCL_ZETA_FORMAT_Z32_FLOAT);
140 break;
141 case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED:
142 so_data(so, NV50TCL_ZETA_FORMAT_Z32_FLOAT_X24S8_UNORM);
143 break;
144 case PIPE_FORMAT_Z16_UNORM:
145 so_data(so, NV50TCL_ZETA_FORMAT_Z16_UNORM);
146 break;
147 default:
148 NOUVEAU_ERR("AIIII unknown format %s\n",
149 util_format_name(fb->zsbuf->format));
150 so_data(so, NV50TCL_ZETA_FORMAT_S8Z24_UNORM);
151 break;
152 }
153 so_data(so, nv50_miptree(pt)->
154 level[fb->zsbuf->level].tile_mode << 4);
155 so_data(so, 0x00000000);
156
157 so_method(so, tesla, NV50TCL_ZETA_ENABLE, 1);
158 so_data (so, 1);
159 so_method(so, tesla, NV50TCL_ZETA_HORIZ, 3);
160 so_data (so, fb->zsbuf->width);
161 so_data (so, fb->zsbuf->height);
162 so_data (so, 0x00010001);
163 } else {
164 so_method(so, tesla, NV50TCL_ZETA_ENABLE, 1);
165 so_data (so, 0);
166 }
167
168 so_method(so, tesla, NV50TCL_VIEWPORT_HORIZ(0), 2);
169 so_data (so, w << 16);
170 so_data (so, h << 16);
171 /* set window lower left corner */
172 so_method(so, tesla, NV50TCL_WINDOW_OFFSET_X, 2);
173 so_data (so, 0);
174 so_data (so, 0);
175 /* set screen scissor rectangle */
176 so_method(so, tesla, NV50TCL_SCREEN_SCISSOR_HORIZ, 2);
177 so_data (so, w << 16);
178 so_data (so, h << 16);
179
180 return so;
181 }
182
183 static void
184 nv50_validate_samplers(struct nv50_context *nv50, struct nouveau_stateobj *so,
185 unsigned p)
186 {
187 struct nouveau_grobj *eng2d = nv50->screen->eng2d;
188 unsigned i, j, dw = nv50->sampler_nr[p] * 8;
189
190 if (!dw)
191 return;
192 nv50_so_init_sifc(nv50, so, nv50->screen->tsc, NOUVEAU_BO_VRAM,
193 p * (32 * 8 * 4), dw * 4);
194
195 so_method(so, eng2d, NV50_2D_SIFC_DATA | (2 << 29), dw);
196
197 for (i = 0; i < nv50->sampler_nr[p]; ++i) {
198 if (nv50->sampler[p][i])
199 so_datap(so, nv50->sampler[p][i]->tsc, 8);
200 else {
201 for (j = 0; j < 8; ++j) /* you get punished */
202 so_data(so, 0); /* ... for leaving holes */
203 }
204 }
205 }
206
207 static struct nouveau_stateobj *
208 validate_blend(struct nv50_context *nv50)
209 {
210 struct nouveau_stateobj *so = NULL;
211 so_ref(nv50->blend->so, &so);
212 return so;
213 }
214
215 static struct nouveau_stateobj *
216 validate_zsa(struct nv50_context *nv50)
217 {
218 struct nouveau_stateobj *so = NULL;
219 so_ref(nv50->zsa->so, &so);
220 return so;
221 }
222
223 static struct nouveau_stateobj *
224 validate_rast(struct nv50_context *nv50)
225 {
226 struct nouveau_stateobj *so = NULL;
227 so_ref(nv50->rasterizer->so, &so);
228 return so;
229 }
230
231 static struct nouveau_stateobj *
232 validate_blend_colour(struct nv50_context *nv50)
233 {
234 struct nouveau_grobj *tesla = nv50->screen->tesla;
235 struct nouveau_stateobj *so = so_new(1, 4, 0);
236
237 so_method(so, tesla, NV50TCL_BLEND_COLOR(0), 4);
238 so_data (so, fui(nv50->blend_colour.color[0]));
239 so_data (so, fui(nv50->blend_colour.color[1]));
240 so_data (so, fui(nv50->blend_colour.color[2]));
241 so_data (so, fui(nv50->blend_colour.color[3]));
242 return so;
243 }
244
245 static struct nouveau_stateobj *
246 validate_stencil_ref(struct nv50_context *nv50)
247 {
248 struct nouveau_grobj *tesla = nv50->screen->tesla;
249 struct nouveau_stateobj *so = so_new(2, 2, 0);
250
251 so_method(so, tesla, NV50TCL_STENCIL_FRONT_FUNC_REF, 1);
252 so_data (so, nv50->stencil_ref.ref_value[0]);
253 so_method(so, tesla, NV50TCL_STENCIL_BACK_FUNC_REF, 1);
254 so_data (so, nv50->stencil_ref.ref_value[1]);
255 return so;
256 }
257
258 static struct nouveau_stateobj *
259 validate_stipple(struct nv50_context *nv50)
260 {
261 struct nouveau_grobj *tesla = nv50->screen->tesla;
262 struct nouveau_stateobj *so = so_new(1, 32, 0);
263 int i;
264
265 so_method(so, tesla, NV50TCL_POLYGON_STIPPLE_PATTERN(0), 32);
266 for (i = 0; i < 32; i++)
267 so_data(so, util_bswap32(nv50->stipple.stipple[i]));
268 return so;
269 }
270
271 static struct nouveau_stateobj *
272 validate_scissor(struct nv50_context *nv50)
273 {
274 struct nouveau_grobj *tesla = nv50->screen->tesla;
275 struct pipe_scissor_state *s = &nv50->scissor;
276 struct nouveau_stateobj *so;
277
278 so = so_new(1, 2, 0);
279 so_method(so, tesla, NV50TCL_SCISSOR_HORIZ(0), 2);
280 so_data (so, (s->maxx << 16) | s->minx);
281 so_data (so, (s->maxy << 16) | s->miny);
282 return so;
283 }
284
285 static struct nouveau_stateobj *
286 validate_viewport(struct nv50_context *nv50)
287 {
288 struct nouveau_grobj *tesla = nv50->screen->tesla;
289 struct nouveau_stateobj *so = so_new(3, 7, 0);
290
291 so_method(so, tesla, NV50TCL_VIEWPORT_TRANSLATE_X(0), 3);
292 so_data (so, fui(nv50->viewport.translate[0]));
293 so_data (so, fui(nv50->viewport.translate[1]));
294 so_data (so, fui(nv50->viewport.translate[2]));
295 so_method(so, tesla, NV50TCL_VIEWPORT_SCALE_X(0), 3);
296 so_data (so, fui(nv50->viewport.scale[0]));
297 so_data (so, fui(nv50->viewport.scale[1]));
298 so_data (so, fui(nv50->viewport.scale[2]));
299
300 /* no idea what 0f90 does */
301 so_method(so, tesla, 0x0f90, 1);
302 so_data (so, 0);
303
304 return so;
305 }
306
307 static struct nouveau_stateobj *
308 validate_sampler(struct nv50_context *nv50)
309 {
310 struct nouveau_grobj *tesla = nv50->screen->tesla;
311 struct nouveau_stateobj *so;
312 unsigned nr = 0, i;
313
314 for (i = 0; i < 3; ++i)
315 nr += nv50->sampler_nr[i];
316
317 so = so_new(1 + 5 * 3, 1 + 19 * 3 + nr * 8, 3 * 2);
318
319 nv50_validate_samplers(nv50, so, 0); /* VP */
320 nv50_validate_samplers(nv50, so, 2); /* FP */
321
322 so_method(so, tesla, 0x1334, 1); /* flush TSC */
323 so_data (so, 0);
324
325 return so;
326 }
327
328 static struct nouveau_stateobj *
329 validate_vtxbuf(struct nv50_context *nv50)
330 {
331 struct nouveau_stateobj *so = NULL;
332 so_ref(nv50->state.vtxbuf, &so);
333 return so;
334 }
335
336 static struct nouveau_stateobj *
337 validate_vtxattr(struct nv50_context *nv50)
338 {
339 struct nouveau_stateobj *so = NULL;
340 so_ref(nv50->state.vtxattr, &so);
341 return so;
342 }
343
344 static struct nouveau_stateobj *
345 validate_clip(struct nv50_context *nv50)
346 {
347 struct nouveau_grobj *tesla = nv50->screen->tesla;
348 struct nouveau_stateobj *so = so_new(1, 1, 0);
349 uint32_t vvcc;
350
351 /* 0x0000 = remove whole primitive only (xyz)
352 * 0x1018 = remove whole primitive only (xy), clamp z
353 * 0x1080 = clip primitive (xyz)
354 * 0x1098 = clip primitive (xy), clamp z
355 */
356 vvcc = nv50->clip.depth_clamp ? 0x1098 : 0x1080;
357
358 so_method(so, tesla, NV50TCL_VIEW_VOLUME_CLIP_CTRL, 1);
359 so_data (so, vvcc);
360
361 return so;
362 }
363
364 struct state_validate {
365 struct nouveau_stateobj *(*func)(struct nv50_context *nv50);
366 unsigned states;
367 } validate_list[] = {
368 { validate_fb , NV50_NEW_FRAMEBUFFER },
369 { validate_blend , NV50_NEW_BLEND },
370 { validate_zsa , NV50_NEW_ZSA },
371 { nv50_vertprog_validate , NV50_NEW_VERTPROG | NV50_NEW_VERTPROG_CB },
372 { nv50_fragprog_validate , NV50_NEW_FRAGPROG | NV50_NEW_FRAGPROG_CB },
373 { nv50_geomprog_validate , NV50_NEW_GEOMPROG | NV50_NEW_GEOMPROG_CB },
374 { nv50_fp_linkage_validate, NV50_NEW_VERTPROG | NV50_NEW_GEOMPROG |
375 NV50_NEW_FRAGPROG | NV50_NEW_RASTERIZER },
376 { nv50_gp_linkage_validate, NV50_NEW_VERTPROG | NV50_NEW_GEOMPROG },
377 { validate_rast , NV50_NEW_RASTERIZER },
378 { validate_blend_colour , NV50_NEW_BLEND_COLOUR },
379 { validate_stencil_ref , NV50_NEW_STENCIL_REF },
380 { validate_stipple , NV50_NEW_STIPPLE },
381 { validate_scissor , NV50_NEW_SCISSOR },
382 { validate_viewport , NV50_NEW_VIEWPORT },
383 { validate_sampler , NV50_NEW_SAMPLER },
384 { nv50_tex_validate , NV50_NEW_TEXTURE | NV50_NEW_SAMPLER },
385 { nv50_vbo_validate , NV50_NEW_ARRAYS },
386 { validate_vtxbuf , NV50_NEW_ARRAYS },
387 { validate_vtxattr , NV50_NEW_ARRAYS },
388 { validate_clip , NV50_NEW_CLIP },
389 {}
390 };
391 #define validate_list_len (sizeof(validate_list) / sizeof(validate_list[0]))
392
393 boolean
394 nv50_state_validate(struct nv50_context *nv50, unsigned wait_dwords)
395 {
396 struct nouveau_channel *chan = nv50->screen->base.channel;
397 struct nouveau_grobj *tesla = nv50->screen->tesla;
398 unsigned nr_relocs = 128, nr_dwords = wait_dwords + 128 + 4;
399 int ret, i;
400
401 for (i = 0; i < validate_list_len; i++) {
402 struct state_validate *validate = &validate_list[i];
403 struct nouveau_stateobj *so;
404
405 if (!(nv50->dirty & validate->states))
406 continue;
407
408 so = validate->func(nv50);
409 if (!so)
410 continue;
411
412 nr_dwords += (so->total + so->cur);
413 nr_relocs += so->cur_reloc;
414
415 so_ref(so, &nv50->state.hw[i]);
416 so_ref(NULL, &so);
417 nv50->state.hw_dirty |= (1 << i);
418 }
419 nv50->dirty = 0;
420
421 if (nv50->screen->cur_ctx != nv50) {
422 for (i = 0; i < validate_list_len; i++) {
423 if (!nv50->state.hw[i] ||
424 (nv50->state.hw_dirty & (1 << i)))
425 continue;
426
427 nr_dwords += (nv50->state.hw[i]->total +
428 nv50->state.hw[i]->cur);
429 nr_relocs += nv50->state.hw[i]->cur_reloc;
430 nv50->state.hw_dirty |= (1 << i);
431 }
432
433 nv50->screen->cur_ctx = nv50;
434 }
435
436 ret = MARK_RING(chan, nr_dwords, nr_relocs);
437 if (ret) {
438 debug_printf("MARK_RING(%d, %d) failed: %d\n",
439 nr_dwords, nr_relocs, ret);
440 return FALSE;
441 }
442
443 while (nv50->state.hw_dirty) {
444 i = ffs(nv50->state.hw_dirty) - 1;
445 nv50->state.hw_dirty &= ~(1 << i);
446
447 so_emit(chan, nv50->state.hw[i]);
448 }
449
450 /* Yes, really, we need to do this. If a buffer that is referenced
451 * on the hardware isn't part of changed state above, without doing
452 * this the kernel is given no clue that the buffer is being used
453 * still. This can cause all sorts of fun issues.
454 */
455 nv50_tex_relocs(nv50);
456 so_emit_reloc_markers(chan, nv50->state.hw[0]); /* fb */
457 so_emit_reloc_markers(chan, nv50->state.hw[3]); /* vp */
458 so_emit_reloc_markers(chan, nv50->state.hw[4]); /* fp */
459 so_emit_reloc_markers(chan, nv50->state.hw[17]); /* vb */
460 nv50_screen_relocs(nv50->screen);
461
462 /* No idea.. */
463 BEGIN_RING(chan, tesla, 0x142c, 1);
464 OUT_RING (chan, 0);
465 BEGIN_RING(chan, tesla, 0x142c, 1);
466 OUT_RING (chan, 0);
467 return TRUE;
468 }
469
470 void nv50_so_init_sifc(struct nv50_context *nv50,
471 struct nouveau_stateobj *so,
472 struct nouveau_bo *bo, unsigned reloc,
473 unsigned offset, unsigned size)
474 {
475 struct nouveau_grobj *eng2d = nv50->screen->eng2d;
476
477 reloc |= NOUVEAU_BO_WR;
478
479 so_method(so, eng2d, NV50_2D_DST_FORMAT, 2);
480 so_data (so, NV50_2D_DST_FORMAT_R8_UNORM);
481 so_data (so, 1);
482 so_method(so, eng2d, NV50_2D_DST_PITCH, 5);
483 so_data (so, 262144);
484 so_data (so, 65536);
485 so_data (so, 1);
486 so_reloc (so, bo, offset, reloc | NOUVEAU_BO_HIGH, 0, 0);
487 so_reloc (so, bo, offset, reloc | NOUVEAU_BO_LOW, 0, 0);
488 so_method(so, eng2d, NV50_2D_SIFC_BITMAP_ENABLE, 2);
489 so_data (so, 0);
490 so_data (so, NV50_2D_SIFC_FORMAT_R8_UNORM);
491 so_method(so, eng2d, NV50_2D_SIFC_WIDTH, 10);
492 so_data (so, size);
493 so_data (so, 1);
494 so_data (so, 0);
495 so_data (so, 1);
496 so_data (so, 0);
497 so_data (so, 1);
498 so_data (so, 0);
499 so_data (so, 0);
500 so_data (so, 0);
501 so_data (so, 0);
502 }