summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/nv20/nv20_state_emit.c
blob: 6bbd1fdae9df47463017d31cded3f9408b7afe31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#include "nv20_context.h"
#include "nv20_state.h"
#include "draw/draw_context.h"

static void nv20_state_emit_blend(struct nv20_context* nv20)
{
	struct nv20_blend_state *b = nv20->blend;
	struct nv20_screen *screen = nv20->screen;
	struct nouveau_channel *chan = screen->base.channel;
	struct nouveau_grobj *kelvin = screen->kelvin;

	BEGIN_RING(chan, kelvin, NV20TCL_DITHER_ENABLE, 1);
	OUT_RING  (chan, b->d_enable);

	BEGIN_RING(chan, kelvin, NV20TCL_BLEND_FUNC_ENABLE, 1);
	OUT_RING  (chan, b->b_enable);

	BEGIN_RING(chan, kelvin, NV20TCL_BLEND_FUNC_SRC, 2);
	OUT_RING  (chan, b->b_srcfunc);
	OUT_RING  (chan, b->b_dstfunc);

	BEGIN_RING(chan, kelvin, NV20TCL_COLOR_MASK, 1);
	OUT_RING  (chan, b->c_mask);
}

static void nv20_state_emit_blend_color(struct nv20_context* nv20)
{
	struct pipe_blend_color *c = nv20->blend_color;
	struct nv20_screen *screen = nv20->screen;
	struct nouveau_channel *chan = screen->base.channel;
	struct nouveau_grobj *kelvin = screen->kelvin;

	BEGIN_RING(chan, kelvin, NV20TCL_BLEND_COLOR, 1);
	OUT_RING  (chan,
		   (float_to_ubyte(c->color[3]) << 24)|
		   (float_to_ubyte(c->color[0]) << 16)|
		   (float_to_ubyte(c->color[1]) << 8) |
		   (float_to_ubyte(c->color[2]) << 0));
}

static void nv20_state_emit_rast(struct nv20_context* nv20)
{
	struct nv20_rasterizer_state *r = nv20->rast;
	struct nv20_screen *screen = nv20->screen;
	struct nouveau_channel *chan = screen->base.channel;
	struct nouveau_grobj *kelvin = screen->kelvin;

	BEGIN_RING(chan, kelvin, NV20TCL_SHADE_MODEL, 2);
	OUT_RING  (chan, r->shade_model);
	OUT_RING  (chan, r->line_width);


	BEGIN_RING(chan, kelvin, NV20TCL_POINT_SIZE, 1);
	OUT_RING  (chan, r->point_size);

	BEGIN_RING(chan, kelvin, NV20TCL_POLYGON_MODE_FRONT, 2);
	OUT_RING  (chan, r->poly_mode_front);
	OUT_RING  (chan, r->poly_mode_back);


	BEGIN_RING(chan, kelvin, NV20TCL_CULL_FACE, 2);
	OUT_RING  (chan, r->cull_face);
	OUT_RING  (chan, r->front_face);

	BEGIN_RING(chan, kelvin, NV20TCL_LINE_SMOOTH_ENABLE, 2);
	OUT_RING  (chan, r->line_smooth_en);
	OUT_RING  (chan, r->poly_smooth_en);

	BEGIN_RING(chan, kelvin, NV20TCL_CULL_FACE_ENABLE, 1);
	OUT_RING  (chan, r->cull_face_en);
}

static void nv20_state_emit_dsa(struct nv20_context* nv20)
{
	struct nv20_depth_stencil_alpha_state *d = nv20->dsa;
	struct nv20_screen *screen = nv20->screen;
	struct nouveau_channel *chan = screen->base.channel;
	struct nouveau_grobj *kelvin = screen->kelvin;

	BEGIN_RING(chan, kelvin, NV20TCL_DEPTH_FUNC, 1);
	OUT_RING (chan, d->depth.func);

	BEGIN_RING(chan, kelvin, NV20TCL_DEPTH_WRITE_ENABLE, 1);
	OUT_RING (chan, d->depth.write_enable);

	BEGIN_RING(chan, kelvin, NV20TCL_DEPTH_TEST_ENABLE, 1);
	OUT_RING (chan, d->depth.test_enable);

	BEGIN_RING(chan, kelvin, NV20TCL_DEPTH_UNK17D8, 1);
	OUT_RING (chan, 1);

#if 0
	BEGIN_RING(chan, kelvin, NV20TCL_STENCIL_ENABLE, 1);
	OUT_RING (chan, d->stencil.enable);
	BEGIN_RING(chan, kelvin, NV20TCL_STENCIL_MASK, 7);
	OUT_RINGp (chan, (uint32_t *)&(d->stencil.wmask), 7);
#endif

	BEGIN_RING(chan, kelvin, NV20TCL_ALPHA_FUNC_ENABLE, 1);
	OUT_RING (chan, d->alpha.enabled);

	BEGIN_RING(chan, kelvin, NV20TCL_ALPHA_FUNC_FUNC, 1);
	OUT_RING (chan, d->alpha.func);

	BEGIN_RING(chan, kelvin, NV20TCL_ALPHA_FUNC_REF, 1);
	OUT_RING (chan, d->alpha.ref);
}

static void nv20_state_emit_viewport(struct nv20_context* nv20)
{
}

static void nv20_state_emit_scissor(struct nv20_context* nv20)
{
	/* NV20TCL_SCISSOR_* is probably a software method */
/*	struct pipe_scissor_state *s = nv20->scissor;
	struct nv20_screen *screen = nv20->screen;
	struct nouveau_channel *chan = screen->base.channel;
	struct nouveau_grobj *kelvin = screen->kelvin;

	BEGIN_RING(chan, kelvin, NV20TCL_SCISSOR_HORIZ, 2);
	OUT_RING  (chan, ((s->maxx - s->minx) << 16) | s->minx);
	OUT_RING  (chan, ((s->maxy - s->miny) << 16) | s->miny);*/
}

static void nv20_state_emit_framebuffer(struct nv20_context* nv20)
{
	struct pipe_framebuffer_state* fb = nv20->framebuffer;
	struct nv04_surface *rt, *zeta = NULL;
	uint32_t rt_format, w, h;
	int colour_format = 0, zeta_format = 0;
	struct nv20_miptree *nv20mt = 0;
	struct nv20_screen *screen = nv20->screen;
	struct nouveau_channel *chan = screen->base.channel;
	struct nouveau_grobj *kelvin = screen->kelvin;

	w = fb->cbufs[0]->width;
	h = fb->cbufs[0]->height;
	colour_format = fb->cbufs[0]->format;
	rt = (struct nv04_surface *)fb->cbufs[0];

	if (fb->zsbuf) {
		if (colour_format) {
			assert(w == fb->zsbuf->width);
			assert(h == fb->zsbuf->height);
		} else {
			w = fb->zsbuf->width;
			h = fb->zsbuf->height;
		}

		zeta_format = fb->zsbuf->format;
		zeta = (struct nv04_surface *)fb->zsbuf;
	}

	rt_format = NV20TCL_RT_FORMAT_TYPE_LINEAR | 0x20;

	switch (colour_format) {
	case PIPE_FORMAT_X8R8G8B8_UNORM:
		rt_format |= NV20TCL_RT_FORMAT_COLOR_X8R8G8B8;
		break;
	case PIPE_FORMAT_A8R8G8B8_UNORM:
	case 0:
		rt_format |= NV20TCL_RT_FORMAT_COLOR_A8R8G8B8;
		break;
	case PIPE_FORMAT_R5G6B5_UNORM:
		rt_format |= NV20TCL_RT_FORMAT_COLOR_R5G6B5;
		break;
	default:
		assert(0);
	}

	if (zeta) {
		BEGIN_RING(chan, kelvin, NV20TCL_RT_PITCH, 1);
		OUT_RING  (chan, rt->pitch | (zeta->pitch << 16));
	} else {
		BEGIN_RING(chan, kelvin, NV20TCL_RT_PITCH, 1);
		OUT_RING  (chan, rt->pitch | (rt->pitch << 16));
	}

	nv20mt = (struct nv20_miptree *)rt->base.texture;
	nv20->rt[0] = nv20mt->buffer;

	if (zeta_format)
	{
		nv20mt = (struct nv20_miptree *)zeta->base.texture;
		nv20->zeta = nv20mt->buffer;
	}

	BEGIN_RING(chan, kelvin, NV20TCL_RT_HORIZ, 3);
	OUT_RING  (chan, (w << 16) | 0);
	OUT_RING  (chan, (h << 16) | 0); /*NV20TCL_RT_VERT */
	OUT_RING  (chan, rt_format); /* NV20TCL_RT_FORMAT */
	BEGIN_RING(chan, kelvin, NV20TCL_VIEWPORT_CLIP_HORIZ(0), 2);
	OUT_RING  (chan, ((w - 1) << 16) | 0);
	OUT_RING  (chan, ((h - 1) << 16) | 0);
}

static void nv20_vertex_layout(struct nv20_context *nv20)
{
	struct nv20_fragment_program *fp = nv20->fragprog.current;
	struct draw_context *dc = nv20->draw;
	int src;
	int i;
	struct vertex_info *vinfo = &nv20->vertex_info;
	const enum interp_mode colorInterp = INTERP_LINEAR;
	boolean colors[2] = { FALSE };
	boolean generics[12] = { FALSE };
	boolean fog = FALSE;

	memset(vinfo, 0, sizeof(*vinfo));

	/*
	 * Assumed NV20 hardware vertex attribute order:
	 * 0 position, 1 ?, 2 ?, 3 col0,
	 * 4 col1?, 5 ?, 6 ?, 7 ?,
	 * 8 ?, 9 tex0, 10 tex1, 11 tex2,
	 * 12 tex3, 13 ?, 14 ?, 15 ?
	 * unaccounted: wgh, nor, fog
	 * There are total 16 attrs.
	 * vinfo->hwfmt[0] has a used-bit corresponding to each of these.
	 * relation to TGSI_SEMANTIC_*:
	 * - POSITION: position (always used)
	 * - COLOR: col1, col0
	 * - GENERIC: tex3, tex2, tex1, tex0, normal, weight
	 * - FOG: fog
	 */

	for (i = 0; i < fp->info.num_inputs; i++) {
		int isn = fp->info.input_semantic_name[i];
		int isi = fp->info.input_semantic_index[i];
		switch (isn) {
		case TGSI_SEMANTIC_POSITION:
			break;
		case TGSI_SEMANTIC_COLOR:
			assert(isi < 2);
			colors[isi] = TRUE;
			break;
		case TGSI_SEMANTIC_GENERIC:
			assert(isi < 12);
			generics[isi] = TRUE;
			break;
		case TGSI_SEMANTIC_FOG:
			fog = TRUE;
			break;
		default:
			assert(0 && "unknown input_semantic_name");
		}
	}

	/* always do position */ {
		src = draw_find_shader_output(dc, TGSI_SEMANTIC_POSITION, 0);
		draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_LINEAR, src);
		vinfo->hwfmt[0] |= (1 << 0);
	}

	/* two unnamed generics */
	for (i = 4; i < 6; i++) {
		if (!generics[i])
			continue;
		src = draw_find_shader_output(dc, TGSI_SEMANTIC_GENERIC, i);
		draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE, src);
		vinfo->hwfmt[0] |= (1 << (i - 3));
	}

	if (colors[0]) {
		src = draw_find_shader_output(dc, TGSI_SEMANTIC_COLOR, 0);
		draw_emit_vertex_attr(vinfo, EMIT_4F, colorInterp, src);
		vinfo->hwfmt[0] |= (1 << 3);
	}

	if (colors[1]) {
		src = draw_find_shader_output(dc, TGSI_SEMANTIC_COLOR, 1);
		draw_emit_vertex_attr(vinfo, EMIT_4F, colorInterp, src);
		vinfo->hwfmt[0] |= (1 << 4);
	}

	/* four unnamed generics */
	for (i = 6; i < 10; i++) {
		if (!generics[i])
			continue;
		src = draw_find_shader_output(dc, TGSI_SEMANTIC_GENERIC, i);
		draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE, src);
		vinfo->hwfmt[0] |= (1 << (i - 1));
	}

	/* tex0, tex1, tex2, tex3 */
	for (i = 0; i < 4; i++) {
		if (!generics[i])
			continue;
		src = draw_find_shader_output(dc, TGSI_SEMANTIC_GENERIC, i);
		draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE, src);
		vinfo->hwfmt[0] |= (1 << (i + 9));
	}

	/* two unnamed generics */
	for (i = 10; i < 12; i++) {
		if (!generics[i])
			continue;
		src = draw_find_shader_output(dc, TGSI_SEMANTIC_GENERIC, i);
		draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE, src);
		vinfo->hwfmt[0] |= (1 << (i + 3));
	}

	if (fog) {
		src = draw_find_shader_output(dc, TGSI_SEMANTIC_FOG, 0);
		draw_emit_vertex_attr(vinfo, EMIT_1F, INTERP_PERSPECTIVE, src);
		vinfo->hwfmt[0] |= (1 << 15);
	}

	draw_compute_vertex_size(vinfo);
}

void
nv20_emit_hw_state(struct nv20_context *nv20)
{
	struct nv20_screen *screen = nv20->screen;
	struct nouveau_channel *chan = screen->base.channel;
	struct nouveau_grobj *kelvin = screen->kelvin;
	struct nouveau_bo *rt_bo;
	int i;

	if (nv20->dirty & NV20_NEW_VERTPROG) {
		//nv20_vertprog_bind(nv20, nv20->vertprog.current);
		nv20->dirty &= ~NV20_NEW_VERTPROG;
	}

	if (nv20->dirty & NV20_NEW_FRAGPROG) {
		nv20_fragprog_bind(nv20, nv20->fragprog.current);
		/*XXX: clear NV20_NEW_FRAGPROG if no new program uploaded */
		nv20->dirty_samplers |= (1<<10);
		nv20->dirty_samplers = 0;
	}

	if (nv20->dirty_samplers || (nv20->dirty & NV20_NEW_FRAGPROG)) {
		nv20_fragtex_bind(nv20);
		nv20->dirty &= ~NV20_NEW_FRAGPROG;
	}

	if (nv20->dirty & NV20_NEW_VTXARRAYS) {
		nv20->dirty &= ~NV20_NEW_VTXARRAYS;
		nv20_vertex_layout(nv20);
		nv20_vtxbuf_bind(nv20);
	}

	if (nv20->dirty & NV20_NEW_BLEND) {
		nv20->dirty &= ~NV20_NEW_BLEND;
		nv20_state_emit_blend(nv20);
	}

	if (nv20->dirty & NV20_NEW_BLENDCOL) {
		nv20->dirty &= ~NV20_NEW_BLENDCOL;
		nv20_state_emit_blend_color(nv20);
	}

	if (nv20->dirty & NV20_NEW_RAST) {
		nv20->dirty &= ~NV20_NEW_RAST;
		nv20_state_emit_rast(nv20);
	}

	if (nv20->dirty & NV20_NEW_DSA) {
		nv20->dirty &= ~NV20_NEW_DSA;
		nv20_state_emit_dsa(nv20);
	}

 	if (nv20->dirty & NV20_NEW_VIEWPORT) {
		nv20->dirty &= ~NV20_NEW_VIEWPORT;
		nv20_state_emit_viewport(nv20);
	}

 	if (nv20->dirty & NV20_NEW_SCISSOR) {
		nv20->dirty &= ~NV20_NEW_SCISSOR;
		nv20_state_emit_scissor(nv20);
	}

 	if (nv20->dirty & NV20_NEW_FRAMEBUFFER) {
		nv20->dirty &= ~NV20_NEW_FRAMEBUFFER;
		nv20_state_emit_framebuffer(nv20);
	}

	/* Emit relocs for every referenced buffer.
	 * This is to ensure the bufmgr has an accurate idea of how
	 * the buffer is used.  This isn't very efficient, but we don't
	 * seem to take a significant performance hit.  Will be improved
	 * at some point.  Vertex arrays are emitted by nv20_vbo.c
	 */

	/* Render target */
	rt_bo = nouveau_bo(nv20->rt[0]);
	BEGIN_RING(chan, kelvin, NV20TCL_DMA_COLOR, 1);
	OUT_RELOCo(chan, rt_bo, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
	BEGIN_RING(chan, kelvin, NV20TCL_COLOR_OFFSET, 1);
	OUT_RELOCl(chan, rt_bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);

	if (nv20->zeta) {
		struct nouveau_bo *zeta_bo = nouveau_bo(nv20->zeta);
		BEGIN_RING(chan, kelvin, NV20TCL_DMA_ZETA, 1);
		OUT_RELOCo(chan, zeta_bo, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
		BEGIN_RING(chan, kelvin, NV20TCL_ZETA_OFFSET, 1);
		OUT_RELOCl(chan, zeta_bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
		/* XXX for when we allocate LMA on nv17 */
/*		BEGIN_RING(chan, kelvin, NV10TCL_LMA_DEPTH_BUFFER_OFFSET, 1);
		OUT_RELOCl(chan, nouveau_bo(nv20->zeta + lma_offset));*/
	}

	/* Vertex buffer */
	BEGIN_RING(chan, kelvin, NV20TCL_DMA_VTXBUF0, 1);
	OUT_RELOCo(chan, rt_bo, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
	BEGIN_RING(chan, kelvin, NV20TCL_COLOR_OFFSET, 1);
	OUT_RELOCl(chan, rt_bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);

	/* Texture images */
	for (i = 0; i < 2; i++) {
		if (!(nv20->fp_samplers & (1 << i)))
			continue;
		struct nouveau_bo *bo = nouveau_bo(nv20->tex[i].buffer);
		BEGIN_RING(chan, kelvin, NV20TCL_TX_OFFSET(i), 1);
		OUT_RELOCl(chan, bo, 0, NOUVEAU_BO_VRAM |
			   NOUVEAU_BO_GART | NOUVEAU_BO_RD);
		BEGIN_RING(chan, kelvin, NV20TCL_TX_FORMAT(i), 1);
		OUT_RELOCd(chan, bo, nv20->tex[i].format,
			   NOUVEAU_BO_VRAM | NOUVEAU_BO_GART | NOUVEAU_BO_RD |
			   NOUVEAU_BO_OR, NV20TCL_TX_FORMAT_DMA0,
			   NV20TCL_TX_FORMAT_DMA1);
	}
}