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