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