nv50_state_validate.c revision 67479eea365954396679714901c11175253bb13b
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
32static void
33nv50_state_validate_fb(struct nv50_context *nv50)
34{
35	struct nouveau_grobj *tesla = nv50->screen->tesla;
36	struct nouveau_stateobj *so = so_new(32, 79, 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, NV50TCL_RT_CONTROL, 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, NV50TCL_RT_ARRAY_MODE, 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, NV50TCL_ZETA_ENABLE, 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, NV50TCL_ZETA_ENABLE, 1);
135		so_data  (so, 0);
136	}
137
138	so_method(so, tesla, NV50TCL_VIEWPORT_HORIZ(0), 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_OFFSET_X, 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
158static void
159nv50_validate_samplers(struct nv50_context *nv50, struct nouveau_stateobj *so,
160		       unsigned p)
161{
162	struct nouveau_grobj *eng2d = nv50->screen->eng2d;
163	unsigned i, j, dw = nv50->sampler_nr[p] * 8;
164
165	if (!dw)
166		return;
167	nv50_so_init_sifc(nv50, so, nv50->screen->tsc, NOUVEAU_BO_VRAM,
168			  p * (32 * 8 * 4), dw * 4);
169
170	so_method(so, eng2d, NV50_2D_SIFC_DATA | (2 << 29), dw);
171
172	for (i = 0; i < nv50->sampler_nr[p]; ++i) {
173		if (nv50->sampler[p][i])
174			so_datap(so, nv50->sampler[p][i]->tsc, 8);
175		else {
176			for (j = 0; j < 8; ++j) /* you get punished */
177				so_data(so, 0); /* ... for leaving holes */
178		}
179	}
180}
181
182static void
183nv50_state_emit(struct nv50_context *nv50)
184{
185	struct nv50_screen *screen = nv50->screen;
186	struct nouveau_channel *chan = screen->base.channel;
187
188	/* XXX: this is racy for multiple contexts active on separate
189	 * threads.
190	 */
191	if (screen->cur_ctx != nv50) {
192		if (nv50->state.fb)
193			nv50->state.dirty |= NV50_NEW_FRAMEBUFFER;
194		if (nv50->state.blend)
195			nv50->state.dirty |= NV50_NEW_BLEND;
196		if (nv50->state.zsa)
197			nv50->state.dirty |= NV50_NEW_ZSA;
198		if (nv50->state.vertprog)
199			nv50->state.dirty |= NV50_NEW_VERTPROG;
200		if (nv50->state.fragprog)
201			nv50->state.dirty |= NV50_NEW_FRAGPROG;
202		if (nv50->state.geomprog)
203			nv50->state.dirty |= NV50_NEW_GEOMPROG;
204		if (nv50->state.rast)
205			nv50->state.dirty |= NV50_NEW_RASTERIZER;
206		if (nv50->state.blend_colour)
207			nv50->state.dirty |= NV50_NEW_BLEND_COLOUR;
208		if (nv50->state.stencil_ref)
209			nv50->state.dirty |= NV50_NEW_STENCIL_REF;
210		if (nv50->state.stipple)
211			nv50->state.dirty |= NV50_NEW_STIPPLE;
212		if (nv50->state.scissor)
213			nv50->state.dirty |= NV50_NEW_SCISSOR;
214		if (nv50->state.viewport)
215			nv50->state.dirty |= NV50_NEW_VIEWPORT;
216		if (nv50->state.tsc_upload)
217			nv50->state.dirty |= NV50_NEW_SAMPLER;
218		if (nv50->state.tic_upload)
219			nv50->state.dirty |= NV50_NEW_TEXTURE;
220		if (nv50->state.vtxfmt && nv50->state.vtxbuf)
221			nv50->state.dirty |= NV50_NEW_ARRAYS;
222		screen->cur_ctx = nv50;
223	}
224
225	if (nv50->state.dirty & NV50_NEW_FRAMEBUFFER)
226		so_emit(chan, nv50->state.fb);
227	if (nv50->state.dirty & NV50_NEW_BLEND)
228		so_emit(chan, nv50->state.blend);
229	if (nv50->state.dirty & NV50_NEW_ZSA)
230		so_emit(chan, nv50->state.zsa);
231	if (nv50->state.dirty & NV50_NEW_VERTPROG)
232		so_emit(chan, nv50->state.vertprog);
233	if (nv50->state.dirty & NV50_NEW_FRAGPROG)
234		so_emit(chan, nv50->state.fragprog);
235	if (nv50->state.dirty & NV50_NEW_GEOMPROG && nv50->state.geomprog)
236		so_emit(chan, nv50->state.geomprog);
237	if (nv50->state.dirty & (NV50_NEW_FRAGPROG | NV50_NEW_VERTPROG |
238				 NV50_NEW_GEOMPROG | NV50_NEW_RASTERIZER))
239		so_emit(chan, nv50->state.fp_linkage);
240	if ((nv50->state.dirty & (NV50_NEW_VERTPROG | NV50_NEW_GEOMPROG))
241	    && nv50->state.gp_linkage)
242		so_emit(chan, nv50->state.gp_linkage);
243	if (nv50->state.dirty & NV50_NEW_RASTERIZER)
244		so_emit(chan, nv50->state.rast);
245	if (nv50->state.dirty & NV50_NEW_BLEND_COLOUR)
246		so_emit(chan, nv50->state.blend_colour);
247	if (nv50->state.dirty & NV50_NEW_STENCIL_REF)
248		so_emit(chan, nv50->state.stencil_ref);
249	if (nv50->state.dirty & NV50_NEW_STIPPLE)
250		so_emit(chan, nv50->state.stipple);
251	if (nv50->state.dirty & NV50_NEW_SCISSOR)
252		so_emit(chan, nv50->state.scissor);
253	if (nv50->state.dirty & NV50_NEW_VIEWPORT)
254		so_emit(chan, nv50->state.viewport);
255	if (nv50->state.dirty & NV50_NEW_SAMPLER)
256		so_emit(chan, nv50->state.tsc_upload);
257	if (nv50->state.dirty & NV50_NEW_TEXTURE)
258		so_emit(chan, nv50->state.tic_upload);
259	if (nv50->state.dirty & NV50_NEW_ARRAYS) {
260		so_emit(chan, nv50->state.vtxfmt);
261		so_emit(chan, nv50->state.vtxbuf);
262		if (nv50->state.vtxattr)
263			so_emit(chan, nv50->state.vtxattr);
264	}
265	nv50->state.dirty = 0;
266}
267
268void
269nv50_state_flush_notify(struct nouveau_channel *chan)
270{
271	struct nv50_context *nv50 = chan->user_private;
272
273	if (nv50->state.tic_upload && !(nv50->dirty & NV50_NEW_TEXTURE))
274		so_emit(chan, nv50->state.tic_upload);
275
276	so_emit_reloc_markers(chan, nv50->state.fb);
277	so_emit_reloc_markers(chan, nv50->state.vertprog);
278	so_emit_reloc_markers(chan, nv50->state.fragprog);
279	so_emit_reloc_markers(chan, nv50->state.vtxbuf);
280	so_emit_reloc_markers(chan, nv50->screen->static_init);
281
282	if (nv50->state.instbuf)
283		so_emit_reloc_markers(chan, nv50->state.instbuf);
284}
285
286boolean
287nv50_state_validate(struct nv50_context *nv50)
288{
289	struct nouveau_grobj *tesla = nv50->screen->tesla;
290	struct nouveau_stateobj *so;
291	unsigned i;
292
293	if (nv50->dirty & NV50_NEW_FRAMEBUFFER)
294		nv50_state_validate_fb(nv50);
295
296	if (nv50->dirty & NV50_NEW_BLEND)
297		so_ref(nv50->blend->so, &nv50->state.blend);
298
299	if (nv50->dirty & NV50_NEW_ZSA)
300		so_ref(nv50->zsa->so, &nv50->state.zsa);
301
302	if (nv50->dirty & (NV50_NEW_VERTPROG | NV50_NEW_VERTPROG_CB))
303		nv50_vertprog_validate(nv50);
304
305	if (nv50->dirty & (NV50_NEW_FRAGPROG | NV50_NEW_FRAGPROG_CB))
306		nv50_fragprog_validate(nv50);
307
308	if (nv50->dirty & (NV50_NEW_GEOMPROG | NV50_NEW_GEOMPROG_CB))
309		nv50_geomprog_validate(nv50);
310
311	if (nv50->dirty & (NV50_NEW_FRAGPROG | NV50_NEW_VERTPROG |
312			   NV50_NEW_GEOMPROG | NV50_NEW_RASTERIZER))
313		nv50_fp_linkage_validate(nv50);
314
315	if (nv50->dirty & (NV50_NEW_GEOMPROG | NV50_NEW_VERTPROG))
316		nv50_gp_linkage_validate(nv50);
317
318	if (nv50->dirty & NV50_NEW_RASTERIZER)
319		so_ref(nv50->rasterizer->so, &nv50->state.rast);
320
321	if (nv50->dirty & NV50_NEW_BLEND_COLOUR) {
322		so = so_new(1, 4, 0);
323		so_method(so, tesla, NV50TCL_BLEND_COLOR(0), 4);
324		so_data  (so, fui(nv50->blend_colour.color[0]));
325		so_data  (so, fui(nv50->blend_colour.color[1]));
326		so_data  (so, fui(nv50->blend_colour.color[2]));
327		so_data  (so, fui(nv50->blend_colour.color[3]));
328		so_ref(so, &nv50->state.blend_colour);
329		so_ref(NULL, &so);
330	}
331
332	if (nv50->dirty & NV50_NEW_STENCIL_REF) {
333		so = so_new(2, 2, 0);
334		so_method(so, tesla, NV50TCL_STENCIL_FRONT_FUNC_REF, 1);
335		so_data  (so, nv50->stencil_ref.ref_value[0];
336		so_method(so, tesla, NV50TCL_STENCIL_BACK_FUNC_REF, 1);
337		so_data  (so, nv50->stencil_ref.ref_value[1];
338		so_ref(so, &nv50->state.stencil_ref);
339		so_ref(NULL, &so);
340	}
341
342	if (nv50->dirty & NV50_NEW_STIPPLE) {
343		so = so_new(1, 32, 0);
344		so_method(so, tesla, NV50TCL_POLYGON_STIPPLE_PATTERN(0), 32);
345		for (i = 0; i < 32; i++)
346			so_data(so, util_bswap32(nv50->stipple.stipple[i]));
347		so_ref(so, &nv50->state.stipple);
348		so_ref(NULL, &so);
349	}
350
351	if (nv50->dirty & (NV50_NEW_SCISSOR | NV50_NEW_RASTERIZER)) {
352		struct pipe_rasterizer_state *rast = &nv50->rasterizer->pipe;
353		struct pipe_scissor_state *s = &nv50->scissor;
354
355		if (nv50->state.scissor &&
356		    (rast->scissor == 0 && nv50->state.scissor_enabled == 0))
357			goto scissor_uptodate;
358		nv50->state.scissor_enabled = rast->scissor;
359
360		so = so_new(1, 2, 0);
361		so_method(so, tesla, NV50TCL_SCISSOR_HORIZ(0), 2);
362		if (nv50->state.scissor_enabled) {
363			so_data(so, (s->maxx << 16) | s->minx);
364			so_data(so, (s->maxy << 16) | s->miny);
365		} else {
366			so_data(so, (nv50->framebuffer.width << 16));
367			so_data(so, (nv50->framebuffer.height << 16));
368		}
369		so_ref(so, &nv50->state.scissor);
370		so_ref(NULL, &so);
371		nv50->state.dirty |= NV50_NEW_SCISSOR;
372	}
373scissor_uptodate:
374
375	if (nv50->dirty & (NV50_NEW_VIEWPORT | NV50_NEW_RASTERIZER)) {
376		unsigned bypass;
377
378		if (!nv50->rasterizer->pipe.bypass_vs_clip_and_viewport)
379			bypass = 0;
380		else
381			bypass = 1;
382
383		if (nv50->state.viewport &&
384		    (bypass || !(nv50->dirty & NV50_NEW_VIEWPORT)) &&
385		    nv50->state.viewport_bypass == bypass)
386			goto viewport_uptodate;
387		nv50->state.viewport_bypass = bypass;
388
389		so = so_new(5, 9, 0);
390		if (!bypass) {
391			so_method(so, tesla, NV50TCL_VIEWPORT_TRANSLATE_X(0), 3);
392			so_data  (so, fui(nv50->viewport.translate[0]));
393			so_data  (so, fui(nv50->viewport.translate[1]));
394			so_data  (so, fui(nv50->viewport.translate[2]));
395			so_method(so, tesla, NV50TCL_VIEWPORT_SCALE_X(0), 3);
396			so_data  (so, fui(nv50->viewport.scale[0]));
397			so_data  (so, fui(nv50->viewport.scale[1]));
398			so_data  (so, fui(nv50->viewport.scale[2]));
399
400			so_method(so, tesla, NV50TCL_VIEWPORT_TRANSFORM_EN, 1);
401			so_data  (so, 1);
402			/* 0x0000 = remove whole primitive only (xyz)
403			 * 0x1018 = remove whole primitive only (xy), clamp z
404			 * 0x1080 = clip primitive (xyz)
405			 * 0x1098 = clip primitive (xy), clamp z
406			 */
407			so_method(so, tesla, NV50TCL_VIEW_VOLUME_CLIP_CTRL, 1);
408			so_data  (so, 0x1080);
409			/* no idea what 0f90 does */
410			so_method(so, tesla, 0x0f90, 1);
411			so_data  (so, 0);
412		} else {
413			so_method(so, tesla, NV50TCL_VIEWPORT_TRANSFORM_EN, 1);
414			so_data  (so, 0);
415			so_method(so, tesla, NV50TCL_VIEW_VOLUME_CLIP_CTRL, 1);
416			so_data  (so, 0x0000);
417			so_method(so, tesla, 0x0f90, 1);
418			so_data  (so, 1);
419		}
420
421		so_ref(so, &nv50->state.viewport);
422		so_ref(NULL, &so);
423		nv50->state.dirty |= NV50_NEW_VIEWPORT;
424	}
425viewport_uptodate:
426
427	if (nv50->dirty & NV50_NEW_SAMPLER) {
428		unsigned nr = 0;
429
430		for (i = 0; i < PIPE_SHADER_TYPES; ++i)
431			nr += nv50->sampler_nr[i];
432
433		so = so_new(1 + 5 * PIPE_SHADER_TYPES,
434			    1 + 19 * PIPE_SHADER_TYPES + nr * 8,
435			    PIPE_SHADER_TYPES * 2);
436
437		nv50_validate_samplers(nv50, so, PIPE_SHADER_VERTEX);
438		nv50_validate_samplers(nv50, so, PIPE_SHADER_FRAGMENT);
439
440		so_method(so, tesla, 0x1334, 1); /* flush TSC */
441		so_data  (so, 0);
442
443		so_ref(so, &nv50->state.tsc_upload);
444		so_ref(NULL, &so);
445	}
446
447	if (nv50->dirty & (NV50_NEW_TEXTURE | NV50_NEW_SAMPLER))
448		nv50_tex_validate(nv50);
449
450	if (nv50->dirty & NV50_NEW_ARRAYS)
451		nv50_vbo_validate(nv50);
452
453	nv50->state.dirty |= nv50->dirty;
454	nv50->dirty = 0;
455	nv50_state_emit(nv50);
456
457	return TRUE;
458}
459
460void nv50_so_init_sifc(struct nv50_context *nv50,
461		       struct nouveau_stateobj *so,
462		       struct nouveau_bo *bo, unsigned reloc,
463		       unsigned offset, unsigned size)
464{
465	struct nouveau_grobj *eng2d = nv50->screen->eng2d;
466
467	reloc |= NOUVEAU_BO_WR;
468
469	so_method(so, eng2d, NV50_2D_DST_FORMAT, 2);
470	so_data  (so, NV50_2D_DST_FORMAT_R8_UNORM);
471	so_data  (so, 1);
472	so_method(so, eng2d, NV50_2D_DST_PITCH, 5);
473	so_data  (so, 262144);
474	so_data  (so, 65536);
475	so_data  (so, 1);
476	so_reloc (so, bo, offset, reloc | NOUVEAU_BO_HIGH, 0, 0);
477	so_reloc (so, bo, offset, reloc | NOUVEAU_BO_LOW, 0, 0);
478	so_method(so, eng2d, NV50_2D_SIFC_BITMAP_ENABLE, 2);
479	so_data  (so, 0);
480	so_data  (so, NV50_2D_SIFC_FORMAT_R8_UNORM);
481	so_method(so, eng2d, NV50_2D_SIFC_WIDTH, 10);
482	so_data  (so, size);
483	so_data  (so, 1);
484	so_data  (so, 0);
485	so_data  (so, 1);
486	so_data  (so, 0);
487	so_data  (so, 1);
488	so_data  (so, 0);
489	so_data  (so, 0);
490	so_data  (so, 0);
491	so_data  (so, 0);
492}
493