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_R5G6B5_UNORM:
61 so_data(so, NV50TCL_RT_FORMAT_R5G6B5_UNORM);
62 break;
63 default:
64 NOUVEAU_ERR("AIIII unknown format %s\n",
65 pf_name(fb->cbufs[i]->format));
66 so_data(so, NV50TCL_RT_FORMAT_X8R8G8B8_UNORM);
67 break;
68 }
69 so_data(so, nv50_miptree(pt)->
70 level[fb->cbufs[i]->level].tile_mode << 4);
71 so_data(so, 0x00000000);
72
73 so_method(so, tesla, 0x1224, 1);
74 so_data (so, 1);
75 }
76
77 if (fb->zsbuf) {
78 struct pipe_texture *pt = fb->zsbuf->texture;
79 struct nouveau_bo *bo = nv50_miptree(pt)->base.bo;
80
81 if (!gw) {
82 w = fb->zsbuf->width;
83 h = fb->zsbuf->height;
84 gw = 1;
85 } else {
86 assert(w == fb->zsbuf->width);
87 assert(h == fb->zsbuf->height);
88 }
89
90 so_method(so, tesla, NV50TCL_ZETA_ADDRESS_HIGH, 5);
91 so_reloc (so, bo, fb->zsbuf->offset, NOUVEAU_BO_VRAM |
92 NOUVEAU_BO_HIGH | NOUVEAU_BO_RDWR, 0, 0);
93 so_reloc (so, bo, fb->zsbuf->offset, NOUVEAU_BO_VRAM |
94 NOUVEAU_BO_LOW | NOUVEAU_BO_RDWR, 0, 0);
95 switch (fb->zsbuf->format) {
96 case PIPE_FORMAT_Z32_FLOAT:
97 so_data(so, NV50TCL_ZETA_FORMAT_Z32_FLOAT);
98 break;
99 case PIPE_FORMAT_Z24S8_UNORM:
100 so_data(so, NV50TCL_ZETA_FORMAT_Z24S8_UNORM);
101 break;
102 case PIPE_FORMAT_X8Z24_UNORM:
103 so_data(so, NV50TCL_ZETA_FORMAT_X8Z24_UNORM);
104 break;
105 case PIPE_FORMAT_S8Z24_UNORM:
106 so_data(so, NV50TCL_ZETA_FORMAT_S8Z24_UNORM);
107 break;
108 default:
109 NOUVEAU_ERR("AIIII unknown format %s\n",
110 pf_name(fb->zsbuf->format));
111 so_data(so, NV50TCL_ZETA_FORMAT_S8Z24_UNORM);
112 break;
113 }
114 so_data(so, nv50_miptree(pt)->
115 level[fb->zsbuf->level].tile_mode << 4);
116 so_data(so, 0x00000000);
117
118 so_method(so, tesla, 0x1538, 1);
119 so_data (so, 1);
120 so_method(so, tesla, NV50TCL_ZETA_HORIZ, 3);
121 so_data (so, fb->zsbuf->width);
122 so_data (so, fb->zsbuf->height);
123 so_data (so, 0x00010001);
124 }
125
126 so_method(so, tesla, NV50TCL_VIEWPORT_HORIZ, 2);
127 so_data (so, w << 16);
128 so_data (so, h << 16);
129 /* set window lower left corner */
130 so_method(so, tesla, NV50TCL_WINDOW_LEFT, 2);
131 so_data (so, 0);
132 so_data (so, 0);
133 /* set screen scissor rectangle */
134 so_method(so, tesla, NV50TCL_SCREEN_SCISSOR_HORIZ, 2);
135 so_data (so, w << 16);
136 so_data (so, h << 16);
137
138 /* we set scissors to framebuffer size when they're 'turned off' */
139 nv50->dirty |= NV50_NEW_SCISSOR;
140 so_ref(NULL, &nv50->state.scissor);
141
142 so_ref(so, &nv50->state.fb);
143 so_ref(NULL, &so);
144 }
145
146 static void
147 nv50_state_emit(struct nv50_context *nv50)
148 {
149 struct nv50_screen *screen = nv50->screen;
150 struct nouveau_channel *chan = screen->base.channel;
151
152 if (nv50->pctx_id != screen->cur_pctx) {
153 if (nv50->state.fb)
154 nv50->state.dirty |= NV50_NEW_FRAMEBUFFER;
155 if (nv50->state.blend)
156 nv50->state.dirty |= NV50_NEW_BLEND;
157 if (nv50->state.zsa)
158 nv50->state.dirty |= NV50_NEW_ZSA;
159 if (nv50->state.vertprog)
160 nv50->state.dirty |= NV50_NEW_VERTPROG;
161 if (nv50->state.fragprog)
162 nv50->state.dirty |= NV50_NEW_FRAGPROG;
163 if (nv50->state.rast)
164 nv50->state.dirty |= NV50_NEW_RASTERIZER;
165 if (nv50->state.blend_colour)
166 nv50->state.dirty |= NV50_NEW_BLEND_COLOUR;
167 if (nv50->state.stipple)
168 nv50->state.dirty |= NV50_NEW_STIPPLE;
169 if (nv50->state.scissor)
170 nv50->state.dirty |= NV50_NEW_SCISSOR;
171 if (nv50->state.viewport)
172 nv50->state.dirty |= NV50_NEW_VIEWPORT;
173 if (nv50->state.tsc_upload)
174 nv50->state.dirty |= NV50_NEW_SAMPLER;
175 if (nv50->state.tic_upload)
176 nv50->state.dirty |= NV50_NEW_TEXTURE;
177 if (nv50->state.vtxfmt && nv50->state.vtxbuf)
178 nv50->state.dirty |= NV50_NEW_ARRAYS;
179 screen->cur_pctx = nv50->pctx_id;
180 }
181
182 if (nv50->state.dirty & NV50_NEW_FRAMEBUFFER)
183 so_emit(chan, nv50->state.fb);
184 if (nv50->state.dirty & NV50_NEW_BLEND)
185 so_emit(chan, nv50->state.blend);
186 if (nv50->state.dirty & NV50_NEW_ZSA)
187 so_emit(chan, nv50->state.zsa);
188 if (nv50->state.dirty & NV50_NEW_VERTPROG)
189 so_emit(chan, nv50->state.vertprog);
190 if (nv50->state.dirty & NV50_NEW_FRAGPROG)
191 so_emit(chan, nv50->state.fragprog);
192 if (nv50->state.dirty & (NV50_NEW_FRAGPROG | NV50_NEW_VERTPROG))
193 so_emit(chan, nv50->state.programs);
194 if (nv50->state.dirty & NV50_NEW_RASTERIZER)
195 so_emit(chan, nv50->state.rast);
196 if (nv50->state.dirty & NV50_NEW_BLEND_COLOUR)
197 so_emit(chan, nv50->state.blend_colour);
198 if (nv50->state.dirty & NV50_NEW_STIPPLE)
199 so_emit(chan, nv50->state.stipple);
200 if (nv50->state.dirty & NV50_NEW_SCISSOR)
201 so_emit(chan, nv50->state.scissor);
202 if (nv50->state.dirty & NV50_NEW_VIEWPORT)
203 so_emit(chan, nv50->state.viewport);
204 if (nv50->state.dirty & NV50_NEW_SAMPLER)
205 so_emit(chan, nv50->state.tsc_upload);
206 if (nv50->state.dirty & NV50_NEW_TEXTURE)
207 so_emit(chan, nv50->state.tic_upload);
208 if (nv50->state.dirty & NV50_NEW_ARRAYS) {
209 so_emit(chan, nv50->state.vtxfmt);
210 so_emit(chan, nv50->state.vtxbuf);
211 if (nv50->state.vtxattr)
212 so_emit(chan, nv50->state.vtxattr);
213 }
214 nv50->state.dirty = 0;
215 }
216
217 void
218 nv50_state_flush_notify(struct nouveau_channel *chan)
219 {
220 struct nv50_context *nv50 = chan->user_private;
221
222 so_emit_reloc_markers(chan, nv50->state.fb);
223 so_emit_reloc_markers(chan, nv50->state.vertprog);
224 so_emit_reloc_markers(chan, nv50->state.fragprog);
225 so_emit_reloc_markers(chan, nv50->state.vtxbuf);
226 so_emit_reloc_markers(chan, nv50->screen->static_init);
227 }
228
229 boolean
230 nv50_state_validate(struct nv50_context *nv50)
231 {
232 struct nouveau_grobj *tesla = nv50->screen->tesla;
233 struct nouveau_stateobj *so;
234 unsigned i;
235
236 if (nv50->dirty & NV50_NEW_FRAMEBUFFER)
237 nv50_state_validate_fb(nv50);
238
239 if (nv50->dirty & NV50_NEW_BLEND)
240 so_ref(nv50->blend->so, &nv50->state.blend);
241
242 if (nv50->dirty & NV50_NEW_ZSA)
243 so_ref(nv50->zsa->so, &nv50->state.zsa);
244
245 if (nv50->dirty & (NV50_NEW_VERTPROG | NV50_NEW_VERTPROG_CB))
246 nv50_vertprog_validate(nv50);
247
248 if (nv50->dirty & (NV50_NEW_FRAGPROG | NV50_NEW_FRAGPROG_CB))
249 nv50_fragprog_validate(nv50);
250
251 if (nv50->dirty & (NV50_NEW_FRAGPROG | NV50_NEW_VERTPROG))
252 nv50_linkage_validate(nv50);
253
254 if (nv50->dirty & NV50_NEW_RASTERIZER)
255 so_ref(nv50->rasterizer->so, &nv50->state.rast);
256
257 if (nv50->dirty & NV50_NEW_BLEND_COLOUR) {
258 so = so_new(5, 0);
259 so_method(so, tesla, NV50TCL_BLEND_COLOR(0), 4);
260 so_data (so, fui(nv50->blend_colour.color[0]));
261 so_data (so, fui(nv50->blend_colour.color[1]));
262 so_data (so, fui(nv50->blend_colour.color[2]));
263 so_data (so, fui(nv50->blend_colour.color[3]));
264 so_ref(so, &nv50->state.blend_colour);
265 so_ref(NULL, &so);
266 }
267
268 if (nv50->dirty & NV50_NEW_STIPPLE) {
269 so = so_new(33, 0);
270 so_method(so, tesla, NV50TCL_POLYGON_STIPPLE_PATTERN(0), 32);
271 for (i = 0; i < 32; i++)
272 so_data(so, nv50->stipple.stipple[i]);
273 so_ref(so, &nv50->state.stipple);
274 so_ref(NULL, &so);
275 }
276
277 if (nv50->dirty & (NV50_NEW_SCISSOR | NV50_NEW_RASTERIZER)) {
278 struct pipe_rasterizer_state *rast = &nv50->rasterizer->pipe;
279 struct pipe_scissor_state *s = &nv50->scissor;
280
281 if (nv50->state.scissor &&
282 (rast->scissor == 0 && nv50->state.scissor_enabled == 0))
283 goto scissor_uptodate;
284 nv50->state.scissor_enabled = rast->scissor;
285
286 so = so_new(3, 0);
287 so_method(so, tesla, NV50TCL_SCISSOR_HORIZ, 2);
288 if (nv50->state.scissor_enabled) {
289 so_data(so, (s->maxx << 16) | s->minx);
290 so_data(so, (s->maxy << 16) | s->miny);
291 } else {
292 so_data(so, (nv50->framebuffer.width << 16));
293 so_data(so, (nv50->framebuffer.height << 16));
294 }
295 so_ref(so, &nv50->state.scissor);
296 so_ref(NULL, &so);
297 nv50->state.dirty |= NV50_NEW_SCISSOR;
298 }
299 scissor_uptodate:
300
301 if (nv50->dirty & (NV50_NEW_VIEWPORT | NV50_NEW_RASTERIZER)) {
302 unsigned bypass;
303
304 if (!nv50->rasterizer->pipe.bypass_vs_clip_and_viewport)
305 bypass = 0;
306 else
307 bypass = 1;
308
309 if (nv50->state.viewport &&
310 (bypass || !(nv50->dirty & NV50_NEW_VIEWPORT)) &&
311 nv50->state.viewport_bypass == bypass)
312 goto viewport_uptodate;
313 nv50->state.viewport_bypass = bypass;
314
315 so = so_new(12, 0);
316 if (!bypass) {
317 so_method(so, tesla, NV50TCL_VIEWPORT_TRANSLATE(0), 3);
318 so_data (so, fui(nv50->viewport.translate[0]));
319 so_data (so, fui(nv50->viewport.translate[1]));
320 so_data (so, fui(nv50->viewport.translate[2]));
321 so_method(so, tesla, NV50TCL_VIEWPORT_SCALE(0), 3);
322 so_data (so, fui(nv50->viewport.scale[0]));
323 so_data (so, fui(nv50->viewport.scale[1]));
324 so_data (so, fui(nv50->viewport.scale[2]));
325
326 so_method(so, tesla, NV50TCL_VIEWPORT_TRANSFORM_EN, 1);
327 so_data (so, 1);
328 /* no idea what 0f90 does */
329 so_method(so, tesla, 0x0f90, 1);
330 so_data (so, 0);
331 } else {
332 so_method(so, tesla, NV50TCL_VIEWPORT_TRANSFORM_EN, 1);
333 so_data (so, 0);
334 so_method(so, tesla, 0x0f90, 1);
335 so_data (so, 1);
336 }
337
338 so_ref(so, &nv50->state.viewport);
339 so_ref(NULL, &so);
340 nv50->state.dirty |= NV50_NEW_VIEWPORT;
341 }
342 viewport_uptodate:
343
344 if (nv50->dirty & NV50_NEW_SAMPLER) {
345 int i;
346
347 so = so_new(nv50->sampler_nr * 8 + 3, 0);
348 so_method(so, tesla, NV50TCL_CB_ADDR, 1);
349 so_data (so, NV50_CB_TSC);
350 so_method(so, tesla, NV50TCL_CB_DATA(0) | 0x40000000,
351 nv50->sampler_nr * 8);
352 for (i = 0; i < nv50->sampler_nr; i++)
353 so_datap (so, nv50->sampler[i]->tsc, 8);
354 so_ref(so, &nv50->state.tsc_upload);
355 so_ref(NULL, &so);
356 }
357
358 if (nv50->dirty & (NV50_NEW_TEXTURE | NV50_NEW_SAMPLER))
359 nv50_tex_validate(nv50);
360
361 if (nv50->dirty & NV50_NEW_ARRAYS)
362 nv50_vbo_validate(nv50);
363
364 nv50->state.dirty |= nv50->dirty;
365 nv50->dirty = 0;
366 nv50_state_emit(nv50);
367
368 return TRUE;
369 }
370