nv: Use util_format_name().
[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 "nouveau/nouveau_stateobj.h"
27
28 #define NV50_CBUF_FORMAT_CASE(n) \
29 case PIPE_FORMAT_##n: so_data(so, NV50TCL_RT_FORMAT_##n); break
30
31 #define NV50_ZETA_FORMAT_CASE(n) \
32 case PIPE_FORMAT_##n: so_data(so, NV50TCL_ZETA_FORMAT_##n); break
33
34 static void
35 nv50_state_validate_fb(struct nv50_context *nv50)
36 {
37 struct nouveau_grobj *tesla = nv50->screen->tesla;
38 struct nouveau_stateobj *so = so_new(32, 79, 18);
39 struct pipe_framebuffer_state *fb = &nv50->framebuffer;
40 unsigned i, w, h, gw = 0;
41
42 /* Set nr of active RTs and select RT for each colour output.
43 * FP result 0 always goes to RT[0], bits 4 - 6 are ignored.
44 * Ambiguous assignment results in no rendering (no DATA_ERROR).
45 */
46 so_method(so, tesla, NV50TCL_RT_CONTROL, 1);
47 so_data (so, fb->nr_cbufs |
48 (0 << 4) | (1 << 7) | (2 << 10) | (3 << 13) |
49 (4 << 16) | (5 << 19) | (6 << 22) | (7 << 25));
50
51 for (i = 0; i < fb->nr_cbufs; i++) {
52 struct pipe_texture *pt = fb->cbufs[i]->texture;
53 struct nouveau_bo *bo = nv50_miptree(pt)->base.bo;
54
55 if (!gw) {
56 w = fb->cbufs[i]->width;
57 h = fb->cbufs[i]->height;
58 gw = 1;
59 } else {
60 assert(w == fb->cbufs[i]->width);
61 assert(h == fb->cbufs[i]->height);
62 }
63
64 so_method(so, tesla, NV50TCL_RT_HORIZ(i), 2);
65 so_data (so, fb->cbufs[i]->width);
66 so_data (so, fb->cbufs[i]->height);
67
68 so_method(so, tesla, NV50TCL_RT_ADDRESS_HIGH(i), 5);
69 so_reloc (so, bo, fb->cbufs[i]->offset, NOUVEAU_BO_VRAM |
70 NOUVEAU_BO_HIGH | NOUVEAU_BO_RDWR, 0, 0);
71 so_reloc (so, bo, fb->cbufs[i]->offset, NOUVEAU_BO_VRAM |
72 NOUVEAU_BO_LOW | NOUVEAU_BO_RDWR, 0, 0);
73 switch (fb->cbufs[i]->format) {
74 NV50_CBUF_FORMAT_CASE(A8R8G8B8_UNORM);
75 NV50_CBUF_FORMAT_CASE(X8R8G8B8_UNORM);
76 NV50_CBUF_FORMAT_CASE(R5G6B5_UNORM);
77 NV50_CBUF_FORMAT_CASE(R16G16B16A16_SNORM);
78 NV50_CBUF_FORMAT_CASE(R16G16B16A16_UNORM);
79 NV50_CBUF_FORMAT_CASE(R32G32B32A32_FLOAT);
80 NV50_CBUF_FORMAT_CASE(R16G16_SNORM);
81 NV50_CBUF_FORMAT_CASE(R16G16_UNORM);
82 default:
83 NOUVEAU_ERR("AIIII unknown format %s\n",
84 util_format_name(fb->cbufs[i]->format));
85 so_data(so, NV50TCL_RT_FORMAT_X8R8G8B8_UNORM);
86 break;
87 }
88 so_data(so, nv50_miptree(pt)->
89 level[fb->cbufs[i]->level].tile_mode << 4);
90 so_data(so, 0x00000000);
91
92 so_method(so, tesla, NV50TCL_RT_ARRAY_MODE, 1);
93 so_data (so, 1);
94 }
95
96 if (fb->zsbuf) {
97 struct pipe_texture *pt = fb->zsbuf->texture;
98 struct nouveau_bo *bo = nv50_miptree(pt)->base.bo;
99
100 if (!gw) {
101 w = fb->zsbuf->width;
102 h = fb->zsbuf->height;
103 gw = 1;
104 } else {
105 assert(w == fb->zsbuf->width);
106 assert(h == fb->zsbuf->height);
107 }
108
109 so_method(so, tesla, NV50TCL_ZETA_ADDRESS_HIGH, 5);
110 so_reloc (so, bo, fb->zsbuf->offset, NOUVEAU_BO_VRAM |
111 NOUVEAU_BO_HIGH | NOUVEAU_BO_RDWR, 0, 0);
112 so_reloc (so, bo, fb->zsbuf->offset, NOUVEAU_BO_VRAM |
113 NOUVEAU_BO_LOW | NOUVEAU_BO_RDWR, 0, 0);
114 switch (fb->zsbuf->format) {
115 NV50_ZETA_FORMAT_CASE(S8Z24_UNORM);
116 NV50_ZETA_FORMAT_CASE(X8Z24_UNORM);
117 NV50_ZETA_FORMAT_CASE(Z24S8_UNORM);
118 NV50_ZETA_FORMAT_CASE(Z32_FLOAT);
119 default:
120 NOUVEAU_ERR("AIIII unknown format %s\n",
121 util_format_name(fb->zsbuf->format));
122 so_data(so, NV50TCL_ZETA_FORMAT_S8Z24_UNORM);
123 break;
124 }
125 so_data(so, nv50_miptree(pt)->
126 level[fb->zsbuf->level].tile_mode << 4);
127 so_data(so, 0x00000000);
128
129 so_method(so, tesla, NV50TCL_ZETA_ENABLE, 1);
130 so_data (so, 1);
131 so_method(so, tesla, NV50TCL_ZETA_HORIZ, 3);
132 so_data (so, fb->zsbuf->width);
133 so_data (so, fb->zsbuf->height);
134 so_data (so, 0x00010001);
135 } else {
136 so_method(so, tesla, NV50TCL_ZETA_ENABLE, 1);
137 so_data (so, 0);
138 }
139
140 so_method(so, tesla, NV50TCL_VIEWPORT_HORIZ(0), 2);
141 so_data (so, w << 16);
142 so_data (so, h << 16);
143 /* set window lower left corner */
144 so_method(so, tesla, NV50TCL_WINDOW_OFFSET_X, 2);
145 so_data (so, 0);
146 so_data (so, 0);
147 /* set screen scissor rectangle */
148 so_method(so, tesla, NV50TCL_SCREEN_SCISSOR_HORIZ, 2);
149 so_data (so, w << 16);
150 so_data (so, h << 16);
151
152 /* we set scissors to framebuffer size when they're 'turned off' */
153 nv50->dirty |= NV50_NEW_SCISSOR;
154 so_ref(NULL, &nv50->state.scissor);
155
156 so_ref(so, &nv50->state.fb);
157 so_ref(NULL, &so);
158 }
159
160 static void
161 nv50_validate_samplers(struct nv50_context *nv50, struct nouveau_stateobj *so,
162 unsigned p)
163 {
164 struct nouveau_grobj *eng2d = nv50->screen->eng2d;
165 unsigned i, j, dw = nv50->sampler_nr[p] * 8;
166
167 if (!dw)
168 return;
169 nv50_so_init_sifc(nv50, so, nv50->screen->tsc, NOUVEAU_BO_VRAM,
170 p * (32 * 8 * 4), dw * 4);
171
172 so_method(so, eng2d, NV50_2D_SIFC_DATA | (2 << 29), dw);
173
174 for (i = 0; i < nv50->sampler_nr[p]; ++i) {
175 if (nv50->sampler[p][i])
176 so_datap(so, nv50->sampler[p][i]->tsc, 8);
177 else {
178 for (j = 0; j < 8; ++j) /* you get punished */
179 so_data(so, 0); /* ... for leaving holes */
180 }
181 }
182 }
183
184 static void
185 nv50_state_emit(struct nv50_context *nv50)
186 {
187 struct nv50_screen *screen = nv50->screen;
188 struct nouveau_channel *chan = screen->base.channel;
189
190 /* XXX: this is racy for multiple contexts active on separate
191 * threads.
192 */
193 if (screen->cur_ctx != nv50) {
194 if (nv50->state.fb)
195 nv50->state.dirty |= NV50_NEW_FRAMEBUFFER;
196 if (nv50->state.blend)
197 nv50->state.dirty |= NV50_NEW_BLEND;
198 if (nv50->state.zsa)
199 nv50->state.dirty |= NV50_NEW_ZSA;
200 if (nv50->state.vertprog)
201 nv50->state.dirty |= NV50_NEW_VERTPROG;
202 if (nv50->state.fragprog)
203 nv50->state.dirty |= NV50_NEW_FRAGPROG;
204 if (nv50->state.geomprog)
205 nv50->state.dirty |= NV50_NEW_GEOMPROG;
206 if (nv50->state.rast)
207 nv50->state.dirty |= NV50_NEW_RASTERIZER;
208 if (nv50->state.blend_colour)
209 nv50->state.dirty |= NV50_NEW_BLEND_COLOUR;
210 if (nv50->state.stencil_ref)
211 nv50->state.dirty |= NV50_NEW_STENCIL_REF;
212 if (nv50->state.stipple)
213 nv50->state.dirty |= NV50_NEW_STIPPLE;
214 if (nv50->state.scissor)
215 nv50->state.dirty |= NV50_NEW_SCISSOR;
216 if (nv50->state.viewport)
217 nv50->state.dirty |= NV50_NEW_VIEWPORT;
218 if (nv50->state.tsc_upload)
219 nv50->state.dirty |= NV50_NEW_SAMPLER;
220 if (nv50->state.tic_upload)
221 nv50->state.dirty |= NV50_NEW_TEXTURE;
222 if (nv50->state.vtxfmt && nv50->state.vtxbuf)
223 nv50->state.dirty |= NV50_NEW_ARRAYS;
224 screen->cur_ctx = nv50;
225 }
226
227 if (nv50->state.dirty & NV50_NEW_FRAMEBUFFER)
228 so_emit(chan, nv50->state.fb);
229 if (nv50->state.dirty & NV50_NEW_BLEND)
230 so_emit(chan, nv50->state.blend);
231 if (nv50->state.dirty & NV50_NEW_ZSA)
232 so_emit(chan, nv50->state.zsa);
233 if (nv50->state.dirty & NV50_NEW_VERTPROG)
234 so_emit(chan, nv50->state.vertprog);
235 if (nv50->state.dirty & NV50_NEW_FRAGPROG)
236 so_emit(chan, nv50->state.fragprog);
237 if (nv50->state.dirty & NV50_NEW_GEOMPROG && nv50->state.geomprog)
238 so_emit(chan, nv50->state.geomprog);
239 if (nv50->state.dirty & (NV50_NEW_FRAGPROG | NV50_NEW_VERTPROG |
240 NV50_NEW_GEOMPROG | NV50_NEW_RASTERIZER))
241 so_emit(chan, nv50->state.fp_linkage);
242 if ((nv50->state.dirty & (NV50_NEW_VERTPROG | NV50_NEW_GEOMPROG))
243 && nv50->state.gp_linkage)
244 so_emit(chan, nv50->state.gp_linkage);
245 if (nv50->state.dirty & NV50_NEW_RASTERIZER)
246 so_emit(chan, nv50->state.rast);
247 if (nv50->state.dirty & NV50_NEW_BLEND_COLOUR)
248 so_emit(chan, nv50->state.blend_colour);
249 if (nv50->state.dirty & NV50_NEW_STENCIL_REF)
250 so_emit(chan, nv50->state.stencil_ref);
251 if (nv50->state.dirty & NV50_NEW_STIPPLE)
252 so_emit(chan, nv50->state.stipple);
253 if (nv50->state.dirty & NV50_NEW_SCISSOR)
254 so_emit(chan, nv50->state.scissor);
255 if (nv50->state.dirty & NV50_NEW_VIEWPORT)
256 so_emit(chan, nv50->state.viewport);
257 if (nv50->state.dirty & NV50_NEW_SAMPLER)
258 so_emit(chan, nv50->state.tsc_upload);
259 if (nv50->state.dirty & NV50_NEW_TEXTURE)
260 so_emit(chan, nv50->state.tic_upload);
261 if (nv50->state.dirty & NV50_NEW_ARRAYS) {
262 so_emit(chan, nv50->state.vtxfmt);
263 so_emit(chan, nv50->state.vtxbuf);
264 if (nv50->state.vtxattr)
265 so_emit(chan, nv50->state.vtxattr);
266 }
267 nv50->state.dirty = 0;
268 }
269
270 void
271 nv50_state_flush_notify(struct nouveau_channel *chan)
272 {
273 struct nv50_context *nv50 = chan->user_private;
274
275 if (nv50->state.tic_upload && !(nv50->dirty & NV50_NEW_TEXTURE))
276 so_emit(chan, nv50->state.tic_upload);
277
278 so_emit_reloc_markers(chan, nv50->state.fb);
279 so_emit_reloc_markers(chan, nv50->state.vertprog);
280 so_emit_reloc_markers(chan, nv50->state.fragprog);
281 so_emit_reloc_markers(chan, nv50->state.vtxbuf);
282 so_emit_reloc_markers(chan, nv50->screen->static_init);
283
284 if (nv50->state.instbuf)
285 so_emit_reloc_markers(chan, nv50->state.instbuf);
286 }
287
288 boolean
289 nv50_state_validate(struct nv50_context *nv50)
290 {
291 struct nouveau_grobj *tesla = nv50->screen->tesla;
292 struct nouveau_stateobj *so;
293 unsigned i;
294
295 if (nv50->dirty & NV50_NEW_FRAMEBUFFER)
296 nv50_state_validate_fb(nv50);
297
298 if (nv50->dirty & NV50_NEW_BLEND)
299 so_ref(nv50->blend->so, &nv50->state.blend);
300
301 if (nv50->dirty & NV50_NEW_ZSA)
302 so_ref(nv50->zsa->so, &nv50->state.zsa);
303
304 if (nv50->dirty & (NV50_NEW_VERTPROG | NV50_NEW_VERTPROG_CB))
305 nv50_vertprog_validate(nv50);
306
307 if (nv50->dirty & (NV50_NEW_FRAGPROG | NV50_NEW_FRAGPROG_CB))
308 nv50_fragprog_validate(nv50);
309
310 if (nv50->dirty & (NV50_NEW_GEOMPROG | NV50_NEW_GEOMPROG_CB))
311 nv50_geomprog_validate(nv50);
312
313 if (nv50->dirty & (NV50_NEW_FRAGPROG | NV50_NEW_VERTPROG |
314 NV50_NEW_GEOMPROG | NV50_NEW_RASTERIZER))
315 nv50_fp_linkage_validate(nv50);
316
317 if (nv50->dirty & (NV50_NEW_GEOMPROG | NV50_NEW_VERTPROG))
318 nv50_gp_linkage_validate(nv50);
319
320 if (nv50->dirty & NV50_NEW_RASTERIZER)
321 so_ref(nv50->rasterizer->so, &nv50->state.rast);
322
323 if (nv50->dirty & NV50_NEW_BLEND_COLOUR) {
324 so = so_new(1, 4, 0);
325 so_method(so, tesla, NV50TCL_BLEND_COLOR(0), 4);
326 so_data (so, fui(nv50->blend_colour.color[0]));
327 so_data (so, fui(nv50->blend_colour.color[1]));
328 so_data (so, fui(nv50->blend_colour.color[2]));
329 so_data (so, fui(nv50->blend_colour.color[3]));
330 so_ref(so, &nv50->state.blend_colour);
331 so_ref(NULL, &so);
332 }
333
334 if (nv50->dirty & NV50_NEW_STENCIL_REF) {
335 so = so_new(2, 2, 0);
336 so_method(so, tesla, NV50TCL_STENCIL_FRONT_FUNC_REF, 1);
337 so_data (so, nv50->stencil_ref.ref_value[0]);
338 so_method(so, tesla, NV50TCL_STENCIL_BACK_FUNC_REF, 1);
339 so_data (so, nv50->stencil_ref.ref_value[1]);
340 so_ref(so, &nv50->state.stencil_ref);
341 so_ref(NULL, &so);
342 }
343
344 if (nv50->dirty & NV50_NEW_STIPPLE) {
345 so = so_new(1, 32, 0);
346 so_method(so, tesla, NV50TCL_POLYGON_STIPPLE_PATTERN(0), 32);
347 for (i = 0; i < 32; i++)
348 so_data(so, util_bswap32(nv50->stipple.stipple[i]));
349 so_ref(so, &nv50->state.stipple);
350 so_ref(NULL, &so);
351 }
352
353 if (nv50->dirty & (NV50_NEW_SCISSOR | NV50_NEW_RASTERIZER)) {
354 struct pipe_rasterizer_state *rast = &nv50->rasterizer->pipe;
355 struct pipe_scissor_state *s = &nv50->scissor;
356
357 if (nv50->state.scissor &&
358 (rast->scissor == 0 && nv50->state.scissor_enabled == 0))
359 goto scissor_uptodate;
360 nv50->state.scissor_enabled = rast->scissor;
361
362 so = so_new(1, 2, 0);
363 so_method(so, tesla, NV50TCL_SCISSOR_HORIZ(0), 2);
364 if (nv50->state.scissor_enabled) {
365 so_data(so, (s->maxx << 16) | s->minx);
366 so_data(so, (s->maxy << 16) | s->miny);
367 } else {
368 so_data(so, (nv50->framebuffer.width << 16));
369 so_data(so, (nv50->framebuffer.height << 16));
370 }
371 so_ref(so, &nv50->state.scissor);
372 so_ref(NULL, &so);
373 nv50->state.dirty |= NV50_NEW_SCISSOR;
374 }
375 scissor_uptodate:
376
377 if (nv50->dirty & (NV50_NEW_VIEWPORT | NV50_NEW_RASTERIZER)) {
378 unsigned bypass;
379
380 if (!nv50->rasterizer->pipe.bypass_vs_clip_and_viewport)
381 bypass = 0;
382 else
383 bypass = 1;
384
385 if (nv50->state.viewport &&
386 (bypass || !(nv50->dirty & NV50_NEW_VIEWPORT)) &&
387 nv50->state.viewport_bypass == bypass)
388 goto viewport_uptodate;
389 nv50->state.viewport_bypass = bypass;
390
391 so = so_new(5, 9, 0);
392 if (!bypass) {
393 so_method(so, tesla, NV50TCL_VIEWPORT_TRANSLATE_X(0), 3);
394 so_data (so, fui(nv50->viewport.translate[0]));
395 so_data (so, fui(nv50->viewport.translate[1]));
396 so_data (so, fui(nv50->viewport.translate[2]));
397 so_method(so, tesla, NV50TCL_VIEWPORT_SCALE_X(0), 3);
398 so_data (so, fui(nv50->viewport.scale[0]));
399 so_data (so, fui(nv50->viewport.scale[1]));
400 so_data (so, fui(nv50->viewport.scale[2]));
401
402 so_method(so, tesla, NV50TCL_VIEWPORT_TRANSFORM_EN, 1);
403 so_data (so, 1);
404 /* 0x0000 = remove whole primitive only (xyz)
405 * 0x1018 = remove whole primitive only (xy), clamp z
406 * 0x1080 = clip primitive (xyz)
407 * 0x1098 = clip primitive (xy), clamp z
408 */
409 so_method(so, tesla, NV50TCL_VIEW_VOLUME_CLIP_CTRL, 1);
410 so_data (so, 0x1080);
411 /* no idea what 0f90 does */
412 so_method(so, tesla, 0x0f90, 1);
413 so_data (so, 0);
414 } else {
415 so_method(so, tesla, NV50TCL_VIEWPORT_TRANSFORM_EN, 1);
416 so_data (so, 0);
417 so_method(so, tesla, NV50TCL_VIEW_VOLUME_CLIP_CTRL, 1);
418 so_data (so, 0x0000);
419 so_method(so, tesla, 0x0f90, 1);
420 so_data (so, 1);
421 }
422
423 so_ref(so, &nv50->state.viewport);
424 so_ref(NULL, &so);
425 nv50->state.dirty |= NV50_NEW_VIEWPORT;
426 }
427 viewport_uptodate:
428
429 if (nv50->dirty & NV50_NEW_SAMPLER) {
430 unsigned nr = 0;
431
432 for (i = 0; i < PIPE_SHADER_TYPES; ++i)
433 nr += nv50->sampler_nr[i];
434
435 so = so_new(1 + 5 * PIPE_SHADER_TYPES,
436 1 + 19 * PIPE_SHADER_TYPES + nr * 8,
437 PIPE_SHADER_TYPES * 2);
438
439 nv50_validate_samplers(nv50, so, PIPE_SHADER_VERTEX);
440 nv50_validate_samplers(nv50, so, PIPE_SHADER_FRAGMENT);
441
442 so_method(so, tesla, 0x1334, 1); /* flush TSC */
443 so_data (so, 0);
444
445 so_ref(so, &nv50->state.tsc_upload);
446 so_ref(NULL, &so);
447 }
448
449 if (nv50->dirty & (NV50_NEW_TEXTURE | NV50_NEW_SAMPLER))
450 nv50_tex_validate(nv50);
451
452 if (nv50->dirty & NV50_NEW_ARRAYS)
453 nv50_vbo_validate(nv50);
454
455 nv50->state.dirty |= nv50->dirty;
456 nv50->dirty = 0;
457 nv50_state_emit(nv50);
458
459 return TRUE;
460 }
461
462 void nv50_so_init_sifc(struct nv50_context *nv50,
463 struct nouveau_stateobj *so,
464 struct nouveau_bo *bo, unsigned reloc,
465 unsigned offset, unsigned size)
466 {
467 struct nouveau_grobj *eng2d = nv50->screen->eng2d;
468
469 reloc |= NOUVEAU_BO_WR;
470
471 so_method(so, eng2d, NV50_2D_DST_FORMAT, 2);
472 so_data (so, NV50_2D_DST_FORMAT_R8_UNORM);
473 so_data (so, 1);
474 so_method(so, eng2d, NV50_2D_DST_PITCH, 5);
475 so_data (so, 262144);
476 so_data (so, 65536);
477 so_data (so, 1);
478 so_reloc (so, bo, offset, reloc | NOUVEAU_BO_HIGH, 0, 0);
479 so_reloc (so, bo, offset, reloc | NOUVEAU_BO_LOW, 0, 0);
480 so_method(so, eng2d, NV50_2D_SIFC_BITMAP_ENABLE, 2);
481 so_data (so, 0);
482 so_data (so, NV50_2D_SIFC_FORMAT_R8_UNORM);
483 so_method(so, eng2d, NV50_2D_SIFC_WIDTH, 10);
484 so_data (so, size);
485 so_data (so, 1);
486 so_data (so, 0);
487 so_data (so, 1);
488 so_data (so, 0);
489 so_data (so, 1);
490 so_data (so, 0);
491 so_data (so, 0);
492 so_data (so, 0);
493 so_data (so, 0);
494 }