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