nv50_state_validate.c revision c10fb9579027ae34eda0c52acb353e8da5832495
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
26static void
27nv50_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		if (!gw) {
36			w = fb->cbufs[i]->width;
37			h = fb->cbufs[i]->height;
38			gw = 1;
39		} else {
40			assert(w == fb->cbufs[i]->width);
41			assert(h == fb->cbufs[i]->height);
42		}
43
44		so_method(so, tesla, NV50TCL_RT_HORIZ(i), 2);
45		so_data  (so, fb->cbufs[i]->width);
46		so_data  (so, fb->cbufs[i]->height);
47
48		so_method(so, tesla, NV50TCL_RT_ADDRESS_HIGH(i), 5);
49		so_reloc (so, nv50_surface_buffer(fb->cbufs[i]), fb->cbufs[i]->offset,
50			  NOUVEAU_BO_VRAM | NOUVEAU_BO_HIGH |
51			  NOUVEAU_BO_RDWR, 0, 0);
52		so_reloc (so, nv50_surface_buffer(fb->cbufs[i]), fb->cbufs[i]->offset,
53			  NOUVEAU_BO_VRAM | NOUVEAU_BO_LOW |
54			  NOUVEAU_BO_RDWR, 0, 0);
55		switch (fb->cbufs[i]->format) {
56		case PIPE_FORMAT_A8R8G8B8_UNORM:
57			so_data(so, 0xcf);
58			break;
59		case PIPE_FORMAT_R5G6B5_UNORM:
60			so_data(so, 0xe8);
61			break;
62		default:
63			NOUVEAU_ERR("AIIII unknown format %s\n",
64				    pf_name(fb->cbufs[i]->format));
65			so_data(so, 0xe6);
66			break;
67		}
68		so_data(so, 0x00000000);
69		so_data(so, 0x00000000);
70
71		so_method(so, tesla, 0x1224, 1);
72		so_data  (so, 1);
73	}
74
75	if (fb->zsbuf) {
76		if (!gw) {
77			w = fb->zsbuf->width;
78			h = fb->zsbuf->height;
79			gw = 1;
80		} else {
81			assert(w == fb->zsbuf->width);
82			assert(h == fb->zsbuf->height);
83		}
84
85		so_method(so, tesla, NV50TCL_ZETA_ADDRESS_HIGH, 5);
86		so_reloc (so, nv50_surface_buffer(fb->zsbuf), fb->zsbuf->offset,
87			  NOUVEAU_BO_VRAM | NOUVEAU_BO_HIGH |
88			  NOUVEAU_BO_RDWR, 0, 0);
89		so_reloc (so, nv50_surface_buffer(fb->zsbuf), fb->zsbuf->offset,
90			  NOUVEAU_BO_VRAM | NOUVEAU_BO_LOW |
91			  NOUVEAU_BO_RDWR, 0, 0);
92		switch (fb->zsbuf->format) {
93		case PIPE_FORMAT_Z24S8_UNORM:
94			so_data(so, 0x16);
95			break;
96		case PIPE_FORMAT_Z16_UNORM:
97			so_data(so, 0x15);
98			break;
99		default:
100			NOUVEAU_ERR("AIIII unknown format %s\n",
101				    pf_name(fb->zsbuf->format));
102			so_data(so, 0x16);
103			break;
104		}
105		so_data(so, 0x00000000);
106		so_data(so, 0x00000000);
107
108		so_method(so, tesla, 0x1538, 1);
109		so_data  (so, 1);
110		so_method(so, tesla, 0x1228, 3);
111		so_data  (so, fb->zsbuf->width);
112		so_data  (so, fb->zsbuf->height);
113		so_data  (so, 0x00010001);
114	}
115
116	so_method(so, tesla, NV50TCL_VIEWPORT_HORIZ, 2);
117	so_data  (so, w << 16);
118	so_data  (so, h << 16);
119	so_method(so, tesla, 0x0e04, 2);
120	so_data  (so, w << 16);
121	so_data  (so, h << 16);
122	so_method(so, tesla, 0xdf8, 2);
123	so_data  (so, 0);
124	so_data  (so, h);
125
126	so_ref(so, &nv50->state.fb);
127}
128
129static void
130nv50_state_emit(struct nv50_context *nv50)
131{
132	struct nv50_screen *screen = nv50->screen;
133	struct nouveau_winsys *nvws = screen->nvws;
134
135	if (nv50->pctx_id != screen->cur_pctx) {
136		nv50->state.dirty |= 0xffffffff;
137		screen->cur_pctx = nv50->pctx_id;
138	}
139
140	if (nv50->state.dirty & NV50_NEW_FRAMEBUFFER)
141		so_emit(nvws, nv50->state.fb);
142	if (nv50->state.dirty & NV50_NEW_BLEND)
143		so_emit(nvws, nv50->state.blend);
144	if (nv50->state.dirty & NV50_NEW_ZSA)
145		so_emit(nvws, nv50->state.zsa);
146	if (nv50->state.dirty & NV50_NEW_VERTPROG)
147		so_emit(nvws, nv50->state.vertprog);
148	if (nv50->state.dirty & NV50_NEW_FRAGPROG)
149		so_emit(nvws, nv50->state.fragprog);
150	if (nv50->state.dirty & NV50_NEW_RASTERIZER)
151		so_emit(nvws, nv50->state.rast);
152	if (nv50->state.dirty & NV50_NEW_BLEND_COLOUR)
153		so_emit(nvws, nv50->state.blend_colour);
154	if (nv50->state.dirty & NV50_NEW_STIPPLE)
155		so_emit(nvws, nv50->state.stipple);
156	if (nv50->state.dirty & NV50_NEW_SCISSOR)
157		so_emit(nvws, nv50->state.scissor);
158	if (nv50->state.dirty & NV50_NEW_VIEWPORT)
159		so_emit(nvws, nv50->state.viewport);
160	if (nv50->state.dirty & NV50_NEW_SAMPLER)
161		so_emit(nvws, nv50->state.tsc_upload);
162	if (nv50->state.dirty & NV50_NEW_TEXTURE)
163		so_emit(nvws, nv50->state.tic_upload);
164	if (nv50->state.dirty & NV50_NEW_ARRAYS) {
165		so_emit(nvws, nv50->state.vtxfmt);
166		so_emit(nvws, nv50->state.vtxbuf);
167	}
168	nv50->state.dirty = 0;
169
170	so_emit_reloc_markers(nvws, nv50->state.fb);
171	so_emit_reloc_markers(nvws, nv50->state.vertprog);
172	so_emit_reloc_markers(nvws, nv50->state.fragprog);
173	so_emit_reloc_markers(nvws, nv50->state.vtxbuf);
174	so_emit_reloc_markers(nvws, nv50->screen->static_init);
175}
176
177boolean
178nv50_state_validate(struct nv50_context *nv50)
179{
180	const struct pipe_framebuffer_state *fb = &nv50->framebuffer;
181	struct nouveau_grobj *tesla = nv50->screen->tesla;
182	struct nouveau_stateobj *so;
183	unsigned i;
184
185	for (i = 0; i < fb->nr_cbufs; i++)
186		fb->cbufs[i]->status = PIPE_SURFACE_STATUS_DEFINED;
187
188	if (fb->zsbuf)
189		fb->zsbuf->status = PIPE_SURFACE_STATUS_DEFINED;
190
191	if (nv50->dirty & NV50_NEW_FRAMEBUFFER)
192		nv50_state_validate_fb(nv50);
193
194	if (nv50->dirty & NV50_NEW_BLEND)
195		so_ref(nv50->blend->so, &nv50->state.blend);
196
197	if (nv50->dirty & NV50_NEW_ZSA)
198		so_ref(nv50->zsa->so, &nv50->state.zsa);
199
200	if (nv50->dirty & (NV50_NEW_VERTPROG | NV50_NEW_VERTPROG_CB))
201		nv50_vertprog_validate(nv50);
202
203	if (nv50->dirty & (NV50_NEW_FRAGPROG | NV50_NEW_FRAGPROG_CB))
204		nv50_fragprog_validate(nv50);
205
206	if (nv50->dirty & NV50_NEW_RASTERIZER)
207		so_ref(nv50->rasterizer->so, &nv50->state.rast);
208
209	if (nv50->dirty & NV50_NEW_BLEND_COLOUR) {
210		so = so_new(5, 0);
211		so_method(so, tesla, NV50TCL_BLEND_COLOR(0), 4);
212		so_data  (so, fui(nv50->blend_colour.color[0]));
213		so_data  (so, fui(nv50->blend_colour.color[1]));
214		so_data  (so, fui(nv50->blend_colour.color[2]));
215		so_data  (so, fui(nv50->blend_colour.color[3]));
216		so_ref(so, &nv50->state.blend_colour);
217	}
218
219	if (nv50->dirty & NV50_NEW_STIPPLE) {
220		so = so_new(33, 0);
221		so_method(so, tesla, NV50TCL_POLYGON_STIPPLE_PATTERN(0), 32);
222		for (i = 0; i < 32; i++)
223			so_data(so, nv50->stipple.stipple[i]);
224		so_ref(so, &nv50->state.stipple);
225	}
226
227	if (nv50->dirty & (NV50_NEW_SCISSOR | NV50_NEW_RASTERIZER)) {
228		struct pipe_rasterizer_state *rast = &nv50->rasterizer->pipe;
229		struct pipe_scissor_state *s = &nv50->scissor;
230
231		if (nv50->state.scissor &&
232		    (rast->scissor == 0 && nv50->state.scissor_enabled == 0))
233			goto scissor_uptodate;
234		nv50->state.scissor_enabled = rast->scissor;
235
236		so = so_new(3, 0);
237		so_method(so, tesla, 0x0ff4, 2);
238		if (nv50->state.scissor_enabled) {
239			so_data(so, ((s->maxx - s->minx) << 16) | s->minx);
240			so_data(so, ((s->maxy - s->miny) << 16) | s->miny);
241		} else {
242			so_data(so, (8192 << 16));
243			so_data(so, (8192 << 16));
244		}
245		so_ref(so, &nv50->state.scissor);
246		nv50->state.dirty |= NV50_NEW_SCISSOR;
247	}
248scissor_uptodate:
249
250	if (nv50->dirty & NV50_NEW_VIEWPORT) {
251		unsigned bypass;
252
253		if (!nv50->rasterizer->pipe.bypass_clipping)
254			bypass = 0;
255		else
256			bypass = 1;
257
258		if (nv50->state.viewport &&
259		    (bypass || !(nv50->dirty & NV50_NEW_VIEWPORT)) &&
260		    nv50->state.viewport_bypass == bypass)
261			goto viewport_uptodate;
262		nv50->state.viewport_bypass = bypass;
263
264		so = so_new(12, 0);
265		if (!bypass) {
266			so_method(so, tesla, NV50TCL_VIEWPORT_UNK1(0), 3);
267			so_data  (so, fui(nv50->viewport.translate[0]));
268			so_data  (so, fui(nv50->viewport.translate[1]));
269			so_data  (so, fui(nv50->viewport.translate[2]));
270			so_method(so, tesla, NV50TCL_VIEWPORT_UNK0(0), 3);
271			so_data  (so, fui(nv50->viewport.scale[0]));
272			so_data  (so, fui(-nv50->viewport.scale[1]));
273			so_data  (so, fui(nv50->viewport.scale[2]));
274			so_method(so, tesla, 0x192c, 1);
275			so_data  (so, 1);
276			so_method(so, tesla, 0x0f90, 1);
277			so_data  (so, 0);
278		} else {
279			so_method(so, tesla, 0x192c, 1);
280			so_data  (so, 0);
281			so_method(so, tesla, 0x0f90, 1);
282			so_data  (so, 1);
283		}
284
285		so_ref(so, &nv50->state.viewport);
286	}
287viewport_uptodate:
288
289	if (nv50->dirty & NV50_NEW_SAMPLER) {
290		int i;
291
292		so = so_new(nv50->sampler_nr * 8 + 3, 0);
293		so_method(so, tesla, 0x0f00, 1);
294		so_data  (so, NV50_CB_TSC);
295		so_method(so, tesla, 0x40000f04, nv50->sampler_nr * 8);
296		for (i = 0; i < nv50->sampler_nr; i++)
297			so_datap (so, nv50->sampler[i], 8);
298		so_ref(so, &nv50->state.tsc_upload);
299	}
300
301	if (nv50->dirty & NV50_NEW_TEXTURE)
302		nv50_tex_validate(nv50);
303
304	if (nv50->dirty & NV50_NEW_ARRAYS)
305		nv50_vbo_validate(nv50);
306
307	nv50->state.dirty |= nv50->dirty;
308	nv50->dirty = 0;
309	nv50_state_emit(nv50);
310
311	return TRUE;
312}
313
314