summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/nvfx/nvfx_state_emit.c
blob: 60ed2d0a4c61529c4182d141ea1677f6dde5749f (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
427
428
429
430
431
432
433
434
435
436
437
438
439
#include "nvfx_context.h"
#include "nvfx_state.h"
#include "nvfx_resource.h"
#include "draw/draw_context.h"

void
nvfx_state_viewport_validate(struct nvfx_context *nvfx)
{
	struct nouveau_channel *chan = nvfx->screen->base.channel;
	struct nouveau_grobj *eng3d = nvfx->screen->eng3d;
	struct pipe_viewport_state *vpt = &nvfx->viewport;

	if(nvfx->render_mode == HW) {
		BEGIN_RING(chan, eng3d, NV30_3D_VIEWPORT_TRANSLATE_X, 8);
		OUT_RINGf(chan, vpt->translate[0]);
		OUT_RINGf(chan, vpt->translate[1]);
		OUT_RINGf(chan, vpt->translate[2]);
		OUT_RINGf(chan, vpt->translate[3]);
		OUT_RINGf(chan, vpt->scale[0]);
		OUT_RINGf(chan, vpt->scale[1]);
		OUT_RINGf(chan, vpt->scale[2]);
		OUT_RINGf(chan, vpt->scale[3]);
		BEGIN_RING(chan, eng3d, 0x1d78, 1);
		OUT_RING(chan, 1);
	} else {
		BEGIN_RING(chan, eng3d, NV30_3D_VIEWPORT_TRANSLATE_X, 8);
		OUT_RINGf(chan, 0.0f);
		OUT_RINGf(chan, 0.0f);
		OUT_RINGf(chan, 0.0f);
		OUT_RINGf(chan, 0.0f);
		OUT_RINGf(chan, 1.0f);
		OUT_RINGf(chan, 1.0f);
		OUT_RINGf(chan, 1.0f);
		OUT_RINGf(chan, 1.0f);
		BEGIN_RING(chan, eng3d, 0x1d78, 1);
		OUT_RING(chan, nvfx->is_nv4x ? 0x110 : 1);
	}
}

void
nvfx_state_scissor_validate(struct nvfx_context *nvfx)
{
	struct nouveau_channel *chan = nvfx->screen->base.channel;
	struct nouveau_grobj *eng3d = nvfx->screen->eng3d;
	struct pipe_rasterizer_state *rast = &nvfx->rasterizer->pipe;
	struct pipe_scissor_state *s = &nvfx->scissor;

	if ((rast->scissor == 0 && nvfx->state.scissor_enabled == 0))
		return;
	nvfx->state.scissor_enabled = rast->scissor;

	BEGIN_RING(chan, eng3d, NV30_3D_SCISSOR_HORIZ, 2);
	if (nvfx->state.scissor_enabled) {
		OUT_RING(chan, ((s->maxx - s->minx) << 16) | s->minx);
		OUT_RING(chan, ((s->maxy - s->miny) << 16) | s->miny);
	} else {
		OUT_RING(chan, 4096 << 16);
		OUT_RING(chan, 4096 << 16);
	}
}

void
nvfx_state_sr_validate(struct nvfx_context *nvfx)
{
	struct nouveau_channel* chan = nvfx->screen->base.channel;
	struct nouveau_grobj *eng3d = nvfx->screen->eng3d;
	struct pipe_stencil_ref *sr = &nvfx->stencil_ref;

	BEGIN_RING(chan, eng3d, NV30_3D_STENCIL_FUNC_REF(0), 1);
	OUT_RING(chan, sr->ref_value[0]);
	BEGIN_RING(chan, eng3d, NV30_3D_STENCIL_FUNC_REF(1), 1);
	OUT_RING(chan, sr->ref_value[1]);
}

void
nvfx_state_blend_colour_validate(struct nvfx_context *nvfx)
{
	struct nouveau_channel* chan = nvfx->screen->base.channel;
	struct nouveau_grobj *eng3d = nvfx->screen->eng3d;
	struct pipe_blend_color *bcol = &nvfx->blend_colour;

	BEGIN_RING(chan, eng3d, NV30_3D_BLEND_COLOR, 1);
	OUT_RING(chan, ((float_to_ubyte(bcol->color[3]) << 24) |
		       (float_to_ubyte(bcol->color[0]) << 16) |
		       (float_to_ubyte(bcol->color[1]) <<  8) |
		       (float_to_ubyte(bcol->color[2]) <<  0)));
}

void
nvfx_state_stipple_validate(struct nvfx_context *nvfx)
{
	struct nouveau_channel *chan = nvfx->screen->base.channel;
	struct nouveau_grobj *eng3d = nvfx->screen->eng3d;

	BEGIN_RING(chan, eng3d, NV30_3D_POLYGON_STIPPLE_PATTERN(0), 32);
	OUT_RINGp(chan, nvfx->stipple, 32);
}

static void
nvfx_coord_conventions_validate(struct nvfx_context* nvfx)
{
	struct nouveau_channel* chan = nvfx->screen->base.channel;
	struct nouveau_grobj *eng3d = nvfx->screen->eng3d;
	unsigned value = nvfx->hw_fragprog->coord_conventions;
	if(value & NV30_3D_COORD_CONVENTIONS_ORIGIN_INVERTED)
		value |= nvfx->framebuffer.height << NV30_3D_COORD_CONVENTIONS_HEIGHT__SHIFT;

	BEGIN_RING(chan, eng3d, NV30_3D_COORD_CONVENTIONS, 1);
	OUT_RING(chan, value);
}

static void
nvfx_ucp_validate(struct nvfx_context* nvfx)
{
	struct nouveau_channel* chan = nvfx->screen->base.channel;
	struct nouveau_grobj *eng3d = nvfx->screen->eng3d;
	unsigned enables[] =
	{
			NV30_3D_VP_CLIP_PLANES_ENABLE_PLANE0,
			NV30_3D_VP_CLIP_PLANES_ENABLE_PLANE1,
			NV30_3D_VP_CLIP_PLANES_ENABLE_PLANE2,
			NV30_3D_VP_CLIP_PLANES_ENABLE_PLANE3,
			NV30_3D_VP_CLIP_PLANES_ENABLE_PLANE4,
			NV30_3D_VP_CLIP_PLANES_ENABLE_PLANE5
	};
	unsigned i, enable = 0, nr = 0;

	for (i = 0; i < 6; i++) {
		if (nvfx->rasterizer->pipe.clip_plane_enable & (1 << i)) {
			enable |= enables[i];
			nr = i+1;
		}
	}

	if(!nvfx->use_vp_clipping)
	{
		BEGIN_RING(chan, eng3d, NV30_3D_VP_CLIP_PLANES_ENABLE, 1);
		OUT_RING(chan, 0);

		BEGIN_RING(chan, eng3d, NV30_3D_VP_CLIP_PLANE(0, 0),
			   nr * 4);
		OUT_RINGp(chan, &nvfx->clip.ucp[0][0], nr * 4);
	}

	BEGIN_RING(chan, eng3d, NV30_3D_VP_CLIP_PLANES_ENABLE, 1);
	OUT_RING(chan, enable);
}

static void
nvfx_vertprog_ucp_validate(struct nvfx_context* nvfx)
{
	struct nouveau_channel* chan = nvfx->screen->base.channel;
	struct nouveau_grobj *eng3d = nvfx->screen->eng3d;
	unsigned i;
	struct nvfx_vertex_program* vp = nvfx->hw_vertprog;
	unsigned enable = nvfx->rasterizer->pipe.clip_plane_enable;
	unsigned nr = util_bitcount(enable);

	if(nr != vp->clip_nr)
	{
		unsigned idx;

		/* remove last instruction bit */
		if(vp->clip_nr >= 0)
		{
			idx = vp->nr_insns - 7 + vp->clip_nr;
			BEGIN_RING(chan, eng3d, NV30_3D_VP_UPLOAD_FROM_ID, 1);
			OUT_RING(chan,  vp->exec->start + idx);
			BEGIN_RING(chan, eng3d, NV30_3D_VP_UPLOAD_INST(0), 4);
			OUT_RINGp (chan, vp->insns[idx].data, 4);
		}

		 /* set last instruction bit */
		idx = vp->nr_insns - 7 + nr;
		BEGIN_RING(chan, eng3d, NV30_3D_VP_UPLOAD_FROM_ID, 1);
		OUT_RING(chan,  vp->exec->start + idx);
		BEGIN_RING(chan, eng3d, NV30_3D_VP_UPLOAD_INST(0), 4);
		OUT_RINGp(chan, vp->insns[idx].data, 3);
		OUT_RING(chan, vp->insns[idx].data[3] | 1);
		vp->clip_nr = nr;
	}

	// TODO: only do this for the ones changed
	for(i = 0; enable; ++i)
	{
		unsigned index = ffs(enable) - 1;
		enable &= ~(1 << index);

		BEGIN_RING(chan, eng3d, NV30_3D_VP_UPLOAD_CONST_ID, 5);
		OUT_RING(chan, vp->data->start + i);
		OUT_RINGp (chan, nvfx->clip.ucp[index], 4);
	}
}

static boolean
nvfx_state_validate_common(struct nvfx_context *nvfx)
{
	struct nouveau_channel* chan = nvfx->screen->base.channel;
	struct nouveau_grobj *eng3d = nvfx->screen->eng3d;
	unsigned dirty;
	unsigned still_dirty = 0;
	boolean flush_tex_cache = FALSE;

	if(nvfx != nvfx->screen->cur_ctx)
	{
		nvfx->dirty = ~0;
		nvfx->hw_vtxelt_nr = 16;
		nvfx->hw_pointsprite_control = -1;
		nvfx->hw_vp_output = -1;
		nvfx->screen->cur_ctx = nvfx;
		nvfx->relocs_needed = NVFX_RELOCATE_ALL;
	}

	dirty = nvfx->dirty;

	if(nvfx->render_mode == HW)
	{
		if(dirty & (NVFX_NEW_VERTPROG | NVFX_NEW_VERTCONST))
		{
			if(!nvfx_vertprog_validate(nvfx))
				return FALSE;
		}

		if(dirty & NVFX_NEW_ARRAYS)
		{
			if(!nvfx_vbo_validate(nvfx))
				return FALSE;
		}

		if(dirty & NVFX_NEW_INDEX)
		{
			if(nvfx->use_index_buffer)
				nvfx_idxbuf_validate(nvfx);
			else
				still_dirty = NVFX_NEW_INDEX;
		}
	}
	else
	{
		if(dirty & NVFX_NEW_VERTPROG) {
			assert(nvfx_vertprog_validate(nvfx));
			nvfx_vbo_swtnl_validate(nvfx);
		}
	}

	if(dirty & NVFX_NEW_SAMPLER) {
		/* XXX: we take the big hammer here, I have no idea why this is needed
		   to make this work properly */
		nvfx->dirty &= ~NVFX_NEW_SAMPLER;
		nvfx_fragtex_validate(nvfx);

		// TODO: only set this if really necessary
		flush_tex_cache = TRUE;
	}

	if(dirty & NVFX_NEW_RAST)
		sb_emit(chan, nvfx->rasterizer->sb, nvfx->rasterizer->sb_len);

	if(dirty & NVFX_NEW_SCISSOR)
		nvfx_state_scissor_validate(nvfx);

	if(dirty & NVFX_NEW_STIPPLE)
		nvfx_state_stipple_validate(nvfx);

       if(nvfx->dirty & (NVFX_NEW_UCP | NVFX_NEW_RAST))
	       nvfx_ucp_validate(nvfx);

	if(nvfx->use_vp_clipping && (nvfx->dirty &
			                     (NVFX_NEW_UCP | NVFX_NEW_VERTPROG |
			                      NVFX_NEW_RAST)))
		nvfx_vertprog_ucp_validate(nvfx);

	if(dirty & (NVFX_NEW_FRAGPROG | NVFX_NEW_FRAGCONST |
			    NVFX_NEW_VERTPROG | NVFX_NEW_SPRITE))
	{
		nvfx_fragprog_validate(nvfx);
		if(dirty & NVFX_NEW_FRAGPROG)
			flush_tex_cache = TRUE; // TODO: do we need this?
	}

	if(nvfx->is_nv4x)
	{
		unsigned vp_output = nvfx->hw_vertprog->or | nvfx->hw_fragprog->or;
		vp_output |= ((1 << (nvfx->rasterizer->pipe.clip_plane_enable & 63)) - 1) << 6;

		if(vp_output != nvfx->hw_vp_output)
		{
			BEGIN_RING(chan, eng3d, NV40_3D_VP_RESULT_EN, 1);
			OUT_RING(chan, vp_output);
			nvfx->hw_vp_output = vp_output;
		}
	}

	if(dirty & NVFX_NEW_FB)
		nvfx_framebuffer_validate(nvfx);

	if(dirty & NVFX_NEW_BLEND)
		sb_emit(chan, nvfx->blend->sb, nvfx->blend->sb_len);

	if(dirty & NVFX_NEW_BCOL)
		nvfx_state_blend_colour_validate(nvfx);

	if(dirty & NVFX_NEW_ZSA)
		sb_emit(chan, nvfx->zsa->sb, nvfx->zsa->sb_len);

	if(dirty & NVFX_NEW_SR)
		nvfx_state_sr_validate(nvfx);

	/* XXX: nv3x needs viewport revalidation after RAST or ZSA change */
	if(dirty & (NVFX_NEW_VIEWPORT | NVFX_NEW_RAST | NVFX_NEW_ZSA))
	{
		nvfx_state_viewport_validate(nvfx);
	}

	if(dirty & (NVFX_NEW_ZSA | NVFX_NEW_FB))
	{
		BEGIN_RING(chan, eng3d, NV30_3D_DEPTH_WRITE_ENABLE, 2);
		OUT_RING(chan, nvfx->framebuffer.zsbuf &&
                       nvfx->zsa->pipe.depth.writemask);
	    OUT_RING(chan, nvfx->framebuffer.zsbuf &&
                       nvfx->zsa->pipe.depth.enabled);
	}

	if(dirty & (NVFX_NEW_FRAGPROG | NVFX_NEW_FB))
		nvfx_coord_conventions_validate(nvfx);

	if(flush_tex_cache && nvfx->is_nv4x)
	{
		BEGIN_RING(chan, eng3d, NV40_3D_TEX_CACHE_CTL, 1);
		OUT_RING(chan, 2);
		BEGIN_RING(chan, eng3d, NV40_3D_TEX_CACHE_CTL, 1);
		OUT_RING(chan, 1);
	}

	nvfx->dirty = dirty & still_dirty;

	return TRUE;
}

inline void
nvfx_state_relocate(struct nvfx_context *nvfx, unsigned relocs)
{
	struct nouveau_channel* chan = nvfx->screen->base.channel;
	/* we need to ensure there is enough space to output relocations in one go */
	const unsigned max_relocs = 0
	      + 16 /* vertex buffers, incl. dma flag */
	      + 2 /* index buffer plus format+dma flag */
	      + 2 * 5 /* 4 cbufs + zsbuf, plus dma objects */
	      + 2 * 16 /* fragment textures plus format+dma flag */
	      + 2 * 4 /* vertex textures plus format+dma flag */
	      + 1 /* fragprog incl dma flag */
	      ;

	MARK_RING(chan, max_relocs * 2, max_relocs * 2);

	if(relocs & NVFX_RELOCATE_FRAMEBUFFER)
		nvfx_framebuffer_relocate(nvfx);
	if(relocs & NVFX_RELOCATE_FRAGTEX)
		nvfx_fragtex_relocate(nvfx);
	if(relocs & NVFX_RELOCATE_FRAGPROG)
		nvfx_fragprog_relocate(nvfx);
	if(relocs & NVFX_RELOCATE_VTXBUF)
		nvfx_vbo_relocate(nvfx);
	if(relocs & NVFX_RELOCATE_IDXBUF)
		nvfx_idxbuf_relocate(nvfx);
}

boolean
nvfx_state_validate(struct nvfx_context *nvfx)
{
	if (nvfx->render_mode != HW) {
		/* Don't even bother trying to go back to hw if none
		 * of the states that caused swtnl previously have changed.
		 */
		if ((nvfx->fallback_swtnl & nvfx->dirty)
				!= nvfx->fallback_swtnl)
			return FALSE;

		/* Attempt to go to hwtnl again */
		nvfx->dirty |= (NVFX_NEW_VIEWPORT |
				NVFX_NEW_VERTPROG |
				NVFX_NEW_ARRAYS);
		nvfx->render_mode = HW;
	}

	if(!nvfx_state_validate_common(nvfx))
		return FALSE;

	return TRUE;
}

boolean
nvfx_state_validate_swtnl(struct nvfx_context *nvfx)
{
	struct draw_context *draw = nvfx->draw;

	/* Setup for swtnl */
	if (nvfx->render_mode == HW) {
		static boolean warned = FALSE;
		if(!warned) {
			NOUVEAU_ERR("hw->swtnl 0x%08x\n", nvfx->fallback_swtnl);
			warned = TRUE;
		}
                nvfx->pipe.flush(&nvfx->pipe, NULL);
		nvfx->dirty |= (NVFX_NEW_VIEWPORT |
				NVFX_NEW_VERTPROG |
				NVFX_NEW_ARRAYS);
		nvfx->render_mode = SWTNL;
	}

	if (nvfx->draw_dirty & NVFX_NEW_VERTPROG) {
		if(!nvfx->vertprog->draw_vs)
			nvfx->vertprog->draw_vs = draw_create_vertex_shader(draw, &nvfx->vertprog->pipe);
		draw_bind_vertex_shader(draw, nvfx->vertprog->draw_vs);
	}

	if (nvfx->draw_dirty & NVFX_NEW_RAST)
           draw_set_rasterizer_state(draw, &nvfx->rasterizer->pipe,
                                     nvfx->rasterizer);

	if (nvfx->draw_dirty & NVFX_NEW_UCP)
		draw_set_clip_state(draw, &nvfx->clip);

	if (nvfx->draw_dirty & NVFX_NEW_VIEWPORT)
		draw_set_viewport_state(draw, &nvfx->viewport);

	if (nvfx->draw_dirty & NVFX_NEW_ARRAYS) {
		draw_set_vertex_buffers(draw, nvfx->vtxbuf_nr, nvfx->vtxbuf);
		draw_set_vertex_elements(draw, nvfx->vtxelt->num_elements, nvfx->vtxelt->pipe);
	}

	if (nvfx->draw_dirty & NVFX_NEW_INDEX)
		draw_set_index_buffer(draw, &nvfx->idxbuf);

	nvfx_state_validate_common(nvfx);

	nvfx->draw_dirty = 0;
	return TRUE;
}