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